mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
feat(Structured Output Parser Node): Refactor Output Parsers and Improve Error Handling (#11148)
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
|
||||
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
|
||||
import type { BaseChatMemory } from '@langchain/community/memory/chat_memory';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
isChatInstance,
|
||||
getPromptInputByType,
|
||||
getOptionalOutputParsers,
|
||||
getConnectedTools,
|
||||
} from '../../../../../utils/helpers';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
import { getOptionalOutputParsers } from '../../../../../utils/output_parsers/N8nOutputParser';
|
||||
import { throwIfToolSchema } from '../../../../../utils/schemaParsing';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
|
||||
export async function conversationalAgentExecute(
|
||||
this: IExecuteFunctions,
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { ChatOpenAI } from '@langchain/openai';
|
||||
import type { AgentExecutorInput } from 'langchain/agents';
|
||||
import { AgentExecutor, OpenAIAgent } from 'langchain/agents';
|
||||
import { BufferMemory, type BaseChatMemory } from 'langchain/memory';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import {
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
@@ -5,18 +12,8 @@ import {
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import type { AgentExecutorInput } from 'langchain/agents';
|
||||
import { AgentExecutor, OpenAIAgent } from 'langchain/agents';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import { BufferMemory, type BaseChatMemory } from 'langchain/memory';
|
||||
import { ChatOpenAI } from '@langchain/openai';
|
||||
import {
|
||||
getConnectedTools,
|
||||
getOptionalOutputParsers,
|
||||
getPromptInputByType,
|
||||
} from '../../../../../utils/helpers';
|
||||
import { getConnectedTools, getPromptInputByType } from '../../../../../utils/helpers';
|
||||
import { getOptionalOutputParsers } from '../../../../../utils/output_parsers/N8nOutputParser';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
|
||||
export async function openAiFunctionsAgentExecute(
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { PlanAndExecuteAgentExecutor } from 'langchain/experimental/plan_and_execute';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import {
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
@@ -5,18 +10,10 @@ import {
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import { PlanAndExecuteAgentExecutor } from 'langchain/experimental/plan_and_execute';
|
||||
import {
|
||||
getConnectedTools,
|
||||
getOptionalOutputParsers,
|
||||
getPromptInputByType,
|
||||
} from '../../../../../utils/helpers';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
import { getConnectedTools, getPromptInputByType } from '../../../../../utils/helpers';
|
||||
import { getOptionalOutputParsers } from '../../../../../utils/output_parsers/N8nOutputParser';
|
||||
import { throwIfToolSchema } from '../../../../../utils/schemaParsing';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
|
||||
export async function planAndExecuteAgentExecute(
|
||||
this: IExecuteFunctions,
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { AgentExecutor, ChatAgent, ZeroShotAgent } from 'langchain/agents';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import {
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
@@ -5,20 +11,14 @@ import {
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { AgentExecutor, ChatAgent, ZeroShotAgent } from 'langchain/agents';
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { PromptTemplate } from '@langchain/core/prompts';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import {
|
||||
getConnectedTools,
|
||||
getOptionalOutputParsers,
|
||||
getPromptInputByType,
|
||||
isChatInstance,
|
||||
} from '../../../../../utils/helpers';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
import { getOptionalOutputParsers } from '../../../../../utils/output_parsers/N8nOutputParser';
|
||||
import { throwIfToolSchema } from '../../../../../utils/schemaParsing';
|
||||
import { getTracingConfig } from '../../../../../utils/tracing';
|
||||
|
||||
export async function reActAgentAgentExecute(
|
||||
this: IExecuteFunctions,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { BaseChatMemory } from '@langchain/community/memory/chat_memory';
|
||||
import { HumanMessage } from '@langchain/core/messages';
|
||||
import type { BaseMessage } from '@langchain/core/messages';
|
||||
import type { BaseOutputParser, StructuredOutputParser } from '@langchain/core/output_parsers';
|
||||
import type { BaseMessagePromptTemplateLike } from '@langchain/core/prompts';
|
||||
import { ChatPromptTemplate } from '@langchain/core/prompts';
|
||||
import { RunnableSequence } from '@langchain/core/runnables';
|
||||
@@ -9,7 +8,6 @@ import type { Tool } from '@langchain/core/tools';
|
||||
import { DynamicStructuredTool } from '@langchain/core/tools';
|
||||
import type { AgentAction, AgentFinish } from 'langchain/agents';
|
||||
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents';
|
||||
import { OutputFixingParser } from 'langchain/output_parsers';
|
||||
import { omit } from 'lodash';
|
||||
import { BINARY_ENCODING, jsonParse, NodeConnectionType, NodeOperationError } from 'n8n-workflow';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
@@ -20,24 +18,16 @@ import { SYSTEM_MESSAGE } from './prompt';
|
||||
import {
|
||||
isChatInstance,
|
||||
getPromptInputByType,
|
||||
getOptionalOutputParsers,
|
||||
getConnectedTools,
|
||||
} from '../../../../../utils/helpers';
|
||||
import {
|
||||
getOptionalOutputParsers,
|
||||
type N8nOutputParser,
|
||||
} from '../../../../../utils/output_parsers/N8nOutputParser';
|
||||
|
||||
function getOutputParserSchema(outputParser: BaseOutputParser): ZodObject<any, any, any, any> {
|
||||
const parserType = outputParser.lc_namespace[outputParser.lc_namespace.length - 1];
|
||||
let schema: ZodObject<any, any, any, any>;
|
||||
|
||||
if (parserType === 'structured') {
|
||||
// If the output parser is a structured output parser, we will use the schema from the parser
|
||||
schema = (outputParser as StructuredOutputParser<ZodObject<any, any, any, any>>).schema;
|
||||
} else if (parserType === 'fix' && outputParser instanceof OutputFixingParser) {
|
||||
// If the output parser is a fixing parser, we will use the schema from the connected structured output parser
|
||||
schema = (outputParser.parser as StructuredOutputParser<ZodObject<any, any, any, any>>).schema;
|
||||
} else {
|
||||
// If the output parser is not a structured output parser, we will use a fallback schema
|
||||
schema = z.object({ text: z.string() });
|
||||
}
|
||||
function getOutputParserSchema(outputParser: N8nOutputParser): ZodObject<any, any, any, any> {
|
||||
const schema =
|
||||
(outputParser.getSchema() as ZodObject<any, any, any, any>) ?? z.object({ text: z.string() });
|
||||
|
||||
return schema;
|
||||
}
|
||||
@@ -205,10 +195,9 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
|
||||
const responseParserTool = steps.find((step) => step.tool === 'format_final_response');
|
||||
if (responseParserTool) {
|
||||
const toolInput = responseParserTool?.toolInput;
|
||||
const returnValues = (await outputParser.parse(toolInput as unknown as string)) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
// Check if the tool input is a string or an object and convert it to a string
|
||||
const parserInput = toolInput instanceof Object ? JSON.stringify(toolInput) : toolInput;
|
||||
const returnValues = (await outputParser.parse(parserInput)) as Record<string, unknown>;
|
||||
|
||||
return handleParsedStepOutput(returnValues);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import { HumanMessage } from '@langchain/core/messages';
|
||||
import {
|
||||
ApplicationError,
|
||||
NodeApiError,
|
||||
NodeConnectionType,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
AIMessagePromptTemplate,
|
||||
PromptTemplate,
|
||||
SystemMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
ChatPromptTemplate,
|
||||
} from '@langchain/core/prompts';
|
||||
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
||||
import { ChatOllama } from '@langchain/ollama';
|
||||
import { LLMChain } from 'langchain/chains';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import type {
|
||||
IBinaryData,
|
||||
IDataObject,
|
||||
@@ -12,28 +20,17 @@ import type {
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import {
|
||||
ApplicationError,
|
||||
NodeApiError,
|
||||
NodeConnectionType,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import {
|
||||
AIMessagePromptTemplate,
|
||||
PromptTemplate,
|
||||
SystemMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
ChatPromptTemplate,
|
||||
} from '@langchain/core/prompts';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import { CombiningOutputParser } from 'langchain/output_parsers';
|
||||
import { LLMChain } from 'langchain/chains';
|
||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||
import { HumanMessage } from '@langchain/core/messages';
|
||||
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
||||
import { ChatOllama } from '@langchain/ollama';
|
||||
import { getPromptInputByType, isChatInstance } from '../../../utils/helpers';
|
||||
import type { N8nOutputParser } from '../../../utils/output_parsers/N8nOutputParser';
|
||||
import { getOptionalOutputParsers } from '../../../utils/output_parsers/N8nOutputParser';
|
||||
import { getTemplateNoticeField } from '../../../utils/sharedFields';
|
||||
import {
|
||||
getOptionalOutputParsers,
|
||||
getPromptInputByType,
|
||||
isChatInstance,
|
||||
} from '../../../utils/helpers';
|
||||
import { getTracingConfig } from '../../../utils/tracing';
|
||||
import {
|
||||
getCustomErrorMessage as getCustomOpenAiErrorMessage,
|
||||
@@ -189,7 +186,7 @@ async function getChain(
|
||||
itemIndex: number,
|
||||
query: string,
|
||||
llm: BaseLanguageModel,
|
||||
outputParsers: BaseOutputParser[],
|
||||
outputParsers: N8nOutputParser[],
|
||||
messages?: MessagesTemplate[],
|
||||
): Promise<unknown[]> {
|
||||
const chatTemplate: ChatPromptTemplate | PromptTemplate = await getChainPromptTemplate(
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
||||
import {
|
||||
NodeConnectionType,
|
||||
type IExecuteFunctions,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
import { OutputFixingParser } from 'langchain/output_parsers';
|
||||
import type { BaseOutputParser } from '@langchain/core/output_parsers';
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import { logWrapper } from '../../../utils/logWrapper';
|
||||
import { NodeConnectionType } from 'n8n-workflow';
|
||||
import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
N8nOutputFixingParser,
|
||||
type N8nStructuredOutputParser,
|
||||
} from '../../../utils/output_parsers/N8nOutputParser';
|
||||
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
|
||||
|
||||
export class OutputParserAutofixing implements INodeType {
|
||||
@@ -75,12 +71,12 @@ export class OutputParserAutofixing implements INodeType {
|
||||
const outputParser = (await this.getInputConnectionData(
|
||||
NodeConnectionType.AiOutputParser,
|
||||
itemIndex,
|
||||
)) as BaseOutputParser;
|
||||
)) as N8nStructuredOutputParser;
|
||||
|
||||
const parser = OutputFixingParser.fromLLM(model, outputParser);
|
||||
const parser = new N8nOutputFixingParser(this, model, outputParser);
|
||||
|
||||
return {
|
||||
response: logWrapper(parser, this),
|
||||
response: parser,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { MockProxy } from 'jest-mock-extended';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { normalizeItems } from 'n8n-core';
|
||||
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
|
||||
import { ApplicationError, NodeConnectionType } from 'n8n-workflow';
|
||||
|
||||
import { N8nOutputFixingParser } from '../../../../utils/output_parsers/N8nOutputParser';
|
||||
import type { N8nStructuredOutputParser } from '../../../../utils/output_parsers/N8nOutputParser';
|
||||
import { OutputParserAutofixing } from '../OutputParserAutofixing.node';
|
||||
|
||||
describe('OutputParserAutofixing', () => {
|
||||
let outputParser: OutputParserAutofixing;
|
||||
let thisArg: MockProxy<IExecuteFunctions>;
|
||||
let mockModel: MockProxy<BaseLanguageModel>;
|
||||
let mockStructuredOutputParser: MockProxy<N8nStructuredOutputParser>;
|
||||
|
||||
beforeEach(() => {
|
||||
outputParser = new OutputParserAutofixing();
|
||||
thisArg = mock<IExecuteFunctions>({
|
||||
helpers: { normalizeItems },
|
||||
});
|
||||
mockModel = mock<BaseLanguageModel>();
|
||||
mockStructuredOutputParser = mock<N8nStructuredOutputParser>();
|
||||
|
||||
thisArg.getWorkflowDataProxy.mockReturnValue(mock<IWorkflowDataProxyData>({ $input: mock() }));
|
||||
thisArg.addInputData.mockReturnValue({ index: 0 });
|
||||
thisArg.addOutputData.mockReturnValue();
|
||||
thisArg.getInputConnectionData.mockImplementation(async (type: NodeConnectionType) => {
|
||||
if (type === NodeConnectionType.AiLanguageModel) return mockModel;
|
||||
if (type === NodeConnectionType.AiOutputParser) return mockStructuredOutputParser;
|
||||
|
||||
throw new ApplicationError('Unexpected connection type');
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
function getMockedRetryChain(output: string) {
|
||||
return jest.fn().mockReturnValue({
|
||||
invoke: jest.fn().mockResolvedValue({
|
||||
content: output,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
it('should successfully parse valid output without needing to fix it', async () => {
|
||||
const validOutput = { name: 'Alice', age: 25 };
|
||||
|
||||
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nOutputFixingParser;
|
||||
};
|
||||
|
||||
// Ensure the response contains the output-fixing parser
|
||||
expect(response).toBeDefined();
|
||||
expect(response).toBeInstanceOf(N8nOutputFixingParser);
|
||||
|
||||
const result = await response.parse('{"name": "Alice", "age": 25}');
|
||||
|
||||
// Validate that the parser succeeds without retry
|
||||
expect(result).toEqual(validOutput);
|
||||
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(1); // Only one call to parse
|
||||
});
|
||||
|
||||
it('should throw an error when both structured parser and fixing parser fail', async () => {
|
||||
mockStructuredOutputParser.parse
|
||||
.mockRejectedValueOnce(new Error('Invalid JSON')) // First attempt fails
|
||||
.mockRejectedValueOnce(new Error('Fixing attempt failed')); // Second attempt fails
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nOutputFixingParser;
|
||||
};
|
||||
|
||||
response.getRetryChain = getMockedRetryChain('{}');
|
||||
|
||||
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Fixing attempt failed');
|
||||
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should reject on the first attempt and succeed on retry with the parsed content', async () => {
|
||||
const validOutput = { name: 'Bob', age: 28 };
|
||||
|
||||
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nOutputFixingParser;
|
||||
};
|
||||
|
||||
response.getRetryChain = getMockedRetryChain(JSON.stringify(validOutput));
|
||||
|
||||
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
|
||||
|
||||
const result = await response.parse('Invalid JSON string');
|
||||
|
||||
expect(result).toEqual(validOutput);
|
||||
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second succeeds
|
||||
});
|
||||
|
||||
it('should handle non-JSON formatted response from fixing parser', async () => {
|
||||
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nOutputFixingParser;
|
||||
};
|
||||
|
||||
response.getRetryChain = getMockedRetryChain('This is not JSON');
|
||||
|
||||
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Unexpected token'));
|
||||
|
||||
// Expect the structured parser to throw an error on invalid JSON from retry
|
||||
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Unexpected token');
|
||||
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second tries and fails
|
||||
});
|
||||
});
|
||||
@@ -1,51 +0,0 @@
|
||||
import { BaseOutputParser, OutputParserException } from '@langchain/core/output_parsers';
|
||||
|
||||
export class ItemListOutputParser extends BaseOutputParser<string[]> {
|
||||
lc_namespace = ['n8n-nodes-langchain', 'output_parsers', 'list_items'];
|
||||
|
||||
private numberOfItems: number | undefined;
|
||||
|
||||
private separator: string;
|
||||
|
||||
constructor(options: { numberOfItems?: number; separator?: string }) {
|
||||
super();
|
||||
if (options.numberOfItems && options.numberOfItems > 0) {
|
||||
this.numberOfItems = options.numberOfItems;
|
||||
}
|
||||
this.separator = options.separator ?? '\\n';
|
||||
if (this.separator === '\\n') {
|
||||
this.separator = '\n';
|
||||
}
|
||||
}
|
||||
|
||||
async parse(text: string): Promise<string[]> {
|
||||
const response = text
|
||||
.split(this.separator)
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item);
|
||||
|
||||
if (this.numberOfItems && response.length < this.numberOfItems) {
|
||||
// Only error if to few items got returned, if there are to many we can autofix it
|
||||
throw new OutputParserException(
|
||||
`Wrong number of items returned. Expected ${this.numberOfItems} items but got ${response.length} items instead.`,
|
||||
);
|
||||
}
|
||||
|
||||
return response.slice(0, this.numberOfItems);
|
||||
}
|
||||
|
||||
getFormatInstructions(): string {
|
||||
const instructions = `Your response should be a list of ${
|
||||
this.numberOfItems ? this.numberOfItems + ' ' : ''
|
||||
}items separated by`;
|
||||
|
||||
const numberOfExamples = this.numberOfItems ?? 3;
|
||||
|
||||
const examples: string[] = [];
|
||||
for (let i = 1; i <= numberOfExamples; i++) {
|
||||
examples.push(`item${i}`);
|
||||
}
|
||||
|
||||
return `${instructions} "${this.separator}" (for example: "${examples.join(this.separator)}")`;
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,9 @@ import {
|
||||
type INodeTypeDescription,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
import { logWrapper } from '../../../utils/logWrapper';
|
||||
|
||||
import { N8nItemListOutputParser } from '../../../utils/output_parsers/N8nItemListOutputParser';
|
||||
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
|
||||
import { ItemListOutputParser } from './ItemListOutputParser';
|
||||
|
||||
export class OutputParserItemList implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
@@ -86,10 +86,10 @@ export class OutputParserItemList implements INodeType {
|
||||
separator?: string;
|
||||
};
|
||||
|
||||
const parser = new ItemListOutputParser(options);
|
||||
const parser = new N8nItemListOutputParser(options);
|
||||
|
||||
return {
|
||||
response: logWrapper(parser, this),
|
||||
response: parser,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { normalizeItems } from 'n8n-core';
|
||||
import {
|
||||
ApplicationError,
|
||||
type IExecuteFunctions,
|
||||
type IWorkflowDataProxyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { N8nItemListOutputParser } from '../../../../utils/output_parsers/N8nItemListOutputParser';
|
||||
import { OutputParserItemList } from '../OutputParserItemList.node';
|
||||
|
||||
describe('OutputParserItemList', () => {
|
||||
let outputParser: OutputParserItemList;
|
||||
const thisArg = mock<IExecuteFunctions>({
|
||||
helpers: { normalizeItems },
|
||||
});
|
||||
const workflowDataProxy = mock<IWorkflowDataProxyData>({ $input: mock() });
|
||||
|
||||
beforeEach(() => {
|
||||
outputParser = new OutputParserItemList();
|
||||
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
|
||||
thisArg.addInputData.mockReturnValue({ index: 0 });
|
||||
thisArg.addOutputData.mockReturnValue();
|
||||
thisArg.getNodeParameter.mockReset();
|
||||
});
|
||||
|
||||
describe('supplyData', () => {
|
||||
it('should create a parser with default options', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return {};
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
||||
});
|
||||
|
||||
it('should create a parser with custom number of items', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return { numberOfItems: 5 };
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
||||
expect((response as any).numberOfItems).toBe(5);
|
||||
});
|
||||
|
||||
it('should create a parser with custom separator', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return { separator: ',' };
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
expect(response).toBeInstanceOf(N8nItemListOutputParser);
|
||||
expect((response as any).separator).toBe(',');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parse', () => {
|
||||
it('should parse a list with default separator', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return {};
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
const result = await (response as N8nItemListOutputParser).parse('item1\nitem2\nitem3');
|
||||
expect(result).toEqual(['item1', 'item2', 'item3']);
|
||||
});
|
||||
|
||||
it('should parse a list with custom separator', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return { separator: ',' };
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
const result = await (response as N8nItemListOutputParser).parse('item1,item2,item3');
|
||||
expect(result).toEqual(['item1', 'item2', 'item3']);
|
||||
});
|
||||
|
||||
it('should limit the number of items returned', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return { numberOfItems: 2 };
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
const result = await (response as N8nItemListOutputParser).parse(
|
||||
'item1\nitem2\nitem3\nitem4',
|
||||
);
|
||||
expect(result).toEqual(['item1', 'item2']);
|
||||
});
|
||||
|
||||
it('should throw an error if not enough items are returned', async () => {
|
||||
thisArg.getNodeParameter.mockImplementation((parameterName) => {
|
||||
if (parameterName === 'options') {
|
||||
return { numberOfItems: 5 };
|
||||
}
|
||||
throw new ApplicationError('Not implemented');
|
||||
});
|
||||
|
||||
const { response } = await outputParser.supplyData.call(thisArg, 0);
|
||||
await expect(
|
||||
(response as N8nItemListOutputParser).parse('item1\nitem2\nitem3'),
|
||||
).rejects.toThrow('Wrong number of items returned');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,4 @@
|
||||
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
||||
import { OutputParserException } from '@langchain/core/output_parsers';
|
||||
import type { JSONSchema7 } from 'json-schema';
|
||||
import { StructuredOutputParser } from 'langchain/output_parsers';
|
||||
import get from 'lodash/get';
|
||||
import {
|
||||
jsonParse,
|
||||
type IExecuteFunctions,
|
||||
@@ -12,77 +8,17 @@ import {
|
||||
NodeOperationError,
|
||||
NodeConnectionType,
|
||||
} from 'n8n-workflow';
|
||||
import { z } from 'zod';
|
||||
import type { z } from 'zod';
|
||||
|
||||
import {
|
||||
inputSchemaField,
|
||||
jsonSchemaExampleField,
|
||||
schemaTypeField,
|
||||
} from '../../../utils/descriptions';
|
||||
import { logWrapper } from '../../../utils/logWrapper';
|
||||
import { N8nStructuredOutputParser } from '../../../utils/output_parsers/N8nOutputParser';
|
||||
import { convertJsonSchemaToZod, generateSchema } from '../../../utils/schemaParsing';
|
||||
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
|
||||
|
||||
const STRUCTURED_OUTPUT_KEY = '__structured__output';
|
||||
const STRUCTURED_OUTPUT_OBJECT_KEY = '__structured__output__object';
|
||||
const STRUCTURED_OUTPUT_ARRAY_KEY = '__structured__output__array';
|
||||
|
||||
export class N8nStructuredOutputParser<T extends z.ZodTypeAny> extends StructuredOutputParser<T> {
|
||||
async parse(text: string): Promise<z.infer<T>> {
|
||||
try {
|
||||
const parsed = (await super.parse(text)) as object;
|
||||
|
||||
return (
|
||||
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_OBJECT_KEY]) ??
|
||||
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_ARRAY_KEY]) ??
|
||||
get(parsed, STRUCTURED_OUTPUT_KEY) ??
|
||||
parsed
|
||||
);
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line n8n-nodes-base/node-execute-block-wrong-error-thrown
|
||||
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
||||
}
|
||||
}
|
||||
|
||||
static async fromZedSchema(
|
||||
zodSchema: z.ZodSchema<object>,
|
||||
nodeVersion: number,
|
||||
): Promise<StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>> {
|
||||
let returnSchema: z.ZodSchema<object>;
|
||||
if (nodeVersion === 1) {
|
||||
returnSchema = z.object({
|
||||
[STRUCTURED_OUTPUT_KEY]: z
|
||||
.object({
|
||||
[STRUCTURED_OUTPUT_OBJECT_KEY]: zodSchema.optional(),
|
||||
[STRUCTURED_OUTPUT_ARRAY_KEY]: z.array(zodSchema).optional(),
|
||||
})
|
||||
.describe(
|
||||
`Wrapper around the output data. It can only contain ${STRUCTURED_OUTPUT_OBJECT_KEY} or ${STRUCTURED_OUTPUT_ARRAY_KEY} but never both.`,
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
// Validate that one and only one of the properties exists
|
||||
return (
|
||||
Boolean(data[STRUCTURED_OUTPUT_OBJECT_KEY]) !==
|
||||
Boolean(data[STRUCTURED_OUTPUT_ARRAY_KEY])
|
||||
);
|
||||
},
|
||||
{
|
||||
message:
|
||||
'One and only one of __structured__output__object and __structured__output__array should be present.',
|
||||
path: [STRUCTURED_OUTPUT_KEY],
|
||||
},
|
||||
),
|
||||
});
|
||||
} else {
|
||||
returnSchema = z.object({
|
||||
output: zodSchema.optional(),
|
||||
});
|
||||
}
|
||||
|
||||
return N8nStructuredOutputParser.fromZodSchema(returnSchema);
|
||||
}
|
||||
}
|
||||
export class OutputParserStructured implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Structured Output Parser',
|
||||
@@ -205,9 +141,13 @@ export class OutputParserStructured implements INodeType {
|
||||
const zodSchema = convertJsonSchemaToZod<z.ZodSchema<object>>(jsonSchema);
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
try {
|
||||
const parser = await N8nStructuredOutputParser.fromZedSchema(zodSchema, nodeVersion);
|
||||
const parser = await N8nStructuredOutputParser.fromZodJsonSchema(
|
||||
zodSchema,
|
||||
nodeVersion,
|
||||
this,
|
||||
);
|
||||
return {
|
||||
response: logWrapper(parser, this),
|
||||
response: parser,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(this.getNode(), 'Error during parsing of JSON Schema.');
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import type { IExecuteFunctions, INode, IWorkflowDataProxyData } from 'n8n-workflow';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { normalizeItems } from 'n8n-core';
|
||||
import type { z } from 'zod';
|
||||
import type { StructuredOutputParser } from 'langchain/output_parsers';
|
||||
import {
|
||||
jsonParse,
|
||||
type IExecuteFunctions,
|
||||
type INode,
|
||||
type IWorkflowDataProxyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import type { N8nStructuredOutputParser } from '../../../../utils/output_parsers/N8nStructuredOutputParser';
|
||||
import { OutputParserStructured } from '../OutputParserStructured.node';
|
||||
|
||||
describe('OutputParserStructured', () => {
|
||||
@@ -11,139 +16,451 @@ describe('OutputParserStructured', () => {
|
||||
helpers: { normalizeItems },
|
||||
});
|
||||
const workflowDataProxy = mock<IWorkflowDataProxyData>({ $input: mock() });
|
||||
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
|
||||
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.1 }));
|
||||
thisArg.addInputData.mockReturnValue({ index: 0 });
|
||||
thisArg.addOutputData.mockReturnValue();
|
||||
|
||||
beforeEach(() => {
|
||||
outputParser = new OutputParserStructured();
|
||||
thisArg.getWorkflowDataProxy.mockReturnValue(workflowDataProxy);
|
||||
thisArg.addInputData.mockReturnValue({ index: 0 });
|
||||
thisArg.addOutputData.mockReturnValue();
|
||||
});
|
||||
|
||||
describe('supplyData', () => {
|
||||
it('should parse a valid JSON schema', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
describe('Version 1.1 and below', () => {
|
||||
beforeEach(() => {
|
||||
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.1 }));
|
||||
});
|
||||
|
||||
it('should parse a complex nested schema', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"details": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"age": { "type": "number" },
|
||||
"hobbies": { "type": "array", "items": { "type": "string" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"timestamp": { "type": "string", "format": "date-time" }
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
|
||||
};
|
||||
const outputObject = { output: { name: 'Mac', age: 27 } };
|
||||
const parsersOutput = await response.parse(`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
it('should handle missing required properties', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
|
||||
};
|
||||
const outputObject = { output: { name: 'Mac' } };
|
||||
|
||||
await expect(
|
||||
response.parse(`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`),
|
||||
).rejects.toThrow('Required');
|
||||
});
|
||||
|
||||
it('should throw on wrong type', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
|
||||
};
|
||||
const outputObject = { output: { name: 'Mac', age: '27' } };
|
||||
|
||||
await expect(
|
||||
response.parse(`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`),
|
||||
).rejects.toThrow('Expected number, received string');
|
||||
});
|
||||
|
||||
it('should parse array output', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"myArr": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
"required": ["user", "timestamp"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
user: {
|
||||
name: 'Alice',
|
||||
details: {
|
||||
age: 30,
|
||||
hobbies: ['reading', 'hiking'],
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["myArr"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: StructuredOutputParser<z.ZodType<object, z.ZodTypeDef, object>>;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
myArr: [
|
||||
{ name: 'Mac', age: 27 },
|
||||
{ name: 'Alice', age: 25 },
|
||||
],
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
},
|
||||
timestamp: '2023-04-01T12:00:00Z',
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the complex output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should handle optional fields correctly', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"age": { "type": "number" },
|
||||
"email": { "type": "string", "format": "email" }
|
||||
},
|
||||
"required": ["name"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
name: 'Bob',
|
||||
email: 'bob@example.com',
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the output with optional fields:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should handle arrays of objects', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"users": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": { "type": "number" },
|
||||
"name": { "type": "string" }
|
||||
},
|
||||
"required": ["id", "name"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["users"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
users: [
|
||||
{ id: 1, name: 'Alice' },
|
||||
{ id: 2, name: 'Bob' },
|
||||
],
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the array output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should handle empty objects', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"required": ["data"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
data: {},
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the empty object output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should throw error for null values in non-nullable fields', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"age": { "type": "number" }
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('jsonSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
name: 'Charlie',
|
||||
age: null,
|
||||
},
|
||||
};
|
||||
|
||||
await expect(
|
||||
response.parse(
|
||||
`Here's the output with null value:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`,
|
||||
undefined,
|
||||
(e) => e,
|
||||
),
|
||||
).rejects.toThrow('Expected number, received null');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Version 1.2 and above', () => {
|
||||
beforeEach(() => {
|
||||
thisArg.getNode.mockReturnValue(mock<INode>({ typeVersion: 1.2 }));
|
||||
});
|
||||
|
||||
it('should parse output using schema generated from complex JSON example', async () => {
|
||||
const jsonExample = `{
|
||||
"user": {
|
||||
"name": "Alice",
|
||||
"details": {
|
||||
"age": 30,
|
||||
"address": {
|
||||
"street": "123 Main St",
|
||||
"city": "Anytown",
|
||||
"zipCode": "12345"
|
||||
}
|
||||
}
|
||||
},
|
||||
"orders": [
|
||||
{
|
||||
"id": "ORD-001",
|
||||
"items": ["item1", "item2"],
|
||||
"total": 50.99
|
||||
},
|
||||
{
|
||||
"id": "ORD-002",
|
||||
"items": ["item3"],
|
||||
"total": 25.50
|
||||
}
|
||||
],
|
||||
"isActive": true
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
|
||||
thisArg.getNodeParameter
|
||||
.calledWith('jsonSchemaExample', 0)
|
||||
.mockReturnValueOnce(jsonExample);
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
|
||||
const outputObject = {
|
||||
output: jsonParse(jsonExample),
|
||||
};
|
||||
|
||||
const parsersOutput = await response.parse(`Here's the complex output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should validate enum values', async () => {
|
||||
const inputSchema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"color": {
|
||||
"type": "string",
|
||||
"enum": ["red", "green", "blue"]
|
||||
}
|
||||
},
|
||||
"required": ["color"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
|
||||
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(inputSchema);
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
|
||||
const validOutput = {
|
||||
output: {
|
||||
color: 'green',
|
||||
},
|
||||
};
|
||||
|
||||
const invalidOutput = {
|
||||
output: {
|
||||
color: 'yellow',
|
||||
},
|
||||
};
|
||||
|
||||
await expect(
|
||||
response.parse(`Valid output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(validOutput)}
|
||||
\`\`\`
|
||||
`),
|
||||
).resolves.toEqual(validOutput);
|
||||
|
||||
await expect(
|
||||
response.parse(
|
||||
`Invalid output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(invalidOutput)}
|
||||
\`\`\`
|
||||
`,
|
||||
undefined,
|
||||
(e) => e,
|
||||
),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should handle recursive structures', async () => {
|
||||
const inputSchema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"children": {
|
||||
"type": "array",
|
||||
"items": { "$ref": "#" }
|
||||
}
|
||||
},
|
||||
"required": ["name"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
|
||||
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(inputSchema);
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
|
||||
const outputObject = {
|
||||
output: {
|
||||
name: 'Root',
|
||||
children: [
|
||||
{
|
||||
name: 'Child1',
|
||||
children: [{ name: 'Grandchild1' }, { name: 'Grandchild2' }],
|
||||
},
|
||||
{
|
||||
name: 'Child2',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const parsersOutput = await response.parse(`Here's the recursive structure output:
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
|
||||
it('should handle missing required properties', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('manual');
|
||||
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
|
||||
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = { output: { name: 'Mac' } };
|
||||
|
||||
await expect(
|
||||
response.parse(
|
||||
`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`,
|
||||
undefined,
|
||||
(e) => e,
|
||||
),
|
||||
).rejects.toThrow('Required');
|
||||
});
|
||||
it('should throw on wrong type', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = { output: { name: 'Mac', age: '27' } };
|
||||
|
||||
await expect(
|
||||
response.parse(
|
||||
`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`,
|
||||
undefined,
|
||||
(e) => e,
|
||||
),
|
||||
).rejects.toThrow('Expected number, received string');
|
||||
});
|
||||
|
||||
it('should parse array output', async () => {
|
||||
const schema = `{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"myArr": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["myArr"]
|
||||
}`;
|
||||
thisArg.getNodeParameter.calledWith('inputSchema', 0).mockReturnValueOnce(schema);
|
||||
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
|
||||
response: N8nStructuredOutputParser;
|
||||
};
|
||||
const outputObject = {
|
||||
output: {
|
||||
myArr: [
|
||||
{ name: 'Mac', age: 27 },
|
||||
{ name: 'Alice', age: 25 },
|
||||
],
|
||||
},
|
||||
};
|
||||
const parsersOutput = await response.parse(`Here's the output!
|
||||
\`\`\`json
|
||||
${JSON.stringify(outputObject)}
|
||||
\`\`\`
|
||||
`);
|
||||
|
||||
expect(parsersOutput).toEqual(outputObject);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user