返回 DeepSeek-Reasonix
historyReadBinding.ts
根目录 / desktop / frontend / src / lib / historyReadBinding.ts
1 import { app } from "./bridge";
2 import type { HistoryWindowPage, HistoryOutlinePage, HistoryOutlineRequest, SearchHistoryPage } from "../generated/desktopContract.generated";
3 import type { HistoryWindowRequestView, HistoryWindowPageView } from "./types";
4 import { supportsHistoryRead, acquireHistoryRead, currentHistoryReadBinding, releaseHistoryRead } from "./historyReadScope";
5 export { releaseHistoryRead } from "./historyReadScope";
6
7 const stale = (): HistoryWindowPageView => ({ entries: [], status: "stale_cursor", olderCursor: "", newerCursor: "",
8 hasOlder: false, hasNewer: false, totalTurns: 0, startTurn: 0, endTurn: 0, revision: 0, revisionKnown: false, digest: "" });
9
10 export async function readBoundHistoryWindow(tabId: string, req: HistoryWindowRequestView): Promise<HistoryWindowPage | HistoryWindowPageView | undefined> {
11 if (!supportsHistoryRead()) return undefined;
12 const binding = acquireHistoryRead(tabId);
13 const handle = await binding.promise;
14 if (currentHistoryReadBinding(tabId) !== binding) return stale();
15 if (!handle.capabilities?.includes("history-read-binding-v1")) return undefined;
16 if (handle.storageBackend === "canonical") {
17 const page = await app.ReadSessionHistoryWindow(handle.id, req);
18 if (currentHistoryReadBinding(tabId) !== binding) return stale();
19 if (page.status === "stale_cursor") releaseHistoryRead(tabId);
20 return page;
21 }
22 if (req.anchor && !["newest", "cursor"].includes(req.anchor) && !handle.capabilities?.includes("history-native-navigation-v1")) {
23 return { ...stale(), status: "unsupported" };
24 }
25 const result = await app.ReadSessionHistorySlice(handle.id, { cursor: req.cursor ?? "", turns: req.limit ?? 32, entries: req.limit ?? 32, bytes: 1 << 20, newer: req.direction === "newer",
26 anchor: req.anchor, turn: req.turn, messageId: req.messageId, generation: req.generation, snapshotSequence: req.snapshotSequence });
27 if (currentHistoryReadBinding(tabId) !== binding) return stale();
28 if (result.status === "stale_cursor") releaseHistoryRead(tabId);
29 const page = result.page;
30 return { entries: page.entries ?? [], status: result.status as HistoryWindowPageView["status"], olderCursor: page.nextCursor ?? "", newerCursor: page.newerCursor ?? "",
31 hasOlder: page.hasOlder, hasNewer: page.hasNewer ?? false, totalTurns: page.totalTurns, startTurn: page.startTurn, endTurn: page.endTurn,
32 revision: page.revision, revisionKnown: page.revisionKnown ?? false, digest: page.digest ?? "" };
33 }
34
35 export async function readBoundHistoryOutline(tabId: string, req: HistoryOutlineRequest): Promise<HistoryOutlinePage | undefined> {
36 if (!supportsHistoryRead() || typeof app.ReadSessionHistoryOutline !== "function") return undefined;
37 const binding = acquireHistoryRead(tabId);
38 const handle = await binding.promise;
39 if (currentHistoryReadBinding(tabId) !== binding) return { entries: [], status: "stale_cursor", totalTurns: 0, nextTurn: 0, done: true, snapshotSequence: 0, coverageSequence: 0, generation: "" };
40 if (!handle.capabilities?.includes("history-read-binding-v1")) return undefined;
41 const page = await app.ReadSessionHistoryOutline(handle.id, req);
42 if (currentHistoryReadBinding(tabId) !== binding) return { ...page, entries: [], status: "stale_cursor" };
43 if (page.status === "stale_cursor") releaseHistoryRead(tabId);
44 return page;
45 }
46
47 export async function searchBoundHistory(tabId: string, text: string, cursor = "", limit = 50): Promise<SearchHistoryPage | undefined> {
48 if (!supportsHistoryRead() || typeof app.SearchSessionHistoryRead !== "function") return undefined;
49 const empty = (status: string): SearchHistoryPage => ({ hits: [], status, hasMore: false, snapshotSequence: 0, coverageSequence: 0 });
50 const binding = acquireHistoryRead(tabId);
51 const handle = await binding.promise;
52 if (currentHistoryReadBinding(tabId) !== binding) return empty("stale_cursor");
53 if (!handle.capabilities?.includes("history-read-binding-v1")) return undefined;
54 if (handle.storageBackend !== "canonical" && !handle.capabilities?.includes("history-native-search-v1")) return empty("unsupported");
55 const page = await app.SearchSessionHistoryRead(handle.id, text, cursor, limit);
56 if (currentHistoryReadBinding(tabId) !== binding) return empty("stale_cursor");
57 if (page.status === "stale_cursor") releaseHistoryRead(tabId);
58 return page;
59 }
60
60 lines TYPESCRIPT