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