@linxiraos/pi-tui 1.4.12 → 1.5.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 +31 -44
- package/dist/types/chat/messages.d.ts +2 -0
- package/dist/types/chat/spotlight.d.ts +8 -3
- package/dist/types/chat/transcript-render-helpers.d.ts +5 -0
- package/dist/types/components/editor.d.ts +4 -1
- package/dist/types/hotkeys-markdown.d.ts +2 -0
- package/dist/types/html-form/data.d.ts +56 -0
- package/dist/types/html-form/dialog.d.ts +51 -0
- package/dist/types/html-form/media.d.ts +28 -0
- package/dist/types/html-form/paginate.d.ts +43 -0
- package/dist/types/html-form/parse.d.ts +99 -0
- package/dist/types/overlays/ask-dialog.d.ts +11 -6
- package/dist/types/overlays/copy-targets.d.ts +8 -6
- package/dist/types/overlays/hook-editor.d.ts +3 -1
- package/dist/types/overlays/hub-frame.d.ts +2 -0
- package/dist/types/overlays/model-hub.d.ts +2 -0
- package/dist/types/overlays/team-hub.d.ts +150 -0
- package/dist/types/render/row-moves.d.ts +41 -0
- package/dist/types/status-line/component.d.ts +5 -0
- package/dist/types/status-line/host.d.ts +2 -2
- package/dist/types/status-line/schema.d.ts +1 -1
- package/dist/types/status-line/types.d.ts +8 -0
- package/dist/types/tools/ask.d.ts +11 -49
- package/dist/types/tools/autoresearch.d.ts +2 -2
- package/dist/types/tools/eval.d.ts +4 -0
- package/dist/types/tools/grouped-file-output.d.ts +2 -0
- package/dist/types/tools/shell-viz.d.ts +6 -2
- package/dist/types/tools/xd-url.d.ts +6 -0
- package/dist/types/windows-input-mode.d.ts +8 -0
- package/package.json +9 -9
- package/src/charts/table-data.ts +18 -5
- package/src/chat/chat-transcript-builder.ts +27 -10
- package/src/chat/image-loading.ts +55 -0
- package/src/chat/messages.d.ts +2 -0
- package/src/chat/messages.ts +3 -0
- package/src/chat/spotlight.d.ts +8 -3
- package/src/chat/spotlight.ts +23 -11
- package/src/chat/tool-execution.ts +5 -0
- package/src/chat/transcript-render-helpers.d.ts +5 -0
- package/src/chat/transcript-render-helpers.ts +13 -0
- package/src/chrome/transcript-container.ts +1 -1
- package/src/components/editor.d.ts +4 -1
- package/src/components/editor.ts +3 -2
- package/src/components/markdown.ts +70 -12
- package/src/hotkeys-markdown.ts +5 -1
- package/src/html-form/data.d.ts +56 -0
- package/src/html-form/data.ts +177 -0
- package/src/html-form/dialog.ts +1450 -0
- package/src/html-form/media.ts +105 -0
- package/src/html-form/paginate.ts +99 -0
- package/src/html-form/parse.d.ts +99 -0
- package/src/html-form/parse.ts +568 -0
- package/src/lang-from-path.ts +1 -0
- package/src/latex-block.ts +3 -3
- package/src/latex-to-unicode.ts +18 -5
- package/src/overlays/ask-dialog.ts +69 -52
- package/src/overlays/copy-targets.d.ts +8 -6
- package/src/overlays/copy-targets.ts +25 -46
- package/src/overlays/hook-editor.ts +16 -15
- package/src/overlays/hub-frame.ts +8 -1
- package/src/overlays/model-hub.ts +12 -2
- package/src/overlays/spotlight-sheet.ts +18 -18
- package/src/overlays/team-hub.ts +901 -0
- package/src/render/row-moves.d.ts +41 -0
- package/src/render/row-moves.ts +109 -0
- package/src/setup/wizard-overlay.ts +7 -3
- package/src/status-line/component.ts +19 -0
- package/src/status-line/footer.ts +2 -2
- package/src/status-line/host.ts +2 -2
- package/src/status-line/presets.ts +6 -6
- package/src/status-line/schema.ts +2 -1
- package/src/status-line/segments.ts +31 -4
- package/src/status-line/startup.ts +1 -1
- package/src/status-line/types.ts +8 -0
- package/src/stdin-buffer.ts +15 -1
- package/src/terminal-capabilities.ts +12 -3
- package/src/terminal.ts +41 -3
- package/src/tools/ask.d.ts +11 -49
- package/src/tools/ask.ts +201 -606
- package/src/tools/autoresearch.ts +2 -2
- package/src/tools/debug.ts +31 -38
- package/src/tools/eval.d.ts +4 -0
- package/src/tools/eval.ts +4 -0
- package/src/tools/grep.ts +38 -16
- package/src/tools/grouped-file-output.d.ts +2 -0
- package/src/tools/grouped-file-output.ts +5 -0
- package/src/tools/shell-viz-render.ts +37 -6
- package/src/tools/shell-viz.d.ts +6 -2
- package/src/tools/shell-viz.ts +19 -6
- package/src/tools/xd-url.d.ts +6 -0
- package/src/tools/xd-url.ts +19 -1
- package/src/tui.ts +129 -1
- package/src/windows-input-mode.d.ts +8 -0
- package/src/windows-input-mode.ts +13 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pictures an HTML form shows: `<img src>` loaded from a URL, a `data:` URI or
|
|
3
|
+
* a local file, and inline `<svg>` drawings, as bytes plus a MIME type —
|
|
4
|
+
* a raster (`image/png`, `image/jpeg`, `image/gif`, `image/webp`) or
|
|
5
|
+
* `image/svg+xml`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const media = await loadFormImage("docs/arch.png", { cwd, signal });
|
|
9
|
+
* media.mime; // "image/png"
|
|
10
|
+
*/
|
|
11
|
+
import * as os from "node:os";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
import { parseImageMetadata } from "@linxiraos/pi-utils";
|
|
14
|
+
|
|
15
|
+
/** An image's bytes and MIME type. */
|
|
16
|
+
export interface FormMedia {
|
|
17
|
+
bytes: Uint8Array;
|
|
18
|
+
mime: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Where relative paths resolve, and the abort for in-flight loads. */
|
|
22
|
+
export interface FormMediaOptions {
|
|
23
|
+
cwd: string;
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Largest picture a form loads. */
|
|
28
|
+
export const MAX_FORM_IMAGE_BYTES = 10 * 1024 * 1024;
|
|
29
|
+
/** Longest a remote picture may take to arrive. */
|
|
30
|
+
const FETCH_TIMEOUT_MS = 10_000;
|
|
31
|
+
const SVG_MIME = "image/svg+xml";
|
|
32
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Load an `<img src>`: an `http(s):` URL, a `data:` URI (base64 or URL-encoded),
|
|
36
|
+
* a `file:` URL, or a path (`~/`-relative, absolute, or relative to `cwd`).
|
|
37
|
+
*
|
|
38
|
+
* @throws {Error} the source cannot be read, exceeds {@link MAX_FORM_IMAGE_BYTES},
|
|
39
|
+
* times out, or is not a PNG, JPEG, GIF, WebP or SVG image.
|
|
40
|
+
*/
|
|
41
|
+
export async function loadFormImage(src: string, options: FormMediaOptions): Promise<FormMedia> {
|
|
42
|
+
const bytes = await readSource(src, options);
|
|
43
|
+
if (bytes.byteLength > MAX_FORM_IMAGE_BYTES) throw new Error(`larger than ${MAX_FORM_IMAGE_BYTES >> 20} MiB`);
|
|
44
|
+
const raster = parseImageMetadata(bytes.subarray(0, 64));
|
|
45
|
+
if (raster) return { bytes, mime: raster.mimeType };
|
|
46
|
+
const text = new TextDecoder().decode(bytes.subarray(0, 4096));
|
|
47
|
+
if (/<svg[\s>]/i.test(text)) return svgMedia(new TextDecoder().decode(bytes));
|
|
48
|
+
throw new Error("not a PNG, JPEG, GIF, WebP or SVG image");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An `<svg>` as standalone media: the SVG namespace added when missing, and a
|
|
53
|
+
* drawing sized only by its `viewBox` given that size as `width`/`height`, since
|
|
54
|
+
* renderers drop a drawing without one.
|
|
55
|
+
*/
|
|
56
|
+
export function svgMedia(markup: string): FormMedia {
|
|
57
|
+
const open = /<svg\b[^>]*>/i.exec(markup)?.[0];
|
|
58
|
+
if (!open) return { bytes: new TextEncoder().encode(markup), mime: SVG_MIME };
|
|
59
|
+
const added: string[] = [];
|
|
60
|
+
if (!/\bxmlns\s*=/i.test(open)) added.push(`xmlns="${SVG_NAMESPACE}"`);
|
|
61
|
+
const box = /\bviewBox\s*=\s*["']\s*[-\d.]+[\s,]+[-\d.]+[\s,]+([\d.]+)[\s,]+([\d.]+)\s*["']/i.exec(open);
|
|
62
|
+
if (box && !/\swidth\s*=/i.test(open) && !/\sheight\s*=/i.test(open)) {
|
|
63
|
+
added.push(`width="${box[1]}"`, `height="${box[2]}"`);
|
|
64
|
+
}
|
|
65
|
+
const sized = added.length > 0 ? markup.replace(/<svg\b/i, `<svg ${added.join(" ")}`) : markup;
|
|
66
|
+
return { bytes: new TextEncoder().encode(sized), mime: SVG_MIME };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether `media` is an SVG drawing rather than a raster. */
|
|
70
|
+
export function isSvgMedia(media: FormMedia): boolean {
|
|
71
|
+
return media.mime === SVG_MIME;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function readSource(src: string, options: FormMediaOptions): Promise<Uint8Array> {
|
|
75
|
+
if (/^data:/i.test(src)) return decodeDataUri(src);
|
|
76
|
+
if (/^https?:\/\//i.test(src)) {
|
|
77
|
+
const signal = options.signal
|
|
78
|
+
? AbortSignal.any([options.signal, AbortSignal.timeout(FETCH_TIMEOUT_MS)])
|
|
79
|
+
: AbortSignal.timeout(FETCH_TIMEOUT_MS);
|
|
80
|
+
const response = await fetch(src, { signal });
|
|
81
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
82
|
+
const declared = Number(response.headers.get("content-length") ?? 0);
|
|
83
|
+
if (declared > MAX_FORM_IMAGE_BYTES) throw new Error(`larger than ${MAX_FORM_IMAGE_BYTES >> 20} MiB`);
|
|
84
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
85
|
+
}
|
|
86
|
+
const file = /^file:/i.test(src) ? Bun.fileURLToPath(src) : resolvePath(src, options.cwd);
|
|
87
|
+
const handle = Bun.file(file);
|
|
88
|
+
if (handle.size > MAX_FORM_IMAGE_BYTES) throw new Error(`larger than ${MAX_FORM_IMAGE_BYTES >> 20} MiB`);
|
|
89
|
+
return new Uint8Array(await handle.arrayBuffer());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function resolvePath(src: string, cwd: string): string {
|
|
93
|
+
if (src === "~" || src.startsWith("~/")) return path.join(os.homedir(), src.slice(1));
|
|
94
|
+
return path.resolve(cwd, src);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The payload of a `data:` URI; base64 when marked so, else percent-encoded text. */
|
|
98
|
+
function decodeDataUri(uri: string): Uint8Array {
|
|
99
|
+
const comma = uri.indexOf(",");
|
|
100
|
+
if (comma === -1) throw new Error("malformed data: URI");
|
|
101
|
+
const header = uri.slice(5, comma);
|
|
102
|
+
const payload = uri.slice(comma + 1);
|
|
103
|
+
if (/;base64$/i.test(header)) return Uint8Array.fromBase64(payload.replace(/\s+/g, ""));
|
|
104
|
+
return new TextEncoder().encode(decodeURIComponent(payload));
|
|
105
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a rendered form body into pages of a fixed row budget.
|
|
3
|
+
*
|
|
4
|
+
* The body arrives as atoms: unbreakable row runs (an option with its wrapped
|
|
5
|
+
* label, a text field with its input, one line of a paragraph). Each atom ranks
|
|
6
|
+
* how good a page break before it is; a page ends at the break that best
|
|
7
|
+
* trades rank against the rows it leaves empty ({@link breakScore}), so a
|
|
8
|
+
* section boundary wins unless it strands most of the page, and a break
|
|
9
|
+
* never lands before a {@link BreakRank.Never} atom (a heading's first line,
|
|
10
|
+
* a block's widow) unless nothing else fits.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** How good a page break before an atom is. */
|
|
14
|
+
export const enum BreakRank {
|
|
15
|
+
/** Keep with the previous atom (the line after a heading, an error after its field). */
|
|
16
|
+
Never = 0,
|
|
17
|
+
/** Inside a block: between options of a group, lines of a paragraph. */
|
|
18
|
+
Inside = 1,
|
|
19
|
+
/** Between blocks of one section. */
|
|
20
|
+
Block = 2,
|
|
21
|
+
/** Between sections. */
|
|
22
|
+
Section = 3,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One unbreakable run of body rows. */
|
|
26
|
+
export interface PageAtom {
|
|
27
|
+
lines: readonly string[];
|
|
28
|
+
/** Blank rows before this atom; dropped when the atom starts a page. */
|
|
29
|
+
gap: number;
|
|
30
|
+
breakRank: BreakRank;
|
|
31
|
+
/** Title repeated atop a page that starts at this atom (`Database (cont.)`); costs one row. */
|
|
32
|
+
continuation?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Atoms `[start, end)` of one page. */
|
|
36
|
+
export interface Page {
|
|
37
|
+
start: number;
|
|
38
|
+
end: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Rows a page uses when it holds atoms `[start, end)`. */
|
|
42
|
+
export function pageHeight(atoms: readonly PageAtom[], start: number, end: number): number {
|
|
43
|
+
let used = atoms[start]?.continuation !== undefined ? 1 : 0;
|
|
44
|
+
for (let index = start; index < end; index++) {
|
|
45
|
+
const atom = atoms[index]!;
|
|
46
|
+
used += (index === start ? 0 : atom.gap) + atom.lines.length;
|
|
47
|
+
}
|
|
48
|
+
return used;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Pack atoms into pages of at most `rows` rows. An atom taller than a page
|
|
53
|
+
* gets a page of its own (the caller clips it). Always returns at least one page.
|
|
54
|
+
*/
|
|
55
|
+
export function paginate(atoms: readonly PageAtom[], rows: number): Page[] {
|
|
56
|
+
const budget = Math.max(1, rows);
|
|
57
|
+
const pages: Page[] = [];
|
|
58
|
+
let start = 0;
|
|
59
|
+
while (start < atoms.length) {
|
|
60
|
+
// Greedy fill: the first atom always fits, however tall.
|
|
61
|
+
let end = start + 1;
|
|
62
|
+
while (end < atoms.length && pageHeight(atoms, start, end + 1) <= budget) end++;
|
|
63
|
+
if (end < atoms.length) end = bestBreak(atoms, start, end, budget);
|
|
64
|
+
pages.push({ start, end });
|
|
65
|
+
start = end;
|
|
66
|
+
}
|
|
67
|
+
if (pages.length === 0) pages.push({ start: 0, end: 0 });
|
|
68
|
+
return pages;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Worth of ending a page with `used` of `budget` rows at a break of `rank`:
|
|
73
|
+
* the rank's bonus minus the fraction left empty. A section break beats a
|
|
74
|
+
* full page while it leaves under half the page empty; a block break, under a quarter.
|
|
75
|
+
*/
|
|
76
|
+
function breakScore(rank: BreakRank, used: number, budget: number): number {
|
|
77
|
+
const bonus = rank === BreakRank.Section ? 0.5 : rank === BreakRank.Block ? 0.25 : 0;
|
|
78
|
+
return bonus - (budget - used) / budget;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Pick the break in `(start, end]` for a page that greedily filled up to `end`:
|
|
83
|
+
* the allowed break with the best {@link breakScore} (latest on ties), else
|
|
84
|
+
* `end` itself when every candidate is {@link BreakRank.Never}.
|
|
85
|
+
*/
|
|
86
|
+
function bestBreak(atoms: readonly PageAtom[], start: number, end: number, budget: number): number {
|
|
87
|
+
let best = end;
|
|
88
|
+
let bestScore = Number.NEGATIVE_INFINITY;
|
|
89
|
+
for (let candidate = end; candidate > start; candidate--) {
|
|
90
|
+
const rank = atoms[candidate]!.breakRank;
|
|
91
|
+
if (rank === BreakRank.Never) continue;
|
|
92
|
+
const score = breakScore(rank, pageHeight(atoms, start, candidate), budget);
|
|
93
|
+
if (score > bestScore) {
|
|
94
|
+
best = candidate;
|
|
95
|
+
bestScore = score;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return best;
|
|
99
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Inline emphasis a span keeps from `<code>`, `<strong>`/`<b>` and `<em>`/`<i>`. */
|
|
2
|
+
export type FormSpanStyle = "code" | "strong" | "em";
|
|
3
|
+
/** A run of inline text; `"\n"` inside `text` is a hard line break (`<br>`). */
|
|
4
|
+
export interface FormSpan {
|
|
5
|
+
text: string;
|
|
6
|
+
style?: FormSpanStyle;
|
|
7
|
+
}
|
|
8
|
+
/** One answer of a {@link ChoiceField}. */
|
|
9
|
+
export interface ChoiceOption {
|
|
10
|
+
value: string;
|
|
11
|
+
label: FormSpan[];
|
|
12
|
+
checked: boolean;
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** A radio group (`multi: false`), checkbox group (`multi: true`) or `<select>` (`select: true`). */
|
|
16
|
+
export interface ChoiceField {
|
|
17
|
+
kind: "choice";
|
|
18
|
+
name: string;
|
|
19
|
+
multi: boolean;
|
|
20
|
+
/** From a `<select>`: a single select draws as a one-row dropdown instead of option rows. */
|
|
21
|
+
select: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The UI adds a free-text "Other" answer: set for radio and checkbox groups of
|
|
24
|
+
* two or more options that don't already offer one of their own.
|
|
25
|
+
*/
|
|
26
|
+
other: boolean;
|
|
27
|
+
/** The control's own label; empty for radio/checkbox groups, whose question is the surrounding text. */
|
|
28
|
+
label: FormSpan[];
|
|
29
|
+
options: ChoiceOption[];
|
|
30
|
+
required: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** A single-line `<input>` (`multiline: false`) or a `<textarea>`. */
|
|
33
|
+
export interface TextField {
|
|
34
|
+
kind: "text";
|
|
35
|
+
name: string;
|
|
36
|
+
/** The `<input type>` (`text`, `number`, `url`, …); `textarea` for textareas. */
|
|
37
|
+
type: string;
|
|
38
|
+
multiline: boolean;
|
|
39
|
+
label: FormSpan[];
|
|
40
|
+
value: string;
|
|
41
|
+
placeholder: string;
|
|
42
|
+
required: boolean;
|
|
43
|
+
min?: number;
|
|
44
|
+
max?: number;
|
|
45
|
+
pattern?: string;
|
|
46
|
+
minLength?: number;
|
|
47
|
+
maxLength?: number;
|
|
48
|
+
/** Visible rows a textarea asked for (`rows`), 1 for inputs. */
|
|
49
|
+
rows: number;
|
|
50
|
+
}
|
|
51
|
+
/** Any control the user answers. */
|
|
52
|
+
export type FormField = ChoiceField | TextField;
|
|
53
|
+
/** A renderable unit of a section, in document order. */
|
|
54
|
+
export type FormBlock = {
|
|
55
|
+
kind: "text";
|
|
56
|
+
spans: FormSpan[];
|
|
57
|
+
heading: boolean;
|
|
58
|
+
} | {
|
|
59
|
+
kind: "list";
|
|
60
|
+
items: FormSpan[][];
|
|
61
|
+
ordered: boolean;
|
|
62
|
+
} | {
|
|
63
|
+
kind: "pre";
|
|
64
|
+
text: string;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "table";
|
|
67
|
+
rows: FormSpan[][][];
|
|
68
|
+
headerRows: number;
|
|
69
|
+
}
|
|
70
|
+
/** An `<img>`: `src` is an http(s) or `data:` URL, a `file:` URL or a local path (see `media.ts`). */
|
|
71
|
+
| {
|
|
72
|
+
kind: "image";
|
|
73
|
+
src: string;
|
|
74
|
+
alt: string;
|
|
75
|
+
}
|
|
76
|
+
/** An inline `<svg>`, serialized whole. */
|
|
77
|
+
| {
|
|
78
|
+
kind: "svg";
|
|
79
|
+
markup: string;
|
|
80
|
+
alt: string;
|
|
81
|
+
} | {
|
|
82
|
+
kind: "field";
|
|
83
|
+
field: FormField;
|
|
84
|
+
};
|
|
85
|
+
/** A `<fieldset>` (titled by its `<legend>`) or loose content between fieldsets (untitled). */
|
|
86
|
+
export interface FormSection {
|
|
87
|
+
title: FormSpan[];
|
|
88
|
+
blocks: FormBlock[];
|
|
89
|
+
}
|
|
90
|
+
/** A parsed form: what to draw, the fields in document order, and `<input type="hidden">` pairs. */
|
|
91
|
+
export interface HtmlForm {
|
|
92
|
+
sections: FormSection[];
|
|
93
|
+
fields: FormField[];
|
|
94
|
+
hidden: [name: string, value: string][];
|
|
95
|
+
}
|
|
96
|
+
/** Parse agent-written form HTML; content outside the first `<form>` is ignored when one exists. */
|
|
97
|
+
export declare function parseHtmlForm(html: string): HtmlForm;
|
|
98
|
+
/** Plain text of spans, for labels that fall back to a single string. */
|
|
99
|
+
export declare function spansText(spans: readonly FormSpan[]): string;
|