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

threadCheckpoints.ts

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