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

ProjectionThreadMessages.ts

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