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