kaizen-agent 1.85.1 → 1.86.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/web/term.ts CHANGED
@@ -9,6 +9,7 @@ import { WebLinksAddon } from "@xterm/addon-web-links";
9
9
  import { SearchAddon } from "@xterm/addon-search";
10
10
  import { Unicode11Addon } from "@xterm/addon-unicode11";
11
11
  import { fontRead, fontSet, fontStep } from "./term-font.ts";
12
+ import { imageFiles, pathsText } from "./term-paste.ts";
12
13
 
13
14
  // Orca's defaults (its "Ghostty Default Style Dark" and "Builtin Tango Light"): a terminal's ANSI
14
15
  // palette is content, not board chrome, so these hex values are the one exception to design.md's
@@ -94,11 +95,25 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
94
95
  };
95
96
  // Only to a program that asked: a plain shell would print the reply as text.
96
97
  let schemeNotify = false;
98
+ // Output replayed on attach (the session so far) repeats old questions; answering them again
99
+ // would type the reply into whatever runs now. Output in the first 150 ms after the socket
100
+ // opens is taken as that replay. ponytail: a timing guess; a server-side replay marker if a
101
+ // real query ever lands in that window (agents start slower than 150 ms).
102
+ let replaying = false, attachedAt = 0;
103
+ const scheme = () => `\x1b[?997;${dark() ? 1 : 2}n`;
97
104
  const reportScheme = () => {
98
105
  if (!schemeNotify) return;
99
- const t = document.documentElement.dataset.theme;
100
- send({ t: "i", d: `\x1b[?997;${(t ? t === "dark" : os.matches) ? 1 : 2}n` });
106
+ send({ t: "i", d: scheme() });
101
107
  };
108
+ // OSC 10/11 "what are your colours?": an agent on an auto theme (Claude Code's default) picks
109
+ // light or dark from the background it is told. xterm.js does not answer these itself.
110
+ const rgb = (hex: string) => "rgb:" + [1, 3, 5].map((i) => hex.slice(i, i + 2).repeat(2)).join("/");
111
+ for (const [n, k] of [[10, "foreground"], [11, "background"]] as const)
112
+ term.parser.registerOscHandler(n, (data) => {
113
+ if (data !== "?") return false;
114
+ if (!replaying) send({ t: "i", d: `\x1b]${n};${rgb((dark() ? ORCA_DARK : ORCA_LIGHT)[k])}\x1b\\` });
115
+ return true;
116
+ });
102
117
  // Returning false lets xterm.js handle the sequence as usual. A replayed session repeats
103
118
  // the request on attach, which also corrects a theme changed while this view was closed.
104
119
  const schemeMode = (on: boolean) => (params: (number | number[])[]) => {
@@ -111,7 +126,8 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
111
126
  term.parser.registerCsiHandler({ prefix: "?", final: "h" }, schemeMode(true));
112
127
  term.parser.registerCsiHandler({ prefix: "?", final: "l" }, schemeMode(false));
113
128
  term.parser.registerCsiHandler({ prefix: "?", final: "n" }, (params) => {
114
- if (params[0] === 996) reportScheme();
129
+ // A one-off "dark or light?" is answered whether or not the program subscribed (2031).
130
+ if (params[0] === 996 && !replaying) send({ t: "i", d: scheme() });
115
131
  return false;
116
132
  });
117
133
  // Each message makes the agent repaint its whole screen, so a size already sent on this
@@ -157,13 +173,18 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
157
173
  `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/pty?key=${encodeURIComponent(key)}`,
158
174
  );
159
175
  ws.onopen = () => {
176
+ attachedAt = performance.now();
160
177
  if (lost) { term.reset(); schemeNotify = false; }
161
178
  lost = false;
162
179
  tries = 0;
163
180
  reBtn.hidden = true;
164
181
  size(true);
165
182
  };
166
- ws.onmessage = (e) => term.write(String(e.data));
183
+ ws.onmessage = (e) => {
184
+ if (performance.now() - attachedAt > 150) return void term.write(String(e.data));
185
+ replaying = true;
186
+ term.write(String(e.data), () => (replaying = false));
187
+ };
167
188
  // The board closes with "ended" when the agent exits; anything else is the board gone.
168
189
  ws.onclose = (e) => {
169
190
  if (e.reason === "ended") {
@@ -179,6 +200,57 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
179
200
  onEnd?.();
180
201
  };
181
202
  };
203
+ // A terminal carries no pictures: an image is saved on the board's computer and its path
204
+ // typed in, which is how Claude Code and Codex take one. Used by desktop paste and drop, and
205
+ // by the phone's Paste and Pick image keys. A refused image says why instead of doing nothing.
206
+ const note = (text: string) => {
207
+ const n = document.createElement("div");
208
+ n.className = "termnote";
209
+ n.setAttribute("role", "status");
210
+ n.textContent = text;
211
+ n.style.cssText = "position:absolute;right:12px;bottom:12px;z-index:5;padding:6px 10px;border-radius:6px;font:12px system-ui,sans-serif;background:#000c;color:#fff;pointer-events:none";
212
+ (el.querySelector(".xterm") ?? el).append(n); // .xterm is position:relative
213
+ setTimeout(() => n.remove(), 4000);
214
+ };
215
+ const upload = async (b: Blob) => {
216
+ const r = await fetch("/term-image", { method: "POST", headers: { "content-type": b.type || "image/png" }, body: b }).catch(() => null);
217
+ if (r?.ok) return String((await r.json()).path ?? "");
218
+ const why = r ? String((await r.json().catch(() => ({}))).error ?? r.status) : "the board did not answer";
219
+ note(`Image not pasted: ${why}`);
220
+ return "";
221
+ };
222
+ const sendImages = async (files: Blob[]) => {
223
+ const text = pathsText(await Promise.all(files.map(upload)));
224
+ if (text) term.paste(text);
225
+ };
226
+ const sendImage = (b: Blob) => sendImages([b]);
227
+ // Ctrl+V / Cmd+V on a desktop: xterm reads only text from the clipboard, so a screenshot
228
+ // (Windows Snipping Tool, Print Screen, macOS Cmd+Ctrl+Shift+4) pasted nothing. Capture runs
229
+ // before xterm's own handler on its textarea. Text wins whenever there is any: Excel and Word
230
+ // put a picture of the selection beside its text, and the text is what was meant.
231
+ const pasteOff = new AbortController();
232
+ el.addEventListener("paste", (e) => {
233
+ if (e.clipboardData?.getData("text/plain")) return;
234
+ const files = imageFiles(e.clipboardData?.files);
235
+ if (!files.length) return;
236
+ e.preventDefault();
237
+ e.stopPropagation();
238
+ void sendImages(files);
239
+ }, { capture: true, signal: pasteOff.signal });
240
+ // Dropping image files onto the terminal does the same.
241
+ el.addEventListener("dragover", (e) => {
242
+ if (![...(e.dataTransfer?.items ?? [])].some((i) => i.kind === "file")) return;
243
+ e.preventDefault();
244
+ if (e.dataTransfer) e.dataTransfer.dropEffect = "copy";
245
+ }, { signal: pasteOff.signal });
246
+ el.addEventListener("drop", (e) => {
247
+ const all = [...(e.dataTransfer?.files ?? [])];
248
+ if (!all.length) return;
249
+ e.preventDefault();
250
+ const files = imageFiles(e.dataTransfer?.files);
251
+ if (files.length) void sendImages(files);
252
+ else note("Only images can be dropped here");
253
+ }, { signal: pasteOff.signal });
182
254
  // Phone/tablet: a row of chips for the keys a soft keyboard lacks, and a message box
183
255
  // that types into the agent and presses Enter. Alt is sticky for the next key. Ctrl
184
256
  // opens its own a-z pad instead of waiting on the native keyboard, whose IME can
@@ -194,13 +266,6 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
194
266
  let stopViewport = () => {};
195
267
  if (matchMedia("(pointer: coarse)").matches) {
196
268
  const arrow = (c: string) => () => `\x1b${term.modes.applicationCursorKeysMode ? "O" : "["}${c}`;
197
- // A terminal carries no pictures: an image is saved on the board's computer and its path
198
- // typed in, which is how Claude Code and Codex take one.
199
- const sendImage = async (b: Blob) => {
200
- const r = await fetch("/term-image", { method: "POST", headers: { "content-type": b.type || "image/png" }, body: b }).catch(() => null);
201
- const path = r?.ok ? (await r.json()).path : "";
202
- if (path) term.paste(/\s/.test(path) ? `"${path}" ` : `${path} `);
203
- };
204
269
  const paste = async () => {
205
270
  const items = await navigator.clipboard?.read?.().catch(() => []) ?? [];
206
271
  for (const it of items) {
@@ -576,6 +641,7 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
576
641
  term.focus();
577
642
 
578
643
  return () => {
644
+ pasteOff.abort();
579
645
  ro.disconnect();
580
646
  clearTimeout(resizing);
581
647
  cancelAnimationFrame(frame);