@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.
@@ -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 answers: PlanModeQuestionAnswer[] = [];
191
- for (const question of questions) {
192
- const choices = question.options.map(formatPlanModeQuestionChoice);
193
- const otherChoice = `${question.options.length + 1}. Other (free-form)`;
194
- const choice = await ctx.ui.select(`${question.header}: ${question.question}`, [
195
- ...choices,
196
- otherChoice,
197
- ]);
198
- if (!shouldContinue() || !choice) return undefined;
199
- if (choice === otherChoice) {
200
- const customAnswer = (await ctx.ui.editor(question.question, ""))?.trim();
201
- if (!shouldContinue() || !customAnswer) return undefined;
202
- answers.push({
203
- id: question.id,
204
- header: question.header,
205
- question: question.question,
206
- answer: customAnswer,
207
- wasCustom: true,
208
- });
209
- continue;
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
- const optionIndex = choices.indexOf(choice);
212
- const option = question.options[optionIndex];
213
- if (!option) return undefined;
214
- answers.push({
215
- id: question.id,
216
- header: question.header,
217
- question: question.question,
218
- answer: option.label,
219
- wasCustom: false,
220
- optionIndex: optionIndex + 1,
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
- return answers;
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 formatPlanModeQuestionChoice(option: PlanModeQuestionOption, index: number) {
227
- return `${index + 1}. ${option.label}${option.description ? ` — ${option.description}` : ""}`;
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(