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
@@ -1,82 +1,68 @@
import { instrumentAnthropicAiClient } from '@sentry/core';
import Anthropic from '@anthropic-ai/sdk';
import * as Sentry from '@sentry/node';
import express from 'express';

class MockAnthropic {
constructor(config) {
this.apiKey = config.apiKey;
this.messages = {
create: this._messagesCreate.bind(this),
};
this.models = {
retrieve: this._modelsRetrieve.bind(this),
};
}
function startMockAnthropicServer() {
const app = express();
app.use(express.json());

async _messagesCreate(params) {
await new Promise(resolve => setTimeout(resolve, 5));

// Case 1: Invalid tool format error
if (params.model === 'invalid-format') {
const error = new Error('Invalid format');
error.status = 400;
error.headers = { 'x-request-id': 'mock-invalid-tool-format-error' };
throw error;
app.post('/anthropic/v1/messages', (req, res) => {
if (req.body.model === 'invalid-format') {
res
.status(400)
.set('x-request-id', 'mock-invalid-tool-format-error')
.send({ type: 'error', error: { type: 'invalid_request_error', message: 'Invalid format' } });
return;
}

// Default case (success) - return tool use for successful tool usage test
return {
res.send({
id: 'msg_ok',
type: 'message',
model: params.model,
model: req.body.model,
role: 'assistant',
content: [
{
type: 'tool_use',
id: 'tool_ok_1',
name: 'calculator',
input: { expression: '2+2' },
},
],
content: [{ type: 'tool_use', id: 'tool_ok_1', name: 'calculator', input: { expression: '2+2' } }],
stop_reason: 'tool_use',
stop_sequence: null,
usage: { input_tokens: 7, output_tokens: 9 },
};
}

async _modelsRetrieve(modelId) {
await new Promise(resolve => setTimeout(resolve, 5));
});
});

// Case for model retrieval error
if (modelId === 'nonexistent-model') {
const error = new Error('Model not found');
error.status = 404;
error.headers = { 'x-request-id': 'mock-model-retrieval-error' };
throw error;
app.get('/anthropic/v1/models/:model', (req, res) => {
if (req.params.model === 'nonexistent-model') {
res
.status(404)
.set('x-request-id', 'mock-model-retrieval-error')
.send({ type: 'error', error: { type: 'not_found_error', message: 'Model not found' } });
return;
}
res.send({ id: req.params.model, name: req.params.model, created_at: 1715145600, model: req.params.model });
});

return {
id: modelId,
name: modelId,
created_at: 1715145600,
model: modelId,
};
}
return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockAnthropicServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockAnthropic({ apiKey: 'mock-api-key' });
const client = instrumentAnthropicAiClient(mockClient);
const client = new Anthropic({
apiKey: 'mock-api-key',
baseURL: `http://localhost:${server.address().port}/anthropic`,
});

// 1. Test invalid format error
// https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#handling-tool-use-and-tool-result-content-blocks
// 1. Invalid format error
try {
await client.messages.create({
model: 'invalid-format',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Here are the results:' }, // ❌ Text before tool_result
{ type: 'text', text: 'Here are the results:' },
{ type: 'tool_result', tool_use_id: 'toolu_01' },
],
},
Expand All @@ -86,14 +72,14 @@ async function run() {
// Error expected
}

// 2. Test model retrieval error
// 2. Model retrieval error
try {
await client.models.retrieve('nonexistent-model');
} catch {
// Error expected
}

// 3. Test successful tool usage for comparison
// 3. Successful tool usage for comparison
await client.messages.create({
model: 'claude-3-haiku-20240307',
messages: [{ role: 'user', content: 'Calculate 2+2' }],
Expand All @@ -110,6 +96,10 @@ async function run() {
],
});
});

await Sentry.flush(2000);

server.close();
}

run();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
import { instrumentAnthropicAiClient } from '@sentry/core';
import Anthropic from '@anthropic-ai/sdk';
import * as Sentry from '@sentry/node';
import express from 'express';

class MockAnthropic {
constructor(config) {
this.apiKey = config.apiKey;
this.baseURL = config.baseURL;
function startMockAnthropicServer() {
const app = express();
app.use(express.json({ limit: '10mb' }));

// Create messages object with create method
this.messages = {
create: this._messagesCreate.bind(this),
};
}

/**
* Create a mock message
*/
async _messagesCreate(params) {
// Simulate processing time
await new Promise(resolve => setTimeout(resolve, 10));

return {
app.post('/anthropic/v1/messages', (req, res) => {
res.send({
id: 'msg-truncation-test',
type: 'message',
role: 'assistant',
content: [
{
type: 'text',
text: 'This is the number **3**.',
},
],
model: params.model,
content: [{ type: 'text', text: 'This is the number **3**.' }],
model: req.body.model,
stop_reason: 'end_turn',
stop_sequence: null,
usage: {
input_tokens: 10,
output_tokens: 15,
},
};
}
usage: { input_tokens: 10, output_tokens: 15 },
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockAnthropicServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockAnthropic({
const client = new Anthropic({
apiKey: 'mock-api-key',
baseURL: `http://localhost:${server.address().port}/anthropic`,
});

const client = instrumentAnthropicAiClient(mockClient, { enableTruncation: true, recordInputs: true });

// Send the image showing the number 3
// Put the image in the last message so it doesn't get dropped
await client.messages.create({
Expand Down Expand Up @@ -75,6 +62,10 @@ async function run() {
temperature: 0.7,
});
});

await Sentry.flush(2000);

server.close();
}

run();
Loading
Loading