ai 4.0.19 → 4.0.20

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
@@ -32,15 +32,19 @@ function createDataStream({
32
32
  controller = controllerArg;
33
33
  }
34
34
  });
35
+ function safeEnqueue(data) {
36
+ try {
37
+ controller.enqueue(data);
38
+ } catch (error) {
39
+ }
40
+ }
35
41
  try {
36
42
  const result = execute({
37
43
  writeData(data) {
38
- controller.enqueue(formatDataStreamPart("data", [data]));
44
+ safeEnqueue(formatDataStreamPart("data", [data]));
39
45
  },
40
46
  writeMessageAnnotation(annotation) {
41
- controller.enqueue(
42
- formatDataStreamPart("message_annotations", [annotation])
43
- );
47
+ safeEnqueue(formatDataStreamPart("message_annotations", [annotation]));
44
48
  },
45
49
  merge(streamArg) {
46
50
  ongoingStreamPromises.push(
@@ -50,10 +54,10 @@ function createDataStream({
50
54
  const { done, value } = await reader.read();
51
55
  if (done)
52
56
  break;
53
- controller.enqueue(value);
57
+ safeEnqueue(value);
54
58
  }
55
59
  })().catch((error) => {
56
- controller.enqueue(formatDataStreamPart("error", onError(error)));
60
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
57
61
  })
58
62
  );
59
63
  },
@@ -62,12 +66,12 @@ function createDataStream({
62
66
  if (result) {
63
67
  ongoingStreamPromises.push(
64
68
  result.catch((error) => {
65
- controller.enqueue(formatDataStreamPart("error", onError(error)));
69
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
66
70
  })
67
71
  );
68
72
  }
69
73
  } catch (error) {
70
- controller.enqueue(formatDataStreamPart("error", onError(error)));
74
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
71
75
  }
72
76
  const waitForStreams = new Promise(async (resolve) => {
73
77
  while (ongoingStreamPromises.length > 0) {
@@ -76,7 +80,10 @@ function createDataStream({
76
80
  resolve();
77
81
  });
78
82
  waitForStreams.finally(() => {
79
- controller.close();
83
+ try {
84
+ controller.close();
85
+ } catch (error) {
86
+ }
80
87
  });
81
88
  return stream;
82
89
  }
@@ -4127,25 +4134,6 @@ function runToolsTransformation({
4127
4134
  break;
4128
4135
  }
4129
4136
  case "tool-call": {
4130
- const toolName = chunk.toolName;
4131
- if (tools == null) {
4132
- toolResultsStreamController.enqueue({
4133
- type: "error",
4134
- error: new NoSuchToolError({ toolName: chunk.toolName })
4135
- });
4136
- break;
4137
- }
4138
- const tool2 = tools[toolName];
4139
- if (tool2 == null) {
4140
- toolResultsStreamController.enqueue({
4141
- type: "error",
4142
- error: new NoSuchToolError({
4143
- toolName: chunk.toolName,
4144
- availableTools: Object.keys(tools)
4145
- })
4146
- });
4147
- break;
4148
- }
4149
4137
  try {
4150
4138
  const toolCall = await parseToolCall({
4151
4139
  toolCall: chunk,
@@ -4155,6 +4143,7 @@ function runToolsTransformation({
4155
4143
  messages
4156
4144
  });
4157
4145
  controller.enqueue(toolCall);
4146
+ const tool2 = tools[toolCall.toolName];
4158
4147
  if (tool2.execute != null) {
4159
4148
  const toolExecutionId = generateId();
4160
4149
  outstandingToolResults.add(toolExecutionId);