@copilotkit/react-core 1.4.8-next.2 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -13
- package/dist/{chunk-DIX6ISOB.mjs → chunk-6EMLM6WX.mjs} +20 -19
- package/dist/chunk-6EMLM6WX.mjs.map +1 -0
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-XXCJFOLY.mjs → chunk-AG7FH7OD.mjs} +2 -2
- package/dist/{chunk-XYFTGCMS.mjs → chunk-FSC4A3JN.mjs} +18 -14
- package/dist/chunk-FSC4A3JN.mjs.map +1 -0
- package/dist/{chunk-E3HHCHMQ.mjs → chunk-IFTHM7LF.mjs} +7 -10
- package/dist/chunk-IFTHM7LF.mjs.map +1 -0
- package/dist/{chunk-V6CEKRZA.mjs → chunk-IVYL7JRC.mjs} +12 -11
- package/dist/chunk-IVYL7JRC.mjs.map +1 -0
- package/dist/chunk-NTLCOVE5.mjs +321 -0
- package/dist/chunk-NTLCOVE5.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-CBMCK7UC.mjs → chunk-UOVONDR6.mjs} +2 -2
- package/dist/{chunk-XSORW54K.mjs → chunk-X6ZF5WAX.mjs} +2 -2
- package/dist/{chunk-4P56GAFP.mjs → chunk-XQFVXX6R.mjs} +2 -3
- package/dist/chunk-XQFVXX6R.mjs.map +1 -0
- package/dist/{chunk-ETCKRNXQ.mjs → chunk-YUY5ZAST.mjs} +7 -4
- package/dist/chunk-YUY5ZAST.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -5
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit.js +158 -95
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -4
- package/dist/components/copilot-provider/index.d.ts +0 -1
- package/dist/components/copilot-provider/index.js +158 -95
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -4
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.d.ts +0 -1
- package/dist/components/index.js +158 -95
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -4
- package/dist/components/toast/toast-provider.d.ts +2 -1
- package/dist/components/toast/toast-provider.js +76 -62
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/context/copilot-context.d.ts +1 -5
- package/dist/context/copilot-context.js +1 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.js +1 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +401 -260
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -18
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +343 -200
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.js +1 -2
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-coagent.js +381 -240
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +9 -8
- package/dist/hooks/use-copilot-action.js +133 -9
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -2
- package/dist/hooks/use-copilot-chat.js +370 -230
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -7
- package/dist/hooks/use-copilot-readable.js +1 -2
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +110 -4
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +1 -2
- 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/index.d.ts +1 -1
- package/dist/index.js +450 -353
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -19
- package/dist/lib/copilot-task.d.ts +1 -6
- package/dist/lib/copilot-task.js +4 -22
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +4 -22
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -5
- package/dist/utils/extract.d.ts +2 -3
- package/dist/utils/extract.js +3 -21
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +5 -4
- package/dist/utils/index.js +3 -21
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -4
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit-props.tsx +0 -6
- package/src/components/copilot-provider/copilotkit.tsx +4 -2
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/toast/toast-provider.tsx +10 -49
- package/src/context/copilot-context.tsx +1 -8
- package/src/hooks/use-chat.ts +286 -266
- package/src/hooks/use-coagent.ts +14 -11
- package/src/hooks/use-copilot-action.ts +3 -2
- package/src/hooks/use-copilot-chat.ts +9 -10
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -10
- package/src/utils/extract.ts +0 -4
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-4P56GAFP.mjs.map +0 -1
- package/dist/chunk-DIX6ISOB.mjs.map +0 -1
- package/dist/chunk-E3HHCHMQ.mjs.map +0 -1
- package/dist/chunk-ETCKRNXQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-PG5XEJVS.mjs +0 -295
- package/dist/chunk-PG5XEJVS.mjs.map +0 -1
- package/dist/chunk-V6CEKRZA.mjs.map +0 -1
- package/dist/chunk-XYFTGCMS.mjs.map +0 -1
- /package/dist/{chunk-XXCJFOLY.mjs.map → chunk-AG7FH7OD.mjs.map} +0 -0
- /package/dist/{chunk-CBMCK7UC.mjs.map → chunk-UOVONDR6.mjs.map} +0 -0
- /package/dist/{chunk-XSORW54K.mjs.map → chunk-X6ZF5WAX.mjs.map} +0 -0
|
@@ -56,8 +56,7 @@ var emptyCopilotContext = {
|
|
|
56
56
|
},
|
|
57
57
|
agentSession: null,
|
|
58
58
|
setAgentSession: () => {
|
|
59
|
-
}
|
|
60
|
-
forwardedParameters: {}
|
|
59
|
+
}
|
|
61
60
|
};
|
|
62
61
|
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
63
62
|
function useCopilotContext() {
|
|
@@ -76,4 +75,4 @@ export {
|
|
|
76
75
|
CopilotContext,
|
|
77
76
|
useCopilotContext
|
|
78
77
|
};
|
|
79
|
-
//# sourceMappingURL=chunk-
|
|
78
|
+
//# sourceMappingURL=chunk-XQFVXX6R.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/context/copilot-context.tsx"],"sourcesContent":["import { CopilotCloudConfig, FunctionCallHandler } from \"@copilotkit/shared\";\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\";\nimport { CoAgentStateRender, CoAgentStateRenderProps } from \"../types/coagent-action\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { CopilotRuntimeClient } from \"@copilotkit/runtime-client-gql\";\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 * 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 * Custom properties to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'user_id': 'user_id'\n * }\n * ```\n */\n properties?: 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;\nexport type CoagentInChatRenderFunction = (\n props: CoAgentStateRenderProps<any>,\n) => string | JSX.Element | undefined | null;\n\nexport interface ChatComponentsCache {\n actions: Record<string, InChatRenderFunction | string>;\n coAgentStateRenders: Record<string, CoagentInChatRenderFunction | string>;\n}\n\nexport interface AgentSession {\n agentName: string;\n threadId?: string;\n nodeName?: string;\n}\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\n // coagent actions\n coAgentStateRenders: Record<string, CoAgentStateRender<any>>;\n setCoAgentStateRender: (id: string, stateRender: CoAgentStateRender<any>) => void;\n removeCoAgentStateRender: (id: string) => void;\n\n chatComponentsCache: React.RefObject<ChatComponentsCache>;\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 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 showDevConsole: boolean | \"auto\";\n\n // agents\n coagentStates: Record<string, CoagentState>;\n setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;\n coagentStatesRef: React.RefObject<Record<string, CoagentState>>;\n setCoagentStatesWithRef: (\n value:\n | Record<string, CoagentState>\n | ((prev: Record<string, CoagentState>) => Record<string, CoagentState>),\n ) => void;\n\n agentSession: AgentSession | null;\n setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;\n\n // runtime\n runtimeClient: CopilotRuntimeClient;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n actions: {},\n setAction: () => {},\n removeAction: () => {},\n\n coAgentStateRenders: {},\n setCoAgentStateRender: () => {},\n removeCoAgentStateRender: () => {},\n\n chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\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 runtimeClient: {} as any,\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\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 showDevConsole: \"auto\",\n coagentStates: {},\n setCoagentStates: () => {},\n coagentStatesRef: { current: {} },\n setCoagentStatesWithRef: () => {},\n\n agentSession: null,\n setAgentSession: () => {},\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nexport function useCopilotContext(): CopilotContextParams {\n const context = React.useContext(CopilotContext);\n if (context === emptyCopilotContext) {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n return context;\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":";;;;;AAEA,OAAO,WAAW;AAkJlB,IAAM,sBAA4C;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,cAAc,MAAM;AAAA,EAAC;AAAA,EAErB,qBAAqB,CAAC;AAAA,EACtB,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAC9B,0BAA0B,MAAM;AAAA,EAAC;AAAA,EAEjC,qBAAqB,EAAE,SAAS,EAAE,SAAS,CAAC,GAAG,qBAAqB,CAAC,EAAE,EAAE;AAAA,EACzE,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,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,EAC9B,eAAe,CAAC;AAAA,EAEhB,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IAEA,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;AAAA,EAC1C,gBAAgB;AAAA,EAChB,eAAe,CAAC;AAAA,EAChB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,kBAAkB,EAAE,SAAS,CAAC,EAAE;AAAA,EAChC,yBAAyB,MAAM;AAAA,EAAC;AAAA,EAEhC,cAAc;AAAA,EACd,iBAAiB,MAAM;AAAA,EAAC;AAC1B;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAEpF,SAAS,oBAA0C;AACxD,QAAM,UAAU,MAAM,WAAW,cAAc;AAC/C,MAAI,YAAY,qBAAqB;AACnC,UAAM,IAAI,MAAM,uEAAuE;AAAA,EACzF;AACA,SAAO;AACT;AAEA,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;","names":[]}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAsyncCallback
|
|
3
|
+
} from "./chunk-SFPANIOY.mjs";
|
|
1
4
|
import {
|
|
2
5
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-XQFVXX6R.mjs";
|
|
4
7
|
import {
|
|
5
8
|
__async,
|
|
6
9
|
__spreadValues
|
|
@@ -18,7 +21,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
18
21
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
19
22
|
action.renderAndWait = void 0;
|
|
20
23
|
action.renderAndWaitForResponse = void 0;
|
|
21
|
-
action.handler = () => __async(this, null, function* () {
|
|
24
|
+
action.handler = useAsyncCallback(() => __async(this, null, function* () {
|
|
22
25
|
let resolve;
|
|
23
26
|
let reject;
|
|
24
27
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -27,7 +30,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
27
30
|
});
|
|
28
31
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
29
32
|
return yield promise;
|
|
30
|
-
});
|
|
33
|
+
}), []);
|
|
31
34
|
action.render = (props) => {
|
|
32
35
|
const waitProps = {
|
|
33
36
|
status: props.status,
|
|
@@ -88,4 +91,4 @@ function useCopilotAction(action, dependencies) {
|
|
|
88
91
|
export {
|
|
89
92
|
useCopilotAction
|
|
90
93
|
};
|
|
91
|
-
//# sourceMappingURL=chunk-
|
|
94
|
+
//# sourceMappingURL=chunk-YUY5ZAST.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-copilot-action.ts"],"sourcesContent":["/**\n * Example usage of useCopilotAction with complex parameters:\n *\n * @example\n * useCopilotAction({\n * name: \"myAction\",\n * parameters: [\n * { name: \"arg1\", type: \"string\", enum: [\"option1\", \"option2\", \"option3\"], required: false },\n * { name: \"arg2\", type: \"number\" },\n * {\n * name: \"arg3\",\n * type: \"object\",\n * attributes: [\n * { name: \"nestedArg1\", type: \"boolean\" },\n * { name: \"xyz\", required: false },\n * ],\n * },\n * { name: \"arg4\", type: \"number[]\" },\n * ],\n * handler: ({ arg1, arg2, arg3, arg4 }) => {\n * const x = arg3.nestedArg1;\n * const z = arg3.xyz;\n * console.log(arg1, arg2, arg3);\n * },\n * });\n *\n * @example\n * // Simple action without parameters\n * useCopilotAction({\n * name: \"myAction\",\n * handler: () => {\n * console.log(\"No parameters provided.\");\n * },\n * });\n *\n * @example\n * // Interactive action with UI rendering and response handling\n * useCopilotAction({\n * name: \"handleMeeting\",\n * description: \"Handle a meeting by booking or canceling\",\n * parameters: [\n * {\n * name: \"meeting\",\n * type: \"string\",\n * description: \"The meeting to handle\",\n * required: true,\n * },\n * {\n * name: \"date\",\n * type: \"string\",\n * description: \"The date of the meeting\",\n * required: true,\n * },\n * {\n * name: \"title\",\n * type: \"string\",\n * description: \"The title of the meeting\",\n * required: true,\n * },\n * ],\n * renderAndWaitForResponse: ({ args, respond, status }) => {\n * const { meeting, date, title } = args;\n * return (\n * <MeetingConfirmationDialog\n * meeting={meeting}\n * date={date}\n * title={title}\n * onConfirm={() => respond('meeting confirmed')}\n * onCancel={() => respond('meeting canceled')}\n * />\n * );\n * },\n * });\n */\n\n/**\n * <img src=\"/images/use-copilot-action/useCopilotAction.gif\" width=\"500\" />\n * `useCopilotAction` is a React hook that you can use in your application to provide\n * custom actions that can be called by the AI. Essentially, it allows the Copilot to\n * execute these actions contextually during a chat, based on the user's interactions\n * and needs.\n *\n * Here's how it works:\n *\n * Use `useCopilotAction` to set up actions that the Copilot can call. To provide\n * more context to the Copilot, you can provide it with a `description` (for example to explain\n * what the action does, under which conditions it can be called, etc.).\n *\n * Then you define the parameters of the action, which can be simple, e.g. primitives like strings or numbers,\n * or complex, e.g. objects or arrays.\n *\n * Finally, you provide a `handler` function that receives the parameters and returns a result.\n * CopilotKit takes care of automatically inferring the parameter types, so you get type safety\n * and autocompletion for free.\n *\n * To render a custom UI for the action, you can provide a `render()` function. This function\n * lets you render a custom component or return a string to display.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * useCopilotAction({\n * name: \"sayHello\",\n * description: \"Say hello to someone.\",\n * parameters: [\n * {\n * name: \"name\",\n * type: \"string\",\n * description: \"name of the person to say greet\",\n * },\n * ],\n * handler: async ({ name }) => {\n * alert(`Hello, ${name}!`);\n * },\n * });\n * ```\n *\n * ## Generative UI\n *\n * This hooks enables you to dynamically generate UI elements and render them in the copilot chat. For more information, check out the [Generative UI](/guides/generative-ui) page.\n */\nimport { Parameter, randomId } from \"@copilotkit/shared\";\nimport { createElement, Fragment, useEffect, useRef } from \"react\";\nimport { useCopilotContext } from \"../context/copilot-context\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport {\n ActionRenderProps,\n ActionRenderPropsNoArgsWait,\n ActionRenderPropsWait,\n FrontendAction,\n} from \"../types/frontend-action\";\n\n// We implement useCopilotAction dependency handling so that\n// the developer has the option to not provide any dependencies.\n// In this case, we assume they want to update the handler on each rerender.\n// To avoid getting stuck in an infinite loop, we update the handler directly,\n// skipping React state updates.\n// This is ok in this case, because the handler is not part of any UI that\n// needs to be updated.\n// useCallback, useMemo or other memoization techniques are not suitable here,\n// because they will cause a infinite rerender loop.\nexport function useCopilotAction<const T extends Parameter[] | [] = []>(\n action: FrontendAction<T>,\n dependencies?: any[],\n): void {\n const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();\n const idRef = useRef<string>(randomId());\n const renderAndWaitRef = useRef<RenderAndWaitForResponse | null>(null);\n\n // clone the action to avoid mutating the original object\n action = { ...action };\n\n // If the developer provides a renderAndWait function, we transform the action\n // to use a promise internally, so that we can treat it like a normal action.\n if (action.renderAndWait || action.renderAndWaitForResponse) {\n const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;\n // remove the renderAndWait function from the action\n action.renderAndWait = undefined;\n action.renderAndWaitForResponse = undefined;\n // add a handler that will be called when the action is executed\n action.handler = useAsyncCallback(async () => {\n // we create a new promise when the handler is called\n let resolve: (result: any) => void;\n let reject: (error: any) => void;\n const promise = new Promise<any>((resolvePromise, rejectPromise) => {\n resolve = resolvePromise;\n reject = rejectPromise;\n });\n renderAndWaitRef.current = { promise, resolve: resolve!, reject: reject! };\n // then we await the promise (it will be resolved in the original renderAndWait function)\n return await promise;\n }, []) as any;\n\n // add a render function that will be called when the action is rendered\n action.render = ((props: ActionRenderProps<T>): React.ReactElement => {\n // Create type safe waitProps based on whether T extends empty array or not\n const waitProps = {\n status: props.status,\n args: props.args,\n result: props.result,\n handler: props.status === \"executing\" ? renderAndWaitRef.current!.resolve : undefined,\n respond: props.status === \"executing\" ? renderAndWaitRef.current!.resolve : undefined,\n } as T extends [] ? ActionRenderPropsNoArgsWait<T> : ActionRenderPropsWait<T>;\n\n // Type guard to check if renderAndWait is for no args case\n const isNoArgsRenderWait = (\n _fn:\n | ((props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement)\n | ((props: ActionRenderPropsWait<T>) => React.ReactElement),\n ): _fn is (props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement => {\n return action.parameters?.length === 0;\n };\n\n // Safely call renderAndWait with correct props type\n if (renderAndWait) {\n if (isNoArgsRenderWait(renderAndWait)) {\n return renderAndWait(waitProps as ActionRenderPropsNoArgsWait<T>);\n } else {\n return renderAndWait(waitProps as ActionRenderPropsWait<T>);\n }\n }\n\n // Return empty Fragment instead of null\n return createElement(Fragment);\n }) as any;\n }\n\n // If the developer doesn't provide dependencies, we assume they want to\n // update handler and render function when the action object changes.\n // This ensures that any captured variables in the handler are up to date.\n if (dependencies === undefined) {\n if (actions[idRef.current]) {\n actions[idRef.current].handler = action.handler as any;\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n }\n }\n }\n\n useEffect(() => {\n setAction(idRef.current, action as any);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n return () => {\n // NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.\n // This is because we currently don't have access to the messages array in CopilotContext.\n removeAction(idRef.current);\n };\n }, [\n setAction,\n removeAction,\n action.description,\n action.name,\n action.disabled,\n action.available,\n // This should be faster than deep equality checking\n // In addition, all major JS engines guarantee the order of object keys\n JSON.stringify(action.parameters),\n // include render only if it's a string\n typeof action.render === \"string\" ? action.render : undefined,\n // dependencies set by the developer\n ...(dependencies || []),\n ]);\n}\n\ninterface RenderAndWaitForResponse {\n promise: Promise<any>;\n resolve: (result: any) => void;\n reject: (error: any) => void;\n}\n"],"mappings":";;;;;;;;;;;;AA2HA,SAAoB,gBAAgB;AACpC,SAAS,eAAe,UAAU,WAAW,cAAc;AAmBpD,SAAS,iBACd,QACA,cACM;AACN,QAAM,EAAE,WAAW,cAAc,SAAS,oBAAoB,IAAI,kBAAkB;AACpF,QAAM,QAAQ,OAAe,SAAS,CAAC;AACvC,QAAM,mBAAmB,OAAwC,IAAI;AAGrE,WAAS,mBAAK;AAId,MAAI,OAAO,iBAAiB,OAAO,0BAA0B;AAC3D,UAAM,gBAAgB,OAAO,iBAAiB,OAAO;AAErD,WAAO,gBAAgB;AACvB,WAAO,2BAA2B;AAElC,WAAO,UAAU,iBAAiB,MAAY;AAE5C,UAAI;AACJ,UAAI;AACJ,YAAM,UAAU,IAAI,QAAa,CAAC,gBAAgB,kBAAkB;AAClE,kBAAU;AACV,iBAAS;AAAA,MACX,CAAC;AACD,uBAAiB,UAAU,EAAE,SAAS,SAAmB,OAAgB;AAEzE,aAAO,MAAM;AAAA,IACf,IAAG,CAAC,CAAC;AAGL,WAAO,SAAU,CAAC,UAAoD;AAEpE,YAAM,YAAY;AAAA,QAChB,QAAQ,MAAM;AAAA,QACd,MAAM,MAAM;AAAA,QACZ,QAAQ,MAAM;AAAA,QACd,SAAS,MAAM,WAAW,cAAc,iBAAiB,QAAS,UAAU;AAAA,QAC5E,SAAS,MAAM,WAAW,cAAc,iBAAiB,QAAS,UAAU;AAAA,MAC9E;AAGA,YAAM,qBAAqB,CACzB,QAGyE;AA/LjF;AAgMQ,iBAAO,YAAO,eAAP,mBAAmB,YAAW;AAAA,MACvC;AAGA,UAAI,eAAe;AACjB,YAAI,mBAAmB,aAAa,GAAG;AACrC,iBAAO,cAAc,SAA2C;AAAA,QAClE,OAAO;AACL,iBAAO,cAAc,SAAqC;AAAA,QAC5D;AAAA,MACF;AAGA,aAAO,cAAc,QAAQ;AAAA,IAC/B;AAAA,EACF;AAKA,MAAI,iBAAiB,QAAW;AAC9B,QAAI,QAAQ,MAAM,OAAO,GAAG;AAC1B,cAAQ,MAAM,OAAO,EAAE,UAAU,OAAO;AACxC,UAAI,OAAO,OAAO,WAAW,YAAY;AACvC,YAAI,oBAAoB,YAAY,MAAM;AACxC,8BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,QAC5D;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,cAAU,MAAM,SAAS,MAAa;AACtC,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AACvE,0BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,IAC5D;AACA,WAAO,MAAM;AAGX,mBAAa,MAAM,OAAO;AAAA,IAC5B;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA;AAAA;AAAA,IAGP,KAAK,UAAU,OAAO,UAAU;AAAA;AAAA,IAEhC,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":[]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { CopilotKitProps } from './copilotkit-props.js';
|
|
3
|
-
import '@copilotkit/runtime-client-gql';
|
|
4
3
|
import 'react';
|
|
5
4
|
|
|
6
5
|
declare function CopilotMessages({ children, ...props }: CopilotKitProps): react_jsx_runtime.JSX.Element;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ForwardedParametersInput } from '@copilotkit/runtime-client-gql';
|
|
2
1
|
import { ReactNode } from 'react';
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -68,10 +67,6 @@ interface CopilotKitProps {
|
|
|
68
67
|
* The name of the agent to use.
|
|
69
68
|
*/
|
|
70
69
|
agent?: string;
|
|
71
|
-
/**
|
|
72
|
-
* The forwarded parameters to use for the task.
|
|
73
|
-
*/
|
|
74
|
-
forwardedParameters?: Pick<ForwardedParametersInput, "temperature">;
|
|
75
70
|
}
|
|
76
71
|
|
|
77
72
|
export { CopilotKitProps };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/components/copilot-provider/copilotkit-props.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/components/copilot-provider/copilotkit-props.tsx"],"sourcesContent":["import { ReactNode } from \"react\";\n\n/**\n * Props for CopilotKit.\n */\n\nexport interface CopilotKitProps {\n /**\n * Your Copilot Cloud API key. Don't have it yet? Go to https://cloud.copilotkit.ai and get one for free.\n */\n publicApiKey?: string;\n\n /**\n * Restrict input to a specific topic.\n */\n cloudRestrictToTopic?: {\n validTopics?: string[];\n invalidTopics?: string[];\n };\n\n /**\n * The endpoint for the Copilot Runtime instance. [Click here for more information](/concepts/copilot-runtime).\n */\n runtimeUrl?: 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 * Additional headers to be sent with the request.\n *\n * For example:\n * ```json\n * {\n * \"Authorization\": \"Bearer X\"\n * }\n * ```\n */\n headers?: Record<string, string>;\n\n /**\n * The children to be rendered within the CopilotKit.\n */\n children: ReactNode;\n\n /**\n * Custom properties to be sent with the request\n * For example:\n * ```js\n * {\n * 'user_id': 'users_id',\n * }\n * ```\n */\n properties?: 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 /**\n * Whether to show the dev console.\n *\n * If set to \"auto\", the dev console will be show on localhost only.\n */\n showDevConsole?: boolean | \"auto\";\n\n /**\n * The name of the agent to use.\n */\n agent?: string;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CopilotKitProps } from './copilotkit-props.js';
|
|
2
2
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
-
import '@copilotkit/runtime-client-gql';
|
|
4
3
|
import 'react';
|
|
5
4
|
|
|
6
5
|
declare function CopilotKit({ children, ...props }: CopilotKitProps): react_jsx_runtime.JSX.Element;
|
|
@@ -85,7 +85,7 @@ __export(copilotkit_exports, {
|
|
|
85
85
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories
|
|
86
86
|
});
|
|
87
87
|
module.exports = __toCommonJS(copilotkit_exports);
|
|
88
|
-
var
|
|
88
|
+
var import_react10 = require("react");
|
|
89
89
|
|
|
90
90
|
// src/context/copilot-context.tsx
|
|
91
91
|
var import_react = __toESM(require("react"));
|
|
@@ -141,8 +141,7 @@ var emptyCopilotContext = {
|
|
|
141
141
|
},
|
|
142
142
|
agentSession: null,
|
|
143
143
|
setAgentSession: () => {
|
|
144
|
-
}
|
|
145
|
-
forwardedParameters: {}
|
|
144
|
+
}
|
|
146
145
|
};
|
|
147
146
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
148
147
|
function returnAndThrowInDebug(value) {
|
|
@@ -380,6 +379,9 @@ function CopilotMessages(_a) {
|
|
|
380
379
|
}
|
|
381
380
|
|
|
382
381
|
// src/components/toast/toast-provider.tsx
|
|
382
|
+
var import_react7 = require("react");
|
|
383
|
+
|
|
384
|
+
// src/components/error-boundary/error-utils.tsx
|
|
383
385
|
var import_react6 = require("react");
|
|
384
386
|
|
|
385
387
|
// src/components/toast/exclamation-mark-icon.tsx
|
|
@@ -409,11 +411,82 @@ var ExclamationMarkIcon = ({
|
|
|
409
411
|
}
|
|
410
412
|
);
|
|
411
413
|
|
|
412
|
-
// src/components/
|
|
414
|
+
// src/components/error-boundary/error-utils.tsx
|
|
413
415
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
414
|
-
|
|
416
|
+
function ErrorToast({ errors }) {
|
|
417
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
418
|
+
var _a, _b, _c;
|
|
419
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
420
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
421
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
422
|
+
"div",
|
|
423
|
+
{
|
|
424
|
+
style: {
|
|
425
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
426
|
+
marginBottom: 14
|
|
427
|
+
},
|
|
428
|
+
children: [
|
|
429
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
430
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
431
|
+
"div",
|
|
432
|
+
{
|
|
433
|
+
style: {
|
|
434
|
+
fontWeight: "600",
|
|
435
|
+
marginBottom: 4
|
|
436
|
+
},
|
|
437
|
+
children: [
|
|
438
|
+
"Copilot Cloud Error:",
|
|
439
|
+
" ",
|
|
440
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
441
|
+
]
|
|
442
|
+
}
|
|
443
|
+
),
|
|
444
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
445
|
+
]
|
|
446
|
+
},
|
|
447
|
+
idx
|
|
448
|
+
);
|
|
449
|
+
});
|
|
450
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
451
|
+
"div",
|
|
452
|
+
{
|
|
453
|
+
style: {
|
|
454
|
+
fontSize: "13px",
|
|
455
|
+
maxWidth: "600px"
|
|
456
|
+
},
|
|
457
|
+
children: [
|
|
458
|
+
errorsToRender,
|
|
459
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
460
|
+
]
|
|
461
|
+
}
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
function useErrorToast() {
|
|
465
|
+
const { addToast } = useToast();
|
|
466
|
+
return (0, import_react6.useCallback)(
|
|
467
|
+
(error) => {
|
|
468
|
+
const errorId = error.map((err) => {
|
|
469
|
+
var _a, _b;
|
|
470
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
471
|
+
const stack = err.stack || "";
|
|
472
|
+
return btoa(message + stack).slice(0, 32);
|
|
473
|
+
}).join("|");
|
|
474
|
+
addToast({
|
|
475
|
+
type: "error",
|
|
476
|
+
id: errorId,
|
|
477
|
+
// Toast libraries typically dedupe by id
|
|
478
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
[addToast]
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// src/components/toast/toast-provider.tsx
|
|
486
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
487
|
+
var ToastContext = (0, import_react7.createContext)(void 0);
|
|
415
488
|
function useToast() {
|
|
416
|
-
const context = (0,
|
|
489
|
+
const context = (0, import_react7.useContext)(ToastContext);
|
|
417
490
|
if (!context) {
|
|
418
491
|
throw new Error("useToast must be used within a ToastProvider");
|
|
419
492
|
}
|
|
@@ -423,68 +496,28 @@ function ToastProvider({
|
|
|
423
496
|
enabled,
|
|
424
497
|
children
|
|
425
498
|
}) {
|
|
426
|
-
const [toasts, setToasts] = (0,
|
|
427
|
-
const addToast = (0,
|
|
428
|
-
|
|
429
|
-
|
|
499
|
+
const [toasts, setToasts] = (0, import_react7.useState)([]);
|
|
500
|
+
const addToast = (0, import_react7.useCallback)((toast) => {
|
|
501
|
+
var _a;
|
|
502
|
+
const id = (_a = toast.id) != null ? _a : Math.random().toString(36).substring(2, 9);
|
|
503
|
+
setToasts((currentToasts) => {
|
|
504
|
+
if (currentToasts.find((toast2) => toast2.id === id))
|
|
505
|
+
return currentToasts;
|
|
506
|
+
return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
|
|
507
|
+
});
|
|
430
508
|
if (toast.duration) {
|
|
431
509
|
setTimeout(() => {
|
|
432
510
|
removeToast(id);
|
|
433
511
|
}, toast.duration);
|
|
434
512
|
}
|
|
435
513
|
}, []);
|
|
436
|
-
const addGraphQLErrorsToast = (0,
|
|
437
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
438
|
-
var _a;
|
|
439
|
-
const message = error.message;
|
|
440
|
-
const code = (_a = error.extensions) == null ? void 0 : _a.code;
|
|
441
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
442
|
-
"div",
|
|
443
|
-
{
|
|
444
|
-
style: {
|
|
445
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
446
|
-
marginBottom: 14
|
|
447
|
-
},
|
|
448
|
-
children: [
|
|
449
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
450
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
451
|
-
"div",
|
|
452
|
-
{
|
|
453
|
-
style: {
|
|
454
|
-
fontWeight: "600",
|
|
455
|
-
marginBottom: 4
|
|
456
|
-
},
|
|
457
|
-
children: [
|
|
458
|
-
"Copilot Cloud Error:",
|
|
459
|
-
" ",
|
|
460
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
461
|
-
]
|
|
462
|
-
}
|
|
463
|
-
),
|
|
464
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
465
|
-
]
|
|
466
|
-
},
|
|
467
|
-
idx
|
|
468
|
-
);
|
|
469
|
-
});
|
|
514
|
+
const addGraphQLErrorsToast = (0, import_react7.useCallback)((errors) => {
|
|
470
515
|
addToast({
|
|
471
516
|
type: "error",
|
|
472
|
-
message: /* @__PURE__ */ (0,
|
|
473
|
-
"div",
|
|
474
|
-
{
|
|
475
|
-
style: {
|
|
476
|
-
fontSize: "13px",
|
|
477
|
-
maxWidth: "600px"
|
|
478
|
-
},
|
|
479
|
-
children: [
|
|
480
|
-
errorsToRender,
|
|
481
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This is a Copilot Cloud error, and it only displays during local development." })
|
|
482
|
-
]
|
|
483
|
-
}
|
|
484
|
-
)
|
|
517
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorToast, { errors })
|
|
485
518
|
});
|
|
486
519
|
}, []);
|
|
487
|
-
const removeToast = (0,
|
|
520
|
+
const removeToast = (0, import_react7.useCallback)((id) => {
|
|
488
521
|
setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
|
|
489
522
|
}, []);
|
|
490
523
|
const value = {
|
|
@@ -494,8 +527,8 @@ function ToastProvider({
|
|
|
494
527
|
removeToast,
|
|
495
528
|
enabled
|
|
496
529
|
};
|
|
497
|
-
return /* @__PURE__ */ (0,
|
|
498
|
-
/* @__PURE__ */ (0,
|
|
530
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ToastContext.Provider, { value, children: [
|
|
531
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
499
532
|
"div",
|
|
500
533
|
{
|
|
501
534
|
style: {
|
|
@@ -509,7 +542,7 @@ function ToastProvider({
|
|
|
509
542
|
gap: "0.5rem"
|
|
510
543
|
},
|
|
511
544
|
children: [
|
|
512
|
-
toasts.length > 1 && /* @__PURE__ */ (0,
|
|
545
|
+
toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
513
546
|
"button",
|
|
514
547
|
{
|
|
515
548
|
onClick: () => setToasts([]),
|
|
@@ -524,7 +557,7 @@ function ToastProvider({
|
|
|
524
557
|
children: "Close All"
|
|
525
558
|
}
|
|
526
559
|
) }),
|
|
527
|
-
toasts.map((toast) => /* @__PURE__ */ (0,
|
|
560
|
+
toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
528
561
|
Toast,
|
|
529
562
|
{
|
|
530
563
|
message: toast.message,
|
|
@@ -550,7 +583,7 @@ function Toast({
|
|
|
550
583
|
warning: "#eab308",
|
|
551
584
|
error: "#ef4444"
|
|
552
585
|
};
|
|
553
|
-
return /* @__PURE__ */ (0,
|
|
586
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
554
587
|
"div",
|
|
555
588
|
{
|
|
556
589
|
style: {
|
|
@@ -563,8 +596,8 @@ function Toast({
|
|
|
563
596
|
minWidth: "200px"
|
|
564
597
|
},
|
|
565
598
|
children: [
|
|
566
|
-
/* @__PURE__ */ (0,
|
|
567
|
-
/* @__PURE__ */ (0,
|
|
599
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: message }),
|
|
600
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
568
601
|
"button",
|
|
569
602
|
{
|
|
570
603
|
onClick: onClose,
|
|
@@ -589,14 +622,17 @@ function Toast({
|
|
|
589
622
|
|
|
590
623
|
// src/hooks/use-copilot-runtime-client.ts
|
|
591
624
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
592
|
-
var
|
|
625
|
+
var import_react8 = require("react");
|
|
593
626
|
var useCopilotRuntimeClient = (options) => {
|
|
594
627
|
const { addGraphQLErrorsToast } = useToast();
|
|
595
|
-
const
|
|
628
|
+
const addErrorToast = useErrorToast();
|
|
629
|
+
const runtimeClient = (0, import_react8.useMemo)(() => {
|
|
596
630
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
597
631
|
handleGQLErrors: (error) => {
|
|
598
632
|
if (error.graphQLErrors.length) {
|
|
599
633
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
634
|
+
} else {
|
|
635
|
+
addErrorToast([error]);
|
|
600
636
|
}
|
|
601
637
|
}
|
|
602
638
|
}));
|
|
@@ -618,13 +654,41 @@ function getHostname() {
|
|
|
618
654
|
return "";
|
|
619
655
|
}
|
|
620
656
|
|
|
657
|
+
// src/components/error-boundary/error-boundary.tsx
|
|
658
|
+
var import_react9 = __toESM(require("react"));
|
|
659
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
660
|
+
var CopilotErrorBoundary = class extends import_react9.default.Component {
|
|
661
|
+
constructor(props) {
|
|
662
|
+
super(props);
|
|
663
|
+
this.state = { hasError: false };
|
|
664
|
+
}
|
|
665
|
+
static getDerivedStateFromError(error) {
|
|
666
|
+
return { hasError: true, error };
|
|
667
|
+
}
|
|
668
|
+
componentDidCatch(error, errorInfo) {
|
|
669
|
+
console.error("CopilotKit Error:", error, errorInfo);
|
|
670
|
+
}
|
|
671
|
+
render() {
|
|
672
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
function ErrorToast2({ error, children }) {
|
|
676
|
+
const addErrorToast = useErrorToast();
|
|
677
|
+
(0, import_react9.useEffect)(() => {
|
|
678
|
+
if (error) {
|
|
679
|
+
addErrorToast([error]);
|
|
680
|
+
}
|
|
681
|
+
}, [error, addErrorToast]);
|
|
682
|
+
return children;
|
|
683
|
+
}
|
|
684
|
+
|
|
621
685
|
// src/components/copilot-provider/copilotkit.tsx
|
|
622
|
-
var
|
|
686
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
623
687
|
function CopilotKit(_a) {
|
|
624
688
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
625
689
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
626
690
|
const enabled = shouldShowDevConsole(showDevConsole);
|
|
627
|
-
return /* @__PURE__ */ (0,
|
|
691
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) }) });
|
|
628
692
|
}
|
|
629
693
|
function CopilotKitInternal(_a) {
|
|
630
694
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
@@ -634,49 +698,49 @@ function CopilotKitInternal(_a) {
|
|
|
634
698
|
);
|
|
635
699
|
}
|
|
636
700
|
const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
|
|
637
|
-
const [actions, setActions] = (0,
|
|
638
|
-
const [coAgentStateRenders, setCoAgentStateRenders] = (0,
|
|
639
|
-
const chatComponentsCache = (0,
|
|
701
|
+
const [actions, setActions] = (0, import_react10.useState)({});
|
|
702
|
+
const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react10.useState)({});
|
|
703
|
+
const chatComponentsCache = (0, import_react10.useRef)({
|
|
640
704
|
actions: {},
|
|
641
705
|
coAgentStateRenders: {}
|
|
642
706
|
});
|
|
643
707
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
644
|
-
const [isLoading, setIsLoading] = (0,
|
|
645
|
-
const [chatInstructions, setChatInstructions] = (0,
|
|
708
|
+
const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
|
|
709
|
+
const [chatInstructions, setChatInstructions] = (0, import_react10.useState)("");
|
|
646
710
|
const {
|
|
647
711
|
addElement: addDocument,
|
|
648
712
|
removeElement: removeDocument,
|
|
649
713
|
allElements: allDocuments
|
|
650
714
|
} = use_flat_category_store_default();
|
|
651
|
-
const setAction = (0,
|
|
715
|
+
const setAction = (0, import_react10.useCallback)((id, action) => {
|
|
652
716
|
setActions((prevPoints) => {
|
|
653
717
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
654
718
|
[id]: action
|
|
655
719
|
});
|
|
656
720
|
});
|
|
657
721
|
}, []);
|
|
658
|
-
const removeAction = (0,
|
|
722
|
+
const removeAction = (0, import_react10.useCallback)((id) => {
|
|
659
723
|
setActions((prevPoints) => {
|
|
660
724
|
const newPoints = __spreadValues({}, prevPoints);
|
|
661
725
|
delete newPoints[id];
|
|
662
726
|
return newPoints;
|
|
663
727
|
});
|
|
664
728
|
}, []);
|
|
665
|
-
const setCoAgentStateRender = (0,
|
|
729
|
+
const setCoAgentStateRender = (0, import_react10.useCallback)((id, stateRender) => {
|
|
666
730
|
setCoAgentStateRenders((prevPoints) => {
|
|
667
731
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
668
732
|
[id]: stateRender
|
|
669
733
|
});
|
|
670
734
|
});
|
|
671
735
|
}, []);
|
|
672
|
-
const removeCoAgentStateRender = (0,
|
|
736
|
+
const removeCoAgentStateRender = (0, import_react10.useCallback)((id) => {
|
|
673
737
|
setCoAgentStateRenders((prevPoints) => {
|
|
674
738
|
const newPoints = __spreadValues({}, prevPoints);
|
|
675
739
|
delete newPoints[id];
|
|
676
740
|
return newPoints;
|
|
677
741
|
});
|
|
678
742
|
}, []);
|
|
679
|
-
const getContextString = (0,
|
|
743
|
+
const getContextString = (0, import_react10.useCallback)(
|
|
680
744
|
(documents, categories) => {
|
|
681
745
|
const documentsString = documents.map((document) => {
|
|
682
746
|
return `${document.name} (${document.sourceApplication}):
|
|
@@ -689,37 +753,37 @@ ${nonDocumentStrings}`;
|
|
|
689
753
|
},
|
|
690
754
|
[printTree]
|
|
691
755
|
);
|
|
692
|
-
const addContext = (0,
|
|
756
|
+
const addContext = (0, import_react10.useCallback)(
|
|
693
757
|
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
694
758
|
return addElement(context, categories, parentId);
|
|
695
759
|
},
|
|
696
760
|
[addElement]
|
|
697
761
|
);
|
|
698
|
-
const removeContext = (0,
|
|
762
|
+
const removeContext = (0, import_react10.useCallback)(
|
|
699
763
|
(id) => {
|
|
700
764
|
removeElement(id);
|
|
701
765
|
},
|
|
702
766
|
[removeElement]
|
|
703
767
|
);
|
|
704
|
-
const getFunctionCallHandler = (0,
|
|
768
|
+
const getFunctionCallHandler = (0, import_react10.useCallback)(
|
|
705
769
|
(customEntryPoints) => {
|
|
706
770
|
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
|
|
707
771
|
},
|
|
708
772
|
[actions]
|
|
709
773
|
);
|
|
710
|
-
const getDocumentsContext = (0,
|
|
774
|
+
const getDocumentsContext = (0, import_react10.useCallback)(
|
|
711
775
|
(categories) => {
|
|
712
776
|
return allDocuments(categories);
|
|
713
777
|
},
|
|
714
778
|
[allDocuments]
|
|
715
779
|
);
|
|
716
|
-
const addDocumentContext = (0,
|
|
780
|
+
const addDocumentContext = (0, import_react10.useCallback)(
|
|
717
781
|
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
718
782
|
return addDocument(documentPointer, categories);
|
|
719
783
|
},
|
|
720
784
|
[addDocument]
|
|
721
785
|
);
|
|
722
|
-
const removeDocumentContext = (0,
|
|
786
|
+
const removeDocumentContext = (0, import_react10.useCallback)(
|
|
723
787
|
(documentId) => {
|
|
724
788
|
removeDocument(documentId);
|
|
725
789
|
},
|
|
@@ -732,7 +796,7 @@ ${nonDocumentStrings}`;
|
|
|
732
796
|
);
|
|
733
797
|
}
|
|
734
798
|
}
|
|
735
|
-
const copilotApiConfig = (0,
|
|
799
|
+
const copilotApiConfig = (0, import_react10.useMemo)(() => {
|
|
736
800
|
var _a2, _b2;
|
|
737
801
|
let cloud = void 0;
|
|
738
802
|
if (props.publicApiKey) {
|
|
@@ -774,7 +838,7 @@ ${nonDocumentStrings}`;
|
|
|
774
838
|
headers,
|
|
775
839
|
credentials: copilotApiConfig.credentials
|
|
776
840
|
});
|
|
777
|
-
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0,
|
|
841
|
+
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react10.useState)({});
|
|
778
842
|
const addChatSuggestionConfiguration = (id, suggestion) => {
|
|
779
843
|
setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
|
|
780
844
|
};
|
|
@@ -784,9 +848,9 @@ ${nonDocumentStrings}`;
|
|
|
784
848
|
return rest;
|
|
785
849
|
});
|
|
786
850
|
};
|
|
787
|
-
const [coagentStates, setCoagentStates] = (0,
|
|
788
|
-
const coagentStatesRef = (0,
|
|
789
|
-
const setCoagentStatesWithRef = (0,
|
|
851
|
+
const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
|
|
852
|
+
const coagentStatesRef = (0, import_react10.useRef)({});
|
|
853
|
+
const setCoagentStatesWithRef = (0, import_react10.useCallback)(
|
|
790
854
|
(value) => {
|
|
791
855
|
const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
|
|
792
856
|
coagentStatesRef.current = newValue;
|
|
@@ -802,9 +866,9 @@ ${nonDocumentStrings}`;
|
|
|
802
866
|
agentName: props.agent
|
|
803
867
|
};
|
|
804
868
|
}
|
|
805
|
-
const [agentSession, setAgentSession] = (0,
|
|
869
|
+
const [agentSession, setAgentSession] = (0, import_react10.useState)(initialAgentSession);
|
|
806
870
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
807
|
-
return /* @__PURE__ */ (0,
|
|
871
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
808
872
|
CopilotContext.Provider,
|
|
809
873
|
{
|
|
810
874
|
value: {
|
|
@@ -837,10 +901,9 @@ ${nonDocumentStrings}`;
|
|
|
837
901
|
setCoagentStatesWithRef,
|
|
838
902
|
agentSession,
|
|
839
903
|
setAgentSession,
|
|
840
|
-
runtimeClient
|
|
841
|
-
forwardedParameters: props.forwardedParameters || {}
|
|
904
|
+
runtimeClient
|
|
842
905
|
},
|
|
843
|
-
children: /* @__PURE__ */ (0,
|
|
906
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotMessages, { children })
|
|
844
907
|
}
|
|
845
908
|
);
|
|
846
909
|
}
|