@copilotkit/react-core 1.6.0-next.0 → 1.6.0-next.10
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 +78 -0
- package/dist/{chunk-VMYLY5KI.mjs → chunk-ADTDNMYG.mjs} +58 -48
- package/dist/chunk-ADTDNMYG.mjs.map +1 -0
- package/dist/{chunk-2DPTBHGN.mjs → chunk-ALW2T2LL.mjs} +3 -5
- package/dist/chunk-ALW2T2LL.mjs.map +1 -0
- package/dist/{chunk-ZAU4EMBM.mjs → chunk-F7O6L7C3.mjs} +4 -5
- package/dist/chunk-F7O6L7C3.mjs.map +1 -0
- package/dist/{chunk-7LZQFHNW.mjs → chunk-MPFP6ANM.mjs} +3 -4
- package/dist/chunk-MPFP6ANM.mjs.map +1 -0
- package/dist/{chunk-Z3U4UBG2.mjs → chunk-QJPWWVJQ.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +1 -2
- package/dist/components/copilot-provider/copilotkit.d.ts +1 -2
- package/dist/components/copilot-provider/index.d.ts +1 -2
- package/dist/components/index.d.ts +1 -2
- package/dist/context/copilot-context.d.ts +10 -148
- package/dist/context/index.d.ts +1 -2
- package/dist/copilot-context-9ae8990b.d.ts +183 -0
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.js +68 -62
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +5 -5
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +2 -4
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +1 -1
- package/dist/hooks/use-coagent.d.ts +1 -2
- package/dist/hooks/use-coagent.js +58 -50
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +3 -3
- package/dist/hooks/use-copilot-chat.d.ts +1 -2
- package/dist/hooks/use-copilot-chat.js +3 -5
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +2 -2
- package/dist/hooks/use-langgraph-interrupt-render.js +3 -4
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +1 -1
- package/dist/hooks/use-langgraph-interrupt.d.ts +10 -3
- package/dist/hooks/use-langgraph-interrupt.js +6 -9
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +3 -3
- package/dist/index.d.ts +1 -2
- package/dist/index.js +67 -61
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/lib/copilot-task.d.ts +1 -2
- package/dist/lib/index.d.ts +1 -2
- package/dist/types/interrupt-action.d.ts +10 -35
- package/dist/types/interrupt-action.js.map +1 -1
- package/dist/utils/extract.d.ts +1 -2
- package/dist/utils/index.d.ts +1 -2
- package/package.json +3 -3
- package/src/hooks/use-chat.ts +2 -4
- package/src/hooks/use-coagent.ts +70 -56
- package/src/hooks/use-langgraph-interrupt-render.ts +7 -3
- package/src/hooks/use-langgraph-interrupt.ts +3 -4
- package/src/types/interrupt-action.ts +17 -13
- package/dist/chunk-2DPTBHGN.mjs.map +0 -1
- package/dist/chunk-7LZQFHNW.mjs.map +0 -1
- package/dist/chunk-VMYLY5KI.mjs.map +0 -1
- package/dist/chunk-ZAU4EMBM.mjs.map +0 -1
- /package/dist/{chunk-Z3U4UBG2.mjs.map → chunk-QJPWWVJQ.mjs.map} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -7,10 +7,10 @@ import {
|
|
|
7
7
|
import "./chunk-IGLITGLC.mjs";
|
|
8
8
|
import {
|
|
9
9
|
useLangGraphInterruptRender
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-F7O6L7C3.mjs";
|
|
11
11
|
import {
|
|
12
12
|
useLangGraphInterrupt
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-MPFP6ANM.mjs";
|
|
14
14
|
import {
|
|
15
15
|
useMakeCopilotDocumentReadable
|
|
16
16
|
} from "./chunk-XOHLZIZK.mjs";
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
startAgent,
|
|
23
23
|
stopAgent,
|
|
24
24
|
useCoAgent
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-ADTDNMYG.mjs";
|
|
26
26
|
import {
|
|
27
27
|
useCopilotAuthenticatedAction_c
|
|
28
28
|
} from "./chunk-2F6RYBYS.mjs";
|
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
} from "./chunk-T2EFA3D5.mjs";
|
|
32
32
|
import {
|
|
33
33
|
useCopilotChat
|
|
34
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-QJPWWVJQ.mjs";
|
|
35
35
|
import {
|
|
36
36
|
useCopilotReadable
|
|
37
37
|
} from "./chunk-7HWSAEOY.mjs";
|
|
@@ -58,7 +58,7 @@ import {
|
|
|
58
58
|
CopilotMessagesContext,
|
|
59
59
|
useCopilotMessagesContext
|
|
60
60
|
} from "./chunk-DCTJZ742.mjs";
|
|
61
|
-
import "./chunk-
|
|
61
|
+
import "./chunk-ALW2T2LL.mjs";
|
|
62
62
|
import "./chunk-XERJQUHA.mjs";
|
|
63
63
|
import {
|
|
64
64
|
useCopilotRuntimeClient
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ForwardedParametersInput } from '@copilotkit/runtime-client-gql';
|
|
2
2
|
import { FrontendAction } from '../types/frontend-action.js';
|
|
3
|
-
import { CopilotContextParams } from '../
|
|
3
|
+
import { a as CopilotContextParams } from '../copilot-context-9ae8990b.js';
|
|
4
4
|
import '@copilotkit/shared';
|
|
5
5
|
import 'react';
|
|
6
6
|
import '../hooks/use-tree.js';
|
|
@@ -8,7 +8,6 @@ import '../types/document-pointer.js';
|
|
|
8
8
|
import '../types/chat-suggestion-configuration.js';
|
|
9
9
|
import '../types/coagent-action.js';
|
|
10
10
|
import '../types/coagent-state.js';
|
|
11
|
-
import '../types/interrupt-action.js';
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* This class is used to execute one-off tasks, for example on button press. It can use the context available via [useCopilotReadable](/reference/hooks/useCopilotReadable) and the actions provided by [useCopilotAction](/reference/hooks/useCopilotAction), or you can provide your own context and actions.
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -3,10 +3,9 @@ import '@copilotkit/runtime-client-gql';
|
|
|
3
3
|
import '../types/frontend-action.js';
|
|
4
4
|
import '@copilotkit/shared';
|
|
5
5
|
import 'react';
|
|
6
|
-
import '../
|
|
6
|
+
import '../copilot-context-9ae8990b.js';
|
|
7
7
|
import '../hooks/use-tree.js';
|
|
8
8
|
import '../types/document-pointer.js';
|
|
9
9
|
import '../types/chat-suggestion-configuration.js';
|
|
10
10
|
import '../types/coagent-action.js';
|
|
11
11
|
import '../types/coagent-state.js';
|
|
12
|
-
import '../types/interrupt-action.js';
|
|
@@ -1,35 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
resolve: (resolution: string) => void;
|
|
12
|
-
}) => unknown | Promise<unknown>;
|
|
13
|
-
/**
|
|
14
|
-
* The render function to handle the event.
|
|
15
|
-
*/
|
|
16
|
-
render?: (props: {
|
|
17
|
-
result: unknown;
|
|
18
|
-
event: LangGraphInterruptEvent;
|
|
19
|
-
resolve: (resolution: string) => void;
|
|
20
|
-
}) => string | React.ReactElement;
|
|
21
|
-
/**
|
|
22
|
-
* Conditions to render based on.
|
|
23
|
-
* Useful when using multiple interrupts
|
|
24
|
-
*/
|
|
25
|
-
conditions?: Condition[];
|
|
26
|
-
}
|
|
27
|
-
type LangGraphInterruptAction = LangGraphInterruptRender & {
|
|
28
|
-
event?: LangGraphInterruptEvent;
|
|
29
|
-
};
|
|
30
|
-
type LangGraphInterruptActionSetterArgs = (Partial<LangGraphInterruptRender> & {
|
|
31
|
-
event?: Partial<LangGraphInterruptEvent>;
|
|
32
|
-
}) | null;
|
|
33
|
-
type LangGraphInterruptActionSetter = (action: LangGraphInterruptActionSetterArgs) => void;
|
|
34
|
-
|
|
35
|
-
export { LangGraphInterruptAction, LangGraphInterruptActionSetter, LangGraphInterruptActionSetterArgs, LangGraphInterruptRender };
|
|
1
|
+
import '@copilotkit/runtime-client-gql';
|
|
2
|
+
export { L as LangGraphInterruptAction, d as LangGraphInterruptActionSetter, i as LangGraphInterruptActionSetterArgs, e as LangGraphInterruptRender, g as LangGraphInterruptRenderHandlerProps, h as LangGraphInterruptRenderProps } from '../copilot-context-9ae8990b.js';
|
|
3
|
+
import '@copilotkit/shared';
|
|
4
|
+
import './frontend-action.js';
|
|
5
|
+
import 'react';
|
|
6
|
+
import '../hooks/use-tree.js';
|
|
7
|
+
import './document-pointer.js';
|
|
8
|
+
import './chat-suggestion-configuration.js';
|
|
9
|
+
import './coagent-action.js';
|
|
10
|
+
import './coagent-state.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types/interrupt-action.ts"],"sourcesContent":["import { LangGraphInterruptEvent } from \"@copilotkit/runtime-client-gql\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/types/interrupt-action.ts"],"sourcesContent":["import { LangGraphInterruptEvent } from \"@copilotkit/runtime-client-gql\";\nimport { AgentSession } from \"../context/copilot-context\";\n\nexport interface LangGraphInterruptRenderHandlerProps<TEventValue = any> {\n event: LangGraphInterruptEvent<TEventValue>;\n resolve: (resolution: string) => void;\n}\n\nexport interface LangGraphInterruptRenderProps<TEventValue = any> {\n result: unknown;\n event: LangGraphInterruptEvent<TEventValue>;\n resolve: (resolution: string) => void;\n}\n\nexport interface LangGraphInterruptRender<TEventValue = any> {\n id: string;\n /**\n * The handler function to handle the event.\n */\n handler?: (props: LangGraphInterruptRenderHandlerProps<TEventValue>) => any | Promise<any>;\n /**\n * The render function to handle the event.\n */\n render?: (props: LangGraphInterruptRenderProps<TEventValue>) => string | React.ReactElement;\n /**\n * Method that returns a boolean, indicating if the interrupt action should run\n * Useful when using multiple interrupts\n */\n enabled?: (args: { eventValue: TEventValue; agentMetadata: AgentSession }) => boolean;\n}\n\nexport type LangGraphInterruptAction = LangGraphInterruptRender & {\n event?: LangGraphInterruptEvent;\n};\n\nexport type LangGraphInterruptActionSetterArgs =\n | (Partial<LangGraphInterruptRender> & { event?: Partial<LangGraphInterruptEvent> })\n | null;\nexport type LangGraphInterruptActionSetter = (action: LangGraphInterruptActionSetterArgs) => void;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/dist/utils/extract.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Parameter, MappedParameterTypes } from '@copilotkit/shared';
|
|
2
2
|
import { CopilotRequestType, ForwardedParametersInput } from '@copilotkit/runtime-client-gql';
|
|
3
|
-
import { CopilotContextParams } from '../
|
|
3
|
+
import { a as CopilotContextParams } from '../copilot-context-9ae8990b.js';
|
|
4
4
|
import { CopilotMessagesContextParams } from '../context/copilot-messages-context.js';
|
|
5
5
|
import '../types/frontend-action.js';
|
|
6
6
|
import 'react';
|
|
@@ -9,7 +9,6 @@ import '../types/document-pointer.js';
|
|
|
9
9
|
import '../types/chat-suggestion-configuration.js';
|
|
10
10
|
import '../types/coagent-action.js';
|
|
11
11
|
import '../types/coagent-state.js';
|
|
12
|
-
import '../types/interrupt-action.js';
|
|
13
12
|
|
|
14
13
|
interface InitialState<T extends Parameter[] | [] = []> {
|
|
15
14
|
status: "initial";
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { extract } from './extract.js';
|
|
|
2
2
|
export { shouldShowDevConsole } from './dev-console.js';
|
|
3
3
|
import '@copilotkit/shared';
|
|
4
4
|
import '@copilotkit/runtime-client-gql';
|
|
5
|
-
import '../
|
|
5
|
+
import '../copilot-context-9ae8990b.js';
|
|
6
6
|
import '../types/frontend-action.js';
|
|
7
7
|
import 'react';
|
|
8
8
|
import '../hooks/use-tree.js';
|
|
@@ -10,5 +10,4 @@ import '../types/document-pointer.js';
|
|
|
10
10
|
import '../types/chat-suggestion-configuration.js';
|
|
11
11
|
import '../types/coagent-action.js';
|
|
12
12
|
import '../types/coagent-state.js';
|
|
13
|
-
import '../types/interrupt-action.js';
|
|
14
13
|
import '../context/copilot-messages-context.js';
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.6.0-next.
|
|
12
|
+
"version": "1.6.0-next.10",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"module": "./dist/index.mjs",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"@scarf/scarf": "^1.3.0",
|
|
44
44
|
"react-markdown": "^8.0.7",
|
|
45
45
|
"untruncate-json": "^0.0.1",
|
|
46
|
-
"@copilotkit/runtime-client-gql": "1.6.0-next.
|
|
47
|
-
"@copilotkit/shared": "1.6.0-next.
|
|
46
|
+
"@copilotkit/runtime-client-gql": "1.6.0-next.10",
|
|
47
|
+
"@copilotkit/shared": "1.6.0-next.10"
|
|
48
48
|
},
|
|
49
49
|
"keywords": [
|
|
50
50
|
"copilotkit",
|
package/src/hooks/use-chat.ts
CHANGED
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
LangGraphInterruptEvent,
|
|
41
41
|
MetaEventInput,
|
|
42
42
|
} from "@copilotkit/runtime-client-gql";
|
|
43
|
+
import { parseJson } from "@copilotkit/shared";
|
|
43
44
|
|
|
44
45
|
export type UseChatOptions = {
|
|
45
46
|
/**
|
|
@@ -377,10 +378,7 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
|
|
|
377
378
|
(value.generateCopilotResponse?.metaEvents ?? []).forEach((ev) => {
|
|
378
379
|
if (ev.name === MetaEventName.LangGraphInterruptEvent) {
|
|
379
380
|
let eventValue = langGraphInterruptEvent(ev as LangGraphInterruptEvent).value;
|
|
380
|
-
|
|
381
|
-
eventValue = JSON.parse(eventValue);
|
|
382
|
-
// In case of unparsable string, we keep the event as is
|
|
383
|
-
} catch (e) {}
|
|
381
|
+
eventValue = parseJson(eventValue, eventValue);
|
|
384
382
|
setLangGraphInterruptAction({
|
|
385
383
|
event: {
|
|
386
384
|
...langGraphInterruptEvent(ev as LangGraphInterruptEvent),
|
package/src/hooks/use-coagent.ts
CHANGED
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
* </PropertyReference>
|
|
89
89
|
*/
|
|
90
90
|
|
|
91
|
-
import { useEffect, useRef } from "react";
|
|
91
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
92
92
|
import {
|
|
93
93
|
CopilotContextParams,
|
|
94
94
|
CopilotMessagesContextParams,
|
|
@@ -101,6 +101,7 @@ import { Message } from "@copilotkit/runtime-client-gql";
|
|
|
101
101
|
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
102
102
|
import { useToast } from "../components/toast/toast-provider";
|
|
103
103
|
import { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
|
|
104
|
+
import { parseJson } from "@copilotkit/shared";
|
|
104
105
|
|
|
105
106
|
interface WithInternalStateManagementAndInitial<T> {
|
|
106
107
|
/**
|
|
@@ -222,12 +223,6 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
222
223
|
const lastLoadedThreadId = useRef<string>();
|
|
223
224
|
const lastLoadedState = useRef<any>();
|
|
224
225
|
|
|
225
|
-
const isExternalStateManagement = (
|
|
226
|
-
options: UseCoagentOptions<T>,
|
|
227
|
-
): options is WithExternalStateManagement<T> => {
|
|
228
|
-
return "state" in options && "setState" in options;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
226
|
const { name } = options;
|
|
232
227
|
useEffect(() => {
|
|
233
228
|
if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {
|
|
@@ -237,35 +232,12 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
237
232
|
}
|
|
238
233
|
}, [availableAgents]);
|
|
239
234
|
|
|
240
|
-
const isInternalStateManagementWithInitial = (
|
|
241
|
-
options: UseCoagentOptions<T>,
|
|
242
|
-
): options is WithInternalStateManagementAndInitial<T> => {
|
|
243
|
-
return "initialState" in options;
|
|
244
|
-
};
|
|
245
|
-
|
|
246
235
|
const messagesContext = useCopilotMessagesContext();
|
|
247
236
|
const context = { ...generalContext, ...messagesContext };
|
|
248
237
|
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =
|
|
249
238
|
context;
|
|
250
239
|
const { appendMessage, runChatCompletion } = useCopilotChat();
|
|
251
240
|
|
|
252
|
-
const getCoagentState = (coagentStates: Record<string, CoagentState>, name: string) => {
|
|
253
|
-
if (coagentStates[name]) {
|
|
254
|
-
return coagentStates[name];
|
|
255
|
-
} else {
|
|
256
|
-
return {
|
|
257
|
-
name,
|
|
258
|
-
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
259
|
-
configurable: options.configurable ?? {},
|
|
260
|
-
running: false,
|
|
261
|
-
active: false,
|
|
262
|
-
threadId: undefined,
|
|
263
|
-
nodeName: undefined,
|
|
264
|
-
runId: undefined,
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
|
|
269
241
|
const runtimeClient = useCopilotRuntimeClient({
|
|
270
242
|
url: copilotApiConfig.chatApiEndpoint,
|
|
271
243
|
publicApiKey: copilotApiConfig.publicApiKey,
|
|
@@ -273,19 +245,23 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
273
245
|
});
|
|
274
246
|
|
|
275
247
|
// if we manage state internally, we need to provide a function to set the state
|
|
276
|
-
const setState = (
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
248
|
+
const setState = useCallback(
|
|
249
|
+
(newState: T | ((prevState: T | undefined) => T)) => {
|
|
250
|
+
// coagentStatesRef.current || {}
|
|
251
|
+
let coagentState: CoagentState = getCoagentState({ coagentStates, name, options });
|
|
252
|
+
const updatedState =
|
|
253
|
+
typeof newState === "function" ? (newState as Function)(coagentState.state) : newState;
|
|
280
254
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
255
|
+
setCoagentStatesWithRef({
|
|
256
|
+
...coagentStatesRef.current,
|
|
257
|
+
[name]: {
|
|
258
|
+
...coagentState,
|
|
259
|
+
state: updatedState,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
[coagentStates, name],
|
|
264
|
+
);
|
|
289
265
|
|
|
290
266
|
useEffect(() => {
|
|
291
267
|
const fetchAgentState = async () => {
|
|
@@ -302,7 +278,7 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
302
278
|
if (result.data?.loadAgentState?.threadExists && newState && newState != "{}") {
|
|
303
279
|
lastLoadedState.current = newState;
|
|
304
280
|
lastLoadedThreadId.current = threadId;
|
|
305
|
-
const fetchedState =
|
|
281
|
+
const fetchedState = parseJson(newState, {});
|
|
306
282
|
isExternalStateManagement(options)
|
|
307
283
|
? options.setState(fetchedState)
|
|
308
284
|
: setState(fetchedState);
|
|
@@ -311,8 +287,6 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
311
287
|
void fetchAgentState();
|
|
312
288
|
}, [threadId]);
|
|
313
289
|
|
|
314
|
-
const coagentState = getCoagentState(coagentStates, name);
|
|
315
|
-
|
|
316
290
|
// Sync internal state with external state if state management is external
|
|
317
291
|
useEffect(() => {
|
|
318
292
|
if (isExternalStateManagement(options)) {
|
|
@@ -334,17 +308,20 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
334
308
|
);
|
|
335
309
|
|
|
336
310
|
// Return the state and setState function
|
|
337
|
-
return {
|
|
338
|
-
name,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
311
|
+
return useMemo(() => {
|
|
312
|
+
const coagentState = getCoagentState({ coagentStates, name, options });
|
|
313
|
+
return {
|
|
314
|
+
name,
|
|
315
|
+
nodeName: coagentState.nodeName,
|
|
316
|
+
threadId: coagentState.threadId,
|
|
317
|
+
running: coagentState.running,
|
|
318
|
+
state: coagentState.state,
|
|
319
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
320
|
+
start: () => startAgent(name, context),
|
|
321
|
+
stop: () => stopAgent(name, context),
|
|
322
|
+
run: runAgentCallback,
|
|
323
|
+
};
|
|
324
|
+
}, [name, coagentStates, options, setState, runAgentCallback]);
|
|
348
325
|
}
|
|
349
326
|
|
|
350
327
|
export function startAgent(name: string, context: CopilotContextParams) {
|
|
@@ -411,3 +388,40 @@ export async function runAgent(
|
|
|
411
388
|
await runChatCompletion();
|
|
412
389
|
}
|
|
413
390
|
}
|
|
391
|
+
|
|
392
|
+
const isExternalStateManagement = <T>(
|
|
393
|
+
options: UseCoagentOptions<T>,
|
|
394
|
+
): options is WithExternalStateManagement<T> => {
|
|
395
|
+
return "state" in options && "setState" in options;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const isInternalStateManagementWithInitial = <T>(
|
|
399
|
+
options: UseCoagentOptions<T>,
|
|
400
|
+
): options is WithInternalStateManagementAndInitial<T> => {
|
|
401
|
+
return "initialState" in options;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const getCoagentState = <T>({
|
|
405
|
+
coagentStates,
|
|
406
|
+
name,
|
|
407
|
+
options,
|
|
408
|
+
}: {
|
|
409
|
+
coagentStates: Record<string, CoagentState>;
|
|
410
|
+
name: string;
|
|
411
|
+
options: UseCoagentOptions<T>;
|
|
412
|
+
}) => {
|
|
413
|
+
if (coagentStates[name]) {
|
|
414
|
+
return coagentStates[name];
|
|
415
|
+
} else {
|
|
416
|
+
return {
|
|
417
|
+
name,
|
|
418
|
+
state: isInternalStateManagementWithInitial<T>(options) ? options.initialState : {},
|
|
419
|
+
configurable: options.configurable ?? {},
|
|
420
|
+
running: false,
|
|
421
|
+
active: false,
|
|
422
|
+
threadId: undefined,
|
|
423
|
+
nodeName: undefined,
|
|
424
|
+
runId: undefined,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
};
|
|
@@ -18,7 +18,8 @@ const InterruptRenderer: React.FC<InterruptProps> = ({ event, result, render, re
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
export function useLangGraphInterruptRender(): string | React.ReactElement | null {
|
|
21
|
-
const { langGraphInterruptAction, setLangGraphInterruptAction } =
|
|
21
|
+
const { langGraphInterruptAction, setLangGraphInterruptAction, agentSession } =
|
|
22
|
+
useCopilotContext();
|
|
22
23
|
|
|
23
24
|
const responseRef = React.useRef<string>();
|
|
24
25
|
const resolveInterrupt = useCallback(
|
|
@@ -39,9 +40,12 @@ export function useLangGraphInterruptRender(): string | React.ReactElement | nul
|
|
|
39
40
|
)
|
|
40
41
|
return null;
|
|
41
42
|
|
|
42
|
-
const { render, handler, event,
|
|
43
|
+
const { render, handler, event, enabled } = langGraphInterruptAction;
|
|
43
44
|
|
|
44
|
-
const conditionsMet =
|
|
45
|
+
const conditionsMet =
|
|
46
|
+
!agentSession || !enabled
|
|
47
|
+
? true
|
|
48
|
+
: enabled({ eventValue: event.value, agentMetadata: agentSession });
|
|
45
49
|
if (!conditionsMet) {
|
|
46
50
|
return null;
|
|
47
51
|
}
|
|
@@ -5,8 +5,8 @@ import { useCopilotChat } from "./use-copilot-chat";
|
|
|
5
5
|
import { useToast } from "../components/toast/toast-provider";
|
|
6
6
|
import { dataToUUID } from "@copilotkit/shared";
|
|
7
7
|
|
|
8
|
-
export function useLangGraphInterrupt(
|
|
9
|
-
action: Omit<LangGraphInterruptRender
|
|
8
|
+
export function useLangGraphInterrupt<TEventValue = any>(
|
|
9
|
+
action: Omit<LangGraphInterruptRender<TEventValue>, "id">,
|
|
10
10
|
dependencies?: any[],
|
|
11
11
|
) {
|
|
12
12
|
const { setLangGraphInterruptAction, removeLangGraphInterruptAction, langGraphInterruptAction } =
|
|
@@ -35,10 +35,9 @@ export function useLangGraphInterrupt(
|
|
|
35
35
|
|
|
36
36
|
useEffect(() => {
|
|
37
37
|
if (!action) return;
|
|
38
|
-
// console.log(2, { hasAction, isCurrentAction, conditions: action.conditions })
|
|
39
38
|
// An action was already set, with no conditions and it's not the action we're using right now.
|
|
40
39
|
// Show a warning, as this action will not be executed
|
|
41
|
-
if (hasAction && !isCurrentAction && !action.
|
|
40
|
+
if (hasAction && !isCurrentAction && !action.enabled) {
|
|
42
41
|
addToast({
|
|
43
42
|
type: "warning",
|
|
44
43
|
message: "An action is already registered for the interrupt event",
|
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
import { LangGraphInterruptEvent } from "@copilotkit/runtime-client-gql";
|
|
2
|
-
import {
|
|
2
|
+
import { AgentSession } from "../context/copilot-context";
|
|
3
3
|
|
|
4
|
-
export interface
|
|
4
|
+
export interface LangGraphInterruptRenderHandlerProps<TEventValue = any> {
|
|
5
|
+
event: LangGraphInterruptEvent<TEventValue>;
|
|
6
|
+
resolve: (resolution: string) => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface LangGraphInterruptRenderProps<TEventValue = any> {
|
|
10
|
+
result: unknown;
|
|
11
|
+
event: LangGraphInterruptEvent<TEventValue>;
|
|
12
|
+
resolve: (resolution: string) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface LangGraphInterruptRender<TEventValue = any> {
|
|
5
16
|
id: string;
|
|
6
17
|
/**
|
|
7
18
|
* The handler function to handle the event.
|
|
8
19
|
*/
|
|
9
|
-
handler?: (props:
|
|
10
|
-
event: LangGraphInterruptEvent;
|
|
11
|
-
resolve: (resolution: string) => void;
|
|
12
|
-
}) => unknown | Promise<unknown>;
|
|
20
|
+
handler?: (props: LangGraphInterruptRenderHandlerProps<TEventValue>) => any | Promise<any>;
|
|
13
21
|
/**
|
|
14
22
|
* The render function to handle the event.
|
|
15
23
|
*/
|
|
16
|
-
render?: (props:
|
|
17
|
-
result: unknown;
|
|
18
|
-
event: LangGraphInterruptEvent;
|
|
19
|
-
resolve: (resolution: string) => void;
|
|
20
|
-
}) => string | React.ReactElement;
|
|
24
|
+
render?: (props: LangGraphInterruptRenderProps<TEventValue>) => string | React.ReactElement;
|
|
21
25
|
/**
|
|
22
|
-
*
|
|
26
|
+
* Method that returns a boolean, indicating if the interrupt action should run
|
|
23
27
|
* Useful when using multiple interrupts
|
|
24
28
|
*/
|
|
25
|
-
|
|
29
|
+
enabled?: (args: { eventValue: TEventValue; agentMetadata: AgentSession }) => boolean;
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
export type LangGraphInterruptAction = LangGraphInterruptRender & {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-chat.ts"],"sourcesContent":["import React, { useCallback, useRef } from \"react\";\nimport {\n FunctionCallHandler,\n COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,\n CoAgentStateRenderHandler,\n randomId,\n} from \"@copilotkit/shared\";\nimport {\n Message,\n TextMessage,\n ResultMessage,\n convertMessagesToGqlInput,\n filterAdjacentAgentStateMessages,\n filterAgentStateMessages,\n convertGqlOutputToMessages,\n MessageStatusCode,\n MessageRole,\n Role,\n CopilotRequestType,\n ForwardedParametersInput,\n loadMessagesFromJsonRepresentation,\n ExtensionsInput,\n CopilotRuntimeClient,\n langGraphInterruptEvent,\n} from \"@copilotkit/runtime-client-gql\";\n\nimport { CopilotApiConfig } from \"../context\";\nimport { FrontendAction, processActionsForRuntimeRequest } from \"../types/frontend-action\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { AgentSession } from \"../context/copilot-context\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\nimport { useAsyncCallback, useErrorToast } from \"../components/error-boundary/error-utils\";\nimport {\n LangGraphInterruptAction,\n LangGraphInterruptActionSetter,\n} from \"../types/interrupt-action\";\nimport { MetaEvent, MetaEventName } from \"@copilotkit/runtime-client-gql\";\nimport {\n CopilotKitLangGraphInterruptEvent,\n LangGraphInterruptEvent,\n MetaEventInput,\n} from \"@copilotkit/runtime-client-gql\";\n\nexport type UseChatOptions = {\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n /**\n * Callback function to be called when a function call is received.\n * If the function returns a `ChatRequest` object, the request will be sent\n * automatically to the API and will be used to update the chat.\n */\n onFunctionCall?: FunctionCallHandler;\n\n /**\n * Callback function to be called when a coagent action is received.\n */\n onCoAgentStateRender?: CoAgentStateRenderHandler;\n\n /**\n * Function definitions to be sent to the API.\n */\n actions: FrontendAction<any>[];\n\n /**\n * The CopilotKit API configuration.\n */\n copilotConfig: CopilotApiConfig;\n\n /**\n * The current list of messages in the chat.\n */\n messages: Message[];\n /**\n * The setState-powered method to update the chat messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * A callback to get the latest system message.\n */\n makeSystemMessageCallback: () => TextMessage;\n\n /**\n * Whether the API request is in progress\n */\n isLoading: boolean;\n\n /**\n * setState-powered method to update the isChatLoading value\n */\n setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n /**\n * The current list of coagent states.\n */\n coagentStatesRef: React.RefObject<Record<string, CoagentState>>;\n\n /**\n * setState-powered method to update the agent states\n */\n setCoagentStatesWithRef: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;\n\n /**\n * The current agent session.\n */\n agentSession: AgentSession | null;\n\n /**\n * setState-powered method to update the agent session\n */\n setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;\n\n /**\n * The forwarded parameters.\n */\n forwardedParameters?: Pick<ForwardedParametersInput, \"temperature\">;\n\n /**\n * The current thread ID.\n */\n threadId: string;\n /**\n * set the current thread ID\n */\n setThreadId: (threadId: string) => void;\n /**\n * The current run ID.\n */\n runId: string | null;\n /**\n * set the current run ID\n */\n setRunId: (runId: string | null) => void;\n /**\n * The global chat abort controller.\n */\n chatAbortControllerRef: React.MutableRefObject<AbortController | null>;\n /**\n * The agent lock.\n */\n agentLock: string | null;\n /**\n * The extensions.\n */\n extensions: ExtensionsInput;\n /**\n * The setState-powered method to update the extensions.\n */\n setExtensions: React.Dispatch<React.SetStateAction<ExtensionsInput>>;\n\n langGraphInterruptAction: LangGraphInterruptAction | null;\n\n setLangGraphInterruptAction: LangGraphInterruptActionSetter;\n};\n\nexport type UseChatHelpers = {\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n */\n append: (message: Message, options?: AppendMessageOptions) => Promise<void>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: () => Promise<void>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n\n /**\n * Run the chat completion.\n */\n runChatCompletion: () => Promise<Message[]>;\n};\n\nexport interface AppendMessageOptions {\n /**\n * Whether to run the chat completion after appending the message. Defaults to `true`.\n */\n followUp?: boolean;\n}\n\nexport function useChat(options: UseChatOptions): UseChatHelpers {\n const {\n messages,\n setMessages,\n makeSystemMessageCallback,\n copilotConfig,\n setIsLoading,\n initialMessages,\n isLoading,\n actions,\n onFunctionCall,\n onCoAgentStateRender,\n setCoagentStatesWithRef,\n coagentStatesRef,\n agentSession,\n setAgentSession,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n agentLock,\n extensions,\n setExtensions,\n langGraphInterruptAction,\n setLangGraphInterruptAction,\n } = options;\n const runChatCompletionRef = useRef<(previousMessages: Message[]) => Promise<Message[]>>();\n const addErrorToast = useErrorToast();\n // We need to keep a ref of coagent states and session because of renderAndWait - making sure\n // the latest state is sent to the API\n // This is a workaround and needs to be addressed in the future\n const agentSessionRef = useRef<AgentSession | null>(agentSession);\n agentSessionRef.current = agentSession;\n\n const runIdRef = useRef<string | null>(runId);\n runIdRef.current = runId;\n const extensionsRef = useRef<ExtensionsInput>(extensions);\n extensionsRef.current = extensions;\n\n const publicApiKey = copilotConfig.publicApiKey;\n\n const headers = {\n ...(copilotConfig.headers || {}),\n ...(publicApiKey ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {}),\n };\n\n const runtimeClient = useCopilotRuntimeClient({\n url: copilotConfig.chatApiEndpoint,\n publicApiKey: copilotConfig.publicApiKey,\n headers,\n credentials: copilotConfig.credentials,\n });\n\n const runChatCompletion = useAsyncCallback(\n async (previousMessages: Message[]): Promise<Message[]> => {\n setIsLoading(true);\n const interruptEvent = langGraphInterruptAction?.event;\n // In case an interrupt event exist and valid but has no response yet, we cannot process further messages to an agent\n if (\n interruptEvent?.name === MetaEventName.LangGraphInterruptEvent &&\n interruptEvent?.value &&\n !interruptEvent?.response &&\n agentSessionRef.current\n ) {\n addErrorToast([\n new Error(\n \"A message was sent while interrupt is active. This will cause failure on the agent side\",\n ),\n ]);\n }\n\n // this message is just a placeholder. It will disappear once the first real message\n // is received\n let newMessages: Message[] = [\n new TextMessage({\n content: \"\",\n role: Role.Assistant,\n }),\n ];\n\n chatAbortControllerRef.current = new AbortController();\n\n setMessages([...previousMessages, ...newMessages]);\n\n const systemMessage = makeSystemMessageCallback();\n\n const messagesWithContext = [systemMessage, ...(initialMessages || []), ...previousMessages];\n\n const isAgentRun = agentSessionRef.current !== null;\n\n const stream = runtimeClient.asStream(\n runtimeClient.generateCopilotResponse({\n data: {\n frontend: {\n actions: processActionsForRuntimeRequest(actions),\n url: window.location.href,\n },\n threadId: threadId,\n runId: runIdRef.current,\n extensions: extensionsRef.current,\n metaEvents: composeAndFlushMetaEventsInput([langGraphInterruptAction?.event]),\n messages: convertMessagesToGqlInput(filterAgentStateMessages(messagesWithContext)),\n ...(copilotConfig.cloud\n ? {\n cloud: {\n ...(copilotConfig.cloud.guardrails?.input?.restrictToTopic?.enabled\n ? {\n guardrails: {\n inputValidationRules: {\n allowList:\n copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,\n denyList:\n copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics,\n },\n },\n }\n : {}),\n },\n }\n : {}),\n metadata: {\n requestType: CopilotRequestType.Chat,\n },\n ...(agentSessionRef.current\n ? {\n agentSession: agentSessionRef.current,\n }\n : {}),\n agentStates: Object.values(coagentStatesRef.current!).map((state) => ({\n agentName: state.name,\n state: JSON.stringify(state.state),\n configurable: JSON.stringify(state.configurable ?? {}),\n })),\n forwardedParameters: options.forwardedParameters || {},\n },\n properties: copilotConfig.properties,\n signal: chatAbortControllerRef.current?.signal,\n }),\n );\n\n const guardrailsEnabled =\n copilotConfig.cloud?.guardrails?.input?.restrictToTopic.enabled || false;\n\n const reader = stream.getReader();\n\n let executedCoAgentStateRenders: string[] = [];\n let followUp: FrontendAction[\"followUp\"] = undefined;\n\n let messages: Message[] = [];\n let syncedMessages: Message[] = [];\n let interruptMessages: Message[] = [];\n\n try {\n while (true) {\n let done, value;\n\n try {\n const readResult = await reader.read();\n done = readResult.done;\n value = readResult.value;\n } catch (readError) {\n break;\n }\n\n if (done) {\n if (chatAbortControllerRef.current.signal.aborted) {\n return [];\n }\n break;\n }\n\n if (!value?.generateCopilotResponse) {\n continue;\n }\n\n runIdRef.current = value.generateCopilotResponse.runId || null;\n\n // in the output, graphql inserts __typename, which leads to an error when sending it along\n // as input to the next request.\n extensionsRef.current = CopilotRuntimeClient.removeGraphQLTypename(\n value.generateCopilotResponse.extensions || {},\n );\n\n // setThreadId(threadIdRef.current);\n setRunId(runIdRef.current);\n setExtensions(extensionsRef.current);\n let rawMessagesResponse = value.generateCopilotResponse.messages;\n (value.generateCopilotResponse?.metaEvents ?? []).forEach((ev) => {\n if (ev.name === MetaEventName.LangGraphInterruptEvent) {\n let eventValue = langGraphInterruptEvent(ev as LangGraphInterruptEvent).value;\n try {\n eventValue = JSON.parse(eventValue);\n // In case of unparsable string, we keep the event as is\n } catch (e) {}\n setLangGraphInterruptAction({\n event: {\n ...langGraphInterruptEvent(ev as LangGraphInterruptEvent),\n value: eventValue,\n },\n });\n }\n if (ev.name === MetaEventName.CopilotKitLangGraphInterruptEvent) {\n const data = (ev as CopilotKitLangGraphInterruptEvent).data;\n\n // @ts-expect-error -- same type of messages\n rawMessagesResponse = [...rawMessagesResponse, ...data.messages];\n interruptMessages = convertGqlOutputToMessages(\n // @ts-ignore\n filterAdjacentAgentStateMessages(data.messages),\n );\n }\n });\n\n messages = convertGqlOutputToMessages(\n filterAdjacentAgentStateMessages(rawMessagesResponse),\n );\n\n if (messages.length === 0) {\n continue;\n }\n\n newMessages = [];\n\n // request failed, display error message and quit\n if (\n value.generateCopilotResponse.status?.__typename === \"FailedResponseStatus\" &&\n value.generateCopilotResponse.status.reason === \"GUARDRAILS_VALIDATION_FAILED\"\n ) {\n newMessages = [\n new TextMessage({\n role: MessageRole.Assistant,\n content: value.generateCopilotResponse.status.details?.guardrailsReason || \"\",\n }),\n ];\n setMessages([...previousMessages, ...newMessages]);\n break;\n }\n\n // add messages to the chat\n else {\n newMessages = [...messages];\n\n for (const message of messages) {\n // execute onCoAgentStateRender handler\n if (\n message.isAgentStateMessage() &&\n !message.active &&\n !executedCoAgentStateRenders.includes(message.id) &&\n onCoAgentStateRender\n ) {\n // Do not execute a coagent action if guardrails are enabled but the status is not known\n if (guardrailsEnabled && value.generateCopilotResponse.status === undefined) {\n break;\n }\n // execute coagent action\n await onCoAgentStateRender({\n name: message.agentName,\n nodeName: message.nodeName,\n state: message.state,\n });\n executedCoAgentStateRenders.push(message.id);\n }\n }\n\n const lastAgentStateMessage = [...messages]\n .reverse()\n .find((message) => message.isAgentStateMessage());\n\n if (lastAgentStateMessage) {\n if (\n lastAgentStateMessage.state.messages &&\n lastAgentStateMessage.state.messages.length > 0\n ) {\n syncedMessages = loadMessagesFromJsonRepresentation(\n lastAgentStateMessage.state.messages,\n );\n }\n setCoagentStatesWithRef((prevAgentStates) => ({\n ...prevAgentStates,\n [lastAgentStateMessage.agentName]: {\n name: lastAgentStateMessage.agentName,\n state: lastAgentStateMessage.state,\n running: lastAgentStateMessage.running,\n active: lastAgentStateMessage.active,\n threadId: lastAgentStateMessage.threadId,\n nodeName: lastAgentStateMessage.nodeName,\n runId: lastAgentStateMessage.runId,\n },\n }));\n if (lastAgentStateMessage.running) {\n setAgentSession({\n threadId: lastAgentStateMessage.threadId,\n agentName: lastAgentStateMessage.agentName,\n nodeName: lastAgentStateMessage.nodeName,\n });\n } else {\n if (agentLock) {\n setAgentSession({\n threadId: randomId(),\n agentName: agentLock,\n nodeName: undefined,\n });\n } else {\n setAgentSession(null);\n }\n }\n }\n }\n\n if (newMessages.length > 0) {\n // Update message state\n setMessages([...previousMessages, ...newMessages]);\n }\n }\n const finalMessages = constructFinalMessages(\n [...syncedMessages, ...interruptMessages],\n previousMessages,\n newMessages,\n );\n\n let didExecuteAction = false;\n\n // execute regular action executions that are specific to the frontend (last actions)\n if (onFunctionCall) {\n // Find consecutive action execution messages at the end\n const lastMessages = [];\n for (let i = finalMessages.length - 1; i >= 0; i--) {\n const message = finalMessages[i];\n if (\n message.isActionExecutionMessage() &&\n message.status.code !== MessageStatusCode.Pending\n ) {\n lastMessages.unshift(message);\n } else {\n break;\n }\n }\n\n for (const message of lastMessages) {\n // We update the message state before calling the handler so that the render\n // function can be called with `executing` state\n setMessages(finalMessages);\n\n const action = actions.find((action) => action.name === message.name);\n\n if (action) {\n followUp = action.followUp;\n let result: any;\n let error: Error | null = null;\n try {\n result = await Promise.race([\n onFunctionCall({\n messages: previousMessages,\n name: message.name,\n args: message.arguments,\n }),\n new Promise((resolve) =>\n chatAbortControllerRef.current?.signal.addEventListener(\"abort\", () =>\n resolve(\"Operation was aborted by the user\"),\n ),\n ),\n // if the user stopped generation, we also abort consecutive actions\n new Promise((resolve) => {\n if (chatAbortControllerRef.current?.signal.aborted) {\n resolve(\"Operation was aborted by the user\");\n }\n }),\n ]);\n } catch (e) {\n error = e as Error;\n addErrorToast([error]);\n result = `Failed to execute action ${message.name}. ${error.message}`;\n console.error(`Failed to execute action ${message.name}: ${error}`);\n }\n didExecuteAction = true;\n const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);\n finalMessages.splice(\n messageIndex + 1,\n 0,\n new ResultMessage({\n id: \"result-\" + message.id,\n result: ResultMessage.encodeResult(\n error\n ? {\n content: result,\n error: JSON.parse(\n JSON.stringify(error, Object.getOwnPropertyNames(error)),\n ),\n }\n : result,\n ),\n actionExecutionId: message.id,\n actionName: message.name,\n }),\n );\n }\n }\n\n setMessages(finalMessages);\n }\n\n if (\n // if followUp is not explicitly false\n followUp !== false &&\n // and we executed an action\n (didExecuteAction ||\n // the last message is a server side result\n (!isAgentRun &&\n finalMessages.length &&\n finalMessages[finalMessages.length - 1].isResultMessage())) &&\n // the user did not stop generation\n !chatAbortControllerRef.current?.signal.aborted\n ) {\n // run the completion again and return the result\n\n // wait for next tick to make sure all the react state updates\n // - tried using react-dom's flushSync, but it did not work\n await new Promise((resolve) => setTimeout(resolve, 10));\n\n return await runChatCompletionRef.current!(finalMessages);\n } else if (chatAbortControllerRef.current?.signal.aborted) {\n // filter out all the action execution messages that do not have a consecutive matching result message\n const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {\n if (message.isActionExecutionMessage()) {\n return finalMessages.find(\n (msg, resultIndex) =>\n msg.isResultMessage() &&\n msg.actionExecutionId === message.id &&\n resultIndex === actionExecutionIndex + 1,\n );\n }\n return true;\n });\n const repairedMessageIds = repairedMessages.map((message) => message.id);\n setMessages(repairedMessages);\n\n // LangGraph needs two pieces of information to continue execution:\n // 1. The threadId\n // 2. The nodeName it came from\n // When stopping the agent, we don't know the nodeName the agent would have ended with\n // Therefore, we set the nodeName to the most reasonable thing we can guess, which\n // is \"__end__\"\n if (agentSessionRef.current?.nodeName) {\n setAgentSession({\n threadId: agentSessionRef.current.threadId,\n agentName: agentSessionRef.current.agentName,\n nodeName: \"__end__\",\n });\n }\n // only return new messages that were not filtered out\n return newMessages.filter((message) => repairedMessageIds.includes(message.id));\n } else {\n return newMessages.slice();\n }\n } finally {\n setIsLoading(false);\n }\n },\n [\n messages,\n setMessages,\n makeSystemMessageCallback,\n copilotConfig,\n setIsLoading,\n initialMessages,\n isLoading,\n actions,\n onFunctionCall,\n onCoAgentStateRender,\n setCoagentStatesWithRef,\n coagentStatesRef,\n agentSession,\n setAgentSession,\n ],\n );\n\n runChatCompletionRef.current = runChatCompletion;\n\n const runChatCompletionAndHandleFunctionCall = useAsyncCallback(\n async (messages: Message[]): Promise<void> => {\n await runChatCompletionRef.current!(messages);\n },\n [messages],\n );\n\n // Go over all events and see that they include data that should be returned to the agent\n const composeAndFlushMetaEventsInput = useCallback(\n (metaEvents: (MetaEvent | undefined | null)[]) => {\n return metaEvents.reduce((acc: MetaEventInput[], event) => {\n if (!event) return acc;\n\n switch (event.name) {\n case MetaEventName.LangGraphInterruptEvent:\n if (event.response) {\n // Flush interrupt event from state\n setLangGraphInterruptAction(null);\n const value = (event as LangGraphInterruptEvent).value;\n return [\n ...acc,\n {\n name: event.name,\n value: typeof value === \"string\" ? value : JSON.stringify(value),\n response:\n typeof event.response === \"string\"\n ? event.response\n : JSON.stringify(event.response),\n },\n ];\n }\n return acc;\n default:\n return acc;\n }\n }, []);\n },\n [setLangGraphInterruptAction],\n );\n\n const append = useAsyncCallback(\n async (message: Message, options?: AppendMessageOptions): Promise<void> => {\n if (isLoading) {\n return;\n }\n\n const newMessages = [...messages, message];\n setMessages(newMessages);\n const followUp = options?.followUp ?? true;\n if (followUp) {\n return runChatCompletionAndHandleFunctionCall(newMessages);\n }\n },\n [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall],\n );\n\n const reload = useAsyncCallback(async (): Promise<void> => {\n if (isLoading || messages.length === 0) {\n return;\n }\n let newMessages = [...messages];\n const lastMessage = messages[messages.length - 1];\n\n if (lastMessage.isTextMessage() && lastMessage.role === \"assistant\") {\n newMessages = newMessages.slice(0, -1);\n }\n\n setMessages(newMessages);\n\n return runChatCompletionAndHandleFunctionCall(newMessages);\n }, [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);\n\n const stop = (): void => {\n chatAbortControllerRef.current?.abort(\"Stop was called\");\n };\n\n return {\n append,\n reload,\n stop,\n runChatCompletion: () => runChatCompletionRef.current!(messages),\n };\n}\n\nfunction constructFinalMessages(\n syncedMessages: Message[],\n previousMessages: Message[],\n newMessages: Message[],\n): Message[] {\n const finalMessages =\n syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];\n\n if (syncedMessages.length > 0) {\n const messagesWithAgentState = [...previousMessages, ...newMessages];\n\n let previousMessageId: string | undefined = undefined;\n\n for (const message of messagesWithAgentState) {\n if (message.isAgentStateMessage()) {\n // insert this message into finalMessages after the position of previousMessageId\n const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);\n if (index !== -1) {\n finalMessages.splice(index + 1, 0, message);\n }\n }\n\n previousMessageId = message.id;\n }\n }\n\n return finalMessages;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA,SAAgB,aAAa,cAAc;AAC3C;AAAA,EAEE;AAAA,EAEA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AAYP,SAAoB,qBAAqB;AAwJlC,SAAS,QAAQ,SAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA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QAAI,cAAc,CAAC,GAAG,QAAQ;AAC9B,UAAM,cAAc,SAAS,SAAS,SAAS,CAAC;AAEhD,QAAI,YAAY,cAAc,KAAK,YAAY,SAAS,aAAa;AACnE,oBAAc,YAAY,MAAM,GAAG,EAAE;AAAA,IACvC;AAEA,gBAAY,WAAW;AAEvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D,IAAG,CAAC,WAAW,UAAU,aAAa,sCAAsC,CAAC;AAE7E,QAAM,OAAO,MAAY;AAnuB3B;AAouBI,iCAAuB,YAAvB,mBAAgC,MAAM;AAAA,EACxC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,mBAAmB,MAAM,qBAAqB,QAAS,QAAQ;AAAA,EACjE;AACF;AAEA,SAAS,uBACP,gBACA,kBACA,aACW;AACX,QAAM,gBACJ,eAAe,SAAS,IAAI,CAAC,GAAG,cAAc,IAAI,CAAC,GAAG,kBAAkB,GAAG,WAAW;AAExF,MAAI,eAAe,SAAS,GAAG;AAC7B,UAAM,yBAAyB,CAAC,GAAG,kBAAkB,GAAG,WAAW;AAEnE,QAAI,oBAAwC;AAE5C,eAAW,WAAW,wBAAwB;AAC5C,UAAI,QAAQ,oBAAoB,GAAG;AAEjC,cAAM,QAAQ,cAAc,UAAU,CAAC,QAAQ,IAAI,OAAO,iBAAiB;AAC3E,YAAI,UAAU,IAAI;AAChB,wBAAc,OAAO,QAAQ,GAAG,GAAG,OAAO;AAAA,QAC5C;AAAA,MACF;AAEA,0BAAoB,QAAQ;AAAA,IAC9B;AAAA,EACF;AAEA,SAAO;AACT;","names":["_a","messages","action","options"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-langgraph-interrupt.ts"],"sourcesContent":["import { useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { LangGraphInterruptRender } from \"../types/interrupt-action\";\nimport { useCopilotChat } from \"./use-copilot-chat\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { dataToUUID } from \"@copilotkit/shared\";\n\nexport function useLangGraphInterrupt(\n action: Omit<LangGraphInterruptRender, \"id\">,\n dependencies?: any[],\n) {\n const { setLangGraphInterruptAction, removeLangGraphInterruptAction, langGraphInterruptAction } =\n useContext(CopilotContext);\n const { runChatCompletion } = useCopilotChat();\n const { addToast } = useToast();\n\n const actionId = dataToUUID(JSON.stringify(action), \"lgAction\");\n // We only consider action to be defined once the ID is there\n const hasAction = useMemo(\n () => Boolean(langGraphInterruptAction?.id),\n [langGraphInterruptAction],\n );\n\n const isCurrentAction = useMemo(\n () => langGraphInterruptAction?.id && langGraphInterruptAction?.id === actionId,\n [langGraphInterruptAction],\n );\n\n // Run chat completion to submit a response event. Only if it's the current action\n useEffect(() => {\n if (hasAction && isCurrentAction && langGraphInterruptAction?.event?.response) {\n runChatCompletion();\n }\n }, [langGraphInterruptAction?.event?.response, runChatCompletion, hasAction, isCurrentAction]);\n\n useEffect(() => {\n if (!action) return;\n // console.log(2, { hasAction, isCurrentAction, conditions: action.conditions })\n // An action was already set, with no conditions and it's not the action we're using right now.\n // Show a warning, as this action will not be executed\n if (hasAction && !isCurrentAction && !action.conditions?.length) {\n addToast({\n type: \"warning\",\n message: \"An action is already registered for the interrupt event\",\n });\n return;\n }\n\n if (hasAction && isCurrentAction) {\n return;\n }\n\n setLangGraphInterruptAction({ ...action, id: actionId });\n }, [\n action,\n hasAction,\n isCurrentAction,\n setLangGraphInterruptAction,\n removeLangGraphInterruptAction,\n ...(dependencies || []),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,YAAY,WAAW,eAAe;AAK/C,SAAS,kBAAkB;AAEpB,SAAS,sBACd,QACA,cACA;AAVF;AAWE,QAAM,EAAE,6BAA6B,gCAAgC,yBAAyB,IAC5F,WAAW,cAAc;AAC3B,QAAM,EAAE,kBAAkB,IAAI,eAAe;AAC7C,QAAM,EAAE,SAAS,IAAI,SAAS;AAE9B,QAAM,WAAW,WAAW,KAAK,UAAU,MAAM,GAAG,UAAU;AAE9D,QAAM,YAAY;AAAA,IAChB,MAAM,QAAQ,qEAA0B,EAAE;AAAA,IAC1C,CAAC,wBAAwB;AAAA,EAC3B;AAEA,QAAM,kBAAkB;AAAA,IACtB,OAAM,qEAA0B,QAAM,qEAA0B,QAAO;AAAA,IACvE,CAAC,wBAAwB;AAAA,EAC3B;AAGA,YAAU,MAAM;AA7BlB,QAAAA;AA8BI,QAAI,aAAa,qBAAmBA,MAAA,qEAA0B,UAA1B,gBAAAA,IAAiC,WAAU;AAC7E,wBAAkB;AAAA,IACpB;AAAA,EACF,GAAG,EAAC,0EAA0B,UAA1B,mBAAiC,UAAU,mBAAmB,WAAW,eAAe,CAAC;AAE7F,YAAU,MAAM;AAnClB,QAAAA;AAoCI,QAAI,CAAC;AAAQ;AAIb,QAAI,aAAa,CAAC,mBAAmB,GAACA,MAAA,OAAO,eAAP,gBAAAA,IAAmB,SAAQ;AAC/D,eAAS;AAAA,QACP,MAAM;AAAA,QACN,SAAS;AAAA,MACX,CAAC;AACD;AAAA,IACF;AAEA,QAAI,aAAa,iBAAiB;AAChC;AAAA,IACF;AAEA,gCAA4B,iCAAK,SAAL,EAAa,IAAI,SAAS,EAAC;AAAA,EACzD,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":["_a"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-coagent.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * Usage of this hook assumes some additional setup in your application, for more information\n * on that see the CoAgents <span className=\"text-blue-500\">[getting started guide](/coagents/quickstart/langgraph)</span>.\n * </Callout>\n * <Frame className=\"my-12\">\n * <img\n * src=\"/images/coagents/SharedStateCoAgents.gif\"\n * alt=\"CoAgents demonstration\"\n * className=\"w-auto\"\n * />\n * </Frame>\n *\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of an agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences agentic copilots, or CoAgents for short.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCoAgent } from \"@copilotkit/react-core\";\n *\n * type AgentState = {\n * count: number;\n * }\n *\n * const agent = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * ```\n *\n * `useCoAgent` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * name, // The name of the agent currently being used.\n * nodeName, // The name of the current LangGraph node.\n * state, // The current state of the agent.\n * setState, // A function to update the state of the agent.\n * running, // A boolean indicating if the agent is currently running.\n * start, // A function to start the agent.\n * stop, // A function to stop the agent.\n * run, // A function to re-run the agent. Takes a HintFunction to inform the agent why it is being re-run.\n * } = agent;\n * ```\n *\n * Finally we can leverage these properties to create reactive experiences with the agent!\n *\n * ```tsx\n * const { state, setState } = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * return (\n * <div>\n * <p>Count: {state.count}</p>\n * <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>\n * </div>\n * );\n * ```\n *\n * This reactivity is bidirectional, meaning that changes to the state from the agent will be reflected in the UI and vice versa.\n *\n * ## Parameters\n * <PropertyReference name=\"options\" type=\"UseCoagentOptions<T>\" required>\n * The options to use when creating the coagent.\n * <PropertyReference name=\"name\" type=\"string\" required>\n * The name of the agent to use.\n * </PropertyReference>\n * <PropertyReference name=\"initialState\" type=\"T | any\">\n * The initial state of the agent.\n * </PropertyReference>\n * <PropertyReference name=\"state\" type=\"T | any\">\n * State to manage externally if you are using this hook with external state management.\n * </PropertyReference>\n * <PropertyReference name=\"setState\" type=\"(newState: T | ((prevState: T | undefined) => T)) => void\">\n * A function to update the state of the agent if you are using this hook with external state management.\n * </PropertyReference>\n * </PropertyReference>\n */\n\nimport { useEffect, useRef } from \"react\";\nimport {\n CopilotContextParams,\n CopilotMessagesContextParams,\n useCopilotContext,\n useCopilotMessagesContext,\n} from \"../context\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { useCopilotChat } from \"./use-copilot-chat\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\n\ninterface WithInternalStateManagementAndInitial<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The initial state of the agent.\n */\n initialState: T;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ninterface WithInternalStateManagement {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * Optional initialState with default type any\n */\n initialState?: any;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ninterface WithExternalStateManagement<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ntype UseCoagentOptions<T> =\n | WithInternalStateManagementAndInitial<T>\n | WithInternalStateManagement\n | WithExternalStateManagement<T>;\n\nexport interface UseCoagentReturnType<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The name of the current LangGraph node.\n */\n nodeName?: string;\n /**\n * The ID of the thread the agent is running in.\n */\n threadId?: string;\n /**\n * A boolean indicating if the agent is currently running.\n */\n running: boolean;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * A function to start the agent.\n */\n start: () => void;\n /**\n * A function to stop the agent.\n */\n stop: () => void;\n /**\n * A function to re-run the agent. The hint function can be used to provide a hint to the agent\n * about why it is being re-run again.\n */\n run: (hint?: HintFunction) => Promise<void>;\n}\n\nexport interface HintFunctionParams {\n /**\n * The previous state of the agent.\n */\n previousState: any;\n /**\n * The current state of the agent.\n */\n currentState: any;\n}\n\nexport type HintFunction = (params: HintFunctionParams) => Message | undefined;\n\n/**\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of the agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences \"agentic copilots\". To get started using agentic copilots, which\n * we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart/langgraph.\n */\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const generalContext = useCopilotContext();\n const { availableAgents } = generalContext;\n const { addToast } = useToast();\n const lastLoadedThreadId = useRef<string>();\n const lastLoadedState = useRef<any>();\n\n const isExternalStateManagement = (\n options: UseCoagentOptions<T>,\n ): options is WithExternalStateManagement<T> => {\n return \"state\" in options && \"setState\" in options;\n };\n\n const { name } = options;\n useEffect(() => {\n if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {\n const message = `(useCoAgent): Agent \"${name}\" not found. Make sure the agent exists and is properly configured.`;\n console.warn(message);\n addToast({ type: \"warning\", message });\n }\n }, [availableAgents]);\n\n const isInternalStateManagementWithInitial = (\n options: UseCoagentOptions<T>,\n ): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n };\n\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =\n context;\n const { appendMessage, runChatCompletion } = useCopilotChat();\n\n const getCoagentState = (coagentStates: Record<string, CoagentState>, name: string) => {\n if (coagentStates[name]) {\n return coagentStates[name];\n } else {\n return {\n name,\n state: isInternalStateManagementWithInitial(options) ? options.initialState : {},\n configurable: options.configurable ?? {},\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n };\n }\n };\n\n const runtimeClient = useCopilotRuntimeClient({\n url: copilotApiConfig.chatApiEndpoint,\n publicApiKey: copilotApiConfig.publicApiKey,\n credentials: copilotApiConfig.credentials,\n });\n\n // if we manage state internally, we need to provide a function to set the state\n const setState = (newState: T | ((prevState: T | undefined) => T)) => {\n let coagentState: CoagentState = getCoagentState(coagentStatesRef.current || {}, name);\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n setCoagentStatesWithRef({\n ...coagentStatesRef.current,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n });\n };\n\n useEffect(() => {\n const fetchAgentState = async () => {\n if (!threadId || threadId === lastLoadedThreadId.current) return;\n\n const result = await runtimeClient.loadAgentState({\n threadId,\n agentName: name,\n });\n\n const newState = result.data?.loadAgentState?.state;\n if (newState === lastLoadedState.current) return;\n\n if (result.data?.loadAgentState?.threadExists && newState && newState != \"{}\") {\n lastLoadedState.current = newState;\n lastLoadedThreadId.current = threadId;\n const fetchedState = JSON.parse(newState);\n isExternalStateManagement(options)\n ? options.setState(fetchedState)\n : setState(fetchedState);\n }\n };\n void fetchAgentState();\n }, [threadId]);\n\n const coagentState = getCoagentState(coagentStates, name);\n\n // Sync internal state with external state if state management is external\n useEffect(() => {\n if (isExternalStateManagement(options)) {\n setState(options.state);\n } else if (coagentStates[name] === undefined) {\n setState(options.initialState === undefined ? {} : options.initialState);\n }\n }, [\n isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined,\n // reset initialstate on reset\n coagentStates[name] === undefined,\n ]);\n\n const runAgentCallback = useAsyncCallback(\n async (hint?: HintFunction) => {\n await runAgent(name, context, appendMessage, runChatCompletion, hint);\n },\n [name, context, appendMessage, runChatCompletion],\n );\n\n // Return the state and setState function\n return {\n name,\n nodeName: coagentState.nodeName,\n threadId: coagentState.threadId,\n running: coagentState.running,\n state: coagentState.state,\n setState: isExternalStateManagement(options) ? options.setState : setState,\n start: () => startAgent(name, context),\n stop: () => stopAgent(name, context),\n run: runAgentCallback,\n };\n}\n\nexport function startAgent(name: string, context: CopilotContextParams) {\n const { setAgentSession } = context;\n setAgentSession({\n agentName: name,\n });\n}\n\nexport function stopAgent(name: string, context: CopilotContextParams) {\n const { agentSession, setAgentSession } = context;\n if (agentSession && agentSession.agentName === name) {\n setAgentSession(null);\n context.setCoagentStates((prevAgentStates) => {\n return {\n ...prevAgentStates,\n [name]: {\n ...prevAgentStates[name],\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n },\n };\n });\n } else {\n console.warn(`No agent session found for ${name}`);\n }\n}\n\nexport async function runAgent(\n name: string,\n context: CopilotContextParams & CopilotMessagesContextParams,\n appendMessage: (message: Message) => Promise<void>,\n runChatCompletion: () => Promise<Message[]>,\n hint?: HintFunction,\n) {\n const { agentSession, setAgentSession } = context;\n if (!agentSession || agentSession.agentName !== name) {\n setAgentSession({\n agentName: name,\n });\n }\n\n let previousState: any = null;\n for (let i = context.messages.length - 1; i >= 0; i--) {\n const message = context.messages[i];\n if (message.isAgentStateMessage() && message.agentName === name) {\n previousState = message.state;\n }\n }\n\n let state = context.coagentStatesRef.current?.[name]?.state || {};\n\n if (hint) {\n const hintMessage = hint({ previousState, currentState: state });\n if (hintMessage) {\n await appendMessage(hintMessage);\n } else {\n await runChatCompletion();\n }\n } else {\n await runChatCompletion();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0FA,SAAS,WAAW,cAAc;AA+H3B,SAAS,WAAoB,SAAwD;AAC1F,QAAM,iBAAiB,kBAAkB;AACzC,QAAM,EAAE,gBAAgB,IAAI;AAC5B,QAAM,EAAE,SAAS,IAAI,SAAS;AAC9B,QAAM,qBAAqB,OAAe;AAC1C,QAAM,kBAAkB,OAAY;AAEpC,QAAM,4BAA4B,CAChCA,aAC8C;AAC9C,WAAO,WAAWA,YAAW,cAAcA;AAAA,EAC7C;AAEA,QAAM,EAAE,KAAK,IAAI;AACjB,YAAU,MAAM;AACd,SAAI,mDAAiB,WAAU,CAAC,gBAAgB,KAAK,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,cAAc,KAAK,EAAE,eAAe,cAAc,MAAM,CAAC;AAC/D,UAAI,aAAa;AACf,cAAM,cAAc,WAAW;AAAA,MACjC,OAAO;AACL,cAAM,kBAAkB;AAAA,MAC1B;AAAA,IACF,OAAO;AACL,YAAM,kBAAkB;AAAA,IAC1B;AAAA,EACF;AAAA;","names":["options","coagentStates","name","coagentState"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-langgraph-interrupt-render.ts"],"sourcesContent":["import { useCopilotContext } from \"../context\";\nimport React, { useCallback } from \"react\";\nimport { executeConditions } from \"@copilotkit/shared\";\n\ntype InterruptProps = {\n event: any;\n result: any;\n render: (props: {\n event: any;\n result: any;\n resolve: (response: string) => void;\n }) => string | React.ReactElement;\n resolve: (response: string) => void;\n};\n\nconst InterruptRenderer: React.FC<InterruptProps> = ({ event, result, render, resolve }) => {\n return render({ event, result, resolve });\n};\n\nexport function useLangGraphInterruptRender(): string | React.ReactElement | null {\n const { langGraphInterruptAction, setLangGraphInterruptAction } = useCopilotContext();\n\n const responseRef = React.useRef<string>();\n const resolveInterrupt = useCallback(\n (response: string) => {\n responseRef.current = response;\n // Use setTimeout to defer the state update to next tick\n setTimeout(() => {\n setLangGraphInterruptAction({ event: { response } });\n }, 0);\n },\n [setLangGraphInterruptAction],\n );\n\n if (\n !langGraphInterruptAction ||\n !langGraphInterruptAction.event ||\n !langGraphInterruptAction.render\n )\n return null;\n\n const { render, handler, event, conditions } = langGraphInterruptAction;\n\n const conditionsMet = executeConditions({ conditions, value: event.value });\n if (!conditionsMet) {\n return null;\n }\n\n let result = null;\n if (handler) {\n result = handler({\n event,\n resolve: resolveInterrupt,\n });\n }\n\n return React.createElement(InterruptRenderer, {\n event,\n result,\n render,\n resolve: resolveInterrupt,\n });\n}\n"],"mappings":";;;;;AACA,OAAO,SAAS,mBAAmB;AACnC,SAAS,yBAAyB;AAalC,IAAM,oBAA8C,CAAC,EAAE,OAAO,QAAQ,QAAQ,QAAQ,MAAM;AAC1F,SAAO,OAAO,EAAE,OAAO,QAAQ,QAAQ,CAAC;AAC1C;AAEO,SAAS,8BAAkE;AAChF,QAAM,EAAE,0BAA0B,4BAA4B,IAAI,kBAAkB;AAEpF,QAAM,cAAc,MAAM,OAAe;AACzC,QAAM,mBAAmB;AAAA,IACvB,CAAC,aAAqB;AACpB,kBAAY,UAAU;AAEtB,iBAAW,MAAM;AACf,oCAA4B,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAAA,MACrD,GAAG,CAAC;AAAA,IACN;AAAA,IACA,CAAC,2BAA2B;AAAA,EAC9B;AAEA,MACE,CAAC,4BACD,CAAC,yBAAyB,SAC1B,CAAC,yBAAyB;AAE1B,WAAO;AAET,QAAM,EAAE,QAAQ,SAAS,OAAO,WAAW,IAAI;AAE/C,QAAM,gBAAgB,kBAAkB,EAAE,YAAY,OAAO,MAAM,MAAM,CAAC;AAC1E,MAAI,CAAC,eAAe;AAClB,WAAO;AAAA,EACT;AAEA,MAAI,SAAS;AACb,MAAI,SAAS;AACX,aAAS,QAAQ;AAAA,MACf;AAAA,MACA,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AAEA,SAAO,MAAM,cAAc,mBAAmB;AAAA,IAC5C;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS;AAAA,EACX,CAAC;AACH;","names":[]}
|