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