@lune_99/pi-ask-question 0.0.0-stage → 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/CHANGELOG.md +102 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/docs/question-panel.png +0 -0
- package/package.json +62 -5
- package/src/commands/question.ts +35 -0
- package/src/core/question-delivery.ts +163 -0
- package/src/core/question-format.ts +134 -0
- package/src/core/question-types.ts +91 -0
- package/src/core/questionManager.ts +708 -0
- package/src/index.ts +171 -0
- package/src/tools/ask-user-questions.ts +270 -0
- package/src/tools/withdraw-user-questions.ts +169 -0
- package/src/tui/dock-line.ts +42 -0
- package/src/tui/panel/clipboard-paste.ts +124 -0
- package/src/tui/panel/question-panel-controller.ts +262 -0
- package/src/tui/panel/question-panel-renderer.ts +867 -0
- package/src/tui/panel/question-panel.ts +864 -0
- package/src/tui/preview/preview-box.ts +175 -0
- package/src/tui/preview/preview-layout.ts +119 -0
- package/src/tui/preview/preview-renderer.ts +134 -0
- package/src/tui/question-answer-message.ts +44 -0
- package/src/tui/question-dock-contribution.ts +81 -0
- package/src/tui/question-dock.ts +105 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { registerQuestionCommand } from "./commands/question.ts";
|
|
4
|
+
import {
|
|
5
|
+
ASK_QUESTION_ANSWER_MESSAGE,
|
|
6
|
+
reconcileQuestionDeliveries,
|
|
7
|
+
registerQuestionAnswerNotifications,
|
|
8
|
+
releaseDeliveredQuestions,
|
|
9
|
+
type QuestionDeliveryOptions,
|
|
10
|
+
} from "./core/question-delivery.ts";
|
|
11
|
+
import { QuestionManager } from "./core/questionManager.ts";
|
|
12
|
+
import { askUserQuestions } from "./tools/ask-user-questions.ts";
|
|
13
|
+
import { withdrawUserQuestions } from "./tools/withdraw-user-questions.ts";
|
|
14
|
+
import {
|
|
15
|
+
renderQuestionAnswerMessage,
|
|
16
|
+
type AskQuestionAnswerMessageDetails,
|
|
17
|
+
} from "./tui/question-answer-message.ts";
|
|
18
|
+
import { createQuestionDockContribution } from "./tui/question-dock-contribution.ts";
|
|
19
|
+
import { QuestionPanels } from "./tui/panel/question-panel.ts";
|
|
20
|
+
|
|
21
|
+
export default function (pi: ExtensionAPI): void {
|
|
22
|
+
installAskQuestion(pi);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Register the extension on `pi` and return the question manager of this load.
|
|
27
|
+
*
|
|
28
|
+
* The manager and the panel ownership belong to the load, not to the module: Pi reuses a loaded
|
|
29
|
+
* module for the sessions it starts in the same working directory, subagent runs included, and
|
|
30
|
+
* each of them owns its questions.
|
|
31
|
+
*/
|
|
32
|
+
export function installAskQuestion(pi: ExtensionAPI): QuestionManager {
|
|
33
|
+
const questionManager = new QuestionManager();
|
|
34
|
+
const panels = new QuestionPanels(questionManager);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Write the question state, and report a failure instead of throwing it.
|
|
38
|
+
*
|
|
39
|
+
* Everything that persists does a lifecycle step right after - the overlay closes, the
|
|
40
|
+
* session shuts down, the branch switches - and a throw from `appendEntry` would leave
|
|
41
|
+
* that step half done. The user still has to learn that the state will not survive a
|
|
42
|
+
* restart, so the failure is shown rather than dropped.
|
|
43
|
+
*/
|
|
44
|
+
const persist = (ctx: ExtensionContext): void => {
|
|
45
|
+
try {
|
|
46
|
+
questionManager.persist(pi);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
ctx.ui.notify(
|
|
49
|
+
`Question state could not be saved: ${error instanceof Error ? error.message : String(error)}`,
|
|
50
|
+
"error",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const dock = createQuestionDockContribution(questionManager, panels, persist);
|
|
56
|
+
|
|
57
|
+
let unsubscribeManager: (() => void) | undefined;
|
|
58
|
+
let unsubscribeNotifications: (() => void) | undefined;
|
|
59
|
+
|
|
60
|
+
const unsubscribe = (): void => {
|
|
61
|
+
unsubscribeManager?.();
|
|
62
|
+
unsubscribeManager = undefined;
|
|
63
|
+
unsubscribeNotifications?.();
|
|
64
|
+
unsubscribeNotifications = undefined;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** Report a delivery attempt that pi rejected synchronously; the outbox keeps the request. */
|
|
68
|
+
const deliveryOptions = (ctx: ExtensionContext): QuestionDeliveryOptions => ({
|
|
69
|
+
onError: (error) => {
|
|
70
|
+
ctx.ui.notify(
|
|
71
|
+
`Question answers could not be delivered: ${error instanceof Error ? error.message : String(error)}`,
|
|
72
|
+
"error",
|
|
73
|
+
);
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
pi.on("session_start", (_event, ctx) => {
|
|
78
|
+
// The previous session's listeners close over its ctx and a duplicate subscription
|
|
79
|
+
// would render and persist twice per event, so they go first - before the state
|
|
80
|
+
// changes below would wake them.
|
|
81
|
+
unsubscribe();
|
|
82
|
+
dock.detach({ retired: true });
|
|
83
|
+
|
|
84
|
+
// A session starts from the question state recorded on its branch.
|
|
85
|
+
questionManager.restore(ctx);
|
|
86
|
+
dock.attach(ctx);
|
|
87
|
+
|
|
88
|
+
unsubscribeManager = questionManager.subscribe(
|
|
89
|
+
(event) => {
|
|
90
|
+
try {
|
|
91
|
+
// `requests-cleared` is the session-boundary reset, not a state change;
|
|
92
|
+
// writing an empty snapshot there would shadow the pending questions of
|
|
93
|
+
// the branch being restored.
|
|
94
|
+
if (event.type !== "requests-cleared") {
|
|
95
|
+
persist(ctx);
|
|
96
|
+
}
|
|
97
|
+
} finally {
|
|
98
|
+
// A snapshot that failed must not also leave the dock showing stale order.
|
|
99
|
+
dock.refresh();
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
(error) => {
|
|
103
|
+
// Persistence reports its own failures; this is for a listener that threw for
|
|
104
|
+
// another reason, such as the dock render.
|
|
105
|
+
ctx.ui.notify(
|
|
106
|
+
`Question dock could not be updated: ${error instanceof Error ? error.message : String(error)}`,
|
|
107
|
+
"error",
|
|
108
|
+
);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// Registered after the persist listener: the snapshot has to exist before the
|
|
113
|
+
// answer message resumes the model.
|
|
114
|
+
unsubscribeNotifications = registerQuestionAnswerNotifications(
|
|
115
|
+
pi,
|
|
116
|
+
questionManager,
|
|
117
|
+
deliveryOptions(ctx),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
// A session can start with settled answers whose message never reached the previous
|
|
121
|
+
// one; the outbox is durable, so reconcile it against this branch. Confirmed answers
|
|
122
|
+
// are released (and persisted), the rest are attempted again.
|
|
123
|
+
reconcileQuestionDeliveries(pi, questionManager, ctx, deliveryOptions(ctx));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// A settled run has appended every answer message it accepted, so the confirmed ones leave
|
|
127
|
+
// the outbox here instead of riding along in every snapshot until the next session start.
|
|
128
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
129
|
+
releaseDeliveredQuestions(questionManager, ctx);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
133
|
+
// Drafts change without a manager event, so the final snapshot is written here.
|
|
134
|
+
persist(ctx);
|
|
135
|
+
unsubscribe();
|
|
136
|
+
dock.detach();
|
|
137
|
+
// A panel that is still up belongs to the session that ends; the reset stops it from
|
|
138
|
+
// continuing after the boundary.
|
|
139
|
+
panels.reset();
|
|
140
|
+
questionManager.clearAll();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
pi.on("session_before_tree", (_event, ctx) => {
|
|
144
|
+
persist(ctx);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
148
|
+
// The branch changed under the overlay, so a panel that is still up belongs to the
|
|
149
|
+
// branch being left; the reset stops it from continuing into this one.
|
|
150
|
+
panels.reset();
|
|
151
|
+
questionManager.clearAll();
|
|
152
|
+
questionManager.restore(ctx);
|
|
153
|
+
|
|
154
|
+
// Reconciliation reads the branch that is active now, never the one being left:
|
|
155
|
+
// branch A's undelivered answers must not be sent into branch B.
|
|
156
|
+
reconcileQuestionDeliveries(pi, questionManager, ctx, deliveryOptions(ctx));
|
|
157
|
+
|
|
158
|
+
dock.refresh();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
pi.registerTool(askUserQuestions(questionManager, panels, persist));
|
|
162
|
+
pi.registerTool(withdrawUserQuestions(questionManager));
|
|
163
|
+
registerQuestionCommand(pi, questionManager, panels, persist);
|
|
164
|
+
|
|
165
|
+
pi.registerMessageRenderer<AskQuestionAnswerMessageDetails>(
|
|
166
|
+
ASK_QUESTION_ANSWER_MESSAGE,
|
|
167
|
+
(message, _options, theme) => renderQuestionAnswerMessage(message.details, theme),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
return questionManager;
|
|
171
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { defineTool, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
|
+
|
|
5
|
+
import { committedRequestIds } from "../core/question-delivery.ts";
|
|
6
|
+
import {
|
|
7
|
+
formatAnswerMessage,
|
|
8
|
+
formatInterruptedMessage,
|
|
9
|
+
formatPendingMessage,
|
|
10
|
+
summarizeText,
|
|
11
|
+
} from "../core/question-format.ts";
|
|
12
|
+
import { toAnswerMessageQuestion, type QuestionManager } from "../core/questionManager.ts";
|
|
13
|
+
import type { AnswerMessageQuestion, AskQuestionAnswer } from "../core/question-types.ts";
|
|
14
|
+
import type { QuestionPanels } from "../tui/panel/question-panel.ts";
|
|
15
|
+
import { renderQuestionAnswerMessage } from "../tui/question-answer-message.ts";
|
|
16
|
+
|
|
17
|
+
import Type from "typebox";
|
|
18
|
+
|
|
19
|
+
export type AskUserQuestionsDetails =
|
|
20
|
+
| {
|
|
21
|
+
/** `waiting` is the partial result of a blocking call. */
|
|
22
|
+
status: "pending" | "waiting" | "interrupted";
|
|
23
|
+
requestId: string;
|
|
24
|
+
}
|
|
25
|
+
| {
|
|
26
|
+
status: "answered" | "skipped";
|
|
27
|
+
requestId: string;
|
|
28
|
+
questions: AnswerMessageQuestion[];
|
|
29
|
+
answers?: AskQuestionAnswer[];
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
status: "unavailable";
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* By default the tool does not wait: it returns as soon as the panel is open, the model keeps
|
|
37
|
+
* working on what it is sure about, and the answers arrive as a follow-up message. In block
|
|
38
|
+
* mode it waits until the user answers or skips, and returns that as its result.
|
|
39
|
+
*
|
|
40
|
+
* `persist` writes the pending questions (with drafts) into the session, which is what
|
|
41
|
+
* keeps them answerable after Esc or a restart.
|
|
42
|
+
*/
|
|
43
|
+
export function askUserQuestions(
|
|
44
|
+
manager: QuestionManager,
|
|
45
|
+
panels: QuestionPanels,
|
|
46
|
+
persist: (ctx: ExtensionContext) => void,
|
|
47
|
+
) {
|
|
48
|
+
return defineTool({
|
|
49
|
+
name: "ask_user_questions",
|
|
50
|
+
label: "Ask User Questions",
|
|
51
|
+
// Each rule lives in one place: when and how to ask in the guidelines, what a field holds
|
|
52
|
+
// in its schema description. All of it is sent with every request.
|
|
53
|
+
description:
|
|
54
|
+
"Ask the user structured questions about user-owned decisions or meaningful tradeoffs. The panel opens immediately; answers arrive later as a follow-up message, or as this tool's result in block mode.",
|
|
55
|
+
|
|
56
|
+
promptSnippet:
|
|
57
|
+
"Ask for user-owned decisions; discover facts and use reasonable defaults yourself.",
|
|
58
|
+
|
|
59
|
+
promptGuidelines: [
|
|
60
|
+
"Ask only when the answer materially changes what you do next; inspect the conversation and workspace first, and use reasonable defaults for low-impact choices.",
|
|
61
|
+
"Use ask_user_questions mode 'block' when the next step depends on the answer and no independent work is left, to confirm a hard-to-undo action (delete, push, migrate), or when the choice controls most later work. Otherwise keep 'continue': do the independent work, and stop only when progress depends on the answers.",
|
|
62
|
+
"Add option previews only for concrete alternatives worth comparing - code, configuration, structured text or UI mockups - not for simple preferences. The pane is titled with the option label, so do not repeat it; content shared by every option belongs in displayText.",
|
|
63
|
+
"Give every question a short header naming its subject ('Storage', 'Auth', 'Timeouts'); it is the tab label, so never number questions or repeat the question text.",
|
|
64
|
+
],
|
|
65
|
+
parameters: askUserQuestionsSchema,
|
|
66
|
+
// The panel is a user interaction, not a nested-tool capability, and shared
|
|
67
|
+
// panel state must not be mutated by parallel calls.
|
|
68
|
+
exposure: "model-only",
|
|
69
|
+
executionMode: "sequential",
|
|
70
|
+
|
|
71
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
72
|
+
if (ctx.mode !== "tui") {
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: "text" as const,
|
|
77
|
+
text: "Error: ask_user_questions needs the interactive TUI; ask your questions in plain text in this mode instead.",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
details: { status: "unavailable" } as AskUserQuestionsDetails,
|
|
81
|
+
isError: true,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const request = manager.create(params.questions, committedRequestIds(ctx));
|
|
86
|
+
const requestId = request.id;
|
|
87
|
+
// Registered before the panel opens, so the panel picks this request first.
|
|
88
|
+
const wait =
|
|
89
|
+
params.mode === "block" ? manager.waitForSettle(requestId, signal) : undefined;
|
|
90
|
+
|
|
91
|
+
// Not awaited: Esc closes the panel without an answer, so the overlay closing ends
|
|
92
|
+
// neither this call nor a wait.
|
|
93
|
+
void panels.open(ctx, { onDeferred: () => persist(ctx) });
|
|
94
|
+
|
|
95
|
+
if (!wait) {
|
|
96
|
+
return {
|
|
97
|
+
content: [{ type: "text" as const, text: formatPendingMessage(requestId) }],
|
|
98
|
+
details: { status: "pending", requestId } as AskUserQuestionsDetails,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
onUpdate?.({
|
|
103
|
+
content: [{ type: "text" as const, text: "Waiting for the user's answers." }],
|
|
104
|
+
details: { status: "waiting", requestId } as AskUserQuestionsDetails,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const outcome = await wait;
|
|
108
|
+
|
|
109
|
+
if (outcome.status === "interrupted") {
|
|
110
|
+
return {
|
|
111
|
+
content: [{ type: "text" as const, text: formatInterruptedMessage(requestId) }],
|
|
112
|
+
details: { status: "interrupted", requestId } as AskUserQuestionsDetails,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const settled = outcome.request;
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
content: [{ type: "text" as const, text: formatAnswerMessage(settled) }],
|
|
120
|
+
details: {
|
|
121
|
+
status: settled.status === "skipped" ? "skipped" : "answered",
|
|
122
|
+
requestId,
|
|
123
|
+
questions: settled.questions.map(toAnswerMessageQuestion),
|
|
124
|
+
answers: settled.answers,
|
|
125
|
+
} as AskUserQuestionsDetails,
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
renderCall(args, theme, _context) {
|
|
130
|
+
// Pi renders the call while the model still streams its arguments, so any field can
|
|
131
|
+
// be missing yet. A throw here would swap the card for pi's raw-argument fallback.
|
|
132
|
+
const questions = Array.isArray(args.questions) ? args.questions : [];
|
|
133
|
+
const count = questions.length;
|
|
134
|
+
const label = `${count} question${count === 1 ? "" : "s"}${args.mode === "block" ? " · block" : ""}`;
|
|
135
|
+
|
|
136
|
+
const lines = questions
|
|
137
|
+
.filter((question) => typeof question?.question === "string")
|
|
138
|
+
.map((question) => theme.fg("text", summarizeText(question.question, 60)));
|
|
139
|
+
|
|
140
|
+
return new Text(
|
|
141
|
+
theme.fg("toolTitle", theme.bold("ask_user_questions ")) +
|
|
142
|
+
theme.fg("muted", label) +
|
|
143
|
+
(lines.length > 0 ? `\n${lines.join("\n")}` : ""),
|
|
144
|
+
0,
|
|
145
|
+
0,
|
|
146
|
+
);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
renderResult(result, _options, theme, _context) {
|
|
150
|
+
const details = result.details as AskUserQuestionsDetails | undefined;
|
|
151
|
+
|
|
152
|
+
switch (details?.status) {
|
|
153
|
+
case "unavailable":
|
|
154
|
+
return new Text(
|
|
155
|
+
theme.fg(
|
|
156
|
+
"error",
|
|
157
|
+
result.content[0]?.type === "text"
|
|
158
|
+
? result.content[0].text
|
|
159
|
+
: "unavailable",
|
|
160
|
+
),
|
|
161
|
+
0,
|
|
162
|
+
0,
|
|
163
|
+
);
|
|
164
|
+
case "answered":
|
|
165
|
+
case "skipped":
|
|
166
|
+
return renderQuestionAnswerMessage(details, theme);
|
|
167
|
+
case "waiting":
|
|
168
|
+
return new Text(
|
|
169
|
+
theme.fg("warning", "the agent waits for your answer") +
|
|
170
|
+
theme.fg("dim", " · /question to reopen the panel"),
|
|
171
|
+
0,
|
|
172
|
+
0,
|
|
173
|
+
);
|
|
174
|
+
case "interrupted":
|
|
175
|
+
return new Text(
|
|
176
|
+
theme.fg("warning", "interrupted, still pending") +
|
|
177
|
+
theme.fg("dim", " · /question to answer"),
|
|
178
|
+
0,
|
|
179
|
+
0,
|
|
180
|
+
);
|
|
181
|
+
default:
|
|
182
|
+
return new Text(
|
|
183
|
+
theme.fg("warning", "waiting for the user") +
|
|
184
|
+
theme.fg("dim", " · /question to reopen the panel"),
|
|
185
|
+
0,
|
|
186
|
+
0,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const askUserQuestionsSchema = Type.Object({
|
|
194
|
+
questions: Type.Array(
|
|
195
|
+
Type.Object({
|
|
196
|
+
question: Type.String({
|
|
197
|
+
description:
|
|
198
|
+
"One atomic decision prompt in a single sentence, with enough context to answer without reading surrounding prose.",
|
|
199
|
+
}),
|
|
200
|
+
|
|
201
|
+
header: Type.String({
|
|
202
|
+
minLength: 1,
|
|
203
|
+
maxLength: 16,
|
|
204
|
+
description: "Tab label naming the question's subject in a word or two.",
|
|
205
|
+
}),
|
|
206
|
+
|
|
207
|
+
displayText: Type.Optional(
|
|
208
|
+
Type.String({
|
|
209
|
+
description:
|
|
210
|
+
"Markdown shown above the options, the same for every option: context or evidence the whole question needs, such as a diff, an excerpt or the current values.",
|
|
211
|
+
}),
|
|
212
|
+
),
|
|
213
|
+
|
|
214
|
+
options: Type.Optional(
|
|
215
|
+
Type.Array(
|
|
216
|
+
Type.Object({
|
|
217
|
+
label: Type.String({
|
|
218
|
+
description:
|
|
219
|
+
"Short, scannable choice label, preferably 1-5 words. Put a clearly preferred option first and suffix its label with '(Recommended)'.",
|
|
220
|
+
}),
|
|
221
|
+
|
|
222
|
+
description: Type.Optional(
|
|
223
|
+
Type.String({
|
|
224
|
+
description:
|
|
225
|
+
"One short sentence on the consequence or tradeoff of this option.",
|
|
226
|
+
}),
|
|
227
|
+
),
|
|
228
|
+
|
|
229
|
+
preview: Type.Optional(
|
|
230
|
+
Type.String({
|
|
231
|
+
description:
|
|
232
|
+
"Markdown for this option only, shown beside the options while it is focused. Keep it to about a dozen lines.",
|
|
233
|
+
}),
|
|
234
|
+
),
|
|
235
|
+
}),
|
|
236
|
+
{
|
|
237
|
+
minItems: 2,
|
|
238
|
+
maxItems: 4,
|
|
239
|
+
description: "Two to four distinct choices. Omit for free-form input.",
|
|
240
|
+
},
|
|
241
|
+
),
|
|
242
|
+
),
|
|
243
|
+
|
|
244
|
+
multiSelect: Type.Optional(
|
|
245
|
+
Type.Boolean({
|
|
246
|
+
default: false,
|
|
247
|
+
description:
|
|
248
|
+
"True when several options can apply at once; keep false for mutually exclusive choices.",
|
|
249
|
+
}),
|
|
250
|
+
),
|
|
251
|
+
}),
|
|
252
|
+
{
|
|
253
|
+
minItems: 1,
|
|
254
|
+
maxItems: 4,
|
|
255
|
+
description:
|
|
256
|
+
"Questions shown together in one panel. Prefer one; batch up to four only when the decisions are independent and already known to be necessary.",
|
|
257
|
+
},
|
|
258
|
+
),
|
|
259
|
+
|
|
260
|
+
mode: Type.Optional(
|
|
261
|
+
// A plain string enum: some providers reject the anyOf/const schema of a literal union.
|
|
262
|
+
Type.Unsafe<"continue" | "block">({
|
|
263
|
+
type: "string",
|
|
264
|
+
enum: ["continue", "block"],
|
|
265
|
+
default: "continue",
|
|
266
|
+
description:
|
|
267
|
+
"'continue' returns at once and the answers arrive as a follow-up message; 'block' waits and returns them as this tool's result.",
|
|
268
|
+
}),
|
|
269
|
+
),
|
|
270
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
|
+
|
|
5
|
+
import { committedRequestIds } from "../core/question-delivery.ts";
|
|
6
|
+
import { requestHeaders, singleLine, summarizeText } from "../core/question-format.ts";
|
|
7
|
+
import type { QuestionManager } from "../core/questionManager.ts";
|
|
8
|
+
|
|
9
|
+
import Type from "typebox";
|
|
10
|
+
|
|
11
|
+
export type WithdrawUserQuestionsDetails =
|
|
12
|
+
| {
|
|
13
|
+
status: "withdrawn";
|
|
14
|
+
requestId: string;
|
|
15
|
+
headers: string;
|
|
16
|
+
reason: string;
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
/** The user answered or skipped first; the answer reaches the model anyway. */
|
|
20
|
+
status: "too-late";
|
|
21
|
+
requestId: string;
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
status: "refused";
|
|
25
|
+
requestId: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Lets the agent take back its own pending question. The tool stays active at all times:
|
|
30
|
+
* switching tools on and off changes the system prompt and breaks the prompt cache.
|
|
31
|
+
*/
|
|
32
|
+
export function withdrawUserQuestions(manager: QuestionManager) {
|
|
33
|
+
return defineTool({
|
|
34
|
+
name: "withdraw_user_questions",
|
|
35
|
+
label: "Withdraw User Questions",
|
|
36
|
+
description:
|
|
37
|
+
"Withdraw your own pending ask_user_questions request by its request id when it no longer needs the user's answer.",
|
|
38
|
+
|
|
39
|
+
promptSnippet: "Withdraw your own pending question when it no longer needs the user.",
|
|
40
|
+
|
|
41
|
+
promptGuidelines: [
|
|
42
|
+
"Withdraw a pending question when a file, tool or test answered it, the user answered it in a chat message, or the plan changed so it no longer matters; to improve a question, withdraw it and ask again.",
|
|
43
|
+
"Never withdraw because the user is slow or to decide a user-owned question yourself; if you withdraw and use a default, say which and why.",
|
|
44
|
+
],
|
|
45
|
+
parameters: withdrawUserQuestionsSchema,
|
|
46
|
+
// It closes the shared panel, so it runs alone like `ask_user_questions`.
|
|
47
|
+
exposure: "model-only",
|
|
48
|
+
executionMode: "sequential",
|
|
49
|
+
|
|
50
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
51
|
+
const requestId = params.requestId.trim();
|
|
52
|
+
const reason = singleLine(params.reason);
|
|
53
|
+
const outcome = manager.withdraw(requestId, reason);
|
|
54
|
+
|
|
55
|
+
if (outcome.status === "withdrawn") {
|
|
56
|
+
return {
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: "text" as const,
|
|
60
|
+
text: `Withdrew request ${requestId}. The user will not answer it.`,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
details: {
|
|
64
|
+
status: "withdrawn",
|
|
65
|
+
requestId,
|
|
66
|
+
headers: requestHeaders(outcome.request),
|
|
67
|
+
reason,
|
|
68
|
+
} as WithdrawUserQuestionsDetails,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (outcome.status === "waiting") {
|
|
73
|
+
return refused(
|
|
74
|
+
requestId,
|
|
75
|
+
`Request ${requestId} cannot be withdrawn: a blocking ask_user_questions call waits on it.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// A settled request leaves the outbox once its message is on the branch, so the
|
|
80
|
+
// branch is what still knows about it.
|
|
81
|
+
const inConversation = committedRequestIds(ctx).has(requestId);
|
|
82
|
+
|
|
83
|
+
if (outcome.status === "unknown" && !inConversation) {
|
|
84
|
+
return refused(requestId, `No pending question request has id ${requestId}.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const settled =
|
|
88
|
+
outcome.status !== "settled"
|
|
89
|
+
? "answered or skipped"
|
|
90
|
+
: outcome.request.status === "skipped"
|
|
91
|
+
? "skipped"
|
|
92
|
+
: "answered";
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
content: [
|
|
96
|
+
{
|
|
97
|
+
type: "text" as const,
|
|
98
|
+
text: [
|
|
99
|
+
`Too late: the user already ${settled} request ${requestId}.`,
|
|
100
|
+
inConversation
|
|
101
|
+
? "The answer message is already in the conversation."
|
|
102
|
+
: "The answer message will arrive as a follow-up message.",
|
|
103
|
+
].join(" "),
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
details: { status: "too-late", requestId } as WithdrawUserQuestionsDetails,
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
renderCall(args, theme, _context) {
|
|
111
|
+
// Pi renders the call while the model still streams its arguments.
|
|
112
|
+
const requestId = typeof args.requestId === "string" ? args.requestId : "";
|
|
113
|
+
|
|
114
|
+
return new Text(
|
|
115
|
+
theme.fg("toolTitle", theme.bold("withdraw_user_questions ")) +
|
|
116
|
+
theme.fg("muted", summarizeText(requestId, 40)),
|
|
117
|
+
0,
|
|
118
|
+
0,
|
|
119
|
+
);
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
renderResult(result, _options, theme, _context) {
|
|
123
|
+
const details = result.details as WithdrawUserQuestionsDetails | undefined;
|
|
124
|
+
const text = result.content[0]?.type === "text" ? result.content[0].text : "";
|
|
125
|
+
|
|
126
|
+
if (details?.status === "withdrawn") {
|
|
127
|
+
return new Text(
|
|
128
|
+
theme.bold(theme.fg("warning", "Question withdrawn")) +
|
|
129
|
+
theme.fg(
|
|
130
|
+
"muted",
|
|
131
|
+
[details.headers, details.reason]
|
|
132
|
+
.filter((part) => part.length > 0)
|
|
133
|
+
.map((part) => ` · ${summarizeText(part, 60)}`)
|
|
134
|
+
.join(""),
|
|
135
|
+
),
|
|
136
|
+
0,
|
|
137
|
+
0,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return new Text(
|
|
142
|
+
theme.fg(details?.status === "too-late" ? "warning" : "error", text),
|
|
143
|
+
0,
|
|
144
|
+
0,
|
|
145
|
+
);
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function refused(requestId: string, text: string) {
|
|
151
|
+
return {
|
|
152
|
+
content: [{ type: "text" as const, text }],
|
|
153
|
+
details: { status: "refused", requestId } as WithdrawUserQuestionsDetails,
|
|
154
|
+
isError: true,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const withdrawUserQuestionsSchema = Type.Object({
|
|
159
|
+
requestId: Type.String({
|
|
160
|
+
minLength: 1,
|
|
161
|
+
description: "The request id from the ask_user_questions result.",
|
|
162
|
+
}),
|
|
163
|
+
|
|
164
|
+
reason: Type.String({
|
|
165
|
+
minLength: 1,
|
|
166
|
+
description:
|
|
167
|
+
"Short reason shown to the user, such as 'answered by the test run' or 'replaced by a clearer question'.",
|
|
168
|
+
}),
|
|
169
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
/** A part of a one-line summary; parts with a lower rank give way first when the line is too long. */
|
|
4
|
+
export interface RankedPart {
|
|
5
|
+
text: string;
|
|
6
|
+
rank: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The richest line that fits in `width`. `compose` gets the kept parts in their original order;
|
|
11
|
+
* the lowest-ranked part, the last one among equals, is left out until the line fits. So a narrow
|
|
12
|
+
* row shows fewer complete parts instead of a cut one. Undefined when not even `compose([])` fits.
|
|
13
|
+
*/
|
|
14
|
+
export function fitRanked(
|
|
15
|
+
parts: readonly RankedPart[],
|
|
16
|
+
width: number,
|
|
17
|
+
compose: (texts: readonly string[]) => string,
|
|
18
|
+
): string | undefined {
|
|
19
|
+
const kept = [...parts];
|
|
20
|
+
|
|
21
|
+
for (;;) {
|
|
22
|
+
const line = compose(kept.map((part) => part.text));
|
|
23
|
+
|
|
24
|
+
if (visibleWidth(line) <= width) {
|
|
25
|
+
return line;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (kept.length === 0) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let lowest = 0;
|
|
33
|
+
|
|
34
|
+
for (let index = 1; index < kept.length; index++) {
|
|
35
|
+
if (kept[index]!.rank <= kept[lowest]!.rank) {
|
|
36
|
+
lowest = index;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
kept.splice(lowest, 1);
|
|
41
|
+
}
|
|
42
|
+
}
|