@ftrdotdev/pi-inquiry 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -4
- package/index.ts +1 -0
- package/package.json +5 -4
- package/src/core/domain.test.ts +56 -1
- package/src/core/domain.ts +21 -8
- package/src/core/flow.test.ts +43 -0
- package/src/core/machine.test.ts +157 -3
- package/src/core/machine.ts +171 -24
- package/src/core/scene.test.ts +155 -0
- package/src/core/scene.ts +94 -13
- package/src/core/text.test.ts +85 -1
- package/src/core/text.ts +92 -7
- package/src/sdk/pi-ui.test.ts +2 -0
- package/src/sdk/schema.test.ts +10 -0
- package/src/tool/question.test.ts +1 -1
- package/src/tool/question.ts +33 -15
package/src/core/text.test.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
charWidth,
|
|
4
|
+
stripLeadingEmoji,
|
|
5
|
+
truncateToWidth,
|
|
6
|
+
visibleWidth,
|
|
7
|
+
wrapText,
|
|
8
|
+
} from "./text.ts";
|
|
3
9
|
|
|
4
10
|
describe("visibleWidth", () => {
|
|
5
11
|
it("counts ASCII as one column per character", () => {
|
|
@@ -29,6 +35,84 @@ describe("visibleWidth", () => {
|
|
|
29
35
|
it("counts combining marks as zero", () => {
|
|
30
36
|
expect(visibleWidth("e\u0301")).toBe(1);
|
|
31
37
|
});
|
|
38
|
+
|
|
39
|
+
// pi-tui measures these (its `visibleWidth` backs pi's crash guard, which
|
|
40
|
+
// hard-exits on any custom-UI line wider than the terminal), so inquiry must
|
|
41
|
+
// agree to the column. Explored cases: models prefix prompts and option text
|
|
42
|
+
// with `➡️`/`⚠️`/`❓`; the crash that started this work was a `➡️` under-
|
|
43
|
+
// measured as one column, producing a line pi refused to render.
|
|
44
|
+
it("counts emoji with variation selector as two columns (crash parity)", () => {
|
|
45
|
+
expect(visibleWidth("➡️")).toBe(2);
|
|
46
|
+
expect(visibleWidth("⚠️")).toBe(2);
|
|
47
|
+
expect(visibleWidth("❓")).toBe(2);
|
|
48
|
+
expect(visibleWidth("✈️")).toBe(2);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("counts bare text-presentation arrows and checks as one column (pi-tui parity)", () => {
|
|
52
|
+
expect(visibleWidth("➡")).toBe(1);
|
|
53
|
+
expect(visibleWidth("⚠")).toBe(1);
|
|
54
|
+
expect(visibleWidth("✔")).toBe(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("counts flags as two columns", () => {
|
|
58
|
+
expect(visibleWidth("🇺🇸")).toBe(2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("counts zwj family emoji per cluster like pi-tui", () => {
|
|
62
|
+
// 2 when the runtime groups the family into one grapheme, 6 when it
|
|
63
|
+
// segments the members (both are what pi-tui's rule yields on that engine).
|
|
64
|
+
expect([2, 6]).toContain(visibleWidth("👨👩👧"));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("widths add up across the crash prompt continuation", () => {
|
|
68
|
+
const text =
|
|
69
|
+
"➡️ Recommended: A — document-only. Zero mechanics, and (b) is a 10-line add if you want the operator signal.";
|
|
70
|
+
// Exactly what pi-tui measured: 108 columns, one more than our old
|
|
71
|
+
// per-code-point measure (107), which made the renderer skip wrapping.
|
|
72
|
+
expect(visibleWidth(text)).toBe(108);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("wraps the crash prompt continuation at the render width", () => {
|
|
76
|
+
const text =
|
|
77
|
+
"➡️ Recommended: A — document-only. Zero mechanics, and (b) is a 10-line add if you want the operator signal.";
|
|
78
|
+
// Render width: 110-col terminal, 1 rail each side, 1-column prompt gutter.
|
|
79
|
+
const wrapped = wrapText(text, 107);
|
|
80
|
+
expect(wrapped.length).toBeGreaterThan(1);
|
|
81
|
+
for (const line of wrapped) expect(visibleWidth(line)).toBeLessThanOrEqual(107);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("truncates on grapheme boundaries", () => {
|
|
85
|
+
expect(truncateToWidth("➡️ xy", 3)).toBe("➡️ ");
|
|
86
|
+
expect(truncateToWidth("➡️ xy", 2)).toBe("➡️");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("stripLeadingEmoji", () => {
|
|
91
|
+
it("strips a leading marker emoji and the following space", () => {
|
|
92
|
+
expect(stripLeadingEmoji("❓ Q7 — deployBlock guard?")).toBe("Q7 — deployBlock guard?");
|
|
93
|
+
expect(stripLeadingEmoji("➡️ Recommended: A — document-only.")).toBe(
|
|
94
|
+
"Recommended: A — document-only.",
|
|
95
|
+
);
|
|
96
|
+
expect(stripLeadingEmoji("⚠️ careful")).toBe("careful");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("strips a run of emoji, not just one", () => {
|
|
100
|
+
expect(stripLeadingEmoji("➡️ ➡️ twice")).toBe("twice");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("strips from each line independently", () => {
|
|
104
|
+
expect(stripLeadingEmoji("❓ Question?\n\n➡️ Recommended: A.")).toBe(
|
|
105
|
+
"Question?\n\nRecommended: A.",
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("leaves non-emoji prefixes alone", () => {
|
|
110
|
+
expect(stripLeadingEmoji("→ text")).toBe("→ text");
|
|
111
|
+
expect(stripLeadingEmoji("• bullet")).toBe("• bullet");
|
|
112
|
+
expect(stripLeadingEmoji("Q7 deployBlock")).toBe("Q7 deployBlock");
|
|
113
|
+
expect(stripLeadingEmoji("1. First")).toBe("1. First");
|
|
114
|
+
expect(stripLeadingEmoji("")).toBe("");
|
|
115
|
+
});
|
|
32
116
|
});
|
|
33
117
|
|
|
34
118
|
describe("charWidth", () => {
|
package/src/core/text.ts
CHANGED
|
@@ -3,8 +3,48 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The core never deals with ANSI codes or terminal themes, only plain strings
|
|
5
5
|
* and their visible widths. Styling is applied later by the fringe painter.
|
|
6
|
+
*
|
|
7
|
+
* Widths must match `@earendil-works/pi-tui`'s `visibleWidth` exactly: pi
|
|
8
|
+
* treats any rendered custom-UI line wider than the terminal as a hard crash
|
|
9
|
+
* ("Rendered line exceeds terminal width"), so under-measuring by a column
|
|
10
|
+
* kills the whole agent. Emoji are therefore measured per grapheme with the
|
|
11
|
+
* same RGI_Emoji rule pi-tui uses, not per code point.
|
|
6
12
|
*/
|
|
7
13
|
|
|
14
|
+
/** Grapheme segmenter shared by the width and emoji-strip helpers. */
|
|
15
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
16
|
+
|
|
17
|
+
// --- Emoji classification, mirrored from pi-tui's utils (the measure pi's
|
|
18
|
+
// own crash guard uses).
|
|
19
|
+
const rgiEmojiRegex = /^\p{RGI_Emoji}$/v;
|
|
20
|
+
const couldBeEmoji = (segment: string): boolean => {
|
|
21
|
+
const cp = segment.codePointAt(0) ?? 0;
|
|
22
|
+
return (
|
|
23
|
+
(cp >= 0x1f000 && cp <= 0x1fbff) || // Emoji and pictographs
|
|
24
|
+
(cp >= 0x2300 && cp <= 0x23ff) || // Misc technical
|
|
25
|
+
(cp >= 0x2600 && cp <= 0x27bf) || // Misc symbols, dingbats, arrows
|
|
26
|
+
(cp >= 0x2b50 && cp <= 0x2b55) || // Stars/circles
|
|
27
|
+
segment.includes("\uFE0F") || // Emoji presentation selector (VS16)
|
|
28
|
+
segment.length > 2 // Multi-codepoint sequences (ZWJ, skin tones, flags)
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Width in terminal columns of a single grapheme, matching pi-tui. */
|
|
33
|
+
const graphemeWidth = (segment: string): number => {
|
|
34
|
+
if (segment === "\t") return 3;
|
|
35
|
+
if (couldBeEmoji(segment) && rgiEmojiRegex.test(segment)) return 2;
|
|
36
|
+
const cp = segment.codePointAt(0) ?? 0;
|
|
37
|
+
// Regional indicators (flags) render full-width even when isolated.
|
|
38
|
+
if (cp >= 0x1f1e6 && cp <= 0x1f1ff) return 2;
|
|
39
|
+
let width = 0;
|
|
40
|
+
for (const ch of segment) width += charWidth(ch);
|
|
41
|
+
return width;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** True when `segment` is an emoji as pi-tui defines it (`➡️`, `⚠️`, `👨👩👧`). */
|
|
45
|
+
const isEmoji = (segment: string): boolean =>
|
|
46
|
+
couldBeEmoji(segment) && rgiEmojiRegex.test(segment);
|
|
47
|
+
|
|
8
48
|
/** Width in terminal columns of a single character (code point). */
|
|
9
49
|
export const charWidth = (ch: string): number => {
|
|
10
50
|
const cp = ch.codePointAt(0) ?? 0;
|
|
@@ -17,30 +57,75 @@ export const charWidth = (ch: string): number => {
|
|
|
17
57
|
return 1;
|
|
18
58
|
};
|
|
19
59
|
|
|
20
|
-
/** Visible width of a string in terminal columns. */
|
|
60
|
+
/** Visible width of a string in terminal columns (grapheme-aware). */
|
|
21
61
|
export const visibleWidth = (text: string): number => {
|
|
22
62
|
let width = 0;
|
|
23
|
-
for (const
|
|
24
|
-
width +=
|
|
63
|
+
for (const { segment } of graphemeSegmenter.segment(text)) {
|
|
64
|
+
width += graphemeWidth(segment);
|
|
25
65
|
}
|
|
26
66
|
return width;
|
|
27
67
|
};
|
|
28
68
|
|
|
29
|
-
/** Truncate `text` to at most `width` visible columns, cutting whole
|
|
69
|
+
/** Truncate `text` to at most `width` visible columns, cutting whole graphemes. */
|
|
30
70
|
export const truncateToWidth = (text: string, width: number): string => {
|
|
31
71
|
if (width <= 0) return "";
|
|
32
72
|
if (visibleWidth(text) <= width) return text;
|
|
33
73
|
let out = "";
|
|
34
74
|
let used = 0;
|
|
35
|
-
for (const
|
|
36
|
-
const w =
|
|
75
|
+
for (const { segment } of graphemeSegmenter.segment(text)) {
|
|
76
|
+
const w = graphemeWidth(segment);
|
|
37
77
|
if (used + w > width) break;
|
|
38
|
-
out +=
|
|
78
|
+
out += segment;
|
|
39
79
|
used += w;
|
|
40
80
|
}
|
|
41
81
|
return out;
|
|
42
82
|
};
|
|
43
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Strip a leading run of emoji from the start of each line of `text`.
|
|
86
|
+
*
|
|
87
|
+
* Models prefix prompts and option text with marker emoji (`❓ Q7 — …`,
|
|
88
|
+
* `➡️ Recommended: …`). The UI already renders selection affordances, so
|
|
89
|
+
* those markers are pure noise; a leading pointer such as `→ ` or `• ` is
|
|
90
|
+
* left alone. The whitespace after the stripped run is removed. Literal
|
|
91
|
+
* newlines are preserved (each line is stripped independently).
|
|
92
|
+
*/
|
|
93
|
+
export const stripLeadingEmoji = (text: string): string => {
|
|
94
|
+
if (text === "") return "";
|
|
95
|
+
return text
|
|
96
|
+
.split(/\r\n|\r|\n/)
|
|
97
|
+
.map((line) => {
|
|
98
|
+
const segments = [...graphemeSegmenter.segment(line)];
|
|
99
|
+
// Walk the leading run: whitespace is consumed along with the emoji it
|
|
100
|
+
// separates ("➡️ ➡️ text"), but the run stops at the first
|
|
101
|
+
// non-whitespace, non-emoji grapheme.
|
|
102
|
+
let sawEmoji = false;
|
|
103
|
+
let lastEmoji = -1;
|
|
104
|
+
let i = 0;
|
|
105
|
+
while (i < segments.length) {
|
|
106
|
+
const seg = segments[i]!.segment;
|
|
107
|
+
if (seg.trim() === "") {
|
|
108
|
+
i++;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (isEmoji(seg)) {
|
|
112
|
+
sawEmoji = true;
|
|
113
|
+
lastEmoji = i;
|
|
114
|
+
i++;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
if (!sawEmoji) return line;
|
|
120
|
+
return segments
|
|
121
|
+
.slice(lastEmoji + 1)
|
|
122
|
+
.map((s) => s.segment)
|
|
123
|
+
.join("")
|
|
124
|
+
.replace(/^\s+/, "");
|
|
125
|
+
})
|
|
126
|
+
.join("\n");
|
|
127
|
+
};
|
|
128
|
+
|
|
44
129
|
/**
|
|
45
130
|
* Wrap plain text to a maximum visible width per line.
|
|
46
131
|
*
|
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
|
|
|
@@ -31,9 +32,10 @@ const questionToolSystemPromptContribution = {
|
|
|
31
32
|
"Ask the user to decide: interactive questions with selectable options or a typed answer",
|
|
32
33
|
guidelines: [
|
|
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
|
-
"Ask all open questions in one
|
|
35
|
+
"Ask all open questions in one call. Put one question per entry 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",
|
|
36
|
-
"
|
|
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",
|
|
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.",
|
|
37
39
|
],
|
|
38
40
|
} as const;
|
|
39
41
|
|
|
@@ -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
|
|
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 is a selectable list (single question) or a tabbed form (multiple questions). It 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
|
},
|