Repository navigation
refactor(ai): one plain Model record for every kind - #68585
Conversation
There was a problem hiding this comment.
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,DecisionModelandEmbeddingModelchange from classes to type aliases of the newModel<K>.instanceofandnewstop working, and thepromise.*Modelvalue exports are gone.*ModelSchemanow checkskind. - Removed:
*Model.fromRoute,DecisionModel.make,EmbeddingModel.make,DecisionModelRoute,EmbeddingModelRoute, and theImageRoute/SpeechRoute/TranscriptionRoute/VideoRoutetypes. model.routeis gone: readmodel.api.name(wasroute.id) andmodel.api.protocol.kind(wasroute.kind).model.apiwidens: on language models it is now the fullApi.Http, sowithtakes the full patch.- Added: every model has a
kindfield, and theModeltype is exported. @opencode/ai/internal:Api.record,Api.bind,Api.MediaBinding,Api.JsonBinding,Api.MediaModels,DecisionRoute.make,EmbeddingRoute.makeandMediaRoute.ModelInputare removed.@opencode/ai/experimental:ExtractionModelbecomes a plain interface withkind: "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.
There was a problem hiding this comment.
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.apion language models changes from the fullApi.Httprecord to a narrow view withname,kind,with, and read-onlybaseURL?: stringandmetadataKey?: string. Media, decision and embedding models get the same view with these read-only fields.- The
Compacttype parameter onLanguageModel,LanguageApiandProviderPackagechanges fromCompactionOperationsto the new exportedCompactionCapability({ 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.
994db7d to
02efdd0
Compare
There was a problem hiding this comment.
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.
Issue for this PR
Closes #
Part 1 of 2 for the plain
Modelplan. This PR is the plain record only and keeps today's auth wiring. Provider-ownedAuthandSecretcome in the follow-up.Type of change
What does this PR do?
Every model is now one plain record,
Model<K>, indexed byApi.KindMap. It replaces the seven per-kind classes (LanguageModel,ImageModel,VideoModel,SpeechModel,TranscriptionModel,DecisionModel,EmbeddingModel) and theMediaModelbase class. The record holdskind,id,provider, and theapithe model is reached through, with that host's connection. Language models also carry options, capabilities, compatibility, limits, cost and modalities. The other kinds carrytransportandnative, and decision models carrycompatibility.A model's
apiis a narrow view:name,kind,with, and read-onlybaseURL?: stringandmetadataKey?: string. The record behind it (codec, transport, auth handler, compaction operations, query, deferred base URL) sits under a module-private symbol. Only the package-privateinternal(api)insrc/api-internal.tsreads it; it isn't exported from@opencode/ai/internalor any public entry.internalchecks 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;baseURLis set only when it's already a plain string, so the public type never lies about anEffector a per-model function.Provider.makereads its APIs throughinternaltoo, so the last view-to-record cast inprovider.tsis gone.Model types carry a plain
CompactionCapability({ endpoint?: true; trigger?: true }) instead ofCompactionOperations, so the public.d.tsnever referencesRequestExecutor.ai.compactandai.canCompactkeep 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.apion each call (routeOf/jsonRouteOfinmedia-model.ts), so the model stores only facts.Api.bind,Api.record,DecisionRoute.make,EmbeddingRoute.makeand everyfromRouteare deleted.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:instanceofchecks,Schema.is(*ModelSchema)and the prototype copy infromCatalogMediabecomekindchecks and a spread. Chat's keyless override becomesapiKey: falseup front, the wayloadMediaalready did it; AI SDK packages are skipped because their custom transport never applied that auth. The native-compaction check usesai.canCompact.session/*readsmodel.api.metadataKeyandmodel.api.baseURL.RequestExecutorexport, instead of reading the API record. No new/internalexports.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,DecisionModelandEmbeddingModelare now type aliases ofModel<K>, not classes.instanceof,newand thepromise.SpeechModel-style value exports are gone.LanguageModel.make,.updateand.inputremain as functions.*ModelSchemaremain and checkkind.kindfield, and models are plain objects.JSON.stringify(model)shows only the API'sname,kind,baseURLandmetadataKey.route. Readmodel.api.name(wasroute.id).ImageModel.fromRoute,SpeechModel.fromRoute,TranscriptionModel.fromRoute,VideoModel.fromRoute,DecisionModel.make,EmbeddingModel.make,DecisionModelRoute,EmbeddingModelRoute, and theImageRoute/SpeechRoute/TranscriptionRoute/VideoRoutetypes.model.api(and everyApi.Ref-based view a model holds) gains read-onlybaseURL?: stringandmetadataKey?: string. It still doesn't show the codec, transport, auth, query or compaction.Compacttype parameter onLanguageModel,LanguageApiandProviderPackageis nowCompactionCapability({ endpoint?: true; trigger?: true }), notCompactionOperations.ModelandCompactionCapabilitytypes.@opencode/ai/internal(no compatibility promise):Api.record,Api.bind,Api.MediaBinding,Api.JsonBindingandApi.MediaModelsare removed.KindMapentries losemodel.DecisionRoute.make,EmbeddingRoute.makeandMediaRoute.ModelInputare removed. Nothing is added.@opencode/ai/experimental:ExtractionModelis a plain interface withkind: "extraction", not a class.Not changed: provider
model(id, settings)package paths and settings, wire bytes,packages/plugintypes.How did you verify your code works?
./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 onorigin/devand 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 emittedmodel.d.tshas noprotocol,RequestExecutor,CompactionOperationsor auth definition. Its onetransportis the existingtransport?: TransportOptionsHTTP-settings field on media models.packages/core:typecheck:tsc; model-resolver, aisdk, session-native-compaction, session-model-request and generate tests: 138 pass.packages/plugintypecheck; rootbun run check.Checklist
Added by the opencode bot (AI-generated).
Public API: @opencode/ai: model classes become plain
Model<K>records withkindand a narrowapiview;fromRoute/make/routeandApi.record/bindare removed;Compacttype params becomeCompactionCapability(breaking for library callers; provider entrypoints and wire format unchanged)