first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,563 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
/* eslint-disable @typescript-eslint/unbound-method */
import { ChatAnthropic } from '@langchain/anthropic';
import { N8nLlmTracing, makeN8nLlmFailedAttemptHandler, getProxyAgent } from '@n8n/ai-utilities';
import { createMockExecuteFunction } from 'n8n-nodes-base/test/nodes/Helpers';
import type { ILoadOptionsFunctions, INode, ISupplyDataFunctions } from 'n8n-workflow';
import { LmChatAnthropic } from '../LMChatAnthropic/LmChatAnthropic.node';
jest.mock('@langchain/anthropic');
jest.mock('@n8n/ai-utilities');
const MockedChatAnthropic = jest.mocked(ChatAnthropic);
const MockedN8nLlmTracing = jest.mocked(N8nLlmTracing);
const mockedMakeN8nLlmFailedAttemptHandler = jest.mocked(makeN8nLlmFailedAttemptHandler);
const mockedGetProxyAgent = jest.mocked(getProxyAgent);
describe('LmChatAnthropic', () => {
let lmChatAnthropic: LmChatAnthropic;
let mockContext: jest.Mocked<ISupplyDataFunctions>;
const mockNode: INode = {
id: '1',
name: 'Anthropic Chat Model',
typeVersion: 1.3,
type: 'n8n-nodes-langchain.lmChatAnthropic',
position: [0, 0],
parameters: {},
};
const setupMockContext = (nodeOverrides: Partial<INode> = {}) => {
const node = { ...mockNode, ...nodeOverrides };
mockContext = createMockExecuteFunction<ISupplyDataFunctions>(
{},
node,
) as jest.Mocked<ISupplyDataFunctions>;
// Setup default mocks
mockContext.getCredentials = jest.fn().mockResolvedValue({
apiKey: 'test-api-key',
});
mockContext.getNode = jest.fn().mockReturnValue(node);
mockContext.getNodeParameter = jest.fn();
// Mock the constructors/functions properly
MockedN8nLlmTracing.mockImplementation(() => ({}) as any);
mockedMakeN8nLlmFailedAttemptHandler.mockReturnValue(jest.fn());
mockedGetProxyAgent.mockReturnValue({} as any);
return mockContext;
};
beforeEach(() => {
lmChatAnthropic = new LmChatAnthropic();
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});
describe('node description', () => {
it('should have correct node properties', () => {
expect(lmChatAnthropic.description).toMatchObject({
displayName: 'Anthropic Chat Model',
name: 'lmChatAnthropic',
group: ['transform'],
version: [1, 1.1, 1.2, 1.3],
description: 'Language Model Anthropic',
});
});
it('should have correct credentials configuration', () => {
expect(lmChatAnthropic.description.credentials).toEqual([
{
name: 'anthropicApi',
required: true,
},
]);
});
it('should have correct output configuration', () => {
expect(lmChatAnthropic.description.outputs).toEqual(['ai_languageModel']);
expect(lmChatAnthropic.description.outputNames).toEqual(['Model']);
});
});
describe('supplyData', () => {
it('should create ChatAnthropic instance with basic configuration (version >= 1.3)', async () => {
const mockContext = setupMockContext({ typeVersion: 1.3 });
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
const result = await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(mockContext.getCredentials).toHaveBeenCalledWith('anthropicApi');
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('model.value', 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('options', 0, {});
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
anthropicApiUrl: 'https://api.anthropic.com',
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
expect(result).toEqual({
response: expect.any(Object),
});
});
it('should create ChatAnthropic instance with basic configuration (version < 1.3)', async () => {
const mockContext = setupMockContext({ typeVersion: 1.2 });
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model') return 'claude-3-5-sonnet-20240620';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('model', 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('options', 0, {});
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-3-5-sonnet-20240620',
anthropicApiUrl: 'https://api.anthropic.com',
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
});
it('should handle custom baseURL from credentials', async () => {
const customURL = 'https://custom-anthropic.example.com';
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
url: customURL,
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
anthropicApiUrl: customURL,
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
});
it('should handle custom headers from credentials', async () => {
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
header: true,
headerName: 'X-Custom-Header',
headerValue: 'custom-value',
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
anthropicApiUrl: 'https://api.anthropic.com',
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
defaultHeaders: {
'X-Custom-Header': 'custom-value',
},
},
}),
);
});
it('should handle all available options', async () => {
const mockContext = setupMockContext();
const options = {
maxTokensToSample: 1000,
temperature: 0.8,
topK: 5,
topP: 0.9,
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return options;
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
maxTokens: 1000,
temperature: 0.8,
topK: 5,
topP: 0.9,
anthropicApiUrl: 'https://api.anthropic.com',
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
});
it('should handle thinking mode', async () => {
const mockContext = setupMockContext();
const options = {
thinking: true,
thinkingBudget: 2048,
maxTokensToSample: 4096,
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return options;
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
maxTokens: 4096,
anthropicApiUrl: 'https://api.anthropic.com',
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {
thinking: {
type: 'enabled',
budget_tokens: 2048,
},
max_tokens: 4096,
top_k: undefined,
top_p: undefined,
temperature: undefined,
},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
});
it('should create N8nLlmTracing callback', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedN8nLlmTracing).toHaveBeenCalledWith(mockContext, {
tokensUsageParser: expect.any(Function),
});
});
it('should create failed attempt handler', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(mockedMakeN8nLlmFailedAttemptHandler).toHaveBeenCalledWith(mockContext, undefined);
});
it('should not add custom headers when header toggle is disabled', async () => {
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
header: false,
headerName: 'X-Custom-Header',
headerValue: 'custom-value',
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
clientOptions: {
fetchOptions: {
dispatcher: {},
},
},
}),
);
// Verify that defaultHeaders is not set
const callArgs = MockedChatAnthropic.mock.calls[0]?.[0];
expect(callArgs?.clientOptions?.defaultHeaders).toBeUndefined();
});
it('should handle custom headers and custom URL together', async () => {
const customURL = 'https://custom-anthropic.example.com';
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
url: customURL,
header: true,
headerName: 'X-Custom-Header',
headerValue: 'custom-value',
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'claude-sonnet-4-20250514';
if (paramName === 'options') return {};
return undefined;
});
await lmChatAnthropic.supplyData.call(mockContext, 0);
expect(MockedChatAnthropic).toHaveBeenCalledWith(
expect.objectContaining({
anthropicApiKey: 'test-api-key',
model: 'claude-sonnet-4-20250514',
anthropicApiUrl: customURL,
callbacks: expect.arrayContaining([expect.any(Object)]),
onFailedAttempt: expect.any(Function),
invocationKwargs: {},
clientOptions: {
fetchOptions: {
dispatcher: {},
},
defaultHeaders: {
'X-Custom-Header': 'custom-value',
},
},
}),
);
});
});
describe('methods', () => {
describe('searchModels', () => {
let mockLoadContext: ILoadOptionsFunctions;
let mockGetCredentials: jest.Mock;
let mockHttpRequest: jest.Mock;
beforeEach(() => {
mockGetCredentials = jest.fn();
mockHttpRequest = jest.fn();
mockLoadContext = {
getCredentials: mockGetCredentials,
helpers: {
httpRequestWithAuthentication: mockHttpRequest,
},
} as unknown as ILoadOptionsFunctions;
});
it('should return all models sorted by creation date', async () => {
const mockModels = [
{
id: 'claude-2',
display_name: 'Claude 2',
type: 'chat',
created_at: '2023-01-01T00:00:00Z',
},
{
id: 'claude-3-opus-20240229',
display_name: 'Claude 3 Opus',
type: 'chat',
created_at: '2024-02-29T00:00:00Z',
},
{
id: 'claude-3-sonnet-20240229',
display_name: 'Claude 3 Sonnet',
type: 'chat',
created_at: '2024-02-29T00:00:00Z',
},
];
mockGetCredentials.mockResolvedValue({});
mockHttpRequest.mockResolvedValue({
data: mockModels,
});
const { searchModels } = lmChatAnthropic.methods.listSearch;
const result = await searchModels.call(mockLoadContext);
expect(mockHttpRequest).toHaveBeenCalledWith('anthropicApi', {
url: 'https://api.anthropic.com/v1/models',
headers: {
'anthropic-version': '2023-06-01',
},
});
expect(result.results).toHaveLength(3);
// Verify sorted by creation date (newest first)
expect(result.results[0].value).toBe('claude-3-opus-20240229');
expect(result.results[0].name).toBe('Claude 3 Opus');
expect(result.results[2].value).toBe('claude-2');
});
it('should filter models by search term', async () => {
const mockModels = [
{
id: 'claude-2',
display_name: 'Claude 2',
type: 'chat',
created_at: '2023-01-01T00:00:00Z',
},
{
id: 'claude-3-opus-20240229',
display_name: 'Claude 3 Opus',
type: 'chat',
created_at: '2024-02-29T00:00:00Z',
},
{
id: 'claude-3-sonnet-20240229',
display_name: 'Claude 3 Sonnet',
type: 'chat',
created_at: '2024-02-29T00:00:00Z',
},
];
mockGetCredentials.mockResolvedValue({});
mockHttpRequest.mockResolvedValue({
data: mockModels,
});
const { searchModels } = lmChatAnthropic.methods.listSearch;
const result = await searchModels.call(mockLoadContext, 'opus');
expect(result.results).toHaveLength(1);
expect(result.results[0].value).toBe('claude-3-opus-20240229');
expect(result.results[0].name).toBe('Claude 3 Opus');
});
it('should filter models case-insensitively', async () => {
const mockModels = [
{
id: 'claude-3-sonnet-20240229',
display_name: 'Claude 3 Sonnet',
type: 'chat',
created_at: '2024-02-29T00:00:00Z',
},
];
mockGetCredentials.mockResolvedValue({});
mockHttpRequest.mockResolvedValue({
data: mockModels,
});
const { searchModels } = lmChatAnthropic.methods.listSearch;
const result = await searchModels.call(mockLoadContext, 'SONNET');
expect(result.results).toHaveLength(1);
expect(result.results[0].value).toBe('claude-3-sonnet-20240229');
});
it('should use custom URL from credentials', async () => {
const customURL = 'https://custom-anthropic.example.com';
mockGetCredentials.mockResolvedValue({
url: customURL,
});
mockHttpRequest.mockResolvedValue({
data: [],
});
const { searchModels } = lmChatAnthropic.methods.listSearch;
await searchModels.call(mockLoadContext);
expect(mockHttpRequest).toHaveBeenCalledWith('anthropicApi', {
url: `${customURL}/v1/models`,
headers: {
'anthropic-version': '2023-06-01',
},
});
});
it('should handle empty model list', async () => {
mockGetCredentials.mockResolvedValue({});
mockHttpRequest.mockResolvedValue({
data: [],
});
const { searchModels } = lmChatAnthropic.methods.listSearch;
const result = await searchModels.call(mockLoadContext);
expect(result.results).toHaveLength(0);
});
});
});
});
@@ -0,0 +1,597 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
/* eslint-disable @typescript-eslint/unbound-method */
import { ChatOpenAI } from '@langchain/openai';
import { makeN8nLlmFailedAttemptHandler, N8nLlmTracing, getProxyAgent } from '@n8n/ai-utilities';
import { AiConfig } from '@n8n/config';
import { Container } from '@n8n/di';
import { createMockExecuteFunction } from 'n8n-nodes-base/test/nodes/Helpers';
import type { IDataObject, INode, ISupplyDataFunctions } from 'n8n-workflow';
import * as common from '../LMChatOpenAi/common';
import { LmChatOpenAi } from '../LMChatOpenAi/LmChatOpenAi.node';
jest.mock('@langchain/openai');
jest.mock('@n8n/ai-utilities');
jest.mock('../LMChatOpenAi/common');
const MockedChatOpenAI = jest.mocked(ChatOpenAI);
const MockedN8nLlmTracing = jest.mocked(N8nLlmTracing);
const mockedMakeN8nLlmFailedAttemptHandler = jest.mocked(makeN8nLlmFailedAttemptHandler);
const mockedCommon = jest.mocked(common);
const mockedGetProxyAgent = jest.mocked(getProxyAgent);
const { openAiDefaultHeaders: defaultHeaders } = Container.get(AiConfig);
describe('LmChatOpenAi', () => {
let lmChatOpenAi: LmChatOpenAi;
let mockContext: jest.Mocked<ISupplyDataFunctions>;
const mockNode: INode = {
id: '1',
name: 'OpenAI Chat Model',
typeVersion: 1.2,
type: 'n8n-nodes-langchain.lmChatOpenAi',
position: [0, 0],
parameters: {},
};
const setupMockContext = (nodeOverrides: Partial<INode> = {}) => {
const node = { ...mockNode, ...nodeOverrides };
mockContext = createMockExecuteFunction<ISupplyDataFunctions>(
{},
node,
) as jest.Mocked<ISupplyDataFunctions>;
// Setup default mocks
mockContext.getCredentials = jest.fn().mockResolvedValue({
apiKey: 'test-api-key',
});
mockContext.getNode = jest.fn().mockReturnValue(node);
mockContext.getNodeParameter = jest.fn();
// Mock the constructors/functions properly
MockedN8nLlmTracing.mockImplementation(() => ({}) as any);
mockedMakeN8nLlmFailedAttemptHandler.mockReturnValue(jest.fn());
mockedGetProxyAgent.mockReturnValue({} as any);
return mockContext;
};
beforeEach(() => {
lmChatOpenAi = new LmChatOpenAi();
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});
describe('node description', () => {
it('should have correct node properties', () => {
expect(lmChatOpenAi.description).toMatchObject({
displayName: 'OpenAI Chat Model',
name: 'lmChatOpenAi',
group: ['transform'],
version: [1, 1.1, 1.2, 1.3],
description: 'For advanced usage with an AI chain',
});
});
it('should have correct credentials configuration', () => {
expect(lmChatOpenAi.description.credentials).toEqual([
{
name: 'openAiApi',
required: true,
},
]);
});
it('should have correct output configuration', () => {
expect(lmChatOpenAi.description.outputs).toEqual(['ai_languageModel']);
expect(lmChatOpenAi.description.outputNames).toEqual(['Model']);
});
});
describe('supplyData', () => {
it('should create ChatOpenAI instance with basic configuration (version >= 1.2)', async () => {
const mockContext = setupMockContext({ typeVersion: 1.2 });
// Mock getNodeParameter to handle the proper parameter names for v1.2
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
const result = await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(mockContext.getCredentials).toHaveBeenCalledWith('openAiApi');
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('model.value', 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('options', 0, {});
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
maxRetries: 2,
configuration: {
defaultHeaders,
fetchOptions: {
dispatcher: {},
},
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {},
onFailedAttempt: expect.any(Function),
}),
);
expect(result).toEqual({
response: expect.any(Object),
});
});
it('should create ChatOpenAI instance with basic configuration (version < 1.2)', async () => {
const mockContext = setupMockContext({ typeVersion: 1.1 });
// Mock getNodeParameter to handle the proper parameter names for v1.1
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('model', 0);
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('options', 0, {});
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
maxRetries: 2,
configuration: {
defaultHeaders,
fetchOptions: {
dispatcher: {},
},
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {},
onFailedAttempt: expect.any(Function),
}),
);
});
it('should handle custom baseURL from options', async () => {
const customBaseURL = 'https://custom-api.example.com/v1';
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options')
return {
baseURL: customBaseURL,
timeout: 30000,
maxRetries: 5,
};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
baseURL: customBaseURL,
timeout: 30000,
maxRetries: 5,
configuration: {
baseURL: customBaseURL,
fetchOptions: {
dispatcher: {},
},
defaultHeaders,
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {},
onFailedAttempt: expect.any(Function),
}),
);
});
it('should handle custom baseURL from credentials', async () => {
const customURL = 'https://custom-openai.example.com/v1';
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
url: customURL,
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
maxRetries: 2,
configuration: {
baseURL: customURL,
fetchOptions: {
dispatcher: {},
},
defaultHeaders,
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {},
onFailedAttempt: expect.any(Function),
}),
);
});
it('should handle custom headers from credentials', async () => {
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
header: true,
headerName: 'X-Custom-Header',
headerValue: 'custom-value',
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
maxRetries: 2,
configuration: {
defaultHeaders: {
...defaultHeaders,
'X-Custom-Header': 'custom-value',
},
fetchOptions: {
dispatcher: {},
},
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {},
onFailedAttempt: expect.any(Function),
}),
);
});
it('should handle all available options v1.2', async () => {
const mockContext = setupMockContext({ typeVersion: 1.2 });
const options = {
frequencyPenalty: 0.5,
maxTokens: 1000,
presencePenalty: 0.3,
temperature: 0.8,
topP: 0.9,
timeout: 45000,
maxRetries: 3,
responseFormat: 'json_object' as const,
reasoningEffort: 'high' as const,
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return options;
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: 'test-api-key',
model: 'gpt-4o-mini',
frequencyPenalty: 0.5,
maxTokens: 1000,
presencePenalty: 0.3,
temperature: 0.8,
topP: 0.9,
timeout: 45000,
maxRetries: 3,
configuration: {
defaultHeaders,
fetchOptions: {
dispatcher: {},
},
},
callbacks: expect.arrayContaining([expect.any(Object)]),
modelKwargs: {
response_format: { type: 'json_object' },
reasoning_effort: 'high',
},
onFailedAttempt: expect.any(Function),
}),
);
});
it('should only add valid reasoning effort to modelKwargs', async () => {
const mockContext = setupMockContext();
const options = {
reasoningEffort: 'invalid' as 'low' | 'medium' | 'high',
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return options;
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-4o-mini',
modelKwargs: {}, // Should not include invalid reasoning_effort
}),
);
});
it('should create N8nLlmTracing callback', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedN8nLlmTracing).toHaveBeenCalledWith(mockContext);
});
it('should create failed attempt handler', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(mockedMakeN8nLlmFailedAttemptHandler).toHaveBeenCalledWith(
mockContext,
expect.any(Function), // openAiFailedAttemptHandler
);
});
it('should use default values for maxRetries when not provided', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
// timeout is now controlled at the undici level via fetchOptions dispatcher
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
maxRetries: 2,
}),
);
});
it('should set supportsStrictToolCalling to false for OpenAI-compatible backends', async () => {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
supportsStrictToolCalling: false,
}),
);
});
it('should prioritize options.baseURL over credentials.url', async () => {
const optionsBaseURL = 'https://options-api.example.com/v1';
const credentialsURL = 'https://credentials-api.example.com/v1';
const mockContext = setupMockContext();
mockContext.getCredentials.mockResolvedValue({
apiKey: 'test-api-key',
url: credentialsURL,
});
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options')
return {
baseURL: optionsBaseURL,
};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
configuration: {
baseURL: optionsBaseURL,
fetchOptions: {
dispatcher: {},
},
defaultHeaders,
},
}),
);
});
it('should handle text response format correctly v1.2', async () => {
const mockContext = setupMockContext({ typeVersion: 1.2 });
const options = {
responseFormat: 'text' as const,
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return options;
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
modelKwargs: {
response_format: { type: 'text' },
},
}),
);
});
it('should handle all reasoning effort values correctly', async () => {
const reasoningEffortValues = ['low', 'medium', 'high'] as const;
for (const effort of reasoningEffortValues) {
const mockContext = setupMockContext();
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options')
return {
reasoningEffort: effort,
};
return undefined;
});
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
modelKwargs: {
reasoning_effort: effort,
},
}),
);
jest.clearAllMocks();
}
});
});
describe('methods', () => {
beforeEach(() => {
setupMockContext();
});
it('should have searchModels method', () => {
expect(lmChatOpenAi.methods).toEqual({
listSearch: {
searchModels: expect.any(Function),
},
});
});
it('should force Responses API and include additional params for v1.3', async () => {
const mockContext = setupMockContext({ typeVersion: 1.3 });
const options: IDataObject = {
conversationId: 'conv_123',
promptCacheKey: 'cache_key_1',
safetyIdentifier: 'user-42',
serviceTier: 'priority' as const,
topLogprobs: 10,
metadata: '{"team":"ai"}',
textFormat: {
textOptions: [{ type: 'json_object', verbosity: 'high' }],
},
promptConfig: {
promptOptions: [{ promptId: 'p_1', version: '1', variables: '{"name":"n8n"}' }],
},
};
const mockResponsesParams = {
custom: true,
};
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'responsesApiEnabled') return true;
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return options;
return undefined;
});
mockedCommon.prepareAdditionalResponsesParams = jest
.fn()
.mockReturnValue(mockResponsesParams);
mockedCommon.formatBuiltInTools = jest.fn().mockReturnValue([]);
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(mockedCommon.prepareAdditionalResponsesParams).toHaveBeenCalledWith(options);
expect(MockedChatOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
useResponsesApi: true,
modelKwargs: mockResponsesParams,
}),
);
});
it('should attach built-in tools to model metadata (v1.3)', async () => {
const mockContext = setupMockContext({ typeVersion: 1.3 });
const builtInTools: IDataObject = {
webSearch: { searchContextSize: 'high', allowedDomains: 'google.com, wikipedia.org' },
fileSearch: { vectorStoreIds: '["vs_1"]', filters: '{}', maxResults: 2 },
codeInterpreter: true,
};
const mockTools = [
{
customTools: true,
},
];
mockContext.getNodeParameter = jest.fn().mockImplementation((paramName: string) => {
if (paramName === 'responsesApiEnabled') return true;
if (paramName === 'model.value') return 'gpt-4o-mini';
if (paramName === 'options') return {};
if (paramName === 'builtInTools') return builtInTools;
return undefined;
});
mockedCommon.formatBuiltInTools = jest.fn().mockReturnValue(mockTools);
await lmChatOpenAi.supplyData.call(mockContext, 0);
expect(mockedCommon.formatBuiltInTools).toHaveBeenCalledWith(builtInTools);
const instance: unknown = MockedChatOpenAI.mock.instances[0];
expect(instance).toBeDefined();
expect((instance as { metadata?: { tools?: unknown } }).metadata).toBeDefined();
expect((instance as { metadata?: { tools?: unknown } }).metadata?.tools).toEqual(mockTools);
});
});
});
@@ -0,0 +1,390 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { Serialized } from '@langchain/core/load/serializable';
import type { LLMResult } from '@langchain/core/outputs';
import { mock } from 'jest-mock-extended';
import type { IDataObject, ISupplyDataFunctions } from 'n8n-workflow';
import { NodeOperationError, NodeApiError } from 'n8n-workflow';
import { N8nLlmTracing } from '@n8n/ai-utilities';
describe('N8nLlmTracing', () => {
const executionFunctions = mock<ISupplyDataFunctions>({
addInputData: jest.fn().mockReturnValue({ index: 0 }),
addOutputData: jest.fn(),
getNode: jest.fn().mockReturnValue({ name: 'TestNode' }),
getNextRunIndex: jest.fn().mockReturnValue(1),
});
beforeEach(() => {
jest.clearAllMocks();
});
describe('tokensUsageParser', () => {
it('should parse OpenAI format tokens correctly', () => {
const tracer = new N8nLlmTracing(executionFunctions);
const llmResult: LLMResult = {
generations: [],
llmOutput: {
tokenUsage: {
completionTokens: 100,
promptTokens: 50,
},
},
};
const result = tracer.options.tokensUsageParser(llmResult);
expect(result).toEqual({
completionTokens: 100,
promptTokens: 50,
totalTokens: 150,
});
});
it('should handle missing token data', () => {
const tracer = new N8nLlmTracing(executionFunctions);
const llmResult: LLMResult = {
generations: [],
};
const result = tracer.options.tokensUsageParser(llmResult);
expect(result).toEqual({
completionTokens: 0,
promptTokens: 0,
totalTokens: 0,
});
});
it('should handle undefined llmOutput', () => {
const tracer = new N8nLlmTracing(executionFunctions);
const llmResult: LLMResult = {
generations: [],
llmOutput: undefined,
};
const result = tracer.options.tokensUsageParser(llmResult);
expect(result).toEqual({
completionTokens: 0,
promptTokens: 0,
totalTokens: 0,
});
});
it('should use custom tokensUsageParser when provided', () => {
// Custom parser for Cohere format
const customParser = (result: LLMResult) => {
let totalInputTokens = 0;
let totalOutputTokens = 0;
result.generations?.forEach((generationArray) => {
generationArray.forEach((gen) => {
const inputTokens = gen.generationInfo?.meta?.tokens?.inputTokens ?? 0;
const outputTokens = gen.generationInfo?.meta?.tokens?.outputTokens ?? 0;
totalInputTokens += inputTokens;
totalOutputTokens += outputTokens;
});
});
return {
completionTokens: totalOutputTokens,
promptTokens: totalInputTokens,
totalTokens: totalInputTokens + totalOutputTokens,
};
};
const tracer = new N8nLlmTracing(executionFunctions, {
tokensUsageParser: customParser,
});
const llmResult: LLMResult = {
generations: [
[
{
text: 'Response 1',
generationInfo: {
meta: {
tokens: {
inputTokens: 30,
outputTokens: 40,
},
},
},
},
],
[
{
text: 'Response 2',
generationInfo: {
meta: {
tokens: {
inputTokens: 20,
outputTokens: 60,
},
},
},
},
],
],
};
const result = tracer.options.tokensUsageParser(llmResult);
expect(result).toEqual({
completionTokens: 100, // 40 + 60
promptTokens: 50, // 30 + 20
totalTokens: 150,
});
});
it('should handle Anthropic format with custom parser', () => {
const anthropicParser = (result: LLMResult) => {
const usage = (result?.llmOutput?.usage as {
input_tokens: number;
output_tokens: number;
}) ?? {
input_tokens: 0,
output_tokens: 0,
};
return {
completionTokens: usage.output_tokens,
promptTokens: usage.input_tokens,
totalTokens: usage.input_tokens + usage.output_tokens,
};
};
const tracer = new N8nLlmTracing(executionFunctions, {
tokensUsageParser: anthropicParser,
});
const llmResult: LLMResult = {
generations: [],
llmOutput: {
usage: {
input_tokens: 75,
output_tokens: 125,
},
},
};
const result = tracer.options.tokensUsageParser(llmResult);
expect(result).toEqual({
completionTokens: 125,
promptTokens: 75,
totalTokens: 200,
});
});
});
describe('handleLLMEnd', () => {
it('should process LLM output and use token usage when available', async () => {
const tracer = new N8nLlmTracing(executionFunctions);
const runId = 'test-run-id';
// Set up run details
tracer.runsMap[runId] = {
index: 0,
messages: ['Test prompt'],
options: { model: 'test-model' },
};
const output: LLMResult = {
generations: [
[
{
text: 'Test response',
generationInfo: { meta: {} },
},
],
],
llmOutput: {
tokenUsage: {
completionTokens: 50,
promptTokens: 25,
},
},
};
await tracer.handleLLMEnd(output, runId);
expect(executionFunctions.addOutputData).toHaveBeenCalledWith(
'ai_languageModel',
0,
[
[
{
json: expect.objectContaining({
response: { generations: output.generations },
tokenUsage: {
completionTokens: 50,
promptTokens: 25,
totalTokens: 75,
},
}),
},
],
],
undefined,
undefined,
);
});
it('should use token estimates when actual usage is not available', async () => {
const tracer = new N8nLlmTracing(executionFunctions);
const runId = 'test-run-id';
// Set up run details and prompt estimate
tracer.runsMap[runId] = {
index: 0,
messages: ['Test prompt'],
options: { model: 'test-model' },
};
tracer.promptTokensEstimate = 30;
const output: LLMResult = {
generations: [
[
{
text: 'Test response',
generationInfo: { meta: {} },
},
],
],
llmOutput: {},
};
jest.spyOn(tracer, 'estimateTokensFromGeneration').mockResolvedValue(45);
await tracer.handleLLMEnd(output, runId);
expect(executionFunctions.addOutputData).toHaveBeenCalledWith(
'ai_languageModel',
0,
[
[
{
json: expect.objectContaining({
response: { generations: output.generations },
tokenUsageEstimate: {
completionTokens: 45,
promptTokens: 30,
totalTokens: 75,
},
}),
},
],
],
undefined,
undefined,
);
});
});
describe('handleLLMError', () => {
it('should handle NodeError with custom error description mapper', async () => {
const customMapper = jest.fn().mockReturnValue('Mapped error description');
const tracer = new N8nLlmTracing(executionFunctions, {
errorDescriptionMapper: customMapper,
});
const runId = 'test-run-id';
tracer.runsMap[runId] = { index: 0, messages: [], options: {} };
const error = new NodeApiError(executionFunctions.getNode(), {
message: 'Test error',
description: 'Original description',
});
await tracer.handleLLMError(error, runId);
expect(customMapper).toHaveBeenCalledWith(error);
expect(error.description).toBe('Mapped error description');
expect(executionFunctions.addOutputData).toHaveBeenCalledWith('ai_languageModel', 0, error);
});
it('should wrap non-NodeError in NodeOperationError', async () => {
const tracer = new N8nLlmTracing(executionFunctions);
const runId = 'test-run-id';
tracer.runsMap[runId] = { index: 0, messages: [], options: {} };
const error = new Error('Regular error');
await tracer.handleLLMError(error, runId);
expect(executionFunctions.addOutputData).toHaveBeenCalledWith(
'ai_languageModel',
0,
expect.any(NodeOperationError),
);
});
it('should filter out non-x- headers from error objects', async () => {
const tracer = new N8nLlmTracing(executionFunctions);
const runId = 'test-run-id';
tracer.runsMap[runId] = { index: 0, messages: [], options: {} };
const error = {
message: 'API Error',
headers: {
'x-request-id': 'keep-this',
authorization: 'remove-this',
'x-rate-limit': 'keep-this-too',
'content-type': 'remove-this-too',
},
};
await tracer.handleLLMError(error as IDataObject, runId);
expect(error.headers).toEqual({
'x-request-id': 'keep-this',
'x-rate-limit': 'keep-this-too',
});
});
});
describe('handleLLMStart', () => {
it('should estimate tokens and create run details', async () => {
const tracer = new N8nLlmTracing(executionFunctions);
const runId = 'test-run-id';
const prompts = ['Prompt 1', 'Prompt 2'];
jest.spyOn(tracer, 'estimateTokensFromStringList').mockResolvedValue(100);
const llm = {
type: 'constructor',
kwargs: { model: 'test-model' },
};
await tracer.handleLLMStart(llm as unknown as Serialized, prompts, runId);
expect(tracer.estimateTokensFromStringList).toHaveBeenCalledWith(prompts);
expect(tracer.promptTokensEstimate).toBe(100);
expect(tracer.runsMap[runId]).toEqual({
index: 0,
options: { model: 'test-model' },
messages: prompts,
});
expect(executionFunctions.addInputData).toHaveBeenCalledWith(
'ai_languageModel',
[
[
{
json: {
messages: prompts,
estimatedTokens: 100,
options: { model: 'test-model' },
},
},
],
],
undefined,
);
});
});
});