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

assets.ts

303 lines10.1 KB

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