@copilotkit/react-core 1.52.0-next.5 → 1.52.0-next.7

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # ui
2
2
 
3
+ ## 1.52.0-next.7
4
+
5
+ ### Patch Changes
6
+
7
+ - d77f347: Added in the useInterrupt hook
8
+ - Updated dependencies [d77f347]
9
+ - @copilotkitnext/react@1.52.0-next.7
10
+ - @copilotkitnext/core@1.52.0-next.7
11
+ - @copilotkit/runtime-client-gql@1.52.0-next.7
12
+ - @copilotkit/shared@1.52.0-next.7
13
+
14
+ ## 1.52.0-next.6
15
+
16
+ ### Patch Changes
17
+
18
+ - 2007f8b: feat: useComponent improvements
19
+ - 5f941db: Prevent CPK styles from polluting user app
20
+ - Updated dependencies [2007f8b]
21
+ - @copilotkitnext/react@1.52.0-next.6
22
+ - @copilotkit/runtime-client-gql@1.52.0-next.6
23
+ - @copilotkit/shared@1.52.0-next.6
24
+ - @copilotkitnext/core@1.52.0-next.6
25
+
3
26
  ## 1.52.0-next.5
4
27
 
5
28
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -2021,118 +2021,6 @@ function validateProps(props) {
2021
2021
  if (cloudFeatures.length > 0 && !hasApiKey) throw new _copilotkit_shared.MissingPublicApiKeyError(`Missing required prop: 'publicApiKey' or 'publicLicenseKey' to use cloud features: ${cloudFeatures.map(formatFeatureName).join(", ")}`);
2022
2022
  }
2023
2023
 
2024
- //#endregion
2025
- //#region src/hooks/use-agent-nodename.ts
2026
- function useAgentNodeName(agentName) {
2027
- const { agent } = (0, _copilotkitnext_react.useAgent)({ agentId: agentName });
2028
- const nodeNameRef = (0, react.useRef)("start");
2029
- (0, react.useEffect)(() => {
2030
- if (!agent) return;
2031
- const subscription = agent.subscribe({
2032
- onStepStartedEvent: ({ event }) => {
2033
- nodeNameRef.current = event.stepName;
2034
- },
2035
- onRunStartedEvent: () => {
2036
- nodeNameRef.current = "start";
2037
- },
2038
- onRunFinishedEvent: () => {
2039
- nodeNameRef.current = "end";
2040
- }
2041
- });
2042
- return () => {
2043
- subscription.unsubscribe();
2044
- };
2045
- }, [agent]);
2046
- return nodeNameRef.current;
2047
- }
2048
-
2049
- //#endregion
2050
- //#region src/hooks/use-langgraph-interrupt-render.ts
2051
- const InterruptRenderer = ({ event, result, render, resolve }) => {
2052
- return render({
2053
- event,
2054
- result,
2055
- resolve
2056
- });
2057
- };
2058
- function useLangGraphInterruptRender(agent) {
2059
- const { interruptActions, agentSession, threadId, interruptEventQueue, addInterruptEvent, resolveInterruptEvent } = useCopilotContext();
2060
- const nodeName = useAgentNodeName((0, _copilotkitnext_react.useCopilotChatConfiguration)()?.agentId ?? "default");
2061
- (0, react.useEffect)(() => {
2062
- if (!agent) return;
2063
- let localInterrupt = null;
2064
- const { unsubscribe } = agent.subscribe({
2065
- onCustomEvent: ({ event }) => {
2066
- if (event.name === "on_interrupt") {
2067
- const eventData = {
2068
- name: _copilotkit_runtime_client_gql.MetaEventName.LangGraphInterruptEvent,
2069
- type: event.type,
2070
- value: (0, _copilotkit_shared.parseJson)(event.value, event.value)
2071
- };
2072
- localInterrupt = {
2073
- eventId: (0, _copilotkit_shared.dataToUUID)(eventData, "interruptEvents"),
2074
- threadId,
2075
- event: eventData
2076
- };
2077
- }
2078
- },
2079
- onRunStartedEvent: () => {
2080
- localInterrupt = null;
2081
- },
2082
- onRunFinalized: () => {
2083
- if (localInterrupt) {
2084
- addInterruptEvent(localInterrupt);
2085
- localInterrupt = null;
2086
- }
2087
- }
2088
- });
2089
- return () => {
2090
- unsubscribe();
2091
- };
2092
- }, [agent, threadId]);
2093
- const handleResolve = (0, react.useCallback)((eventId, response) => {
2094
- agent?.runAgent({ forwardedProps: { command: { resume: response } } });
2095
- resolveInterruptEvent(threadId, eventId, response ?? "");
2096
- }, [agent, threadId]);
2097
- return (0, react.useMemo)(() => {
2098
- const currentQueuedEvent = (interruptEventQueue[threadId] || []).find((qe) => !qe.event.response);
2099
- if (!currentQueuedEvent || !agentSession) return null;
2100
- const matchingAction = Object.values(interruptActions).find((action) => {
2101
- if (!action.enabled) return true;
2102
- return action.enabled({
2103
- eventValue: currentQueuedEvent.event.value,
2104
- agentMetadata: {
2105
- ...agentSession,
2106
- nodeName
2107
- }
2108
- });
2109
- });
2110
- if (!matchingAction) return null;
2111
- const { render, handler } = matchingAction;
2112
- const resolveInterrupt = (response) => {
2113
- handleResolve(currentQueuedEvent.eventId, response);
2114
- };
2115
- let result = null;
2116
- if (handler) result = handler({
2117
- event: currentQueuedEvent.event,
2118
- resolve: resolveInterrupt
2119
- });
2120
- if (!render) return null;
2121
- return react.default.createElement(InterruptRenderer, {
2122
- event: currentQueuedEvent.event,
2123
- result,
2124
- render,
2125
- resolve: resolveInterrupt
2126
- });
2127
- }, [
2128
- interruptActions,
2129
- interruptEventQueue,
2130
- threadId,
2131
- agentSession,
2132
- handleResolve
2133
- ]);
2134
- }
2135
-
2136
2024
  //#endregion
2137
2025
  //#region src/hooks/use-lazy-tool-renderer.tsx
2138
2026
  function useLazyToolRenderer() {
@@ -2182,7 +2070,14 @@ function useCopilotChatInternal({ suggestions, onInProgress, onSubmitMessage, on
2182
2070
  (0, react.useEffect)(() => {
2183
2071
  onInProgress?.(Boolean(agent?.isRunning));
2184
2072
  }, [agent?.isRunning, onInProgress]);
2185
- const interrupt = useLangGraphInterruptRender(agent);
2073
+ const [interrupt, setInterrupt] = (0, react.useState)(null);
2074
+ (0, react.useEffect)(() => {
2075
+ setInterrupt(copilotkit.interruptElement);
2076
+ const subscription = copilotkit.subscribe({ onInterruptElementChanged: ({ interruptElement }) => {
2077
+ setInterrupt(interruptElement);
2078
+ } });
2079
+ return () => subscription.unsubscribe();
2080
+ }, [copilotkit]);
2186
2081
  const reset = () => {
2187
2082
  agent?.setMessages([]);
2188
2083
  agent?.setState(null);
@@ -3311,6 +3206,31 @@ function useCopilotReadable({ description, value, convert, available }, dependen
3311
3206
  return ctxIdRef.current;
3312
3207
  }
3313
3208
 
3209
+ //#endregion
3210
+ //#region src/hooks/use-agent-nodename.ts
3211
+ function useAgentNodeName(agentName) {
3212
+ const { agent } = (0, _copilotkitnext_react.useAgent)({ agentId: agentName });
3213
+ const nodeNameRef = (0, react.useRef)("start");
3214
+ (0, react.useEffect)(() => {
3215
+ if (!agent) return;
3216
+ const subscription = agent.subscribe({
3217
+ onStepStartedEvent: ({ event }) => {
3218
+ nodeNameRef.current = event.stepName;
3219
+ },
3220
+ onRunStartedEvent: () => {
3221
+ nodeNameRef.current = "start";
3222
+ },
3223
+ onRunFinishedEvent: () => {
3224
+ nodeNameRef.current = "end";
3225
+ }
3226
+ });
3227
+ return () => {
3228
+ subscription.unsubscribe();
3229
+ };
3230
+ }, [agent]);
3231
+ return nodeNameRef.current;
3232
+ }
3233
+
3314
3234
  //#endregion
3315
3235
  //#region src/hooks/use-coagent.ts
3316
3236
  /**
@@ -3666,26 +3586,62 @@ function useCopilotAuthenticatedAction_c(action, dependencies) {
3666
3586
 
3667
3587
  //#endregion
3668
3588
  //#region src/hooks/use-langgraph-interrupt.ts
3669
- function useLangGraphInterrupt(action, dependencies) {
3670
- const { setInterruptAction, removeInterruptAction, interruptActions, threadId } = (0, react.useContext)(CopilotContext);
3671
- const { addToast } = useToast();
3672
- const actionId = (0, _copilotkit_shared.dataToUUID)(action, "lgAction");
3673
- (0, react.useEffect)(() => {
3674
- if (!action) return;
3675
- setInterruptAction({
3676
- ...action,
3677
- id: actionId
3678
- });
3679
- return () => {
3680
- removeInterruptAction(actionId);
3681
- };
3682
- }, [
3683
- setInterruptAction,
3684
- removeInterruptAction,
3589
+ /**
3590
+ * Transforms a v2 InterruptEvent into the v1 LangGraphInterruptEvent shape
3591
+ * expected by existing useLangGraphInterrupt callbacks.
3592
+ */
3593
+ function toV1Event(event) {
3594
+ const value = typeof event.value === "string" ? (0, _copilotkit_shared.parseJson)(event.value, event.value) : event.value;
3595
+ return {
3596
+ name: _copilotkit_runtime_client_gql.MetaEventName.LangGraphInterruptEvent,
3597
+ type: "MetaEvent",
3598
+ value
3599
+ };
3600
+ }
3601
+ function useLangGraphInterrupt(action, _dependencies) {
3602
+ const actionRef = (0, react.useRef)(action);
3603
+ actionRef.current = action;
3604
+ const existingConfig = (0, _copilotkitnext_react.useCopilotChatConfiguration)();
3605
+ const resolvedAgentId = existingConfig?.agentId ?? "default";
3606
+ const threadId = existingConfig?.threadId;
3607
+ const nodeName = useAgentNodeName(resolvedAgentId);
3608
+ const metadataRef = (0, react.useRef)({
3609
+ agentName: resolvedAgentId,
3685
3610
  threadId,
3686
- actionId,
3687
- ...dependencies || []
3688
- ]);
3611
+ nodeName
3612
+ });
3613
+ metadataRef.current = {
3614
+ agentName: resolvedAgentId,
3615
+ threadId,
3616
+ nodeName
3617
+ };
3618
+ (0, _copilotkitnext_react.useInterrupt)({
3619
+ render: (0, react.useCallback)(({ event, result, resolve }) => {
3620
+ const renderFn = actionRef.current.render;
3621
+ if (!renderFn) return react.default.createElement(react.default.Fragment);
3622
+ const rendered = renderFn({
3623
+ event: toV1Event(event),
3624
+ result,
3625
+ resolve: (r) => resolve(r)
3626
+ });
3627
+ if (typeof rendered === "string") return react.default.createElement(react.default.Fragment, null, rendered);
3628
+ return rendered;
3629
+ }, []),
3630
+ handler: (0, react.useCallback)(({ event, resolve }) => {
3631
+ return actionRef.current.handler?.({
3632
+ event: toV1Event(event),
3633
+ resolve: (r) => resolve(r)
3634
+ });
3635
+ }, []),
3636
+ enabled: (0, react.useCallback)((event) => {
3637
+ if (!actionRef.current.enabled) return true;
3638
+ return actionRef.current.enabled({
3639
+ eventValue: toV1Event(event).value,
3640
+ agentMetadata: metadataRef.current
3641
+ });
3642
+ }, []),
3643
+ agentId: resolvedAgentId
3644
+ });
3689
3645
  }
3690
3646
 
3691
3647
  //#endregion
@@ -4002,7 +3958,6 @@ exports.useDefaultTool = useDefaultTool;
4002
3958
  exports.useFrontendTool = useFrontendTool;
4003
3959
  exports.useHumanInTheLoop = useHumanInTheLoop;
4004
3960
  exports.useLangGraphInterrupt = useLangGraphInterrupt;
4005
- exports.useLangGraphInterruptRender = useLangGraphInterruptRender;
4006
3961
  exports.useLazyToolRenderer = useLazyToolRenderer;
4007
3962
  exports.useMakeCopilotDocumentReadable = useMakeCopilotDocumentReadable;
4008
3963
  exports.useRenderToolCall = useRenderToolCall;