@copilotkit/react-core 1.52.0-next.6 → 1.52.0-next.8
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 +21 -0
- package/dist/index.cjs +88 -133
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +90 -134
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +90 -135
- package/dist/index.umd.js.map +1 -1
- package/package.json +5 -5
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -4
- package/src/hooks/index.ts +0 -1
- package/src/hooks/use-copilot-chat_internal.ts +14 -3
- package/src/hooks/use-langgraph-interrupt.ts +97 -38
- package/src/hooks/use-langgraph-interrupt-render.ts +0 -147
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"version": "1.52.0-next.
|
|
13
|
+
"version": "1.52.0-next.8",
|
|
14
14
|
"sideEffects": [
|
|
15
15
|
"**/*.css"
|
|
16
16
|
],
|
|
@@ -59,10 +59,10 @@
|
|
|
59
59
|
"@scarf/scarf": "^1.3.0",
|
|
60
60
|
"react-markdown": "^8.0.7",
|
|
61
61
|
"untruncate-json": "^0.0.1",
|
|
62
|
-
"@copilotkit/runtime-client-gql": "1.52.0-next.
|
|
63
|
-
"@copilotkit/shared": "1.52.0-next.
|
|
64
|
-
"@copilotkitnext/core": "1.52.0-next.
|
|
65
|
-
"@copilotkitnext/react": "1.52.0-next.
|
|
62
|
+
"@copilotkit/runtime-client-gql": "1.52.0-next.8",
|
|
63
|
+
"@copilotkit/shared": "1.52.0-next.8",
|
|
64
|
+
"@copilotkitnext/core": "1.52.0-next.8",
|
|
65
|
+
"@copilotkitnext/react": "1.52.0-next.8"
|
|
66
66
|
},
|
|
67
67
|
"keywords": [
|
|
68
68
|
"copilotkit",
|
|
@@ -49,6 +49,8 @@ vi.mock("@copilotkitnext/react", () => ({
|
|
|
49
49
|
clearSuggestions: vi.fn(),
|
|
50
50
|
addSuggestionsConfig: vi.fn(),
|
|
51
51
|
reloadSuggestions: vi.fn(),
|
|
52
|
+
interruptElement: null,
|
|
53
|
+
subscribe: vi.fn(() => ({ unsubscribe: vi.fn() })),
|
|
52
54
|
},
|
|
53
55
|
})),
|
|
54
56
|
useCopilotChatConfiguration: vi.fn(() => ({ agentId: "test-agent" })),
|
|
@@ -67,10 +69,6 @@ vi.mock("../../components/error-boundary/error-utils", () => ({
|
|
|
67
69
|
useAsyncCallback: <T extends (...args: unknown[]) => unknown>(fn: T) => fn,
|
|
68
70
|
}));
|
|
69
71
|
|
|
70
|
-
vi.mock("../use-langgraph-interrupt-render", () => ({
|
|
71
|
-
useLangGraphInterruptRender: vi.fn(() => null),
|
|
72
|
-
}));
|
|
73
|
-
|
|
74
72
|
vi.mock("../use-lazy-tool-renderer", () => ({
|
|
75
73
|
useLazyToolRenderer: vi.fn(() => () => null),
|
|
76
74
|
}));
|
package/src/hooks/index.ts
CHANGED
|
@@ -20,7 +20,6 @@ export { useCoAgent, type HintFunction } from "./use-coagent";
|
|
|
20
20
|
export { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
|
|
21
21
|
export { useCopilotAuthenticatedAction_c } from "./use-copilot-authenticated-action";
|
|
22
22
|
export { useLangGraphInterrupt } from "./use-langgraph-interrupt";
|
|
23
|
-
export { useLangGraphInterruptRender } from "./use-langgraph-interrupt-render";
|
|
24
23
|
export { useCopilotAdditionalInstructions } from "./use-copilot-additional-instructions";
|
|
25
24
|
export type { Tree, TreeNode } from "./use-tree";
|
|
26
25
|
export { useFrontendTool } from "./use-frontend-tool";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import React, {
|
|
2
2
|
useRef,
|
|
3
3
|
useEffect,
|
|
4
4
|
useCallback,
|
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
gqlToAGUI,
|
|
15
15
|
Message as DeprecatedGqlMessage,
|
|
16
16
|
} from "@copilotkit/runtime-client-gql";
|
|
17
|
-
import { useLangGraphInterruptRender } from "./use-langgraph-interrupt-render";
|
|
18
17
|
import {
|
|
19
18
|
useAgent,
|
|
20
19
|
useCopilotChatConfiguration,
|
|
@@ -363,7 +362,19 @@ export function useCopilotChatInternal({
|
|
|
363
362
|
onInProgress?.(Boolean(agent?.isRunning));
|
|
364
363
|
}, [agent?.isRunning, onInProgress]);
|
|
365
364
|
|
|
366
|
-
|
|
365
|
+
// Subscribe to copilotkit.interruptElement so the v1 return type stays
|
|
366
|
+
// reactive. The element is published by useInterrupt (v2) when user code
|
|
367
|
+
// calls useLangGraphInterrupt({ render, ... }).
|
|
368
|
+
const [interrupt, setInterrupt] = useState<React.ReactElement | null>(null);
|
|
369
|
+
useEffect(() => {
|
|
370
|
+
setInterrupt(copilotkit.interruptElement);
|
|
371
|
+
const subscription = copilotkit.subscribe({
|
|
372
|
+
onInterruptElementChanged: ({ interruptElement }) => {
|
|
373
|
+
setInterrupt(interruptElement);
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
return () => subscription.unsubscribe();
|
|
377
|
+
}, [copilotkit]);
|
|
367
378
|
|
|
368
379
|
const reset = () => {
|
|
369
380
|
agent?.setMessages([]);
|
|
@@ -1,46 +1,105 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CopilotContext } from "../context/copilot-context";
|
|
1
|
+
import React, { useCallback, useRef } from "react";
|
|
3
2
|
import { LangGraphInterruptRender } from "../types/interrupt-action";
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import {
|
|
4
|
+
useInterrupt,
|
|
5
|
+
useCopilotChatConfiguration,
|
|
6
|
+
} from "@copilotkitnext/react";
|
|
7
|
+
import type {
|
|
8
|
+
InterruptEvent,
|
|
9
|
+
InterruptRenderProps,
|
|
10
|
+
InterruptHandlerProps,
|
|
11
|
+
} from "@copilotkitnext/react";
|
|
12
|
+
import { MetaEventName } from "@copilotkit/runtime-client-gql";
|
|
13
|
+
import { parseJson } from "@copilotkit/shared";
|
|
14
|
+
import { useAgentNodeName } from "./use-agent-nodename";
|
|
15
|
+
import type { AgentSession } from "../context/copilot-context";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Transforms a v2 InterruptEvent into the v1 LangGraphInterruptEvent shape
|
|
19
|
+
* expected by existing useLangGraphInterrupt callbacks.
|
|
20
|
+
*/
|
|
21
|
+
function toV1Event<TEventValue>(event: InterruptEvent<TEventValue>) {
|
|
22
|
+
const value =
|
|
23
|
+
typeof event.value === "string"
|
|
24
|
+
? parseJson(event.value, event.value)
|
|
25
|
+
: event.value;
|
|
26
|
+
return {
|
|
27
|
+
name: MetaEventName.LangGraphInterruptEvent,
|
|
28
|
+
type: "MetaEvent" as const,
|
|
29
|
+
value,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
6
32
|
|
|
7
33
|
export function useLangGraphInterrupt<TEventValue = any>(
|
|
8
34
|
action: Omit<LangGraphInterruptRender<TEventValue>, "id">,
|
|
9
|
-
|
|
35
|
+
_dependencies?: any[],
|
|
10
36
|
) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
37
|
+
const actionRef = useRef(action);
|
|
38
|
+
// Update ref synchronously during render so it's always current
|
|
39
|
+
// when callbacks read from it (useEffect would be one tick late).
|
|
40
|
+
actionRef.current = action;
|
|
41
|
+
|
|
42
|
+
const existingConfig = useCopilotChatConfiguration();
|
|
43
|
+
const resolvedAgentId = existingConfig?.agentId ?? "default";
|
|
44
|
+
const threadId = existingConfig?.threadId;
|
|
45
|
+
const nodeName = useAgentNodeName(resolvedAgentId);
|
|
46
|
+
|
|
47
|
+
// Keep agentMetadata in a ref so stable callbacks always see current values.
|
|
48
|
+
const metadataRef = useRef<AgentSession>({
|
|
49
|
+
agentName: resolvedAgentId,
|
|
15
50
|
threadId,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
if (!action) return;
|
|
23
|
-
|
|
24
|
-
// if (!action.enabled) {
|
|
25
|
-
// TODO: if there are any other actions registered, we need to warn the user that a current action without "enabled" might render for everything
|
|
26
|
-
// addToast({
|
|
27
|
-
// type: "warning",
|
|
28
|
-
// message: "An action is already registered for the interrupt event",
|
|
29
|
-
// });
|
|
30
|
-
// return;
|
|
31
|
-
// }
|
|
32
|
-
|
|
33
|
-
setInterruptAction({ ...action, id: actionId });
|
|
34
|
-
|
|
35
|
-
// Cleanup: remove action on unmount
|
|
36
|
-
return () => {
|
|
37
|
-
removeInterruptAction(actionId);
|
|
38
|
-
};
|
|
39
|
-
}, [
|
|
40
|
-
setInterruptAction,
|
|
41
|
-
removeInterruptAction,
|
|
51
|
+
nodeName,
|
|
52
|
+
});
|
|
53
|
+
metadataRef.current = {
|
|
54
|
+
agentName: resolvedAgentId,
|
|
42
55
|
threadId,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
nodeName,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Stable callback references that always read the latest action from the ref.
|
|
60
|
+
// This prevents useInterrupt's internal useMemo/useEffect from seeing new
|
|
61
|
+
// function identities on every render, which would cause an infinite loop.
|
|
62
|
+
const render = useCallback(
|
|
63
|
+
({ event, result, resolve }: InterruptRenderProps<TEventValue>) => {
|
|
64
|
+
const renderFn = actionRef.current.render;
|
|
65
|
+
if (!renderFn) return React.createElement(React.Fragment);
|
|
66
|
+
const rendered = renderFn({
|
|
67
|
+
event: toV1Event(event) as any,
|
|
68
|
+
result,
|
|
69
|
+
resolve: (r) => resolve(r),
|
|
70
|
+
});
|
|
71
|
+
if (typeof rendered === "string") {
|
|
72
|
+
return React.createElement(React.Fragment, null, rendered);
|
|
73
|
+
}
|
|
74
|
+
return rendered;
|
|
75
|
+
},
|
|
76
|
+
[],
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// Handler always delegates to the ref — if no handler is set at call time,
|
|
80
|
+
// the optional chaining returns undefined which useInterrupt treats as null.
|
|
81
|
+
const handler = useCallback(
|
|
82
|
+
({ event, resolve }: InterruptHandlerProps<TEventValue>) => {
|
|
83
|
+
return actionRef.current.handler?.({
|
|
84
|
+
event: toV1Event(event) as any,
|
|
85
|
+
resolve: (r) => resolve(r),
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
[],
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const enabled = useCallback((event: InterruptEvent<TEventValue>) => {
|
|
92
|
+
if (!actionRef.current.enabled) return true;
|
|
93
|
+
return actionRef.current.enabled({
|
|
94
|
+
eventValue: toV1Event(event).value,
|
|
95
|
+
agentMetadata: metadataRef.current,
|
|
96
|
+
});
|
|
97
|
+
}, []);
|
|
98
|
+
|
|
99
|
+
useInterrupt({
|
|
100
|
+
render,
|
|
101
|
+
handler,
|
|
102
|
+
enabled,
|
|
103
|
+
agentId: resolvedAgentId,
|
|
104
|
+
});
|
|
46
105
|
}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { useCopilotContext } from "../context";
|
|
2
|
-
import React, { useCallback, useEffect, useMemo } from "react";
|
|
3
|
-
import type { AbstractAgent, AgentSubscriber } from "@ag-ui/client";
|
|
4
|
-
import { MetaEventName } from "@copilotkit/runtime-client-gql";
|
|
5
|
-
import { dataToUUID, parseJson } from "@copilotkit/shared";
|
|
6
|
-
import { useAgentNodeName } from "./use-agent-nodename";
|
|
7
|
-
import { useCopilotChatConfiguration } from "@copilotkitnext/react";
|
|
8
|
-
|
|
9
|
-
type InterruptProps = {
|
|
10
|
-
event: any;
|
|
11
|
-
result: any;
|
|
12
|
-
render: (props: {
|
|
13
|
-
event: any;
|
|
14
|
-
result: any;
|
|
15
|
-
resolve: (response: string) => void;
|
|
16
|
-
}) => string | React.ReactElement;
|
|
17
|
-
resolve: (response: string) => void;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const InterruptRenderer: React.FC<InterruptProps> = ({
|
|
21
|
-
event,
|
|
22
|
-
result,
|
|
23
|
-
render,
|
|
24
|
-
resolve,
|
|
25
|
-
}) => {
|
|
26
|
-
return render({ event, result, resolve });
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export function useLangGraphInterruptRender(
|
|
30
|
-
agent: AbstractAgent,
|
|
31
|
-
): string | React.ReactElement | null {
|
|
32
|
-
const {
|
|
33
|
-
interruptActions,
|
|
34
|
-
agentSession,
|
|
35
|
-
threadId,
|
|
36
|
-
interruptEventQueue,
|
|
37
|
-
addInterruptEvent,
|
|
38
|
-
resolveInterruptEvent,
|
|
39
|
-
} = useCopilotContext();
|
|
40
|
-
const existingConfig = useCopilotChatConfiguration();
|
|
41
|
-
const resolvedAgentId = existingConfig?.agentId ?? "default";
|
|
42
|
-
const nodeName = useAgentNodeName(resolvedAgentId);
|
|
43
|
-
|
|
44
|
-
useEffect(() => {
|
|
45
|
-
if (!agent) return;
|
|
46
|
-
let localInterrupt: any = null;
|
|
47
|
-
const subscriber: AgentSubscriber = {
|
|
48
|
-
onCustomEvent: ({ event }) => {
|
|
49
|
-
if (event.name === "on_interrupt") {
|
|
50
|
-
const eventData = {
|
|
51
|
-
name: MetaEventName.LangGraphInterruptEvent,
|
|
52
|
-
type: event.type,
|
|
53
|
-
value: parseJson(event.value, event.value),
|
|
54
|
-
};
|
|
55
|
-
const eventId = dataToUUID(eventData, "interruptEvents");
|
|
56
|
-
localInterrupt = {
|
|
57
|
-
eventId,
|
|
58
|
-
threadId,
|
|
59
|
-
event: eventData,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
onRunStartedEvent: () => {
|
|
64
|
-
localInterrupt = null;
|
|
65
|
-
},
|
|
66
|
-
onRunFinalized: () => {
|
|
67
|
-
if (localInterrupt) {
|
|
68
|
-
addInterruptEvent(localInterrupt);
|
|
69
|
-
localInterrupt = null;
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const { unsubscribe } = agent.subscribe(subscriber);
|
|
75
|
-
return () => {
|
|
76
|
-
unsubscribe();
|
|
77
|
-
};
|
|
78
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
79
|
-
}, [agent, threadId]);
|
|
80
|
-
|
|
81
|
-
const handleResolve = useCallback(
|
|
82
|
-
(eventId: string, response?: string) => {
|
|
83
|
-
agent?.runAgent({
|
|
84
|
-
forwardedProps: {
|
|
85
|
-
command: {
|
|
86
|
-
resume: response,
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
resolveInterruptEvent(threadId, eventId, response ?? "");
|
|
91
|
-
},
|
|
92
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
93
|
-
[agent, threadId],
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
return useMemo(() => {
|
|
97
|
-
// Get the queue for this thread and find the first unresponded event
|
|
98
|
-
const eventQueue = interruptEventQueue[threadId] || [];
|
|
99
|
-
const currentQueuedEvent = eventQueue.find((qe) => !qe.event.response);
|
|
100
|
-
|
|
101
|
-
if (!currentQueuedEvent || !agentSession) return null;
|
|
102
|
-
|
|
103
|
-
// Find the first matching action from all registered actions
|
|
104
|
-
const allActions = Object.values(interruptActions);
|
|
105
|
-
const matchingAction = allActions.find((action) => {
|
|
106
|
-
if (!action.enabled) return true; // No filter = match all
|
|
107
|
-
return action.enabled({
|
|
108
|
-
eventValue: currentQueuedEvent.event.value,
|
|
109
|
-
agentMetadata: {
|
|
110
|
-
...agentSession,
|
|
111
|
-
nodeName,
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
if (!matchingAction) return null;
|
|
117
|
-
|
|
118
|
-
const { render, handler } = matchingAction;
|
|
119
|
-
|
|
120
|
-
const resolveInterrupt = (response: string) => {
|
|
121
|
-
handleResolve(currentQueuedEvent.eventId, response);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
let result = null;
|
|
125
|
-
if (handler) {
|
|
126
|
-
result = handler({
|
|
127
|
-
event: currentQueuedEvent.event,
|
|
128
|
-
resolve: resolveInterrupt,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (!render) return null;
|
|
133
|
-
|
|
134
|
-
return React.createElement(InterruptRenderer, {
|
|
135
|
-
event: currentQueuedEvent.event,
|
|
136
|
-
result,
|
|
137
|
-
render,
|
|
138
|
-
resolve: resolveInterrupt,
|
|
139
|
-
});
|
|
140
|
-
}, [
|
|
141
|
-
interruptActions,
|
|
142
|
-
interruptEventQueue,
|
|
143
|
-
threadId,
|
|
144
|
-
agentSession,
|
|
145
|
-
handleResolve,
|
|
146
|
-
]);
|
|
147
|
-
}
|