@copilotkit/react-core 0.38.0-mme-pre.0 → 0.38.0-mme-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 +187 -234
- package/CHANGELOG.md +4 -3
- package/dist/{chunk-NKIUZSGZ.mjs → chunk-5Q7DU3XW.mjs} +40 -40
- package/dist/chunk-5Q7DU3XW.mjs.map +1 -0
- package/dist/{chunk-B244LK6F.mjs → chunk-ATWK243A.mjs} +2 -2
- package/dist/{chunk-AJSY6LS5.mjs → chunk-FY3TQ7Q6.mjs} +17 -32
- package/dist/chunk-FY3TQ7Q6.mjs.map +1 -0
- package/dist/{chunk-SR4RW4CU.mjs → chunk-GFQBAEJL.mjs} +6 -6
- package/dist/chunk-GFQBAEJL.mjs.map +1 -0
- package/dist/{chunk-DY63PD22.mjs → chunk-GLPZSBNA.mjs} +4 -5
- package/dist/chunk-GLPZSBNA.mjs.map +1 -0
- package/dist/{chunk-S5LWO5V3.mjs → chunk-IBOL6LE5.mjs} +2 -2
- package/dist/{chunk-NSUYO6TP.mjs → chunk-MI7BERU7.mjs} +9 -9
- package/dist/chunk-MI7BERU7.mjs.map +1 -0
- package/dist/chunk-MW2IVCDP.mjs +188 -0
- package/dist/chunk-MW2IVCDP.mjs.map +1 -0
- package/dist/{chunk-ZHYCHXIO.mjs → chunk-PSZAPRNZ.mjs} +56 -42
- package/dist/chunk-PSZAPRNZ.mjs.map +1 -0
- package/dist/{chunk-DY4YVUO3.mjs → chunk-WJIS7HTH.mjs} +14 -25
- package/dist/chunk-WJIS7HTH.mjs.map +1 -0
- package/dist/{chunk-MNZXQ6UH.mjs → chunk-WTGFKR3D.mjs} +2 -2
- package/dist/chunk-YJLRG5U3.mjs +1 -0
- package/dist/components/copilot-provider/copilotkit.js +17 -32
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +2 -2
- package/dist/components/copilot-provider/index.js +17 -32
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/index.js +17 -32
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +5 -5
- package/dist/context/copilot-context.js +3 -4
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.js +3 -4
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +136 -232
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +9 -10
- package/dist/hooks/use-chat.d.ts +26 -52
- package/dist/hooks/use-chat.js +104 -203
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +1 -2
- package/dist/hooks/use-copilot-action.js +10 -11
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -2
- package/dist/hooks/use-copilot-chat.d.ts +1 -1
- package/dist/hooks/use-copilot-chat.js +119 -215
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +4 -5
- package/dist/hooks/use-copilot-readable.js +3 -4
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-actionable.js +7 -8
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +3 -4
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-readable.js +3 -4
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +2 -2
- package/dist/index.d.ts +1 -3
- package/dist/index.js +227 -499
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -31
- package/dist/lib/copilot-task.d.ts +1 -0
- package/dist/lib/copilot-task.js +31 -110
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +3 -4
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +33 -110
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +3 -4
- package/dist/utils/extract.d.ts +1 -0
- package/dist/utils/extract.js +49 -109
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +3 -4
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +52 -132
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -13
- package/package.json +6 -5
- package/src/components/copilot-provider/copilotkit.tsx +16 -33
- package/src/context/copilot-context.tsx +9 -16
- package/src/hooks/use-chat.ts +204 -212
- package/src/hooks/use-copilot-action.ts +7 -8
- package/src/hooks/use-copilot-chat.ts +12 -36
- package/src/hooks/use-make-copilot-actionable.ts +4 -4
- package/src/index.tsx +0 -1
- package/src/lib/copilot-task.ts +42 -42
- package/src/utils/extract.ts +61 -36
- package/src/utils/index.ts +0 -7
- package/dist/chunk-36FKUOWM.mjs +0 -137
- package/dist/chunk-36FKUOWM.mjs.map +0 -1
- package/dist/chunk-3XVO5UEG.mjs +0 -205
- package/dist/chunk-3XVO5UEG.mjs.map +0 -1
- package/dist/chunk-7GFKOIO7.mjs +0 -1
- package/dist/chunk-AJSY6LS5.mjs.map +0 -1
- package/dist/chunk-BABVSMJR.mjs +0 -1
- package/dist/chunk-BABVSMJR.mjs.map +0 -1
- package/dist/chunk-CYDWEPFL.mjs +0 -1
- package/dist/chunk-CYDWEPFL.mjs.map +0 -1
- package/dist/chunk-DY4YVUO3.mjs.map +0 -1
- package/dist/chunk-DY63PD22.mjs.map +0 -1
- package/dist/chunk-FRAKUJWH.mjs +0 -1
- package/dist/chunk-FRAKUJWH.mjs.map +0 -1
- package/dist/chunk-MJKBCG4U.mjs +0 -91
- package/dist/chunk-MJKBCG4U.mjs.map +0 -1
- package/dist/chunk-NKIUZSGZ.mjs.map +0 -1
- package/dist/chunk-NSUYO6TP.mjs.map +0 -1
- package/dist/chunk-SR4RW4CU.mjs.map +0 -1
- package/dist/chunk-ZFS5SQUT.mjs +0 -31
- package/dist/chunk-ZFS5SQUT.mjs.map +0 -1
- package/dist/chunk-ZHYCHXIO.mjs.map +0 -1
- package/dist/openai-assistants/hooks/index.d.ts +0 -2
- package/dist/openai-assistants/hooks/index.js +0 -277
- package/dist/openai-assistants/hooks/index.js.map +0 -1
- package/dist/openai-assistants/hooks/index.mjs +0 -18
- package/dist/openai-assistants/hooks/index.mjs.map +0 -1
- package/dist/openai-assistants/hooks/use-assistants.d.ts +0 -17
- package/dist/openai-assistants/hooks/use-assistants.js +0 -154
- package/dist/openai-assistants/hooks/use-assistants.js.map +0 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +0 -92
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +0 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +0 -44
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -277
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +0 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +0 -19
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +0 -1
- package/dist/openai-assistants/index.d.ts +0 -3
- package/dist/openai-assistants/index.js +0 -279
- package/dist/openai-assistants/index.js.map +0 -1
- package/dist/openai-assistants/index.mjs +0 -22
- package/dist/openai-assistants/index.mjs.map +0 -1
- package/dist/openai-assistants/utils/index.d.ts +0 -1
- package/dist/openai-assistants/utils/index.js +0 -73
- package/dist/openai-assistants/utils/index.js.map +0 -1
- package/dist/openai-assistants/utils/index.mjs +0 -9
- package/dist/openai-assistants/utils/index.mjs.map +0 -1
- package/dist/openai-assistants/utils/process-message-stream.d.ts +0 -3
- package/dist/openai-assistants/utils/process-message-stream.js +0 -71
- package/dist/openai-assistants/utils/process-message-stream.js.map +0 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +0 -8
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +0 -1
- package/dist/utils/fetch-chat-completion.d.ts +0 -36
- package/dist/utils/fetch-chat-completion.js +0 -141
- package/dist/utils/fetch-chat-completion.js.map +0 -1
- package/dist/utils/fetch-chat-completion.mjs +0 -12
- package/dist/utils/fetch-chat-completion.mjs.map +0 -1
- package/src/openai-assistants/hooks/index.ts +0 -9
- package/src/openai-assistants/hooks/use-assistants.ts +0 -112
- package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +0 -189
- package/src/openai-assistants/index.ts +0 -2
- package/src/openai-assistants/utils/index.ts +0 -1
- package/src/openai-assistants/utils/process-message-stream.ts +0 -25
- package/src/utils/fetch-chat-completion.ts +0 -120
- /package/dist/{chunk-B244LK6F.mjs.map → chunk-ATWK243A.mjs.map} +0 -0
- /package/dist/{chunk-S5LWO5V3.mjs.map → chunk-IBOL6LE5.mjs.map} +0 -0
- /package/dist/{chunk-MNZXQ6UH.mjs.map → chunk-WTGFKR3D.mjs.map} +0 -0
- /package/dist/{chunk-7GFKOIO7.mjs.map → chunk-YJLRG5U3.mjs.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context/copilot-context.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/context/copilot-context.tsx"],"sourcesContent":["import { CopilotCloudConfig, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { ActionRenderProps, FrontendAction } from \"../types/frontend-action\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\nimport { CopilotChatSuggestionConfiguration } from \"../types/chat-suggestion-configuration\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The public API key for Copilot Cloud.\n */\n publicApiKey?: string;\n\n /**\n * The configuration for Copilot Cloud.\n */\n cloud?: CopilotCloudConfig;\n\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the Copilot transcribe audio service.\n */\n transcribeAudioUrl?: string;\n\n /**\n * The endpoint for the Copilot text to speech service.\n */\n textToSpeechUrl?: 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 /**\n * Backend only props that will be combined to body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'user_id': 'user_id'\n * }\n * ```\n */\n backendOnlyProps?: Record<string, any>;\n\n /**\n * Indicates whether the user agent should send or receive cookies from the other domain\n * in the case of cross-origin requests.\n */\n credentials?: RequestCredentials;\n}\n\nexport type InChatRenderFunction = (props: ActionRenderProps<any>) => string | JSX.Element;\n\nexport interface CopilotContextParams {\n // function-calling\n actions: Record<string, FrontendAction<any>>;\n setAction: (id: string, action: FrontendAction<any>) => void;\n removeAction: (id: string) => void;\n chatComponentsCache: React.RefObject<Record<string, InChatRenderFunction | string>>;\n\n getFunctionCallHandler: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => 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 // chat\n messages: Message[];\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n isLoading: boolean;\n setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n chatSuggestionConfiguration: { [key: string]: CopilotChatSuggestionConfiguration };\n addChatSuggestionConfiguration: (\n id: string,\n suggestion: CopilotChatSuggestionConfiguration,\n ) => void;\n removeChatSuggestionConfiguration: (id: string) => void;\n\n chatInstructions: string;\n setChatInstructions: React.Dispatch<React.SetStateAction<string>>;\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n actions: {},\n setAction: () => {},\n removeAction: () => {},\n\n chatComponentsCache: { current: {} },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n messages: [],\n setMessages: () => returnAndThrowInDebug([]),\n\n isLoading: false,\n setIsLoading: () => returnAndThrowInDebug(false),\n\n chatInstructions: \"\",\n setChatInstructions: () => returnAndThrowInDebug(\"\"),\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 chatSuggestionConfiguration: {},\n addChatSuggestionConfiguration: () => {},\n removeChatSuggestionConfiguration: () => {},\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nexport function useCopilotContext(): CopilotContextParams {\n return React.useContext(CopilotContext);\n}\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAAkB;AA8HlB,IAAM,sBAA4C;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,cAAc,MAAM;AAAA,EAAC;AAAA,EAErB,qBAAqB,EAAE,SAAS,CAAC,EAAE;AAAA,EACnC,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,UAAU,CAAC;AAAA,EACX,aAAa,MAAM,sBAAsB,CAAC,CAAC;AAAA,EAE3C,WAAW;AAAA,EACX,cAAc,MAAM,sBAAsB,KAAK;AAAA,EAE/C,kBAAkB;AAAA,EAClB,qBAAqB,MAAM,sBAAsB,EAAE;AAAA,EAEnD,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;AAAA,EAEH,6BAA6B,CAAC;AAAA,EAC9B,gCAAgC,MAAM;AAAA,EAAC;AAAA,EACvC,mCAAmC,MAAM;AAAA,EAAC;AAC5C;AAEO,IAAM,iBAAiB,aAAAA,QAAM,cAAoC,mBAAmB;AAEpF,SAAS,oBAA0C;AACxD,SAAO,aAAAA,QAAM,WAAW,cAAc;AACxC;AAEA,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;","names":["React"]}
|
package/dist/context/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { CopilotApiConfig, CopilotContext, CopilotContextParams, useCopilotContext } from './copilot-context.js';
|
|
2
2
|
import '@copilotkit/shared';
|
|
3
|
+
import '@copilotkit/runtime-client-gql';
|
|
3
4
|
import '../types/frontend-action.js';
|
|
4
5
|
import 'react';
|
|
5
6
|
import '../hooks/use-tree.js';
|
package/dist/context/index.js
CHANGED
|
@@ -58,17 +58,16 @@ module.exports = __toCommonJS(context_exports);
|
|
|
58
58
|
// src/context/copilot-context.tsx
|
|
59
59
|
var import_react = __toESM(require("react"));
|
|
60
60
|
var emptyCopilotContext = {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
actions: {},
|
|
62
|
+
setAction: () => {
|
|
63
63
|
},
|
|
64
|
-
|
|
64
|
+
removeAction: () => {
|
|
65
65
|
},
|
|
66
66
|
chatComponentsCache: { current: {} },
|
|
67
67
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
68
68
|
addContext: () => "",
|
|
69
69
|
removeContext: () => {
|
|
70
70
|
},
|
|
71
|
-
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
72
71
|
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
73
72
|
})),
|
|
74
73
|
messages: [],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context/index.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["export { CopilotContext, useCopilotContext } from \"./copilot-context\";\nexport type { CopilotContextParams } from \"./copilot-context\";\nexport type { CopilotApiConfig } from \"./copilot-context\";\n","import {
|
|
1
|
+
{"version":3,"sources":["../../src/context/index.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["export { CopilotContext, useCopilotContext } from \"./copilot-context\";\nexport type { CopilotContextParams } from \"./copilot-context\";\nexport type { CopilotApiConfig } from \"./copilot-context\";\n","import { CopilotCloudConfig, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { ActionRenderProps, FrontendAction } from \"../types/frontend-action\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\nimport { CopilotChatSuggestionConfiguration } from \"../types/chat-suggestion-configuration\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The public API key for Copilot Cloud.\n */\n publicApiKey?: string;\n\n /**\n * The configuration for Copilot Cloud.\n */\n cloud?: CopilotCloudConfig;\n\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the Copilot transcribe audio service.\n */\n transcribeAudioUrl?: string;\n\n /**\n * The endpoint for the Copilot text to speech service.\n */\n textToSpeechUrl?: 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 /**\n * Backend only props that will be combined to body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'user_id': 'user_id'\n * }\n * ```\n */\n backendOnlyProps?: Record<string, any>;\n\n /**\n * Indicates whether the user agent should send or receive cookies from the other domain\n * in the case of cross-origin requests.\n */\n credentials?: RequestCredentials;\n}\n\nexport type InChatRenderFunction = (props: ActionRenderProps<any>) => string | JSX.Element;\n\nexport interface CopilotContextParams {\n // function-calling\n actions: Record<string, FrontendAction<any>>;\n setAction: (id: string, action: FrontendAction<any>) => void;\n removeAction: (id: string) => void;\n chatComponentsCache: React.RefObject<Record<string, InChatRenderFunction | string>>;\n\n getFunctionCallHandler: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => 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 // chat\n messages: Message[];\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n isLoading: boolean;\n setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n chatSuggestionConfiguration: { [key: string]: CopilotChatSuggestionConfiguration };\n addChatSuggestionConfiguration: (\n id: string,\n suggestion: CopilotChatSuggestionConfiguration,\n ) => void;\n removeChatSuggestionConfiguration: (id: string) => void;\n\n chatInstructions: string;\n setChatInstructions: React.Dispatch<React.SetStateAction<string>>;\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n actions: {},\n setAction: () => {},\n removeAction: () => {},\n\n chatComponentsCache: { current: {} },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n messages: [],\n setMessages: () => returnAndThrowInDebug([]),\n\n isLoading: false,\n setIsLoading: () => returnAndThrowInDebug(false),\n\n chatInstructions: \"\",\n setChatInstructions: () => returnAndThrowInDebug(\"\"),\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 chatSuggestionConfiguration: {},\n addChatSuggestionConfiguration: () => {},\n removeChatSuggestionConfiguration: () => {},\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nexport function useCopilotContext(): CopilotContextParams {\n return React.useContext(CopilotContext);\n}\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,mBAAkB;AA8HlB,IAAM,sBAA4C;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,cAAc,MAAM;AAAA,EAAC;AAAA,EAErB,qBAAqB,EAAE,SAAS,CAAC,EAAE;AAAA,EACnC,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,UAAU,CAAC;AAAA,EACX,aAAa,MAAM,sBAAsB,CAAC,CAAC;AAAA,EAE3C,WAAW;AAAA,EACX,cAAc,MAAM,sBAAsB,KAAK;AAAA,EAE/C,kBAAkB;AAAA,EAClB,qBAAqB,MAAM,sBAAsB,EAAE;AAAA,EAEnD,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;AAAA,EAEH,6BAA6B,CAAC;AAAA,EAC9B,gCAAgC,MAAM;AAAA,EAAC;AAAA,EACvC,mCAAmC,MAAM;AAAA,EAAC;AAC5C;AAEO,IAAM,iBAAiB,aAAAA,QAAM,cAAoC,mBAAmB;AAEpF,SAAS,oBAA0C;AACxD,SAAO,aAAAA,QAAM,WAAW,cAAc;AACxC;AAEA,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;","names":["React"]}
|
package/dist/context/index.mjs
CHANGED
package/dist/hooks/index.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ export { useMakeCopilotReadable } from './use-make-copilot-readable.js';
|
|
|
5
5
|
export { useMakeCopilotDocumentReadable } from './use-make-copilot-document-readable.js';
|
|
6
6
|
export { UseChatHelpers } from './use-chat.js';
|
|
7
7
|
export { useCopilotReadable } from './use-copilot-readable.js';
|
|
8
|
-
import '@copilotkit/
|
|
8
|
+
import '@copilotkit/runtime-client-gql';
|
|
9
9
|
import '../types/system-message.js';
|
|
10
|
+
import '@copilotkit/shared';
|
|
10
11
|
import '../types/frontend-action.js';
|
|
11
12
|
import 'react';
|
|
12
13
|
import '../types/document-pointer.js';
|
package/dist/hooks/index.js
CHANGED
|
@@ -94,17 +94,16 @@ var import_react4 = require("react");
|
|
|
94
94
|
// src/context/copilot-context.tsx
|
|
95
95
|
var import_react = __toESM(require("react"));
|
|
96
96
|
var emptyCopilotContext = {
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
actions: {},
|
|
98
|
+
setAction: () => {
|
|
99
99
|
},
|
|
100
|
-
|
|
100
|
+
removeAction: () => {
|
|
101
101
|
},
|
|
102
102
|
chatComponentsCache: { current: {} },
|
|
103
103
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
104
104
|
addContext: () => "",
|
|
105
105
|
removeContext: () => {
|
|
106
106
|
},
|
|
107
|
-
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
108
107
|
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
109
108
|
})),
|
|
110
109
|
messages: [],
|
|
@@ -143,229 +142,146 @@ function returnAndThrowInDebug(value) {
|
|
|
143
142
|
return value;
|
|
144
143
|
}
|
|
145
144
|
|
|
145
|
+
// src/hooks/use-copilot-chat.ts
|
|
146
|
+
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
147
|
+
|
|
146
148
|
// src/hooks/use-chat.ts
|
|
147
149
|
var import_react2 = require("react");
|
|
148
|
-
var import_shared2 = require("@copilotkit/shared");
|
|
149
|
-
var import_nanoid = require("nanoid");
|
|
150
|
-
|
|
151
|
-
// src/utils/fetch-chat-completion.ts
|
|
152
150
|
var import_shared = require("@copilotkit/shared");
|
|
153
|
-
|
|
154
|
-
return __async(this, arguments, function* ({
|
|
155
|
-
copilotConfig,
|
|
156
|
-
model,
|
|
157
|
-
messages,
|
|
158
|
-
tools,
|
|
159
|
-
temperature,
|
|
160
|
-
headers,
|
|
161
|
-
body,
|
|
162
|
-
signal,
|
|
163
|
-
toolChoice
|
|
164
|
-
}) {
|
|
165
|
-
temperature || (temperature = 0.5);
|
|
166
|
-
tools || (tools = []);
|
|
167
|
-
const cleanedMessages = messages.map((message) => {
|
|
168
|
-
const { content, role, name, function_call } = message;
|
|
169
|
-
return { content, role, name, function_call };
|
|
170
|
-
});
|
|
171
|
-
toolChoice || (toolChoice = "auto");
|
|
172
|
-
const response = yield fetch(copilotConfig.chatApiEndpoint, {
|
|
173
|
-
method: "POST",
|
|
174
|
-
headers: __spreadValues(__spreadValues({
|
|
175
|
-
"Content-Type": "application/json"
|
|
176
|
-
}, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
|
|
177
|
-
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
178
|
-
model,
|
|
179
|
-
messages: cleanedMessages,
|
|
180
|
-
stream: true
|
|
181
|
-
}, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: toolChoice } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {}), copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {})),
|
|
182
|
-
signal,
|
|
183
|
-
credentials: copilotConfig.credentials
|
|
184
|
-
});
|
|
185
|
-
return response;
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
function excludeBackendOnlyProps(copilotConfig) {
|
|
189
|
-
var _a;
|
|
190
|
-
const backendOnlyProps = (_a = copilotConfig.backendOnlyProps) != null ? _a : {};
|
|
191
|
-
if (Object.keys(backendOnlyProps).length > 0) {
|
|
192
|
-
return {
|
|
193
|
-
[import_shared.EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps)
|
|
194
|
-
};
|
|
195
|
-
} else {
|
|
196
|
-
return {};
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
function fetchAndDecodeChatCompletion(params) {
|
|
200
|
-
return __async(this, null, function* () {
|
|
201
|
-
const response = yield fetchChatCompletion(params);
|
|
202
|
-
if (!response.ok || !response.body) {
|
|
203
|
-
response.events = null;
|
|
204
|
-
} else {
|
|
205
|
-
const events = yield (0, import_shared.decodeChatCompletion)((0, import_shared.parseChatCompletion)(response.body));
|
|
206
|
-
response.events = events;
|
|
207
|
-
}
|
|
208
|
-
return response;
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// src/hooks/use-chat.ts
|
|
213
|
-
var import_untruncate_json = __toESM(require("untruncate-json"));
|
|
151
|
+
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
214
152
|
function useChat(options) {
|
|
215
|
-
const {
|
|
153
|
+
const {
|
|
154
|
+
messages,
|
|
155
|
+
setMessages,
|
|
156
|
+
makeSystemMessageCallback,
|
|
157
|
+
copilotConfig,
|
|
158
|
+
setIsLoading,
|
|
159
|
+
initialMessages,
|
|
160
|
+
isLoading,
|
|
161
|
+
actions,
|
|
162
|
+
onFunctionCall
|
|
163
|
+
} = options;
|
|
216
164
|
const abortControllerRef = (0, import_react2.useRef)();
|
|
217
165
|
const threadIdRef = (0, import_react2.useRef)(null);
|
|
218
166
|
const runIdRef = (0, import_react2.useRef)(null);
|
|
219
|
-
const publicApiKey =
|
|
220
|
-
const headers = __spreadValues(__spreadValues({},
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
167
|
+
const publicApiKey = copilotConfig.publicApiKey;
|
|
168
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
169
|
+
const runtimeClient = new import_runtime_client_gql.CopilotRuntimeClient({
|
|
170
|
+
url: copilotConfig.chatApiEndpoint,
|
|
171
|
+
publicApiKey: copilotConfig.publicApiKey,
|
|
172
|
+
headers
|
|
173
|
+
});
|
|
174
|
+
const runChatCompletion = (previousMessages) => __async(this, null, function* () {
|
|
175
|
+
var _a, _b, _c, _d, _e;
|
|
176
|
+
setIsLoading(true);
|
|
177
|
+
let newMessages = [
|
|
178
|
+
new import_runtime_client_gql.TextMessage({
|
|
228
179
|
content: "",
|
|
229
|
-
role:
|
|
230
|
-
}
|
|
180
|
+
role: import_runtime_client_gql.Role.Assistant
|
|
181
|
+
})
|
|
231
182
|
];
|
|
232
183
|
const abortController = new AbortController();
|
|
233
184
|
abortControllerRef.current = abortController;
|
|
234
|
-
setMessages([...
|
|
235
|
-
const copilotConfigBody = options.copilotConfig.body || {};
|
|
236
|
-
if (threadIdRef.current) {
|
|
237
|
-
copilotConfigBody.threadId = threadIdRef.current;
|
|
238
|
-
}
|
|
239
|
-
if (runIdRef.current) {
|
|
240
|
-
copilotConfigBody.runId = runIdRef.current;
|
|
241
|
-
}
|
|
185
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
242
186
|
const systemMessage = makeSystemMessageCallback();
|
|
243
|
-
const messagesWithContext = [systemMessage, ...
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
role: "assistant"
|
|
187
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
188
|
+
const stream = import_runtime_client_gql.CopilotRuntimeClient.asStream(
|
|
189
|
+
runtimeClient.generateCopilotResponse(__spreadValues({
|
|
190
|
+
frontend: {
|
|
191
|
+
actions: actions.map((action) => ({
|
|
192
|
+
name: action.name,
|
|
193
|
+
description: action.description || "",
|
|
194
|
+
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || []))
|
|
195
|
+
}))
|
|
196
|
+
},
|
|
197
|
+
threadId: threadIdRef.current,
|
|
198
|
+
runId: runIdRef.current,
|
|
199
|
+
messages: (0, import_runtime_client_gql.convertMessagesToGqlInput)(messagesWithContext)
|
|
200
|
+
}, copilotConfig.cloud ? {
|
|
201
|
+
cloud: {
|
|
202
|
+
guardrails: {
|
|
203
|
+
inputValidationRules: {
|
|
204
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
205
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
206
|
+
}
|
|
207
|
+
}
|
|
265
208
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
let feedback = false;
|
|
209
|
+
} : {}))
|
|
210
|
+
);
|
|
211
|
+
const guardrailsEnabled = ((_c = (_b = (_a = copilotConfig.cloud) == null ? void 0 : _a.guardrails) == null ? void 0 : _b.input) == null ? void 0 : _c.restrictToTopic.enabled) || false;
|
|
212
|
+
const reader = stream.getReader();
|
|
213
|
+
let results = {};
|
|
272
214
|
try {
|
|
273
215
|
while (true) {
|
|
274
216
|
const { done, value } = yield reader.read();
|
|
275
217
|
if (done) {
|
|
276
218
|
break;
|
|
277
219
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
};
|
|
310
|
-
newMessages.push(currentMessage);
|
|
311
|
-
}
|
|
312
|
-
if (value.type === "function") {
|
|
313
|
-
currentMessage.function_call = {
|
|
314
|
-
name: value.name,
|
|
315
|
-
arguments: JSON.stringify(value.arguments),
|
|
316
|
-
scope: value.scope
|
|
317
|
-
};
|
|
318
|
-
} else if (value.type === "partial") {
|
|
319
|
-
let partialArguments = {};
|
|
320
|
-
try {
|
|
321
|
-
partialArguments = JSON.parse((0, import_untruncate_json.default)(value.arguments));
|
|
322
|
-
} catch (e) {
|
|
323
|
-
}
|
|
324
|
-
currentMessage.partialFunctionCall = {
|
|
325
|
-
name: value.name,
|
|
326
|
-
arguments: partialArguments
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
newMessages[newMessages.length - 1] = currentMessage;
|
|
330
|
-
setMessages([...messages2, ...newMessages]);
|
|
331
|
-
if (value.type === "function") {
|
|
332
|
-
try {
|
|
333
|
-
if (((_b = options.onFunctionCall) == null ? void 0 : _b.current) && value.scope === "client") {
|
|
334
|
-
const result = yield options.onFunctionCall.current(
|
|
335
|
-
messages2,
|
|
336
|
-
currentMessage.function_call
|
|
337
|
-
);
|
|
338
|
-
currentMessage = {
|
|
339
|
-
id: (0, import_nanoid.nanoid)(),
|
|
340
|
-
role: "function",
|
|
341
|
-
content: (0, import_shared2.encodeResult)(result),
|
|
342
|
-
name: currentMessage.function_call.name
|
|
343
|
-
};
|
|
344
|
-
newMessages.push(currentMessage);
|
|
345
|
-
setMessages([...messages2, ...newMessages]);
|
|
346
|
-
feedback = true;
|
|
220
|
+
if (!value.generateCopilotResponse) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
224
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
225
|
+
const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(value.generateCopilotResponse.messages);
|
|
226
|
+
if (messages2.length === 0) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
newMessages = [];
|
|
230
|
+
if (((_d = value.generateCopilotResponse.status) == null ? void 0 : _d.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
231
|
+
newMessages = [
|
|
232
|
+
new import_runtime_client_gql.TextMessage({
|
|
233
|
+
role: import_runtime_client_gql.MessageRole.Assistant,
|
|
234
|
+
content: ((_e = value.generateCopilotResponse.status.details) == null ? void 0 : _e.guardrailsReason) || ""
|
|
235
|
+
})
|
|
236
|
+
];
|
|
237
|
+
} else {
|
|
238
|
+
for (const message of messages2) {
|
|
239
|
+
newMessages.push(message);
|
|
240
|
+
if (message instanceof import_runtime_client_gql.ActionExecutionMessage && message.status.code !== import_runtime_client_gql.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
241
|
+
if (!(message.id in results)) {
|
|
242
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
const result = yield onFunctionCall({
|
|
246
|
+
messages: previousMessages,
|
|
247
|
+
name: message.name,
|
|
248
|
+
args: message.arguments
|
|
249
|
+
});
|
|
250
|
+
results[message.id] = result;
|
|
347
251
|
}
|
|
348
|
-
|
|
349
|
-
|
|
252
|
+
newMessages.push(
|
|
253
|
+
new import_runtime_client_gql.ResultMessage({
|
|
254
|
+
result: import_runtime_client_gql.ResultMessage.encodeResult(results[message.id]),
|
|
255
|
+
actionExecutionId: message.id,
|
|
256
|
+
actionName: message.name
|
|
257
|
+
})
|
|
258
|
+
);
|
|
350
259
|
}
|
|
351
260
|
}
|
|
352
261
|
}
|
|
262
|
+
if (newMessages.length > 0) {
|
|
263
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
264
|
+
}
|
|
353
265
|
}
|
|
354
|
-
if (
|
|
266
|
+
if (
|
|
267
|
+
// if we have client side results
|
|
268
|
+
Object.values(results).length || // or the last message we received is a result
|
|
269
|
+
newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
|
|
270
|
+
) {
|
|
355
271
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
356
|
-
return yield runChatCompletion([...
|
|
272
|
+
return yield runChatCompletion([...previousMessages, ...newMessages]);
|
|
357
273
|
} else {
|
|
358
274
|
return newMessages.slice();
|
|
359
275
|
}
|
|
360
276
|
} finally {
|
|
361
|
-
|
|
277
|
+
setIsLoading(false);
|
|
362
278
|
}
|
|
363
279
|
});
|
|
364
280
|
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
365
281
|
yield runChatCompletion(messages2);
|
|
366
282
|
});
|
|
367
283
|
const append = (message) => __async(this, null, function* () {
|
|
368
|
-
if (
|
|
284
|
+
if (isLoading) {
|
|
369
285
|
return;
|
|
370
286
|
}
|
|
371
287
|
const newMessages = [...messages, message];
|
|
@@ -373,12 +289,12 @@ function useChat(options) {
|
|
|
373
289
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
374
290
|
});
|
|
375
291
|
const reload = () => __async(this, null, function* () {
|
|
376
|
-
if (
|
|
292
|
+
if (isLoading || messages.length === 0) {
|
|
377
293
|
return;
|
|
378
294
|
}
|
|
379
295
|
let newMessages = [...messages];
|
|
380
296
|
const lastMessage = messages[messages.length - 1];
|
|
381
|
-
if (lastMessage.role === "assistant") {
|
|
297
|
+
if (lastMessage instanceof import_runtime_client_gql.TextMessage && lastMessage.role === "assistant") {
|
|
382
298
|
newMessages = newMessages.slice(0, -1);
|
|
383
299
|
}
|
|
384
300
|
setMessages(newMessages);
|
|
@@ -397,7 +313,7 @@ function useChat(options) {
|
|
|
397
313
|
|
|
398
314
|
// src/components/copilot-provider/copilotkit.tsx
|
|
399
315
|
var import_react3 = require("react");
|
|
400
|
-
var
|
|
316
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
401
317
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
402
318
|
var defaultCopilotContextCategories = ["global"];
|
|
403
319
|
|
|
@@ -410,14 +326,14 @@ function useCopilotChat(_a = {}) {
|
|
|
410
326
|
]);
|
|
411
327
|
const {
|
|
412
328
|
getContextString,
|
|
413
|
-
getChatCompletionFunctionDescriptions,
|
|
414
329
|
getFunctionCallHandler,
|
|
415
330
|
copilotApiConfig,
|
|
416
331
|
messages,
|
|
417
332
|
setMessages,
|
|
418
333
|
isLoading,
|
|
419
334
|
setIsLoading,
|
|
420
|
-
chatInstructions
|
|
335
|
+
chatInstructions,
|
|
336
|
+
actions
|
|
421
337
|
} = (0, import_react4.useContext)(CopilotContext);
|
|
422
338
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
423
339
|
const deleteMessage = (0, import_react4.useCallback)(
|
|
@@ -429,36 +345,24 @@ function useCopilotChat(_a = {}) {
|
|
|
429
345
|
const makeSystemMessageCallback = (0, import_react4.useCallback)(() => {
|
|
430
346
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
431
347
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
432
|
-
return {
|
|
433
|
-
id: "system",
|
|
348
|
+
return new import_runtime_client_gql2.TextMessage({
|
|
434
349
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
435
|
-
role:
|
|
436
|
-
};
|
|
350
|
+
role: import_runtime_client_gql2.Role.System
|
|
351
|
+
});
|
|
437
352
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
438
|
-
const functionDescriptions = (0, import_react4.useMemo)(() => {
|
|
439
|
-
return getChatCompletionFunctionDescriptions();
|
|
440
|
-
}, [getChatCompletionFunctionDescriptions]);
|
|
441
|
-
const tools = useUpdatedRef(functionDescriptions);
|
|
442
|
-
const onFunctionCall = useUpdatedRef(getFunctionCallHandler());
|
|
443
353
|
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
354
|
+
actions: Object.values(actions),
|
|
444
355
|
copilotConfig: copilotApiConfig,
|
|
445
|
-
id: options.id,
|
|
446
356
|
initialMessages: options.initialMessages || [],
|
|
447
|
-
|
|
448
|
-
onFunctionCall,
|
|
449
|
-
headers: __spreadValues({}, options.headers),
|
|
450
|
-
body: __spreadValues({}, options.body),
|
|
357
|
+
onFunctionCall: getFunctionCallHandler(),
|
|
451
358
|
messages,
|
|
452
359
|
setMessages,
|
|
453
360
|
makeSystemMessageCallback,
|
|
454
361
|
isLoading,
|
|
455
362
|
setIsLoading
|
|
456
363
|
}));
|
|
457
|
-
const visibleMessages = messages.filter(
|
|
458
|
-
(message) => message.role === "user" || message.role === "assistant" || message.role === "function"
|
|
459
|
-
);
|
|
460
364
|
return {
|
|
461
|
-
visibleMessages,
|
|
365
|
+
visibleMessages: messages,
|
|
462
366
|
appendMessage: append,
|
|
463
367
|
setMessages,
|
|
464
368
|
reloadMessages: reload,
|
|
@@ -500,11 +404,11 @@ ${additionalInstructions}` : "");
|
|
|
500
404
|
|
|
501
405
|
// src/hooks/use-make-copilot-actionable.ts
|
|
502
406
|
var import_react5 = require("react");
|
|
503
|
-
var
|
|
504
|
-
var
|
|
407
|
+
var import_nanoid = require("nanoid");
|
|
408
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
505
409
|
function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
506
|
-
const idRef = (0, import_react5.useRef)((0,
|
|
507
|
-
const {
|
|
410
|
+
const idRef = (0, import_react5.useRef)((0, import_nanoid.nanoid)());
|
|
411
|
+
const { setAction, removeAction } = (0, import_react5.useContext)(CopilotContext);
|
|
508
412
|
const memoizedAnnotatedFunction = (0, import_react5.useMemo)(
|
|
509
413
|
() => ({
|
|
510
414
|
name: annotatedFunction.name,
|
|
@@ -515,23 +419,23 @@ function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
|
515
419
|
dependencies
|
|
516
420
|
);
|
|
517
421
|
(0, import_react5.useEffect)(() => {
|
|
518
|
-
const action = (0,
|
|
519
|
-
|
|
422
|
+
const action = (0, import_shared3.annotatedFunctionToAction)(memoizedAnnotatedFunction);
|
|
423
|
+
setAction(idRef.current, action);
|
|
520
424
|
return () => {
|
|
521
|
-
|
|
425
|
+
removeAction(idRef.current);
|
|
522
426
|
};
|
|
523
|
-
}, [memoizedAnnotatedFunction,
|
|
427
|
+
}, [memoizedAnnotatedFunction, setAction, removeAction]);
|
|
524
428
|
}
|
|
525
429
|
|
|
526
430
|
// src/hooks/use-copilot-action.ts
|
|
527
431
|
var import_react6 = require("react");
|
|
528
|
-
var
|
|
432
|
+
var import_nanoid2 = require("nanoid");
|
|
529
433
|
function useCopilotAction(action, dependencies) {
|
|
530
|
-
const {
|
|
531
|
-
const idRef = (0, import_react6.useRef)((0,
|
|
434
|
+
const { setAction, removeAction, actions, chatComponentsCache } = (0, import_react6.useContext)(CopilotContext);
|
|
435
|
+
const idRef = (0, import_react6.useRef)((0, import_nanoid2.nanoid)());
|
|
532
436
|
if (dependencies === void 0) {
|
|
533
|
-
if (
|
|
534
|
-
|
|
437
|
+
if (actions[idRef.current]) {
|
|
438
|
+
actions[idRef.current].handler = action.handler;
|
|
535
439
|
if (typeof action.render === "function") {
|
|
536
440
|
if (chatComponentsCache.current !== null) {
|
|
537
441
|
chatComponentsCache.current[action.name] = action.render;
|
|
@@ -540,16 +444,16 @@ function useCopilotAction(action, dependencies) {
|
|
|
540
444
|
}
|
|
541
445
|
}
|
|
542
446
|
(0, import_react6.useEffect)(() => {
|
|
543
|
-
|
|
447
|
+
setAction(idRef.current, action);
|
|
544
448
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
545
449
|
chatComponentsCache.current[action.name] = action.render;
|
|
546
450
|
}
|
|
547
451
|
return () => {
|
|
548
|
-
|
|
452
|
+
removeAction(idRef.current);
|
|
549
453
|
};
|
|
550
454
|
}, [
|
|
551
|
-
|
|
552
|
-
|
|
455
|
+
setAction,
|
|
456
|
+
removeAction,
|
|
553
457
|
action.description,
|
|
554
458
|
action.name,
|
|
555
459
|
// This should be faster than deep equality checking
|