@rol1n/ask-u-questions 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/README.md +31 -2
- package/extensions/ask-u-questions/README.md +32 -0
- package/extensions/ask-u-questions/answers.ts +53 -0
- package/extensions/ask-u-questions/dialog-ui.ts +57 -0
- package/extensions/ask-u-questions/index.ts +88 -0
- package/extensions/ask-u-questions/multi-choice.ts +260 -0
- package/extensions/ask-u-questions/results.ts +83 -0
- package/extensions/ask-u-questions/schema.ts +17 -0
- package/extensions/ask-u-questions/single-choice.ts +182 -0
- package/extensions/ask-u-questions/tool-renderers.ts +69 -0
- package/extensions/ask-u-questions/types.ts +45 -0
- package/extensions/ask-u-questions/ui-lock.ts +24 -0
- package/package.json +41 -4
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ask-u-questions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Interactive single-choice, multiple-choice, and free-text questions for Pi. Registers the `ask_user_question` tool and serializes dialogs through a shared UI lock.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@rol1n/ask-u-questions
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Also included in `@rol1n/pi-kit`. Enable one copy of the extension.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
The agent calls the tool with a question and optional choices:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"question": "Which output format should I use?",
|
|
20
|
+
"options": [
|
|
21
|
+
{ "label": "Markdown (Recommended)", "value": "markdown" },
|
|
22
|
+
{ "label": "JSON", "value": "json" }
|
|
23
|
+
],
|
|
24
|
+
"multiSelect": false
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Omit `options` or supply an empty array for free-text input. Set `multiSelect` only when several choices are expected. `details` adds supporting context; an Other choice is supplied automatically for choice questions.
|
|
29
|
+
|
|
30
|
+
## Requirements and behavior
|
|
31
|
+
|
|
32
|
+
Use an interactive Pi terminal session for dialogs. Results distinguish answered, cancelled, and unavailable states, so a cancelled dialog is not an approval. No API credentials or package-specific settings are required.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# ask-u-questions
|
|
2
|
+
|
|
3
|
+
Interactive single-choice, multiple-choice, and free-text questions for Pi. Registers the `ask_user_question` tool and serializes dialogs through a shared UI lock.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@rol1n/ask-u-questions
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Also included in `@rol1n/pi-kit`. Enable one copy of the extension.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
The agent calls the tool with a question and optional choices:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"question": "Which output format should I use?",
|
|
20
|
+
"options": [
|
|
21
|
+
{ "label": "Markdown (Recommended)", "value": "markdown" },
|
|
22
|
+
{ "label": "JSON", "value": "json" }
|
|
23
|
+
],
|
|
24
|
+
"multiSelect": false
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Omit `options` or supply an empty array for free-text input. Set `multiSelect` only when several choices are expected. `details` adds supporting context; an Other choice is supplied automatically for choice questions.
|
|
29
|
+
|
|
30
|
+
## Requirements and behavior
|
|
31
|
+
|
|
32
|
+
Use an interactive Pi terminal session for dialogs. Results distinguish answered, cancelled, and unavailable states, so a cancelled dialog is not an approval. No API credentials or package-specific settings are required.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { AskAnswer, AskOption } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
export function normalizeOptions(
|
|
4
|
+
options: Array<{ label: string; value?: string; description?: string }> | undefined,
|
|
5
|
+
): AskOption[] {
|
|
6
|
+
return (options || [])
|
|
7
|
+
.map((option) => ({
|
|
8
|
+
label: option.label.trim(),
|
|
9
|
+
value: option.value?.trim() || option.label.trim(),
|
|
10
|
+
description: option.description?.trim() || undefined,
|
|
11
|
+
}))
|
|
12
|
+
.filter((option) => option.label.length > 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isChineseContext(
|
|
16
|
+
question: string,
|
|
17
|
+
options?: Array<{ label?: string; description?: string }>,
|
|
18
|
+
context?: string,
|
|
19
|
+
): boolean {
|
|
20
|
+
if (/[\u4e00-\u9fa5]/.test(question)) return true;
|
|
21
|
+
if (context && /[\u4e00-\u9fa5]/.test(context)) return true;
|
|
22
|
+
if (options && options.some((o) => (o.label && /[\u4e00-\u9fa5]/.test(o.label)) || (o.description && /[\u4e00-\u9fa5]/.test(o.description)))) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getOtherLabel(options: AskOption[], isZh = false): string {
|
|
29
|
+
if (isZh) {
|
|
30
|
+
return options.some((option) => option.label === "其他(自定义输入)")
|
|
31
|
+
? "自定义输入"
|
|
32
|
+
: "其他(自定义输入)";
|
|
33
|
+
}
|
|
34
|
+
return options.some((option) => option.label === "Other (custom input)")
|
|
35
|
+
? "Custom input"
|
|
36
|
+
: "Other (custom input)";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function answerSortRank(answer: AskAnswer): number {
|
|
40
|
+
switch (answer.type) {
|
|
41
|
+
case "option":
|
|
42
|
+
return answer.index;
|
|
43
|
+
case "other":
|
|
44
|
+
return Number.MAX_SAFE_INTEGER - 1;
|
|
45
|
+
case "text":
|
|
46
|
+
return Number.MAX_SAFE_INTEGER;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function sortAnswers(answers: AskAnswer[]): AskAnswer[] {
|
|
51
|
+
return [...answers].sort((a, b) => answerSortRank(a) - answerSortRank(b));
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type EditorTheme, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
export function createEditorTheme(theme: Theme): EditorTheme {
|
|
5
|
+
return {
|
|
6
|
+
borderColor: (s) => theme.fg("accent", s),
|
|
7
|
+
selectList: {
|
|
8
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
9
|
+
selectedText: (t) => theme.fg("accent", t),
|
|
10
|
+
description: (t) => theme.fg("muted", t),
|
|
11
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
12
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function addWrapped(
|
|
18
|
+
lines: string[],
|
|
19
|
+
text: string,
|
|
20
|
+
width: number,
|
|
21
|
+
firstIndent = " ",
|
|
22
|
+
restIndent = firstIndent,
|
|
23
|
+
styleFn?: (line: string) => string,
|
|
24
|
+
): void {
|
|
25
|
+
if (!text) return;
|
|
26
|
+
const paragraphs = text.split(/\r?\n/);
|
|
27
|
+
for (let pIdx = 0; pIdx < paragraphs.length; pIdx++) {
|
|
28
|
+
const paragraph = paragraphs[pIdx];
|
|
29
|
+
if (paragraph.length === 0) {
|
|
30
|
+
lines.push("");
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const isFirstParagraph = pIdx === 0;
|
|
34
|
+
const currentFirstIndent = isFirstParagraph ? firstIndent : restIndent;
|
|
35
|
+
const firstWidth = Math.max(1, width - visibleWidth(currentFirstIndent));
|
|
36
|
+
const restWidth = Math.max(1, width - visibleWidth(restIndent));
|
|
37
|
+
|
|
38
|
+
if (firstWidth === restWidth) {
|
|
39
|
+
const wrapped = wrapTextWithAnsi(paragraph, firstWidth);
|
|
40
|
+
for (let i = 0; i < wrapped.length; i++) {
|
|
41
|
+
const indent = isFirstParagraph && i === 0 ? firstIndent : restIndent;
|
|
42
|
+
const rawLine = wrapped[i];
|
|
43
|
+
const styled = styleFn ? styleFn(rawLine) : rawLine;
|
|
44
|
+
lines.push(truncateToWidth(`${indent}${styled}`, width));
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
const wrapped = wrapTextWithAnsi(paragraph, Math.min(firstWidth, restWidth));
|
|
48
|
+
for (let i = 0; i < wrapped.length; i++) {
|
|
49
|
+
const indent = isFirstParagraph && i === 0 ? firstIndent : restIndent;
|
|
50
|
+
const rawLine = wrapped[i];
|
|
51
|
+
const styled = styleFn ? styleFn(rawLine) : rawLine;
|
|
52
|
+
lines.push(truncateToWidth(`${indent}${styled}`, width));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** Interactive questions, serialized through the shared dialog lock. */
|
|
2
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { AskUserQuestionMode } from "./types.ts";
|
|
4
|
+
import { AskUserQuestionParams } from "./schema.ts";
|
|
5
|
+
import { normalizeOptions } from "./answers.ts";
|
|
6
|
+
import { buildResult, cancelledResult, unavailableResult } from "./results.ts";
|
|
7
|
+
import { askSingleChoice } from "./single-choice.ts";
|
|
8
|
+
import { askMultiChoice } from "./multi-choice.ts";
|
|
9
|
+
import { withUILock } from "./ui-lock.ts";
|
|
10
|
+
import { questionRenderers } from "./tool-renderers.ts";
|
|
11
|
+
|
|
12
|
+
export type { AskOption, TextAnswer, OptionAnswer, OtherAnswer, AskAnswer, AskUserQuestionStatus, AskUserQuestionMode, AskUserQuestionResultDetails } from "./types.ts";
|
|
13
|
+
|
|
14
|
+
export default function (pi: ExtensionAPI) {
|
|
15
|
+
pi.registerTool({
|
|
16
|
+
name: "ask_user_question",
|
|
17
|
+
label: "Ask User Question",
|
|
18
|
+
description:
|
|
19
|
+
"Ask the user a clear question and pause execution until they select an option or provide input. Useful when requirements are ambiguous, user preferences or architectural decisions are needed, or critical operations require explicit user confirmation. Always phrase the question and options in the same language as the conversation context. Ask exactly one focused question per call.",
|
|
20
|
+
promptSnippet:
|
|
21
|
+
"Ask the user a question to clarify requirements or gather decisions instead of making assumptions.",
|
|
22
|
+
promptGuidelines: [
|
|
23
|
+
"Always phrase the question and options in the same language as the conversation context (e.g. ask in Chinese if conversing in Chinese, ask in English if conversing in English).",
|
|
24
|
+
"Ask exactly one specific question per tool call.",
|
|
25
|
+
"If you need input across multiple distinct dimensions, make separate tool calls instead of combining them into one.",
|
|
26
|
+
"When options are provided, an 'Other (custom input)' choice is automatically available in the user interface.",
|
|
27
|
+
"Set multiSelect: true only when the user is expected to select multiple choices.",
|
|
28
|
+
'Put the recommended option first in the list and append "(Recommended)" to its label.',
|
|
29
|
+
"Prefer using this tool whenever multiple valid implementation paths exist that depend on user preference.",
|
|
30
|
+
],
|
|
31
|
+
parameters: AskUserQuestionParams,
|
|
32
|
+
|
|
33
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
34
|
+
const options = normalizeOptions(params.options);
|
|
35
|
+
const context = params.details?.trim() || undefined;
|
|
36
|
+
const mode: AskUserQuestionMode =
|
|
37
|
+
options.length === 0
|
|
38
|
+
? "text"
|
|
39
|
+
: params.multiSelect
|
|
40
|
+
? "multi-select"
|
|
41
|
+
: "single-select";
|
|
42
|
+
|
|
43
|
+
if (signal?.aborted) {
|
|
44
|
+
return cancelledResult(params.question, mode, context);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!ctx.hasUI) {
|
|
48
|
+
return unavailableResult(
|
|
49
|
+
params.question,
|
|
50
|
+
mode,
|
|
51
|
+
"ask_user_question requires an interactive TUI environment",
|
|
52
|
+
context,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return withUILock(async () => {
|
|
57
|
+
if (mode === "text") {
|
|
58
|
+
const editorTitle = context
|
|
59
|
+
? `${params.question}\n\n${context}`
|
|
60
|
+
: params.question;
|
|
61
|
+
const answer = await ctx.ui.editor(editorTitle);
|
|
62
|
+
if (answer === undefined) {
|
|
63
|
+
return cancelledResult(params.question, mode, context);
|
|
64
|
+
}
|
|
65
|
+
return buildResult(params.question, context, mode, [
|
|
66
|
+
{ type: "text", label: answer.trim(), value: answer.trim() },
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (mode === "single-select") {
|
|
71
|
+
const answer = await askSingleChoice(ctx, params.question, context, options);
|
|
72
|
+
if (!answer) {
|
|
73
|
+
return cancelledResult(params.question, mode, context);
|
|
74
|
+
}
|
|
75
|
+
return buildResult(params.question, context, mode, [answer]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const answers = await askMultiChoice(ctx, params.question, context, options);
|
|
79
|
+
if (!answers) {
|
|
80
|
+
return cancelledResult(params.question, mode, context);
|
|
81
|
+
}
|
|
82
|
+
return buildResult(params.question, context, mode, answers);
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
...questionRenderers,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Editor, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { AskAnswer, AskOption, DisplayOption } from "./types.ts";
|
|
4
|
+
import { getOtherLabel, isChineseContext, sortAnswers } from "./answers.ts";
|
|
5
|
+
import { addWrapped, createEditorTheme } from "./dialog-ui.ts";
|
|
6
|
+
|
|
7
|
+
export async function askMultiChoice(
|
|
8
|
+
ctx: ExtensionContext,
|
|
9
|
+
question: string,
|
|
10
|
+
context: string | undefined,
|
|
11
|
+
options: AskOption[],
|
|
12
|
+
): Promise<AskAnswer[] | null> {
|
|
13
|
+
const isZh = isChineseContext(question, options, context);
|
|
14
|
+
const otherLabel = getOtherLabel(options, isZh);
|
|
15
|
+
const choiceItems: DisplayOption[] = options.map((option, index) => ({
|
|
16
|
+
...option,
|
|
17
|
+
id: `option:${index}`,
|
|
18
|
+
index: index + 1,
|
|
19
|
+
}));
|
|
20
|
+
const submitItem: DisplayOption = {
|
|
21
|
+
id: "submit",
|
|
22
|
+
label: isZh ? "提交确认" : "Confirm & Submit",
|
|
23
|
+
value: "__submit__",
|
|
24
|
+
isSubmit: true,
|
|
25
|
+
};
|
|
26
|
+
const allItems: DisplayOption[] = [
|
|
27
|
+
...choiceItems,
|
|
28
|
+
{ id: "other", label: otherLabel, value: "__other__", isOther: true },
|
|
29
|
+
submitItem,
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
return ctx.ui.custom<AskAnswer[] | null>(
|
|
33
|
+
(tui, theme, _kb, done: (result: AskAnswer[] | null) => void) => {
|
|
34
|
+
let optionIndex = 0;
|
|
35
|
+
let editMode = false;
|
|
36
|
+
let cachedLines: string[] | undefined;
|
|
37
|
+
let cachedWidth = -1;
|
|
38
|
+
let componentFocused = false;
|
|
39
|
+
const selected = new Map<string, AskAnswer>();
|
|
40
|
+
const editor = new Editor(tui, createEditorTheme(theme));
|
|
41
|
+
|
|
42
|
+
editor.onSubmit = (value) => {
|
|
43
|
+
const trimmed = value.trim();
|
|
44
|
+
if (!trimmed) return;
|
|
45
|
+
selected.set("other", { type: "other", label: trimmed, value: trimmed });
|
|
46
|
+
editMode = false;
|
|
47
|
+
editor.focused = false;
|
|
48
|
+
refresh();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function refresh() {
|
|
52
|
+
cachedLines = undefined;
|
|
53
|
+
tui.requestRender();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function toggleOption(item: DisplayOption) {
|
|
57
|
+
if (selected.has(item.id)) {
|
|
58
|
+
selected.delete(item.id);
|
|
59
|
+
} else {
|
|
60
|
+
selected.set(item.id, {
|
|
61
|
+
type: "option",
|
|
62
|
+
label: item.label,
|
|
63
|
+
value: item.value,
|
|
64
|
+
index: item.index!,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
refresh();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function handleInput(data: string) {
|
|
71
|
+
if (editMode) {
|
|
72
|
+
if (matchesKey(data, Key.escape)) {
|
|
73
|
+
editMode = false;
|
|
74
|
+
editor.focused = false;
|
|
75
|
+
editor.setText(selected.get("other")?.label || "");
|
|
76
|
+
refresh();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
editor.handleInput(data);
|
|
80
|
+
refresh();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (matchesKey(data, Key.up)) {
|
|
85
|
+
optionIndex = Math.max(0, optionIndex - 1);
|
|
86
|
+
refresh();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (matchesKey(data, Key.down)) {
|
|
90
|
+
optionIndex = Math.min(allItems.length - 1, optionIndex + 1);
|
|
91
|
+
refresh();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const current = allItems[optionIndex];
|
|
96
|
+
if (matchesKey(data, Key.space)) {
|
|
97
|
+
if (current.isSubmit) return;
|
|
98
|
+
if (current.isOther) {
|
|
99
|
+
if (selected.has("other")) {
|
|
100
|
+
selected.delete("other");
|
|
101
|
+
refresh();
|
|
102
|
+
} else {
|
|
103
|
+
editMode = true;
|
|
104
|
+
editor.focused = componentFocused;
|
|
105
|
+
editor.setText("");
|
|
106
|
+
refresh();
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
toggleOption(current);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (matchesKey(data, Key.enter)) {
|
|
115
|
+
if (current.isSubmit) {
|
|
116
|
+
if (selected.size > 0) {
|
|
117
|
+
done(sortAnswers(Array.from(selected.values())));
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (current.isOther) {
|
|
122
|
+
editMode = true;
|
|
123
|
+
editor.focused = componentFocused;
|
|
124
|
+
editor.setText(selected.get("other")?.label || "");
|
|
125
|
+
refresh();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
toggleOption(current);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (matchesKey(data, Key.escape)) {
|
|
133
|
+
done(null);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function render(width: number): string[] {
|
|
138
|
+
if (cachedLines && cachedWidth === width) return cachedLines;
|
|
139
|
+
|
|
140
|
+
const lines: string[] = [];
|
|
141
|
+
const add = (text: string) => lines.push(truncateToWidth(text, width));
|
|
142
|
+
|
|
143
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
144
|
+
|
|
145
|
+
if (editMode) {
|
|
146
|
+
addWrapped(lines, question, width, " ", " ", (l) => theme.fg("text", theme.bold(l)));
|
|
147
|
+
if (context) {
|
|
148
|
+
addWrapped(lines, context, width, " ", " ", (l) => theme.fg("muted", l));
|
|
149
|
+
}
|
|
150
|
+
lines.push("");
|
|
151
|
+
const header = allItems[optionIndex]?.label || otherLabel;
|
|
152
|
+
add(theme.fg("accent", ` > ${header}:`));
|
|
153
|
+
for (const line of editor.render(Math.max(1, width - 4))) {
|
|
154
|
+
add(` ${line}`);
|
|
155
|
+
}
|
|
156
|
+
lines.push("");
|
|
157
|
+
const editHint = isZh
|
|
158
|
+
? " Enter 保存 • Esc 返回多选"
|
|
159
|
+
: " Enter to save • Esc to return";
|
|
160
|
+
add(theme.fg("dim", editHint));
|
|
161
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
162
|
+
cachedLines = lines;
|
|
163
|
+
cachedWidth = width;
|
|
164
|
+
return lines;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
addWrapped(lines, question, width, " ", " ", (l) => theme.fg("text", theme.bold(l)));
|
|
168
|
+
if (context) {
|
|
169
|
+
addWrapped(lines, context, width, " ", " ", (l) => theme.fg("muted", l));
|
|
170
|
+
}
|
|
171
|
+
lines.push("");
|
|
172
|
+
|
|
173
|
+
for (let i = 0; i < allItems.length; i++) {
|
|
174
|
+
const item = allItems[i];
|
|
175
|
+
const isFocused = i === optionIndex;
|
|
176
|
+
const prefix = isFocused ? theme.fg("accent", " > ") : " ";
|
|
177
|
+
|
|
178
|
+
if (item.isSubmit) {
|
|
179
|
+
const hasSelected = selected.size > 0;
|
|
180
|
+
const countText = isZh
|
|
181
|
+
? `(已选 ${selected.size} 项)`
|
|
182
|
+
: `(${selected.size} selected)`;
|
|
183
|
+
const label = hasSelected
|
|
184
|
+
? `✓ ${item.label} ${countText}`
|
|
185
|
+
: `○ ${item.label}`;
|
|
186
|
+
const styled = isFocused
|
|
187
|
+
? theme.bold(theme.fg("accent", label))
|
|
188
|
+
: theme.fg(hasSelected ? "success" : "dim", label);
|
|
189
|
+
lines.push(truncateToWidth(`${prefix}${styled}`, width));
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (item.isOther) {
|
|
194
|
+
const other = selected.get("other");
|
|
195
|
+
const marker = other ? "[x]" : "[ ]";
|
|
196
|
+
const suffix = other ? ` — ${other.label}` : "";
|
|
197
|
+
const label = `${marker} ${item.label}${suffix}`;
|
|
198
|
+
const styled = isFocused
|
|
199
|
+
? theme.bold(theme.fg("accent", label))
|
|
200
|
+
: theme.fg(other ? "success" : "text", label);
|
|
201
|
+
lines.push(truncateToWidth(`${prefix}${styled}`, width));
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const checked = selected.has(item.id);
|
|
206
|
+
const marker = checked ? "[x] " : "[ ] ";
|
|
207
|
+
const numPrefix = `${item.index}. `;
|
|
208
|
+
const firstIndent = `${prefix}${checked ? theme.fg("success", marker) : marker}${numPrefix}`;
|
|
209
|
+
const restIndent = " ".repeat(visibleWidth(firstIndent));
|
|
210
|
+
const styleLabel = (l: string) => {
|
|
211
|
+
if (isFocused) return theme.bold(theme.fg("accent", l));
|
|
212
|
+
if (checked) return theme.fg("success", l);
|
|
213
|
+
return theme.fg("text", l);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
addWrapped(lines, item.label, width, firstIndent, restIndent, styleLabel);
|
|
217
|
+
|
|
218
|
+
if (item.description) {
|
|
219
|
+
const descIndent = " ".repeat(visibleWidth(firstIndent));
|
|
220
|
+
const descStyle = (l: string) => theme.fg("muted", l);
|
|
221
|
+
addWrapped(lines, item.description, width, descIndent, descIndent, descStyle);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
lines.push("");
|
|
226
|
+
if (selected.size === 0) {
|
|
227
|
+
const warnText = isZh
|
|
228
|
+
? " 请至少选择一项回答后再提交。"
|
|
229
|
+
: " Please select at least one option before submitting.";
|
|
230
|
+
add(theme.fg("warning", warnText));
|
|
231
|
+
}
|
|
232
|
+
const navHint = isZh
|
|
233
|
+
? " ↑↓ 切换 • 空格 勾选 • Enter 编辑/提交 • Esc 取消"
|
|
234
|
+
: " ↑↓ to navigate • Space to select • Enter to edit/submit • Esc to cancel";
|
|
235
|
+
add(theme.fg("dim", navHint));
|
|
236
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
237
|
+
|
|
238
|
+
cachedLines = lines;
|
|
239
|
+
cachedWidth = width;
|
|
240
|
+
return lines;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
render,
|
|
245
|
+
invalidate: () => {
|
|
246
|
+
cachedLines = undefined;
|
|
247
|
+
},
|
|
248
|
+
handleInput,
|
|
249
|
+
get focused(): boolean {
|
|
250
|
+
return componentFocused;
|
|
251
|
+
},
|
|
252
|
+
set focused(value: boolean) {
|
|
253
|
+
componentFocused = value;
|
|
254
|
+
editor.focused = editMode && componentFocused;
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { AskAnswer, AskUserQuestionMode, AskUserQuestionResultDetails, AskUserQuestionStatus } from "./types.ts";
|
|
2
|
+
import { isChineseContext } from "./answers.ts";
|
|
3
|
+
|
|
4
|
+
function formatAnswerForModel(answer: AskAnswer): string {
|
|
5
|
+
switch (answer.type) {
|
|
6
|
+
case "text":
|
|
7
|
+
return answer.label;
|
|
8
|
+
case "other":
|
|
9
|
+
return `Other: ${answer.label}`;
|
|
10
|
+
case "option":
|
|
11
|
+
return `${answer.index}. ${answer.label}`;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function buildStructuredResult(
|
|
16
|
+
status: AskUserQuestionStatus,
|
|
17
|
+
question: string,
|
|
18
|
+
mode: AskUserQuestionMode,
|
|
19
|
+
answers: AskAnswer[],
|
|
20
|
+
context?: string,
|
|
21
|
+
message?: string,
|
|
22
|
+
): AskUserQuestionResultDetails {
|
|
23
|
+
return {
|
|
24
|
+
status,
|
|
25
|
+
question,
|
|
26
|
+
context,
|
|
27
|
+
mode,
|
|
28
|
+
answers,
|
|
29
|
+
message,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function cancelledResult(question: string, mode: AskUserQuestionMode, context?: string) {
|
|
34
|
+
const isZh = isChineseContext(question, undefined, context);
|
|
35
|
+
const message = isZh ? "用户取消了本次提问" : "User cancelled the question.";
|
|
36
|
+
return {
|
|
37
|
+
content: [{ type: "text" as const, text: message }],
|
|
38
|
+
details: buildStructuredResult("cancelled", question, mode, [], context, message),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function unavailableResult(question: string, mode: AskUserQuestionMode, message: string, context?: string) {
|
|
43
|
+
return {
|
|
44
|
+
content: [{ type: "text" as const, text: message }],
|
|
45
|
+
details: buildStructuredResult("unavailable", question, mode, [], context, message),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function buildResult(
|
|
50
|
+
question: string,
|
|
51
|
+
context: string | undefined,
|
|
52
|
+
mode: AskUserQuestionMode,
|
|
53
|
+
answers: AskAnswer[],
|
|
54
|
+
) {
|
|
55
|
+
const isZh = isChineseContext(question, answers, context);
|
|
56
|
+
let text: string;
|
|
57
|
+
if (mode === "text") {
|
|
58
|
+
const answer = answers[0];
|
|
59
|
+
if (isZh) {
|
|
60
|
+
text = answer.label.trim().length > 0
|
|
61
|
+
? `用户输入回答: ${answer.label}`
|
|
62
|
+
: "用户提交了空回答";
|
|
63
|
+
} else {
|
|
64
|
+
text = answer.label.trim().length > 0
|
|
65
|
+
? `User answered: ${answer.label}`
|
|
66
|
+
: "User submitted an empty answer";
|
|
67
|
+
}
|
|
68
|
+
} else if (mode === "single-select") {
|
|
69
|
+
text = isZh
|
|
70
|
+
? `用户选择项: ${formatAnswerForModel(answers[0])}`
|
|
71
|
+
: `User selected: ${formatAnswerForModel(answers[0])}`;
|
|
72
|
+
} else {
|
|
73
|
+
text = isZh
|
|
74
|
+
? `用户选择多项:\n${answers.map((answer) => `- ${formatAnswerForModel(answer)}`).join("\n")}`
|
|
75
|
+
: `User selected multiple:\n${answers.map((answer) => `- ${formatAnswerForModel(answer)}`).join("\n")}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
content: [{ type: "text" as const, text }],
|
|
80
|
+
details: buildStructuredResult("answered", question, mode, answers, context),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
|
|
3
|
+
const OptionSchema = Type.Object({
|
|
4
|
+
label: Type.String({ description: 'Option label. Put a recommended option first and append "(Recommended)".' }),
|
|
5
|
+
value: Type.Optional(Type.String({ description: "Machine-readable value; defaults to the label." })),
|
|
6
|
+
description: Type.Optional(Type.String({ description: "Additional context shown below the option." })),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export const AskUserQuestionParams = Type.Object({
|
|
10
|
+
question: Type.String({ description: "The question to ask. Ask only one question per call." }),
|
|
11
|
+
details: Type.Optional(Type.String({ description: "Background or instructions shown below the question." })),
|
|
12
|
+
options: Type.Optional(Type.Array(OptionSchema, {
|
|
13
|
+
description: "Choices for selection. Omit or pass an empty array for free-text input. An Other choice is always available.",
|
|
14
|
+
})),
|
|
15
|
+
multiSelect: Type.Optional(Type.Boolean({ description: "Allow selecting multiple options when true." })),
|
|
16
|
+
});
|
|
17
|
+
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Editor, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { AskAnswer, AskOption, DisplayOption } from "./types.ts";
|
|
4
|
+
import { getOtherLabel, isChineseContext } from "./answers.ts";
|
|
5
|
+
import { addWrapped, createEditorTheme } from "./dialog-ui.ts";
|
|
6
|
+
|
|
7
|
+
export async function askSingleChoice(
|
|
8
|
+
ctx: ExtensionContext,
|
|
9
|
+
question: string,
|
|
10
|
+
context: string | undefined,
|
|
11
|
+
options: AskOption[],
|
|
12
|
+
): Promise<AskAnswer | null> {
|
|
13
|
+
const isZh = isChineseContext(question, options, context);
|
|
14
|
+
const otherLabel = getOtherLabel(options, isZh);
|
|
15
|
+
const allOptions: DisplayOption[] = [
|
|
16
|
+
...options.map((option, index) => ({
|
|
17
|
+
...option,
|
|
18
|
+
id: `option:${index}`,
|
|
19
|
+
index: index + 1,
|
|
20
|
+
})),
|
|
21
|
+
{ id: "other", label: otherLabel, value: "__other__", isOther: true },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
return ctx.ui.custom<AskAnswer | null>(
|
|
25
|
+
(tui, theme, _kb, done: (result: AskAnswer | null) => void) => {
|
|
26
|
+
let optionIndex = 0;
|
|
27
|
+
let editMode = false;
|
|
28
|
+
let cachedLines: string[] | undefined;
|
|
29
|
+
let cachedWidth = -1;
|
|
30
|
+
let componentFocused = false;
|
|
31
|
+
const editor = new Editor(tui, createEditorTheme(theme));
|
|
32
|
+
|
|
33
|
+
editor.onSubmit = (value) => {
|
|
34
|
+
const trimmed = value.trim();
|
|
35
|
+
if (!trimmed) return;
|
|
36
|
+
done({ type: "other", label: trimmed, value: trimmed });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function refresh() {
|
|
40
|
+
cachedLines = undefined;
|
|
41
|
+
tui.requestRender();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function handleInput(data: string) {
|
|
45
|
+
if (editMode) {
|
|
46
|
+
if (matchesKey(data, Key.escape)) {
|
|
47
|
+
editMode = false;
|
|
48
|
+
editor.focused = false;
|
|
49
|
+
editor.setText("");
|
|
50
|
+
refresh();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
editor.handleInput(data);
|
|
54
|
+
refresh();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (matchesKey(data, Key.up)) {
|
|
59
|
+
optionIndex = Math.max(0, optionIndex - 1);
|
|
60
|
+
refresh();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (matchesKey(data, Key.down)) {
|
|
64
|
+
optionIndex = Math.min(allOptions.length - 1, optionIndex + 1);
|
|
65
|
+
refresh();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (matchesKey(data, Key.enter)) {
|
|
69
|
+
const selected = allOptions[optionIndex];
|
|
70
|
+
if (selected.isOther) {
|
|
71
|
+
editMode = true;
|
|
72
|
+
editor.focused = componentFocused;
|
|
73
|
+
editor.setText("");
|
|
74
|
+
refresh();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
done({
|
|
78
|
+
type: "option",
|
|
79
|
+
label: selected.label,
|
|
80
|
+
value: selected.value,
|
|
81
|
+
index: selected.index!,
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (matchesKey(data, Key.escape)) {
|
|
86
|
+
done(null);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function render(width: number): string[] {
|
|
91
|
+
if (cachedLines && cachedWidth === width) return cachedLines;
|
|
92
|
+
|
|
93
|
+
const lines: string[] = [];
|
|
94
|
+
const add = (text: string) => lines.push(truncateToWidth(text, width));
|
|
95
|
+
|
|
96
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
97
|
+
|
|
98
|
+
if (editMode) {
|
|
99
|
+
addWrapped(lines, question, width, " ", " ", (l) => theme.fg("text", theme.bold(l)));
|
|
100
|
+
if (context) {
|
|
101
|
+
addWrapped(lines, context, width, " ", " ", (l) => theme.fg("muted", l));
|
|
102
|
+
}
|
|
103
|
+
lines.push("");
|
|
104
|
+
const header = allOptions[optionIndex]?.label || otherLabel;
|
|
105
|
+
add(theme.fg("accent", ` > ${header}:`));
|
|
106
|
+
for (const line of editor.render(Math.max(1, width - 4))) {
|
|
107
|
+
add(` ${line}`);
|
|
108
|
+
}
|
|
109
|
+
lines.push("");
|
|
110
|
+
const editHint = isZh
|
|
111
|
+
? " Enter 提交 • Esc 返回选项"
|
|
112
|
+
: " Enter to submit • Esc to return";
|
|
113
|
+
add(theme.fg("dim", editHint));
|
|
114
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
115
|
+
cachedLines = lines;
|
|
116
|
+
cachedWidth = width;
|
|
117
|
+
return lines;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
addWrapped(lines, question, width, " ", " ", (l) => theme.fg("text", theme.bold(l)));
|
|
121
|
+
if (context) {
|
|
122
|
+
addWrapped(lines, context, width, " ", " ", (l) => theme.fg("muted", l));
|
|
123
|
+
}
|
|
124
|
+
lines.push("");
|
|
125
|
+
|
|
126
|
+
for (let i = 0; i < allOptions.length; i++) {
|
|
127
|
+
const item = allOptions[i];
|
|
128
|
+
const selected = i === optionIndex;
|
|
129
|
+
const prefix = selected ? theme.fg("accent", " > ") : " ";
|
|
130
|
+
|
|
131
|
+
if (item.isOther) {
|
|
132
|
+
const styledLabel = selected
|
|
133
|
+
? theme.bold(theme.fg("accent", item.label))
|
|
134
|
+
: theme.fg("text", item.label);
|
|
135
|
+
lines.push(truncateToWidth(`${prefix}${styledLabel}`, width));
|
|
136
|
+
} else {
|
|
137
|
+
const numPrefix = `${item.index}. `;
|
|
138
|
+
const firstIndent = `${prefix}${selected ? theme.fg("accent", numPrefix) : numPrefix}`;
|
|
139
|
+
const restIndent = " ".repeat(visibleWidth(firstIndent));
|
|
140
|
+
const styleLabel = (l: string) =>
|
|
141
|
+
selected ? theme.bold(theme.fg("accent", l)) : theme.fg("text", l);
|
|
142
|
+
|
|
143
|
+
addWrapped(lines, item.label, width, firstIndent, restIndent, styleLabel);
|
|
144
|
+
|
|
145
|
+
if (item.description) {
|
|
146
|
+
const descIndent = " ".repeat(visibleWidth(firstIndent));
|
|
147
|
+
const descStyle = (l: string) => theme.fg("muted", l);
|
|
148
|
+
addWrapped(lines, item.description, width, descIndent, descIndent, descStyle);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
lines.push("");
|
|
154
|
+
const navHint = isZh
|
|
155
|
+
? " ↑↓ 选择 • Enter 确定 • Esc 取消"
|
|
156
|
+
: " ↑↓ to navigate • Enter to select • Esc to cancel";
|
|
157
|
+
add(theme.fg("dim", navHint));
|
|
158
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
159
|
+
|
|
160
|
+
cachedLines = lines;
|
|
161
|
+
cachedWidth = width;
|
|
162
|
+
return lines;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
render,
|
|
167
|
+
invalidate: () => {
|
|
168
|
+
cachedLines = undefined;
|
|
169
|
+
},
|
|
170
|
+
handleInput,
|
|
171
|
+
get focused(): boolean {
|
|
172
|
+
return componentFocused;
|
|
173
|
+
},
|
|
174
|
+
set focused(value: boolean) {
|
|
175
|
+
componentFocused = value;
|
|
176
|
+
editor.focused = editMode && componentFocused;
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { AskUserQuestionParams } from "./schema.ts";
|
|
4
|
+
import type { AskUserQuestionResultDetails } from "./types.ts";
|
|
5
|
+
import { getOtherLabel, isChineseContext, normalizeOptions } from "./answers.ts";
|
|
6
|
+
|
|
7
|
+
export const questionRenderers: Pick<ToolDefinition<typeof AskUserQuestionParams, AskUserQuestionResultDetails>, "renderCall" | "renderResult"> = {
|
|
8
|
+
renderCall(args, theme, context) {
|
|
9
|
+
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
|
|
10
|
+
const options = normalizeOptions(
|
|
11
|
+
args.options as Array<{ label: string; value?: string; description?: string }> | undefined,
|
|
12
|
+
);
|
|
13
|
+
const isZh = isChineseContext(args.question || "", options, args.details);
|
|
14
|
+
let summary =
|
|
15
|
+
theme.fg("toolTitle", theme.bold("ask_user_question ")) +
|
|
16
|
+
theme.fg("accent", `"${args.question || ""}"`);
|
|
17
|
+
if (args.multiSelect) {
|
|
18
|
+
summary += theme.fg("dim", " [multi-select]");
|
|
19
|
+
}
|
|
20
|
+
if (options.length > 0) {
|
|
21
|
+
const otherLbl = getOtherLabel(options, isZh);
|
|
22
|
+
const labels = [...options.map((option) => option.label), otherLbl].join(", ");
|
|
23
|
+
const optPrefix = isZh ? " 选项: " : " Options: ";
|
|
24
|
+
summary += `\n${theme.fg("dim", `${optPrefix}${labels}`)}`;
|
|
25
|
+
}
|
|
26
|
+
text.setText(summary);
|
|
27
|
+
return text;
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
renderResult(result, _options, theme, context) {
|
|
31
|
+
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
|
|
32
|
+
const details = result.details as AskUserQuestionResultDetails | undefined;
|
|
33
|
+
if (!details) {
|
|
34
|
+
const first = result.content?.[0];
|
|
35
|
+
text.setText(first?.type === "text" ? first.text : "");
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isZh = isChineseContext(details.question || "", details.answers, details.context);
|
|
40
|
+
|
|
41
|
+
if (details.status === "cancelled") {
|
|
42
|
+
const defaultCancelled = isZh ? "已取消" : "Cancelled";
|
|
43
|
+
text.setText(theme.fg("warning", `⊘ ${details.message || defaultCancelled}`));
|
|
44
|
+
return text;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (details.status === "unavailable") {
|
|
48
|
+
const defaultUnavailable = isZh ? "提问工具不可用" : "Tool unavailable";
|
|
49
|
+
text.setText(theme.fg("error", `! ${details.message || defaultUnavailable}`));
|
|
50
|
+
return text;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const emptyText = isZh ? "(空回答)" : "(empty)";
|
|
54
|
+
const otherPrefix = isZh ? "其他:" : "Other: ";
|
|
55
|
+
|
|
56
|
+
const lines = details.answers.map((answer) => {
|
|
57
|
+
switch (answer.type) {
|
|
58
|
+
case "text":
|
|
59
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", answer.label || emptyText)}`;
|
|
60
|
+
case "other":
|
|
61
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("muted", otherPrefix)}${theme.fg("accent", answer.label)}`;
|
|
62
|
+
case "option":
|
|
63
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", `${answer.index}. ${answer.label}`)}`;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
text.setText(lines.join("\n"));
|
|
67
|
+
return text;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface AskOption {
|
|
2
|
+
label: string;
|
|
3
|
+
value: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface DisplayOption extends AskOption {
|
|
8
|
+
id: string;
|
|
9
|
+
index?: number;
|
|
10
|
+
isOther?: boolean;
|
|
11
|
+
isSubmit?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TextAnswer {
|
|
15
|
+
type: "text";
|
|
16
|
+
label: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface OptionAnswer {
|
|
21
|
+
type: "option";
|
|
22
|
+
label: string;
|
|
23
|
+
value: string;
|
|
24
|
+
index: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface OtherAnswer {
|
|
28
|
+
type: "other";
|
|
29
|
+
label: string;
|
|
30
|
+
value: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type AskAnswer = TextAnswer | OptionAnswer | OtherAnswer;
|
|
34
|
+
export type AskUserQuestionStatus = "answered" | "cancelled" | "unavailable";
|
|
35
|
+
export type AskUserQuestionMode = "text" | "single-select" | "multi-select";
|
|
36
|
+
|
|
37
|
+
export interface AskUserQuestionResultDetails {
|
|
38
|
+
status: AskUserQuestionStatus;
|
|
39
|
+
question: string;
|
|
40
|
+
context?: string;
|
|
41
|
+
mode: AskUserQuestionMode;
|
|
42
|
+
answers: AskAnswer[];
|
|
43
|
+
message?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const SHARED_UI_LOCK_KEY = "__piSharedUiLock";
|
|
2
|
+
function getSharedUiLock(): { withLock<T>(fn: () => T | Promise<T>): Promise<T> } {
|
|
3
|
+
const g = globalThis as any;
|
|
4
|
+
if (!g[SHARED_UI_LOCK_KEY]) {
|
|
5
|
+
let chain: Promise<void> = Promise.resolve();
|
|
6
|
+
g[SHARED_UI_LOCK_KEY] = {
|
|
7
|
+
withLock<T>(fn: () => T | Promise<T>): Promise<T> {
|
|
8
|
+
const prev = chain;
|
|
9
|
+
let release: () => void;
|
|
10
|
+
chain = new Promise<void>((r) => {
|
|
11
|
+
release = r;
|
|
12
|
+
});
|
|
13
|
+
return prev.then(fn).finally(() => release!());
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return g[SHARED_UI_LOCK_KEY];
|
|
18
|
+
}
|
|
19
|
+
const sharedUiLock = getSharedUiLock();
|
|
20
|
+
|
|
21
|
+
export function withUILock<T>(fn: () => Promise<T>): Promise<T> {
|
|
22
|
+
return sharedUiLock.withLock(fn);
|
|
23
|
+
}
|
|
24
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rol1n/ask-u-questions",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "ask-u-questions extension from pi-kit",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+ssh://git@github.com/RolinShmily/pi-kit.git",
|
|
12
|
+
"directory": "extensions/ask-u-questions"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/RolinShmily/pi-kit#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/RolinShmily/pi-kit/issues"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22.19.0"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"extensions/ask-u-questions",
|
|
26
|
+
"extensions/ask-u-questions/README.md"
|
|
27
|
+
],
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@earendil-works/pi-ai": "*",
|
|
30
|
+
"@earendil-works/pi-agent-core": "*",
|
|
31
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
32
|
+
"@earendil-works/pi-tui": "*",
|
|
33
|
+
"typebox": "*"
|
|
34
|
+
},
|
|
35
|
+
"pi": {
|
|
36
|
+
"extensions": [
|
|
37
|
+
"./extensions/ask-u-questions/index.ts"
|
|
38
|
+
],
|
|
39
|
+
"skills": [],
|
|
40
|
+
"prompts": [],
|
|
41
|
+
"themes": []
|
|
42
|
+
}
|
|
43
|
+
}
|