@lune_99/pi-ask-question 0.0.0-stage → 0.1.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/CHANGELOG.md +66 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/docs/question-answer-row.png +0 -0
- package/docs/question-answer-row.svg +16 -0
- package/docs/question-dock.png +0 -0
- package/docs/question-dock.svg +8 -0
- package/docs/question-panel.png +0 -0
- package/docs/question-panel.svg +58 -0
- package/package.json +62 -5
- package/src/commands/question.ts +25 -0
- package/src/core/question-delivery.ts +111 -0
- package/src/core/question-format.ts +109 -0
- package/src/core/question-notification.ts +34 -0
- package/src/core/question-types.ts +91 -0
- package/src/core/questionManager.ts +484 -0
- package/src/index.ts +142 -0
- package/src/tools/ask-user-questions.ts +184 -0
- package/src/tui/panel/question-panel-controller.ts +261 -0
- package/src/tui/panel/question-panel-renderer.ts +813 -0
- package/src/tui/panel/question-panel.ts +702 -0
- package/src/tui/preview/preview-box.ts +168 -0
- package/src/tui/preview/preview-layout.ts +116 -0
- package/src/tui/preview/preview-renderer.ts +129 -0
- package/src/tui/question-answer-message.ts +42 -0
- package/src/tui/question-dock-contribution.ts +41 -0
- package/src/tui/question-dock.ts +68 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure box helpers for the option preview pane.
|
|
3
|
+
*
|
|
4
|
+
* Every width here is terminal columns (`visibleWidth`), never code units: CJK content is
|
|
5
|
+
* twice as wide as its `length`, and ANSI sequences are zero columns with non-zero length.
|
|
6
|
+
* Measuring the wrong one misaligns the box and, in a side-by-side layout, the whole panel.
|
|
7
|
+
*/
|
|
8
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
9
|
+
|
|
10
|
+
const ANSI_SGR_RE = /\x1b\[[0-9;]*m/g;
|
|
11
|
+
const ANSI_OSC8_RE = /\x1b\]8;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
12
|
+
const FENCE_MARKER_RE = /^`{3}/;
|
|
13
|
+
|
|
14
|
+
/** Top and bottom border rows of a rendered box. */
|
|
15
|
+
export const BORDER_VERTICAL_OVERHEAD = 2;
|
|
16
|
+
/** Left and right border columns of a rendered box. */
|
|
17
|
+
export const BORDER_HORIZONTAL_OVERHEAD = 2;
|
|
18
|
+
/** Inner horizontal padding between a border column and the content. */
|
|
19
|
+
export const BORDER_INNER_PADDING_HORIZONTAL = 1;
|
|
20
|
+
/** Floor for the content width, so a one-word preview still reads as a panel. */
|
|
21
|
+
export const MIN_BOX_INNER_WIDTH = 40;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Drop the fence lines pi-tui renders around a code block, leaving the highlighted body.
|
|
25
|
+
* Inline code is unaffected: markdown renders it without backticks in the first place.
|
|
26
|
+
*/
|
|
27
|
+
export function stripFenceMarkers(lines: readonly string[]): string[] {
|
|
28
|
+
return lines.filter((line) => {
|
|
29
|
+
const withoutAnsi = line.replace(ANSI_SGR_RE, "").replace(ANSI_OSC8_RE, "");
|
|
30
|
+
|
|
31
|
+
return !FENCE_MARKER_RE.test(withoutAnsi);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BorderedBoxOptions {
|
|
36
|
+
/** Styles the border characters. */
|
|
37
|
+
colorFn: (text: string) => string;
|
|
38
|
+
/** Content rows below the box; replaces the bottom border with a notice. */
|
|
39
|
+
hidden?: number;
|
|
40
|
+
/** Content rows scrolled out above the box; reported in the same notice. */
|
|
41
|
+
hiddenAbove?: number;
|
|
42
|
+
/** Drawn into the top border, so the pane names the option it belongs to. */
|
|
43
|
+
title?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Drop blank lines at the start and end of a rendered block.
|
|
48
|
+
*
|
|
49
|
+
* Markdown keeps the blank lines its source carries, so a preview ending with one would push
|
|
50
|
+
* the bottom border down and leave a gap inside the box. Blank lines in the middle are content
|
|
51
|
+
* (paragraph spacing) and stay.
|
|
52
|
+
*/
|
|
53
|
+
export function trimBlankEdges(lines: readonly string[]): string[] {
|
|
54
|
+
let start = 0;
|
|
55
|
+
let end = lines.length;
|
|
56
|
+
|
|
57
|
+
while (start < end && isBlank(lines[start]!)) {
|
|
58
|
+
start++;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
while (end > start && isBlank(lines[end - 1]!)) {
|
|
62
|
+
end--;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return lines.slice(start, end);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isBlank(line: string): boolean {
|
|
69
|
+
return line.replace(ANSI_SGR_RE, "").replace(ANSI_OSC8_RE, "").trim().length === 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Outer width of a box for `lines`, never wider than `maxInnerWidth` (+ border and padding).
|
|
74
|
+
*
|
|
75
|
+
* Trailing whitespace is ignored because pi-tui renders every markdown line padded to the
|
|
76
|
+
* render width; measuring that padding would stretch the box to its whole column. A title
|
|
77
|
+
* needs its own room inside the top border: one dash, the title, a space and one more dash.
|
|
78
|
+
*/
|
|
79
|
+
export function computeBoxWidth(lines: readonly string[], maxInnerWidth: number, title?: string): number {
|
|
80
|
+
const available = Math.max(1, maxInnerWidth);
|
|
81
|
+
let widest = Math.min(MIN_BOX_INNER_WIDTH, available);
|
|
82
|
+
|
|
83
|
+
for (const line of lines) {
|
|
84
|
+
const width = visibleWidth(line.replace(/\s+$/, ""));
|
|
85
|
+
|
|
86
|
+
if (width > widest) {
|
|
87
|
+
widest = width;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (title !== undefined && title.length > 0) {
|
|
92
|
+
widest = Math.max(widest, visibleWidth(title) + 4);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return Math.min(widest, available) + BORDER_HORIZONTAL_OVERHEAD + 2 * BORDER_INNER_PADDING_HORIZONTAL;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Draw `lines` inside a bordered box exactly `width` columns wide.
|
|
100
|
+
*
|
|
101
|
+
* `hidden` replaces the bottom border with a notice instead of adding a row, so the block
|
|
102
|
+
* height stays predictable for the panel's window.
|
|
103
|
+
*/
|
|
104
|
+
export function renderBorderedBox(
|
|
105
|
+
lines: readonly string[],
|
|
106
|
+
width: number,
|
|
107
|
+
options: BorderedBoxOptions,
|
|
108
|
+
): string[] {
|
|
109
|
+
const dashSpan = Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD);
|
|
110
|
+
const contentInner = Math.max(1, dashSpan - 2 * BORDER_INNER_PADDING_HORIZONTAL);
|
|
111
|
+
const padding = " ".repeat(BORDER_INNER_PADDING_HORIZONTAL);
|
|
112
|
+
const boxed: string[] = [options.colorFn(topBorder(dashSpan, options.title))];
|
|
113
|
+
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
// Markdown pads its lines to the render width; that padding is not content, and
|
|
116
|
+
// keeping it would mark every line as truncated.
|
|
117
|
+
const content = truncateToWidth(line.replace(/\s+$/, ""), contentInner, "…", true);
|
|
118
|
+
|
|
119
|
+
boxed.push(`${options.colorFn("│")}${padding}${content}${padding}${options.colorFn("│")}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const hidden = options.hidden ?? 0;
|
|
123
|
+
const hiddenAbove = options.hiddenAbove ?? 0;
|
|
124
|
+
|
|
125
|
+
boxed.push(options.colorFn(
|
|
126
|
+
hidden > 0 || hiddenAbove > 0
|
|
127
|
+
? hiddenNotice(hiddenAbove, hidden, dashSpan)
|
|
128
|
+
: `└${"─".repeat(dashSpan)}┘`,
|
|
129
|
+
));
|
|
130
|
+
|
|
131
|
+
return boxed;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Top border, with the title worked into the dashes when there is room for it. */
|
|
135
|
+
function topBorder(dashSpan: number, title: string | undefined): string {
|
|
136
|
+
const plain = `┌${"─".repeat(dashSpan)}┐`;
|
|
137
|
+
|
|
138
|
+
if (title === undefined || title.length === 0) {
|
|
139
|
+
return plain;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// "─ ", the title, a trailing space and at least one dash have to fit the border.
|
|
143
|
+
const budget = dashSpan - 4;
|
|
144
|
+
|
|
145
|
+
if (budget < 1) {
|
|
146
|
+
return plain;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const head = `─ ${truncateToWidth(title, budget, "…")} `;
|
|
150
|
+
|
|
151
|
+
return `┌${head}${"─".repeat(Math.max(1, dashSpan - visibleWidth(head)))}┐`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Bottom border with the hidden-line notice, kept exactly `dashSpan` columns wide. */
|
|
155
|
+
function hiddenNotice(above: number, below: number, dashSpan: number): string {
|
|
156
|
+
const text = above > 0
|
|
157
|
+
? `${[`↑ ${above}`, below > 0 ? `↓ ${below}` : undefined].filter(Boolean).join(" · ")} hidden`
|
|
158
|
+
: `${below} line${below === 1 ? "" : "s"} hidden`;
|
|
159
|
+
const notice = truncateToWidth(
|
|
160
|
+
` ✂ ── ${text} ── `,
|
|
161
|
+
dashSpan,
|
|
162
|
+
"…",
|
|
163
|
+
);
|
|
164
|
+
const fill = Math.max(0, dashSpan - visibleWidth(notice));
|
|
165
|
+
const left = Math.floor(fill / 2);
|
|
166
|
+
|
|
167
|
+
return `└${"─".repeat(left)}${notice}${"─".repeat(fill - left)}┘`;
|
|
168
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure layout decisions for the option preview column: whether a question has previews at
|
|
3
|
+
* all, how wide the option column has to be, and how the content width is split.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is a function of data the caller passes in — no session, no manager, no
|
|
6
|
+
* per-render state — so the panel always renders the layout it just measured.
|
|
7
|
+
*/
|
|
8
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
9
|
+
|
|
10
|
+
import type { AskQuestion, AskQuestionOption } from "../../core/question-types.ts";
|
|
11
|
+
import { BORDER_VERTICAL_OVERHEAD } from "./preview-box.ts";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Columns between the option list and the preview box: two blank columns plus one inset so
|
|
15
|
+
* the box border is not glued to the labels.
|
|
16
|
+
*/
|
|
17
|
+
export const PREVIEW_COLUMN_GAP = 3;
|
|
18
|
+
/** Minimum width of the preview box itself; below this the layout stacks. */
|
|
19
|
+
export const MIN_PREVIEW_WIDTH = 45;
|
|
20
|
+
/** Floor for the option column: it fits the "Type something" row and a short label. */
|
|
21
|
+
export const MIN_LEFT_WIDTH = 30;
|
|
22
|
+
/** The option column never takes more than this share of the content width. */
|
|
23
|
+
export const MAX_LEFT_RATIO = 0.5;
|
|
24
|
+
/** Preview block height cap when the columns sit side by side. */
|
|
25
|
+
export const PREVIEW_MAX_BLOCK_ROWS_SIDE_BY_SIDE = 14;
|
|
26
|
+
/** Preview block height cap when the preview sits under the options. */
|
|
27
|
+
export const PREVIEW_MAX_BLOCK_ROWS_STACKED = 10;
|
|
28
|
+
/** Blank rows between the option list and a stacked preview block. */
|
|
29
|
+
export const STACKED_GAP_ROWS = 1;
|
|
30
|
+
/** Smallest block worth drawing: two borders and one content row. */
|
|
31
|
+
export const MIN_BLOCK_ROWS = BORDER_VERTICAL_OVERHEAD + 1;
|
|
32
|
+
|
|
33
|
+
export type PreviewLayoutMode = "side-by-side" | "stacked";
|
|
34
|
+
|
|
35
|
+
/** Whitespace-only previews would render as an empty box, so they count as absent. */
|
|
36
|
+
export function optionHasPreview(option: AskQuestionOption): boolean {
|
|
37
|
+
return typeof option.preview === "string" && option.preview.trim().length > 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function questionHasPreview(question: AskQuestion): boolean {
|
|
41
|
+
return (question.options ?? []).some((option) => optionHasPreview(option));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Width of an option row as the panel draws it: cursor column, selection marker, numbering
|
|
46
|
+
* and label. Measuring the label alone would under-reserve every row that carries a marker.
|
|
47
|
+
*/
|
|
48
|
+
function optionRowWidth(question: AskQuestion, option: AskQuestionOption): number {
|
|
49
|
+
const markerWidth = question.multiSelect === true ? 3 : 1; // "[x]" vs "●"
|
|
50
|
+
const numberingWidth = String(Math.max(1, question.options?.length ?? 0)).length + 2; // "1. "
|
|
51
|
+
|
|
52
|
+
return 2 + markerWidth + 1 + numberingWidth + visibleWidth(option.label);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Width of the option column for the whole request.
|
|
57
|
+
*
|
|
58
|
+
* It stays the same for every question of the request — the panel switches tabs without the
|
|
59
|
+
* columns moving — so the widest row of the widest question wins.
|
|
60
|
+
*/
|
|
61
|
+
export function leftColumnWidth(questions: readonly AskQuestion[], contentWidth: number): number {
|
|
62
|
+
let desired = 0;
|
|
63
|
+
|
|
64
|
+
for (const question of questions) {
|
|
65
|
+
for (const option of question.options ?? []) {
|
|
66
|
+
desired = Math.max(desired, optionRowWidth(question, option));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const ceiling = Math.max(
|
|
71
|
+
1,
|
|
72
|
+
Math.min(
|
|
73
|
+
Math.floor(contentWidth * MAX_LEFT_RATIO),
|
|
74
|
+
contentWidth - PREVIEW_COLUMN_GAP - MIN_PREVIEW_WIDTH,
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return Math.max(MIN_LEFT_WIDTH, Math.min(desired, ceiling));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Side by side only when the measured option column, the gap and a usable preview box fit:
|
|
83
|
+
* a fixed threshold would either stack usable widths or squeeze the box into unusable ones.
|
|
84
|
+
*/
|
|
85
|
+
export function decidePreviewLayout(contentWidth: number, leftWidth: number): PreviewLayoutMode {
|
|
86
|
+
return contentWidth >= leftWidth + PREVIEW_COLUMN_GAP + MIN_PREVIEW_WIDTH ? "side-by-side" : "stacked";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function previewColumnWidths(
|
|
90
|
+
contentWidth: number,
|
|
91
|
+
leftWidth: number,
|
|
92
|
+
mode: PreviewLayoutMode,
|
|
93
|
+
): { leftWidth: number; rightWidth: number; gap: number } {
|
|
94
|
+
if (mode === "stacked") {
|
|
95
|
+
return { leftWidth: contentWidth, rightWidth: contentWidth, gap: 0 };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const gap = PREVIEW_COLUMN_GAP;
|
|
99
|
+
const fittedLeft = Math.min(leftWidth, Math.max(1, contentWidth - gap - 1));
|
|
100
|
+
|
|
101
|
+
return { leftWidth: fittedLeft, rightWidth: Math.max(1, contentWidth - fittedLeft - gap), gap };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Row budget for the preview block: the rows the caller can spare, capped per layout.
|
|
106
|
+
*
|
|
107
|
+
* The caller subtracts what already occupies the body (header, option rows, gap); a block
|
|
108
|
+
* taller than that would be clipped by the panel window and lose its bottom border.
|
|
109
|
+
*/
|
|
110
|
+
export function previewBlockRowBudget(mode: PreviewLayoutMode, availableRows: number): number {
|
|
111
|
+
const cap = mode === "side-by-side"
|
|
112
|
+
? PREVIEW_MAX_BLOCK_ROWS_SIDE_BY_SIDE
|
|
113
|
+
: PREVIEW_MAX_BLOCK_ROWS_STACKED;
|
|
114
|
+
|
|
115
|
+
return Math.max(MIN_BLOCK_ROWS, Math.min(cap, availableRows));
|
|
116
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders one question's option preview as a bordered markdown block.
|
|
3
|
+
*
|
|
4
|
+
* One instance per question. `optionIndex` only identifies an option inside its own
|
|
5
|
+
* question, so a renderer reused across questions would hand out the wrong preview; keeping
|
|
6
|
+
* it per question is what makes the index key safe.
|
|
7
|
+
*
|
|
8
|
+
* Width handling is left to pi-tui's `Markdown`, which caches its rendered lines per width
|
|
9
|
+
* (and its parsed tokens per text), so there is no width-keyed cache to maintain here.
|
|
10
|
+
*
|
|
11
|
+
* The renderer holds no interaction state: focus, selection and answers belong to the panel
|
|
12
|
+
* and the question manager, and it never reads the manager itself. Its only state is the
|
|
13
|
+
* per-option `Markdown` cache, which is a render concern; the caller decides which option is
|
|
14
|
+
* focused and how much room the block gets.
|
|
15
|
+
*/
|
|
16
|
+
import { getMarkdownTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import { Markdown, type MarkdownTheme } from "@earendil-works/pi-tui";
|
|
18
|
+
|
|
19
|
+
import type { AskQuestion } from "../../core/question-types.ts";
|
|
20
|
+
import {
|
|
21
|
+
BORDER_HORIZONTAL_OVERHEAD,
|
|
22
|
+
BORDER_INNER_PADDING_HORIZONTAL,
|
|
23
|
+
BORDER_VERTICAL_OVERHEAD,
|
|
24
|
+
computeBoxWidth,
|
|
25
|
+
renderBorderedBox,
|
|
26
|
+
stripFenceMarkers,
|
|
27
|
+
trimBlankEdges,
|
|
28
|
+
} from "./preview-box.ts";
|
|
29
|
+
import { optionHasPreview, questionHasPreview } from "./preview-layout.ts";
|
|
30
|
+
|
|
31
|
+
/** Shown for a focused option without a preview, keeping the box in place. */
|
|
32
|
+
export const NO_PREVIEW_TEXT = "No preview available";
|
|
33
|
+
|
|
34
|
+
export interface QuestionPreviewRendererOptions {
|
|
35
|
+
question: AskQuestion;
|
|
36
|
+
theme: Theme;
|
|
37
|
+
/** Defaults to the shared markdown theme the rest of the TUI uses. */
|
|
38
|
+
markdownTheme?: MarkdownTheme;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PreviewBlock {
|
|
42
|
+
/** Box lines, borders included; the caller pads them into its preview column. */
|
|
43
|
+
lines: string[];
|
|
44
|
+
/** Content rows the block could not show, above and below; reported in its bottom border. */
|
|
45
|
+
hidden: number;
|
|
46
|
+
/** First content row shown, after clamping the requested scroll. */
|
|
47
|
+
offset: number;
|
|
48
|
+
/** Largest scroll offset that still fills the block. */
|
|
49
|
+
maxOffset: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class QuestionPreviewRenderer {
|
|
53
|
+
private readonly question: AskQuestion;
|
|
54
|
+
private readonly theme: Theme;
|
|
55
|
+
private readonly markdownTheme: MarkdownTheme;
|
|
56
|
+
private readonly markdownByOption = new Map<number, Markdown>();
|
|
57
|
+
private readonly hasPreview: boolean;
|
|
58
|
+
|
|
59
|
+
constructor(options: QuestionPreviewRendererOptions) {
|
|
60
|
+
this.question = options.question;
|
|
61
|
+
this.theme = options.theme;
|
|
62
|
+
this.markdownTheme = options.markdownTheme ?? getMarkdownTheme();
|
|
63
|
+
this.hasPreview = questionHasPreview(options.question);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** False when no option of this question carries a preview: the panel then skips the column. */
|
|
67
|
+
hasAnyPreview(): boolean {
|
|
68
|
+
return this.hasPreview;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
render(optionIndex: number, width: number, maxBlockRows: number, scrollOffset = 0): PreviewBlock {
|
|
72
|
+
const maxInnerWidth = Math.max(
|
|
73
|
+
1,
|
|
74
|
+
width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL,
|
|
75
|
+
);
|
|
76
|
+
const contentBudget = Math.max(1, maxBlockRows - BORDER_VERTICAL_OVERHEAD);
|
|
77
|
+
const title = this.question.options?.[optionIndex]?.label;
|
|
78
|
+
const raw = this.contentLines(optionIndex, maxInnerWidth);
|
|
79
|
+
const maxOffset = Math.max(0, raw.length - contentBudget);
|
|
80
|
+
const offset = Math.min(maxOffset, Math.max(0, scrollOffset));
|
|
81
|
+
const visible = raw.slice(offset, offset + contentBudget);
|
|
82
|
+
const hiddenBelow = raw.length - offset - visible.length;
|
|
83
|
+
// Measured on the whole preview so the box does not change width while it scrolls.
|
|
84
|
+
const boxWidth = computeBoxWidth(raw, maxInnerWidth, title);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
lines: renderBorderedBox(visible, boxWidth, {
|
|
88
|
+
colorFn: (text) => this.theme.fg("border", text),
|
|
89
|
+
hidden: hiddenBelow,
|
|
90
|
+
hiddenAbove: offset,
|
|
91
|
+
title,
|
|
92
|
+
}),
|
|
93
|
+
hidden: offset + hiddenBelow,
|
|
94
|
+
offset,
|
|
95
|
+
maxOffset,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private contentLines(optionIndex: number, innerWidth: number): string[] {
|
|
100
|
+
const option = this.question.options?.[optionIndex];
|
|
101
|
+
|
|
102
|
+
if (option && optionHasPreview(option) && option.preview !== undefined) {
|
|
103
|
+
const rendered = trimBlankEdges(
|
|
104
|
+
stripFenceMarkers(this.markdownFor(optionIndex, option.preview).render(innerWidth)),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// A preview of nothing but fence markers or blank lines renders to nothing; fall
|
|
108
|
+
// back to the placeholder so the box never collapses into an empty frame.
|
|
109
|
+
if (rendered.length > 0) {
|
|
110
|
+
return rendered;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return [this.theme.fg("dim", NO_PREVIEW_TEXT)];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private markdownFor(optionIndex: number, text: string): Markdown {
|
|
118
|
+
const cached = this.markdownByOption.get(optionIndex);
|
|
119
|
+
|
|
120
|
+
if (cached) {
|
|
121
|
+
return cached;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const markdown = new Markdown(text, 0, 0, this.markdownTheme);
|
|
125
|
+
this.markdownByOption.set(optionIndex, markdown);
|
|
126
|
+
|
|
127
|
+
return markdown;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
|
+
|
|
5
|
+
import { formatAnswer, questionHeaderPrefix, summarizeText } from "../core/question-format.ts";
|
|
6
|
+
import type { AnswerMessageQuestion, AskQuestionAnswer } from "../core/questionManager.ts";
|
|
7
|
+
|
|
8
|
+
export interface AskQuestionAnswerMessageDetails {
|
|
9
|
+
requestId?: string;
|
|
10
|
+
status?: "answered" | "skipped";
|
|
11
|
+
questions?: AnswerMessageQuestion[];
|
|
12
|
+
answers?: AskQuestionAnswer[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Transcript row for the follow-up message that carries answers or a skip. */
|
|
16
|
+
export function renderQuestionAnswerMessage(
|
|
17
|
+
details: AskQuestionAnswerMessageDetails | undefined,
|
|
18
|
+
theme: Theme,
|
|
19
|
+
): Text {
|
|
20
|
+
const questions = details?.questions ?? [];
|
|
21
|
+
const skipped = details?.status === "skipped";
|
|
22
|
+
|
|
23
|
+
const lines: string[] = [
|
|
24
|
+
theme.bold(skipped
|
|
25
|
+
? theme.fg("warning", "Questions skipped")
|
|
26
|
+
: theme.fg("success", "Questions answered")),
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
for (const [index, question] of questions.entries()) {
|
|
30
|
+
lines.push(
|
|
31
|
+
`${theme.fg("muted", questionHeaderPrefix(question))}${theme.fg("text", summarizeText(question.question, 60))}`,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const answer = formatAnswer(question, details?.answers?.[index]);
|
|
35
|
+
|
|
36
|
+
if (answer !== undefined) {
|
|
37
|
+
lines.push(` ${theme.fg("accent", answer)}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
42
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { stripTerminalSequences, truncateToWidth, type Component } from "@earendil-works/pi-tui";
|
|
3
|
+
import { createDockContribution, type LuneDockSnapshot } from "@lune_99/pi-lune-extensions-core/dock";
|
|
4
|
+
import { openPendingQuestions } from "../commands/question.ts";
|
|
5
|
+
import { questionManager } from "../core/questionManager.ts";
|
|
6
|
+
import { questionDock } from "./question-dock.ts";
|
|
7
|
+
|
|
8
|
+
export const QUESTION_CONTRIBUTION_ID = "ask-question";
|
|
9
|
+
|
|
10
|
+
export function getQuestionDockSnapshot(ctx: ExtensionContext): LuneDockSnapshot {
|
|
11
|
+
const count = questionManager.getPendingRequests().length;
|
|
12
|
+
const next = questionManager.nextPendingRequest();
|
|
13
|
+
const title = stripTerminalSequences(
|
|
14
|
+
next?.questions[0]?.header ?? next?.questions[0]?.question ?? "",
|
|
15
|
+
).replace(/[\r\n\t]+/g, " ");
|
|
16
|
+
const summary =
|
|
17
|
+
count === 0 ? "idle" : `${count} pending ${count === 1 ? "request" : "requests"}`;
|
|
18
|
+
const component = (suffix: string): Component => ({
|
|
19
|
+
render: (width) => [
|
|
20
|
+
truncateToWidth(
|
|
21
|
+
`${ctx.ui.theme.fg(count > 0 ? "warning" : "dim", count > 0 ? "●" : "○")} Ask${suffix ? ctx.ui.theme.fg("muted", ` · ${suffix}`) : ""}`,
|
|
22
|
+
width,
|
|
23
|
+
),
|
|
24
|
+
],
|
|
25
|
+
invalidate() {},
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
base: component(""),
|
|
29
|
+
detail: component(summary),
|
|
30
|
+
full: component([summary, title].filter(Boolean).join(" · ")),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createQuestionDockContribution(persist: (ctx: ExtensionContext) => void) {
|
|
35
|
+
return createDockContribution({
|
|
36
|
+
id: QUESTION_CONTRIBUTION_ID,
|
|
37
|
+
getSnapshot: getQuestionDockSnapshot,
|
|
38
|
+
activate: (ctx) => openPendingQuestions(ctx, persist),
|
|
39
|
+
standalone: questionDock,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { summarizeText } from "../core/question-format.ts";
|
|
4
|
+
import { questionManager } from "../core/questionManager.ts";
|
|
5
|
+
|
|
6
|
+
const WIDGET_ID = "pi-ask-question";
|
|
7
|
+
|
|
8
|
+
/** Budget for the question preview before the hint; keeps the hint visible on narrow terminals. */
|
|
9
|
+
const SUMMARY_MAX_WIDTH = 40;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Persistent hint below the editor that a question is waiting.
|
|
13
|
+
*
|
|
14
|
+
* The panel itself is transient: Esc closes it, so without this row a closed panel
|
|
15
|
+
* would be invisible until the user remembered `/question`.
|
|
16
|
+
*/
|
|
17
|
+
export class QuestionDock {
|
|
18
|
+
private ctx: ExtensionContext | undefined;
|
|
19
|
+
|
|
20
|
+
setCtx(ctx: ExtensionContext | undefined): void {
|
|
21
|
+
this.ctx = ctx;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
render(): void {
|
|
25
|
+
if (!this.ctx || !this.ctx.hasUI) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const pending = questionManager.getPendingRequests();
|
|
30
|
+
const next = questionManager.nextPendingRequest();
|
|
31
|
+
|
|
32
|
+
if (pending.length === 0) {
|
|
33
|
+
this.ctx.ui.setWidget(WIDGET_ID, undefined);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const theme = this.ctx.ui.theme;
|
|
38
|
+
const count = `${pending.length} pending ${pending.length === 1 ? "question" : "questions"}`;
|
|
39
|
+
// The question the panel would show next, so the hint and `/question` agree.
|
|
40
|
+
const summary = summarizeText(next?.questions[0]?.question ?? "", SUMMARY_MAX_WIDTH);
|
|
41
|
+
|
|
42
|
+
this.ctx.ui.setWidget(
|
|
43
|
+
WIDGET_ID,
|
|
44
|
+
[
|
|
45
|
+
[
|
|
46
|
+
theme.fg("accent", count),
|
|
47
|
+
summary.length > 0 ? theme.fg("muted", summary) : undefined,
|
|
48
|
+
theme.fg("dim", "/question to answer"),
|
|
49
|
+
]
|
|
50
|
+
.filter((part): part is string => part !== undefined)
|
|
51
|
+
.join(theme.fg("dim", " · ")),
|
|
52
|
+
],
|
|
53
|
+
{
|
|
54
|
+
placement: "belowEditor",
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
clear(): void {
|
|
60
|
+
if (!this.ctx || !this.ctx.hasUI) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.ctx.ui.setWidget(WIDGET_ID, undefined);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const questionDock = new QuestionDock();
|