n8n-nodes-query-retriever-rerank
Version:
Advanced n8n community node for intelligent document retrieval with multi-step reasoning, reranking, and comprehensive debugging
179 lines (178 loc) • 8.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugInspector = void 0;
class DebugInspector {
constructor() {
this.description = {
displayName: 'Debug Inspector',
name: 'debugInspector',
group: ['transform'],
version: 1,
description: 'Pass-through node that adds Query Retriever debug information to agent responses',
defaults: {
name: 'Debug Inspector',
},
inputs: [
{
displayName: 'Agent Response',
maxConnections: 1,
type: "main" /* NodeConnectionType.Main */,
},
],
outputs: [
{
displayName: 'Enhanced Response',
type: "main" /* NodeConnectionType.Main */,
},
],
properties: [
{
displayName: 'Debug Output Format',
name: 'outputFormat',
type: 'options',
default: 'separate_output',
description: 'How to include the debug information',
options: [
{
name: 'Separate Output Item',
value: 'separate_output',
description: 'Pass through agent response + add debug as separate output item',
},
{
name: 'Add to Response JSON',
value: 'add_to_json',
description: 'Add debug field to the agent response JSON',
},
{
name: 'Analysis Summary Only',
value: 'summary_only',
description: 'Add only the AI-generated analysis summary',
},
],
},
{
displayName: 'Include Debug History',
name: 'includeHistory',
type: 'boolean',
default: false,
description: 'Include historical debug data from previous executions',
},
{
displayName: 'Filter by Node Name',
name: 'filterNodeName',
type: 'string',
default: '',
placeholder: 'Query Retriever with Rerank',
description: 'Only include debug data from nodes with this name (leave empty for all)',
},
],
};
}
async execute() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const items = this.getInputData();
const outputFormat = this.getNodeParameter('outputFormat', 0);
const includeHistory = this.getNodeParameter('includeHistory', 0);
const filterNodeName = this.getNodeParameter('filterNodeName', 0);
const returnData = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const item = items[itemIndex];
// Get debug data from node static data
const nodeStaticData = this.getWorkflowStaticData('node');
// Get latest debug data (stored by QueryRetriever node)
const latestDebug = nodeStaticData.lastDebugData;
// Get debug history (also stored by QueryRetriever in its node context)
// Note: This will only work if the Debug Inspector can access the QueryRetriever's node data
// For now, let's just use an empty array and focus on latest debug data
const debugHistory = [];
// Filter by node name if specified
const filteredHistory = filterNodeName
? debugHistory.filter(entry => entry.nodeName === filterNodeName)
: debugHistory;
// Prepare debug information
const debugInfo = {};
if (latestDebug) {
debugInfo.latest = {
node: latestDebug.nodeName,
timestamp: latestDebug.timestamp,
analysis: latestDebug.analysis,
metrics: {
strategy: (_a = latestDebug.fullMetrics) === null || _a === void 0 ? void 0 : _a.strategy,
timing: (_b = latestDebug.fullMetrics) === null || _b === void 0 ? void 0 : _b.timing,
documentFlow: (_c = latestDebug.fullMetrics) === null || _c === void 0 ? void 0 : _c.documentFlow,
effectiveness: ((_f = (_e = (_d = latestDebug.fullMetrics) === null || _d === void 0 ? void 0 : _d.reranking) === null || _e === void 0 ? void 0 : _e.multiQueryFinalRerank) === null || _f === void 0 ? void 0 : _f.effectiveness) ||
((_j = (_h = (_g = latestDebug.fullMetrics) === null || _g === void 0 ? void 0 : _g.reranking) === null || _h === void 0 ? void 0 : _h.simpleQuery) === null || _j === void 0 ? void 0 : _j.effectiveness)
}
};
}
if (includeHistory && filteredHistory.length > 0) {
debugInfo.history = filteredHistory;
debugInfo.historyStats = {
totalExecutions: filteredHistory.length,
averageTime: DebugInspector.calculateAverageTime(filteredHistory),
strategyBreakdown: DebugInspector.getStrategyBreakdown(filteredHistory)
};
}
// Handle different output formats
switch (outputFormat) {
case 'separate_output':
// Pass through original response
returnData.push(item);
// Add debug info as separate item
if (latestDebug || filteredHistory.length > 0) {
returnData.push({
json: {
type: 'query_retriever_debug',
timestamp: new Date().toISOString(),
...debugInfo
}
});
}
break;
case 'add_to_json':
// Add debug info to the original response
const enhancedResponse = {
...item.json,
debug: debugInfo
};
returnData.push({
...item,
json: enhancedResponse
});
break;
case 'summary_only':
// Add only the analysis summary
const summaryResponse = {
...item.json,
debugAnalysis: (latestDebug === null || latestDebug === void 0 ? void 0 : latestDebug.analysis) || 'No debug analysis available'
};
returnData.push({
...item,
json: summaryResponse
});
break;
}
}
return [returnData];
}
static calculateAverageTime(history) {
const times = history
.map(entry => { var _a; return (_a = entry.key_metrics) === null || _a === void 0 ? void 0 : _a.total_time; })
.filter(time => time && time.endsWith('ms'))
.map(time => parseInt(time.replace('ms', '')));
if (times.length === 0)
return 'N/A';
const average = times.reduce((sum, time) => sum + time, 0) / times.length;
return `${Math.round(average)}ms`;
}
static getStrategyBreakdown(history) {
const breakdown = {};
history.forEach(entry => {
var _a;
const strategy = ((_a = entry.key_metrics) === null || _a === void 0 ? void 0 : _a.strategy) || 'unknown';
breakdown[strategy] = (breakdown[strategy] || 0) + 1;
});
return breakdown;
}
}
exports.DebugInspector = DebugInspector;