|
| 1 | +import { existsSync, readFileSync, writeFileSync } from "node:fs" |
| 2 | +import { homedir } from "node:os" |
| 3 | +import { join } from "node:path" |
| 4 | +import { CURSOR_PROVIDER_ID } from "../shared.js" |
| 5 | +import { CURSOR_WIRE_MODEL_ID_KEY, type ModelInfo } from "../models.js" |
| 6 | +import { modelsToConfig } from "../model-config.js" |
| 7 | +import { CURSOR_AISDK_PACKAGE, CURSOR_INTEGRATION_ID } from "./catalog.js" |
| 8 | + |
| 9 | +/** |
| 10 | + * OpenCode 2.0.5 (stable) ships `ctx.provider` / `ctx.model` but **no** |
| 11 | + * `ctx.catalog`. Plugin transforms on those split domains do not flush into the |
| 12 | + * live model picker (draft mutations stay invisible to `list()`). |
| 13 | + * |
| 14 | + * Fallback: write discovered Cursor models into the user config document under |
| 15 | + * `providers.cursor` (OC2) or `provider.cursor` (legacy key), which 2.0.5 does |
| 16 | + * honor. Beta hosts with `ctx.catalog` never need this path. |
| 17 | + */ |
| 18 | + |
| 19 | +export type ConfigCatalogSyncResult = { |
| 20 | + path: string |
| 21 | + modelCount: number |
| 22 | + changed: boolean |
| 23 | +} |
| 24 | + |
| 25 | +function configDir(): string { |
| 26 | + return process.env.OPENCODE_CONFIG_DIR?.trim() || join(homedir(), ".config", "opencode") |
| 27 | +} |
| 28 | + |
| 29 | +function resolveConfigPath(dir: string): string { |
| 30 | + const jsonc = join(dir, "opencode.jsonc") |
| 31 | + const json = join(dir, "opencode.json") |
| 32 | + if (existsSync(jsonc)) return jsonc |
| 33 | + if (existsSync(json)) return json |
| 34 | + return jsonc |
| 35 | +} |
| 36 | + |
| 37 | +function stripJsonc(raw: string): string { |
| 38 | + // ponytail: cheap JSONC strip — enough for our generated/hand-edited configs. |
| 39 | + // Upgrade: real JSONC parser if users embed exotic comments inside strings. |
| 40 | + return raw |
| 41 | + .replace(/\/\*[\s\S]*?\*\//g, "") |
| 42 | + .replace(/^\s*\/\/.*$/gm, "") |
| 43 | +} |
| 44 | + |
| 45 | +function modelsToProvidersEntry(models: ModelInfo[]): Record<string, unknown> { |
| 46 | + const config = modelsToConfig(models) |
| 47 | + const out: Record<string, unknown> = {} |
| 48 | + for (const [id, entry] of Object.entries(config)) { |
| 49 | + const options = (entry as any).options as Record<string, unknown> | undefined |
| 50 | + const wireId = |
| 51 | + typeof options?.[CURSOR_WIRE_MODEL_ID_KEY] === "string" |
| 52 | + ? (options[CURSOR_WIRE_MODEL_ID_KEY] as string) |
| 53 | + : id |
| 54 | + const input = Array.isArray((entry as any).modalities?.input) |
| 55 | + ? (entry as any).modalities.input.filter((m: unknown): m is string => typeof m === "string") |
| 56 | + : ["text"] |
| 57 | + const output = Array.isArray((entry as any).modalities?.output) |
| 58 | + ? (entry as any).modalities.output.filter((m: unknown): m is string => typeof m === "string") |
| 59 | + : ["text"] |
| 60 | + out[id] = { |
| 61 | + name: (entry as any).name ?? id, |
| 62 | + modelID: wireId, |
| 63 | + capabilities: { |
| 64 | + tools: (entry as any).tool_call !== false, |
| 65 | + input, |
| 66 | + output, |
| 67 | + }, |
| 68 | + limit: { |
| 69 | + context: (entry as any).limit?.context ?? 200_000, |
| 70 | + output: (entry as any).limit?.output ?? 8192, |
| 71 | + }, |
| 72 | + status: "active", |
| 73 | + enabled: true, |
| 74 | + ...(options ? { settings: { ...options } } : {}), |
| 75 | + } |
| 76 | + } |
| 77 | + return out |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Upsert `providers.cursor` (preferred) with the AI SDK package + model map. |
| 82 | + * Returns whether the on-disk document changed. |
| 83 | + */ |
| 84 | +export function syncCursorProvidersConfig(models: ModelInfo[]): ConfigCatalogSyncResult { |
| 85 | + const dir = configDir() |
| 86 | + const path = resolveConfigPath(dir) |
| 87 | + let doc: Record<string, any> = {} |
| 88 | + if (existsSync(path)) { |
| 89 | + try { |
| 90 | + doc = JSON.parse(stripJsonc(readFileSync(path, "utf8"))) |
| 91 | + } catch { |
| 92 | + doc = {} |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const modelMap = modelsToProvidersEntry(models) |
| 97 | + const nextProvider = { |
| 98 | + name: "Cursor", |
| 99 | + package: CURSOR_AISDK_PACKAGE, |
| 100 | + integrationID: CURSOR_INTEGRATION_ID, |
| 101 | + models: modelMap, |
| 102 | + } |
| 103 | + |
| 104 | + // Prefer OC2 `providers`; keep legacy `provider` in sync when already present. |
| 105 | + const prevProviders = doc.providers && typeof doc.providers === "object" ? doc.providers : {} |
| 106 | + const prevProvider = doc.provider && typeof doc.provider === "object" ? doc.provider : {} |
| 107 | + const prev = JSON.stringify(prevProviders[CURSOR_PROVIDER_ID] ?? prevProvider[CURSOR_PROVIDER_ID] ?? null) |
| 108 | + const next = JSON.stringify(nextProvider) |
| 109 | + if (prev === next) { |
| 110 | + return { path, modelCount: Object.keys(modelMap).length, changed: false } |
| 111 | + } |
| 112 | + |
| 113 | + doc.providers = { ...prevProviders, [CURSOR_PROVIDER_ID]: nextProvider } |
| 114 | + if (doc.provider && typeof doc.provider === "object") { |
| 115 | + // Drop legacy npm-shaped block if we now own providers.cursor, to avoid |
| 116 | + // duplicate/conflicting Cursor entries. |
| 117 | + const { [CURSOR_PROVIDER_ID]: _drop, ...rest } = doc.provider as Record<string, unknown> |
| 118 | + doc.provider = rest |
| 119 | + if (Object.keys(doc.provider).length === 0) delete doc.provider |
| 120 | + } |
| 121 | + |
| 122 | + writeFileSync(path, `${JSON.stringify(doc, null, 2)}\n`, "utf8") |
| 123 | + return { path, modelCount: Object.keys(modelMap).length, changed: true } |
| 124 | +} |
| 125 | + |
| 126 | +export function hasCatalogDomain(ctx: { catalog?: { transform?: unknown; reload?: unknown } }): boolean { |
| 127 | + return typeof ctx.catalog?.transform === "function" |
| 128 | +} |
0 commit comments