Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerReactQuery.ts

135 lines4.2 KB

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