Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Add WhatsApp Chat History node and fix Gemini 3 tool calling
trohitga800ed9b958b
diff --git a/README.md b/README.mdindex 1c44291f..bc5cdddd 100644--- a/README.md+++ b/README.md@@ -26,10 +26,11 @@ npm run start## Features-- **AI Workflows** - OpenAI, Claude, Gemini models-- **Android Automation** - Control your phone-- **WhatsApp Bots** - Send/receive messages+- **AI Workflows** - OpenAI, Claude, Gemini models with tool calling+- **Android Automation** - Control your phone via 17 service nodes+- **WhatsApp Bots** - Send/receive messages with filters- **HTTP/Webhooks** - API integrations+- **Tool Schema Editor** - Customize AI tool schemas per service## Docker Commandsdiff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsxindex e954be23..b0dafedd 100644--- a/client/src/Dashboard.tsx+++ b/client/src/Dashboard.tsx@@ -77,7 +77,7 @@ const createNodeTypes = (): Record<string, React.ComponentType<any>> => {} else if (type === 'simpleMemory') {// Simple Memory node for AI conversation history - uses circular ModelNode designtypes[type] = ModelNode;- } else if (type === 'whatsappConnect' || type === 'whatsappSend') {+ } else if (type === 'whatsappConnect' || type === 'whatsappSend' || type === 'whatsappChatHistory') {// WhatsApp action nodes use SquareNode (whatsappReceive is a trigger)types[type] = SquareNode;} else if (ANDROID_SERVICE_NODE_TYPES.includes(type) || ANDROID_DEVICE_NODE_TYPES.includes(type)) {diff --git a/client/src/components/ParameterRenderer.tsx b/client/src/components/ParameterRenderer.tsxindex 1f65da75..0991306e 100644--- a/client/src/components/ParameterRenderer.tsx+++ b/client/src/components/ParameterRenderer.tsx@@ -215,7 +215,7 @@ const GroupIdSelector: React.FC<{onDragLeave: (e: React.DragEvent) => void;onDrop: (e: React.DragEvent) => void;}> = ({ value, onChange, onNameChange, storedName, placeholder, theme, isDragOver, onDragOver, onDragLeave, onDrop }) => {- const [groups, setGroups] = useState<Array<{ jid: string; name: string; topic?: string; size?: number }>>([]);+ const [groups, setGroups] = useState<Array<{ jid: string; name: string; topic?: string; size?: number; is_community?: boolean }>>([]);const [isLoading, setIsLoading] = useState(false);const [showDropdown, setShowDropdown] = useState(false);const [error, setError] = useState<string | null>(null);@@ -236,12 +236,20 @@ const GroupIdSelector: React.FC<{setError(null);try {const result = await getWhatsAppGroups();+ console.log('[GroupIdSelector] Raw groups from API:', result.groups?.map(g => ({ name: g.name, jid: g.jid, is_community: g.is_community })));if (result.success && result.groups.length > 0) {- setGroups(result.groups);+ // Filter out communities - they don't have regular chat history+ const regularGroups = result.groups.filter(g => !g.is_community);+ console.log('[GroupIdSelector] After filtering communities:', regularGroups.length, 'groups remaining');+ if (regularGroups.length === 0) {+ setError('Only communities found (no chat history available)');+ return;+ }+ setGroups(regularGroups);setShowDropdown(true);// If we already have a value, try to find its name and update storageif (value) {- const matchingGroup = result.groups.find(g => g.jid === value);+ const matchingGroup = regularGroups.find(g => g.jid === value);if (matchingGroup && matchingGroup.name !== storedName) {setLocalGroupName(matchingGroup.name);onNameChange?.(matchingGroup.name);diff --git a/client/src/components/SquareNode.tsx b/client/src/components/SquareNode.tsxindex 54e64381..d4a30a27 100644--- a/client/src/components/SquareNode.tsx+++ b/client/src/components/SquareNode.tsx@@ -21,7 +21,7 @@ const ANDROID_TOOL_CAPABLE_NODES = ANDROID_SERVICE_NODE_TYPES;const GOOGLE_MAPS_NODE_TYPES = ['createMap', 'addLocations', 'showNearbyPlaces'];// WhatsApp node types-const WHATSAPP_NODE_TYPES = ['whatsappConnect', 'whatsappSend', 'whatsappReceive'];+const WHATSAPP_NODE_TYPES = ['whatsappConnect', 'whatsappSend', 'whatsappReceive', 'whatsappChatHistory'];// AI Model node types with their provider IDsconst AI_MODEL_NODE_TYPES: Record<string, string> = {diff --git a/client/src/components/ui/NodeOutputPanel.tsx b/client/src/components/ui/NodeOutputPanel.tsxindex e02d8451..dd4bb228 100644--- a/client/src/components/ui/NodeOutputPanel.tsx+++ b/client/src/components/ui/NodeOutputPanel.tsx@@ -96,6 +96,13 @@ const NodeOutputPanel: React.FC<NodeOutputPanelProps> = ({// WhatsApp message previewif (data?.result?.preview) return data.result.preview;if (data?.preview) return data.preview;+ // WhatsApp Chat History output+ if (data?.result?.messages !== undefined && data?.result?.total !== undefined) {+ return { type: 'whatsapp_history', messages: data.result.messages, total: data.result.total, count: data.result.count, hasMore: data.result.has_more };+ }+ if (data?.messages !== undefined && data?.total !== undefined) {+ return { type: 'whatsapp_history', messages: data.messages, total: data.total, count: data.count || data.messages?.length, hasMore: data.has_more };+ }// Webhook trigger outputif (data?.method && data?.path !== undefined) {let bodyData = data.json || (data.body ? (() => { try { return JSON.parse(data.body); } catch { return data.body; } })() : null);@@ -348,6 +355,168 @@ const NodeOutputPanel: React.FC<NodeOutputPanelProps> = ({{highlightJSON(JSON.stringify((mainResponse as any).data, null, 2), theme.dracula)}</pre></div>+ ) : mainResponse && typeof mainResponse === 'object' && (mainResponse as any).type === 'whatsapp_history' ? (+ /* WhatsApp Chat History Response */+ <div style={{+ marginBottom: theme.spacing.md,+ }}>+ {/* Header with count */}+ <div style={{+ padding: theme.spacing.md,+ backgroundColor: theme.colors.backgroundElevated,+ border: `1px solid #25D36640`,+ borderRadius: `${theme.borderRadius.md} ${theme.borderRadius.md} 0 0`,+ borderLeft: `3px solid #25D366`,+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'space-between',+ }}>+ <div style={{+ fontSize: theme.fontSize.xs,+ fontWeight: theme.fontWeight.semibold,+ color: '#25D366',+ textTransform: 'uppercase',+ letterSpacing: '0.1em',+ display: 'flex',+ alignItems: 'center',+ gap: theme.spacing.sm,+ }}>+ <svg width="14" height="14" viewBox="0 0 24 24" fill="#25D366">+ <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/>+ </svg>+ Chat History+ </div>+ <div style={{+ fontSize: theme.fontSize.xs,+ color: theme.colors.textSecondary,+ display: 'flex',+ gap: theme.spacing.md,+ }}>+ <span style={{ color: '#25D366' }}>{(mainResponse as any).count || 0} messages</span>+ {(mainResponse as any).total > 0 && (+ <span>of {(mainResponse as any).total} total</span>+ )}+ {(mainResponse as any).hasMore && (+ <span style={{ color: theme.dracula.orange }}>more available</span>+ )}+ </div>+ </div>++ {/* Messages list */}+ <div style={{+ backgroundColor: theme.colors.backgroundElevated,+ border: `1px solid #25D36640`,+ borderTop: 'none',+ borderRadius: `0 0 ${theme.borderRadius.md} ${theme.borderRadius.md}`,+ maxHeight: '400px',+ overflow: 'auto',+ }}>+ {(mainResponse as any).messages?.length === 0 ? (+ <div style={{+ padding: theme.spacing.xl,+ textAlign: 'center',+ color: theme.colors.textMuted,+ fontSize: theme.fontSize.sm,+ }}>+ No messages found for this chat+ </div>+ ) : (+ (mainResponse as any).messages?.map((msg: any, idx: number) => (+ <div+ key={msg.message_id || idx}+ style={{+ padding: theme.spacing.md,+ borderBottom: idx < (mainResponse as any).messages.length - 1 ? `1px solid ${theme.colors.border}` : 'none',+ display: 'flex',+ flexDirection: 'column',+ gap: theme.spacing.xs,+ }}+ >+ {/* Message header */}+ <div style={{+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'space-between',+ gap: theme.spacing.sm,+ }}>+ <div style={{+ display: 'flex',+ alignItems: 'center',+ gap: theme.spacing.sm,+ }}>+ {/* From me indicator */}+ {msg.is_from_me ? (+ <span style={{+ fontSize: '10px',+ padding: '2px 6px',+ backgroundColor: theme.dracula.cyan + '20',+ color: theme.dracula.cyan,+ borderRadius: theme.borderRadius.sm,+ fontWeight: theme.fontWeight.medium,+ }}>+ You+ </span>+ ) : (+ <span style={{+ fontSize: theme.fontSize.xs,+ fontWeight: theme.fontWeight.medium,+ color: theme.dracula.purple,+ }}>+ {msg.sender_phone || msg.sender?.split('@')[0] || 'Unknown'}+ </span>+ )}+ {/* Message type badge */}+ {msg.message_type !== 'text' && (+ <span style={{+ fontSize: '10px',+ padding: '2px 6px',+ backgroundColor: theme.dracula.orange + '20',+ color: theme.dracula.orange,+ borderRadius: theme.borderRadius.sm,+ textTransform: 'uppercase',+ }}>+ {msg.message_type}+ </span>+ )}+ {msg.is_group && (+ <span style={{+ fontSize: '10px',+ padding: '2px 6px',+ backgroundColor: theme.dracula.green + '20',+ color: theme.dracula.green,+ borderRadius: theme.borderRadius.sm,+ }}>+ Group+ </span>+ )}+ </div>+ {/* Timestamp */}+ <span style={{+ fontSize: theme.fontSize.xs,+ color: theme.colors.textMuted,+ }}>+ {msg.timestamp ? new Date(msg.timestamp).toLocaleString() : ''}+ </span>+ </div>+ {/* Message text */}+ {msg.text && (+ <div style={{+ fontSize: theme.fontSize.sm,+ color: theme.colors.text,+ lineHeight: 1.5,+ whiteSpace: 'pre-wrap',+ wordBreak: 'break-word',+ paddingLeft: theme.spacing.sm,+ borderLeft: `2px solid ${msg.is_from_me ? theme.dracula.cyan : theme.dracula.purple}30`,+ }}>+ {msg.text}+ </div>+ )}+ </div>+ ))+ )}+ </div>+ </div>) : mainResponse && (/* Standard Response (AI, etc) */<div style={{diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsxindex e173d2a6..cd8fd21b 100644--- a/client/src/contexts/WebSocketContext.tsx+++ b/client/src/contexts/WebSocketContext.tsx@@ -207,7 +207,7 @@ interface WebSocketContextValue {sendWhatsAppMessage: (phone: string, message: string) => Promise<{ success: boolean; messageId?: string; error?: string }>;startWhatsAppConnection: () => Promise<{ success: boolean; message?: string }>;restartWhatsAppConnection: () => Promise<{ success: boolean; message?: string }>;- getWhatsAppGroups: () => Promise<{ success: boolean; groups: Array<{ jid: string; name: string; topic?: string; size?: number }>; error?: string }>;+ getWhatsAppGroups: () => Promise<{ success: boolean; groups: Array<{ jid: string; name: string; topic?: string; size?: number; is_community?: boolean }>; error?: string }>;getWhatsAppGroupInfo: (groupId: string) => Promise<{ success: boolean; participants: Array<{ phone: string; name: string; jid: string; is_admin?: boolean }>; name?: string; error?: string }>;}@@ -1480,7 +1480,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi}}, [sendRequest]);- const getWhatsAppGroupsAsync = useCallback(async (): Promise<{ success: boolean; groups: Array<{ jid: string; name: string; topic?: string; size?: number }>; error?: string }> => {+ const getWhatsAppGroupsAsync = useCallback(async (): Promise<{ success: boolean; groups: Array<{ jid: string; name: string; topic?: string; size?: number; is_community?: boolean }>; error?: string }> => {try {const response = await sendRequest<any>('whatsapp_groups', {});return {diff --git a/client/src/nodeDefinitions/whatsappNodes.ts b/client/src/nodeDefinitions/whatsappNodes.tsindex 3573124d..5c7d3053 100644--- a/client/src/nodeDefinitions/whatsappNodes.ts+++ b/client/src/nodeDefinitions/whatsappNodes.ts@@ -67,6 +67,9 @@ const WHATSAPP_CONNECT_ICON = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.or// WhatsApp Receive - Notification bell with dot (trigger node)const WHATSAPP_RECEIVE_ICON = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2325D366'%3E%3Cpath d='M12 22c1.1 0 2-.9 2-2h-4c0 1.1.9 2 2 2zm6-6v-5c0-3.07-1.63-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.64 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2zm-2 1H8v-6c0-2.48 1.51-4.5 4-4.5s4 2.02 4 4.5v6z'/%3E%3Ccircle cx='18' cy='6' r='4'/%3E%3C/svg%3E";+// WhatsApp Chat History - Clock icon (retrieve history)+const WHATSAPP_HISTORY_ICON = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2325D366'%3E%3Cpath d='M13 3a9 9 0 0 0-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42A8.954 8.954 0 0 0 13 21a9 9 0 0 0 0-18zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z'/%3E%3C/svg%3E";+// ============================================================================// WHATSAPP NODES// ============================================================================@@ -550,10 +553,15 @@ export const whatsappNodes: Record<string, INodeTypeDescription> = {loadOptions: {async getWhatsAppGroups(): Promise<Array<{name: string, value: string, description?: string}>> {try {- const response = await wsRequest('whatsapp_groups') as { success: boolean; groups?: Array<{ jid: string; name: string; participant_count?: number }> };+ const response = await wsRequest('whatsapp_groups') as { success: boolean; groups?: Array<{ jid: string; name: string; participant_count?: number; is_community?: boolean }> };if (response.success && response.groups) {- return response.groups.map((group) => ({+ // Filter out communities - they don't have regular chat history+ const regularGroups = response.groups.filter((group) => !group.is_community);+ if (regularGroups.length === 0) {+ return [{ name: 'No groups found', value: '', description: 'Only communities found (no chat history)' }];+ }+ return regularGroups.map((group) => ({name: group.name || group.jid,value: group.jid,description: group.participant_count ? `${group.participant_count} members` : undefined@@ -607,6 +615,154 @@ export const whatsappNodes: Record<string, INodeTypeDescription> = {}}}+ },++ // WhatsApp Chat History Node - Retrieve stored messages+ whatsappChatHistory: {+ displayName: 'WhatsApp Chat History',+ name: 'whatsappChatHistory',+ icon: WHATSAPP_HISTORY_ICON,+ group: ['whatsapp'],+ version: 1,+ subtitle: 'Get Messages',+ description: 'Retrieve chat history from WhatsApp conversations. Messages are synced on first login and stored for later retrieval.',+ defaults: { name: 'Chat History', color: '#25D366' },+ inputs: [{+ name: 'main',+ displayName: 'Input',+ type: 'main' as NodeConnectionType,+ description: 'Trigger input'+ }],+ outputs: [{+ name: 'main',+ displayName: 'Messages',+ type: 'main' as NodeConnectionType,+ description: 'Chat history messages (messages, total, has_more)'+ }],+ properties: [+ // ===== CHAT TYPE =====+ {+ displayName: 'Chat Type',+ name: 'chatType',+ type: 'options',+ options: [+ { name: 'Individual Chat', value: 'individual' },+ { name: 'Group Chat', value: 'group' }+ ],+ default: 'individual',+ description: 'Type of chat to retrieve history from'+ },++ // ===== INDIVIDUAL CHAT =====+ {+ displayName: 'Phone Number',+ name: 'phone',+ type: 'string',+ default: '',+ required: true,+ placeholder: '1234567890',+ description: 'Phone number of the contact (without + prefix)',+ displayOptions: {+ show: { chatType: ['individual'] }+ }+ },++ // ===== GROUP CHAT =====+ {+ displayName: 'Group',+ name: 'group_id',+ type: 'string',+ default: '',+ required: true,+ placeholder: '[email protected]',+ description: 'Group JID to retrieve messages from',+ displayOptions: {+ show: { chatType: ['group'] }+ }+ },+ {+ displayName: 'Message Filter',+ name: 'groupFilter',+ type: 'options',+ options: [+ { name: 'All Messages', value: 'all' },+ { name: 'From Specific Contact', value: 'contact' }+ ],+ default: 'all',+ description: 'Filter messages in group',+ displayOptions: {+ show: { chatType: ['group'] }+ }+ },+ {+ displayName: 'Contact Phone',+ name: 'senderPhone',+ type: 'string',+ default: '',+ placeholder: '1234567890',+ description: 'Filter messages from specific group member',+ displayOptions: {+ show: { chatType: ['group'], groupFilter: ['contact'] }+ }+ },++ // ===== MESSAGE TYPE FILTER =====+ {+ displayName: 'Message Type',+ name: 'textOnly',+ type: 'options',+ options: [+ { name: 'All Types', value: 'false' },+ { name: 'Text Only', value: 'true' }+ ],+ default: 'false',+ description: 'Filter by message type'+ },++ // ===== PAGINATION =====+ {+ displayName: 'Limit',+ name: 'limit',+ type: 'number',+ default: 50,+ typeOptions: { minValue: 1, maxValue: 500 },+ description: 'Maximum number of messages to retrieve (1-500)'+ },+ {+ displayName: 'Offset',+ name: 'offset',+ type: 'number',+ default: 0,+ typeOptions: { minValue: 0 },+ description: 'Number of messages to skip (for pagination)'+ }+ ],+ methods: {+ loadOptions: {+ async getWhatsAppGroups(): Promise<Array<{name: string, value: string, description?: string}>> {+ try {+ const response = await wsRequest('whatsapp_groups') as { success: boolean; groups?: Array<{ jid: string; name: string; participant_count?: number; is_community?: boolean }> };++ if (response.success && response.groups) {+ // Filter out communities - they don't have regular chat history+ const regularGroups = response.groups.filter((group) => !group.is_community);+ if (regularGroups.length === 0) {+ return [{ name: 'No groups found', value: '', description: 'Only communities found (no chat history)' }];+ }+ return regularGroups.map((group) => ({+ name: group.name || group.jid,+ value: group.jid,+ description: group.participant_count ? `${group.participant_count} members` : undefined+ }));+ }+ return [{ name: 'No groups found', value: '', description: 'Connect WhatsApp first' }];+ } catch (error) {+ console.error('Error loading WhatsApp groups:', error);+ return [{ name: 'Error loading groups', value: '', description: 'Check WhatsApp connection' }];+ }+ }+ }+ }}};@@ -614,4 +770,4 @@ export const whatsappNodes: Record<string, INodeTypeDescription> = {// EXPORTS// ============================================================================-export const WHATSAPP_NODE_TYPES = ['whatsappSend', 'whatsappConnect', 'whatsappReceive'];+export const WHATSAPP_NODE_TYPES = ['whatsappSend', 'whatsappConnect', 'whatsappReceive', 'whatsappChatHistory'];diff --git a/client/src/services/executionService.ts b/client/src/services/executionService.tsindex 2e834117..e3262aed 100644--- a/client/src/services/executionService.ts+++ b/client/src/services/executionService.ts@@ -204,6 +204,7 @@ export class ExecutionService {'whatsappSend','whatsappConnect','whatsappReceive',+ 'whatsappChatHistory',// Text Processing Nodes'textGenerator','fileHandler',diff --git a/server/requirements.txt b/server/requirements.txtindex 72db08c3..6cf564df 100644--- a/server/requirements.txt+++ b/server/requirements.txt@@ -34,14 +34,15 @@ qrcode[pil]>=7.4.0# Loggingstructlog>=23.2.0-# AI Providers (LangChain) - minimal versions-langchain-core>=0.1.40-langchain-openai>=0.1.0-langchain-anthropic>=0.1.5-langchain-google-genai>=1.0.0+# AI Providers (LangChain) - compatible versions for langchain-core 1.2.x+langchain-core>=1.2.5+langchain-openai>=1.0.0+langchain-anthropic>=1.0.0+# Gemini 3+ requires >=3.1.0 for thought_signature support in tool calling+langchain-google-genai>=3.1.0# LangGraph - State machine for AI agents-langgraph>=1.0.0+langgraph>=1.0.6# Google Servicesgooglemaps>=4.10.0diff --git a/server/routers/websocket.py b/server/routers/websocket.pyindex 0c58de9a..60cda13a 100644--- a/server/routers/websocket.py+++ b/server/routers/websocket.py@@ -1287,6 +1287,7 @@ from routers.whatsapp import (handle_whatsapp_restart as _wa_restart,handle_whatsapp_groups as _wa_groups,handle_whatsapp_group_info as _wa_group_info,+ handle_whatsapp_chat_history as _wa_chat_history,)@@ -1321,6 +1322,11 @@ async def handle_whatsapp_group_info(data: Dict[str, Any], websocket: WebSocket)return await _wa_group_info(group_id)+async def handle_whatsapp_chat_history(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Get chat history from WhatsApp history store."""+ return await _wa_chat_history(data)++# ============================================================================# Workflow Storage Operations# ============================================================================@@ -1459,6 +1465,7 @@ MESSAGE_HANDLERS: Dict[str, MessageHandler] = {"whatsapp_restart": handle_whatsapp_restart,"whatsapp_groups": handle_whatsapp_groups,"whatsapp_group_info": handle_whatsapp_group_info,+ "whatsapp_chat_history": handle_whatsapp_chat_history,# Workflow storage operations"save_workflow": handle_save_workflow,diff --git a/server/routers/whatsapp.py b/server/routers/whatsapp.pyindex 801f3b8e..d4b771ed 100644--- a/server/routers/whatsapp.py+++ b/server/routers/whatsapp.py@@ -663,3 +663,74 @@ async def handle_whatsapp_group_info(group_id: str) -> dict:except Exception as e:logger.error(f"WhatsApp group_info fetch failed for {group_id}: {e}")return {"success": False, "error": str(e), "participants": []}+++async def handle_whatsapp_chat_history(params: dict) -> dict:+ """Get chat history from WhatsApp via direct RPC.++ Retrieves stored messages from the Go service's history store.+ Messages are automatically stored from HistorySync (on first login)+ and from real-time incoming messages.++ Params:+ - chat_id: Direct chat JID (e.g., '[email protected]')+ - phone: Phone number (alternative to chat_id, will be converted)+ - group_id: Group JID (alternative for group chats)+ - limit: Max messages to return (default 50, max 500)+ - offset: Pagination offset (default 0)+ - sender_phone: Filter by sender phone in group chats+ - text_only: Only return text messages (default false)++ Returns:+ - messages: Array of MessageRecord+ - total: Total matching messages count+ - has_more: Whether more messages exist+ """+ try:+ client = await get_client()++ # Build RPC params+ rpc_params = {}++ # Determine chat_id from various inputs+ chat_id = params.get("chat_id")+ phone = params.get("phone")+ group_id = params.get("group_id")++ if chat_id:+ rpc_params["chat_id"] = chat_id+ elif phone:+ rpc_params["phone"] = phone+ elif group_id:+ rpc_params["group_id"] = group_id+ else:+ return {"success": False, "error": "Either chat_id, phone, or group_id is required"}++ # Optional filters+ limit = params.get("limit", 50)+ if limit > 500:+ limit = 500+ rpc_params["limit"] = limit++ offset = params.get("offset", 0)+ rpc_params["offset"] = offset++ sender_phone = params.get("sender_phone")+ if sender_phone:+ rpc_params["sender_phone"] = sender_phone++ text_only = params.get("text_only", False)+ rpc_params["text_only"] = text_only++ result = await client.call("chat_history", rpc_params)++ return {+ "success": True,+ "messages": result.get("messages", []),+ "total": result.get("total", 0),+ "has_more": result.get("has_more", False),+ "timestamp": time.time()+ }+ except Exception as e:+ logger.error(f"WhatsApp chat_history fetch failed: {e}")+ return {"success": False, "error": str(e), "messages": [], "total": 0, "has_more": False}diff --git a/server/services/ai.py b/server/services/ai.pyindex fdf90703..b62bf647 100644--- a/server/services/ai.py+++ b/server/services/ai.py@@ -111,6 +111,114 @@ def get_default_model(provider: str) -> str:return config.default_model if config else 'gpt-4o-mini'+# =============================================================================+# MESSAGE FILTERING UTILITIES - Standardized for all providers+# =============================================================================++def is_valid_message_content(content: Any) -> bool:+ """Check if message content is valid (non-empty) for API calls.++ This is a standardized utility for validating message content before:+ - Saving to conversation memory+ - Including in API requests+ - Building message history++ Args:+ content: The message content to validate (str, list, or other)++ Returns:+ True if content is valid and non-empty, False otherwise+ """+ if content is None:+ return False++ # Handle list content format (Gemini returns [{"type": "text", "text": "..."}])+ if isinstance(content, list):+ return any(+ (isinstance(block, dict) and block.get('text', '').strip()) or+ (isinstance(block, str) and block.strip())+ for block in content+ )++ # Handle string content (most common)+ if isinstance(content, str):+ return bool(content.strip())++ # Other truthy content types+ return bool(content)+++def filter_empty_messages(messages: Sequence[BaseMessage]) -> List[BaseMessage]:+ """Filter out messages with empty content to prevent API errors.++ This is a standardized utility that handles empty message filtering for all+ AI providers (OpenAI, Anthropic/Claude, Google Gemini, and future providers).++ Different providers have different sensitivities:+ - Gemini: Emits "HumanMessage with empty content was removed" warning+ - Claude/Anthropic: Throws errors for empty HumanMessage content+ - OpenAI: Generally tolerant but empty messages waste tokens++ This filter preserves:+ - ToolMessage: Always kept (contains tool execution results)+ - AIMessage with tool_calls: Kept even if content empty (tool calls are content)+ - SystemMessage: Kept only if has non-empty content+ - HumanMessage/others: Filtered if content is empty++ Args:+ messages: Sequence of LangChain BaseMessage objects++ Returns:+ Filtered list of messages with empty content removed+ """+ filtered = []++ for m in messages:+ # ToolMessage - always keep (contains tool execution results from LangGraph)+ if isinstance(m, ToolMessage):+ filtered.append(m)+ continue++ # AIMessage with tool_calls - keep even if content is empty+ # (the tool calls themselves are the meaningful content)+ if isinstance(m, AIMessage) and hasattr(m, 'tool_calls') and m.tool_calls:+ filtered.append(m)+ continue++ # SystemMessage - keep only if has non-empty content+ if isinstance(m, SystemMessage):+ if hasattr(m, 'content') and m.content and str(m.content).strip():+ filtered.append(m)+ continue++ # HumanMessage and other message types - filter out empty content+ if hasattr(m, 'content'):+ content = m.content++ # Handle list content format (Gemini returns [{"type": "text", "text": "..."}])+ if isinstance(content, list):+ has_content = any(+ (isinstance(block, dict) and block.get('text', '').strip()) or+ (isinstance(block, str) and block.strip())+ for block in content+ )+ if has_content:+ filtered.append(m)++ # Handle string content (most common)+ elif isinstance(content, str) and content.strip():+ filtered.append(m)++ # Handle other non-empty content types (keep if truthy)+ elif content:+ filtered.append(m)+ else:+ # Message without content attr - keep it (might be special message type)+ filtered.append(m)++ return filtered++# =============================================================================# LANGGRAPH STATE MACHINE DEFINITIONS# =============================================================================@@ -149,10 +257,36 @@ def create_agent_node(chat_model):logger.info(f"[LangGraph] Agent node invoked, iteration={iteration}, messages={len(messages)}")+ # Filter out messages with empty content using standardized utility+ # Prevents API errors/warnings from Gemini, Claude, and other providers+ filtered_messages = filter_empty_messages(messages)++ if len(filtered_messages) != len(messages):+ logger.info(f"[LangGraph] Filtered out {len(messages) - len(filtered_messages)} empty messages before LLM invoke")+# Invoke the model- response = chat_model.invoke(list(messages))+ response = chat_model.invoke(filtered_messages)logger.info(f"[LangGraph] LLM response type: {type(response)}, has tool_calls attr: {hasattr(response, 'tool_calls')}")++ # Debug log full response for troubleshooting empty responses (especially Gemini)+ if hasattr(response, 'content'):+ logger.info(f"[LangGraph] response.content type: {type(response.content)}, length: {len(str(response.content)) if response.content else 0}")++ # Check for Gemini-specific response attributes (safety ratings, block reason)+ if hasattr(response, 'response_metadata'):+ meta = response.response_metadata+ logger.info(f"[LangGraph] response_metadata: {meta}")+ # Check for Gemini finish reason (could indicate blocked content)+ if meta.get('finish_reason') == 'SAFETY':+ logger.warning("[LangGraph] Gemini response blocked by safety filters")+ if meta.get('block_reason'):+ logger.warning(f"[LangGraph] Gemini block reason: {meta.get('block_reason')}")++ # Log additional_kwargs which may contain Gemini-specific info+ if hasattr(response, 'additional_kwargs') and response.additional_kwargs:+ logger.info(f"[LangGraph] additional_kwargs: {response.additional_kwargs}")+if hasattr(response, 'tool_calls'):logger.info(f"[LangGraph] tool_calls value: {response.tool_calls}")@@ -317,6 +451,82 @@ class AIService:"""Detect AI provider from model name."""return detect_provider_from_model(model)+ def _extract_text_content(self, content, ai_response=None) -> str:+ """Extract text content from various response formats.++ Handles:+ - String content (OpenAI, Anthropic)+ - List of content blocks (Gemini 3+ models)+ - Empty/None content with error details from metadata++ Args:+ content: The raw content from response (str, list, or None)+ ai_response: The full AIMessage for metadata inspection++ Returns:+ Extracted text string++ Raises:+ ValueError: If content is empty with details about why+ """+ # Handle list content (Gemini format: [{"type": "text", "text": "..."}])+ if isinstance(content, list):+ text_parts = []+ for block in content:+ if isinstance(block, dict):+ if block.get('type') == 'text' and block.get('text'):+ text_parts.append(block['text'])+ elif 'text' in block:+ text_parts.append(str(block['text']))+ elif isinstance(block, str):+ text_parts.append(block)+ extracted = '\n'.join(text_parts)+ if extracted.strip():+ return extracted+ # List was present but no text extracted+ logger.warning(f"[LangGraph] Content was list but no text extracted: {content}")++ # Handle string content+ if isinstance(content, str) and content.strip():+ return content++ # Content is empty - try to get error details from metadata+ error_details = []+ if ai_response and hasattr(ai_response, 'response_metadata'):+ meta = ai_response.response_metadata+ finish_reason = meta.get('finish_reason', '')++ if finish_reason == 'SAFETY':+ error_details.append("Content blocked by safety filters")+ # Try to get specific blocked categories+ safety_ratings = meta.get('safety_ratings', [])+ blocked = [r.get('category') for r in safety_ratings if r.get('blocked')]+ if blocked:+ error_details.append(f"Blocked categories: {', '.join(blocked)}")++ elif finish_reason == 'MAX_TOKENS':+ # Check if reasoning consumed all tokens+ token_details = meta.get('output_token_details', {})+ reasoning_tokens = token_details.get('reasoning', 0)+ output_tokens = meta.get('usage_metadata', {}).get('candidates_token_count', 0)+ if reasoning_tokens > 0 and output_tokens == 0:+ error_details.append(f"Model used all tokens for reasoning ({reasoning_tokens} tokens). Try increasing max_tokens or simplifying the prompt.")+ else:+ error_details.append("Response truncated due to max_tokens limit")++ elif finish_reason == 'MALFORMED_FUNCTION_CALL':+ error_details.append("Model returned malformed function call. Tool schema may be incompatible.")++ if meta.get('block_reason'):+ error_details.append(f"Block reason: {meta.get('block_reason')}")++ if error_details:+ raise ValueError(f"AI returned empty response. {'; '.join(error_details)}")++ # Generic empty response+ logger.warning(f"[LangGraph] Empty response with no error details. Content type: {type(content)}, value: {content}")+ raise ValueError("AI generated empty response. Try rephrasing your prompt or using a different model.")+def create_model(self, provider: str, api_key: str, model: str,temperature: float, max_tokens: int):"""Create LangChain model instance using provider registry."""@@ -417,18 +627,25 @@ class AIService:if not api_key:raise ValueError("API key is required")+ # Validate prompt is not empty (prevents wasted API calls for all providers)+ if not is_valid_message_content(prompt):+ raise ValueError("Prompt cannot be empty")+# Create modelprovider = self.detect_provider(model)chat_model = self.create_model(provider, api_key, model, temperature, max_tokens)# Prepare messagesmessages = []- if system_prompt:+ if system_prompt and is_valid_message_content(system_prompt):messages.append(SystemMessage(content=system_prompt))messages.append(HumanMessage(content=prompt))+ # Filter messages using standardized utility (handles all providers consistently)+ filtered_messages = filter_empty_messages(messages)+# Execute- response = chat_model.invoke(messages)+ response = chat_model.invoke(filtered_messages)result = {"response": response.content,@@ -566,12 +783,16 @@ class AIService:history_data = await self.database.get_conversation_messages(session_id, window_size)history_count = len(history_data)- # Convert to LangChain messages+ # Convert to LangChain messages using standardized content validationfor m in history_data:+ content = m.get('content', '')+ if not is_valid_message_content(content):+ logger.debug(f"[LangGraph Memory] Skipping empty {m['role']} message")+ continueif m['role'] == 'human':- initial_messages.append(HumanMessage(content=m['content']))+ initial_messages.append(HumanMessage(content=content))elif m['role'] == 'ai':- initial_messages.append(AIMessage(content=m['content']))+ initial_messages.append(AIMessage(content=content))logger.info(f"[LangGraph Memory] Loaded {history_count} messages from session '{session_id}'")@@ -699,13 +920,16 @@ class AIService:if not ai_response or not hasattr(ai_response, 'content'):raise ValueError("No response generated from agent")- response_content = ai_response.content+ # Handle different content formats (Gemini can return list of content blocks)+ raw_content = ai_response.content+ response_content = self._extract_text_content(raw_content, ai_response)iterations = final_state.get("iteration", 1)- logger.info(f"[LangGraph] Agent completed in {iterations} iteration(s)")+ logger.info(f"[LangGraph] Agent completed in {iterations} iteration(s), response length: {len(response_content)}")# Save to memory if connected (persist to database)- if session_id:+ # Only save non-empty messages using standardized validation+ if session_id and is_valid_message_content(prompt) and is_valid_message_content(response_content):# Broadcast: Saving to memoryawait broadcast_status("saving_memory", {"message": "Saving to conversation memory...",diff --git a/server/services/handlers/__init__.py b/server/services/handlers/__init__.pyindex 2dca0f65..e54b6d2b 100644--- a/server/services/handlers/__init__.py+++ b/server/services/handlers/__init__.py@@ -59,6 +59,7 @@ from .utility import (from .whatsapp import (handle_whatsapp_send,handle_whatsapp_connect,+ handle_whatsapp_chat_history,)# Tool execution handlers (for AI Agent tool calling)@@ -96,6 +97,7 @@ __all__ = ['handle_whatsapp_send','handle_whatsapp_connect',+ 'handle_whatsapp_chat_history',# Tools'execute_tool',]diff --git a/server/services/handlers/whatsapp.py b/server/services/handlers/whatsapp.pyindex 2a31466d..9d802f60 100644--- a/server/services/handlers/whatsapp.py+++ b/server/services/handlers/whatsapp.py@@ -164,3 +164,106 @@ async def handle_whatsapp_connect("execution_time": time.time() - start_time,"timestamp": datetime.now().isoformat()}+++async def handle_whatsapp_chat_history(+ node_id: str,+ node_type: str,+ parameters: Dict[str, Any],+ context: Dict[str, Any]+) -> Dict[str, Any]:+ """Handle WhatsApp chat history - retrieve messages from history store.++ Messages are automatically captured from:+ - HistorySync event on first login (all past messages)+ - Real-time incoming messages++ Args:+ node_id: The node ID+ node_type: The node type (whatsappChatHistory)+ parameters: Resolved parameters including:+ - chatType: 'individual' or 'group'+ - phone: Phone number for individual chats+ - group_id: Group JID for group chats+ - groupFilter: 'all' or 'contact' for group message filtering+ - senderPhone: Filter by sender in groups+ - messageFilter: 'all' or 'text_only'+ - limit: Max messages to return+ - offset: Pagination offset+ context: Execution context++ Returns:+ Execution result dict with messages array+ """+ from routers.whatsapp import handle_whatsapp_chat_history as whatsapp_chat_history_handler+ start_time = time.time()++ try:+ chat_type = parameters.get('chatType', 'individual')++ # Build RPC params based on chat type+ rpc_params: Dict[str, Any] = {}++ if chat_type == 'individual':+ phone = parameters.get('phone')+ if not phone:+ raise ValueError("Phone number is required for individual chats")+ rpc_params['phone'] = phone+ else:+ group_id = parameters.get('group_id')+ if not group_id:+ raise ValueError("Group ID is required for group chats")+ rpc_params['group_id'] = group_id++ # Optional sender filter for groups+ group_filter = parameters.get('groupFilter', 'all')+ if group_filter == 'contact':+ sender_phone = parameters.get('senderPhone')+ if sender_phone:+ rpc_params['sender_phone'] = sender_phone++ # Message type filter+ message_filter = parameters.get('messageFilter', 'all')+ rpc_params['text_only'] = message_filter == 'text_only'++ # Pagination+ rpc_params['limit'] = parameters.get('limit', 50)+ rpc_params['offset'] = parameters.get('offset', 0)++ # Call WhatsApp Go RPC service+ data = await whatsapp_chat_history_handler(rpc_params)++ success = data.get('success', False)+ if not success:+ raise Exception(data.get('error', 'Failed to retrieve chat history'))++ messages = data.get('messages', [])+ total = data.get('total', 0)+ has_more = data.get('has_more', False)++ return {+ "success": True,+ "node_id": node_id,+ "node_type": "whatsappChatHistory",+ "result": {+ "messages": messages,+ "total": total,+ "has_more": has_more,+ "count": len(messages),+ "chat_type": chat_type,+ "timestamp": datetime.now().isoformat()+ },+ "execution_time": time.time() - start_time,+ "timestamp": datetime.now().isoformat()+ }++ except Exception as e:+ logger.error("WhatsApp chat history failed", node_id=node_id, error=str(e))+ return {+ "success": False,+ "node_id": node_id,+ "node_type": "whatsappChatHistory",+ "error": str(e),+ "execution_time": time.time() - start_time,+ "timestamp": datetime.now().isoformat()+ }diff --git a/server/services/node_executor.py b/server/services/node_executor.pyindex e69d773f..8f2ce475 100644--- a/server/services/node_executor.py+++ b/server/services/node_executor.py@@ -31,7 +31,7 @@ from services.handlers import (handle_text_generator, handle_file_handler,handle_chat_send, handle_chat_history,handle_start, handle_cron_scheduler, handle_timer,- handle_whatsapp_send, handle_whatsapp_connect,+ handle_whatsapp_send, handle_whatsapp_connect, handle_whatsapp_chat_history,)if TYPE_CHECKING:@@ -115,6 +115,7 @@ class NodeExecutor:'whatsappSend': handle_whatsapp_send,'whatsappConnect': handle_whatsapp_connect,+ 'whatsappChatHistory': handle_whatsapp_chat_history,# Chat'chatSend': handle_chat_send,'chatHistory': handle_chat_history,diff --git a/server/whatsapp-rpc/schema.json b/server/whatsapp-rpc/schema.jsonindex 6c09133b..91924be2 100644--- a/server/whatsapp-rpc/schema.json+++ b/server/whatsapp-rpc/schema.json@@ -649,10 +649,97 @@}}}+ },+ {+ "name": "chat_history",+ "summary": "Get chat history from stored messages",+ "description": "Retrieve messages from SQLite history store. Messages are synced on first WhatsApp login and stored for later retrieval. Supports filtering by chat type (individual/group), sender, and message type.",+ "params": [+ {+ "name": "chat_id",+ "description": "Chat JID (e.g., [email protected] for individual, [email protected] for group)",+ "schema": { "type": "string" },+ "required": false+ },+ {+ "name": "phone",+ "description": "Phone number for individual chats (without +). Converted to JID automatically.",+ "schema": { "type": "string" },+ "required": false+ },+ {+ "name": "group_id",+ "description": "Group JID (e.g., [email protected])",+ "schema": { "type": "string" },+ "required": false+ },+ {+ "name": "limit",+ "description": "Maximum messages to return (default: 50, max: 500)",+ "schema": { "type": "integer", "default": 50, "minimum": 1, "maximum": 500 },+ "required": false+ },+ {+ "name": "offset",+ "description": "Number of messages to skip for pagination (default: 0)",+ "schema": { "type": "integer", "default": 0 },+ "required": false+ },+ {+ "name": "sender_phone",+ "description": "Filter group messages by sender phone number",+ "schema": { "type": "string" },+ "required": false+ },+ {+ "name": "text_only",+ "description": "Only return text messages (default: false)",+ "schema": { "type": "boolean", "default": false },+ "required": false+ }+ ],+ "result": {+ "name": "ChatHistoryResult",+ "schema": { "$ref": "#/components/schemas/ChatHistoryResult" }+ }}],"components": {"schemas": {+ "ChatHistoryResult": {+ "type": "object",+ "description": "Chat history query result",+ "properties": {+ "messages": {+ "type": "array",+ "description": "Array of message records",+ "items": { "$ref": "#/components/schemas/MessageRecord" }+ },+ "total": { "type": "integer", "description": "Total matching messages in database" },+ "has_more": { "type": "boolean", "description": "Whether more messages exist beyond limit+offset" }+ },+ "required": ["messages", "total", "has_more"]+ },+ "MessageRecord": {+ "type": "object",+ "description": "Stored message record from history",+ "properties": {+ "message_id": { "type": "string", "description": "Unique message ID" },+ "chat_id": { "type": "string", "description": "Chat JID (individual or group)" },+ "sender": { "type": "string", "description": "Sender JID" },+ "sender_phone": { "type": "string", "description": "Sender phone number" },+ "message_type": {+ "type": "string",+ "enum": ["text", "image", "video", "audio", "document", "sticker", "location", "contact"],+ "description": "Type of message"+ },+ "text": { "type": "string", "description": "Text content (or caption for media)" },+ "timestamp": { "type": "string", "format": "date-time", "description": "Message timestamp" },+ "is_group": { "type": "boolean", "description": "Whether message is from a group chat" },+ "is_from_me": { "type": "boolean", "description": "Whether message was sent by the connected device" }+ },+ "required": ["message_id", "chat_id", "sender", "message_type", "timestamp", "is_group", "is_from_me"]+ },"MediaData": {"type": "object","description": "Media content for sending",@@ -960,6 +1047,13 @@"is_reply": { "type": "boolean" },"quoted_message_id": { "type": "string" }}+ },+ "event.history_sync_complete": {+ "description": "History sync completed after first login",+ "params": {+ "conversations": { "type": "integer", "description": "Number of conversations synced" },+ "messages": { "type": "integer", "description": "Total messages stored" }+ }}}}diff --git a/server/whatsapp-rpc/src/go/rpc/rpc.go b/server/whatsapp-rpc/src/go/rpc/rpc.goindex fc6e595a..387ff91b 100644--- a/server/whatsapp-rpc/src/go/rpc/rpc.go+++ b/server/whatsapp-rpc/src/go/rpc/rpc.go@@ -304,6 +304,48 @@ func (h *RPCHandler) HandleRequest(req *RPCRequest) RPCResponse {resp.Result = result}+ case "chat_history":+ var p struct {+ ChatID string `json:"chat_id"`+ Phone string `json:"phone"`+ GroupID string `json:"group_id"`+ Limit int `json:"limit"`+ Offset int `json:"offset"`+ SenderPhone string `json:"sender_phone"`+ TextOnly bool `json:"text_only"`+ }+ if err := json.Unmarshal(req.Params, &p); err != nil {+ resp.Error = &RPCError{Code: -32602, Message: "Invalid params: " + err.Error()}+ } else {+ // Resolve chat_id from phone or group_id if not directly provided+ chatID := p.ChatID+ if chatID == "" {+ if p.Phone != "" {+ chatID = p.Phone + "@s.whatsapp.net"+ } else if p.GroupID != "" {+ chatID = p.GroupID+ } else {+ resp.Error = &RPCError{Code: -32602, Message: "Either chat_id, phone, or group_id is required"}+ break+ }+ }++ // Set defaults+ limit := p.Limit+ if limit <= 0 {+ limit = 50+ }+ if limit > 500 {+ limit = 500+ }++ if result, err := h.service.GetChatHistory(chatID, limit, p.Offset, p.SenderPhone, p.TextOnly); err != nil {+ resp.Error = &RPCError{Code: -32000, Message: err.Error()}+ } else {+ resp.Result = result+ }+ }+case "rate_limit_get":resp.Result = map[string]interface{}{"config": h.service.GetRateLimitConfig(),diff --git a/server/whatsapp-rpc/src/go/whatsapp/history.go b/server/whatsapp-rpc/src/go/whatsapp/history.gonew file mode 100644index 00000000..19eae8db--- /dev/null+++ b/server/whatsapp-rpc/src/go/whatsapp/history.go@@ -0,0 +1,160 @@+package whatsapp++import (+ "database/sql"+ "time"++ "github.com/sirupsen/logrus"+ _ "modernc.org/sqlite"+)++// HistoryStore handles message persistence for chat history+type HistoryStore struct {+ db *sql.DB+ logger *logrus.Logger+}++// MessageRecord represents a stored message+type MessageRecord struct {+ MessageID string `json:"message_id"`+ ChatID string `json:"chat_id"`+ Sender string `json:"sender"`+ SenderPhone string `json:"sender_phone"`+ MessageType string `json:"message_type"`+ Text string `json:"text"`+ Timestamp time.Time `json:"timestamp"`+ IsGroup bool `json:"is_group"`+ IsFromMe bool `json:"is_from_me"`+}++// ChatHistoryResult is the response for chat_history queries+type ChatHistoryResult struct {+ Messages []MessageRecord `json:"messages"`+ Total int `json:"total"`+ HasMore bool `json:"has_more"`+}++// NewHistoryStore creates a new history store with SQLite persistence+func NewHistoryStore(dbPath string, logger *logrus.Logger) (*HistoryStore, error) {+ // Use separate file for history to avoid conflicts with whatsmeow's session store+ historyPath := dbPath + "_history"+ db, err := sql.Open("sqlite", "file:"+historyPath+"?_journal_mode=WAL&_busy_timeout=5000")+ if err != nil {+ return nil, err+ }++ store := &HistoryStore{db: db, logger: logger}+ if err := store.initTable(); err != nil {+ return nil, err+ }+ return store, nil+}++func (h *HistoryStore) initTable() error {+ _, err := h.db.Exec(`+ CREATE TABLE IF NOT EXISTS message_history (+ message_id TEXT PRIMARY KEY,+ chat_id TEXT NOT NULL,+ sender TEXT NOT NULL,+ sender_phone TEXT,+ message_type TEXT NOT NULL,+ text TEXT,+ timestamp INTEGER NOT NULL,+ is_group INTEGER NOT NULL DEFAULT 0,+ is_from_me INTEGER NOT NULL DEFAULT 0,+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))+ );+ CREATE INDEX IF NOT EXISTS idx_chat_id ON message_history(chat_id);+ CREATE INDEX IF NOT EXISTS idx_timestamp ON message_history(timestamp DESC);+ CREATE INDEX IF NOT EXISTS idx_sender_phone ON message_history(sender_phone);+ `)+ return err+}++// StoreMessage persists a message to the history store+func (h *HistoryStore) StoreMessage(msg MessageRecord) error {+ _, err := h.db.Exec(`+ INSERT OR IGNORE INTO message_history+ (message_id, chat_id, sender, sender_phone, message_type, text, timestamp, is_group, is_from_me)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)+ `, msg.MessageID, msg.ChatID, msg.Sender, msg.SenderPhone, msg.MessageType,+ msg.Text, msg.Timestamp.Unix(), boolToInt(msg.IsGroup), boolToInt(msg.IsFromMe))+ return err+}++// GetChatHistory retrieves messages for a chat with optional filters+func (h *HistoryStore) GetChatHistory(chatID string, limit, offset int, senderPhone string, textOnly bool) (*ChatHistoryResult, error) {+ // Build query with filters+ query := `SELECT message_id, chat_id, sender, sender_phone, message_type, text, timestamp, is_group, is_from_me+ FROM message_history WHERE chat_id = ?`+ args := []interface{}{chatID}++ if senderPhone != "" {+ query += " AND sender_phone = ?"+ args = append(args, senderPhone)+ }+ if textOnly {+ query += " AND message_type = 'text'"+ }++ // Count total matching+ countQuery := "SELECT COUNT(*) FROM message_history WHERE chat_id = ?"+ countArgs := []interface{}{chatID}+ if senderPhone != "" {+ countQuery += " AND sender_phone = ?"+ countArgs = append(countArgs, senderPhone)+ }+ if textOnly {+ countQuery += " AND message_type = 'text'"+ }++ var total int+ h.db.QueryRow(countQuery, countArgs...).Scan(&total)++ // Get messages ordered by timestamp descending (newest first)+ query += " ORDER BY timestamp DESC LIMIT ? OFFSET ?"+ args = append(args, limit, offset)++ rows, err := h.db.Query(query, args...)+ if err != nil {+ return nil, err+ }+ defer rows.Close()++ messages := []MessageRecord{}+ for rows.Next() {+ var msg MessageRecord+ var ts int64+ var isGroup, isFromMe int+ err := rows.Scan(&msg.MessageID, &msg.ChatID, &msg.Sender, &msg.SenderPhone,+ &msg.MessageType, &msg.Text, &ts, &isGroup, &isFromMe)+ if err != nil {+ continue+ }+ msg.Timestamp = time.Unix(ts, 0)+ msg.IsGroup = isGroup == 1+ msg.IsFromMe = isFromMe == 1+ messages = append(messages, msg)+ }++ return &ChatHistoryResult{+ Messages: messages,+ Total: total,+ HasMore: offset+len(messages) < total,+ }, nil+}++// Close closes the database connection+func (h *HistoryStore) Close() error {+ if h.db != nil {+ return h.db.Close()+ }+ return nil+}++func boolToInt(b bool) int {+ if b {+ return 1+ }+ return 0+}diff --git a/server/whatsapp-rpc/src/go/whatsapp/service.go b/server/whatsapp-rpc/src/go/whatsapp/service.goindex ebf3e77f..37c09982 100644--- a/server/whatsapp-rpc/src/go/whatsapp/service.go+++ b/server/whatsapp-rpc/src/go/whatsapp/service.go@@ -39,6 +39,7 @@ type Service struct {lastQRCodeTime time.Timemessages map[string]*events.Message // Store messages by ID for media downloadmessageOrder []string // Track message insertion order for FIFO cleanup+ historyStore *HistoryStore // Persistent message history store// Rate limiting for anti-ban protectionrateMu sync.Mutex@@ -101,7 +102,16 @@ func NewService(dbConfig config.DatabaseConfig, logger *logrus.Logger) (*ServicenewContactsToday: make(map[string]time.Time),dailyResetTime: time.Now(),}-++ // Initialize history store for message persistence+ historyStore, err := NewHistoryStore(dbConfig.Path, logger)+ if err != nil {+ logger.Warnf("Failed to init history store (history disabled): %v", err)+ } else {+ service.historyStore = historyStore+ logger.Info("History store initialized")+ }+// Reset any previous connection state on service creationif client.IsConnected() {client.Disconnect()@@ -346,6 +356,8 @@ func (s *Service) eventHandler(evt interface{}) {},Time: time.Now(),})+ case *events.HistorySync:+ s.handleHistorySync(v)case *events.Message:s.handleIncomingMessage(v)default:@@ -624,6 +636,30 @@ func (s *Service) handleIncomingMessage(v *events.Message) {// Log the messages.logger.Infof("Received %s message from %s", messageType, sender)+ // Persist ALL messages to history store for future retrieval+ if s.historyStore != nil {+ textContent := ""+ if t, ok := eventData["text"].(string); ok {+ textContent = t+ }+ record := MessageRecord{+ MessageID: messageID,+ ChatID: chatID,+ Sender: sender,+ SenderPhone: senderPhone,+ MessageType: messageType,+ Text: textContent,+ Timestamp: timestamp,+ IsGroup: isGroup,+ IsFromMe: v.Info.IsFromMe,+ }+ go func() {+ if err := s.historyStore.StoreMessage(record); err != nil {+ s.logger.Debugf("Failed to store message: %v", err)+ }+ }()+ }+// Broadcast events.safeEventSend(Event{Type: "message_received",@@ -632,6 +668,140 @@ func (s *Service) handleIncomingMessage(v *events.Message) {})}+// handleHistorySync processes history sync events received on first login+// This contains all past conversations and messages from WhatsApp+func (s *Service) handleHistorySync(evt *events.HistorySync) {+ if s.historyStore == nil {+ s.logger.Warn("History sync received but history store not initialized")+ return+ }++ historyData := evt.Data+ if historyData == nil {+ return+ }++ conversations := historyData.GetConversations()+ s.logger.Infof("Processing history sync: %d conversations", len(conversations))++ totalMessages := 0+ for _, conv := range conversations {+ chatJID, err := types.ParseJID(conv.GetID())+ if err != nil {+ s.logger.Warnf("Invalid chat JID in history: %s", conv.GetID())+ continue+ }++ isGroup := chatJID.Server == "g.us"+ chatID := chatJID.String()++ for _, historyMsg := range conv.GetMessages() {+ webMsg := historyMsg.GetMessage()+ if webMsg == nil {+ continue+ }++ // Parse the web message to get standard message format+ parsedMsg, err := s.client.ParseWebMessage(chatJID, webMsg)+ if err != nil {+ continue+ }++ // Extract message info+ msgInfo := parsedMsg.Info+ messageID := msgInfo.ID+ sender := msgInfo.Sender.String()+ senderPhone := msgInfo.Sender.User+ timestamp := msgInfo.Timestamp++ // Determine message type and extract text content+ messageType := "unknown"+ text := ""++ msg := parsedMsg.Message+ if msg == nil {+ // Skip messages with nil content (protocol messages, etc.)+ continue+ }++ switch {+ case msg.Conversation != nil && *msg.Conversation != "":+ messageType = "text"+ text = *msg.Conversation+ case msg.ExtendedTextMessage != nil && msg.ExtendedTextMessage.Text != nil:+ messageType = "text"+ text = *msg.ExtendedTextMessage.Text+ case msg.ImageMessage != nil:+ messageType = "image"+ if msg.ImageMessage.Caption != nil {+ text = *msg.ImageMessage.Caption+ }+ case msg.VideoMessage != nil:+ messageType = "video"+ if msg.VideoMessage.Caption != nil {+ text = *msg.VideoMessage.Caption+ }+ case msg.AudioMessage != nil:+ messageType = "audio"+ case msg.DocumentMessage != nil:+ messageType = "document"+ if msg.DocumentMessage.Caption != nil {+ text = *msg.DocumentMessage.Caption+ }+ case msg.StickerMessage != nil:+ messageType = "sticker"+ case msg.LocationMessage != nil:+ messageType = "location"+ case msg.ContactMessage != nil:+ messageType = "contact"+ default:+ // Skip unknown/protocol messages+ continue+ }++ // Store ALL message types+ record := MessageRecord{+ MessageID: messageID,+ ChatID: chatID,+ Sender: sender,+ SenderPhone: senderPhone,+ MessageType: messageType,+ Text: text,+ Timestamp: timestamp,+ IsGroup: isGroup,+ IsFromMe: msgInfo.IsFromMe,+ }++ if err := s.historyStore.StoreMessage(record); err != nil {+ s.logger.Debugf("Failed to store history message: %v", err)+ } else {+ totalMessages+++ }+ }+ }++ s.logger.Infof("History sync complete: stored %d messages from %d conversations",+ totalMessages, len(conversations))++ // Broadcast event for frontend notification+ s.safeEventSend(Event{+ Type: "history_sync_complete",+ Data: map[string]interface{}{+ "conversations": len(conversations),+ "messages": totalMessages,+ },+ Time: time.Now(),+ })+}++// GetChatHistory retrieves stored messages for a chat+func (s *Service) GetChatHistory(chatID string, limit, offset int, senderPhone string, textOnly bool) (*ChatHistoryResult, error) {+ if s.historyStore == nil {+ return nil, fmt.Errorf("history store not initialized")+ }+ return s.historyStore.GetChatHistory(chatID, limit, offset, senderPhone, textOnly)+}+func (s *Service) GetStatus() map[string]interface{} {status := map[string]interface{}{"connected": s.client.IsConnected(),@@ -870,6 +1040,7 @@ func (s *Service) GetGroups() ([]GroupInfo, error) {Size: len(g.Participants),IsAnnounce: g.IsAnnounce,IsLocked: g.IsLocked,+ IsCommunity: g.IsParent,})}@@ -916,6 +1087,7 @@ func (s *Service) GetGroupInfo(groupJID string) (*GroupInfo, error) {Size: len(info.Participants),IsAnnounce: info.IsAnnounce,IsLocked: info.IsLocked,+ IsCommunity: info.IsParent,}s.logger.Infof("Retrieved info for group %s (%s)", info.Name, groupJID)diff --git a/server/whatsapp-rpc/src/go/whatsapp/types.go b/server/whatsapp-rpc/src/go/whatsapp/types.goindex d05dd67f..d73410ed 100644--- a/server/whatsapp-rpc/src/go/whatsapp/types.go+++ b/server/whatsapp-rpc/src/go/whatsapp/types.go@@ -82,8 +82,9 @@ type GroupInfo struct {Participants []GroupParticipant `json:"participants"`CreatedAt time.Time `json:"created_at"`Size int `json:"size"`- IsAnnounce bool `json:"is_announce"` // Only admins can send messages- IsLocked bool `json:"is_locked"` // Only admins can edit group info+ IsAnnounce bool `json:"is_announce"` // Only admins can send messages+ IsLocked bool `json:"is_locked"` // Only admins can edit group info+ IsCommunity bool `json:"is_community"` // True if this is a community (parent group)}// GroupUpdateRequest represents parameters for updating a group