pi-image-preview 0.1.5 → 0.2.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 +23 -9
- package/index.ts +28 -45
- package/package.json +12 -2
- package/src/extension-runtime.ts +47 -40
- package/src/image-gallery.ts +24 -189
- package/src/image-paths.ts +46 -0
- package/src/image-protocol.ts +206 -0
- package/src/inline-images.ts +253 -0
- package/src/kitty-placeholder.ts +160 -0
- package/src/preview-resize.ts +47 -0
- package/src/submission-resize.ts +55 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { execFile, execFileSync } from "node:child_process";
|
|
2
|
+
import { getCapabilities, type ImageProtocol } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decides whether kitty images can be drawn, working around pi-tui ≥0.67.6,
|
|
6
|
+
* which reports `images: null` inside tmux/screen for safety. Unicode
|
|
7
|
+
* placeholders are made for tmux, so inside tmux we ask tmux which terminals
|
|
8
|
+
* are viewing this pane's session instead of trusting environment variables.
|
|
9
|
+
*
|
|
10
|
+
* Environment variables are not enough: tmux copies KITTY_WINDOW_ID into its
|
|
11
|
+
* global environment, so a pane opened from an SSH client (for example Termux
|
|
12
|
+
* on a phone) still claims to be kitty. Placeholders are pane text, so every
|
|
13
|
+
* client sees them: draw images only when every viewing client speaks kitty.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export type TmuxClient = { termType: string; termName: string };
|
|
17
|
+
|
|
18
|
+
const KITTY_TERM_TYPE = /^(kitty|ghostty|wezterm|rex)\b/i;
|
|
19
|
+
const KITTY_TERM_NAMES = new Set(["xterm-kitty", "xterm-ghostty", "wezterm"]);
|
|
20
|
+
|
|
21
|
+
export function clientSupportsKitty(client: TmuxClient): boolean {
|
|
22
|
+
return (
|
|
23
|
+
KITTY_TERM_TYPE.test(client.termType) ||
|
|
24
|
+
KITTY_TERM_NAMES.has(client.termName.toLowerCase())
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const CLIENT_FORMAT =
|
|
29
|
+
"#{session_id}\t#{client_tty}\t#{client_termtype}\t#{client_termname}";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parses the output of one probe: the first line is this pane's session and
|
|
33
|
+
* whether its window is active, the rest are every client on the server.
|
|
34
|
+
* Keeps the clients viewing this pane's session. A header without the window
|
|
35
|
+
* field counts as visible.
|
|
36
|
+
*/
|
|
37
|
+
export function parseClients(output: string): {
|
|
38
|
+
signature: string;
|
|
39
|
+
clients: TmuxClient[];
|
|
40
|
+
windowActive: boolean;
|
|
41
|
+
} {
|
|
42
|
+
const [header = "", ...rest] = output.split("\n").map((line) => line.trim());
|
|
43
|
+
const [session = "", windowActive = "1"] = header.split("\t");
|
|
44
|
+
const rows = rest
|
|
45
|
+
.filter((row) => row && row.split("\t")[0] === session)
|
|
46
|
+
.sort();
|
|
47
|
+
const clients = rows.map((row) => {
|
|
48
|
+
const [, , termType = "", termName = ""] = row.split("\t");
|
|
49
|
+
return { termType, termName };
|
|
50
|
+
});
|
|
51
|
+
return { signature: rows.join("\n"), clients, windowActive: windowActive !== "0" };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The kitty decision for a set of viewing clients. With no client attached,
|
|
56
|
+
* nothing is shown anyway, so keep the previous decision.
|
|
57
|
+
*/
|
|
58
|
+
export function decide(
|
|
59
|
+
clients: TmuxClient[],
|
|
60
|
+
previous: boolean,
|
|
61
|
+
): boolean {
|
|
62
|
+
if (clients.length === 0) return previous;
|
|
63
|
+
return clients.every(clientSupportsKitty);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* True only when this process runs in a tmux pane. TERM=tmux-* without $TMUX
|
|
68
|
+
* means pi runs downstream of someone else's tmux (ssh out of a pane): that
|
|
69
|
+
* tmux cannot be asked who is watching, and passthrough would not reach it.
|
|
70
|
+
*/
|
|
71
|
+
export function isInTmuxSession(): boolean {
|
|
72
|
+
return Boolean(process.env.TMUX);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function probeArgs(): string[] {
|
|
76
|
+
// One tmux call: this pane's session, then all clients. Resolving the
|
|
77
|
+
// session every time follows a pane moved with join-pane.
|
|
78
|
+
const pane = process.env.TMUX_PANE;
|
|
79
|
+
const target = pane ? ["-t", pane] : [];
|
|
80
|
+
return [
|
|
81
|
+
"display-message",
|
|
82
|
+
"-p",
|
|
83
|
+
...target,
|
|
84
|
+
"#{session_id}\t#{window_active}",
|
|
85
|
+
";",
|
|
86
|
+
"list-clients",
|
|
87
|
+
"-F",
|
|
88
|
+
CLIENT_FORMAT,
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── Probe state ────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
let kitty: boolean | undefined;
|
|
95
|
+
let signature = "";
|
|
96
|
+
let windowActive: boolean | undefined;
|
|
97
|
+
let epoch = 0;
|
|
98
|
+
/** When a probe last saw this pane's window visible, and when we last sent image data. */
|
|
99
|
+
let lastSeenVisibleAt = 0;
|
|
100
|
+
let lastTransmitAt = -1;
|
|
101
|
+
const listeners = new Set<() => void>();
|
|
102
|
+
|
|
103
|
+
function changed(firstProbe: boolean): void {
|
|
104
|
+
// A new epoch tells renderers to send their images again.
|
|
105
|
+
epoch++;
|
|
106
|
+
if (!firstProbe) for (const listener of listeners) listener();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Records the output of one tmux probe (see probeArgs). Exported for tests. */
|
|
110
|
+
export function recordTmuxClients(output: string, now = Date.now()): void {
|
|
111
|
+
const parsed = parseClients(output);
|
|
112
|
+
const firstProbe = kitty === undefined;
|
|
113
|
+
const next = decide(parsed.clients, kitty ?? false);
|
|
114
|
+
const clientsChanged = parsed.signature !== signature || next !== kitty;
|
|
115
|
+
// tmux forwards image data only to clients that can see the pane. If data
|
|
116
|
+
// went out after the last probe that saw the window visible, it may have
|
|
117
|
+
// been dropped while the window was hidden, so send it again on return.
|
|
118
|
+
const missedWhileHidden =
|
|
119
|
+
parsed.windowActive &&
|
|
120
|
+
windowActive === false &&
|
|
121
|
+
lastTransmitAt >= lastSeenVisibleAt;
|
|
122
|
+
signature = parsed.signature;
|
|
123
|
+
kitty = next;
|
|
124
|
+
windowActive = parsed.windowActive;
|
|
125
|
+
if (parsed.windowActive) lastSeenVisibleAt = now;
|
|
126
|
+
if (firstProbe || clientsChanged || missedWhileHidden) changed(firstProbe);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Without an answer from tmux nobody is confirmed to speak kitty, so images
|
|
131
|
+
* stay off until a probe succeeds. Exported for tests.
|
|
132
|
+
*/
|
|
133
|
+
export function recordProbeFailure(): void {
|
|
134
|
+
const firstProbe = kitty === undefined;
|
|
135
|
+
signature = "";
|
|
136
|
+
if (kitty === false) return;
|
|
137
|
+
kitty = false;
|
|
138
|
+
changed(firstProbe);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Called whenever kitty image data is written to the terminal. */
|
|
142
|
+
export function noteImageTransmitted(now = Date.now()): void {
|
|
143
|
+
lastTransmitAt = now;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function probeSync(): void {
|
|
147
|
+
try {
|
|
148
|
+
recordTmuxClients(
|
|
149
|
+
execFileSync("tmux", probeArgs(), {
|
|
150
|
+
encoding: "utf8",
|
|
151
|
+
timeout: 250,
|
|
152
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
} catch {
|
|
156
|
+
recordProbeFailure();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
161
|
+
let probing = false;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Poll tmux once a second without blocking, and call `onChange` when the
|
|
165
|
+
* viewing clients change. Returns a function that stops this listener.
|
|
166
|
+
*/
|
|
167
|
+
export function watchTmuxClients(onChange: () => void): () => void {
|
|
168
|
+
listeners.add(onChange);
|
|
169
|
+
if (!timer && isInTmuxSession()) {
|
|
170
|
+
timer = setInterval(() => {
|
|
171
|
+
if (probing) return;
|
|
172
|
+
probing = true;
|
|
173
|
+
execFile(
|
|
174
|
+
"tmux",
|
|
175
|
+
probeArgs(),
|
|
176
|
+
{ encoding: "utf8", timeout: 1000 },
|
|
177
|
+
(error, stdout) => {
|
|
178
|
+
probing = false;
|
|
179
|
+
if (error) recordProbeFailure();
|
|
180
|
+
else recordTmuxClients(stdout);
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
}, 1000);
|
|
184
|
+
timer.unref?.();
|
|
185
|
+
}
|
|
186
|
+
return () => {
|
|
187
|
+
listeners.delete(onChange);
|
|
188
|
+
if (listeners.size === 0 && timer) {
|
|
189
|
+
clearInterval(timer);
|
|
190
|
+
timer = undefined;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Changes whenever images must be sent to the terminal again. */
|
|
196
|
+
export function imageEpoch(): number {
|
|
197
|
+
return epoch;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function detectImageProtocol(): ImageProtocol | null {
|
|
201
|
+
if (!isInTmuxSession()) return getCapabilities().images;
|
|
202
|
+
// Inside tmux only the probe counts: pi's own answer is always null here,
|
|
203
|
+
// and inline-images may have overridden it to "kitty".
|
|
204
|
+
if (kitty === undefined) probeSync();
|
|
205
|
+
return kitty ? "kitty" : null;
|
|
206
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Container,
|
|
3
|
+
getCapabilities,
|
|
4
|
+
Image,
|
|
5
|
+
setCapabilities,
|
|
6
|
+
type TerminalCapabilities,
|
|
7
|
+
} from "@earendil-works/pi-tui";
|
|
8
|
+
import {
|
|
9
|
+
detectImageProtocol,
|
|
10
|
+
imageEpoch,
|
|
11
|
+
isInTmuxSession,
|
|
12
|
+
watchTmuxClients,
|
|
13
|
+
} from "./image-protocol.ts";
|
|
14
|
+
import {
|
|
15
|
+
allocateImageId,
|
|
16
|
+
buildPlaceholderRow,
|
|
17
|
+
transmitImageWithPlaceholder,
|
|
18
|
+
} from "./kitty-placeholder.ts";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Draws chat images (tool results, sent attachments) inline inside tmux, the
|
|
22
|
+
* way pi draws them directly in kitty, Ghostty, WezTerm or Rex.
|
|
23
|
+
*
|
|
24
|
+
* pi-tui reports `images: null` inside tmux because direct kitty placements
|
|
25
|
+
* are not tied to a pane, and pi then skips creating Image components at all.
|
|
26
|
+
* While every client viewing this pane speaks kitty, this module sets
|
|
27
|
+
* `images: "kitty"` so pi builds its images exactly as in kitty or Rex, and
|
|
28
|
+
* patches Image.render to swap the direct placement for Unicode placeholders,
|
|
29
|
+
* which tmux stores as pane text. When another client views the pane (a phone
|
|
30
|
+
* over SSH), pi's own capabilities come back and images fall back to text.
|
|
31
|
+
* Outside tmux, or when pi-tui already draws images, nothing changes.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
type ImageInternals = {
|
|
35
|
+
base64Data: string;
|
|
36
|
+
mimeType: string;
|
|
37
|
+
pngData?: string;
|
|
38
|
+
cachedLines?: string[];
|
|
39
|
+
cachedWidth?: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type InlineState = {
|
|
43
|
+
key: string;
|
|
44
|
+
lines: string[];
|
|
45
|
+
imageId: number;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type Renderable = { render(width: number): string[] };
|
|
49
|
+
type RenderFn = (this: Renderable, width: number) => string[];
|
|
50
|
+
type Redrawable = {
|
|
51
|
+
invalidate(): void;
|
|
52
|
+
requestRender(force?: boolean): void;
|
|
53
|
+
terminal: unknown;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const PATCHED = Symbol.for("pi-image-preview.inline-images");
|
|
57
|
+
const states = new WeakMap<object, InlineState>();
|
|
58
|
+
|
|
59
|
+
/** Reads `c=` and `r=` from the kitty sequence pi-tui produced. */
|
|
60
|
+
export function parsePlacementSize(
|
|
61
|
+
sequence: string,
|
|
62
|
+
): { columns: number; rows: number } | null {
|
|
63
|
+
const columns = /[_,;]c=(\d+)/.exec(sequence);
|
|
64
|
+
const rows = /[_,;]r=(\d+)/.exec(sequence);
|
|
65
|
+
if (!columns || !rows) return null;
|
|
66
|
+
return { columns: Number(columns[1]), rows: Number(rows[1]) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function placeholderLines(
|
|
70
|
+
imageId: number,
|
|
71
|
+
columns: number,
|
|
72
|
+
rows: number,
|
|
73
|
+
): string[] {
|
|
74
|
+
return Array.from({ length: rows }, (_, row) =>
|
|
75
|
+
buildPlaceholderRow(imageId, row, columns),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function renderDirect(
|
|
80
|
+
image: ImageInternals,
|
|
81
|
+
original: RenderFn,
|
|
82
|
+
self: Renderable,
|
|
83
|
+
width: number,
|
|
84
|
+
): string[] {
|
|
85
|
+
const lines = original.call(self, width);
|
|
86
|
+
// pi-tui's cache now holds direct-placement lines; drop them so its own
|
|
87
|
+
// path never returns them after the override ends.
|
|
88
|
+
image.cachedLines = undefined;
|
|
89
|
+
image.cachedWidth = undefined;
|
|
90
|
+
return lines;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function renderInline(
|
|
94
|
+
this: Renderable,
|
|
95
|
+
original: RenderFn,
|
|
96
|
+
width: number,
|
|
97
|
+
): string[] {
|
|
98
|
+
// SAFETY: only installed on Image.prototype; pi-tui's Image keeps these as
|
|
99
|
+
// TypeScript-private fields, which are plain properties at runtime.
|
|
100
|
+
const image = this as unknown as ImageInternals;
|
|
101
|
+
const key = `${width}:${imageEpoch()}`;
|
|
102
|
+
const previous = states.get(this);
|
|
103
|
+
if (previous?.key === key) return previous.lines;
|
|
104
|
+
|
|
105
|
+
const direct = renderDirect(image, original, this, width);
|
|
106
|
+
const size = direct[0] ? parsePlacementSize(direct[0]) : null;
|
|
107
|
+
const data = image.mimeType === "image/png" ? image.base64Data : image.pngData;
|
|
108
|
+
// No size means pi-tui fell back to text (for example, no PNG converter).
|
|
109
|
+
if (!size || !data) return direct;
|
|
110
|
+
|
|
111
|
+
const imageId = sendOnce(data, size.columns, size.rows, previous?.imageId);
|
|
112
|
+
const lines = placeholderLines(imageId, size.columns, size.rows);
|
|
113
|
+
states.set(this, { key, lines, imageId });
|
|
114
|
+
return lines;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
type Sent = { imageId: number; epoch: number; columns: number; rows: number };
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Image data already in kitty, by base64 payload. pi rebuilds its Image
|
|
121
|
+
* components on every invalidate, so without this each redraw would send
|
|
122
|
+
* every transcript image again under a new ID and leave the old ones behind.
|
|
123
|
+
*/
|
|
124
|
+
const sent = new Map<string, Sent>();
|
|
125
|
+
|
|
126
|
+
function sendOnce(
|
|
127
|
+
data: string,
|
|
128
|
+
columns: number,
|
|
129
|
+
rows: number,
|
|
130
|
+
fallbackId: number | undefined,
|
|
131
|
+
): number {
|
|
132
|
+
const epoch = imageEpoch();
|
|
133
|
+
const known = sent.get(data);
|
|
134
|
+
if (
|
|
135
|
+
known &&
|
|
136
|
+
known.epoch === epoch &&
|
|
137
|
+
known.columns === columns &&
|
|
138
|
+
known.rows === rows
|
|
139
|
+
) {
|
|
140
|
+
return known.imageId;
|
|
141
|
+
}
|
|
142
|
+
// Re-sending under the same ID replaces the image in kitty.
|
|
143
|
+
const imageId = known?.imageId ?? fallbackId ?? allocateImageId();
|
|
144
|
+
transmitImageWithPlaceholder(data, imageId, columns, rows);
|
|
145
|
+
sent.set(data, { imageId, epoch, columns, rows });
|
|
146
|
+
return imageId;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ── Redraw on client change ────────────────────────────────
|
|
150
|
+
|
|
151
|
+
let tui: Redrawable | undefined;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Grabs the TUI instance on its next frame, then restores Container.render.
|
|
155
|
+
* Returns a function that restores it early if no frame was drawn.
|
|
156
|
+
*/
|
|
157
|
+
function captureTui(): () => void {
|
|
158
|
+
if (tui) return () => {};
|
|
159
|
+
// SAFETY: Container declares render(width): string[]; this only widens `this`.
|
|
160
|
+
const proto = Container.prototype as unknown as { render: RenderFn };
|
|
161
|
+
const original = proto.render;
|
|
162
|
+
const capture = function (this: Renderable, width: number) {
|
|
163
|
+
// SAFETY: fields are checked below before the value is kept.
|
|
164
|
+
const candidate = this as unknown as Partial<Redrawable>;
|
|
165
|
+
if (
|
|
166
|
+
candidate.terminal &&
|
|
167
|
+
typeof candidate.requestRender === "function" &&
|
|
168
|
+
typeof candidate.invalidate === "function"
|
|
169
|
+
) {
|
|
170
|
+
tui = candidate as Redrawable;
|
|
171
|
+
proto.render = original;
|
|
172
|
+
}
|
|
173
|
+
return original.call(this, width);
|
|
174
|
+
};
|
|
175
|
+
proto.render = capture;
|
|
176
|
+
return () => {
|
|
177
|
+
if (proto.render === capture) proto.render = original;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* pi decides whether a tool result gets an Image component when it builds the
|
|
183
|
+
* result, and rebuilds it on invalidate. Invalidating the whole tree therefore
|
|
184
|
+
* adds images that were skipped while a non-kitty client was viewing, and
|
|
185
|
+
* drops them when one arrives.
|
|
186
|
+
*/
|
|
187
|
+
function redraw(): void {
|
|
188
|
+
tui?.invalidate();
|
|
189
|
+
tui?.requestRender(true);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ── Capability override ────────────────────────────────
|
|
193
|
+
|
|
194
|
+
/** pi-tui's own capabilities, saved while the override is active. */
|
|
195
|
+
let saved: TerminalCapabilities | undefined;
|
|
196
|
+
|
|
197
|
+
function overriding(): boolean {
|
|
198
|
+
return saved !== undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Turns the kitty override on or off to match the current tmux viewers. */
|
|
202
|
+
function syncCapabilities(): void {
|
|
203
|
+
const wanted = detectImageProtocol() === "kitty";
|
|
204
|
+
// pi resets its capability cache when terminal settings change, which
|
|
205
|
+
// drops the override, so re-apply it whenever it is missing.
|
|
206
|
+
if (wanted && getCapabilities().images !== "kitty") {
|
|
207
|
+
saved ??= getCapabilities();
|
|
208
|
+
setCapabilities({ ...getCapabilities(), images: "kitty" });
|
|
209
|
+
} else if (!wanted && saved) {
|
|
210
|
+
setCapabilities(saved);
|
|
211
|
+
saved = undefined;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ── Install ────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
export function installInlineImages(): () => void {
|
|
218
|
+
// pi-tui already draws images here (kitty, Ghostty, Rex outside tmux).
|
|
219
|
+
if (getCapabilities().images || !isInTmuxSession()) return () => {};
|
|
220
|
+
// SAFETY: Image declares render(width): string[]; the symbol slot is ours.
|
|
221
|
+
const proto = Image.prototype as unknown as Renderable & {
|
|
222
|
+
[PATCHED]?: RenderFn;
|
|
223
|
+
};
|
|
224
|
+
if (proto[PATCHED]) return () => {};
|
|
225
|
+
const original = proto.render as RenderFn;
|
|
226
|
+
proto[PATCHED] = original;
|
|
227
|
+
proto.render = function (this: Renderable, width: number) {
|
|
228
|
+
// Not overriding means pi's own setting applies: either a native image
|
|
229
|
+
// terminal, or a non-kitty viewer (for example, a phone over SSH), where
|
|
230
|
+
// pi draws its text fallback. No delete is sent in that case, since it
|
|
231
|
+
// would reach the non-kitty client too; kitty evicts unused images on its
|
|
232
|
+
// own, and a new epoch resends them when kitty is the only viewer again.
|
|
233
|
+
if (!overriding()) return original.call(this, width);
|
|
234
|
+
return renderInline.call(this, original, width);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
syncCapabilities();
|
|
238
|
+
const stopCapture = captureTui();
|
|
239
|
+
const stopWatching = watchTmuxClients(() => {
|
|
240
|
+
syncCapabilities();
|
|
241
|
+
redraw();
|
|
242
|
+
});
|
|
243
|
+
return () => {
|
|
244
|
+
stopWatching();
|
|
245
|
+
stopCapture();
|
|
246
|
+
if (saved) setCapabilities(saved);
|
|
247
|
+
saved = undefined;
|
|
248
|
+
proto.render = original;
|
|
249
|
+
delete proto[PATCHED];
|
|
250
|
+
sent.clear();
|
|
251
|
+
redraw();
|
|
252
|
+
};
|
|
253
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kitty graphics protocol helpers shared by the attachment gallery and the
|
|
3
|
+
* inline transcript images. Images are drawn with Unicode placeholders, which
|
|
4
|
+
* tmux stores as ordinary text, so they stay with their pane.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isInTmuxSession, noteImageTransmitted } from "./image-protocol.ts";
|
|
8
|
+
|
|
9
|
+
// Counter for kitty image IDs over the full 24-bit range (1–16,777,215) that
|
|
10
|
+
// kitty's true-color encoding supports. Counting avoids collisions inside one
|
|
11
|
+
// process; the random start keeps several pi processes sharing one kitty from
|
|
12
|
+
// all starting at 1 and overwriting each other's images.
|
|
13
|
+
let nextImageId = 1 + Math.floor(Math.random() * 0xfffffe);
|
|
14
|
+
export function allocateImageId(): number {
|
|
15
|
+
const id = nextImageId;
|
|
16
|
+
nextImageId = (nextImageId % 0xffffff) + 1; // wrap at 16M, skip 0
|
|
17
|
+
return id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ── Kitty Unicode Placeholder Protocol ─────────────────────
|
|
21
|
+
// Instead of rendering pixels directly (which ghost across tmux panes),
|
|
22
|
+
// we transmit the image data and then output U+10EEEE placeholder
|
|
23
|
+
// characters with diacritics encoding row/col. Since these are just
|
|
24
|
+
// text characters, tmux manages them per-pane — images appear/disappear
|
|
25
|
+
// naturally when switching panes.
|
|
26
|
+
//
|
|
27
|
+
// Protocol:
|
|
28
|
+
// 1. Transmit image: ESC_G a=T,U=1,f=100,i=<id>,c=<cols>,r=<rows>,q=2; base64 ESC\
|
|
29
|
+
// 2. Print placeholder chars with foreground color set to image_id:
|
|
30
|
+
// ESC[38;5;<id>m <U+10EEEE><row_diac><col_diac> ... ESC[39m
|
|
31
|
+
//
|
|
32
|
+
// Diacritics: row 0 = U+0305, row 1 = U+030D, row 2 = U+030E, etc.
|
|
33
|
+
// See kitty docs rowcolumn-diacritics.txt
|
|
34
|
+
|
|
35
|
+
// Row/column diacritics from kitty's rowcolumn-diacritics.txt
|
|
36
|
+
// These are the combining characters used to encode row and column numbers
|
|
37
|
+
const ROW_COL_DIACRITICS = [
|
|
38
|
+
0x0305, 0x030d, 0x030e, 0x0310, 0x0312, 0x033d, 0x033e, 0x033f,
|
|
39
|
+
0x0346, 0x034a, 0x034b, 0x034c, 0x0350, 0x0351, 0x0352, 0x0353,
|
|
40
|
+
0x0357, 0x035b, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367, 0x0368,
|
|
41
|
+
0x0369, 0x036a, 0x036b, 0x036c, 0x036d, 0x036e, 0x036f, 0x0483,
|
|
42
|
+
0x0484, 0x0485, 0x0486, 0x0592, 0x0593, 0x0594, 0x0595, 0x0597,
|
|
43
|
+
0x0598, 0x0599, 0x059c, 0x059d, 0x059e, 0x059f, 0x05a0, 0x05a1,
|
|
44
|
+
0x05a8, 0x05a9, 0x05ab, 0x05ac, 0x05af, 0x05c4, 0x0610, 0x0611,
|
|
45
|
+
0x0612, 0x0613, 0x0614, 0x0615, 0x0616, 0x0617, 0x0618, 0x0619,
|
|
46
|
+
0x061a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f, 0x0650, 0x0651,
|
|
47
|
+
0x0652, 0x0653, 0x0654, 0x0655, 0x0656, 0x0657, 0x0658, 0x0659,
|
|
48
|
+
0x065a, 0x065b, 0x065c, 0x065d, 0x065e, 0x065f, 0x0670, 0x06d6,
|
|
49
|
+
0x06d7, 0x06d8, 0x06d9, 0x06da, 0x06db, 0x06dc, 0x06df, 0x06e0,
|
|
50
|
+
0x06e1, 0x06e2, 0x06e3, 0x06e4, 0x06e7, 0x06e8, 0x06ea, 0x06eb,
|
|
51
|
+
0x06ec, 0x06ed,
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
const PLACEHOLDER_CHAR = String.fromCodePoint(0x10EEEE);
|
|
55
|
+
|
|
56
|
+
function diacriticFor(n: number): string {
|
|
57
|
+
if (n < ROW_COL_DIACRITICS.length) {
|
|
58
|
+
return String.fromCodePoint(ROW_COL_DIACRITICS[n]);
|
|
59
|
+
}
|
|
60
|
+
// Fallback for very large values (shouldn't happen for thumbnails)
|
|
61
|
+
return String.fromCodePoint(ROW_COL_DIACRITICS[0]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Wrap kitty APC sequences in DCS passthrough for tmux.
|
|
66
|
+
*/
|
|
67
|
+
function wrapForTmux(sequence: string): string {
|
|
68
|
+
if (!isInTmuxSession()) return sequence;
|
|
69
|
+
return sequence.replace(
|
|
70
|
+
/\x1b_G([^\x1b]*)\x1b\\/g,
|
|
71
|
+
(_match, content) =>
|
|
72
|
+
`\x1bPtmux;\x1b\x1b_G${content}\x1b\x1b\\\x1b\\`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Transmit image and create virtual placement using Unicode placeholder mode.
|
|
78
|
+
* The image data is sent to kitty but NOT displayed directly.
|
|
79
|
+
* Display happens via U+10EEEE placeholder characters.
|
|
80
|
+
*/
|
|
81
|
+
export function transmitImageWithPlaceholder(
|
|
82
|
+
base64Data: string,
|
|
83
|
+
imageId: number,
|
|
84
|
+
columns: number,
|
|
85
|
+
rows: number,
|
|
86
|
+
): void {
|
|
87
|
+
// Transmit image + create virtual placement in one command
|
|
88
|
+
// a=T: transmit and display, U=1: use unicode placeholders
|
|
89
|
+
// q=2: suppress all responses (important in tmux)
|
|
90
|
+
const CHUNK_SIZE = 4096;
|
|
91
|
+
noteImageTransmitted();
|
|
92
|
+
|
|
93
|
+
if (base64Data.length <= CHUNK_SIZE) {
|
|
94
|
+
const seq = `\x1b_Ga=T,U=1,f=100,i=${imageId},c=${columns},r=${rows},q=2;${base64Data}\x1b\\`;
|
|
95
|
+
process.stdout.write(wrapForTmux(seq));
|
|
96
|
+
} else {
|
|
97
|
+
// Chunked transfer
|
|
98
|
+
let offset = 0;
|
|
99
|
+
let isFirst = true;
|
|
100
|
+
while (offset < base64Data.length) {
|
|
101
|
+
const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
|
|
102
|
+
const isLast = offset + CHUNK_SIZE >= base64Data.length;
|
|
103
|
+
let seq: string;
|
|
104
|
+
|
|
105
|
+
if (isFirst) {
|
|
106
|
+
seq = `\x1b_Ga=T,U=1,f=100,i=${imageId},c=${columns},r=${rows},q=2,m=1;${chunk}\x1b\\`;
|
|
107
|
+
isFirst = false;
|
|
108
|
+
} else if (isLast) {
|
|
109
|
+
seq = `\x1b_Gm=0;${chunk}\x1b\\`;
|
|
110
|
+
} else {
|
|
111
|
+
seq = `\x1b_Gm=1;${chunk}\x1b\\`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
process.stdout.write(wrapForTmux(seq));
|
|
115
|
+
offset += CHUNK_SIZE;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Delete a kitty image by ID.
|
|
122
|
+
*/
|
|
123
|
+
export function deleteImage(imageId: number): void {
|
|
124
|
+
const seq = `\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`;
|
|
125
|
+
process.stdout.write(wrapForTmux(seq));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Build a row of Unicode placeholder characters for the given image.
|
|
130
|
+
* Uses foreground color to encode image_id, diacritics to encode row/col.
|
|
131
|
+
*/
|
|
132
|
+
export function buildPlaceholderRow(
|
|
133
|
+
imageId: number,
|
|
134
|
+
row: number,
|
|
135
|
+
columns: number,
|
|
136
|
+
): string {
|
|
137
|
+
// Set foreground color to image_id (using 24-bit true color for large IDs)
|
|
138
|
+
const r = (imageId >> 16) & 0xff;
|
|
139
|
+
const g = (imageId >> 8) & 0xff;
|
|
140
|
+
const b = imageId & 0xff;
|
|
141
|
+
const fgStart = imageId < 256
|
|
142
|
+
? `\x1b[38;5;${imageId}m`
|
|
143
|
+
: `\x1b[38;2;${r};${g};${b}m`;
|
|
144
|
+
const fgEnd = `\x1b[39m`;
|
|
145
|
+
|
|
146
|
+
let line = fgStart;
|
|
147
|
+
|
|
148
|
+
// First cell: full diacritics (row + col)
|
|
149
|
+
line += PLACEHOLDER_CHAR + diacriticFor(row) + diacriticFor(0);
|
|
150
|
+
|
|
151
|
+
// Subsequent cells: no diacritics needed — kitty auto-increments
|
|
152
|
+
// column index from the left neighbor's col diacritic
|
|
153
|
+
for (let col = 1; col < columns; col++) {
|
|
154
|
+
line += PLACEHOLDER_CHAR;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
line += fgEnd;
|
|
158
|
+
return line;
|
|
159
|
+
}
|
|
160
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ImageContent } from "./content.ts";
|
|
2
|
+
|
|
3
|
+
/** Thumbnails only need to fill a ~25-cell gallery slot; keep them small so the
|
|
4
|
+
* data survives kitty transmission through tmux passthrough. */
|
|
5
|
+
export const PREVIEW_MAX_DIMENSION = 480;
|
|
6
|
+
export const PREVIEW_MAX_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
type ResizedImage = { data: string; mimeType: string };
|
|
9
|
+
|
|
10
|
+
export interface PreviewResizeDeps {
|
|
11
|
+
/** Resize raw image bytes within the given bounds (pi's WASM resizer). */
|
|
12
|
+
resizeImage: (
|
|
13
|
+
bytes: Uint8Array,
|
|
14
|
+
mimeType: string,
|
|
15
|
+
options: { maxWidth: number; maxHeight: number; maxBytes: number },
|
|
16
|
+
) => Promise<ResizedImage | null>;
|
|
17
|
+
/** Re-encode an image as PNG (kitty's f=100 transmission requires PNG). */
|
|
18
|
+
convertToPng: (
|
|
19
|
+
data: string,
|
|
20
|
+
mimeType: string,
|
|
21
|
+
) => Promise<ResizedImage | null>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Build a small PNG thumbnail for the gallery preview. The kitty graphics
|
|
26
|
+
* transmission uses f=100 (PNG), so the result is always converted to PNG;
|
|
27
|
+
* a non-PNG payload would render as a blank block.
|
|
28
|
+
*/
|
|
29
|
+
export async function resizeForPreview(
|
|
30
|
+
image: ImageContent,
|
|
31
|
+
deps: PreviewResizeDeps,
|
|
32
|
+
): Promise<ImageContent> {
|
|
33
|
+
try {
|
|
34
|
+
const bytes = Buffer.from(image.data, "base64");
|
|
35
|
+
const resized = await deps.resizeImage(bytes, image.mimeType, {
|
|
36
|
+
maxWidth: PREVIEW_MAX_DIMENSION,
|
|
37
|
+
maxHeight: PREVIEW_MAX_DIMENSION,
|
|
38
|
+
maxBytes: PREVIEW_MAX_BYTES,
|
|
39
|
+
});
|
|
40
|
+
const small = resized ?? { data: image.data, mimeType: image.mimeType };
|
|
41
|
+
const png = await deps.convertToPng(small.data, small.mimeType);
|
|
42
|
+
const final = png ?? small;
|
|
43
|
+
return { type: "image", data: final.data, mimeType: final.mimeType };
|
|
44
|
+
} catch {
|
|
45
|
+
return image;
|
|
46
|
+
}
|
|
47
|
+
}
|