@ftrdotdev/pi-inquiry 0.3.0 → 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 +3 -3
- package/package.json +3 -3
- package/src/core/domain.test.ts +38 -0
- package/src/core/domain.ts +13 -8
- package/src/core/scene.test.ts +103 -0
- package/src/core/scene.ts +31 -2
- package/src/core/text.test.ts +85 -1
- package/src/core/text.ts +92 -7
- package/src/tool/question.ts +3 -3
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
|
|
65
|
+
`question` call. Put one question per entry in `questions[]`.
|
|
66
66
|
|
|
67
67
|
### Parameters
|
|
68
68
|
|
|
@@ -130,8 +130,8 @@ The tool groups a question's answers together in its output, so a
|
|
|
130
130
|
multi-select question reads as e.g. `Q1: user selected: 1. A, 2. B, user
|
|
131
131
|
wrote: X` rather than several detached rows.
|
|
132
132
|
|
|
133
|
-
Treat a cancelled result as
|
|
134
|
-
|
|
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.
|
|
135
135
|
|
|
136
136
|
## Pairing with the tracker
|
|
137
137
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ftrdotdev/pi-inquiry",
|
|
3
|
-
"version": "0.
|
|
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",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"fmt": "oxfmt -c oxfmt.config.ts"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ftrdotdev/pi-tui": "^0.
|
|
45
|
+
"@ftrdotdev/pi-tui": "^0.4.0",
|
|
46
46
|
"effect": "4.0.0-rc.108"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@earendil-works/pi-ai": "0.84.
|
|
49
|
+
"@earendil-works/pi-ai": "0.84.4",
|
|
50
50
|
"@effect/tsgo": "0.36.4",
|
|
51
51
|
"@effect/vitest": "4.0.0-rc.108",
|
|
52
52
|
"@types/bun": "latest",
|
package/src/core/domain.test.ts
CHANGED
|
@@ -168,4 +168,42 @@ describe("normalizeQuestions", () => {
|
|
|
168
168
|
expect(questions[0]?.options[0]?.label).toBe("a");
|
|
169
169
|
expect(questions[0]?.options[0]?.description).toBe("d");
|
|
170
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
|
+
});
|
|
171
209
|
});
|
package/src/core/domain.ts
CHANGED
|
@@ -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
|
|
@@ -137,20 +138,24 @@ export class QuestionResult extends Schema.Class<QuestionResult>(
|
|
|
137
138
|
* Normalize wire params into domain questions.
|
|
138
139
|
*
|
|
139
140
|
* Applies the defaults: `id` falls back to `q{n}`, `label` to `Q{n}`,
|
|
140
|
-
* `allowOther` defaults to `true`, `multiple` to `false`.
|
|
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.
|
|
141
145
|
*/
|
|
142
146
|
export const normalizeQuestions = (params: QuestionListParams): readonly Question[] =>
|
|
143
147
|
params.questions.map(
|
|
144
148
|
(q, i) =>
|
|
145
149
|
new Question({
|
|
146
150
|
id: q.id ?? `q${i + 1}`,
|
|
147
|
-
label: q.label ?? `Q${i + 1}
|
|
148
|
-
prompt: q.prompt,
|
|
149
|
-
options: q.options.map((o) =>
|
|
150
|
-
o.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
+
}),
|
|
154
159
|
allowOther: q.allowOther !== false,
|
|
155
160
|
multiple: q.multiple === true,
|
|
156
161
|
}),
|
package/src/core/scene.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { Option, Question, type Question as QuestionType } from "./domain.ts";
|
|
3
|
+
import { decodeParams, normalizeQuestions } from "./domain.ts";
|
|
3
4
|
import { KeyEvent } from "./keyboard.ts";
|
|
4
5
|
import {
|
|
5
6
|
initialMachineState,
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
buildEditorBox,
|
|
13
14
|
buildScene,
|
|
14
15
|
buildSuggestionPopup,
|
|
16
|
+
clampLine,
|
|
15
17
|
line,
|
|
16
18
|
plain,
|
|
17
19
|
span,
|
|
@@ -411,6 +413,107 @@ describe("buildEditorBox", () => {
|
|
|
411
413
|
});
|
|
412
414
|
});
|
|
413
415
|
|
|
416
|
+
describe("buildScene: terminal-width crash regression", () => {
|
|
417
|
+
// Independent pi-tui-style measure (the module under test must never emit a
|
|
418
|
+
// line pi's crash guard would reject: "Rendered line exceeds terminal
|
|
419
|
+
// width"). The 2026-08-31 incident: the model prefixed a prompt
|
|
420
|
+
// continuation with ➡️; the old per-code-point measure counted it as one
|
|
421
|
+
// column, pi counts two, and the framed line came out 111 on a 110-column
|
|
422
|
+
// terminal, killing the agent.
|
|
423
|
+
const terminalColumns = (text: string): number => {
|
|
424
|
+
const rgiEmoji = /^\p{RGI_Emoji}$/v;
|
|
425
|
+
const couldBeEmoji = (s: string): boolean => {
|
|
426
|
+
const cp = s.codePointAt(0) ?? 0;
|
|
427
|
+
return (
|
|
428
|
+
(cp >= 0x1f000 && cp <= 0x1fbff) ||
|
|
429
|
+
(cp >= 0x2300 && cp <= 0x23ff) ||
|
|
430
|
+
(cp >= 0x2600 && cp <= 0x27bf) ||
|
|
431
|
+
(cp >= 0x2b50 && cp <= 0x2b55) ||
|
|
432
|
+
s.includes("\uFE0F") ||
|
|
433
|
+
s.length > 2
|
|
434
|
+
);
|
|
435
|
+
};
|
|
436
|
+
let w = 0;
|
|
437
|
+
for (const { segment } of new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(text)) {
|
|
438
|
+
if (segment === "\t") { w += 3; continue; }
|
|
439
|
+
if (couldBeEmoji(segment) && rgiEmoji.test(segment)) { w += 2; continue; }
|
|
440
|
+
for (const ch of segment) {
|
|
441
|
+
const cp = ch.codePointAt(0)!;
|
|
442
|
+
if (cp === 0xfe0f || cp === 0x200d) w += 0;
|
|
443
|
+
else if (cp >= 0x2e80) w += 2;
|
|
444
|
+
else w += 1;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return w;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// The Q7 payload from the crashing session, minus the noise the strip
|
|
451
|
+
// removes. Width 110 matches the crashed terminal.
|
|
452
|
+
const q7CrashPayload = () => ({
|
|
453
|
+
id: "q7",
|
|
454
|
+
label: "Q7 deployBlock",
|
|
455
|
+
prompt:
|
|
456
|
+
"❓ Q7 — deployBlock guard: under net-deltas semantics, deployBlock set AFTER contract creation is legitimate (read model = deltas since deploy). A deployBlock in the future is indistinguishable from a token not yet deployed — both idle until head passes. So there is no misconfig that fails loudly today; a wrong deployBlock is a silent, visible-only-in-output mistake. Should anything guard it?\n\n➡️ Recommended: A — document-only. Zero mechanics, and (b) is a 10-line add if you want the operator signal.",
|
|
457
|
+
options: [
|
|
458
|
+
{
|
|
459
|
+
label: "Document-only",
|
|
460
|
+
description:
|
|
461
|
+
"➡️ Recommended. Zero mechanics; the Slice-13 gate + docs cover operator responsibility.",
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
label: "One-shot warning log",
|
|
465
|
+
description:
|
|
466
|
+
"Engine logs a one-shot warning on first tick if getCode(deployBlock-1) is non-empty ('history starts mid-token-life'). One RPC per token, no boot-time network.",
|
|
467
|
+
},
|
|
468
|
+
{ label: "Fail boot" },
|
|
469
|
+
],
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it("emits no line wider than the terminal", () => {
|
|
473
|
+
const questions = normalizeQuestions(
|
|
474
|
+
decodeParams({ questions: [q7CrashPayload()] }),
|
|
475
|
+
);
|
|
476
|
+
const scene = buildScene(initialMachineState(questions), 110);
|
|
477
|
+
for (const l of plainLines(scene)) {
|
|
478
|
+
expect(terminalColumns(l)).toBeLessThanOrEqual(110);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("renders the prompt continuation and option description without the model's leading emoji", () => {
|
|
483
|
+
const questions = normalizeQuestions(
|
|
484
|
+
decodeParams({ questions: [q7CrashPayload()] }),
|
|
485
|
+
);
|
|
486
|
+
const scene = buildScene(initialMachineState(questions), 110);
|
|
487
|
+
const text = plainLines(scene).join("\n");
|
|
488
|
+
expect(text).toContain("Recommended: A — document-only.");
|
|
489
|
+
expect(text).toContain("Recommended. Zero mechanics; the Slice-13 gate");
|
|
490
|
+
expect(text).not.toContain("➡️");
|
|
491
|
+
expect(text).toContain("Q7 — deployBlock guard:");
|
|
492
|
+
expect(text).not.toContain("❓");
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
describe("clampLine", () => {
|
|
497
|
+
it("is a no-op when the line fits", () => {
|
|
498
|
+
const l: SceneLine = [span("abc")];
|
|
499
|
+
expect(clampLine(l, 5)).toEqual(l);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("truncates the last kept span at the width", () => {
|
|
503
|
+
expect(plain(clampLine([span("abcdef")], 3))).toBe("abc");
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("truncates the span that overflows", () => {
|
|
507
|
+
const out = clampLine([span("ab"), span("cd", "muted")], 3);
|
|
508
|
+
expect(plain(out)).toBe("abc");
|
|
509
|
+
expect(out[1]).toEqual({ text: "c", style: "muted" });
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("clips on grapheme boundaries", () => {
|
|
513
|
+
expect(plain(clampLine([span("➡️ x")], 2))).toBe("➡️");
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
|
|
414
517
|
describe("wrapLine", () => {
|
|
415
518
|
it("preserves spans across line breaks", () => {
|
|
416
519
|
const wrapped = wrapLine(line(span("one", "accent"), span(" two three", "muted")), 7);
|
package/src/core/scene.ts
CHANGED
|
@@ -348,20 +348,49 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
348
348
|
|
|
349
349
|
// Frame the interior: accent border with rails, body padded to the content
|
|
350
350
|
// width so the rails stay flush (the same layout the BorderedBox component
|
|
351
|
-
// produces around ANSI children).
|
|
351
|
+
// produces around ANSI children). Body rows are pre-wrapped, so a row that
|
|
352
|
+
// still exceeds the content width indicates a measurement mismatch; clamp it
|
|
353
|
+
// rather than letting it push the rail past the terminal (pi hard-crashes on
|
|
354
|
+
// any custom-UI line wider than the terminal).
|
|
352
355
|
const border = (text: string): Span => span(text, "accent");
|
|
353
356
|
const framed: SceneLine[] = [
|
|
354
357
|
[border(frame.top.left), border(frame.top.fill), border(frame.top.right)],
|
|
355
358
|
];
|
|
356
359
|
for (const content of lines) {
|
|
357
360
|
const pad = contentWidth - visibleWidth(plain(content));
|
|
358
|
-
const body =
|
|
361
|
+
const body =
|
|
362
|
+
pad > 0 ? [...content, span(" ".repeat(pad))] : pad < 0 ? clampLine(content, contentWidth) : content;
|
|
359
363
|
framed.push([border(frame.railLeft), ...body, border(frame.railRight)]);
|
|
360
364
|
}
|
|
361
365
|
framed.push([border(frame.bottom.left), border(frame.bottom.fill), border(frame.bottom.right)]);
|
|
362
366
|
return { lines: framed };
|
|
363
367
|
};
|
|
364
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Clip a scene line to at most `width` visible columns, dropping whole spans
|
|
371
|
+
* and truncating the last kept span (the same clipping BorderedBox applies to
|
|
372
|
+
* body rows via truncateToWidth). Defensive only: scene content is
|
|
373
|
+
* pre-wrapped, so this should never truncate once widths are accurate.
|
|
374
|
+
*/
|
|
375
|
+
export const clampLine = (content: SceneLine, width: number): SceneLine => {
|
|
376
|
+
const out: SceneLine = [];
|
|
377
|
+
let used = 0;
|
|
378
|
+
for (const s of content) {
|
|
379
|
+
const w = visibleWidth(s.text);
|
|
380
|
+
const room = width - used;
|
|
381
|
+
if (room <= 0) break;
|
|
382
|
+
if (w <= room) {
|
|
383
|
+
out.push(s);
|
|
384
|
+
used += w;
|
|
385
|
+
} else {
|
|
386
|
+
const kept = truncateToWidth(s.text, room);
|
|
387
|
+
if (kept !== "" || out.length === 0) out.push({ ...s, text: kept });
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return out;
|
|
392
|
+
};
|
|
393
|
+
|
|
365
394
|
/**
|
|
366
395
|
* Build the inline editor box lines (without the leading gutter space).
|
|
367
396
|
*/
|
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/tool/question.ts
CHANGED
|
@@ -32,10 +32,10 @@ const questionToolSystemPromptContribution = {
|
|
|
32
32
|
"Ask the user to decide: interactive questions with selectable options or a typed answer",
|
|
33
33
|
guidelines: [
|
|
34
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",
|
|
35
|
-
"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.",
|
|
36
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
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
|
|
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;
|
|
41
41
|
|
|
@@ -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 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).",
|
|
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,
|