@ftrdotdev/pi-inquiry 0.2.0 → 0.3.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 +23 -1
- package/index.ts +1 -0
- package/package.json +4 -3
- package/src/core/domain.test.ts +18 -1
- package/src/core/domain.ts +9 -1
- 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 +52 -0
- package/src/core/scene.ts +63 -11
- 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 +31 -13
package/README.md
CHANGED
|
@@ -74,7 +74,8 @@ pick between options, or confirm a decision. Ask all open questions in one
|
|
|
74
74
|
"options": [{ "label": "Short label", "description": "Optional one-line description" }],
|
|
75
75
|
"id": "optional-unique-id", // defaults to q1, q2, ...
|
|
76
76
|
"label": "Scope", // short tab-bar label, defaults to Q1, Q2
|
|
77
|
-
"allowOther": true // defaults to true; false forces the listed options
|
|
77
|
+
"allowOther": true, // defaults to true; false forces the listed options
|
|
78
|
+
"multiple": false // defaults to false; true asks for one or more answers
|
|
78
79
|
}
|
|
79
80
|
]
|
|
80
81
|
}
|
|
@@ -87,6 +88,22 @@ pick between options, or confirm a decision. Ask all open questions in one
|
|
|
87
88
|
- `allowOther` — when `true` (default), the user can also type a free-text
|
|
88
89
|
answer ("Type something."). Set it to `false` only when one of the listed
|
|
89
90
|
options is required.
|
|
91
|
+
- `multiple` — when `true`, the question is multi-select: the options render
|
|
92
|
+
as checkboxes and the user picks one or more of them, then presses Enter to
|
|
93
|
+
confirm the selection instead of choosing a single one. Space toggles a
|
|
94
|
+
checkbox on or off; Enter confirms. This is the natural way to ask "which
|
|
95
|
+
of these apply?" without turning each option into its own binary question.
|
|
96
|
+
|
|
97
|
+
### Multi-select answers
|
|
98
|
+
|
|
99
|
+
A multi-select question keeps an "Add your own answer" entry (when
|
|
100
|
+
`allowOther` is true) so the user can type additional alternatives; each typed
|
|
101
|
+
answer becomes one more chosen value. Space toggles each option and removes a
|
|
102
|
+
typed chip; Enter confirms and records the whole selection. As with a
|
|
103
|
+
single-select question, pressing Enter on the "Add your own answer" row opens
|
|
104
|
+
the type mode (Space does too). In the result, a multi-select question
|
|
105
|
+
contributes one `Answer` per chosen option or typed alternative, all sharing
|
|
106
|
+
the question's `id`.
|
|
90
107
|
|
|
91
108
|
### Result
|
|
92
109
|
|
|
@@ -103,11 +120,16 @@ The tool returns its answers both as human-readable content and in
|
|
|
103
120
|
wasCustom: false, // true when the user typed a free-text answer
|
|
104
121
|
index: 2 // option index, absent for custom answers
|
|
105
122
|
}
|
|
123
|
+
// A multi-select question contributes one entry per chosen value.
|
|
106
124
|
],
|
|
107
125
|
cancelled: false // true when the user dismissed the form
|
|
108
126
|
}
|
|
109
127
|
```
|
|
110
128
|
|
|
129
|
+
The tool groups a question's answers together in its output, so a
|
|
130
|
+
multi-select question reads as e.g. `Q1: user selected: 1. A, 2. B, user
|
|
131
|
+
wrote: X` rather than several detached rows.
|
|
132
|
+
|
|
111
133
|
Treat a cancelled result as the user declining to answer. Do not re-ask
|
|
112
134
|
unless the answer is essential; then ask once more in a different form.
|
|
113
135
|
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./src/tool/question";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ftrdotdev/pi-inquiry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Interactive questions for the pi coding agent: a single `question` tool renders option lists, tabbed forms, and free-text answers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"directory": "packages/inquiry"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
+
"index.ts",
|
|
27
28
|
"src",
|
|
28
29
|
"README.md",
|
|
29
30
|
"LICENSE"
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
"fmt": "oxfmt -c oxfmt.config.ts"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
44
|
-
"@ftrdotdev/pi-tui": "^0.
|
|
45
|
+
"@ftrdotdev/pi-tui": "^0.3.0",
|
|
45
46
|
"effect": "4.0.0-rc.108"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
@@ -62,7 +63,7 @@
|
|
|
62
63
|
},
|
|
63
64
|
"pi": {
|
|
64
65
|
"extensions": [
|
|
65
|
-
"./
|
|
66
|
+
"./index.ts"
|
|
66
67
|
]
|
|
67
68
|
}
|
|
68
69
|
}
|
package/src/core/domain.test.ts
CHANGED
|
@@ -25,12 +25,14 @@ describe("decodeParams", () => {
|
|
|
25
25
|
prompt: "Pick one?",
|
|
26
26
|
options: [{ label: "Yes", description: "Go ahead" }],
|
|
27
27
|
allowOther: false,
|
|
28
|
+
multiple: true,
|
|
28
29
|
},
|
|
29
30
|
],
|
|
30
31
|
});
|
|
31
32
|
expect(params.questions[0]?.id).toBe("scope");
|
|
32
33
|
expect(params.questions[0]?.label).toBe("Scope");
|
|
33
34
|
expect(params.questions[0]?.allowOther).toBe(false);
|
|
35
|
+
expect(params.questions[0]?.multiple).toBe(true);
|
|
34
36
|
expect(params.questions[0]?.options[0]?.description).toBe("Go ahead");
|
|
35
37
|
});
|
|
36
38
|
|
|
@@ -135,13 +137,28 @@ describe("normalizeQuestions", () => {
|
|
|
135
137
|
const questions = normalizeQuestions(
|
|
136
138
|
decodeParams({
|
|
137
139
|
questions: [
|
|
138
|
-
{
|
|
140
|
+
{
|
|
141
|
+
id: "x",
|
|
142
|
+
label: "Scope",
|
|
143
|
+
prompt: "p",
|
|
144
|
+
options: [{ label: "a" }],
|
|
145
|
+
allowOther: false,
|
|
146
|
+
multiple: true,
|
|
147
|
+
},
|
|
139
148
|
],
|
|
140
149
|
}),
|
|
141
150
|
);
|
|
142
151
|
expect(questions[0]?.id).toBe("x");
|
|
143
152
|
expect(questions[0]?.label).toBe("Scope");
|
|
144
153
|
expect(questions[0]?.allowOther).toBe(false);
|
|
154
|
+
expect(questions[0]?.multiple).toBe(true);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("defaults multiple to false", () => {
|
|
158
|
+
const questions = normalizeQuestions(
|
|
159
|
+
decodeParams({ questions: [{ prompt: "p", options: [{ label: "a" }] }] }),
|
|
160
|
+
);
|
|
161
|
+
expect(questions[0]?.multiple).toBe(false);
|
|
145
162
|
});
|
|
146
163
|
|
|
147
164
|
it("maps option labels and descriptions", () => {
|
package/src/core/domain.ts
CHANGED
|
@@ -43,6 +43,12 @@ export const QuestionParamsSchema = Schema.Struct({
|
|
|
43
43
|
allowOther: Schema.optionalKey(
|
|
44
44
|
Schema.Boolean.annotate({ description: "Allow 'Type something' option (default: true)" }),
|
|
45
45
|
),
|
|
46
|
+
multiple: Schema.optionalKey(
|
|
47
|
+
Schema.Boolean.annotate({
|
|
48
|
+
description:
|
|
49
|
+
"Ask for one or more answers instead of a single choice; renders checkboxes and lets the user add custom alternatives (default: false)",
|
|
50
|
+
}),
|
|
51
|
+
),
|
|
46
52
|
});
|
|
47
53
|
|
|
48
54
|
export type QuestionParams = typeof QuestionParamsSchema.Type;
|
|
@@ -109,6 +115,7 @@ export class Question extends Schema.Class<Question>("inquiry/core/domain/Questi
|
|
|
109
115
|
prompt: Schema.String,
|
|
110
116
|
options: Schema.Array(Option),
|
|
111
117
|
allowOther: Schema.Boolean,
|
|
118
|
+
multiple: Schema.Boolean,
|
|
112
119
|
}) {}
|
|
113
120
|
|
|
114
121
|
export class Answer extends Schema.Class<Answer>("inquiry/core/domain/Answer")({
|
|
@@ -130,7 +137,7 @@ export class QuestionResult extends Schema.Class<QuestionResult>(
|
|
|
130
137
|
* Normalize wire params into domain questions.
|
|
131
138
|
*
|
|
132
139
|
* Applies the defaults: `id` falls back to `q{n}`, `label` to `Q{n}`,
|
|
133
|
-
* `allowOther` defaults to `true`.
|
|
140
|
+
* `allowOther` defaults to `true`, `multiple` to `false`.
|
|
134
141
|
*/
|
|
135
142
|
export const normalizeQuestions = (params: QuestionListParams): readonly Question[] =>
|
|
136
143
|
params.questions.map(
|
|
@@ -145,5 +152,6 @@ export const normalizeQuestions = (params: QuestionListParams): readonly Questio
|
|
|
145
152
|
: new Option({ label: o.label, description: o.description }),
|
|
146
153
|
),
|
|
147
154
|
allowOther: q.allowOther !== false,
|
|
155
|
+
multiple: q.multiple === true,
|
|
148
156
|
}),
|
|
149
157
|
);
|
package/src/core/flow.test.ts
CHANGED
|
@@ -13,6 +13,7 @@ const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
|
13
13
|
prompt: "Pick one?",
|
|
14
14
|
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
15
15
|
allowOther: true,
|
|
16
|
+
multiple: false,
|
|
16
17
|
...over,
|
|
17
18
|
});
|
|
18
19
|
|
|
@@ -118,6 +119,48 @@ describe("runQuestionnaire", () => {
|
|
|
118
119
|
}),
|
|
119
120
|
);
|
|
120
121
|
|
|
122
|
+
it.effect("collects several answers from a multi-select question", () =>
|
|
123
|
+
Effect.gen(function* () {
|
|
124
|
+
const harness = yield* run(
|
|
125
|
+
[question({ multiple: true })],
|
|
126
|
+
[
|
|
127
|
+
KeyEvent.char(" "), // toggle Yes
|
|
128
|
+
KeyEvent.down,
|
|
129
|
+
KeyEvent.down,
|
|
130
|
+
KeyEvent.char(" "), // open the add editor
|
|
131
|
+
KeyEvent.char("M"),
|
|
132
|
+
KeyEvent.char("y"),
|
|
133
|
+
KeyEvent.enter, // submit custom "My"
|
|
134
|
+
KeyEvent.enter, // confirm selection
|
|
135
|
+
],
|
|
136
|
+
);
|
|
137
|
+
expect(harness.completed?.cancelled).toBe(false);
|
|
138
|
+
expect(harness.completed?.answers.map((a) => [a.label, a.wasCustom])).toEqual([
|
|
139
|
+
["Yes", false],
|
|
140
|
+
["My", true],
|
|
141
|
+
]);
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
it.effect("advances a multi-select question to the next tab on Enter", () =>
|
|
146
|
+
Effect.gen(function* () {
|
|
147
|
+
const harness = yield* run(
|
|
148
|
+
[question({ multiple: true }), question({ id: "q2", label: "Q2" })],
|
|
149
|
+
[
|
|
150
|
+
KeyEvent.char(" "), // toggle Yes on q1
|
|
151
|
+
KeyEvent.enter, // confirm -> q2
|
|
152
|
+
KeyEvent.enter, // answer q2 -> submit tab
|
|
153
|
+
KeyEvent.enter, // confirm submission
|
|
154
|
+
],
|
|
155
|
+
);
|
|
156
|
+
expect(harness.completed?.cancelled).toBe(false);
|
|
157
|
+
const q1 = harness.completed?.answers.find((a) => a.id === "q1");
|
|
158
|
+
const q2 = harness.completed?.answers.find((a) => a.id === "q2");
|
|
159
|
+
expect(q1).toMatchObject({ label: "Yes", index: 1, wasCustom: false });
|
|
160
|
+
expect(q2).toMatchObject({ label: "Yes", index: 1, wasCustom: false });
|
|
161
|
+
}),
|
|
162
|
+
);
|
|
163
|
+
|
|
121
164
|
it.effect("completes with the selected option", () =>
|
|
122
165
|
Effect.gen(function* () {
|
|
123
166
|
const harness = yield* run([question()], [KeyEvent.down, KeyEvent.enter]);
|
package/src/core/machine.test.ts
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
currentOptions,
|
|
6
6
|
initialMachineState,
|
|
7
7
|
isAtCompletionContext,
|
|
8
|
+
isMultiEntrySelected,
|
|
9
|
+
multiEntries,
|
|
8
10
|
openSuggestions,
|
|
9
11
|
step,
|
|
10
12
|
textBeforeCursor,
|
|
@@ -19,6 +21,7 @@ const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
|
19
21
|
prompt: "Pick one?",
|
|
20
22
|
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
21
23
|
allowOther: true,
|
|
24
|
+
multiple: false,
|
|
22
25
|
...over,
|
|
23
26
|
});
|
|
24
27
|
|
|
@@ -193,7 +196,7 @@ describe("multi question", () => {
|
|
|
193
196
|
it("answers and advances to the next tab", () => {
|
|
194
197
|
const result = step(initialMachineState(twoQuestions()), KeyEvent.enter);
|
|
195
198
|
expect(result.state.currentTab).toBe(1);
|
|
196
|
-
expect(result.state.answers.get("q1")?.label).toBe("Yes");
|
|
199
|
+
expect(result.state.answers.get("q1")?.[0]?.label).toBe("Yes");
|
|
197
200
|
expect(result.action.type).toBe("none");
|
|
198
201
|
});
|
|
199
202
|
|
|
@@ -318,8 +321,8 @@ describe("multi question", () => {
|
|
|
318
321
|
state = step(state, KeyEvent.enter).state; // reopen, pre-filled
|
|
319
322
|
state = step(state, KeyEvent.char("!")).state;
|
|
320
323
|
state = step(state, KeyEvent.enter).state; // re-submit "hey!"
|
|
321
|
-
expect(state.answers.get("q1")?.label).toBe("hey!");
|
|
322
|
-
expect(state.answers.get("q1")?.wasCustom).toBe(true);
|
|
324
|
+
expect(state.answers.get("q1")?.[0]?.label).toBe("hey!");
|
|
325
|
+
expect(state.answers.get("q1")?.[0]?.wasCustom).toBe(true);
|
|
323
326
|
expect(state.currentTab).toBe(1);
|
|
324
327
|
});
|
|
325
328
|
|
|
@@ -336,6 +339,157 @@ describe("multi question", () => {
|
|
|
336
339
|
});
|
|
337
340
|
});
|
|
338
341
|
|
|
342
|
+
describe("multi-select question", () => {
|
|
343
|
+
const multiQuestion = (): QuestionType => question({ multiple: true });
|
|
344
|
+
|
|
345
|
+
it("starts with checkbox options and an add entry", () => {
|
|
346
|
+
const state = initialMachineState([multiQuestion()]);
|
|
347
|
+
expect(multiEntries(state).map((e) => e.kind)).toEqual(["option", "option", "add"]);
|
|
348
|
+
expect(isMultiEntrySelected(state, multiEntries(state)[0]!)).toBe(false);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("toggles an option on and off with space", () => {
|
|
352
|
+
let state = initialMachineState([multiQuestion()]);
|
|
353
|
+
state = step(state, KeyEvent.char(" ")).state; // toggle option 0 on
|
|
354
|
+
expect(state.answers.get("q1")?.[0]).toMatchObject({ index: 1, wasCustom: false });
|
|
355
|
+
expect(isMultiEntrySelected(state, multiEntries(state)[0]!)).toBe(true);
|
|
356
|
+
state = step(state, KeyEvent.char(" ")).state; // toggle the same option off
|
|
357
|
+
expect(state.answers.get("q1")).toBeUndefined(); // emptied, no stale key
|
|
358
|
+
expect(isMultiEntrySelected(state, multiEntries(state)[0]!)).toBe(false);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("selects several options before confirming", () => {
|
|
362
|
+
let state = initialMachineState([multiQuestion()]);
|
|
363
|
+
state = step(state, KeyEvent.char(" ")).state; // Yes
|
|
364
|
+
state = step(state, KeyEvent.down).state;
|
|
365
|
+
state = step(state, KeyEvent.char(" ")).state; // No
|
|
366
|
+
expect(state.answers.get("q1")?.map((a) => a.index)).toEqual([1, 2]);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("selects a single option with space", () => {
|
|
370
|
+
let state = initialMachineState([question()]);
|
|
371
|
+
const result = step(state, KeyEvent.char(" "));
|
|
372
|
+
expect(result.action.type).toBe("complete");
|
|
373
|
+
if (result.action.type === "complete") {
|
|
374
|
+
expect(result.action.result.answers.map((a) => a.label)).toEqual(["Yes"]);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("adds a custom answer from the editor without advancing", () => {
|
|
379
|
+
let state = initialMachineState([multiQuestion()]);
|
|
380
|
+
state = step(state, KeyEvent.down).state;
|
|
381
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
382
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
383
|
+
expect(state.inputMode).toBe(true);
|
|
384
|
+
for (const ch of ["M", "y"]) state = step(state, KeyEvent.char(ch)).state;
|
|
385
|
+
const next = step(state, KeyEvent.enter); // submit
|
|
386
|
+
expect(next.state.inputMode).toBe(false);
|
|
387
|
+
expect(next.state.currentTab).toBe(0); // stays on the tab
|
|
388
|
+
expect(next.state.answers.get("q1")?.map((a) => [a.label, a.wasCustom])).toEqual([
|
|
389
|
+
["My", true],
|
|
390
|
+
]);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("opens the add-editor with enter on the add row, like single-select", () => {
|
|
394
|
+
let state = initialMachineState([multiQuestion()]);
|
|
395
|
+
state = step(state, KeyEvent.down).state;
|
|
396
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
397
|
+
const next = step(state, KeyEvent.enter); // enter instead of space
|
|
398
|
+
expect(next.action.type).toBe("none");
|
|
399
|
+
expect(next.state.inputMode).toBe(true);
|
|
400
|
+
expect(next.state.currentTab).toBe(0); // did not advance
|
|
401
|
+
// Enter on an option row still confirms rather than opening the editor
|
|
402
|
+
let confirm = initialMachineState([multiQuestion()]);
|
|
403
|
+
confirm = step(confirm, KeyEvent.char(" ")).state; // select option 0
|
|
404
|
+
const done = step(confirm, KeyEvent.enter);
|
|
405
|
+
expect(done.action.type).toBe("complete");
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("types a space into the add-editor instead of toggling", () => {
|
|
409
|
+
let state = initialMachineState([multiQuestion()]);
|
|
410
|
+
state = step(state, KeyEvent.down).state;
|
|
411
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
412
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
413
|
+
expect(state.inputMode).toBe(true);
|
|
414
|
+
state = step(state, KeyEvent.char("h")).state;
|
|
415
|
+
state = step(state, KeyEvent.char(" ")).state; // space while typing
|
|
416
|
+
state = step(state, KeyEvent.char("i")).state;
|
|
417
|
+
expect(state.editor.text).toBe("h i"); // inserted literally, not a toggle
|
|
418
|
+
expect(state.answers.get("q1")).toBeUndefined(); // selection untouched
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("removes an added custom answer chip with space", () => {
|
|
422
|
+
let state = initialMachineState([multiQuestion()]);
|
|
423
|
+
state = step(state, KeyEvent.down).state;
|
|
424
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
425
|
+
state = step(state, KeyEvent.char(" ")).state;
|
|
426
|
+
for (const ch of ["M", "y"]) state = step(state, KeyEvent.char(ch)).state;
|
|
427
|
+
state = step(state, KeyEvent.enter).state; // submit -> chip at index 2
|
|
428
|
+
state = step(state, KeyEvent.char(" ")).state; // space on the chip removes it
|
|
429
|
+
expect(state.answers.get("q1")).toBeUndefined();
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("blocks Enter until at least one answer is chosen", () => {
|
|
433
|
+
let state = initialMachineState([multiQuestion()]);
|
|
434
|
+
const blocked = step(state, KeyEvent.enter); // no selection yet
|
|
435
|
+
expect(blocked.action.type).toBe("none");
|
|
436
|
+
expect(state.answers.get("q1")).toBeUndefined();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("completes a single multi-select question on Enter", () => {
|
|
440
|
+
let state = initialMachineState([multiQuestion()]);
|
|
441
|
+
state = step(state, KeyEvent.char(" ")).state; // Yes
|
|
442
|
+
const result = step(state, KeyEvent.enter); // confirm
|
|
443
|
+
expect(result.action.type).toBe("complete");
|
|
444
|
+
if (result.action.type === "complete") {
|
|
445
|
+
expect(result.action.result.cancelled).toBe(false);
|
|
446
|
+
expect(result.action.result.answers.map((a) => a.label)).toEqual(["Yes"]);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it("combines chosen options and custom answers in the result", () => {
|
|
451
|
+
let state = initialMachineState([multiQuestion()]);
|
|
452
|
+
state = step(state, KeyEvent.char(" ")).state; // Yes
|
|
453
|
+
state = step(state, KeyEvent.down).state;
|
|
454
|
+
state = step(state, KeyEvent.char(" ")).state; // No
|
|
455
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
456
|
+
state = step(state, KeyEvent.char(" ")).state;
|
|
457
|
+
for (const ch of ["M", "y"]) state = step(state, KeyEvent.char(ch)).state;
|
|
458
|
+
state = step(state, KeyEvent.enter).state; // submit custom -> chip at index 2
|
|
459
|
+
const result = step(state, KeyEvent.enter); // confirm
|
|
460
|
+
expect(result.action.type).toBe("complete");
|
|
461
|
+
if (result.action.type === "complete") {
|
|
462
|
+
expect(result.action.result.answers.map((a) => [a.label, a.wasCustom])).toEqual([
|
|
463
|
+
["Yes", false],
|
|
464
|
+
["No", false],
|
|
465
|
+
["My", true],
|
|
466
|
+
]);
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("advances a multi-select question to the next tab on Enter", () => {
|
|
471
|
+
let state = initialMachineState([multiQuestion(), question({ id: "q2" })]);
|
|
472
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0 on q1
|
|
473
|
+
const done = step(state, KeyEvent.enter); // confirm -> q2
|
|
474
|
+
expect(done.action.type).toBe("none");
|
|
475
|
+
expect(done.state.currentTab).toBe(1);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it("resets the highlight when returning to an answered multi-select tab", () => {
|
|
479
|
+
let state = initialMachineState([multiQuestion(), question({ id: "q2" })]);
|
|
480
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0
|
|
481
|
+
state = step(state, KeyEvent.enter).state; // advance to q2
|
|
482
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
483
|
+
expect(state.currentTab).toBe(0);
|
|
484
|
+
expect(state.optionIndex).toBe(0); // multi-select has no single highlight
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it("hides the add entry when allowOther is false", () => {
|
|
488
|
+
const state = initialMachineState([question({ multiple: true, allowOther: false })]);
|
|
489
|
+
expect(multiEntries(state).map((e) => e.kind)).toEqual(["option", "option"]);
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
|
|
339
493
|
describe("file suggestions", () => {
|
|
340
494
|
const suggestions = (over: Partial<SuggestionState> = {}): SuggestionState => ({
|
|
341
495
|
prefix: "@src",
|
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
|
@@ -28,6 +28,7 @@ const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
|
28
28
|
prompt: "Pick one?",
|
|
29
29
|
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
30
30
|
allowOther: true,
|
|
31
|
+
multiple: false,
|
|
31
32
|
...over,
|
|
32
33
|
});
|
|
33
34
|
|
|
@@ -188,6 +189,57 @@ describe("buildScene: multi question", () => {
|
|
|
188
189
|
});
|
|
189
190
|
});
|
|
190
191
|
|
|
192
|
+
describe("buildScene: multi-select question", () => {
|
|
193
|
+
const multiQuestion = (): QuestionType => question({ multiple: true });
|
|
194
|
+
|
|
195
|
+
it("renders checkbox options and an add entry", () => {
|
|
196
|
+
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
197
|
+
const lines = plainLines(scene);
|
|
198
|
+
expect(lines[3]).toBe(boxed("→ □ 1. Yes", 60));
|
|
199
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
200
|
+
expect(lines[5]).toBe(boxed(" + Add your own answer", 60));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("shows multi-select controls in the help line", () => {
|
|
204
|
+
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
205
|
+
expect(plainLines(scene).some((l) => l.includes("Space toggles • Enter confirm"))).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("marks selected options with a filled box", () => {
|
|
209
|
+
let state = initialMachineState([multiQuestion()]);
|
|
210
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0
|
|
211
|
+
const lines = plainLines(buildScene(state, 60));
|
|
212
|
+
expect(lines[3]).toBe(boxed("→ ■ 1. Yes", 60));
|
|
213
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("renders an added custom answer as a chip", () => {
|
|
217
|
+
let state = initialMachineState([multiQuestion()]);
|
|
218
|
+
state = step(state, KeyEvent.down).state;
|
|
219
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
220
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
221
|
+
for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
|
|
222
|
+
state = step(state, KeyEvent.enter).state; // submit
|
|
223
|
+
const lines = plainLines(buildScene(state, 60));
|
|
224
|
+
expect(lines.some((l) => l.includes("■ hi"))).toBe(true);
|
|
225
|
+
expect(lines.some((l) => l.includes("+ Add your own answer"))).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("shows an added custom answer in the submit summary alongside the option", () => {
|
|
229
|
+
let state = initialMachineState([multiQuestion(), question({ id: "q2" })]);
|
|
230
|
+
state = step(state, KeyEvent.char(" ")).state; // select option 0 on q1
|
|
231
|
+
state = step(state, KeyEvent.down).state;
|
|
232
|
+
state = step(state, KeyEvent.down).state; // "add"
|
|
233
|
+
state = step(state, KeyEvent.char(" ")).state; // open editor
|
|
234
|
+
for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
|
|
235
|
+
state = step(state, KeyEvent.enter).state; // submit custom
|
|
236
|
+
state = step(state, KeyEvent.enter).state; // confirm -> q2
|
|
237
|
+
state = step(state, KeyEvent.enter).state; // answer q2 -> submit tab
|
|
238
|
+
const lines = plainLines(buildScene(state, 60));
|
|
239
|
+
expect(lines.some((l) => l.includes(" Q1: 1. Yes, (wrote) hi"))).toBe(true);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
191
243
|
describe("buildScene: editor mode", () => {
|
|
192
244
|
it("shows the editor box with prompt and options", () => {
|
|
193
245
|
const scene = buildScene(openEditor(initialMachineState([question()])), 40);
|
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,10 +334,15 @@ 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
|
|
package/src/sdk/pi-ui.test.ts
CHANGED
|
@@ -27,6 +27,7 @@ describe("makeSceneRenderer cache", () => {
|
|
|
27
27
|
prompt: "Pick one?",
|
|
28
28
|
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
29
29
|
allowOther: true,
|
|
30
|
+
multiple: false,
|
|
30
31
|
}),
|
|
31
32
|
]);
|
|
32
33
|
return { state, theme, paints: () => count };
|
|
@@ -128,6 +129,7 @@ describe("runQuestionUi input routing", () => {
|
|
|
128
129
|
prompt: "Pick one?",
|
|
129
130
|
options: [new Option({ label: "Yes" })],
|
|
130
131
|
allowOther: true,
|
|
132
|
+
multiple: false,
|
|
131
133
|
}),
|
|
132
134
|
];
|
|
133
135
|
|
package/src/sdk/schema.test.ts
CHANGED
|
@@ -42,6 +42,16 @@ describe("pi validation of the generated schema", () => {
|
|
|
42
42
|
expect(validateToolCall(tools, call(args))).toEqual(args);
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
+
it("accepts the multiple flag for multi-select questions", () => {
|
|
46
|
+
const args = {
|
|
47
|
+
questions: [{ prompt: "Pick any?", options: [{ label: "A" }], multiple: true }],
|
|
48
|
+
};
|
|
49
|
+
expect(validateToolCall(tools, call(args))).toEqual(args);
|
|
50
|
+
expect(() =>
|
|
51
|
+
validateToolCall(tools, call({ ...args, questions: [{ multiple: "yes" }] })),
|
|
52
|
+
).toThrow();
|
|
53
|
+
});
|
|
54
|
+
|
|
45
55
|
it("coerces stringified booleans", () => {
|
|
46
56
|
const args = { questions: [{ prompt: "p", options: [{ label: "a" }], allowOther: "false" }] };
|
|
47
57
|
expect(validateToolCall(tools, call(args))).toEqual({
|
|
@@ -42,7 +42,7 @@ describe("question tool registration", () => {
|
|
|
42
42
|
|
|
43
43
|
it("provides guidelines that each name the tool", () => {
|
|
44
44
|
const tool = register();
|
|
45
|
-
expect(tool.promptGuidelines).toHaveLength(
|
|
45
|
+
expect(tool.promptGuidelines).toHaveLength(5);
|
|
46
46
|
for (const bullet of tool.promptGuidelines ?? []) {
|
|
47
47
|
expect(bullet).toContain("question");
|
|
48
48
|
}
|
package/src/tool/question.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
decodeParams,
|
|
18
18
|
normalizeQuestions,
|
|
19
19
|
prepareQuestionArgs,
|
|
20
|
+
type Answer,
|
|
20
21
|
} from "../core/domain.ts";
|
|
21
22
|
import { runQuestionUi } from "../sdk/pi-ui.ts";
|
|
22
23
|
|
|
@@ -33,6 +34,7 @@ const questionToolSystemPromptContribution = {
|
|
|
33
34
|
"Use question whenever you need the user to decide something, pick between options, or confirm a decision — instead of ending your reply with a question in plain text",
|
|
34
35
|
"Ask all open questions in one question call, with one entry per question in questions[]; a single question shows a simple option list, multiple questions show a tabbed interface with a submit tab",
|
|
35
36
|
"Keep options short: a label plus an optional one-line description, 2-5 options per question. The user can also type a free-text answer ('Type something.'), so only set allowOther: false when one of the listed options is required",
|
|
37
|
+
"Set multiple: true on a question when the user should pick one or more answers (checkboxes) rather than a single option — for example instead of asking several binary questions, one per option; the user can still add custom alternatives when allowOther is true",
|
|
36
38
|
"Treat a cancelled question result as the user declining to answer: do not re-ask unless the answer is essential, and then ask once more in a different form",
|
|
37
39
|
],
|
|
38
40
|
} as const;
|
|
@@ -57,7 +59,7 @@ export default function question(pi: ExtensionAPI) {
|
|
|
57
59
|
name: "question",
|
|
58
60
|
label: "Question",
|
|
59
61
|
description:
|
|
60
|
-
"Ask the user one or more multiple-choice questions and get their answers back. Use whenever you need input from the user: clarifying ambiguous requirements, choosing between approaches or options, or confirming a decision. Prefer this over asking in plain text — it renders as a selectable list (single question) or a tabbed form (multiple questions) and lets the user type a custom answer.",
|
|
62
|
+
"Ask the user one or more multiple-choice questions and get their answers back. Use whenever you need input from the user: clarifying ambiguous requirements, choosing between approaches or options, or confirming a decision. Prefer this over asking in plain text — it renders as a selectable list (single question) or a tabbed form (multiple questions) and lets the user type a custom answer. Set multiple: true to ask for one or more answers as checkboxes (for example, instead of asking several binary yes/no questions, one per option).",
|
|
61
63
|
// Single source of truth: generated from the Effect schema in the core.
|
|
62
64
|
parameters: QuestionListParamsJsonSchema as TSchema,
|
|
63
65
|
promptSnippet: questionToolSystemPromptContribution.snippet,
|
|
@@ -109,12 +111,20 @@ export default function question(pi: ExtensionAPI) {
|
|
|
109
111
|
};
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
// Group answers by question so a multi-select question reads as one
|
|
115
|
+
// block ("Q1: selected 1. A, wrote: X") rather than several rows.
|
|
116
|
+
const byId = new Map<string, Answer[]>();
|
|
117
|
+
for (const a of result.answers) {
|
|
118
|
+
const list = byId.get(a.id) ?? [];
|
|
119
|
+
list.push(a);
|
|
120
|
+
byId.set(a.id, list);
|
|
121
|
+
}
|
|
122
|
+
const answerLines = [...byId.entries()].map(([id, ans]) => {
|
|
123
|
+
const qLabel = questions.find((q) => q.id === id)?.label ?? id;
|
|
124
|
+
const parts = ans.map((a) =>
|
|
125
|
+
a.wasCustom ? `user wrote: ${a.label}` : `user selected: ${a.index}. ${a.label}`,
|
|
126
|
+
);
|
|
127
|
+
return `${qLabel}: ${parts.join(", ")}`;
|
|
118
128
|
});
|
|
119
129
|
|
|
120
130
|
return {
|
|
@@ -163,12 +173,20 @@ export default function question(pi: ExtensionAPI) {
|
|
|
163
173
|
if (details.cancelled) {
|
|
164
174
|
return new Text(theme.fg("warning", "Cancelled"), 0, 0);
|
|
165
175
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
176
|
+
// Group by question so a multi-select answer renders as one line.
|
|
177
|
+
const grouped = new Map<string, Array<(typeof details.answers)[number]>>();
|
|
178
|
+
for (const a of details.answers ?? []) {
|
|
179
|
+
const list = grouped.get(a.id) ?? [];
|
|
180
|
+
list.push(a);
|
|
181
|
+
grouped.set(a.id, list);
|
|
182
|
+
}
|
|
183
|
+
const lines = [...grouped.entries()].map(([id, ans]) => {
|
|
184
|
+
const parts = ans.map((a) =>
|
|
185
|
+
a.wasCustom
|
|
186
|
+
? `${theme.fg("muted", "(wrote) ")}${a.label}`
|
|
187
|
+
: `${a.index ? `${a.index}. ` : ""}${a.label}`,
|
|
188
|
+
);
|
|
189
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", id)}: ${parts.join(", ")}`;
|
|
172
190
|
});
|
|
173
191
|
return new Text(lines.join("\n"), 0, 0);
|
|
174
192
|
},
|