@copilotkit/react-core 0.0.0-feat-dynamic-copilotcloud-qa-20250117190454 → 0.0.0-feat-langgraph-interrupt-hook-20250203094147
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 +125 -3
- package/dist/{chunk-KSPLXKSX.mjs → chunk-5SGZKEPX.mjs} +28 -7
- package/dist/chunk-5SGZKEPX.mjs.map +1 -0
- package/dist/{chunk-FQ53HQYZ.mjs → chunk-7HWSAEOY.mjs} +2 -2
- package/dist/{chunk-EWKJMQOF.mjs → chunk-AL3MOOKF.mjs} +3 -3
- package/dist/{chunk-6BQVZFNO.mjs → chunk-CQWEXISS.mjs} +2 -2
- package/dist/chunk-D34OH4VN.mjs +201 -0
- package/dist/chunk-D34OH4VN.mjs.map +1 -0
- package/dist/{chunk-4TDYSNUD.mjs → chunk-DEPX7JPX.mjs} +20 -10
- package/dist/chunk-DEPX7JPX.mjs.map +1 -0
- package/dist/{chunk-U6JICUGO.mjs → chunk-FMROB4TE.mjs} +78 -21
- package/dist/chunk-FMROB4TE.mjs.map +1 -0
- package/dist/{chunk-LHKIIAUQ.mjs → chunk-GANI4ZZM.mjs} +10 -10
- package/dist/chunk-GANI4ZZM.mjs.map +1 -0
- package/dist/{chunk-XOQMISG3.mjs → chunk-H2OR7FUU.mjs} +2 -2
- package/dist/{chunk-LNI2Z45S.mjs → chunk-HZCROITJ.mjs} +5 -3
- package/dist/chunk-HZCROITJ.mjs.map +1 -0
- package/dist/chunk-IGLITGLC.mjs +1 -0
- package/dist/chunk-IHWTLSHY.mjs +51 -0
- package/dist/chunk-IHWTLSHY.mjs.map +1 -0
- package/dist/{chunk-2KT73RVG.mjs → chunk-IU7OLWWW.mjs} +77 -34
- package/dist/chunk-IU7OLWWW.mjs.map +1 -0
- package/dist/chunk-LH3RYVV2.mjs +66 -0
- package/dist/chunk-LH3RYVV2.mjs.map +1 -0
- package/dist/chunk-PMAFHQ7P.mjs +65 -0
- package/dist/chunk-PMAFHQ7P.mjs.map +1 -0
- package/dist/chunk-UMQQA6HK.mjs +80 -0
- package/dist/chunk-UMQQA6HK.mjs.map +1 -0
- package/dist/{chunk-22XKTBPF.mjs → chunk-WU53OU3E.mjs} +3 -3
- package/dist/{chunk-HIUT2NLX.mjs → chunk-XOHLZIZK.mjs} +2 -2
- package/dist/chunk-ZAU4EMBM.mjs +48 -0
- package/dist/chunk-ZAU4EMBM.mjs.map +1 -0
- package/dist/{chunk-WXREY7UP.mjs → chunk-ZM56S3LJ.mjs} +9 -3
- package/dist/chunk-ZM56S3LJ.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +4 -12
- package/dist/components/copilot-provider/copilot-messages.js +129 -18
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
- package/dist/components/copilot-provider/copilot-messages.mjs +2 -1
- package/dist/components/copilot-provider/copilotkit-props.d.ts +20 -1
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +2 -1
- package/dist/components/copilot-provider/copilotkit.js +401 -39
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +8 -6
- package/dist/components/copilot-provider/index.d.ts +1 -0
- package/dist/components/copilot-provider/index.js +401 -39
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +8 -6
- package/dist/components/error-boundary/error-boundary.d.ts +12 -3
- package/dist/components/error-boundary/error-boundary.js +327 -19
- package/dist/components/error-boundary/error-boundary.js.map +1 -1
- package/dist/components/error-boundary/error-boundary.mjs +4 -2
- package/dist/components/error-boundary/error-utils.js +14 -2
- package/dist/components/error-boundary/error-utils.js.map +1 -1
- package/dist/components/error-boundary/error-utils.mjs +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +401 -39
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +8 -6
- package/dist/components/toast/toast-provider.js +14 -2
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/components/usage-banner.d.ts +22 -0
- package/dist/components/usage-banner.js +226 -0
- package/dist/components/usage-banner.js.map +1 -0
- package/dist/components/usage-banner.mjs +10 -0
- package/dist/components/usage-banner.mjs.map +1 -0
- package/dist/context/copilot-context.d.ts +13 -7
- package/dist/context/copilot-context.js +8 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.js +8 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/hooks/index.d.ts +4 -1
- package/dist/hooks/index.js +217 -35
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +31 -21
- package/dist/hooks/use-chat.d.ts +15 -4
- package/dist/hooks/use-chat.js +83 -17
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.js +8 -2
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +3 -3
- package/dist/hooks/use-coagent.d.ts +1 -0
- package/dist/hooks/use-coagent.js +117 -27
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +11 -9
- package/dist/hooks/use-copilot-action.js +12 -4
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +3 -3
- package/dist/hooks/use-copilot-authenticated-action.d.ts +12 -2
- package/dist/hooks/use-copilot-authenticated-action.js +20 -12
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +6 -6
- package/dist/hooks/use-copilot-chat.d.ts +2 -1
- package/dist/hooks/use-copilot-chat.js +97 -25
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +10 -8
- package/dist/hooks/use-copilot-readable.js +8 -2
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +14 -2
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/hooks/use-langgraph-interrupt-render.d.ts +5 -0
- package/dist/hooks/use-langgraph-interrupt-render.js +184 -0
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -0
- package/dist/hooks/use-langgraph-interrupt-render.mjs +11 -0
- package/dist/hooks/use-langgraph-interrupt-render.mjs.map +1 -0
- package/dist/hooks/use-langgraph-interrupt.d.ts +45 -0
- package/dist/hooks/use-langgraph-interrupt.js +1050 -0
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -0
- package/dist/hooks/use-langgraph-interrupt.mjs +25 -0
- package/dist/hooks/use-langgraph-interrupt.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.js +8 -2
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/index.d.ts +4 -1
- package/dist/index.js +642 -113
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +35 -25
- package/dist/lib/copilot-task.d.ts +1 -0
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +9 -7
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -7
- package/dist/lib/status-checker.d.ts +17 -0
- package/dist/lib/status-checker.js +102 -0
- package/dist/lib/status-checker.js.map +1 -0
- package/dist/lib/status-checker.mjs +8 -0
- package/dist/lib/status-checker.mjs.map +1 -0
- package/dist/types/interrupt-action.d.ts +35 -0
- package/dist/types/interrupt-action.js +19 -0
- package/dist/types/interrupt-action.js.map +1 -0
- package/dist/types/interrupt-action.mjs +1 -0
- package/dist/types/interrupt-action.mjs.map +1 -0
- package/dist/utils/extract.d.ts +1 -0
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +8 -6
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +8 -6
- package/package.json +5 -4
- package/src/components/copilot-provider/copilot-messages.tsx +25 -3
- package/src/components/copilot-provider/copilotkit-props.tsx +21 -1
- package/src/components/copilot-provider/copilotkit.tsx +93 -26
- package/src/components/error-boundary/error-boundary.tsx +51 -4
- package/src/components/error-boundary/error-utils.tsx +11 -6
- package/src/components/usage-banner.tsx +205 -0
- package/src/context/copilot-context.tsx +28 -10
- package/src/hooks/index.ts +3 -1
- package/src/hooks/use-chat.ts +96 -12
- package/src/hooks/use-coagent.ts +30 -4
- package/src/hooks/use-copilot-authenticated-action.ts +17 -7
- package/src/hooks/use-copilot-chat.ts +12 -2
- package/src/hooks/use-langgraph-interrupt-render.ts +63 -0
- package/src/hooks/use-langgraph-interrupt.ts +100 -0
- package/src/lib/status-checker.ts +64 -0
- package/src/types/interrupt-action.ts +35 -0
- package/dist/chunk-2KT73RVG.mjs.map +0 -1
- package/dist/chunk-4TDYSNUD.mjs.map +0 -1
- package/dist/chunk-KSPLXKSX.mjs.map +0 -1
- package/dist/chunk-LHKIIAUQ.mjs.map +0 -1
- package/dist/chunk-LNI2Z45S.mjs.map +0 -1
- package/dist/chunk-NR2BTA6Z.mjs +0 -1
- package/dist/chunk-U6JICUGO.mjs.map +0 -1
- package/dist/chunk-VNTDIJSG.mjs +0 -37
- package/dist/chunk-VNTDIJSG.mjs.map +0 -1
- package/dist/chunk-WXREY7UP.mjs.map +0 -1
- package/dist/chunk-XXR4QFAQ.mjs +0 -29
- package/dist/chunk-XXR4QFAQ.mjs.map +0 -1
- /package/dist/{chunk-FQ53HQYZ.mjs.map → chunk-7HWSAEOY.mjs.map} +0 -0
- /package/dist/{chunk-EWKJMQOF.mjs.map → chunk-AL3MOOKF.mjs.map} +0 -0
- /package/dist/{chunk-6BQVZFNO.mjs.map → chunk-CQWEXISS.mjs.map} +0 -0
- /package/dist/{chunk-XOQMISG3.mjs.map → chunk-H2OR7FUU.mjs.map} +0 -0
- /package/dist/{chunk-NR2BTA6Z.mjs.map → chunk-IGLITGLC.mjs.map} +0 -0
- /package/dist/{chunk-22XKTBPF.mjs.map → chunk-WU53OU3E.mjs.map} +0 -0
- /package/dist/{chunk-HIUT2NLX.mjs.map → chunk-XOHLZIZK.mjs.map} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,134 @@
|
|
|
1
1
|
# ui
|
|
2
2
|
|
|
3
|
-
## 0.0.0-feat-
|
|
3
|
+
## 0.0.0-feat-langgraph-interrupt-hook-20250203094147
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- 3b1b633: - feat(interrupt): support LG interrupt with useLangGraphInterrupt hook
|
|
8
|
+
- chore(interrupt): add e2e test to interrupt functionality
|
|
9
|
+
- feat(interrupt): add support for multiple interrupts and conditions
|
|
10
|
+
- Updated dependencies [3b1b633]
|
|
11
|
+
- Updated dependencies [0bbb4ab]
|
|
12
|
+
- @copilotkit/runtime-client-gql@0.0.0-feat-langgraph-interrupt-hook-20250203094147
|
|
13
|
+
- @copilotkit/shared@0.0.0-feat-langgraph-interrupt-hook-20250203094147
|
|
14
|
+
|
|
15
|
+
## 1.5.15-next.3
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- @copilotkit/runtime-client-gql@1.5.15-next.3
|
|
20
|
+
- @copilotkit/shared@1.5.15-next.3
|
|
21
|
+
|
|
22
|
+
## 1.5.15-next.2
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- @copilotkit/runtime-client-gql@1.5.15-next.2
|
|
27
|
+
- @copilotkit/shared@1.5.15-next.2
|
|
28
|
+
|
|
29
|
+
## 1.5.15-next.1
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- Updated dependencies [0bbb4ab]
|
|
34
|
+
- @copilotkit/runtime-client-gql@1.5.15-next.1
|
|
35
|
+
- @copilotkit/shared@1.5.15-next.1
|
|
36
|
+
|
|
37
|
+
## 1.5.15-next.0
|
|
38
|
+
|
|
39
|
+
### Patch Changes
|
|
40
|
+
|
|
41
|
+
- @copilotkit/runtime-client-gql@1.5.15-next.0
|
|
42
|
+
- @copilotkit/shared@1.5.15-next.0
|
|
43
|
+
|
|
44
|
+
## 1.5.14
|
|
45
|
+
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- 0061f65: - feat: allows dev mode for cloud onboarding flow
|
|
49
|
+
- Updated dependencies [0061f65]
|
|
50
|
+
- @copilotkit/shared@1.5.14
|
|
51
|
+
- @copilotkit/runtime-client-gql@1.5.14
|
|
52
|
+
|
|
53
|
+
## 1.5.14-next.0
|
|
54
|
+
|
|
55
|
+
### Patch Changes
|
|
56
|
+
|
|
57
|
+
- 0061f65: - feat: allows dev mode for cloud onboarding flow
|
|
58
|
+
- Updated dependencies [0061f65]
|
|
59
|
+
- @copilotkit/shared@1.5.14-next.0
|
|
60
|
+
- @copilotkit/runtime-client-gql@1.5.14-next.0
|
|
61
|
+
|
|
62
|
+
## 1.5.13
|
|
63
|
+
|
|
64
|
+
### Patch Changes
|
|
65
|
+
|
|
66
|
+
- @copilotkit/runtime-client-gql@1.5.13
|
|
67
|
+
- @copilotkit/shared@1.5.13
|
|
68
|
+
|
|
69
|
+
## 1.5.13-next.0
|
|
70
|
+
|
|
71
|
+
### Patch Changes
|
|
72
|
+
|
|
73
|
+
- @copilotkit/runtime-client-gql@1.5.13-next.0
|
|
74
|
+
- @copilotkit/shared@1.5.13-next.0
|
|
75
|
+
|
|
76
|
+
## 1.5.12
|
|
77
|
+
|
|
78
|
+
### Patch Changes
|
|
79
|
+
|
|
80
|
+
- 926499b: - Load the previous state of an agent if `threadId` is provided to CopilotKit, including all messages
|
|
81
|
+
- 6136a57: - fix(errors): add custom error classes to better describe library errors
|
|
82
|
+
- fix(errors): use new errors in error handling
|
|
83
|
+
- chore: add documentation and links to respective errors
|
|
84
|
+
- cb43c05: - fix: set up managed LLM retries and report error to render method
|
|
7
85
|
- Updated dependencies [fb87bcf]
|
|
8
|
-
|
|
9
|
-
|
|
86
|
+
- Updated dependencies [926499b]
|
|
87
|
+
- Updated dependencies [6136a57]
|
|
88
|
+
- @copilotkit/runtime-client-gql@1.5.12
|
|
89
|
+
- @copilotkit/shared@1.5.12
|
|
90
|
+
|
|
91
|
+
## 1.5.12-next.7
|
|
92
|
+
|
|
93
|
+
### Patch Changes
|
|
94
|
+
|
|
95
|
+
- 926499b: - Load the previous state of an agent if `threadId` is provided to CopilotKit, including all messages
|
|
96
|
+
- Updated dependencies [926499b]
|
|
97
|
+
- @copilotkit/runtime-client-gql@1.5.12-next.7
|
|
98
|
+
- @copilotkit/shared@1.5.12-next.7
|
|
99
|
+
|
|
100
|
+
## 1.5.12-next.6
|
|
101
|
+
|
|
102
|
+
### Patch Changes
|
|
103
|
+
|
|
104
|
+
- 6136a57: - fix(errors): add custom error classes to better describe library errors
|
|
105
|
+
- fix(errors): use new errors in error handling
|
|
106
|
+
- chore: add documentation and links to respective errors
|
|
107
|
+
- Updated dependencies [6136a57]
|
|
108
|
+
- @copilotkit/runtime-client-gql@1.5.12-next.6
|
|
109
|
+
- @copilotkit/shared@1.5.12-next.6
|
|
110
|
+
|
|
111
|
+
## 1.5.12-next.5
|
|
112
|
+
|
|
113
|
+
### Patch Changes
|
|
114
|
+
|
|
115
|
+
- @copilotkit/runtime-client-gql@1.5.12-next.5
|
|
116
|
+
- @copilotkit/shared@1.5.12-next.5
|
|
117
|
+
|
|
118
|
+
## 1.5.12-next.4
|
|
119
|
+
|
|
120
|
+
### Patch Changes
|
|
121
|
+
|
|
122
|
+
- @copilotkit/runtime-client-gql@1.5.12-next.4
|
|
123
|
+
- @copilotkit/shared@1.5.12-next.4
|
|
124
|
+
|
|
125
|
+
## 1.5.12-next.3
|
|
126
|
+
|
|
127
|
+
### Patch Changes
|
|
128
|
+
|
|
129
|
+
- cb43c05: - fix: set up managed LLM retries and report error to render method
|
|
130
|
+
- @copilotkit/runtime-client-gql@1.5.12-next.3
|
|
131
|
+
- @copilotkit/shared@1.5.12-next.3
|
|
10
132
|
|
|
11
133
|
## 1.5.12-next.2
|
|
12
134
|
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotChat
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-DEPX7JPX.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useCopilotContext
|
|
6
|
+
} from "./chunk-ZM56S3LJ.mjs";
|
|
4
7
|
import {
|
|
5
8
|
useCopilotMessagesContext
|
|
6
9
|
} from "./chunk-DCTJZ742.mjs";
|
|
7
10
|
import {
|
|
8
|
-
|
|
9
|
-
} from "./chunk-
|
|
11
|
+
useCopilotRuntimeClient
|
|
12
|
+
} from "./chunk-CQWEXISS.mjs";
|
|
10
13
|
import {
|
|
11
14
|
useAsyncCallback,
|
|
12
15
|
useToast
|
|
13
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-HZCROITJ.mjs";
|
|
14
17
|
import {
|
|
15
18
|
__async,
|
|
16
19
|
__spreadProps,
|
|
@@ -39,7 +42,7 @@ function useCoAgent(options) {
|
|
|
39
42
|
};
|
|
40
43
|
const messagesContext = useCopilotMessagesContext();
|
|
41
44
|
const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
|
|
42
|
-
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef } = context;
|
|
45
|
+
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
|
|
43
46
|
const { appendMessage, runChatCompletion } = useCopilotChat();
|
|
44
47
|
const getCoagentState = (coagentStates2, name2) => {
|
|
45
48
|
if (coagentStates2[name2]) {
|
|
@@ -56,6 +59,11 @@ function useCoAgent(options) {
|
|
|
56
59
|
};
|
|
57
60
|
}
|
|
58
61
|
};
|
|
62
|
+
const runtimeClient = useCopilotRuntimeClient({
|
|
63
|
+
url: copilotApiConfig.chatApiEndpoint,
|
|
64
|
+
publicApiKey: copilotApiConfig.publicApiKey,
|
|
65
|
+
credentials: copilotApiConfig.credentials
|
|
66
|
+
});
|
|
59
67
|
const setState = (newState) => {
|
|
60
68
|
let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
|
|
61
69
|
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
@@ -65,8 +73,21 @@ function useCoAgent(options) {
|
|
|
65
73
|
})
|
|
66
74
|
}));
|
|
67
75
|
};
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const fetchAgentState = () => __async(this, null, function* () {
|
|
78
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
79
|
+
const result = yield runtimeClient.loadAgentState({
|
|
80
|
+
threadId,
|
|
81
|
+
agentName: name
|
|
82
|
+
});
|
|
83
|
+
if (((_b = (_a = result.data) == null ? void 0 : _a.loadAgentState) == null ? void 0 : _b.threadExists) && ((_d = (_c = result.data) == null ? void 0 : _c.loadAgentState) == null ? void 0 : _d.state) && ((_f = (_e = result.data) == null ? void 0 : _e.loadAgentState) == null ? void 0 : _f.state) != "{}") {
|
|
84
|
+
const fetchedState = JSON.parse((_h = (_g = result.data) == null ? void 0 : _g.loadAgentState) == null ? void 0 : _h.state);
|
|
85
|
+
isExternalStateManagement(options) ? options.setState(fetchedState) : setState(fetchedState);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
void fetchAgentState();
|
|
89
|
+
}, [threadId]);
|
|
68
90
|
const coagentState = getCoagentState(coagentStates, name);
|
|
69
|
-
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
70
91
|
useEffect(() => {
|
|
71
92
|
if (isExternalStateManagement(options)) {
|
|
72
93
|
setState(options.state);
|
|
@@ -157,4 +178,4 @@ export {
|
|
|
157
178
|
stopAgent,
|
|
158
179
|
runAgent
|
|
159
180
|
};
|
|
160
|
-
//# sourceMappingURL=chunk-
|
|
181
|
+
//# sourceMappingURL=chunk-5SGZKEPX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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)</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 } 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 { loadMessagesFromJsonRepresentation, Message } from \"@copilotkit/runtime-client-gql\";\nimport { flushSync } from \"react-dom\";\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\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\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\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.\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 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 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 const result = await runtimeClient.loadAgentState({\n threadId: threadId,\n agentName: name,\n });\n if (\n result.data?.loadAgentState?.threadExists &&\n result.data?.loadAgentState?.state &&\n result.data?.loadAgentState?.state != \"{}\"\n ) {\n const fetchedState = JSON.parse(result.data?.loadAgentState?.state);\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,iBAAiB;AAoHnB,SAAS,WAAoB,SAAwD;AAC1F,QAAM,iBAAiB,kBAAkB;AACzC,QAAM,EAAE,gBAAgB,IAAI;AAC5B,QAAM,EAAE,SAAS,IAAI,SAAS;AAC9B,QAAM,4BAA4B,CAChCA,aAC8C;AAC9C,WAAO,WAAWA,YAAW,cAAcA;AAAA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AAc,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,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-ZM56S3LJ.mjs";
|
|
4
4
|
|
|
5
5
|
// src/hooks/use-copilot-readable.ts
|
|
6
6
|
import { useEffect, useRef } from "react";
|
|
@@ -25,4 +25,4 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
25
25
|
export {
|
|
26
26
|
useCopilotReadable
|
|
27
27
|
};
|
|
28
|
-
//# sourceMappingURL=chunk-
|
|
28
|
+
//# sourceMappingURL=chunk-7HWSAEOY.mjs.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-ZM56S3LJ.mjs";
|
|
4
4
|
import {
|
|
5
5
|
useAsyncCallback
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-HZCROITJ.mjs";
|
|
7
7
|
import {
|
|
8
8
|
__async,
|
|
9
9
|
__spreadValues
|
|
@@ -104,4 +104,4 @@ function isFrontendAction(action) {
|
|
|
104
104
|
export {
|
|
105
105
|
useCopilotAction
|
|
106
106
|
};
|
|
107
|
-
//# sourceMappingURL=chunk-
|
|
107
|
+
//# sourceMappingURL=chunk-AL3MOOKF.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useErrorToast,
|
|
3
3
|
useToast
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-HZCROITJ.mjs";
|
|
5
5
|
import {
|
|
6
6
|
__spreadProps,
|
|
7
7
|
__spreadValues
|
|
@@ -32,4 +32,4 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
32
32
|
export {
|
|
33
33
|
useCopilotRuntimeClient
|
|
34
34
|
};
|
|
35
|
-
//# sourceMappingURL=chunk-
|
|
35
|
+
//# sourceMappingURL=chunk-CQWEXISS.mjs.map
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// src/components/usage-banner.tsx
|
|
2
|
+
import { Severity, ERROR_NAMES } from "@copilotkit/shared";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
var defaultIcons = {
|
|
5
|
+
[Severity.Error]: /* @__PURE__ */ jsxs(
|
|
6
|
+
"svg",
|
|
7
|
+
{
|
|
8
|
+
viewBox: "0 0 24 24",
|
|
9
|
+
width: "20",
|
|
10
|
+
height: "20",
|
|
11
|
+
stroke: "currentColor",
|
|
12
|
+
strokeWidth: "2",
|
|
13
|
+
fill: "none",
|
|
14
|
+
strokeLinecap: "round",
|
|
15
|
+
strokeLinejoin: "round",
|
|
16
|
+
children: [
|
|
17
|
+
/* @__PURE__ */ jsx("circle", { cx: "12", cy: "12", r: "10" }),
|
|
18
|
+
/* @__PURE__ */ jsx("line", { x1: "15", y1: "9", x2: "9", y2: "15" }),
|
|
19
|
+
/* @__PURE__ */ jsx("line", { x1: "9", y1: "9", x2: "15", y2: "15" })
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
};
|
|
24
|
+
function UsageBanner({
|
|
25
|
+
severity = Severity.Error,
|
|
26
|
+
message = "",
|
|
27
|
+
icon,
|
|
28
|
+
actions
|
|
29
|
+
}) {
|
|
30
|
+
if (!message || !severity) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const Icon = icon || defaultIcons[severity];
|
|
34
|
+
const bgColor = {
|
|
35
|
+
info: "#dbeafe",
|
|
36
|
+
warning: "#fef3c7",
|
|
37
|
+
error: "#fee2e2"
|
|
38
|
+
}[severity];
|
|
39
|
+
const textColor = {
|
|
40
|
+
info: "#1e40af",
|
|
41
|
+
warning: "#854d0e",
|
|
42
|
+
error: "#991b1b"
|
|
43
|
+
}[severity];
|
|
44
|
+
const iconColor = {
|
|
45
|
+
info: "#3b82f6",
|
|
46
|
+
warning: "#eab308",
|
|
47
|
+
error: "#ef4444"
|
|
48
|
+
}[severity];
|
|
49
|
+
const primaryButtonColor = {
|
|
50
|
+
info: "#3b82f6",
|
|
51
|
+
warning: "#eab308",
|
|
52
|
+
error: "#ef4444"
|
|
53
|
+
}[severity];
|
|
54
|
+
const primaryButtonHoverColor = {
|
|
55
|
+
info: "#2563eb",
|
|
56
|
+
warning: "#ca8a04",
|
|
57
|
+
error: "#dc2626"
|
|
58
|
+
}[severity];
|
|
59
|
+
return /* @__PURE__ */ jsx(
|
|
60
|
+
"div",
|
|
61
|
+
{
|
|
62
|
+
style: {
|
|
63
|
+
position: "fixed",
|
|
64
|
+
bottom: "16px",
|
|
65
|
+
left: "50%",
|
|
66
|
+
transform: "translateX(-50%)",
|
|
67
|
+
maxWidth: "90%",
|
|
68
|
+
zIndex: 9999
|
|
69
|
+
},
|
|
70
|
+
children: /* @__PURE__ */ jsxs(
|
|
71
|
+
"div",
|
|
72
|
+
{
|
|
73
|
+
style: {
|
|
74
|
+
display: "flex",
|
|
75
|
+
flexWrap: "wrap",
|
|
76
|
+
alignItems: "center",
|
|
77
|
+
gap: "12px",
|
|
78
|
+
borderRadius: "9999px",
|
|
79
|
+
border: "1px solid #e5e7eb",
|
|
80
|
+
backgroundColor: bgColor,
|
|
81
|
+
padding: "8px 16px",
|
|
82
|
+
boxShadow: "0 4px 6px -1px rgba(0, 0, 0, 0.1)"
|
|
83
|
+
},
|
|
84
|
+
children: [
|
|
85
|
+
/* @__PURE__ */ jsx("div", { style: { color: iconColor }, children: Icon }),
|
|
86
|
+
/* @__PURE__ */ jsx(
|
|
87
|
+
"span",
|
|
88
|
+
{
|
|
89
|
+
style: {
|
|
90
|
+
flex: 1,
|
|
91
|
+
fontSize: "14px",
|
|
92
|
+
fontWeight: 500,
|
|
93
|
+
color: textColor,
|
|
94
|
+
whiteSpace: "normal",
|
|
95
|
+
wordBreak: "break-word"
|
|
96
|
+
},
|
|
97
|
+
children: message
|
|
98
|
+
}
|
|
99
|
+
),
|
|
100
|
+
/* @__PURE__ */ jsxs(
|
|
101
|
+
"div",
|
|
102
|
+
{
|
|
103
|
+
style: {
|
|
104
|
+
display: "flex",
|
|
105
|
+
gap: "8px",
|
|
106
|
+
flexWrap: "wrap"
|
|
107
|
+
},
|
|
108
|
+
children: [
|
|
109
|
+
(actions == null ? void 0 : actions.secondary) && /* @__PURE__ */ jsx(
|
|
110
|
+
"button",
|
|
111
|
+
{
|
|
112
|
+
onClick: actions.secondary.onClick,
|
|
113
|
+
style: {
|
|
114
|
+
borderRadius: "9999px",
|
|
115
|
+
padding: "4px 12px",
|
|
116
|
+
fontSize: "14px",
|
|
117
|
+
fontWeight: 500,
|
|
118
|
+
color: textColor,
|
|
119
|
+
backgroundColor: "transparent",
|
|
120
|
+
border: "none",
|
|
121
|
+
cursor: "pointer",
|
|
122
|
+
transition: "background-color 0.2s"
|
|
123
|
+
},
|
|
124
|
+
onMouseOver: (e) => e.currentTarget.style.backgroundColor = "rgba(255,255,255,0.5)",
|
|
125
|
+
onMouseOut: (e) => e.currentTarget.style.backgroundColor = "transparent",
|
|
126
|
+
children: actions.secondary.label
|
|
127
|
+
}
|
|
128
|
+
),
|
|
129
|
+
(actions == null ? void 0 : actions.primary) && /* @__PURE__ */ jsx(
|
|
130
|
+
"button",
|
|
131
|
+
{
|
|
132
|
+
onClick: actions.primary.onClick,
|
|
133
|
+
style: {
|
|
134
|
+
borderRadius: "9999px",
|
|
135
|
+
padding: "4px 12px",
|
|
136
|
+
fontSize: "14px",
|
|
137
|
+
fontWeight: 500,
|
|
138
|
+
color: "#fff",
|
|
139
|
+
backgroundColor: primaryButtonColor,
|
|
140
|
+
border: "none",
|
|
141
|
+
cursor: "pointer",
|
|
142
|
+
transition: "background-color 0.2s"
|
|
143
|
+
},
|
|
144
|
+
onMouseOver: (e) => e.currentTarget.style.backgroundColor = primaryButtonHoverColor,
|
|
145
|
+
onMouseOut: (e) => e.currentTarget.style.backgroundColor = primaryButtonColor,
|
|
146
|
+
children: actions.primary.label
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
function renderCopilotKitUsage(error) {
|
|
159
|
+
switch (error.name) {
|
|
160
|
+
case ERROR_NAMES.CONFIGURATION_ERROR:
|
|
161
|
+
return /* @__PURE__ */ jsx(UsageBanner, { severity: error.severity, message: error.message });
|
|
162
|
+
case ERROR_NAMES.MISSING_PUBLIC_API_KEY_ERROR:
|
|
163
|
+
return /* @__PURE__ */ jsx(
|
|
164
|
+
UsageBanner,
|
|
165
|
+
{
|
|
166
|
+
severity: error.severity,
|
|
167
|
+
message: error.message,
|
|
168
|
+
actions: {
|
|
169
|
+
primary: {
|
|
170
|
+
label: "Sign In",
|
|
171
|
+
onClick: () => {
|
|
172
|
+
window.location.href = "https://cloud.copilotkit.ai";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
case ERROR_NAMES.UPGRADE_REQUIRED_ERROR:
|
|
179
|
+
return /* @__PURE__ */ jsx(
|
|
180
|
+
UsageBanner,
|
|
181
|
+
{
|
|
182
|
+
severity: error.severity,
|
|
183
|
+
message: error.message,
|
|
184
|
+
actions: {
|
|
185
|
+
primary: {
|
|
186
|
+
label: "Upgrade",
|
|
187
|
+
onClick: () => {
|
|
188
|
+
window.location.href = "https://copilotkit.ai/";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export {
|
|
198
|
+
UsageBanner,
|
|
199
|
+
renderCopilotKitUsage
|
|
200
|
+
};
|
|
201
|
+
//# sourceMappingURL=chunk-D34OH4VN.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/usage-banner.tsx"],"sourcesContent":["import { Severity, CopilotKitError, ERROR_NAMES } from \"@copilotkit/shared\";\n\ninterface UsageBannerProps {\n severity?: Severity;\n message?: string;\n icon?: React.ReactNode;\n actions?: {\n primary?: {\n label: string;\n onClick: () => void;\n };\n secondary?: {\n label: string;\n onClick: () => void;\n };\n };\n}\n\nconst defaultIcons: Record<Severity, JSX.Element> = {\n [Severity.Error]: (\n <svg\n viewBox=\"0 0 24 24\"\n width=\"20\"\n height=\"20\"\n stroke=\"currentColor\"\n strokeWidth=\"2\"\n fill=\"none\"\n strokeLinecap=\"round\"\n strokeLinejoin=\"round\"\n >\n <circle cx=\"12\" cy=\"12\" r=\"10\" />\n <line x1=\"15\" y1=\"9\" x2=\"9\" y2=\"15\" />\n <line x1=\"9\" y1=\"9\" x2=\"15\" y2=\"15\" />\n </svg>\n ),\n};\n\nexport function UsageBanner({\n severity = Severity.Error,\n message = \"\",\n icon,\n actions,\n}: UsageBannerProps) {\n if (!message || !severity) {\n return null;\n }\n\n const Icon = icon || defaultIcons[severity];\n\n const bgColor = {\n info: \"#dbeafe\",\n warning: \"#fef3c7\",\n error: \"#fee2e2\",\n }[severity];\n\n const textColor = {\n info: \"#1e40af\",\n warning: \"#854d0e\",\n error: \"#991b1b\",\n }[severity];\n\n const iconColor = {\n info: \"#3b82f6\",\n warning: \"#eab308\",\n error: \"#ef4444\",\n }[severity];\n\n const primaryButtonColor = {\n info: \"#3b82f6\",\n warning: \"#eab308\",\n error: \"#ef4444\",\n }[severity];\n\n const primaryButtonHoverColor = {\n info: \"#2563eb\",\n warning: \"#ca8a04\",\n error: \"#dc2626\",\n }[severity];\n\n return (\n <div\n style={{\n position: \"fixed\",\n bottom: \"16px\",\n left: \"50%\",\n transform: \"translateX(-50%)\",\n maxWidth: \"90%\",\n zIndex: 9999,\n }}\n >\n <div\n style={{\n display: \"flex\",\n flexWrap: \"wrap\",\n alignItems: \"center\",\n gap: \"12px\",\n borderRadius: \"9999px\",\n border: \"1px solid #e5e7eb\",\n backgroundColor: bgColor,\n padding: \"8px 16px\",\n boxShadow: \"0 4px 6px -1px rgba(0, 0, 0, 0.1)\",\n }}\n >\n <div style={{ color: iconColor }}>{Icon}</div>\n <span\n style={{\n flex: 1,\n fontSize: \"14px\",\n fontWeight: 500,\n color: textColor,\n whiteSpace: \"normal\",\n wordBreak: \"break-word\",\n }}\n >\n {message}\n </span>\n <div\n style={{\n display: \"flex\",\n gap: \"8px\",\n flexWrap: \"wrap\",\n }}\n >\n {actions?.secondary && (\n <button\n onClick={actions.secondary.onClick}\n style={{\n borderRadius: \"9999px\",\n padding: \"4px 12px\",\n fontSize: \"14px\",\n fontWeight: 500,\n color: textColor,\n backgroundColor: \"transparent\",\n border: \"none\",\n cursor: \"pointer\",\n transition: \"background-color 0.2s\",\n }}\n onMouseOver={(e) => (e.currentTarget.style.backgroundColor = \"rgba(255,255,255,0.5)\")}\n onMouseOut={(e) => (e.currentTarget.style.backgroundColor = \"transparent\")}\n >\n {actions.secondary.label}\n </button>\n )}\n {actions?.primary && (\n <button\n onClick={actions.primary.onClick}\n style={{\n borderRadius: \"9999px\",\n padding: \"4px 12px\",\n fontSize: \"14px\",\n fontWeight: 500,\n color: \"#fff\",\n backgroundColor: primaryButtonColor,\n border: \"none\",\n cursor: \"pointer\",\n transition: \"background-color 0.2s\",\n }}\n onMouseOver={(e) => (e.currentTarget.style.backgroundColor = primaryButtonHoverColor)}\n onMouseOut={(e) => (e.currentTarget.style.backgroundColor = primaryButtonColor)}\n >\n {actions.primary.label}\n </button>\n )}\n </div>\n </div>\n </div>\n );\n}\n\nexport function renderCopilotKitUsage(error: CopilotKitError) {\n switch (error.name) {\n case ERROR_NAMES.CONFIGURATION_ERROR:\n return <UsageBanner severity={error.severity} message={error.message} />;\n case ERROR_NAMES.MISSING_PUBLIC_API_KEY_ERROR:\n return (\n <UsageBanner\n severity={error.severity}\n message={error.message}\n actions={{\n primary: {\n label: \"Sign In\",\n onClick: () => {\n window.location.href = \"https://cloud.copilotkit.ai\";\n },\n },\n }}\n />\n );\n case ERROR_NAMES.UPGRADE_REQUIRED_ERROR:\n return (\n <UsageBanner\n severity={error.severity}\n message={error.message}\n actions={{\n primary: {\n label: \"Upgrade\",\n onClick: () => {\n window.location.href = \"https://copilotkit.ai/\";\n },\n },\n }}\n />\n );\n }\n}\n"],"mappings":";AAAA,SAAS,UAA2B,mBAAmB;AAoBnD,SAUE,KAVF;AAFJ,IAAM,eAA8C;AAAA,EAClD,CAAC,SAAS,KAAK,GACb;AAAA,IAAC;AAAA;AAAA,MACC,SAAQ;AAAA,MACR,OAAM;AAAA,MACN,QAAO;AAAA,MACP,QAAO;AAAA,MACP,aAAY;AAAA,MACZ,MAAK;AAAA,MACL,eAAc;AAAA,MACd,gBAAe;AAAA,MAEf;AAAA,4BAAC,YAAO,IAAG,MAAK,IAAG,MAAK,GAAE,MAAK;AAAA,QAC/B,oBAAC,UAAK,IAAG,MAAK,IAAG,KAAI,IAAG,KAAI,IAAG,MAAK;AAAA,QACpC,oBAAC,UAAK,IAAG,KAAI,IAAG,KAAI,IAAG,MAAK,IAAG,MAAK;AAAA;AAAA;AAAA,EACtC;AAEJ;AAEO,SAAS,YAAY;AAAA,EAC1B,WAAW,SAAS;AAAA,EACpB,UAAU;AAAA,EACV;AAAA,EACA;AACF,GAAqB;AACnB,MAAI,CAAC,WAAW,CAAC,UAAU;AACzB,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,QAAQ,aAAa,QAAQ;AAE1C,QAAM,UAAU;AAAA,IACd,MAAM;AAAA,IACN,SAAS;AAAA,IACT,OAAO;AAAA,EACT,EAAE,QAAQ;AAEV,QAAM,YAAY;A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ACF;AAAA,IAEJ,KAAK,YAAY;AACf,aACE;AAAA,QAAC;AAAA;AAAA,UACC,UAAU,MAAM;AAAA,UAChB,SAAS,MAAM;AAAA,UACf,SAAS;AAAA,YACP,SAAS;AAAA,cACP,OAAO;AAAA,cACP,SAAS,MAAM;AACb,uBAAO,SAAS,OAAO;AAAA,cACzB;AAAA,YACF;AAAA,UACF;AAAA;AAAA,MACF;AAAA,EAEN;AACF;","names":[]}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
defaultCopilotContextCategories
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-IU7OLWWW.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useCopilotContext
|
|
6
|
+
} from "./chunk-ZM56S3LJ.mjs";
|
|
4
7
|
import {
|
|
5
8
|
useCopilotMessagesContext
|
|
6
9
|
} from "./chunk-DCTJZ742.mjs";
|
|
7
10
|
import {
|
|
8
11
|
useChat
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import {
|
|
11
|
-
useCopilotContext
|
|
12
|
-
} from "./chunk-WXREY7UP.mjs";
|
|
12
|
+
} from "./chunk-FMROB4TE.mjs";
|
|
13
13
|
import {
|
|
14
14
|
useAsyncCallback
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-HZCROITJ.mjs";
|
|
16
16
|
import {
|
|
17
17
|
__async,
|
|
18
18
|
__objRest,
|
|
@@ -48,7 +48,11 @@ function useCopilotChat(_a = {}) {
|
|
|
48
48
|
setThreadId,
|
|
49
49
|
runId,
|
|
50
50
|
setRunId,
|
|
51
|
-
chatAbortControllerRef
|
|
51
|
+
chatAbortControllerRef,
|
|
52
|
+
extensions,
|
|
53
|
+
setExtensions,
|
|
54
|
+
langGraphInterruptAction,
|
|
55
|
+
setLangGraphInterruptAction
|
|
52
56
|
} = useCopilotContext();
|
|
53
57
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
54
58
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
@@ -105,7 +109,11 @@ function useCopilotChat(_a = {}) {
|
|
|
105
109
|
runId,
|
|
106
110
|
setRunId,
|
|
107
111
|
chatAbortControllerRef,
|
|
108
|
-
agentLock
|
|
112
|
+
agentLock,
|
|
113
|
+
extensions,
|
|
114
|
+
setExtensions,
|
|
115
|
+
langGraphInterruptAction,
|
|
116
|
+
setLangGraphInterruptAction
|
|
109
117
|
}));
|
|
110
118
|
const latestAppend = useUpdatedRef(append);
|
|
111
119
|
const latestAppendFunc = useAsyncCallback(
|
|
@@ -143,7 +151,6 @@ function useCopilotChat(_a = {}) {
|
|
|
143
151
|
const reset = useCallback(() => {
|
|
144
152
|
latestStopFunc();
|
|
145
153
|
setMessages([]);
|
|
146
|
-
setThreadId(null);
|
|
147
154
|
setRunId(null);
|
|
148
155
|
setCoagentStatesWithRef({});
|
|
149
156
|
let initialAgentSession = null;
|
|
@@ -203,6 +210,9 @@ Please assist them as best you can.
|
|
|
203
210
|
You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
|
|
204
211
|
|
|
205
212
|
If you would like to call a function, call it without saying anything else.
|
|
213
|
+
In case of a function error:
|
|
214
|
+
- If this error stems from incorrect function parameters or syntax, you may retry with corrected arguments.
|
|
215
|
+
- If the error's source is unclear or seems unrelated to your input, do not attempt further retries.
|
|
206
216
|
` + (additionalInstructions ? `
|
|
207
217
|
|
|
208
218
|
${additionalInstructions}` : "");
|
|
@@ -212,4 +222,4 @@ export {
|
|
|
212
222
|
useCopilotChat,
|
|
213
223
|
defaultSystemMessage
|
|
214
224
|
};
|
|
215
|
-
//# sourceMappingURL=chunk-
|
|
225
|
+
//# sourceMappingURL=chunk-DEPX7JPX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"sourcesContent":["/**\n * `useCopilotChat` is a React hook that lets you directly interact with the\n * Copilot instance. Use to implement a fully custom UI (headless UI) or to\n * programmatically interact with the Copilot instance managed by the default\n * UI.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChat } from \"@copilotkit/react-core\";\n * import { Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\n *\n * export function YourComponent() {\n * const { appendMessage } = useCopilotChat();\n *\n * appendMessage(\n * new TextMessage({\n * content: \"Hello World\",\n * role: Role.User,\n * }),\n * );\n *\n * // optionally, you can append a message without running chat completion\n * appendMessage(yourMessage, { followUp: false });\n * }\n * ```\n *\n * `useCopilotChat` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * visibleMessages, // An array of messages that are currently visible in the chat.\n * appendMessage, // A function to append a message to the chat.\n * setMessages, // A function to set the messages in the chat.\n * deleteMessage, // A function to delete a message from the chat.\n * reloadMessages, // A function to reload the messages from the API.\n * stopGeneration, // A function to stop the generation of the next message.\n * isLoading, // A boolean indicating if the chat is loading.\n * } = useCopilotChat();\n * ```\n */\nimport { useRef, useEffect, useCallback } from \"react\";\nimport { AgentSession, useCopilotContext } from \"../context/copilot-context\";\nimport { Message, Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\nimport { SystemMessageFunction } from \"../types\";\nimport { useChat, AppendMessageOptions } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { CoAgentStateRenderHandlerArguments, randomId } from \"@copilotkit/shared\";\nimport { useCopilotMessagesContext } from \"../context\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\n\nexport interface UseCopilotChatOptions {\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n\n /**\n * A function to generate the system message. Defaults to `defaultSystemMessage`.\n */\n makeSystemMessage?: SystemMessageFunction;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n appendMessage: (message: Message, options?: AppendMessageOptions) => Promise<void>;\n setMessages: (messages: Message[]) => void;\n deleteMessage: (messageId: string) => void;\n reloadMessages: () => Promise<void>;\n stopGeneration: () => void;\n reset: () => void;\n isLoading: boolean;\n runChatCompletion: () => Promise<Message[]>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions = {}): UseCopilotChatReturn {\n const {\n getContextString,\n getFunctionCallHandler,\n copilotApiConfig,\n isLoading,\n setIsLoading,\n chatInstructions,\n actions,\n coagentStatesRef,\n setCoagentStatesWithRef,\n coAgentStateRenders,\n agentSession,\n setAgentSession,\n forwardedParameters,\n agentLock,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n extensions,\n setExtensions,\n langGraphInterruptAction,\n setLangGraphInterruptAction,\n } = useCopilotContext();\n const { messages, setMessages } = useCopilotMessagesContext();\n\n // We need to ensure that makeSystemMessageCallback always uses the latest\n // useCopilotReadable data.\n const latestGetContextString = useUpdatedRef(getContextString);\n const deleteMessage = useCallback(\n (messageId: string) => {\n setMessages((prev) => prev.filter((message) => message.id !== messageId));\n },\n [setMessages],\n );\n\n const makeSystemMessageCallback = useCallback(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n // this always gets the latest context string\n const contextString = latestGetContextString.current([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return new TextMessage({\n content: systemMessageMaker(contextString, chatInstructions),\n role: Role.System,\n });\n }, [getContextString, makeSystemMessage, chatInstructions]);\n\n const onCoAgentStateRender = useAsyncCallback(\n async (args: CoAgentStateRenderHandlerArguments) => {\n const { name, nodeName, state } = args;\n let action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && action.nodeName === nodeName,\n );\n if (!action) {\n action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && !action.nodeName,\n );\n }\n if (action) {\n await action.handler?.({ state, nodeName });\n }\n },\n [coAgentStateRenders],\n );\n\n const { append, reload, stop, runChatCompletion } = useChat({\n ...options,\n actions: Object.values(actions),\n copilotConfig: copilotApiConfig,\n initialMessages: options.initialMessages || [],\n onFunctionCall: getFunctionCallHandler(),\n onCoAgentStateRender,\n messages,\n setMessages,\n makeSystemMessageCallback,\n isLoading,\n setIsLoading,\n coagentStatesRef,\n setCoagentStatesWithRef,\n agentSession,\n setAgentSession,\n forwardedParameters,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n agentLock,\n extensions,\n setExtensions,\n langGraphInterruptAction,\n setLangGraphInterruptAction,\n });\n\n // this is a workaround born out of a bug that Athena incessantly ran into.\n // We could not find the origin of the bug, however, it was clear that an outdated version of the append function was being used somehow --\n // it referenced the old state of the messages array, and not the latest one.\n //\n // We want to make copilotkit as abuse-proof as possible, so we are adding this workaround to ensure that the latest version of the append function is always used.\n //\n // How does this work?\n // we store the relevant function in a ref that is always up-to-date, and then we use that ref in the callback.\n const latestAppend = useUpdatedRef(append);\n const latestAppendFunc = useAsyncCallback(\n async (message: Message, options?: AppendMessageOptions) => {\n return await latestAppend.current(message, options);\n },\n [latestAppend],\n );\n\n const latestReload = useUpdatedRef(reload);\n const latestReloadFunc = useAsyncCallback(async () => {\n return await latestReload.current();\n }, [latestReload]);\n\n const latestStop = useUpdatedRef(stop);\n const latestStopFunc = useCallback(() => {\n return latestStop.current();\n }, [latestStop]);\n\n const latestDelete = useUpdatedRef(deleteMessage);\n const latestDeleteFunc = useCallback(\n (messageId: string) => {\n return latestDelete.current(messageId);\n },\n [latestDelete],\n );\n\n const latestSetMessages = useUpdatedRef(setMessages);\n const latestSetMessagesFunc = useCallback(\n (messages: Message[]) => {\n return latestSetMessages.current(messages);\n },\n [latestSetMessages],\n );\n\n const latestRunChatCompletion = useUpdatedRef(runChatCompletion);\n const latestRunChatCompletionFunc = useAsyncCallback(async () => {\n return await latestRunChatCompletion.current!();\n }, [latestRunChatCompletion]);\n\n const reset = useCallback(() => {\n latestStopFunc();\n setMessages([]);\n setRunId(null);\n setCoagentStatesWithRef({});\n let initialAgentSession: AgentSession | null = null;\n if (agentLock) {\n initialAgentSession = {\n agentName: agentLock,\n };\n }\n setAgentSession(initialAgentSession);\n }, [\n latestStopFunc,\n setMessages,\n setThreadId,\n setCoagentStatesWithRef,\n setAgentSession,\n agentLock,\n ]);\n\n const latestReset = useUpdatedRef(reset);\n const latestResetFunc = useCallback(() => {\n return latestReset.current();\n }, [latestReset]);\n\n return {\n visibleMessages: messages,\n appendMessage: latestAppendFunc,\n setMessages: latestSetMessagesFunc,\n reloadMessages: latestReloadFunc,\n stopGeneration: latestStopFunc,\n reset: latestResetFunc,\n deleteMessage: latestDeleteFunc,\n runChatCompletion: latestRunChatCompletionFunc,\n isLoading,\n };\n}\n\n// store `value` in a ref and update\n// it whenever it changes.\nfunction useUpdatedRef<T>(value: T) {\n const ref = useRef(value);\n\n useEffect(() => {\n ref.current = value;\n }, [value]);\n\n return ref;\n}\n\nexport function defaultSystemMessage(\n contextString: string,\n additionalInstructions?: string,\n): string {\n return (\n `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\nIn case of a function error:\n- If this error stems from incorrect function parameters or syntax, you may retry with corrected arguments.\n- If the error's source is unclear or seems unrelated to your input, do not attempt further retries.\n` + (additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\")\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAS,QAAQ,WAAW,mBAAmB;AAE/C,SAAkB,MAAM,mBAA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