返回 DeepSeek-Reasonix
canonicalHistoryWindow.ts
根目录 / desktop / frontend / src / lib / canonicalHistoryWindow.ts
1 import { app } from "./bridge";
2 import { asArray } from "./array";
3 import { entriesFor } from "./canonicalTranscriptBackend";
4 import { readBoundHistoryWindow } from "./historyReadBinding";
5 import type { HistoryWindowPage, PersistentMessage } from "../generated/desktopContract.generated";
6 import type { HistoryWindowPageView, HistoryWindowRequestView } from "./types";
7
8 function unsupportedWindow(): HistoryWindowPageView {
9 return {
10 entries: [], status: "unsupported", olderCursor: "", newerCursor: "",
11 hasOlder: false, hasNewer: false, totalTurns: 0, startTurn: 0, endTurn: 0,
12 revision: 0, revisionKnown: false, digest: "",
13 };
14 }
15
16 export async function readCanonicalHistoryWindow(tabId: string, req: HistoryWindowRequestView, remote: boolean): Promise<HistoryWindowPageView> {
17 let page: HistoryWindowPage;
18 if (remote) {
19 if (typeof app.RemoteSessionHistoryWindowForTab !== "function") {
20 return unsupportedWindow();
21 }
22 page = await app.RemoteSessionHistoryWindowForTab(tabId, req);
23 } else {
24 const bound = await readBoundHistoryWindow(tabId, req);
25 if (bound && "entries" in bound) return bound;
26 if (bound) page = bound;
27 else {
28 if (typeof app.SessionHistoryWindowForTab !== "function") {
29 return unsupportedWindow();
30 }
31 page = await app.SessionHistoryWindowForTab(tabId, req);
32 }
33 }
34 const status = (page.status || "ready") as HistoryWindowPageView["status"];
35 if (status === "unsupported") {
36 return unsupportedWindow();
37 }
38 const entries = entriesFor(asArray<PersistentMessage>(page.messages), page.snapshotSequence);
39 const turns = entries.map(entry => entry.turn).filter(turn => turn > 0);
40 return {
41 entries,
42 status,
43 olderCursor: page.olderCursor ?? "",
44 newerCursor: page.newerCursor ?? "",
45 hasOlder: Boolean(page.hasOlder),
46 hasNewer: Boolean(page.hasNewer),
47 totalTurns: page.totalTurns ?? (turns.length > 0 ? Math.max(...turns) : 0),
48 startTurn: turns.length > 0 ? Math.min(...turns) : 0,
49 endTurn: turns.length > 0 ? Math.max(...turns) : 0,
50 revision: page.snapshotSequence ?? 0,
51 revisionKnown: (page.snapshotSequence ?? 0) > 0,
52 digest: page.generation ?? "",
53 };
54 }
55
55 lines TYPESCRIPT