Files
t3code/packages/contracts/src/assets.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

321 lines
11 KiB
TypeScript

import * as Schema from "effect/Schema";
import { NonNegativeInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
import {
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES,
} from "./chatAttachment.ts";
import { ProjectFaviconPath } from "./project.ts";
import { ToolActivityNativeAppReference } from "./providerRuntime.ts";
const ASSET_PATH_MAX_LENGTH = 1024;
export const AssetResource = Schema.Union([
Schema.TaggedStruct("workspace-file", {
threadId: ThreadId,
path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
}),
// One file served in place from anywhere the environment host can read:
// images, videos, HTML, and PDF. An absolute path may lie outside the
// workspace; a relative one resolves against the thread's workspace.
Schema.TaggedStruct("media-file", {
threadId: ThreadId,
path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
}),
// A workspace file named by a draft that has no thread yet. The draft names
// its workspace root explicitly instead of resolving one from a thread.
Schema.TaggedStruct("draft-workspace-file", {
cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
path: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
}),
Schema.TaggedStruct("attachment", {
attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
/** Display name and mime from the `ChatAttachment` the caller holds. The
server bakes both into the signed URL so downloads carry the real
filename and Content-Type. Absent on older clients, which fall back to
an octet-stream download without a filename. */
fileName: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(255))),
mimeType: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
/** Generic attachments download by default. Document viewers opt into an
inline response after deciding the file type is safe to preview. */
disposition: Schema.optionalKey(Schema.Literals(["inline", "attachment"])),
}),
Schema.TaggedStruct("project-favicon", {
cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
// A cache-key hint only. The server reads the authoritative path from the
// project projection before it issues the signed URL.
path: Schema.optional(ProjectFaviconPath),
}),
Schema.TaggedStruct("native-app-icon", {
app: ToolActivityNativeAppReference,
}),
// An upload a pull request body points at on GitHub. A private repository serves these only
// to a request that carries a credential, which the client has none of, so the server fetches
// them with the `gh` credential the repository at `cwd` authenticates with.
Schema.TaggedStruct("github-media", {
cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)),
}),
]);
export type AssetResource = typeof AssetResource.Type;
export const AssetCreateUrlInput = Schema.Struct({
resource: AssetResource,
});
export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type;
export const AssetImageDimensions = Schema.Struct({
width: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
height: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
});
export type AssetImageDimensions = typeof AssetImageDimensions.Type;
export const AssetCreateUrlResult = Schema.Struct({
relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
expiresAt: Schema.Number,
sourcePath: Schema.optional(
TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
),
/** Pixel size read from the image header, so a client can reserve the exact box before the bytes arrive. */
imageDimensions: Schema.optional(AssetImageDimensions),
});
export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000;
const ImageAttachmentCreateUploadUrlInput = Schema.Struct({
type: Schema.optionalKey(Schema.Literal("image")),
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: Schema.Literals(PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES),
sizeBytes: NonNegativeInt.check(
Schema.isGreaterThanOrEqualTo(1),
Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
),
});
const FileAttachmentCreateUploadUrlInput = Schema.Struct({
type: Schema.Literal("file"),
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100)),
sizeBytes: NonNegativeInt.check(
Schema.isGreaterThanOrEqualTo(1),
Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_FILE_BYTES),
),
});
export const AttachmentCreateUploadUrlInput = Schema.Union([
ImageAttachmentCreateUploadUrlInput,
FileAttachmentCreateUploadUrlInput,
]);
export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type;
export const AttachmentCreateUploadUrlResult = Schema.Struct({
attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
expiresAt: Schema.Number,
});
export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type;
export const AttachmentDeleteInput = Schema.Struct({
attachmentId: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
});
export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type;
export class AttachmentUploadSigningKeyError extends Schema.TaggedError<AttachmentUploadSigningKeyError>()(
"AttachmentUploadSigningKeyError",
{
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to load the attachment upload signing key.";
}
}
export class AssetWorkspaceContextNotFoundError extends Schema.TaggedError<AssetWorkspaceContextNotFoundError>()(
"AssetWorkspaceContextNotFoundError",
{
resource: AssetResource,
},
) {
override get message(): string {
return "Workspace context was not found.";
}
}
export class AssetWorkspaceContextResolutionError extends Schema.TaggedError<AssetWorkspaceContextResolutionError>()(
"AssetWorkspaceContextResolutionError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to resolve workspace context.";
}
}
export class AssetWorkspaceRootNormalizationError extends Schema.TaggedError<AssetWorkspaceRootNormalizationError>()(
"AssetWorkspaceRootNormalizationError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to normalize the workspace root.";
}
}
export class AssetWorkspacePathValidationError extends Schema.TaggedError<AssetWorkspacePathValidationError>()(
"AssetWorkspacePathValidationError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Workspace file path must be relative to the project root.";
}
}
export class AssetPreviewTypeValidationError extends Schema.TaggedError<AssetPreviewTypeValidationError>()(
"AssetPreviewTypeValidationError",
{
resource: AssetResource,
},
) {
override get message(): string {
// Draft resources serve absolute paths through the same host-media
// validation as media files, so they share its message.
return this.resource._tag === "media-file" || this.resource._tag === "draft-workspace-file"
? "Only images, videos, audio, HTML, and PDF files can be previewed."
: "Only browser documents and images can be previewed.";
}
}
export class AssetWorkspaceAssetInspectionError extends Schema.TaggedError<AssetWorkspaceAssetInspectionError>()(
"AssetWorkspaceAssetInspectionError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return this.resource._tag === "media-file"
? "Failed to inspect the media file."
: "Failed to inspect the workspace asset.";
}
}
export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedError<AssetWorkspaceAssetNotFoundError>()(
"AssetWorkspaceAssetNotFoundError",
{
resource: AssetResource,
},
) {
override get message(): string {
return this.resource._tag === "media-file"
? "Media file was not found."
: "Workspace asset was not found.";
}
}
export class AssetWorkspaceResolutionError extends Schema.TaggedError<AssetWorkspaceResolutionError>()(
"AssetWorkspaceResolutionError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to resolve workspace.";
}
}
export class AssetAttachmentNotFoundError extends Schema.TaggedError<AssetAttachmentNotFoundError>()(
"AssetAttachmentNotFoundError",
{
resource: AssetResource,
},
) {
override get message(): string {
return "Attachment was not found.";
}
}
export class AssetProjectFaviconResolutionError extends Schema.TaggedError<AssetProjectFaviconResolutionError>()(
"AssetProjectFaviconResolutionError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to resolve project favicon.";
}
}
export class AssetProjectFaviconInspectionError extends Schema.TaggedError<AssetProjectFaviconInspectionError>()(
"AssetProjectFaviconInspectionError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to inspect the project favicon.";
}
}
export class AssetProjectFaviconNotFoundError extends Schema.TaggedError<AssetProjectFaviconNotFoundError>()(
"AssetProjectFaviconNotFoundError",
{
resource: AssetResource,
},
) {
override get message(): string {
return "Project favicon was not found.";
}
}
export class AssetSigningKeyLoadError extends Schema.TaggedError<AssetSigningKeyLoadError>()(
"AssetSigningKeyLoadError",
{
resource: AssetResource,
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to load the asset signing key.";
}
}
export class AssetGitHubMediaUrlValidationError extends Schema.TaggedError<AssetGitHubMediaUrlValidationError>()(
"AssetGitHubMediaUrlValidationError",
{},
) {
override get message(): string {
return "Only media hosted by GitHub can be fetched with a GitHub credential.";
}
}
export const AssetAccessError = Schema.Union([
AssetWorkspaceContextNotFoundError,
AssetWorkspaceContextResolutionError,
AssetWorkspaceRootNormalizationError,
AssetWorkspacePathValidationError,
AssetPreviewTypeValidationError,
AssetWorkspaceAssetInspectionError,
AssetWorkspaceAssetNotFoundError,
AssetWorkspaceResolutionError,
AssetAttachmentNotFoundError,
AssetProjectFaviconResolutionError,
AssetProjectFaviconInspectionError,
AssetProjectFaviconNotFoundError,
AssetGitHubMediaUrlValidationError,
AssetSigningKeyLoadError,
]);
export type AssetAccessError = typeof AssetAccessError.Type;