import { readFileSync } from "node:fs"; import { Ajv, type JSONSchemaType } from "ajv"; export type RunMetadata = { workspaceId: string; workerId: string; runGroupId: string; runId: string; capabilityType: "workflow"; attemptNumber: number; }; const runMetadataSchema: JSONSchemaType = { type: "object", properties: { workspaceId: { type: "string" }, workerId: { type: "string" }, runGroupId: { type: "string" }, runId: { type: "string" }, capabilityType: { type: "string", const: "workflow" }, attemptNumber: { type: "number" }, }, required: ["workspaceId", "workerId", "runGroupId", "runId", "capabilityType", "attemptNumber"], additionalProperties: false, }; const ajv = new Ajv(); const validateRunMetadata = ajv.compile(runMetadataSchema); /** @internal */ export function readRunMetadata(): RunMetadata { const runMetadataPath = process.env.NOTION_RUN_METADATA_PATH; if (runMetadataPath === undefined) { throw new Error("Workflow run metadata path is not configured"); } const parsed: unknown = JSON.parse(readFileSync(runMetadataPath, "utf8")); if (!validateRunMetadata(parsed)) { throw new Error( `Invalid workflow run metadata: ${ajv.errorsText(validateRunMetadata.errors)}`, ); } return parsed; }