@narumitw/pi-plan-mode 0.50.1 → 0.51.1
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 +239 -130
- package/dist/chunks/{chunk-6SDTCIHD.ts → chunk-H3RGWRY7.ts} +68 -35
- package/dist/chunks/{chunk-6SDTCIHD.ts.map → chunk-H3RGWRY7.ts.map} +2 -2
- package/dist/chunks/{interactive-ui-JBSBPK6S.ts → interactive-ui-GFNX5OF4.ts} +2 -2
- package/dist/index.ts +2 -2
- package/package.json +4 -4
- package/src/question-tool.ts +90 -35
- /package/dist/chunks/{interactive-ui-JBSBPK6S.ts.map → interactive-ui-GFNX5OF4.ts.map} +0 -0
package/src/question-tool.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type {
|
|
3
|
+
QuestionnaireAnswer,
|
|
4
|
+
QuestionnaireQuestion,
|
|
5
|
+
RunQuestionnaireResult,
|
|
6
|
+
} from "@narumitw/pi-tui-kit";
|
|
2
7
|
|
|
3
8
|
export const PLAN_MODE_QUESTION_TOOL_NAME = "plan_mode_question";
|
|
9
|
+
export const MAX_PLAN_MODE_RESPONSE_LENGTH = 4_000;
|
|
4
10
|
|
|
5
11
|
export type PlanModeQuestionOption = {
|
|
6
12
|
label: string;
|
|
@@ -14,13 +20,14 @@ export type PlanModeQuestion = {
|
|
|
14
20
|
options: PlanModeQuestionOption[];
|
|
15
21
|
};
|
|
16
22
|
|
|
17
|
-
type PlanModeQuestionAnswer = {
|
|
23
|
+
export type PlanModeQuestionAnswer = {
|
|
18
24
|
id: string;
|
|
19
25
|
header: string;
|
|
20
26
|
question: string;
|
|
21
27
|
answer: string;
|
|
22
28
|
wasCustom: boolean;
|
|
23
29
|
optionIndex?: number;
|
|
30
|
+
note?: string;
|
|
24
31
|
};
|
|
25
32
|
|
|
26
33
|
type PlanModeQuestionReason =
|
|
@@ -187,44 +194,92 @@ export async function askPlanModeQuestions(
|
|
|
187
194
|
ctx: ExtensionContext,
|
|
188
195
|
shouldContinue: () => boolean = () => true,
|
|
189
196
|
): Promise<PlanModeQuestionAnswer[] | undefined> {
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
197
|
+
const { runQuestionnaire, sanitizeTerminalText } = await import("@narumitw/pi-tui-kit");
|
|
198
|
+
if (!shouldContinue()) return undefined;
|
|
199
|
+
const runnerQuestions: QuestionnaireQuestion<string>[] = questions.map((question, index) => ({
|
|
200
|
+
id: String(index),
|
|
201
|
+
header: displayText(question.header, `Question ${index + 1}`, sanitizeTerminalText),
|
|
202
|
+
prompt: displayText(question.question, `Question ${index + 1}`, sanitizeTerminalText),
|
|
203
|
+
options: question.options.map((option, optionIndex) => ({
|
|
204
|
+
...option,
|
|
205
|
+
label: displayText(option.label, `Option ${optionIndex + 1}`, sanitizeTerminalText),
|
|
206
|
+
})),
|
|
207
|
+
}));
|
|
208
|
+
const result = await runQuestionnaire(ctx, {
|
|
209
|
+
questions: runnerQuestions,
|
|
210
|
+
allowNotes: true,
|
|
211
|
+
maxTextLength: MAX_PLAN_MODE_RESPONSE_LENGTH,
|
|
212
|
+
signal: ctx.signal,
|
|
213
|
+
isCurrent: shouldContinue,
|
|
214
|
+
});
|
|
215
|
+
if (!shouldContinue()) return undefined;
|
|
216
|
+
return mapQuestionnaireResult(questions, result);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function mapQuestionnaireResult(
|
|
220
|
+
questions: PlanModeQuestion[],
|
|
221
|
+
result: RunQuestionnaireResult<string>,
|
|
222
|
+
): PlanModeQuestionAnswer[] | undefined {
|
|
223
|
+
if (result.kind !== "submitted") return undefined;
|
|
224
|
+
if (result.answers.length !== questions.length) {
|
|
225
|
+
throw new Error("Questionnaire returned an incomplete answer set");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const answers = new Map<number, QuestionnaireAnswer<string>>();
|
|
229
|
+
for (const answer of result.answers) {
|
|
230
|
+
const index = Number(answer.questionId);
|
|
231
|
+
if (!Number.isSafeInteger(index) || String(index) !== answer.questionId || !questions[index]) {
|
|
232
|
+
throw new Error("Questionnaire returned an answer for an unknown question");
|
|
210
233
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
answer
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
234
|
+
if (answers.has(index)) throw new Error("Questionnaire returned duplicate answers");
|
|
235
|
+
if (answer.wasCustom) {
|
|
236
|
+
if (answer.optionIndex !== undefined) {
|
|
237
|
+
throw new Error("Questionnaire returned a custom answer with an option index");
|
|
238
|
+
}
|
|
239
|
+
if (!answer.answer.trim()) return undefined;
|
|
240
|
+
} else if (
|
|
241
|
+
answer.optionIndex === undefined ||
|
|
242
|
+
!questions[index]?.options[answer.optionIndex - 1]
|
|
243
|
+
) {
|
|
244
|
+
throw new Error("Questionnaire returned an invalid selected option");
|
|
245
|
+
}
|
|
246
|
+
answers.set(index, answer);
|
|
222
247
|
}
|
|
223
|
-
|
|
248
|
+
|
|
249
|
+
return questions.map((question, index) => {
|
|
250
|
+
const answer = answers.get(index);
|
|
251
|
+
if (!answer) throw new Error("Questionnaire returned an incomplete answer set");
|
|
252
|
+
const answerText = answer.wasCustom
|
|
253
|
+
? answer.answer
|
|
254
|
+
: (question.options[(answer.optionIndex ?? 0) - 1]?.label ?? answer.answer);
|
|
255
|
+
return answerFor(question, answerText, {
|
|
256
|
+
wasCustom: answer.wasCustom,
|
|
257
|
+
optionIndex: answer.optionIndex,
|
|
258
|
+
note: answer.note,
|
|
259
|
+
});
|
|
260
|
+
});
|
|
224
261
|
}
|
|
225
262
|
|
|
226
|
-
function
|
|
227
|
-
|
|
263
|
+
function displayText(value: string, fallback: string, sanitize: (value: string) => string): string {
|
|
264
|
+
const sanitized = sanitize(value);
|
|
265
|
+
return sanitized.trim() ? sanitized : fallback;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function answerFor(
|
|
269
|
+
question: PlanModeQuestion,
|
|
270
|
+
answer: string,
|
|
271
|
+
options: { wasCustom: boolean; optionIndex?: number; note?: string },
|
|
272
|
+
): PlanModeQuestionAnswer {
|
|
273
|
+
const result: PlanModeQuestionAnswer = {
|
|
274
|
+
id: question.id,
|
|
275
|
+
header: question.header,
|
|
276
|
+
question: question.question,
|
|
277
|
+
answer,
|
|
278
|
+
wasCustom: options.wasCustom,
|
|
279
|
+
};
|
|
280
|
+
if (options.optionIndex !== undefined) result.optionIndex = options.optionIndex;
|
|
281
|
+
if (options.note !== undefined) result.note = options.note;
|
|
282
|
+
return result;
|
|
228
283
|
}
|
|
229
284
|
|
|
230
285
|
export function planModeQuestionAnswered(
|
|
File without changes
|