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

assets.ts

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