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
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:
@@ -0,0 +1,150 @@
|
||||
import { SearxngSearch } from '@langchain/community/tools/searxng_search';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INode,
|
||||
INodeExecutionData,
|
||||
ISupplyDataFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ToolSearXng } from './ToolSearXng.node';
|
||||
|
||||
describe('ToolSearXng', () => {
|
||||
describe('supplyData', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should return SearXNG tool instance', async () => {
|
||||
const node = new ToolSearXng();
|
||||
|
||||
const supplyDataResult = await node.supplyData.call(
|
||||
mock<ISupplyDataFunctions>({
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test searxng' })),
|
||||
getCredentials: jest.fn().mockResolvedValue({ apiUrl: 'https://searx.example.com' }),
|
||||
getNodeParameter: jest.fn().mockReturnValue({}),
|
||||
}),
|
||||
0,
|
||||
);
|
||||
|
||||
expect(supplyDataResult.response).toBeInstanceOf(SearxngSearch);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should execute SearXNG search and return result', async () => {
|
||||
const node = new ToolSearXng();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'artificial intelligence' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test searxng' })),
|
||||
getCredentials: jest.fn().mockResolvedValue({ apiUrl: 'https://searx.example.com' }),
|
||||
getNodeParameter: jest.fn().mockReturnValue({}),
|
||||
});
|
||||
|
||||
// Mock the SearxngSearch.invoke method
|
||||
const mockResult = 'Search results for artificial intelligence...';
|
||||
SearxngSearch.prototype.invoke = jest.fn().mockResolvedValue(mockResult);
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: mockResult,
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(SearxngSearch.prototype.invoke).toHaveBeenCalledWith({
|
||||
query: 'artificial intelligence',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple input items', async () => {
|
||||
const node = new ToolSearXng();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'machine learning' },
|
||||
},
|
||||
{
|
||||
json: { query: 'deep learning' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test searxng' })),
|
||||
getCredentials: jest.fn().mockResolvedValue({ apiUrl: 'https://searx.example.com' }),
|
||||
getNodeParameter: jest.fn().mockReturnValue({}),
|
||||
});
|
||||
|
||||
// Mock the SearxngSearch.invoke method
|
||||
SearxngSearch.prototype.invoke = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce('Machine learning search results')
|
||||
.mockResolvedValueOnce('Deep learning search results');
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: 'Machine learning search results',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
json: {
|
||||
response: 'Deep learning search results',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(SearxngSearch.prototype.invoke).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should handle credentials and options correctly', async () => {
|
||||
const node = new ToolSearXng();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'test query' },
|
||||
},
|
||||
];
|
||||
|
||||
const testOptions = { engines: ['google'], safesearch: 1 };
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test searxng' })),
|
||||
getCredentials: jest.fn().mockResolvedValue({ apiUrl: 'https://searx.test.com' }),
|
||||
getNodeParameter: jest.fn().mockReturnValue(testOptions),
|
||||
});
|
||||
|
||||
SearxngSearch.prototype.invoke = jest.fn().mockResolvedValue('test result');
|
||||
|
||||
await node.execute.call(mockExecute);
|
||||
|
||||
expect(mockExecute.getCredentials).toHaveBeenCalledWith('searXngApi');
|
||||
expect(mockExecute.getNodeParameter).toHaveBeenCalledWith('options', 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,147 @@
|
||||
import { SearxngSearch } from '@langchain/community/tools/searxng_search';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
ISupplyDataFunctions,
|
||||
SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
type Options = {
|
||||
numResults: number;
|
||||
pageNumber: number;
|
||||
language: string;
|
||||
safesearch: 0 | 1 | 2;
|
||||
};
|
||||
|
||||
async function getTool(ctx: ISupplyDataFunctions | IExecuteFunctions, itemIndex: number) {
|
||||
const credentials = await ctx.getCredentials<{ apiUrl: string }>('searXngApi');
|
||||
const options = ctx.getNodeParameter('options', itemIndex) as Options;
|
||||
|
||||
return new SearxngSearch({
|
||||
apiBase: credentials.apiUrl,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
params: options,
|
||||
});
|
||||
}
|
||||
|
||||
export class ToolSearXng implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'SearXNG',
|
||||
name: 'toolSearXng',
|
||||
icon: 'file:searXng.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Search in SearXNG',
|
||||
defaults: {
|
||||
name: 'SearXNG',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Tools'],
|
||||
Tools: ['Other Tools'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolsearxng',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
inputs: [],
|
||||
outputs: [NodeConnectionTypes.AiTool],
|
||||
outputNames: ['Tool'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'searXngApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiAgent]),
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Number of Results',
|
||||
name: 'numResults',
|
||||
type: 'number',
|
||||
default: 10,
|
||||
},
|
||||
{
|
||||
displayName: 'Search Page Number',
|
||||
name: 'pageNumber',
|
||||
type: 'number',
|
||||
default: 1,
|
||||
},
|
||||
{
|
||||
displayName: 'Language',
|
||||
name: 'language',
|
||||
type: 'string',
|
||||
default: 'en',
|
||||
description:
|
||||
'Defines the language to use. It\'s a two-letter language code. (e.g., `en` for English, `es` for Spanish, or `fr` for French). Head to <a href="https://docs.searxng.org/user/search-syntax.html#select-language">SearXNG search syntax page</a> for more info.',
|
||||
},
|
||||
{
|
||||
displayName: 'Safe Search',
|
||||
name: 'safesearch',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'None',
|
||||
value: 0,
|
||||
},
|
||||
{
|
||||
name: 'Moderate',
|
||||
value: 1,
|
||||
},
|
||||
{
|
||||
name: 'Strict',
|
||||
value: 2,
|
||||
},
|
||||
],
|
||||
default: 0,
|
||||
description: 'Filter search results of engines which support safe search',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
return {
|
||||
response: logWrapper(await getTool(this, itemIndex), this),
|
||||
};
|
||||
}
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const result: INodeExecutionData[] = [];
|
||||
const input = this.getInputData();
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const item = input[i];
|
||||
const tool = await getTool(this, i);
|
||||
result.push({
|
||||
json: {
|
||||
response: await tool.invoke(item.json),
|
||||
},
|
||||
pairedItem: {
|
||||
item: i,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return [result];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<svg height="92mm" viewBox="0 0 92 92" width="92mm" xmlns="http://www.w3.org/2000/svg"><g transform="translate(-40.921303 -17.416526)"><g fill="none"><circle cx="75" cy="92" r="0" stroke="#000" stroke-width="12"/><circle cx="75.921" cy="53.903" r="30" stroke="#3050ff" stroke-width="10"/><path d="m67.514849 37.91524a18 18 0 0 1 21.051475 3.312407 18 18 0 0 1 3.137312 21.078282" stroke="#3050ff" stroke-width="5"/></g><path d="m3.706 122.09h18.846v39.963h-18.846z" fill="#3050ff" transform="matrix(.69170581 -.72217939 .72217939 .69170581 0 0)"/></g></svg>
|
||||
|
After Width: | Height: | Size: 558 B |
Reference in New Issue
Block a user