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

Utils.ts

40 lines1.4 KB

1import * as Encoding from "effect/Encoding";
2import { CheckpointRef, ProjectId, type ThreadId } from "@t3tools/contracts";
3​
4export const CHECKPOINT_REFS_PREFIX = "refs/t3/checkpoints";
5​
6export function checkpointRefForThreadTurn(threadId: ThreadId, turnCount: number): CheckpointRef {
7 return CheckpointRef.make(
8 `${CHECKPOINT_REFS_PREFIX}/${Encoding.encodeBase64Url(threadId)}/turn/${turnCount}`,
9 );
11​
12/**
13 * Working-tree snapshot taken right before a revert so a failed provider
14 * rollback can be compensated back to the exact pre-revert state — prior
15 * checkpoints can miss uncommitted local edits. One per thread; the capture
16 * overwrites it on each revert attempt via `git update-ref`.
17 */
18export function checkpointRefForRevertBase(threadId: ThreadId): CheckpointRef {
19 return CheckpointRef.make(
20 `${CHECKPOINT_REFS_PREFIX}/${Encoding.encodeBase64Url(threadId)}/revert-base`,
21 );
23​
24export function resolveThreadWorkspaceCwd(input: {
25 readonly thread: {
26 readonly projectId: ProjectId;
27 readonly worktreePath: string | null;
28 };
29 readonly projects: ReadonlyArray<{
30 readonly id: ProjectId;
31 readonly workspaceRoot: string;
32 }>;
33}): string | undefined {
34 const worktreeCwd = input.thread.worktreePath ?? undefined;
35 if (worktreeCwd) {
36 return worktreeCwd;
37 }
38​
39 return input.projects.find((project) => project.id === input.thread.projectId)?.workspaceRoot;