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

providerReactQuery.ts

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