|
@@ -0,0 +1,216 @@
|
|
|
|
|
+import { For, Show, createEffect, createMemo, createSignal, on, onCleanup } from "solid-js"
|
|
|
|
|
+import { paasRecord, paasText, type PaasPlatform } from "@/paas"
|
|
|
|
|
+import { readRunInspector, type InspectorEvent } from "./run-inspector-data"
|
|
|
|
|
+
|
|
|
|
|
+const eventLabels: Record<string, string> = {
|
|
|
|
|
+ started: "开始",
|
|
|
|
|
+ done: "完成",
|
|
|
|
|
+ error: "错误",
|
|
|
|
|
+ tool_call: "工具调用",
|
|
|
|
|
+ tool_result: "工具结果",
|
|
|
|
|
+ progress: "进度",
|
|
|
|
|
+ approval_required: "等待确认",
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const stageLabels: Record<string, string> = {
|
|
|
|
|
+ completed: "已完成",
|
|
|
|
|
+ running: "进行中",
|
|
|
|
|
+ failed: "失败",
|
|
|
|
|
+ cancelled: "已取消",
|
|
|
|
|
+ pending: "等待中",
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const duration = (value?: number) => {
|
|
|
|
|
+ if (value === undefined) return ""
|
|
|
|
|
+ if (value < 1000) return `${value} ms`
|
|
|
|
|
+ return `${(value / 1000).toFixed(value < 10000 ? 1 : 0)} 秒`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const usage = (run: Record<string, unknown>) => paasRecord(run.usage) || {}
|
|
|
|
|
+
|
|
|
|
|
+function eventLabel(event: InspectorEvent) {
|
|
|
|
|
+ return eventLabels[event.type] || event.type
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function RunInspector(props: {
|
|
|
|
|
+ api: PaasPlatform
|
|
|
|
|
+ agentID: string
|
|
|
|
|
+ runID: string
|
|
|
|
|
+ run: Record<string, unknown>
|
|
|
|
|
+}) {
|
|
|
|
|
+ const [state, setState] = createSignal({
|
|
|
|
|
+ source: "projection" as "projection" | "service",
|
|
|
|
|
+ loading: false,
|
|
|
|
|
+ loaded: false,
|
|
|
|
|
+ error: "",
|
|
|
|
|
+ data: { stages: [], events: [] } as ReturnType<typeof readRunInspector>,
|
|
|
|
|
+ })
|
|
|
|
|
+ let request = 0
|
|
|
|
|
+ let root: HTMLDetailsElement | undefined
|
|
|
|
|
+ onCleanup(() => request++)
|
|
|
|
|
+
|
|
|
|
|
+ const metrics = createMemo(() => {
|
|
|
|
|
+ const value = usage(props.run)
|
|
|
|
|
+ const data = state().data
|
|
|
|
|
+ return {
|
|
|
|
|
+ stages: data.stages.length,
|
|
|
|
|
+ events: data.events.length,
|
|
|
|
|
+ duration: typeof value.duration_ms === "number" ? value.duration_ms : undefined,
|
|
|
|
|
+ cost: typeof value.cost_usd === "number" ? value.cost_usd : undefined,
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ createEffect(
|
|
|
|
|
+ on(
|
|
|
|
|
+ () => props.runID,
|
|
|
|
|
+ () => {
|
|
|
|
|
+ request++
|
|
|
|
|
+ setState({
|
|
|
|
|
+ source: "projection",
|
|
|
|
|
+ loading: false,
|
|
|
|
|
+ loaded: false,
|
|
|
|
|
+ error: "",
|
|
|
|
|
+ data: { stages: [], events: [] },
|
|
|
|
|
+ })
|
|
|
|
|
+ if (root?.open) void load()
|
|
|
|
|
+ },
|
|
|
|
|
+ { defer: true },
|
|
|
|
|
+ ),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ const load = async (source: "projection" | "service" = "projection") => {
|
|
|
|
|
+ const current = ++request
|
|
|
|
|
+ setState((value) => ({ ...value, source, loading: true, loaded: false, error: "" }))
|
|
|
|
|
+ try {
|
|
|
|
|
+ const result = await props.api.request({
|
|
|
|
|
+ action: "run-events",
|
|
|
|
|
+ agentID: props.agentID,
|
|
|
|
|
+ runID: props.runID,
|
|
|
|
|
+ source,
|
|
|
|
|
+ })
|
|
|
|
|
+ if (current !== request) return
|
|
|
|
|
+ setState((value) => ({ ...value, loading: false, loaded: true, data: readRunInspector(result) }))
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (current !== request) return
|
|
|
|
|
+ setState((value) => ({
|
|
|
|
|
+ ...value,
|
|
|
|
|
+ loading: false,
|
|
|
|
|
+ loaded: false,
|
|
|
|
|
+ error: error instanceof Error ? error.message : "执行事件读取失败。",
|
|
|
|
|
+ }))
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <details
|
|
|
|
|
+ ref={root}
|
|
|
|
|
+ class="paas-run-inspector"
|
|
|
|
|
+ onToggle={(event) => {
|
|
|
|
|
+ if (event.currentTarget.open && !state().loaded && !state().loading) void load()
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ <summary>
|
|
|
|
|
+ 执行检查器
|
|
|
|
|
+ <span>
|
|
|
|
|
+ {metrics().stages ? `${metrics().stages} 阶段` : "阶段"} · {metrics().events || 0} 事件
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </summary>
|
|
|
|
|
+ <div class="paas-inspector-body">
|
|
|
|
|
+ <div class="paas-inspector-metrics">
|
|
|
|
|
+ <span>
|
|
|
|
|
+ 阶段<strong>{metrics().stages}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <span>
|
|
|
|
|
+ 事件<strong>{metrics().events}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <Show when={metrics().duration !== undefined}>
|
|
|
|
|
+ <span>
|
|
|
|
|
+ 时长<strong>{duration(metrics().duration)}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={metrics().cost !== undefined}>
|
|
|
|
|
+ <span>
|
|
|
|
|
+ 成本<strong>${metrics().cost!.toFixed(4)}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <span>
|
|
|
|
|
+ 来源<strong>{state().source === "service" ? "执行服务" : "平台投影"}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <Show when={state().loading}>
|
|
|
|
|
+ <p class="paas-muted" role="status">
|
|
|
|
|
+ 正在读取执行事件…
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={state().error}>
|
|
|
|
|
+ <div class="paas-notice" role="alert">
|
|
|
|
|
+ <strong>执行事件读取失败</strong>
|
|
|
|
|
+ <p>{state().error}</p>
|
|
|
|
|
+ <button type="button" onClick={() => void load(state().source)}>
|
|
|
|
|
+ 重试读取
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={state().loaded && !state().error}>
|
|
|
|
|
+ <Show when={state().data.stages.length}>
|
|
|
|
|
+ <section class="paas-inspector-section" aria-label="执行阶段">
|
|
|
|
|
+ <h3>阶段</h3>
|
|
|
|
|
+ <ol class="paas-inspector-stages">
|
|
|
|
|
+ <For each={state().data.stages}>
|
|
|
|
|
+ {(stage) => (
|
|
|
|
|
+ <li data-status={stage.status}>
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <strong>{stage.id || "未命名阶段"}</strong>
|
|
|
|
|
+ <span>{stageLabels[stage.status] || stage.status}</span>
|
|
|
|
|
+ <small>{duration(stage.durationMs)}</small>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <Show when={Object.keys(stage.details).length}>
|
|
|
|
|
+ <details>
|
|
|
|
|
+ <summary>技术详情</summary>
|
|
|
|
|
+ <pre>{JSON.stringify(stage.details, null, 2)}</pre>
|
|
|
|
|
+ </details>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </li>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </For>
|
|
|
|
|
+ </ol>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={state().data.events.length}>
|
|
|
|
|
+ <section class="paas-inspector-section" aria-label="执行事件">
|
|
|
|
|
+ <h3>事件</h3>
|
|
|
|
|
+ <ol class="paas-inspector-events">
|
|
|
|
|
+ <For each={state().data.events.slice(-100)}>
|
|
|
|
|
+ {(event) => (
|
|
|
|
|
+ <li data-type={event.type}>
|
|
|
|
|
+ <span>{eventLabel(event)}</span>
|
|
|
|
|
+ <strong>{event.stageID || event.detail || `#${event.sequence}`}</strong>
|
|
|
|
|
+ <Show when={event.detail && event.stageID}>
|
|
|
|
|
+ <small>{event.detail}</small>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <time>{event.at}</time>
|
|
|
|
|
+ </li>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </For>
|
|
|
|
|
+ </ol>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={!state().data.stages.length && !state().data.events.length}>
|
|
|
|
|
+ <p class="paas-muted">平台未返回阶段或事件记录;这不表示任务没有执行。</p>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <div class="paas-inspector-actions">
|
|
|
|
|
+ <button type="button" disabled={state().loading} onClick={() => void load("projection")}>
|
|
|
|
|
+ 刷新平台投影
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button type="button" disabled={state().loading} onClick={() => void load("service")}>
|
|
|
|
|
+ 回放执行服务日志
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <Show when={paasText(props.run.checkpoint_id)}>
|
|
|
|
|
+ <p class="paas-muted">最近恢复点:{paasText(props.run.checkpoint_id)}</p>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </details>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|