kaizen-agent 1.85.1 → 1.85.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/web/term.ts CHANGED
@@ -94,11 +94,25 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
94
94
  };
95
95
  // Only to a program that asked: a plain shell would print the reply as text.
96
96
  let schemeNotify = false;
97
+ // Output replayed on attach (the session so far) repeats old questions; answering them again
98
+ // would type the reply into whatever runs now. Output in the first 150 ms after the socket
99
+ // opens is taken as that replay. ponytail: a timing guess; a server-side replay marker if a
100
+ // real query ever lands in that window (agents start slower than 150 ms).
101
+ let replaying = false, attachedAt = 0;
102
+ const scheme = () => `\x1b[?997;${dark() ? 1 : 2}n`;
97
103
  const reportScheme = () => {
98
104
  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` });
105
+ send({ t: "i", d: scheme() });
101
106
  };
107
+ // OSC 10/11 "what are your colours?": an agent on an auto theme (Claude Code's default) picks
108
+ // light or dark from the background it is told. xterm.js does not answer these itself.
109
+ const rgb = (hex: string) => "rgb:" + [1, 3, 5].map((i) => hex.slice(i, i + 2).repeat(2)).join("/");
110
+ for (const [n, k] of [[10, "foreground"], [11, "background"]] as const)
111
+ term.parser.registerOscHandler(n, (data) => {
112
+ if (data !== "?") return false;
113
+ if (!replaying) send({ t: "i", d: `\x1b]${n};${rgb((dark() ? ORCA_DARK : ORCA_LIGHT)[k])}\x1b\\` });
114
+ return true;
115
+ });
102
116
  // Returning false lets xterm.js handle the sequence as usual. A replayed session repeats
103
117
  // the request on attach, which also corrects a theme changed while this view was closed.
104
118
  const schemeMode = (on: boolean) => (params: (number | number[])[]) => {
@@ -111,7 +125,8 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
111
125
  term.parser.registerCsiHandler({ prefix: "?", final: "h" }, schemeMode(true));
112
126
  term.parser.registerCsiHandler({ prefix: "?", final: "l" }, schemeMode(false));
113
127
  term.parser.registerCsiHandler({ prefix: "?", final: "n" }, (params) => {
114
- if (params[0] === 996) reportScheme();
128
+ // A one-off "dark or light?" is answered whether or not the program subscribed (2031).
129
+ if (params[0] === 996 && !replaying) send({ t: "i", d: scheme() });
115
130
  return false;
116
131
  });
117
132
  // Each message makes the agent repaint its whole screen, so a size already sent on this
@@ -157,13 +172,18 @@ function mountTerm(el: HTMLElement, key: string, onEnd?: () => void): () => void
157
172
  `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/pty?key=${encodeURIComponent(key)}`,
158
173
  );
159
174
  ws.onopen = () => {
175
+ attachedAt = performance.now();
160
176
  if (lost) { term.reset(); schemeNotify = false; }
161
177
  lost = false;
162
178
  tries = 0;
163
179
  reBtn.hidden = true;
164
180
  size(true);
165
181
  };
166
- ws.onmessage = (e) => term.write(String(e.data));
182
+ ws.onmessage = (e) => {
183
+ if (performance.now() - attachedAt > 150) return void term.write(String(e.data));
184
+ replaying = true;
185
+ term.write(String(e.data), () => (replaying = false));
186
+ };
167
187
  // The board closes with "ended" when the agent exits; anything else is the board gone.
168
188
  ws.onclose = (e) => {
169
189
  if (e.reason === "ended") {