@lune_99/pi-ask-question 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 +66 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/docs/question-answer-row.png +0 -0
- package/docs/question-answer-row.svg +16 -0
- package/docs/question-dock.png +0 -0
- package/docs/question-dock.svg +8 -0
- package/docs/question-panel.png +0 -0
- package/docs/question-panel.svg +58 -0
- package/package.json +62 -5
- package/src/commands/question.ts +25 -0
- package/src/core/question-delivery.ts +111 -0
- package/src/core/question-format.ts +109 -0
- package/src/core/question-notification.ts +34 -0
- package/src/core/question-types.ts +91 -0
- package/src/core/questionManager.ts +484 -0
- package/src/index.ts +142 -0
- package/src/tools/ask-user-questions.ts +184 -0
- package/src/tui/panel/question-panel-controller.ts +261 -0
- package/src/tui/panel/question-panel-renderer.ts +813 -0
- package/src/tui/panel/question-panel.ts +702 -0
- package/src/tui/preview/preview-box.ts +168 -0
- package/src/tui/preview/preview-layout.ts +116 -0
- package/src/tui/preview/preview-renderer.ts +129 -0
- package/src/tui/question-answer-message.ts +42 -0
- package/src/tui/question-dock-contribution.ts +41 -0
- package/src/tui/question-dock.ts +68 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { defineTool, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
|
+
|
|
5
|
+
import { formatPendingMessage, summarizeText } from "../core/question-format.ts";
|
|
6
|
+
import { questionManager } from "../core/questionManager.ts";
|
|
7
|
+
import { openQuestionPanel } from "../tui/panel/question-panel.ts";
|
|
8
|
+
|
|
9
|
+
import Type from "typebox";
|
|
10
|
+
|
|
11
|
+
export type AskUserQuestionsDetails =
|
|
12
|
+
| {
|
|
13
|
+
status: "pending";
|
|
14
|
+
requestId: string;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
status: "unavailable";
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The tool is asynchronous by design: it returns as soon as the panel is open, the model
|
|
22
|
+
* keeps working on what it is sure about, and the answers arrive as a follow-up message.
|
|
23
|
+
*
|
|
24
|
+
* `persist` writes the pending questions (with drafts) into the session, which is what
|
|
25
|
+
* keeps them answerable after Esc or a restart.
|
|
26
|
+
*/
|
|
27
|
+
export function askUserQuestions(persist: (ctx: ExtensionContext) => void) {
|
|
28
|
+
return defineTool({
|
|
29
|
+
name: "ask_user_questions",
|
|
30
|
+
label: "Ask User Questions",
|
|
31
|
+
description:
|
|
32
|
+
"Ask the user structured questions when their answer is needed to choose what to do next. Use this for user-owned decisions or meaningful tradeoffs that cannot be resolved from the conversation or workspace. The panel opens immediately and answers arrive later as a follow-up message.",
|
|
33
|
+
|
|
34
|
+
promptSnippet:
|
|
35
|
+
"Ask for user-owned decisions; discover facts and use reasonable defaults yourself.",
|
|
36
|
+
|
|
37
|
+
promptGuidelines: [
|
|
38
|
+
"Ask only when the answer materially changes what you should do next; inspect the conversation and workspace first, and use reasonable defaults for low-impact choices.",
|
|
39
|
+
"Ask the smallest set of questions needed. After calling the tool, continue independent work and stop only when further progress depends on the answers.",
|
|
40
|
+
"Use option previews for concrete alternatives that benefit from visual comparison - code, configuration, structured text, or UI mockups - and do not add previews to simple preference questions. The pane is titled with the option label, so a preview must not repeat it; anything shared by every option belongs in displayText instead.",
|
|
41
|
+
"Give every question a short header naming its subject ('Storage', 'Auth', 'Timeouts'); the panel shows it as the tab label. The header is required, so never number questions yourself or repeat the question text.",
|
|
42
|
+
],
|
|
43
|
+
parameters: askUserQuestionsSchema,
|
|
44
|
+
// The panel is a user interaction, not a nested-tool capability, and shared
|
|
45
|
+
// panel state must not be mutated by parallel calls.
|
|
46
|
+
exposure: "model-only",
|
|
47
|
+
executionMode: "sequential",
|
|
48
|
+
|
|
49
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
50
|
+
if (ctx.mode !== "tui") {
|
|
51
|
+
return {
|
|
52
|
+
content: [
|
|
53
|
+
{
|
|
54
|
+
type: "text" as const,
|
|
55
|
+
text: "Error: ask_user_questions needs the interactive TUI; ask your questions in plain text in this mode instead.",
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
details: { status: "unavailable" } as AskUserQuestionsDetails,
|
|
59
|
+
isError: true,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const request = questionManager.create(params.questions);
|
|
64
|
+
|
|
65
|
+
// Not awaited: the tool result is what lets the model keep working while the
|
|
66
|
+
// user reads the panel.
|
|
67
|
+
void openQuestionPanel(ctx, { onDeferred: () => persist(ctx) });
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
content: [
|
|
71
|
+
{
|
|
72
|
+
type: "text" as const,
|
|
73
|
+
text: formatPendingMessage(),
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
details: {
|
|
77
|
+
status: "pending",
|
|
78
|
+
requestId: request.id,
|
|
79
|
+
} as AskUserQuestionsDetails,
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
renderCall(args, theme, _context) {
|
|
84
|
+
const count = args.questions.length;
|
|
85
|
+
const label = `${count} question${count === 1 ? "" : "s"}`;
|
|
86
|
+
|
|
87
|
+
const lines = args.questions.map(
|
|
88
|
+
(question) => theme.fg("text", summarizeText(question.question, 60)),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return new Text(
|
|
92
|
+
theme.fg("toolTitle", theme.bold("ask_user_questions ")) + theme.fg("muted", label) +
|
|
93
|
+
(lines.length > 0 ? `\n${lines.join("\n")}` : ""),
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
renderResult(result, _options, theme, _context) {
|
|
100
|
+
const details = result.details as AskUserQuestionsDetails | undefined;
|
|
101
|
+
|
|
102
|
+
if (details?.status === "unavailable") {
|
|
103
|
+
return new Text(theme.fg("error", result.content[0]?.type === "text" ? result.content[0].text : "unavailable"), 0, 0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return new Text(
|
|
107
|
+
theme.fg("warning", "waiting for the user") +
|
|
108
|
+
theme.fg("dim", " · /question to reopen the panel"),
|
|
109
|
+
0,
|
|
110
|
+
0,
|
|
111
|
+
);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const askUserQuestionsSchema = Type.Object({
|
|
117
|
+
questions: Type.Array(
|
|
118
|
+
Type.Object({
|
|
119
|
+
question: Type.String({
|
|
120
|
+
description:
|
|
121
|
+
"A single-sentence, atomic decision prompt shown to the user. Ask exactly one thing and include enough context to answer without reading surrounding prose.",
|
|
122
|
+
}),
|
|
123
|
+
|
|
124
|
+
header: Type.String({
|
|
125
|
+
minLength: 1,
|
|
126
|
+
maxLength: 16,
|
|
127
|
+
description:
|
|
128
|
+
"Short label naming this question's subject ('Storage', 'Auth', 'Timeouts'), shown as the tab label in the panel. Always provide it, keep it to a word or two, and never number the questions.",
|
|
129
|
+
}),
|
|
130
|
+
|
|
131
|
+
displayText: Type.Optional(
|
|
132
|
+
Type.String({
|
|
133
|
+
description:
|
|
134
|
+
"Optional Markdown shown above the answer controls, the same for every option: the context or evidence the whole question needs, such as a diff, an excerpt, or the current values. It is not for generic explanation that belongs in the question or option descriptions, and anything that changes with the focused option belongs in that option's preview instead.",
|
|
135
|
+
}),
|
|
136
|
+
),
|
|
137
|
+
|
|
138
|
+
options: Type.Optional(
|
|
139
|
+
Type.Array(
|
|
140
|
+
Type.Object({
|
|
141
|
+
label: Type.String({
|
|
142
|
+
description:
|
|
143
|
+
"Short, scannable user-visible choice label, preferably 1-5 words. If an option is clearly preferred, place it first and suffix the label with '(Recommended)'.",
|
|
144
|
+
}),
|
|
145
|
+
|
|
146
|
+
description: Type.Optional(
|
|
147
|
+
Type.String({
|
|
148
|
+
description:
|
|
149
|
+
"One short sentence explaining the consequence, tradeoff, or meaning of selecting this option.",
|
|
150
|
+
}),
|
|
151
|
+
),
|
|
152
|
+
|
|
153
|
+
preview: Type.Optional(
|
|
154
|
+
Type.String({
|
|
155
|
+
description:
|
|
156
|
+
"Optional Markdown artifact for this one option, shown in a pane beside the options while it is focused and titled with its label. Use it when the choice is worth comparing rather than describing - a mockup, a code or config snippet, a concrete before/after - and keep it to roughly a dozen lines.",
|
|
157
|
+
}),
|
|
158
|
+
),
|
|
159
|
+
}),
|
|
160
|
+
{
|
|
161
|
+
minItems: 2,
|
|
162
|
+
maxItems: 4,
|
|
163
|
+
description:
|
|
164
|
+
"Two to four meaningful, mutually distinct choices. Omit the entire field for genuinely free-form input.",
|
|
165
|
+
},
|
|
166
|
+
),
|
|
167
|
+
),
|
|
168
|
+
|
|
169
|
+
multiSelect: Type.Optional(
|
|
170
|
+
Type.Boolean({
|
|
171
|
+
default: false,
|
|
172
|
+
description:
|
|
173
|
+
"Whether multiple predefined options can validly apply at the same time. Keep false for mutually exclusive choices.",
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
176
|
+
}),
|
|
177
|
+
{
|
|
178
|
+
minItems: 1,
|
|
179
|
+
maxItems: 4,
|
|
180
|
+
description:
|
|
181
|
+
"Questions presented together in one panel. Prefer one question; batch up to four only when the decisions are independent and already known to be necessary.",
|
|
182
|
+
},
|
|
183
|
+
),
|
|
184
|
+
});
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Question interaction model of the panel: the rows a question offers and the draft
|
|
3
|
+
* transitions that move between them.
|
|
4
|
+
*
|
|
5
|
+
* It works on the request's `draft` in place instead of holding state of its own, so the only
|
|
6
|
+
* copy that has to be persisted is the one already on the request. Nothing here touches the
|
|
7
|
+
* terminal: rendering the resulting draft is the renderer's job, and the panel is what turns a
|
|
8
|
+
* transition into a render request, an editor mode change or a settle.
|
|
9
|
+
*/
|
|
10
|
+
import type {
|
|
11
|
+
AskQuestion,
|
|
12
|
+
AskQuestionAnswer,
|
|
13
|
+
AskQuestionDraft,
|
|
14
|
+
AskQuestionRequest,
|
|
15
|
+
} from "../../core/question-types.ts";
|
|
16
|
+
|
|
17
|
+
/** One selectable line of the focused question, in panel order. */
|
|
18
|
+
export type QuestionRow =
|
|
19
|
+
| { kind: "option"; index: number }
|
|
20
|
+
| { kind: "custom" }
|
|
21
|
+
| { kind: "next" };
|
|
22
|
+
|
|
23
|
+
/** Rows of one question: its options, the free-form row, and the multi-select confirm row. */
|
|
24
|
+
export function questionRows(question: AskQuestion): QuestionRow[] {
|
|
25
|
+
const rows: QuestionRow[] = (question.options ?? []).map((_, index) => ({
|
|
26
|
+
kind: "option" as const,
|
|
27
|
+
index,
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
rows.push({ kind: "custom" });
|
|
31
|
+
|
|
32
|
+
// `Enter` toggles checkboxes in a multi-select question, so confirming the
|
|
33
|
+
// selection needs a row of its own.
|
|
34
|
+
if (question.multiSelect === true && (question.options?.length ?? 0) > 0) {
|
|
35
|
+
rows.push({ kind: "next" });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return rows;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Clamped row index: a restored draft may carry an `optionIndex` past the current rows. */
|
|
42
|
+
export function focusedRowIndex(rows: readonly QuestionRow[], optionIndex: number): number {
|
|
43
|
+
return Math.min(rows.length - 1, Math.max(0, optionIndex));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function focusedQuestionRow(
|
|
47
|
+
question: AskQuestion,
|
|
48
|
+
optionIndex: number,
|
|
49
|
+
): QuestionRow | undefined {
|
|
50
|
+
const rows = questionRows(question);
|
|
51
|
+
|
|
52
|
+
return rows[focusedRowIndex(rows, optionIndex)];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** True when every question has an answer; the header, the tabs and submit read it. */
|
|
56
|
+
export function areAllAnswered(
|
|
57
|
+
questions: readonly AskQuestion[],
|
|
58
|
+
draft: AskQuestionDraft,
|
|
59
|
+
): boolean {
|
|
60
|
+
return questions.every((_, index) => draft.answers[index] !== undefined);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What a number key did, so the caller knows whether advancing has to follow. */
|
|
64
|
+
export type PickOptionResult = "answer" | "toggle" | "ignored";
|
|
65
|
+
|
|
66
|
+
/** What follows an answered question: advance to the next one, or settle the request. */
|
|
67
|
+
export type AdvanceResult = "submit" | "next";
|
|
68
|
+
|
|
69
|
+
export class QuestionPanelController {
|
|
70
|
+
private readonly request: AskQuestionRequest;
|
|
71
|
+
|
|
72
|
+
constructor(request: AskQuestionRequest) {
|
|
73
|
+
this.request = request;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get draft(): AskQuestionDraft {
|
|
77
|
+
return this.request.draft;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get questions(): readonly AskQuestion[] {
|
|
81
|
+
return this.request.questions;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get isMultiQuestion(): boolean {
|
|
85
|
+
return this.questions.length > 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get isSubmitTab(): boolean {
|
|
89
|
+
return this.draft.currentIndex >= this.questions.length;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
currentQuestion(): AskQuestion | undefined {
|
|
93
|
+
return this.questions[this.draft.currentIndex];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
focusedRow(): QuestionRow | undefined {
|
|
97
|
+
const question = this.currentQuestion();
|
|
98
|
+
|
|
99
|
+
return question === undefined
|
|
100
|
+
? undefined
|
|
101
|
+
: focusedQuestionRow(question, this.draft.optionIndex);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** True while the cursor sits on the "type something" row of the current question. */
|
|
105
|
+
isCustomRowFocused(): boolean {
|
|
106
|
+
return this.focusedRow()?.kind === "custom";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
moveRow(step: number): void {
|
|
110
|
+
const question = this.currentQuestion();
|
|
111
|
+
|
|
112
|
+
if (!question) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const rows = questionRows(question);
|
|
117
|
+
|
|
118
|
+
this.draft.optionIndex = (focusedRowIndex(rows, this.draft.optionIndex) + step + rows.length)
|
|
119
|
+
% rows.length;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Jump to a tab; the submit tab is `questions.length`. Returns whether the tab changed. */
|
|
123
|
+
selectTab(index: number): boolean {
|
|
124
|
+
if (index < 0 || index > this.questions.length || index === this.draft.currentIndex) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.draft.currentIndex = index;
|
|
129
|
+
this.draft.optionIndex = 0;
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
moveTab(step: number): void {
|
|
135
|
+
const total = this.questions.length + 1;
|
|
136
|
+
const current = Math.min(total - 1, Math.max(0, this.draft.currentIndex));
|
|
137
|
+
|
|
138
|
+
this.draft.currentIndex = (current + step + total) % total;
|
|
139
|
+
this.draft.optionIndex = 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pickOption(index: number): PickOptionResult {
|
|
143
|
+
const question = this.currentQuestion();
|
|
144
|
+
|
|
145
|
+
if (!question?.options?.[index]) {
|
|
146
|
+
return "ignored";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.draft.optionIndex = index;
|
|
150
|
+
|
|
151
|
+
if (question.multiSelect === true) {
|
|
152
|
+
this.toggleOption(index);
|
|
153
|
+
return "toggle";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.answerWithOption(index);
|
|
157
|
+
|
|
158
|
+
return "answer";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
toggleFocusedRow(): void {
|
|
162
|
+
const question = this.currentQuestion();
|
|
163
|
+
|
|
164
|
+
if (!question || question.multiSelect !== true) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const row = this.focusedRow();
|
|
169
|
+
|
|
170
|
+
if (row?.kind === "option") {
|
|
171
|
+
this.toggleOption(row.index);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
toggleOption(index: number): void {
|
|
176
|
+
const question = this.currentQuestion();
|
|
177
|
+
|
|
178
|
+
if (!question?.options?.[index]) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const questionIndex = this.draft.currentIndex;
|
|
183
|
+
const answer = this.draft.answers[questionIndex];
|
|
184
|
+
const selected = new Set(answer?.selectedIndexes ?? []);
|
|
185
|
+
|
|
186
|
+
if (selected.has(index)) {
|
|
187
|
+
selected.delete(index);
|
|
188
|
+
} else {
|
|
189
|
+
selected.add(index);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const indexes = Array.from(selected).sort((left, right) => left - right);
|
|
193
|
+
|
|
194
|
+
// A written answer and selected options are mutually exclusive; the written
|
|
195
|
+
// draft itself stays in `customDrafts` so the editor can restore it.
|
|
196
|
+
this.draft.answers[questionIndex] = indexes.length > 0
|
|
197
|
+
? { selectedIndexes: indexes }
|
|
198
|
+
: undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
answerWithOption(index: number): void {
|
|
202
|
+
this.draft.answers[this.draft.currentIndex] = { selectedIndexes: [index] };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Move past the answered question; `"submit"` asks the caller to settle a single-question
|
|
207
|
+
* request instead of advancing.
|
|
208
|
+
*/
|
|
209
|
+
advanceAfterAnswer(): AdvanceResult {
|
|
210
|
+
if (this.questions.length === 1) {
|
|
211
|
+
return "submit";
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const questionIndex = this.draft.currentIndex;
|
|
215
|
+
|
|
216
|
+
this.draft.currentIndex = questionIndex < this.questions.length - 1
|
|
217
|
+
? questionIndex + 1
|
|
218
|
+
: this.questions.length;
|
|
219
|
+
this.draft.optionIndex = 0;
|
|
220
|
+
|
|
221
|
+
return "next";
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
setCustomAnswer(text: string): void {
|
|
225
|
+
const questionIndex = this.draft.currentIndex;
|
|
226
|
+
|
|
227
|
+
this.draft.customDrafts[questionIndex] = text;
|
|
228
|
+
this.draft.answers[questionIndex] = { selectedIndexes: [], customText: text };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
allAnswered(): boolean {
|
|
232
|
+
return areAllAnswered(this.questions, this.draft);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* True when the request can be submitted; otherwise the focus moves to the first gap and
|
|
237
|
+
* the caller must not settle it. Landing on the first gap is more useful than a dead Enter
|
|
238
|
+
* press.
|
|
239
|
+
*/
|
|
240
|
+
prepareSubmit(): boolean {
|
|
241
|
+
if (this.allAnswered()) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const missing = this.draft.answers.findIndex((answer) => answer === undefined);
|
|
246
|
+
|
|
247
|
+
if (missing >= 0) {
|
|
248
|
+
this.draft.currentIndex = missing;
|
|
249
|
+
this.draft.optionIndex = 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Answers in question order for the manager; complete only after `prepareSubmit`. */
|
|
256
|
+
collectAnswers(): AskQuestionAnswer[] {
|
|
257
|
+
return this.draft.answers.filter(
|
|
258
|
+
(answer): answer is AskQuestionAnswer => answer !== undefined,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|