@copilotkit/shared 0.9.0-function-calling-fixes.1 → 0.9.0-function-calling-fixes.2
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 +63 -59
- package/CHANGELOG.md +6 -0
- package/dist/constants/copilot-protocol.mjs +2 -3
- package/dist/constants/copilot-protocol.mjs.map +1 -1
- package/dist/constants/index.mjs +2 -4
- package/dist/constants/index.mjs.map +1 -1
- package/dist/index.mjs +497 -34
- package/dist/index.mjs.map +1 -1
- package/dist/types/action.mjs +0 -1
- package/dist/types/annotated-function.mjs +0 -1
- package/dist/types/index.mjs +0 -4
- package/dist/types/openai-assistant.mjs +0 -1
- package/dist/utils/annotated-function.mjs +153 -5
- package/dist/utils/annotated-function.mjs.map +1 -1
- package/dist/utils/decode-chat-completion-as-text.mjs +27 -3
- package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
- package/dist/utils/decode-chat-completion.mjs +111 -3
- package/dist/utils/decode-chat-completion.mjs.map +1 -1
- package/dist/utils/index.mjs +494 -26
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/parse-chat-completion.mjs +55 -3
- package/dist/utils/parse-chat-completion.mjs.map +1 -1
- package/dist/utils/utils.mjs +144 -11
- package/dist/utils/utils.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -1,8 +1,156 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
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
|
+
}
|
|
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
|
+
}
|
|
6
154
|
export {
|
|
7
155
|
actionToChatCompletionFunction,
|
|
8
156
|
annotatedFunctionToAction,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/annotated-function.ts"],"sourcesContent":["import { Action, AnnotatedFunction, ToolDefinition, Parameter } 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\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\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;AA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,SAAS,WAAW;AAC9C,iBAAO;AAAA,QACT,WAAW,WAAW,MAAM,SAAS,UAAU;AAC7C,iBAAO;AAAA,QACT,OAAO;AACL,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,UACL,MAAM,WAAW;AAAA,UACjB,aAAa,WAAW;AAAA,UACxB;AAAA,UACA,UAAU,WAAW;AAAA,QACvB;AAAA,IACJ;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B;AAAA,IACA,SAAS,CAAC,SAAS;AACjB,YAAM,uBAA8B,CAAC;AACrC,eAAS,OAAO,kBAAkB,qBAAqB;AACrD,6BAAqB,KAAK,KAAK,IAAI,IAAI,CAAC;AAAA,MAC1C;AACA,aAAO,kBAAkB,eAAe,GAAG,oBAAoB;AAAA,IACjE;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1,6 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
}
|
|
4
28
|
export {
|
|
5
29
|
decodeChatCompletionAsText
|
|
6
30
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
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,6 +1,114 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/utils/decode-chat-completion.ts
|
|
2
|
+
function decodeChatCompletion(stream) {
|
|
3
|
+
const reader = stream.getReader();
|
|
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 == null ? void 0 : mode.type) === "function") {
|
|
50
|
+
flushFunctionCall();
|
|
51
|
+
}
|
|
52
|
+
await cleanup(controller);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
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))) {
|
|
56
|
+
if (!flushFunctionCall()) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
61
|
+
if (maybeFunctionCall) {
|
|
62
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
63
|
+
} else {
|
|
64
|
+
mode = { type: "message" };
|
|
65
|
+
}
|
|
66
|
+
if (mode.type === "message") {
|
|
67
|
+
if (value.choices[0].delta.role === "function") {
|
|
68
|
+
controller.enqueue({
|
|
69
|
+
type: "result",
|
|
70
|
+
content: value.choices[0].delta.content,
|
|
71
|
+
name: value.choices[0].delta.name
|
|
72
|
+
});
|
|
73
|
+
} else if (value.choices[0].delta.content) {
|
|
74
|
+
controller.enqueue({
|
|
75
|
+
type: "content",
|
|
76
|
+
content: value.choices[0].delta.content
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
continue;
|
|
80
|
+
} else if (mode.type === "function") {
|
|
81
|
+
const maybeFunctionCallName = mode.function.name;
|
|
82
|
+
if (maybeFunctionCallName) {
|
|
83
|
+
functionCallName = maybeFunctionCallName;
|
|
84
|
+
}
|
|
85
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
86
|
+
if (maybeFunctionCallArguments) {
|
|
87
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
88
|
+
}
|
|
89
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
90
|
+
if (maybeFunctionCallScope) {
|
|
91
|
+
functionCallScope = maybeFunctionCallScope;
|
|
92
|
+
}
|
|
93
|
+
controller.enqueue({
|
|
94
|
+
type: "partial",
|
|
95
|
+
name: functionCallName,
|
|
96
|
+
arguments: functionCallArguments
|
|
97
|
+
});
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
controller.error(error);
|
|
102
|
+
await cleanup(controller);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
cancel() {
|
|
108
|
+
reader.cancel();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
4
112
|
export {
|
|
5
113
|
decodeChatCompletion
|
|
6
114
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
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":";AAgCO,SAAS,qBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAIhC,MAAI,OAAoB;AACxB,MAAI,mBAA2B;AAC/B,MAAI,wBAAg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