/** * RFC-0048 ยง5.3: how `create_upload` and `complete_upload` refuse. The store's invariant codes are * answered with existing tool codes and a `details.reason`, each with the agent's next step (next * to `field-hints.ts`, one responsibility per module). Anything else goes to `mapError`. */ import { InvariantError, NotFoundError } from "@cloudhal/core-ts"; import { formatBytes } from "@cloudhal/core-ts/media"; import { mapError, ToolError, type ToolErrorCode } from "./common.ts"; export const HASH_COMMANDS = "shasum -a 256 on macOS, sha256sum on Linux, Get-FileHash -Algorithm SHA256 in PowerShell"; /** What a hint may name: the server's cap and the types agents may upload. */ export interface UploadHintInfo { readonly cap: number; readonly types: readonly string[]; } const refuse = ( code: ToolErrorCode, reason: string, message: string, details: Readonly> = {}, ) => new ToolError(code, message, { ...details, reason }); /** The answer when this server cannot hand out links (stdio, no shared secret, turned off). */ export function unsupported(): ToolError { return refuse( "invalid", "signed_upload_unsupported", "this server has no upload links (stdio, no shared signing secret, or turned off by the operator): use upload_asset with base64 (at most 4 MB)", ); } /** Maps a refusal of `create_upload`'s store side; never returns. */ export function createRefusal(e: unknown, info: UploadHintInfo): never { if (e instanceof InvariantError) { const d = e.details; switch (e.code) { case "too_large": throw refuse( "too_large", "upload_cap", `${e.message}. This server takes agent uploads up to ${formatBytes(info.cap)}; ask a person to upload it in Studio, or use fetch_asset_url for a public file`, d, ); case "unsupported_type": throw refuse( "invalid", "unsupported_type", `${e.message}. Agents may upload ${info.types.join(", ")}; SVG and HTML are refused`, d, ); case "invalid_asset": throw refuse( "invalid", "checksum_required", `${e.message}. Compute the file's SHA-256 (${HASH_COMMANDS}) and pass it as sha256`, d, ); case "too_many_uploads": throw refuse( "denied", "too_many_uploads", `${e.message}. Finish open uploads (PUT, then complete_upload) or let them expire: links that were never used expire 10 minutes after create_upload`, d, ); case "quota_exceeded": throw refuse("denied", "quota_exceeded", e.message, d); } } mapError(e); } /** Maps a refusal of `complete_upload`; never returns. */ export function completeRefusal(e: unknown): never { if (e instanceof NotFoundError && e.kind === "upload") throw refuse( "not_found", "upload_not_found", "no such upload for this key; call create_upload", ); if (e instanceof InvariantError) { const d = e.details; const attempts = typeof d.attempts === "number" ? d.attempts : 0; if (e.code === "upload_pending" && attempts >= 3) throw refuse( "invalid", "too_many_attempts", "three PUTs of this upload were refused; call create_upload again", d, ); if (e.code === "upload_pending" && attempts > 0) throw refuse( "conflict", "upload_failed", `the last PUT was refused (wrong size, type or SHA-256); PUT the exact file again before ${String(d.expiresAt)} (${attempts} of 3 attempts used), or call create_upload again`, d, ); if (e.code === "upload_pending") throw refuse( "conflict", "upload_pending", `PUT the file to the url from create_upload first (the curl line), then call complete_upload again; start before ${String(d.expiresAt)}`, d, ); if (e.code === "upload_expired") throw refuse( "invalid", "upload_expired", `the upload expired before the bytes arrived (start the PUT by ${String(d.expiresAt)}) or before it was completed (by ${String(d.completeBy)}); call create_upload again`, d, ); } mapError(e); }