import * as z from "zod/mini"; import { coreCommandKindSchema, coreCommandKindWithoutFlowResultSchema, } from "../command-kinds.js"; import { generationSchema } from "./v3-generation.js"; const canonicalIdStringSchema = z.string().check(z.trim(), z.minLength(1)); /** * Aggregate browser Loader-Core runtime revision required before lifecycle * admission. This is compatibility evidence, not caller authorization. See * docs/specs/2026-08-02-flow-interaction-model.md. */ export const UNIFIED_FLOW_RUNTIME_V1_CAPABILITY = "unified-flow-runtime-v1" as const; const coreHostCommandSettlementEnvelopeSchema = z.object({ t: z.string().check(z.startsWith("getuserfeedback:")), gen: generationSchema, viewId: z.optional(canonicalIdStringSchema), }); const commandSettlementErrorSchema = z.object({ message: z.string().check(z.minLength(1)), code: z.optional(z.string().check(z.minLength(1))), }); const commandSettledFlowHandleResultSchema = z.strictObject({ flowHandleId: canonicalIdStringSchema, flowRunId: z.optional(canonicalIdStringSchema), }); const commandSettledFlowRunResultSchema = z.strictObject({ flowHandleId: canonicalIdStringSchema, flowRunId: canonicalIdStringSchema, }); const commandSettledBaseSchema = z.extend( coreHostCommandSettlementEnvelopeSchema, { t: z.literal("getuserfeedback:event:commandSettled"), requestId: canonicalIdStringSchema, kind: coreCommandKindSchema, instanceId: z.optional(canonicalIdStringSchema), }, ); const optionalFlowKeySchema = { flowKey: z.optional(canonicalIdStringSchema), } as const; const commandSettledSuccessOpenSchema = z.extend(commandSettledBaseSchema, { kind: z.literal("open"), ok: z.literal(true), result: commandSettledFlowRunResultSchema, ...optionalFlowKeySchema, }); const commandSettledSuccessPrerenderSchema = z.extend( commandSettledBaseSchema, { kind: z.literal("prerender"), ok: z.literal(true), result: commandSettledFlowRunResultSchema, ...optionalFlowKeySchema, }, ); const commandSettledSuccessPrefetchSchema = z.extend(commandSettledBaseSchema, { kind: z.literal("prefetch"), ok: z.literal(true), result: commandSettledFlowHandleResultSchema, ...optionalFlowKeySchema, }); const commandSettledSuccessGenericSchema = z.extend(commandSettledBaseSchema, { kind: coreCommandKindWithoutFlowResultSchema, ok: z.literal(true), result: z.optional(z.unknown()), }); const eventCommandSettledSuccessSchema = z.discriminatedUnion("kind", [ commandSettledSuccessOpenSchema, commandSettledSuccessPrerenderSchema, commandSettledSuccessPrefetchSchema, commandSettledSuccessGenericSchema, ]); const eventCommandSettledFailureSchema = z.extend(commandSettledBaseSchema, { ok: z.literal(false), error: commandSettlementErrorSchema, }); export const eventCommandSettledSchema = z.discriminatedUnion("ok", [ eventCommandSettledSuccessSchema, eventCommandSettledFailureSchema, ]); export type EventCommandSettled = z.output;