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

composerContext.ts

305 lines11.0 KB

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