ai 3.4.33 → 4.0.0-canary.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.
package/dist/index.mjs CHANGED
@@ -5120,19 +5120,12 @@ function createCallbacksTransformer(cb) {
5120
5120
  }
5121
5121
  },
5122
5122
  async flush() {
5123
- const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
5124
5123
  if (callbacks.onCompletion) {
5125
5124
  await callbacks.onCompletion(aggregatedResponse);
5126
5125
  }
5127
- if (callbacks.onFinal && !isOpenAICallbacks) {
5128
- await callbacks.onFinal(aggregatedResponse);
5129
- }
5130
5126
  }
5131
5127
  });
5132
5128
  }
5133
- function isOfTypeOpenAIStreamCallbacks(callbacks) {
5134
- return "experimental_onFunctionCall" in callbacks;
5135
- }
5136
5129
  function trimStartOfStreamHelper() {
5137
5130
  let isStreamStart = true;
5138
5131
  return (text) => {
@@ -5192,136 +5185,9 @@ function readableFromAsyncIterable(iterable) {
5192
5185
  });
5193
5186
  }
5194
5187
 
5195
- // streams/stream-data.ts
5196
- import { formatStreamPart as formatStreamPart2 } from "@ai-sdk/ui-utils";
5197
-
5198
- // util/constants.ts
5199
- var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
5200
-
5201
- // streams/stream-data.ts
5202
- var StreamData2 = class {
5203
- constructor() {
5204
- this.encoder = new TextEncoder();
5205
- this.controller = null;
5206
- this.isClosed = false;
5207
- this.warningTimeout = null;
5208
- const self = this;
5209
- this.stream = new ReadableStream({
5210
- start: async (controller) => {
5211
- self.controller = controller;
5212
- if (process.env.NODE_ENV === "development") {
5213
- self.warningTimeout = setTimeout(() => {
5214
- console.warn(
5215
- "The data stream is hanging. Did you forget to close it with `data.close()`?"
5216
- );
5217
- }, HANGING_STREAM_WARNING_TIME_MS);
5218
- }
5219
- },
5220
- pull: (controller) => {
5221
- },
5222
- cancel: (reason) => {
5223
- this.isClosed = true;
5224
- }
5225
- });
5226
- }
5227
- async close() {
5228
- if (this.isClosed) {
5229
- throw new Error("Data Stream has already been closed.");
5230
- }
5231
- if (!this.controller) {
5232
- throw new Error("Stream controller is not initialized.");
5233
- }
5234
- this.controller.close();
5235
- this.isClosed = true;
5236
- if (this.warningTimeout) {
5237
- clearTimeout(this.warningTimeout);
5238
- }
5239
- }
5240
- append(value) {
5241
- if (this.isClosed) {
5242
- throw new Error("Data Stream has already been closed.");
5243
- }
5244
- if (!this.controller) {
5245
- throw new Error("Stream controller is not initialized.");
5246
- }
5247
- this.controller.enqueue(
5248
- this.encoder.encode(formatStreamPart2("data", [value]))
5249
- );
5250
- }
5251
- appendMessageAnnotation(value) {
5252
- if (this.isClosed) {
5253
- throw new Error("Data Stream has already been closed.");
5254
- }
5255
- if (!this.controller) {
5256
- throw new Error("Stream controller is not initialized.");
5257
- }
5258
- this.controller.enqueue(
5259
- this.encoder.encode(formatStreamPart2("message_annotations", [value]))
5260
- );
5261
- }
5262
- };
5263
- function createStreamDataTransformer() {
5264
- const encoder = new TextEncoder();
5265
- const decoder = new TextDecoder();
5266
- return new TransformStream({
5267
- transform: async (chunk, controller) => {
5268
- const message = decoder.decode(chunk);
5269
- controller.enqueue(encoder.encode(formatStreamPart2("text", message)));
5270
- }
5271
- });
5272
- }
5273
- var experimental_StreamData = class extends StreamData2 {
5274
- };
5275
-
5276
- // streams/anthropic-stream.ts
5277
- function parseAnthropicStream() {
5278
- let previous = "";
5279
- return (data) => {
5280
- const json = JSON.parse(data);
5281
- if ("error" in json) {
5282
- throw new Error(`${json.error.type}: ${json.error.message}`);
5283
- }
5284
- if (!("completion" in json)) {
5285
- return;
5286
- }
5287
- const text = json.completion;
5288
- if (!previous || text.length > previous.length && text.startsWith(previous)) {
5289
- const delta = text.slice(previous.length);
5290
- previous = text;
5291
- return delta;
5292
- }
5293
- return text;
5294
- };
5295
- }
5296
- async function* streamable(stream) {
5297
- for await (const chunk of stream) {
5298
- if ("completion" in chunk) {
5299
- const text = chunk.completion;
5300
- if (text)
5301
- yield text;
5302
- } else if ("delta" in chunk) {
5303
- const { delta } = chunk;
5304
- if ("text" in delta) {
5305
- const text = delta.text;
5306
- if (text)
5307
- yield text;
5308
- }
5309
- }
5310
- }
5311
- }
5312
- function AnthropicStream(res, cb) {
5313
- if (Symbol.asyncIterator in res) {
5314
- return readableFromAsyncIterable(streamable(res)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
5315
- } else {
5316
- return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
5317
- createStreamDataTransformer()
5318
- );
5319
- }
5320
- }
5321
-
5322
5188
  // streams/assistant-response.ts
5323
5189
  import {
5324
- formatStreamPart as formatStreamPart3
5190
+ formatStreamPart as formatStreamPart2
5325
5191
  } from "@ai-sdk/ui-utils";
5326
5192
  function AssistantResponse({ threadId, messageId }, process2) {
5327
5193
  const stream = new ReadableStream({
@@ -5330,17 +5196,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
5330
5196
  const textEncoder = new TextEncoder();
5331
5197
  const sendMessage = (message) => {
5332
5198
  controller.enqueue(
5333
- textEncoder.encode(formatStreamPart3("assistant_message", message))
5199
+ textEncoder.encode(formatStreamPart2("assistant_message", message))
5334
5200
  );
5335
5201
  };
5336
5202
  const sendDataMessage = (message) => {
5337
5203
  controller.enqueue(
5338
- textEncoder.encode(formatStreamPart3("data_message", message))
5204
+ textEncoder.encode(formatStreamPart2("data_message", message))
5339
5205
  );
5340
5206
  };
5341
5207
  const sendError = (errorMessage) => {
5342
5208
  controller.enqueue(
5343
- textEncoder.encode(formatStreamPart3("error", errorMessage))
5209
+ textEncoder.encode(formatStreamPart2("error", errorMessage))
5344
5210
  );
5345
5211
  };
5346
5212
  const forwardStream = async (stream2) => {
@@ -5351,7 +5217,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5351
5217
  case "thread.message.created": {
5352
5218
  controller.enqueue(
5353
5219
  textEncoder.encode(
5354
- formatStreamPart3("assistant_message", {
5220
+ formatStreamPart2("assistant_message", {
5355
5221
  id: value.data.id,
5356
5222
  role: "assistant",
5357
5223
  content: [{ type: "text", text: { value: "" } }]
@@ -5365,7 +5231,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5365
5231
  if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
5366
5232
  controller.enqueue(
5367
5233
  textEncoder.encode(
5368
- formatStreamPart3("text", content.text.value)
5234
+ formatStreamPart2("text", content.text.value)
5369
5235
  )
5370
5236
  );
5371
5237
  }
@@ -5382,7 +5248,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5382
5248
  };
5383
5249
  controller.enqueue(
5384
5250
  textEncoder.encode(
5385
- formatStreamPart3("assistant_control_data", {
5251
+ formatStreamPart2("assistant_control_data", {
5386
5252
  threadId,
5387
5253
  messageId
5388
5254
  })
@@ -5416,192 +5282,96 @@ function AssistantResponse({ threadId, messageId }, process2) {
5416
5282
  }
5417
5283
  var experimental_AssistantResponse = AssistantResponse;
5418
5284
 
5419
- // streams/aws-bedrock-stream.ts
5420
- async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
5421
- var _a11, _b;
5422
- const decoder = new TextDecoder();
5423
- for await (const chunk of (_a11 = response.body) != null ? _a11 : []) {
5424
- const bytes = (_b = chunk.chunk) == null ? void 0 : _b.bytes;
5425
- if (bytes != null) {
5426
- const chunkText = decoder.decode(bytes);
5427
- const chunkJSON = JSON.parse(chunkText);
5428
- const delta = extractTextDeltaFromChunk(chunkJSON);
5429
- if (delta != null) {
5430
- yield delta;
5285
+ // streams/langchain-adapter.ts
5286
+ var langchain_adapter_exports = {};
5287
+ __export(langchain_adapter_exports, {
5288
+ toAIStream: () => toAIStream,
5289
+ toDataStream: () => toDataStream,
5290
+ toDataStreamResponse: () => toDataStreamResponse
5291
+ });
5292
+
5293
+ // streams/stream-data.ts
5294
+ import { formatStreamPart as formatStreamPart3 } from "@ai-sdk/ui-utils";
5295
+
5296
+ // util/constants.ts
5297
+ var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
5298
+
5299
+ // streams/stream-data.ts
5300
+ var StreamData2 = class {
5301
+ constructor() {
5302
+ this.encoder = new TextEncoder();
5303
+ this.controller = null;
5304
+ this.isClosed = false;
5305
+ this.warningTimeout = null;
5306
+ const self = this;
5307
+ this.stream = new ReadableStream({
5308
+ start: async (controller) => {
5309
+ self.controller = controller;
5310
+ if (process.env.NODE_ENV === "development") {
5311
+ self.warningTimeout = setTimeout(() => {
5312
+ console.warn(
5313
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
5314
+ );
5315
+ }, HANGING_STREAM_WARNING_TIME_MS);
5316
+ }
5317
+ },
5318
+ pull: (controller) => {
5319
+ },
5320
+ cancel: (reason) => {
5321
+ this.isClosed = true;
5431
5322
  }
5432
- }
5323
+ });
5433
5324
  }
5434
- }
5435
- function AWSBedrockAnthropicMessagesStream(response, callbacks) {
5436
- return AWSBedrockStream(response, callbacks, (chunk) => {
5437
- var _a11;
5438
- return (_a11 = chunk.delta) == null ? void 0 : _a11.text;
5439
- });
5440
- }
5441
- function AWSBedrockAnthropicStream(response, callbacks) {
5442
- return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
5443
- }
5444
- function AWSBedrockCohereStream(response, callbacks) {
5445
- return AWSBedrockStream(response, callbacks, (chunk) => chunk == null ? void 0 : chunk.text);
5446
- }
5447
- function AWSBedrockLlama2Stream(response, callbacks) {
5448
- return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
5449
- }
5450
- function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
5451
- return readableFromAsyncIterable(
5452
- asDeltaIterable(response, extractTextDeltaFromChunk)
5453
- ).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
5454
- }
5455
-
5456
- // streams/cohere-stream.ts
5457
- var utf8Decoder = new TextDecoder("utf-8");
5458
- async function processLines(lines, controller) {
5459
- for (const line of lines) {
5460
- const { text, is_finished } = JSON.parse(line);
5461
- if (!is_finished) {
5462
- controller.enqueue(text);
5325
+ async close() {
5326
+ if (this.isClosed) {
5327
+ throw new Error("Data Stream has already been closed.");
5463
5328
  }
5464
- }
5465
- }
5466
- async function readAndProcessLines(reader, controller) {
5467
- let segment = "";
5468
- while (true) {
5469
- const { value: chunk, done } = await reader.read();
5470
- if (done) {
5471
- break;
5329
+ if (!this.controller) {
5330
+ throw new Error("Stream controller is not initialized.");
5331
+ }
5332
+ this.controller.close();
5333
+ this.isClosed = true;
5334
+ if (this.warningTimeout) {
5335
+ clearTimeout(this.warningTimeout);
5472
5336
  }
5473
- segment += utf8Decoder.decode(chunk, { stream: true });
5474
- const linesArray = segment.split(/\r\n|\n|\r/g);
5475
- segment = linesArray.pop() || "";
5476
- await processLines(linesArray, controller);
5477
- }
5478
- if (segment) {
5479
- const linesArray = [segment];
5480
- await processLines(linesArray, controller);
5481
5337
  }
5482
- controller.close();
5483
- }
5484
- function createParser2(res) {
5485
- var _a11;
5486
- const reader = (_a11 = res.body) == null ? void 0 : _a11.getReader();
5487
- return new ReadableStream({
5488
- async start(controller) {
5489
- if (!reader) {
5490
- controller.close();
5491
- return;
5492
- }
5493
- await readAndProcessLines(reader, controller);
5338
+ append(value) {
5339
+ if (this.isClosed) {
5340
+ throw new Error("Data Stream has already been closed.");
5494
5341
  }
5495
- });
5496
- }
5497
- async function* streamable2(stream) {
5498
- for await (const chunk of stream) {
5499
- if (chunk.eventType === "text-generation") {
5500
- const text = chunk.text;
5501
- if (text)
5502
- yield text;
5342
+ if (!this.controller) {
5343
+ throw new Error("Stream controller is not initialized.");
5503
5344
  }
5345
+ this.controller.enqueue(
5346
+ this.encoder.encode(formatStreamPart3("data", [value]))
5347
+ );
5504
5348
  }
5505
- }
5506
- function CohereStream(reader, callbacks) {
5507
- if (Symbol.asyncIterator in reader) {
5508
- return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
5509
- } else {
5510
- return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
5511
- }
5512
- }
5513
-
5514
- // streams/google-generative-ai-stream.ts
5515
- async function* streamable3(response) {
5516
- var _a11, _b, _c;
5517
- for await (const chunk of response.stream) {
5518
- const parts = (_c = (_b = (_a11 = chunk.candidates) == null ? void 0 : _a11[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
5519
- if (parts === void 0) {
5520
- continue;
5349
+ appendMessageAnnotation(value) {
5350
+ if (this.isClosed) {
5351
+ throw new Error("Data Stream has already been closed.");
5521
5352
  }
5522
- const firstPart = parts[0];
5523
- if (typeof firstPart.text === "string") {
5524
- yield firstPart.text;
5353
+ if (!this.controller) {
5354
+ throw new Error("Stream controller is not initialized.");
5525
5355
  }
5356
+ this.controller.enqueue(
5357
+ this.encoder.encode(formatStreamPart3("message_annotations", [value]))
5358
+ );
5526
5359
  }
5527
- }
5528
- function GoogleGenerativeAIStream(response, cb) {
5529
- return readableFromAsyncIterable(streamable3(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
5530
- }
5531
-
5532
- // streams/huggingface-stream.ts
5533
- function createParser3(res) {
5534
- const trimStartOfStream = trimStartOfStreamHelper();
5535
- return new ReadableStream({
5536
- async pull(controller) {
5537
- var _a11, _b;
5538
- const { value, done } = await res.next();
5539
- if (done) {
5540
- controller.close();
5541
- return;
5542
- }
5543
- const text = trimStartOfStream((_b = (_a11 = value.token) == null ? void 0 : _a11.text) != null ? _b : "");
5544
- if (!text)
5545
- return;
5546
- if (value.generated_text != null && value.generated_text.length > 0) {
5547
- return;
5548
- }
5549
- if (text === "</s>" || text === "<|endoftext|>" || text === "<|end|>") {
5550
- return;
5551
- }
5552
- controller.enqueue(text);
5360
+ };
5361
+ function createStreamDataTransformer() {
5362
+ const encoder = new TextEncoder();
5363
+ const decoder = new TextDecoder();
5364
+ return new TransformStream({
5365
+ transform: async (chunk, controller) => {
5366
+ const message = decoder.decode(chunk);
5367
+ controller.enqueue(encoder.encode(formatStreamPart3("text", message)));
5553
5368
  }
5554
5369
  });
5555
5370
  }
5556
- function HuggingFaceStream(res, callbacks) {
5557
- return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
5558
- }
5559
-
5560
- // streams/inkeep-stream.ts
5561
- function InkeepStream(res, callbacks) {
5562
- if (!res.body) {
5563
- throw new Error("Response body is null");
5564
- }
5565
- let chat_session_id = "";
5566
- let records_cited;
5567
- const inkeepEventParser = (data, options) => {
5568
- var _a11, _b;
5569
- const { event } = options;
5570
- if (event === "records_cited") {
5571
- records_cited = JSON.parse(data);
5572
- (_a11 = callbacks == null ? void 0 : callbacks.onRecordsCited) == null ? void 0 : _a11.call(callbacks, records_cited);
5573
- }
5574
- if (event === "message_chunk") {
5575
- const inkeepMessageChunk = JSON.parse(data);
5576
- chat_session_id = (_b = inkeepMessageChunk.chat_session_id) != null ? _b : chat_session_id;
5577
- return inkeepMessageChunk.content_chunk;
5578
- }
5579
- return;
5580
- };
5581
- let { onRecordsCited, ...passThroughCallbacks } = callbacks || {};
5582
- passThroughCallbacks = {
5583
- ...passThroughCallbacks,
5584
- onFinal: (completion) => {
5585
- var _a11;
5586
- const inkeepOnFinalMetadata = {
5587
- chat_session_id,
5588
- records_cited
5589
- };
5590
- (_a11 = callbacks == null ? void 0 : callbacks.onFinal) == null ? void 0 : _a11.call(callbacks, completion, inkeepOnFinalMetadata);
5591
- }
5592
- };
5593
- return AIStream(res, inkeepEventParser, passThroughCallbacks).pipeThrough(
5594
- createStreamDataTransformer()
5595
- );
5596
- }
5371
+ var experimental_StreamData = class extends StreamData2 {
5372
+ };
5597
5373
 
5598
5374
  // streams/langchain-adapter.ts
5599
- var langchain_adapter_exports = {};
5600
- __export(langchain_adapter_exports, {
5601
- toAIStream: () => toAIStream,
5602
- toDataStream: () => toDataStream,
5603
- toDataStreamResponse: () => toDataStreamResponse
5604
- });
5605
5375
  function toAIStream(stream, callbacks) {
5606
5376
  return toDataStream(stream, callbacks);
5607
5377
  }
@@ -5698,425 +5468,6 @@ function toReadableStream(res) {
5698
5468
  });
5699
5469
  }
5700
5470
 
5701
- // streams/langchain-stream.ts
5702
- function LangChainStream(callbacks) {
5703
- const stream = new TransformStream();
5704
- const writer = stream.writable.getWriter();
5705
- const runs = /* @__PURE__ */ new Set();
5706
- const handleError = async (e, runId) => {
5707
- runs.delete(runId);
5708
- await writer.ready;
5709
- await writer.abort(e);
5710
- };
5711
- const handleStart = async (runId) => {
5712
- runs.add(runId);
5713
- };
5714
- const handleEnd = async (runId) => {
5715
- runs.delete(runId);
5716
- if (runs.size === 0) {
5717
- await writer.ready;
5718
- await writer.close();
5719
- }
5720
- };
5721
- return {
5722
- stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer()),
5723
- writer,
5724
- handlers: {
5725
- handleLLMNewToken: async (token) => {
5726
- await writer.ready;
5727
- await writer.write(token);
5728
- },
5729
- handleLLMStart: async (_llm, _prompts, runId) => {
5730
- handleStart(runId);
5731
- },
5732
- handleLLMEnd: async (_output, runId) => {
5733
- await handleEnd(runId);
5734
- },
5735
- handleLLMError: async (e, runId) => {
5736
- await handleError(e, runId);
5737
- },
5738
- handleChainStart: async (_chain, _inputs, runId) => {
5739
- handleStart(runId);
5740
- },
5741
- handleChainEnd: async (_outputs, runId) => {
5742
- await handleEnd(runId);
5743
- },
5744
- handleChainError: async (e, runId) => {
5745
- await handleError(e, runId);
5746
- },
5747
- handleToolStart: async (_tool, _input, runId) => {
5748
- handleStart(runId);
5749
- },
5750
- handleToolEnd: async (_output, runId) => {
5751
- await handleEnd(runId);
5752
- },
5753
- handleToolError: async (e, runId) => {
5754
- await handleError(e, runId);
5755
- }
5756
- }
5757
- };
5758
- }
5759
-
5760
- // streams/mistral-stream.ts
5761
- async function* streamable4(stream) {
5762
- var _a11, _b;
5763
- for await (const chunk of stream) {
5764
- const content = (_b = (_a11 = chunk.choices[0]) == null ? void 0 : _a11.delta) == null ? void 0 : _b.content;
5765
- if (content === void 0 || content === "") {
5766
- continue;
5767
- }
5768
- yield content;
5769
- }
5770
- }
5771
- function MistralStream(response, callbacks) {
5772
- const stream = readableFromAsyncIterable(streamable4(response));
5773
- return stream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
5774
- }
5775
-
5776
- // streams/openai-stream.ts
5777
- import {
5778
- createChunkDecoder,
5779
- formatStreamPart as formatStreamPart4
5780
- } from "@ai-sdk/ui-utils";
5781
- function parseOpenAIStream() {
5782
- const extract = chunkToText();
5783
- return (data) => extract(JSON.parse(data));
5784
- }
5785
- async function* streamable5(stream) {
5786
- const extract = chunkToText();
5787
- for await (let chunk of stream) {
5788
- if ("promptFilterResults" in chunk) {
5789
- chunk = {
5790
- id: chunk.id,
5791
- created: chunk.created.getDate(),
5792
- object: chunk.object,
5793
- // not exposed by Azure API
5794
- model: chunk.model,
5795
- // not exposed by Azure API
5796
- choices: chunk.choices.map((choice) => {
5797
- var _a11, _b, _c, _d, _e, _f, _g;
5798
- return {
5799
- delta: {
5800
- content: (_a11 = choice.delta) == null ? void 0 : _a11.content,
5801
- function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
5802
- role: (_c = choice.delta) == null ? void 0 : _c.role,
5803
- tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
5804
- index,
5805
- id: toolCall.id,
5806
- function: toolCall.function,
5807
- type: toolCall.type
5808
- })) : void 0
5809
- },
5810
- finish_reason: choice.finishReason,
5811
- index: choice.index
5812
- };
5813
- })
5814
- };
5815
- }
5816
- const text = extract(chunk);
5817
- if (text)
5818
- yield text;
5819
- }
5820
- }
5821
- function chunkToText() {
5822
- const trimStartOfStream = trimStartOfStreamHelper();
5823
- let isFunctionStreamingIn;
5824
- return (json) => {
5825
- var _a11, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
5826
- if (isChatCompletionChunk(json)) {
5827
- const delta = (_a11 = json.choices[0]) == null ? void 0 : _a11.delta;
5828
- if ((_b = delta.function_call) == null ? void 0 : _b.name) {
5829
- isFunctionStreamingIn = true;
5830
- return {
5831
- isText: false,
5832
- content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
5833
- };
5834
- } else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
5835
- isFunctionStreamingIn = true;
5836
- const toolCall = delta.tool_calls[0];
5837
- if (toolCall.index === 0) {
5838
- return {
5839
- isText: false,
5840
- content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
5841
- };
5842
- } else {
5843
- return {
5844
- isText: false,
5845
- content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
5846
- };
5847
- }
5848
- } else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
5849
- return {
5850
- isText: false,
5851
- content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
5852
- };
5853
- } else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
5854
- return {
5855
- isText: false,
5856
- content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
5857
- };
5858
- } else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
5859
- isFunctionStreamingIn = false;
5860
- return {
5861
- isText: false,
5862
- content: '"}}'
5863
- };
5864
- } else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
5865
- isFunctionStreamingIn = false;
5866
- return {
5867
- isText: false,
5868
- content: '"}}]}'
5869
- };
5870
- }
5871
- }
5872
- const text = trimStartOfStream(
5873
- isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
5874
- );
5875
- return text;
5876
- };
5877
- function cleanupArguments(argumentChunk) {
5878
- 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");
5879
- return `${escapedPartialJson}`;
5880
- }
5881
- }
5882
- var __internal__OpenAIFnMessagesSymbol = Symbol(
5883
- "internal_openai_fn_messages"
5884
- );
5885
- function isChatCompletionChunk(data) {
5886
- return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
5887
- }
5888
- function isCompletion(data) {
5889
- return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
5890
- }
5891
- function OpenAIStream(res, callbacks) {
5892
- const cb = callbacks;
5893
- let stream;
5894
- if (Symbol.asyncIterator in res) {
5895
- stream = readableFromAsyncIterable(streamable5(res)).pipeThrough(
5896
- createCallbacksTransformer(
5897
- (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
5898
- ...cb,
5899
- onFinal: void 0
5900
- } : {
5901
- ...cb
5902
- }
5903
- )
5904
- );
5905
- } else {
5906
- stream = AIStream(
5907
- res,
5908
- parseOpenAIStream(),
5909
- (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
5910
- ...cb,
5911
- onFinal: void 0
5912
- } : {
5913
- ...cb
5914
- }
5915
- );
5916
- }
5917
- if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
5918
- const functionCallTransformer = createFunctionCallTransformer(cb);
5919
- return stream.pipeThrough(functionCallTransformer);
5920
- } else {
5921
- return stream.pipeThrough(createStreamDataTransformer());
5922
- }
5923
- }
5924
- function createFunctionCallTransformer(callbacks) {
5925
- const textEncoder = new TextEncoder();
5926
- let isFirstChunk = true;
5927
- let aggregatedResponse = "";
5928
- let aggregatedFinalCompletionResponse = "";
5929
- let isFunctionStreamingIn = false;
5930
- let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
5931
- const decode = createChunkDecoder();
5932
- return new TransformStream({
5933
- async transform(chunk, controller) {
5934
- const message = decode(chunk);
5935
- aggregatedFinalCompletionResponse += message;
5936
- const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
5937
- if (shouldHandleAsFunction) {
5938
- isFunctionStreamingIn = true;
5939
- aggregatedResponse += message;
5940
- isFirstChunk = false;
5941
- return;
5942
- }
5943
- if (!isFunctionStreamingIn) {
5944
- controller.enqueue(
5945
- textEncoder.encode(formatStreamPart4("text", message))
5946
- );
5947
- return;
5948
- } else {
5949
- aggregatedResponse += message;
5950
- }
5951
- },
5952
- async flush(controller) {
5953
- try {
5954
- if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
5955
- isFunctionStreamingIn = false;
5956
- const payload = JSON.parse(aggregatedResponse);
5957
- let newFunctionCallMessages = [
5958
- ...functionCallMessages
5959
- ];
5960
- let functionResponse = void 0;
5961
- if (callbacks.experimental_onFunctionCall) {
5962
- if (payload.function_call === void 0) {
5963
- console.warn(
5964
- "experimental_onFunctionCall should not be defined when using tools"
5965
- );
5966
- }
5967
- const argumentsPayload = JSON.parse(
5968
- payload.function_call.arguments
5969
- );
5970
- functionResponse = await callbacks.experimental_onFunctionCall(
5971
- {
5972
- name: payload.function_call.name,
5973
- arguments: argumentsPayload
5974
- },
5975
- (result) => {
5976
- newFunctionCallMessages = [
5977
- ...functionCallMessages,
5978
- {
5979
- role: "assistant",
5980
- content: "",
5981
- function_call: payload.function_call
5982
- },
5983
- {
5984
- role: "function",
5985
- name: payload.function_call.name,
5986
- content: JSON.stringify(result)
5987
- }
5988
- ];
5989
- return newFunctionCallMessages;
5990
- }
5991
- );
5992
- }
5993
- if (callbacks.experimental_onToolCall) {
5994
- const toolCalls = {
5995
- tools: []
5996
- };
5997
- for (const tool2 of payload.tool_calls) {
5998
- toolCalls.tools.push({
5999
- id: tool2.id,
6000
- type: "function",
6001
- func: {
6002
- name: tool2.function.name,
6003
- arguments: JSON.parse(tool2.function.arguments)
6004
- }
6005
- });
6006
- }
6007
- let responseIndex = 0;
6008
- try {
6009
- functionResponse = await callbacks.experimental_onToolCall(
6010
- toolCalls,
6011
- (result) => {
6012
- if (result) {
6013
- const { tool_call_id, function_name, tool_call_result } = result;
6014
- newFunctionCallMessages = [
6015
- ...newFunctionCallMessages,
6016
- // Only append the assistant message if it's the first response
6017
- ...responseIndex === 0 ? [
6018
- {
6019
- role: "assistant",
6020
- content: "",
6021
- tool_calls: payload.tool_calls.map(
6022
- (tc) => ({
6023
- id: tc.id,
6024
- type: "function",
6025
- function: {
6026
- name: tc.function.name,
6027
- // we send the arguments an object to the user, but as the API expects a string, we need to stringify it
6028
- arguments: JSON.stringify(
6029
- tc.function.arguments
6030
- )
6031
- }
6032
- })
6033
- )
6034
- }
6035
- ] : [],
6036
- // Append the function call result message
6037
- {
6038
- role: "tool",
6039
- tool_call_id,
6040
- name: function_name,
6041
- content: JSON.stringify(tool_call_result)
6042
- }
6043
- ];
6044
- responseIndex++;
6045
- }
6046
- return newFunctionCallMessages;
6047
- }
6048
- );
6049
- } catch (e) {
6050
- console.error("Error calling experimental_onToolCall:", e);
6051
- }
6052
- }
6053
- if (!functionResponse) {
6054
- controller.enqueue(
6055
- textEncoder.encode(
6056
- formatStreamPart4(
6057
- payload.function_call ? "function_call" : "tool_calls",
6058
- // parse to prevent double-encoding:
6059
- JSON.parse(aggregatedResponse)
6060
- )
6061
- )
6062
- );
6063
- return;
6064
- } else if (typeof functionResponse === "string") {
6065
- controller.enqueue(
6066
- textEncoder.encode(formatStreamPart4("text", functionResponse))
6067
- );
6068
- aggregatedFinalCompletionResponse = functionResponse;
6069
- return;
6070
- }
6071
- const filteredCallbacks = {
6072
- ...callbacks,
6073
- onStart: void 0
6074
- };
6075
- callbacks.onFinal = void 0;
6076
- const openAIStream = OpenAIStream(functionResponse, {
6077
- ...filteredCallbacks,
6078
- [__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
6079
- });
6080
- const reader = openAIStream.getReader();
6081
- while (true) {
6082
- const { done, value } = await reader.read();
6083
- if (done) {
6084
- break;
6085
- }
6086
- controller.enqueue(value);
6087
- }
6088
- }
6089
- } finally {
6090
- if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
6091
- await callbacks.onFinal(aggregatedFinalCompletionResponse);
6092
- }
6093
- }
6094
- }
6095
- });
6096
- }
6097
-
6098
- // streams/replicate-stream.ts
6099
- async function ReplicateStream(res, cb, options) {
6100
- var _a11;
6101
- const url = (_a11 = res.urls) == null ? void 0 : _a11.stream;
6102
- if (!url) {
6103
- if (res.error)
6104
- throw new Error(res.error);
6105
- else
6106
- throw new Error("Missing stream URL in Replicate response");
6107
- }
6108
- const eventStream = await fetch(url, {
6109
- method: "GET",
6110
- headers: {
6111
- Accept: "text/event-stream",
6112
- ...options == null ? void 0 : options.headers
6113
- }
6114
- });
6115
- return AIStream(eventStream, void 0, cb).pipeThrough(
6116
- createStreamDataTransformer()
6117
- );
6118
- }
6119
-
6120
5471
  // streams/stream-to-response.ts
6121
5472
  function streamToResponse(res, response, init, data) {
6122
5473
  var _a11;
@@ -6161,24 +5512,13 @@ var StreamingTextResponse = class extends Response {
6161
5512
 
6162
5513
  // streams/index.ts
6163
5514
  var generateId2 = generateIdImpl;
6164
- var nanoid = generateIdImpl;
6165
5515
  export {
6166
5516
  AISDKError10 as AISDKError,
6167
5517
  AIStream,
6168
5518
  APICallError2 as APICallError,
6169
- AWSBedrockAnthropicMessagesStream,
6170
- AWSBedrockAnthropicStream,
6171
- AWSBedrockCohereStream,
6172
- AWSBedrockLlama2Stream,
6173
- AWSBedrockStream,
6174
- AnthropicStream,
6175
5519
  AssistantResponse,
6176
- CohereStream,
6177
5520
  DownloadError,
6178
5521
  EmptyResponseBodyError,
6179
- GoogleGenerativeAIStream,
6180
- HuggingFaceStream,
6181
- InkeepStream,
6182
5522
  InvalidArgumentError,
6183
5523
  InvalidDataContentError,
6184
5524
  InvalidMessageRoleError,
@@ -6187,18 +5527,14 @@ export {
6187
5527
  InvalidToolArgumentsError,
6188
5528
  JSONParseError,
6189
5529
  langchain_adapter_exports as LangChainAdapter,
6190
- LangChainStream,
6191
5530
  llamaindex_adapter_exports as LlamaIndexAdapter,
6192
5531
  LoadAPIKeyError,
6193
5532
  MessageConversionError,
6194
- MistralStream,
6195
5533
  NoContentGeneratedError,
6196
5534
  NoObjectGeneratedError,
6197
5535
  NoSuchModelError,
6198
5536
  NoSuchProviderError,
6199
5537
  NoSuchToolError,
6200
- OpenAIStream,
6201
- ReplicateStream,
6202
5538
  RetryError,
6203
5539
  StreamData2 as StreamData,
6204
5540
  StreamingTextResponse,
@@ -6226,7 +5562,6 @@ export {
6226
5562
  generateObject,
6227
5563
  generateText,
6228
5564
  jsonSchema,
6229
- nanoid,
6230
5565
  parseStreamPart,
6231
5566
  processDataProtocolResponse,
6232
5567
  readDataStream,