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

ProjectionThreadMessages.ts

89 lines3.0 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 { ChatAttachment, MessageId, ThreadId, TurnId, IsoDateTime } from "@t3tools/contracts";
10import { OrchestrationMessageRole } from "@t3tools/contracts/legacy-orchestration";
11import * as Schema from "effect/Schema";
12import * as Context from "effect/Context";
13import type * as Option from "effect/Option";
14import type * as Effect from "effect/Effect";
15​
16import type { ProjectionRepositoryError } from "../Errors.ts";
17​
18export const ProjectionThreadMessage = Schema.Struct({
19 messageId: MessageId,
20 threadId: ThreadId,
21 turnId: Schema.NullOr(TurnId),
22 role: OrchestrationMessageRole,
23 text: Schema.String,
24 attachments: Schema.optional(Schema.Array(ChatAttachment)),
25 isStreaming: Schema.Boolean,
26 createdAt: IsoDateTime,
27 updatedAt: IsoDateTime,
28});
29export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
30​
31export const ListProjectionThreadMessagesInput = Schema.Struct({
32 threadId: ThreadId,
33});
34export type ListProjectionThreadMessagesInput = typeof ListProjectionThreadMessagesInput.Type;
35​
36export const GetProjectionThreadMessageInput = Schema.Struct({
37 messageId: MessageId,
38});
39export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
40​
41export const DeleteProjectionThreadMessagesInput = Schema.Struct({
42 threadId: ThreadId,
43});
44export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
45​
46/**
47 * ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
48 */
49export interface ProjectionThreadMessageRepositoryShape {
50 /**
51 * Insert or replace a projected thread message row.
52 *
53 * Upserts by `messageId`.
54 */
55 readonly upsert: (
56 message: ProjectionThreadMessage,
57 ) => Effect.Effect<void, ProjectionRepositoryError>;
58​
59 /**
60 * Read a projected thread message by id.
61 */
62 readonly getByMessageId: (
63 input: GetProjectionThreadMessageInput,
64 ) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
65​
66 /**
67 * List projected thread messages for a thread.
68 *
69 * Returned in ascending creation order.
70 */
71 readonly listByThreadId: (
72 input: ListProjectionThreadMessagesInput,
73 ) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
74​
75 /**
76 * Delete projected thread messages by thread.
77 */
78 readonly deleteByThreadId: (
79 input: DeleteProjectionThreadMessagesInput,
80 ) => Effect.Effect<void, ProjectionRepositoryError>;
82​
83/**
84 * ProjectionThreadMessageRepository - Service tag for message projection persistence.
85 */
86export class ProjectionThreadMessageRepository extends Context.Service<
87 ProjectionThreadMessageRepository,
88 ProjectionThreadMessageRepositoryShape
89>()("t3/persistence/Services/ProjectionThreadMessages/ProjectionThreadMessageRepository") {}