🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
// @vitest-environment node
import type { Mock } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { buildDefaultAnthropicPayload } from '../../core/anthropicCompatible';
import * as anthropicHelpers from '../../core/protocols/anthropicMessages/messages';
import type { ChatCompletionTool, ChatStreamPayload } from '../../types/chat';
import * as debugStreamModule from '../../utils/debugStream';
import { LobeAnthropicAI } from './index';
const createHelloStream = () =>
new ReadableStream({
start(controller) {
controller.enqueue('Hello, world!');
controller.close();
},
});
const provider = 'anthropic';
const bizErrorType = 'ProviderBizError';
const invalidErrorType = 'InvalidProviderAPIKey';
// Mock the console.error to avoid polluting test output
vi.spyOn(console, 'error').mockImplementation(() => {});
let instance: InstanceType<typeof LobeAnthropicAI>;
beforeEach(() => {
instance = new LobeAnthropicAI({ apiKey: 'test' });
// Use vi.spyOn to mock the Anthropic messages.create call.
vi.spyOn(instance['client'].messages, 'create').mockResolvedValue(new ReadableStream() as any);
});
afterEach(() => {
vi.clearAllMocks();
});
describe('LobeAnthropicAI', () => {
describe('init', () => {
it('should correctly initialize with an API key', async () => {
const instance = new LobeAnthropicAI({ apiKey: 'test_api_key' });
expect(instance).toBeInstanceOf(LobeAnthropicAI);
expect(instance.baseURL).toBe('https://api.anthropic.com');
});
it('should correctly initialize with a baseURL', async () => {
const instance = new LobeAnthropicAI({
apiKey: 'test_api_key',
baseURL: 'https://api.anthropic.proxy',
});
expect(instance).toBeInstanceOf(LobeAnthropicAI);
expect(instance.baseURL).toBe('https://api.anthropic.proxy');
});
it('should correctly initialize with different id', async () => {
const instance = new LobeAnthropicAI({
apiKey: 'test_api_key',
id: 'abc',
});
expect(instance).toBeInstanceOf(LobeAnthropicAI);
expect(instance['id']).toBe('abc');
});
});
describe('chat', () => {
it('should return a StreamingTextResponse on successful API call', async () => {
const result = await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
});
// Assert
expect(result).toBeInstanceOf(Response);
});
it('should handle text messages correctly', async () => {
// Arrange
const mockStream = createHelloStream();
const mockResponse = Promise.resolve(mockStream);
(instance['client'].messages.create as Mock).mockResolvedValue(mockResponse);
// Act
const result = await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
top_p: 1,
});
// Assert
expect(instance['client'].messages.create).toHaveBeenCalledWith(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
stream: true,
temperature: 0,
top_p: 1,
}),
expect.objectContaining({}),
);
expect(result).toBeInstanceOf(Response);
});
it.each([
['claude-opus-4-6', 'high', 'high'],
['claude-haiku-4-5-20251001', 'high', undefined],
['claude-sonnet-4-6', 'xhigh', undefined],
['claude-opus-4-7', 'xhigh', 'xhigh'],
] as const)(
'should map routed effort %s / %s to Anthropic output config %s',
async (model, reasoningEffort, expectedEffort) => {
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model,
reasoning_effort: reasoningEffort,
});
const payload = (instance['client'].messages.create as Mock).mock.calls[0][0];
expect(payload.output_config).toEqual(
expectedEffort ? { effort: expectedEffort } : undefined,
);
expect(payload).not.toHaveProperty('reasoning_effort');
},
);
it('should handle system prompt correctly', async () => {
// Arrange
const mockStream = createHelloStream();
const mockResponse = Promise.resolve(mockStream);
(instance['client'].messages.create as Mock).mockResolvedValue(mockResponse);
// Act
const result = await instance.chat({
messages: [
{ content: 'You are an awesome greeter', role: 'system' },
{ content: 'Hello', role: 'user' },
],
model: 'claude-sonnet-4-5-20250929',
temperature: 0,
});
// Assert
expect(instance['client'].messages.create).toHaveBeenCalledWith(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-sonnet-4-5-20250929',
stream: true,
system: [
{
cache_control: { type: 'ephemeral' },
type: 'text',
text: 'You are an awesome greeter',
},
],
temperature: 0,
metadata: undefined,
tools: undefined,
top_p: undefined,
}),
expect.objectContaining({ signal: undefined }),
);
expect(result).toBeInstanceOf(Response);
});
it('should call Anthropic API with supported opions in streaming mode', async () => {
// Arrange
const mockStream = createHelloStream();
const mockResponse = Promise.resolve(mockStream);
(instance['client'].messages.create as Mock).mockResolvedValue(mockResponse);
// Act
const result = await instance.chat({
max_tokens: 2048,
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.5,
top_p: 1,
});
// Assert
expect(instance['client'].messages.create).toHaveBeenCalledWith(
expect.objectContaining({
max_tokens: 2048,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
stream: true,
temperature: 0.25,
top_p: 1,
}),
expect.objectContaining({}),
);
expect(result).toBeInstanceOf(Response);
});
it('should call Anthropic API without unsupported opions', async () => {
// Arrange
const mockStream = createHelloStream();
const mockResponse = Promise.resolve(mockStream);
(instance['client'].messages.create as Mock).mockResolvedValue(mockResponse);
// Act
const result = await instance.chat({
frequency_penalty: 0.5, // Unsupported option
max_tokens: 2048,
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
presence_penalty: 0.5,
temperature: 0.5,
top_p: 1,
});
// Assert
expect(instance['client'].messages.create).toHaveBeenCalledWith(
expect.objectContaining({
max_tokens: 2048,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
stream: true,
temperature: 0.25,
top_p: 1,
}),
expect.objectContaining({}),
);
expect(result).toBeInstanceOf(Response);
});
it('should call debugStream in DEBUG mode', async () => {
// Arrange
const mockProdStream = createHelloStream() as any;
const mockDebugStream = new ReadableStream({
start(controller) {
controller.enqueue('Debug stream content');
controller.close();
},
}) as any;
mockDebugStream.toReadableStream = () => mockDebugStream;
(instance['client'].messages.create as Mock).mockResolvedValue({
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
});
const originalDebugValue = process.env.DEBUG_ANTHROPIC_CHAT_COMPLETION;
process.env.DEBUG_ANTHROPIC_CHAT_COMPLETION = '1';
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
// Act
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
});
// Assert
expect(debugStreamModule.debugStream).toHaveBeenCalled();
// Cleanup
process.env.DEBUG_ANTHROPIC_CHAT_COMPLETION = originalDebugValue;
});
it('should convert Claude assistant reasoning signatures to thinking content', async () => {
await instance.chat({
messages: [
{ content: 'Hello', role: 'user' },
{
content: 'Here is my response.',
model: 'claude-opus-4-7',
reasoning: {
content: 'Let me think about this...',
signature: 'EuYBCkQYAiJAHnHRJG4nPBrdTlo6CmXoyE8WYoQ=',
},
role: 'assistant',
} as any,
{ content: 'Continue', role: 'user' },
],
model: 'claude-opus-4-7',
temperature: 0,
});
const payload = (instance['client'].messages.create as Mock).mock.calls[0][0];
expect(payload.messages[1]).toEqual({
content: [
{
signature: 'EuYBCkQYAiJAHnHRJG4nPBrdTlo6CmXoyE8WYoQ=',
thinking: 'Let me think about this...',
type: 'thinking',
},
{ text: 'Here is my response.', type: 'text' },
],
role: 'assistant',
});
});
it('should not convert non-Claude reasoning signatures to thinking content', async () => {
await instance.chat({
messages: [
{ content: 'Hello', role: 'user' },
{
content: 'Here is my response.',
model: 'deepseek-v4-pro',
provider: 'lobehub',
reasoning: {
content: 'DeepSeek reasoning',
signature: '340acffe-0000-4000-8000-000000000000',
},
role: 'assistant',
} as any,
{ content: 'Continue', role: 'user' },
],
model: 'claude-opus-4-7',
temperature: 0,
});
const payload = (instance['client'].messages.create as Mock).mock.calls[0][0];
expect(payload.messages[1]).toEqual({
content: 'Here is my response.',
role: 'assistant',
});
});
describe('chat with tools', () => {
it('should call tools when tools are provided', async () => {
// Arrange
const tools: ChatCompletionTool[] = [
{ function: { name: 'tool1', description: 'desc1' }, type: 'function' },
];
const spyOn = vi.spyOn(anthropicHelpers, 'buildAnthropicTools');
// Act
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
tools,
});
// Assert
expect(instance['client'].messages.create).toHaveBeenCalled();
expect(spyOn).toHaveBeenCalledWith(
[{ function: { name: 'tool1', description: 'desc1' }, type: 'function' }],
{ enabledContextCaching: true },
);
});
it('should build payload with tools and web search enabled', async () => {
const tools: ChatCompletionTool[] = [
{ function: { name: 'tool1', description: 'desc1' }, type: 'function' },
];
const mockAnthropicTools = [{ name: 'tool1', description: 'desc1' }];
vi.spyOn(anthropicHelpers, 'buildAnthropicTools').mockReturnValue(
mockAnthropicTools as any,
);
const payload: ChatStreamPayload = {
messages: [{ content: 'Search and get info', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.5,
tools,
enabledSearch: true,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(anthropicHelpers.buildAnthropicTools).toHaveBeenCalledWith(tools, {
enabledContextCaching: true,
});
// Should include both the converted tools and web search tool
expect(result.tools).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: 'tool1' }),
expect.objectContaining({ name: 'web_search', type: 'web_search_20250305' }),
]),
);
});
it('should build payload with web search enabled but no other tools', async () => {
vi.spyOn(anthropicHelpers, 'buildAnthropicTools').mockReturnValue(undefined);
const payload: ChatStreamPayload = {
messages: [{ content: 'Search for information', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.5,
enabledSearch: true,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(anthropicHelpers.buildAnthropicTools).toHaveBeenCalledWith(undefined, {
enabledContextCaching: true,
});
// Should only include web search tool
expect(result.tools).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: 'web_search', type: 'web_search_20250305' }),
]),
);
});
});
describe('Error', () => {
it('should throw InvalidAnthropicAPIKey error on API_KEY_INVALID error', async () => {
// Arrange
const apiError = {
status: 401,
error: {
type: 'error',
error: {
type: 'authentication_error',
message: 'invalid x-api-key',
},
},
};
(instance['client'].messages.create as Mock).mockRejectedValue(apiError);
try {
// Act
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
});
} catch (e) {
// Assert
expect(e).toEqual({
endpoint: 'https://api.anthropic.com',
error: apiError,
errorType: invalidErrorType,
provider,
});
}
});
it('should throw BizError error', async () => {
// Arrange
const apiError = {
status: 529,
error: {
type: 'error',
error: {
type: 'overloaded_error',
message: "Anthropic's API is temporarily overloaded",
},
},
};
(instance['client'].messages.create as Mock).mockRejectedValue(apiError);
try {
// Act
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
});
} catch (e) {
// Assert
expect(e).toEqual({
endpoint: 'https://api.anthropic.com',
error: apiError.error.error,
errorType: bizErrorType,
message: "Anthropic's API is temporarily overloaded",
provider,
});
}
});
it('should throw InvalidAnthropicAPIKey if no apiKey is provided', async () => {
try {
new LobeAnthropicAI({});
} catch (e) {
expect(e).toEqual({ errorType: invalidErrorType });
}
});
});
describe('Error handling', () => {
it('should throw LocationNotSupportError on 403 error', async () => {
// Arrange
const apiError = { status: 403 };
(instance['client'].messages.create as Mock).mockRejectedValue(apiError);
// Act & Assert
await expect(
instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
}),
).rejects.toEqual({
endpoint: 'https://api.anthropic.com',
error: apiError,
errorType: 'LocationNotSupportError',
provider,
});
});
it('should throw AnthropicBizError on other error status codes', async () => {
// Arrange
const apiError = { status: 500 };
(instance['client'].messages.create as Mock).mockRejectedValue(apiError);
// Act & Assert
await expect(
instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
}),
).rejects.toEqual({
endpoint: 'https://api.anthropic.com',
error: apiError,
errorType: bizErrorType,
provider,
});
});
it('should desensitize custom baseURL in error message', async () => {
// Arrange
const apiError = { status: 401 };
const customInstance = new LobeAnthropicAI({
apiKey: 'test',
baseURL: 'https://api.custom.com/v1',
});
vi.spyOn(customInstance['client'].messages, 'create').mockRejectedValue(apiError);
// Act & Assert
// anthropicCompatible normalizes the `/v1` suffix away (see #14960),
// then desensitizeUrl reconstructs via the WHATWG URL parser which always
// emits a trailing `/` in the pathname.
await expect(
customInstance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
}),
).rejects.toEqual({
endpoint: 'https://api.cu****om.com/',
error: apiError,
errorType: invalidErrorType,
provider,
});
});
});
describe('Options', () => {
it('should pass signal to API call', async () => {
// Arrange
const controller = new AbortController();
// Act
await instance.chat(
{
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
},
{ signal: controller.signal },
);
// Assert
expect(instance['client'].messages.create).toHaveBeenCalledWith(
expect.objectContaining({}),
expect.objectContaining({ signal: controller.signal }),
);
});
it('should apply callback to the returned stream', async () => {
// Arrange
const callback = vi.fn();
// Act
await instance.chat(
{
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0,
},
{
callback: { onStart: callback },
},
);
// Assert
expect(callback).toHaveBeenCalled();
});
it('should set headers on the response', async () => {
// Arrange
const headers = { 'X-Test-Header': 'test' };
// Act
const result = await instance.chat(
{
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
},
{ headers },
);
// Assert
expect(result.headers.get('X-Test-Header')).toBe('test');
});
});
describe('Edge cases', () => {
it('should handle empty messages array', async () => {
// Act & Assert
await expect(
instance.chat({
messages: [],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
}),
).resolves.toBeInstanceOf(Response);
});
});
describe('buildDefaultAnthropicPayload', () => {
it('should correctly build payload with user messages only', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.5,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
temperature: 0.25,
}),
);
});
it('should correctly build payload with system message', async () => {
const payload: ChatStreamPayload = {
messages: [
{ content: 'You are a helpful assistant', role: 'system' },
{ content: 'Hello', role: 'user' },
],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
system: [
{
cache_control: { type: 'ephemeral' },
text: 'You are a helpful assistant',
type: 'text',
},
],
temperature: 0.35,
}),
);
});
it('should omit top_p for Claude 4+ models when both temperature and top_p are set', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-sonnet-4-5-20250929',
temperature: 0.8,
top_p: 0.9,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
model: 'claude-sonnet-4-5-20250929',
temperature: 0.4,
top_p: undefined,
}),
);
});
it('should keep top_p for Claude 4+ models when only top_p is set', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-sonnet-4-5-20250929',
top_p: 0.9,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
model: 'claude-sonnet-4-5-20250929',
temperature: undefined,
top_p: 0.9,
}),
);
});
it('should ignore whitespace-only system prompts', async () => {
const payload: ChatStreamPayload = {
messages: [
{ content: ' \n\t ', role: 'system' },
{ content: 'Hello', role: 'user' },
],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
system: undefined,
}),
);
});
it('should correctly build payload with tools', async () => {
const tools: ChatCompletionTool[] = [
{ function: { name: 'tool1', description: 'desc1' }, type: 'function' },
];
const spyOn = vi.spyOn(anthropicHelpers, 'buildAnthropicTools').mockReturnValueOnce([
{
name: 'tool1',
description: 'desc1',
},
] as any);
const payload: ChatStreamPayload = {
messages: [{ content: 'Use a tool', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.8,
tools,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Use a tool', type: 'text' },
],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
temperature: 0.4,
tools: [{ name: 'tool1', description: 'desc1' }],
}),
);
expect(spyOn).toHaveBeenCalledWith(tools, {
enabledContextCaching: true,
});
});
it('should correctly build payload with thinking mode enabled', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.9,
thinking: { type: 'enabled', budget_tokens: 0 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 32000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
system: undefined,
thinking: { type: 'enabled', budget_tokens: 1024 },
tools: undefined,
});
});
it('should correctly build payload with adaptive thinking and effort', async () => {
const payload: ChatStreamPayload = {
max_tokens: 16000,
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-opus-4-6',
effort: 'high',
thinking: { type: 'adaptive', budget_tokens: 0 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 16000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-opus-4-6',
output_config: { effort: 'high' },
system: undefined,
thinking: { type: 'adaptive' },
tools: undefined,
});
});
it('should correctly build payload for Claude Opus 4.7 with xhigh effort', async () => {
const payload: ChatStreamPayload = {
max_tokens: 16000,
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-opus-4-7',
effort: 'xhigh',
thinking: { type: 'adaptive', budget_tokens: 0 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 16000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-opus-4-7',
output_config: { effort: 'xhigh' },
system: undefined,
// Opus 4.7 defaults `display` to `omitted`, so reasoning has to be opted into
thinking: { display: 'summarized', type: 'adaptive' },
tools: undefined,
});
});
it('should drop assistant prefill for Claude Opus 4.7', async () => {
const payload: ChatStreamPayload = {
messages: [
{ content: 'Continue this answer', role: 'user' },
{ content: 'Partial assistant draft', role: 'assistant' },
],
model: 'claude-opus-4-7',
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.messages).toEqual([
{
content: 'Continue this answer',
role: 'user',
},
]);
});
it('should drop ALL stacked trailing assistant messages', async () => {
// Failed-run placeholder rows can stack several assistant turns at the
// payload tail; popping only one still triggers the prefill 400.
const payload: ChatStreamPayload = {
messages: [
{ content: 'Continue this answer', role: 'user' },
{ content: '...', role: 'assistant' },
{ content: '...', role: 'assistant' },
],
model: 'claude-opus-5',
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.messages).toEqual([
{
content: 'Continue this answer',
role: 'user',
},
]);
});
it('should respect max_tokens in thinking mode when provided', async () => {
const payload: ChatStreamPayload = {
max_tokens: 1000,
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
thinking: { type: 'enabled', budget_tokens: 0 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 1000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
system: undefined,
thinking: { type: 'enabled', budget_tokens: 999 },
tools: undefined,
});
});
it('should use budget_tokens in thinking mode when provided', async () => {
const payload: ChatStreamPayload = {
max_tokens: 1000,
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.5,
thinking: { type: 'enabled', budget_tokens: 2000 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 1000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
system: undefined,
thinking: { type: 'enabled', budget_tokens: 999 },
tools: undefined,
});
});
it('should cap max_tokens at 64000 in thinking mode', async () => {
const payload: ChatStreamPayload = {
max_tokens: 10000,
messages: [{ content: 'Solve this problem', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.6,
thinking: { type: 'enabled', budget_tokens: 60000 },
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result).toEqual({
max_tokens: 10000,
messages: [
{
content: [
{ cache_control: { type: 'ephemeral' }, text: 'Solve this problem', type: 'text' },
],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
system: undefined,
thinking: { type: 'enabled', budget_tokens: 9999 },
tools: undefined,
});
});
it('should respect max_tokens when explicitly provided', async () => {
const payload: ChatStreamPayload = {
max_tokens: 2000,
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.max_tokens).toBe(2000);
});
it('should correctly handle temperature scaling', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 1,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.temperature).toBe(0.5); // Anthropic uses 0-1 scale, so divide by 2
});
it('should not include temperature when not provided in payload', async () => {
// We need to create a partial payload without temperature
// but since the type requires it, we'll use type assertion
const partialPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
} as ChatStreamPayload;
// Delete the temperature property to simulate it not being provided
delete (partialPayload as any).temperature;
const result = await buildDefaultAnthropicPayload(partialPayload);
expect(result.temperature).toBeUndefined();
});
it('should not include top_p when thinking is enabled', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
thinking: { type: 'enabled', budget_tokens: 0 },
top_p: 0.9,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.top_p).toBeUndefined();
});
it('should include top_p when thinking is not enabled', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
top_p: 0.9,
};
const result = await buildDefaultAnthropicPayload(payload);
expect(result.top_p).toBe(0.9);
});
it('should handle thinking with type disabled', async () => {
const payload: ChatStreamPayload = {
messages: [{ content: 'Hello', role: 'user' }],
model: 'claude-3-5-haiku-20241022',
temperature: 0.7,
thinking: { type: 'disabled', budget_tokens: 0 },
};
const result = await buildDefaultAnthropicPayload(payload);
// When thinking is disabled, it should be treated as if thinking wasn't provided
expect(result).toEqual(
expect.objectContaining({
max_tokens: 64000,
messages: [
{
content: [{ cache_control: { type: 'ephemeral' }, text: 'Hello', type: 'text' }],
role: 'user',
},
],
model: 'claude-3-5-haiku-20241022',
temperature: 0.35,
}),
);
});
});
});
});