Open History to see the last commit
1/**
2 * ProjectionThreadMessageRepository - Projection repository interface for messages.
3 *
4 * Owns persistence operations for projected thread messages rendered in the
5 * orchestration read model.
6 *
7 * @module ProjectionThreadMessageRepository
8 */
9import {
10 ChatAttachment,
11 MessageId,
12 OrchestrationMessageRole,
13 ThreadId,
14 TurnId,
15 IsoDateTime,
16} from "@t3tools/contracts";
17import * as Schema from "effect/Schema";
18import * as Context from "effect/Context";
19import type * as Option from "effect/Option";
20import type * as Effect from "effect/Effect";
21
22import type { ProjectionRepositoryError } from "../Errors.ts";
23
24export const ProjectionThreadMessage = Schema.Struct({
25 messageId: MessageId,
26 threadId: ThreadId,
27 turnId: Schema.NullOr(TurnId),
28 role: OrchestrationMessageRole,
29 text: Schema.String,
30 attachments: Schema.optional(Schema.Array(ChatAttachment)),
31 isStreaming: Schema.Boolean,
32 createdAt: IsoDateTime,
33 updatedAt: IsoDateTime,
34 deliveryState: Schema.optional(Schema.Literal("queued")),
35});
36export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
37
38export const ListProjectionThreadMessagesInput = Schema.Struct({
39 threadId: ThreadId,
40});
41export type ListProjectionThreadMessagesInput = typeof ListProjectionThreadMessagesInput.Type;
42
43export const GetProjectionThreadMessageInput = Schema.Struct({
44 messageId: MessageId,
45});
46export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
47
48export const DeleteProjectionThreadMessagesInput = Schema.Struct({
49 threadId: ThreadId,
50});
51export const SetProjectionThreadMessageDeliveryStateInput = Schema.Struct({
52 messageId: MessageId,
53 deliveryState: Schema.NullOr(Schema.Literal("queued")),
54});
55export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
56
57/**
58 * ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
59 */
60export interface ProjectionThreadMessageRepositoryShape {
61 /**
62 * Insert or replace a projected thread message row.
63 *
64 * Upserts by `messageId`.
65 */
66 readonly upsert: (
67 message: ProjectionThreadMessage,
68 ) => Effect.Effect<void, ProjectionRepositoryError>;
69
70 /**
71 * Read a projected thread message by id.
72 */
73 readonly getByMessageId: (
74 input: GetProjectionThreadMessageInput,
75 ) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
76
77 readonly setDeliveryState: (
78 input: typeof SetProjectionThreadMessageDeliveryStateInput.Type,
79 ) => Effect.Effect<void, ProjectionRepositoryError>;
80
81 readonly deleteByMessageId: (
82 input: GetProjectionThreadMessageInput,
83 ) => Effect.Effect<void, ProjectionRepositoryError>;
84
85 /**
86 * List projected thread messages for a thread.
87 *
88 * Returned in ascending creation order.
89 */
90 readonly listByThreadId: (
91 input: ListProjectionThreadMessagesInput,
92 ) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
93
94 /**
95 * Delete projected thread messages by thread.
96 */
97 readonly deleteByThreadId: (
98 input: DeleteProjectionThreadMessagesInput,
99 ) => Effect.Effect<void, ProjectionRepositoryError>;
100}
101
102/**
103 * ProjectionThreadMessageRepository - Service tag for message projection persistence.
104 */
105export class ProjectionThreadMessageRepository extends Context.Service<
106 ProjectionThreadMessageRepository,
107 ProjectionThreadMessageRepositoryShape
108>()("t3/persistence/Services/ProjectionThreadMessages/ProjectionThreadMessageRepository") {}