Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# 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*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# Sentry Config File
.env.sentry-build-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createServer } from 'node:http';

// A single mock server standing in for the OpenAI, Anthropic and Google GenAI HTTP APIs, so the real
// SDK clients emit gen_ai spans without any live credentials. Response bodies mirror the mock servers
// in the node-integration tests (suites/tracing/{openai,anthropic,google-genai}). Uses raw `node:http`
// (not express) so the mock doesn't itself get instrumented.

function readJson(req) {
return new Promise(resolve => {
let body = '';
req.on('data', chunk => (body += chunk));
req.on('end', () => {
try {
resolve(JSON.parse(body || '{}'));
} catch {
resolve({});
}
});
});
}

function sendJson(res, status, obj) {
res.writeHead(status, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(obj));
}

let serverPromise;

/** Lazily starts the shared mock server and resolves to its port. */
export function getMockAiPort() {
serverPromise ??= new Promise(resolve => {
const server = createServer(async (req, res) => {
const url = req.url || '';

// OpenAI: chat completions
if (req.method === 'POST' && url.endsWith('/openai/chat/completions')) {
const { model } = await readJson(req);
sendJson(res, 200, {
id: 'chatcmpl-mock123',
object: 'chat.completion',
created: 1677652288,
model,
choices: [
{ index: 0, message: { role: 'assistant', content: 'Hello from OpenAI mock!' }, finish_reason: 'stop' },
],
usage: { prompt_tokens: 10, completion_tokens: 15, total_tokens: 25 },
});
return;
}

// Anthropic: messages
if (req.method === 'POST' && url.endsWith('/anthropic/v1/messages')) {
const { model } = await readJson(req);
sendJson(res, 200, {
id: 'msg_mock123',
type: 'message',
model,
role: 'assistant',
content: [{ type: 'text', text: 'Hello from Anthropic mock!' }],
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 10, output_tokens: 15 },
});
return;
}

// Google GenAI: generateContent (the model name is embedded in the path before `:generateContent`).
// Plain string checks avoid the polynomial-backtracking risk of a `.+` regex on the URL.
if (req.method === 'POST' && url.startsWith('/v1beta/models/') && url.endsWith(':generateContent')) {
await readJson(req);
sendJson(res, 200, {
candidates: [
{
content: { parts: [{ text: 'Mock response from Google GenAI!' }], role: 'model' },
finishReason: 'stop',
index: 0,
},
],
usageMetadata: { promptTokenCount: 8, candidatesTokenCount: 12, totalTokenCount: 20 },
});
return;
}

res.writeHead(404).end();
});

server.listen(0, () => {
resolve(server.address().port);
});
});

return serverPromise;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PropsWithChildren } from 'react';

export const dynamic = 'force-dynamic';

export default function Layout({ children }: PropsWithChildren<{}>) {
return (
<div>
<p>Layout</p>
{children}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PropsWithChildren } from 'react';

export const dynamic = 'force-dynamic';

export default function Layout({ children }: PropsWithChildren<{}>) {
return (
<div>
<p>DynamicLayout</p>
{children}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const dynamic = 'force-dynamic';

export default async function Page() {
return (
<div>
<p>Dynamic Page</p>
</div>
);
}

export async function generateMetadata() {
return {
title: 'I am dynamic page generated metadata',
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PropsWithChildren } from 'react';

export const dynamic = 'force-dynamic';

export default function Layout({ children }: PropsWithChildren<{}>) {
return (
<div>
<p>Layout</p>
{children}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const dynamic = 'force-dynamic';

export default function Page() {
return <p>Hello World!</p>;
}

export async function generateMetadata() {
return {
title: 'I am generated metadata',
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { generateText } from 'ai';
import { MockLanguageModelV1 } from 'ai/test';
import { z } from 'zod';
import * as Sentry from '@sentry/nextjs';

export const dynamic = 'force-dynamic';

// Error trace handling in tool calls
async function runAITest() {
const result = await generateText({
experimental_telemetry: { isEnabled: true },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'tool-calls',
usage: { promptTokens: 15, completionTokens: 25 },
text: 'Tool call completed!',
toolCalls: [
{
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'getWeather',
args: '{ "location": "San Francisco" }',
},
],
}),
}),
tools: {
getWeather: {
parameters: z.object({ location: z.string() }),
execute: async args => {
throw new Error('Tool call failed');
},
},
},
prompt: 'What is the weather in San Francisco?',
});
}

export default async function Page() {
await Sentry.startSpan({ op: 'function', name: 'ai-error-test' }, async () => {
return await runAITest();
});

return (
<div>
<h1>AI Test Results</h1>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { generateText } from 'ai';
import { MockLanguageModelV1 } from 'ai/test';
import { z } from 'zod';
import * as Sentry from '@sentry/nextjs';

export const dynamic = 'force-dynamic';

async function runAITest() {
// First span - telemetry should be enabled automatically but no input/output recorded by default
const result1 = await generateText({
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 20 },
text: 'First span here!',
}),
}),
prompt: 'Where is the first span?',
});

// Second span - explicitly enabled telemetry, should record inputs/outputs
const result2 = await generateText({
experimental_telemetry: { isEnabled: true },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 20 },
text: 'Second span here!',
}),
}),
prompt: 'Where is the second span?',
});

// Third span - with tool calls and tool results
const result3 = await generateText({
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'tool-calls',
usage: { promptTokens: 15, completionTokens: 25 },
text: 'Tool call completed!',
toolCalls: [
{
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'getWeather',
args: '{ "location": "San Francisco" }',
},
],
}),
}),
tools: {
getWeather: {
parameters: z.object({ location: z.string() }),
execute: async args => {
return `Weather in ${args.location}: Sunny, 72°F`;
},
},
},
prompt: 'What is the weather in San Francisco?',
});

// Fourth span - explicitly disabled telemetry, should not be captured
const result4 = await generateText({
experimental_telemetry: { isEnabled: false },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 20 },
text: 'Third span here!',
}),
}),
prompt: 'Where is the third span?',
});

return {
result1: result1.text,
result2: result2.text,
result3: result3.text,
result4: result4.text,
};
}

export default async function Page() {
const results = await Sentry.startSpan({ op: 'function', name: 'ai-test' }, async () => {
return await runAITest();
});

return (
<div>
<h1>AI Test Results</h1>
<pre id="ai-results">{JSON.stringify(results, null, 2)}</pre>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-dynamic';

export async function GET() {
throw new Error('Cron job error');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET() {
// Simulate some work
await new Promise(resolve => setTimeout(resolve, 100));
return NextResponse.json({ message: 'Cron job executed successfully' });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function GET() {
return Response.json({ name: 'John Doe' });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextResponse } from 'next/server';
import OpenAI from 'openai';
import { getMockAiPort } from '../../../ai-mock-server.mjs';

export const dynamic = 'force-dynamic';

export async function GET() {
const port = await getMockAiPort();
const client = new OpenAI({
baseURL: `http://localhost:${port}/openai`,
apiKey: 'mock-api-key',
});

await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});

return NextResponse.json({ status: 'ok' });
}
Loading
Loading