/** * RFC-0048 ยง3.3 and "Capability flag": the two `cloudhal.json` `media` keys of agents' upload * links, read leniently (a value of the wrong type is left out, like the rest of the file). * `media` stays the starter's key (`OWN_KEYS` keeps it as written); this module only reads it. * * - `media.agentUploads: false` turns the links off (`get_mode.uploads.signedUrl` false); * - `media.agentUploadBytes`: the cap per file, 1 byte to 100 MB (default 25 MB). * * On Cloudflare they reach the admin Worker as the vars `CLOUDHAL_AGENT_UPLOADS` and * `CLOUDHAL_AGENT_UPLOAD_BYTES`; on self-host the operator sets those vars; `cloudhal studio` reads * them from the project directly. */ import { readFile } from "node:fs/promises"; import path from "node:path"; import { CONFIG_FILE } from "./config.ts"; export interface AgentUploadsConfig { readonly agentUploads?: false; readonly agentUploadBytes?: number; } const MAX_BYTES = 100 * 1024 * 1024; /** The keys from a parsed `cloudhal.json`; undefined when neither is set (validly). */ export function parseAgentUploads(raw: unknown): AgentUploadsConfig | undefined { const media = (raw as { media?: unknown } | undefined)?.media; if (!media || typeof media !== "object") return undefined; const m = media as Record; const bytes = m.agentUploadBytes; const out: AgentUploadsConfig = { ...(m.agentUploads === false ? { agentUploads: false as const } : {}), ...(typeof bytes === "number" && Number.isInteger(bytes) && bytes >= 1 && bytes <= MAX_BYTES ? { agentUploadBytes: bytes } : {}), }; return Object.keys(out).length > 0 ? out : undefined; } /** The project's settings; undefined without a readable `cloudhal.json` or without the keys. */ export async function readAgentUploads( projectDir: string, ): Promise { try { return parseAgentUploads( JSON.parse(await readFile(path.join(projectDir, CONFIG_FILE), "utf8")), ); } catch { return undefined; } } /** The admin Worker's vars for them (none when unset). */ export function agentUploadVars(c: AgentUploadsConfig | undefined): Record { return { ...(c?.agentUploads === false ? { CLOUDHAL_AGENT_UPLOADS: "off" } : {}), ...(c?.agentUploadBytes ? { CLOUDHAL_AGENT_UPLOAD_BYTES: String(c.agentUploadBytes) } : {}), }; }