@copilotkit/react-core 0.25.0-function-calling-fixes.2 → 0.25.0-mme-cloud.1
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 +216 -234
- package/CHANGELOG.md +6 -17
- package/dist/{chunk-3WKJ25L3.mjs → chunk-CGT3AVYT.mjs} +3 -3
- package/dist/chunk-CGT3AVYT.mjs.map +1 -0
- package/dist/{chunk-H3FQWB4B.mjs → chunk-DFCYHW6B.mjs} +29 -13
- package/dist/chunk-DFCYHW6B.mjs.map +1 -0
- package/dist/{chunk-FCEPEM4B.mjs → chunk-GUQTL3VG.mjs} +3 -3
- package/dist/{chunk-GVLCTSXW.mjs → chunk-ICZAFR7J.mjs} +11 -7
- package/dist/chunk-ICZAFR7J.mjs.map +1 -0
- package/dist/{chunk-QGRMN7L3.mjs → chunk-MBLTLCOG.mjs} +3 -3
- package/dist/{chunk-AEUR5JBT.mjs → chunk-MESZST5T.mjs} +8 -6
- package/dist/chunk-MESZST5T.mjs.map +1 -0
- package/dist/{chunk-KJQZ2VEU.mjs → chunk-Q6YXCGNM.mjs} +2 -2
- package/dist/{chunk-H6V2C2AN.mjs → chunk-R3D5GIQT.mjs} +2 -2
- package/dist/{chunk-75RLNHDB.mjs → chunk-WHODFSGA.mjs} +5 -5
- package/dist/chunk-WHODFSGA.mjs.map +1 -0
- package/dist/{chunk-KIKHLN4X.mjs → chunk-XUCMY57N.mjs} +3 -1
- package/dist/chunk-XUCMY57N.mjs.map +1 -0
- package/dist/{chunk-BGB5G33N.mjs → chunk-YMX4OOEL.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +13 -1
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -17
- package/dist/components/copilot-provider/copilotkit.js +25 -17
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +8 -435
- package/dist/components/copilot-provider/copilotkit.mjs.map +1 -1
- package/dist/components/copilot-provider/index.js +25 -17
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +9 -435
- package/dist/components/copilot-provider/index.mjs.map +1 -1
- package/dist/components/index.js +25 -17
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +10 -435
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +18 -2
- package/dist/context/copilot-context.js +2 -0
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +5 -64
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.js +2 -0
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +6 -64
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.js +17 -15
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +23 -518
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-chat.d.ts +8 -2
- package/dist/hooks/use-chat.js +6 -5
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -285
- package/dist/hooks/use-chat.mjs.map +1 -1
- package/dist/hooks/use-copilot-action.d.ts +1 -1
- package/dist/hooks/use-copilot-action.js +4 -9
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +5 -107
- package/dist/hooks/use-copilot-action.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.js +15 -8
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +12 -420
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +4 -65
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.js +2 -0
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +5 -88
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.js +2 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +5 -78
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.js +2 -0
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +5 -78
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +4 -150
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.js +40 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +48 -1103
- package/dist/index.mjs.map +1 -1
- package/dist/lib/copilot-task.js +2 -2
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +10 -192
- package/dist/lib/copilot-task.mjs.map +1 -1
- package/dist/lib/index.js +2 -2
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +11 -192
- package/dist/lib/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/index.js +2 -0
- package/dist/openai-assistants/hooks/index.js.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +14 -228
- package/dist/openai-assistants/hooks/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +7 -45
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +2 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +14 -228
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
- package/dist/openai-assistants/index.js +2 -0
- package/dist/openai-assistants/index.js.map +1 -1
- package/dist/openai-assistants/index.mjs +17 -228
- package/dist/openai-assistants/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/index.mjs +5 -43
- package/dist/openai-assistants/utils/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +4 -43
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
- package/dist/types/index.mjs +1 -0
- package/dist/utils/fetch-chat-completion.js +2 -2
- package/dist/utils/fetch-chat-completion.js.map +1 -1
- package/dist/utils/fetch-chat-completion.mjs +5 -112
- package/dist/utils/fetch-chat-completion.mjs.map +1 -1
- package/package.json +4 -4
- package/src/components/copilot-provider/copilotkit-props.tsx +15 -1
- package/src/components/copilot-provider/copilotkit.tsx +31 -25
- package/src/context/copilot-context.tsx +23 -1
- package/src/hooks/use-chat.ts +16 -5
- package/src/hooks/use-copilot-action.ts +58 -10
- package/src/hooks/use-copilot-chat.ts +5 -1
- package/src/utils/fetch-chat-completion.ts +1 -0
- package/dist/chunk-3WKJ25L3.mjs.map +0 -1
- package/dist/chunk-75RLNHDB.mjs.map +0 -1
- package/dist/chunk-AEUR5JBT.mjs.map +0 -1
- package/dist/chunk-F2JIAPZQ.mjs +0 -14
- package/dist/chunk-F2JIAPZQ.mjs.map +0 -1
- package/dist/chunk-GVLCTSXW.mjs.map +0 -1
- package/dist/chunk-H3FQWB4B.mjs.map +0 -1
- package/dist/chunk-ISKBWE4O.mjs +0 -13
- package/dist/chunk-ISKBWE4O.mjs.map +0 -1
- package/dist/chunk-KIKHLN4X.mjs.map +0 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +0 -23
- package/dist/components/copilot-provider/standard-copilot-api-config.js +0 -38
- package/dist/components/copilot-provider/standard-copilot-api-config.js.map +0 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs +0 -13
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +0 -1
- package/dist/hooks/use-copilot-action-implementation.d.ts +0 -7
- package/dist/hooks/use-copilot-action-implementation.js +0 -140
- package/dist/hooks/use-copilot-action-implementation.js.map +0 -1
- package/dist/hooks/use-copilot-action-implementation.mjs +0 -106
- package/dist/hooks/use-copilot-action-implementation.mjs.map +0 -1
- package/src/components/copilot-provider/standard-copilot-api-config.tsx +0 -28
- package/src/hooks/use-copilot-action-implementation.ts +0 -60
- /package/dist/{chunk-FCEPEM4B.mjs.map → chunk-GUQTL3VG.mjs.map} +0 -0
- /package/dist/{chunk-QGRMN7L3.mjs.map → chunk-MBLTLCOG.mjs.map} +0 -0
- /package/dist/{chunk-KJQZ2VEU.mjs.map → chunk-Q6YXCGNM.mjs.map} +0 -0
- /package/dist/{chunk-H6V2C2AN.mjs.map → chunk-R3D5GIQT.mjs.map} +0 -0
- /package/dist/{chunk-BGB5G33N.mjs.map → chunk-YMX4OOEL.mjs.map} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -1,1109 +1,54 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
-
var __spreadValues = (a, b) => {
|
|
10
|
-
for (var prop in b || (b = {}))
|
|
11
|
-
if (__hasOwnProp.call(b, prop))
|
|
12
|
-
__defNormalProp(a, prop, b[prop]);
|
|
13
|
-
if (__getOwnPropSymbols)
|
|
14
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
-
if (__propIsEnum.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
}
|
|
18
|
-
return a;
|
|
19
|
-
};
|
|
20
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
-
var __objRest = (source, exclude) => {
|
|
22
|
-
var target = {};
|
|
23
|
-
for (var prop in source)
|
|
24
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
25
|
-
target[prop] = source[prop];
|
|
26
|
-
if (source != null && __getOwnPropSymbols)
|
|
27
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
28
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
}
|
|
31
|
-
return target;
|
|
32
|
-
};
|
|
33
|
-
var __async = (__this, __arguments, generator) => {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
var fulfilled = (value) => {
|
|
36
|
-
try {
|
|
37
|
-
step(generator.next(value));
|
|
38
|
-
} catch (e) {
|
|
39
|
-
reject(e);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
var rejected = (value) => {
|
|
43
|
-
try {
|
|
44
|
-
step(generator.throw(value));
|
|
45
|
-
} catch (e) {
|
|
46
|
-
reject(e);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
50
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// src/components/copilot-provider/copilotkit.tsx
|
|
55
|
-
import { useCallback as useCallback3, useRef, useState } from "react";
|
|
56
|
-
|
|
57
|
-
// src/context/copilot-context.tsx
|
|
58
|
-
import React from "react";
|
|
59
|
-
var emptyCopilotContext = {
|
|
60
|
-
entryPoints: {},
|
|
61
|
-
setEntryPoint: () => {
|
|
62
|
-
},
|
|
63
|
-
removeEntryPoint: () => {
|
|
64
|
-
},
|
|
65
|
-
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
66
|
-
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
67
|
-
})),
|
|
68
|
-
chatComponentsCache: { current: {} },
|
|
69
|
-
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
70
|
-
addContext: () => "",
|
|
71
|
-
removeContext: () => {
|
|
72
|
-
},
|
|
73
|
-
getDocumentsContext: (categories) => returnAndThrowInDebug([]),
|
|
74
|
-
addDocumentContext: () => returnAndThrowInDebug(""),
|
|
75
|
-
removeDocumentContext: () => {
|
|
76
|
-
},
|
|
77
|
-
copilotApiConfig: new class {
|
|
78
|
-
get chatApiEndpoint() {
|
|
79
|
-
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
80
|
-
}
|
|
81
|
-
get chatApiEndpointV2() {
|
|
82
|
-
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
83
|
-
}
|
|
84
|
-
get headers() {
|
|
85
|
-
return {};
|
|
86
|
-
}
|
|
87
|
-
get body() {
|
|
88
|
-
return {};
|
|
89
|
-
}
|
|
90
|
-
}()
|
|
91
|
-
};
|
|
92
|
-
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
93
|
-
function useCopilotContext() {
|
|
94
|
-
return React.useContext(CopilotContext);
|
|
95
|
-
}
|
|
96
|
-
function returnAndThrowInDebug(value) {
|
|
97
|
-
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
98
|
-
return value;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/hooks/use-tree.ts
|
|
102
|
-
import { nanoid } from "nanoid";
|
|
103
|
-
import { useCallback, useReducer } from "react";
|
|
104
|
-
var removeNode = (nodes, id) => {
|
|
105
|
-
return nodes.reduce((result, node) => {
|
|
106
|
-
if (node.id !== id) {
|
|
107
|
-
const newNode = __spreadProps(__spreadValues({}, node), { children: removeNode(node.children, id) });
|
|
108
|
-
result.push(newNode);
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
}, []);
|
|
112
|
-
};
|
|
113
|
-
var addNode = (nodes, newNode, parentId) => {
|
|
114
|
-
if (!parentId) {
|
|
115
|
-
return [...nodes, newNode];
|
|
116
|
-
}
|
|
117
|
-
return nodes.map((node) => {
|
|
118
|
-
if (node.id === parentId) {
|
|
119
|
-
return __spreadProps(__spreadValues({}, node), { children: [...node.children, newNode] });
|
|
120
|
-
} else if (node.children.length) {
|
|
121
|
-
return __spreadProps(__spreadValues({}, node), { children: addNode(node.children, newNode, parentId) });
|
|
122
|
-
}
|
|
123
|
-
return node;
|
|
124
|
-
});
|
|
125
|
-
};
|
|
126
|
-
var treeIndentationRepresentation = (index, indentLevel) => {
|
|
127
|
-
if (indentLevel === 0) {
|
|
128
|
-
return (index + 1).toString();
|
|
129
|
-
} else if (indentLevel === 1) {
|
|
130
|
-
return String.fromCharCode(65 + index);
|
|
131
|
-
} else if (indentLevel === 2) {
|
|
132
|
-
return String.fromCharCode(97 + index);
|
|
133
|
-
} else {
|
|
134
|
-
return "-";
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
var printNode = (node, prefix = "", indentLevel = 0) => {
|
|
138
|
-
const indent = " ".repeat(3).repeat(indentLevel);
|
|
139
|
-
const prefixPlusIndentLength = prefix.length + indent.length;
|
|
140
|
-
const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
|
|
141
|
-
const valueLines = node.value.split("\n");
|
|
142
|
-
const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
|
|
143
|
-
const outputSubsequentLines = valueLines.slice(1).map((line) => `${subsequentLinesPrefix}${line}`).join("\n");
|
|
144
|
-
let output = `${outputFirstLine}
|
|
145
|
-
`;
|
|
146
|
-
if (outputSubsequentLines) {
|
|
147
|
-
output += `${outputSubsequentLines}
|
|
148
|
-
`;
|
|
149
|
-
}
|
|
150
|
-
const childPrePrefix = " ".repeat(prefix.length);
|
|
151
|
-
node.children.forEach(
|
|
152
|
-
(child, index) => output += printNode(
|
|
153
|
-
child,
|
|
154
|
-
`${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
|
|
155
|
-
indentLevel + 1
|
|
156
|
-
)
|
|
157
|
-
);
|
|
158
|
-
return output;
|
|
159
|
-
};
|
|
160
|
-
function treeReducer(state, action) {
|
|
161
|
-
switch (action.type) {
|
|
162
|
-
case "ADD_NODE": {
|
|
163
|
-
const { value, parentId, id: newNodeId } = action;
|
|
164
|
-
const newNode = {
|
|
165
|
-
id: newNodeId,
|
|
166
|
-
value,
|
|
167
|
-
children: [],
|
|
168
|
-
categories: new Set(action.categories)
|
|
169
|
-
};
|
|
170
|
-
try {
|
|
171
|
-
return addNode(state, newNode, parentId);
|
|
172
|
-
} catch (error) {
|
|
173
|
-
console.error(`Error while adding node with id ${newNodeId}: ${error}`);
|
|
174
|
-
return state;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
case "REMOVE_NODE":
|
|
178
|
-
return removeNode(state, action.id);
|
|
179
|
-
default:
|
|
180
|
-
return state;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
var useTree = () => {
|
|
184
|
-
const [tree, dispatch] = useReducer(treeReducer, []);
|
|
185
|
-
const addElement = useCallback(
|
|
186
|
-
(value, categories, parentId) => {
|
|
187
|
-
const newNodeId = nanoid();
|
|
188
|
-
dispatch({
|
|
189
|
-
type: "ADD_NODE",
|
|
190
|
-
value,
|
|
191
|
-
parentId,
|
|
192
|
-
id: newNodeId,
|
|
193
|
-
categories
|
|
194
|
-
});
|
|
195
|
-
return newNodeId;
|
|
196
|
-
},
|
|
197
|
-
[]
|
|
198
|
-
);
|
|
199
|
-
const removeElement = useCallback((id) => {
|
|
200
|
-
dispatch({ type: "REMOVE_NODE", id });
|
|
201
|
-
}, []);
|
|
202
|
-
const printTree = useCallback(
|
|
203
|
-
(categories) => {
|
|
204
|
-
const categoriesSet = new Set(categories);
|
|
205
|
-
let output = "";
|
|
206
|
-
tree.forEach((node, index) => {
|
|
207
|
-
if (!setsHaveIntersection(categoriesSet, node.categories)) {
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
if (index !== 0) {
|
|
211
|
-
output += "\n";
|
|
212
|
-
}
|
|
213
|
-
output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);
|
|
214
|
-
});
|
|
215
|
-
return output;
|
|
216
|
-
},
|
|
217
|
-
[tree]
|
|
218
|
-
);
|
|
219
|
-
return { tree, addElement, printTree, removeElement };
|
|
220
|
-
};
|
|
221
|
-
var use_tree_default = useTree;
|
|
222
|
-
function setsHaveIntersection(setA, setB) {
|
|
223
|
-
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
224
|
-
for (let item of smallerSet) {
|
|
225
|
-
if (largerSet.has(item)) {
|
|
226
|
-
return true;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return false;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// src/components/copilot-provider/copilotkit.tsx
|
|
233
|
-
import { actionToChatCompletionFunction } from "@copilotkit/shared";
|
|
234
|
-
|
|
235
|
-
// src/hooks/use-flat-category-store.ts
|
|
236
|
-
import { nanoid as nanoid2 } from "nanoid";
|
|
237
|
-
import { useCallback as useCallback2, useReducer as useReducer2 } from "react";
|
|
238
|
-
var useFlatCategoryStore = () => {
|
|
239
|
-
const [elements, dispatch] = useReducer2(flatCategoryStoreReducer, /* @__PURE__ */ new Map());
|
|
240
|
-
const addElement = useCallback2((value, categories) => {
|
|
241
|
-
const newId = nanoid2();
|
|
242
|
-
dispatch({
|
|
243
|
-
type: "ADD_ELEMENT",
|
|
244
|
-
value,
|
|
245
|
-
id: newId,
|
|
246
|
-
categories
|
|
247
|
-
});
|
|
248
|
-
return newId;
|
|
249
|
-
}, []);
|
|
250
|
-
const removeElement = useCallback2((id) => {
|
|
251
|
-
dispatch({ type: "REMOVE_ELEMENT", id });
|
|
252
|
-
}, []);
|
|
253
|
-
const allElements = useCallback2(
|
|
254
|
-
(categories) => {
|
|
255
|
-
const categoriesSet = new Set(categories);
|
|
256
|
-
const result = [];
|
|
257
|
-
elements.forEach((element) => {
|
|
258
|
-
if (setsHaveIntersection2(categoriesSet, element.categories)) {
|
|
259
|
-
result.push(element.value);
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
return result;
|
|
263
|
-
},
|
|
264
|
-
[elements]
|
|
265
|
-
);
|
|
266
|
-
return { addElement, removeElement, allElements };
|
|
267
|
-
};
|
|
268
|
-
var use_flat_category_store_default = useFlatCategoryStore;
|
|
269
|
-
function flatCategoryStoreReducer(state, action) {
|
|
270
|
-
switch (action.type) {
|
|
271
|
-
case "ADD_ELEMENT": {
|
|
272
|
-
const { value, id, categories } = action;
|
|
273
|
-
const newElement = {
|
|
274
|
-
id,
|
|
275
|
-
value,
|
|
276
|
-
categories: new Set(categories)
|
|
277
|
-
};
|
|
278
|
-
const newState = new Map(state);
|
|
279
|
-
newState.set(id, newElement);
|
|
280
|
-
return newState;
|
|
281
|
-
}
|
|
282
|
-
case "REMOVE_ELEMENT": {
|
|
283
|
-
const newState = new Map(state);
|
|
284
|
-
newState.delete(action.id);
|
|
285
|
-
return newState;
|
|
286
|
-
}
|
|
287
|
-
default:
|
|
288
|
-
return state;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
function setsHaveIntersection2(setA, setB) {
|
|
292
|
-
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
293
|
-
for (let item of smallerSet) {
|
|
294
|
-
if (largerSet.has(item)) {
|
|
295
|
-
return true;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return false;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// src/components/copilot-provider/standard-copilot-api-config.tsx
|
|
302
|
-
var StandardCopilotApiConfig = class {
|
|
303
|
-
constructor(chatApiEndpoint, chatApiEndpointV2, headers, body) {
|
|
304
|
-
this.chatApiEndpoint = chatApiEndpoint;
|
|
305
|
-
this.chatApiEndpointV2 = chatApiEndpointV2;
|
|
306
|
-
this.headers = headers;
|
|
307
|
-
this.body = body;
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
// src/components/copilot-provider/copilotkit.tsx
|
|
312
|
-
import { jsx } from "react/jsx-runtime";
|
|
313
|
-
function CopilotKit(_a) {
|
|
314
|
-
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
315
|
-
const [entryPoints, setEntryPoints] = useState({});
|
|
316
|
-
const chatComponentsCache = useRef({});
|
|
317
|
-
const { addElement, removeElement, printTree } = use_tree_default();
|
|
318
|
-
const {
|
|
319
|
-
addElement: addDocument,
|
|
320
|
-
removeElement: removeDocument,
|
|
321
|
-
allElements: allDocuments
|
|
322
|
-
} = use_flat_category_store_default();
|
|
323
|
-
const setEntryPoint = useCallback3((id, entryPoint) => {
|
|
324
|
-
setEntryPoints((prevPoints) => {
|
|
325
|
-
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
326
|
-
[id]: entryPoint
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
}, []);
|
|
330
|
-
const removeEntryPoint = useCallback3((id) => {
|
|
331
|
-
setEntryPoints((prevPoints) => {
|
|
332
|
-
const newPoints = __spreadValues({}, prevPoints);
|
|
333
|
-
delete newPoints[id];
|
|
334
|
-
return newPoints;
|
|
335
|
-
});
|
|
336
|
-
}, []);
|
|
337
|
-
const getContextString = useCallback3(
|
|
338
|
-
(documents, categories) => {
|
|
339
|
-
const documentsString = documents.map((document) => {
|
|
340
|
-
return `${document.name} (${document.sourceApplication}):
|
|
341
|
-
${document.getContents()}`;
|
|
342
|
-
}).join("\n\n");
|
|
343
|
-
const nonDocumentStrings = printTree(categories);
|
|
344
|
-
return `${documentsString}
|
|
345
|
-
|
|
346
|
-
${nonDocumentStrings}`;
|
|
347
|
-
},
|
|
348
|
-
[printTree]
|
|
349
|
-
);
|
|
350
|
-
const addContext = useCallback3(
|
|
351
|
-
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
352
|
-
return addElement(context, categories, parentId);
|
|
353
|
-
},
|
|
354
|
-
[addElement]
|
|
355
|
-
);
|
|
356
|
-
const removeContext = useCallback3(
|
|
357
|
-
(id) => {
|
|
358
|
-
removeElement(id);
|
|
359
|
-
},
|
|
360
|
-
[removeElement]
|
|
361
|
-
);
|
|
362
|
-
const getChatCompletionFunctionDescriptions = useCallback3(
|
|
363
|
-
(customEntryPoints) => {
|
|
364
|
-
return entryPointsToChatCompletionFunctions(Object.values(customEntryPoints || entryPoints));
|
|
365
|
-
},
|
|
366
|
-
[entryPoints]
|
|
367
|
-
);
|
|
368
|
-
const getFunctionCallHandler = useCallback3(
|
|
369
|
-
(customEntryPoints) => {
|
|
370
|
-
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || entryPoints));
|
|
371
|
-
},
|
|
372
|
-
[entryPoints]
|
|
373
|
-
);
|
|
374
|
-
const getDocumentsContext = useCallback3(
|
|
375
|
-
(categories) => {
|
|
376
|
-
return allDocuments(categories);
|
|
377
|
-
},
|
|
378
|
-
[allDocuments]
|
|
379
|
-
);
|
|
380
|
-
const addDocumentContext = useCallback3(
|
|
381
|
-
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
382
|
-
return addDocument(documentPointer, categories);
|
|
383
|
-
},
|
|
384
|
-
[addDocument]
|
|
385
|
-
);
|
|
386
|
-
const removeDocumentContext = useCallback3(
|
|
387
|
-
(documentId) => {
|
|
388
|
-
removeDocument(documentId);
|
|
389
|
-
},
|
|
390
|
-
[removeDocument]
|
|
391
|
-
);
|
|
392
|
-
const copilotApiConfig = new StandardCopilotApiConfig(
|
|
393
|
-
props.url,
|
|
394
|
-
`${props.url}/v2`,
|
|
395
|
-
props.headers || {},
|
|
396
|
-
__spreadValues(__spreadValues({}, props.body), props.backendOnlyProps)
|
|
397
|
-
);
|
|
398
|
-
return /* @__PURE__ */ jsx(
|
|
399
|
-
CopilotContext.Provider,
|
|
400
|
-
{
|
|
401
|
-
value: {
|
|
402
|
-
entryPoints,
|
|
403
|
-
chatComponentsCache,
|
|
404
|
-
getChatCompletionFunctionDescriptions,
|
|
405
|
-
getFunctionCallHandler,
|
|
406
|
-
setEntryPoint,
|
|
407
|
-
removeEntryPoint,
|
|
408
|
-
getContextString,
|
|
409
|
-
addContext,
|
|
410
|
-
removeContext,
|
|
411
|
-
getDocumentsContext,
|
|
412
|
-
addDocumentContext,
|
|
413
|
-
removeDocumentContext,
|
|
414
|
-
copilotApiConfig
|
|
415
|
-
},
|
|
416
|
-
children
|
|
417
|
-
}
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
var defaultCopilotContextCategories = ["global"];
|
|
421
|
-
function entryPointsToChatCompletionFunctions(actions) {
|
|
422
|
-
return actions.map(actionToChatCompletionFunction);
|
|
423
|
-
}
|
|
424
|
-
function entryPointsToFunctionCallHandler(actions) {
|
|
425
|
-
return (chatMessages, functionCall) => __async(this, null, function* () {
|
|
426
|
-
let actionsByFunctionName = {};
|
|
427
|
-
for (let action2 of actions) {
|
|
428
|
-
actionsByFunctionName[action2.name] = action2;
|
|
429
|
-
}
|
|
430
|
-
const action = actionsByFunctionName[functionCall.name || ""];
|
|
431
|
-
if (action) {
|
|
432
|
-
let functionCallArguments = [];
|
|
433
|
-
if (functionCall.arguments) {
|
|
434
|
-
functionCallArguments = JSON.parse(functionCall.arguments);
|
|
435
|
-
}
|
|
436
|
-
return yield action.handler(functionCallArguments);
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// src/hooks/use-copilot-chat.ts
|
|
442
|
-
import { useMemo, useContext } from "react";
|
|
443
|
-
|
|
444
|
-
// src/hooks/use-chat.ts
|
|
445
|
-
import { useRef as useRef2, useState as useState2 } from "react";
|
|
2
|
+
import "./chunk-EFZPSZWO.mjs";
|
|
3
|
+
import "./chunk-LUGEI4YQ.mjs";
|
|
446
4
|
import {
|
|
447
|
-
|
|
448
|
-
} from "
|
|
449
|
-
import
|
|
450
|
-
|
|
451
|
-
// src/utils/fetch-chat-completion.ts
|
|
5
|
+
CopilotTask
|
|
6
|
+
} from "./chunk-MBLTLCOG.mjs";
|
|
7
|
+
import "./chunk-7GFKOIO7.mjs";
|
|
8
|
+
import "./chunk-BABVSMJR.mjs";
|
|
452
9
|
import {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
[EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps)
|
|
496
|
-
};
|
|
497
|
-
} else {
|
|
498
|
-
return {};
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
function fetchAndDecodeChatCompletion(params) {
|
|
502
|
-
return __async(this, null, function* () {
|
|
503
|
-
const response = yield fetchChatCompletion(params);
|
|
504
|
-
if (!response.ok || !response.body) {
|
|
505
|
-
response.events = null;
|
|
506
|
-
} else {
|
|
507
|
-
const events = yield decodeChatCompletion(parseChatCompletion(response.body));
|
|
508
|
-
response.events = events;
|
|
509
|
-
}
|
|
510
|
-
return response;
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
function fetchAndDecodeChatCompletionAsText(params) {
|
|
514
|
-
return __async(this, null, function* () {
|
|
515
|
-
const response = yield fetchChatCompletion(params);
|
|
516
|
-
if (!response.ok || !response.body) {
|
|
517
|
-
response.events = null;
|
|
518
|
-
} else {
|
|
519
|
-
const events = yield decodeChatCompletionAsText(
|
|
520
|
-
decodeChatCompletion(parseChatCompletion(response.body))
|
|
521
|
-
);
|
|
522
|
-
response.events = events;
|
|
523
|
-
}
|
|
524
|
-
return response;
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// src/hooks/use-chat.ts
|
|
529
|
-
import untruncateJson from "untruncate-json";
|
|
530
|
-
function useChat(options) {
|
|
531
|
-
const [messages, setMessages] = useState2([]);
|
|
532
|
-
const [input, setInput] = useState2("");
|
|
533
|
-
const [isLoading, setIsLoading] = useState2(false);
|
|
534
|
-
const abortControllerRef = useRef2();
|
|
535
|
-
const threadIdRef = useRef2(null);
|
|
536
|
-
const runIdRef = useRef2(null);
|
|
537
|
-
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
538
|
-
setIsLoading(true);
|
|
539
|
-
const newMessages = [
|
|
540
|
-
{
|
|
541
|
-
id: nanoid3(),
|
|
542
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
543
|
-
content: "",
|
|
544
|
-
role: "assistant"
|
|
545
|
-
}
|
|
546
|
-
];
|
|
547
|
-
const abortController = new AbortController();
|
|
548
|
-
abortControllerRef.current = abortController;
|
|
549
|
-
setMessages([...messages2, ...newMessages]);
|
|
550
|
-
const copilotConfigBody = options.copilotConfig.body || {};
|
|
551
|
-
if (threadIdRef.current) {
|
|
552
|
-
copilotConfigBody.threadId = threadIdRef.current;
|
|
553
|
-
}
|
|
554
|
-
if (runIdRef.current) {
|
|
555
|
-
copilotConfigBody.runId = runIdRef.current;
|
|
556
|
-
}
|
|
557
|
-
const messagesWithContext = [...options.initialMessages || [], ...messages2];
|
|
558
|
-
const response = yield fetchAndDecodeChatCompletion({
|
|
559
|
-
copilotConfig: __spreadProps(__spreadValues({}, options.copilotConfig), { body: copilotConfigBody }),
|
|
560
|
-
messages: messagesWithContext,
|
|
561
|
-
tools: options.tools,
|
|
562
|
-
headers: options.headers,
|
|
563
|
-
signal: abortController.signal
|
|
564
|
-
});
|
|
565
|
-
if (response.headers.get("threadid")) {
|
|
566
|
-
threadIdRef.current = response.headers.get("threadid");
|
|
567
|
-
}
|
|
568
|
-
if (response.headers.get("runid")) {
|
|
569
|
-
runIdRef.current = response.headers.get("runid");
|
|
570
|
-
}
|
|
571
|
-
if (!response.events) {
|
|
572
|
-
throw new Error("Failed to fetch chat completion");
|
|
573
|
-
}
|
|
574
|
-
const reader = response.events.getReader();
|
|
575
|
-
let feedback = false;
|
|
576
|
-
try {
|
|
577
|
-
while (true) {
|
|
578
|
-
const { done, value } = yield reader.read();
|
|
579
|
-
if (done) {
|
|
580
|
-
break;
|
|
581
|
-
}
|
|
582
|
-
let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);
|
|
583
|
-
if (value.type === "content") {
|
|
584
|
-
if (currentMessage.function_call || currentMessage.role === "function") {
|
|
585
|
-
currentMessage = {
|
|
586
|
-
id: nanoid3(),
|
|
587
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
588
|
-
content: "",
|
|
589
|
-
role: "assistant"
|
|
590
|
-
};
|
|
591
|
-
newMessages.push(currentMessage);
|
|
592
|
-
}
|
|
593
|
-
currentMessage.content += value.content;
|
|
594
|
-
newMessages[newMessages.length - 1] = currentMessage;
|
|
595
|
-
setMessages([...messages2, ...newMessages]);
|
|
596
|
-
} else if (value.type === "result") {
|
|
597
|
-
currentMessage = {
|
|
598
|
-
id: nanoid3(),
|
|
599
|
-
role: "function",
|
|
600
|
-
content: value.content,
|
|
601
|
-
name: value.name
|
|
602
|
-
};
|
|
603
|
-
newMessages.push(currentMessage);
|
|
604
|
-
setMessages([...messages2, ...newMessages]);
|
|
605
|
-
feedback = true;
|
|
606
|
-
} else if (value.type === "function" || value.type === "partial") {
|
|
607
|
-
if (currentMessage.content != "" || currentMessage.function_call || currentMessage.role == "function") {
|
|
608
|
-
currentMessage = {
|
|
609
|
-
id: nanoid3(),
|
|
610
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
611
|
-
content: "",
|
|
612
|
-
role: "assistant"
|
|
613
|
-
};
|
|
614
|
-
newMessages.push(currentMessage);
|
|
615
|
-
}
|
|
616
|
-
if (value.type === "function") {
|
|
617
|
-
currentMessage.function_call = {
|
|
618
|
-
name: value.name,
|
|
619
|
-
arguments: JSON.stringify(value.arguments),
|
|
620
|
-
scope: value.scope
|
|
621
|
-
};
|
|
622
|
-
} else if (value.type === "partial") {
|
|
623
|
-
let partialArguments = {};
|
|
624
|
-
try {
|
|
625
|
-
partialArguments = JSON.parse(untruncateJson(value.arguments));
|
|
626
|
-
} catch (e) {
|
|
627
|
-
}
|
|
628
|
-
currentMessage.partialFunctionCall = {
|
|
629
|
-
name: value.name,
|
|
630
|
-
arguments: partialArguments
|
|
631
|
-
};
|
|
632
|
-
}
|
|
633
|
-
newMessages[newMessages.length - 1] = currentMessage;
|
|
634
|
-
setMessages([...messages2, ...newMessages]);
|
|
635
|
-
if (value.type === "function") {
|
|
636
|
-
try {
|
|
637
|
-
if (options.onFunctionCall && value.scope === "client") {
|
|
638
|
-
const result = yield options.onFunctionCall(
|
|
639
|
-
messages2,
|
|
640
|
-
currentMessage.function_call
|
|
641
|
-
);
|
|
642
|
-
currentMessage = {
|
|
643
|
-
id: nanoid3(),
|
|
644
|
-
role: "function",
|
|
645
|
-
content: encodeResult(result),
|
|
646
|
-
name: currentMessage.function_call.name
|
|
647
|
-
};
|
|
648
|
-
newMessages.push(currentMessage);
|
|
649
|
-
setMessages([...messages2, ...newMessages]);
|
|
650
|
-
feedback = true;
|
|
651
|
-
}
|
|
652
|
-
} catch (error) {
|
|
653
|
-
console.error("Failed to execute function call", error);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
if (feedback) {
|
|
659
|
-
return yield runChatCompletion([...messages2, ...newMessages]);
|
|
660
|
-
} else {
|
|
661
|
-
return newMessages.slice();
|
|
662
|
-
}
|
|
663
|
-
} finally {
|
|
664
|
-
setIsLoading(false);
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
668
|
-
yield runChatCompletion(messages2);
|
|
669
|
-
});
|
|
670
|
-
const append = (message) => __async(this, null, function* () {
|
|
671
|
-
if (isLoading) {
|
|
672
|
-
return;
|
|
673
|
-
}
|
|
674
|
-
const newMessages = [...messages, message];
|
|
675
|
-
setMessages(newMessages);
|
|
676
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
677
|
-
});
|
|
678
|
-
const reload = () => __async(this, null, function* () {
|
|
679
|
-
if (isLoading || messages.length === 0) {
|
|
680
|
-
return;
|
|
681
|
-
}
|
|
682
|
-
let newMessages = [...messages];
|
|
683
|
-
const lastMessage = messages[messages.length - 1];
|
|
684
|
-
if (lastMessage.role === "assistant") {
|
|
685
|
-
newMessages = newMessages.slice(0, -1);
|
|
686
|
-
}
|
|
687
|
-
setMessages(newMessages);
|
|
688
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
689
|
-
});
|
|
690
|
-
const stop = () => {
|
|
691
|
-
var _a;
|
|
692
|
-
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
693
|
-
};
|
|
694
|
-
return {
|
|
695
|
-
messages,
|
|
696
|
-
append,
|
|
697
|
-
reload,
|
|
698
|
-
stop,
|
|
699
|
-
isLoading,
|
|
700
|
-
input,
|
|
701
|
-
setInput
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
// src/hooks/use-copilot-chat.ts
|
|
706
|
-
function useCopilotChat(_a) {
|
|
707
|
-
var _b = _a, {
|
|
708
|
-
makeSystemMessage,
|
|
709
|
-
additionalInstructions
|
|
710
|
-
} = _b, options = __objRest(_b, [
|
|
711
|
-
"makeSystemMessage",
|
|
712
|
-
"additionalInstructions"
|
|
713
|
-
]);
|
|
714
|
-
const {
|
|
715
|
-
getContextString,
|
|
716
|
-
getChatCompletionFunctionDescriptions,
|
|
717
|
-
getFunctionCallHandler,
|
|
718
|
-
copilotApiConfig
|
|
719
|
-
} = useContext(CopilotContext);
|
|
720
|
-
const systemMessage = useMemo(() => {
|
|
721
|
-
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
722
|
-
const contextString = getContextString([], defaultCopilotContextCategories);
|
|
723
|
-
return {
|
|
724
|
-
id: "system",
|
|
725
|
-
content: systemMessageMaker(contextString, additionalInstructions),
|
|
726
|
-
role: "system"
|
|
727
|
-
};
|
|
728
|
-
}, [getContextString, makeSystemMessage, additionalInstructions]);
|
|
729
|
-
const functionDescriptions = useMemo(() => {
|
|
730
|
-
return getChatCompletionFunctionDescriptions();
|
|
731
|
-
}, [getChatCompletionFunctionDescriptions]);
|
|
732
|
-
const { messages, append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
733
|
-
copilotConfig: copilotApiConfig,
|
|
734
|
-
id: options.id,
|
|
735
|
-
initialMessages: [systemMessage].concat(options.initialMessages || []),
|
|
736
|
-
tools: functionDescriptions,
|
|
737
|
-
onFunctionCall: getFunctionCallHandler(),
|
|
738
|
-
headers: __spreadValues({}, options.headers),
|
|
739
|
-
body: __spreadValues({}, options.body)
|
|
740
|
-
}));
|
|
741
|
-
const visibleMessages = messages.filter(
|
|
742
|
-
(message) => message.role === "user" || message.role === "assistant" || message.role === "function"
|
|
743
|
-
);
|
|
744
|
-
return {
|
|
745
|
-
visibleMessages,
|
|
746
|
-
append,
|
|
747
|
-
reload,
|
|
748
|
-
stop,
|
|
749
|
-
isLoading,
|
|
750
|
-
input,
|
|
751
|
-
setInput
|
|
752
|
-
};
|
|
753
|
-
}
|
|
754
|
-
function defaultSystemMessage(contextString, additionalInstructions) {
|
|
755
|
-
return `
|
|
756
|
-
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
757
|
-
|
|
758
|
-
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.
|
|
759
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
760
|
-
|
|
761
|
-
The user has provided you with the following context:
|
|
762
|
-
\`\`\`
|
|
763
|
-
${contextString}
|
|
764
|
-
\`\`\`
|
|
765
|
-
|
|
766
|
-
They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
|
|
767
|
-
|
|
768
|
-
Please assist them as best you can.
|
|
769
|
-
|
|
770
|
-
You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
|
|
771
|
-
|
|
772
|
-
If you would like to call a function, call it without saying anything else.
|
|
773
|
-
` + (additionalInstructions ? `
|
|
774
|
-
|
|
775
|
-
${additionalInstructions}` : "");
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
// src/hooks/use-make-copilot-actionable.ts
|
|
779
|
-
import { useRef as useRef3, useContext as useContext2, useEffect, useMemo as useMemo2 } from "react";
|
|
780
|
-
import { nanoid as nanoid4 } from "nanoid";
|
|
781
|
-
import { annotatedFunctionToAction } from "@copilotkit/shared";
|
|
782
|
-
function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
783
|
-
const idRef = useRef3(nanoid4());
|
|
784
|
-
const { setEntryPoint, removeEntryPoint } = useContext2(CopilotContext);
|
|
785
|
-
const memoizedAnnotatedFunction = useMemo2(
|
|
786
|
-
() => ({
|
|
787
|
-
name: annotatedFunction.name,
|
|
788
|
-
description: annotatedFunction.description,
|
|
789
|
-
argumentAnnotations: annotatedFunction.argumentAnnotations,
|
|
790
|
-
implementation: annotatedFunction.implementation
|
|
791
|
-
}),
|
|
792
|
-
dependencies
|
|
793
|
-
);
|
|
794
|
-
useEffect(() => {
|
|
795
|
-
const action = annotatedFunctionToAction(memoizedAnnotatedFunction);
|
|
796
|
-
setEntryPoint(idRef.current, action);
|
|
797
|
-
return () => {
|
|
798
|
-
removeEntryPoint(idRef.current);
|
|
799
|
-
};
|
|
800
|
-
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
// src/hooks/use-copilot-action-implementation.ts
|
|
804
|
-
import { useRef as useRef4, useContext as useContext3, useEffect as useEffect2 } from "react";
|
|
805
|
-
import { nanoid as nanoid5 } from "nanoid";
|
|
806
|
-
function useCopilotActionImplementation(action, dependencies) {
|
|
807
|
-
const { setEntryPoint, removeEntryPoint, entryPoints, chatComponentsCache } = useContext3(CopilotContext);
|
|
808
|
-
const idRef = useRef4(nanoid5());
|
|
809
|
-
if (dependencies === void 0) {
|
|
810
|
-
if (entryPoints[idRef.current]) {
|
|
811
|
-
entryPoints[idRef.current].handler = action.handler;
|
|
812
|
-
if (typeof action.render === "function") {
|
|
813
|
-
if (chatComponentsCache.current !== null) {
|
|
814
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
useEffect2(() => {
|
|
820
|
-
setEntryPoint(idRef.current, action);
|
|
821
|
-
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
822
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
823
|
-
}
|
|
824
|
-
return () => {
|
|
825
|
-
removeEntryPoint(idRef.current);
|
|
826
|
-
};
|
|
827
|
-
}, [
|
|
828
|
-
setEntryPoint,
|
|
829
|
-
removeEntryPoint,
|
|
830
|
-
action.description,
|
|
831
|
-
action.name,
|
|
832
|
-
// This should be faster than deep equality checking
|
|
833
|
-
// In addition, all major JS engines guarantee the order of object keys
|
|
834
|
-
JSON.stringify(action.parameters),
|
|
835
|
-
// include render only if it's a string
|
|
836
|
-
typeof action.render === "string" ? action.render : void 0,
|
|
837
|
-
// dependencies set by the developer
|
|
838
|
-
...dependencies || []
|
|
839
|
-
]);
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
// src/hooks/use-copilot-action.ts
|
|
843
|
-
function useCopilotAction(action, dependencies) {
|
|
844
|
-
return useCopilotActionImplementation(action, dependencies);
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
// src/hooks/use-make-copilot-readable.ts
|
|
848
|
-
import { useContext as useContext4, useEffect as useEffect3, useRef as useRef5 } from "react";
|
|
849
|
-
function useMakeCopilotReadable(information, parentId, categories) {
|
|
850
|
-
const { addContext, removeContext } = useContext4(CopilotContext);
|
|
851
|
-
const idRef = useRef5();
|
|
852
|
-
useEffect3(() => {
|
|
853
|
-
const id = addContext(information, parentId, categories);
|
|
854
|
-
idRef.current = id;
|
|
855
|
-
return () => {
|
|
856
|
-
removeContext(id);
|
|
857
|
-
};
|
|
858
|
-
}, [information, parentId, addContext, removeContext]);
|
|
859
|
-
return idRef.current;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
// src/hooks/use-make-copilot-document-readable.ts
|
|
863
|
-
import { useContext as useContext5, useEffect as useEffect4, useRef as useRef6 } from "react";
|
|
864
|
-
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
865
|
-
const { addDocumentContext, removeDocumentContext } = useContext5(CopilotContext);
|
|
866
|
-
const idRef = useRef6();
|
|
867
|
-
useEffect4(() => {
|
|
868
|
-
const id = addDocumentContext(document, categories);
|
|
869
|
-
idRef.current = id;
|
|
870
|
-
return () => {
|
|
871
|
-
removeDocumentContext(id);
|
|
872
|
-
};
|
|
873
|
-
}, [addDocumentContext, removeDocumentContext, ...dependencies]);
|
|
874
|
-
return idRef.current;
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
// src/openai-assistants/hooks/use-copilot-chat-v2.ts
|
|
878
|
-
import { useContext as useContext6, useMemo as useMemo3, useState as useState3 } from "react";
|
|
879
|
-
|
|
880
|
-
// src/openai-assistants/utils/process-message-stream.ts
|
|
881
|
-
function processMessageStream(reader, processMessage) {
|
|
882
|
-
return __async(this, null, function* () {
|
|
883
|
-
const decoder = new TextDecoder();
|
|
884
|
-
let buffer = "";
|
|
885
|
-
while (true) {
|
|
886
|
-
const { done, value } = yield reader.read();
|
|
887
|
-
if (done) {
|
|
888
|
-
if (buffer.length > 0) {
|
|
889
|
-
processMessage(buffer);
|
|
890
|
-
}
|
|
891
|
-
break;
|
|
892
|
-
}
|
|
893
|
-
buffer += decoder.decode(value, { stream: true });
|
|
894
|
-
let endIndex;
|
|
895
|
-
while ((endIndex = buffer.indexOf("\n")) !== -1) {
|
|
896
|
-
processMessage(buffer.substring(0, endIndex).trim());
|
|
897
|
-
buffer = buffer.substring(endIndex + 1);
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
// src/openai-assistants/hooks/use-copilot-chat-v2.ts
|
|
904
|
-
import { parseStreamPart } from "@copilotkit/shared";
|
|
905
|
-
function useCopilotChatV2(options) {
|
|
906
|
-
const {
|
|
907
|
-
getContextString,
|
|
908
|
-
getChatCompletionFunctionDescriptions,
|
|
909
|
-
getFunctionCallHandler,
|
|
910
|
-
copilotApiConfig
|
|
911
|
-
} = useContext6(CopilotContext);
|
|
912
|
-
const [messages, setMessages] = useState3([]);
|
|
913
|
-
const [input, setInput] = useState3("");
|
|
914
|
-
const [threadId, setThreadId] = useState3(void 0);
|
|
915
|
-
const [status, setStatus] = useState3("awaiting_message");
|
|
916
|
-
const [error, setError] = useState3(void 0);
|
|
917
|
-
const systemMessage = useMemo3(() => {
|
|
918
|
-
const systemMessageMaker = options.makeSystemMessage || defaultSystemMessage2;
|
|
919
|
-
const contextString = getContextString([], defaultCopilotContextCategories);
|
|
920
|
-
return {
|
|
921
|
-
id: "system",
|
|
922
|
-
content: systemMessageMaker(contextString),
|
|
923
|
-
role: "system"
|
|
924
|
-
};
|
|
925
|
-
}, [getContextString, options.makeSystemMessage]);
|
|
926
|
-
const handleInputChange = (e) => {
|
|
927
|
-
setInput(e.target.value);
|
|
928
|
-
};
|
|
929
|
-
const submitMessage = (e) => __async(this, null, function* () {
|
|
930
|
-
var _a, _b;
|
|
931
|
-
e.preventDefault();
|
|
932
|
-
if (input === "") {
|
|
933
|
-
return;
|
|
934
|
-
}
|
|
935
|
-
setStatus("in_progress");
|
|
936
|
-
setMessages((messages2) => [...messages2, { id: "", role: "user", content: input }]);
|
|
937
|
-
setInput("");
|
|
938
|
-
const apiUrl = copilotApiConfig.chatApiEndpointV2;
|
|
939
|
-
const functions = getChatCompletionFunctionDescriptions();
|
|
940
|
-
const result = yield fetch(apiUrl, {
|
|
941
|
-
method: "POST",
|
|
942
|
-
headers: __spreadValues(__spreadValues({
|
|
943
|
-
"Content-Type": "application/json"
|
|
944
|
-
}, copilotApiConfig.headers), options.headers),
|
|
945
|
-
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues({
|
|
946
|
-
// always use user-provided threadId when available:
|
|
947
|
-
threadId: (_b = (_a = options.threadId) != null ? _a : threadId) != null ? _b : null,
|
|
948
|
-
message: input
|
|
949
|
-
}, functions.length > 0 && { functions }), copilotApiConfig.body), options.body))
|
|
950
|
-
});
|
|
951
|
-
if (result.body == null) {
|
|
952
|
-
throw new Error("The response body is empty.");
|
|
953
|
-
}
|
|
954
|
-
yield processMessageStream(result.body.getReader(), (message) => {
|
|
955
|
-
try {
|
|
956
|
-
const { type, value } = parseStreamPart(message);
|
|
957
|
-
switch (type) {
|
|
958
|
-
case "assistant_message": {
|
|
959
|
-
setMessages((messages2) => [
|
|
960
|
-
...messages2,
|
|
961
|
-
{
|
|
962
|
-
id: value.id,
|
|
963
|
-
role: value.role,
|
|
964
|
-
content: value.content[0].text.value
|
|
965
|
-
}
|
|
966
|
-
]);
|
|
967
|
-
break;
|
|
968
|
-
}
|
|
969
|
-
case "assistant_control_data": {
|
|
970
|
-
setThreadId(value.threadId);
|
|
971
|
-
setMessages((messages2) => {
|
|
972
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
973
|
-
lastMessage.id = value.messageId;
|
|
974
|
-
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
975
|
-
});
|
|
976
|
-
break;
|
|
977
|
-
}
|
|
978
|
-
case "error": {
|
|
979
|
-
setError(value);
|
|
980
|
-
break;
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
} catch (error2) {
|
|
984
|
-
setError(error2);
|
|
985
|
-
}
|
|
986
|
-
});
|
|
987
|
-
setStatus("awaiting_message");
|
|
988
|
-
});
|
|
989
|
-
return {
|
|
990
|
-
messages,
|
|
991
|
-
input,
|
|
992
|
-
handleInputChange,
|
|
993
|
-
submitMessage,
|
|
994
|
-
status,
|
|
995
|
-
error
|
|
996
|
-
};
|
|
997
|
-
}
|
|
998
|
-
function defaultSystemMessage2(contextString) {
|
|
999
|
-
return `
|
|
1000
|
-
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
1001
|
-
|
|
1002
|
-
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.
|
|
1003
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
1004
|
-
|
|
1005
|
-
The user has provided you with the following context:
|
|
1006
|
-
\`\`\`
|
|
1007
|
-
${contextString}
|
|
1008
|
-
\`\`\`
|
|
1009
|
-
|
|
1010
|
-
They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
|
|
1011
|
-
|
|
1012
|
-
Please assist them as best you can.
|
|
1013
|
-
|
|
1014
|
-
You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
|
|
1015
|
-
|
|
1016
|
-
If you would like to call a function, call it without saying anything else.
|
|
1017
|
-
`;
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
// src/lib/copilot-task.ts
|
|
1021
|
-
var CopilotTask = class {
|
|
1022
|
-
constructor(config) {
|
|
1023
|
-
this.instructions = config.instructions;
|
|
1024
|
-
this.actions = config.actions || [];
|
|
1025
|
-
this.includeCopilotReadable = config.includeCopilotReadable || true;
|
|
1026
|
-
this.includeCopilotActionable = config.includeCopilotActionable || true;
|
|
1027
|
-
}
|
|
1028
|
-
run(context, data) {
|
|
1029
|
-
return __async(this, null, function* () {
|
|
1030
|
-
const entryPoints = this.includeCopilotActionable ? Object.assign({}, context.entryPoints) : {};
|
|
1031
|
-
for (const fn of this.actions) {
|
|
1032
|
-
entryPoints[fn.name] = fn;
|
|
1033
|
-
}
|
|
1034
|
-
let contextString = "";
|
|
1035
|
-
if (data) {
|
|
1036
|
-
contextString = (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
|
|
1037
|
-
}
|
|
1038
|
-
if (this.includeCopilotReadable) {
|
|
1039
|
-
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
1040
|
-
}
|
|
1041
|
-
const systemMessage = {
|
|
1042
|
-
id: "system",
|
|
1043
|
-
content: taskSystemMessage(contextString, this.instructions),
|
|
1044
|
-
role: "system"
|
|
1045
|
-
};
|
|
1046
|
-
const messages = [systemMessage];
|
|
1047
|
-
const response = yield fetchAndDecodeChatCompletion({
|
|
1048
|
-
copilotConfig: context.copilotApiConfig,
|
|
1049
|
-
messages,
|
|
1050
|
-
tools: context.getChatCompletionFunctionDescriptions(entryPoints),
|
|
1051
|
-
headers: context.copilotApiConfig.headers,
|
|
1052
|
-
body: context.copilotApiConfig.body
|
|
1053
|
-
});
|
|
1054
|
-
if (!response.events) {
|
|
1055
|
-
throw new Error("Failed to execute task");
|
|
1056
|
-
}
|
|
1057
|
-
const reader = response.events.getReader();
|
|
1058
|
-
let functionCalls = [];
|
|
1059
|
-
while (true) {
|
|
1060
|
-
const { done, value } = yield reader.read();
|
|
1061
|
-
if (done) {
|
|
1062
|
-
break;
|
|
1063
|
-
}
|
|
1064
|
-
if (value.type === "function") {
|
|
1065
|
-
functionCalls.push({
|
|
1066
|
-
name: value.name,
|
|
1067
|
-
arguments: JSON.stringify(value.arguments)
|
|
1068
|
-
});
|
|
1069
|
-
break;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
if (!functionCalls.length) {
|
|
1073
|
-
throw new Error("No function call occurred");
|
|
1074
|
-
}
|
|
1075
|
-
const functionCallHandler = context.getFunctionCallHandler(entryPoints);
|
|
1076
|
-
for (const functionCall of functionCalls) {
|
|
1077
|
-
yield functionCallHandler(messages, functionCall);
|
|
1078
|
-
}
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
};
|
|
1082
|
-
function taskSystemMessage(contextString, instructions) {
|
|
1083
|
-
return `
|
|
1084
|
-
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
1085
|
-
|
|
1086
|
-
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.
|
|
1087
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
1088
|
-
|
|
1089
|
-
The user has provided you with the following context:
|
|
1090
|
-
\`\`\`
|
|
1091
|
-
${contextString}
|
|
1092
|
-
\`\`\`
|
|
1093
|
-
|
|
1094
|
-
They have also provided you with functions you can call to initiate actions on their behalf.
|
|
1095
|
-
|
|
1096
|
-
Please assist them as best you can.
|
|
1097
|
-
|
|
1098
|
-
This is not a conversation, so please do not ask questions. Just call a function without saying anything else.
|
|
1099
|
-
|
|
1100
|
-
The user has given you the following task to complete:
|
|
1101
|
-
|
|
1102
|
-
\`\`\`
|
|
1103
|
-
${instructions}
|
|
1104
|
-
\`\`\`
|
|
1105
|
-
`;
|
|
1106
|
-
}
|
|
10
|
+
useCopilotChatV2
|
|
11
|
+
} from "./chunk-GUQTL3VG.mjs";
|
|
12
|
+
import "./chunk-FRAKUJWH.mjs";
|
|
13
|
+
import {
|
|
14
|
+
processMessageStream
|
|
15
|
+
} from "./chunk-MZ5UN3BY.mjs";
|
|
16
|
+
import "./chunk-VNRDQJXW.mjs";
|
|
17
|
+
import "./chunk-IOP6JX34.mjs";
|
|
18
|
+
import {
|
|
19
|
+
useMakeCopilotActionable
|
|
20
|
+
} from "./chunk-YMX4OOEL.mjs";
|
|
21
|
+
import {
|
|
22
|
+
useMakeCopilotDocumentReadable
|
|
23
|
+
} from "./chunk-R3D5GIQT.mjs";
|
|
24
|
+
import {
|
|
25
|
+
useMakeCopilotReadable
|
|
26
|
+
} from "./chunk-Q6YXCGNM.mjs";
|
|
27
|
+
import {
|
|
28
|
+
useCopilotAction
|
|
29
|
+
} from "./chunk-WHODFSGA.mjs";
|
|
30
|
+
import {
|
|
31
|
+
useCopilotChat
|
|
32
|
+
} from "./chunk-ICZAFR7J.mjs";
|
|
33
|
+
import "./chunk-JD7BAH7U.mjs";
|
|
34
|
+
import "./chunk-SPCZTZCY.mjs";
|
|
35
|
+
import {
|
|
36
|
+
CopilotKit,
|
|
37
|
+
defaultCopilotContextCategories
|
|
38
|
+
} from "./chunk-DFCYHW6B.mjs";
|
|
39
|
+
import "./chunk-YULKJPY3.mjs";
|
|
40
|
+
import "./chunk-VUY2K2DI.mjs";
|
|
41
|
+
import "./chunk-MESZST5T.mjs";
|
|
42
|
+
import {
|
|
43
|
+
fetchAndDecodeChatCompletion,
|
|
44
|
+
fetchAndDecodeChatCompletionAsText,
|
|
45
|
+
fetchChatCompletion
|
|
46
|
+
} from "./chunk-CGT3AVYT.mjs";
|
|
47
|
+
import {
|
|
48
|
+
CopilotContext,
|
|
49
|
+
useCopilotContext
|
|
50
|
+
} from "./chunk-XUCMY57N.mjs";
|
|
51
|
+
import "./chunk-MRXNTQOX.mjs";
|
|
1107
52
|
export {
|
|
1108
53
|
CopilotContext,
|
|
1109
54
|
CopilotKit,
|