@copilotkit/shared 2.0.0-rc.2 → 2.0.0-rc.20

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 (272) hide show
  1. package/README.md +3 -14
  2. package/dist/a2ui-envelope.cjs +2 -2
  3. package/dist/a2ui-envelope.cjs.map +1 -1
  4. package/dist/a2ui-envelope.d.cts +1 -1
  5. package/dist/a2ui-envelope.d.cts.map +1 -1
  6. package/dist/a2ui-envelope.d.mts +1 -1
  7. package/dist/a2ui-envelope.d.mts.map +1 -1
  8. package/dist/a2ui-envelope.mjs +2 -2
  9. package/dist/a2ui-envelope.mjs.map +1 -1
  10. package/dist/a2ui-prompts.cjs +21 -62
  11. package/dist/a2ui-prompts.cjs.map +1 -1
  12. package/dist/a2ui-prompts.d.cts +8 -1
  13. package/dist/a2ui-prompts.d.cts.map +1 -1
  14. package/dist/a2ui-prompts.d.mts +8 -1
  15. package/dist/a2ui-prompts.d.mts.map +1 -1
  16. package/dist/a2ui-prompts.mjs +22 -62
  17. package/dist/a2ui-prompts.mjs.map +1 -1
  18. package/dist/finalize-events.cjs +16 -0
  19. package/dist/finalize-events.cjs.map +1 -1
  20. package/dist/finalize-events.d.cts.map +1 -1
  21. package/dist/finalize-events.d.mts.map +1 -1
  22. package/dist/finalize-events.mjs +16 -0
  23. package/dist/finalize-events.mjs.map +1 -1
  24. package/dist/generative-ui/basic-functions.cjs +1 -1
  25. package/dist/generative-ui/basic-functions.cjs.map +1 -1
  26. package/dist/generative-ui/basic-functions.mjs +1 -1
  27. package/dist/generative-ui/basic-functions.mjs.map +1 -1
  28. package/dist/generative-ui/child-refs.cjs +3 -3
  29. package/dist/generative-ui/child-refs.cjs.map +1 -1
  30. package/dist/generative-ui/child-refs.d.cts.map +1 -1
  31. package/dist/generative-ui/child-refs.d.mts.map +1 -1
  32. package/dist/generative-ui/child-refs.mjs +3 -3
  33. package/dist/generative-ui/child-refs.mjs.map +1 -1
  34. package/dist/generative-ui/compact-schema.cjs +187 -0
  35. package/dist/generative-ui/compact-schema.cjs.map +1 -0
  36. package/dist/generative-ui/compact-schema.d.cts +55 -0
  37. package/dist/generative-ui/compact-schema.d.cts.map +1 -0
  38. package/dist/generative-ui/compact-schema.d.mts +55 -0
  39. package/dist/generative-ui/compact-schema.d.mts.map +1 -0
  40. package/dist/generative-ui/compact-schema.mjs +185 -0
  41. package/dist/generative-ui/compact-schema.mjs.map +1 -0
  42. package/dist/generative-ui/prompt-spec.cjs +225 -0
  43. package/dist/generative-ui/prompt-spec.cjs.map +1 -0
  44. package/dist/generative-ui/prompt-spec.d.cts +94 -0
  45. package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
  46. package/dist/generative-ui/prompt-spec.d.mts +94 -0
  47. package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
  48. package/dist/generative-ui/prompt-spec.mjs +220 -0
  49. package/dist/generative-ui/prompt-spec.mjs.map +1 -0
  50. package/dist/generative-ui/prompt.cjs +10 -36
  51. package/dist/generative-ui/prompt.cjs.map +1 -1
  52. package/dist/generative-ui/prompt.d.cts +2 -1
  53. package/dist/generative-ui/prompt.d.cts.map +1 -1
  54. package/dist/generative-ui/prompt.d.mts +2 -1
  55. package/dist/generative-ui/prompt.d.mts.map +1 -1
  56. package/dist/generative-ui/prompt.mjs +12 -38
  57. package/dist/generative-ui/prompt.mjs.map +1 -1
  58. package/dist/generative-ui/render-ui-args.cjs +1 -0
  59. package/dist/generative-ui/render-ui-args.cjs.map +1 -1
  60. package/dist/generative-ui/render-ui-args.d.cts.map +1 -1
  61. package/dist/generative-ui/render-ui-args.d.mts.map +1 -1
  62. package/dist/generative-ui/render-ui-args.mjs +1 -0
  63. package/dist/generative-ui/render-ui-args.mjs.map +1 -1
  64. package/dist/generative-ui/schema-render.cjs +242 -0
  65. package/dist/generative-ui/schema-render.cjs.map +1 -0
  66. package/dist/generative-ui/schema-render.d.cts +44 -0
  67. package/dist/generative-ui/schema-render.d.cts.map +1 -0
  68. package/dist/generative-ui/schema-render.d.mts +44 -0
  69. package/dist/generative-ui/schema-render.d.mts.map +1 -0
  70. package/dist/generative-ui/schema-render.mjs +239 -0
  71. package/dist/generative-ui/schema-render.mjs.map +1 -0
  72. package/dist/generative-ui/types.cjs +2 -2
  73. package/dist/generative-ui/types.cjs.map +1 -1
  74. package/dist/generative-ui/types.d.cts +7 -7
  75. package/dist/generative-ui/types.d.cts.map +1 -1
  76. package/dist/generative-ui/types.d.mts +7 -7
  77. package/dist/generative-ui/types.d.mts.map +1 -1
  78. package/dist/generative-ui/types.mjs +2 -2
  79. package/dist/generative-ui/types.mjs.map +1 -1
  80. package/dist/generative-ui/validate-operation.cjs +2 -2
  81. package/dist/generative-ui/validate-operation.cjs.map +1 -1
  82. package/dist/generative-ui/validate-operation.mjs +2 -2
  83. package/dist/generative-ui/validate-operation.mjs.map +1 -1
  84. package/dist/index.cjs +17 -15
  85. package/dist/index.cjs.map +1 -1
  86. package/dist/index.d.cts +10 -9
  87. package/dist/index.d.cts.map +1 -1
  88. package/dist/index.d.mts +10 -9
  89. package/dist/index.d.mts.map +1 -1
  90. package/dist/index.mjs +9 -7
  91. package/dist/index.mjs.map +1 -1
  92. package/dist/index.umd.js +839 -447
  93. package/dist/index.umd.js.map +1 -1
  94. package/dist/package.cjs +1 -1
  95. package/dist/package.mjs +1 -1
  96. package/dist/standard-schema.cjs +20 -7
  97. package/dist/standard-schema.cjs.map +1 -1
  98. package/dist/standard-schema.d.cts +15 -16
  99. package/dist/standard-schema.d.cts.map +1 -1
  100. package/dist/standard-schema.d.mts +15 -16
  101. package/dist/standard-schema.d.mts.map +1 -1
  102. package/dist/standard-schema.mjs +20 -8
  103. package/dist/standard-schema.mjs.map +1 -1
  104. package/dist/telemetry/lambda-client.cjs +1 -1
  105. package/dist/telemetry/lambda-client.cjs.map +1 -1
  106. package/dist/telemetry/lambda-client.d.cts.map +1 -1
  107. package/dist/telemetry/lambda-client.d.mts.map +1 -1
  108. package/dist/telemetry/lambda-client.mjs +1 -1
  109. package/dist/telemetry/lambda-client.mjs.map +1 -1
  110. package/dist/telemetry/telemetry-disabled.cjs +3 -6
  111. package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
  112. package/dist/telemetry/telemetry-disabled.d.cts +0 -5
  113. package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
  114. package/dist/telemetry/telemetry-disabled.d.mts +0 -5
  115. package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
  116. package/dist/telemetry/telemetry-disabled.mjs +4 -6
  117. package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
  118. package/dist/types/message.d.cts +1 -18
  119. package/dist/types/message.d.cts.map +1 -1
  120. package/dist/types/message.d.mts +1 -18
  121. package/dist/types/message.d.mts.map +1 -1
  122. package/dist/utils/errors.cjs +5 -42
  123. package/dist/utils/errors.cjs.map +1 -1
  124. package/dist/utils/errors.d.cts +16 -34
  125. package/dist/utils/errors.d.cts.map +1 -1
  126. package/dist/utils/errors.d.mts +16 -34
  127. package/dist/utils/errors.d.mts.map +1 -1
  128. package/dist/utils/errors.mjs +6 -41
  129. package/dist/utils/errors.mjs.map +1 -1
  130. package/dist/utils/index.cjs +1 -2
  131. package/dist/utils/index.cjs.map +1 -1
  132. package/dist/utils/index.d.cts +4 -5
  133. package/dist/utils/index.d.cts.map +1 -1
  134. package/dist/utils/index.d.mts +4 -5
  135. package/dist/utils/index.d.mts.map +1 -1
  136. package/dist/utils/index.mjs +4 -5
  137. package/dist/utils/index.mjs.map +1 -1
  138. package/dist/utils/json-schema.cjs +4 -79
  139. package/dist/utils/json-schema.cjs.map +1 -1
  140. package/dist/utils/json-schema.d.cts +1 -4
  141. package/dist/utils/json-schema.d.cts.map +1 -1
  142. package/dist/utils/json-schema.d.mts +1 -4
  143. package/dist/utils/json-schema.d.mts.map +1 -1
  144. package/dist/utils/json-schema.mjs +5 -78
  145. package/dist/utils/json-schema.mjs.map +1 -1
  146. package/dist/utils/process-env.cjs +14 -0
  147. package/dist/utils/process-env.cjs.map +1 -0
  148. package/dist/utils/process-env.d.cts +10 -0
  149. package/dist/utils/process-env.d.cts.map +1 -0
  150. package/dist/utils/process-env.d.mts +10 -0
  151. package/dist/utils/process-env.d.mts.map +1 -0
  152. package/dist/utils/process-env.mjs +13 -0
  153. package/dist/utils/process-env.mjs.map +1 -0
  154. package/dist/utils/random-id.cjs +13 -26
  155. package/dist/utils/random-id.cjs.map +1 -1
  156. package/dist/utils/random-id.d.cts +6 -3
  157. package/dist/utils/random-id.d.cts.map +1 -1
  158. package/dist/utils/random-id.d.mts +6 -3
  159. package/dist/utils/random-id.d.mts.map +1 -1
  160. package/dist/utils/random-id.mjs +14 -25
  161. package/dist/utils/random-id.mjs.map +1 -1
  162. package/package.json +8 -32
  163. package/scripts/verify-cjs-exports.cjs +1 -1
  164. package/src/__tests__/a2ui-envelope.test.ts +18 -13
  165. package/src/__tests__/a2ui-prompts.test.ts +47 -14
  166. package/src/__tests__/fixtures/prompt-goldens.json +354 -0
  167. package/src/__tests__/prompt-spec.test.ts +191 -0
  168. package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
  169. package/src/__tests__/schema-render.test.ts +245 -0
  170. package/src/__tests__/standard-schema.test.ts +61 -101
  171. package/src/a2ui-envelope.ts +7 -6
  172. package/src/a2ui-prompts.ts +47 -96
  173. package/src/finalize-events.ts +16 -0
  174. package/src/generative-ui/__tests__/render-ui-args.test.ts +7 -7
  175. package/src/generative-ui/__tests__/validate-operation.test.ts +19 -13
  176. package/src/generative-ui/basic-functions.ts +1 -1
  177. package/src/generative-ui/child-refs.ts +5 -4
  178. package/src/generative-ui/compact-schema.ts +277 -0
  179. package/src/generative-ui/index.ts +3 -0
  180. package/src/generative-ui/prompt-spec.ts +335 -0
  181. package/src/generative-ui/prompt.ts +25 -75
  182. package/src/generative-ui/render-ui-args.ts +3 -0
  183. package/src/generative-ui/schema-render.ts +377 -0
  184. package/src/generative-ui/types.ts +10 -7
  185. package/src/generative-ui/validate-operation.ts +2 -2
  186. package/src/index.ts +3 -16
  187. package/src/standard-schema.ts +24 -27
  188. package/src/telemetry/lambda-client.ts +8 -3
  189. package/src/telemetry/model-host.test.ts +2 -5
  190. package/src/telemetry/telemetry-disabled.ts +7 -12
  191. package/src/types/message.ts +1 -19
  192. package/src/utils/__tests__/errors.test.ts +44 -0
  193. package/src/utils/__tests__/json-schema.test.ts +3 -479
  194. package/src/utils/__tests__/process-env.test.ts +22 -0
  195. package/src/utils/__tests__/random-id.test.ts +37 -0
  196. package/src/utils/errors.ts +28 -85
  197. package/src/utils/index.ts +1 -2
  198. package/src/utils/json-schema.ts +5 -152
  199. package/src/utils/process-env.ts +14 -0
  200. package/src/utils/random-id.ts +17 -37
  201. package/tsconfig.json +3 -1
  202. package/tsdown.config.ts +2 -19
  203. package/dist/event-transforms/index.cjs +0 -15
  204. package/dist/event-transforms/index.d.cts +0 -4
  205. package/dist/event-transforms/index.d.mts +0 -4
  206. package/dist/event-transforms/index.mjs +0 -5
  207. package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
  208. package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
  209. package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
  210. package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
  211. package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
  212. package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
  213. package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
  214. package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
  215. package/dist/event-transforms/recorded-events.cjs +0 -59
  216. package/dist/event-transforms/recorded-events.cjs.map +0 -1
  217. package/dist/event-transforms/recorded-events.d.cts +0 -23
  218. package/dist/event-transforms/recorded-events.d.cts.map +0 -1
  219. package/dist/event-transforms/recorded-events.d.mts +0 -23
  220. package/dist/event-transforms/recorded-events.d.mts.map +0 -1
  221. package/dist/event-transforms/recorded-events.mjs +0 -58
  222. package/dist/event-transforms/recorded-events.mjs.map +0 -1
  223. package/dist/telemetry/events.d.cts +0 -72
  224. package/dist/telemetry/events.d.cts.map +0 -1
  225. package/dist/telemetry/events.d.mts +0 -72
  226. package/dist/telemetry/events.d.mts.map +0 -1
  227. package/dist/telemetry/index.cjs +0 -18
  228. package/dist/telemetry/index.d.cts +0 -5
  229. package/dist/telemetry/index.d.mts +0 -5
  230. package/dist/telemetry/index.mjs +0 -6
  231. package/dist/telemetry/telemetry-client.cjs +0 -165
  232. package/dist/telemetry/telemetry-client.cjs.map +0 -1
  233. package/dist/telemetry/telemetry-client.d.cts +0 -87
  234. package/dist/telemetry/telemetry-client.d.cts.map +0 -1
  235. package/dist/telemetry/telemetry-client.d.mts +0 -87
  236. package/dist/telemetry/telemetry-client.d.mts.map +0 -1
  237. package/dist/telemetry/telemetry-client.mjs +0 -164
  238. package/dist/telemetry/telemetry-client.mjs.map +0 -1
  239. package/dist/telemetry/utils.cjs +0 -14
  240. package/dist/telemetry/utils.cjs.map +0 -1
  241. package/dist/telemetry/utils.mjs +0 -13
  242. package/dist/telemetry/utils.mjs.map +0 -1
  243. package/dist/utils/conditions.cjs +0 -32
  244. package/dist/utils/conditions.cjs.map +0 -1
  245. package/dist/utils/conditions.d.cts +0 -31
  246. package/dist/utils/conditions.d.cts.map +0 -1
  247. package/dist/utils/conditions.d.mts +0 -31
  248. package/dist/utils/conditions.d.mts.map +0 -1
  249. package/dist/utils/conditions.mjs +0 -31
  250. package/dist/utils/conditions.mjs.map +0 -1
  251. package/dist/utils/console-styling.cjs +0 -91
  252. package/dist/utils/console-styling.cjs.map +0 -1
  253. package/dist/utils/console-styling.d.cts +0 -65
  254. package/dist/utils/console-styling.d.cts.map +0 -1
  255. package/dist/utils/console-styling.d.mts +0 -65
  256. package/dist/utils/console-styling.d.mts.map +0 -1
  257. package/dist/utils/console-styling.mjs +0 -85
  258. package/dist/utils/console-styling.mjs.map +0 -1
  259. package/src/__tests__/zod-regression.test.ts +0 -319
  260. package/src/event-transforms/index.ts +0 -5
  261. package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
  262. package/src/event-transforms/recorded-events.test.ts +0 -166
  263. package/src/event-transforms/recorded-events.ts +0 -74
  264. package/src/telemetry/events.ts +0 -68
  265. package/src/telemetry/index.ts +0 -9
  266. package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
  267. package/src/telemetry/telemetry-client.test.ts +0 -798
  268. package/src/telemetry/telemetry-client.ts +0 -328
  269. package/src/telemetry/utils.ts +0 -15
  270. package/src/utils/__tests__/conditions.test.ts +0 -161
  271. package/src/utils/conditions.ts +0 -116
  272. package/src/utils/console-styling.ts +0 -126
@@ -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,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,MA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,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,sBAAsB;;;;;;;;AASnC,MAAa,yBAAyB;AACtC,MAAa,uBAAuB;;AAgBpC,SAAgB,aACd,QAC0B;CAC1B,MAAM,MAAM,OAAO;AACnB,KAAI,QAAQ,sBAAuB,QAAO;AAC1C,KAAI,QA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AET,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;;;;;;;;;AAUtB,SAAgB,iBAAiB,QAA4C;CAC3E,MAAM,OAAmB,EAAE;CAC3B,MAAM,uBAAO,IAAI,KAAa;CAC9B,MAAM,OAAO,MAAgB,SAAuB;EAClD,MAAM,MAAM,KAAK,KAAK,KA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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,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,CAA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C;AACpE;;AAEF,MAAI,SAAS,MAAM,IAAI,OAAO,UAAU,eAAe,KAAK,OAAO,KAAK,CACtE,OAAM,MAAM,OAAO,KAAK;;AAG5B,MAAK,MAAM,OAAO,KAAM,OAAM,WAAW,IAAI,KAAK;AAClD,QAAO;;;;;;;AAQT,SAAgB,iBACd,WACA,MACS;CACT,MAAM,aAAa,UACjB,SAAS,MAAM,KAAK,WAAW,SAAS,WAAW;CACrD,MAAM,SAAS,OAAgB,SAAqC;AAClE,MAAI,UAAU,MAAM,CAAE,QAAO;EAC7B,MAAM,CAAC,MAAM,GAAG,QAAQ;AACxB,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,SAAS,IACX,QAAO,MAAM,QAAQ,MAAM,IAAI,MAAM,MAAM,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;CACT,MAAM,CAAC,MAAM,GAAG,QAAQ;AACxB,KAAI,SAAS,OAAW,QAAO,QAAQ,MAAM;AAC7C,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"}
@@ -0,0 +1,187 @@
1
+ const require_basic_functions = require('./basic-functions.cjs');
2
+ const require_child_refs = require('./child-refs.cjs');
3
+ const require_types = require('./types.cjs');
4
+
5
+ //#region src/generative-ui/compact-schema.ts
6
+ /** Objects nested deeper than this show as `object`. */
7
+ const MAX_OBJECT_DEPTH = 2;
8
+ /** A prop must appear verbatim in at least this many components to be hoisted. */
9
+ const COMMON_PROP_MIN_COMPONENTS = 8;
10
+ function isRecord(value) {
11
+ return typeof value === "object" && value !== null && !Array.isArray(value);
12
+ }
13
+ /** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */
14
+ function schemaList(value) {
15
+ return Array.isArray(value) ? value.filter(isRecord) : [];
16
+ }
17
+ /**
18
+ * Puts a top-level union in parentheses, so that `[]` applies to all of
19
+ * it. A union inside braces needs none.
20
+ */
21
+ function asArrayItem(type) {
22
+ let outside = type;
23
+ for (let inner = outside.replace(/\{[^{}]*\}/g, ""); inner !== outside;) {
24
+ outside = inner;
25
+ inner = outside.replace(/\{[^{}]*\}/g, "");
26
+ }
27
+ return outside.includes(" | ") ? `(${type})[]` : `${type}[]`;
28
+ }
29
+ /**
30
+ * A JSON Schema as a short TypeScript-like type, for example
31
+ * `{ label: string; size?: "sm" | "md" }`.
32
+ */
33
+ function compactType(schema, depth) {
34
+ const ref = require_child_refs.childRefKind(schema);
35
+ if (ref === "single") return "componentId";
36
+ if (ref === "multiple") return "componentId[] | { componentId: string; path: string }";
37
+ if ("const" in schema) return JSON.stringify(schema.const);
38
+ if (Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
39
+ const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];
40
+ if (variants.length > 0) return variants.map((variant) => compactType(variant, depth)).join(" | ");
41
+ return (Array.isArray(schema.type) ? schema.type : [schema.type]).map((type) => {
42
+ switch (type) {
43
+ case "string":
44
+ case "boolean":
45
+ case "null": return type;
46
+ case "number":
47
+ case "integer": return "number";
48
+ case "array": return isRecord(schema.items) ? asArrayItem(compactType(schema.items, depth)) : "unknown[]";
49
+ case "object": return compactObject(schema, depth);
50
+ default: return "unknown";
51
+ }
52
+ }).join(" | ");
53
+ }
54
+ /** An object schema as `{ key: type; other?: type }`. */
55
+ function compactObject(schema, depth) {
56
+ const properties = isRecord(schema.properties) ? schema.properties : {};
57
+ const entries = Object.entries(properties);
58
+ if (entries.length === 0) return isRecord(schema.additionalProperties) ? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>` : "object";
59
+ if (depth >= MAX_OBJECT_DEPTH) return "object";
60
+ const required = Array.isArray(schema.required) ? schema.required : [];
61
+ return `{ ${entries.map(([key, value]) => {
62
+ const type = isRecord(value) ? compactType(value, depth + 1) : "unknown";
63
+ return `${key}${required.includes(key) ? "" : "?"}: ${type}`;
64
+ }).join("; ")} }`;
65
+ }
66
+ /** A schema as its type, with its description after `//`. */
67
+ function describedType(schema, depth) {
68
+ const type = compactType(schema, depth);
69
+ return typeof schema.description === "string" ? `${type} // ${schema.description}` : type;
70
+ }
71
+ /**
72
+ * The props of a component: `{ "label": "string", "size?": "..." }`. With
73
+ * `descriptions`, each type carries its description after `//`.
74
+ */
75
+ function compactProps(schema, descriptions = true) {
76
+ const properties = isRecord(schema.properties) ? schema.properties : {};
77
+ const required = Array.isArray(schema.required) ? schema.required : [];
78
+ const props = {};
79
+ for (const [key, value] of Object.entries(properties)) {
80
+ if (!isRecord(value)) continue;
81
+ const name = required.includes(key) ? key : `${key}?`;
82
+ props[name] = descriptions ? describedType(value, 1) : compactType(value, 1);
83
+ }
84
+ return props;
85
+ }
86
+ /**
87
+ * The prop set worth listing once. Candidates are (key, type) pairs that at
88
+ * least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The
89
+ * candidate subset of each component is a possible common set, scored by
90
+ * the characters it saves (set size times the components that contain all
91
+ * of it). `undefined` when no set is shared by enough components.
92
+ */
93
+ function findCommonProps(propsList) {
94
+ const pairKey = (key, type) => `${key}\u0000${type}`;
95
+ const counts = /* @__PURE__ */ new Map();
96
+ for (const props of propsList) for (const [key, type] of Object.entries(props)) {
97
+ const id = pairKey(key, type);
98
+ counts.set(id, (counts.get(id) ?? 0) + 1);
99
+ }
100
+ let best;
101
+ let bestScore = 0;
102
+ const seen = /* @__PURE__ */ new Set();
103
+ for (const props of propsList) {
104
+ const subset = Object.entries(props).filter(([key, type]) => (counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS);
105
+ if (subset.length < 2) continue;
106
+ const signature = subset.map(([key, type]) => pairKey(key, type)).join("\n");
107
+ if (seen.has(signature)) continue;
108
+ seen.add(signature);
109
+ const members = propsList.filter((other) => subset.every(([key, type]) => other[key] === type)).length;
110
+ if (members < COMMON_PROP_MIN_COMPONENTS) continue;
111
+ const score = subset.reduce((total, [key, type]) => total + key.length + type.length, 0) * (members - 1);
112
+ if (score > bestScore) {
113
+ bestScore = score;
114
+ best = Object.fromEntries(subset);
115
+ }
116
+ }
117
+ return best;
118
+ }
119
+ /**
120
+ * The component schema of the markdown transport, from the per-run
121
+ * manifest. It is smaller than the manifest: each prop is one short type,
122
+ * and the props that many components share verbatim (the layout props of
123
+ * the containers) are listed once in `commonProps`; a component that takes
124
+ * them is flagged `includesCommonProps`.
125
+ * It has the catalog id, the targets with their descriptions and their
126
+ * component lists, the functions with their parameters, the components
127
+ * with their props, events and slots, and the surfaces of the thread. A surface lists the
128
+ * values that the user set and the operations that the client dropped.
129
+ *
130
+ * @param manifest - The manifest from `buildGenerativeUIManifest`.
131
+ * @param options - `propDescriptions` (default true) writes each prop's
132
+ * description; `commonProps` (default true) lists shared props once.
133
+ * @returns A JSON-ready object.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * const value = JSON.stringify(
138
+ * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
139
+ * );
140
+ * ```
141
+ */
142
+ function buildCompactGenerativeUISchema(manifest, options = {}) {
143
+ const { propDescriptions = true, commonProps = true } = options;
144
+ const components = {};
145
+ for (const [name, component] of Object.entries(manifest.components)) {
146
+ const events = Object.entries(component.events ?? {}).map(([event, definition]) => [event, definition.description ?? compactType(definition.parameters, 0)]);
147
+ components[name] = {
148
+ ...component.description ? { description: component.description } : {},
149
+ props: compactProps(component.props, propDescriptions),
150
+ ...events.length > 0 ? { events: Object.fromEntries(events) } : {},
151
+ ...component.slots ? { slots: component.slots } : {}
152
+ };
153
+ }
154
+ const common = commonProps ? findCommonProps(Object.values(components).map((component) => component.props)) : void 0;
155
+ if (common) {
156
+ const commonKeys = Object.keys(common);
157
+ for (const component of Object.values(components)) {
158
+ if (!commonKeys.every((key) => component.props[key] === common[key])) continue;
159
+ component.includesCommonProps = true;
160
+ component.props = Object.fromEntries(Object.entries(component.props).filter(([key]) => !(key in common)));
161
+ }
162
+ }
163
+ const functions = Object.fromEntries(Object.entries(manifest.functions).map(([name, fn]) => {
164
+ const signature = fn.parameters.description;
165
+ const hasSignature = typeof signature === "string" && signature.startsWith("{") && Object.keys(fn.parameters).length === 2;
166
+ const callers = require_basic_functions.functionCallers(fn);
167
+ return [name, {
168
+ ...fn.description ? { description: fn.description } : {},
169
+ parameters: hasSignature ? signature : compactType(fn.parameters, 0),
170
+ ...callers.agent && require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS ? { callers: callers.renderer ? "UI or agent" : "agent only" } : {}
171
+ }];
172
+ }));
173
+ return {
174
+ catalogId: manifest.catalogId,
175
+ targets: manifest.targets,
176
+ functions,
177
+ ...common ? { commonProps: common } : {},
178
+ components,
179
+ surfaces: manifest.surfaces
180
+ };
181
+ }
182
+
183
+ //#endregion
184
+ exports.buildCompactGenerativeUISchema = buildCompactGenerativeUISchema;
185
+ exports.compactProps = compactProps;
186
+ exports.compactType = compactType;
187
+ //# sourceMappingURL=compact-schema.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compact-schema.cjs","names":["childRefKind","functionCallers","GENERATIVE_UI_AGENT_FUNCTION_CALLS"],"sources":["../../src/generative-ui/compact-schema.ts"],"sourcesContent":["import { childRefKind } from \"./child-refs\";\nimport { functionCallers } from \"./basic-functions\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type { GenerativeUIJSONSchema, GenerativeUIManifest } from \"./types\";\n\n/** Objects nested deeper than this show as `object`. */\nconst MAX_OBJECT_DEPTH = 2;\n\n/** A prop must appear verbatim in at least this many components to be hoisted. */\nconst COMMON_PROP_MIN_COMPONENTS = 8;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */\nfunction schemaList(value: unknown) {\n return Array.isArray(value) ? value.filter(isRecord) : [];\n}\n\n/**\n * Puts a top-level union in parentheses, so that `[]` applies to all of\n * it. A union inside braces needs none.\n */\nfunction asArrayItem(type: string) {\n let outside = type;\n for (let inner = outside.replace(/\\{[^{}]*\\}/g, \"\"); inner !== outside; ) {\n outside = inner;\n inner = outside.replace(/\\{[^{}]*\\}/g, \"\");\n }\n return outside.includes(\" | \") ? `(${type})[]` : `${type}[]`;\n}\n\n/**\n * A JSON Schema as a short TypeScript-like type, for example\n * `{ label: string; size?: \"sm\" | \"md\" }`.\n */\nexport function compactType(\n schema: GenerativeUIJSONSchema,\n depth: number,\n): string {\n const ref = childRefKind(schema);\n if (ref === \"single\") return \"componentId\";\n if (ref === \"multiple\")\n return \"componentId[] | { componentId: string; path: string }\";\n if (\"const\" in schema) return JSON.stringify(schema.const);\n if (Array.isArray(schema.enum)) {\n return schema.enum.map((value) => JSON.stringify(value)).join(\" | \");\n }\n const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];\n if (variants.length > 0) {\n return variants.map((variant) => compactType(variant, depth)).join(\" | \");\n }\n const types = Array.isArray(schema.type) ? schema.type : [schema.type];\n const summaries = types.map((type) => {\n switch (type) {\n case \"string\":\n case \"boolean\":\n case \"null\":\n return type;\n case \"number\":\n case \"integer\":\n return \"number\";\n case \"array\":\n return isRecord(schema.items)\n ? asArrayItem(compactType(schema.items, depth))\n : \"unknown[]\";\n case \"object\":\n return compactObject(schema, depth);\n default:\n return \"unknown\";\n }\n });\n return summaries.join(\" | \");\n}\n\n/** An object schema as `{ key: type; other?: type }`. */\nfunction compactObject(schema: GenerativeUIJSONSchema, depth: number) {\n const properties = isRecord(schema.properties) ? schema.properties : {};\n const entries = Object.entries(properties);\n if (entries.length === 0) {\n return isRecord(schema.additionalProperties)\n ? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>`\n : \"object\";\n }\n if (depth >= MAX_OBJECT_DEPTH) return \"object\";\n const required = Array.isArray(schema.required) ? schema.required : [];\n const fields = entries.map(([key, value]) => {\n const type = isRecord(value) ? compactType(value, depth + 1) : \"unknown\";\n return `${key}${required.includes(key) ? \"\" : \"?\"}: ${type}`;\n });\n return `{ ${fields.join(\"; \")} }`;\n}\n\n/** A schema as its type, with its description after `//`. */\nfunction describedType(schema: GenerativeUIJSONSchema, depth: number) {\n const type = compactType(schema, depth);\n return typeof schema.description === \"string\"\n ? `${type} // ${schema.description}`\n : type;\n}\n\n/**\n * The props of a component: `{ \"label\": \"string\", \"size?\": \"...\" }`. With\n * `descriptions`, each type carries its description after `//`.\n */\nexport function compactProps(\n schema: GenerativeUIJSONSchema,\n descriptions = true,\n) {\n const properties = isRecord(schema.properties) ? schema.properties : {};\n const required = Array.isArray(schema.required) ? schema.required : [];\n const props: Record<string, string> = {};\n for (const [key, value] of Object.entries(properties)) {\n if (!isRecord(value)) continue;\n const name = required.includes(key) ? key : `${key}?`;\n props[name] = descriptions\n ? describedType(value, 1)\n : compactType(value, 1);\n }\n return props;\n}\n\n/**\n * The prop set worth listing once. Candidates are (key, type) pairs that at\n * least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The\n * candidate subset of each component is a possible common set, scored by\n * the characters it saves (set size times the components that contain all\n * of it). `undefined` when no set is shared by enough components.\n */\nfunction findCommonProps(propsList: Array<Record<string, string>>) {\n const pairKey = (key: string, type: string) => `${key}\\u0000${type}`;\n const counts = new Map<string, number>();\n for (const props of propsList) {\n for (const [key, type] of Object.entries(props)) {\n const id = pairKey(key, type);\n counts.set(id, (counts.get(id) ?? 0) + 1);\n }\n }\n let best: Record<string, string> | undefined;\n let bestScore = 0;\n const seen = new Set<string>();\n for (const props of propsList) {\n const subset = Object.entries(props).filter(\n ([key, type]) =>\n (counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS,\n );\n if (subset.length < 2) continue;\n const signature = subset\n .map(([key, type]) => pairKey(key, type))\n .join(\"\\n\");\n if (seen.has(signature)) continue;\n seen.add(signature);\n const members = propsList.filter((other) =>\n subset.every(([key, type]) => other[key] === type),\n ).length;\n if (members < COMMON_PROP_MIN_COMPONENTS) continue;\n const size = subset.reduce(\n (total, [key, type]) => total + key.length + type.length,\n 0,\n );\n const score = size * (members - 1);\n if (score > bestScore) {\n bestScore = score;\n best = Object.fromEntries(subset);\n }\n }\n return best;\n}\n\n/** One component of the compact schema. */\ninterface CompactComponent {\n description?: string;\n /** True when the component also takes every prop in `commonProps`. */\n includesCommonProps?: true;\n props: Record<string, string>;\n events?: Record<string, string>;\n slots?: Record<string, \"single\" | \"multiple\">;\n}\n\n/**\n * The component schema of the markdown transport, from the per-run\n * manifest. It is smaller than the manifest: each prop is one short type,\n * and the props that many components share verbatim (the layout props of\n * the containers) are listed once in `commonProps`; a component that takes\n * them is flagged `includesCommonProps`.\n * It has the catalog id, the targets with their descriptions and their\n * component lists, the functions with their parameters, the components\n * with their props, events and slots, and the surfaces of the thread. A surface lists the\n * values that the user set and the operations that the client dropped.\n *\n * @param manifest - The manifest from `buildGenerativeUIManifest`.\n * @param options - `propDescriptions` (default true) writes each prop's\n * description; `commonProps` (default true) lists shared props once.\n * @returns A JSON-ready object.\n *\n * @example\n * ```ts\n * const value = JSON.stringify(\n * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),\n * );\n * ```\n */\nexport function buildCompactGenerativeUISchema(\n manifest: Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n >,\n options: { propDescriptions?: boolean; commonProps?: boolean } = {},\n) {\n const { propDescriptions = true, commonProps = true } = options;\n const components: Record<string, CompactComponent> = {};\n for (const [name, component] of Object.entries(manifest.components)) {\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) => [\n event,\n definition.description ?? compactType(definition.parameters, 0),\n ],\n );\n components[name] = {\n ...(component.description ? { description: component.description } : {}),\n props: compactProps(component.props, propDescriptions),\n ...(events.length > 0 ? { events: Object.fromEntries(events) } : {}),\n ...(component.slots ? { slots: component.slots } : {}),\n };\n }\n const common = commonProps\n ? findCommonProps(\n Object.values(components).map((component) => component.props),\n )\n : undefined;\n if (common) {\n const commonKeys = Object.keys(common);\n for (const component of Object.values(components)) {\n const includesCommon = commonKeys.every(\n (key) => component.props[key] === common[key],\n );\n if (!includesCommon) continue;\n component.includesCommonProps = true;\n component.props = Object.fromEntries(\n Object.entries(component.props).filter(([key]) => !(key in common)),\n );\n }\n }\n // A built-in function carries its short signature in the description of\n // its parameters (see `builtInFunctionManifests`); an app function gets\n // its JSON Schema made short. A function is for the UI unless it says\n // that the agent may call it too, or only the agent.\n const functions = Object.fromEntries(\n Object.entries(manifest.functions).map(([name, fn]) => {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n const callers = functionCallers(fn);\n return [\n name,\n {\n ...(fn.description ? { description: fn.description } : {}),\n parameters: hasSignature ? signature : compactType(fn.parameters, 0),\n ...(callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? { callers: callers.renderer ? \"UI or agent\" : \"agent only\" }\n : {}),\n },\n ];\n }),\n );\n return {\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions,\n ...(common ? { commonProps: common } : {}),\n components,\n surfaces: manifest.surfaces,\n };\n}\n"],"mappings":";;;;;;AAMA,MAAM,mBAAmB;;AAGzB,MAAM,6BAA6B;AAEnC,SAAS,SAAS,OAAkD;AAClE,QAAO,OAAO,UAAU,YAAY,UA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GAAI,SAAS,EAAE,aAAa,QAAQ,GAAG,EAAE;EACzC;EACA,UAAU,SAAS;EACpB"}
@@ -0,0 +1,55 @@
1
+ import { GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./types.cjs";
2
+
3
+ //#region src/generative-ui/compact-schema.d.ts
4
+ /** One component of the compact schema. */
5
+ interface CompactComponent {
6
+ description?: string;
7
+ /** True when the component also takes every prop in `commonProps`. */
8
+ includesCommonProps?: true;
9
+ props: Record<string, string>;
10
+ events?: Record<string, string>;
11
+ slots?: Record<string, "single" | "multiple">;
12
+ }
13
+ /**
14
+ * The component schema of the markdown transport, from the per-run
15
+ * manifest. It is smaller than the manifest: each prop is one short type,
16
+ * and the props that many components share verbatim (the layout props of
17
+ * the containers) are listed once in `commonProps`; a component that takes
18
+ * them is flagged `includesCommonProps`.
19
+ * It has the catalog id, the targets with their descriptions and their
20
+ * component lists, the functions with their parameters, the components
21
+ * with their props, events and slots, and the surfaces of the thread. A surface lists the
22
+ * values that the user set and the operations that the client dropped.
23
+ *
24
+ * @param manifest - The manifest from `buildGenerativeUIManifest`.
25
+ * @param options - `propDescriptions` (default true) writes each prop's
26
+ * description; `commonProps` (default true) lists shared props once.
27
+ * @returns A JSON-ready object.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const value = JSON.stringify(
32
+ * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
33
+ * );
34
+ * ```
35
+ */
36
+ declare function buildCompactGenerativeUISchema(manifest: Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">, options?: {
37
+ propDescriptions?: boolean;
38
+ commonProps?: boolean;
39
+ }): {
40
+ components: Record<string, CompactComponent>;
41
+ surfaces: Record<string, GenerativeUISurfaceSummary>;
42
+ commonProps?: Record<string, string> | undefined;
43
+ catalogId: string;
44
+ targets: GenerativeUITarget[];
45
+ functions: {
46
+ [k: string]: {
47
+ callers?: string | undefined;
48
+ parameters: string;
49
+ description?: string | undefined;
50
+ };
51
+ };
52
+ };
53
+ //#endregion
54
+ export { buildCompactGenerativeUISchema };
55
+ //# sourceMappingURL=compact-schema.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compact-schema.d.cts","names":[],"sources":["../../src/generative-ui/compact-schema.ts"],"mappings":";;;;UA2KU,gBAAA;EACR,WAAA;EAGO;EADP,mBAAA;EACA,KAAA,EAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA,GAAQ,MAAA;AAAA;;AA0BV;;;;;;;;;;;;;;;;;;;;;;iBAAgB,8BAAA,CACd,QAAA,EAAU,IAAA,CACR,oBAAA,sEAGF,OAAA;EAAW,gBAAA;EAA4B,WAAA;AAAA"}
@@ -0,0 +1,55 @@
1
+ import { GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./types.mjs";
2
+
3
+ //#region src/generative-ui/compact-schema.d.ts
4
+ /** One component of the compact schema. */
5
+ interface CompactComponent {
6
+ description?: string;
7
+ /** True when the component also takes every prop in `commonProps`. */
8
+ includesCommonProps?: true;
9
+ props: Record<string, string>;
10
+ events?: Record<string, string>;
11
+ slots?: Record<string, "single" | "multiple">;
12
+ }
13
+ /**
14
+ * The component schema of the markdown transport, from the per-run
15
+ * manifest. It is smaller than the manifest: each prop is one short type,
16
+ * and the props that many components share verbatim (the layout props of
17
+ * the containers) are listed once in `commonProps`; a component that takes
18
+ * them is flagged `includesCommonProps`.
19
+ * It has the catalog id, the targets with their descriptions and their
20
+ * component lists, the functions with their parameters, the components
21
+ * with their props, events and slots, and the surfaces of the thread. A surface lists the
22
+ * values that the user set and the operations that the client dropped.
23
+ *
24
+ * @param manifest - The manifest from `buildGenerativeUIManifest`.
25
+ * @param options - `propDescriptions` (default true) writes each prop's
26
+ * description; `commonProps` (default true) lists shared props once.
27
+ * @returns A JSON-ready object.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const value = JSON.stringify(
32
+ * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
33
+ * );
34
+ * ```
35
+ */
36
+ declare function buildCompactGenerativeUISchema(manifest: Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">, options?: {
37
+ propDescriptions?: boolean;
38
+ commonProps?: boolean;
39
+ }): {
40
+ components: Record<string, CompactComponent>;
41
+ surfaces: Record<string, GenerativeUISurfaceSummary>;
42
+ commonProps?: Record<string, string> | undefined;
43
+ catalogId: string;
44
+ targets: GenerativeUITarget[];
45
+ functions: {
46
+ [k: string]: {
47
+ callers?: string | undefined;
48
+ parameters: string;
49
+ description?: string | undefined;
50
+ };
51
+ };
52
+ };
53
+ //#endregion
54
+ export { buildCompactGenerativeUISchema };
55
+ //# sourceMappingURL=compact-schema.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compact-schema.d.mts","names":[],"sources":["../../src/generative-ui/compact-schema.ts"],"mappings":";;;;UA2KU,gBAAA;EACR,WAAA;EAGO;EADP,mBAAA;EACA,KAAA,EAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA,GAAQ,MAAA;AAAA;;AA0BV;;;;;;;;;;;;;;;;;;;;;;iBAAgB,8BAAA,CACd,QAAA,EAAU,IAAA,CACR,oBAAA,sEAGF,OAAA;EAAW,gBAAA;EAA4B,WAAA;AAAA"}