Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

assets.ts

336 lines11.5 KB

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