Open History to see the last commit
1/**
2 * ProjectionThreadSessionRepository - Repository interface for thread sessions.
3 *
4 * Owns persistence operations for projected provider-session linkage and
5 * runtime status for each thread.
6 *
7 * @module ProjectionThreadSessionRepository
8 */
9import {
10 RuntimeMode,
11 IsoDateTime,
12 OrchestrationSessionStatus,
13 ProviderInstanceId,
14 ThreadId,
15 TurnId,
16} from "@t3tools/contracts";
17import * as Option from "effect/Option";
18import * as Schema from "effect/Schema";
19import * as Context from "effect/Context";
20import type * as Effect from "effect/Effect";
21
22import type { ProjectionRepositoryError } from "../Errors.ts";
23
24export const ProjectionThreadSession = Schema.Struct({
25 threadId: ThreadId,
26 status: OrchestrationSessionStatus,
27 providerName: Schema.NullOr(Schema.String),
28 providerInstanceId: Schema.NullOr(ProviderInstanceId),
29 runtimeMode: RuntimeMode,
30 activeTurnId: Schema.NullOr(TurnId),
31 lastError: Schema.NullOr(Schema.String),
32 lastErrorKind: Schema.optional(Schema.NullOr(Schema.Literals(["usage_limit"]))),
33 lastErrorResetsAt: Schema.optional(Schema.NullOr(IsoDateTime)),
34 updatedAt: IsoDateTime,
35});
36export type ProjectionThreadSession = typeof ProjectionThreadSession.Type;
37
38export const GetProjectionThreadSessionInput = Schema.Struct({
39 threadId: ThreadId,
40});
41export type GetProjectionThreadSessionInput = typeof GetProjectionThreadSessionInput.Type;
42
43export const DeleteProjectionThreadSessionInput = Schema.Struct({
44 threadId: ThreadId,
45});
46export type DeleteProjectionThreadSessionInput = typeof DeleteProjectionThreadSessionInput.Type;
47
48/**
49 * ProjectionThreadSessionRepositoryShape - Service API for projected thread sessions.
50 */
51export interface ProjectionThreadSessionRepositoryShape {
52 /**
53 * Insert or replace a projected thread-session row.
54 *
55 * Upserts by `threadId`.
56 */
57 readonly upsert: (row: ProjectionThreadSession) => Effect.Effect<void, ProjectionRepositoryError>;
58
59 /**
60 * Read projected thread-session state by thread id.
61 */
62 readonly getByThreadId: (
63 input: GetProjectionThreadSessionInput,
64 ) => Effect.Effect<Option.Option<ProjectionThreadSession>, ProjectionRepositoryError>;
65
66 /**
67 * Delete projected thread-session state by thread id.
68 */
69 readonly deleteByThreadId: (
70 input: DeleteProjectionThreadSessionInput,
71 ) => Effect.Effect<void, ProjectionRepositoryError>;
72}
73
74/**
75 * ProjectionThreadSessionRepository - Service tag for thread-session persistence.
76 */
77export class ProjectionThreadSessionRepository extends Context.Service<
78 ProjectionThreadSessionRepository,
79 ProjectionThreadSessionRepositoryShape
80>()("t3/persistence/Services/ProjectionThreadSessions/ProjectionThreadSessionRepository") {}