|
| 1 | +import Anthropic from '@anthropic-ai/sdk'; |
| 2 | +import { SessionToolRunner } from '@anthropic-ai/sdk/resources/beta/sessions/events'; |
| 3 | +import * as Sentry from '@sentry/node'; |
| 4 | +import express from 'express'; |
| 5 | + |
| 6 | +const STREAM_EVENTS = { |
| 7 | + sesn_weather: [ |
| 8 | + { type: 'agent.tool_use', id: 'sevt_weather', name: 'weather', input: { city: 'Vienna' } }, |
| 9 | + { type: 'agent.tool_use', id: 'sevt_explode', name: 'explode', input: {} }, |
| 10 | + { type: 'session.status_terminated', id: 'sevt_weather_end' }, |
| 11 | + ], |
| 12 | + // Served by a runner built with `new SessionToolRunner()`, as `EnvironmentWorker` does. |
| 13 | + sesn_direct: [ |
| 14 | + { type: 'agent.tool_use', id: 'sevt_read', name: 'read', input: { file_path: 'README.md' } }, |
| 15 | + { type: 'session.status_terminated', id: 'sevt_direct_end' }, |
| 16 | + ], |
| 17 | +}; |
| 18 | + |
| 19 | +function startMockSessionsServer() { |
| 20 | + const app = express(); |
| 21 | + app.use(express.json()); |
| 22 | + |
| 23 | + app.get('/anthropic/v1/sessions/:sessionId/events/stream', (req, res) => { |
| 24 | + res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' }); |
| 25 | + |
| 26 | + const events = STREAM_EVENTS[req.params.sessionId]; |
| 27 | + events.forEach((event, index) => { |
| 28 | + setTimeout(() => { |
| 29 | + res.write(`event: ${event.type}\n`); |
| 30 | + res.write(`data: ${JSON.stringify(event)}\n\n`); |
| 31 | + if (index === events.length - 1) { |
| 32 | + res.end(); |
| 33 | + } |
| 34 | + }, index * 10); |
| 35 | + }); |
| 36 | + }); |
| 37 | + app.get('/anthropic/v1/sessions/:sessionId/events', (_req, res) => { |
| 38 | + res.json({ data: [], next_page: null }); |
| 39 | + }); |
| 40 | + app.post('/anthropic/v1/sessions/:sessionId/events', (_req, res) => { |
| 41 | + res.json({ data: [] }); |
| 42 | + }); |
| 43 | + |
| 44 | + return new Promise(resolve => { |
| 45 | + const server = app.listen(0, () => { |
| 46 | + resolve(server); |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +async function drain(runner) { |
| 52 | + for await (const _call of runner) { |
| 53 | + // drain |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function run() { |
| 58 | + const server = await startMockSessionsServer(); |
| 59 | + |
| 60 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 61 | + const client = new Anthropic({ |
| 62 | + apiKey: 'mock-api-key', |
| 63 | + baseURL: `http://localhost:${server.address().port}/anthropic`, |
| 64 | + }); |
| 65 | + |
| 66 | + await drain( |
| 67 | + client.beta.sessions.events.toolRunner('sesn_weather', { |
| 68 | + tools: [ |
| 69 | + { |
| 70 | + name: 'weather', |
| 71 | + description: 'Get the weather by city', |
| 72 | + input_schema: { type: 'object', properties: { city: { type: 'string' } } }, |
| 73 | + run: () => 'sunny', |
| 74 | + }, |
| 75 | + { |
| 76 | + name: 'explode', |
| 77 | + description: 'Always fails', |
| 78 | + input_schema: { type: 'object', properties: {} }, |
| 79 | + run: () => { |
| 80 | + throw new Error('Tool exploded'); |
| 81 | + }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }), |
| 85 | + ); |
| 86 | + |
| 87 | + await drain( |
| 88 | + new SessionToolRunner('sesn_direct', { |
| 89 | + client, |
| 90 | + tools: [ |
| 91 | + { |
| 92 | + name: 'read', |
| 93 | + description: 'Read a file', |
| 94 | + input_schema: { type: 'object', properties: { file_path: { type: 'string' } } }, |
| 95 | + run: () => '# Weather app', |
| 96 | + }, |
| 97 | + ], |
| 98 | + }), |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + await Sentry.flush(2000); |
| 103 | + |
| 104 | + server.close(); |
| 105 | +} |
| 106 | + |
| 107 | +run(); |
0 commit comments