Something went wrong. Try again.
A local-first event pipeline for independent agents, built on Jazz.
Something went wrong. Try again.
3.6 kB · 70 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { z } from "zod";import type { JsonObject } from "../core/json.js";import { ARTIFACT_KINDS, ARTIFACT_MAX_IMAGE_BYTES, ARTIFACT_MAX_SUMMARY_LENGTH, ARTIFACT_MAX_TEXT_BYTES, ARTIFACT_MAX_TITLE_LENGTH, ARTIFACT_MEDIA_TYPES, ARTIFACT_REQUEST_STATUSES, ARTIFACT_VISIBILITY,} from "./types.js";
const sha256Schema = z.string().regex(/^[a-f0-9]{64}$/);const bounded = z.string().min(1).max(200);const notAbsolute = (value: string) => !/^(?:\/|[A-Za-z]:[\\/])/.test(value);const labelSchema = bounded.refine(notAbsolute, "Label must not be an absolute filesystem path");const relativePathSchema = z.string().min(1).max(500).refine((value) => { if (value.includes("\\") || value.startsWith("/") || value.endsWith("/")) return false; const parts = value.split("/"); return parts.every((part) => part.length > 0 && part !== "." && part !== "..");}, "Artifact file path must be one normalized relative path");
const provenanceSchema = z.object({ source: labelSchema, label: labelSchema }).strict();const relationSchema = z.object({ type: bounded, eventId: bounded }).strict();const commonMetadata = { artifactId: bounded, artifactVersion: z.number().int().positive().max(1_000_000), kind: z.enum(ARTIFACT_KINDS), title: z.string().min(1).max(ARTIFACT_MAX_TITLE_LENGTH), summary: z.string().min(1).max(ARTIFACT_MAX_SUMMARY_LENGTH), mediaType: z.enum(ARTIFACT_MEDIA_TYPES), provenance: provenanceSchema, visibility: z.enum(ARTIFACT_VISIBILITY), publicationEligible: z.literal(false),};
export const artifactPayloadSchema = z.object({ ...commonMetadata, blob: z.object({ algorithm: z.literal("sha256"), sha256: sha256Schema, relativePath: z.string().regex(/^sha256\/[a-f0-9]{2}\/[a-f0-9]{64}$/), byteCount: z.number().int().positive().max(ARTIFACT_MAX_IMAGE_BYTES), }).strict().superRefine((blob, context) => { if (blob.relativePath !== `sha256/${blob.sha256.slice(0, 2)}/${blob.sha256}`) { context.addIssue({ code: "custom", path: ["relativePath"], message: "Blob path is not canonical for its SHA-256" }); } }), relations: z.array(relationSchema).min(1).max(50).refine( (relations) => relations.some((relation) => relation.type === "materialized-from-request"), "Artifact must strongly relate to its materialization request", ), supersedesArtifactEventId: bounded.optional(),}).strict().superRefine((value, context) => { const max = value.mediaType.startsWith("image/") ? ARTIFACT_MAX_IMAGE_BYTES : ARTIFACT_MAX_TEXT_BYTES; if (value.blob.byteCount > max) context.addIssue({ code: "custom", path: ["blob", "byteCount"], message: `Artifact exceeds ${max} bytes` });}) as unknown as z.ZodType<JsonObject>;
export const artifactRequestPayloadSchema = z.object({ requestId: bounded, workspaceLeaseId: bounded, workspaceRootLabel: labelSchema, relativeFilePath: relativePathSchema, ...commonMetadata, publicationAuthority: z.literal(false), status: z.enum(ARTIFACT_REQUEST_STATUSES), reasonCode: z.string().min(1).max(100).regex(/^[a-z0-9-]+$/).optional(), requestingAgentId: bounded.optional(), requestingRunId: bounded.optional(), sourceEventId: bounded.optional(), supersedesArtifactEventId: bounded.optional(),}).strict().superRefine((value, context) => { if (value.status === "pending" && value.reasonCode) context.addIssue({ code: "custom", path: ["reasonCode"], message: "Pending request cannot have a reason code" }); if (value.status !== "pending" && !value.reasonCode) context.addIssue({ code: "custom", path: ["reasonCode"], message: "Terminal request requires a reason code" });}) as unknown as z.ZodType<JsonObject>;