Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import { MessageId, NonNegativeInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
4
5export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
6export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
7export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
8export const PROVIDER_SEND_TURN_MAX_FILE_BYTES = 50 * 1024 * 1024;
9export const PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES = [
10 "image/gif",
11 "image/jpeg",
12 "image/png",
13 "image/webp",
14] as const;
15const PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPE_SET = new Set<string>(
16 PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES,
17);
18
19/** Whether a pasted or picked image mime type can be sent on a provider turn. */
20export function isProviderSendTurnSupportedImageMimeType(mimeType: string): boolean {
21 return PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPE_SET.has(mimeType.toLowerCase());
22}
23const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
24const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
25
26export const ChatAttachmentId = TrimmedNonEmptyString.check(
27 Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
28 Schema.isPattern(/^[a-z0-9_-]+$/i),
29);
30export type ChatAttachmentId = typeof ChatAttachmentId.Type;
31
32export const ChatImageAttachment = Schema.Struct({
33 type: Schema.Literal("image"),
34 id: ChatAttachmentId,
35 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
36 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
37 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
38});
39export type ChatImageAttachment = typeof ChatImageAttachment.Type;
40
41export const ChatFileAttachment = Schema.Struct({
42 type: Schema.Literal("file"),
43 id: ChatAttachmentId,
44 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
45 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
46 sizeBytes: NonNegativeInt.check(
47 Schema.isGreaterThanOrEqualTo(1),
48 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
49 ),
50});
51export type ChatFileAttachment = typeof ChatFileAttachment.Type;
52
53/**
54 * Catch-all for attachment types this build does not know. Attachments ride on
55 * persisted events and thread streams, so a newer server or client must be able
56 * to introduce a type without making older readers fail to decode the whole
57 * message. Decoders keep the shared base fields; consumers skip these or render
58 * them as unsupported. Mirrors how `OrchestrationThreadActivity` keeps `kind`
59 * open. The known discriminators are excluded so a malformed image or file
60 * attachment fails its own schema instead of sliding through here with its
61 * size and mime constraints unchecked.
62 */
63export const ChatUnknownAttachment = Schema.Struct({
64 type: TrimmedNonEmptyString.check(
65 Schema.isMaxLength(50),
66 Schema.isPattern(/^(?!(?:image|file)$)/),
67 ),
68 id: ChatAttachmentId,
69 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
70 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
71 sizeBytes: NonNegativeInt,
72});
73export type ChatUnknownAttachment = typeof ChatUnknownAttachment.Type;
74
75export const UploadChatImageAttachment = Schema.Struct({
76 type: Schema.Literal("image"),
77 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
78 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
79 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
80 dataUrl: TrimmedNonEmptyString.check(
81 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
82 ),
83});
84export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
85
86export const ChatAttachment = Schema.Union([
87 ChatImageAttachment,
88 ChatFileAttachment,
89 ChatUnknownAttachment,
90]);
91export type ChatAttachment = typeof ChatAttachment.Type;
92
93export const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
94export type UploadChatAttachment = typeof UploadChatAttachment.Type;
95
96export const PersistChatAttachmentsInput = Schema.Struct({
97 threadId: ThreadId,
98 messageId: MessageId,
99 attachments: Schema.Array(UploadChatAttachment),
100});
101export type PersistChatAttachmentsInput = typeof PersistChatAttachmentsInput.Type;
102
103export const PersistChatAttachmentsResult = Schema.Struct({
104 attachments: Schema.Array(ChatAttachment),
105});
106export type PersistChatAttachmentsResult = typeof PersistChatAttachmentsResult.Type;
107
108export class PersistChatAttachmentsError extends Schema.TaggedErrorClass<PersistChatAttachmentsError>()(
109 "PersistChatAttachmentsError",
110 {
111 message: Schema.String,
112 cause: Schema.optional(Schema.Defect()),
113 },
114) {}