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

ProjectionCheckpoints.ts

95 lines2.9 KB

1/**
2 * ProjectionCheckpointRepository - Projection repository interface for checkpoints.
3 *
4 * Owns persistence operations for projected checkpoint summaries in thread
5 * timelines.
6 *
7 * @module ProjectionCheckpointRepository
8 */
9import {
10 CheckpointRef,
11 IsoDateTime,
12 MessageId,
13 NonNegativeInt,
14 OrchestrationCheckpointFile,
15 OrchestrationCheckpointStatus,
16 ThreadId,
17 TurnId,
18} from "@t3tools/contracts";
19import * as Option from "effect/Option";
20import * as Context from "effect/Context";
21import * as Schema from "effect/Schema";
22import type * as Effect from "effect/Effect";
23​
24import type { ProjectionRepositoryError } from "../Errors.ts";
25​
26export const ProjectionCheckpoint = Schema.Struct({
27 threadId: ThreadId,
28 turnId: TurnId,
29 checkpointTurnCount: NonNegativeInt,
30 checkpointRef: CheckpointRef,
31 status: OrchestrationCheckpointStatus,
32 files: Schema.Array(OrchestrationCheckpointFile),
33 assistantMessageId: Schema.NullOr(MessageId),
34 completedAt: IsoDateTime,
35});
36export type ProjectionCheckpoint = typeof ProjectionCheckpoint.Type;
37​
38export const ListByThreadIdInput = Schema.Struct({
39 threadId: ThreadId,
40});
41export type ListByThreadIdInput = typeof ListByThreadIdInput.Type;
42​
43export const GetByThreadAndTurnCountInput = Schema.Struct({
44 threadId: ThreadId,
45 checkpointTurnCount: NonNegativeInt,
46});
47export type GetByThreadAndTurnCountInput = typeof GetByThreadAndTurnCountInput.Type;
48​
49export const DeleteByThreadIdInput = Schema.Struct({
50 threadId: ThreadId,
51});
52export type DeleteByThreadIdInput = typeof DeleteByThreadIdInput.Type;
53​
54/**
55 * ProjectionCheckpointRepositoryShape - Service API for projected checkpoints.
56 */
57export interface ProjectionCheckpointRepositoryShape {
58 /**
59 * Insert or replace a projected checkpoint row.
60 *
61 * Upserts by composite key `(threadId, checkpointTurnCount)`.
62 */
63 readonly upsert: (row: ProjectionCheckpoint) => Effect.Effect<void, ProjectionRepositoryError>;
64​
65 /**
66 * List projected checkpoints for a thread.
67 *
68 * Returned in ascending checkpoint turn-count order.
69 */
70 readonly listByThreadId: (
71 input: ListByThreadIdInput,
72 ) => Effect.Effect<ReadonlyArray<ProjectionCheckpoint>, ProjectionRepositoryError>;
73​
74 /**
75 * Read a projected checkpoint by thread and turn-count key.
76 */
77 readonly getByThreadAndTurnCount: (
78 input: GetByThreadAndTurnCountInput,
79 ) => Effect.Effect<Option.Option<ProjectionCheckpoint>, ProjectionRepositoryError>;
80​
81 /**
82 * Delete projected checkpoint rows by thread.
83 */
84 readonly deleteByThreadId: (
85 input: DeleteByThreadIdInput,
86 ) => Effect.Effect<void, ProjectionRepositoryError>;
88​
89/**
90 * ProjectionCheckpointRepository - Service tag for checkpoint projection persistence.
91 */
92export class ProjectionCheckpointRepository extends Context.Service<
93 ProjectionCheckpointRepository,
94 ProjectionCheckpointRepositoryShape
95>()("t3/persistence/Services/ProjectionCheckpoints/ProjectionCheckpointRepository") {}