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

assets.ts

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