Repository navigation
Expand file tree
/
Copy pathcomposerContext.ts
More file actions
281 lines (254 loc) · 10.7 KB
/
Copy pathcomposerContext.ts
File metadata and controls
281 lines (254 loc) · 10.7 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
import * as Schema from "effect/Schema";
import {
ForwardCompatibleArray,
NonNegativeInt,
PositiveInt,
TrimmedNonEmptyString,
} from "./baseSchemas.ts";
/**
* Inline context records: the typed payload behind every composer chip.
* A message's `text` carries position through canonical reference links
* (`[label](t3-context://v1/<kind>/<contextId>)`, see
* `@t3tools/shared/composerContextReferences`); these records carry the payload,
* keyed by `contextId`. Bytes never live here: image and file records bind to a
* `ChatAttachment` by id.
*/
export const COMPOSER_CONTEXT_KINDS = [
"image",
"file",
"terminal",
"element",
"preview-annotation",
"review-comment",
"mention",
"skill",
] 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;
/** Exported so producers can clamp to the same boundary the schema enforces, rather than
minting a record the send path would fail to encode. */
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;
/** Durable identity of one payload. Shared by every chip that points at it. */
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;
/** Identity of one occurrence in a document. Changes when a chip is duplicated. */
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;
/** Open kind: known kinds get typed records, everything else preserves its payload. */
export const ComposerContextKind = TrimmedNonEmptyString.check(
Schema.isPattern(CONTEXT_KIND_PATTERN),
);
export type ComposerContextKind = typeof ComposerContextKind.Type;
// Same rules as the private `ChatAttachmentId` in orchestration.ts; duplicated here because
// orchestration.ts imports this module.
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);
/** Snapshot used to identify and present a pull request carried by a review-context record. */
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;
/** What a picked page element looks like to the agent; shared by element and annotation records. */
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;
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)),
/** Picked elements inside the annotation, with the detail the agent needs to find them. */
elements: Schema.optional(Schema.Array(ElementContextDetails).check(Schema.isMaxLength(50))),
/** Original target ids and edits allow pasted annotations to retain exact style changes. */
elementIds: Schema.optional(Schema.Array(ShortString).check(Schema.isMaxLength(50))),
/** Region and stroke geometry is lossy on purpose, but their counts feed the target summary,
so a pasted annotation still says what it marked. */
regionCount: Schema.optional(NonNegativeInt),
strokeCount: Schema.optional(NonNegativeInt),
styleChangeDetails: Schema.optional(
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)),
),
/** The screenshot travels as its own image record; this links the two. */
screenshotContextId: Schema.optional(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.optional(BoundedString(64)),
pullRequest: Schema.optional(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;
/**
* Catch-all for kinds this build does not know. Known discriminators are excluded so a
* malformed known record fails its own schema instead of sliding through unchecked.
* Mirrors `ChatUnknownAttachment`.
*/
export const UnknownContextRecord = Schema.Struct({
...recordBase,
kind: ComposerContextKind.check(Schema.isPattern(KNOWN_KIND_PATTERN)),
payload: Schema.Unknown.check(
Schema.makeFilter((payload) => {
try {
const encoded = JSON.stringify(payload);
return encoded !== undefined && encoded.length <= 64_000;
} catch {
return false;
}
}),
),
});
export type UnknownContextRecord = typeof UnknownContextRecord.Type;
export const KnownComposerContextRecord = Schema.Union([
ImageContextRecord,
FileContextRecord,
TerminalContextRecord,
ElementContextRecord,
PreviewAnnotationContextRecord,
ReviewCommentContextRecord,
MentionContextRecord,
SkillContextRecord,
]);
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;
/** Structured context riding on a user message. Undecodable records are dropped, not fatal. */
export const OrchestrationMessageContext = Schema.Struct({
version: Schema.Literal(1),
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 OrchestrationMessageContext.Type;