@ftrdotdev/pi-inquiry 0.6.0 → 0.8.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 +16 -3
- package/package.json +9 -9
- package/src/core/domain.test.ts +165 -0
- package/src/core/domain.ts +50 -4
- package/src/core/flow.test.ts +16 -0
- package/src/core/machine.test.ts +23 -0
- package/src/core/machine.ts +5 -1
- package/src/core/scene.test.ts +65 -16
- package/src/core/scene.ts +41 -22
- package/src/sdk/pi-ui.test.ts +12 -0
- package/src/sdk/schema.test.ts +30 -0
- package/src/tool/question.test.ts +139 -3
- package/src/tool/question.ts +6 -6
package/README.md
CHANGED
|
@@ -71,7 +71,13 @@ pick between options, or confirm a decision. Ask all open questions in one
|
|
|
71
71
|
"questions": [
|
|
72
72
|
{
|
|
73
73
|
"prompt": "The full question text to display",
|
|
74
|
-
"options": [
|
|
74
|
+
"options": [
|
|
75
|
+
{
|
|
76
|
+
"label": "Short label",
|
|
77
|
+
"description": "Optional one-line description",
|
|
78
|
+
"recommended": true // the UI shows ✓ beside the option. Defaults to false.
|
|
79
|
+
}
|
|
80
|
+
],
|
|
75
81
|
"id": "optional-unique-id", // defaults to q1, q2, ...
|
|
76
82
|
"label": "Scope", // short tab-bar label, defaults to Q1, Q2
|
|
77
83
|
"allowOther": true, // defaults to true; false forces the listed options
|
|
@@ -83,8 +89,13 @@ pick between options, or confirm a decision. Ask all open questions in one
|
|
|
83
89
|
|
|
84
90
|
- `questions` — one or more questions. A single question shows a simple
|
|
85
91
|
option list; multiple questions show a tabbed interface with a submit tab.
|
|
86
|
-
- `options` — 2–5
|
|
87
|
-
one-line `description
|
|
92
|
+
- `options` — 2–5 options per question, each a `label`, an optional
|
|
93
|
+
one-line `description`, and an optional `recommended` flag.
|
|
94
|
+
- `recommended` — set this flag to `true` on the option you recommend to the user.
|
|
95
|
+
The UI shows `✓` in a two-cell marker column left of the label. The flag defaults
|
|
96
|
+
to `false`. On a single-choice question, recommend at most one option. The tool
|
|
97
|
+
rejects two or more recommendations as invalid parameters. On a multi-select
|
|
98
|
+
question, recommend any number of options.
|
|
88
99
|
- `allowOther` — when `true` (default), the user can also type a free-text
|
|
89
100
|
answer ("Type something."). Set it to `false` only when one of the listed
|
|
90
101
|
options is required.
|
|
@@ -105,6 +116,8 @@ the type mode (Space does too). In the result, a multi-select question
|
|
|
105
116
|
contributes one `Answer` per chosen option or typed alternative, all sharing
|
|
106
117
|
the question's `id`.
|
|
107
118
|
|
|
119
|
+
On a multi-select question, recommend any number of options with `recommended: true`.
|
|
120
|
+
|
|
108
121
|
### Result
|
|
109
122
|
|
|
110
123
|
The tool returns its answers both as human-readable content and in
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ftrdotdev/pi-inquiry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|
|
@@ -42,19 +42,19 @@
|
|
|
42
42
|
"fmt": "oxfmt"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ftrdotdev/pi-tui": "^0.
|
|
46
|
-
"effect": "4.
|
|
45
|
+
"@ftrdotdev/pi-tui": "^0.8.0",
|
|
46
|
+
"effect": "4.1.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@earendil-works/pi-ai": "1.0
|
|
50
|
-
"@effect/tsgo": "0.
|
|
51
|
-
"@effect/vitest": "4.
|
|
49
|
+
"@earendil-works/pi-ai": "1.4.0",
|
|
50
|
+
"@effect/tsgo": "0.63.0",
|
|
51
|
+
"@effect/vitest": "4.1.1",
|
|
52
52
|
"@types/bun": "latest",
|
|
53
53
|
"oxfmt": "^0.63.0",
|
|
54
|
-
"oxlint": "1.
|
|
55
|
-
"oxlint-tsgolint": "7.0.
|
|
54
|
+
"oxlint": "1.90.0",
|
|
55
|
+
"oxlint-tsgolint": "7.0.2005",
|
|
56
56
|
"typescript": "7.0.2",
|
|
57
|
-
"vitest": "^
|
|
57
|
+
"vitest": "^5.0.5"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"@earendil-works/pi-coding-agent": "*",
|
package/src/core/domain.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { Schema } from "effect";
|
|
3
3
|
import {
|
|
4
|
+
Option,
|
|
4
5
|
QuestionListParamsJsonSchema,
|
|
5
6
|
decodeParams,
|
|
6
7
|
normalizeQuestions,
|
|
@@ -8,6 +9,16 @@ import {
|
|
|
8
9
|
} from "./domain.ts";
|
|
9
10
|
|
|
10
11
|
describe("decodeParams", () => {
|
|
12
|
+
/** Message of the `SchemaError` the decoder throws, for wording pins. */
|
|
13
|
+
const decodeError = (input: unknown): string => {
|
|
14
|
+
try {
|
|
15
|
+
decodeParams(input);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return (error as Error).message;
|
|
18
|
+
}
|
|
19
|
+
throw new Error("expected decodeParams to throw");
|
|
20
|
+
};
|
|
21
|
+
|
|
11
22
|
it("decodes valid params", () => {
|
|
12
23
|
const params = decodeParams({
|
|
13
24
|
questions: [{ prompt: "Pick one?", options: [{ label: "Yes" }] }],
|
|
@@ -36,6 +47,15 @@ describe("decodeParams", () => {
|
|
|
36
47
|
expect(params.questions[0]?.options[0]?.description).toBe("Go ahead");
|
|
37
48
|
});
|
|
38
49
|
|
|
50
|
+
it("decodes the optional recommended flag on an option", () => {
|
|
51
|
+
const params = decodeParams({
|
|
52
|
+
questions: [{ prompt: "p", options: [{ label: "a", recommended: true }, { label: "b" }] }],
|
|
53
|
+
});
|
|
54
|
+
const options = params.questions[0]?.options as readonly { recommended?: boolean }[];
|
|
55
|
+
expect(options[0]?.recommended).toBe(true);
|
|
56
|
+
expect(options[1]?.recommended).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
|
|
39
59
|
it("rejects invalid input with a SchemaError", () => {
|
|
40
60
|
expect(() => decodeParams({ questions: [{ prompt: "p", options: [{ label: 42 }] }] })).toThrow(
|
|
41
61
|
Schema.SchemaError,
|
|
@@ -52,6 +72,106 @@ describe("decodeParams", () => {
|
|
|
52
72
|
});
|
|
53
73
|
expect(params.questions[0]?.options[0]?.label).toBe("Yes");
|
|
54
74
|
});
|
|
75
|
+
|
|
76
|
+
it("rejects two recommendations when multiple is absent", () => {
|
|
77
|
+
const params = {
|
|
78
|
+
questions: [
|
|
79
|
+
{
|
|
80
|
+
label: "Scope",
|
|
81
|
+
prompt: "p",
|
|
82
|
+
options: [
|
|
83
|
+
{ label: "a", recommended: true },
|
|
84
|
+
{ label: "b", recommended: true },
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
expect(() => decodeParams(params)).toThrow(Schema.SchemaError);
|
|
90
|
+
const message = decodeError(params);
|
|
91
|
+
expect(message).toContain('question "Scope" is single-choice but has 2 recommended options');
|
|
92
|
+
expect(message).toContain("keep at most one recommended option, or set multiple: true");
|
|
93
|
+
expect(message).toContain('at ["questions"][0]["options"][1]');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("rejects two recommendations when multiple is false", () => {
|
|
97
|
+
const params = {
|
|
98
|
+
questions: [
|
|
99
|
+
{
|
|
100
|
+
label: "Scope",
|
|
101
|
+
prompt: "p",
|
|
102
|
+
multiple: false,
|
|
103
|
+
options: [
|
|
104
|
+
{ label: "a", recommended: true },
|
|
105
|
+
{ label: "b", recommended: true },
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
expect(() => decodeParams(params)).toThrow(Schema.SchemaError);
|
|
111
|
+
expect(decodeError(params)).toContain('at ["questions"][0]["options"][1]');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("counts three recommendations", () => {
|
|
115
|
+
const params = {
|
|
116
|
+
questions: [
|
|
117
|
+
{
|
|
118
|
+
prompt: "p",
|
|
119
|
+
options: [
|
|
120
|
+
{ label: "a", recommended: true },
|
|
121
|
+
{ label: "b", recommended: true },
|
|
122
|
+
{ label: "c", recommended: true },
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
};
|
|
127
|
+
const message = decodeError(params);
|
|
128
|
+
expect(message).toContain("has 3 recommended options");
|
|
129
|
+
expect(message.startsWith("this question is single-choice")).toBe(true);
|
|
130
|
+
expect(message).toContain('at ["questions"][0]["options"][1]');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("names the second question's pointer when it is invalid", () => {
|
|
134
|
+
const params = {
|
|
135
|
+
questions: [
|
|
136
|
+
{ prompt: "ok", options: [{ label: "a", recommended: true }] },
|
|
137
|
+
{
|
|
138
|
+
prompt: "bad",
|
|
139
|
+
options: [
|
|
140
|
+
{ label: "b", recommended: true },
|
|
141
|
+
{ label: "c", recommended: true },
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
};
|
|
146
|
+
expect(decodeError(params)).toContain('at ["questions"][1]["options"][1]');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("accepts two recommendations when multiple is true", () => {
|
|
150
|
+
const params = decodeParams({
|
|
151
|
+
questions: [
|
|
152
|
+
{
|
|
153
|
+
prompt: "p",
|
|
154
|
+
multiple: true,
|
|
155
|
+
options: [
|
|
156
|
+
{ label: "a", recommended: true },
|
|
157
|
+
{ label: "b", recommended: true },
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
expect(params.questions[0]?.options.map((o) => o.recommended)).toEqual([true, true]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("rejects a legacy-shaped call with two recommendations", () => {
|
|
166
|
+
const prepared = prepareQuestionArgs({
|
|
167
|
+
question: "Pick one?",
|
|
168
|
+
options: [
|
|
169
|
+
{ label: "a", recommended: true },
|
|
170
|
+
{ label: "b", recommended: true },
|
|
171
|
+
],
|
|
172
|
+
});
|
|
173
|
+
expect(decodeError(prepared)).toContain('at ["questions"][0]["options"][1]');
|
|
174
|
+
});
|
|
55
175
|
});
|
|
56
176
|
|
|
57
177
|
describe("prepareQuestionArgs", () => {
|
|
@@ -94,6 +214,19 @@ describe("QuestionListParamsJsonSchema", () => {
|
|
|
94
214
|
const option = question.properties.options.items;
|
|
95
215
|
expect(option.additionalProperties).toBe(true);
|
|
96
216
|
expect(option.required).toEqual(["label"]);
|
|
217
|
+
expect(Object.keys(option.properties)).toEqual(["label", "description", "recommended"]);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("does not project the recommendation rule", () => {
|
|
221
|
+
const question = schema.properties.questions.items;
|
|
222
|
+
const option = question.properties.options.items;
|
|
223
|
+
expect(option.required).toEqual(["label"]);
|
|
224
|
+
expect(option.additionalProperties).toBe(true);
|
|
225
|
+
for (const node of [option, question]) {
|
|
226
|
+
for (const artifact of ["allOf", "anyOf", "oneOf", "not", "minItems"]) {
|
|
227
|
+
expect(node).not.toHaveProperty(artifact);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
97
230
|
});
|
|
98
231
|
|
|
99
232
|
it("carries the field descriptions the LLM sees", () => {
|
|
@@ -106,6 +239,11 @@ describe("QuestionListParamsJsonSchema", () => {
|
|
|
106
239
|
expect(q.label.description).toContain("Short contextual label for tab bar");
|
|
107
240
|
expect(q.allowOther.description).toContain("default: true");
|
|
108
241
|
expect(q.options.items.properties.label.description).toBe("Display label for the option");
|
|
242
|
+
expect(q.options.items.properties.recommended).toEqual({
|
|
243
|
+
type: "boolean",
|
|
244
|
+
description:
|
|
245
|
+
"Recommend this option to the user (default: false). On a single-choice question, recommend at most one option. Set multiple: true to recommend more than one option.",
|
|
246
|
+
});
|
|
109
247
|
});
|
|
110
248
|
});
|
|
111
249
|
|
|
@@ -202,6 +340,26 @@ describe("normalizeQuestions", () => {
|
|
|
202
340
|
expect(q.options[1]?.label).toBe("One-shot warning log");
|
|
203
341
|
});
|
|
204
342
|
|
|
343
|
+
it("collapses the wire recommendation to a concrete boolean", () => {
|
|
344
|
+
const questions = normalizeQuestions(
|
|
345
|
+
decodeParams({
|
|
346
|
+
questions: [{ prompt: "p", options: [{ label: "a", recommended: true }, { label: "b" }] }],
|
|
347
|
+
}),
|
|
348
|
+
);
|
|
349
|
+
expect(questions[0]?.options[0]?.recommended).toBe(true);
|
|
350
|
+
expect(questions[0]?.options[1]?.recommended).toBe(false);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("strips label emoji without touching the recommendation", () => {
|
|
354
|
+
const questions = normalizeQuestions(
|
|
355
|
+
decodeParams({
|
|
356
|
+
questions: [{ prompt: "p", options: [{ label: "✅ Document-only", recommended: true }] }],
|
|
357
|
+
}),
|
|
358
|
+
);
|
|
359
|
+
expect(questions[0]?.options[0]?.label).toBe("Document-only");
|
|
360
|
+
expect(questions[0]?.options[0]?.recommended).toBe(true);
|
|
361
|
+
});
|
|
362
|
+
|
|
205
363
|
it("drops option descriptions that strip to empty", () => {
|
|
206
364
|
const questions = normalizeQuestions(
|
|
207
365
|
decodeParams({ questions: [{ prompt: "p", options: [{ label: "a", description: "➡️" }] }] }),
|
|
@@ -209,3 +367,10 @@ describe("normalizeQuestions", () => {
|
|
|
209
367
|
expect(questions[0]?.options[0]?.description).toBeUndefined();
|
|
210
368
|
});
|
|
211
369
|
});
|
|
370
|
+
|
|
371
|
+
describe("Option", () => {
|
|
372
|
+
it("defaults recommended to false when constructed without it", () => {
|
|
373
|
+
expect(new Option({ label: "Yes" }).recommended).toBe(false);
|
|
374
|
+
expect(new Option({ label: "Yes", recommended: true }).recommended).toBe(true);
|
|
375
|
+
});
|
|
376
|
+
});
|
package/src/core/domain.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* `QuestionResult`) are the validated internal representation.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { Schema } from "effect";
|
|
9
|
+
import { Effect, Schema } from "effect";
|
|
10
10
|
import { stripLeadingEmoji } from "./text.ts";
|
|
11
11
|
|
|
12
12
|
// ---------------------------------------------------------------------------
|
|
@@ -21,11 +21,17 @@ export const OptionParamsSchema = Schema.Struct({
|
|
|
21
21
|
description: Schema.optionalKey(
|
|
22
22
|
Schema.String.annotate({ description: "Optional description shown below label" }),
|
|
23
23
|
),
|
|
24
|
+
recommended: Schema.optionalKey(
|
|
25
|
+
Schema.Boolean.annotate({
|
|
26
|
+
description:
|
|
27
|
+
"Recommend this option to the user (default: false). On a single-choice question, recommend at most one option. Set multiple: true to recommend more than one option.",
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
24
30
|
});
|
|
25
31
|
|
|
26
32
|
export type OptionParams = typeof OptionParamsSchema.Type;
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
const QuestionParamsStruct = Schema.Struct({
|
|
29
35
|
prompt: Schema.String.annotate({ description: "The full question text to display" }),
|
|
30
36
|
options: Schema.Array(OptionParamsSchema).annotate({
|
|
31
37
|
description: "Available options to choose from",
|
|
@@ -52,7 +58,33 @@ export const QuestionParamsSchema = Schema.Struct({
|
|
|
52
58
|
),
|
|
53
59
|
});
|
|
54
60
|
|
|
55
|
-
export type QuestionParams = typeof
|
|
61
|
+
export type QuestionParams = typeof QuestionParamsStruct.Type;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A single-choice question can recommend at most one option. The rule lives on
|
|
65
|
+
* the wire record. `decodeParams` is then the only rejection point, and its
|
|
66
|
+
* failure text is the message the model already receives. The rule rejects the
|
|
67
|
+
* call and does not trim options. An absent or false `multiple` counts as
|
|
68
|
+
* single-choice. The pointer names the second recommended option. The decoder
|
|
69
|
+
* appends the pointer.
|
|
70
|
+
*/
|
|
71
|
+
const atMostOneRecommendation = Schema.makeFilter((q: QuestionParams): Schema.FilterOutput => {
|
|
72
|
+
if (q.multiple === true) return undefined;
|
|
73
|
+
const recommended = q.options.flatMap((option, index) =>
|
|
74
|
+
option.recommended === true ? [index] : [],
|
|
75
|
+
);
|
|
76
|
+
if (recommended.length <= 1) return undefined;
|
|
77
|
+
const name = q.label ?? q.id;
|
|
78
|
+
const subject = name === undefined ? "this question" : `question "${name}"`;
|
|
79
|
+
return {
|
|
80
|
+
path: ["options", recommended[1]!],
|
|
81
|
+
issue:
|
|
82
|
+
`${subject} is single-choice but has ${recommended.length} recommended options; ` +
|
|
83
|
+
`keep at most one recommended option, or set multiple: true`,
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const QuestionParamsSchema = QuestionParamsStruct.check(atMostOneRecommendation);
|
|
56
88
|
|
|
57
89
|
export const QuestionListParamsSchema = Schema.Struct({
|
|
58
90
|
questions: Schema.Array(QuestionParamsSchema).annotate({
|
|
@@ -105,9 +137,20 @@ export const prepareQuestionArgs = (args: unknown): unknown => {
|
|
|
105
137
|
// Domain model
|
|
106
138
|
// ---------------------------------------------------------------------------
|
|
107
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Collapse the wire recommendation to a concrete boolean at the wire-to-domain
|
|
142
|
+
* boundary. Nothing downstream then distinguishes "absent" from "false". The
|
|
143
|
+
* constructor default keeps hand-built `new Option({ label })` values working.
|
|
144
|
+
*/
|
|
145
|
+
const optionRecommended = Schema.Boolean.pipe(
|
|
146
|
+
Schema.withDecodingDefaultType(Effect.succeed(false)),
|
|
147
|
+
Schema.withConstructorDefault(Effect.succeed(false)),
|
|
148
|
+
);
|
|
149
|
+
|
|
108
150
|
export class Option extends Schema.Class<Option>("inquiry/core/domain/Option")({
|
|
109
151
|
label: Schema.String,
|
|
110
152
|
description: Schema.optionalKey(Schema.String),
|
|
153
|
+
recommended: optionRecommended,
|
|
111
154
|
}) {}
|
|
112
155
|
|
|
113
156
|
export class Question extends Schema.Class<Question>("inquiry/core/domain/Question")({
|
|
@@ -154,7 +197,10 @@ export const normalizeQuestions = (params: QuestionListParams): readonly Questio
|
|
|
154
197
|
const label = stripLeadingEmoji(o.label);
|
|
155
198
|
const description =
|
|
156
199
|
o.description === undefined ? undefined : stripLeadingEmoji(o.description);
|
|
157
|
-
|
|
200
|
+
const recommended = o.recommended === true;
|
|
201
|
+
return description
|
|
202
|
+
? new Option({ label, description, recommended })
|
|
203
|
+
: new Option({ label, recommended });
|
|
158
204
|
}),
|
|
159
205
|
allowOther: q.allowOther !== false,
|
|
160
206
|
multiple: q.multiple === true,
|
package/src/core/flow.test.ts
CHANGED
|
@@ -119,6 +119,22 @@ describe("runQuestionnaire", () => {
|
|
|
119
119
|
}),
|
|
120
120
|
);
|
|
121
121
|
|
|
122
|
+
it.effect("carries the recommendation in the recorded questions", () =>
|
|
123
|
+
Effect.gen(function* () {
|
|
124
|
+
const harness = yield* run(
|
|
125
|
+
[
|
|
126
|
+
question({
|
|
127
|
+
options: [new Option({ label: "Yes", recommended: true }), new Option({ label: "No" })],
|
|
128
|
+
}),
|
|
129
|
+
],
|
|
130
|
+
[KeyEvent.enter],
|
|
131
|
+
);
|
|
132
|
+
expect(harness.completed?.questions[0]?.options[0]?.recommended).toBe(true);
|
|
133
|
+
expect(harness.completed?.questions[0]?.options[1]?.recommended).toBe(false);
|
|
134
|
+
expect(harness.completed?.answers[0]?.index).toBe(1);
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
|
|
122
138
|
it.effect("collects several answers from a multi-select question", () =>
|
|
123
139
|
Effect.gen(function* () {
|
|
124
140
|
const harness = yield* run(
|
package/src/core/machine.test.ts
CHANGED
|
@@ -258,6 +258,15 @@ describe("multi question", () => {
|
|
|
258
258
|
expect(currentOptions(state).every((o) => !o.isOther)).toBe(true);
|
|
259
259
|
});
|
|
260
260
|
|
|
261
|
+
it("carries the recommendation on every option row and false on 'Type something.'", () => {
|
|
262
|
+
const state = initialMachineState([
|
|
263
|
+
question({
|
|
264
|
+
options: [new Option({ label: "Yes", recommended: true }), new Option({ label: "No" })],
|
|
265
|
+
}),
|
|
266
|
+
]);
|
|
267
|
+
expect(currentOptions(state).map((o) => o.recommended)).toEqual([true, false, false]);
|
|
268
|
+
});
|
|
269
|
+
|
|
261
270
|
it("answers with an index relative to the visible options", () => {
|
|
262
271
|
let state = initialMachineState([question({ allowOther: false })]);
|
|
263
272
|
state = step(state, KeyEvent.down).state; // "No" is index 1 of 2
|
|
@@ -348,6 +357,20 @@ describe("multi-select question", () => {
|
|
|
348
357
|
expect(isMultiEntrySelected(state, multiEntries(state)[0]!)).toBe(false);
|
|
349
358
|
});
|
|
350
359
|
|
|
360
|
+
it("carries the recommendation on option rows only", () => {
|
|
361
|
+
const state = initialMachineState([
|
|
362
|
+
question({
|
|
363
|
+
multiple: true,
|
|
364
|
+
options: [new Option({ label: "Yes", recommended: true }), new Option({ label: "No" })],
|
|
365
|
+
}),
|
|
366
|
+
]);
|
|
367
|
+
const entries = multiEntries(state);
|
|
368
|
+
expect(entries.map((e) => e.kind)).toEqual(["option", "option", "add"]);
|
|
369
|
+
expect(entries[0]).toMatchObject({ recommended: true });
|
|
370
|
+
expect(entries[1]).toMatchObject({ recommended: false });
|
|
371
|
+
expect(Object.keys(entries[2]!)).toEqual(["kind"]);
|
|
372
|
+
});
|
|
373
|
+
|
|
351
374
|
it("toggles an option on and off with space", () => {
|
|
352
375
|
let state = initialMachineState([multiQuestion()]);
|
|
353
376
|
state = step(state, KeyEvent.char(" ")).state; // toggle option 0 on
|
package/src/core/machine.ts
CHANGED
|
@@ -36,6 +36,7 @@ import type { SuggestionItem } from "./services.ts";
|
|
|
36
36
|
export interface OptionChoice {
|
|
37
37
|
readonly label: string;
|
|
38
38
|
readonly description?: string;
|
|
39
|
+
readonly recommended: boolean;
|
|
39
40
|
readonly isOther: boolean;
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -50,6 +51,7 @@ export type MultiEntry =
|
|
|
50
51
|
readonly index: number;
|
|
51
52
|
readonly label: string;
|
|
52
53
|
readonly description?: string;
|
|
54
|
+
readonly recommended: boolean;
|
|
53
55
|
}
|
|
54
56
|
| { readonly kind: "custom"; readonly label: string }
|
|
55
57
|
| { readonly kind: "add" };
|
|
@@ -68,6 +70,7 @@ export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
|
68
70
|
index: i,
|
|
69
71
|
label: o.label,
|
|
70
72
|
...(o.description === undefined ? {} : { description: o.description }),
|
|
73
|
+
recommended: o.recommended,
|
|
71
74
|
}));
|
|
72
75
|
for (const a of answers) {
|
|
73
76
|
if (a.wasCustom) entries.push({ kind: "custom", label: a.label });
|
|
@@ -147,10 +150,11 @@ export const optionsForTab = (
|
|
|
147
150
|
const options: OptionChoice[] = question.options.map((o) => ({
|
|
148
151
|
label: o.label,
|
|
149
152
|
description: o.description,
|
|
153
|
+
recommended: o.recommended,
|
|
150
154
|
isOther: false,
|
|
151
155
|
}));
|
|
152
156
|
if (question.allowOther) {
|
|
153
|
-
options.push({ label: "Type something.", isOther: true });
|
|
157
|
+
options.push({ label: "Type something.", isOther: true, recommended: false });
|
|
154
158
|
}
|
|
155
159
|
return options;
|
|
156
160
|
};
|
package/src/core/scene.test.ts
CHANGED
|
@@ -60,9 +60,9 @@ describe("buildScene: single question", () => {
|
|
|
60
60
|
expect(lines[0]).toBe(boxTop(60));
|
|
61
61
|
expect(lines[1]).toBe(boxed(" Pick one?", 60));
|
|
62
62
|
expect(lines[2]).toBe(boxed("", 60));
|
|
63
|
-
expect(lines[3]).toBe(boxed("→
|
|
64
|
-
expect(lines[4]).toBe(boxed("
|
|
65
|
-
expect(lines[5]).toBe(boxed("
|
|
63
|
+
expect(lines[3]).toBe(boxed("→ 1. Yes", 60));
|
|
64
|
+
expect(lines[4]).toBe(boxed(" 2. No", 60));
|
|
65
|
+
expect(lines[5]).toBe(boxed(" 3. Type something.", 60));
|
|
66
66
|
expect(lines[6]).toBe(boxed("", 60));
|
|
67
67
|
expect(lines[7]).toBe(boxed(" ↑↓ navigate • Enter to select • Esc to cancel", 60));
|
|
68
68
|
expect(lines[8]).toBe(boxBottom(60));
|
|
@@ -74,9 +74,16 @@ describe("buildScene: single question", () => {
|
|
|
74
74
|
// framed: [rail, marker, label, pad..., rail]
|
|
75
75
|
const selectedLine = scene.lines[3]!;
|
|
76
76
|
expect(selectedLine[1]).toEqual({ text: "→ ", style: "accent" });
|
|
77
|
-
expect(selectedLine
|
|
77
|
+
expect(selectedLine.find((s) => s.text === "1. Yes")).toEqual({
|
|
78
|
+
text: "1. Yes",
|
|
79
|
+
style: "accent",
|
|
80
|
+
});
|
|
78
81
|
const unselectedLine = scene.lines[4]!;
|
|
79
|
-
expect(unselectedLine
|
|
82
|
+
expect(unselectedLine.find((s) => s.text === "2. No")).toEqual({
|
|
83
|
+
text: "2. No",
|
|
84
|
+
style: "text",
|
|
85
|
+
});
|
|
86
|
+
// The marker column's width is pinned once, in the recommendation slice.
|
|
80
87
|
});
|
|
81
88
|
|
|
82
89
|
it("shows descriptions indented under options", () => {
|
|
@@ -85,7 +92,7 @@ describe("buildScene: single question", () => {
|
|
|
85
92
|
});
|
|
86
93
|
const scene = buildScene(initialMachineState([q]), 40);
|
|
87
94
|
const lines = plainLines(scene);
|
|
88
|
-
expect(lines.
|
|
95
|
+
expect(lines[4]).toBe(boxed(" Go ahead", 40));
|
|
89
96
|
});
|
|
90
97
|
|
|
91
98
|
it("wraps long prompts with continuation indentation", () => {
|
|
@@ -98,6 +105,48 @@ describe("buildScene: single question", () => {
|
|
|
98
105
|
});
|
|
99
106
|
});
|
|
100
107
|
|
|
108
|
+
describe("buildScene: recommended options", () => {
|
|
109
|
+
const recommended = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
110
|
+
question({
|
|
111
|
+
options: [new Option({ label: "Yes", recommended: true }), new Option({ label: "No" })],
|
|
112
|
+
...over,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("renders a ✓ in the marker column for a recommended single-choice option", () => {
|
|
116
|
+
const scene = buildScene(initialMachineState([recommended()]), 60);
|
|
117
|
+
const lines = plainLines(scene);
|
|
118
|
+
expect(lines[3]).toBe(boxed("→ ✓ 1. Yes", 60));
|
|
119
|
+
expect(scene.lines[3]![1]).toEqual({ text: "→ ", style: "accent" });
|
|
120
|
+
expect(scene.lines[3]![2]).toEqual({ text: "✓ ", style: "success" });
|
|
121
|
+
expect(scene.lines[3]![3]).toEqual({ text: "1. Yes", style: "accent" });
|
|
122
|
+
expect(lines[4]).toBe(boxed(" 2. No", 60));
|
|
123
|
+
expect(scene.lines[4]![2]).toEqual({ text: " " });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("renders a ✓ beside the checkbox for a recommended multi-select option", () => {
|
|
127
|
+
const multi = recommended({ multiple: true });
|
|
128
|
+
const unchecked = buildScene(initialMachineState([multi]), 60);
|
|
129
|
+
expect(plainLines(unchecked)[3]).toBe(boxed("→ ✓ □ 1. Yes", 60));
|
|
130
|
+
let state = initialMachineState([multi]);
|
|
131
|
+
state = step(state, KeyEvent.char(" ")).state; // check option 0
|
|
132
|
+
expect(plainLines(buildScene(state, 60))[3]).toBe(boxed("→ ✓ ■ 1. Yes", 60));
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("leaves the reserved column blank on non-option rows", () => {
|
|
136
|
+
const scene = buildScene(initialMachineState([recommended({ multiple: true })]), 60);
|
|
137
|
+
expect(plainLines(scene)[5]).toBe(boxed(" + Add your own answer", 60));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("wraps a marked long label past the marker", () => {
|
|
141
|
+
const q = question({
|
|
142
|
+
options: [new Option({ label: "Document the deployment guard", recommended: true })],
|
|
143
|
+
});
|
|
144
|
+
const lines = plainLines(buildScene(initialMachineState([q]), 26));
|
|
145
|
+
expect(lines[3]).toBe(boxed("→ ✓ 1. Document the", 26));
|
|
146
|
+
expect(lines[4]).toBe(boxed(" deployment guard", 26));
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
101
150
|
describe("buildScene: multi question", () => {
|
|
102
151
|
it("renders a tab bar with the active tab marked by a bordered square", () => {
|
|
103
152
|
const scene = buildScene(initialMachineState(twoQuestions()), 60);
|
|
@@ -144,8 +193,8 @@ describe("buildScene: multi question", () => {
|
|
|
144
193
|
state = step(state, KeyEvent.enter).state; // submit, advance to q2
|
|
145
194
|
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
146
195
|
const lines = plainLines(buildScene(state, 60));
|
|
147
|
-
expect(lines.some((l) => l.includes("→
|
|
148
|
-
expect(lines.some((l) => l.includes("
|
|
196
|
+
expect(lines.some((l) => l.includes("→ 3. Type something."))).toBe(true);
|
|
197
|
+
expect(lines.some((l) => l.includes(" Current answer: hi"))).toBe(true);
|
|
149
198
|
});
|
|
150
199
|
|
|
151
200
|
it("shows the chosen option when returning to its tab", () => {
|
|
@@ -154,7 +203,7 @@ describe("buildScene: multi question", () => {
|
|
|
154
203
|
state = step(state, KeyEvent.enter).state; // answer q1, advance to q2
|
|
155
204
|
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
156
205
|
const lines = plainLines(buildScene(state, 60));
|
|
157
|
-
expect(lines.some((l) => l.includes("→
|
|
206
|
+
expect(lines.some((l) => l.includes("→ 2. No"))).toBe(true);
|
|
158
207
|
expect(lines.some((l) => l.includes("Current answer"))).toBe(false);
|
|
159
208
|
});
|
|
160
209
|
|
|
@@ -197,9 +246,9 @@ describe("buildScene: multi-select question", () => {
|
|
|
197
246
|
it("renders checkbox options and an add entry", () => {
|
|
198
247
|
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
199
248
|
const lines = plainLines(scene);
|
|
200
|
-
expect(lines[3]).toBe(boxed("→
|
|
201
|
-
expect(lines[4]).toBe(boxed("
|
|
202
|
-
expect(lines[5]).toBe(boxed("
|
|
249
|
+
expect(lines[3]).toBe(boxed("→ □ 1. Yes", 60));
|
|
250
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
251
|
+
expect(lines[5]).toBe(boxed(" + Add your own answer", 60));
|
|
203
252
|
});
|
|
204
253
|
|
|
205
254
|
it("shows multi-select controls in the help line", () => {
|
|
@@ -211,8 +260,8 @@ describe("buildScene: multi-select question", () => {
|
|
|
211
260
|
let state = initialMachineState([multiQuestion()]);
|
|
212
261
|
state = step(state, KeyEvent.char(" ")).state; // select option 0
|
|
213
262
|
const lines = plainLines(buildScene(state, 60));
|
|
214
|
-
expect(lines[3]).toBe(boxed("→
|
|
215
|
-
expect(lines[4]).toBe(boxed("
|
|
263
|
+
expect(lines[3]).toBe(boxed("→ ■ 1. Yes", 60));
|
|
264
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
216
265
|
});
|
|
217
266
|
|
|
218
267
|
it("renders an added custom answer as a chip", () => {
|
|
@@ -250,8 +299,8 @@ describe("buildScene: editor mode", () => {
|
|
|
250
299
|
expect(lines).toContain(boxed(" Your answer:", 40));
|
|
251
300
|
expect(lines.some((l) => l.includes("Shift+Enter"))).toBe(true);
|
|
252
301
|
expect(lines.some((l) => l.includes("Esc to go back"))).toBe(true);
|
|
253
|
-
expect(lines[3]).toBe(boxed("
|
|
254
|
-
expect(lines[5]).toBe(boxed("→
|
|
302
|
+
expect(lines[3]).toBe(boxed(" 1. Yes", 40));
|
|
303
|
+
expect(lines[5]).toBe(boxed("→ 3. Type something. ✎", 40));
|
|
255
304
|
// editor box frame, prefixed with the gutter space, inside the outer rails
|
|
256
305
|
expect(lines[8]).toBe("│ ╭" + "─".repeat(35) + "╮│");
|
|
257
306
|
});
|
package/src/core/scene.ts
CHANGED
|
@@ -155,6 +155,21 @@ const breakLongSpan = (token: Span, width: number): readonly Span[][] => {
|
|
|
155
155
|
return chunks.map((text) => [{ text, style: token.style, bold: token.bold }]);
|
|
156
156
|
};
|
|
157
157
|
|
|
158
|
+
/** Description and subtitle indent for the option block: seven cells, two more than before the marker column. */
|
|
159
|
+
const OPTION_TEXT_INDENT = " ";
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The option block's left gutter: the cursor cell, then the reserved marker
|
|
163
|
+
* column. Every row reserves the column, so the block has one geometry
|
|
164
|
+
* regardless of question data. The `✓` is the tab bar's success glyph. The
|
|
165
|
+
* cell after `✓` stays blank, so labels start at the same column with or
|
|
166
|
+
* without a recommendation.
|
|
167
|
+
*/
|
|
168
|
+
const optionRowPrefix = (selected: boolean, recommended: boolean): SceneLine => [
|
|
169
|
+
selected ? span("→ ", "accent") : span(" "),
|
|
170
|
+
recommended ? span("✓ ", "success") : span(" "),
|
|
171
|
+
];
|
|
172
|
+
|
|
158
173
|
/**
|
|
159
174
|
* Build the scene for the current machine state.
|
|
160
175
|
*
|
|
@@ -174,16 +189,16 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
174
189
|
const question = state.questions[state.currentTab];
|
|
175
190
|
const options = currentOptions(state);
|
|
176
191
|
|
|
177
|
-
const pushWrapped = (prefix:
|
|
178
|
-
const prefixWidth = visibleWidth(prefix
|
|
192
|
+
const pushWrapped = (prefix: SceneLine, content: SceneLine): void => {
|
|
193
|
+
const prefixWidth = visibleWidth(plain(prefix));
|
|
179
194
|
if (prefixWidth >= contentWidth) {
|
|
180
|
-
lines.push([prefix, ...content]);
|
|
195
|
+
lines.push([...prefix, ...content]);
|
|
181
196
|
return;
|
|
182
197
|
}
|
|
183
198
|
const wrapped = wrapLine(content, contentWidth - prefixWidth);
|
|
184
199
|
for (let i = 0; i < wrapped.length; i++) {
|
|
185
200
|
lines.push(
|
|
186
|
-
i === 0 ? [prefix, ...wrapped[i]!] : [span(" ".repeat(prefixWidth)), ...wrapped[i]!],
|
|
201
|
+
i === 0 ? [...prefix, ...wrapped[i]!] : [span(" ".repeat(prefixWidth)), ...wrapped[i]!],
|
|
187
202
|
);
|
|
188
203
|
}
|
|
189
204
|
};
|
|
@@ -199,14 +214,17 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
199
214
|
const isOther = option.isOther;
|
|
200
215
|
const labelText = `${i + 1}. ${option.label}${isOther && state.inputMode ? " ✎" : ""}`;
|
|
201
216
|
const color = selected || (isOther && state.inputMode) ? "accent" : "text";
|
|
202
|
-
pushWrapped(selected
|
|
217
|
+
pushWrapped(optionRowPrefix(selected, option.recommended), [span(labelText, color)]);
|
|
203
218
|
if (option.description) {
|
|
204
|
-
pushWrapped(span(
|
|
219
|
+
pushWrapped([span(OPTION_TEXT_INDENT)], [span(option.description, "muted")]);
|
|
205
220
|
} else if (option.isOther && !state.inputMode && question) {
|
|
206
221
|
// Show an existing custom answer as a subtitle under "Type something".
|
|
207
222
|
const answer = state.answers.get(question.id)?.[0];
|
|
208
223
|
if (answer?.wasCustom) {
|
|
209
|
-
pushWrapped(
|
|
224
|
+
pushWrapped(
|
|
225
|
+
[span(OPTION_TEXT_INDENT)],
|
|
226
|
+
[span(`Current answer: ${answer.label}`, "muted")],
|
|
227
|
+
);
|
|
210
228
|
}
|
|
211
229
|
}
|
|
212
230
|
}
|
|
@@ -234,11 +252,11 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
234
252
|
} else {
|
|
235
253
|
labelText = "+ Add your own answer";
|
|
236
254
|
}
|
|
237
|
-
pushWrapped(selected
|
|
255
|
+
pushWrapped(optionRowPrefix(selected, entry.kind === "option" && entry.recommended), [
|
|
238
256
|
span(labelText, selected ? "accent" : "text"),
|
|
239
257
|
]);
|
|
240
258
|
if (description) {
|
|
241
|
-
pushWrapped(span(
|
|
259
|
+
pushWrapped([span(OPTION_TEXT_INDENT)], [span(description, "muted")]);
|
|
242
260
|
}
|
|
243
261
|
}
|
|
244
262
|
};
|
|
@@ -265,17 +283,17 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
265
283
|
isSubmitTab ? span(submitText, "accent") : span(submitText, canSubmit ? "success" : "dim"),
|
|
266
284
|
span(" →"),
|
|
267
285
|
);
|
|
268
|
-
pushWrapped(span(" "), tabs);
|
|
286
|
+
pushWrapped([span(" ")], tabs);
|
|
269
287
|
lines.push([]);
|
|
270
288
|
}
|
|
271
289
|
|
|
272
290
|
// Content
|
|
273
291
|
if (state.inputMode && question) {
|
|
274
|
-
pushWrapped(span(" "), [span(question.prompt, "text")]);
|
|
292
|
+
pushWrapped([span(" ")], [span(question.prompt, "text")]);
|
|
275
293
|
lines.push([]);
|
|
276
294
|
renderOptions();
|
|
277
295
|
lines.push([]);
|
|
278
|
-
pushWrapped(span(" "), [span("Your answer:", "muted")]);
|
|
296
|
+
pushWrapped([span(" ")], [span("Your answer:", "muted")]);
|
|
279
297
|
// The editor box spans the content between the rails minus its 1-space
|
|
280
298
|
// gutter; the popup floats two spaces deeper under the input.
|
|
281
299
|
for (const editorLine of buildEditorBox(state.editor, Math.max(1, contentWidth - 1))) {
|
|
@@ -291,14 +309,15 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
291
309
|
}
|
|
292
310
|
lines.push([]);
|
|
293
311
|
if (state.suggestions) {
|
|
294
|
-
pushWrapped(span(" "), [span("↑↓ pick • Enter/Tab complete • Esc close", "dim")]);
|
|
312
|
+
pushWrapped([span(" ")], [span("↑↓ pick • Enter/Tab complete • Esc close", "dim")]);
|
|
295
313
|
} else {
|
|
296
|
-
pushWrapped(
|
|
297
|
-
span("
|
|
298
|
-
|
|
314
|
+
pushWrapped(
|
|
315
|
+
[span(" ")],
|
|
316
|
+
[span("Enter to submit • Shift+Enter for newline • Esc to go back", "dim")],
|
|
317
|
+
);
|
|
299
318
|
}
|
|
300
319
|
} else if (state.currentTab === state.questions.length) {
|
|
301
|
-
pushWrapped(span(" "), [span("Ready to submit", "accent", true)]);
|
|
320
|
+
pushWrapped([span(" ")], [span("Ready to submit", "accent", true)]);
|
|
302
321
|
lines.push([]);
|
|
303
322
|
for (const q of state.questions) {
|
|
304
323
|
const answers = state.answers.get(q.id);
|
|
@@ -313,21 +332,21 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
313
332
|
: answers
|
|
314
333
|
.map((a) => (a.wasCustom ? `(wrote) ${a.label}` : `${a.index}. ${a.label}`))
|
|
315
334
|
.join(", ");
|
|
316
|
-
pushWrapped(span(" "), [span(`${q.label}: `, "muted"), span(rendered, "text")]);
|
|
335
|
+
pushWrapped([span(" ")], [span(`${q.label}: `, "muted"), span(rendered, "text")]);
|
|
317
336
|
}
|
|
318
337
|
}
|
|
319
338
|
lines.push([]);
|
|
320
339
|
if (isAllAnswered(state)) {
|
|
321
|
-
pushWrapped(span(" "), [span("Press Enter to submit", "success")]);
|
|
340
|
+
pushWrapped([span(" ")], [span("Press Enter to submit", "success")]);
|
|
322
341
|
} else {
|
|
323
342
|
const missing = state.questions
|
|
324
343
|
.filter((q) => !state.answers.has(q.id))
|
|
325
344
|
.map((q) => q.label)
|
|
326
345
|
.join(", ");
|
|
327
|
-
pushWrapped(span(" "), [span(`Unanswered: ${missing}`, "warning")]);
|
|
346
|
+
pushWrapped([span(" ")], [span(`Unanswered: ${missing}`, "warning")]);
|
|
328
347
|
}
|
|
329
348
|
} else if (question) {
|
|
330
|
-
pushWrapped(span(" "), [span(question.prompt, "text")]);
|
|
349
|
+
pushWrapped([span(" ")], [span(question.prompt, "text")]);
|
|
331
350
|
lines.push([]);
|
|
332
351
|
renderOptions();
|
|
333
352
|
}
|
|
@@ -343,7 +362,7 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
343
362
|
: isMultiSelect
|
|
344
363
|
? "↑↓ move • Space toggles • Enter confirm • Esc cancel"
|
|
345
364
|
: "↑↓ navigate • Enter to select • Esc to cancel";
|
|
346
|
-
pushWrapped(span(" "), [span(help, "dim")]);
|
|
365
|
+
pushWrapped([span(" ")], [span(help, "dim")]);
|
|
347
366
|
}
|
|
348
367
|
|
|
349
368
|
// Frame the interior: accent border with rails, body padded to the content
|
package/src/sdk/pi-ui.test.ts
CHANGED
|
@@ -217,6 +217,18 @@ describe("questionClipboardText", () => {
|
|
|
217
217
|
expect(text.endsWith("3. Type something.")).toBe(true);
|
|
218
218
|
});
|
|
219
219
|
|
|
220
|
+
it("ignores the recommended flag", () => {
|
|
221
|
+
const q = new Question({
|
|
222
|
+
id: "q1",
|
|
223
|
+
label: "Q1",
|
|
224
|
+
prompt: "Pick one?",
|
|
225
|
+
options: [new Option({ label: "First", recommended: true }), new Option({ label: "Second" })],
|
|
226
|
+
allowOther: false,
|
|
227
|
+
multiple: false,
|
|
228
|
+
});
|
|
229
|
+
expect(questionClipboardText(initialMachineState([q]))).toBe("Pick one?\n1. First\n2. Second");
|
|
230
|
+
});
|
|
231
|
+
|
|
220
232
|
it("is empty on the submit tab", () => {
|
|
221
233
|
const state = { ...initialMachineState([question(false)]), currentTab: 1 };
|
|
222
234
|
expect(questionClipboardText(state)).toBe("");
|
package/src/sdk/schema.test.ts
CHANGED
|
@@ -59,6 +59,36 @@ describe("pi validation of the generated schema", () => {
|
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
it("accepts two recommendations (our gate is the single rejection point)", () => {
|
|
63
|
+
const args = {
|
|
64
|
+
questions: [
|
|
65
|
+
{
|
|
66
|
+
prompt: "p",
|
|
67
|
+
options: [
|
|
68
|
+
{ label: "a", recommended: true },
|
|
69
|
+
{ label: "b", recommended: true },
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
expect(validateToolCall(tools, call(args))).toEqual(args);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("coerces a stringified recommended flag and rejects a non-boolean", () => {
|
|
78
|
+
const args = {
|
|
79
|
+
questions: [{ prompt: "p", options: [{ label: "a", recommended: "true" }] }],
|
|
80
|
+
};
|
|
81
|
+
expect(validateToolCall(tools, call(args))).toEqual({
|
|
82
|
+
questions: [{ prompt: "p", options: [{ label: "a", recommended: true }] }],
|
|
83
|
+
});
|
|
84
|
+
expect(() =>
|
|
85
|
+
validateToolCall(
|
|
86
|
+
tools,
|
|
87
|
+
call({ questions: [{ prompt: "p", options: [{ label: "a", recommended: "yes" }] }] }),
|
|
88
|
+
),
|
|
89
|
+
).toThrow(/recommended.*must be boolean/);
|
|
90
|
+
});
|
|
91
|
+
|
|
62
92
|
it("accepts an empty questions array (handled in execute)", () => {
|
|
63
93
|
expect(validateToolCall(tools, call({ questions: [] }))).toEqual({ questions: [] });
|
|
64
94
|
});
|
|
@@ -3,10 +3,21 @@
|
|
|
3
3
|
* parameter schema (single-sourced from the core), and the legacy
|
|
4
4
|
* prepareArguments shim wiring.
|
|
5
5
|
*/
|
|
6
|
-
import { describe, expect, it } from "vitest";
|
|
7
|
-
import type {
|
|
6
|
+
import { describe, expect, it, vi } from "vitest";
|
|
7
|
+
import type {
|
|
8
|
+
ExtensionAPI,
|
|
9
|
+
ExtensionToolContext,
|
|
10
|
+
Theme,
|
|
11
|
+
ToolDefinition,
|
|
12
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
13
|
import question from "./question.ts";
|
|
9
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
Answer,
|
|
16
|
+
Option,
|
|
17
|
+
Question,
|
|
18
|
+
QuestionListParamsJsonSchema,
|
|
19
|
+
QuestionResult,
|
|
20
|
+
} from "../core/domain.ts";
|
|
10
21
|
|
|
11
22
|
const register = (): ToolDefinition<any, unknown, any> => {
|
|
12
23
|
let captured: ToolDefinition<any, unknown, any> | undefined;
|
|
@@ -20,6 +31,18 @@ const register = (): ToolDefinition<any, unknown, any> => {
|
|
|
20
31
|
return captured;
|
|
21
32
|
};
|
|
22
33
|
|
|
34
|
+
/** Theme double: every style and colour returns its text unchanged. */
|
|
35
|
+
const theme = {
|
|
36
|
+
style: (text: string) => text,
|
|
37
|
+
fg: (_color: string, text: string) => text,
|
|
38
|
+
} as unknown as Theme;
|
|
39
|
+
|
|
40
|
+
const rendered = (component: { render(width: number): string[] }): string =>
|
|
41
|
+
component
|
|
42
|
+
.render(200)
|
|
43
|
+
.map((l) => l.trimEnd())
|
|
44
|
+
.join("\n");
|
|
45
|
+
|
|
23
46
|
describe("question tool registration", () => {
|
|
24
47
|
it("registers under the legacy name with a stable label", () => {
|
|
25
48
|
const tool = register();
|
|
@@ -68,3 +91,116 @@ describe("question tool registration", () => {
|
|
|
68
91
|
expect(tool.prepareArguments?.(passthrough)).toBe(passthrough);
|
|
69
92
|
});
|
|
70
93
|
});
|
|
94
|
+
|
|
95
|
+
describe("question tool renderers and execute (unchanged surfaces)", () => {
|
|
96
|
+
it("renderCall ignores the recommended flag", () => {
|
|
97
|
+
const tool = register();
|
|
98
|
+
const out = tool.renderCall!(
|
|
99
|
+
{
|
|
100
|
+
questions: [
|
|
101
|
+
{
|
|
102
|
+
prompt: "Pick one?",
|
|
103
|
+
options: [{ label: "First", recommended: true }, { label: "Second" }],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
} as any,
|
|
107
|
+
theme,
|
|
108
|
+
{} as any,
|
|
109
|
+
);
|
|
110
|
+
expect(rendered(out)).toBe(
|
|
111
|
+
"question Pick one?\n Options: 1. First, 2. Second, 3. Type something.",
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("renderResult ignores the recommended flag", () => {
|
|
116
|
+
const tool = register();
|
|
117
|
+
const result = {
|
|
118
|
+
content: [{ type: "text", text: "Q1: user selected: 1. First" }],
|
|
119
|
+
details: {
|
|
120
|
+
questions: [
|
|
121
|
+
{
|
|
122
|
+
id: "q1",
|
|
123
|
+
label: "Q1",
|
|
124
|
+
prompt: "Pick one?",
|
|
125
|
+
options: [{ label: "First", recommended: true }],
|
|
126
|
+
allowOther: false,
|
|
127
|
+
multiple: false,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
answers: [{ id: "q1", label: "First", wasCustom: false, index: 1 }],
|
|
131
|
+
cancelled: false,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
const out = tool.renderResult!(result as any, {} as any, theme, {} as any);
|
|
135
|
+
expect(rendered(out)).toBe("✓ q1: 1. First");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("execute keeps the marker out of the answer text", async () => {
|
|
139
|
+
const tool = register();
|
|
140
|
+
const recorded = new Question({
|
|
141
|
+
id: "q1",
|
|
142
|
+
label: "Q1",
|
|
143
|
+
prompt: "Pick one?",
|
|
144
|
+
options: [new Option({ label: "First", recommended: true })],
|
|
145
|
+
allowOther: false,
|
|
146
|
+
multiple: false,
|
|
147
|
+
});
|
|
148
|
+
const result = new QuestionResult({
|
|
149
|
+
questions: [recorded],
|
|
150
|
+
answers: [new Answer({ id: "q1", label: "First", wasCustom: false, index: 1 })],
|
|
151
|
+
cancelled: false,
|
|
152
|
+
});
|
|
153
|
+
const custom = vi.fn(() => Promise.resolve(result));
|
|
154
|
+
const ctx = { mode: "tui", ui: { custom } } as unknown as ExtensionToolContext;
|
|
155
|
+
const out = await tool.execute(
|
|
156
|
+
"call-1",
|
|
157
|
+
{
|
|
158
|
+
questions: [
|
|
159
|
+
{
|
|
160
|
+
id: "q1",
|
|
161
|
+
label: "Q1",
|
|
162
|
+
prompt: "Pick one?",
|
|
163
|
+
options: [{ label: "First", recommended: true }],
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
undefined,
|
|
168
|
+
undefined,
|
|
169
|
+
ctx,
|
|
170
|
+
);
|
|
171
|
+
expect(out.content[0]).toEqual({ type: "text", text: "Q1: user selected: 1. First" });
|
|
172
|
+
expect(out.details).toBe(result);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("question tool execute: invalid recommendations", () => {
|
|
177
|
+
it("returns one invalid-parameters message and cancelled details without opening the UI", async () => {
|
|
178
|
+
const tool = register();
|
|
179
|
+
const custom = vi.fn();
|
|
180
|
+
const ctx = { mode: "tui", ui: { custom } } as unknown as ExtensionToolContext;
|
|
181
|
+
const out = await tool.execute(
|
|
182
|
+
"call-1",
|
|
183
|
+
{
|
|
184
|
+
questions: [
|
|
185
|
+
{
|
|
186
|
+
prompt: "Pick one?",
|
|
187
|
+
options: [
|
|
188
|
+
{ label: "a", recommended: true },
|
|
189
|
+
{ label: "b", recommended: true },
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
},
|
|
194
|
+
undefined,
|
|
195
|
+
undefined,
|
|
196
|
+
ctx,
|
|
197
|
+
);
|
|
198
|
+
expect(out.content).toHaveLength(1);
|
|
199
|
+
const entry = out.content[0] as { type: string; text: string };
|
|
200
|
+
expect(entry.type).toBe("text");
|
|
201
|
+
expect(entry.text.startsWith("Error: invalid parameters: ")).toBe(true);
|
|
202
|
+
expect(entry.text).toContain("keep at most one recommended option, or set multiple: true");
|
|
203
|
+
expect(out.details).toEqual({ questions: [], answers: [], cancelled: true });
|
|
204
|
+
expect(custom).not.toHaveBeenCalled();
|
|
205
|
+
});
|
|
206
|
+
});
|
package/src/tool/question.ts
CHANGED
|
@@ -29,12 +29,12 @@ import { runQuestionUi } from "../sdk/pi-ui.ts";
|
|
|
29
29
|
*/
|
|
30
30
|
const questionToolSystemPromptContribution = {
|
|
31
31
|
snippet:
|
|
32
|
-
"Ask the user to decide
|
|
32
|
+
"Ask the user to decide with interactive questions, selectable options, or a typed answer",
|
|
33
33
|
guidelines: [
|
|
34
|
-
"Use question
|
|
35
|
-
"Ask all open questions in one call. Put one question per entry in questions[]. A single question shows
|
|
36
|
-
"
|
|
37
|
-
"Set multiple: true
|
|
34
|
+
"Use the question tool when the user must decide, choose between options, or confirm. Use the tool instead of ending your reply with a question in plain text.",
|
|
35
|
+
"Ask all open questions in one call. Put one question per entry in questions[]. A single question shows an option list. Multiple questions show a tabbed interface with a submit tab.",
|
|
36
|
+
"Write 2-5 options per question. Each option has a label. An option can also have a one-line description. The user can type a free-text answer at 'Type something.'. Set allowOther: false only when one of the listed options is required.",
|
|
37
|
+
"Set multiple: true when the user can pick more than one answer. The options render as checkboxes. A multi-select question has many options. You do not need many yes/no questions. The user can still add custom alternatives when allowOther is true.",
|
|
38
38
|
"Treat a cancelled question result as a declined answer. Do not re-ask unless the answer is needed for the next step. When you re-ask, change the form.",
|
|
39
39
|
],
|
|
40
40
|
} as const;
|
|
@@ -59,7 +59,7 @@ export default function question(pi: ExtensionAPI) {
|
|
|
59
59
|
name: "question",
|
|
60
60
|
label: "Question",
|
|
61
61
|
description:
|
|
62
|
-
"Ask the user one or more multiple-choice questions
|
|
62
|
+
"Ask the user one or more multiple-choice questions. The tool returns the user's answers. Use the tool when you need input from the user. Clarify requirements, choose an approach, or confirm a decision. Prefer this over asking in plain text. A single question shows an option list. Multiple questions show a tabbed interface with a submit tab. The user can also type a custom answer. Set multiple: true to ask for one or more answers as checkboxes.",
|
|
63
63
|
// Single source of truth: generated from the Effect schema in the core.
|
|
64
64
|
parameters: QuestionListParamsJsonSchema as TSchema,
|
|
65
65
|
promptSnippet: questionToolSystemPromptContribution.snippet,
|