-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(vercel-ai): Add rerank support and fix token attribute mapping #19144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85e26d0
feat(core): Add AI SDK rerank support and fix token attribute mapping
sergical dadcb6a
feat(node): Add rerank to Vercel AI instrumented methods
sergical 6055a18
fix(node): Add null check for Vercel AI methods before patching
sergical a55c7fa
chore: fix formatting
sergical 82838a3
fix(e2e): Use V3 mock models for AI SDK v6
sergical af6061a
fix(e2e): Update AI SDK v6 mock models to correct V3 API format
sergical 1432759
test(e2e): Remove nextjs-15-ai-v6 test due to AI SDK mock bug
sergical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # next.js | ||
| /.next/ | ||
| /out/ | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| *.pem | ||
|
|
||
| # debug | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| .pnpm-debug.log* | ||
|
|
||
| # local env files | ||
| .env*.local | ||
|
|
||
| # vercel | ||
| .vercel | ||
|
|
||
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
|
|
||
| !*.d.ts | ||
|
|
||
| # Sentry | ||
| .sentryclirc | ||
|
|
||
| .vscode | ||
|
|
||
| test-results | ||
| event-dumps | ||
|
|
||
| .tmp_dev_server_logs |
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/.npmrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| @sentry:registry=http://127.0.0.1:4873 | ||
| @sentry-internal:registry=http://127.0.0.1:4873 | ||
| public-hoist-pattern[]=*import-in-the-middle* | ||
| public-hoist-pattern[]=*require-in-the-middle* |
57 changes: 57 additions & 0 deletions
57
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/app/ai-test/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { generateText, rerank } from 'ai'; | ||
| import { MockLanguageModelV3, MockRerankingModelV3 } from 'ai/test'; | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| export const dynamic = 'force-dynamic'; | ||
|
|
||
| async function runAITest() { | ||
| // Test generateText - uses V3 mock format for AI SDK v6 | ||
| const textResult = await generateText({ | ||
| experimental_telemetry: { isEnabled: true }, | ||
| model: new MockLanguageModelV3({ | ||
| doGenerate: async () => ({ | ||
| content: [{ type: 'text', text: 'Generated text from v6!' }], | ||
| finishReason: 'stop', | ||
| usage: { promptTokens: 10, completionTokens: 20 }, | ||
| warnings: [], | ||
| }), | ||
| }), | ||
| prompt: 'Test prompt for AI SDK v6', | ||
| }); | ||
|
|
||
| // Test rerank - uses V3 mock format for AI SDK v6 | ||
| const rerankResult = await rerank({ | ||
| experimental_telemetry: { isEnabled: true }, | ||
| model: new MockRerankingModelV3({ | ||
| doRerank: async () => ({ | ||
| results: [ | ||
| { documentIndex: 1, relevanceScore: 0.95 }, | ||
| { documentIndex: 0, relevanceScore: 0.8 }, | ||
| { documentIndex: 2, relevanceScore: 0.6 }, | ||
| ], | ||
| usage: { tokens: 50 }, | ||
| warnings: [], | ||
| }), | ||
| }), | ||
| query: 'search query for reranking', | ||
| documents: ['Document A', 'Document B', 'Document C'], | ||
| }); | ||
|
|
||
| return { | ||
| textResult: textResult.text, | ||
| rerankResult: rerankResult.results.map(r => ({ score: r.relevanceScore, index: r.documentIndex })), | ||
| }; | ||
| } | ||
|
|
||
| export default async function Page() { | ||
| const results = await Sentry.startSpan({ op: 'function', name: 'ai-test' }, async () => { | ||
| return await runAITest(); | ||
| }); | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>AI SDK v6 Test Results</h1> | ||
| <pre id="ai-results">{JSON.stringify(results, null, 2)}</pre> | ||
| </div> | ||
| ); | ||
| } |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/app/layout.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export default function Layout({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body>{children}</body> | ||
| </html> | ||
| ); | ||
| } |
3 changes: 3 additions & 0 deletions
3
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/app/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default function Page() { | ||
| return <p>Next 15 AI SDK v6 test app</p>; | ||
| } |
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/globals.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| interface Window { | ||
| recordedTransactions?: string[]; | ||
| capturedExceptionId?: string; | ||
| } |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/instrumentation-client.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| Sentry.init({ | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN, | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| }); | ||
|
|
||
| export const onRouterTransitionStart = Sentry.captureRouterTransitionStart; |
13 changes: 13 additions & 0 deletions
13
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/instrumentation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| export async function register() { | ||
| if (process.env.NEXT_RUNTIME === 'nodejs') { | ||
| await import('./sentry.server.config'); | ||
| } | ||
|
|
||
| if (process.env.NEXT_RUNTIME === 'edge') { | ||
| await import('./sentry.edge.config'); | ||
| } | ||
| } | ||
|
|
||
| export const onRequestError = Sentry.captureRequestError; |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/next.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const { withSentryConfig } = require('@sentry/nextjs'); | ||
|
|
||
| /** @type {import('next').NextConfig} */ | ||
| const nextConfig = {}; | ||
|
|
||
| module.exports = withSentryConfig(nextConfig, { | ||
| silent: true, | ||
| release: { | ||
| name: 'foobar123', | ||
| }, | ||
| }); |
31 changes: 31 additions & 0 deletions
31
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "name": "nextjs-15-ai-v6", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "build": "next build > .tmp_build_stdout 2> .tmp_build_stderr || (cat .tmp_build_stdout && cat .tmp_build_stderr && exit 1)", | ||
| "clean": "npx rimraf node_modules pnpm-lock.yaml .tmp_dev_server_logs", | ||
| "test:prod": "TEST_ENV=production playwright test", | ||
| "test:dev": "TEST_ENV=development playwright test", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm test:prod && pnpm test:dev" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/nextjs": "latest || *", | ||
| "@types/node": "^18.19.1", | ||
| "@types/react": "18.0.26", | ||
| "@types/react-dom": "18.0.9", | ||
| "ai": "^6.0.0", | ||
| "next": "15.5.10", | ||
| "react": "latest", | ||
| "react-dom": "latest", | ||
| "typescript": "~5.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.56.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/playwright.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
| const testEnv = process.env.TEST_ENV; | ||
|
|
||
| if (!testEnv) { | ||
| throw new Error('No test env defined'); | ||
| } | ||
|
|
||
| const getStartCommand = () => { | ||
| if (testEnv === 'development') { | ||
| return 'pnpm next dev -p 3030 2>&1 | tee .tmp_dev_server_logs'; | ||
| } | ||
|
|
||
| if (testEnv === 'production') { | ||
| return 'pnpm next start -p 3030'; | ||
| } | ||
|
|
||
| throw new Error(`Unknown test env: ${testEnv}`); | ||
| }; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: getStartCommand(), | ||
| port: 3030, | ||
| }); | ||
|
|
||
| export default config; |
13 changes: 13 additions & 0 deletions
13
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/sentry.edge.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| Sentry.init({ | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN, | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| transportOptions: { | ||
| // We are doing a lot of events at once in this test | ||
| bufferSize: 1000, | ||
| }, | ||
| }); |
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/sentry.server.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| Sentry.init({ | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN, | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| transportOptions: { | ||
| // We are doing a lot of events at once in this test | ||
| bufferSize: 1000, | ||
| }, | ||
| integrations: [Sentry.vercelAIIntegration()], | ||
| }); |
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/start-event-proxy.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| const packageJson = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'))); | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'nextjs-15-ai-v6', | ||
| envelopeDumpPath: path.join( | ||
| process.cwd(), | ||
| `event-dumps/next-15-ai-v6-v${packageJson.dependencies.next}-${process.env.TEST_ENV}.dump`, | ||
| ), | ||
| }); |
49 changes: 49 additions & 0 deletions
49
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/tests/ai-test.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('should create AI spans including rerank with correct attributes', async ({ page }) => { | ||
| const aiTransactionPromise = waitForTransaction('nextjs-15-ai-v6', async transactionEvent => { | ||
| return transactionEvent.transaction === 'GET /ai-test'; | ||
| }); | ||
|
|
||
| await page.goto('/ai-test'); | ||
|
|
||
| const aiTransaction = await aiTransactionPromise; | ||
|
|
||
| expect(aiTransaction).toBeDefined(); | ||
| expect(aiTransaction.transaction).toBe('GET /ai-test'); | ||
|
|
||
| const spans = aiTransaction.spans || []; | ||
|
|
||
| // Check for generateText spans | ||
| const aiPipelineSpans = spans.filter(span => span.op === 'gen_ai.invoke_agent'); | ||
| const aiGenerateSpans = spans.filter(span => span.op === 'gen_ai.generate_text'); | ||
|
|
||
| // Check for rerank spans - the main feature being tested | ||
| const rerankSpans = spans.filter(span => span.op === 'gen_ai.rerank'); | ||
|
|
||
| expect(aiPipelineSpans.length).toBeGreaterThanOrEqual(1); | ||
| expect(aiGenerateSpans.length).toBeGreaterThanOrEqual(1); | ||
| expect(rerankSpans.length).toBeGreaterThanOrEqual(1); | ||
|
|
||
| // Verify generateText span has expected attributes | ||
| const generatePipelineSpan = aiPipelineSpans.find(span => | ||
| span.data?.['vercel.ai.prompt']?.includes('Test prompt for AI SDK v6'), | ||
| ); | ||
| expect(generatePipelineSpan).toBeDefined(); | ||
| expect(generatePipelineSpan?.data?.['gen_ai.response.text']).toContain('Generated text from v6!'); | ||
|
|
||
| // Verify rerank span has expected attributes | ||
| const rerankPipelineSpan = aiPipelineSpans.find(span => | ||
| span.data?.['gen_ai.request.rerank.query']?.includes('search query for reranking'), | ||
| ); | ||
| expect(rerankPipelineSpan).toBeDefined(); | ||
| expect(rerankPipelineSpan?.data?.['gen_ai.request.rerank.documents_count']).toBe(3); | ||
| expect(rerankPipelineSpan?.data?.['gen_ai.response.rerank.top_score']).toBe(0.95); | ||
|
|
||
| // Verify results are displayed on the page | ||
| const resultsText = await page.locator('#ai-results').textContent(); | ||
| expect(resultsText).toContain('Generated text from v6!'); | ||
| expect(resultsText).toContain('Document B'); | ||
| expect(resultsText).toContain('0.95'); | ||
| }); | ||
26 changes: 26 additions & 0 deletions
26
dev-packages/e2e-tests/test-applications/nextjs-15-ai-v6/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es2018", | ||
| "allowImportingTsExtensions": true, | ||
| "lib": ["dom", "dom.iterable", "esnext"], | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "noEmit": true, | ||
| "esModuleInterop": true, | ||
| "module": "esnext", | ||
| "moduleResolution": "node", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "jsx": "preserve", | ||
| "plugins": [ | ||
| { | ||
| "name": "next" | ||
| } | ||
| ], | ||
| "incremental": true | ||
| }, | ||
| "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "next.config.js", ".next/types/**/*.ts"], | ||
| "exclude": ["node_modules", "playwright.config.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E2E test expects unmapped rerank attributes
Medium Severity
The E2E test expects rerank-specific attributes under the
gen_ai.*namespace (gen_ai.request.rerank.query,gen_ai.request.rerank.documents_count,gen_ai.response.rerank.top_score), but the production code doesn't include any attribute mappings for rerank-specific data. Based on the existing codebase pattern, unmappedai.*attributes get prefixed withvercel.and becomevercel.ai.*. This mismatch means either the test expectations are incorrect (should usevercel.ai.*) or the production code is missing attribute mappings for rerank. Per the review rules, this flags that the test may not properly test the newly added behavior.