@copilotkit/react-core 0.0.0-feat-dynamic-copilotcloud-qa-20250117190454
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 +2001 -0
- package/README.md +48 -0
- package/dist/chunk-22XKTBPF.mjs +61 -0
- package/dist/chunk-22XKTBPF.mjs.map +1 -0
- package/dist/chunk-2KT73RVG.mjs +482 -0
- package/dist/chunk-2KT73RVG.mjs.map +1 -0
- package/dist/chunk-4TDYSNUD.mjs +215 -0
- package/dist/chunk-4TDYSNUD.mjs.map +1 -0
- package/dist/chunk-5FHSUKQL.mjs +70 -0
- package/dist/chunk-5FHSUKQL.mjs.map +1 -0
- package/dist/chunk-6BQVZFNO.mjs +35 -0
- package/dist/chunk-6BQVZFNO.mjs.map +1 -0
- package/dist/chunk-6U3UH3KO.mjs +140 -0
- package/dist/chunk-6U3UH3KO.mjs.map +1 -0
- package/dist/chunk-DCTJZ742.mjs +22 -0
- package/dist/chunk-DCTJZ742.mjs.map +1 -0
- package/dist/chunk-EFZPSZWO.mjs +1 -0
- package/dist/chunk-EFZPSZWO.mjs.map +1 -0
- package/dist/chunk-EWKJMQOF.mjs +107 -0
- package/dist/chunk-EWKJMQOF.mjs.map +1 -0
- package/dist/chunk-FQ53HQYZ.mjs +28 -0
- package/dist/chunk-FQ53HQYZ.mjs.map +1 -0
- package/dist/chunk-HIUT2NLX.mjs +23 -0
- package/dist/chunk-HIUT2NLX.mjs.map +1 -0
- package/dist/chunk-KSPLXKSX.mjs +160 -0
- package/dist/chunk-KSPLXKSX.mjs.map +1 -0
- package/dist/chunk-LHKIIAUQ.mjs +59 -0
- package/dist/chunk-LHKIIAUQ.mjs.map +1 -0
- package/dist/chunk-LNI2Z45S.mjs +242 -0
- package/dist/chunk-LNI2Z45S.mjs.map +1 -0
- package/dist/chunk-LUGEI4YQ.mjs +1 -0
- package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
- package/dist/chunk-MLAS4QUR.mjs +18 -0
- package/dist/chunk-MLAS4QUR.mjs.map +1 -0
- package/dist/chunk-NR2BTA6Z.mjs +1 -0
- package/dist/chunk-NR2BTA6Z.mjs.map +1 -0
- package/dist/chunk-O7ARI5CV.mjs +31 -0
- package/dist/chunk-O7ARI5CV.mjs.map +1 -0
- package/dist/chunk-SKC7AJIV.mjs +61 -0
- package/dist/chunk-SKC7AJIV.mjs.map +1 -0
- package/dist/chunk-U6JICUGO.mjs +405 -0
- package/dist/chunk-U6JICUGO.mjs.map +1 -0
- package/dist/chunk-VNTDIJSG.mjs +37 -0
- package/dist/chunk-VNTDIJSG.mjs.map +1 -0
- package/dist/chunk-WXREY7UP.mjs +87 -0
- package/dist/chunk-WXREY7UP.mjs.map +1 -0
- package/dist/chunk-XERJQUHA.mjs +31 -0
- package/dist/chunk-XERJQUHA.mjs.map +1 -0
- package/dist/chunk-XOQMISG3.mjs +120 -0
- package/dist/chunk-XOQMISG3.mjs.map +1 -0
- package/dist/chunk-XXR4QFAQ.mjs +29 -0
- package/dist/chunk-XXR4QFAQ.mjs.map +1 -0
- package/dist/chunk-YPSGKPDA.mjs +1 -0
- package/dist/chunk-YPSGKPDA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +16 -0
- package/dist/components/copilot-provider/copilot-messages.js +80 -0
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.mjs +9 -0
- package/dist/components/copilot-provider/copilot-messages.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit-props.d.ts +93 -0
- package/dist/components/copilot-provider/copilotkit-props.js +19 -0
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -0
- package/dist/components/copilot-provider/copilotkit-props.mjs +1 -0
- package/dist/components/copilot-provider/copilotkit-props.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.d.ts +18 -0
- package/dist/components/copilot-provider/copilotkit.js +990 -0
- package/dist/components/copilot-provider/copilotkit.js.map +1 -0
- package/dist/components/copilot-provider/copilotkit.mjs +22 -0
- package/dist/components/copilot-provider/copilotkit.mjs.map +1 -0
- package/dist/components/copilot-provider/index.d.ts +13 -0
- package/dist/components/copilot-provider/index.js +990 -0
- package/dist/components/copilot-provider/index.js.map +1 -0
- package/dist/components/copilot-provider/index.mjs +20 -0
- package/dist/components/copilot-provider/index.mjs.map +1 -0
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.d.ts +13 -0
- package/dist/components/index.js +990 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +20 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/components/toast/exclamation-mark-icon.d.ts +9 -0
- package/dist/components/toast/exclamation-mark-icon.js +55 -0
- package/dist/components/toast/exclamation-mark-icon.js.map +1 -0
- package/dist/components/toast/exclamation-mark-icon.mjs +8 -0
- package/dist/components/toast/exclamation-mark-icon.mjs.map +1 -0
- package/dist/components/toast/toast-provider.d.ts +30 -0
- package/dist/components/toast/toast-provider.js +269 -0
- package/dist/components/toast/toast-provider.js.map +1 -0
- package/dist/components/toast/toast-provider.mjs +11 -0
- package/dist/components/toast/toast-provider.mjs.map +1 -0
- package/dist/context/copilot-context.d.ts +142 -0
- package/dist/context/copilot-context.js +138 -0
- package/dist/context/copilot-context.js.map +1 -0
- package/dist/context/copilot-context.mjs +10 -0
- package/dist/context/copilot-context.mjs.map +1 -0
- package/dist/context/copilot-messages-context.d.ts +15 -0
- package/dist/context/copilot-messages-context.js +57 -0
- package/dist/context/copilot-messages-context.js.map +1 -0
- package/dist/context/copilot-messages-context.mjs +10 -0
- package/dist/context/copilot-messages-context.mjs.map +1 -0
- package/dist/context/index.d.ts +11 -0
- package/dist/context/index.js +161 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +17 -0
- package/dist/context/index.mjs.map +1 -0
- package/dist/hooks/index.d.ts +21 -0
- package/dist/hooks/index.js +1307 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +56 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/hooks/use-chat.d.ts +130 -0
- package/dist/hooks/use-chat.js +612 -0
- package/dist/hooks/use-chat.js.map +1 -0
- package/dist/hooks/use-chat.mjs +12 -0
- package/dist/hooks/use-chat.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render.d.ts +60 -0
- package/dist/hooks/use-coagent-state-render.js +195 -0
- package/dist/hooks/use-coagent-state-render.js.map +1 -0
- package/dist/hooks/use-coagent-state-render.mjs +11 -0
- package/dist/hooks/use-coagent-state-render.mjs.map +1 -0
- package/dist/hooks/use-coagent.d.ts +199 -0
- package/dist/hooks/use-coagent.js +1068 -0
- package/dist/hooks/use-coagent.js.map +1 -0
- package/dist/hooks/use-coagent.mjs +29 -0
- package/dist/hooks/use-coagent.mjs.map +1 -0
- package/dist/hooks/use-copilot-action.d.ts +140 -0
- package/dist/hooks/use-copilot-action.js +369 -0
- package/dist/hooks/use-copilot-action.js.map +1 -0
- package/dist/hooks/use-copilot-action.mjs +11 -0
- package/dist/hooks/use-copilot-action.mjs.map +1 -0
- package/dist/hooks/use-copilot-authenticated-action.d.ts +8 -0
- package/dist/hooks/use-copilot-authenticated-action.js +418 -0
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -0
- package/dist/hooks/use-copilot-authenticated-action.mjs +12 -0
- package/dist/hooks/use-copilot-authenticated-action.mjs.map +1 -0
- package/dist/hooks/use-copilot-chat.d.ts +48 -0
- package/dist/hooks/use-copilot-chat.js +932 -0
- package/dist/hooks/use-copilot-chat.js.map +1 -0
- package/dist/hooks/use-copilot-chat.mjs +24 -0
- package/dist/hooks/use-copilot-chat.mjs.map +1 -0
- package/dist/hooks/use-copilot-readable.d.ts +33 -0
- package/dist/hooks/use-copilot-readable.js +158 -0
- package/dist/hooks/use-copilot-readable.js.map +1 -0
- package/dist/hooks/use-copilot-readable.mjs +9 -0
- package/dist/hooks/use-copilot-readable.mjs.map +1 -0
- package/dist/hooks/use-copilot-runtime-client.d.ts +5 -0
- package/dist/hooks/use-copilot-runtime-client.js +182 -0
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -0
- package/dist/hooks/use-copilot-runtime-client.mjs +10 -0
- package/dist/hooks/use-copilot-runtime-client.mjs.map +1 -0
- package/dist/hooks/use-flat-category-store.d.ts +9 -0
- package/dist/hooks/use-flat-category-store.js +90 -0
- package/dist/hooks/use-flat-category-store.js.map +1 -0
- package/dist/hooks/use-flat-category-store.mjs +8 -0
- package/dist/hooks/use-flat-category-store.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
- package/dist/hooks/use-make-copilot-document-readable.js +153 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs +9 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
- package/dist/hooks/use-tree.d.ts +18 -0
- package/dist/hooks/use-tree.js +172 -0
- package/dist/hooks/use-tree.js.map +1 -0
- package/dist/hooks/use-tree.mjs +8 -0
- package/dist/hooks/use-tree.mjs.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +2228 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +83 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lib/copilot-task.d.ts +97 -0
- package/dist/lib/copilot-task.js +195 -0
- package/dist/lib/copilot-task.js.map +1 -0
- package/dist/lib/copilot-task.mjs +20 -0
- package/dist/lib/copilot-task.mjs.map +1 -0
- package/dist/lib/index.d.ts +11 -0
- package/dist/lib/index.js +197 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/index.mjs +21 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/types/chat-suggestion-configuration.d.ts +22 -0
- package/dist/types/chat-suggestion-configuration.js +19 -0
- package/dist/types/chat-suggestion-configuration.js.map +1 -0
- package/dist/types/chat-suggestion-configuration.mjs +1 -0
- package/dist/types/chat-suggestion-configuration.mjs.map +1 -0
- package/dist/types/coagent-action.d.ts +29 -0
- package/dist/types/coagent-action.js +19 -0
- package/dist/types/coagent-action.js.map +1 -0
- package/dist/types/coagent-action.mjs +1 -0
- package/dist/types/coagent-action.mjs.map +1 -0
- package/dist/types/coagent-state.d.ts +11 -0
- package/dist/types/coagent-state.js +19 -0
- package/dist/types/coagent-state.js.map +1 -0
- package/dist/types/coagent-state.mjs +1 -0
- package/dist/types/coagent-state.mjs.map +1 -0
- package/dist/types/document-pointer.d.ts +9 -0
- package/dist/types/document-pointer.js +19 -0
- package/dist/types/document-pointer.js.map +1 -0
- package/dist/types/document-pointer.mjs +1 -0
- package/dist/types/document-pointer.mjs.map +1 -0
- package/dist/types/frontend-action.d.ts +126 -0
- package/dist/types/frontend-action.js +53 -0
- package/dist/types/frontend-action.js.map +1 -0
- package/dist/types/frontend-action.mjs +8 -0
- package/dist/types/frontend-action.mjs.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.js +19 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +2 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/system-message.d.ts +3 -0
- package/dist/types/system-message.js +19 -0
- package/dist/types/system-message.js.map +1 -0
- package/dist/types/system-message.mjs +1 -0
- package/dist/types/system-message.mjs.map +1 -0
- package/dist/utils/dev-console.d.ts +3 -0
- package/dist/utils/dev-console.js +42 -0
- package/dist/utils/dev-console.js.map +1 -0
- package/dist/utils/dev-console.mjs +8 -0
- package/dist/utils/dev-console.mjs.map +1 -0
- package/dist/utils/extract.d.ts +43 -0
- package/dist/utils/extract.js +208 -0
- package/dist/utils/extract.js.map +1 -0
- package/dist/utils/extract.mjs +18 -0
- package/dist/utils/extract.mjs.map +1 -0
- package/dist/utils/index.d.ts +13 -0
- package/dist/utils/index.js +226 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +21 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/utils.d.ts +2 -0
- package/dist/utils/utils.js +19 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist/utils/utils.mjs +1 -0
- package/dist/utils/utils.mjs.map +1 -0
- package/dist/utils/utils.test.d.ts +2 -0
- package/dist/utils/utils.test.js +9 -0
- package/dist/utils/utils.test.js.map +1 -0
- package/dist/utils/utils.test.mjs +7 -0
- package/dist/utils/utils.test.mjs.map +1 -0
- package/jest.config.js +5 -0
- package/package.json +69 -0
- package/src/components/copilot-provider/copilot-messages.tsx +23 -0
- package/src/components/copilot-provider/copilotkit-props.tsx +97 -0
- package/src/components/copilot-provider/copilotkit.tsx +401 -0
- package/src/components/copilot-provider/index.ts +3 -0
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/index.ts +1 -0
- package/src/components/toast/exclamation-mark-icon.tsx +27 -0
- package/src/components/toast/toast-provider.tsx +170 -0
- package/src/context/copilot-context.tsx +271 -0
- package/src/context/copilot-messages-context.tsx +29 -0
- package/src/context/index.ts +8 -0
- package/src/hooks/index.ts +12 -0
- package/src/hooks/use-chat.ts +649 -0
- package/src/hooks/use-coagent-state-render.ts +118 -0
- package/src/hooks/use-coagent.ts +368 -0
- package/src/hooks/use-copilot-action.ts +289 -0
- package/src/hooks/use-copilot-authenticated-action.ts +60 -0
- package/src/hooks/use-copilot-chat.ts +304 -0
- package/src/hooks/use-copilot-readable.ts +123 -0
- package/src/hooks/use-copilot-runtime-client.ts +28 -0
- package/src/hooks/use-flat-category-store.ts +104 -0
- package/src/hooks/use-make-copilot-document-readable.ts +30 -0
- package/src/hooks/use-tree.ts +202 -0
- package/src/index.tsx +7 -0
- package/src/lib/copilot-task.ts +201 -0
- package/src/lib/index.ts +1 -0
- package/src/types/chat-suggestion-configuration.ts +23 -0
- package/src/types/coagent-action.ts +31 -0
- package/src/types/coagent-state.ts +9 -0
- package/src/types/document-pointer.ts +7 -0
- package/src/types/frontend-action.ts +199 -0
- package/src/types/index.ts +8 -0
- package/src/types/system-message.ts +4 -0
- package/src/utils/dev-console.ts +18 -0
- package/src/utils/extract.ts +215 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/utils.test.ts +7 -0
- package/src/utils/utils.ts +1 -0
- package/tsconfig.json +12 -0
- package/tsup.config.ts +16 -0
- package/typedoc.json +4 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example usage of useCopilotAction with complex parameters:
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* useCopilotAction({
|
|
6
|
+
* name: "myAction",
|
|
7
|
+
* parameters: [
|
|
8
|
+
* { name: "arg1", type: "string", enum: ["option1", "option2", "option3"], required: false },
|
|
9
|
+
* { name: "arg2", type: "number" },
|
|
10
|
+
* {
|
|
11
|
+
* name: "arg3",
|
|
12
|
+
* type: "object",
|
|
13
|
+
* attributes: [
|
|
14
|
+
* { name: "nestedArg1", type: "boolean" },
|
|
15
|
+
* { name: "xyz", required: false },
|
|
16
|
+
* ],
|
|
17
|
+
* },
|
|
18
|
+
* { name: "arg4", type: "number[]" },
|
|
19
|
+
* ],
|
|
20
|
+
* handler: ({ arg1, arg2, arg3, arg4 }) => {
|
|
21
|
+
* const x = arg3.nestedArg1;
|
|
22
|
+
* const z = arg3.xyz;
|
|
23
|
+
* console.log(arg1, arg2, arg3);
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Simple action without parameters
|
|
29
|
+
* useCopilotAction({
|
|
30
|
+
* name: "myAction",
|
|
31
|
+
* handler: () => {
|
|
32
|
+
* console.log("No parameters provided.");
|
|
33
|
+
* },
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Interactive action with UI rendering and response handling
|
|
38
|
+
* useCopilotAction({
|
|
39
|
+
* name: "handleMeeting",
|
|
40
|
+
* description: "Handle a meeting by booking or canceling",
|
|
41
|
+
* parameters: [
|
|
42
|
+
* {
|
|
43
|
+
* name: "meeting",
|
|
44
|
+
* type: "string",
|
|
45
|
+
* description: "The meeting to handle",
|
|
46
|
+
* required: true,
|
|
47
|
+
* },
|
|
48
|
+
* {
|
|
49
|
+
* name: "date",
|
|
50
|
+
* type: "string",
|
|
51
|
+
* description: "The date of the meeting",
|
|
52
|
+
* required: true,
|
|
53
|
+
* },
|
|
54
|
+
* {
|
|
55
|
+
* name: "title",
|
|
56
|
+
* type: "string",
|
|
57
|
+
* description: "The title of the meeting",
|
|
58
|
+
* required: true,
|
|
59
|
+
* },
|
|
60
|
+
* ],
|
|
61
|
+
* renderAndWaitForResponse: ({ args, respond, status }) => {
|
|
62
|
+
* const { meeting, date, title } = args;
|
|
63
|
+
* return (
|
|
64
|
+
* <MeetingConfirmationDialog
|
|
65
|
+
* meeting={meeting}
|
|
66
|
+
* date={date}
|
|
67
|
+
* title={title}
|
|
68
|
+
* onConfirm={() => respond('meeting confirmed')}
|
|
69
|
+
* onCancel={() => respond('meeting canceled')}
|
|
70
|
+
* />
|
|
71
|
+
* );
|
|
72
|
+
* },
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* // Catch all action allows you to render actions that are not defined in the frontend
|
|
77
|
+
* useCopilotAction({
|
|
78
|
+
* name: "*",
|
|
79
|
+
* render: ({ name, args, status, result, handler, respond }) => {
|
|
80
|
+
* return <div>Rendering action: {name}</div>;
|
|
81
|
+
* },
|
|
82
|
+
* });
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* <img src="/images/use-copilot-action/useCopilotAction.gif" width="500" />
|
|
87
|
+
* `useCopilotAction` is a React hook that you can use in your application to provide
|
|
88
|
+
* custom actions that can be called by the AI. Essentially, it allows the Copilot to
|
|
89
|
+
* execute these actions contextually during a chat, based on the user's interactions
|
|
90
|
+
* and needs.
|
|
91
|
+
*
|
|
92
|
+
* Here's how it works:
|
|
93
|
+
*
|
|
94
|
+
* Use `useCopilotAction` to set up actions that the Copilot can call. To provide
|
|
95
|
+
* more context to the Copilot, you can provide it with a `description` (for example to explain
|
|
96
|
+
* what the action does, under which conditions it can be called, etc.).
|
|
97
|
+
*
|
|
98
|
+
* Then you define the parameters of the action, which can be simple, e.g. primitives like strings or numbers,
|
|
99
|
+
* or complex, e.g. objects or arrays.
|
|
100
|
+
*
|
|
101
|
+
* Finally, you provide a `handler` function that receives the parameters and returns a result.
|
|
102
|
+
* CopilotKit takes care of automatically inferring the parameter types, so you get type safety
|
|
103
|
+
* and autocompletion for free.
|
|
104
|
+
*
|
|
105
|
+
* To render a custom UI for the action, you can provide a `render()` function. This function
|
|
106
|
+
* lets you render a custom component or return a string to display.
|
|
107
|
+
*
|
|
108
|
+
* ## Usage
|
|
109
|
+
*
|
|
110
|
+
* ### Simple Usage
|
|
111
|
+
*
|
|
112
|
+
* ```tsx
|
|
113
|
+
* useCopilotAction({
|
|
114
|
+
* name: "sayHello",
|
|
115
|
+
* description: "Say hello to someone.",
|
|
116
|
+
* parameters: [
|
|
117
|
+
* {
|
|
118
|
+
* name: "name",
|
|
119
|
+
* type: "string",
|
|
120
|
+
* description: "name of the person to say greet",
|
|
121
|
+
* },
|
|
122
|
+
* ],
|
|
123
|
+
* handler: async ({ name }) => {
|
|
124
|
+
* alert(`Hello, ${name}!`);
|
|
125
|
+
* },
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* ## Generative UI
|
|
130
|
+
*
|
|
131
|
+
* This hooks enables you to dynamically generate UI elements and render them in the copilot chat. For more information, check out the [Generative UI](/guides/generative-ui) page.
|
|
132
|
+
*/
|
|
133
|
+
import { Parameter, randomId } from "@copilotkit/shared";
|
|
134
|
+
import { createElement, Fragment, useEffect, useRef } from "react";
|
|
135
|
+
import { useCopilotContext } from "../context/copilot-context";
|
|
136
|
+
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
137
|
+
import {
|
|
138
|
+
ActionRenderProps,
|
|
139
|
+
ActionRenderPropsNoArgsWait,
|
|
140
|
+
ActionRenderPropsWait,
|
|
141
|
+
CatchAllFrontendAction,
|
|
142
|
+
FrontendAction,
|
|
143
|
+
} from "../types/frontend-action";
|
|
144
|
+
|
|
145
|
+
// We implement useCopilotAction dependency handling so that
|
|
146
|
+
// the developer has the option to not provide any dependencies.
|
|
147
|
+
// In this case, we assume they want to update the handler on each rerender.
|
|
148
|
+
// To avoid getting stuck in an infinite loop, we update the handler directly,
|
|
149
|
+
// skipping React state updates.
|
|
150
|
+
// This is ok in this case, because the handler is not part of any UI that
|
|
151
|
+
// needs to be updated.
|
|
152
|
+
// useCallback, useMemo or other memoization techniques are not suitable here,
|
|
153
|
+
// because they will cause a infinite rerender loop.
|
|
154
|
+
export function useCopilotAction<const T extends Parameter[] | [] = []>(
|
|
155
|
+
action: FrontendAction<T> | CatchAllFrontendAction,
|
|
156
|
+
dependencies?: any[],
|
|
157
|
+
): void {
|
|
158
|
+
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
159
|
+
const idRef = useRef<string>(randomId());
|
|
160
|
+
const renderAndWaitRef = useRef<RenderAndWaitForResponse | null>(null);
|
|
161
|
+
|
|
162
|
+
// clone the action to avoid mutating the original object
|
|
163
|
+
action = { ...action };
|
|
164
|
+
|
|
165
|
+
// If the developer provides a renderAndWaitForResponse function, we transform the action
|
|
166
|
+
// to use a promise internally, so that we can treat it like a normal action.
|
|
167
|
+
if (
|
|
168
|
+
// renderAndWaitForResponse is not available for catch all actions
|
|
169
|
+
isFrontendAction(action) &&
|
|
170
|
+
// check if renderAndWaitForResponse is set
|
|
171
|
+
(action.renderAndWait || action.renderAndWaitForResponse)
|
|
172
|
+
) {
|
|
173
|
+
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
174
|
+
// remove the renderAndWait function from the action
|
|
175
|
+
action.renderAndWait = undefined;
|
|
176
|
+
action.renderAndWaitForResponse = undefined;
|
|
177
|
+
// add a handler that will be called when the action is executed
|
|
178
|
+
action.handler = useAsyncCallback(async () => {
|
|
179
|
+
// we create a new promise when the handler is called
|
|
180
|
+
let resolve: (result: any) => void;
|
|
181
|
+
let reject: (error: any) => void;
|
|
182
|
+
const promise = new Promise<any>((resolvePromise, rejectPromise) => {
|
|
183
|
+
resolve = resolvePromise;
|
|
184
|
+
reject = rejectPromise;
|
|
185
|
+
});
|
|
186
|
+
renderAndWaitRef.current = { promise, resolve: resolve!, reject: reject! };
|
|
187
|
+
// then we await the promise (it will be resolved in the original renderAndWait function)
|
|
188
|
+
return await promise;
|
|
189
|
+
}, []) as any;
|
|
190
|
+
|
|
191
|
+
// add a render function that will be called when the action is rendered
|
|
192
|
+
action.render = ((props: ActionRenderProps<T>): React.ReactElement => {
|
|
193
|
+
// Specifically for renderAndWaitForResponse the executing state is set too early, causing a race condition
|
|
194
|
+
// To fit it: we will wait for the handler to be ready
|
|
195
|
+
let status = props.status;
|
|
196
|
+
if (props.status === "executing" && !renderAndWaitRef.current) {
|
|
197
|
+
status = "inProgress";
|
|
198
|
+
}
|
|
199
|
+
// Create type safe waitProps based on whether T extends empty array or not
|
|
200
|
+
const waitProps = {
|
|
201
|
+
status,
|
|
202
|
+
args: props.args,
|
|
203
|
+
result: props.result,
|
|
204
|
+
handler: status === "executing" ? renderAndWaitRef.current!.resolve : undefined,
|
|
205
|
+
respond: status === "executing" ? renderAndWaitRef.current!.resolve : undefined,
|
|
206
|
+
} as T extends [] ? ActionRenderPropsNoArgsWait<T> : ActionRenderPropsWait<T>;
|
|
207
|
+
|
|
208
|
+
// Type guard to check if renderAndWait is for no args case
|
|
209
|
+
const isNoArgsRenderWait = (
|
|
210
|
+
_fn:
|
|
211
|
+
| ((props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement)
|
|
212
|
+
| ((props: ActionRenderPropsWait<T>) => React.ReactElement),
|
|
213
|
+
): _fn is (props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement => {
|
|
214
|
+
return action.parameters?.length === 0;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// Safely call renderAndWait with correct props type
|
|
218
|
+
if (renderAndWait) {
|
|
219
|
+
if (isNoArgsRenderWait(renderAndWait)) {
|
|
220
|
+
return renderAndWait(waitProps as ActionRenderPropsNoArgsWait<T>);
|
|
221
|
+
} else {
|
|
222
|
+
return renderAndWait(waitProps as ActionRenderPropsWait<T>);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Return empty Fragment instead of null
|
|
227
|
+
return createElement(Fragment);
|
|
228
|
+
}) as any;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// If the developer doesn't provide dependencies, we assume they want to
|
|
232
|
+
// update handler and render function when the action object changes.
|
|
233
|
+
// This ensures that any captured variables in the handler are up to date.
|
|
234
|
+
if (dependencies === undefined) {
|
|
235
|
+
if (actions[idRef.current]) {
|
|
236
|
+
// catch all actions don't have a handler
|
|
237
|
+
if (isFrontendAction(action)) {
|
|
238
|
+
actions[idRef.current].handler = action.handler as any;
|
|
239
|
+
}
|
|
240
|
+
if (typeof action.render === "function") {
|
|
241
|
+
if (chatComponentsCache.current !== null) {
|
|
242
|
+
// TODO: using as any here because the type definitions are getting to tricky
|
|
243
|
+
// not wasting time on this now - we know the types are compatible
|
|
244
|
+
chatComponentsCache.current.actions[action.name] = action.render as any;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
setAction(idRef.current, action as any);
|
|
252
|
+
if (chatComponentsCache.current !== null && action.render !== undefined) {
|
|
253
|
+
// see comment about type safety above
|
|
254
|
+
chatComponentsCache.current.actions[action.name] = action.render as any;
|
|
255
|
+
}
|
|
256
|
+
return () => {
|
|
257
|
+
// NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.
|
|
258
|
+
// This is because we currently don't have access to the messages array in CopilotContext.
|
|
259
|
+
// UPDATE: We now have access, we should remove the entry if not referenced by any message.
|
|
260
|
+
removeAction(idRef.current);
|
|
261
|
+
};
|
|
262
|
+
}, [
|
|
263
|
+
setAction,
|
|
264
|
+
removeAction,
|
|
265
|
+
isFrontendAction(action) ? action.description : undefined,
|
|
266
|
+
action.name,
|
|
267
|
+
isFrontendAction(action) ? action.disabled : undefined,
|
|
268
|
+
isFrontendAction(action) ? action.available : undefined,
|
|
269
|
+
// This should be faster than deep equality checking
|
|
270
|
+
// In addition, all major JS engines guarantee the order of object keys
|
|
271
|
+
JSON.stringify(isFrontendAction(action) ? action.parameters : []),
|
|
272
|
+
// include render only if it's a string
|
|
273
|
+
typeof action.render === "string" ? action.render : undefined,
|
|
274
|
+
// dependencies set by the developer
|
|
275
|
+
...(dependencies || []),
|
|
276
|
+
]);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isFrontendAction<T extends Parameter[]>(
|
|
280
|
+
action: FrontendAction<T> | CatchAllFrontendAction,
|
|
281
|
+
): action is FrontendAction<T> {
|
|
282
|
+
return action.name !== "*";
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
interface RenderAndWaitForResponse {
|
|
286
|
+
promise: Promise<any>;
|
|
287
|
+
resolve: (result: any) => void;
|
|
288
|
+
reject: (error: any) => void;
|
|
289
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Parameter } from "@copilotkit/shared";
|
|
2
|
+
import { Fragment, useCallback, useRef } from "react";
|
|
3
|
+
import { useCopilotContext } from "../context/copilot-context";
|
|
4
|
+
import { FrontendAction, ActionRenderProps } from "../types/frontend-action";
|
|
5
|
+
import { useCopilotAction } from "./use-copilot-action";
|
|
6
|
+
import React from "react";
|
|
7
|
+
|
|
8
|
+
export function useCopilotAuthenticatedAction<T extends Parameter[]>(
|
|
9
|
+
action: FrontendAction<T>,
|
|
10
|
+
dependencies?: any[],
|
|
11
|
+
): void {
|
|
12
|
+
const { authConfig, authStates, setAuthStates } = useCopilotContext();
|
|
13
|
+
const pendingActionRef = useRef<ActionRenderProps<Parameter[]> | null>(null);
|
|
14
|
+
|
|
15
|
+
const executeAction = useCallback(
|
|
16
|
+
(props: ActionRenderProps<Parameter[]>) => {
|
|
17
|
+
if (typeof action.render === "function") {
|
|
18
|
+
return action.render(props);
|
|
19
|
+
}
|
|
20
|
+
return action.render || React.createElement(Fragment);
|
|
21
|
+
},
|
|
22
|
+
[action],
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const wrappedRender = useCallback(
|
|
26
|
+
(props: ActionRenderProps<Parameter[]>): string | React.ReactElement => {
|
|
27
|
+
const isAuthenticated = Object.values(authStates || {}).some(
|
|
28
|
+
(state) => state.status === "authenticated",
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
if (!isAuthenticated) {
|
|
32
|
+
// Store action details for later execution
|
|
33
|
+
pendingActionRef.current = props;
|
|
34
|
+
|
|
35
|
+
return authConfig?.SignInComponent
|
|
36
|
+
? React.createElement(authConfig.SignInComponent, {
|
|
37
|
+
onSignInComplete: (authState) => {
|
|
38
|
+
setAuthStates?.((prev) => ({ ...prev, [action.name]: authState }));
|
|
39
|
+
if (pendingActionRef.current) {
|
|
40
|
+
executeAction(pendingActionRef.current);
|
|
41
|
+
pendingActionRef.current = null;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
: React.createElement(Fragment);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return executeAction(props);
|
|
49
|
+
},
|
|
50
|
+
[action, authStates, setAuthStates],
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
useCopilotAction(
|
|
54
|
+
{
|
|
55
|
+
...action,
|
|
56
|
+
render: wrappedRender,
|
|
57
|
+
} as FrontendAction<T>,
|
|
58
|
+
dependencies,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useCopilotChat` is a React hook that lets you directly interact with the
|
|
3
|
+
* Copilot instance. Use to implement a fully custom UI (headless UI) or to
|
|
4
|
+
* programmatically interact with the Copilot instance managed by the default
|
|
5
|
+
* UI.
|
|
6
|
+
*
|
|
7
|
+
* ## Usage
|
|
8
|
+
*
|
|
9
|
+
* ### Simple Usage
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { useCopilotChat } from "@copilotkit/react-core";
|
|
13
|
+
* import { Role, TextMessage } from "@copilotkit/runtime-client-gql";
|
|
14
|
+
*
|
|
15
|
+
* export function YourComponent() {
|
|
16
|
+
* const { appendMessage } = useCopilotChat();
|
|
17
|
+
*
|
|
18
|
+
* appendMessage(
|
|
19
|
+
* new TextMessage({
|
|
20
|
+
* content: "Hello World",
|
|
21
|
+
* role: Role.User,
|
|
22
|
+
* }),
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* // optionally, you can append a message without running chat completion
|
|
26
|
+
* appendMessage(yourMessage, { followUp: false });
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* `useCopilotChat` returns an object with the following properties:
|
|
31
|
+
*
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const {
|
|
34
|
+
* visibleMessages, // An array of messages that are currently visible in the chat.
|
|
35
|
+
* appendMessage, // A function to append a message to the chat.
|
|
36
|
+
* setMessages, // A function to set the messages in the chat.
|
|
37
|
+
* deleteMessage, // A function to delete a message from the chat.
|
|
38
|
+
* reloadMessages, // A function to reload the messages from the API.
|
|
39
|
+
* stopGeneration, // A function to stop the generation of the next message.
|
|
40
|
+
* isLoading, // A boolean indicating if the chat is loading.
|
|
41
|
+
* } = useCopilotChat();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
45
|
+
import { AgentSession, useCopilotContext } from "../context/copilot-context";
|
|
46
|
+
import { Message, Role, TextMessage } from "@copilotkit/runtime-client-gql";
|
|
47
|
+
import { SystemMessageFunction } from "../types";
|
|
48
|
+
import { useChat, AppendMessageOptions } from "./use-chat";
|
|
49
|
+
import { defaultCopilotContextCategories } from "../components";
|
|
50
|
+
import { CoAgentStateRenderHandlerArguments } from "@copilotkit/shared";
|
|
51
|
+
import { useCopilotMessagesContext } from "../context";
|
|
52
|
+
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
53
|
+
|
|
54
|
+
export interface UseCopilotChatOptions {
|
|
55
|
+
/**
|
|
56
|
+
* A unique identifier for the chat. If not provided, a random one will be
|
|
57
|
+
* generated. When provided, the `useChat` hook with the same `id` will
|
|
58
|
+
* have shared states across components.
|
|
59
|
+
*/
|
|
60
|
+
id?: string;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* HTTP headers to be sent with the API request.
|
|
64
|
+
*/
|
|
65
|
+
headers?: Record<string, string> | Headers;
|
|
66
|
+
/**
|
|
67
|
+
* System messages of the chat. Defaults to an empty array.
|
|
68
|
+
*/
|
|
69
|
+
initialMessages?: Message[];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A function to generate the system message. Defaults to `defaultSystemMessage`.
|
|
73
|
+
*/
|
|
74
|
+
makeSystemMessage?: SystemMessageFunction;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface UseCopilotChatReturn {
|
|
78
|
+
visibleMessages: Message[];
|
|
79
|
+
appendMessage: (message: Message, options?: AppendMessageOptions) => Promise<void>;
|
|
80
|
+
setMessages: (messages: Message[]) => void;
|
|
81
|
+
deleteMessage: (messageId: string) => void;
|
|
82
|
+
reloadMessages: () => Promise<void>;
|
|
83
|
+
stopGeneration: () => void;
|
|
84
|
+
reset: () => void;
|
|
85
|
+
isLoading: boolean;
|
|
86
|
+
runChatCompletion: () => Promise<Message[]>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function useCopilotChat({
|
|
90
|
+
makeSystemMessage,
|
|
91
|
+
...options
|
|
92
|
+
}: UseCopilotChatOptions = {}): UseCopilotChatReturn {
|
|
93
|
+
const {
|
|
94
|
+
getContextString,
|
|
95
|
+
getFunctionCallHandler,
|
|
96
|
+
copilotApiConfig,
|
|
97
|
+
isLoading,
|
|
98
|
+
setIsLoading,
|
|
99
|
+
chatInstructions,
|
|
100
|
+
actions,
|
|
101
|
+
coagentStatesRef,
|
|
102
|
+
setCoagentStatesWithRef,
|
|
103
|
+
coAgentStateRenders,
|
|
104
|
+
agentSession,
|
|
105
|
+
setAgentSession,
|
|
106
|
+
forwardedParameters,
|
|
107
|
+
agentLock,
|
|
108
|
+
threadId,
|
|
109
|
+
setThreadId,
|
|
110
|
+
runId,
|
|
111
|
+
setRunId,
|
|
112
|
+
chatAbortControllerRef,
|
|
113
|
+
} = useCopilotContext();
|
|
114
|
+
const { messages, setMessages } = useCopilotMessagesContext();
|
|
115
|
+
|
|
116
|
+
// We need to ensure that makeSystemMessageCallback always uses the latest
|
|
117
|
+
// useCopilotReadable data.
|
|
118
|
+
const latestGetContextString = useUpdatedRef(getContextString);
|
|
119
|
+
const deleteMessage = useCallback(
|
|
120
|
+
(messageId: string) => {
|
|
121
|
+
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
122
|
+
},
|
|
123
|
+
[setMessages],
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const makeSystemMessageCallback = useCallback(() => {
|
|
127
|
+
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
128
|
+
// this always gets the latest context string
|
|
129
|
+
const contextString = latestGetContextString.current([], defaultCopilotContextCategories); // TODO: make the context categories configurable
|
|
130
|
+
|
|
131
|
+
return new TextMessage({
|
|
132
|
+
content: systemMessageMaker(contextString, chatInstructions),
|
|
133
|
+
role: Role.System,
|
|
134
|
+
});
|
|
135
|
+
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
136
|
+
|
|
137
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
138
|
+
async (args: CoAgentStateRenderHandlerArguments) => {
|
|
139
|
+
const { name, nodeName, state } = args;
|
|
140
|
+
let action = Object.values(coAgentStateRenders).find(
|
|
141
|
+
(action) => action.name === name && action.nodeName === nodeName,
|
|
142
|
+
);
|
|
143
|
+
if (!action) {
|
|
144
|
+
action = Object.values(coAgentStateRenders).find(
|
|
145
|
+
(action) => action.name === name && !action.nodeName,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
if (action) {
|
|
149
|
+
await action.handler?.({ state, nodeName });
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
[coAgentStateRenders],
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const { append, reload, stop, runChatCompletion } = useChat({
|
|
156
|
+
...options,
|
|
157
|
+
actions: Object.values(actions),
|
|
158
|
+
copilotConfig: copilotApiConfig,
|
|
159
|
+
initialMessages: options.initialMessages || [],
|
|
160
|
+
onFunctionCall: getFunctionCallHandler(),
|
|
161
|
+
onCoAgentStateRender,
|
|
162
|
+
messages,
|
|
163
|
+
setMessages,
|
|
164
|
+
makeSystemMessageCallback,
|
|
165
|
+
isLoading,
|
|
166
|
+
setIsLoading,
|
|
167
|
+
coagentStatesRef,
|
|
168
|
+
setCoagentStatesWithRef,
|
|
169
|
+
agentSession,
|
|
170
|
+
setAgentSession,
|
|
171
|
+
forwardedParameters,
|
|
172
|
+
threadId,
|
|
173
|
+
setThreadId,
|
|
174
|
+
runId,
|
|
175
|
+
setRunId,
|
|
176
|
+
chatAbortControllerRef,
|
|
177
|
+
agentLock,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// this is a workaround born out of a bug that Athena incessantly ran into.
|
|
181
|
+
// 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 --
|
|
182
|
+
// it referenced the old state of the messages array, and not the latest one.
|
|
183
|
+
//
|
|
184
|
+
// 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.
|
|
185
|
+
//
|
|
186
|
+
// How does this work?
|
|
187
|
+
// we store the relevant function in a ref that is always up-to-date, and then we use that ref in the callback.
|
|
188
|
+
const latestAppend = useUpdatedRef(append);
|
|
189
|
+
const latestAppendFunc = useAsyncCallback(
|
|
190
|
+
async (message: Message, options?: AppendMessageOptions) => {
|
|
191
|
+
return await latestAppend.current(message, options);
|
|
192
|
+
},
|
|
193
|
+
[latestAppend],
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const latestReload = useUpdatedRef(reload);
|
|
197
|
+
const latestReloadFunc = useAsyncCallback(async () => {
|
|
198
|
+
return await latestReload.current();
|
|
199
|
+
}, [latestReload]);
|
|
200
|
+
|
|
201
|
+
const latestStop = useUpdatedRef(stop);
|
|
202
|
+
const latestStopFunc = useCallback(() => {
|
|
203
|
+
return latestStop.current();
|
|
204
|
+
}, [latestStop]);
|
|
205
|
+
|
|
206
|
+
const latestDelete = useUpdatedRef(deleteMessage);
|
|
207
|
+
const latestDeleteFunc = useCallback(
|
|
208
|
+
(messageId: string) => {
|
|
209
|
+
return latestDelete.current(messageId);
|
|
210
|
+
},
|
|
211
|
+
[latestDelete],
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const latestSetMessages = useUpdatedRef(setMessages);
|
|
215
|
+
const latestSetMessagesFunc = useCallback(
|
|
216
|
+
(messages: Message[]) => {
|
|
217
|
+
return latestSetMessages.current(messages);
|
|
218
|
+
},
|
|
219
|
+
[latestSetMessages],
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
223
|
+
const latestRunChatCompletionFunc = useAsyncCallback(async () => {
|
|
224
|
+
return await latestRunChatCompletion.current!();
|
|
225
|
+
}, [latestRunChatCompletion]);
|
|
226
|
+
|
|
227
|
+
const reset = useCallback(() => {
|
|
228
|
+
latestStopFunc();
|
|
229
|
+
setMessages([]);
|
|
230
|
+
setThreadId(null);
|
|
231
|
+
setRunId(null);
|
|
232
|
+
setCoagentStatesWithRef({});
|
|
233
|
+
let initialAgentSession: AgentSession | null = null;
|
|
234
|
+
if (agentLock) {
|
|
235
|
+
initialAgentSession = {
|
|
236
|
+
agentName: agentLock,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
setAgentSession(initialAgentSession);
|
|
240
|
+
}, [
|
|
241
|
+
latestStopFunc,
|
|
242
|
+
setMessages,
|
|
243
|
+
setThreadId,
|
|
244
|
+
setCoagentStatesWithRef,
|
|
245
|
+
setAgentSession,
|
|
246
|
+
agentLock,
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
const latestReset = useUpdatedRef(reset);
|
|
250
|
+
const latestResetFunc = useCallback(() => {
|
|
251
|
+
return latestReset.current();
|
|
252
|
+
}, [latestReset]);
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
visibleMessages: messages,
|
|
256
|
+
appendMessage: latestAppendFunc,
|
|
257
|
+
setMessages: latestSetMessagesFunc,
|
|
258
|
+
reloadMessages: latestReloadFunc,
|
|
259
|
+
stopGeneration: latestStopFunc,
|
|
260
|
+
reset: latestResetFunc,
|
|
261
|
+
deleteMessage: latestDeleteFunc,
|
|
262
|
+
runChatCompletion: latestRunChatCompletionFunc,
|
|
263
|
+
isLoading,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// store `value` in a ref and update
|
|
268
|
+
// it whenever it changes.
|
|
269
|
+
function useUpdatedRef<T>(value: T) {
|
|
270
|
+
const ref = useRef(value);
|
|
271
|
+
|
|
272
|
+
useEffect(() => {
|
|
273
|
+
ref.current = value;
|
|
274
|
+
}, [value]);
|
|
275
|
+
|
|
276
|
+
return ref;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function defaultSystemMessage(
|
|
280
|
+
contextString: string,
|
|
281
|
+
additionalInstructions?: string,
|
|
282
|
+
): string {
|
|
283
|
+
return (
|
|
284
|
+
`
|
|
285
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
286
|
+
|
|
287
|
+
Help 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.
|
|
288
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
289
|
+
|
|
290
|
+
The user has provided you with the following context:
|
|
291
|
+
\`\`\`
|
|
292
|
+
${contextString}
|
|
293
|
+
\`\`\`
|
|
294
|
+
|
|
295
|
+
They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
|
|
296
|
+
|
|
297
|
+
Please assist them as best you can.
|
|
298
|
+
|
|
299
|
+
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.
|
|
300
|
+
|
|
301
|
+
If you would like to call a function, call it without saying anything else.
|
|
302
|
+
` + (additionalInstructions ? `\n\n${additionalInstructions}` : "")
|
|
303
|
+
);
|
|
304
|
+
}
|