Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
node-version: "24.22.2"
cache: npm
cache-dependency-path: web/package-lock.json

Expand All @@ -53,6 +53,10 @@ jobs:
run: npm run build
working-directory: web

- name: Verify paginated log retrieval
run: npm test
working-directory: web

- name: Set up Go
uses: actions/setup-go@v5
with:
Expand Down
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build && touch dist/.gitkeep",
"typecheck": "tsc -b"
"typecheck": "tsc -b",
"test": "node --experimental-transform-types --test tests/*.test.ts"
},
"dependencies": {
"@assistant-ui/react": "0.15.36",
Expand Down
11 changes: 10 additions & 1 deletion web/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ export const api = {
run: (id: string) => req<Run>("GET", `/runs/${encodeURIComponent(id)}`),
control: (id: string, action: "pause" | "resume" | "stop" | "retry" | "continue") =>
req<any>("POST", `/runs/${encodeURIComponent(id)}/${action}`),
logs: (id: string, after = 0) => req<LogEntry[]>("GET", `/runs/${encodeURIComponent(id)}/logs?after=${after}`),
logs: async (id: string, after = 0): Promise<LogEntry[]> => {
const entries: LogEntry[] = [];
const limit = 5000;
for (;;) {
const page = await req<LogEntry[]>("GET", `/runs/${encodeURIComponent(id)}/logs?after=${after}&limit=${limit}`);
entries.push(...page);
if (page.length < limit) return entries;
after = page[page.length - 1].seq;
}
},

tasks: () => req<Task[]>("GET", "/tasklib"),
task: (name: string) => req<Task>("GET", `/tasklib/${encodeURIComponent(name)}`),
Expand Down
25 changes: 16 additions & 9 deletions web/src/components/LogsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useMemo, useRef, useState } from "react";
import clsx from "clsx";
import { Download, X } from "lucide-react";
import { useStore, closeLogs } from "../store";
import { useStore, closeLogs, guard } from "../store";
import { api } from "../api";
import type { LogEntry } from "../types";

const sources = [
Expand Down Expand Up @@ -29,6 +30,7 @@ export function LogsPanel() {
const [step, setStep] = useState<string>(panel?.stepId ?? "");
const [query, setQuery] = useState("");
const [follow, setFollow] = useState(true);
const [downloading, setDownloading] = useState(false);
const ref = useRef<HTMLDivElement>(null);

useEffect(() => setStep(panel?.stepId ?? ""), [panel?.runId, panel?.stepId]);
Expand All @@ -49,13 +51,18 @@ export function LogsPanel() {

if (!panel) return null;

const download = () => {
const text = logs.map((l) => `${l.time} [${l.source}]${l.stepId ? ` [${l.stepId}]` : ""} ${l.text}`).join("\n");
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([text], { type: "text/plain" }));
a.download = `${panel.runId}.log`;
a.click();
URL.revokeObjectURL(a.href);
const download = async () => {
setDownloading(true);
try {
const entries = await guard(api.logs(panel.runId));
if (!entries) return;
const text = entries.map((l) => `${l.time} [${l.source}]${l.stepId ? ` [${l.stepId}]` : ""} ${l.text}`).join("\n");
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([text], { type: "text/plain" }));
a.download = `${panel.runId}.log`;
a.click();
URL.revokeObjectURL(a.href);
} finally { setDownloading(false); }
};

return (
Expand All @@ -68,7 +75,7 @@ export function LogsPanel() {
{run?.title} · <span className="font-mono">{panel.runId}</span>
</div>
</div>
<button className="btn-ghost ml-auto" onClick={download} title="下载日志">
<button className="btn-ghost ml-auto" onClick={download} disabled={downloading} title={downloading ? "正在下载完整日志…" : "下载日志"}>
<Download className="h-4 w-4" />
</button>
<button className="btn-ghost" onClick={closeLogs}>
Expand Down
3 changes: 2 additions & 1 deletion web/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ export async function loadRun(id: string) {

export async function loadLogs(runId: string) {
set((s) => ({ logs: { ...s.logs, [runId]: s.logs[runId] ?? [] } }));
const entries = await api.logs(runId);
const entries = await guard(api.logs(runId));
if (!entries) return;
set((s) => {
const live = (s.logs[runId] ?? []).filter((e) => !entries.length || e.seq > entries[entries.length - 1].seq);
return { logs: { ...s.logs, [runId]: [...entries, ...live] } };
Expand Down
40 changes: 40 additions & 0 deletions web/tests/logs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { once } from "node:events";
import test from "node:test";
import { api } from "../src/api.ts";

test("log client reads all HTTP pages and rejects incomplete history", async () => {
const requested: number[] = [];
let failTail = false;
const entries = Array.from({ length: 6001 }, (_, index) => ({ seq: index + 1, runId: "run", source: "host", time: "2026-11-17T00:00:00Z", text: `line ${index + 1}` }));
const server = createServer((request, response) => {
const url = new URL(request.url!, "http://fixture.test");
const after = Number(url.searchParams.get("after"));
requested.push(after);
response.setHeader("Content-Type", "application/json");
if (failTail && after > 0) {
response.writeHead(503);
response.end(JSON.stringify({ error: "tail unavailable" }));
} else response.end(JSON.stringify(entries.filter((entry) => entry.seq > after).slice(0, 5000)));
});
server.listen(0, "127.0.0.1");
await once(server, "listening");
const origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
const fetch = globalThis.fetch;
// Point the client's normal HTTP boundary at an isolated server.
globalThis.fetch = (input, init) => fetch(new URL(String(input), origin), init);
try {
const logs = await api.logs("run");
assert.equal(logs.length, 6001);
assert.equal(logs.at(-1)?.text, "line 6001");
assert.deepEqual(requested, [0, 5000]);
assert.equal((await api.logs("run", 6001)).length, 0);
failTail = true;
await assert.rejects(api.logs("run"), /tail unavailable/);
} finally {
globalThis.fetch = fetch;
server.closeAllConnections();
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
Loading