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,192 @@
import type { ILoadOptionsFunctions } from 'n8n-workflow';
import OpenAI from 'openai';
import { searchModels } from '../loadModels';
jest.mock('openai');
describe('searchModels', () => {
let mockContext: jest.Mocked<ILoadOptionsFunctions>;
let mockOpenAI: jest.Mocked<typeof OpenAI>;
beforeEach(() => {
mockContext = {
getCredentials: jest.fn().mockResolvedValue({
apiKey: 'test-api-key',
}),
getNodeParameter: jest.fn().mockReturnValue(''),
} as unknown as jest.Mocked<ILoadOptionsFunctions>;
// Setup OpenAI mock with required properties
const mockOpenAIInstance = {
apiKey: 'test-api-key',
organization: null,
project: null,
_options: {},
models: {
list: jest.fn().mockResolvedValue({
data: [
{ id: 'gpt-4' },
{ id: 'gpt-3.5-turbo' },
{ id: 'gpt-3.5-turbo-instruct' },
{ id: 'ft:gpt-3.5-turbo' },
{ id: 'o1-model' },
{ id: 'whisper-1' },
{ id: 'davinci-instruct-beta' },
{ id: 'computer-use-preview' },
{ id: 'whisper-1-preview' },
{ id: 'tts-model' },
{ id: 'other-model' },
],
}),
},
} as unknown as OpenAI;
(OpenAI as jest.MockedClass<typeof OpenAI>).mockImplementation(() => mockOpenAIInstance);
mockOpenAI = OpenAI as jest.Mocked<typeof OpenAI>;
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return filtered models if custom API endpoint is not provided', async () => {
const result = await searchModels.call(mockContext);
expect(mockOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: 'https://api.openai.com/v1',
apiKey: 'test-api-key',
}),
);
expect(result.results).toEqual([
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-4', value: 'gpt-4' },
{ name: 'o1-model', value: 'o1-model' },
{ name: 'other-model', value: 'other-model' },
]);
});
it('should initialize OpenAI with correct credentials', async () => {
mockContext.getCredentials.mockResolvedValueOnce({
apiKey: 'test-api-key',
url: 'https://test-url.com',
});
await searchModels.call(mockContext);
expect(mockOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: 'https://test-url.com',
apiKey: 'test-api-key',
}),
);
});
it('should use default OpenAI URL if no custom URL provided', async () => {
mockContext.getCredentials = jest.fn().mockResolvedValue({
apiKey: 'test-api-key',
});
await searchModels.call(mockContext);
expect(mockOpenAI).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: 'https://api.openai.com/v1',
apiKey: 'test-api-key',
}),
);
});
it('should include all models for custom API endpoints', async () => {
mockContext.getNodeParameter = jest.fn().mockReturnValue('https://custom-api.com');
const result = await searchModels.call(mockContext);
expect(result.results).toEqual([
{ name: 'computer-use-preview', value: 'computer-use-preview' },
{ name: 'davinci-instruct-beta', value: 'davinci-instruct-beta' },
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo-instruct', value: 'gpt-3.5-turbo-instruct' },
{ name: 'gpt-4', value: 'gpt-4' },
{ name: 'o1-model', value: 'o1-model' },
{ name: 'other-model', value: 'other-model' },
{ name: 'tts-model', value: 'tts-model' },
{ name: 'whisper-1', value: 'whisper-1' },
{ name: 'whisper-1-preview', value: 'whisper-1-preview' },
]);
expect(result.results).toHaveLength(11);
});
it('should treat ai-assistant.n8n.io as official API', async () => {
mockContext.getCredentials.mockResolvedValueOnce({
apiKey: 'test-api-key',
url: 'https://ai-assistant.n8n.io/v1',
});
const result = await searchModels.call(mockContext);
expect(result.results).toEqual([
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-4', value: 'gpt-4' },
{ name: 'o1-model', value: 'o1-model' },
{ name: 'other-model', value: 'other-model' },
]);
});
it('should filter models based on search term', async () => {
const result = await searchModels.call(mockContext, 'gpt');
expect(result.results).toEqual([
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-4', value: 'gpt-4' },
]);
});
it('should handle case-insensitive search', async () => {
const result = await searchModels.call(mockContext, 'GPT');
expect(result.results).toEqual([
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-4', value: 'gpt-4' },
]);
});
it('should return models sorted alphabetically by id', async () => {
// Setup a mock with scrambled order
const mockUnsortedInstance = {
apiKey: 'test-api-key',
models: {
list: jest.fn().mockResolvedValue({
data: [
{ id: 'gpt-4' },
{ id: 'a-model' },
{ id: 'o1-model' },
{ id: 'gpt-3.5-turbo' },
{ id: 'z-model' },
],
}),
},
} as unknown as OpenAI;
(OpenAI as jest.MockedClass<typeof OpenAI>).mockImplementation(() => mockUnsortedInstance);
// Custom API endpoint to include all models
mockContext.getNodeParameter = jest.fn().mockReturnValue('https://custom-api.com');
const result = await searchModels.call(mockContext);
// Verify the results are sorted alphabetically
expect(result.results).toEqual([
{ name: 'a-model', value: 'a-model' },
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
{ name: 'gpt-4', value: 'gpt-4' },
{ name: 'o1-model', value: 'o1-model' },
{ name: 'z-model', value: 'z-model' },
]);
});
});
@@ -0,0 +1,49 @@
import type { ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
import OpenAI from 'openai';
import { shouldIncludeModel } from '../../../vendors/OpenAi/helpers/modelFiltering';
import { getProxyAgent } from '@n8n/ai-utilities';
import { Container } from '@n8n/di';
import { AiConfig } from '@n8n/config';
export async function searchModels(
this: ILoadOptionsFunctions,
filter?: string,
): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials('openAiApi');
const baseURL =
(this.getNodeParameter('options.baseURL', '') as string) ||
(credentials.url as string) ||
'https://api.openai.com/v1';
const { openAiDefaultHeaders: defaultHeaders } = Container.get(AiConfig);
const openai = new OpenAI({
baseURL,
apiKey: credentials.apiKey as string,
fetchOptions: {
dispatcher: getProxyAgent(baseURL),
},
defaultHeaders,
});
const { data: models = [] } = await openai.models.list();
const url = baseURL && new URL(baseURL);
const isCustomAPI = !!(url && !['api.openai.com', 'ai-assistant.n8n.io'].includes(url.hostname));
const filteredModels = models.filter((model: { id: string }) => {
const includeModel = shouldIncludeModel(model.id, isCustomAPI);
if (!filter) return includeModel;
return includeModel && model.id.toLowerCase().includes(filter.toLowerCase());
});
filteredModels.sort((a, b) => a.id.localeCompare(b.id));
return {
results: filteredModels.map((model: { id: string }) => ({
name: model.id,
value: model.id,
})),
};
}