Skip to content

Self-improving AI Employees for everyone

Add Tool Use for the AI Agent.

trohitg32235fe12a76
diff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsx
index fa98316b..7661dee4 100644
--- a/client/src/Dashboard.tsx
+++ b/client/src/Dashboard.tsx
@@ -26,6 +26,7 @@ import { SCHEDULER_NODE_TYPES } from './nodeDefinitions/schedulerNodes';
import { CHAT_NODE_TYPES } from './nodeDefinitions/chatNodes';
import { CODE_NODE_TYPES } from './nodeDefinitions/codeNodes';
import { UTILITY_NODE_TYPES } from './nodeDefinitions/utilityNodes';
+import { TOOL_NODE_TYPES } from './nodeDefinitions/toolNodes';
import ParameterPanel from './ParameterPanel';
import LocationParameterPanel from './components/LocationParameterPanel';
import { useAppStore } from './store/useAppStore';
@@ -94,6 +95,9 @@ const createNodeTypes = (): Record<string, React.ComponentType<any>> => {
// Utility nodes (HTTP, Webhooks) use SquareNode component
// Note: webhookTrigger is already handled as trigger above
types[type] = SquareNode;
+ } else if (TOOL_NODE_TYPES.includes(type)) {
+ // Tool nodes (Calculator, etc.) use circular ModelNode like Simple Memory
+ types[type] = ModelNode;
} else if (definition?.group?.includes('model')) {
// Fallback for other model nodes
types[type] = ModelNode;
@@ -276,7 +280,7 @@ const DashboardContent: React.FC = () => {
} = useWorkflowManagement();
const { collapsedSections, searchQuery, setSearchQuery, toggleSection } = useComponentPalette();
- const { saveNodeParameters, getAllNodeParameters, executeWorkflow, deployWorkflow, cancelDeployment, nodeStatuses, deploymentStatus, workflowLock, isConnected } = useWebSocket();
+ const { saveNodeParameters, executeWorkflow, deployWorkflow, cancelDeployment, nodeStatuses, deploymentStatus, workflowLock } = useWebSocket();
// Scope deployment and lock to current workflow (n8n pattern)
// Only show as "running" or "locked" if it applies to the currently viewed workflow
@@ -582,17 +586,13 @@ const DashboardContent: React.FC = () => {
}
try {
- console.log('[Dashboard] Starting deployment (using backend stored settings)');
-
// Settings are already synced to backend via WebSocket from SettingsPanel
// Backend will use the stored settings
const result = await deployWorkflow(currentWorkflow.id, nodes, edges, 'default');
if (!result.success) {
- console.error('[Dashboard] Deployment failed to start:', result.error);
+ console.error('[Dashboard] Deployment failed:', result.error);
alert(`Failed to start deployment: ${result.error}`);
- } else {
- console.log('[Dashboard] Deployment started successfully');
}
} catch (error: any) {
console.error('[Dashboard] Deployment error:', error);
diff --git a/client/src/components/AIAgentNode.tsx b/client/src/components/AIAgentNode.tsx
index 5806b2f9..93ed158f 100644
--- a/client/src/components/AIAgentNode.tsx
+++ b/client/src/components/AIAgentNode.tsx
@@ -11,8 +11,11 @@ const PHASE_CONFIG: Record<string, { icon: string; label: string; color: string
initializing: { icon: '⚑', label: 'Initializing', color: '#8be9fd' },
loading_memory: { icon: 'πŸ’Ύ', label: 'Loading Memory', color: '#bd93f9' },
memory_loaded: { icon: 'βœ“', label: 'Memory Ready', color: '#50fa7b' },
+ building_tools: { icon: 'πŸ”§', label: 'Building Tools', color: '#ffb86c' },
building_graph: { icon: 'πŸ”—', label: 'Building Graph', color: '#ffb86c' },
invoking_llm: { icon: '🧠', label: 'Thinking...', color: '#ff79c6' },
+ executing_tool: { icon: '⚑', label: 'Using Tool', color: '#ff79c6' },
+ tool_completed: { icon: 'βœ“', label: 'Tool Done', color: '#50fa7b' },
saving_memory: { icon: 'πŸ’Ύ', label: 'Saving Memory', color: '#bd93f9' },
};
@@ -206,29 +209,6 @@ const AIAgentNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnecta
{isExecuting && phaseConfig ? phaseConfig.label : 'LangGraph Agent'}
</div>
- {/* Memory Info Badge - shows when loading/using memory */}
- {isExecuting && nodeStatus?.data?.has_memory && (
- <div style={{
- position: 'absolute',
- top: '-8px',
- left: '50%',
- transform: 'translateX(-50%)',
- backgroundColor: theme.dracula.purple,
- color: '#fff',
- fontSize: '10px',
- padding: '2px 8px',
- borderRadius: '10px',
- whiteSpace: 'nowrap',
- boxShadow: `0 0 8px ${theme.dracula.purple}80`,
- animation: 'pulse 1s ease-in-out infinite',
- zIndex: 30
- }}>
- {nodeStatus?.data?.history_count !== undefined
- ? `Memory: ${nodeStatus.data.history_count} msgs`
- : 'Using Memory'}
- </div>
- )}
-
{/* Parameter Labels */}
<div style={{
position: 'absolute',
@@ -257,18 +237,10 @@ const AIAgentNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnecta
left: '35%',
width: theme.nodeSize.handle,
height: theme.nodeSize.handle,
- backgroundColor: isExecuting && nodeStatus?.data?.has_memory
- ? theme.dracula.purple
- : theme.colors.background,
- border: `2px solid ${isExecuting && nodeStatus?.data?.has_memory
- ? theme.dracula.purple
- : theme.colors.textSecondary}`,
+ backgroundColor: theme.colors.background,
+ border: `2px solid ${theme.colors.textSecondary}`,
borderRadius: '0',
- transform: 'translateX(-50%) rotate(45deg)',
- boxShadow: isExecuting && nodeStatus?.data?.has_memory
- ? `0 0 10px ${theme.dracula.purple}`
- : 'none',
- transition: 'all 0.3s ease'
+ transform: 'translateX(-50%) rotate(45deg)'
}}
title="Memory"
/>
diff --git a/client/src/components/ModelNode.tsx b/client/src/components/ModelNode.tsx
index f91aae66..ab23cc24 100644
--- a/client/src/components/ModelNode.tsx
+++ b/client/src/components/ModelNode.tsx
@@ -2,8 +2,8 @@ import React, { useMemo } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { NodeData } from '../types/NodeTypes';
import { useAppStore } from '../store/useAppStore';
-import { nodeDefinitions } from '../nodeDefinitions';
-import { useWebSocket, useNodeStatus } from '../contexts/WebSocketContext';
+import { nodeDefinitions, TOOL_NODES } from '../nodeDefinitions';
+import { useWebSocket, useNodeStatus, useIsToolExecuting } from '../contexts/WebSocketContext';
import { useAppTheme } from '../hooks/useAppTheme';
// Map credential names to provider keys
@@ -29,6 +29,22 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
const definition = nodeDefinitions[type as keyof typeof nodeDefinitions];
+ // Check if this is a tool node and if it's being executed by an AI Agent
+ const isToolNode = TOOL_NODES.includes(type || '');
+ // Get tool name from node data or definition defaults
+ const toolName = useMemo(() => {
+ if (!isToolNode) return '';
+ // Check node data first, then definition defaults
+ const nameFromData = data?.toolName;
+ if (nameFromData) return nameFromData;
+ // Get default from definition properties
+ const toolNameProp = definition?.properties?.find((p: any) => p.name === 'toolName');
+ return toolNameProp?.default || '';
+ }, [isToolNode, data?.toolName, definition?.properties]);
+ const isToolBeingExecuted = useIsToolExecuting(toolName);
+ // Tool is "executing" when AI Agent is using it
+ const isToolActive = isToolNode && isToolBeingExecuted;
+
// Determine provider from node definition or type
const provider = useMemo(() => {
const credentials = definition?.credentials?.[0];
@@ -119,7 +135,7 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
background: theme.isDarkMode
? `linear-gradient(135deg, ${nodeColor}20 0%, ${theme.colors.backgroundAlt} 100%)`
: `linear-gradient(145deg, #ffffff 0%, ${nodeColor}10 100%)`,
- border: `2px solid ${isExecuting
+ border: `2px solid ${(isExecuting || isToolActive)
? (theme.isDarkMode ? theme.dracula.cyan : '#2563eb')
: selected
? theme.colors.focus
@@ -133,7 +149,7 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
fontSize: type === 'aiAgent' ? theme.fontSize.lg : theme.iconSize.xl,
fontWeight: theme.fontWeight.semibold,
transition: theme.transitions.fast,
- boxShadow: isExecuting
+ boxShadow: (isExecuting || isToolActive)
? theme.isDarkMode
? `0 4px 12px ${theme.dracula.cyan}66, 0 0 0 3px ${theme.dracula.cyan}4D`
: `0 0 0 3px rgba(37, 99, 235, 0.5), 0 4px 16px rgba(37, 99, 235, 0.35)`
@@ -144,9 +160,30 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
? `0 2px 8px ${nodeColor}30`
: `0 2px 8px ${nodeColor}25, 0 4px 12px rgba(0,0,0,0.06)`)
: `0 2px 8px ${theme.dracula.red}4D`,
- animation: isExecuting ? 'pulse 1.5s ease-in-out infinite' : 'none',
+ animation: (isExecuting || isToolActive) ? 'pulse 1.5s ease-in-out infinite' : 'none',
}}
>
+ {/* Spinning Arrow Ring - shows when tool is being executed by AI Agent */}
+ {isToolActive && (
+ <div
+ style={{
+ position: 'absolute',
+ top: -8,
+ left: -8,
+ right: -8,
+ bottom: -8,
+ borderRadius: '50%',
+ border: '3px solid transparent',
+ borderTopColor: theme.dracula.cyan,
+ borderRightColor: theme.dracula.purple,
+ borderBottomColor: theme.dracula.cyan,
+ animation: 'spin 1s linear infinite',
+ zIndex: 50,
+ pointerEvents: 'none',
+ }}
+ />
+ )}
+
{/* AI Text */}
{getProviderDisplay()}
@@ -189,7 +226,7 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
width: theme.nodeSize.statusIndicator,
height: theme.nodeSize.statusIndicator,
borderRadius: '50%',
- backgroundColor: isExecuting
+ backgroundColor: (isExecuting || isToolActive)
? theme.dracula.cyan
: executionStatus === 'success'
? theme.dracula.green
@@ -201,7 +238,7 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
? theme.dracula.orange
: theme.dracula.red,
border: `2px solid ${theme.isDarkMode ? theme.colors.background : '#ffffff'}`,
- boxShadow: isExecuting
+ boxShadow: (isExecuting || isToolActive)
? theme.isDarkMode
? `0 0 6px ${theme.dracula.cyan}80`
: '0 0 4px rgba(37, 99, 235, 0.5)'
@@ -209,18 +246,22 @@ const ModelNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectabl
? `0 1px 3px ${theme.colors.shadow}`
: '0 1px 3px rgba(0,0,0,0.15)',
zIndex: 30,
- animation: isExecuting ? 'pulse 1s ease-in-out infinite' : 'none',
+ animation: (isExecuting || isToolActive) ? 'pulse 1s ease-in-out infinite' : 'none',
}}
title={
- isExecuting
- ? 'Executing...'
- : isMemoryNode
- ? 'Memory node ready'
- : isConfigured
- ? 'Model configured and ready'
- : hasApiKey
- ? 'API key found, model needs configuration'
- : 'API key required'
+ isToolActive
+ ? 'Tool executing...'
+ : isExecuting
+ ? 'Executing...'
+ : isMemoryNode
+ ? 'Memory node ready'
+ : isToolNode
+ ? 'Tool ready'
+ : isConfigured
+ ? 'Model configured and ready'
+ : hasApiKey
+ ? 'API key found, model needs configuration'
+ : 'API key required'
}
/>
diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsx
index 482bbc2b..1bd7e1ab 100644
--- a/client/src/contexts/WebSocketContext.tsx
+++ b/client/src/contexts/WebSocketContext.tsx
@@ -11,6 +11,7 @@
*/
import React, { createContext, useContext, useEffect, useState, useCallback, useRef, useMemo } from 'react';
+import { flushSync } from 'react-dom';
import { API_CONFIG } from '../config/api';
import { useAppStore } from '../store/useAppStore';
import { useAuth } from './AuthContext';
@@ -484,13 +485,26 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
// Store status under workflow_id -> node_id structure
if (node_id) {
const statusWorkflowId = message.workflow_id || 'unknown';
- setAllNodeStatuses((prev: Record<string, Record<string, NodeStatus>>) => ({
- ...prev,
- [statusWorkflowId]: {
- ...(prev[statusWorkflowId] || {}),
- [node_id]: { ...data, workflow_id: statusWorkflowId }
- }
- }));
+ const phase = data?.phase;
+
+ // Use flushSync for tool execution phases to force immediate re-render
+ // This ensures the spinning animation appears before tool_completed arrives
+ const isToolPhase = phase === 'executing_tool' || phase === 'tool_completed';
+ const updateFn = () => {
+ setAllNodeStatuses((prev: Record<string, Record<string, NodeStatus>>) => ({
+ ...prev,
+ [statusWorkflowId]: {
+ ...(prev[statusWorkflowId] || {}),
+ [node_id]: { ...data, workflow_id: statusWorkflowId }
+ }
+ }));
+ };
+
+ if (isToolPhase) {
+ flushSync(updateFn);
+ } else {
+ updateFn();
+ }
}
break;
@@ -1704,4 +1718,19 @@ export const useWhatsAppMessages = (): {
};
};
+// Hook to check if a tool is currently being executed by any AI Agent
+// Used by tool nodes to show spinning indicator when they're being used
+export const useIsToolExecuting = (toolName: string): boolean => {
+ const { nodeStatuses } = useWebSocket();
+
+ // Scan all node statuses to find if any AI Agent is executing this tool
+ for (const nodeId in nodeStatuses) {
+ const status = nodeStatuses[nodeId];
+ if (status?.data?.phase === 'executing_tool' && status?.data?.tool_name === toolName) {
+ return true;
+ }
+ }
+ return false;
+};
+
export default WebSocketContext;
diff --git a/client/src/nodeDefinitions.ts b/client/src/nodeDefinitions.ts
index 6743c8ec..2c6e39f4 100644
--- a/client/src/nodeDefinitions.ts
+++ b/client/src/nodeDefinitions.ts
@@ -4,6 +4,7 @@ import { INodeTypeDescription } from './types/INodeProperties';
// Import modular node definitions
import { aiModelNodes } from './nodeDefinitions/aiModelNodes';
import { aiAgentNodes, AI_NODE_TYPES } from './nodeDefinitions/aiAgentNodes';
+import { toolNodes, TOOL_NODE_TYPES } from './nodeDefinitions/toolNodes';
import { locationNodes, LOCATION_NODE_TYPES } from './nodeDefinitions/locationNodes';
import { whatsappNodes, WHATSAPP_NODE_TYPES } from './nodeDefinitions/whatsappNodes';
import { workflowNodes } from './nodeDefinitions/workflowNodes';
@@ -24,6 +25,7 @@ export const nodeDefinitions: Record<string, INodeTypeDescription> = {
...schedulerNodes,
...aiModelNodes,
...aiAgentNodes,
+ ...toolNodes,
...locationNodes,
...whatsappNodes,
...androidServiceNodes,
@@ -82,5 +84,9 @@ export const UTILITY_NODES = [
...UTILITY_NODE_TYPES
];
+export const TOOL_NODES = [
+ ...TOOL_NODE_TYPES
+];
+
// Re-export types and utilities from modular files for external access
export { createBaseChatModel } from './nodeDefinitions/aiModelNodes';
\ No newline at end of file
diff --git a/client/src/nodeDefinitions/toolNodes.ts b/client/src/nodeDefinitions/toolNodes.ts
new file mode 100644
index 00000000..3e5f6398
--- /dev/null
+++ b/client/src/nodeDefinitions/toolNodes.ts
@@ -0,0 +1,169 @@
+// Tool Node Definitions - Tool nodes for AI Agent tool calling
+import {
+ INodeTypeDescription,
+ NodeConnectionType
+} from '../types/INodeProperties';
+
+// ============================================================================
+// TOOL NODES - Connect to AI Agent's input-tools handle
+// ============================================================================
+
+export const toolNodes: Record<string, INodeTypeDescription> = {
+ // Calculator Tool - allows AI Agent to perform mathematical calculations
+ calculatorTool: {
+ displayName: 'Calculator Tool',
+ name: 'calculatorTool',
+ icon: 'πŸ”’',
+ group: ['ai', 'tool'],
+ version: 1,
+ subtitle: 'Math Operations',
+ description: 'Allow AI Agent to perform mathematical calculations',
+ defaults: { name: 'Calculator', color: '#50fa7b' },
+ inputs: [], // No input - tool node is passive
+ outputs: [{
+ name: 'tool',
+ displayName: 'Tool',
+ type: 'main' as NodeConnectionType,
+ description: 'Connect to AI Agent tool handle'
+ }],
+ properties: [
+ {
+ displayName: 'Tool Name',
+ name: 'toolName',
+ type: 'string',
+ default: 'calculator',
+ required: true,
+ description: 'Name the AI will use to call this tool'
+ },
+ {
+ displayName: 'Description',
+ name: 'toolDescription',
+ type: 'string',
+ default: 'Perform mathematical calculations. Operations: add, subtract, multiply, divide, power, sqrt, mod, abs',
+ typeOptions: { rows: 2 },
+ description: 'Describe the tool capabilities for the AI'
+ }
+ ]
+ },
+
+ // Current Time Tool - allows AI Agent to get current date and time
+ currentTimeTool: {
+ displayName: 'Current Time Tool',
+ name: 'currentTimeTool',
+ icon: 'πŸ•',
+ group: ['ai', 'tool'],
+ version: 1,
+ subtitle: 'Date & Time',
+ description: 'Allow AI Agent to get current date and time',
+ defaults: { name: 'Current Time', color: '#ffb86c' },
+ inputs: [],
+ outputs: [{
+ name: 'tool',
+ displayName: 'Tool',
+ type: 'main' as NodeConnectionType,
+ description: 'Connect to AI Agent tool handle'
+ }],
+ properties: [
+ {
+ displayName: 'Tool Name',
+ name: 'toolName',
+ type: 'string',
+ default: 'get_current_time',
+ required: true,
+ description: 'Name the AI will use to call this tool'
+ },
+ {
+ displayName: 'Description',
+ name: 'toolDescription',
+ type: 'string',
+ default: 'Get the current date and time. Optionally specify timezone.',
+ typeOptions: { rows: 2 },
+ description: 'Describe the tool capabilities for the AI'
+ },
+ {
+ displayName: 'Default Timezone',
+ name: 'timezone',
+ type: 'string',
+ default: 'UTC',
+ placeholder: 'America/New_York',
+ description: 'Default timezone (e.g., UTC, America/New_York, Europe/London)'
+ }
+ ]
+ },
+
+ // Web Search Tool - allows AI Agent to search the web
+ webSearchTool: {
+ displayName: 'Web Search Tool',
+ name: 'webSearchTool',
+ icon: 'πŸ”',
+ group: ['ai', 'tool'],
+ version: 1,
+ subtitle: 'Web Search',
+ description: 'Allow AI Agent to search the web for information',
+ defaults: { name: 'Web Search', color: '#bd93f9' },
+ inputs: [],
+ outputs: [{
+ name: 'tool',
+ displayName: 'Tool',
+ type: 'main' as NodeConnectionType,
+ description: 'Connect to AI Agent tool handle'
+ }],
+ properties: [
+ {
+ displayName: 'Tool Name',
+ name: 'toolName',
+ type: 'string',
+ default: 'web_search',
+ required: true,
+ description: 'Name the AI will use to call this tool'
+ },
+ {
+ displayName: 'Description',
+ name: 'toolDescription',
+ type: 'string',
+ default: 'Search the web for information. Returns relevant search results.',
+ typeOptions: { rows: 2 },
+ description: 'Describe the tool capabilities for the AI'
+ },
+ {
+ displayName: 'Search Provider',
+ name: 'provider',
+ type: 'options',
+ options: [
+ { name: 'DuckDuckGo (Free)', value: 'duckduckgo' },
+ { name: 'Serper API', value: 'serper' },
+ { name: 'Google Custom Search', value: 'google' }
+ ],
+ default: 'duckduckgo',
+ description: 'Search provider to use'
+ },
+ {
+ displayName: 'API Key',
+ name: 'apiKey',
+ type: 'string',
+ default: '',
+ description: 'API key for Serper or Google (not needed for DuckDuckGo)',
+ displayOptions: {
+ show: {
+ provider: ['serper', 'google']
+ }
+ }
+ },
+ {
+ displayName: 'Max Results',
+ name: 'maxResults',
+ type: 'number',
+ default: 5,
+ typeOptions: { minValue: 1, maxValue: 10 },
+ description: 'Maximum number of results to return'
+ }
+ ]
+ }
+};
+
+// ============================================================================
+// EXPORTS
+// ============================================================================
+
+// List of tool node types for identification
+export const TOOL_NODE_TYPES = ['calculatorTool', 'currentTimeTool', 'webSearchTool'];
diff --git a/client/src/vite-env.d.ts b/client/src/vite-env.d.ts
new file mode 100644
index 00000000..72ed8d4b
--- /dev/null
+++ b/client/src/vite-env.d.ts
@@ -0,0 +1,12 @@
+/// <reference types="vite/client" />
+
+interface ImportMetaEnv {
+ readonly VITE_ANDROID_RELAY_URL: string;
+ readonly VITE_AUTH_ENABLED: string;
+ readonly VITE_CLIENT_PORT: string;
+ readonly VITE_PYTHON_BACKEND_URL: string;
+}
+
+interface ImportMeta {
+ readonly env: ImportMetaEnv;
+}
diff --git a/server/constants.py b/server/constants.py
index cfe2451b..60c66e1b 100644
--- a/server/constants.py
+++ b/server/constants.py
@@ -24,9 +24,28 @@ AI_MEMORY_TYPES: FrozenSet[str] = frozenset([
'simpleMemory',
])
+# Tool node types (connect to AI Agent's input-tools handle)
+AI_TOOL_TYPES: FrozenSet[str] = frozenset([
+ 'calculatorTool',
+ 'currentTimeTool',
+ 'webSearchTool',
+])
+
# All AI-related node types (for API key injection)
AI_MODEL_TYPES: FrozenSet[str] = AI_AGENT_TYPES | AI_CHAT_MODEL_TYPES
+# =============================================================================
+# CONFIG NODE TYPES (excluded from workflow execution)
+# =============================================================================
+
+# Config nodes provide configuration to other nodes via special handles
+# (input-memory, input-tools, input-model). They don't execute independently.
+CONFIG_NODE_TYPES: FrozenSet[str] = (
+ AI_MEMORY_TYPES | # Memory nodes (connect to input-memory)
+ AI_TOOL_TYPES | # Tool nodes (connect to input-tools)
+ AI_CHAT_MODEL_TYPES # Model config nodes (connect to input-model)
+)
+
# =============================================================================
# GOOGLE MAPS NODE TYPES
# =============================================================================
diff --git a/server/requirements.txt b/server/requirements.txt
index a5468f2e..72db08c3 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -49,6 +49,9 @@ googlemaps>=4.10.0
# Scheduling
APScheduler>=3.10.0
+# Web Search
+duckduckgo-search>=8.0.0
+
# Authentication
bcrypt>=4.1.0
python-jose[cryptography]>=3.3.0
diff --git a/server/services/ai.py b/server/services/ai.py
index 568a11f0..cdc9baf3 100644
--- a/server/services/ai.py
+++ b/server/services/ai.py
@@ -10,8 +10,11 @@ import operator
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
-from langchain_core.messages import HumanMessage, SystemMessage, AIMessage, BaseMessage
+from langchain_core.messages import HumanMessage, SystemMessage, AIMessage, BaseMessage, ToolMessage
+from langchain_core.tools import StructuredTool
from langgraph.graph import StateGraph, END
+from pydantic import BaseModel, Field
+import json
from core.config import Settings
from core.logging import get_logger, log_execution_time, log_api_call
@@ -119,8 +122,10 @@ class AgentState(TypedDict):
This is the core pattern from LangGraph for stateful conversations.
"""
messages: Annotated[Sequence[BaseMessage], operator.add]
- # Tool outputs for future tool integration
+ # Tool outputs storage
tool_outputs: Dict[str, Any]
+ # Tool calling support
+ pending_tool_calls: List[Dict[str, Any]] # Tool calls from LLM to execute
# Agent metadata
iteration: int
max_iterations: int
@@ -133,7 +138,8 @@ def create_agent_node(chat_model):
The agent node:
1. Receives current state with messages
2. Invokes the LLM
- 3. Returns updated state with new AI message
+ 3. Checks for tool calls in response
+ 4. Returns updated state with new AI message and pending tool calls
"""
def agent_node(state: AgentState) -> Dict[str, Any]:
"""Process messages through the LLM and return response."""
@@ -141,16 +147,31 @@ def create_agent_node(chat_model):
iteration = state.get("iteration", 0)
max_iterations = state.get("max_iterations", 10)
+ logger.info(f"[LangGraph] Agent node invoked, iteration={iteration}, messages={len(messages)}")
+
# Invoke the model
response = chat_model.invoke(list(messages))
- # Check if we should continue (for future tool use)
- # Currently we just do single turn, but structure supports multi-turn
+ logger.info(f"[LangGraph] LLM response type: {type(response)}, has tool_calls attr: {hasattr(response, 'tool_calls')}")
+ if hasattr(response, 'tool_calls'):
+ logger.info(f"[LangGraph] tool_calls value: {response.tool_calls}")
+
+ # Check for tool calls in the response
+ pending_tool_calls = []
should_continue = False
+ if hasattr(response, 'tool_calls') and response.tool_calls:
+ # Model wants to use tools
+ pending_tool_calls = response.tool_calls
+ should_continue = True
+ logger.info(f"[LangGraph] Agent requesting {len(pending_tool_calls)} tool call(s): {[tc.get('name', tc) for tc in pending_tool_calls]}")
+ else:
+ logger.info(f"[LangGraph] No tool calls in response, content preview: {str(response.content)[:100]}")
+
return {
"messages": [response], # Will be appended via operator.add
"tool_outputs": {},
+ "pending_tool_calls": pending_tool_calls,
"iteration": iteration + 1,
"max_iterations": max_iterations,
"should_continue": should_continue
@@ -159,49 +180,125 @@ def create_agent_node(chat_model):
return agent_node
+def create_tool_node(tool_executor: Callable):
+ """Create an async tool execution node for LangGraph.
+
+ The tool node:
+ 1. Receives pending tool calls from agent
+ 2. Executes each tool via the async tool_executor callback
+ 3. Returns ToolMessages with results for the agent
+
+ Note: This returns an async function for use with ainvoke().
+ LangGraph supports async node functions natively.
+ """
+ async def tool_node(state: AgentState) -> Dict[str, Any]:
+ """Execute pending tool calls and return results as ToolMessages."""
+ tool_messages = []
+
+ for tool_call in state.get("pending_tool_calls", []):
+ tool_name = tool_call.get("name", "unknown")
+ tool_args = tool_call.get("args", {})
+ tool_id = tool_call.get("id", "")
+
+ logger.info(f"[LangGraph] Executing tool: {tool_name} with args: {tool_args}")
+
+ try:
+ # Directly await the async tool executor (proper async pattern)
+ result = await tool_executor(tool_name, tool_args)
+ except Exception as e:
+ logger.error(f"[LangGraph] Tool execution failed: {tool_name}", error=str(e))
+ result = {"error": str(e)}
+
+ # Create ToolMessage with result
+ tool_messages.append(ToolMessage(
+ content=json.dumps(result, default=str),
+ tool_call_id=tool_id,
+ name=tool_name
+ ))
+
+ logger.info(f"[LangGraph] Tool {tool_name} completed with result: {result}")
+
+ return {
+ "messages": tool_messages,
+ "pending_tool_calls": [], # Clear pending after execution
+ }
+
+ return tool_node
+
+
def should_continue(state: AgentState) -> str:
"""Determine if the agent should continue or end.
This is the conditional edge function for LangGraph.
- Returns "continue" to loop back to tools, or "end" to finish.
+ Returns "tools" to execute pending tool calls, or "end" to finish.
"""
if state.get("should_continue", False):
if state.get("iteration", 0) < state.get("max_iterations", 10):
- return "continue"
+ return "tools"
return "end"
-def build_agent_graph(chat_model):
- """Build the LangGraph agent workflow.
+def build_agent_graph(chat_model, tools: List = None, tool_executor: Callable = None):
+ """Build the LangGraph agent workflow with optional tool support.
+
+ Architecture (with tools):
+ START -> agent -> (conditional) -> tools -> agent -> ... -> END
+ |
+ +-> END (no tool calls)
- Architecture:
- START -> agent -> (conditional) -> END
- ^ |
- | v
- +--- tools --+ (future: tool execution loop)
+ Architecture (without tools):
+ START -> agent -> END
- For now, this is a simple single-node graph that can be extended
- with tool nodes when tool integration is added.
+ Args:
+ chat_model: The LangChain chat model
+ tools: Optional list of LangChain tools to bind to the model
+ tool_executor: Optional async callback to execute tools
"""
# Create the graph with our state schema
graph = StateGraph(AgentState)
+ # Bind tools to model if provided
+ model_with_tools = chat_model
+ if tools:
+ model_with_tools = chat_model.bind_tools(tools)
+ logger.info(f"[LangGraph] Bound {len(tools)} tools to model")
+
# Add the agent node
- agent_fn = create_agent_node(chat_model)
+ agent_fn = create_agent_node(model_with_tools)
graph.add_node("agent", agent_fn)
# Set entry point
graph.set_entry_point("agent")
- # Add conditional edge - currently always ends, but structured for tools
- graph.add_conditional_edges(
- "agent",
- should_continue,
- {
- "continue": "agent", # Loop back for multi-turn (future tools)
- "end": END
- }
- )
+ if tools and tool_executor:
+ # Add tool execution node
+ tool_fn = create_tool_node(tool_executor)
+ graph.add_node("tools", tool_fn)
+
+ # Conditional routing: agent -> tools or end
+ graph.add_conditional_edges(
+ "agent",
+ should_continue,
+ {
+ "tools": "tools",
+ "end": END
+ }
+ )
+
+ # Tools always route back to agent
+ graph.add_edge("tools", "agent")
+
+ logger.info("[LangGraph] Built graph with tool execution loop")
+ else:
+ # Simple graph without tools
+ graph.add_conditional_edges(
+ "agent",
+ should_continue,
+ {
+ "tools": "agent", # Fallback loop (shouldn't happen without tools)
+ "end": END
+ }
+ )
# Compile the graph
return graph.compile()
@@ -372,12 +469,14 @@ class AIService:
async def execute_agent(self, node_id: str, parameters: Dict[str, Any],
memory_data: Optional[Dict[str, Any]] = None,
- broadcaster = None) -> Dict[str, Any]:
+ tool_data: Optional[List[Dict[str, Any]]] = None,
+ broadcaster = None,
+ workflow_id: Optional[str] = None) -> Dict[str, Any]:
"""Execute AI Agent using LangGraph state machine.
This method uses LangGraph for structured agent execution with:
- State management via TypedDict
- - Conditional edges for future tool integration
+ - Tool calling via bind_tools and tool execution node
- Message accumulation via operator.add pattern
- Real-time status broadcasts for UI animations
@@ -386,20 +485,22 @@ class AIService:
parameters: Node parameters including prompt, model, etc.
memory_data: Optional memory data from connected simpleMemory node
containing session_id, window_size for conversation history
+ tool_data: Optional list of tool configurations from connected tool nodes
broadcaster: Optional StatusBroadcaster for real-time UI updates
+ workflow_id: Optional workflow ID for scoped status broadcasts
"""
start_time = time.time()
provider = 'unknown'
model = 'unknown'
- # Helper to broadcast status updates
+ # Helper to broadcast status updates with workflow_id for proper scoping
async def broadcast_status(phase: str, details: Dict[str, Any] = None):
if broadcaster:
await broadcaster.update_node_status(node_id, "executing", {
"phase": phase,
"agent_type": "langgraph",
**(details or {})
- })
+ }, workflow_id=workflow_id)
try:
# Extract top-level parameters (always visible in UI)
@@ -417,7 +518,7 @@ class AIService:
temperature = float(flattened.get('temperature', 0.7))
max_tokens = int(flattened.get('max_tokens') or flattened.get('maxTokens') or 1000)
- logger.info(f"[LangGraph] AI Agent execution - Provider: {provider}, Model: {model}, Memory: {bool(memory_data)}")
+ logger.info(f"[LangGraph] AI Agent execution - Provider: {provider}, Model: {model}, Memory: {bool(memory_data)}, Tools: {len(tool_data) if tool_data else 0}")
# If no model specified or model doesn't match provider, use default from registry
if not model or not is_model_valid_for_provider(model, provider):
@@ -484,22 +585,70 @@ class AIService:
# Add current user prompt
initial_messages.append(HumanMessage(content=prompt))
+ # Build tools if provided
+ tools = []
+ tool_configs = {}
+
+ if tool_data:
+ await broadcast_status("building_tools", {
+ "message": f"Building {len(tool_data)} tool(s)...",
+ "tool_count": len(tool_data)
+ })
+
+ for tool_info in tool_data:
+ tool, config = self._build_tool_from_node(tool_info)
+ if tool:
+ tools.append(tool)
+ tool_configs[tool.name] = config
+
+ logger.info(f"[LangGraph] Built {len(tools)} tools: {[t.name for t in tools]}")
+
+ # Create tool executor callback
+ async def tool_executor(tool_name: str, tool_args: Dict) -> Any:
+ """Execute a tool by name."""
+ from services.handlers.tools import execute_tool
+
+ logger.info(f"[LangGraph] tool_executor called for: {tool_name}, broadcasting executing_tool status")
+ await broadcast_status("executing_tool", {
+ "message": f"Executing tool: {tool_name}",
+ "tool_name": tool_name,
+ "tool_args": tool_args
+ })
+ logger.info(f"[LangGraph] broadcast_status completed for executing_tool: {tool_name}")
+
+ config = tool_configs.get(tool_name, {})
+ result = await execute_tool(tool_name, tool_args, config)
+
+ await broadcast_status("tool_completed", {
+ "message": f"Tool completed: {tool_name}",
+ "tool_name": tool_name,
+ "result_preview": str(result)[:100]
+ })
+
+ return result
+
# Broadcast: Building graph
await broadcast_status("building_graph", {
"message": "Building LangGraph agent...",
"message_count": len(initial_messages),
"has_memory": bool(session_id),
- "history_count": history_count
+ "history_count": history_count,
+ "tool_count": len(tools)
})
# Build and execute LangGraph agent
- logger.info(f"[LangGraph] Building agent graph with {len(initial_messages)} initial messages")
- agent_graph = build_agent_graph(chat_model)
+ logger.info(f"[LangGraph] Building agent graph with {len(initial_messages)} initial messages and {len(tools)} tools")
+ agent_graph = build_agent_graph(
+ chat_model,
+ tools=tools if tools else None,
+ tool_executor=tool_executor if tools else None
+ )
# Create initial state
initial_state: AgentState = {
"messages": initial_messages,
"tool_outputs": {},
+ "pending_tool_calls": [],
"iteration": 0,
"max_iterations": 10,
"should_continue": False
@@ -515,8 +664,9 @@ class AIService:
"history_count": history_count
})
- # Execute the graph
- final_state = agent_graph.invoke(initial_state)
+ # Execute the graph using ainvoke for proper async support
+ # This allows async tool nodes and WebSocket broadcasts to work correctly
+ final_state = await agent_graph.ainvoke(initial_state)
# Extract the AI response (last message in the accumulated messages)
all_messages = final_state["messages"]
@@ -587,4 +737,150 @@ class AIService:
"error": str(e),
"execution_time": time.time() - start_time,
"timestamp": datetime.now().isoformat()
- }
\ No newline at end of file
+ }
+
+ def _build_tool_from_node(self, tool_info: Dict[str, Any]) -> tuple:
+ """Convert a node configuration into a LangChain StructuredTool.
+
+ Args:
+ tool_info: Dict containing node_id, node_type, parameters, label
+
+ Returns:
+ Tuple of (StructuredTool, config_dict) or (None, None) on failure
+ """
+ # Default tool names matching frontend toolNodes.ts definitions
+ DEFAULT_TOOL_NAMES = {
+ 'calculatorTool': 'calculator',
+ 'currentTimeTool': 'get_current_time',
+ 'webSearchTool': 'web_search',
+ }
+ DEFAULT_TOOL_DESCRIPTIONS = {
+ 'calculatorTool': 'Perform mathematical calculations. Operations: add, subtract, multiply, divide, power, sqrt, mod, abs',
+ 'currentTimeTool': 'Get the current date and time. Optionally specify timezone.',
+ 'webSearchTool': 'Search the web for information. Returns relevant search results.',
+ }
+
+ try:
+ node_type = tool_info.get('node_type', '')
+ node_params = tool_info.get('parameters', {})
+ node_label = tool_info.get('label', node_type)
+ node_id = tool_info.get('node_id', '')
+
+ # Get tool name from params, then type-specific default, then generic fallback
+ tool_name = (
+ node_params.get('toolName') or
+ DEFAULT_TOOL_NAMES.get(node_type) or
+ f"tool_{node_label}".replace(' ', '_').replace('-', '_').lower()
+ )
+ tool_description = (
+ node_params.get('toolDescription') or
+ DEFAULT_TOOL_DESCRIPTIONS.get(node_type) or
+ f"Execute {node_label} node"
+ )
+
+ # Clean tool name (LangChain requires alphanumeric + underscores)
+ import re
+ tool_name = re.sub(r'[^a-zA-Z0-9_]', '_', tool_name)
+
+ # Build schema based on node type
+ schema = self._get_tool_schema(node_type, node_params)
+
+ # Create StructuredTool - the func is a placeholder, actual execution via tool_executor
+ def placeholder_func(**kwargs):
+ return kwargs
+
+ tool = StructuredTool.from_function(
+ name=tool_name,
+ description=tool_description,
+ func=placeholder_func,
+ args_schema=schema
+ )
+
+ config = {
+ 'node_type': node_type,
+ 'node_id': node_id,
+ 'parameters': node_params,
+ 'label': node_label
+ }
+
+ logger.info(f"[LangGraph] Built tool '{tool_name}' from node type '{node_type}'")
+ return tool, config
+
+ except Exception as e:
+ logger.error(f"[LangGraph] Failed to build tool from node: {e}")
+ return None, None
+
+ def _get_tool_schema(self, node_type: str, params: Dict[str, Any]) -> Type[BaseModel]:
+ """Get Pydantic schema for tool based on node type.
+
+ Args:
+ node_type: The node type (e.g., 'calculatorTool', 'httpRequest')
+ params: Node parameters
+
+ Returns:
+ Pydantic BaseModel class for the tool's arguments
+ """
+ # Calculator tool schema
+ if node_type == 'calculatorTool':
+ class CalculatorSchema(BaseModel):
+ """Schema for calculator tool arguments."""
+ operation: str = Field(
+ description="Math operation: add, subtract, multiply, divide, power, sqrt, mod, abs"
+ )
+ a: float = Field(description="First number")
+ b: float = Field(default=0, description="Second number (not needed for sqrt, abs)")
+
+ return CalculatorSchema
+
+ # HTTP Request tool schema
+ if node_type in ('httpRequest', 'httpRequestTool'):
+ class HttpRequestSchema(BaseModel):
+ """Schema for HTTP request tool arguments."""
+ url: str = Field(description="URL path or full URL to request")
+ method: str = Field(default="GET", description="HTTP method: GET, POST, PUT, DELETE")
+ body: Optional[Dict[str, Any]] = Field(default=None, description="Request body as JSON object")
+
+ return HttpRequestSchema
+
+ # Python executor tool schema
+ if node_type == 'pythonExecutor':
+ class PythonCodeSchema(BaseModel):
+ """Schema for Python code execution."""
+ code: str = Field(description="Python code to execute")
+
+ return PythonCodeSchema
+
+ # Current time tool schema
+ if node_type == 'currentTimeTool':
+ class CurrentTimeSchema(BaseModel):
+ """Schema for current time tool arguments."""
+ timezone: str = Field(
+ default="UTC",
+ description="Timezone (e.g., UTC, America/New_York, Europe/London)"
+ )
+
+ return CurrentTimeSchema
+
+ # Web search tool schema
+ if node_type == 'webSearchTool':
+ class WebSearchSchema(BaseModel):
+ """Schema for web search tool arguments."""
+ query: str = Field(description="Search query to look up on the web")
+
+ return WebSearchSchema
+
+ # WhatsApp send schema (existing node used as tool)
+ if node_type == 'whatsappSend':
+ class WhatsAppSendSchema(BaseModel):
+ """Schema for WhatsApp send tool arguments."""
+ phone_number: str = Field(description="Phone number to send message to (e.g., +1234567890)")
+ message: str = Field(description="Message text to send")
+
+ return WhatsAppSendSchema
+
+ # Generic schema for other nodes
+ class GenericToolSchema(BaseModel):
+ """Generic schema for tool arguments."""
+ input: str = Field(description="Input data for the tool")
+
+ return GenericToolSchema
\ No newline at end of file
diff --git a/server/services/auth.py b/server/services/auth.py
index 7c40c1d8..3842bd2e 100644
--- a/server/services/auth.py
+++ b/server/services/auth.py
@@ -2,7 +2,7 @@
import hashlib
import base64
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
from typing import Dict, Any, Optional, List
from core.config import Settings
@@ -86,7 +86,7 @@ class AuthService:
api_key_record = await self.database.get_api_key_by_provider(provider, session_id)
if api_key_record and api_key_record.is_valid:
# Check if not expired (30 days)
- if datetime.utcnow() - api_key_record.last_validated < timedelta(days=30):
+ if datetime.now(timezone.utc) - api_key_record.last_validated < timedelta(days=30):
return self.decrypt_api_key(api_key_record.key_encrypted)
return None
diff --git a/server/services/execution/executor.py b/server/services/execution/executor.py
index f2d479da..10563ffc 100644
--- a/server/services/execution/executor.py
+++ b/server/services/execution/executor.py
@@ -988,10 +988,18 @@ class WorkflowExecutor:
Returns:
List of NodeExecution ready to run
"""
+ from constants import CONFIG_NODE_TYPES
+
# Build set of completed nodes
completed = set(ctx.get_completed_nodes())
+ # Build map of node_id -> node_type for config node detection
+ node_types: Dict[str, str] = {}
+ for node in ctx.nodes:
+ node_types[node.get("id", "")] = node.get("type", "unknown")
+
# Build dependency map and track conditional edges
+ # Skip edges from config nodes (they don't execute, provide config only)
dependencies: Dict[str, Set[str]] = defaultdict(set)
conditional_edges: Dict[str, List[Dict]] = defaultdict(list) # target -> edges with conditions
@@ -999,6 +1007,11 @@ class WorkflowExecutor:
target = edge.get("target")
source = edge.get("source")
if target and source:
+ # Skip edges from config nodes - they provide configuration, not execution dependencies
+ source_type = node_types.get(source, "unknown")
+ if source_type in CONFIG_NODE_TYPES:
+ continue
+
dependencies[target].add(source)
# Track edges with conditions for evaluation
if edge.get("data", {}).get("condition"):
diff --git a/server/services/execution/models.py b/server/services/execution/models.py
index cb6a77c1..381b5619 100644
--- a/server/services/execution/models.py
+++ b/server/services/execution/models.py
@@ -302,7 +302,12 @@ class ExecutionContext:
Supports pre-executed nodes (marked with _pre_executed=True) for
event-driven execution where trigger nodes are already complete.
+
+ Config nodes (memory, tools, model configs) are excluded from execution
+ as they provide configuration to other nodes via special handles.
"""
+ from constants import CONFIG_NODE_TYPES
+
execution_id = str(uuid.uuid4())
ctx = cls(
execution_id=execution_id,
@@ -312,11 +317,16 @@ class ExecutionContext:
edges=edges or [],
)
- # Initialize node executions for all nodes
+ # Initialize node executions for all nodes (excluding config nodes)
for node in (nodes or []):
node_id = node.get("id")
node_type = node.get("type", "unknown")
+ # Skip config nodes - they don't execute independently
+ # They provide configuration to other nodes via special handles
+ if node_type in CONFIG_NODE_TYPES:
+ continue
+
# Check if node is pre-executed (e.g., trigger that already fired)
if node.get("_pre_executed"):
# Mark as COMPLETED with trigger output
diff --git a/server/services/handlers/__init__.py b/server/services/handlers/__init__.py
index 60858f0f..2dca0f65 100644
--- a/server/services/handlers/__init__.py
+++ b/server/services/handlers/__init__.py
@@ -5,6 +5,7 @@ This package contains all node execution handlers organized by category:
- android.py: Android Device Setup, Android Services
- code.py: Python Executor, JavaScript Executor
- http.py: HTTP Request, Webhook Response
+- tools.py: Tool execution handlers for AI Agent tool calling
- triggers.py: Generic trigger node handler
- utility.py: Maps, Text, Chat, Cron, Start
- whatsapp.py: WhatsApp Send, WhatsApp Connect
@@ -60,6 +61,11 @@ from .whatsapp import (
handle_whatsapp_connect,
)
+# Tool execution handlers (for AI Agent tool calling)
+from .tools import (
+ execute_tool,
+)
+
__all__ = [
# AI
'handle_ai_agent',
@@ -90,4 +96,6 @@ __all__ = [
# WhatsApp
'handle_whatsapp_send',
'handle_whatsapp_connect',
+ # Tools
+ 'execute_tool',
]
diff --git a/server/services/handlers/ai.py b/server/services/handlers/ai.py
index 1a9fb1ad..847a2c96 100644
--- a/server/services/handlers/ai.py
+++ b/server/services/handlers/ai.py
@@ -1,6 +1,6 @@
"""AI node handlers - AI Agent, AI Chat Model, Simple Memory."""
-from typing import Dict, Any, TYPE_CHECKING
+from typing import Dict, Any, List, TYPE_CHECKING
from core.logging import get_logger
if TYPE_CHECKING:
@@ -18,7 +18,7 @@ async def handle_ai_agent(
ai_service: "AIService",
database: "Database"
) -> Dict[str, Any]:
- """Handle AI agent node execution with memory support.
+ """Handle AI agent node execution with memory and tool support.
Args:
node_id: The node ID
@@ -34,15 +34,26 @@ async def handle_ai_agent(
nodes = context.get('nodes')
edges = context.get('edges')
execution_id = context.get('execution_id', 'unknown')
+ workflow_id = context.get('workflow_id') # Extract for status broadcasts
memory_data = None
+ tool_data: List[Dict[str, Any]] = [] # Collect connected tool nodes
if edges and nodes:
for edge in edges:
- if edge.get('target') == node_id and edge.get('targetHandle') == 'input-memory':
- memory_node_id = edge.get('source')
- memory_node = next((n for n in nodes if n.get('id') == memory_node_id), None)
- if memory_node and memory_node.get('type') == 'simpleMemory':
- memory_params = await database.get_node_parameters(memory_node_id) or {}
+ if edge.get('target') != node_id:
+ continue
+
+ target_handle = edge.get('targetHandle')
+ source_node_id = edge.get('source')
+ source_node = next((n for n in nodes if n.get('id') == source_node_id), None)
+
+ if not source_node:
+ continue
+
+ # Memory detection (existing)
+ if target_handle == 'input-memory':
+ if source_node.get('type') == 'simpleMemory':
+ memory_params = await database.get_node_parameters(source_node_id) or {}
memory_session_id = memory_params.get('sessionId', 'default')
memory_type = memory_params.get('memoryType', 'buffer')
window_size = int(memory_params.get('windowSize', 10)) if memory_type == 'window' else None
@@ -53,13 +64,32 @@ async def handle_ai_agent(
}
logger.info("AI Agent connected memory node", node_id=node_id, execution_id=execution_id,
memory_session=memory_session_id, memory_type=memory_type)
- break
+
+ # Tool detection (new) - any node connected to input-tools becomes a tool
+ elif target_handle == 'input-tools':
+ tool_type = source_node.get('type')
+ tool_params = await database.get_node_parameters(source_node_id) or {}
+ tool_data.append({
+ 'node_id': source_node_id,
+ 'node_type': tool_type,
+ 'parameters': tool_params,
+ 'label': source_node.get('data', {}).get('label', tool_type)
+ })
+ logger.info("AI Agent connected tool node", node_id=node_id, execution_id=execution_id,
+ tool_type=tool_type, tool_node_id=source_node_id)
# Get broadcaster for real-time status updates
from services.status_broadcaster import get_status_broadcaster
broadcaster = get_status_broadcaster()
- return await ai_service.execute_agent(node_id, parameters, memory_data, broadcaster)
+ return await ai_service.execute_agent(
+ node_id,
+ parameters,
+ memory_data=memory_data,
+ tool_data=tool_data if tool_data else None,
+ broadcaster=broadcaster,
+ workflow_id=workflow_id
+ )
async def handle_ai_chat_model(
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
new file mode 100644
index 00000000..936eb723
--- /dev/null
+++ b/server/services/handlers/tools.py
@@ -0,0 +1,475 @@
+"""Tool execution handlers for AI Agent tool calling.
+
+This module contains handlers for executing tools called by the AI Agent.
+Each tool type has its own handler function that processes the tool call
+and returns results.
+"""
+
+import math
+import json
+from typing import Dict, Any, Optional
+from core.logging import get_logger
+
+logger = get_logger(__name__)
+
+
+async def execute_tool(tool_name: str, tool_args: Dict[str, Any],
+ config: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute a tool by name using the appropriate handler.
+
+ This is the main dispatch function that routes tool calls to specific handlers
+ based on the node_type in the config.
+
+ Args:
+ tool_name: Name of the tool (for logging)
+ tool_args: Arguments provided by the AI model
+ config: Tool configuration containing node_type, node_id, parameters
+
+ Returns:
+ Tool execution result dict
+ """
+ node_type = config.get('node_type', '')
+
+ logger.info(f"[Tool] Executing tool '{tool_name}' (node_type: {node_type})")
+
+ # Calculator tool
+ if node_type == 'calculatorTool':
+ return await _execute_calculator(tool_args)
+
+ # HTTP Request tool (existing httpRequest node as tool)
+ if node_type in ('httpRequest', 'httpRequestTool'):
+ return await _execute_http_request(tool_args, config.get('parameters', {}))
+
+ # Python executor tool
+ if node_type == 'pythonExecutor':
+ return await _execute_python_code(tool_args, config.get('parameters', {}))
+
+ # Current time tool
+ if node_type == 'currentTimeTool':
+ return await _execute_current_time(tool_args, config.get('parameters', {}))
+
+ # Web search tool
+ if node_type == 'webSearchTool':
+ return await _execute_web_search(tool_args, config.get('parameters', {}))
+
+ # WhatsApp send (existing node used as tool)
+ if node_type == 'whatsappSend':
+ return await _execute_whatsapp_send(tool_args, config.get('parameters', {}))
+
+ # Generic fallback for unknown node types
+ logger.warning(f"[Tool] Unknown tool type: {node_type}, using generic handler")
+ return await _execute_generic(tool_args, config)
+
+
+async def _execute_calculator(args: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute calculator operations.
+
+ Supported operations: add, subtract, multiply, divide, power, sqrt, mod, abs
+
+ Args:
+ args: Dict with 'operation', 'a', and optionally 'b'
+
+ Returns:
+ Dict with operation, inputs, and result
+ """
+ operation = args.get('operation', '').lower()
+ a = float(args.get('a', 0))
+ b = float(args.get('b', 0))
+
+ operations = {
+ 'add': lambda: a + b,
+ 'subtract': lambda: a - b,
+ 'multiply': lambda: a * b,
+ 'divide': lambda: a / b if b != 0 else float('inf'),
+ 'power': lambda: math.pow(a, b),
+ 'sqrt': lambda: math.sqrt(abs(a)), # Use abs to handle negative
+ 'mod': lambda: a % b if b != 0 else 0,
+ 'abs': lambda: abs(a),
+ }
+
+ if operation not in operations:
+ return {
+ "error": f"Unknown operation: {operation}",
+ "supported_operations": list(operations.keys())
+ }
+
+ try:
+ result = operations[operation]()
+ logger.info(f"[Calculator] {operation}({a}, {b}) = {result}")
+ return {
+ "operation": operation,
+ "a": a,
+ "b": b,
+ "result": result
+ }
+ except Exception as e:
+ logger.error(f"[Calculator] Error: {e}")
+ return {"error": str(e)}
+
+
+async def _execute_http_request(args: Dict[str, Any],
+ node_params: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute HTTP request tool.
+
+ Args:
+ args: Dict with 'url', 'method', optionally 'body'
+ node_params: Node parameters containing base_url, headers, etc.
+
+ Returns:
+ Dict with status code, data, and url
+ """
+ import httpx
+
+ base_url = node_params.get('url', '')
+ url = args.get('url', '')
+ method = args.get('method', 'GET').upper()
+ body = args.get('body')
+
+ # Build full URL
+ if base_url and url and not url.startswith('http'):
+ full_url = f"{base_url.rstrip('/')}/{url.lstrip('/')}"
+ else:
+ full_url = url or base_url
+
+ if not full_url:
+ return {"error": "No URL provided"}
+
+ # Parse headers from node params
+ try:
+ default_headers = json.loads(node_params.get('headers', '{}'))
+ except:
+ default_headers = {}
+
+ logger.info(f"[HTTP Tool] {method} {full_url}")
+
+ try:
+ async with httpx.AsyncClient(timeout=30.0) as client:
+ response = await client.request(
+ method=method,
+ url=full_url,
+ headers=default_headers,
+ json=body if body else None
+ )
+
+ # Try to parse JSON response
+ try:
+ data = response.json()
+ except:
+ data = response.text
+
+ return {
+ "status": response.status_code,
+ "data": data,
+ "url": full_url,
+ "method": method
+ }
+
+ except httpx.TimeoutException:
+ return {"error": "Request timed out"}
+ except httpx.ConnectError as e:
+ return {"error": f"Connection failed: {str(e)}"}
+ except Exception as e:
+ logger.error(f"[HTTP Tool] Error: {e}")
+ return {"error": str(e)}
+
+
+async def _execute_python_code(args: Dict[str, Any],
+ node_params: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute Python code tool.
+
+ Args:
+ args: Dict with 'code'
+ node_params: Node parameters containing timeout, etc.
+
+ Returns:
+ Dict with result or output
+ """
+ import subprocess
+ import tempfile
+ import os
+
+ code = args.get('code', '')
+ timeout = int(node_params.get('timeout', 30))
+
+ if not code:
+ return {"error": "No code provided"}
+
+ # Create a temporary file with the code
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
+ # Wrap code to capture the result
+ wrapped_code = f"""
+import json
+import sys
+
+def main():
+{chr(10).join(' ' + line for line in code.split(chr(10)))}
+
+try:
+ result = main()
+ if result is not None:
+ print(json.dumps({{"result": result}}, default=str))
+ else:
+ print(json.dumps({{"result": "Code executed successfully"}}))
+except Exception as e:
+ print(json.dumps({{"error": str(e)}}))
+"""
+ f.write(wrapped_code)
+ temp_path = f.name
+
+ try:
+ logger.info(f"[Python Tool] Executing code (timeout: {timeout}s)")
+ result = subprocess.run(
+ ['python', temp_path],
+ capture_output=True,
+ text=True,
+ timeout=timeout
+ )
+
+ if result.returncode == 0:
+ try:
+ output = json.loads(result.stdout.strip())
+ return output
+ except:
+ return {"output": result.stdout.strip()}
+ else:
+ return {"error": result.stderr or "Code execution failed"}
+
+ except subprocess.TimeoutExpired:
+ return {"error": f"Code execution timed out after {timeout} seconds"}
+ except Exception as e:
+ logger.error(f"[Python Tool] Error: {e}")
+ return {"error": str(e)}
+ finally:
+ # Clean up temp file
+ try:
+ os.unlink(temp_path)
+ except:
+ pass
+
+
+async def _execute_current_time(args: Dict[str, Any],
+ node_params: Dict[str, Any]) -> Dict[str, Any]:
+ """Get current date and time.
+
+ Args:
+ args: Dict with optional 'timezone'
+ node_params: Node parameters containing default timezone
+
+ Returns:
+ Dict with datetime, date, time, timezone, day_of_week, timestamp
+ """
+ from datetime import datetime
+ import pytz
+
+ timezone_str = args.get('timezone') or node_params.get('timezone', 'UTC')
+
+ try:
+ tz = pytz.timezone(timezone_str)
+ now = datetime.now(tz)
+
+ result = {
+ "datetime": now.isoformat(),
+ "date": now.strftime("%Y-%m-%d"),
+ "time": now.strftime("%H:%M:%S"),
+ "timezone": timezone_str,
+ "day_of_week": now.strftime("%A"),
+ "timestamp": int(now.timestamp())
+ }
+ logger.info(f"[CurrentTime] {timezone_str}: {result['datetime']}")
+ return result
+ except Exception as e:
+ logger.error(f"[CurrentTime] Error: {e}")
+ return {"error": f"Invalid timezone: {timezone_str}. Error: {str(e)}"}
+
+
+async def _execute_web_search(args: Dict[str, Any],
+ node_params: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute web search.
+
+ Args:
+ args: Dict with 'query'
+ node_params: Node parameters containing provider, apiKey, maxResults
+
+ Returns:
+ Dict with query, results list, provider
+ """
+ import httpx
+ import asyncio
+
+ query = args.get('query', '')
+ if not query:
+ return {"error": "No search query provided"}
+
+ provider = node_params.get('provider', 'duckduckgo')
+ max_results = int(node_params.get('maxResults', 5))
+
+ logger.info(f"[WebSearch] Searching '{query}' via {provider}")
+
+ try:
+ if provider == 'duckduckgo':
+ # Use the duckduckgo-search library for proper web search results
+ try:
+ from duckduckgo_search import DDGS
+
+ # Run synchronous DDGS in a thread pool to not block async
+ def do_search():
+ with DDGS() as ddgs:
+ return list(ddgs.text(query, max_results=max_results))
+
+ search_results = await asyncio.get_event_loop().run_in_executor(
+ None, do_search
+ )
+
+ results = []
+ for item in search_results:
+ results.append({
+ "title": item.get('title', ''),
+ "snippet": item.get('body', ''),
+ "url": item.get('href', '')
+ })
+
+ logger.info(f"[WebSearch] Found {len(results)} results via DuckDuckGo")
+ return {
+ "query": query,
+ "results": results,
+ "provider": "duckduckgo"
+ }
+
+ except ImportError:
+ logger.warning("[WebSearch] duckduckgo-search not installed, falling back to Instant Answer API")
+ # Fallback to Instant Answer API (limited results)
+ async with httpx.AsyncClient(timeout=10.0) as client:
+ response = await client.get(
+ "https://api.duckduckgo.com/",
+ params={"q": query, "format": "json", "no_html": 1}
+ )
+ data = response.json()
+
+ results = []
+ if data.get('AbstractText'):
+ results.append({
+ "title": data.get('Heading', 'Result'),
+ "snippet": data.get('AbstractText'),
+ "url": data.get('AbstractURL', '')
+ })
+
+ for topic in data.get('RelatedTopics', [])[:max_results]:
+ if isinstance(topic, dict) and 'Text' in topic:
+ results.append({
+ "title": topic.get('Text', '')[:50],
+ "snippet": topic.get('Text', ''),
+ "url": topic.get('FirstURL', '')
+ })
+
+ logger.info(f"[WebSearch] Found {len(results)} results (Instant Answer API fallback)")
+ return {
+ "query": query,
+ "results": results[:max_results],
+ "provider": "duckduckgo"
+ }
+
+ elif provider == 'serper':
+ api_key = node_params.get('apiKey', '')
+ if not api_key:
+ return {"error": "Serper API key required"}
+
+ async with httpx.AsyncClient(timeout=10.0) as client:
+ response = await client.post(
+ "https://google.serper.dev/search",
+ headers={"X-API-KEY": api_key, "Content-Type": "application/json"},
+ json={"q": query, "num": max_results}
+ )
+ data = response.json()
+
+ results = []
+ for item in data.get('organic', [])[:max_results]:
+ results.append({
+ "title": item.get('title', ''),
+ "snippet": item.get('snippet', ''),
+ "url": item.get('link', '')
+ })
+
+ logger.info(f"[WebSearch] Found {len(results)} results via Serper")
+ return {
+ "query": query,
+ "results": results,
+ "provider": "serper"
+ }
+
+ return {"error": f"Unknown search provider: {provider}"}
+
+ except Exception as e:
+ logger.error(f"[WebSearch] Error: {e}")
+ return {"error": f"Search failed: {str(e)}"}
+
+
+async def _execute_whatsapp_send(args: Dict[str, Any],
+ node_params: Dict[str, Any]) -> Dict[str, Any]:
+ """Send WhatsApp message.
+
+ Args:
+ args: Dict with 'phone_number' and 'message'
+ node_params: Node parameters containing defaultPhone, etc.
+
+ Returns:
+ Dict with success status, phone, message_sent
+ """
+ import httpx
+
+ phone = args.get('phone_number') or node_params.get('phoneNumber', '') or node_params.get('to', '')
+ message = args.get('message') or node_params.get('message', '')
+
+ if not phone:
+ return {"error": "No phone number provided"}
+ if not message:
+ return {"error": "No message provided"}
+
+ # Clean phone number
+ phone = phone.replace('+', '').replace(' ', '').replace('-', '')
+ if not phone.endswith('@s.whatsapp.net'):
+ phone = f"{phone}@s.whatsapp.net"
+
+ logger.info(f"[WhatsApp] Sending message to {phone[:10]}...")
+
+ try:
+ async with httpx.AsyncClient(timeout=30.0) as client:
+ response = await client.post(
+ "http://localhost:5000/send",
+ json={"to": phone, "message": message}
+ )
+
+ if response.status_code == 200:
+ logger.info(f"[WhatsApp] Message sent successfully")
+ return {
+ "success": True,
+ "phone": phone,
+ "message_sent": message[:100] + "..." if len(message) > 100 else message
+ }
+ else:
+ return {"error": f"Failed to send: {response.text}"}
+
+ except Exception as e:
+ logger.error(f"[WhatsApp] Error: {e}")
+ return {"error": f"WhatsApp send failed: {str(e)}"}
+
+
+async def _execute_generic(args: Dict[str, Any],
+ config: Dict[str, Any]) -> Dict[str, Any]:
+ """Execute a generic tool (fallback handler).
+
+ For node types without specific handlers, this returns the input
+ along with node information.
+
+ Args:
+ args: Tool arguments
+ config: Tool configuration
+
+ Returns:
+ Dict with input echoed and node info
+ """
+ return {
+ "input": args.get('input', ''),
+ "node_type": config.get('node_type'),
+ "node_id": config.get('node_id'),
+ "message": "Generic tool executed - no specific handler for this node type"
+ }