ai 2.1.20 → 2.1.21
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/dist/index.js +6 -3
- package/dist/index.mjs +6 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -178,20 +178,23 @@ function createEmptyReadableStream() {
|
|
178
178
|
// streams/openai-stream.ts
|
179
179
|
function parseOpenAIStream() {
|
180
180
|
const trimStartOfStream = trimStartOfStreamHelper();
|
181
|
+
let isFunctionStreamingIn;
|
181
182
|
return (data) => {
|
182
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
183
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
183
184
|
const json = JSON.parse(data);
|
184
185
|
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) {
|
186
|
+
isFunctionStreamingIn = true;
|
185
187
|
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`;
|
186
188
|
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) {
|
187
189
|
const argumentChunk = json.choices[0].delta.function_call.arguments;
|
188
190
|
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
189
191
|
return `${escapedPartialJson}`;
|
190
|
-
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") {
|
192
|
+
} else if ((((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call" || ((_j = json.choices[0]) == null ? void 0 : _j.finish_reason) === "stop") && isFunctionStreamingIn) {
|
193
|
+
isFunctionStreamingIn = false;
|
191
194
|
return '"}}';
|
192
195
|
}
|
193
196
|
const text = trimStartOfStream(
|
194
|
-
(
|
197
|
+
(_o = (_n = (_l = (_k = json.choices[0]) == null ? void 0 : _k.delta) == null ? void 0 : _l.content) != null ? _n : (_m = json.choices[0]) == null ? void 0 : _m.text) != null ? _o : ""
|
195
198
|
);
|
196
199
|
return text;
|
197
200
|
};
|
package/dist/index.mjs
CHANGED
@@ -145,20 +145,23 @@ function createEmptyReadableStream() {
|
|
145
145
|
// streams/openai-stream.ts
|
146
146
|
function parseOpenAIStream() {
|
147
147
|
const trimStartOfStream = trimStartOfStreamHelper();
|
148
|
+
let isFunctionStreamingIn;
|
148
149
|
return (data) => {
|
149
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
150
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
150
151
|
const json = JSON.parse(data);
|
151
152
|
if ((_c = (_b = (_a = json.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.function_call) == null ? void 0 : _c.name) {
|
153
|
+
isFunctionStreamingIn = true;
|
152
154
|
return `{"function_call": {"name": "${(_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.function_call.name}", "arguments": "`;
|
153
155
|
} else if ((_h = (_g = (_f = json.choices[0]) == null ? void 0 : _f.delta) == null ? void 0 : _g.function_call) == null ? void 0 : _h.arguments) {
|
154
156
|
const argumentChunk = json.choices[0].delta.function_call.arguments;
|
155
157
|
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
156
158
|
return `${escapedPartialJson}`;
|
157
|
-
} else if (((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call") {
|
159
|
+
} else if ((((_i = json.choices[0]) == null ? void 0 : _i.finish_reason) === "function_call" || ((_j = json.choices[0]) == null ? void 0 : _j.finish_reason) === "stop") && isFunctionStreamingIn) {
|
160
|
+
isFunctionStreamingIn = false;
|
158
161
|
return '"}}';
|
159
162
|
}
|
160
163
|
const text = trimStartOfStream(
|
161
|
-
(
|
164
|
+
(_o = (_n = (_l = (_k = json.choices[0]) == null ? void 0 : _k.delta) == null ? void 0 : _l.content) != null ? _n : (_m = json.choices[0]) == null ? void 0 : _m.text) != null ? _o : ""
|
162
165
|
);
|
163
166
|
return text;
|
164
167
|
};
|