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,236 @@
|
|
|
1
|
+
"""WebSocket client for Temporal activities.
|
|
2
|
+
|
|
3
|
+
Uses aiohttp ClientSession for proper connection pooling and concurrent
|
|
4
|
+
request handling. Each activity creates its own WebSocket connection
|
|
5
|
+
to avoid race conditions, but the session manages connection reuse.
|
|
6
|
+
|
|
7
|
+
References:
|
|
8
|
+
- https://docs.aiohttp.org/en/stable/client_quickstart.html
|
|
9
|
+
- https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import asyncio
|
|
13
|
+
import json
|
|
14
|
+
import uuid
|
|
15
|
+
from typing import Any, Dict, Optional
|
|
16
|
+
from contextlib import asynccontextmanager
|
|
17
|
+
|
|
18
|
+
import aiohttp
|
|
19
|
+
|
|
20
|
+
from core.logging import get_logger
|
|
21
|
+
from core.config import Settings
|
|
22
|
+
|
|
23
|
+
logger = get_logger(__name__)
|
|
24
|
+
|
|
25
|
+
# Load settings
|
|
26
|
+
_settings = Settings()
|
|
27
|
+
WS_URL = f"ws://{_settings.host}:{_settings.port}/ws/internal"
|
|
28
|
+
|
|
29
|
+
print(f"[Temporal WS Client] WebSocket URL configured: {WS_URL}")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class WSConnectionPool:
|
|
33
|
+
"""WebSocket connection pool using aiohttp ClientSession.
|
|
34
|
+
|
|
35
|
+
aiohttp ClientSession provides built-in connection pooling with:
|
|
36
|
+
- Connection reuse and keep-alive
|
|
37
|
+
- Configurable connection limits
|
|
38
|
+
- Proper concurrent request handling
|
|
39
|
+
|
|
40
|
+
Each execute_node call gets its own WebSocket connection from the pool,
|
|
41
|
+
avoiding the ConcurrencyError that occurs when sharing a single connection.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self, url: str = WS_URL, pool_size: int = 100):
|
|
45
|
+
self.url = url
|
|
46
|
+
self.pool_size = pool_size
|
|
47
|
+
self._session: Optional[aiohttp.ClientSession] = None
|
|
48
|
+
self._lock = asyncio.Lock()
|
|
49
|
+
|
|
50
|
+
async def _get_session(self) -> aiohttp.ClientSession:
|
|
51
|
+
"""Get or create the aiohttp session with connection pooling."""
|
|
52
|
+
if self._session is None or self._session.closed:
|
|
53
|
+
async with self._lock:
|
|
54
|
+
if self._session is None or self._session.closed:
|
|
55
|
+
connector = aiohttp.TCPConnector(
|
|
56
|
+
limit=self.pool_size,
|
|
57
|
+
limit_per_host=self.pool_size,
|
|
58
|
+
enable_cleanup_closed=True,
|
|
59
|
+
)
|
|
60
|
+
timeout = aiohttp.ClientTimeout(
|
|
61
|
+
total=300, # 5 min total timeout
|
|
62
|
+
connect=10, # 10 sec connect timeout
|
|
63
|
+
)
|
|
64
|
+
self._session = aiohttp.ClientSession(
|
|
65
|
+
connector=connector,
|
|
66
|
+
timeout=timeout,
|
|
67
|
+
)
|
|
68
|
+
print(f"[WS Pool] Created session with pool_size={self.pool_size}")
|
|
69
|
+
return self._session
|
|
70
|
+
|
|
71
|
+
@asynccontextmanager
|
|
72
|
+
async def connection(self):
|
|
73
|
+
"""Get a WebSocket connection from the pool.
|
|
74
|
+
|
|
75
|
+
Usage:
|
|
76
|
+
async with pool.connection() as ws:
|
|
77
|
+
await ws.send_json(request)
|
|
78
|
+
response = await ws.receive_json()
|
|
79
|
+
"""
|
|
80
|
+
session = await self._get_session()
|
|
81
|
+
async with session.ws_connect(
|
|
82
|
+
self.url,
|
|
83
|
+
heartbeat=20,
|
|
84
|
+
receive_timeout=120,
|
|
85
|
+
) as ws:
|
|
86
|
+
yield ws
|
|
87
|
+
|
|
88
|
+
async def execute_node(
|
|
89
|
+
self,
|
|
90
|
+
node_id: str,
|
|
91
|
+
node_type: str,
|
|
92
|
+
data: Dict[str, Any],
|
|
93
|
+
context: Dict[str, Any],
|
|
94
|
+
timeout: float = 120.0,
|
|
95
|
+
) -> Dict[str, Any]:
|
|
96
|
+
"""Execute a node via WebSocket.
|
|
97
|
+
|
|
98
|
+
Each call gets its own connection from the pool, allowing
|
|
99
|
+
concurrent execution without race conditions.
|
|
100
|
+
"""
|
|
101
|
+
request_id = str(uuid.uuid4())
|
|
102
|
+
|
|
103
|
+
message = {
|
|
104
|
+
"type": "execute_node",
|
|
105
|
+
"request_id": request_id,
|
|
106
|
+
"node_id": node_id,
|
|
107
|
+
"node_type": node_type,
|
|
108
|
+
"parameters": data,
|
|
109
|
+
"nodes": context.get("nodes", []),
|
|
110
|
+
"edges": context.get("edges", []),
|
|
111
|
+
"session_id": context.get("session_id", "default"),
|
|
112
|
+
"workflow_id": context.get("workflow_id"),
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
async with self.connection() as ws:
|
|
117
|
+
await ws.send_json(message)
|
|
118
|
+
print(f"[WS Pool] Sent execute_node for {node_id}")
|
|
119
|
+
|
|
120
|
+
# Wait for response with matching request_id
|
|
121
|
+
async with asyncio.timeout(timeout):
|
|
122
|
+
async for msg in ws:
|
|
123
|
+
if msg.type == aiohttp.WSMsgType.TEXT:
|
|
124
|
+
response = json.loads(msg.data)
|
|
125
|
+
if response.get("request_id") == request_id:
|
|
126
|
+
print(f"[WS Pool] Received response for {node_id}: success={response.get('success')}")
|
|
127
|
+
return response
|
|
128
|
+
elif msg.type == aiohttp.WSMsgType.ERROR:
|
|
129
|
+
raise Exception(f"WebSocket error: {ws.exception()}")
|
|
130
|
+
elif msg.type == aiohttp.WSMsgType.CLOSED:
|
|
131
|
+
raise Exception("WebSocket closed unexpectedly")
|
|
132
|
+
|
|
133
|
+
raise Exception(f"No response received for request {request_id}")
|
|
134
|
+
|
|
135
|
+
except asyncio.TimeoutError:
|
|
136
|
+
raise Exception(f"WebSocket request timeout ({timeout}s) for node {node_id}")
|
|
137
|
+
except aiohttp.ClientError as e:
|
|
138
|
+
raise Exception(f"WebSocket connection error: {e}")
|
|
139
|
+
|
|
140
|
+
async def close(self):
|
|
141
|
+
"""Close the connection pool."""
|
|
142
|
+
if self._session and not self._session.closed:
|
|
143
|
+
await self._session.close()
|
|
144
|
+
print("[WS Pool] Session closed")
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# Global connection pool instance
|
|
148
|
+
_pool: Optional[WSConnectionPool] = None
|
|
149
|
+
_pool_lock = asyncio.Lock()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
async def get_ws_pool() -> WSConnectionPool:
|
|
153
|
+
"""Get or create the global WebSocket connection pool."""
|
|
154
|
+
global _pool
|
|
155
|
+
|
|
156
|
+
async with _pool_lock:
|
|
157
|
+
if _pool is None:
|
|
158
|
+
_pool = WSConnectionPool()
|
|
159
|
+
return _pool
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
async def execute_node_ws(
|
|
163
|
+
node_id: str,
|
|
164
|
+
node_type: str,
|
|
165
|
+
data: Dict[str, Any],
|
|
166
|
+
context: Dict[str, Any],
|
|
167
|
+
timeout: float = 120.0,
|
|
168
|
+
) -> Dict[str, Any]:
|
|
169
|
+
"""Execute a node via WebSocket using the connection pool.
|
|
170
|
+
|
|
171
|
+
This is the recommended way to execute nodes from activities.
|
|
172
|
+
Each call gets its own connection from the pool.
|
|
173
|
+
"""
|
|
174
|
+
pool = await get_ws_pool()
|
|
175
|
+
return await pool.execute_node(
|
|
176
|
+
node_id=node_id,
|
|
177
|
+
node_type=node_type,
|
|
178
|
+
data=data,
|
|
179
|
+
context=context,
|
|
180
|
+
timeout=timeout,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
async def close_ws_pool() -> None:
|
|
185
|
+
"""Close the global WebSocket connection pool."""
|
|
186
|
+
global _pool
|
|
187
|
+
|
|
188
|
+
async with _pool_lock:
|
|
189
|
+
if _pool:
|
|
190
|
+
await _pool.close()
|
|
191
|
+
_pool = None
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# Backwards compatibility aliases
|
|
195
|
+
async def get_ws_client() -> WSConnectionPool:
|
|
196
|
+
"""Alias for get_ws_pool() for backwards compatibility."""
|
|
197
|
+
return await get_ws_pool()
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
async def close_ws_client() -> None:
|
|
201
|
+
"""Alias for close_ws_pool() for backwards compatibility."""
|
|
202
|
+
await close_ws_pool()
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# For backwards compatibility with old code that expects TemporalWSClient
|
|
206
|
+
class TemporalWSClient:
|
|
207
|
+
"""Backwards compatibility wrapper around WSConnectionPool."""
|
|
208
|
+
|
|
209
|
+
def __init__(self, url: str = WS_URL):
|
|
210
|
+
self._pool = WSConnectionPool(url=url)
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def connected(self) -> bool:
|
|
214
|
+
return True # Pool manages connections
|
|
215
|
+
|
|
216
|
+
async def connect(self) -> None:
|
|
217
|
+
pass # Pool connects on demand
|
|
218
|
+
|
|
219
|
+
async def disconnect(self) -> None:
|
|
220
|
+
await self._pool.close()
|
|
221
|
+
|
|
222
|
+
async def execute_node(
|
|
223
|
+
self,
|
|
224
|
+
node_id: str,
|
|
225
|
+
node_type: str,
|
|
226
|
+
data: Dict[str, Any],
|
|
227
|
+
context: Dict[str, Any],
|
|
228
|
+
timeout: float = 120.0,
|
|
229
|
+
) -> Dict[str, Any]:
|
|
230
|
+
return await self._pool.execute_node(
|
|
231
|
+
node_id=node_id,
|
|
232
|
+
node_type=node_type,
|
|
233
|
+
data=data,
|
|
234
|
+
context=context,
|
|
235
|
+
timeout=timeout,
|
|
236
|
+
)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Text generation service."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing import Dict, Any
|
|
6
|
+
|
|
7
|
+
from core.logging import get_logger, log_execution_time
|
|
8
|
+
|
|
9
|
+
logger = get_logger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TextService:
|
|
13
|
+
"""Text generation and processing service."""
|
|
14
|
+
|
|
15
|
+
def __init__(self):
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
async def execute_text_generator(self, node_id: str, parameters: Dict[str, Any]) -> Dict[str, Any]:
|
|
19
|
+
"""Execute text generator node (migrated from Node.js)."""
|
|
20
|
+
start_time = time.time()
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
text = parameters.get('text', 'Hello World')
|
|
24
|
+
include_timestamp = parameters.get('includeTimestamp', True)
|
|
25
|
+
|
|
26
|
+
result_data = {
|
|
27
|
+
"text": text,
|
|
28
|
+
"length": len(text),
|
|
29
|
+
"nodeId": node_id
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if include_timestamp:
|
|
33
|
+
result_data["timestamp"] = datetime.now().isoformat()
|
|
34
|
+
|
|
35
|
+
result = {
|
|
36
|
+
"type": "text",
|
|
37
|
+
"data": result_data,
|
|
38
|
+
"nodeId": node_id,
|
|
39
|
+
"timestamp": datetime.now().isoformat()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
log_execution_time(logger, "text_generator", start_time, time.time())
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
"success": True,
|
|
46
|
+
"node_id": node_id,
|
|
47
|
+
"node_type": "textGenerator",
|
|
48
|
+
"result": result,
|
|
49
|
+
"execution_time": time.time() - start_time,
|
|
50
|
+
"timestamp": datetime.now().isoformat()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
except Exception as e:
|
|
54
|
+
logger.error("Text generator execution failed", node_id=node_id, error=str(e))
|
|
55
|
+
return {
|
|
56
|
+
"success": False,
|
|
57
|
+
"node_id": node_id,
|
|
58
|
+
"node_type": "textGenerator",
|
|
59
|
+
"error": str(e),
|
|
60
|
+
"execution_time": time.time() - start_time,
|
|
61
|
+
"timestamp": datetime.now().isoformat()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async def execute_file_handler(self, node_id: str, parameters: Dict[str, Any]) -> Dict[str, Any]:
|
|
65
|
+
"""Execute file handler node (migrated from Node.js)."""
|
|
66
|
+
start_time = time.time()
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
file_type = parameters.get('fileType', 'generic')
|
|
70
|
+
file_content = parameters.get('content', '')
|
|
71
|
+
file_name = parameters.get('fileName', 'untitled.txt')
|
|
72
|
+
|
|
73
|
+
# Basic file processing
|
|
74
|
+
result_data = {
|
|
75
|
+
"fileName": file_name,
|
|
76
|
+
"fileType": file_type,
|
|
77
|
+
"content": file_content,
|
|
78
|
+
"size": len(file_content),
|
|
79
|
+
"processed": True,
|
|
80
|
+
"processingType": file_type,
|
|
81
|
+
"nodeId": node_id
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
result = {
|
|
85
|
+
"type": "file",
|
|
86
|
+
"data": result_data,
|
|
87
|
+
"nodeId": node_id,
|
|
88
|
+
"timestamp": datetime.now().isoformat()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
log_execution_time(logger, "file_handler", start_time, time.time())
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
"success": True,
|
|
95
|
+
"node_id": node_id,
|
|
96
|
+
"node_type": "fileHandler",
|
|
97
|
+
"result": result,
|
|
98
|
+
"execution_time": time.time() - start_time,
|
|
99
|
+
"timestamp": datetime.now().isoformat()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
except Exception as e:
|
|
103
|
+
logger.error("File handler execution failed", node_id=node_id, error=str(e))
|
|
104
|
+
return {
|
|
105
|
+
"success": False,
|
|
106
|
+
"node_id": node_id,
|
|
107
|
+
"node_type": "fileHandler",
|
|
108
|
+
"error": str(e),
|
|
109
|
+
"execution_time": time.time() - start_time,
|
|
110
|
+
"timestamp": datetime.now().isoformat()
|
|
111
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""User authentication service with JWT handling."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from datetime import datetime, timezone, timedelta
|
|
5
|
+
from typing import Optional, Dict, Any
|
|
6
|
+
|
|
7
|
+
from jose import jwt, JWTError
|
|
8
|
+
from sqlmodel import select
|
|
9
|
+
|
|
10
|
+
from core.config import Settings
|
|
11
|
+
from core.database import Database
|
|
12
|
+
from models.auth import User
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class UserAuthService:
|
|
18
|
+
"""Handles user authentication, registration, and JWT token management."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, database: Database, settings: Settings):
|
|
21
|
+
self.database = database
|
|
22
|
+
self.settings = settings
|
|
23
|
+
self._algorithm = "HS256"
|
|
24
|
+
|
|
25
|
+
async def get_user_count(self) -> int:
|
|
26
|
+
"""Get total number of users."""
|
|
27
|
+
async with self.database.get_session() as session:
|
|
28
|
+
result = await session.execute(select(User))
|
|
29
|
+
return len(result.scalars().all())
|
|
30
|
+
|
|
31
|
+
async def get_user_by_email(self, email: str) -> Optional[User]:
|
|
32
|
+
"""Get user by email address."""
|
|
33
|
+
async with self.database.get_session() as session:
|
|
34
|
+
result = await session.execute(
|
|
35
|
+
select(User).where(User.email == email.lower().strip())
|
|
36
|
+
)
|
|
37
|
+
return result.scalars().first()
|
|
38
|
+
|
|
39
|
+
async def get_user_by_id(self, user_id: int) -> Optional[User]:
|
|
40
|
+
"""Get user by ID."""
|
|
41
|
+
async with self.database.get_session() as session:
|
|
42
|
+
result = await session.execute(
|
|
43
|
+
select(User).where(User.id == user_id)
|
|
44
|
+
)
|
|
45
|
+
return result.scalars().first()
|
|
46
|
+
|
|
47
|
+
async def can_register(self) -> bool:
|
|
48
|
+
"""Check if registration is allowed based on auth mode."""
|
|
49
|
+
if self.settings.auth_mode == "multi":
|
|
50
|
+
return True
|
|
51
|
+
# Single-owner mode: only allow if no users exist
|
|
52
|
+
count = await self.get_user_count()
|
|
53
|
+
return count == 0
|
|
54
|
+
|
|
55
|
+
async def register(
|
|
56
|
+
self, email: str, password: str, display_name: str
|
|
57
|
+
) -> tuple[Optional[User], Optional[str]]:
|
|
58
|
+
"""
|
|
59
|
+
Register a new user.
|
|
60
|
+
Returns (user, None) on success, (None, error_message) on failure.
|
|
61
|
+
"""
|
|
62
|
+
# Check if registration is allowed
|
|
63
|
+
if not await self.can_register():
|
|
64
|
+
if self.settings.auth_mode == "single":
|
|
65
|
+
return None, "Registration disabled - owner account already exists"
|
|
66
|
+
return None, "Registration is currently disabled"
|
|
67
|
+
|
|
68
|
+
# Check if email already exists
|
|
69
|
+
existing = await self.get_user_by_email(email)
|
|
70
|
+
if existing:
|
|
71
|
+
return None, "Email already registered"
|
|
72
|
+
|
|
73
|
+
# Validate password strength
|
|
74
|
+
if len(password) < 8:
|
|
75
|
+
return None, "Password must be at least 8 characters"
|
|
76
|
+
|
|
77
|
+
# Determine if this is the owner (first user in single-owner mode)
|
|
78
|
+
is_owner = self.settings.auth_mode == "single" and await self.get_user_count() == 0
|
|
79
|
+
|
|
80
|
+
# Create user
|
|
81
|
+
user = User.create(
|
|
82
|
+
email=email,
|
|
83
|
+
password=password,
|
|
84
|
+
display_name=display_name,
|
|
85
|
+
is_owner=is_owner
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
async with self.database.get_session() as session:
|
|
89
|
+
session.add(user)
|
|
90
|
+
await session.commit()
|
|
91
|
+
await session.refresh(user)
|
|
92
|
+
|
|
93
|
+
logger.info(f"User registered: {email} (owner={is_owner})")
|
|
94
|
+
return user, None
|
|
95
|
+
|
|
96
|
+
async def login(
|
|
97
|
+
self, email: str, password: str
|
|
98
|
+
) -> tuple[Optional[User], Optional[str]]:
|
|
99
|
+
"""
|
|
100
|
+
Authenticate user and return user object.
|
|
101
|
+
Returns (user, None) on success, (None, error_message) on failure.
|
|
102
|
+
"""
|
|
103
|
+
user = await self.get_user_by_email(email)
|
|
104
|
+
if not user:
|
|
105
|
+
return None, "Invalid email or password"
|
|
106
|
+
|
|
107
|
+
if not user.is_active:
|
|
108
|
+
return None, "Account is disabled"
|
|
109
|
+
|
|
110
|
+
if not user.verify_password(password):
|
|
111
|
+
return None, "Invalid email or password"
|
|
112
|
+
|
|
113
|
+
# Update last login
|
|
114
|
+
async with self.database.get_session() as session:
|
|
115
|
+
result = await session.execute(
|
|
116
|
+
select(User).where(User.id == user.id)
|
|
117
|
+
)
|
|
118
|
+
db_user = result.scalars().first()
|
|
119
|
+
if db_user:
|
|
120
|
+
db_user.last_login = datetime.now(timezone.utc)
|
|
121
|
+
await session.commit()
|
|
122
|
+
|
|
123
|
+
logger.info(f"User logged in: {email}")
|
|
124
|
+
return user, None
|
|
125
|
+
|
|
126
|
+
def create_access_token(self, user: User) -> str:
|
|
127
|
+
"""Create JWT access token for user."""
|
|
128
|
+
expire = datetime.now(timezone.utc) + timedelta(minutes=self.settings.jwt_expire_minutes)
|
|
129
|
+
payload = {
|
|
130
|
+
"sub": str(user.id),
|
|
131
|
+
"email": user.email,
|
|
132
|
+
"display_name": user.display_name,
|
|
133
|
+
"is_owner": user.is_owner,
|
|
134
|
+
"exp": expire,
|
|
135
|
+
"iat": datetime.now(timezone.utc)
|
|
136
|
+
}
|
|
137
|
+
return jwt.encode(payload, self.settings.jwt_secret_key, algorithm=self._algorithm)
|
|
138
|
+
|
|
139
|
+
def verify_token(self, token: str) -> Optional[Dict[str, Any]]:
|
|
140
|
+
"""
|
|
141
|
+
Verify JWT token and return payload.
|
|
142
|
+
Returns None if token is invalid or expired.
|
|
143
|
+
"""
|
|
144
|
+
try:
|
|
145
|
+
payload = jwt.decode(
|
|
146
|
+
token,
|
|
147
|
+
self.settings.jwt_secret_key,
|
|
148
|
+
algorithms=[self._algorithm]
|
|
149
|
+
)
|
|
150
|
+
return payload
|
|
151
|
+
except JWTError as e:
|
|
152
|
+
logger.debug(f"Token verification failed: {e}")
|
|
153
|
+
return None
|
|
154
|
+
|
|
155
|
+
async def get_current_user(self, token: str) -> Optional[User]:
|
|
156
|
+
"""Get current user from token."""
|
|
157
|
+
payload = self.verify_token(token)
|
|
158
|
+
if not payload:
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
user_id = payload.get("sub")
|
|
162
|
+
if not user_id:
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
return await self.get_user_by_id(int(user_id))
|
|
166
|
+
|
|
167
|
+
def get_auth_status(self) -> Dict[str, Any]:
|
|
168
|
+
"""Get authentication status and mode info."""
|
|
169
|
+
return {
|
|
170
|
+
"auth_mode": self.settings.auth_mode,
|
|
171
|
+
"registration_enabled": self.settings.auth_mode == "multi"
|
|
172
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Android WebSocket Client - DEPRECATED
|
|
3
|
+
|
|
4
|
+
This module is deprecated. Use services.android instead:
|
|
5
|
+
|
|
6
|
+
from services.android import (
|
|
7
|
+
RelayWebSocketClient,
|
|
8
|
+
get_relay_client,
|
|
9
|
+
close_relay_client,
|
|
10
|
+
get_current_relay_client,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
This file re-exports from the new module for backwards compatibility.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from services.android import (
|
|
17
|
+
RelayWebSocketClient,
|
|
18
|
+
get_relay_client,
|
|
19
|
+
close_relay_client,
|
|
20
|
+
get_current_relay_client,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Re-export for backwards compatibility
|
|
24
|
+
__all__ = [
|
|
25
|
+
"RelayWebSocketClient",
|
|
26
|
+
"get_relay_client",
|
|
27
|
+
"close_relay_client",
|
|
28
|
+
"get_current_relay_client",
|
|
29
|
+
]
|