mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
fix(Item List Output Parser Node): Fix number of items parameter issue (#11696)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
This commit is contained in:
parent
a025848ec4
commit
01ebe9dd38
|
@ -35,6 +35,7 @@ describe('OutputParserItemList', () => {
|
||||||
|
|
||||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||||
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
||||||
|
expect((response as any).numberOfItems).toBe(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a parser with custom number of items', async () => {
|
it('should create a parser with custom number of items', async () => {
|
||||||
|
@ -50,6 +51,20 @@ describe('OutputParserItemList', () => {
|
||||||
expect((response as any).numberOfItems).toBe(5);
|
expect((response as any).numberOfItems).toBe(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should create a parser with unlimited number of items', async () => {
|
||||||
|
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||||
|
if (parameterName === 'options') {
|
||||||
|
return { numberOfItems: -1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ApplicationError('Not implemented');
|
||||||
|
});
|
||||||
|
|
||||||
|
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||||
|
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
||||||
|
expect((response as any).numberOfItems).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
it('should create a parser with custom separator', async () => {
|
it('should create a parser with custom separator', async () => {
|
||||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||||
if (parameterName === 'options') {
|
if (parameterName === 'options') {
|
||||||
|
|
|
@ -3,16 +3,21 @@ import { BaseOutputParser, OutputParserException } from '@langchain/core/output_
|
||||||
export class N8nItemListOutputParser extends BaseOutputParser<string[]> {
|
export class N8nItemListOutputParser extends BaseOutputParser<string[]> {
|
||||||
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
|
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
|
||||||
|
|
||||||
private numberOfItems: number = 3;
|
private numberOfItems: number | undefined;
|
||||||
|
|
||||||
private separator: string;
|
private separator: string;
|
||||||
|
|
||||||
constructor(options: { numberOfItems?: number; separator?: string }) {
|
constructor(options: { numberOfItems?: number; separator?: string }) {
|
||||||
super();
|
super();
|
||||||
if (options.numberOfItems && options.numberOfItems > 0) {
|
|
||||||
this.numberOfItems = options.numberOfItems;
|
const { numberOfItems = 3, separator = '\n' } = options;
|
||||||
|
|
||||||
|
if (numberOfItems && numberOfItems > 0) {
|
||||||
|
this.numberOfItems = numberOfItems;
|
||||||
}
|
}
|
||||||
this.separator = options.separator ?? '\\n';
|
|
||||||
|
this.separator = separator;
|
||||||
|
|
||||||
if (this.separator === '\\n') {
|
if (this.separator === '\\n') {
|
||||||
this.separator = '\n';
|
this.separator = '\n';
|
||||||
}
|
}
|
||||||
|
@ -39,7 +44,7 @@ export class N8nItemListOutputParser extends BaseOutputParser<string[]> {
|
||||||
this.numberOfItems ? this.numberOfItems + ' ' : ''
|
this.numberOfItems ? this.numberOfItems + ' ' : ''
|
||||||
}items separated by`;
|
}items separated by`;
|
||||||
|
|
||||||
const numberOfExamples = this.numberOfItems;
|
const numberOfExamples = this.numberOfItems ?? 3; // Default number of examples in case numberOfItems is not set
|
||||||
|
|
||||||
const examples: string[] = [];
|
const examples: string[] = [];
|
||||||
for (let i = 1; i <= numberOfExamples; i++) {
|
for (let i = 1; i <= numberOfExamples; i++) {
|
||||||
|
|
Loading…
Reference in a new issue