Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import {
4 EnvironmentId,
5 ForwardCompatibleArray,
6 GuestMessageId,
7 GuestShareId,
8 NonNegativeInt,
9 PositiveInt,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import { GUEST_MESSAGE_MAX_LENGTH, GUEST_NAME_MAX_LENGTH } from "./guestShare.ts";
14
15/** Image and file records reference `ChatAttachment` ids, never bytes. */
16
17export const COMPOSER_CONTEXT_KINDS = [
18 "image",
19 "file",
20 "terminal",
21 "element",
22 "preview-annotation",
23 "review-comment",
24 "mention",
25 "skill",
26 "thread",
27] as const;
28export type KnownComposerContextKind = (typeof COMPOSER_CONTEXT_KINDS)[number];
29
30const CONTEXT_ID_MAX_CHARS = 128;
31const CONTEXT_ID_PATTERN = /^[a-z0-9_-]+$/i;
32const CONTEXT_KIND_PATTERN = /^[a-z][a-z0-9-]{0,39}$/;
33const KNOWN_KIND_PATTERN = new RegExp(`^(?!(?:${COMPOSER_CONTEXT_KINDS.join("|")})$)`);
34
35export const COMPOSER_CONTEXT_LABEL_MAX_CHARS = 200;
36export const COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS = 64_000;
37const COMPOSER_CONTEXT_ELEMENT_HTML_MAX_CHARS = 8_000;
38const COMPOSER_CONTEXT_ELEMENT_STYLES_MAX_CHARS = 8_000;
39export const COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS = 16_000;
40export const COMPOSER_CONTEXT_REVIEW_DIFF_MAX_CHARS = 32_000;
41const COMPOSER_CONTEXT_PREVIEW_COMMENT_MAX_CHARS = 8_000;
42
43export const ComposerContextId = TrimmedNonEmptyString.check(
44 Schema.isMaxLength(CONTEXT_ID_MAX_CHARS),
45 Schema.isPattern(CONTEXT_ID_PATTERN),
46).pipe(Schema.brand("ComposerContextId"));
47export type ComposerContextId = typeof ComposerContextId.Type;
48
49export const ComposerContextReferenceId = TrimmedNonEmptyString.check(
50 Schema.isMaxLength(CONTEXT_ID_MAX_CHARS),
51 Schema.isPattern(CONTEXT_ID_PATTERN),
52).pipe(Schema.brand("ComposerContextReferenceId"));
53export type ComposerContextReferenceId = typeof ComposerContextReferenceId.Type;
54
55export const ComposerContextKind = TrimmedNonEmptyString.check(
56 Schema.isPattern(CONTEXT_KIND_PATTERN),
57);
58export type ComposerContextKind = typeof ComposerContextKind.Type;
59
60// Duplicates the private ChatAttachmentId rules to avoid an orchestration.ts import cycle.
61const ContextAttachmentId = TrimmedNonEmptyString.check(
62 Schema.isMaxLength(CONTEXT_ID_MAX_CHARS),
63 Schema.isPattern(CONTEXT_ID_PATTERN),
64);
65const ContextLabel = Schema.String.check(Schema.isMaxLength(COMPOSER_CONTEXT_LABEL_MAX_CHARS));
66const BoundedString = (max: number) => Schema.String.check(Schema.isMaxLength(max));
67const ShortString = BoundedString(2_048);
68const NullableShortString = Schema.NullOr(ShortString);
69
70export const PullRequestContextMetadata = Schema.Struct({
71 number: PositiveInt,
72 title: ShortString,
73 url: ShortString,
74 headBranch: ShortString,
75 baseBranch: ShortString,
76 state: Schema.Literals(["open", "closed", "merged"]),
77 isDraft: Schema.Boolean,
78});
79export type PullRequestContextMetadata = typeof PullRequestContextMetadata.Type;
80
81const recordBase = {
82 version: Schema.Literal(1),
83 contextId: ComposerContextId,
84 label: ContextLabel,
85} as const;
86
87const attachmentBinding = {
88 attachmentId: ContextAttachmentId,
89 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
90 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
91 sizeBytes: NonNegativeInt,
92} as const;
93
94export const ImageContextRecord = Schema.Struct({
95 ...recordBase,
96 kind: Schema.Literal("image"),
97 ...attachmentBinding,
98});
99export type ImageContextRecord = typeof ImageContextRecord.Type;
100
101export const FileContextRecord = Schema.Struct({
102 ...recordBase,
103 kind: Schema.Literal("file"),
104 ...attachmentBinding,
105});
106export type FileContextRecord = typeof FileContextRecord.Type;
107
108export const TerminalContextRecord = Schema.Struct({
109 ...recordBase,
110 kind: Schema.Literal("terminal"),
111 terminalId: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
112 terminalLabel: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
113 lineStart: NonNegativeInt,
114 lineEnd: NonNegativeInt,
115 text: BoundedString(COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS),
116}).check(Schema.makeFilter((record) => record.lineEnd >= record.lineStart));
117export type TerminalContextRecord = typeof TerminalContextRecord.Type;
118
119export const ElementContextSource = Schema.Struct({
120 functionName: NullableShortString,
121 fileName: NullableShortString,
122 lineNumber: Schema.NullOr(NonNegativeInt),
123 columnNumber: Schema.NullOr(NonNegativeInt),
124});
125export type ElementContextSource = typeof ElementContextSource.Type;
126
127export const ElementContextDetails = Schema.Struct({
128 pageUrl: ShortString,
129 pageTitle: NullableShortString,
130 tagName: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
131 selector: NullableShortString,
132 htmlPreview: BoundedString(COMPOSER_CONTEXT_ELEMENT_HTML_MAX_CHARS),
133 componentName: NullableShortString,
134 source: Schema.NullOr(ElementContextSource),
135 styles: BoundedString(COMPOSER_CONTEXT_ELEMENT_STYLES_MAX_CHARS),
136});
137export type ElementContextDetails = typeof ElementContextDetails.Type;
138
139export const ElementContextRecord = Schema.Struct({
140 ...recordBase,
141 kind: Schema.Literal("element"),
142 ...ElementContextDetails.fields,
143});
144export type ElementContextRecord = typeof ElementContextRecord.Type;
145
146// `optionalKey` rejects explicit undefined within one record, so only that record is dropped.
147export const PreviewAnnotationContextRecord = Schema.Struct({
148 ...recordBase,
149 kind: Schema.Literal("preview-annotation"),
150 annotationId: ShortString,
151 pageUrl: ShortString,
152 pageTitle: NullableShortString,
153 comment: BoundedString(COMPOSER_CONTEXT_PREVIEW_COMMENT_MAX_CHARS),
154 targetSummary: ShortString,
155 styleChanges: Schema.Array(ShortString).check(Schema.isMaxLength(200)),
156 elements: Schema.optionalKey(Schema.Array(ElementContextDetails).check(Schema.isMaxLength(50))),
157 elementIds: Schema.optionalKey(Schema.Array(ShortString).check(Schema.isMaxLength(50))),
158 regionCount: Schema.optionalKey(NonNegativeInt),
159 strokeCount: Schema.optionalKey(NonNegativeInt),
160 styleChangeDetails: Schema.optionalKey(
161 Schema.Array(
162 Schema.Struct({
163 targetId: ShortString,
164 selector: NullableShortString,
165 property: ShortString,
166 previousValue: BoundedString(COMPOSER_CONTEXT_ELEMENT_STYLES_MAX_CHARS),
167 value: BoundedString(COMPOSER_CONTEXT_ELEMENT_STYLES_MAX_CHARS),
168 }),
169 ).check(Schema.isMaxLength(200)),
170 ),
171 screenshotContextId: Schema.optionalKey(ComposerContextId),
172});
173export type PreviewAnnotationContextRecord = typeof PreviewAnnotationContextRecord.Type;
174
175export const ReviewCommentContextRecord = Schema.Struct({
176 ...recordBase,
177 kind: Schema.Literal("review-comment"),
178 sectionId: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
179 sectionTitle: ShortString,
180 filePath: TrimmedNonEmptyString.check(Schema.isMaxLength(2_048)),
181 startIndex: NonNegativeInt,
182 endIndex: NonNegativeInt,
183 rangeLabel: ShortString,
184 text: BoundedString(COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS),
185 diff: BoundedString(COMPOSER_CONTEXT_REVIEW_DIFF_MAX_CHARS),
186 fenceLanguage: Schema.optionalKey(BoundedString(64)),
187 pullRequest: Schema.optionalKey(PullRequestContextMetadata),
188}).check(Schema.makeFilter((record) => record.endIndex >= record.startIndex));
189export type ReviewCommentContextRecord = typeof ReviewCommentContextRecord.Type;
190
191export const MentionContextRecord = Schema.Struct({
192 ...recordBase,
193 kind: Schema.Literal("mention"),
194 path: TrimmedNonEmptyString.check(Schema.isMaxLength(2_048)),
195});
196export type MentionContextRecord = typeof MentionContextRecord.Type;
197
198export const SkillContextRecord = Schema.Struct({
199 ...recordBase,
200 kind: Schema.Literal("skill"),
201 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
202});
203export type SkillContextRecord = typeof SkillContextRecord.Type;
204
205export const ThreadContextRecord = Schema.Struct({
206 ...recordBase,
207 kind: Schema.Literal("thread"),
208 environmentId: EnvironmentId,
209 threadId: ThreadId,
210 title: ContextLabel,
211});
212export type ThreadContextRecord = typeof ThreadContextRecord.Type;
213
214const isJson = Schema.is(Schema.Json);
215
216/** Excludes known discriminators so malformed known records fail their own schema. */
217export const UnknownContextRecord = Schema.Struct({
218 ...recordBase,
219 kind: ComposerContextKind.check(Schema.isPattern(KNOWN_KIND_PATTERN)),
220 payload: Schema.Unknown.check(
221 Schema.makeFilter((payload) => {
222 let encoded: string | undefined;
223 try {
224 encoded = JSON.stringify(payload);
225 } catch {
226 return false;
227 }
228 return encoded !== undefined && encoded.length <= 64_000 && isJson(payload);
229 }),
230 ),
231});
232export type UnknownContextRecord = typeof UnknownContextRecord.Type;
233
234export const KnownComposerContextRecord = Schema.Union([
235 ImageContextRecord,
236 FileContextRecord,
237 TerminalContextRecord,
238 ElementContextRecord,
239 PreviewAnnotationContextRecord,
240 ReviewCommentContextRecord,
241 MentionContextRecord,
242 SkillContextRecord,
243 ThreadContextRecord,
244]);
245export type KnownComposerContextRecord = typeof KnownComposerContextRecord.Type;
246
247export const ComposerContextRecord = Schema.Union([
248 ...KnownComposerContextRecord.members,
249 UnknownContextRecord,
250]);
251export type ComposerContextRecord = typeof ComposerContextRecord.Type;
252
253export const COMPOSER_CONTEXT_MAX_RECORDS = 200;
254const COMPOSER_CONTEXT_MAX_SERIALIZED_CHARS = 16_000_000;
255
256/**
257 * Marks a user message as a guest's, sent by the owner. The message `text` keeps the attributed
258 * form the agent reads; clients show `text` here with `name` beside it. Only the server sets it.
259 */
260export const GuestMessageAttribution = Schema.Struct({
261 guestMessageId: GuestMessageId,
262 name: TrimmedNonEmptyString.check(Schema.isMaxLength(GUEST_NAME_MAX_LENGTH)),
263 text: Schema.String.check(Schema.isMaxLength(GUEST_MESSAGE_MAX_LENGTH)),
264 /** The link the guest used. Only that link's guest may load the message's files. */
265 shareId: Schema.optional(GuestShareId),
266});
267export type GuestMessageAttribution = typeof GuestMessageAttribution.Type;
268
269/** Structured context riding on a user message. Undecodable records are dropped, not fatal. */
270export const OrchestrationMessageContext = Schema.Struct({
271 version: Schema.Literal(1),
272 guest: Schema.optional(GuestMessageAttribution),
273 records: Schema.Array(Schema.Unknown)
274 .check(
275 Schema.isMaxLength(COMPOSER_CONTEXT_MAX_RECORDS),
276 Schema.makeFilter((records) => {
277 try {
278 return JSON.stringify(records).length <= COMPOSER_CONTEXT_MAX_SERIALIZED_CHARS;
279 } catch {
280 return false;
281 }
282 }),
283 )
284 .pipe(Schema.decodeTo(ForwardCompatibleArray(ComposerContextRecord)))
285 .check(
286 Schema.makeFilter(
287 (records) => new Set(records.map((record) => record.contextId)).size === records.length,
288 ),
289 ),
290});
291export type OrchestrationMessageContext = typeof OrchestrationMessageContext.Type;