fix(organize): 展示智能体处理状态并刷新恢复结果

This commit is contained in:
root committed 2026-10-13 15:38:24 +08:00
1 parent 86351f20f3
commit b306d27d08
9 files changed
+172 -12

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
import { QueryClient } from "@tanstack/react-query";
import { afterEach, describe, expect, it, vi } from "vitest";
const state = vi.hoisted(() => ({ client: null as QueryClient | null, onSuccess: null as (() => void) | null }));
vi.mock("@tanstack/react-query", async (original) => ({
...(await original<typeof import("@tanstack/react-query")>()),
useQueryClient: () => state.client,
useMutation: (options: { onSuccess: () => void }) => {
state.onSuccess = options.onSuccess;
return {};
},
}));
import { useResolveDecision } from "./decisions.ts";
afterEach(() => {
state.client?.clear();
state.client = null;
state.onSuccess = null;
});
describe("事项处理后的业务状态刷新", () => {
it("指定作品或确认映射后,记录、统计、关注汇总和规则预览都会重新读取", () => {
const client = new QueryClient();
state.client = client;
const keys = [
["decisions", "list"],
["identity", "entity"],
["records", "one", "record"],
["records", "list", { attention: true }],
["records", "stats", {}],
["attention"],
["episode-mapping", "words", "entity"],
["episode-mapping", "preview", "entity", {}],
];
for (const key of keys) client.setQueryData(key, { previous: true });
client.setQueryData(["settings"], { unrelated: true });
useResolveDecision();
state.onSuccess!();
for (const key of keys) expect(client.getQueryState(key)?.isInvalidated, JSON.stringify(key)).toBe(true);
expect(client.getQueryState(["settings"])?.isInvalidated).toBe(false);
});
});
+4
View File
@@ -55,6 +55,10 @@ export function useResolveDecision() {
onSuccess: () => {
void qc.invalidateQueries({ queryKey: decisionKeys.all });
void qc.invalidateQueries({ queryKey: ["identity"] });
void qc.invalidateQueries({ queryKey: ["records"] });
void qc.invalidateQueries({ queryKey: ["attention"] });
// 集数确认会写入作品识别词并重算,预览与已保存的规则要一起刷新。
void qc.invalidateQueries({ queryKey: ["episode-mapping"] });
},
});
}
+7 -5
View File
@@ -42,6 +42,11 @@ export type RecordFilters = {
category?: string;
};
/** 记录执行结束后,关联智能体仍可能在补全事实;此时也要继续刷新处理状态。 */
export function recordNeedsRefresh(r: Pick<PublicOrganizationRecord, "status" | "attention">): boolean {
return r.status === "running" || r.status === "queued" || r.attention?.decision?.status === "investigating";
}
export const recordKeys = {
all: ["records"] as const,
list: (f: RecordFilters) => ["records", "list", f] as const,
@@ -114,9 +119,7 @@ export function useRecords(f: RecordFilters) {
}),
getNextPageParam: (last) => last.nextCursor ?? undefined,
refetchInterval: (q) =>
q.state.data?.pages.some((p) => p.items.some((r) => r.status === "running" || r.status === "queued"))
? 5 * TTL_SECOND
: false,
q.state.data?.pages.some((p) => p.items.some(recordNeedsRefresh)) ? 5 * TTL_SECOND : false,
});
}
@@ -129,8 +132,7 @@ export function useRecord(id: string | undefined) {
pathParams: { id: id ?? "" },
signal,
}),
refetchInterval: (q) =>
q.state.data && (q.state.data.status === "running" || q.state.data.status === "queued") ? 3 * TTL_SECOND : false,
refetchInterval: (q) => (q.state.data && recordNeedsRefresh(q.state.data) ? 3 * TTL_SECOND : false),
});
}