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

providerReactQuery.ts

139 lines4.3 KB

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