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