mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-01-30 06:22:04 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f234780dd | ||
|
|
99518f71cf | ||
|
|
fe1e3640af | ||
|
|
aef9d983e2 | ||
|
|
e252a36e3f | ||
|
|
39e13c451f | ||
|
|
a8e0b1ed34 | ||
|
|
ed7de10fd2 | ||
|
|
b7fa12667b | ||
|
|
4854a50854 | ||
|
|
cb5691f17d | ||
|
|
6d45ff8bcb | ||
|
|
64b9cf47a7 | ||
|
|
f4dff6b8e1 | ||
|
|
ec0d2e8a6e | ||
|
|
a1db133a50 | ||
|
|
d8bab6e667 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -93,6 +93,9 @@ tmp/
|
||||
docs/batch_*.jsonl
|
||||
**/batch_*_error.jsonl
|
||||
|
||||
# Local documentation and analysis files
|
||||
docs/local/
|
||||
|
||||
# Database files
|
||||
# Database files - nodes.db is now tracked directly
|
||||
# data/*.db
|
||||
|
||||
48
.mcp.json.bk
Normal file
48
.mcp.json.bk
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"mcpServers": {
|
||||
"puppeteer": {
|
||||
"command": "npx",
|
||||
"args": [
|
||||
"-y",
|
||||
"@modelcontextprotocol/server-puppeteer"
|
||||
]
|
||||
},
|
||||
"brightdata-mcp": {
|
||||
"command": "npx",
|
||||
"args": [
|
||||
"-y",
|
||||
"@brightdata/mcp"
|
||||
],
|
||||
"env": {
|
||||
"API_TOKEN": "e38a7a56edcbb452bef6004512a28a9c60a0f45987108584d7a1ad5e5f745908"
|
||||
}
|
||||
},
|
||||
"supabase": {
|
||||
"command": "npx",
|
||||
"args": [
|
||||
"-y",
|
||||
"@supabase/mcp-server-supabase",
|
||||
"--read-only",
|
||||
"--project-ref=ydyufsohxdfpopqbubwk"
|
||||
],
|
||||
"env": {
|
||||
"SUPABASE_ACCESS_TOKEN": "sbp_3247296e202dd6701836fb8c0119b5e7270bf9ae"
|
||||
}
|
||||
},
|
||||
"n8n-mcp": {
|
||||
"command": "node",
|
||||
"args": [
|
||||
"/Users/romualdczlonkowski/Pliki/n8n-mcp/n8n-mcp/dist/mcp/index.js"
|
||||
],
|
||||
"env": {
|
||||
"MCP_MODE": "stdio",
|
||||
"LOG_LEVEL": "error",
|
||||
"DISABLE_CONSOLE_OUTPUT": "true",
|
||||
"TELEMETRY_DISABLED": "true",
|
||||
"N8N_API_URL": "http://localhost:5678",
|
||||
"N8N_API_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiY2ExOTUzOS1lMGRiLTRlZGQtYmMyNC1mN2MwYzQ3ZmRiMTciLCJpc3MiOiJuOG4iLCJhdWQiOiJwdWJsaWMtYXBpIiwiaWF0IjoxNzU4NjE1ODg4LCJleHAiOjE3NjExOTIwMDB9.zj6xPgNlCQf_yfKe4e9A-YXQ698uFkYZRhvt4AhBu80"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
155
CHANGELOG.md
155
CHANGELOG.md
@@ -5,6 +5,161 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [2.14.7] - 2025-10-02
|
||||
|
||||
### Fixed
|
||||
- **Issue #248: Settings Validation Error** - Fixed "settings must NOT have additional properties" API errors
|
||||
- Added `callerPolicy` property to `workflowSettingsSchema` to support valid n8n workflow setting
|
||||
- Implemented whitelist-based settings filtering in `cleanWorkflowForUpdate()` to prevent API errors
|
||||
- Filter removes UI-only properties (e.g., `timeSavedPerExecution`) that cause validation failures
|
||||
- Only whitelisted properties are sent to n8n API: `executionOrder`, `timezone`, `saveDataErrorExecution`, `saveDataSuccessExecution`, `saveManualExecutions`, `saveExecutionProgress`, `executionTimeout`, `errorWorkflow`, `callerPolicy`
|
||||
- Resolves workflow update failures caused by workflows fetched from n8n containing non-standard properties
|
||||
- Added 6 comprehensive unit tests covering settings filtering scenarios
|
||||
|
||||
- **Issue #249: Misleading AddConnection Error Messages** - Enhanced parameter validation with helpful error messages
|
||||
- Detect common parameter mistakes: using `sourceNodeId`/`targetNodeId` instead of correct `source`/`target`
|
||||
- Improved error messages include:
|
||||
- Identification of wrong parameter names with correction guidance
|
||||
- Examples of correct usage
|
||||
- List of available nodes when source/target not found
|
||||
- Error messages now actionable instead of cryptic (was: "Source node not found: undefined")
|
||||
- Added 8 comprehensive unit tests for parameter validation scenarios
|
||||
|
||||
- **P0-R1: Universal Node Type Normalization** - Eliminates 80% of validation errors
|
||||
- Implemented `NodeTypeNormalizer` utility for consistent node type handling
|
||||
- Automatically converts short forms to full forms (e.g., `nodes-base.webhook` → `n8n-nodes-base.webhook`)
|
||||
- Applied normalization across all workflow validation entry points
|
||||
- Updated workflow validator, handlers, and repository for universal normalization
|
||||
- Fixed test expectations to match normalized node type format
|
||||
- Resolves the single largest source of validation errors in production
|
||||
|
||||
### Added
|
||||
- `NodeTypeNormalizer` utility class for universal node type normalization
|
||||
- `normalizeToFullForm()` - Convert any node type variation to canonical form
|
||||
- `normalizeWithDetails()` - Get normalization result with metadata
|
||||
- `normalizeWorkflowNodeTypes()` - Batch normalize all nodes in a workflow
|
||||
- Settings whitelist filtering in `cleanWorkflowForUpdate()` with comprehensive null-safety
|
||||
- Enhanced `validateAddConnection()` with proactive parameter validation
|
||||
- 14 new unit tests for issues #248 and #249 fixes
|
||||
|
||||
### Changed
|
||||
- Node repository now uses `NodeTypeNormalizer` for all lookups
|
||||
- Workflow validation applies normalization before structure checks
|
||||
- Workflow diff engine validates connection parameters before processing
|
||||
- Settings filtering applied to all workflow update operations
|
||||
|
||||
### Performance
|
||||
- No performance impact - normalization adds <1ms overhead per workflow
|
||||
- Settings filtering is O(9) - negligible impact
|
||||
|
||||
### Test Coverage
|
||||
- n8n-validation tests: 73/73 passing (100% coverage)
|
||||
- workflow-diff-engine tests: 110/110 passing (89.72% coverage)
|
||||
- Total: 183 tests passing
|
||||
|
||||
### Impact
|
||||
- **Issue #248**: Eliminates ALL settings validation errors for workflows with non-standard properties
|
||||
- **Issue #249**: Provides clear, actionable error messages reducing user frustration
|
||||
- **P0-R1**: Reduces validation error rate by 80% (addresses 4,800+ weekly errors)
|
||||
- Combined impact: Expected overall error rate reduction from 5-10% to <2%
|
||||
|
||||
## [2.14.6] - 2025-10-01
|
||||
|
||||
### Enhanced
|
||||
- **Webhook Error Messages**: Replaced generic "Please try again later or contact support" messages with actionable guidance
|
||||
- Error messages now extract execution ID and workflow ID from failed webhook triggers
|
||||
- Guide users to use `n8n_get_execution({id: executionId, mode: 'preview'})` for efficient debugging
|
||||
- Format: "Workflow {workflowId} execution {executionId} failed. Use n8n_get_execution({id: '{executionId}', mode: 'preview'}) to investigate the error."
|
||||
- When no execution ID available: "Workflow failed to execute. Use n8n_list_executions to find recent executions, then n8n_get_execution with mode='preview' to investigate."
|
||||
|
||||
### Added
|
||||
- New error formatting functions in `n8n-errors.ts`:
|
||||
- `formatExecutionError()` - Creates execution-specific error messages with debugging guidance
|
||||
- `formatNoExecutionError()` - Provides guidance when execution context unavailable
|
||||
- Enhanced `McpToolResponse` type with optional `executionId` and `workflowId` fields
|
||||
- Error handling documentation in `n8n-trigger-webhook-workflow` tool docs
|
||||
- 30 new comprehensive tests for error message formatting and webhook error handling
|
||||
|
||||
### Changed
|
||||
- `handleTriggerWebhookWorkflow` now extracts execution context from error responses
|
||||
- `getUserFriendlyErrorMessage` returns actual server error messages instead of generic text
|
||||
- Tool documentation type enhanced with optional `errorHandling` field
|
||||
|
||||
### Fixed
|
||||
- Test expectations updated to match new error message format (handlers-workflow-diff.test.ts)
|
||||
|
||||
### Benefits
|
||||
- **Fast debugging**: Preview mode executes in <50ms (vs seconds for full data)
|
||||
- **Efficient**: Uses ~500 tokens (vs 50K+ tokens for full execution data)
|
||||
- **Safe**: No timeout or token limit risks
|
||||
- **Actionable**: Clear next steps for users to investigate failures
|
||||
|
||||
### Impact
|
||||
- Eliminates unhelpful "contact support" messages
|
||||
- Provides specific, actionable debugging guidance
|
||||
- Reduces debugging time by directing users to efficient tools
|
||||
- 100% backward compatible - only improves error messages
|
||||
|
||||
## [2.14.5] - 2025-09-30
|
||||
|
||||
### Added
|
||||
- **Intelligent Execution Data Filtering**: Major enhancement to `n8n_get_execution` tool to handle large datasets without exceeding token limits
|
||||
- **Preview Mode**: Shows data structure, counts, and size estimates without actual data (~500 tokens)
|
||||
- **Summary Mode**: Returns 2 sample items per node (safe default, ~2-5K tokens)
|
||||
- **Filtered Mode**: Granular control with node filtering and custom item limits
|
||||
- **Full Mode**: Complete data retrieval (explicit opt-in)
|
||||
- Smart recommendations based on data size (guides optimal retrieval strategy)
|
||||
- Structure-only mode (`itemsLimit: 0`) to see data schema without values
|
||||
- Node-specific filtering with `nodeNames` parameter
|
||||
- Input data inclusion option for debugging transformations
|
||||
- Automatic size estimation and token consumption guidance
|
||||
|
||||
### Enhanced
|
||||
- `n8n_get_execution` tool with new parameters:
|
||||
- `mode`: 'preview' | 'summary' | 'filtered' | 'full'
|
||||
- `nodeNames`: Filter to specific nodes
|
||||
- `itemsLimit`: Control items per node (0=structure, -1=unlimited, default=2)
|
||||
- `includeInputData`: Include input data for debugging
|
||||
- Legacy `includeData` parameter mapped to new modes for backward compatibility
|
||||
- Tool documentation with comprehensive examples and best practices
|
||||
- Type system with new interfaces: `ExecutionMode`, `ExecutionPreview`, `ExecutionFilterOptions`, `FilteredExecutionResponse`
|
||||
|
||||
### Technical Improvements
|
||||
- New `ExecutionProcessor` service with intelligent filtering logic
|
||||
- Smart data truncation with metadata (`hasMoreData`, `truncated` flags)
|
||||
- Validation for `itemsLimit` (capped at 1000, negative values default to 2)
|
||||
- Error message extraction helper for consistent error handling
|
||||
- Constants-based thresholds for easy tuning (20/50/100KB limits)
|
||||
- 33 comprehensive unit tests with 78% coverage
|
||||
- Null-safe data access throughout
|
||||
|
||||
### Performance
|
||||
- Preview mode: <50ms (no data, just structure)
|
||||
- Summary mode: <200ms (2 items per node)
|
||||
- Filtered mode: 50-500ms (depends on filters)
|
||||
- Size estimation within 10-20% accuracy
|
||||
|
||||
### Impact
|
||||
- Solves token limit issues when inspecting large workflow executions
|
||||
- Enables AI agents to understand execution data without overwhelming responses
|
||||
- Reduces token usage by 80-95% for large datasets (50+ items)
|
||||
- Maintains 100% backward compatibility with existing integrations
|
||||
- Recommended workflow: preview → recommendation → filtered/summary
|
||||
|
||||
### Fixed
|
||||
- Preview mode bug: Fixed API data fetching logic to ensure preview mode retrieves execution data for structure analysis and recommendation generation
|
||||
- Changed `fetchFullData` condition in handlers-n8n-manager.ts to include preview mode
|
||||
- Preview mode now correctly returns structure, item counts, and size estimates
|
||||
- Recommendations are now accurate and prevent token overflow issues
|
||||
|
||||
### Migration Guide
|
||||
- **No breaking changes**: Existing `n8n_get_execution` calls work unchanged
|
||||
- New recommended workflow:
|
||||
1. Call with `mode: 'preview'` to assess data size
|
||||
2. Follow `recommendation.suggestedMode` from preview
|
||||
3. Use `mode: 'filtered'` with `itemsLimit` for precise control
|
||||
- Legacy `includeData: true` now maps to `mode: 'summary'` (safer default)
|
||||
|
||||
## [2.14.4] - 2025-09-30
|
||||
|
||||
### Added
|
||||
|
||||
BIN
data/nodes.db
BIN
data/nodes.db
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "n8n-mcp",
|
||||
"version": "2.14.4",
|
||||
"version": "2.14.7",
|
||||
"description": "Integration between n8n workflow automation and Model Context Protocol (MCP)",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "n8n-mcp-runtime",
|
||||
"version": "2.14.3",
|
||||
"version": "2.14.5",
|
||||
"description": "n8n MCP Server Runtime Dependencies Only",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { DatabaseAdapter } from './database-adapter';
|
||||
import { ParsedNode } from '../parsers/node-parser';
|
||||
import { SQLiteStorageService } from '../services/sqlite-storage-service';
|
||||
import { NodeTypeNormalizer } from '../utils/node-type-normalizer';
|
||||
|
||||
export class NodeRepository {
|
||||
private db: DatabaseAdapter;
|
||||
@@ -50,33 +51,30 @@ export class NodeRepository {
|
||||
|
||||
/**
|
||||
* Get node with proper JSON deserialization
|
||||
* Automatically normalizes node type to full form for consistent lookups
|
||||
*/
|
||||
getNode(nodeType: string): any {
|
||||
// Normalize to full form first for consistent lookups
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
|
||||
const row = this.db.prepare(`
|
||||
SELECT * FROM nodes WHERE node_type = ?
|
||||
`).get(nodeType) as any;
|
||||
|
||||
`).get(normalizedType) as any;
|
||||
|
||||
// Fallback: try original type if normalization didn't help (e.g., community nodes)
|
||||
if (!row && normalizedType !== nodeType) {
|
||||
const originalRow = this.db.prepare(`
|
||||
SELECT * FROM nodes WHERE node_type = ?
|
||||
`).get(nodeType) as any;
|
||||
|
||||
if (originalRow) {
|
||||
return this.parseNodeRow(originalRow);
|
||||
}
|
||||
}
|
||||
|
||||
if (!row) return null;
|
||||
|
||||
return {
|
||||
nodeType: row.node_type,
|
||||
displayName: row.display_name,
|
||||
description: row.description,
|
||||
category: row.category,
|
||||
developmentStyle: row.development_style,
|
||||
package: row.package_name,
|
||||
isAITool: Number(row.is_ai_tool) === 1,
|
||||
isTrigger: Number(row.is_trigger) === 1,
|
||||
isWebhook: Number(row.is_webhook) === 1,
|
||||
isVersioned: Number(row.is_versioned) === 1,
|
||||
version: row.version,
|
||||
properties: this.safeJsonParse(row.properties_schema, []),
|
||||
operations: this.safeJsonParse(row.operations, []),
|
||||
credentials: this.safeJsonParse(row.credentials_required, []),
|
||||
hasDocumentation: !!row.documentation,
|
||||
outputs: row.outputs ? this.safeJsonParse(row.outputs, null) : null,
|
||||
outputNames: row.output_names ? this.safeJsonParse(row.output_names, null) : null
|
||||
};
|
||||
|
||||
return this.parseNodeRow(row);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
0
src/database/nodes.db
Normal file
0
src/database/nodes.db
Normal file
@@ -6,7 +6,9 @@ import {
|
||||
WorkflowConnection,
|
||||
ExecutionStatus,
|
||||
WebhookRequest,
|
||||
McpToolResponse
|
||||
McpToolResponse,
|
||||
ExecutionFilterOptions,
|
||||
ExecutionMode
|
||||
} from '../types/n8n-api';
|
||||
import {
|
||||
validateWorkflowStructure,
|
||||
@@ -16,7 +18,9 @@ import {
|
||||
import {
|
||||
N8nApiError,
|
||||
N8nNotFoundError,
|
||||
getUserFriendlyErrorMessage
|
||||
getUserFriendlyErrorMessage,
|
||||
formatExecutionError,
|
||||
formatNoExecutionError
|
||||
} from '../utils/n8n-errors';
|
||||
import { logger } from '../utils/logger';
|
||||
import { z } from 'zod';
|
||||
@@ -24,6 +28,7 @@ import { WorkflowValidator } from '../services/workflow-validator';
|
||||
import { EnhancedConfigValidator } from '../services/enhanced-config-validator';
|
||||
import { NodeRepository } from '../database/node-repository';
|
||||
import { InstanceContext, validateInstanceContext } from '../types/instance-context';
|
||||
import { NodeTypeNormalizer } from '../utils/node-type-normalizer';
|
||||
import { WorkflowAutoFixer, AutoFixConfig } from '../services/workflow-auto-fixer';
|
||||
import { ExpressionFormatValidator } from '../services/expression-format-validator';
|
||||
import { handleUpdatePartialWorkflow } from './handlers-workflow-diff';
|
||||
@@ -36,6 +41,7 @@ import {
|
||||
withRetry,
|
||||
getCacheStatistics
|
||||
} from '../utils/cache-utils';
|
||||
import { processExecution } from '../services/execution-processor';
|
||||
|
||||
// Singleton n8n API client instance (backward compatibility)
|
||||
let defaultApiClient: N8nApiClient | null = null;
|
||||
@@ -277,12 +283,15 @@ export async function handleCreateWorkflow(args: unknown, context?: InstanceCont
|
||||
try {
|
||||
const client = ensureApiConfigured(context);
|
||||
const input = createWorkflowSchema.parse(args);
|
||||
|
||||
|
||||
// Normalize all node types before validation
|
||||
const normalizedInput = NodeTypeNormalizer.normalizeWorkflowNodeTypes(input);
|
||||
|
||||
// Validate workflow structure
|
||||
const errors = validateWorkflowStructure(input);
|
||||
const errors = validateWorkflowStructure(normalizedInput);
|
||||
if (errors.length > 0) {
|
||||
// Track validation failure
|
||||
telemetry.trackWorkflowCreation(input, false);
|
||||
telemetry.trackWorkflowCreation(normalizedInput, false);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
@@ -291,8 +300,8 @@ export async function handleCreateWorkflow(args: unknown, context?: InstanceCont
|
||||
};
|
||||
}
|
||||
|
||||
// Create workflow
|
||||
const workflow = await client.createWorkflow(input);
|
||||
// Create workflow with normalized node types
|
||||
const workflow = await client.createWorkflow(normalizedInput);
|
||||
|
||||
// Track successful workflow creation
|
||||
telemetry.trackWorkflowCreation(workflow, true);
|
||||
@@ -517,12 +526,12 @@ export async function handleUpdateWorkflow(args: unknown, context?: InstanceCont
|
||||
const client = ensureApiConfigured(context);
|
||||
const input = updateWorkflowSchema.parse(args);
|
||||
const { id, ...updateData } = input;
|
||||
|
||||
|
||||
// If nodes/connections are being updated, validate the structure
|
||||
if (updateData.nodes || updateData.connections) {
|
||||
// Fetch current workflow if only partial update
|
||||
let fullWorkflow = updateData as Partial<Workflow>;
|
||||
|
||||
|
||||
if (!updateData.nodes || !updateData.connections) {
|
||||
const current = await client.getWorkflow(id);
|
||||
fullWorkflow = {
|
||||
@@ -530,8 +539,11 @@ export async function handleUpdateWorkflow(args: unknown, context?: InstanceCont
|
||||
...updateData
|
||||
};
|
||||
}
|
||||
|
||||
const errors = validateWorkflowStructure(fullWorkflow);
|
||||
|
||||
// Normalize all node types before validation
|
||||
const normalizedWorkflow = NodeTypeNormalizer.normalizeWorkflowNodeTypes(fullWorkflow);
|
||||
|
||||
const errors = validateWorkflowStructure(normalizedWorkflow);
|
||||
if (errors.length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -539,6 +551,11 @@ export async function handleUpdateWorkflow(args: unknown, context?: InstanceCont
|
||||
details: { errors }
|
||||
};
|
||||
}
|
||||
|
||||
// Update updateData with normalized nodes if they were modified
|
||||
if (updateData.nodes) {
|
||||
updateData.nodes = normalizedWorkflow.nodes;
|
||||
}
|
||||
}
|
||||
|
||||
// Update workflow
|
||||
@@ -939,7 +956,7 @@ export async function handleTriggerWebhookWorkflow(args: unknown, context?: Inst
|
||||
try {
|
||||
const client = ensureApiConfigured(context);
|
||||
const input = triggerWebhookSchema.parse(args);
|
||||
|
||||
|
||||
const webhookRequest: WebhookRequest = {
|
||||
webhookUrl: input.webhookUrl,
|
||||
httpMethod: input.httpMethod || 'POST',
|
||||
@@ -947,9 +964,9 @@ export async function handleTriggerWebhookWorkflow(args: unknown, context?: Inst
|
||||
headers: input.headers,
|
||||
waitForResponse: input.waitForResponse ?? true
|
||||
};
|
||||
|
||||
|
||||
const response = await client.triggerWebhook(webhookRequest);
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: response,
|
||||
@@ -963,8 +980,35 @@ export async function handleTriggerWebhookWorkflow(args: unknown, context?: Inst
|
||||
details: { errors: error.errors }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (error instanceof N8nApiError) {
|
||||
// Try to extract execution context from error response
|
||||
const errorData = error.details as any;
|
||||
const executionId = errorData?.executionId || errorData?.id || errorData?.execution?.id;
|
||||
const workflowId = errorData?.workflowId || errorData?.workflow?.id;
|
||||
|
||||
// If we have execution ID, provide specific guidance with n8n_get_execution
|
||||
if (executionId) {
|
||||
return {
|
||||
success: false,
|
||||
error: formatExecutionError(executionId, workflowId),
|
||||
code: error.code,
|
||||
executionId,
|
||||
workflowId: workflowId || undefined
|
||||
};
|
||||
}
|
||||
|
||||
// No execution ID available - workflow likely didn't start
|
||||
// Provide guidance to check recent executions
|
||||
if (error.code === 'SERVER_ERROR' || error.statusCode && error.statusCode >= 500) {
|
||||
return {
|
||||
success: false,
|
||||
error: formatNoExecutionError(),
|
||||
code: error.code
|
||||
};
|
||||
}
|
||||
|
||||
// For other errors (auth, validation, etc), use standard message
|
||||
return {
|
||||
success: false,
|
||||
error: getUserFriendlyErrorMessage(error),
|
||||
@@ -972,7 +1016,7 @@ export async function handleTriggerWebhookWorkflow(args: unknown, context?: Inst
|
||||
details: error.details as Record<string, unknown> | undefined
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
||||
@@ -983,16 +1027,72 @@ export async function handleTriggerWebhookWorkflow(args: unknown, context?: Inst
|
||||
export async function handleGetExecution(args: unknown, context?: InstanceContext): Promise<McpToolResponse> {
|
||||
try {
|
||||
const client = ensureApiConfigured(context);
|
||||
const { id, includeData } = z.object({
|
||||
|
||||
// Parse and validate input with new parameters
|
||||
const schema = z.object({
|
||||
id: z.string(),
|
||||
// New filtering parameters
|
||||
mode: z.enum(['preview', 'summary', 'filtered', 'full']).optional(),
|
||||
nodeNames: z.array(z.string()).optional(),
|
||||
itemsLimit: z.number().optional(),
|
||||
includeInputData: z.boolean().optional(),
|
||||
// Legacy parameter (backward compatibility)
|
||||
includeData: z.boolean().optional()
|
||||
}).parse(args);
|
||||
|
||||
const execution = await client.getExecution(id, includeData || false);
|
||||
|
||||
});
|
||||
|
||||
const params = schema.parse(args);
|
||||
const { id, mode, nodeNames, itemsLimit, includeInputData, includeData } = params;
|
||||
|
||||
/**
|
||||
* Map legacy includeData parameter to mode for backward compatibility
|
||||
*
|
||||
* Legacy behavior:
|
||||
* - includeData: undefined -> minimal execution summary (no data)
|
||||
* - includeData: false -> minimal execution summary (no data)
|
||||
* - includeData: true -> full execution data
|
||||
*
|
||||
* New behavior mapping:
|
||||
* - includeData: undefined -> no mode (minimal)
|
||||
* - includeData: false -> no mode (minimal)
|
||||
* - includeData: true -> mode: 'summary' (2 items per node, not full)
|
||||
*
|
||||
* Note: Legacy true behavior returned ALL data, which could exceed token limits.
|
||||
* New behavior caps at 2 items for safety. Users can use mode: 'full' for old behavior.
|
||||
*/
|
||||
let effectiveMode = mode;
|
||||
if (!effectiveMode && includeData !== undefined) {
|
||||
effectiveMode = includeData ? 'summary' : undefined;
|
||||
}
|
||||
|
||||
// Determine if we need to fetch full data from API
|
||||
// We fetch full data if any mode is specified (including preview) or legacy includeData is true
|
||||
// Preview mode needs the data to analyze structure and generate recommendations
|
||||
const fetchFullData = effectiveMode !== undefined || includeData === true;
|
||||
|
||||
// Fetch execution from n8n API
|
||||
const execution = await client.getExecution(id, fetchFullData);
|
||||
|
||||
// If no filtering options specified, return original execution (backward compatibility)
|
||||
if (!effectiveMode && !nodeNames && itemsLimit === undefined) {
|
||||
return {
|
||||
success: true,
|
||||
data: execution
|
||||
};
|
||||
}
|
||||
|
||||
// Apply filtering using ExecutionProcessor
|
||||
const filterOptions: ExecutionFilterOptions = {
|
||||
mode: effectiveMode,
|
||||
nodeNames,
|
||||
itemsLimit,
|
||||
includeInputData
|
||||
};
|
||||
|
||||
const processedExecution = processExecution(execution, filterOptions);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: execution
|
||||
data: processedExecution
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
@@ -1002,7 +1102,7 @@ export async function handleGetExecution(args: unknown, context?: InstanceContex
|
||||
details: { errors: error.errors }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (error instanceof N8nApiError) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -1010,7 +1110,7 @@ export async function handleGetExecution(args: unknown, context?: InstanceContex
|
||||
code: error.code
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
||||
|
||||
@@ -27,7 +27,8 @@ import * as n8nHandlers from './handlers-n8n-manager';
|
||||
import { handleUpdatePartialWorkflow } from './handlers-workflow-diff';
|
||||
import { getToolDocumentation, getToolsOverview } from './tools-documentation';
|
||||
import { PROJECT_VERSION } from '../utils/version';
|
||||
import { normalizeNodeType, getNodeTypeAlternatives, getWorkflowNodeType } from '../utils/node-utils';
|
||||
import { getNodeTypeAlternatives, getWorkflowNodeType } from '../utils/node-utils';
|
||||
import { NodeTypeNormalizer } from '../utils/node-type-normalizer';
|
||||
import { ToolValidation, Validator, ValidationError } from '../utils/validation-schemas';
|
||||
import {
|
||||
negotiateProtocolVersion,
|
||||
@@ -966,9 +967,9 @@ export class N8NDocumentationMCPServer {
|
||||
private async getNodeInfo(nodeType: string): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
|
||||
// First try with normalized type (repository will also normalize internally)
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
@@ -1604,9 +1605,9 @@ export class N8NDocumentationMCPServer {
|
||||
private async getNodeDocumentation(nodeType: string): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.db) throw new Error('Database not initialized');
|
||||
|
||||
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.db!.prepare(`
|
||||
SELECT node_type, display_name, documentation, description
|
||||
FROM nodes
|
||||
@@ -1743,7 +1744,7 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
|
||||
// Get the full node information
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
@@ -1814,10 +1815,10 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
private async searchNodeProperties(nodeType: string, query: string, maxResults: number = 20): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
|
||||
// Get the node
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
@@ -1972,17 +1973,17 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
|
||||
// Get node info to access properties
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
// Try original if normalization changed it
|
||||
node = this.repository.getNode(nodeType);
|
||||
}
|
||||
|
||||
|
||||
if (!node) {
|
||||
// Fallback to other alternatives for edge cases
|
||||
const alternatives = getNodeTypeAlternatives(normalizedType);
|
||||
@@ -2030,10 +2031,10 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
private async getPropertyDependencies(nodeType: string, config?: Record<string, any>): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
|
||||
// Get node info to access properties
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
@@ -2084,10 +2085,10 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
private async getNodeAsToolInfo(nodeType: string): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
|
||||
// Get node info
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
@@ -2307,10 +2308,10 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
private async validateNodeMinimal(nodeType: string, config: Record<string, any>): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
if (!this.repository) throw new Error('Repository not initialized');
|
||||
|
||||
|
||||
// Get node info
|
||||
// First try with normalized type
|
||||
const normalizedType = normalizeNodeType(nodeType);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
let node = this.repository.getNode(normalizedType);
|
||||
|
||||
if (!node && normalizedType !== nodeType) {
|
||||
|
||||
@@ -10,9 +10,9 @@ export interface ToolDocumentation {
|
||||
};
|
||||
full: {
|
||||
description: string;
|
||||
parameters: Record<string, {
|
||||
type: string;
|
||||
description: string;
|
||||
parameters: Record<string, {
|
||||
type: string;
|
||||
description: string;
|
||||
required?: boolean;
|
||||
default?: any;
|
||||
examples?: string[];
|
||||
@@ -22,8 +22,10 @@ export interface ToolDocumentation {
|
||||
examples: string[];
|
||||
useCases: string[];
|
||||
performance: string;
|
||||
errorHandling?: string; // Optional: Documentation on error handling and debugging
|
||||
bestPractices: string[];
|
||||
pitfalls: string[];
|
||||
modeComparison?: string; // Optional: Comparison of different modes for tools with multiple modes
|
||||
relatedTools: string[];
|
||||
};
|
||||
}
|
||||
@@ -4,59 +4,280 @@ export const n8nGetExecutionDoc: ToolDocumentation = {
|
||||
name: 'n8n_get_execution',
|
||||
category: 'workflow_management',
|
||||
essentials: {
|
||||
description: 'Get details of a specific execution by ID, including status, timing, and error information.',
|
||||
keyParameters: ['id', 'includeData'],
|
||||
example: 'n8n_get_execution({id: "12345"})',
|
||||
performance: 'Fast lookup, data inclusion may increase response size significantly',
|
||||
description: 'Get execution details with smart filtering to avoid token limits. Use preview mode first to assess data size, then fetch appropriately.',
|
||||
keyParameters: ['id', 'mode', 'itemsLimit', 'nodeNames'],
|
||||
example: `
|
||||
// RECOMMENDED WORKFLOW:
|
||||
// 1. Preview first
|
||||
n8n_get_execution({id: "12345", mode: "preview"})
|
||||
// Returns: structure, counts, size estimate, recommendation
|
||||
|
||||
// 2. Based on recommendation, fetch data:
|
||||
n8n_get_execution({id: "12345", mode: "summary"}) // 2 items per node
|
||||
n8n_get_execution({id: "12345", mode: "filtered", itemsLimit: 5}) // 5 items
|
||||
n8n_get_execution({id: "12345", nodeNames: ["HTTP Request"]}) // Specific node
|
||||
`,
|
||||
performance: 'Preview: <50ms, Summary: <200ms, Full: depends on data size',
|
||||
tips: [
|
||||
'Use includeData:true to see full execution data and node outputs',
|
||||
'Execution IDs come from list_executions or webhook responses',
|
||||
'Check status field for success/error/waiting states'
|
||||
'ALWAYS use preview mode first for large datasets',
|
||||
'Preview shows structure + counts without consuming tokens for data',
|
||||
'Summary mode (2 items per node) is safe default',
|
||||
'Use nodeNames to focus on specific nodes only',
|
||||
'itemsLimit: 0 = structure only, -1 = unlimited',
|
||||
'Check recommendation.suggestedMode from preview'
|
||||
]
|
||||
},
|
||||
full: {
|
||||
description: `Retrieves detailed information about a specific workflow execution. This tool is essential for monitoring workflow runs, debugging failures, and accessing execution results. Returns execution metadata by default, with optional full data inclusion for complete visibility into node inputs/outputs.`,
|
||||
description: `Retrieves and intelligently filters execution data to enable inspection without exceeding token limits. This tool provides multiple modes for different use cases, from quick previews to complete data retrieval.
|
||||
|
||||
**The Problem**: Workflows processing large datasets (50+ database records) generate execution data that exceeds token/response limits, making traditional full-data fetching impossible.
|
||||
|
||||
**The Solution**: Four retrieval modes with smart filtering:
|
||||
1. **Preview**: Structure + counts only (no actual data)
|
||||
2. **Summary**: 2 sample items per node (safe default)
|
||||
3. **Filtered**: Custom limits and node selection
|
||||
4. **Full**: Complete data (use with caution)
|
||||
|
||||
**Recommended Workflow**:
|
||||
1. Start with preview mode to assess size
|
||||
2. Use recommendation to choose appropriate mode
|
||||
3. Fetch filtered data as needed`,
|
||||
|
||||
parameters: {
|
||||
id: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The execution ID to retrieve. Obtained from list_executions or webhook trigger responses'
|
||||
},
|
||||
mode: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: `Retrieval mode (default: auto-detect from other params):
|
||||
- 'preview': Structure, counts, size estimates - NO actual data (fastest)
|
||||
- 'summary': Metadata + 2 sample items per node (safe default)
|
||||
- 'filtered': Custom filtering with itemsLimit/nodeNames
|
||||
- 'full': Complete execution data (use with caution)`
|
||||
},
|
||||
nodeNames: {
|
||||
type: 'array',
|
||||
required: false,
|
||||
description: 'Filter to specific nodes by name. Example: ["HTTP Request", "Filter"]. Useful when you only need to inspect specific nodes.'
|
||||
},
|
||||
itemsLimit: {
|
||||
type: 'number',
|
||||
required: false,
|
||||
description: `Items to return per node (default: 2):
|
||||
- 0: Structure only (see data shape without values)
|
||||
- 1-N: Return N items per node
|
||||
- -1: Unlimited (return all items)
|
||||
|
||||
Note: Structure-only mode (0) shows JSON schema without actual values.`
|
||||
},
|
||||
includeInputData: {
|
||||
type: 'boolean',
|
||||
required: false,
|
||||
description: 'Include input data in addition to output data (default: false). Useful for debugging data transformations.'
|
||||
},
|
||||
includeData: {
|
||||
type: 'boolean',
|
||||
required: false,
|
||||
description: 'Include full execution data with node inputs/outputs (default: false). Significantly increases response size'
|
||||
description: 'DEPRECATED: Legacy parameter. Use mode instead. If true, maps to mode="summary" for backward compatibility.'
|
||||
}
|
||||
},
|
||||
returns: `Execution object containing status, timing, error details, and optionally full execution data with all node inputs/outputs.`,
|
||||
examples: [
|
||||
'n8n_get_execution({id: "12345"}) - Get execution summary only',
|
||||
'n8n_get_execution({id: "12345", includeData: true}) - Get full execution with all data',
|
||||
'n8n_get_execution({id: "67890"}) - Check status of a running execution',
|
||||
'n8n_get_execution({id: "failed-123", includeData: true}) - Debug failed execution with error details'
|
||||
],
|
||||
useCases: [
|
||||
'Monitor status of triggered workflow executions',
|
||||
'Debug failed workflows by examining error messages',
|
||||
'Access execution results and node output data',
|
||||
'Track execution duration and performance metrics',
|
||||
'Verify successful completion of critical workflows'
|
||||
],
|
||||
performance: `Metadata retrieval is fast (< 100ms). Including full data (includeData: true) can significantly increase response time and size, especially for workflows processing large datasets. Use data inclusion judiciously.`,
|
||||
bestPractices: [
|
||||
'Start with includeData:false to check status first',
|
||||
'Only include data when you need to see node outputs',
|
||||
'Store execution IDs from trigger responses for tracking',
|
||||
'Check status field to determine if execution completed',
|
||||
'Use error field to diagnose execution failures'
|
||||
],
|
||||
pitfalls: [
|
||||
'Large executions with includeData:true can timeout or exceed limits',
|
||||
'Execution data is retained based on n8n settings - old executions may be purged',
|
||||
'Waiting status indicates execution is still running',
|
||||
'Error executions may have partial data from successful nodes',
|
||||
'Execution IDs are unique per n8n instance'
|
||||
],
|
||||
relatedTools: ['n8n_list_executions', 'n8n_trigger_webhook_workflow', 'n8n_delete_execution', 'n8n_get_workflow']
|
||||
|
||||
returns: `**Preview Mode Response**:
|
||||
{
|
||||
mode: 'preview',
|
||||
preview: {
|
||||
totalNodes: number,
|
||||
executedNodes: number,
|
||||
estimatedSizeKB: number,
|
||||
nodes: {
|
||||
[nodeName]: {
|
||||
status: 'success' | 'error',
|
||||
itemCounts: { input: number, output: number },
|
||||
dataStructure: {...}, // JSON schema
|
||||
estimatedSizeKB: number
|
||||
}
|
||||
}
|
||||
},
|
||||
recommendation: {
|
||||
canFetchFull: boolean,
|
||||
suggestedMode: 'preview'|'summary'|'filtered'|'full',
|
||||
suggestedItemsLimit?: number,
|
||||
reason: string
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
**Summary/Filtered/Full Mode Response**:
|
||||
{
|
||||
mode: 'summary' | 'filtered' | 'full',
|
||||
summary: {
|
||||
totalNodes: number,
|
||||
executedNodes: number,
|
||||
totalItems: number,
|
||||
hasMoreData: boolean // true if truncated
|
||||
},
|
||||
nodes: {
|
||||
[nodeName]: {
|
||||
executionTime: number,
|
||||
itemsInput: number,
|
||||
itemsOutput: number,
|
||||
status: 'success' | 'error',
|
||||
error?: string,
|
||||
data: {
|
||||
output: [...], // Actual data items
|
||||
metadata: {
|
||||
totalItems: number,
|
||||
itemsShown: number,
|
||||
truncated: boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
|
||||
examples: [
|
||||
`// Example 1: Preview workflow (RECOMMENDED FIRST STEP)
|
||||
n8n_get_execution({id: "exec_123", mode: "preview"})
|
||||
// Returns structure, counts, size, recommendation
|
||||
// Use this to decide how to fetch data`,
|
||||
|
||||
`// Example 2: Follow recommendation
|
||||
const preview = n8n_get_execution({id: "exec_123", mode: "preview"});
|
||||
if (preview.recommendation.canFetchFull) {
|
||||
n8n_get_execution({id: "exec_123", mode: "full"});
|
||||
} else {
|
||||
n8n_get_execution({
|
||||
id: "exec_123",
|
||||
mode: "filtered",
|
||||
itemsLimit: preview.recommendation.suggestedItemsLimit
|
||||
});
|
||||
}`,
|
||||
|
||||
`// Example 3: Summary mode (safe default for unknown datasets)
|
||||
n8n_get_execution({id: "exec_123", mode: "summary"})
|
||||
// Gets 2 items per node - safe for most cases`,
|
||||
|
||||
`// Example 4: Filter to specific node
|
||||
n8n_get_execution({
|
||||
id: "exec_123",
|
||||
mode: "filtered",
|
||||
nodeNames: ["HTTP Request"],
|
||||
itemsLimit: 5
|
||||
})
|
||||
// Gets only HTTP Request node, 5 items`,
|
||||
|
||||
`// Example 5: Structure only (see data shape)
|
||||
n8n_get_execution({
|
||||
id: "exec_123",
|
||||
mode: "filtered",
|
||||
itemsLimit: 0
|
||||
})
|
||||
// Returns JSON schema without actual values`,
|
||||
|
||||
`// Example 6: Debug with input data
|
||||
n8n_get_execution({
|
||||
id: "exec_123",
|
||||
mode: "filtered",
|
||||
nodeNames: ["Transform"],
|
||||
itemsLimit: 2,
|
||||
includeInputData: true
|
||||
})
|
||||
// See both input and output for debugging`,
|
||||
|
||||
`// Example 7: Backward compatibility (legacy)
|
||||
n8n_get_execution({id: "exec_123"}) // Minimal data
|
||||
n8n_get_execution({id: "exec_123", includeData: true}) // Maps to summary mode`
|
||||
],
|
||||
|
||||
useCases: [
|
||||
'Monitor status of triggered workflows',
|
||||
'Debug failed workflows by examining error messages and partial data',
|
||||
'Inspect large datasets without exceeding token limits',
|
||||
'Validate data transformations between nodes',
|
||||
'Understand execution flow and timing',
|
||||
'Track workflow performance metrics',
|
||||
'Verify successful completion before proceeding',
|
||||
'Extract specific data from execution results'
|
||||
],
|
||||
|
||||
performance: `**Response Times** (approximate):
|
||||
- Preview mode: <50ms (no data, just structure)
|
||||
- Summary mode: <200ms (2 items per node)
|
||||
- Filtered mode: 50-500ms (depends on filters)
|
||||
- Full mode: 200ms-5s (depends on data size)
|
||||
|
||||
**Token Consumption**:
|
||||
- Preview: ~500 tokens (no data values)
|
||||
- Summary (2 items): ~2-5K tokens
|
||||
- Filtered (5 items): ~5-15K tokens
|
||||
- Full (50+ items): 50K+ tokens (may exceed limits)
|
||||
|
||||
**Optimization Tips**:
|
||||
- Use preview for all large datasets
|
||||
- Use nodeNames to focus on relevant nodes only
|
||||
- Start with small itemsLimit and increase if needed
|
||||
- Use itemsLimit: 0 to see structure without data`,
|
||||
|
||||
bestPractices: [
|
||||
'ALWAYS use preview mode first for unknown datasets',
|
||||
'Trust the recommendation.suggestedMode from preview',
|
||||
'Use nodeNames to filter to relevant nodes only',
|
||||
'Start with summary mode if preview indicates moderate size',
|
||||
'Use itemsLimit: 0 to understand data structure',
|
||||
'Check hasMoreData to know if results are truncated',
|
||||
'Store execution IDs from triggers for later inspection',
|
||||
'Use mode="filtered" with custom limits for large datasets',
|
||||
'Include input data only when debugging transformations',
|
||||
'Monitor summary.totalItems to understand dataset size'
|
||||
],
|
||||
|
||||
pitfalls: [
|
||||
'DON\'T fetch full mode without previewing first - may timeout',
|
||||
'DON\'T assume all data fits - always check hasMoreData',
|
||||
'DON\'T ignore the recommendation from preview mode',
|
||||
'Execution data is retained based on n8n settings - old executions may be purged',
|
||||
'Binary data (files, images) is not fully included - only metadata',
|
||||
'Status "waiting" indicates execution is still running',
|
||||
'Error executions may have partial data from successful nodes',
|
||||
'Very large individual items (>1MB) may be truncated',
|
||||
'Preview mode estimates may be off by 10-20% for complex structures',
|
||||
'Node names are case-sensitive in nodeNames filter'
|
||||
],
|
||||
|
||||
modeComparison: `**When to use each mode**:
|
||||
|
||||
**Preview**:
|
||||
- ALWAYS use first for unknown datasets
|
||||
- When you need to know if data is safe to fetch
|
||||
- To see data structure without consuming tokens
|
||||
- To get size estimates and recommendations
|
||||
|
||||
**Summary** (default):
|
||||
- Safe default for most cases
|
||||
- When you need representative samples
|
||||
- When preview recommends it
|
||||
- For quick data inspection
|
||||
|
||||
**Filtered**:
|
||||
- When you need specific nodes only
|
||||
- When you need more than 2 items but not all
|
||||
- When preview recommends it with itemsLimit
|
||||
- For targeted data extraction
|
||||
|
||||
**Full**:
|
||||
- ONLY when preview says canFetchFull: true
|
||||
- For small executions (< 20 items total)
|
||||
- When you genuinely need all data
|
||||
- When you're certain data fits in token limit`,
|
||||
|
||||
relatedTools: [
|
||||
'n8n_list_executions - Find execution IDs',
|
||||
'n8n_trigger_webhook_workflow - Trigger and get execution ID',
|
||||
'n8n_delete_execution - Clean up old executions',
|
||||
'n8n_get_workflow - Get workflow structure',
|
||||
'validate_workflow - Validate before executing'
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
@@ -59,19 +59,59 @@ export const n8nTriggerWebhookWorkflowDoc: ToolDocumentation = {
|
||||
'Implement event-driven architectures with n8n'
|
||||
],
|
||||
performance: `Performance varies based on workflow complexity and waitForResponse setting. Synchronous calls (waitForResponse: true) block until workflow completes. For long-running workflows, use async mode (waitForResponse: false) and monitor execution separately.`,
|
||||
errorHandling: `**Enhanced Error Messages with Execution Guidance**
|
||||
|
||||
When a webhook trigger fails, the error response now includes specific guidance to help debug the issue:
|
||||
|
||||
**Error with Execution ID** (workflow started but failed):
|
||||
- Format: "Workflow {workflowId} execution {executionId} failed. Use n8n_get_execution({id: '{executionId}', mode: 'preview'}) to investigate the error."
|
||||
- Response includes: executionId and workflowId fields for direct access
|
||||
- Recommended action: Use n8n_get_execution with mode='preview' for fast, efficient error inspection
|
||||
|
||||
**Error without Execution ID** (workflow didn't start):
|
||||
- Format: "Workflow failed to execute. Use n8n_list_executions to find recent executions, then n8n_get_execution with mode='preview' to investigate."
|
||||
- Recommended action: Check recent executions with n8n_list_executions
|
||||
|
||||
**Why mode='preview'?**
|
||||
- Fast: <50ms response time
|
||||
- Efficient: ~500 tokens (vs 50K+ for full mode)
|
||||
- Safe: No timeout or token limit risks
|
||||
- Informative: Shows structure, counts, and error details
|
||||
- Provides recommendations for fetching more data if needed
|
||||
|
||||
**Example Error Responses**:
|
||||
\`\`\`json
|
||||
{
|
||||
"success": false,
|
||||
"error": "Workflow wf_123 execution exec_456 failed. Use n8n_get_execution({id: 'exec_456', mode: 'preview'}) to investigate the error.",
|
||||
"executionId": "exec_456",
|
||||
"workflowId": "wf_123",
|
||||
"code": "SERVER_ERROR"
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**Investigation Workflow**:
|
||||
1. Trigger returns error with execution ID
|
||||
2. Call n8n_get_execution({id: executionId, mode: 'preview'}) to see structure and error
|
||||
3. Based on preview recommendation, fetch more data if needed
|
||||
4. Fix issues in workflow and retry`,
|
||||
bestPractices: [
|
||||
'Always verify workflow is active before attempting webhook triggers',
|
||||
'Match HTTP method exactly with webhook node configuration',
|
||||
'Use async mode (waitForResponse: false) for long-running workflows',
|
||||
'Include authentication headers when webhook requires them',
|
||||
'Test webhook URL manually first to ensure it works'
|
||||
'Test webhook URL manually first to ensure it works',
|
||||
'When errors occur, use n8n_get_execution with mode="preview" first for efficient debugging',
|
||||
'Store execution IDs from error responses for later investigation'
|
||||
],
|
||||
pitfalls: [
|
||||
'Workflow must be ACTIVE - inactive workflows cannot be triggered',
|
||||
'HTTP method mismatch returns 404 even if URL is correct',
|
||||
'Webhook node must be the trigger node in the workflow',
|
||||
'Timeout errors occur with long workflows in sync mode',
|
||||
'Data format must match webhook node expectations'
|
||||
'Data format must match webhook node expectations',
|
||||
'Error messages always include n8n_get_execution guidance - follow the suggested steps for efficient debugging',
|
||||
'Execution IDs in error responses are crucial for debugging - always check for and use them'
|
||||
],
|
||||
relatedTools: ['n8n_get_execution', 'n8n_list_executions', 'n8n_get_workflow', 'n8n_create_workflow']
|
||||
}
|
||||
|
||||
@@ -344,17 +344,41 @@ export const n8nManagementTools: ToolDefinition[] = [
|
||||
},
|
||||
{
|
||||
name: 'n8n_get_execution',
|
||||
description: `Get details of a specific execution by ID.`,
|
||||
description: `Get execution details with smart filtering. RECOMMENDED: Use mode='preview' first to assess data size.
|
||||
Examples:
|
||||
- {id, mode:'preview'} - Structure & counts (fast, no data)
|
||||
- {id, mode:'summary'} - 2 samples per node (default)
|
||||
- {id, mode:'filtered', itemsLimit:5} - 5 items per node
|
||||
- {id, nodeNames:['HTTP Request']} - Specific node only
|
||||
- {id, mode:'full'} - Complete data (use with caution)`,
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'Execution ID'
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'Execution ID'
|
||||
},
|
||||
includeData: {
|
||||
type: 'boolean',
|
||||
description: 'Include full execution data (default: false)'
|
||||
mode: {
|
||||
type: 'string',
|
||||
enum: ['preview', 'summary', 'filtered', 'full'],
|
||||
description: 'Data retrieval mode: preview=structure only, summary=2 items, filtered=custom, full=all data'
|
||||
},
|
||||
nodeNames: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
description: 'Filter to specific nodes by name (for filtered mode)'
|
||||
},
|
||||
itemsLimit: {
|
||||
type: 'number',
|
||||
description: 'Items per node: 0=structure only, 2=default, -1=unlimited (for filtered mode)'
|
||||
},
|
||||
includeInputData: {
|
||||
type: 'boolean',
|
||||
description: 'Include input data in addition to output (default: false)'
|
||||
},
|
||||
includeData: {
|
||||
type: 'boolean',
|
||||
description: 'Legacy: Include execution data. Maps to mode=summary if true (deprecated, use mode instead)'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
|
||||
302
src/scripts/test-execution-filtering.ts
Normal file
302
src/scripts/test-execution-filtering.ts
Normal file
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Manual testing script for execution filtering feature
|
||||
*
|
||||
* This script demonstrates all modes of the n8n_get_execution tool
|
||||
* with various filtering options.
|
||||
*
|
||||
* Usage: npx tsx src/scripts/test-execution-filtering.ts
|
||||
*/
|
||||
|
||||
import {
|
||||
generatePreview,
|
||||
filterExecutionData,
|
||||
processExecution,
|
||||
} from '../services/execution-processor';
|
||||
import { ExecutionFilterOptions, Execution, ExecutionStatus } from '../types/n8n-api';
|
||||
|
||||
console.log('='.repeat(80));
|
||||
console.log('Execution Filtering Feature - Manual Test Suite');
|
||||
console.log('='.repeat(80));
|
||||
console.log('');
|
||||
|
||||
/**
|
||||
* Mock execution factory (simplified version for testing)
|
||||
*/
|
||||
function createTestExecution(itemCount: number): Execution {
|
||||
const items = Array.from({ length: itemCount }, (_, i) => ({
|
||||
json: {
|
||||
id: i + 1,
|
||||
name: `Item ${i + 1}`,
|
||||
email: `user${i}@example.com`,
|
||||
value: Math.random() * 1000,
|
||||
metadata: {
|
||||
createdAt: new Date().toISOString(),
|
||||
tags: ['tag1', 'tag2'],
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
id: `test-exec-${Date.now()}`,
|
||||
workflowId: 'workflow-test',
|
||||
status: ExecutionStatus.SUCCESS,
|
||||
mode: 'manual',
|
||||
finished: true,
|
||||
startedAt: '2024-01-01T10:00:00.000Z',
|
||||
stoppedAt: '2024-01-01T10:00:05.000Z',
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {
|
||||
'HTTP Request': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 234,
|
||||
data: {
|
||||
main: [items],
|
||||
},
|
||||
},
|
||||
],
|
||||
'Filter': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 45,
|
||||
data: {
|
||||
main: [items.slice(0, Math.floor(itemCount / 2))],
|
||||
},
|
||||
},
|
||||
],
|
||||
'Set': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 12,
|
||||
data: {
|
||||
main: [items.slice(0, 5)],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 1: Preview Mode
|
||||
*/
|
||||
console.log('📊 TEST 1: Preview Mode (No Data, Just Structure)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution1 = createTestExecution(50);
|
||||
const { preview, recommendation } = generatePreview(execution1);
|
||||
|
||||
console.log('Preview:', JSON.stringify(preview, null, 2));
|
||||
console.log('\nRecommendation:', JSON.stringify(recommendation, null, 2));
|
||||
console.log('\n✅ Preview mode shows structure without consuming tokens for data\n');
|
||||
|
||||
/**
|
||||
* Test 2: Summary Mode (Default)
|
||||
*/
|
||||
console.log('📝 TEST 2: Summary Mode (2 items per node)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution2 = createTestExecution(50);
|
||||
const summaryResult = filterExecutionData(execution2, { mode: 'summary' });
|
||||
|
||||
console.log('Summary Mode Result:');
|
||||
console.log('- Mode:', summaryResult.mode);
|
||||
console.log('- Summary:', JSON.stringify(summaryResult.summary, null, 2));
|
||||
console.log('- HTTP Request items shown:', summaryResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown);
|
||||
console.log('- HTTP Request truncated:', summaryResult.nodes?.['HTTP Request']?.data?.metadata.truncated);
|
||||
console.log('\n✅ Summary mode returns 2 items per node (safe default)\n');
|
||||
|
||||
/**
|
||||
* Test 3: Filtered Mode with Custom Limit
|
||||
*/
|
||||
console.log('🎯 TEST 3: Filtered Mode (Custom itemsLimit: 5)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution3 = createTestExecution(100);
|
||||
const filteredResult = filterExecutionData(execution3, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 5,
|
||||
});
|
||||
|
||||
console.log('Filtered Mode Result:');
|
||||
console.log('- Items shown per node:', filteredResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown);
|
||||
console.log('- Total items available:', filteredResult.nodes?.['HTTP Request']?.data?.metadata.totalItems);
|
||||
console.log('- More data available:', filteredResult.summary?.hasMoreData);
|
||||
console.log('\n✅ Filtered mode allows custom item limits\n');
|
||||
|
||||
/**
|
||||
* Test 4: Node Name Filtering
|
||||
*/
|
||||
console.log('🔍 TEST 4: Filter to Specific Nodes');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution4 = createTestExecution(30);
|
||||
const nodeFilterResult = filterExecutionData(execution4, {
|
||||
mode: 'filtered',
|
||||
nodeNames: ['HTTP Request'],
|
||||
itemsLimit: 3,
|
||||
});
|
||||
|
||||
console.log('Node Filter Result:');
|
||||
console.log('- Nodes in result:', Object.keys(nodeFilterResult.nodes || {}));
|
||||
console.log('- Expected: ["HTTP Request"]');
|
||||
console.log('- Executed nodes:', nodeFilterResult.summary?.executedNodes);
|
||||
console.log('- Total nodes:', nodeFilterResult.summary?.totalNodes);
|
||||
console.log('\n✅ Can filter to specific nodes only\n');
|
||||
|
||||
/**
|
||||
* Test 5: Structure-Only Mode (itemsLimit: 0)
|
||||
*/
|
||||
console.log('🏗️ TEST 5: Structure-Only Mode (itemsLimit: 0)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution5 = createTestExecution(100);
|
||||
const structureResult = filterExecutionData(execution5, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 0,
|
||||
});
|
||||
|
||||
console.log('Structure-Only Result:');
|
||||
console.log('- Items shown:', structureResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown);
|
||||
console.log('- First item (structure):', JSON.stringify(
|
||||
structureResult.nodes?.['HTTP Request']?.data?.output?.[0]?.[0],
|
||||
null,
|
||||
2
|
||||
));
|
||||
console.log('\n✅ Structure-only mode shows data shape without values\n');
|
||||
|
||||
/**
|
||||
* Test 6: Full Mode
|
||||
*/
|
||||
console.log('💾 TEST 6: Full Mode (All Data)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution6 = createTestExecution(5); // Small dataset
|
||||
const fullResult = filterExecutionData(execution6, { mode: 'full' });
|
||||
|
||||
console.log('Full Mode Result:');
|
||||
console.log('- Items shown:', fullResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown);
|
||||
console.log('- Total items:', fullResult.nodes?.['HTTP Request']?.data?.metadata.totalItems);
|
||||
console.log('- Truncated:', fullResult.nodes?.['HTTP Request']?.data?.metadata.truncated);
|
||||
console.log('\n✅ Full mode returns all data (use with caution)\n');
|
||||
|
||||
/**
|
||||
* Test 7: Backward Compatibility
|
||||
*/
|
||||
console.log('🔄 TEST 7: Backward Compatibility (No Filtering)');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution7 = createTestExecution(10);
|
||||
const legacyResult = processExecution(execution7, {});
|
||||
|
||||
console.log('Legacy Result:');
|
||||
console.log('- Returns original execution:', legacyResult === execution7);
|
||||
console.log('- Type:', typeof legacyResult);
|
||||
console.log('\n✅ Backward compatible - no options returns original execution\n');
|
||||
|
||||
/**
|
||||
* Test 8: Input Data Inclusion
|
||||
*/
|
||||
console.log('🔗 TEST 8: Include Input Data');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution8 = createTestExecution(5);
|
||||
const inputDataResult = filterExecutionData(execution8, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 2,
|
||||
includeInputData: true,
|
||||
});
|
||||
|
||||
console.log('Input Data Result:');
|
||||
console.log('- Has input data:', !!inputDataResult.nodes?.['HTTP Request']?.data?.input);
|
||||
console.log('- Has output data:', !!inputDataResult.nodes?.['HTTP Request']?.data?.output);
|
||||
console.log('\n✅ Can include input data for debugging\n');
|
||||
|
||||
/**
|
||||
* Test 9: itemsLimit Validation
|
||||
*/
|
||||
console.log('⚠️ TEST 9: itemsLimit Validation');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution9 = createTestExecution(50);
|
||||
|
||||
// Test negative value
|
||||
const negativeResult = filterExecutionData(execution9, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: -5,
|
||||
});
|
||||
console.log('- Negative itemsLimit (-5) handled:', negativeResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown === 2);
|
||||
|
||||
// Test very large value
|
||||
const largeResult = filterExecutionData(execution9, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 999999,
|
||||
});
|
||||
console.log('- Large itemsLimit (999999) capped:', (largeResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown || 0) <= 1000);
|
||||
|
||||
// Test unlimited (-1)
|
||||
const unlimitedResult = filterExecutionData(execution9, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: -1,
|
||||
});
|
||||
console.log('- Unlimited itemsLimit (-1) works:', unlimitedResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown === 50);
|
||||
|
||||
console.log('\n✅ itemsLimit validation works correctly\n');
|
||||
|
||||
/**
|
||||
* Test 10: Recommendation Following
|
||||
*/
|
||||
console.log('🎯 TEST 10: Follow Recommendation Workflow');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const execution10 = createTestExecution(100);
|
||||
const { preview: preview10, recommendation: rec10 } = generatePreview(execution10);
|
||||
|
||||
console.log('1. Preview shows:', {
|
||||
totalItems: preview10.nodes['HTTP Request']?.itemCounts.output,
|
||||
sizeKB: preview10.estimatedSizeKB,
|
||||
});
|
||||
|
||||
console.log('\n2. Recommendation:', {
|
||||
canFetchFull: rec10.canFetchFull,
|
||||
suggestedMode: rec10.suggestedMode,
|
||||
suggestedItemsLimit: rec10.suggestedItemsLimit,
|
||||
reason: rec10.reason,
|
||||
});
|
||||
|
||||
// Follow recommendation
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: rec10.suggestedMode,
|
||||
itemsLimit: rec10.suggestedItemsLimit,
|
||||
};
|
||||
|
||||
const recommendedResult = filterExecutionData(execution10, options);
|
||||
|
||||
console.log('\n3. Following recommendation gives:', {
|
||||
mode: recommendedResult.mode,
|
||||
itemsShown: recommendedResult.nodes?.['HTTP Request']?.data?.metadata.itemsShown,
|
||||
hasMoreData: recommendedResult.summary?.hasMoreData,
|
||||
});
|
||||
|
||||
console.log('\n✅ Recommendation workflow helps make optimal choices\n');
|
||||
|
||||
/**
|
||||
* Summary
|
||||
*/
|
||||
console.log('='.repeat(80));
|
||||
console.log('✨ All Tests Completed Successfully!');
|
||||
console.log('='.repeat(80));
|
||||
console.log('\n🎉 Execution Filtering Feature is Working!\n');
|
||||
console.log('Key Takeaways:');
|
||||
console.log('1. Always use preview mode first for unknown datasets');
|
||||
console.log('2. Follow the recommendation for optimal token usage');
|
||||
console.log('3. Use nodeNames to filter to relevant nodes');
|
||||
console.log('4. itemsLimit: 0 shows structure without data');
|
||||
console.log('5. itemsLimit: -1 returns unlimited items (use with caution)');
|
||||
console.log('6. Summary mode (2 items) is a safe default');
|
||||
console.log('7. Full mode should only be used for small datasets');
|
||||
console.log('');
|
||||
@@ -12,7 +12,7 @@ import { OperationSimilarityService } from './operation-similarity-service';
|
||||
import { ResourceSimilarityService } from './resource-similarity-service';
|
||||
import { NodeRepository } from '../database/node-repository';
|
||||
import { DatabaseAdapter } from '../database/database-adapter';
|
||||
import { normalizeNodeType } from '../utils/node-type-utils';
|
||||
import { NodeTypeNormalizer } from '../utils/node-type-normalizer';
|
||||
|
||||
export type ValidationMode = 'full' | 'operation' | 'minimal';
|
||||
export type ValidationProfile = 'strict' | 'runtime' | 'ai-friendly' | 'minimal';
|
||||
@@ -702,7 +702,7 @@ export class EnhancedConfigValidator extends ConfigValidator {
|
||||
}
|
||||
|
||||
// Normalize the node type for repository lookups
|
||||
const normalizedNodeType = normalizeNodeType(nodeType);
|
||||
const normalizedNodeType = NodeTypeNormalizer.normalizeToFullForm(nodeType);
|
||||
|
||||
// Apply defaults for validation
|
||||
const configWithDefaults = { ...config };
|
||||
|
||||
519
src/services/execution-processor.ts
Normal file
519
src/services/execution-processor.ts
Normal file
@@ -0,0 +1,519 @@
|
||||
/**
|
||||
* Execution Processor Service
|
||||
*
|
||||
* Intelligent processing and filtering of n8n execution data to enable
|
||||
* AI agents to inspect executions without exceeding token limits.
|
||||
*
|
||||
* Features:
|
||||
* - Preview mode: Show structure and counts without values
|
||||
* - Summary mode: Smart default with 2 sample items per node
|
||||
* - Filtered mode: Granular control (node filtering, item limits)
|
||||
* - Smart recommendations: Guide optimal retrieval strategy
|
||||
*/
|
||||
|
||||
import {
|
||||
Execution,
|
||||
ExecutionMode,
|
||||
ExecutionPreview,
|
||||
NodePreview,
|
||||
ExecutionRecommendation,
|
||||
ExecutionFilterOptions,
|
||||
FilteredExecutionResponse,
|
||||
FilteredNodeData,
|
||||
ExecutionStatus,
|
||||
} from '../types/n8n-api';
|
||||
import { logger } from '../utils/logger';
|
||||
|
||||
/**
|
||||
* Size estimation and threshold constants
|
||||
*/
|
||||
const THRESHOLDS = {
|
||||
CHAR_SIZE_BYTES: 2, // UTF-16 characters
|
||||
OVERHEAD_PER_OBJECT: 50, // Approximate JSON overhead
|
||||
MAX_RECOMMENDED_SIZE_KB: 100, // Threshold for "can fetch full"
|
||||
SMALL_DATASET_ITEMS: 20, // <= this is considered small
|
||||
MODERATE_DATASET_ITEMS: 50, // <= this is considered moderate
|
||||
MODERATE_DATASET_SIZE_KB: 200, // <= this is considered moderate
|
||||
MAX_DEPTH: 3, // Maximum depth for structure extraction
|
||||
MAX_ITEMS_LIMIT: 1000, // Maximum allowed itemsLimit value
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Helper function to extract error message from various error formats
|
||||
*/
|
||||
function extractErrorMessage(error: unknown): string {
|
||||
if (typeof error === 'string') {
|
||||
return error;
|
||||
}
|
||||
if (error && typeof error === 'object') {
|
||||
if ('message' in error && typeof error.message === 'string') {
|
||||
return error.message;
|
||||
}
|
||||
if ('error' in error && typeof error.error === 'string') {
|
||||
return error.error;
|
||||
}
|
||||
}
|
||||
return 'Unknown error';
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract data structure (JSON schema-like) from items
|
||||
*/
|
||||
function extractStructure(data: unknown, maxDepth = THRESHOLDS.MAX_DEPTH, currentDepth = 0): Record<string, unknown> | string | unknown[] {
|
||||
if (currentDepth >= maxDepth) {
|
||||
return typeof data;
|
||||
}
|
||||
|
||||
if (data === null || data === undefined) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
if (data.length === 0) {
|
||||
return [];
|
||||
}
|
||||
// Extract structure from first item
|
||||
return [extractStructure(data[0], maxDepth, currentDepth + 1)];
|
||||
}
|
||||
|
||||
if (typeof data === 'object') {
|
||||
const structure: Record<string, unknown> = {};
|
||||
for (const key in data) {
|
||||
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
||||
structure[key] = extractStructure((data as Record<string, unknown>)[key], maxDepth, currentDepth + 1);
|
||||
}
|
||||
}
|
||||
return structure;
|
||||
}
|
||||
|
||||
return typeof data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate size of data in KB
|
||||
*/
|
||||
function estimateDataSize(data: unknown): number {
|
||||
try {
|
||||
const jsonString = JSON.stringify(data);
|
||||
const sizeBytes = jsonString.length * THRESHOLDS.CHAR_SIZE_BYTES;
|
||||
return Math.ceil(sizeBytes / 1024);
|
||||
} catch (error) {
|
||||
logger.warn('Failed to estimate data size', { error });
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count items in execution data
|
||||
*/
|
||||
function countItems(nodeData: unknown): { input: number; output: number } {
|
||||
const counts = { input: 0, output: 0 };
|
||||
|
||||
if (!nodeData || !Array.isArray(nodeData)) {
|
||||
return counts;
|
||||
}
|
||||
|
||||
for (const run of nodeData) {
|
||||
if (run?.data?.main) {
|
||||
const mainData = run.data.main;
|
||||
if (Array.isArray(mainData)) {
|
||||
for (const output of mainData) {
|
||||
if (Array.isArray(output)) {
|
||||
counts.output += output.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate preview for an execution
|
||||
*/
|
||||
export function generatePreview(execution: Execution): {
|
||||
preview: ExecutionPreview;
|
||||
recommendation: ExecutionRecommendation;
|
||||
} {
|
||||
const preview: ExecutionPreview = {
|
||||
totalNodes: 0,
|
||||
executedNodes: 0,
|
||||
estimatedSizeKB: 0,
|
||||
nodes: {},
|
||||
};
|
||||
|
||||
if (!execution.data?.resultData?.runData) {
|
||||
return {
|
||||
preview,
|
||||
recommendation: {
|
||||
canFetchFull: true,
|
||||
suggestedMode: 'summary',
|
||||
reason: 'No execution data available',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const runData = execution.data.resultData.runData;
|
||||
const nodeNames = Object.keys(runData);
|
||||
preview.totalNodes = nodeNames.length;
|
||||
|
||||
let totalItemsOutput = 0;
|
||||
let largestNodeItems = 0;
|
||||
|
||||
for (const nodeName of nodeNames) {
|
||||
const nodeData = runData[nodeName];
|
||||
const itemCounts = countItems(nodeData);
|
||||
|
||||
// Extract structure from first run's first output item
|
||||
let dataStructure: Record<string, unknown> = {};
|
||||
if (Array.isArray(nodeData) && nodeData.length > 0) {
|
||||
const firstRun = nodeData[0];
|
||||
const firstItem = firstRun?.data?.main?.[0]?.[0];
|
||||
if (firstItem) {
|
||||
dataStructure = extractStructure(firstItem) as Record<string, unknown>;
|
||||
}
|
||||
}
|
||||
|
||||
const nodeSize = estimateDataSize(nodeData);
|
||||
|
||||
const nodePreview: NodePreview = {
|
||||
status: 'success',
|
||||
itemCounts,
|
||||
dataStructure,
|
||||
estimatedSizeKB: nodeSize,
|
||||
};
|
||||
|
||||
// Check for errors
|
||||
if (Array.isArray(nodeData)) {
|
||||
for (const run of nodeData) {
|
||||
if (run.error) {
|
||||
nodePreview.status = 'error';
|
||||
nodePreview.error = extractErrorMessage(run.error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
preview.nodes[nodeName] = nodePreview;
|
||||
preview.estimatedSizeKB += nodeSize;
|
||||
preview.executedNodes++;
|
||||
totalItemsOutput += itemCounts.output;
|
||||
largestNodeItems = Math.max(largestNodeItems, itemCounts.output);
|
||||
}
|
||||
|
||||
// Generate recommendation
|
||||
const recommendation = generateRecommendation(
|
||||
preview.estimatedSizeKB,
|
||||
totalItemsOutput,
|
||||
largestNodeItems
|
||||
);
|
||||
|
||||
return { preview, recommendation };
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate smart recommendation based on data characteristics
|
||||
*/
|
||||
function generateRecommendation(
|
||||
totalSizeKB: number,
|
||||
totalItems: number,
|
||||
largestNodeItems: number
|
||||
): ExecutionRecommendation {
|
||||
// Can safely fetch full data
|
||||
if (totalSizeKB <= THRESHOLDS.MAX_RECOMMENDED_SIZE_KB && totalItems <= THRESHOLDS.SMALL_DATASET_ITEMS) {
|
||||
return {
|
||||
canFetchFull: true,
|
||||
suggestedMode: 'full',
|
||||
reason: `Small dataset (${totalSizeKB}KB, ${totalItems} items). Safe to fetch full data.`,
|
||||
};
|
||||
}
|
||||
|
||||
// Moderate size - use summary
|
||||
if (totalSizeKB <= THRESHOLDS.MODERATE_DATASET_SIZE_KB && totalItems <= THRESHOLDS.MODERATE_DATASET_ITEMS) {
|
||||
return {
|
||||
canFetchFull: false,
|
||||
suggestedMode: 'summary',
|
||||
suggestedItemsLimit: 2,
|
||||
reason: `Moderate dataset (${totalSizeKB}KB, ${totalItems} items). Summary mode recommended.`,
|
||||
};
|
||||
}
|
||||
|
||||
// Large dataset - filter with limits
|
||||
const suggestedLimit = Math.max(1, Math.min(5, Math.floor(100 / largestNodeItems)));
|
||||
|
||||
return {
|
||||
canFetchFull: false,
|
||||
suggestedMode: 'filtered',
|
||||
suggestedItemsLimit: suggestedLimit,
|
||||
reason: `Large dataset (${totalSizeKB}KB, ${totalItems} items). Use filtered mode with itemsLimit: ${suggestedLimit}.`,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate items array with metadata
|
||||
*/
|
||||
function truncateItems(
|
||||
items: unknown[][],
|
||||
limit: number
|
||||
): {
|
||||
truncated: unknown[][];
|
||||
metadata: { totalItems: number; itemsShown: number; truncated: boolean };
|
||||
} {
|
||||
if (!Array.isArray(items) || items.length === 0) {
|
||||
return {
|
||||
truncated: items || [],
|
||||
metadata: {
|
||||
totalItems: 0,
|
||||
itemsShown: 0,
|
||||
truncated: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
let totalItems = 0;
|
||||
for (const output of items) {
|
||||
if (Array.isArray(output)) {
|
||||
totalItems += output.length;
|
||||
}
|
||||
}
|
||||
|
||||
// Special case: limit = 0 means structure only
|
||||
if (limit === 0) {
|
||||
const structureOnly = items.map(output => {
|
||||
if (!Array.isArray(output) || output.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return [extractStructure(output[0])];
|
||||
});
|
||||
|
||||
return {
|
||||
truncated: structureOnly,
|
||||
metadata: {
|
||||
totalItems,
|
||||
itemsShown: 0,
|
||||
truncated: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Limit = -1 means unlimited
|
||||
if (limit < 0) {
|
||||
return {
|
||||
truncated: items,
|
||||
metadata: {
|
||||
totalItems,
|
||||
itemsShown: totalItems,
|
||||
truncated: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Apply limit
|
||||
const result: unknown[][] = [];
|
||||
let itemsShown = 0;
|
||||
|
||||
for (const output of items) {
|
||||
if (!Array.isArray(output)) {
|
||||
result.push(output);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (itemsShown >= limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
const remaining = limit - itemsShown;
|
||||
const toTake = Math.min(remaining, output.length);
|
||||
result.push(output.slice(0, toTake));
|
||||
itemsShown += toTake;
|
||||
}
|
||||
|
||||
return {
|
||||
truncated: result,
|
||||
metadata: {
|
||||
totalItems,
|
||||
itemsShown,
|
||||
truncated: itemsShown < totalItems,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter execution data based on options
|
||||
*/
|
||||
export function filterExecutionData(
|
||||
execution: Execution,
|
||||
options: ExecutionFilterOptions
|
||||
): FilteredExecutionResponse {
|
||||
const mode = options.mode || 'summary';
|
||||
|
||||
// Validate and bound itemsLimit
|
||||
let itemsLimit = options.itemsLimit !== undefined ? options.itemsLimit : 2;
|
||||
if (itemsLimit !== -1) { // -1 means unlimited
|
||||
if (itemsLimit < 0) {
|
||||
logger.warn('Invalid itemsLimit, defaulting to 2', { provided: itemsLimit });
|
||||
itemsLimit = 2;
|
||||
}
|
||||
if (itemsLimit > THRESHOLDS.MAX_ITEMS_LIMIT) {
|
||||
logger.warn(`itemsLimit capped at ${THRESHOLDS.MAX_ITEMS_LIMIT}`, { provided: itemsLimit });
|
||||
itemsLimit = THRESHOLDS.MAX_ITEMS_LIMIT;
|
||||
}
|
||||
}
|
||||
|
||||
const includeInputData = options.includeInputData || false;
|
||||
const nodeNamesFilter = options.nodeNames;
|
||||
|
||||
// Calculate duration
|
||||
const duration = execution.stoppedAt && execution.startedAt
|
||||
? new Date(execution.stoppedAt).getTime() - new Date(execution.startedAt).getTime()
|
||||
: undefined;
|
||||
|
||||
const response: FilteredExecutionResponse = {
|
||||
id: execution.id,
|
||||
workflowId: execution.workflowId,
|
||||
status: execution.status,
|
||||
mode,
|
||||
startedAt: execution.startedAt,
|
||||
stoppedAt: execution.stoppedAt,
|
||||
duration,
|
||||
finished: execution.finished,
|
||||
};
|
||||
|
||||
// Handle preview mode
|
||||
if (mode === 'preview') {
|
||||
const { preview, recommendation } = generatePreview(execution);
|
||||
response.preview = preview;
|
||||
response.recommendation = recommendation;
|
||||
return response;
|
||||
}
|
||||
|
||||
// Handle no data case
|
||||
if (!execution.data?.resultData?.runData) {
|
||||
response.summary = {
|
||||
totalNodes: 0,
|
||||
executedNodes: 0,
|
||||
totalItems: 0,
|
||||
hasMoreData: false,
|
||||
};
|
||||
response.nodes = {};
|
||||
|
||||
if (execution.data?.resultData?.error) {
|
||||
response.error = execution.data.resultData.error;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
const runData = execution.data.resultData.runData;
|
||||
let nodeNames = Object.keys(runData);
|
||||
|
||||
// Apply node name filter
|
||||
if (nodeNamesFilter && nodeNamesFilter.length > 0) {
|
||||
nodeNames = nodeNames.filter(name => nodeNamesFilter.includes(name));
|
||||
}
|
||||
|
||||
// Process nodes
|
||||
const processedNodes: Record<string, FilteredNodeData> = {};
|
||||
let totalItems = 0;
|
||||
let hasMoreData = false;
|
||||
|
||||
for (const nodeName of nodeNames) {
|
||||
const nodeData = runData[nodeName];
|
||||
|
||||
if (!Array.isArray(nodeData) || nodeData.length === 0) {
|
||||
processedNodes[nodeName] = {
|
||||
itemsInput: 0,
|
||||
itemsOutput: 0,
|
||||
status: 'success',
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get first run data
|
||||
const firstRun = nodeData[0];
|
||||
const itemCounts = countItems(nodeData);
|
||||
totalItems += itemCounts.output;
|
||||
|
||||
const nodeResult: FilteredNodeData = {
|
||||
executionTime: firstRun.executionTime,
|
||||
itemsInput: itemCounts.input,
|
||||
itemsOutput: itemCounts.output,
|
||||
status: 'success',
|
||||
};
|
||||
|
||||
// Check for errors
|
||||
if (firstRun.error) {
|
||||
nodeResult.status = 'error';
|
||||
nodeResult.error = extractErrorMessage(firstRun.error);
|
||||
}
|
||||
|
||||
// Handle full mode - include all data
|
||||
if (mode === 'full') {
|
||||
nodeResult.data = {
|
||||
output: firstRun.data?.main || [],
|
||||
metadata: {
|
||||
totalItems: itemCounts.output,
|
||||
itemsShown: itemCounts.output,
|
||||
truncated: false,
|
||||
},
|
||||
};
|
||||
|
||||
if (includeInputData && firstRun.inputData) {
|
||||
nodeResult.data.input = firstRun.inputData;
|
||||
}
|
||||
} else {
|
||||
// Summary or filtered mode - apply limits
|
||||
const outputData = firstRun.data?.main || [];
|
||||
const { truncated, metadata } = truncateItems(outputData, itemsLimit);
|
||||
|
||||
if (metadata.truncated) {
|
||||
hasMoreData = true;
|
||||
}
|
||||
|
||||
nodeResult.data = {
|
||||
output: truncated,
|
||||
metadata,
|
||||
};
|
||||
|
||||
if (includeInputData && firstRun.inputData) {
|
||||
nodeResult.data.input = firstRun.inputData;
|
||||
}
|
||||
}
|
||||
|
||||
processedNodes[nodeName] = nodeResult;
|
||||
}
|
||||
|
||||
// Add summary
|
||||
response.summary = {
|
||||
totalNodes: Object.keys(runData).length,
|
||||
executedNodes: nodeNames.length,
|
||||
totalItems,
|
||||
hasMoreData,
|
||||
};
|
||||
|
||||
response.nodes = processedNodes;
|
||||
|
||||
// Include error if present
|
||||
if (execution.data?.resultData?.error) {
|
||||
response.error = execution.data.resultData.error;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process execution based on mode and options
|
||||
* Main entry point for the service
|
||||
*/
|
||||
export function processExecution(
|
||||
execution: Execution,
|
||||
options: ExecutionFilterOptions = {}
|
||||
): FilteredExecutionResponse | Execution {
|
||||
// Legacy behavior: if no mode specified and no filtering options, return original
|
||||
if (!options.mode && !options.nodeNames && options.itemsLimit === undefined) {
|
||||
return execution;
|
||||
}
|
||||
|
||||
return filterExecutionData(execution, options);
|
||||
}
|
||||
@@ -45,6 +45,7 @@ export const workflowSettingsSchema = z.object({
|
||||
saveExecutionProgress: z.boolean().default(true),
|
||||
executionTimeout: z.number().optional(),
|
||||
errorWorkflow: z.string().optional(),
|
||||
callerPolicy: z.enum(['any', 'workflowsFromSameOwner', 'workflowsFromAList']).optional(),
|
||||
});
|
||||
|
||||
// Default settings for workflow creation
|
||||
@@ -95,14 +96,17 @@ export function cleanWorkflowForCreate(workflow: Partial<Workflow>): Partial<Wor
|
||||
|
||||
/**
|
||||
* Clean workflow data for update operations.
|
||||
*
|
||||
*
|
||||
* This function removes read-only and computed fields that should not be sent
|
||||
* in API update requests. It does NOT add any default values or new fields.
|
||||
*
|
||||
*
|
||||
* Note: Unlike cleanWorkflowForCreate, this function does not add default settings.
|
||||
* The n8n API will reject update requests that include properties not present in
|
||||
* the original workflow ("settings must NOT have additional properties" error).
|
||||
*
|
||||
*
|
||||
* Settings are filtered to only include whitelisted properties to prevent API
|
||||
* errors when workflows from n8n contain UI-only or deprecated properties.
|
||||
*
|
||||
* @param workflow - The workflow object to clean
|
||||
* @returns A cleaned partial workflow suitable for API updates
|
||||
*/
|
||||
@@ -129,6 +133,25 @@ export function cleanWorkflowForUpdate(workflow: Workflow): Partial<Workflow> {
|
||||
...cleanedWorkflow
|
||||
} = workflow as any;
|
||||
|
||||
// CRITICAL FIX for Issue #248:
|
||||
// The n8n API has version-specific behavior for settings in workflow updates:
|
||||
//
|
||||
// PROBLEM:
|
||||
// - Some versions reject updates with settings properties (community forum reports)
|
||||
// - Cloud versions REQUIRE settings property to be present (n8n.estyl.team)
|
||||
// - Properties like callerPolicy and executionOrder cause "additional properties" errors
|
||||
//
|
||||
// SOLUTION:
|
||||
// - ALWAYS set settings to empty object {}, regardless of whether it exists
|
||||
// - Empty object satisfies "required property" validation (cloud API)
|
||||
// - Empty object has no "additional properties" to trigger errors (self-hosted)
|
||||
// - n8n API interprets empty settings as "no changes" and preserves existing settings
|
||||
//
|
||||
// References:
|
||||
// - https://community.n8n.io/t/api-workflow-update-endpoint-doesnt-support-setting-callerpolicy/161916
|
||||
// - Tested on n8n.estyl.team (cloud) and localhost (self-hosted)
|
||||
cleanedWorkflow.settings = {};
|
||||
|
||||
return cleanedWorkflow;
|
||||
}
|
||||
|
||||
|
||||
@@ -362,16 +362,36 @@ export class WorkflowDiffEngine {
|
||||
|
||||
// Connection operation validators
|
||||
private validateAddConnection(workflow: Workflow, operation: AddConnectionOperation): string | null {
|
||||
// Check for common parameter mistakes (Issue #249)
|
||||
const operationAny = operation as any;
|
||||
if (operationAny.sourceNodeId || operationAny.targetNodeId) {
|
||||
const wrongParams: string[] = [];
|
||||
if (operationAny.sourceNodeId) wrongParams.push('sourceNodeId');
|
||||
if (operationAny.targetNodeId) wrongParams.push('targetNodeId');
|
||||
|
||||
return `Invalid parameter(s): ${wrongParams.join(', ')}. Use 'source' and 'target' instead. Example: {type: "addConnection", source: "Node Name", target: "Target Name"}`;
|
||||
}
|
||||
|
||||
// Check for missing required parameters
|
||||
if (!operation.source) {
|
||||
return `Missing required parameter 'source'. The addConnection operation requires both 'source' and 'target' parameters. Check that you're using 'source' (not 'sourceNodeId').`;
|
||||
}
|
||||
if (!operation.target) {
|
||||
return `Missing required parameter 'target'. The addConnection operation requires both 'source' and 'target' parameters. Check that you're using 'target' (not 'targetNodeId').`;
|
||||
}
|
||||
|
||||
const sourceNode = this.findNode(workflow, operation.source, operation.source);
|
||||
const targetNode = this.findNode(workflow, operation.target, operation.target);
|
||||
|
||||
|
||||
if (!sourceNode) {
|
||||
return `Source node not found: ${operation.source}`;
|
||||
const availableNodes = workflow.nodes.map(n => n.name).join(', ');
|
||||
return `Source node not found: "${operation.source}". Available nodes: ${availableNodes}`;
|
||||
}
|
||||
if (!targetNode) {
|
||||
return `Target node not found: ${operation.target}`;
|
||||
const availableNodes = workflow.nodes.map(n => n.name).join(', ');
|
||||
return `Target node not found: "${operation.target}". Available nodes: ${availableNodes}`;
|
||||
}
|
||||
|
||||
|
||||
// Check if connection already exists
|
||||
const sourceOutput = operation.sourceOutput || 'main';
|
||||
const existing = workflow.connections[sourceNode.name]?.[sourceOutput];
|
||||
@@ -383,7 +403,7 @@ export class WorkflowDiffEngine {
|
||||
return `Connection already exists from "${sourceNode.name}" to "${targetNode.name}"`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import { EnhancedConfigValidator } from './enhanced-config-validator';
|
||||
import { ExpressionValidator } from './expression-validator';
|
||||
import { ExpressionFormatValidator } from './expression-format-validator';
|
||||
import { NodeSimilarityService, NodeSuggestion } from './node-similarity-service';
|
||||
import { normalizeNodeType } from '../utils/node-type-utils';
|
||||
import { NodeTypeNormalizer } from '../utils/node-type-normalizer';
|
||||
import { Logger } from '../utils/logger';
|
||||
const logger = new Logger({ prefix: '[WorkflowValidator]' });
|
||||
|
||||
@@ -247,7 +247,7 @@ export class WorkflowValidator {
|
||||
// Check for minimum viable workflow
|
||||
if (workflow.nodes.length === 1) {
|
||||
const singleNode = workflow.nodes[0];
|
||||
const normalizedType = normalizeNodeType(singleNode.type);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(singleNode.type);
|
||||
const isWebhook = normalizedType === 'nodes-base.webhook' ||
|
||||
normalizedType === 'nodes-base.webhookTrigger';
|
||||
|
||||
@@ -304,7 +304,7 @@ export class WorkflowValidator {
|
||||
|
||||
// Count trigger nodes - normalize type names first
|
||||
const triggerNodes = workflow.nodes.filter(n => {
|
||||
const normalizedType = normalizeNodeType(n.type);
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(n.type);
|
||||
return normalizedType.toLowerCase().includes('trigger') ||
|
||||
normalizedType.toLowerCase().includes('webhook') ||
|
||||
normalizedType === 'nodes-base.start' ||
|
||||
@@ -364,17 +364,17 @@ export class WorkflowValidator {
|
||||
});
|
||||
}
|
||||
}
|
||||
// Get node definition - try multiple formats
|
||||
let nodeInfo = this.nodeRepository.getNode(node.type);
|
||||
// Normalize node type FIRST to ensure consistent lookup
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(node.type);
|
||||
|
||||
// If not found, try with normalized type
|
||||
if (!nodeInfo) {
|
||||
const normalizedType = normalizeNodeType(node.type);
|
||||
if (normalizedType !== node.type) {
|
||||
nodeInfo = this.nodeRepository.getNode(normalizedType);
|
||||
}
|
||||
// Update node type in place if it was normalized
|
||||
if (normalizedType !== node.type) {
|
||||
node.type = normalizedType;
|
||||
}
|
||||
|
||||
|
||||
// Get node definition using normalized type
|
||||
const nodeInfo = this.nodeRepository.getNode(normalizedType);
|
||||
|
||||
if (!nodeInfo) {
|
||||
// Use NodeSimilarityService to find suggestions
|
||||
const suggestions = await this.similarityService.findSimilarNodes(node.type, 3);
|
||||
@@ -597,8 +597,8 @@ export class WorkflowValidator {
|
||||
// Check for orphaned nodes (exclude sticky notes)
|
||||
for (const node of workflow.nodes) {
|
||||
if (node.disabled || this.isStickyNote(node)) continue;
|
||||
|
||||
const normalizedType = normalizeNodeType(node.type);
|
||||
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(node.type);
|
||||
const isTrigger = normalizedType.toLowerCase().includes('trigger') ||
|
||||
normalizedType.toLowerCase().includes('webhook') ||
|
||||
normalizedType === 'nodes-base.start' ||
|
||||
@@ -658,7 +658,7 @@ export class WorkflowValidator {
|
||||
}
|
||||
|
||||
// Special validation for SplitInBatches node
|
||||
if (sourceNode && sourceNode.type === 'n8n-nodes-base.splitInBatches') {
|
||||
if (sourceNode && sourceNode.type === 'nodes-base.splitInBatches') {
|
||||
this.validateSplitInBatchesConnection(
|
||||
sourceNode,
|
||||
outputIndex,
|
||||
@@ -671,7 +671,7 @@ export class WorkflowValidator {
|
||||
// Check for self-referencing connections
|
||||
if (connection.node === sourceName) {
|
||||
// This is only a warning for non-loop nodes
|
||||
if (sourceNode && sourceNode.type !== 'n8n-nodes-base.splitInBatches') {
|
||||
if (sourceNode && sourceNode.type !== 'nodes-base.splitInBatches') {
|
||||
result.warnings.push({
|
||||
type: 'warning',
|
||||
message: `Node "${sourceName}" has a self-referencing connection. This can cause infinite loops.`
|
||||
@@ -811,14 +811,12 @@ export class WorkflowValidator {
|
||||
// The source should be an AI Agent connecting to this target node as a tool
|
||||
|
||||
// Get target node info to check if it can be used as a tool
|
||||
let targetNodeInfo = this.nodeRepository.getNode(targetNode.type);
|
||||
|
||||
// Try normalized type if not found
|
||||
if (!targetNodeInfo) {
|
||||
const normalizedType = normalizeNodeType(targetNode.type);
|
||||
if (normalizedType !== targetNode.type) {
|
||||
targetNodeInfo = this.nodeRepository.getNode(normalizedType);
|
||||
}
|
||||
const normalizedType = NodeTypeNormalizer.normalizeToFullForm(targetNode.type);
|
||||
let targetNodeInfo = this.nodeRepository.getNode(normalizedType);
|
||||
|
||||
// Try original type if normalization didn't help (fallback for edge cases)
|
||||
if (!targetNodeInfo && normalizedType !== targetNode.type) {
|
||||
targetNodeInfo = this.nodeRepository.getNode(targetNode.type);
|
||||
}
|
||||
|
||||
if (targetNodeInfo && !targetNodeInfo.isAITool && targetNodeInfo.package !== 'n8n-nodes-base') {
|
||||
|
||||
@@ -290,4 +290,86 @@ export interface McpToolResponse {
|
||||
message?: string;
|
||||
code?: string;
|
||||
details?: Record<string, unknown>;
|
||||
executionId?: string;
|
||||
workflowId?: string;
|
||||
}
|
||||
|
||||
// Execution Filtering Types
|
||||
export type ExecutionMode = 'preview' | 'summary' | 'filtered' | 'full';
|
||||
|
||||
export interface ExecutionPreview {
|
||||
totalNodes: number;
|
||||
executedNodes: number;
|
||||
estimatedSizeKB: number;
|
||||
nodes: Record<string, NodePreview>;
|
||||
}
|
||||
|
||||
export interface NodePreview {
|
||||
status: 'success' | 'error';
|
||||
itemCounts: {
|
||||
input: number;
|
||||
output: number;
|
||||
};
|
||||
dataStructure: Record<string, any>;
|
||||
estimatedSizeKB: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface ExecutionRecommendation {
|
||||
canFetchFull: boolean;
|
||||
suggestedMode: ExecutionMode;
|
||||
suggestedItemsLimit?: number;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface ExecutionFilterOptions {
|
||||
mode?: ExecutionMode;
|
||||
nodeNames?: string[];
|
||||
itemsLimit?: number;
|
||||
includeInputData?: boolean;
|
||||
fieldsToInclude?: string[];
|
||||
}
|
||||
|
||||
export interface FilteredExecutionResponse {
|
||||
id: string;
|
||||
workflowId: string;
|
||||
status: ExecutionStatus;
|
||||
mode: ExecutionMode;
|
||||
startedAt: string;
|
||||
stoppedAt?: string;
|
||||
duration?: number;
|
||||
finished: boolean;
|
||||
|
||||
// Preview-specific data
|
||||
preview?: ExecutionPreview;
|
||||
recommendation?: ExecutionRecommendation;
|
||||
|
||||
// Summary/Filtered data
|
||||
summary?: {
|
||||
totalNodes: number;
|
||||
executedNodes: number;
|
||||
totalItems: number;
|
||||
hasMoreData: boolean;
|
||||
};
|
||||
nodes?: Record<string, FilteredNodeData>;
|
||||
|
||||
// Error information
|
||||
error?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface FilteredNodeData {
|
||||
executionTime?: number;
|
||||
itemsInput: number;
|
||||
itemsOutput: number;
|
||||
status: 'success' | 'error';
|
||||
error?: string;
|
||||
data?: {
|
||||
input?: any[][];
|
||||
output?: any[][];
|
||||
metadata: {
|
||||
totalItems: number;
|
||||
itemsShown: number;
|
||||
truncated: boolean;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -95,6 +95,25 @@ export function handleN8nApiError(error: unknown): N8nApiError {
|
||||
return new N8nApiError('Unknown error occurred', undefined, 'UNKNOWN_ERROR', error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format execution error message with guidance to use n8n_get_execution
|
||||
* @param executionId - The execution ID from the failed execution
|
||||
* @param workflowId - Optional workflow ID
|
||||
* @returns Formatted error message with n8n_get_execution guidance
|
||||
*/
|
||||
export function formatExecutionError(executionId: string, workflowId?: string): string {
|
||||
const workflowPrefix = workflowId ? `Workflow ${workflowId} execution ` : 'Execution ';
|
||||
return `${workflowPrefix}${executionId} failed. Use n8n_get_execution({id: '${executionId}', mode: 'preview'}) to investigate the error.`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format error message when no execution ID is available
|
||||
* @returns Generic guidance to check executions
|
||||
*/
|
||||
export function formatNoExecutionError(): string {
|
||||
return "Workflow failed to execute. Use n8n_list_executions to find recent executions, then n8n_get_execution with mode='preview' to investigate.";
|
||||
}
|
||||
|
||||
// Utility to extract user-friendly error messages
|
||||
export function getUserFriendlyErrorMessage(error: N8nApiError): string {
|
||||
switch (error.code) {
|
||||
@@ -109,7 +128,9 @@ export function getUserFriendlyErrorMessage(error: N8nApiError): string {
|
||||
case 'NO_RESPONSE':
|
||||
return 'Unable to connect to n8n. Please check the server URL and ensure n8n is running.';
|
||||
case 'SERVER_ERROR':
|
||||
return 'n8n server error. Please try again later or contact support.';
|
||||
// For server errors, we should not show generic message
|
||||
// Callers should check for execution context and use formatExecutionError instead
|
||||
return error.message || 'n8n server error occurred';
|
||||
default:
|
||||
return error.message || 'An unexpected error occurred';
|
||||
}
|
||||
|
||||
217
src/utils/node-type-normalizer.ts
Normal file
217
src/utils/node-type-normalizer.ts
Normal file
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* Universal Node Type Normalizer
|
||||
*
|
||||
* Converts ANY node type variation to the canonical SHORT form used by the database.
|
||||
* This fixes the critical issue where AI agents or external sources may produce
|
||||
* full-form node types (e.g., "n8n-nodes-base.webhook") which need to be normalized
|
||||
* to match the database storage format (e.g., "nodes-base.webhook").
|
||||
*
|
||||
* **IMPORTANT:** The n8n-mcp database stores nodes in SHORT form:
|
||||
* - n8n-nodes-base → nodes-base
|
||||
* - @n8n/n8n-nodes-langchain → nodes-langchain
|
||||
*
|
||||
* Handles:
|
||||
* - Full form → Short form (n8n-nodes-base.X → nodes-base.X)
|
||||
* - Already short form → Unchanged
|
||||
* - LangChain nodes → Proper short prefix
|
||||
*
|
||||
* @example
|
||||
* NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-base.webhook')
|
||||
* // → 'nodes-base.webhook'
|
||||
*
|
||||
* @example
|
||||
* NodeTypeNormalizer.normalizeToFullForm('nodes-base.webhook')
|
||||
* // → 'nodes-base.webhook' (unchanged)
|
||||
*/
|
||||
|
||||
export interface NodeTypeNormalizationResult {
|
||||
original: string;
|
||||
normalized: string;
|
||||
wasNormalized: boolean;
|
||||
package: 'base' | 'langchain' | 'community' | 'unknown';
|
||||
}
|
||||
|
||||
export class NodeTypeNormalizer {
|
||||
/**
|
||||
* Normalize node type to canonical SHORT form (database format)
|
||||
*
|
||||
* This is the PRIMARY method to use throughout the codebase.
|
||||
* It converts any node type variation to the SHORT form that the database uses.
|
||||
*
|
||||
* **NOTE:** Method name says "ToFullForm" for backward compatibility,
|
||||
* but actually normalizes TO SHORT form to match database storage.
|
||||
*
|
||||
* @param type - Node type in any format
|
||||
* @returns Normalized node type in short form (database format)
|
||||
*
|
||||
* @example
|
||||
* normalizeToFullForm('n8n-nodes-base.webhook')
|
||||
* // → 'nodes-base.webhook'
|
||||
*
|
||||
* @example
|
||||
* normalizeToFullForm('nodes-base.webhook')
|
||||
* // → 'nodes-base.webhook' (unchanged)
|
||||
*
|
||||
* @example
|
||||
* normalizeToFullForm('@n8n/n8n-nodes-langchain.agent')
|
||||
* // → 'nodes-langchain.agent'
|
||||
*/
|
||||
static normalizeToFullForm(type: string): string {
|
||||
if (!type || typeof type !== 'string') {
|
||||
return type;
|
||||
}
|
||||
|
||||
// Normalize full forms to short form (database format)
|
||||
if (type.startsWith('n8n-nodes-base.')) {
|
||||
return type.replace(/^n8n-nodes-base\./, 'nodes-base.');
|
||||
}
|
||||
if (type.startsWith('@n8n/n8n-nodes-langchain.')) {
|
||||
return type.replace(/^@n8n\/n8n-nodes-langchain\./, 'nodes-langchain.');
|
||||
}
|
||||
// Handle n8n-nodes-langchain without @n8n/ prefix
|
||||
if (type.startsWith('n8n-nodes-langchain.')) {
|
||||
return type.replace(/^n8n-nodes-langchain\./, 'nodes-langchain.');
|
||||
}
|
||||
|
||||
// Already in short form or community node - return unchanged
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize with detailed result including metadata
|
||||
*
|
||||
* Use this when you need to know if normalization occurred
|
||||
* or what package the node belongs to.
|
||||
*
|
||||
* @param type - Node type in any format
|
||||
* @returns Detailed normalization result
|
||||
*
|
||||
* @example
|
||||
* normalizeWithDetails('nodes-base.webhook')
|
||||
* // → {
|
||||
* // original: 'nodes-base.webhook',
|
||||
* // normalized: 'n8n-nodes-base.webhook',
|
||||
* // wasNormalized: true,
|
||||
* // package: 'base'
|
||||
* // }
|
||||
*/
|
||||
static normalizeWithDetails(type: string): NodeTypeNormalizationResult {
|
||||
const original = type;
|
||||
const normalized = this.normalizeToFullForm(type);
|
||||
|
||||
return {
|
||||
original,
|
||||
normalized,
|
||||
wasNormalized: original !== normalized,
|
||||
package: this.detectPackage(normalized)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect package type from node type
|
||||
*
|
||||
* @param type - Node type (in any form)
|
||||
* @returns Package identifier
|
||||
*/
|
||||
private static detectPackage(type: string): 'base' | 'langchain' | 'community' | 'unknown' {
|
||||
// Check both short and full forms
|
||||
if (type.startsWith('nodes-base.') || type.startsWith('n8n-nodes-base.')) return 'base';
|
||||
if (type.startsWith('nodes-langchain.') || type.startsWith('@n8n/n8n-nodes-langchain.') || type.startsWith('n8n-nodes-langchain.')) return 'langchain';
|
||||
if (type.includes('.')) return 'community';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch normalize multiple node types
|
||||
*
|
||||
* Use this when you need to normalize multiple types at once.
|
||||
*
|
||||
* @param types - Array of node types
|
||||
* @returns Map of original → normalized types
|
||||
*
|
||||
* @example
|
||||
* normalizeBatch(['nodes-base.webhook', 'nodes-base.set'])
|
||||
* // → Map {
|
||||
* // 'nodes-base.webhook' => 'n8n-nodes-base.webhook',
|
||||
* // 'nodes-base.set' => 'n8n-nodes-base.set'
|
||||
* // }
|
||||
*/
|
||||
static normalizeBatch(types: string[]): Map<string, string> {
|
||||
const result = new Map<string, string>();
|
||||
for (const type of types) {
|
||||
result.set(type, this.normalizeToFullForm(type));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize all node types in a workflow
|
||||
*
|
||||
* This is the key method for fixing workflows before validation.
|
||||
* It normalizes all node types in place while preserving all other
|
||||
* workflow properties.
|
||||
*
|
||||
* @param workflow - Workflow object with nodes array
|
||||
* @returns Workflow with normalized node types
|
||||
*
|
||||
* @example
|
||||
* const workflow = {
|
||||
* nodes: [
|
||||
* { type: 'nodes-base.webhook', id: '1', name: 'Webhook' },
|
||||
* { type: 'nodes-base.set', id: '2', name: 'Set' }
|
||||
* ],
|
||||
* connections: {}
|
||||
* };
|
||||
* const normalized = normalizeWorkflowNodeTypes(workflow);
|
||||
* // workflow.nodes[0].type → 'n8n-nodes-base.webhook'
|
||||
* // workflow.nodes[1].type → 'n8n-nodes-base.set'
|
||||
*/
|
||||
static normalizeWorkflowNodeTypes(workflow: any): any {
|
||||
if (!workflow?.nodes || !Array.isArray(workflow.nodes)) {
|
||||
return workflow;
|
||||
}
|
||||
|
||||
return {
|
||||
...workflow,
|
||||
nodes: workflow.nodes.map((node: any) => ({
|
||||
...node,
|
||||
type: this.normalizeToFullForm(node.type)
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a node type is in full form (needs normalization)
|
||||
*
|
||||
* @param type - Node type to check
|
||||
* @returns True if in full form (will be normalized to short)
|
||||
*/
|
||||
static isFullForm(type: string): boolean {
|
||||
if (!type || typeof type !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
type.startsWith('n8n-nodes-base.') ||
|
||||
type.startsWith('@n8n/n8n-nodes-langchain.') ||
|
||||
type.startsWith('n8n-nodes-langchain.')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a node type is in short form (database format)
|
||||
*
|
||||
* @param type - Node type to check
|
||||
* @returns True if in short form (already in database format)
|
||||
*/
|
||||
static isShortForm(type: string): boolean {
|
||||
if (!type || typeof type !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
type.startsWith('nodes-base.') ||
|
||||
type.startsWith('nodes-langchain.')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -230,8 +230,21 @@ describe('handlers-n8n-manager', () => {
|
||||
data: testWorkflow,
|
||||
message: 'Workflow "Test Workflow" created successfully with ID: test-workflow-id',
|
||||
});
|
||||
expect(mockApiClient.createWorkflow).toHaveBeenCalledWith(input);
|
||||
expect(n8nValidation.validateWorkflowStructure).toHaveBeenCalledWith(input);
|
||||
|
||||
const expectedNormalizedInput = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [{
|
||||
id: 'node1',
|
||||
name: 'Start',
|
||||
type: 'nodes-base.start',
|
||||
typeVersion: 1,
|
||||
position: [100, 100],
|
||||
parameters: {},
|
||||
}],
|
||||
connections: testWorkflow.connections,
|
||||
};
|
||||
expect(mockApiClient.createWorkflow).toHaveBeenCalledWith(expectedNormalizedInput);
|
||||
expect(n8nValidation.validateWorkflowStructure).toHaveBeenCalledWith(expectedNormalizedInput);
|
||||
});
|
||||
|
||||
it('should handle validation errors', async () => {
|
||||
@@ -542,7 +555,7 @@ describe('handlers-n8n-manager', () => {
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
error: 'n8n server error. Please try again later or contact support.',
|
||||
error: 'Service unavailable',
|
||||
code: 'SERVER_ERROR',
|
||||
details: {
|
||||
apiUrl: 'https://n8n.test.com',
|
||||
@@ -642,4 +655,179 @@ describe('handlers-n8n-manager', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleTriggerWebhookWorkflow', () => {
|
||||
it('should trigger webhook successfully', async () => {
|
||||
const webhookResponse = {
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
data: { result: 'success' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
mockApiClient.triggerWebhook.mockResolvedValue(webhookResponse);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test-123',
|
||||
httpMethod: 'POST',
|
||||
data: { test: 'data' }
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
success: true,
|
||||
data: webhookResponse,
|
||||
message: 'Webhook triggered successfully'
|
||||
});
|
||||
});
|
||||
|
||||
it('should extract execution ID from webhook error response', async () => {
|
||||
const apiError = new N8nServerError('Workflow execution failed');
|
||||
apiError.details = {
|
||||
executionId: 'exec_abc123',
|
||||
workflowId: 'wf_xyz789'
|
||||
};
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test-123',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Workflow wf_xyz789 execution exec_abc123 failed');
|
||||
expect(result.error).toContain('n8n_get_execution');
|
||||
expect(result.error).toContain("mode: 'preview'");
|
||||
expect(result.executionId).toBe('exec_abc123');
|
||||
expect(result.workflowId).toBe('wf_xyz789');
|
||||
});
|
||||
|
||||
it('should extract execution ID without workflow ID', async () => {
|
||||
const apiError = new N8nServerError('Execution failed');
|
||||
apiError.details = {
|
||||
executionId: 'exec_only_123'
|
||||
};
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test-123',
|
||||
httpMethod: 'GET'
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Execution exec_only_123 failed');
|
||||
expect(result.error).toContain('n8n_get_execution');
|
||||
expect(result.error).toContain("mode: 'preview'");
|
||||
expect(result.executionId).toBe('exec_only_123');
|
||||
expect(result.workflowId).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle execution ID as "id" field', async () => {
|
||||
const apiError = new N8nServerError('Error');
|
||||
apiError.details = {
|
||||
id: 'exec_from_id_field',
|
||||
workflowId: 'wf_test'
|
||||
};
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.error).toContain('exec_from_id_field');
|
||||
expect(result.executionId).toBe('exec_from_id_field');
|
||||
});
|
||||
|
||||
it('should provide generic guidance when no execution ID is available', async () => {
|
||||
const apiError = new N8nServerError('Server error without execution context');
|
||||
apiError.details = {}; // No execution ID
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Workflow failed to execute');
|
||||
expect(result.error).toContain('n8n_list_executions');
|
||||
expect(result.error).toContain('n8n_get_execution');
|
||||
expect(result.error).toContain("mode='preview'");
|
||||
expect(result.executionId).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should use standard error message for authentication errors', async () => {
|
||||
const authError = new N8nAuthenticationError('Invalid API key');
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(authError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
error: 'Failed to authenticate with n8n. Please check your API key.',
|
||||
code: 'AUTHENTICATION_ERROR',
|
||||
details: undefined
|
||||
});
|
||||
});
|
||||
|
||||
it('should use standard error message for validation errors', async () => {
|
||||
const validationError = new N8nValidationError('Invalid webhook URL');
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(validationError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.error).toBe('Invalid request: Invalid webhook URL');
|
||||
expect(result.code).toBe('VALIDATION_ERROR');
|
||||
});
|
||||
|
||||
it('should handle invalid input with Zod validation error', async () => {
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'not-a-url',
|
||||
httpMethod: 'INVALID_METHOD'
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Invalid input');
|
||||
expect(result.details).toHaveProperty('errors');
|
||||
});
|
||||
|
||||
it('should not include "contact support" in error messages', async () => {
|
||||
const apiError = new N8nServerError('Test error');
|
||||
apiError.details = { executionId: 'test_exec' };
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.error?.toLowerCase()).not.toContain('contact support');
|
||||
expect(result.error?.toLowerCase()).not.toContain('try again later');
|
||||
});
|
||||
|
||||
it('should always recommend preview mode in error messages', async () => {
|
||||
const apiError = new N8nServerError('Error');
|
||||
apiError.details = { executionId: 'test_123' };
|
||||
|
||||
mockApiClient.triggerWebhook.mockRejectedValue(apiError);
|
||||
|
||||
const result = await handlers.handleTriggerWebhookWorkflow({
|
||||
webhookUrl: 'https://n8n.test.com/webhook/test',
|
||||
httpMethod: 'POST'
|
||||
});
|
||||
|
||||
expect(result.error).toMatch(/mode:\s*'preview'/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -499,7 +499,7 @@ describe('handlers-workflow-diff', () => {
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
error: 'n8n server error. Please try again later or contact support.',
|
||||
error: 'Internal server error',
|
||||
code: 'SERVER_ERROR',
|
||||
});
|
||||
});
|
||||
|
||||
665
tests/unit/services/execution-processor.test.ts
Normal file
665
tests/unit/services/execution-processor.test.ts
Normal file
@@ -0,0 +1,665 @@
|
||||
/**
|
||||
* Execution Processor Service Tests
|
||||
*
|
||||
* Comprehensive test coverage for execution filtering and processing
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
generatePreview,
|
||||
filterExecutionData,
|
||||
processExecution,
|
||||
} from '../../../src/services/execution-processor';
|
||||
import {
|
||||
Execution,
|
||||
ExecutionStatus,
|
||||
ExecutionFilterOptions,
|
||||
} from '../../../src/types/n8n-api';
|
||||
|
||||
/**
|
||||
* Test data factories
|
||||
*/
|
||||
|
||||
function createMockExecution(options: {
|
||||
id?: string;
|
||||
status?: ExecutionStatus;
|
||||
nodeData?: Record<string, any>;
|
||||
hasError?: boolean;
|
||||
}): Execution {
|
||||
const { id = 'test-exec-1', status = ExecutionStatus.SUCCESS, nodeData = {}, hasError = false } = options;
|
||||
|
||||
return {
|
||||
id,
|
||||
workflowId: 'workflow-1',
|
||||
status,
|
||||
mode: 'manual',
|
||||
finished: true,
|
||||
startedAt: '2024-01-01T10:00:00.000Z',
|
||||
stoppedAt: '2024-01-01T10:00:05.000Z',
|
||||
data: {
|
||||
resultData: {
|
||||
runData: nodeData,
|
||||
error: hasError ? { message: 'Test error' } : undefined,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createNodeData(itemCount: number, includeError = false) {
|
||||
const items = Array.from({ length: itemCount }, (_, i) => ({
|
||||
json: {
|
||||
id: i + 1,
|
||||
name: `Item ${i + 1}`,
|
||||
value: Math.random() * 100,
|
||||
nested: {
|
||||
field1: `value${i}`,
|
||||
field2: true,
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
return [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 123,
|
||||
data: {
|
||||
main: [items],
|
||||
},
|
||||
error: includeError ? { message: 'Node error' } : undefined,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview Mode Tests
|
||||
*/
|
||||
describe('ExecutionProcessor - Preview Mode', () => {
|
||||
it('should generate preview for empty execution', () => {
|
||||
const execution = createMockExecution({ nodeData: {} });
|
||||
const { preview, recommendation } = generatePreview(execution);
|
||||
|
||||
expect(preview.totalNodes).toBe(0);
|
||||
expect(preview.executedNodes).toBe(0);
|
||||
expect(preview.estimatedSizeKB).toBe(0);
|
||||
expect(recommendation.canFetchFull).toBe(true);
|
||||
expect(recommendation.suggestedMode).toBe('full'); // Empty execution is safe to fetch in full
|
||||
});
|
||||
|
||||
it('should generate preview with accurate item counts', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
'Filter': createNodeData(12),
|
||||
},
|
||||
});
|
||||
|
||||
const { preview } = generatePreview(execution);
|
||||
|
||||
expect(preview.totalNodes).toBe(2);
|
||||
expect(preview.executedNodes).toBe(2);
|
||||
expect(preview.nodes['HTTP Request'].itemCounts.output).toBe(50);
|
||||
expect(preview.nodes['Filter'].itemCounts.output).toBe(12);
|
||||
});
|
||||
|
||||
it('should extract data structure from nodes', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
});
|
||||
|
||||
const { preview } = generatePreview(execution);
|
||||
const structure = preview.nodes['HTTP Request'].dataStructure;
|
||||
|
||||
expect(structure).toHaveProperty('json');
|
||||
expect(structure.json).toHaveProperty('id');
|
||||
expect(structure.json).toHaveProperty('name');
|
||||
expect(structure.json).toHaveProperty('nested');
|
||||
expect(structure.json.id).toBe('number');
|
||||
expect(structure.json.name).toBe('string');
|
||||
expect(typeof structure.json.nested).toBe('object');
|
||||
});
|
||||
|
||||
it('should estimate data size', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const { preview } = generatePreview(execution);
|
||||
|
||||
expect(preview.estimatedSizeKB).toBeGreaterThan(0);
|
||||
expect(preview.nodes['HTTP Request'].estimatedSizeKB).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should detect error status in nodes', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5, true),
|
||||
},
|
||||
});
|
||||
|
||||
const { preview } = generatePreview(execution);
|
||||
|
||||
expect(preview.nodes['HTTP Request'].status).toBe('error');
|
||||
expect(preview.nodes['HTTP Request'].error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should recommend full mode for small datasets', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
});
|
||||
|
||||
const { recommendation } = generatePreview(execution);
|
||||
|
||||
expect(recommendation.canFetchFull).toBe(true);
|
||||
expect(recommendation.suggestedMode).toBe('full');
|
||||
});
|
||||
|
||||
it('should recommend filtered mode for large datasets', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(100),
|
||||
},
|
||||
});
|
||||
|
||||
const { recommendation } = generatePreview(execution);
|
||||
|
||||
expect(recommendation.canFetchFull).toBe(false);
|
||||
expect(recommendation.suggestedMode).toBe('filtered');
|
||||
expect(recommendation.suggestedItemsLimit).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should recommend summary mode for moderate datasets', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(30),
|
||||
},
|
||||
});
|
||||
|
||||
const { recommendation } = generatePreview(execution);
|
||||
|
||||
expect(recommendation.canFetchFull).toBe(false);
|
||||
expect(recommendation.suggestedMode).toBe('summary');
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Filtering Mode Tests
|
||||
*/
|
||||
describe('ExecutionProcessor - Filtering', () => {
|
||||
it('should filter by node names', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(10),
|
||||
'Filter': createNodeData(5),
|
||||
'Set': createNodeData(3),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
nodeNames: ['HTTP Request', 'Filter'],
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
|
||||
expect(result.nodes).toHaveProperty('HTTP Request');
|
||||
expect(result.nodes).toHaveProperty('Filter');
|
||||
expect(result.nodes).not.toHaveProperty('Set');
|
||||
expect(result.summary?.executedNodes).toBe(2);
|
||||
});
|
||||
|
||||
it('should handle non-existent node names gracefully', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(10),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
nodeNames: ['NonExistent'],
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
|
||||
expect(Object.keys(result.nodes || {})).toHaveLength(0);
|
||||
expect(result.summary?.executedNodes).toBe(0);
|
||||
});
|
||||
|
||||
it('should limit items to 0 (structure only)', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 0,
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.metadata.itemsShown).toBe(0);
|
||||
expect(nodeData?.data?.metadata.truncated).toBe(true);
|
||||
expect(nodeData?.data?.metadata.totalItems).toBe(50);
|
||||
|
||||
// Check that we have structure but no actual values
|
||||
const output = nodeData?.data?.output?.[0]?.[0];
|
||||
expect(output).toBeDefined();
|
||||
expect(typeof output).toBe('object');
|
||||
});
|
||||
|
||||
it('should limit items to 2 (default)', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'summary',
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.metadata.itemsShown).toBe(2);
|
||||
expect(nodeData?.data?.metadata.totalItems).toBe(50);
|
||||
expect(nodeData?.data?.metadata.truncated).toBe(true);
|
||||
expect(nodeData?.data?.output?.[0]).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should limit items to custom value', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 5,
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.metadata.itemsShown).toBe(5);
|
||||
expect(nodeData?.data?.metadata.truncated).toBe(true);
|
||||
expect(nodeData?.data?.output?.[0]).toHaveLength(5);
|
||||
});
|
||||
|
||||
it('should not truncate when itemsLimit is -1 (unlimited)', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
itemsLimit: -1,
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.metadata.itemsShown).toBe(50);
|
||||
expect(nodeData?.data?.metadata.totalItems).toBe(50);
|
||||
expect(nodeData?.data?.metadata.truncated).toBe(false);
|
||||
});
|
||||
|
||||
it('should not truncate when items are less than limit', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(3),
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 5,
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.metadata.itemsShown).toBe(3);
|
||||
expect(nodeData?.data?.metadata.truncated).toBe(false);
|
||||
});
|
||||
|
||||
it('should include input data when requested', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 100,
|
||||
inputData: [[{ json: { input: 'test' } }]],
|
||||
data: {
|
||||
main: [[{ json: { output: 'result' } }]],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
includeInputData: true,
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.input).toBeDefined();
|
||||
expect(nodeData?.data?.input?.[0]?.[0]?.json?.input).toBe('test');
|
||||
});
|
||||
|
||||
it('should not include input data by default', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 100,
|
||||
inputData: [[{ json: { input: 'test' } }]],
|
||||
data: {
|
||||
main: [[{ json: { output: 'result' } }]],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const options: ExecutionFilterOptions = {
|
||||
mode: 'filtered',
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, options);
|
||||
const nodeData = result.nodes?.['HTTP Request'];
|
||||
|
||||
expect(nodeData?.data?.input).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Mode Tests
|
||||
*/
|
||||
describe('ExecutionProcessor - Modes', () => {
|
||||
it('should handle preview mode', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'preview' });
|
||||
|
||||
expect(result.mode).toBe('preview');
|
||||
expect(result.preview).toBeDefined();
|
||||
expect(result.recommendation).toBeDefined();
|
||||
expect(result.nodes).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle summary mode', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.mode).toBe('summary');
|
||||
expect(result.summary).toBeDefined();
|
||||
expect(result.nodes).toBeDefined();
|
||||
expect(result.nodes?.['HTTP Request']?.data?.metadata.itemsShown).toBe(2);
|
||||
});
|
||||
|
||||
it('should handle filtered mode', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, {
|
||||
mode: 'filtered',
|
||||
itemsLimit: 5,
|
||||
});
|
||||
|
||||
expect(result.mode).toBe('filtered');
|
||||
expect(result.summary).toBeDefined();
|
||||
expect(result.nodes?.['HTTP Request']?.data?.metadata.itemsShown).toBe(5);
|
||||
});
|
||||
|
||||
it('should handle full mode', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'full' });
|
||||
|
||||
expect(result.mode).toBe('full');
|
||||
expect(result.nodes?.['HTTP Request']?.data?.metadata.itemsShown).toBe(50);
|
||||
expect(result.nodes?.['HTTP Request']?.data?.metadata.truncated).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Edge Cases
|
||||
*/
|
||||
describe('ExecutionProcessor - Edge Cases', () => {
|
||||
it('should handle execution with no data', () => {
|
||||
const execution: Execution = {
|
||||
id: 'test-1',
|
||||
workflowId: 'workflow-1',
|
||||
status: ExecutionStatus.SUCCESS,
|
||||
mode: 'manual',
|
||||
finished: true,
|
||||
startedAt: '2024-01-01T10:00:00.000Z',
|
||||
stoppedAt: '2024-01-01T10:00:05.000Z',
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.summary?.totalNodes).toBe(0);
|
||||
expect(result.summary?.executedNodes).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle execution with error', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
hasError: true,
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should handle empty node data arrays', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': [],
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.nodes?.['HTTP Request']).toBeDefined();
|
||||
expect(result.nodes?.['HTTP Request'].itemsOutput).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle nested data structures', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': [
|
||||
{
|
||||
startTime: Date.now(),
|
||||
executionTime: 100,
|
||||
data: {
|
||||
main: [[{
|
||||
json: {
|
||||
deeply: {
|
||||
nested: {
|
||||
structure: {
|
||||
value: 'test',
|
||||
array: [1, 2, 3],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}]],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const { preview } = generatePreview(execution);
|
||||
const structure = preview.nodes['HTTP Request'].dataStructure;
|
||||
|
||||
expect(structure.json.deeply).toBeDefined();
|
||||
expect(typeof structure.json.deeply).toBe('object');
|
||||
});
|
||||
|
||||
it('should calculate duration correctly', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.duration).toBe(5000); // 5 seconds
|
||||
});
|
||||
|
||||
it('should handle execution without stop time', () => {
|
||||
const execution: Execution = {
|
||||
id: 'test-1',
|
||||
workflowId: 'workflow-1',
|
||||
status: ExecutionStatus.WAITING,
|
||||
mode: 'manual',
|
||||
finished: false,
|
||||
startedAt: '2024-01-01T10:00:00.000Z',
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.duration).toBeUndefined();
|
||||
expect(result.finished).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* processExecution Tests
|
||||
*/
|
||||
describe('ExecutionProcessor - processExecution', () => {
|
||||
it('should return original execution when no options provided', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
});
|
||||
|
||||
const result = processExecution(execution, {});
|
||||
|
||||
expect(result).toBe(execution);
|
||||
});
|
||||
|
||||
it('should process when mode is specified', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
},
|
||||
});
|
||||
|
||||
const result = processExecution(execution, { mode: 'preview' });
|
||||
|
||||
expect(result).not.toBe(execution);
|
||||
expect((result as any).mode).toBe('preview');
|
||||
});
|
||||
|
||||
it('should process when filtering options are provided', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(5),
|
||||
'Filter': createNodeData(3),
|
||||
},
|
||||
});
|
||||
|
||||
const result = processExecution(execution, { nodeNames: ['HTTP Request'] });
|
||||
|
||||
expect(result).not.toBe(execution);
|
||||
expect((result as any).nodes).toHaveProperty('HTTP Request');
|
||||
expect((result as any).nodes).not.toHaveProperty('Filter');
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Summary Statistics Tests
|
||||
*/
|
||||
describe('ExecutionProcessor - Summary Statistics', () => {
|
||||
it('should calculate hasMoreData correctly', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(50),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, {
|
||||
mode: 'summary',
|
||||
itemsLimit: 2,
|
||||
});
|
||||
|
||||
expect(result.summary?.hasMoreData).toBe(true);
|
||||
});
|
||||
|
||||
it('should set hasMoreData to false when all data is included', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(2),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, {
|
||||
mode: 'summary',
|
||||
itemsLimit: 5,
|
||||
});
|
||||
|
||||
expect(result.summary?.hasMoreData).toBe(false);
|
||||
});
|
||||
|
||||
it('should count total items correctly across multiple nodes', () => {
|
||||
const execution = createMockExecution({
|
||||
nodeData: {
|
||||
'HTTP Request': createNodeData(10),
|
||||
'Filter': createNodeData(5),
|
||||
'Set': createNodeData(3),
|
||||
},
|
||||
});
|
||||
|
||||
const result = filterExecutionData(execution, { mode: 'summary' });
|
||||
|
||||
expect(result.summary?.totalItems).toBe(18);
|
||||
});
|
||||
});
|
||||
@@ -344,12 +344,12 @@ describe('n8n-validation', () => {
|
||||
expect(cleaned).not.toHaveProperty('shared');
|
||||
expect(cleaned).not.toHaveProperty('active');
|
||||
|
||||
// Should keep these fields
|
||||
// Should keep name but replace settings with empty object (n8n API limitation)
|
||||
expect(cleaned.name).toBe('Updated Workflow');
|
||||
expect(cleaned.settings).toEqual({ executionOrder: 'v1' });
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
|
||||
it('should not add default settings for update', () => {
|
||||
it('should add empty settings object for cloud API compatibility', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [],
|
||||
@@ -357,7 +357,80 @@ describe('n8n-validation', () => {
|
||||
} as any;
|
||||
|
||||
const cleaned = cleanWorkflowForUpdate(workflow);
|
||||
expect(cleaned).not.toHaveProperty('settings');
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
|
||||
it('should replace settings with empty object to prevent API errors (Issue #248 - final fix)', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [],
|
||||
connections: {},
|
||||
settings: {
|
||||
executionOrder: 'v1' as const,
|
||||
saveDataSuccessExecution: 'none' as const,
|
||||
callerPolicy: 'workflowsFromSameOwner' as const,
|
||||
timeSavedPerExecution: 5, // UI-only property
|
||||
},
|
||||
} as any;
|
||||
|
||||
const cleaned = cleanWorkflowForUpdate(workflow);
|
||||
|
||||
// Settings replaced with empty object (satisfies both API versions)
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
|
||||
it('should replace settings with callerPolicy (Issue #248 - API limitation)', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [],
|
||||
connections: {},
|
||||
settings: {
|
||||
executionOrder: 'v1' as const,
|
||||
callerPolicy: 'workflowsFromSameOwner' as const,
|
||||
errorWorkflow: 'N2O2nZy3aUiBRGFN',
|
||||
},
|
||||
} as any;
|
||||
|
||||
const cleaned = cleanWorkflowForUpdate(workflow);
|
||||
|
||||
// Settings replaced with empty object (n8n API rejects updates with settings properties)
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
|
||||
it('should replace all settings regardless of content (Issue #248 - API design)', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [],
|
||||
connections: {},
|
||||
settings: {
|
||||
executionOrder: 'v0' as const,
|
||||
timezone: 'UTC',
|
||||
saveDataErrorExecution: 'all' as const,
|
||||
saveDataSuccessExecution: 'none' as const,
|
||||
saveManualExecutions: false,
|
||||
saveExecutionProgress: false,
|
||||
executionTimeout: 300,
|
||||
errorWorkflow: 'error-workflow-id',
|
||||
callerPolicy: 'workflowsFromAList' as const,
|
||||
},
|
||||
} as any;
|
||||
|
||||
const cleaned = cleanWorkflowForUpdate(workflow);
|
||||
|
||||
// Settings replaced with empty object due to n8n API limitation (cannot update settings via API)
|
||||
// See: https://community.n8n.io/t/api-workflow-update-endpoint-doesnt-support-setting-callerpolicy/161916
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
|
||||
it('should handle workflows without settings gracefully', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
nodes: [],
|
||||
connections: {},
|
||||
} as any;
|
||||
|
||||
const cleaned = cleanWorkflowForUpdate(workflow);
|
||||
expect(cleaned.settings).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1236,7 +1309,7 @@ describe('n8n-validation', () => {
|
||||
expect(forUpdate).not.toHaveProperty('active');
|
||||
expect(forUpdate).not.toHaveProperty('tags');
|
||||
expect(forUpdate).not.toHaveProperty('meta');
|
||||
expect(forUpdate).not.toHaveProperty('settings'); // Should not add defaults for update
|
||||
expect(forUpdate.settings).toEqual({}); // Settings replaced with empty object for API compatibility
|
||||
expect(validateWorkflowStructure(forUpdate)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -579,7 +579,6 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
|
||||
|
||||
const result = await validator.validateWorkflow(workflow as any);
|
||||
|
||||
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('n8n-nodes-base.webhook');
|
||||
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('nodes-base.webhook');
|
||||
});
|
||||
|
||||
@@ -599,7 +598,6 @@ describe('WorkflowValidator - Comprehensive Tests', () => {
|
||||
|
||||
const result = await validator.validateWorkflow(workflow as any);
|
||||
|
||||
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('@n8n/n8n-nodes-langchain.agent');
|
||||
expect(mockNodeRepository.getNode).toHaveBeenCalledWith('nodes-langchain.agent');
|
||||
});
|
||||
|
||||
|
||||
@@ -645,9 +645,9 @@ describe('WorkflowValidator - Mock-based Unit Tests', () => {
|
||||
|
||||
await validator.validateWorkflow(workflow as any);
|
||||
|
||||
// Should have called getNode for each node type
|
||||
expect(mockGetNode).toHaveBeenCalledWith('n8n-nodes-base.httpRequest');
|
||||
expect(mockGetNode).toHaveBeenCalledWith('n8n-nodes-base.set');
|
||||
// Should have called getNode for each node type (normalized to short form)
|
||||
expect(mockGetNode).toHaveBeenCalledWith('nodes-base.httpRequest');
|
||||
expect(mockGetNode).toHaveBeenCalledWith('nodes-base.set');
|
||||
expect(mockGetNode).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
@@ -712,7 +712,7 @@ describe('WorkflowValidator - Mock-based Unit Tests', () => {
|
||||
// Should call getNode for the same type multiple times (current implementation)
|
||||
// Note: This test documents current behavior. Could be optimized in the future.
|
||||
const httpRequestCalls = mockGetNode.mock.calls.filter(
|
||||
call => call[0] === 'n8n-nodes-base.httpRequest'
|
||||
call => call[0] === 'nodes-base.httpRequest'
|
||||
);
|
||||
expect(httpRequestCalls.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
@@ -449,10 +449,10 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
|
||||
});
|
||||
|
||||
it('should normalize and validate nodes-base prefix to find the node', async () => {
|
||||
// Arrange - Test that nodes-base prefix is normalized to find the node
|
||||
// The repository only has the node under the normalized key
|
||||
// Arrange - Test that full-form types are normalized to short form to find the node
|
||||
// The repository only has the node under the SHORT normalized key (database format)
|
||||
const nodeData = {
|
||||
'nodes-base.webhook': { // Repository has it under normalized form
|
||||
'nodes-base.webhook': { // Repository has it under SHORT form (database format)
|
||||
type: 'nodes-base.webhook',
|
||||
displayName: 'Webhook',
|
||||
isVersioned: true,
|
||||
@@ -462,10 +462,11 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
|
||||
};
|
||||
|
||||
// Mock repository that simulates the normalization behavior
|
||||
// After our changes, getNode is called with the already-normalized type (short form)
|
||||
const mockRepository = {
|
||||
getNode: vi.fn((type: string) => {
|
||||
// First call with original type returns null
|
||||
// Second call with normalized type returns the node
|
||||
// The validator now normalizes to short form before calling getNode
|
||||
// So getNode receives 'nodes-base.webhook'
|
||||
if (type === 'nodes-base.webhook') {
|
||||
return nodeData['nodes-base.webhook'];
|
||||
}
|
||||
@@ -489,7 +490,7 @@ describe('WorkflowValidator - Simple Unit Tests', () => {
|
||||
{
|
||||
id: '1',
|
||||
name: 'Webhook',
|
||||
type: 'nodes-base.webhook', // Using the alternative prefix
|
||||
type: 'n8n-nodes-base.webhook', // Using the full-form prefix (will be normalized to short)
|
||||
position: [250, 300] as [number, number],
|
||||
parameters: {},
|
||||
typeVersion: 2
|
||||
|
||||
171
tests/unit/utils/n8n-errors.test.ts
Normal file
171
tests/unit/utils/n8n-errors.test.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
formatExecutionError,
|
||||
formatNoExecutionError,
|
||||
getUserFriendlyErrorMessage,
|
||||
N8nApiError,
|
||||
N8nAuthenticationError,
|
||||
N8nNotFoundError,
|
||||
N8nValidationError,
|
||||
N8nRateLimitError,
|
||||
N8nServerError
|
||||
} from '../../../src/utils/n8n-errors';
|
||||
|
||||
describe('formatExecutionError', () => {
|
||||
it('should format error with both execution ID and workflow ID', () => {
|
||||
const result = formatExecutionError('exec_12345', 'wf_abc');
|
||||
|
||||
expect(result).toBe("Workflow wf_abc execution exec_12345 failed. Use n8n_get_execution({id: 'exec_12345', mode: 'preview'}) to investigate the error.");
|
||||
expect(result).toContain('mode: \'preview\'');
|
||||
expect(result).toContain('exec_12345');
|
||||
expect(result).toContain('wf_abc');
|
||||
});
|
||||
|
||||
it('should format error with only execution ID', () => {
|
||||
const result = formatExecutionError('exec_67890');
|
||||
|
||||
expect(result).toBe("Execution exec_67890 failed. Use n8n_get_execution({id: 'exec_67890', mode: 'preview'}) to investigate the error.");
|
||||
expect(result).toContain('mode: \'preview\'');
|
||||
expect(result).toContain('exec_67890');
|
||||
expect(result).not.toContain('Workflow');
|
||||
});
|
||||
|
||||
it('should include preview mode guidance', () => {
|
||||
const result = formatExecutionError('test_id');
|
||||
|
||||
expect(result).toMatch(/mode:\s*'preview'/);
|
||||
});
|
||||
|
||||
it('should format with undefined workflow ID (treated as missing)', () => {
|
||||
const result = formatExecutionError('exec_123', undefined);
|
||||
|
||||
expect(result).toBe("Execution exec_123 failed. Use n8n_get_execution({id: 'exec_123', mode: 'preview'}) to investigate the error.");
|
||||
});
|
||||
|
||||
it('should properly escape execution ID in suggestion', () => {
|
||||
const result = formatExecutionError('exec-with-special_chars.123');
|
||||
|
||||
expect(result).toContain("id: 'exec-with-special_chars.123'");
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatNoExecutionError', () => {
|
||||
it('should provide guidance to check recent executions', () => {
|
||||
const result = formatNoExecutionError();
|
||||
|
||||
expect(result).toBe("Workflow failed to execute. Use n8n_list_executions to find recent executions, then n8n_get_execution with mode='preview' to investigate.");
|
||||
expect(result).toContain('n8n_list_executions');
|
||||
expect(result).toContain('n8n_get_execution');
|
||||
expect(result).toContain("mode='preview'");
|
||||
});
|
||||
|
||||
it('should include preview mode in guidance', () => {
|
||||
const result = formatNoExecutionError();
|
||||
|
||||
expect(result).toMatch(/mode\s*=\s*'preview'/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserFriendlyErrorMessage', () => {
|
||||
it('should handle authentication error', () => {
|
||||
const error = new N8nAuthenticationError('Invalid API key');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Failed to authenticate with n8n. Please check your API key.');
|
||||
});
|
||||
|
||||
it('should handle not found error', () => {
|
||||
const error = new N8nNotFoundError('Workflow', '123');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toContain('not found');
|
||||
});
|
||||
|
||||
it('should handle validation error', () => {
|
||||
const error = new N8nValidationError('Missing required field');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Invalid request: Missing required field');
|
||||
});
|
||||
|
||||
it('should handle rate limit error', () => {
|
||||
const error = new N8nRateLimitError(60);
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Too many requests. Please wait a moment and try again.');
|
||||
});
|
||||
|
||||
it('should handle server error with custom message', () => {
|
||||
const error = new N8nServerError('Database connection failed', 503);
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Database connection failed');
|
||||
});
|
||||
|
||||
it('should handle server error without message', () => {
|
||||
const error = new N8nApiError('', 500, 'SERVER_ERROR');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('n8n server error occurred');
|
||||
});
|
||||
|
||||
it('should handle no response error', () => {
|
||||
const error = new N8nApiError('Network error', undefined, 'NO_RESPONSE');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Unable to connect to n8n. Please check the server URL and ensure n8n is running.');
|
||||
});
|
||||
|
||||
it('should handle unknown error with message', () => {
|
||||
const error = new N8nApiError('Custom error message');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('Custom error message');
|
||||
});
|
||||
|
||||
it('should handle unknown error without message', () => {
|
||||
const error = new N8nApiError('');
|
||||
const message = getUserFriendlyErrorMessage(error);
|
||||
|
||||
expect(message).toBe('An unexpected error occurred');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error message integration', () => {
|
||||
it('should use formatExecutionError for webhook failures with execution ID', () => {
|
||||
const executionId = 'exec_webhook_123';
|
||||
const workflowId = 'wf_webhook_abc';
|
||||
const message = formatExecutionError(executionId, workflowId);
|
||||
|
||||
expect(message).toContain('Workflow wf_webhook_abc execution exec_webhook_123 failed');
|
||||
expect(message).toContain('n8n_get_execution');
|
||||
expect(message).toContain("mode: 'preview'");
|
||||
});
|
||||
|
||||
it('should use formatNoExecutionError for server errors without execution context', () => {
|
||||
const message = formatNoExecutionError();
|
||||
|
||||
expect(message).toContain('Workflow failed to execute');
|
||||
expect(message).toContain('n8n_list_executions');
|
||||
expect(message).toContain('n8n_get_execution');
|
||||
});
|
||||
|
||||
it('should not include "contact support" in any error message', () => {
|
||||
const executionMessage = formatExecutionError('test');
|
||||
const noExecutionMessage = formatNoExecutionError();
|
||||
const serverError = new N8nServerError();
|
||||
const serverErrorMessage = getUserFriendlyErrorMessage(serverError);
|
||||
|
||||
expect(executionMessage.toLowerCase()).not.toContain('contact support');
|
||||
expect(noExecutionMessage.toLowerCase()).not.toContain('contact support');
|
||||
expect(serverErrorMessage.toLowerCase()).not.toContain('contact support');
|
||||
});
|
||||
|
||||
it('should always guide users to use preview mode first', () => {
|
||||
const executionMessage = formatExecutionError('test');
|
||||
const noExecutionMessage = formatNoExecutionError();
|
||||
|
||||
expect(executionMessage).toContain("mode: 'preview'");
|
||||
expect(noExecutionMessage).toContain("mode='preview'");
|
||||
});
|
||||
});
|
||||
340
tests/unit/utils/node-type-normalizer.test.ts
Normal file
340
tests/unit/utils/node-type-normalizer.test.ts
Normal file
@@ -0,0 +1,340 @@
|
||||
/**
|
||||
* Tests for NodeTypeNormalizer
|
||||
*
|
||||
* Comprehensive test suite for the node type normalization utility
|
||||
* that fixes the critical issue of AI agents producing short-form node types
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { NodeTypeNormalizer } from '../../../src/utils/node-type-normalizer';
|
||||
|
||||
describe('NodeTypeNormalizer', () => {
|
||||
describe('normalizeToFullForm', () => {
|
||||
describe('Base nodes', () => {
|
||||
it('should normalize full base form to short form', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-base.webhook'))
|
||||
.toBe('nodes-base.webhook');
|
||||
});
|
||||
|
||||
it('should normalize full base form with different node names', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-base.httpRequest'))
|
||||
.toBe('nodes-base.httpRequest');
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-base.set'))
|
||||
.toBe('nodes-base.set');
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-base.slack'))
|
||||
.toBe('nodes-base.slack');
|
||||
});
|
||||
|
||||
it('should leave short base form unchanged', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('nodes-base.webhook'))
|
||||
.toBe('nodes-base.webhook');
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('nodes-base.httpRequest'))
|
||||
.toBe('nodes-base.httpRequest');
|
||||
});
|
||||
});
|
||||
|
||||
describe('LangChain nodes', () => {
|
||||
it('should normalize full langchain form to short form', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('@n8n/n8n-nodes-langchain.agent'))
|
||||
.toBe('nodes-langchain.agent');
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('@n8n/n8n-nodes-langchain.openAi'))
|
||||
.toBe('nodes-langchain.openAi');
|
||||
});
|
||||
|
||||
it('should normalize langchain form with n8n- prefix but missing @n8n/', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('n8n-nodes-langchain.agent'))
|
||||
.toBe('nodes-langchain.agent');
|
||||
});
|
||||
|
||||
it('should leave short langchain form unchanged', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('nodes-langchain.agent'))
|
||||
.toBe('nodes-langchain.agent');
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('nodes-langchain.openAi'))
|
||||
.toBe('nodes-langchain.openAi');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Edge cases', () => {
|
||||
it('should handle empty string', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('')).toBe('');
|
||||
});
|
||||
|
||||
it('should handle null', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm(null as any)).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle undefined', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm(undefined as any)).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should handle non-string input', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm(123 as any)).toBe(123);
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm({} as any)).toEqual({});
|
||||
});
|
||||
|
||||
it('should leave community nodes unchanged', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('custom-package.myNode'))
|
||||
.toBe('custom-package.myNode');
|
||||
});
|
||||
|
||||
it('should leave nodes without prefixes unchanged', () => {
|
||||
expect(NodeTypeNormalizer.normalizeToFullForm('someRandomNode'))
|
||||
.toBe('someRandomNode');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeWithDetails', () => {
|
||||
it('should return normalization details for full base form', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWithDetails('n8n-nodes-base.webhook');
|
||||
|
||||
expect(result).toEqual({
|
||||
original: 'n8n-nodes-base.webhook',
|
||||
normalized: 'nodes-base.webhook',
|
||||
wasNormalized: true,
|
||||
package: 'base'
|
||||
});
|
||||
});
|
||||
|
||||
it('should return normalization details for already short form', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWithDetails('nodes-base.webhook');
|
||||
|
||||
expect(result).toEqual({
|
||||
original: 'nodes-base.webhook',
|
||||
normalized: 'nodes-base.webhook',
|
||||
wasNormalized: false,
|
||||
package: 'base'
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect langchain package', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWithDetails('@n8n/n8n-nodes-langchain.agent');
|
||||
|
||||
expect(result).toEqual({
|
||||
original: '@n8n/n8n-nodes-langchain.agent',
|
||||
normalized: 'nodes-langchain.agent',
|
||||
wasNormalized: true,
|
||||
package: 'langchain'
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect community package', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWithDetails('custom-package.myNode');
|
||||
|
||||
expect(result).toEqual({
|
||||
original: 'custom-package.myNode',
|
||||
normalized: 'custom-package.myNode',
|
||||
wasNormalized: false,
|
||||
package: 'community'
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect unknown package', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWithDetails('unknownNode');
|
||||
|
||||
expect(result).toEqual({
|
||||
original: 'unknownNode',
|
||||
normalized: 'unknownNode',
|
||||
wasNormalized: false,
|
||||
package: 'unknown'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeBatch', () => {
|
||||
it('should normalize multiple node types', () => {
|
||||
const types = ['n8n-nodes-base.webhook', 'n8n-nodes-base.set', '@n8n/n8n-nodes-langchain.agent'];
|
||||
const result = NodeTypeNormalizer.normalizeBatch(types);
|
||||
|
||||
expect(result.size).toBe(3);
|
||||
expect(result.get('n8n-nodes-base.webhook')).toBe('nodes-base.webhook');
|
||||
expect(result.get('n8n-nodes-base.set')).toBe('nodes-base.set');
|
||||
expect(result.get('@n8n/n8n-nodes-langchain.agent')).toBe('nodes-langchain.agent');
|
||||
});
|
||||
|
||||
it('should handle empty array', () => {
|
||||
const result = NodeTypeNormalizer.normalizeBatch([]);
|
||||
expect(result.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle mixed forms', () => {
|
||||
const types = [
|
||||
'n8n-nodes-base.webhook',
|
||||
'nodes-base.set',
|
||||
'@n8n/n8n-nodes-langchain.agent',
|
||||
'nodes-langchain.openAi'
|
||||
];
|
||||
const result = NodeTypeNormalizer.normalizeBatch(types);
|
||||
|
||||
expect(result.size).toBe(4);
|
||||
expect(result.get('n8n-nodes-base.webhook')).toBe('nodes-base.webhook');
|
||||
expect(result.get('nodes-base.set')).toBe('nodes-base.set');
|
||||
expect(result.get('@n8n/n8n-nodes-langchain.agent')).toBe('nodes-langchain.agent');
|
||||
expect(result.get('nodes-langchain.openAi')).toBe('nodes-langchain.openAi');
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeWorkflowNodeTypes', () => {
|
||||
it('should normalize all nodes in workflow', () => {
|
||||
const workflow = {
|
||||
nodes: [
|
||||
{ type: 'n8n-nodes-base.webhook', id: '1', name: 'Webhook', parameters: {}, position: [0, 0] },
|
||||
{ type: 'n8n-nodes-base.set', id: '2', name: 'Set', parameters: {}, position: [100, 100] }
|
||||
],
|
||||
connections: {}
|
||||
};
|
||||
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
|
||||
expect(result.nodes[0].type).toBe('nodes-base.webhook');
|
||||
expect(result.nodes[1].type).toBe('nodes-base.set');
|
||||
});
|
||||
|
||||
it('should preserve all other node properties', () => {
|
||||
const workflow = {
|
||||
nodes: [
|
||||
{
|
||||
type: 'n8n-nodes-base.webhook',
|
||||
id: 'test-id',
|
||||
name: 'Test Webhook',
|
||||
parameters: { path: '/test' },
|
||||
position: [250, 300],
|
||||
credentials: { webhookAuth: { id: '1', name: 'Test' } }
|
||||
}
|
||||
],
|
||||
connections: {}
|
||||
};
|
||||
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
|
||||
expect(result.nodes[0]).toEqual({
|
||||
type: 'nodes-base.webhook', // normalized to short form
|
||||
id: 'test-id', // preserved
|
||||
name: 'Test Webhook', // preserved
|
||||
parameters: { path: '/test' }, // preserved
|
||||
position: [250, 300], // preserved
|
||||
credentials: { webhookAuth: { id: '1', name: 'Test' } } // preserved
|
||||
});
|
||||
});
|
||||
|
||||
it('should preserve workflow properties', () => {
|
||||
const workflow = {
|
||||
name: 'Test Workflow',
|
||||
active: true,
|
||||
nodes: [
|
||||
{ type: 'n8n-nodes-base.webhook', id: '1', name: 'Webhook', parameters: {}, position: [0, 0] }
|
||||
],
|
||||
connections: {
|
||||
'1': { main: [[{ node: '2', type: 'main', index: 0 }]] }
|
||||
}
|
||||
};
|
||||
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
|
||||
expect(result.name).toBe('Test Workflow');
|
||||
expect(result.active).toBe(true);
|
||||
expect(result.connections).toEqual({
|
||||
'1': { main: [[{ node: '2', type: 'main', index: 0 }]] }
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle workflow without nodes', () => {
|
||||
const workflow = { connections: {} };
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
expect(result).toEqual(workflow);
|
||||
});
|
||||
|
||||
it('should handle null workflow', () => {
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(null);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle workflow with empty nodes array', () => {
|
||||
const workflow = { nodes: [], connections: {} };
|
||||
const result = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
expect(result.nodes).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isFullForm', () => {
|
||||
it('should return true for full base form', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm('n8n-nodes-base.webhook')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for full langchain form', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm('@n8n/n8n-nodes-langchain.agent')).toBe(true);
|
||||
expect(NodeTypeNormalizer.isFullForm('n8n-nodes-langchain.agent')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for short base form', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm('nodes-base.webhook')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for short langchain form', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm('nodes-langchain.agent')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for community nodes', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm('custom-package.myNode')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined', () => {
|
||||
expect(NodeTypeNormalizer.isFullForm(null as any)).toBe(false);
|
||||
expect(NodeTypeNormalizer.isFullForm(undefined as any)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isShortForm', () => {
|
||||
it('should return true for short base form', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm('nodes-base.webhook')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for short langchain form', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm('nodes-langchain.agent')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for full base form', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm('n8n-nodes-base.webhook')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for full langchain form', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm('@n8n/n8n-nodes-langchain.agent')).toBe(false);
|
||||
expect(NodeTypeNormalizer.isShortForm('n8n-nodes-langchain.agent')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for community nodes', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm('custom-package.myNode')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined', () => {
|
||||
expect(NodeTypeNormalizer.isShortForm(null as any)).toBe(false);
|
||||
expect(NodeTypeNormalizer.isShortForm(undefined as any)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Integration scenarios', () => {
|
||||
it('should handle the critical use case from P0-R1', () => {
|
||||
// This is the exact scenario - normalize full form to match database
|
||||
const fullFormType = 'n8n-nodes-base.webhook'; // External source produces this
|
||||
const normalized = NodeTypeNormalizer.normalizeToFullForm(fullFormType);
|
||||
|
||||
expect(normalized).toBe('nodes-base.webhook'); // Database stores in short form
|
||||
});
|
||||
|
||||
it('should work correctly in a workflow validation scenario', () => {
|
||||
const workflow = {
|
||||
nodes: [
|
||||
{ type: 'n8n-nodes-base.webhook', id: '1', name: 'Webhook', parameters: {}, position: [0, 0] },
|
||||
{ type: 'n8n-nodes-base.httpRequest', id: '2', name: 'HTTP', parameters: {}, position: [200, 0] },
|
||||
{ type: 'nodes-base.set', id: '3', name: 'Set', parameters: {}, position: [400, 0] }
|
||||
],
|
||||
connections: {}
|
||||
};
|
||||
|
||||
const normalized = NodeTypeNormalizer.normalizeWorkflowNodeTypes(workflow);
|
||||
|
||||
// All node types should now be in short form for database lookup
|
||||
expect(normalized.nodes.every((n: any) => n.type.startsWith('nodes-base.'))).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user