@keyou007/agent-bot 0.1.27-alpha.2 → 0.1.27-alpha.4
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 +22 -0
- package/README.md +78 -13
- package/README.zh.md +78 -13
- package/assets/agent-bot.ico +0 -0
- package/assets/roboto-mono-OFL.txt +93 -0
- package/assets/roboto-mono-latin.woff2 +0 -0
- package/assets/workspace-drafts.js +88 -0
- package/assets/workspace-sessions.js +1935 -232
- package/assets/workspace.css +360 -35
- package/assets/workspace.js +2115 -249
- package/dist/cli/SelfUpdateObserver.js +124 -0
- package/dist/cli/SelfUpdateObserver.js.map +1 -0
- package/dist/cli/SelfUpdateRunner.js +267 -70
- package/dist/cli/SelfUpdateRunner.js.map +1 -1
- package/dist/cli/SelfUpdater.js +54 -15
- package/dist/cli/SelfUpdater.js.map +1 -1
- package/dist/cli/controlProtocol.js.map +1 -1
- package/dist/cli/help.js +6 -0
- package/dist/cli/help.js.map +1 -1
- package/dist/cli.js +85 -6
- package/dist/cli.js.map +1 -1
- package/dist/codex/CodexEventMapper.js +29 -5
- package/dist/codex/CodexEventMapper.js.map +1 -1
- package/dist/codex/CodexRuntime.js +3 -0
- package/dist/codex/CodexRuntime.js.map +1 -1
- package/dist/feishu/CardRenderer.js +45 -13
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/ConsoleFeishuClient.js +4 -0
- package/dist/feishu/ConsoleFeishuClient.js.map +1 -1
- package/dist/feishu/FeishuMarkdown.js +2 -14
- package/dist/feishu/FeishuMarkdown.js.map +1 -1
- package/dist/feishu/FeishuMessageClient.js +9 -0
- package/dist/feishu/FeishuMessageClient.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +26 -14
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/index.js +47 -12
- package/dist/index.js.map +1 -1
- package/dist/local-files/DiagramPreview.js +11 -6
- package/dist/local-files/DiagramPreview.js.map +1 -1
- package/dist/local-files/GitDiffPreview.js +40 -10
- package/dist/local-files/GitDiffPreview.js.map +1 -1
- package/dist/local-files/LiveMarkup.js +104 -0
- package/dist/local-files/LiveMarkup.js.map +1 -0
- package/dist/local-files/LocalFileViewerServer.js +219 -129
- package/dist/local-files/LocalFileViewerServer.js.map +1 -1
- package/dist/local-files/MarkdownExport.js +54 -0
- package/dist/local-files/MarkdownExport.js.map +1 -0
- package/dist/local-files/PreviewCodeFont.js +11 -0
- package/dist/local-files/PreviewCodeFont.js.map +1 -0
- package/dist/local-files/PreviewSyntaxHighlight.js +73 -0
- package/dist/local-files/PreviewSyntaxHighlight.js.map +1 -1
- package/dist/local-files/ToolCommandDisplay.js +111 -0
- package/dist/local-files/ToolCommandDisplay.js.map +1 -0
- package/dist/local-files/TurnPreviewPage.js +23 -269
- package/dist/local-files/TurnPreviewPage.js.map +1 -1
- package/dist/local-files/WorkspaceFileEvents.js +206 -0
- package/dist/local-files/WorkspaceFileEvents.js.map +1 -0
- package/dist/local-files/WorkspaceFiles.js +79 -3
- package/dist/local-files/WorkspaceFiles.js.map +1 -1
- package/dist/local-files/WorkspaceImages.js +15 -0
- package/dist/local-files/WorkspaceImages.js.map +1 -0
- package/dist/local-files/WorkspacePage.js +20 -5
- package/dist/local-files/WorkspacePage.js.map +1 -1
- package/dist/local-files/WorkspaceSessions.js +881 -79
- package/dist/local-files/WorkspaceSessions.js.map +1 -1
- package/dist/local-files/WorkspaceTimeline.js +105 -0
- package/dist/local-files/WorkspaceTimeline.js.map +1 -0
- package/dist/presentation/OutboundRouter.js +13 -6
- package/dist/presentation/OutboundRouter.js.map +1 -1
- package/dist/presentation/TurnStateReducer.js +28 -21
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +229 -92
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/proxy/TaskMetadata.js +86 -0
- package/dist/proxy/TaskMetadata.js.map +1 -0
- package/dist/scheduling/ScheduleCards.js +10 -4
- package/dist/scheduling/ScheduleCards.js.map +1 -1
- package/dist/scheduling/ScheduleCommand.js +4 -0
- package/dist/scheduling/ScheduleCommand.js.map +1 -1
- package/dist/scheduling/ScheduledTaskStore.js +51 -12
- package/dist/scheduling/ScheduledTaskStore.js.map +1 -1
- package/dist/scheduling/types.js +18 -4
- package/dist/scheduling/types.js.map +1 -1
- package/dist/state/StateStore.js +145 -19
- package/dist/state/StateStore.js.map +1 -1
- package/dist/state/TurnPreviewJournal.js +143 -36
- package/dist/state/TurnPreviewJournal.js.map +1 -1
- package/dist/state/TurnToolText.js +172 -0
- package/dist/state/TurnToolText.js.map +1 -0
- package/dist/state/migrations.js +4 -0
- package/dist/state/migrations.js.map +1 -1
- package/dist/supervision/SafeRestartNotifier.js +9 -6
- package/dist/supervision/SafeRestartNotifier.js.map +1 -1
- package/dist/terminal/TerminalPage.js +5 -0
- package/dist/terminal/TerminalPage.js.map +1 -1
- package/dist/updates/AutomaticUpdatePreparer.js +63 -12
- package/dist/updates/AutomaticUpdatePreparer.js.map +1 -1
- package/dist/updates/ManualUpdateController.js +7 -2
- package/dist/updates/ManualUpdateController.js.map +1 -1
- package/dist/utils/promptAttachments.js +20 -0
- package/dist/utils/promptAttachments.js.map +1 -0
- package/dist/utils/toolTimestamp.js +6 -0
- package/dist/utils/toolTimestamp.js.map +1 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +23 -2
- package/src/cli/SelfUpdateObserver.ts +111 -0
- package/src/cli/SelfUpdateRunner.ts +271 -85
- package/src/cli/SelfUpdater.ts +53 -16
- package/src/cli/controlProtocol.ts +1 -0
- package/src/cli/help.ts +6 -0
- package/src/cli.ts +71 -5
- package/src/codex/CodexEventMapper.ts +25 -5
- package/src/codex/CodexRuntime.ts +4 -0
- package/src/feishu/CardRenderer.ts +48 -13
- package/src/feishu/ConsoleFeishuClient.ts +6 -1
- package/src/feishu/FeishuMarkdown.ts +2 -14
- package/src/feishu/FeishuMessageClient.ts +10 -0
- package/src/feishu/FeishuTurnPresenter.ts +22 -8
- package/src/feishu/types.ts +1 -0
- package/src/index.ts +47 -12
- package/src/local-files/DiagramPreview.ts +11 -6
- package/src/local-files/GitDiffPreview.ts +38 -10
- package/src/local-files/LiveMarkup.ts +103 -0
- package/src/local-files/LocalFileViewerServer.ts +207 -132
- package/src/local-files/MarkdownExport.ts +54 -0
- package/src/local-files/PreviewCodeFont.ts +12 -0
- package/src/local-files/PreviewSyntaxHighlight.ts +75 -0
- package/src/local-files/ToolCommandDisplay.ts +104 -0
- package/src/local-files/TurnPreviewPage.ts +23 -254
- package/src/local-files/WorkspaceFileEvents.ts +186 -0
- package/src/local-files/WorkspaceFiles.ts +72 -5
- package/src/local-files/WorkspaceImages.ts +10 -0
- package/src/local-files/WorkspacePage.ts +20 -5
- package/src/local-files/WorkspaceSessions.ts +811 -82
- package/src/local-files/WorkspaceTimeline.ts +109 -0
- package/src/presentation/OutboundRouter.ts +15 -4
- package/src/presentation/TurnStateReducer.ts +27 -18
- package/src/presentation/turnViewTypes.ts +2 -0
- package/src/proxy/ProxySessionController.ts +226 -77
- package/src/proxy/TaskMetadata.ts +81 -0
- package/src/runtime/types.ts +2 -1
- package/src/scheduling/ScheduleCards.ts +10 -4
- package/src/scheduling/ScheduleCommand.ts +4 -0
- package/src/scheduling/ScheduledTaskStore.ts +54 -9
- package/src/scheduling/types.ts +24 -4
- package/src/state/StateStore.ts +157 -20
- package/src/state/TurnPreviewJournal.ts +138 -34
- package/src/state/TurnToolText.ts +156 -0
- package/src/state/migrations.ts +4 -0
- package/src/supervision/SafeRestartNotifier.ts +9 -6
- package/src/terminal/TerminalPage.ts +5 -0
- package/src/updates/AutomaticUpdatePreparer.ts +52 -11
- package/src/updates/ManualUpdateController.ts +8 -3
- package/src/utils/promptAttachments.ts +24 -0
- package/src/utils/toolTimestamp.ts +5 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TurnPreviewProjection, type TurnPreviewJournal } from "../state/TurnPreviewJournal.js";
|
|
2
|
-
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import http, { type IncomingMessage, type ServerResponse } from "node:http";
|
|
@@ -7,14 +7,14 @@ import type { Duplex } from "node:stream";
|
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import hljs from "highlight.js/lib/common";
|
|
10
|
-
import dos from "highlight.js/lib/languages/dos";
|
|
11
|
-
import dockerfile from "highlight.js/lib/languages/dockerfile";
|
|
12
|
-
import powershell from "highlight.js/lib/languages/powershell";
|
|
13
10
|
import MarkdownIt from "markdown-it";
|
|
14
11
|
import { CSV_PREVIEW_CSS, renderCsvPreview, csvSourcePreview } from "./CsvPreview.js";
|
|
15
12
|
import { GIT_DIFF_PREVIEW_CSS, readGitFileDiff, renderGitFileDiff } from "./GitDiffPreview.js";
|
|
16
|
-
import { renderWorkspacePage, WORKSPACE_SCRIPT } from "./WorkspacePage.js";
|
|
13
|
+
import { renderWorkspacePage, WORKSPACE_ICON, WORKSPACE_ICON_NAME, WORKSPACE_INSTANCE, WORKSPACE_INSTANCE_HEADER, WORKSPACE_REVISION, WORKSPACE_REVISION_HEADER, WORKSPACE_SCRIPT } from "./WorkspacePage.js";
|
|
14
|
+
import { PREVIEW_CODE_FONT, PREVIEW_CODE_FONT_NAME, previewCodeFontCss } from "./PreviewCodeFont.js";
|
|
15
|
+
import { highlightLines, highlightTextLines, MAX_SYNTAX_HIGHLIGHT_BYTES, syntaxLanguageFor } from "./PreviewSyntaxHighlight.js";
|
|
17
16
|
import { directWorkspaceOrigin } from "./WorkspaceOrigin.js";
|
|
17
|
+
import { WorkspaceFileEvents } from "./WorkspaceFileEvents.js";
|
|
18
18
|
import { readWebPageTitle } from "./WebPageTitle.js";
|
|
19
19
|
import { WorkspaceError, readWorkspaceAddress, readWorkspaceEntries, readWorkspaceGitStatus, readWorkspaceChanges, readWorkspaceFileVersions, createWorkspaceDiff, renderWorkspaceDiff, resolveWorkspacePath, type WorkspaceChange } from "./WorkspaceFiles.js";
|
|
20
20
|
import { enableDiagramFences, DIAGRAM_PREVIEW_CLIENT_SCRIPT, DIAGRAM_PREVIEW_CSS } from "./DiagramPreview.js";
|
|
@@ -42,21 +42,17 @@ const SHORT_TOKEN_BYTES = 12;
|
|
|
42
42
|
const SHORT_TOKEN_LENGTH = 16;
|
|
43
43
|
const MAX_TEXT_PREVIEW_BYTES = 2 * 1024 * 1024;
|
|
44
44
|
const MAX_CONTENT_SAMPLE_BYTES = 64 * 1024;
|
|
45
|
-
const MAX_SYNTAX_HIGHLIGHT_BYTES = 512 * 1024;
|
|
46
45
|
const DIRECTORY_PAGE_SIZE = 100;
|
|
47
46
|
const WORKSPACE_QUERY = z.object({
|
|
48
47
|
api: z.string().optional(), entry: z.string().default(""),
|
|
49
48
|
page: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
|
|
50
49
|
mode: z.enum(["all", "staged", "unstaged"]).default("all"), original: z.string().optional(),
|
|
51
50
|
prefetch: z.enum(["0", "1"]).optional(), query: z.string().trim().max(160).optional(),
|
|
51
|
+
refresh: z.enum(["1"]).optional(),
|
|
52
52
|
instance: z.string().regex(/^[a-f0-9]{16}$/u).optional(),
|
|
53
53
|
});
|
|
54
54
|
const MARKDOWN_EXTENSIONS = new Set([".markdown", ".md", ".mdown", ".mkd", ".mkdn"]);
|
|
55
55
|
const HTML_EXTENSIONS = new Set([".html", ".htm"]);
|
|
56
|
-
hljs.registerLanguage("dos", dos);
|
|
57
|
-
hljs.registerLanguage("dockerfile", dockerfile);
|
|
58
|
-
hljs.registerLanguage("powershell", powershell);
|
|
59
|
-
|
|
60
56
|
const MARKDOWN_RENDERER = new MarkdownIt({
|
|
61
57
|
breaks: false,
|
|
62
58
|
html: false,
|
|
@@ -73,29 +69,20 @@ const MARKDOWN_RENDERER = new MarkdownIt({
|
|
|
73
69
|
},
|
|
74
70
|
});
|
|
75
71
|
enableDiagramFences(MARKDOWN_RENDERER);
|
|
76
|
-
MARKDOWN_RENDERER.renderer.rules.table_open = () =>
|
|
77
|
-
'<div class="markdown-table-scroll" role="region" aria-label="Markdown 表格" tabindex="0"><table>\n';
|
|
72
|
+
MARKDOWN_RENDERER.renderer.rules.table_open = (tokens, index, _options, _env, renderer) =>
|
|
73
|
+
'<div class="markdown-table-scroll" role="region" aria-label="Markdown 表格" tabindex="0"><table' + renderer.renderAttrs(tokens[index]!) + '>\n';
|
|
78
74
|
MARKDOWN_RENDERER.renderer.rules.table_close = () => '</table></div>\n';
|
|
75
|
+
for (const kind of ["fence", "code_block"] as const) {
|
|
76
|
+
const render = MARKDOWN_RENDERER.renderer.rules[kind]!;
|
|
77
|
+
MARKDOWN_RENDERER.renderer.rules[kind] = (tokens, index, options, env, renderer) => {
|
|
78
|
+
const html = render(tokens, index, options, env, renderer), token = tokens[index]!;
|
|
79
|
+
const start = token.attrGet("data-source-start"), end = token.attrGet("data-source-end");
|
|
80
|
+
return start === null ? html : `<div data-source-start="${start}" data-source-end="${end}">${html}</div>\n`;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
79
83
|
const validateMarkdownLink = MARKDOWN_RENDERER.validateLink.bind(MARKDOWN_RENDERER);
|
|
80
84
|
MARKDOWN_RENDERER.validateLink = (url) => isFileUrl(url) || validateMarkdownLink(url);
|
|
81
85
|
|
|
82
|
-
const HIGHLIGHT_LANGUAGE_BY_EXTENSION: Readonly<Record<string, string>> = {
|
|
83
|
-
".bash": "bash", ".bat": "dos", ".c": "c", ".cc": "cpp", ".cmd": "dos", ".cpp": "cpp",
|
|
84
|
-
".cs": "csharp", ".css": "css", ".cxx": "cpp", ".diff": "diff", ".go": "go", ".h": "cpp",
|
|
85
|
-
".hpp": "cpp", ".htm": "xml", ".html": "xml", ".ini": "ini", ".ipynb": "json", ".java": "java",
|
|
86
|
-
".js": "javascript", ".json": "json", ".jsonl": "json", ".jsx": "javascript", ".kt": "kotlin",
|
|
87
|
-
".kts": "kotlin", ".less": "less", ".lua": "lua", ".markdown": "markdown", ".md": "markdown",
|
|
88
|
-
".mjs": "javascript", ".patch": "diff", ".php": "php", ".pl": "perl", ".ps1": "powershell",
|
|
89
|
-
".py": "python", ".r": "r", ".rb": "ruby", ".rs": "rust", ".scss": "scss", ".sh": "bash",
|
|
90
|
-
".sql": "sql", ".swift": "swift", ".toml": "ini", ".ts": "typescript", ".tsx": "typescript",
|
|
91
|
-
".vue": "xml", ".xml": "xml", ".yaml": "yaml", ".yml": "yaml", ".zsh": "bash",
|
|
92
|
-
};
|
|
93
|
-
const HIGHLIGHT_LANGUAGE_BY_FILENAME: Readonly<Record<string, string>> = {
|
|
94
|
-
".env": "ini",
|
|
95
|
-
".gitignore": "ini",
|
|
96
|
-
"dockerfile": "dockerfile",
|
|
97
|
-
"makefile": "makefile",
|
|
98
|
-
};
|
|
99
86
|
const VIEWER_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() => {
|
|
100
87
|
const eventsUrl = document.body.dataset.eventsUrl;
|
|
101
88
|
const content = document.getElementById("viewer-content");
|
|
@@ -103,6 +90,8 @@ const VIEWER_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() => {
|
|
|
103
90
|
const header = document.getElementById("viewer-header");
|
|
104
91
|
const title = document.getElementById("viewer-title");
|
|
105
92
|
const viewSwitch = document.getElementById("viewer-view-switch");
|
|
93
|
+
const diffPrevious = document.getElementById("diff-previous");
|
|
94
|
+
const diffNext = document.getElementById("diff-next");
|
|
106
95
|
const viewButtons = Array.from(document.querySelectorAll("[data-view-mode-button]"));
|
|
107
96
|
if (!content || !metadata || !header || !title) return;
|
|
108
97
|
const filePath = title.dataset.filePath || title.textContent || "";
|
|
@@ -138,35 +127,97 @@ const VIEWER_CLIENT_SCRIPT = DIAGRAM_PREVIEW_CLIENT_SCRIPT + `(() => {
|
|
|
138
127
|
selection.addRange(range);
|
|
139
128
|
});
|
|
140
129
|
|
|
130
|
+
const diffBlocks = [];
|
|
131
|
+
if (diffPrevious && diffNext) {
|
|
132
|
+
for (const line of content.querySelectorAll(".git-diff .diff-addition, .git-diff .diff-deletion")) {
|
|
133
|
+
if (!line.previousElementSibling?.matches(".diff-addition, .diff-deletion")) {
|
|
134
|
+
diffBlocks.push({ first: line, hash: "#diff-" + line.id });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
let diffPositions = [], selectedDiff = -1;
|
|
139
|
+
const updateDiffNavigation = () => {
|
|
140
|
+
let previous = -1, next = -1;
|
|
141
|
+
const top = window.scrollY;
|
|
142
|
+
// Adjacent blocks can share a scroll position near the ends of the file.
|
|
143
|
+
const current = Math.abs(diffPositions[selectedDiff] - top) <= 1
|
|
144
|
+
? selectedDiff : diffPositions.findIndex(position => Math.abs(position - top) <= 1);
|
|
145
|
+
if (current >= 0) {
|
|
146
|
+
previous = current - 1;
|
|
147
|
+
next = current + 1 < diffPositions.length ? current + 1 : -1;
|
|
148
|
+
} else {
|
|
149
|
+
for (let i = 0; i < diffPositions.length; i++) {
|
|
150
|
+
if (diffPositions[i] < top) previous = i;
|
|
151
|
+
else { next = i; break; }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (diffPrevious && diffNext) {
|
|
155
|
+
diffPrevious.disabled = previous < 0;
|
|
156
|
+
diffNext.disabled = next < 0;
|
|
157
|
+
}
|
|
158
|
+
return { previous, next };
|
|
159
|
+
};
|
|
160
|
+
const measureDiffPositions = () => {
|
|
161
|
+
if (!diffBlocks.length) return;
|
|
162
|
+
const height = window.innerHeight;
|
|
163
|
+
const readerCenter = (header.getBoundingClientRect().height + height) / 2;
|
|
164
|
+
const maxTop = Math.max(0, document.documentElement.scrollHeight - height);
|
|
165
|
+
diffPositions = diffBlocks.map(({ first }) => {
|
|
166
|
+
const line = first.getBoundingClientRect();
|
|
167
|
+
const center = (line.top + line.bottom) / 2;
|
|
168
|
+
return Math.min(maxTop, Math.max(0, window.scrollY + center - readerCenter));
|
|
169
|
+
});
|
|
170
|
+
updateDiffNavigation();
|
|
171
|
+
};
|
|
141
172
|
const updateHeaderOffset = () => {
|
|
142
173
|
const offset = Math.ceil(header.getBoundingClientRect().height) + 8;
|
|
143
174
|
document.documentElement.style.setProperty("--viewer-header-offset", offset + "px");
|
|
175
|
+
measureDiffPositions();
|
|
144
176
|
};
|
|
145
177
|
const highlightHashTarget = () => {
|
|
146
|
-
|
|
178
|
+
content.querySelector(".line.is-target-line")?.classList.remove("is-target-line");
|
|
179
|
+
const hash = window.location.hash;
|
|
180
|
+
selectedDiff = hash ? diffBlocks.findIndex(block => block.hash === hash) : (diffBlocks.length ? 0 : -1);
|
|
147
181
|
let id;
|
|
148
|
-
try {
|
|
149
|
-
id = decodeURIComponent(window.location.hash.slice(1));
|
|
150
|
-
} catch {
|
|
151
|
-
title.textContent = filePath;
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
182
|
+
try { id = decodeURIComponent(hash.slice(1)); } catch { id = ""; }
|
|
154
183
|
const lineNumber = /^L(\\d+)$/.exec(id)?.[1];
|
|
155
184
|
title.textContent = lineNumber ? filePath + ":" + lineNumber : filePath;
|
|
156
|
-
|
|
157
|
-
const target = document.getElementById(id);
|
|
185
|
+
const target = diffBlocks[selectedDiff]?.first ?? (lineNumber ? document.getElementById(id) : null);
|
|
158
186
|
target?.classList.add("is-target-line");
|
|
159
187
|
return target;
|
|
160
188
|
};
|
|
189
|
+
const centerDiffBlock = () => {
|
|
190
|
+
measureDiffPositions();
|
|
191
|
+
const top = diffPositions[selectedDiff];
|
|
192
|
+
if (top !== undefined) window.scrollTo(window.scrollX, top);
|
|
193
|
+
updateDiffNavigation();
|
|
194
|
+
};
|
|
161
195
|
const positionHashTarget = () => {
|
|
162
|
-
|
|
196
|
+
const target = highlightHashTarget();
|
|
197
|
+
if (window.location.hash && selectedDiff >= 0) centerDiffBlock();
|
|
198
|
+
else if (selectedDiff < 0) target?.scrollIntoView({ block: "start" });
|
|
199
|
+
if (diffBlocks.length) updateDiffNavigation();
|
|
163
200
|
};
|
|
164
201
|
const positionInitialContent = () => {
|
|
165
|
-
|
|
166
|
-
if (
|
|
167
|
-
|
|
168
|
-
|
|
202
|
+
if (!diffBlocks.length) return positionHashTarget();
|
|
203
|
+
if (["reload", "back_forward"].includes(performance.getEntriesByType("navigation")[0]?.type)) {
|
|
204
|
+
highlightHashTarget();
|
|
205
|
+
updateDiffNavigation();
|
|
206
|
+
} else if (window.location.hash) positionHashTarget();
|
|
207
|
+
else { highlightHashTarget(); centerDiffBlock(); }
|
|
169
208
|
};
|
|
209
|
+
const jumpDiff = (direction) => {
|
|
210
|
+
const index = updateDiffNavigation()[direction];
|
|
211
|
+
if (index < 0) return;
|
|
212
|
+
window.history.pushState(null, "", diffBlocks[index].hash);
|
|
213
|
+
positionHashTarget();
|
|
214
|
+
};
|
|
215
|
+
if (diffBlocks.length) {
|
|
216
|
+
diffPrevious.addEventListener("click", () => jumpDiff("previous"));
|
|
217
|
+
diffNext.addEventListener("click", () => jumpDiff("next"));
|
|
218
|
+
window.addEventListener("scroll", updateDiffNavigation, { passive: true });
|
|
219
|
+
window.addEventListener("resize", measureDiffPositions);
|
|
220
|
+
}
|
|
170
221
|
if (/^#L\\d+$/u.test(window.location.hash)) document.body.dataset.viewMode = "code";
|
|
171
222
|
syncViewMode();
|
|
172
223
|
updateHeaderOffset();
|
|
@@ -316,6 +367,7 @@ export class LocalFileViewerServer {
|
|
|
316
367
|
private address?: LocalFileViewerAddress;
|
|
317
368
|
private basePath = "";
|
|
318
369
|
private readonly services = new Map<string, LocalFileViewerService>();
|
|
370
|
+
private readonly workspaceFileEvents = new WorkspaceFileEvents();
|
|
319
371
|
private workspaceAppBase?: string;
|
|
320
372
|
|
|
321
373
|
enableWorkspaceApp(base: string): void { this.workspaceAppBase = base; }
|
|
@@ -403,6 +455,7 @@ export class LocalFileViewerServer {
|
|
|
403
455
|
async close(): Promise<void> {
|
|
404
456
|
await Promise.all([...this.services.values()].map((service) => service.close()));
|
|
405
457
|
this.services.clear();
|
|
458
|
+
this.workspaceFileEvents.close();
|
|
406
459
|
const server = this.server;
|
|
407
460
|
this.server = undefined;
|
|
408
461
|
this.address = undefined;
|
|
@@ -494,7 +547,20 @@ export class LocalFileViewerServer {
|
|
|
494
547
|
|
|
495
548
|
const requestUrl = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
496
549
|
const route = routeWithinBasePath(requestUrl.pathname, this.basePath);
|
|
550
|
+
const workspaceIcon = route === "/assets/" + WORKSPACE_ICON_NAME;
|
|
551
|
+
if (workspaceIcon || route === "/assets/" + PREVIEW_CODE_FONT_NAME) {
|
|
552
|
+
const asset = workspaceIcon ? WORKSPACE_ICON : PREVIEW_CODE_FONT;
|
|
553
|
+
setSecurityHeaders(response);
|
|
554
|
+
response.statusCode = 200;
|
|
555
|
+
response.setHeader("Content-Type", workspaceIcon ? "image/x-icon" : "font/woff2");
|
|
556
|
+
response.setHeader("Cache-Control", "public, max-age=86400");
|
|
557
|
+
response.setHeader("Content-Length", asset.length);
|
|
558
|
+
response.end(request.method === "HEAD" ? undefined : asset);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
497
561
|
if (route === "/assets/workspace.js") {
|
|
562
|
+
response.setHeader(WORKSPACE_REVISION_HEADER, WORKSPACE_REVISION);
|
|
563
|
+
response.setHeader(WORKSPACE_INSTANCE_HEADER, WORKSPACE_INSTANCE);
|
|
498
564
|
this.sendJavascript(response, request.method === "HEAD", WORKSPACE_SCRIPT);
|
|
499
565
|
return;
|
|
500
566
|
}
|
|
@@ -508,8 +574,7 @@ export class LocalFileViewerServer {
|
|
|
508
574
|
}
|
|
509
575
|
try { await this.handleWorkspace(request, response, requestUrl, root); }
|
|
510
576
|
catch (error) {
|
|
511
|
-
|
|
512
|
-
this.sendWorkspaceJson(response, error instanceof WorkspaceError ? error.status : code === "ENOENT" ? 404 : code === "EACCES" || code === "EPERM" ? 403 : 500,
|
|
577
|
+
this.sendWorkspaceJson(response, this.workspaceErrorStatus(error),
|
|
513
578
|
{ error: error instanceof Error ? error.message : String(error) }, request.method === "HEAD");
|
|
514
579
|
}
|
|
515
580
|
return;
|
|
@@ -551,7 +616,7 @@ export class LocalFileViewerServer {
|
|
|
551
616
|
}
|
|
552
617
|
const detailKey = requestUrl.searchParams.get("detail");
|
|
553
618
|
let snapshot = recorded
|
|
554
|
-
? journal!.
|
|
619
|
+
? await journal!.loadAsync(journalId, detailKey ?? false)
|
|
555
620
|
: this.options.getTurnSnapshot?.(turnId);
|
|
556
621
|
if (!recorded && this.options.loadTurnSnapshot && request.method !== "HEAD"
|
|
557
622
|
&& requestUrl.searchParams.get("events") !== "1" && !requestUrl.searchParams.has("detail")) {
|
|
@@ -646,7 +711,7 @@ export class LocalFileViewerServer {
|
|
|
646
711
|
actions.push({ href: original, label: "查看文件" }, { href: raw.toString(), label: "打开原始文件" }, { href: download.toString(), label: "下载" });
|
|
647
712
|
}
|
|
648
713
|
this.sendHtml(response, 200, renderViewerPage({ title: path.basename(filePath), filePath,
|
|
649
|
-
metadata: ["Git diff", "完整文件", "当前工作区"], actions, content,
|
|
714
|
+
metadata: ["Git diff", "完整文件", "当前工作区"], actions, content, gitDiff: true,
|
|
650
715
|
scriptPath: `${this.basePath}/assets/viewer.js`.replace(/\/{2,}/gu, "/") }), request.method === "HEAD");
|
|
651
716
|
return;
|
|
652
717
|
}
|
|
@@ -920,14 +985,16 @@ export class LocalFileViewerServer {
|
|
|
920
985
|
if (request.method === "HEAD") { response.end(); return; }
|
|
921
986
|
response.flushHeaders();
|
|
922
987
|
const language = detectTurnPreviewLanguage(request.headers["accept-language"]);
|
|
923
|
-
|
|
988
|
+
let projection = new TurnPreviewProjection(false);
|
|
924
989
|
let offset = 0;
|
|
925
990
|
let closed = false;
|
|
926
991
|
let initial = true;
|
|
927
992
|
const cursor = request.headers["last-event-id"] ?? new URL(request.url!, "http://localhost").searchParams.get("after");
|
|
928
993
|
if (typeof cursor === "string" && /^\d+$/u.test(cursor) && Number.isSafeInteger(Number(cursor)) && Number(cursor) > 0
|
|
929
994
|
&& Number(cursor) <= fs.statSync(journal.file(turnId)).size) {
|
|
930
|
-
|
|
995
|
+
const cached = journal.cachedProjection(turnId, Number(cursor));
|
|
996
|
+
if (cached) { projection = cached; offset = Number(cursor); }
|
|
997
|
+
while (!cached && offset < Number(cursor)) {
|
|
931
998
|
const batch = journal.read(turnId, offset, 256 * 1024, Number(cursor));
|
|
932
999
|
for (const record of batch.records) projection.apply(record, ++projection.revision);
|
|
933
1000
|
if (batch.offset === offset) break;
|
|
@@ -1032,32 +1099,77 @@ export class LocalFileViewerServer {
|
|
|
1032
1099
|
response.once("close", cleanup);
|
|
1033
1100
|
}
|
|
1034
1101
|
|
|
1035
|
-
private sendWorkspaceJson(response: ServerResponse, status: number, value: unknown, headOnly: boolean): void {
|
|
1102
|
+
private sendWorkspaceJson(response: ServerResponse, status: number, value: unknown, headOnly: boolean, request?: IncomingMessage): void {
|
|
1036
1103
|
const body = Buffer.from(JSON.stringify(value), "utf8");
|
|
1037
1104
|
setSecurityHeaders(response);
|
|
1105
|
+
if (request) {
|
|
1106
|
+
const etag = '"' + createHash("sha256").update(body).digest("base64url") + '"';
|
|
1107
|
+
response.setHeader("ETag", etag);
|
|
1108
|
+
if (request.headers["if-none-match"] === etag) {
|
|
1109
|
+
response.writeHead(304, { "Cache-Control": "no-store" }); response.end(); return;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1038
1112
|
response.writeHead(status, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store", "Content-Length": body.length });
|
|
1039
1113
|
response.end(headOnly ? undefined : body);
|
|
1040
1114
|
}
|
|
1041
1115
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1116
|
+
async serveTaskDirectory(request: IncomingMessage, response: ServerResponse, url: URL, root: string, base: URL): Promise<void> {
|
|
1117
|
+
try {
|
|
1118
|
+
await this.handleWorkspace(request, response, url, root, base);
|
|
1119
|
+
} catch (error) {
|
|
1120
|
+
if (response.headersSent) throw error;
|
|
1121
|
+
const status = this.workspaceErrorStatus(error);
|
|
1122
|
+
const message = status < 500 && error instanceof Error ? error.message : "暂时无法打开此目录,请重试。";
|
|
1123
|
+
if (url.searchParams.has("api")) this.sendWorkspaceJson(response, status, { error: message }, request.method === "HEAD");
|
|
1124
|
+
else this.sendHtml(response, status, errorPage("无法打开任务目录", message), request.method === "HEAD", "embedded");
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
private workspaceErrorStatus(error: unknown): number {
|
|
1129
|
+
if (error instanceof WorkspaceError) return error.status;
|
|
1130
|
+
const code = (error as NodeJS.ErrnoException)?.code;
|
|
1131
|
+
return code === "ENOENT" || code === "ENOTDIR" ? 404 : code === "EACCES" || code === "EPERM" ? 403 : 500;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
private async handleWorkspace(request: IncomingMessage, response: ServerResponse, url: URL, root: string, scopedBase?: URL): Promise<void> {
|
|
1135
|
+
if (!fs.statSync(resolveWorkspacePath(root)).isDirectory()) throw new WorkspaceError("目录已不可用。", 404);
|
|
1044
1136
|
const headOnly = request.method === "HEAD";
|
|
1045
1137
|
const published = new URL(this.address!.publicBaseUrl);
|
|
1046
|
-
const origin = request.headers.host === published.host ? published.origin : directWorkspaceOrigin(request);
|
|
1047
|
-
const workspaceUrl = this.createWorkspaceUrl(root, origin)!;
|
|
1048
|
-
if (!url.searchParams.
|
|
1049
|
-
this.sendHtml(response, 200, renderWorkspacePage(root, workspaceUrl, this.basePath + "/assets/workspace.js",
|
|
1138
|
+
const origin = scopedBase?.origin ?? (request.headers.host === published.host ? published.origin : directWorkspaceOrigin(request));
|
|
1139
|
+
const workspaceUrl = scopedBase?.href ?? this.createWorkspaceUrl(root, origin)!;
|
|
1140
|
+
if (!url.searchParams.get("api")) {
|
|
1141
|
+
this.sendHtml(response, 200, renderWorkspacePage(root, workspaceUrl, this.basePath + "/assets/workspace.js",
|
|
1142
|
+
scopedBase ? undefined : this.workspaceAppBase, Boolean(scopedBase)), headOnly, scopedBase ? "embedded" : true);
|
|
1050
1143
|
return;
|
|
1051
1144
|
}
|
|
1052
1145
|
const parsed = WORKSPACE_QUERY.safeParse(Object.fromEntries(url.searchParams));
|
|
1053
1146
|
if (!parsed.success) throw new WorkspaceError("工作台参数无效:" + parsed.error.issues.map((issue) => issue.message).join(";"));
|
|
1054
|
-
const { api, entry, page, mode, original, prefetch, query, instance } = parsed.data;
|
|
1055
|
-
if (
|
|
1056
|
-
|
|
1147
|
+
const { api, entry, page, mode, original, prefetch, query, instance, refresh } = parsed.data;
|
|
1148
|
+
if (scopedBase && (api === "terminal" || api === "web-title")) throw new WorkspaceError("该目录只支持文件浏览。", 404);
|
|
1149
|
+
if (api === "events") {
|
|
1150
|
+
if (request.headers["sec-fetch-site"] === "cross-site" || (request.headers.origin && request.headers.origin !== origin)) {
|
|
1151
|
+
throw new WorkspaceError("项目实时连接来源不受信任。", 403);
|
|
1152
|
+
}
|
|
1153
|
+
if (headOnly) this.sendWorkspaceJson(response, 200, {}, true);
|
|
1154
|
+
else this.workspaceFileEvents.subscribe(root, response);
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
if (api === "tree" || api === "changes") {
|
|
1158
|
+
if (refresh) this.workspaceFileEvents.invalidateGit(root);
|
|
1159
|
+
const read = api === "tree" ? readWorkspaceEntries : readWorkspaceChanges;
|
|
1160
|
+
const data = await read(root, entry, page, {
|
|
1161
|
+
prefetch: prefetch === "1" && request.headers["save-data"] !== "on", query, readGitStatus: () => this.workspaceFileEvents.gitStatus(root),
|
|
1162
|
+
});
|
|
1163
|
+
if (!headOnly && !query && page === 0) {
|
|
1164
|
+
this.workspaceFileEvents.watchDirectory(root, entry);
|
|
1165
|
+
if ("preloaded" in data && data.preloaded) for (const item of data.preloaded) if (item.directory) this.workspaceFileEvents.watchDirectory(root, item.path);
|
|
1166
|
+
}
|
|
1167
|
+
this.sendWorkspaceJson(response, 200, data, headOnly, request);
|
|
1057
1168
|
return;
|
|
1058
1169
|
}
|
|
1059
|
-
if (api === "
|
|
1060
|
-
|
|
1170
|
+
if (api === "changes-summary") {
|
|
1171
|
+
const { changes, repository, branch, notice } = await this.workspaceFileEvents.gitStatus(root);
|
|
1172
|
+
this.sendWorkspaceJson(response, 200, { totalChanges: changes.length, repository, branch, notice }, headOnly, request);
|
|
1061
1173
|
return;
|
|
1062
1174
|
}
|
|
1063
1175
|
if (api === "address") {
|
|
@@ -1068,10 +1180,6 @@ export class LocalFileViewerServer {
|
|
|
1068
1180
|
this.sendWorkspaceJson(response, 200, headOnly ? {} : await readWebPageTitle(entry), headOnly);
|
|
1069
1181
|
return;
|
|
1070
1182
|
}
|
|
1071
|
-
if (api === "changes") {
|
|
1072
|
-
this.sendWorkspaceJson(response, 200, await readWorkspaceChanges(root, entry, page, { prefetch: prefetch === "1", query }), headOnly);
|
|
1073
|
-
return;
|
|
1074
|
-
}
|
|
1075
1183
|
if (api === "terminal") {
|
|
1076
1184
|
if (!instance) throw new WorkspaceError("终端标签标识无效。");
|
|
1077
1185
|
if (!this.options.createWorkspaceTerminalUrl) throw new WorkspaceError("网页终端服务未启用。", 503);
|
|
@@ -1089,7 +1197,7 @@ export class LocalFileViewerServer {
|
|
|
1089
1197
|
const previous = decodeText(versions.before, before.encoding, true), current = decodeText(versions.after, after.encoding, true);
|
|
1090
1198
|
html = renderWorkspaceDiff(await createWorkspaceDiff(root, previous, current), current, (value) => highlightTextLines(value, file));
|
|
1091
1199
|
}
|
|
1092
|
-
this.sendWorkspaceJson(response, 200, { html, canViewFile: versions.canViewFile }, headOnly);
|
|
1200
|
+
this.sendWorkspaceJson(response, 200, { html, canViewFile: versions.canViewFile }, headOnly, request);
|
|
1093
1201
|
return;
|
|
1094
1202
|
}
|
|
1095
1203
|
if (api !== "file" && api !== "raw") throw new WorkspaceError("工作台接口不存在。", 404);
|
|
@@ -1123,11 +1231,17 @@ export class LocalFileViewerServer {
|
|
|
1123
1231
|
const normalize = (value: string) => process.platform === "win32" ? value.toLowerCase() : value;
|
|
1124
1232
|
change = (await readWorkspaceGitStatus(root)).changes.find(item => normalize(item.path) === normalize(relative));
|
|
1125
1233
|
} catch (error) { gitError = error instanceof Error ? error.message : String(error); }
|
|
1126
|
-
this.sendWorkspaceJson(response, 200, { html, rawUrl: raw.toString(), change, gitError }, headOnly);
|
|
1234
|
+
this.sendWorkspaceJson(response, 200, { html, rawUrl: raw.toString(), change, gitError }, headOnly, request);
|
|
1127
1235
|
}
|
|
1128
1236
|
|
|
1129
1237
|
private renderWorkspaceMarkdown(root: string, file: string, value: string, workspaceUrl: string): string {
|
|
1130
1238
|
const environment = {}, tokens = MARKDOWN_RENDERER.parse(value, environment), targets = new Map<string, string>();
|
|
1239
|
+
for (const token of tokens) {
|
|
1240
|
+
if (token.map && !token.hidden && token.tag && token.nesting >= 0) {
|
|
1241
|
+
token.attrSet("data-source-start", String(token.map[0] + 1));
|
|
1242
|
+
token.attrSet("data-source-end", String(token.map[1]));
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1131
1245
|
rewriteMarkdownFileLinks(tokens, file, (target) => {
|
|
1132
1246
|
const entry = path.relative(root, target).split(path.sep).join("/");
|
|
1133
1247
|
try {
|
|
@@ -1252,12 +1366,12 @@ export class LocalFileViewerServer {
|
|
|
1252
1366
|
stream.pipe(response);
|
|
1253
1367
|
}
|
|
1254
1368
|
|
|
1255
|
-
private sendHtml(response: ServerResponse, status: number, html: string, headOnly = false, workspace = false): void {
|
|
1369
|
+
private sendHtml(response: ServerResponse, status: number, html: string, headOnly = false, workspace: boolean | "embedded" = false): void {
|
|
1256
1370
|
const body = Buffer.from(html, "utf8");
|
|
1257
1371
|
setSecurityHeaders(response);
|
|
1258
1372
|
// External frame redirects must not expose privileged viewer documents.
|
|
1259
|
-
response.setHeader("X-Frame-Options", "DENY");
|
|
1260
|
-
if (workspace) response.setHeader("Content-Security-Policy", "default-src 'none'; img-src 'self' data:; media-src 'self'; object-src 'self'; frame-src 'self' http: https:; script-src 'self'; connect-src 'self'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'");
|
|
1373
|
+
response.setHeader("X-Frame-Options", workspace === "embedded" ? "SAMEORIGIN" : "DENY");
|
|
1374
|
+
if (workspace) response.setHeader("Content-Security-Policy", "default-src 'none'; img-src 'self' data: blob:; media-src 'self'; object-src 'self'; frame-src 'self' http: https:; script-src 'self'; connect-src 'self'; font-src 'self'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors " + (workspace === "embedded" ? "'self'" : "'none'"));
|
|
1261
1375
|
else response.setHeader("Content-Security-Policy", String(response.getHeader("Content-Security-Policy")).replace("frame-ancestors 'self'", "frame-ancestors 'none'"));
|
|
1262
1376
|
response.statusCode = status;
|
|
1263
1377
|
response.setHeader("Cache-Control", "no-store");
|
|
@@ -1283,6 +1397,7 @@ function renderViewerPage(input: {
|
|
|
1283
1397
|
metadata: string[];
|
|
1284
1398
|
actions: Array<{ href: string; label: string }>;
|
|
1285
1399
|
content: string;
|
|
1400
|
+
gitDiff?: boolean;
|
|
1286
1401
|
viewMode?: RenderedFileSnapshot["viewMode"];
|
|
1287
1402
|
diagramScriptPath?: string;
|
|
1288
1403
|
eventsUrl?: string;
|
|
@@ -1295,8 +1410,11 @@ function renderViewerPage(input: {
|
|
|
1295
1410
|
const viewSwitch = input.viewMode
|
|
1296
1411
|
? '<div class="view-switch" id="viewer-view-switch" role="tablist" aria-label="预览模式"><button class="is-active" type="button" role="tab" aria-selected="true" data-view-mode-button="rendered">预览</button><button type="button" role="tab" aria-selected="false" data-view-mode-button="code">代码</button></div>'
|
|
1297
1412
|
: "";
|
|
1298
|
-
const
|
|
1299
|
-
|
|
1413
|
+
const diffNavigation = input.gitDiff ? `<div class="diff-navigation" role="group" aria-label="Git Diff 导航">${[
|
|
1414
|
+
["previous", "上一处改动", "M12 20V4m-6 6 6-6 6 6"], ["next", "下一处改动", "M12 4v16m-6-6 6 6 6-6"],
|
|
1415
|
+
].map(([direction, label, arrow]) => `<button type="button" id="diff-${direction}" title="${label}" aria-label="${label}" disabled><svg aria-hidden="true" focusable="false" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="${arrow}"/></svg></button>`).join("")}</div>` : "";
|
|
1416
|
+
const toolbar = viewSwitch || actions || diffNavigation
|
|
1417
|
+
? `<div class="toolbar">${viewSwitch}${diffNavigation}${actions}</div>`
|
|
1300
1418
|
: "";
|
|
1301
1419
|
const liveAttributes = input.eventsUrl
|
|
1302
1420
|
? ` data-events-url="${escapeAttribute(input.eventsUrl)}"`
|
|
@@ -1310,8 +1428,8 @@ function renderViewerPage(input: {
|
|
|
1310
1428
|
<meta charset="utf-8">
|
|
1311
1429
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
1312
1430
|
<title>${escapeHtml(input.title)} · Agent Bot</title>
|
|
1313
|
-
<style>${input.diagramScriptPath ? DIAGRAM_PREVIEW_CSS : ""}
|
|
1314
|
-
:root { --viewer-header-offset: 56px;
|
|
1431
|
+
<style>${input.scriptPath ? previewCodeFontCss(input.scriptPath) : ""}${input.diagramScriptPath ? DIAGRAM_PREVIEW_CSS : ""}
|
|
1432
|
+
:root { --viewer-header-offset: 56px; color-scheme: light dark; font-family: Inter, "Segoe UI", system-ui, sans-serif; }
|
|
1315
1433
|
* { box-sizing: border-box; }
|
|
1316
1434
|
body { margin: 0; background: #f5f7fa; color: #1f2329; }
|
|
1317
1435
|
header { position: sticky; top: 0; z-index: 2; padding: 7px 12px; background: rgba(255,255,255,.96); border-bottom: 1px solid #dfe3e8; }
|
|
@@ -1321,6 +1439,12 @@ function renderViewerPage(input: {
|
|
|
1321
1439
|
.toolbar { margin-left: auto; display: flex; gap: 8px; align-items: center; }
|
|
1322
1440
|
.actions { display: flex; gap: 8px; }
|
|
1323
1441
|
.actions a { color: #1456f0; text-decoration: none; padding: 3px 7px; border: 1px solid #c9d0db; border-radius: 4px; background: #fff; }
|
|
1442
|
+
.diff-navigation { display: inline-flex; gap: 4px; }
|
|
1443
|
+
.diff-navigation button { display: grid; place-items: center; width: 28px; height: 28px; padding: 0; border: 1px solid #c9d0db; border-radius: 4px; background: transparent; color: inherit; cursor: pointer; }
|
|
1444
|
+
.diff-navigation button:disabled { opacity: .4; cursor: default; }
|
|
1445
|
+
.diff-navigation button:not(:disabled):hover { background: #f0f4ff; color: #1456f0; }
|
|
1446
|
+
.diff-navigation button:focus-visible { outline: 2px solid currentColor; outline-offset: 1px; }
|
|
1447
|
+
.diff-navigation svg { width: 16px; height: 16px; }
|
|
1324
1448
|
.view-switch { display: inline-flex; padding: 2px; border: 1px solid #c9d0db; border-radius: 5px; background: #f2f3f5; }
|
|
1325
1449
|
.view-switch[hidden] { display: none; }
|
|
1326
1450
|
.view-switch button { min-width: 46px; padding: 2px 8px; border: 0; border-radius: 3px; background: transparent; color: #646a73; cursor: pointer; font: inherit; }
|
|
@@ -1328,10 +1452,10 @@ function renderViewerPage(input: {
|
|
|
1328
1452
|
main { padding: 0; }
|
|
1329
1453
|
.notice, .unsupported { margin: 8px; padding: 10px 12px; border: 1px solid #f3cf8f; background: #fff7e6; border-radius: 5px; }
|
|
1330
1454
|
.code { margin: 0; padding: 6px 0; overflow: auto; border: 0; border-radius: 0; background: #fff; color: #1f2329; font-size: 13px; line-height: 1.5; tab-size: 2; }
|
|
1331
|
-
.code, .code code, .code .line, .code .line * { font-family: var(--
|
|
1455
|
+
.code, .code code, .code .line, .code .line * { font-family: var(--preview-code-font); font-variant-ligatures: none; }
|
|
1332
1456
|
.line { display: block; min-height: 1.55em; padding-right: 16px; scroll-margin-top: var(--viewer-header-offset); }
|
|
1333
1457
|
.line:target, .line.is-target-line { background: #fff1b8; box-shadow: inset 4px 0 #e6a700; }
|
|
1334
|
-
.line-number { display: inline-block; width:
|
|
1458
|
+
.line-number { display: inline-block; box-sizing: content-box; width: var(--line-number-width, 2ch); margin-right: 8px; padding: 0 6px; text-align: right; color: #8f959e; border-right: 1px solid #eceff3; text-decoration: none; user-select: none; }
|
|
1335
1459
|
.hljs-comment, .hljs-quote { color: #6a737d; font-style: italic; }
|
|
1336
1460
|
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-section, .hljs-link { color: #a626a4; }
|
|
1337
1461
|
.hljs-string, .hljs-regexp, .hljs-title, .hljs-name, .hljs-type, .hljs-attribute, .hljs-symbol, .hljs-bullet, .hljs-addition, .hljs-variable, .hljs-template-tag, .hljs-template-variable { color: #087f5b; }
|
|
@@ -1355,7 +1479,7 @@ function renderViewerPage(input: {
|
|
|
1355
1479
|
.markdown-body blockquote { margin-left: 0; padding: .15em 1em; border-left: 4px solid #c9d0db; color: #646a73; }
|
|
1356
1480
|
.markdown-body a { color: #1456f0; text-decoration: none; }
|
|
1357
1481
|
.markdown-body a:hover { text-decoration: underline; }
|
|
1358
|
-
.markdown-body code { padding: .14em .35em; border-radius: 4px; background: #f2f3f5; font-family: var(--
|
|
1482
|
+
.markdown-body code { padding: .14em .35em; border-radius: 4px; background: #f2f3f5; font-family: var(--preview-code-font); font-size: .9em; }
|
|
1359
1483
|
.markdown-body .markdown-code-block { margin: 0 0 1em; padding: 14px 16px; overflow: auto; border-radius: 6px; background: #f6f8fa; line-height: 1.55; }
|
|
1360
1484
|
.markdown-body .markdown-code-block code { padding: 0; background: transparent; font-size: 13px; }
|
|
1361
1485
|
.markdown-body .diagram-source { padding: 14px 16px; }
|
|
@@ -1393,6 +1517,8 @@ function renderViewerPage(input: {
|
|
|
1393
1517
|
.meta, .pagination { color: #a6a9ad; }
|
|
1394
1518
|
.actions a, .pagination a { color: #8ab4ff; }
|
|
1395
1519
|
.actions a { border-color: #55585c; background: #292a2d; }
|
|
1520
|
+
.diff-navigation button { border-color: #55585c; }
|
|
1521
|
+
.diff-navigation button:not(:disabled):hover { background: #292f3d; color: #8ab4ff; }
|
|
1396
1522
|
.view-switch { border-color: #55585c; background: #292a2d; }
|
|
1397
1523
|
.view-switch button { color: #a6a9ad; }
|
|
1398
1524
|
.view-switch button.is-active { background: #3a3b3d; color: #8ab4ff; box-shadow: none; }
|
|
@@ -1706,20 +1832,13 @@ function hasTextLikeCharacters(value: string): boolean {
|
|
|
1706
1832
|
function renderText(value: string, filePath: string): string {
|
|
1707
1833
|
const language = syntaxLanguageFor(filePath), lines = highlightTextLines(value, filePath);
|
|
1708
1834
|
const languageClass = language ? ` class="language-${escapeAttribute(language)}"` : "";
|
|
1709
|
-
|
|
1835
|
+
const numberWidth = Math.max(2, String(lines.length).length);
|
|
1836
|
+
return `<pre class="code hljs" style="--line-number-width:${numberWidth}ch"><code${languageClass}>${lines.map((line, index) => {
|
|
1710
1837
|
const lineNumber = index + 1;
|
|
1711
|
-
return `<span class="line" id="L${lineNumber}"><a class="line-number" href="#L${lineNumber}">${lineNumber}</a>${line}</span>`;
|
|
1838
|
+
return `<span class="line" id="L${lineNumber}"><a class="line-number" href="#L${lineNumber}">${lineNumber}</a><span class="line-text">${line}</span></span>`;
|
|
1712
1839
|
}).join("")}</code></pre>`;
|
|
1713
1840
|
}
|
|
1714
1841
|
|
|
1715
|
-
function highlightTextLines(value: string, filePath: string): string[] {
|
|
1716
|
-
const normalized = value.replace(/\r\n?/gu, "\n");
|
|
1717
|
-
const language = syntaxLanguageFor(filePath);
|
|
1718
|
-
return language && Buffer.byteLength(normalized, "utf8") <= MAX_SYNTAX_HIGHLIGHT_BYTES
|
|
1719
|
-
? highlightLines(normalized, language)
|
|
1720
|
-
: normalized.split("\n").map(escapeHtml);
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
1842
|
function renderMarkdownDocument(value: string, filePath: string, createPreviewUrl: (filePath: string) => URL): string {
|
|
1724
1843
|
const environment = {};
|
|
1725
1844
|
const tokens = MARKDOWN_RENDERER.parse(value, environment);
|
|
@@ -1732,50 +1851,6 @@ function isMarkdownFile(filePath: string): boolean {
|
|
|
1732
1851
|
return MARKDOWN_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
|
1733
1852
|
}
|
|
1734
1853
|
|
|
1735
|
-
function syntaxLanguageFor(filePath: string): string | undefined {
|
|
1736
|
-
if (isMarkdownFile(filePath)) return "markdown";
|
|
1737
|
-
const fileName = path.basename(filePath).toLowerCase();
|
|
1738
|
-
return Object.hasOwn(HIGHLIGHT_LANGUAGE_BY_FILENAME, fileName) ? HIGHLIGHT_LANGUAGE_BY_FILENAME[fileName]
|
|
1739
|
-
: HIGHLIGHT_LANGUAGE_BY_EXTENSION[path.extname(fileName)];
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1742
|
-
function highlightLines(value: string, language: string): string[] {
|
|
1743
|
-
try {
|
|
1744
|
-
return splitHighlightedLines(hljs.highlight(value, { language, ignoreIllegals: true }).value);
|
|
1745
|
-
} catch {
|
|
1746
|
-
return value.split("\n").map(escapeHtml);
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
|
|
1750
|
-
function splitHighlightedLines(value: string): string[] {
|
|
1751
|
-
const lines: string[] = [];
|
|
1752
|
-
const openTags: string[] = [];
|
|
1753
|
-
const tokenPattern = /<span class="[^"]+">|<\/span>|\n/gu;
|
|
1754
|
-
let current = "";
|
|
1755
|
-
let offset = 0;
|
|
1756
|
-
for (const match of value.matchAll(tokenPattern)) {
|
|
1757
|
-
const token = match[0];
|
|
1758
|
-
const index = match.index ?? offset;
|
|
1759
|
-
current += value.slice(offset, index);
|
|
1760
|
-
offset = index + token.length;
|
|
1761
|
-
if (token === "\n") {
|
|
1762
|
-
current += "</span>".repeat(openTags.length);
|
|
1763
|
-
lines.push(current);
|
|
1764
|
-
current = openTags.join("");
|
|
1765
|
-
} else if (token === "</span>") {
|
|
1766
|
-
current += token;
|
|
1767
|
-
openTags.pop();
|
|
1768
|
-
} else {
|
|
1769
|
-
current += token;
|
|
1770
|
-
openTags.push(token);
|
|
1771
|
-
}
|
|
1772
|
-
}
|
|
1773
|
-
current += value.slice(offset);
|
|
1774
|
-
current += "</span>".repeat(openTags.length);
|
|
1775
|
-
lines.push(current);
|
|
1776
|
-
return lines;
|
|
1777
|
-
}
|
|
1778
|
-
|
|
1779
1854
|
function contentTypeFor(extension: string): string {
|
|
1780
1855
|
const known: Record<string, string> = {
|
|
1781
1856
|
".aac": "audio/aac", ".avi": "video/x-msvideo", ".bmp": "image/bmp", ".css": "text/css; charset=utf-8",
|
|
@@ -1852,7 +1927,7 @@ function writeServerSentEvent(response: ServerResponse, event: string, data: str
|
|
|
1852
1927
|
}
|
|
1853
1928
|
|
|
1854
1929
|
function setSecurityHeaders(response: ServerResponse): void {
|
|
1855
|
-
response.setHeader("Content-Security-Policy", "default-src 'none'; img-src 'self' data:; media-src 'self'; object-src 'self'; frame-src 'self'; script-src 'self'; connect-src 'self'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'");
|
|
1930
|
+
response.setHeader("Content-Security-Policy", "default-src 'none'; img-src 'self' data:; media-src 'self'; object-src 'self'; frame-src 'self'; script-src 'self'; connect-src 'self'; font-src 'self'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'");
|
|
1856
1931
|
response.setHeader("Cross-Origin-Resource-Policy", "same-origin");
|
|
1857
1932
|
response.setHeader("Referrer-Policy", "no-referrer");
|
|
1858
1933
|
response.setHeader("X-Content-Type-Options", "nosniff");
|