@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 41 lines 1.47 KB
Raw
1 import type * as CodexSchema from "effect-codex-app-server/schema";
2 import type * as CodexClient from "effect-codex-app-server/client";
3 import * as CodexErrors from "effect-codex-app-server/errors";
4 import * as Effect from "effect/Effect";
5
6 export const revertCodexThread = Effect.fn("revertCodexThread")(function* (
7 client: Pick<CodexClient.CodexAppServerClient["Service"], "request">,
8 threadId: string,
9 numTurns: number,
10 ) {
11 let remaining = numTurns;
12 let beforeTurnId: string | undefined;
13 let cursor: string | null = null;
14 const visited = new Set<string | null>();
15 while (remaining > 0) {
16 if (visited.has(cursor)) {
17 return yield* CodexErrors.CodexAppServerRequestError.internalError(
18 "Thread history pagination repeated a cursor.",
19 undefined,
20 { method: "thread/turns/list", operation: "decode-payload" },
21 );
22 }
23 visited.add(cursor);
24 const page: CodexSchema.V2ThreadTurnsListResponse = yield* client.request("thread/turns/list", {
25 threadId,
26 cursor,
27 limit: Math.min(remaining, 100),
28 sortDirection: "desc",
29 itemsView: "summary",
30 });
31 for (const turn of page.data) {
32 beforeTurnId = turn.id;
33 if (--remaining === 0) break;
34 }
35 cursor = page.nextCursor ?? null;
36 if (cursor === null) break;
37 }
38 return beforeTurnId === undefined
39 ? yield* client.request("thread/read", { threadId, includeTurns: false })
40 : yield* client.request("thread/revert", { threadId, beforeTurnId });
41 });