/** * @file Keyless on-device execution path for a `local` TierCandidate, the * non-CLI complement to `spawn.mts`. Where `spawnAiAgent` shells out to an * installed agent CLI, this drives an on-device LanguageModel and normalizes * the reply into the SAME `AgentSpawnResult` shape, so a caller (or * `spawnTierWithFallback`) treats a local rung exactly like a CLI rung. * Injection, exactly like `ai/exec.mts`: the lib OWNS the * `LocalAgentProvider` interface and ships a thin built-in provider over the * `builtin.mts` LanguageModel factory (`getLanguageModel()`), while the HEAVY * provider — a real on-device model such as odai — is INJECTED by the caller * and never imported here. That keeps the small-dist lib free of any model * dependency: a consumer that never routes local pays nothing, and the local * engine is discovered lazily through the injection point. */ import type { LanguageModelAvailability, LanguageModelFactory } from './builtin.js'; import type { AgentSpawnResult, SpawnAiAgentOptions } from './types.js'; /** * A minimal Prompt-API session: `prompt(input)` resolves to the model's reply * text. Matches the shape a `LanguageModelFactory.create()` session exposes * across Chrome and the native Node builds. Kept structural so any conforming * on-device session satisfies it, injected sessions included. */ export interface LocalModelSession { prompt(input: unknown, options?: unknown | undefined): Promise; } /** * Inputs to a single local generation. Mirrors the CLI spawn surface a `local` * candidate needs: the prompt, the working directory (advisory for a local * engine), the model id from the tier candidate, and an optional timeout / * abort signal. */ export interface LocalSpawnOptions { readonly prompt: string; readonly cwd: string; readonly model?: string | undefined; readonly signal?: AbortSignal | undefined; readonly timeoutMs?: number | undefined; } /** * The injectable keyless-local primitive: probe availability, then generate a * completion as plain text. The lib provides `builtinLocalProvider`; a caller * injects a heavier implementation backed by a real on-device model that * satisfies this same interface. `generate` MUST resolve with the reply text * or throw, never return a sentinel, so `spawnLocalAgent` can normalize both * paths. */ export interface LocalAgentProvider { availability(): Promise; generate(options: LocalSpawnOptions): Promise; } /** * The built-in keyless provider: drives the `builtin.mts` LanguageModel factory * (`getLanguageModel()` unless a factory is injected for tests). `availability` * forwards the factory's own probe (or `unavailable` when no local engine * resolves); `generate` creates a session and prompts it. Zero heavy deps: the * factory itself is resolved lazily and may be absent, which surfaces as * `unavailable` rather than a throw on the probe path. */ export declare function builtinLocalProvider(factory?: LanguageModelFactory | undefined): LocalAgentProvider; /** * Probe whether the keyless local engine is usable, for building * `RouteContext.localAvailable`. Availability `'available'` is the only usable * state: `downloadable`/`downloading`/`unavailable` all mean "not ready now". * Never throws: any probe failure resolves to `false` so a caller can fan this * out alongside its `which` / credential probes without a try/catch. */ export declare function isLocalEngineAvailable(provider?: LocalAgentProvider | undefined): Promise; /** * True when `value` is a usable Prompt-API session (has a callable `prompt`). */ export declare function isLocalModelSession(value: unknown): value is LocalModelSession; /** * Tier-orchestration entry: run a `local` candidate for * `spawnTierWithFallback`. Maps the shared spawn options plus the candidate's * model onto a `LocalSpawnOptions`, defaults the provider to the built-in * LanguageModel injection point, and normalizes prototype-polluted input. Kept * here (not inline in spawn.mts) so the null-proto normalization and provider * default live beside the local path they belong to. */ export declare function runLocalTierSpawn(options: Pick, model: string, provider?: LocalAgentProvider | undefined): Promise; /** * Run a `local` candidate through a `LocalAgentProvider`, normalizing the reply * into the SAME `AgentSpawnResult` a CLI spawn returns. Contract mirrors * `spawnAiAgent`: never throws, always resolves to a result the caller branches * on via `exitCode`. * * - Availability other than `'available'` sets `unavailable: true` with a * non-zero exit, the same signal `spawnTierWithFallback` uses to fall over to * the next rung. * - A generation throw yields a non-zero exit with the message on `stderr`; this * is a genuine failure on a reachable engine, so `unavailable` stays false * rather than silently downgrading a real failure. * - Success returns the reply text on `stdout` with exit 0. * * A local engine has no retry/overload/quota surface, so `attempts` is always 1 * and `overloaded` always false. */ export declare function spawnLocalAgent(options: LocalSpawnOptions, provider: LocalAgentProvider): Promise;