@copilotkit/shared 0.4.0-tools.0 → 0.4.0-tools.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.
Files changed (58) hide show
  1. package/.turbo/turbo-build.log +63 -63
  2. package/CHANGELOG.md +6 -0
  3. package/dist/{chunk-FHLEKC6N.mjs → chunk-CPRMXQXK.mjs} +14 -3
  4. package/dist/chunk-CPRMXQXK.mjs.map +1 -0
  5. package/dist/{chunk-HT5RV27L.mjs → chunk-T6YVHLB7.mjs} +1 -1
  6. package/dist/chunk-T6YVHLB7.mjs.map +1 -0
  7. package/dist/{chunk-NCTOECKL.mjs → chunk-XJLZZQTA.mjs} +11 -1
  8. package/dist/chunk-XJLZZQTA.mjs.map +1 -0
  9. package/dist/index.d.ts +2 -2
  10. package/dist/index.js +24 -2
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +27 -333
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/types/annotated-function.d.ts +1 -1
  15. package/dist/types/annotated-function.js.map +1 -1
  16. package/dist/types/annotated-function.mjs +1 -0
  17. package/dist/types/index.mjs +3 -0
  18. package/dist/types/openai-assistant.d.ts +5 -1
  19. package/dist/types/openai-assistant.js.map +1 -1
  20. package/dist/types/openai-assistant.mjs +1 -0
  21. package/dist/utils/annotated-function.mjs +3 -27
  22. package/dist/utils/annotated-function.mjs.map +1 -1
  23. package/dist/utils/decode-chat-completion-as-text.mjs +3 -27
  24. package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
  25. package/dist/utils/decode-chat-completion.d.ts +8 -2
  26. package/dist/utils/decode-chat-completion.js +13 -2
  27. package/dist/utils/decode-chat-completion.js.map +1 -1
  28. package/dist/utils/decode-chat-completion.mjs +3 -92
  29. package/dist/utils/decode-chat-completion.mjs.map +1 -1
  30. package/dist/utils/index.d.ts +2 -2
  31. package/dist/utils/index.js +24 -2
  32. package/dist/utils/index.js.map +1 -1
  33. package/dist/utils/index.mjs +24 -333
  34. package/dist/utils/index.mjs.map +1 -1
  35. package/dist/utils/parse-chat-completion.d.ts +2 -0
  36. package/dist/utils/parse-chat-completion.js.map +1 -1
  37. package/dist/utils/parse-chat-completion.mjs +3 -55
  38. package/dist/utils/parse-chat-completion.mjs.map +1 -1
  39. package/dist/utils/utils.d.ts +2 -1
  40. package/dist/utils/utils.js +11 -0
  41. package/dist/utils/utils.js.map +1 -1
  42. package/dist/utils/utils.mjs +11 -128
  43. package/dist/utils/utils.mjs.map +1 -1
  44. package/package.json +3 -3
  45. package/src/types/annotated-function.ts +1 -1
  46. package/src/types/openai-assistant.ts +6 -1
  47. package/src/utils/decode-chat-completion.ts +26 -2
  48. package/src/utils/parse-chat-completion.ts +15 -0
  49. package/src/utils/utils.ts +10 -0
  50. package/dist/chunk-FHLEKC6N.mjs.map +0 -1
  51. package/dist/chunk-GFCJUF6J.mjs +0 -96
  52. package/dist/chunk-GFCJUF6J.mjs.map +0 -1
  53. package/dist/chunk-HT5RV27L.mjs.map +0 -1
  54. package/dist/chunk-L46AW3ET.mjs +0 -29
  55. package/dist/chunk-L46AW3ET.mjs.map +0 -1
  56. package/dist/chunk-NCTOECKL.mjs.map +0 -1
  57. package/dist/chunk-W6NBBCWB.mjs +0 -60
  58. package/dist/chunk-W6NBBCWB.mjs.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,342 +1,36 @@
1
- // src/utils/utils.ts
2
- var textStreamPart = {
3
- code: "0",
4
- name: "text",
5
- parse: (value) => {
6
- if (typeof value !== "string") {
7
- throw new Error('"text" parts expect a string value.');
8
- }
9
- return { type: "text", value };
10
- }
11
- };
12
- var functionCallStreamPart = {
13
- code: "1",
14
- name: "function_call",
15
- parse: (value) => {
16
- if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
17
- throw new Error('"function_call" parts expect an object with a "function_call" property.');
18
- }
19
- return {
20
- type: "function_call",
21
- value
22
- };
23
- }
24
- };
25
- var dataStreamPart = {
26
- code: "2",
27
- name: "data",
28
- parse: (value) => {
29
- if (!Array.isArray(value)) {
30
- throw new Error('"data" parts expect an array value.');
31
- }
32
- return { type: "data", value };
33
- }
34
- };
35
- var errorStreamPart = {
36
- code: "3",
37
- name: "error",
38
- parse: (value) => {
39
- if (typeof value !== "string") {
40
- throw new Error('"error" parts expect a string value.');
41
- }
42
- return { type: "error", value };
43
- }
44
- };
45
- var assistantMessage = {
46
- code: "4",
47
- name: "assistant_message",
48
- parse: (value) => {
49
- if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
50
- (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
51
- )) {
52
- throw new Error(
53
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
54
- );
55
- }
56
- return {
57
- type: "assistant_message",
58
- value
59
- };
60
- }
61
- };
62
- var assistantControlData = {
63
- code: "5",
64
- name: "assistant_control_data",
65
- parse: (value) => {
66
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
67
- throw new Error(
68
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
69
- );
70
- }
71
- return {
72
- type: "assistant_control_data",
73
- value: {
74
- threadId: value.threadId,
75
- messageId: value.messageId
76
- }
77
- };
78
- }
79
- };
80
- var streamParts = [
81
- textStreamPart,
82
- functionCallStreamPart,
83
- dataStreamPart,
84
- errorStreamPart,
85
- assistantMessage,
86
- assistantControlData
87
- ];
88
- var streamPartsByCode = {
89
- [textStreamPart.code]: textStreamPart,
90
- [functionCallStreamPart.code]: functionCallStreamPart,
91
- [dataStreamPart.code]: dataStreamPart,
92
- [errorStreamPart.code]: errorStreamPart,
93
- [assistantMessage.code]: assistantMessage,
94
- [assistantControlData.code]: assistantControlData
95
- };
96
- var StreamStringPrefixes = {
97
- [textStreamPart.name]: textStreamPart.code,
98
- [functionCallStreamPart.name]: functionCallStreamPart.code,
99
- [dataStreamPart.name]: dataStreamPart.code,
100
- [errorStreamPart.name]: errorStreamPart.code,
101
- [assistantMessage.name]: assistantMessage.code,
102
- [assistantControlData.name]: assistantControlData.code
103
- };
104
- var validCodes = streamParts.map((part) => part.code);
105
- var parseStreamPart = (line) => {
106
- const firstSeparatorIndex = line.indexOf(":");
107
- if (firstSeparatorIndex === -1) {
108
- throw new Error("Failed to parse stream string. No separator found.");
109
- }
110
- const prefix = line.slice(0, firstSeparatorIndex);
111
- if (!validCodes.includes(prefix)) {
112
- throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
113
- }
114
- const code = prefix;
115
- const textValue = line.slice(firstSeparatorIndex + 1);
116
- const jsonValue = JSON.parse(textValue);
117
- return streamPartsByCode[code].parse(jsonValue);
118
- };
119
- function formatStreamPart(type, value) {
120
- const streamPart = streamParts.find((part) => part.name === type);
121
- if (!streamPart) {
122
- throw new Error(`Invalid stream part type: ${type}`);
123
- }
124
- return `${streamPart.code}:${JSON.stringify(value)}
125
- `;
126
- }
127
- var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
128
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
129
-
130
- // src/utils/parse-chat-completion.ts
131
- function parseChatCompletion(stream) {
132
- const reader = stream.getReader();
133
- let buffer = new Uint8Array();
134
- async function cleanup(controller) {
135
- if (controller) {
136
- try {
137
- controller.close();
138
- } catch (_) {
139
- }
140
- }
141
- if (reader) {
142
- try {
143
- await reader.cancel();
144
- } catch (_) {
145
- }
146
- }
147
- }
148
- return new ReadableStream({
149
- async pull(controller) {
150
- while (true) {
151
- try {
152
- const { done, value } = await reader.read();
153
- if (done) {
154
- await cleanup(controller);
155
- return;
156
- }
157
- const newBuffer = new Uint8Array(buffer.length + value.length);
158
- newBuffer.set(buffer);
159
- newBuffer.set(value, buffer.length);
160
- buffer = newBuffer;
161
- const valueString = new TextDecoder("utf-8").decode(buffer);
162
- const lines = valueString.split("\n").filter((line) => line.trim() !== "");
163
- buffer = !valueString.endsWith("\n") ? new TextEncoder().encode(lines.pop() || "") : new Uint8Array();
164
- for (const line of lines) {
165
- const cleanedLine = line.replace(/^data: /, "");
166
- if (cleanedLine === "[DONE]") {
167
- await cleanup(controller);
168
- return;
169
- }
170
- const json = JSON.parse(cleanedLine);
171
- controller.enqueue(json);
172
- }
173
- } catch (error) {
174
- controller.error(error);
175
- await cleanup(controller);
176
- return;
177
- }
178
- }
179
- },
180
- cancel() {
181
- reader.cancel();
182
- }
183
- });
184
- }
185
-
186
- // src/utils/decode-chat-completion.ts
187
- function decodeChatCompletion(stream) {
188
- const reader = stream.getReader();
189
- let mode = null;
190
- let functionCallName = "";
191
- let functionCallArguments = "";
192
- async function cleanup(controller) {
193
- if (controller) {
194
- try {
195
- controller.close();
196
- } catch (_) {
197
- }
198
- }
199
- if (reader) {
200
- try {
201
- await reader.cancel();
202
- } catch (_) {
203
- }
204
- }
205
- }
206
- return new ReadableStream({
207
- async pull(controller) {
208
- var _a, _b, _c, _d, _e, _f;
209
- const flushFunctionCall = () => {
210
- let args = null;
211
- try {
212
- args = JSON.parse(functionCallArguments);
213
- } catch (error) {
214
- cleanup(controller);
215
- controller.error(error);
216
- return false;
217
- }
218
- controller.enqueue({
219
- type: "function",
220
- name: functionCallName,
221
- arguments: args
222
- });
223
- mode = null;
224
- functionCallName = "";
225
- functionCallArguments = "";
226
- return true;
227
- };
228
- while (true) {
229
- try {
230
- const { done, value } = await reader.read();
231
- if (done) {
232
- if (mode === "function") {
233
- flushFunctionCall();
234
- }
235
- await cleanup(controller);
236
- return;
237
- }
238
- if (mode === "function" && (!((_b = (_a = value.choices[0].delta.tool_calls) == null ? void 0 : _a[0]) == null ? void 0 : _b.function) || ((_d = (_c = value.choices[0].delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function.name))) {
239
- if (!flushFunctionCall()) {
240
- return;
241
- }
242
- }
243
- mode = ((_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function) ? "function" : "message";
244
- if (mode === "message") {
245
- if (value.choices[0].delta.content) {
246
- controller.enqueue({
247
- type: "content",
248
- content: value.choices[0].delta.content
249
- });
250
- }
251
- continue;
252
- } else if (mode === "function") {
253
- if (value.choices[0].delta.tool_calls[0].function.name) {
254
- functionCallName = value.choices[0].delta.tool_calls[0].function.name;
255
- }
256
- if (value.choices[0].delta.tool_calls[0].function.arguments) {
257
- functionCallArguments += value.choices[0].delta.tool_calls[0].function.arguments;
258
- }
259
- controller.enqueue({
260
- type: "partial",
261
- name: functionCallName,
262
- arguments: functionCallArguments
263
- });
264
- continue;
265
- }
266
- } catch (error) {
267
- controller.error(error);
268
- await cleanup(controller);
269
- return;
270
- }
271
- }
272
- },
273
- cancel() {
274
- reader.cancel();
275
- }
276
- });
277
- }
278
-
279
- // src/utils/decode-chat-completion-as-text.ts
280
- function decodeChatCompletionAsText(stream) {
281
- const reader = stream.getReader();
282
- return new ReadableStream({
283
- async pull(controller) {
284
- while (true) {
285
- try {
286
- const { done, value } = await reader.read();
287
- if (done) {
288
- controller.close();
289
- return;
290
- }
291
- if (value.type === "content") {
292
- controller.enqueue(value.content);
293
- continue;
294
- }
295
- } catch (error) {
296
- controller.error(error);
297
- return;
298
- }
299
- }
300
- },
301
- cancel() {
302
- reader.cancel();
303
- }
304
- });
305
- }
306
-
307
- // src/utils/annotated-function.ts
308
- function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
309
- let parameters = {};
310
- for (let arg of annotatedFunction.argumentAnnotations) {
311
- let { name, required, ...forwardedArgs } = arg;
312
- parameters[arg.name] = forwardedArgs;
313
- }
314
- let requiredParameterNames = [];
315
- for (let arg of annotatedFunction.argumentAnnotations) {
316
- if (arg.required) {
317
- requiredParameterNames.push(arg.name);
318
- }
319
- }
320
- let chatCompletionFunction = {
321
- type: "function",
322
- function: {
323
- name: annotatedFunction.name,
324
- description: annotatedFunction.description,
325
- parameters: {
326
- type: "object",
327
- properties: parameters,
328
- required: requiredParameterNames
329
- }
330
- }
331
- };
332
- return chatCompletionFunction;
333
- }
1
+ import "./chunk-FD6FGKYY.mjs";
2
+ import "./chunk-YBHX4Y25.mjs";
3
+ import "./chunk-IAFBVORQ.mjs";
4
+ import "./chunk-UAPRMZEY.mjs";
5
+ import {
6
+ parseChatCompletion
7
+ } from "./chunk-T6YVHLB7.mjs";
8
+ import {
9
+ COMPLEX_HEADER,
10
+ StreamStringPrefixes,
11
+ encodeResult,
12
+ formatStreamPart,
13
+ isStreamStringEqualToType,
14
+ parseStreamPart,
15
+ streamPartsByCode,
16
+ validCodes
17
+ } from "./chunk-XJLZZQTA.mjs";
18
+ import {
19
+ annotatedFunctionToChatCompletionFunction
20
+ } from "./chunk-NSY3OEEP.mjs";
21
+ import {
22
+ decodeChatCompletionAsText
23
+ } from "./chunk-4MTSDAP6.mjs";
24
+ import {
25
+ decodeChatCompletion
26
+ } from "./chunk-CPRMXQXK.mjs";
334
27
  export {
335
28
  COMPLEX_HEADER,
336
29
  StreamStringPrefixes,
337
30
  annotatedFunctionToChatCompletionFunction,
338
31
  decodeChatCompletion,
339
32
  decodeChatCompletionAsText,
33
+ encodeResult,
340
34
  formatStreamPart,
341
35
  isStreamStringEqualToType,
342
36
  parseChatCompletion,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils/utils.ts","../src/utils/parse-chat-completion.ts","../src/utils/decode-chat-completion.ts","../src/utils/decode-chat-completion-as-text.ts","../src/utils/annotated-function.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n","import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: {\n index: number;\n id?: string;\n function: {\n arguments?: string;\n name?: string;\n };\n }[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionChunk } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are in a function call but the next message is\n // - not a function call\n // - another function call (when name is present)\n // => flush it.\n if (\n mode === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n mode = value.choices[0].delta.tool_calls?.[0]?.function ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"message\") {\n if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode === \"function\") {\n if (value.choices[0].delta.tool_calls![0].function.name) {\n functionCallName = value.choices[0].delta.tool_calls![0].function.name!;\n }\n if (value.choices[0].delta.tool_calls![0].function.arguments) {\n functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;\n }\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { AnnotatedFunction, FunctionDefinition, ToolDefinition } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";AAQA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AASA,IAAM,yBAA4F;AAAA,EAChG,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM,IAAI,MAAM,yEAAyE;AAAA,IAC3F;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,iBAA4D;AAAA,EAChE,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,WAAO,EAAE,MAAM,QAA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B,QAAM,SAAS,OAAO,UAAU;AAEhC,SAAO,IAAI,eAAuB;AAAA,IAChC,MAAM,KAAK,YAAY;AACrB,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,uBAAW,MAAM;AACjB;AAAA,UACF;AAEA,cAAI,MAAM,SAAS,WAAW;AAC5B,uBAAW,QAAQ,MAAM,OAAO;AAChC;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;;;AC9BO,SAAS,0CACd,mBACgB;AAEhB,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAAI,EAAE,MAAM,UAAU,GAAG,cAAc,IAAI;AAC3C,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -18,7 +18,7 @@ interface AnnotatedFunction<Inputs extends any[]> {
18
18
  name: string;
19
19
  description: string;
20
20
  argumentAnnotations: AnnotatedFunctionArgument[];
21
- implementation: (...args: Inputs) => Promise<void>;
21
+ implementation: (...args: Inputs) => Promise<any>;
22
22
  }
23
23
 
24
24
  export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/types/annotated-function.ts"],"sourcesContent":["export interface AnnotatedFunctionSimpleArgument {\n name: string;\n type: \"string\" | \"number\" | \"boolean\" | \"object\"; // Add or change types according to your needs.\n description: string;\n required: boolean;\n}\n\nexport interface AnnotatedFunctionArrayArgument {\n name: string;\n type: \"array\";\n items: {\n type: string;\n };\n description: string;\n required: boolean;\n}\n\nexport type AnnotatedFunctionArgument =\n | AnnotatedFunctionSimpleArgument\n | AnnotatedFunctionArrayArgument;\n\nexport interface AnnotatedFunction<Inputs extends any[]> {\n name: string;\n description: string;\n argumentAnnotations: AnnotatedFunctionArgument[];\n implementation: (...args: Inputs) => Promise<void>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
1
+ {"version":3,"sources":["../../src/types/annotated-function.ts"],"sourcesContent":["export interface AnnotatedFunctionSimpleArgument {\n name: string;\n type: \"string\" | \"number\" | \"boolean\" | \"object\"; // Add or change types according to your needs.\n description: string;\n required: boolean;\n}\n\nexport interface AnnotatedFunctionArrayArgument {\n name: string;\n type: \"array\";\n items: {\n type: string;\n };\n description: string;\n required: boolean;\n}\n\nexport type AnnotatedFunctionArgument =\n | AnnotatedFunctionSimpleArgument\n | AnnotatedFunctionArrayArgument;\n\nexport interface AnnotatedFunction<Inputs extends any[]> {\n name: string;\n description: string;\n argumentAnnotations: AnnotatedFunctionArgument[];\n implementation: (...args: Inputs) => Promise<any>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
@@ -1 +1,2 @@
1
+ import "../chunk-YBHX4Y25.mjs";
1
2
  //# sourceMappingURL=annotated-function.mjs.map
@@ -1 +1,4 @@
1
+ import "../chunk-FD6FGKYY.mjs";
2
+ import "../chunk-YBHX4Y25.mjs";
3
+ import "../chunk-IAFBVORQ.mjs";
1
4
  //# sourceMappingURL=index.mjs.map
@@ -11,6 +11,10 @@ interface FunctionCall {
11
11
  * The name of the function to call.
12
12
  */
13
13
  name?: string;
14
+ /**
15
+ * Temporarily add scope to the function call.
16
+ */
17
+ scope?: "client" | "server";
14
18
  }
15
19
  /**
16
20
  * Shared types between the API and UI packages.
@@ -59,7 +63,7 @@ interface ToolDefinition {
59
63
  type: "function";
60
64
  function: FunctionDefinition;
61
65
  }
62
- type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<void>;
66
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<any>;
63
67
  type AssistantMessage = {
64
68
  id: string;
65
69
  role: "assistant";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/types/openai-assistant.ts"],"sourcesContent":["export type Role = \"system\" | \"user\" | \"assistant\" | \"function\";\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments?: string;\n\n /**\n * The name of the function to call.\n */\n name?: string;\n}\n\n/**\n * Shared types between the API and UI packages.\n */\nexport interface Message {\n id: string;\n createdAt?: Date;\n content: string;\n ui?: string | null | undefined;\n role: Role;\n /**\n * If the message has a role of `function`, the `name` field is the name of the function.\n * Otherwise, the name field should not be set.\n */\n name?: string;\n /**\n * If the assistant role makes a function call, the `function_call` field\n * contains the function call name and arguments. Otherwise, the field should\n * not be set.\n */\n function_call?: string | FunctionCall;\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](/docs/guides/gpt/function-calling) for examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown>;\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallHandler = (\n chatMessages: Message[],\n functionCall: FunctionCall,\n) => Promise<void>;\n\nexport type AssistantMessage = {\n id: string;\n role: \"assistant\";\n content: Array<{\n type: \"text\";\n text: {\n value: string;\n };\n }>;\n};\n\nexport type JSONValue =\n | null\n | string\n | number\n | boolean\n | { [x: string]: JSONValue }\n | Array<JSONValue>;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
1
+ {"version":3,"sources":["../../src/types/openai-assistant.ts"],"sourcesContent":["export type Role = \"system\" | \"user\" | \"assistant\" | \"function\";\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments?: string;\n\n /**\n * The name of the function to call.\n */\n name?: string;\n\n /**\n * Temporarily add scope to the function call.\n */\n scope?: \"client\" | \"server\";\n}\n\n/**\n * Shared types between the API and UI packages.\n */\nexport interface Message {\n id: string;\n createdAt?: Date;\n content: string;\n ui?: string | null | undefined;\n role: Role;\n /**\n * If the message has a role of `function`, the `name` field is the name of the function.\n * Otherwise, the name field should not be set.\n */\n name?: string;\n /**\n * If the assistant role makes a function call, the `function_call` field\n * contains the function call name and arguments. Otherwise, the field should\n * not be set.\n */\n function_call?: string | FunctionCall;\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](/docs/guides/gpt/function-calling) for examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown>;\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallHandler = (\n chatMessages: Message[],\n functionCall: FunctionCall,\n) => Promise<any>;\n\nexport type AssistantMessage = {\n id: string;\n role: \"assistant\";\n content: Array<{\n type: \"text\";\n text: {\n value: string;\n };\n }>;\n};\n\nexport type JSONValue =\n | null\n | string\n | number\n | boolean\n | { [x: string]: JSONValue }\n | Array<JSONValue>;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
@@ -1 +1,2 @@
1
+ import "../chunk-IAFBVORQ.mjs";
1
2
  //# sourceMappingURL=openai-assistant.mjs.map
@@ -1,30 +1,6 @@
1
- // src/utils/annotated-function.ts
2
- function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
3
- let parameters = {};
4
- for (let arg of annotatedFunction.argumentAnnotations) {
5
- let { name, required, ...forwardedArgs } = arg;
6
- parameters[arg.name] = forwardedArgs;
7
- }
8
- let requiredParameterNames = [];
9
- for (let arg of annotatedFunction.argumentAnnotations) {
10
- if (arg.required) {
11
- requiredParameterNames.push(arg.name);
12
- }
13
- }
14
- let chatCompletionFunction = {
15
- type: "function",
16
- function: {
17
- name: annotatedFunction.name,
18
- description: annotatedFunction.description,
19
- parameters: {
20
- type: "object",
21
- properties: parameters,
22
- required: requiredParameterNames
23
- }
24
- }
25
- };
26
- return chatCompletionFunction;
27
- }
1
+ import {
2
+ annotatedFunctionToChatCompletionFunction
3
+ } from "../chunk-NSY3OEEP.mjs";
28
4
  export {
29
5
  annotatedFunctionToChatCompletionFunction
30
6
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/annotated-function.ts"],"sourcesContent":["import { AnnotatedFunction, FunctionDefinition, ToolDefinition } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";AAEO,SAAS,0CACd,mBACgB;AAEhB,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAAI,EAAE,MAAM,UAAU,GAAG,cAAc,IAAI;AAC3C,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,30 +1,6 @@
1
- // src/utils/decode-chat-completion-as-text.ts
2
- function decodeChatCompletionAsText(stream) {
3
- const reader = stream.getReader();
4
- return new ReadableStream({
5
- async pull(controller) {
6
- while (true) {
7
- try {
8
- const { done, value } = await reader.read();
9
- if (done) {
10
- controller.close();
11
- return;
12
- }
13
- if (value.type === "content") {
14
- controller.enqueue(value.content);
15
- continue;
16
- }
17
- } catch (error) {
18
- controller.error(error);
19
- return;
20
- }
21
- }
22
- },
23
- cancel() {
24
- reader.cancel();
25
- }
26
- });
27
- }
1
+ import {
2
+ decodeChatCompletionAsText
3
+ } from "../chunk-4MTSDAP6.mjs";
28
4
  export {
29
5
  decodeChatCompletionAsText
30
6
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/decode-chat-completion-as-text.ts"],"sourcesContent":["import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n"],"mappings":";AAEO,SAAS,2BACd,QACwB;AACxB,QAAM,SAAS,OAAO,UAAU;AAEhC,SAAO,IAAI,eAAuB;AAAA,IAChC,MAAM,KAAK,YAAY;AACrB,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,uBAAW,MAAM;AACjB;AAAA,UACF;AAEA,cAAI,MAAM,SAAS,WAAW;AAC5B,uBAAW,QAAQ,MAAM,OAAO;AAChC;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -14,8 +14,14 @@ interface ChatCompletionFunctionEvent {
14
14
  type: "function";
15
15
  name: string;
16
16
  arguments: any;
17
+ scope: "client" | "server";
17
18
  }
18
- type ChatCompletionEvent = ChatCompletionContentEvent | ChatCompletionPartialEvent | ChatCompletionFunctionEvent;
19
+ interface ChatCompletionResultEvent {
20
+ type: "result";
21
+ content: string;
22
+ name: string;
23
+ }
24
+ type ChatCompletionEvent = ChatCompletionContentEvent | ChatCompletionPartialEvent | ChatCompletionFunctionEvent | ChatCompletionResultEvent;
19
25
  declare function decodeChatCompletion(stream: ReadableStream<ChatCompletionChunk>): ReadableStream<ChatCompletionEvent>;
20
26
 
21
- export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, decodeChatCompletion };
27
+ export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, ChatCompletionResultEvent, decodeChatCompletion };
@@ -28,6 +28,7 @@ function decodeChatCompletion(stream) {
28
28
  let mode = null;
29
29
  let functionCallName = "";
30
30
  let functionCallArguments = "";
31
+ let functionCallScope = "client";
31
32
  async function cleanup(controller) {
32
33
  if (controller) {
33
34
  try {
@@ -57,7 +58,8 @@ function decodeChatCompletion(stream) {
57
58
  controller.enqueue({
58
59
  type: "function",
59
60
  name: functionCallName,
60
- arguments: args
61
+ arguments: args,
62
+ scope: functionCallScope
61
63
  });
62
64
  mode = null;
63
65
  functionCallName = "";
@@ -81,7 +83,13 @@ function decodeChatCompletion(stream) {
81
83
  }
82
84
  mode = ((_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function) ? "function" : "message";
83
85
  if (mode === "message") {
84
- if (value.choices[0].delta.content) {
86
+ if (value.choices[0].delta.role === "function") {
87
+ controller.enqueue({
88
+ type: "result",
89
+ content: value.choices[0].delta.content,
90
+ name: value.choices[0].delta.name
91
+ });
92
+ } else if (value.choices[0].delta.content) {
85
93
  controller.enqueue({
86
94
  type: "content",
87
95
  content: value.choices[0].delta.content
@@ -95,6 +103,9 @@ function decodeChatCompletion(stream) {
95
103
  if (value.choices[0].delta.tool_calls[0].function.arguments) {
96
104
  functionCallArguments += value.choices[0].delta.tool_calls[0].function.arguments;
97
105
  }
106
+ if (value.choices[0].delta.tool_calls[0].function.scope) {
107
+ functionCallScope = value.choices[0].delta.tool_calls[0].function.scope;
108
+ }
98
109
  controller.enqueue({
99
110
  type: "partial",
100
111
  name: functionCallName,