Open History to see the last commit
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());
30}
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>;
72}
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 PastedTextAttachmentSource = Schema.TaggedStruct("pasted-text", {});
159export type PastedTextAttachmentSource = typeof PastedTextAttachmentSource.Type;
160
161export const ChatFileAttachment = Schema.Struct({
162 type: Schema.Literal("file"),
163 id: ChatAttachmentId,
164 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
165 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
166 sizeBytes: NonNegativeInt.check(
167 Schema.isGreaterThanOrEqualTo(1),
168 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
169 ),
170 /** Clipboard text folded by a client. Providers keep these path-only so the
171 agent can inspect the file selectively instead of eagerly spending the
172 same context the fold is intended to preserve. */
173 source: Schema.optional(PastedTextAttachmentSource),
174});
175export type ChatFileAttachment = typeof ChatFileAttachment.Type;
176
177/**
178 * Catch-all for attachment types this build does not know. Attachments ride on
179 * persisted events and thread streams, so a newer server or client must be able
180 * to introduce a type without making older readers fail to decode the whole
181 * message. Decoders keep the shared base fields; consumers skip these or render
182 * them as unsupported. Mirrors how `OrchestrationThreadActivity` keeps `kind`
183 * open. The known discriminators are excluded so a malformed image or file
184 * attachment fails its own schema instead of sliding through here with its
185 * size and mime constraints unchecked.
186 */
187export const ChatUnknownAttachment = Schema.Struct({
188 type: TrimmedNonEmptyString.check(
189 Schema.isMaxLength(50),
190 Schema.isPattern(/^(?!(?:image|file)$)/),
191 ),
192 id: ChatAttachmentId,
193 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
194 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
195 sizeBytes: NonNegativeInt,
196});
197export type ChatUnknownAttachment = typeof ChatUnknownAttachment.Type;
198
199export const UploadChatImageAttachment = Schema.Struct({
200 id: Schema.optional(ChatAttachmentId),
201 type: Schema.Literal("image"),
202 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
203 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
204 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
205 dataUrl: TrimmedNonEmptyString.check(
206 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
207 ),
208 source: Schema.optional(SnapShotSource),
209});
210export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
211
212export const ChatAttachment = Schema.Union([
213 ChatImageAttachment,
214 ChatFileAttachment,
215 ChatUnknownAttachment,
216]);
217export type ChatAttachment = typeof ChatAttachment.Type;
218
219export const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
220export type UploadChatAttachment = typeof UploadChatAttachment.Type;
221
222export const PersistChatAttachmentsInput = Schema.Struct({
223 threadId: ThreadId,
224 messageId: MessageId,
225 attachments: Schema.Array(UploadChatAttachment),
226});
227export type PersistChatAttachmentsInput = typeof PersistChatAttachmentsInput.Type;
228
229export const PersistChatAttachmentsResult = Schema.Struct({
230 attachments: Schema.Array(ChatAttachment),
231});
232export type PersistChatAttachmentsResult = typeof PersistChatAttachmentsResult.Type;
233
234export class PersistChatAttachmentsError extends Schema.TaggedError<PersistChatAttachmentsError>()(
235 "PersistChatAttachmentsError",
236 {
237 message: Schema.String,
238 cause: Schema.optional(Schema.Defect()),
239 },
240) {}