@stablekernel/opencode-cursor 0.4.4 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,10 +3,11 @@ import {
3
3
  buildModelSelection,
4
4
  fingerprintApiKey,
5
5
  loadCursorSdk,
6
+ removeSystemRule,
6
7
  resolveControls,
7
8
  resolveCursorApiKey,
8
9
  streamAgentTurn
9
- } from "../chunk-KGDYXGZL.js";
10
+ } from "../chunk-734L3SKU.js";
10
11
 
11
12
  // src/model-cache.ts
12
13
  import { mkdirSync, readFileSync, writeFileSync } from "fs";
@@ -558,14 +559,130 @@ function buildCursorTools(deps) {
558
559
  };
559
560
  }
560
561
 
562
+ // src/version-check.ts
563
+ import { createRequire } from "module";
564
+ import { homedir as homedir2, tmpdir as tmpdir2 } from "os";
565
+ import { join as join2 } from "path";
566
+ import { get } from "https";
567
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
568
+ import semver from "semver";
569
+ var PACKAGE_NAME = "@stablekernel/opencode-cursor";
570
+ var REGISTRY_URL = `https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}/latest`;
571
+ var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
572
+ var FAILURE_TTL_MS = 60 * 60 * 1e3;
573
+ var REQUEST_TIMEOUT_MS = 5e3;
574
+ function cacheDir2() {
575
+ const base = process.env.XDG_CACHE_HOME?.trim() || (homedir2() ? join2(homedir2(), ".cache") : tmpdir2());
576
+ return join2(base, "opencode-cursor");
577
+ }
578
+ function cacheFile2() {
579
+ return join2(cacheDir2(), "version-check.json");
580
+ }
581
+ function readCache() {
582
+ try {
583
+ const parsed = JSON.parse(readFileSync2(cacheFile2(), "utf8"));
584
+ if (typeof parsed.checkedAt === "number") return parsed;
585
+ } catch {
586
+ }
587
+ return void 0;
588
+ }
589
+ function writeCache(latest) {
590
+ try {
591
+ mkdirSync2(cacheDir2(), { recursive: true });
592
+ writeFileSync2(
593
+ cacheFile2(),
594
+ JSON.stringify({ checkedAt: Date.now(), latest }),
595
+ "utf8"
596
+ );
597
+ } catch {
598
+ }
599
+ }
600
+ function getLocalVersion() {
601
+ if (true) return "0.4.5";
602
+ try {
603
+ const require2 = createRequire(import.meta.url);
604
+ const pkg = require2("../package.json");
605
+ return pkg.version;
606
+ } catch {
607
+ return void 0;
608
+ }
609
+ }
610
+ function fetchLatestVersion() {
611
+ return new Promise((resolve) => {
612
+ const req = get(
613
+ REGISTRY_URL,
614
+ { headers: { Accept: "application/json", Connection: "close" } },
615
+ (res) => {
616
+ if (res.statusCode !== 200) {
617
+ res.resume();
618
+ resolve(void 0);
619
+ return;
620
+ }
621
+ let body = "";
622
+ res.setEncoding("utf8");
623
+ res.on("data", (chunk) => {
624
+ body += chunk;
625
+ });
626
+ res.on("end", () => {
627
+ try {
628
+ const parsed = JSON.parse(body);
629
+ resolve(parsed.version);
630
+ } catch {
631
+ resolve(void 0);
632
+ }
633
+ });
634
+ res.on("error", () => resolve(void 0));
635
+ }
636
+ );
637
+ req.setTimeout(REQUEST_TIMEOUT_MS, () => {
638
+ req.destroy();
639
+ resolve(void 0);
640
+ });
641
+ req.on("error", () => resolve(void 0));
642
+ });
643
+ }
644
+ async function getLatestVersion() {
645
+ const cached = readCache();
646
+ if (cached) {
647
+ const ttl = cached.latest ? CHECK_INTERVAL_MS : FAILURE_TTL_MS;
648
+ if (Date.now() - cached.checkedAt < ttl) return cached.latest;
649
+ }
650
+ const latest = await fetchLatestVersion();
651
+ writeCache(latest);
652
+ return latest;
653
+ }
654
+ async function warnIfStale() {
655
+ if (process.env.CI || process.env.NO_UPDATE_NOTIFIER) return;
656
+ const local = getLocalVersion();
657
+ if (!local || !semver.valid(local)) return;
658
+ const latest = await getLatestVersion();
659
+ if (!latest || !semver.valid(latest)) return;
660
+ if (!semver.gt(latest, local)) return;
661
+ const removeCommand = process.platform === "win32" ? `rmdir /s /q "%LocalAppData%\\opencode\\cache\\packages\\@stablekernel\\opencode-cursor@latest"` : `rm -rf ~/.cache/opencode/packages/${PACKAGE_NAME}@latest`;
662
+ console.warn(
663
+ `
664
+ \u26A0\uFE0F @stablekernel/opencode-cursor update available: v${local} \u2192 v${latest}.
665
+ opencode caches the @latest plugin on first install and never auto-updates it.
666
+ To upgrade, exit opencode, run:
667
+
668
+ ${removeCommand}
669
+
670
+ then restart opencode.
671
+ `
672
+ );
673
+ }
674
+
561
675
  // src/plugin/index.ts
562
676
  function apiKeyFromAuth(auth) {
563
677
  return auth?.type === "api" ? auth.key : void 0;
564
678
  }
565
679
  var CursorPlugin = async (input) => {
680
+ void warnIfStale().catch(() => {
681
+ });
566
682
  let capturedApiKey;
567
683
  const client = input?.client;
568
684
  const directory = input?.directory;
685
+ let resolvedCwd = directory ?? process.cwd();
569
686
  let forwardMcp = true;
570
687
  let userMcp = {};
571
688
  const warnedOAuth = /* @__PURE__ */ new Set();
@@ -599,12 +716,15 @@ var CursorPlugin = async (input) => {
599
716
  forwardMcp = existingOptions["forwardMcp"] !== false;
600
717
  userMcp = existingOptions["mcpServers"] ?? {};
601
718
  const mcpServers = forwardMcp ? { ...userMcp, ...translateMcpServers(config.mcp) } : userMcp;
719
+ const optionCwd = existingOptions["cwd"];
720
+ resolvedCwd = (typeof optionCwd === "string" ? optionCwd : void 0) ?? directory ?? process.cwd();
602
721
  config.provider[PROVIDER_ID] = {
603
722
  name: "Cursor",
604
723
  npm: providerNpm(),
605
724
  ...existing,
606
725
  options: {
607
726
  ...existingOptions,
727
+ cwd: resolvedCwd,
608
728
  ...Object.keys(mcpServers).length > 0 ? { mcpServers } : {}
609
729
  },
610
730
  models: { ...toOpencodeModels(models), ...existing.models ?? {} }
@@ -694,6 +814,9 @@ var CursorPlugin = async (input) => {
694
814
  resolveApiKey: () => resolveCursorApiKey(capturedApiKey),
695
815
  defaultCwd: () => input?.directory ?? process.cwd()
696
816
  })
817
+ },
818
+ dispose: async () => {
819
+ removeSystemRule(resolvedCwd);
697
820
  }
698
821
  };
699
822
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model-cache.ts","../../src/fallback-models.ts","../../src/model-variants.ts","../../src/model-discovery.ts","../../src/plugin/model-v2.ts","../../src/plugin/mcp-config.ts","../../src/plugin/cursor-tools.ts","../../src/provider/cloud-agent.ts","../../src/provider/delegate.ts","../../src/plugin/index.ts"],"sourcesContent":["import { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir, tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { ModelListItem } from \"@cursor/sdk\";\n\n/** Default cache lifetime: 24 hours, overridable via env. */\nconst DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;\n\nfunction ttlMs(): number {\n const raw = process.env.OPENCODE_CURSOR_MODEL_CACHE_TTL_MS;\n const parsed = raw ? Number.parseInt(raw, 10) : NaN;\n return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_TTL_MS;\n}\n\nfunction cacheDir(): string {\n const base =\n process.env.XDG_CACHE_HOME?.trim() ||\n (homedir() ? join(homedir(), \".cache\") : tmpdir());\n return join(base, \"opencode-cursor\");\n}\n\nfunction cacheFile(fingerprint: string): string {\n return join(cacheDir(), `models-${fingerprint}.json`);\n}\n\n/**\n * Key-independent \"latest known catalog\" file. The `config` plugin hook runs\n * without access to the stored API key, so it can't read the per-key cache.\n * This file lets a keyless caller (the config hook) seed opencode's model\n * picker with the real catalog that a previous *authed* load discovered.\n */\nfunction latestCacheFile(): string {\n return join(cacheDir(), \"models-latest.json\");\n}\n\n/** The latest-catalog seed is kept longer than the per-key cache: the catalog\n * is stable and this only feeds pre-auth UI seeding. */\nconst LATEST_TTL_MS = 30 * 24 * 60 * 60 * 1000;\n\ninterface CacheEnvelope {\n savedAt: number;\n models: ModelListItem[];\n}\n\nfunction readCacheFile(file: string, maxAgeMs: number): ModelListItem[] | undefined {\n try {\n const parsed = JSON.parse(readFileSync(file, \"utf8\")) as CacheEnvelope;\n if (!parsed?.savedAt || !Array.isArray(parsed.models)) return undefined;\n if (Date.now() - parsed.savedAt > maxAgeMs) return undefined;\n return parsed.models;\n } catch {\n return undefined;\n }\n}\n\nfunction writeCacheFile(file: string, models: ModelListItem[]): void {\n try {\n mkdirSync(cacheDir(), { recursive: true });\n const envelope: CacheEnvelope = { savedAt: Date.now(), models };\n writeFileSync(file, JSON.stringify(envelope), \"utf8\");\n } catch {\n // Caching is an optimization; ignore write failures.\n }\n}\n\n/**\n * Return cached models for the given API-key fingerprint when present and still\n * fresh, otherwise `undefined`. Never throws on a missing/corrupt cache.\n */\nexport function readModelCache(fingerprint: string): ModelListItem[] | undefined {\n return readCacheFile(cacheFile(fingerprint), ttlMs());\n}\n\n/** Persist the discovered model list (per-key cache + key-independent latest\n * catalog). Best-effort; never throws. */\nexport function writeModelCache(fingerprint: string, models: ModelListItem[]): void {\n writeCacheFile(cacheFile(fingerprint), models);\n writeCacheFile(latestCacheFile(), models);\n}\n\n/**\n * Return the most recently discovered catalog regardless of API key, when\n * present and within {@link LATEST_TTL_MS}. Used by the keyless `config` hook to\n * seed the picker with the real catalog after a prior authed load.\n */\nexport function readLatestModelCache(): ModelListItem[] | undefined {\n return readCacheFile(latestCacheFile(), LATEST_TTL_MS);\n}\n","import type { ModelListItem } from \"@cursor/sdk\";\n\n/**\n * A small static snapshot of well-known Cursor models, used only when live\n * discovery is unavailable (no API key, offline, or an SDK error). The live\n * `Cursor.models.list()` result always takes precedence; this just lets the\n * provider appear in opencode with sensible defaults so the user can reach the\n * login flow. Refresh the real catalog with the `cursor_refresh_models` tool.\n */\nexport const FALLBACK_MODELS: ModelListItem[] = [\n {\n id: \"composer-2.5\",\n displayName: \"Composer 2.5\",\n description: \"Cursor's default agent model (fallback entry).\",\n parameters: [\n { id: \"thinking\", displayName: \"Thinking\", values: [{ value: \"off\" }, { value: \"on\" }] },\n ],\n },\n { id: \"claude-opus-4-8\", displayName: \"Claude Opus 4.8 (via Cursor)\" },\n { id: \"claude-sonnet-4-6\", displayName: \"Claude Sonnet 4.6 (via Cursor)\" },\n { id: \"gpt-5.5\", displayName: \"GPT-5.5 (via Cursor)\" },\n];\n","import type { ModelListItem } from \"@cursor/sdk\";\n\n/**\n * A Cursor model \"variant\" as opencode stores it: an options object that, when\n * the variant is selected, is merged into `providerOptions.cursor` and read back\n * by {@link resolveControls}.\n */\nexport interface CursorVariant {\n params?: Record<string, string>;\n mode?: \"agent\" | \"plan\";\n}\n\nconst REASONING_PARAM = /think|reason|effort/i;\nconst BOOLEAN_VALUES = new Set([\"true\", \"false\"]);\n\nfunction paramValues(param: NonNullable<ModelListItem[\"parameters\"]>[number]): string[] {\n return (param.values ?? []).map((v) => v.value);\n}\n\nfunction isBooleanParam(values: string[]): boolean {\n return values.length > 0 && values.every((v) => BOOLEAN_VALUES.has(v));\n}\n\n/**\n * Params opencode must send by DEFAULT for this model — i.e. when the user has\n * NOT picked a variant. Non-reasoning boolean toggles (notably Cursor's `fast`)\n * are pinned OFF here so the provider never silently inherits Cursor's\n * server-side default, which is `fast: true` for several models (composer-*,\n * gpt-*-codex). The user opts back IN via the matching picker variant.\n *\n * Seeded into each model's opencode `options.params` (see `toOpencodeModels` /\n * `buildModelV2Map`); {@link resolveControls} merges it into the request.\n */\nexport function defaultModelParams(item: ModelListItem): Record<string, string> {\n const out: Record<string, string> = {};\n for (const param of item.parameters ?? []) {\n if (REASONING_PARAM.test(param.id)) continue;\n if (isBooleanParam(paramValues(param))) out[param.id] = \"false\";\n }\n return out;\n}\n\n/**\n * Derive opencode model variants from a Cursor model's parameters so the\n * variant picker can expose thinking/reasoning levels plus the `fast` toggle.\n * Each variant's object is exactly what {@link resolveControls} consumes. Plan\n * mode is NOT a variant: opencode's plan agent (Tab) is mapped to Cursor's plan\n * mode by the plugin's `chat.params` hook.\n *\n * Every variant for a fast-capable model carries an explicit `fast` value\n * (reasoning variants pin it OFF via {@link defaultModelParams}; the `fast`\n * variant turns it ON) so a selection never depends on Cursor's server-side\n * default for an omitted param.\n */\nexport function buildModelVariants(item: ModelListItem): Record<string, CursorVariant> {\n const out: Record<string, CursorVariant> = {};\n // Non-reasoning boolean defaults (e.g. { fast: \"false\" }), pinned into every\n // reasoning variant so picking a reasoning level never re-enables fast.\n const defaults = defaultModelParams(item);\n\n // Pre-pass: does any reasoning param expose a non-boolean effort enum (e.g.\n // [\"low\",\"medium\",\"high\",\"xhigh\",\"max\"])? When it does, a coexisting boolean\n // reasoning toggle (Cursor's `thinking=[\"false\",\"true\"]` on claude-* models)\n // is redundant — selecting any effort level already enables reasoning — and\n // surfacing it would add a stray `thinking` variant the standard opencode\n // providers don't show. Suppress the boolean variant for parity. Order-\n // independent: the enum may be declared before or after the boolean.\n const hasEffortEnum = (item.parameters ?? []).some(\n (p) => REASONING_PARAM.test(p.id) && !isBooleanParam(paramValues(p)) && paramValues(p).length > 0,\n );\n\n for (const param of item.parameters ?? []) {\n const values = paramValues(param);\n if (values.length === 0) continue;\n const boolean = isBooleanParam(values);\n\n if (REASONING_PARAM.test(param.id)) {\n if (boolean) {\n // Boolean toggle (e.g. thinking=[\"false\",\"true\"]). Literal true/false\n // variant names are meaningless in the picker — surface a single\n // variant named after the param that switches it on. \"Off\" is the\n // model's default (no variant selected). Skipped entirely when an\n // effort enum coexists (see hasEffortEnum above).\n if (!hasEffortEnum && values.includes(\"true\")) {\n out[param.id.toLowerCase()] = { params: { ...defaults, [param.id]: \"true\" } };\n }\n continue;\n }\n\n for (const value of values) {\n // `none` means reasoning OFF — the model's default when no variant is\n // selected. Surfacing it as a selectable variant is meaningless (you\n // get it by picking nothing), so skip it. Standard providers\n // (models.dev) include `none` in their effort values, but the\n // no-variant-selected state already represents it.\n if (value === \"none\") continue;\n // Cursor labels the top reasoning tier \"extra-high\"; the opencode\n // standard (models.dev) calls it \"xhigh\". Use the standard label for\n // the variant key so the cycler is consistent across providers, but\n // keep the Cursor wire-format value (\"extra-high\") in the params sent\n // to the API.\n const displayKey = value === \"extra-high\" ? \"xhigh\" : value;\n const key = out[displayKey] === undefined ? displayKey : `${param.id}-${displayKey}`;\n out[key] = { params: { ...defaults, [param.id]: value } };\n }\n continue;\n }\n\n // Non-reasoning boolean toggle (e.g. Cursor's `fast`). Default is OFF (see\n // defaultModelParams); expose a single opt-in variant that turns it ON.\n if (boolean && values.includes(\"true\")) {\n out[param.id.toLowerCase()] = { params: { ...defaults, [param.id]: \"true\" } };\n }\n // Non-reasoning enum params (e.g. `context`) remain unsupported in the picker.\n }\n\n return out;\n}\n","import type { ModelListItem } from \"@cursor/sdk\";\nimport { fingerprintApiKey, resolveCursorApiKey } from \"./api-key.js\";\nimport { readLatestModelCache, readModelCache, writeModelCache } from \"./model-cache.js\";\nimport { FALLBACK_MODELS } from \"./fallback-models.js\";\nimport { loadCursorSdk } from \"./cursor-runtime.js\";\nimport { buildModelVariants, defaultModelParams, type CursorVariant } from \"./model-variants.js\";\n\nexport type ModelSource = \"live\" | \"cache\" | \"fallback\";\n\nexport interface DiscoveryResult {\n models: ModelListItem[];\n source: ModelSource;\n /** Human-readable note when discovery degraded (e.g. missing key, error). */\n warning?: string;\n}\n\nexport interface DiscoverOptions {\n /** Explicit key; falls back to CURSOR_API_KEY. */\n apiKey?: string;\n /** Bypass the on-disk cache and force a live `Cursor.models.list()`. */\n forceRefresh?: boolean;\n}\n\n/**\n * Discover the Cursor model catalog. Tries (in order): on-disk cache (unless\n * forced), live `Cursor.models.list()`, then the static fallback snapshot.\n * Always resolves — failures degrade to the fallback with a `warning`.\n */\nexport async function discoverModels(options: DiscoverOptions = {}): Promise<DiscoveryResult> {\n const apiKey = resolveCursorApiKey(options.apiKey);\n if (!apiKey) {\n // No key here (e.g. the keyless `config` hook). Prefer the real catalog a\n // prior authed load cached, so opencode's picker shows the full list rather\n // than only the static snapshot.\n const latest = readLatestModelCache();\n if (latest && latest.length > 0) return { models: latest, source: \"cache\" };\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning:\n \"No Cursor API key found. Run `opencode auth login` and choose Cursor, or set CURSOR_API_KEY. Showing fallback models.\",\n };\n }\n\n const fingerprint = fingerprintApiKey(apiKey);\n\n if (!options.forceRefresh) {\n const cached = readModelCache(fingerprint);\n if (cached && cached.length > 0) {\n return { models: cached, source: \"cache\" };\n }\n }\n\n try {\n const { Cursor } = await loadCursorSdk();\n const models = await Cursor.models.list({ apiKey });\n if (models.length > 0) {\n writeModelCache(fingerprint, models);\n return { models, source: \"live\" };\n }\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning: \"Cursor.models.list() returned no models; showing fallback models.\",\n };\n } catch (err) {\n const detail = err instanceof Error ? err.message : String(err);\n // A stale cache is better than nothing on a transient failure.\n const stale = readModelCache(fingerprint);\n if (stale && stale.length > 0) {\n return { models: stale, source: \"cache\", warning: `Live discovery failed (${detail}); using cached models.` };\n }\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning: `Live discovery failed (${detail}); showing fallback models.`,\n };\n }\n}\n\n/** True when a model exposes a thinking/reasoning parameter. */\nexport function modelSupportsReasoning(item: ModelListItem): boolean {\n return (item.parameters ?? []).some((p) => /think|reason/i.test(p.id));\n}\n\n/** Shape of a single entry in opencode's `provider.<id>.models` config map. */\nexport interface OpencodeModelConfigEntry {\n id: string;\n name: string;\n attachment: boolean;\n reasoning: boolean;\n temperature: boolean;\n tool_call: boolean;\n /**\n * opencode model variants (thinking levels + plan mode). They MUST be seeded\n * here: opencode discards the plugin `provider.models()` hook for providers\n * absent from its models.dev catalog, so this config map is the only channel\n * through which cursor model variants reach the picker.\n */\n variants: Record<string, CursorVariant>;\n /**\n * Default `providerOptions.cursor` for the model, merged into every request\n * unless a variant overrides it. Carries the non-reasoning boolean defaults\n * (e.g. `{ params: { fast: \"false\" } }`) so the provider never silently runs\n * Cursor's server-side `fast` default. See {@link defaultModelParams}.\n */\n options: { params?: Record<string, string> };\n}\n\n/**\n * Map discovered Cursor models to opencode's provider config `models` map. The\n * Cursor SDK runs an agent (it calls tools itself), so every model is marked\n * `tool_call: true` and `temperature: false`.\n */\nexport function toOpencodeModels(items: ModelListItem[]): Record<string, OpencodeModelConfigEntry> {\n const out: Record<string, OpencodeModelConfigEntry> = {};\n for (const item of items) {\n const params = defaultModelParams(item);\n out[item.id] = {\n id: item.id,\n name: item.displayName || item.id,\n attachment: true,\n reasoning: modelSupportsReasoning(item),\n temperature: false,\n tool_call: true,\n variants: buildModelVariants(item),\n options: Object.keys(params).length > 0 ? { params } : {},\n };\n }\n return out;\n}\n","import type { Model as ModelV2 } from \"@opencode-ai/sdk/v2\";\nimport type { ModelListItem } from \"@cursor/sdk\";\nimport { modelSupportsReasoning } from \"../model-discovery.js\";\nimport { buildModelVariants, defaultModelParams } from \"../model-variants.js\";\n\nexport const PROVIDER_ID = \"cursor\";\nexport const NPM_PACKAGE = \"@stablekernel/opencode-cursor\";\n\n/**\n * The npm specifier opencode uses to load the provider SDK. Defaults to the\n * published package name; can be overridden with a `file://...` URL (which\n * opencode imports directly, skipping a registry install) via\n * `OPENCODE_CURSOR_PROVIDER_NPM` — useful for local development and CI before\n * the package is published.\n */\nexport function providerNpm(): string {\n return process.env.OPENCODE_CURSOR_PROVIDER_NPM?.trim() || NPM_PACKAGE;\n}\n\n/**\n * Build opencode's rich runtime `Model` objects from discovered Cursor models.\n * Used by the auth-aware `provider.models()` hook. Fields opencode does not get\n * from the Cursor catalog are filled with safe defaults (zero cost — Cursor\n * bills separately; generous context limits).\n */\nexport function buildModelV2Map(items: ModelListItem[]): Record<string, ModelV2> {\n const out: Record<string, ModelV2> = {};\n for (const item of items) {\n const params = defaultModelParams(item);\n out[item.id] = {\n id: item.id,\n providerID: PROVIDER_ID,\n api: { id: item.id, url: \"\", npm: providerNpm() },\n name: item.displayName || item.id,\n capabilities: {\n temperature: false,\n reasoning: modelSupportsReasoning(item),\n attachment: true,\n toolcall: true,\n input: { text: true, audio: false, image: true, video: false, pdf: false },\n output: { text: true, audio: false, image: false, video: false, pdf: false },\n interleaved: false,\n },\n cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },\n limit: { context: 200_000, output: 32_000 },\n status: \"active\",\n options: Object.keys(params).length > 0 ? { params } : {},\n headers: {},\n release_date: \"\",\n variants: buildModelVariants(item) as ModelV2[\"variants\"],\n };\n }\n return out;\n}\n","import type { Config } from \"@opencode-ai/plugin\";\nimport type { McpServerConfig } from \"@cursor/sdk\";\n\n/** The value type of opencode's `config.mcp` map. */\ntype OpencodeMcp = NonNullable<Config[\"mcp\"]>;\ntype OpencodeMcpEntry = OpencodeMcp[string];\n\n/**\n * Live MCP server status, keyed by server name, as reported by opencode's\n * `client.mcp.status()`. Only the `status` field is consumed; `\"connected\"`\n * means the server is currently usable. Mirrors the SDK's `McpStatus` union\n * without importing it (keeps this module dependency-light).\n */\nexport type McpStatusMap = Record<string, { status?: string } | undefined>;\n\n/** opencode runtime statuses that mean a server still needs OAuth to connect. */\nconst NEEDS_AUTH_STATUS = new Set([\"needs_auth\", \"needs_client_registration\"]);\n\n/** The OAuth client registration on a remote entry, or undefined when none. */\nfunction oauthConfig(\n\tentry: OpencodeMcpEntry,\n): { clientId?: string; clientSecret?: string; scope?: string } | undefined {\n\tif (entry.type !== \"remote\") return undefined;\n\t// `oauth` is `McpOAuthConfig | false | undefined`; both false and undefined\n\t// are falsy, so a truthy value is the client-registration object.\n\treturn entry.oauth ? entry.oauth : undefined;\n}\n\n/**\n * Map opencode's OAuth client registration to the Cursor SDK's `auth` block so\n * the Cursor agent can run its own OAuth flow. Returns undefined when there is\n * no `clientId` to share (e.g. RFC 7591 dynamic registration) — opencode's\n * access token itself never reaches `config.mcp`, so a bare URL would fail.\n */\nfunction toCursorAuth(\n\toauth:\n\t\t| { clientId?: string; clientSecret?: string; scope?: string }\n\t\t| undefined,\n):\n\t| { CLIENT_ID: string; CLIENT_SECRET?: string; scopes?: string[] }\n\t| undefined {\n\tif (!oauth?.clientId) return undefined;\n\tconst scopes = oauth.scope?.split(/\\s+/).filter(Boolean);\n\treturn {\n\t\tCLIENT_ID: oauth.clientId,\n\t\t...(oauth.clientSecret ? { CLIENT_SECRET: oauth.clientSecret } : {}),\n\t\t...(scopes && scopes.length > 0 ? { scopes } : {}),\n\t};\n}\n\n/**\n * Names of remote servers that require OAuth but cannot be forwarded to the\n * Cursor agent because no shareable client registration exists (dynamic\n * registration, or a `needs_auth` runtime status with no configured\n * `clientId`). The plugin surfaces these to the user instead of silently\n * forwarding a spec that would 401.\n */\nexport function findUnshareableOAuthServers(\n\tmcp: Config[\"mcp\"],\n\tstatus?: McpStatusMap,\n): string[] {\n\tconst names: string[] = [];\n\tif (!mcp) return names;\n\tfor (const [name, entry] of Object.entries(mcp) as Array<\n\t\t[string, OpencodeMcpEntry]\n\t>) {\n\t\tif (!entry || entry.type !== \"remote\") continue;\n\t\tif (!status && entry.enabled === false) continue;\n\t\tconst s = status?.[name]?.status;\n\t\tif (status && s !== \"connected\" && !NEEDS_AUTH_STATUS.has(s ?? \"\"))\n\t\t\tcontinue;\n\t\tconst oauth = oauthConfig(entry);\n\t\tconst needsOAuth = Boolean(oauth) || NEEDS_AUTH_STATUS.has(s ?? \"\");\n\t\tif (needsOAuth && !toCursorAuth(oauth)) names.push(name);\n\t}\n\treturn names;\n}\n\n/**\n * Translate opencode's configured MCP servers (`config.mcp`) into the Cursor\n * SDK's `McpServerConfig` shape so the same servers can be handed\n * to the Cursor agent via `Agent.create({ mcpServers })`.\n *\n * MCP servers are independent processes addressed by a launch spec, so opencode\n * and the Cursor agent can each connect to the same server. Disabled entries\n * (`enabled: false`) are skipped. The `timeout` field is dropped (no Cursor\n * equivalent). OAuth is mapped where possible: a remote server's `oauth` client\n * registration becomes Cursor's `auth` block so the agent runs its own OAuth\n * flow; servers needing OAuth with no shareable `clientId` are skipped (the\n * plugin reports them via {@link findUnshareableOAuthServers}).\n */\nexport function translateMcpServers(\n\tmcp: Config[\"mcp\"],\n\tstatus?: McpStatusMap,\n): Record<string, McpServerConfig> {\n\tconst out: Record<string, McpServerConfig> = {};\n\tif (!mcp) return out;\n\n\tfor (const [name, entry] of Object.entries(mcp) as Array<\n\t\t[string, OpencodeMcpEntry]\n\t>) {\n\t\tif (!entry) continue;\n\n\t\t// When a live status map is supplied (per-turn dynamic forwarding), it is\n\t\t// the source of truth: forward only servers opencode has currently\n\t\t// connected, so mid-session enable/disable propagates to the Cursor agent.\n\t\t// Without it (the startup config snapshot), fall back to the static\n\t\t// `enabled` flag.\n\t\tif (status) {\n\t\t\tif (status[name]?.status !== \"connected\") continue;\n\t\t} else if (entry.enabled === false) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"local\") {\n\t\t\tconst [command, ...args] = entry.command ?? [];\n\t\t\tif (!command) continue;\n\t\t\tout[name] = {\n\t\t\t\ttype: \"stdio\",\n\t\t\t\tcommand,\n\t\t\t\t...(args.length > 0 ? { args } : {}),\n\t\t\t\t...(entry.environment && Object.keys(entry.environment).length > 0\n\t\t\t\t\t? { env: entry.environment }\n\t\t\t\t\t: {}),\n\t\t\t};\n\t\t} else if (entry.type === \"remote\") {\n\t\t\tif (!entry.url) continue;\n\t\t\tconst oauth = oauthConfig(entry);\n\t\t\tconst auth = toCursorAuth(oauth);\n\t\t\t// OAuth server with no shareable client registration: opencode holds the\n\t\t\t// token and it never lands in config.mcp, so skip rather than forward a\n\t\t\t// bare URL that would 401. The plugin notifies the user (see\n\t\t\t// findUnshareableOAuthServers).\n\t\t\tif (oauth && !auth) continue;\n\t\t\tout[name] = {\n\t\t\t\ttype: \"http\",\n\t\t\t\turl: entry.url,\n\t\t\t\t...(entry.headers && Object.keys(entry.headers).length > 0\n\t\t\t\t\t? { headers: entry.headers }\n\t\t\t\t\t: {}),\n\t\t\t\t...(auth ? { auth } : {}),\n\t\t\t};\n\t\t}\n\t}\n\n\treturn out;\n}\n","import { tool, type ToolContext, type ToolDefinition } from \"@opencode-ai/plugin\";\nimport { runCloudAgent } from \"../provider/cloud-agent.js\";\nimport { runDelegate } from \"../provider/delegate.js\";\n\nconst s = tool.schema;\n\nexport interface CursorToolDeps {\n /**\n * Resolve the Cursor API key (from opencode auth, captured by the plugin's\n * auth loader, or the CURSOR_API_KEY env var). Returns undefined when no key\n * is available so the tool can return a clear \"needs auth\" message.\n */\n resolveApiKey: () => string | undefined;\n /** Default working directory for local delegation (the session worktree/cwd). */\n defaultCwd: () => string;\n}\n\nconst NEEDS_AUTH =\n \"No Cursor API key available. Run `opencode auth login` and choose Cursor, or set CURSOR_API_KEY.\";\n\n/**\n * Request approval for a sensitive Cursor invocation. `context.ask` is the\n * opencode mechanism a custom tool uses to gate itself; it honors the user's\n * `permission` config (allow resolves silently, ask prompts, deny rejects).\n *\n * Returns `{ ok: true }` when approved, or `{ ok: false, reason }` when the\n * request was rejected. We deliberately do not claim the rejection was a policy\n * \"deny\" — `context.ask` rejects on both an explicit deny and an internal\n * failure, and conflating them produces misleading messages. The gate is\n * fail-closed: any rejection (including a host that doesn't provide `ask`)\n * blocks the call rather than silently allowing it.\n */\nasync function requestApproval(\n context: ToolContext,\n permission: string,\n patterns: string[],\n metadata: Record<string, unknown>,\n): Promise<{ ok: boolean; reason?: string }> {\n try {\n await context.ask({ permission, patterns, always: patterns, metadata });\n return { ok: true };\n } catch (err) {\n return { ok: false, reason: err instanceof Error ? err.message : String(err) };\n }\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\n/**\n * Build the Cursor delegation tools that complement the native provider:\n * - `cursor_cloud_agent`: run a background agent on a remote repo (optionally\n * opening a PR) — work that maps poorly onto the synchronous provider path.\n * - `cursor_delegate`: run a single local Cursor turn as a permission-gated,\n * auditable tool call (for users who want Cursor as a delegate rather than\n * as their primary model).\n *\n * Both are gated via `context.ask`, so a user `permission` policy controls them.\n */\nexport function buildCursorTools(deps: CursorToolDeps): Record<string, ToolDefinition> {\n return {\n cursor_cloud_agent: tool({\n description:\n \"Launch a Cursor background ('cloud') agent on a remote repository. Runs autonomously \" +\n \"(may take minutes) and can open a pull request. Returns the cloud agent id, final \" +\n \"status, result, and PR url when available.\",\n args: {\n prompt: s.string().describe(\"The task/instruction for the background agent.\"),\n repoUrl: s\n .string()\n .describe(\"Target repository URL, e.g. https://github.com/owner/repo.\"),\n startingRef: s\n .string()\n .optional()\n .describe(\"Branch or ref to start from (defaults to the repo default branch).\"),\n model: s.string().optional().describe(\"Cursor model id (optional for cloud).\"),\n mode: s.enum([\"agent\", \"plan\"]).optional().describe(\"Conversation mode.\"),\n thinking: s.string().optional().describe(\"Thinking level, e.g. 'high'.\"),\n autoCreatePR: s\n .boolean()\n .optional()\n .describe(\"Open a pull request automatically when finished.\"),\n workOnCurrentBranch: s\n .boolean()\n .optional()\n .describe(\"Operate on the current branch instead of creating a new one.\"),\n },\n execute: async (args, context) => {\n const apiKey = deps.resolveApiKey();\n if (!apiKey) return NEEDS_AUTH;\n\n const approval = await requestApproval(\n context,\n \"cursor_cloud_agent\",\n [args.repoUrl],\n { repoUrl: args.repoUrl, autoCreatePR: args.autoCreatePR ?? false },\n );\n if (!approval.ok) {\n return `Cloud agent not approved for ${args.repoUrl}${approval.reason ? `: ${approval.reason}` : \".\"}`;\n }\n\n let result;\n try {\n result = await runCloudAgent({\n apiKey,\n prompt: args.prompt,\n repoUrl: args.repoUrl,\n ...(args.startingRef ? { startingRef: args.startingRef } : {}),\n ...(args.model ? { model: args.model } : {}),\n ...(args.mode ? { mode: args.mode } : {}),\n ...(args.thinking ? { thinking: args.thinking } : {}),\n ...(args.autoCreatePR !== undefined ? { autoCreatePR: args.autoCreatePR } : {}),\n ...(args.workOnCurrentBranch !== undefined\n ? { workOnCurrentBranch: args.workOnCurrentBranch }\n : {}),\n abortSignal: context.abort,\n });\n } catch (err) {\n return `Cloud agent failed: ${errorMessage(err)}`;\n }\n\n const lines = [\n `Cloud agent ${result.agentId} — ${result.status}`,\n ...(result.prUrl ? [`PR: ${result.prUrl}`] : []),\n ...(result.branches.length > 0\n ? [`Branches: ${result.branches.map((b) => b.branch ?? b.repoUrl).join(\", \")}`]\n : []),\n ...(result.result ? [\"\", result.result] : []),\n ...(result.progress.length > 0 ? [\"\", \"Progress:\", ...result.progress] : []),\n ];\n\n return {\n title: `Cursor cloud agent (${result.status})`,\n output: lines.join(\"\\n\"),\n metadata: {\n agentId: result.agentId,\n status: result.status,\n prUrl: result.prUrl ?? null,\n durationMs: result.durationMs ?? null,\n },\n };\n },\n }),\n\n cursor_delegate: tool({\n description:\n \"Delegate a single subtask to a local Cursor agent and return its result. Use to hand \" +\n \"off discrete work to Cursor while keeping your primary model in control. Permission-gated.\",\n args: {\n prompt: s.string().describe(\"The subtask to delegate to Cursor.\"),\n model: s.string().describe(\"Cursor model id to run the delegation on.\"),\n mode: s.enum([\"agent\", \"plan\"]).optional().describe(\"Conversation mode.\"),\n thinking: s.string().optional().describe(\"Thinking level, e.g. 'high'.\"),\n cwd: s\n .string()\n .optional()\n .describe(\"Working directory (defaults to the session directory).\"),\n sandbox: s.boolean().optional().describe(\"Run the agent's tools in Cursor's sandbox.\"),\n agentId: s\n .string()\n .optional()\n .describe(\"Resume a specific Cursor agent id instead of starting fresh.\"),\n },\n execute: async (args, context) => {\n const apiKey = deps.resolveApiKey();\n if (!apiKey) return NEEDS_AUTH;\n\n const approval = await requestApproval(context, \"cursor_delegate\", [args.model], {\n model: args.model,\n prompt: args.prompt,\n });\n if (!approval.ok) {\n return `Delegation to ${args.model} not approved${approval.reason ? `: ${approval.reason}` : \".\"}`;\n }\n\n let result;\n try {\n result = await runDelegate({\n apiKey,\n prompt: args.prompt,\n model: args.model,\n cwd: args.cwd ?? context.directory ?? deps.defaultCwd(),\n ...(args.mode ? { mode: args.mode } : {}),\n ...(args.thinking ? { thinking: args.thinking } : {}),\n ...(args.sandbox !== undefined ? { sandbox: args.sandbox } : {}),\n ...(args.agentId ? { agentId: args.agentId } : {}),\n abortSignal: context.abort,\n });\n } catch (err) {\n return `Delegation failed: ${errorMessage(err)}`;\n }\n\n const toolNote =\n result.toolActivity.length > 0\n ? `\\n\\n(${result.toolActivity.length} tool call(s)` +\n `${result.toolActivity.some((t) => t.isError) ? \", some failed\" : \"\"})`\n : \"\";\n\n return {\n title: `Cursor delegate (${args.model})`,\n output: (result.text || \"(no text output)\") + toolNote,\n metadata: {\n agentId: result.agentId,\n model: args.model,\n toolCalls: result.toolActivity.length,\n usage: result.usage ?? null,\n },\n };\n },\n }),\n };\n}\n","import type { AgentModeOption, ConversationStep, InteractionUpdate } from \"@cursor/sdk\";\nimport { loadCursorSdk } from \"../cursor-runtime.js\";\nimport { buildModelSelection } from \"./controls.js\";\n\n/**\n * A target repository for a cloud agent. Cursor's cloud runtime accepts an\n * array of repos; the tool surface exposes the common single-repo case.\n */\nexport interface CloudRepoTarget {\n url: string;\n startingRef?: string;\n}\n\nexport interface CloudAgentParams {\n apiKey: string;\n /** The instruction/task for the background agent. */\n prompt: string;\n /** Target repository URL (e.g. https://github.com/owner/repo). */\n repoUrl: string;\n /** Branch/ref to start from. Defaults to the repo's default branch. */\n startingRef?: string;\n /** Cursor model id. Optional for cloud (server picks a default otherwise). */\n model?: string;\n /** Conversation mode; defaults to \"agent\". */\n mode?: AgentModeOption;\n /** Convenience for the Cursor `thinking` model param (e.g. \"high\"). */\n thinking?: string;\n /** When true, open a PR automatically once the agent finishes. */\n autoCreatePR?: boolean;\n /** Operate on the current branch instead of creating a new one. */\n workOnCurrentBranch?: boolean;\n /** Cancels the run when aborted (wired to the tool's abort signal). */\n abortSignal?: AbortSignal;\n}\n\nexport interface CloudAgentBranch {\n repoUrl: string;\n branch?: string;\n prUrl?: string;\n}\n\nexport interface CloudAgentResult {\n agentId: string;\n /** Terminal run status: \"finished\" | \"error\" | \"cancelled\". */\n status: string;\n /** The agent's final textual result, when present. */\n result?: string;\n /** First PR url found across result branches (when `autoCreatePR`). */\n prUrl?: string;\n /** Per-repo branch/PR info reported by the run. */\n branches: CloudAgentBranch[];\n durationMs?: number;\n /** Human-readable progress lines captured from status/step/summary updates. */\n progress: string[];\n}\n\n/**\n * Run a Cursor background (\"cloud\") agent against a remote repository and wait\n * for it to finish, returning the final status, result text, and any PR url.\n *\n * A cloud agent can run for minutes and produce a PR rather than a chat reply,\n * which maps poorly onto the synchronous provider `doStream` path — so this is\n * exposed as an opencode tool instead (see plugin/index.ts). Progress is\n * collected into `progress[]` (opencode custom tools return a single result\n * rather than a live stream) and the lifecycle is bridged through the same\n * `loadCursorSdk` plumbing the provider uses.\n */\nexport async function runCloudAgent(params: CloudAgentParams): Promise<CloudAgentResult> {\n const { Agent } = await loadCursorSdk();\n const modelSelection = params.model\n ? buildModelSelection(params.model, params.thinking ? { thinking: params.thinking } : undefined)\n : undefined;\n const mode: AgentModeOption = params.mode ?? \"agent\";\n\n const createOptions = {\n apiKey: params.apiKey,\n ...(modelSelection ? { model: modelSelection } : {}),\n mode,\n cloud: {\n repos: [\n {\n url: params.repoUrl,\n ...(params.startingRef ? { startingRef: params.startingRef } : {}),\n },\n ],\n ...(params.autoCreatePR !== undefined ? { autoCreatePR: params.autoCreatePR } : {}),\n ...(params.workOnCurrentBranch !== undefined\n ? { workOnCurrentBranch: params.workOnCurrentBranch }\n : {}),\n },\n };\n\n const progress: string[] = [];\n const agent = await Agent.create(createOptions);\n\n // `onDelta` carries fine-grained updates; for a cloud (background) run the\n // higher-signal progress arrives via `onStep` (whole conversation steps) and\n // `run.onDidChangeStatus`. We capture all three — whichever the runtime emits.\n const onDelta = ({ update }: { update: InteractionUpdate }) => {\n if (update.type === \"summary\") progress.push(`summary: ${update.summary}`);\n };\n\n const onStep = ({ step }: { step: ConversationStep }) => {\n progress.push(`step: ${describeStep(step)}`);\n };\n\n try {\n const run = await agent.send(params.prompt, { mode, onDelta, onStep });\n\n const off = run.onDidChangeStatus?.((status: string) => {\n progress.push(`status: ${status}`);\n });\n const onAbort = () => {\n run.cancel().catch(() => {});\n };\n params.abortSignal?.addEventListener(\"abort\", onAbort);\n\n try {\n const result = await run.wait();\n const branches: CloudAgentBranch[] = (result.git?.branches ?? []).map((b) => ({\n repoUrl: b.repoUrl,\n ...(b.branch ? { branch: b.branch } : {}),\n ...(b.prUrl ? { prUrl: b.prUrl } : {}),\n }));\n const prUrl = branches.find((b) => b.prUrl)?.prUrl;\n return {\n agentId: agent.agentId,\n status: result.status,\n ...(result.result !== undefined ? { result: result.result } : {}),\n ...(prUrl ? { prUrl } : {}),\n branches,\n ...(result.durationMs !== undefined ? { durationMs: result.durationMs } : {}),\n progress,\n };\n } finally {\n off?.();\n params.abortSignal?.removeEventListener(\"abort\", onAbort);\n }\n } finally {\n try {\n agent.close();\n } catch {\n // best effort; cloud agents persist server-side regardless.\n }\n }\n}\n\n/** A short, log-friendly description of a conversation step for progress output. */\nfunction describeStep(step: ConversationStep): string {\n if (step.type === \"toolCall\") return `toolCall:${step.message.type}`;\n return step.type;\n}\n","import type { AgentModeOption } from \"@cursor/sdk\";\nimport type { CursorUsage } from \"./agent-events.js\";\nimport { streamAgentTurn } from \"./agent-events.js\";\nimport { resolveControls } from \"./controls.js\";\nimport { acquireAgent } from \"./session-pool.js\";\n\nexport interface DelegateParams {\n\tapiKey: string;\n\t/** The subtask to delegate to the Cursor agent. */\n\tprompt: string;\n\t/** Cursor model id to run the delegation on. */\n\tmodel: string;\n\t/** Conversation mode; defaults to \"agent\". */\n\tmode?: AgentModeOption;\n\t/** Convenience for the Cursor `thinking` model param (e.g. \"high\"). */\n\tthinking?: string;\n\t/** Working directory the local agent operates in. */\n\tcwd: string;\n\t/** Run the agent's tools inside Cursor's sandbox. */\n\tsandbox?: boolean;\n\t/** Resume a specific Cursor agent by id instead of creating a fresh one. */\n\tagentId?: string;\n\t/** Cancels the run when aborted (wired to the tool's abort signal). */\n\tabortSignal?: AbortSignal;\n}\n\nexport interface DelegateToolActivity {\n\tname: string;\n\tisError: boolean;\n}\n\nexport interface DelegateResult {\n\tagentId: string;\n\ttext: string;\n\treasoning: string;\n\ttoolActivity: DelegateToolActivity[];\n\tusage?: CursorUsage;\n}\n\n/**\n * Run a single delegated turn on a fresh (or explicitly resumed) local Cursor\n * agent and aggregate the outcome into a plain result. This backs the opt-in\n * `cursor_delegate` tool, which gives users a permission-gated boundary around\n * Cursor (the provider path runs Cursor's own loop without per-call gating).\n *\n * Reuses the provider's `acquireAgent` + `streamAgentTurn` plumbing; the turn\n * is consumed eagerly here because a tool returns a single result rather than a\n * live stream.\n */\nexport async function runDelegate(\n\tparams: DelegateParams,\n): Promise<DelegateResult> {\n\tconst { mode, modelSelection } = resolveControls(\n\t\tparams.model,\n\t\t{\n\t\t\tmode: params.mode ?? \"agent\",\n\t\t\t...(params.thinking ? { params: { thinking: params.thinking } } : {}),\n\t\t},\n\t\tundefined,\n\t);\n\n\tconst acquired = await acquireAgent({\n\t\tapiKey: params.apiKey,\n\t\tmodelSelection,\n\t\tmode,\n\t\tcwd: params.cwd,\n\t\t...(params.sandbox !== undefined ? { sandbox: params.sandbox } : {}),\n\t\t...(params.agentId ? { resumeAgentId: params.agentId } : {}),\n\t});\n\n\tconst text: string[] = [];\n\tconst reasoning: string[] = [];\n\tconst toolActivity: DelegateToolActivity[] = [];\n\tlet usage: CursorUsage | undefined;\n\n\ttry {\n\t\tfor await (const event of streamAgentTurn(\n\t\t\tacquired.agent,\n\t\t\t{ text: params.prompt },\n\t\t\t{\n\t\t\t\tmode,\n\t\t\t\t...(params.abortSignal ? { abortSignal: params.abortSignal } : {}),\n\t\t\t},\n\t\t)) {\n\t\t\tswitch (event.type) {\n\t\t\t\tcase \"text-delta\":\n\t\t\t\t\ttext.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"reasoning-delta\":\n\t\t\t\t\treasoning.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"tool-call\":\n\t\t\t\t\ttoolActivity.push({ name: event.name, isError: false });\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"tool-result\":\n\t\t\t\t\tif (event.isError)\n\t\t\t\t\t\ttoolActivity.push({ name: event.name, isError: true });\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"usage\":\n\t\t\t\t\tusage = event.usage;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"finish\":\n\t\t\t\t\t// The aggregated result text; prefer it when deltas were absent.\n\t\t\t\t\tif (event.text && text.length === 0) text.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t} finally {\n\t\tacquired.release();\n\t}\n\n\treturn {\n\t\tagentId: acquired.agent.agentId,\n\t\ttext: text.join(\"\"),\n\t\treasoning: reasoning.join(\"\"),\n\t\ttoolActivity,\n\t\t...(usage ? { usage } : {}),\n\t};\n}\n","import type { Config, Plugin } from \"@opencode-ai/plugin\";\nimport type { Auth } from \"@opencode-ai/sdk/v2\";\nimport type { McpServerConfig } from \"@cursor/sdk\";\nimport { resolveCursorApiKey } from \"../api-key.js\";\nimport { discoverModels, toOpencodeModels } from \"../model-discovery.js\";\nimport { buildModelV2Map, PROVIDER_ID, providerNpm } from \"./model-v2.js\";\nimport {\n\tfindUnshareableOAuthServers,\n\ttype McpStatusMap,\n\ttranslateMcpServers,\n} from \"./mcp-config.js\";\nimport { buildCursorTools } from \"./cursor-tools.js\";\n\nfunction apiKeyFromAuth(auth: Auth | undefined): string | undefined {\n\treturn auth?.type === \"api\" ? auth.key : undefined;\n}\n\n/**\n * opencode plugin that adds a \"Cursor\" provider backed by the official Cursor\n * SDK (`@cursor/sdk`).\n *\n * - `auth`: registers an API-key login for Cursor and a `loader` that feeds the\n * key into the AI-SDK provider factory. The key is validated on first use\n * (model discovery / first call), not at login — see the note on `methods`.\n * - `config`: registers the provider (npm package + discovered/fallback models)\n * so it shows up in opencode immediately.\n * - `provider.models`: auth-aware live model discovery via `Cursor.models.list`.\n * - `tool.cursor_refresh_models`: force-refresh the model catalog.\n */\nexport const CursorPlugin: Plugin = async (input) => {\n\t// The Cursor API key resolved by opencode's auth loader, captured so the\n\t// delegation tools (which don't receive auth directly) can reuse it. Falls\n\t// back to the CURSOR_API_KEY env var when the loader hasn't run.\n\tlet capturedApiKey: string | undefined;\n\n\t// opencode client + MCP-forwarding settings captured at config time so the\n\t// per-turn chat.params hook can re-forward the *live* MCP server set\n\t// (reflecting mid-session enable/disable) rather than the startup snapshot.\n\tconst client = input?.client;\n\tconst directory = input?.directory;\n\tlet forwardMcp = true;\n\tlet userMcp: Record<string, McpServerConfig> = {};\n\t// OAuth servers we've already warned about, so the toast fires once per\n\t// server rather than on every turn.\n\tconst warnedOAuth = new Set<string>();\n\n\treturn {\n\t\tauth: {\n\t\t\tprovider: PROVIDER_ID,\n\t\t\tloader: async (getAuth) => {\n\t\t\t\tconst apiKey = resolveCursorApiKey(\n\t\t\t\t\tapiKeyFromAuth(await getAuth().catch(() => undefined)),\n\t\t\t\t);\n\t\t\t\tif (apiKey) {\n\t\t\t\t\tcapturedApiKey = apiKey;\n\t\t\t\t\t// The `config` hook (which seeds opencode's model picker) runs without\n\t\t\t\t\t// a key. Warm the catalog cache here — the loader is the hook that\n\t\t\t\t\t// reliably has the key — so the next launch seeds the full live\n\t\t\t\t\t// catalog instead of the static fallback. Fire-and-forget: discovery\n\t\t\t\t\t// never throws and must not block auth/provider load.\n\t\t\t\t\tvoid discoverModels({ apiKey });\n\t\t\t\t}\n\t\t\t\treturn apiKey ? { apiKey } : {};\n\t\t\t},\n\t\t\t// A single API-key method. opencode always shows its built-in \"Enter your\n\t\t\t// API key\" prompt for `type: \"api\"`, so we intentionally do NOT declare\n\t\t\t// custom `prompts` (that asks for the key a second time) or an `authorize`\n\t\t\t// callback. opencode only passes `authorize` the *custom-prompt* inputs —\n\t\t\t// never the built-in key — so validating the key in `authorize` would\n\t\t\t// force that redundant extra prompt. Instead the key is validated on first\n\t\t\t// use (model discovery / the first call both surface a bad key clearly).\n\t\t\tmethods: [{ type: \"api\", label: \"Cursor API Key\" }],\n\t\t},\n\n\t\tconfig: async (config) => {\n\t\t\tconst { models } = await discoverModels({});\n\t\t\tconfig.provider ??= {};\n\t\t\tconst existing = config.provider[PROVIDER_ID] ?? {};\n\t\t\tconst existingOptions = (existing.options ?? {}) as Record<\n\t\t\t\tstring,\n\t\t\t\tunknown\n\t\t\t>;\n\n\t\t\t// Forward opencode's configured MCP servers to the Cursor\n\t\t\t// agent so it can use the same servers. Opt out via\n\t\t\t// `provider.cursor.options.forwardMcp: false`.\n\t\t\tforwardMcp = existingOptions[\"forwardMcp\"] !== false;\n\t\t\tuserMcp = (existingOptions[\"mcpServers\"] ?? {}) as Record<\n\t\t\t\tstring,\n\t\t\t\tMcpServerConfig\n\t\t\t>;\n\t\t\tconst mcpServers = forwardMcp\n\t\t\t\t? { ...userMcp, ...translateMcpServers(config.mcp) }\n\t\t\t\t: userMcp;\n\n\t\t\tconfig.provider[PROVIDER_ID] = {\n\t\t\t\tname: \"Cursor\",\n\t\t\t\tnpm: providerNpm(),\n\t\t\t\t...existing,\n\t\t\t\toptions: {\n\t\t\t\t\t...existingOptions,\n\t\t\t\t\t...(Object.keys(mcpServers).length > 0 ? { mcpServers } : {}),\n\t\t\t\t},\n\t\t\t\tmodels: { ...toOpencodeModels(models), ...(existing.models ?? {}) },\n\t\t\t};\n\t\t},\n\n\t\tprovider: {\n\t\t\tid: PROVIDER_ID,\n\t\t\tmodels: async (_provider, ctx) => {\n\t\t\t\tconst apiKey = apiKeyFromAuth(ctx.auth);\n\t\t\t\tconst { models } = await discoverModels({ apiKey });\n\t\t\t\treturn buildModelV2Map(models);\n\t\t\t},\n\t\t},\n\n\t\t// Bridge opencode's session id to the provider: it lands in\n\t\t// providerOptions.cursor.sessionID, which the provider reads to pool/resume a\n\t\t// Cursor agent per session (when the `session` option is enabled).\n\t\t//\n\t\t// Also map opencode's plan AGENT to Cursor's plan mode. This hook fires\n\t\t// after opencode merges the selected variant into `output.options`, so an\n\t\t// explicit mode from the `plan` variant (or model options) wins — the\n\t\t// agent-based default only applies when no mode was set.\n\t\t\"chat.params\": async (input, output) => {\n\t\t\tif (input.model?.providerID !== PROVIDER_ID) return;\n\t\t\toutput.options = {\n\t\t\t\t...(output.options ?? {}),\n\t\t\t\tsessionID: input.sessionID,\n\t\t\t};\n\t\t\tif (input.agent === \"plan\" && output.options[\"mode\"] === undefined) {\n\t\t\t\toutput.options[\"mode\"] = \"plan\";\n\t\t\t}\n\n\t\t\t// Dynamically re-forward MCP servers from opencode's *live* state so\n\t\t\t// mid-session enable/disable reaches the Cursor agent (the config hook\n\t\t\t// only snapshots the set once, at startup). `client.mcp.status()` is the\n\t\t\t// runtime truth (connected/disabled/...) and `client.config.get()`\n\t\t\t// supplies the launch specs. On any failure we leave the static snapshot\n\t\t\t// (already baked into the provider options) in place.\n\t\t\tif (forwardMcp && client) {\n\t\t\t\ttry {\n\t\t\t\t\tconst query = directory ? { query: { directory } } : undefined;\n\t\t\t\t\tconst [cfgRes, statusRes] = await Promise.all([\n\t\t\t\t\t\tclient.config.get(),\n\t\t\t\t\t\tclient.mcp.status(query),\n\t\t\t\t\t]);\n\t\t\t\t\tconst liveMcp = (cfgRes?.data as Config | undefined)?.mcp;\n\t\t\t\t\tconst status = statusRes?.data as McpStatusMap | undefined;\n\t\t\t\t\tif (status) {\n\t\t\t\t\t\toutput.options[\"mcpServers\"] = {\n\t\t\t\t\t\t\t...userMcp,\n\t\t\t\t\t\t\t...translateMcpServers(liveMcp, status),\n\t\t\t\t\t\t};\n\t\t\t\t\t\t// Notify (once) about OAuth servers we can't forward: opencode\n\t\t\t\t\t\t// holds their token and it never reaches config.mcp, so the\n\t\t\t\t\t\t// Cursor agent can't connect. Only those without a shareable\n\t\t\t\t\t\t// client registration are skipped; ones with a clientId are\n\t\t\t\t\t\t// forwarded with an `auth` block for the agent's own OAuth flow.\n\t\t\t\t\t\tconst unshareable = findUnshareableOAuthServers(\n\t\t\t\t\t\t\tliveMcp,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t).filter((name) => !warnedOAuth.has(name));\n\t\t\t\t\t\tif (unshareable.length > 0) {\n\t\t\t\t\t\t\tfor (const name of unshareable) warnedOAuth.add(name);\n\t\t\t\t\t\t\tconst plural = unshareable.length > 1;\n\t\t\t\t\t\t\tvoid client.tui\n\t\t\t\t\t\t\t\t.showToast({\n\t\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\t\ttitle: \"Cursor MCP\",\n\t\t\t\t\t\t\t\t\t\tmessage: `Skipped OAuth MCP server${plural ? \"s\" : \"\"}: ${unshareable.join(\", \")}. opencode's token can't be shared with the Cursor agent; configure an OAuth clientId to forward ${plural ? \"them\" : \"it\"}.`,\n\t\t\t\t\t\t\t\t\t\tvariant: \"warning\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t.catch(() => {});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Keep the static snapshot; live forwarding is best-effort.\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\ttool: {\n\t\t\tcursor_refresh_models: {\n\t\t\t\tdescription:\n\t\t\t\t\t\"Refresh the live Cursor model catalog (bypasses the 24h cache) and report the available models.\",\n\t\t\t\targs: {},\n\t\t\t\texecute: async () => {\n\t\t\t\t\tconst result = await discoverModels({ forceRefresh: true });\n\t\t\t\t\tconst lines = result.models.map(\n\t\t\t\t\t\t(m) => `- ${m.id} — ${m.displayName}`,\n\t\t\t\t\t);\n\t\t\t\t\tconst header =\n\t\t\t\t\t\tresult.source === \"live\"\n\t\t\t\t\t\t\t? `Refreshed ${result.models.length} Cursor models (live):`\n\t\t\t\t\t\t\t: `Could not fetch live models (${result.source}). ${result.warning ?? \"\"}`.trim();\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttitle: `Cursor models (${result.source})`,\n\t\t\t\t\t\toutput: [header, ...lines].join(\"\\n\"),\n\t\t\t\t\t\tmetadata: { source: result.source, count: result.models.length },\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t},\n\t\t\t// Delegation tools that complement the provider: a cloud/background agent\n\t\t\t// and a permission-gated local delegate. They resolve the Cursor key from\n\t\t\t// the auth loader (captured above) or CURSOR_API_KEY.\n\t\t\t...buildCursorTools({\n\t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Y;AACpB,gBAAM,SAAS,MAAM,eAAe,EAAE,cAAc,KAAK,CAAC;AAC1D,gBAAM,QAAQ,OAAO,OAAO;AAAA,YAC3B,CAAC,MAAM,KAAK,EAAE,EAAE,WAAM,EAAE,WAAW;AAAA,UACpC;AACA,gBAAM,SACL,OAAO,WAAW,SACf,aAAa,OAAO,OAAO,MAAM,2BACjC,gCAAgC,OAAO,MAAM,MAAM,OAAO,WAAW,EAAE,GAAG,KAAK;AACnF,iBAAO;AAAA,YACN,OAAO,kBAAkB,OAAO,MAAM;AAAA,YACtC,QAAQ,CAAC,QAAQ,GAAG,KAAK,EAAE,KAAK,IAAI;AAAA,YACpC,UAAU,EAAE,QAAQ,OAAO,QAAQ,OAAO,OAAO,OAAO,OAAO;AAAA,UAChE;AAAA,QACD;AAAA,MACD;AAAA;AAAA;AAAA;AAAA,MAIA,GAAG,iBAAiB;AAAA,QACnB,eAAe,MAAM,oBAAoB,cAAc;AAAA,QACvD,YAAY,MAAM,OAAO,aAAa,QAAQ,IAAI;AAAA,MACnD,CAAC;AAAA,IACF;AAAA,EACD;AACD;AAEA,IAAO,iBAAQ;","names":["s","input"]}
1
+ {"version":3,"sources":["../../src/model-cache.ts","../../src/fallback-models.ts","../../src/model-variants.ts","../../src/model-discovery.ts","../../src/plugin/model-v2.ts","../../src/plugin/mcp-config.ts","../../src/plugin/cursor-tools.ts","../../src/provider/cloud-agent.ts","../../src/provider/delegate.ts","../../src/version-check.ts","../../src/plugin/index.ts"],"sourcesContent":["import { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir, tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { ModelListItem } from \"@cursor/sdk\";\n\n/** Default cache lifetime: 24 hours, overridable via env. */\nconst DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;\n\nfunction ttlMs(): number {\n const raw = process.env.OPENCODE_CURSOR_MODEL_CACHE_TTL_MS;\n const parsed = raw ? Number.parseInt(raw, 10) : NaN;\n return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_TTL_MS;\n}\n\nfunction cacheDir(): string {\n const base =\n process.env.XDG_CACHE_HOME?.trim() ||\n (homedir() ? join(homedir(), \".cache\") : tmpdir());\n return join(base, \"opencode-cursor\");\n}\n\nfunction cacheFile(fingerprint: string): string {\n return join(cacheDir(), `models-${fingerprint}.json`);\n}\n\n/**\n * Key-independent \"latest known catalog\" file. The `config` plugin hook runs\n * without access to the stored API key, so it can't read the per-key cache.\n * This file lets a keyless caller (the config hook) seed opencode's model\n * picker with the real catalog that a previous *authed* load discovered.\n */\nfunction latestCacheFile(): string {\n return join(cacheDir(), \"models-latest.json\");\n}\n\n/** The latest-catalog seed is kept longer than the per-key cache: the catalog\n * is stable and this only feeds pre-auth UI seeding. */\nconst LATEST_TTL_MS = 30 * 24 * 60 * 60 * 1000;\n\ninterface CacheEnvelope {\n savedAt: number;\n models: ModelListItem[];\n}\n\nfunction readCacheFile(file: string, maxAgeMs: number): ModelListItem[] | undefined {\n try {\n const parsed = JSON.parse(readFileSync(file, \"utf8\")) as CacheEnvelope;\n if (!parsed?.savedAt || !Array.isArray(parsed.models)) return undefined;\n if (Date.now() - parsed.savedAt > maxAgeMs) return undefined;\n return parsed.models;\n } catch {\n return undefined;\n }\n}\n\nfunction writeCacheFile(file: string, models: ModelListItem[]): void {\n try {\n mkdirSync(cacheDir(), { recursive: true });\n const envelope: CacheEnvelope = { savedAt: Date.now(), models };\n writeFileSync(file, JSON.stringify(envelope), \"utf8\");\n } catch {\n // Caching is an optimization; ignore write failures.\n }\n}\n\n/**\n * Return cached models for the given API-key fingerprint when present and still\n * fresh, otherwise `undefined`. Never throws on a missing/corrupt cache.\n */\nexport function readModelCache(fingerprint: string): ModelListItem[] | undefined {\n return readCacheFile(cacheFile(fingerprint), ttlMs());\n}\n\n/** Persist the discovered model list (per-key cache + key-independent latest\n * catalog). Best-effort; never throws. */\nexport function writeModelCache(fingerprint: string, models: ModelListItem[]): void {\n writeCacheFile(cacheFile(fingerprint), models);\n writeCacheFile(latestCacheFile(), models);\n}\n\n/**\n * Return the most recently discovered catalog regardless of API key, when\n * present and within {@link LATEST_TTL_MS}. Used by the keyless `config` hook to\n * seed the picker with the real catalog after a prior authed load.\n */\nexport function readLatestModelCache(): ModelListItem[] | undefined {\n return readCacheFile(latestCacheFile(), LATEST_TTL_MS);\n}\n","import type { ModelListItem } from \"@cursor/sdk\";\n\n/**\n * A small static snapshot of well-known Cursor models, used only when live\n * discovery is unavailable (no API key, offline, or an SDK error). The live\n * `Cursor.models.list()` result always takes precedence; this just lets the\n * provider appear in opencode with sensible defaults so the user can reach the\n * login flow. Refresh the real catalog with the `cursor_refresh_models` tool.\n */\nexport const FALLBACK_MODELS: ModelListItem[] = [\n {\n id: \"composer-2.5\",\n displayName: \"Composer 2.5\",\n description: \"Cursor's default agent model (fallback entry).\",\n parameters: [\n { id: \"thinking\", displayName: \"Thinking\", values: [{ value: \"off\" }, { value: \"on\" }] },\n ],\n },\n { id: \"claude-opus-4-8\", displayName: \"Claude Opus 4.8 (via Cursor)\" },\n { id: \"claude-sonnet-4-6\", displayName: \"Claude Sonnet 4.6 (via Cursor)\" },\n { id: \"gpt-5.5\", displayName: \"GPT-5.5 (via Cursor)\" },\n];\n","import type { ModelListItem } from \"@cursor/sdk\";\n\n/**\n * A Cursor model \"variant\" as opencode stores it: an options object that, when\n * the variant is selected, is merged into `providerOptions.cursor` and read back\n * by {@link resolveControls}.\n */\nexport interface CursorVariant {\n params?: Record<string, string>;\n mode?: \"agent\" | \"plan\";\n}\n\nconst REASONING_PARAM = /think|reason|effort/i;\nconst BOOLEAN_VALUES = new Set([\"true\", \"false\"]);\n\nfunction paramValues(param: NonNullable<ModelListItem[\"parameters\"]>[number]): string[] {\n return (param.values ?? []).map((v) => v.value);\n}\n\nfunction isBooleanParam(values: string[]): boolean {\n return values.length > 0 && values.every((v) => BOOLEAN_VALUES.has(v));\n}\n\n/**\n * Params opencode must send by DEFAULT for this model — i.e. when the user has\n * NOT picked a variant. Non-reasoning boolean toggles (notably Cursor's `fast`)\n * are pinned OFF here so the provider never silently inherits Cursor's\n * server-side default, which is `fast: true` for several models (composer-*,\n * gpt-*-codex). The user opts back IN via the matching picker variant.\n *\n * Seeded into each model's opencode `options.params` (see `toOpencodeModels` /\n * `buildModelV2Map`); {@link resolveControls} merges it into the request.\n */\nexport function defaultModelParams(item: ModelListItem): Record<string, string> {\n const out: Record<string, string> = {};\n for (const param of item.parameters ?? []) {\n if (REASONING_PARAM.test(param.id)) continue;\n if (isBooleanParam(paramValues(param))) out[param.id] = \"false\";\n }\n return out;\n}\n\n/**\n * Derive opencode model variants from a Cursor model's parameters so the\n * variant picker can expose thinking/reasoning levels plus the `fast` toggle.\n * Each variant's object is exactly what {@link resolveControls} consumes. Plan\n * mode is NOT a variant: opencode's plan agent (Tab) is mapped to Cursor's plan\n * mode by the plugin's `chat.params` hook.\n *\n * Every variant for a fast-capable model carries an explicit `fast` value\n * (reasoning variants pin it OFF via {@link defaultModelParams}; the `fast`\n * variant turns it ON) so a selection never depends on Cursor's server-side\n * default for an omitted param.\n */\nexport function buildModelVariants(item: ModelListItem): Record<string, CursorVariant> {\n const out: Record<string, CursorVariant> = {};\n // Non-reasoning boolean defaults (e.g. { fast: \"false\" }), pinned into every\n // reasoning variant so picking a reasoning level never re-enables fast.\n const defaults = defaultModelParams(item);\n\n // Pre-pass: does any reasoning param expose a non-boolean effort enum (e.g.\n // [\"low\",\"medium\",\"high\",\"xhigh\",\"max\"])? When it does, a coexisting boolean\n // reasoning toggle (Cursor's `thinking=[\"false\",\"true\"]` on claude-* models)\n // is redundant — selecting any effort level already enables reasoning — and\n // surfacing it would add a stray `thinking` variant the standard opencode\n // providers don't show. Suppress the boolean variant for parity. Order-\n // independent: the enum may be declared before or after the boolean.\n const hasEffortEnum = (item.parameters ?? []).some(\n (p) => REASONING_PARAM.test(p.id) && !isBooleanParam(paramValues(p)) && paramValues(p).length > 0,\n );\n\n for (const param of item.parameters ?? []) {\n const values = paramValues(param);\n if (values.length === 0) continue;\n const boolean = isBooleanParam(values);\n\n if (REASONING_PARAM.test(param.id)) {\n if (boolean) {\n // Boolean toggle (e.g. thinking=[\"false\",\"true\"]). Literal true/false\n // variant names are meaningless in the picker — surface a single\n // variant named after the param that switches it on. \"Off\" is the\n // model's default (no variant selected). Skipped entirely when an\n // effort enum coexists (see hasEffortEnum above).\n if (!hasEffortEnum && values.includes(\"true\")) {\n out[param.id.toLowerCase()] = { params: { ...defaults, [param.id]: \"true\" } };\n }\n continue;\n }\n\n for (const value of values) {\n // `none` means reasoning OFF — the model's default when no variant is\n // selected. Surfacing it as a selectable variant is meaningless (you\n // get it by picking nothing), so skip it. Standard providers\n // (models.dev) include `none` in their effort values, but the\n // no-variant-selected state already represents it.\n if (value === \"none\") continue;\n // Cursor labels the top reasoning tier \"extra-high\"; the opencode\n // standard (models.dev) calls it \"xhigh\". Use the standard label for\n // the variant key so the cycler is consistent across providers, but\n // keep the Cursor wire-format value (\"extra-high\") in the params sent\n // to the API.\n const displayKey = value === \"extra-high\" ? \"xhigh\" : value;\n const key = out[displayKey] === undefined ? displayKey : `${param.id}-${displayKey}`;\n out[key] = { params: { ...defaults, [param.id]: value } };\n }\n continue;\n }\n\n // Non-reasoning boolean toggle (e.g. Cursor's `fast`). Default is OFF (see\n // defaultModelParams); expose a single opt-in variant that turns it ON.\n if (boolean && values.includes(\"true\")) {\n out[param.id.toLowerCase()] = { params: { ...defaults, [param.id]: \"true\" } };\n }\n // Non-reasoning enum params (e.g. `context`) remain unsupported in the picker.\n }\n\n return out;\n}\n","import type { ModelListItem } from \"@cursor/sdk\";\nimport { fingerprintApiKey, resolveCursorApiKey } from \"./api-key.js\";\nimport { readLatestModelCache, readModelCache, writeModelCache } from \"./model-cache.js\";\nimport { FALLBACK_MODELS } from \"./fallback-models.js\";\nimport { loadCursorSdk } from \"./cursor-runtime.js\";\nimport { buildModelVariants, defaultModelParams, type CursorVariant } from \"./model-variants.js\";\n\nexport type ModelSource = \"live\" | \"cache\" | \"fallback\";\n\nexport interface DiscoveryResult {\n models: ModelListItem[];\n source: ModelSource;\n /** Human-readable note when discovery degraded (e.g. missing key, error). */\n warning?: string;\n}\n\nexport interface DiscoverOptions {\n /** Explicit key; falls back to CURSOR_API_KEY. */\n apiKey?: string;\n /** Bypass the on-disk cache and force a live `Cursor.models.list()`. */\n forceRefresh?: boolean;\n}\n\n/**\n * Discover the Cursor model catalog. Tries (in order): on-disk cache (unless\n * forced), live `Cursor.models.list()`, then the static fallback snapshot.\n * Always resolves — failures degrade to the fallback with a `warning`.\n */\nexport async function discoverModels(options: DiscoverOptions = {}): Promise<DiscoveryResult> {\n const apiKey = resolveCursorApiKey(options.apiKey);\n if (!apiKey) {\n // No key here (e.g. the keyless `config` hook). Prefer the real catalog a\n // prior authed load cached, so opencode's picker shows the full list rather\n // than only the static snapshot.\n const latest = readLatestModelCache();\n if (latest && latest.length > 0) return { models: latest, source: \"cache\" };\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning:\n \"No Cursor API key found. Run `opencode auth login` and choose Cursor, or set CURSOR_API_KEY. Showing fallback models.\",\n };\n }\n\n const fingerprint = fingerprintApiKey(apiKey);\n\n if (!options.forceRefresh) {\n const cached = readModelCache(fingerprint);\n if (cached && cached.length > 0) {\n return { models: cached, source: \"cache\" };\n }\n }\n\n try {\n const { Cursor } = await loadCursorSdk();\n const models = await Cursor.models.list({ apiKey });\n if (models.length > 0) {\n writeModelCache(fingerprint, models);\n return { models, source: \"live\" };\n }\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning: \"Cursor.models.list() returned no models; showing fallback models.\",\n };\n } catch (err) {\n const detail = err instanceof Error ? err.message : String(err);\n // A stale cache is better than nothing on a transient failure.\n const stale = readModelCache(fingerprint);\n if (stale && stale.length > 0) {\n return { models: stale, source: \"cache\", warning: `Live discovery failed (${detail}); using cached models.` };\n }\n return {\n models: FALLBACK_MODELS,\n source: \"fallback\",\n warning: `Live discovery failed (${detail}); showing fallback models.`,\n };\n }\n}\n\n/** True when a model exposes a thinking/reasoning parameter. */\nexport function modelSupportsReasoning(item: ModelListItem): boolean {\n return (item.parameters ?? []).some((p) => /think|reason/i.test(p.id));\n}\n\n/** Shape of a single entry in opencode's `provider.<id>.models` config map. */\nexport interface OpencodeModelConfigEntry {\n id: string;\n name: string;\n attachment: boolean;\n reasoning: boolean;\n temperature: boolean;\n tool_call: boolean;\n /**\n * opencode model variants (thinking levels + plan mode). They MUST be seeded\n * here: opencode discards the plugin `provider.models()` hook for providers\n * absent from its models.dev catalog, so this config map is the only channel\n * through which cursor model variants reach the picker.\n */\n variants: Record<string, CursorVariant>;\n /**\n * Default `providerOptions.cursor` for the model, merged into every request\n * unless a variant overrides it. Carries the non-reasoning boolean defaults\n * (e.g. `{ params: { fast: \"false\" } }`) so the provider never silently runs\n * Cursor's server-side `fast` default. See {@link defaultModelParams}.\n */\n options: { params?: Record<string, string> };\n}\n\n/**\n * Map discovered Cursor models to opencode's provider config `models` map. The\n * Cursor SDK runs an agent (it calls tools itself), so every model is marked\n * `tool_call: true` and `temperature: false`.\n */\nexport function toOpencodeModels(items: ModelListItem[]): Record<string, OpencodeModelConfigEntry> {\n const out: Record<string, OpencodeModelConfigEntry> = {};\n for (const item of items) {\n const params = defaultModelParams(item);\n out[item.id] = {\n id: item.id,\n name: item.displayName || item.id,\n attachment: true,\n reasoning: modelSupportsReasoning(item),\n temperature: false,\n tool_call: true,\n variants: buildModelVariants(item),\n options: Object.keys(params).length > 0 ? { params } : {},\n };\n }\n return out;\n}\n","import type { Model as ModelV2 } from \"@opencode-ai/sdk/v2\";\nimport type { ModelListItem } from \"@cursor/sdk\";\nimport { modelSupportsReasoning } from \"../model-discovery.js\";\nimport { buildModelVariants, defaultModelParams } from \"../model-variants.js\";\n\nexport const PROVIDER_ID = \"cursor\";\nexport const NPM_PACKAGE = \"@stablekernel/opencode-cursor\";\n\n/**\n * The npm specifier opencode uses to load the provider SDK. Defaults to the\n * published package name; can be overridden with a `file://...` URL (which\n * opencode imports directly, skipping a registry install) via\n * `OPENCODE_CURSOR_PROVIDER_NPM` — useful for local development and CI before\n * the package is published.\n */\nexport function providerNpm(): string {\n return process.env.OPENCODE_CURSOR_PROVIDER_NPM?.trim() || NPM_PACKAGE;\n}\n\n/**\n * Build opencode's rich runtime `Model` objects from discovered Cursor models.\n * Used by the auth-aware `provider.models()` hook. Fields opencode does not get\n * from the Cursor catalog are filled with safe defaults (zero cost — Cursor\n * bills separately; generous context limits).\n */\nexport function buildModelV2Map(items: ModelListItem[]): Record<string, ModelV2> {\n const out: Record<string, ModelV2> = {};\n for (const item of items) {\n const params = defaultModelParams(item);\n out[item.id] = {\n id: item.id,\n providerID: PROVIDER_ID,\n api: { id: item.id, url: \"\", npm: providerNpm() },\n name: item.displayName || item.id,\n capabilities: {\n temperature: false,\n reasoning: modelSupportsReasoning(item),\n attachment: true,\n toolcall: true,\n input: { text: true, audio: false, image: true, video: false, pdf: false },\n output: { text: true, audio: false, image: false, video: false, pdf: false },\n interleaved: false,\n },\n cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },\n limit: { context: 200_000, output: 32_000 },\n status: \"active\",\n options: Object.keys(params).length > 0 ? { params } : {},\n headers: {},\n release_date: \"\",\n variants: buildModelVariants(item) as ModelV2[\"variants\"],\n };\n }\n return out;\n}\n","import type { Config } from \"@opencode-ai/plugin\";\nimport type { McpServerConfig } from \"@cursor/sdk\";\n\n/** The value type of opencode's `config.mcp` map. */\ntype OpencodeMcp = NonNullable<Config[\"mcp\"]>;\ntype OpencodeMcpEntry = OpencodeMcp[string];\n\n/**\n * Live MCP server status, keyed by server name, as reported by opencode's\n * `client.mcp.status()`. Only the `status` field is consumed; `\"connected\"`\n * means the server is currently usable. Mirrors the SDK's `McpStatus` union\n * without importing it (keeps this module dependency-light).\n */\nexport type McpStatusMap = Record<string, { status?: string } | undefined>;\n\n/** opencode runtime statuses that mean a server still needs OAuth to connect. */\nconst NEEDS_AUTH_STATUS = new Set([\"needs_auth\", \"needs_client_registration\"]);\n\n/** The OAuth client registration on a remote entry, or undefined when none. */\nfunction oauthConfig(\n\tentry: OpencodeMcpEntry,\n): { clientId?: string; clientSecret?: string; scope?: string } | undefined {\n\tif (entry.type !== \"remote\") return undefined;\n\t// `oauth` is `McpOAuthConfig | false | undefined`; both false and undefined\n\t// are falsy, so a truthy value is the client-registration object.\n\treturn entry.oauth ? entry.oauth : undefined;\n}\n\n/**\n * Map opencode's OAuth client registration to the Cursor SDK's `auth` block so\n * the Cursor agent can run its own OAuth flow. Returns undefined when there is\n * no `clientId` to share (e.g. RFC 7591 dynamic registration) — opencode's\n * access token itself never reaches `config.mcp`, so a bare URL would fail.\n */\nfunction toCursorAuth(\n\toauth:\n\t\t| { clientId?: string; clientSecret?: string; scope?: string }\n\t\t| undefined,\n):\n\t| { CLIENT_ID: string; CLIENT_SECRET?: string; scopes?: string[] }\n\t| undefined {\n\tif (!oauth?.clientId) return undefined;\n\tconst scopes = oauth.scope?.split(/\\s+/).filter(Boolean);\n\treturn {\n\t\tCLIENT_ID: oauth.clientId,\n\t\t...(oauth.clientSecret ? { CLIENT_SECRET: oauth.clientSecret } : {}),\n\t\t...(scopes && scopes.length > 0 ? { scopes } : {}),\n\t};\n}\n\n/**\n * Names of remote servers that require OAuth but cannot be forwarded to the\n * Cursor agent because no shareable client registration exists (dynamic\n * registration, or a `needs_auth` runtime status with no configured\n * `clientId`). The plugin surfaces these to the user instead of silently\n * forwarding a spec that would 401.\n */\nexport function findUnshareableOAuthServers(\n\tmcp: Config[\"mcp\"],\n\tstatus?: McpStatusMap,\n): string[] {\n\tconst names: string[] = [];\n\tif (!mcp) return names;\n\tfor (const [name, entry] of Object.entries(mcp) as Array<\n\t\t[string, OpencodeMcpEntry]\n\t>) {\n\t\tif (!entry || entry.type !== \"remote\") continue;\n\t\tif (!status && entry.enabled === false) continue;\n\t\tconst s = status?.[name]?.status;\n\t\tif (status && s !== \"connected\" && !NEEDS_AUTH_STATUS.has(s ?? \"\"))\n\t\t\tcontinue;\n\t\tconst oauth = oauthConfig(entry);\n\t\tconst needsOAuth = Boolean(oauth) || NEEDS_AUTH_STATUS.has(s ?? \"\");\n\t\tif (needsOAuth && !toCursorAuth(oauth)) names.push(name);\n\t}\n\treturn names;\n}\n\n/**\n * Translate opencode's configured MCP servers (`config.mcp`) into the Cursor\n * SDK's `McpServerConfig` shape so the same servers can be handed\n * to the Cursor agent via `Agent.create({ mcpServers })`.\n *\n * MCP servers are independent processes addressed by a launch spec, so opencode\n * and the Cursor agent can each connect to the same server. Disabled entries\n * (`enabled: false`) are skipped. The `timeout` field is dropped (no Cursor\n * equivalent). OAuth is mapped where possible: a remote server's `oauth` client\n * registration becomes Cursor's `auth` block so the agent runs its own OAuth\n * flow; servers needing OAuth with no shareable `clientId` are skipped (the\n * plugin reports them via {@link findUnshareableOAuthServers}).\n */\nexport function translateMcpServers(\n\tmcp: Config[\"mcp\"],\n\tstatus?: McpStatusMap,\n): Record<string, McpServerConfig> {\n\tconst out: Record<string, McpServerConfig> = {};\n\tif (!mcp) return out;\n\n\tfor (const [name, entry] of Object.entries(mcp) as Array<\n\t\t[string, OpencodeMcpEntry]\n\t>) {\n\t\tif (!entry) continue;\n\n\t\t// When a live status map is supplied (per-turn dynamic forwarding), it is\n\t\t// the source of truth: forward only servers opencode has currently\n\t\t// connected, so mid-session enable/disable propagates to the Cursor agent.\n\t\t// Without it (the startup config snapshot), fall back to the static\n\t\t// `enabled` flag.\n\t\tif (status) {\n\t\t\tif (status[name]?.status !== \"connected\") continue;\n\t\t} else if (entry.enabled === false) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"local\") {\n\t\t\tconst [command, ...args] = entry.command ?? [];\n\t\t\tif (!command) continue;\n\t\t\tout[name] = {\n\t\t\t\ttype: \"stdio\",\n\t\t\t\tcommand,\n\t\t\t\t...(args.length > 0 ? { args } : {}),\n\t\t\t\t...(entry.environment && Object.keys(entry.environment).length > 0\n\t\t\t\t\t? { env: entry.environment }\n\t\t\t\t\t: {}),\n\t\t\t};\n\t\t} else if (entry.type === \"remote\") {\n\t\t\tif (!entry.url) continue;\n\t\t\tconst oauth = oauthConfig(entry);\n\t\t\tconst auth = toCursorAuth(oauth);\n\t\t\t// OAuth server with no shareable client registration: opencode holds the\n\t\t\t// token and it never lands in config.mcp, so skip rather than forward a\n\t\t\t// bare URL that would 401. The plugin notifies the user (see\n\t\t\t// findUnshareableOAuthServers).\n\t\t\tif (oauth && !auth) continue;\n\t\t\tout[name] = {\n\t\t\t\ttype: \"http\",\n\t\t\t\turl: entry.url,\n\t\t\t\t...(entry.headers && Object.keys(entry.headers).length > 0\n\t\t\t\t\t? { headers: entry.headers }\n\t\t\t\t\t: {}),\n\t\t\t\t...(auth ? { auth } : {}),\n\t\t\t};\n\t\t}\n\t}\n\n\treturn out;\n}\n","import { tool, type ToolContext, type ToolDefinition } from \"@opencode-ai/plugin\";\nimport { runCloudAgent } from \"../provider/cloud-agent.js\";\nimport { runDelegate } from \"../provider/delegate.js\";\n\nconst s = tool.schema;\n\nexport interface CursorToolDeps {\n /**\n * Resolve the Cursor API key (from opencode auth, captured by the plugin's\n * auth loader, or the CURSOR_API_KEY env var). Returns undefined when no key\n * is available so the tool can return a clear \"needs auth\" message.\n */\n resolveApiKey: () => string | undefined;\n /** Default working directory for local delegation (the session worktree/cwd). */\n defaultCwd: () => string;\n}\n\nconst NEEDS_AUTH =\n \"No Cursor API key available. Run `opencode auth login` and choose Cursor, or set CURSOR_API_KEY.\";\n\n/**\n * Request approval for a sensitive Cursor invocation. `context.ask` is the\n * opencode mechanism a custom tool uses to gate itself; it honors the user's\n * `permission` config (allow resolves silently, ask prompts, deny rejects).\n *\n * Returns `{ ok: true }` when approved, or `{ ok: false, reason }` when the\n * request was rejected. We deliberately do not claim the rejection was a policy\n * \"deny\" — `context.ask` rejects on both an explicit deny and an internal\n * failure, and conflating them produces misleading messages. The gate is\n * fail-closed: any rejection (including a host that doesn't provide `ask`)\n * blocks the call rather than silently allowing it.\n */\nasync function requestApproval(\n context: ToolContext,\n permission: string,\n patterns: string[],\n metadata: Record<string, unknown>,\n): Promise<{ ok: boolean; reason?: string }> {\n try {\n await context.ask({ permission, patterns, always: patterns, metadata });\n return { ok: true };\n } catch (err) {\n return { ok: false, reason: err instanceof Error ? err.message : String(err) };\n }\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\n/**\n * Build the Cursor delegation tools that complement the native provider:\n * - `cursor_cloud_agent`: run a background agent on a remote repo (optionally\n * opening a PR) — work that maps poorly onto the synchronous provider path.\n * - `cursor_delegate`: run a single local Cursor turn as a permission-gated,\n * auditable tool call (for users who want Cursor as a delegate rather than\n * as their primary model).\n *\n * Both are gated via `context.ask`, so a user `permission` policy controls them.\n */\nexport function buildCursorTools(deps: CursorToolDeps): Record<string, ToolDefinition> {\n return {\n cursor_cloud_agent: tool({\n description:\n \"Launch a Cursor background ('cloud') agent on a remote repository. Runs autonomously \" +\n \"(may take minutes) and can open a pull request. Returns the cloud agent id, final \" +\n \"status, result, and PR url when available.\",\n args: {\n prompt: s.string().describe(\"The task/instruction for the background agent.\"),\n repoUrl: s\n .string()\n .describe(\"Target repository URL, e.g. https://github.com/owner/repo.\"),\n startingRef: s\n .string()\n .optional()\n .describe(\"Branch or ref to start from (defaults to the repo default branch).\"),\n model: s.string().optional().describe(\"Cursor model id (optional for cloud).\"),\n mode: s.enum([\"agent\", \"plan\"]).optional().describe(\"Conversation mode.\"),\n thinking: s.string().optional().describe(\"Thinking level, e.g. 'high'.\"),\n autoCreatePR: s\n .boolean()\n .optional()\n .describe(\"Open a pull request automatically when finished.\"),\n workOnCurrentBranch: s\n .boolean()\n .optional()\n .describe(\"Operate on the current branch instead of creating a new one.\"),\n },\n execute: async (args, context) => {\n const apiKey = deps.resolveApiKey();\n if (!apiKey) return NEEDS_AUTH;\n\n const approval = await requestApproval(\n context,\n \"cursor_cloud_agent\",\n [args.repoUrl],\n { repoUrl: args.repoUrl, autoCreatePR: args.autoCreatePR ?? false },\n );\n if (!approval.ok) {\n return `Cloud agent not approved for ${args.repoUrl}${approval.reason ? `: ${approval.reason}` : \".\"}`;\n }\n\n let result;\n try {\n result = await runCloudAgent({\n apiKey,\n prompt: args.prompt,\n repoUrl: args.repoUrl,\n ...(args.startingRef ? { startingRef: args.startingRef } : {}),\n ...(args.model ? { model: args.model } : {}),\n ...(args.mode ? { mode: args.mode } : {}),\n ...(args.thinking ? { thinking: args.thinking } : {}),\n ...(args.autoCreatePR !== undefined ? { autoCreatePR: args.autoCreatePR } : {}),\n ...(args.workOnCurrentBranch !== undefined\n ? { workOnCurrentBranch: args.workOnCurrentBranch }\n : {}),\n abortSignal: context.abort,\n });\n } catch (err) {\n return `Cloud agent failed: ${errorMessage(err)}`;\n }\n\n const lines = [\n `Cloud agent ${result.agentId} — ${result.status}`,\n ...(result.prUrl ? [`PR: ${result.prUrl}`] : []),\n ...(result.branches.length > 0\n ? [`Branches: ${result.branches.map((b) => b.branch ?? b.repoUrl).join(\", \")}`]\n : []),\n ...(result.result ? [\"\", result.result] : []),\n ...(result.progress.length > 0 ? [\"\", \"Progress:\", ...result.progress] : []),\n ];\n\n return {\n title: `Cursor cloud agent (${result.status})`,\n output: lines.join(\"\\n\"),\n metadata: {\n agentId: result.agentId,\n status: result.status,\n prUrl: result.prUrl ?? null,\n durationMs: result.durationMs ?? null,\n },\n };\n },\n }),\n\n cursor_delegate: tool({\n description:\n \"Delegate a single subtask to a local Cursor agent and return its result. Use to hand \" +\n \"off discrete work to Cursor while keeping your primary model in control. Permission-gated.\",\n args: {\n prompt: s.string().describe(\"The subtask to delegate to Cursor.\"),\n model: s.string().describe(\"Cursor model id to run the delegation on.\"),\n mode: s.enum([\"agent\", \"plan\"]).optional().describe(\"Conversation mode.\"),\n thinking: s.string().optional().describe(\"Thinking level, e.g. 'high'.\"),\n cwd: s\n .string()\n .optional()\n .describe(\"Working directory (defaults to the session directory).\"),\n sandbox: s.boolean().optional().describe(\"Run the agent's tools in Cursor's sandbox.\"),\n agentId: s\n .string()\n .optional()\n .describe(\"Resume a specific Cursor agent id instead of starting fresh.\"),\n },\n execute: async (args, context) => {\n const apiKey = deps.resolveApiKey();\n if (!apiKey) return NEEDS_AUTH;\n\n const approval = await requestApproval(context, \"cursor_delegate\", [args.model], {\n model: args.model,\n prompt: args.prompt,\n });\n if (!approval.ok) {\n return `Delegation to ${args.model} not approved${approval.reason ? `: ${approval.reason}` : \".\"}`;\n }\n\n let result;\n try {\n result = await runDelegate({\n apiKey,\n prompt: args.prompt,\n model: args.model,\n cwd: args.cwd ?? context.directory ?? deps.defaultCwd(),\n ...(args.mode ? { mode: args.mode } : {}),\n ...(args.thinking ? { thinking: args.thinking } : {}),\n ...(args.sandbox !== undefined ? { sandbox: args.sandbox } : {}),\n ...(args.agentId ? { agentId: args.agentId } : {}),\n abortSignal: context.abort,\n });\n } catch (err) {\n return `Delegation failed: ${errorMessage(err)}`;\n }\n\n const toolNote =\n result.toolActivity.length > 0\n ? `\\n\\n(${result.toolActivity.length} tool call(s)` +\n `${result.toolActivity.some((t) => t.isError) ? \", some failed\" : \"\"})`\n : \"\";\n\n return {\n title: `Cursor delegate (${args.model})`,\n output: (result.text || \"(no text output)\") + toolNote,\n metadata: {\n agentId: result.agentId,\n model: args.model,\n toolCalls: result.toolActivity.length,\n usage: result.usage ?? null,\n },\n };\n },\n }),\n };\n}\n","import type { AgentModeOption, ConversationStep, InteractionUpdate } from \"@cursor/sdk\";\nimport { loadCursorSdk } from \"../cursor-runtime.js\";\nimport { buildModelSelection } from \"./controls.js\";\n\n/**\n * A target repository for a cloud agent. Cursor's cloud runtime accepts an\n * array of repos; the tool surface exposes the common single-repo case.\n */\nexport interface CloudRepoTarget {\n url: string;\n startingRef?: string;\n}\n\nexport interface CloudAgentParams {\n apiKey: string;\n /** The instruction/task for the background agent. */\n prompt: string;\n /** Target repository URL (e.g. https://github.com/owner/repo). */\n repoUrl: string;\n /** Branch/ref to start from. Defaults to the repo's default branch. */\n startingRef?: string;\n /** Cursor model id. Optional for cloud (server picks a default otherwise). */\n model?: string;\n /** Conversation mode; defaults to \"agent\". */\n mode?: AgentModeOption;\n /** Convenience for the Cursor `thinking` model param (e.g. \"high\"). */\n thinking?: string;\n /** When true, open a PR automatically once the agent finishes. */\n autoCreatePR?: boolean;\n /** Operate on the current branch instead of creating a new one. */\n workOnCurrentBranch?: boolean;\n /** Cancels the run when aborted (wired to the tool's abort signal). */\n abortSignal?: AbortSignal;\n}\n\nexport interface CloudAgentBranch {\n repoUrl: string;\n branch?: string;\n prUrl?: string;\n}\n\nexport interface CloudAgentResult {\n agentId: string;\n /** Terminal run status: \"finished\" | \"error\" | \"cancelled\". */\n status: string;\n /** The agent's final textual result, when present. */\n result?: string;\n /** First PR url found across result branches (when `autoCreatePR`). */\n prUrl?: string;\n /** Per-repo branch/PR info reported by the run. */\n branches: CloudAgentBranch[];\n durationMs?: number;\n /** Human-readable progress lines captured from status/step/summary updates. */\n progress: string[];\n}\n\n/**\n * Run a Cursor background (\"cloud\") agent against a remote repository and wait\n * for it to finish, returning the final status, result text, and any PR url.\n *\n * A cloud agent can run for minutes and produce a PR rather than a chat reply,\n * which maps poorly onto the synchronous provider `doStream` path — so this is\n * exposed as an opencode tool instead (see plugin/index.ts). Progress is\n * collected into `progress[]` (opencode custom tools return a single result\n * rather than a live stream) and the lifecycle is bridged through the same\n * `loadCursorSdk` plumbing the provider uses.\n */\nexport async function runCloudAgent(params: CloudAgentParams): Promise<CloudAgentResult> {\n const { Agent } = await loadCursorSdk();\n const modelSelection = params.model\n ? buildModelSelection(params.model, params.thinking ? { thinking: params.thinking } : undefined)\n : undefined;\n const mode: AgentModeOption = params.mode ?? \"agent\";\n\n const createOptions = {\n apiKey: params.apiKey,\n ...(modelSelection ? { model: modelSelection } : {}),\n mode,\n cloud: {\n repos: [\n {\n url: params.repoUrl,\n ...(params.startingRef ? { startingRef: params.startingRef } : {}),\n },\n ],\n ...(params.autoCreatePR !== undefined ? { autoCreatePR: params.autoCreatePR } : {}),\n ...(params.workOnCurrentBranch !== undefined\n ? { workOnCurrentBranch: params.workOnCurrentBranch }\n : {}),\n },\n };\n\n const progress: string[] = [];\n const agent = await Agent.create(createOptions);\n\n // `onDelta` carries fine-grained updates; for a cloud (background) run the\n // higher-signal progress arrives via `onStep` (whole conversation steps) and\n // `run.onDidChangeStatus`. We capture all three — whichever the runtime emits.\n const onDelta = ({ update }: { update: InteractionUpdate }) => {\n if (update.type === \"summary\") progress.push(`summary: ${update.summary}`);\n };\n\n const onStep = ({ step }: { step: ConversationStep }) => {\n progress.push(`step: ${describeStep(step)}`);\n };\n\n try {\n const run = await agent.send(params.prompt, { mode, onDelta, onStep });\n\n const off = run.onDidChangeStatus?.((status: string) => {\n progress.push(`status: ${status}`);\n });\n const onAbort = () => {\n run.cancel().catch(() => {});\n };\n params.abortSignal?.addEventListener(\"abort\", onAbort);\n\n try {\n const result = await run.wait();\n const branches: CloudAgentBranch[] = (result.git?.branches ?? []).map((b) => ({\n repoUrl: b.repoUrl,\n ...(b.branch ? { branch: b.branch } : {}),\n ...(b.prUrl ? { prUrl: b.prUrl } : {}),\n }));\n const prUrl = branches.find((b) => b.prUrl)?.prUrl;\n return {\n agentId: agent.agentId,\n status: result.status,\n ...(result.result !== undefined ? { result: result.result } : {}),\n ...(prUrl ? { prUrl } : {}),\n branches,\n ...(result.durationMs !== undefined ? { durationMs: result.durationMs } : {}),\n progress,\n };\n } finally {\n off?.();\n params.abortSignal?.removeEventListener(\"abort\", onAbort);\n }\n } finally {\n try {\n agent.close();\n } catch {\n // best effort; cloud agents persist server-side regardless.\n }\n }\n}\n\n/** A short, log-friendly description of a conversation step for progress output. */\nfunction describeStep(step: ConversationStep): string {\n if (step.type === \"toolCall\") return `toolCall:${step.message.type}`;\n return step.type;\n}\n","import type { AgentModeOption } from \"@cursor/sdk\";\nimport type { CursorUsage } from \"./agent-events.js\";\nimport { streamAgentTurn } from \"./agent-events.js\";\nimport { resolveControls } from \"./controls.js\";\nimport { acquireAgent } from \"./session-pool.js\";\n\nexport interface DelegateParams {\n\tapiKey: string;\n\t/** The subtask to delegate to the Cursor agent. */\n\tprompt: string;\n\t/** Cursor model id to run the delegation on. */\n\tmodel: string;\n\t/** Conversation mode; defaults to \"agent\". */\n\tmode?: AgentModeOption;\n\t/** Convenience for the Cursor `thinking` model param (e.g. \"high\"). */\n\tthinking?: string;\n\t/** Working directory the local agent operates in. */\n\tcwd: string;\n\t/** Run the agent's tools inside Cursor's sandbox. */\n\tsandbox?: boolean;\n\t/** Resume a specific Cursor agent by id instead of creating a fresh one. */\n\tagentId?: string;\n\t/** Cancels the run when aborted (wired to the tool's abort signal). */\n\tabortSignal?: AbortSignal;\n}\n\nexport interface DelegateToolActivity {\n\tname: string;\n\tisError: boolean;\n}\n\nexport interface DelegateResult {\n\tagentId: string;\n\ttext: string;\n\treasoning: string;\n\ttoolActivity: DelegateToolActivity[];\n\tusage?: CursorUsage;\n}\n\n/**\n * Run a single delegated turn on a fresh (or explicitly resumed) local Cursor\n * agent and aggregate the outcome into a plain result. This backs the opt-in\n * `cursor_delegate` tool, which gives users a permission-gated boundary around\n * Cursor (the provider path runs Cursor's own loop without per-call gating).\n *\n * Reuses the provider's `acquireAgent` + `streamAgentTurn` plumbing; the turn\n * is consumed eagerly here because a tool returns a single result rather than a\n * live stream.\n */\nexport async function runDelegate(\n\tparams: DelegateParams,\n): Promise<DelegateResult> {\n\tconst { mode, modelSelection } = resolveControls(\n\t\tparams.model,\n\t\t{\n\t\t\tmode: params.mode ?? \"agent\",\n\t\t\t...(params.thinking ? { params: { thinking: params.thinking } } : {}),\n\t\t},\n\t\tundefined,\n\t);\n\n\tconst acquired = await acquireAgent({\n\t\tapiKey: params.apiKey,\n\t\tmodelSelection,\n\t\tmode,\n\t\tcwd: params.cwd,\n\t\t...(params.sandbox !== undefined ? { sandbox: params.sandbox } : {}),\n\t\t...(params.agentId ? { resumeAgentId: params.agentId } : {}),\n\t});\n\n\tconst text: string[] = [];\n\tconst reasoning: string[] = [];\n\tconst toolActivity: DelegateToolActivity[] = [];\n\tlet usage: CursorUsage | undefined;\n\n\ttry {\n\t\tfor await (const event of streamAgentTurn(\n\t\t\tacquired.agent,\n\t\t\t{ text: params.prompt },\n\t\t\t{\n\t\t\t\tmode,\n\t\t\t\t...(params.abortSignal ? { abortSignal: params.abortSignal } : {}),\n\t\t\t},\n\t\t)) {\n\t\t\tswitch (event.type) {\n\t\t\t\tcase \"text-delta\":\n\t\t\t\t\ttext.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"reasoning-delta\":\n\t\t\t\t\treasoning.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"tool-call\":\n\t\t\t\t\ttoolActivity.push({ name: event.name, isError: false });\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"tool-result\":\n\t\t\t\t\tif (event.isError)\n\t\t\t\t\t\ttoolActivity.push({ name: event.name, isError: true });\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"usage\":\n\t\t\t\t\tusage = event.usage;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"finish\":\n\t\t\t\t\t// The aggregated result text; prefer it when deltas were absent.\n\t\t\t\t\tif (event.text && text.length === 0) text.push(event.text);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t} finally {\n\t\tacquired.release();\n\t}\n\n\treturn {\n\t\tagentId: acquired.agent.agentId,\n\t\ttext: text.join(\"\"),\n\t\treasoning: reasoning.join(\"\"),\n\t\ttoolActivity,\n\t\t...(usage ? { usage } : {}),\n\t};\n}\n","import { createRequire } from \"node:module\";\nimport { homedir, tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { get } from \"node:https\";\nimport { mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport semver from \"semver\";\n\n/**\n * Inlined by tsup's `define` option in the published bundle (see\n * tsup.config.ts). In the bundle, a relative require of `../package.json`\n * would resolve inside `dist/` where no package.json exists, so the version\n * must be baked in at build time. When running un-bundled (tests against\n * `src/`), this stays undefined and `getLocalVersion` falls back to reading\n * package.json.\n */\ndeclare const __PKG_VERSION__: string | undefined;\n\nconst PACKAGE_NAME = \"@stablekernel/opencode-cursor\";\nconst REGISTRY_URL = `https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}/latest`;\nconst CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;\n// Failed fetches are retried sooner than successful ones so a transient\n// network error doesn't suppress the check for a full day.\nconst FAILURE_TTL_MS = 60 * 60 * 1000;\nconst REQUEST_TIMEOUT_MS = 5000;\n\ninterface VersionCheckCache {\n\tcheckedAt: number;\n\tlatest: string | undefined;\n}\n\nfunction cacheDir(): string {\n\tconst base =\n\t\tprocess.env.XDG_CACHE_HOME?.trim() ||\n\t\t(homedir() ? join(homedir(), \".cache\") : tmpdir());\n\treturn join(base, \"opencode-cursor\");\n}\n\nfunction cacheFile(): string {\n\treturn join(cacheDir(), \"version-check.json\");\n}\n\nfunction readCache(): VersionCheckCache | undefined {\n\ttry {\n\t\tconst parsed = JSON.parse(readFileSync(cacheFile(), \"utf8\")) as VersionCheckCache;\n\t\tif (typeof parsed.checkedAt === \"number\") return parsed;\n\t} catch {\n\t\t// ignore\n\t}\n\treturn undefined;\n}\n\nfunction writeCache(latest: string | undefined): void {\n\ttry {\n\t\tmkdirSync(cacheDir(), { recursive: true });\n\t\twriteFileSync(\n\t\t\tcacheFile(),\n\t\t\tJSON.stringify({ checkedAt: Date.now(), latest }),\n\t\t\t\"utf8\",\n\t\t);\n\t} catch {\n\t\t// Best-effort; never block plugin init.\n\t}\n}\n\nfunction getLocalVersion(): string | undefined {\n\t// Build-time inlined version (published bundle path).\n\tif (typeof __PKG_VERSION__ === \"string\") return __PKG_VERSION__;\n\t// Un-bundled fallback: resolve package.json relative to this source file.\n\ttry {\n\t\tconst require = createRequire(import.meta.url);\n\t\tconst pkg = require(\"../package.json\") as { version: string };\n\t\treturn pkg.version;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction fetchLatestVersion(): Promise<string | undefined> {\n\treturn new Promise((resolve) => {\n\t\tconst req = get(\n\t\t\tREGISTRY_URL,\n\t\t\t{ headers: { Accept: \"application/json\", Connection: \"close\" } },\n\t\t\t(res) => {\n\t\t\t\tif (res.statusCode !== 200) {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tresolve(undefined);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet body = \"\";\n\t\t\t\tres.setEncoding(\"utf8\");\n\t\t\t\tres.on(\"data\", (chunk: string) => {\n\t\t\t\t\tbody += chunk;\n\t\t\t\t});\n\t\t\t\tres.on(\"end\", () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst parsed = JSON.parse(body) as { version?: string };\n\t\t\t\t\t\tresolve(parsed.version);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\tresolve(undefined);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tres.on(\"error\", () => resolve(undefined));\n\t\t\t},\n\t\t);\n\t\treq.setTimeout(REQUEST_TIMEOUT_MS, () => {\n\t\t\treq.destroy();\n\t\t\tresolve(undefined);\n\t\t});\n\t\treq.on(\"error\", () => resolve(undefined));\n\t});\n}\n\n/** Return the cached latest version if fresh, else fetch from npm. */\nasync function getLatestVersion(): Promise<string | undefined> {\n\tconst cached = readCache();\n\tif (cached) {\n\t\t// Successful lookups are trusted for 24h; failures only briefly.\n\t\tconst ttl = cached.latest ? CHECK_INTERVAL_MS : FAILURE_TTL_MS;\n\t\tif (Date.now() - cached.checkedAt < ttl) return cached.latest;\n\t}\n\tconst latest = await fetchLatestVersion();\n\twriteCache(latest);\n\treturn latest;\n}\n\n/**\n * Print a warning when this installed plugin is older than the registry's\n * `latest` tag. opencode resolves `@latest` once and then never reinstalls\n * the plugin, so users can silently stay on old versions. This surfaces the\n * staleness with actionable instructions.\n *\n * The registry fetch is throttled to once per 24h via an on-disk cache;\n * while the cached result says the install is stale, the warning prints on\n * each startup until the user upgrades.\n *\n * Set CI or NO_UPDATE_NOTIFIER to skip the check entirely.\n */\nexport async function warnIfStale(): Promise<void> {\n\tif (process.env.CI || process.env.NO_UPDATE_NOTIFIER) return;\n\n\tconst local = getLocalVersion();\n\tif (!local || !semver.valid(local)) return;\n\tconst latest = await getLatestVersion();\n\tif (!latest || !semver.valid(latest)) return;\n\tif (!semver.gt(latest, local)) return;\n\n\tconst removeCommand = process.platform === \"win32\"\n\t\t? `rmdir /s /q \"%LocalAppData%\\\\opencode\\\\cache\\\\packages\\\\@stablekernel\\\\opencode-cursor@latest\"`\n\t\t: `rm -rf ~/.cache/opencode/packages/${PACKAGE_NAME}@latest`;\n\n\tconsole.warn(\n\t\t`\\n⚠️ @stablekernel/opencode-cursor update available: v${local} → v${latest}.\\n` +\n\t\t` opencode caches the @latest plugin on first install and never auto-updates it.\\n` +\n\t\t` To upgrade, exit opencode, run:\\n\\n` +\n\t\t` ${removeCommand}\\n\\n` +\n\t\t` then restart opencode.\\n`,\n\t);\n}\n","import type { Config, Plugin } from \"@opencode-ai/plugin\";\nimport type { Auth } from \"@opencode-ai/sdk/v2\";\nimport type { McpServerConfig } from \"@cursor/sdk\";\nimport { resolveCursorApiKey } from \"../api-key.js\";\nimport { discoverModels, toOpencodeModels } from \"../model-discovery.js\";\nimport { buildModelV2Map, PROVIDER_ID, providerNpm } from \"./model-v2.js\";\nimport {\n\tfindUnshareableOAuthServers,\n\ttype McpStatusMap,\n\ttranslateMcpServers,\n} from \"./mcp-config.js\";\nimport { buildCursorTools } from \"./cursor-tools.js\";\nimport { warnIfStale } from \"../version-check.js\";\nimport { removeSystemRule } from \"../provider/system-rule.js\";\n\nfunction apiKeyFromAuth(auth: Auth | undefined): string | undefined {\n\treturn auth?.type === \"api\" ? auth.key : undefined;\n}\n\n/**\n * opencode plugin that adds a \"Cursor\" provider backed by the official Cursor\n * SDK (`@cursor/sdk`).\n *\n * - `auth`: registers an API-key login for Cursor and a `loader` that feeds the\n * key into the AI-SDK provider factory. The key is validated on first use\n * (model discovery / first call), not at login — see the note on `methods`.\n * - `config`: registers the provider (npm package + discovered/fallback models)\n * so it shows up in opencode immediately.\n * - `provider.models`: auth-aware live model discovery via `Cursor.models.list`.\n * - `tool.cursor_refresh_models`: force-refresh the model catalog.\n */\nexport const CursorPlugin: Plugin = async (input) => {\n\t// Warn if the installed plugin is behind the npm `latest` tag. The registry\n\t// fetch is throttled to once per 24h via an on-disk cache, but while the\n\t// cached result says the install is stale the warning prints on each\n\t// startup. opencode freezes `@latest` plugin installs on first use, so this\n\t// keeps users from silently running stale releases. Fire-and-forget: never\n\t// block or fail plugin init.\n\tvoid warnIfStale().catch(() => {});\n\n\t// The Cursor API key resolved by opencode's auth loader, captured so the\n\t// delegation tools (which don't receive auth directly) can reuse it. Falls\n\t// back to the CURSOR_API_KEY env var when the loader hasn't run.\n\tlet capturedApiKey: string | undefined;\n\n\t// opencode client + MCP-forwarding settings captured at config time so the\n\t// per-turn chat.params hook can re-forward the *live* MCP server set\n\t// (reflecting mid-session enable/disable) rather than the startup snapshot.\n\tconst client = input?.client;\n\tconst directory = input?.directory;\n\t// Canonical working directory for the generated system-prompt rule: the\n\t// provider writes `.cursor/rules/opencode.mdc` under this path and dispose\n\t// cleans it up from the same path. The config hook threads it into the\n\t// provider options (respecting a user-configured `cwd` option) so write and\n\t// cleanup can never diverge.\n\tlet resolvedCwd = directory ?? process.cwd();\n\tlet forwardMcp = true;\n\tlet userMcp: Record<string, McpServerConfig> = {};\n\t// OAuth servers we've already warned about, so the toast fires once per\n\t// server rather than on every turn.\n\tconst warnedOAuth = new Set<string>();\n\n\treturn {\n\t\tauth: {\n\t\t\tprovider: PROVIDER_ID,\n\t\t\tloader: async (getAuth) => {\n\t\t\t\tconst apiKey = resolveCursorApiKey(\n\t\t\t\t\tapiKeyFromAuth(await getAuth().catch(() => undefined)),\n\t\t\t\t);\n\t\t\t\tif (apiKey) {\n\t\t\t\t\tcapturedApiKey = apiKey;\n\t\t\t\t\t// The `config` hook (which seeds opencode's model picker) runs without\n\t\t\t\t\t// a key. Warm the catalog cache here — the loader is the hook that\n\t\t\t\t\t// reliably has the key — so the next launch seeds the full live\n\t\t\t\t\t// catalog instead of the static fallback. Fire-and-forget: discovery\n\t\t\t\t\t// never throws and must not block auth/provider load.\n\t\t\t\t\tvoid discoverModels({ apiKey });\n\t\t\t\t}\n\t\t\t\treturn apiKey ? { apiKey } : {};\n\t\t\t},\n\t\t\t// A single API-key method. opencode always shows its built-in \"Enter your\n\t\t\t// API key\" prompt for `type: \"api\"`, so we intentionally do NOT declare\n\t\t\t// custom `prompts` (that asks for the key a second time) or an `authorize`\n\t\t\t// callback. opencode only passes `authorize` the *custom-prompt* inputs —\n\t\t\t// never the built-in key — so validating the key in `authorize` would\n\t\t\t// force that redundant extra prompt. Instead the key is validated on first\n\t\t\t// use (model discovery / the first call both surface a bad key clearly).\n\t\t\tmethods: [{ type: \"api\", label: \"Cursor API Key\" }],\n\t\t},\n\n\t\tconfig: async (config) => {\n\t\t\tconst { models } = await discoverModels({});\n\t\t\tconfig.provider ??= {};\n\t\t\tconst existing = config.provider[PROVIDER_ID] ?? {};\n\t\t\tconst existingOptions = (existing.options ?? {}) as Record<\n\t\t\t\tstring,\n\t\t\t\tunknown\n\t\t\t>;\n\n\t\t\t// Forward opencode's configured MCP servers to the Cursor\n\t\t\t// agent so it can use the same servers. Opt out via\n\t\t\t// `provider.cursor.options.forwardMcp: false`.\n\t\t\tforwardMcp = existingOptions[\"forwardMcp\"] !== false;\n\t\t\tuserMcp = (existingOptions[\"mcpServers\"] ?? {}) as Record<\n\t\t\t\tstring,\n\t\t\t\tMcpServerConfig\n\t\t\t>;\n\t\t\tconst mcpServers = forwardMcp\n\t\t\t\t? { ...userMcp, ...translateMcpServers(config.mcp) }\n\t\t\t\t: userMcp;\n\n\t\t\t// One canonical cwd for the provider's rule write and our dispose\n\t\t\t// cleanup: an explicit user option wins, else the plugin directory.\n\t\t\tconst optionCwd = existingOptions[\"cwd\"];\n\t\t\tresolvedCwd =\n\t\t\t\t(typeof optionCwd === \"string\" ? optionCwd : undefined) ??\n\t\t\t\tdirectory ??\n\t\t\t\tprocess.cwd();\n\n\t\t\tconfig.provider[PROVIDER_ID] = {\n\t\t\t\tname: \"Cursor\",\n\t\t\t\tnpm: providerNpm(),\n\t\t\t\t...existing,\n\t\t\t\toptions: {\n\t\t\t\t\t...existingOptions,\n\t\t\t\t\tcwd: resolvedCwd,\n\t\t\t\t\t...(Object.keys(mcpServers).length > 0 ? { mcpServers } : {}),\n\t\t\t\t},\n\t\t\t\tmodels: { ...toOpencodeModels(models), ...(existing.models ?? {}) },\n\t\t\t};\n\t\t},\n\n\t\tprovider: {\n\t\t\tid: PROVIDER_ID,\n\t\t\tmodels: async (_provider, ctx) => {\n\t\t\t\tconst apiKey = apiKeyFromAuth(ctx.auth);\n\t\t\t\tconst { models } = await discoverModels({ apiKey });\n\t\t\t\treturn buildModelV2Map(models);\n\t\t\t},\n\t\t},\n\n\t\t// Bridge opencode's session id to the provider: it lands in\n\t\t// providerOptions.cursor.sessionID, which the provider reads to pool/resume a\n\t\t// Cursor agent per session (when the `session` option is enabled).\n\t\t//\n\t\t// Also map opencode's plan AGENT to Cursor's plan mode. This hook fires\n\t\t// after opencode merges the selected variant into `output.options`, so an\n\t\t// explicit mode from the `plan` variant (or model options) wins — the\n\t\t// agent-based default only applies when no mode was set.\n\t\t\"chat.params\": async (input, output) => {\n\t\t\tif (input.model?.providerID !== PROVIDER_ID) return;\n\t\t\toutput.options = {\n\t\t\t\t...(output.options ?? {}),\n\t\t\t\tsessionID: input.sessionID,\n\t\t\t};\n\t\t\tif (input.agent === \"plan\" && output.options[\"mode\"] === undefined) {\n\t\t\t\toutput.options[\"mode\"] = \"plan\";\n\t\t\t}\n\n\t\t\t// Dynamically re-forward MCP servers from opencode's *live* state so\n\t\t\t// mid-session enable/disable reaches the Cursor agent (the config hook\n\t\t\t// only snapshots the set once, at startup). `client.mcp.status()` is the\n\t\t\t// runtime truth (connected/disabled/...) and `client.config.get()`\n\t\t\t// supplies the launch specs. On any failure we leave the static snapshot\n\t\t\t// (already baked into the provider options) in place.\n\t\t\tif (forwardMcp && client) {\n\t\t\t\ttry {\n\t\t\t\t\tconst query = directory ? { query: { directory } } : undefined;\n\t\t\t\t\tconst [cfgRes, statusRes] = await Promise.all([\n\t\t\t\t\t\tclient.config.get(),\n\t\t\t\t\t\tclient.mcp.status(query),\n\t\t\t\t\t]);\n\t\t\t\t\tconst liveMcp = (cfgRes?.data as Config | undefined)?.mcp;\n\t\t\t\t\tconst status = statusRes?.data as McpStatusMap | undefined;\n\t\t\t\t\tif (status) {\n\t\t\t\t\t\toutput.options[\"mcpServers\"] = {\n\t\t\t\t\t\t\t...userMcp,\n\t\t\t\t\t\t\t...translateMcpServers(liveMcp, status),\n\t\t\t\t\t\t};\n\t\t\t\t\t\t// Notify (once) about OAuth servers we can't forward: opencode\n\t\t\t\t\t\t// holds their token and it never reaches config.mcp, so the\n\t\t\t\t\t\t// Cursor agent can't connect. Only those without a shareable\n\t\t\t\t\t\t// client registration are skipped; ones with a clientId are\n\t\t\t\t\t\t// forwarded with an `auth` block for the agent's own OAuth flow.\n\t\t\t\t\t\tconst unshareable = findUnshareableOAuthServers(\n\t\t\t\t\t\t\tliveMcp,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t).filter((name) => !warnedOAuth.has(name));\n\t\t\t\t\t\tif (unshareable.length > 0) {\n\t\t\t\t\t\t\tfor (const name of unshareable) warnedOAuth.add(name);\n\t\t\t\t\t\t\tconst plural = unshareable.length > 1;\n\t\t\t\t\t\t\tvoid client.tui\n\t\t\t\t\t\t\t\t.showToast({\n\t\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\t\ttitle: \"Cursor MCP\",\n\t\t\t\t\t\t\t\t\t\tmessage: `Skipped OAuth MCP server${plural ? \"s\" : \"\"}: ${unshareable.join(\", \")}. opencode's token can't be shared with the Cursor agent; configure an OAuth clientId to forward ${plural ? \"them\" : \"it\"}.`,\n\t\t\t\t\t\t\t\t\t\tvariant: \"warning\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t.catch(() => {});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Keep the static snapshot; live forwarding is best-effort.\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\ttool: {\n\t\t\tcursor_refresh_models: {\n\t\t\t\tdescription:\n\t\t\t\t\t\"Refresh the live Cursor model catalog (bypasses the 24h cache) and report the available models.\",\n\t\t\t\targs: {},\n\t\t\t\texecute: async () => {\n\t\t\t\t\tconst result = await discoverModels({ forceRefresh: true });\n\t\t\t\t\tconst lines = result.models.map(\n\t\t\t\t\t\t(m) => `- ${m.id} — ${m.displayName}`,\n\t\t\t\t\t);\n\t\t\t\t\tconst header =\n\t\t\t\t\t\tresult.source === \"live\"\n\t\t\t\t\t\t\t? `Refreshed ${result.models.length} Cursor models (live):`\n\t\t\t\t\t\t\t: `Could not fetch live models (${result.source}). ${result.warning ?? \"\"}`.trim();\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttitle: `Cursor models (${result.source})`,\n\t\t\t\t\t\toutput: [header, ...lines].join(\"\\n\"),\n\t\t\t\t\t\tmetadata: { source: result.source, count: result.models.length },\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t},\n\t\t\t// Delegation tools that complement the provider: a cloud/background agent\n\t\t\t// and a permission-gated local delegate. They resolve the Cursor key from\n\t\t\t// the auth loader (captured above) or CURSOR_API_KEY.\n\t\t\t...buildCursorTools({\n\t\t\t\tresolveApiKey: () => resolveCursorApiKey(capturedApiKey),\n\t\t\t\tdefaultCwd: () => input?.directory ?? process.cwd(),\n\t\t\t}),\n\t\t},\n\n\t\tdispose: async () => {\n\t\t\t// Best-effort: drop the generated system-prompt rule so it doesn't\n\t\t\t// linger in the user's workspace / Cursor IDE after the session ends.\n\t\t\t// Uses the same canonical cwd the provider wrote to; sentinel-guarded,\n\t\t\t// so a user-owned opencode.mdc is never deleted.\n\t\t\tremoveSystemRule(resolvedCwd);\n\t\t},\n\t};\n};\n\nexport default CursorPlugin;\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,WAAW,cAAc,qBAAqB;AACvD,SAAS,SAAS,cAAc;AAChC,SAAS,YAAY;AAIrB,IAAM,iBAAiB,KAAK,KAAK,KAAK;AAEtC,SAAS,QAAgB;AACvB,QAAM,MAAM,QAAQ,IAAI;AACxB,QAAM,SAAS,MAAM,OAAO,SAAS,KAAK,EAAE,IAAI;AAChD,SAAO,OAAO,SAAS,MAAM,KAAK,SAAS,IAAI,SAAS;AAC1D;AAEA,SAAS,WAAmB;AAC1B,QAAM,OACJ,QAAQ,IAAI,gBAAgB,KAAK,MAChC,QAAQ,IAAI,KAAK,QAAQ,GAAG,QAAQ,IAAI,OAAO;AAClD,SAAO,KAAK,MAAM,iBAAiB;AACrC;AAEA,SAAS,UAAU,aAA6B;AAC9C,SAAO,KAAK,SAAS,GAAG,UAAU,WAAW,OAAO;AACtD;AAQA,SAAS,kBAA0B;AACjC,SAAO,KAAK,SAAS,GAAG,oBAAoB;AAC9C;AAIA,IAAM,gBAAgB,KAAK,KAAK,KAAK,KAAK;AAO1C,SAAS,cAAc,MAAc,UAA+C;AAClF,MAAI;AACF,UAAM,SAAS,KAAK,MAAM,aAAa,MAAM,MAAM,CAAC;AACpD,QAAI,CAAC,QAAQ,WAAW,CAAC,MAAM,QAAQ,OAAO,MAAM,EAAG,QAAO;AAC9D,QAAI,KAAK,IAAI,IAAI,OAAO,UAAU,SAAU,QAAO;AACnD,WAAO,OAAO;AAAA,EAChB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAe,MAAc,QAA+B;AACnE,MAAI;AACF,cAAU,SAAS,GAAG,EAAE,WAAW,KAAK,CAAC;AACzC,UAAM,WAA0B,EAAE,SAAS,KAAK,IAAI,GA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pdir","join","mkdirSync","readFileSync","writeFileSync","cacheDir","cacheFile","require","input"]}
@@ -18,6 +18,16 @@ import { AgentModeOption, McpServerConfig, SettingSource, AgentDefinition } from
18
18
  */
19
19
  type ToolDisplay = "reasoning" | "blocks";
20
20
 
21
+ /**
22
+ * How opencode's system prompt reaches the Cursor agent.
23
+ * - "rules" (default): delivered out-of-band as a Cursor project rule
24
+ * (see system-rule.ts) and therefore omitted from this flattened transcript.
25
+ * - "message": legacy — inlined as a `# System` block in the transcript.
26
+ * Injection-hardened Cursor models may reject this; kept for back-compat.
27
+ * - "omit": dropped entirely (no rule, no inline).
28
+ */
29
+ type SystemPromptMode = "rules" | "message" | "omit";
30
+
21
31
  interface CursorProviderOptions {
22
32
  /**
23
33
  * Cursor API key. opencode passes this from the provider's resolved auth /
@@ -66,6 +76,17 @@ interface CursorProviderOptions {
66
76
  * hosts (works everywhere).
67
77
  */
68
78
  toolDisplay?: ToolDisplay;
79
+ /**
80
+ * How opencode's system prompt reaches the Cursor agent:
81
+ * - "rules" (default): written to `<cwd>/.cursor/rules/opencode.mdc`
82
+ * (git-ignored, alwaysApply) and loaded via the `project` settings layer —
83
+ * Cursor's authoritative channel, so it is not rejected as prompt injection.
84
+ * Note: a project rule also applies to your own Cursor IDE in this repo, and
85
+ * enabling the project layer also loads other `.cursor/` config.
86
+ * - "message": legacy inline `# System` block (may be flagged as injection).
87
+ * - "omit": not forwarded at all.
88
+ */
89
+ systemPrompt?: SystemPromptMode;
69
90
  }
70
91
  /**
71
92
  * Cursor provider for the Vercel AI SDK (V3), backed by the official