@copilotkit/react-core 0.13.0 → 0.15.0-alpha.0
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/.turbo/turbo-build.log +141 -88
- package/CHANGELOG.md +17 -0
- package/dist/components/copilot-provider/copilot-provider-props.d.ts +50 -0
- package/dist/components/copilot-provider/copilot-provider-props.mjs +2 -0
- package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-provider.d.ts +45 -0
- package/dist/components/copilot-provider/copilot-provider.mjs +469 -0
- package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -0
- package/dist/components/copilot-provider/index.d.ts +9 -0
- package/dist/components/copilot-provider/index.mjs +468 -0
- package/dist/components/copilot-provider/index.mjs.map +1 -0
- package/dist/components/copilot-provider/standard-cpilot-api-config.d.ts +24 -0
- package/dist/components/copilot-provider/standard-cpilot-api-config.mjs +13 -0
- package/dist/components/copilot-provider/standard-cpilot-api-config.mjs.map +1 -0
- package/dist/components/index.d.ts +8 -1
- package/dist/components/index.mjs +467 -7
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +5 -0
- package/dist/context/copilot-context.mjs +81 -3
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.mjs +80 -4
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.mjs +249 -12
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +194 -8
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +68 -3
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +95 -4
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +87 -4
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +87 -4
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +153 -3
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.mjs +755 -14
- package/dist/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/index.d.ts +2 -0
- package/dist/openai-assistants/hooks/index.mjs +246 -0
- package/dist/openai-assistants/hooks/index.mjs.map +1 -0
- package/dist/openai-assistants/hooks/use-assistants.d.ts +17 -0
- package/dist/openai-assistants/hooks/use-assistants.mjs +131 -0
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +44 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +247 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -0
- package/dist/openai-assistants/index.d.ts +3 -0
- package/dist/openai-assistants/index.mjs +247 -0
- package/dist/openai-assistants/index.mjs.map +1 -0
- package/dist/openai-assistants/utils/index.d.ts +1 -0
- package/dist/openai-assistants/utils/index.mjs +47 -0
- package/dist/openai-assistants/utils/index.mjs.map +1 -0
- package/dist/openai-assistants/utils/process-message-stream.d.ts +3 -0
- package/dist/openai-assistants/utils/process-message-stream.mjs +47 -0
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -0
- package/dist/types/annotated-function.mjs +0 -2
- package/dist/types/annotated-function.mjs.map +1 -1
- package/dist/types/document-pointer.mjs +0 -2
- package/dist/types/document-pointer.mjs.map +1 -1
- package/dist/types/index.mjs +0 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.mjs +0 -2
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/utils.test.d.ts +1 -0
- package/dist/utils/utils.test.mjs +0 -1
- package/dist/utils/utils.test.mjs.map +1 -1
- package/package.json +7 -6
- package/src/components/copilot-provider/copilot-provider-props.tsx +50 -0
- package/src/components/{copilot-provider.tsx → copilot-provider/copilot-provider.tsx} +84 -63
- package/src/components/copilot-provider/index.ts +7 -0
- package/src/components/copilot-provider/standard-cpilot-api-config.tsx +28 -0
- package/src/components/index.ts +1 -4
- package/src/context/copilot-context.tsx +19 -17
- package/src/hooks/use-copilot-chat.ts +17 -22
- package/src/hooks/use-flat-category-store.ts +15 -25
- package/src/hooks/use-make-copilot-actionable.ts +3 -6
- package/src/hooks/use-make-copilot-document-readable.ts +2 -3
- package/src/hooks/use-make-copilot-readable.ts +1 -1
- package/src/hooks/use-tree.ts +10 -28
- package/src/index.tsx +1 -0
- package/src/openai-assistants/hooks/index.ts +9 -0
- package/src/openai-assistants/hooks/use-assistants.ts +114 -0
- package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +186 -0
- package/src/openai-assistants/index.ts +2 -0
- package/src/openai-assistants/utils/index.ts +1 -0
- package/src/openai-assistants/utils/process-message-stream.ts +25 -0
- package/dist/chunk-3BOHSSKR.mjs +0 -141
- package/dist/chunk-3BOHSSKR.mjs.map +0 -1
- package/dist/chunk-63ILXW5V.mjs +0 -76
- package/dist/chunk-63ILXW5V.mjs.map +0 -1
- package/dist/chunk-AMFRKVFT.mjs +0 -19
- package/dist/chunk-AMFRKVFT.mjs.map +0 -1
- package/dist/chunk-EFZPSZWO.mjs +0 -3
- package/dist/chunk-EFZPSZWO.mjs.map +0 -1
- package/dist/chunk-FIYUJVWU.mjs +0 -161
- package/dist/chunk-FIYUJVWU.mjs.map +0 -1
- package/dist/chunk-G5LZS72K.mjs +0 -19
- package/dist/chunk-G5LZS72K.mjs.map +0 -1
- package/dist/chunk-JD7BAH7U.mjs +0 -3
- package/dist/chunk-JD7BAH7U.mjs.map +0 -1
- package/dist/chunk-MRXNTQOX.mjs +0 -55
- package/dist/chunk-MRXNTQOX.mjs.map +0 -1
- package/dist/chunk-P3QUZTN7.mjs +0 -30
- package/dist/chunk-P3QUZTN7.mjs.map +0 -1
- package/dist/chunk-PRPQRCVY.mjs +0 -82
- package/dist/chunk-PRPQRCVY.mjs.map +0 -1
- package/dist/chunk-QACD2U6P.mjs +0 -3
- package/dist/chunk-QACD2U6P.mjs.map +0 -1
- package/dist/chunk-X24EEZGG.mjs +0 -50
- package/dist/chunk-X24EEZGG.mjs.map +0 -1
- package/dist/chunk-YPSGKPDA.mjs +0 -3
- package/dist/chunk-YPSGKPDA.mjs.map +0 -1
- package/dist/components/copilot-provider.d.ts +0 -11
- package/dist/components/copilot-provider.mjs +0 -7
- package/dist/components/copilot-provider.mjs.map +0 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/openai-assistants/utils/process-message-stream.ts
|
|
23
|
+
function processMessageStream(reader, processMessage) {
|
|
24
|
+
return __async(this, null, function* () {
|
|
25
|
+
const decoder = new TextDecoder();
|
|
26
|
+
let buffer = "";
|
|
27
|
+
while (true) {
|
|
28
|
+
const { done, value } = yield reader.read();
|
|
29
|
+
if (done) {
|
|
30
|
+
if (buffer.length > 0) {
|
|
31
|
+
processMessage(buffer);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
buffer += decoder.decode(value, { stream: true });
|
|
36
|
+
let endIndex;
|
|
37
|
+
while ((endIndex = buffer.indexOf("\n")) !== -1) {
|
|
38
|
+
processMessage(buffer.substring(0, endIndex).trim());
|
|
39
|
+
buffer = buffer.substring(endIndex + 1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
processMessageStream
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=process-message-stream.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/openai-assistants/utils/process-message-stream.ts"],"sourcesContent":["export async function processMessageStream(\n reader: ReadableStreamDefaultReader<Uint8Array>,\n processMessage: (message: string) => void | Promise<void>,\n) {\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n if (buffer.length > 0) {\n processMessage(buffer);\n }\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n let endIndex: number;\n while ((endIndex = buffer.indexOf(\"\\n\")) !== -1) {\n processMessage(buffer.substring(0, endIndex).trim());\n buffer = buffer.substring(endIndex + 1); // Remove the processed instruction + delimiter\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAsB,qBACpB,QACA,gBACA;AAAA;AACA,UAAM,UAAU,IAAI,YAAY;AAChC,QAAI,SAAS;AACb,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,UAAI,MAAM;AACR,YAAI,OAAO,SAAS,GAAG;AACrB,yBAAe,MAAM;AAAA,QACvB;AACA;AAAA,MACF;AAEA,gBAAU,QAAQ,OAAO,OAAO,EAAE,QAAQ,KAAK,CAAC;AAEhD,UAAI;AACJ,cAAQ,WAAW,OAAO,QAAQ,IAAI,OAAO,IAAI;AAC/C,uBAAe,OAAO,UAAU,GAAG,QAAQ,EAAE,KAAK,CAAC;AACnD,iBAAS,OAAO,UAAU,WAAW,CAAC;AAAA,MACxC;AAAA,IACF;AAAA,EACF;AAAA;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/types/index.mjs
CHANGED
package/dist/types/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/utils/utils.d.ts
CHANGED
package/dist/utils/utils.mjs
CHANGED
package/dist/utils/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/utils.test.ts"],"
|
|
1
|
+
{"version":3,"sources":["../../src/utils/utils.test.ts"],"sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"],"mappings":";AAEA,SAAS,aAAa,MAAM;AAC1B,KAAG,oBAAoB,MAAM;AAC3B,WAAO,IAAI,EAAE,WAAW;AAAA,EAC1B,CAAC;AACH,CAAC;","names":[]}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.15.0-alpha.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
@@ -21,15 +21,16 @@
|
|
|
21
21
|
"jest": "^29.6.4",
|
|
22
22
|
"react": "^18.2.0",
|
|
23
23
|
"ts-jest": "^29.1.1",
|
|
24
|
-
"tsup": "^6.
|
|
25
|
-
"typescript": "^
|
|
24
|
+
"tsup": "^6.7.0",
|
|
25
|
+
"typescript": "^5.1.3",
|
|
26
26
|
"eslint-config-custom": "0.2.0",
|
|
27
|
-
"tsconfig": "0.
|
|
27
|
+
"tsconfig": "0.6.0-alpha.0"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"ai": "^2.2.
|
|
30
|
+
"ai": "^2.2.23",
|
|
31
31
|
"nanoid": "^4.0.2",
|
|
32
|
-
"openai": "^4.6.0"
|
|
32
|
+
"openai": "^4.6.0",
|
|
33
|
+
"@copilotkit/shared": "0.1.0-alpha.0"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
36
|
"build": "tsup --treeshake",
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
import { CopilotApiConfig } from "../../context/copilot-context";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Props for the CopilotProvider when using a chat API endpoint.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface CopilotProviderApiEndpointProps {
|
|
10
|
+
/**
|
|
11
|
+
* The endpoint for the chat API.
|
|
12
|
+
*/
|
|
13
|
+
chatApiEndpoint: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The endpoint for the chat API v2.
|
|
17
|
+
* If not provided, defaults to chatApiEndpoint + "/v2".
|
|
18
|
+
* This is used for the chat API v2.
|
|
19
|
+
* If you are not using the chat API v2, you can ignore this.
|
|
20
|
+
* @default chatApiEndpoint + "/v2"
|
|
21
|
+
* @optional
|
|
22
|
+
*/
|
|
23
|
+
chatApiEndpointV2?: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The children to be rendered within the CopilotProvider.
|
|
27
|
+
*/
|
|
28
|
+
children: ReactNode;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Props for the CopilotProvider when using a CopilotApiConfig.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export interface CopilotProviderApiConfigProps {
|
|
35
|
+
/**
|
|
36
|
+
* The configuration for the Copilot API.
|
|
37
|
+
*/
|
|
38
|
+
chatApiConfig: CopilotApiConfig;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The children to be rendered within the CopilotProvider.
|
|
42
|
+
*/
|
|
43
|
+
children: ReactNode;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Props for the CopilotProvider component.
|
|
47
|
+
* Can be either CopilotProviderApiEndpointProps or CopilotProviderApiConfigProps.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
export type CopilotProviderProps = CopilotProviderApiEndpointProps | CopilotProviderApiConfigProps;
|
|
@@ -1,27 +1,53 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
|
|
2
3
|
import { FunctionCallHandler } from "ai";
|
|
3
|
-
import {
|
|
4
|
-
import { CopilotContext, CopilotApiConfig } from "
|
|
5
|
-
import useTree from "
|
|
6
|
-
import { AnnotatedFunction } from "
|
|
4
|
+
import { useCallback, useState } from "react";
|
|
5
|
+
import { CopilotContext, CopilotApiConfig } from "../../context/copilot-context";
|
|
6
|
+
import useTree from "../../hooks/use-tree";
|
|
7
|
+
import { AnnotatedFunction } from "../../types/annotated-function";
|
|
7
8
|
import { ChatCompletionCreateParams } from "openai/resources/chat";
|
|
8
|
-
import { DocumentPointer } from "
|
|
9
|
-
import useFlatCategoryStore from "
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
import { DocumentPointer } from "../../types";
|
|
10
|
+
import useFlatCategoryStore from "../../hooks/use-flat-category-store";
|
|
11
|
+
import { StandardCopilotApiConfig } from "./standard-cpilot-api-config";
|
|
12
|
+
import { CopilotProviderProps } from "./copilot-provider-props";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The CopilotProvider component.
|
|
16
|
+
* This component provides the Copilot context to its children.
|
|
17
|
+
* It can be configured either with a chat API endpoint or a CopilotApiConfig.
|
|
18
|
+
*
|
|
19
|
+
* Example usage:
|
|
20
|
+
* ```
|
|
21
|
+
* <CopilotProvider chatApiEndpoint="https://api.copilot.chat">
|
|
22
|
+
* <App />
|
|
23
|
+
* </CopilotProvider>
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* or
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* const copilotApiConfig = new StandardCopilotApiConfig(
|
|
30
|
+
* "https://api.copilot.chat",
|
|
31
|
+
* "https://api.copilot.chat/v2",
|
|
32
|
+
* {},
|
|
33
|
+
* {}
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* // ...
|
|
37
|
+
*
|
|
38
|
+
* <CopilotProvider chatApiConfig={copilotApiConfig}>
|
|
39
|
+
* <App />
|
|
40
|
+
* </CopilotProvider>
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param props - The props for the component.
|
|
44
|
+
* @returns The CopilotProvider component.
|
|
45
|
+
*/
|
|
46
|
+
export function CopilotProvider({ children, ...props }: CopilotProviderProps): JSX.Element {
|
|
47
|
+
// Compute all the functions and properties that we need to pass
|
|
48
|
+
// to the CopilotContext.
|
|
49
|
+
|
|
50
|
+
const [entryPoints, setEntryPoints] = useState<Record<string, AnnotatedFunction<any[]>>>({});
|
|
25
51
|
|
|
26
52
|
const { addElement, removeElement, printTree } = useTree();
|
|
27
53
|
const {
|
|
@@ -30,17 +56,14 @@ export function CopilotProvider({
|
|
|
30
56
|
allElements: allDocuments,
|
|
31
57
|
} = useFlatCategoryStore<DocumentPointer>();
|
|
32
58
|
|
|
33
|
-
const setEntryPoint = useCallback(
|
|
34
|
-
(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
42
|
-
[]
|
|
43
|
-
);
|
|
59
|
+
const setEntryPoint = useCallback((id: string, entryPoint: AnnotatedFunction<any[]>) => {
|
|
60
|
+
setEntryPoints((prevPoints) => {
|
|
61
|
+
return {
|
|
62
|
+
...prevPoints,
|
|
63
|
+
[id]: entryPoint,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}, []);
|
|
44
67
|
|
|
45
68
|
const removeEntryPoint = useCallback((id: string) => {
|
|
46
69
|
setEntryPoints((prevPoints) => {
|
|
@@ -54,9 +77,7 @@ export function CopilotProvider({
|
|
|
54
77
|
(documents: DocumentPointer[], categories: string[]) => {
|
|
55
78
|
const documentsString = documents
|
|
56
79
|
.map((document) => {
|
|
57
|
-
return `${document.name} (${
|
|
58
|
-
document.sourceApplication
|
|
59
|
-
}):\n${document.getContents()}`;
|
|
80
|
+
return `${document.name} (${document.sourceApplication}):\n${document.getContents()}`;
|
|
60
81
|
})
|
|
61
82
|
.join("\n\n");
|
|
62
83
|
|
|
@@ -64,25 +85,25 @@ export function CopilotProvider({
|
|
|
64
85
|
|
|
65
86
|
return `${documentsString}\n\n${nonDocumentStrings}`;
|
|
66
87
|
},
|
|
67
|
-
[printTree]
|
|
88
|
+
[printTree],
|
|
68
89
|
);
|
|
69
90
|
|
|
70
91
|
const addContext = useCallback(
|
|
71
92
|
(
|
|
72
93
|
context: string,
|
|
73
94
|
parentId?: string,
|
|
74
|
-
categories: string[] = defaultCopilotContextCategories
|
|
95
|
+
categories: string[] = defaultCopilotContextCategories,
|
|
75
96
|
) => {
|
|
76
97
|
return addElement(context, categories, parentId);
|
|
77
98
|
},
|
|
78
|
-
[addElement]
|
|
99
|
+
[addElement],
|
|
79
100
|
);
|
|
80
101
|
|
|
81
102
|
const removeContext = useCallback(
|
|
82
103
|
(id: string) => {
|
|
83
104
|
removeElement(id);
|
|
84
105
|
},
|
|
85
|
-
[removeElement]
|
|
106
|
+
[removeElement],
|
|
86
107
|
);
|
|
87
108
|
|
|
88
109
|
const getChatCompletionFunctionDescriptions = useCallback(() => {
|
|
@@ -97,26 +118,36 @@ export function CopilotProvider({
|
|
|
97
118
|
(categories: string[]) => {
|
|
98
119
|
return allDocuments(categories);
|
|
99
120
|
},
|
|
100
|
-
[allDocuments]
|
|
121
|
+
[allDocuments],
|
|
101
122
|
);
|
|
102
123
|
|
|
103
124
|
const addDocumentContext = useCallback(
|
|
104
|
-
(
|
|
105
|
-
documentPointer: DocumentPointer,
|
|
106
|
-
categories: string[] = defaultCopilotContextCategories
|
|
107
|
-
) => {
|
|
125
|
+
(documentPointer: DocumentPointer, categories: string[] = defaultCopilotContextCategories) => {
|
|
108
126
|
return addDocument(documentPointer, categories);
|
|
109
127
|
},
|
|
110
|
-
[addDocument]
|
|
128
|
+
[addDocument],
|
|
111
129
|
);
|
|
112
130
|
|
|
113
131
|
const removeDocumentContext = useCallback(
|
|
114
132
|
(documentId: string) => {
|
|
115
133
|
removeDocument(documentId);
|
|
116
134
|
},
|
|
117
|
-
[removeDocument]
|
|
135
|
+
[removeDocument],
|
|
118
136
|
);
|
|
119
137
|
|
|
138
|
+
// get the appropriate CopilotApiConfig from the props
|
|
139
|
+
let copilotApiConfig: CopilotApiConfig;
|
|
140
|
+
if ("chatApiEndpoint" in props) {
|
|
141
|
+
copilotApiConfig = new StandardCopilotApiConfig(
|
|
142
|
+
props.chatApiEndpoint,
|
|
143
|
+
props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,
|
|
144
|
+
{},
|
|
145
|
+
{},
|
|
146
|
+
);
|
|
147
|
+
} else {
|
|
148
|
+
copilotApiConfig = props.chatApiConfig;
|
|
149
|
+
}
|
|
150
|
+
|
|
120
151
|
return (
|
|
121
152
|
<CopilotContext.Provider
|
|
122
153
|
value={{
|
|
@@ -131,11 +162,7 @@ export function CopilotProvider({
|
|
|
131
162
|
getDocumentsContext,
|
|
132
163
|
addDocumentContext,
|
|
133
164
|
removeDocumentContext,
|
|
134
|
-
copilotApiConfig:
|
|
135
|
-
chatApiEndpoint,
|
|
136
|
-
headers: headers || {},
|
|
137
|
-
body: body || {},
|
|
138
|
-
},
|
|
165
|
+
copilotApiConfig: copilotApiConfig,
|
|
139
166
|
}}
|
|
140
167
|
>
|
|
141
168
|
{children}
|
|
@@ -146,19 +173,15 @@ export function CopilotProvider({
|
|
|
146
173
|
export const defaultCopilotContextCategories = ["global"];
|
|
147
174
|
|
|
148
175
|
function entryPointsToFunctionCallHandler(
|
|
149
|
-
entryPoints: AnnotatedFunction<any[]>[]
|
|
176
|
+
entryPoints: AnnotatedFunction<any[]>[],
|
|
150
177
|
): FunctionCallHandler {
|
|
151
178
|
return async (chatMessages, functionCall) => {
|
|
152
|
-
let entrypointsByFunctionName: Record<
|
|
153
|
-
string,
|
|
154
|
-
AnnotatedFunction<any[]>
|
|
155
|
-
> = {};
|
|
179
|
+
let entrypointsByFunctionName: Record<string, AnnotatedFunction<any[]>> = {};
|
|
156
180
|
for (let entryPoint of entryPoints) {
|
|
157
181
|
entrypointsByFunctionName[entryPoint.name] = entryPoint;
|
|
158
182
|
}
|
|
159
183
|
|
|
160
|
-
const entryPointFunction =
|
|
161
|
-
entrypointsByFunctionName[functionCall.name || ""];
|
|
184
|
+
const entryPointFunction = entrypointsByFunctionName[functionCall.name || ""];
|
|
162
185
|
if (entryPointFunction) {
|
|
163
186
|
let parsedFunctionCallArguments: Record<string, any>[] = [];
|
|
164
187
|
if (functionCall.arguments) {
|
|
@@ -168,9 +191,7 @@ function entryPointsToFunctionCallHandler(
|
|
|
168
191
|
const paramsInCorrectOrder: any[] = [];
|
|
169
192
|
for (let arg of entryPointFunction.argumentAnnotations) {
|
|
170
193
|
paramsInCorrectOrder.push(
|
|
171
|
-
parsedFunctionCallArguments[
|
|
172
|
-
arg.name as keyof typeof parsedFunctionCallArguments
|
|
173
|
-
]
|
|
194
|
+
parsedFunctionCallArguments[arg.name as keyof typeof parsedFunctionCallArguments],
|
|
174
195
|
);
|
|
175
196
|
}
|
|
176
197
|
|
|
@@ -198,13 +219,13 @@ function entryPointsToFunctionCallHandler(
|
|
|
198
219
|
}
|
|
199
220
|
|
|
200
221
|
function entryPointsToChatCompletionFunctions(
|
|
201
|
-
entryPoints: AnnotatedFunction<any[]>[]
|
|
222
|
+
entryPoints: AnnotatedFunction<any[]>[],
|
|
202
223
|
): ChatCompletionCreateParams.Function[] {
|
|
203
224
|
return entryPoints.map(annotatedFunctionToChatCompletionFunction);
|
|
204
225
|
}
|
|
205
226
|
|
|
206
227
|
function annotatedFunctionToChatCompletionFunction(
|
|
207
|
-
annotatedFunction: AnnotatedFunction<any[]
|
|
228
|
+
annotatedFunction: AnnotatedFunction<any[]>,
|
|
208
229
|
): ChatCompletionCreateParams.Function {
|
|
209
230
|
// Create the parameters object based on the argumentAnnotations
|
|
210
231
|
let parameters: { [key: string]: any } = {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CopilotApiConfig } from "../../context/copilot-context";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A standard implementation of the CopilotApiConfig interface.
|
|
5
|
+
*
|
|
6
|
+
* Pass in the base URL of the chat API, the headers to be sent with each request, and the body to be sent with each request.
|
|
7
|
+
* The rest of the CopilotApiConfig interface is implemented by default.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export class StandardCopilotApiConfig implements CopilotApiConfig {
|
|
12
|
+
chatApiEndpoint: string;
|
|
13
|
+
chatApiEndpointV2: string;
|
|
14
|
+
headers: Record<string, string>;
|
|
15
|
+
body: Record<string, any>;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
chatApiEndpoint: string,
|
|
19
|
+
chatApiEndpointV2: string,
|
|
20
|
+
headers: Record<string, string>,
|
|
21
|
+
body: Record<string, any>,
|
|
22
|
+
) {
|
|
23
|
+
this.chatApiEndpoint = chatApiEndpoint;
|
|
24
|
+
this.chatApiEndpointV2 = chatApiEndpointV2;
|
|
25
|
+
this.headers = headers;
|
|
26
|
+
this.body = body;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -16,6 +16,11 @@ export interface CopilotApiConfig {
|
|
|
16
16
|
*/
|
|
17
17
|
chatApiEndpoint: string;
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* The endpoint for the chat API v2.
|
|
21
|
+
*/
|
|
22
|
+
chatApiEndpointV2: string;
|
|
23
|
+
|
|
19
24
|
/**
|
|
20
25
|
* additional headers to be sent with the request
|
|
21
26
|
* @default {}
|
|
@@ -46,6 +51,9 @@ export function copilotApiConfigExtrapolator(config: CopilotApiConfig) {
|
|
|
46
51
|
get chatApiEndpoint(): string {
|
|
47
52
|
return `${config.chatApiEndpoint}`;
|
|
48
53
|
},
|
|
54
|
+
get chatApiEndpointV2(): string {
|
|
55
|
+
return `${config.chatApiEndpointV2}`;
|
|
56
|
+
},
|
|
49
57
|
};
|
|
50
58
|
}
|
|
51
59
|
|
|
@@ -58,23 +66,13 @@ export interface CopilotContextParams {
|
|
|
58
66
|
getFunctionCallHandler: () => FunctionCallHandler;
|
|
59
67
|
|
|
60
68
|
// text context
|
|
61
|
-
getContextString: (
|
|
62
|
-
|
|
63
|
-
categories: string[]
|
|
64
|
-
) => string;
|
|
65
|
-
addContext: (
|
|
66
|
-
context: string,
|
|
67
|
-
parentId?: string,
|
|
68
|
-
categories?: string[]
|
|
69
|
-
) => TreeNodeId;
|
|
69
|
+
getContextString: (documents: DocumentPointer[], categories: string[]) => string;
|
|
70
|
+
addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;
|
|
70
71
|
removeContext: (id: TreeNodeId) => void;
|
|
71
72
|
|
|
72
73
|
// document context
|
|
73
74
|
getDocumentsContext: (categories: string[]) => DocumentPointer[];
|
|
74
|
-
addDocumentContext: (
|
|
75
|
-
documentPointer: DocumentPointer,
|
|
76
|
-
categories?: string[]
|
|
77
|
-
) => TreeNodeId;
|
|
75
|
+
addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;
|
|
78
76
|
removeDocumentContext: (documentId: string) => void;
|
|
79
77
|
|
|
80
78
|
// api endpoints
|
|
@@ -100,7 +98,12 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
100
98
|
copilotApiConfig: new (class implements CopilotApiConfig {
|
|
101
99
|
get chatApiEndpoint(): string {
|
|
102
100
|
throw new Error(
|
|
103
|
-
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
101
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
get chatApiEndpointV2(): string {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
|
|
104
107
|
);
|
|
105
108
|
}
|
|
106
109
|
get headers(): Record<string, string> {
|
|
@@ -112,12 +115,11 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
112
115
|
})(),
|
|
113
116
|
};
|
|
114
117
|
|
|
115
|
-
export const CopilotContext =
|
|
116
|
-
React.createContext<CopilotContextParams>(emptyCopilotContext);
|
|
118
|
+
export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
|
|
117
119
|
|
|
118
120
|
function returnAndThrowInDebug<T>(value: T): T {
|
|
119
121
|
throw new Error(
|
|
120
|
-
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
122
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
|
|
121
123
|
);
|
|
122
124
|
return value;
|
|
123
125
|
}
|
|
@@ -17,11 +17,9 @@ export interface UseCopilotChatReturn {
|
|
|
17
17
|
visibleMessages: Message[];
|
|
18
18
|
append: (
|
|
19
19
|
message: Message | CreateMessage,
|
|
20
|
-
chatRequestOptions?: ChatRequestOptions
|
|
21
|
-
) => Promise<string | null | undefined>;
|
|
22
|
-
reload: (
|
|
23
|
-
chatRequestOptions?: ChatRequestOptions
|
|
20
|
+
chatRequestOptions?: ChatRequestOptions,
|
|
24
21
|
) => Promise<string | null | undefined>;
|
|
22
|
+
reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
|
|
25
23
|
stop: () => void;
|
|
26
24
|
isLoading: boolean;
|
|
27
25
|
input: string;
|
|
@@ -50,32 +48,29 @@ export function useCopilotChat({
|
|
|
50
48
|
};
|
|
51
49
|
}, [getContextString, makeSystemMessage]);
|
|
52
50
|
|
|
53
|
-
const initialMessagesWithContext = [systemMessage].concat(
|
|
54
|
-
options.initialMessages || []
|
|
55
|
-
);
|
|
51
|
+
const initialMessagesWithContext = [systemMessage].concat(options.initialMessages || []);
|
|
56
52
|
|
|
57
53
|
const functionDescriptions = useMemo(() => {
|
|
58
54
|
return getChatCompletionFunctionDescriptions();
|
|
59
55
|
}, [getChatCompletionFunctionDescriptions]);
|
|
60
56
|
|
|
61
|
-
const { messages, append, reload, stop, isLoading, input, setInput } =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
const { messages, append, reload, stop, isLoading, input, setInput } = useChat({
|
|
58
|
+
...options,
|
|
59
|
+
api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
|
|
60
|
+
id: options.id,
|
|
61
|
+
initialMessages: initialMessagesWithContext,
|
|
62
|
+
experimental_onFunctionCall: getFunctionCallHandler(),
|
|
63
|
+
headers: { ...copilotApiConfig.headers, ...options.headers },
|
|
64
|
+
body: {
|
|
65
65
|
id: options.id,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
functions: functionDescriptions,
|
|
72
|
-
...copilotApiConfig.body,
|
|
73
|
-
...options.body,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
66
|
+
functions: functionDescriptions,
|
|
67
|
+
...copilotApiConfig.body,
|
|
68
|
+
...options.body,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
76
71
|
|
|
77
72
|
const visibleMessages = messages.filter(
|
|
78
|
-
(message) => message.role === "user" || message.role === "assistant"
|
|
73
|
+
(message) => message.role === "user" || message.role === "assistant",
|
|
79
74
|
);
|
|
80
75
|
|
|
81
76
|
return {
|