opencode-rag-plugin 2.3.0 → 2.3.1
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.
- package/ReadMe.md +2 -0
- package/dist/cli/commands/init-helpers.d.ts +4 -1
- package/dist/cli/commands/init-helpers.js +55 -20
- package/dist/cli/commands/init.js +13 -1
- package/dist/cli/commands/setup.js +43 -12
- package/dist/cli/commands/update.d.ts +3 -2
- package/dist/cli/commands/update.js +7 -3
- package/dist/core/config.d.ts +7 -2
- package/dist/core/config.js +2 -2
- package/dist/core/package-manager.d.ts +69 -0
- package/dist/core/package-manager.js +250 -0
- package/dist/core/setup-runtime.d.ts +7 -4
- package/dist/core/setup-runtime.js +33 -31
- package/dist/core/version-check.d.ts +19 -11
- package/dist/core/version-check.js +30 -11
- package/package.json +1 -1
package/ReadMe.md
CHANGED
|
@@ -34,6 +34,8 @@ workspace, and `opencode-rag update` upgrades to a newer release.
|
|
|
34
34
|
|
|
35
35
|
**Prerequisites:** Node.js v22+, [Ollama](https://ollama.ai) (default) or other LLM-hosters (OpenAI-, Google- or Anthropic-compatible).
|
|
36
36
|
|
|
37
|
+
`pnpm add -g opencode-rag-plugin`, `yarn global add opencode-rag-plugin`, and `bun add -g opencode-rag-plugin` work as drop-in replacements for the global install — setup and update detect the package manager that owns the install and never require npm on `PATH`.
|
|
38
|
+
|
|
37
39
|
> **Contributors / developers:** Clone the repo and use `npm install --legacy-peer-deps; npm run build` — see [Development docs](doc/development.md).
|
|
38
40
|
|
|
39
41
|
## Key Features
|
|
@@ -123,6 +123,9 @@ export declare function installPluginFromGlobal(opencodeDir: string, packageName
|
|
|
123
123
|
*
|
|
124
124
|
* @param tuning - Optional embedding batch tuning (auto-detected from the
|
|
125
125
|
* Ollama backend). Falls back to `DEFAULT_CONFIG` for any omitted field.
|
|
126
|
+
* @param existingConfigRaw - Optional raw text of the config file that is about
|
|
127
|
+
* to be replaced. Its `apiKey` and `vectorDimension` are carried over: a
|
|
128
|
+
* reset must not silently change the store schema or drop provider auth.
|
|
126
129
|
* @returns A pretty-printed JSON string with all default configuration values.
|
|
127
130
|
*/
|
|
128
|
-
export declare function generateDefaultConfigJson(tuning?: Partial<IndexingTuning
|
|
131
|
+
export declare function generateDefaultConfigJson(tuning?: Partial<IndexingTuning>, existingConfigRaw?: string): string;
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
import os from "node:os";
|
|
10
10
|
import { existsSync, mkdirSync, rmSync, symlinkSync, } from "node:fs";
|
|
11
|
-
import { execSync } from "node:child_process";
|
|
12
11
|
import { DEFAULT_CONFIG } from "../../core/config.js";
|
|
12
|
+
import { installHint, resolveGlobalInstall } from "../../core/package-manager.js";
|
|
13
13
|
import { c } from "../format.js";
|
|
14
14
|
import { BEGIN_MARKER, END_MARKER, buildAgentsMdDirective } from "../../opencode/system-guidance.js";
|
|
15
15
|
import { getStringRecord, readJsonObject, writeJsonFile, } from "../helpers.js";
|
|
@@ -359,28 +359,22 @@ export async function installPluginFromGlobal(opencodeDir, packageName, skipInst
|
|
|
359
359
|
const globalPluginDir = path.join(runtimeDir, "node_modules", packageName);
|
|
360
360
|
const workspaceTarget = path.join(opencodeDir, "node_modules", packageName);
|
|
361
361
|
if (!existsSync(globalPluginDir)) {
|
|
362
|
-
// Fallback:
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
throw new Error(`Global plugin cache not found at ${globalPluginDir}. ` +
|
|
369
|
-
"Run 'opencode-rag setup' or install globally with 'npm install -g opencode-rag-plugin' first.");
|
|
370
|
-
}
|
|
371
|
-
const globalNpmPluginDir = path.join(globalRoot, packageName);
|
|
372
|
-
if (existsSync(globalNpmPluginDir) && existsSync(path.join(globalNpmPluginDir, "dist", "cli.js"))) {
|
|
373
|
-
console.log(` ${c.created("Found:")} ${packageName} in global npm prefix, linking...`);
|
|
362
|
+
// Fallback: locate a global install via any package manager (issue #34:
|
|
363
|
+
// npm may be absent — pnpm/yarn/bun installs must resolve too).
|
|
364
|
+
const install = resolveGlobalInstall(packageName);
|
|
365
|
+
if (install && existsSync(path.join(install.packageDir, "dist", "cli.js"))) {
|
|
366
|
+
const pmLabel = install.pm ? `global ${install.pm} install` : "global install";
|
|
367
|
+
console.log(` ${c.created("Found:")} ${packageName} in ${pmLabel}, linking...`);
|
|
374
368
|
if (existsSync(workspaceTarget)) {
|
|
375
369
|
rmSync(workspaceTarget, { recursive: true, force: true });
|
|
376
370
|
}
|
|
377
371
|
mkdirSync(path.dirname(workspaceTarget), { recursive: true });
|
|
378
|
-
createJunction(
|
|
372
|
+
createJunction(install.packageDir, workspaceTarget);
|
|
379
373
|
const cliEntry = path.join(workspaceTarget, "dist", "cli.js");
|
|
380
374
|
if (existsSync(cliEntry)) {
|
|
381
|
-
console.log(` ${c.success("Linked:")} ${packageName} from global
|
|
382
|
-
// Also link @opencode-ai/plugin from global
|
|
383
|
-
const globalSdkDir = path.join(globalRoot, "@opencode-ai", "plugin");
|
|
375
|
+
console.log(` ${c.success("Linked:")} ${packageName} from the global install`);
|
|
376
|
+
// Also link @opencode-ai/plugin from the same global node_modules
|
|
377
|
+
const globalSdkDir = path.join(install.globalRoot, "@opencode-ai", "plugin");
|
|
384
378
|
const workspaceSdkDir = path.join(opencodeDir, "node_modules", "@opencode-ai", "plugin");
|
|
385
379
|
if (existsSync(globalSdkDir) && !existsSync(workspaceSdkDir)) {
|
|
386
380
|
mkdirSync(path.dirname(workspaceSdkDir), { recursive: true });
|
|
@@ -392,14 +386,14 @@ export async function installPluginFromGlobal(opencodeDir, packageName, skipInst
|
|
|
392
386
|
cpSync(globalSdkDir, workspaceSdkDir, { recursive: true });
|
|
393
387
|
}
|
|
394
388
|
}
|
|
395
|
-
console.log(` ${c.success("Linked:")} @opencode-ai/plugin from global
|
|
389
|
+
console.log(` ${c.success("Linked:")} @opencode-ai/plugin from the global install`);
|
|
396
390
|
return;
|
|
397
391
|
}
|
|
398
392
|
// Junction failed or incomplete — fall through to error
|
|
399
393
|
rmSync(workspaceTarget, { recursive: true, force: true });
|
|
400
394
|
}
|
|
401
395
|
throw new Error(`Global plugin cache not found at ${globalPluginDir}. ` +
|
|
402
|
-
|
|
396
|
+
`Run 'opencode-rag setup' or install globally first (${installHint(packageName)}).`);
|
|
403
397
|
}
|
|
404
398
|
// Remove any stale copy (e.g. from a previous 'npm install' run)
|
|
405
399
|
if (existsSync(workspaceTarget)) {
|
|
@@ -437,20 +431,61 @@ export async function installPluginFromGlobal(opencodeDir, packageName, skipInst
|
|
|
437
431
|
"Run 'install.sh compile' / 'install.ps1 compile' to install it.");
|
|
438
432
|
}
|
|
439
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* `embedding` keys that the default template does not model but that MUST
|
|
436
|
+
* survive a rewrite. Losing `vectorDimension` makes the next run fall back to
|
|
437
|
+
* the 384 default (and corrupts a rebuild against a store whose schema is
|
|
438
|
+
* 4096), and `apiKey` is provider auth the template never carries — both are
|
|
439
|
+
* invisible data loss when a user confirms "Overwrite with default config?".
|
|
440
|
+
*/
|
|
441
|
+
const PRESERVED_EMBEDDING_KEYS = ["apiKey", "vectorDimension"];
|
|
442
|
+
/**
|
|
443
|
+
* Pick the identity-critical `embedding` keys out of an existing config file so
|
|
444
|
+
* the generated defaults can re-apply them. Malformed input simply yields an
|
|
445
|
+
* empty set (the pure default template).
|
|
446
|
+
*/
|
|
447
|
+
function preservedEmbeddingKeys(existingConfigRaw) {
|
|
448
|
+
if (!existingConfigRaw)
|
|
449
|
+
return {};
|
|
450
|
+
try {
|
|
451
|
+
const raw = existingConfigRaw.charCodeAt(0) === 0xfeff
|
|
452
|
+
? existingConfigRaw.slice(1)
|
|
453
|
+
: existingConfigRaw;
|
|
454
|
+
const parsed = JSON.parse(raw);
|
|
455
|
+
const embedding = parsed?.embedding;
|
|
456
|
+
if (!embedding || typeof embedding !== "object")
|
|
457
|
+
return {};
|
|
458
|
+
const preserved = {};
|
|
459
|
+
for (const key of PRESERVED_EMBEDDING_KEYS) {
|
|
460
|
+
const value = embedding[key];
|
|
461
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
462
|
+
preserved[key] = value;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return preserved;
|
|
466
|
+
}
|
|
467
|
+
catch {
|
|
468
|
+
return {};
|
|
469
|
+
}
|
|
470
|
+
}
|
|
440
471
|
/**
|
|
441
472
|
* Generate the default `opencode-rag.json` configuration content.
|
|
442
473
|
*
|
|
443
474
|
* @param tuning - Optional embedding batch tuning (auto-detected from the
|
|
444
475
|
* Ollama backend). Falls back to `DEFAULT_CONFIG` for any omitted field.
|
|
476
|
+
* @param existingConfigRaw - Optional raw text of the config file that is about
|
|
477
|
+
* to be replaced. Its `apiKey` and `vectorDimension` are carried over: a
|
|
478
|
+
* reset must not silently change the store schema or drop provider auth.
|
|
445
479
|
* @returns A pretty-printed JSON string with all default configuration values.
|
|
446
480
|
*/
|
|
447
|
-
export function generateDefaultConfigJson(tuning) {
|
|
481
|
+
export function generateDefaultConfigJson(tuning, existingConfigRaw) {
|
|
448
482
|
return JSON.stringify({
|
|
449
483
|
embedding: {
|
|
450
484
|
provider: DEFAULT_CONFIG.embedding.provider,
|
|
451
485
|
baseUrl: DEFAULT_CONFIG.embedding.baseUrl,
|
|
452
486
|
model: DEFAULT_CONFIG.embedding.model,
|
|
453
487
|
timeoutMs: DEFAULT_CONFIG.embedding.timeoutMs,
|
|
488
|
+
...preservedEmbeddingKeys(existingConfigRaw),
|
|
454
489
|
},
|
|
455
490
|
indexing: {
|
|
456
491
|
includeExtensions: DEFAULT_CONFIG.indexing.includeExtensions,
|
|
@@ -226,7 +226,19 @@ export async function runWorkspaceInit(options = {}) {
|
|
|
226
226
|
detectedTuning = undefined;
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
|
-
|
|
229
|
+
// Carry the identity-critical embedding keys (apiKey, vectorDimension)
|
|
230
|
+
// over from the file being replaced: a reset must never downgrade the
|
|
231
|
+
// store schema to the 384 default or drop provider auth.
|
|
232
|
+
let existingRaw;
|
|
233
|
+
if (configExists) {
|
|
234
|
+
try {
|
|
235
|
+
existingRaw = readFileSync(configPath, "utf-8");
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
existingRaw = undefined;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return generateDefaultConfigJson(detectedTuning, existingRaw);
|
|
230
242
|
};
|
|
231
243
|
if (!configExists) {
|
|
232
244
|
writeFileSync(configPath, await configContent(), "utf-8");
|
|
@@ -5,6 +5,7 @@ import { c } from "../format.js";
|
|
|
5
5
|
import { getPackageMetadata } from "../helpers.js";
|
|
6
6
|
import { getRuntimeDir, getVersionFile, readVersionFile, setupRuntime, } from "../../core/setup-runtime.js";
|
|
7
7
|
import { compareVersions, getCurrentVersion, installLatestUpdate } from "../../core/version-check.js";
|
|
8
|
+
import { bestAvailablePackageManager, globalUninstallCommand } from "../../core/package-manager.js";
|
|
8
9
|
import { isLikelyProjectRoot, isWorkspaceInitialized, runWorkspaceInit } from "./init.js";
|
|
9
10
|
const PLUGIN_NAME = "opencode-rag-plugin";
|
|
10
11
|
function removeIfExists(targetPath) {
|
|
@@ -98,7 +99,8 @@ export function registerSetupCommand(program) {
|
|
|
98
99
|
removeIfExists(versionFile);
|
|
99
100
|
console.log(` ${c.updated("Removed:")} ${c.file(runtimePluginDir)}`);
|
|
100
101
|
console.log(` ${c.updated("Removed:")} ${c.file(runtimeSdkPluginDir)}`);
|
|
101
|
-
|
|
102
|
+
const uninstallPm = bestAvailablePackageManager() ?? "npm";
|
|
103
|
+
console.log(`\n ${c.success("Done.")} Run ${c.file(globalUninstallCommand(uninstallPm, PLUGIN_NAME))} to remove the global package.\n`);
|
|
102
104
|
return;
|
|
103
105
|
}
|
|
104
106
|
// --- install ---
|
|
@@ -161,9 +163,9 @@ export function registerSetupCommand(program) {
|
|
|
161
163
|
*/
|
|
162
164
|
async function ensureUpToDate() {
|
|
163
165
|
const current = getCurrentVersion();
|
|
164
|
-
const latest = getLatestNpmVersion();
|
|
166
|
+
const latest = await getLatestNpmVersion();
|
|
165
167
|
if (!latest) {
|
|
166
|
-
console.log(` ${c.dim("Could not check npm for the latest version (offline?).")}`);
|
|
168
|
+
console.log(` ${c.dim("Could not check the npm registry for the latest version (offline?).")}`);
|
|
167
169
|
console.log(` ${c.dim("Run `opencode-rag update` later to upgrade.\n")}`);
|
|
168
170
|
return;
|
|
169
171
|
}
|
|
@@ -184,20 +186,49 @@ async function ensureUpToDate() {
|
|
|
184
186
|
/**
|
|
185
187
|
* Query the npm registry for the latest published version of the plugin.
|
|
186
188
|
*
|
|
187
|
-
*
|
|
189
|
+
* Fetches the registry over HTTPS directly so the check works no matter which
|
|
190
|
+
* package manager installed this CLI — the npm binary may not even exist on
|
|
191
|
+
* machines set up via pnpm/yarn/bun (GitHub issue #34). Honors
|
|
192
|
+
* `npm_config_registry` (set by all four managers) for private mirrors.
|
|
193
|
+
*
|
|
194
|
+
* Best-effort: returns `null` when every endpoint lookup fails (offline,
|
|
195
|
+
* mirror without the endpoint, malformed response).
|
|
188
196
|
*
|
|
189
197
|
* @returns The latest version string, or `null` on failure.
|
|
190
198
|
*/
|
|
191
|
-
function getLatestNpmVersion() {
|
|
199
|
+
async function getLatestNpmVersion() {
|
|
200
|
+
const registry = (process.env.npm_config_registry || "https://registry.npmjs.org").replace(/\/+$/, "");
|
|
201
|
+
const pkgPath = PLUGIN_NAME.includes("/") ? PLUGIN_NAME.replace("/", "%2F") : PLUGIN_NAME;
|
|
202
|
+
// Prefer the lightweight dist-tags endpoint; fall back to the version
|
|
203
|
+
// document for mirrors that do not implement it.
|
|
204
|
+
const urls = [
|
|
205
|
+
`${registry}/-/package/${pkgPath}/dist-tags`,
|
|
206
|
+
`${registry}/${pkgPath}/latest`,
|
|
207
|
+
];
|
|
208
|
+
const controller = new AbortController();
|
|
209
|
+
const timeout = setTimeout(() => controller.abort(), 15_000);
|
|
192
210
|
try {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
211
|
+
for (const url of urls) {
|
|
212
|
+
try {
|
|
213
|
+
const response = await fetch(url, {
|
|
214
|
+
headers: { Accept: "application/json" },
|
|
215
|
+
signal: controller.signal,
|
|
216
|
+
});
|
|
217
|
+
if (!response.ok)
|
|
218
|
+
continue;
|
|
219
|
+
const data = (await response.json());
|
|
220
|
+
const version = data.latest ?? data.version;
|
|
221
|
+
if (typeof version === "string" && version)
|
|
222
|
+
return version;
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
// Try the next endpoint.
|
|
226
|
+
}
|
|
227
|
+
}
|
|
200
228
|
return null;
|
|
201
229
|
}
|
|
230
|
+
finally {
|
|
231
|
+
clearTimeout(timeout);
|
|
232
|
+
}
|
|
202
233
|
}
|
|
203
234
|
//# sourceMappingURL=setup.js.map
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
5
|
* `update` command — checks for a newer published version of OpenCodeRAG and,
|
|
6
|
-
* by default, installs it
|
|
7
|
-
*
|
|
6
|
+
* by default, installs it via the package manager that owns the global install
|
|
7
|
+
* (npm/pnpm/yarn/bun, `<pkg>@latest`) then re-syncs the OpenCode runtime
|
|
8
|
+
* junctions so the new build is picked up on next restart.
|
|
8
9
|
*/
|
|
9
10
|
import type { Command } from "commander";
|
|
10
11
|
/**
|
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
5
|
* `update` command — checks for a newer published version of OpenCodeRAG and,
|
|
6
|
-
* by default, installs it
|
|
7
|
-
*
|
|
6
|
+
* by default, installs it via the package manager that owns the global install
|
|
7
|
+
* (npm/pnpm/yarn/bun, `<pkg>@latest`) then re-syncs the OpenCode runtime
|
|
8
|
+
* junctions so the new build is picked up on next restart.
|
|
8
9
|
*/
|
|
9
10
|
import { c } from "../format.js";
|
|
10
11
|
import { checkForUpdate, getCurrentVersion, installLatestUpdate } from "../../core/version-check.js";
|
|
12
|
+
import { bestAvailablePackageManager, globalInstallCommand } from "../../core/package-manager.js";
|
|
13
|
+
const PLUGIN_NAME = "opencode-rag-plugin";
|
|
11
14
|
/**
|
|
12
15
|
* Register the `update` command on the given Commander program.
|
|
13
16
|
*
|
|
@@ -56,7 +59,8 @@ export function registerUpdateCommand(program) {
|
|
|
56
59
|
else {
|
|
57
60
|
console.error(` ${c.error("✗")} ${result.message}`);
|
|
58
61
|
console.error(`\n ${c.error("Update failed. You can retry with `opencode-rag update` or install manually:")}\n`);
|
|
59
|
-
|
|
62
|
+
const hintPm = bestAvailablePackageManager() ?? "npm";
|
|
63
|
+
console.error(` ${c.dim(` ${globalInstallCommand(hintPm, `${PLUGIN_NAME}@latest`)} && opencode-rag setup`)}\n`);
|
|
60
64
|
process.exit(1);
|
|
61
65
|
}
|
|
62
66
|
});
|
package/dist/core/config.d.ts
CHANGED
|
@@ -306,7 +306,7 @@ export interface RerankingConfig {
|
|
|
306
306
|
maxDocChars?: number;
|
|
307
307
|
/** Additional gate on the rerank score itself (0 = pure reorder, default). NOT comparable to retrieval.minScore — rerank scores are provider-scaled, not calibrated. */
|
|
308
308
|
minScore?: number;
|
|
309
|
-
/** Per-call timeout in ms; on expiry the fusion order is kept. @default
|
|
309
|
+
/** Per-call timeout in ms; on expiry the fusion order is kept. @default 4000 */
|
|
310
310
|
timeoutMs?: number;
|
|
311
311
|
/**
|
|
312
312
|
* Query template applied before scoring. With a `{query}` placeholder the
|
|
@@ -315,7 +315,12 @@ export interface RerankingConfig {
|
|
|
315
315
|
* reuse embedding.queryPrefix here. @default ""
|
|
316
316
|
*/
|
|
317
317
|
queryTemplate?: string;
|
|
318
|
-
/**
|
|
318
|
+
/**
|
|
319
|
+
* Document text: raw content, or description + content.
|
|
320
|
+
* `"content+description"` was the best arm in the 2026-10-02 eval (Hit@1 29.2 % vs 22.9 %,
|
|
321
|
+
* MRR 0.466 vs 0.414) — descriptions carry signal the raw code text misses.
|
|
322
|
+
* @default "content+description"
|
|
323
|
+
*/
|
|
319
324
|
docField?: "content" | "content+description";
|
|
320
325
|
/** Proxy configuration for the rerank endpoint. */
|
|
321
326
|
proxy?: ProxyConfig;
|
package/dist/core/config.js
CHANGED
|
@@ -155,9 +155,9 @@ export const DEFAULT_CONFIG = {
|
|
|
155
155
|
candidates: 20,
|
|
156
156
|
maxDocChars: 1200,
|
|
157
157
|
minScore: 0,
|
|
158
|
-
timeoutMs:
|
|
158
|
+
timeoutMs: 4000,
|
|
159
159
|
queryTemplate: "",
|
|
160
|
-
docField: "content",
|
|
160
|
+
docField: "content+description",
|
|
161
161
|
},
|
|
162
162
|
openCode: {
|
|
163
163
|
enabled: true,
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Package-manager detection for the setup and self-update flows.
|
|
3
|
+
*
|
|
4
|
+
* OpenCodeRAG is usually installed globally with npm, but users also install it
|
|
5
|
+
* via pnpm, yarn, or bun. Earlier releases shelled out to `npm` directly and
|
|
6
|
+
* failed with "/bin/sh: 1: npm: not found" on machines where npm is not on
|
|
7
|
+
* PATH (GitHub issue #34). Every global-package operation now goes through
|
|
8
|
+
* this module: it finds the package manager that actually owns the global
|
|
9
|
+
* install and renders the equivalent commands for it
|
|
10
|
+
* (`npm root -g` / `pnpm root -g` / `yarn global dir` / bun's fixed layout).
|
|
11
|
+
*/
|
|
12
|
+
/** Package managers with a working global-install mechanism that OpenCodeRAG supports. */
|
|
13
|
+
export type PackageManagerName = "npm" | "pnpm" | "yarn" | "bun";
|
|
14
|
+
/** Location of a global package install, independent of how it was installed. */
|
|
15
|
+
export interface GlobalInstall {
|
|
16
|
+
/** Package manager that owns the install, or `null` when only the directory could be located. */
|
|
17
|
+
pm: PackageManagerName | null;
|
|
18
|
+
/** Absolute path of the global `node_modules` directory that contains the package. */
|
|
19
|
+
globalRoot: string;
|
|
20
|
+
/** Absolute path of the package directory itself (the one containing `dist/`). */
|
|
21
|
+
packageDir: string;
|
|
22
|
+
}
|
|
23
|
+
/** Injectable seams so detection can be unit-tested without real package managers. */
|
|
24
|
+
export interface PackageManagerDeps {
|
|
25
|
+
/** Command runner (defaults to `execSync` with utf-8 output). */
|
|
26
|
+
exec?: (command: string, timeoutMs: number) => string;
|
|
27
|
+
/** Existence check (defaults to `fs.existsSync`). */
|
|
28
|
+
exists?: (filePath: string) => boolean;
|
|
29
|
+
/** JSON file reader (defaults to read + JSON.parse). */
|
|
30
|
+
readJson?: (filePath: string) => unknown;
|
|
31
|
+
/** Environment to detect from (defaults to `process.env`). */
|
|
32
|
+
env?: Record<string, string | undefined>;
|
|
33
|
+
/** Module path to derive a fallback install location from (defaults to this file). */
|
|
34
|
+
selfPath?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Candidate package managers, best guess first.
|
|
38
|
+
*
|
|
39
|
+
* Detection order: the manager that launched this process (user-agent), then
|
|
40
|
+
* the manager implied by where this module is installed, then the default
|
|
41
|
+
* probe order (npm, pnpm, bun, yarn).
|
|
42
|
+
*/
|
|
43
|
+
export declare function candidatePackageManagerOrder(deps?: PackageManagerDeps): PackageManagerName[];
|
|
44
|
+
/** The first package manager whose global root can be resolved (availability probe). */
|
|
45
|
+
export declare function bestAvailablePackageManager(deps?: PackageManagerDeps): PackageManagerName | null;
|
|
46
|
+
/** Shell command that globally installs `pkg` with the given manager. */
|
|
47
|
+
export declare function globalInstallCommand(pm: PackageManagerName, pkg: string): string;
|
|
48
|
+
/** Shell command that globally removes `pkg` with the given manager. */
|
|
49
|
+
export declare function globalUninstallCommand(pm: PackageManagerName, pkg: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Human-friendly install hint using the most likely manager for this machine
|
|
52
|
+
* (falls back to npm when nothing can be detected, e.g. npm-less CI probes).
|
|
53
|
+
*/
|
|
54
|
+
export declare function installHint(pkg: string, deps?: PackageManagerDeps): string;
|
|
55
|
+
/**
|
|
56
|
+
* Locate the global install of `pluginName` and the package manager that owns it.
|
|
57
|
+
*
|
|
58
|
+
* Strategy: ask each candidate manager for its global root and check whether
|
|
59
|
+
* the package sits there. When no package-manager CLI works at all (the
|
|
60
|
+
* issue-#34 case: pnpm install, npm missing), fall back to deriving the
|
|
61
|
+
* location from this module's own resolved path — the running CLI *is* the
|
|
62
|
+
* installed package, so its real path identifies the install even without any
|
|
63
|
+
* package manager on PATH.
|
|
64
|
+
*
|
|
65
|
+
* @param pluginName - npm package name to look for (e.g. `opencode-rag-plugin`).
|
|
66
|
+
* @param deps - Test seams; leave empty in production.
|
|
67
|
+
* @returns The resolved install, or `null` when the package is not globally installed.
|
|
68
|
+
*/
|
|
69
|
+
export declare function resolveGlobalInstall(pluginName: string, deps?: PackageManagerDeps): GlobalInstall | null;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Package-manager detection for the setup and self-update flows.
|
|
3
|
+
*
|
|
4
|
+
* OpenCodeRAG is usually installed globally with npm, but users also install it
|
|
5
|
+
* via pnpm, yarn, or bun. Earlier releases shelled out to `npm` directly and
|
|
6
|
+
* failed with "/bin/sh: 1: npm: not found" on machines where npm is not on
|
|
7
|
+
* PATH (GitHub issue #34). Every global-package operation now goes through
|
|
8
|
+
* this module: it finds the package manager that actually owns the global
|
|
9
|
+
* install and renders the equivalent commands for it
|
|
10
|
+
* (`npm root -g` / `pnpm root -g` / `yarn global dir` / bun's fixed layout).
|
|
11
|
+
*/
|
|
12
|
+
import os from "node:os";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
15
|
+
import { execSync } from "node:child_process";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
/** Default probe order when no signal identifies the owning package manager. */
|
|
18
|
+
const DEFAULT_ORDER = ["npm", "pnpm", "bun", "yarn"];
|
|
19
|
+
const EXEC_TIMEOUT_MS = 15_000;
|
|
20
|
+
/** Run a command and return trimmed stdout, or `null` on any failure. */
|
|
21
|
+
function tryRun(command, deps) {
|
|
22
|
+
try {
|
|
23
|
+
const out = deps.exec
|
|
24
|
+
? deps.exec(command, EXEC_TIMEOUT_MS)
|
|
25
|
+
: execSync(command, { encoding: "utf-8", timeout: EXEC_TIMEOUT_MS, stdio: ["ignore", "pipe", "ignore"] });
|
|
26
|
+
const trimmed = out.trim();
|
|
27
|
+
return trimmed || null;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const SPECS = {
|
|
34
|
+
npm: {
|
|
35
|
+
name: "npm",
|
|
36
|
+
globalRoot(deps) {
|
|
37
|
+
return tryRun("npm root -g", deps);
|
|
38
|
+
},
|
|
39
|
+
installCommand(pkg) {
|
|
40
|
+
return `npm install -g ${pkg} --no-fund --no-audit`;
|
|
41
|
+
},
|
|
42
|
+
uninstallCommand(pkg) {
|
|
43
|
+
return `npm uninstall -g ${pkg}`;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
pnpm: {
|
|
47
|
+
name: "pnpm",
|
|
48
|
+
globalRoot(deps) {
|
|
49
|
+
// Returns e.g. `~/.local/share/pnpm/global/v10/node_modules` on Linux or
|
|
50
|
+
// `%LOCALAPPDATA%\pnpm\global\v10\node_modules` on Windows. Installed
|
|
51
|
+
// packages are symlinks/junctions inside it, so existence checks follow.
|
|
52
|
+
return tryRun("pnpm root -g", deps);
|
|
53
|
+
},
|
|
54
|
+
installCommand(pkg) {
|
|
55
|
+
return `pnpm add -g ${pkg}`;
|
|
56
|
+
},
|
|
57
|
+
uninstallCommand(pkg) {
|
|
58
|
+
return `pnpm remove -g ${pkg}`;
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
yarn: {
|
|
62
|
+
name: "yarn",
|
|
63
|
+
globalRoot(deps) {
|
|
64
|
+
// Yarn 1 ("classic") keeps globals in `<yarn global dir>/node_modules`.
|
|
65
|
+
// Yarn Berry has no global installs and errors on `global` — the null
|
|
66
|
+
// return makes callers skip it.
|
|
67
|
+
const dir = tryRun("yarn global dir", deps);
|
|
68
|
+
return dir ? path.join(dir, "node_modules") : null;
|
|
69
|
+
},
|
|
70
|
+
installCommand(pkg) {
|
|
71
|
+
return `yarn global add ${pkg}`;
|
|
72
|
+
},
|
|
73
|
+
uninstallCommand(pkg) {
|
|
74
|
+
return `yarn global remove ${pkg}`;
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
bun: {
|
|
78
|
+
name: "bun",
|
|
79
|
+
globalRoot(deps) {
|
|
80
|
+
// `bun pm` has no root query; the global layout is fixed at
|
|
81
|
+
// `$BUN_INSTALL/install/global/node_modules` (default `~/.bun`).
|
|
82
|
+
if (tryRun("bun --version", deps) === null)
|
|
83
|
+
return null;
|
|
84
|
+
const base = (deps.env ?? process.env).BUN_INSTALL || path.join(os.homedir(), ".bun");
|
|
85
|
+
return path.join(base, "install", "global", "node_modules");
|
|
86
|
+
},
|
|
87
|
+
installCommand(pkg) {
|
|
88
|
+
return `bun add -g ${pkg}`;
|
|
89
|
+
},
|
|
90
|
+
uninstallCommand(pkg) {
|
|
91
|
+
return `bun remove -g ${pkg}`;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
/** Package manager that started this process, from `npm_config_user_agent` (set by all four). */
|
|
96
|
+
function preferredFromEnv(deps) {
|
|
97
|
+
const ua = (deps.env ?? process.env).npm_config_user_agent ?? "";
|
|
98
|
+
for (const name of DEFAULT_ORDER) {
|
|
99
|
+
if (ua.startsWith(`${name}/`))
|
|
100
|
+
return name;
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Package manager implied by the running module's own path.
|
|
106
|
+
*
|
|
107
|
+
* A pnpm-global install lives under `…/pnpm/global/…/node_modules/.pnpm/…`,
|
|
108
|
+
* a bun-global one under `~/.bun/install/global/…`, a Yarn classic global one
|
|
109
|
+
* under `~/.config/yarn/global/…`. npm's global roots carry no such marker.
|
|
110
|
+
*/
|
|
111
|
+
function preferredFromSelfLocation(deps) {
|
|
112
|
+
let self;
|
|
113
|
+
try {
|
|
114
|
+
self = deps.selfPath ?? fileURLToPath(import.meta.url);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
const segments = self.toLowerCase().split(/[\\/]+/);
|
|
120
|
+
if (segments.includes(".pnpm"))
|
|
121
|
+
return "pnpm";
|
|
122
|
+
for (const segment of segments) {
|
|
123
|
+
if (segment === "pnpm")
|
|
124
|
+
return "pnpm";
|
|
125
|
+
if (segment === ".bun")
|
|
126
|
+
return "bun";
|
|
127
|
+
if (segment === "yarn")
|
|
128
|
+
return "yarn";
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Candidate package managers, best guess first.
|
|
134
|
+
*
|
|
135
|
+
* Detection order: the manager that launched this process (user-agent), then
|
|
136
|
+
* the manager implied by where this module is installed, then the default
|
|
137
|
+
* probe order (npm, pnpm, bun, yarn).
|
|
138
|
+
*/
|
|
139
|
+
export function candidatePackageManagerOrder(deps = {}) {
|
|
140
|
+
const preferred = preferredFromEnv(deps) ?? preferredFromSelfLocation(deps);
|
|
141
|
+
if (!preferred)
|
|
142
|
+
return [...DEFAULT_ORDER];
|
|
143
|
+
return [preferred, ...DEFAULT_ORDER.filter((name) => name !== preferred)];
|
|
144
|
+
}
|
|
145
|
+
/** The first package manager whose global root can be resolved (availability probe). */
|
|
146
|
+
export function bestAvailablePackageManager(deps = {}) {
|
|
147
|
+
for (const name of candidatePackageManagerOrder(deps)) {
|
|
148
|
+
if (SPECS[name].globalRoot(deps) !== null)
|
|
149
|
+
return name;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
/** Shell command that globally installs `pkg` with the given manager. */
|
|
154
|
+
export function globalInstallCommand(pm, pkg) {
|
|
155
|
+
return SPECS[pm].installCommand(pkg);
|
|
156
|
+
}
|
|
157
|
+
/** Shell command that globally removes `pkg` with the given manager. */
|
|
158
|
+
export function globalUninstallCommand(pm, pkg) {
|
|
159
|
+
return SPECS[pm].uninstallCommand(pkg);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Human-friendly install hint using the most likely manager for this machine
|
|
163
|
+
* (falls back to npm when nothing can be detected, e.g. npm-less CI probes).
|
|
164
|
+
*/
|
|
165
|
+
export function installHint(pkg, deps = {}) {
|
|
166
|
+
const preferred = preferredFromEnv(deps) ?? preferredFromSelfLocation(deps) ?? bestAvailablePackageManager(deps);
|
|
167
|
+
return SPECS[preferred ?? "npm"].installCommand(pkg);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Locate the global install of `pluginName` and the package manager that owns it.
|
|
171
|
+
*
|
|
172
|
+
* Strategy: ask each candidate manager for its global root and check whether
|
|
173
|
+
* the package sits there. When no package-manager CLI works at all (the
|
|
174
|
+
* issue-#34 case: pnpm install, npm missing), fall back to deriving the
|
|
175
|
+
* location from this module's own resolved path — the running CLI *is* the
|
|
176
|
+
* installed package, so its real path identifies the install even without any
|
|
177
|
+
* package manager on PATH.
|
|
178
|
+
*
|
|
179
|
+
* @param pluginName - npm package name to look for (e.g. `opencode-rag-plugin`).
|
|
180
|
+
* @param deps - Test seams; leave empty in production.
|
|
181
|
+
* @returns The resolved install, or `null` when the package is not globally installed.
|
|
182
|
+
*/
|
|
183
|
+
export function resolveGlobalInstall(pluginName, deps = {}) {
|
|
184
|
+
for (const name of candidatePackageManagerOrder(deps)) {
|
|
185
|
+
const root = SPECS[name].globalRoot(deps);
|
|
186
|
+
if (!root)
|
|
187
|
+
continue;
|
|
188
|
+
const packageDir = path.join(root, pluginName);
|
|
189
|
+
if ((deps.exists ?? existsSync)(packageDir)) {
|
|
190
|
+
return { pm: name, globalRoot: root, packageDir };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return deriveFromSelfLocation(pluginName, deps);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Derive `{ globalRoot, packageDir }` by walking up from this module's path to
|
|
197
|
+
* the package.json that names `pluginName`, then to the shallowest
|
|
198
|
+
* `node_modules` ancestor that still contains the package (for pnpm that is
|
|
199
|
+
* the outer global root, not the `.pnpm` store directory).
|
|
200
|
+
*/
|
|
201
|
+
function deriveFromSelfLocation(pluginName, deps) {
|
|
202
|
+
const exists = deps.exists ?? existsSync;
|
|
203
|
+
const readJson = deps.readJson ?? ((filePath) => JSON.parse(readFileSync(filePath, "utf-8")));
|
|
204
|
+
let self;
|
|
205
|
+
try {
|
|
206
|
+
self = deps.selfPath ?? fileURLToPath(import.meta.url);
|
|
207
|
+
// Resolve junctions/symlinks so a runtime-dir junction points at the real
|
|
208
|
+
// global install instead of back at `~/.opencode/node_modules`.
|
|
209
|
+
if (!deps.selfPath)
|
|
210
|
+
self = realpathSync(self);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
let dir = path.dirname(self);
|
|
216
|
+
for (let depth = 0; depth < 24; depth++) {
|
|
217
|
+
const pkgJson = path.join(dir, "package.json");
|
|
218
|
+
if (exists(pkgJson)) {
|
|
219
|
+
let name;
|
|
220
|
+
try {
|
|
221
|
+
name = readJson(pkgJson).name;
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
name = undefined;
|
|
225
|
+
}
|
|
226
|
+
if (name === pluginName) {
|
|
227
|
+
let globalRoot = null;
|
|
228
|
+
let probe = dir;
|
|
229
|
+
let parent = path.dirname(probe);
|
|
230
|
+
while (parent !== probe) {
|
|
231
|
+
if (path.basename(probe) === "node_modules" && exists(path.join(probe, pluginName))) {
|
|
232
|
+
globalRoot = probe;
|
|
233
|
+
}
|
|
234
|
+
probe = parent;
|
|
235
|
+
parent = path.dirname(probe);
|
|
236
|
+
}
|
|
237
|
+
if (globalRoot) {
|
|
238
|
+
return { pm: null, globalRoot, packageDir: dir };
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const parent = path.dirname(dir);
|
|
244
|
+
if (parent === dir)
|
|
245
|
+
break;
|
|
246
|
+
dir = parent;
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=package-manager.js.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { type GlobalInstall } from "./package-manager.js";
|
|
1
2
|
export interface SetupResult {
|
|
2
3
|
success: boolean;
|
|
3
4
|
errors: string[];
|
|
4
5
|
}
|
|
5
6
|
export declare function getRuntimeDir(): string;
|
|
6
|
-
export declare function getNpmGlobalRoot(): string;
|
|
7
7
|
export declare function getVersionFile(runtimeDir: string): string;
|
|
8
8
|
export declare function readVersionFile(versionFile: string): string | null;
|
|
9
9
|
/**
|
|
@@ -12,10 +12,13 @@ export declare function readVersionFile(versionFile: string): string | null;
|
|
|
12
12
|
* (e.g. Notepad++) instead of Node.js, the CLI opens the editor instead of
|
|
13
13
|
* running. This function patches the wrappers to call `node` explicitly.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* npm only: pnpm/bun/yarn shims already invoke `node` explicitly, and their
|
|
16
|
+
* global bin directories are not laid out relative to the global root the way
|
|
17
|
+
* npm's are (`<prefix>\node_modules` next to `<prefix>`). Runs only on
|
|
18
|
+
* Windows; safe to call on every setup — detects already-patched wrappers by
|
|
19
|
+
* checking for the `node` prefix.
|
|
17
20
|
*/
|
|
18
|
-
export declare function patchWindowsWrappers(
|
|
21
|
+
export declare function patchWindowsWrappers(install: GlobalInstall): void;
|
|
19
22
|
export declare function setupRuntime(options?: {
|
|
20
23
|
force?: boolean;
|
|
21
24
|
silent?: boolean;
|
|
@@ -2,16 +2,11 @@ import path from "node:path";
|
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import { existsSync, mkdirSync, rmSync, writeFileSync, readFileSync, symlinkSync, lstatSync, } from "node:fs";
|
|
4
4
|
import { execSync } from "node:child_process";
|
|
5
|
+
import { bestAvailablePackageManager, globalInstallCommand, installHint, resolveGlobalInstall, } from "./package-manager.js";
|
|
5
6
|
const PLUGIN_NAME = "opencode-rag-plugin";
|
|
6
7
|
export function getRuntimeDir() {
|
|
7
8
|
return path.join(os.homedir(), ".opencode");
|
|
8
9
|
}
|
|
9
|
-
export function getNpmGlobalRoot() {
|
|
10
|
-
return execSync("npm root -g", {
|
|
11
|
-
encoding: "utf-8",
|
|
12
|
-
timeout: 10_000,
|
|
13
|
-
}).trim();
|
|
14
|
-
}
|
|
15
10
|
function createJunction(targetPath, linkPath) {
|
|
16
11
|
const type = process.platform === "win32" ? "junction" : "dir";
|
|
17
12
|
symlinkSync(targetPath, linkPath, type);
|
|
@@ -33,8 +28,8 @@ export function readVersionFile(versionFile) {
|
|
|
33
28
|
}
|
|
34
29
|
}
|
|
35
30
|
/** Bin name npm generates for this package (without .cmd/.ps1). */
|
|
36
|
-
function getBinName() {
|
|
37
|
-
const pkgJson = path.join(
|
|
31
|
+
function getBinName(packageDir) {
|
|
32
|
+
const pkgJson = path.join(packageDir, "package.json");
|
|
38
33
|
try {
|
|
39
34
|
const pkg = JSON.parse(readFileSync(pkgJson, "utf-8"));
|
|
40
35
|
if (pkg.bin && typeof pkg.bin === "object") {
|
|
@@ -55,14 +50,19 @@ function getBinName() {
|
|
|
55
50
|
* (e.g. Notepad++) instead of Node.js, the CLI opens the editor instead of
|
|
56
51
|
* running. This function patches the wrappers to call `node` explicitly.
|
|
57
52
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
53
|
+
* npm only: pnpm/bun/yarn shims already invoke `node` explicitly, and their
|
|
54
|
+
* global bin directories are not laid out relative to the global root the way
|
|
55
|
+
* npm's are (`<prefix>\node_modules` next to `<prefix>`). Runs only on
|
|
56
|
+
* Windows; safe to call on every setup — detects already-patched wrappers by
|
|
57
|
+
* checking for the `node` prefix.
|
|
60
58
|
*/
|
|
61
|
-
export function patchWindowsWrappers(
|
|
59
|
+
export function patchWindowsWrappers(install) {
|
|
62
60
|
if (process.platform !== "win32")
|
|
63
61
|
return;
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
if (install.pm !== "npm")
|
|
63
|
+
return;
|
|
64
|
+
const binDir = path.resolve(install.globalRoot, "..");
|
|
65
|
+
const binName = getBinName(install.packageDir);
|
|
66
66
|
// ── .cmd wrapper ──────────────────────────────────────────────
|
|
67
67
|
const cmdFile = path.join(binDir, `${binName}.cmd`);
|
|
68
68
|
if (existsSync(cmdFile)) {
|
|
@@ -107,20 +107,16 @@ export async function setupRuntime(options) {
|
|
|
107
107
|
if (alreadyInstalled && installedVersion === pluginVersion && !options?.force) {
|
|
108
108
|
return { success: true, errors: [] };
|
|
109
109
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return { success: false, errors };
|
|
117
|
-
}
|
|
118
|
-
const globalPluginDir = path.join(npmGlobalRoot, PLUGIN_NAME);
|
|
119
|
-
const globalSdkPluginDir = path.join(npmGlobalRoot, "@opencode-ai", "plugin");
|
|
120
|
-
if (!existsSync(globalPluginDir)) {
|
|
121
|
-
errors.push(`Plugin not found at: ${globalPluginDir}`);
|
|
110
|
+
// Locate the global install with any package manager (issue #34: npm may be
|
|
111
|
+
// absent entirely — pnpm/yarn/bun installs must work too).
|
|
112
|
+
const install = resolveGlobalInstall(PLUGIN_NAME);
|
|
113
|
+
if (!install) {
|
|
114
|
+
errors.push(`Could not find a global ${PLUGIN_NAME} install (checked npm, pnpm, yarn, and bun). ` +
|
|
115
|
+
`Install it first: ${installHint(PLUGIN_NAME)}`);
|
|
122
116
|
return { success: false, errors };
|
|
123
117
|
}
|
|
118
|
+
const globalPluginDir = install.packageDir;
|
|
119
|
+
const globalSdkPluginDir = path.join(install.globalRoot, "@opencode-ai", "plugin");
|
|
124
120
|
if (!existsSync(path.join(globalPluginDir, "dist", "cli.js"))) {
|
|
125
121
|
errors.push(`Global install seems incomplete: dist/ not found in ${globalPluginDir}`);
|
|
126
122
|
return { success: false, errors };
|
|
@@ -161,18 +157,24 @@ export async function setupRuntime(options) {
|
|
|
161
157
|
cpSync(globalPluginDir, runtimePluginDir, { recursive: true });
|
|
162
158
|
}
|
|
163
159
|
// Ensure the @opencode-ai/plugin SDK is available globally.
|
|
164
|
-
// We must NOT run
|
|
165
|
-
// re-resolves all of node_modules/ and replaces the plugin
|
|
166
|
-
// with the published npm version (corrupting the local link).
|
|
160
|
+
// We must NOT run a package install inside the runtime dir because the
|
|
161
|
+
// package manager re-resolves all of node_modules/ and replaces the plugin
|
|
162
|
+
// junction with the published npm version (corrupting the local link).
|
|
167
163
|
if (!existsSync(globalSdkPluginDir)) {
|
|
164
|
+
const sdkPm = install.pm ?? bestAvailablePackageManager();
|
|
165
|
+
if (!sdkPm) {
|
|
166
|
+
errors.push("No package manager (npm/pnpm/yarn/bun) found on PATH — cannot install the @opencode-ai/plugin SDK globally.");
|
|
167
|
+
return { success: false, errors };
|
|
168
|
+
}
|
|
169
|
+
const sdkInstallCommand = globalInstallCommand(sdkPm, "@opencode-ai/plugin");
|
|
168
170
|
try {
|
|
169
|
-
execSync(
|
|
171
|
+
execSync(sdkInstallCommand, {
|
|
170
172
|
stdio: "pipe",
|
|
171
173
|
timeout: 60_000,
|
|
172
174
|
});
|
|
173
175
|
}
|
|
174
176
|
catch (cause) {
|
|
175
|
-
errors.push(`Failed to install @opencode-ai/plugin SDK globally: ${cause.message}`);
|
|
177
|
+
errors.push(`Failed to install @opencode-ai/plugin SDK globally (${sdkInstallCommand}): ${cause.message}`);
|
|
176
178
|
return { success: false, errors };
|
|
177
179
|
}
|
|
178
180
|
}
|
|
@@ -213,7 +215,7 @@ export async function setupRuntime(options) {
|
|
|
213
215
|
return { success: false, errors };
|
|
214
216
|
}
|
|
215
217
|
writeFileSync(versionFile, pluginVersion, "utf-8");
|
|
216
|
-
patchWindowsWrappers(
|
|
218
|
+
patchWindowsWrappers(install);
|
|
217
219
|
const cliEntry = path.join(runtimePluginDir, "dist", "cli.js");
|
|
218
220
|
const pluginEntry = path.join(runtimePluginDir, "dist", "plugin-entry.js");
|
|
219
221
|
const sdkPkg = path.join(runtimeSdkPluginDir, "package.json");
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Version check and self-update functionality. Checks GitHub
|
|
3
|
-
* releases for new versions and installs the newest version via
|
|
4
|
-
* re-syncs the
|
|
3
|
+
* releases for new versions and installs the newest version via the package
|
|
4
|
+
* manager that owns the global install (npm/pnpm/yarn/bun), then re-syncs the
|
|
5
|
+
* OpenCode runtime junctions.
|
|
5
6
|
*/
|
|
6
7
|
import { setupRuntime } from "./setup-runtime.js";
|
|
8
|
+
import { type PackageManagerName } from "./package-manager.js";
|
|
7
9
|
/** Information about an available update. */
|
|
8
10
|
export interface UpdateInfo {
|
|
9
11
|
/** The currently installed version string. */
|
|
@@ -28,7 +30,7 @@ export interface InstallUpdateResult {
|
|
|
28
30
|
/** Version after the update (undefined if the install failed). */
|
|
29
31
|
toVersion?: string;
|
|
30
32
|
}
|
|
31
|
-
/** Callable shape for the
|
|
33
|
+
/** Callable shape for the installer runner (a subset of execSync's signature). */
|
|
32
34
|
type NpmRunner = (command: string, options: {
|
|
33
35
|
stdio: "inherit" | "pipe";
|
|
34
36
|
timeout: number;
|
|
@@ -67,24 +69,30 @@ export declare function checkForUpdate(currentVersion: string): Promise<UpdateIn
|
|
|
67
69
|
/**
|
|
68
70
|
* Install the newest published version of OpenCodeRAG.
|
|
69
71
|
*
|
|
70
|
-
* Runs
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* the
|
|
72
|
+
* Runs the global install command of the package manager that owns the
|
|
73
|
+
* current install (`npm install -g` / `pnpm add -g` / `yarn global add` /
|
|
74
|
+
* `bun add -g` with `<package>@latest`) — the npm binary itself is not
|
|
75
|
+
* required when the package was installed with another manager (GitHub
|
|
76
|
+
* issue #34). Then calls {@link setupRuntime} with `force: true` to
|
|
77
|
+
* re-create the `~/.opencode/node_modules/` junctions so OpenCode picks up
|
|
78
|
+
* the new build on the next restart.
|
|
74
79
|
*
|
|
75
|
-
* @param options - Optional verbosity flag. When `verbose` is true,
|
|
76
|
-
* is streamed to the console; otherwise it is captured silently. The
|
|
77
|
-
* `_execSync` and `
|
|
80
|
+
* @param options - Optional verbosity flag. When `verbose` is true, installer
|
|
81
|
+
* output is streamed to the console; otherwise it is captured silently. The
|
|
82
|
+
* `_execSync`, `_setupRuntime`, `_getCurrentVersion`, and `_packageManager`
|
|
83
|
+
* seams are for testing only.
|
|
78
84
|
* @returns An {@link InstallUpdateResult} with success status, message, and the
|
|
79
85
|
* from/to versions.
|
|
80
86
|
*/
|
|
81
87
|
export declare function installLatestUpdate(options?: {
|
|
82
88
|
verbose?: boolean;
|
|
83
|
-
/** Test seam: override the
|
|
89
|
+
/** Test seam: override the installer runner. */
|
|
84
90
|
_execSync?: NpmRunner;
|
|
85
91
|
/** Test seam: override the runtime sync. */
|
|
86
92
|
_setupRuntime?: typeof setupRuntime;
|
|
87
93
|
/** Test seam: override the version reader for the "to" version. */
|
|
88
94
|
_getCurrentVersion?: typeof getCurrentVersion;
|
|
95
|
+
/** Test seam: pin the package manager (`null` = force "none found"). */
|
|
96
|
+
_packageManager?: PackageManagerName | null;
|
|
89
97
|
}): Promise<InstallUpdateResult>;
|
|
90
98
|
export {};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Version check and self-update functionality. Checks GitHub
|
|
3
|
-
* releases for new versions and installs the newest version via
|
|
4
|
-
* re-syncs the
|
|
3
|
+
* releases for new versions and installs the newest version via the package
|
|
4
|
+
* manager that owns the global install (npm/pnpm/yarn/bun), then re-syncs the
|
|
5
|
+
* OpenCode runtime junctions.
|
|
5
6
|
*/
|
|
6
7
|
import { readFileSync } from "node:fs";
|
|
7
8
|
import path from "node:path";
|
|
8
9
|
import { fileURLToPath } from "node:url";
|
|
9
10
|
import { execSync } from "node:child_process";
|
|
10
11
|
import { setupRuntime } from "./setup-runtime.js";
|
|
12
|
+
import { bestAvailablePackageManager, globalInstallCommand, resolveGlobalInstall, } from "./package-manager.js";
|
|
11
13
|
/** npm package name (must match the `name` field in package.json). */
|
|
12
14
|
const PACKAGE_NAME = "opencode-rag-plugin";
|
|
13
15
|
/** GitHub repo used for release checks (owner/repo). */
|
|
@@ -107,14 +109,18 @@ export async function checkForUpdate(currentVersion) {
|
|
|
107
109
|
/**
|
|
108
110
|
* Install the newest published version of OpenCodeRAG.
|
|
109
111
|
*
|
|
110
|
-
* Runs
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* the
|
|
112
|
+
* Runs the global install command of the package manager that owns the
|
|
113
|
+
* current install (`npm install -g` / `pnpm add -g` / `yarn global add` /
|
|
114
|
+
* `bun add -g` with `<package>@latest`) — the npm binary itself is not
|
|
115
|
+
* required when the package was installed with another manager (GitHub
|
|
116
|
+
* issue #34). Then calls {@link setupRuntime} with `force: true` to
|
|
117
|
+
* re-create the `~/.opencode/node_modules/` junctions so OpenCode picks up
|
|
118
|
+
* the new build on the next restart.
|
|
114
119
|
*
|
|
115
|
-
* @param options - Optional verbosity flag. When `verbose` is true,
|
|
116
|
-
* is streamed to the console; otherwise it is captured silently. The
|
|
117
|
-
* `_execSync` and `
|
|
120
|
+
* @param options - Optional verbosity flag. When `verbose` is true, installer
|
|
121
|
+
* output is streamed to the console; otherwise it is captured silently. The
|
|
122
|
+
* `_execSync`, `_setupRuntime`, `_getCurrentVersion`, and `_packageManager`
|
|
123
|
+
* seams are for testing only.
|
|
118
124
|
* @returns An {@link InstallUpdateResult} with success status, message, and the
|
|
119
125
|
* from/to versions.
|
|
120
126
|
*/
|
|
@@ -125,8 +131,21 @@ export async function installLatestUpdate(options) {
|
|
|
125
131
|
const syncRuntime = options?._setupRuntime ?? setupRuntime;
|
|
126
132
|
const readVersion = options?._getCurrentVersion ?? getCurrentVersion;
|
|
127
133
|
const fromVersion = readVersion();
|
|
134
|
+
// Note: an explicit `null` pins "no package manager" (test seam), so this
|
|
135
|
+
// must not use ?? — null is a value here, not an absence.
|
|
136
|
+
const pm = options?._packageManager !== undefined
|
|
137
|
+
? options._packageManager
|
|
138
|
+
: resolveGlobalInstall(PACKAGE_NAME)?.pm ?? bestAvailablePackageManager();
|
|
139
|
+
if (!pm) {
|
|
140
|
+
return {
|
|
141
|
+
success: false,
|
|
142
|
+
message: `No package manager (npm/pnpm/yarn/bun) found on PATH. Update manually with ` +
|
|
143
|
+
`'npm install -g ${PACKAGE_NAME}@latest' or the equivalent for your package manager.`,
|
|
144
|
+
fromVersion,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
128
147
|
try {
|
|
129
|
-
run(
|
|
148
|
+
run(globalInstallCommand(pm, `${PACKAGE_NAME}@latest`), {
|
|
130
149
|
stdio,
|
|
131
150
|
timeout: 120_000,
|
|
132
151
|
});
|
|
@@ -134,7 +153,7 @@ export async function installLatestUpdate(options) {
|
|
|
134
153
|
catch (err) {
|
|
135
154
|
return {
|
|
136
155
|
success: false,
|
|
137
|
-
message:
|
|
156
|
+
message: `${pm} install failed: ${err.message}`,
|
|
138
157
|
fromVersion,
|
|
139
158
|
};
|
|
140
159
|
}
|