mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
feat(protocol): list models by types, like event filtering (#59355)
This commit is contained in:
committed by
GitHub
Unverified
parent
0f3a0c206f
commit
ae876eec1b
Generated
+1
-1
@@ -1401,7 +1401,7 @@ export interface MessageApi<E = never> {
|
||||
|
||||
export type ModelListInput = {
|
||||
readonly location?: { readonly directory?: string | undefined } | undefined
|
||||
readonly type?: Model.Type | undefined
|
||||
readonly types?: ReadonlyArray<Model.Type> | undefined
|
||||
}
|
||||
export type ModelListOutput = { readonly location: Location.PublicRef; readonly data: ReadonlyArray<Model.Info> }
|
||||
export type ModelListOperation<E = never> = (input?: ModelListInput) => Effect.Effect<ModelListOutput, E>
|
||||
|
||||
+1
-1
@@ -862,7 +862,7 @@ const adaptGroupMessage = (raw: RawClient["server.message"]) => ({ list: Endpoin
|
||||
|
||||
const EndpointModelList = (raw: RawClient["server.model"]) => (input?: ModelListInput) =>
|
||||
preserveEffect<ModelListOutput>()(
|
||||
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),
|
||||
),
|
||||
)
|
||||
|
||||
+1
-1
@@ -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,
|
||||
|
||||
+1
-1
@@ -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<ModelInfo> }
|
||||
|
||||
@@ -77,8 +77,8 @@ export interface Editor {
|
||||
export interface Interface extends State.Transformable<Editor> {
|
||||
readonly get: (providerID: Provider.ID, modelID: ID) => Effect.Effect<Info | undefined>
|
||||
readonly all: () => Effect.Effect<readonly Info[]>
|
||||
/** Enabled chat models, or enabled models of another type. Only chat models are offered for sessions. */
|
||||
readonly available: (type?: Model.Type) => Effect.Effect<readonly Info[]>
|
||||
/** 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 Info[]>
|
||||
readonly default: () => Effect.Effect<Info | undefined>
|
||||
readonly small: (providerID: Provider.ID) => Effect.Effect<Info | undefined>
|
||||
}
|
||||
@@ -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) ?? []),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
@@ -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])
|
||||
}),
|
||||
)
|
||||
@@ -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([])
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Generated
+14
-4
@@ -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"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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()
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user