mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix: Continue on fail / error output support for chains and agents (#9078)
This commit is contained in:
@@ -320,67 +320,76 @@ export class OpenAiAssistant implements INodeType {
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
const input = this.getNodeParameter('text', itemIndex) as string;
|
||||
const assistantId = this.getNodeParameter('assistantId', itemIndex, '') as string;
|
||||
const nativeTools = this.getNodeParameter('nativeTools', itemIndex, []) as Array<
|
||||
'code_interpreter' | 'retrieval'
|
||||
>;
|
||||
try {
|
||||
const input = this.getNodeParameter('text', itemIndex) as string;
|
||||
const assistantId = this.getNodeParameter('assistantId', itemIndex, '') as string;
|
||||
const nativeTools = this.getNodeParameter('nativeTools', itemIndex, []) as Array<
|
||||
'code_interpreter' | 'retrieval'
|
||||
>;
|
||||
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as {
|
||||
baseURL?: string;
|
||||
maxRetries: number;
|
||||
timeout: number;
|
||||
};
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as {
|
||||
baseURL?: string;
|
||||
maxRetries: number;
|
||||
timeout: number;
|
||||
};
|
||||
|
||||
if (input === undefined) {
|
||||
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
|
||||
}
|
||||
if (input === undefined) {
|
||||
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
|
||||
}
|
||||
|
||||
const client = new OpenAIClient({
|
||||
apiKey: credentials.apiKey as string,
|
||||
maxRetries: options.maxRetries ?? 2,
|
||||
timeout: options.timeout ?? 10000,
|
||||
baseURL: options.baseURL,
|
||||
});
|
||||
let agent;
|
||||
const nativeToolsParsed: OpenAIToolType = nativeTools.map((tool) => ({ type: tool }));
|
||||
const transformedConnectedTools = tools?.map(formatToOpenAIAssistantTool) ?? [];
|
||||
const newTools = [...transformedConnectedTools, ...nativeToolsParsed];
|
||||
|
||||
// Existing agent, update tools with currently assigned
|
||||
if (assistantId) {
|
||||
agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });
|
||||
|
||||
await client.beta.assistants.update(assistantId, {
|
||||
tools: newTools,
|
||||
const client = new OpenAIClient({
|
||||
apiKey: credentials.apiKey as string,
|
||||
maxRetries: options.maxRetries ?? 2,
|
||||
timeout: options.timeout ?? 10000,
|
||||
baseURL: options.baseURL,
|
||||
});
|
||||
} else {
|
||||
const name = this.getNodeParameter('name', itemIndex, '') as string;
|
||||
const instructions = this.getNodeParameter('instructions', itemIndex, '') as string;
|
||||
const model = this.getNodeParameter('model', itemIndex, 'gpt-3.5-turbo-1106') as string;
|
||||
let agent;
|
||||
const nativeToolsParsed: OpenAIToolType = nativeTools.map((tool) => ({ type: tool }));
|
||||
const transformedConnectedTools = tools?.map(formatToOpenAIAssistantTool) ?? [];
|
||||
const newTools = [...transformedConnectedTools, ...nativeToolsParsed];
|
||||
|
||||
agent = await OpenAIAssistantRunnable.createAssistant({
|
||||
model,
|
||||
client,
|
||||
instructions,
|
||||
name,
|
||||
tools: newTools,
|
||||
asAgent: true,
|
||||
// Existing agent, update tools with currently assigned
|
||||
if (assistantId) {
|
||||
agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });
|
||||
|
||||
await client.beta.assistants.update(assistantId, {
|
||||
tools: newTools,
|
||||
});
|
||||
} else {
|
||||
const name = this.getNodeParameter('name', itemIndex, '') as string;
|
||||
const instructions = this.getNodeParameter('instructions', itemIndex, '') as string;
|
||||
const model = this.getNodeParameter('model', itemIndex, 'gpt-3.5-turbo-1106') as string;
|
||||
|
||||
agent = await OpenAIAssistantRunnable.createAssistant({
|
||||
model,
|
||||
client,
|
||||
instructions,
|
||||
name,
|
||||
tools: newTools,
|
||||
asAgent: true,
|
||||
});
|
||||
}
|
||||
|
||||
const agentExecutor = AgentExecutor.fromAgentAndTools({
|
||||
agent,
|
||||
tools,
|
||||
});
|
||||
|
||||
const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
|
||||
content: input,
|
||||
signal: this.getExecutionCancelSignal(),
|
||||
timeout: options.timeout ?? 10000,
|
||||
});
|
||||
|
||||
returnData.push({ json: response });
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const agentExecutor = AgentExecutor.fromAgentAndTools({
|
||||
agent,
|
||||
tools,
|
||||
});
|
||||
|
||||
const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
|
||||
content: input,
|
||||
signal: this.getExecutionCancelSignal(),
|
||||
timeout: options.timeout ?? 10000,
|
||||
});
|
||||
|
||||
returnData.push({ json: response });
|
||||
}
|
||||
|
||||
return await this.prepareOutputData(returnData);
|
||||
|
||||
Reference in New Issue
Block a user