Skip to content

Commit bf4f48e

Browse files
committed
fix(opencode2): pin the aisdk package to the plugin's own version
The OpenCode 2 inventory registered `package: "aisdk:cursor-opencode-provider"`. OpenCode resolves a bare npm name as `@latest`, and on 2.0.22 the language model is built from that spec: the built-in `opencode.provider.dynamic` plugin is registered before external plugins and its `sdk` hook installs and imports the package itself (anomalyco/opencode#42788), so this plugin's own `sdk` hook finds `event.sdk` already set. A user who pinned `[email protected]` in `plugins` therefore ran the last published language model (`<cache>/npm/cursor-opencode-provider@latest/`), and that copy changed whenever OpenCode refreshed its package cache. Register `aisdk:cursor-opencode-provider@<version>`, read from this package's own package.json (two levels above both src/opencode2 and dist/opencode2), and fall back to the bare name when that file is missing or describes another package. `CURSOR_OPENCODE2_DEV_ENTRY` still wins. Same fix as stablekernel/opencode-cursor#129.
1 parent f65ffc7 commit bf4f48e

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- OpenCode 2.0 loads the AI SDK provider at the installed plugin's version instead of the latest npm release ([#41](https://github.com/oakimov/cursor-opencode-provider/pull/41) by [@nkoynov](https://github.com/nkoynov))
8+
59
## [0.8.0] - 2026-10-04
610

711
### Changed

‎docs/opencode-2.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Synced models set `time.released` to `0` (Cursor AvailableModels does not expose
2626

2727
Pin a version if you want: `"[email protected]/plugin/opencode2"`.
2828

29-
OpenCode 2.0 installs the published package into its host cache and loads the AI SDK entry from there (`aisdk:cursor-opencode-provider`). No extra env vars are required.
29+
OpenCode 2.0 installs the published package into its host cache and loads the AI SDK entry from there (`aisdk:cursor-opencode-provider@<version>`, pinned to the version of the plugin that registered it, so a pinned plugin never runs a newer language model). No extra env vars are required.
3030

3131
## Config isolation
3232

‎src/opencode2/catalog.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from "node:fs"
12
import { pathToFileURL } from "node:url"
23
import { CURSOR_PROVIDER_ID } from "../shared.js"
34
import { CURSOR_WIRE_MODEL_ID_KEY, type ModelInfo } from "../models.js"
@@ -17,14 +18,18 @@ import type { ConnectionInfo, ModelVariantInfo, ProviderEditor } from "./types.j
1718
/** Integration id owning Cursor credentials. Matches the provider id. */
1819
export const CURSOR_INTEGRATION_ID = CURSOR_PROVIDER_ID
1920

21+
const CURSOR_PACKAGE_NAME = "cursor-opencode-provider"
22+
2023
/**
2124
* `aisdk:` selects OpenCode 2.0's AI SDK path, which is what surfaces the
2225
* `aisdk.hook("sdk")` / `("language")` extension points we supply the provider
23-
* through. The suffix is this package's npm name so the host's built-in
24-
* fallback can still resolve it if our own hook is ever bypassed — that
25-
* fallback runs `npm.add(pkg)` against the *published* registry into
26-
* `<host-cache>/packages/<pkg>/node_modules/<pkg>`, ignoring any local
27-
* `file://` plugin path this process was loaded from.
26+
* through. The suffix is this package's npm name for the host's built-in
27+
* fallback, which runs `npm.add(pkg)` against the *published* registry,
28+
* ignoring any local `file://` plugin path this process was loaded from. On
29+
* OpenCode 2.0.22 that fallback runs before plugin `sdk` hooks
30+
* (anomalyco/opencode#42788), so the language model always comes from this
31+
* spec. It is pinned to this package's own version: OpenCode resolves a bare
32+
* name as `@latest`, which would load whatever was published last.
2833
*
2934
* `CURSOR_OPENCODE2_DEV_ENTRY` overrides the suffix with an `aisdk:file://…`
3035
* spec instead, pointed at a local built entry file (e.g. `dist/index.js`,
@@ -35,7 +40,30 @@ export const CURSOR_INTEGRATION_ID = CURSOR_PROVIDER_ID
3540
*/
3641
export const CURSOR_AISDK_PACKAGE = process.env.CURSOR_OPENCODE2_DEV_ENTRY
3742
? `aisdk:${pathToFileURL(process.env.CURSOR_OPENCODE2_DEV_ENTRY).href}`
38-
: "aisdk:cursor-opencode-provider"
43+
: `aisdk:${ownPackageSpec()}`
44+
45+
/**
46+
* `cursor-opencode-provider@<version>` from this package's own package.json
47+
* (two levels up from both `src/opencode2/` and `dist/opencode2/`), or the
48+
* bare name when that file is missing or does not describe this package.
49+
*/
50+
export function ownPackageSpec(
51+
packageJsonUrl: URL = new URL("../../package.json", import.meta.url),
52+
): string {
53+
try {
54+
const pkg = JSON.parse(readFileSync(packageJsonUrl, "utf8")) as { name?: unknown; version?: unknown }
55+
if (
56+
pkg.name === CURSOR_PACKAGE_NAME
57+
&& typeof pkg.version === "string"
58+
&& /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(pkg.version)
59+
) {
60+
return `${CURSOR_PACKAGE_NAME}@${pkg.version}`
61+
}
62+
} catch {
63+
// Bundled or relocated copies have no package.json beside them.
64+
}
65+
return CURSOR_PACKAGE_NAME
66+
}
3967

4068
/**
4169
* Plain-object `Model.Info` equivalent used by `ctx.provider.transform`.

‎test/plugin-opencode2.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os"
44
import { join, resolve } from "node:path"
55
import plugin from "../src/plugin-opencode2.js"
66
import { CursorPlugin } from "../src/plugin.js"
7-
import { applyCursorProviderInventory, CURSOR_AISDK_PACKAGE } from "../src/opencode2/catalog.js"
7+
import { applyCursorProviderInventory, CURSOR_AISDK_PACKAGE, ownPackageSpec } from "../src/opencode2/catalog.js"
88
import {
99
accessTokenFromCredential,
1010
applyCursorIntegration,
@@ -159,6 +159,29 @@ describe("opencode2 provider inventory", () => {
159159
expect(provider!.activation).toBe("enabled")
160160
})
161161

162+
test("pins the aisdk package to this package's own version", () => {
163+
// A bare name is resolved as `@latest`, so the host would load a newer
164+
// language model than the plugin the user pinned.
165+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"))
166+
if (!process.env.CURSOR_OPENCODE2_DEV_ENTRY) {
167+
expect(CURSOR_AISDK_PACKAGE).toBe(`aisdk:cursor-opencode-provider@${pkg.version}`)
168+
}
169+
expect(ownPackageSpec()).toBe(`cursor-opencode-provider@${pkg.version}`)
170+
})
171+
172+
test("falls back to the bare package name without a matching package.json", () => {
173+
const dir = mkdtempSync(join(tmpdir(), "cursor-pin-"))
174+
try {
175+
expect(ownPackageSpec(new URL(`file://${dir}/missing.json`))).toBe("cursor-opencode-provider")
176+
writeFileSync(join(dir, "other.json"), JSON.stringify({ name: "some-bundle", version: "1.2.3" }))
177+
expect(ownPackageSpec(new URL(`file://${dir}/other.json`))).toBe("cursor-opencode-provider")
178+
writeFileSync(join(dir, "range.json"), JSON.stringify({ name: "cursor-opencode-provider", version: "^0.8.0" }))
179+
expect(ownPackageSpec(new URL(`file://${dir}/range.json`))).toBe("cursor-opencode-provider")
180+
} finally {
181+
rmSync(dir, { recursive: true, force: true })
182+
}
183+
})
184+
162185
test("maps a model into the 2.0 shape", () => {
163186
const { editor, models } = fakeProviderEditor()
164187
applyCursorProviderInventory(editor, [baseModel])

0 commit comments

Comments
 (0)