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

ProjectionThreadMessages.ts

95 lines2.9 KB

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});
35export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
36​
37export const ListProjectionThreadMessagesInput = Schema.Struct({
38 threadId: ThreadId,
39});
40export type ListProjectionThreadMessagesInput = typeof ListProjectionThreadMessagesInput.Type;
41​
42export const GetProjectionThreadMessageInput = Schema.Struct({
43 messageId: MessageId,
44});
45export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
46​
47export const DeleteProjectionThreadMessagesInput = Schema.Struct({
48 threadId: ThreadId,
49});
50export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
51​
52/**
53 * ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
54 */
55export interface ProjectionThreadMessageRepositoryShape {
56 /**
57 * Insert or replace a projected thread message row.
58 *
59 * Upserts by `messageId`.
60 */
61 readonly upsert: (
62 message: ProjectionThreadMessage,
63 ) => Effect.Effect<void, ProjectionRepositoryError>;
64​
65 /**
66 * Read a projected thread message by id.
67 */
68 readonly getByMessageId: (
69 input: GetProjectionThreadMessageInput,
70 ) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
71​
72 /**
73 * List projected thread messages for a thread.
74 *
75 * Returned in ascending creation order.
76 */
77 readonly listByThreadId: (
78 input: ListProjectionThreadMessagesInput,
79 ) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
80​
81 /**
82 * Delete projected thread messages by thread.
83 */
84 readonly deleteByThreadId: (
85 input: DeleteProjectionThreadMessagesInput,
86 ) => Effect.Effect<void, ProjectionRepositoryError>;
88​
89/**
90 * ProjectionThreadMessageRepository - Service tag for message projection persistence.
91 */
92export class ProjectionThreadMessageRepository extends Context.Service<
93 ProjectionThreadMessageRepository,
94 ProjectionThreadMessageRepositoryShape
95>()("t3/persistence/Services/ProjectionThreadMessages/ProjectionThreadMessageRepository") {}