@keyou007/agent-bot 0.1.27-alpha.0 → 0.1.27-alpha.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/CHANGELOG.md +35 -0
- package/README.md +63 -7
- package/README.zh.md +62 -6
- package/SECURITY.md +18 -4
- package/assets/workspace-sessions.js +648 -0
- package/assets/workspace.css +160 -0
- package/assets/workspace.js +1795 -0
- package/config.example.yaml +3 -2
- package/dist/codex/CodexEventMapper.js +35 -0
- package/dist/codex/CodexEventMapper.js.map +1 -1
- package/dist/codex/CodexRuntime.js +50 -4
- package/dist/codex/CodexRuntime.js.map +1 -1
- package/dist/codex/TraeTaskNotification.js +44 -0
- package/dist/codex/TraeTaskNotification.js.map +1 -0
- package/dist/config/SettingsStore.js +1 -1
- package/dist/config/SettingsStore.js.map +1 -1
- package/dist/config/loadConfig.js +3 -2
- package/dist/config/loadConfig.js.map +1 -1
- package/dist/config/paths.js +3 -0
- package/dist/config/paths.js.map +1 -1
- package/dist/feishu/CardRenderer.js +56 -12
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/FeishuMessageClient.js +2 -2
- package/dist/feishu/FeishuMessageClient.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +103 -16
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/index.js +61 -17
- package/dist/index.js.map +1 -1
- package/dist/local-files/DiagramPreview.js +1 -1
- package/dist/local-files/GitDiffPreview.js +167 -0
- package/dist/local-files/GitDiffPreview.js.map +1 -0
- package/dist/local-files/LocalFileViewerServer.js +332 -30
- package/dist/local-files/LocalFileViewerServer.js.map +1 -1
- package/dist/local-files/MarkdownFileLinks.js +5 -4
- package/dist/local-files/MarkdownFileLinks.js.map +1 -1
- package/dist/local-files/TurnPreviewPage.js +138 -58
- package/dist/local-files/TurnPreviewPage.js.map +1 -1
- package/dist/local-files/WebPageTitle.js +85 -0
- package/dist/local-files/WebPageTitle.js.map +1 -0
- package/dist/local-files/WorkspaceFiles.js +553 -0
- package/dist/local-files/WorkspaceFiles.js.map +1 -0
- package/dist/local-files/WorkspaceOrigin.js +38 -0
- package/dist/local-files/WorkspaceOrigin.js.map +1 -0
- package/dist/local-files/WorkspacePage.js +19 -0
- package/dist/local-files/WorkspacePage.js.map +1 -0
- package/dist/local-files/WorkspaceSessions.js +689 -0
- package/dist/local-files/WorkspaceSessions.js.map +1 -0
- package/dist/presentation/TurnStateReducer.js +74 -13
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/presentation/turnActivity.js +41 -0
- package/dist/presentation/turnActivity.js.map +1 -0
- package/dist/presentation/turnStatus.js +11 -0
- package/dist/presentation/turnStatus.js.map +1 -1
- package/dist/presentation/visibleTurnActivities.js +4 -2
- package/dist/presentation/visibleTurnActivities.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +150 -14
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/runtime/types.js +1 -1
- package/dist/runtime/types.js.map +1 -1
- package/dist/state/InboundMessageStore.js +54 -0
- package/dist/state/InboundMessageStore.js.map +1 -0
- package/dist/state/StateStore.js +71 -1
- package/dist/state/StateStore.js.map +1 -1
- package/dist/state/TurnPreviewJournal.js +4 -2
- package/dist/state/TurnPreviewJournal.js.map +1 -1
- package/dist/state/migrations.js +20 -0
- package/dist/state/migrations.js.map +1 -1
- package/dist/supervision/SafeRestartNotifier.js +1 -0
- package/dist/supervision/SafeRestartNotifier.js.map +1 -1
- package/dist/supervision/SafeRestartScheduler.js +33 -16
- package/dist/supervision/SafeRestartScheduler.js.map +1 -1
- package/dist/terminal/TerminalLinks.js +32 -0
- package/dist/terminal/TerminalLinks.js.map +1 -1
- package/dist/terminal/TerminalPage.js +75 -26
- package/dist/terminal/TerminalPage.js.map +1 -1
- package/dist/terminal/TerminalServer.js +125 -56
- package/dist/terminal/TerminalServer.js.map +1 -1
- package/docs/technical-reference.md +14 -4
- package/docs/technical-reference.zh.md +15 -5
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +6 -2
- package/src/codex/CodexEventMapper.ts +39 -1
- package/src/codex/CodexRuntime.ts +54 -4
- package/src/codex/TraeTaskNotification.ts +49 -0
- package/src/config/SettingsStore.ts +1 -1
- package/src/config/loadConfig.ts +3 -2
- package/src/config/paths.ts +4 -0
- package/src/feishu/CardRenderer.ts +47 -13
- package/src/feishu/FeishuMessageClient.ts +2 -2
- package/src/feishu/FeishuTurnPresenter.ts +110 -20
- package/src/index.ts +62 -17
- package/src/local-files/DiagramPreview.ts +1 -1
- package/src/local-files/GitDiffPreview.ts +144 -0
- package/src/local-files/LocalFileViewerServer.ts +294 -31
- package/src/local-files/MarkdownFileLinks.ts +5 -3
- package/src/local-files/TurnPreviewPage.ts +159 -69
- package/src/local-files/WebPageTitle.ts +59 -0
- package/src/local-files/WorkspaceFiles.ts +468 -0
- package/src/local-files/WorkspaceOrigin.ts +31 -0
- package/src/local-files/WorkspacePage.ts +21 -0
- package/src/local-files/WorkspaceSessions.ts +587 -0
- package/src/presentation/TurnStateReducer.ts +76 -14
- package/src/presentation/turnActivity.ts +43 -0
- package/src/presentation/turnStatus.ts +14 -0
- package/src/presentation/turnViewTypes.ts +10 -1
- package/src/presentation/visibleTurnActivities.ts +4 -2
- package/src/proxy/ProxySessionController.ts +149 -13
- package/src/runtime/types.ts +17 -0
- package/src/state/InboundMessageStore.ts +72 -0
- package/src/state/StateStore.ts +80 -1
- package/src/state/TurnPreviewJournal.ts +4 -2
- package/src/state/migrations.ts +20 -0
- package/src/supervision/SafeRestartNotifier.ts +1 -0
- package/src/supervision/SafeRestartScheduler.ts +42 -18
- package/src/terminal/TerminalLinks.ts +26 -0
- package/src/terminal/TerminalPage.ts +76 -26
- package/src/terminal/TerminalServer.ts +119 -48
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { approvalActivityId } from "../presentation/TurnStateReducer.js";
|
|
3
|
-
import { isSettledTurnStatus } from "../presentation/turnStatus.js";
|
|
3
|
+
import { isSettledTurnStatus, modelQueueLabel } from "../presentation/turnStatus.js";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { enableDiagramFences, DIAGRAM_PREVIEW_CLIENT_SCRIPT, DIAGRAM_PREVIEW_CSS } from "./DiagramPreview.js";
|
|
6
6
|
import MarkdownIt from "markdown-it";
|
|
@@ -10,9 +10,12 @@ import type { FileSummary, TurnActivity, TurnReasoningItem, TurnViewState, TurnV
|
|
|
10
10
|
import { displayFilePath, displayToolCommand, formatShellCommandForDisplay, toolStatusIcon } from "../feishu/CardRenderer.js";
|
|
11
11
|
import { turnReasoningItems } from "../presentation/turnReasoning.js";
|
|
12
12
|
import { visibleTurnActivities } from "../presentation/visibleTurnActivities.js";
|
|
13
|
+
import { formatModelQueueDuration, liveModelQueueActivity, systemActivityQuote, USER_ACTIVITY_ICON } from "../presentation/turnActivity.js";
|
|
13
14
|
import { displayRuntimeError } from "../runtime/responseErrors.js";
|
|
14
15
|
import { isFileUrl, rewriteMarkdownFileLinks } from "./MarkdownFileLinks.js";
|
|
15
16
|
|
|
17
|
+
type PreviewFileUrl = (filePath: string, options?: { diff?: boolean }) => string | undefined;
|
|
18
|
+
|
|
16
19
|
const MARKDOWN = new MarkdownIt({
|
|
17
20
|
breaks: true,
|
|
18
21
|
html: false,
|
|
@@ -236,7 +239,9 @@ export const TURN_PREVIEW_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() =
|
|
|
236
239
|
});
|
|
237
240
|
|
|
238
241
|
let source;
|
|
242
|
+
let eventCursor;
|
|
239
243
|
const terminalAtLoad = document.body.dataset.terminal === "true";
|
|
244
|
+
let terminal = terminalAtLoad;
|
|
240
245
|
const updateElapsed = () => {
|
|
241
246
|
document.querySelectorAll("[data-live-elapsed]").forEach((element) => {
|
|
242
247
|
const startedAt = Number(element.dataset.startedAt);
|
|
@@ -268,9 +273,48 @@ export const TURN_PREVIEW_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() =
|
|
|
268
273
|
: remainder > 0 ? String(remainder) + "s" : "";
|
|
269
274
|
element.textContent = element.dataset.shortDuration === "1" ? shortDuration : labels.elapsed + duration;
|
|
270
275
|
});
|
|
276
|
+
document.querySelectorAll("[data-live-queue-duration]").forEach((element) => {
|
|
277
|
+
const startedAt = Number(element.dataset.startedAt);
|
|
278
|
+
if (!Number.isFinite(startedAt)) return;
|
|
279
|
+
const seconds = Math.max(0, Math.floor((Date.now() - startedAt) / 1000));
|
|
280
|
+
const hours = Math.floor(seconds / 3600);
|
|
281
|
+
const minutes = Math.floor((seconds % 3600) / 60);
|
|
282
|
+
const time = String(minutes).padStart(2, "0") + ":" + String(seconds % 60).padStart(2, "0");
|
|
283
|
+
element.textContent = hours > 0 ? String(hours).padStart(2, "0") + ":" + time : time;
|
|
284
|
+
});
|
|
271
285
|
};
|
|
272
286
|
updateElapsed();
|
|
273
|
-
|
|
287
|
+
let clock = terminalAtLoad ? undefined : setInterval(updateElapsed, 1000);
|
|
288
|
+
|
|
289
|
+
const historyKey = "agentBotTurnPreview";
|
|
290
|
+
const turnId = document.body.dataset.turnId;
|
|
291
|
+
const disclosureStates = new Map();
|
|
292
|
+
const restoredDisclosures = new WeakSet();
|
|
293
|
+
try {
|
|
294
|
+
const saved = window.history?.state?.[historyKey];
|
|
295
|
+
if (saved?.turnId === turnId && Array.isArray(saved.details)) {
|
|
296
|
+
for (const entry of saved.details) {
|
|
297
|
+
if (Array.isArray(entry) && typeof entry[0] === "string" && typeof entry[1] === "boolean") disclosureStates.set(entry[0], entry[1]);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
} catch {}
|
|
301
|
+
const saveDisclosures = () => {
|
|
302
|
+
for (const details of content.querySelectorAll("details[data-activity-id]")) {
|
|
303
|
+
disclosureStates.set(details.dataset.activityId, details.hasAttribute("open"));
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
window.history?.replaceState({ ...window.history.state,
|
|
307
|
+
[historyKey]: { turnId, details: Array.from(disclosureStates) } }, "");
|
|
308
|
+
} catch {}
|
|
309
|
+
};
|
|
310
|
+
const restoreDisclosures = () => {
|
|
311
|
+
for (const details of content.querySelectorAll("details[data-activity-id]")) {
|
|
312
|
+
if (restoredDisclosures.has(details)) continue;
|
|
313
|
+
restoredDisclosures.add(details);
|
|
314
|
+
const open = disclosureStates.get(details.dataset.activityId);
|
|
315
|
+
if (typeof open === "boolean") details.toggleAttribute("open", open);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
274
318
|
|
|
275
319
|
const nodeKey = (node) => {
|
|
276
320
|
if (node.nodeType !== 1) return undefined;
|
|
@@ -441,6 +485,7 @@ export const TURN_PREVIEW_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() =
|
|
|
441
485
|
};
|
|
442
486
|
content.addEventListener("toggle", (event) => {
|
|
443
487
|
const details = event.target;
|
|
488
|
+
if (details.matches?.("details[data-activity-id]")) saveDisclosures();
|
|
444
489
|
if (!details.matches?.("details[data-detail-key]")) return;
|
|
445
490
|
if (details.open) { void loadDetails(details, true); return; }
|
|
446
491
|
const state = detailState(details);
|
|
@@ -451,13 +496,22 @@ export const TURN_PREVIEW_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() =
|
|
|
451
496
|
if (message) message.textContent = "";
|
|
452
497
|
}, true);
|
|
453
498
|
const refreshDetails = () => {
|
|
499
|
+
restoreDisclosures();
|
|
454
500
|
for (const details of content.querySelectorAll("details[data-detail-key][open]")) void loadDetails(details);
|
|
455
501
|
};
|
|
456
502
|
window.addEventListener("pagehide", () => {
|
|
503
|
+
saveDisclosures();
|
|
457
504
|
source?.close();
|
|
505
|
+
source = undefined;
|
|
458
506
|
clearInterval(clock);
|
|
459
|
-
|
|
460
|
-
|
|
507
|
+
clock = undefined;
|
|
508
|
+
for (const details of content.querySelectorAll("details[data-detail-key]")) {
|
|
509
|
+
const state = detailStates.get(details);
|
|
510
|
+
state?.pending?.controller.abort();
|
|
511
|
+
if (state) state.pending = undefined;
|
|
512
|
+
details.querySelector("[data-detail-body]")?.removeAttribute("aria-busy");
|
|
513
|
+
}
|
|
514
|
+
});
|
|
461
515
|
let previousContent;
|
|
462
516
|
let previousMetadata;
|
|
463
517
|
const applySnapshot = (update) => {
|
|
@@ -533,38 +587,58 @@ export const TURN_PREVIEW_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() =
|
|
|
533
587
|
refreshDetails();
|
|
534
588
|
updateElapsed();
|
|
535
589
|
};
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
if (
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
590
|
+
const connect = () => {
|
|
591
|
+
if (terminal || source || typeof EventSource !== "function") return;
|
|
592
|
+
const resumeUrl = eventCursor ? new URL(eventsUrl, window.location.href) : undefined;
|
|
593
|
+
if (resumeUrl) resumeUrl.searchParams.set("after", eventCursor);
|
|
594
|
+
const connection = new EventSource(resumeUrl?.toString() ?? eventsUrl);
|
|
595
|
+
source = connection;
|
|
596
|
+
connection.addEventListener("patch", (event) => {
|
|
597
|
+
if (source !== connection) return;
|
|
598
|
+
try {
|
|
599
|
+
const update = JSON.parse(event.data);
|
|
600
|
+
applyPatch(update);
|
|
601
|
+
eventCursor = event.lastEventId || eventCursor;
|
|
602
|
+
live.textContent = update.terminal ? "" : labels.live;
|
|
603
|
+
live.className = update.terminal ? "live terminal" : "live connected";
|
|
604
|
+
if (update.terminal) { terminal = true; connection.close(); clearInterval(clock); }
|
|
605
|
+
} catch { live.textContent = labels.waitingForUpdates; }
|
|
606
|
+
});
|
|
607
|
+
connection.addEventListener("update", (event) => {
|
|
608
|
+
if (source !== connection) return;
|
|
609
|
+
try {
|
|
610
|
+
const update = JSON.parse(event.data);
|
|
611
|
+
if (typeof update.content !== "string" || typeof update.metadata !== "string") return;
|
|
612
|
+
applySnapshot(update);
|
|
613
|
+
eventCursor = event.lastEventId || eventCursor;
|
|
614
|
+
live.textContent = update.terminal ? "" : labels.live;
|
|
615
|
+
live.className = update.terminal ? "live terminal" : "live connected";
|
|
616
|
+
if (update.terminal) { terminal = true; connection.close(); clearInterval(clock); }
|
|
617
|
+
} catch {
|
|
618
|
+
live.textContent = labels.waitingForUpdates;
|
|
619
|
+
live.className = "live";
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
connection.addEventListener("unavailable", () => {
|
|
623
|
+
if (source !== connection) return;
|
|
624
|
+
live.textContent = labels.unavailable;
|
|
625
|
+
live.className = "live disconnected";
|
|
626
|
+
});
|
|
627
|
+
connection.onerror = () => {
|
|
628
|
+
if (source !== connection) return;
|
|
629
|
+
live.textContent = labels.reconnecting;
|
|
630
|
+
live.className = "live disconnected";
|
|
631
|
+
};
|
|
567
632
|
};
|
|
633
|
+
window.addEventListener("pageshow", (event) => {
|
|
634
|
+
if (!event.persisted) return;
|
|
635
|
+
updateElapsed();
|
|
636
|
+
refreshDetails();
|
|
637
|
+
if (!terminal && clock === undefined) clock = setInterval(updateElapsed, 1000);
|
|
638
|
+
connect();
|
|
639
|
+
});
|
|
640
|
+
refreshDetails();
|
|
641
|
+
connect();
|
|
568
642
|
})();
|
|
569
643
|
`;
|
|
570
644
|
|
|
@@ -590,11 +664,12 @@ export function isTurnPreviewState(value: unknown): value is TurnViewState {
|
|
|
590
664
|
}
|
|
591
665
|
|
|
592
666
|
export function renderTurnPreviewPage(input: {
|
|
667
|
+
workspaceUrl?: string;
|
|
593
668
|
state: TurnViewState;
|
|
594
669
|
eventsUrl: string;
|
|
595
670
|
scriptPath: string;
|
|
596
671
|
diagramScriptPath?: string;
|
|
597
|
-
localFileUrl?:
|
|
672
|
+
localFileUrl?: PreviewFileUrl;
|
|
598
673
|
language?: TurnPreviewLanguage;
|
|
599
674
|
}): string {
|
|
600
675
|
const language = input.language ?? "zh";
|
|
@@ -608,12 +683,13 @@ export function renderTurnPreviewPage(input: {
|
|
|
608
683
|
<title>${escapeHtml(title)} · Agent Bot</title>
|
|
609
684
|
<style>${TURN_PREVIEW_CSS}${DIAGRAM_PREVIEW_CSS}${PREVIEW_SYNTAX_CSS}</style>
|
|
610
685
|
</head>
|
|
611
|
-
<body data-terminal="${snapshot.terminal}" data-events-url="${escapeAttribute(input.eventsUrl)}" data-diagram-script="${escapeAttribute(input.diagramScriptPath ?? "")}">
|
|
686
|
+
<body data-turn-id="${escapeAttribute(input.state.turnId)}" data-terminal="${snapshot.terminal}" data-events-url="${escapeAttribute(input.eventsUrl)}" data-diagram-script="${escapeAttribute(input.diagramScriptPath ?? "")}">
|
|
612
687
|
<header class="page-header">
|
|
613
688
|
<div class="header-inner">
|
|
614
689
|
<div class="header-top">
|
|
615
690
|
<h1 title="Turn ${escapeAttribute(input.state.turnId)}">Turn Preview</h1>
|
|
616
691
|
<span id="turn-status" class="status ${snapshot.status}" role="status">${escapeHtml(snapshot.statusLabel)}</span>
|
|
692
|
+
${input.workspaceUrl ? '<a class="workspace-link" href="' + escapeAttribute(input.workspaceUrl) + '">Workspace</a>' : ""}
|
|
617
693
|
<span id="turn-live" class="live ${snapshot.terminal ? "terminal" : "connected"}" role="status">${snapshot.terminal ? "" : escapeHtml(previewLabels(language).live)}</span>
|
|
618
694
|
</div>
|
|
619
695
|
<div id="turn-metadata" class="metadata">${snapshot.metadata}</div>
|
|
@@ -627,7 +703,7 @@ export function renderTurnPreviewPage(input: {
|
|
|
627
703
|
|
|
628
704
|
export function renderTurnPreviewPatch(
|
|
629
705
|
state: TurnViewState, changed: Set<string>, removed: Set<string>,
|
|
630
|
-
localFileUrl:
|
|
706
|
+
localFileUrl: PreviewFileUrl, language: TurnPreviewLanguage,
|
|
631
707
|
): TurnPreviewSnapshot & { removed: string[]; before: Record<string, string> } {
|
|
632
708
|
const visible = visibleTurnActivities(state);
|
|
633
709
|
const visibleIds = new Set(visible.map((activity) => activity.id));
|
|
@@ -646,15 +722,15 @@ export function renderTurnPreviewPatch(
|
|
|
646
722
|
|
|
647
723
|
export function renderTurnPreviewSnapshot(
|
|
648
724
|
state: TurnViewState,
|
|
649
|
-
localFileUrl?:
|
|
725
|
+
localFileUrl?: PreviewFileUrl,
|
|
650
726
|
language: TurnPreviewLanguage = "zh",
|
|
651
727
|
options: { deferDetails?: boolean } = {},
|
|
652
728
|
): TurnPreviewSnapshot {
|
|
653
729
|
const labels = previewLabels(language);
|
|
654
|
-
const resolveFileUrl = localFileUrl ? (filePath
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
return localFileUrl(
|
|
730
|
+
const resolveFileUrl: PreviewFileUrl | undefined = localFileUrl ? (filePath, options) => {
|
|
731
|
+
const target = path.isAbsolute(filePath) || path.win32.isAbsolute(filePath) ? filePath
|
|
732
|
+
: state.projectCwd && path.isAbsolute(state.projectCwd) ? path.resolve(state.projectCwd, filePath) : undefined;
|
|
733
|
+
return target ? options ? localFileUrl(target, options) : localFileUrl(target) : undefined;
|
|
658
734
|
} : undefined;
|
|
659
735
|
const renderText = (value: string): string => renderMarkdown(value, state.projectCwd, localFileUrl, language);
|
|
660
736
|
const timeline = renderTimeline({ ...state, activities: visibleTurnActivities(state) }, renderText, resolveFileUrl, state.fullToolOutputs, state.fullToolErrors, language, state.projectCwd, options.deferDetails);
|
|
@@ -690,7 +766,7 @@ export function renderTurnPreviewSnapshot(
|
|
|
690
766
|
content: sections,
|
|
691
767
|
metadata: renderMetadata(state, language),
|
|
692
768
|
status: state.status,
|
|
693
|
-
statusLabel: statusLabel(state.status, language),
|
|
769
|
+
statusLabel: modelQueueLabel(state, language) ?? statusLabel(state.status, language),
|
|
694
770
|
terminal,
|
|
695
771
|
};
|
|
696
772
|
}
|
|
@@ -736,7 +812,7 @@ function renderPlan(state: TurnViewState, language: TurnPreviewLanguage): string
|
|
|
736
812
|
function renderTimeline(
|
|
737
813
|
state: TurnViewState,
|
|
738
814
|
renderText: (value: string) => string,
|
|
739
|
-
localFileUrl?:
|
|
815
|
+
localFileUrl?: PreviewFileUrl,
|
|
740
816
|
fullToolOutputs?: Record<string, string>,
|
|
741
817
|
fullToolErrors?: Record<string, string>,
|
|
742
818
|
language: TurnPreviewLanguage = "zh",
|
|
@@ -755,11 +831,13 @@ function renderTimeline(
|
|
|
755
831
|
}
|
|
756
832
|
const renderReasoning = (position: number): string => (reasoning.get(position) ?? [])
|
|
757
833
|
.map((item) => renderReasoningItem(item, state.projectCwd, localFileUrl, language, deferDetails)).join("");
|
|
834
|
+
const queue = liveModelQueueActivity(state);
|
|
758
835
|
return renderReasoning(-1) + activities.map((activity, index) => {
|
|
759
836
|
const visible = activity.kind === "reasoning"
|
|
760
837
|
? activity.id.startsWith("commentary:") ? { ...activity, kind: "assistant" as const } : undefined
|
|
761
838
|
: activity;
|
|
762
|
-
return (visible ? renderActivity(visible, renderText, localFileUrl, fullToolOutputs, fullToolErrors, language, projectCwd, deferDetails
|
|
839
|
+
return (visible ? renderActivity(visible, renderText, localFileUrl, fullToolOutputs, fullToolErrors, language, projectCwd, deferDetails,
|
|
840
|
+
visible.kind === "system" && visible.category === "model_queue" && visible.id === queue?.id ? queue : undefined) : "")
|
|
763
841
|
+ renderReasoning(index);
|
|
764
842
|
}).join("");
|
|
765
843
|
}
|
|
@@ -800,7 +878,7 @@ function leadingReasoningHeading(text: string): { title: string; body: string }
|
|
|
800
878
|
function renderReasoningItem(
|
|
801
879
|
item: TurnReasoningItem,
|
|
802
880
|
cwd: string | undefined,
|
|
803
|
-
localFileUrl:
|
|
881
|
+
localFileUrl: PreviewFileUrl | undefined,
|
|
804
882
|
language: TurnPreviewLanguage,
|
|
805
883
|
deferDetails: boolean,
|
|
806
884
|
): string {
|
|
@@ -819,7 +897,7 @@ function renderReasoningItem(
|
|
|
819
897
|
function renderReasoningBody(
|
|
820
898
|
item: TurnReasoningItem,
|
|
821
899
|
cwd: string | undefined,
|
|
822
|
-
localFileUrl:
|
|
900
|
+
localFileUrl: PreviewFileUrl | undefined,
|
|
823
901
|
language: TurnPreviewLanguage,
|
|
824
902
|
presentation = reasoningPresentation(item),
|
|
825
903
|
): string {
|
|
@@ -838,31 +916,40 @@ function reasoningRevision(item: TurnReasoningItem, cwd?: string): string {
|
|
|
838
916
|
function renderActivity(
|
|
839
917
|
activity: Exclude<TurnActivity, { kind: "reasoning" }>,
|
|
840
918
|
renderText: (value: string) => string,
|
|
841
|
-
localFileUrl?:
|
|
919
|
+
localFileUrl?: PreviewFileUrl,
|
|
842
920
|
fullToolOutputs?: Record<string, string>,
|
|
843
921
|
fullToolErrors?: Record<string, string>,
|
|
844
922
|
language: TurnPreviewLanguage = "zh",
|
|
845
923
|
projectCwd?: string,
|
|
846
924
|
deferDetails = false,
|
|
925
|
+
queue?: ReturnType<typeof liveModelQueueActivity>,
|
|
847
926
|
): string {
|
|
848
927
|
switch (activity.kind) {
|
|
928
|
+
case "system":
|
|
929
|
+
if (queue) {
|
|
930
|
+
const timer = `<span data-live-queue-duration data-started-at="${queue.startedAt}">${formatModelQueueDuration(Date.now() - queue.startedAt)}</span>`;
|
|
931
|
+
return `<article class="activity message system model_queue" data-activity="${escapeAttribute(activity.id)}"><div class="markdown"><blockquote><p>${escapeHtml(queue.label)} · ${language === "zh" ? "已等待" : "Waited"} ${timer}。</p></blockquote></div></article>`;
|
|
932
|
+
}
|
|
933
|
+
return renderMessageActivity(activity.id, systemActivityQuote(activity.text), `system ${activity.category}`, renderText);
|
|
849
934
|
case "assistant":
|
|
850
935
|
if (activity.id.startsWith("commentary:runtime-error:")) {
|
|
851
936
|
return `<article class="activity message runtime-error" data-activity="${escapeAttribute(activity.id)}"><pre>${escapeHtml(displayRuntimeError(activity.text))}</pre></article>`;
|
|
852
937
|
}
|
|
853
938
|
return renderMessageActivity(activity.id, activity.text, "commentary", renderText);
|
|
854
939
|
case "user":
|
|
855
|
-
return renderMessageActivity(activity.id, activity.text, "user", renderText,
|
|
940
|
+
return renderMessageActivity(activity.id, activity.text, "user", renderText,
|
|
941
|
+
renderMessageImages(activity.localImagePaths, localFileUrl, language), language === "zh" ? "用户补充" : "User message");
|
|
856
942
|
case "tool":
|
|
857
943
|
return renderToolActivity(activity.id, activity.tool, localFileUrl, fullToolOutputs, fullToolErrors, language, projectCwd, deferDetails);
|
|
858
944
|
}
|
|
859
945
|
}
|
|
860
946
|
|
|
861
|
-
function renderMessageActivity(id: string, text: string, kind: string, renderText: (value: string) => string, images = ""): string {
|
|
862
|
-
|
|
947
|
+
function renderMessageActivity(id: string, text: string, kind: string, renderText: (value: string) => string, images = "", userLabel?: string): string {
|
|
948
|
+
const icon = userLabel ? `<span class="message-icon" role="img" aria-label="${escapeAttribute(userLabel)}">${USER_ACTIVITY_ICON}</span>` : "";
|
|
949
|
+
return `<article class="activity message ${kind}${icon ? " with-icon" : ""}" data-activity="${escapeAttribute(id)}">${icon}<div class="markdown">${renderText(text)}${images}</div></article>`;
|
|
863
950
|
}
|
|
864
951
|
|
|
865
|
-
function renderMessageImages(paths: string[] | undefined, localFileUrl:
|
|
952
|
+
function renderMessageImages(paths: string[] | undefined, localFileUrl: PreviewFileUrl | undefined, language: TurnPreviewLanguage): string {
|
|
866
953
|
if (!paths?.length) return "";
|
|
867
954
|
return [...new Set(paths)].map((filePath, index) => {
|
|
868
955
|
const label = language === "zh" ? `图片 ${index + 1}` : `Image ${index + 1}`;
|
|
@@ -883,7 +970,7 @@ function renderMessageImages(paths: string[] | undefined, localFileUrl: ((filePa
|
|
|
883
970
|
function renderToolActivity(
|
|
884
971
|
activityId: string,
|
|
885
972
|
tool: ToolState,
|
|
886
|
-
localFileUrl?:
|
|
973
|
+
localFileUrl?: PreviewFileUrl,
|
|
887
974
|
fullToolOutputs?: Record<string, string>,
|
|
888
975
|
fullToolErrors?: Record<string, string>,
|
|
889
976
|
language: TurnPreviewLanguage = "zh",
|
|
@@ -916,7 +1003,7 @@ function renderToolActivity(
|
|
|
916
1003
|
function renderToolBody(
|
|
917
1004
|
activityId: string,
|
|
918
1005
|
tool: ToolState,
|
|
919
|
-
localFileUrl?:
|
|
1006
|
+
localFileUrl?: PreviewFileUrl,
|
|
920
1007
|
fullToolOutputs?: Record<string, string>,
|
|
921
1008
|
fullToolErrors?: Record<string, string>,
|
|
922
1009
|
language: TurnPreviewLanguage = "zh",
|
|
@@ -1109,7 +1196,7 @@ function formatShortDuration(duration: string): string {
|
|
|
1109
1196
|
|
|
1110
1197
|
function renderToolImage(
|
|
1111
1198
|
tool: ToolState,
|
|
1112
|
-
localFileUrl?:
|
|
1199
|
+
localFileUrl?: PreviewFileUrl,
|
|
1113
1200
|
language: TurnPreviewLanguage = "zh",
|
|
1114
1201
|
): string {
|
|
1115
1202
|
if (!tool.imagePath || !localFileUrl) return "";
|
|
@@ -1127,7 +1214,7 @@ function renderToolImage(
|
|
|
1127
1214
|
function renderFileSummary(
|
|
1128
1215
|
state: TurnViewState,
|
|
1129
1216
|
language: TurnPreviewLanguage,
|
|
1130
|
-
localFileUrl?:
|
|
1217
|
+
localFileUrl?: PreviewFileUrl,
|
|
1131
1218
|
deferDetails = false,
|
|
1132
1219
|
): string {
|
|
1133
1220
|
const labels = previewLabels(language);
|
|
@@ -1136,7 +1223,7 @@ function renderFileSummary(
|
|
|
1136
1223
|
return `<details class="files-summary" data-activity-id="turn:files-summary"${lazy}><summary><h2>${escapeHtml(labels.fileChanges)} <span>${state.fileSummary.length}</span></h2></summary>${body}</details>`;
|
|
1137
1224
|
}
|
|
1138
1225
|
|
|
1139
|
-
function renderFileSummaryBody(state: TurnViewState, localFileUrl?:
|
|
1226
|
+
function renderFileSummaryBody(state: TurnViewState, localFileUrl?: PreviewFileUrl): string {
|
|
1140
1227
|
return `<ul class="files">${state.fileSummary.map((file) => renderFileEntry(file, localFileUrl, state.projectCwd)).join("")}</ul>`;
|
|
1141
1228
|
}
|
|
1142
1229
|
|
|
@@ -1167,13 +1254,13 @@ export interface TurnPreviewDetail {
|
|
|
1167
1254
|
export function renderTurnPreviewDetail(
|
|
1168
1255
|
state: TurnViewState,
|
|
1169
1256
|
key: string,
|
|
1170
|
-
localFileUrl?:
|
|
1257
|
+
localFileUrl?: PreviewFileUrl,
|
|
1171
1258
|
language: TurnPreviewLanguage = "zh",
|
|
1172
1259
|
): TurnPreviewDetail | undefined {
|
|
1173
|
-
const resolveFileUrl = localFileUrl ? (filePath
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
return localFileUrl(
|
|
1260
|
+
const resolveFileUrl: PreviewFileUrl | undefined = localFileUrl ? (filePath, options) => {
|
|
1261
|
+
const target = path.isAbsolute(filePath) || path.win32.isAbsolute(filePath) ? filePath
|
|
1262
|
+
: state.projectCwd && path.isAbsolute(state.projectCwd) ? path.resolve(state.projectCwd, filePath) : undefined;
|
|
1263
|
+
return target ? options ? localFileUrl(target, options) : localFileUrl(target) : undefined;
|
|
1177
1264
|
} : undefined;
|
|
1178
1265
|
if (key === "files" && state.fileSummary.length > 0) {
|
|
1179
1266
|
return { key, revision: fileSummaryRevision(state), content: renderFileSummaryBody(state, resolveFileUrl) };
|
|
@@ -1195,9 +1282,9 @@ export function renderTurnPreviewDetail(
|
|
|
1195
1282
|
};
|
|
1196
1283
|
}
|
|
1197
1284
|
|
|
1198
|
-
function renderFileEntry(file: FileSummary, localFileUrl?:
|
|
1285
|
+
function renderFileEntry(file: FileSummary, localFileUrl?: PreviewFileUrl, projectCwd?: string): string {
|
|
1199
1286
|
const label = `<code>${escapeHtml(displayFilePath(file.path, projectCwd))}</code>`;
|
|
1200
|
-
const url = localFileUrl?.(file.path);
|
|
1287
|
+
const url = localFileUrl?.(file.path, { diff: true });
|
|
1201
1288
|
const link = url
|
|
1202
1289
|
? `<a class="file-link" href="${escapeAttribute(url)}" target="_blank" rel="noreferrer noopener">${label}</a>`
|
|
1203
1290
|
: label;
|
|
@@ -1238,7 +1325,7 @@ function previewTitle(state: TurnViewState): string {
|
|
|
1238
1325
|
return value.length > 80 ? `${value.slice(0, 79)}…` : value;
|
|
1239
1326
|
}
|
|
1240
1327
|
|
|
1241
|
-
function renderMarkdown(value: string, projectCwd?: string, localFileUrl?:
|
|
1328
|
+
function renderMarkdown(value: string, projectCwd?: string, localFileUrl?: PreviewFileUrl, language: TurnPreviewLanguage = "zh"): string {
|
|
1242
1329
|
const environment = { language };
|
|
1243
1330
|
const tokens = MARKDOWN.parse(value, environment);
|
|
1244
1331
|
// Only the directory is used to resolve relative links; no Markdown file is created.
|
|
@@ -1312,12 +1399,13 @@ body { margin: 0; background: var(--page); color: var(--text); font-size: 13px;
|
|
|
1312
1399
|
.page-header { position: sticky; top: 0; z-index: 3; background: var(--page); border-bottom: 1px solid var(--line); }
|
|
1313
1400
|
.header-inner { display: flex; flex-wrap: wrap; align-items: center; gap: 3px 12px; max-width: 1320px; margin: auto; padding: 6px 20px; }
|
|
1314
1401
|
.header-top { display: flex; flex-wrap: wrap; align-items: center; min-width: 0; max-width: 100%; gap: 2px 8px; }
|
|
1402
|
+
.workspace-link { display: inline-flex; align-items: center; gap: 8px; color: var(--accent); white-space: nowrap; font-size: 12px; text-decoration: none; }
|
|
1315
1403
|
h1 { flex: none; margin: 0; font-size: 14px; line-height: 20px; font-weight: 650; }
|
|
1316
1404
|
h2 { margin: 0 0 8px; font-size: 13px; font-weight: 650; }
|
|
1317
1405
|
h2 span { margin-left: 6px; color: var(--muted); font-size: 12px; font-weight: 400; }
|
|
1318
1406
|
.status, .live { flex: none; font-size: 12px; white-space: nowrap; }
|
|
1319
1407
|
.status { display: inline-flex; align-items: center; gap: 8px; }
|
|
1320
|
-
.status::before { content: "·"; color: var(--muted); }
|
|
1408
|
+
.status::before, .workspace-link::before { content: "·"; color: var(--muted); }
|
|
1321
1409
|
.status.starting, .status.running, .status.tool_running, .tool-state.running { color: var(--accent); }
|
|
1322
1410
|
.status.waiting_for_approval, .status.unknown { color: #926a08; }
|
|
1323
1411
|
.status.completed, .tool-state.completed { color: var(--success); }
|
|
@@ -1346,6 +1434,8 @@ main { max-width: 1320px; margin: 0 auto; padding: 0 20px 40px; }
|
|
|
1346
1434
|
.timeline { padding-top: 2px; }
|
|
1347
1435
|
.activity { min-width: 0; }
|
|
1348
1436
|
.message { padding: 12px 0; }
|
|
1437
|
+
.message.with-icon { display: grid; grid-template-columns: 20px minmax(0, 1fr); column-gap: 6px; }
|
|
1438
|
+
.message-icon { font-size: 14px; line-height: 1.6; }
|
|
1349
1439
|
.prompt { border-bottom: 1px solid var(--line); }
|
|
1350
1440
|
.timeline .markdown hr { display: none; }
|
|
1351
1441
|
.markdown { min-width: 0; overflow-wrap: anywhere; }
|
|
@@ -1364,7 +1454,7 @@ main { max-width: 1320px; margin: 0 auto; padding: 0 20px 40px; }
|
|
|
1364
1454
|
.markdown a { color: var(--accent); text-underline-offset: 2px; }
|
|
1365
1455
|
.markdown table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; }
|
|
1366
1456
|
.markdown th, .markdown td { padding: 5px 8px; border: 1px solid var(--line); }
|
|
1367
|
-
.markdown img { max-width: 100%; height: auto; }
|
|
1457
|
+
.markdown img, .tool-image img { width: auto; max-width: 100%; height: auto; max-height: min(720px, 60svh); }
|
|
1368
1458
|
code { padding: 1px 3px; border-radius: 3px; background: var(--surface); font: 12px/1.55 "Cascadia Mono", "SFMono-Regular", Consolas, monospace; overflow-wrap: anywhere; }
|
|
1369
1459
|
pre { max-width: 100%; max-height: 480px; margin: 0; padding: 9px 12px; overflow: auto; background: var(--code); color: var(--text); font: 12px/1.55 "Cascadia Mono", "SFMono-Regular", Consolas, monospace; tab-size: 2; white-space: pre; }
|
|
1370
1460
|
.markdown pre code { padding: 0; background: transparent; color: inherit; }
|
|
@@ -1434,7 +1524,7 @@ summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px;
|
|
|
1434
1524
|
.detail-message { padding: 8px 12px; color: var(--muted); }
|
|
1435
1525
|
.detail-message:empty { display: none; }
|
|
1436
1526
|
.detail-message button { margin-left: 8px; cursor: pointer; }
|
|
1437
|
-
.tool-image img { display: block;
|
|
1527
|
+
.tool-image img { display: block; }
|
|
1438
1528
|
.final-result h2 { color: var(--success); }
|
|
1439
1529
|
.approval-reason { white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
1440
1530
|
.approval-reason:empty { display: none; }
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { lookup } from "node:dns/promises";
|
|
2
|
+
import http, { type IncomingMessage } from "node:http";
|
|
3
|
+
import https from "node:https";
|
|
4
|
+
import { BlockList, isIP } from "node:net";
|
|
5
|
+
import MarkdownIt from "markdown-it";
|
|
6
|
+
import { WorkspaceError } from "./WorkspaceFiles.js";
|
|
7
|
+
|
|
8
|
+
const blocked = new BlockList(), globalV6 = new BlockList();
|
|
9
|
+
for (const [address, prefix] of [["0.0.0.0",8],["10.0.0.0",8],["100.64.0.0",10],["127.0.0.0",8],["169.254.0.0",16],["172.16.0.0",12],["192.0.0.0",24],["192.0.2.0",24],["192.88.99.0",24],["192.168.0.0",16],["198.18.0.0",15],["198.51.100.0",24],["203.0.113.0",24],["224.0.0.0",4],["240.0.0.0",4]] as const) blocked.addSubnet(address, prefix, "ipv4");
|
|
10
|
+
globalV6.addSubnet("2000::", 3, "ipv6");
|
|
11
|
+
for (const [address, prefix] of [["2001::",23],["2001:db8::",32],["2002::",16],["3fff::",20]] as const) blocked.addSubnet(address, prefix, "ipv6");
|
|
12
|
+
const unescape = new MarkdownIt().utils.unescapeAll;
|
|
13
|
+
const MAX_BYTES = 64 * 1024;
|
|
14
|
+
|
|
15
|
+
function publicAddress(address: string): boolean {
|
|
16
|
+
const family = isIP(address);
|
|
17
|
+
return family === 4 ? !blocked.check(address, "ipv4") : family === 6 && globalV6.check(address, "ipv6") && !blocked.check(address, "ipv6");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function readWebPageTitle(input: string): Promise<{ title?: string }> {
|
|
21
|
+
if (input.length > 4096) throw new WorkspaceError("网页地址过长。");
|
|
22
|
+
let url: URL;
|
|
23
|
+
try { url = new URL(input); } catch { throw new WorkspaceError("网页地址无效。"); }
|
|
24
|
+
const signal = AbortSignal.timeout(5000);
|
|
25
|
+
for (let redirects = 0; redirects <= 3; redirects++) {
|
|
26
|
+
if (!["http:","https:"].includes(url.protocol) || url.username || url.password || url.port) throw new WorkspaceError("标题读取仅支持不含账号密码、使用标准端口的公开 HTTP / HTTPS 网页。", 403);
|
|
27
|
+
const hostname = url.hostname.replace(/^\[|\]$/gu, "");
|
|
28
|
+
let abort!: () => void;
|
|
29
|
+
const addresses = await Promise.race([lookup(hostname, { all: true, verbatim: true }), new Promise<never>((_, reject) => { abort = () => reject(new WorkspaceError("读取网页标题超时。", 504)); signal.addEventListener("abort", abort, { once: true }); if (signal.aborted) abort(); })]).finally(() => signal.removeEventListener("abort", abort));
|
|
30
|
+
if (!addresses.length || addresses.some(item => !publicAddress(item.address))) throw new WorkspaceError("为保护本机和内网,不能从这个地址读取网页标题。", 403);
|
|
31
|
+
const address = addresses.find(item => item.family === 4) ?? addresses[0]!;
|
|
32
|
+
// Pin the validated DNS result so a second lookup cannot reach a private address.
|
|
33
|
+
const response = await new Promise<IncomingMessage>((resolve, reject) => {
|
|
34
|
+
const request = (url.protocol === "https:" ? https : http).get(url, { signal, family: address.family, lookup: (_host, _options, done) => done(null, address.address, address.family), headers: { Accept: "text/html,application/xhtml+xml" } }, resolve);
|
|
35
|
+
request.on("error", reject);
|
|
36
|
+
});
|
|
37
|
+
if (response.statusCode! >= 300 && response.statusCode! < 400 && response.headers.location) {
|
|
38
|
+
response.destroy();
|
|
39
|
+
if (redirects === 3) throw new WorkspaceError("网页重定向次数过多。", 502);
|
|
40
|
+
url = new URL(response.headers.location, url); continue;
|
|
41
|
+
}
|
|
42
|
+
if (response.statusCode! < 200 || response.statusCode! >= 300) { response.destroy(); throw new WorkspaceError("读取网页标题失败(HTTP " + response.statusCode + ")。", 502); }
|
|
43
|
+
const contentType = response.headers["content-type"] ?? "";
|
|
44
|
+
if (!/^(?:text\/html|application\/xhtml\+xml)(?:;|$)/iu.test(contentType)) { response.destroy(); return {}; }
|
|
45
|
+
const chunks: Buffer[] = []; let size = 0;
|
|
46
|
+
for await (const chunk of response) {
|
|
47
|
+
const data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array), remaining = MAX_BYTES - size;
|
|
48
|
+
chunks.push(data.subarray(0, remaining)); size += Math.min(data.length, remaining);
|
|
49
|
+
if (size === MAX_BYTES || /<\/title\s*>|<\/head\s*>/iu.test(Buffer.concat(chunks).toString("latin1"))) break;
|
|
50
|
+
}
|
|
51
|
+
const buffer = Buffer.concat(chunks), prefix = buffer.subarray(0, 1024).toString("latin1");
|
|
52
|
+
const encoding = /charset\s*=\s*["']?([^\s;"'>]+)/iu.exec(contentType)?.[1] ?? /<meta\b[^>]*charset\s*=\s*["']?([^\s;"'>]+)/iu.exec(prefix)?.[1] ?? "utf-8";
|
|
53
|
+
let decoder: TextDecoder; try { decoder = new TextDecoder(encoding); } catch { decoder = new TextDecoder("utf-8"); }
|
|
54
|
+
const html = decoder.decode(buffer).replace(/<!--[\s\S]*?-->|<(script|style)\b[^>]*>[\s\S]*?<\/\1\s*>/giu, ""), text = /<title\b[^>]*>([\s\S]*?)<\/title\s*>/iu.exec(html)?.[1];
|
|
55
|
+
const title = text && unescape(text.replaceAll("\\", "\")).replace(/[\u0000-\u001f\u007f]/gu, " " ).replace(/\s+/gu, " ").trim().slice(0, 200);
|
|
56
|
+
return title ? { title } : {};
|
|
57
|
+
}
|
|
58
|
+
return {};
|
|
59
|
+
}
|