@copilotkit/shared 2.0.0-next.1 → 2.0.0-rc.0
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.
- package/LICENSE +21 -0
- package/README.md +93 -12
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/a2ui-envelope.cjs +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +259 -0
- package/dist/a2ui-prompts.cjs.map +1 -0
- package/dist/a2ui-prompts.d.cts +79 -0
- package/dist/a2ui-prompts.d.cts.map +1 -0
- package/dist/a2ui-prompts.d.mts +79 -0
- package/dist/a2ui-prompts.d.mts.map +1 -0
- package/dist/a2ui-prompts.mjs +251 -0
- package/dist/a2ui-prompts.mjs.map +1 -0
- package/dist/attachments/content.cjs +17 -0
- package/dist/attachments/content.cjs.map +1 -0
- package/dist/attachments/content.d.cts +9 -0
- package/dist/attachments/content.d.cts.map +1 -0
- package/dist/attachments/content.d.mts +9 -0
- package/dist/attachments/content.d.mts.map +1 -0
- package/dist/attachments/content.mjs +16 -0
- package/dist/attachments/content.mjs.map +1 -0
- package/dist/attachments/types.d.cts +64 -0
- package/dist/attachments/types.d.cts.map +1 -0
- package/dist/attachments/types.d.mts +64 -0
- package/dist/attachments/types.d.mts.map +1 -0
- package/dist/attachments/utils.cjs +137 -0
- package/dist/attachments/utils.cjs.map +1 -0
- package/dist/attachments/utils.d.cts +44 -0
- package/dist/attachments/utils.d.cts.map +1 -0
- package/dist/attachments/utils.d.mts +44 -0
- package/dist/attachments/utils.d.mts.map +1 -0
- package/dist/attachments/utils.mjs +129 -0
- package/dist/attachments/utils.mjs.map +1 -0
- package/dist/constants/index.cjs +25 -0
- package/dist/constants/index.cjs.map +1 -0
- package/dist/constants/index.d.cts +17 -0
- package/dist/constants/index.d.cts.map +1 -0
- package/dist/constants/index.d.mts +17 -0
- package/dist/constants/index.d.mts.map +1 -0
- package/dist/constants/index.mjs +17 -12
- package/dist/constants/index.mjs.map +1 -1
- package/dist/debug-event-envelope.d.cts +13 -0
- package/dist/debug-event-envelope.d.cts.map +1 -0
- package/dist/debug-event-envelope.d.mts +13 -0
- package/dist/debug-event-envelope.d.mts.map +1 -0
- package/dist/debug.cjs +38 -0
- package/dist/debug.cjs.map +1 -0
- package/dist/debug.d.cts +29 -0
- package/dist/debug.d.cts.map +1 -0
- package/dist/debug.d.mts +29 -0
- package/dist/debug.d.mts.map +1 -0
- package/dist/debug.mjs +37 -0
- package/dist/debug.mjs.map +1 -0
- package/dist/event-transforms/index.cjs +15 -0
- package/dist/event-transforms/index.d.cts +4 -0
- package/dist/event-transforms/index.d.mts +4 -0
- package/dist/event-transforms/index.mjs +5 -0
- package/dist/event-transforms/open-generative-ui-middleware.cjs +286 -0
- package/dist/event-transforms/open-generative-ui-middleware.cjs.map +1 -0
- package/dist/event-transforms/open-generative-ui-middleware.d.cts +60 -0
- package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +1 -0
- package/dist/event-transforms/open-generative-ui-middleware.d.mts +60 -0
- package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +1 -0
- package/dist/event-transforms/open-generative-ui-middleware.mjs +283 -0
- package/dist/event-transforms/open-generative-ui-middleware.mjs.map +1 -0
- package/dist/event-transforms/recorded-events.cjs +59 -0
- package/dist/event-transforms/recorded-events.cjs.map +1 -0
- package/dist/event-transforms/recorded-events.d.cts +23 -0
- package/dist/event-transforms/recorded-events.d.cts.map +1 -0
- package/dist/event-transforms/recorded-events.d.mts +23 -0
- package/dist/event-transforms/recorded-events.d.mts.map +1 -0
- package/dist/event-transforms/recorded-events.mjs +58 -0
- package/dist/event-transforms/recorded-events.mjs.map +1 -0
- package/dist/finalize-events.cjs +186 -0
- package/dist/finalize-events.cjs.map +1 -0
- package/dist/finalize-events.d.cts +40 -0
- package/dist/finalize-events.d.cts.map +1 -0
- package/dist/finalize-events.d.mts +40 -0
- package/dist/finalize-events.d.mts.map +1 -0
- package/dist/finalize-events.mjs +184 -0
- package/dist/finalize-events.mjs.map +1 -0
- package/dist/generative-ui/basic-functions.cjs +177 -0
- package/dist/generative-ui/basic-functions.cjs.map +1 -0
- package/dist/generative-ui/basic-functions.d.cts +108 -0
- package/dist/generative-ui/basic-functions.d.cts.map +1 -0
- package/dist/generative-ui/basic-functions.d.mts +108 -0
- package/dist/generative-ui/basic-functions.d.mts.map +1 -0
- package/dist/generative-ui/basic-functions.mjs +167 -0
- package/dist/generative-ui/basic-functions.mjs.map +1 -0
- package/dist/generative-ui/child-refs.cjs +161 -0
- package/dist/generative-ui/child-refs.cjs.map +1 -0
- package/dist/generative-ui/child-refs.d.cts +68 -0
- package/dist/generative-ui/child-refs.d.cts.map +1 -0
- package/dist/generative-ui/child-refs.d.mts +68 -0
- package/dist/generative-ui/child-refs.d.mts.map +1 -0
- package/dist/generative-ui/child-refs.mjs +152 -0
- package/dist/generative-ui/child-refs.mjs.map +1 -0
- package/dist/generative-ui/is-record.cjs +10 -0
- package/dist/generative-ui/is-record.cjs.map +1 -0
- package/dist/generative-ui/is-record.mjs +9 -0
- package/dist/generative-ui/is-record.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +91 -0
- package/dist/generative-ui/prompt.cjs.map +1 -0
- package/dist/generative-ui/prompt.d.cts +13 -0
- package/dist/generative-ui/prompt.d.cts.map +1 -0
- package/dist/generative-ui/prompt.d.mts +13 -0
- package/dist/generative-ui/prompt.d.mts.map +1 -0
- package/dist/generative-ui/prompt.mjs +90 -0
- package/dist/generative-ui/prompt.mjs.map +1 -0
- package/dist/generative-ui/render-ui-args.cjs +160 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -0
- package/dist/generative-ui/render-ui-args.d.cts +40 -0
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
- package/dist/generative-ui/render-ui-args.d.mts +40 -0
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
- package/dist/generative-ui/render-ui-args.mjs +159 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +101 -0
- package/dist/generative-ui/types.cjs.map +1 -0
- package/dist/generative-ui/types.d.cts +270 -0
- package/dist/generative-ui/types.d.cts.map +1 -0
- package/dist/generative-ui/types.d.mts +270 -0
- package/dist/generative-ui/types.d.mts.map +1 -0
- package/dist/generative-ui/types.mjs +86 -0
- package/dist/generative-ui/types.mjs.map +1 -0
- package/dist/generative-ui/validate-operation.cjs +405 -0
- package/dist/generative-ui/validate-operation.cjs.map +1 -0
- package/dist/generative-ui/validate-operation.d.cts +86 -0
- package/dist/generative-ui/validate-operation.d.cts.map +1 -0
- package/dist/generative-ui/validate-operation.d.mts +86 -0
- package/dist/generative-ui/validate-operation.d.mts.map +1 -0
- package/dist/generative-ui/validate-operation.mjs +399 -0
- package/dist/generative-ui/validate-operation.mjs.map +1 -0
- package/dist/index.cjs +233 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +80 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +78 -123
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +3979 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/intelligence-routing-fields.cjs +35 -0
- package/dist/intelligence-routing-fields.cjs.map +1 -0
- package/dist/intelligence-routing-fields.d.cts +18 -0
- package/dist/intelligence-routing-fields.d.cts.map +1 -0
- package/dist/intelligence-routing-fields.d.mts +18 -0
- package/dist/intelligence-routing-fields.d.mts.map +1 -0
- package/dist/intelligence-routing-fields.mjs +34 -0
- package/dist/intelligence-routing-fields.mjs.map +1 -0
- package/dist/logger.cjs +7 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +6 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/open-generative-ui-prompts.cjs +8 -0
- package/dist/open-generative-ui-prompts.cjs.map +1 -0
- package/dist/open-generative-ui-prompts.d.cts +6 -0
- package/dist/open-generative-ui-prompts.d.cts.map +1 -0
- package/dist/open-generative-ui-prompts.d.mts +6 -0
- package/dist/open-generative-ui-prompts.d.mts.map +1 -0
- package/dist/open-generative-ui-prompts.mjs +7 -0
- package/dist/open-generative-ui-prompts.mjs.map +1 -0
- package/dist/package.cjs +12 -0
- package/dist/package.cjs.map +1 -0
- package/dist/package.mjs +6 -0
- package/dist/package.mjs.map +1 -0
- package/dist/standard-schema.cjs +31 -0
- package/dist/standard-schema.cjs.map +1 -0
- package/dist/standard-schema.d.cts +33 -0
- package/dist/standard-schema.d.cts.map +1 -0
- package/dist/standard-schema.d.mts +33 -0
- package/dist/standard-schema.d.mts.map +1 -0
- package/dist/standard-schema.mjs +30 -0
- package/dist/standard-schema.mjs.map +1 -0
- package/dist/telemetry/events.d.cts +72 -0
- package/dist/telemetry/events.d.cts.map +1 -0
- package/dist/telemetry/events.d.mts +72 -0
- package/dist/telemetry/events.d.mts.map +1 -0
- package/dist/telemetry/index.cjs +18 -0
- package/dist/telemetry/index.d.cts +5 -0
- package/dist/telemetry/index.d.mts +5 -0
- package/dist/telemetry/index.mjs +6 -12
- package/dist/telemetry/lambda-client.cjs +91 -0
- package/dist/telemetry/lambda-client.cjs.map +1 -0
- package/dist/telemetry/lambda-client.d.cts +31 -0
- package/dist/telemetry/lambda-client.d.cts.map +1 -0
- package/dist/telemetry/lambda-client.d.mts +31 -0
- package/dist/telemetry/lambda-client.d.mts.map +1 -0
- package/dist/telemetry/lambda-client.mjs +87 -0
- package/dist/telemetry/lambda-client.mjs.map +1 -0
- package/dist/telemetry/model-host.cjs +104 -0
- package/dist/telemetry/model-host.cjs.map +1 -0
- package/dist/telemetry/model-host.d.cts +26 -0
- package/dist/telemetry/model-host.d.cts.map +1 -0
- package/dist/telemetry/model-host.d.mts +26 -0
- package/dist/telemetry/model-host.d.mts.map +1 -0
- package/dist/telemetry/model-host.mjs +102 -0
- package/dist/telemetry/model-host.mjs.map +1 -0
- package/dist/telemetry/sampling.cjs +50 -0
- package/dist/telemetry/sampling.cjs.map +1 -0
- package/dist/telemetry/sampling.d.cts +57 -0
- package/dist/telemetry/sampling.d.cts.map +1 -0
- package/dist/telemetry/sampling.d.mts +57 -0
- package/dist/telemetry/sampling.d.mts.map +1 -0
- package/dist/telemetry/sampling.mjs +44 -0
- package/dist/telemetry/sampling.mjs.map +1 -0
- package/dist/telemetry/telemetry-client.cjs +165 -0
- package/dist/telemetry/telemetry-client.cjs.map +1 -0
- package/dist/telemetry/telemetry-client.d.cts +87 -0
- package/dist/telemetry/telemetry-client.d.cts.map +1 -0
- package/dist/telemetry/telemetry-client.d.mts +87 -0
- package/dist/telemetry/telemetry-client.d.mts.map +1 -0
- package/dist/telemetry/telemetry-client.mjs +162 -9
- package/dist/telemetry/telemetry-client.mjs.map +1 -1
- package/dist/telemetry/telemetry-disabled.cjs +20 -0
- package/dist/telemetry/telemetry-disabled.cjs.map +1 -0
- package/dist/telemetry/telemetry-disabled.d.cts +16 -0
- package/dist/telemetry/telemetry-disabled.d.cts.map +1 -0
- package/dist/telemetry/telemetry-disabled.d.mts +16 -0
- package/dist/telemetry/telemetry-disabled.d.mts.map +1 -0
- package/dist/telemetry/telemetry-disabled.mjs +19 -0
- package/dist/telemetry/telemetry-disabled.mjs.map +1 -0
- package/dist/telemetry/utils.cjs +14 -0
- package/dist/telemetry/utils.cjs.map +1 -0
- package/dist/telemetry/utils.mjs +12 -8
- package/dist/telemetry/utils.mjs.map +1 -1
- package/dist/transcription-errors.cjs +84 -0
- package/dist/transcription-errors.cjs.map +1 -0
- package/dist/transcription-errors.d.cts +52 -0
- package/dist/transcription-errors.d.cts.map +1 -0
- package/dist/transcription-errors.d.mts +52 -0
- package/dist/transcription-errors.d.mts.map +1 -0
- package/dist/transcription-errors.mjs +82 -0
- package/dist/transcription-errors.mjs.map +1 -0
- package/dist/types/{action.d.ts → action.d.cts} +32 -32
- package/dist/types/action.d.cts.map +1 -0
- package/dist/types/action.d.mts +59 -0
- package/dist/types/action.d.mts.map +1 -0
- package/dist/types/copilot-cloud-config.d.cts +15 -0
- package/dist/types/copilot-cloud-config.d.cts.map +1 -0
- package/dist/types/copilot-cloud-config.d.mts +15 -0
- package/dist/types/copilot-cloud-config.d.mts.map +1 -0
- package/dist/types/error.d.cts +57 -0
- package/dist/types/error.d.cts.map +1 -0
- package/dist/types/error.d.mts +57 -0
- package/dist/types/error.d.mts.map +1 -0
- package/dist/types/message.d.cts +42 -0
- package/dist/types/message.d.cts.map +1 -0
- package/dist/types/message.d.mts +42 -0
- package/dist/types/message.d.mts.map +1 -0
- package/dist/types/openai-assistant.d.cts +55 -0
- package/dist/types/openai-assistant.d.cts.map +1 -0
- package/dist/types/openai-assistant.d.mts +55 -0
- package/dist/types/openai-assistant.d.mts.map +1 -0
- package/dist/types/utility.d.cts +6 -0
- package/dist/types/utility.d.cts.map +1 -0
- package/dist/types/utility.d.mts +6 -0
- package/dist/types/utility.d.mts.map +1 -0
- package/dist/utils/clipboard.cjs +28 -0
- package/dist/utils/clipboard.cjs.map +1 -0
- package/dist/utils/clipboard.d.cts +14 -0
- package/dist/utils/clipboard.d.cts.map +1 -0
- package/dist/utils/clipboard.d.mts +14 -0
- package/dist/utils/clipboard.d.mts.map +1 -0
- package/dist/utils/clipboard.mjs +27 -0
- package/dist/utils/clipboard.mjs.map +1 -0
- package/dist/utils/conditions.cjs +32 -0
- package/dist/utils/conditions.cjs.map +1 -0
- package/dist/utils/{conditions.d.ts → conditions.d.cts} +16 -11
- package/dist/utils/conditions.d.cts.map +1 -0
- package/dist/utils/conditions.d.mts +31 -0
- package/dist/utils/conditions.d.mts.map +1 -0
- package/dist/utils/conditions.mjs +30 -6
- package/dist/utils/conditions.mjs.map +1 -1
- package/dist/utils/console-styling.cjs +91 -0
- package/dist/utils/console-styling.cjs.map +1 -0
- package/dist/utils/console-styling.d.cts +65 -0
- package/dist/utils/console-styling.d.cts.map +1 -0
- package/dist/utils/console-styling.d.mts +65 -0
- package/dist/utils/console-styling.d.mts.map +1 -0
- package/dist/utils/console-styling.mjs +83 -15
- package/dist/utils/console-styling.mjs.map +1 -1
- package/dist/utils/dashboard-url.cjs +19 -0
- package/dist/utils/dashboard-url.cjs.map +1 -0
- package/dist/utils/dashboard-url.mjs +18 -0
- package/dist/utils/dashboard-url.mjs.map +1 -0
- package/dist/utils/errors.cjs +439 -0
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/{errors.d.ts → errors.d.cts} +188 -156
- package/dist/utils/errors.d.cts.map +1 -0
- package/dist/utils/errors.d.mts +312 -0
- package/dist/utils/errors.d.mts.map +1 -0
- package/dist/utils/errors.mjs +416 -63
- package/dist/utils/errors.mjs.map +1 -1
- package/dist/utils/index.cjs +105 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +58 -0
- package/dist/utils/index.d.cts.map +1 -0
- package/dist/utils/index.d.mts +58 -0
- package/dist/utils/index.d.mts.map +1 -0
- package/dist/utils/index.mjs +97 -107
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-learning.cjs +217 -0
- package/dist/utils/inspector-learning.cjs.map +1 -0
- package/dist/utils/inspector-learning.d.cts +104 -0
- package/dist/utils/inspector-learning.d.cts.map +1 -0
- package/dist/utils/inspector-learning.d.mts +104 -0
- package/dist/utils/inspector-learning.d.mts.map +1 -0
- package/dist/utils/inspector-learning.mjs +215 -0
- package/dist/utils/inspector-learning.mjs.map +1 -0
- package/dist/utils/inspector-metadata.cjs +146 -0
- package/dist/utils/inspector-metadata.cjs.map +1 -0
- package/dist/utils/inspector-metadata.d.cts +55 -0
- package/dist/utils/inspector-metadata.d.cts.map +1 -0
- package/dist/utils/inspector-metadata.d.mts +55 -0
- package/dist/utils/inspector-metadata.d.mts.map +1 -0
- package/dist/utils/inspector-metadata.mjs +146 -0
- package/dist/utils/inspector-metadata.mjs.map +1 -0
- package/dist/utils/inspector-visibility.cjs +13 -0
- package/dist/utils/inspector-visibility.cjs.map +1 -0
- package/dist/utils/inspector-visibility.d.cts +18 -0
- package/dist/utils/inspector-visibility.d.cts.map +1 -0
- package/dist/utils/inspector-visibility.d.mts +18 -0
- package/dist/utils/inspector-visibility.d.mts.map +1 -0
- package/dist/utils/inspector-visibility.mjs +12 -0
- package/dist/utils/inspector-visibility.mjs.map +1 -0
- package/dist/utils/json-schema.cjs +215 -0
- package/dist/utils/json-schema.cjs.map +1 -0
- package/dist/utils/{json-schema.d.ts → json-schema.d.cts} +20 -18
- package/dist/utils/json-schema.d.cts.map +1 -0
- package/dist/utils/json-schema.d.mts +36 -0
- package/dist/utils/json-schema.d.mts.map +1 -0
- package/dist/utils/json-schema.mjs +210 -12
- package/dist/utils/json-schema.mjs.map +1 -1
- package/dist/utils/random-id.cjs +38 -0
- package/dist/utils/random-id.cjs.map +1 -0
- package/dist/utils/{random-id.d.ts → random-id.d.cts} +3 -1
- package/dist/utils/random-id.d.cts.map +1 -0
- package/dist/utils/random-id.d.mts +8 -0
- package/dist/utils/random-id.d.mts.map +1 -0
- package/dist/utils/random-id.mjs +33 -12
- package/dist/utils/random-id.mjs.map +1 -1
- package/dist/utils/requests.cjs +58 -0
- package/dist/utils/requests.cjs.map +1 -0
- package/dist/utils/{requests.d.ts → requests.d.cts} +3 -1
- package/dist/utils/requests.d.cts.map +1 -0
- package/dist/utils/requests.d.mts +12 -0
- package/dist/utils/requests.d.mts.map +1 -0
- package/dist/utils/requests.mjs +56 -6
- package/dist/utils/requests.mjs.map +1 -1
- package/dist/utils/row-render-keys.cjs +206 -0
- package/dist/utils/row-render-keys.cjs.map +1 -0
- package/dist/utils/row-render-keys.d.cts +65 -0
- package/dist/utils/row-render-keys.d.cts.map +1 -0
- package/dist/utils/row-render-keys.d.mts +65 -0
- package/dist/utils/row-render-keys.d.mts.map +1 -0
- package/dist/utils/row-render-keys.mjs +201 -0
- package/dist/utils/row-render-keys.mjs.map +1 -0
- package/dist/utils/types.cjs +9 -0
- package/dist/utils/types.cjs.map +1 -0
- package/dist/utils/types.d.cts +122 -0
- package/dist/utils/types.d.cts.map +1 -0
- package/dist/utils/types.d.mts +122 -0
- package/dist/utils/types.d.mts.map +1 -0
- package/dist/utils/types.mjs +6 -1
- package/dist/utils/types.mjs.map +1 -1
- package/package.json +74 -40
- package/scripts/verify-cjs-exports.cjs +16 -0
- package/src/__tests__/a2ui-envelope.test.ts +434 -0
- package/src/__tests__/a2ui-prompts.test.ts +362 -0
- package/src/__tests__/debug.test.ts +116 -0
- package/src/__tests__/license-context.test.ts +253 -0
- package/src/__tests__/root-entry-browser-safety.test.ts +91 -0
- package/src/__tests__/standard-schema-types.test.ts +144 -0
- package/src/__tests__/standard-schema.test.ts +359 -0
- package/src/__tests__/zod-regression.test.ts +319 -0
- package/src/a2ui-envelope.ts +309 -0
- package/src/a2ui-prompts.ts +322 -0
- package/src/attachments/__tests__/utils.test.ts +214 -0
- package/src/attachments/content.test.ts +48 -0
- package/src/attachments/content.ts +16 -0
- package/src/attachments/index.ts +21 -0
- package/src/attachments/types.ts +77 -0
- package/src/attachments/utils.ts +169 -0
- package/src/constants/index.ts +14 -1
- package/src/debug-event-envelope.ts +9 -0
- package/src/debug.ts +55 -0
- package/src/event-transforms/index.ts +5 -0
- package/src/event-transforms/open-generative-ui-middleware.ts +392 -0
- package/src/event-transforms/recorded-events.test.ts +166 -0
- package/src/event-transforms/recorded-events.ts +74 -0
- package/src/finalize-events.test.ts +284 -0
- package/src/finalize-events.ts +260 -0
- package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
- package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
- package/src/generative-ui/basic-functions.ts +282 -0
- package/src/generative-ui/child-refs.ts +210 -0
- package/src/generative-ui/index.ts +6 -0
- package/src/generative-ui/is-record.ts +4 -0
- package/src/generative-ui/prompt.ts +174 -0
- package/src/generative-ui/render-ui-args.ts +199 -0
- package/src/generative-ui/types.ts +314 -0
- package/src/generative-ui/validate-operation.ts +723 -0
- package/src/index.ts +190 -1
- package/src/intelligence-routing-fields.test.ts +51 -0
- package/src/intelligence-routing-fields.ts +40 -0
- package/src/logger.ts +1 -0
- package/src/open-generative-ui-prompts.ts +20 -0
- package/src/standard-schema.ts +82 -0
- package/src/telemetry/events.ts +37 -10
- package/src/telemetry/index.ts +8 -0
- package/src/telemetry/lambda-client.test.ts +446 -0
- package/src/telemetry/lambda-client.ts +194 -0
- package/src/telemetry/model-host.test.ts +145 -0
- package/src/telemetry/model-host.ts +156 -0
- package/src/telemetry/sampling.test.ts +65 -0
- package/src/telemetry/sampling.ts +103 -0
- package/src/telemetry/telemetry-blank-license-identity.test.ts +128 -0
- package/src/telemetry/telemetry-client.test.ts +798 -0
- package/src/telemetry/telemetry-client.ts +223 -37
- package/src/telemetry/telemetry-disabled.ts +22 -0
- package/src/telemetry/utils.ts +0 -24
- package/src/transcription-errors.ts +99 -0
- package/src/types/__tests__/message.test.ts +62 -0
- package/src/types/action.ts +9 -5
- package/src/types/error.ts +11 -2
- package/src/types/message.ts +33 -5
- package/src/types/openai-assistant.ts +6 -2
- package/src/utils/__tests__/clipboard.test.ts +87 -0
- package/src/utils/__tests__/conditions.test.ts +161 -0
- package/src/utils/__tests__/json-schema.test.ts +812 -0
- package/src/utils/__tests__/row-render-keys.test.ts +388 -0
- package/src/utils/clipboard.ts +23 -0
- package/src/utils/conditions.ts +27 -9
- package/src/utils/console-styling.ts +10 -8
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/errors.ts +55 -22
- package/src/utils/index.ts +70 -1
- package/src/utils/inspector-learning.test.ts +423 -0
- package/src/utils/inspector-learning.ts +523 -0
- package/src/utils/inspector-metadata.test.ts +743 -0
- package/src/utils/inspector-metadata.ts +263 -0
- package/src/utils/inspector-visibility.test.ts +43 -0
- package/src/utils/inspector-visibility.ts +17 -0
- package/src/utils/json-schema.ts +156 -12
- package/src/utils/random-id.ts +5 -2
- package/src/utils/requests.ts +3 -1
- package/src/utils/row-render-keys.ts +263 -0
- package/src/utils/types.ts +128 -1
- package/src/utils.test.ts +137 -0
- package/tsconfig.json +10 -2
- package/tsdown.config.ts +49 -0
- package/vitest.config.mjs +11 -0
- package/CHANGELOG.md +0 -1817
- package/dist/chunk-2OJ5OJ2D.mjs +0 -1
- package/dist/chunk-2OJ5OJ2D.mjs.map +0 -1
- package/dist/chunk-64DYJ27R.mjs +0 -201
- package/dist/chunk-64DYJ27R.mjs.map +0 -1
- package/dist/chunk-6QGXWNS5.mjs +0 -34
- package/dist/chunk-6QGXWNS5.mjs.map +0 -1
- package/dist/chunk-6W2UEO2M.mjs +0 -94
- package/dist/chunk-6W2UEO2M.mjs.map +0 -1
- package/dist/chunk-7DJDTRNX.mjs +0 -426
- package/dist/chunk-7DJDTRNX.mjs.map +0 -1
- package/dist/chunk-A66772CV.mjs +0 -107
- package/dist/chunk-A66772CV.mjs.map +0 -1
- package/dist/chunk-BOKE4OKY.mjs +0 -41
- package/dist/chunk-BOKE4OKY.mjs.map +0 -1
- package/dist/chunk-FCCOSO5L.mjs +0 -1
- package/dist/chunk-FCCOSO5L.mjs.map +0 -1
- package/dist/chunk-GYZIHHE6.mjs +0 -13
- package/dist/chunk-GYZIHHE6.mjs.map +0 -1
- package/dist/chunk-IAFBVORQ.mjs +0 -1
- package/dist/chunk-IAFBVORQ.mjs.map +0 -1
- package/dist/chunk-JAAUCDUR.mjs +0 -38
- package/dist/chunk-JAAUCDUR.mjs.map +0 -1
- package/dist/chunk-MSUB6DGR.mjs +0 -1
- package/dist/chunk-MSUB6DGR.mjs.map +0 -1
- package/dist/chunk-N5EP5OD5.mjs +0 -1
- package/dist/chunk-N5EP5OD5.mjs.map +0 -1
- package/dist/chunk-NAFEBKSO.mjs +0 -1
- package/dist/chunk-NAFEBKSO.mjs.map +0 -1
- package/dist/chunk-P7STFMPO.mjs +0 -1
- package/dist/chunk-P7STFMPO.mjs.map +0 -1
- package/dist/chunk-PL5WNHFZ.mjs +0 -50
- package/dist/chunk-PL5WNHFZ.mjs.map +0 -1
- package/dist/chunk-UYARLQNA.mjs +0 -1
- package/dist/chunk-UYARLQNA.mjs.map +0 -1
- package/dist/chunk-XEMZTHQZ.mjs +0 -67
- package/dist/chunk-XEMZTHQZ.mjs.map +0 -1
- package/dist/chunk-XTHC46M2.mjs +0 -1
- package/dist/chunk-XTHC46M2.mjs.map +0 -1
- package/dist/constants/index.d.ts +0 -6
- package/dist/constants/index.js +0 -40
- package/dist/constants/index.js.map +0 -1
- package/dist/index.d.ts +0 -25
- package/dist/index.js +0 -1098
- package/dist/index.js.map +0 -1
- package/dist/telemetry/events.d.ts +0 -45
- package/dist/telemetry/events.js +0 -19
- package/dist/telemetry/events.js.map +0 -1
- package/dist/telemetry/events.mjs +0 -1
- package/dist/telemetry/events.mjs.map +0 -1
- package/dist/telemetry/index.d.ts +0 -3
- package/dist/telemetry/index.js +0 -188
- package/dist/telemetry/index.js.map +0 -1
- package/dist/telemetry/index.mjs.map +0 -1
- package/dist/telemetry/scarf-client.d.ts +0 -7
- package/dist/telemetry/scarf-client.js +0 -59
- package/dist/telemetry/scarf-client.js.map +0 -1
- package/dist/telemetry/scarf-client.mjs +0 -7
- package/dist/telemetry/scarf-client.mjs.map +0 -1
- package/dist/telemetry/security-check.d.ts +0 -2
- package/dist/telemetry/security-check.js +0 -2
- package/dist/telemetry/security-check.js.map +0 -1
- package/dist/telemetry/security-check.mjs +0 -1
- package/dist/telemetry/security-check.mjs.map +0 -1
- package/dist/telemetry/telemetry-client.d.ts +0 -40
- package/dist/telemetry/telemetry-client.js +0 -186
- package/dist/telemetry/telemetry-client.js.map +0 -1
- package/dist/telemetry/utils.d.ts +0 -8
- package/dist/telemetry/utils.js +0 -69
- package/dist/telemetry/utils.js.map +0 -1
- package/dist/types/action.js +0 -19
- package/dist/types/action.js.map +0 -1
- package/dist/types/action.mjs +0 -2
- package/dist/types/action.mjs.map +0 -1
- package/dist/types/copilot-cloud-config.d.ts +0 -13
- package/dist/types/copilot-cloud-config.js +0 -19
- package/dist/types/copilot-cloud-config.js.map +0 -1
- package/dist/types/copilot-cloud-config.mjs +0 -2
- package/dist/types/copilot-cloud-config.mjs.map +0 -1
- package/dist/types/error.d.ts +0 -55
- package/dist/types/error.js +0 -19
- package/dist/types/error.js.map +0 -1
- package/dist/types/error.mjs +0 -2
- package/dist/types/error.mjs.map +0 -1
- package/dist/types/index.d.ts +0 -7
- package/dist/types/index.js +0 -19
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -8
- package/dist/types/index.mjs.map +0 -1
- package/dist/types/message.d.ts +0 -26
- package/dist/types/message.js +0 -19
- package/dist/types/message.js.map +0 -1
- package/dist/types/message.mjs +0 -2
- package/dist/types/message.mjs.map +0 -1
- package/dist/types/openai-assistant.d.ts +0 -53
- package/dist/types/openai-assistant.js +0 -19
- package/dist/types/openai-assistant.js.map +0 -1
- package/dist/types/openai-assistant.mjs +0 -2
- package/dist/types/openai-assistant.mjs.map +0 -1
- package/dist/types/utility.d.ts +0 -6
- package/dist/types/utility.js +0 -19
- package/dist/types/utility.js.map +0 -1
- package/dist/types/utility.mjs +0 -2
- package/dist/types/utility.mjs.map +0 -1
- package/dist/utils/conditions.js +0 -74
- package/dist/utils/conditions.js.map +0 -1
- package/dist/utils/console-styling.d.ts +0 -84
- package/dist/utils/console-styling.js +0 -123
- package/dist/utils/console-styling.js.map +0 -1
- package/dist/utils/errors.js +0 -443
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/index.d.ts +0 -32
- package/dist/utils/index.js +0 -927
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/json-schema.js +0 -228
- package/dist/utils/json-schema.js.map +0 -1
- package/dist/utils/random-id.js +0 -68
- package/dist/utils/random-id.js.map +0 -1
- package/dist/utils/requests.js +0 -91
- package/dist/utils/requests.js.map +0 -1
- package/dist/utils/types.d.ts +0 -20
- package/dist/utils/types.js +0 -19
- package/dist/utils/types.js.map +0 -1
- package/jest.config.js +0 -5
- package/src/telemetry/scarf-client.ts +0 -39
- package/src/telemetry/security-check.ts +0 -0
- package/src/utils/json-schema.test.ts +0 -383
- package/tsup.config.ts +0 -11
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
const require_is_record = require('./is-record.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/child-refs.ts
|
|
4
|
+
/**
|
|
5
|
+
* How A2UI 1.0 marks a prop that holds a component reference: the JSON
|
|
6
|
+
* Schema of the prop is a `$ref` to one of these two common types.
|
|
7
|
+
* Validators and renderers find the children of a component by these refs,
|
|
8
|
+
* at any depth of the props schema.
|
|
9
|
+
*/
|
|
10
|
+
const A2UI_COMPONENT_ID_REF = "common_types.json#/$defs/ComponentId";
|
|
11
|
+
const A2UI_CHILD_LIST_REF = "common_types.json#/$defs/ChildList";
|
|
12
|
+
/**
|
|
13
|
+
* The start of the `description` that marks a schema as a component
|
|
14
|
+
* reference before it is turned into JSON Schema. `child()` and
|
|
15
|
+
* `childList()` set it; a schema from another library can set a description
|
|
16
|
+
* that starts with the same mark to get the same effect. What follows the
|
|
17
|
+
* mark (after a space) stays as the description of the reference.
|
|
18
|
+
*/
|
|
19
|
+
const A2UI_COMPONENT_ID_MARK = "@a2ui/ComponentId";
|
|
20
|
+
const A2UI_CHILD_LIST_MARK = "@a2ui/ChildList";
|
|
21
|
+
/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */
|
|
22
|
+
function childRefKind(schema) {
|
|
23
|
+
const ref = schema.$ref;
|
|
24
|
+
if (ref === A2UI_COMPONENT_ID_REF) return "single";
|
|
25
|
+
if (ref === A2UI_CHILD_LIST_REF) return "multiple";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of
|
|
29
|
+
* the marked node. The mark is the `description` set by `child()` and
|
|
30
|
+
* `childList()`; after this, the node is `{ "$ref": "common_types.json#/..." }`.
|
|
31
|
+
*/
|
|
32
|
+
function applyChildRefMarks(schema) {
|
|
33
|
+
const marked = (description, mark, ref) => {
|
|
34
|
+
if (typeof description !== "string" || !description.startsWith(mark)) return;
|
|
35
|
+
const rest = description.slice(mark.length).trim();
|
|
36
|
+
return rest === "" ? { $ref: ref } : {
|
|
37
|
+
$ref: ref,
|
|
38
|
+
description: rest
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const visit = (node) => {
|
|
42
|
+
if (Array.isArray(node)) return node.map(visit);
|
|
43
|
+
if (!require_is_record.isRecord(node)) return node;
|
|
44
|
+
const reference = marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ?? marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);
|
|
45
|
+
if (reference) return reference;
|
|
46
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, visit(value)]));
|
|
47
|
+
};
|
|
48
|
+
return visit(schema);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every component reference in a props schema, with its path. Walks
|
|
52
|
+
* `properties`, array `items`, and the variants of `anyOf` / `oneOf` /
|
|
53
|
+
* `allOf`. A union counts as a reference only when every alternative is
|
|
54
|
+
* one of the same kind: a prop that is text in one branch and a child in
|
|
55
|
+
* another cannot be told apart from its value, so it stays data.
|
|
56
|
+
*/
|
|
57
|
+
function collectChildRefs(schema) {
|
|
58
|
+
const refs = [];
|
|
59
|
+
const seen = /* @__PURE__ */ new Set();
|
|
60
|
+
const add = (path, kind) => {
|
|
61
|
+
const key = path.join("\0");
|
|
62
|
+
if (seen.has(key)) return;
|
|
63
|
+
seen.add(key);
|
|
64
|
+
refs.push({
|
|
65
|
+
path,
|
|
66
|
+
kind
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
const visit = (node, path) => {
|
|
70
|
+
if (!require_is_record.isRecord(node)) return;
|
|
71
|
+
const kind = childRefKind(node);
|
|
72
|
+
if (kind) return add(path, kind);
|
|
73
|
+
for (const list of [node.anyOf, node.oneOf]) {
|
|
74
|
+
if (!Array.isArray(list) || list.length === 0) continue;
|
|
75
|
+
const kinds = list.map((variant) => require_is_record.isRecord(variant) ? childRefKind(variant) : void 0);
|
|
76
|
+
const [first] = kinds;
|
|
77
|
+
if (first && kinds.every((k) => k === first)) add(path, first);
|
|
78
|
+
else if (kinds.every((k) => k === void 0)) {
|
|
79
|
+
const perVariant = list.map((variant) => collectChildRefs(variant));
|
|
80
|
+
const key = (ref) => `${ref.kind}\u0000${ref.path.join("\0")}`;
|
|
81
|
+
perVariant[0].filter((ref) => perVariant.every((refs) => refs.some((r) => key(r) === key(ref)))).forEach((ref) => add([...path, ...ref.path], ref.kind));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(node.allOf)) node.allOf.forEach((variant) => visit(variant, path));
|
|
85
|
+
if (require_is_record.isRecord(node.properties)) for (const [key, value] of Object.entries(node.properties)) visit(value, [...path, key]);
|
|
86
|
+
if (require_is_record.isRecord(node.items)) visit(node.items, [...path, "*"]);
|
|
87
|
+
};
|
|
88
|
+
visit(schema, []);
|
|
89
|
+
return refs;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The component ids at the references of one component node: a string id,
|
|
93
|
+
* a list of ids, or the `componentId` of a template, at every ref path.
|
|
94
|
+
* Bound or computed values are not ids and are left out.
|
|
95
|
+
*/
|
|
96
|
+
function childIdsAtRefs(component, refs) {
|
|
97
|
+
const ids = [];
|
|
98
|
+
const collect = (value) => {
|
|
99
|
+
if (typeof value === "string") ids.push(value);
|
|
100
|
+
else if (Array.isArray(value)) value.forEach((id) => typeof id === "string" && ids.push(id));
|
|
101
|
+
else if (require_is_record.isRecord(value) && typeof value.componentId === "string") ids.push(value.componentId);
|
|
102
|
+
};
|
|
103
|
+
const visit = (value, path) => {
|
|
104
|
+
if (path.length === 0) return collect(value);
|
|
105
|
+
const [head, ...rest] = path;
|
|
106
|
+
if (head === "*") {
|
|
107
|
+
if (Array.isArray(value)) value.forEach((item) => visit(item, rest));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (require_is_record.isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) visit(value[head], rest);
|
|
111
|
+
};
|
|
112
|
+
for (const ref of refs) visit(component, ref.path);
|
|
113
|
+
return ids;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* True when a reference of the component is bound or computed (`{"@path"}`,
|
|
117
|
+
* `{"@call"}`) at the ref itself or above it, so its ids are known only
|
|
118
|
+
* at render time.
|
|
119
|
+
*/
|
|
120
|
+
function hasBoundChildRef(component, refs) {
|
|
121
|
+
const isDynamic = (value) => require_is_record.isRecord(value) && ("@path" in value || "@call" in value);
|
|
122
|
+
const visit = (value, path) => {
|
|
123
|
+
if (isDynamic(value)) return true;
|
|
124
|
+
if (path.length === 0) return false;
|
|
125
|
+
const [head, ...rest] = path;
|
|
126
|
+
if (head === "*") return Array.isArray(value) && value.some((item) => visit(item, rest));
|
|
127
|
+
return require_is_record.isRecord(value) && Object.prototype.hasOwnProperty.call(value, head) && visit(value[head], rest);
|
|
128
|
+
};
|
|
129
|
+
return refs.some((ref) => visit(component, ref.path));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Walks `value` along a ref path and replaces what sits at the end with
|
|
133
|
+
* `replace(found)`. Objects and arrays on the way are copied; everything
|
|
134
|
+
* else keeps its identity. Missing keys are left alone.
|
|
135
|
+
*/
|
|
136
|
+
function mapAtChildRef(value, path, replace) {
|
|
137
|
+
if (path.length === 0) return replace(value);
|
|
138
|
+
const [head, ...rest] = path;
|
|
139
|
+
if (head === "*") {
|
|
140
|
+
if (!Array.isArray(value)) return value;
|
|
141
|
+
return value.map((item) => mapAtChildRef(item, rest, replace));
|
|
142
|
+
}
|
|
143
|
+
if (!require_is_record.isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) return value;
|
|
144
|
+
return {
|
|
145
|
+
...value,
|
|
146
|
+
[head]: mapAtChildRef(value[head], rest, replace)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
//#endregion
|
|
151
|
+
exports.A2UI_CHILD_LIST_MARK = A2UI_CHILD_LIST_MARK;
|
|
152
|
+
exports.A2UI_CHILD_LIST_REF = A2UI_CHILD_LIST_REF;
|
|
153
|
+
exports.A2UI_COMPONENT_ID_MARK = A2UI_COMPONENT_ID_MARK;
|
|
154
|
+
exports.A2UI_COMPONENT_ID_REF = A2UI_COMPONENT_ID_REF;
|
|
155
|
+
exports.applyChildRefMarks = applyChildRefMarks;
|
|
156
|
+
exports.childIdsAtRefs = childIdsAtRefs;
|
|
157
|
+
exports.childRefKind = childRefKind;
|
|
158
|
+
exports.collectChildRefs = collectChildRefs;
|
|
159
|
+
exports.hasBoundChildRef = hasBoundChildRef;
|
|
160
|
+
exports.mapAtChildRef = mapAtChildRef;
|
|
161
|
+
//# sourceMappingURL=child-refs.cjs.map
|
|
@@ -0,0 +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;CAC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E,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"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GenerativeUIJSONSchema } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/child-refs.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* How A2UI 1.0 marks a prop that holds a component reference: the JSON
|
|
6
|
+
* Schema of the prop is a `$ref` to one of these two common types.
|
|
7
|
+
* Validators and renderers find the children of a component by these refs,
|
|
8
|
+
* at any depth of the props schema.
|
|
9
|
+
*/
|
|
10
|
+
declare const A2UI_COMPONENT_ID_REF = "common_types.json#/$defs/ComponentId";
|
|
11
|
+
declare const A2UI_CHILD_LIST_REF = "common_types.json#/$defs/ChildList";
|
|
12
|
+
/**
|
|
13
|
+
* The start of the `description` that marks a schema as a component
|
|
14
|
+
* reference before it is turned into JSON Schema. `child()` and
|
|
15
|
+
* `childList()` set it; a schema from another library can set a description
|
|
16
|
+
* that starts with the same mark to get the same effect. What follows the
|
|
17
|
+
* mark (after a space) stays as the description of the reference.
|
|
18
|
+
*/
|
|
19
|
+
declare const A2UI_COMPONENT_ID_MARK = "@a2ui/ComponentId";
|
|
20
|
+
declare const A2UI_CHILD_LIST_MARK = "@a2ui/ChildList";
|
|
21
|
+
type ChildRefKind = "single" | "multiple";
|
|
22
|
+
/** One component reference in a props schema. */
|
|
23
|
+
interface ChildRef {
|
|
24
|
+
/**
|
|
25
|
+
* The keys from the props root to the reference. `"*"` stands for the
|
|
26
|
+
* items of an array. `["tabs", "*", "child"]` is the `child` of each
|
|
27
|
+
* entry of `tabs`.
|
|
28
|
+
*/
|
|
29
|
+
path: readonly string[];
|
|
30
|
+
kind: ChildRefKind;
|
|
31
|
+
}
|
|
32
|
+
/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */
|
|
33
|
+
declare function childRefKind(schema: GenerativeUIJSONSchema): ChildRefKind | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of
|
|
36
|
+
* the marked node. The mark is the `description` set by `child()` and
|
|
37
|
+
* `childList()`; after this, the node is `{ "$ref": "common_types.json#/..." }`.
|
|
38
|
+
*/
|
|
39
|
+
declare function applyChildRefMarks(schema: GenerativeUIJSONSchema): GenerativeUIJSONSchema;
|
|
40
|
+
/**
|
|
41
|
+
* Every component reference in a props schema, with its path. Walks
|
|
42
|
+
* `properties`, array `items`, and the variants of `anyOf` / `oneOf` /
|
|
43
|
+
* `allOf`. A union counts as a reference only when every alternative is
|
|
44
|
+
* one of the same kind: a prop that is text in one branch and a child in
|
|
45
|
+
* another cannot be told apart from its value, so it stays data.
|
|
46
|
+
*/
|
|
47
|
+
declare function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[];
|
|
48
|
+
/**
|
|
49
|
+
* The component ids at the references of one component node: a string id,
|
|
50
|
+
* a list of ids, or the `componentId` of a template, at every ref path.
|
|
51
|
+
* Bound or computed values are not ids and are left out.
|
|
52
|
+
*/
|
|
53
|
+
declare function childIdsAtRefs(component: Record<string, unknown>, refs: readonly ChildRef[]): string[];
|
|
54
|
+
/**
|
|
55
|
+
* True when a reference of the component is bound or computed (`{"@path"}`,
|
|
56
|
+
* `{"@call"}`) at the ref itself or above it, so its ids are known only
|
|
57
|
+
* at render time.
|
|
58
|
+
*/
|
|
59
|
+
declare function hasBoundChildRef(component: Record<string, unknown>, refs: readonly ChildRef[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Walks `value` along a ref path and replaces what sits at the end with
|
|
62
|
+
* `replace(found)`. Objects and arrays on the way are copied; everything
|
|
63
|
+
* else keeps its identity. Missing keys are left alone.
|
|
64
|
+
*/
|
|
65
|
+
declare function mapAtChildRef(value: unknown, path: readonly string[], replace: (found: unknown) => unknown): unknown;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF, ChildRef, ChildRefKind, applyChildRefMarks, childIdsAtRefs, childRefKind, collectChildRefs, hasBoundChildRef, mapAtChildRef };
|
|
68
|
+
//# sourceMappingURL=child-refs.d.cts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GenerativeUIJSONSchema } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/child-refs.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* How A2UI 1.0 marks a prop that holds a component reference: the JSON
|
|
6
|
+
* Schema of the prop is a `$ref` to one of these two common types.
|
|
7
|
+
* Validators and renderers find the children of a component by these refs,
|
|
8
|
+
* at any depth of the props schema.
|
|
9
|
+
*/
|
|
10
|
+
declare const A2UI_COMPONENT_ID_REF = "common_types.json#/$defs/ComponentId";
|
|
11
|
+
declare const A2UI_CHILD_LIST_REF = "common_types.json#/$defs/ChildList";
|
|
12
|
+
/**
|
|
13
|
+
* The start of the `description` that marks a schema as a component
|
|
14
|
+
* reference before it is turned into JSON Schema. `child()` and
|
|
15
|
+
* `childList()` set it; a schema from another library can set a description
|
|
16
|
+
* that starts with the same mark to get the same effect. What follows the
|
|
17
|
+
* mark (after a space) stays as the description of the reference.
|
|
18
|
+
*/
|
|
19
|
+
declare const A2UI_COMPONENT_ID_MARK = "@a2ui/ComponentId";
|
|
20
|
+
declare const A2UI_CHILD_LIST_MARK = "@a2ui/ChildList";
|
|
21
|
+
type ChildRefKind = "single" | "multiple";
|
|
22
|
+
/** One component reference in a props schema. */
|
|
23
|
+
interface ChildRef {
|
|
24
|
+
/**
|
|
25
|
+
* The keys from the props root to the reference. `"*"` stands for the
|
|
26
|
+
* items of an array. `["tabs", "*", "child"]` is the `child` of each
|
|
27
|
+
* entry of `tabs`.
|
|
28
|
+
*/
|
|
29
|
+
path: readonly string[];
|
|
30
|
+
kind: ChildRefKind;
|
|
31
|
+
}
|
|
32
|
+
/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */
|
|
33
|
+
declare function childRefKind(schema: GenerativeUIJSONSchema): ChildRefKind | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of
|
|
36
|
+
* the marked node. The mark is the `description` set by `child()` and
|
|
37
|
+
* `childList()`; after this, the node is `{ "$ref": "common_types.json#/..." }`.
|
|
38
|
+
*/
|
|
39
|
+
declare function applyChildRefMarks(schema: GenerativeUIJSONSchema): GenerativeUIJSONSchema;
|
|
40
|
+
/**
|
|
41
|
+
* Every component reference in a props schema, with its path. Walks
|
|
42
|
+
* `properties`, array `items`, and the variants of `anyOf` / `oneOf` /
|
|
43
|
+
* `allOf`. A union counts as a reference only when every alternative is
|
|
44
|
+
* one of the same kind: a prop that is text in one branch and a child in
|
|
45
|
+
* another cannot be told apart from its value, so it stays data.
|
|
46
|
+
*/
|
|
47
|
+
declare function collectChildRefs(schema: GenerativeUIJSONSchema): ChildRef[];
|
|
48
|
+
/**
|
|
49
|
+
* The component ids at the references of one component node: a string id,
|
|
50
|
+
* a list of ids, or the `componentId` of a template, at every ref path.
|
|
51
|
+
* Bound or computed values are not ids and are left out.
|
|
52
|
+
*/
|
|
53
|
+
declare function childIdsAtRefs(component: Record<string, unknown>, refs: readonly ChildRef[]): string[];
|
|
54
|
+
/**
|
|
55
|
+
* True when a reference of the component is bound or computed (`{"@path"}`,
|
|
56
|
+
* `{"@call"}`) at the ref itself or above it, so its ids are known only
|
|
57
|
+
* at render time.
|
|
58
|
+
*/
|
|
59
|
+
declare function hasBoundChildRef(component: Record<string, unknown>, refs: readonly ChildRef[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Walks `value` along a ref path and replaces what sits at the end with
|
|
62
|
+
* `replace(found)`. Objects and arrays on the way are copied; everything
|
|
63
|
+
* else keeps its identity. Missing keys are left alone.
|
|
64
|
+
*/
|
|
65
|
+
declare function mapAtChildRef(value: unknown, path: readonly string[], replace: (found: unknown) => unknown): unknown;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF, ChildRef, ChildRefKind, applyChildRefMarks, childIdsAtRefs, childRefKind, collectChildRefs, hasBoundChildRef, mapAtChildRef };
|
|
68
|
+
//# sourceMappingURL=child-refs.d.mts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { isRecord } from "./is-record.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/child-refs.ts
|
|
4
|
+
/**
|
|
5
|
+
* How A2UI 1.0 marks a prop that holds a component reference: the JSON
|
|
6
|
+
* Schema of the prop is a `$ref` to one of these two common types.
|
|
7
|
+
* Validators and renderers find the children of a component by these refs,
|
|
8
|
+
* at any depth of the props schema.
|
|
9
|
+
*/
|
|
10
|
+
const A2UI_COMPONENT_ID_REF = "common_types.json#/$defs/ComponentId";
|
|
11
|
+
const A2UI_CHILD_LIST_REF = "common_types.json#/$defs/ChildList";
|
|
12
|
+
/**
|
|
13
|
+
* The start of the `description` that marks a schema as a component
|
|
14
|
+
* reference before it is turned into JSON Schema. `child()` and
|
|
15
|
+
* `childList()` set it; a schema from another library can set a description
|
|
16
|
+
* that starts with the same mark to get the same effect. What follows the
|
|
17
|
+
* mark (after a space) stays as the description of the reference.
|
|
18
|
+
*/
|
|
19
|
+
const A2UI_COMPONENT_ID_MARK = "@a2ui/ComponentId";
|
|
20
|
+
const A2UI_CHILD_LIST_MARK = "@a2ui/ChildList";
|
|
21
|
+
/** The ref kind of a JSON Schema node, or `undefined` for a plain value. */
|
|
22
|
+
function childRefKind(schema) {
|
|
23
|
+
const ref = schema.$ref;
|
|
24
|
+
if (ref === A2UI_COMPONENT_ID_REF) return "single";
|
|
25
|
+
if (ref === A2UI_CHILD_LIST_REF) return "multiple";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns the reference marks of a JSON Schema into A2UI `$ref`s, in place of
|
|
29
|
+
* the marked node. The mark is the `description` set by `child()` and
|
|
30
|
+
* `childList()`; after this, the node is `{ "$ref": "common_types.json#/..." }`.
|
|
31
|
+
*/
|
|
32
|
+
function applyChildRefMarks(schema) {
|
|
33
|
+
const marked = (description, mark, ref) => {
|
|
34
|
+
if (typeof description !== "string" || !description.startsWith(mark)) return;
|
|
35
|
+
const rest = description.slice(mark.length).trim();
|
|
36
|
+
return rest === "" ? { $ref: ref } : {
|
|
37
|
+
$ref: ref,
|
|
38
|
+
description: rest
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const visit = (node) => {
|
|
42
|
+
if (Array.isArray(node)) return node.map(visit);
|
|
43
|
+
if (!isRecord(node)) return node;
|
|
44
|
+
const reference = marked(node.description, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF) ?? marked(node.description, A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF);
|
|
45
|
+
if (reference) return reference;
|
|
46
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, visit(value)]));
|
|
47
|
+
};
|
|
48
|
+
return visit(schema);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every component reference in a props schema, with its path. Walks
|
|
52
|
+
* `properties`, array `items`, and the variants of `anyOf` / `oneOf` /
|
|
53
|
+
* `allOf`. A union counts as a reference only when every alternative is
|
|
54
|
+
* one of the same kind: a prop that is text in one branch and a child in
|
|
55
|
+
* another cannot be told apart from its value, so it stays data.
|
|
56
|
+
*/
|
|
57
|
+
function collectChildRefs(schema) {
|
|
58
|
+
const refs = [];
|
|
59
|
+
const seen = /* @__PURE__ */ new Set();
|
|
60
|
+
const add = (path, kind) => {
|
|
61
|
+
const key = path.join("\0");
|
|
62
|
+
if (seen.has(key)) return;
|
|
63
|
+
seen.add(key);
|
|
64
|
+
refs.push({
|
|
65
|
+
path,
|
|
66
|
+
kind
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
const visit = (node, path) => {
|
|
70
|
+
if (!isRecord(node)) return;
|
|
71
|
+
const kind = childRefKind(node);
|
|
72
|
+
if (kind) return add(path, kind);
|
|
73
|
+
for (const list of [node.anyOf, node.oneOf]) {
|
|
74
|
+
if (!Array.isArray(list) || list.length === 0) continue;
|
|
75
|
+
const kinds = list.map((variant) => isRecord(variant) ? childRefKind(variant) : void 0);
|
|
76
|
+
const [first] = kinds;
|
|
77
|
+
if (first && kinds.every((k) => k === first)) add(path, first);
|
|
78
|
+
else if (kinds.every((k) => k === void 0)) {
|
|
79
|
+
const perVariant = list.map((variant) => collectChildRefs(variant));
|
|
80
|
+
const key = (ref) => `${ref.kind}\u0000${ref.path.join("\0")}`;
|
|
81
|
+
perVariant[0].filter((ref) => perVariant.every((refs) => refs.some((r) => key(r) === key(ref)))).forEach((ref) => add([...path, ...ref.path], ref.kind));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(node.allOf)) node.allOf.forEach((variant) => visit(variant, path));
|
|
85
|
+
if (isRecord(node.properties)) for (const [key, value] of Object.entries(node.properties)) visit(value, [...path, key]);
|
|
86
|
+
if (isRecord(node.items)) visit(node.items, [...path, "*"]);
|
|
87
|
+
};
|
|
88
|
+
visit(schema, []);
|
|
89
|
+
return refs;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The component ids at the references of one component node: a string id,
|
|
93
|
+
* a list of ids, or the `componentId` of a template, at every ref path.
|
|
94
|
+
* Bound or computed values are not ids and are left out.
|
|
95
|
+
*/
|
|
96
|
+
function childIdsAtRefs(component, refs) {
|
|
97
|
+
const ids = [];
|
|
98
|
+
const collect = (value) => {
|
|
99
|
+
if (typeof value === "string") ids.push(value);
|
|
100
|
+
else if (Array.isArray(value)) value.forEach((id) => typeof id === "string" && ids.push(id));
|
|
101
|
+
else if (isRecord(value) && typeof value.componentId === "string") ids.push(value.componentId);
|
|
102
|
+
};
|
|
103
|
+
const visit = (value, path) => {
|
|
104
|
+
if (path.length === 0) return collect(value);
|
|
105
|
+
const [head, ...rest] = path;
|
|
106
|
+
if (head === "*") {
|
|
107
|
+
if (Array.isArray(value)) value.forEach((item) => visit(item, rest));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (isRecord(value) && Object.prototype.hasOwnProperty.call(value, head)) visit(value[head], rest);
|
|
111
|
+
};
|
|
112
|
+
for (const ref of refs) visit(component, ref.path);
|
|
113
|
+
return ids;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* True when a reference of the component is bound or computed (`{"@path"}`,
|
|
117
|
+
* `{"@call"}`) at the ref itself or above it, so its ids are known only
|
|
118
|
+
* at render time.
|
|
119
|
+
*/
|
|
120
|
+
function hasBoundChildRef(component, refs) {
|
|
121
|
+
const isDynamic = (value) => isRecord(value) && ("@path" in value || "@call" in value);
|
|
122
|
+
const visit = (value, path) => {
|
|
123
|
+
if (isDynamic(value)) return true;
|
|
124
|
+
if (path.length === 0) return false;
|
|
125
|
+
const [head, ...rest] = path;
|
|
126
|
+
if (head === "*") return Array.isArray(value) && value.some((item) => visit(item, rest));
|
|
127
|
+
return isRecord(value) && Object.prototype.hasOwnProperty.call(value, head) && visit(value[head], rest);
|
|
128
|
+
};
|
|
129
|
+
return refs.some((ref) => visit(component, ref.path));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Walks `value` along a ref path and replaces what sits at the end with
|
|
133
|
+
* `replace(found)`. Objects and arrays on the way are copied; everything
|
|
134
|
+
* else keeps its identity. Missing keys are left alone.
|
|
135
|
+
*/
|
|
136
|
+
function mapAtChildRef(value, path, replace) {
|
|
137
|
+
if (path.length === 0) return replace(value);
|
|
138
|
+
const [head, ...rest] = path;
|
|
139
|
+
if (head === "*") {
|
|
140
|
+
if (!Array.isArray(value)) return value;
|
|
141
|
+
return value.map((item) => mapAtChildRef(item, rest, replace));
|
|
142
|
+
}
|
|
143
|
+
if (!isRecord(value) || !Object.prototype.hasOwnProperty.call(value, head)) return value;
|
|
144
|
+
return {
|
|
145
|
+
...value,
|
|
146
|
+
[head]: mapAtChildRef(value[head], rest, replace)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
//#endregion
|
|
151
|
+
export { A2UI_CHILD_LIST_MARK, A2UI_CHILD_LIST_REF, A2UI_COMPONENT_ID_MARK, A2UI_COMPONENT_ID_REF, applyChildRefMarks, childIdsAtRefs, childRefKind, collectChildRefs, hasBoundChildRef, mapAtChildRef };
|
|
152
|
+
//# sourceMappingURL=child-refs.mjs.map
|
|
@@ -0,0 +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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generative-ui/is-record.ts
|
|
3
|
+
/** True for a plain object: not null, not an array. Internal, not exported. */
|
|
4
|
+
function isRecord(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
exports.isRecord = isRecord;
|
|
10
|
+
//# sourceMappingURL=is-record.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-record.cjs","names":[],"sources":["../../src/generative-ui/is-record.ts"],"sourcesContent":["/** True for a plain object: not null, not an array. Internal, not exported. */\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"],"mappings":";;;AACA,SAAgB,SAAS,OAAkD;AACzE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/generative-ui/is-record.ts
|
|
2
|
+
/** True for a plain object: not null, not an array. Internal, not exported. */
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
export { isRecord };
|
|
9
|
+
//# sourceMappingURL=is-record.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-record.mjs","names":[],"sources":["../../src/generative-ui/is-record.ts"],"sourcesContent":["/** True for a plain object: not null, not an array. Internal, not exported. */\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"],"mappings":";;AACA,SAAgB,SAAS,OAAkD;AACzE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const require_basic_functions = require('./basic-functions.cjs');
|
|
2
|
+
const require_types = require('./types.cjs');
|
|
3
|
+
const require_a2ui_prompts = require('../a2ui-prompts.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/generative-ui/prompt.ts
|
|
6
|
+
const GENERATIVE_UI_CONTEXT_DESCRIPTION = "Generative UI guide: available components, functions, targets, and how to call render_ui.";
|
|
7
|
+
/** Most characters of the user values of one surface in the guide. */
|
|
8
|
+
const MAX_USER_VALUES_LENGTH = 500;
|
|
9
|
+
/** Cuts long text and marks the cut with "...". */
|
|
10
|
+
function shorten(text) {
|
|
11
|
+
return text.length > MAX_USER_VALUES_LENGTH ? `${text.slice(0, MAX_USER_VALUES_LENGTH)}...` : text;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The guide that the runtime adds as a context entry to a tool-transport run:
|
|
15
|
+
* the `render_ui` rules, the targets, components, functions, and surfaces of
|
|
16
|
+
* the manifest. It is in shared so that the inspector shows the same text.
|
|
17
|
+
*/
|
|
18
|
+
function buildGenerativeUIPrompt(manifest) {
|
|
19
|
+
const targets = manifest.targets.map((t) => {
|
|
20
|
+
const place = t.kind === "chat" ? "inside the chat transcript" : "a named area of the app";
|
|
21
|
+
const limit = t.components ? `; only ${t.components.join(", ") || "no components"}` : "";
|
|
22
|
+
const description = t.description ? `: ${t.description}` : "";
|
|
23
|
+
return `- "${t.id}" (${place}${limit})${description}`;
|
|
24
|
+
}).join("\n");
|
|
25
|
+
const components = Object.entries(manifest.components).map(([name, c]) => {
|
|
26
|
+
const lines = [`- **${name}**${c.description ? `: ${c.description}` : ""}`, ` props: ${JSON.stringify(c.props)}`];
|
|
27
|
+
if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);
|
|
28
|
+
if (c.slots) lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);
|
|
29
|
+
return lines.join("\n");
|
|
30
|
+
}).join("\n");
|
|
31
|
+
const functions = Object.entries(manifest.functions).map(([name, f]) => {
|
|
32
|
+
const signature = f.parameters.description;
|
|
33
|
+
const args = typeof signature === "string" && signature.startsWith("{") && Object.keys(f.parameters).length === 2 ? signature : JSON.stringify(f.parameters);
|
|
34
|
+
const callers = require_basic_functions.functionCallers(f);
|
|
35
|
+
return `- **${name}**${callers.agent && require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS ? callers.renderer ? " (UI or agent)" : " (agent only)" : ""}${f.description ? `: ${f.description}` : ""}\n args: ${args}`;
|
|
36
|
+
}).join("\n");
|
|
37
|
+
const surfaces = Object.entries(manifest.surfaces).map(([id, s]) => {
|
|
38
|
+
const state = s.status === "deleted" ? ", deleted: never use this id again" : s.status === "failed" ? ", not created: send a corrected createSurface with this id" : "";
|
|
39
|
+
const lines = [`- "${id}" (target "${s.target}"${state})`];
|
|
40
|
+
if (s.dataModel) lines.push(` user values: ${shorten(JSON.stringify(s.dataModel))}`);
|
|
41
|
+
for (const rejection of s.rejected ?? []) lines.push(` rejected: ${rejection}`);
|
|
42
|
+
return lines.join("\n");
|
|
43
|
+
}).join("\n");
|
|
44
|
+
const surfaceNotes = Object.values(manifest.surfaces).some((s) => s.dataModel !== void 0 || (s.rejected?.length ?? 0) > 0) ? "User values are what the user entered, by path. Your writes to those paths are ignored. Rejected operations were dropped: send them again with the problem fixed.\n" : "";
|
|
45
|
+
const { functions: hasFunctions, events, templates, checks } = require_a2ui_prompts.generativeUIGuideFeatures(manifest);
|
|
46
|
+
const agentCalls = hasFunctions && require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS;
|
|
47
|
+
const operations = [
|
|
48
|
+
"{\"version\":\"v1.0\",\"createSurface\":{\"surfaceId\":\"<new id>\",\"dataModel\":{...},\"components\":[{\"id\":\"root\",\"component\":\"<Name>\",\"children\":[\"<child id>\"]}]}}: the first operation of a new surface. \"dataModel\" and \"components\" are optional.",
|
|
49
|
+
"{\"version\":\"v1.0\",\"updateComponents\":{\"surfaceId\":\"<id>\",\"components\":[<component>, ...]}}: add or replace components by id.",
|
|
50
|
+
"{\"version\":\"v1.0\",\"updateDataModel\":{\"surfaceId\":\"<id>\",\"path\":\"/some/path\",\"value\":<json>}}: set the value at \"path\". Always give \"value\". \"value\":null deletes the key at \"path\".",
|
|
51
|
+
"{\"version\":\"v1.0\",\"deleteSurface\":{\"surfaceId\":\"<id>\"}}: remove the surface.",
|
|
52
|
+
...agentCalls ? ["{\"version\":\"v1.0\",\"callRendererFunction\":{\"functionCallId\":\"<unique id>\",\"callFunction\":{\"@call\":\"<function>\",\"args\":{...}}}}: run an app function now, with no click. It needs no surface."] : []
|
|
53
|
+
];
|
|
54
|
+
const rules = [
|
|
55
|
+
"Components are flat objects: {\"id\":\"...\",\"component\":\"<Name>\", ...props}. Every surface renders from the component with id \"root\". Reference children by id in slot props.",
|
|
56
|
+
"A component may take \"accessibility\":{\"label\":\"<1 to 3 words>\",\"description\":\"<what it does>\"} for screen readers: on the component it describes, when nothing visible names it. Not on every component, and \"live\":\"polite\" only on text that changes by itself.",
|
|
57
|
+
"Stream progressively: put the initial data in \"dataModel\" and the root in \"components\" of the createSurface operation, then send one updateComponents operation per child, so the UI appears while you are still writing. The root must already list the ids of its children in its slot props (for example \"children\":[\"card-1\",\"card-2\"]), even though the children come later. A component that no slot references is never shown.",
|
|
58
|
+
"Updating a component replaces all of its props, so resend every prop.",
|
|
59
|
+
"Prop values are literals or data bindings {\"@path\":\"/data/path\"}. Never write {\"path\":\"/data/path\"} as a binding.",
|
|
60
|
+
...events ? [hasFunctions ? "Event props take an action. {\"functionCall\":{\"@call\":\"<function>\",\"args\":{...}}} runs an app function in the browser immediately, and you are not told about it. {\"event\":{\"name\":\"<name>\",\"userMessage\":\"<text>\",\"context\":{...}}} sends the interaction back to you as a user message, with \"userMessage\" as its first line. \"userMessage\" is plain text: ${...} is not interpolated in it; put values in \"context\". Only call functions listed below." : "Event props take an action: {\"event\":{\"name\":\"<name>\",\"userMessage\":\"<text>\",\"context\":{...}}} sends the interaction back to you as a user message, with \"userMessage\" as its first line. \"userMessage\" is plain text: ${...} is not interpolated in it; put values in \"context\"."] : [],
|
|
61
|
+
"To show a different view, create a new surface with a new surfaceId (for example, a checkout form after a cart summary). Update an existing surface only when the user asks to change that same view: then do not send createSurface, use its surfaceId, and pass its existing target. Never reuse an earlier chat surface for a different view, because that replaces the earlier UI and anything the user typed into it.",
|
|
62
|
+
"Never use a surfaceId again, also after deleteSurface.",
|
|
63
|
+
"A prop can also be a function value that updates with the data: {\"@call\":\"formatCurrency\",\"args\":{\"value\":{\"@path\":\"/total\"},\"currency\":\"EUR\"}}. Only call functions listed below. formatString interpolates ${/path} and ${fn(arg: value)} in its value.",
|
|
64
|
+
"${...} is evaluated only in the value of formatString and in the item of series; in other strings it stays literal text. Inside formatString, ${...} does not support operators. For arithmetic use calc, with the values as named args: {\"@call\":\"calc\",\"args\":{\"expression\":\"seats * 29 * (annual ? 0.8 : 1)\",\"seats\":{\"@path\":\"/seats\"},\"annual\":{\"@path\":\"/annual\"}}}.",
|
|
65
|
+
"For a computed list (chart data, template items) use series. Its description under Functions gives the rules and an example.",
|
|
66
|
+
...checks ? ["Validate an input with \"checks\": [{\"condition\":{\"@call\":\"email\",\"args\":{\"value\":{\"@path\":\"/form/email\"}}},\"message\":\"Enter a valid email\"}]. The check functions are required, regex, length, numeric and email. A failed check shows the message under the input and disables a Button that has the same checks."] : [],
|
|
67
|
+
...templates ? ["To repeat a component for each item of a list, give a multiple slot a template: \"children\":{\"componentId\":\"row\",\"path\":\"/items\"}. Inside the template, paths are relative to the item (\"name\", not \"/name\"). {\"@call\":\"@index\"} gives the position of the item, from 0; {\"@call\":\"@index\",\"args\":{\"offset\":1}} counts from 1."] : [],
|
|
68
|
+
...agentCalls ? ["Give every callRendererFunction a new functionCallId. Call only a function marked (UI or agent) or (agent only); a function with no mark is for the UI only. A function marked (agent only) is only for callRendererFunction: do not bind an event to it.", "The result of a callRendererFunction comes back to you in a new turn, as a developer message: {\"version\":\"v1.0\",\"rendererFunctionResponse\":{\"functionCallId\":\"<id>\",\"value\":...}}. If the call failed, it has \"error\" in place of \"value\". Do not wait for the result in this reply, and do not guess it."] : []
|
|
69
|
+
];
|
|
70
|
+
return [
|
|
71
|
+
`## Generative UI
|
|
72
|
+
Call \`${require_types.GENERATIVE_UI_RENDER_TOOL}\` to show UI. Arguments: \`target\` first, then \`operations\` (A2UI v1.0 operations, applied in order).
|
|
73
|
+
|
|
74
|
+
${require_a2ui_prompts.GENERATIVE_UI_WHEN_TO_SHOW}
|
|
75
|
+
|
|
76
|
+
Every operation has "version":"v1.0" and exactly one of:
|
|
77
|
+
${operations.map((op) => `- ${op}`).join("\n")}
|
|
78
|
+
|
|
79
|
+
Rules:
|
|
80
|
+
${rules.map((rule, index) => `${index + 1}. ${rule}`).join("\n")}`,
|
|
81
|
+
`### Targets\n${targets || "- none"}`,
|
|
82
|
+
`### Components\n${components || "- none"}`,
|
|
83
|
+
`### Functions\n${functions || "- none"}`,
|
|
84
|
+
`### Existing surfaces\n${surfaceNotes}${surfaces || "- none"}`
|
|
85
|
+
].join("\n\n");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
exports.GENERATIVE_UI_CONTEXT_DESCRIPTION = GENERATIVE_UI_CONTEXT_DESCRIPTION;
|
|
90
|
+
exports.buildGenerativeUIPrompt = buildGenerativeUIPrompt;
|
|
91
|
+
//# sourceMappingURL=prompt.cjs.map
|