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

ProjectionThreadMessages.ts

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