|
| 1 | +import express from 'express'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { Mastra } from '@mastra/core'; |
| 5 | +import { Agent } from '@mastra/core/agent'; |
| 6 | +import { createTool } from '@mastra/core/tools'; |
| 7 | +import { Observability } from '@mastra/observability'; |
| 8 | +import { SentryMastraExporter } from '@sentry/node'; |
| 9 | + |
| 10 | +// Two-step agent loop. Inlined OpenAI-compatible mock: the ESM/CJS runner copies only this file. |
| 11 | +function startMockProvider(responses) { |
| 12 | + const app = express(); |
| 13 | + app.use(express.json()); |
| 14 | + |
| 15 | + let call = 0; |
| 16 | + app.post('/v1/chat/completions', (req, res) => { |
| 17 | + const response = responses[Math.min(call, responses.length - 1)]; |
| 18 | + call++; |
| 19 | + res.json({ |
| 20 | + id: response.id, |
| 21 | + object: 'chat.completion', |
| 22 | + created: 1, |
| 23 | + model: req.body.model, |
| 24 | + choices: [ |
| 25 | + { |
| 26 | + index: 0, |
| 27 | + finish_reason: response.toolCalls ? 'tool_calls' : 'stop', |
| 28 | + message: { |
| 29 | + role: 'assistant', |
| 30 | + content: response.content ?? null, |
| 31 | + ...(response.toolCalls ? { tool_calls: response.toolCalls } : {}), |
| 32 | + }, |
| 33 | + }, |
| 34 | + ], |
| 35 | + usage: response.usage, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const server = app.listen(0); |
| 40 | + return { |
| 41 | + url: `http://localhost:${server.address().port}/v1`, |
| 42 | + close: () => server.close(), |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +const provider = startMockProvider([ |
| 47 | + { |
| 48 | + id: 'chatcmpl-tool', |
| 49 | + toolCalls: [ |
| 50 | + { |
| 51 | + id: 'call_1', |
| 52 | + type: 'function', |
| 53 | + function: { name: 'get_weather', arguments: '{"city":"Berlin"}' }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + usage: { prompt_tokens: 20, completion_tokens: 5, total_tokens: 25 }, |
| 57 | + }, |
| 58 | + { |
| 59 | + id: 'chatcmpl-final', |
| 60 | + content: 'It is 22C in Berlin.', |
| 61 | + usage: { prompt_tokens: 30, completion_tokens: 8, total_tokens: 38 }, |
| 62 | + }, |
| 63 | +]); |
| 64 | + |
| 65 | +async function run() { |
| 66 | + const agent = new Agent({ |
| 67 | + id: 'weather_agent', |
| 68 | + name: 'weather_agent', |
| 69 | + instructions: 'Use the weather tool.', |
| 70 | + model: { id: 'openai/gpt-4o-mini', url: provider.url, apiKey: 'test' }, |
| 71 | + tools: { |
| 72 | + get_weather: createTool({ |
| 73 | + id: 'get_weather', |
| 74 | + description: 'Get weather for a city', |
| 75 | + inputSchema: z.object({ city: z.string() }), |
| 76 | + execute: async ({ city }) => ({ temperature: 22, city }), |
| 77 | + }), |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const mastra = new Mastra({ |
| 82 | + agents: { weather_agent: agent }, |
| 83 | + logger: false, |
| 84 | + observability: new Observability({ |
| 85 | + configs: { |
| 86 | + default: { |
| 87 | + serviceName: 'mastra-test', |
| 88 | + exporters: [new SentryMastraExporter()], |
| 89 | + }, |
| 90 | + }, |
| 91 | + }), |
| 92 | + }); |
| 93 | + |
| 94 | + await Sentry.startSpan({ op: 'function', name: 'mastra-test' }, async () => { |
| 95 | + await mastra.getAgent('weather_agent').generate('Weather in Berlin?', { maxSteps: 3 }); |
| 96 | + }); |
| 97 | + |
| 98 | + await mastra.observability.shutdown(); |
| 99 | + provider.close(); |
| 100 | +} |
| 101 | + |
| 102 | +run(); |
0 commit comments