import * as z from "zod/mini"; import type { CoreCommandEnvelope } from "../core-command-envelope.js"; import { commandEnvelopeSchema } from "../host/command-envelope-schema.js"; import { type InternalHostInlineFlowCommandPayload, internalHostCommandPayloadSchema, toCoreInlineFlowCommandPayload, } from "./host-command-payload.js"; const instanceIdSchema = z.string().check(z.trim(), z.minLength(1)); /** @internal Command envelope accepted by first-party widget hosts. */ export const internalHostCommandEnvelopeSchema = z.extend( commandEnvelopeSchema, { command: internalHostCommandPayloadSchema, }, ); /** @internal First-party host command envelope with a required instance ID. */ export const internalHostCommandEnvelopeWithInstanceIdSchema = z.extend( internalHostCommandEnvelopeSchema, { instanceId: instanceIdSchema, }, ); /** @internal Command envelope accepted by first-party widget hosts. */ export type InternalHostCommandEnvelope = z.output< typeof internalHostCommandEnvelopeSchema >; /** @internal First-party host command envelope with a required instance ID. */ export type InternalHostCommandEnvelopeWithInstanceId = z.output< typeof internalHostCommandEnvelopeWithInstanceIdSchema >; /** @internal Supplied-flow host envelope before host-only fields are removed. */ export type InternalHostInlineFlowCommandEnvelopeWithInstanceId = Omit< InternalHostCommandEnvelopeWithInstanceId, "command" > & { command: InternalHostInlineFlowCommandPayload; }; /** @internal Loader dispatch accepts Core-ready or host-projected commands. */ export type InternalHostDispatchEnvelopeWithInstanceId = | CoreCommandEnvelope | InternalHostInlineFlowCommandEnvelopeWithInstanceId; /** Remove browser-host projection data before dispatching to Core. */ export const toCoreInlineFlowCommandEnvelope = ( envelope: InternalHostInlineFlowCommandEnvelopeWithInstanceId, ): CoreCommandEnvelope => ({ ...envelope, command: toCoreInlineFlowCommandPayload(envelope.command), });