Skip to content

refactor(ai): one plain Model record for every kind - #68585

Merged
rekram1-node merged 2 commits into
devfrom
ai-plain-model
Dec 6, 2026
Merged

rekram1-node merged 2 commits into
devfrom
ai-plain-model

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #

Part 1 of 2 for the plain Model plan. This PR is the plain record only and keeps today's auth wiring. Provider-owned Auth and Secret come in the follow-up.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Every model is now one plain record, Model<K>, indexed by Api.KindMap. It replaces the seven per-kind classes (LanguageModel, ImageModel, VideoModel, SpeechModel, TranscriptionModel, DecisionModel, EmbeddingModel) and the MediaModel base class. The record holds kind, id, provider, and the api the model is reached through, with that host's connection. Language models also carry options, capabilities, compatibility, limits, cost and modalities. The other kinds carry transport and native, and decision models carry compatibility.

- export class ImageModel<Options> extends MediaModel<ImageRoute, Options> { static fromRoute(...) }
- export const ImageModelSchema = Schema.declare((v): v is ImageModel => v instanceof ImageModel)
+ export type ImageModel<Options extends ImageOptions = ImageOptions> = Model<"image", Options>
+ export const ImageModelSchema = Schema.declare((v): v is ImageModel => isModel(v, "image"))

A model's api is a narrow view: name, kind, with, and read-only baseURL?: string and metadataKey?: string. The record behind it (codec, transport, auth handler, compaction operations, query, deferred base URL) sits under a module-private symbol. Only the package-private internal(api) in src/api-internal.ts reads it; it isn't exported from @opencode/ai/internal or any public entry.

- const metadataKey = Api.record(request.model.api).metadataKey ?? request.model.provider
+ const metadataKey = request.model.api.metadataKey ?? request.model.provider   // public
- const compact = Api.record(model.api).compact
+ const compact = internal(model.api).compact                                    // package-private

internal checks the symbol at runtime instead of casting, and an API used as built (OpenAIChat.api, @opencode/ai/apis) is recognized by its codec. view(record, modelID) builds the view; baseURL is set only when it's already a plain string, so the public type never lies about an Effect or a per-model function. Provider.make reads its APIs through internal too, so the last view-to-record cast in provider.ts is gone.

Model types carry a plain CompactionCapability ({ endpoint?: true; trigger?: true }) instead of CompactionOperations, so the public .d.ts never references RequestExecutor. ai.compact and ai.canCompact keep their compile-time and runtime checks.

Media, decision and embedding models used to store a composed route with closures. The client now builds that route from model.api on each call (routeOf/jsonRouteOf in media-model.ts), so the model stores only facts. Api.bind, Api.record, DecisionRoute.make, EmbeddingRoute.make and every fromRoute are deleted.

ai.image.generate(request)
  route = protocol of model.api, renamed to model.api.name, at model.api.path / baseURL(model.id), auth from model.api
  inline → generate · stream → stream + collect · queued → start/poll
decision / embedding: same, as one JSON inline route (decision downloads URL media first; embedding splits by maxValues)

The order of headers and query is unchanged: API headers and query sit below the model's transport, which sits below the request's.

Core moves to the record in the same PR. It reads only public fields, with no behavior change:

  • model-resolver.ts: instanceof checks, Schema.is(*ModelSchema) and the prototype copy in fromCatalogMedia become kind checks and a spread. Chat's keyless override becomes apiKey: false up front, the way loadMedia already did it; AI SDK packages are skipped because their custom transport never applied that auth. The native-compaction check uses ai.canCompact.
  • session/* reads model.api.metadataKey and model.api.baseURL.
  • Core tests check auth headers on the request a model actually sends, through the existing RequestExecutor export, instead of reading the API record. No new /internal exports.

Net: +1435 / −1872 (−437 lines), of which the narrow-view amendment is +326 / −257.

Public changes

Accepted direction: one plain record whose public types show no internal machinery. Everything that changes for callers:

  • LanguageModel, ImageModel, VideoModel, SpeechModel, TranscriptionModel, DecisionModel and EmbeddingModel are now type aliases of Model<K>, not classes. instanceof, new and the promise.SpeechModel-style value exports are gone. LanguageModel.make, .update and .input remain as functions. *ModelSchema remain and check kind.
  • Every model has a kind field, and models are plain objects. JSON.stringify(model) shows only the API's name, kind, baseURL and metadataKey.
  • Media, decision and embedding models no longer have route. Read model.api.name (was route.id).
  • Removed: ImageModel.fromRoute, SpeechModel.fromRoute, TranscriptionModel.fromRoute, VideoModel.fromRoute, DecisionModel.make, EmbeddingModel.make, DecisionModelRoute, EmbeddingModelRoute, and the ImageRoute / SpeechRoute / TranscriptionRoute / VideoRoute types.
  • model.api (and every Api.Ref-based view a model holds) gains read-only baseURL?: string and metadataKey?: string. It still doesn't show the codec, transport, auth, query or compaction.
  • The Compact type parameter on LanguageModel, LanguageApi and ProviderPackage is now CompactionCapability ({ endpoint?: true; trigger?: true }), not CompactionOperations.
  • New exports: the Model and CompactionCapability types.
  • @opencode/ai/internal (no compatibility promise): Api.record, Api.bind, Api.MediaBinding, Api.JsonBinding and Api.MediaModels are removed. KindMap entries lose model. DecisionRoute.make, EmbeddingRoute.make and MediaRoute.ModelInput are removed. Nothing is added.
  • @opencode/ai/experimental: ExtractionModel is a plain interface with kind: "extraction", not a class.

Not changed: provider model(id, settings) package paths and settings, wire bytes, packages/plugin types.

How did you verify your code works?

  • A local request snapshot covers every ./providers/* entrypoint plus seven settings variants. It records the API, metadata key, options, URL, non-auth headers, body and wire bytes. The snapshot was regenerated on origin/dev and is unchanged after this PR (130/130). It isn't committed.
  • packages/ai: bun run typecheck, bun run typecheck:tsc, bun test: 2560 pass. The emitted model.d.ts has no protocol, RequestExecutor, CompactionOperations or auth definition. Its one transport is the existing transport?: TransportOptions HTTP-settings field on media models.
  • packages/core: typecheck:tsc; model-resolver, aisdk, session-native-compaction, session-model-request and generate tests: 138 pass.
  • packages/plugin typecheck; root bun run check.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: @opencode/ai: model classes become plain Model<K> records with kind and a narrow api view; fromRoute/make/route and Api.record/bind are removed; Compact type params become CompactionCapability (breaking for library callers; provider entrypoints and wire format unchanged)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound. I ran the packages/ai tests (2430 pass) and typecheck:tsc for both packages/ai and packages/core (both pass). I also ran the model-resolver, aisdk and native-compaction tests in packages/core (104 pass) and an end-to-end TUI turn against the simulated model from this checkout.

In the code, the order of headers and query is unchanged: the API's query and headers still sit below the model's transport and then the request's, and the query now goes through Composition.query. Moving keyless chat to apiKey: false up front gives the same Auth.none result as the old override.

Public API (@opencode/ai), not backwards compatible for library callers:

  • Models become plain records: LanguageModel, ImageModel, VideoModel, SpeechModel, TranscriptionModel, DecisionModel and EmbeddingModel change from classes to type aliases of the new Model<K>. instanceof and new stop working, and the promise.*Model value exports are gone. *ModelSchema now checks kind.
  • Removed: *Model.fromRoute, DecisionModel.make, EmbeddingModel.make, DecisionModelRoute, EmbeddingModelRoute, and the ImageRoute / SpeechRoute / TranscriptionRoute / VideoRoute types.
  • model.route is gone: read model.api.name (was route.id) and model.api.protocol.kind (was route.kind).
  • model.api widens: on language models it is now the full Api.Http, so with takes the full patch.
  • Added: every model has a kind field, and the Model type is exported.
  • @opencode/ai/internal: Api.record, Api.bind, Api.MediaBinding, Api.JsonBinding, Api.MediaModels, DecisionRoute.make, EmbeddingRoute.make and MediaRoute.ModelInput are removed.
  • @opencode/ai/experimental: ExtractionModel becomes a plain interface with kind: "extraction".

Callers that construct models directly or use instanceof must switch to provider accessors and kind checks. Provider model(id, settings) entrypoints and their settings, wire bytes, the SDK, the server's HTTP API, the plugin API, config and the CLI are unchanged.

No blocking concerns.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing from my earlier review was left open, and I found nothing new that should block this. I checked the new commit by running packages/ai bun test (2430 pass) and typecheck:tsc for both packages/ai and packages/core (both pass). I also ran the core model-resolver, aisdk, native-compaction and model-request tests (116 pass) and a TUI turn against the simulated model from this checkout.

The narrow view holds up in the code: every reader inside packages/ai that needs the full record goes through internal(). The symbol-keyed record survives { ...model } spreads, and records passed as they were built (OpenAIChat.api, @opencode/ai/apis) still work through the codec check.

Public API change since the last review (@opencode/ai):

  • model.api on language models changes from the full Api.Http record to a narrow view with name, kind, with, and read-only baseURL?: string and metadataKey?: string. Media, decision and embedding models get the same view with these read-only fields.
  • The Compact type parameter on LanguageModel, LanguageApi and ProviderPackage changes from CompactionOperations to the new exported CompactionCapability ({ endpoint?: true; trigger?: true }).

The rest of the earlier breaking changes for library callers stay as described: class to plain record, removed fromRoute/make/route, and removed Api.record/bind. Provider entrypoints, settings and wire format are unchanged. Nothing changes in the SDK, the server's HTTP API, the plugin API, config or the CLI.

No blocking concerns.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This push rebases the PR onto the latest dev with no new behaviour. The only change to the PR's own commits is updating the imports for #68578's tryRequest → requestParser rename. On the new head, packages/ai bun test passes (2430) and so does typecheck:tsc for packages/ai and packages/core. The core model-resolver, aisdk, native-compaction and model-request tests also pass (116).

The public API impact is the same as in my last review. In @opencode/ai, models become plain Model<K> records with a narrow api view, fromRoute/make/route and Api.record/bind are removed, and the Compact type parameters become CompactionCapability. This breaks library callers; provider entrypoints and the wire format are unchanged.

No new concerns.

@rekram1-node
rekram1-node merged commit 0dd5b35 into dev Dec 6, 2026
10 checks passed
@rekram1-node
rekram1-node deleted the ai-plain-model branch December 6, 2026 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant