@ftrdotdev/pi-inquiry 0.2.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/LICENSE +21 -0
- package/README.md +161 -0
- package/package.json +68 -0
- package/src/core/domain.test.ts +154 -0
- package/src/core/domain.ts +149 -0
- package/src/core/editor.test.ts +124 -0
- package/src/core/editor.ts +78 -0
- package/src/core/flow.test.ts +445 -0
- package/src/core/flow.ts +98 -0
- package/src/core/keyboard.test.ts +44 -0
- package/src/core/keyboard.ts +59 -0
- package/src/core/machine.test.ts +438 -0
- package/src/core/machine.ts +378 -0
- package/src/core/scene.test.ts +385 -0
- package/src/core/scene.ts +555 -0
- package/src/core/services.ts +81 -0
- package/src/core/text.test.ts +77 -0
- package/src/core/text.ts +207 -0
- package/src/sdk/box-parity.test.ts +76 -0
- package/src/sdk/keys.test.ts +59 -0
- package/src/sdk/keys.ts +77 -0
- package/src/sdk/painter.ts +33 -0
- package/src/sdk/pi-ui.test.ts +185 -0
- package/src/sdk/pi-ui.ts +210 -0
- package/src/sdk/schema.test.ts +67 -0
- package/src/tool/question.test.ts +70 -0
- package/src/tool/question.ts +176 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Question Tool - the single entry point for interactive questions.
|
|
3
|
+
*
|
|
4
|
+
* Asks the user one or more questions with options, plus an optional
|
|
5
|
+
* "Type something" free-text path. A single question renders a simple option
|
|
6
|
+
* list; multiple questions render a tabbed interface with a submit tab.
|
|
7
|
+
*
|
|
8
|
+
* The core logic lives in `src/core/` (pure Effect) and `src/sdk/` (pi
|
|
9
|
+
* adapter). This file only wires the tool definition to pi.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
14
|
+
import type { TSchema } from "typebox";
|
|
15
|
+
import {
|
|
16
|
+
QuestionListParamsJsonSchema,
|
|
17
|
+
decodeParams,
|
|
18
|
+
normalizeQuestions,
|
|
19
|
+
prepareQuestionArgs,
|
|
20
|
+
} from "../core/domain.ts";
|
|
21
|
+
import { runQuestionUi } from "../sdk/pi-ui.ts";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* System prompt contribution. The snippet appears in the "Available tools"
|
|
25
|
+
* section; the guidelines are appended to the Guidelines section when this
|
|
26
|
+
* tool is active. Every bullet names the tool so the model associates the
|
|
27
|
+
* guidance with it.
|
|
28
|
+
*/
|
|
29
|
+
const questionToolSystemPromptContribution = {
|
|
30
|
+
snippet:
|
|
31
|
+
"Ask the user to decide: interactive questions with selectable options or a typed answer",
|
|
32
|
+
guidelines: [
|
|
33
|
+
"Use question whenever you need the user to decide something, pick between options, or confirm a decision — instead of ending your reply with a question in plain text",
|
|
34
|
+
"Ask all open questions in one question call, with one entry per question in questions[]; a single question shows a simple option list, multiple questions show a tabbed interface with a submit tab",
|
|
35
|
+
"Keep options short: a label plus an optional one-line description, 2-5 options per question. The user can also type a free-text answer ('Type something.'), so only set allowOther: false when one of the listed options is required",
|
|
36
|
+
"Treat a cancelled question result as the user declining to answer: do not re-ask unless the answer is essential, and then ask once more in a different form",
|
|
37
|
+
],
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
const cancelledDetails = { questions: [], answers: [], cancelled: true };
|
|
41
|
+
|
|
42
|
+
type RenderableQuestion = {
|
|
43
|
+
prompt?: unknown;
|
|
44
|
+
label?: unknown;
|
|
45
|
+
id?: unknown;
|
|
46
|
+
options?: Array<{ label?: unknown }>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const questionLabel = (q: RenderableQuestion): string => {
|
|
50
|
+
if (typeof q.label === "string") return q.label;
|
|
51
|
+
if (typeof q.id === "string") return q.id;
|
|
52
|
+
return "";
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default function question(pi: ExtensionAPI) {
|
|
56
|
+
pi.registerTool({
|
|
57
|
+
name: "question",
|
|
58
|
+
label: "Question",
|
|
59
|
+
description:
|
|
60
|
+
"Ask the user one or more multiple-choice questions and get their answers back. Use whenever you need input from the user: clarifying ambiguous requirements, choosing between approaches or options, or confirming a decision. Prefer this over asking in plain text — it renders as a selectable list (single question) or a tabbed form (multiple questions) and lets the user type a custom answer.",
|
|
61
|
+
// Single source of truth: generated from the Effect schema in the core.
|
|
62
|
+
parameters: QuestionListParamsJsonSchema as TSchema,
|
|
63
|
+
promptSnippet: questionToolSystemPromptContribution.snippet,
|
|
64
|
+
promptGuidelines: [...questionToolSystemPromptContribution.guidelines],
|
|
65
|
+
executionMode: "sequential",
|
|
66
|
+
|
|
67
|
+
prepareArguments(args: unknown): unknown {
|
|
68
|
+
// Shim legacy calls from older sessions (old `question` tool shape).
|
|
69
|
+
// The schema tolerates unknown keys, so extra fields pass through.
|
|
70
|
+
return prepareQuestionArgs(args);
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
|
|
74
|
+
if (ctx.mode !== "tui") {
|
|
75
|
+
return {
|
|
76
|
+
content: [
|
|
77
|
+
{ type: "text", text: "Error: UI not available (running in non-interactive mode)" },
|
|
78
|
+
],
|
|
79
|
+
details: { ...cancelledDetails },
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let decoded;
|
|
84
|
+
try {
|
|
85
|
+
decoded = decodeParams(params);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return {
|
|
88
|
+
content: [
|
|
89
|
+
{ type: "text", text: `Error: invalid parameters: ${(error as Error).message}` },
|
|
90
|
+
],
|
|
91
|
+
details: { ...cancelledDetails },
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const questions = normalizeQuestions(decoded);
|
|
96
|
+
if (questions.length === 0) {
|
|
97
|
+
return {
|
|
98
|
+
content: [{ type: "text", text: "Error: No questions provided" }],
|
|
99
|
+
details: { ...cancelledDetails },
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const result = await runQuestionUi(ctx.ui, questions, ctx.cwd);
|
|
104
|
+
|
|
105
|
+
if (result.cancelled) {
|
|
106
|
+
return {
|
|
107
|
+
content: [{ type: "text", text: "User cancelled the questionnaire" }],
|
|
108
|
+
details: result,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const answerLines = result.answers.map((a) => {
|
|
113
|
+
const qLabel = questions.find((q) => q.id === a.id)?.label ?? a.id;
|
|
114
|
+
if (a.wasCustom) {
|
|
115
|
+
return `${qLabel}: user wrote: ${a.label}`;
|
|
116
|
+
}
|
|
117
|
+
return `${qLabel}: user selected: ${a.index}. ${a.label}`;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
content: [{ type: "text", text: answerLines.join("\n") }],
|
|
122
|
+
details: result,
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
renderCall(args: unknown, theme, _context) {
|
|
127
|
+
const qs = (args as { questions?: RenderableQuestion[] }).questions ?? [];
|
|
128
|
+
if (qs.length === 1) {
|
|
129
|
+
const q = qs[0];
|
|
130
|
+
let text =
|
|
131
|
+
theme.fg("toolTitle", theme.bold("question ")) +
|
|
132
|
+
theme.fg("muted", typeof q?.prompt === "string" ? q.prompt : "");
|
|
133
|
+
const opts = Array.isArray(q?.options) ? q.options : [];
|
|
134
|
+
if (opts.length > 0) {
|
|
135
|
+
const labels = opts.map((o) => (typeof o.label === "string" ? o.label : ""));
|
|
136
|
+
const numbered = [...labels, "Type something."].map((o, i) => `${i + 1}. ${o}`);
|
|
137
|
+
text += `\n${theme.fg("dim", ` Options: ${numbered.join(", ")}`)}`;
|
|
138
|
+
}
|
|
139
|
+
return new Text(text, 0, 0);
|
|
140
|
+
}
|
|
141
|
+
const labels = qs.map(questionLabel).join(", ");
|
|
142
|
+
let text =
|
|
143
|
+
theme.fg("toolTitle", theme.bold("question ")) +
|
|
144
|
+
theme.fg("muted", `${qs.length} questions`);
|
|
145
|
+
if (labels) {
|
|
146
|
+
text += theme.fg("dim", ` (${labels})`);
|
|
147
|
+
}
|
|
148
|
+
return new Text(text, 0, 0);
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
renderResult(result, _options, theme, _context) {
|
|
152
|
+
const details = result.details as
|
|
153
|
+
| {
|
|
154
|
+
questions?: unknown[];
|
|
155
|
+
answers?: Array<{ id: string; label: string; wasCustom: boolean; index?: number }>;
|
|
156
|
+
cancelled?: boolean;
|
|
157
|
+
}
|
|
158
|
+
| undefined;
|
|
159
|
+
if (!details || !Array.isArray(details.answers)) {
|
|
160
|
+
const text = result.content[0];
|
|
161
|
+
return new Text(text?.type === "text" ? text.text : "", 0, 0);
|
|
162
|
+
}
|
|
163
|
+
if (details.cancelled) {
|
|
164
|
+
return new Text(theme.fg("warning", "Cancelled"), 0, 0);
|
|
165
|
+
}
|
|
166
|
+
const lines = details.answers.map((a) => {
|
|
167
|
+
if (a.wasCustom) {
|
|
168
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${theme.fg("muted", "(wrote) ")}${a.label}`;
|
|
169
|
+
}
|
|
170
|
+
const display = a.index ? `${a.index}. ${a.label}` : a.label;
|
|
171
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${display}`;
|
|
172
|
+
});
|
|
173
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|