@copilotkit/react-core 0.19.0 → 0.20.0-demofixes.2
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 +215 -197
- package/CHANGELOG.md +33 -0
- package/dist/{chunk-VP2EU4US.mjs → chunk-4LKP4DMT.mjs} +3 -3
- package/dist/{chunk-4CFJ6A6A.mjs → chunk-4OWTRVNI.mjs} +6 -2
- package/dist/chunk-4OWTRVNI.mjs.map +1 -0
- package/dist/{chunk-IQEWY7P4.mjs → chunk-ABXVFQLN.mjs} +46 -29
- package/dist/chunk-ABXVFQLN.mjs.map +1 -0
- package/dist/{chunk-YOXWT7I6.mjs → chunk-CPEUPJQJ.mjs} +2 -2
- package/dist/{chunk-UAKLDQMP.mjs → chunk-JIKWSWIX.mjs} +4 -4
- package/dist/chunk-JIKWSWIX.mjs.map +1 -0
- package/dist/{chunk-DGUVNY3V.mjs → chunk-K2LVBFRY.mjs} +2 -2
- package/dist/{chunk-GBZZPQ5X.mjs → chunk-KAZJPE2I.mjs} +14 -8
- package/dist/chunk-KAZJPE2I.mjs.map +1 -0
- package/dist/{chunk-5GS7FQPW.mjs → chunk-KZNQLITJ.mjs} +5 -5
- package/dist/chunk-KZNQLITJ.mjs.map +1 -0
- package/dist/chunk-LUGEI4YQ.mjs +1 -0
- package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
- package/dist/chunk-TZPIY754.mjs +102 -0
- package/dist/chunk-TZPIY754.mjs.map +1 -0
- package/dist/{chunk-IQH5AQQC.mjs → chunk-XBLCD24W.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit.js +12 -6
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +439 -8
- package/dist/components/copilot-provider/copilotkit.mjs.map +1 -1
- package/dist/components/copilot-provider/index.js +12 -6
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +439 -9
- package/dist/components/copilot-provider/index.mjs.map +1 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs +9 -4
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
- package/dist/components/index.js +12 -6
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +439 -10
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +5 -4
- package/dist/context/copilot-context.js +7 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +65 -5
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +7 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +65 -6
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.js +47 -29
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +392 -21
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-chat.d.ts +2 -2
- package/dist/hooks/use-chat.js +46 -28
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +204 -4
- package/dist/hooks/use-chat.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.js +47 -29
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +340 -13
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +65 -4
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +85 -5
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +77 -5
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +77 -5
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +150 -4
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +154 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +981 -41
- package/dist/index.mjs.map +1 -1
- package/dist/lib/copilot-task.d.ts +34 -0
- package/dist/lib/copilot-task.js +206 -0
- package/dist/lib/copilot-task.js.map +1 -0
- package/dist/lib/copilot-task.mjs +188 -0
- package/dist/lib/copilot-task.mjs.map +1 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.js +206 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/index.mjs +188 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/openai-assistants/hooks/index.js.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +228 -14
- package/dist/openai-assistants/hooks/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +45 -7
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +228 -14
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
- package/dist/openai-assistants/index.js.map +1 -1
- package/dist/openai-assistants/index.mjs +228 -17
- package/dist/openai-assistants/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/index.mjs +43 -5
- package/dist/openai-assistants/utils/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +43 -4
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
- package/dist/types/index.mjs +0 -1
- package/dist/utils/fetch-chat-completion.d.ts +3 -3
- package/dist/utils/fetch-chat-completion.js +3 -3
- package/dist/utils/fetch-chat-completion.js.map +1 -1
- package/dist/utils/fetch-chat-completion.mjs +99 -5
- package/dist/utils/fetch-chat-completion.mjs.map +1 -1
- package/package.json +4 -4
- package/src/components/copilot-provider/copilotkit.tsx +17 -8
- package/src/context/copilot-context.tsx +11 -3
- package/src/context/index.ts +1 -1
- package/src/hooks/use-chat.ts +52 -30
- package/src/hooks/use-copilot-chat.ts +3 -3
- package/src/index.tsx +1 -0
- package/src/lib/copilot-task.ts +131 -0
- package/src/lib/index.ts +1 -0
- package/src/utils/fetch-chat-completion.ts +6 -6
- package/dist/chunk-4CFJ6A6A.mjs.map +0 -1
- package/dist/chunk-5GS7FQPW.mjs.map +0 -1
- package/dist/chunk-GBZZPQ5X.mjs.map +0 -1
- package/dist/chunk-IQEWY7P4.mjs.map +0 -1
- package/dist/chunk-UAKLDQMP.mjs.map +0 -1
- /package/dist/{chunk-VP2EU4US.mjs.map → chunk-4LKP4DMT.mjs.map} +0 -0
- /package/dist/{chunk-YOXWT7I6.mjs.map → chunk-CPEUPJQJ.mjs.map} +0 -0
- /package/dist/{chunk-DGUVNY3V.mjs.map → chunk-K2LVBFRY.mjs.map} +0 -0
- /package/dist/{chunk-IQH5AQQC.mjs.map → chunk-XBLCD24W.mjs.map} +0 -0
package/dist/hooks/use-tree.mjs
CHANGED
|
@@ -1,7 +1,153 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
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,CAAC;AACP;AAEA,IAAM,UAAU,CAAC,OAAa,SAAmB,aAAgC;AAC/E,MAAI,CAAC,UAAU;AACb,WAAO,CAAC,GAAG,OAAO,OAAO;AAAA,EAC3B;AACA,SAAO,MAAM,IAAI,CAAC,SAAS;AACzB,QAAI,KAAK,OAAO,UAAU;AACxB,aAAO,iCAAK,OAAL,EAAW,UAAU,CAAC,GAAG,KAAK,UAAU,OAAO,EAAE;AAAA,IAC1D,WAAW,KAAK,SAAS,QAAQ;AAC/B,aAAO,iCAAK,OAAL,EAAW,UAAU,QAAQ,KAAK,UAAU,SAAS,QAAQ,EAAE;AAAA,IACxE;AACA,WAAO;AAAA,EACT,CAAC;AACH;AAEA,IAAM,gCAAgC,CAAC,OAAe,gBAAgC;AACpF,MAAI,gBAAgB,GAAG;AACrB,YAAQ,QAAQ,GAAG,SAAS;AAAA,EAC9B,WAAW,gBAAgB,GAAG;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ACT;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { CopilotKit, defaultCopilotContextCategories } from './components/copilot-provider/copilotkit.js';
|
|
2
2
|
export { CopilotKitProps } from './components/copilot-provider/copilotkit-props.js';
|
|
3
|
-
export { CopilotApiConfig, CopilotContext, CopilotContextParams } from './context/copilot-context.js';
|
|
3
|
+
export { CopilotApiConfig, CopilotContext, CopilotContextParams, useCopilotContext } from './context/copilot-context.js';
|
|
4
4
|
export { UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat } from './hooks/use-copilot-chat.js';
|
|
5
5
|
export { useMakeCopilotActionable } from './hooks/use-make-copilot-actionable.js';
|
|
6
6
|
export { useMakeCopilotReadable } from './hooks/use-make-copilot-readable.js';
|
|
@@ -10,6 +10,7 @@ export { DocumentPointer } from './types/document-pointer.js';
|
|
|
10
10
|
export { SystemMessageFunction } from './types/system-message.js';
|
|
11
11
|
export { AssistantStatus, RequestForwardingOptions, UseCopilotChatOptionsV2, UseCopilotChatV2Result, useCopilotChatV2 } from './openai-assistants/hooks/use-copilot-chat-v2.js';
|
|
12
12
|
export { processMessageStream } from './openai-assistants/utils/process-message-stream.js';
|
|
13
|
+
export { CopilotTask, CopilotTaskConfig } from './lib/copilot-task.js';
|
|
13
14
|
export { FetchChatCompletionParams, fetchAndDecodeChatCompletion, fetchAndDecodeChatCompletionAsText, fetchChatCompletion } from './utils/fetch-chat-completion.js';
|
|
14
15
|
import 'react';
|
|
15
16
|
import '@copilotkit/shared';
|
package/dist/index.js
CHANGED
|
@@ -81,6 +81,7 @@ var src_exports = {};
|
|
|
81
81
|
__export(src_exports, {
|
|
82
82
|
CopilotContext: () => CopilotContext,
|
|
83
83
|
CopilotKit: () => CopilotKit,
|
|
84
|
+
CopilotTask: () => CopilotTask,
|
|
84
85
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories,
|
|
85
86
|
fetchAndDecodeChatCompletion: () => fetchAndDecodeChatCompletion,
|
|
86
87
|
fetchAndDecodeChatCompletionAsText: () => fetchAndDecodeChatCompletionAsText,
|
|
@@ -88,6 +89,7 @@ __export(src_exports, {
|
|
|
88
89
|
processMessageStream: () => processMessageStream,
|
|
89
90
|
useCopilotChat: () => useCopilotChat,
|
|
90
91
|
useCopilotChatV2: () => useCopilotChatV2,
|
|
92
|
+
useCopilotContext: () => useCopilotContext,
|
|
91
93
|
useMakeCopilotActionable: () => useMakeCopilotActionable,
|
|
92
94
|
useMakeCopilotDocumentReadable: () => useMakeCopilotDocumentReadable,
|
|
93
95
|
useMakeCopilotReadable: () => useMakeCopilotReadable
|
|
@@ -132,6 +134,9 @@ var emptyCopilotContext = {
|
|
|
132
134
|
}()
|
|
133
135
|
};
|
|
134
136
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
137
|
+
function useCopilotContext() {
|
|
138
|
+
return import_react.default.useContext(CopilotContext);
|
|
139
|
+
}
|
|
135
140
|
function returnAndThrowInDebug(value) {
|
|
136
141
|
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
137
142
|
return value;
|
|
@@ -397,12 +402,18 @@ ${nonDocumentStrings}`;
|
|
|
397
402
|
},
|
|
398
403
|
[removeElement]
|
|
399
404
|
);
|
|
400
|
-
const getChatCompletionFunctionDescriptions = (0, import_react4.useCallback)(
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
405
|
+
const getChatCompletionFunctionDescriptions = (0, import_react4.useCallback)(
|
|
406
|
+
(customEntryPoints) => {
|
|
407
|
+
return entryPointsToChatCompletionFunctions(Object.values(customEntryPoints || entryPoints));
|
|
408
|
+
},
|
|
409
|
+
[entryPoints]
|
|
410
|
+
);
|
|
411
|
+
const getFunctionCallHandler = (0, import_react4.useCallback)(
|
|
412
|
+
(customEntryPoints) => {
|
|
413
|
+
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || entryPoints));
|
|
414
|
+
},
|
|
415
|
+
[entryPoints]
|
|
416
|
+
);
|
|
406
417
|
const getDocumentsContext = (0, import_react4.useCallback)(
|
|
407
418
|
(categories) => {
|
|
408
419
|
return allDocuments(categories);
|
|
@@ -489,14 +500,14 @@ function fetchChatCompletion(_0) {
|
|
|
489
500
|
copilotConfig,
|
|
490
501
|
model,
|
|
491
502
|
messages,
|
|
492
|
-
|
|
503
|
+
tools,
|
|
493
504
|
temperature,
|
|
494
505
|
headers,
|
|
495
506
|
body,
|
|
496
507
|
signal
|
|
497
508
|
}) {
|
|
498
509
|
temperature || (temperature = 0.5);
|
|
499
|
-
|
|
510
|
+
tools || (tools = []);
|
|
500
511
|
const cleanedMessages = messages.map((message) => {
|
|
501
512
|
const { content, role, name, function_call } = message;
|
|
502
513
|
return { content, role, name, function_call };
|
|
@@ -510,7 +521,7 @@ function fetchChatCompletion(_0) {
|
|
|
510
521
|
model,
|
|
511
522
|
messages: cleanedMessages,
|
|
512
523
|
stream: true
|
|
513
|
-
},
|
|
524
|
+
}, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), body ? __spreadValues({}, body) : {})),
|
|
514
525
|
signal
|
|
515
526
|
});
|
|
516
527
|
return response;
|
|
@@ -546,21 +557,24 @@ function useChat(options) {
|
|
|
546
557
|
const [isLoading, setIsLoading] = (0, import_react5.useState)(false);
|
|
547
558
|
const abortControllerRef = (0, import_react5.useRef)();
|
|
548
559
|
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
560
|
+
var _a;
|
|
549
561
|
setIsLoading(true);
|
|
550
|
-
const
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
562
|
+
const newMessages = [
|
|
563
|
+
{
|
|
564
|
+
id: (0, import_nanoid3.nanoid)(),
|
|
565
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
566
|
+
content: "",
|
|
567
|
+
role: "assistant"
|
|
568
|
+
}
|
|
569
|
+
];
|
|
556
570
|
const abortController = new AbortController();
|
|
557
571
|
abortControllerRef.current = abortController;
|
|
558
|
-
setMessages([...messages2,
|
|
572
|
+
setMessages([...messages2, ...newMessages]);
|
|
559
573
|
const messagesWithContext = [...options.initialMessages || [], ...messages2];
|
|
560
574
|
const response = yield fetchAndDecodeChatCompletion({
|
|
561
575
|
copilotConfig: options.copilotConfig,
|
|
562
576
|
messages: messagesWithContext,
|
|
563
|
-
|
|
577
|
+
tools: options.tools,
|
|
564
578
|
headers: options.headers,
|
|
565
579
|
signal: abortController.signal
|
|
566
580
|
});
|
|
@@ -568,36 +582,51 @@ function useChat(options) {
|
|
|
568
582
|
throw new Error("Failed to fetch chat completion");
|
|
569
583
|
}
|
|
570
584
|
const reader = response.events.getReader();
|
|
571
|
-
|
|
572
|
-
|
|
585
|
+
try {
|
|
586
|
+
while (true) {
|
|
573
587
|
const { done, value } = yield reader.read();
|
|
574
588
|
if (done) {
|
|
575
|
-
|
|
576
|
-
return __spreadValues({}, assistantMessage);
|
|
589
|
+
return newMessages.slice();
|
|
577
590
|
}
|
|
591
|
+
let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);
|
|
578
592
|
if (value.type === "content") {
|
|
579
|
-
|
|
580
|
-
|
|
593
|
+
if (currentMessage.function_call) {
|
|
594
|
+
currentMessage = {
|
|
595
|
+
id: (0, import_nanoid3.nanoid)(),
|
|
596
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
597
|
+
content: "",
|
|
598
|
+
role: "assistant"
|
|
599
|
+
};
|
|
600
|
+
newMessages.push(currentMessage);
|
|
601
|
+
}
|
|
602
|
+
currentMessage.content += value.content;
|
|
603
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
604
|
+
setMessages([...messages2, ...newMessages]);
|
|
581
605
|
} else if (value.type === "function") {
|
|
582
|
-
|
|
606
|
+
if (currentMessage.content != "" || currentMessage.function_call) {
|
|
607
|
+
currentMessage = {
|
|
608
|
+
id: (0, import_nanoid3.nanoid)(),
|
|
609
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
610
|
+
content: "",
|
|
611
|
+
role: "assistant"
|
|
612
|
+
};
|
|
613
|
+
newMessages.push(currentMessage);
|
|
614
|
+
}
|
|
615
|
+
currentMessage.function_call = {
|
|
583
616
|
name: value.name,
|
|
584
617
|
arguments: JSON.stringify(value.arguments)
|
|
585
618
|
};
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
619
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
620
|
+
setMessages([...messages2, ...newMessages]);
|
|
621
|
+
yield (_a = options.onFunctionCall) == null ? void 0 : _a.call(options, messages2, currentMessage.function_call);
|
|
589
622
|
}
|
|
590
|
-
} catch (error) {
|
|
591
|
-
setIsLoading(false);
|
|
592
|
-
throw error;
|
|
593
623
|
}
|
|
624
|
+
} finally {
|
|
625
|
+
setIsLoading(false);
|
|
594
626
|
}
|
|
595
627
|
});
|
|
596
628
|
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
597
|
-
|
|
598
|
-
if (message.function_call && options.onFunctionCall) {
|
|
599
|
-
yield options.onFunctionCall(messages2, message.function_call);
|
|
600
|
-
}
|
|
629
|
+
yield runChatCompletion(messages2);
|
|
601
630
|
});
|
|
602
631
|
const append = (message) => __async(this, null, function* () {
|
|
603
632
|
if (isLoading) {
|
|
@@ -665,7 +694,7 @@ function useCopilotChat(_a) {
|
|
|
665
694
|
copilotConfig: copilotApiConfig,
|
|
666
695
|
id: options.id,
|
|
667
696
|
initialMessages: [systemMessage].concat(options.initialMessages || []),
|
|
668
|
-
|
|
697
|
+
tools: functionDescriptions,
|
|
669
698
|
onFunctionCall: getFunctionCallHandler(),
|
|
670
699
|
headers: __spreadValues({}, options.headers),
|
|
671
700
|
body: __spreadValues({}, options.body)
|
|
@@ -902,10 +931,99 @@ You can ask them for clarifying questions if needed, but don't be annoying about
|
|
|
902
931
|
If you would like to call a function, call it without saying anything else.
|
|
903
932
|
`;
|
|
904
933
|
}
|
|
934
|
+
|
|
935
|
+
// src/lib/copilot-task.ts
|
|
936
|
+
var CopilotTask = class {
|
|
937
|
+
constructor(config) {
|
|
938
|
+
this.instructions = config.instructions;
|
|
939
|
+
this.functions = config.functions || [];
|
|
940
|
+
this.includeCopilotReadable = config.includeCopilotReadable || true;
|
|
941
|
+
this.includeCopilotActionable = config.includeCopilotActionable || true;
|
|
942
|
+
}
|
|
943
|
+
run(context, data) {
|
|
944
|
+
return __async(this, null, function* () {
|
|
945
|
+
const entryPoints = this.includeCopilotActionable ? Object.assign({}, context.entryPoints) : {};
|
|
946
|
+
for (const fn of this.functions) {
|
|
947
|
+
entryPoints[fn.name] = fn;
|
|
948
|
+
}
|
|
949
|
+
let contextString = "";
|
|
950
|
+
if (data) {
|
|
951
|
+
contextString = (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
|
|
952
|
+
}
|
|
953
|
+
if (this.includeCopilotReadable) {
|
|
954
|
+
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
955
|
+
}
|
|
956
|
+
const systemMessage = {
|
|
957
|
+
id: "system",
|
|
958
|
+
content: taskSystemMessage(contextString, this.instructions),
|
|
959
|
+
role: "system"
|
|
960
|
+
};
|
|
961
|
+
const messages = [systemMessage];
|
|
962
|
+
const response = yield fetchAndDecodeChatCompletion({
|
|
963
|
+
copilotConfig: context.copilotApiConfig,
|
|
964
|
+
messages,
|
|
965
|
+
tools: context.getChatCompletionFunctionDescriptions(entryPoints),
|
|
966
|
+
headers: context.copilotApiConfig.headers,
|
|
967
|
+
body: context.copilotApiConfig.body
|
|
968
|
+
});
|
|
969
|
+
if (!response.events) {
|
|
970
|
+
throw new Error("Failed to execute task");
|
|
971
|
+
}
|
|
972
|
+
const reader = response.events.getReader();
|
|
973
|
+
let functionCalls = [];
|
|
974
|
+
while (true) {
|
|
975
|
+
const { done, value } = yield reader.read();
|
|
976
|
+
if (done) {
|
|
977
|
+
break;
|
|
978
|
+
}
|
|
979
|
+
if (value.type === "function") {
|
|
980
|
+
functionCalls.push({
|
|
981
|
+
name: value.name,
|
|
982
|
+
arguments: JSON.stringify(value.arguments)
|
|
983
|
+
});
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
if (!functionCalls.length) {
|
|
988
|
+
throw new Error("No function call occurred");
|
|
989
|
+
}
|
|
990
|
+
const functionCallHandler = context.getFunctionCallHandler(entryPoints);
|
|
991
|
+
for (const functionCall of functionCalls) {
|
|
992
|
+
yield functionCallHandler(messages, functionCall);
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
function taskSystemMessage(contextString, instructions) {
|
|
998
|
+
return `
|
|
999
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
1000
|
+
|
|
1001
|
+
Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
|
|
1002
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
1003
|
+
|
|
1004
|
+
The user has provided you with the following context:
|
|
1005
|
+
\`\`\`
|
|
1006
|
+
${contextString}
|
|
1007
|
+
\`\`\`
|
|
1008
|
+
|
|
1009
|
+
They have also provided you with functions you can call to initiate actions on their behalf.
|
|
1010
|
+
|
|
1011
|
+
Please assist them as best you can.
|
|
1012
|
+
|
|
1013
|
+
This is not a conversation, so please do not ask questions. Just call a function without saying anything else.
|
|
1014
|
+
|
|
1015
|
+
The user has given you the following task to complete:
|
|
1016
|
+
|
|
1017
|
+
\`\`\`
|
|
1018
|
+
${instructions}
|
|
1019
|
+
\`\`\`
|
|
1020
|
+
`;
|
|
1021
|
+
}
|
|
905
1022
|
// Annotate the CommonJS export names for ESM import in node:
|
|
906
1023
|
0 && (module.exports = {
|
|
907
1024
|
CopilotContext,
|
|
908
1025
|
CopilotKit,
|
|
1026
|
+
CopilotTask,
|
|
909
1027
|
defaultCopilotContextCategories,
|
|
910
1028
|
fetchAndDecodeChatCompletion,
|
|
911
1029
|
fetchAndDecodeChatCompletionAsText,
|
|
@@ -913,6 +1031,7 @@ If you would like to call a function, call it without saying anything else.
|
|
|
913
1031
|
processMessageStream,
|
|
914
1032
|
useCopilotChat,
|
|
915
1033
|
useCopilotChatV2,
|
|
1034
|
+
useCopilotContext,
|
|
916
1035
|
useMakeCopilotActionable,
|
|
917
1036
|
useMakeCopilotDocumentReadable,
|
|
918
1037
|
useMakeCopilotReadable
|