@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.
@@ -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
- }