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