@copilotkit/react-core 1.83.0 → 1.84.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 (46) hide show
  1. package/dist/{MarkdownSurfaces-DGpjmPaB.mjs → MarkdownSurfaces-DV9P0Gi9.mjs} +55 -13
  2. package/dist/MarkdownSurfaces-DV9P0Gi9.mjs.map +1 -0
  3. package/dist/{MarkdownSurfaces-FF5za2kU.cjs → MarkdownSurfaces-IUaWSJ0E.cjs} +54 -12
  4. package/dist/MarkdownSurfaces-IUaWSJ0E.cjs.map +1 -0
  5. package/dist/{copilotkit-BuIuo-Z1.cjs → copilotkit-BOTf5RHF.cjs} +296 -291
  6. package/dist/copilotkit-BOTf5RHF.cjs.map +1 -0
  7. package/dist/{copilotkit-wqaNanhw.d.cts → copilotkit-C3mvJexb.d.cts} +31 -8
  8. package/dist/copilotkit-C3mvJexb.d.cts.map +1 -0
  9. package/dist/{copilotkit-DyxwF9wL.mjs → copilotkit-CKnD8GeM.mjs} +300 -295
  10. package/dist/copilotkit-CKnD8GeM.mjs.map +1 -0
  11. package/dist/{copilotkit-C765tOUi.d.mts → copilotkit-CnYo662V.d.mts} +31 -8
  12. package/dist/copilotkit-CnYo662V.d.mts.map +1 -0
  13. package/dist/index.cjs +2 -2
  14. package/dist/index.d.cts +1 -1
  15. package/dist/index.d.mts +1 -1
  16. package/dist/index.mjs +2 -2
  17. package/dist/index.umd.js +199 -289
  18. package/dist/index.umd.js.map +1 -1
  19. package/dist/v2/generative-ui.cjs +33 -54
  20. package/dist/v2/generative-ui.cjs.map +1 -1
  21. package/dist/v2/generative-ui.d.cts +64 -64
  22. package/dist/v2/generative-ui.d.mts +64 -64
  23. package/dist/v2/generative-ui.mjs +33 -54
  24. package/dist/v2/generative-ui.mjs.map +1 -1
  25. package/dist/v2/headless.cjs +32 -16
  26. package/dist/v2/headless.cjs.map +1 -1
  27. package/dist/v2/headless.d.cts +2 -0
  28. package/dist/v2/headless.d.cts.map +1 -1
  29. package/dist/v2/headless.d.mts +2 -0
  30. package/dist/v2/headless.d.mts.map +1 -1
  31. package/dist/v2/headless.mjs +33 -17
  32. package/dist/v2/headless.mjs.map +1 -1
  33. package/dist/v2/index.cjs +14 -2
  34. package/dist/v2/index.css +1 -1
  35. package/dist/v2/index.d.cts +2 -2
  36. package/dist/v2/index.d.mts +2 -2
  37. package/dist/v2/index.mjs +3 -3
  38. package/dist/v2/index.umd.js +359 -300
  39. package/dist/v2/index.umd.js.map +1 -1
  40. package/package.json +8 -8
  41. package/dist/MarkdownSurfaces-DGpjmPaB.mjs.map +0 -1
  42. package/dist/MarkdownSurfaces-FF5za2kU.cjs.map +0 -1
  43. package/dist/copilotkit-BuIuo-Z1.cjs.map +0 -1
  44. package/dist/copilotkit-C765tOUi.d.mts.map +0 -1
  45. package/dist/copilotkit-DyxwF9wL.mjs.map +0 -1
  46. package/dist/copilotkit-wqaNanhw.d.cts.map +0 -1
package/dist/index.umd.js CHANGED
@@ -239,6 +239,7 @@ react_markdown = __toESM(react_markdown);
239
239
  chatToggleOpenLabel: "Open chat",
240
240
  chatToggleCloseLabel: "Close chat",
241
241
  chatScrollToBottomLabel: "Scroll to bottom",
242
+ chatConnectingLabel: "Loading conversation",
242
243
  modalHeaderTitle: "CopilotKit Chat",
243
244
  welcomeMessageText: "How can I help you today?",
244
245
  reasoningMessageThinkingLabel: "Thinking…",
@@ -624,7 +625,7 @@ react_markdown = __toESM(react_markdown);
624
625
 
625
626
  //#endregion
626
627
  //#region package.json
627
- var version = "1.83.0";
628
+ var version = "1.84.0";
628
629
 
629
630
  //#endregion
630
631
  //#region src/v2/components/CopilotKitInspector.tsx
@@ -2397,6 +2398,35 @@ react_markdown = __toESM(react_markdown);
2397
2398
  }]));
2398
2399
  }
2399
2400
  /**
2401
+ * The manifest of a catalog, with no React runtime: the components of `ui`,
2402
+ * the built-in functions (minus the names the app declares) and
2403
+ * `options.functions`, the chat target and `options.targets`, and
2404
+ * `options.surfaces`. `buildGenerativeUIManifest` builds the per-run
2405
+ * manifest with it; tools that run outside React use it directly.
2406
+ *
2407
+ * @example
2408
+ * ```ts
2409
+ * const manifest = buildCatalogManifest(standardUI);
2410
+ * manifest.targets; // [{ id: "chat", kind: "chat" }]
2411
+ * ```
2412
+ */
2413
+ function buildCatalogManifest(ui, options = {}) {
2414
+ return {
2415
+ catalogId: ui.id,
2416
+ catalogIds: catalogIdsOf(ui),
2417
+ components: (0, _copilotkit_shared.applyDescriptionOverrides)({ components: ui.manifestComponents }, (0, _copilotkit_shared.defaultGenerativeUIPromptSpec)().descriptions).manifest.components,
2418
+ functions: {
2419
+ ...Object.fromEntries(Object.entries(builtInFunctionManifests()).filter(([name]) => !ui.functions.has(name))),
2420
+ ...options.functions
2421
+ },
2422
+ targets: [{
2423
+ id: _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET,
2424
+ kind: "chat"
2425
+ }, ...options.targets ?? []],
2426
+ surfaces: options.surfaces ?? {}
2427
+ };
2428
+ }
2429
+ /**
2400
2430
  * Builds the per-run manifest. Never contains handlers or renderers.
2401
2431
  *
2402
2432
  * - `functions`: the built-in functions, then the declared functions with
@@ -2411,20 +2441,11 @@ react_markdown = __toESM(react_markdown);
2411
2441
  kind: "inline",
2412
2442
  ...rest
2413
2443
  }));
2414
- return {
2415
- catalogId: ui.id,
2416
- catalogIds: catalogIdsOf(ui),
2417
- components: ui.manifestComponents,
2418
- functions: {
2419
- ...Object.fromEntries(Object.entries(builtInFunctionManifests()).filter(([name]) => !ui.functions.has(name))),
2420
- ...dispatcher.listExposed()
2421
- },
2422
- targets: [{
2423
- id: _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET,
2424
- kind: "chat"
2425
- }, ...inlineTargets],
2444
+ return buildCatalogManifest(ui, {
2445
+ functions: dispatcher.listExposed(),
2446
+ targets: inlineTargets,
2426
2447
  surfaces: threadId ? manager.describeSurfaces(threadId) : {}
2427
- };
2448
+ });
2428
2449
  }
2429
2450
  /**
2430
2451
  * Builds the envelope that a run sends as `forwardedProps.copilotkit.ui`,
@@ -2447,7 +2468,7 @@ react_markdown = __toESM(react_markdown);
2447
2468
  * includeCatalog: true,
2448
2469
  * contextSent: false,
2449
2470
  * });
2450
- * envelope["v1.0"].supportedCatalogIds; // [runtime.ui.id]
2471
+ * envelope["1.0"].supportedCatalogIds; // [runtime.ui.id]
2451
2472
  * ```
2452
2473
  */
2453
2474
  function buildGenerativeUIEnvelope(runtime, threadId, options) {
@@ -2456,7 +2477,7 @@ react_markdown = __toESM(react_markdown);
2456
2477
  const instructions = options.instructions ?? (transport === "markdown" ? (0, _copilotkit_shared.buildGenerativeUIMarkdownGuidelines)((0, _copilotkit_shared.generativeUIGuideFeatures)(manifest)) : void 0);
2457
2478
  const omitComponents = transport === "markdown" && !includeCatalog;
2458
2479
  return {
2459
- "v1.0": {
2480
+ "1.0": {
2460
2481
  supportedCatalogIds: manifest.catalogIds ?? [manifest.catalogId],
2461
2482
  inlineCatalogs: [{
2462
2483
  catalogId: manifest.catalogId,
@@ -2477,258 +2498,6 @@ react_markdown = __toESM(react_markdown);
2477
2498
  };
2478
2499
  }
2479
2500
 
2480
- //#endregion
2481
- //#region src/v2/generative-ui/is-record.ts
2482
- /** True for a plain object: not null, not an array. Internal, not exported. */
2483
- function isRecord(value) {
2484
- return typeof value === "object" && value !== null && !Array.isArray(value);
2485
- }
2486
-
2487
- //#endregion
2488
- //#region src/v2/generative-ui/markdown/context.ts
2489
- /** Objects nested deeper than this show as `object`. */
2490
- const MAX_OBJECT_DEPTH = 2;
2491
- /** A prop must appear verbatim in at least this many components to be hoisted. */
2492
- const COMMON_PROP_MIN_COMPONENTS = 8;
2493
- /** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */
2494
- function schemaList(value) {
2495
- return Array.isArray(value) ? value.filter(isRecord) : [];
2496
- }
2497
- /**
2498
- * Puts a top-level union in parentheses, so that `[]` applies to all of
2499
- * it. A union inside braces needs none.
2500
- */
2501
- function asArrayItem(type) {
2502
- let outside = type;
2503
- for (let inner = outside.replace(/\{[^{}]*\}/g, ""); inner !== outside;) {
2504
- outside = inner;
2505
- inner = outside.replace(/\{[^{}]*\}/g, "");
2506
- }
2507
- return outside.includes(" | ") ? `(${type})[]` : `${type}[]`;
2508
- }
2509
- /**
2510
- * A JSON Schema as a short TypeScript-like type, for example
2511
- * `{ label: string; size?: "sm" | "md" }`.
2512
- */
2513
- function compactType(schema, depth) {
2514
- const ref = (0, _copilotkit_shared.childRefKind)(schema);
2515
- if (ref === "single") return "componentId";
2516
- if (ref === "multiple") return "componentId[] | { componentId: string; path: string }";
2517
- if ("const" in schema) return JSON.stringify(schema.const);
2518
- if (Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
2519
- const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];
2520
- if (variants.length > 0) return variants.map((variant) => compactType(variant, depth)).join(" | ");
2521
- return (Array.isArray(schema.type) ? schema.type : [schema.type]).map((type) => {
2522
- switch (type) {
2523
- case "string":
2524
- case "boolean":
2525
- case "null": return type;
2526
- case "number":
2527
- case "integer": return "number";
2528
- case "array": return isRecord(schema.items) ? asArrayItem(compactType(schema.items, depth)) : "unknown[]";
2529
- case "object": return compactObject(schema, depth);
2530
- default: return "unknown";
2531
- }
2532
- }).join(" | ");
2533
- }
2534
- /** An object schema as `{ key: type; other?: type }`. */
2535
- function compactObject(schema, depth) {
2536
- const properties = isRecord(schema.properties) ? schema.properties : {};
2537
- const entries = Object.entries(properties);
2538
- if (entries.length === 0) return isRecord(schema.additionalProperties) ? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>` : "object";
2539
- if (depth >= MAX_OBJECT_DEPTH) return "object";
2540
- const required = Array.isArray(schema.required) ? schema.required : [];
2541
- return `{ ${entries.map(([key, value]) => {
2542
- const type = isRecord(value) ? compactType(value, depth + 1) : "unknown";
2543
- return `${key}${required.includes(key) ? "" : "?"}: ${type}`;
2544
- }).join("; ")} }`;
2545
- }
2546
- /** A schema as its type, with its description after `//`. */
2547
- function describedType(schema, depth) {
2548
- const type = compactType(schema, depth);
2549
- return typeof schema.description === "string" ? `${type} // ${schema.description}` : type;
2550
- }
2551
- /** The props of a component: `{ "label": "string", "size?": "..." }`. */
2552
- function compactProps(schema) {
2553
- const properties = isRecord(schema.properties) ? schema.properties : {};
2554
- const required = Array.isArray(schema.required) ? schema.required : [];
2555
- const props = {};
2556
- for (const [key, value] of Object.entries(properties)) {
2557
- if (!isRecord(value)) continue;
2558
- const name = required.includes(key) ? key : `${key}?`;
2559
- props[name] = describedType(value, 1);
2560
- }
2561
- return props;
2562
- }
2563
- /**
2564
- * The prop set worth listing once. Candidates are (key, type) pairs that at
2565
- * least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The
2566
- * candidate subset of each component is a possible common set, scored by
2567
- * the characters it saves (set size times the components that contain all
2568
- * of it). `undefined` when no set is shared by enough components.
2569
- */
2570
- function findCommonProps(propsList) {
2571
- const pairKey = (key, type) => `${key}\u0000${type}`;
2572
- const counts = /* @__PURE__ */ new Map();
2573
- for (const props of propsList) for (const [key, type] of Object.entries(props)) {
2574
- const id = pairKey(key, type);
2575
- counts.set(id, (counts.get(id) ?? 0) + 1);
2576
- }
2577
- let best;
2578
- let bestScore = 0;
2579
- const seen = /* @__PURE__ */ new Set();
2580
- for (const props of propsList) {
2581
- const subset = Object.entries(props).filter(([key, type]) => (counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS);
2582
- if (subset.length < 2) continue;
2583
- const signature = subset.map(([key, type]) => pairKey(key, type)).join("\n");
2584
- if (seen.has(signature)) continue;
2585
- seen.add(signature);
2586
- const members = propsList.filter((other) => subset.every(([key, type]) => other[key] === type)).length;
2587
- if (members < COMMON_PROP_MIN_COMPONENTS) continue;
2588
- const score = subset.reduce((total, [key, type]) => total + key.length + type.length, 0) * (members - 1);
2589
- if (score > bestScore) {
2590
- bestScore = score;
2591
- best = Object.fromEntries(subset);
2592
- }
2593
- }
2594
- return best;
2595
- }
2596
- /**
2597
- * The component schema of the markdown transport, from the per-run
2598
- * manifest. It is smaller than the manifest: each prop is one short type,
2599
- * and the props that many components share verbatim (the layout props of
2600
- * the containers) are listed once in `commonProps`; a component that takes
2601
- * them is flagged `includesCommonProps`.
2602
- * It has the catalog id, the targets with their descriptions and their
2603
- * component lists, the functions with their parameters, the components
2604
- * with their props, events and slots, and the surfaces of the thread. A surface lists the
2605
- * values that the user set and the operations that the client dropped.
2606
- *
2607
- * @param manifest - The manifest from `buildGenerativeUIManifest`.
2608
- * @returns A JSON-ready object.
2609
- *
2610
- * @example
2611
- * ```ts
2612
- * const value = JSON.stringify(
2613
- * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
2614
- * );
2615
- * ```
2616
- */
2617
- function buildCompactGenerativeUISchema(manifest) {
2618
- const components = {};
2619
- for (const [name, component] of Object.entries(manifest.components)) {
2620
- const events = Object.entries(component.events ?? {}).map(([event, definition]) => [event, definition.description ?? compactType(definition.parameters, 0)]);
2621
- components[name] = {
2622
- ...component.description ? { description: component.description } : {},
2623
- props: compactProps(component.props),
2624
- ...events.length > 0 ? { events: Object.fromEntries(events) } : {},
2625
- ...component.slots ? { slots: component.slots } : {}
2626
- };
2627
- }
2628
- const common = findCommonProps(Object.values(components).map((component) => component.props));
2629
- if (common) {
2630
- const commonKeys = Object.keys(common);
2631
- for (const component of Object.values(components)) {
2632
- if (!commonKeys.every((key) => component.props[key] === common[key])) continue;
2633
- component.includesCommonProps = true;
2634
- component.props = Object.fromEntries(Object.entries(component.props).filter(([key]) => !(key in common)));
2635
- }
2636
- }
2637
- const functions = Object.fromEntries(Object.entries(manifest.functions).map(([name, fn]) => {
2638
- const signature = fn.parameters.description;
2639
- const hasSignature = typeof signature === "string" && signature.startsWith("{") && Object.keys(fn.parameters).length === 2;
2640
- const callers = (0, _copilotkit_shared.functionCallers)(fn);
2641
- return [name, {
2642
- ...fn.description ? { description: fn.description } : {},
2643
- parameters: hasSignature ? signature : compactType(fn.parameters, 0),
2644
- ...callers.agent && _copilotkit_shared.GENERATIVE_UI_AGENT_FUNCTION_CALLS ? { callers: callers.renderer ? "UI or agent" : "agent only" } : {}
2645
- }];
2646
- }));
2647
- return {
2648
- catalogId: manifest.catalogId,
2649
- targets: manifest.targets,
2650
- functions,
2651
- ...common ? { commonProps: common } : {},
2652
- components,
2653
- surfaces: manifest.surfaces
2654
- };
2655
- }
2656
- /**
2657
- * Registers the two context entries of the markdown transport: the compact
2658
- * component schema and the fence guideline. Both go only to `agentIds`.
2659
- *
2660
- * The schema is built again at each run start and when a
2661
- * `<GenerativeUISurface>` mounts, unmounts, or changes its description or
2662
- * its components, so its targets, functions, and surfaces stay current. The surfaces are those of the running thread.
2663
- *
2664
- * @param copilotkit - The CopilotKit core of the provider.
2665
- * @param runtime - The generative UI runtime parts, or null without one.
2666
- * @param options - `enabled` and `agentIds`.
2667
- *
2668
- * @example
2669
- * ```ts
2670
- * useGenerativeUIMarkdownContext(copilotkit, core, {
2671
- * enabled: config?.transport === "markdown" && config.sendContext !== false,
2672
- * agentIds: config?.agentIds,
2673
- * });
2674
- * ```
2675
- */
2676
- function useGenerativeUIMarkdownContext(copilotkit, runtime, options) {
2677
- const { enabled, agentIds } = options;
2678
- const agentIdsKey = agentIds === void 0 ? "" : JSON.stringify(agentIds);
2679
- (0, react.useEffect)(() => {
2680
- if (!runtime || !enabled) return;
2681
- const scopedAgentIds = agentIdsKey ? JSON.parse(agentIdsKey) : void 0;
2682
- const scope = scopedAgentIds ? { agentIds: scopedAgentIds } : {};
2683
- let guidelineId;
2684
- let guidelineValue;
2685
- let schemaId;
2686
- let schemaValue;
2687
- const refreshSchema = (threadId) => {
2688
- const manifest = buildGenerativeUIManifest(runtime, threadId);
2689
- const guideline = (0, _copilotkit_shared.buildGenerativeUIMarkdownGuidelines)((0, _copilotkit_shared.generativeUIGuideFeatures)(manifest));
2690
- if (guideline !== guidelineValue) {
2691
- if (guidelineId !== void 0) copilotkit.removeContext(guidelineId);
2692
- guidelineValue = guideline;
2693
- guidelineId = copilotkit.addContext({
2694
- description: _copilotkit_shared.GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
2695
- value: guideline,
2696
- ...scope
2697
- });
2698
- }
2699
- const value = JSON.stringify(buildCompactGenerativeUISchema(manifest));
2700
- if (value === schemaValue) return;
2701
- if (schemaId !== void 0) copilotkit.removeContext(schemaId);
2702
- schemaValue = value;
2703
- schemaId = copilotkit.addContext({
2704
- description: _copilotkit_shared.GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION,
2705
- value,
2706
- ...scope
2707
- });
2708
- };
2709
- refreshSchema(runtime.manager.getActiveThread());
2710
- let targetsKey = JSON.stringify(runtime.manager.getMountedTargets());
2711
- const unsubscribeManager = runtime.manager.subscribe(() => {
2712
- const nextKey = JSON.stringify(runtime.manager.getMountedTargets());
2713
- if (nextKey === targetsKey) return;
2714
- targetsKey = nextKey;
2715
- refreshSchema(runtime.manager.getActiveThread());
2716
- });
2717
- const subscription = copilotkit.subscribe({ onAgentRunStarted: ({ agent }) => refreshSchema(agent.threadId) });
2718
- return () => {
2719
- subscription.unsubscribe();
2720
- unsubscribeManager();
2721
- if (guidelineId !== void 0) copilotkit.removeContext(guidelineId);
2722
- if (schemaId !== void 0) copilotkit.removeContext(schemaId);
2723
- };
2724
- }, [
2725
- copilotkit,
2726
- runtime,
2727
- enabled,
2728
- agentIdsKey
2729
- ]);
2730
- }
2731
-
2732
2501
  //#endregion
2733
2502
  //#region src/v2/generative-ui/debug-source.ts
2734
2503
  /**
@@ -2761,7 +2530,7 @@ react_markdown = __toESM(react_markdown);
2761
2530
  }]
2762
2531
  };
2763
2532
  const guideline = (0, _copilotkit_shared.buildGenerativeUIMarkdownGuidelines)((0, _copilotkit_shared.generativeUIGuideFeatures)(manifest));
2764
- const schema = JSON.stringify(buildCompactGenerativeUISchema(manifest));
2533
+ const schema = (0, _copilotkit_shared.renderGenerativeUISchema)(manifest, (0, _copilotkit_shared.defaultGenerativeUIPromptSpec)().schema.markdown);
2765
2534
  return {
2766
2535
  envelope,
2767
2536
  prompt: [{
@@ -3253,6 +3022,91 @@ react_markdown = __toESM(react_markdown);
3253
3022
  };
3254
3023
  }
3255
3024
 
3025
+ //#endregion
3026
+ //#region src/v2/generative-ui/markdown/context.ts
3027
+ /**
3028
+ * Registers the two context entries of the markdown transport: the compact
3029
+ * component schema and the fence guideline. Both go only to `agentIds`.
3030
+ *
3031
+ * The schema is built again at each run start and when a
3032
+ * `<GenerativeUISurface>` mounts, unmounts, or changes its description or
3033
+ * its components, so its targets, functions, and surfaces stay current. The surfaces are those of the running thread.
3034
+ *
3035
+ * @param copilotkit - The CopilotKit core of the provider.
3036
+ * @param runtime - The generative UI runtime parts, or null without one.
3037
+ * @param options - `enabled` and `agentIds`.
3038
+ *
3039
+ * @example
3040
+ * ```ts
3041
+ * useGenerativeUIMarkdownContext(copilotkit, core, {
3042
+ * enabled: config?.transport === "markdown" && config.sendContext !== false,
3043
+ * agentIds: config?.agentIds,
3044
+ * });
3045
+ * ```
3046
+ */
3047
+ function useGenerativeUIMarkdownContext(copilotkit, runtime, options) {
3048
+ const { enabled, agentIds } = options;
3049
+ const agentIdsKey = agentIds === void 0 ? "" : JSON.stringify(agentIds);
3050
+ (0, react.useEffect)(() => {
3051
+ if (!runtime || !enabled) return;
3052
+ const scopedAgentIds = agentIdsKey ? JSON.parse(agentIdsKey) : void 0;
3053
+ const scope = scopedAgentIds ? { agentIds: scopedAgentIds } : {};
3054
+ let guidelineId;
3055
+ let guidelineValue;
3056
+ let schemaId;
3057
+ let schemaValue;
3058
+ const refreshSchema = (threadId) => {
3059
+ const manifest = buildGenerativeUIManifest(runtime, threadId);
3060
+ const guideline = (0, _copilotkit_shared.buildGenerativeUIMarkdownGuidelines)((0, _copilotkit_shared.generativeUIGuideFeatures)(manifest));
3061
+ if (guideline !== guidelineValue) {
3062
+ if (guidelineId !== void 0) copilotkit.removeContext(guidelineId);
3063
+ guidelineValue = guideline;
3064
+ guidelineId = copilotkit.addContext({
3065
+ description: _copilotkit_shared.GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
3066
+ value: guideline,
3067
+ ...scope
3068
+ });
3069
+ }
3070
+ const value = (0, _copilotkit_shared.renderGenerativeUISchema)(manifest, (0, _copilotkit_shared.defaultGenerativeUIPromptSpec)().schema.markdown);
3071
+ if (value === schemaValue) return;
3072
+ if (schemaId !== void 0) copilotkit.removeContext(schemaId);
3073
+ schemaValue = value;
3074
+ schemaId = copilotkit.addContext({
3075
+ description: _copilotkit_shared.GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION,
3076
+ value,
3077
+ ...scope
3078
+ });
3079
+ };
3080
+ refreshSchema(runtime.manager.getActiveThread());
3081
+ let targetsKey = JSON.stringify(runtime.manager.getMountedTargets());
3082
+ const unsubscribeManager = runtime.manager.subscribe(() => {
3083
+ const nextKey = JSON.stringify(runtime.manager.getMountedTargets());
3084
+ if (nextKey === targetsKey) return;
3085
+ targetsKey = nextKey;
3086
+ refreshSchema(runtime.manager.getActiveThread());
3087
+ });
3088
+ const subscription = copilotkit.subscribe({ onAgentRunStarted: ({ agent }) => refreshSchema(agent.threadId) });
3089
+ return () => {
3090
+ subscription.unsubscribe();
3091
+ unsubscribeManager();
3092
+ if (guidelineId !== void 0) copilotkit.removeContext(guidelineId);
3093
+ if (schemaId !== void 0) copilotkit.removeContext(schemaId);
3094
+ };
3095
+ }, [
3096
+ copilotkit,
3097
+ runtime,
3098
+ enabled,
3099
+ agentIdsKey
3100
+ ]);
3101
+ }
3102
+
3103
+ //#endregion
3104
+ //#region src/v2/generative-ui/is-record.ts
3105
+ /** True for a plain object: not null, not an array. Internal, not exported. */
3106
+ function isRecord(value) {
3107
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3108
+ }
3109
+
3256
3110
  //#endregion
3257
3111
  //#region src/v2/generative-ui/markdown/source.ts
3258
3112
  /**
@@ -3901,6 +3755,7 @@ react_markdown = __toESM(react_markdown);
3901
3755
  "*",
3902
3756
  "/",
3903
3757
  "%",
3758
+ "^",
3904
3759
  "(",
3905
3760
  ")",
3906
3761
  ",",
@@ -3987,7 +3842,29 @@ react_markdown = __toESM(react_markdown);
3987
3842
  round: ([x, decimals = 0]) => {
3988
3843
  const factor = 10 ** Math.trunc(decimals);
3989
3844
  return Math.round(x * factor) / factor;
3990
- }
3845
+ },
3846
+ pow: ([x, y]) => x ** y,
3847
+ sqrt: ([x]) => Math.sqrt(x),
3848
+ exp: ([x]) => Math.exp(x),
3849
+ ln: ([x]) => Math.log(x),
3850
+ log10: ([x]) => Math.log10(x)
3851
+ };
3852
+ /** How many args a function takes, at least and at most; min and max take any. */
3853
+ const ARITY = {
3854
+ abs: [1, 1],
3855
+ floor: [1, 1],
3856
+ ceil: [1, 1],
3857
+ round: [1, 2],
3858
+ pow: [2, 2],
3859
+ sqrt: [1, 1],
3860
+ exp: [1, 1],
3861
+ ln: [1, 1],
3862
+ log10: [1, 1]
3863
+ };
3864
+ /** Named constants, when no arg of the same name is passed. */
3865
+ const CONSTANTS = {
3866
+ pi: Math.PI,
3867
+ e: Math.E
3991
3868
  };
3992
3869
  var CalcParser = class {
3993
3870
  constructor(tokens, scope, source) {
@@ -4122,7 +3999,20 @@ react_markdown = __toESM(react_markdown);
4122
3999
  this.pos++;
4123
4000
  return !truthOf(this.unary());
4124
4001
  }
4125
- return this.primary();
4002
+ return this.power();
4003
+ }
4004
+ /** `a ^ b`: binds tighter than `*` and unary minus, right to left. */
4005
+ power() {
4006
+ const base = this.primary();
4007
+ if (!this.peekOp("^")) return base;
4008
+ this.pos++;
4009
+ const exponent = this.unary();
4010
+ if (this.skipDepth > 0) return null;
4011
+ return this.finite(this.num(base, "the left side of ^") ** this.num(exponent, "the right side of ^"), "^");
4012
+ }
4013
+ finite(value, what) {
4014
+ if (!Number.isFinite(value)) this.fail(`${what} has no finite result`);
4015
+ return value;
4126
4016
  }
4127
4017
  primary() {
4128
4018
  const token = this.tokens[this.pos++];
@@ -4157,10 +4047,15 @@ react_markdown = __toESM(react_markdown);
4157
4047
  const fn = Object.prototype.hasOwnProperty.call(MATH, name) ? MATH[name] : void 0;
4158
4048
  if (!fn) this.fail(`unknown function ${name}`);
4159
4049
  if (args.length === 0) this.fail(`${name} needs an argument`);
4160
- return fn(args);
4050
+ const [least, most] = ARITY[name] ?? [1, Infinity];
4051
+ if (args.length < least || args.length > most) this.fail(`${name} takes ${least === most ? least : `${least} or ${most}`} argument${most === 1 ? "" : "s"}`);
4052
+ return this.finite(fn(args), name);
4161
4053
  }
4162
4054
  if (this.skipDepth > 0) return null;
4163
- if (!Object.prototype.hasOwnProperty.call(this.scope, name)) this.fail(`unknown name ${name}; pass it as an arg`);
4055
+ if (!Object.prototype.hasOwnProperty.call(this.scope, name)) {
4056
+ if (Object.prototype.hasOwnProperty.call(CONSTANTS, name)) return CONSTANTS[name];
4057
+ this.fail(`unknown name ${name}; pass it as an arg`);
4058
+ }
4164
4059
  const value = this.scope[name];
4165
4060
  if (value !== null && typeof value !== "number" && typeof value !== "string" && typeof value !== "boolean") this.fail(`${name} is not a number, text or boolean (${describe(value)})`);
4166
4061
  return value;
@@ -7908,6 +7803,7 @@ react_markdown = __toESM(react_markdown);
7908
7803
 
7909
7804
  //#endregion
7910
7805
  //#region src/v2/hooks/use-interrupt.tsx
7806
+ /** Recognize thenables returned by interrupt preprocessing handlers. */
7911
7807
  function isPromiseLike(value) {
7912
7808
  return (typeof value === "object" || typeof value === "function") && value !== null && typeof Reflect.get(value, "then") === "function";
7913
7809
  }
@@ -7958,7 +7854,6 @@ react_markdown = __toESM(react_markdown);
7958
7854
  const [handlerResult, setHandlerResult] = (0, react.useState)(null);
7959
7855
  const interruptStateRef = (0, react.useRef)(new _copilotkit_core.ɵInterruptState());
7960
7856
  (0, react.useEffect)(() => {
7961
- const interruptState = interruptStateRef.current;
7962
7857
  let localLegacy = null;
7963
7858
  let localStandard = null;
7964
7859
  const subscription = agent.subscribe({
@@ -7971,19 +7866,21 @@ react_markdown = __toESM(react_markdown);
7971
7866
  onRunFinishedEvent: (params) => {
7972
7867
  if (params.outcome === "interrupt") localStandard = params.interrupts;
7973
7868
  },
7974
- onRunStartedEvent: () => {
7869
+ onRunStartedEvent: (params) => {
7870
+ if (!agent.pendingInterrupts?.length || params?.input?.resume !== void 0) (0, _copilotkit_core.ɵclearAgentInterruptState)(agent);
7975
7871
  localLegacy = null;
7976
7872
  localStandard = null;
7977
- interruptState.clear();
7873
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
7978
7874
  setPending(null);
7979
7875
  },
7980
7876
  onRunFinalized: () => {
7981
7877
  if (localStandard && localStandard.length > 0) {
7982
- interruptState.setStandard(localStandard);
7983
- setPending(interruptState.pending);
7878
+ interruptStateRef.current = (0, _copilotkit_core.ɵgetAgentInterruptState)(agent, localStandard);
7879
+ setPending(interruptStateRef.current.pending);
7984
7880
  } else if (localLegacy) {
7985
- interruptState.setLegacy(localLegacy);
7986
- setPending(interruptState.pending);
7881
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
7882
+ interruptStateRef.current.setLegacy(localLegacy);
7883
+ setPending(interruptStateRef.current.pending);
7987
7884
  }
7988
7885
  localLegacy = null;
7989
7886
  localStandard = null;
@@ -7991,20 +7888,27 @@ react_markdown = __toESM(react_markdown);
7991
7888
  onRunFailed: () => {
7992
7889
  localLegacy = null;
7993
7890
  localStandard = null;
7994
- interruptState.clear();
7891
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
7995
7892
  setPending(null);
7996
7893
  }
7997
7894
  });
7895
+ interruptStateRef.current = agent.pendingInterrupts?.length ? (0, _copilotkit_core.ɵgetAgentInterruptState)(agent) : new _copilotkit_core.ɵInterruptState();
7896
+ setPending(interruptStateRef.current.pending);
7998
7897
  return () => {
7999
7898
  subscription.unsubscribe();
8000
- interruptState.clear();
7899
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8001
7900
  };
8002
7901
  }, [agent]);
7902
+ /** Record an explicit answer and resume once the complete batch is addressed. */
8003
7903
  const resolve = (0, react.useCallback)(async (payload, interruptId) => {
8004
7904
  const current = pendingRef.current;
8005
7905
  if (!current) return;
8006
7906
  if (current.kind === "standard" && current.interrupts.length > 1 && interruptId === void 0) console.warn(`[CopilotKit] useInterrupt: resolve()/cancel() called without an interruptId while ${current.interrupts.length} interrupts are open; defaulting to the first. Pass an interruptId to address a specific interrupt.`);
8007
- const decision = interruptStateRef.current.resolve(payload, interruptId);
7907
+ if (interruptStateRef.current.isCompletedToolCall(interruptId, agent.messages)) {
7908
+ console.warn("[CopilotKit] useInterrupt: ignoring an answer for an already completed frontend tool call.");
7909
+ return;
7910
+ }
7911
+ const decision = interruptStateRef.current.resolve(payload, interruptId, agent.messages);
8008
7912
  if (decision.kind === "legacy-resume") try {
8009
7913
  return await copilotkit.runAgent({
8010
7914
  agent,
@@ -8020,7 +7924,7 @@ react_markdown = __toESM(react_markdown);
8020
7924
  }
8021
7925
  if (decision.kind === "expired") {
8022
7926
  console.error(`[CopilotKit] useInterrupt: interrupt ${decision.interrupt.id} expired at ${decision.interrupt.expiresAt}; not resuming.`);
8023
- interruptStateRef.current.clear();
7927
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8024
7928
  setPending(null);
8025
7929
  return;
8026
7930
  }
@@ -8038,25 +7942,30 @@ react_markdown = __toESM(react_markdown);
8038
7942
  });
8039
7943
  } catch (err) {
8040
7944
  console.error("[CopilotKit] useInterrupt resolve: runAgent rejected; clearing pending + rethrowing", err);
8041
- interruptStateRef.current.clear();
7945
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8042
7946
  setPending(null);
8043
7947
  throw err;
8044
7948
  }
8045
7949
  }, [agent, copilotkit]);
7950
+ /** Cancel a standard interrupt or dismiss a legacy interrupt locally. */
8046
7951
  const cancel = (0, react.useCallback)(async (interruptId) => {
8047
7952
  const current = pendingRef.current;
8048
7953
  if (!current) return;
8049
7954
  if (current.kind === "standard" && current.interrupts.length > 1 && interruptId === void 0) console.warn(`[CopilotKit] useInterrupt: resolve()/cancel() called without an interruptId while ${current.interrupts.length} interrupts are open; defaulting to the first. Pass an interruptId to address a specific interrupt.`);
8050
- const decision = interruptStateRef.current.cancel(interruptId);
7955
+ if (interruptStateRef.current.isCompletedToolCall(interruptId, agent.messages)) {
7956
+ console.warn("[CopilotKit] useInterrupt: ignoring an answer for an already completed frontend tool call.");
7957
+ return;
7958
+ }
7959
+ const decision = interruptStateRef.current.cancel(interruptId, agent.messages);
8051
7960
  if (decision.kind === "dismiss") {
8052
7961
  console.warn("[CopilotKit] useInterrupt: cancel() is not supported for legacy on_interrupt interrupts; dismissing.");
8053
- interruptStateRef.current.clear();
7962
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8054
7963
  setPending(null);
8055
7964
  return;
8056
7965
  }
8057
7966
  if (decision.kind === "expired") {
8058
7967
  console.error(`[CopilotKit] useInterrupt: interrupt ${decision.interrupt.id} expired at ${decision.interrupt.expiresAt}; not resuming.`);
8059
- interruptStateRef.current.clear();
7968
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8060
7969
  setPending(null);
8061
7970
  return;
8062
7971
  }
@@ -8074,7 +7983,7 @@ react_markdown = __toESM(react_markdown);
8074
7983
  });
8075
7984
  } catch (err) {
8076
7985
  console.error("[CopilotKit] useInterrupt resolve: runAgent rejected; clearing pending + rethrowing", err);
8077
- interruptStateRef.current.clear();
7986
+ interruptStateRef.current = new _copilotkit_core.ɵInterruptState();
8078
7987
  setPending(null);
8079
7988
  throw err;
8080
7989
  }
@@ -8089,6 +7998,7 @@ react_markdown = __toESM(react_markdown);
8089
7998
  resolveRef.current = resolve;
8090
7999
  const cancelRef = (0, react.useRef)(cancel);
8091
8000
  cancelRef.current = cancel;
8001
+ /** Evaluate the consumer filter, logging failures and disabling their UI. */
8092
8002
  const isEnabled = (event) => {
8093
8003
  const predicate = enabledRef.current;
8094
8004
  if (!predicate) return true;