@ai-sdk/amazon-bedrock 4.0.20 → 4.0.22

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
@@ -99,70 +99,84 @@ import {
99
99
  extractResponseHeaders,
100
100
  safeValidateTypes
101
101
  } from "@ai-sdk/provider-utils";
102
+
103
+ // src/bedrock-event-stream-decoder.ts
102
104
  import { EventStreamCodec } from "@smithy/eventstream-codec";
103
105
  import { toUtf8, fromUtf8 } from "@smithy/util-utf8";
106
+ function createBedrockEventStreamDecoder(body, processEvent) {
107
+ const codec = new EventStreamCodec(toUtf8, fromUtf8);
108
+ let buffer = new Uint8Array(0);
109
+ const textDecoder = new TextDecoder();
110
+ return body.pipeThrough(
111
+ new TransformStream({
112
+ async transform(chunk, controller) {
113
+ var _a, _b;
114
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
115
+ newBuffer.set(buffer);
116
+ newBuffer.set(chunk, buffer.length);
117
+ buffer = newBuffer;
118
+ while (buffer.length >= 4) {
119
+ const totalLength = new DataView(
120
+ buffer.buffer,
121
+ buffer.byteOffset,
122
+ buffer.byteLength
123
+ ).getUint32(0, false);
124
+ if (buffer.length < totalLength) {
125
+ break;
126
+ }
127
+ try {
128
+ const subView = buffer.subarray(0, totalLength);
129
+ const decoded = codec.decode(subView);
130
+ buffer = buffer.slice(totalLength);
131
+ const messageType = (_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value;
132
+ const eventType = (_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value;
133
+ const data = textDecoder.decode(decoded.body);
134
+ await processEvent({ messageType, eventType, data }, controller);
135
+ } catch (e) {
136
+ break;
137
+ }
138
+ }
139
+ }
140
+ })
141
+ );
142
+ }
143
+
144
+ // src/bedrock-event-stream-response-handler.ts
104
145
  var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => {
105
146
  const responseHeaders = extractResponseHeaders(response);
106
147
  if (response.body == null) {
107
148
  throw new EmptyResponseBodyError({});
108
149
  }
109
- const codec = new EventStreamCodec(toUtf8, fromUtf8);
110
- let buffer = new Uint8Array(0);
111
- const textDecoder = new TextDecoder();
112
150
  return {
113
151
  responseHeaders,
114
- value: response.body.pipeThrough(
115
- new TransformStream({
116
- async transform(chunk, controller) {
117
- var _a, _b;
118
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
119
- newBuffer.set(buffer);
120
- newBuffer.set(chunk, buffer.length);
121
- buffer = newBuffer;
122
- while (buffer.length >= 4) {
123
- const totalLength = new DataView(
124
- buffer.buffer,
125
- buffer.byteOffset,
126
- buffer.byteLength
127
- ).getUint32(0, false);
128
- if (buffer.length < totalLength) {
129
- break;
130
- }
131
- try {
132
- const subView = buffer.subarray(0, totalLength);
133
- const decoded = codec.decode(subView);
134
- buffer = buffer.slice(totalLength);
135
- if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
136
- const data = textDecoder.decode(decoded.body);
137
- const parsedDataResult = await safeParseJSON({ text: data });
138
- if (!parsedDataResult.success) {
139
- controller.enqueue(parsedDataResult);
140
- break;
141
- }
142
- delete parsedDataResult.value.p;
143
- let wrappedData = {
144
- [(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
145
- };
146
- const validatedWrappedData = await safeValidateTypes({
147
- value: wrappedData,
148
- schema: chunkSchema
149
- });
150
- if (!validatedWrappedData.success) {
151
- controller.enqueue(validatedWrappedData);
152
- } else {
153
- controller.enqueue({
154
- success: true,
155
- value: validatedWrappedData.value,
156
- rawValue: wrappedData
157
- });
158
- }
159
- }
160
- } catch (e) {
161
- break;
162
- }
152
+ value: createBedrockEventStreamDecoder(
153
+ response.body,
154
+ async (event, controller) => {
155
+ if (event.messageType === "event") {
156
+ const parsedDataResult = await safeParseJSON({ text: event.data });
157
+ if (!parsedDataResult.success) {
158
+ controller.enqueue(parsedDataResult);
159
+ return;
160
+ }
161
+ delete parsedDataResult.value.p;
162
+ const wrappedData = {
163
+ [event.eventType]: parsedDataResult.value
164
+ };
165
+ const validatedWrappedData = await safeValidateTypes({
166
+ value: wrappedData,
167
+ schema: chunkSchema
168
+ });
169
+ if (!validatedWrappedData.success) {
170
+ controller.enqueue(validatedWrappedData);
171
+ } else {
172
+ controller.enqueue({
173
+ success: true,
174
+ value: validatedWrappedData.value,
175
+ rawValue: wrappedData
176
+ });
163
177
  }
164
178
  }
165
- })
179
+ }
166
180
  )
167
181
  };
168
182
  };
@@ -1759,7 +1773,7 @@ import {
1759
1773
  import { AwsV4Signer } from "aws4fetch";
1760
1774
 
1761
1775
  // src/version.ts
1762
- var VERSION = true ? "4.0.20" : "0.0.0-test";
1776
+ var VERSION = true ? "4.0.22" : "0.0.0-test";
1763
1777
 
1764
1778
  // src/bedrock-sigv4-fetch.ts
1765
1779
  function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {