@keyou007/agent-bot 0.1.16-alpha.0 → 0.1.16-alpha.2

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.
@@ -0,0 +1,135 @@
1
+ import type { Readable, Writable } from "node:stream";
2
+ import {
3
+ cancel,
4
+ intro,
5
+ isCancel,
6
+ log,
7
+ multiselect,
8
+ note,
9
+ outro,
10
+ select,
11
+ } from "@clack/prompts";
12
+ import { cliText } from "./i18n.js";
13
+
14
+ export interface InitUiOption<Value extends string | number> {
15
+ value: Value;
16
+ label: string;
17
+ hint?: string;
18
+ }
19
+
20
+ export interface InitUiOptions {
21
+ interactive: boolean;
22
+ input?: Readable;
23
+ output?: Writable;
24
+ }
25
+
26
+ export class InitializationPromptCancelledError extends Error {
27
+ constructor() {
28
+ super(cliText("Initialization was cancelled.", "初始化已取消。"));
29
+ this.name = "InitializationPromptCancelledError";
30
+ }
31
+ }
32
+
33
+ export class InitUi {
34
+ readonly interactive: boolean;
35
+ private readonly input: Readable;
36
+ private readonly output: Writable;
37
+
38
+ constructor(options: InitUiOptions) {
39
+ this.interactive = options.interactive;
40
+ this.input = options.input ?? process.stdin;
41
+ this.output = options.output ?? process.stderr;
42
+ }
43
+
44
+ start(title: string): void {
45
+ if (this.interactive) intro(title, this.commonOptions());
46
+ }
47
+
48
+ finish(message: string): void {
49
+ if (this.interactive) outro(message, this.commonOptions());
50
+ }
51
+
52
+ step(message: string): void {
53
+ if (this.interactive) log.step(message, this.commonOptions());
54
+ }
55
+
56
+ info(message: string): void {
57
+ if (this.interactive) log.info(message, this.commonOptions());
58
+ }
59
+
60
+ success(message: string): void {
61
+ if (this.interactive) log.success(message, this.commonOptions());
62
+ }
63
+
64
+ warn(message: string): void {
65
+ if (this.interactive) log.warn(message, this.commonOptions());
66
+ }
67
+
68
+ note(title: string, message: string): void {
69
+ if (this.interactive) note(message, title, this.commonOptions());
70
+ }
71
+
72
+ async select<Value extends string | number>(options: {
73
+ message: string;
74
+ options: InitUiOption<Value>[];
75
+ initialValue?: Value;
76
+ }): Promise<Value> {
77
+ this.requireInteractive();
78
+ const value = await select<string | number>({
79
+ message: options.message,
80
+ options: promptOptions(options.options),
81
+ ...(options.initialValue !== undefined ? { initialValue: options.initialValue } : {}),
82
+ ...this.commonOptions(),
83
+ });
84
+ return this.requireValue(value) as Value;
85
+ }
86
+
87
+ async multiselect<Value extends string | number>(options: {
88
+ message: string;
89
+ options: InitUiOption<Value>[];
90
+ initialValues?: Value[];
91
+ required?: boolean;
92
+ }): Promise<Value[]> {
93
+ this.requireInteractive();
94
+ const values = await multiselect<string | number>({
95
+ message: options.message,
96
+ options: promptOptions(options.options),
97
+ ...(options.initialValues ? { initialValues: options.initialValues } : {}),
98
+ ...(options.required !== undefined ? { required: options.required } : {}),
99
+ ...this.commonOptions(),
100
+ });
101
+ return this.requireValue(values) as Value[];
102
+ }
103
+
104
+ private commonOptions(): { input: Readable; output: Writable } {
105
+ return { input: this.input, output: this.output };
106
+ }
107
+
108
+ private requireInteractive(): void {
109
+ if (!this.interactive) {
110
+ throw new Error("Interactive initialization is not available in this terminal.");
111
+ }
112
+ }
113
+
114
+ private requireValue<Value>(value: Value | symbol): Value {
115
+ if (!isCancel(value)) return value;
116
+ cancel(cliText("Initialization cancelled.", "初始化已取消。"), this.commonOptions());
117
+ throw new InitializationPromptCancelledError();
118
+ }
119
+ }
120
+
121
+ function promptOptions<Value extends string | number>(
122
+ options: InitUiOption<Value>[],
123
+ ): Array<InitUiOption<string> | InitUiOption<number>> {
124
+ return options.map(({ value, label, hint }) => typeof value === "number"
125
+ ? { value: value as number, label, ...(hint ? { hint } : {}) }
126
+ : { value: value as string, label, ...(hint ? { hint } : {}) });
127
+ }
128
+
129
+ export function shouldUseInteractiveInitialization(
130
+ json: boolean,
131
+ inputIsTty = process.stdin.isTTY === true,
132
+ outputIsTty = process.stderr.isTTY === true,
133
+ ): boolean {
134
+ return !json && inputIsTty && outputIsTty;
135
+ }