@keyou007/agent-bot 0.1.27-alpha.3 → 0.1.27-alpha.5

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.
Files changed (159) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +102 -18
  3. package/README.zh.md +102 -18
  4. package/assets/agent-bot.ico +0 -0
  5. package/assets/roboto-mono-OFL.txt +93 -0
  6. package/assets/roboto-mono-latin.woff2 +0 -0
  7. package/assets/workspace-drafts.js +88 -0
  8. package/assets/workspace-sessions.js +2676 -243
  9. package/assets/workspace.css +553 -44
  10. package/assets/workspace.js +3429 -327
  11. package/dist/cli/SelfUpdateObserver.js +124 -0
  12. package/dist/cli/SelfUpdateObserver.js.map +1 -0
  13. package/dist/cli/SelfUpdateRunner.js +267 -70
  14. package/dist/cli/SelfUpdateRunner.js.map +1 -1
  15. package/dist/cli/SelfUpdater.js +54 -15
  16. package/dist/cli/SelfUpdater.js.map +1 -1
  17. package/dist/cli/controlProtocol.js.map +1 -1
  18. package/dist/cli/help.js +6 -0
  19. package/dist/cli/help.js.map +1 -1
  20. package/dist/cli.js +85 -6
  21. package/dist/cli.js.map +1 -1
  22. package/dist/codex/CodexEventMapper.js +29 -5
  23. package/dist/codex/CodexEventMapper.js.map +1 -1
  24. package/dist/codex/CodexRuntime.js +3 -0
  25. package/dist/codex/CodexRuntime.js.map +1 -1
  26. package/dist/feishu/CardRenderer.js +45 -13
  27. package/dist/feishu/CardRenderer.js.map +1 -1
  28. package/dist/feishu/ConsoleFeishuClient.js +4 -0
  29. package/dist/feishu/ConsoleFeishuClient.js.map +1 -1
  30. package/dist/feishu/FeishuMarkdown.js +2 -14
  31. package/dist/feishu/FeishuMarkdown.js.map +1 -1
  32. package/dist/feishu/FeishuMessageClient.js +9 -0
  33. package/dist/feishu/FeishuMessageClient.js.map +1 -1
  34. package/dist/feishu/FeishuTurnPresenter.js +51 -22
  35. package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
  36. package/dist/index.js +47 -12
  37. package/dist/index.js.map +1 -1
  38. package/dist/local-files/DiagramPreview.js +11 -6
  39. package/dist/local-files/DiagramPreview.js.map +1 -1
  40. package/dist/local-files/GitDiffPreview.js +106 -14
  41. package/dist/local-files/GitDiffPreview.js.map +1 -1
  42. package/dist/local-files/LiveMarkup.js +104 -0
  43. package/dist/local-files/LiveMarkup.js.map +1 -0
  44. package/dist/local-files/LocalFileViewerServer.js +141 -122
  45. package/dist/local-files/LocalFileViewerServer.js.map +1 -1
  46. package/dist/local-files/MarkdownExport.js +54 -0
  47. package/dist/local-files/MarkdownExport.js.map +1 -0
  48. package/dist/local-files/PreviewCodeFont.js +11 -0
  49. package/dist/local-files/PreviewCodeFont.js.map +1 -0
  50. package/dist/local-files/PreviewSyntaxHighlight.js +73 -0
  51. package/dist/local-files/PreviewSyntaxHighlight.js.map +1 -1
  52. package/dist/local-files/ToolCommandDisplay.js +111 -0
  53. package/dist/local-files/ToolCommandDisplay.js.map +1 -0
  54. package/dist/local-files/TurnPreviewPage.js +53 -276
  55. package/dist/local-files/TurnPreviewPage.js.map +1 -1
  56. package/dist/local-files/WorkspaceFileEvents.js +206 -0
  57. package/dist/local-files/WorkspaceFileEvents.js.map +1 -0
  58. package/dist/local-files/WorkspaceFiles.js +79 -3
  59. package/dist/local-files/WorkspaceFiles.js.map +1 -1
  60. package/dist/local-files/WorkspaceImages.js +43 -0
  61. package/dist/local-files/WorkspaceImages.js.map +1 -0
  62. package/dist/local-files/WorkspacePage.js +24 -6
  63. package/dist/local-files/WorkspacePage.js.map +1 -1
  64. package/dist/local-files/WorkspaceSessions.js +1153 -104
  65. package/dist/local-files/WorkspaceSessions.js.map +1 -1
  66. package/dist/local-files/WorkspaceTimeline.js +129 -0
  67. package/dist/local-files/WorkspaceTimeline.js.map +1 -0
  68. package/dist/presentation/OutboundRouter.js +15 -6
  69. package/dist/presentation/OutboundRouter.js.map +1 -1
  70. package/dist/presentation/TurnStateReducer.js +31 -22
  71. package/dist/presentation/TurnStateReducer.js.map +1 -1
  72. package/dist/proxy/ProxySessionController.js +335 -113
  73. package/dist/proxy/ProxySessionController.js.map +1 -1
  74. package/dist/proxy/TaskMetadata.js +86 -0
  75. package/dist/proxy/TaskMetadata.js.map +1 -0
  76. package/dist/scheduling/ScheduleCards.js +10 -4
  77. package/dist/scheduling/ScheduleCards.js.map +1 -1
  78. package/dist/scheduling/ScheduleCommand.js +4 -0
  79. package/dist/scheduling/ScheduleCommand.js.map +1 -1
  80. package/dist/scheduling/ScheduledTaskStore.js +53 -12
  81. package/dist/scheduling/ScheduledTaskStore.js.map +1 -1
  82. package/dist/scheduling/types.js +19 -4
  83. package/dist/scheduling/types.js.map +1 -1
  84. package/dist/state/StateStore.js +213 -23
  85. package/dist/state/StateStore.js.map +1 -1
  86. package/dist/state/TurnPreviewJournal.js +157 -39
  87. package/dist/state/TurnPreviewJournal.js.map +1 -1
  88. package/dist/state/TurnToolText.js +172 -0
  89. package/dist/state/TurnToolText.js.map +1 -0
  90. package/dist/state/migrations.js +4 -0
  91. package/dist/state/migrations.js.map +1 -1
  92. package/dist/supervision/SafeRestartNotifier.js +9 -6
  93. package/dist/supervision/SafeRestartNotifier.js.map +1 -1
  94. package/dist/terminal/TerminalPage.js +5 -0
  95. package/dist/terminal/TerminalPage.js.map +1 -1
  96. package/dist/updates/AutomaticUpdatePreparer.js +63 -12
  97. package/dist/updates/AutomaticUpdatePreparer.js.map +1 -1
  98. package/dist/updates/ManualUpdateController.js +7 -2
  99. package/dist/updates/ManualUpdateController.js.map +1 -1
  100. package/dist/utils/promptAttachments.js +22 -0
  101. package/dist/utils/promptAttachments.js.map +1 -0
  102. package/dist/utils/promptQuotes.js +47 -0
  103. package/dist/utils/promptQuotes.js.map +1 -0
  104. package/dist/utils/toolTimestamp.js +6 -0
  105. package/dist/utils/toolTimestamp.js.map +1 -0
  106. package/npm-shrinkwrap.json +2 -2
  107. package/package.json +1 -1
  108. package/skills/agent-bot/SKILL.md +23 -2
  109. package/src/cli/SelfUpdateObserver.ts +111 -0
  110. package/src/cli/SelfUpdateRunner.ts +271 -85
  111. package/src/cli/SelfUpdater.ts +53 -16
  112. package/src/cli/controlProtocol.ts +1 -0
  113. package/src/cli/help.ts +6 -0
  114. package/src/cli.ts +71 -5
  115. package/src/codex/CodexEventMapper.ts +25 -5
  116. package/src/codex/CodexRuntime.ts +4 -0
  117. package/src/feishu/CardRenderer.ts +48 -13
  118. package/src/feishu/ConsoleFeishuClient.ts +6 -1
  119. package/src/feishu/FeishuMarkdown.ts +2 -14
  120. package/src/feishu/FeishuMessageClient.ts +10 -0
  121. package/src/feishu/FeishuTurnPresenter.ts +49 -16
  122. package/src/feishu/types.ts +1 -0
  123. package/src/index.ts +47 -12
  124. package/src/local-files/DiagramPreview.ts +11 -6
  125. package/src/local-files/GitDiffPreview.ts +107 -15
  126. package/src/local-files/LiveMarkup.ts +103 -0
  127. package/src/local-files/LocalFileViewerServer.ts +128 -125
  128. package/src/local-files/MarkdownExport.ts +54 -0
  129. package/src/local-files/PreviewCodeFont.ts +12 -0
  130. package/src/local-files/PreviewSyntaxHighlight.ts +75 -0
  131. package/src/local-files/ToolCommandDisplay.ts +104 -0
  132. package/src/local-files/TurnPreviewPage.ts +58 -261
  133. package/src/local-files/WorkspaceFileEvents.ts +186 -0
  134. package/src/local-files/WorkspaceFiles.ts +72 -5
  135. package/src/local-files/WorkspaceImages.ts +35 -0
  136. package/src/local-files/WorkspacePage.ts +24 -6
  137. package/src/local-files/WorkspaceSessions.ts +1057 -111
  138. package/src/local-files/WorkspaceTimeline.ts +135 -0
  139. package/src/presentation/OutboundRouter.ts +18 -4
  140. package/src/presentation/TurnStateReducer.ts +30 -19
  141. package/src/presentation/turnViewTypes.ts +3 -1
  142. package/src/proxy/ProxySessionController.ts +336 -96
  143. package/src/proxy/TaskMetadata.ts +81 -0
  144. package/src/runtime/types.ts +2 -1
  145. package/src/scheduling/ScheduleCards.ts +10 -4
  146. package/src/scheduling/ScheduleCommand.ts +4 -0
  147. package/src/scheduling/ScheduledTaskStore.ts +56 -9
  148. package/src/scheduling/types.ts +27 -4
  149. package/src/state/StateStore.ts +227 -24
  150. package/src/state/TurnPreviewJournal.ts +155 -38
  151. package/src/state/TurnToolText.ts +156 -0
  152. package/src/state/migrations.ts +4 -0
  153. package/src/supervision/SafeRestartNotifier.ts +9 -6
  154. package/src/terminal/TerminalPage.ts +5 -0
  155. package/src/updates/AutomaticUpdatePreparer.ts +52 -11
  156. package/src/updates/ManualUpdateController.ts +8 -3
  157. package/src/utils/promptAttachments.ts +27 -0
  158. package/src/utils/promptQuotes.ts +48 -0
  159. package/src/utils/toolTimestamp.ts +5 -0
@@ -48,6 +48,8 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
48
48
  const states = new WeakMap();
49
49
  let library;
50
50
  let sequence = 0;
51
+ const workspaceTheme = () => document.documentElement.dataset.workspaceTheme;
52
+ const isRendered = (state) => state.rendered === state.source && state.renderedTheme === workspaceTheme();
51
53
  const loadLibrary = () => {
52
54
  if (!library) library = new Promise((resolve, reject) => {
53
55
  const url = document.body.dataset.diagramScript;
@@ -84,20 +86,21 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
84
86
  block.querySelector(".diagram-source").hidden = mode !== "source";
85
87
  block.dataset.diagramSize = state.size;
86
88
  const sizeButton = block.querySelector("[data-diagram-size-toggle]");
87
- sizeButton.hidden = mode !== "preview" || state.rendered !== state.source;
89
+ const ready = isRendered(state);
90
+ sizeButton.hidden = mode !== "preview" || !ready;
88
91
  sizeButton.textContent = state.size === "actual" ? (zh ? "适应" : "Fit") : (zh ? "原大" : "Actual size");
89
92
  sizeButton.setAttribute("aria-label", state.size === "actual"
90
93
  ? (zh ? "适应容器宽度" : "Fit diagram to width")
91
94
  : (zh ? "按原始尺寸查看图表" : "View diagram at actual size"));
92
95
  const notice = block.querySelector(".diagram-notice");
93
- notice.hidden = !state.error && (state.rendered === state.source || mode === "source");
96
+ notice.hidden = !state.error && (ready || mode === "source");
94
97
  notice.textContent = state.error ? failed : loading;
95
98
  block.querySelectorAll("[data-diagram-mode]").forEach((button) => {
96
99
  button.setAttribute("aria-pressed", String(button.dataset.diagramMode === mode));
97
100
  });
98
101
  };
99
102
  const render = async (block, state, revision) => {
100
- const source = state.source;
103
+ const source = state.source, theme = workspaceTheme();
101
104
  let container;
102
105
  try {
103
106
  // Keep model-authored Mermaid config from weakening the renderer's safety policy.
@@ -109,7 +112,8 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
109
112
  container = document.createElement("div");
110
113
  container.style.cssText = "position:absolute;left:-100000px;top:0;width:1000px;visibility:hidden;pointer-events:none";
111
114
  document.body.appendChild(container);
112
- const result = await mermaid.render("agentbot-diagram-" + (++sequence), source, container);
115
+ const themedSource = theme ? "---\nconfig:\n theme: " + (theme === "dark" ? "dark" : "default") + "\n---\n" + source : source;
116
+ const result = await mermaid.render("agentbot-diagram-" + (++sequence), themedSource, container);
113
117
  if (!block.isConnected || state.revision !== revision) return;
114
118
  const template = document.createElement("template");
115
119
  template.innerHTML = result.svg;
@@ -128,6 +132,7 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
128
132
  }
129
133
  block.querySelector(".diagram-preview").replaceChildren(template.content);
130
134
  state.rendered = source;
135
+ state.renderedTheme = theme;
131
136
  state.error = false;
132
137
  show(block, state);
133
138
  } catch {
@@ -137,7 +142,7 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
137
142
  const schedule = (block, state) => {
138
143
  clearTimeout(state.timer);
139
144
  state.revision += 1;
140
- if (state.mode !== "preview" || state.rendered === state.source) return;
145
+ if (state.mode !== "preview" || isRendered(state)) return;
141
146
  const revision = state.revision;
142
147
  state.timer = setTimeout(() => void render(block, state, revision), 180);
143
148
  };
@@ -162,7 +167,7 @@ export const DIAGRAM_PREVIEW_CLIENT_SCRIPT = String.raw`(() => {
162
167
  schedule(block, state);
163
168
  });
164
169
  });
165
- } else if (state.source === source && (state.mode !== "preview" || state.rendered === source || state.error)) { return; }
170
+ } else if (state.source === source && (state.mode !== "preview" || isRendered(state) || state.error)) { return; }
166
171
  else { state.source = source; state.error = false; }
167
172
  show(block, state);
168
173
  schedule(block, state);
@@ -2,6 +2,7 @@ import { execFile } from "node:child_process";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
4
  import { promisify } from "node:util";
5
+ import { highlightPreviewCode, highlightTextLines, MAX_PREVIEW_HIGHLIGHT_BYTES, syntaxLanguageFor } from "./PreviewSyntaxHighlight.js";
5
6
 
6
7
  const runFile = promisify(execFile);
7
8
  const MAX_DIFF_BYTES = 1024 * 1024;
@@ -55,7 +56,37 @@ export async function readGitFileDiff(filePath: string): Promise<string | undefi
55
56
  }
56
57
  }
57
58
 
58
- type DiffLine = { kind: "context" | "addition" | "deletion"; text: string; oldNumber?: number; newNumber?: number };
59
+ type DiffLine = { kind: "context" | "addition" | "deletion"; text: string; oldNumber?: number; newNumber?: number; hunk?: string };
60
+
61
+ const PATCH_HUNK_START = /(?=^@@(?:[ \t]|$))/mu;
62
+ export const PATCH_HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?:.*)$/u;
63
+
64
+ export function highlightRecordedPatch(patch: string, filePath: string, originalPath: string): string | undefined {
65
+ if (Buffer.byteLength(patch, "utf8") > MAX_PREVIEW_HIGHLIGHT_BYTES) return undefined;
66
+ const normalized = patch.replace(/\r\n?/gu, "\n");
67
+ if (!PATCH_HUNK_START.test(normalized)) {
68
+ const language = normalized.startsWith("diff --git ") ? "diff" : syntaxLanguageFor(filePath) ?? "diff";
69
+ return highlightPreviewCode(normalized, language) || undefined;
70
+ }
71
+ return normalized.split(PATCH_HUNK_START).map((block) => {
72
+ if (!block.startsWith("@@")) return highlightPreviewCode(block, "diff") || escapeHtml(block);
73
+ const [header, ...lines] = block.split("\n");
74
+ // Each side gets its own syntax state, including context shared by both sides of the hunk.
75
+ const before = highlightTextLines(lines.filter((line) => /^[ -]/u.test(line)).map((line) => line.slice(1)).join("\n"), originalPath);
76
+ const after = highlightTextLines(lines.filter((line) => /^[ +]/u.test(line)).map((line) => line.slice(1)).join("\n"), filePath);
77
+ let oldLine = 0;
78
+ let newLine = 0;
79
+ const rows = lines.map((line) => {
80
+ switch (line[0]) {
81
+ case "-": return `<span class="hljs-deletion">-${before[oldLine++]}</span>`;
82
+ case "+": return `<span class="hljs-addition">+${after[newLine++]}</span>`;
83
+ case " ": oldLine++; return ` ${after[newLine++]}`;
84
+ default: return escapeHtml(line);
85
+ }
86
+ });
87
+ return [`<span class="hljs-meta">${escapeHtml(header!)}</span>`, ...rows].join("\n");
88
+ }).join("");
89
+ }
59
90
 
60
91
  export function renderGitFileDiff(diff: string, currentText: string, highlight?: (text: string) => string[]): string | undefined {
61
92
  const source = splitLines(currentText.replace(/^\uFEFF/u, ""));
@@ -78,7 +109,7 @@ export function renderGitFileDiff(diff: string, currentText: string, highlight?:
78
109
  // Git identifies edits; fill the gaps from the complete current file, checking hunk coordinates and text.
79
110
  for (let index = 1; index < lines.length; index += 1) {
80
111
  const line = lines[index]!;
81
- const hunk = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?:.*)$/u.exec(line);
112
+ const hunk = PATCH_HUNK_HEADER.exec(line);
82
113
  if (!hunk) {
83
114
  if (line === "\") continue;
84
115
  if (inHunks || line.startsWith("@@") || line.startsWith("diff --git ") || line === "GIT binary patch") return undefined;
@@ -107,17 +138,81 @@ export function renderGitFileDiff(diff: string, currentText: string, highlight?:
107
138
  while (newLine <= source.length) if (!append("context", source[newLine - 1]!)) return undefined;
108
139
  const before = highlight?.(rows.filter((row) => row.kind !== "addition").map((row) => row.text).join("\n"));
109
140
  const after = highlight?.(source.join("\n"));
110
- const content = rows.map((row) => {
141
+ const oldNumberWidth = Math.max(2, String(oldLine - 1).length);
142
+ const newNumberWidth = Math.max(2, String(newLine - 1).length);
143
+ return renderDiffRows(rows, oldNumberWidth, newNumberWidth, "Git diff", (row) =>
144
+ row.kind === "deletion" ? before?.[row.oldNumber! - 1] : after?.[row.newNumber! - 1]);
145
+ }
146
+
147
+ export function renderRecordedDiff(
148
+ patch: string, filePath: string, originalPath: string, additions?: number, deletions?: number,
149
+ ): string {
150
+ const label = "本次修改";
151
+ const normalized = patch.replace(/\r\n?/gu, "\n");
152
+ const rows: Array<DiffLine & { highlighted?: string }> = [];
153
+ if (PATCH_HUNK_START.test(normalized)) {
154
+ for (const block of normalized.split(PATCH_HUNK_START)) {
155
+ const [header, ...lines] = splitLines(block);
156
+ const range = header && PATCH_HUNK_HEADER.exec(header);
157
+ if (!range) continue;
158
+ let oldNumber = Number(range[1]), newNumber = Number(range[3]);
159
+ let oldRemaining = Number(range[2] ?? 1), newRemaining = Number(range[4] ?? 1);
160
+ if (![oldNumber, newNumber, oldRemaining, newRemaining].every(Number.isSafeInteger)) continue;
161
+ const hunk: DiffLine[] = [];
162
+ for (const line of lines) {
163
+ if (line === "\") continue;
164
+ const marker = line[0];
165
+ if (marker !== " " && marker !== "+" && marker !== "-") break;
166
+ if ((marker !== "+" && oldRemaining-- <= 0) || (marker !== "-" && newRemaining-- <= 0)) break;
167
+ hunk.push({ kind: marker === "+" ? "addition" : marker === "-" ? "deletion" : "context", text: line.slice(1),
168
+ oldNumber: marker === "+" ? undefined : oldNumber++, newNumber: marker === "-" ? undefined : newNumber++,
169
+ hunk: hunk.length ? undefined : header });
170
+ }
171
+ const before = highlightTextLines(hunk.filter((row) => row.kind !== "addition").map((row) => row.text).join("\n"), originalPath);
172
+ const after = highlightTextLines(hunk.filter((row) => row.kind !== "deletion").map((row) => row.text).join("\n"), filePath);
173
+ let oldIndex = 0, newIndex = 0;
174
+ for (const row of hunk) {
175
+ const left = row.kind === "addition" ? undefined : before[oldIndex++];
176
+ const right = row.kind === "deletion" ? undefined : after[newIndex++];
177
+ rows.push({ ...row, highlighted: right ?? left });
178
+ }
179
+ }
180
+ } else if (!/^diff --git |^--- [^\n]*\n\+\+\+ /u.test(normalized)) {
181
+ // Whole-file additions and deletions are recorded as file contents instead of hunks.
182
+ const kind = additions && deletions === 0 ? "addition" : deletions && additions === 0 ? "deletion" : undefined;
183
+ if (kind) {
184
+ const highlighted = highlightTextLines(normalized, kind === "addition" ? filePath : originalPath);
185
+ splitLines(normalized).forEach((text, index) => rows.push({ kind, text, highlighted: highlighted[index],
186
+ ...(kind === "addition" ? { newNumber: index + 1 } : { oldNumber: index + 1 }) }));
187
+ }
188
+ }
189
+ if (!rows.length) {
190
+ return `<pre class="code" aria-label="${label}"><code>${highlightRecordedPatch(patch, filePath, originalPath) ?? escapeHtml(patch)}</code></pre>`;
191
+ }
192
+ let oldWidth = 2, newWidth = 2;
193
+ for (const row of rows) {
194
+ oldWidth = Math.max(oldWidth, String(row.oldNumber ?? "").length);
195
+ newWidth = Math.max(newWidth, String(row.newNumber ?? "").length);
196
+ }
197
+ return renderDiffRows(rows, oldWidth, newWidth, label, (_row, index) => rows[index]?.highlighted);
198
+ }
199
+
200
+ function renderDiffRows(
201
+ rows: DiffLine[], oldNumberWidth: number, newNumberWidth: number, label: string,
202
+ highlight: (row: DiffLine, index: number) => string | undefined,
203
+ ): string {
204
+ const content = rows.map((row, index) => {
111
205
  const id = row.newNumber !== undefined ? `L${row.newNumber}` : `D${row.oldNumber}`;
112
206
  const oldNumber = `<span class="diff-number" aria-hidden="true">${row.oldNumber ?? ""}</span>`;
113
207
  const newNumber = row.newNumber !== undefined
114
208
  ? `<a class="diff-number" href="#${id}" aria-label="Line ${row.newNumber}">${row.newNumber}</a>`
115
209
  : '<span class="diff-number" aria-hidden="true"></span>';
116
210
  const marker = row.kind === "addition" ? "+" : row.kind === "deletion" ? "-" : " ";
117
- const text = row.kind === "deletion" ? before?.[row.oldNumber! - 1] : after?.[row.newNumber! - 1];
118
- return `<span class="line diff-${row.kind}" id="${id}">${oldNumber}${newNumber}<span class="diff-marker" aria-hidden="true">${marker}</span><span class="diff-text">${text ?? escapeHtml(row.text)}</span></span>`;
211
+ const text = highlight(row, index);
212
+ const header = row.hunk ? `<span class="line diff-hunk">${escapeHtml(row.hunk)}</span>` : "";
213
+ return `${header}<span class="line diff-${row.kind}" id="${id}">${oldNumber}${newNumber}<span class="diff-marker" aria-hidden="true">${marker}</span><span class="diff-text">${text ?? escapeHtml(row.text)}</span></span>`;
119
214
  }).join("");
120
- return `<pre class="code git-diff" aria-label="Git diff"><code>${content}</code></pre>`;
215
+ return `<pre class="code git-diff" aria-label="${label}" style="--old-line-number-width:${oldNumberWidth}ch;--new-line-number-width:${newNumberWidth}ch"><code>${content}</code></pre>`;
121
216
  }
122
217
 
123
218
  function splitLines(value: string): string[] {
@@ -130,15 +225,12 @@ function escapeHtml(value: string): string {
130
225
 
131
226
  export const GIT_DIFF_PREVIEW_CSS = `
132
227
  .git-diff .line { display:flex; width:max-content; min-width:100%; padding-right:16px; }
133
- .git-diff .diff-number { display:inline-block; flex:0 0 4.5em; width:4.5em; padding:0 10px; text-align:right; color:#6e7781; border-right:1px solid #d8dee4; text-decoration:none; user-select:none; }
228
+ .git-diff .diff-number { display:inline-block; flex:none; box-sizing:content-box; width:var(--new-line-number-width, 2ch); padding:0 6px; text-align:right; color:var(--git-diff-gutter-text, light-dark(#6e7781, #8b949e)); border-right:1px solid var(--git-diff-gutter-border, light-dark(#d8dee4, #3a3b3d)); text-decoration:none; user-select:none; }
229
+ .git-diff .diff-number:first-child { width:var(--old-line-number-width, 2ch); }
134
230
  .git-diff .diff-marker { flex:0 0 2em; width:2em; text-align:center; user-select:none; }
135
231
  .git-diff .diff-text { white-space:pre; }
136
- .git-diff .diff-addition { background:#dafbe1; color:#116329; }
137
- .git-diff .diff-deletion { background:#ffebe9; color:#a40e26; }
138
- @media (max-width:640px) { .git-diff .diff-number { flex-basis:3.5em; width:3.5em; padding:0 6px; } }
139
- @media (prefers-color-scheme:dark) {
140
- .git-diff .diff-number { color:#8b949e; border-color:#3a3b3d; }
141
- .git-diff .diff-addition { background:#033a16; color:#aff5b4; }
142
- .git-diff .diff-deletion { background:#67060c; color:#ffdcd7; }
143
- }
232
+ .git-diff .diff-hunk { padding-inline-start:6px; color:var(--git-diff-gutter-text, light-dark(#6e7781, #8b949e)); }
233
+ .git-diff .diff-addition { background:var(--git-diff-added-bg, light-dark(#dafbe1, #033a16)); color:var(--git-diff-added-text, light-dark(#116329, #aff5b4)); }
234
+ .git-diff .diff-deletion { background:var(--git-diff-removed-bg, light-dark(#ffebe9, #67060c)); color:var(--git-diff-removed-text, light-dark(#a40e26, #ffdcd7)); }
235
+ @media (max-width:640px) { .git-diff .diff-number { padding:0 4px; } }
144
236
  `;
@@ -0,0 +1,103 @@
1
+ export const LIVE_MARKUP_CLIENT_SCRIPT = `(() => {
2
+ const nodeKey = (node) => {
3
+ if (node.nodeType !== 1) return undefined;
4
+ for (const attribute of ["data-preview-key", "data-activity", "data-activity-id", "data-scroll-id", "data-reasoning-field", "data-reasoning-part"]) {
5
+ if (node.hasAttribute(attribute)) return attribute + ":" + node.getAttribute(attribute);
6
+ }
7
+ if (node.tagName === "IMG") return "image:" + node.getAttribute("src");
8
+ // Keep Markdown image paragraphs/links anchored when text is inserted before them.
9
+ if (["P", "A", "FIGURE", "PICTURE"].includes(node.tagName)) {
10
+ const image = node.querySelector("img");
11
+ if (image) return node.tagName + ":image:" + image.getAttribute("src");
12
+ }
13
+ for (const name of ["markdown", "tool-command", "tool-image", "files", "tool-header", "tool-body", "tool-footer"]) {
14
+ if (node.classList.contains(name)) return node.tagName + ":" + name;
15
+ }
16
+ return undefined;
17
+ };
18
+ const compatible = (current, next) => current.nodeType === next.nodeType && current.nodeName === next.nodeName;
19
+ const patchNode = (current, next) => {
20
+ if (!compatible(current, next)) {
21
+ current.replaceWith(next.cloneNode(true));
22
+ return;
23
+ }
24
+ // Summary snapshots never own a detail body that has already been fetched.
25
+ if (current.nodeType === 1 && current.hasAttribute("data-detail-body") && next.hasAttribute("data-detail-body")) return;
26
+ if (current.isEqualNode(next)) return;
27
+ if (current.nodeType !== 1) {
28
+ if (current.nodeValue !== next.nodeValue) current.nodeValue = next.nodeValue;
29
+ return;
30
+ }
31
+ if (current.hasAttribute("data-diagram") && next.hasAttribute("data-diagram")) {
32
+ const code = current.querySelector(".diagram-source code");
33
+ const nextCode = next.querySelector(".diagram-source code");
34
+ if (code && nextCode && code.textContent !== nextCode.textContent) {
35
+ code.textContent = nextCode.textContent;
36
+ window.agentBotDiagrams.refresh(current);
37
+ }
38
+ return;
39
+ }
40
+ const preserveOpen = current.tagName === "DETAILS";
41
+ for (const attribute of Array.from(current.attributes)) {
42
+ if (preserveOpen && attribute.name === "open") continue;
43
+ if (!next.hasAttribute(attribute.name)) current.removeAttribute(attribute.name);
44
+ }
45
+ for (const attribute of Array.from(next.attributes)) {
46
+ if (preserveOpen && attribute.name === "open") continue;
47
+ if (current.getAttribute(attribute.name) !== attribute.value) current.setAttribute(attribute.name, attribute.value);
48
+ }
49
+ patchChildren(current, next);
50
+ };
51
+ const patchChildren = (parent, next) => {
52
+ const nextChildren = Array.from(next.childNodes);
53
+ const nextKeys = new Set(nextChildren.map(nodeKey).filter((key) => key !== undefined));
54
+ const remaining = new Set(Array.from(parent.childNodes));
55
+ const keyed = new Map();
56
+ for (const child of remaining) {
57
+ const key = nodeKey(child);
58
+ if (key === undefined) continue;
59
+ if (!nextKeys.has(key)) {
60
+ child.remove();
61
+ remaining.delete(child);
62
+ continue;
63
+ }
64
+ const matches = keyed.get(key) || [];
65
+ matches.push(child);
66
+ keyed.set(key, matches);
67
+ }
68
+ let cursor = parent.firstChild;
69
+ for (const child of nextChildren) {
70
+ const key = nodeKey(child);
71
+ const matches = key === undefined ? undefined : keyed.get(key);
72
+ const current = key === undefined
73
+ ? cursor && nodeKey(cursor) === undefined && compatible(cursor, child) ? cursor : undefined
74
+ : matches?.find((candidate) => remaining.has(candidate) && compatible(candidate, child));
75
+ if (current) {
76
+ remaining.delete(current);
77
+ // Drop obsolete text/markup before a retained block without detaching that block.
78
+ while (cursor && cursor !== current && nodeKey(cursor) === undefined) {
79
+ const obsolete = cursor;
80
+ cursor = cursor.nextSibling;
81
+ remaining.delete(obsolete);
82
+ obsolete.remove();
83
+ }
84
+ if (current !== cursor) {
85
+ if (typeof parent.moveBefore === "function" && current.isConnected) parent.moveBefore(current, cursor);
86
+ else parent.insertBefore(current, cursor);
87
+ }
88
+ patchNode(current, child);
89
+ cursor = current.nextSibling;
90
+ } else {
91
+ const added = child.cloneNode(true);
92
+ parent.insertBefore(added, cursor);
93
+ }
94
+ }
95
+ for (const child of remaining) child.remove();
96
+ };
97
+ const patchMarkup = (element, markup) => {
98
+ const template = document.createElement("template");
99
+ template.innerHTML = markup;
100
+ patchChildren(element, template.content);
101
+ };
102
+ window.agentBotLiveMarkup = { nodeKey, patchMarkup, patchNode };
103
+ })();\n`;