Open History to see the last commit
1import {
2 OrchestrationGetFullThreadDiffInput,
3 OrchestrationGetTurnDiffInput,
4 ThreadId,
5} from "@t3tools/contracts";
6import { queryOptions } from "@tanstack/react-query";
7import { Option, Schema } from "effect";
8import { ensureNativeApi } from "../nativeApi";
9
10interface CheckpointDiffQueryInput {
11 threadId: ThreadId | null;
12 fromTurnCount: number | null;
13 toTurnCount: number | null;
14 cacheScope?: string | null;
15 enabled?: boolean;
16}
17
18export const providerQueryKeys = {
19 all: ["providers"] as const,
20 checkpointDiff: (input: CheckpointDiffQueryInput) =>
21 [
22 "providers",
23 "checkpointDiff",
24 input.threadId,
25 input.fromTurnCount,
26 input.toTurnCount,
27 input.cacheScope ?? null,
28 ] as const,
29};
30
31function decodeCheckpointDiffRequest(input: CheckpointDiffQueryInput) {
32 if (input.fromTurnCount === 0) {
33 return Schema.decodeUnknownOption(OrchestrationGetFullThreadDiffInput)({
34 threadId: input.threadId,
35 toTurnCount: input.toTurnCount,
36 }).pipe(Option.map((fields) => ({ kind: "fullThreadDiff" as const, input: fields })));
37 }
38
39 return Schema.decodeUnknownOption(OrchestrationGetTurnDiffInput)({
40 threadId: input.threadId,
41 fromTurnCount: input.fromTurnCount,
42 toTurnCount: input.toTurnCount,
43 }).pipe(Option.map((fields) => ({ kind: "turnDiff" as const, input: fields })));
44}
45
46function asCheckpointErrorMessage(error: unknown): string {
47 if (error instanceof Error) {
48 return error.message;
49 }
50 if (typeof error === "string") {
51 return error;
52 }
53 return "";
54}
55
56function normalizeCheckpointErrorMessage(error: unknown): string {
57 const message = asCheckpointErrorMessage(error).trim();
58 if (message.length === 0) {
59 return "Failed to load checkpoint diff.";
60 }
61
62 const lower = message.toLowerCase();
63 if (lower.includes("not a git repository")) {
64 return "Turn diffs are unavailable because this project is not a git repository.";
65 }
66
67 if (
68 lower.includes("checkpoint unavailable for thread") ||
69 lower.includes("checkpoint invariant violation")
70 ) {
71 const separatorIndex = message.indexOf(":");
72 if (separatorIndex >= 0) {
73 const detail = message.slice(separatorIndex + 1).trim();
74 if (detail.length > 0) {
75 return detail;
76 }
77 }
78 }
79
80 return message;
81}
82
83function isCheckpointTemporarilyUnavailable(error: unknown): boolean {
84 const message = asCheckpointErrorMessage(error).toLowerCase();
85 return (
86 message.includes("exceeds current turn count") ||
87 message.includes("checkpoint is unavailable for turn") ||
88 message.includes("filesystem checkpoint is unavailable")
89 );
90}
91
92export function checkpointDiffQueryOptions(input: CheckpointDiffQueryInput) {
93 const decodedRequest = decodeCheckpointDiffRequest(input);
94
95 return queryOptions({
96 queryKey: providerQueryKeys.checkpointDiff(input),
97 queryFn: async () => {
98 const api = ensureNativeApi();
99 if (!input.threadId || decodedRequest._tag === "None") {
100 throw new Error("Checkpoint diff is unavailable.");
101 }
102 try {
103 if (decodedRequest.value.kind === "fullThreadDiff") {
104 return await api.orchestration.getFullThreadDiff(decodedRequest.value.input);
105 }
106 return await api.orchestration.getTurnDiff(decodedRequest.value.input);
107 } catch (error) {
108 throw new Error(normalizeCheckpointErrorMessage(error), { cause: error });
109 }
110 },
111 enabled: (input.enabled ?? true) && !!input.threadId && decodedRequest._tag === "Some",
112 staleTime: Infinity,
113 retry: (failureCount, error) => {
114 if (isCheckpointTemporarilyUnavailable(error)) {
115 return failureCount < 12;
116 }
117 return failureCount < 3;
118 },
119 retryDelay: (attempt, error) =>
120 isCheckpointTemporarilyUnavailable(error)
121 ? Math.min(5_000, 250 * 2 ** (attempt - 1))
122 : Math.min(1_000, 100 * 2 ** (attempt - 1)),
123 });
124}