返回 DeepSeek-Reasonix
useSessionCatalogActions.ts
根目录 / desktop / frontend / src / lib / useSessionCatalogActions.ts
1 import { useCallback, useRef } from "react";
2 import { app } from "./bridge";
3 import { asArray } from "./array";
4 import type { HistoryMessage } from "./types";
5 import type { SessionRef } from "./sessionRef";
6
7 export function useSessionCatalogActions(
8 requireIntent: (seq: number) => Promise<void>,
9 current: (seq: number) => boolean,
10 sync: (reset: boolean, guard: boolean, options: { navigationIntentSeq: number; surfacePolicy: "replace-surface" }) => Promise<string | undefined>,
11 invalidateCache: () => void,
12 ) {
13 const opening = useRef<{ intent: number; sessionId: string } | null>(null);
14 const openCanonicalSession = useCallback(async (ref: SessionRef, navigationIntentSeq: number): Promise<void> => {
15 await requireIntent(navigationIntentSeq);
16 if (!current(navigationIntentSeq)) return;
17 const owner = { intent: navigationIntentSeq, sessionId: ref.sessionId };
18 opening.current = owner;
19 try {
20 await app.OpenSession(ref);
21 if (!current(navigationIntentSeq)) return;
22 // OpenSession publishes rebuilt/ready before its response. This intent
23 // owns the single hydration; those notifications must not start a second
24 // follower against the surface while its binding is still committing.
25 await sync(true, false, { navigationIntentSeq, surfacePolicy: "replace-surface" });
26 } finally {
27 if (opening.current === owner) opening.current = null;
28 }
29 }, [requireIntent, current, sync]);
30 const previewSession = useCallback((path: string): Promise<HistoryMessage[]> => app.PreviewSession(path).then(asArray<HistoryMessage>, () => []), []);
31 const deleteSession = useCallback((path: string) => app.DeleteSession(path).finally(invalidateCache), [invalidateCache]);
32 const restoreSession = useCallback((path: string) => app.RestoreSession(path).finally(invalidateCache), [invalidateCache]);
33 const purgeTrashedSession = useCallback((path: string) => app.PurgeTrashedSession(path).finally(invalidateCache), [invalidateCache]);
34 const renameSession = useCallback((path: string, title: string) => app.RenameSession(path, title).catch(() => {}).finally(invalidateCache), [invalidateCache]);
35 return { openCanonicalSession, opening, previewSession, deleteSession, restoreSession, purgeTrashedSession, renameSession };
36 }
37
37 lines TYPESCRIPT