@copilotkit/shared 0.9.0-multi-release-alpha.0 → 0.9.0-multi-feature-usecopilotreadable.3
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 +73 -77
- package/CHANGELOG.md +20 -2
- package/dist/constants/copilot-protocol.mjs +3 -2
- package/dist/constants/copilot-protocol.mjs.map +1 -1
- package/dist/constants/index.mjs +4 -2
- package/dist/constants/index.mjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -489
- package/dist/index.mjs.map +1 -1
- package/dist/types/action.mjs +1 -0
- package/dist/types/annotated-function.mjs +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/types/openai-assistant.mjs +1 -0
- package/dist/utils/annotated-function.mjs +5 -153
- package/dist/utils/annotated-function.mjs.map +1 -1
- package/dist/utils/decode-chat-completion-as-text.mjs +3 -27
- package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
- package/dist/utils/decode-chat-completion.js +19 -11
- package/dist/utils/decode-chat-completion.js.map +1 -1
- package/dist/utils/decode-chat-completion.mjs +3 -103
- package/dist/utils/decode-chat-completion.mjs.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +19 -11
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +26 -486
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/parse-chat-completion.d.ts +12 -10
- package/dist/utils/parse-chat-completion.js.map +1 -1
- package/dist/utils/parse-chat-completion.mjs +3 -55
- package/dist/utils/parse-chat-completion.mjs.map +1 -1
- package/dist/utils/utils.mjs +11 -144
- package/dist/utils/utils.mjs.map +1 -1
- package/package.json +3 -3
- package/src/utils/decode-chat-completion.ts +29 -16
- package/src/utils/parse-chat-completion.ts +19 -15
|
@@ -1,156 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
}
|
|
28
|
-
function convertAttribute(attribute) {
|
|
29
|
-
var _a, _b, _c;
|
|
30
|
-
switch (attribute.type) {
|
|
31
|
-
case "string":
|
|
32
|
-
return {
|
|
33
|
-
type: "string",
|
|
34
|
-
description: attribute.description,
|
|
35
|
-
...attribute.enum && { enum: attribute.enum }
|
|
36
|
-
};
|
|
37
|
-
case "number":
|
|
38
|
-
case "boolean":
|
|
39
|
-
return {
|
|
40
|
-
type: attribute.type,
|
|
41
|
-
description: attribute.description
|
|
42
|
-
};
|
|
43
|
-
case "object":
|
|
44
|
-
case "object[]":
|
|
45
|
-
const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce((acc, attr) => {
|
|
46
|
-
acc[attr.name] = convertAttribute(attr);
|
|
47
|
-
return acc;
|
|
48
|
-
}, {});
|
|
49
|
-
const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
50
|
-
if (attribute.type === "object[]") {
|
|
51
|
-
return {
|
|
52
|
-
type: "array",
|
|
53
|
-
items: {
|
|
54
|
-
type: "object",
|
|
55
|
-
...properties && { properties },
|
|
56
|
-
...required && required.length > 0 && { required }
|
|
57
|
-
},
|
|
58
|
-
description: attribute.description
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
type: "object",
|
|
63
|
-
description: attribute.description,
|
|
64
|
-
...properties && { properties },
|
|
65
|
-
...required && required.length > 0 && { required }
|
|
66
|
-
};
|
|
67
|
-
default:
|
|
68
|
-
if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
|
|
69
|
-
const itemType = attribute.type.slice(0, -2);
|
|
70
|
-
return {
|
|
71
|
-
type: "array",
|
|
72
|
-
items: { type: itemType },
|
|
73
|
-
description: attribute.description
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
type: "string",
|
|
78
|
-
description: attribute.description
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function actionToChatCompletionFunction(action) {
|
|
83
|
-
let parameters = {};
|
|
84
|
-
for (let parameter of action.parameters || []) {
|
|
85
|
-
parameters[parameter.name] = convertAttribute(parameter);
|
|
86
|
-
}
|
|
87
|
-
let requiredParameterNames = [];
|
|
88
|
-
for (let arg of action.parameters || []) {
|
|
89
|
-
if (arg.required !== false) {
|
|
90
|
-
requiredParameterNames.push(arg.name);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
let chatCompletionFunction = {
|
|
94
|
-
type: "function",
|
|
95
|
-
function: {
|
|
96
|
-
name: action.name,
|
|
97
|
-
...action.description && { description: action.description },
|
|
98
|
-
parameters: {
|
|
99
|
-
type: "object",
|
|
100
|
-
properties: parameters,
|
|
101
|
-
required: requiredParameterNames
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
return chatCompletionFunction;
|
|
106
|
-
}
|
|
107
|
-
function annotatedFunctionToAction(annotatedFunction) {
|
|
108
|
-
const parameters = annotatedFunction.argumentAnnotations.map((annotation) => {
|
|
109
|
-
switch (annotation.type) {
|
|
110
|
-
case "string":
|
|
111
|
-
case "number":
|
|
112
|
-
case "boolean":
|
|
113
|
-
case "object":
|
|
114
|
-
return {
|
|
115
|
-
name: annotation.name,
|
|
116
|
-
description: annotation.description,
|
|
117
|
-
type: annotation.type,
|
|
118
|
-
required: annotation.required
|
|
119
|
-
};
|
|
120
|
-
case "array":
|
|
121
|
-
let type;
|
|
122
|
-
if (annotation.items.type === "string") {
|
|
123
|
-
type = "string[]";
|
|
124
|
-
} else if (annotation.items.type === "number") {
|
|
125
|
-
type = "number[]";
|
|
126
|
-
} else if (annotation.items.type === "boolean") {
|
|
127
|
-
type = "boolean[]";
|
|
128
|
-
} else if (annotation.items.type === "object") {
|
|
129
|
-
type = "object[]";
|
|
130
|
-
} else {
|
|
131
|
-
type = "string[]";
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
name: annotation.name,
|
|
135
|
-
description: annotation.description,
|
|
136
|
-
type,
|
|
137
|
-
required: annotation.required
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
return {
|
|
142
|
-
name: annotatedFunction.name,
|
|
143
|
-
description: annotatedFunction.description,
|
|
144
|
-
parameters,
|
|
145
|
-
handler: (args) => {
|
|
146
|
-
const paramsInCorrectOrder = [];
|
|
147
|
-
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
148
|
-
paramsInCorrectOrder.push(args[arg.name]);
|
|
149
|
-
}
|
|
150
|
-
return annotatedFunction.implementation(...paramsInCorrectOrder);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
actionToChatCompletionFunction,
|
|
3
|
+
annotatedFunctionToAction,
|
|
4
|
+
annotatedFunctionToChatCompletionFunction
|
|
5
|
+
} from "../chunk-K7TZQSAZ.mjs";
|
|
154
6
|
export {
|
|
155
7
|
actionToChatCompletionFunction,
|
|
156
8
|
annotatedFunctionToAction,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,30 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -70,19 +70,24 @@ function decodeChatCompletion(stream) {
|
|
|
70
70
|
try {
|
|
71
71
|
const { done, value } = await reader.read();
|
|
72
72
|
if (done) {
|
|
73
|
-
if (mode === "function") {
|
|
73
|
+
if ((mode == null ? void 0 : mode.type) === "function") {
|
|
74
74
|
flushFunctionCall();
|
|
75
75
|
}
|
|
76
76
|
await cleanup(controller);
|
|
77
77
|
return;
|
|
78
78
|
}
|
|
79
|
-
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))) {
|
|
79
|
+
if ((mode == null ? void 0 : mode.type) === "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))) {
|
|
80
80
|
if (!flushFunctionCall()) {
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
|
|
85
|
-
if (
|
|
84
|
+
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
85
|
+
if (maybeFunctionCall) {
|
|
86
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
87
|
+
} else {
|
|
88
|
+
mode = { type: "message" };
|
|
89
|
+
}
|
|
90
|
+
if (mode.type === "message") {
|
|
86
91
|
if (value.choices[0].delta.role === "function") {
|
|
87
92
|
controller.enqueue({
|
|
88
93
|
type: "result",
|
|
@@ -96,15 +101,18 @@ function decodeChatCompletion(stream) {
|
|
|
96
101
|
});
|
|
97
102
|
}
|
|
98
103
|
continue;
|
|
99
|
-
} else if (mode === "function") {
|
|
100
|
-
|
|
101
|
-
|
|
104
|
+
} else if (mode.type === "function") {
|
|
105
|
+
const maybeFunctionCallName = mode.function.name;
|
|
106
|
+
if (maybeFunctionCallName) {
|
|
107
|
+
functionCallName = maybeFunctionCallName;
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
|
|
109
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
110
|
+
if (maybeFunctionCallArguments) {
|
|
111
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
105
112
|
}
|
|
106
|
-
|
|
107
|
-
|
|
113
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
114
|
+
if (maybeFunctionCallScope) {
|
|
115
|
+
functionCallScope = maybeFunctionCallScope;
|
|
108
116
|
}
|
|
109
117
|
controller.enqueue({
|
|
110
118
|
type: "partial",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/decode-chat-completion.ts"],"sourcesContent":["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 scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n
|
|
1
|
+
{"version":3,"sources":["../../src/utils/decode-chat-completion.ts"],"sourcesContent":["import { ChatCompletionChunk, ToolCallFunctionCall } 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 scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n type Mode = { type: \"function\"; function: ToolCallFunctionCall } | { type: \"message\" };\n\n let mode: Mode | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\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 scope: functionCallScope,\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?.type === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are currently handling a function call but the next message is either\n // - not a function call\n // - or is another function call (indicated by the presence of 'name' field in the next function call object)\n // => flush the current function call.\n if (\n mode?.type === \"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 const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;\n if (maybeFunctionCall) {\n mode = { type: \"function\", function: maybeFunctionCall };\n } else {\n mode = { type: \"message\" };\n }\n\n // if we get a message, emit the content and continue;\n if (mode.type === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else 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.type === \"function\") {\n const maybeFunctionCallName = mode.function.name;\n if (maybeFunctionCallName) {\n functionCallName = maybeFunctionCallName;\n }\n\n const maybeFunctionCallArguments = mode.function.arguments;\n if (maybeFunctionCallArguments) {\n functionCallArguments += maybeFunctionCallArguments;\n }\n\n const maybeFunctionCallScope = mode.function.scope;\n if (maybeFunctionCallScope) {\n functionCallScope = maybeFunctionCallScope;\n }\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"],"mappings":"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","names":[]}
|
|
@@ -1,106 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
let mode = null;
|
|
5
|
-
let functionCallName = "";
|
|
6
|
-
let functionCallArguments = "";
|
|
7
|
-
let functionCallScope = "client";
|
|
8
|
-
async function cleanup(controller) {
|
|
9
|
-
if (controller) {
|
|
10
|
-
try {
|
|
11
|
-
controller.close();
|
|
12
|
-
} catch (_) {
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
if (reader) {
|
|
16
|
-
try {
|
|
17
|
-
await reader.cancel();
|
|
18
|
-
} catch (_) {
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return new ReadableStream({
|
|
23
|
-
async pull(controller) {
|
|
24
|
-
var _a, _b, _c, _d, _e, _f;
|
|
25
|
-
const flushFunctionCall = () => {
|
|
26
|
-
let args = null;
|
|
27
|
-
try {
|
|
28
|
-
args = JSON.parse(functionCallArguments);
|
|
29
|
-
} catch (error) {
|
|
30
|
-
cleanup(controller);
|
|
31
|
-
controller.error(error);
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
controller.enqueue({
|
|
35
|
-
type: "function",
|
|
36
|
-
name: functionCallName,
|
|
37
|
-
arguments: args,
|
|
38
|
-
scope: functionCallScope
|
|
39
|
-
});
|
|
40
|
-
mode = null;
|
|
41
|
-
functionCallName = "";
|
|
42
|
-
functionCallArguments = "";
|
|
43
|
-
return true;
|
|
44
|
-
};
|
|
45
|
-
while (true) {
|
|
46
|
-
try {
|
|
47
|
-
const { done, value } = await reader.read();
|
|
48
|
-
if (done) {
|
|
49
|
-
if (mode === "function") {
|
|
50
|
-
flushFunctionCall();
|
|
51
|
-
}
|
|
52
|
-
await cleanup(controller);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
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))) {
|
|
56
|
-
if (!flushFunctionCall()) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
mode = ((_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function) ? "function" : "message";
|
|
61
|
-
if (mode === "message") {
|
|
62
|
-
if (value.choices[0].delta.role === "function") {
|
|
63
|
-
controller.enqueue({
|
|
64
|
-
type: "result",
|
|
65
|
-
content: value.choices[0].delta.content,
|
|
66
|
-
name: value.choices[0].delta.name
|
|
67
|
-
});
|
|
68
|
-
} else if (value.choices[0].delta.content) {
|
|
69
|
-
controller.enqueue({
|
|
70
|
-
type: "content",
|
|
71
|
-
content: value.choices[0].delta.content
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
continue;
|
|
75
|
-
} else if (mode === "function") {
|
|
76
|
-
if (value.choices[0].delta.tool_calls[0].function.name) {
|
|
77
|
-
functionCallName = value.choices[0].delta.tool_calls[0].function.name;
|
|
78
|
-
}
|
|
79
|
-
if (value.choices[0].delta.tool_calls[0].function.arguments) {
|
|
80
|
-
functionCallArguments += value.choices[0].delta.tool_calls[0].function.arguments;
|
|
81
|
-
}
|
|
82
|
-
if (value.choices[0].delta.tool_calls[0].function.scope) {
|
|
83
|
-
functionCallScope = value.choices[0].delta.tool_calls[0].function.scope;
|
|
84
|
-
}
|
|
85
|
-
controller.enqueue({
|
|
86
|
-
type: "partial",
|
|
87
|
-
name: functionCallName,
|
|
88
|
-
arguments: functionCallArguments
|
|
89
|
-
});
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
} catch (error) {
|
|
93
|
-
controller.error(error);
|
|
94
|
-
await cleanup(controller);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
cancel() {
|
|
100
|
-
reader.cancel();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
decodeChatCompletion
|
|
3
|
+
} from "../chunk-CBF2BIA7.mjs";
|
|
104
4
|
export {
|
|
105
5
|
decodeChatCompletion
|
|
106
6
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, decodeResult, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils.js';
|
|
2
|
-
export { ChatCompletionChunk, parseChatCompletion } from './parse-chat-completion.js';
|
|
2
|
+
export { ChatCompletionChunk, ToolCallFunctionCall, ToolCallPayload, parseChatCompletion } from './parse-chat-completion.js';
|
|
3
3
|
export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, ChatCompletionResultEvent, decodeChatCompletion } from './decode-chat-completion.js';
|
|
4
4
|
export { decodeChatCompletionAsText } from './decode-chat-completion-as-text.js';
|
|
5
5
|
export { actionToChatCompletionFunction, annotatedFunctionToAction, annotatedFunctionToChatCompletionFunction } from './annotated-function.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -287,19 +287,24 @@ function decodeChatCompletion(stream) {
|
|
|
287
287
|
try {
|
|
288
288
|
const { done, value } = await reader.read();
|
|
289
289
|
if (done) {
|
|
290
|
-
if (mode === "function") {
|
|
290
|
+
if ((mode == null ? void 0 : mode.type) === "function") {
|
|
291
291
|
flushFunctionCall();
|
|
292
292
|
}
|
|
293
293
|
await cleanup(controller);
|
|
294
294
|
return;
|
|
295
295
|
}
|
|
296
|
-
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))) {
|
|
296
|
+
if ((mode == null ? void 0 : mode.type) === "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))) {
|
|
297
297
|
if (!flushFunctionCall()) {
|
|
298
298
|
return;
|
|
299
299
|
}
|
|
300
300
|
}
|
|
301
|
-
|
|
302
|
-
if (
|
|
301
|
+
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
302
|
+
if (maybeFunctionCall) {
|
|
303
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
304
|
+
} else {
|
|
305
|
+
mode = { type: "message" };
|
|
306
|
+
}
|
|
307
|
+
if (mode.type === "message") {
|
|
303
308
|
if (value.choices[0].delta.role === "function") {
|
|
304
309
|
controller.enqueue({
|
|
305
310
|
type: "result",
|
|
@@ -313,15 +318,18 @@ function decodeChatCompletion(stream) {
|
|
|
313
318
|
});
|
|
314
319
|
}
|
|
315
320
|
continue;
|
|
316
|
-
} else if (mode === "function") {
|
|
317
|
-
|
|
318
|
-
|
|
321
|
+
} else if (mode.type === "function") {
|
|
322
|
+
const maybeFunctionCallName = mode.function.name;
|
|
323
|
+
if (maybeFunctionCallName) {
|
|
324
|
+
functionCallName = maybeFunctionCallName;
|
|
319
325
|
}
|
|
320
|
-
|
|
321
|
-
|
|
326
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
327
|
+
if (maybeFunctionCallArguments) {
|
|
328
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
322
329
|
}
|
|
323
|
-
|
|
324
|
-
|
|
330
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
331
|
+
if (maybeFunctionCallScope) {
|
|
332
|
+
functionCallScope = maybeFunctionCallScope;
|
|
325
333
|
}
|
|
326
334
|
controller.enqueue({
|
|
327
335
|
type: "partial",
|