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

threadCheckpoints.ts

51 lines1.6 KB

1import type {
2 CheckpointId,
3 CheckpointRef,
4 CheckpointScopeId,
5 MessageId,
6 OrchestrationV2ThreadProjection,
7 RunId,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10​
11export interface ThreadCheckpointSummary {
12 readonly checkpointId?: CheckpointId;
13 readonly scopeId?: CheckpointScopeId;
14 readonly runId: RunId;
15 readonly checkpointTurnCount: number;
16 readonly checkpointRef: CheckpointRef;
17 readonly status: "ready" | "missing" | "error" | "stale";
18 readonly files: ReadonlyArray<{
19 readonly path: string;
20 readonly kind: string;
21 readonly additions: number;
22 readonly deletions: number;
23 }>;
24 readonly assistantMessageId: MessageId | null;
25 readonly completedAt: string;
27​
28export function deriveThreadCheckpointSummaries(
29 projection: Pick<OrchestrationV2ThreadProjection, "checkpoints" | "messages">,
30): ReadonlyArray<ThreadCheckpointSummary> {
31 return projection.checkpoints.flatMap((checkpoint) => {
32 if (checkpoint.appRunOrdinal === null || checkpoint.runId === null) return [];
33 const assistantMessageId =
34 projection.messages.findLast(
35 (message) => message.runId === checkpoint.runId && message.role === "assistant",
36 )?.id ?? null;
37 return [
38 {
39 checkpointId: checkpoint.id,
40 scopeId: checkpoint.scopeId,
41 runId: checkpoint.runId,
42 checkpointTurnCount: checkpoint.appRunOrdinal,
43 checkpointRef: checkpoint.ref,
44 status: checkpoint.status,
45 files: checkpoint.files,
46 assistantMessageId,
47 completedAt: DateTime.formatIso(checkpoint.capturedAt),
48 },
49 ];
50 });