pi-ask-question-lite 0.0.0-stage → 0.1.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/CHANGELOG.md +18 -0
- package/README.md +105 -2
- package/README.zh-CN.md +105 -0
- package/package.json +50 -4
- package/src/config.ts +409 -0
- package/src/index.ts +397 -0
- package/src/schema.ts +223 -0
- package/src/state.ts +389 -0
- package/src/view.ts +710 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
export type Config = {
|
|
6
|
+
tools: { ask: { name: string } };
|
|
7
|
+
limits: { maxQuestions: number; maxOptions: number };
|
|
8
|
+
ui: {
|
|
9
|
+
maxHeightRatio: number;
|
|
10
|
+
bell: boolean;
|
|
11
|
+
showHints: boolean;
|
|
12
|
+
preview: { minSideBySideWidth: number };
|
|
13
|
+
};
|
|
14
|
+
keys: { annotate: string; submitEarly: string; collapse: string };
|
|
15
|
+
guidance: {
|
|
16
|
+
description: string;
|
|
17
|
+
promptSnippet: string;
|
|
18
|
+
promptGuidelines: string[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type LoadOptions = {
|
|
23
|
+
configPath?: string;
|
|
24
|
+
env?: NodeJS.ProcessEnv;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const DEFAULTS: Config = {
|
|
28
|
+
tools: { ask: { name: "ask" } },
|
|
29
|
+
limits: { maxQuestions: 4, maxOptions: 6 },
|
|
30
|
+
ui: {
|
|
31
|
+
maxHeightRatio: 0.45,
|
|
32
|
+
bell: true,
|
|
33
|
+
showHints: true,
|
|
34
|
+
preview: { minSideBySideWidth: 100 },
|
|
35
|
+
},
|
|
36
|
+
keys: { annotate: "e", submitEarly: "ctrl+enter", collapse: "ctrl+]" },
|
|
37
|
+
guidance: {
|
|
38
|
+
description:
|
|
39
|
+
"Ask structured questions when user intent or trade-offs are unclear.",
|
|
40
|
+
promptSnippet: "",
|
|
41
|
+
promptGuidelines: [
|
|
42
|
+
"tool-name: Check code, docs, or tools first; only ask when user intent or trade-offs are unclear.",
|
|
43
|
+
"tool-name: Ask only independent questions together; defer follow-ups that depend on earlier answers.",
|
|
44
|
+
"tool-name: Put the recommended option first, keep `label` short, and explain trade-offs in `detail`.",
|
|
45
|
+
"tool-name: Put concrete examples, code, or layouts in `preview` when options differ in behavior.",
|
|
46
|
+
"tool-name: Set `multiple: true` only when choices are not mutually exclusive.",
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const MIN_HEIGHT_RATIO = 0.2;
|
|
52
|
+
const MAX_HEIGHT_RATIO = 0.8;
|
|
53
|
+
const TOOL_NAME = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
54
|
+
|
|
55
|
+
export function resolveConfigPath(options: LoadOptions = {}): string {
|
|
56
|
+
if (options.configPath !== undefined) return resolve(options.configPath);
|
|
57
|
+
const env = options.env ?? process.env;
|
|
58
|
+
const agentDir = env.PI_CODING_AGENT_DIR
|
|
59
|
+
? resolve(env.PI_CODING_AGENT_DIR)
|
|
60
|
+
: join(homedir(), ".pi", "agent");
|
|
61
|
+
return join(agentDir, "pi-ask", "config.json");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function loadConfig(options: LoadOptions = {}): Config {
|
|
65
|
+
const path = resolveConfigPath(options);
|
|
66
|
+
const source = readConfigFile(path);
|
|
67
|
+
if (source === undefined) return structuredClone(DEFAULTS);
|
|
68
|
+
|
|
69
|
+
let parsed: unknown;
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(source);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`pi-ask config ${path}: invalid JSON: ${error instanceof Error ? error.message : String(error)}`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const errors: string[] = [];
|
|
79
|
+
const config = buildConfig(parsed, errors);
|
|
80
|
+
if (errors.length > 0)
|
|
81
|
+
throw new Error(`pi-ask config ${path}: ${errors.join("\n")}`);
|
|
82
|
+
return config;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type Raw = Record<string, unknown>;
|
|
86
|
+
|
|
87
|
+
function readConfigFile(path: string): string | undefined {
|
|
88
|
+
try {
|
|
89
|
+
return readFileSync(path, "utf-8");
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (isRecord(error) && error.code === "ENOENT") return undefined;
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function buildConfig(input: unknown, errors: string[]): Config {
|
|
97
|
+
if (!isRecord(input)) {
|
|
98
|
+
errors.push(`expected object, got ${describe(input)}`);
|
|
99
|
+
return structuredClone(DEFAULTS);
|
|
100
|
+
}
|
|
101
|
+
checkKeys(input, "", ["tools", "limits", "ui", "keys", "guidance"], errors);
|
|
102
|
+
|
|
103
|
+
const tools = readGroup(input.tools, "tools", errors);
|
|
104
|
+
checkKeys(tools, "tools", ["ask"], errors);
|
|
105
|
+
const ask = readGroup(tools.ask, "tools.ask", errors);
|
|
106
|
+
checkKeys(ask, "tools.ask", ["name"], errors);
|
|
107
|
+
const name =
|
|
108
|
+
readString(ask.name, "tools.ask.name", errors) ?? DEFAULTS.tools.ask.name;
|
|
109
|
+
if (!TOOL_NAME.test(name))
|
|
110
|
+
errors.push(`tools.ask.name: expected a tool name, got "${name}"`);
|
|
111
|
+
|
|
112
|
+
const limits = readGroup(input.limits, "limits", errors);
|
|
113
|
+
checkKeys(limits, "limits", ["maxQuestions", "maxOptions"], errors);
|
|
114
|
+
const maxQuestions =
|
|
115
|
+
readInteger(limits.maxQuestions, "limits.maxQuestions", errors, {
|
|
116
|
+
min: 1,
|
|
117
|
+
}) ?? DEFAULTS.limits.maxQuestions;
|
|
118
|
+
const maxOptions =
|
|
119
|
+
readInteger(limits.maxOptions, "limits.maxOptions", errors, { min: 2 }) ??
|
|
120
|
+
DEFAULTS.limits.maxOptions;
|
|
121
|
+
|
|
122
|
+
const ui = readGroup(input.ui, "ui", errors);
|
|
123
|
+
checkKeys(
|
|
124
|
+
ui,
|
|
125
|
+
"ui",
|
|
126
|
+
["maxHeightRatio", "bell", "showHints", "preview"],
|
|
127
|
+
errors,
|
|
128
|
+
);
|
|
129
|
+
const ratio = readNumber(ui.maxHeightRatio, "ui.maxHeightRatio", errors);
|
|
130
|
+
const bell = readBoolean(ui.bell, "ui.bell", errors) ?? DEFAULTS.ui.bell;
|
|
131
|
+
const showHints =
|
|
132
|
+
readBoolean(ui.showHints, "ui.showHints", errors) ?? DEFAULTS.ui.showHints;
|
|
133
|
+
const preview = readGroup(ui.preview, "ui.preview", errors);
|
|
134
|
+
checkKeys(preview, "ui.preview", ["minSideBySideWidth"], errors);
|
|
135
|
+
const minSideBySideWidth =
|
|
136
|
+
readInteger(
|
|
137
|
+
preview.minSideBySideWidth,
|
|
138
|
+
"ui.preview.minSideBySideWidth",
|
|
139
|
+
errors,
|
|
140
|
+
{
|
|
141
|
+
min: 20,
|
|
142
|
+
},
|
|
143
|
+
) ?? DEFAULTS.ui.preview.minSideBySideWidth;
|
|
144
|
+
|
|
145
|
+
const keys = readGroup(input.keys, "keys", errors);
|
|
146
|
+
checkKeys(keys, "keys", ["annotate", "submitEarly", "collapse"], errors);
|
|
147
|
+
const annotate =
|
|
148
|
+
readString(keys.annotate, "keys.annotate", errors) ??
|
|
149
|
+
DEFAULTS.keys.annotate;
|
|
150
|
+
const submitEarly =
|
|
151
|
+
readString(keys.submitEarly, "keys.submitEarly", errors) ??
|
|
152
|
+
DEFAULTS.keys.submitEarly;
|
|
153
|
+
if (annotate.trim() === "") errors.push("keys.annotate: expected a key");
|
|
154
|
+
if (submitEarly.trim() === "")
|
|
155
|
+
errors.push("keys.submitEarly: expected a key");
|
|
156
|
+
const collapse = readCollapse(
|
|
157
|
+
keys.collapse,
|
|
158
|
+
{ annotate, submitEarly },
|
|
159
|
+
errors,
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
const guidance = readGroup(input.guidance, "guidance", errors);
|
|
163
|
+
checkKeys(
|
|
164
|
+
guidance,
|
|
165
|
+
"guidance",
|
|
166
|
+
["description", "promptSnippet", "promptGuidelines"],
|
|
167
|
+
errors,
|
|
168
|
+
);
|
|
169
|
+
const description =
|
|
170
|
+
readString(guidance.description, "guidance.description", errors) ??
|
|
171
|
+
DEFAULTS.guidance.description;
|
|
172
|
+
const promptSnippet =
|
|
173
|
+
readString(guidance.promptSnippet, "guidance.promptSnippet", errors) ??
|
|
174
|
+
DEFAULTS.guidance.promptSnippet;
|
|
175
|
+
const promptGuidelines =
|
|
176
|
+
readStringArray(
|
|
177
|
+
guidance.promptGuidelines,
|
|
178
|
+
"guidance.promptGuidelines",
|
|
179
|
+
errors,
|
|
180
|
+
) ?? DEFAULTS.guidance.promptGuidelines;
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
tools: { ask: { name } },
|
|
184
|
+
limits: { maxQuestions, maxOptions },
|
|
185
|
+
ui: {
|
|
186
|
+
maxHeightRatio: clampRatio(ratio ?? DEFAULTS.ui.maxHeightRatio),
|
|
187
|
+
bell,
|
|
188
|
+
showHints,
|
|
189
|
+
preview: { minSideBySideWidth },
|
|
190
|
+
},
|
|
191
|
+
keys: { annotate, submitEarly, collapse },
|
|
192
|
+
guidance: { description, promptSnippet, promptGuidelines },
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const SPECIAL_KEYS = new Set([
|
|
197
|
+
"escape",
|
|
198
|
+
"esc",
|
|
199
|
+
"enter",
|
|
200
|
+
"return",
|
|
201
|
+
"tab",
|
|
202
|
+
"space",
|
|
203
|
+
"backspace",
|
|
204
|
+
"delete",
|
|
205
|
+
"insert",
|
|
206
|
+
"clear",
|
|
207
|
+
"home",
|
|
208
|
+
"end",
|
|
209
|
+
"pageUp",
|
|
210
|
+
"pageDown",
|
|
211
|
+
"up",
|
|
212
|
+
"down",
|
|
213
|
+
"left",
|
|
214
|
+
"right",
|
|
215
|
+
...Array.from({ length: 12 }, (_value, index) => `f${index + 1}`),
|
|
216
|
+
]);
|
|
217
|
+
|
|
218
|
+
const SYMBOL_KEYS = new Set("`-=[]\\;',./!@#$%^&*()_|~{}:<>?".split(""));
|
|
219
|
+
|
|
220
|
+
/** 修饰键只能按这个顺序写:与 matchesKey 的 KeyId 一致,也避免 shift+ctrl 这类两种写法。 */
|
|
221
|
+
const MODIFIERS = ["ctrl", "shift", "alt", "super"] as const;
|
|
222
|
+
|
|
223
|
+
/** 组件里写死的导航键,以及它们在老终端里的同义控制字符。 */
|
|
224
|
+
const RESERVED_KEYS = new Map([
|
|
225
|
+
["up", "up"],
|
|
226
|
+
["down", "down"],
|
|
227
|
+
["left", "left"],
|
|
228
|
+
["right", "right"],
|
|
229
|
+
["tab", "tab"],
|
|
230
|
+
["shift+tab", "shift+tab"],
|
|
231
|
+
["enter", "enter"],
|
|
232
|
+
["return", "enter"],
|
|
233
|
+
["escape", "escape"],
|
|
234
|
+
["esc", "escape"],
|
|
235
|
+
["space", "space"],
|
|
236
|
+
["1", "1"],
|
|
237
|
+
["2", "2"],
|
|
238
|
+
["3", "3"],
|
|
239
|
+
["4", "4"],
|
|
240
|
+
["5", "5"],
|
|
241
|
+
["6", "6"],
|
|
242
|
+
["7", "7"],
|
|
243
|
+
["8", "8"],
|
|
244
|
+
["9", "9"],
|
|
245
|
+
// 老终端把控制字符当别名:ctrl+m 与 ctrl+j 发 \r,ctrl+i 发 \t,ctrl+[ 发 ESC。
|
|
246
|
+
["ctrl+m", "enter"],
|
|
247
|
+
["ctrl+j", "enter"],
|
|
248
|
+
["ctrl+i", "tab"],
|
|
249
|
+
["ctrl+[", "escape"],
|
|
250
|
+
["ctrl+h", "backspace"],
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
/** `keys.collapse`:具体按键或 "off"。格式不严会让 matchesKey 静默失效。 */
|
|
254
|
+
function readCollapse(
|
|
255
|
+
value: unknown,
|
|
256
|
+
taken: { annotate: string; submitEarly: string },
|
|
257
|
+
errors: string[],
|
|
258
|
+
): string {
|
|
259
|
+
const text = readString(value, "keys.collapse", errors);
|
|
260
|
+
if (text === undefined) return DEFAULTS.keys.collapse;
|
|
261
|
+
if (text === "off") return text;
|
|
262
|
+
const problem = keyProblem(text);
|
|
263
|
+
if (problem !== undefined) {
|
|
264
|
+
errors.push(`keys.collapse: ${problem}`);
|
|
265
|
+
return DEFAULTS.keys.collapse;
|
|
266
|
+
}
|
|
267
|
+
const reserved = RESERVED_KEYS.get(text);
|
|
268
|
+
if (reserved !== undefined) {
|
|
269
|
+
errors.push(`keys.collapse: "${text}" is reserved for ${reserved}`);
|
|
270
|
+
return DEFAULTS.keys.collapse;
|
|
271
|
+
}
|
|
272
|
+
for (const [name, key] of Object.entries(taken)) {
|
|
273
|
+
if (key !== text) continue;
|
|
274
|
+
errors.push(`keys.collapse: "${text}" conflicts with keys.${name}`);
|
|
275
|
+
return DEFAULTS.keys.collapse;
|
|
276
|
+
}
|
|
277
|
+
return text;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function keyProblem(text: string): string | undefined {
|
|
281
|
+
const parts = text.split("+");
|
|
282
|
+
const base = parts.pop() ?? "";
|
|
283
|
+
if (!isBaseKey(base))
|
|
284
|
+
return `expected a key like "ctrl+]" or "off", got "${text}"`;
|
|
285
|
+
let previous = -1;
|
|
286
|
+
for (const part of parts) {
|
|
287
|
+
const index = MODIFIERS.indexOf(part as (typeof MODIFIERS)[number]);
|
|
288
|
+
if (index <= previous)
|
|
289
|
+
return `expected modifiers in ctrl+shift+alt+super order, got "${text}"`;
|
|
290
|
+
previous = index;
|
|
291
|
+
}
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function isBaseKey(text: string): boolean {
|
|
296
|
+
if (/^[a-z0-9]$/.test(text)) return true;
|
|
297
|
+
return SPECIAL_KEYS.has(text) || SYMBOL_KEYS.has(text);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** 越界夹到区间,不报错:显示比例是口味问题,不是配置错误。 */
|
|
301
|
+
function clampRatio(value: number): number {
|
|
302
|
+
return Math.min(MAX_HEIGHT_RATIO, Math.max(MIN_HEIGHT_RATIO, value));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function readGroup(value: unknown, path: string, errors: string[]): Raw {
|
|
306
|
+
if (value === undefined) return {};
|
|
307
|
+
if (!isRecord(value)) {
|
|
308
|
+
errors.push(`${path}: expected object, got ${describe(value)}`);
|
|
309
|
+
return {};
|
|
310
|
+
}
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function readString(
|
|
315
|
+
value: unknown,
|
|
316
|
+
path: string,
|
|
317
|
+
errors: string[],
|
|
318
|
+
): string | undefined {
|
|
319
|
+
if (value === undefined) return undefined;
|
|
320
|
+
if (typeof value !== "string") {
|
|
321
|
+
errors.push(`${path}: expected string, got ${describe(value)}`);
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|
|
324
|
+
return value;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function readBoolean(
|
|
328
|
+
value: unknown,
|
|
329
|
+
path: string,
|
|
330
|
+
errors: string[],
|
|
331
|
+
): boolean | undefined {
|
|
332
|
+
if (value === undefined) return undefined;
|
|
333
|
+
if (typeof value !== "boolean") {
|
|
334
|
+
errors.push(`${path}: expected boolean, got ${describe(value)}`);
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function readNumber(
|
|
341
|
+
value: unknown,
|
|
342
|
+
path: string,
|
|
343
|
+
errors: string[],
|
|
344
|
+
): number | undefined {
|
|
345
|
+
if (value === undefined) return undefined;
|
|
346
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
347
|
+
errors.push(`${path}: expected number, got ${describe(value)}`);
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
return value;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function readInteger(
|
|
354
|
+
value: unknown,
|
|
355
|
+
path: string,
|
|
356
|
+
errors: string[],
|
|
357
|
+
options: { min: number },
|
|
358
|
+
): number | undefined {
|
|
359
|
+
const number = readNumber(value, path, errors);
|
|
360
|
+
if (number === undefined) return undefined;
|
|
361
|
+
if (!Number.isInteger(number) || number < options.min) {
|
|
362
|
+
errors.push(`${path}: expected integer >= ${options.min}, got ${number}`);
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
return number;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function readStringArray(
|
|
369
|
+
value: unknown,
|
|
370
|
+
path: string,
|
|
371
|
+
errors: string[],
|
|
372
|
+
): string[] | undefined {
|
|
373
|
+
if (value === undefined) return undefined;
|
|
374
|
+
if (!Array.isArray(value)) {
|
|
375
|
+
errors.push(`${path}: expected array, got ${describe(value)}`);
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
const items: string[] = [];
|
|
379
|
+
for (const [index, item] of value.entries()) {
|
|
380
|
+
if (typeof item !== "string") {
|
|
381
|
+
errors.push(`${path}[${index}]: expected string, got ${describe(item)}`);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
items.push(item);
|
|
385
|
+
}
|
|
386
|
+
return items;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function checkKeys(
|
|
390
|
+
input: Raw,
|
|
391
|
+
path: string,
|
|
392
|
+
allowed: readonly string[],
|
|
393
|
+
errors: string[],
|
|
394
|
+
): void {
|
|
395
|
+
const prefix = path === "" ? "" : `${path}.`;
|
|
396
|
+
for (const key of Object.keys(input)) {
|
|
397
|
+
if (!allowed.includes(key)) errors.push(`${prefix}${key}: unknown key`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function isRecord(value: unknown): value is Raw {
|
|
402
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function describe(value: unknown): string {
|
|
406
|
+
if (value === null) return "null";
|
|
407
|
+
if (Array.isArray(value)) return "array";
|
|
408
|
+
return typeof value;
|
|
409
|
+
}
|