ADR 0013: Client-side hosted LLM adapters
- Status: Partially superseded by ADR 0037
Historical scope: references to the retired native-local language-model route are superseded by ADR 0037, which also keeps automatic mode local; clause 9 states that rule. The hosted-provider decisions below remain accepted.
- Date: 2026-07-27
- Amended by: ADR 0035 for remote HTTPS OpenAI-compatible transport; loopback development remains browser-side.
- Amended by: ADR 0036 for the provider-neutral ordered and bounded stream envelope.
Context
Sourdaw currently constructs one Anthropic client directly in the browser. The agentic command boundary must also support OpenAI and arbitrary OpenAI-compatible providers while preserving WebLLM. A Sourdaw AI/provider server is explicitly out of scope for this phase.
Provider APIs differ in authentication, message streaming, and tool-call response shapes. Those differences must terminate inside repositories; use cases and the LLM action bridge must remain provider-neutral.
Browser-held API keys are inherently less protected than server-held credentials. This phase is intended for local bring-your-own-key testing and must not silently turn volatile credentials into durable application state.
Decision
The volatile hosted-LLM session owns one discriminated provider runtime:
anthropic: Anthropic Messages API through repository-owned transports: the official SDK for structured tool planning, and authenticated fetch/SSE with explicit request, response, event, and cancellation bounds for chat streaming.openai: OpenAI Chat Completions through the official HTTPS endpoint.openai-compatible: a user-supplied Chat Completions base URL and model.
Hosted adapters implement the same two capabilities: structured tool planning and chat text streaming. Provider-specific request and response formats do not escape the repository layer.
Configuration follows these rules:
- API keys, provider runtime objects, and active request controllers remain in memory only.
- Reconfiguration or removal aborts every active hosted request before replacing credentials.
- Keys are never logged or included in application errors.
- Model identifiers remain user-configurable rather than being treated as permanent constants.
- Custom base URLs require HTTPS, except loopback HTTP endpoints used for local development.
- Browser CORS or provider-policy failures are surfaced as failures; the app does not bypass browser security controls.
- Credential drafts are cleared when their provider or compatible endpoint changes, preventing a key from being sent to a different destination.
- UI consumers observe one AiRuntime-owned, non-secret provider-status projection; credentials and provider client objects never enter that store.
- Users can explicitly select WebLLM or hosted execution. Explicit selection is exclusive and never silently falls through to another provider. Automatic mode uses browser WebLLM only, fails closed when WebLLM is unavailable, and never routes a prompt to a hosted provider; remote execution requires the user's explicit selection of a hosted provider.
- Anthropic and OpenAI require credentials. OpenAI-compatible endpoints may be auth-free; in that case no
Authorizationheader is sent. - Tool planning rejects token-limited, refused, malformed, or otherwise incomplete provider envelopes before any proposed call reaches the action bridge.
- Credential removal or reconfiguration is a terminal cancellation, distinct from user Stop and ordinary provider failure.
- Stream adapters require the provider protocol's terminal event or finish reason; a transport that ends without one fails instead of being accepted as complete.
- Every chat consumer receives the terminal outcome. Structured-data consumers reject incomplete output before parsing or mutation; human-readable chat preserves partial text but marks it incomplete.
- Changing backend preference aborts active generation and backend initialization, releases in-progress WebLLM worker resources, and prevents stale readiness from the previous selection.
- Hosted credentials are shown as configured, not connected or ready; hosted readiness is established only by a successful provider request, never by configuration alone.
- Unavailable explicit selections report the missing capability or configuration instead of a generic GPU failure.
- UI surfaces own an abort signal for every hosted request and abort it on close or replacement so stale tokens cannot update a later run.
- Cancelled edit planning terminalizes its assistant message and restores the prior ready status only when it remains compatible with the selected backend.
- WebLLM initialization is attempt-owned. Only a live same-model attempt may be coalesced; superseded attempts cannot clear or overwrite a replacement attempt's worker, promise, engine, model, or status.
- Coalesced WebLLM callers own independent waits; one caller's abort cancels the shared initialization only when no live caller remains.
The OpenAI-compatible adapter uses the widely implemented /chat/completions tool-calling contract in this phase. Provider-specific APIs such as OpenAI Responses may be admitted later behind the same repository capability boundary.
Consequences
- Anthropic, OpenAI, and compatible local/hosted gateways share one command path.
- No server, credential persistence, or provider-specific project mutation path is introduced.
- Some providers or gateways will not be browser-callable until they explicitly allow the app origin.
- A future server can replace credential transport without changing action validation or DAW execution.