|
|
@@ -1,6 +1,15 @@
|
|
|
import { For, Show, createEffect, createMemo, on, onCleanup } from "solid-js"
|
|
|
import { createStore, reconcile } from "solid-js/store"
|
|
|
-import { paasRows, paasStatus, paasStatuses, paasTerminalStatuses, paasText, type PaasPlatform } from "@/paas"
|
|
|
+import {
|
|
|
+ paasRecord,
|
|
|
+ paasRows,
|
|
|
+ paasRunDetail,
|
|
|
+ paasStatus,
|
|
|
+ paasStatuses,
|
|
|
+ paasTerminalStatuses,
|
|
|
+ paasText,
|
|
|
+ type PaasPlatform,
|
|
|
+} from "@/paas"
|
|
|
|
|
|
/** Persistent task index: navigation never owns or cancels a remote execution. */
|
|
|
export function createTaskIndex(props: {
|
|
|
@@ -16,14 +25,17 @@ export function createTaskIndex(props: {
|
|
|
loaded: false,
|
|
|
error: "",
|
|
|
updated: "",
|
|
|
+ next: {} as Record<string, number | null>,
|
|
|
+ limited: [] as string[],
|
|
|
+ expanded: [] as string[],
|
|
|
})
|
|
|
const life = { epoch: 0, active: true, request: 0 }
|
|
|
- async function refresh() {
|
|
|
+ async function refresh(more = false) {
|
|
|
if (!props.connected || !props.api || state.loading) return
|
|
|
const epoch = life.epoch,
|
|
|
request = ++life.request
|
|
|
setState("loading", true)
|
|
|
- const agents = props.agents.slice()
|
|
|
+ const agents = props.agents.filter((a) => !more || typeof state.next[paasText(a.id)] === "number")
|
|
|
const results: Record<string, unknown>[] = []
|
|
|
const failures: string[] = []
|
|
|
try {
|
|
|
@@ -32,9 +44,31 @@ export function createTaskIndex(props: {
|
|
|
agents.slice(start, start + 4).map(async (agent) => {
|
|
|
const id = paasText(agent.id)
|
|
|
try {
|
|
|
- const response = await props.api!.request({ action: "history", agentID: id })
|
|
|
+ const offset = more ? state.next[id]! : 0
|
|
|
+ const response = await props.api!.request({
|
|
|
+ action: "history",
|
|
|
+ agentID: id,
|
|
|
+ offset,
|
|
|
+ includeActive: !more,
|
|
|
+ })
|
|
|
+ if (!life.active || epoch !== life.epoch) return
|
|
|
+ const rows = paasRows(response, "runs")
|
|
|
+ const page = paasRecord(response)
|
|
|
+ if (!more && page?.active_included !== true && !state.limited.includes(id))
|
|
|
+ setState("limited", (ids) => [...ids, id])
|
|
|
+ if (!more && page?.active_included === true)
|
|
|
+ setState("limited", (ids) => ids.filter((value) => value !== id))
|
|
|
+ if (more || !state.expanded.includes(id)) {
|
|
|
+ const next = page?.next_offset
|
|
|
+ setState(
|
|
|
+ "next",
|
|
|
+ id,
|
|
|
+ page?.pagination_supported === true && typeof next === "number" && next > offset ? next : null,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ if (more && !state.expanded.includes(id)) setState("expanded", (ids) => [...ids, id])
|
|
|
results.push(
|
|
|
- ...paasRows(response, "runs").map((run) => ({
|
|
|
+ ...rows.map((run) => ({
|
|
|
...run,
|
|
|
run_id: paasText(run.run_id) || paasText(run.id),
|
|
|
agent_id: id,
|
|
|
@@ -49,6 +83,27 @@ export function createTaskIndex(props: {
|
|
|
)
|
|
|
if (!life.active || epoch !== life.epoch) return
|
|
|
}
|
|
|
+ // A formerly active task may have ended outside the most recent history page.
|
|
|
+ if (!more) {
|
|
|
+ const missing = state.rows.filter(
|
|
|
+ (row) =>
|
|
|
+ !paasTerminalStatuses.has(paasStatus(row)) &&
|
|
|
+ !results.some((run) => run.agent_id === row.agent_id && run.run_id === row.run_id),
|
|
|
+ )
|
|
|
+ for (let start = 0; start < missing.length; start += 4) {
|
|
|
+ await Promise.all(
|
|
|
+ missing.slice(start, start + 4).map(async (row) => {
|
|
|
+ try {
|
|
|
+ const run = await paasRunDetail(props.api!, paasText(row.agent_id), paasText(row.run_id))
|
|
|
+ results.push({ ...row, ...run })
|
|
|
+ } catch {
|
|
|
+ failures.push(paasText(row.agent_name))
|
|
|
+ }
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ if (!life.active || epoch !== life.epoch) return
|
|
|
+ }
|
|
|
+ }
|
|
|
if (life.active && epoch === life.epoch && request === life.request) {
|
|
|
results.forEach((run) => {
|
|
|
const previous = state.rows.find((row) => row.agent_id === run.agent_id && row.run_id === run.run_id)
|
|
|
@@ -60,7 +115,9 @@ export function createTaskIndex(props: {
|
|
|
)
|
|
|
props.onNotice?.(run)
|
|
|
})
|
|
|
- setState("rows", reconcile(results, { key: null }))
|
|
|
+ const merged = new Map(state.rows.map((run) => [`${run.agent_id}:${run.run_id}`, run]))
|
|
|
+ results.forEach((run) => merged.set(`${run.agent_id}:${run.run_id}`, run))
|
|
|
+ setState("rows", reconcile([...merged.values()], { key: null }))
|
|
|
setState({
|
|
|
loaded: true,
|
|
|
error: failures.length ? `${failures.join("、")}暂未同步,保留上次记录,稍后自动重试。` : "",
|
|
|
@@ -74,20 +131,35 @@ export function createTaskIndex(props: {
|
|
|
createEffect(
|
|
|
on([() => props.connected, () => props.scope, () => props.agents.map((a) => a.id).join("|")], () => {
|
|
|
life.epoch++
|
|
|
- setState({ rows: [], loaded: false, loading: false, error: "", updated: "" })
|
|
|
+ setState({ rows: [], loaded: false, loading: false, error: "", updated: "", limited: [], expanded: [] })
|
|
|
+ setState("next", reconcile({}))
|
|
|
if (props.connected) void refresh()
|
|
|
}),
|
|
|
)
|
|
|
- const timer = setInterval(() => void refresh(), 10000)
|
|
|
+ const timer = setInterval(() => {
|
|
|
+ if (!document.hidden) void refresh()
|
|
|
+ }, 10000)
|
|
|
+ const visible = () => {
|
|
|
+ if (!document.hidden) void refresh()
|
|
|
+ }
|
|
|
+ document.addEventListener("visibilitychange", visible)
|
|
|
onCleanup(() => {
|
|
|
life.active = false
|
|
|
clearInterval(timer)
|
|
|
+ document.removeEventListener("visibilitychange", visible)
|
|
|
})
|
|
|
const waiting = createMemo(() => state.rows.filter((row) => paasStatus(row).startsWith("waiting")))
|
|
|
const running = createMemo(() =>
|
|
|
state.rows.filter((row) => !paasTerminalStatuses.has(paasStatus(row)) && !paasStatus(row).startsWith("waiting")),
|
|
|
)
|
|
|
- return { state, refresh, waiting, running }
|
|
|
+ return {
|
|
|
+ state,
|
|
|
+ refresh,
|
|
|
+ waiting,
|
|
|
+ running,
|
|
|
+ more: () => refresh(true),
|
|
|
+ hasMore: () => Object.values(state.next).some((value) => typeof value === "number"),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export function PaasTasks(props: {
|
|
|
@@ -123,7 +195,7 @@ export function PaasTasks(props: {
|
|
|
<div class="paas-section-title">
|
|
|
<div>
|
|
|
<h1>平台任务</h1>
|
|
|
- <p>自动汇总各智能体最近任务,执行中的任务优先显示。打开任务可继续查看成果或提交确认。</p>
|
|
|
+ <p>汇总活动任务与最近记录,支持加载更早历史。打开任务可查看成果或处理确认。</p>
|
|
|
</div>
|
|
|
<button disabled={!props.connected || props.index.state.loading} onClick={() => void props.index.refresh()}>
|
|
|
刷新任务
|
|
|
@@ -141,6 +213,9 @@ export function PaasTasks(props: {
|
|
|
{props.index.state.error}
|
|
|
</p>
|
|
|
</Show>
|
|
|
+ <Show when={props.index.state.limited.length}>
|
|
|
+ <p class="paas-muted">部分平台仅提供最近记录,活动任务数量可能不完整;升级对应服务后可补齐。</p>
|
|
|
+ </Show>
|
|
|
<div class="paas-toolbar">
|
|
|
<label>
|
|
|
搜索任务
|
|
|
@@ -215,7 +290,12 @@ export function PaasTasks(props: {
|
|
|
</table>
|
|
|
</div>
|
|
|
</Show>
|
|
|
- <p class="paas-muted">展示平台返回的最近记录;状态变化不会重新执行任务。</p>
|
|
|
+ <Show when={props.index.hasMore()}>
|
|
|
+ <button disabled={!props.connected || props.index.state.loading} onClick={() => void props.index.more()}>
|
|
|
+ {props.index.state.loading ? "正在读取…" : "加载更早任务"}
|
|
|
+ </button>
|
|
|
+ </Show>
|
|
|
+ <p class="paas-muted">刷新只同步记录,不会重新执行任务。搜索范围为已加载的记录。</p>
|
|
|
</section>
|
|
|
)
|
|
|
}
|