@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,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useCopilotReadable` is a React hook that provides app-state and other information
|
|
3
|
+
* to the Copilot. Optionally, the hook can also handle hierarchical state within your
|
|
4
|
+
* application, passing these parent-child relationships to the Copilot.
|
|
5
|
+
*
|
|
6
|
+
* ## Usage
|
|
7
|
+
*
|
|
8
|
+
* ### Simple Usage
|
|
9
|
+
*
|
|
10
|
+
* In its most basic usage, useCopilotReadable accepts a single string argument
|
|
11
|
+
* representing any piece of app state, making it available for the Copilot to use
|
|
12
|
+
* as context when responding to user input.
|
|
13
|
+
*
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { useCopilotReadable } from "@copilotkit/react-core";
|
|
16
|
+
*
|
|
17
|
+
* export function MyComponent() {
|
|
18
|
+
* const [employees, setEmployees] = useState([]);
|
|
19
|
+
*
|
|
20
|
+
* useCopilotReadable({
|
|
21
|
+
* description: "The list of employees",
|
|
22
|
+
* value: employees,
|
|
23
|
+
* });
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* ### Nested Components
|
|
28
|
+
*
|
|
29
|
+
* Optionally, you can maintain the hierarchical structure of information by passing
|
|
30
|
+
* `parentId`. This allows you to use `useCopilotReadable` in nested components:
|
|
31
|
+
*
|
|
32
|
+
* ```tsx /employeeContextId/1 {17,23}
|
|
33
|
+
* import { useCopilotReadable } from "@copilotkit/react-core";
|
|
34
|
+
*
|
|
35
|
+
* function Employee(props: EmployeeProps) {
|
|
36
|
+
* const { employeeName, workProfile, metadata } = props;
|
|
37
|
+
*
|
|
38
|
+
* // propagate any information to copilot
|
|
39
|
+
* const employeeContextId = useCopilotReadable({
|
|
40
|
+
* description: "Employee name",
|
|
41
|
+
* value: employeeName
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Pass a parentID to maintain a hierarchical structure.
|
|
45
|
+
* // Especially useful with child React components, list elements, etc.
|
|
46
|
+
* useCopilotReadable({
|
|
47
|
+
* description: "Work profile",
|
|
48
|
+
* value: workProfile.description(),
|
|
49
|
+
* parentId: employeeContextId
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* useCopilotReadable({
|
|
53
|
+
* description: "Employee metadata",
|
|
54
|
+
* value: metadata.description(),
|
|
55
|
+
* parentId: employeeContextId
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* return (
|
|
59
|
+
* // Render as usual...
|
|
60
|
+
* );
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
import { useEffect, useRef } from "react";
|
|
65
|
+
import { useCopilotContext } from "../context/copilot-context";
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Options for the useCopilotReadable hook.
|
|
69
|
+
*/
|
|
70
|
+
export interface UseCopilotReadableOptions {
|
|
71
|
+
/**
|
|
72
|
+
* The description of the information to be added to the Copilot context.
|
|
73
|
+
*/
|
|
74
|
+
description: string;
|
|
75
|
+
/**
|
|
76
|
+
* The value to be added to the Copilot context. Object values are automatically stringified.
|
|
77
|
+
*/
|
|
78
|
+
value: any;
|
|
79
|
+
/**
|
|
80
|
+
* The ID of the parent context, if any.
|
|
81
|
+
*/
|
|
82
|
+
parentId?: string;
|
|
83
|
+
/**
|
|
84
|
+
* An array of categories to control which context are visible where. Particularly useful
|
|
85
|
+
* with CopilotTextarea (see `useMakeAutosuggestionFunction`)
|
|
86
|
+
*/
|
|
87
|
+
categories?: string[];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A custom conversion function to use to serialize the value to a string. If not provided, the value
|
|
91
|
+
* will be serialized using `JSON.stringify`.
|
|
92
|
+
*/
|
|
93
|
+
convert?: (description: string, value: any) => string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function convertToJSON(description: string, value: any): string {
|
|
97
|
+
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Adds the given information to the Copilot context to make it readable by Copilot.
|
|
102
|
+
*/
|
|
103
|
+
export function useCopilotReadable(
|
|
104
|
+
{ description, value, parentId, categories, convert }: UseCopilotReadableOptions,
|
|
105
|
+
dependencies?: any[],
|
|
106
|
+
): string | undefined {
|
|
107
|
+
const { addContext, removeContext } = useCopilotContext();
|
|
108
|
+
const idRef = useRef<string>();
|
|
109
|
+
convert = convert || convertToJSON;
|
|
110
|
+
|
|
111
|
+
const information = convert(description, value);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
const id = addContext(information, parentId, categories);
|
|
115
|
+
idRef.current = id;
|
|
116
|
+
|
|
117
|
+
return () => {
|
|
118
|
+
removeContext(id);
|
|
119
|
+
};
|
|
120
|
+
}, [information, parentId, addContext, removeContext, ...(dependencies || [])]);
|
|
121
|
+
|
|
122
|
+
return idRef.current;
|
|
123
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CopilotRuntimeClient,
|
|
3
|
+
CopilotRuntimeClientOptions,
|
|
4
|
+
GraphQLError,
|
|
5
|
+
} from "@copilotkit/runtime-client-gql";
|
|
6
|
+
import { useToast } from "../components/toast/toast-provider";
|
|
7
|
+
import { useMemo } from "react";
|
|
8
|
+
import { useErrorToast } from "../components/error-boundary/error-utils";
|
|
9
|
+
|
|
10
|
+
export const useCopilotRuntimeClient = (options: CopilotRuntimeClientOptions) => {
|
|
11
|
+
const { addGraphQLErrorsToast } = useToast();
|
|
12
|
+
const addErrorToast = useErrorToast();
|
|
13
|
+
|
|
14
|
+
const runtimeClient = useMemo(() => {
|
|
15
|
+
return new CopilotRuntimeClient({
|
|
16
|
+
...options,
|
|
17
|
+
handleGQLErrors: (error) => {
|
|
18
|
+
if ((error as any).graphQLErrors.length) {
|
|
19
|
+
addGraphQLErrorsToast((error as any).graphQLErrors as GraphQLError[]);
|
|
20
|
+
} else {
|
|
21
|
+
addErrorToast([error]);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}, [options, addGraphQLErrorsToast]);
|
|
26
|
+
|
|
27
|
+
return runtimeClient;
|
|
28
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { useCallback, useReducer } from "react";
|
|
2
|
+
import { randomId } from "@copilotkit/shared";
|
|
3
|
+
|
|
4
|
+
export type FlatCategoryStoreId = string;
|
|
5
|
+
|
|
6
|
+
export interface UseFlatCategoryStoreReturn<T> {
|
|
7
|
+
addElement: (value: T, categories: string[]) => FlatCategoryStoreId;
|
|
8
|
+
removeElement: (id: FlatCategoryStoreId) => void;
|
|
9
|
+
allElements: (categories: string[]) => T[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FlatCategoryStoreElement<T> {
|
|
13
|
+
id: FlatCategoryStoreId;
|
|
14
|
+
value: T;
|
|
15
|
+
categories: Set<string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {
|
|
19
|
+
const [elements, dispatch] = useReducer<
|
|
20
|
+
React.Reducer<Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>, Action<T>>
|
|
21
|
+
>(flatCategoryStoreReducer, new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>());
|
|
22
|
+
|
|
23
|
+
const addElement = useCallback((value: T, categories: string[]): FlatCategoryStoreId => {
|
|
24
|
+
const newId = randomId();
|
|
25
|
+
dispatch({
|
|
26
|
+
type: "ADD_ELEMENT",
|
|
27
|
+
value,
|
|
28
|
+
id: newId,
|
|
29
|
+
categories,
|
|
30
|
+
});
|
|
31
|
+
return newId;
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
const removeElement = useCallback((id: FlatCategoryStoreId): void => {
|
|
35
|
+
dispatch({ type: "REMOVE_ELEMENT", id });
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
const allElements = useCallback(
|
|
39
|
+
(categories: string[]): T[] => {
|
|
40
|
+
const categoriesSet = new Set(categories);
|
|
41
|
+
const result: T[] = [];
|
|
42
|
+
elements.forEach((element) => {
|
|
43
|
+
if (setsHaveIntersection(categoriesSet, element.categories)) {
|
|
44
|
+
result.push(element.value);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
},
|
|
49
|
+
[elements],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return { addElement, removeElement, allElements };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default useFlatCategoryStore;
|
|
56
|
+
|
|
57
|
+
// Action types
|
|
58
|
+
type Action<T> =
|
|
59
|
+
| {
|
|
60
|
+
type: "ADD_ELEMENT";
|
|
61
|
+
value: T;
|
|
62
|
+
id: FlatCategoryStoreId;
|
|
63
|
+
categories: string[];
|
|
64
|
+
}
|
|
65
|
+
| { type: "REMOVE_ELEMENT"; id: FlatCategoryStoreId };
|
|
66
|
+
|
|
67
|
+
// Reducer
|
|
68
|
+
function flatCategoryStoreReducer<T>(
|
|
69
|
+
state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
|
|
70
|
+
action: Action<T>,
|
|
71
|
+
): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {
|
|
72
|
+
switch (action.type) {
|
|
73
|
+
case "ADD_ELEMENT": {
|
|
74
|
+
const { value, id, categories } = action;
|
|
75
|
+
const newElement: FlatCategoryStoreElement<T> = {
|
|
76
|
+
id,
|
|
77
|
+
value,
|
|
78
|
+
categories: new Set(categories),
|
|
79
|
+
};
|
|
80
|
+
const newState = new Map(state);
|
|
81
|
+
newState.set(id, newElement);
|
|
82
|
+
return newState;
|
|
83
|
+
}
|
|
84
|
+
case "REMOVE_ELEMENT": {
|
|
85
|
+
const newState = new Map(state);
|
|
86
|
+
newState.delete(action.id);
|
|
87
|
+
return newState;
|
|
88
|
+
}
|
|
89
|
+
default:
|
|
90
|
+
return state;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
|
|
95
|
+
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
96
|
+
|
|
97
|
+
for (let item of smallerSet) {
|
|
98
|
+
if (largerSet.has(item)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { useCopilotContext } from "../context/copilot-context";
|
|
3
|
+
import { DocumentPointer } from "../types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Makes a document readable by Copilot.
|
|
7
|
+
* @param document The document to make readable.
|
|
8
|
+
* @param categories The categories to associate with the document.
|
|
9
|
+
* @param dependencies The dependencies to use for the effect.
|
|
10
|
+
* @returns The id of the document.
|
|
11
|
+
*/
|
|
12
|
+
export function useMakeCopilotDocumentReadable(
|
|
13
|
+
document: DocumentPointer,
|
|
14
|
+
categories?: string[],
|
|
15
|
+
dependencies: any[] = [],
|
|
16
|
+
): string | undefined {
|
|
17
|
+
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
18
|
+
const idRef = useRef<string>();
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const id = addDocumentContext(document, categories);
|
|
22
|
+
idRef.current = id;
|
|
23
|
+
|
|
24
|
+
return () => {
|
|
25
|
+
removeDocumentContext(id);
|
|
26
|
+
};
|
|
27
|
+
}, [addDocumentContext, removeDocumentContext, ...dependencies]);
|
|
28
|
+
|
|
29
|
+
return idRef.current;
|
|
30
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { randomId } from "@copilotkit/shared";
|
|
2
|
+
import { useCallback, useReducer } from "react";
|
|
3
|
+
|
|
4
|
+
export type TreeNodeId = string;
|
|
5
|
+
|
|
6
|
+
export interface TreeNode {
|
|
7
|
+
id: TreeNodeId;
|
|
8
|
+
value: string;
|
|
9
|
+
children: TreeNode[];
|
|
10
|
+
parentId?: TreeNodeId;
|
|
11
|
+
categories: Set<string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Tree = TreeNode[];
|
|
15
|
+
|
|
16
|
+
export interface UseTreeReturn {
|
|
17
|
+
tree: Tree;
|
|
18
|
+
addElement: (value: string, categories: string[], parentId?: TreeNodeId) => TreeNodeId;
|
|
19
|
+
printTree: (categories: string[]) => string;
|
|
20
|
+
removeElement: (id: TreeNodeId) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {
|
|
24
|
+
for (const node of nodes) {
|
|
25
|
+
if (node.id === id) {
|
|
26
|
+
return node;
|
|
27
|
+
}
|
|
28
|
+
const result = findNode(node.children, id);
|
|
29
|
+
if (result) {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const removeNode = (nodes: Tree, id: TreeNodeId): Tree => {
|
|
37
|
+
return nodes.reduce((result: Tree, node) => {
|
|
38
|
+
if (node.id !== id) {
|
|
39
|
+
const newNode = { ...node, children: removeNode(node.children, id) };
|
|
40
|
+
result.push(newNode);
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}, []);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const addNode = (nodes: Tree, newNode: TreeNode, parentId?: TreeNodeId): Tree => {
|
|
47
|
+
if (!parentId) {
|
|
48
|
+
return [...nodes, newNode];
|
|
49
|
+
}
|
|
50
|
+
return nodes.map((node) => {
|
|
51
|
+
if (node.id === parentId) {
|
|
52
|
+
return { ...node, children: [...node.children, newNode] };
|
|
53
|
+
} else if (node.children.length) {
|
|
54
|
+
return { ...node, children: addNode(node.children, newNode, parentId) };
|
|
55
|
+
}
|
|
56
|
+
return node;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const treeIndentationRepresentation = (index: number, indentLevel: number): string => {
|
|
61
|
+
if (indentLevel === 0) {
|
|
62
|
+
return (index + 1).toString();
|
|
63
|
+
} else if (indentLevel === 1) {
|
|
64
|
+
return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'
|
|
65
|
+
} else if (indentLevel === 2) {
|
|
66
|
+
return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'
|
|
67
|
+
} else {
|
|
68
|
+
return "-";
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const printNode = (node: TreeNode, prefix = "", indentLevel = 0): string => {
|
|
73
|
+
const indent = " ".repeat(3).repeat(indentLevel);
|
|
74
|
+
|
|
75
|
+
const prefixPlusIndentLength = prefix.length + indent.length;
|
|
76
|
+
const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
|
|
77
|
+
|
|
78
|
+
const valueLines = node.value.split("\n");
|
|
79
|
+
|
|
80
|
+
const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
|
|
81
|
+
const outputSubsequentLines = valueLines
|
|
82
|
+
.slice(1)
|
|
83
|
+
.map((line) => `${subsequentLinesPrefix}${line}`)
|
|
84
|
+
.join("\n");
|
|
85
|
+
|
|
86
|
+
let output = `${outputFirstLine}\n`;
|
|
87
|
+
if (outputSubsequentLines) {
|
|
88
|
+
output += `${outputSubsequentLines}\n`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const childPrePrefix = " ".repeat(prefix.length);
|
|
92
|
+
|
|
93
|
+
node.children.forEach(
|
|
94
|
+
(child, index) =>
|
|
95
|
+
(output += printNode(
|
|
96
|
+
child,
|
|
97
|
+
`${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
|
|
98
|
+
indentLevel + 1,
|
|
99
|
+
)),
|
|
100
|
+
);
|
|
101
|
+
return output;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Action types
|
|
105
|
+
type Action =
|
|
106
|
+
| {
|
|
107
|
+
type: "ADD_NODE";
|
|
108
|
+
value: string;
|
|
109
|
+
parentId?: string;
|
|
110
|
+
id: string;
|
|
111
|
+
categories: string[];
|
|
112
|
+
}
|
|
113
|
+
| { type: "REMOVE_NODE"; id: string };
|
|
114
|
+
|
|
115
|
+
// Reducer function
|
|
116
|
+
function treeReducer(state: Tree, action: Action): Tree {
|
|
117
|
+
switch (action.type) {
|
|
118
|
+
case "ADD_NODE": {
|
|
119
|
+
const { value, parentId, id: newNodeId } = action;
|
|
120
|
+
const newNode: TreeNode = {
|
|
121
|
+
id: newNodeId,
|
|
122
|
+
value,
|
|
123
|
+
children: [],
|
|
124
|
+
categories: new Set(action.categories),
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
return addNode(state, newNode, parentId);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(`Error while adding node with id ${newNodeId}: ${error}`);
|
|
131
|
+
return state;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
case "REMOVE_NODE":
|
|
135
|
+
return removeNode(state, action.id);
|
|
136
|
+
default:
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// useTree hook
|
|
142
|
+
const useTree = (): UseTreeReturn => {
|
|
143
|
+
const [tree, dispatch] = useReducer(treeReducer, []);
|
|
144
|
+
|
|
145
|
+
const addElement = useCallback(
|
|
146
|
+
(value: string, categories: string[], parentId?: string): TreeNodeId => {
|
|
147
|
+
const newNodeId = randomId(); // Generate new ID outside of dispatch
|
|
148
|
+
dispatch({
|
|
149
|
+
type: "ADD_NODE",
|
|
150
|
+
value,
|
|
151
|
+
parentId,
|
|
152
|
+
id: newNodeId,
|
|
153
|
+
categories: categories,
|
|
154
|
+
});
|
|
155
|
+
return newNodeId; // Return the new ID
|
|
156
|
+
},
|
|
157
|
+
[],
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const removeElement = useCallback((id: TreeNodeId): void => {
|
|
161
|
+
dispatch({ type: "REMOVE_NODE", id });
|
|
162
|
+
}, []);
|
|
163
|
+
|
|
164
|
+
const printTree = useCallback(
|
|
165
|
+
(categories: string[]): string => {
|
|
166
|
+
const categoriesSet = new Set(categories);
|
|
167
|
+
|
|
168
|
+
let output = "";
|
|
169
|
+
tree.forEach((node, index) => {
|
|
170
|
+
// if the node does not have any of the desired categories, continue to the next node
|
|
171
|
+
if (!setsHaveIntersection(categoriesSet, node.categories)) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// add a new line before each node except the first one
|
|
176
|
+
if (index !== 0) {
|
|
177
|
+
output += "\n";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);
|
|
181
|
+
});
|
|
182
|
+
return output;
|
|
183
|
+
},
|
|
184
|
+
[tree],
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return { tree, addElement, printTree, removeElement };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export default useTree;
|
|
191
|
+
|
|
192
|
+
function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
|
|
193
|
+
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
194
|
+
|
|
195
|
+
for (let item of smallerSet) {
|
|
196
|
+
if (largerSet.has(item)) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return false;
|
|
202
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This class is used to execute one-off tasks, for example on button press. It can use the context available via [useCopilotReadable](/reference/hooks/useCopilotReadable) and the actions provided by [useCopilotAction](/reference/hooks/useCopilotAction), or you can provide your own context and actions.
|
|
3
|
+
*
|
|
4
|
+
* ## Example
|
|
5
|
+
* In the simplest case, use CopilotTask in the context of your app by giving it instructions on what to do.
|
|
6
|
+
*
|
|
7
|
+
* ```tsx
|
|
8
|
+
* import { CopilotTask, useCopilotContext } from "@copilotkit/react-core";
|
|
9
|
+
*
|
|
10
|
+
* export function MyComponent() {
|
|
11
|
+
* const context = useCopilotContext();
|
|
12
|
+
*
|
|
13
|
+
* const task = new CopilotTask({
|
|
14
|
+
* instructions: "Set a random message",
|
|
15
|
+
* actions: [
|
|
16
|
+
* {
|
|
17
|
+
* name: "setMessage",
|
|
18
|
+
* description: "Set the message.",
|
|
19
|
+
* argumentAnnotations: [
|
|
20
|
+
* {
|
|
21
|
+
* name: "message",
|
|
22
|
+
* type: "string",
|
|
23
|
+
* description:
|
|
24
|
+
* "A message to display.",
|
|
25
|
+
* required: true,
|
|
26
|
+
* },
|
|
27
|
+
* ],
|
|
28
|
+
* }
|
|
29
|
+
* ]
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const executeTask = async () => {
|
|
33
|
+
* await task.run(context, action);
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* return (
|
|
37
|
+
* <>
|
|
38
|
+
* <button onClick={executeTask}>
|
|
39
|
+
* Execute task
|
|
40
|
+
* </button>
|
|
41
|
+
* </>
|
|
42
|
+
* )
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* Have a look at the [Presentation Example App](https://github.com/CopilotKit/CopilotKit/blob/main/CopilotKit/examples/next-openai/src/app/presentation/page.tsx) for a more complete example.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import {
|
|
50
|
+
ActionExecutionMessage,
|
|
51
|
+
CopilotRuntimeClient,
|
|
52
|
+
Message,
|
|
53
|
+
Role,
|
|
54
|
+
TextMessage,
|
|
55
|
+
convertGqlOutputToMessages,
|
|
56
|
+
convertMessagesToGqlInput,
|
|
57
|
+
filterAgentStateMessages,
|
|
58
|
+
CopilotRequestType,
|
|
59
|
+
ForwardedParametersInput,
|
|
60
|
+
} from "@copilotkit/runtime-client-gql";
|
|
61
|
+
import { FrontendAction, processActionsForRuntimeRequest } from "../types/frontend-action";
|
|
62
|
+
import { CopilotContextParams } from "../context";
|
|
63
|
+
import { defaultCopilotContextCategories } from "../components";
|
|
64
|
+
|
|
65
|
+
export interface CopilotTaskConfig {
|
|
66
|
+
/**
|
|
67
|
+
* The instructions to be given to the assistant.
|
|
68
|
+
*/
|
|
69
|
+
instructions: string;
|
|
70
|
+
/**
|
|
71
|
+
* An array of action definitions that can be called.
|
|
72
|
+
*/
|
|
73
|
+
actions?: FrontendAction<any>[];
|
|
74
|
+
/**
|
|
75
|
+
* Whether to include the copilot readable context in the task.
|
|
76
|
+
*/
|
|
77
|
+
includeCopilotReadable?: boolean;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Whether to include actions defined via useCopilotAction in the task.
|
|
81
|
+
*/
|
|
82
|
+
includeCopilotActions?: boolean;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The forwarded parameters to use for the task.
|
|
86
|
+
*/
|
|
87
|
+
forwardedParameters?: ForwardedParametersInput;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export class CopilotTask<T = any> {
|
|
91
|
+
private instructions: string;
|
|
92
|
+
private actions: FrontendAction<any>[];
|
|
93
|
+
private includeCopilotReadable: boolean;
|
|
94
|
+
private includeCopilotActions: boolean;
|
|
95
|
+
private forwardedParameters?: ForwardedParametersInput;
|
|
96
|
+
constructor(config: CopilotTaskConfig) {
|
|
97
|
+
this.instructions = config.instructions;
|
|
98
|
+
this.actions = config.actions || [];
|
|
99
|
+
this.includeCopilotReadable = config.includeCopilotReadable !== false;
|
|
100
|
+
this.includeCopilotActions = config.includeCopilotActions !== false;
|
|
101
|
+
this.forwardedParameters = config.forwardedParameters;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Run the task.
|
|
106
|
+
* @param context The CopilotContext to use for the task. Use `useCopilotContext` to obtain the current context.
|
|
107
|
+
* @param data The data to use for the task.
|
|
108
|
+
*/
|
|
109
|
+
async run(context: CopilotContextParams, data?: T): Promise<void> {
|
|
110
|
+
const actions = this.includeCopilotActions ? Object.assign({}, context.actions) : {};
|
|
111
|
+
|
|
112
|
+
// merge functions into entry points
|
|
113
|
+
for (const fn of this.actions) {
|
|
114
|
+
actions[fn.name] = fn;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let contextString = "";
|
|
118
|
+
|
|
119
|
+
if (data) {
|
|
120
|
+
contextString = (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (this.includeCopilotReadable) {
|
|
124
|
+
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const systemMessage = new TextMessage({
|
|
128
|
+
content: taskSystemMessage(contextString, this.instructions),
|
|
129
|
+
role: Role.System,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const messages: Message[] = [systemMessage];
|
|
133
|
+
|
|
134
|
+
const runtimeClient = new CopilotRuntimeClient({
|
|
135
|
+
url: context.copilotApiConfig.chatApiEndpoint,
|
|
136
|
+
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
137
|
+
headers: context.copilotApiConfig.headers,
|
|
138
|
+
credentials: context.copilotApiConfig.credentials,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const response = await runtimeClient
|
|
142
|
+
.generateCopilotResponse({
|
|
143
|
+
data: {
|
|
144
|
+
frontend: {
|
|
145
|
+
actions: processActionsForRuntimeRequest(Object.values(actions)),
|
|
146
|
+
url: window.location.href,
|
|
147
|
+
},
|
|
148
|
+
messages: convertMessagesToGqlInput(filterAgentStateMessages(messages)),
|
|
149
|
+
metadata: {
|
|
150
|
+
requestType: CopilotRequestType.Task,
|
|
151
|
+
},
|
|
152
|
+
forwardedParameters: {
|
|
153
|
+
// if forwardedParameters is provided, use it
|
|
154
|
+
...(this.forwardedParameters ?? {}),
|
|
155
|
+
toolChoice: "required",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
properties: context.copilotApiConfig.properties,
|
|
159
|
+
})
|
|
160
|
+
.toPromise();
|
|
161
|
+
|
|
162
|
+
const functionCallHandler = context.getFunctionCallHandler(actions);
|
|
163
|
+
const functionCalls = convertGqlOutputToMessages(
|
|
164
|
+
response.data?.generateCopilotResponse?.messages || [],
|
|
165
|
+
).filter((m): m is ActionExecutionMessage => m.isActionExecutionMessage());
|
|
166
|
+
|
|
167
|
+
for (const functionCall of functionCalls) {
|
|
168
|
+
await functionCallHandler({
|
|
169
|
+
messages,
|
|
170
|
+
name: functionCall.name,
|
|
171
|
+
args: functionCall.arguments,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function taskSystemMessage(contextString: string, instructions: string): string {
|
|
178
|
+
return `
|
|
179
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
180
|
+
|
|
181
|
+
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.
|
|
182
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
183
|
+
|
|
184
|
+
The user has provided you with the following context:
|
|
185
|
+
\`\`\`
|
|
186
|
+
${contextString}
|
|
187
|
+
\`\`\`
|
|
188
|
+
|
|
189
|
+
They have also provided you with functions you can call to initiate actions on their behalf.
|
|
190
|
+
|
|
191
|
+
Please assist them as best you can.
|
|
192
|
+
|
|
193
|
+
This is not a conversation, so please do not ask questions. Just call a function without saying anything else.
|
|
194
|
+
|
|
195
|
+
The user has given you the following task to complete:
|
|
196
|
+
|
|
197
|
+
\`\`\`
|
|
198
|
+
${instructions}
|
|
199
|
+
\`\`\`
|
|
200
|
+
`;
|
|
201
|
+
}
|
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./copilot-task";
|