|
| 1 | +import { generateText } from 'ai'; |
| 2 | +import { MockLanguageModelV1 } from 'ai/test'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import * as Sentry from '@sentry/nextjs'; |
| 5 | + |
| 6 | +export const dynamic = 'force-dynamic'; |
| 7 | + |
| 8 | +async function runAITest() { |
| 9 | + // First span - telemetry should be enabled automatically but no input/output recorded when sendDefaultPii: true |
| 10 | + const result1 = await generateText({ |
| 11 | + model: new MockLanguageModelV1({ |
| 12 | + doGenerate: async () => ({ |
| 13 | + rawCall: { rawPrompt: null, rawSettings: {} }, |
| 14 | + finishReason: 'stop', |
| 15 | + usage: { promptTokens: 10, completionTokens: 20 }, |
| 16 | + text: 'First span here!', |
| 17 | + }), |
| 18 | + }), |
| 19 | + prompt: 'Where is the first span?', |
| 20 | + }); |
| 21 | + |
| 22 | + // Second span - explicitly enabled telemetry, should record inputs/outputs |
| 23 | + const result2 = await generateText({ |
| 24 | + experimental_telemetry: { isEnabled: true }, |
| 25 | + model: new MockLanguageModelV1({ |
| 26 | + doGenerate: async () => ({ |
| 27 | + rawCall: { rawPrompt: null, rawSettings: {} }, |
| 28 | + finishReason: 'stop', |
| 29 | + usage: { promptTokens: 10, completionTokens: 20 }, |
| 30 | + text: 'Second span here!', |
| 31 | + }), |
| 32 | + }), |
| 33 | + prompt: 'Where is the second span?', |
| 34 | + }); |
| 35 | + |
| 36 | + // Third span - with tool calls and tool results |
| 37 | + const result3 = await generateText({ |
| 38 | + model: new MockLanguageModelV1({ |
| 39 | + doGenerate: async () => ({ |
| 40 | + rawCall: { rawPrompt: null, rawSettings: {} }, |
| 41 | + finishReason: 'tool-calls', |
| 42 | + usage: { promptTokens: 15, completionTokens: 25 }, |
| 43 | + text: 'Tool call completed!', |
| 44 | + toolCalls: [ |
| 45 | + { |
| 46 | + toolCallType: 'function', |
| 47 | + toolCallId: 'call-1', |
| 48 | + toolName: 'getWeather', |
| 49 | + args: '{ "location": "San Francisco" }', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }), |
| 53 | + }), |
| 54 | + tools: { |
| 55 | + getWeather: { |
| 56 | + parameters: z.object({ location: z.string() }), |
| 57 | + execute: async (args) => { |
| 58 | + return `Weather in ${args.location}: Sunny, 72°F`; |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + prompt: 'What is the weather in San Francisco?', |
| 63 | + }); |
| 64 | + |
| 65 | + // Fourth span - explicitly disabled telemetry, should not be captured |
| 66 | + const result4 = await generateText({ |
| 67 | + experimental_telemetry: { isEnabled: false }, |
| 68 | + model: new MockLanguageModelV1({ |
| 69 | + doGenerate: async () => ({ |
| 70 | + rawCall: { rawPrompt: null, rawSettings: {} }, |
| 71 | + finishReason: 'stop', |
| 72 | + usage: { promptTokens: 10, completionTokens: 20 }, |
| 73 | + text: 'Third span here!', |
| 74 | + }), |
| 75 | + }), |
| 76 | + prompt: 'Where is the third span?', |
| 77 | + }); |
| 78 | + |
| 79 | + return { |
| 80 | + result1: result1.text, |
| 81 | + result2: result2.text, |
| 82 | + result3: result3.text, |
| 83 | + result4: result4.text, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +export default async function Page() { |
| 88 | + const results = await Sentry.startSpan( |
| 89 | + { op: 'function', name: 'ai-test' }, |
| 90 | + async () => { |
| 91 | + return await runAITest(); |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <h1>AI Test Results</h1> |
| 98 | + <pre id="ai-results">{JSON.stringify(results, null, 2)}</pre> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments