The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
fix(opencode): register configured model in runtime config (#10178)
Dottaee3ed0117e0f
diff --git a/packages/adapters/opencode-local/src/server/runtime-config.test.ts b/packages/adapters/opencode-local/src/server/runtime-config.test.tsindex 19f61c06d3..14791829a8 100644--- a/packages/adapters/opencode-local/src/server/runtime-config.test.ts+++ b/packages/adapters/opencode-local/src/server/runtime-config.test.ts@@ -249,6 +249,67 @@ describe("prepareOpenCodeRuntimeConfig", () => {await prepared.cleanup();});+ it("registers a configured model missing from the catalog on its provider", async () => {+ const configHome = await makeConfigHome({ permission: { read: "allow" } });+ const prepared = await prepareOpenCodeRuntimeConfig({+ env: { XDG_CONFIG_HOME: configHome },+ config: { model: "openrouter/openai/gpt-oss-120b:nitro" },+ });+ cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);+ const runtimeConfig = JSON.parse(+ await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),+ ) as { provider?: Record<string, { models?: Record<string, unknown> }> };+ expect(runtimeConfig.provider?.openrouter?.models).toEqual({+ "openai/gpt-oss-120b:nitro": {},+ });+ expect(prepared.notes).toContain(+ "Registered configured model openrouter/openai/gpt-oss-120b:nitro in the runtime OpenCode config.",+ );+ await prepared.cleanup();+ });++ it("does not clobber an explicit model definition when registering the configured model", async () => {+ const configHome = await makeConfigHome({ permission: { read: "allow" } });+ const providers = {+ openrouter: {+ models: {+ "openai/gpt-oss-120b:nitro": { name: "GPT-OSS 120B (nitro)" },+ "example/other": {},+ },+ },+ };+ const prepared = await prepareOpenCodeRuntimeConfig({+ env: {+ XDG_CONFIG_HOME: configHome,+ PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify(providers),+ },+ config: { model: "openrouter/openai/gpt-oss-120b:nitro" },+ });+ cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);+ const runtimeConfig = JSON.parse(+ await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),+ ) as { provider?: Record<string, { models?: Record<string, unknown> }> };+ expect(runtimeConfig.provider?.openrouter?.models).toEqual(providers.openrouter.models);+ expect(+ prepared.notes.some((note) => note.startsWith("Registered configured model")),+ ).toBe(false);+ await prepared.cleanup();+ });++ it("skips model registration when the configured model is not provider/model shaped", async () => {+ const configHome = await makeConfigHome({ permission: { read: "allow" } });+ const prepared = await prepareOpenCodeRuntimeConfig({+ env: { XDG_CONFIG_HOME: configHome },+ config: { model: "not-a-provider-model" },+ });+ cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);+ const runtimeConfig = JSON.parse(+ await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),+ ) as Record<string, unknown>;+ expect(runtimeConfig.provider).toBeUndefined();+ await prepared.cleanup();+ });+it("respects explicit opt-out", async () => {const configHome = await makeConfigHome();const prepared = await prepareOpenCodeRuntimeConfig({diff --git a/packages/adapters/opencode-local/src/server/runtime-config.ts b/packages/adapters/opencode-local/src/server/runtime-config.tsindex 146b371f29..dd466cdb2d 100644--- a/packages/adapters/opencode-local/src/server/runtime-config.ts+++ b/packages/adapters/opencode-local/src/server/runtime-config.ts@@ -84,6 +84,14 @@ function parseProviderConfig(return Object.keys(providers).length > 0 ? providers : null;}+function parseConfiguredModelRef(raw: unknown): { provider: string; model: string } | null {+ if (typeof raw !== "string") return null;+ const trimmed = raw.trim();+ const slash = trimmed.indexOf("/");+ if (slash <= 0 || slash === trimmed.length - 1) return null;+ return { provider: trimmed.slice(0, slash), model: trimmed.slice(slash + 1) };+}+async function readJsonObject(filepath: string): Promise<Record<string, unknown>> {try {const raw = await fs.readFile(filepath, "utf8");@@ -162,7 +170,7 @@ export async function prepareOpenCodeRuntimeConfig(input: {notes,);const existingProvider = isPlainObject(existingConfig.provider) ? existingConfig.provider : {};- const nextProvider = gatewayProviders+ let nextProvider = gatewayProviders? { ...existingProvider, ...gatewayProviders }: existingProvider;if (gatewayProviders) {@@ -171,6 +179,32 @@ export async function prepareOpenCodeRuntimeConfig(input: {);}+ // Register the configured model on its provider's models map. OpenCode resolves+ // `--model provider/model` only when the model id exists in that map, so ids the+ // models.dev catalog does not carry — OpenRouter routing variants such as+ // `openai/gpt-oss-120b:nitro`, or models newer than the bundled catalog — are+ // otherwise rejected with "Model not found" even though the provider serves them.+ // An empty entry deep-merges with catalog metadata, so this is a no-op for models+ // the catalog already knows, and we never clobber an explicit definition from the+ // user config or PAPERCLIP_OPENCODE_PROVIDERS.+ const configuredModel = parseConfiguredModelRef(input.config.model);+ if (configuredModel) {+ const providerEntry = isPlainObject(nextProvider[configuredModel.provider])+ ? { ...(nextProvider[configuredModel.provider] as Record<string, unknown>) }+ : {};+ const providerModels = isPlainObject(providerEntry.models)+ ? { ...(providerEntry.models as Record<string, unknown>) }+ : {};+ if (!isPlainObject(providerModels[configuredModel.model])) {+ providerModels[configuredModel.model] = {};+ providerEntry.models = providerModels;+ nextProvider = { ...nextProvider, [configuredModel.provider]: providerEntry };+ notes.push(+ `Registered configured model ${configuredModel.provider}/${configuredModel.model} in the runtime OpenCode config.`,+ );+ }+ }+const nextConfig: Record<string, unknown> = {...existingConfig,permission: {