@copilotkit/react-core 0.18.0-alpha.1 → 0.18.0-alpha.3
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 +184 -122
- package/CHANGELOG.md +25 -1
- package/dist/{chunk-6A4PCNMZ.mjs → chunk-4CFJ6A6A.mjs} +4 -10
- package/dist/chunk-4CFJ6A6A.mjs.map +1 -0
- package/dist/{chunk-FY7XNRJP.mjs → chunk-5GS7FQPW.mjs} +4 -4
- package/dist/chunk-5GS7FQPW.mjs.map +1 -0
- package/dist/{chunk-FP4EXCGS.mjs → chunk-DGUVNY3V.mjs} +2 -2
- package/dist/chunk-DGUVNY3V.mjs.map +1 -0
- package/dist/{chunk-D4NQCVHE.mjs → chunk-GBZZPQ5X.mjs} +11 -38
- package/dist/chunk-GBZZPQ5X.mjs.map +1 -0
- package/dist/chunk-IQEWY7P4.mjs +102 -0
- package/dist/chunk-IQEWY7P4.mjs.map +1 -0
- package/dist/{chunk-7P4OYNP3.mjs → chunk-IQH5AQQC.mjs} +2 -2
- package/dist/chunk-UAKLDQMP.mjs +61 -0
- package/dist/chunk-UAKLDQMP.mjs.map +1 -0
- package/dist/{chunk-P34OLW2N.mjs → chunk-VP2EU4US.mjs} +3 -3
- package/dist/{chunk-HW5IH6PV.mjs → chunk-YOXWT7I6.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +39 -0
- package/dist/components/copilot-provider/copilotkit-props.js +4 -0
- package/dist/components/copilot-provider/copilotkit-props.mjs +3 -0
- package/dist/components/copilot-provider/{copilot-provider.d.ts → copilotkit.d.ts} +9 -15
- package/dist/components/copilot-provider/copilotkit.js +426 -0
- package/dist/components/copilot-provider/copilotkit.js.map +1 -0
- package/dist/components/copilot-provider/copilotkit.mjs +8 -0
- package/dist/components/copilot-provider/index.d.ts +2 -8
- package/dist/components/copilot-provider/index.js +426 -0
- package/dist/components/copilot-provider/index.js.map +1 -0
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +2 -4
- package/dist/components/copilot-provider/standard-copilot-api-config.js +15 -0
- package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
- package/dist/components/index.d.ts +2 -8
- package/dist/components/index.js +426 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +3 -5
- package/dist/context/copilot-context.js +47 -0
- package/dist/context/copilot-context.js.map +1 -0
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +2 -4
- package/dist/context/index.js +47 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -4
- package/dist/hooks/index.js +358 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +8 -9
- package/dist/hooks/use-chat.d.ts +2 -4
- package/dist/hooks/use-chat.js +186 -0
- package/dist/hooks/use-chat.js.map +1 -0
- package/dist/hooks/use-chat.mjs +2 -3
- package/dist/hooks/use-copilot-chat.d.ts +2 -4
- package/dist/hooks/use-copilot-chat.js +313 -0
- package/dist/hooks/use-copilot-chat.js.map +1 -0
- package/dist/hooks/use-copilot-chat.mjs +5 -6
- package/dist/hooks/use-flat-category-store.js +72 -0
- package/dist/hooks/use-flat-category-store.js.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.d.ts +1 -1
- package/dist/hooks/use-make-copilot-actionable.js +67 -0
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +61 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-readable.js +61 -0
- package/dist/hooks/use-make-copilot-readable.js.map +1 -0
- package/dist/hooks/use-make-copilot-readable.mjs +2 -2
- package/dist/hooks/use-tree.js +155 -0
- package/dist/hooks/use-tree.js.map +1 -0
- package/dist/index.d.ts +4 -9
- package/dist/index.js +838 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +10 -13
- package/dist/openai-assistants/hooks/index.js +225 -0
- package/dist/openai-assistants/hooks/index.js.map +1 -0
- package/dist/openai-assistants/hooks/index.mjs +3 -3
- package/dist/openai-assistants/hooks/use-assistants.js +129 -0
- package/dist/openai-assistants/hooks/use-assistants.js.map +1 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +226 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +3 -3
- package/dist/openai-assistants/index.js +226 -0
- package/dist/openai-assistants/index.js.map +1 -0
- package/dist/openai-assistants/index.mjs +3 -3
- package/dist/openai-assistants/utils/index.js +49 -0
- package/dist/openai-assistants/utils/index.js.map +1 -0
- package/dist/openai-assistants/utils/process-message-stream.js +49 -0
- package/dist/openai-assistants/utils/process-message-stream.js.map +1 -0
- package/dist/types/document-pointer.js +4 -0
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.js +4 -0
- package/dist/types/system-message.js +4 -0
- package/dist/utils/fetch-chat-completion.d.ts +28 -0
- package/dist/utils/fetch-chat-completion.js +103 -0
- package/dist/utils/fetch-chat-completion.js.map +1 -0
- package/dist/utils/fetch-chat-completion.mjs +4 -0
- package/dist/utils/utils.js +4 -0
- package/dist/utils/utils.test.js +10 -0
- package/dist/utils/utils.test.js.map +1 -0
- package/package.json +10 -10
- package/src/components/copilot-provider/copilotkit-props.tsx +42 -0
- package/src/components/copilot-provider/{copilot-provider.tsx → copilotkit.tsx} +22 -57
- package/src/components/copilot-provider/index.ts +2 -6
- package/src/context/copilot-context.tsx +5 -13
- package/src/hooks/use-chat.ts +54 -72
- package/src/hooks/use-copilot-chat.ts +4 -4
- package/src/hooks/use-make-copilot-actionable.ts +1 -1
- package/src/index.tsx +6 -1
- package/src/types/index.ts +0 -3
- package/src/utils/fetch-chat-completion.ts +95 -0
- package/tsup.config.ts +1 -2
- package/dist/chunk-6A4PCNMZ.mjs.map +0 -1
- package/dist/chunk-6SOQYBNX.mjs +0 -44
- package/dist/chunk-6SOQYBNX.mjs.map +0 -1
- package/dist/chunk-D4NQCVHE.mjs.map +0 -1
- package/dist/chunk-FP4EXCGS.mjs.map +0 -1
- package/dist/chunk-FY7XNRJP.mjs.map +0 -1
- package/dist/chunk-LMZN4NSM.mjs +0 -113
- package/dist/chunk-LMZN4NSM.mjs.map +0 -1
- package/dist/chunk-PUFR5OAI.mjs +0 -3
- package/dist/chunk-TKXPMMND.mjs +0 -177
- package/dist/chunk-TKXPMMND.mjs.map +0 -1
- package/dist/chunk-VK25FIML.mjs +0 -144
- package/dist/chunk-VK25FIML.mjs.map +0 -1
- package/dist/components/copilot-provider/copilot-provider-props.d.ts +0 -50
- package/dist/components/copilot-provider/copilot-provider-props.mjs +0 -3
- package/dist/components/copilot-provider/copilot-provider.mjs +0 -8
- package/dist/openai/chat-completion-client.d.ts +0 -52
- package/dist/openai/chat-completion-client.mjs +0 -5
- package/dist/openai/chat-completion-stream.d.ts +0 -21
- package/dist/openai/chat-completion-stream.mjs +0 -5
- package/dist/openai/chat-completion-transport.d.ts +0 -38
- package/dist/openai/chat-completion-transport.mjs +0 -4
- package/dist/openai/index.d.ts +0 -10
- package/dist/openai/index.mjs +0 -6
- package/dist/types/annotated-function.d.ts +0 -24
- package/dist/types/annotated-function.mjs +0 -3
- package/dist/types/base.d.ts +0 -56
- package/dist/types/base.mjs +0 -3
- package/dist/types/base.mjs.map +0 -1
- package/src/components/copilot-provider/copilot-provider-props.tsx +0 -50
- package/src/openai/chat-completion-client.ts +0 -229
- package/src/openai/chat-completion-stream.ts +0 -54
- package/src/openai/chat-completion-transport.ts +0 -186
- package/src/openai/index.tsx +0 -5
- package/src/types/annotated-function.ts +0 -27
- package/src/types/base.ts +0 -61
- /package/dist/{chunk-7P4OYNP3.mjs.map → chunk-IQH5AQQC.mjs.map} +0 -0
- /package/dist/{chunk-P34OLW2N.mjs.map → chunk-VP2EU4US.mjs.map} +0 -0
- /package/dist/{chunk-HW5IH6PV.mjs.map → chunk-YOXWT7I6.mjs.map} +0 -0
- /package/dist/{chunk-PUFR5OAI.mjs.map → components/copilot-provider/copilotkit-props.js.map} +0 -0
- /package/dist/components/copilot-provider/{copilot-provider-props.mjs.map → copilotkit-props.mjs.map} +0 -0
- /package/dist/components/copilot-provider/{copilot-provider.mjs.map → copilotkit.mjs.map} +0 -0
- /package/dist/{openai/chat-completion-client.mjs.map → types/document-pointer.js.map} +0 -0
- /package/dist/{openai/chat-completion-stream.mjs.map → types/index.js.map} +0 -0
- /package/dist/{openai/chat-completion-transport.mjs.map → types/system-message.js.map} +0 -0
- /package/dist/{openai/index.mjs.map → utils/fetch-chat-completion.mjs.map} +0 -0
- /package/dist/{types/annotated-function.mjs.map → utils/utils.js.map} +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
8
|
+
|
|
9
|
+
var emptyCopilotContext = {
|
|
10
|
+
entryPoints: {},
|
|
11
|
+
setEntryPoint: () => {
|
|
12
|
+
},
|
|
13
|
+
removeEntryPoint: () => {
|
|
14
|
+
},
|
|
15
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
|
|
16
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(),
|
|
17
|
+
getContextString: (documents, categories) => returnAndThrowInDebug(),
|
|
18
|
+
addContext: () => "",
|
|
19
|
+
removeContext: () => {
|
|
20
|
+
},
|
|
21
|
+
getDocumentsContext: (categories) => returnAndThrowInDebug(),
|
|
22
|
+
addDocumentContext: () => returnAndThrowInDebug(),
|
|
23
|
+
removeDocumentContext: () => {
|
|
24
|
+
},
|
|
25
|
+
copilotApiConfig: new class {
|
|
26
|
+
get chatApiEndpoint() {
|
|
27
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
28
|
+
}
|
|
29
|
+
get chatApiEndpointV2() {
|
|
30
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
31
|
+
}
|
|
32
|
+
get headers() {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
get body() {
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
38
|
+
}()
|
|
39
|
+
};
|
|
40
|
+
var CopilotContext = React__default.default.createContext(emptyCopilotContext);
|
|
41
|
+
function returnAndThrowInDebug(value) {
|
|
42
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/hooks/use-make-copilot-document-readable.ts
|
|
46
|
+
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
47
|
+
const { addDocumentContext, removeDocumentContext } = React.useContext(CopilotContext);
|
|
48
|
+
const idRef = React.useRef();
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
const id = addDocumentContext(document, categories);
|
|
51
|
+
idRef.current = id;
|
|
52
|
+
return () => {
|
|
53
|
+
removeDocumentContext(id);
|
|
54
|
+
};
|
|
55
|
+
}, [addDocumentContext, removeDocumentContext, ...dependencies]);
|
|
56
|
+
return idRef.current;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
exports.useMakeCopilotDocumentReadable = useMakeCopilotDocumentReadable;
|
|
60
|
+
//# sourceMappingURL=out.js.map
|
|
61
|
+
//# sourceMappingURL=use-make-copilot-document-readable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-make-copilot-document-readable.ts","../../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;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GAAG,CAAC,oBAAoB,uBAAuB,GAAG,YAAY,CAAC;AAE/D,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Makes a document readable by Copilot.\n * @param document The document to make readable.\n * @param categories The categories to associate with the document.\n * @param dependencies The dependencies to use for the effect.\n * @returns The id of the document.\n */\nexport function useMakeCopilotDocumentReadable(\n document: DocumentPointer,\n categories?: string[],\n dependencies: any[] = [],\n): string | undefined {\n const { addDocumentContext, removeDocumentContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addDocumentContext(document, categories);\n idRef.current = id;\n\n return () => {\n removeDocumentContext(id);\n };\n }, [addDocumentContext, removeDocumentContext, ...dependencies]);\n\n return idRef.current;\n}\n","\"use client\";\n\nimport { FunctionCallHandler, AnnotatedFunction, Function } from \"@copilotkit/shared\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get chatApiEndpointV2(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n return value;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { useMakeCopilotDocumentReadable } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
1
|
+
export { useMakeCopilotDocumentReadable } from '../chunk-IQH5AQQC.mjs';
|
|
2
|
+
import '../chunk-4CFJ6A6A.mjs';
|
|
3
3
|
import '../chunk-MRXNTQOX.mjs';
|
|
4
4
|
//# sourceMappingURL=out.js.map
|
|
5
5
|
//# sourceMappingURL=use-make-copilot-document-readable.mjs.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
8
|
+
|
|
9
|
+
var emptyCopilotContext = {
|
|
10
|
+
entryPoints: {},
|
|
11
|
+
setEntryPoint: () => {
|
|
12
|
+
},
|
|
13
|
+
removeEntryPoint: () => {
|
|
14
|
+
},
|
|
15
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
|
|
16
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(),
|
|
17
|
+
getContextString: (documents, categories) => returnAndThrowInDebug(),
|
|
18
|
+
addContext: () => "",
|
|
19
|
+
removeContext: () => {
|
|
20
|
+
},
|
|
21
|
+
getDocumentsContext: (categories) => returnAndThrowInDebug(),
|
|
22
|
+
addDocumentContext: () => returnAndThrowInDebug(),
|
|
23
|
+
removeDocumentContext: () => {
|
|
24
|
+
},
|
|
25
|
+
copilotApiConfig: new class {
|
|
26
|
+
get chatApiEndpoint() {
|
|
27
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
28
|
+
}
|
|
29
|
+
get chatApiEndpointV2() {
|
|
30
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
31
|
+
}
|
|
32
|
+
get headers() {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
get body() {
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
38
|
+
}()
|
|
39
|
+
};
|
|
40
|
+
var CopilotContext = React__default.default.createContext(emptyCopilotContext);
|
|
41
|
+
function returnAndThrowInDebug(value) {
|
|
42
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/hooks/use-make-copilot-readable.ts
|
|
46
|
+
function useMakeCopilotReadable(information, parentId, categories) {
|
|
47
|
+
const { addContext, removeContext } = React.useContext(CopilotContext);
|
|
48
|
+
const idRef = React.useRef();
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
const id = addContext(information, parentId, categories);
|
|
51
|
+
idRef.current = id;
|
|
52
|
+
return () => {
|
|
53
|
+
removeContext(id);
|
|
54
|
+
};
|
|
55
|
+
}, [information, parentId, addContext, removeContext]);
|
|
56
|
+
return idRef.current;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
exports.useMakeCopilotReadable = useMakeCopilotReadable;
|
|
60
|
+
//# sourceMappingURL=out.js.map
|
|
61
|
+
//# sourceMappingURL=use-make-copilot-readable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-make-copilot-readable.ts","../../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,YAAY,WAAW,cAAc;;;ACC9C,OAAO,WAAW;AAiElB,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAE3F,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;;;AD7FO,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n","\"use client\";\n\nimport { FunctionCallHandler, AnnotatedFunction, Function } from \"@copilotkit/shared\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get chatApiEndpointV2(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n return value;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { useMakeCopilotReadable } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
1
|
+
export { useMakeCopilotReadable } from '../chunk-YOXWT7I6.mjs';
|
|
2
|
+
import '../chunk-4CFJ6A6A.mjs';
|
|
3
3
|
import '../chunk-MRXNTQOX.mjs';
|
|
4
4
|
//# sourceMappingURL=out.js.map
|
|
5
5
|
//# sourceMappingURL=use-make-copilot-readable.mjs.map
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var nanoid = require('nanoid');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defProps = Object.defineProperties;
|
|
8
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
9
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var removeNode = (nodes, id) => {
|
|
26
|
+
return nodes.reduce((result, node) => {
|
|
27
|
+
if (node.id !== id) {
|
|
28
|
+
const newNode = __spreadProps(__spreadValues({}, node), { children: removeNode(node.children, id) });
|
|
29
|
+
result.push(newNode);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}, []);
|
|
33
|
+
};
|
|
34
|
+
var addNode = (nodes, newNode, parentId) => {
|
|
35
|
+
if (!parentId) {
|
|
36
|
+
return [...nodes, newNode];
|
|
37
|
+
}
|
|
38
|
+
return nodes.map((node) => {
|
|
39
|
+
if (node.id === parentId) {
|
|
40
|
+
return __spreadProps(__spreadValues({}, node), { children: [...node.children, newNode] });
|
|
41
|
+
} else if (node.children.length) {
|
|
42
|
+
return __spreadProps(__spreadValues({}, node), { children: addNode(node.children, newNode, parentId) });
|
|
43
|
+
}
|
|
44
|
+
return node;
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
var treeIndentationRepresentation = (index, indentLevel) => {
|
|
48
|
+
if (indentLevel === 0) {
|
|
49
|
+
return (index + 1).toString();
|
|
50
|
+
} else if (indentLevel === 1) {
|
|
51
|
+
return String.fromCharCode(65 + index);
|
|
52
|
+
} else if (indentLevel === 2) {
|
|
53
|
+
return String.fromCharCode(97 + index);
|
|
54
|
+
} else {
|
|
55
|
+
return "-";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var printNode = (node, prefix = "", indentLevel = 0) => {
|
|
59
|
+
const indent = " ".repeat(3).repeat(indentLevel);
|
|
60
|
+
const prefixPlusIndentLength = prefix.length + indent.length;
|
|
61
|
+
const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
|
|
62
|
+
const valueLines = node.value.split("\n");
|
|
63
|
+
const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
|
|
64
|
+
const outputSubsequentLines = valueLines.slice(1).map((line) => `${subsequentLinesPrefix}${line}`).join("\n");
|
|
65
|
+
let output = `${outputFirstLine}
|
|
66
|
+
`;
|
|
67
|
+
if (outputSubsequentLines) {
|
|
68
|
+
output += `${outputSubsequentLines}
|
|
69
|
+
`;
|
|
70
|
+
}
|
|
71
|
+
const childPrePrefix = " ".repeat(prefix.length);
|
|
72
|
+
node.children.forEach(
|
|
73
|
+
(child, index) => output += printNode(
|
|
74
|
+
child,
|
|
75
|
+
`${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
|
|
76
|
+
indentLevel + 1
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
return output;
|
|
80
|
+
};
|
|
81
|
+
function treeReducer(state, action) {
|
|
82
|
+
switch (action.type) {
|
|
83
|
+
case "ADD_NODE": {
|
|
84
|
+
const { value, parentId, id: newNodeId } = action;
|
|
85
|
+
const newNode = {
|
|
86
|
+
id: newNodeId,
|
|
87
|
+
value,
|
|
88
|
+
children: [],
|
|
89
|
+
categories: new Set(action.categories)
|
|
90
|
+
};
|
|
91
|
+
try {
|
|
92
|
+
return addNode(state, newNode, parentId);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error(`Error while adding node with id ${newNodeId}: ${error}`);
|
|
95
|
+
return state;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
case "REMOVE_NODE":
|
|
99
|
+
return removeNode(state, action.id);
|
|
100
|
+
default:
|
|
101
|
+
return state;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
var useTree = () => {
|
|
105
|
+
const [tree, dispatch] = react.useReducer(treeReducer, []);
|
|
106
|
+
const addElement = react.useCallback(
|
|
107
|
+
(value, categories, parentId) => {
|
|
108
|
+
const newNodeId = nanoid.nanoid();
|
|
109
|
+
dispatch({
|
|
110
|
+
type: "ADD_NODE",
|
|
111
|
+
value,
|
|
112
|
+
parentId,
|
|
113
|
+
id: newNodeId,
|
|
114
|
+
categories
|
|
115
|
+
});
|
|
116
|
+
return newNodeId;
|
|
117
|
+
},
|
|
118
|
+
[]
|
|
119
|
+
);
|
|
120
|
+
const removeElement = react.useCallback((id) => {
|
|
121
|
+
dispatch({ type: "REMOVE_NODE", id });
|
|
122
|
+
}, []);
|
|
123
|
+
const printTree = react.useCallback(
|
|
124
|
+
(categories) => {
|
|
125
|
+
const categoriesSet = new Set(categories);
|
|
126
|
+
let output = "";
|
|
127
|
+
tree.forEach((node, index) => {
|
|
128
|
+
if (!setsHaveIntersection(categoriesSet, node.categories)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (index !== 0) {
|
|
132
|
+
output += "\n";
|
|
133
|
+
}
|
|
134
|
+
output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);
|
|
135
|
+
});
|
|
136
|
+
return output;
|
|
137
|
+
},
|
|
138
|
+
[tree]
|
|
139
|
+
);
|
|
140
|
+
return { tree, addElement, printTree, removeElement };
|
|
141
|
+
};
|
|
142
|
+
var use_tree_default = useTree;
|
|
143
|
+
function setsHaveIntersection(setA, setB) {
|
|
144
|
+
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
145
|
+
for (let item of smallerSet) {
|
|
146
|
+
if (largerSet.has(item)) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = use_tree_default;
|
|
154
|
+
//# sourceMappingURL=out.js.map
|
|
155
|
+
//# sourceMappingURL=use-tree.js.map
|
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OAAO;AACzB,eAAS;AAAA,QACP,MAAM;AAAA,QACN;AAAA,QACA;AAAA,QACA,IAAI;AAAA,QACJ;AAAA,MACF,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA,EACH;AAEA,QAAM,gBAAgB,YAAY,CAAC,OAAyB;AAC1D,aAAS,EAAE,MAAM,eAAe,GAAG,CAAC;AAAA,EACtC,GAAG,CAAC,CAAC;AAEL,QAAM,YAAY;AAAA,IAChB,CAAC,eAAiC;AAChC,YAAM,gBAAgB,IAAI,IAAI,UAAU;AAExC,UAAI,SAAS;AACb,WAAK,QAAQ,CAAC,MAAM,UAAU;AAE5B,YAAI,CAAC,qBAAqB,eAAe,KAAK,UAAU,GAAG;AACzD;AAAA,QACF;AAGA,YAAI,UAAU,GAAG;AACf,oBAAU;AAAA,QACZ;AAEA,kBAAU,UAAU,MAAM,GAAG,8BAA8B,OAAO,CAAC,KAAK;AAAA,MAC1E,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,MAAM,YAAY,WAAW,cAAc;AACtD;AAEA,IAAO,mBAAQ;AAEf,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAAI,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAEnF,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["import { nanoid } from \"nanoid\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type TreeNodeId = string;\n\nexport interface TreeNode {\n id: TreeNodeId;\n value: string;\n children: TreeNode[];\n parentId?: TreeNodeId;\n categories: Set<string>;\n}\n\nexport type Tree = TreeNode[];\n\nexport interface UseTreeReturn {\n tree: Tree;\n addElement: (value: string, categories: string[], parentId?: TreeNodeId) => TreeNodeId;\n printTree: (categories: string[]) => string;\n removeElement: (id: TreeNodeId) => void;\n}\n\nconst findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {\n for (const node of nodes) {\n if (node.id === id) {\n return node;\n }\n const result = findNode(node.children, id);\n if (result) {\n return result;\n }\n }\n return undefined;\n};\n\nconst removeNode = (nodes: Tree, id: TreeNodeId): Tree => {\n return nodes.reduce((result: Tree, node) => {\n if (node.id !== id) {\n const newNode = { ...node, children: removeNode(node.children, id) };\n result.push(newNode);\n }\n return result;\n }, []);\n};\n\nconst addNode = (nodes: Tree, newNode: TreeNode, parentId?: TreeNodeId): Tree => {\n if (!parentId) {\n return [...nodes, newNode];\n }\n return nodes.map((node) => {\n if (node.id === parentId) {\n return { ...node, children: [...node.children, newNode] };\n } else if (node.children.length) {\n return { ...node, children: addNode(node.children, newNode, parentId) };\n }\n return node;\n });\n};\n\nconst treeIndentationRepresentation = (index: number, indentLevel: number): string => {\n if (indentLevel === 0) {\n return (index + 1).toString();\n } else if (indentLevel === 1) {\n return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'\n } else if (indentLevel === 2) {\n return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'\n } else {\n return \"-\";\n }\n};\n\nconst printNode = (node: TreeNode, prefix = \"\", indentLevel = 0): string => {\n const indent = \" \".repeat(3).repeat(indentLevel);\n\n const prefixPlusIndentLength = prefix.length + indent.length;\n const subsequentLinesPrefix = \" \".repeat(prefixPlusIndentLength);\n\n const valueLines = node.value.split(\"\\n\");\n\n const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;\n const outputSubsequentLines = valueLines\n .slice(1)\n .map((line) => `${subsequentLinesPrefix}${line}`)\n .join(\"\\n\");\n\n let output = `${outputFirstLine}\\n`;\n if (outputSubsequentLines) {\n output += `${outputSubsequentLines}\\n`;\n }\n\n const childPrePrefix = \" \".repeat(prefix.length);\n\n node.children.forEach(\n (child, index) =>\n (output += printNode(\n child,\n `${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,\n indentLevel + 1,\n )),\n );\n return output;\n};\n\n// Action types\ntype Action =\n | {\n type: \"ADD_NODE\";\n value: string;\n parentId?: string;\n id: string;\n categories: string[];\n }\n | { type: \"REMOVE_NODE\"; id: string };\n\n// Reducer function\nfunction treeReducer(state: Tree, action: Action): Tree {\n switch (action.type) {\n case \"ADD_NODE\": {\n const { value, parentId, id: newNodeId } = action;\n const newNode: TreeNode = {\n id: newNodeId,\n value,\n children: [],\n categories: new Set(action.categories),\n };\n\n try {\n return addNode(state, newNode, parentId);\n } catch (error) {\n console.error(`Error while adding node with id ${newNodeId}: ${error}`);\n return state;\n }\n }\n case \"REMOVE_NODE\":\n return removeNode(state, action.id);\n default:\n return state;\n }\n}\n\n// useTree hook\nconst useTree = (): UseTreeReturn => {\n const [tree, dispatch] = useReducer(treeReducer, []);\n\n const addElement = useCallback(\n (value: string, categories: string[], parentId?: string): TreeNodeId => {\n const newNodeId = nanoid(); // Generate new ID outside of dispatch\n dispatch({\n type: \"ADD_NODE\",\n value,\n parentId,\n id: newNodeId,\n categories: categories,\n });\n return newNodeId; // Return the new ID\n },\n [],\n );\n\n const removeElement = useCallback((id: TreeNodeId): void => {\n dispatch({ type: \"REMOVE_NODE\", id });\n }, []);\n\n const printTree = useCallback(\n (categories: string[]): string => {\n const categoriesSet = new Set(categories);\n\n let output = \"\";\n tree.forEach((node, index) => {\n // if the node does not have any of the desired categories, continue to the next node\n if (!setsHaveIntersection(categoriesSet, node.categories)) {\n return;\n }\n\n // add a new line before each node except the first one\n if (index !== 0) {\n output += \"\\n\";\n }\n\n output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);\n });\n return output;\n },\n [tree],\n );\n\n return { tree, addElement, printTree, removeElement };\n};\n\nexport default useTree;\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { CopilotKit, defaultCopilotContextCategories } from './components/copilot-provider/copilotkit.js';
|
|
2
|
+
export { CopilotKitProps } from './components/copilot-provider/copilotkit-props.js';
|
|
3
3
|
export { CopilotApiConfig, CopilotContext, CopilotContextParams } from './context/copilot-context.js';
|
|
4
4
|
export { UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat } from './hooks/use-copilot-chat.js';
|
|
5
5
|
export { useMakeCopilotActionable } from './hooks/use-make-copilot-actionable.js';
|
|
6
6
|
export { useMakeCopilotReadable } from './hooks/use-make-copilot-readable.js';
|
|
7
7
|
export { useMakeCopilotDocumentReadable } from './hooks/use-make-copilot-document-readable.js';
|
|
8
8
|
export { UseChatHelpers } from './hooks/use-chat.js';
|
|
9
|
-
export { AnnotatedFunction, AnnotatedFunctionArgument } from './types/annotated-function.js';
|
|
10
9
|
export { DocumentPointer } from './types/document-pointer.js';
|
|
11
|
-
export { Function, FunctionCall, FunctionCallHandler, Message, Role } from './types/base.js';
|
|
12
10
|
export { SystemMessageFunction } from './types/system-message.js';
|
|
13
11
|
export { AssistantStatus, RequestForwardingOptions, UseCopilotChatOptionsV2, UseCopilotChatV2Result, useCopilotChatV2 } from './openai-assistants/hooks/use-copilot-chat-v2.js';
|
|
14
12
|
export { processMessageStream } from './openai-assistants/utils/process-message-stream.js';
|
|
15
|
-
export {
|
|
16
|
-
export { ChatCompletionTransportFetchParams } from './openai/chat-completion-transport.js';
|
|
13
|
+
export { FetchChatCompletionParams, fetchAndDecodeChatCompletion, fetchAndDecodeChatCompletionAsText, fetchChatCompletion } from './utils/fetch-chat-completion.js';
|
|
17
14
|
import 'react';
|
|
18
|
-
import './hooks/use-tree.js';
|
|
19
|
-
import 'openai/resources/chat';
|
|
20
15
|
import '@copilotkit/shared';
|
|
21
|
-
import '
|
|
16
|
+
import './hooks/use-tree.js';
|