mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-25 18:11:50 +08:00
feat(tasks): add read-only task file browser (GITHUB-3)
This commit is contained in:
1 parent
873e3a667b
commit
62ed7db7b2
14 files changed
+1567
-21
No files matched your search
@@ -0,0 +1,366 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
|
||||
import {
|
||||
ChevronDownIcon,
|
||||
ChevronRightIcon,
|
||||
CopyIcon,
|
||||
DownloadIcon,
|
||||
FileIcon,
|
||||
FolderIcon,
|
||||
RefreshCwIcon,
|
||||
} from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { PacketCodeEditor } from "@/components/packet-code-editor";
|
||||
import { StatusBadge } from "@/components/status-badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { useUITranslation } from "@/i18n/use-ui-translation";
|
||||
import { api } from "@/lib/api";
|
||||
import type { Task, TaskFileEntry, TaskFileListing, WorkspaceFile } from "@/lib/types";
|
||||
|
||||
function fileSize(n: number) {
|
||||
if (n < 1024) return `${n} B`;
|
||||
if (n < 1024 ** 2) return `${(n / 1024).toFixed(1)} KiB`;
|
||||
return `${(n / 1024 ** 2).toFixed(1)} MiB`;
|
||||
}
|
||||
const isImage = (path: string) => /\.(png|jpe?g|webp|gif)$/i.test(path);
|
||||
|
||||
export function FilesTab({ task, active }: { task: Task; active: boolean }) {
|
||||
const { ui, liveUI, i18n } = useUITranslation();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const requestedPath = searchParams.get("path") ?? "";
|
||||
const [mode, setMode] = React.useState<"tree" | "recent">("tree");
|
||||
const [listings, setListings] = React.useState<Record<string, TaskFileEntry[]>>({});
|
||||
const [expanded, setExpanded] = React.useState<string[]>([]);
|
||||
const [summary, setSummary] = React.useState<TaskFileListing | null>(null);
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
const [error, setError] = React.useState("");
|
||||
const [preview, setPreview] = React.useState<{ path: string; file?: WorkspaceFile; error?: string } | null>(null);
|
||||
const generation = React.useRef(0);
|
||||
const previewGeneration = React.useRef(0);
|
||||
|
||||
const message = React.useCallback(
|
||||
(err: unknown) => {
|
||||
const raw = (err as Error).message;
|
||||
return raw.startsWith("taskFiles.") ? liveUI(raw) : liveUI("taskFiles.failed");
|
||||
},
|
||||
[liveUI],
|
||||
);
|
||||
|
||||
const refresh = React.useCallback(async () => {
|
||||
const seq = ++generation.current;
|
||||
setLoading(true);
|
||||
try {
|
||||
const [all, ...dirs] = await Promise.all([
|
||||
api.taskFilesAll(task.id),
|
||||
...["", ...expanded].map(async (path) => {
|
||||
try {
|
||||
return { path, result: await api.taskFiles(task.id, path) };
|
||||
} catch (err) {
|
||||
if (path === "") throw err;
|
||||
return { path, result: null };
|
||||
}
|
||||
}),
|
||||
]);
|
||||
if (generation.current !== seq) return;
|
||||
setSummary(all);
|
||||
setListings(Object.fromEntries(dirs.filter((d) => d.result).map((d) => [d.path, d.result?.entries ?? []])));
|
||||
setError(dirs.some((d) => !d.result) ? liveUI("taskFiles.pathGone") : "");
|
||||
} catch (err) {
|
||||
if (generation.current === seq) setError(message(err));
|
||||
} finally {
|
||||
if (generation.current === seq) setLoading(false);
|
||||
}
|
||||
}, [task.id, expanded, liveUI, message]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!active) return;
|
||||
void refresh();
|
||||
return () => {
|
||||
generation.current++;
|
||||
};
|
||||
}, [active, refresh]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!active || task.status !== "running" || task.paused) return;
|
||||
const timer = setInterval(() => {
|
||||
if (document.visibilityState === "visible") void refresh();
|
||||
}, 5000);
|
||||
return () => clearInterval(timer);
|
||||
}, [active, task.status, task.paused, refresh]);
|
||||
|
||||
const openPreview = React.useCallback(
|
||||
async (path: string) => {
|
||||
const seq = ++previewGeneration.current;
|
||||
setPreview({ path });
|
||||
if (isImage(path)) return;
|
||||
try {
|
||||
const file = await api.taskFileRead(task.id, path);
|
||||
if (previewGeneration.current === seq) setPreview({ path, file });
|
||||
} catch (err) {
|
||||
if (previewGeneration.current === seq) setPreview({ path, error: message(err) });
|
||||
}
|
||||
},
|
||||
[task.id, message],
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!active || !requestedPath) return;
|
||||
let alive = true;
|
||||
const parts = requestedPath.split("/");
|
||||
const ancestors = parts.slice(0, -1).map((_, i) => parts.slice(0, i + 1).join("/"));
|
||||
void api
|
||||
.taskFiles(task.id, requestedPath)
|
||||
.then(() => {
|
||||
if (alive) setExpanded((prev) => Array.from(new Set([...prev, ...ancestors, requestedPath])));
|
||||
})
|
||||
.catch(() => {
|
||||
if (!alive) return;
|
||||
setExpanded((prev) => Array.from(new Set([...prev, ...ancestors])));
|
||||
void openPreview(requestedPath);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [active, task.id, requestedPath, openPreview]);
|
||||
|
||||
const locate = (path: string) => {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("tab", "files");
|
||||
if (path) params.set("path", path);
|
||||
else params.delete("path");
|
||||
router.replace(`/function/tasks/detail?${params}`, { scroll: false });
|
||||
};
|
||||
const workspacePath = (path: string) => `tasks/${task.id}${path ? `/${path}` : ""}`;
|
||||
const download = (path: string, archive = false) => {
|
||||
void api.taskFileDownload(task.id, path, archive).catch((err: unknown) => toast.error(message(err)));
|
||||
};
|
||||
const name = (entry: TaskFileEntry) => {
|
||||
if (entry.intent_id) return ui("taskFiles.intent", { id: entry.intent_id, title: entry.intent_title ?? "" });
|
||||
const key: Record<string, string> = {
|
||||
uploads: "uploads",
|
||||
"cmd-output": "commands",
|
||||
"findings/screenshots": "screenshots",
|
||||
};
|
||||
return key[entry.path] ? ui(`taskFiles.${key[entry.path]}`) : entry.name;
|
||||
};
|
||||
const rows: { entry: TaskFileEntry; depth: number }[] = [];
|
||||
const append = (path: string, depth: number) => {
|
||||
for (const entry of listings[path] ?? []) {
|
||||
rows.push({ entry, depth });
|
||||
if (entry.dir && expanded.includes(entry.path)) append(entry.path, depth + 1);
|
||||
}
|
||||
};
|
||||
if (mode === "tree") append("", 0);
|
||||
else for (const entry of summary?.entries ?? []) rows.push({ entry, depth: 0 });
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<p className="text-muted-foreground text-sm">{ui("taskFiles.scope")}</p>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button variant={mode === "tree" ? "secondary" : "ghost"} onClick={() => setMode("tree")}>
|
||||
{ui("taskFiles.tree")}
|
||||
</Button>
|
||||
<Button variant={mode === "recent" ? "secondary" : "ghost"} onClick={() => setMode("recent")}>
|
||||
{ui("taskFiles.recent")}
|
||||
</Button>
|
||||
<span className="mr-auto text-muted-foreground text-sm">
|
||||
{summary && ui("taskFiles.summary", { count: summary.total, size: fileSize(summary.size) })}
|
||||
</span>
|
||||
<Button variant="outline" disabled={loading} onClick={() => void refresh()}>
|
||||
<RefreshCwIcon />
|
||||
{ui("taskFiles.refresh")}
|
||||
</Button>
|
||||
<Button variant="outline" disabled={summary?.missing ?? true} onClick={() => download("", true)}>
|
||||
<DownloadIcon />
|
||||
{ui("taskFiles.archiveAll")}
|
||||
</Button>
|
||||
<Button variant="outline" asChild>
|
||||
<Link href={`/function/workspace?path=${encodeURIComponent(workspacePath(""))}`}>
|
||||
{ui("taskFiles.workspace")}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
{error && (
|
||||
<p role="alert" className="text-destructive text-sm">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{mode === "recent" && summary?.truncated && (
|
||||
<p className="text-muted-foreground text-sm">{ui("taskFiles.truncated")}</p>
|
||||
)}
|
||||
<div className="overflow-x-auto rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>{ui("taskFiles.name")}</TableHead>
|
||||
<TableHead>{ui("taskFiles.size")}</TableHead>
|
||||
<TableHead>{ui("taskFiles.modified")}</TableHead>
|
||||
<TableHead>{ui("taskFiles.actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{!rows.length && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4} className="py-12 text-center text-muted-foreground">
|
||||
{loading ? ui("taskFiles.loading") : ui("taskFiles.empty")}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{rows.map(({ entry, depth }) => (
|
||||
<TableRow key={entry.path}>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2" style={{ paddingLeft: depth * 20 }}>
|
||||
<button
|
||||
type="button"
|
||||
className="flex min-w-0 items-center gap-2 text-left hover:underline"
|
||||
disabled={entry.symlink}
|
||||
aria-expanded={entry.dir ? expanded.includes(entry.path) : undefined}
|
||||
onClick={() => {
|
||||
if (entry.dir) {
|
||||
if (expanded.includes(entry.path))
|
||||
setExpanded((prev) =>
|
||||
prev.filter((p) => p !== entry.path && !p.startsWith(`${entry.path}/`)),
|
||||
);
|
||||
else {
|
||||
setExpanded((prev) => [...prev, entry.path]);
|
||||
locate(entry.path);
|
||||
}
|
||||
} else {
|
||||
locate(entry.path);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{entry.dir ? (
|
||||
<>
|
||||
{expanded.includes(entry.path) ? (
|
||||
<ChevronDownIcon className="size-4" />
|
||||
) : (
|
||||
<ChevronRightIcon className="size-4" />
|
||||
)}
|
||||
<FolderIcon className="size-4 shrink-0" />
|
||||
</>
|
||||
) : (
|
||||
<FileIcon className="size-4 shrink-0" />
|
||||
)}
|
||||
<span className="max-w-lg truncate" title={entry.path}>
|
||||
{mode === "recent" ? entry.path : name(entry)}
|
||||
</span>
|
||||
</button>
|
||||
{entry.symlink && <span className="text-muted-foreground text-xs">{ui("taskFiles.symlink")}</span>}
|
||||
{entry.intent_state && <StatusBadge domain="intent" value={entry.intent_state} />}
|
||||
{entry.intent_id && (
|
||||
<Link
|
||||
className="whitespace-nowrap text-xs underline"
|
||||
href={`/function/tasks/detail?id=${task.id}&tab=sessions&session=intent:${entry.intent_id}`}
|
||||
>
|
||||
{ui("taskFiles.session")}
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="whitespace-nowrap text-muted-foreground">
|
||||
{entry.dir || entry.symlink ? "—" : fileSize(entry.size)}
|
||||
</TableCell>
|
||||
<TableCell className="whitespace-nowrap text-muted-foreground">
|
||||
{new Date(entry.mtime).toLocaleString(i18n.language)}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={entry.symlink}
|
||||
onClick={() => download(entry.path, entry.dir)}
|
||||
title={entry.dir ? ui("taskFiles.archive") : ui("taskFiles.download")}
|
||||
aria-label={entry.dir ? ui("taskFiles.archive") : ui("taskFiles.download")}
|
||||
>
|
||||
<DownloadIcon />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
title={ui("taskFiles.copy")}
|
||||
aria-label={ui("taskFiles.copy")}
|
||||
onClick={() => {
|
||||
void navigator.clipboard
|
||||
.writeText(workspacePath(entry.path))
|
||||
.then(() => toast.success(ui("taskFiles.copied")))
|
||||
.catch(() => toast.error(ui("taskFiles.copyFailed")));
|
||||
}}
|
||||
>
|
||||
<CopyIcon />
|
||||
</Button>
|
||||
{!entry.symlink && (
|
||||
<Link
|
||||
className="whitespace-nowrap text-xs underline"
|
||||
href={`/function/workspace?path=${encodeURIComponent(workspacePath(entry.path))}`}
|
||||
>
|
||||
{ui("taskFiles.workspace")}
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
<Dialog
|
||||
open={!!preview}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
previewGeneration.current++;
|
||||
setPreview(null);
|
||||
locate("");
|
||||
}
|
||||
}}
|
||||
>
|
||||
<DialogContent className="flex h-[80vh] flex-col sm:max-w-5xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="truncate pr-6">{preview?.path}</DialogTitle>
|
||||
</DialogHeader>
|
||||
{preview && (
|
||||
<div className="flex gap-2">
|
||||
<Button variant="outline" onClick={() => download(preview.path)}>
|
||||
{ui("taskFiles.download")}
|
||||
</Button>
|
||||
<Button variant="outline" asChild>
|
||||
<Link href={`/function/workspace?path=${encodeURIComponent(workspacePath(preview.path))}`}>
|
||||
{ui("taskFiles.workspace")}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<div className="min-h-0 flex-1 overflow-auto">
|
||||
{preview?.error && <p role="alert">{preview.error}</p>}
|
||||
{preview && !preview.error && isImage(preview.path) && (
|
||||
// biome-ignore lint/performance/noImgElement: authenticated raster preview
|
||||
<img
|
||||
src={api.taskFilePreview(task.id, preview.path)}
|
||||
alt={preview.path}
|
||||
className="mx-auto max-h-full object-contain"
|
||||
onError={() => setPreview((prev) => (prev ? { ...prev, error: ui("taskFiles.imageFailed") } : prev))}
|
||||
/>
|
||||
)}
|
||||
{preview && !preview.error && !isImage(preview.path) && !preview.file && <p>{ui("taskFiles.loading")}</p>}
|
||||
{preview?.file && !preview.error && (preview.file.binary || preview.file.too_large) && (
|
||||
<p>{ui(preview.file.too_large ? "taskFiles.tooLarge" : "taskFiles.binary")}</p>
|
||||
)}
|
||||
{preview?.file && !preview.error && !preview.file.binary && !preview.file.too_large && (
|
||||
<PacketCodeEditor value={preview.file.content ?? ""} readOnly ariaLabel={preview.path} />
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -44,6 +44,7 @@ import type { LLMProfile, Task } from "@/lib/types";
|
||||
import { AssetsTab } from "./_tabs/assets-tab";
|
||||
import { BroadcastTab } from "./_tabs/broadcast-tab";
|
||||
import { CoverageGraphTab } from "./_tabs/coverage-graph-tab";
|
||||
import { FilesTab } from "./_tabs/files-tab";
|
||||
import { FindingsTab } from "./_tabs/findings-tab";
|
||||
import { GraphTab } from "./_tabs/graph-tab";
|
||||
import { InterceptTab } from "./_tabs/intercept-tab";
|
||||
@@ -61,6 +62,7 @@ const TABS = (ui: UITranslate) => [
|
||||
{ value: "intelligence", label: ui("screen.bfaee3ed1f063c") },
|
||||
{ value: "retests", label: ui("screen.d95315ddeb80c2") },
|
||||
{ value: "traffic", label: ui("screen.81a9d0b5a2a24f") },
|
||||
{ value: "files", label: ui("taskFiles.tab") },
|
||||
{ value: "assets", label: ui("screen.e3a1dbbaefc3d7") },
|
||||
{ value: "coverage", label: ui("screen.8bd56c272abd3c") },
|
||||
{ value: "intercept", label: ui("screen.b4c245b6421a35") },
|
||||
@@ -522,6 +524,9 @@ function TaskDetailInner() {
|
||||
<TabsContent value="retests" className="mt-0">
|
||||
<RetestsTab key={id} taskId={id} />
|
||||
</TabsContent>
|
||||
<TabsContent value="files" className="mt-0">
|
||||
<FilesTab key={id} task={task} active={tab === "files"} />
|
||||
</TabsContent>
|
||||
<TabsContent value="assets" className="mt-0">
|
||||
<AssetsTab taskId={id} />
|
||||
</TabsContent>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"use client";
|
||||
import * as React from "react";
|
||||
|
||||
import { useSearchParams } from "next/navigation";
|
||||
|
||||
import {
|
||||
DownloadIcon,
|
||||
FileIcon,
|
||||
@@ -49,7 +51,9 @@ type EditState = {
|
||||
saving: boolean;
|
||||
};
|
||||
|
||||
export default function WorkspacePage() {
|
||||
function WorkspaceInner() {
|
||||
const searchParams = useSearchParams();
|
||||
const requestedPath = searchParams.get("path") ?? "";
|
||||
const { ui, liveUI } = useUITranslation();
|
||||
|
||||
const [path, setPath] = React.useState("");
|
||||
@@ -76,8 +80,31 @@ export default function WorkspacePage() {
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
load("");
|
||||
}, [load]);
|
||||
let alive = true;
|
||||
// A path may identify a file or a directory. Directory listing wins;
|
||||
// otherwise open the file and show its containing directory.
|
||||
void api
|
||||
.workspaceList(requestedPath)
|
||||
.then((result) => {
|
||||
if (!alive) return;
|
||||
setEntries(result.entries);
|
||||
setPath(result.path);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(async () => {
|
||||
if (!alive) return;
|
||||
load(requestedPath.split("/").slice(0, -1).join("/"));
|
||||
try {
|
||||
const file = await api.workspaceRead(requestedPath);
|
||||
if (alive) setEdit({ file, content: file.content ?? "", dirty: false, saving: false });
|
||||
} catch (error) {
|
||||
if (alive) toast.error((error as Error).message);
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [load, requestedPath]);
|
||||
|
||||
const crumbs = React.useMemo(() => {
|
||||
const parts = path ? path.split("/") : [];
|
||||
@@ -358,3 +385,11 @@ export default function WorkspacePage() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function WorkspacePage() {
|
||||
return (
|
||||
<React.Suspense fallback={null}>
|
||||
<WorkspaceInner />
|
||||
</React.Suspense>
|
||||
);
|
||||
}
|
||||
@@ -118,9 +118,10 @@ test("all task detail tabs render their English fixed UI", async () => {
|
||||
["intercept", "InterceptTab"],
|
||||
["report", "ReportTab"],
|
||||
["graph", "GraphTab"],
|
||||
["files", "FilesTab"],
|
||||
]) {
|
||||
const Component = load(base + `/src/app/(main)/function/tasks/detail/_tabs/${file}-tab.tsx`)[name];
|
||||
const html = visible(render(Component, { taskId: "1" }));
|
||||
const html = visible(render(Component, { taskId: "1", task: { id: "1", status: "done" }, active: true }));
|
||||
assert.doesNotMatch(html, /[\p{Script=Han}]/u, `${name}: ${html}`);
|
||||
}
|
||||
const { IntelligencePanel } = load(base + "/src/components/intelligence-panel.tsx");
|
||||
|
||||
@@ -1,4 +1,63 @@
|
||||
export const taskMessages = {
|
||||
"taskFiles.tab": ["文件", "Files"],
|
||||
"taskFiles.tree": ["目录树", "Directory tree"],
|
||||
"taskFiles.recent": ["最近文件", "Recent files"],
|
||||
"taskFiles.summary": ["{{count}} 个文件 · {{size}}", "{{count}} files · {{size}}"],
|
||||
"taskFiles.refresh": ["刷新", "Refresh"],
|
||||
"taskFiles.archiveAll": ["下载整个任务 ZIP", "Download task ZIP"],
|
||||
"taskFiles.workspace": ["在工作空间中打开", "Open in workspace"],
|
||||
"taskFiles.scope": [
|
||||
"显示任务运行目录中的文件;写到目录外的文件不会出现在此处。",
|
||||
"Files in this task’s run directory. Files written elsewhere are not included.",
|
||||
],
|
||||
"taskFiles.truncated": [
|
||||
"仅显示最新的 2000 个文件;统计包含全部文件。",
|
||||
"Showing the latest 2,000 files; totals include all files.",
|
||||
],
|
||||
"taskFiles.name": ["名称", "Name"],
|
||||
"taskFiles.size": ["大小", "Size"],
|
||||
"taskFiles.modified": ["修改时间", "Modified"],
|
||||
"taskFiles.actions": ["操作", "Actions"],
|
||||
"taskFiles.loading": ["正在加载…", "Loading…"],
|
||||
"taskFiles.empty": [
|
||||
"任务尚未产生文件或此目录为空。",
|
||||
"This task has not produced files, or this directory is empty.",
|
||||
],
|
||||
"taskFiles.symlink": ["符号链接(不跟随)", "Symbolic link (not followed)"],
|
||||
"taskFiles.intent": ["意图 #{{id}} · {{title}}", "Intent #{{id}} · {{title}}"],
|
||||
"taskFiles.session": ["查看会话", "View session"],
|
||||
"taskFiles.uploads": ["上传附件", "Uploaded attachments"],
|
||||
"taskFiles.commands": ["命令输出", "Command output"],
|
||||
"taskFiles.screenshots": ["报告截图", "Report screenshots"],
|
||||
"taskFiles.archive": ["下载目录 ZIP", "Download directory ZIP"],
|
||||
"taskFiles.download": ["下载", "Download"],
|
||||
"taskFiles.copy": ["复制工作空间相对路径", "Copy workspace-relative path"],
|
||||
"taskFiles.copied": ["路径已复制", "Path copied"],
|
||||
"taskFiles.copyFailed": ["无法复制路径,请检查剪贴板权限。", "Could not copy the path. Check clipboard permissions."],
|
||||
"taskFiles.tooLarge": ["文件超过 2 MiB,请下载后查看。", "This file exceeds 2 MiB. Download it to view."],
|
||||
"taskFiles.binary": ["二进制文件,请下载后查看。", "Binary file. Download it to view."],
|
||||
"taskFiles.imageFailed": [
|
||||
"图片无法预览,文件可能已消失或内容不是支持的图片。",
|
||||
"Cannot preview this image. It may be missing or contain unsupported data.",
|
||||
],
|
||||
"taskFiles.unavailable": [
|
||||
"任务已删除或归档,或文件已不存在。",
|
||||
"The task was deleted or archived, or the file no longer exists.",
|
||||
],
|
||||
"taskFiles.denied": ["无法访问该文件或符号链接目标。", "Access to this file or symbolic link target is denied."],
|
||||
"taskFiles.invalidPath": ["路径无效或无法访问。", "Invalid or inaccessible path."],
|
||||
"taskFiles.notFile": ["请选择普通文件。", "Select a regular file."],
|
||||
"taskFiles.notDirectory": ["请选择目录进行打包。", "Select a directory to archive."],
|
||||
"taskFiles.notImage": [
|
||||
"仅支持 PNG、JPEG、WebP 和 GIF 图片预览。",
|
||||
"Only PNG, JPEG, WebP and GIF images can be previewed.",
|
||||
],
|
||||
"taskFiles.failed": ["文件加载失败,请刷新重试。", "Could not load files. Refresh to try again."],
|
||||
"taskFiles.pathGone": [
|
||||
"部分目录已消失或无法访问,请重新选择目录。",
|
||||
"Some directories are missing or inaccessible. Select another directory.",
|
||||
],
|
||||
|
||||
"taskUI.filesDeleted": ["删除任务文件", "Task files deleted"],
|
||||
"taskUI.taskDeleted": ["任务已删除", "Task deleted"],
|
||||
"taskUI.created": ["已创建", "Created"],
|
||||
|
||||
+29
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { ChatMention } from "@/lib/chat-mentions";
|
||||
import { MOCK } from "@/lib/mock/enabled";
|
||||
import { mockHandle } from "@/lib/mock/handler";
|
||||
import { mockHandle, mockTaskFileURL } from "@/lib/mock/handler";
|
||||
import type {
|
||||
ActiveFindingRetest,
|
||||
Activity,
|
||||
@@ -98,6 +98,7 @@ import type {
|
||||
TaskAssetScopeMutation,
|
||||
TaskCategory,
|
||||
TaskConstraint,
|
||||
TaskFileListing,
|
||||
TaskGoal,
|
||||
TaskIntelligence,
|
||||
TaskLLMResolutions,
|
||||
@@ -596,6 +597,33 @@ export const api = {
|
||||
// 某资产在本任务里关联的意图 / 事实 / 发现(覆盖图节点抽屉用)。
|
||||
taskAssetRefs: (id: string, assetId: number) => get<CoverageAssetRefs>(`/tasks/${id}/asset-refs?asset_id=${assetId}`),
|
||||
|
||||
taskFiles: (id: string, path = "") => get<TaskFileListing>(`/tasks/${id}/files?path=${encodeURIComponent(path)}`),
|
||||
taskFilesAll: (id: string) => get<TaskFileListing>(`/tasks/${id}/files/all`),
|
||||
taskFileRead: (id: string, path: string) =>
|
||||
get<WorkspaceFile>(`/tasks/${id}/files/read?path=${encodeURIComponent(path)}`),
|
||||
// Browser downloads stream to disk using the same auth cookie as SSE. Avoid
|
||||
// fetching ZIPs into a Blob, which would buffer the entire archive in memory.
|
||||
taskFileDownload: async (id: string, path: string, archive = false) => {
|
||||
const action = archive ? "archive" : "download";
|
||||
if (!MOCK) {
|
||||
const token = getToken();
|
||||
const response = await apiFetch(`/api/tasks/${id}/files/${action}?path=${encodeURIComponent(path)}`, {
|
||||
method: "HEAD",
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
});
|
||||
if (!response.ok) throw new Error(response.status === 404 ? "taskFiles.unavailable" : "taskFiles.failed");
|
||||
}
|
||||
const a = document.createElement("a");
|
||||
a.href = MOCK ? mockTaskFileURL(path, action) : `/api/tasks/${id}/files/${action}?path=${encodeURIComponent(path)}`;
|
||||
a.download = archive ? `${path.split("/").pop() || `task-${id}`}.zip` : path.split("/").pop() || "download";
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
if (MOCK && a.href.startsWith("blob:")) setTimeout(() => URL.revokeObjectURL(a.href), 60_000);
|
||||
},
|
||||
taskFilePreview: (id: string, path: string) =>
|
||||
MOCK ? mockTaskFileURL(path, "preview") : `/api/tasks/${id}/files/preview?path=${encodeURIComponent(path)}`,
|
||||
|
||||
// ---- workspace file manager (workDir) ----
|
||||
workspaceList: (path = "") => get<WorkspaceListing>(`/workspace/list?path=${encodeURIComponent(path)}`),
|
||||
workspaceRead: (path: string) => get<WorkspaceFile>(`/workspace/read?path=${encodeURIComponent(path)}`),
|
||||
|
||||
@@ -38,6 +38,7 @@ import { MAX_TASK_REMARK_RUNES, MAX_TASK_WORKER_COUNT } from "../types";
|
||||
import * as D from "./data";
|
||||
import { createMockNotifications } from "./notifications";
|
||||
import { createMockSchedules } from "./schedules";
|
||||
import { mockTaskFileRead, mockTaskFiles } from "./task-files";
|
||||
|
||||
const delay = (ms = 120) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
@@ -1060,6 +1061,13 @@ export async function mockHandle<T>(method: string, rawPath: string, body?: Body
|
||||
|
||||
function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Record<string, unknown>): unknown {
|
||||
const task = q.get("task") ?? undefined;
|
||||
if (seg[0] === "tasks" && seg[2] === "files") {
|
||||
if (!mockTasks.some((item) => item.id === seg[1] || mockTaskAssetID(item.id) === Number(seg[1])))
|
||||
throw new Error("taskFiles.unavailable");
|
||||
return seg[3] === "read"
|
||||
? mockTaskFileRead(q.get("path") ?? "")
|
||||
: mockTaskFiles(q.get("path") ?? "", seg[3] === "all");
|
||||
}
|
||||
|
||||
if (seg[0] === "schedules") return mockSchedules(m, path, b, new Set(mockTaskAssetIDs.values()));
|
||||
|
||||
@@ -1783,8 +1791,24 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
};
|
||||
|
||||
// ── 工作空间文件管理器(demo:静态示例树;写/建/删走下方写兜底 {ok:true})──
|
||||
if (path === "/workspace/list") return D.workspaceList(q.get("path") ?? "");
|
||||
if (path === "/workspace/read") return D.workspaceRead(q.get("path") ?? "");
|
||||
if (path === "/workspace/list") {
|
||||
const value = q.get("path") ?? "";
|
||||
const taskPath = value.match(/^tasks\/[^/]+(?:\/(.*))?$/);
|
||||
if (taskPath) {
|
||||
const result = mockTaskFiles(taskPath[1] ?? "");
|
||||
return {
|
||||
...result,
|
||||
path: value,
|
||||
entries: result.entries.map((entry) => ({ ...entry, path: `tasks/${value.split("/")[1]}/${entry.path}` })),
|
||||
};
|
||||
}
|
||||
return D.workspaceList(value);
|
||||
}
|
||||
if (path === "/workspace/read") {
|
||||
const value = q.get("path") ?? "";
|
||||
if (value.startsWith("tasks/")) return { ...mockTaskFileRead(value.split("/").slice(2).join("/")), path: value };
|
||||
return D.workspaceRead(value);
|
||||
}
|
||||
|
||||
// ── stats ──
|
||||
if (path === "/stats") {
|
||||
@@ -2804,3 +2828,5 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
? []
|
||||
: {};
|
||||
}
|
||||
|
||||
export { mockTaskFileURL } from "./task-files";
|
||||
@@ -0,0 +1,107 @@
|
||||
import type { TaskFileEntry, TaskFileListing, WorkspaceFile } from "../types";
|
||||
|
||||
const contents: Record<string, string> = {
|
||||
"notes.md": "# Task artifacts\n\nRead-only task file browser demo.\n",
|
||||
"i12/recon.txt": "acme.com\nwww.acme.com\napi.acme.com\n",
|
||||
"uploads/scope.txt": "https://www.acme.com\n",
|
||||
"cmd-output/scan.log": "[demo] Scan complete: 3 hosts discovered.\n",
|
||||
};
|
||||
const screenshot =
|
||||
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=";
|
||||
const paths = [
|
||||
"i12",
|
||||
"uploads",
|
||||
"cmd-output",
|
||||
"findings",
|
||||
"findings/screenshots",
|
||||
...Object.keys(contents),
|
||||
"findings/screenshots/finding-1-step-1.png",
|
||||
];
|
||||
const entries: TaskFileEntry[] = paths.map((path, i) => ({
|
||||
path,
|
||||
name: path.split("/").pop() ?? path,
|
||||
dir: !path.includes("."),
|
||||
size: contents[path]?.length ?? 68,
|
||||
mtime: Date.parse("2026-11-11T05:00:00Z") - i * 60000,
|
||||
symlink: false,
|
||||
...(path === "i12" ? { intent_id: 12, intent_title: "HTTP reconnaissance", intent_state: "done" } : {}),
|
||||
}));
|
||||
|
||||
export function mockTaskFiles(path = "", all = false): TaskFileListing {
|
||||
if (!all && path && !entries.some((e) => e.path === path && e.dir)) throw new Error("taskFiles.notDirectory");
|
||||
const files = entries.filter((e) => !e.dir);
|
||||
return {
|
||||
path,
|
||||
entries: all
|
||||
? files.toSorted((a, b) => b.mtime - a.mtime)
|
||||
: entries.filter((e) => e.path.split("/").slice(0, -1).join("/") === path),
|
||||
total: files.length,
|
||||
size: files.reduce((n, e) => n + e.size, 0),
|
||||
truncated: false,
|
||||
missing: false,
|
||||
};
|
||||
}
|
||||
export function mockTaskFileRead(path: string): WorkspaceFile {
|
||||
if (!(path in contents)) throw new Error("taskFiles.unavailable");
|
||||
return { path, size: contents[path].length, binary: false, content: contents[path] };
|
||||
}
|
||||
|
||||
// A small valid, stored ZIP keeps demo downloads useful without a dependency.
|
||||
export function mockTaskFileURL(path: string, action: string): string {
|
||||
if (action === "preview" || path.endsWith(".png")) return screenshot;
|
||||
if (action !== "archive")
|
||||
return URL.createObjectURL(new Blob([contents[path] ?? "Demo file\n"], { type: "text/plain" }));
|
||||
const parts: Uint8Array[] = [];
|
||||
const central: Uint8Array[] = [];
|
||||
const encoder = new TextEncoder();
|
||||
let offset = 0;
|
||||
const files: [string, Uint8Array][] = Object.entries(contents).map(([name, text]) => [name, encoder.encode(text)]);
|
||||
files.push([
|
||||
"findings/screenshots/finding-1-step-1.png",
|
||||
Uint8Array.from(atob(screenshot.split(",")[1]), (c) => c.charCodeAt(0)),
|
||||
]);
|
||||
for (const [fullName, body] of files.filter(([name]) => !path || name.startsWith(`${path}/`))) {
|
||||
const name = encoder.encode(path ? fullName.slice(path.length + 1) : fullName);
|
||||
let crc = 0xffffffff;
|
||||
for (const byte of body) {
|
||||
crc ^= byte;
|
||||
for (let bit = 0; bit < 8; bit++) crc = (crc >>> 1) ^ (crc & 1 ? 0xedb88320 : 0);
|
||||
}
|
||||
crc = (crc ^ 0xffffffff) >>> 0;
|
||||
const header = new Uint8Array(30 + name.length);
|
||||
const h = new DataView(header.buffer);
|
||||
h.setUint32(0, 0x04034b50, true);
|
||||
h.setUint16(4, 20, true);
|
||||
h.setUint32(14, crc, true);
|
||||
h.setUint32(18, body.length, true);
|
||||
h.setUint32(22, body.length, true);
|
||||
h.setUint16(26, name.length, true);
|
||||
header.set(name, 30);
|
||||
const entry = new Uint8Array(46 + name.length);
|
||||
const c = new DataView(entry.buffer);
|
||||
c.setUint32(0, 0x02014b50, true);
|
||||
c.setUint16(4, 20, true);
|
||||
c.setUint16(6, 20, true);
|
||||
c.setUint32(16, crc, true);
|
||||
c.setUint32(20, body.length, true);
|
||||
c.setUint32(24, body.length, true);
|
||||
c.setUint16(28, name.length, true);
|
||||
c.setUint32(42, offset, true);
|
||||
entry.set(name, 46);
|
||||
central.push(entry);
|
||||
parts.push(header, body);
|
||||
offset += header.length + body.length;
|
||||
}
|
||||
const end = new Uint8Array(22);
|
||||
const e = new DataView(end.buffer);
|
||||
e.setUint32(0, 0x06054b50, true);
|
||||
e.setUint16(8, central.length, true);
|
||||
e.setUint16(10, central.length, true);
|
||||
e.setUint32(
|
||||
12,
|
||||
central.reduce((n, p) => n + p.length, 0),
|
||||
true,
|
||||
);
|
||||
e.setUint32(16, offset, true);
|
||||
return URL.createObjectURL(new Blob([...parts, ...central, end] as BlobPart[], { type: "application/zip" }));
|
||||
}
|
||||
@@ -1976,3 +1976,19 @@ export interface EgressReport {
|
||||
warnings?: string[];
|
||||
warning_details?: { code: string; params?: Record<string, unknown>; message: string }[];
|
||||
}
|
||||
|
||||
// Task-relative, read-only file browser.
|
||||
export interface TaskFileEntry extends WorkspaceEntry {
|
||||
symlink: boolean;
|
||||
intent_id?: number;
|
||||
intent_title?: string;
|
||||
intent_state?: string;
|
||||
}
|
||||
export interface TaskFileListing {
|
||||
path: string;
|
||||
entries: TaskFileEntry[];
|
||||
total: number;
|
||||
size: number;
|
||||
truncated: boolean;
|
||||
missing: boolean;
|
||||
}
|
||||
Reference in new issue
Block a user