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

chatAttachment.ts

232 lines8.8 KB

1import * as Schema from "effect/Schema";
2​
3import {
4 IsoDateTime,
5 MessageId,
6 NonNegativeInt,
7 PositiveInt,
8 ThreadId,
9 TrimmedNonEmptyString,
10 TrimmedString,
11} from "./baseSchemas.ts";
12​
13export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
14export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
15export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
16export const PROVIDER_SEND_TURN_MAX_FILE_BYTES = 50 * 1024 * 1024;
17export const PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES = [
18 "image/gif",
19 "image/jpeg",
20 "image/png",
21 "image/webp",
22] as const;
23const PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPE_SET = new Set<string>(
24 PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES,
25);
26​
27/** Whether a pasted or picked image mime type can be sent on a provider turn. */
28export function isProviderSendTurnSupportedImageMimeType(mimeType: string): boolean {
29 return PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPE_SET.has(mimeType.toLowerCase());
31const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
32const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
33​
34export const ChatAttachmentId = TrimmedNonEmptyString.check(
35 Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
36 Schema.isPattern(/^[a-z0-9_-]+$/i),
37);
38export type ChatAttachmentId = typeof ChatAttachmentId.Type;
39​
40export const SNAP_SHOT_ACCESSIBLE_TEXT_MAX_CHARS = 32_000;
41export const SNAP_SHOT_ACCESSIBILITY_MAX_NODES = 10_000;
42export const SNAP_SHOT_ACCESSIBILITY_MAX_SERIALIZED_CHARS = 32_000;
43​
44const SnapShotAccessibilityBounds = Schema.Struct({
45 x: NonNegativeInt,
46 y: NonNegativeInt,
47 width: PositiveInt,
48 height: PositiveInt,
49});
50​
51const SnapShotAccessibilityState = Schema.Struct({
52 active: Schema.optional(Schema.Boolean),
53 busy: Schema.optional(Schema.Boolean),
54 checked: Schema.optional(Schema.Literals(["on", "off", "mixed"])),
55 editable: Schema.optional(Schema.Boolean),
56 enabled: Schema.optional(Schema.Boolean),
57 expanded: Schema.optional(Schema.Boolean),
58 focused: Schema.optional(Schema.Boolean),
59 selected: Schema.optional(Schema.Boolean),
60 visible: Schema.optional(Schema.Boolean),
61});
62​
63export interface SnapShotAccessibilityNode {
64 readonly role: string;
65 readonly name?: string;
66 readonly value?: string;
67 readonly description?: string;
68 readonly bounds: typeof SnapShotAccessibilityBounds.Type | null;
69 readonly state?: typeof SnapShotAccessibilityState.Type;
70 readonly actions?: Array<string>;
71 readonly children: Array<SnapShotAccessibilityNode>;
73​
74export const SnapShotAccessibilityNode: Schema.Codec<SnapShotAccessibilityNode> = Schema.Struct({
75 role: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
76 name: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(1_000))),
77 value: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(8_000))),
78 description: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(2_000))),
79 bounds: Schema.NullOr(SnapShotAccessibilityBounds),
80 state: Schema.optionalKey(SnapShotAccessibilityState),
81 actions: Schema.optionalKey(
82 Schema.mutable(Schema.Array(TrimmedNonEmptyString.check(Schema.isMaxLength(100)))).check(
83 Schema.isMaxLength(32),
84 ),
85 ),
86 children: Schema.mutable(
87 Schema.Array(
88 Schema.suspend((): Schema.Codec<SnapShotAccessibilityNode> => SnapShotAccessibilityNode),
89 ),
90 ).check(Schema.isMaxLength(SNAP_SHOT_ACCESSIBILITY_MAX_NODES)),
91});
92​
93const SnapShotAccessibilityWire = Schema.Union([
94 Schema.Struct({
95 format: Schema.Literal("flat-text"),
96 text: TrimmedNonEmptyString.check(Schema.isMaxLength(SNAP_SHOT_ACCESSIBLE_TEXT_MAX_CHARS)),
97 truncated: Schema.Boolean,
98 }),
99 Schema.Struct({
100 format: Schema.Literal("element-tree"),
101 coordinateSpace: Schema.Literal("captured-image"),
102 imageSize: Schema.Struct({ width: PositiveInt, height: PositiveInt }),
103 truncated: Schema.Boolean,
104 root: SnapShotAccessibilityNode,
105 }),
106]);
107​
108export const SnapShotAccessibility = SnapShotAccessibilityWire.check(
109 Schema.makeFilter((accessibility: typeof SnapShotAccessibilityWire.Type) => {
110 if (accessibility.format === "flat-text") return undefined;
111 let nodes = 0;
112 const stack = [accessibility.root];
113 while (stack.length > 0) {
114 const node = stack.pop()!;
115 nodes += 1;
116 if (nodes > SNAP_SHOT_ACCESSIBILITY_MAX_NODES) {
117 return `Accessibility trees must not exceed ${SNAP_SHOT_ACCESSIBILITY_MAX_NODES} nodes.`;
118 }
119 stack.push(...node.children);
120 }
121 return (
122 JSON.stringify(accessibility).length <= SNAP_SHOT_ACCESSIBILITY_MAX_SERIALIZED_CHARS ||
123 `Accessibility trees must not exceed ${SNAP_SHOT_ACCESSIBILITY_MAX_SERIALIZED_CHARS} serialized characters.`
124 );
125 }),
126);
127export type SnapShotAccessibility = typeof SnapShotAccessibility.Type;
128​
129export const SnapShotSource = Schema.Struct({
130 kind: Schema.Literal("snap-shot"),
131 capturedAt: IsoDateTime,
132 appName: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
133 windowTitle: TrimmedString.check(Schema.isMaxLength(1_000)),
134 accessibleText: Schema.optional(
135 TrimmedNonEmptyString.check(Schema.isMaxLength(SNAP_SHOT_ACCESSIBLE_TEXT_MAX_CHARS)),
136 ),
137 accessibility: Schema.optional(SnapShotAccessibility),
138 appIdentifier: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(255))),
139 appIconDataUrl: Schema.optional(
140 TrimmedNonEmptyString.check(
141 Schema.isMaxLength(100_000),
142 Schema.isPattern(/^data:image\/png;base64,/i),
143 ),
144 ),
145});
146export type SnapShotSource = typeof SnapShotSource.Type;
147​
148export const ChatImageAttachment = Schema.Struct({
149 type: Schema.Literal("image"),
150 id: ChatAttachmentId,
151 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
152 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
153 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
154 source: Schema.optional(SnapShotSource),
155});
156export type ChatImageAttachment = typeof ChatImageAttachment.Type;
157​
158export const ChatFileAttachment = Schema.Struct({
159 type: Schema.Literal("file"),
160 id: ChatAttachmentId,
161 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
162 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
163 sizeBytes: NonNegativeInt.check(
164 Schema.isGreaterThanOrEqualTo(1),
165 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
166 ),
167});
168export type ChatFileAttachment = typeof ChatFileAttachment.Type;
169​
170/**
171 * Catch-all for attachment types this build does not know. Attachments ride on
172 * persisted events and thread streams, so a newer server or client must be able
173 * to introduce a type without making older readers fail to decode the whole
174 * message. Decoders keep the shared base fields; consumers skip these or render
175 * them as unsupported. Mirrors how `OrchestrationThreadActivity` keeps `kind`
176 * open. The known discriminators are excluded so a malformed image or file
177 * attachment fails its own schema instead of sliding through here with its
178 * size and mime constraints unchecked.
179 */
180export const ChatUnknownAttachment = Schema.Struct({
181 type: TrimmedNonEmptyString.check(
182 Schema.isMaxLength(50),
183 Schema.isPattern(/^(?!(?:image|file)$)/),
184 ),
185 id: ChatAttachmentId,
186 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
187 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
188 sizeBytes: NonNegativeInt,
189});
190export type ChatUnknownAttachment = typeof ChatUnknownAttachment.Type;
191​
192export const UploadChatImageAttachment = Schema.Struct({
193 type: Schema.Literal("image"),
194 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
195 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
196 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
197 dataUrl: TrimmedNonEmptyString.check(
198 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
199 ),
200 source: Schema.optional(SnapShotSource),
201});
202export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
203​
204export const ChatAttachment = Schema.Union([
205 ChatImageAttachment,
206 ChatFileAttachment,
207 ChatUnknownAttachment,
208]);
209export type ChatAttachment = typeof ChatAttachment.Type;
210​
211export const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
212export type UploadChatAttachment = typeof UploadChatAttachment.Type;
213​
214export const PersistChatAttachmentsInput = Schema.Struct({
215 threadId: ThreadId,
216 messageId: MessageId,
217 attachments: Schema.Array(UploadChatAttachment),
218});
219export type PersistChatAttachmentsInput = typeof PersistChatAttachmentsInput.Type;
220​
221export const PersistChatAttachmentsResult = Schema.Struct({
222 attachments: Schema.Array(ChatAttachment),
223});
224export type PersistChatAttachmentsResult = typeof PersistChatAttachmentsResult.Type;
225​
226export class PersistChatAttachmentsError extends Schema.TaggedError<PersistChatAttachmentsError>()(
227 "PersistChatAttachmentsError",
228 {
229 message: Schema.String,
230 cause: Schema.optional(Schema.Defect()),
231 },
232) {}