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

assets.ts

280 lines8.9 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 }),
34 Schema.TaggedStruct("project-favicon", {
35 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
36 // A cache-key hint only. The server reads the authoritative path from the
37 // project projection before it issues the signed URL.
38 path: Schema.optional(ProjectFaviconPath),
39 }),
40]);
41export type AssetResource = typeof AssetResource.Type;
42​
43export const AssetCreateUrlInput = Schema.Struct({
44 resource: AssetResource,
45});
46export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type;
47​
48export const AssetCreateUrlResult = Schema.Struct({
49 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
50 expiresAt: Schema.Number,
51 sourcePath: Schema.optional(
52 TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
53 ),
54});
55export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
56​
57export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000;
58​
59const ImageAttachmentCreateUploadUrlInput = Schema.Struct({
60 type: Schema.optionalKey(Schema.Literal("image")),
61 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
62 mimeType: Schema.Literals(PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES),
63 sizeBytes: NonNegativeInt.check(
64 Schema.isGreaterThanOrEqualTo(1),
65 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
66 ),
67});
68​
69const FileAttachmentCreateUploadUrlInput = Schema.Struct({
70 type: Schema.Literal("file"),
71 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
72 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
73 sizeBytes: NonNegativeInt.check(
74 Schema.isGreaterThanOrEqualTo(1),
75 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
76 ),
77});
78​
79export const AttachmentCreateUploadUrlInput = Schema.Union([
80 ImageAttachmentCreateUploadUrlInput,
81 FileAttachmentCreateUploadUrlInput,
82]);
83export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type;
84​
85export const AttachmentCreateUploadUrlResult = Schema.Struct({
86 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
87 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
88 expiresAt: Schema.Number,
89});
90export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type;
91​
92export const AttachmentDeleteInput = Schema.Struct({
93 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
94});
95export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type;
96​
97export class AttachmentUploadSigningKeyError extends Schema.TaggedErrorClass<AttachmentUploadSigningKeyError>()(
98 "AttachmentUploadSigningKeyError",
99 {
100 cause: Schema.Defect(),
101 },
102) {
103 override get message(): string {
104 return "Failed to load the attachment upload signing key.";
105 }
107​
108export class AssetWorkspaceContextNotFoundError extends Schema.TaggedErrorClass<AssetWorkspaceContextNotFoundError>()(
109 "AssetWorkspaceContextNotFoundError",
110 {
111 resource: AssetResource,
112 },
113) {
114 override get message(): string {
115 return "Workspace context was not found.";
116 }
118​
119export class AssetWorkspaceContextResolutionError extends Schema.TaggedErrorClass<AssetWorkspaceContextResolutionError>()(
120 "AssetWorkspaceContextResolutionError",
121 {
122 resource: AssetResource,
123 cause: Schema.Defect(),
124 },
125) {
126 override get message(): string {
127 return "Failed to resolve workspace context.";
128 }
130​
131export class AssetWorkspaceRootNormalizationError extends Schema.TaggedErrorClass<AssetWorkspaceRootNormalizationError>()(
132 "AssetWorkspaceRootNormalizationError",
133 {
134 resource: AssetResource,
135 cause: Schema.Defect(),
136 },
137) {
138 override get message(): string {
139 return "Failed to normalize the workspace root.";
140 }
142​
143export class AssetWorkspacePathValidationError extends Schema.TaggedErrorClass<AssetWorkspacePathValidationError>()(
144 "AssetWorkspacePathValidationError",
145 {
146 resource: AssetResource,
147 cause: Schema.Defect(),
148 },
149) {
150 override get message(): string {
151 return "Workspace file path must be relative to the project root.";
152 }
154​
155export class AssetPreviewTypeValidationError extends Schema.TaggedErrorClass<AssetPreviewTypeValidationError>()(
156 "AssetPreviewTypeValidationError",
157 {
158 resource: AssetResource,
159 },
160) {
161 override get message(): string {
162 return this.resource._tag === "media-file"
163 ? "Only images, videos, HTML, and PDF files can be previewed."
164 : "Only browser documents and images can be previewed.";
165 }
167​
168export class AssetWorkspaceAssetInspectionError extends Schema.TaggedErrorClass<AssetWorkspaceAssetInspectionError>()(
169 "AssetWorkspaceAssetInspectionError",
170 {
171 resource: AssetResource,
172 cause: Schema.Defect(),
173 },
174) {
175 override get message(): string {
176 return this.resource._tag === "media-file"
177 ? "Failed to inspect the media file."
178 : "Failed to inspect the workspace asset.";
179 }
181​
182export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedErrorClass<AssetWorkspaceAssetNotFoundError>()(
183 "AssetWorkspaceAssetNotFoundError",
184 {
185 resource: AssetResource,
186 },
187) {
188 override get message(): string {
189 return this.resource._tag === "media-file"
190 ? "Media file was not found."
191 : "Workspace asset was not found.";
192 }
194​
195export class AssetWorkspaceResolutionError extends Schema.TaggedErrorClass<AssetWorkspaceResolutionError>()(
196 "AssetWorkspaceResolutionError",
197 {
198 resource: AssetResource,
199 cause: Schema.Defect(),
200 },
201) {
202 override get message(): string {
203 return "Failed to resolve workspace.";
204 }
206​
207export class AssetAttachmentNotFoundError extends Schema.TaggedErrorClass<AssetAttachmentNotFoundError>()(
208 "AssetAttachmentNotFoundError",
209 {
210 resource: AssetResource,
211 },
212) {
213 override get message(): string {
214 return "Attachment was not found.";
215 }
217​
218export class AssetProjectFaviconResolutionError extends Schema.TaggedErrorClass<AssetProjectFaviconResolutionError>()(
219 "AssetProjectFaviconResolutionError",
220 {
221 resource: AssetResource,
222 cause: Schema.Defect(),
223 },
224) {
225 override get message(): string {
226 return "Failed to resolve project favicon.";
227 }
229​
230export class AssetProjectFaviconInspectionError extends Schema.TaggedErrorClass<AssetProjectFaviconInspectionError>()(
231 "AssetProjectFaviconInspectionError",
232 {
233 resource: AssetResource,
234 cause: Schema.Defect(),
235 },
236) {
237 override get message(): string {
238 return "Failed to inspect the project favicon.";
239 }
241​
242export class AssetProjectFaviconNotFoundError extends Schema.TaggedErrorClass<AssetProjectFaviconNotFoundError>()(
243 "AssetProjectFaviconNotFoundError",
244 {
245 resource: AssetResource,
246 },
247) {
248 override get message(): string {
249 return "Project favicon was not found.";
250 }
252​
253export class AssetSigningKeyLoadError extends Schema.TaggedErrorClass<AssetSigningKeyLoadError>()(
254 "AssetSigningKeyLoadError",
255 {
256 resource: AssetResource,
257 cause: Schema.Defect(),
258 },
259) {
260 override get message(): string {
261 return "Failed to load the asset signing key.";
262 }
264​
265export const AssetAccessError = Schema.Union([
266 AssetWorkspaceContextNotFoundError,
267 AssetWorkspaceContextResolutionError,
268 AssetWorkspaceRootNormalizationError,
269 AssetWorkspacePathValidationError,
270 AssetPreviewTypeValidationError,
271 AssetWorkspaceAssetInspectionError,
272 AssetWorkspaceAssetNotFoundError,
273 AssetWorkspaceResolutionError,
274 AssetAttachmentNotFoundError,
275 AssetProjectFaviconResolutionError,
276 AssetProjectFaviconInspectionError,
277 AssetProjectFaviconNotFoundError,
278 AssetSigningKeyLoadError,
279]);
280export type AssetAccessError = typeof AssetAccessError.Type;