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

ProjectionThreadMessages.ts

143 lines4.8 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 OrchestrationMessageContext,
13 OrchestrationMessageRole,
14 ThreadId,
15 TurnId,
16 IsoDateTime,
17 TrimmedNonEmptyString,
18} from "@t3tools/contracts";
19import * as Schema from "effect/Schema";
20import * as Context from "effect/Context";
21import * as Struct from "effect/Struct";
22import type * as Option from "effect/Option";
23import type * as Effect from "effect/Effect";
24​
25import type { ProjectionRepositoryError } from "../Errors.ts";
26​
27export const ProjectionThreadMessage = Schema.Struct({
28 messageId: MessageId,
29 threadId: ThreadId,
30 turnId: Schema.NullOr(TurnId),
31 role: OrchestrationMessageRole,
32 text: Schema.String,
33 actualModel: Schema.optional(TrimmedNonEmptyString),
34 attachments: Schema.optional(Schema.Array(ChatAttachment)),
35 context: Schema.optional(OrchestrationMessageContext),
36 isStreaming: Schema.Boolean,
37 createdAt: IsoDateTime,
38 updatedAt: IsoDateTime,
39});
40export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
41​
42export const AppendStreamingProjectionThreadMessage = Schema.Struct(
43 Struct.omit(ProjectionThreadMessage.fields, ["isStreaming"]),
44);
45export type AppendStreamingProjectionThreadMessage =
46 typeof AppendStreamingProjectionThreadMessage.Type;
47​
48export const ListProjectionThreadMessagesInput = Schema.Struct({
49 threadId: ThreadId,
50});
51export type ListProjectionThreadMessagesInput = typeof ListProjectionThreadMessagesInput.Type;
52​
53export const GetProjectionThreadMessageInput = Schema.Struct({
54 messageId: MessageId,
55});
56export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
57​
58export const HasProjectionThreadAssistantMessageInput = Schema.Struct({
59 threadId: ThreadId,
60 turnId: TurnId,
61 streamingOnly: Schema.Boolean,
62});
63export type HasProjectionThreadAssistantMessageInput =
64 typeof HasProjectionThreadAssistantMessageInput.Type;
65​
66export const GetLatestProjectionThreadAssistantMessageInput = Schema.Struct({
67 threadId: ThreadId,
68 turnId: TurnId,
69});
70export type GetLatestProjectionThreadAssistantMessageInput =
71 typeof GetLatestProjectionThreadAssistantMessageInput.Type;
72​
73export const DeleteProjectionThreadMessagesInput = Schema.Struct({
74 threadId: ThreadId,
75});
76export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
77​
78/**
79 * ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
80 */
81export interface ProjectionThreadMessageRepositoryShape {
82 /**
83 * Insert or replace a projected thread message row.
84 *
85 * Upserts by `messageId`.
86 */
87 readonly upsert: (
88 message: ProjectionThreadMessage,
89 ) => Effect.Effect<void, ProjectionRepositoryError>;
90​
91 /** Insert a streaming message or append text to its existing row. */
92 readonly appendStreaming: (
93 message: AppendStreamingProjectionThreadMessage,
94 ) => Effect.Effect<void, ProjectionRepositoryError>;
95​
96 /**
97 * Read a projected thread message by id.
98 */
99 readonly getByMessageId: (
100 input: GetProjectionThreadMessageInput,
101 ) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
102​
103 /**
104 * Check for an assistant message in a turn without hydrating message text.
105 */
106 readonly hasAssistantMessageForTurn: (
107 input: HasProjectionThreadAssistantMessageInput,
108 ) => Effect.Effect<boolean, ProjectionRepositoryError>;
109​
110 /** Read the latest assistant message id for a turn without hydrating its text. */
111 readonly getLatestAssistantMessageIdForTurn: (
112 input: GetLatestProjectionThreadAssistantMessageInput,
113 ) => Effect.Effect<Option.Option<MessageId>, ProjectionRepositoryError>;
114​
115 /**
116 * List projected thread messages for a thread.
117 *
118 * Returned in ascending creation order.
119 */
120 readonly listByThreadId: (
121 input: ListProjectionThreadMessagesInput,
122 ) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
123​
124 /** Read the latest user-message timestamp without loading message bodies. */
125 readonly getLatestUserMessageAt: (
126 input: ListProjectionThreadMessagesInput,
127 ) => Effect.Effect<ProjectionThreadMessage["createdAt"] | null, ProjectionRepositoryError>;
128​
129 /**
130 * Delete projected thread messages by thread.
131 */
132 readonly deleteByThreadId: (
133 input: DeleteProjectionThreadMessagesInput,
134 ) => Effect.Effect<void, ProjectionRepositoryError>;
136​
137/**
138 * ProjectionThreadMessageRepository - Service tag for message projection persistence.
139 */
140export class ProjectionThreadMessageRepository extends Context.Service<
141 ProjectionThreadMessageRepository,
142 ProjectionThreadMessageRepositoryShape
143>()("t3/persistence/Services/ProjectionThreadMessages/ProjectionThreadMessageRepository") {}