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

assets.ts

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