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

assets.ts

329 lines11.3 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​
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 // An image a tool returned inline, such as a device screenshot, by its order
45 // in the stored output. The timeline never carries these bytes.
46 Schema.TaggedStruct("tool-output-image", {
47 threadId: ThreadId,
48 itemId: TurnItemId,
49 index: NonNegativeInt,
50 }),
51 Schema.TaggedStruct("project-favicon", {
52 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
53 // A cache-key hint only. The server reads the authoritative path from the
54 // project projection before it issues the signed URL.
55 path: Schema.optional(ProjectFaviconPath),
56 }),
57 Schema.TaggedStruct("native-app-icon", {
58 app: ToolActivityNativeAppReference,
59 }),
60 // An upload a pull request body points at on GitHub. A private repository serves these only
61 // to a request that carries a credential, which the client has none of, so the server fetches
62 // them with the `gh` credential the repository at `cwd` authenticates with.
63 Schema.TaggedStruct("github-media", {
64 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
65 url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)),
66 }),
67]);
68export type AssetResource = typeof AssetResource.Type;
69​
70export const AssetCreateUrlInput = Schema.Struct({
71 resource: AssetResource,
72});
73export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type;
74​
75export const AssetImageDimensions = Schema.Struct({
76 width: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
77 height: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
78});
79export type AssetImageDimensions = typeof AssetImageDimensions.Type;
80​
81export const AssetCreateUrlResult = Schema.Struct({
82 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
83 expiresAt: Schema.Number,
84 sourcePath: Schema.optional(
85 TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
86 ),
87 /** Pixel size read from the image header, so a client can reserve the exact box before the bytes arrive. */
88 imageDimensions: Schema.optional(AssetImageDimensions),
89});
90export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
91​
92export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000;
93​
94const ImageAttachmentCreateUploadUrlInput = Schema.Struct({
95 type: Schema.optionalKey(Schema.Literal("image")),
96 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
97 mimeType: Schema.Literals(PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES),
98 sizeBytes: NonNegativeInt.check(
99 Schema.isGreaterThanOrEqualTo(1),
100 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
101 ),
102});
103​
104const FileAttachmentCreateUploadUrlInput = Schema.Struct({
105 type: Schema.Literal("file"),
106 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
107 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
108 sizeBytes: NonNegativeInt.check(
109 Schema.isGreaterThanOrEqualTo(1),
110 Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
111 ),
112});
113​
114export const AttachmentCreateUploadUrlInput = Schema.Union([
115 ImageAttachmentCreateUploadUrlInput,
116 FileAttachmentCreateUploadUrlInput,
117]);
118export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type;
119​
120export const AttachmentCreateUploadUrlResult = Schema.Struct({
121 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
122 relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
123 expiresAt: Schema.Number,
124});
125export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type;
126​
127export const AttachmentDeleteInput = Schema.Struct({
128 attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
129});
130export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type;
131​
132export class AttachmentUploadSigningKeyError extends Schema.TaggedError<AttachmentUploadSigningKeyError>()(
133 "AttachmentUploadSigningKeyError",
134 {
135 cause: Schema.Defect(),
136 },
137) {
138 override get message(): string {
139 return "Failed to load the attachment upload signing key.";
140 }
142​
143export class AssetWorkspaceContextNotFoundError extends Schema.TaggedError<AssetWorkspaceContextNotFoundError>()(
144 "AssetWorkspaceContextNotFoundError",
145 {
146 resource: AssetResource,
147 },
148) {
149 override get message(): string {
150 return "Workspace context was not found.";
151 }
153​
154export class AssetWorkspaceContextResolutionError extends Schema.TaggedError<AssetWorkspaceContextResolutionError>()(
155 "AssetWorkspaceContextResolutionError",
156 {
157 resource: AssetResource,
158 cause: Schema.Defect(),
159 },
160) {
161 override get message(): string {
162 return "Failed to resolve workspace context.";
163 }
165​
166export class AssetWorkspaceRootNormalizationError extends Schema.TaggedError<AssetWorkspaceRootNormalizationError>()(
167 "AssetWorkspaceRootNormalizationError",
168 {
169 resource: AssetResource,
170 cause: Schema.Defect(),
171 },
172) {
173 override get message(): string {
174 return "Failed to normalize the workspace root.";
175 }
177​
178export class AssetWorkspacePathValidationError extends Schema.TaggedError<AssetWorkspacePathValidationError>()(
179 "AssetWorkspacePathValidationError",
180 {
181 resource: AssetResource,
182 cause: Schema.Defect(),
183 },
184) {
185 override get message(): string {
186 return "Workspace file path must be relative to the project root.";
187 }
189​
190export class AssetPreviewTypeValidationError extends Schema.TaggedError<AssetPreviewTypeValidationError>()(
191 "AssetPreviewTypeValidationError",
192 {
193 resource: AssetResource,
194 },
195) {
196 override get message(): string {
197 // Draft resources serve absolute paths through the same host-media
198 // validation as media files, so they share its message.
199 return this.resource._tag === "media-file" || this.resource._tag === "draft-workspace-file"
200 ? "Only images, videos, audio, HTML, and PDF files can be previewed."
201 : "Only browser documents and images can be previewed.";
202 }
204​
205export class AssetWorkspaceAssetInspectionError extends Schema.TaggedError<AssetWorkspaceAssetInspectionError>()(
206 "AssetWorkspaceAssetInspectionError",
207 {
208 resource: AssetResource,
209 cause: Schema.Defect(),
210 },
211) {
212 override get message(): string {
213 return this.resource._tag === "media-file"
214 ? "Failed to inspect the media file."
215 : "Failed to inspect the workspace asset.";
216 }
218​
219export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedError<AssetWorkspaceAssetNotFoundError>()(
220 "AssetWorkspaceAssetNotFoundError",
221 {
222 resource: AssetResource,
223 },
224) {
225 override get message(): string {
226 return this.resource._tag === "media-file"
227 ? "Media file was not found."
228 : this.resource._tag === "tool-output-image"
229 ? "Tool output image was not found."
230 : "Workspace asset was not found.";
231 }
233​
234export class AssetWorkspaceResolutionError extends Schema.TaggedError<AssetWorkspaceResolutionError>()(
235 "AssetWorkspaceResolutionError",
236 {
237 resource: AssetResource,
238 cause: Schema.Defect(),
239 },
240) {
241 override get message(): string {
242 return "Failed to resolve workspace.";
243 }
245​
246export class AssetAttachmentNotFoundError extends Schema.TaggedError<AssetAttachmentNotFoundError>()(
247 "AssetAttachmentNotFoundError",
248 {
249 resource: AssetResource,
250 },
251) {
252 override get message(): string {
253 return "Attachment was not found.";
254 }
256​
257export class AssetProjectFaviconResolutionError extends Schema.TaggedError<AssetProjectFaviconResolutionError>()(
258 "AssetProjectFaviconResolutionError",
259 {
260 resource: AssetResource,
261 cause: Schema.Defect(),
262 },
263) {
264 override get message(): string {
265 return "Failed to resolve project favicon.";
266 }
268​
269export class AssetProjectFaviconInspectionError extends Schema.TaggedError<AssetProjectFaviconInspectionError>()(
270 "AssetProjectFaviconInspectionError",
271 {
272 resource: AssetResource,
273 cause: Schema.Defect(),
274 },
275) {
276 override get message(): string {
277 return "Failed to inspect the project favicon.";
278 }
280​
281export class AssetProjectFaviconNotFoundError extends Schema.TaggedError<AssetProjectFaviconNotFoundError>()(
282 "AssetProjectFaviconNotFoundError",
283 {
284 resource: AssetResource,
285 },
286) {
287 override get message(): string {
288 return "Project favicon was not found.";
289 }
291​
292export class AssetSigningKeyLoadError extends Schema.TaggedError<AssetSigningKeyLoadError>()(
293 "AssetSigningKeyLoadError",
294 {
295 resource: AssetResource,
296 cause: Schema.Defect(),
297 },
298) {
299 override get message(): string {
300 return "Failed to load the asset signing key.";
301 }
303​
304export class AssetGitHubMediaUrlValidationError extends Schema.TaggedError<AssetGitHubMediaUrlValidationError>()(
305 "AssetGitHubMediaUrlValidationError",
306 {},
307) {
308 override get message(): string {
309 return "Only media hosted by GitHub can be fetched with a GitHub credential.";
310 }
312​
313export const AssetAccessError = Schema.Union([
314 AssetWorkspaceContextNotFoundError,
315 AssetWorkspaceContextResolutionError,
316 AssetWorkspaceRootNormalizationError,
317 AssetWorkspacePathValidationError,
318 AssetPreviewTypeValidationError,
319 AssetWorkspaceAssetInspectionError,
320 AssetWorkspaceAssetNotFoundError,
321 AssetWorkspaceResolutionError,
322 AssetAttachmentNotFoundError,
323 AssetProjectFaviconResolutionError,
324 AssetProjectFaviconInspectionError,
325 AssetProjectFaviconNotFoundError,
326 AssetGitHubMediaUrlValidationError,
327 AssetSigningKeyLoadError,
328]);
329export type AssetAccessError = typeof AssetAccessError.Type;