ai 2.1.19 → 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.d.ts CHANGED
@@ -268,7 +268,7 @@ type OpenAIStreamCallbacks = AIStreamCallbacks & {
268
268
  * // ... run your custom logic here
269
269
  * const result = await myFunction(functionCallPayload)
270
270
  *
271
- * // Ask for another completion
271
+ * // Ask for another completion, or return a string to send to the client as an assistant message.
272
272
  * return await openai.createChatCompletion({
273
273
  * model: 'gpt-3.5-turbo-0613',
274
274
  * stream: true,
@@ -280,7 +280,7 @@ type OpenAIStreamCallbacks = AIStreamCallbacks & {
280
280
  * })
281
281
  * ```
282
282
  */
283
- experimental_onFunctionCall?: (functionCallPayload: FunctionCallPayload, createFunctionCallMessages: (functionCallResult: JSONValue) => CreateMessage[]) => Promise<Response | undefined>;
283
+ experimental_onFunctionCall?: (functionCallPayload: FunctionCallPayload, createFunctionCallMessages: (functionCallResult: JSONValue) => CreateMessage[]) => Promise<Response | undefined | void | string>;
284
284
  };
285
285
  declare function OpenAIStream(res: Response, callbacks?: OpenAIStreamCallbacks): ReadableStream;
286
286
 
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
- (_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : ""
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
  };
@@ -265,6 +268,9 @@ function createFunctionCallTransformer(callbacks) {
265
268
  if (!functionResponse) {
266
269
  controller.enqueue(textEncoder.encode(aggregatedResponse));
267
270
  return;
271
+ } else if (typeof functionResponse === "string") {
272
+ controller.enqueue(textEncoder.encode(functionResponse));
273
+ return;
268
274
  }
269
275
  const openAIStream = OpenAIStream(functionResponse, __spreadProps(__spreadValues({}, callbacks), {
270
276
  [__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
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
- (_n = (_m = (_k = (_j = json.choices[0]) == null ? void 0 : _j.delta) == null ? void 0 : _k.content) != null ? _m : (_l = json.choices[0]) == null ? void 0 : _l.text) != null ? _n : ""
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
  };
@@ -232,6 +235,9 @@ function createFunctionCallTransformer(callbacks) {
232
235
  if (!functionResponse) {
233
236
  controller.enqueue(textEncoder.encode(aggregatedResponse));
234
237
  return;
238
+ } else if (typeof functionResponse === "string") {
239
+ controller.enqueue(textEncoder.encode(functionResponse));
240
+ return;
235
241
  }
236
242
  const openAIStream = OpenAIStream(functionResponse, __spreadProps(__spreadValues({}, callbacks), {
237
243
  [__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.1.19",
3
+ "version": "2.1.21",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",