machinaos 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.template +71 -0
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/bin/cli.js +159 -0
- package/client/.dockerignore +45 -0
- package/client/Dockerfile +68 -0
- package/client/eslint.config.js +29 -0
- package/client/index.html +13 -0
- package/client/nginx.conf +66 -0
- package/client/package.json +48 -0
- package/client/src/App.tsx +27 -0
- package/client/src/Dashboard.tsx +1173 -0
- package/client/src/ParameterPanel.tsx +301 -0
- package/client/src/components/AIAgentNode.tsx +321 -0
- package/client/src/components/APIKeyValidator.tsx +118 -0
- package/client/src/components/ClaudeChatModelNode.tsx +18 -0
- package/client/src/components/ConditionalEdge.tsx +189 -0
- package/client/src/components/CredentialsModal.tsx +306 -0
- package/client/src/components/EdgeConditionEditor.tsx +443 -0
- package/client/src/components/GeminiChatModelNode.tsx +18 -0
- package/client/src/components/GenericNode.tsx +357 -0
- package/client/src/components/LocationParameterPanel.tsx +154 -0
- package/client/src/components/ModelNode.tsx +286 -0
- package/client/src/components/OpenAIChatModelNode.tsx +18 -0
- package/client/src/components/OutputPanel.tsx +471 -0
- package/client/src/components/ParameterRenderer.tsx +1874 -0
- package/client/src/components/SkillEditorModal.tsx +417 -0
- package/client/src/components/SquareNode.tsx +797 -0
- package/client/src/components/StartNode.tsx +250 -0
- package/client/src/components/ToolkitNode.tsx +365 -0
- package/client/src/components/TriggerNode.tsx +463 -0
- package/client/src/components/auth/LoginPage.tsx +247 -0
- package/client/src/components/auth/ProtectedRoute.tsx +59 -0
- package/client/src/components/base/BaseChatModelNode.tsx +271 -0
- package/client/src/components/icons/AIProviderIcons.tsx +50 -0
- package/client/src/components/maps/GoogleMapsPicker.tsx +137 -0
- package/client/src/components/maps/MapsPreviewPanel.tsx +110 -0
- package/client/src/components/maps/index.ts +26 -0
- package/client/src/components/parameterPanel/InputSection.tsx +1094 -0
- package/client/src/components/parameterPanel/LocationPanelLayout.tsx +65 -0
- package/client/src/components/parameterPanel/MapsSection.tsx +92 -0
- package/client/src/components/parameterPanel/MiddleSection.tsx +571 -0
- package/client/src/components/parameterPanel/OutputSection.tsx +81 -0
- package/client/src/components/parameterPanel/ParameterPanelLayout.tsx +82 -0
- package/client/src/components/parameterPanel/ToolSchemaEditor.tsx +436 -0
- package/client/src/components/parameterPanel/index.ts +42 -0
- package/client/src/components/shared/DataPanel.tsx +142 -0
- package/client/src/components/shared/JSONTreeRenderer.tsx +106 -0
- package/client/src/components/ui/AIResultModal.tsx +204 -0
- package/client/src/components/ui/AndroidSettingsPanel.tsx +401 -0
- package/client/src/components/ui/CodeEditor.tsx +81 -0
- package/client/src/components/ui/CollapsibleSection.tsx +88 -0
- package/client/src/components/ui/ComponentItem.tsx +154 -0
- package/client/src/components/ui/ComponentPalette.tsx +321 -0
- package/client/src/components/ui/ConsolePanel.tsx +1074 -0
- package/client/src/components/ui/ErrorBoundary.tsx +196 -0
- package/client/src/components/ui/InputNodesPanel.tsx +204 -0
- package/client/src/components/ui/MapSelector.tsx +314 -0
- package/client/src/components/ui/Modal.tsx +149 -0
- package/client/src/components/ui/NodeContextMenu.tsx +192 -0
- package/client/src/components/ui/NodeOutputPanel.tsx +1150 -0
- package/client/src/components/ui/OutputDisplayPanel.tsx +381 -0
- package/client/src/components/ui/SettingsPanel.tsx +243 -0
- package/client/src/components/ui/TopToolbar.tsx +736 -0
- package/client/src/components/ui/WhatsAppSettingsPanel.tsx +345 -0
- package/client/src/components/ui/WorkflowSidebar.tsx +294 -0
- package/client/src/config/antdTheme.ts +186 -0
- package/client/src/config/api.ts +54 -0
- package/client/src/contexts/AuthContext.tsx +221 -0
- package/client/src/contexts/ThemeContext.tsx +42 -0
- package/client/src/contexts/WebSocketContext.tsx +1971 -0
- package/client/src/factories/baseChatModelFactory.ts +256 -0
- package/client/src/hooks/useAndroidOperations.ts +164 -0
- package/client/src/hooks/useApiKeyValidation.ts +107 -0
- package/client/src/hooks/useApiKeys.ts +238 -0
- package/client/src/hooks/useAppTheme.ts +17 -0
- package/client/src/hooks/useComponentPalette.ts +51 -0
- package/client/src/hooks/useCopyPaste.ts +155 -0
- package/client/src/hooks/useDragAndDrop.ts +124 -0
- package/client/src/hooks/useDragVariable.ts +88 -0
- package/client/src/hooks/useExecution.ts +313 -0
- package/client/src/hooks/useParameterPanel.ts +176 -0
- package/client/src/hooks/useReactFlowNodes.ts +189 -0
- package/client/src/hooks/useToolSchema.ts +209 -0
- package/client/src/hooks/useWhatsApp.ts +196 -0
- package/client/src/hooks/useWorkflowManagement.ts +46 -0
- package/client/src/index.css +315 -0
- package/client/src/main.tsx +19 -0
- package/client/src/nodeDefinitions/aiAgentNodes.ts +336 -0
- package/client/src/nodeDefinitions/aiModelNodes.ts +340 -0
- package/client/src/nodeDefinitions/androidDeviceNodes.ts +140 -0
- package/client/src/nodeDefinitions/androidServiceNodes.ts +383 -0
- package/client/src/nodeDefinitions/chatNodes.ts +135 -0
- package/client/src/nodeDefinitions/codeNodes.ts +54 -0
- package/client/src/nodeDefinitions/documentNodes.ts +379 -0
- package/client/src/nodeDefinitions/index.ts +15 -0
- package/client/src/nodeDefinitions/locationNodes.ts +463 -0
- package/client/src/nodeDefinitions/schedulerNodes.ts +220 -0
- package/client/src/nodeDefinitions/skillNodes.ts +211 -0
- package/client/src/nodeDefinitions/toolNodes.ts +198 -0
- package/client/src/nodeDefinitions/utilityNodes.ts +284 -0
- package/client/src/nodeDefinitions/whatsappNodes.ts +865 -0
- package/client/src/nodeDefinitions/workflowNodes.ts +41 -0
- package/client/src/nodeDefinitions.ts +104 -0
- package/client/src/schemas/workflowSchema.ts +264 -0
- package/client/src/services/dynamicParameterService.ts +96 -0
- package/client/src/services/execution/aiAgentExecutionService.ts +35 -0
- package/client/src/services/executionService.ts +232 -0
- package/client/src/services/workflowApi.ts +91 -0
- package/client/src/store/useAppStore.ts +582 -0
- package/client/src/styles/theme.ts +508 -0
- package/client/src/styles/zIndex.ts +17 -0
- package/client/src/types/ComponentTypes.ts +39 -0
- package/client/src/types/EdgeCondition.ts +231 -0
- package/client/src/types/INodeProperties.ts +288 -0
- package/client/src/types/NodeTypes.ts +28 -0
- package/client/src/utils/formatters.ts +33 -0
- package/client/src/utils/googleMapsLoader.ts +140 -0
- package/client/src/utils/locationUtils.ts +85 -0
- package/client/src/utils/nodeUtils.ts +31 -0
- package/client/src/utils/workflow.ts +30 -0
- package/client/src/utils/workflowExport.ts +120 -0
- package/client/src/vite-env.d.ts +12 -0
- package/client/tailwind.config.js +60 -0
- package/client/tsconfig.json +25 -0
- package/client/tsconfig.node.json +11 -0
- package/client/vite.config.js +35 -0
- package/docker-compose.prod.yml +107 -0
- package/docker-compose.yml +104 -0
- package/docs-MachinaOs/README.md +85 -0
- package/docs-MachinaOs/deployment/docker.mdx +228 -0
- package/docs-MachinaOs/deployment/production.mdx +345 -0
- package/docs-MachinaOs/docs.json +75 -0
- package/docs-MachinaOs/faq.mdx +309 -0
- package/docs-MachinaOs/favicon.svg +5 -0
- package/docs-MachinaOs/installation.mdx +160 -0
- package/docs-MachinaOs/introduction.mdx +114 -0
- package/docs-MachinaOs/logo/dark.svg +6 -0
- package/docs-MachinaOs/logo/light.svg +6 -0
- package/docs-MachinaOs/nodes/ai-agent.mdx +216 -0
- package/docs-MachinaOs/nodes/ai-models.mdx +240 -0
- package/docs-MachinaOs/nodes/android.mdx +411 -0
- package/docs-MachinaOs/nodes/overview.mdx +181 -0
- package/docs-MachinaOs/nodes/schedulers.mdx +316 -0
- package/docs-MachinaOs/nodes/webhooks.mdx +330 -0
- package/docs-MachinaOs/nodes/whatsapp.mdx +305 -0
- package/docs-MachinaOs/quickstart.mdx +119 -0
- package/docs-MachinaOs/tutorials/ai-agent-workflow.mdx +177 -0
- package/docs-MachinaOs/tutorials/android-automation.mdx +242 -0
- package/docs-MachinaOs/tutorials/first-workflow.mdx +134 -0
- package/docs-MachinaOs/tutorials/whatsapp-automation.mdx +185 -0
- package/nul +0 -0
- package/package.json +70 -0
- package/scripts/build.js +158 -0
- package/scripts/check-ports.ps1 +33 -0
- package/scripts/clean.js +40 -0
- package/scripts/docker.js +93 -0
- package/scripts/kill-port.ps1 +154 -0
- package/scripts/start.js +210 -0
- package/scripts/stop.js +325 -0
- package/server/.dockerignore +44 -0
- package/server/Dockerfile +45 -0
- package/server/constants.py +249 -0
- package/server/core/__init__.py +1 -0
- package/server/core/cache.py +461 -0
- package/server/core/config.py +128 -0
- package/server/core/container.py +99 -0
- package/server/core/database.py +1211 -0
- package/server/core/logging.py +314 -0
- package/server/main.py +289 -0
- package/server/middleware/__init__.py +5 -0
- package/server/middleware/auth.py +89 -0
- package/server/models/__init__.py +1 -0
- package/server/models/auth.py +52 -0
- package/server/models/cache.py +24 -0
- package/server/models/database.py +211 -0
- package/server/models/nodes.py +455 -0
- package/server/package.json +9 -0
- package/server/pyproject.toml +72 -0
- package/server/requirements.txt +83 -0
- package/server/routers/__init__.py +1 -0
- package/server/routers/android.py +294 -0
- package/server/routers/auth.py +203 -0
- package/server/routers/database.py +151 -0
- package/server/routers/maps.py +142 -0
- package/server/routers/nodejs_compat.py +289 -0
- package/server/routers/webhook.py +90 -0
- package/server/routers/websocket.py +2127 -0
- package/server/routers/whatsapp.py +761 -0
- package/server/routers/workflow.py +200 -0
- package/server/services/__init__.py +1 -0
- package/server/services/ai.py +2415 -0
- package/server/services/android/__init__.py +27 -0
- package/server/services/android/broadcaster.py +114 -0
- package/server/services/android/client.py +608 -0
- package/server/services/android/manager.py +78 -0
- package/server/services/android/protocol.py +165 -0
- package/server/services/android_service.py +588 -0
- package/server/services/auth.py +131 -0
- package/server/services/chat_client.py +160 -0
- package/server/services/deployment/__init__.py +12 -0
- package/server/services/deployment/manager.py +706 -0
- package/server/services/deployment/state.py +47 -0
- package/server/services/deployment/triggers.py +275 -0
- package/server/services/event_waiter.py +785 -0
- package/server/services/execution/__init__.py +77 -0
- package/server/services/execution/cache.py +769 -0
- package/server/services/execution/conditions.py +373 -0
- package/server/services/execution/dlq.py +132 -0
- package/server/services/execution/executor.py +1351 -0
- package/server/services/execution/models.py +531 -0
- package/server/services/execution/recovery.py +235 -0
- package/server/services/handlers/__init__.py +126 -0
- package/server/services/handlers/ai.py +355 -0
- package/server/services/handlers/android.py +260 -0
- package/server/services/handlers/code.py +278 -0
- package/server/services/handlers/document.py +598 -0
- package/server/services/handlers/http.py +193 -0
- package/server/services/handlers/polyglot.py +105 -0
- package/server/services/handlers/tools.py +845 -0
- package/server/services/handlers/triggers.py +107 -0
- package/server/services/handlers/utility.py +822 -0
- package/server/services/handlers/whatsapp.py +476 -0
- package/server/services/maps.py +289 -0
- package/server/services/memory_store.py +103 -0
- package/server/services/node_executor.py +375 -0
- package/server/services/parameter_resolver.py +218 -0
- package/server/services/polyglot_client.py +169 -0
- package/server/services/scheduler.py +155 -0
- package/server/services/skill_loader.py +417 -0
- package/server/services/status_broadcaster.py +826 -0
- package/server/services/temporal/__init__.py +23 -0
- package/server/services/temporal/activities.py +344 -0
- package/server/services/temporal/client.py +76 -0
- package/server/services/temporal/executor.py +147 -0
- package/server/services/temporal/worker.py +251 -0
- package/server/services/temporal/workflow.py +355 -0
- package/server/services/temporal/ws_client.py +236 -0
- package/server/services/text.py +111 -0
- package/server/services/user_auth.py +172 -0
- package/server/services/websocket_client.py +29 -0
- package/server/services/workflow.py +597 -0
- package/server/skills/android-skill/SKILL.md +82 -0
- package/server/skills/assistant-personality/SKILL.md +45 -0
- package/server/skills/code-skill/SKILL.md +140 -0
- package/server/skills/http-skill/SKILL.md +161 -0
- package/server/skills/maps-skill/SKILL.md +170 -0
- package/server/skills/memory-skill/SKILL.md +154 -0
- package/server/skills/scheduler-skill/SKILL.md +84 -0
- package/server/skills/whatsapp-skill/SKILL.md +283 -0
- package/server/uv.lock +2916 -0
- package/server/whatsapp-rpc/.dockerignore +30 -0
- package/server/whatsapp-rpc/Dockerfile +44 -0
- package/server/whatsapp-rpc/Dockerfile.web +17 -0
- package/server/whatsapp-rpc/README.md +139 -0
- package/server/whatsapp-rpc/cli.js +95 -0
- package/server/whatsapp-rpc/configs/config.yaml +7 -0
- package/server/whatsapp-rpc/docker-compose.yml +35 -0
- package/server/whatsapp-rpc/docs/API.md +410 -0
- package/server/whatsapp-rpc/go.mod +67 -0
- package/server/whatsapp-rpc/go.sum +203 -0
- package/server/whatsapp-rpc/package.json +30 -0
- package/server/whatsapp-rpc/schema.json +1294 -0
- package/server/whatsapp-rpc/scripts/clean.cjs +66 -0
- package/server/whatsapp-rpc/scripts/cli.js +162 -0
- package/server/whatsapp-rpc/src/go/cmd/server/main.go +91 -0
- package/server/whatsapp-rpc/src/go/config/config.go +49 -0
- package/server/whatsapp-rpc/src/go/rpc/rpc.go +446 -0
- package/server/whatsapp-rpc/src/go/rpc/server.go +112 -0
- package/server/whatsapp-rpc/src/go/whatsapp/history.go +166 -0
- package/server/whatsapp-rpc/src/go/whatsapp/messages.go +390 -0
- package/server/whatsapp-rpc/src/go/whatsapp/service.go +2130 -0
- package/server/whatsapp-rpc/src/go/whatsapp/types.go +261 -0
- package/server/whatsapp-rpc/src/python/pyproject.toml +15 -0
- package/server/whatsapp-rpc/src/python/whatsapp_rpc/__init__.py +4 -0
- package/server/whatsapp-rpc/src/python/whatsapp_rpc/client.py +427 -0
- package/server/whatsapp-rpc/web/app.py +609 -0
- package/server/whatsapp-rpc/web/requirements.txt +6 -0
- package/server/whatsapp-rpc/web/rpc_client.py +427 -0
- package/server/whatsapp-rpc/web/static/openapi.yaml +59 -0
- package/server/whatsapp-rpc/web/templates/base.html +150 -0
- package/server/whatsapp-rpc/web/templates/contacts.html +240 -0
- package/server/whatsapp-rpc/web/templates/dashboard.html +320 -0
- package/server/whatsapp-rpc/web/templates/groups.html +328 -0
- package/server/whatsapp-rpc/web/templates/messages.html +465 -0
- package/server/whatsapp-rpc/web/templates/messaging.html +681 -0
- package/server/whatsapp-rpc/web/templates/send.html +259 -0
- package/server/whatsapp-rpc/web/templates/settings.html +459 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
"""WhatsApp node handlers - Send and Connect."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing import Dict, Any
|
|
6
|
+
from core.logging import get_logger
|
|
7
|
+
|
|
8
|
+
logger = get_logger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def handle_whatsapp_send(
|
|
12
|
+
node_id: str,
|
|
13
|
+
node_type: str,
|
|
14
|
+
parameters: Dict[str, Any],
|
|
15
|
+
context: Dict[str, Any]
|
|
16
|
+
) -> Dict[str, Any]:
|
|
17
|
+
"""Handle WhatsApp send message node via Go RPC service.
|
|
18
|
+
|
|
19
|
+
Supports all message types: text, image, video, audio, document, sticker, location, contact
|
|
20
|
+
Recipients: phone number or group_id
|
|
21
|
+
Media sources: base64, file path, or URL
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
node_id: The node ID
|
|
25
|
+
node_type: The node type (whatsappSend)
|
|
26
|
+
parameters: Resolved parameters
|
|
27
|
+
context: Execution context
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Execution result dict
|
|
31
|
+
"""
|
|
32
|
+
from routers.whatsapp import handle_whatsapp_send as whatsapp_send_handler
|
|
33
|
+
start_time = time.time()
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
# Determine recipient (snake_case parameters)
|
|
37
|
+
recipient_type = parameters.get('recipient_type', 'phone')
|
|
38
|
+
group_id = parameters.get('group_id')
|
|
39
|
+
recipient = parameters.get('phone') if recipient_type == 'phone' else group_id
|
|
40
|
+
message_type = parameters.get('message_type', 'text')
|
|
41
|
+
|
|
42
|
+
if not recipient:
|
|
43
|
+
raise ValueError(f"{'Phone number' if recipient_type == 'phone' else 'Group ID'} is required")
|
|
44
|
+
|
|
45
|
+
# For text messages, validate message content
|
|
46
|
+
if message_type == 'text' and not parameters.get('message'):
|
|
47
|
+
raise ValueError("Message content is required for text messages")
|
|
48
|
+
|
|
49
|
+
# Call WhatsApp Go RPC service via handler - pass full params
|
|
50
|
+
data = await whatsapp_send_handler(parameters)
|
|
51
|
+
|
|
52
|
+
success = data.get('success', False)
|
|
53
|
+
if not success:
|
|
54
|
+
raise Exception(data.get('error', 'Send failed'))
|
|
55
|
+
|
|
56
|
+
# Build informative result based on message type (snake_case output)
|
|
57
|
+
result = {
|
|
58
|
+
"status": "sent",
|
|
59
|
+
"recipient": recipient,
|
|
60
|
+
"recipient_type": recipient_type,
|
|
61
|
+
"message_type": message_type,
|
|
62
|
+
"timestamp": datetime.now().isoformat()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# Add type-specific details using match statement
|
|
66
|
+
match message_type:
|
|
67
|
+
case 'text':
|
|
68
|
+
msg_content = parameters.get('message', '')
|
|
69
|
+
result["preview"] = msg_content[:100] + "..." if len(msg_content) > 100 else msg_content
|
|
70
|
+
case 'image' | 'video' | 'audio' | 'document' | 'sticker':
|
|
71
|
+
media_source = parameters.get('media_source', 'base64')
|
|
72
|
+
result["media_source"] = media_source
|
|
73
|
+
if parameters.get('caption'):
|
|
74
|
+
result["caption"] = parameters.get('caption')
|
|
75
|
+
if parameters.get('filename'):
|
|
76
|
+
result["filename"] = parameters.get('filename')
|
|
77
|
+
if parameters.get('mime_type'):
|
|
78
|
+
result["mime_type"] = parameters.get('mime_type')
|
|
79
|
+
# For file uploads, include the uploaded filename
|
|
80
|
+
file_param = parameters.get('file_path')
|
|
81
|
+
if isinstance(file_param, dict) and file_param.get('type') == 'upload':
|
|
82
|
+
result["uploaded_file"] = file_param.get('filename')
|
|
83
|
+
result["mime_type"] = file_param.get('mimeType')
|
|
84
|
+
case 'location':
|
|
85
|
+
result["location"] = {
|
|
86
|
+
"latitude": parameters.get('latitude'),
|
|
87
|
+
"longitude": parameters.get('longitude'),
|
|
88
|
+
"name": parameters.get('location_name'),
|
|
89
|
+
"address": parameters.get('address')
|
|
90
|
+
}
|
|
91
|
+
case 'contact':
|
|
92
|
+
result["contact_name"] = parameters.get('contact_name')
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
"success": success,
|
|
96
|
+
"node_id": node_id,
|
|
97
|
+
"node_type": "whatsappSend",
|
|
98
|
+
"result": result,
|
|
99
|
+
"execution_time": time.time() - start_time,
|
|
100
|
+
"timestamp": datetime.now().isoformat()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
except Exception as e:
|
|
104
|
+
logger.error("WhatsApp send failed", node_id=node_id, error=str(e))
|
|
105
|
+
return {
|
|
106
|
+
"success": False,
|
|
107
|
+
"node_id": node_id,
|
|
108
|
+
"node_type": "whatsappSend",
|
|
109
|
+
"error": str(e),
|
|
110
|
+
"execution_time": time.time() - start_time,
|
|
111
|
+
"timestamp": datetime.now().isoformat()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
async def handle_whatsapp_connect(
|
|
116
|
+
node_id: str,
|
|
117
|
+
node_type: str,
|
|
118
|
+
parameters: Dict[str, Any],
|
|
119
|
+
context: Dict[str, Any]
|
|
120
|
+
) -> Dict[str, Any]:
|
|
121
|
+
"""Handle WhatsApp connect - check status via Go RPC service.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
node_id: The node ID
|
|
125
|
+
node_type: The node type (whatsappConnect)
|
|
126
|
+
parameters: Resolved parameters
|
|
127
|
+
context: Execution context
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
Execution result dict with connection status
|
|
131
|
+
"""
|
|
132
|
+
from routers.whatsapp import handle_whatsapp_status
|
|
133
|
+
start_time = time.time()
|
|
134
|
+
|
|
135
|
+
try:
|
|
136
|
+
# Call WhatsApp Go RPC service via handler
|
|
137
|
+
data = await handle_whatsapp_status()
|
|
138
|
+
|
|
139
|
+
result_data = data.get('data', {})
|
|
140
|
+
return {
|
|
141
|
+
"success": data.get('success', False),
|
|
142
|
+
"node_id": node_id,
|
|
143
|
+
"node_type": "whatsappConnect",
|
|
144
|
+
"result": {
|
|
145
|
+
"connected": result_data.get('connected', False),
|
|
146
|
+
"device_id": result_data.get('device_id'),
|
|
147
|
+
"has_session": result_data.get('has_session', False),
|
|
148
|
+
"running": result_data.get('running', False),
|
|
149
|
+
"status": "connected" if result_data.get('connected') else "disconnected",
|
|
150
|
+
"timestamp": datetime.now().isoformat()
|
|
151
|
+
},
|
|
152
|
+
"execution_time": time.time() - start_time,
|
|
153
|
+
"timestamp": datetime.now().isoformat()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
except Exception as e:
|
|
157
|
+
logger.error("WhatsApp connect failed", node_id=node_id, error=str(e))
|
|
158
|
+
return {
|
|
159
|
+
"success": False,
|
|
160
|
+
"node_id": node_id,
|
|
161
|
+
"node_type": "whatsappConnect",
|
|
162
|
+
"error": str(e),
|
|
163
|
+
"execution_time": time.time() - start_time,
|
|
164
|
+
"timestamp": datetime.now().isoformat()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
async def handle_whatsapp_db(
|
|
169
|
+
node_id: str,
|
|
170
|
+
node_type: str,
|
|
171
|
+
parameters: Dict[str, Any],
|
|
172
|
+
context: Dict[str, Any]
|
|
173
|
+
) -> Dict[str, Any]:
|
|
174
|
+
"""Handle WhatsApp DB node - query contacts, groups, messages.
|
|
175
|
+
|
|
176
|
+
Operations:
|
|
177
|
+
- chat_history: Retrieve messages from history store
|
|
178
|
+
- search_groups: Search groups by name
|
|
179
|
+
- get_group_info: Get group details with participant names
|
|
180
|
+
- get_contact_info: Get full contact info (name, phone, photo)
|
|
181
|
+
- list_contacts: List contacts with saved names
|
|
182
|
+
- check_contacts: Check WhatsApp registration
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
node_id: The node ID
|
|
186
|
+
node_type: The node type (whatsappDb)
|
|
187
|
+
parameters: Resolved parameters including operation and operation-specific params
|
|
188
|
+
context: Execution context
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
Execution result dict
|
|
192
|
+
"""
|
|
193
|
+
from routers.whatsapp import (
|
|
194
|
+
handle_whatsapp_chat_history as whatsapp_chat_history_handler,
|
|
195
|
+
whatsapp_rpc_call
|
|
196
|
+
)
|
|
197
|
+
start_time = time.time()
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
operation = parameters.get('operation', 'chat_history')
|
|
201
|
+
|
|
202
|
+
if operation == 'chat_history':
|
|
203
|
+
return await _handle_chat_history(node_id, parameters, start_time, whatsapp_chat_history_handler)
|
|
204
|
+
elif operation == 'search_groups':
|
|
205
|
+
return await _handle_search_groups(node_id, parameters, start_time, whatsapp_rpc_call)
|
|
206
|
+
elif operation == 'get_group_info':
|
|
207
|
+
return await _handle_get_group_info(node_id, parameters, start_time, whatsapp_rpc_call)
|
|
208
|
+
elif operation == 'get_contact_info':
|
|
209
|
+
return await _handle_get_contact_info(node_id, parameters, start_time, whatsapp_rpc_call)
|
|
210
|
+
elif operation == 'list_contacts':
|
|
211
|
+
return await _handle_list_contacts(node_id, parameters, start_time, whatsapp_rpc_call)
|
|
212
|
+
elif operation == 'check_contacts':
|
|
213
|
+
return await _handle_check_contacts(node_id, parameters, start_time, whatsapp_rpc_call)
|
|
214
|
+
else:
|
|
215
|
+
raise ValueError(f"Unknown operation: {operation}")
|
|
216
|
+
|
|
217
|
+
except Exception as e:
|
|
218
|
+
logger.error("WhatsApp DB failed", node_id=node_id, operation=parameters.get('operation'), error=str(e))
|
|
219
|
+
return {
|
|
220
|
+
"success": False,
|
|
221
|
+
"node_id": node_id,
|
|
222
|
+
"node_type": "whatsappDb",
|
|
223
|
+
"error": str(e),
|
|
224
|
+
"execution_time": time.time() - start_time,
|
|
225
|
+
"timestamp": datetime.now().isoformat()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
async def _handle_chat_history(node_id: str, parameters: Dict[str, Any], start_time: float, handler) -> Dict[str, Any]:
|
|
230
|
+
"""Handle chat_history operation."""
|
|
231
|
+
chat_type = parameters.get('chat_type', 'individual')
|
|
232
|
+
rpc_params: Dict[str, Any] = {}
|
|
233
|
+
|
|
234
|
+
if chat_type == 'individual':
|
|
235
|
+
phone = parameters.get('phone')
|
|
236
|
+
if not phone:
|
|
237
|
+
raise ValueError("Phone number is required for individual chats")
|
|
238
|
+
rpc_params['phone'] = phone
|
|
239
|
+
else:
|
|
240
|
+
group_id = parameters.get('group_id')
|
|
241
|
+
if not group_id:
|
|
242
|
+
raise ValueError("Group ID is required for group chats")
|
|
243
|
+
rpc_params['group_id'] = group_id
|
|
244
|
+
|
|
245
|
+
group_filter = parameters.get('group_filter', 'all')
|
|
246
|
+
if group_filter == 'contact':
|
|
247
|
+
sender_phone = parameters.get('sender_phone')
|
|
248
|
+
if sender_phone:
|
|
249
|
+
rpc_params['sender_phone'] = sender_phone
|
|
250
|
+
|
|
251
|
+
message_filter = parameters.get('message_filter', 'all')
|
|
252
|
+
rpc_params['text_only'] = message_filter == 'text_only'
|
|
253
|
+
rpc_params['limit'] = parameters.get('limit', 50)
|
|
254
|
+
rpc_params['offset'] = parameters.get('offset', 0)
|
|
255
|
+
|
|
256
|
+
data = await handler(rpc_params)
|
|
257
|
+
|
|
258
|
+
if not data.get('success', False):
|
|
259
|
+
raise Exception(data.get('error', 'Failed to retrieve chat history'))
|
|
260
|
+
|
|
261
|
+
messages = data.get('messages', [])
|
|
262
|
+
base_offset = rpc_params.get('offset', 0)
|
|
263
|
+
for i, msg in enumerate(messages):
|
|
264
|
+
msg['index'] = base_offset + i + 1
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
"success": True,
|
|
268
|
+
"node_id": node_id,
|
|
269
|
+
"node_type": "whatsappDb",
|
|
270
|
+
"result": {
|
|
271
|
+
"operation": "chat_history",
|
|
272
|
+
"messages": messages,
|
|
273
|
+
"total": data.get('total', 0),
|
|
274
|
+
"has_more": data.get('has_more', False),
|
|
275
|
+
"count": len(messages),
|
|
276
|
+
"chat_type": chat_type,
|
|
277
|
+
"timestamp": datetime.now().isoformat()
|
|
278
|
+
},
|
|
279
|
+
"execution_time": time.time() - start_time,
|
|
280
|
+
"timestamp": datetime.now().isoformat()
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
async def _handle_search_groups(node_id: str, parameters: Dict[str, Any], start_time: float, rpc_call) -> Dict[str, Any]:
|
|
285
|
+
"""Handle search_groups operation."""
|
|
286
|
+
query = parameters.get('query', '')
|
|
287
|
+
limit = parameters.get('limit', 20) # Default limit to prevent context overflow
|
|
288
|
+
data = await rpc_call('groups', {})
|
|
289
|
+
|
|
290
|
+
if not data.get('success', True):
|
|
291
|
+
raise Exception(data.get('error', 'Failed to get groups'))
|
|
292
|
+
|
|
293
|
+
groups = data if isinstance(data, list) else data.get('result', [])
|
|
294
|
+
|
|
295
|
+
# Filter by query if provided
|
|
296
|
+
if query:
|
|
297
|
+
query_lower = query.lower()
|
|
298
|
+
groups = [g for g in groups if query_lower in g.get('name', '').lower()]
|
|
299
|
+
|
|
300
|
+
total_found = len(groups)
|
|
301
|
+
|
|
302
|
+
# Apply limit to prevent context overflow (51 groups * ~4KB = 200KB+ tokens)
|
|
303
|
+
# Only return essential fields: jid and name
|
|
304
|
+
groups_limited = [
|
|
305
|
+
{"jid": g.get("jid", ""), "name": g.get("name", "")}
|
|
306
|
+
for g in groups[:limit]
|
|
307
|
+
]
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
"success": True,
|
|
311
|
+
"node_id": node_id,
|
|
312
|
+
"node_type": "whatsappDb",
|
|
313
|
+
"result": {
|
|
314
|
+
"operation": "search_groups",
|
|
315
|
+
"groups": groups_limited,
|
|
316
|
+
"total": total_found,
|
|
317
|
+
"returned": len(groups_limited),
|
|
318
|
+
"has_more": total_found > limit,
|
|
319
|
+
"query": query,
|
|
320
|
+
"hint": f"Showing {len(groups_limited)} of {total_found} groups. Use a more specific query or get_group_info for details." if total_found > limit else None,
|
|
321
|
+
"timestamp": datetime.now().isoformat()
|
|
322
|
+
},
|
|
323
|
+
"execution_time": time.time() - start_time,
|
|
324
|
+
"timestamp": datetime.now().isoformat()
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
async def _handle_get_group_info(node_id: str, parameters: Dict[str, Any], start_time: float, rpc_call) -> Dict[str, Any]:
|
|
329
|
+
"""Handle get_group_info operation."""
|
|
330
|
+
group_id = parameters.get('group_id_for_info') or parameters.get('group_id')
|
|
331
|
+
if not group_id:
|
|
332
|
+
raise ValueError("Group ID is required")
|
|
333
|
+
|
|
334
|
+
participant_limit = parameters.get('participant_limit', 50) # Limit participants to prevent overflow
|
|
335
|
+
|
|
336
|
+
data = await rpc_call('group_info', {'group_id': group_id})
|
|
337
|
+
|
|
338
|
+
if not data.get('success', True) if isinstance(data, dict) else True:
|
|
339
|
+
raise Exception(data.get('error', 'Failed to get group info') if isinstance(data, dict) else 'Failed')
|
|
340
|
+
|
|
341
|
+
result = data if not isinstance(data, dict) or 'result' not in data else data.get('result', data)
|
|
342
|
+
|
|
343
|
+
# Limit participants and return only essential fields
|
|
344
|
+
participants = result.get('participants', [])
|
|
345
|
+
total_participants = len(participants)
|
|
346
|
+
participants_limited = [
|
|
347
|
+
{"phone": p.get("phone", ""), "name": p.get("name", ""), "is_admin": p.get("is_admin", False)}
|
|
348
|
+
for p in participants[:participant_limit]
|
|
349
|
+
]
|
|
350
|
+
|
|
351
|
+
# Build limited result
|
|
352
|
+
limited_result = {
|
|
353
|
+
"name": result.get("name", ""),
|
|
354
|
+
"jid": result.get("jid", group_id),
|
|
355
|
+
"participants": participants_limited,
|
|
356
|
+
"total_participants": total_participants,
|
|
357
|
+
"participants_shown": len(participants_limited),
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if total_participants > participant_limit:
|
|
361
|
+
limited_result["hint"] = f"Showing {participant_limit} of {total_participants} participants."
|
|
362
|
+
|
|
363
|
+
return {
|
|
364
|
+
"success": True,
|
|
365
|
+
"node_id": node_id,
|
|
366
|
+
"node_type": "whatsappDb",
|
|
367
|
+
"result": {
|
|
368
|
+
"operation": "get_group_info",
|
|
369
|
+
**limited_result,
|
|
370
|
+
"timestamp": datetime.now().isoformat()
|
|
371
|
+
},
|
|
372
|
+
"execution_time": time.time() - start_time,
|
|
373
|
+
"timestamp": datetime.now().isoformat()
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
async def _handle_get_contact_info(node_id: str, parameters: Dict[str, Any], start_time: float, rpc_call) -> Dict[str, Any]:
|
|
378
|
+
"""Handle get_contact_info operation."""
|
|
379
|
+
phone = parameters.get('contact_phone') or parameters.get('phone')
|
|
380
|
+
if not phone:
|
|
381
|
+
raise ValueError("Phone number is required")
|
|
382
|
+
|
|
383
|
+
data = await rpc_call('contact_info', {'phone': phone})
|
|
384
|
+
|
|
385
|
+
if isinstance(data, dict) and not data.get('success', True):
|
|
386
|
+
raise Exception(data.get('error', 'Failed to get contact info'))
|
|
387
|
+
|
|
388
|
+
result = data if not isinstance(data, dict) or 'result' not in data else data.get('result', data)
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
"success": True,
|
|
392
|
+
"node_id": node_id,
|
|
393
|
+
"node_type": "whatsappDb",
|
|
394
|
+
"result": {
|
|
395
|
+
"operation": "get_contact_info",
|
|
396
|
+
**result,
|
|
397
|
+
"timestamp": datetime.now().isoformat()
|
|
398
|
+
},
|
|
399
|
+
"execution_time": time.time() - start_time,
|
|
400
|
+
"timestamp": datetime.now().isoformat()
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
async def _handle_list_contacts(node_id: str, parameters: Dict[str, Any], start_time: float, rpc_call) -> Dict[str, Any]:
|
|
405
|
+
"""Handle list_contacts operation."""
|
|
406
|
+
query = parameters.get('query', '')
|
|
407
|
+
limit = parameters.get('limit', 50) # Default limit to prevent context overflow
|
|
408
|
+
|
|
409
|
+
data = await rpc_call('contacts', {'query': query})
|
|
410
|
+
|
|
411
|
+
if isinstance(data, dict) and not data.get('success', True):
|
|
412
|
+
raise Exception(data.get('error', 'Failed to list contacts'))
|
|
413
|
+
|
|
414
|
+
result = data if not isinstance(data, dict) or 'result' not in data else data.get('result', data)
|
|
415
|
+
contacts = result.get('contacts', []) if isinstance(result, dict) else result
|
|
416
|
+
|
|
417
|
+
total_found = len(contacts)
|
|
418
|
+
|
|
419
|
+
# Apply limit and return only essential fields: phone, name, jid
|
|
420
|
+
contacts_limited = [
|
|
421
|
+
{"phone": c.get("phone", ""), "name": c.get("name", ""), "jid": c.get("jid", "")}
|
|
422
|
+
for c in contacts[:limit]
|
|
423
|
+
]
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
"success": True,
|
|
427
|
+
"node_id": node_id,
|
|
428
|
+
"node_type": "whatsappDb",
|
|
429
|
+
"result": {
|
|
430
|
+
"operation": "list_contacts",
|
|
431
|
+
"contacts": contacts_limited,
|
|
432
|
+
"total": total_found,
|
|
433
|
+
"returned": len(contacts_limited),
|
|
434
|
+
"has_more": total_found > limit,
|
|
435
|
+
"query": query,
|
|
436
|
+
"hint": f"Showing {len(contacts_limited)} of {total_found} contacts. Use a more specific query to narrow results." if total_found > limit else None,
|
|
437
|
+
"timestamp": datetime.now().isoformat()
|
|
438
|
+
},
|
|
439
|
+
"execution_time": time.time() - start_time,
|
|
440
|
+
"timestamp": datetime.now().isoformat()
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
async def _handle_check_contacts(node_id: str, parameters: Dict[str, Any], start_time: float, rpc_call) -> Dict[str, Any]:
|
|
445
|
+
"""Handle check_contacts operation."""
|
|
446
|
+
phones_str = parameters.get('phones', '')
|
|
447
|
+
if not phones_str:
|
|
448
|
+
raise ValueError("Phone numbers are required")
|
|
449
|
+
|
|
450
|
+
# Parse comma-separated phones
|
|
451
|
+
phones = [p.strip() for p in phones_str.split(',') if p.strip()]
|
|
452
|
+
if not phones:
|
|
453
|
+
raise ValueError("At least one phone number is required")
|
|
454
|
+
|
|
455
|
+
data = await rpc_call('contact_check', {'phones': phones})
|
|
456
|
+
|
|
457
|
+
if isinstance(data, dict) and not data.get('success', True):
|
|
458
|
+
raise Exception(data.get('error', 'Failed to check contacts'))
|
|
459
|
+
|
|
460
|
+
results = data if isinstance(data, list) else data.get('result', [])
|
|
461
|
+
|
|
462
|
+
return {
|
|
463
|
+
"success": True,
|
|
464
|
+
"node_id": node_id,
|
|
465
|
+
"node_type": "whatsappDb",
|
|
466
|
+
"result": {
|
|
467
|
+
"operation": "check_contacts",
|
|
468
|
+
"results": results,
|
|
469
|
+
"total": len(results),
|
|
470
|
+
"timestamp": datetime.now().isoformat()
|
|
471
|
+
},
|
|
472
|
+
"execution_time": time.time() - start_time,
|
|
473
|
+
"timestamp": datetime.now().isoformat()
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
|