@copilotkit/shared 2.0.0-rc.13 → 2.0.0-rc.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/a2ui-envelope.cjs.map +1 -1
  2. package/dist/a2ui-envelope.mjs.map +1 -1
  3. package/dist/generative-ui/child-refs.cjs +3 -3
  4. package/dist/generative-ui/child-refs.cjs.map +1 -1
  5. package/dist/generative-ui/child-refs.d.cts.map +1 -1
  6. package/dist/generative-ui/child-refs.d.mts.map +1 -1
  7. package/dist/generative-ui/child-refs.mjs +3 -3
  8. package/dist/generative-ui/child-refs.mjs.map +1 -1
  9. package/dist/generative-ui/schema-render.cjs +1 -1
  10. package/dist/generative-ui/schema-render.cjs.map +1 -1
  11. package/dist/generative-ui/schema-render.d.cts.map +1 -1
  12. package/dist/generative-ui/schema-render.d.mts.map +1 -1
  13. package/dist/generative-ui/schema-render.mjs +1 -1
  14. package/dist/generative-ui/schema-render.mjs.map +1 -1
  15. package/dist/generative-ui/validate-operation.cjs +2 -2
  16. package/dist/generative-ui/validate-operation.cjs.map +1 -1
  17. package/dist/generative-ui/validate-operation.mjs +2 -2
  18. package/dist/generative-ui/validate-operation.mjs.map +1 -1
  19. package/dist/index.umd.js +8 -8
  20. package/dist/index.umd.js.map +1 -1
  21. package/dist/package.cjs +1 -1
  22. package/dist/package.mjs +1 -1
  23. package/dist/telemetry/lambda-client.cjs +1 -1
  24. package/dist/telemetry/lambda-client.cjs.map +1 -1
  25. package/dist/telemetry/lambda-client.d.cts.map +1 -1
  26. package/dist/telemetry/lambda-client.d.mts.map +1 -1
  27. package/dist/telemetry/lambda-client.mjs +1 -1
  28. package/dist/telemetry/lambda-client.mjs.map +1 -1
  29. package/package.json +2 -2
  30. package/src/a2ui-envelope.ts +2 -1
  31. package/src/generative-ui/child-refs.ts +5 -4
  32. package/src/generative-ui/schema-render.ts +4 -1
  33. package/src/generative-ui/validate-operation.ts +2 -2
  34. package/src/telemetry/lambda-client.ts +5 -1
  35. package/tsconfig.json +3 -1
@@ -1 +1 @@
1
- {"version":3,"file":"a2ui-envelope.cjs","names":["isRecord","GENERATIVE_UI_MAX_TARGET_COMPONENTS","GENERATIVE_UI_MAX_SUMMARY_REJECTIONS","GENERATIVE_UI_MAX_REJECTION_LENGTH"],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n const catalogId = supportedCatalogIds[0];\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n };\n}\n"],"mappings":";;;;;;;;;;;;AAuBA,MAAa,iCAAiC;;AAG9C,MAAa,gCAAgC;AA8C7C,SAAS,iBAAiB,OAAgB;AACxC,QAAO,UAAU,UAAa,OAAO,UAAU;;AAGjD,SAAS,iBAAiB,OAAiC;AACzD,QAAO,OAAO,UAAU,YAAY,UAAU;;AAG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OAA+C;AACjE,QACEA,2BAAS,MAAM,IACf,eAAe,MAAM,QAAQ,IAC7BA,2BAAS,MAAM,SAAS,IACxB,aAAa,MAAM,SAAS,WAAW;;;;;;;;;;;;;;;;;;;;AAsB3C,SAAgB,yBAAyB,gBAAyB;AAChE,KAAI,CAACA,2BAAS,eAAe,CAAE,QAAO;CACtC,MAAM,YAAY,eAAe;AACjC,KAAI,CAACA,2BAAS,UAAU,CAAE,QAAO;CACjC,MAAM,WAAW,UAAU;AAC3B,QAAO,WAAW,SAAS,GAAG,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8B3C,SAAgB,mBAAmB,UAAgC;CACjE,MAAM,EAAE,qBAAqB,mBAAmB,SAAS;CACzD,MAAM,YAAY,oBAAoB;CACtC,MAAM,UAAU,gBAAgB,MAC7B,UAAU,MAAM,cAAc,UAChC;CACD,MAAM,aAAa,SAAS,SAAS;AASrC,QAAO;EAPL;EACA,YAAY;EACZ,YAAY,SAAS,cAAc,EAAE;EACrC,WAAW,SAAS,aAAa,EAAE;EACnC,SAAS,WAAW;EACpB,UAAU,WAAW;EAIrB,WAAW,WAAW;EACtB,aAAa,WAAW;EACxB,cAAc,SAAS;EACvB,eAAe,SAAS,eAAe;EACvC,GAAI,WAAW,sBAAsB,SACjC,EAAE,GACF,EAAE,QAAQ,WAAW,mBAAmB;EAC7C"}
1
+ {"version":3,"file":"a2ui-envelope.cjs","names":["isRecord","GENERATIVE_UI_MAX_TARGET_COMPONENTS","GENERATIVE_UI_MAX_SUMMARY_REJECTIONS","GENERATIVE_UI_MAX_REJECTION_LENGTH"],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n // `readCopilotKitUIEnvelope` only accepts a non-empty `supportedCatalogIds`.\n const catalogId = supportedCatalogIds[0]!;\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n };\n}\n"],"mappings":";;;;;;;;;;;;AAuBA,MAAa,iCAAiC;;AAG9C,MAAa,gCAAgC;AA8C7C,SAAS,iBAAiB,OAAgB;AACxC,QAAO,UAAU,UAAa,OAAO,UAAU;;AAGjD,SAAS,iBAAiB,OAAiC;AACzD,QAAO,OAAO,UAAU,YAAY,UAAU;;AAGhD,SAAS,WACP,OACA,SAC4B;AAC5B,QAAOA,2BAAS,MAAM,IAAI,OAAO,OAAO,MAAM,CAAC,MAAM,QAAQ;;;AAI/D,SAAS,cAAc,OAAuD;AAC5E,QACEA,2BAAS,MAAM,IACfA,2BAAS,MAAM,WAAW,IAC1B,iBAAiB,MAAM,YAAY;;AAIvC,SAAS,WAAW,OAAgD;AAClE,QAAO,UAAU,YAAY,UAAU;;;;;;AAOzC,SAAS,cAAc,OAAkD;AACvE,QACEA,2BAAS,MAAM,KACd,MAAM,eAAe,UAAaA,2BAAS,MAAM,WAAW,MAC5D,MAAM,aAAa,UACjB,MAAM,QA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,UAAgC;CACjE,MAAM,EAAE,qBAAqB,mBAAmB,SAAS;CAEzD,MAAM,YAAY,oBAAoB;CACtC,MAAM,UAAU,gBAAgB,MAC7B,UAAU,MAAM,cAAc,UAChC;CACD,MAAM,aAAa,SAAS,SAAS;AASrC,QAAO;EAPL;EACA,YAAY;EACZ,YAAY,SAAS,cAAc,EAAE;EACrC,WAAW,SAAS,aAAa,EAAE;EACnC,SAAS,WAAW;EACpB,UAAU,WAAW;EAIrB,WAAW,WAAW;EACtB,aAAa,WAAW;EACxB,cAAc,SAAS;EACvB,eAAe,SAAS,eAAe;EACvC,GAAI,WAAW,sBAAsB,SACjC,EAAE,GACF,EAAE,QAAQ,WAAW,mBAAmB;EAC7C"}
@@ -1 +1 @@
1
- {"version":3,"file":"a2ui-envelope.mjs","names":[],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n const catalogId = supportedCatalogIds[0];\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n };\n}\n"],"mappings":";;;;;;;;;;;;AAuBA,MAAa,iCAAiC;;AAG9C,MAAa,gCAAgC;AA8C7C,SAAS,iBAAiB,OAAgB;AACxC,QAAO,UAAU,UAAa,OAAO,UAAU;;AAGjD,SAAS,iBAAiB,OAAiC;AACzD,QAAO,OAAO,UAAU,YAAY,UAAU;;AAGhD,SAAS,WACP,OACA,SAC4B;AAC5B,QAAO,SAAS,MAAM,IAAI,OAAO,OAAO,MAAM,CAAC,MAAM,QAAQ;;;AAI/D,SAAS,cAAc,OAAuD;AAC5E,QACE,SAAS,MAAM,IACf,SAAS,MAAM,WAAW,IAC1B,iBAAiB,MAAM,YAAY;;AAIvC,SAAS,WAAW,OAAgD;AAClE,QAAO,UAAU,YAAY,UAAU;;;;;;AAOzC,SAAS,cAAc,OAAkD;AACvE,QACE,SAAS,MAAM,KACd,MAAM,eAAe,UAAa,SAAS,MAAM,WAAW,MAC5D,MAAM,aAAa,UACjB,MAAM,QAAQ,MAAM,SAAS,IAC5B,MAAM,SAAS,OAAO,SAAS,OAAO,SAAS,SAAS;;AAIhE,SAAS,oBACP,OACwC;AACxC,KACE,CAAC,SAAS,MAAM,IAChB,CAAC,cAAc,MAAM,MAAM,IAC3B,CAAC,iBAAiB,MAAM,YAAY,IACnC,MAAM,WAAW,UAAa,CAAC,WAAW,MAAM,QAAQ,cAAc,IACtE,MAAM,UAAU,UAAa,CAAC,WAAW,MAAM,OAAO,WAAW,CAElE,QAAO;CACT,MAAM,QAAQ,MAAM;AACpB,KAAI,UAAU,OAAW,QAAO;CAChC,MAAM,OAAO,IAAI,IAAI;EACnB,GAAG,OAAO,KAAK,MAAM,MAAM,cAAc,EAAE,CAAC;EAC5C,GAAG,OAAO,KAAK,MAAM,SAAS,EAAE,CAAC;EACjC,GAAG,OAAO,KAAK,MAAM,UAAU,EAAE,CAAC;EACnC,CAAC;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;;AAKvB,SAAS,aAAa,OAAqC;AACzD,QACE,SAAS,MAAM,IACf,MAAM,sBAAsB,kCAC3B,MAAM,yBAAyB,UAC9B,MAAM,yBAAyB,eACjC,MAAM,QAAQ,MAAM,mBAAmB,IACvC,MAAM,mBAAmB,MAAM,SAAS,IACxC,WAAW,MAAM,qBAAqB,eAAe,IACrD,OAAO,MAAM,2BAA2B;;AAI5C,SAAS,WAAW,OAA+C;AACjE,QACE,SAAS,MAAM,IACf,eAAe,MAAM,QAAQ,IAC7B,SAAS,MAAM,SAAS,IACxB,aAAa,MAAM,SAAS,WAAW;;;;;;;;;;;;;;;;;;;;AAsB3C,SAAgB,yBAAyB,gBAAyB;AAChE,KAAI,CAAC,SAAS,eAAe,CAAE,QAAO;CACtC,MAAM,YAAY,eAAe;AACjC,KAAI,CAAC,SAAS,UAAU,CAAE,QAAO;CACjC,MAAM,WAAW,UAAU;AAC3B,QAAO,WAAW,SAAS,GAAG,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8B3C,SAAgB,mBAAmB,UAAgC;CACjE,MAAM,EAAE,qBAAqB,mBAAmB,SAAS;CACzD,MAAM,YAAY,oBAAoB;CACtC,MAAM,UAAU,gBAAgB,MAC7B,UAAU,MAAM,cAAc,UAChC;CACD,MAAM,aAAa,SAAS,SAAS;AASrC,QAAO;EAPL;EACA,YAAY;EACZ,YAAY,SAAS,cAAc,EAAE;EACrC,WAAW,SAAS,aAAa,EAAE;EACnC,SAAS,WAAW;EACpB,UAAU,WAAW;EAIrB,WAAW,WAAW;EACtB,aAAa,WAAW;EACxB,cAAc,SAAS;EACvB,eAAe,SAAS,eAAe;EACvC,GAAI,WAAW,sBAAsB,SACjC,EAAE,GACF,EAAE,QAAQ,WAAW,mBAAmB;EAC7C"}
1
+ {"version":3,"file":"a2ui-envelope.mjs","names":[],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n // `readCopilotKitUIEnvelope` only accepts a non-empty `supportedCatalogIds`.\n const catalogId = supportedCatalogIds[0]!;\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n };\n}\n"],"mappings":";;;;;;;;;;;;AAuBA,MAAa,iCAAiC;;AAG9C,MAAa,gCAAgC;AA8C7C,SAAS,iBAAiB,OAAgB;AACxC,QAAO,UAAU,UAAa,OAAO,UAAU;;AAGjD,SAAS,iBAAiB,OAAiC;AACzD,QAAO,OAAO,UAAU,YAAY,UAAU;;AAGhD,SAAS,WACP,OACA,SAC4B;AAC5B,QAAO,SAAS,MAAM,IAAI,OAAO,OAAO,MAAM,CAAC,MAAM,QAAQ;;;AAI/D,SAAS,cAAc,OAAuD;AAC5E,QACE,SAAS,MAAM,IACf,SAAS,MAAM,WAAW,IAC1B,iBAAiB,MAAM,YAAY;;AAIvC,SAAS,WAAW,OAAgD;AAClE,QAAO,UAAU,YAAY,UA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M,WAAW,UAAU;AAC3B,QAAO,WAAW,SAAS,GAAG,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8B3C,SAAgB,mBAAmB,UAAgC;CACjE,MAAM,EAAE,qBAAqB,mBAAmB,SAAS;CAEzD,MAAM,YAAY,oBAAoB;CACtC,MAAM,UAAU,gBAAgB,MAC7B,UAAU,MAAM,cAAc,UAChC;CACD,MAAM,aAAa,SAAS,SAAS;AASrC,QAAO;EAPL;EACA,YAAY;EACZ,YAAY,SAAS,cAAc,EAAE;EACrC,WAAW,SAAS,aAAa,EAAE;EACnC,SAAS,WAAW;EACpB,UAAU,WAAW;EAIrB,WAAW,WAAW;EACtB,aAAa,WAAW;EACxB,cAAc,SAAS;EACvB,eAAe,SAAS,eAAe;EACvC,GAAI,WAAW,sBAAsB,SACjC,EAAE,GACF,EAAE,QAAQ,WAAW,mBAAmB;EAC7C"}
@@ -101,8 +101,8 @@ function childIdsAtRefs(component, refs) {
101
101
  else if (require_is_record.isRecord(value) && typeof value.componentId === "string") ids.push(value.componentId);
102
102
  };
103
103
  const visit = (value, path) => {
104
- if (path.length === 0) return collect(value);
105
104
  const [head, ...rest] = path;
105
+ if (head === void 0) return collect(value);
106
106
  if (head === "*") {
107
107
  if (Array.isArray(value)) value.forEach((item) => visit(item, rest));
108
108
  return;
@@ -121,8 +121,8 @@ function hasBoundChildRef(component, refs) {
121
121
  const isDynamic = (value) => require_is_record.isRecord(value) && ("@path" in value || "@call" in value);
122
122
  const visit = (value, path) => {
123
123
  if (isDynamic(value)) return true;
124
- if (path.length === 0) return false;
125
124
  const [head, ...rest] = path;
125
+ if (head === void 0) return false;
126
126
  if (head === "*") return Array.isArray(value) && value.some((item) => visit(item, rest));
127
127
  return require_is_record.isRecord(value) && Object.prototype.hasOwnProperty.call(value, head) && visit(value[head], rest);
128
128
  };
@@ -134,8 +134,8 @@ function hasBoundChildRef(component, refs) {
134
134
  * else keeps its identity. Missing keys are left alone.
135
135
  */
136
136
  function mapAtChildRef(value, path, replace) {
137
- if (path.length === 0) return replace(value);
138
137
  const [head, ...rest] = path;
138
+ if (head === void 0) return replace(value);
139
139
  if (head === "*") {
140
140
  if (!Array.isArray(value)) return value;
141
141
  return value.map((item) => mapAtChildRef(item, rest, replace));
@@ -1 +1 @@
1
- {"version":3,"file":"child-refs.cjs","names":["isRecord"],"sources":["../../src/generative-ui/child-refs.ts"],"sourcesContent":["import type { GenerativeUIJSONSchema } from \"./types\";\nimport { isRecord } from \"./is-record\";\n\n/**\n * How A2UI 1.0 marks a prop that holds a component reference: the JSON\n * Schema of the prop is a `$ref` to one of these two common types.\n * Validators and renderers find the children of a component by these refs,\n * at any depth of the props schema.\n */\nexport const A2UI_COMPONENT_ID_REF = \"common_types.json#/$defs/ComponentId\";\nexport const A2UI_CHILD_LIST_REF = \"common_types.json#/$defs/ChildList\";\n\n/**\n * The start of the `description` that marks a schema as a component\n * reference before it is turned into JSON Schema. `child()` and\n * `childList()` set it; a schema from another library can set a description\n * that starts with the same mark to get the same effect. What follows the\n * mark (after a space) stays as the description of the reference.\n */\nexport const A2UI_COMPONENT_ID_MARK = \"@a2ui/ComponentId\";\nexport const A2UI_CHILD_LIST_MARK = \"@a2ui/ChildList\";\n\nexport type ChildRefKind = \"single\" | \"multiple\";\n\n/** One component reference in a props schema. */\nexport interface ChildRef {\n /**\n * The keys from the props root to the reference. `\"*\"` stands for the\n * items of an array. `[\"tabs\", \"*\", \"child\"]` is the `child` of each\n * entry of `tabs`.\n */\n path: readonly string[];\n kind: ChildRefKind;\n}\n\n/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */\nexport function childRefKind(\n schema: GenerativeUIJSONSchema,\n): ChildRefKind | undefined {\n const ref = schema.$ref;\n if (ref === A2UI_COMPONENT_ID_REF) return \"single\";\n if (ref === A2UI_CHILD_LIST_REF) return \"multiple\";\n return undefined;\n}\n\n/**\n * Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of\n * the marked node. The mark is the `description` set by `child()` and\n * `childList()`; after this, the node is `{ \"$ref\": \"common_types.json#/...\" }`.\n */\nexport function applyChildRefMarks(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n const marked = (description: unknown, mark: string, ref: string) => {\n if (typeof description !== \"string\" || !description.startsWith(mark)) {\n return undefined;\n }\n const rest = description.slice(mark.length).trim();\n return rest === \"\" ? { $ref: ref } : { $ref: ref, description: rest };\n };\n const visit = (node: unknown): unknown => {\n if (Array.isArray(node)) return node.map(visit);\n if (!isRecord(node)) return node;\n const reference =\n marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ??\n marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);\n if (reference) return reference;\n return Object.fromEntries(\n Object.entries(node).map(([key, value]) => [key, visit(value)]),\n );\n };\n return visit(schema) as GenerativeUIJSONSchema;\n}\n\n/**\n * Every component reference in a props schema, with its path. Walks\n * `properties`, array `items`, and the variants of `anyOf` / `oneOf` /\n * `allOf`. A union counts as a reference only when every alternative is\n * one of the same kind: a prop that is text in one branch and a child in\n * another cannot be told apart from its value, so it stays data.\n */\nexport function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[] {\n const refs: ChildRef[] = [];\n const seen = new Set<string>();\n const add = (path: string[], kind: ChildRefKind) => {\n const key = path.join(\"\\u0000\");\n if (seen.has(key)) return;\n seen.add(key);\n refs.push({ path, kind });\n };\n const visit = (node: unknown, path: string[]) => {\n if (!isRecord(node)) return;\n const kind = childRefKind(node);\n if (kind) return add(path, kind);\n for (const list of [node.anyOf, node.oneOf]) {\n if (!Array.isArray(list) || list.length === 0) continue;\n const kinds = list.map((variant) =>\n isRecord(variant) ? childRefKind(variant) : undefined,\n );\n const [first] = kinds;\n if (first && kinds.every((k) => k === first)) add(path, first);\n else if (kinds.every((k) => k === undefined)) {\n // Objects in a union: a nested reference counts only when every\n // alternative has it at the same path with the same kind.\n const perVariant = list.map((variant) =>\n collectChildRefs(variant as GenerativeUIJSONSchema),\n );\n const key = (ref: ChildRef) =>\n `${ref.kind}\\u0000${ref.path.join(\"\\u0000\")}`;\n const shared = perVariant[0].filter((ref) =>\n perVariant.every((refs) => refs.some((r) => key(r) === key(ref))),\n );\n shared.forEach((ref) => add([...path, ...ref.path], ref.kind));\n }\n }\n if (Array.isArray(node.allOf)) {\n node.allOf.forEach((variant) => visit(variant, path));\n }\n if (isRecord(node.properties)) {\n for (const [key, value] of Object.entries(node.properties)) {\n visit(value, [...path, key]);\n }\n }\n if (isRecord(node.items)) visit(node.items, [...path, \"*\"]);\n };\n visit(schema, []);\n return refs;\n}\n\n/**\n * The component ids at the references of one component node: a string id,\n * a list of ids, or the `componentId` of a template, at every ref path.\n * Bound or computed values are not ids and are left out.\n */\nexport function childIdsAtRefs(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): string[] {\n const ids: string[] = [];\n const collect = (value: unknown) => {\n if (typeof value === \"string\") ids.push(value);\n else if (Array.isArray(value)) {\n value.forEach((id) => typeof id === \"string\" && ids.push(id));\n } else if (isRecord(value) && typeof value.componentId === \"string\") {\n ids.push(value.componentId);\n }\n };\n const visit = (value: unknown, path: readonly string[]) => {\n if (path.length === 0) return collect(value);\n const [head, ...rest] = path;\n if (head === \"*\") {\n if (Array.isArray(value)) value.forEach((item) => visit(item, rest));\n return;\n }\n if (isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) {\n visit(value[head], rest);\n }\n };\n for (const ref of refs) visit(component, ref.path);\n return ids;\n}\n\n/**\n * True when a reference of the component is bound or computed (`{\"@path\"}`,\n * `{\"@call\"}`) at the ref itself or above it, so its ids are known only\n * at render time.\n */\nexport function hasBoundChildRef(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): boolean {\n const isDynamic = (value: unknown) =>\n isRecord(value) && (\"@path\" in value || \"@call\" in value);\n const visit = (value: unknown, path: readonly string[]): boolean => {\n if (isDynamic(value)) return true;\n if (path.length === 0) return false;\n const [head, ...rest] = path;\n if (head === \"*\") {\n return Array.isArray(value) && value.some((item) => visit(item, rest));\n }\n return (\n isRecord(value) &&\n Object.prototype.hasOwnProperty.call(value, head) &&\n visit(value[head], rest)\n );\n };\n return refs.some((ref) => visit(component, ref.path));\n}\n\n/**\n * Walks `value` along a ref path and replaces what sits at the end with\n * `replace(found)`. Objects and arrays on the way are copied; everything\n * else keeps its identity. Missing keys are left alone.\n */\nexport function mapAtChildRef(\n value: unknown,\n path: readonly string[],\n replace: (found: unknown) => unknown,\n): unknown {\n if (path.length === 0) return replace(value);\n const [head, ...rest] = path;\n if (head === \"*\") {\n if (!Array.isArray(value)) return value;\n return value.map((item) => mapAtChildRef(item, rest, replace));\n }\n if (!isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) {\n return value;\n }\n return { ...value, [head]: mapAtChildRef(value[head], rest, replace) };\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,wBAAwB;AACrC,MAAa,sBAAsB;;;;;;;;AASnC,MAAa,yBAAyB;AACtC,MAAa,uBAAuB;;AAgBpC,SAAgB,aACd,QAC0B;CAC1B,MAAM,MAAM,OAAO;AACnB,KAAI,QAAQ,sBAAuB,QAAO;AAC1C,KAAI,QAAQ,oBAAqB,QAAO;;;;;;;AAS1C,SAAgB,mBACd,QACwB;CACxB,MAAM,UAAU,aAAsB,MAAc,QAAgB;AAClE,MAAI,OAAO,gBAAgB,YAAY,CAAC,YAAY,WAAW,KAAK,CAClE;EAEF,MAAM,OAAO,YAAY,MAAM,KAAK,OAAO,CAAC,MAAM;AAClD,SAAO,SAAS,KAAK,EAAE,MAAM,KAAK,GAAG;GAAE,MAAM;GAAK,aAAa;GAAM;;CAEvE,MAAM,SAAS,SAA2B;AACxC,MAAI,MAAM,QAAQ,KAAK,CAAE,QAAO,KAAK,IAAI,MAAM;AAC/C,MAAI,CAACA,2BAAS,KAAK,CAAE,QAAO;EAC5B,MAAM,YACJ,OAAO,KAAK,aAAa,wBAAwB,sBAAsB,IACvE,OAAO,KAAK,aAAa,sBAAsB,oB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,OAAgB,SAAqC;AAClE,MAAI,UAAU,MAAM,CAAE,QAAO;AAC7B,MAAI,KAAK,WAAW,EAAG,QAAO;EAC9B,MAAM,CAAC,MAAM,GAAG,QAAQ;AACxB,MAAI,SAAS,IACX,QAAO,MAAM,QAAQ,MAAM,IAAI,MAAM,MAAM,SAAS,MAAM,MAAM,KAAK,CAAC;AAExE,SACEA,2BAAS,MAAM,IACf,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,IACjD,MAAM,MAAM,OAAO,KAAK;;AAG5B,QAAO,KAAK,MAAM,QAAQ,MAAM,WAAW,IAAI,KAAK,CAAC;;;;;;;AAQvD,SAAgB,cACd,OACA,MACA,SACS;AACT,KAAI,KAAK,WAAW,EAAG,QAAO,QAAQ,MAAM;CAC5C,MAAM,CAAC,MAAM,GAAG,QAAQ;AACxB,KAAI,SAAS,KAAK;AAChB,MAAI,CAAC,MAAM,QAAQ,MAAM,CAAE,QAAO;AAClC,SAAO,MAAM,KAAK,SAAS,cAAc,MAAM,MAAM,QAAQ,CAAC;;AAEhE,KAAI,CAACA,2BAAS,MAAM,IAAI,CAAC,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,CACxE,QAAO;AAET,QAAO;EAAE,GAAG;GAAQ,OAAO,cAAc,MAAM,OAAO,MAAM,QAAQ;EAAE"}
1
+ {"version":3,"file":"child-refs.cjs","names":["isRecord"],"sources":["../../src/generative-ui/child-refs.ts"],"sourcesContent":["import type { GenerativeUIJSONSchema } from \"./types\";\nimport { isRecord } from \"./is-record\";\n\n/**\n * How A2UI 1.0 marks a prop that holds a component reference: the JSON\n * Schema of the prop is a `$ref` to one of these two common types.\n * Validators and renderers find the children of a component by these refs,\n * at any depth of the props schema.\n */\nexport const A2UI_COMPONENT_ID_REF = \"common_types.json#/$defs/ComponentId\";\nexport const A2UI_CHILD_LIST_REF = \"common_types.json#/$defs/ChildList\";\n\n/**\n * The start of the `description` that marks a schema as a component\n * reference before it is turned into JSON Schema. `child()` and\n * `childList()` set it; a schema from another library can set a description\n * that starts with the same mark to get the same effect. What follows the\n * mark (after a space) stays as the description of the reference.\n */\nexport const A2UI_COMPONENT_ID_MARK = \"@a2ui/ComponentId\";\nexport const A2UI_CHILD_LIST_MARK = \"@a2ui/ChildList\";\n\nexport type ChildRefKind = \"single\" | \"multiple\";\n\n/** One component reference in a props schema. */\nexport interface ChildRef {\n /**\n * The keys from the props root to the reference. `\"*\"` stands for the\n * items of an array. `[\"tabs\", \"*\", \"child\"]` is the `child` of each\n * entry of `tabs`.\n */\n path: readonly string[];\n kind: ChildRefKind;\n}\n\n/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */\nexport function childRefKind(\n schema: GenerativeUIJSONSchema,\n): ChildRefKind | undefined {\n const ref = schema.$ref;\n if (ref === A2UI_COMPONENT_ID_REF) return \"single\";\n if (ref === A2UI_CHILD_LIST_REF) return \"multiple\";\n return undefined;\n}\n\n/**\n * Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of\n * the marked node. The mark is the `description` set by `child()` and\n * `childList()`; after this, the node is `{ \"$ref\": \"common_types.json#/...\" }`.\n */\nexport function applyChildRefMarks(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n const marked = (description: unknown, mark: string, ref: string) => {\n if (typeof description !== \"string\" || !description.startsWith(mark)) {\n return undefined;\n }\n const rest = description.slice(mark.length).trim();\n return rest === \"\" ? { $ref: ref } : { $ref: ref, description: rest };\n };\n const visit = (node: unknown): unknown => {\n if (Array.isArray(node)) return node.map(visit);\n if (!isRecord(node)) return node;\n const reference =\n marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ??\n marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);\n if (reference) return reference;\n return Object.fromEntries(\n Object.entries(node).map(([key, value]) => [key, visit(value)]),\n );\n };\n return visit(schema) as GenerativeUIJSONSchema;\n}\n\n/**\n * Every component reference in a props schema, with its path. Walks\n * `properties`, array `items`, and the variants of `anyOf` / `oneOf` /\n * `allOf`. A union counts as a reference only when every alternative is\n * one of the same kind: a prop that is text in one branch and a child in\n * another cannot be told apart from its value, so it stays data.\n */\nexport function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[] {\n const refs: ChildRef[] = [];\n const seen = new Set<string>();\n const add = (path: string[], kind: ChildRefKind) => {\n const key = path.join(\"\\u0000\");\n if (seen.has(key)) return;\n seen.add(key);\n refs.push({ path, kind });\n };\n const visit = (node: unknown, path: string[]) => {\n if (!isRecord(node)) return;\n const kind = childRefKind(node);\n if (kind) return add(path, kind);\n for (const list of [node.anyOf, node.oneOf]) {\n if (!Array.isArray(list) || list.length === 0) continue;\n const kinds = list.map((variant) =>\n isRecord(variant) ? childRefKind(variant) : undefined,\n );\n const [first] = kinds;\n if (first && kinds.every((k) => k === first)) add(path, first);\n else if (kinds.every((k) => k === undefined)) {\n // Objects in a union: a nested reference counts only when every\n // alternative has it at the same path with the same kind.\n const perVariant = list.map((variant) =>\n collectChildRefs(variant as GenerativeUIJSONSchema),\n );\n const key = (ref: ChildRef) =>\n `${ref.kind}\\u0000${ref.path.join(\"\\u0000\")}`;\n // `list` is not empty (checked above), so `perVariant` is not empty.\n const shared = perVariant[0]!.filter((ref) =>\n perVariant.every((refs) => refs.some((r) => key(r) === key(ref))),\n );\n shared.forEach((ref) => add([...path, ...ref.path], ref.kind));\n }\n }\n if (Array.isArray(node.allOf)) {\n node.allOf.forEach((variant) => visit(variant, path));\n }\n if (isRecord(node.properties)) {\n for (const [key, value] of Object.entries(node.properties)) {\n visit(value, [...path, key]);\n }\n }\n if (isRecord(node.items)) visit(node.items, [...path, \"*\"]);\n };\n visit(schema, []);\n return refs;\n}\n\n/**\n * The component ids at the references of one component node: a string id,\n * a list of ids, or the `componentId` of a template, at every ref path.\n * Bound or computed values are not ids and are left out.\n */\nexport function childIdsAtRefs(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): string[] {\n const ids: string[] = [];\n const collect = (value: unknown) => {\n if (typeof value === \"string\") ids.push(value);\n else if (Array.isArray(value)) {\n value.forEach((id) => typeof id === \"string\" && ids.push(id));\n } else if (isRecord(value) && typeof value.componentId === \"string\") {\n ids.push(value.componentId);\n }\n };\n const visit = (value: unknown, path: readonly string[]) => {\n const [head, ...rest] = path;\n if (head === undefined) return collect(value);\n if (head === \"*\") {\n if (Array.isArray(value)) value.forEach((item) => visit(item, rest));\n return;\n }\n if (isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) {\n visit(value[head], rest);\n }\n };\n for (const ref of refs) visit(component, ref.path);\n return ids;\n}\n\n/**\n * True when a reference of the component is bound or computed (`{\"@path\"}`,\n * `{\"@call\"}`) at the ref itself or above it, so its ids are known only\n * at render time.\n */\nexport function hasBoundChildRef(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): boolean {\n const isDynamic = (value: unknown) =>\n isRecord(value) && (\"@path\" in value || \"@call\" in value);\n const visit = (value: unknown, path: readonly string[]): boolean => {\n if (isDynamic(value)) return true;\n const [head, ...rest] = path;\n if (head === undefined) return false;\n if (head === \"*\") {\n return Array.isArray(value) && value.some((item) => visit(item, rest));\n }\n return (\n isRecord(value) &&\n Object.prototype.hasOwnProperty.call(value, head) &&\n visit(value[head], rest)\n );\n };\n return refs.some((ref) => visit(component, ref.path));\n}\n\n/**\n * Walks `value` along a ref path and replaces what sits at the end with\n * `replace(found)`. Objects and arrays on the way are copied; everything\n * else keeps its identity. Missing keys are left alone.\n */\nexport function mapAtChildRef(\n value: unknown,\n path: readonly string[],\n replace: (found: unknown) => unknown,\n): unknown {\n const [head, ...rest] = path;\n if (head === undefined) return replace(value);\n if (head === \"*\") {\n if (!Array.isArray(value)) return value;\n return value.map((item) => mapAtChildRef(item, rest, replace));\n }\n if (!isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) {\n return value;\n }\n return { ...value, [head]: mapAtChildRef(value[head], rest, replace) };\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,wBAAwB;AACrC,MAAa,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AAO;AAClC,SAAO,MAAM,KAAK,SAAS,cAAc,MAAM,MAAM,QAAQ,CAAC;;AAEhE,KAAI,CAACA,2BAAS,MAAM,IAAI,CAAC,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,CACxE,QAAO;AAET,QAAO;EAAE,GAAG;GAAQ,OAAO,cAAc,MAAM,OAAO,MAAM,QAAQ;EAAE"}
@@ -1 +1 @@
1
- {"version":3,"file":"child-refs.d.cts","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"mappings":";;;;;AASA;;;;cAAa,qBAAA;AAAA,cACA,mBAAA;;;;;AASb;;;cAAa,sBAAA;AAAA,cACA,oBAAA;AAAA,KAED,YAAA;;UAGK,QAAA;EALgB;;AAEjC;;;EASE,IAAA;EACA,IAAA,EAAM,YAAA;AAAA;;iBAIQ,YAAA,CACd,MAAA,EAAQ,sBAAA,GACP,YAAA;;;;;;iBAYa,kBAAA,CACd,MAAA,EAAQ,sBAAA,GACP,sBAAA;AAhBH;;;;;;;AAAA,iBA6CgB,gBAAA,CAAiB,MAAA,EAAQ,sBAAA,GAAyB,QAAA;;AA/BlE;;;;iBAoFgB,cAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;AAvDjB;iBAsFgB,gBAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;;iBAyBD,aAAA,CACd,KAAA,WACA,IAAA,qBACA,OAAA,GAAU,KAAA"}
1
+ {"version":3,"file":"child-refs.d.cts","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"mappings":";;;;;AASA;;;;cAAa,qBAAA;AAAA,cACA,mBAAA;;;;;AASb;;;cAAa,sBAAA;AAAA,cACA,oBAAA;AAAA,KAED,YAAA;;UAGK,QAAA;EALgB;;AAEjC;;;EASE,IAAA;EACA,IAAA,EAAM,YAAA;AAAA;;iBAIQ,YAAA,CACd,MAAA,EAAQ,sBAAA,GACP,YAAA;;;;;;iBAYa,kBAAA,CACd,MAAA,EAAQ,sBAAA,GACP,sBAAA;AAhBH;;;;;;;AAAA,iBA6CgB,gBAAA,CAAiB,MAAA,EAAQ,sBAAA,GAAyB,QAAA;;AA/BlE;;;;iBAqFgB,cAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;AAxDjB;iBAuFgB,gBAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;;iBAyBD,aAAA,CACd,KAAA,WACA,IAAA,qBACA,OAAA,GAAU,KAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"child-refs.d.mts","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"mappings":";;;;;AASA;;;;cAAa,qBAAA;AAAA,cACA,mBAAA;;;;;AASb;;;cAAa,sBAAA;AAAA,cACA,oBAAA;AAAA,KAED,YAAA;;UAGK,QAAA;EALgB;;AAEjC;;;EASE,IAAA;EACA,IAAA,EAAM,YAAA;AAAA;;iBAIQ,YAAA,CACd,MAAA,EAAQ,sBAAA,GACP,YAAA;;;;;;iBAYa,kBAAA,CACd,MAAA,EAAQ,sBAAA,GACP,sBAAA;AAhBH;;;;;;;AAAA,iBA6CgB,gBAAA,CAAiB,MAAA,EAAQ,sBAAA,GAAyB,QAAA;;AA/BlE;;;;iBAoFgB,cAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;AAvDjB;iBAsFgB,gBAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;;iBAyBD,aAAA,CACd,KAAA,WACA,IAAA,qBACA,OAAA,GAAU,KAAA"}
1
+ {"version":3,"file":"child-refs.d.mts","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"mappings":";;;;;AASA;;;;cAAa,qBAAA;AAAA,cACA,mBAAA;;;;;AASb;;;cAAa,sBAAA;AAAA,cACA,oBAAA;AAAA,KAED,YAAA;;UAGK,QAAA;EALgB;;AAEjC;;;EASE,IAAA;EACA,IAAA,EAAM,YAAA;AAAA;;iBAIQ,YAAA,CACd,MAAA,EAAQ,sBAAA,GACP,YAAA;;;;;;iBAYa,kBAAA,CACd,MAAA,EAAQ,sBAAA,GACP,sBAAA;AAhBH;;;;;;;AAAA,iBA6CgB,gBAAA,CAAiB,MAAA,EAAQ,sBAAA,GAAyB,QAAA;;AA/BlE;;;;iBAqFgB,cAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;AAxDjB;iBAuFgB,gBAAA,CACd,SAAA,EAAW,MAAA,mBACX,IAAA,WAAe,QAAA;;;;;;iBAyBD,aAAA,CACd,KAAA,WACA,IAAA,qBACA,OAAA,GAAU,KAAA"}
@@ -101,8 +101,8 @@ function childIdsAtRefs(component, refs) {
101
101
  else if (isRecord(value) && typeof value.componentId === "string") ids.push(value.componentId);
102
102
  };
103
103
  const visit = (value, path) => {
104
- if (path.length === 0) return collect(value);
105
104
  const [head, ...rest] = path;
105
+ if (head === void 0) return collect(value);
106
106
  if (head === "*") {
107
107
  if (Array.isArray(value)) value.forEach((item) => visit(item, rest));
108
108
  return;
@@ -121,8 +121,8 @@ function hasBoundChildRef(component, refs) {
121
121
  const isDynamic = (value) => isRecord(value) && ("@path" in value || "@call" in value);
122
122
  const visit = (value, path) => {
123
123
  if (isDynamic(value)) return true;
124
- if (path.length === 0) return false;
125
124
  const [head, ...rest] = path;
125
+ if (head === void 0) return false;
126
126
  if (head === "*") return Array.isArray(value) && value.some((item) => visit(item, rest));
127
127
  return isRecord(value) && Object.prototype.hasOwnProperty.call(value, head) && visit(value[head], rest);
128
128
  };
@@ -134,8 +134,8 @@ function hasBoundChildRef(component, refs) {
134
134
  * else keeps its identity. Missing keys are left alone.
135
135
  */
136
136
  function mapAtChildRef(value, path, replace) {
137
- if (path.length === 0) return replace(value);
138
137
  const [head, ...rest] = path;
138
+ if (head === void 0) return replace(value);
139
139
  if (head === "*") {
140
140
  if (!Array.isArray(value)) return value;
141
141
  return value.map((item) => mapAtChildRef(item, rest, replace));
@@ -1 +1 @@
1
- {"version":3,"file":"child-refs.mjs","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"sourcesContent":["import type { GenerativeUIJSONSchema } from \"./types\";\nimport { isRecord } from \"./is-record\";\n\n/**\n * How A2UI 1.0 marks a prop that holds a component reference: the JSON\n * Schema of the prop is a `$ref` to one of these two common types.\n * Validators and renderers find the children of a component by these refs,\n * at any depth of the props schema.\n */\nexport const A2UI_COMPONENT_ID_REF = \"common_types.json#/$defs/ComponentId\";\nexport const A2UI_CHILD_LIST_REF = \"common_types.json#/$defs/ChildList\";\n\n/**\n * The start of the `description` that marks a schema as a component\n * reference before it is turned into JSON Schema. `child()` and\n * `childList()` set it; a schema from another library can set a description\n * that starts with the same mark to get the same effect. What follows the\n * mark (after a space) stays as the description of the reference.\n */\nexport const A2UI_COMPONENT_ID_MARK = \"@a2ui/ComponentId\";\nexport const A2UI_CHILD_LIST_MARK = \"@a2ui/ChildList\";\n\nexport type ChildRefKind = \"single\" | \"multiple\";\n\n/** One component reference in a props schema. */\nexport interface ChildRef {\n /**\n * The keys from the props root to the reference. `\"*\"` stands for the\n * items of an array. `[\"tabs\", \"*\", \"child\"]` is the `child` of each\n * entry of `tabs`.\n */\n path: readonly string[];\n kind: ChildRefKind;\n}\n\n/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */\nexport function childRefKind(\n schema: GenerativeUIJSONSchema,\n): ChildRefKind | undefined {\n const ref = schema.$ref;\n if (ref === A2UI_COMPONENT_ID_REF) return \"single\";\n if (ref === A2UI_CHILD_LIST_REF) return \"multiple\";\n return undefined;\n}\n\n/**\n * Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of\n * the marked node. The mark is the `description` set by `child()` and\n * `childList()`; after this, the node is `{ \"$ref\": \"common_types.json#/...\" }`.\n */\nexport function applyChildRefMarks(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n const marked = (description: unknown, mark: string, ref: string) => {\n if (typeof description !== \"string\" || !description.startsWith(mark)) {\n return undefined;\n }\n const rest = description.slice(mark.length).trim();\n return rest === \"\" ? { $ref: ref } : { $ref: ref, description: rest };\n };\n const visit = (node: unknown): unknown => {\n if (Array.isArray(node)) return node.map(visit);\n if (!isRecord(node)) return node;\n const reference =\n marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ??\n marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);\n if (reference) return reference;\n return Object.fromEntries(\n Object.entries(node).map(([key, value]) => [key, visit(value)]),\n );\n };\n return visit(schema) as GenerativeUIJSONSchema;\n}\n\n/**\n * Every component reference in a props schema, with its path. Walks\n * `properties`, array `items`, and the variants of `anyOf` / `oneOf` /\n * `allOf`. A union counts as a reference only when every alternative is\n * one of the same kind: a prop that is text in one branch and a child in\n * another cannot be told apart from its value, so it stays data.\n */\nexport function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[] {\n const refs: ChildRef[] = [];\n const seen = new Set<string>();\n const add = (path: string[], kind: ChildRefKind) => {\n const key = path.join(\"\\u0000\");\n if (seen.has(key)) return;\n seen.add(key);\n refs.push({ path, kind });\n };\n const visit = (node: unknown, path: string[]) => {\n if (!isRecord(node)) return;\n const kind = childRefKind(node);\n if (kind) return add(path, kind);\n for (const list of [node.anyOf, node.oneOf]) {\n if (!Array.isArray(list) || list.length === 0) continue;\n const kinds = list.map((variant) =>\n isRecord(variant) ? childRefKind(variant) : undefined,\n );\n const [first] = kinds;\n if (first && kinds.every((k) => k === first)) add(path, first);\n else if (kinds.every((k) => k === undefined)) {\n // Objects in a union: a nested reference counts only when every\n // alternative has it at the same path with the same kind.\n const perVariant = list.map((variant) =>\n collectChildRefs(variant as GenerativeUIJSONSchema),\n );\n const key = (ref: ChildRef) =>\n `${ref.kind}\\u0000${ref.path.join(\"\\u0000\")}`;\n const shared = perVariant[0].filter((ref) =>\n perVariant.every((refs) => refs.some((r) => key(r) === key(ref))),\n );\n shared.forEach((ref) => add([...path, ...ref.path], ref.kind));\n }\n }\n if (Array.isArray(node.allOf)) {\n node.allOf.forEach((variant) => visit(variant, path));\n }\n if (isRecord(node.properties)) {\n for (const [key, value] of Object.entries(node.properties)) {\n visit(value, [...path, key]);\n }\n }\n if (isRecord(node.items)) visit(node.items, [...path, \"*\"]);\n };\n visit(schema, []);\n return refs;\n}\n\n/**\n * The component ids at the references of one component node: a string id,\n * a list of ids, or the `componentId` of a template, at every ref path.\n * Bound or computed values are not ids and are left out.\n */\nexport function childIdsAtRefs(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): string[] {\n const ids: string[] = [];\n const collect = (value: unknown) => {\n if (typeof value === \"string\") ids.push(value);\n else if (Array.isArray(value)) {\n value.forEach((id) => typeof id === \"string\" && ids.push(id));\n } else if (isRecord(value) && typeof value.componentId === \"string\") {\n ids.push(value.componentId);\n }\n };\n const visit = (value: unknown, path: readonly string[]) => {\n if (path.length === 0) return collect(value);\n const [head, ...rest] = path;\n if (head === \"*\") {\n if (Array.isArray(value)) value.forEach((item) => visit(item, rest));\n return;\n }\n if (isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) {\n visit(value[head], rest);\n }\n };\n for (const ref of refs) visit(component, ref.path);\n return ids;\n}\n\n/**\n * True when a reference of the component is bound or computed (`{\"@path\"}`,\n * `{\"@call\"}`) at the ref itself or above it, so its ids are known only\n * at render time.\n */\nexport function hasBoundChildRef(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): boolean {\n const isDynamic = (value: unknown) =>\n isRecord(value) && (\"@path\" in value || \"@call\" in value);\n const visit = (value: unknown, path: readonly string[]): boolean => {\n if (isDynamic(value)) return true;\n if (path.length === 0) return false;\n const [head, ...rest] = path;\n if (head === \"*\") {\n return Array.isArray(value) && value.some((item) => visit(item, rest));\n }\n return (\n isRecord(value) &&\n Object.prototype.hasOwnProperty.call(value, head) &&\n visit(value[head], rest)\n );\n };\n return refs.some((ref) => visit(component, ref.path));\n}\n\n/**\n * Walks `value` along a ref path and replaces what sits at the end with\n * `replace(found)`. Objects and arrays on the way are copied; everything\n * else keeps its identity. Missing keys are left alone.\n */\nexport function mapAtChildRef(\n value: unknown,\n path: readonly string[],\n replace: (found: unknown) => unknown,\n): unknown {\n if (path.length === 0) return replace(value);\n const [head, ...rest] = path;\n if (head === \"*\") {\n if (!Array.isArray(value)) return value;\n return value.map((item) => mapAtChildRef(item, rest, replace));\n }\n if (!isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) {\n return value;\n }\n return { ...value, [head]: mapAtChildRef(value[head], rest, replace) };\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,wBAAwB;AACrC,MAAa,sBAAsB;;;;;;;;AASnC,MAAa,yBAAyB;AACtC,MAAa,uBAAuB;;AAgBpC,SAAgB,aACd,QAC0B;CAC1B,MAAM,MAAM,OAAO;AACnB,KAAI,QAAQ,sBAAuB,QAAO;AAC1C,KAAI,QAAQ,oBAAqB,QAAO;;;;;;;AAS1C,SAAgB,mBACd,QACwB;CACxB,MAAM,UAAU,aAAsB,MAAc,QAAgB;AAClE,MAAI,OAAO,gBAAgB,YAAY,CAAC,YAAY,WAAW,KAAK,CAClE;EAEF,MAAM,OAAO,YAAY,MAAM,KAAK,OAAO,CAAC,MAAM;AAClD,SAAO,SAAS,KAAK,EAAE,MAAM,KAAK,GAAG;GAAE,MAAM;GAAK,aAAa;GAAM;;CAEvE,MAAM,SAAS,SAA2B;AACxC,MAAI,MAAM,QAAQ,KAAK,CAAE,QAAO,KAAK,IAAI,MAAM;AAC/C,MAAI,CAAC,SAAS,KAAK,CAAE,QAAO;EAC5B,MAAM,YACJ,OAAO,KAAK,aAAa,wBAAwB,sBAAsB,IACvE,OAAO,KAAK,aAAa,sBAAsB,oBAAoB;AACrE,MAAI,UAAW,QAAO;AACtB,SAAO,OAAO,YACZ,OAAO,QAAQ,KAAK,CAAC,KAAK,CAAC,KAAK,WAAW,CAAC,KAAK,MAAM,MAAM,CAAC,CAAC,CAChE;;AAEH,QAAO,MAAM,OAAO;;;;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AM,SAAS,MAAM,MAAM,KAAK,CAAC;AAExE,SACE,SAAS,MAAM,IACf,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,IACjD,MAAM,MAAM,OAAO,KAAK;;AAG5B,QAAO,KAAK,MAAM,QAAQ,MAAM,WAAW,IAAI,KAAK,CAAC;;;;;;;AAQvD,SAAgB,cACd,OACA,MACA,SACS;AACT,KAAI,KAAK,WAAW,EAAG,QAAO,QAAQ,MAAM;CAC5C,MAAM,CAAC,MAAM,GAAG,QAAQ;AACxB,KAAI,SAAS,KAAK;AAChB,MAAI,CAAC,MAAM,QAAQ,MAAM,CAAE,QAAO;AAClC,SAAO,MAAM,KAAK,SAAS,cAAc,MAAM,MAAM,QAAQ,CAAC;;AAEhE,KAAI,CAAC,SAAS,MAAM,IAAI,CAAC,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,CACxE,QAAO;AAET,QAAO;EAAE,GAAG;GAAQ,OAAO,cAAc,MAAM,OAAO,MAAM,QAAQ;EAAE"}
1
+ {"version":3,"file":"child-refs.mjs","names":[],"sources":["../../src/generative-ui/child-refs.ts"],"sourcesContent":["import type { GenerativeUIJSONSchema } from \"./types\";\nimport { isRecord } from \"./is-record\";\n\n/**\n * How A2UI 1.0 marks a prop that holds a component reference: the JSON\n * Schema of the prop is a `$ref` to one of these two common types.\n * Validators and renderers find the children of a component by these refs,\n * at any depth of the props schema.\n */\nexport const A2UI_COMPONENT_ID_REF = \"common_types.json#/$defs/ComponentId\";\nexport const A2UI_CHILD_LIST_REF = \"common_types.json#/$defs/ChildList\";\n\n/**\n * The start of the `description` that marks a schema as a component\n * reference before it is turned into JSON Schema. `child()` and\n * `childList()` set it; a schema from another library can set a description\n * that starts with the same mark to get the same effect. What follows the\n * mark (after a space) stays as the description of the reference.\n */\nexport const A2UI_COMPONENT_ID_MARK = \"@a2ui/ComponentId\";\nexport const A2UI_CHILD_LIST_MARK = \"@a2ui/ChildList\";\n\nexport type ChildRefKind = \"single\" | \"multiple\";\n\n/** One component reference in a props schema. */\nexport interface ChildRef {\n /**\n * The keys from the props root to the reference. `\"*\"` stands for the\n * items of an array. `[\"tabs\", \"*\", \"child\"]` is the `child` of each\n * entry of `tabs`.\n */\n path: readonly string[];\n kind: ChildRefKind;\n}\n\n/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */\nexport function childRefKind(\n schema: GenerativeUIJSONSchema,\n): ChildRefKind | undefined {\n const ref = schema.$ref;\n if (ref === A2UI_COMPONENT_ID_REF) return \"single\";\n if (ref === A2UI_CHILD_LIST_REF) return \"multiple\";\n return undefined;\n}\n\n/**\n * Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of\n * the marked node. The mark is the `description` set by `child()` and\n * `childList()`; after this, the node is `{ \"$ref\": \"common_types.json#/...\" }`.\n */\nexport function applyChildRefMarks(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n const marked = (description: unknown, mark: string, ref: string) => {\n if (typeof description !== \"string\" || !description.startsWith(mark)) {\n return undefined;\n }\n const rest = description.slice(mark.length).trim();\n return rest === \"\" ? { $ref: ref } : { $ref: ref, description: rest };\n };\n const visit = (node: unknown): unknown => {\n if (Array.isArray(node)) return node.map(visit);\n if (!isRecord(node)) return node;\n const reference =\n marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ??\n marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);\n if (reference) return reference;\n return Object.fromEntries(\n Object.entries(node).map(([key, value]) => [key, visit(value)]),\n );\n };\n return visit(schema) as GenerativeUIJSONSchema;\n}\n\n/**\n * Every component reference in a props schema, with its path. Walks\n * `properties`, array `items`, and the variants of `anyOf` / `oneOf` /\n * `allOf`. A union counts as a reference only when every alternative is\n * one of the same kind: a prop that is text in one branch and a child in\n * another cannot be told apart from its value, so it stays data.\n */\nexport function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[] {\n const refs: ChildRef[] = [];\n const seen = new Set<string>();\n const add = (path: string[], kind: ChildRefKind) => {\n const key = path.join(\"\\u0000\");\n if (seen.has(key)) return;\n seen.add(key);\n refs.push({ path, kind });\n };\n const visit = (node: unknown, path: string[]) => {\n if (!isRecord(node)) return;\n const kind = childRefKind(node);\n if (kind) return add(path, kind);\n for (const list of [node.anyOf, node.oneOf]) {\n if (!Array.isArray(list) || list.length === 0) continue;\n const kinds = list.map((variant) =>\n isRecord(variant) ? childRefKind(variant) : undefined,\n );\n const [first] = kinds;\n if (first && kinds.every((k) => k === first)) add(path, first);\n else if (kinds.every((k) => k === undefined)) {\n // Objects in a union: a nested reference counts only when every\n // alternative has it at the same path with the same kind.\n const perVariant = list.map((variant) =>\n collectChildRefs(variant as GenerativeUIJSONSchema),\n );\n const key = (ref: ChildRef) =>\n `${ref.kind}\\u0000${ref.path.join(\"\\u0000\")}`;\n // `list` is not empty (checked above), so `perVariant` is not empty.\n const shared = perVariant[0]!.filter((ref) =>\n perVariant.every((refs) => refs.some((r) => key(r) === key(ref))),\n );\n shared.forEach((ref) => add([...path, ...ref.path], ref.kind));\n }\n }\n if (Array.isArray(node.allOf)) {\n node.allOf.forEach((variant) => visit(variant, path));\n }\n if (isRecord(node.properties)) {\n for (const [key, value] of Object.entries(node.properties)) {\n visit(value, [...path, key]);\n }\n }\n if (isRecord(node.items)) visit(node.items, [...path, \"*\"]);\n };\n visit(schema, []);\n return refs;\n}\n\n/**\n * The component ids at the references of one component node: a string id,\n * a list of ids, or the `componentId` of a template, at every ref path.\n * Bound or computed values are not ids and are left out.\n */\nexport function childIdsAtRefs(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): string[] {\n const ids: string[] = [];\n const collect = (value: unknown) => {\n if (typeof value === \"string\") ids.push(value);\n else if (Array.isArray(value)) {\n value.forEach((id) => typeof id === \"string\" && ids.push(id));\n } else if (isRecord(value) && typeof value.componentId === \"string\") {\n ids.push(value.componentId);\n }\n };\n const visit = (value: unknown, path: readonly string[]) => {\n const [head, ...rest] = path;\n if (head === undefined) return collect(value);\n if (head === \"*\") {\n if (Array.isArray(value)) value.forEach((item) => visit(item, rest));\n return;\n }\n if (isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) {\n visit(value[head], rest);\n }\n };\n for (const ref of refs) visit(component, ref.path);\n return ids;\n}\n\n/**\n * True when a reference of the component is bound or computed (`{\"@path\"}`,\n * `{\"@call\"}`) at the ref itself or above it, so its ids are known only\n * at render time.\n */\nexport function hasBoundChildRef(\n component: Record<string, unknown>,\n refs: readonly ChildRef[],\n): boolean {\n const isDynamic = (value: unknown) =>\n isRecord(value) && (\"@path\" in value || \"@call\" in value);\n const visit = (value: unknown, path: readonly string[]): boolean => {\n if (isDynamic(value)) return true;\n const [head, ...rest] = path;\n if (head === undefined) return false;\n if (head === \"*\") {\n return Array.isArray(value) && value.some((item) => visit(item, rest));\n }\n return (\n isRecord(value) &&\n Object.prototype.hasOwnProperty.call(value, head) &&\n visit(value[head], rest)\n );\n };\n return refs.some((ref) => visit(component, ref.path));\n}\n\n/**\n * Walks `value` along a ref path and replaces what sits at the end with\n * `replace(found)`. Objects and arrays on the way are copied; everything\n * else keeps its identity. Missing keys are left alone.\n */\nexport function mapAtChildRef(\n value: unknown,\n path: readonly string[],\n replace: (found: unknown) => unknown,\n): unknown {\n const [head, ...rest] = path;\n if (head === undefined) return replace(value);\n if (head === \"*\") {\n if (!Array.isArray(value)) return value;\n return value.map((item) => mapAtChildRef(item, rest, replace));\n }\n if (!isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) {\n return value;\n }\n return { ...value, [head]: mapAtChildRef(value[head], rest, replace) };\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,wBAAwB;AACrC,MAAa,sBAAsB;;;;;;;;AASnC,MAAa,yBAAyB;AACtC,MAAa,uBAAuB;;AAgBpC,SAAgB,aACd,QAC0B;CAC1B,MAAM,MAAM,OAAO;AACnB,KAAI,QAAQ,sBAAuB,QAAO;AAC1C,KAAI,QAAQ,oBAAqB,QAAO;;;;;;;AAS1C,SAAg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QAAQ;EAAE"}
@@ -194,7 +194,7 @@ function applyDescriptionOverrides(manifest, overrides) {
194
194
  const unknown = [];
195
195
  for (const [key, description] of Object.entries(overrides)) {
196
196
  const [name, prop, ...rest] = key.split(".");
197
- const component = hasOwn(components, name) ? components[name] : void 0;
197
+ const component = name !== void 0 && hasOwn(components, name) ? components[name] : void 0;
198
198
  if (!component || rest.length > 0) {
199
199
  unknown.push(key);
200
200
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"schema-render.cjs","names":["compactType","compactProps","functionCallers","GENERATIVE_UI_AGENT_FUNCTION_CALLS","buildCompactGenerativeUISchema"],"sources":["../../src/generative-ui/schema-render.ts"],"sourcesContent":["import { functionCallers } from \"./basic-functions\";\nimport {\n buildCompactGenerativeUISchema,\n compactProps,\n compactType,\n} from \"./compact-schema\";\nimport type { GenerativeUISchemaSettings } from \"./prompt-spec\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type {\n GenerativeUIComponentManifest,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\n\ntype SchemaManifest = Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n>;\n\n/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */\nconst hasOwn = (target: object, key: string) =>\n Object.prototype.hasOwnProperty.call(target, key);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The props schema without the description of each prop. */\nfunction withoutPropDescriptions(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n if (!isRecord(schema.properties)) return schema;\n return {\n ...schema,\n properties: Object.fromEntries(\n Object.entries(schema.properties).map(([key, value]) => {\n if (!isRecord(value)) return [key, value];\n const { description: _description, ...rest } = value;\n return [key, rest];\n }),\n ),\n };\n}\n\n/** The short signature of a function: its built-in signature, or its schema made short. */\nfunction functionSignature(fn: GenerativeUIManifest[\"functions\"][string]) {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n return hasSignature ? signature : compactType(fn.parameters, 0);\n}\n\n/**\n * A function name with its parameters: `pick{ id: string }`, `clearCart{}`\n * for no arguments, `setVolume(number)` for a parameter that is not an object.\n */\nfunction nameWithParameters(name: string, parameters: string) {\n if (parameters.startsWith(\"{\")) return `${name}${parameters}`;\n if (parameters === \"object\") return `${name}{}`;\n return `${name}(${parameters})`;\n}\n\n/**\n * One component as a TypeScript-like signature line, with its events and\n * slots on the lines below:\n * `- Text(text?: string // The text): Shows text`.\n */\nfunction signatureLines(\n name: string,\n component: GenerativeUIComponentManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const props = Object.entries(\n compactProps(component.props, settings.propDescriptions),\n ).map(([key, type]) => `${key}: ${type}`);\n const lines = [\n `- ${name}(${props.join(\", \")})${component.description ? `: ${oneLine(component.description)}` : \"\"}`,\n ];\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) =>\n `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`,\n );\n if (events.length > 0) lines.push(` events: ${events.join(\", \")}`);\n const slots = Object.entries(component.slots ?? {}).map(\n ([slot, kind]) => `${slot} (${kind})`,\n );\n if (slots.length > 0) lines.push(` slots: ${slots.join(\", \")}`);\n return lines;\n}\n\n/** The whole schema as signature text: targets, functions, components, surfaces. */\nfunction signatureSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const targets = manifest.targets.map((t) => {\n const limit = t.components ? `; only ${t.components.join(\", \")}` : \"\";\n return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n });\n const functions = Object.entries(manifest.functions).map(([name, fn]) => {\n const callers = functionCallers(fn);\n const who =\n callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? callers.renderer\n ? \" (UI or agent)\"\n : \" (agent only)\"\n : \"\";\n return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`;\n });\n const components = Object.entries(manifest.components).flatMap(\n ([name, component]) => signatureLines(name, component, settings),\n );\n const surfaces = Object.entries(manifest.surfaces).map(\n ([id, s]) =>\n `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : \"\"}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : \"\"}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join(\"\")}`,\n );\n return [\n `Catalog: ${manifest.catalogId}`,\n `Targets:\\n${targets.join(\"\\n\") || \"- none\"}`,\n `Functions:\\n${functions.join(\"\\n\") || \"- none\"}`,\n `Components:\\n${components.join(\"\\n\") || \"- none\"}`,\n `Surfaces:\\n${surfaces.join(\"\\n\") || \"- none\"}`,\n ].join(\"\\n\\n\");\n}\n\n/** A description on one line, so its lines cannot read as new entries. */\nfunction oneLine(text: string) {\n return text.replace(/\\s*\\n\\s*/g, \" \");\n}\n\n/** Values that would read as a type or a number once their quotes are gone. */\nconst AMBIGUOUS_VALUE =\n /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\\d[\\d.]*)$/;\n\n/**\n * A compact type with bare enum values and tight unions:\n * `\"sm\" | \"md\" | \"lg\"` becomes `sm|md|lg`. A union keeps its quotes when it\n * also holds a type (`\"sm\" | number`) or a value that reads as a type or a\n * number (`\"number\" | \"currency\"`, `\"1\" | \"2\"`), and so do values that are\n * not plain words.\n */\nfunction bareType(type: string) {\n return type\n .replace(\n /\"[A-Za-z0-9_#%.-]+\"(?: \\| \"[A-Za-z0-9_#%.-]+\")*(?! \\|)/g,\n (union: string, offset: number, whole: string) => {\n if (whole.slice(offset - 2, offset) === \"| \") return union;\n const values = union.split(\" | \").map((v) => v.slice(1, -1));\n if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;\n return values.join(\"|\");\n },\n )\n .replace(/ \\| /g, \"|\");\n}\n\n/** A compact prop (`type // description`) as `name: type - description`. */\nfunction propLine(key: string, value: string) {\n const name = key.endsWith(\"?\") ? key.slice(0, -1) : `${key}*`;\n const cut = value.indexOf(\" // \");\n const type = bareType(cut < 0 ? value : value.slice(0, cut));\n const text = cut < 0 ? \"\" : value.slice(cut + 4).trim();\n const description = text ? ` - ${oneLine(text)}` : \"\";\n return ` ${name}: ${type}${description}`;\n}\n\n/**\n * The whole schema as plain lines: a header line per component and one\n * indented line per prop: events are `action` props and slots `componentId`\n * or `childList` props. Props are optional unless marked `*`, enum values\n * are bare, and the common props are listed once. It carries the same names, types and descriptions as\n * `compact`, in fewer tokens.\n */\nfunction linesSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const compact = buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n });\n const targets = compact.targets\n .map((t) => {\n const limit = t.components ? `, only ${t.components.join(\", \")}` : \"\";\n return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n })\n .join(\"; \");\n const functions = Object.entries(compact.functions).map(\n ([name, fn]) =>\n `${nameWithParameters(name, fn.parameters)}${\"callers\" in fn ? ` (${fn.callers})` : \"\"}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`,\n );\n const common = compact.commonProps\n ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v))\n : [];\n const lineSets = Object.entries(compact.components).map(([name, c]) => ({\n name,\n c,\n own: [\n ...Object.entries(c.props).map(([k, v]) => propLine(k, v)),\n ...Object.entries(c.events ?? {}).map(\n ([e, d]) => ` ${e}: action - ${oneLine(d)}`,\n ),\n ],\n }));\n const components = lineSets.flatMap(({ name, c, own }) => {\n const lines = [\n `${name}${c.includesCommonProps ? \" +common\" : \"\"}${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ...own,\n ];\n // Slots are props that take component ids.\n for (const [slot, kind] of Object.entries(c.slots ?? {})) {\n lines.push(\n ` ${slot}: ${kind === \"single\" ? \"componentId\" : \"childList\"}`,\n );\n }\n return lines;\n });\n const surfaces = Object.keys(compact.surfaces).length\n ? JSON.stringify(compact.surfaces)\n : \"none\";\n return [\n `Catalog ${compact.catalogId}. Targets: ${targets || \"none\"}`,\n 'Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.',\n `Functions:\\n${functions.join(\"\\n\")}`,\n ...(common.length ? [`Common props:\\n${common.join(\"\\n\")}`] : []),\n `Components:\\n${components.join(\"\\n\")}`,\n `Surfaces: ${surfaces}`,\n ].join(\"\\n\\n\");\n}\n\n/**\n * The component schema of the markdown transport, as the text of its\n * context entry, in the notation of `settings`. `compact` is the schema that\n * ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts\n * as JSON, `ts-signature` one line per component, `lines` one line per\n * prop with bare enums (the fewest tokens).\n *\n * @example\n * ```ts\n * const value = renderGenerativeUISchema(manifest, spec.schema.markdown);\n * ```\n */\nexport function renderGenerativeUISchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n): string {\n switch (settings.notation) {\n case \"compact\":\n return JSON.stringify(\n buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n }),\n );\n case \"json-schema\": {\n const components = Object.fromEntries(\n Object.entries(manifest.components).map(([name, c]) => [\n name,\n settings.propDescriptions\n ? c\n : { ...c, props: withoutPropDescriptions(c.props) },\n ]),\n );\n return JSON.stringify({\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions: manifest.functions,\n components,\n surfaces: manifest.surfaces,\n });\n }\n case \"ts-signature\":\n return signatureSchema(manifest, settings);\n case \"lines\":\n return linesSchema(manifest, settings);\n }\n}\n\n/**\n * The component list of the tool guide, one entry per component, in the\n * notation of `settings`. `json-schema` is the list that ships.\n */\nexport function renderGenerativeUIComponentList(\n components: GenerativeUIManifest[\"components\"],\n settings: GenerativeUISchemaSettings,\n): string {\n return Object.entries(components)\n .map(([name, c]) => {\n if (\n settings.notation === \"ts-signature\" ||\n settings.notation === \"lines\"\n ) {\n return signatureLines(name, c, settings).join(\"\\n\");\n }\n const props =\n settings.notation === \"compact\"\n ? compactProps(c.props, settings.propDescriptions)\n : settings.propDescriptions\n ? c.props\n : withoutPropDescriptions(c.props);\n const lines = [\n `- **${name}**${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ` props: ${JSON.stringify(props)}`,\n ];\n if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);\n if (c.slots)\n lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);\n return lines.join(\"\\n\");\n })\n .join(\"\\n\");\n}\n\n/**\n * The manifest with replaced descriptions: `\"Name\"` replaces the\n * description of a component, `\"Name.prop\"` the description of one of its\n * props. Keys that name no component or prop are left out of the result\n * and listed in `unknown`. The manifest is not changed.\n */\nexport function applyDescriptionOverrides<\n M extends Pick<GenerativeUIManifest, \"components\">,\n>(\n manifest: M,\n overrides: Record<string, string>,\n): { manifest: M; unknown: string[] } {\n const components = structuredClone(manifest.components);\n const unknown: string[] = [];\n for (const [key, description] of Object.entries(overrides)) {\n const [name, prop, ...rest] = key.split(\".\");\n // Own keys only: \"__proto__\" must never reach Object.prototype.\n const component = hasOwn(components, name) ? components[name] : undefined;\n if (!component || rest.length > 0) {\n unknown.push(key);\n continue;\n }\n if (prop === undefined) {\n component.description = description;\n continue;\n }\n const properties = component.props.properties;\n const property =\n isRecord(properties) && hasOwn(properties, prop)\n ? properties[prop]\n : undefined;\n if (!isRecord(property)) {\n unknown.push(key);\n continue;\n }\n property.description = description;\n }\n return { manifest: { ...manifest, components }, unknown };\n}\n\n/**\n * The components and props of `manifest` whose names do not appear in\n * `rendered`. A schema notation that leaves one out would hide part of the\n * catalog from the model.\n */\nexport function schemaCoverage(\n manifest: Pick<GenerativeUIManifest, \"components\">,\n rendered: string,\n): { missing: string[] } {\n const missing: string[] = [];\n for (const [name, component] of Object.entries(manifest.components)) {\n if (!rendered.includes(name)) missing.push(name);\n const properties = isRecord(component.props.properties)\n ? component.props.properties\n : {};\n for (const prop of Object.keys(properties)) {\n if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);\n }\n }\n return { missing };\n}\n"],"mappings":";;;;;;AAoBA,MAAM,UAAU,QAAgB,QAC9B,OAAO,UAAU,eAAe,KAAK,QAAQ,IAAI;AAEnD,SAAS,SAAS,OAAkD;AAClE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;;AAI7E,SAAS,wBACP,QACwB;AACxB,KAAI,CAAC,SAAS,OAAO,WAAW,CAAE,QAAO;AACzC,QAAO;EACL,GAAG;EACH,YAAY,OAAO,YACjB,OAAO,QAAQ,OAAO,WAAW,CAAC,KAAK,CAAC,KAAK,WAAW;AACtD,OAAI,CAAC,SAAS,MAAM,CAAE,QAAO,CAAC,KAAK,MAAM;GACzC,MAAM,EAAE,aAAa,cAAc,GAAG,SAAS;AAC/C,UAAO,CAAC,KAAK,KAAK;IAClB,CACH;EACF;;;AAIH,SAAS,kBAAkB,IAA+C;CACxE,MAAM,YAAY,GAAG,WAAW;AAKhC,QAHE,OAAO,cAAc,YACrB,UAAU,WAAW,IAAI,IACzB,OAAO,KAAK,GAAG,WAAW,CAAC,WAAW,IAClB,YAAYA,mCAAY,GAAG,YAAY,EAAE;;;;;;AAOjE,SAAS,mBAAmB,MAAc,YAAoB;AAC5D,KAAI,WAAW,WAAW,IAAI,CAAE,QAAO,GAAG,OAAO;AACjD,KAAI,eAAe,SAAU,QAAO,GAAG,KAAK;AAC5C,QAAO,GAAG,KAAK,GAAG,WAAW;;;;;;;AAQ/B,SAAS,eACP,MACA,WACA,UACA;CAIA,MAAM,QAAQ,CACZ,KAAK,KAAK,GAJE,OAAO,QACnBC,oCAAa,UAAU,OAAO,SAAS,iBAAiB,CACzD,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I,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,KAAK;EAChD,MAAM,aAAa,SAAS,UAAU,MAAM,WAAW,GACnD,UAAU,MAAM,aAChB,EAAE;AACN,OAAK,MAAM,QAAQ,OAAO,KAAK,WAAW,CACxC,KAAI,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,GAAG,KAAK,GAAG,OAAO;;AAGjE,QAAO,EAAE,SAAS"}
1
+ {"version":3,"file":"schema-render.cjs","names":["compactType","compactProps","functionCallers","GENERATIVE_UI_AGENT_FUNCTION_CALLS","buildCompactGenerativeUISchema"],"sources":["../../src/generative-ui/schema-render.ts"],"sourcesContent":["import { functionCallers } from \"./basic-functions\";\nimport {\n buildCompactGenerativeUISchema,\n compactProps,\n compactType,\n} from \"./compact-schema\";\nimport type { GenerativeUISchemaSettings } from \"./prompt-spec\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type {\n GenerativeUIComponentManifest,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\n\ntype SchemaManifest = Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n>;\n\n/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */\nconst hasOwn = (target: object, key: string) =>\n Object.prototype.hasOwnProperty.call(target, key);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The props schema without the description of each prop. */\nfunction withoutPropDescriptions(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n if (!isRecord(schema.properties)) return schema;\n return {\n ...schema,\n properties: Object.fromEntries(\n Object.entries(schema.properties).map(([key, value]) => {\n if (!isRecord(value)) return [key, value];\n const { description: _description, ...rest } = value;\n return [key, rest];\n }),\n ),\n };\n}\n\n/** The short signature of a function: its built-in signature, or its schema made short. */\nfunction functionSignature(fn: GenerativeUIManifest[\"functions\"][string]) {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n return hasSignature ? signature : compactType(fn.parameters, 0);\n}\n\n/**\n * A function name with its parameters: `pick{ id: string }`, `clearCart{}`\n * for no arguments, `setVolume(number)` for a parameter that is not an object.\n */\nfunction nameWithParameters(name: string, parameters: string) {\n if (parameters.startsWith(\"{\")) return `${name}${parameters}`;\n if (parameters === \"object\") return `${name}{}`;\n return `${name}(${parameters})`;\n}\n\n/**\n * One component as a TypeScript-like signature line, with its events and\n * slots on the lines below:\n * `- Text(text?: string // The text): Shows text`.\n */\nfunction signatureLines(\n name: string,\n component: GenerativeUIComponentManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const props = Object.entries(\n compactProps(component.props, settings.propDescriptions),\n ).map(([key, type]) => `${key}: ${type}`);\n const lines = [\n `- ${name}(${props.join(\", \")})${component.description ? `: ${oneLine(component.description)}` : \"\"}`,\n ];\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) =>\n `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`,\n );\n if (events.length > 0) lines.push(` events: ${events.join(\", \")}`);\n const slots = Object.entries(component.slots ?? {}).map(\n ([slot, kind]) => `${slot} (${kind})`,\n );\n if (slots.length > 0) lines.push(` slots: ${slots.join(\", \")}`);\n return lines;\n}\n\n/** The whole schema as signature text: targets, functions, components, surfaces. */\nfunction signatureSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const targets = manifest.targets.map((t) => {\n const limit = t.components ? `; only ${t.components.join(\", \")}` : \"\";\n return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n });\n const functions = Object.entries(manifest.functions).map(([name, fn]) => {\n const callers = functionCallers(fn);\n const who =\n callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? callers.renderer\n ? \" (UI or agent)\"\n : \" (agent only)\"\n : \"\";\n return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`;\n });\n const components = Object.entries(manifest.components).flatMap(\n ([name, component]) => signatureLines(name, component, settings),\n );\n const surfaces = Object.entries(manifest.surfaces).map(\n ([id, s]) =>\n `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : \"\"}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : \"\"}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join(\"\")}`,\n );\n return [\n `Catalog: ${manifest.catalogId}`,\n `Targets:\\n${targets.join(\"\\n\") || \"- none\"}`,\n `Functions:\\n${functions.join(\"\\n\") || \"- none\"}`,\n `Components:\\n${components.join(\"\\n\") || \"- none\"}`,\n `Surfaces:\\n${surfaces.join(\"\\n\") || \"- none\"}`,\n ].join(\"\\n\\n\");\n}\n\n/** A description on one line, so its lines cannot read as new entries. */\nfunction oneLine(text: string) {\n return text.replace(/\\s*\\n\\s*/g, \" \");\n}\n\n/** Values that would read as a type or a number once their quotes are gone. */\nconst AMBIGUOUS_VALUE =\n /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\\d[\\d.]*)$/;\n\n/**\n * A compact type with bare enum values and tight unions:\n * `\"sm\" | \"md\" | \"lg\"` becomes `sm|md|lg`. A union keeps its quotes when it\n * also holds a type (`\"sm\" | number`) or a value that reads as a type or a\n * number (`\"number\" | \"currency\"`, `\"1\" | \"2\"`), and so do values that are\n * not plain words.\n */\nfunction bareType(type: string) {\n return type\n .replace(\n /\"[A-Za-z0-9_#%.-]+\"(?: \\| \"[A-Za-z0-9_#%.-]+\")*(?! \\|)/g,\n (union: string, offset: number, whole: string) => {\n if (whole.slice(offset - 2, offset) === \"| \") return union;\n const values = union.split(\" | \").map((v) => v.slice(1, -1));\n if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;\n return values.join(\"|\");\n },\n )\n .replace(/ \\| /g, \"|\");\n}\n\n/** A compact prop (`type // description`) as `name: type - description`. */\nfunction propLine(key: string, value: string) {\n const name = key.endsWith(\"?\") ? key.slice(0, -1) : `${key}*`;\n const cut = value.indexOf(\" // \");\n const type = bareType(cut < 0 ? value : value.slice(0, cut));\n const text = cut < 0 ? \"\" : value.slice(cut + 4).trim();\n const description = text ? ` - ${oneLine(text)}` : \"\";\n return ` ${name}: ${type}${description}`;\n}\n\n/**\n * The whole schema as plain lines: a header line per component and one\n * indented line per prop: events are `action` props and slots `componentId`\n * or `childList` props. Props are optional unless marked `*`, enum values\n * are bare, and the common props are listed once. It carries the same names, types and descriptions as\n * `compact`, in fewer tokens.\n */\nfunction linesSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const compact = buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n });\n const targets = compact.targets\n .map((t) => {\n const limit = t.components ? `, only ${t.components.join(\", \")}` : \"\";\n return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n })\n .join(\"; \");\n const functions = Object.entries(compact.functions).map(\n ([name, fn]) =>\n `${nameWithParameters(name, fn.parameters)}${\"callers\" in fn ? ` (${fn.callers})` : \"\"}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`,\n );\n const common = compact.commonProps\n ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v))\n : [];\n const lineSets = Object.entries(compact.components).map(([name, c]) => ({\n name,\n c,\n own: [\n ...Object.entries(c.props).map(([k, v]) => propLine(k, v)),\n ...Object.entries(c.events ?? {}).map(\n ([e, d]) => ` ${e}: action - ${oneLine(d)}`,\n ),\n ],\n }));\n const components = lineSets.flatMap(({ name, c, own }) => {\n const lines = [\n `${name}${c.includesCommonProps ? \" +common\" : \"\"}${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ...own,\n ];\n // Slots are props that take component ids.\n for (const [slot, kind] of Object.entries(c.slots ?? {})) {\n lines.push(\n ` ${slot}: ${kind === \"single\" ? \"componentId\" : \"childList\"}`,\n );\n }\n return lines;\n });\n const surfaces = Object.keys(compact.surfaces).length\n ? JSON.stringify(compact.surfaces)\n : \"none\";\n return [\n `Catalog ${compact.catalogId}. Targets: ${targets || \"none\"}`,\n 'Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.',\n `Functions:\\n${functions.join(\"\\n\")}`,\n ...(common.length ? [`Common props:\\n${common.join(\"\\n\")}`] : []),\n `Components:\\n${components.join(\"\\n\")}`,\n `Surfaces: ${surfaces}`,\n ].join(\"\\n\\n\");\n}\n\n/**\n * The component schema of the markdown transport, as the text of its\n * context entry, in the notation of `settings`. `compact` is the schema that\n * ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts\n * as JSON, `ts-signature` one line per component, `lines` one line per\n * prop with bare enums (the fewest tokens).\n *\n * @example\n * ```ts\n * const value = renderGenerativeUISchema(manifest, spec.schema.markdown);\n * ```\n */\nexport function renderGenerativeUISchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n): string {\n switch (settings.notation) {\n case \"compact\":\n return JSON.stringify(\n buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n }),\n );\n case \"json-schema\": {\n const components = Object.fromEntries(\n Object.entries(manifest.components).map(([name, c]) => [\n name,\n settings.propDescriptions\n ? c\n : { ...c, props: withoutPropDescriptions(c.props) },\n ]),\n );\n return JSON.stringify({\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions: manifest.functions,\n components,\n surfaces: manifest.surfaces,\n });\n }\n case \"ts-signature\":\n return signatureSchema(manifest, settings);\n case \"lines\":\n return linesSchema(manifest, settings);\n }\n}\n\n/**\n * The component list of the tool guide, one entry per component, in the\n * notation of `settings`. `json-schema` is the list that ships.\n */\nexport function renderGenerativeUIComponentList(\n components: GenerativeUIManifest[\"components\"],\n settings: GenerativeUISchemaSettings,\n): string {\n return Object.entries(components)\n .map(([name, c]) => {\n if (\n settings.notation === \"ts-signature\" ||\n settings.notation === \"lines\"\n ) {\n return signatureLines(name, c, settings).join(\"\\n\");\n }\n const props =\n settings.notation === \"compact\"\n ? compactProps(c.props, settings.propDescriptions)\n : settings.propDescriptions\n ? c.props\n : withoutPropDescriptions(c.props);\n const lines = [\n `- **${name}**${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ` props: ${JSON.stringify(props)}`,\n ];\n if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);\n if (c.slots)\n lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);\n return lines.join(\"\\n\");\n })\n .join(\"\\n\");\n}\n\n/**\n * The manifest with replaced descriptions: `\"Name\"` replaces the\n * description of a component, `\"Name.prop\"` the description of one of its\n * props. Keys that name no component or prop are left out of the result\n * and listed in `unknown`. The manifest is not changed.\n */\nexport function applyDescriptionOverrides<\n M extends Pick<GenerativeUIManifest, \"components\">,\n>(\n manifest: M,\n overrides: Record<string, string>,\n): { manifest: M; unknown: string[] } {\n const components = structuredClone(manifest.components);\n const unknown: string[] = [];\n for (const [key, description] of Object.entries(overrides)) {\n const [name, prop, ...rest] = key.split(\".\");\n // Own keys only: \"__proto__\" must never reach Object.prototype.\n const component =\n name !== undefined && hasOwn(components, name)\n ? components[name]\n : undefined;\n if (!component || rest.length > 0) {\n unknown.push(key);\n continue;\n }\n if (prop === undefined) {\n component.description = description;\n continue;\n }\n const properties = component.props.properties;\n const property =\n isRecord(properties) && hasOwn(properties, prop)\n ? properties[prop]\n : undefined;\n if (!isRecord(property)) {\n unknown.push(key);\n continue;\n }\n property.description = description;\n }\n return { manifest: { ...manifest, components }, unknown };\n}\n\n/**\n * The components and props of `manifest` whose names do not appear in\n * `rendered`. A schema notation that leaves one out would hide part of the\n * catalog from the model.\n */\nexport function schemaCoverage(\n manifest: Pick<GenerativeUIManifest, \"components\">,\n rendered: string,\n): { missing: string[] } {\n const missing: string[] = [];\n for (const [name, component] of Object.entries(manifest.components)) {\n if (!rendered.includes(name)) missing.push(name);\n const properties = isRecord(component.props.properties)\n ? component.props.properties\n : {};\n for (const prop of Object.keys(properties)) {\n if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);\n }\n }\n return { missing };\n}\n"],"mappings":";;;;;;AAoBA,MAAM,UAAU,QAAgB,QAC9B,OAAO,UAAU,eAAe,KAAK,QAAQ,IAAI;AAEnD,SAAS,SAAS,OAAkD;AAClE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;;AAI7E,SAAS,wBACP,QACwB;AACxB,KAAI,CAAC,SAAS,OAAO,WAAW,CAAE,QAAO;AACzC,QAAO;EACL,GAAG;EACH,YAAY,OAAO,YACjB,OAAO,QAAQ,OAAO,WAAW,CAAC,KAAK,CAAC,KAAK,WAAW;AACtD,OAAI,CAAC,SAAS,MAAM,CAAE,QAAO,CAAC,KAAK,MAAM;GACzC,MAAM,EAAE,aAAa,cAAc,GAAG,SAAS;AAC/C,UAAO,CAAC,KAAK,KAAK;IAClB,CACH;EACF;;;AAIH,SAAS,kBAAkB,IAA+C;CACxE,MAAM,YAAY,GAAG,WAAW;AAKhC,QAHE,OAAO,cAAc,YACrB,UAAU,WAAW,IAAI,IACzB,OAAO,KAAK,GAAG,WAAW,CAAC,WAAW,IAClB,YAAYA,mCAAY,GAAG,YAAY,EAAE;;;;;;AAOjE,SAAS,mBAAmB,MAAc,YAAoB;AAC5D,KAAI,WAAW,WAAW,IAAI,CAAE,QAAO,GAAG,OAAO;AACjD,KAAI,eAAe,SAAU,QAAO,GAAG,KAAK;AAC5C,QAAO,GAAG,KAAK,GAAG,WAAW;;;;;;;AAQ/B,SAAS,eACP,MACA,WACA,UACA;CAIA,MAAM,QAAQ,CACZ,KAAK,KAAK,GAJE,OAAO,QACnBC,oCAAa,UAAU,OAAO,SAAS,iBAAiB,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EAAE;AACnE,MAAI,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,KAAK;EAChD,MAAM,aAAa,SAAS,UAAU,MAAM,WAAW,GACnD,UAAU,MAAM,aAChB,EAAE;AACN,OAAK,MAAM,QAAQ,OAAO,KAAK,WAAW,CACxC,KAAI,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,GAAG,KAAK,GAAG,OAAO;;AAGjE,QAAO,EAAE,SAAS"}
@@ -1 +1 @@
1
- {"version":3,"file":"schema-render.d.cts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAkCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
1
+ {"version":3,"file":"schema-render.d.cts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAqCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"schema-render.d.mts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAkCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
1
+ {"version":3,"file":"schema-render.d.mts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAqCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
@@ -194,7 +194,7 @@ function applyDescriptionOverrides(manifest, overrides) {
194
194
  const unknown = [];
195
195
  for (const [key, description] of Object.entries(overrides)) {
196
196
  const [name, prop, ...rest] = key.split(".");
197
- const component = hasOwn(components, name) ? components[name] : void 0;
197
+ const component = name !== void 0 && hasOwn(components, name) ? components[name] : void 0;
198
198
  if (!component || rest.length > 0) {
199
199
  unknown.push(key);
200
200
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"schema-render.mjs","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"sourcesContent":["import { functionCallers } from \"./basic-functions\";\nimport {\n buildCompactGenerativeUISchema,\n compactProps,\n compactType,\n} from \"./compact-schema\";\nimport type { GenerativeUISchemaSettings } from \"./prompt-spec\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type {\n GenerativeUIComponentManifest,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\n\ntype SchemaManifest = Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n>;\n\n/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */\nconst hasOwn = (target: object, key: string) =>\n Object.prototype.hasOwnProperty.call(target, key);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The props schema without the description of each prop. */\nfunction withoutPropDescriptions(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n if (!isRecord(schema.properties)) return schema;\n return {\n ...schema,\n properties: Object.fromEntries(\n Object.entries(schema.properties).map(([key, value]) => {\n if (!isRecord(value)) return [key, value];\n const { description: _description, ...rest } = value;\n return [key, rest];\n }),\n ),\n };\n}\n\n/** The short signature of a function: its built-in signature, or its schema made short. */\nfunction functionSignature(fn: GenerativeUIManifest[\"functions\"][string]) {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n return hasSignature ? signature : compactType(fn.parameters, 0);\n}\n\n/**\n * A function name with its parameters: `pick{ id: string }`, `clearCart{}`\n * for no arguments, `setVolume(number)` for a parameter that is not an object.\n */\nfunction nameWithParameters(name: string, parameters: string) {\n if (parameters.startsWith(\"{\")) return `${name}${parameters}`;\n if (parameters === \"object\") return `${name}{}`;\n return `${name}(${parameters})`;\n}\n\n/**\n * One component as a TypeScript-like signature line, with its events and\n * slots on the lines below:\n * `- Text(text?: string // The text): Shows text`.\n */\nfunction signatureLines(\n name: string,\n component: GenerativeUIComponentManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const props = Object.entries(\n compactProps(component.props, settings.propDescriptions),\n ).map(([key, type]) => `${key}: ${type}`);\n const lines = [\n `- ${name}(${props.join(\", \")})${component.description ? `: ${oneLine(component.description)}` : \"\"}`,\n ];\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) =>\n `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`,\n );\n if (events.length > 0) lines.push(` events: ${events.join(\", \")}`);\n const slots = Object.entries(component.slots ?? {}).map(\n ([slot, kind]) => `${slot} (${kind})`,\n );\n if (slots.length > 0) lines.push(` slots: ${slots.join(\", \")}`);\n return lines;\n}\n\n/** The whole schema as signature text: targets, functions, components, surfaces. */\nfunction signatureSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const targets = manifest.targets.map((t) => {\n const limit = t.components ? `; only ${t.components.join(\", \")}` : \"\";\n return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n });\n const functions = Object.entries(manifest.functions).map(([name, fn]) => {\n const callers = functionCallers(fn);\n const who =\n callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? callers.renderer\n ? \" (UI or agent)\"\n : \" (agent only)\"\n : \"\";\n return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`;\n });\n const components = Object.entries(manifest.components).flatMap(\n ([name, component]) => signatureLines(name, component, settings),\n );\n const surfaces = Object.entries(manifest.surfaces).map(\n ([id, s]) =>\n `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : \"\"}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : \"\"}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join(\"\")}`,\n );\n return [\n `Catalog: ${manifest.catalogId}`,\n `Targets:\\n${targets.join(\"\\n\") || \"- none\"}`,\n `Functions:\\n${functions.join(\"\\n\") || \"- none\"}`,\n `Components:\\n${components.join(\"\\n\") || \"- none\"}`,\n `Surfaces:\\n${surfaces.join(\"\\n\") || \"- none\"}`,\n ].join(\"\\n\\n\");\n}\n\n/** A description on one line, so its lines cannot read as new entries. */\nfunction oneLine(text: string) {\n return text.replace(/\\s*\\n\\s*/g, \" \");\n}\n\n/** Values that would read as a type or a number once their quotes are gone. */\nconst AMBIGUOUS_VALUE =\n /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\\d[\\d.]*)$/;\n\n/**\n * A compact type with bare enum values and tight unions:\n * `\"sm\" | \"md\" | \"lg\"` becomes `sm|md|lg`. A union keeps its quotes when it\n * also holds a type (`\"sm\" | number`) or a value that reads as a type or a\n * number (`\"number\" | \"currency\"`, `\"1\" | \"2\"`), and so do values that are\n * not plain words.\n */\nfunction bareType(type: string) {\n return type\n .replace(\n /\"[A-Za-z0-9_#%.-]+\"(?: \\| \"[A-Za-z0-9_#%.-]+\")*(?! \\|)/g,\n (union: string, offset: number, whole: string) => {\n if (whole.slice(offset - 2, offset) === \"| \") return union;\n const values = union.split(\" | \").map((v) => v.slice(1, -1));\n if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;\n return values.join(\"|\");\n },\n )\n .replace(/ \\| /g, \"|\");\n}\n\n/** A compact prop (`type // description`) as `name: type - description`. */\nfunction propLine(key: string, value: string) {\n const name = key.endsWith(\"?\") ? key.slice(0, -1) : `${key}*`;\n const cut = value.indexOf(\" // \");\n const type = bareType(cut < 0 ? value : value.slice(0, cut));\n const text = cut < 0 ? \"\" : value.slice(cut + 4).trim();\n const description = text ? ` - ${oneLine(text)}` : \"\";\n return ` ${name}: ${type}${description}`;\n}\n\n/**\n * The whole schema as plain lines: a header line per component and one\n * indented line per prop: events are `action` props and slots `componentId`\n * or `childList` props. Props are optional unless marked `*`, enum values\n * are bare, and the common props are listed once. It carries the same names, types and descriptions as\n * `compact`, in fewer tokens.\n */\nfunction linesSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const compact = buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n });\n const targets = compact.targets\n .map((t) => {\n const limit = t.components ? `, only ${t.components.join(\", \")}` : \"\";\n return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n })\n .join(\"; \");\n const functions = Object.entries(compact.functions).map(\n ([name, fn]) =>\n `${nameWithParameters(name, fn.parameters)}${\"callers\" in fn ? ` (${fn.callers})` : \"\"}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`,\n );\n const common = compact.commonProps\n ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v))\n : [];\n const lineSets = Object.entries(compact.components).map(([name, c]) => ({\n name,\n c,\n own: [\n ...Object.entries(c.props).map(([k, v]) => propLine(k, v)),\n ...Object.entries(c.events ?? {}).map(\n ([e, d]) => ` ${e}: action - ${oneLine(d)}`,\n ),\n ],\n }));\n const components = lineSets.flatMap(({ name, c, own }) => {\n const lines = [\n `${name}${c.includesCommonProps ? \" +common\" : \"\"}${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ...own,\n ];\n // Slots are props that take component ids.\n for (const [slot, kind] of Object.entries(c.slots ?? {})) {\n lines.push(\n ` ${slot}: ${kind === \"single\" ? \"componentId\" : \"childList\"}`,\n );\n }\n return lines;\n });\n const surfaces = Object.keys(compact.surfaces).length\n ? JSON.stringify(compact.surfaces)\n : \"none\";\n return [\n `Catalog ${compact.catalogId}. Targets: ${targets || \"none\"}`,\n 'Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.',\n `Functions:\\n${functions.join(\"\\n\")}`,\n ...(common.length ? [`Common props:\\n${common.join(\"\\n\")}`] : []),\n `Components:\\n${components.join(\"\\n\")}`,\n `Surfaces: ${surfaces}`,\n ].join(\"\\n\\n\");\n}\n\n/**\n * The component schema of the markdown transport, as the text of its\n * context entry, in the notation of `settings`. `compact` is the schema that\n * ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts\n * as JSON, `ts-signature` one line per component, `lines` one line per\n * prop with bare enums (the fewest tokens).\n *\n * @example\n * ```ts\n * const value = renderGenerativeUISchema(manifest, spec.schema.markdown);\n * ```\n */\nexport function renderGenerativeUISchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n): string {\n switch (settings.notation) {\n case \"compact\":\n return JSON.stringify(\n buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n }),\n );\n case \"json-schema\": {\n const components = Object.fromEntries(\n Object.entries(manifest.components).map(([name, c]) => [\n name,\n settings.propDescriptions\n ? c\n : { ...c, props: withoutPropDescriptions(c.props) },\n ]),\n );\n return JSON.stringify({\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions: manifest.functions,\n components,\n surfaces: manifest.surfaces,\n });\n }\n case \"ts-signature\":\n return signatureSchema(manifest, settings);\n case \"lines\":\n return linesSchema(manifest, settings);\n }\n}\n\n/**\n * The component list of the tool guide, one entry per component, in the\n * notation of `settings`. `json-schema` is the list that ships.\n */\nexport function renderGenerativeUIComponentList(\n components: GenerativeUIManifest[\"components\"],\n settings: GenerativeUISchemaSettings,\n): string {\n return Object.entries(components)\n .map(([name, c]) => {\n if (\n settings.notation === \"ts-signature\" ||\n settings.notation === \"lines\"\n ) {\n return signatureLines(name, c, settings).join(\"\\n\");\n }\n const props =\n settings.notation === \"compact\"\n ? compactProps(c.props, settings.propDescriptions)\n : settings.propDescriptions\n ? c.props\n : withoutPropDescriptions(c.props);\n const lines = [\n `- **${name}**${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ` props: ${JSON.stringify(props)}`,\n ];\n if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);\n if (c.slots)\n lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);\n return lines.join(\"\\n\");\n })\n .join(\"\\n\");\n}\n\n/**\n * The manifest with replaced descriptions: `\"Name\"` replaces the\n * description of a component, `\"Name.prop\"` the description of one of its\n * props. Keys that name no component or prop are left out of the result\n * and listed in `unknown`. The manifest is not changed.\n */\nexport function applyDescriptionOverrides<\n M extends Pick<GenerativeUIManifest, \"components\">,\n>(\n manifest: M,\n overrides: Record<string, string>,\n): { manifest: M; unknown: string[] } {\n const components = structuredClone(manifest.components);\n const unknown: string[] = [];\n for (const [key, description] of Object.entries(overrides)) {\n const [name, prop, ...rest] = key.split(\".\");\n // Own keys only: \"__proto__\" must never reach Object.prototype.\n const component = hasOwn(components, name) ? components[name] : undefined;\n if (!component || rest.length > 0) {\n unknown.push(key);\n continue;\n }\n if (prop === undefined) {\n component.description = description;\n continue;\n }\n const properties = component.props.properties;\n const property =\n isRecord(properties) && hasOwn(properties, prop)\n ? properties[prop]\n : undefined;\n if (!isRecord(property)) {\n unknown.push(key);\n continue;\n }\n property.description = description;\n }\n return { manifest: { ...manifest, components }, unknown };\n}\n\n/**\n * The components and props of `manifest` whose names do not appear in\n * `rendered`. A schema notation that leaves one out would hide part of the\n * catalog from the model.\n */\nexport function schemaCoverage(\n manifest: Pick<GenerativeUIManifest, \"components\">,\n rendered: string,\n): { missing: string[] } {\n const missing: string[] = [];\n for (const [name, component] of Object.entries(manifest.components)) {\n if (!rendered.includes(name)) missing.push(name);\n const properties = isRecord(component.props.properties)\n ? component.props.properties\n : {};\n for (const prop of Object.keys(properties)) {\n if (!rendered.includes(prop)) missing.push(`${name}.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E,SAAQ,KAAK,GAAG,KAAK,GAAG,OAAO;;AAGjE,QAAO,EAAE,SAAS"}
1
+ {"version":3,"file":"schema-render.mjs","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"sourcesContent":["import { functionCallers } from \"./basic-functions\";\nimport {\n buildCompactGenerativeUISchema,\n compactProps,\n compactType,\n} from \"./compact-schema\";\nimport type { GenerativeUISchemaSettings } from \"./prompt-spec\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type {\n GenerativeUIComponentManifest,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\n\ntype SchemaManifest = Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n>;\n\n/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */\nconst hasOwn = (target: object, key: string) =>\n Object.prototype.hasOwnProperty.call(target, key);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The props schema without the description of each prop. */\nfunction withoutPropDescriptions(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n if (!isRecord(schema.properties)) return schema;\n return {\n ...schema,\n properties: Object.fromEntries(\n Object.entries(schema.properties).map(([key, value]) => {\n if (!isRecord(value)) return [key, value];\n const { description: _description, ...rest } = value;\n return [key, rest];\n }),\n ),\n };\n}\n\n/** The short signature of a function: its built-in signature, or its schema made short. */\nfunction functionSignature(fn: GenerativeUIManifest[\"functions\"][string]) {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n return hasSignature ? signature : compactType(fn.parameters, 0);\n}\n\n/**\n * A function name with its parameters: `pick{ id: string }`, `clearCart{}`\n * for no arguments, `setVolume(number)` for a parameter that is not an object.\n */\nfunction nameWithParameters(name: string, parameters: string) {\n if (parameters.startsWith(\"{\")) return `${name}${parameters}`;\n if (parameters === \"object\") return `${name}{}`;\n return `${name}(${parameters})`;\n}\n\n/**\n * One component as a TypeScript-like signature line, with its events and\n * slots on the lines below:\n * `- Text(text?: string // The text): Shows text`.\n */\nfunction signatureLines(\n name: string,\n component: GenerativeUIComponentManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const props = Object.entries(\n compactProps(component.props, settings.propDescriptions),\n ).map(([key, type]) => `${key}: ${type}`);\n const lines = [\n `- ${name}(${props.join(\", \")})${component.description ? `: ${oneLine(component.description)}` : \"\"}`,\n ];\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) =>\n `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`,\n );\n if (events.length > 0) lines.push(` events: ${events.join(\", \")}`);\n const slots = Object.entries(component.slots ?? {}).map(\n ([slot, kind]) => `${slot} (${kind})`,\n );\n if (slots.length > 0) lines.push(` slots: ${slots.join(\", \")}`);\n return lines;\n}\n\n/** The whole schema as signature text: targets, functions, components, surfaces. */\nfunction signatureSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const targets = manifest.targets.map((t) => {\n const limit = t.components ? `; only ${t.components.join(\", \")}` : \"\";\n return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n });\n const functions = Object.entries(manifest.functions).map(([name, fn]) => {\n const callers = functionCallers(fn);\n const who =\n callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? callers.renderer\n ? \" (UI or agent)\"\n : \" (agent only)\"\n : \"\";\n return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`;\n });\n const components = Object.entries(manifest.components).flatMap(\n ([name, component]) => signatureLines(name, component, settings),\n );\n const surfaces = Object.entries(manifest.surfaces).map(\n ([id, s]) =>\n `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : \"\"}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : \"\"}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join(\"\")}`,\n );\n return [\n `Catalog: ${manifest.catalogId}`,\n `Targets:\\n${targets.join(\"\\n\") || \"- none\"}`,\n `Functions:\\n${functions.join(\"\\n\") || \"- none\"}`,\n `Components:\\n${components.join(\"\\n\") || \"- none\"}`,\n `Surfaces:\\n${surfaces.join(\"\\n\") || \"- none\"}`,\n ].join(\"\\n\\n\");\n}\n\n/** A description on one line, so its lines cannot read as new entries. */\nfunction oneLine(text: string) {\n return text.replace(/\\s*\\n\\s*/g, \" \");\n}\n\n/** Values that would read as a type or a number once their quotes are gone. */\nconst AMBIGUOUS_VALUE =\n /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\\d[\\d.]*)$/;\n\n/**\n * A compact type with bare enum values and tight unions:\n * `\"sm\" | \"md\" | \"lg\"` becomes `sm|md|lg`. A union keeps its quotes when it\n * also holds a type (`\"sm\" | number`) or a value that reads as a type or a\n * number (`\"number\" | \"currency\"`, `\"1\" | \"2\"`), and so do values that are\n * not plain words.\n */\nfunction bareType(type: string) {\n return type\n .replace(\n /\"[A-Za-z0-9_#%.-]+\"(?: \\| \"[A-Za-z0-9_#%.-]+\")*(?! \\|)/g,\n (union: string, offset: number, whole: string) => {\n if (whole.slice(offset - 2, offset) === \"| \") return union;\n const values = union.split(\" | \").map((v) => v.slice(1, -1));\n if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;\n return values.join(\"|\");\n },\n )\n .replace(/ \\| /g, \"|\");\n}\n\n/** A compact prop (`type // description`) as `name: type - description`. */\nfunction propLine(key: string, value: string) {\n const name = key.endsWith(\"?\") ? key.slice(0, -1) : `${key}*`;\n const cut = value.indexOf(\" // \");\n const type = bareType(cut < 0 ? value : value.slice(0, cut));\n const text = cut < 0 ? \"\" : value.slice(cut + 4).trim();\n const description = text ? ` - ${oneLine(text)}` : \"\";\n return ` ${name}: ${type}${description}`;\n}\n\n/**\n * The whole schema as plain lines: a header line per component and one\n * indented line per prop: events are `action` props and slots `componentId`\n * or `childList` props. Props are optional unless marked `*`, enum values\n * are bare, and the common props are listed once. It carries the same names, types and descriptions as\n * `compact`, in fewer tokens.\n */\nfunction linesSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const compact = buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n });\n const targets = compact.targets\n .map((t) => {\n const limit = t.components ? `, only ${t.components.join(\", \")}` : \"\";\n return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n })\n .join(\"; \");\n const functions = Object.entries(compact.functions).map(\n ([name, fn]) =>\n `${nameWithParameters(name, fn.parameters)}${\"callers\" in fn ? ` (${fn.callers})` : \"\"}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`,\n );\n const common = compact.commonProps\n ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v))\n : [];\n const lineSets = Object.entries(compact.components).map(([name, c]) => ({\n name,\n c,\n own: [\n ...Object.entries(c.props).map(([k, v]) => propLine(k, v)),\n ...Object.entries(c.events ?? {}).map(\n ([e, d]) => ` ${e}: action - ${oneLine(d)}`,\n ),\n ],\n }));\n const components = lineSets.flatMap(({ name, c, own }) => {\n const lines = [\n `${name}${c.includesCommonProps ? \" +common\" : \"\"}${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ...own,\n ];\n // Slots are props that take component ids.\n for (const [slot, kind] of Object.entries(c.slots ?? {})) {\n lines.push(\n ` ${slot}: ${kind === \"single\" ? \"componentId\" : \"childList\"}`,\n );\n }\n return lines;\n });\n const surfaces = Object.keys(compact.surfaces).length\n ? JSON.stringify(compact.surfaces)\n : \"none\";\n return [\n `Catalog ${compact.catalogId}. Targets: ${targets || \"none\"}`,\n 'Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.',\n `Functions:\\n${functions.join(\"\\n\")}`,\n ...(common.length ? [`Common props:\\n${common.join(\"\\n\")}`] : []),\n `Components:\\n${components.join(\"\\n\")}`,\n `Surfaces: ${surfaces}`,\n ].join(\"\\n\\n\");\n}\n\n/**\n * The component schema of the markdown transport, as the text of its\n * context entry, in the notation of `settings`. `compact` is the schema that\n * ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts\n * as JSON, `ts-signature` one line per component, `lines` one line per\n * prop with bare enums (the fewest tokens).\n *\n * @example\n * ```ts\n * const value = renderGenerativeUISchema(manifest, spec.schema.markdown);\n * ```\n */\nexport function renderGenerativeUISchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n): string {\n switch (settings.notation) {\n case \"compact\":\n return JSON.stringify(\n buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n }),\n );\n case \"json-schema\": {\n const components = Object.fromEntries(\n Object.entries(manifest.components).map(([name, c]) => [\n name,\n settings.propDescriptions\n ? c\n : { ...c, props: withoutPropDescriptions(c.props) },\n ]),\n );\n return JSON.stringify({\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions: manifest.functions,\n components,\n surfaces: manifest.surfaces,\n });\n }\n case \"ts-signature\":\n return signatureSchema(manifest, settings);\n case \"lines\":\n return linesSchema(manifest, settings);\n }\n}\n\n/**\n * The component list of the tool guide, one entry per component, in the\n * notation of `settings`. `json-schema` is the list that ships.\n */\nexport function renderGenerativeUIComponentList(\n components: GenerativeUIManifest[\"components\"],\n settings: GenerativeUISchemaSettings,\n): string {\n return Object.entries(components)\n .map(([name, c]) => {\n if (\n settings.notation === \"ts-signature\" ||\n settings.notation === \"lines\"\n ) {\n return signatureLines(name, c, settings).join(\"\\n\");\n }\n const props =\n settings.notation === \"compact\"\n ? compactProps(c.props, settings.propDescriptions)\n : settings.propDescriptions\n ? c.props\n : withoutPropDescriptions(c.props);\n const lines = [\n `- **${name}**${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ` props: ${JSON.stringify(props)}`,\n ];\n if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);\n if (c.slots)\n lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);\n return lines.join(\"\\n\");\n })\n .join(\"\\n\");\n}\n\n/**\n * The manifest with replaced descriptions: `\"Name\"` replaces the\n * description of a component, `\"Name.prop\"` the description of one of its\n * props. Keys that name no component or prop are left out of the result\n * and listed in `unknown`. The manifest is not changed.\n */\nexport function applyDescriptionOverrides<\n M extends Pick<GenerativeUIManifest, \"components\">,\n>(\n manifest: M,\n overrides: Record<string, string>,\n): { manifest: M; unknown: string[] } {\n const components = structuredClone(manifest.components);\n const unknown: string[] = [];\n for (const [key, description] of Object.entries(overrides)) {\n const [name, prop, ...rest] = key.split(\".\");\n // Own keys only: \"__proto__\" must never reach Object.prototype.\n const component =\n name !== undefined && hasOwn(components, name)\n ? components[name]\n : undefined;\n if (!component || rest.length > 0) {\n unknown.push(key);\n continue;\n }\n if (prop === undefined) {\n component.description = description;\n continue;\n }\n const properties = component.props.properties;\n const property =\n isRecord(properties) && hasOwn(properties, prop)\n ? properties[prop]\n : undefined;\n if (!isRecord(property)) {\n unknown.push(key);\n continue;\n }\n property.description = description;\n }\n return { manifest: { ...manifest, components }, unknown };\n}\n\n/**\n * The components and props of `manifest` whose names do not appear in\n * `rendered`. A schema notation that leaves one out would hide part of the\n * catalog from the model.\n */\nexport function schemaCoverage(\n manifest: Pick<GenerativeUIManifest, \"components\">,\n rendered: string,\n): { missing: string[] } {\n const missing: string[] = [];\n for (const [name, component] of Object.entries(manifest.components)) {\n if (!rendered.includes(name)) missing.push(name);\n const properties = isRecord(component.props.properties)\n ? component.props.properties\n : {};\n for (const prop of Object.keys(properties)) {\n if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);\n }\n }\n return { missing };\n}\n"],"mappings":";;;;;;AAoBA,MAAM,UAAU,QAAgB,QAC9B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