Open History to see the last commit
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
12import { deriveTurnRepositoryChanges, type TurnRepositoryChanges } from "./threadRepositories.ts";
13
14export interface ThreadCheckpointSummary {
15 readonly checkpointId?: CheckpointId;
16 readonly scopeId?: CheckpointScopeId;
17 readonly runId: RunId;
18 readonly checkpointTurnCount: number;
19 readonly checkpointRef: CheckpointRef;
20 readonly status: "ready" | "missing" | "error" | "stale";
21 /** A prefix of the changed files when `filesTotal` is set; see `changedFileTotals`. */
22 readonly files: ReadonlyArray<{
23 readonly path: string;
24 readonly kind: string;
25 readonly additions: number;
26 readonly deletions: number;
27 }>;
28 readonly filesTotal?: OrchestrationV2CheckpointFileTotals;
29 /**
30 * Tracked repositories this turn changed, by name; set only when there are any. `files` above
31 * stays the thread folder's own, so a turn can have repositories while `status` is `missing`.
32 */
33 readonly repositories?: ReadonlyArray<TurnRepositoryChanges>;
34 readonly assistantMessageId: MessageId | null;
35 readonly completedAt: string;
36}
37
38export function deriveThreadCheckpointSummaries(
39 projection: Pick<OrchestrationV2ThreadProjection, "checkpoints" | "messages"> &
40 Partial<Pick<OrchestrationV2ThreadProjection, "checkpointScopes">>,
41): ReadonlyArray<ThreadCheckpointSummary> {
42 const repositoriesByRun =
43 projection.checkpointScopes === undefined
44 ? undefined
45 : deriveTurnRepositoryChanges({
46 checkpoints: projection.checkpoints,
47 checkpointScopes: projection.checkpointScopes,
48 });
49 let assistantMessageIdsByRun: Map<RunId, MessageId | null | undefined> | undefined;
50 let nextMessageIndex = -1;
51 return projection.checkpoints.flatMap((checkpoint) => {
52 if (checkpoint.appRunOrdinal === null || checkpoint.runId === null) return [];
53 const messages = projection.messages;
54 if (assistantMessageIdsByRun === undefined) {
55 assistantMessageIdsByRun = new Map();
56 for (const entry of projection.checkpoints) {
57 if (entry.appRunOrdinal !== null && entry.runId !== null) {
58 assistantMessageIdsByRun.set(entry.runId, undefined);
59 }
60 }
61 nextMessageIndex = messages.length - 1;
62 }
63 // Resume the reverse scan only as far as this checkpoint needs. Recent
64 // checkpoints must not force a traversal of unrelated older history.
65 while (nextMessageIndex >= 0 && assistantMessageIdsByRun.get(checkpoint.runId) === undefined) {
66 const message = messages[nextMessageIndex--]!;
67 const { runId, role } = message;
68 if (
69 role === "assistant" &&
70 runId !== null &&
71 assistantMessageIdsByRun.has(runId) &&
72 assistantMessageIdsByRun.get(runId) === undefined
73 ) {
74 assistantMessageIdsByRun.set(runId, message.id ?? null);
75 }
76 }
77 const assistantMessageId = assistantMessageIdsByRun.get(checkpoint.runId) ?? null;
78 const repositories = repositoriesByRun?.get(checkpoint.runId);
79 return [
80 {
81 checkpointId: checkpoint.id,
82 scopeId: checkpoint.scopeId,
83 runId: checkpoint.runId,
84 checkpointTurnCount: checkpoint.appRunOrdinal,
85 checkpointRef: checkpoint.ref,
86 status: checkpoint.status,
87 files: checkpoint.files,
88 ...(checkpoint.filesTotal === undefined ? {} : { filesTotal: checkpoint.filesTotal }),
89 ...(repositories === undefined ? {} : { repositories }),
90 assistantMessageId,
91 completedAt: DateTime.formatIso(checkpoint.capturedAt),
92 },
93 ];
94 });
95}