import { ActionError, defineAction } from "astro:actions"; import { z } from "astro/zod"; import { MAX_INBOX_ATTACHMENTS, parsedRequestBodySize, requireProject, submitInboxMessage, updateInstructions, } from "../core/index.js"; import { resolveWebProject } from "../web/projects.js"; export const server = { updateInstructions: defineAction({ accept: "form", input: z.object({ body: z.union([z.string(), z.null()]).transform((value) => value ?? ""), projectKey: z.string().min(1).optional(), }), handler: async ({ body, projectKey }) => { const { project } = await resolveWebProject(projectKey); const result = await updateInstructions(body, project.root); return { body: result.record.body, path: result.path, changed: result.changed, created: result.created, commitSha: result.commitSha, }; }, }), submitInboxMessage: defineAction({ accept: "form", input: z.object({ id: z.string(), body: z.union([z.string(), z.null()]).transform((value) => value ?? ""), authorName: z.string().min(1), authorEmail: z.email(), projectKey: z.string().min(1).optional(), attachments: z .array(z.instanceof(File)) .max(MAX_INBOX_ATTACHMENTS) .default([]), }), handler: async (input, context) => { const { project } = await resolveWebProject(input.projectKey); const owningProject = await requireProject(project.root); const requestBodySize = parsedRequestBodySize(context.request, [ input.id, input.body, input.authorName, input.authorEmail, ...input.attachments, ]); const maxRequestBodyBytes = owningProject.config.web.max_request_body_bytes; if (requestBodySize > maxRequestBodyBytes) { throw new ActionError({ code: "CONTENT_TOO_LARGE", message: `Request body exceeds configured limit of ${maxRequestBodyBytes} bytes`, }); } const result = await submitInboxMessage( { id: input.id, body: input.body, authorName: input.authorName, authorEmail: input.authorEmail, attachments: await Promise.all( input.attachments.map(async (attachment) => ({ name: attachment.name, mediaType: attachment.type, data: new Uint8Array(await attachment.arrayBuffer()), })), ), }, project.root, ); return { id: result.record.data.id, path: result.path, commitSha: result.commitSha, created: result.created ?? false, gitDelivery: result.gitDelivery, }; }, }), };