@juicesharp/rpiv-ask-user-question 1.9.0 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,6 +23,7 @@ Let the model ask you structured clarifying questions instead of guessing. `rpiv
23
23
  - **Multi-select questions** - checkboxes with `Space` to toggle, Enter-as-toggle on rows, a `Next` sentinel to advance, and toggles persisted across tab switches.
24
24
  - **Submit tab** - review every answer before submitting; warns about unanswered questions and offers a Submit picker.
25
25
  - **Chat row on every tab** - redirect the conversation without leaving the dialog.
26
+ - **Terminal-row-aware overflow scroll** - when the dialog is taller than the terminal, the body scrolls between a sticky heading and sticky hints/border; overflow indicators (↑ / ↓ / ↕) show what's clipped.
26
27
  - **"Other" free-text fallback** - type a custom answer when no option fits.
27
28
  - **Localized UI** - sentinel rows, hints, submit/cancel labels, review pane, and notes affordance display in the user's chosen language via `@juicesharp/rpiv-i18n`. Ships Deutsch / English / Español / Français / Português (PT) / Português (BR) / Русский / Українська; switch with `/languages` or `pi --locale <code>`. LLM-facing copy (tool description, schemas, errors) stays English by design.
28
29
 
package/index.ts CHANGED
@@ -10,54 +10,29 @@
10
10
  * at every call site. The extension stays online either way.
11
11
  *
12
12
  * Adding a locale: drop `locales/<code>.json` next to en.json (mirroring the
13
- * key set), then add the load + entry to the `registerStrings` call below.
14
- * See `@juicesharp/rpiv-i18n` README → "Contributing translations" for the
15
- * full convention.
13
+ * key set). No edit needed here — `registerLocalesFromDir` iterates
14
+ * `SUPPORTED_LOCALES` from the SDK. See `@juicesharp/rpiv-i18n` README →
15
+ * "Contributing translations" for the full convention.
16
16
  */
17
17
 
18
- import { readFileSync } from "node:fs";
19
- import { fileURLToPath } from "node:url";
20
18
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
21
19
  import { registerAskUserQuestionTool } from "./ask-user-question.js";
22
20
  import { I18N_NAMESPACE } from "./state/i18n-bridge.js";
23
21
 
24
- type TranslationMap = Readonly<Record<string, string>>;
25
- type I18nSDK = { registerStrings: (namespace: string, byLocale: Record<string, TranslationMap>) => void };
26
-
27
- function loadLocale(code: string): TranslationMap {
28
- // A missing or malformed locale file degrades gracefully: registerStrings
29
- // records an empty map for the locale, so render-time `tr(key, fallback)`
30
- // returns the canonical English literal at the call site. Crashing here
31
- // would take the entire ask_user_question tool offline at module init —
32
- // publish-manifest miss would brick the extension.
33
- try {
34
- return JSON.parse(
35
- readFileSync(fileURLToPath(new URL(`./locales/${code}.json`, import.meta.url)), "utf-8"),
36
- ) as TranslationMap;
37
- } catch (err) {
38
- console.warn(
39
- `rpiv-ask-user-question: failed to load locales/${code}.json — falling back to English (${(err as Error).message})`,
40
- );
41
- return {};
42
- }
43
- }
22
+ type I18nLoader = {
23
+ registerLocalesFromDir: (namespace: string, packageUrl: string, options?: { label?: string }) => void;
24
+ };
44
25
 
45
26
  // Dynamic import keeps `@juicesharp/rpiv-i18n` a soft optional peer: when the
46
27
  // SDK is installed alongside this package the strings register and
47
28
  // `/languages` flips them live; when it isn't, the import rejects here, we
48
29
  // no-op, and the bridge's English-fallback shim keeps the extension online.
30
+ //
31
+ // The `/loader` subpath is used instead of the SDK entry so the i18n-ui +
32
+ // pi-tui modules are not pulled into our load graph just to register strings.
49
33
  try {
50
- const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
51
- sdk.registerStrings(I18N_NAMESPACE, {
52
- de: loadLocale("de"),
53
- en: loadLocale("en"),
54
- es: loadLocale("es"),
55
- fr: loadLocale("fr"),
56
- pt: loadLocale("pt"),
57
- "pt-BR": loadLocale("pt-BR"),
58
- ru: loadLocale("ru"),
59
- uk: loadLocale("uk"),
60
- });
34
+ const sdk = (await import("@juicesharp/rpiv-i18n/loader")) as I18nLoader;
35
+ sdk.registerLocalesFromDir(I18N_NAMESPACE, import.meta.url, { label: "rpiv-ask-user-question" });
61
36
  } catch {
62
37
  // SDK absent — extension still loads with English-only UI.
63
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Pi extension. A structured questionnaire the model can put to you when it would otherwise guess, with typed options instead of free-form replies.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -82,6 +82,6 @@
82
82
  }
83
83
  },
84
84
  "dependencies": {
85
- "@juicesharp/rpiv-config": "^1.9.0"
85
+ "@juicesharp/rpiv-config": "^1.9.2"
86
86
  }
87
87
  }
@@ -11,6 +11,7 @@ export interface BindingContext {
11
11
  readonly totalQuestions: number;
12
12
  readonly activeView: ActiveView;
13
13
  readonly inputBuffer: string;
14
+ readonly inputCursorOffset: number | undefined;
14
15
  readonly activePreviewPane: StatefulView<PreviewPaneProps>;
15
16
  }
16
17
 
@@ -34,6 +34,7 @@ export const selectOptionListProps: PerTabSelector<OptionListViewProps> = (state
34
34
  selectedIndex: state.optionIndex,
35
35
  focused,
36
36
  inputBuffer: ctx.inputBuffer,
37
+ inputCursorOffset: ctx.inputCursorOffset,
37
38
  ...(confirmed ? { confirmed } : {}),
38
39
  };
39
40
  };
@@ -23,6 +23,7 @@ export interface OptionListViewProps {
23
23
  selectedIndex: number;
24
24
  focused: boolean;
25
25
  inputBuffer: string;
26
+ inputCursorOffset?: number;
26
27
  /** Optional previously-confirmed indicator. Omit when no marker should be drawn. */
27
28
  confirmed?: { index: number; labelOverride?: string };
28
29
  }
@@ -51,6 +52,7 @@ export class OptionListView implements StatefulView<OptionListViewProps> {
51
52
  this.select.setFocused(props.focused);
52
53
  this.select.setConfirmedIndex(props.confirmed?.index, props.confirmed?.labelOverride);
53
54
  this.select.setInputBuffer(props.inputBuffer);
55
+ this.select.setInputCursorOffset(props.inputCursorOffset);
54
56
  }
55
57
 
56
58
  handleInput(_data: string): void {}
@@ -1,5 +1,11 @@
1
1
  import type { Component } from "@earendil-works/pi-tui";
2
- import { visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
2
+ import { CURSOR_MARKER, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
3
+
4
+ // Grapheme-aware extraction at the cursor: pi-tui's Input advances `cursor` by
5
+ // grapheme-cluster code-unit length, so the cursor can land between code units of
6
+ // one cluster (emoji, ZWJ, combining marks). Single-code-unit slicing would split
7
+ // the cluster across the SGR 7/27 boundary.
8
+ const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
3
9
 
4
10
  /**
5
11
  * Row-intent discriminated union. `kind` is the single discriminator —
@@ -47,7 +53,6 @@ export class WrappingSelect implements Component {
47
53
  private static readonly ACTIVE_POINTER = "❯ ";
48
54
  private static readonly INACTIVE_POINTER = " ";
49
55
  private static readonly NUMBER_SEPARATOR = ". ";
50
- private static readonly INPUT_CURSOR = "▌";
51
56
  private static readonly CONFIRMED_MARK = " ✔";
52
57
  private static readonly MIN_CONTENT_WIDTH = 1;
53
58
 
@@ -60,6 +65,7 @@ export class WrappingSelect implements Component {
60
65
  private selectedIndex = 0;
61
66
  private focused = true;
62
67
  private inputBuffer = "";
68
+ private inputCursorOffset: number | undefined = undefined;
63
69
  /**
64
70
  * Index of the row that was previously confirmed for this list (e.g. the user's prior
65
71
  * answer when re-entering a multi-question tab). Renders `<label> ✔` in the active-row
@@ -125,6 +131,11 @@ export class WrappingSelect implements Component {
125
131
  this.inputBuffer = text;
126
132
  }
127
133
 
134
+ /** Set the cursor offset for the inline input row. `undefined` → end-of-buffer fallback. */
135
+ setInputCursorOffset(offset: number | undefined): void {
136
+ this.inputCursorOffset = offset;
137
+ }
138
+
128
139
  /** Intentionally empty — input is routed at the container level. */
129
140
  handleInput(_data: string): void {}
130
141
 
@@ -248,14 +259,43 @@ export class WrappingSelect implements Component {
248
259
  * so long input doesn't run off the right edge or trip the parent renderer's
249
260
  * width invariant. Mirrors `renderLabelBlock`'s contract: first line carries
250
261
  * `rowPrefix`, continuation lines carry `continuationPrefix` (spaces), and every
251
- * emitted line passes through `theme.selectedText`. The trailing cursor glyph
252
- * `▌` is appended to the buffer pre-wrap so it lands at the visual end of the
253
- * input — `Input` only exposes `getValue()` (cursor offset is private), so
254
- * cursor-mid-string is intentionally not rendered here; that would require
255
- * either an `Input.getCursorOffset()` API or delegating to `Input.render`.
262
+ * emitted line passes through `theme.selectedText`.
263
+ *
264
+ * Cursor visualization follows the standard TUI input-widget pattern (ECMA-48
265
+ * SGR 7 reverse-video on the cell *at* the cursor, not an inserted glyph) —
266
+ * same approach used by pi-tui Input.render (input.js:411-418), ink-text-input,
267
+ * terkelg/prompts, ratatui's user-input example, etc. Split: `before | at | after`
268
+ * where `at` is the single character under the cursor (or U+00A0 NBSP at end-of-
269
+ * buffer) wrapped in `\x1b[7m…\x1b[27m`. No characters shift; the column under
270
+ * the cursor inverts.
271
+ *
272
+ * pi-tui's zero-width `CURSOR_MARKER` (APC sentinel) is emitted immediately
273
+ * before the `at` cell so the TUI framebuffer can position the hardware
274
+ * terminal cursor at that column (visible iff pi's `showHardwareCursor`
275
+ * setting is on — `pi-coding-agent/main.js:303`). `visibleWidth` strips the
276
+ * marker as zero-width (utils.js:187-203), so wrap math is preserved.
277
+ *
278
+ * NBSP at end-of-buffer (rather than literal space): `wrapTextWithAnsi`
279
+ * tokenizes on whitespace, so a literal space inside the reverse-video escape
280
+ * pair would cause `wrapTextWithAnsi` to break the line at the cursor. NBSP
281
+ * preserves the visible width-1 contribution without registering as a wrap
282
+ * break — the conventional workaround documented across ANSI-aware string
283
+ * wrappers.
256
284
  */
257
285
  private renderInlineInputRow(rowPrefix: string, continuationPrefix: string, contentWidth: number): string[] {
258
- const raw = `${this.inputBuffer}${WrappingSelect.INPUT_CURSOR}`;
286
+ const buffer = this.inputBuffer;
287
+ const requested = this.inputCursorOffset;
288
+ const offset =
289
+ requested !== undefined && requested >= 0 && requested <= buffer.length ? requested : buffer.length;
290
+ const before = buffer.slice(0, offset);
291
+ const [firstGrapheme] = graphemeSegmenter.segment(buffer.slice(offset));
292
+ const rawAt = firstGrapheme ? firstGrapheme.segment : "";
293
+ // Whitespace at cursor (including end-of-buffer fallback) tokenizes as a wrap
294
+ // break inside `wrapTextWithAnsi`, splitting the line at the cursor. Substitute
295
+ // U+00A0 (NBSP): visually identical to a space, wrap-safe.
296
+ const atCursor = rawAt === "" || rawAt === " " ? " " : rawAt;
297
+ const after = buffer.slice(offset + rawAt.length);
298
+ const raw = `${before}${CURSOR_MARKER}\x1b[7m${atCursor}\x1b[27m${after}`;
259
299
  const wrapped = wrapTextWithAnsi(raw, contentWidth);
260
300
  return wrapped.map((segment, index) => {
261
301
  const prefix = index === 0 ? rowPrefix : continuationPrefix;
@@ -13,6 +13,20 @@ interface Invalidatable {
13
13
  invalidate(): void;
14
14
  }
15
15
 
16
+ /**
17
+ * Reads pi-tui Input's private `cursor` field via type escape with full runtime validation.
18
+ * Returns `undefined` on any failure → graceful degradation to end-of-buffer cursor.
19
+ * Follow-up: replace with Input.getCursorOffset() when pi-tui exposes a public API.
20
+ */
21
+ function getInputCursorOffset(input: Input): number | undefined {
22
+ const raw = (input as unknown as { cursor?: unknown }).cursor;
23
+ if (typeof raw !== "number") return undefined;
24
+ if (!Number.isSafeInteger(raw)) return undefined;
25
+ const value = input.getValue();
26
+ if (raw < 0 || raw > value.length) return undefined;
27
+ return raw;
28
+ }
29
+
16
30
  export interface QuestionnairePropsAdapterConfig {
17
31
  tui: { requestRender(): void };
18
32
  questions: readonly QuestionData[];
@@ -71,6 +85,7 @@ export class QuestionnairePropsAdapter {
71
85
  totalQuestions,
72
86
  activeView,
73
87
  inputBuffer: this.inlineInput.getValue(),
88
+ inputCursorOffset: getInputCursorOffset(this.inlineInput),
74
89
  activePreviewPane,
75
90
  };
76
91