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

threadCheckpoints.ts

68 lines2.3 KB

1import type {
2 CheckpointId,
3 CheckpointRef,
4 CheckpointScopeId,
5 MessageId,
6 OrchestrationV2CheckpointFileTotals,
7 OrchestrationV2ThreadProjection,
8 RunId,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11​
12export interface ThreadCheckpointSummary {
13 readonly checkpointId?: CheckpointId;
14 readonly scopeId?: CheckpointScopeId;
15 readonly runId: RunId;
16 readonly checkpointTurnCount: number;
17 readonly checkpointRef: CheckpointRef;
18 readonly status: "ready" | "missing" | "error" | "stale";
19 /** A prefix of the changed files when `filesTotal` is set; see `checkpointFileTotals`. */
20 readonly files: ReadonlyArray<{
21 readonly path: string;
22 readonly kind: string;
23 readonly additions: number;
24 readonly deletions: number;
25 }>;
26 readonly filesTotal?: OrchestrationV2CheckpointFileTotals;
27 readonly assistantMessageId: MessageId | null;
28 readonly completedAt: string;
30​
31export function deriveThreadCheckpointSummaries(
32 projection: Pick<OrchestrationV2ThreadProjection, "checkpoints" | "messages">,
33): ReadonlyArray<ThreadCheckpointSummary> {
34 return projection.checkpoints.flatMap((checkpoint) => {
35 if (checkpoint.appRunOrdinal === null || checkpoint.runId === null) return [];
36 const assistantMessageId =
37 projection.messages.findLast(
38 (message) => message.runId === checkpoint.runId && message.role === "assistant",
39 )?.id ?? null;
40 return [
41 {
42 checkpointId: checkpoint.id,
43 scopeId: checkpoint.scopeId,
44 runId: checkpoint.runId,
45 checkpointTurnCount: checkpoint.appRunOrdinal,
46 checkpointRef: checkpoint.ref,
47 status: checkpoint.status,
48 files: checkpoint.files,
49 ...(checkpoint.filesTotal === undefined ? {} : { filesTotal: checkpoint.filesTotal }),
50 assistantMessageId,
51 completedAt: DateTime.formatIso(checkpoint.capturedAt),
52 },
53 ];
54 });
56​
57export function checkpointFileTotals(
58 checkpoint: Pick<ThreadCheckpointSummary, "files" | "filesTotal">,
59): OrchestrationV2CheckpointFileTotals {
60 if (checkpoint.filesTotal !== undefined) return checkpoint.filesTotal;
61 let additions = 0;
62 let deletions = 0;
63 for (const file of checkpoint.files) {
64 additions += file.additions;
65 deletions += file.deletions;
66 }
67 return { count: checkpoint.files.length, additions, deletions };