@copilotkit/shared 2.0.0-rc.3 → 2.0.0-rc.31

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 (287) 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/attachments/types.d.cts +1 -3
  19. package/dist/attachments/types.d.cts.map +1 -1
  20. package/dist/attachments/types.d.mts +1 -3
  21. package/dist/attachments/types.d.mts.map +1 -1
  22. package/dist/attachments/utils.cjs +16 -5
  23. package/dist/attachments/utils.cjs.map +1 -1
  24. package/dist/attachments/utils.d.cts +3 -1
  25. package/dist/attachments/utils.d.cts.map +1 -1
  26. package/dist/attachments/utils.d.mts +3 -1
  27. package/dist/attachments/utils.d.mts.map +1 -1
  28. package/dist/attachments/utils.mjs +16 -5
  29. package/dist/attachments/utils.mjs.map +1 -1
  30. package/dist/finalize-events.cjs +16 -0
  31. package/dist/finalize-events.cjs.map +1 -1
  32. package/dist/finalize-events.d.cts.map +1 -1
  33. package/dist/finalize-events.d.mts.map +1 -1
  34. package/dist/finalize-events.mjs +16 -0
  35. package/dist/finalize-events.mjs.map +1 -1
  36. package/dist/generative-ui/basic-functions.cjs +1 -1
  37. package/dist/generative-ui/basic-functions.cjs.map +1 -1
  38. package/dist/generative-ui/basic-functions.mjs +1 -1
  39. package/dist/generative-ui/basic-functions.mjs.map +1 -1
  40. package/dist/generative-ui/child-refs.cjs +3 -3
  41. package/dist/generative-ui/child-refs.cjs.map +1 -1
  42. package/dist/generative-ui/child-refs.d.cts.map +1 -1
  43. package/dist/generative-ui/child-refs.d.mts.map +1 -1
  44. package/dist/generative-ui/child-refs.mjs +3 -3
  45. package/dist/generative-ui/child-refs.mjs.map +1 -1
  46. package/dist/generative-ui/compact-schema.cjs +187 -0
  47. package/dist/generative-ui/compact-schema.cjs.map +1 -0
  48. package/dist/generative-ui/compact-schema.d.cts +55 -0
  49. package/dist/generative-ui/compact-schema.d.cts.map +1 -0
  50. package/dist/generative-ui/compact-schema.d.mts +55 -0
  51. package/dist/generative-ui/compact-schema.d.mts.map +1 -0
  52. package/dist/generative-ui/compact-schema.mjs +185 -0
  53. package/dist/generative-ui/compact-schema.mjs.map +1 -0
  54. package/dist/generative-ui/prompt-spec.cjs +225 -0
  55. package/dist/generative-ui/prompt-spec.cjs.map +1 -0
  56. package/dist/generative-ui/prompt-spec.d.cts +94 -0
  57. package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
  58. package/dist/generative-ui/prompt-spec.d.mts +94 -0
  59. package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
  60. package/dist/generative-ui/prompt-spec.mjs +220 -0
  61. package/dist/generative-ui/prompt-spec.mjs.map +1 -0
  62. package/dist/generative-ui/prompt.cjs +10 -36
  63. package/dist/generative-ui/prompt.cjs.map +1 -1
  64. package/dist/generative-ui/prompt.d.cts +2 -1
  65. package/dist/generative-ui/prompt.d.cts.map +1 -1
  66. package/dist/generative-ui/prompt.d.mts +2 -1
  67. package/dist/generative-ui/prompt.d.mts.map +1 -1
  68. package/dist/generative-ui/prompt.mjs +12 -38
  69. package/dist/generative-ui/prompt.mjs.map +1 -1
  70. package/dist/generative-ui/render-ui-args.cjs +1 -0
  71. package/dist/generative-ui/render-ui-args.cjs.map +1 -1
  72. package/dist/generative-ui/render-ui-args.d.cts.map +1 -1
  73. package/dist/generative-ui/render-ui-args.d.mts.map +1 -1
  74. package/dist/generative-ui/render-ui-args.mjs +1 -0
  75. package/dist/generative-ui/render-ui-args.mjs.map +1 -1
  76. package/dist/generative-ui/schema-render.cjs +242 -0
  77. package/dist/generative-ui/schema-render.cjs.map +1 -0
  78. package/dist/generative-ui/schema-render.d.cts +44 -0
  79. package/dist/generative-ui/schema-render.d.cts.map +1 -0
  80. package/dist/generative-ui/schema-render.d.mts +44 -0
  81. package/dist/generative-ui/schema-render.d.mts.map +1 -0
  82. package/dist/generative-ui/schema-render.mjs +239 -0
  83. package/dist/generative-ui/schema-render.mjs.map +1 -0
  84. package/dist/generative-ui/types.cjs +2 -2
  85. package/dist/generative-ui/types.cjs.map +1 -1
  86. package/dist/generative-ui/types.d.cts +7 -7
  87. package/dist/generative-ui/types.d.cts.map +1 -1
  88. package/dist/generative-ui/types.d.mts +7 -7
  89. package/dist/generative-ui/types.d.mts.map +1 -1
  90. package/dist/generative-ui/types.mjs +2 -2
  91. package/dist/generative-ui/types.mjs.map +1 -1
  92. package/dist/generative-ui/validate-operation.cjs +2 -2
  93. package/dist/generative-ui/validate-operation.cjs.map +1 -1
  94. package/dist/generative-ui/validate-operation.mjs +2 -2
  95. package/dist/generative-ui/validate-operation.mjs.map +1 -1
  96. package/dist/index.cjs +16 -15
  97. package/dist/index.cjs.map +1 -1
  98. package/dist/index.d.cts +10 -10
  99. package/dist/index.d.cts.map +1 -1
  100. package/dist/index.d.mts +10 -10
  101. package/dist/index.d.mts.map +1 -1
  102. package/dist/index.mjs +9 -8
  103. package/dist/index.mjs.map +1 -1
  104. package/dist/index.umd.js +866 -455
  105. package/dist/index.umd.js.map +1 -1
  106. package/dist/package.cjs +1 -1
  107. package/dist/package.mjs +1 -1
  108. package/dist/standard-schema.cjs +20 -7
  109. package/dist/standard-schema.cjs.map +1 -1
  110. package/dist/standard-schema.d.cts +15 -16
  111. package/dist/standard-schema.d.cts.map +1 -1
  112. package/dist/standard-schema.d.mts +15 -16
  113. package/dist/standard-schema.d.mts.map +1 -1
  114. package/dist/standard-schema.mjs +20 -8
  115. package/dist/standard-schema.mjs.map +1 -1
  116. package/dist/telemetry/lambda-client.cjs +1 -1
  117. package/dist/telemetry/lambda-client.cjs.map +1 -1
  118. package/dist/telemetry/lambda-client.d.cts.map +1 -1
  119. package/dist/telemetry/lambda-client.d.mts.map +1 -1
  120. package/dist/telemetry/lambda-client.mjs +1 -1
  121. package/dist/telemetry/lambda-client.mjs.map +1 -1
  122. package/dist/telemetry/telemetry-disabled.cjs +0 -5
  123. package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
  124. package/dist/telemetry/telemetry-disabled.d.cts +0 -5
  125. package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
  126. package/dist/telemetry/telemetry-disabled.d.mts +0 -5
  127. package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
  128. package/dist/telemetry/telemetry-disabled.mjs +0 -5
  129. package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
  130. package/dist/types/message.d.cts +1 -18
  131. package/dist/types/message.d.cts.map +1 -1
  132. package/dist/types/message.d.mts +1 -18
  133. package/dist/types/message.d.mts.map +1 -1
  134. package/dist/utils/errors.cjs +5 -42
  135. package/dist/utils/errors.cjs.map +1 -1
  136. package/dist/utils/errors.d.cts +16 -34
  137. package/dist/utils/errors.d.cts.map +1 -1
  138. package/dist/utils/errors.d.mts +16 -34
  139. package/dist/utils/errors.d.mts.map +1 -1
  140. package/dist/utils/errors.mjs +6 -41
  141. package/dist/utils/errors.mjs.map +1 -1
  142. package/dist/utils/index.cjs +0 -2
  143. package/dist/utils/index.cjs.map +1 -1
  144. package/dist/utils/index.d.cts +4 -6
  145. package/dist/utils/index.d.cts.map +1 -1
  146. package/dist/utils/index.d.mts +4 -6
  147. package/dist/utils/index.d.mts.map +1 -1
  148. package/dist/utils/index.mjs +4 -6
  149. package/dist/utils/index.mjs.map +1 -1
  150. package/dist/utils/inspector-visibility.cjs +25 -3
  151. package/dist/utils/inspector-visibility.cjs.map +1 -1
  152. package/dist/utils/inspector-visibility.d.cts +26 -3
  153. package/dist/utils/inspector-visibility.d.cts.map +1 -1
  154. package/dist/utils/inspector-visibility.d.mts +26 -3
  155. package/dist/utils/inspector-visibility.d.mts.map +1 -1
  156. package/dist/utils/inspector-visibility.mjs +25 -4
  157. package/dist/utils/inspector-visibility.mjs.map +1 -1
  158. package/dist/utils/json-schema.cjs +4 -79
  159. package/dist/utils/json-schema.cjs.map +1 -1
  160. package/dist/utils/json-schema.d.cts +1 -4
  161. package/dist/utils/json-schema.d.cts.map +1 -1
  162. package/dist/utils/json-schema.d.mts +1 -4
  163. package/dist/utils/json-schema.d.mts.map +1 -1
  164. package/dist/utils/json-schema.mjs +5 -78
  165. package/dist/utils/json-schema.mjs.map +1 -1
  166. package/dist/utils/random-id.cjs +13 -26
  167. package/dist/utils/random-id.cjs.map +1 -1
  168. package/dist/utils/random-id.d.cts +6 -3
  169. package/dist/utils/random-id.d.cts.map +1 -1
  170. package/dist/utils/random-id.d.mts +6 -3
  171. package/dist/utils/random-id.d.mts.map +1 -1
  172. package/dist/utils/random-id.mjs +14 -25
  173. package/dist/utils/random-id.mjs.map +1 -1
  174. package/package.json +10 -34
  175. package/scripts/verify-cjs-exports.cjs +1 -1
  176. package/src/__tests__/a2ui-envelope.test.ts +18 -13
  177. package/src/__tests__/a2ui-prompts.test.ts +47 -14
  178. package/src/__tests__/fixtures/prompt-goldens.json +354 -0
  179. package/src/__tests__/prompt-spec.test.ts +191 -0
  180. package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
  181. package/src/__tests__/schema-render.test.ts +245 -0
  182. package/src/__tests__/standard-schema.test.ts +61 -101
  183. package/src/a2ui-envelope.ts +7 -6
  184. package/src/a2ui-prompts.ts +47 -96
  185. package/src/attachments/__tests__/utils.test.ts +22 -0
  186. package/src/attachments/types.ts +1 -3
  187. package/src/attachments/utils.ts +23 -5
  188. package/src/finalize-events.ts +16 -0
  189. package/src/generative-ui/__tests__/render-ui-args.test.ts +7 -7
  190. package/src/generative-ui/__tests__/validate-operation.test.ts +19 -13
  191. package/src/generative-ui/basic-functions.ts +1 -1
  192. package/src/generative-ui/child-refs.ts +5 -4
  193. package/src/generative-ui/compact-schema.ts +277 -0
  194. package/src/generative-ui/index.ts +3 -0
  195. package/src/generative-ui/prompt-spec.ts +335 -0
  196. package/src/generative-ui/prompt.ts +25 -75
  197. package/src/generative-ui/render-ui-args.ts +3 -0
  198. package/src/generative-ui/schema-render.ts +377 -0
  199. package/src/generative-ui/types.ts +10 -7
  200. package/src/generative-ui/validate-operation.ts +2 -2
  201. package/src/index.ts +3 -16
  202. package/src/standard-schema.ts +24 -27
  203. package/src/telemetry/lambda-client.ts +8 -3
  204. package/src/telemetry/model-host.test.ts +2 -5
  205. package/src/telemetry/telemetry-disabled.ts +0 -5
  206. package/src/types/message.ts +1 -19
  207. package/src/utils/__tests__/errors.test.ts +44 -0
  208. package/src/utils/__tests__/json-schema.test.ts +3 -479
  209. package/src/utils/__tests__/random-id.test.ts +37 -0
  210. package/src/utils/errors.ts +28 -85
  211. package/src/utils/index.ts +0 -2
  212. package/src/utils/inspector-visibility.test.ts +26 -0
  213. package/src/utils/inspector-visibility.ts +35 -3
  214. package/src/utils/json-schema.ts +5 -152
  215. package/src/utils/random-id.ts +17 -37
  216. package/tsconfig.json +3 -1
  217. package/tsdown.config.ts +2 -19
  218. package/dist/event-transforms/index.cjs +0 -15
  219. package/dist/event-transforms/index.d.cts +0 -4
  220. package/dist/event-transforms/index.d.mts +0 -4
  221. package/dist/event-transforms/index.mjs +0 -5
  222. package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
  223. package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
  224. package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
  225. package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
  226. package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
  227. package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
  228. package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
  229. package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
  230. package/dist/event-transforms/recorded-events.cjs +0 -59
  231. package/dist/event-transforms/recorded-events.cjs.map +0 -1
  232. package/dist/event-transforms/recorded-events.d.cts +0 -23
  233. package/dist/event-transforms/recorded-events.d.cts.map +0 -1
  234. package/dist/event-transforms/recorded-events.d.mts +0 -23
  235. package/dist/event-transforms/recorded-events.d.mts.map +0 -1
  236. package/dist/event-transforms/recorded-events.mjs +0 -58
  237. package/dist/event-transforms/recorded-events.mjs.map +0 -1
  238. package/dist/telemetry/events.d.cts +0 -72
  239. package/dist/telemetry/events.d.cts.map +0 -1
  240. package/dist/telemetry/events.d.mts +0 -72
  241. package/dist/telemetry/events.d.mts.map +0 -1
  242. package/dist/telemetry/index.cjs +0 -18
  243. package/dist/telemetry/index.d.cts +0 -5
  244. package/dist/telemetry/index.d.mts +0 -5
  245. package/dist/telemetry/index.mjs +0 -6
  246. package/dist/telemetry/telemetry-client.cjs +0 -165
  247. package/dist/telemetry/telemetry-client.cjs.map +0 -1
  248. package/dist/telemetry/telemetry-client.d.cts +0 -87
  249. package/dist/telemetry/telemetry-client.d.cts.map +0 -1
  250. package/dist/telemetry/telemetry-client.d.mts +0 -87
  251. package/dist/telemetry/telemetry-client.d.mts.map +0 -1
  252. package/dist/telemetry/telemetry-client.mjs +0 -164
  253. package/dist/telemetry/telemetry-client.mjs.map +0 -1
  254. package/dist/telemetry/utils.cjs +0 -14
  255. package/dist/telemetry/utils.cjs.map +0 -1
  256. package/dist/telemetry/utils.mjs +0 -13
  257. package/dist/telemetry/utils.mjs.map +0 -1
  258. package/dist/utils/conditions.cjs +0 -32
  259. package/dist/utils/conditions.cjs.map +0 -1
  260. package/dist/utils/conditions.d.cts +0 -31
  261. package/dist/utils/conditions.d.cts.map +0 -1
  262. package/dist/utils/conditions.d.mts +0 -31
  263. package/dist/utils/conditions.d.mts.map +0 -1
  264. package/dist/utils/conditions.mjs +0 -31
  265. package/dist/utils/conditions.mjs.map +0 -1
  266. package/dist/utils/console-styling.cjs +0 -91
  267. package/dist/utils/console-styling.cjs.map +0 -1
  268. package/dist/utils/console-styling.d.cts +0 -65
  269. package/dist/utils/console-styling.d.cts.map +0 -1
  270. package/dist/utils/console-styling.d.mts +0 -65
  271. package/dist/utils/console-styling.d.mts.map +0 -1
  272. package/dist/utils/console-styling.mjs +0 -85
  273. package/dist/utils/console-styling.mjs.map +0 -1
  274. package/src/__tests__/zod-regression.test.ts +0 -319
  275. package/src/event-transforms/index.ts +0 -5
  276. package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
  277. package/src/event-transforms/recorded-events.test.ts +0 -166
  278. package/src/event-transforms/recorded-events.ts +0 -74
  279. package/src/telemetry/events.ts +0 -68
  280. package/src/telemetry/index.ts +0 -9
  281. package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
  282. package/src/telemetry/telemetry-client.test.ts +0 -798
  283. package/src/telemetry/telemetry-client.ts +0 -328
  284. package/src/telemetry/utils.ts +0 -15
  285. package/src/utils/__tests__/conditions.test.ts +0 -161
  286. package/src/utils/conditions.ts +0 -116
  287. package/src/utils/console-styling.ts +0 -126
@@ -41,3 +41,29 @@ describe("shouldEnableInspector", () => {
41
41
  ).toBe(false);
42
42
  });
43
43
  });
44
+
45
+ describe("shouldEnableInspector hostname rule", () => {
46
+ const developmentBrowser = { isBrowser: true, isDevelopment: true };
47
+
48
+ it.each(["localhost", "127.0.0.1", "[::1]"])(
49
+ "shows on the loopback host %s",
50
+ (hostname) => {
51
+ expect(shouldEnableInspector({ ...developmentBrowser, hostname })).toBe(
52
+ true,
53
+ );
54
+ },
55
+ );
56
+
57
+ it.each(["192.168.1.20", "dev.example.com", "my-tunnel.ngrok.app"])(
58
+ "hides on the remote host %s, even when explicitly enabled",
59
+ (hostname) => {
60
+ expect(
61
+ shouldEnableInspector({
62
+ ...developmentBrowser,
63
+ enableInspector: true,
64
+ hostname,
65
+ }),
66
+ ).toBe(false);
67
+ },
68
+ );
69
+ });
@@ -1,17 +1,49 @@
1
+ /**
2
+ * Hosts the Inspector runs on. The Inspector reads agent state, messages and
3
+ * request headers, so it only mounts on a page served from this machine.
4
+ *
5
+ * @internal Framework adapters use this list. Applications must not depend on it.
6
+ */
7
+ export const ɵINSPECTOR_LOOPBACK_HOSTS: readonly string[] = Object.freeze([
8
+ "localhost",
9
+ "127.0.0.1",
10
+ "[::1]",
11
+ ]);
12
+
1
13
  export interface InspectorVisibilityOptions {
2
14
  enableInspector?: boolean;
3
15
  isBrowser: boolean;
4
16
  isDevelopment: boolean;
17
+ /**
18
+ * The page's `location.hostname`. When set, the Inspector is enabled only on
19
+ * a loopback host (see {@link ɵINSPECTOR_LOOPBACK_HOSTS}), so a development
20
+ * build served on a LAN or tunnel address does not expose it.
21
+ */
22
+ hostname?: string;
5
23
  }
6
24
 
7
25
  /**
8
26
  * The Inspector is a development-only browser tool. Consumers may disable it,
9
- * but an explicit `true` never overrides a production or server environment.
27
+ * but an explicit `true` never overrides a production or server environment,
28
+ * or a page served from a non-loopback host.
29
+ *
30
+ * @example
31
+ * shouldEnableInspector({
32
+ * enableInspector,
33
+ * isBrowser: true,
34
+ * isDevelopment: process.env.NODE_ENV === "development",
35
+ * hostname: window.location.hostname,
36
+ * });
10
37
  */
11
38
  export function shouldEnableInspector({
12
39
  enableInspector,
13
40
  isBrowser,
14
41
  isDevelopment,
15
- }: InspectorVisibilityOptions): boolean {
16
- return isBrowser && isDevelopment && enableInspector !== false;
42
+ hostname,
43
+ }: InspectorVisibilityOptions) {
44
+ const isAllowedHost =
45
+ hostname === undefined || ɵINSPECTOR_LOOPBACK_HOSTS.includes(hostname);
46
+ return (
47
+ isBrowser && isDevelopment && enableInspector !== false && isAllowedHost
48
+ );
17
49
  }
@@ -1,5 +1,4 @@
1
- import { z } from "zod";
2
- import { Parameter } from "../types";
1
+ import type { Parameter } from "../types";
3
2
 
4
3
  export type JSONSchemaString = {
5
4
  type: "string";
@@ -233,6 +232,10 @@ function convertAttribute(attribute: Parameter): JSONSchema {
233
232
  ...(properties && { properties }),
234
233
  ...(required && required.length > 0 && { required }),
235
234
  };
235
+ case "string[]":
236
+ case "number[]":
237
+ case "boolean[]":
238
+ case undefined:
236
239
  default:
237
240
  // Handle arrays of primitive types and undefined attribute.type
238
241
  if (attribute.type?.endsWith("[]")) {
@@ -250,153 +253,3 @@ function convertAttribute(attribute: Parameter): JSONSchema {
250
253
  };
251
254
  }
252
255
  }
253
-
254
- export function convertJsonSchemaToZodSchema(
255
- jsonSchema: any,
256
- required: boolean,
257
- definitions?: Record<string, any>,
258
- visitedRefs?: Set<string>,
259
- ): z.ZodSchema {
260
- // Resolve $ref references
261
- if (jsonSchema.$ref && definitions) {
262
- const refPath = jsonSchema.$ref.replace(
263
- /^#\/\$defs\/|^#\/definitions\//,
264
- "",
265
- );
266
-
267
- // Detect circular $ref cycles
268
- const refs = visitedRefs ?? new Set<string>();
269
- if (refs.has(refPath)) {
270
- console.warn(
271
- `[CopilotKit] Circular $ref detected for "${refPath}" — falling back to z.any()`,
272
- );
273
- let schema = z.any();
274
- if (jsonSchema.description) {
275
- schema = schema.describe(jsonSchema.description);
276
- }
277
- return required ? schema : schema.optional();
278
- }
279
-
280
- const resolved = definitions[refPath];
281
- if (resolved) {
282
- // Clone the set so sibling branches don't see each other's visited refs
283
- const nextRefs = new Set(refs);
284
- nextRefs.add(refPath);
285
- return convertJsonSchemaToZodSchema(
286
- resolved,
287
- required,
288
- definitions,
289
- nextRefs,
290
- );
291
- }
292
- }
293
-
294
- // Collect top-level definitions for $ref resolution
295
- const defs = definitions ?? jsonSchema.$defs ?? jsonSchema.definitions;
296
-
297
- // Handle null-union types like ["string", "null"]
298
- if (Array.isArray(jsonSchema.type)) {
299
- const types = jsonSchema.type as string[];
300
- const hasNull = types.includes("null");
301
- const nonNullTypes = types.filter((t: string) => t !== "null");
302
- const resolvedType = nonNullTypes.length > 0 ? nonNullTypes[0] : "string";
303
- const innerSchema = convertJsonSchemaToZodSchema(
304
- { ...jsonSchema, type: resolvedType },
305
- true,
306
- defs,
307
- visitedRefs,
308
- );
309
- let schema = hasNull ? z.union([innerSchema, z.null()]) : innerSchema;
310
- if (jsonSchema.description) {
311
- schema = schema.describe(jsonSchema.description);
312
- }
313
- return required ? schema : schema.optional();
314
- }
315
-
316
- // Handle anyOf / oneOf as z.union
317
- const unionVariants = jsonSchema.anyOf ?? jsonSchema.oneOf;
318
- if (Array.isArray(unionVariants) && unionVariants.length > 0) {
319
- if (unionVariants.length === 1) {
320
- return convertJsonSchemaToZodSchema(
321
- unionVariants[0],
322
- required,
323
- defs,
324
- visitedRefs,
325
- );
326
- }
327
- const schemas = unionVariants.map((v: any) =>
328
- convertJsonSchemaToZodSchema(v, true, defs, visitedRefs),
329
- );
330
- let schema = z.union(
331
- schemas as [z.ZodSchema, z.ZodSchema, ...z.ZodSchema[]],
332
- );
333
- if (jsonSchema.description) {
334
- schema = schema.describe(jsonSchema.description);
335
- }
336
- return required ? schema : schema.optional();
337
- }
338
-
339
- if (jsonSchema.type === "object") {
340
- const spec: { [key: string]: z.ZodSchema } = {};
341
-
342
- if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
343
- return !required ? z.object(spec).optional() : z.object(spec);
344
- }
345
-
346
- for (const [key, value] of Object.entries(jsonSchema.properties)) {
347
- spec[key] = convertJsonSchemaToZodSchema(
348
- value,
349
- jsonSchema.required ? jsonSchema.required.includes(key) : false,
350
- defs,
351
- visitedRefs,
352
- );
353
- }
354
- let schema = z.object(spec).describe(jsonSchema.description);
355
- return required ? schema : schema.optional();
356
- } else if (jsonSchema.type === "string") {
357
- if (jsonSchema.enum && jsonSchema.enum.length > 0) {
358
- let schema = z
359
- .enum(jsonSchema.enum as [string, ...string[]])
360
- .describe(jsonSchema.description);
361
- return required ? schema : schema.optional();
362
- }
363
- let schema = z.string().describe(jsonSchema.description);
364
- return required ? schema : schema.optional();
365
- } else if (jsonSchema.type === "number" || jsonSchema.type === "integer") {
366
- let schema = z.number().describe(jsonSchema.description);
367
- return required ? schema : schema.optional();
368
- } else if (jsonSchema.type === "boolean") {
369
- let schema = z.boolean().describe(jsonSchema.description);
370
- return required ? schema : schema.optional();
371
- } else if (jsonSchema.type === "array") {
372
- let itemSchema = convertJsonSchemaToZodSchema(
373
- jsonSchema.items,
374
- true,
375
- defs,
376
- visitedRefs,
377
- );
378
- let schema = z.array(itemSchema).describe(jsonSchema.description);
379
- return required ? schema : schema.optional();
380
- } else if (jsonSchema.type === "null") {
381
- let schema = z.null().describe(jsonSchema.description);
382
- return required ? schema : schema.optional();
383
- }
384
-
385
- // Fallback: accept any value rather than throwing
386
- console.warn(
387
- `[CopilotKit] Unsupported JSON schema type "${jsonSchema.type ?? "unknown"}" — falling back to z.any()`,
388
- );
389
- let schema = z.any();
390
- if (jsonSchema.description) {
391
- schema = schema.describe(jsonSchema.description);
392
- }
393
- return required ? schema : schema.optional();
394
- }
395
-
396
- export function getZodParameters<T extends [] | Parameter[] | undefined>(
397
- parameters: T,
398
- ): any {
399
- if (!parameters) return z.object({});
400
- const jsonParams = actionParametersToJsonSchema(parameters);
401
- return convertJsonSchemaToZodSchema(jsonParams, true);
402
- }
@@ -1,45 +1,25 @@
1
- import { v4 as uuidv4, validate, v5 as uuidv5 } from "uuid";
2
-
3
1
  export function randomId() {
4
- return "ck-" + uuidv4();
5
- }
6
-
7
- export function randomUUID() {
8
- return uuidv4();
2
+ return "ck-" + randomUUID();
9
3
  }
10
4
 
11
5
  /**
12
- * Recursively converts an object to a serializable form by converting functions to their string representation.
6
+ * Returns an RFC 4122 version 4 UUID. Uses `crypto.randomUUID()` when it
7
+ * exists, and builds the UUID from `crypto.getRandomValues()` when it does not
8
+ * (React Native's Hermes has no `randomUUID`).
13
9
  */
14
- function toSerializable(value: unknown): unknown {
15
- if (typeof value === "function") {
16
- return value.toString();
10
+ export function randomUUID(): string {
11
+ const crypto = globalThis.crypto;
12
+ if (typeof crypto?.randomUUID === "function") {
13
+ return crypto.randomUUID();
17
14
  }
18
- if (Array.isArray(value)) {
19
- return value.map(toSerializable);
15
+ if (typeof crypto?.getRandomValues !== "function") {
16
+ throw new Error(
17
+ "crypto.getRandomValues() is not available. In React Native, install a polyfill such as react-native-get-random-values.",
18
+ );
20
19
  }
21
- if (value !== null && typeof value === "object") {
22
- const result: Record<string, unknown> = {};
23
- for (const key of Object.keys(value)) {
24
- result[key] = toSerializable((value as Record<string, unknown>)[key]);
25
- }
26
- return result;
27
- }
28
- return value;
29
- }
30
-
31
- export function dataToUUID(input: string | object, namespace?: string): string {
32
- const BASE_NAMESPACE = "e4b01160-ff74-4c6e-9b27-d53cd930fe8e";
33
- // Since namespace needs to be a uuid, we are creating a uuid for it.
34
- const boundNamespace = namespace
35
- ? uuidv5(namespace, BASE_NAMESPACE)
36
- : BASE_NAMESPACE;
37
-
38
- const stringInput =
39
- typeof input === "string" ? input : JSON.stringify(toSerializable(input));
40
- return uuidv5(stringInput, boundNamespace);
41
- }
42
-
43
- export function isValidUUID(uuid: string) {
44
- return validate(uuid);
20
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
21
+ bytes[6] = (bytes[6]! & 0x0f) | 0x40;
22
+ bytes[8] = (bytes[8]! & 0x3f) | 0x80;
23
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
24
+ return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10).join("")}`;
45
25
  }
package/tsconfig.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
- "extends": "../tsconfig/base.json",
2
+ "extends": "@copilotkit/typescript-config/base.json",
3
3
  "include": ["."],
4
4
  "compilerOptions": {
5
+ "module": "ESNext",
6
+ "moduleResolution": "Bundler",
5
7
  "target": "es2017",
6
8
  "lib": ["es2017", "dom"],
7
9
  "resolveJsonModule": true
package/tsdown.config.ts CHANGED
@@ -2,13 +2,7 @@ import { defineConfig } from "tsdown";
2
2
 
3
3
  export default defineConfig([
4
4
  {
5
- // Node-only telemetry and event transforms have separate entries.
6
- // The browser-facing root must not re-export either (#4151).
7
- entry: [
8
- "src/index.ts",
9
- "src/telemetry/index.ts",
10
- "src/event-transforms/index.ts",
11
- ],
5
+ entry: ["src/index.ts"],
12
6
  format: ["esm", "cjs"],
13
7
  dts: true,
14
8
  sourcemap: true,
@@ -24,23 +18,12 @@ export default defineConfig([
24
18
  sourcemap: true,
25
19
  target: "es2018",
26
20
  outDir: "dist",
27
- external: [
28
- "zod",
29
- "graphql",
30
- "uuid",
31
- "@ag-ui/core",
32
- "@ag-ui/client",
33
- "partial-json",
34
- ],
21
+ external: ["@ag-ui/core", "@ag-ui/client", "partial-json"],
35
22
  outputOptions(options) {
36
23
  options.entryFileNames = "[name].umd.js";
37
24
  options.globals = {
38
- zod: "Zod",
39
- graphql: "GraphQL",
40
- uuid: "UUID",
41
25
  "@ag-ui/core": "AgUICore",
42
26
  "@ag-ui/client": "AgUIClient",
43
- "@segment/analytics-node": "SegmentAnalyticsNode",
44
27
  "partial-json": "PartialJSON",
45
28
  };
46
29
  return options;
@@ -1,15 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
3
- const require_open_generative_ui_middleware = require('./open-generative-ui-middleware.cjs');
4
- const require_recorded_events = require('./recorded-events.cjs');
5
- let _ag_ui_a2ui_middleware = require("@ag-ui/a2ui-middleware");
6
-
7
- Object.defineProperty(exports, 'A2UIMiddleware', {
8
- enumerable: true,
9
- get: function () {
10
- return _ag_ui_a2ui_middleware.A2UIMiddleware;
11
- }
12
- });
13
- exports.ArgsParser = require_open_generative_ui_middleware.ArgsParser;
14
- exports.OpenGenerativeUIMiddleware = require_open_generative_ui_middleware.OpenGenerativeUIMiddleware;
15
- exports.transformRecordedEvents = require_recorded_events.transformRecordedEvents;
@@ -1,4 +0,0 @@
1
- import { ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware } from "./open-generative-ui-middleware.cjs";
2
- import { RecordedEventTransformOptions, transformRecordedEvents } from "./recorded-events.cjs";
3
- import { A2UIMiddleware, A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
4
- export { A2UIMiddleware, type A2UIMiddlewareConfig, ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware, RecordedEventTransformOptions, transformRecordedEvents };
@@ -1,4 +0,0 @@
1
- import { ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware } from "./open-generative-ui-middleware.mjs";
2
- import { RecordedEventTransformOptions, transformRecordedEvents } from "./recorded-events.mjs";
3
- import { A2UIMiddleware, A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
4
- export { A2UIMiddleware, type A2UIMiddlewareConfig, ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMiddleware, RecordedEventTransformOptions, transformRecordedEvents };
@@ -1,5 +0,0 @@
1
- import { ArgsParser, OpenGenerativeUIMiddleware } from "./open-generative-ui-middleware.mjs";
2
- import { transformRecordedEvents } from "./recorded-events.mjs";
3
- import { A2UIMiddleware } from "@ag-ui/a2ui-middleware";
4
-
5
- export { A2UIMiddleware, ArgsParser, OpenGenerativeUIMiddleware, transformRecordedEvents };
@@ -1,286 +0,0 @@
1
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
- let _ag_ui_client = require("@ag-ui/client");
3
- let rxjs = require("rxjs");
4
- let clarinet = require("clarinet");
5
- clarinet = require_runtime.__toESM(clarinet);
6
-
7
- //#region src/event-transforms/open-generative-ui-middleware.ts
8
- const TOOL_NAME = "generateSandboxedUi";
9
- const ACTIVITY_TYPE = "open-generative-ui";
10
- /**
11
- * Tracks incremental JSON parsing state for a single tool call's arguments.
12
- * Emits activity events via the onEvent callback as parameters complete.
13
- */
14
- var ArgsParser = class {
15
- constructor(toolCallId, onEvent) {
16
- this.currentKey = null;
17
- this.depth = 0;
18
- this.currentArrayKey = null;
19
- this.snapshotEmitted = false;
20
- this.streamingHtmlKey = false;
21
- this.htmlEmittedLength = 0;
22
- this.htmlArrayEmitted = false;
23
- this.params = {};
24
- this.messageId = `${toolCallId}-activity`;
25
- this.onEvent = onEvent;
26
- this.parser = clarinet.default.parser();
27
- this.parser.onopenobject = (key) => {
28
- this.depth++;
29
- if (key !== void 0 && this.depth === 1) {
30
- this.currentKey = key;
31
- this.initHtmlStreaming(key);
32
- }
33
- };
34
- this.parser.onkey = (key) => {
35
- if (this.depth === 1) {
36
- this.currentKey = key;
37
- this.initHtmlStreaming(key);
38
- }
39
- };
40
- this.parser.onvalue = (value) => {
41
- if (this.depth === 1 && this.currentKey) if (this.currentArrayKey) {
42
- const strValue = String(value);
43
- if (this.currentArrayKey === "jsExpressions") {
44
- if (!this.params.jsExpressions) this.params.jsExpressions = [];
45
- this.params.jsExpressions.push(strValue);
46
- } else if (this.currentArrayKey === "placeholderMessages") {
47
- if (!this.params.placeholderMessages) this.params.placeholderMessages = [];
48
- this.params.placeholderMessages.push(strValue);
49
- }
50
- this.emitArrayItemDelta(this.currentArrayKey, strValue);
51
- } else if (this.streamingHtmlKey) {
52
- const fullHtml = value != null ? String(value) : "";
53
- this.params.html = fullHtml || void 0;
54
- this.emitPendingHtml(fullHtml);
55
- this.emitParamDelta("htmlComplete", true);
56
- this.streamingHtmlKey = false;
57
- } else this.setParam(this.currentKey, value);
58
- };
59
- this.parser.onopenarray = () => {
60
- if (this.depth === 1 && this.currentKey) {
61
- const key = this.currentKey;
62
- if (key === "jsExpressions" || key === "placeholderMessages") {
63
- this.currentArrayKey = key;
64
- if (key === "jsExpressions") this.params.jsExpressions = [];
65
- else this.params.placeholderMessages = [];
66
- this.emitParamDelta(key, []);
67
- }
68
- }
69
- };
70
- this.parser.onclosearray = () => {
71
- if (this.depth === 1) {
72
- if (this.currentArrayKey === "jsExpressions") this.emitParamDelta("jsExpressionsComplete", true);
73
- this.currentArrayKey = null;
74
- }
75
- };
76
- this.parser.oncloseobject = () => {
77
- this.depth--;
78
- };
79
- this.parser.onerror = (err) => {
80
- console.warn("[OpenGenerativeUI] JSON parse error in streaming args, resuming:", err?.message ?? err);
81
- this.parser.error = null;
82
- this.parser.resume();
83
- };
84
- }
85
- write(chunk) {
86
- this.parser.write(chunk);
87
- this.flushHtmlChunks();
88
- }
89
- initHtmlStreaming(key) {
90
- if (key === "html") {
91
- this.streamingHtmlKey = true;
92
- this.htmlEmittedLength = 0;
93
- this.htmlArrayEmitted = false;
94
- }
95
- }
96
- /**
97
- * Read clarinet's internal textNode buffer to emit html chunks incrementally.
98
- * Called after every write() so partial string content is emitted as it streams in.
99
- */
100
- flushHtmlChunks() {
101
- if (!this.streamingHtmlKey) return;
102
- const textNode = this.parser.textNode;
103
- if (typeof textNode !== "string") return;
104
- if (textNode.length === this.htmlEmittedLength) return;
105
- this.emitPendingHtml(textNode);
106
- }
107
- /**
108
- * Emit accumulated html content since the last emission.
109
- * Called by flushHtmlChunks and directly when html completes.
110
- */
111
- emitPendingHtml(textNode) {
112
- const newContent = textNode.slice(this.htmlEmittedLength);
113
- if (newContent.length === 0) return;
114
- if (!this.htmlArrayEmitted) {
115
- this.htmlArrayEmitted = true;
116
- this.emitParamDelta("html", []);
117
- }
118
- this.emitArrayItemDelta("html", newContent);
119
- this.htmlEmittedLength = textNode.length;
120
- }
121
- setParam(key, value) {
122
- switch (key) {
123
- case "initialHeight":
124
- this.params.initialHeight = typeof value === "number" ? value : void 0;
125
- if (this.snapshotEmitted) this.emitParamDelta("initialHeight", this.params.initialHeight);
126
- else this.emitSnapshot();
127
- break;
128
- case "css":
129
- this.params.css = value != null ? String(value) : void 0;
130
- this.emitParamDelta("css", this.params.css);
131
- this.emitParamDelta("cssComplete", true);
132
- break;
133
- case "jsFunctions":
134
- this.params.jsFunctions = value != null ? String(value) : void 0;
135
- this.emitParamDelta("jsFunctions", this.params.jsFunctions);
136
- this.emitParamDelta("jsFunctionsComplete", true);
137
- break;
138
- }
139
- }
140
- emitSnapshot() {
141
- if (this.snapshotEmitted) return;
142
- this.snapshotEmitted = true;
143
- const event = {
144
- type: _ag_ui_client.EventType.ACTIVITY_SNAPSHOT,
145
- messageId: this.messageId,
146
- activityType: ACTIVITY_TYPE,
147
- content: {
148
- initialHeight: this.params.initialHeight,
149
- generating: true
150
- }
151
- };
152
- this.onEvent(event);
153
- }
154
- emitParamDelta(key, value) {
155
- if (value === void 0) return;
156
- this.emitSnapshot();
157
- const event = {
158
- type: _ag_ui_client.EventType.ACTIVITY_DELTA,
159
- messageId: this.messageId,
160
- activityType: ACTIVITY_TYPE,
161
- patch: [{
162
- op: "add",
163
- path: `/${key}`,
164
- value
165
- }]
166
- };
167
- this.onEvent(event);
168
- }
169
- emitArrayItemDelta(arrayKey, value) {
170
- this.emitSnapshot();
171
- const event = {
172
- type: _ag_ui_client.EventType.ACTIVITY_DELTA,
173
- messageId: this.messageId,
174
- activityType: ACTIVITY_TYPE,
175
- patch: [{
176
- op: "add",
177
- path: `/${arrayKey}/-`,
178
- value
179
- }]
180
- };
181
- this.onEvent(event);
182
- }
183
- };
184
- var OpenGenerativeUIMiddleware = class extends _ag_ui_client.Middleware {
185
- run(input, next) {
186
- return this.processStream(this.runNextWithState(input, next));
187
- }
188
- processStream(source) {
189
- return new rxjs.Observable((subscriber) => {
190
- let heldRunFinished = null;
191
- const activeParsers = /* @__PURE__ */ new Map();
192
- const heldToolCallEvents = /* @__PURE__ */ new Map();
193
- const flushedToolCalls = /* @__PURE__ */ new Set();
194
- const flushHeldEvents = (toolCallId) => {
195
- if (flushedToolCalls.has(toolCallId)) return;
196
- flushedToolCalls.add(toolCallId);
197
- const held = heldToolCallEvents.get(toolCallId);
198
- if (held) {
199
- for (const e of held) subscriber.next(e);
200
- heldToolCallEvents.delete(toolCallId);
201
- }
202
- };
203
- const subscription = source.subscribe({
204
- next: (eventWithState) => {
205
- const event = eventWithState.event;
206
- if (heldRunFinished) {
207
- subscriber.next(heldRunFinished.event);
208
- heldRunFinished = null;
209
- }
210
- if (event.type === _ag_ui_client.EventType.RUN_FINISHED) {
211
- heldRunFinished = eventWithState;
212
- return;
213
- }
214
- if (event.type === _ag_ui_client.EventType.TOOL_CALL_START) {
215
- const startEvent = event;
216
- if (startEvent.toolCallName === TOOL_NAME) {
217
- heldToolCallEvents.set(startEvent.toolCallId, [event]);
218
- activeParsers.set(startEvent.toolCallId, new ArgsParser(startEvent.toolCallId, (activityEvent) => {
219
- subscriber.next(activityEvent);
220
- flushHeldEvents(startEvent.toolCallId);
221
- }));
222
- return;
223
- }
224
- }
225
- if (event.type === _ag_ui_client.EventType.TOOL_CALL_ARGS) {
226
- const argsEvent = event;
227
- const parser = activeParsers.get(argsEvent.toolCallId);
228
- if (parser) {
229
- if (!flushedToolCalls.has(argsEvent.toolCallId)) heldToolCallEvents.get(argsEvent.toolCallId).push(event);
230
- else subscriber.next(event);
231
- parser.write(argsEvent.delta);
232
- return;
233
- }
234
- }
235
- if (event.type === _ag_ui_client.EventType.TOOL_CALL_END) {
236
- const endEvent = event;
237
- const parser = activeParsers.get(endEvent.toolCallId);
238
- if (parser) {
239
- const completeEvent = {
240
- type: _ag_ui_client.EventType.ACTIVITY_DELTA,
241
- messageId: parser.messageId,
242
- activityType: ACTIVITY_TYPE,
243
- patch: [{
244
- op: "add",
245
- path: "/generating",
246
- value: false
247
- }]
248
- };
249
- subscriber.next(completeEvent);
250
- if (!flushedToolCalls.has(endEvent.toolCallId)) heldToolCallEvents.get(endEvent.toolCallId).push(event);
251
- else subscriber.next(event);
252
- return;
253
- }
254
- }
255
- subscriber.next(event);
256
- },
257
- error: (err) => {
258
- for (const [, events] of heldToolCallEvents) for (const event of events) subscriber.next(event);
259
- heldToolCallEvents.clear();
260
- if (heldRunFinished) {
261
- subscriber.next(heldRunFinished.event);
262
- heldRunFinished = null;
263
- }
264
- subscriber.error(err);
265
- },
266
- complete: () => {
267
- heldToolCallEvents.forEach((_, toolCallId) => {
268
- flushHeldEvents(toolCallId);
269
- });
270
- if (heldRunFinished) {
271
- subscriber.next(heldRunFinished.event);
272
- heldRunFinished = null;
273
- }
274
- activeParsers.clear();
275
- subscriber.complete();
276
- }
277
- });
278
- return () => subscription.unsubscribe();
279
- });
280
- }
281
- };
282
-
283
- //#endregion
284
- exports.ArgsParser = ArgsParser;
285
- exports.OpenGenerativeUIMiddleware = OpenGenerativeUIMiddleware;
286
- //# sourceMappingURL=open-generative-ui-middleware.cjs.map