Files
t3code/apps/server/src/checkpointing/Utils.ts
T
2026-02-26 13:20:36 -08:00

37 lines
1.1 KiB
TypeScript

import { Encoding } from "effect";
import { CheckpointRef, ProjectId, type ThreadId } from "@t3tools/contracts";
export const CHECKPOINT_REFS_PREFIX = "refs/t3/checkpoints";
export function checkpointRefForThreadTurn(threadId: ThreadId, turnCount: number): CheckpointRef {
return CheckpointRef.makeUnsafe(
`${CHECKPOINT_REFS_PREFIX}/${Encoding.encodeBase64Url(threadId)}/turn/${turnCount}`,
);
}
function trimToNonEmpty(value: string | null | undefined): string | undefined {
const normalized = value?.trim();
return normalized && normalized.length > 0 ? normalized : undefined;
}
export function resolveThreadWorkspaceCwd(input: {
readonly thread: {
readonly projectId: ProjectId;
readonly worktreePath: string | null;
};
readonly projects: ReadonlyArray<{
readonly id: ProjectId;
readonly workspaceRoot: string;
}>;
}): string | undefined {
const worktreeCwd = trimToNonEmpty(input.thread.worktreePath);
if (worktreeCwd) {
return worktreeCwd;
}
const workspaceRoot = input.projects.find(
(project) => project.id === input.thread.projectId,
)?.workspaceRoot;
return trimToNonEmpty(workspaceRoot);
}