@copilotkit/react-core 1.83.0 → 2.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/{runtime-context-CrHq-iTf.d.mts → MarkdownRendererContext-B4Us6mwL.d.cts} +127 -97
  2. package/dist/MarkdownRendererContext-B4Us6mwL.d.cts.map +1 -0
  3. package/dist/{runtime-context-D3NcP9oE.d.cts → MarkdownRendererContext-CRKFzsEB.d.mts} +127 -97
  4. package/dist/MarkdownRendererContext-CRKFzsEB.d.mts.map +1 -0
  5. package/dist/{MarkdownSurfaces-FF5za2kU.cjs → MarkdownSurfaces-C2XZISKn.cjs} +203 -55
  6. package/dist/MarkdownSurfaces-C2XZISKn.cjs.map +1 -0
  7. package/dist/{MarkdownSurfaces-DGpjmPaB.mjs → MarkdownSurfaces-ChSlVYOk.mjs} +171 -47
  8. package/dist/MarkdownSurfaces-ChSlVYOk.mjs.map +1 -0
  9. package/dist/{v2/context.cjs → context.cjs} +2 -2
  10. package/dist/context.cjs.map +1 -0
  11. package/dist/{v2/context.d.cts → context.d.cts} +5 -5
  12. package/dist/context.d.cts.map +1 -0
  13. package/dist/{v2/context.d.mts → context.d.mts} +5 -5
  14. package/dist/context.d.mts.map +1 -0
  15. package/dist/{v2/context.mjs → context.mjs} +2 -2
  16. package/dist/context.mjs.map +1 -0
  17. package/dist/{v2/generative-ui.cjs → generative-ui.cjs} +40 -43
  18. package/dist/generative-ui.cjs.map +1 -0
  19. package/dist/{v2/generative-ui.d.cts → generative-ui.d.cts} +14 -12
  20. package/dist/generative-ui.d.cts.map +1 -0
  21. package/dist/{v2/generative-ui.d.mts → generative-ui.d.mts} +14 -12
  22. package/dist/generative-ui.d.mts.map +1 -0
  23. package/dist/{v2/generative-ui.mjs → generative-ui.mjs} +40 -43
  24. package/dist/generative-ui.mjs.map +1 -0
  25. package/dist/{v2/headless.cjs → headless.cjs} +68 -53
  26. package/dist/headless.cjs.map +1 -0
  27. package/dist/{v2/headless.d.cts → headless.d.cts} +24 -23
  28. package/dist/headless.d.cts.map +1 -0
  29. package/dist/{v2/headless.d.mts → headless.d.mts} +21 -20
  30. package/dist/headless.d.mts.map +1 -0
  31. package/dist/{v2/headless.mjs → headless.mjs} +51 -36
  32. package/dist/headless.mjs.map +1 -0
  33. package/dist/index.cjs +15391 -1862
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.css +2 -0
  36. package/dist/index.d.cts +3252 -1370
  37. package/dist/index.d.cts.map +1 -1
  38. package/dist/index.d.mts +3234 -1369
  39. package/dist/index.d.mts.map +1 -1
  40. package/dist/index.mjs +13733 -1815
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/index.umd.js +11944 -5481
  43. package/dist/index.umd.js.map +1 -1
  44. package/package.json +21 -30
  45. package/dist/MarkdownSurfaces-DGpjmPaB.mjs.map +0 -1
  46. package/dist/MarkdownSurfaces-FF5za2kU.cjs.map +0 -1
  47. package/dist/copilotkit-BuIuo-Z1.cjs +0 -16531
  48. package/dist/copilotkit-BuIuo-Z1.cjs.map +0 -1
  49. package/dist/copilotkit-C765tOUi.d.mts +0 -4112
  50. package/dist/copilotkit-C765tOUi.d.mts.map +0 -1
  51. package/dist/copilotkit-DyxwF9wL.mjs +0 -16025
  52. package/dist/copilotkit-DyxwF9wL.mjs.map +0 -1
  53. package/dist/copilotkit-wqaNanhw.d.cts +0 -4129
  54. package/dist/copilotkit-wqaNanhw.d.cts.map +0 -1
  55. package/dist/runtime-context-CrHq-iTf.d.mts.map +0 -1
  56. package/dist/runtime-context-D3NcP9oE.d.cts.map +0 -1
  57. package/dist/v2/context.cjs.map +0 -1
  58. package/dist/v2/context.d.cts.map +0 -1
  59. package/dist/v2/context.d.mts.map +0 -1
  60. package/dist/v2/context.mjs.map +0 -1
  61. package/dist/v2/generative-ui.cjs.map +0 -1
  62. package/dist/v2/generative-ui.d.cts.map +0 -1
  63. package/dist/v2/generative-ui.d.mts.map +0 -1
  64. package/dist/v2/generative-ui.mjs.map +0 -1
  65. package/dist/v2/headless.cjs.map +0 -1
  66. package/dist/v2/headless.d.cts.map +0 -1
  67. package/dist/v2/headless.d.mts.map +0 -1
  68. package/dist/v2/headless.mjs.map +0 -1
  69. package/dist/v2/index.cjs +0 -1635
  70. package/dist/v2/index.css +0 -2
  71. package/dist/v2/index.d.cts +0 -4
  72. package/dist/v2/index.d.mts +0 -4
  73. package/dist/v2/index.mjs +0 -8
  74. package/dist/v2/index.umd.js +0 -20695
  75. package/dist/v2/index.umd.js.map +0 -1
@@ -1,13 +1,13 @@
1
- import { CopilotKitContext, CopilotKitCoreReact, useCopilotKit, useCopilotKit as useCopilotKit$1, useDefaultAgentId } from "@copilotkit/react-core/v2/context";
1
+ import { CopilotKitContext, CopilotKitCoreReact, useCopilotKit, useCopilotKit as useCopilotKit$1, useDefaultAgentId } from "@copilotkit/react-core/context";
2
2
  import React, { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
3
3
  import { DEFAULT_AGENT_ID, partialJSONParse, randomUUID } from "@copilotkit/shared";
4
4
  import { Fragment, jsx } from "react/jsx-runtime";
5
- import { CopilotKitCoreRuntimeConnectionStatus, ProxiedCopilotRuntimeAgent, ToolCallStatus, ɵInterruptState, ɵcreateThreadStore, ɵisHttpAgent, ɵselectFetchMoreError, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectIsMutating, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading } from "@copilotkit/core";
5
+ import { CopilotKitCoreRuntimeConnectionStatus, ProxiedCopilotRuntimeAgent, ToolCallStatus, ɵInterruptState, ɵclearAgentInterruptState, ɵcreateThreadStore, ɵgetAgentInterruptState, ɵisHttpAgent, ɵselectFetchMoreError, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectIsMutating, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading } from "@copilotkit/core";
6
6
  import { randomUUID as randomUUID$1 } from "@ag-ui/client";
7
7
  import { z } from "zod";
8
8
  import { contentToText } from "@ag-ui/core";
9
9
 
10
- //#region src/v2/lib/shallow-stable-ref.ts
10
+ //#region src/lib/shallow-stable-ref.ts
11
11
  /**
12
12
  * Shallow equality comparison for objects.
13
13
  */
@@ -49,7 +49,7 @@ function useShallowStableRef(value) {
49
49
  }
50
50
 
51
51
  //#endregion
52
- //#region src/v2/providers/CopilotChatConfigurationProvider.tsx
52
+ //#region src/providers/CopilotChatConfigurationProvider.tsx
53
53
  const CopilotChatDefaultLabels = {
54
54
  chatInputPlaceholder: "Type a message...",
55
55
  chatInputToolbarStartTranscribeButtonLabel: "Transcribe",
@@ -352,7 +352,7 @@ const ControlledModalOpenScope = ({ children, open, onOpenChange }) => {
352
352
  ControlledModalOpenScope.displayName = "ControlledModalOpenScope";
353
353
 
354
354
  //#endregion
355
- //#region src/v2/hooks/use-agent.tsx
355
+ //#region src/hooks/use-agent.tsx
356
356
  let UseAgentUpdate = /* @__PURE__ */ function(UseAgentUpdate) {
357
357
  UseAgentUpdate["OnMessagesChanged"] = "OnMessagesChanged";
358
358
  UseAgentUpdate["OnStateChanged"] = "OnStateChanged";
@@ -550,7 +550,7 @@ function useAgent({ agentId, threadId, runtimeAgentId, updates, throttleMs } = {
550
550
  }
551
551
 
552
552
  //#endregion
553
- //#region src/v2/hooks/tool-registration.ts
553
+ //#region src/hooks/tool-registration.ts
554
554
  /**
555
555
  * Deps comparison shared by `useFrontendTool` (and through it
556
556
  * `useHumanInTheLoop` and `useComponent`) and `useRenderTool`. Not part of the
@@ -625,7 +625,7 @@ function useDepsVersion(deps) {
625
625
  }
626
626
 
627
627
  //#endregion
628
- //#region src/v2/hooks/use-frontend-tool.tsx
628
+ //#region src/hooks/use-frontend-tool.tsx
629
629
  function useFrontendTool(tool, deps) {
630
630
  const { copilotkit } = useCopilotKit$1();
631
631
  const depsVersion = useDepsVersion(deps);
@@ -658,7 +658,7 @@ function useFrontendTool(tool, deps) {
658
658
  }
659
659
 
660
660
  //#endregion
661
- //#region src/v2/hooks/use-frontend-tools.tsx
661
+ //#region src/hooks/use-frontend-tools.tsx
662
662
  const EMPTY_DEPS = [];
663
663
  const EMPTY_TOOLS = [];
664
664
  /**
@@ -755,7 +755,7 @@ function useFrontendTools(tools, deps) {
755
755
  }
756
756
 
757
757
  //#endregion
758
- //#region src/v2/hooks/use-component.tsx
758
+ //#region src/hooks/use-component.tsx
759
759
  /**
760
760
  * Registers a React component as a frontend tool renderer in chat.
761
761
  *
@@ -823,7 +823,7 @@ function useComponent(config, deps) {
823
823
  }
824
824
 
825
825
  //#endregion
826
- //#region src/v2/hooks/use-human-in-the-loop.tsx
826
+ //#region src/hooks/use-human-in-the-loop.tsx
827
827
  /** Registration name of a catch-all tool: handles any otherwise-unhandled call. */
828
828
  const WILDCARD_TOOL_NAME = "*";
829
829
  function useHumanInTheLoop(tool, deps) {
@@ -916,12 +916,13 @@ function useHumanInTheLoop(tool, deps) {
916
916
  }
917
917
 
918
918
  //#endregion
919
- //#region src/v2/types/interrupt.ts
919
+ //#region src/types/interrupt.ts
920
920
  /** Name of the legacy custom event agents emit to signal an interrupt. */
921
921
  const INTERRUPT_EVENT_NAME = "on_interrupt";
922
922
 
923
923
  //#endregion
924
- //#region src/v2/hooks/use-interrupt.tsx
924
+ //#region src/hooks/use-interrupt.tsx
925
+ /** Recognize thenables returned by interrupt preprocessing handlers. */
925
926
  function isPromiseLike(value) {
926
927
  return (typeof value === "object" || typeof value === "function") && value !== null && typeof Reflect.get(value, "then") === "function";
927
928
  }
@@ -972,7 +973,6 @@ function useInterrupt(config) {
972
973
  const [handlerResult, setHandlerResult] = useState(null);
973
974
  const interruptStateRef = useRef(new ɵInterruptState());
974
975
  useEffect(() => {
975
- const interruptState = interruptStateRef.current;
976
976
  let localLegacy = null;
977
977
  let localStandard = null;
978
978
  const subscription = agent.subscribe({
@@ -985,19 +985,21 @@ function useInterrupt(config) {
985
985
  onRunFinishedEvent: (params) => {
986
986
  if (params.outcome === "interrupt") localStandard = params.interrupts;
987
987
  },
988
- onRunStartedEvent: () => {
988
+ onRunStartedEvent: (params) => {
989
+ if (!agent.pendingInterrupts?.length || params?.input?.resume !== void 0) ɵclearAgentInterruptState(agent);
989
990
  localLegacy = null;
990
991
  localStandard = null;
991
- interruptState.clear();
992
+ interruptStateRef.current = new ɵInterruptState();
992
993
  setPending(null);
993
994
  },
994
995
  onRunFinalized: () => {
995
996
  if (localStandard && localStandard.length > 0) {
996
- interruptState.setStandard(localStandard);
997
- setPending(interruptState.pending);
997
+ interruptStateRef.current = ɵgetAgentInterruptState(agent, localStandard);
998
+ setPending(interruptStateRef.current.pending);
998
999
  } else if (localLegacy) {
999
- interruptState.setLegacy(localLegacy);
1000
- setPending(interruptState.pending);
1000
+ interruptStateRef.current = new ɵInterruptState();
1001
+ interruptStateRef.current.setLegacy(localLegacy);
1002
+ setPending(interruptStateRef.current.pending);
1001
1003
  }
1002
1004
  localLegacy = null;
1003
1005
  localStandard = null;
@@ -1005,20 +1007,27 @@ function useInterrupt(config) {
1005
1007
  onRunFailed: () => {
1006
1008
  localLegacy = null;
1007
1009
  localStandard = null;
1008
- interruptState.clear();
1010
+ interruptStateRef.current = new ɵInterruptState();
1009
1011
  setPending(null);
1010
1012
  }
1011
1013
  });
1014
+ interruptStateRef.current = agent.pendingInterrupts?.length ? ɵgetAgentInterruptState(agent) : new ɵInterruptState();
1015
+ setPending(interruptStateRef.current.pending);
1012
1016
  return () => {
1013
1017
  subscription.unsubscribe();
1014
- interruptState.clear();
1018
+ interruptStateRef.current = new ɵInterruptState();
1015
1019
  };
1016
1020
  }, [agent]);
1021
+ /** Record an explicit answer and resume once the complete batch is addressed. */
1017
1022
  const resolve = useCallback(async (payload, interruptId) => {
1018
1023
  const current = pendingRef.current;
1019
1024
  if (!current) return;
1020
1025
  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.`);
1021
- const decision = interruptStateRef.current.resolve(payload, interruptId);
1026
+ if (interruptStateRef.current.isCompletedToolCall(interruptId, agent.messages)) {
1027
+ console.warn("[CopilotKit] useInterrupt: ignoring an answer for an already completed frontend tool call.");
1028
+ return;
1029
+ }
1030
+ const decision = interruptStateRef.current.resolve(payload, interruptId, agent.messages);
1022
1031
  if (decision.kind === "legacy-resume") try {
1023
1032
  return await copilotkit.runAgent({
1024
1033
  agent,
@@ -1034,7 +1043,7 @@ function useInterrupt(config) {
1034
1043
  }
1035
1044
  if (decision.kind === "expired") {
1036
1045
  console.error(`[CopilotKit] useInterrupt: interrupt ${decision.interrupt.id} expired at ${decision.interrupt.expiresAt}; not resuming.`);
1037
- interruptStateRef.current.clear();
1046
+ interruptStateRef.current = new ɵInterruptState();
1038
1047
  setPending(null);
1039
1048
  return;
1040
1049
  }
@@ -1052,25 +1061,30 @@ function useInterrupt(config) {
1052
1061
  });
1053
1062
  } catch (err) {
1054
1063
  console.error("[CopilotKit] useInterrupt resolve: runAgent rejected; clearing pending + rethrowing", err);
1055
- interruptStateRef.current.clear();
1064
+ interruptStateRef.current = new ɵInterruptState();
1056
1065
  setPending(null);
1057
1066
  throw err;
1058
1067
  }
1059
1068
  }, [agent, copilotkit]);
1069
+ /** Cancel a standard interrupt or dismiss a legacy interrupt locally. */
1060
1070
  const cancel = useCallback(async (interruptId) => {
1061
1071
  const current = pendingRef.current;
1062
1072
  if (!current) return;
1063
1073
  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.`);
1064
- const decision = interruptStateRef.current.cancel(interruptId);
1074
+ if (interruptStateRef.current.isCompletedToolCall(interruptId, agent.messages)) {
1075
+ console.warn("[CopilotKit] useInterrupt: ignoring an answer for an already completed frontend tool call.");
1076
+ return;
1077
+ }
1078
+ const decision = interruptStateRef.current.cancel(interruptId, agent.messages);
1065
1079
  if (decision.kind === "dismiss") {
1066
1080
  console.warn("[CopilotKit] useInterrupt: cancel() is not supported for legacy on_interrupt interrupts; dismissing.");
1067
- interruptStateRef.current.clear();
1081
+ interruptStateRef.current = new ɵInterruptState();
1068
1082
  setPending(null);
1069
1083
  return;
1070
1084
  }
1071
1085
  if (decision.kind === "expired") {
1072
1086
  console.error(`[CopilotKit] useInterrupt: interrupt ${decision.interrupt.id} expired at ${decision.interrupt.expiresAt}; not resuming.`);
1073
- interruptStateRef.current.clear();
1087
+ interruptStateRef.current = new ɵInterruptState();
1074
1088
  setPending(null);
1075
1089
  return;
1076
1090
  }
@@ -1088,7 +1102,7 @@ function useInterrupt(config) {
1088
1102
  });
1089
1103
  } catch (err) {
1090
1104
  console.error("[CopilotKit] useInterrupt resolve: runAgent rejected; clearing pending + rethrowing", err);
1091
- interruptStateRef.current.clear();
1105
+ interruptStateRef.current = new ɵInterruptState();
1092
1106
  setPending(null);
1093
1107
  throw err;
1094
1108
  }
@@ -1103,6 +1117,7 @@ function useInterrupt(config) {
1103
1117
  resolveRef.current = resolve;
1104
1118
  const cancelRef = useRef(cancel);
1105
1119
  cancelRef.current = cancel;
1120
+ /** Evaluate the consumer filter, logging failures and disabling their UI. */
1106
1121
  const isEnabled = (event) => {
1107
1122
  const predicate = enabledRef.current;
1108
1123
  if (!predicate) return true;
@@ -1192,7 +1207,7 @@ function useInterrupt(config) {
1192
1207
  }
1193
1208
 
1194
1209
  //#endregion
1195
- //#region src/v2/hooks/use-suggestions.tsx
1210
+ //#region src/hooks/use-suggestions.tsx
1196
1211
  function useSuggestions({ agentId } = {}) {
1197
1212
  const { copilotkit } = useCopilotKit$1();
1198
1213
  const config = useCopilotChatConfiguration();
@@ -1250,7 +1265,7 @@ function useSuggestions({ agentId } = {}) {
1250
1265
  }
1251
1266
 
1252
1267
  //#endregion
1253
- //#region src/v2/hooks/use-configure-suggestions.tsx
1268
+ //#region src/hooks/use-configure-suggestions.tsx
1254
1269
  function useConfigureSuggestions(config, deps) {
1255
1270
  const { copilotkit } = useCopilotKit$1();
1256
1271
  const chatConfig = useCopilotChatConfiguration();
@@ -1409,7 +1424,7 @@ function normalizeStaticSuggestions(suggestions) {
1409
1424
  }
1410
1425
 
1411
1426
  //#endregion
1412
- //#region src/v2/hooks/use-agent-context.tsx
1427
+ //#region src/hooks/use-agent-context.tsx
1413
1428
  function useAgentContext(context) {
1414
1429
  const { description, value } = context;
1415
1430
  const { copilotkit } = useCopilotKit$1();
@@ -1434,7 +1449,7 @@ function useAgentContext(context) {
1434
1449
  }
1435
1450
 
1436
1451
  //#endregion
1437
- //#region src/v2/hooks/use-threads.tsx
1452
+ //#region src/hooks/use-threads.tsx
1438
1453
  function useThreadStoreSelector(store, selector) {
1439
1454
  return useSyncExternalStore(useCallback((onStoreChange) => {
1440
1455
  const subscription = store.select(selector).subscribe(onStoreChange);
@@ -1618,7 +1633,7 @@ function useThreads({ agentId, includeArchived, limit, enabled = true }) {
1618
1633
  }
1619
1634
 
1620
1635
  //#endregion
1621
- //#region src/v2/types/defineToolCallRenderer.ts
1636
+ //#region src/types/defineToolCallRenderer.ts
1622
1637
  function defineToolCallRenderer(def) {
1623
1638
  const argsSchema = def.name === "*" && !def.args ? z.any() : def.args;
1624
1639
  return {
@@ -1631,7 +1646,7 @@ function defineToolCallRenderer(def) {
1631
1646
  }
1632
1647
 
1633
1648
  //#endregion
1634
- //#region src/v2/hooks/use-render-tool.tsx
1649
+ //#region src/hooks/use-render-tool.tsx
1635
1650
  /**
1636
1651
  * Registers a renderer entry in CopilotKit's `renderToolCalls` registry.
1637
1652
  *
@@ -1715,7 +1730,7 @@ function useRenderTool(config, deps) {
1715
1730
  }
1716
1731
 
1717
1732
  //#endregion
1718
- //#region src/v2/hooks/use-render-tool-call.tsx
1733
+ //#region src/hooks/use-render-tool-call.tsx
1719
1734
  /**
1720
1735
  * Memoized component that renders a single tool call.
1721
1736
  * This prevents unnecessary re-renders when parent components update
@@ -1830,7 +1845,7 @@ function useRenderToolCall() {
1830
1845
  }
1831
1846
 
1832
1847
  //#endregion
1833
- //#region src/v2/hooks/use-capabilities.tsx
1848
+ //#region src/hooks/use-capabilities.tsx
1834
1849
  /**
1835
1850
  * Returns the capabilities declared by the given agent (or the agent resolved
1836
1851
  * from the surrounding chat configuration, falling back to the default agent).