opencode-rag-plugin 2.1.0 → 2.2.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 +9 -2
- package/dist/cli/commands/init.d.ts +29 -0
- package/dist/cli/commands/init.js +326 -281
- package/dist/cli/commands/setup.js +84 -12
- package/package.json +1 -1
package/ReadMe.md
CHANGED
|
@@ -14,9 +14,9 @@ You don't need a dedicated GPU to run smaller embedding LLMs, as these models ca
|
|
|
14
14
|
# 1. Install globally (automatically sets up the OpenCode runtime)
|
|
15
15
|
npm install -g opencode-rag-plugin
|
|
16
16
|
|
|
17
|
-
# 2.
|
|
17
|
+
# 2. One command per workspace - runtime check + workspace setup
|
|
18
18
|
cd /path/to/your/project
|
|
19
|
-
opencode-rag
|
|
19
|
+
opencode-rag setup
|
|
20
20
|
|
|
21
21
|
# 3. Index your workspace
|
|
22
22
|
opencode-rag index
|
|
@@ -25,6 +25,13 @@ opencode-rag index
|
|
|
25
25
|
opencode-rag query "authentication middleware"
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
`opencode-rag setup` installs/verifies the machine-level runtime and, when
|
|
29
|
+
run inside a project, initializes the workspace (`.opencode/`, plugin entries,
|
|
30
|
+
skill file, `opencode-rag.json`) in one step. When the installed version is
|
|
31
|
+
outdated, it fetches and installs the latest published version from npm.
|
|
32
|
+
`opencode-rag init` remains available for re-syncing an already initialized
|
|
33
|
+
workspace, and `opencode-rag update` upgrades to a newer release.
|
|
34
|
+
|
|
28
35
|
**Prerequisites:** Node.js v22+, [Ollama](https://ollama.ai) (default) or other LLM-hosters (OpenAI-, Google- or Anthropic-compatible).
|
|
29
36
|
|
|
30
37
|
> **Contributors / developers:** Clone the repo and use `npm install --legacy-peer-deps; npm run build` — see [Development docs](doc/development.md).
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* installation.
|
|
11
11
|
*/
|
|
12
12
|
import type { Command } from "commander";
|
|
13
|
+
import type { InitOptions } from "../types.js";
|
|
13
14
|
/**
|
|
14
15
|
* Register the `init` command on the given Commander program.
|
|
15
16
|
*
|
|
@@ -20,3 +21,31 @@ import type { Command } from "commander";
|
|
|
20
21
|
* @param program - The Commander `Command` instance to register on.
|
|
21
22
|
*/
|
|
22
23
|
export declare function registerInitCommand(program: Command): void;
|
|
24
|
+
/**
|
|
25
|
+
* Run the full workspace initialization: `.opencode/` directory structure,
|
|
26
|
+
* plugin entry files, `opencode-rag.json`, skill file, AGENTS.md guidance,
|
|
27
|
+
* provider health checks, and plugin linking from the global runtime cache.
|
|
28
|
+
*
|
|
29
|
+
* Shared between the `init` command and the automatic workspace step of
|
|
30
|
+
* `opencode-rag setup`.
|
|
31
|
+
*
|
|
32
|
+
* @param options - init options (force, skipInstall, skipHealthCheck).
|
|
33
|
+
*/
|
|
34
|
+
export declare function runWorkspaceInit(options?: InitOptions): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Best-effort check whether `cwd` looks like a project root (git repo or
|
|
37
|
+
* common project manifest files). `opencode-rag setup` uses this to decide
|
|
38
|
+
* whether to automatically initialize the current workspace.
|
|
39
|
+
*
|
|
40
|
+
* @param cwd - Directory to inspect.
|
|
41
|
+
* @returns Whether the directory looks like a project root.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isLikelyProjectRoot(cwd: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Check whether the workspace at `cwd` has already been initialized with
|
|
46
|
+
* OpenCodeRAG (plugin entry files and the TUI plugin config are present).
|
|
47
|
+
*
|
|
48
|
+
* @param cwd - Directory to inspect.
|
|
49
|
+
* @returns Whether the workspace looks initialized.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isWorkspaceInitialized(cwd: string): boolean;
|
|
@@ -37,315 +37,360 @@ export function registerInitCommand(program) {
|
|
|
37
37
|
" Ollama backend (GPU vs CPU).\n" +
|
|
38
38
|
" - Re-running in an existing workspace: re-syncs plugin/skill files and keeps the\n" +
|
|
39
39
|
" existing opencode-rag.json (overwriting requires interactive confirmation).\n" +
|
|
40
|
-
"\
|
|
40
|
+
"\nUsually run automatically by 'opencode-rag setup'; run manually to re-sync an existing workspace.\n")
|
|
41
41
|
.option("-f, --force", "overwrite existing files")
|
|
42
42
|
.option("--skip-install", "skip installing workspace-local plugin dependencies")
|
|
43
43
|
.option("--skip-health-check", "skip provider connectivity and model availability check")
|
|
44
|
-
.action(
|
|
44
|
+
.action((options) => runWorkspaceInit(options));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Run the full workspace initialization: `.opencode/` directory structure,
|
|
48
|
+
* plugin entry files, `opencode-rag.json`, skill file, AGENTS.md guidance,
|
|
49
|
+
* provider health checks, and plugin linking from the global runtime cache.
|
|
50
|
+
*
|
|
51
|
+
* Shared between the `init` command and the automatic workspace step of
|
|
52
|
+
* `opencode-rag setup`.
|
|
53
|
+
*
|
|
54
|
+
* @param options - init options (force, skipInstall, skipHealthCheck).
|
|
55
|
+
*/
|
|
56
|
+
export async function runWorkspaceInit(options = {}) {
|
|
57
|
+
try {
|
|
58
|
+
const cwd = process.cwd();
|
|
59
|
+
const packageMetadata = getPackageMetadata();
|
|
60
|
+
// Use findConfigFile so an existing config in .opencode/rag.json (or
|
|
61
|
+
// .opencode/opencode-rag.json) is respected instead of being shadowed
|
|
62
|
+
// by a new root-level opencode-rag.json.
|
|
63
|
+
const configPath = findConfigFile(cwd) ?? path.join(cwd, "opencode-rag.json");
|
|
64
|
+
const opencodeDir = path.join(cwd, ".opencode");
|
|
65
|
+
const gitignorePath = path.join(opencodeDir, ".gitignore");
|
|
66
|
+
const opencodeConfigPath = path.join(opencodeDir, "opencode.json");
|
|
67
|
+
const pluginsDir = path.join(opencodeDir, "plugins");
|
|
68
|
+
const pluginEntryPath = path.join(pluginsDir, "rag-plugin.js");
|
|
69
|
+
const tuiPluginEntryPath = path.join(pluginsDir, "rag-tui.js");
|
|
70
|
+
const tuiConfigPath = path.join(opencodeDir, "tui.json");
|
|
71
|
+
const opencodePackagePath = path.join(opencodeDir, "package.json");
|
|
72
|
+
console.log(`\n${c.heading("Initializing OpenCodeRAG in workspace...")}\n`);
|
|
73
|
+
if (!existsSync(opencodeDir)) {
|
|
74
|
+
mkdirSync(opencodeDir, { recursive: true });
|
|
75
|
+
console.log(` ${c.created("Created:")} .opencode/`);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log(` ${c.exists("Exists:")} .opencode/`);
|
|
79
|
+
}
|
|
80
|
+
// Wiki safety: warn if wiki exists but would be affected (currently init never touches it)
|
|
81
|
+
const wikiDir = path.join(opencodeDir, "wiki");
|
|
82
|
+
if (existsSync(wikiDir)) {
|
|
83
|
+
console.log(` ${c.exists("Preserved:")} .opencode/wiki/ (never modified by init)`);
|
|
84
|
+
}
|
|
85
|
+
if (!existsSync(pluginsDir)) {
|
|
86
|
+
mkdirSync(pluginsDir, { recursive: true });
|
|
87
|
+
console.log(` ${c.created("Created:")} .opencode/plugins/`);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
console.log(` ${c.exists("Exists:")} .opencode/plugins/`);
|
|
91
|
+
}
|
|
92
|
+
const gitignoreExists = existsSync(gitignorePath);
|
|
93
|
+
const nextGitignoreContent = mergeGitignoreContent(gitignoreExists ? readFileSync(gitignorePath, "utf-8") : undefined);
|
|
94
|
+
if (!gitignoreExists || options.force || readFileSync(gitignorePath, "utf-8") !== nextGitignoreContent) {
|
|
95
|
+
writeFileSync(gitignorePath, nextGitignoreContent, "utf-8");
|
|
96
|
+
console.log(` ${gitignoreExists ? c.updated("Updated:") : c.created("Created:")} .opencode/.gitignore`);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
console.log(` ${c.exists("Exists:")} .opencode/.gitignore`);
|
|
100
|
+
}
|
|
101
|
+
const opencodeConfigExists = existsSync(opencodeConfigPath);
|
|
102
|
+
const nextOpencodeConfig = buildOpencodeConfig(readJsonObject(opencodeConfigPath));
|
|
103
|
+
if (!opencodeConfigExists || options.force) {
|
|
104
|
+
writeJsonFile(opencodeConfigPath, nextOpencodeConfig);
|
|
105
|
+
console.log(` ${opencodeConfigExists ? c.updated("Updated:") : c.created("Created:")} .opencode/opencode.json`);
|
|
106
|
+
}
|
|
107
|
+
else if (JSON.stringify(readJsonObject(opencodeConfigPath)) !== JSON.stringify(nextOpencodeConfig)) {
|
|
108
|
+
writeJsonFile(opencodeConfigPath, nextOpencodeConfig);
|
|
109
|
+
console.log(` ${c.updated("Updated:")} .opencode/opencode.json`);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
console.log(` ${c.exists("Exists:")} .opencode/opencode.json`);
|
|
113
|
+
}
|
|
114
|
+
const pluginEntryExists = existsSync(pluginEntryPath);
|
|
115
|
+
const pluginEntryContent = generateWorkspacePluginFile(packageMetadata.name);
|
|
116
|
+
if (!pluginEntryExists || options.force) {
|
|
117
|
+
writeFileSync(pluginEntryPath, pluginEntryContent, "utf-8");
|
|
118
|
+
console.log(` ${pluginEntryExists ? c.updated("Updated:") : c.created("Created:")} .opencode/plugins/rag-plugin.js`);
|
|
119
|
+
}
|
|
120
|
+
else if (readFileSync(pluginEntryPath, "utf-8") !== pluginEntryContent) {
|
|
121
|
+
writeFileSync(pluginEntryPath, pluginEntryContent, "utf-8");
|
|
122
|
+
console.log(` ${c.updated("Updated:")} .opencode/plugins/rag-plugin.js`);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
console.log(` ${c.exists("Exists:")} .opencode/plugins/rag-plugin.js`);
|
|
126
|
+
}
|
|
127
|
+
const tuiPluginEntryExists = existsSync(tuiPluginEntryPath);
|
|
128
|
+
const tuiPluginEntryContent = generateWorkspaceTuiPluginFile(packageMetadata.name);
|
|
129
|
+
if (!tuiPluginEntryExists || options.force) {
|
|
130
|
+
writeFileSync(tuiPluginEntryPath, tuiPluginEntryContent, "utf-8");
|
|
131
|
+
console.log(` ${tuiPluginEntryExists ? c.updated("Updated:") : c.created("Created:")} .opencode/plugins/rag-tui.js`);
|
|
132
|
+
}
|
|
133
|
+
else if (readFileSync(tuiPluginEntryPath, "utf-8") !== tuiPluginEntryContent) {
|
|
134
|
+
writeFileSync(tuiPluginEntryPath, tuiPluginEntryContent, "utf-8");
|
|
135
|
+
console.log(` ${c.updated("Updated:")} .opencode/plugins/rag-tui.js`);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
console.log(` ${c.exists("Exists:")} .opencode/plugins/rag-tui.js`);
|
|
139
|
+
}
|
|
140
|
+
const tuiConfigExists = existsSync(tuiConfigPath);
|
|
141
|
+
const nextTuiConfig = { plugin: ["./plugins/rag-tui.js"] };
|
|
142
|
+
if (!tuiConfigExists || options.force) {
|
|
143
|
+
writeJsonFile(tuiConfigPath, nextTuiConfig);
|
|
144
|
+
console.log(` ${tuiConfigExists ? c.updated("Updated:") : c.created("Created:")} .opencode/tui.json`);
|
|
145
|
+
}
|
|
146
|
+
else if (JSON.stringify(readJsonObject(tuiConfigPath)) !== JSON.stringify(nextTuiConfig)) {
|
|
147
|
+
writeJsonFile(tuiConfigPath, nextTuiConfig);
|
|
148
|
+
console.log(` ${c.updated("Updated:")} .opencode/tui.json`);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
console.log(` ${c.exists("Exists:")} .opencode/tui.json`);
|
|
152
|
+
}
|
|
153
|
+
const skillsDir = path.join(opencodeDir, "skills");
|
|
154
|
+
const skillDir = path.join(skillsDir, "opencode-rag");
|
|
155
|
+
const skillPath = path.join(skillDir, "SKILL.md");
|
|
156
|
+
if (!existsSync(skillDir)) {
|
|
157
|
+
mkdirSync(skillDir, { recursive: true });
|
|
158
|
+
console.log(` ${c.created("Created:")} .opencode/skills/opencode-rag/`);
|
|
159
|
+
}
|
|
160
|
+
const skillContent = generateSkillFile();
|
|
161
|
+
const skillExists = existsSync(skillPath);
|
|
162
|
+
if (!skillExists || options.force) {
|
|
163
|
+
writeFileSync(skillPath, skillContent, "utf-8");
|
|
164
|
+
console.log(` ${skillExists ? c.updated("Updated:") : c.created("Created:")} .opencode/skills/opencode-rag/SKILL.md`);
|
|
165
|
+
}
|
|
166
|
+
else if (readFileSync(skillPath, "utf-8") !== skillContent) {
|
|
167
|
+
writeFileSync(skillPath, skillContent, "utf-8");
|
|
168
|
+
console.log(` ${c.updated("Updated:")} .opencode/skills/opencode-rag/SKILL.md`);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
console.log(` ${c.exists("Exists:")} .opencode/skills/opencode-rag/SKILL.md`);
|
|
172
|
+
}
|
|
173
|
+
const agentsMdPath = path.join(cwd, "AGENTS.md");
|
|
174
|
+
const agentsMdExists = existsSync(agentsMdPath);
|
|
175
|
+
// Read effective promptEnforcement from existing config (if present)
|
|
176
|
+
let promptEnforcement = true;
|
|
45
177
|
try {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// .opencode/opencode-rag.json) is respected instead of being shadowed
|
|
50
|
-
// by a new root-level opencode-rag.json.
|
|
51
|
-
const configPath = findConfigFile(cwd) ?? path.join(cwd, "opencode-rag.json");
|
|
52
|
-
const opencodeDir = path.join(cwd, ".opencode");
|
|
53
|
-
const gitignorePath = path.join(opencodeDir, ".gitignore");
|
|
54
|
-
const opencodeConfigPath = path.join(opencodeDir, "opencode.json");
|
|
55
|
-
const pluginsDir = path.join(opencodeDir, "plugins");
|
|
56
|
-
const pluginEntryPath = path.join(pluginsDir, "rag-plugin.js");
|
|
57
|
-
const tuiPluginEntryPath = path.join(pluginsDir, "rag-tui.js");
|
|
58
|
-
const tuiConfigPath = path.join(opencodeDir, "tui.json");
|
|
59
|
-
const opencodePackagePath = path.join(opencodeDir, "package.json");
|
|
60
|
-
console.log(`\n${c.heading("Initializing OpenCodeRAG in workspace...")}\n`);
|
|
61
|
-
if (!existsSync(opencodeDir)) {
|
|
62
|
-
mkdirSync(opencodeDir, { recursive: true });
|
|
63
|
-
console.log(` ${c.created("Created:")} .opencode/`);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
console.log(` ${c.exists("Exists:")} .opencode/`);
|
|
67
|
-
}
|
|
68
|
-
// Wiki safety: warn if wiki exists but would be affected (currently init never touches it)
|
|
69
|
-
const wikiDir = path.join(opencodeDir, "wiki");
|
|
70
|
-
if (existsSync(wikiDir)) {
|
|
71
|
-
console.log(` ${c.exists("Preserved:")} .opencode/wiki/ (never modified by init)`);
|
|
72
|
-
}
|
|
73
|
-
if (!existsSync(pluginsDir)) {
|
|
74
|
-
mkdirSync(pluginsDir, { recursive: true });
|
|
75
|
-
console.log(` ${c.created("Created:")} .opencode/plugins/`);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
console.log(` ${c.exists("Exists:")} .opencode/plugins/`);
|
|
79
|
-
}
|
|
80
|
-
const gitignoreExists = existsSync(gitignorePath);
|
|
81
|
-
const nextGitignoreContent = mergeGitignoreContent(gitignoreExists ? readFileSync(gitignorePath, "utf-8") : undefined);
|
|
82
|
-
if (!gitignoreExists || options.force || readFileSync(gitignorePath, "utf-8") !== nextGitignoreContent) {
|
|
83
|
-
writeFileSync(gitignorePath, nextGitignoreContent, "utf-8");
|
|
84
|
-
console.log(` ${gitignoreExists ? c.updated("Updated:") : c.created("Created:")} .opencode/.gitignore`);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
console.log(` ${c.exists("Exists:")} .opencode/.gitignore`);
|
|
88
|
-
}
|
|
89
|
-
const opencodeConfigExists = existsSync(opencodeConfigPath);
|
|
90
|
-
const nextOpencodeConfig = buildOpencodeConfig(readJsonObject(opencodeConfigPath));
|
|
91
|
-
if (!opencodeConfigExists || options.force) {
|
|
92
|
-
writeJsonFile(opencodeConfigPath, nextOpencodeConfig);
|
|
93
|
-
console.log(` ${opencodeConfigExists ? c.updated("Updated:") : c.created("Created:")} .opencode/opencode.json`);
|
|
94
|
-
}
|
|
95
|
-
else if (JSON.stringify(readJsonObject(opencodeConfigPath)) !== JSON.stringify(nextOpencodeConfig)) {
|
|
96
|
-
writeJsonFile(opencodeConfigPath, nextOpencodeConfig);
|
|
97
|
-
console.log(` ${c.updated("Updated:")} .opencode/opencode.json`);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
console.log(` ${c.exists("Exists:")} .opencode/opencode.json`);
|
|
101
|
-
}
|
|
102
|
-
const pluginEntryExists = existsSync(pluginEntryPath);
|
|
103
|
-
const pluginEntryContent = generateWorkspacePluginFile(packageMetadata.name);
|
|
104
|
-
if (!pluginEntryExists || options.force) {
|
|
105
|
-
writeFileSync(pluginEntryPath, pluginEntryContent, "utf-8");
|
|
106
|
-
console.log(` ${pluginEntryExists ? c.updated("Updated:") : c.created("Created:")} .opencode/plugins/rag-plugin.js`);
|
|
107
|
-
}
|
|
108
|
-
else if (readFileSync(pluginEntryPath, "utf-8") !== pluginEntryContent) {
|
|
109
|
-
writeFileSync(pluginEntryPath, pluginEntryContent, "utf-8");
|
|
110
|
-
console.log(` ${c.updated("Updated:")} .opencode/plugins/rag-plugin.js`);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
console.log(` ${c.exists("Exists:")} .opencode/plugins/rag-plugin.js`);
|
|
114
|
-
}
|
|
115
|
-
const tuiPluginEntryExists = existsSync(tuiPluginEntryPath);
|
|
116
|
-
const tuiPluginEntryContent = generateWorkspaceTuiPluginFile(packageMetadata.name);
|
|
117
|
-
if (!tuiPluginEntryExists || options.force) {
|
|
118
|
-
writeFileSync(tuiPluginEntryPath, tuiPluginEntryContent, "utf-8");
|
|
119
|
-
console.log(` ${tuiPluginEntryExists ? c.updated("Updated:") : c.created("Created:")} .opencode/plugins/rag-tui.js`);
|
|
120
|
-
}
|
|
121
|
-
else if (readFileSync(tuiPluginEntryPath, "utf-8") !== tuiPluginEntryContent) {
|
|
122
|
-
writeFileSync(tuiPluginEntryPath, tuiPluginEntryContent, "utf-8");
|
|
123
|
-
console.log(` ${c.updated("Updated:")} .opencode/plugins/rag-tui.js`);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
console.log(` ${c.exists("Exists:")} .opencode/plugins/rag-tui.js`);
|
|
127
|
-
}
|
|
128
|
-
const tuiConfigExists = existsSync(tuiConfigPath);
|
|
129
|
-
const nextTuiConfig = { plugin: ["./plugins/rag-tui.js"] };
|
|
130
|
-
if (!tuiConfigExists || options.force) {
|
|
131
|
-
writeJsonFile(tuiConfigPath, nextTuiConfig);
|
|
132
|
-
console.log(` ${tuiConfigExists ? c.updated("Updated:") : c.created("Created:")} .opencode/tui.json`);
|
|
133
|
-
}
|
|
134
|
-
else if (JSON.stringify(readJsonObject(tuiConfigPath)) !== JSON.stringify(nextTuiConfig)) {
|
|
135
|
-
writeJsonFile(tuiConfigPath, nextTuiConfig);
|
|
136
|
-
console.log(` ${c.updated("Updated:")} .opencode/tui.json`);
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
console.log(` ${c.exists("Exists:")} .opencode/tui.json`);
|
|
140
|
-
}
|
|
141
|
-
const skillsDir = path.join(opencodeDir, "skills");
|
|
142
|
-
const skillDir = path.join(skillsDir, "opencode-rag");
|
|
143
|
-
const skillPath = path.join(skillDir, "SKILL.md");
|
|
144
|
-
if (!existsSync(skillDir)) {
|
|
145
|
-
mkdirSync(skillDir, { recursive: true });
|
|
146
|
-
console.log(` ${c.created("Created:")} .opencode/skills/opencode-rag/`);
|
|
178
|
+
if (existsSync(configPath)) {
|
|
179
|
+
const existingCfg = loadConfig(configPath, false);
|
|
180
|
+
promptEnforcement = existingCfg.memory?.promptEnforcement ?? true;
|
|
147
181
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Malformed or missing config — use default
|
|
185
|
+
}
|
|
186
|
+
const nextAgentsMd = mergeAgentsMdContent(agentsMdExists ? readFileSync(agentsMdPath, "utf-8") : undefined, { promptEnforcement });
|
|
187
|
+
if (!agentsMdExists || options.force) {
|
|
188
|
+
writeFileSync(agentsMdPath, nextAgentsMd, "utf-8");
|
|
189
|
+
console.log(` ${agentsMdExists ? c.updated("Updated:") : c.created("Created:")} AGENTS.md`);
|
|
190
|
+
}
|
|
191
|
+
else if (readFileSync(agentsMdPath, "utf-8") !== nextAgentsMd) {
|
|
192
|
+
writeFileSync(agentsMdPath, nextAgentsMd, "utf-8");
|
|
193
|
+
console.log(` ${c.updated("Updated:")} AGENTS.md`);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
console.log(` ${c.exists("Exists:")} AGENTS.md`);
|
|
197
|
+
}
|
|
198
|
+
const workspacePackageExists = existsSync(opencodePackagePath);
|
|
199
|
+
const nextWorkspacePackage = buildWorkspacePackageJson(readJsonObject(opencodePackagePath), packageMetadata);
|
|
200
|
+
if (!workspacePackageExists || options.force) {
|
|
201
|
+
writeJsonFile(opencodePackagePath, nextWorkspacePackage);
|
|
202
|
+
console.log(` ${workspacePackageExists ? c.updated("Updated:") : c.created("Created:")} .opencode/package.json`);
|
|
203
|
+
}
|
|
204
|
+
else if (JSON.stringify(readJsonObject(opencodePackagePath)) !== JSON.stringify(nextWorkspacePackage)) {
|
|
205
|
+
writeJsonFile(opencodePackagePath, nextWorkspacePackage);
|
|
206
|
+
console.log(` ${c.updated("Updated:")} .opencode/package.json`);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
console.log(` ${c.exists("Exists:")} .opencode/package.json`);
|
|
210
|
+
}
|
|
211
|
+
const configExists = existsSync(configPath);
|
|
212
|
+
// Detect the Ollama backend (CPU vs GPU) lazily — only when we are
|
|
213
|
+
// actually about to write a config — and tune embedding batches for it.
|
|
214
|
+
let detectedTuning;
|
|
215
|
+
const configContent = async () => {
|
|
216
|
+
if (!detectedTuning) {
|
|
217
|
+
try {
|
|
218
|
+
const info = await detectOllamaBackend();
|
|
219
|
+
detectedTuning = info.tuning;
|
|
220
|
+
const icon = info.backend === "gpu" ? c.success("GPU:") :
|
|
221
|
+
info.backend === "cpu" ? c.warn("CPU:") :
|
|
222
|
+
c.dim("Backend:");
|
|
223
|
+
console.log(` ${icon} ${info.message}`);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
detectedTuning = undefined;
|
|
169
227
|
}
|
|
170
228
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
console.log(` ${c.updated("Updated:")} .opencode/package.json`);
|
|
229
|
+
return generateDefaultConfigJson(detectedTuning);
|
|
230
|
+
};
|
|
231
|
+
if (!configExists) {
|
|
232
|
+
writeFileSync(configPath, await configContent(), "utf-8");
|
|
233
|
+
console.log(` ${c.created("Created:")} opencode-rag.json`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// NEVER overwrite existing config without interactive confirmation
|
|
237
|
+
let overwrite = false;
|
|
238
|
+
if (process.stdin.isTTY) {
|
|
239
|
+
const readline = await import("node:readline");
|
|
240
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
241
|
+
const answer = await new Promise((resolve) => {
|
|
242
|
+
rl.question(` ${c.warn("opencode-rag.json already exists.")} Overwrite with default config? A backup will be saved. (y/N) `, resolve);
|
|
243
|
+
});
|
|
244
|
+
rl.close();
|
|
245
|
+
overwrite = /^y(?:es)?$/i.test(answer.trim());
|
|
246
|
+
}
|
|
247
|
+
if (overwrite) {
|
|
248
|
+
copyFileSync(configPath, `${configPath}.bak`);
|
|
249
|
+
console.log(` ${c.dim("Backup:")} opencode-rag.json.bak`);
|
|
250
|
+
writeFileSync(configPath, await configContent(), "utf-8");
|
|
251
|
+
console.log(` ${c.updated("Updated:")} opencode-rag.json`);
|
|
195
252
|
}
|
|
196
253
|
else {
|
|
197
|
-
console.log(` ${c.exists("Exists:")}
|
|
254
|
+
console.log(` ${c.exists("Exists:")} opencode-rag.json (preserved)`);
|
|
255
|
+
if (!process.stdin.isTTY && options.force) {
|
|
256
|
+
console.log(` ${c.warn("Skipped overwrite:")} non-interactive shell. Edit opencode-rag.json manually or re-run in a TTY.`);
|
|
257
|
+
}
|
|
198
258
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
259
|
+
}
|
|
260
|
+
// ── Provider health check + dependency install (parallel) ──
|
|
261
|
+
const healthPromise = options.skipHealthCheck
|
|
262
|
+
? null
|
|
263
|
+
: (async () => {
|
|
264
|
+
console.log(`\n${c.heading("Checking provider connectivity...")}\n`);
|
|
265
|
+
const ragConfig = loadConfig(configPath);
|
|
266
|
+
return checkProviderHealth(ragConfig);
|
|
267
|
+
})();
|
|
268
|
+
const installPromise = options.skipInstall
|
|
269
|
+
? null
|
|
270
|
+
: installPluginFromGlobal(opencodeDir, packageMetadata.name, false);
|
|
271
|
+
// Wait for health check results first
|
|
272
|
+
if (healthPromise) {
|
|
273
|
+
const results = await healthPromise;
|
|
274
|
+
for (const r of results) {
|
|
275
|
+
const icon = r.status === "ok" ? c.success("✓") : r.status === "missing" ? c.warn("○") : c.error("✗");
|
|
276
|
+
const typeLabel = r.type === "image_description" ? "image description" : r.type === "image_description_on_demand" ? "image description (on-demand)" : r.type;
|
|
277
|
+
const label = `${typeLabel} model`;
|
|
278
|
+
console.log(` ${icon} ${c.value(r.model)} (${r.provider}) — ${label}: ${r.status}`);
|
|
279
|
+
if (r.error)
|
|
280
|
+
console.log(` ${c.dim(r.error)}`);
|
|
281
|
+
}
|
|
282
|
+
const missingOllama = results.filter((r) => r.status === "missing" && r.provider === "ollama");
|
|
283
|
+
if (missingOllama.length > 0) {
|
|
284
|
+
const ragConfig = loadConfig(configPath);
|
|
285
|
+
const pullEntries = missingOllama.map((r) => {
|
|
286
|
+
if (r.type === "embedding") {
|
|
287
|
+
return { model: r.model, baseUrl: ragConfig.embedding.baseUrl, proxy: ragConfig.embedding.proxy };
|
|
212
288
|
}
|
|
213
|
-
|
|
214
|
-
|
|
289
|
+
if (r.type === "description" && ragConfig.description) {
|
|
290
|
+
return { model: r.model, baseUrl: ragConfig.description.baseUrl, proxy: ragConfig.description.proxy };
|
|
215
291
|
}
|
|
292
|
+
if (r.type === "image_description" && ragConfig.imageDescription) {
|
|
293
|
+
return { model: r.model, baseUrl: ragConfig.imageDescription.baseUrl, proxy: ragConfig.imageDescription.proxy };
|
|
294
|
+
}
|
|
295
|
+
if (r.type === "image_description_on_demand" && ragConfig.imageDescription) {
|
|
296
|
+
const onDemand = ragConfig.imageDescription.onDemand;
|
|
297
|
+
return {
|
|
298
|
+
model: r.model,
|
|
299
|
+
baseUrl: onDemand?.baseUrl ?? ragConfig.imageDescription.baseUrl,
|
|
300
|
+
proxy: onDemand?.proxy ?? ragConfig.imageDescription.proxy,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
return { model: r.model, baseUrl: ragConfig.embedding.baseUrl, proxy: ragConfig.embedding.proxy };
|
|
304
|
+
});
|
|
305
|
+
console.log(`\n ${c.warn("Models not found:")} ${pullEntries.map((e) => e.model).join(", ")}`);
|
|
306
|
+
// Non-TTY guard: rl.question would block forever with a still-open
|
|
307
|
+
// stdin pipe (e.g. `echo y | opencode-rag init`, CI runners).
|
|
308
|
+
if (!process.stdin.isTTY) {
|
|
309
|
+
console.log(` ${c.dim("Non-interactive shell — skipping. Pull manually with: ollama pull <model>")}`);
|
|
216
310
|
}
|
|
217
|
-
|
|
218
|
-
};
|
|
219
|
-
if (!configExists) {
|
|
220
|
-
writeFileSync(configPath, await configContent(), "utf-8");
|
|
221
|
-
console.log(` ${c.created("Created:")} opencode-rag.json`);
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
// NEVER overwrite existing config without interactive confirmation
|
|
225
|
-
let overwrite = false;
|
|
226
|
-
if (process.stdin.isTTY) {
|
|
311
|
+
else {
|
|
227
312
|
const readline = await import("node:readline");
|
|
228
313
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
229
314
|
const answer = await new Promise((resolve) => {
|
|
230
|
-
rl.question(` ${
|
|
315
|
+
rl.question(` Pull ${pullEntries.length === 1 ? "this model" : "these models"} now? (y/n) `, resolve);
|
|
231
316
|
});
|
|
232
317
|
rl.close();
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
console.log(` ${c.exists("Exists:")} opencode-rag.json (preserved)`);
|
|
243
|
-
if (!process.stdin.isTTY && options.force) {
|
|
244
|
-
console.log(` ${c.warn("Skipped overwrite:")} non-interactive shell. Edit opencode-rag.json manually or re-run in a TTY.`);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
// ── Provider health check + dependency install (parallel) ──
|
|
249
|
-
const healthPromise = options.skipHealthCheck
|
|
250
|
-
? null
|
|
251
|
-
: (async () => {
|
|
252
|
-
console.log(`\n${c.heading("Checking provider connectivity...")}\n`);
|
|
253
|
-
const ragConfig = loadConfig(configPath);
|
|
254
|
-
return checkProviderHealth(ragConfig);
|
|
255
|
-
})();
|
|
256
|
-
const installPromise = options.skipInstall
|
|
257
|
-
? null
|
|
258
|
-
: installPluginFromGlobal(opencodeDir, packageMetadata.name, false);
|
|
259
|
-
// Wait for health check results first
|
|
260
|
-
if (healthPromise) {
|
|
261
|
-
const results = await healthPromise;
|
|
262
|
-
for (const r of results) {
|
|
263
|
-
const icon = r.status === "ok" ? c.success("✓") : r.status === "missing" ? c.warn("○") : c.error("✗");
|
|
264
|
-
const typeLabel = r.type === "image_description" ? "image description" : r.type === "image_description_on_demand" ? "image description (on-demand)" : r.type;
|
|
265
|
-
const label = `${typeLabel} model`;
|
|
266
|
-
console.log(` ${icon} ${c.value(r.model)} (${r.provider}) — ${label}: ${r.status}`);
|
|
267
|
-
if (r.error)
|
|
268
|
-
console.log(` ${c.dim(r.error)}`);
|
|
269
|
-
}
|
|
270
|
-
const missingOllama = results.filter((r) => r.status === "missing" && r.provider === "ollama");
|
|
271
|
-
if (missingOllama.length > 0) {
|
|
272
|
-
const ragConfig = loadConfig(configPath);
|
|
273
|
-
const pullEntries = missingOllama.map((r) => {
|
|
274
|
-
if (r.type === "embedding") {
|
|
275
|
-
return { model: r.model, baseUrl: ragConfig.embedding.baseUrl, proxy: ragConfig.embedding.proxy };
|
|
276
|
-
}
|
|
277
|
-
if (r.type === "description" && ragConfig.description) {
|
|
278
|
-
return { model: r.model, baseUrl: ragConfig.description.baseUrl, proxy: ragConfig.description.proxy };
|
|
318
|
+
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
|
|
319
|
+
console.log();
|
|
320
|
+
try {
|
|
321
|
+
await pullOllamaModels(pullEntries, (model, line) => {
|
|
322
|
+
console.log(` ${c.value(model)}: ${line}`);
|
|
323
|
+
});
|
|
324
|
+
console.log(`\n ${c.success("Models pulled successfully.")}`);
|
|
279
325
|
}
|
|
280
|
-
|
|
281
|
-
|
|
326
|
+
catch (err) {
|
|
327
|
+
console.error(`\n ${c.error("Pull failed:")} ${err.message}`);
|
|
328
|
+
console.log(` ${c.dim("Pull manually with: ollama pull <model>")}`);
|
|
282
329
|
}
|
|
283
|
-
if (r.type === "image_description_on_demand" && ragConfig.imageDescription) {
|
|
284
|
-
const onDemand = ragConfig.imageDescription.onDemand;
|
|
285
|
-
return {
|
|
286
|
-
model: r.model,
|
|
287
|
-
baseUrl: onDemand?.baseUrl ?? ragConfig.imageDescription.baseUrl,
|
|
288
|
-
proxy: onDemand?.proxy ?? ragConfig.imageDescription.proxy,
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
return { model: r.model, baseUrl: ragConfig.embedding.baseUrl, proxy: ragConfig.embedding.proxy };
|
|
292
|
-
});
|
|
293
|
-
console.log(`\n ${c.warn("Models not found:")} ${pullEntries.map((e) => e.model).join(", ")}`);
|
|
294
|
-
// Non-TTY guard: rl.question would block forever with a still-open
|
|
295
|
-
// stdin pipe (e.g. `echo y | opencode-rag init`, CI runners).
|
|
296
|
-
if (!process.stdin.isTTY) {
|
|
297
|
-
console.log(` ${c.dim("Non-interactive shell — skipping. Pull manually with: ollama pull <model>")}`);
|
|
298
330
|
}
|
|
299
331
|
else {
|
|
300
|
-
|
|
301
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
302
|
-
const answer = await new Promise((resolve) => {
|
|
303
|
-
rl.question(` Pull ${pullEntries.length === 1 ? "this model" : "these models"} now? (y/n) `, resolve);
|
|
304
|
-
});
|
|
305
|
-
rl.close();
|
|
306
|
-
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
|
|
307
|
-
console.log();
|
|
308
|
-
try {
|
|
309
|
-
await pullOllamaModels(pullEntries, (model, line) => {
|
|
310
|
-
console.log(` ${c.value(model)}: ${line}`);
|
|
311
|
-
});
|
|
312
|
-
console.log(`\n ${c.success("Models pulled successfully.")}`);
|
|
313
|
-
}
|
|
314
|
-
catch (err) {
|
|
315
|
-
console.error(`\n ${c.error("Pull failed:")} ${err.message}`);
|
|
316
|
-
console.log(` ${c.dim("Pull manually with: ollama pull <model>")}`);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
console.log(` ${c.dim("Skipped. Pull manually with: ollama pull <model>")}`);
|
|
321
|
-
}
|
|
332
|
+
console.log(` ${c.dim("Skipped. Pull manually with: ollama pull <model>")}`);
|
|
322
333
|
}
|
|
323
334
|
}
|
|
324
|
-
const hasErrors = results.some((r) => r.status === "error");
|
|
325
|
-
if (hasErrors) {
|
|
326
|
-
console.log(`\n ${c.warn("Some providers are not reachable.")} Check configuration and network, then run ${c.file("'opencode-rag index'")}.`);
|
|
327
|
-
}
|
|
328
335
|
}
|
|
329
|
-
|
|
330
|
-
if (
|
|
331
|
-
console.log(`\n${c.
|
|
332
|
-
await installPromise;
|
|
333
|
-
console.log(`\n ${c.success("Installed:")} .opencode/node_modules/`);
|
|
334
|
-
console.log(` ${c.dim("Plugin is linked to global runtime cache; no npm install was required.")}`);
|
|
336
|
+
const hasErrors = results.some((r) => r.status === "error");
|
|
337
|
+
if (hasErrors) {
|
|
338
|
+
console.log(`\n ${c.warn("Some providers are not reachable.")} Check configuration and network, then run ${c.file("'opencode-rag index'")}.`);
|
|
335
339
|
}
|
|
336
|
-
else {
|
|
337
|
-
console.log(`\n ${c.exists("Skipped:")} plugin installation (--skip-install)`);
|
|
338
|
-
}
|
|
339
|
-
console.log(`\n${c.success("Done.")} Restart OpenCode if it is running, then run ${c.file("'opencode-rag index'")} in this workspace.`);
|
|
340
340
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
console.
|
|
344
|
-
|
|
341
|
+
// Now install the plugin from global cache via junction
|
|
342
|
+
if (installPromise) {
|
|
343
|
+
console.log(`\n${c.heading("Installing workspace-local plugin...")}\n`);
|
|
344
|
+
await installPromise;
|
|
345
|
+
console.log(`\n ${c.success("Installed:")} .opencode/node_modules/`);
|
|
346
|
+
console.log(` ${c.dim("Plugin is linked to global runtime cache; no npm install was required.")}`);
|
|
345
347
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
+
else {
|
|
349
|
+
console.log(`\n ${c.exists("Skipped:")} plugin installation (--skip-install)`);
|
|
348
350
|
}
|
|
349
|
-
|
|
351
|
+
console.log(`\n${c.success("Done.")} Restart OpenCode if it is running, then run ${c.file("'opencode-rag index'")} in this workspace.`);
|
|
352
|
+
}
|
|
353
|
+
catch (err) {
|
|
354
|
+
console.error(`\n ${c.error("Init failed:")} ${err.message}`);
|
|
355
|
+
console.error(` ${c.dim("Fix the issue above, then run 'opencode-rag init' again.")}`);
|
|
356
|
+
process.exitCode = 1;
|
|
357
|
+
}
|
|
358
|
+
finally {
|
|
359
|
+
destroyAllPooledConnections();
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Best-effort check whether `cwd` looks like a project root (git repo or
|
|
364
|
+
* common project manifest files). `opencode-rag setup` uses this to decide
|
|
365
|
+
* whether to automatically initialize the current workspace.
|
|
366
|
+
*
|
|
367
|
+
* @param cwd - Directory to inspect.
|
|
368
|
+
* @returns Whether the directory looks like a project root.
|
|
369
|
+
*/
|
|
370
|
+
export function isLikelyProjectRoot(cwd) {
|
|
371
|
+
if (existsSync(path.join(cwd, ".git"))) {
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
const markers = [
|
|
375
|
+
"package.json",
|
|
376
|
+
"AGENTS.md",
|
|
377
|
+
"README.md",
|
|
378
|
+
"pyproject.toml",
|
|
379
|
+
"Cargo.toml",
|
|
380
|
+
"go.mod",
|
|
381
|
+
"opencode-rag.json",
|
|
382
|
+
];
|
|
383
|
+
return markers.some((marker) => existsSync(path.join(cwd, marker)));
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Check whether the workspace at `cwd` has already been initialized with
|
|
387
|
+
* OpenCodeRAG (plugin entry files and the TUI plugin config are present).
|
|
388
|
+
*
|
|
389
|
+
* @param cwd - Directory to inspect.
|
|
390
|
+
* @returns Whether the workspace looks initialized.
|
|
391
|
+
*/
|
|
392
|
+
export function isWorkspaceInitialized(cwd) {
|
|
393
|
+
return (existsSync(path.join(cwd, ".opencode", "plugins", "rag-plugin.js")) &&
|
|
394
|
+
existsSync(path.join(cwd, ".opencode", "tui.json")));
|
|
350
395
|
}
|
|
351
396
|
//# sourceMappingURL=init.js.map
|
|
@@ -4,6 +4,8 @@ import { execSync } from "node:child_process";
|
|
|
4
4
|
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
|
+
import { compareVersions, getCurrentVersion, installLatestUpdate } from "../../core/version-check.js";
|
|
8
|
+
import { isLikelyProjectRoot, isWorkspaceInitialized, runWorkspaceInit } from "./init.js";
|
|
7
9
|
const PLUGIN_NAME = "opencode-rag-plugin";
|
|
8
10
|
function removeIfExists(targetPath) {
|
|
9
11
|
if (existsSync(targetPath)) {
|
|
@@ -36,13 +38,16 @@ function checkOpenCodeRunning() {
|
|
|
36
38
|
export function registerSetupCommand(program) {
|
|
37
39
|
program
|
|
38
40
|
.command("setup")
|
|
39
|
-
.description("Install/update the OpenCodeRAG runtime
|
|
41
|
+
.description("Install/update the OpenCodeRAG runtime and initialize the current workspace")
|
|
40
42
|
.addHelpText("after", "\nUse cases:\n" +
|
|
41
43
|
" - First-time install: run once per machine to install the plugin runtime\n" +
|
|
42
|
-
" into ~/.opencode/ so OpenCode can discover the RAG plugin
|
|
43
|
-
"
|
|
44
|
+
" into ~/.opencode/ so OpenCode can discover the RAG plugin. When\n" +
|
|
45
|
+
" outdated, fetches and installs the latest published version from npm.\n" +
|
|
46
|
+
" - Workspace setup: run inside a project to also initialize .opencode/,\n" +
|
|
47
|
+
" the skill file and opencode-rag.json for this workspace.\n" +
|
|
48
|
+
" - Updating: use 'opencode-rag update' to install a newer release.\n" +
|
|
44
49
|
" - Troubleshooting: use --check to inspect the runtime, or --force to reinstall.\n" +
|
|
45
|
-
"\nMachine
|
|
50
|
+
"\nMachine + workspace step: run 'opencode-rag init' only when you need to re-sync\nan already initialized workspace.\n")
|
|
46
51
|
.option("--uninstall", "remove the runtime and cleanup")
|
|
47
52
|
.option("-f, --force", "force re-setup even if up-to-date")
|
|
48
53
|
.option("--check", "check whether the runtime is correctly installed")
|
|
@@ -103,19 +108,16 @@ export function registerSetupCommand(program) {
|
|
|
103
108
|
const runtimeDist = path.join(runtimePluginDir, "dist");
|
|
104
109
|
const alreadyInstalled = existsSync(runtimeDist);
|
|
105
110
|
if (alreadyInstalled && installedVersion === pluginVersion && !options.force) {
|
|
106
|
-
console.log(` ${c.success("Already up-to-date.")} (${c.value(pluginVersion)}) at ${c.file(runtimeDir)}
|
|
107
|
-
console.log(` ${c.dim("Run `opencode-rag setup --force` to re-install
|
|
108
|
-
|
|
111
|
+
console.log(` ${c.success("Already up-to-date.")} (${c.value(pluginVersion)}) at ${c.file(runtimeDir)}`);
|
|
112
|
+
console.log(` ${c.dim("Run `opencode-rag setup --force` to re-install the runtime.\n")}`);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
await ensureUpToDate();
|
|
109
116
|
}
|
|
110
117
|
const result = await setupRuntime({ force: options.force, version: pluginVersion });
|
|
111
118
|
if (result.success) {
|
|
112
119
|
console.log(` ${c.created("Updated:")} runtime at ${c.file(runtimeDir)}`);
|
|
113
120
|
console.log(` ${c.created("Version:")} ${c.value(pluginVersion)}`);
|
|
114
|
-
console.log(`\n${c.success("Setup complete.")}`);
|
|
115
|
-
console.log(`\n ${c.dim("Next steps:")}`);
|
|
116
|
-
console.log(` ${c.dim(" 1. Restart OpenCode if it is running")}`);
|
|
117
|
-
console.log(` ${c.dim(" 2. Run `opencode-rag init` in each workspace")}`);
|
|
118
|
-
console.log(` ${c.dim(" 3. Run `opencode-rag index` to build the search index")}`);
|
|
119
121
|
}
|
|
120
122
|
else {
|
|
121
123
|
for (const err of result.errors) {
|
|
@@ -124,8 +126,78 @@ export function registerSetupCommand(program) {
|
|
|
124
126
|
console.error(`\n ${c.error("Setup failed. Please check the errors above or run with --force.\n")}`);
|
|
125
127
|
process.exit(1);
|
|
126
128
|
}
|
|
129
|
+
// --- workspace step: automatically initialize the current workspace ---
|
|
130
|
+
const cwd = process.cwd();
|
|
131
|
+
console.log(`\n${c.heading("Workspace")}`);
|
|
132
|
+
if (isLikelyProjectRoot(cwd)) {
|
|
133
|
+
if (isWorkspaceInitialized(cwd)) {
|
|
134
|
+
console.log(` ${c.exists("Exists:")} OpenCodeRAG already initialized in ${c.file(cwd)}`);
|
|
135
|
+
console.log(` ${c.dim("Run `opencode-rag init` to re-sync workspace files when needed.\n")}`);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
await runWorkspaceInit({});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
console.log(` ${c.dim("Current directory is not a project root - skipping workspace initialization.")}`);
|
|
143
|
+
console.log(` ${c.dim("Run `opencode-rag init` inside the workspace when needed.\n")}`);
|
|
144
|
+
}
|
|
145
|
+
console.log(`\n${c.success("Setup complete.")}`);
|
|
146
|
+
console.log(`\n ${c.dim("Next steps:")}`);
|
|
147
|
+
console.log(` ${c.dim(" 1. Restart OpenCode if it is running")}`);
|
|
148
|
+
console.log(` ${c.dim(" 2. Run `opencode-rag index` to build the search index")}`);
|
|
127
149
|
checkOpenCodeRunning();
|
|
128
150
|
console.log();
|
|
129
151
|
});
|
|
130
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Self-update: when this OpenCodeRAG build is outdated compared to the latest
|
|
155
|
+
* version published on npm, fetch and install the new version of itself.
|
|
156
|
+
*
|
|
157
|
+
* This is independent of how/where the package was installed (global npm
|
|
158
|
+
* prefix, runtime cache junction, or local development link) - the comparison
|
|
159
|
+
* is purely between the running version and the published version. A
|
|
160
|
+
* development build that is newer than the published release is left alone.
|
|
161
|
+
*/
|
|
162
|
+
async function ensureUpToDate() {
|
|
163
|
+
const current = getCurrentVersion();
|
|
164
|
+
const latest = getLatestNpmVersion();
|
|
165
|
+
if (!latest) {
|
|
166
|
+
console.log(` ${c.dim("Could not check npm for the latest version (offline?).")}`);
|
|
167
|
+
console.log(` ${c.dim("Run `opencode-rag update` later to upgrade.\n")}`);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (compareVersions(latest, current) <= 0) {
|
|
171
|
+
console.log(` ${c.dim(`No update needed (local v${current}, npm v${latest}).`)}\n`);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
console.log(` ${c.warn("New version available:")} ${c.value(`v${current}`)} ${c.dim("->")} ${c.value(`v${latest}`)}`);
|
|
175
|
+
console.log(` ${c.dim("Fetching and installing it...\n")}`);
|
|
176
|
+
const updated = await installLatestUpdate({ verbose: false });
|
|
177
|
+
if (!updated.success) {
|
|
178
|
+
console.error(` ${c.error("✗-")} ${updated.message}`);
|
|
179
|
+
console.error(` ${c.dim("You can retry later with `opencode-rag update`.\n")}`);
|
|
180
|
+
process.exit(1);
|
|
181
|
+
}
|
|
182
|
+
console.log(` ${c.success("✓")} ${updated.message}`);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Query the npm registry for the latest published version of the plugin.
|
|
186
|
+
*
|
|
187
|
+
* Best-effort: returns `null` when npm is unavailable or the lookup fails.
|
|
188
|
+
*
|
|
189
|
+
* @returns The latest version string, or `null` on failure.
|
|
190
|
+
*/
|
|
191
|
+
function getLatestNpmVersion() {
|
|
192
|
+
try {
|
|
193
|
+
const out = execSync(`npm view ${PLUGIN_NAME} dist-tags.latest`, {
|
|
194
|
+
encoding: "utf-8",
|
|
195
|
+
timeout: 15_000,
|
|
196
|
+
}).trim();
|
|
197
|
+
return out || null;
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
131
203
|
//# sourceMappingURL=setup.js.map
|