API Reference: Interfaces

API Reference: Interfaces

For initialization order, runtime lifecycle, and embedding patterns, see docs/bootstrap.md.

Package path: packages/local-ai-core/.

app.py — AppContext

create_app(...)

def create_app(
    config_path: str | None = None,
    *,
    config: Config | None = None,
    approval_gate: ApprovalGate | None = None,
    require_approval: bool | None = None,
) -> AppContext
Argument Description
config_path Path to config.yaml when config is not passed
config Pre-loaded Config (e.g. after CLI overrides)
approval_gate Optional gate for MCP tools with requires_approval
require_approval When True (default), enforce the gate for those tools; False skips approval

Raises ModelPlanError if the model cannot fit in available RAM/VRAM (single-model path).

AppContext

Defined in core/app_context.py:

@dataclass
class AppContext:
    config: Config
    supervisor: Any  # ProcessSupervisor
    llm_client: Any  # LocalLLMClient | EnsembleRouter
    agent_loop: AgentLoop
    session_runner: SessionRunner
    runtime_guard: DirectoryGuard
    workspace_guard: DirectoryGuard | None = None
    memory_stack: Any | None = None
    skills_stack: Any | None = None
    ensemble_stack: Any | None = None
    monitor_stack: Any | None = None
    degradation: GracefulDegradation | None = None
    autotune_stack: Any | None = None
    mcp_stack: Any | None = None
    supervisors: list[Any] = field(default_factory=list)
    background_tasks: BackgroundTaskRegistry = field(default_factory=BackgroundTaskRegistry)
    shutdown_event: asyncio.Event = field(default_factory=asyncio.Event)

    @property
    def guard(self) -> DirectoryGuard:  # alias for runtime_guard
        ...

    async def start_supervisors(self) -> None  # all llama-server processes (+ autotune schedule)
    async def startup(self) -> None             # memory index, monitor, MCP discover-all
    async def shutdown(self) -> None            # tasks → watchers → MCP → supervisors → LLM client

See docs/bootstrap.md for the full create_app() wiring order and field table.


interfaces/cli.py — CLI

Entry point: local-ai cli [options]

Function Purpose
run_repl(ctx, user_id, session_id, agent_name, *, resume=False) Interactive REPL
run_oneshot(ctx, user_id, session_id, agent_name, user_input, *, resume=False, thumbs=None) Single query
main(args) Sync CLI entry (preflight → create_app → start_supervisors → startup → run)
add_cli_arguments(parser) Registers CLI flags and config overrides

interfaces/stdio_server.py — Stdio protocol

Entry point: local-ai serve-stdio [--config config.yaml]

NDJSON protocol documented in docs/stdio_protocol.md.


interfaces/http_server.py — HTTP API

Entry point: local-ai serve-http [--config config.yaml] (requires local-ai-core[http])

OpenAI-compatible FastAPI server. Config: top-level http: (host, port, bearer_token). See docs/http_api.md.

Function Purpose
create_http_app(...) Build FastAPI app (inject HttpRuntime for tests)
add_serve_http_arguments(parser) Registers --host / --port / --bearer-token / --mcp-config
main(args) Sync entry (preflight → create_app → supervisors → uvicorn)

interfaces/health_cmd.py — Health check

Entry point: local-ai health [--config config.yaml]

Quick runtime check: llama-server reachability, circuit breaker state, optional memory index status.


config/overrides.py — CLI config overrides

def apply_cli_overrides(config: Config, args: argparse.Namespace) -> Config

Only non-None CLI flags override matching config.yaml values. Used by interfaces/cli.py before create_app().


llama-tools CLI (standalone)

Planner, doctor, plan, inspect, and benchmark live in the llama-tools package:

Command Purpose
llama-tools doctor System and model diagnostics
llama-tools plan Launch plan preview
llama-tools inspect <model.gguf> GGUF metadata and recommendations
llama-tools benchmark Performance benchmark

agents/registry.py — get_agent_config

def get_agent_config(config: Config, agent_name: str = "assistant") -> AgentConfig

Returns agent config or raises KeyError for unknown agent names.

Memory RAG categories are configured globally via memory.categories (not per-agent).


Usage pattern

from agents.registry import get_agent_config
from app import create_app, default_agent_name
from config.loader import load_config
from core.shutdown import install_signal_handlers

async def main():
    config = load_config("config.yaml")
    ctx = create_app(config=config)
    install_signal_handlers(ctx)
    await ctx.start_supervisors()
    await ctx.startup()
    try:
        agent_name = default_agent_name(ctx.config)
        agent_config = get_agent_config(ctx.config, agent_name)
        result = await ctx.session_runner.run(
            "user1", "session_123", "Hello", agent_config, agent_name=agent_name
        )
        print(result.response)
    finally:
        await ctx.shutdown()