WIP: Debugging and logging

This commit is contained in:
Oleg Ivaniv 2024-11-20 08:02:00 +01:00
parent 17ef2c63f6
commit 6d1eed8222
No known key found for this signature in database
2 changed files with 66 additions and 5 deletions

View file

@ -382,27 +382,61 @@ class AIParametersParser {
description,
schema,
func: async (functionArgs: z.infer<typeof schema>) => {
// How can we pair this index with the index in that in WorkflowDataProxy.handleFromAi?
const { index } = this.ctx.addInputData(NodeConnectionType.AiTool, [
[{ json: functionArgs }],
]);
console.log('\n\x1b%s\x1b', '🔹 Starting node-tool func execution');
console.log(
'\x1b[33m%s\x1b',
' Input data(functionArgs):',
JSON.stringify(functionArgs, null, 2),
);
try {
// Execute the node with the proxied context
const result = await node.execute?.bind(this.ctx)();
console.log('\x1b%s\x1b', '🔹 Executing node');
console.log('\n%s', '🔹 Getting input data for index: ', index);
// Hacky way to only execute input data once(per each item?)
const proxiedContext = new Proxy(this.ctx, {
get: (target, prop: keyof IExecuteFunctions) => {
if (prop === 'getInputData') {
console.log('Returning modified input data');
return () => [{ json: functionArgs }];
}
return target[prop];
},
});
const result = await node.execute?.bind(proxiedContext)();
console.log('\x1b[32m%s\x1b', '✅ Node execution successful');
console.log('\x1b[33m%s\x1b', ' Raw result:', JSON.stringify(result, null, 2));
// Process and map the results
const mappedResults = result?.[0]?.flatMap((item) => item.json);
console.log('\x1b%s\x1b', '🔹 Mapped results:');
console.log('\x1b[33m%s\x1b', JSON.stringify(mappedResults, null, 2));
// Add output data to the context
this.ctx.addOutputData(NodeConnectionType.AiTool, index, [
[{ json: { response: mappedResults } }],
]);
console.log('\x1b%s\x1b', '🔹 Output data added to context');
// Return the stringified results
return JSON.stringify(mappedResults);
} catch (error) {
console.log('\x1b[31m%s\x1b', '❌ Error during node execution:');
console.error(error);
const nodeError = new NodeOperationError(this.ctx.getNode(), error as Error);
this.ctx.addOutputData(NodeConnectionType.AiTool, index, nodeError);
console.log('\x1b[31m%s\x1b', ' Error added to context output');
return 'Error during node execution: ' + nodeError.description;
}
},

View file

@ -945,14 +945,25 @@ export class WorkflowDataProxy {
_type: string = 'string',
defaultValue?: unknown,
) => {
console.time('⏱️ fromAI execution');
console.log(
'\n🔹 Starting fromAI function, that.runIndex: ',
that.runIndex,
', that.itemIndex: ',
that.itemIndex,
);
if (!name || name === '') {
console.error('\n❌ Error: Missing key');
throw new ExpressionError("Add a key, e.g. $fromAI('placeholder_name')", {
runIndex: that.runIndex,
itemIndex: that.itemIndex,
});
}
const nameValidationRegex = /^[a-zA-Z0-9_-]{0,64}$/;
if (!nameValidationRegex.test(name)) {
console.error('\n❌ Error: Invalid parameter key');
throw new ExpressionError(
'Invalid parameter key, must be between 1 and 64 characters long and only contain lowercase letters, uppercase letters, numbers, underscores, and hyphens',
{
@ -961,19 +972,35 @@ export class WorkflowDataProxy {
},
);
}
console.log('\n🔹 Fetching placeholders data');
const nodeRunData = that.runExecutionData?.resultData.runData[that.activeNodeName] ?? [];
const lastInputOverride = nodeRunData[nodeRunData.length - 1]?.inputOverride;
const placeholdersDataInputData =
that.runExecutionData?.resultData.runData[that.activeNodeName]?.[0].inputOverride?.[
NodeConnectionType.AiTool
]?.[0]?.[0].json;
lastInputOverride?.[NodeConnectionType.AiTool]?.[0]?.[0].json;
console.log('\n📝 Placeholders data:');
console.log(` ${JSON.stringify(placeholdersDataInputData, null, 2)}`);
if (Boolean(!placeholdersDataInputData)) {
console.error('\n❌ Error: No execution data available');
throw new ExpressionError('No execution data available', {
runIndex: that.runIndex,
itemIndex: that.itemIndex,
type: 'no_execution_data',
});
}
return placeholdersDataInputData?.[name] ?? defaultValue;
const result = placeholdersDataInputData?.[name] ?? defaultValue;
console.log(`\n✅ Success: Returning value for key "${name}"`);
console.log(` Value: ${result}`);
console.log(`\n🔹 Resolving placeholder for itemIndex: ${that.itemIndex}`);
console.log(
` Fetched placeholdersDataInputData: ${JSON.stringify(placeholdersDataInputData, null, 2)}`,
);
console.timeEnd('⏱️ fromAI execution');
return result;
};
const base = {