@copilotkit/react-core 0.25.0-mme-cloud.0 → 0.25.0-multi-release-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.
Files changed (147) hide show
  1. package/.turbo/turbo-build.log +234 -216
  2. package/CHANGELOG.md +3 -3
  3. package/dist/{chunk-CGT3AVYT.mjs → chunk-3WKJ25L3.mjs} +3 -3
  4. package/dist/chunk-3WKJ25L3.mjs.map +1 -0
  5. package/dist/{chunk-TEI34ZF5.mjs → chunk-75RLNHDB.mjs} +5 -5
  6. package/dist/chunk-75RLNHDB.mjs.map +1 -0
  7. package/dist/{chunk-APVNN5QF.mjs → chunk-AEUR5JBT.mjs} +4 -3
  8. package/dist/chunk-AEUR5JBT.mjs.map +1 -0
  9. package/dist/{chunk-4E2RCCQB.mjs → chunk-BGB5G33N.mjs} +2 -2
  10. package/dist/chunk-F2JIAPZQ.mjs +14 -0
  11. package/dist/chunk-F2JIAPZQ.mjs.map +1 -0
  12. package/dist/{chunk-PYBBWHNC.mjs → chunk-FCEPEM4B.mjs} +3 -3
  13. package/dist/{chunk-SPLPX72P.mjs → chunk-GVLCTSXW.mjs} +7 -11
  14. package/dist/chunk-GVLCTSXW.mjs.map +1 -0
  15. package/dist/{chunk-KMTE4NLW.mjs → chunk-H3FQWB4B.mjs} +12 -19
  16. package/dist/chunk-H3FQWB4B.mjs.map +1 -0
  17. package/dist/{chunk-YKHUBROO.mjs → chunk-H6V2C2AN.mjs} +2 -2
  18. package/dist/chunk-ISKBWE4O.mjs +13 -0
  19. package/dist/chunk-ISKBWE4O.mjs.map +1 -0
  20. package/dist/{chunk-GH7QK2AZ.mjs → chunk-KIKHLN4X.mjs} +1 -3
  21. package/dist/chunk-KIKHLN4X.mjs.map +1 -0
  22. package/dist/{chunk-M7JUS6OQ.mjs → chunk-KJQZ2VEU.mjs} +2 -2
  23. package/dist/{chunk-O2GQ2XK7.mjs → chunk-QGRMN7L3.mjs} +3 -3
  24. package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -12
  25. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  26. package/dist/components/copilot-provider/copilotkit.d.ts +17 -0
  27. package/dist/components/copilot-provider/copilotkit.js +17 -19
  28. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  29. package/dist/components/copilot-provider/copilotkit.mjs +435 -8
  30. package/dist/components/copilot-provider/copilotkit.mjs.map +1 -1
  31. package/dist/components/copilot-provider/index.js +17 -19
  32. package/dist/components/copilot-provider/index.js.map +1 -1
  33. package/dist/components/copilot-provider/index.mjs +435 -9
  34. package/dist/components/copilot-provider/index.mjs.map +1 -1
  35. package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +23 -0
  36. package/dist/components/copilot-provider/standard-copilot-api-config.js +38 -0
  37. package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
  38. package/dist/components/copilot-provider/standard-copilot-api-config.mjs +13 -0
  39. package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -0
  40. package/dist/components/index.js +17 -19
  41. package/dist/components/index.js.map +1 -1
  42. package/dist/components/index.mjs +435 -10
  43. package/dist/components/index.mjs.map +1 -1
  44. package/dist/context/copilot-context.d.ts +2 -15
  45. package/dist/context/copilot-context.js +0 -2
  46. package/dist/context/copilot-context.js.map +1 -1
  47. package/dist/context/copilot-context.mjs +64 -5
  48. package/dist/context/copilot-context.mjs.map +1 -1
  49. package/dist/context/index.js +0 -2
  50. package/dist/context/index.js.map +1 -1
  51. package/dist/context/index.mjs +64 -6
  52. package/dist/context/index.mjs.map +1 -1
  53. package/dist/hooks/index.js +24 -14
  54. package/dist/hooks/index.js.map +1 -1
  55. package/dist/hooks/index.mjs +528 -23
  56. package/dist/hooks/index.mjs.map +1 -1
  57. package/dist/hooks/use-chat.d.ts +2 -8
  58. package/dist/hooks/use-chat.js +14 -3
  59. package/dist/hooks/use-chat.js.map +1 -1
  60. package/dist/hooks/use-chat.mjs +295 -4
  61. package/dist/hooks/use-chat.mjs.map +1 -1
  62. package/dist/hooks/use-copilot-action-implementation.d.ts +7 -0
  63. package/dist/hooks/use-copilot-action-implementation.js +140 -0
  64. package/dist/hooks/use-copilot-action-implementation.js.map +1 -0
  65. package/dist/hooks/use-copilot-action-implementation.mjs +106 -0
  66. package/dist/hooks/use-copilot-action-implementation.mjs.map +1 -0
  67. package/dist/hooks/use-copilot-action.d.ts +1 -1
  68. package/dist/hooks/use-copilot-action.js +9 -4
  69. package/dist/hooks/use-copilot-action.js.map +1 -1
  70. package/dist/hooks/use-copilot-action.mjs +107 -5
  71. package/dist/hooks/use-copilot-action.mjs.map +1 -1
  72. package/dist/hooks/use-copilot-chat.js +17 -12
  73. package/dist/hooks/use-copilot-chat.js.map +1 -1
  74. package/dist/hooks/use-copilot-chat.mjs +430 -12
  75. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  76. package/dist/hooks/use-flat-category-store.mjs +65 -4
  77. package/dist/hooks/use-flat-category-store.mjs.map +1 -1
  78. package/dist/hooks/use-make-copilot-actionable.js +0 -2
  79. package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
  80. package/dist/hooks/use-make-copilot-actionable.mjs +88 -5
  81. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  82. package/dist/hooks/use-make-copilot-document-readable.js +0 -2
  83. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  84. package/dist/hooks/use-make-copilot-document-readable.mjs +78 -5
  85. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
  86. package/dist/hooks/use-make-copilot-readable.js +0 -2
  87. package/dist/hooks/use-make-copilot-readable.js.map +1 -1
  88. package/dist/hooks/use-make-copilot-readable.mjs +78 -5
  89. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  90. package/dist/hooks/use-tree.mjs +150 -4
  91. package/dist/hooks/use-tree.mjs.map +1 -1
  92. package/dist/index.js +41 -31
  93. package/dist/index.js.map +1 -1
  94. package/dist/index.mjs +1113 -48
  95. package/dist/index.mjs.map +1 -1
  96. package/dist/lib/copilot-task.js +2 -2
  97. package/dist/lib/copilot-task.js.map +1 -1
  98. package/dist/lib/copilot-task.mjs +192 -10
  99. package/dist/lib/copilot-task.mjs.map +1 -1
  100. package/dist/lib/index.js +2 -2
  101. package/dist/lib/index.js.map +1 -1
  102. package/dist/lib/index.mjs +192 -11
  103. package/dist/lib/index.mjs.map +1 -1
  104. package/dist/openai-assistants/hooks/index.js +0 -2
  105. package/dist/openai-assistants/hooks/index.js.map +1 -1
  106. package/dist/openai-assistants/hooks/index.mjs +228 -14
  107. package/dist/openai-assistants/hooks/index.mjs.map +1 -1
  108. package/dist/openai-assistants/hooks/use-assistants.mjs +45 -7
  109. package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
  110. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -2
  111. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
  112. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +228 -14
  113. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
  114. package/dist/openai-assistants/index.js +0 -2
  115. package/dist/openai-assistants/index.js.map +1 -1
  116. package/dist/openai-assistants/index.mjs +228 -17
  117. package/dist/openai-assistants/index.mjs.map +1 -1
  118. package/dist/openai-assistants/utils/index.mjs +43 -5
  119. package/dist/openai-assistants/utils/index.mjs.map +1 -1
  120. package/dist/openai-assistants/utils/process-message-stream.mjs +43 -4
  121. package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
  122. package/dist/types/index.mjs +0 -1
  123. package/dist/utils/fetch-chat-completion.js +2 -2
  124. package/dist/utils/fetch-chat-completion.js.map +1 -1
  125. package/dist/utils/fetch-chat-completion.mjs +112 -5
  126. package/dist/utils/fetch-chat-completion.mjs.map +1 -1
  127. package/package.json +4 -4
  128. package/src/components/copilot-provider/copilotkit-props.tsx +0 -14
  129. package/src/components/copilot-provider/copilotkit.tsx +25 -21
  130. package/src/components/copilot-provider/standard-copilot-api-config.tsx +28 -0
  131. package/src/context/copilot-context.tsx +1 -19
  132. package/src/hooks/use-chat.ts +14 -9
  133. package/src/hooks/use-copilot-action-implementation.ts +60 -0
  134. package/src/hooks/use-copilot-action.ts +10 -58
  135. package/src/hooks/use-copilot-chat.ts +1 -5
  136. package/src/utils/fetch-chat-completion.ts +0 -1
  137. package/dist/chunk-APVNN5QF.mjs.map +0 -1
  138. package/dist/chunk-CGT3AVYT.mjs.map +0 -1
  139. package/dist/chunk-GH7QK2AZ.mjs.map +0 -1
  140. package/dist/chunk-KMTE4NLW.mjs.map +0 -1
  141. package/dist/chunk-SPLPX72P.mjs.map +0 -1
  142. package/dist/chunk-TEI34ZF5.mjs.map +0 -1
  143. /package/dist/{chunk-4E2RCCQB.mjs.map → chunk-BGB5G33N.mjs.map} +0 -0
  144. /package/dist/{chunk-PYBBWHNC.mjs.map → chunk-FCEPEM4B.mjs.map} +0 -0
  145. /package/dist/{chunk-YKHUBROO.mjs.map → chunk-H6V2C2AN.mjs.map} +0 -0
  146. /package/dist/{chunk-M7JUS6OQ.mjs.map → chunk-KJQZ2VEU.mjs.map} +0 -0
  147. /package/dist/{chunk-O2GQ2XK7.mjs.map → chunk-QGRMN7L3.mjs.map} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/use-make-copilot-readable.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["import { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n","import { FunctionCallHandler, ToolDefinition } from \"@copilotkit/shared\";\nimport { ActionRenderProps, FrontendAction } from \"../types/frontend-action\";\nimport React, { Ref } from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n\n /**\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\nexport type InChatRenderFunction = (props: ActionRenderProps<any>) => string | JSX.Element;\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, FrontendAction<any>>;\n setEntryPoint: (id: string, entryPoint: FrontendAction<any>) => void;\n removeEntryPoint: (id: string) => void;\n chatComponentsCache: React.RefObject<Record<string, InChatRenderFunction | string>>;\n getChatCompletionFunctionDescriptions: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => ToolDefinition[];\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 // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n chatComponentsCache: { current: {} },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get chatApiEndpointV2(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\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,SAAS,YAAY,WAAW,cAAc;;;ACE9C,OAAO,WAAoB;AAoF3B,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,C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Property;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/hooks/use-tree.ts
22
+ import { nanoid } from "nanoid";
23
+ import { useCallback, useReducer } from "react";
24
+ var removeNode = (nodes, id) => {
25
+ return nodes.reduce((result, node) => {
26
+ if (node.id !== id) {
27
+ const newNode = __spreadProps(__spreadValues({}, node), { children: removeNode(node.children, id) });
28
+ result.push(newNode);
29
+ }
30
+ return result;
31
+ }, []);
32
+ };
33
+ var addNode = (nodes, newNode, parentId) => {
34
+ if (!parentId) {
35
+ return [...nodes, newNode];
36
+ }
37
+ return nodes.map((node) => {
38
+ if (node.id === parentId) {
39
+ return __spreadProps(__spreadValues({}, node), { children: [...node.children, newNode] });
40
+ } else if (node.children.length) {
41
+ return __spreadProps(__spreadValues({}, node), { children: addNode(node.children, newNode, parentId) });
42
+ }
43
+ return node;
44
+ });
45
+ };
46
+ var treeIndentationRepresentation = (index, indentLevel) => {
47
+ if (indentLevel === 0) {
48
+ return (index + 1).toString();
49
+ } else if (indentLevel === 1) {
50
+ return String.fromCharCode(65 + index);
51
+ } else if (indentLevel === 2) {
52
+ return String.fromCharCode(97 + index);
53
+ } else {
54
+ return "-";
55
+ }
56
+ };
57
+ var printNode = (node, prefix = "", indentLevel = 0) => {
58
+ const indent = " ".repeat(3).repeat(indentLevel);
59
+ const prefixPlusIndentLength = prefix.length + indent.length;
60
+ const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
61
+ const valueLines = node.value.split("\n");
62
+ const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
63
+ const outputSubsequentLines = valueLines.slice(1).map((line) => `${subsequentLinesPrefix}${line}`).join("\n");
64
+ let output = `${outputFirstLine}
65
+ `;
66
+ if (outputSubsequentLines) {
67
+ output += `${outputSubsequentLines}
68
+ `;
69
+ }
70
+ const childPrePrefix = " ".repeat(prefix.length);
71
+ node.children.forEach(
72
+ (child, index) => output += printNode(
73
+ child,
74
+ `${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
75
+ indentLevel + 1
76
+ )
77
+ );
78
+ return output;
79
+ };
80
+ function treeReducer(state, action) {
81
+ switch (action.type) {
82
+ case "ADD_NODE": {
83
+ const { value, parentId, id: newNodeId } = action;
84
+ const newNode = {
85
+ id: newNodeId,
86
+ value,
87
+ children: [],
88
+ categories: new Set(action.categories)
89
+ };
90
+ try {
91
+ return addNode(state, newNode, parentId);
92
+ } catch (error) {
93
+ console.error(`Error while adding node with id ${newNodeId}: ${error}`);
94
+ return state;
95
+ }
96
+ }
97
+ case "REMOVE_NODE":
98
+ return removeNode(state, action.id);
99
+ default:
100
+ return state;
101
+ }
102
+ }
103
+ var useTree = () => {
104
+ const [tree, dispatch] = useReducer(treeReducer, []);
105
+ const addElement = useCallback(
106
+ (value, categories, parentId) => {
107
+ const newNodeId = nanoid();
108
+ dispatch({
109
+ type: "ADD_NODE",
110
+ value,
111
+ parentId,
112
+ id: newNodeId,
113
+ categories
114
+ });
115
+ return newNodeId;
116
+ },
117
+ []
118
+ );
119
+ const removeElement = useCallback((id) => {
120
+ dispatch({ type: "REMOVE_NODE", id });
121
+ }, []);
122
+ const printTree = useCallback(
123
+ (categories) => {
124
+ const categoriesSet = new Set(categories);
125
+ let output = "";
126
+ tree.forEach((node, index) => {
127
+ if (!setsHaveIntersection(categoriesSet, node.categories)) {
128
+ return;
129
+ }
130
+ if (index !== 0) {
131
+ output += "\n";
132
+ }
133
+ output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);
134
+ });
135
+ return output;
136
+ },
137
+ [tree]
138
+ );
139
+ return { tree, addElement, printTree, removeElement };
140
+ };
141
+ var use_tree_default = useTree;
142
+ function setsHaveIntersection(setA, setB) {
143
+ const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
144
+ for (let item of smallerSet) {
145
+ if (largerSet.has(item)) {
146
+ return true;
147
+ }
148
+ }
149
+ return false;
150
+ }
5
151
  export {
6
152
  use_tree_default as default
7
153
  };
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/use-tree.ts"],"sourcesContent":["import { nanoid } from \"nanoid\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type TreeNodeId = string;\n\nexport interface TreeNode {\n id: TreeNodeId;\n value: string;\n children: TreeNode[];\n parentId?: TreeNodeId;\n categories: Set<string>;\n}\n\nexport type Tree = TreeNode[];\n\nexport interface UseTreeReturn {\n tree: Tree;\n addElement: (value: string, categories: string[], parentId?: TreeNodeId) => TreeNodeId;\n printTree: (categories: string[]) => string;\n removeElement: (id: TreeNodeId) => void;\n}\n\nconst findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {\n for (const node of nodes) {\n if (node.id === id) {\n return node;\n }\n const result = findNode(node.children, id);\n if (result) {\n return result;\n }\n }\n return undefined;\n};\n\nconst removeNode = (nodes: Tree, id: TreeNodeId): Tree => {\n return nodes.reduce((result: Tree, node) => {\n if (node.id !== id) {\n const newNode = { ...node, children: removeNode(node.children, id) };\n result.push(newNode);\n }\n return result;\n }, []);\n};\n\nconst addNode = (nodes: Tree, newNode: TreeNode, parentId?: TreeNodeId): Tree => {\n if (!parentId) {\n return [...nodes, newNode];\n }\n return nodes.map((node) => {\n if (node.id === parentId) {\n return { ...node, children: [...node.children, newNode] };\n } else if (node.children.length) {\n return { ...node, children: addNode(node.children, newNode, parentId) };\n }\n return node;\n });\n};\n\nconst treeIndentationRepresentation = (index: number, indentLevel: number): string => {\n if (indentLevel === 0) {\n return (index + 1).toString();\n } else if (indentLevel === 1) {\n return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'\n } else if (indentLevel === 2) {\n return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'\n } else {\n return \"-\";\n }\n};\n\nconst printNode = (node: TreeNode, prefix = \"\", indentLevel = 0): string => {\n const indent = \" \".repeat(3).repeat(indentLevel);\n\n const prefixPlusIndentLength = prefix.length + indent.length;\n const subsequentLinesPrefix = \" \".repeat(prefixPlusIndentLength);\n\n const valueLines = node.value.split(\"\\n\");\n\n const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;\n const outputSubsequentLines = valueLines\n .slice(1)\n .map((line) => `${subsequentLinesPrefix}${line}`)\n .join(\"\\n\");\n\n let output = `${outputFirstLine}\\n`;\n if (outputSubsequentLines) {\n output += `${outputSubsequentLines}\\n`;\n }\n\n const childPrePrefix = \" \".repeat(prefix.length);\n\n node.children.forEach(\n (child, index) =>\n (output += printNode(\n child,\n `${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,\n indentLevel + 1,\n )),\n );\n return output;\n};\n\n// Action types\ntype Action =\n | {\n type: \"ADD_NODE\";\n value: string;\n parentId?: string;\n id: string;\n categories: string[];\n }\n | { type: \"REMOVE_NODE\"; id: string };\n\n// Reducer function\nfunction treeReducer(state: Tree, action: Action): Tree {\n switch (action.type) {\n case \"ADD_NODE\": {\n const { value, parentId, id: newNodeId } = action;\n const newNode: TreeNode = {\n id: newNodeId,\n value,\n children: [],\n categories: new Set(action.categories),\n };\n\n try {\n return addNode(state, newNode, parentId);\n } catch (error) {\n console.error(`Error while adding node with id ${newNodeId}: ${error}`);\n return state;\n }\n }\n case \"REMOVE_NODE\":\n return removeNode(state, action.id);\n default:\n return state;\n }\n}\n\n// useTree hook\nconst useTree = (): UseTreeReturn => {\n const [tree, dispatch] = useReducer(treeReducer, []);\n\n const addElement = useCallback(\n (value: string, categories: string[], parentId?: string): TreeNodeId => {\n const newNodeId = nanoid(); // Generate new ID outside of dispatch\n dispatch({\n type: \"ADD_NODE\",\n value,\n parentId,\n id: newNodeId,\n categories: categories,\n });\n return newNodeId; // Return the new ID\n },\n [],\n );\n\n const removeElement = useCallback((id: TreeNodeId): void => {\n dispatch({ type: \"REMOVE_NODE\", id });\n }, []);\n\n const printTree = useCallback(\n (categories: string[]): string => {\n const categoriesSet = new Set(categories);\n\n let output = \"\";\n tree.forEach((node, index) => {\n // if the node does not have any of the desired categories, continue to the next node\n if (!setsHaveIntersection(categoriesSet, node.categories)) {\n return;\n }\n\n // add a new line before each node except the first one\n if (index !== 0) {\n output += \"\\n\";\n }\n\n output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);\n });\n return output;\n },\n [tree],\n );\n\n return { tree, addElement, printTree, removeElement };\n};\n\nexport default useTree;\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,kBAAkB;AAkCxC,IAAM,aAAa,CAAC,OAAa,OAAyB;AACxD,SAAO,MAAM,OAAO,CAAC,QAAc,SAAS;AAC1C,QAAI,KAAK,OAAO,IAAI;AAClB,YAAM,UAAU,iCAAK,OAAL,EAAW,UAAU,WAAW,KAAK,UAAU,EAAE,EAAE;AACnE,aAAO,KAAK,OAAO;AAAA,IACrB;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAA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AAI,CAAC,qBAAqB,eAAe,KAAK,UAAU,GAAG;AACzD;AAAA,QACF;AAGA,YAAI,UAAU,GAAG;AACf,oBAAU;AAAA,QACZ;AAEA,kBAAU,UAAU,MAAM,GAAG,8BAA8B,OAAO,CAAC,KAAK;AAAA,MAC1E,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,MAAM,YAAY,WAAW,cAAc;AACtD;AAEA,IAAO,mBAAQ;AAEf,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAAI,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAEnF,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
package/dist/index.js CHANGED
@@ -117,8 +117,6 @@ var emptyCopilotContext = {
117
117
  addContext: () => "",
118
118
  removeContext: () => {
119
119
  },
120
- messages: [],
121
- setMessages: () => returnAndThrowInDebug([]),
122
120
  getDocumentsContext: (categories) => returnAndThrowInDebug([]),
123
121
  addDocumentContext: () => returnAndThrowInDebug(""),
124
122
  removeDocumentContext: () => {
@@ -347,6 +345,16 @@ function setsHaveIntersection2(setA, setB) {
347
345
  return false;
348
346
  }
349
347
 
348
+ // src/components/copilot-provider/standard-copilot-api-config.tsx
349
+ var StandardCopilotApiConfig = class {
350
+ constructor(chatApiEndpoint, chatApiEndpointV2, headers, body) {
351
+ this.chatApiEndpoint = chatApiEndpoint;
352
+ this.chatApiEndpointV2 = chatApiEndpointV2;
353
+ this.headers = headers;
354
+ this.body = body;
355
+ }
356
+ };
357
+
350
358
  // src/components/copilot-provider/copilotkit.tsx
351
359
  var import_jsx_runtime = require("react/jsx-runtime");
352
360
  function CopilotKit(_a) {
@@ -354,7 +362,6 @@ function CopilotKit(_a) {
354
362
  const [entryPoints, setEntryPoints] = (0, import_react4.useState)({});
355
363
  const chatComponentsCache = (0, import_react4.useRef)({});
356
364
  const { addElement, removeElement, printTree } = use_tree_default();
357
- const [messages, setMessages] = (0, import_react4.useState)([]);
358
365
  const {
359
366
  addElement: addDocument,
360
367
  removeElement: removeDocument,
@@ -429,19 +436,12 @@ ${nonDocumentStrings}`;
429
436
  },
430
437
  [removeDocument]
431
438
  );
432
- if (!props.apiKey) {
433
- if (props.cloudRestrictToTopic) {
434
- throw new Error(
435
- "To use the cloudRestrictToTopic feature, please sign up at https://copilotkit.ai and provide an apiKey."
436
- );
437
- }
438
- }
439
- const copilotApiConfig = __spreadProps(__spreadValues({}, props.apiKey ? { cloud: { apiKey: props.apiKey, restrictToTopic: props.cloudRestrictToTopic } } : {}), {
440
- chatApiEndpoint: props.url,
441
- chatApiEndpointV2: `${props.url}/v2`,
442
- headers: props.headers || {},
443
- body: __spreadValues(__spreadValues({}, props.body), props.backendOnlyProps)
444
- });
439
+ const copilotApiConfig = new StandardCopilotApiConfig(
440
+ props.url,
441
+ `${props.url}/v2`,
442
+ props.headers || {},
443
+ __spreadValues(__spreadValues({}, props.body), props.backendOnlyProps)
444
+ );
445
445
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
446
446
  CopilotContext.Provider,
447
447
  {
@@ -458,9 +458,7 @@ ${nonDocumentStrings}`;
458
458
  getDocumentsContext,
459
459
  addDocumentContext,
460
460
  removeDocumentContext,
461
- copilotApiConfig,
462
- messages,
463
- setMessages
461
+ copilotApiConfig
464
462
  },
465
463
  children
466
464
  }
@@ -519,11 +517,11 @@ function fetchChatCompletion(_0) {
519
517
  headers: __spreadValues(__spreadValues({
520
518
  "Content-Type": "application/json"
521
519
  }, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
522
- body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
520
+ body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
523
521
  model,
524
522
  messages: cleanedMessages,
525
523
  stream: true
526
- }, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {}), copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {})),
524
+ }, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {})),
527
525
  signal
528
526
  });
529
527
  return response;
@@ -570,7 +568,7 @@ function fetchAndDecodeChatCompletionAsText(params) {
570
568
  // src/hooks/use-chat.ts
571
569
  var import_untruncate_json = __toESM(require("untruncate-json"));
572
570
  function useChat(options) {
573
- const { messages, setMessages } = options;
571
+ const [messages, setMessages] = (0, import_react5.useState)([]);
574
572
  const [input, setInput] = (0, import_react5.useState)("");
575
573
  const [isLoading, setIsLoading] = (0, import_react5.useState)(false);
576
574
  const abortControllerRef = (0, import_react5.useRef)();
@@ -611,6 +609,16 @@ function useChat(options) {
611
609
  runIdRef.current = response.headers.get("runid");
612
610
  }
613
611
  if (!response.events) {
612
+ setMessages([
613
+ ...messages2,
614
+ {
615
+ id: (0, import_nanoid3.nanoid)(),
616
+ createdAt: /* @__PURE__ */ new Date(),
617
+ content: response.statusText,
618
+ role: "assistant"
619
+ }
620
+ ]);
621
+ setIsLoading(false);
614
622
  throw new Error("Failed to fetch chat completion");
615
623
  }
616
624
  const reader = response.events.getReader();
@@ -734,6 +742,7 @@ function useChat(options) {
734
742
  (_a = abortControllerRef.current) == null ? void 0 : _a.abort();
735
743
  };
736
744
  return {
745
+ messages,
737
746
  append,
738
747
  reload,
739
748
  stop,
@@ -756,9 +765,7 @@ function useCopilotChat(_a) {
756
765
  getContextString,
757
766
  getChatCompletionFunctionDescriptions,
758
767
  getFunctionCallHandler,
759
- copilotApiConfig,
760
- messages,
761
- setMessages
768
+ copilotApiConfig
762
769
  } = (0, import_react6.useContext)(CopilotContext);
763
770
  const systemMessage = (0, import_react6.useMemo)(() => {
764
771
  const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
@@ -772,16 +779,14 @@ function useCopilotChat(_a) {
772
779
  const functionDescriptions = (0, import_react6.useMemo)(() => {
773
780
  return getChatCompletionFunctionDescriptions();
774
781
  }, [getChatCompletionFunctionDescriptions]);
775
- const { append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
782
+ const { messages, append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
776
783
  copilotConfig: copilotApiConfig,
777
784
  id: options.id,
778
785
  initialMessages: [systemMessage].concat(options.initialMessages || []),
779
786
  tools: functionDescriptions,
780
787
  onFunctionCall: getFunctionCallHandler(),
781
788
  headers: __spreadValues({}, options.headers),
782
- body: __spreadValues({}, options.body),
783
- messages,
784
- setMessages
789
+ body: __spreadValues({}, options.body)
785
790
  }));
786
791
  const visibleMessages = messages.filter(
787
792
  (message) => message.role === "user" || message.role === "assistant" || message.role === "function"
@@ -845,10 +850,10 @@ function useMakeCopilotActionable(annotatedFunction, dependencies) {
845
850
  }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
846
851
  }
847
852
 
848
- // src/hooks/use-copilot-action.ts
853
+ // src/hooks/use-copilot-action-implementation.ts
849
854
  var import_react8 = require("react");
850
855
  var import_nanoid5 = require("nanoid");
851
- function useCopilotAction(action, dependencies) {
856
+ function useCopilotActionImplementation(action, dependencies) {
852
857
  const { setEntryPoint, removeEntryPoint, entryPoints, chatComponentsCache } = (0, import_react8.useContext)(CopilotContext);
853
858
  const idRef = (0, import_react8.useRef)((0, import_nanoid5.nanoid)());
854
859
  if (dependencies === void 0) {
@@ -884,6 +889,11 @@ function useCopilotAction(action, dependencies) {
884
889
  ]);
885
890
  }
886
891
 
892
+ // src/hooks/use-copilot-action.ts
893
+ function useCopilotAction(action, dependencies) {
894
+ return useCopilotActionImplementation(action, dependencies);
895
+ }
896
+
887
897
  // src/hooks/use-make-copilot-readable.ts
888
898
  var import_react9 = require("react");
889
899
  function useMakeCopilotReadable(information, parentId, categories) {