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 { NonNegativeInt, ThreadId, TrimmedNonEmptyString, TurnItemId } from "./baseSchemas.ts"; |
| 4 | import { |
| 5 | PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 6 | PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, |
| 7 | PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES, |
| 8 | } from "./chatAttachment.ts"; |
| 9 | import { ProjectFaviconPath } from "./project.ts"; |
| 10 | import { ToolActivityNativeAppReference } from "./providerRuntime.ts"; |
| 11 | import { EnvironmentThemeId } from "./server.ts"; |
| 12 | |
| 13 | const ASSET_PATH_MAX_LENGTH = 1024; |
| 14 | |
| 15 | /** |
| 16 | * The wire tag of a source control host's credentialed media. It names GitHub because GitHub had |
| 17 | * it first, and servers and clients of different releases must keep understanding each other. |
| 18 | */ |
| 19 | export const SOURCE_CONTROL_MEDIA_ASSET = "github-media" as const; |
| 20 | |
| 21 | export const NativeAppIconReference = Schema.Union([ |
| 22 | ToolActivityNativeAppReference, |
| 23 | Schema.TaggedStruct("app-path", { |
| 24 | path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 25 | }), |
| 26 | ]); |
| 27 | export type NativeAppIconReference = typeof NativeAppIconReference.Type; |
| 28 | |
| 29 | export const AssetResource = Schema.Union([ |
| 30 | Schema.TaggedStruct("workspace-file", { |
| 31 | threadId: ThreadId, |
| 32 | path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 33 | }), |
| 34 | Schema.TaggedStruct("media-file", { |
| 35 | threadId: ThreadId, |
| 36 | path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 37 | disposition: Schema.optionalKey(Schema.Literal("attachment")), |
| 38 | // Written relative to the thread's workspace, so only that host's file is meant. |
| 39 | hostOnly: Schema.optionalKey(Schema.Literal(true)), |
| 40 | }), |
| 41 | Schema.TaggedStruct("draft-workspace-file", { |
| 42 | cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 43 | path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 44 | }), |
| 45 | Schema.TaggedStruct("attachment", { |
| 46 | attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)), |
| 47 | fileName: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(255))), |
| 48 | mimeType: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(100))), |
| 49 | /** Defaults to download; viewers may request inline after checking the type is safe. */ |
| 50 | disposition: Schema.optionalKey(Schema.Literals(["inline", "attachment"])), |
| 51 | renderIntent: Schema.optionalKey(Schema.Literal("mcp-app")), |
| 52 | }), |
| 53 | Schema.TaggedStruct("tool-output-image", { |
| 54 | threadId: ThreadId, |
| 55 | itemId: TurnItemId, |
| 56 | index: NonNegativeInt, |
| 57 | }), |
| 58 | Schema.TaggedStruct("project-favicon", { |
| 59 | cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 60 | // Cache-key hint only; the project projection supplies the authoritative path. |
| 61 | path: Schema.optional(ProjectFaviconPath), |
| 62 | }), |
| 63 | Schema.TaggedStruct("native-app-icon", { |
| 64 | app: NativeAppIconReference, |
| 65 | }), |
| 66 | // Media a change request body points at on its host. A private repository serves these only |
| 67 | // to a request that carries a credential, which the client has none of, so the server fetches |
| 68 | // them with the credential the host's driver holds for the repository at `cwd`. `repository` |
| 69 | // names the change request's repository, which can differ from the checkout at `cwd`. |
| 70 | Schema.TaggedStruct("environment-theme-background", { |
| 71 | themeId: EnvironmentThemeId, |
| 72 | }), |
| 73 | Schema.TaggedStruct(SOURCE_CONTROL_MEDIA_ASSET, { |
| 74 | cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 75 | url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)), |
| 76 | repository: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(512))), |
| 77 | }), |
| 78 | ]); |
| 79 | export type AssetResource = typeof AssetResource.Type; |
| 80 | |
| 81 | export const AssetCreateUrlInput = Schema.Struct({ |
| 82 | resource: AssetResource, |
| 83 | }); |
| 84 | export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type; |
| 85 | |
| 86 | export const AssetImageDimensions = Schema.Struct({ |
| 87 | width: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)), |
| 88 | height: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)), |
| 89 | }); |
| 90 | export type AssetImageDimensions = typeof AssetImageDimensions.Type; |
| 91 | |
| 92 | export const AssetCreateUrlResult = Schema.Struct({ |
| 93 | relativeUrl: TrimmedNonEmptyString.check( |
| 94 | Schema.isMaxLength(4096), |
| 95 | Schema.isPattern(/^\/api\/assets\/(?!\.{1,2}\/)[\w.-]+\/(?!(?:\.|%2[eE]){1,2}$)[^/\\?#\s]+$/u), |
| 96 | ), |
| 97 | expiresAt: Schema.Number, |
| 98 | sourcePath: Schema.optional( |
| 99 | TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), |
| 100 | ), |
| 101 | imageDimensions: Schema.optional(AssetImageDimensions), |
| 102 | /** Changes only when the file's contents or identity change, unlike `relativeUrl`, which is re-signed on every mint. */ |
| 103 | fileRevision: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(256))), |
| 104 | }); |
| 105 | export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type; |
| 106 | |
| 107 | export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000; |
| 108 | |
| 109 | const ImageAttachmentCreateUploadUrlInput = Schema.Struct({ |
| 110 | type: Schema.optionalKey(Schema.Literal("image")), |
| 111 | name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)), |
| 112 | mimeType: Schema.Literals(PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES), |
| 113 | sizeBytes: NonNegativeInt.check( |
| 114 | Schema.isGreaterThanOrEqualTo(1), |
| 115 | Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES), |
| 116 | ), |
| 117 | }); |
| 118 | |
| 119 | const FileAttachmentCreateUploadUrlInput = Schema.Struct({ |
| 120 | type: Schema.Literal("file"), |
| 121 | name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)), |
| 122 | mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)), |
| 123 | sizeBytes: NonNegativeInt.check( |
| 124 | Schema.isGreaterThanOrEqualTo(1), |
| 125 | Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES), |
| 126 | ), |
| 127 | }); |
| 128 | |
| 129 | export const AttachmentCreateUploadUrlInput = Schema.Union([ |
| 130 | ImageAttachmentCreateUploadUrlInput, |
| 131 | FileAttachmentCreateUploadUrlInput, |
| 132 | ]); |
| 133 | export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type; |
| 134 | |
| 135 | export const AttachmentCreateUploadUrlResult = Schema.Struct({ |
| 136 | attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)), |
| 137 | relativeUrl: TrimmedNonEmptyString.check( |
| 138 | Schema.isMaxLength(4096), |
| 139 | Schema.isPattern(/^\/api\/attachments\/upload\/(?!\.{1,2}$)[\w.-]+$/u), |
| 140 | ), |
| 141 | expiresAt: Schema.Number, |
| 142 | }); |
| 143 | export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type; |
| 144 | |
| 145 | export const AttachmentDeleteInput = Schema.Struct({ |
| 146 | attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)), |
| 147 | }); |
| 148 | export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type; |
| 149 | |
| 150 | export class AttachmentUploadSigningKeyError extends Schema.TaggedError<AttachmentUploadSigningKeyError>()( |
| 151 | "AttachmentUploadSigningKeyError", |
| 152 | { |
| 153 | cause: Schema.Defect(), |
| 154 | }, |
| 155 | ) { |
| 156 | override get message(): string { |
| 157 | return "Failed to load the attachment upload signing key."; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | export class AssetWorkspaceContextNotFoundError extends Schema.TaggedError<AssetWorkspaceContextNotFoundError>()( |
| 162 | "AssetWorkspaceContextNotFoundError", |
| 163 | { |
| 164 | resource: AssetResource, |
| 165 | }, |
| 166 | ) { |
| 167 | override get message(): string { |
| 168 | return "Workspace context was not found."; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | export class AssetWorkspaceContextResolutionError extends Schema.TaggedError<AssetWorkspaceContextResolutionError>()( |
| 173 | "AssetWorkspaceContextResolutionError", |
| 174 | { |
| 175 | resource: AssetResource, |
| 176 | cause: Schema.Defect(), |
| 177 | }, |
| 178 | ) { |
| 179 | override get message(): string { |
| 180 | return "Failed to resolve workspace context."; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | export class AssetWorkspaceRootNormalizationError extends Schema.TaggedError<AssetWorkspaceRootNormalizationError>()( |
| 185 | "AssetWorkspaceRootNormalizationError", |
| 186 | { |
| 187 | resource: AssetResource, |
| 188 | cause: Schema.Defect(), |
| 189 | }, |
| 190 | ) { |
| 191 | override get message(): string { |
| 192 | return "Failed to normalize the workspace root."; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | export class AssetWorkspacePathValidationError extends Schema.TaggedError<AssetWorkspacePathValidationError>()( |
| 197 | "AssetWorkspacePathValidationError", |
| 198 | { |
| 199 | resource: AssetResource, |
| 200 | cause: Schema.Defect(), |
| 201 | }, |
| 202 | ) { |
| 203 | override get message(): string { |
| 204 | return "Workspace file path must be relative to the project root."; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | export class AssetPreviewTypeValidationError extends Schema.TaggedError<AssetPreviewTypeValidationError>()( |
| 209 | "AssetPreviewTypeValidationError", |
| 210 | { |
| 211 | resource: AssetResource, |
| 212 | }, |
| 213 | ) { |
| 214 | override get message(): string { |
| 215 | return this.resource._tag === "media-file" || this.resource._tag === "draft-workspace-file" |
| 216 | ? "Only images, videos, audio, HTML, and PDF files can be previewed." |
| 217 | : "Only browser documents and images can be previewed."; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | export class AssetWorkspaceAssetInspectionError extends Schema.TaggedError<AssetWorkspaceAssetInspectionError>()( |
| 222 | "AssetWorkspaceAssetInspectionError", |
| 223 | { |
| 224 | resource: AssetResource, |
| 225 | cause: Schema.Defect(), |
| 226 | }, |
| 227 | ) { |
| 228 | override get message(): string { |
| 229 | return this.resource._tag === "media-file" |
| 230 | ? "Failed to inspect the media file." |
| 231 | : "Failed to inspect the workspace asset."; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedError<AssetWorkspaceAssetNotFoundError>()( |
| 236 | "AssetWorkspaceAssetNotFoundError", |
| 237 | { |
| 238 | resource: AssetResource, |
| 239 | // Absolute path after home/workspace resolution; absent on older servers. |
| 240 | checkedPath: Schema.optionalKey(Schema.String), |
| 241 | }, |
| 242 | ) { |
| 243 | override get message(): string { |
| 244 | return this.resource._tag === "media-file" |
| 245 | ? "Media file was not found." |
| 246 | : this.resource._tag === "tool-output-image" |
| 247 | ? "Tool output image was not found." |
| 248 | : "Workspace asset was not found."; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | export class AssetWorkspaceResolutionError extends Schema.TaggedError<AssetWorkspaceResolutionError>()( |
| 253 | "AssetWorkspaceResolutionError", |
| 254 | { |
| 255 | resource: AssetResource, |
| 256 | cause: Schema.Defect(), |
| 257 | }, |
| 258 | ) { |
| 259 | override get message(): string { |
| 260 | return "Failed to resolve workspace."; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | export class AssetAttachmentNotFoundError extends Schema.TaggedError<AssetAttachmentNotFoundError>()( |
| 265 | "AssetAttachmentNotFoundError", |
| 266 | { |
| 267 | resource: AssetResource, |
| 268 | }, |
| 269 | ) { |
| 270 | override get message(): string { |
| 271 | return "Attachment was not found."; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | export class AssetProjectFaviconResolutionError extends Schema.TaggedError<AssetProjectFaviconResolutionError>()( |
| 276 | "AssetProjectFaviconResolutionError", |
| 277 | { |
| 278 | resource: AssetResource, |
| 279 | cause: Schema.Defect(), |
| 280 | }, |
| 281 | ) { |
| 282 | override get message(): string { |
| 283 | return "Failed to resolve project favicon."; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | export class AssetProjectFaviconInspectionError extends Schema.TaggedError<AssetProjectFaviconInspectionError>()( |
| 288 | "AssetProjectFaviconInspectionError", |
| 289 | { |
| 290 | resource: AssetResource, |
| 291 | cause: Schema.Defect(), |
| 292 | }, |
| 293 | ) { |
| 294 | override get message(): string { |
| 295 | return "Failed to inspect the project favicon."; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | export class AssetProjectFaviconNotFoundError extends Schema.TaggedError<AssetProjectFaviconNotFoundError>()( |
| 300 | "AssetProjectFaviconNotFoundError", |
| 301 | { |
| 302 | resource: AssetResource, |
| 303 | }, |
| 304 | ) { |
| 305 | override get message(): string { |
| 306 | return "Project favicon was not found."; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | export class AssetSigningKeyLoadError extends Schema.TaggedError<AssetSigningKeyLoadError>()( |
| 311 | "AssetSigningKeyLoadError", |
| 312 | { |
| 313 | resource: AssetResource, |
| 314 | cause: Schema.Defect(), |
| 315 | }, |
| 316 | ) { |
| 317 | override get message(): string { |
| 318 | return "Failed to load the asset signing key."; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // The tag predates the rename and stays: clients on either side of an update decode it. |
| 323 | export class AssetSourceControlMediaUrlValidationError extends Schema.TaggedError<AssetSourceControlMediaUrlValidationError>()( |
| 324 | "AssetGitHubMediaUrlValidationError", |
| 325 | {}, |
| 326 | ) { |
| 327 | override get message(): string { |
| 328 | return "Only media a source control host keeps behind a credential can be fetched with one."; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | export const AssetAccessError = Schema.Union([ |
| 333 | AssetWorkspaceContextNotFoundError, |
| 334 | AssetWorkspaceContextResolutionError, |
| 335 | AssetWorkspaceRootNormalizationError, |
| 336 | AssetWorkspacePathValidationError, |
| 337 | AssetPreviewTypeValidationError, |
| 338 | AssetWorkspaceAssetInspectionError, |
| 339 | AssetWorkspaceAssetNotFoundError, |
| 340 | AssetWorkspaceResolutionError, |
| 341 | AssetAttachmentNotFoundError, |
| 342 | AssetProjectFaviconResolutionError, |
| 343 | AssetProjectFaviconInspectionError, |
| 344 | AssetProjectFaviconNotFoundError, |
| 345 | AssetSourceControlMediaUrlValidationError, |
| 346 | AssetSigningKeyLoadError, |
| 347 | ]); |
| 348 | export type AssetAccessError = typeof AssetAccessError.Type; |