From ae876eec1bbe98902c10f5e234338cddb57530b5 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:28:27 -0500 Subject: [PATCH] feat(protocol): list models by types, like event filtering (#59355) --- packages/client/src/effect/api/api.ts | 2 +- .../client/src/effect/generated/client.ts | 2 +- .../client/src/promise/generated/client.ts | 2 +- .../client/src/promise/generated/types.ts | 2 +- packages/core/src/model.ts | 12 ++++--- packages/core/src/plugin/host.ts | 2 +- packages/core/test/plugin/host-model.test.ts | 34 +++++++++++++++++++ packages/core/test/plugin/models-dev.test.ts | 9 +++-- packages/protocol/openapi.json | 18 +++++++--- packages/protocol/src/groups/model.ts | 6 ++-- packages/server/src/handlers/model.ts | 2 +- packages/server/test/model.test.ts | 9 ++++- 12 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 packages/core/test/plugin/host-model.test.ts diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index f58e47ad8af..9acc4d82636 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -1401,7 +1401,7 @@ export interface MessageApi { export type ModelListInput = { readonly location?: { readonly directory?: string | undefined } | undefined - readonly type?: Model.Type | undefined + readonly types?: ReadonlyArray | undefined } export type ModelListOutput = { readonly location: Location.PublicRef; readonly data: ReadonlyArray } export type ModelListOperation = (input?: ModelListInput) => Effect.Effect diff --git a/packages/client/src/effect/generated/client.ts b/packages/client/src/effect/generated/client.ts index 0cda86841c6..5382cdf1a61 100644 --- a/packages/client/src/effect/generated/client.ts +++ b/packages/client/src/effect/generated/client.ts @@ -862,7 +862,7 @@ const adaptGroupMessage = (raw: RawClient["server.message"]) => ({ list: Endpoin const EndpointModelList = (raw: RawClient["server.model"]) => (input?: ModelListInput) => preserveEffect()( - raw["model.list"]({ query: { location: input?.["location"], type: input?.["type"] } }).pipe( + raw["model.list"]({ query: { location: input?.["location"], types: input?.["types"] } }).pipe( Effect.mapError(mapClientError), ), ) diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index 0019983cbf9..645f4e74bf4 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -1141,7 +1141,7 @@ export function make(options: ClientOptions) { { method: "GET", path: `/api/model`, - query: { location: input?.["location"], type: input?.["type"] }, + query: { location: input?.["location"], types: input?.["types"] }, successStatus: 200, declaredStatuses: [400, 401, 404, 503], empty: false, diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 9850eb46c8c..6db0f8255e6 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -3618,7 +3618,7 @@ export type MessageListOutput = SessionMessagesResponse export type ModelListInput = { readonly location?: { readonly directory?: string | undefined } | undefined - readonly type?: ("chat" | "decision" | "image" | (string & {})) | undefined + readonly types?: ReadonlyArray<"chat" | "decision" | "image" | (string & {})> | undefined } export type ModelListOutput = { location: LocationPublicRef; data: Array } diff --git a/packages/core/src/model.ts b/packages/core/src/model.ts index 5788e8574a4..0640ba8a01b 100644 --- a/packages/core/src/model.ts +++ b/packages/core/src/model.ts @@ -77,8 +77,8 @@ export interface Editor { export interface Interface extends State.Transformable { readonly get: (providerID: Provider.ID, modelID: ID) => Effect.Effect readonly all: () => Effect.Effect - /** Enabled chat models, or enabled models of another type. Only chat models are offered for sessions. */ - readonly available: (type?: Model.Type) => Effect.Effect + /** Enabled models of the given types, chat when none are given. Only chat models are offered for sessions. */ + readonly available: (types?: readonly Model.Type[]) => Effect.Effect readonly default: () => Effect.Effect readonly small: (providerID: Provider.ID) => Effect.Effect } @@ -286,10 +286,14 @@ const layer = Layer.effect( read().pipe(Effect.map((value) => value.byProvider.get(providerID)?.get(modelID))), ), all: Effect.fn("Model.all")(() => read().pipe(Effect.map((value) => value.all))), - available: Effect.fn("Model.available")((type?: Model.Type) => + available: Effect.fn("Model.available")((types?: readonly Model.Type[]) => read().pipe( Effect.map((value) => - type === undefined || type === "chat" ? value.available : (value.other.get(type) ?? []), + types === undefined || types.length === 0 + ? value.available + : [...new Set(types)].flatMap((type) => + type === "chat" ? value.available : (value.other.get(type) ?? []), + ), ), ), ), diff --git a/packages/core/src/plugin/host.ts b/packages/core/src/plugin/host.ts index 6477af57ae3..6bcf93f2d6c 100644 --- a/packages/core/src/plugin/host.ts +++ b/packages/core/src/plugin/host.ts @@ -230,7 +230,7 @@ export const make = Effect.fn("PluginHost.make")(function* ( }), }, model: { - list: () => response(models.available()), + list: (input) => response(models.available(input?.types)), default: () => response(models.default()), reload: models.reload, transform: (callback) => diff --git a/packages/core/test/plugin/host-model.test.ts b/packages/core/test/plugin/host-model.test.ts new file mode 100644 index 00000000000..a472458d983 --- /dev/null +++ b/packages/core/test/plugin/host-model.test.ts @@ -0,0 +1,34 @@ +import { expect } from "bun:test" +import { Agent } from "@opencode/core/agent" +import { Plugin } from "@opencode/core/plugin" +import { PluginHost } from "@opencode/core/plugin/host" +import { Provider } from "@opencode/core/provider" +import { Model } from "@opencode/schema/model" +import { Effect } from "effect" +import { testEffect } from "../lib/effect" +import { PluginTestLayer } from "./fixture" + +const it = testEffect(PluginTestLayer) + +it.effect("lists other model types for plugins only when they ask for them", () => + Effect.gen(function* () { + const agents = yield* Agent.Service + const plugins = yield* Plugin.Service + const catalog = yield* Provider.Service + yield* agents.transform((editor) => editor.update(Agent.ID.make("build"), () => {})) + const host = yield* PluginHost.make(plugins) + const imageID = Model.ID.make("meta/muse-spark-1.1") + yield* catalog.transform((editor) => { + editor.models.update(Provider.ID.make("test"), imageID, (model) => { + model.type = "image" + }) + }) + const ids = (models: readonly Model.Info[]) => models.map((model) => model.id) + + const chat = ids((yield* host.model.list()).data) + expect(chat).not.toContain(imageID) + expect(ids((yield* host.model.list({ types: [] })).data)).toEqual(chat) + expect(ids((yield* host.model.list({ types: ["image"] })).data)).toEqual([imageID]) + expect(ids((yield* host.model.list({ types: ["chat", "image"] })).data)).toEqual([...chat, imageID]) + }), +) diff --git a/packages/core/test/plugin/models-dev.test.ts b/packages/core/test/plugin/models-dev.test.ts index 7a5b296f905..3e9797d2c3d 100644 --- a/packages/core/test/plugin/models-dev.test.ts +++ b/packages/core/test/plugin/models-dev.test.ts @@ -758,9 +758,12 @@ describe("ModelsDevPlugin", () => { expect(ids(yield* modelState.available())).toEqual(["gpt-5.5", "gpt-audio"].map((id) => Model.ID.make(id))) expect(ids(yield* modelState.all())).toEqual(["gpt-5.5", "gpt-audio"].map((id) => Model.ID.make(id))) expect(yield* modelState.get(providerID, Model.ID.make("jev-1.13"))).toBeUndefined() - expect(ids(yield* modelState.available("decision"))).toEqual([Model.ID.make("jev-1.13")]) - expect(ids(yield* modelState.available("image"))).toEqual([Model.ID.make("gpt-image-2")]) - expect(yield* modelState.available("embedding")).toEqual([]) + expect(ids(yield* modelState.available(["decision"]))).toEqual([Model.ID.make("jev-1.13")]) + expect(ids(yield* modelState.available(["image"]))).toEqual([Model.ID.make("gpt-image-2")]) + expect(ids(yield* modelState.available(["chat", "image", "image"]))).toEqual( + ["gpt-5.5", "gpt-audio", "gpt-image-2"].map((id) => Model.ID.make(id)), + ) + expect(yield* modelState.available(["embedding"])).toEqual([]) }), ) diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index b4cd7f1b3a8..eb575f88d56 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -5057,18 +5057,28 @@ "explode": true }, { - "name": "type", + "name": "types", "in": "query", "schema": { "anyOf": [ { - "$ref": "#/components/schemas/Model.Type" + "anyOf": [ + { + "$ref": "#/components/schemas/Model.Type" + }, + { + "type": "array", + "items": { + "$ref": "#/components/schemas/Model.Type" + } + } + ] }, { "type": "null" } ], - "description": "Model type to list. Defaults to chat, the only type offered for sessions." + "description": "Only list models of these types. Defaults to chat, the only type offered for sessions." }, "required": false } @@ -5139,7 +5149,7 @@ } } }, - "description": "Retrieve the current snapshot of available models of one type, chat by default, ordered by release date. The snapshot may precede initial plugin settlement.", + "description": "Retrieve the current snapshot of available models of the requested types, chat by default, ordered by release date within each type. The snapshot may precede initial plugin settlement.", "summary": "List models" } }, diff --git a/packages/protocol/src/groups/model.ts b/packages/protocol/src/groups/model.ts index 7ae78592f07..a1ce90b7a18 100644 --- a/packages/protocol/src/groups/model.ts +++ b/packages/protocol/src/groups/model.ts @@ -7,8 +7,8 @@ import { LocationQuery, locationQueryOpenApi } from "./location.js" const ListQuery = Schema.Struct({ ...LocationQuery.fields, - type: Schema.optional(Model.Type).annotate({ - description: "Model type to list. Defaults to chat, the only type offered for sessions.", + types: Schema.optional(Schema.ArrayEnsure(Model.Type)).annotate({ + description: "Only list models of these types. Defaults to chat, the only type offered for sessions.", }), }) @@ -25,7 +25,7 @@ export const ModelGroup = HttpApiGroup.make("server.model") identifier: "model.list", summary: "List models", description: - "Retrieve the current snapshot of available models of one type, chat by default, ordered by release date. The snapshot may precede initial plugin settlement.", + "Retrieve the current snapshot of available models of the requested types, chat by default, ordered by release date within each type. The snapshot may precede initial plugin settlement.", }), ), ) diff --git a/packages/server/src/handlers/model.ts b/packages/server/src/handlers/model.ts index 07a11daafc5..eb551744994 100644 --- a/packages/server/src/handlers/model.ts +++ b/packages/server/src/handlers/model.ts @@ -12,7 +12,7 @@ export const ModelHandler = HttpApiBuilder.group(Api, "server.model", (handlers) Effect.fn(function* (ctx) { yield* awaitPlugins const models = yield* Model.Service - return yield* response(models.available(ctx.query.type)) + return yield* response(models.available(ctx.query.types)) }), ) .handle( diff --git a/packages/server/test/model.test.ts b/packages/server/test/model.test.ts index 53430357e95..85991acd06d 100644 --- a/packages/server/test/model.test.ts +++ b/packages/server/test/model.test.ts @@ -35,12 +35,19 @@ it.live("waits for plugin initialization before listing models", () => body["data"].some((model) => isRecord(model) && model["providerID"] === "custom" && model["id"] === "chat"), ).toBeTrue() - url.searchParams.set("type", "image") + url.searchParams.set("types", "image") const images = yield* Effect.promise(() => fetch(url, { headers: server.headers })) expect(images.status).toBe(200) const imageBody: unknown = yield* Effect.promise(() => images.json()) if (!isRecord(imageBody) || !Array.isArray(imageBody["data"])) throw new Error("Expected a model list response") expect(imageBody["data"].some((model) => isRecord(model) && model["providerID"] === "custom")).toBeFalse() + + url.searchParams.append("types", "chat") + const both = yield* Effect.promise(() => fetch(url, { headers: server.headers })) + expect(both.status).toBe(200) + const bothBody: unknown = yield* Effect.promise(() => both.json()) + if (!isRecord(bothBody) || !Array.isArray(bothBody["data"])) throw new Error("Expected a model list response") + expect(bothBody["data"].some((model) => isRecord(model) && model["id"] === "chat")).toBeTrue() }), )