@copilotkit/react-core 0.17.0-alpha.5 → 0.17.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +80 -80
- package/CHANGELOG.md +20 -0
- package/dist/chunk-5UGLWBZJ.mjs +3 -0
- package/dist/chunk-5UGLWBZJ.mjs.map +1 -0
- package/dist/chunk-6A4PCNMZ.mjs +48 -0
- package/dist/chunk-6A4PCNMZ.mjs.map +1 -0
- package/dist/chunk-6SOQYBNX.mjs +44 -0
- package/dist/chunk-6SOQYBNX.mjs.map +1 -0
- package/dist/chunk-7GFKOIO7.mjs +3 -0
- package/dist/chunk-7GFKOIO7.mjs.map +1 -0
- package/dist/chunk-7P4OYNP3.mjs +19 -0
- package/dist/chunk-7P4OYNP3.mjs.map +1 -0
- package/dist/chunk-BABVSMJR.mjs +3 -0
- package/dist/chunk-BABVSMJR.mjs.map +1 -0
- package/dist/chunk-EFZPSZWO.mjs +3 -0
- package/dist/chunk-EFZPSZWO.mjs.map +1 -0
- package/dist/chunk-F2JIAPZQ.mjs +13 -0
- package/dist/chunk-F2JIAPZQ.mjs.map +1 -0
- package/dist/chunk-FP4EXCGS.mjs +27 -0
- package/dist/chunk-FP4EXCGS.mjs.map +1 -0
- package/dist/chunk-FRAKUJWH.mjs +3 -0
- package/dist/chunk-FRAKUJWH.mjs.map +1 -0
- package/dist/chunk-HW5IH6PV.mjs +19 -0
- package/dist/chunk-HW5IH6PV.mjs.map +1 -0
- package/dist/chunk-JD7BAH7U.mjs +3 -0
- package/dist/chunk-JD7BAH7U.mjs.map +1 -0
- package/dist/chunk-MRXNTQOX.mjs +55 -0
- package/dist/chunk-MRXNTQOX.mjs.map +1 -0
- package/dist/chunk-MZ5UN3BY.mjs +28 -0
- package/dist/chunk-MZ5UN3BY.mjs.map +1 -0
- package/dist/chunk-PUFR5OAI.mjs +3 -0
- package/dist/chunk-PUFR5OAI.mjs.map +1 -0
- package/dist/chunk-QTET3FBV.mjs +165 -0
- package/dist/chunk-QTET3FBV.mjs.map +1 -0
- package/dist/chunk-R2WOR4SW.mjs +113 -0
- package/dist/chunk-R2WOR4SW.mjs.map +1 -0
- package/dist/chunk-R75NEA76.mjs +77 -0
- package/dist/chunk-R75NEA76.mjs.map +1 -0
- package/dist/chunk-SPCZTZCY.mjs +3 -0
- package/dist/chunk-SPCZTZCY.mjs.map +1 -0
- package/dist/chunk-TKXPMMND.mjs +177 -0
- package/dist/chunk-TKXPMMND.mjs.map +1 -0
- package/dist/chunk-TU4DNDO7.mjs +125 -0
- package/dist/chunk-TU4DNDO7.mjs.map +1 -0
- package/dist/chunk-VK25FIML.mjs +144 -0
- package/dist/chunk-VK25FIML.mjs.map +1 -0
- package/dist/chunk-VNRDQJXW.mjs +3 -0
- package/dist/chunk-VNRDQJXW.mjs.map +1 -0
- package/dist/chunk-VUY2K2DI.mjs +135 -0
- package/dist/chunk-VUY2K2DI.mjs.map +1 -0
- package/dist/chunk-YULKJPY3.mjs +70 -0
- package/dist/chunk-YULKJPY3.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-provider-props.mjs +2 -1
- package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -1
- package/dist/components/copilot-provider/copilot-provider.mjs +7 -468
- package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -1
- package/dist/components/copilot-provider/index.mjs +8 -467
- package/dist/components/copilot-provider/index.mjs.map +1 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs +3 -12
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
- package/dist/components/index.mjs +9 -467
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.mjs +3 -70
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.mjs +4 -69
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.mjs +17 -646
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-chat.mjs +5 -452
- package/dist/hooks/use-chat.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +13 -591
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +3 -68
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +4 -95
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +4 -87
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +4 -87
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +3 -153
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.mjs +26 -1194
- package/dist/index.mjs.map +1 -1
- package/dist/openai/chat-completion-client.mjs +4 -353
- package/dist/openai/chat-completion-client.mjs.map +1 -1
- package/dist/openai/chat-completion-stream.mjs +4 -217
- package/dist/openai/chat-completion-stream.mjs.map +1 -1
- package/dist/openai/chat-completion-transport.mjs +3 -179
- package/dist/openai/chat-completion-transport.mjs.map +1 -1
- package/dist/openai/index.mjs +5 -217
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +14 -235
- package/dist/openai-assistants/hooks/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +8 -52
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +13 -236
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
- package/dist/openai-assistants/index.mjs +15 -236
- package/dist/openai-assistants/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/index.mjs +4 -46
- package/dist/openai-assistants/utils/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +3 -46
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
- package/dist/types/annotated-function.mjs +2 -0
- package/dist/types/annotated-function.mjs.map +1 -1
- package/dist/types/base.mjs +2 -0
- package/dist/types/base.mjs.map +1 -1
- package/dist/types/document-pointer.mjs +2 -0
- package/dist/types/document-pointer.mjs.map +1 -1
- package/dist/types/index.mjs +2 -0
- package/dist/types/index.mjs.map +1 -1
- package/dist/types/message.mjs +2 -0
- package/dist/types/message.mjs.map +1 -1
- package/dist/utils/utils.mjs +2 -0
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/utils.test.mjs +1 -0
- package/dist/utils/utils.test.mjs.map +1 -1
- package/package.json +2 -2
- package/src/hooks/use-chat.ts +15 -2
|
@@ -1,592 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __objRest = (source, exclude) => {
|
|
21
|
-
var target = {};
|
|
22
|
-
for (var prop in source)
|
|
23
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
-
target[prop] = source[prop];
|
|
25
|
-
if (source != null && __getOwnPropSymbols)
|
|
26
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
}
|
|
30
|
-
return target;
|
|
31
|
-
};
|
|
32
|
-
var __async = (__this, __arguments, generator) => {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
var fulfilled = (value) => {
|
|
35
|
-
try {
|
|
36
|
-
step(generator.next(value));
|
|
37
|
-
} catch (e) {
|
|
38
|
-
reject(e);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var rejected = (value) => {
|
|
42
|
-
try {
|
|
43
|
-
step(generator.throw(value));
|
|
44
|
-
} catch (e) {
|
|
45
|
-
reject(e);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/hooks/use-copilot-chat.ts
|
|
54
|
-
import { useMemo, useContext } from "react";
|
|
55
|
-
|
|
56
|
-
// src/context/copilot-context.tsx
|
|
57
|
-
import React from "react";
|
|
58
|
-
var emptyCopilotContext = {
|
|
59
|
-
entryPoints: {},
|
|
60
|
-
setEntryPoint: () => {
|
|
61
|
-
},
|
|
62
|
-
removeEntryPoint: () => {
|
|
63
|
-
},
|
|
64
|
-
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
65
|
-
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
66
|
-
})),
|
|
67
|
-
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
68
|
-
addContext: () => "",
|
|
69
|
-
removeContext: () => {
|
|
70
|
-
},
|
|
71
|
-
getDocumentsContext: (categories) => returnAndThrowInDebug([]),
|
|
72
|
-
addDocumentContext: () => returnAndThrowInDebug(""),
|
|
73
|
-
removeDocumentContext: () => {
|
|
74
|
-
},
|
|
75
|
-
copilotApiConfig: new class {
|
|
76
|
-
get chatApiEndpoint() {
|
|
77
|
-
throw new Error(
|
|
78
|
-
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
get chatApiEndpointV2() {
|
|
82
|
-
throw new Error(
|
|
83
|
-
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
get headers() {
|
|
87
|
-
return {};
|
|
88
|
-
}
|
|
89
|
-
get body() {
|
|
90
|
-
return {};
|
|
91
|
-
}
|
|
92
|
-
}()
|
|
93
|
-
};
|
|
94
|
-
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
95
|
-
function returnAndThrowInDebug(value) {
|
|
96
|
-
throw new Error(
|
|
97
|
-
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
98
|
-
);
|
|
99
|
-
return value;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// src/hooks/use-chat.ts
|
|
103
|
-
import { useState } from "react";
|
|
104
|
-
import { nanoid } from "nanoid";
|
|
105
|
-
|
|
106
|
-
// src/openai/chat-completion-client.ts
|
|
107
|
-
import EventEmitter2 from "eventemitter3";
|
|
108
|
-
|
|
109
|
-
// src/openai/chat-completion-transport.ts
|
|
110
|
-
import EventEmitter from "eventemitter3";
|
|
111
|
-
var DEFAULT_MODEL = "gpt-4-1106-preview";
|
|
112
|
-
var ChatCompletionTransport = class extends EventEmitter {
|
|
113
|
-
constructor(params) {
|
|
114
|
-
super();
|
|
115
|
-
this.buffer = new Uint8Array();
|
|
116
|
-
this.bodyReader = null;
|
|
117
|
-
}
|
|
118
|
-
cleanup() {
|
|
119
|
-
return __async(this, null, function* () {
|
|
120
|
-
if (this.bodyReader) {
|
|
121
|
-
try {
|
|
122
|
-
yield this.bodyReader.cancel();
|
|
123
|
-
} catch (error) {
|
|
124
|
-
console.warn("Failed to cancel body reader:", error);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
this.bodyReader = null;
|
|
128
|
-
this.buffer = new Uint8Array();
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
fetch(_0) {
|
|
132
|
-
return __async(this, arguments, function* ({
|
|
133
|
-
copilotConfig,
|
|
134
|
-
model,
|
|
135
|
-
messages,
|
|
136
|
-
functions,
|
|
137
|
-
temperature,
|
|
138
|
-
headers,
|
|
139
|
-
body,
|
|
140
|
-
signal
|
|
141
|
-
}) {
|
|
142
|
-
yield this.cleanup();
|
|
143
|
-
temperature || (temperature = 0.5);
|
|
144
|
-
functions || (functions = []);
|
|
145
|
-
model || (model = DEFAULT_MODEL);
|
|
146
|
-
const cleanedMessages = messages.map((message) => {
|
|
147
|
-
const { content, role, name, function_call } = message;
|
|
148
|
-
return { content, role, name, function_call };
|
|
149
|
-
});
|
|
150
|
-
try {
|
|
151
|
-
const response = yield fetch(copilotConfig.chatApiEndpoint, {
|
|
152
|
-
method: "POST",
|
|
153
|
-
headers: __spreadValues(__spreadValues({
|
|
154
|
-
"Content-Type": "application/json"
|
|
155
|
-
}, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
|
|
156
|
-
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
157
|
-
model,
|
|
158
|
-
messages: cleanedMessages,
|
|
159
|
-
stream: true
|
|
160
|
-
}, functions.length ? { functions } : {}), temperature ? { temperature } : {}), functions.length != 0 ? { function_call: "auto" } : {}), copilotConfig.body), body ? __spreadValues({}, body) : {})),
|
|
161
|
-
signal
|
|
162
|
-
});
|
|
163
|
-
if (!response.ok) {
|
|
164
|
-
try {
|
|
165
|
-
const errorText = yield response.text();
|
|
166
|
-
yield this.cleanup();
|
|
167
|
-
const msg = `Error ${response.status}: ${errorText}`;
|
|
168
|
-
this.emit("error", new Error(msg));
|
|
169
|
-
} catch (_error) {
|
|
170
|
-
yield this.cleanup();
|
|
171
|
-
const msg = `Error ${response.status}: ${response.statusText}`;
|
|
172
|
-
this.emit("error", new Error(msg));
|
|
173
|
-
}
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (response.body == null) {
|
|
177
|
-
yield this.cleanup();
|
|
178
|
-
const msg = "Response body is null";
|
|
179
|
-
this.emit("error", new Error(msg));
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
this.bodyReader = response.body.getReader();
|
|
183
|
-
yield this.streamBody();
|
|
184
|
-
} catch (error) {
|
|
185
|
-
yield this.cleanup();
|
|
186
|
-
this.emit("error", error);
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
streamBody() {
|
|
192
|
-
return __async(this, null, function* () {
|
|
193
|
-
while (true) {
|
|
194
|
-
try {
|
|
195
|
-
const { done, value } = yield this.bodyReader.read();
|
|
196
|
-
if (done) {
|
|
197
|
-
yield this.cleanup();
|
|
198
|
-
this.emit("end");
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
const shouldContinue = yield this.processData(value);
|
|
202
|
-
if (!shouldContinue) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
} catch (error) {
|
|
206
|
-
yield this.cleanup();
|
|
207
|
-
this.emit("error", error);
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
processData(data) {
|
|
214
|
-
return __async(this, null, function* () {
|
|
215
|
-
const newBuffer = new Uint8Array(this.buffer.length + data.length);
|
|
216
|
-
newBuffer.set(this.buffer);
|
|
217
|
-
newBuffer.set(data, this.buffer.length);
|
|
218
|
-
this.buffer = newBuffer;
|
|
219
|
-
const dataString = new TextDecoder("utf-8").decode(this.buffer);
|
|
220
|
-
let lines = dataString.split("\n").filter((line) => line.trim() !== "");
|
|
221
|
-
if (!dataString.endsWith("\n")) {
|
|
222
|
-
const lastLine = lines.pop() || "";
|
|
223
|
-
const remainingBytes = new TextEncoder().encode(lastLine);
|
|
224
|
-
this.buffer = new Uint8Array(remainingBytes);
|
|
225
|
-
} else {
|
|
226
|
-
this.buffer = new Uint8Array();
|
|
227
|
-
}
|
|
228
|
-
for (const line of lines) {
|
|
229
|
-
const cleanedLine = line.replace(/^data: /, "");
|
|
230
|
-
if (cleanedLine === "[DONE]") {
|
|
231
|
-
yield this.cleanup();
|
|
232
|
-
this.emit("end");
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
let json;
|
|
236
|
-
try {
|
|
237
|
-
json = JSON.parse(cleanedLine);
|
|
238
|
-
} catch (error) {
|
|
239
|
-
console.error("Failed to parse JSON:", error);
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
this.emit("data", json);
|
|
243
|
-
}
|
|
244
|
-
return true;
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
// src/openai/chat-completion-client.ts
|
|
250
|
-
var DEFAULT_MAX_TOKENS = 8192;
|
|
251
|
-
var ChatCompletionClient = class extends EventEmitter2 {
|
|
252
|
-
constructor(params) {
|
|
253
|
-
super();
|
|
254
|
-
this.chatCompletionTransport = null;
|
|
255
|
-
this.mode = null;
|
|
256
|
-
this.functionCallName = "";
|
|
257
|
-
this.functionCallArguments = "";
|
|
258
|
-
this.onData = (data) => {
|
|
259
|
-
if (this.mode === "function" && !data.choices[0].delta.function_call) {
|
|
260
|
-
const success = this.tryFlushFunctionCall();
|
|
261
|
-
if (!success) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
this.mode = data.choices[0].delta.function_call ? "function" : "message";
|
|
266
|
-
if (this.mode === "message") {
|
|
267
|
-
if (data.choices[0].delta.content) {
|
|
268
|
-
this.emit("content", data.choices[0].delta.content);
|
|
269
|
-
}
|
|
270
|
-
return;
|
|
271
|
-
} else if (this.mode === "function") {
|
|
272
|
-
if (data.choices[0].delta.function_call.name) {
|
|
273
|
-
this.functionCallName = data.choices[0].delta.function_call.name;
|
|
274
|
-
}
|
|
275
|
-
if (data.choices[0].delta.function_call.arguments) {
|
|
276
|
-
this.functionCallArguments += data.choices[0].delta.function_call.arguments;
|
|
277
|
-
}
|
|
278
|
-
this.emit("partial", this.functionCallName, this.functionCallArguments);
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
this.onError = (error) => {
|
|
283
|
-
this.emit("error", error);
|
|
284
|
-
this.cleanup();
|
|
285
|
-
};
|
|
286
|
-
this.onEnd = () => {
|
|
287
|
-
if (this.mode === "function") {
|
|
288
|
-
const success = this.tryFlushFunctionCall();
|
|
289
|
-
if (!success) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
this.emit("end");
|
|
294
|
-
this.cleanup();
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
fetch(params) {
|
|
298
|
-
return __async(this, null, function* () {
|
|
299
|
-
params = __spreadValues({}, params);
|
|
300
|
-
if (params.model && params.model in maxTokensByModel) {
|
|
301
|
-
params.maxTokens || (params.maxTokens = maxTokensByModel[params.model]);
|
|
302
|
-
} else {
|
|
303
|
-
params.maxTokens || (params.maxTokens = DEFAULT_MAX_TOKENS);
|
|
304
|
-
}
|
|
305
|
-
params.functions || (params.functions = []);
|
|
306
|
-
params.messages = this.buildPrompt(params);
|
|
307
|
-
return yield this.runPrompt(params);
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
buildPrompt(params) {
|
|
311
|
-
let maxTokens = params.maxTokens;
|
|
312
|
-
const messages = params.messages;
|
|
313
|
-
const functions = params.functions;
|
|
314
|
-
const functionsNumTokens = countFunctionsTokens(functions);
|
|
315
|
-
if (functionsNumTokens > maxTokens) {
|
|
316
|
-
throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);
|
|
317
|
-
}
|
|
318
|
-
maxTokens -= functionsNumTokens;
|
|
319
|
-
for (const message of messages) {
|
|
320
|
-
if (message.role === "system") {
|
|
321
|
-
const numTokens = this.countTokens(message);
|
|
322
|
-
maxTokens -= numTokens;
|
|
323
|
-
if (maxTokens < 0) {
|
|
324
|
-
throw new Error("Not enough tokens for system message.");
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
const result = [];
|
|
329
|
-
let cutoff = false;
|
|
330
|
-
const reversedMessages = [...messages].reverse();
|
|
331
|
-
for (const message of reversedMessages) {
|
|
332
|
-
if (message.role === "system") {
|
|
333
|
-
result.unshift(message);
|
|
334
|
-
continue;
|
|
335
|
-
} else if (cutoff) {
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
let numTokens = this.countTokens(message);
|
|
339
|
-
if (maxTokens < numTokens) {
|
|
340
|
-
cutoff = true;
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
result.unshift(message);
|
|
344
|
-
maxTokens -= numTokens;
|
|
345
|
-
}
|
|
346
|
-
return result;
|
|
347
|
-
}
|
|
348
|
-
runPrompt(params) {
|
|
349
|
-
return __async(this, null, function* () {
|
|
350
|
-
this.chatCompletionTransport = new ChatCompletionTransport({});
|
|
351
|
-
this.chatCompletionTransport.on("data", this.onData);
|
|
352
|
-
this.chatCompletionTransport.on("error", this.onError);
|
|
353
|
-
this.chatCompletionTransport.on("end", this.onEnd);
|
|
354
|
-
yield this.chatCompletionTransport.fetch(params);
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
tryFlushFunctionCall() {
|
|
358
|
-
let args = null;
|
|
359
|
-
try {
|
|
360
|
-
args = JSON.parse(this.functionCallArguments);
|
|
361
|
-
} catch (error) {
|
|
362
|
-
this.emit("error", error);
|
|
363
|
-
this.cleanup();
|
|
364
|
-
return false;
|
|
365
|
-
}
|
|
366
|
-
this.emit("function", {
|
|
367
|
-
name: this.functionCallName,
|
|
368
|
-
arguments: args
|
|
369
|
-
});
|
|
370
|
-
this.mode = null;
|
|
371
|
-
this.functionCallName = "";
|
|
372
|
-
this.functionCallArguments = "";
|
|
373
|
-
return true;
|
|
374
|
-
}
|
|
375
|
-
cleanup() {
|
|
376
|
-
if (this.chatCompletionTransport) {
|
|
377
|
-
this.chatCompletionTransport.off("data", this.onData);
|
|
378
|
-
this.chatCompletionTransport.off("error", this.onError);
|
|
379
|
-
this.chatCompletionTransport.off("end", this.onEnd);
|
|
380
|
-
}
|
|
381
|
-
this.chatCompletionTransport = null;
|
|
382
|
-
this.mode = null;
|
|
383
|
-
this.functionCallName = "";
|
|
384
|
-
this.functionCallArguments = "";
|
|
385
|
-
}
|
|
386
|
-
countTokens(message) {
|
|
387
|
-
if (message.content) {
|
|
388
|
-
return estimateTokens(message.content);
|
|
389
|
-
} else if (message.function_call) {
|
|
390
|
-
return estimateTokens(JSON.stringify(message.function_call));
|
|
391
|
-
}
|
|
392
|
-
return 0;
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
var maxTokensByModel = {
|
|
396
|
-
"gpt-3.5-turbo": 4097,
|
|
397
|
-
"gpt-3.5-turbo-16k": 16385,
|
|
398
|
-
"gpt-4": 8192,
|
|
399
|
-
"gpt-4-1106-preview": 8192,
|
|
400
|
-
"gpt-4-32k": 32768,
|
|
401
|
-
"gpt-3.5-turbo-0301": 4097,
|
|
402
|
-
"gpt-4-0314": 8192,
|
|
403
|
-
"gpt-4-32k-0314": 32768,
|
|
404
|
-
"gpt-3.5-turbo-0613": 4097,
|
|
405
|
-
"gpt-4-0613": 8192,
|
|
406
|
-
"gpt-4-32k-0613": 32768,
|
|
407
|
-
"gpt-3.5-turbo-16k-0613": 16385
|
|
408
|
-
};
|
|
409
|
-
function estimateTokens(text) {
|
|
410
|
-
return text.length / 3;
|
|
411
|
-
}
|
|
412
|
-
function countFunctionsTokens(functions) {
|
|
413
|
-
if (functions.length === 0) {
|
|
414
|
-
return 0;
|
|
415
|
-
}
|
|
416
|
-
const json = JSON.stringify(functions);
|
|
417
|
-
return estimateTokens(json);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// src/hooks/use-chat.ts
|
|
421
|
-
function useChat(options) {
|
|
422
|
-
const [messages, setMessages] = useState([]);
|
|
423
|
-
const [input, setInput] = useState("");
|
|
424
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
425
|
-
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
426
|
-
return new Promise((resolve, reject) => {
|
|
427
|
-
setIsLoading(true);
|
|
428
|
-
const assistantMessage = {
|
|
429
|
-
id: nanoid(),
|
|
430
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
431
|
-
content: "",
|
|
432
|
-
role: "assistant"
|
|
433
|
-
};
|
|
434
|
-
setMessages([...messages2, __spreadValues({}, assistantMessage)]);
|
|
435
|
-
const messagesWithContext = [...options.initialMessages || [], ...messages2];
|
|
436
|
-
const client = new ChatCompletionClient({});
|
|
437
|
-
const cleanup = () => {
|
|
438
|
-
client.off("content");
|
|
439
|
-
client.off("end");
|
|
440
|
-
client.off("error");
|
|
441
|
-
client.off("function");
|
|
442
|
-
};
|
|
443
|
-
client.on("content", (content) => {
|
|
444
|
-
assistantMessage.content += content;
|
|
445
|
-
setMessages([...messages2, __spreadValues({}, assistantMessage)]);
|
|
446
|
-
});
|
|
447
|
-
client.on("end", () => {
|
|
448
|
-
setIsLoading(false);
|
|
449
|
-
cleanup();
|
|
450
|
-
resolve(__spreadValues({}, assistantMessage));
|
|
451
|
-
});
|
|
452
|
-
client.on("error", (error) => {
|
|
453
|
-
setIsLoading(false);
|
|
454
|
-
cleanup();
|
|
455
|
-
reject(error);
|
|
456
|
-
});
|
|
457
|
-
client.on("function", (functionCall) => __async(this, null, function* () {
|
|
458
|
-
assistantMessage.function_call = {
|
|
459
|
-
name: functionCall.name,
|
|
460
|
-
arguments: JSON.stringify(functionCall.arguments)
|
|
461
|
-
};
|
|
462
|
-
setMessages([...messages2, __spreadValues({}, assistantMessage)]);
|
|
463
|
-
setIsLoading(false);
|
|
464
|
-
cleanup();
|
|
465
|
-
resolve(__spreadValues({}, assistantMessage));
|
|
466
|
-
}));
|
|
467
|
-
client.fetch({
|
|
468
|
-
copilotConfig: options.copilotConfig,
|
|
469
|
-
messages: messagesWithContext,
|
|
470
|
-
functions: options.functions,
|
|
471
|
-
headers: options.headers
|
|
472
|
-
});
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
476
|
-
const message = yield runChatCompletion(messages2);
|
|
477
|
-
if (message.function_call && options.onFunctionCall) {
|
|
478
|
-
yield options.onFunctionCall(messages2, message.function_call);
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
const append = (message) => __async(this, null, function* () {
|
|
482
|
-
if (isLoading) {
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
const newMessages = [...messages, message];
|
|
486
|
-
setMessages(newMessages);
|
|
487
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
488
|
-
});
|
|
489
|
-
const reload = () => __async(this, null, function* () {
|
|
490
|
-
if (isLoading || messages.length === 0) {
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
let newMessages = [...messages];
|
|
494
|
-
const lastMessage = messages[messages.length - 1];
|
|
495
|
-
if (lastMessage.role === "assistant") {
|
|
496
|
-
newMessages = newMessages.slice(0, -1);
|
|
497
|
-
}
|
|
498
|
-
setMessages(newMessages);
|
|
499
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
500
|
-
});
|
|
501
|
-
const stop = () => {
|
|
502
|
-
throw new Error("Not implemented");
|
|
503
|
-
};
|
|
504
|
-
return {
|
|
505
|
-
messages,
|
|
506
|
-
append,
|
|
507
|
-
reload,
|
|
508
|
-
stop,
|
|
509
|
-
isLoading,
|
|
510
|
-
input,
|
|
511
|
-
setInput
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
// src/components/copilot-provider/copilot-provider.tsx
|
|
516
|
-
import { useCallback, useState as useState2 } from "react";
|
|
517
|
-
import { jsx } from "react/jsx-runtime";
|
|
518
|
-
var defaultCopilotContextCategories = ["global"];
|
|
519
|
-
|
|
520
|
-
// src/hooks/use-copilot-chat.ts
|
|
521
|
-
function useCopilotChat(_a) {
|
|
522
|
-
var _b = _a, {
|
|
523
|
-
makeSystemMessage
|
|
524
|
-
} = _b, options = __objRest(_b, [
|
|
525
|
-
"makeSystemMessage"
|
|
526
|
-
]);
|
|
527
|
-
const {
|
|
528
|
-
getContextString,
|
|
529
|
-
getChatCompletionFunctionDescriptions,
|
|
530
|
-
getFunctionCallHandler,
|
|
531
|
-
copilotApiConfig
|
|
532
|
-
} = useContext(CopilotContext);
|
|
533
|
-
const systemMessage = useMemo(() => {
|
|
534
|
-
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
535
|
-
const contextString = getContextString([], defaultCopilotContextCategories);
|
|
536
|
-
return {
|
|
537
|
-
id: "system",
|
|
538
|
-
content: systemMessageMaker(contextString),
|
|
539
|
-
role: "system"
|
|
540
|
-
};
|
|
541
|
-
}, [getContextString, makeSystemMessage]);
|
|
542
|
-
const functionDescriptions = useMemo(() => {
|
|
543
|
-
return getChatCompletionFunctionDescriptions();
|
|
544
|
-
}, [getChatCompletionFunctionDescriptions]);
|
|
545
|
-
const { messages, append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
546
|
-
copilotConfig: copilotApiConfig,
|
|
547
|
-
id: options.id,
|
|
548
|
-
initialMessages: [systemMessage].concat(options.initialMessages || []),
|
|
549
|
-
functions: functionDescriptions,
|
|
550
|
-
onFunctionCall: getFunctionCallHandler(),
|
|
551
|
-
headers: __spreadValues({}, options.headers),
|
|
552
|
-
body: __spreadValues({}, options.body)
|
|
553
|
-
}));
|
|
554
|
-
const visibleMessages = messages.filter(
|
|
555
|
-
(message) => message.role === "user" || message.role === "assistant"
|
|
556
|
-
);
|
|
557
|
-
return {
|
|
558
|
-
visibleMessages,
|
|
559
|
-
append,
|
|
560
|
-
reload,
|
|
561
|
-
stop,
|
|
562
|
-
isLoading,
|
|
563
|
-
input,
|
|
564
|
-
setInput
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
|
-
function defaultSystemMessage(contextString) {
|
|
568
|
-
return `
|
|
569
|
-
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
570
|
-
|
|
571
|
-
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.
|
|
572
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
573
|
-
|
|
574
|
-
The user has provided you with the following context:
|
|
575
|
-
\`\`\`
|
|
576
|
-
${contextString}
|
|
577
|
-
\`\`\`
|
|
578
|
-
|
|
579
|
-
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.
|
|
580
|
-
|
|
581
|
-
Please assist them as best you can.
|
|
582
|
-
|
|
583
|
-
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.
|
|
584
|
-
|
|
585
|
-
If you would like to call a function, call it without saying anything else.
|
|
586
|
-
`;
|
|
587
|
-
}
|
|
588
|
-
export {
|
|
589
|
-
defaultSystemMessage,
|
|
590
|
-
useCopilotChat
|
|
591
|
-
};
|
|
1
|
+
export { defaultSystemMessage, useCopilotChat } from '../chunk-R75NEA76.mjs';
|
|
2
|
+
import '../chunk-JD7BAH7U.mjs';
|
|
3
|
+
import '../chunk-SPCZTZCY.mjs';
|
|
4
|
+
import '../chunk-QTET3FBV.mjs';
|
|
5
|
+
import '../chunk-F2JIAPZQ.mjs';
|
|
6
|
+
import '../chunk-VUY2K2DI.mjs';
|
|
7
|
+
import '../chunk-6A4PCNMZ.mjs';
|
|
8
|
+
import '../chunk-R2WOR4SW.mjs';
|
|
9
|
+
import '../chunk-TKXPMMND.mjs';
|
|
10
|
+
import '../chunk-VK25FIML.mjs';
|
|
11
|
+
import '../chunk-YULKJPY3.mjs';
|
|
12
|
+
import '../chunk-MRXNTQOX.mjs';
|
|
13
|
+
//# sourceMappingURL=out.js.map
|
|
592
14
|
//# sourceMappingURL=use-copilot-chat.mjs.map
|