opencode-rag-plugin 2.11.2 → 2.12.0
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 +305 -207
- package/dist/cli/commands/backend-detect.d.ts +1 -1
- package/dist/cli/commands/backend-detect.js +1 -1
- package/dist/cli/commands/index.d.ts +0 -1
- package/dist/cli/commands/index.js +0 -1
- package/dist/cli/commands/setup.js +271 -85
- package/dist/cli/commands/{init-helpers.d.ts → workspace-helpers.d.ts} +10 -7
- package/dist/cli/commands/{init-helpers.js → workspace-helpers.js} +12 -62
- package/dist/cli/index.js +1 -2
- package/dist/cli/prompt.d.ts +84 -0
- package/dist/cli/prompt.js +247 -0
- package/dist/cli/types.d.ts +16 -17
- package/dist/cli/wizard/apply.d.ts +58 -0
- package/dist/cli/wizard/apply.js +234 -0
- package/dist/cli/wizard/flow.d.ts +83 -0
- package/dist/cli/wizard/flow.js +759 -0
- package/dist/cli/wizard/model-catalog.d.ts +80 -0
- package/dist/cli/wizard/model-catalog.js +414 -0
- package/dist/cli/wizard/types.d.ts +94 -0
- package/dist/cli/wizard/types.js +5 -0
- package/dist/cli/workspace.d.ts +40 -0
- package/dist/cli/workspace.js +322 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1 -1
- package/dist/core/config.d.ts +2 -2
- package/dist/core/config.js +2 -2
- package/dist/core/provider-defaults.d.ts +2 -0
- package/dist/core/provider-defaults.js +4 -0
- package/dist/eval/fast-index.js +1 -1
- package/dist/opencode/system-guidance.d.ts +1 -1
- package/dist/opencode/system-guidance.js +1 -1
- package/dist/tui.d.ts +11 -0
- package/dist/tui.js +12 -1
- package/package.json +1 -1
- package/dist/cli/commands/init.d.ts +0 -51
- package/dist/cli/commands/init.js +0 -508
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Minimal interactive prompt layer for the `setup` wizard.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately dependency-free: the CLI only needs text input, masked input,
|
|
5
|
+
* yes/no confirmations and a numbered list selection. `@inquirer/prompts`
|
|
6
|
+
* would add a runtime dependency tree for a single command, so this module
|
|
7
|
+
* implements the small subset on top of `node:readline`.
|
|
8
|
+
*
|
|
9
|
+
* Every prompt resolves `undefined` when the user aborts (Ctrl+C, Ctrl+D or a
|
|
10
|
+
* closing stdin). Callers must treat `undefined` as "cancel the wizard" and
|
|
11
|
+
* leave the configuration untouched.
|
|
12
|
+
*
|
|
13
|
+
* Non-TTY safety: {@link createPrompter} returns a non-interactive prompter
|
|
14
|
+
* when stdin is not a TTY, so a piped/CI invocation can never block forever.
|
|
15
|
+
*/
|
|
16
|
+
/** One selectable entry in {@link Prompter.select}. */
|
|
17
|
+
export interface SelectOption<T> {
|
|
18
|
+
/** Text shown to the user. */
|
|
19
|
+
label: string;
|
|
20
|
+
/** Value returned when the entry is picked. */
|
|
21
|
+
value: T;
|
|
22
|
+
/** Optional dimmed second line shown under the label. */
|
|
23
|
+
description?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Options for {@link Prompter.text}. */
|
|
26
|
+
export interface TextPromptOptions {
|
|
27
|
+
/** Question shown to the user. */
|
|
28
|
+
message: string;
|
|
29
|
+
/** Value returned when the user presses Enter without typing anything. */
|
|
30
|
+
defaultValue?: string;
|
|
31
|
+
/** Hint printed under the question (e.g. an example). */
|
|
32
|
+
hint?: string;
|
|
33
|
+
/** Return an error message to re-ask, or `undefined` when the value is valid. */
|
|
34
|
+
validate?: (value: string) => string | undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Options for {@link Prompter.confirm}. */
|
|
37
|
+
export interface ConfirmPromptOptions {
|
|
38
|
+
/** Question shown to the user. */
|
|
39
|
+
message: string;
|
|
40
|
+
/** Answer used when the user presses Enter without typing anything. Defaults to `true`. */
|
|
41
|
+
defaultValue?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Options for {@link Prompter.select}. */
|
|
44
|
+
export interface SelectPromptOptions<T> {
|
|
45
|
+
/** Question shown to the user. */
|
|
46
|
+
message: string;
|
|
47
|
+
/** Entries to choose from. */
|
|
48
|
+
options: readonly SelectOption<T>[];
|
|
49
|
+
/** Index selected when the user presses Enter without typing anything. Defaults to the first entry. */
|
|
50
|
+
defaultIndex?: number;
|
|
51
|
+
/** Entries per page. Larger lists are paged with `n`/`p`. Defaults to 15. */
|
|
52
|
+
pageSize?: number;
|
|
53
|
+
/** Extra hint printed above the list. */
|
|
54
|
+
hint?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The prompt surface used by the setup wizard.
|
|
58
|
+
*
|
|
59
|
+
* All methods resolve `undefined` when the user cancels; a canceled wizard
|
|
60
|
+
* must not write any configuration.
|
|
61
|
+
*/
|
|
62
|
+
export interface Prompter {
|
|
63
|
+
/** Whether this prompter can actually interact with a user. */
|
|
64
|
+
readonly interactive: boolean;
|
|
65
|
+
/** Ask for a line of text. */
|
|
66
|
+
text(options: TextPromptOptions): Promise<string | undefined>;
|
|
67
|
+
/** Ask for a secret (input is masked on a TTY). */
|
|
68
|
+
password(message: string): Promise<string | undefined>;
|
|
69
|
+
/** Ask a yes/no question. */
|
|
70
|
+
confirm(options: ConfirmPromptOptions): Promise<boolean | undefined>;
|
|
71
|
+
/** Pick one of a list of entries (numbered menu, `n`/`p` to page). */
|
|
72
|
+
select<T>(options: SelectPromptOptions<T>): Promise<T | undefined>;
|
|
73
|
+
/** Release any resources held by the prompter. Safe to call twice. */
|
|
74
|
+
close(): void;
|
|
75
|
+
}
|
|
76
|
+
/** Whether the process is attached to an interactive terminal. */
|
|
77
|
+
export declare function isInteractiveTerminal(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Create a prompter for the current process.
|
|
80
|
+
*
|
|
81
|
+
* When `interactive` is false (or stdin is not a TTY), a non-interactive
|
|
82
|
+
* prompter is returned — the wizard then applies defaults instead of hanging.
|
|
83
|
+
*/
|
|
84
|
+
export declare function createPrompter(interactive?: boolean): Prompter;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Minimal interactive prompt layer for the `setup` wizard.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately dependency-free: the CLI only needs text input, masked input,
|
|
5
|
+
* yes/no confirmations and a numbered list selection. `@inquirer/prompts`
|
|
6
|
+
* would add a runtime dependency tree for a single command, so this module
|
|
7
|
+
* implements the small subset on top of `node:readline`.
|
|
8
|
+
*
|
|
9
|
+
* Every prompt resolves `undefined` when the user aborts (Ctrl+C, Ctrl+D or a
|
|
10
|
+
* closing stdin). Callers must treat `undefined` as "cancel the wizard" and
|
|
11
|
+
* leave the configuration untouched.
|
|
12
|
+
*
|
|
13
|
+
* Non-TTY safety: {@link createPrompter} returns a non-interactive prompter
|
|
14
|
+
* when stdin is not a TTY, so a piped/CI invocation can never block forever.
|
|
15
|
+
*/
|
|
16
|
+
import { createInterface } from "node:readline";
|
|
17
|
+
/** Whether the process is attached to an interactive terminal. */
|
|
18
|
+
export function isInteractiveTerminal() {
|
|
19
|
+
return Boolean(process.stdin.isTTY) && process.stdout.isTTY !== false;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A prompter for non-interactive runs.
|
|
23
|
+
*
|
|
24
|
+
* It never reads stdin; `text`/`select`/`password` resolve the supplied
|
|
25
|
+
* default (or `undefined` when there is none) and `confirm` resolves its
|
|
26
|
+
* default. `interactive` is `false` so callers can skip the wizard entirely.
|
|
27
|
+
*/
|
|
28
|
+
class NonInteractivePrompter {
|
|
29
|
+
interactive = false;
|
|
30
|
+
async text(options) {
|
|
31
|
+
return options.defaultValue;
|
|
32
|
+
}
|
|
33
|
+
async password() {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
async confirm(options) {
|
|
37
|
+
return options.defaultValue ?? true;
|
|
38
|
+
}
|
|
39
|
+
async select(options) {
|
|
40
|
+
const index = options.defaultIndex ?? 0;
|
|
41
|
+
return options.options[index]?.value;
|
|
42
|
+
}
|
|
43
|
+
close() {
|
|
44
|
+
// Nothing to release.
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Write a line to stdout, tolerating a closed/broken pipe.
|
|
49
|
+
*
|
|
50
|
+
* The wizard must not crash when stdout is redirected to a closed pager.
|
|
51
|
+
*/
|
|
52
|
+
function writeLine(output, line) {
|
|
53
|
+
try {
|
|
54
|
+
output.write(line);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Ignore EPIPE — the caller is already gone.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Prompt implementation backed by `node:readline`.
|
|
62
|
+
*
|
|
63
|
+
* A readline interface is created lazily for each text/select prompt and
|
|
64
|
+
* closed in a `finally` block; this keeps cancellation (SIGINT) and terminal
|
|
65
|
+
* state predictable even when the user aborts halfway through the wizard.
|
|
66
|
+
*/
|
|
67
|
+
class ReadlinePrompter {
|
|
68
|
+
input;
|
|
69
|
+
output;
|
|
70
|
+
interactive = true;
|
|
71
|
+
constructor(input, output) {
|
|
72
|
+
this.input = input;
|
|
73
|
+
this.output = output;
|
|
74
|
+
}
|
|
75
|
+
/** Ask one line, resolving `undefined` on abort. */
|
|
76
|
+
ask(question) {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
const rl = createInterface({ input: this.input, output: this.output, terminal: true });
|
|
79
|
+
let settled = false;
|
|
80
|
+
const finish = (value) => {
|
|
81
|
+
if (settled)
|
|
82
|
+
return;
|
|
83
|
+
settled = true;
|
|
84
|
+
rl.close();
|
|
85
|
+
resolve(value);
|
|
86
|
+
};
|
|
87
|
+
rl.once("SIGINT", () => {
|
|
88
|
+
writeLine(this.output, "\n");
|
|
89
|
+
finish(undefined);
|
|
90
|
+
});
|
|
91
|
+
rl.once("close", () => finish(undefined));
|
|
92
|
+
rl.question(question, (answer) => finish(answer));
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async text(options) {
|
|
96
|
+
const suffix = options.defaultValue !== undefined && options.defaultValue !== ""
|
|
97
|
+
? ` [${options.defaultValue}]`
|
|
98
|
+
: "";
|
|
99
|
+
if (options.hint)
|
|
100
|
+
writeLine(this.output, ` ${options.hint}\n`);
|
|
101
|
+
for (;;) {
|
|
102
|
+
const answer = await this.ask(` ${options.message}${suffix}: `);
|
|
103
|
+
if (answer === undefined)
|
|
104
|
+
return undefined;
|
|
105
|
+
const value = answer.trim() === "" ? (options.defaultValue ?? "") : answer.trim();
|
|
106
|
+
const error = options.validate?.(value);
|
|
107
|
+
if (error) {
|
|
108
|
+
writeLine(this.output, ` ${error}\n`);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async password(message) {
|
|
115
|
+
if (!this.input.isTTY || typeof this.input.setRawMode !== "function") {
|
|
116
|
+
// Piped stdin: read a plain line (still masked in the transcript by the caller).
|
|
117
|
+
return this.ask(` ${message}: `);
|
|
118
|
+
}
|
|
119
|
+
writeLine(this.output, ` ${message}: `);
|
|
120
|
+
this.input.setRawMode(true);
|
|
121
|
+
this.input.resume();
|
|
122
|
+
return new Promise((resolve) => {
|
|
123
|
+
let value = "";
|
|
124
|
+
const cleanup = () => {
|
|
125
|
+
this.input.removeListener("data", onData);
|
|
126
|
+
this.input.setRawMode(false);
|
|
127
|
+
this.input.pause();
|
|
128
|
+
writeLine(this.output, "\n");
|
|
129
|
+
};
|
|
130
|
+
const onData = (chunk) => {
|
|
131
|
+
const text = chunk.toString("utf8");
|
|
132
|
+
for (const char of text) {
|
|
133
|
+
if (char === "\u0003") {
|
|
134
|
+
cleanup();
|
|
135
|
+
resolve(undefined);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (char === "\r" || char === "\n") {
|
|
139
|
+
cleanup();
|
|
140
|
+
resolve(value);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (char === "\u007f" || char === "\b") {
|
|
144
|
+
if (value.length > 0) {
|
|
145
|
+
value = value.slice(0, -1);
|
|
146
|
+
writeLine(this.output, "\b \b");
|
|
147
|
+
}
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (char >= " ") {
|
|
151
|
+
value += char;
|
|
152
|
+
writeLine(this.output, "*");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
this.input.on("data", onData);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
async confirm(options) {
|
|
160
|
+
const hint = options.defaultValue === false ? "y/N" : "Y/n";
|
|
161
|
+
for (;;) {
|
|
162
|
+
const answer = await this.ask(` ${options.message} (${hint}): `);
|
|
163
|
+
if (answer === undefined)
|
|
164
|
+
return undefined;
|
|
165
|
+
const normalized = answer.trim().toLowerCase();
|
|
166
|
+
if (normalized === "")
|
|
167
|
+
return options.defaultValue ?? true;
|
|
168
|
+
if (normalized === "y" || normalized === "yes")
|
|
169
|
+
return true;
|
|
170
|
+
if (normalized === "n" || normalized === "no")
|
|
171
|
+
return false;
|
|
172
|
+
writeLine(this.output, " Please answer y or n.\n");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
async select(options) {
|
|
176
|
+
const all = options.options;
|
|
177
|
+
if (all.length === 0)
|
|
178
|
+
return undefined;
|
|
179
|
+
const pageSize = Math.max(1, options.pageSize ?? 15);
|
|
180
|
+
const defaultIndex = Math.min(Math.max(options.defaultIndex ?? 0, 0), all.length - 1);
|
|
181
|
+
let start = Math.floor(defaultIndex / pageSize) * pageSize;
|
|
182
|
+
if (options.hint)
|
|
183
|
+
writeLine(this.output, ` ${options.hint}\n`);
|
|
184
|
+
for (;;) {
|
|
185
|
+
const end = Math.min(start + pageSize, all.length);
|
|
186
|
+
writeLine(this.output, "");
|
|
187
|
+
for (let i = start; i < end; i += 1) {
|
|
188
|
+
const entry = all[i];
|
|
189
|
+
if (!entry)
|
|
190
|
+
continue;
|
|
191
|
+
const marker = i === defaultIndex ? "*" : " ";
|
|
192
|
+
const label = `${marker} ${i + 1}) ${entry.label}`;
|
|
193
|
+
writeLine(this.output, ` ${label}\n`);
|
|
194
|
+
if (entry.description) {
|
|
195
|
+
writeLine(this.output, ` ${entry.description}\n`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const nav = [];
|
|
199
|
+
if (start > 0)
|
|
200
|
+
nav.push("p = previous page");
|
|
201
|
+
if (end < all.length)
|
|
202
|
+
nav.push("n = next page");
|
|
203
|
+
nav.push("number = choose");
|
|
204
|
+
if (all.length > 1)
|
|
205
|
+
nav.push("Enter = default (*)");
|
|
206
|
+
writeLine(this.output, ` ${nav.join(" · ")}\n`);
|
|
207
|
+
const answer = await this.ask(` ${options.message}: `);
|
|
208
|
+
if (answer === undefined)
|
|
209
|
+
return undefined;
|
|
210
|
+
const value = answer.trim().toLowerCase();
|
|
211
|
+
if (value === "")
|
|
212
|
+
return all[defaultIndex]?.value;
|
|
213
|
+
if (value === "n" && end < all.length) {
|
|
214
|
+
start = Math.min(start + pageSize, all.length - 1);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (value === "p" && start > 0) {
|
|
218
|
+
start = Math.max(0, start - pageSize);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const index = Number.parseInt(value, 10);
|
|
222
|
+
if (Number.isInteger(index) && index >= 1 && index <= all.length) {
|
|
223
|
+
return all[index - 1]?.value;
|
|
224
|
+
}
|
|
225
|
+
// Accept the literal value too (e.g. a model name typed by hand).
|
|
226
|
+
const exact = all.find((entry) => String(entry.value) === answer.trim());
|
|
227
|
+
if (exact !== undefined)
|
|
228
|
+
return exact.value;
|
|
229
|
+
writeLine(this.output, " Not a valid selection.\n");
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
close() {
|
|
233
|
+
// Interfaces are per-prompt; nothing persistent to release.
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Create a prompter for the current process.
|
|
238
|
+
*
|
|
239
|
+
* When `interactive` is false (or stdin is not a TTY), a non-interactive
|
|
240
|
+
* prompter is returned — the wizard then applies defaults instead of hanging.
|
|
241
|
+
*/
|
|
242
|
+
export function createPrompter(interactive = isInteractiveTerminal()) {
|
|
243
|
+
if (!interactive)
|
|
244
|
+
return new NonInteractivePrompter();
|
|
245
|
+
return new ReadlinePrompter(process.stdin, process.stdout);
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=prompt.js.map
|
package/dist/cli/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* @fileoverview Shared CLI option interfaces used by all command modules.
|
|
3
3
|
*/
|
|
4
4
|
/** Shared CLI option interfaces used by all command modules. */
|
|
5
|
+
import type { WizardFlags } from "./wizard/flow.js";
|
|
5
6
|
/** Standard CLI options shared by most commands (index, query, clear, etc.). */
|
|
6
7
|
export interface CliOptions {
|
|
7
8
|
/** Optional path to an `opencode-rag.json` config file. */
|
|
@@ -27,26 +28,24 @@ export interface CliOptions {
|
|
|
27
28
|
/** Optional system prompt to steer an image description toward specific features. */
|
|
28
29
|
systemPrompt?: string;
|
|
29
30
|
}
|
|
30
|
-
/** Options for the `
|
|
31
|
-
export interface
|
|
32
|
-
/**
|
|
31
|
+
/** Options for the `setup` command. */
|
|
32
|
+
export interface SetupOptions extends WizardFlags {
|
|
33
|
+
/** Remove the runtime and clean up. */
|
|
34
|
+
uninstall?: boolean;
|
|
35
|
+
/** Force re-setup even if up-to-date. */
|
|
33
36
|
force?: boolean;
|
|
34
|
-
/**
|
|
37
|
+
/** Only inspect the runtime. */
|
|
38
|
+
check?: boolean;
|
|
39
|
+
/** Re-open the configuration wizard even when a config already exists. */
|
|
40
|
+
reconfigure?: boolean;
|
|
41
|
+
/** Non-interactive: never prompt and keep the existing configuration. */
|
|
42
|
+
yes?: boolean;
|
|
43
|
+
/** Skip linking workspace-local plugin dependencies. */
|
|
35
44
|
skipInstall?: boolean;
|
|
36
|
-
/** Skip provider connectivity and model availability
|
|
45
|
+
/** Skip provider connectivity and model availability checks. */
|
|
37
46
|
skipHealthCheck?: boolean;
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
/** ClickHouse endpoint for `--vector-store clickhouse`. */
|
|
41
|
-
clickhouseUrl?: string;
|
|
42
|
-
/** ClickHouse database for `--vector-store clickhouse`. */
|
|
43
|
-
clickhouseDatabase?: string;
|
|
44
|
-
/** ClickHouse user for `--vector-store clickhouse`. */
|
|
45
|
-
clickhouseUser?: string;
|
|
46
|
-
/** ClickHouse password env var name for `--vector-store clickhouse`. */
|
|
47
|
-
clickhousePasswordEnv?: string;
|
|
48
|
-
/** ClickHouse project namespace (table suffix) for `--vector-store clickhouse`. */
|
|
49
|
-
clickhouseNamespace?: string;
|
|
47
|
+
/** Skip the machine-level runtime install (workspace setup only). */
|
|
48
|
+
skipRuntime?: boolean;
|
|
50
49
|
}
|
|
51
50
|
/** Shape of the `package.json` metadata required by CLI helpers. */
|
|
52
51
|
export interface PackageMetadata {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pure mapping from wizard answers to config patches.
|
|
3
|
+
*
|
|
4
|
+
* Split from the interactive flow so the mapping can be unit-tested without a
|
|
5
|
+
* TTY and reused by the flag-driven (`--yes`) path.
|
|
6
|
+
*
|
|
7
|
+
* Layering: provider/model/baseUrl/apiKey/vectorDimension describe the
|
|
8
|
+
* *machine* and go to the global config file; feature toggles and repo
|
|
9
|
+
* concerns go to the workspace config. This mirrors
|
|
10
|
+
* `isProviderScopedPath()` in `src/core/config.ts` (the TUI's rule).
|
|
11
|
+
*/
|
|
12
|
+
import type { WizardAnswers } from "./types.js";
|
|
13
|
+
/** A two-layer config patch produced from {@link WizardAnswers}. */
|
|
14
|
+
export interface ConfigPatch {
|
|
15
|
+
/** Machine-level keys written to `~/.config/opencode/opencode-rag.json`. */
|
|
16
|
+
global: Record<string, unknown>;
|
|
17
|
+
/** Repository-level keys written to `.opencode/opencode-rag.json`. */
|
|
18
|
+
workspace: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/** Whether a value is a plain object (not `null`, not an array). */
|
|
21
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* Deep-merge `patch` into `base`, returning a new object.
|
|
24
|
+
*
|
|
25
|
+
* Arrays and scalars replace the base value; plain objects merge recursively.
|
|
26
|
+
* `undefined` values are ignored so callers can omit optional keys.
|
|
27
|
+
*/
|
|
28
|
+
export declare function mergeConfigObjects(base: Record<string, unknown>, patch: Record<string, unknown>): Record<string, unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* Build the global and workspace patches for a set of answers.
|
|
31
|
+
*
|
|
32
|
+
* The returned objects are sparse: a section that the user did not touch is
|
|
33
|
+
* absent, so merging them never overwrites unrelated settings.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildConfigPatch(answers: WizardAnswers): ConfigPatch;
|
|
36
|
+
/** Read a JSON object from disk, returning `{}` when the file is absent or invalid. */
|
|
37
|
+
export declare function readJsonObjectSafe(filePath: string): Record<string, unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* Merge a patch into a config file, creating the file when needed.
|
|
40
|
+
*
|
|
41
|
+
* A `.bak` copy is written when the file already exists, so a failed setup
|
|
42
|
+
* cannot leave the user without their previous configuration.
|
|
43
|
+
*
|
|
44
|
+
* @returns `true` when the file was written, `false` when the write failed.
|
|
45
|
+
*/
|
|
46
|
+
export declare function writeConfigPatch(filePath: string, patch: Record<string, unknown>): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Detect provider keys that a workspace config inherited from the old
|
|
49
|
+
* `init` template and that now shadow the machine-global layer.
|
|
50
|
+
*
|
|
51
|
+
* The global file is a base layer, so a stale `embedding.provider`/`baseUrl`/
|
|
52
|
+
* `model` in `.opencode/opencode-rag.json` silently overrides the model the
|
|
53
|
+
* wizard wrote to the global file. Returns the dotted paths that should be
|
|
54
|
+
* removed.
|
|
55
|
+
*/
|
|
56
|
+
export declare function findShadowingProviderKeys(workspaceConfig: Record<string, unknown>): string[][];
|
|
57
|
+
/** Remove the given dotted paths from a config object, in place. */
|
|
58
|
+
export declare function removeConfigPaths(config: Record<string, unknown>, paths: readonly string[][]): void;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pure mapping from wizard answers to config patches.
|
|
3
|
+
*
|
|
4
|
+
* Split from the interactive flow so the mapping can be unit-tested without a
|
|
5
|
+
* TTY and reused by the flag-driven (`--yes`) path.
|
|
6
|
+
*
|
|
7
|
+
* Layering: provider/model/baseUrl/apiKey/vectorDimension describe the
|
|
8
|
+
* *machine* and go to the global config file; feature toggles and repo
|
|
9
|
+
* concerns go to the workspace config. This mirrors
|
|
10
|
+
* `isProviderScopedPath()` in `src/core/config.ts` (the TUI's rule).
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, copyFileSync } from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
/** Whether a value is a plain object (not `null`, not an array). */
|
|
15
|
+
export function isPlainObject(value) {
|
|
16
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Deep-merge `patch` into `base`, returning a new object.
|
|
20
|
+
*
|
|
21
|
+
* Arrays and scalars replace the base value; plain objects merge recursively.
|
|
22
|
+
* `undefined` values are ignored so callers can omit optional keys.
|
|
23
|
+
*/
|
|
24
|
+
export function mergeConfigObjects(base, patch) {
|
|
25
|
+
const next = { ...base };
|
|
26
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
27
|
+
if (value === undefined)
|
|
28
|
+
continue;
|
|
29
|
+
const existing = next[key];
|
|
30
|
+
if (isPlainObject(existing) && isPlainObject(value)) {
|
|
31
|
+
next[key] = mergeConfigObjects(existing, value);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
next[key] = value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return next;
|
|
38
|
+
}
|
|
39
|
+
/** Drop `undefined` entries from a record (one level deep). */
|
|
40
|
+
function defined(value) {
|
|
41
|
+
const next = {};
|
|
42
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
43
|
+
if (entry !== undefined)
|
|
44
|
+
next[key] = entry;
|
|
45
|
+
}
|
|
46
|
+
return next;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Build the global and workspace patches for a set of answers.
|
|
50
|
+
*
|
|
51
|
+
* The returned objects are sparse: a section that the user did not touch is
|
|
52
|
+
* absent, so merging them never overwrites unrelated settings.
|
|
53
|
+
*/
|
|
54
|
+
export function buildConfigPatch(answers) {
|
|
55
|
+
const global = {
|
|
56
|
+
embedding: defined({
|
|
57
|
+
provider: answers.embedding.provider,
|
|
58
|
+
baseUrl: answers.embedding.baseUrl,
|
|
59
|
+
model: answers.embedding.model,
|
|
60
|
+
apiKey: answers.embedding.apiKey,
|
|
61
|
+
vectorDimension: answers.embedding.vectorDimension,
|
|
62
|
+
}),
|
|
63
|
+
};
|
|
64
|
+
if (answers.description) {
|
|
65
|
+
global.description = defined({
|
|
66
|
+
provider: answers.description.provider,
|
|
67
|
+
baseUrl: answers.description.baseUrl,
|
|
68
|
+
model: answers.description.model,
|
|
69
|
+
apiKey: answers.description.apiKey,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (answers.imageDescription) {
|
|
73
|
+
global.imageDescription = defined({
|
|
74
|
+
provider: answers.imageDescription.provider,
|
|
75
|
+
baseUrl: answers.imageDescription.baseUrl,
|
|
76
|
+
model: answers.imageDescription.model,
|
|
77
|
+
apiKey: answers.imageDescription.apiKey,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (answers.decision?.enabled) {
|
|
81
|
+
global.decision = defined({
|
|
82
|
+
provider: "ollama",
|
|
83
|
+
baseUrl: answers.decision.baseUrl,
|
|
84
|
+
model: answers.decision.model,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (answers.reranking?.enabled) {
|
|
88
|
+
global.reranking = defined({
|
|
89
|
+
provider: "llama-server",
|
|
90
|
+
baseUrl: answers.reranking.baseUrl,
|
|
91
|
+
model: answers.reranking.model,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const workspace = {};
|
|
95
|
+
if (answers.description !== undefined || answers.descriptionEnabled !== undefined) {
|
|
96
|
+
workspace.description = { enabled: answers.descriptionEnabled ?? Boolean(answers.description) };
|
|
97
|
+
}
|
|
98
|
+
if (answers.imageDescription !== undefined || answers.imageDescriptionEnabled !== undefined) {
|
|
99
|
+
workspace.imageDescription = { enabled: answers.imageDescriptionEnabled ?? Boolean(answers.imageDescription) };
|
|
100
|
+
}
|
|
101
|
+
if (answers.decision !== undefined) {
|
|
102
|
+
workspace.decision = defined({
|
|
103
|
+
enabled: answers.decision.enabled,
|
|
104
|
+
routeBeforeAsking: answers.decision.routeBeforeAsking,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (answers.reranking !== undefined) {
|
|
108
|
+
workspace.reranking = { enabled: answers.reranking.enabled };
|
|
109
|
+
}
|
|
110
|
+
if (answers.shellGuard) {
|
|
111
|
+
workspace.shellGuard = { enabled: answers.shellGuard.enabled };
|
|
112
|
+
}
|
|
113
|
+
if (answers.memory) {
|
|
114
|
+
workspace.memory = defined({
|
|
115
|
+
passiveCapture: answers.memory.passiveCapture,
|
|
116
|
+
promptEnforcement: answers.memory.promptEnforcement,
|
|
117
|
+
sessionEndExtraction: answers.memory.sessionEndExtraction,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (answers.vectorStore) {
|
|
121
|
+
workspace.vectorStore = defined({
|
|
122
|
+
provider: answers.vectorStore.provider,
|
|
123
|
+
clickhouse: answers.vectorStore.clickhouse
|
|
124
|
+
? defined({
|
|
125
|
+
url: answers.vectorStore.clickhouse.url,
|
|
126
|
+
database: answers.vectorStore.clickhouse.database,
|
|
127
|
+
username: answers.vectorStore.clickhouse.username,
|
|
128
|
+
passwordEnv: answers.vectorStore.clickhouse.passwordEnv,
|
|
129
|
+
namespace: answers.vectorStore.clickhouse.namespace,
|
|
130
|
+
})
|
|
131
|
+
: undefined,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
if (answers.indexing) {
|
|
135
|
+
workspace.indexing = defined({ embedDescriptions: answers.indexing.embedDescriptions });
|
|
136
|
+
}
|
|
137
|
+
if (answers.openCode) {
|
|
138
|
+
workspace.openCode = defined({
|
|
139
|
+
autoIndex: answers.openCode.autoIndex === undefined ? undefined : { enabled: answers.openCode.autoIndex },
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return { global, workspace };
|
|
143
|
+
}
|
|
144
|
+
/** Read a JSON object from disk, returning `{}` when the file is absent or invalid. */
|
|
145
|
+
export function readJsonObjectSafe(filePath) {
|
|
146
|
+
if (!existsSync(filePath))
|
|
147
|
+
return {};
|
|
148
|
+
try {
|
|
149
|
+
const raw = readFileSync(filePath, "utf-8");
|
|
150
|
+
const parsed = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
151
|
+
const value = JSON.parse(parsed);
|
|
152
|
+
return isPlainObject(value) ? value : {};
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Merge a patch into a config file, creating the file when needed.
|
|
160
|
+
*
|
|
161
|
+
* A `.bak` copy is written when the file already exists, so a failed setup
|
|
162
|
+
* cannot leave the user without their previous configuration.
|
|
163
|
+
*
|
|
164
|
+
* @returns `true` when the file was written, `false` when the write failed.
|
|
165
|
+
*/
|
|
166
|
+
export function writeConfigPatch(filePath, patch) {
|
|
167
|
+
if (Object.keys(patch).length === 0)
|
|
168
|
+
return false;
|
|
169
|
+
const existed = existsSync(filePath);
|
|
170
|
+
const base = readJsonObjectSafe(filePath);
|
|
171
|
+
const merged = mergeConfigObjects(base, patch);
|
|
172
|
+
try {
|
|
173
|
+
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
174
|
+
const changed = JSON.stringify(merged) !== JSON.stringify(base);
|
|
175
|
+
if (changed) {
|
|
176
|
+
if (existed) {
|
|
177
|
+
try {
|
|
178
|
+
copyFileSync(filePath, `${filePath}.bak`);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// A missing backup must not block the write.
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
writeFileSync(filePath, `${JSON.stringify(merged, null, 2)}\n`, "utf-8");
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Detect provider keys that a workspace config inherited from the old
|
|
194
|
+
* `init` template and that now shadow the machine-global layer.
|
|
195
|
+
*
|
|
196
|
+
* The global file is a base layer, so a stale `embedding.provider`/`baseUrl`/
|
|
197
|
+
* `model` in `.opencode/opencode-rag.json` silently overrides the model the
|
|
198
|
+
* wizard wrote to the global file. Returns the dotted paths that should be
|
|
199
|
+
* removed.
|
|
200
|
+
*/
|
|
201
|
+
export function findShadowingProviderKeys(workspaceConfig) {
|
|
202
|
+
const sections = ["embedding", "description", "imageDescription", "decision", "reranking"];
|
|
203
|
+
const leaves = ["provider", "model", "baseUrl", "apiKey", "vectorDimension"];
|
|
204
|
+
const found = [];
|
|
205
|
+
for (const section of sections) {
|
|
206
|
+
const value = workspaceConfig[section];
|
|
207
|
+
if (!isPlainObject(value))
|
|
208
|
+
continue;
|
|
209
|
+
for (const leaf of leaves) {
|
|
210
|
+
if (value[leaf] !== undefined)
|
|
211
|
+
found.push([section, leaf]);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return found;
|
|
215
|
+
}
|
|
216
|
+
/** Remove the given dotted paths from a config object, in place. */
|
|
217
|
+
export function removeConfigPaths(config, paths) {
|
|
218
|
+
for (const dotted of paths) {
|
|
219
|
+
let cursor = config;
|
|
220
|
+
for (let i = 0; i < dotted.length - 1; i += 1) {
|
|
221
|
+
const segment = dotted[i];
|
|
222
|
+
if (!segment)
|
|
223
|
+
break;
|
|
224
|
+
const next = cursor[segment];
|
|
225
|
+
cursor = isPlainObject(next) ? next : undefined;
|
|
226
|
+
if (!cursor)
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
const leaf = dotted[dotted.length - 1];
|
|
230
|
+
if (cursor && leaf)
|
|
231
|
+
delete cursor[leaf];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=apply.js.map
|