@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.
@@ -1177,6 +1177,7 @@ function isNodeError2(error) {
1177
1177
 
1178
1178
  // src/question-tool.ts
1179
1179
  var PLAN_MODE_QUESTION_TOOL_NAME = "plan_mode_question";
1180
+ var MAX_PLAN_MODE_RESPONSE_LENGTH = 4e3;
1180
1181
  var PLAN_MODE_QUESTION_PARAMS = {
1181
1182
  type: "object",
1182
1183
  additionalProperties: false,
@@ -1309,43 +1310,75 @@ async function answerPlanModeQuestions(questions, ctx, lifecycle) {
1309
1310
  return planModeQuestionAnswered(questions, answers);
1310
1311
  }
1311
1312
  async function askPlanModeQuestions(questions, ctx, shouldContinue = () => true) {
1312
- const answers = [];
1313
- for (const question of questions) {
1314
- const choices = question.options.map(formatPlanModeQuestionChoice);
1315
- const otherChoice = `${question.options.length + 1}. Other (free-form)`;
1316
- const choice = await ctx.ui.select(`${question.header}: ${question.question}`, [
1317
- ...choices,
1318
- otherChoice
1319
- ]);
1320
- if (!shouldContinue() || !choice) return void 0;
1321
- if (choice === otherChoice) {
1322
- const customAnswer = (await ctx.ui.editor(question.question, ""))?.trim();
1323
- if (!shouldContinue() || !customAnswer) return void 0;
1324
- answers.push({
1325
- id: question.id,
1326
- header: question.header,
1327
- question: question.question,
1328
- answer: customAnswer,
1329
- wasCustom: true
1330
- });
1331
- continue;
1313
+ const { runQuestionnaire, sanitizeTerminalText } = await import("@narumitw/pi-tui-kit");
1314
+ if (!shouldContinue()) return void 0;
1315
+ const runnerQuestions = questions.map((question, index) => ({
1316
+ id: String(index),
1317
+ header: displayText(question.header, `Question ${index + 1}`, sanitizeTerminalText),
1318
+ prompt: displayText(question.question, `Question ${index + 1}`, sanitizeTerminalText),
1319
+ options: question.options.map((option, optionIndex) => ({
1320
+ ...option,
1321
+ label: displayText(option.label, `Option ${optionIndex + 1}`, sanitizeTerminalText)
1322
+ }))
1323
+ }));
1324
+ const result = await runQuestionnaire(ctx, {
1325
+ questions: runnerQuestions,
1326
+ allowNotes: true,
1327
+ maxTextLength: MAX_PLAN_MODE_RESPONSE_LENGTH,
1328
+ signal: ctx.signal,
1329
+ isCurrent: shouldContinue
1330
+ });
1331
+ if (!shouldContinue()) return void 0;
1332
+ return mapQuestionnaireResult(questions, result);
1333
+ }
1334
+ function mapQuestionnaireResult(questions, result) {
1335
+ if (result.kind !== "submitted") return void 0;
1336
+ if (result.answers.length !== questions.length) {
1337
+ throw new Error("Questionnaire returned an incomplete answer set");
1338
+ }
1339
+ const answers = /* @__PURE__ */ new Map();
1340
+ for (const answer of result.answers) {
1341
+ const index = Number(answer.questionId);
1342
+ if (!Number.isSafeInteger(index) || String(index) !== answer.questionId || !questions[index]) {
1343
+ throw new Error("Questionnaire returned an answer for an unknown question");
1332
1344
  }
1333
- const optionIndex = choices.indexOf(choice);
1334
- const option = question.options[optionIndex];
1335
- if (!option) return void 0;
1336
- answers.push({
1337
- id: question.id,
1338
- header: question.header,
1339
- question: question.question,
1340
- answer: option.label,
1341
- wasCustom: false,
1342
- optionIndex: optionIndex + 1
1343
- });
1345
+ if (answers.has(index)) throw new Error("Questionnaire returned duplicate answers");
1346
+ if (answer.wasCustom) {
1347
+ if (answer.optionIndex !== void 0) {
1348
+ throw new Error("Questionnaire returned a custom answer with an option index");
1349
+ }
1350
+ if (!answer.answer.trim()) return void 0;
1351
+ } else if (answer.optionIndex === void 0 || !questions[index]?.options[answer.optionIndex - 1]) {
1352
+ throw new Error("Questionnaire returned an invalid selected option");
1353
+ }
1354
+ answers.set(index, answer);
1344
1355
  }
1345
- return answers;
1356
+ return questions.map((question, index) => {
1357
+ const answer = answers.get(index);
1358
+ if (!answer) throw new Error("Questionnaire returned an incomplete answer set");
1359
+ const answerText = answer.wasCustom ? answer.answer : question.options[(answer.optionIndex ?? 0) - 1]?.label ?? answer.answer;
1360
+ return answerFor(question, answerText, {
1361
+ wasCustom: answer.wasCustom,
1362
+ optionIndex: answer.optionIndex,
1363
+ note: answer.note
1364
+ });
1365
+ });
1346
1366
  }
1347
- function formatPlanModeQuestionChoice(option, index) {
1348
- return `${index + 1}. ${option.label}${option.description ? ` \u2014 ${option.description}` : ""}`;
1367
+ function displayText(value, fallback, sanitize) {
1368
+ const sanitized = sanitize(value);
1369
+ return sanitized.trim() ? sanitized : fallback;
1370
+ }
1371
+ function answerFor(question, answer, options) {
1372
+ const result = {
1373
+ id: question.id,
1374
+ header: question.header,
1375
+ question: question.question,
1376
+ answer,
1377
+ wasCustom: options.wasCustom
1378
+ };
1379
+ if (options.optionIndex !== void 0) result.optionIndex = options.optionIndex;
1380
+ if (options.note !== void 0) result.note = options.note;
1381
+ return result;
1349
1382
  }
1350
1383
  function planModeQuestionAnswered(questions, answers) {
1351
1384
  return {
@@ -1487,4 +1520,4 @@ export {
1487
1520
  withRequiredPlanModeTools,
1488
1521
  withoutRequiredPlanModeTools
1489
1522
  };
1490
- //# sourceMappingURL=chunk-6SDTCIHD.ts.map
1523
+ //# sourceMappingURL=chunk-H3RGWRY7.ts.map