|
| 1 | +import { instrumentAnthropicAiClient } from '@sentry/core'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | + |
| 4 | +function createMockStreamEvents(model = 'claude-3-haiku-20240307') { |
| 5 | + async function* generator() { |
| 6 | + // initial message metadata with id/model and input tokens |
| 7 | + yield { |
| 8 | + type: 'content_block_start', |
| 9 | + message: { |
| 10 | + id: 'msg_stream_tool_1', |
| 11 | + type: 'message', |
| 12 | + role: 'assistant', |
| 13 | + model, |
| 14 | + content: [], |
| 15 | + stop_reason: 'end_turn', |
| 16 | + usage: { input_tokens: 11 }, |
| 17 | + }, |
| 18 | + }; |
| 19 | + |
| 20 | + // streamed text |
| 21 | + yield { type: 'content_block_delta', delta: { text: 'Starting tool...' } }; |
| 22 | + |
| 23 | + // tool_use streamed via partial json |
| 24 | + yield { |
| 25 | + type: 'content_block_start', |
| 26 | + index: 0, |
| 27 | + content_block: { type: 'tool_use', id: 'tool_weather_2', name: 'weather' }, |
| 28 | + }; |
| 29 | + yield { type: 'content_block_delta', index: 0, delta: { partial_json: '{"city":' } }; |
| 30 | + yield { type: 'content_block_delta', index: 0, delta: { partial_json: '"Paris"}' } }; |
| 31 | + yield { type: 'content_block_stop', index: 0 }; |
| 32 | + |
| 33 | + // more text |
| 34 | + yield { type: 'content_block_delta', delta: { text: 'Done.' } }; |
| 35 | + |
| 36 | + // final usage |
| 37 | + yield { type: 'message_delta', usage: { output_tokens: 9 } }; |
| 38 | + } |
| 39 | + return generator(); |
| 40 | +} |
| 41 | + |
| 42 | +class MockAnthropic { |
| 43 | + constructor(config) { |
| 44 | + this.apiKey = config.apiKey; |
| 45 | + this.messages = { |
| 46 | + create: this._messagesCreate.bind(this), |
| 47 | + stream: this._messagesStream.bind(this), |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + async _messagesCreate(params) { |
| 52 | + await new Promise(resolve => setTimeout(resolve, 5)); |
| 53 | + if (params?.stream) { |
| 54 | + return createMockStreamEvents(params.model); |
| 55 | + } |
| 56 | + return { |
| 57 | + id: 'msg_mock_no_stream', |
| 58 | + type: 'message', |
| 59 | + model: params.model, |
| 60 | + role: 'assistant', |
| 61 | + content: [{ type: 'text', text: 'No stream' }], |
| 62 | + usage: { input_tokens: 2, output_tokens: 3 }, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + async _messagesStream(params) { |
| 67 | + await new Promise(resolve => setTimeout(resolve, 5)); |
| 68 | + return createMockStreamEvents(params?.model); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +async function run() { |
| 73 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 74 | + const mockClient = new MockAnthropic({ apiKey: 'mock-api-key' }); |
| 75 | + const client = instrumentAnthropicAiClient(mockClient); |
| 76 | + |
| 77 | + // stream via create(stream:true) |
| 78 | + const stream1 = await client.messages.create({ |
| 79 | + model: 'claude-3-haiku-20240307', |
| 80 | + messages: [{ role: 'user', content: 'Need the weather' }], |
| 81 | + tools: [ |
| 82 | + { |
| 83 | + name: 'weather', |
| 84 | + description: 'Get weather', |
| 85 | + input_schema: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] }, |
| 86 | + }, |
| 87 | + ], |
| 88 | + stream: true, |
| 89 | + }); |
| 90 | + for await (const _ of stream1) { |
| 91 | + void _; |
| 92 | + } |
| 93 | + |
| 94 | + // stream via messages.stream |
| 95 | + const stream2 = await client.messages.stream({ |
| 96 | + model: 'claude-3-haiku-20240307', |
| 97 | + messages: [{ role: 'user', content: 'Need the weather' }], |
| 98 | + tools: [ |
| 99 | + { |
| 100 | + name: 'weather', |
| 101 | + description: 'Get weather', |
| 102 | + input_schema: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] }, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }); |
| 106 | + for await (const _ of stream2) { |
| 107 | + void _; |
| 108 | + } |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +run(); |
0 commit comments