Skip to main content

0024: Provider adapters, typed streams and a shared context engine

  • Status: Proposed (implemented in Phase 2, task 017)
  • Date: 2026-12-05

Context​

Phase 1 had one OpenAI-compatible adapter that streamed text, and a request built from the selection only. Chat, inline edits and agents need more providers (official OpenAI, Anthropic, local servers), tool calls, usage, and context beyond the selection: files, nearby code, diagnostics, search results, debugger state. Each provider's stream and tool-call schema differ. Context must stay bounded, private and reviewable, and the user must approve exactly what is sent.

Decision​

  • One adapter per wire format, all behind AiProvider: Chat Completions (kinds openai, openai-compatible, local) and Anthropic Messages. Kinds differ only where the APIs do (auth header, max_completion_tokens vs max_tokens, required output limit, version header). local requires a loopback URL and never starts a runtime.
  • Typed stream events (text, complete tool call, usage, stop reason) replace text chunks. Tool calls are assembled from fragments with bounds and strict checks; invalid calls fail the request.
  • Exact bodies. wire::body is a pure function of kind, model, capabilities and request. The preview shows its output and the adapter sends the same bytes.
  • Capabilities are configuration. No model names are built in and nothing is inferred from a name. Requests needing a missing capability are refused; nothing falls back to another model.
  • Usage is actual or unknown; local estimates are labelled. Cost only from actual usage and user-configured prices.
  • Errors are classified (credentials, rate limit, model unavailable, timeout, outage, protocol) and only transient ones are retried, once, before any output.
  • A shared context engine in ide-ai asks the core through typed ContextRequest calls, ranks by fixed tiers, retrieves lazily within item, byte, token and time budgets, deduplicates, redacts, and records provenance and omissions. It builds no index of its own.
  • Safety in layers. The core refuses ignored and secret files and paths outside the canonical workspace root before reading; the engine checks paths again and redacts; items are framed as untrusted data; debugger state needs its own grant; every item is re-read at send and a change makes the request stale.
  • History is bounded and redacted; persisted only on opt-in, to a private file, and deleted on clear.

Alternatives considered​

  • A provider SDK per vendor. Rust SDKs are unofficial or async-runtime based; two small adapters over the existing blocking ureq transport keep one cancellation and bounding model.
  • Treating Anthropic as OpenAI-compatible. Its compatibility layer exists but differs in tools and usage; the native API was chosen so events and errors are exact.
  • Embeddings and a vector index. Useful for large workspaces, but adds a second index, storage and a model dependency. The engine reuses search, syntax and diagnostics instead; retrieval quality is bounded by those.
  • Regex redaction with a dependency. The heuristics are simple enough to hand-write without one.

Consequences​

  • AiEvent gains ToolCall, Usage and Stopped; consumers use is_terminal().
  • Prepared carries the model request, exact body, bundle and sources; the preview shows destination, omissions, an estimate and the body.
  • Definitions and references from language servers are asynchronous and not yet context sources; the syntax-based enclosing definition is used instead.
  • Git changes are an empty slot until there is version-control integration.
  • Gemini is evaluated (OpenAI-compatible endpoint, beta) but not a supported kind.