Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import { NonNegativeInt, ThreadId, TrimmedNonEmptyString } 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 ProjectFaviconPath,
9} from "./orchestration.ts";
10import { ToolActivityNativeAppReference } from "./providerRuntime.ts";
11
12const ASSET_PATH_MAX_LENGTH = 1024;
13
14export const AssetResource = Schema.Union([
15 Schema.TaggedStruct("workspace-file", {
16 threadId: ThreadId,
17 path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
18 }),
19 // One file served in place from anywhere the environment host can read:
20 // images, videos, HTML, and PDF. An absolute path may lie outside the
21 // workspace; a relative one resolves against the thread's workspace.
22 Schema.TaggedStruct("media-file", {
23 threadId: ThreadId,
24 path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
25 }),
26 Schema.TaggedStruct("attachment", {
27 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
28 /** Display name and mime from the `ChatAttachment` the caller holds. The
29 server bakes both into the signed URL so downloads carry the real
30 filename and Content-Type. Absent on older clients, which fall back to
31 an octet-stream download without a filename. */
32 fileName: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(255))),
33 mimeType: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
34 /** Generic attachments download by default. Document viewers opt into an
35 inline response after deciding the file type is safe to preview. */
36 disposition: Schema.optionalKey(Schema.Literals(["inline", "attachment"])),
37 }),
38 Schema.TaggedStruct("project-favicon", {
39 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
40 // A cache-key hint only. The server reads the authoritative path from the
41 // project projection before it issues the signed URL.
42 path: Schema.optional(ProjectFaviconPath),
43 }),
44 Schema.TaggedStruct("native-app-icon", {
45 app: ToolActivityNativeAppReference,
46 }),
47]);
48export type AssetResource = typeof AssetResource.Type;
49
50export const AssetCreateUrlInput = Schema.Struct({
51 resource: AssetResource,
52});
53export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type;
54
55export const AssetImageDimensions = Schema.Struct({
56 width: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
57 height: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
58});
59export type AssetImageDimensions = typeof AssetImageDimensions.Type;
60
61export const AssetCreateUrlResult = Schema.Struct({
62 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
63 expiresAt: Schema.Number,
64 sourcePath: Schema.optional(
65 TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
66 ),
67 /** Pixel size read from the image header, so a client can reserve the exact box before the bytes arrive. */
68 imageDimensions: Schema.optional(AssetImageDimensions),
69});
70export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
71
72export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000;
73
74const ImageAttachmentCreateUploadUrlInput = Schema.Struct({
75 type: Schema.optionalKey(Schema.Literal("image")),
76 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
77 mimeType: Schema.Literals(PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES),
78 sizeBytes: NonNegativeInt.check(
79 Schema.isGreaterThanOrEqualTo(1),
80 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
81 ),
82});
83
84const FileAttachmentCreateUploadUrlInput = Schema.Struct({
85 type: Schema.Literal("file"),
86 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
87 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
88 sizeBytes: NonNegativeInt.check(
89 Schema.isGreaterThanOrEqualTo(1),
90 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
91 ),
92});
93
94export const AttachmentCreateUploadUrlInput = Schema.Union([
95 ImageAttachmentCreateUploadUrlInput,
96 FileAttachmentCreateUploadUrlInput,
97]);
98export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type;
99
100export const AttachmentCreateUploadUrlResult = Schema.Struct({
101 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
102 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
103 expiresAt: Schema.Number,
104});
105export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type;
106
107export const AttachmentDeleteInput = Schema.Struct({
108 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
109});
110export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type;
111
112export class AttachmentUploadSigningKeyError extends Schema.TaggedError<AttachmentUploadSigningKeyError>()(
113 "AttachmentUploadSigningKeyError",
114 {
115 cause: Schema.Defect(),
116 },
117) {
118 override get message(): string {
119 return "Failed to load the attachment upload signing key.";
120 }
121}
122
123export class AssetWorkspaceContextNotFoundError extends Schema.TaggedError<AssetWorkspaceContextNotFoundError>()(
124 "AssetWorkspaceContextNotFoundError",
125 {
126 resource: AssetResource,
127 },
128) {
129 override get message(): string {
130 return "Workspace context was not found.";
131 }
132}
133
134export class AssetWorkspaceContextResolutionError extends Schema.TaggedError<AssetWorkspaceContextResolutionError>()(
135 "AssetWorkspaceContextResolutionError",
136 {
137 resource: AssetResource,
138 cause: Schema.Defect(),
139 },
140) {
141 override get message(): string {
142 return "Failed to resolve workspace context.";
143 }
144}
145
146export class AssetWorkspaceRootNormalizationError extends Schema.TaggedError<AssetWorkspaceRootNormalizationError>()(
147 "AssetWorkspaceRootNormalizationError",
148 {
149 resource: AssetResource,
150 cause: Schema.Defect(),
151 },
152) {
153 override get message(): string {
154 return "Failed to normalize the workspace root.";
155 }
156}
157
158export class AssetWorkspacePathValidationError extends Schema.TaggedError<AssetWorkspacePathValidationError>()(
159 "AssetWorkspacePathValidationError",
160 {
161 resource: AssetResource,
162 cause: Schema.Defect(),
163 },
164) {
165 override get message(): string {
166 return "Workspace file path must be relative to the project root.";
167 }
168}
169
170export class AssetPreviewTypeValidationError extends Schema.TaggedError<AssetPreviewTypeValidationError>()(
171 "AssetPreviewTypeValidationError",
172 {
173 resource: AssetResource,
174 },
175) {
176 override get message(): string {
177 return this.resource._tag === "media-file"
178 ? "Only images, videos, HTML, and PDF files can be previewed."
179 : "Only browser documents and images can be previewed.";
180 }
181}
182
183export class AssetWorkspaceAssetInspectionError extends Schema.TaggedError<AssetWorkspaceAssetInspectionError>()(
184 "AssetWorkspaceAssetInspectionError",
185 {
186 resource: AssetResource,
187 cause: Schema.Defect(),
188 },
189) {
190 override get message(): string {
191 return this.resource._tag === "media-file"
192 ? "Failed to inspect the media file."
193 : "Failed to inspect the workspace asset.";
194 }
195}
196
197export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedError<AssetWorkspaceAssetNotFoundError>()(
198 "AssetWorkspaceAssetNotFoundError",
199 {
200 resource: AssetResource,
201 },
202) {
203 override get message(): string {
204 return this.resource._tag === "media-file"
205 ? "Media file was not found."
206 : "Workspace asset was not found.";
207 }
208}
209
210export class AssetWorkspaceResolutionError extends Schema.TaggedError<AssetWorkspaceResolutionError>()(
211 "AssetWorkspaceResolutionError",
212 {
213 resource: AssetResource,
214 cause: Schema.Defect(),
215 },
216) {
217 override get message(): string {
218 return "Failed to resolve workspace.";
219 }
220}
221
222export class AssetAttachmentNotFoundError extends Schema.TaggedError<AssetAttachmentNotFoundError>()(
223 "AssetAttachmentNotFoundError",
224 {
225 resource: AssetResource,
226 },
227) {
228 override get message(): string {
229 return "Attachment was not found.";
230 }
231}
232
233export class AssetProjectFaviconResolutionError extends Schema.TaggedError<AssetProjectFaviconResolutionError>()(
234 "AssetProjectFaviconResolutionError",
235 {
236 resource: AssetResource,
237 cause: Schema.Defect(),
238 },
239) {
240 override get message(): string {
241 return "Failed to resolve project favicon.";
242 }
243}
244
245export class AssetProjectFaviconInspectionError extends Schema.TaggedError<AssetProjectFaviconInspectionError>()(
246 "AssetProjectFaviconInspectionError",
247 {
248 resource: AssetResource,
249 cause: Schema.Defect(),
250 },
251) {
252 override get message(): string {
253 return "Failed to inspect the project favicon.";
254 }
255}
256
257export class AssetProjectFaviconNotFoundError extends Schema.TaggedError<AssetProjectFaviconNotFoundError>()(
258 "AssetProjectFaviconNotFoundError",
259 {
260 resource: AssetResource,
261 },
262) {
263 override get message(): string {
264 return "Project favicon was not found.";
265 }
266}
267
268export class AssetSigningKeyLoadError extends Schema.TaggedError<AssetSigningKeyLoadError>()(
269 "AssetSigningKeyLoadError",
270 {
271 resource: AssetResource,
272 cause: Schema.Defect(),
273 },
274) {
275 override get message(): string {
276 return "Failed to load the asset signing key.";
277 }
278}
279
280export const AssetAccessError = Schema.Union([
281 AssetWorkspaceContextNotFoundError,
282 AssetWorkspaceContextResolutionError,
283 AssetWorkspaceRootNormalizationError,
284 AssetWorkspacePathValidationError,
285 AssetPreviewTypeValidationError,
286 AssetWorkspaceAssetInspectionError,
287 AssetWorkspaceAssetNotFoundError,
288 AssetWorkspaceResolutionError,
289 AssetAttachmentNotFoundError,
290 AssetProjectFaviconResolutionError,
291 AssetProjectFaviconInspectionError,
292 AssetProjectFaviconNotFoundError,
293 AssetSigningKeyLoadError,
294]);
295export type AssetAccessError = typeof AssetAccessError.Type;