|
| 1 | +import * as Sentry from '@sentry/aws-serverless'; |
| 2 | +import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from '@aws-sdk/client-bedrock-runtime'; |
| 3 | +// Force the HTTP/1 handler so `nock` can intercept the request instead of hitting real AWS. |
| 4 | +import { NodeHttpHandler } from '@smithy/node-http-handler'; |
| 5 | +import { EventStreamCodec } from '@smithy/core/event-streams'; |
| 6 | +import nock from 'nock'; |
| 7 | +import { Buffer } from 'node:buffer'; |
| 8 | + |
| 9 | +nock.disableNetConnect(); |
| 10 | + |
| 11 | +const region = 'us-east-1'; |
| 12 | +const credentials = { accessKeyId: 'aws-test-key', secretAccessKey: 'aws-test-secret' }; |
| 13 | +const host = `https://bedrock-runtime.${region}.amazonaws.com`; |
| 14 | + |
| 15 | +// Bedrock streams responses in the binary AWS event stream format, not JSON. The SDK decodes and |
| 16 | +// checks each message (headers, payload, CRC32), so the mocked response must use the same format. |
| 17 | +// The SDK's own codec encodes the messages. It needs UTF-8 helpers to encode the header strings. |
| 18 | +// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTSelectObjectAppendix.html |
| 19 | +const codec = new EventStreamCodec( |
| 20 | + bytes => Buffer.from(bytes).toString('utf8'), |
| 21 | + text => Buffer.from(text), |
| 22 | +); |
| 23 | + |
| 24 | +// The codec needs a type for each header value. All Bedrock headers are strings. |
| 25 | +function encodeMessage(headers, payload) { |
| 26 | + return codec.encode({ |
| 27 | + headers: Object.fromEntries(Object.entries(headers).map(([name, value]) => [name, { type: 'string', value }])), |
| 28 | + body: Buffer.from(JSON.stringify(payload)), |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +// The SDK reads `:event-type` to find the stream member (for example `messageStart` or `chunk`). |
| 33 | +function event(eventType, payload) { |
| 34 | + return encodeMessage( |
| 35 | + { ':message-type': 'event', ':event-type': eventType, ':content-type': 'application/json' }, |
| 36 | + payload, |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function mockStream(path, messages) { |
| 41 | + nock(host).post(path).reply(200, Buffer.concat(messages), { 'content-type': 'application/vnd.amazon.eventstream' }); |
| 42 | +} |
| 43 | + |
| 44 | +function createClient() { |
| 45 | + return new BedrockRuntimeClient({ region, credentials, maxAttempts: 1, requestHandler: new NodeHttpHandler() }); |
| 46 | +} |
| 47 | + |
| 48 | +function converse(modelId, privacy) { |
| 49 | + nock(host) |
| 50 | + .post(/\/model\/.*\/converse$/) |
| 51 | + .reply( |
| 52 | + 200, |
| 53 | + JSON.stringify({ |
| 54 | + output: { message: { role: 'assistant', content: [{ text: 'Hello from Bedrock' }] } }, |
| 55 | + stopReason: 'end_turn', |
| 56 | + usage: { inputTokens: 12, outputTokens: 8, totalTokens: 20 }, |
| 57 | + }), |
| 58 | + { 'content-type': 'application/json' }, |
| 59 | + ); |
| 60 | + |
| 61 | + return createClient().send( |
| 62 | + new ConverseCommand({ |
| 63 | + modelId, |
| 64 | + messages: [{ role: 'user', content: [{ text: 'Hello' }] }], |
| 65 | + requestMetadata: { privacy }, |
| 66 | + }), |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +async function run() { |
| 71 | + await Sentry.startSpan({ name: 'Test Transaction' }, async () => { |
| 72 | + await Sentry.withScope(async scope => { |
| 73 | + scope.setTag('privacy', 'public'); |
| 74 | + await converse('amazon.nova-pro-v1:0', 'public'); |
| 75 | + }); |
| 76 | + |
| 77 | + await Sentry.withScope(async scope => { |
| 78 | + scope.setTag('privacy', 'private'); |
| 79 | + await converse('amazon.nova-lite-v1:0', 'private'); |
| 80 | + |
| 81 | + mockStream(/\/model\/.*\/converse-stream$/, [ |
| 82 | + event('messageStart', { role: 'assistant' }), |
| 83 | + event('contentBlockDelta', { contentBlockIndex: 0, delta: { text: 'Hello from Bedrock' } }), |
| 84 | + event('contentBlockStop', { contentBlockIndex: 0 }), |
| 85 | + event('messageStop', { stopReason: 'end_turn' }), |
| 86 | + ]); |
| 87 | + const pending = createClient().send( |
| 88 | + new ConverseStreamCommand({ |
| 89 | + modelId: 'amazon.nova-micro-v1:0', |
| 90 | + messages: [{ role: 'user', content: [{ text: 'Hello' }] }], |
| 91 | + requestMetadata: { privacy: 'public' }, |
| 92 | + }), |
| 93 | + ); |
| 94 | + // Changing the tag after the call started must not change its outputs decision. |
| 95 | + Sentry.getCurrentScope().setTag('privacy', 'public'); |
| 96 | + const response = await pending; |
| 97 | + for await (const _ of response.stream) { |
| 98 | + // drain |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +run(); |
0 commit comments