@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 CHANGED
@@ -62,7 +62,7 @@ with pi-specific adapters under `src/sdk/`.
62
62
 
63
63
  The model calls `question` whenever it needs the user to decide something,
64
64
  pick between options, or confirm a decision. Ask all open questions in one
65
- `question` call, with one entry per question in `questions[]`.
65
+ `question` call. Put one question per entry in `questions[]`.
66
66
 
67
67
  ### Parameters
68
68
 
@@ -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,13 +120,18 @@ 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
 
111
- Treat a cancelled result as the user declining to answer. Do not re-ask
112
- unless the answer is essential; then ask once more in a different form.
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
+
133
+ Treat a cancelled result as a declined answer. Do not re-ask unless the
134
+ answer is needed for the next step. When you re-ask, change the form.
113
135
 
114
136
  ## Pairing with the tracker
115
137
 
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.2.1",
3
+ "version": "0.4.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,11 +42,11 @@
41
42
  "fmt": "oxfmt -c oxfmt.config.ts"
42
43
  },
43
44
  "dependencies": {
44
- "@ftrdotdev/pi-tui": "^0.2.1",
45
+ "@ftrdotdev/pi-tui": "^0.4.0",
45
46
  "effect": "4.0.0-rc.108"
46
47
  },
47
48
  "devDependencies": {
48
- "@earendil-works/pi-ai": "0.84.2",
49
+ "@earendil-works/pi-ai": "0.84.4",
49
50
  "@effect/tsgo": "0.36.4",
50
51
  "@effect/vitest": "4.0.0-rc.108",
51
52
  "@types/bun": "latest",
@@ -62,7 +63,7 @@
62
63
  },
63
64
  "pi": {
64
65
  "extensions": [
65
- "./src/tool/question.ts"
66
+ "./index.ts"
66
67
  ]
67
68
  }
68
69
  }
@@ -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
- { id: "x", label: "Scope", prompt: "p", options: [{ label: "a" }], allowOther: false },
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", () => {
@@ -151,4 +168,42 @@ describe("normalizeQuestions", () => {
151
168
  expect(questions[0]?.options[0]?.label).toBe("a");
152
169
  expect(questions[0]?.options[0]?.description).toBe("d");
153
170
  });
171
+
172
+ it("strips leading marker emoji from prompt, label and options", () => {
173
+ const questions = normalizeQuestions(
174
+ decodeParams({
175
+ questions: [
176
+ {
177
+ id: "q7",
178
+ label: "❓ Q7 deployBlock",
179
+ prompt:
180
+ "❓ Q7 — deployBlock guard: should anything guard it?\n\n➡️ Recommended: A — document-only.",
181
+ options: [
182
+ {
183
+ label: "✅ Document-only",
184
+ description:
185
+ "➡️ Recommended. Zero mechanics; the Slice-13 gate + docs cover operator responsibility.",
186
+ },
187
+ { label: "One-shot warning log" },
188
+ ],
189
+ },
190
+ ],
191
+ }),
192
+ );
193
+ const q = questions[0]!;
194
+ expect(q.label).toBe("Q7 deployBlock");
195
+ expect(q.prompt).toBe("Q7 — deployBlock guard: should anything guard it?\n\nRecommended: A — document-only.");
196
+ expect(q.options[0]?.label).toBe("Document-only");
197
+ expect(q.options[0]?.description).toBe(
198
+ "Recommended. Zero mechanics; the Slice-13 gate + docs cover operator responsibility.",
199
+ );
200
+ expect(q.options[1]?.label).toBe("One-shot warning log");
201
+ });
202
+
203
+ it("drops option descriptions that strip to empty", () => {
204
+ const questions = normalizeQuestions(
205
+ decodeParams({ questions: [{ prompt: "p", options: [{ label: "a", description: "➡️" }] }] }),
206
+ );
207
+ expect(questions[0]?.options[0]?.description).toBeUndefined();
208
+ });
154
209
  });
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  import { Schema } from "effect";
10
+ import { stripLeadingEmoji } from "./text.ts";
10
11
 
11
12
  // ---------------------------------------------------------------------------
12
13
  // Wire schemas (tool parameters) — the single source of truth. The tool's
@@ -43,6 +44,12 @@ export const QuestionParamsSchema = Schema.Struct({
43
44
  allowOther: Schema.optionalKey(
44
45
  Schema.Boolean.annotate({ description: "Allow 'Type something' option (default: true)" }),
45
46
  ),
47
+ multiple: Schema.optionalKey(
48
+ Schema.Boolean.annotate({
49
+ description:
50
+ "Ask for one or more answers instead of a single choice; renders checkboxes and lets the user add custom alternatives (default: false)",
51
+ }),
52
+ ),
46
53
  });
47
54
 
48
55
  export type QuestionParams = typeof QuestionParamsSchema.Type;
@@ -109,6 +116,7 @@ export class Question extends Schema.Class<Question>("inquiry/core/domain/Questi
109
116
  prompt: Schema.String,
110
117
  options: Schema.Array(Option),
111
118
  allowOther: Schema.Boolean,
119
+ multiple: Schema.Boolean,
112
120
  }) {}
113
121
 
114
122
  export class Answer extends Schema.Class<Answer>("inquiry/core/domain/Answer")({
@@ -130,20 +138,25 @@ export class QuestionResult extends Schema.Class<QuestionResult>(
130
138
  * Normalize wire params into domain questions.
131
139
  *
132
140
  * Applies the defaults: `id` falls back to `q{n}`, `label` to `Q{n}`,
133
- * `allowOther` defaults to `true`.
141
+ * `allowOther` defaults to `true`, `multiple` to `false`. Model-authored
142
+ * display text (prompt, label, option label/description) has leading marker
143
+ * emoji stripped — the UI already provides the selection affordances those
144
+ * markers duplicate.
134
145
  */
135
146
  export const normalizeQuestions = (params: QuestionListParams): readonly Question[] =>
136
147
  params.questions.map(
137
148
  (q, i) =>
138
149
  new Question({
139
150
  id: q.id ?? `q${i + 1}`,
140
- label: q.label ?? `Q${i + 1}`,
141
- prompt: q.prompt,
142
- options: q.options.map((o) =>
143
- o.description === undefined
144
- ? new Option({ label: o.label })
145
- : new Option({ label: o.label, description: o.description }),
146
- ),
151
+ label: stripLeadingEmoji(q.label ?? `Q${i + 1}`),
152
+ prompt: stripLeadingEmoji(q.prompt),
153
+ options: q.options.map((o) => {
154
+ const label = stripLeadingEmoji(o.label);
155
+ const description =
156
+ o.description === undefined ? undefined : stripLeadingEmoji(o.description);
157
+ return description ? new Option({ label, description }) : new Option({ label });
158
+ }),
147
159
  allowOther: q.allowOther !== false,
160
+ multiple: q.multiple === true,
148
161
  }),
149
162
  );
@@ -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]);
@@ -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",