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

ProjectionTurns.ts

172 lines6.1 KB

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