@ftrdotdev/pi-inquiry 0.5.2 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftrdotdev/pi-inquiry",
3
- "version": "0.5.2",
3
+ "version": "0.7.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",
@@ -39,14 +39,14 @@
39
39
  "typecheck": "tsc --noEmit",
40
40
  "lint": "oxlint && oxlint --type-aware -A all -D typescript/unbound-method",
41
41
  "lint:fix": "oxlint --fix",
42
- "fmt": "oxfmt -c oxfmt.config.ts"
42
+ "fmt": "oxfmt"
43
43
  },
44
44
  "dependencies": {
45
- "@ftrdotdev/pi-tui": "^0.5.2",
45
+ "@ftrdotdev/pi-tui": "^0.7.0",
46
46
  "effect": "4.0.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@earendil-works/pi-ai": "1.0.0",
49
+ "@earendil-works/pi-ai": "1.1.0",
50
50
  "@effect/tsgo": "0.47.2",
51
51
  "@effect/vitest": "4.0.0",
52
52
  "@types/bun": "latest",
@@ -192,7 +192,9 @@ describe("normalizeQuestions", () => {
192
192
  );
193
193
  const q = questions[0]!;
194
194
  expect(q.label).toBe("Q7 deployBlock");
195
- expect(q.prompt).toBe("Q7 — deployBlock guard: should anything guard it?\n\nRecommended: A — document-only.");
195
+ expect(q.prompt).toBe(
196
+ "Q7 — deployBlock guard: should anything guard it?\n\nRecommended: A — document-only.",
197
+ );
196
198
  expect(q.options[0]?.label).toBe("Document-only");
197
199
  expect(q.options[0]?.description).toBe(
198
200
  "Recommended. Zero mechanics; the Slice-13 gate + docs cover operator responsibility.",
@@ -434,9 +434,17 @@ describe("buildScene: terminal-width crash regression", () => {
434
434
  );
435
435
  };
436
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; }
437
+ for (const { segment } of new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(
438
+ text,
439
+ )) {
440
+ if (segment === "\t") {
441
+ w += 3;
442
+ continue;
443
+ }
444
+ if (couldBeEmoji(segment) && rgiEmoji.test(segment)) {
445
+ w += 2;
446
+ continue;
447
+ }
440
448
  for (const ch of segment) {
441
449
  const cp = ch.codePointAt(0)!;
442
450
  if (cp === 0xfe0f || cp === 0x200d) w += 0;
@@ -470,9 +478,7 @@ describe("buildScene: terminal-width crash regression", () => {
470
478
  });
471
479
 
472
480
  it("emits no line wider than the terminal", () => {
473
- const questions = normalizeQuestions(
474
- decodeParams({ questions: [q7CrashPayload()] }),
475
- );
481
+ const questions = normalizeQuestions(decodeParams({ questions: [q7CrashPayload()] }));
476
482
  const scene = buildScene(initialMachineState(questions), 110);
477
483
  for (const l of plainLines(scene)) {
478
484
  expect(terminalColumns(l)).toBeLessThanOrEqual(110);
@@ -480,9 +486,7 @@ describe("buildScene: terminal-width crash regression", () => {
480
486
  });
481
487
 
482
488
  it("renders the prompt continuation and option description without the model's leading emoji", () => {
483
- const questions = normalizeQuestions(
484
- decodeParams({ questions: [q7CrashPayload()] }),
485
- );
489
+ const questions = normalizeQuestions(decodeParams({ questions: [q7CrashPayload()] }));
486
490
  const scene = buildScene(initialMachineState(questions), 110);
487
491
  const text = plainLines(scene).join("\n");
488
492
  expect(text).toContain("Recommended: A — document-only.");
package/src/core/scene.ts CHANGED
@@ -359,7 +359,11 @@ export const buildScene = (state: MachineState, width: number): Scene => {
359
359
  for (const content of lines) {
360
360
  const pad = contentWidth - visibleWidth(plain(content));
361
361
  const body =
362
- pad > 0 ? [...content, span(" ".repeat(pad))] : pad < 0 ? clampLine(content, contentWidth) : content;
362
+ pad > 0
363
+ ? [...content, span(" ".repeat(pad))]
364
+ : pad < 0
365
+ ? clampLine(content, contentWidth)
366
+ : content;
363
367
  framed.push([border(frame.railLeft), ...body, border(frame.railRight)]);
364
368
  }
365
369
  framed.push([border(frame.bottom.left), border(frame.bottom.fill), border(frame.bottom.right)]);
@@ -1,11 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import {
3
- charWidth,
4
- stripLeadingEmoji,
5
- truncateToWidth,
6
- visibleWidth,
7
- wrapText,
8
- } from "./text.ts";
2
+ import { charWidth, stripLeadingEmoji, truncateToWidth, visibleWidth, wrapText } from "./text.ts";
9
3
 
10
4
  describe("visibleWidth", () => {
11
5
  it("counts ASCII as one column per character", () => {
package/src/core/text.ts CHANGED
@@ -42,8 +42,7 @@ const graphemeWidth = (segment: string): number => {
42
42
  };
43
43
 
44
44
  /** True when `segment` is an emoji as pi-tui defines it (`➡️`, `⚠️`, `👨‍👩‍👧`). */
45
- const isEmoji = (segment: string): boolean =>
46
- couldBeEmoji(segment) && rgiEmojiRegex.test(segment);
45
+ const isEmoji = (segment: string): boolean => couldBeEmoji(segment) && rgiEmojiRegex.test(segment);
47
46
 
48
47
  /** Width in terminal columns of a single character (code point). */
49
48
  export const charWidth = (ch: string): number => {
@@ -22,6 +22,7 @@ const identityTheme = {
22
22
  fg: (_color: string, text: string): string => text,
23
23
  inverse: (text: string): string => text,
24
24
  bold: (text: string): string => text,
25
+ style: (text: string): string => text,
25
26
  } as unknown as Theme;
26
27
 
27
28
  const THEMED = [
@@ -22,12 +22,13 @@ export const paintScene = (scene: Scene, theme: Theme): string[] => {
22
22
  };
23
23
 
24
24
  const paintSpan = (span: Span, theme: Theme): string => {
25
+ // One `theme.style()` call per span: fg (or the cursor's inverse) and bold
26
+ // combine into a single SGR wrapper instead of nested fg(bold(text)).
25
27
  if (span.style === "cursor") {
26
- const inner = span.bold ? theme.bold(span.text) : span.text;
27
- return theme.inverse(inner);
28
+ return theme.style(span.text, { inverse: true, bold: span.bold });
28
29
  }
29
- let text = span.text;
30
- if (span.bold) text = theme.bold(text);
31
- if (span.style) return theme.fg(span.style, text);
32
- return text;
30
+ if (span.style !== undefined) {
31
+ return theme.style(span.text, { fg: span.style, bold: span.bold });
32
+ }
33
+ return span.bold ? theme.bold(span.text) : span.text;
33
34
  };
@@ -4,7 +4,13 @@ import { KeybindingsManager, TUI_KEYBINDINGS } from "@earendil-works/pi-tui";
4
4
  import { initialMachineState, step, type MachineState } from "../core/machine.ts";
5
5
  import { KeyEvent } from "../core/keyboard.ts";
6
6
  import { Option, Question } from "../core/domain.ts";
7
- import { codePointToUtf16, makeSceneRenderer, runQuestionUi, utf16ToCodePoint } from "./pi-ui.ts";
7
+ import {
8
+ codePointToUtf16,
9
+ makeSceneRenderer,
10
+ questionClipboardText,
11
+ runQuestionUi,
12
+ utf16ToCodePoint,
13
+ } from "./pi-ui.ts";
8
14
 
9
15
  describe("makeSceneRenderer cache", () => {
10
16
  const fixture = (): { state: MachineState; theme: Theme; paints: () => number } => {
@@ -19,6 +25,10 @@ describe("makeSceneRenderer cache", () => {
19
25
  return text;
20
26
  },
21
27
  bold: (text: string): string => text,
28
+ style: (text: string): string => {
29
+ count++;
30
+ return text;
31
+ },
22
32
  } as unknown as Theme;
23
33
  const state = initialMachineState([
24
34
  new Question({
@@ -185,3 +195,30 @@ describe("cursor unit conversion", () => {
185
195
  expect(utf16ToCodePoint("ab", -1)).toBe(0);
186
196
  });
187
197
  });
198
+
199
+ describe("questionClipboardText", () => {
200
+ const question = (allowOther: boolean): Question =>
201
+ new Question({
202
+ id: "q1",
203
+ label: "Q1",
204
+ prompt: "Pick one?",
205
+ options: [new Option({ label: "First" }), new Option({ label: "Second" })],
206
+ allowOther,
207
+ multiple: false,
208
+ });
209
+
210
+ it("lists the prompt and numbered options", () => {
211
+ const text = questionClipboardText(initialMachineState([question(false)]));
212
+ expect(text).toBe("Pick one?\n1. First\n2. Second");
213
+ });
214
+
215
+ it("appends the implicit other answer when allowOther is set", () => {
216
+ const text = questionClipboardText(initialMachineState([question(true)]));
217
+ expect(text.endsWith("3. Type something.")).toBe(true);
218
+ });
219
+
220
+ it("is empty on the submit tab", () => {
221
+ const state = { ...initialMachineState([question(false)]), currentTab: 1 };
222
+ expect(questionClipboardText(state)).toBe("");
223
+ });
224
+ });
package/src/sdk/pi-ui.ts CHANGED
@@ -15,7 +15,11 @@ import { existsSync } from "fs";
15
15
  import { homedir } from "os";
16
16
  import { delimiter, join } from "path";
17
17
  import { CombinedAutocompleteProvider } from "@earendil-works/pi-tui";
18
- import type { ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
18
+ import {
19
+ copyToClipboard,
20
+ type ExtensionUIContext,
21
+ type Theme,
22
+ } from "@earendil-works/pi-coding-agent";
19
23
  import { QuestionResult, type Question } from "../core/domain.ts";
20
24
  import { initialMachineState, type MachineState } from "../core/machine.ts";
21
25
  import { buildScene } from "../core/scene.ts";
@@ -25,6 +29,19 @@ import type { KeyEvent } from "../core/keyboard.ts";
25
29
  import { parseKey } from "./keys.ts";
26
30
  import { paintScene } from "./painter.ts";
27
31
 
32
+ /**
33
+ * Plain text of the current question for the clipboard: the prompt, the
34
+ * numbered options, and the implicit "Type something." entry. Empty on the
35
+ * submit tab, where no single question is active.
36
+ */
37
+ export const questionClipboardText = (state: MachineState): string => {
38
+ const question = state.questions[state.currentTab];
39
+ if (!question) return "";
40
+ const lines = [question.prompt, ...question.options.map((o, i) => `${i + 1}. ${o.label}`)];
41
+ if (question.allowOther) lines.push(`${question.options.length + 1}. Type something.`);
42
+ return lines.join("\n");
43
+ };
44
+
28
45
  export const runQuestionUi = (
29
46
  ui: ExtensionUIContext,
30
47
  questions: readonly Question[],
@@ -67,24 +84,54 @@ export const runQuestionUi = (
67
84
  // frame, but the scene only changes when the machine emits a new
68
85
  // snapshot or the terminal is resized (tui.md "Performance").
69
86
  const scene = makeSceneRenderer(() => cell.state, theme);
87
+
88
+ const feed = (data: string): void => {
89
+ // app.tools.expand (default ctrl+o): keep the transcript's tool-output
90
+ // expansion toggle working while the questionnaire owns input focus.
91
+ // The app only routes app actions through the editor, which is not
92
+ // focused here, so intercept the key ourselves. keybindings.matches()
93
+ // honors the user's keybindings.json (rebinds, "[]" disables).
94
+ if (keybindings.matches(data, "app.tools.expand")) {
95
+ ui.setToolsExpanded(!ui.getToolsExpanded());
96
+ return;
97
+ }
98
+ // ctrl+y copies the current question's text. The core has no binding
99
+ // for it, so it never reaches the keyboard queue.
100
+ if (data === "\x19") {
101
+ const text = questionClipboardText(cell.state);
102
+ if (text !== "") {
103
+ void copyToClipboard(text).then(
104
+ () => ui.notify("Copied question"),
105
+ (cause: unknown) =>
106
+ ui.notify(
107
+ `Copy failed: ${cause instanceof Error ? cause.message : String(cause)}`,
108
+ "error",
109
+ ),
110
+ );
111
+ }
112
+ return;
113
+ }
114
+ const key = parseKey(data);
115
+ if (key) Effect.runSync(Queue.offer(keys, key));
116
+ };
117
+
70
118
  return {
71
119
  render: (width) => scene.render(width),
72
120
  invalidate: () => {
73
121
  scene.invalidate();
74
122
  tui.requestRender();
75
123
  },
76
- handleInput: (data) => {
77
- // app.tools.expand (default ctrl+o): keep the transcript's tool-output
78
- // expansion toggle working while the questionnaire owns input focus.
79
- // The app only routes app actions through the editor, which is not
80
- // focused here, so intercept the key ourselves. keybindings.matches()
81
- // honors the user's keybindings.json (rebinds, "[]" disables).
82
- if (keybindings.matches(data, "app.tools.expand")) {
83
- ui.setToolsExpanded(!ui.getToolsExpanded());
84
- return;
85
- }
86
- const key = parseKey(data);
87
- if (key) Effect.runSync(Queue.offer(keys, key));
124
+ handleInput: feed,
125
+ // pi normalizes wheel events to logical lines (the renderer applies
126
+ // `WheelScrollAccelerator`), so the delta is the step count directly.
127
+ // Reuse the arrow-key path so wheel and keyboard share one cursor model.
128
+ handleMouse: (event) => {
129
+ if (event.type !== "wheel") return undefined;
130
+ const delta = event.wheelDelta ?? 0;
131
+ if (delta === 0) return { handled: false };
132
+ const arrow = delta > 0 ? "\x1b[B" : "\x1b[A";
133
+ for (let i = 0; i < Math.abs(delta); i++) feed(arrow);
134
+ return { handled: true };
88
135
  },
89
136
  dispose: () => {
90
137
  Effect.runSync(Fiber.interrupt(fiber));
@@ -138,7 +138,7 @@ export default function question(pi: ExtensionAPI) {
138
138
  if (qs.length === 1) {
139
139
  const q = qs[0];
140
140
  let text =
141
- theme.fg("toolTitle", theme.bold("question ")) +
141
+ theme.style("question ", { fg: "toolTitle", bold: true }) +
142
142
  theme.fg("muted", typeof q?.prompt === "string" ? q.prompt : "");
143
143
  const opts = Array.isArray(q?.options) ? q.options : [];
144
144
  if (opts.length > 0) {
@@ -150,7 +150,7 @@ export default function question(pi: ExtensionAPI) {
150
150
  }
151
151
  const labels = qs.map(questionLabel).join(", ");
152
152
  let text =
153
- theme.fg("toolTitle", theme.bold("question ")) +
153
+ theme.style("question ", { fg: "toolTitle", bold: true }) +
154
154
  theme.fg("muted", `${qs.length} questions`);
155
155
  if (labels) {
156
156
  text += theme.fg("dim", ` (${labels})`);