opencode-rag-plugin 2.11.2 → 2.12.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 +386 -222
- 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 +344 -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} +15 -62
- package/dist/cli/index.js +1 -2
- package/dist/cli/ollama.d.ts +12 -0
- package/dist/cli/ollama.js +26 -0
- package/dist/cli/prompt.d.ts +121 -0
- package/dist/cli/prompt.js +309 -0
- package/dist/cli/types.d.ts +31 -19
- package/dist/cli/wizard/apply.d.ts +60 -0
- package/dist/cli/wizard/apply.js +243 -0
- package/dist/cli/wizard/flow.d.ts +121 -0
- package/dist/cli/wizard/flow.js +904 -0
- package/dist/cli/wizard/model-catalog.d.ts +80 -0
- package/dist/cli/wizard/model-catalog.js +414 -0
- package/dist/cli/wizard/presets.d.ts +55 -0
- package/dist/cli/wizard/presets.js +70 -0
- package/dist/cli/wizard/repair.d.ts +54 -0
- package/dist/cli/wizard/repair.js +266 -0
- package/dist/cli/wizard/types.d.ts +99 -0
- package/dist/cli/wizard/types.js +5 -0
- package/dist/cli/workspace.d.ts +57 -0
- package/dist/cli/workspace.js +407 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1 -1
- package/dist/core/auto-scaffold.d.ts +21 -0
- package/dist/core/auto-scaffold.js +80 -0
- package/dist/core/config.d.ts +13 -2
- package/dist/core/config.js +13 -3
- package/dist/core/global-plugin.d.ts +48 -0
- package/dist/core/global-plugin.js +114 -0
- package/dist/core/provider-defaults.d.ts +2 -0
- package/dist/core/provider-defaults.js +4 -0
- package/dist/core/setup-runtime.js +40 -26
- package/dist/embedder/health.js +44 -2
- 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/dist/v2/v2-adapter.js +9 -0
- package/package.json +3 -2
- package/scripts/postinstall-setup.js +16 -0
- package/dist/cli/commands/init.d.ts +0 -51
- package/dist/cli/commands/init.js +0 -508
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview Helper functions for
|
|
2
|
+
* @fileoverview Helper functions for `opencode-rag setup` — file generation, config building, dependency linking, and gitignore merging.
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* Helper functions for the `
|
|
5
|
+
* Helper functions for the `setup` command — file generation, config building,
|
|
6
6
|
* dependency installation, and gitignore merging.
|
|
7
7
|
*/
|
|
8
8
|
import path from "node:path";
|
|
@@ -514,7 +514,7 @@ export async function installPluginFromGlobal(opencodeDir, packageName, skipInst
|
|
|
514
514
|
}
|
|
515
515
|
// Create directory junction from workspace → global runtime.
|
|
516
516
|
// Junction creation can throw (EPERM, cross-drive on Windows) — fall back
|
|
517
|
-
// to a copy instead of aborting `
|
|
517
|
+
// to a copy instead of aborting `setup`.
|
|
518
518
|
console.log(` ${c.created("Linking:")} ${packageName} from global cache...`);
|
|
519
519
|
mkdirSync(path.dirname(workspaceTarget), { recursive: true });
|
|
520
520
|
try {
|
|
@@ -545,61 +545,20 @@ export async function installPluginFromGlobal(opencodeDir, packageName, skipInst
|
|
|
545
545
|
}
|
|
546
546
|
}
|
|
547
547
|
/**
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
* Pick the identity-critical `embedding` keys out of an existing config file so
|
|
557
|
-
* the generated defaults can re-apply them. Malformed input simply yields an
|
|
558
|
-
* empty set (the pure default template).
|
|
559
|
-
*/
|
|
560
|
-
function preservedEmbeddingKeys(existingConfigRaw) {
|
|
561
|
-
if (!existingConfigRaw)
|
|
562
|
-
return {};
|
|
563
|
-
try {
|
|
564
|
-
const raw = existingConfigRaw.charCodeAt(0) === 0xfeff
|
|
565
|
-
? existingConfigRaw.slice(1)
|
|
566
|
-
: existingConfigRaw;
|
|
567
|
-
const parsed = JSON.parse(raw);
|
|
568
|
-
const embedding = parsed?.embedding;
|
|
569
|
-
if (!embedding || typeof embedding !== "object")
|
|
570
|
-
return {};
|
|
571
|
-
const preserved = {};
|
|
572
|
-
for (const key of PRESERVED_EMBEDDING_KEYS) {
|
|
573
|
-
const value = embedding[key];
|
|
574
|
-
if (value !== undefined && value !== null && value !== "") {
|
|
575
|
-
preserved[key] = value;
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
return preserved;
|
|
579
|
-
}
|
|
580
|
-
catch {
|
|
581
|
-
return {};
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* Generate the default `opencode-rag.json` configuration content.
|
|
548
|
+
* Generate the default workspace `opencode-rag.json` configuration content.
|
|
549
|
+
*
|
|
550
|
+
* Provider-scoped keys (`provider`, `model`, `baseUrl`, `apiKey`,
|
|
551
|
+
* `vectorDimension`) are deliberately NOT emitted: they describe the machine
|
|
552
|
+
* and live in the global config, which is the BASE layer. A workspace copy
|
|
553
|
+
* would shadow the global choice (and could commit an API key), so only
|
|
554
|
+
* repository-scoped keys and feature toggles are written here.
|
|
586
555
|
*
|
|
587
556
|
* @param tuning - Optional embedding batch tuning (auto-detected from the
|
|
588
557
|
* Ollama backend). Falls back to `DEFAULT_CONFIG` for any omitted field.
|
|
589
|
-
* @param existingConfigRaw - Optional raw text of the config file that is about
|
|
590
|
-
* to be replaced. Its `apiKey` and `vectorDimension` are carried over: a
|
|
591
|
-
* reset must not silently change the store schema or drop provider auth.
|
|
592
558
|
* @returns A pretty-printed JSON string with all default configuration values.
|
|
593
559
|
*/
|
|
594
|
-
export function generateDefaultConfigJson(tuning
|
|
560
|
+
export function generateDefaultConfigJson(tuning) {
|
|
595
561
|
return JSON.stringify({
|
|
596
|
-
embedding: {
|
|
597
|
-
provider: DEFAULT_CONFIG.embedding.provider,
|
|
598
|
-
baseUrl: DEFAULT_CONFIG.embedding.baseUrl,
|
|
599
|
-
model: DEFAULT_CONFIG.embedding.model,
|
|
600
|
-
timeoutMs: DEFAULT_CONFIG.embedding.timeoutMs,
|
|
601
|
-
...preservedEmbeddingKeys(existingConfigRaw),
|
|
602
|
-
},
|
|
603
562
|
indexing: {
|
|
604
563
|
includeExtensions: DEFAULT_CONFIG.indexing.includeExtensions,
|
|
605
564
|
excludeDirs: DEFAULT_CONFIG.indexing.excludeDirs,
|
|
@@ -641,18 +600,12 @@ export function generateDefaultConfigJson(tuning, existingConfigRaw) {
|
|
|
641
600
|
},
|
|
642
601
|
imageDescription: {
|
|
643
602
|
enabled: DEFAULT_CONFIG.imageDescription.enabled,
|
|
644
|
-
provider: DEFAULT_CONFIG.imageDescription.provider,
|
|
645
|
-
model: DEFAULT_CONFIG.imageDescription.model,
|
|
646
|
-
baseUrl: DEFAULT_CONFIG.imageDescription.baseUrl,
|
|
647
603
|
timeoutMs: DEFAULT_CONFIG.imageDescription.timeoutMs,
|
|
648
604
|
think: DEFAULT_CONFIG.imageDescription.think,
|
|
649
605
|
numCtx: DEFAULT_CONFIG.imageDescription.numCtx,
|
|
650
606
|
},
|
|
651
607
|
decision: {
|
|
652
608
|
enabled: DEFAULT_CONFIG.decision.enabled,
|
|
653
|
-
provider: DEFAULT_CONFIG.decision.provider,
|
|
654
|
-
baseUrl: DEFAULT_CONFIG.decision.baseUrl,
|
|
655
|
-
model: DEFAULT_CONFIG.decision.model,
|
|
656
609
|
timeoutMs: DEFAULT_CONFIG.decision.timeoutMs,
|
|
657
610
|
keepAlive: DEFAULT_CONFIG.decision.keepAlive,
|
|
658
611
|
routeBeforeAsking: DEFAULT_CONFIG.decision.routeBeforeAsking,
|
|
@@ -666,9 +619,6 @@ export function generateDefaultConfigJson(tuning, existingConfigRaw) {
|
|
|
666
619
|
},
|
|
667
620
|
description: {
|
|
668
621
|
enabled: DEFAULT_CONFIG.description.enabled,
|
|
669
|
-
provider: DEFAULT_CONFIG.description.provider,
|
|
670
|
-
baseUrl: DEFAULT_CONFIG.description.baseUrl,
|
|
671
|
-
model: DEFAULT_CONFIG.description.model,
|
|
672
622
|
think: DEFAULT_CONFIG.description.think,
|
|
673
623
|
numCtx: DEFAULT_CONFIG.description.numCtx,
|
|
674
624
|
timeoutMs: DEFAULT_CONFIG.description.timeoutMs,
|
|
@@ -680,10 +630,13 @@ export function generateDefaultConfigJson(tuning, existingConfigRaw) {
|
|
|
680
630
|
mcp: {
|
|
681
631
|
enabled: DEFAULT_CONFIG.mcp.enabled,
|
|
682
632
|
},
|
|
633
|
+
setup: {
|
|
634
|
+
autoScaffold: DEFAULT_CONFIG.setup.autoScaffold,
|
|
635
|
+
},
|
|
683
636
|
logging: {
|
|
684
637
|
level: DEFAULT_CONFIG.logging.level,
|
|
685
638
|
logFilePath: DEFAULT_CONFIG.logging.logFilePath,
|
|
686
639
|
},
|
|
687
640
|
}, null, 2) + "\n";
|
|
688
641
|
}
|
|
689
|
-
//# sourceMappingURL=
|
|
642
|
+
//# sourceMappingURL=workspace-helpers.js.map
|
package/dist/cli/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { realpathSync } from "node:fs";
|
|
|
15
15
|
import { basename, dirname } from "node:path";
|
|
16
16
|
import { fileURLToPath } from "node:url";
|
|
17
17
|
import { getPackageMetadata } from "./helpers.js";
|
|
18
|
-
import { registerIndexCommand, registerQueryCommand, registerClearCommand, registerStatusCommand, registerListCommand, registerShowCommand, registerDumpCommand,
|
|
18
|
+
import { registerIndexCommand, registerQueryCommand, registerClearCommand, registerStatusCommand, registerListCommand, registerShowCommand, registerDumpCommand, registerClickHouseCommand, registerUiCommand, registerMcpCommand, registerSetupCommand, registerUpdateCommand, registerEvalSessionsCommand, registerEvalAnalyzeCommand, registerEvalCompareCommand, registerEvalGateCommand, registerEvalDecideCommand, registerEvalTuneCommand, registerDescribeImageCommand, registerDecideCommand, registerQuirkCommand, } from "./commands/index.js";
|
|
19
19
|
/**
|
|
20
20
|
* The top-level Commander program instance that defines the `opencode-rag` CLI.
|
|
21
21
|
*
|
|
@@ -57,7 +57,6 @@ registerEvalCompareCommand(program);
|
|
|
57
57
|
registerEvalGateCommand(program);
|
|
58
58
|
registerEvalDecideCommand(program);
|
|
59
59
|
registerEvalTuneCommand(program);
|
|
60
|
-
registerInitCommand(program);
|
|
61
60
|
registerClickHouseCommand(program);
|
|
62
61
|
registerQuirkCommand(program);
|
|
63
62
|
// ── Auto-run detection ──────────────────────────────────────────
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Ollama detection hints shared by `setup` and the wizard.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Platform-specific command that installs Ollama.
|
|
6
|
+
*
|
|
7
|
+
* The CLI never installs Ollama itself (it is a native application with a
|
|
8
|
+
* service), but printing the exact command removes the "what now?" step.
|
|
9
|
+
*/
|
|
10
|
+
export declare function ollamaInstallHint(): string;
|
|
11
|
+
/** Whether an error message looks like a refused/unreachable Ollama connection. */
|
|
12
|
+
export declare function isOllamaConnectionError(message: string | undefined): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Ollama detection hints shared by `setup` and the wizard.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Platform-specific command that installs Ollama.
|
|
6
|
+
*
|
|
7
|
+
* The CLI never installs Ollama itself (it is a native application with a
|
|
8
|
+
* service), but printing the exact command removes the "what now?" step.
|
|
9
|
+
*/
|
|
10
|
+
export function ollamaInstallHint() {
|
|
11
|
+
switch (process.platform) {
|
|
12
|
+
case "win32":
|
|
13
|
+
return "Install it with: winget install Ollama.Ollama";
|
|
14
|
+
case "darwin":
|
|
15
|
+
return "Install it with: brew install ollama (or download it from https://ollama.com)";
|
|
16
|
+
default:
|
|
17
|
+
return "Install it with: curl -fsSL https://ollama.com/install.sh | sh";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Whether an error message looks like a refused/unreachable Ollama connection. */
|
|
21
|
+
export function isOllamaConnectionError(message) {
|
|
22
|
+
if (!message)
|
|
23
|
+
return false;
|
|
24
|
+
return /connection refused|ECONNREFUSED|fetch failed|cannot reach|not reachable/i.test(message);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=ollama.js.map
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
* Prompt implementation backed by one persistent `node:readline` interface.
|
|
80
|
+
*
|
|
81
|
+
* The interface stays attached for the whole wizard and queues lines that
|
|
82
|
+
* arrive while no question is pending: a paste, a piped stdin or a pty script
|
|
83
|
+
* can deliver several answers at once, and a per-prompt interface would drop
|
|
84
|
+
* the extra lines (readline emits them as `line` events with no listener).
|
|
85
|
+
*
|
|
86
|
+
* Exporting the class is deliberate — the queue behaviour is covered by
|
|
87
|
+
* `src/__tests__/cli/prompt.test.ts`, which injects two `PassThrough` streams.
|
|
88
|
+
*/
|
|
89
|
+
export declare class ReadlinePrompter implements Prompter {
|
|
90
|
+
private readonly input;
|
|
91
|
+
private readonly output;
|
|
92
|
+
readonly interactive = true;
|
|
93
|
+
private rl;
|
|
94
|
+
/** Bumped on every attach/detach so stale listeners from an old interface are ignored. */
|
|
95
|
+
private generation;
|
|
96
|
+
private readonly buffered;
|
|
97
|
+
private waiter;
|
|
98
|
+
private inputClosed;
|
|
99
|
+
private cancelled;
|
|
100
|
+
constructor(input: NodeJS.ReadStream, output: NodeJS.WriteStream);
|
|
101
|
+
/** Attach (or re-attach) the line reader. */
|
|
102
|
+
private ensureInterface;
|
|
103
|
+
/** Close the reader without ending the wizard (used by the raw-mode password path). */
|
|
104
|
+
private detach;
|
|
105
|
+
/** Hand a line to the waiting question, or queue it for the next one. */
|
|
106
|
+
private deliver;
|
|
107
|
+
/** Ask one line, resolving `undefined` on abort. */
|
|
108
|
+
private ask;
|
|
109
|
+
text(options: TextPromptOptions): Promise<string | undefined>;
|
|
110
|
+
password(message: string): Promise<string | undefined>;
|
|
111
|
+
confirm(options: ConfirmPromptOptions): Promise<boolean | undefined>;
|
|
112
|
+
select<T>(options: SelectPromptOptions<T>): Promise<T | undefined>;
|
|
113
|
+
close(): void;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a prompter for the current process.
|
|
117
|
+
*
|
|
118
|
+
* When `interactive` is false (or stdin is not a TTY), a non-interactive
|
|
119
|
+
* prompter is returned — the wizard then applies defaults instead of hanging.
|
|
120
|
+
*/
|
|
121
|
+
export declare function createPrompter(interactive?: boolean): Prompter;
|
|
@@ -0,0 +1,309 @@
|
|
|
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 one persistent `node:readline` interface.
|
|
62
|
+
*
|
|
63
|
+
* The interface stays attached for the whole wizard and queues lines that
|
|
64
|
+
* arrive while no question is pending: a paste, a piped stdin or a pty script
|
|
65
|
+
* can deliver several answers at once, and a per-prompt interface would drop
|
|
66
|
+
* the extra lines (readline emits them as `line` events with no listener).
|
|
67
|
+
*
|
|
68
|
+
* Exporting the class is deliberate — the queue behaviour is covered by
|
|
69
|
+
* `src/__tests__/cli/prompt.test.ts`, which injects two `PassThrough` streams.
|
|
70
|
+
*/
|
|
71
|
+
export class ReadlinePrompter {
|
|
72
|
+
input;
|
|
73
|
+
output;
|
|
74
|
+
interactive = true;
|
|
75
|
+
rl;
|
|
76
|
+
/** Bumped on every attach/detach so stale listeners from an old interface are ignored. */
|
|
77
|
+
generation = 0;
|
|
78
|
+
buffered = [];
|
|
79
|
+
waiter;
|
|
80
|
+
inputClosed = false;
|
|
81
|
+
cancelled = false;
|
|
82
|
+
constructor(input, output) {
|
|
83
|
+
this.input = input;
|
|
84
|
+
this.output = output;
|
|
85
|
+
}
|
|
86
|
+
/** Attach (or re-attach) the line reader. */
|
|
87
|
+
ensureInterface() {
|
|
88
|
+
if (this.rl)
|
|
89
|
+
return this.rl;
|
|
90
|
+
const generation = ++this.generation;
|
|
91
|
+
const rl = createInterface({ input: this.input, output: this.output, terminal: true });
|
|
92
|
+
rl.on("line", (line) => {
|
|
93
|
+
if (generation === this.generation)
|
|
94
|
+
this.deliver(line);
|
|
95
|
+
});
|
|
96
|
+
rl.on("SIGINT", () => {
|
|
97
|
+
if (generation !== this.generation)
|
|
98
|
+
return;
|
|
99
|
+
writeLine(this.output, "\n");
|
|
100
|
+
this.cancelled = true;
|
|
101
|
+
this.detach();
|
|
102
|
+
this.deliver(undefined);
|
|
103
|
+
});
|
|
104
|
+
rl.once("close", () => {
|
|
105
|
+
if (generation !== this.generation)
|
|
106
|
+
return;
|
|
107
|
+
this.rl = undefined;
|
|
108
|
+
this.inputClosed = true;
|
|
109
|
+
this.deliver(undefined);
|
|
110
|
+
});
|
|
111
|
+
this.rl = rl;
|
|
112
|
+
return rl;
|
|
113
|
+
}
|
|
114
|
+
/** Close the reader without ending the wizard (used by the raw-mode password path). */
|
|
115
|
+
detach() {
|
|
116
|
+
const rl = this.rl;
|
|
117
|
+
this.rl = undefined;
|
|
118
|
+
this.generation += 1;
|
|
119
|
+
rl?.close();
|
|
120
|
+
}
|
|
121
|
+
/** Hand a line to the waiting question, or queue it for the next one. */
|
|
122
|
+
deliver(line) {
|
|
123
|
+
const waiter = this.waiter;
|
|
124
|
+
if (waiter) {
|
|
125
|
+
this.waiter = undefined;
|
|
126
|
+
waiter(line);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (line !== undefined)
|
|
130
|
+
this.buffered.push(line);
|
|
131
|
+
}
|
|
132
|
+
/** Ask one line, resolving `undefined` on abort. */
|
|
133
|
+
ask(question) {
|
|
134
|
+
// Buffered answers win over EOF: a pipe ends the stream right after the
|
|
135
|
+
// last answer, and the wizard must still apply them.
|
|
136
|
+
const buffered = this.buffered.shift();
|
|
137
|
+
if (buffered !== undefined) {
|
|
138
|
+
// The answer arrived before the question was displayed (paste / piped
|
|
139
|
+
// input): echo both so the transcript still reads like a session.
|
|
140
|
+
writeLine(this.output, `${question}${buffered}\n`);
|
|
141
|
+
return Promise.resolve(buffered);
|
|
142
|
+
}
|
|
143
|
+
if (this.cancelled || this.inputClosed)
|
|
144
|
+
return Promise.resolve(undefined);
|
|
145
|
+
const rl = this.ensureInterface();
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
this.waiter = resolve;
|
|
148
|
+
rl.setPrompt(question);
|
|
149
|
+
rl.prompt();
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
async text(options) {
|
|
153
|
+
const suffix = options.defaultValue !== undefined && options.defaultValue !== ""
|
|
154
|
+
? ` [${options.defaultValue}]`
|
|
155
|
+
: "";
|
|
156
|
+
if (options.hint)
|
|
157
|
+
writeLine(this.output, ` ${options.hint}\n`);
|
|
158
|
+
for (;;) {
|
|
159
|
+
const answer = await this.ask(` ${options.message}${suffix}: `);
|
|
160
|
+
if (answer === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
const value = answer.trim() === "" ? (options.defaultValue ?? "") : answer.trim();
|
|
163
|
+
const error = options.validate?.(value);
|
|
164
|
+
if (error) {
|
|
165
|
+
writeLine(this.output, ` ${error}\n`);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
return value;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async password(message) {
|
|
172
|
+
if (!this.input.isTTY || typeof this.input.setRawMode !== "function") {
|
|
173
|
+
// Piped stdin: read a plain line (still masked in the transcript by the caller).
|
|
174
|
+
return this.ask(` ${message}: `);
|
|
175
|
+
}
|
|
176
|
+
// Raw mode must own the stream: the line reader would otherwise echo and
|
|
177
|
+
// consume the characters. Re-attached lazily by the next `ask`.
|
|
178
|
+
this.detach();
|
|
179
|
+
writeLine(this.output, ` ${message}: `);
|
|
180
|
+
this.input.setRawMode(true);
|
|
181
|
+
this.input.resume();
|
|
182
|
+
return new Promise((resolve) => {
|
|
183
|
+
let value = "";
|
|
184
|
+
const cleanup = () => {
|
|
185
|
+
this.input.removeListener("data", onData);
|
|
186
|
+
this.input.setRawMode(false);
|
|
187
|
+
this.input.pause();
|
|
188
|
+
writeLine(this.output, "\n");
|
|
189
|
+
};
|
|
190
|
+
const onData = (chunk) => {
|
|
191
|
+
const text = chunk.toString("utf8");
|
|
192
|
+
for (const char of text) {
|
|
193
|
+
if (char === "\u0003") {
|
|
194
|
+
cleanup();
|
|
195
|
+
resolve(undefined);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (char === "\r" || char === "\n") {
|
|
199
|
+
cleanup();
|
|
200
|
+
resolve(value);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (char === "\u007f" || char === "\b") {
|
|
204
|
+
if (value.length > 0) {
|
|
205
|
+
value = value.slice(0, -1);
|
|
206
|
+
writeLine(this.output, "\b \b");
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (char >= " ") {
|
|
211
|
+
value += char;
|
|
212
|
+
writeLine(this.output, "*");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
this.input.on("data", onData);
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
async confirm(options) {
|
|
220
|
+
const hint = options.defaultValue === false ? "y/N" : "Y/n";
|
|
221
|
+
for (;;) {
|
|
222
|
+
const answer = await this.ask(` ${options.message} (${hint}): `);
|
|
223
|
+
if (answer === undefined)
|
|
224
|
+
return undefined;
|
|
225
|
+
const normalized = answer.trim().toLowerCase();
|
|
226
|
+
if (normalized === "")
|
|
227
|
+
return options.defaultValue ?? true;
|
|
228
|
+
if (normalized === "y" || normalized === "yes")
|
|
229
|
+
return true;
|
|
230
|
+
if (normalized === "n" || normalized === "no")
|
|
231
|
+
return false;
|
|
232
|
+
writeLine(this.output, " Please answer y or n.\n");
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async select(options) {
|
|
236
|
+
const all = options.options;
|
|
237
|
+
if (all.length === 0)
|
|
238
|
+
return undefined;
|
|
239
|
+
const pageSize = Math.max(1, options.pageSize ?? 15);
|
|
240
|
+
const defaultIndex = Math.min(Math.max(options.defaultIndex ?? 0, 0), all.length - 1);
|
|
241
|
+
let start = Math.floor(defaultIndex / pageSize) * pageSize;
|
|
242
|
+
if (options.hint)
|
|
243
|
+
writeLine(this.output, ` ${options.hint}\n`);
|
|
244
|
+
for (;;) {
|
|
245
|
+
const end = Math.min(start + pageSize, all.length);
|
|
246
|
+
writeLine(this.output, "");
|
|
247
|
+
for (let i = start; i < end; i += 1) {
|
|
248
|
+
const entry = all[i];
|
|
249
|
+
if (!entry)
|
|
250
|
+
continue;
|
|
251
|
+
const marker = i === defaultIndex ? "*" : " ";
|
|
252
|
+
const label = `${marker} ${i + 1}) ${entry.label}`;
|
|
253
|
+
writeLine(this.output, ` ${label}\n`);
|
|
254
|
+
if (entry.description) {
|
|
255
|
+
writeLine(this.output, ` ${entry.description}\n`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const nav = [];
|
|
259
|
+
if (start > 0)
|
|
260
|
+
nav.push("p = previous page");
|
|
261
|
+
if (end < all.length)
|
|
262
|
+
nav.push("n = next page");
|
|
263
|
+
nav.push("number = choose");
|
|
264
|
+
if (all.length > 1)
|
|
265
|
+
nav.push("Enter = default (*)");
|
|
266
|
+
writeLine(this.output, ` ${nav.join(" · ")}\n`);
|
|
267
|
+
const answer = await this.ask(` ${options.message}: `);
|
|
268
|
+
if (answer === undefined)
|
|
269
|
+
return undefined;
|
|
270
|
+
const value = answer.trim().toLowerCase();
|
|
271
|
+
if (value === "")
|
|
272
|
+
return all[defaultIndex]?.value;
|
|
273
|
+
if (value === "n" && end < all.length) {
|
|
274
|
+
start = Math.min(start + pageSize, all.length - 1);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (value === "p" && start > 0) {
|
|
278
|
+
start = Math.max(0, start - pageSize);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const index = Number.parseInt(value, 10);
|
|
282
|
+
if (Number.isInteger(index) && index >= 1 && index <= all.length) {
|
|
283
|
+
return all[index - 1]?.value;
|
|
284
|
+
}
|
|
285
|
+
// Accept the literal value too (e.g. a model name typed by hand).
|
|
286
|
+
const exact = all.find((entry) => String(entry.value) === answer.trim());
|
|
287
|
+
if (exact !== undefined)
|
|
288
|
+
return exact.value;
|
|
289
|
+
writeLine(this.output, " Not a valid selection.\n");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
close() {
|
|
293
|
+
this.cancelled = true;
|
|
294
|
+
this.detach();
|
|
295
|
+
this.deliver(undefined);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Create a prompter for the current process.
|
|
300
|
+
*
|
|
301
|
+
* When `interactive` is false (or stdin is not a TTY), a non-interactive
|
|
302
|
+
* prompter is returned — the wizard then applies defaults instead of hanging.
|
|
303
|
+
*/
|
|
304
|
+
export function createPrompter(interactive = isInteractiveTerminal()) {
|
|
305
|
+
if (!interactive)
|
|
306
|
+
return new NonInteractivePrompter();
|
|
307
|
+
return new ReadlinePrompter(process.stdin, process.stdout);
|
|
308
|
+
}
|
|
309
|
+
//# sourceMappingURL=prompt.js.map
|