|
|
@@ -0,0 +1,409 @@
|
|
|
+import { For, Show, createEffect, createMemo, on, onCleanup } from "solid-js"
|
|
|
+import { createStore } from "solid-js/store"
|
|
|
+import { ResearchIcon } from "./research-icon"
|
|
|
+import { paasRecord, paasRunDetail, paasText, type PaasPlatform } from "@/paas"
|
|
|
+import {
|
|
|
+ artifactCategory,
|
|
|
+ artifactRuns,
|
|
|
+ filterArtifacts,
|
|
|
+ readRunArtifacts,
|
|
|
+ type ArtifactCategory,
|
|
|
+ type ArtifactChat,
|
|
|
+ type ArtifactEntry,
|
|
|
+ type ArtifactRun,
|
|
|
+} from "./artifact-library"
|
|
|
+import type { PaasPreviewFile } from "./preview"
|
|
|
+
|
|
|
+const categories: { id: ArtifactCategory | "all"; label: string }[] = [
|
|
|
+ { id: "all", label: "全部" },
|
|
|
+ { id: "document", label: "文档" },
|
|
|
+ { id: "image", label: "图片" },
|
|
|
+ { id: "data", label: "数据" },
|
|
|
+ { id: "other", label: "其他" },
|
|
|
+]
|
|
|
+
|
|
|
+const categoryLabels: Record<ArtifactCategory, string> = {
|
|
|
+ document: "文档",
|
|
|
+ image: "图片",
|
|
|
+ data: "数据",
|
|
|
+ other: "文件",
|
|
|
+}
|
|
|
+
|
|
|
+const maxRuns = 40
|
|
|
+const cache = new Map<string, ArtifactEntry[]>()
|
|
|
+
|
|
|
+function formatSize(value?: number) {
|
|
|
+ if (!value || value < 0) return ""
|
|
|
+ if (value < 1024) return `${value} B`
|
|
|
+ if (value < 1024 * 1024) return `${Math.round(value / 1024)} KB`
|
|
|
+ return `${(value / 1024 / 1024).toFixed(1)} MB`
|
|
|
+}
|
|
|
+
|
|
|
+function formatTime(value: number) {
|
|
|
+ if (!value) return ""
|
|
|
+ return new Date(value).toLocaleString("zh-CN", {
|
|
|
+ month: "numeric",
|
|
|
+ day: "numeric",
|
|
|
+ hour: "2-digit",
|
|
|
+ minute: "2-digit",
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+export function PaasArtifacts(props: {
|
|
|
+ api?: PaasPlatform
|
|
|
+ connected: boolean
|
|
|
+ scope?: string
|
|
|
+ projectID: string
|
|
|
+ projectName: string
|
|
|
+ chats: () => ArtifactChat[]
|
|
|
+ tasks: () => Record<string, unknown>[]
|
|
|
+ agents: Record<string, unknown>[]
|
|
|
+ onOpenRun: (run: ArtifactRun) => void
|
|
|
+ onPreview: (file: PaasPreviewFile) => void
|
|
|
+ onNewResearch?: () => void
|
|
|
+}) {
|
|
|
+ const [state, setState] = createStore({
|
|
|
+ entries: [] as ArtifactEntry[],
|
|
|
+ loading: false,
|
|
|
+ loaded: false,
|
|
|
+ failed: [] as string[],
|
|
|
+ updated: "",
|
|
|
+ notice: "",
|
|
|
+ saving: "",
|
|
|
+ })
|
|
|
+ const [filters, setFilters] = createStore({
|
|
|
+ query: "",
|
|
|
+ category: "all" as ArtifactCategory | "all",
|
|
|
+ })
|
|
|
+ const runs = createMemo(() =>
|
|
|
+ artifactRuns({ chats: props.chats(), tasks: props.tasks(), projectID: props.projectID }),
|
|
|
+ )
|
|
|
+ const targets = createMemo(() => runs().slice(0, maxRuns))
|
|
|
+ const signature = createMemo(() => targets().map((run) => `${run.agentID}:${run.runID}`).join("|"))
|
|
|
+ // Re-running a chat often republishes the same workspace files; keep the newest copy per chat.
|
|
|
+ const library = createMemo(() => {
|
|
|
+ const seen = new Map<string, ArtifactEntry>()
|
|
|
+ for (const entry of state.entries) {
|
|
|
+ const key = `${entry.agentID}:${entry.chatID || entry.runID}:${entry.name.toLocaleLowerCase()}`
|
|
|
+ if (!seen.has(key)) seen.set(key, entry)
|
|
|
+ }
|
|
|
+ return [...seen.values()]
|
|
|
+ })
|
|
|
+ const entries = createMemo(() => filterArtifacts(library(), { query: filters.query, category: filters.category }))
|
|
|
+ const groups = createMemo(() => {
|
|
|
+ const runsByKey = new Map(targets().map((run) => [`${run.agentID}:${run.runID}`, run]))
|
|
|
+ const result = new Map<string, { run: ArtifactRun; entries: ArtifactEntry[]; runs: Set<string> }>()
|
|
|
+ for (const entry of entries()) {
|
|
|
+ const key = entry.chatID ? `${entry.agentID}:${entry.chatID}` : `${entry.agentID}:${entry.runID}`
|
|
|
+ const run =
|
|
|
+ runsByKey.get(`${entry.agentID}:${entry.runID}`) ||
|
|
|
+ ({
|
|
|
+ agentID: entry.agentID,
|
|
|
+ runID: entry.runID,
|
|
|
+ chatID: entry.chatID,
|
|
|
+ chatTitle: entry.chatTitle,
|
|
|
+ at: entry.at,
|
|
|
+ } satisfies ArtifactRun)
|
|
|
+ const group = result.get(key) || { run, entries: [], runs: new Set<string>() }
|
|
|
+ group.entries.push(entry)
|
|
|
+ group.runs.add(entry.runID)
|
|
|
+ if (run.at > group.run.at) group.run = run
|
|
|
+ result.set(key, group)
|
|
|
+ }
|
|
|
+ for (const group of result.values()) group.entries.sort(byRelevance)
|
|
|
+ return [...result.values()].sort((a, b) => b.run.at - a.run.at)
|
|
|
+ })
|
|
|
+ const counts = createMemo(() => {
|
|
|
+ const result: Record<string, number> = { all: library().length }
|
|
|
+ for (const entry of library()) {
|
|
|
+ const category = artifactCategory(entry)
|
|
|
+ result[category] = (result[category] || 0) + 1
|
|
|
+ }
|
|
|
+ return result
|
|
|
+ })
|
|
|
+ const agentName = (agentID: string) =>
|
|
|
+ paasText(props.agents.find((agent) => paasText(agent.id) === agentID)?.name) || agentID
|
|
|
+ const narrowed = () => !!filters.query.trim() || filters.category !== "all"
|
|
|
+ const cacheKey = (run: ArtifactRun) => `${props.scope || ""}:${run.agentID}:${run.runID}`
|
|
|
+ let epoch = 0
|
|
|
+ onCleanup(() => {
|
|
|
+ epoch++
|
|
|
+ })
|
|
|
+
|
|
|
+ const load = async (force = false) => {
|
|
|
+ const current = ++epoch
|
|
|
+ setState({ notice: "" })
|
|
|
+ if (!props.connected || !props.api) {
|
|
|
+ setState({ entries: [], loading: false, loaded: false, failed: [] })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const list = targets()
|
|
|
+ setState({ loading: true, failed: [] })
|
|
|
+ const collected: ArtifactEntry[] = []
|
|
|
+ const failed: string[] = []
|
|
|
+ await pool(list, 4, async (run) => {
|
|
|
+ const key = cacheKey(run)
|
|
|
+ const cached = !force ? cache.get(key) : undefined
|
|
|
+ if (cached) {
|
|
|
+ collected.push(...cached)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const [detail, files] = await Promise.all([
|
|
|
+ paasRunDetail(props.api!, run.agentID, run.runID).catch(() => undefined),
|
|
|
+ props.api!.request({ action: "run-files", agentID: run.agentID, runID: run.runID }).catch(() => undefined),
|
|
|
+ ])
|
|
|
+ if (!detail && !files) {
|
|
|
+ failed.push(run.chatTitle)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const items = readRunArtifacts({ run, detail, files })
|
|
|
+ cache.set(key, items)
|
|
|
+ collected.push(...items)
|
|
|
+ })
|
|
|
+ if (current !== epoch) return
|
|
|
+ setState({
|
|
|
+ entries: collected.sort((a, b) => b.at - a.at || a.name.localeCompare(b.name, "zh-CN")),
|
|
|
+ loading: false,
|
|
|
+ loaded: true,
|
|
|
+ failed,
|
|
|
+ updated: new Date().toLocaleTimeString(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ createEffect(
|
|
|
+ on(
|
|
|
+ [() => props.connected, () => props.scope, () => props.projectID, signature],
|
|
|
+ () => void load(),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+
|
|
|
+ const save = async (entry: ArtifactEntry) => {
|
|
|
+ if (!props.api || state.saving) return
|
|
|
+ setState({ saving: entry.id, notice: "" })
|
|
|
+ try {
|
|
|
+ const result = paasRecord(
|
|
|
+ await props.api.request({
|
|
|
+ action: "download",
|
|
|
+ agentID: entry.agentID,
|
|
|
+ runID: entry.runID,
|
|
|
+ name: entry.name,
|
|
|
+ path: entry.path,
|
|
|
+ artifactID: entry.artifactID,
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ setState(
|
|
|
+ "notice",
|
|
|
+ result?.saved ? `已保存 ${entry.name}` : `${entry.name} 未保存,文件仍保留在平台。`,
|
|
|
+ )
|
|
|
+ } catch (error) {
|
|
|
+ setState("notice", error instanceof Error ? error.message : "保存失败,请重试。")
|
|
|
+ } finally {
|
|
|
+ setState("saving", "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const card = (entry: ArtifactEntry) => {
|
|
|
+ const category = artifactCategory(entry)
|
|
|
+ return (
|
|
|
+ <li class="paas-artifact-row">
|
|
|
+ <span class="paas-artifact-kind" data-category={category}>
|
|
|
+ {categoryLabels[category]}
|
|
|
+ </span>
|
|
|
+ <div class="paas-artifact-name">
|
|
|
+ <strong title={entry.name}>{entry.name}</strong>
|
|
|
+ <small>
|
|
|
+ {[entry.mime, formatSize(entry.size)].filter(Boolean).join(" · ") || entry.name.split(".").pop()?.toUpperCase()}
|
|
|
+ </small>
|
|
|
+ </div>
|
|
|
+ <div class="paas-artifact-actions">
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ aria-label={`预览 ${entry.name}`}
|
|
|
+ onClick={() =>
|
|
|
+ props.onPreview({
|
|
|
+ agentID: entry.agentID,
|
|
|
+ runID: entry.runID,
|
|
|
+ name: entry.name,
|
|
|
+ path: entry.path,
|
|
|
+ artifactID: entry.artifactID,
|
|
|
+ mime: entry.mime,
|
|
|
+ size: entry.size,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ >
|
|
|
+ 预览
|
|
|
+ </button>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ aria-label={`保存 ${entry.name}`}
|
|
|
+ disabled={!!state.saving}
|
|
|
+ onClick={() => void save(entry)}
|
|
|
+ >
|
|
|
+ {state.saving === entry.id ? "保存中…" : "保存"}
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </li>
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <section class="paas-artifact-page" aria-label="项目成果">
|
|
|
+ <div class="paas-section-title">
|
|
|
+ <div>
|
|
|
+ <h1>成果</h1>
|
|
|
+ <p>{props.projectName} · 聚合项目内已结束任务生成的文件,可预览或保存。</p>
|
|
|
+ </div>
|
|
|
+ <button type="button" disabled={!props.connected || state.loading} onClick={() => void load(true)}>
|
|
|
+ {state.loading ? "正在同步…" : "刷新成果"}
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ <div class="paas-artifact-counts">
|
|
|
+ <span>{library().length} 个文件</span>
|
|
|
+ <span>{groups().length} 个会话</span>
|
|
|
+ <small>{state.updated ? `更新于 ${state.updated}` : "正在读取项目成果…"}</small>
|
|
|
+ </div>
|
|
|
+ <div class="paas-artifact-toolbar">
|
|
|
+ <label>
|
|
|
+ 搜索成果
|
|
|
+ <input
|
|
|
+ type="search"
|
|
|
+ placeholder="文件名或来源会话"
|
|
|
+ value={filters.query}
|
|
|
+ onInput={(event) => setFilters("query", event.currentTarget.value)}
|
|
|
+ />
|
|
|
+ </label>
|
|
|
+ <div class="paas-artifact-filter" role="group" aria-label="成果类型">
|
|
|
+ <For each={categories}>
|
|
|
+ {(category) => (
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ aria-pressed={filters.category === category.id}
|
|
|
+ onClick={() => setFilters("category", category.id)}
|
|
|
+ >
|
|
|
+ {category.label}
|
|
|
+ <Show when={(counts()[category.id] || 0) > 0}> · {counts()[category.id]}</Show>
|
|
|
+ </button>
|
|
|
+ )}
|
|
|
+ </For>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <Show when={state.notice}>
|
|
|
+ <p class="paas-notice" role="status">
|
|
|
+ {state.notice}
|
|
|
+ </p>
|
|
|
+ </Show>
|
|
|
+ <Show when={state.failed.length}>
|
|
|
+ <div class="paas-notice" role="status">
|
|
|
+ <p>部分任务的成果暂未同步,已展示其余内容;可稍后刷新重试。</p>
|
|
|
+ <details>
|
|
|
+ <summary>查看未同步任务 · {state.failed.length}</summary>
|
|
|
+ <p>{state.failed.join("、")}</p>
|
|
|
+ </details>
|
|
|
+ </div>
|
|
|
+ </Show>
|
|
|
+ <Show when={runs().length > maxRuns}>
|
|
|
+ <p class="paas-muted">仅聚合最近 {maxRuns} 个已结束任务;更早任务可在平台任务中查看。</p>
|
|
|
+ </Show>
|
|
|
+ <Show
|
|
|
+ when={props.connected && (state.loading || groups().length)}
|
|
|
+ fallback={
|
|
|
+ <div class="paas-task-empty">
|
|
|
+ <ResearchIcon kind="evidence" />
|
|
|
+ <strong>
|
|
|
+ {!props.connected
|
|
|
+ ? "连接平台后加载成果"
|
|
|
+ : !state.loaded
|
|
|
+ ? "正在读取项目成果…"
|
|
|
+ : runs().length && narrowed()
|
|
|
+ ? "没有匹配的成果"
|
|
|
+ : "还没有可归档的成果"}
|
|
|
+ </strong>
|
|
|
+ <p>
|
|
|
+ {!props.connected
|
|
|
+ ? "连接后会自动同步当前项目已结束任务的成果。"
|
|
|
+ : !state.loaded
|
|
|
+ ? "请稍候。"
|
|
|
+ : runs().length && narrowed()
|
|
|
+ ? "调整搜索或类型筛选,或清除筛选条件。"
|
|
|
+ : runs().length
|
|
|
+ ? "已结束的任务没有返回可归档文件;可打开任务查看运行详情。"
|
|
|
+ : "完成一项研究任务后,生成的文件会汇总到这里。"}
|
|
|
+ </p>
|
|
|
+ <Show when={props.connected && state.loaded && runs().length && narrowed()}>
|
|
|
+ <button type="button" onClick={() => setFilters({ query: "", category: "all" })}>
|
|
|
+ 清除筛选
|
|
|
+ </button>
|
|
|
+ </Show>
|
|
|
+ <Show when={props.connected && state.loaded && !runs().length && props.onNewResearch}>
|
|
|
+ <button type="button" class="paas-primary" onClick={props.onNewResearch}>
|
|
|
+ 开始新研究
|
|
|
+ </button>
|
|
|
+ </Show>
|
|
|
+ </div>
|
|
|
+ }
|
|
|
+ >
|
|
|
+ <Show when={state.loading && !state.loaded}>
|
|
|
+ <p class="paas-muted" role="status">
|
|
|
+ 正在读取项目成果…
|
|
|
+ </p>
|
|
|
+ </Show>
|
|
|
+ <div class="paas-artifact-groups">
|
|
|
+ <For each={groups()}>
|
|
|
+ {(group) => (
|
|
|
+ <section class="paas-artifact-group" aria-label={`${group.run.chatTitle} 的成果`}>
|
|
|
+ <header>
|
|
|
+ <div>
|
|
|
+ <strong>{group.run.chatTitle}</strong>
|
|
|
+ <span>
|
|
|
+ {agentName(group.run.agentID)}
|
|
|
+ <Show when={formatTime(group.run.at)}> · {formatTime(group.run.at)}</Show>
|
|
|
+ <Show when={group.runs.size > 1}> · {group.runs.size} 个任务</Show>
|
|
|
+ {" · "}
|
|
|
+ {group.entries.length} 个文件
|
|
|
+ </span>
|
|
|
+ </div>
|
|
|
+ <button type="button" onClick={() => props.onOpenRun(group.run)}>
|
|
|
+ 打开任务
|
|
|
+ </button>
|
|
|
+ </header>
|
|
|
+ <ul>
|
|
|
+ <For each={group.entries.slice(0, 8)}>{card}</For>
|
|
|
+ </ul>
|
|
|
+ <Show when={group.entries.length > 8}>
|
|
|
+ <details class="paas-artifact-more">
|
|
|
+ <summary>展开其余 {group.entries.length - 8} 个文件</summary>
|
|
|
+ <ul>
|
|
|
+ <For each={group.entries.slice(8, 200)}>{card}</For>
|
|
|
+ </ul>
|
|
|
+ <Show when={group.entries.length > 200}>
|
|
|
+ <p class="paas-muted">
|
|
|
+ 此处最多展开 200 个文件;该任务工作区共 {group.entries.length} 个文件,其余请在任务中查看。
|
|
|
+ </p>
|
|
|
+ </Show>
|
|
|
+ </details>
|
|
|
+ </Show>
|
|
|
+ </section>
|
|
|
+ )}
|
|
|
+ </For>
|
|
|
+ </div>
|
|
|
+ </Show>
|
|
|
+ </section>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+async function pool<T>(items: T[], limit: number, task: (item: T) => Promise<void>) {
|
|
|
+ let index = 0
|
|
|
+ const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
|
+ while (index < items.length) await task(items[index++])
|
|
|
+ })
|
|
|
+ await Promise.all(workers)
|
|
|
+}
|
|
|
+
|
|
|
+const categoryRank: Record<ArtifactCategory, number> = { document: 0, image: 1, data: 2, other: 3 }
|
|
|
+
|
|
|
+function byRelevance(a: ArtifactEntry, b: ArtifactEntry) {
|
|
|
+ return (
|
|
|
+ categoryRank[artifactCategory(a)] - categoryRank[artifactCategory(b)] ||
|
|
|
+ Number(a.source !== "artifact") - Number(b.source !== "artifact") ||
|
|
|
+ a.name.localeCompare(b.name, "zh-CN")
|
|
|
+ )
|
|
|
+}
|