@ftrdotdev/pi-inquiry 0.2.1 → 0.4.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/README.md +26 -4
- package/index.ts +1 -0
- package/package.json +5 -4
- package/src/core/domain.test.ts +56 -1
- package/src/core/domain.ts +21 -8
- package/src/core/flow.test.ts +43 -0
- package/src/core/machine.test.ts +157 -3
- package/src/core/machine.ts +171 -24
- package/src/core/scene.test.ts +155 -0
- package/src/core/scene.ts +94 -13
- package/src/core/text.test.ts +85 -1
- package/src/core/text.ts +92 -7
- package/src/sdk/pi-ui.test.ts +2 -0
- package/src/sdk/schema.test.ts +10 -0
- package/src/tool/question.test.ts +1 -1
- package/src/tool/question.ts +33 -15
package/src/core/machine.ts
CHANGED
|
@@ -39,6 +39,52 @@ export interface OptionChoice {
|
|
|
39
39
|
readonly isOther: boolean;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* One row in a multi-select question's option list. The selection itself
|
|
44
|
+
* lives in the question's answer array; these entries only describe what each
|
|
45
|
+
* row is so the machine and the scene stay in sync.
|
|
46
|
+
*/
|
|
47
|
+
export type MultiEntry =
|
|
48
|
+
| {
|
|
49
|
+
readonly kind: "option";
|
|
50
|
+
readonly index: number;
|
|
51
|
+
readonly label: string;
|
|
52
|
+
readonly description?: string;
|
|
53
|
+
}
|
|
54
|
+
| { readonly kind: "custom"; readonly label: string }
|
|
55
|
+
| { readonly kind: "add" };
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The rows shown on the active multi-select question's tab: the checkbox
|
|
59
|
+
* options, then any custom answers already added (as removable chips), and
|
|
60
|
+
* the "Add your own answer" entry when `allowOther` is set.
|
|
61
|
+
*/
|
|
62
|
+
export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
63
|
+
const question = state.questions[state.currentTab];
|
|
64
|
+
if (!question?.multiple) return [];
|
|
65
|
+
const answers = state.answers.get(question.id) ?? [];
|
|
66
|
+
const entries: MultiEntry[] = question.options.map((o, i) => ({
|
|
67
|
+
kind: "option",
|
|
68
|
+
index: i,
|
|
69
|
+
label: o.label,
|
|
70
|
+
...(o.description === undefined ? {} : { description: o.description }),
|
|
71
|
+
}));
|
|
72
|
+
for (const a of answers) {
|
|
73
|
+
if (a.wasCustom) entries.push({ kind: "custom", label: a.label });
|
|
74
|
+
}
|
|
75
|
+
if (question.allowOther) entries.push({ kind: "add" });
|
|
76
|
+
return entries;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** Whether a multi-select checkbox row is currently selected. */
|
|
80
|
+
export const isMultiEntrySelected = (state: MachineState, entry: MultiEntry): boolean => {
|
|
81
|
+
const question = state.questions[state.currentTab];
|
|
82
|
+
if (!question) return false;
|
|
83
|
+
const answers = state.answers.get(question.id) ?? [];
|
|
84
|
+
if (entry.kind === "option") return isOptionSelected(answers, entry.index);
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
|
|
42
88
|
/** The open file-completion popup over the inline editor. */
|
|
43
89
|
export interface SuggestionState {
|
|
44
90
|
/** The `@` token prefix the items replace. */
|
|
@@ -58,7 +104,8 @@ export interface MachineState {
|
|
|
58
104
|
readonly optionIndex: number;
|
|
59
105
|
/** Whether the inline "Type something" editor is open. */
|
|
60
106
|
readonly inputMode: boolean;
|
|
61
|
-
|
|
107
|
+
/** Answers grouped by question id; a multi-select question holds several. */
|
|
108
|
+
readonly answers: ReadonlyMap<string, readonly Answer[]>;
|
|
62
109
|
readonly editor: EditorState;
|
|
63
110
|
/** Open file-completion popup, or null when closed. */
|
|
64
111
|
readonly suggestions: SuggestionState | null;
|
|
@@ -166,23 +213,36 @@ const bestSuggestionIndex = (items: readonly SuggestionItem[], prefix: string):
|
|
|
166
213
|
return 0;
|
|
167
214
|
};
|
|
168
215
|
|
|
169
|
-
const stepInOptions = (state: MachineState, event: KeyEvent): StepResult =>
|
|
170
|
-
|
|
216
|
+
const stepInOptions = (state: MachineState, event: KeyEvent): StepResult => {
|
|
217
|
+
const question = state.questions[state.currentTab];
|
|
218
|
+
const onQuestion = state.currentTab < state.questions.length;
|
|
219
|
+
return Match.valueTags(event, {
|
|
171
220
|
// Tab navigation (multi-question mode only)
|
|
172
221
|
tab: () => (state.mode === "multi" ? moveTab(state, 1) : ignore(state)),
|
|
173
222
|
right: () => (state.mode === "multi" ? moveTab(state, 1) : ignore(state)),
|
|
174
223
|
shiftTab: () => (state.mode === "multi" ? moveTab(state, -1) : ignore(state)),
|
|
175
224
|
left: () => (state.mode === "multi" ? moveTab(state, -1) : ignore(state)),
|
|
176
225
|
// Selection moves are inert on the submit tab
|
|
177
|
-
up: () =>
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
226
|
+
up: () => (onQuestion ? moveSelection(state, -1) : ignore(state)),
|
|
227
|
+
down: () => (onQuestion ? moveSelection(state, 1) : ignore(state)),
|
|
228
|
+
// Enter confirms the choice: a single-select question records and advances;
|
|
229
|
+
// a multi-select question confirms the selection and moves on.
|
|
181
230
|
enter: () =>
|
|
182
|
-
|
|
231
|
+
!onQuestion
|
|
232
|
+
? maybeSubmit(state)
|
|
233
|
+
: question?.multiple
|
|
234
|
+
? confirmMultiple(state, question)
|
|
235
|
+
: chooseOption(state),
|
|
183
236
|
escape: () => complete(state, true),
|
|
184
|
-
//
|
|
185
|
-
|
|
237
|
+
// Space (a `char(" ")` event) toggles selections on a multi-select
|
|
238
|
+
// question and selects on a single-select question; other characters are
|
|
239
|
+
// ignored while navigating options.
|
|
240
|
+
char: (event) =>
|
|
241
|
+
onQuestion && event.char === " "
|
|
242
|
+
? question?.multiple
|
|
243
|
+
? toggleMultiEntry(state, question)
|
|
244
|
+
: chooseOption(state)
|
|
245
|
+
: ignore(state),
|
|
186
246
|
paste: () => ignore(state),
|
|
187
247
|
shiftEnter: () => ignore(state),
|
|
188
248
|
backspace: () => ignore(state),
|
|
@@ -190,6 +250,7 @@ const stepInOptions = (state: MachineState, event: KeyEvent): StepResult =>
|
|
|
190
250
|
home: () => ignore(state),
|
|
191
251
|
end: () => ignore(state),
|
|
192
252
|
});
|
|
253
|
+
};
|
|
193
254
|
|
|
194
255
|
const moveTab = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
195
256
|
const totalTabs = state.questions.length + 1;
|
|
@@ -214,7 +275,8 @@ const enterTab = (state: MachineState): MachineState => ({
|
|
|
214
275
|
const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
215
276
|
const question = state.questions[state.currentTab];
|
|
216
277
|
if (!question) return undefined;
|
|
217
|
-
|
|
278
|
+
if (question.multiple) return undefined; // no single "current answer" to highlight
|
|
279
|
+
const answer = state.answers.get(question.id)?.[0];
|
|
218
280
|
if (!answer) return undefined;
|
|
219
281
|
const options = optionsForTab(state.questions, state.currentTab);
|
|
220
282
|
if (answer.wasCustom) return options.length - 1; // the "Type something" entry
|
|
@@ -222,9 +284,15 @@ const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
|
222
284
|
return Math.min(Math.max(0, answer.index - 1), options.length - 1);
|
|
223
285
|
};
|
|
224
286
|
|
|
287
|
+
/** Number of rows the user can navigate on the active tab. */
|
|
288
|
+
const navigableCount = (state: MachineState): number => {
|
|
289
|
+
const question = state.questions[state.currentTab];
|
|
290
|
+
return question?.multiple ? multiEntries(state).length : currentOptions(state).length;
|
|
291
|
+
};
|
|
292
|
+
|
|
225
293
|
const moveSelection = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
226
|
-
const
|
|
227
|
-
const optionIndex = Math.min(Math.max(0, state.optionIndex + dir),
|
|
294
|
+
const count = navigableCount(state);
|
|
295
|
+
const optionIndex = Math.min(Math.max(0, state.optionIndex + dir), count - 1);
|
|
228
296
|
return { state: { ...state, optionIndex }, action: { type: "none" } };
|
|
229
297
|
};
|
|
230
298
|
|
|
@@ -235,10 +303,11 @@ const maybeSubmit = (state: MachineState): StepResult =>
|
|
|
235
303
|
/** Enter on a question tab: pick the highlighted option, or open the editor. */
|
|
236
304
|
const chooseOption = (state: MachineState): StepResult => {
|
|
237
305
|
const question = state.questions[state.currentTab];
|
|
306
|
+
if (!question) return ignore(state);
|
|
238
307
|
const option = currentOptions(state)[state.optionIndex];
|
|
239
|
-
if (!
|
|
308
|
+
if (!option) return ignore(state);
|
|
240
309
|
if (option.isOther) {
|
|
241
|
-
const existing = state.answers.get(question.id);
|
|
310
|
+
const existing = state.answers.get(question.id)?.[0];
|
|
242
311
|
return {
|
|
243
312
|
state: {
|
|
244
313
|
...state,
|
|
@@ -254,6 +323,36 @@ const chooseOption = (state: MachineState): StepResult => {
|
|
|
254
323
|
);
|
|
255
324
|
};
|
|
256
325
|
|
|
326
|
+
/** Space on a multi-select row: toggle an option, remove a chip, or open the editor. */
|
|
327
|
+
const toggleMultiEntry = (state: MachineState, question: Question): StepResult => {
|
|
328
|
+
const entry = multiEntries(state)[state.optionIndex];
|
|
329
|
+
if (!entry) return ignore(state);
|
|
330
|
+
switch (entry.kind) {
|
|
331
|
+
case "option":
|
|
332
|
+
return { state: toggleOption(state, question, entry.index), action: { type: "none" } };
|
|
333
|
+
case "custom":
|
|
334
|
+
return { state: removeCustom(state, question, entry.label), action: { type: "none" } };
|
|
335
|
+
case "add":
|
|
336
|
+
return {
|
|
337
|
+
state: { ...state, inputMode: true, editor: emptyEditor(), suggestions: null },
|
|
338
|
+
action: { type: "none" },
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
/** Enter on a multi-select question: open the add-editor or confirm and move on. */
|
|
344
|
+
const confirmMultiple = (state: MachineState, question: Question): StepResult => {
|
|
345
|
+
const entry = multiEntries(state)[state.optionIndex];
|
|
346
|
+
// Mirror single-select: Enter on "Add your own answer" opens the type mode.
|
|
347
|
+
if (entry?.kind === "add") {
|
|
348
|
+
return {
|
|
349
|
+
state: { ...state, inputMode: true, editor: emptyEditor(), suggestions: null },
|
|
350
|
+
action: { type: "none" },
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return answersFor(state, question).length === 0 ? ignore(state) : advanceAfterAnswer(state);
|
|
354
|
+
};
|
|
355
|
+
|
|
257
356
|
const stepInEditor = (state: MachineState, event: KeyEvent): StepResult =>
|
|
258
357
|
state.suggestions ? stepInEditorWithSuggestions(state, event) : stepInEditorPlain(state, event);
|
|
259
358
|
|
|
@@ -332,13 +431,61 @@ const submitEditor = (state: MachineState): StepResult => {
|
|
|
332
431
|
const question = state.questions[state.currentTab];
|
|
333
432
|
if (!question) return ignore(state);
|
|
334
433
|
const label = state.editor.text.trim() || "(no response)";
|
|
434
|
+
const closed = { inputMode: false, editor: emptyEditor(), suggestions: null } as const;
|
|
435
|
+
if (question.multiple) {
|
|
436
|
+
// Multi-select: each typed answer becomes one added alternative and the
|
|
437
|
+
// user stays on the tab to add more or confirm with Done.
|
|
438
|
+
return { state: { ...addCustom(state, question, label), ...closed }, action: { type: "none" } };
|
|
439
|
+
}
|
|
335
440
|
const next = withAnswer(state, question, label, true);
|
|
336
|
-
return advanceAfterAnswer({
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
441
|
+
return advanceAfterAnswer({ ...next, ...closed });
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const answersFor = (state: MachineState, question: Question): readonly Answer[] =>
|
|
445
|
+
state.answers.get(question.id) ?? [];
|
|
446
|
+
|
|
447
|
+
const isOptionSelected = (answers: readonly Answer[], index: number): boolean =>
|
|
448
|
+
answers.some((a) => !a.wasCustom && a.index === index + 1);
|
|
449
|
+
|
|
450
|
+
/** Toggle a checkbox option on/off in the question's answer array. */
|
|
451
|
+
const toggleOption = (
|
|
452
|
+
state: MachineState,
|
|
453
|
+
question: Question,
|
|
454
|
+
optionIndex: number,
|
|
455
|
+
): MachineState => {
|
|
456
|
+
const current = answersFor(state, question);
|
|
457
|
+
const answers = new Map(state.answers);
|
|
458
|
+
if (isOptionSelected(current, optionIndex)) {
|
|
459
|
+
const next = current.filter((a) => !(a.wasCustom === false && a.index === optionIndex + 1));
|
|
460
|
+
if (next.length === 0) answers.delete(question.id);
|
|
461
|
+
else answers.set(question.id, next);
|
|
462
|
+
} else {
|
|
463
|
+
const answer = new Answer({
|
|
464
|
+
id: question.id,
|
|
465
|
+
label: question.options[optionIndex]!.label,
|
|
466
|
+
wasCustom: false,
|
|
467
|
+
index: optionIndex + 1,
|
|
468
|
+
});
|
|
469
|
+
answers.set(question.id, [...current, answer]);
|
|
470
|
+
}
|
|
471
|
+
return { ...state, answers };
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
/** Remove a custom answer chip by label. */
|
|
475
|
+
const removeCustom = (state: MachineState, question: Question, label: string): MachineState => {
|
|
476
|
+
const answers = new Map(state.answers);
|
|
477
|
+
const next = answersFor(state, question).filter((a) => !(a.wasCustom && a.label === label));
|
|
478
|
+
if (next.length === 0) answers.delete(question.id);
|
|
479
|
+
else answers.set(question.id, next);
|
|
480
|
+
return { ...state, answers };
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
/** Append a newly typed custom alternative to the question's answers. */
|
|
484
|
+
const addCustom = (state: MachineState, question: Question, label: string): MachineState => {
|
|
485
|
+
const answers = new Map(state.answers);
|
|
486
|
+
const answer = new Answer({ id: question.id, label, wasCustom: true });
|
|
487
|
+
answers.set(question.id, [...answersFor(state, question), answer]);
|
|
488
|
+
return { ...state, answers };
|
|
342
489
|
};
|
|
343
490
|
|
|
344
491
|
const withAnswer = (
|
|
@@ -352,8 +499,8 @@ const withAnswer = (
|
|
|
352
499
|
answers.set(
|
|
353
500
|
question.id,
|
|
354
501
|
index === undefined
|
|
355
|
-
? new Answer({ id: question.id, label, wasCustom })
|
|
356
|
-
: new Answer({ id: question.id, label, wasCustom, index }),
|
|
502
|
+
? [new Answer({ id: question.id, label, wasCustom })]
|
|
503
|
+
: [new Answer({ id: question.id, label, wasCustom, index })],
|
|
357
504
|
);
|
|
358
505
|
return { ...state, answers };
|
|
359
506
|
};
|
|
@@ -371,7 +518,7 @@ const complete = (state: MachineState, cancelled: boolean): StepResult => ({
|
|
|
371
518
|
type: "complete",
|
|
372
519
|
result: new QuestionResult({
|
|
373
520
|
questions: [...state.questions],
|
|
374
|
-
answers: [...state.answers.values()],
|
|
521
|
+
answers: [...state.answers.values()].flatMap((group) => group),
|
|
375
522
|
cancelled,
|
|
376
523
|
}),
|
|
377
524
|
},
|
package/src/core/scene.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { Option, Question, type Question as QuestionType } from "./domain.ts";
|
|
3
|
+
import { decodeParams, normalizeQuestions } from "./domain.ts";
|
|
3
4
|
import { KeyEvent } from "./keyboard.ts";
|
|
4
5
|
import {
|
|
5
6
|
initialMachineState,
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
buildEditorBox,
|
|
13
14
|
buildScene,
|
|
14
15
|
buildSuggestionPopup,
|
|
16
|
+
clampLine,
|
|
15
17
|
line,
|
|
16
18
|
plain,
|
|
17
19
|
span,
|
|
@@ -28,6 +30,7 @@ const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
|
28
30
|
prompt: "Pick one?",
|
|
29
31
|
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
30
32
|
allowOther: true,
|
|
33
|
+
multiple: false,
|
|
31
34
|
...over,
|
|
32
35
|
});
|
|
33
36
|
|
|
@@ -188,6 +191,57 @@ describe("buildScene: multi question", () => {
|
|
|
188
191
|
});
|
|
189
192
|
});
|
|
190
193
|
|
|
194
|
+
describe("buildScene: multi-select question", () => {
|
|
195
|
+
const multiQuestion = (): QuestionType => question({ multiple: true });
|
|
196
|
+
|
|
197
|
+
it("renders checkbox options and an add entry", () => {
|
|
198
|
+
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
199
|
+
const lines = plainLines(scene);
|
|
200
|
+
expect(lines[3]).toBe(boxed("→ □ 1. Yes", 60));
|
|
201
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
202
|
+
expect(lines[5]).toBe(boxed(" + Add your own answer", 60));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("shows multi-select controls in the help line", () => {
|
|
206
|
+
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
207
|
+
expect(plainLines(scene).some((l) => l.includes("Space toggles • Enter confirm"))).toBe(true);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("marks selected options with a filled box", () => {
|
|
211
|
+
let state = initialMachineState([multiQuestion()]);
|
|
212
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0
|
|
213
|
+
const lines = plainLines(buildScene(state, 60));
|
|
214
|
+
expect(lines[3]).toBe(boxed("→ ■ 1. Yes", 60));
|
|
215
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("renders an added custom answer as a chip", () => {
|
|
219
|
+
let state = initialMachineState([multiQuestion()]);
|
|
220
|
+
state = step(state, KeyEvent.down).state;
|
|
221
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
222
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
223
|
+
for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
|
|
224
|
+
state = step(state, KeyEvent.enter).state; // submit
|
|
225
|
+
const lines = plainLines(buildScene(state, 60));
|
|
226
|
+
expect(lines.some((l) => l.includes("■ hi"))).toBe(true);
|
|
227
|
+
expect(lines.some((l) => l.includes("+ Add your own answer"))).toBe(true);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("shows an added custom answer in the submit summary alongside the option", () => {
|
|
231
|
+
let state = initialMachineState([multiQuestion(), question({ id: "q2" })]);
|
|
232
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0 on q1
|
|
233
|
+
state = step(state, KeyEvent.down).state;
|
|
234
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
235
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
236
|
+
for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
|
|
237
|
+
state = step(state, KeyEvent.enter).state; // submit custom
|
|
238
|
+
state = step(state, KeyEvent.enter).state; // confirm -> q2
|
|
239
|
+
state = step(state, KeyEvent.enter).state; // answer q2 -> submit tab
|
|
240
|
+
const lines = plainLines(buildScene(state, 60));
|
|
241
|
+
expect(lines.some((l) => l.includes(" Q1: 1. Yes, (wrote) hi"))).toBe(true);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
191
245
|
describe("buildScene: editor mode", () => {
|
|
192
246
|
it("shows the editor box with prompt and options", () => {
|
|
193
247
|
const scene = buildScene(openEditor(initialMachineState([question()])), 40);
|
|
@@ -359,6 +413,107 @@ describe("buildEditorBox", () => {
|
|
|
359
413
|
});
|
|
360
414
|
});
|
|
361
415
|
|
|
416
|
+
describe("buildScene: terminal-width crash regression", () => {
|
|
417
|
+
// Independent pi-tui-style measure (the module under test must never emit a
|
|
418
|
+
// line pi's crash guard would reject: "Rendered line exceeds terminal
|
|
419
|
+
// width"). The 2026-08-31 incident: the model prefixed a prompt
|
|
420
|
+
// continuation with ➡️; the old per-code-point measure counted it as one
|
|
421
|
+
// column, pi counts two, and the framed line came out 111 on a 110-column
|
|
422
|
+
// terminal, killing the agent.
|
|
423
|
+
const terminalColumns = (text: string): number => {
|
|
424
|
+
const rgiEmoji = /^\p{RGI_Emoji}$/v;
|
|
425
|
+
const couldBeEmoji = (s: string): boolean => {
|
|
426
|
+
const cp = s.codePointAt(0) ?? 0;
|
|
427
|
+
return (
|
|
428
|
+
(cp >= 0x1f000 && cp <= 0x1fbff) ||
|
|
429
|
+
(cp >= 0x2300 && cp <= 0x23ff) ||
|
|
430
|
+
(cp >= 0x2600 && cp <= 0x27bf) ||
|
|
431
|
+
(cp >= 0x2b50 && cp <= 0x2b55) ||
|
|
432
|
+
s.includes("\uFE0F") ||
|
|
433
|
+
s.length > 2
|
|
434
|
+
);
|
|
435
|
+
};
|
|
436
|
+
let w = 0;
|
|
437
|
+
for (const { segment } of new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(text)) {
|
|
438
|
+
if (segment === "\t") { w += 3; continue; }
|
|
439
|
+
if (couldBeEmoji(segment) && rgiEmoji.test(segment)) { w += 2; continue; }
|
|
440
|
+
for (const ch of segment) {
|
|
441
|
+
const cp = ch.codePointAt(0)!;
|
|
442
|
+
if (cp === 0xfe0f || cp === 0x200d) w += 0;
|
|
443
|
+
else if (cp >= 0x2e80) w += 2;
|
|
444
|
+
else w += 1;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return w;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// The Q7 payload from the crashing session, minus the noise the strip
|
|
451
|
+
// removes. Width 110 matches the crashed terminal.
|
|
452
|
+
const q7CrashPayload = () => ({
|
|
453
|
+
id: "q7",
|
|
454
|
+
label: "Q7 deployBlock",
|
|
455
|
+
prompt:
|
|
456
|
+
"❓ Q7 — deployBlock guard: under net-deltas semantics, deployBlock set AFTER contract creation is legitimate (read model = deltas since deploy). A deployBlock in the future is indistinguishable from a token not yet deployed — both idle until head passes. So there is no misconfig that fails loudly today; a wrong deployBlock is a silent, visible-only-in-output mistake. Should anything guard it?\n\n➡️ Recommended: A — document-only. Zero mechanics, and (b) is a 10-line add if you want the operator signal.",
|
|
457
|
+
options: [
|
|
458
|
+
{
|
|
459
|
+
label: "Document-only",
|
|
460
|
+
description:
|
|
461
|
+
"➡️ Recommended. Zero mechanics; the Slice-13 gate + docs cover operator responsibility.",
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
label: "One-shot warning log",
|
|
465
|
+
description:
|
|
466
|
+
"Engine logs a one-shot warning on first tick if getCode(deployBlock-1) is non-empty ('history starts mid-token-life'). One RPC per token, no boot-time network.",
|
|
467
|
+
},
|
|
468
|
+
{ label: "Fail boot" },
|
|
469
|
+
],
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it("emits no line wider than the terminal", () => {
|
|
473
|
+
const questions = normalizeQuestions(
|
|
474
|
+
decodeParams({ questions: [q7CrashPayload()] }),
|
|
475
|
+
);
|
|
476
|
+
const scene = buildScene(initialMachineState(questions), 110);
|
|
477
|
+
for (const l of plainLines(scene)) {
|
|
478
|
+
expect(terminalColumns(l)).toBeLessThanOrEqual(110);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("renders the prompt continuation and option description without the model's leading emoji", () => {
|
|
483
|
+
const questions = normalizeQuestions(
|
|
484
|
+
decodeParams({ questions: [q7CrashPayload()] }),
|
|
485
|
+
);
|
|
486
|
+
const scene = buildScene(initialMachineState(questions), 110);
|
|
487
|
+
const text = plainLines(scene).join("\n");
|
|
488
|
+
expect(text).toContain("Recommended: A — document-only.");
|
|
489
|
+
expect(text).toContain("Recommended. Zero mechanics; the Slice-13 gate");
|
|
490
|
+
expect(text).not.toContain("➡️");
|
|
491
|
+
expect(text).toContain("Q7 — deployBlock guard:");
|
|
492
|
+
expect(text).not.toContain("❓");
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
describe("clampLine", () => {
|
|
497
|
+
it("is a no-op when the line fits", () => {
|
|
498
|
+
const l: SceneLine = [span("abc")];
|
|
499
|
+
expect(clampLine(l, 5)).toEqual(l);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("truncates the last kept span at the width", () => {
|
|
503
|
+
expect(plain(clampLine([span("abcdef")], 3))).toBe("abc");
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("truncates the span that overflows", () => {
|
|
507
|
+
const out = clampLine([span("ab"), span("cd", "muted")], 3);
|
|
508
|
+
expect(plain(out)).toBe("abc");
|
|
509
|
+
expect(out[1]).toEqual({ text: "c", style: "muted" });
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("clips on grapheme boundaries", () => {
|
|
513
|
+
expect(plain(clampLine([span("➡️ x")], 2))).toBe("➡️");
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
|
|
362
517
|
describe("wrapLine", () => {
|
|
363
518
|
it("preserves spans across line breaks", () => {
|
|
364
519
|
const wrapped = wrapLine(line(span("one", "accent"), span(" two three", "muted")), 7);
|
package/src/core/scene.ts
CHANGED
|
@@ -9,7 +9,13 @@
|
|
|
9
9
|
|
|
10
10
|
import { buildBoxFrame } from "@ftrdotdev/pi-tui/box-frame";
|
|
11
11
|
import type { MachineState } from "./machine.ts";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
currentOptions,
|
|
14
|
+
isAllAnswered,
|
|
15
|
+
isMultiEntrySelected,
|
|
16
|
+
multiEntries,
|
|
17
|
+
type SuggestionState,
|
|
18
|
+
} from "./machine.ts";
|
|
13
19
|
import { truncateToWidth, visibleWidth } from "./text.ts";
|
|
14
20
|
import type { EditorState } from "./editor.ts";
|
|
15
21
|
|
|
@@ -183,6 +189,10 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
183
189
|
};
|
|
184
190
|
|
|
185
191
|
const renderOptions = (): void => {
|
|
192
|
+
if (question?.multiple) {
|
|
193
|
+
renderMultiOptions();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
186
196
|
for (let i = 0; i < options.length; i++) {
|
|
187
197
|
const option = options[i]!;
|
|
188
198
|
const selected = i === state.optionIndex;
|
|
@@ -194,7 +204,7 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
194
204
|
pushWrapped(span(" "), [span(option.description, "muted")]);
|
|
195
205
|
} else if (option.isOther && !state.inputMode && question) {
|
|
196
206
|
// Show an existing custom answer as a subtitle under "Type something".
|
|
197
|
-
const answer = state.answers.get(question.id);
|
|
207
|
+
const answer = state.answers.get(question.id)?.[0];
|
|
198
208
|
if (answer?.wasCustom) {
|
|
199
209
|
pushWrapped(span(" "), [span(`Current answer: ${answer.label}`, "muted")]);
|
|
200
210
|
}
|
|
@@ -202,6 +212,37 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
202
212
|
}
|
|
203
213
|
};
|
|
204
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Multi-select question rows: checkbox options, removable custom-answer
|
|
217
|
+
* chips, and the "Add your own answer" entry.
|
|
218
|
+
*/
|
|
219
|
+
const renderMultiOptions = (): void => {
|
|
220
|
+
const entries = multiEntries(state);
|
|
221
|
+
for (let i = 0; i < entries.length; i++) {
|
|
222
|
+
const entry = entries[i]!;
|
|
223
|
+
const selected = i === state.optionIndex;
|
|
224
|
+
const checked = isMultiEntrySelected(state, entry);
|
|
225
|
+
let labelText: string;
|
|
226
|
+
let description: string | undefined;
|
|
227
|
+
if (entry.kind === "option") {
|
|
228
|
+
// Reuse the tab bar's outline (`□`) / filled (`■`) boxes so a checked
|
|
229
|
+
// checkbox reads like a filled tab square.
|
|
230
|
+
labelText = `${checked ? "■" : "□"} ${entry.index + 1}. ${entry.label}`;
|
|
231
|
+
description = entry.description;
|
|
232
|
+
} else if (entry.kind === "custom") {
|
|
233
|
+
labelText = `■ ${entry.label}`;
|
|
234
|
+
} else {
|
|
235
|
+
labelText = "+ Add your own answer";
|
|
236
|
+
}
|
|
237
|
+
pushWrapped(selected ? span("→ ", "accent") : span(" "), [
|
|
238
|
+
span(labelText, selected ? "accent" : "text"),
|
|
239
|
+
]);
|
|
240
|
+
if (description) {
|
|
241
|
+
pushWrapped(span(" "), [span(description, "muted")]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
205
246
|
// Tab bar (multi-question mode only)
|
|
206
247
|
if (state.mode === "multi") {
|
|
207
248
|
const tabs: Span[] = [span("← ")];
|
|
@@ -260,13 +301,19 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
260
301
|
pushWrapped(span(" "), [span("Ready to submit", "accent", true)]);
|
|
261
302
|
lines.push([]);
|
|
262
303
|
for (const q of state.questions) {
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
304
|
+
const answers = state.answers.get(q.id);
|
|
305
|
+
if (answers && answers.length > 0) {
|
|
306
|
+
// A single answer keeps the legacy bare label; a multi-select answer
|
|
307
|
+
// lists each chosen option/alternative with its index.
|
|
308
|
+
const rendered =
|
|
309
|
+
answers.length === 1
|
|
310
|
+
? answers[0]!.wasCustom
|
|
311
|
+
? `(wrote) ${answers[0]!.label}`
|
|
312
|
+
: answers[0]!.label
|
|
313
|
+
: answers
|
|
314
|
+
.map((a) => (a.wasCustom ? `(wrote) ${a.label}` : `${a.index}. ${a.label}`))
|
|
315
|
+
.join(", ");
|
|
316
|
+
pushWrapped(span(" "), [span(`${q.label}: `, "muted"), span(rendered, "text")]);
|
|
270
317
|
}
|
|
271
318
|
}
|
|
272
319
|
lines.push([]);
|
|
@@ -287,29 +334,63 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
287
334
|
|
|
288
335
|
lines.push([]);
|
|
289
336
|
if (!state.inputMode) {
|
|
337
|
+
const isMultiSelect = question?.multiple === true;
|
|
290
338
|
const help =
|
|
291
339
|
state.mode === "multi"
|
|
292
|
-
?
|
|
293
|
-
|
|
340
|
+
? isMultiSelect
|
|
341
|
+
? "Tab/←→ navigate • ↑↓ move • Space toggles • Enter confirm • Esc cancel"
|
|
342
|
+
: "Tab/←→ navigate • ↑↓ select • Enter confirm • Esc cancel"
|
|
343
|
+
: isMultiSelect
|
|
344
|
+
? "↑↓ move • Space toggles • Enter confirm • Esc cancel"
|
|
345
|
+
: "↑↓ navigate • Enter to select • Esc to cancel";
|
|
294
346
|
pushWrapped(span(" "), [span(help, "dim")]);
|
|
295
347
|
}
|
|
296
348
|
|
|
297
349
|
// Frame the interior: accent border with rails, body padded to the content
|
|
298
350
|
// width so the rails stay flush (the same layout the BorderedBox component
|
|
299
|
-
// produces around ANSI children).
|
|
351
|
+
// produces around ANSI children). Body rows are pre-wrapped, so a row that
|
|
352
|
+
// still exceeds the content width indicates a measurement mismatch; clamp it
|
|
353
|
+
// rather than letting it push the rail past the terminal (pi hard-crashes on
|
|
354
|
+
// any custom-UI line wider than the terminal).
|
|
300
355
|
const border = (text: string): Span => span(text, "accent");
|
|
301
356
|
const framed: SceneLine[] = [
|
|
302
357
|
[border(frame.top.left), border(frame.top.fill), border(frame.top.right)],
|
|
303
358
|
];
|
|
304
359
|
for (const content of lines) {
|
|
305
360
|
const pad = contentWidth - visibleWidth(plain(content));
|
|
306
|
-
const body =
|
|
361
|
+
const body =
|
|
362
|
+
pad > 0 ? [...content, span(" ".repeat(pad))] : pad < 0 ? clampLine(content, contentWidth) : content;
|
|
307
363
|
framed.push([border(frame.railLeft), ...body, border(frame.railRight)]);
|
|
308
364
|
}
|
|
309
365
|
framed.push([border(frame.bottom.left), border(frame.bottom.fill), border(frame.bottom.right)]);
|
|
310
366
|
return { lines: framed };
|
|
311
367
|
};
|
|
312
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Clip a scene line to at most `width` visible columns, dropping whole spans
|
|
371
|
+
* and truncating the last kept span (the same clipping BorderedBox applies to
|
|
372
|
+
* body rows via truncateToWidth). Defensive only: scene content is
|
|
373
|
+
* pre-wrapped, so this should never truncate once widths are accurate.
|
|
374
|
+
*/
|
|
375
|
+
export const clampLine = (content: SceneLine, width: number): SceneLine => {
|
|
376
|
+
const out: SceneLine = [];
|
|
377
|
+
let used = 0;
|
|
378
|
+
for (const s of content) {
|
|
379
|
+
const w = visibleWidth(s.text);
|
|
380
|
+
const room = width - used;
|
|
381
|
+
if (room <= 0) break;
|
|
382
|
+
if (w <= room) {
|
|
383
|
+
out.push(s);
|
|
384
|
+
used += w;
|
|
385
|
+
} else {
|
|
386
|
+
const kept = truncateToWidth(s.text, room);
|
|
387
|
+
if (kept !== "" || out.length === 0) out.push({ ...s, text: kept });
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return out;
|
|
392
|
+
};
|
|
393
|
+
|
|
313
394
|
/**
|
|
314
395
|
* Build the inline editor box lines (without the leading gutter space).
|
|
315
396
|
*/
|