Open History to see the last commit
1/**
2 * ProjectionTurnRepository - Projection repository interface for unified turn state.
3 *
4 * Owns persistence operations for pending starts, running/completed turn lifecycle,
5 * and checkpoint metadata in a single projection table.
6 *
7 * @module ProjectionTurnRepository
8 */
9import {
10 CheckpointRef,
11 IsoDateTime,
12 MessageId,
13 NonNegativeInt,
14 OrchestrationCheckpointFile,
15 OrchestrationCheckpointStatus,
16 ThreadId,
17 TurnId,
18} from "@t3tools/contracts";
19import { Option, Schema, ServiceMap } from "effect";
20import type { Effect } from "effect";
21
22import type { ProjectionRepositoryError } from "../Errors.ts";
23
24export const ProjectionTurnState = Schema.Literals([
25 "pending",
26 "running",
27 "interrupted",
28 "completed",
29 "error",
30]);
31export type ProjectionTurnState = typeof ProjectionTurnState.Type;
32
33export const ProjectionTurn = Schema.Struct({
34 threadId: ThreadId,
35 turnId: Schema.NullOr(TurnId),
36 pendingMessageId: Schema.NullOr(MessageId),
37 assistantMessageId: Schema.NullOr(MessageId),
38 state: ProjectionTurnState,
39 requestedAt: IsoDateTime,
40 startedAt: Schema.NullOr(IsoDateTime),
41 completedAt: Schema.NullOr(IsoDateTime),
42 checkpointTurnCount: Schema.NullOr(NonNegativeInt),
43 checkpointRef: Schema.NullOr(CheckpointRef),
44 checkpointStatus: Schema.NullOr(OrchestrationCheckpointStatus),
45 checkpointFiles: Schema.Array(OrchestrationCheckpointFile),
46});
47export type ProjectionTurn = typeof ProjectionTurn.Type;
48
49export const ProjectionTurnById = Schema.Struct({
50 threadId: ThreadId,
51 turnId: TurnId,
52 pendingMessageId: Schema.NullOr(MessageId),
53 assistantMessageId: Schema.NullOr(MessageId),
54 state: ProjectionTurnState,
55 requestedAt: IsoDateTime,
56 startedAt: Schema.NullOr(IsoDateTime),
57 completedAt: Schema.NullOr(IsoDateTime),
58 checkpointTurnCount: Schema.NullOr(NonNegativeInt),
59 checkpointRef: Schema.NullOr(CheckpointRef),
60 checkpointStatus: Schema.NullOr(OrchestrationCheckpointStatus),
61 checkpointFiles: Schema.Array(OrchestrationCheckpointFile),
62});
63export type ProjectionTurnById = typeof ProjectionTurnById.Type;
64
65export const ProjectionPendingTurnStart = Schema.Struct({
66 threadId: ThreadId,
67 messageId: MessageId,
68 requestedAt: IsoDateTime,
69});
70export type ProjectionPendingTurnStart = typeof ProjectionPendingTurnStart.Type;
71
72export const ListProjectionTurnsByThreadInput = Schema.Struct({
73 threadId: ThreadId,
74});
75export type ListProjectionTurnsByThreadInput = typeof ListProjectionTurnsByThreadInput.Type;
76
77export const GetProjectionTurnByTurnIdInput = Schema.Struct({
78 threadId: ThreadId,
79 turnId: TurnId,
80});
81export type GetProjectionTurnByTurnIdInput = typeof GetProjectionTurnByTurnIdInput.Type;
82
83export const GetProjectionPendingTurnStartInput = Schema.Struct({
84 threadId: ThreadId,
85});
86export type GetProjectionPendingTurnStartInput = typeof GetProjectionPendingTurnStartInput.Type;
87
88export const DeleteProjectionTurnsByThreadInput = Schema.Struct({
89 threadId: ThreadId,
90});
91export type DeleteProjectionTurnsByThreadInput = typeof DeleteProjectionTurnsByThreadInput.Type;
92
93export const ClearCheckpointTurnConflictInput = Schema.Struct({
94 threadId: ThreadId,
95 turnId: TurnId,
96 checkpointTurnCount: NonNegativeInt,
97});
98export type ClearCheckpointTurnConflictInput = typeof ClearCheckpointTurnConflictInput.Type;
99
100export interface ProjectionTurnRepositoryShape {
101 /**
102 * Inserts or updates the canonical row for a concrete `{threadId, turnId}` turn lifecycle state.
103 */
104 readonly upsertByTurnId: (
105 row: ProjectionTurnById,
106 ) => Effect.Effect<void, ProjectionRepositoryError>;
107
108 /**
109 * Replaces any existing pending-start placeholder rows for a thread with exactly one latest pending-start row.
110 */
111 readonly replacePendingTurnStart: (
112 row: ProjectionPendingTurnStart,
113 ) => Effect.Effect<void, ProjectionRepositoryError>;
114
115 /**
116 * Returns the newest pending-start placeholder for a thread; this is expected to be at most one row after replacement writes.
117 */
118 readonly getPendingTurnStartByThreadId: (
119 input: GetProjectionPendingTurnStartInput,
120 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
121
122 /**
123 * Deletes only pending-start placeholder rows (`turnId = null`) for a thread and leaves concrete turn rows untouched.
124 */
125 readonly deletePendingTurnStartByThreadId: (
126 input: GetProjectionPendingTurnStartInput,
127 ) => Effect.Effect<void, ProjectionRepositoryError>;
128
129 /**
130 * Lists all projection rows for a thread, including pending placeholders, with checkpoint rows ordered before non-checkpoint rows.
131 */
132 readonly listByThreadId: (
133 input: ListProjectionTurnsByThreadInput,
134 ) => Effect.Effect<ReadonlyArray<ProjectionTurn>, ProjectionRepositoryError>;
135
136 /**
137 * Looks up a concrete turn row by `{threadId, turnId}` and never returns pending placeholder rows.
138 */
139 readonly getByTurnId: (
140 input: GetProjectionTurnByTurnIdInput,
141 ) => Effect.Effect<Option.Option<ProjectionTurnById>, ProjectionRepositoryError>;
142
143 /**
144 * Clears checkpoint fields on conflicting rows that reuse the same checkpoint turn count in a thread, excluding the provided turn.
145 */
146 readonly clearCheckpointTurnConflict: (
147 input: ClearCheckpointTurnConflictInput,
148 ) => Effect.Effect<void, ProjectionRepositoryError>;
149
150 /**
151 * Hard-deletes all projection rows for a thread, including pending-start placeholders and checkpoint metadata rows.
152 */
153 readonly deleteByThreadId: (
154 input: DeleteProjectionTurnsByThreadInput,
155 ) => Effect.Effect<void, ProjectionRepositoryError>;
156}
157
158export class ProjectionTurnRepository extends ServiceMap.Service<
159 ProjectionTurnRepository,
160 ProjectionTurnRepositoryShape
161>()("t3/persistence/Services/ProjectionTurns/ProjectionTurnRepository") {}