|
| 1 | +import type { CommandInfo, ModelInfo, ModelRef, OpenCodeClient, OpenCodeEvent } from "@opencode/client/promise" |
| 2 | +import { FSUtil } from "@opencode/util/fs-util" |
| 3 | +import type { ConfigOptionProvider } from "./config-option" |
| 4 | + |
| 5 | +export type Catalog = { |
| 6 | + readonly providers: ConfigOptionProvider[] |
| 7 | + readonly models: ModelInfo[] |
| 8 | + readonly defaultModel: ModelRef |
| 9 | + readonly modes: Array<{ id: string; name: string; description?: string }> |
| 10 | + readonly defaultModeID: string |
| 11 | + readonly commands: CommandInfo[] |
| 12 | +} |
| 13 | + |
| 14 | +export type Live = { |
| 15 | + readonly cwd: string |
| 16 | + current: Catalog |
| 17 | +} |
| 18 | + |
| 19 | +// Provider, integration, and credential changes reach the catalog through model.updated. |
| 20 | +const reloadOn = new Set<OpenCodeEvent["type"]>(["model.updated", "agent.updated", "command.updated"]) |
| 21 | + |
| 22 | +export function make(input: { |
| 23 | + readonly client: OpenCodeClient |
| 24 | + readonly signal?: AbortSignal |
| 25 | + readonly changed: (live: Live, previous: Catalog) => Promise<unknown> |
| 26 | +}) { |
| 27 | + const entries = new Map<string, Promise<Live>>() |
| 28 | + const running = new Map<Live, Promise<void>>() |
| 29 | + const queued = new Set<Live>() |
| 30 | + let subscribed: Promise<void> | undefined |
| 31 | + |
| 32 | + const get = (cwd: string) => { |
| 33 | + const key = FSUtil.resolve(cwd) |
| 34 | + const cached = entries.get(key) |
| 35 | + if (cached) return cached |
| 36 | + // Subscribe before the first read so an update between the read and the subscription is not lost. |
| 37 | + const loaded = subscribe() |
| 38 | + .then(() => load(input.client, cwd)) |
| 39 | + .then((current): Live => ({ cwd, current })) |
| 40 | + .catch((error) => { |
| 41 | + entries.delete(key) |
| 42 | + throw error |
| 43 | + }) |
| 44 | + entries.set(key, loaded) |
| 45 | + return loaded |
| 46 | + } |
| 47 | + |
| 48 | + const reload = (live: Live) => { |
| 49 | + const current = running.get(live) |
| 50 | + if (current) { |
| 51 | + queued.add(live) |
| 52 | + return current |
| 53 | + } |
| 54 | + const run = (async () => { |
| 55 | + do { |
| 56 | + queued.delete(live) |
| 57 | + const next = await load(input.client, live.cwd).catch(() => undefined) |
| 58 | + if (!next) break |
| 59 | + const previous = live.current |
| 60 | + live.current = next |
| 61 | + await input.changed(live, previous).catch(() => {}) |
| 62 | + } while (queued.has(live)) |
| 63 | + running.delete(live) |
| 64 | + })() |
| 65 | + running.set(live, run) |
| 66 | + return run |
| 67 | + } |
| 68 | + |
| 69 | + const subscribe = () => |
| 70 | + (subscribed ??= new Promise<void>((ready) => { |
| 71 | + void (async () => { |
| 72 | + for await (const event of input.client.event.subscribe({ signal: input.signal })) { |
| 73 | + if (event.type === "server.connected") ready() |
| 74 | + if (!reloadOn.has(event.type)) continue |
| 75 | + const directory = event.location?.directory |
| 76 | + const targets = directory === undefined ? [...entries.values()] : [entries.get(FSUtil.resolve(directory))] |
| 77 | + targets.forEach((entry) => void entry?.then(reload, () => {})) |
| 78 | + } |
| 79 | + })() |
| 80 | + .catch(() => {}) |
| 81 | + .finally(ready) |
| 82 | + })) |
| 83 | + |
| 84 | + return { get, reload } |
| 85 | +} |
| 86 | + |
| 87 | +async function load(client: OpenCodeClient, cwd: string): Promise<Catalog> { |
| 88 | + const location = { directory: cwd } |
| 89 | + // Some providers discover models in the background after plugin startup begins. |
| 90 | + const deadline = Date.now() + 5_000 |
| 91 | + let missing = "No models are available" |
| 92 | + while (Date.now() < deadline) { |
| 93 | + const [modelResult, defaultResult, agentResult, commandResult] = await Promise.all([ |
| 94 | + client.model.list({ location }), |
| 95 | + client.model.default({ location }), |
| 96 | + client.agent.list({ location }), |
| 97 | + client.command.list({ location }), |
| 98 | + ]) |
| 99 | + const models = modelResult.data.filter((model) => model.enabled) |
| 100 | + const preferred = defaultResult.data |
| 101 | + // Parallel reads can straddle initialization; select only from this model list. |
| 102 | + const defaultModel = preferred |
| 103 | + ? models.find((model) => model.providerID === preferred.providerID && model.id === preferred.id) |
| 104 | + : models[0] |
| 105 | + const agents = agentResult.data.filter((agent) => agent.mode !== "subagent" && !agent.hidden) |
| 106 | + const defaultAgent = agents.find((agent) => agent.mode === "primary") ?? agents[0] |
| 107 | + if (defaultModel && defaultAgent) { |
| 108 | + return { |
| 109 | + providers: providers(models), |
| 110 | + models, |
| 111 | + defaultModel: { |
| 112 | + providerID: defaultModel.providerID, |
| 113 | + id: defaultModel.id, |
| 114 | + variant: defaultModel.variants.find((variant) => variant.id === "default")?.id, |
| 115 | + }, |
| 116 | + modes: agents.map((agent) => ({ id: agent.id, name: agent.name, description: agent.description })), |
| 117 | + defaultModeID: defaultAgent.id, |
| 118 | + commands: commandResult.data, |
| 119 | + } |
| 120 | + } |
| 121 | + missing = defaultModel ? "No primary agents are available" : "No models are available" |
| 122 | + await Bun.sleep(25) |
| 123 | + } |
| 124 | + throw new Error(missing) |
| 125 | +} |
| 126 | + |
| 127 | +function providers(models: readonly ModelInfo[]): ConfigOptionProvider[] { |
| 128 | + return Array.from(new Set(models.map((model) => model.providerID))) |
| 129 | + .toSorted() |
| 130 | + .map((providerID) => ({ |
| 131 | + id: providerID, |
| 132 | + name: providerID, |
| 133 | + models: models |
| 134 | + .filter((model) => model.providerID === providerID) |
| 135 | + .map((model) => ({ id: model.id, name: model.name, variants: model.variants.map((variant) => variant.id) })), |
| 136 | + })) |
| 137 | +} |
| 138 | + |
| 139 | +export * as ACPCatalog from "./catalog" |
0 commit comments