import * as z from "zod/mini"; import type { CoreCommandPayload } from "../core-commands.js"; import { responseMetadataInputSchema } from "../response-metadata.js"; import { publicCommandPayloadSchema } from "../widget-commands.js"; import { suppliedOnlyLocalInitOptionsSchema } from "./core-init-options.js"; import { inlineFlowSchema } from "./inline-flow.js"; const flowHandleIdSchema = z.string().check(z.trim(), z.minLength(1)); /** * @internal Unsupported credential-free host transport registration. * * `runtimeEndpoints` belongs to the host transport. Loader consumes it to * connect to Core and forwards only `opts` into Core's registration contract. * Package visibility is API incubation, not caller authorization; safety comes * from Core's restrictive supplied-only local registration profile. * See docs/specs/2026-08-02-flow-interaction-model.md. */ export const internalHostSuppliedOnlyLocalInitCommandPayloadSchema = z.strictObject({ kind: z.literal("init"), opts: suppliedOnlyLocalInitOptionsSchema, runtimeEndpoints: z.strictObject({ coreUrl: z.string().check(z.trim(), z.url()), }), }); /** @internal Deployment-owned local registration command. */ export type InternalHostSuppliedOnlyLocalInitCommandPayload = z.output< typeof internalHostSuppliedOnlyLocalInitCommandPayloadSchema >; /** * @internal Unsupported inline flow commands accepted by compatible hosts. * * Core owns initial handle allocation, so compatible hosts may omit * `flowHandleId` for the first supplied command. Only a handle-free open may * carry the host-local custom-mode marker; once a handle exists, callers use * the normal `setContainer` command. * See docs/specs/2026-08-02-flow-interaction-model.md. */ const inlineFlowOpenCommandFields = { kind: z.literal("open"), flow: inlineFlowSchema, metadata: z.optional(responseMetadataInputSchema), hideCloseButton: z.optional(z.boolean()), } as const; export const internalHostInlineFlowCommandPayloadSchema = z.union([ z.strictObject({ ...inlineFlowOpenCommandFields, container: z.optional(z.null()), }), z.strictObject({ ...inlineFlowOpenCommandFields, flowHandleId: flowHandleIdSchema, }), z.strictObject({ kind: z.literal("prefetch"), flow: inlineFlowSchema, flowHandleId: z.optional(flowHandleIdSchema), }), z.strictObject({ kind: z.literal("prerender"), flow: inlineFlowSchema, flowHandleId: z.optional(flowHandleIdSchema), hideCloseButton: z.optional(z.boolean()), }), ]); /** @internal Unsupported inline flow command accepted by compatible hosts. */ export type InternalHostInlineFlowCommandPayload = z.output< typeof internalHostInlineFlowCommandPayloadSchema >; /** Remove browser-host projection data before dispatching to Core. */ export const toCoreInlineFlowCommandPayload = ( command: InternalHostInlineFlowCommandPayload, ): CoreCommandPayload => { if (command.kind !== "open") { return command; } if ("container" in command) { const { container: _container, ...coreCommand } = command; return coreCommand; } return command; }; /** * @internal Command accepted by compatible widget hosts. Public commands retain * their documented contract; inline flow commands are an internal extension. */ export const internalHostCommandPayloadSchema = z.union([ publicCommandPayloadSchema, internalHostSuppliedOnlyLocalInitCommandPayloadSchema, internalHostInlineFlowCommandPayloadSchema, ]); /** @internal Command accepted by compatible widget hosts. */ export type InternalHostCommandPayload = z.output< typeof internalHostCommandPayloadSchema >;