@copilotkit/shared 1.80.0 → 1.81.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/dist/a2ui-envelope.cjs +154 -0
  2. package/dist/a2ui-envelope.cjs.map +1 -0
  3. package/dist/a2ui-envelope.d.cts +101 -0
  4. package/dist/a2ui-envelope.d.cts.map +1 -0
  5. package/dist/a2ui-envelope.d.mts +101 -0
  6. package/dist/a2ui-envelope.d.mts.map +1 -0
  7. package/dist/a2ui-envelope.mjs +151 -0
  8. package/dist/a2ui-envelope.mjs.map +1 -0
  9. package/dist/a2ui-prompts.cjs +139 -0
  10. package/dist/a2ui-prompts.cjs.map +1 -1
  11. package/dist/a2ui-prompts.d.cts +60 -1
  12. package/dist/a2ui-prompts.d.cts.map +1 -1
  13. package/dist/a2ui-prompts.d.mts +60 -1
  14. package/dist/a2ui-prompts.d.mts.map +1 -1
  15. package/dist/a2ui-prompts.mjs +134 -1
  16. package/dist/a2ui-prompts.mjs.map +1 -1
  17. package/dist/generative-ui/basic-functions.cjs +177 -0
  18. package/dist/generative-ui/basic-functions.cjs.map +1 -0
  19. package/dist/generative-ui/basic-functions.d.cts +108 -0
  20. package/dist/generative-ui/basic-functions.d.cts.map +1 -0
  21. package/dist/generative-ui/basic-functions.d.mts +108 -0
  22. package/dist/generative-ui/basic-functions.d.mts.map +1 -0
  23. package/dist/generative-ui/basic-functions.mjs +167 -0
  24. package/dist/generative-ui/basic-functions.mjs.map +1 -0
  25. package/dist/generative-ui/child-refs.cjs +161 -0
  26. package/dist/generative-ui/child-refs.cjs.map +1 -0
  27. package/dist/generative-ui/child-refs.d.cts +68 -0
  28. package/dist/generative-ui/child-refs.d.cts.map +1 -0
  29. package/dist/generative-ui/child-refs.d.mts +68 -0
  30. package/dist/generative-ui/child-refs.d.mts.map +1 -0
  31. package/dist/generative-ui/child-refs.mjs +152 -0
  32. package/dist/generative-ui/child-refs.mjs.map +1 -0
  33. package/dist/generative-ui/is-record.cjs +10 -0
  34. package/dist/generative-ui/is-record.cjs.map +1 -0
  35. package/dist/generative-ui/is-record.mjs +9 -0
  36. package/dist/generative-ui/is-record.mjs.map +1 -0
  37. package/dist/generative-ui/prompt.cjs +91 -0
  38. package/dist/generative-ui/prompt.cjs.map +1 -0
  39. package/dist/generative-ui/prompt.d.cts +13 -0
  40. package/dist/generative-ui/prompt.d.cts.map +1 -0
  41. package/dist/generative-ui/prompt.d.mts +13 -0
  42. package/dist/generative-ui/prompt.d.mts.map +1 -0
  43. package/dist/generative-ui/prompt.mjs +90 -0
  44. package/dist/generative-ui/prompt.mjs.map +1 -0
  45. package/dist/generative-ui/render-ui-args.cjs +160 -0
  46. package/dist/generative-ui/render-ui-args.cjs.map +1 -0
  47. package/dist/generative-ui/render-ui-args.d.cts +40 -0
  48. package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
  49. package/dist/generative-ui/render-ui-args.d.mts +40 -0
  50. package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
  51. package/dist/generative-ui/render-ui-args.mjs +159 -0
  52. package/dist/generative-ui/render-ui-args.mjs.map +1 -0
  53. package/dist/generative-ui/types.cjs +101 -0
  54. package/dist/generative-ui/types.cjs.map +1 -0
  55. package/dist/generative-ui/types.d.cts +270 -0
  56. package/dist/generative-ui/types.d.cts.map +1 -0
  57. package/dist/generative-ui/types.d.mts +270 -0
  58. package/dist/generative-ui/types.d.mts.map +1 -0
  59. package/dist/generative-ui/types.mjs +86 -0
  60. package/dist/generative-ui/types.mjs.map +1 -0
  61. package/dist/generative-ui/validate-operation.cjs +405 -0
  62. package/dist/generative-ui/validate-operation.cjs.map +1 -0
  63. package/dist/generative-ui/validate-operation.d.cts +86 -0
  64. package/dist/generative-ui/validate-operation.d.cts.map +1 -0
  65. package/dist/generative-ui/validate-operation.d.mts +86 -0
  66. package/dist/generative-ui/validate-operation.d.mts.map +1 -0
  67. package/dist/generative-ui/validate-operation.mjs +399 -0
  68. package/dist/generative-ui/validate-operation.mjs.map +1 -0
  69. package/dist/index.cjs +63 -0
  70. package/dist/index.cjs.map +1 -1
  71. package/dist/index.d.cts +9 -2
  72. package/dist/index.d.cts.map +1 -1
  73. package/dist/index.d.mts +9 -2
  74. package/dist/index.d.mts.map +1 -1
  75. package/dist/index.mjs +9 -2
  76. package/dist/index.mjs.map +1 -1
  77. package/dist/index.umd.js +1421 -20
  78. package/dist/index.umd.js.map +1 -1
  79. package/dist/package.cjs +1 -1
  80. package/dist/package.mjs +1 -1
  81. package/dist/utils/dashboard-url.cjs +19 -0
  82. package/dist/utils/dashboard-url.cjs.map +1 -0
  83. package/dist/utils/dashboard-url.mjs +18 -0
  84. package/dist/utils/dashboard-url.mjs.map +1 -0
  85. package/dist/utils/index.cjs +2 -1
  86. package/dist/utils/index.cjs.map +1 -1
  87. package/dist/utils/index.d.cts.map +1 -1
  88. package/dist/utils/index.d.mts.map +1 -1
  89. package/dist/utils/index.mjs +2 -1
  90. package/dist/utils/index.mjs.map +1 -1
  91. package/dist/utils/inspector-learning.cjs +10 -7
  92. package/dist/utils/inspector-learning.cjs.map +1 -1
  93. package/dist/utils/inspector-learning.d.cts.map +1 -1
  94. package/dist/utils/inspector-learning.d.mts.map +1 -1
  95. package/dist/utils/inspector-learning.mjs +11 -7
  96. package/dist/utils/inspector-learning.mjs.map +1 -1
  97. package/dist/utils/inspector-metadata.cjs +2 -3
  98. package/dist/utils/inspector-metadata.cjs.map +1 -1
  99. package/dist/utils/inspector-metadata.d.cts.map +1 -1
  100. package/dist/utils/inspector-metadata.d.mts.map +1 -1
  101. package/dist/utils/inspector-metadata.mjs +3 -3
  102. package/dist/utils/inspector-metadata.mjs.map +1 -1
  103. package/package.json +1 -1
  104. package/src/__tests__/a2ui-envelope.test.ts +434 -0
  105. package/src/__tests__/a2ui-prompts.test.ts +362 -0
  106. package/src/a2ui-envelope.ts +309 -0
  107. package/src/a2ui-prompts.ts +207 -0
  108. package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
  109. package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
  110. package/src/generative-ui/basic-functions.ts +282 -0
  111. package/src/generative-ui/child-refs.ts +210 -0
  112. package/src/generative-ui/index.ts +6 -0
  113. package/src/generative-ui/is-record.ts +4 -0
  114. package/src/generative-ui/prompt.ts +174 -0
  115. package/src/generative-ui/render-ui-args.ts +199 -0
  116. package/src/generative-ui/types.ts +314 -0
  117. package/src/generative-ui/validate-operation.ts +723 -0
  118. package/src/index.ts +15 -0
  119. package/src/utils/dashboard-url.ts +17 -0
  120. package/src/utils/index.ts +7 -1
  121. package/src/utils/inspector-learning.test.ts +58 -0
  122. package/src/utils/inspector-learning.ts +26 -5
  123. package/src/utils/inspector-metadata.test.ts +2 -1
  124. package/src/utils/inspector-metadata.ts +3 -13
  125. package/src/utils.test.ts +71 -0
package/src/index.ts CHANGED
@@ -174,8 +174,23 @@ export function createLicenseContextValue(
174
174
  export {
175
175
  A2UI_DEFAULT_GENERATION_GUIDELINES,
176
176
  A2UI_DEFAULT_DESIGN_GUIDELINES,
177
+ ALL_GENERATIVE_UI_GUIDE_FEATURES,
178
+ buildGenerativeUIMarkdownGuidelines,
179
+ generativeUIGuideFeatures,
180
+ GENERATIVE_UI_MARKDOWN_GUIDELINES,
181
+ GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
182
+ GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION,
177
183
  } from "./a2ui-prompts";
184
+ export type { GenerativeUIGuideFeatures } from "./a2ui-prompts";
185
+ export {
186
+ COPILOTKIT_FORWARDED_PROPS_KEY,
187
+ COPILOTKIT_UI_ENVELOPE_FORMAT,
188
+ envelopeToManifest,
189
+ readCopilotKitUIEnvelope,
190
+ } from "./a2ui-envelope";
191
+ export type { CopilotKitUIEnvelope } from "./a2ui-envelope";
178
192
 
179
193
  export type { DebugEventEnvelope } from "./debug-event-envelope";
180
194
 
181
195
  export { GENERATE_SANDBOXED_UI_DESCRIPTION } from "./open-generative-ui-prompts";
196
+ export * from "./generative-ui";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks dashboard navigation transport and credentials only.
3
+ * Callers must separately enforce the configured origin and allowed paths.
4
+ * This policy does not authorize server-side requests.
5
+ */
6
+ export function isSafeDashboardUrl(url: URL): boolean {
7
+ const local =
8
+ ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname) ||
9
+ /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+localhost$/u.test(
10
+ url.hostname,
11
+ );
12
+ return (
13
+ !url.username &&
14
+ !url.password &&
15
+ (url.protocol === "https:" || (url.protocol === "http:" && local))
16
+ );
17
+ }
@@ -27,13 +27,19 @@ export function parseJson(json: string, fallback: any = "unset") {
27
27
  }
28
28
  }
29
29
 
30
+ // Allow partial values of every type except unterminated numbers. A cut-off
31
+ // number is a *different* number (`{"max":1` is about to become 100, not 1),
32
+ // while a cut-off string is still the beginning of the final string. Numbers
33
+ // therefore only appear once terminated by `,`, `}` or `]`.
34
+ const PARTIAL_JSON_ALLOW = PartialJSON.Allow.ALL & ~PartialJSON.Allow.NUM;
35
+
30
36
  /**
31
37
  * Parses a partial/incomplete JSON string, returning as much valid data as possible.
32
38
  * Falls back to an empty object if parsing fails entirely.
33
39
  */
34
40
  export function partialJSONParse(json: string) {
35
41
  try {
36
- const parsed = PartialJSON.parse(json);
42
+ const parsed = PartialJSON.parse(json, PARTIAL_JSON_ALLOW);
37
43
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
38
44
  return parsed;
39
45
  }
@@ -363,3 +363,61 @@ describe("Inspector Learning wire validation", () => {
363
363
  expect(parseInspectorLearningRequestV1({ skillsPage: 0 })).toBeUndefined();
364
364
  });
365
365
  });
366
+
367
+ it.each([
368
+ "http://localhost:7066",
369
+ "http://127.0.0.1:7066",
370
+ "http://[::1]:7066",
371
+ "http://cpk.localhost:7066",
372
+ "http://team.cpk.localhost:7066",
373
+ "https://dashboard.example.com",
374
+ ])("accepts Learning links on configured dashboard origin %s", (origin) => {
375
+ const url = `${origin}/o/acme/support/learning`;
376
+
377
+ expect(parseInspectorLearningUrl(url, origin)).toBe(url);
378
+ });
379
+
380
+ it.each([
381
+ "http://remote.example.com",
382
+ "http://cpk.localhost.example.com",
383
+ "http://-bad.localhost",
384
+ "http://bad_.localhost",
385
+ "http://bad..localhost",
386
+ "http://user:password@cpk.localhost",
387
+ "ftp://cpk.localhost",
388
+ "javascript:alert(1)",
389
+ ])("rejects unsafe dashboard origin %s", (origin) => {
390
+ expect(
391
+ parseInspectorLearningUrl(`${origin}/o/acme/support/learning`, origin),
392
+ ).toBeUndefined();
393
+ });
394
+
395
+ it.each([
396
+ "http://localhost:7066",
397
+ "http://cpk.localhost:7067",
398
+ "https://cpk.localhost:7066",
399
+ "https://other.example.com",
400
+ ])("rejects a Learning link to another origin %s", (origin) => {
401
+ expect(
402
+ parseInspectorLearningUrl(
403
+ `${origin}/o/acme/support/learning`,
404
+ "http://cpk.localhost:7066",
405
+ ),
406
+ ).toBeUndefined();
407
+ });
408
+
409
+ it.each([
410
+ ["learning", "/account"],
411
+ ["learning", "/o/acme/support/learning?redirect=elsewhere"],
412
+ ["candidates", "/o/acme/support/learning/quality/analysis-results"],
413
+ ["runs", "/o/acme/support/learning/quality/skills"],
414
+ ["runs", "/o/acme/support/learning/quality/analysis-results#fragment"],
415
+ ])("rejects invalid %s Learning path %s", (field, path) => {
416
+ const snapshot = emptySnapshot();
417
+ expect(
418
+ parseInspectorLearningSnapshotV1({
419
+ ...snapshot,
420
+ links: { ...snapshot.links, [field]: `${snapshot.webAppOrigin}${path}` },
421
+ }),
422
+ ).toBeUndefined();
423
+ });
@@ -1,3 +1,5 @@
1
+ import { isSafeDashboardUrl } from "./dashboard-url";
2
+
1
3
  /** A bounded page in the Inspector Learning snapshot. */
2
4
  export interface InspectorLearningPage<T> {
3
5
  readonly page: number;
@@ -132,9 +134,6 @@ export function parseInspectorLearningUrl(
132
134
  try {
133
135
  const parsed = new URL(value);
134
136
  const configured = new URL(trustedOrigin);
135
- const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(
136
- configured.hostname,
137
- );
138
137
  if (
139
138
  parsed.username ||
140
139
  parsed.password ||
@@ -143,8 +142,7 @@ export function parseInspectorLearningUrl(
143
142
  configured.pathname !== "/" ||
144
143
  configured.search ||
145
144
  configured.hash ||
146
- (configured.protocol !== "https:" &&
147
- !(configured.protocol === "http:" && loopback)) ||
145
+ !isSafeDashboardUrl(configured) ||
148
146
  parsed.origin !== configured.origin
149
147
  ) {
150
148
  return undefined;
@@ -155,6 +153,26 @@ export function parseInspectorLearningUrl(
155
153
  }
156
154
  }
157
155
 
156
+ /** Validates the canonical dashboard route after the origin check. */
157
+ function validLearningPath(
158
+ value: string,
159
+ kind: "learning" | "candidates" | "runs",
160
+ ): boolean {
161
+ const url = new URL(value);
162
+ if (url.search || url.hash) return false;
163
+ if (kind === "learning") {
164
+ return (
165
+ url.pathname === "/learning" ||
166
+ /^\/o\/[^/]+\/[^/]+\/learning(?:\/[^/]+)?$/u.test(url.pathname)
167
+ );
168
+ }
169
+ return kind === "candidates"
170
+ ? /^\/o\/[^/]+\/[^/]+\/learning\/[^/]+\/skills$/u.test(url.pathname)
171
+ : /^\/o\/[^/]+\/[^/]+\/learning\/[^/]+\/analysis-results$/u.test(
172
+ url.pathname,
173
+ );
174
+ }
175
+
158
176
  function parseEvidence(value: unknown): InspectorLearningEvidence | undefined {
159
177
  if (record(value) && value.status === "unavailable") {
160
178
  return Object.keys(value).length === 1
@@ -453,6 +471,9 @@ export function parseInspectorLearningSnapshotV1(
453
471
  !learning ||
454
472
  candidates === undefined ||
455
473
  runs === undefined ||
474
+ !validLearningPath(learning, "learning") ||
475
+ (candidates !== null && !validLearningPath(candidates, "candidates")) ||
476
+ (runs !== null && !validLearningPath(runs, "runs")) ||
456
477
  (value.pendingThreadCount > 0 && runs === null) ||
457
478
  (value.pendingCandidateCount > 0 && candidates === null)
458
479
  )
@@ -374,6 +374,8 @@ test.each([
374
374
  "https://cloud.copilotkit.ai/manage",
375
375
  "https://cloud.copilotkit.ai/manage/plan",
376
376
  "http://localhost/manage",
377
+ "http://sub.localhost/manage",
378
+ "http://cpk.localhost:7066/manage",
377
379
  "http://localhost:3000/manage",
378
380
  "http://127.0.0.1:3000/manage",
379
381
  "http://[::1]:3000/manage",
@@ -402,7 +404,6 @@ test.each([
402
404
  "ftp://cloud.copilotkit.ai/manage",
403
405
  "http://cloud.copilotkit.ai/manage",
404
406
  "http://localhost.example.com/manage",
405
- "http://sub.localhost/manage",
406
407
  "http://127.0.0.2/manage",
407
408
  "http://[::2]/manage",
408
409
  "http://0.0.0.0/manage",
@@ -1,3 +1,5 @@
1
+ import { isSafeDashboardUrl } from "./dashboard-url";
2
+
1
3
  /**
2
4
  * Inspector metadata supplied by a trusted CopilotKit runtime.
3
5
  *
@@ -157,19 +159,7 @@ function parseSafeActionUrl(value: unknown): string | undefined {
157
159
  return undefined;
158
160
  }
159
161
 
160
- if (parsed.protocol === "https:") {
161
- return url;
162
- }
163
-
164
- const isLoopbackHost =
165
- parsed.hostname === "localhost" ||
166
- parsed.hostname === "127.0.0.1" ||
167
- parsed.hostname === "[::1]";
168
- if (parsed.protocol === "http:" && isLoopbackHost) {
169
- return url;
170
- }
171
-
172
- return undefined;
162
+ return isSafeDashboardUrl(parsed) ? url : undefined;
173
163
  }
174
164
 
175
165
  function parseAction(value: unknown): InspectorAction | undefined {
package/src/utils.test.ts CHANGED
@@ -33,6 +33,77 @@ describe("partialJSONParse", () => {
33
33
  test("should return {} for an empty string", () => {
34
34
  expect(partialJSONParse("")).toEqual({});
35
35
  });
36
+
37
+ test("should withhold an unterminated trailing number", () => {
38
+ expect(partialJSONParse('{"value":72,"max":1')).toEqual({ value: 72 });
39
+ });
40
+
41
+ test("should withhold a number that is still unterminated at end of input", () => {
42
+ expect(partialJSONParse('{"value":72,"max":100')).toEqual({ value: 72 });
43
+ });
44
+
45
+ test("should reveal a number once terminated", () => {
46
+ expect(partialJSONParse('{"value":72,"max":100}')).toEqual({
47
+ value: 72,
48
+ max: 100,
49
+ });
50
+ });
51
+
52
+ test("should withhold an unterminated trailing array number", () => {
53
+ expect(partialJSONParse('{"data":[10,20,3')).toEqual({ data: [10, 20] });
54
+ expect(partialJSONParse('{"data":[10,20,30]')).toEqual({
55
+ data: [10, 20, 30],
56
+ });
57
+ });
58
+
59
+ test("should withhold unterminated numbers at any depth", () => {
60
+ expect(partialJSONParse('{"nested":{"a":1,"b":2')).toEqual({
61
+ nested: { a: 1 },
62
+ });
63
+ });
64
+
65
+ test("should withhold unterminated negatives, decimals and exponents", () => {
66
+ for (const input of [
67
+ '{"x":-',
68
+ '{"x":-4',
69
+ '{"x":3.',
70
+ '{"x":3.1',
71
+ '{"x":1e',
72
+ '{"x":1e-',
73
+ ]) {
74
+ expect(partialJSONParse(input)).toEqual({});
75
+ }
76
+ });
77
+
78
+ test("should keep negative, decimal and exponent numbers once terminated", () => {
79
+ expect(partialJSONParse('{"x":-4,"y":2')).toEqual({ x: -4 });
80
+ expect(partialJSONParse('{"x":3.1,"y":2')).toEqual({ x: 3.1 });
81
+ expect(partialJSONParse('{"x":1e-3,"y":2')).toEqual({ x: 1e-3 });
82
+ });
83
+
84
+ test("should withhold a number followed only by whitespace", () => {
85
+ expect(partialJSONParse('{"max":100\n')).toEqual({});
86
+ });
87
+
88
+ test("should still allow partial strings, booleans and null", () => {
89
+ expect(partialJSONParse('{"name":"jo')).toEqual({ name: "jo" });
90
+ expect(partialJSONParse('{"value":72,"max":tru')).toEqual({
91
+ value: 72,
92
+ max: true,
93
+ });
94
+ expect(partialJSONParse('{"value":72,"max":nul')).toEqual({
95
+ value: 72,
96
+ max: null,
97
+ });
98
+ });
99
+
100
+ test("should still parse complete numbers", () => {
101
+ expect(partialJSONParse('{"value":72,"max":100}')).toEqual({
102
+ value: 72,
103
+ max: 100,
104
+ });
105
+ expect(partialJSONParse('{"pi":3.14}')).toEqual({ pi: 3.14 });
106
+ });
36
107
  });
37
108
 
38
109
  describe("safeParseToolArgs", () => {