@copilotkit/react-core 0.25.0 → 0.36.0-mme-pre.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 +184 -165
- package/CHANGELOG.md +22 -0
- package/dist/{chunk-72BJ3A3S.mjs → chunk-2C4UBKRR.mjs} +2 -2
- package/dist/{chunk-KTUNGBJF.mjs → chunk-2DKLSG5S.mjs} +2 -2
- package/dist/{chunk-4NNFSZOY.mjs → chunk-32T4S22Q.mjs} +3 -3
- package/dist/chunk-32T4S22Q.mjs.map +1 -0
- package/dist/{chunk-CGT3AVYT.mjs → chunk-7YTDTTX5.mjs} +6 -4
- package/dist/chunk-7YTDTTX5.mjs.map +1 -0
- package/dist/{chunk-ZIR7KCEA.mjs → chunk-B5CULQRZ.mjs} +2 -2
- package/dist/chunk-B5CULQRZ.mjs.map +1 -0
- package/dist/chunk-BTJD4QON.mjs +122 -0
- package/dist/chunk-BTJD4QON.mjs.map +1 -0
- package/dist/chunk-CYDWEPFL.mjs +1 -0
- package/dist/{chunk-6ARSX7BW.mjs → chunk-FHGMPFKO.mjs} +2 -2
- package/dist/{chunk-PC6FY3LD.mjs → chunk-GJGYNK46.mjs} +2 -2
- package/dist/{chunk-7LVVZQRG.mjs → chunk-JWANHQCM.mjs} +22 -6
- package/dist/chunk-JWANHQCM.mjs.map +1 -0
- package/dist/{chunk-M6WULIFO.mjs → chunk-L3LL6N7J.mjs} +5 -5
- package/dist/{chunk-6VF626RG.mjs → chunk-S2YA76YW.mjs} +5 -5
- package/dist/{chunk-MRXNTQOX.mjs → chunk-SKC7AJIV.mjs} +3 -1
- package/dist/chunk-SKC7AJIV.mjs.map +1 -0
- package/dist/{chunk-OWBDHSLH.mjs → chunk-T6UD6AJ2.mjs} +9 -4
- package/dist/chunk-T6UD6AJ2.mjs.map +1 -0
- package/dist/{chunk-O35YOIHC.mjs → chunk-TW5FS2QT.mjs} +8 -3
- package/dist/chunk-TW5FS2QT.mjs.map +1 -0
- package/dist/{chunk-VUY2K2DI.mjs → chunk-YWITCRBI.mjs} +2 -2
- package/dist/{chunk-MZ5UN3BY.mjs → chunk-ZFS5SQUT.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +18 -12
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -20
- package/dist/components/copilot-provider/copilotkit.js +24 -3
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +4 -4
- package/dist/components/copilot-provider/index.js +24 -3
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +4 -4
- package/dist/components/index.js +24 -3
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +4 -4
- package/dist/context/copilot-context.d.ts +14 -0
- package/dist/context/copilot-context.js +6 -1
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +2 -2
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.js +6 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +2 -2
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +10 -3
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +12 -12
- package/dist/hooks/use-chat.d.ts +1 -0
- package/dist/hooks/use-chat.js +4 -2
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-copilot-action.js +6 -1
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +3 -3
- package/dist/hooks/use-copilot-chat.d.ts +1 -0
- package/dist/hooks/use-copilot-chat.js +10 -3
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +7 -7
- package/dist/hooks/use-copilot-readable.js +6 -1
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +3 -3
- package/dist/hooks/use-flat-category-store.mjs +1 -1
- package/dist/hooks/use-make-copilot-actionable.js +6 -1
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +3 -3
- package/dist/hooks/use-make-copilot-document-readable.js +6 -1
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +3 -3
- package/dist/hooks/use-make-copilot-readable.js +6 -1
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +3 -3
- package/dist/hooks/use-tree.mjs +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +143 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -15
- package/dist/lib/copilot-task.d.ts +98 -1
- package/dist/lib/copilot-task.js +9 -2
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -6
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +9 -2
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -6
- package/dist/openai-assistants/hooks/index.js +6 -1
- package/dist/openai-assistants/hooks/index.js.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +6 -6
- package/dist/openai-assistants/hooks/use-assistants.mjs +2 -2
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +6 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +6 -6
- package/dist/openai-assistants/index.js +6 -1
- package/dist/openai-assistants/index.js.map +1 -1
- package/dist/openai-assistants/index.mjs +6 -6
- package/dist/openai-assistants/utils/index.mjs +2 -2
- package/dist/openai-assistants/utils/process-message-stream.mjs +2 -2
- package/dist/types/chat-suggestion-configuration.d.ts +22 -0
- package/dist/types/chat-suggestion-configuration.js +19 -0
- package/dist/types/chat-suggestion-configuration.js.map +1 -0
- package/dist/types/chat-suggestion-configuration.mjs +1 -0
- package/dist/types/chat-suggestion-configuration.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.d.ts +37 -0
- package/dist/utils/extract.js +245 -0
- package/dist/utils/extract.js.map +1 -0
- package/dist/utils/extract.mjs +15 -0
- package/dist/utils/extract.mjs.map +1 -0
- package/dist/utils/fetch-chat-completion.d.ts +8 -1
- package/dist/utils/fetch-chat-completion.js +4 -2
- package/dist/utils/fetch-chat-completion.js.map +1 -1
- package/dist/utils/fetch-chat-completion.mjs +2 -2
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.js +267 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +23 -0
- package/dist/utils/index.mjs.map +1 -0
- package/package.json +4 -4
- package/src/components/copilot-provider/copilotkit-props.tsx +20 -12
- package/src/components/copilot-provider/copilotkit.tsx +62 -21
- package/src/context/copilot-context.tsx +24 -1
- package/src/hooks/use-chat.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +94 -0
- package/src/index.tsx +1 -6
- package/src/lib/copilot-task.ts +97 -1
- package/src/types/chat-suggestion-configuration.ts +23 -0
- package/src/types/index.ts +1 -0
- package/src/utils/extract.ts +164 -0
- package/src/utils/fetch-chat-completion.ts +5 -1
- package/src/utils/index.ts +8 -0
- package/typedoc.json +4 -0
- package/dist/chunk-4NNFSZOY.mjs.map +0 -1
- package/dist/chunk-7LVVZQRG.mjs.map +0 -1
- package/dist/chunk-CGT3AVYT.mjs.map +0 -1
- package/dist/chunk-O35YOIHC.mjs.map +0 -1
- package/dist/chunk-OWBDHSLH.mjs.map +0 -1
- package/dist/chunk-ZIR7KCEA.mjs.map +0 -1
- /package/dist/{chunk-72BJ3A3S.mjs.map → chunk-2C4UBKRR.mjs.map} +0 -0
- /package/dist/{chunk-KTUNGBJF.mjs.map → chunk-2DKLSG5S.mjs.map} +0 -0
- /package/dist/{chunk-MRXNTQOX.mjs.map → chunk-CYDWEPFL.mjs.map} +0 -0
- /package/dist/{chunk-6ARSX7BW.mjs.map → chunk-FHGMPFKO.mjs.map} +0 -0
- /package/dist/{chunk-PC6FY3LD.mjs.map → chunk-GJGYNK46.mjs.map} +0 -0
- /package/dist/{chunk-M6WULIFO.mjs.map → chunk-L3LL6N7J.mjs.map} +0 -0
- /package/dist/{chunk-6VF626RG.mjs.map → chunk-S2YA76YW.mjs.map} +0 -0
- /package/dist/{chunk-VUY2K2DI.mjs.map → chunk-YWITCRBI.mjs.map} +0 -0
- /package/dist/{chunk-MZ5UN3BY.mjs.map → chunk-ZFS5SQUT.mjs.map} +0 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
35
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
36
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
37
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
38
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
39
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
40
|
+
mod
|
|
41
|
+
));
|
|
42
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
43
|
+
var __async = (__this, __arguments, generator) => {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
var fulfilled = (value) => {
|
|
46
|
+
try {
|
|
47
|
+
step(generator.next(value));
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var rejected = (value) => {
|
|
53
|
+
try {
|
|
54
|
+
step(generator.throw(value));
|
|
55
|
+
} catch (e) {
|
|
56
|
+
reject(e);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
60
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/utils/index.ts
|
|
65
|
+
var utils_exports = {};
|
|
66
|
+
__export(utils_exports, {
|
|
67
|
+
extract: () => extract,
|
|
68
|
+
fetchAndDecodeChatCompletion: () => fetchAndDecodeChatCompletion,
|
|
69
|
+
fetchAndDecodeChatCompletionAsText: () => fetchAndDecodeChatCompletionAsText,
|
|
70
|
+
fetchChatCompletion: () => fetchChatCompletion
|
|
71
|
+
});
|
|
72
|
+
module.exports = __toCommonJS(utils_exports);
|
|
73
|
+
|
|
74
|
+
// src/utils/fetch-chat-completion.ts
|
|
75
|
+
var import_shared = require("@copilotkit/shared");
|
|
76
|
+
function fetchChatCompletion(_0) {
|
|
77
|
+
return __async(this, arguments, function* ({
|
|
78
|
+
copilotConfig,
|
|
79
|
+
model,
|
|
80
|
+
messages,
|
|
81
|
+
tools,
|
|
82
|
+
temperature,
|
|
83
|
+
headers,
|
|
84
|
+
body,
|
|
85
|
+
signal,
|
|
86
|
+
toolChoice
|
|
87
|
+
}) {
|
|
88
|
+
temperature || (temperature = 0.5);
|
|
89
|
+
tools || (tools = []);
|
|
90
|
+
const cleanedMessages = messages.map((message) => {
|
|
91
|
+
const { content, role, name, function_call } = message;
|
|
92
|
+
return { content, role, name, function_call };
|
|
93
|
+
});
|
|
94
|
+
toolChoice || (toolChoice = "auto");
|
|
95
|
+
const response = yield fetch(copilotConfig.chatApiEndpoint, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers: __spreadValues(__spreadValues({
|
|
98
|
+
"Content-Type": "application/json"
|
|
99
|
+
}, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
|
|
100
|
+
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
101
|
+
model,
|
|
102
|
+
messages: cleanedMessages,
|
|
103
|
+
stream: true
|
|
104
|
+
}, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: toolChoice } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {}), copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {})),
|
|
105
|
+
signal
|
|
106
|
+
});
|
|
107
|
+
return response;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function excludeBackendOnlyProps(copilotConfig) {
|
|
111
|
+
var _a;
|
|
112
|
+
const backendOnlyProps = (_a = copilotConfig.backendOnlyProps) != null ? _a : {};
|
|
113
|
+
if (Object.keys(backendOnlyProps).length > 0) {
|
|
114
|
+
return {
|
|
115
|
+
[import_shared.EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps)
|
|
116
|
+
};
|
|
117
|
+
} else {
|
|
118
|
+
return {};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function fetchAndDecodeChatCompletion(params) {
|
|
122
|
+
return __async(this, null, function* () {
|
|
123
|
+
const response = yield fetchChatCompletion(params);
|
|
124
|
+
if (!response.ok || !response.body) {
|
|
125
|
+
response.events = null;
|
|
126
|
+
} else {
|
|
127
|
+
const events = yield (0, import_shared.decodeChatCompletion)((0, import_shared.parseChatCompletion)(response.body));
|
|
128
|
+
response.events = events;
|
|
129
|
+
}
|
|
130
|
+
return response;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function fetchAndDecodeChatCompletionAsText(params) {
|
|
134
|
+
return __async(this, null, function* () {
|
|
135
|
+
const response = yield fetchChatCompletion(params);
|
|
136
|
+
if (!response.ok || !response.body) {
|
|
137
|
+
response.events = null;
|
|
138
|
+
} else {
|
|
139
|
+
const events = yield (0, import_shared.decodeChatCompletionAsText)(
|
|
140
|
+
(0, import_shared.decodeChatCompletion)((0, import_shared.parseChatCompletion)(response.body))
|
|
141
|
+
);
|
|
142
|
+
response.events = events;
|
|
143
|
+
}
|
|
144
|
+
return response;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/utils/extract.ts
|
|
149
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
150
|
+
|
|
151
|
+
// src/components/copilot-provider/copilotkit.tsx
|
|
152
|
+
var import_react = require("react");
|
|
153
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
154
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
155
|
+
var defaultCopilotContextCategories = ["global"];
|
|
156
|
+
|
|
157
|
+
// src/utils/extract.ts
|
|
158
|
+
var import_untruncate_json = __toESM(require("untruncate-json"));
|
|
159
|
+
function extract(_0) {
|
|
160
|
+
return __async(this, arguments, function* ({
|
|
161
|
+
context,
|
|
162
|
+
instructions,
|
|
163
|
+
parameters,
|
|
164
|
+
include,
|
|
165
|
+
data,
|
|
166
|
+
abortSignal,
|
|
167
|
+
stream
|
|
168
|
+
}) {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
const { messages } = context;
|
|
171
|
+
const action = {
|
|
172
|
+
name: "extract",
|
|
173
|
+
parameters,
|
|
174
|
+
handler: (args) => {
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const includeReadable = (_a = include == null ? void 0 : include.readable) != null ? _a : false;
|
|
178
|
+
const includeMessages = (_b = include == null ? void 0 : include.messages) != null ? _b : false;
|
|
179
|
+
let contextString = "";
|
|
180
|
+
if (data) {
|
|
181
|
+
contextString = (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
|
|
182
|
+
}
|
|
183
|
+
if (includeReadable) {
|
|
184
|
+
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
185
|
+
}
|
|
186
|
+
const systemMessage = {
|
|
187
|
+
id: "system",
|
|
188
|
+
content: makeSystemMessage(contextString, instructions),
|
|
189
|
+
role: "system"
|
|
190
|
+
};
|
|
191
|
+
const headers = __spreadValues(__spreadValues({}, context.copilotApiConfig.headers || {}), context.copilotApiConfig.publicApiKey ? { [import_shared3.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: context.copilotApiConfig.publicApiKey } : {});
|
|
192
|
+
const response = yield fetchAndDecodeChatCompletion({
|
|
193
|
+
copilotConfig: context.copilotApiConfig,
|
|
194
|
+
messages: includeMessages ? [systemMessage, ...messages] : [systemMessage],
|
|
195
|
+
tools: context.getChatCompletionFunctionDescriptions({ extract: action }),
|
|
196
|
+
headers,
|
|
197
|
+
body: context.copilotApiConfig.body,
|
|
198
|
+
toolChoice: { type: "function", function: { name: "extract" } },
|
|
199
|
+
signal: abortSignal
|
|
200
|
+
});
|
|
201
|
+
if (!response.events) {
|
|
202
|
+
throw new Error("extract() failed: Could not fetch chat completion");
|
|
203
|
+
}
|
|
204
|
+
const reader = response.events.getReader();
|
|
205
|
+
let isInitial = true;
|
|
206
|
+
while (true) {
|
|
207
|
+
const { done, value } = yield reader.read();
|
|
208
|
+
if (done) {
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
if (value.type === "partial") {
|
|
212
|
+
try {
|
|
213
|
+
let partialArguments = JSON.parse((0, import_untruncate_json.default)(value.arguments));
|
|
214
|
+
stream == null ? void 0 : stream({
|
|
215
|
+
status: isInitial ? "initial" : "inProgress",
|
|
216
|
+
args: partialArguments
|
|
217
|
+
});
|
|
218
|
+
isInitial = false;
|
|
219
|
+
} catch (e) {
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (value.type === "function") {
|
|
223
|
+
stream == null ? void 0 : stream({
|
|
224
|
+
status: "complete",
|
|
225
|
+
args: value.arguments
|
|
226
|
+
});
|
|
227
|
+
return value.arguments;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
throw new Error("extract() failed: No function call occurred");
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
function makeSystemMessage(contextString, instructions) {
|
|
234
|
+
return `
|
|
235
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
236
|
+
|
|
237
|
+
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.
|
|
238
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
239
|
+
|
|
240
|
+
The user has provided you with the following context:
|
|
241
|
+
\`\`\`
|
|
242
|
+
${contextString}
|
|
243
|
+
\`\`\`
|
|
244
|
+
|
|
245
|
+
They have also provided you with a function you MUST call to initiate actions on their behalf.
|
|
246
|
+
|
|
247
|
+
Please assist them as best you can.
|
|
248
|
+
|
|
249
|
+
This is not a conversation, so please do not ask questions. Just call the function without saying anything else.
|
|
250
|
+
|
|
251
|
+
The user has given you the following task to complete:
|
|
252
|
+
|
|
253
|
+
\`\`\`
|
|
254
|
+
${instructions}
|
|
255
|
+
\`\`\`
|
|
256
|
+
|
|
257
|
+
Any additional messages provided are for providing context only and should not be used to ask questions or engage in conversation.
|
|
258
|
+
`;
|
|
259
|
+
}
|
|
260
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
261
|
+
0 && (module.exports = {
|
|
262
|
+
extract,
|
|
263
|
+
fetchAndDecodeChatCompletion,
|
|
264
|
+
fetchAndDecodeChatCompletionAsText,
|
|
265
|
+
fetchChatCompletion
|
|
266
|
+
});
|
|
267
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/index.ts","../../src/utils/fetch-chat-completion.ts","../../src/utils/extract.ts","../../src/components/copilot-provider/copilotkit.tsx"],"sourcesContent":["export {\n type FetchChatCompletionParams,\n fetchChatCompletion,\n fetchAndDecodeChatCompletion,\n fetchAndDecodeChatCompletionAsText,\n} from \"./fetch-chat-completion\";\n\nexport { extract } from \"./extract\";\n","import {\n Message,\n ToolDefinition,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n EXCLUDE_FROM_FORWARD_PROPS_KEYS,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n tools?: ToolDefinition[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n toolChoice?: string | { type: \"function\"; function: { name: string } };\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n tools,\n temperature,\n headers,\n body,\n signal,\n toolChoice,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n tools ||= [];\n\n // clean up any extra properties from messages\n const cleanedMessages = messages.map((message) => {\n const { content, role, name, function_call } = message;\n return { content, role, name, function_call };\n });\n\n toolChoice ||= \"auto\";\n\n const response = await fetch(copilotConfig.chatApiEndpoint, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...copilotConfig.headers,\n ...(headers ? { ...headers } : {}),\n },\n body: JSON.stringify({\n model,\n messages: cleanedMessages,\n stream: true,\n ...(tools.length ? { tools } : {}),\n ...(temperature ? { temperature } : {}),\n ...(tools.length != 0 ? { tool_choice: toolChoice } : {}),\n ...copilotConfig.body,\n ...copilotConfig.backendOnlyProps,\n ...excludeBackendOnlyProps(copilotConfig),\n ...(body ? { ...body } : {}),\n ...(copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nfunction excludeBackendOnlyProps(copilotConfig: any) {\n const backendOnlyProps = copilotConfig.backendOnlyProps ?? {};\n if (Object.keys(backendOnlyProps).length > 0) {\n return {\n [EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps),\n };\n } else {\n return {};\n }\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n (response as any).events = null;\n } else {\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n (response as any).events = events;\n }\n return response as any;\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n (response as any).events = null;\n } else {\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n (response as any).events = events;\n }\n\n return response as any;\n}\n","import {\n Action,\n COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,\n MappedParameterTypes,\n Message,\n Parameter,\n} from \"@copilotkit/shared\";\nimport { CopilotContextParams } from \"../context\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { fetchAndDecodeChatCompletion } from \"./fetch-chat-completion\";\nimport untruncateJson from \"untruncate-json\";\n\ninterface InitialState<T extends Parameter[] | [] = []> {\n status: \"initial\";\n args: Partial<MappedParameterTypes<T>>;\n}\n\ninterface InProgressState<T extends Parameter[] | [] = []> {\n status: \"inProgress\";\n args: Partial<MappedParameterTypes<T>>;\n}\n\ninterface CompleteState<T extends Parameter[] | [] = []> {\n status: \"complete\";\n args: MappedParameterTypes<T>;\n}\n\ntype StreamHandlerArgs<T extends Parameter[] | [] = []> =\n | InitialState<T>\n | InProgressState<T>\n | CompleteState<T>;\n\ninterface ExtractOptions<T extends Parameter[]> {\n context: CopilotContextParams;\n instructions: string;\n parameters: T;\n include?: IncludeOptions;\n data?: any;\n abortSignal?: AbortSignal;\n stream?: (args: StreamHandlerArgs<T>) => void;\n}\n\ninterface IncludeOptions {\n readable?: boolean;\n messages?: boolean;\n}\n\nexport async function extract<const T extends Parameter[]>({\n context,\n instructions,\n parameters,\n include,\n data,\n abortSignal,\n stream,\n}: ExtractOptions<T>): Promise<MappedParameterTypes<T>> {\n const { messages } = context;\n\n const action: Action<any> = {\n name: \"extract\",\n parameters,\n handler: (args: any) => {},\n };\n\n const includeReadable = include?.readable ?? false;\n const includeMessages = include?.messages ?? false;\n\n let contextString = \"\";\n\n if (data) {\n contextString = (typeof data === \"string\" ? data : JSON.stringify(data)) + \"\\n\\n\";\n }\n\n if (includeReadable) {\n contextString += context.getContextString([], defaultCopilotContextCategories);\n }\n\n const systemMessage: Message = {\n id: \"system\",\n content: makeSystemMessage(contextString, instructions),\n role: \"system\",\n };\n\n const headers = {\n ...(context.copilotApiConfig.headers || {}),\n ...(context.copilotApiConfig.publicApiKey\n ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: context.copilotApiConfig.publicApiKey }\n : {}),\n };\n\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: context.copilotApiConfig,\n messages: includeMessages ? [systemMessage, ...messages] : [systemMessage],\n tools: context.getChatCompletionFunctionDescriptions({ extract: action }),\n headers,\n body: context.copilotApiConfig.body,\n toolChoice: { type: \"function\", function: { name: \"extract\" } },\n signal: abortSignal,\n });\n\n if (!response.events) {\n throw new Error(\"extract() failed: Could not fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n let isInitial = true;\n\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n break;\n }\n\n if (value.type === \"partial\") {\n try {\n let partialArguments = JSON.parse(untruncateJson(value.arguments));\n stream?.({\n status: isInitial ? \"initial\" : \"inProgress\",\n args: partialArguments as Partial<MappedParameterTypes<T>>,\n });\n isInitial = false;\n } catch (e) {}\n }\n\n if (value.type === \"function\") {\n stream?.({\n status: \"complete\",\n args: value.arguments as MappedParameterTypes<T>,\n });\n return value.arguments as MappedParameterTypes<T>;\n }\n }\n\n throw new Error(\"extract() failed: No function call occurred\");\n}\n\nfunction makeSystemMessage(contextString: string, instructions: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp 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.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with a function you MUST call to initiate actions on their behalf.\n\nPlease assist them as best you can.\n\nThis is not a conversation, so please do not ask questions. Just call the function without saying anything else.\n\nThe user has given you the following task to complete:\n\n\\`\\`\\`\n${instructions}\n\\`\\`\\`\n\nAny additional messages provided are for providing context only and should not be used to ask questions or engage in conversation.\n`;\n}\n","/**\n * Provides the Copilot context to its children.\n * \n * <img\n * referrerPolicy=\"no-referrer-when-downgrade\"\n * src=\"https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be\"\n * />\n * \n * This component provides the Copilot context to its children.\n * It can be configured either with a chat API endpoint or a `CopilotApiConfig`.\n * \n * <Note>\n * The backend can use OpenAI, or you can bring your own LLM. For examples of the\n * backend api implementation, see `examples/next-openai` or the [runtime\n * docs](https://docs.copilotkit.ai/getting-started/quickstart-runtime).\n * </Note>\n * \n * <RequestExample>\n * ```jsx CopilotKit Example\n * import { CopilotKit } from \"@copilotkit/react-core\";\n * \n * <CopilotKit \n * runtimeUrl=\"https://your.copilotkit.api\">\n * <YourApp/>\n * </CopilotKit>\n * ```\n * </RequestExample>\n * \n * ## Example usage\n * \n * ```jsx\n * <CopilotKit publicApiKey=\"the api key or self host (see below)\">\n * <App />\n * </CopilotKit>\n```\n */\nimport { Ref, useCallback, useRef, useState } from \"react\";\nimport {\n CopilotContext,\n CopilotApiConfig,\n InChatRenderFunction,\n} from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { CopilotChatSuggestionConfiguration, DocumentPointer } from \"../../types\";\n\nimport {\n COPILOT_CLOUD_CHAT_URL,\n CopilotCloudConfig,\n FunctionCallHandler,\n Message,\n actionToChatCompletionFunction,\n} from \"@copilotkit/shared\";\n\nimport { FrontendAction } from \"../../types/frontend-action\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { CopilotKitProps } from \"./copilotkit-props\";\nimport { ToolDefinition } from \"@copilotkit/shared\";\n\nexport function CopilotKit({ children, ...props }: CopilotKitProps) {\n // Compute all the functions and properties that we need to pass\n // to the CopilotContext.\n\n if (!props.runtimeUrl && !props.url && !props.publicApiKey) {\n throw new Error(\"Please provide either a url or a publicApiKey to the CopilotKit component.\");\n }\n\n const chatApiEndpoint = props.runtimeUrl || props.url || COPILOT_CLOUD_CHAT_URL;\n\n const [entryPoints, setEntryPoints] = useState<Record<string, FrontendAction<any>>>({});\n const chatComponentsCache = useRef<Record<string, InChatRenderFunction | string>>({});\n const { addElement, removeElement, printTree } = useTree();\n const [messages, setMessages] = useState<Message[]>([]);\n\n const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\n\n const setEntryPoint = useCallback((id: string, entryPoint: FrontendAction<any>) => {\n setEntryPoints((prevPoints) => {\n return {\n ...prevPoints,\n [id]: entryPoint,\n };\n });\n }, []);\n\n const removeEntryPoint = useCallback((id: string) => {\n setEntryPoints((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(\n (documents: DocumentPointer[], categories: string[]) => {\n const documentsString = documents\n .map((document) => {\n return `${document.name} (${document.sourceApplication}):\\n${document.getContents()}`;\n })\n .join(\"\\n\\n\");\n\n const nonDocumentStrings = printTree(categories);\n\n return `${documentsString}\\n\\n${nonDocumentStrings}`;\n },\n [printTree],\n );\n\n const addContext = useCallback(\n (\n context: string,\n parentId?: string,\n categories: string[] = defaultCopilotContextCategories,\n ) => {\n return addElement(context, categories, parentId);\n },\n [addElement],\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement],\n );\n\n const getChatCompletionFunctionDescriptions = useCallback(\n (customEntryPoints?: Record<string, FrontendAction<any>>) => {\n return entryPointsToChatCompletionFunctions(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\n\n const getFunctionCallHandler = useCallback(\n (customEntryPoints?: Record<string, FrontendAction<any>>) => {\n return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\n\n const getDocumentsContext = useCallback(\n (categories: string[]) => {\n return allDocuments(categories);\n },\n [allDocuments],\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = defaultCopilotContextCategories) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument],\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument],\n );\n\n if (!props.publicApiKey) {\n if (props.cloudRestrictToTopic) {\n throw new Error(\n \"To use the cloudRestrictToTopic feature, please sign up at https://copilotkit.ai and provide a publicApiKey.\",\n );\n }\n }\n\n let cloud: CopilotCloudConfig | undefined = undefined;\n if (props.publicApiKey) {\n cloud = {\n guardrails: {\n input: {\n restrictToTopic: {\n enabled: props.cloudRestrictToTopic ? true : false,\n validTopics: props.cloudRestrictToTopic?.validTopics || [],\n invalidTopics: props.cloudRestrictToTopic?.invalidTopics || [],\n },\n },\n },\n };\n }\n\n // get the appropriate CopilotApiConfig from the props\n const copilotApiConfig: CopilotApiConfig = {\n publicApiKey: props.publicApiKey,\n ...(cloud ? { cloud } : {}),\n chatApiEndpoint: chatApiEndpoint,\n chatApiEndpointV2: `${props.url}/v2`,\n headers: props.headers || {},\n body: {\n ...props.body,\n ...props.backendOnlyProps,\n },\n transcribeAudioUrl: props.transcribeAudioUrl,\n textToSpeechUrl: props.textToSpeechUrl,\n };\n\n const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = useState<{\n [key: string]: CopilotChatSuggestionConfiguration;\n }>({});\n\n const addChatSuggestionConfiguration = (\n id: string,\n suggestion: CopilotChatSuggestionConfiguration,\n ) => {\n setChatSuggestionConfiguration((prev) => ({ ...prev, [id]: suggestion }));\n };\n\n const removeChatSuggestionConfiguration = (id: string) => {\n setChatSuggestionConfiguration((prev) => {\n const { [id]: _, ...rest } = prev;\n return rest;\n });\n };\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\n chatComponentsCache,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n setEntryPoint,\n removeEntryPoint,\n getContextString,\n addContext,\n removeContext,\n getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: copilotApiConfig,\n messages,\n setMessages,\n chatSuggestionConfiguration,\n addChatSuggestionConfiguration,\n removeChatSuggestionConfiguration,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nexport const defaultCopilotContextCategories = [\"global\"];\n\nfunction entryPointsToChatCompletionFunctions(actions: FrontendAction<any>[]): ToolDefinition[] {\n return actions.map(actionToChatCompletionFunction);\n}\n\nfunction entryPointsToFunctionCallHandler(actions: FrontendAction<any>[]): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let actionsByFunctionName: Record<string, FrontendAction<any>> = {};\n for (let action of actions) {\n actionsByFunctionName[action.name] = action;\n }\n\n const action = actionsByFunctionName[functionCall.name || \"\"];\n if (action) {\n let functionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n functionCallArguments = JSON.parse(functionCall.arguments);\n }\n return await action.handler(functionCallArguments);\n }\n };\n}\n"],"mappings":"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","names":["import_shared","import_shared","untruncateJson"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "../chunk-CYDWEPFL.mjs";
|
|
2
|
+
import {
|
|
3
|
+
extract
|
|
4
|
+
} from "../chunk-BTJD4QON.mjs";
|
|
5
|
+
import "../chunk-JD7BAH7U.mjs";
|
|
6
|
+
import "../chunk-SPCZTZCY.mjs";
|
|
7
|
+
import "../chunk-JWANHQCM.mjs";
|
|
8
|
+
import "../chunk-YULKJPY3.mjs";
|
|
9
|
+
import "../chunk-YWITCRBI.mjs";
|
|
10
|
+
import {
|
|
11
|
+
fetchAndDecodeChatCompletion,
|
|
12
|
+
fetchAndDecodeChatCompletionAsText,
|
|
13
|
+
fetchChatCompletion
|
|
14
|
+
} from "../chunk-7YTDTTX5.mjs";
|
|
15
|
+
import "../chunk-TW5FS2QT.mjs";
|
|
16
|
+
import "../chunk-SKC7AJIV.mjs";
|
|
17
|
+
export {
|
|
18
|
+
extract,
|
|
19
|
+
fetchAndDecodeChatCompletion,
|
|
20
|
+
fetchAndDecodeChatCompletionAsText,
|
|
21
|
+
fetchChatCompletion
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.36.0-mme-pre.1",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"ts-jest": "^29.1.1",
|
|
30
30
|
"tsup": "^6.7.0",
|
|
31
31
|
"typescript": "^5.2.3",
|
|
32
|
-
"eslint-config-custom": "0.
|
|
33
|
-
"tsconfig": "0.
|
|
32
|
+
"eslint-config-custom": "0.11.0-mme-pre.1",
|
|
33
|
+
"tsconfig": "0.15.0-mme-pre.1"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"nanoid": "^4.0.2",
|
|
37
37
|
"untruncate-json": "^0.0.1",
|
|
38
|
-
"@copilotkit/shared": "0.
|
|
38
|
+
"@copilotkit/shared": "0.36.0-mme-pre.1"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsup --clean",
|
|
@@ -6,12 +6,12 @@ import { ReactNode } from "react";
|
|
|
6
6
|
|
|
7
7
|
export interface CopilotKitProps {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Your Copilot Cloud API key.
|
|
10
10
|
*/
|
|
11
11
|
publicApiKey?: string;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* Cloud feature: Restrict to a specific topic.
|
|
14
|
+
* Cloud feature: Restrict input to a specific topic.
|
|
15
15
|
*/
|
|
16
16
|
cloudRestrictToTopic?: {
|
|
17
17
|
validTopics?: string[];
|
|
@@ -23,16 +23,26 @@ export interface CopilotKitProps {
|
|
|
23
23
|
*/
|
|
24
24
|
runtimeUrl?: string;
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* The endpoint for the Copilot transcribe audio service.
|
|
28
|
+
*/
|
|
29
|
+
transcribeAudioUrl?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The endpoint for the Copilot text to speech service.
|
|
33
|
+
*/
|
|
34
|
+
textToSpeechUrl?: string;
|
|
35
|
+
|
|
26
36
|
/**
|
|
27
37
|
* @deprecated use runtimeUrl instead
|
|
28
38
|
*/
|
|
29
39
|
url?: string;
|
|
30
40
|
|
|
31
41
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* ```
|
|
42
|
+
* Additional headers to be sent with the request.
|
|
43
|
+
*
|
|
44
|
+
* For example:
|
|
45
|
+
* ```js
|
|
36
46
|
* {
|
|
37
47
|
* 'Authorization': 'Bearer your_token_here'
|
|
38
48
|
* }
|
|
@@ -42,9 +52,8 @@ export interface CopilotKitProps {
|
|
|
42
52
|
|
|
43
53
|
/**
|
|
44
54
|
* Additional body params to be sent with the request
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* ```
|
|
55
|
+
* For example:
|
|
56
|
+
* ```js
|
|
48
57
|
* {
|
|
49
58
|
* 'message': 'Hello, world!'
|
|
50
59
|
* }
|
|
@@ -59,9 +68,8 @@ export interface CopilotKitProps {
|
|
|
59
68
|
|
|
60
69
|
/**
|
|
61
70
|
* Backend only props that will be combined to body params to be sent with the request
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* ```
|
|
71
|
+
* For example:
|
|
72
|
+
* ```js
|
|
65
73
|
* {
|
|
66
74
|
* 'user_id': 'users_id',
|
|
67
75
|
* }
|
|
@@ -1,3 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides the Copilot context to its children.
|
|
3
|
+
*
|
|
4
|
+
* <img
|
|
5
|
+
* referrerPolicy="no-referrer-when-downgrade"
|
|
6
|
+
* src="https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be"
|
|
7
|
+
* />
|
|
8
|
+
*
|
|
9
|
+
* This component provides the Copilot context to its children.
|
|
10
|
+
* It can be configured either with a chat API endpoint or a `CopilotApiConfig`.
|
|
11
|
+
*
|
|
12
|
+
* <Note>
|
|
13
|
+
* The backend can use OpenAI, or you can bring your own LLM. For examples of the
|
|
14
|
+
* backend api implementation, see `examples/next-openai` or the [runtime
|
|
15
|
+
* docs](https://docs.copilotkit.ai/getting-started/quickstart-runtime).
|
|
16
|
+
* </Note>
|
|
17
|
+
*
|
|
18
|
+
* <RequestExample>
|
|
19
|
+
* ```jsx CopilotKit Example
|
|
20
|
+
* import { CopilotKit } from "@copilotkit/react-core";
|
|
21
|
+
*
|
|
22
|
+
* <CopilotKit
|
|
23
|
+
* runtimeUrl="https://your.copilotkit.api">
|
|
24
|
+
* <YourApp/>
|
|
25
|
+
* </CopilotKit>
|
|
26
|
+
* ```
|
|
27
|
+
* </RequestExample>
|
|
28
|
+
*
|
|
29
|
+
* ## Example usage
|
|
30
|
+
*
|
|
31
|
+
* ```jsx
|
|
32
|
+
* <CopilotKit publicApiKey="the api key or self host (see below)">
|
|
33
|
+
* <App />
|
|
34
|
+
* </CopilotKit>
|
|
35
|
+
```
|
|
36
|
+
*/
|
|
1
37
|
import { Ref, useCallback, useRef, useState } from "react";
|
|
2
38
|
import {
|
|
3
39
|
CopilotContext,
|
|
@@ -5,7 +41,8 @@ import {
|
|
|
5
41
|
InChatRenderFunction,
|
|
6
42
|
} from "../../context/copilot-context";
|
|
7
43
|
import useTree from "../../hooks/use-tree";
|
|
8
|
-
import { DocumentPointer } from "../../types";
|
|
44
|
+
import { CopilotChatSuggestionConfiguration, DocumentPointer } from "../../types";
|
|
45
|
+
|
|
9
46
|
import {
|
|
10
47
|
COPILOT_CLOUD_CHAT_URL,
|
|
11
48
|
CopilotCloudConfig,
|
|
@@ -13,31 +50,12 @@ import {
|
|
|
13
50
|
Message,
|
|
14
51
|
actionToChatCompletionFunction,
|
|
15
52
|
} from "@copilotkit/shared";
|
|
53
|
+
|
|
16
54
|
import { FrontendAction } from "../../types/frontend-action";
|
|
17
55
|
import useFlatCategoryStore from "../../hooks/use-flat-category-store";
|
|
18
56
|
import { CopilotKitProps } from "./copilotkit-props";
|
|
19
57
|
import { ToolDefinition } from "@copilotkit/shared";
|
|
20
58
|
|
|
21
|
-
/**
|
|
22
|
-
* The CopilotKit component.
|
|
23
|
-
* This component provides the Copilot context to its children.
|
|
24
|
-
* It can be configured either with a chat API endpoint or a CopilotApiConfig.
|
|
25
|
-
*
|
|
26
|
-
* NOTE: The backend can use OpenAI, or you can bring your own LLM.
|
|
27
|
-
* For examples of the backend api implementation, see `examples/next-openai` usage (under `src/api/copilotkit`),
|
|
28
|
-
* or read the documentation at https://docs.copilotkit.ai
|
|
29
|
-
* In particular, Getting-Started > Quickstart-Runtime: https://docs.copilotkit.ai/getting-started/quickstart-runtime
|
|
30
|
-
*
|
|
31
|
-
* Example usage:
|
|
32
|
-
* ```
|
|
33
|
-
* <CopilotKit url="https://your.copilotkit.api">
|
|
34
|
-
* <App />
|
|
35
|
-
* </CopilotKit>
|
|
36
|
-
* ```
|
|
37
|
-
*
|
|
38
|
-
* @param props - The props for the component.
|
|
39
|
-
* @returns The CopilotKit component.
|
|
40
|
-
*/
|
|
41
59
|
export function CopilotKit({ children, ...props }: CopilotKitProps) {
|
|
42
60
|
// Compute all the functions and properties that we need to pass
|
|
43
61
|
// to the CopilotContext.
|
|
@@ -178,6 +196,26 @@ export function CopilotKit({ children, ...props }: CopilotKitProps) {
|
|
|
178
196
|
...props.body,
|
|
179
197
|
...props.backendOnlyProps,
|
|
180
198
|
},
|
|
199
|
+
transcribeAudioUrl: props.transcribeAudioUrl,
|
|
200
|
+
textToSpeechUrl: props.textToSpeechUrl,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = useState<{
|
|
204
|
+
[key: string]: CopilotChatSuggestionConfiguration;
|
|
205
|
+
}>({});
|
|
206
|
+
|
|
207
|
+
const addChatSuggestionConfiguration = (
|
|
208
|
+
id: string,
|
|
209
|
+
suggestion: CopilotChatSuggestionConfiguration,
|
|
210
|
+
) => {
|
|
211
|
+
setChatSuggestionConfiguration((prev) => ({ ...prev, [id]: suggestion }));
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const removeChatSuggestionConfiguration = (id: string) => {
|
|
215
|
+
setChatSuggestionConfiguration((prev) => {
|
|
216
|
+
const { [id]: _, ...rest } = prev;
|
|
217
|
+
return rest;
|
|
218
|
+
});
|
|
181
219
|
};
|
|
182
220
|
|
|
183
221
|
return (
|
|
@@ -198,6 +236,9 @@ export function CopilotKit({ children, ...props }: CopilotKitProps) {
|
|
|
198
236
|
copilotApiConfig: copilotApiConfig,
|
|
199
237
|
messages,
|
|
200
238
|
setMessages,
|
|
239
|
+
chatSuggestionConfiguration,
|
|
240
|
+
addChatSuggestionConfiguration,
|
|
241
|
+
removeChatSuggestionConfiguration,
|
|
201
242
|
}}
|
|
202
243
|
>
|
|
203
244
|
{children}
|
|
@@ -5,9 +5,10 @@ import {
|
|
|
5
5
|
ToolDefinition,
|
|
6
6
|
} from "@copilotkit/shared";
|
|
7
7
|
import { ActionRenderProps, FrontendAction } from "../types/frontend-action";
|
|
8
|
-
import React
|
|
8
|
+
import React from "react";
|
|
9
9
|
import { TreeNodeId } from "../hooks/use-tree";
|
|
10
10
|
import { DocumentPointer } from "../types";
|
|
11
|
+
import { CopilotChatSuggestionConfiguration } from "../types/chat-suggestion-configuration";
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Interface for the configuration of the Copilot API.
|
|
@@ -28,6 +29,16 @@ export interface CopilotApiConfig {
|
|
|
28
29
|
*/
|
|
29
30
|
chatApiEndpoint: string;
|
|
30
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The endpoint for the Copilot transcribe audio service.
|
|
34
|
+
*/
|
|
35
|
+
transcribeAudioUrl?: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The endpoint for the Copilot text to speech service.
|
|
39
|
+
*/
|
|
40
|
+
textToSpeechUrl?: string;
|
|
41
|
+
|
|
31
42
|
/**
|
|
32
43
|
* The endpoint for the chat API v2.
|
|
33
44
|
*/
|
|
@@ -98,6 +109,14 @@ export interface CopilotContextParams {
|
|
|
98
109
|
removeDocumentContext: (documentId: string) => void;
|
|
99
110
|
getDocumentsContext: (categories: string[]) => DocumentPointer[];
|
|
100
111
|
|
|
112
|
+
// suggestions configuration
|
|
113
|
+
chatSuggestionConfiguration: { [key: string]: CopilotChatSuggestionConfiguration };
|
|
114
|
+
addChatSuggestionConfiguration: (
|
|
115
|
+
id: string,
|
|
116
|
+
suggestion: CopilotChatSuggestionConfiguration,
|
|
117
|
+
) => void;
|
|
118
|
+
removeChatSuggestionConfiguration: (id: string) => void;
|
|
119
|
+
|
|
101
120
|
// api endpoints
|
|
102
121
|
copilotApiConfig: CopilotApiConfig;
|
|
103
122
|
}
|
|
@@ -134,6 +153,10 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
134
153
|
return {};
|
|
135
154
|
}
|
|
136
155
|
})(),
|
|
156
|
+
|
|
157
|
+
chatSuggestionConfiguration: {},
|
|
158
|
+
addChatSuggestionConfiguration: () => {},
|
|
159
|
+
removeChatSuggestionConfiguration: () => {},
|
|
137
160
|
};
|
|
138
161
|
|
|
139
162
|
export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
|
package/src/hooks/use-chat.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { useRef, useState } from "react";
|
|
1
|
+
import { useRef, useState, useContext, useEffect } from "react";
|
|
2
|
+
import { CopilotContext } from "../context/copilot-context";
|
|
2
3
|
import {
|
|
3
4
|
Message,
|
|
4
5
|
ToolDefinition,
|
|
@@ -123,7 +124,6 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
|
|
|
123
124
|
abortControllerRef.current = abortController;
|
|
124
125
|
|
|
125
126
|
setMessages([...messages, ...newMessages]);
|
|
126
|
-
|
|
127
127
|
// add threadId and runId to the body if it exists
|
|
128
128
|
const copilotConfigBody = options.copilotConfig.body || {};
|
|
129
129
|
if (threadIdRef.current) {
|