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