Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Disable Redis by default, improve stop scripts and add Cerebras/Groq providers
trohitgc356900d16be
diff --git a/.env.template b/.env.templateindex 1fe25d49..63005231 100644--- a/.env.template+++ b/.env.template@@ -35,7 +35,7 @@ DATABASE_POOL_SIZE=5DATABASE_MAX_OVERFLOW=10# Cache Configuration (Disabled for development)-REDIS_ENABLED=true+REDIS_ENABLED=falseCACHE_TTL=300# Loggingdiff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsxindex 7f6c1ca3..91486dac 100644--- a/client/src/Dashboard.tsx+++ b/client/src/Dashboard.tsx@@ -70,7 +70,7 @@ const createNodeTypes = (): Record<string, React.ComponentType<any>> => {// Trigger nodes - no input connections (start workflows)if (TRIGGER_NODE_TYPES.includes(type)) {types[type] = TriggerNode;- } else if (type === 'openaiChatModel' || type === 'anthropicChatModel' || type === 'geminiChatModel' || type === 'openrouterChatModel') {+ } else if (type === 'openaiChatModel' || type === 'anthropicChatModel' || type === 'geminiChatModel' || type === 'openrouterChatModel' || type === 'groqChatModel' || type === 'cerebrasChatModel') {// AI chat model nodes use square designtypes[type] = SquareNode;} else if (type === 'aiAgent') {diff --git a/client/src/components/CredentialsModal.tsx b/client/src/components/CredentialsModal.tsxindex 3d22e18d..b62bf5eb 100644--- a/client/src/components/CredentialsModal.tsx+++ b/client/src/components/CredentialsModal.tsx@@ -1,86 +1,131 @@/**- * CredentialsModal - Manage API credentials globally+ * CredentialsModal - Modern categorized credentials management panel+ * Uses Ant Design Collapse + List for compact, modern UI*/-import React, { useState, useEffect, useCallback } from 'react';-import { Form, Input, Button, Card, Tag, Space, message, Row, Col, Typography } from 'antd';-import { CheckCircleOutlined, DeleteOutlined, SafetyOutlined } from '@ant-design/icons';+import React, { useState, useEffect, useCallback, useMemo } from 'react';+import { Input, Button, message, Collapse, List, Tag, Space, Tooltip } from 'antd';+import {+ CheckCircleOutlined,+ DeleteOutlined,+ SafetyOutlined,+ SearchOutlined,+ EyeOutlined,+ EyeInvisibleOutlined,+} from '@ant-design/icons';import Modal from './ui/Modal';import { useApiKeys } from '../hooks/useApiKeys';+import { useAppTheme } from '../hooks/useAppTheme';+import {+ OpenAIIcon, ClaudeIcon, GeminiIcon, GroqIcon, OpenRouterIcon, CerebrasIcon,+} from './icons/AIProviderIcons';-const { Text } = Typography;--const PROVIDERS = [- { id: 'openai', name: 'OpenAI', placeholder: 'sk-...', color: '#10a37f', desc: 'GPT-4o, GPT-4 Turbo, GPT-3.5' },- { id: 'anthropic', name: 'Anthropic', placeholder: 'sk-ant-...', color: '#d97706', desc: 'Claude 3.5 Sonnet, Claude 3 Opus' },- { id: 'gemini', name: 'Gemini', placeholder: 'AIza...', color: '#4285f4', desc: 'Gemini 1.5 Pro, Gemini Flash' },- { id: 'openrouter', name: 'OpenRouter', placeholder: 'sk-or-...', color: '#6366f1', desc: 'Unified API - OpenAI, Claude, Llama, Mistral, and more' },- { id: 'groq', name: 'Groq', placeholder: 'gsk_...', color: '#F55036', desc: 'Ultra-fast LLM inference - Llama, Mixtral, Gemma' },- { id: 'cerebras', name: 'Cerebras', placeholder: 'csk-...', color: '#FF6600', desc: 'Ultra-fast inference - Llama, Qwen on custom AI hardware' },- { id: 'google_maps', name: 'Google Maps', placeholder: 'AIza...', color: '#34a853', desc: 'Geocoding, Places API' },- { id: 'android_remote', name: 'Android Remote', placeholder: 'your-api-key...', color: '#3ddc84', desc: 'Remote Android device via WebSocket' },+// ============================================================================+// TYPES & DATA+// ============================================================================++interface CredentialItem {+ id: string;+ name: string;+ placeholder: string;+ color: string;+ desc: string;+ Icon?: React.FC<{ size?: number }>;+}++interface Category {+ key: string;+ label: string;+ items: CredentialItem[];+}++const CATEGORIES: Category[] = [+ {+ key: 'ai',+ label: 'AI Providers',+ items: [+ { id: 'openai', name: 'OpenAI', placeholder: 'sk-...', color: '#10a37f', desc: 'GPT-4o, GPT-4, GPT-3.5', Icon: OpenAIIcon },+ { id: 'anthropic', name: 'Anthropic', placeholder: 'sk-ant-...', color: '#d97706', desc: 'Claude 3.5 Sonnet, Opus', Icon: ClaudeIcon },+ { id: 'gemini', name: 'Gemini', placeholder: 'AIza...', color: '#4285f4', desc: 'Gemini 1.5 Pro, Flash', Icon: GeminiIcon },+ { id: 'groq', name: 'Groq', placeholder: 'gsk_...', color: '#F55036', desc: 'Llama, Mixtral - Ultra-fast', Icon: GroqIcon },+ { id: 'cerebras', name: 'Cerebras', placeholder: 'csk-...', color: '#FF6600', desc: 'Llama, Qwen - Ultra-fast', Icon: CerebrasIcon },+ { id: 'openrouter', name: 'OpenRouter', placeholder: 'sk-or-...', color: '#6366f1', desc: 'Unified API - 100+ models', Icon: OpenRouterIcon },+ ],+ },+ {+ key: 'services',+ label: 'Services',+ items: [+ { id: 'google_maps', name: 'Google Maps', placeholder: 'AIza...', color: '#34a853', desc: 'Geocoding, Places API' },+ { id: 'android_remote', name: 'Android Remote', placeholder: 'your-api-key...', color: '#3ddc84', desc: 'WebSocket device control' },+ ],+ },];+// ============================================================================+// MAIN COMPONENT+// ============================================================================+interface Props {visible: boolean;onClose: () => void;}const CredentialsModal: React.FC<Props> = ({ visible, onClose }) => {+ const theme = useAppTheme();const { validateApiKey, saveApiKey, getStoredApiKey, hasStoredKey, removeApiKey, validateGoogleMapsKey } = useApiKeys();- const [form] = Form.useForm();+const [validKeys, setValidKeys] = useState<Record<string, boolean>>({});const [loading, setLoading] = useState<Record<string, boolean>>({});const [models, setModels] = useState<Record<string, string[]>>({});+ const [keys, setKeys] = useState<Record<string, string>>({});+ const [expanded, setExpanded] = useState<string | null>(null);+ const [search, setSearch] = useState('');+ const [showKey, setShowKey] = useState<Record<string, boolean>>({});- const loadStoredKeys = useCallback(async () => {- const fieldValues: Record<string, string> = {};- const validResults: Record<string, boolean> = {};+ // Load stored keys+ const loadKeys = useCallback(async () => {+ const allItems = CATEGORIES.flatMap(c => c.items);+ const newKeys: Record<string, string> = {};+ const newValid: Record<string, boolean> = {};- for (const p of PROVIDERS) {- const has = await hasStoredKey(p.id);- if (has) {- const key = await getStoredApiKey(p.id);+ for (const item of allItems) {+ if (await hasStoredKey(item.id)) {+ const key = await getStoredApiKey(item.id);if (key) {- fieldValues[p.id] = key;- validResults[p.id] = true;+ newKeys[item.id] = key;+ newValid[item.id] = true;}}}-- if (Object.keys(fieldValues).length > 0) {- form.setFieldsValue(fieldValues);- setValidKeys(validResults);- }- }, [form, hasStoredKey, getStoredApiKey]);+ setKeys(newKeys);+ setValidKeys(newValid);+ }, [hasStoredKey, getStoredApiKey]);useEffect(() => {if (visible) {- loadStoredKeys();+ loadKeys();+ setExpanded(null);+ setSearch('');}- }, [visible, loadStoredKeys]);+ }, [visible, loadKeys]);const handleValidate = async (id: string) => {- const key = form.getFieldValue(id);+ const key = keys[id];if (!key?.trim()) return message.warning('Enter an API key first');setLoading(l => ({ ...l, [id]: true }));-- let result;- if (id === 'google_maps') {- result = await validateGoogleMapsKey(key);- } else if (id === 'android_remote') {- // Android Remote key - save without validation (validated on connection)- result = await saveApiKey(id, key);- } else {- result = await validateApiKey(id, key);- }+ const result = id === 'google_maps'+ ? await validateGoogleMapsKey(key)+ : id === 'android_remote'+ ? await saveApiKey(id, key)+ : await validateApiKey(id, key);setLoading(l => ({ ...l, [id]: false }));if (result.isValid) {setValidKeys(v => ({ ...v, [id]: true }));if (result.models) setModels(m => ({ ...m, [id]: result.models! }));- message.success(`${PROVIDERS.find(p => p.id === id)?.name} key saved${id === 'android_remote' ? '' : ' and validated'}`);+ message.success('Key saved and validated');} else {message.error(result.error || 'Invalid key');}@@ -88,78 +133,170 @@ const CredentialsModal: React.FC<Props> = ({ visible, onClose }) => {const handleRemove = async (id: string) => {await removeApiKey(id);- form.setFieldValue(id, '');+ setKeys(k => ({ ...k, [id]: '' }));setValidKeys(v => ({ ...v, [id]: false }));setModels(m => ({ ...m, [id]: [] }));message.success('Key removed');};+ // Filter by search+ const filteredCategories = useMemo(() => {+ if (!search.trim()) return CATEGORIES;+ const q = search.toLowerCase();+ return CATEGORIES.map(cat => ({+ ...cat,+ items: cat.items.filter(i => i.name.toLowerCase().includes(q) || i.desc.toLowerCase().includes(q)),+ })).filter(cat => cat.items.length > 0);+ }, [search]);++ // Count configured per category+ const getCount = (items: typeof CATEGORIES[0]['items']) =>+ items.filter(i => validKeys[i.id]).length;+return (- <Modal- isOpen={visible}- onClose={onClose}- title="API Credentials"- maxWidth="95vw"- maxHeight="95vh"- >- <div style={{ padding: '24px' }}>- <div style={{ marginBottom: 24 }}>- <Space>- <SafetyOutlined style={{ color: '#52c41a' }} />- <Text type="secondary">- API keys are validated and stored securely. They will be used automatically by AI nodes in your workflows.- </Text>- </Space>+ <Modal isOpen={visible} onClose={onClose} title="API Credentials" maxWidth="95vw" maxHeight="95vh">+ <div style={{ padding: 20 }}>+ {/* Info banner */}+ <div style={{+ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 16,+ padding: 12, borderRadius: 8,+ backgroundColor: `${theme.dracula.green}10`,+ border: `1px solid ${theme.dracula.green}30`,+ }}>+ <SafetyOutlined style={{ color: theme.dracula.green }} />+ <span style={{ fontSize: 13, color: theme.colors.textSecondary }}>+ Keys are stored securely and used automatically by AI nodes.+ </span></div>- <Form form={form} layout="vertical">- <Row gutter={[24, 24]}>- {PROVIDERS.map(p => (- <Col xs={24} lg={12} key={p.id}>- <Card- title={- <Space>- <span style={{ color: p.color, fontWeight: 600 }}>{p.name}</span>- {validKeys[p.id] && <Tag color="success" icon={<CheckCircleOutlined />}>Valid</Tag>}- </Space>- }- extra={validKeys[p.id] && (- <Button size="small" danger icon={<DeleteOutlined />} onClick={() => handleRemove(p.id)}>- Remove- </Button>- )}- >- <Text type="secondary" style={{ fontSize: 13, display: 'block', marginBottom: 16 }}>{p.desc}</Text>- <Space.Compact style={{ width: '100%' }}>- <Form.Item name={p.id} noStyle>- <Input.Password- placeholder={p.placeholder}- style={{ fontFamily: 'monospace', fontSize: 14 }}- onChange={() => setValidKeys(v => ({ ...v, [p.id]: false }))}- />- </Form.Item>- <Button- loading={loading[p.id]}- onClick={() => handleValidate(p.id)}- type={validKeys[p.id] ? 'primary' : 'default'}- style={validKeys[p.id] ? { backgroundColor: p.color, borderColor: p.color } : {}}+ {/* Search */}+ <Input+ placeholder="Search credentials..."+ prefix={<SearchOutlined style={{ color: theme.colors.textMuted }} />}+ value={search}+ onChange={e => setSearch(e.target.value)}+ allowClear+ style={{ marginBottom: 16 }}+ />++ {/* Categories */}+ <Collapse+ defaultActiveKey={['ai', 'services']}+ ghost+ style={{ background: 'transparent' }}+ items={filteredCategories.map(cat => ({+ key: cat.key,+ label: (+ <Space>+ <span style={{ fontWeight: 600, textTransform: 'uppercase', fontSize: 11, letterSpacing: '0.05em' }}>+ {cat.label}+ </span>+ <Tag color={getCount(cat.items) === cat.items.length ? 'success' : 'default'}>+ {getCount(cat.items)}/{cat.items.length}+ </Tag>+ </Space>+ ),+ children: (+ <List+ dataSource={cat.items}+ renderItem={item => {+ const isExpanded = expanded === item.id;+ const isValid = validKeys[item.id];+ const Icon = item.Icon;++ return (+ <List.Item+ style={{+ cursor: 'pointer',+ padding: '12px 8px',+ borderRadius: 8,+ marginBottom: 4,+ backgroundColor: isExpanded ? theme.colors.backgroundAlt : 'transparent',+ flexDirection: 'column',+ alignItems: 'stretch',+ }}+ onClick={() => setExpanded(isExpanded ? null : item.id)}>- {validKeys[p.id] ? 'Valid' : 'Validate'}- </Button>- </Space.Compact>- {models[p.id]?.length > 0 && (- <Text type="secondary" style={{ fontSize: 12, marginTop: 12, display: 'block' }}>- Models: {models[p.id].slice(0, 4).join(', ')}{models[p.id].length > 4 && ` +${models[p.id].length - 4} more`}- </Text>- )}- </Card>- </Col>- ))}- </Row>- </Form>-- <div style={{ marginTop: 24, textAlign: 'right' }}>- <Button type="primary" onClick={onClose}>Done</Button>+ {/* Row header */}+ <div style={{ display: 'flex', alignItems: 'center', width: '100%' }}>+ <div style={{+ width: 32, height: 32, borderRadius: 6, marginRight: 12,+ backgroundColor: `${item.color}15`,+ display: 'flex', alignItems: 'center', justifyContent: 'center',+ }}>+ {Icon ? <Icon size={18} /> : <span>{item.id === 'google_maps' ? '📍' : '📱'}</span>}+ </div>+ <div style={{ flex: 1 }}>+ <div style={{ fontWeight: 500, fontSize: 14 }}>{item.name}</div>+ <div style={{ fontSize: 12, color: theme.colors.textMuted }}>{item.desc}</div>+ </div>+ {isValid && (+ <Tag icon={<CheckCircleOutlined />} color="success">Valid</Tag>+ )}+ </div>++ {/* Expanded content */}+ {isExpanded && (+ <div style={{ marginTop: 12, paddingTop: 12, borderTop: `1px solid ${theme.colors.border}` }}+ onClick={e => e.stopPropagation()}+ >+ <Space.Compact style={{ width: '100%', marginBottom: 8 }}>+ <Input+ type={showKey[item.id] ? 'text' : 'password'}+ value={keys[item.id] || ''}+ onChange={e => {+ setKeys(k => ({ ...k, [item.id]: e.target.value }));+ setValidKeys(v => ({ ...v, [item.id]: false }));+ }}+ placeholder={item.placeholder}+ style={{ fontFamily: 'monospace', fontSize: 13 }}+ suffix={+ <Tooltip title={showKey[item.id] ? 'Hide' : 'Show'}>+ <span+ onClick={() => setShowKey(s => ({ ...s, [item.id]: !s[item.id] }))}+ style={{ cursor: 'pointer', color: theme.colors.textMuted }}+ >+ {showKey[item.id] ? <EyeInvisibleOutlined /> : <EyeOutlined />}+ </span>+ </Tooltip>+ }+ />+ <Button+ loading={loading[item.id]}+ onClick={() => handleValidate(item.id)}+ type={isValid ? 'primary' : 'default'}+ style={isValid ? { backgroundColor: theme.dracula.green, borderColor: theme.dracula.green } : {}}+ >+ {isValid ? 'Valid' : 'Validate'}+ </Button>+ </Space.Compact>++ {models[item.id]?.length > 0 && (+ <div style={{ fontSize: 12, color: theme.colors.textMuted, marginBottom: 8 }}>+ Models: {models[item.id].slice(0, 4).join(', ')}{models[item.id].length > 4 && ` +${models[item.id].length - 4}`}+ </div>+ )}++ {isValid && (+ <Button size="small" danger icon={<DeleteOutlined />} onClick={() => handleRemove(item.id)}>+ Remove+ </Button>+ )}+ </div>+ )}+ </List.Item>+ );+ }}+ />+ ),+ }))}+ />++ {/* Footer */}+ <div style={{ marginTop: 20, textAlign: 'right' }}>+ <Button type="primary" onClick={onClose} style={{ backgroundColor: theme.dracula.green, borderColor: theme.dracula.green }}>+ Done+ </Button></div></div></Modal>diff --git a/client/src/components/SquareNode.tsx b/client/src/components/SquareNode.tsxindex d0d53fa5..a27697c7 100644--- a/client/src/components/SquareNode.tsx+++ b/client/src/components/SquareNode.tsx@@ -31,6 +31,9 @@ const AI_MODEL_NODE_TYPES: Record<string, string> = {'openaiChatModel': 'openai','anthropicChatModel': 'anthropic','geminiChatModel': 'gemini',+ 'openrouterChatModel': 'openrouter',+ 'groqChatModel': 'groq',+ 'cerebrasChatModel': 'cerebras',};const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectable, selected }) => {diff --git a/client/src/components/ui/ConsolePanel.tsx b/client/src/components/ui/ConsolePanel.tsxindex 40ec06f5..56cf693b 100644--- a/client/src/components/ui/ConsolePanel.tsx+++ b/client/src/components/ui/ConsolePanel.tsx@@ -23,6 +23,7 @@ interface ConsolePanelProps {// Storage keys for persisting stateconst CONSOLE_HEIGHT_KEY = 'console_panel_height';+const CHAT_WIDTH_KEY = 'console_chat_width_percent';const ConsolePanel: React.FC<ConsolePanelProps> = ({isOpen,@@ -33,9 +34,12 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({}) => {const theme = useAppTheme();const { isDarkMode } = useTheme();- const { consoleLogs, clearConsoleLogs, sendChatMessage, chatMessages, clearChatMessages } = useWebSocket();+ const { consoleLogs, clearConsoleLogs, terminalLogs, clearTerminalLogs, sendChatMessage, chatMessages, clearChatMessages } = useWebSocket();const [filter, setFilter] = useState('');+ const [terminalFilter, setTerminalFilter] = useState('');+ const [terminalLogLevel, setTerminalLogLevel] = useState<'all' | 'error' | 'warning' | 'info' | 'debug'>('all');const [autoScroll, setAutoScroll] = useState(true);+ const [consoleTab, setConsoleTab] = useState<'console' | 'terminal'>('console');const logsEndRef = useRef<HTMLDivElement>(null);const chatEndRef = useRef<HTMLDivElement>(null);const chatInputRef = useRef<HTMLInputElement>(null);@@ -60,11 +64,33 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({return defaultHeight;});- // Resize state+ // Chat section width (percentage) with localStorage persistence+ const [chatWidthPercent, setChatWidthPercent] = useState(() => {+ try {+ const saved = localStorage.getItem(CHAT_WIDTH_KEY);+ if (saved) {+ const width = parseFloat(saved);+ if (!isNaN(width) && width >= 20 && width <= 80) {+ return width;+ }+ }+ } catch {+ // Ignore localStorage errors+ }+ return 50; // Default 50%+ });++ // Vertical resize state (panel height)const [isResizing, setIsResizing] = useState(false);const resizeStartY = useRef(0);const resizeStartHeight = useRef(0);+ // Horizontal resize state (chat/console split)+ const [isHorizontalResizing, setIsHorizontalResizing] = useState(false);+ const resizeStartX = useRef(0);+ const resizeStartWidth = useRef(0);+ const containerRef = useRef<HTMLDivElement>(null);+// Save height to localStorage when it changesuseEffect(() => {try {@@ -74,7 +100,16 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({}}, [panelHeight]);- // Handle resize start+ // Save chat width to localStorage when it changes+ useEffect(() => {+ try {+ localStorage.setItem(CHAT_WIDTH_KEY, chatWidthPercent.toString());+ } catch {+ // Ignore localStorage errors+ }+ }, [chatWidthPercent]);++ // Handle vertical resize start (panel height)const handleResizeStart = useCallback((e: React.MouseEvent) => {e.preventDefault();e.stopPropagation();@@ -83,7 +118,16 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({resizeStartHeight.current = panelHeight;}, [panelHeight]);- // Handle resize move+ // Handle horizontal resize start (chat/console split)+ const handleHorizontalResizeStart = useCallback((e: React.MouseEvent) => {+ e.preventDefault();+ e.stopPropagation();+ setIsHorizontalResizing(true);+ resizeStartX.current = e.clientX;+ resizeStartWidth.current = chatWidthPercent;+ }, [chatWidthPercent]);++ // Handle vertical resize moveuseEffect(() => {if (!isResizing) return;@@ -112,7 +156,39 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({};}, [isResizing, minHeight, maxHeight]);- // Filter logs based on search input+ // Handle horizontal resize move+ useEffect(() => {+ if (!isHorizontalResizing || !containerRef.current) return;++ const handleMouseMove = (e: MouseEvent) => {+ const container = containerRef.current;+ if (!container) return;++ const containerRect = container.getBoundingClientRect();+ const newWidth = ((e.clientX - containerRect.left) / containerRect.width) * 100;+ const clampedWidth = Math.min(80, Math.max(20, newWidth));+ setChatWidthPercent(clampedWidth);+ };++ const handleMouseUp = () => {+ setIsHorizontalResizing(false);+ };++ document.addEventListener('mousemove', handleMouseMove);+ document.addEventListener('mouseup', handleMouseUp);++ document.body.style.cursor = 'ew-resize';+ document.body.style.userSelect = 'none';++ return () => {+ document.removeEventListener('mousemove', handleMouseMove);+ document.removeEventListener('mouseup', handleMouseUp);+ document.body.style.cursor = '';+ document.body.style.userSelect = '';+ };+ }, [isHorizontalResizing]);++ // Filter console logs based on search inputconst filteredLogs = useMemo(() => {if (!filter) return consoleLogs;const lowerFilter = filter.toLowerCase();@@ -123,6 +199,29 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({);}, [consoleLogs, filter]);+ // Filter terminal logs based on search input and log level+ const filteredTerminalLogs = useMemo(() => {+ let filtered = terminalLogs;++ // Filter by log level+ if (terminalLogLevel !== 'all') {+ const levelPriority: Record<string, number> = { error: 0, warning: 1, info: 2, debug: 3 };+ const selectedPriority = levelPriority[terminalLogLevel] ?? 2;+ filtered = filtered.filter(log => (levelPriority[log.level] ?? 2) <= selectedPriority);+ }++ // Filter by search text+ if (terminalFilter) {+ const lowerFilter = terminalFilter.toLowerCase();+ filtered = filtered.filter(log =>+ log.message.toLowerCase().includes(lowerFilter) ||+ (log.source?.toLowerCase().includes(lowerFilter))+ );+ }++ return filtered;+ }, [terminalLogs, terminalFilter, terminalLogLevel]);+// Auto-scroll to bottom when new logs arriveuseEffect(() => {if (autoScroll && isOpen && logsEndRef.current) {@@ -255,17 +354,34 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({height: isOpen ? `${panelHeight}px` : '0px',overflow: 'hidden',backgroundColor: isDarkMode ? theme.dracula.background : theme.colors.background,- transition: isResizing ? 'none' : 'height 0.2s ease-in-out',+ transition: (isResizing || isHorizontalResizing) ? 'none' : 'height 0.2s ease-in-out',display: 'flex',flexDirection: 'row' // Side by side};- const sectionStyle: React.CSSProperties = {- flex: 1,+ const chatSectionStyle: React.CSSProperties = {+ width: `${chatWidthPercent}%`,display: 'flex',flexDirection: 'column',overflow: 'hidden',- borderRight: `1px solid ${isDarkMode ? theme.dracula.selection : theme.colors.border}`+ position: 'relative'+ };++ const consoleSectionStyle: React.CSSProperties = {+ flex: 1,+ display: 'flex',+ flexDirection: 'column',+ overflow: 'hidden'+ };++ const horizontalResizeHandleStyle: React.CSSProperties = {+ width: '6px',+ cursor: 'ew-resize',+ backgroundColor: isHorizontalResizing+ ? (isDarkMode ? theme.dracula.purple : theme.colors.primary)+ : (isDarkMode ? theme.dracula.selection : theme.colors.border),+ transition: isHorizontalResizing ? 'none' : 'background-color 0.15s ease',+ flexShrink: 0};const sectionHeaderStyle: React.CSSProperties = {@@ -374,24 +490,24 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({const chatMessagesStyle: React.CSSProperties = {flex: 1,overflow: 'auto',- padding: '8px 12px'+ padding: '12px 16px'};const chatInputContainerStyle: React.CSSProperties = {display: 'flex',- gap: '8px',- padding: '8px 12px',+ gap: '10px',+ padding: '10px 16px',borderTop: `1px solid ${isDarkMode ? theme.dracula.selection : theme.colors.border}`,backgroundColor: isDarkMode ? theme.dracula.currentLine : theme.colors.backgroundPanel};const chatInputStyle: React.CSSProperties = {flex: 1,- padding: '6px 10px',+ padding: '8px 12px',fontSize: theme.fontSize.sm,backgroundColor: isDarkMode ? theme.dracula.background : theme.colors.background,- border: `1px solid ${theme.colors.border}`,- borderRadius: '6px',+ border: `1px solid ${isDarkMode ? theme.dracula.selection : theme.colors.border}`,+ borderRadius: '8px',color: theme.colors.text,outline: 'none'};@@ -410,15 +526,17 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({};const chatMessageStyle = (isUser: boolean): React.CSSProperties => ({- padding: '6px 10px',- marginBottom: '6px',- borderRadius: '8px',+ padding: '8px 12px',+ marginBottom: '8px',+ borderRadius: isUser ? '12px 12px 4px 12px' : '12px 12px 12px 4px',backgroundColor: isUser- ? (isDarkMode ? `${theme.dracula.purple}30` : `${theme.colors.primary}15`)+ ? (isDarkMode ? `${theme.dracula.purple}40` : `${theme.colors.primary}20`): (isDarkMode ? theme.dracula.selection : theme.colors.backgroundPanel),- maxWidth: '85%',- alignSelf: isUser ? 'flex-end' : 'flex-start',- wordBreak: 'break-word'+ maxWidth: '80%',+ marginLeft: isUser ? 'auto' : '0',+ marginRight: isUser ? '0' : 'auto',+ wordBreak: 'break-word',+ boxShadow: isDarkMode ? 'none' : '0 1px 2px rgba(0,0,0,0.05)'});const chatMessageTextStyle: React.CSSProperties = {@@ -478,10 +596,10 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({</div></div>- {/* Panel Content - Split 50/50 */}- <div style={contentStyle}>- {/* Chat Section - Left Half */}- <div style={sectionStyle}>+ {/* Panel Content - Resizable Split */}+ <div ref={containerRef} style={contentStyle}>+ {/* Chat Section - Left */}+ <div style={chatSectionStyle}><div style={sectionHeaderStyle}><span style={sectionTitleStyle}>Chat@@ -516,7 +634,7 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({Send a message to trigger chatTrigger nodes</div>) : (- <div style={{ display: 'flex', flexDirection: 'column' }}>+ <div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>{chatMessages.map((msg, index) => (<div key={`${msg.timestamp}-${index}`} style={chatMessageStyle(msg.role === 'user')}><p style={chatMessageTextStyle}>{msg.message}</p>@@ -550,21 +668,107 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({</div></div>- {/* Console Section - Right Half */}- <div style={{ ...sectionStyle, borderRight: 'none' }}>+ {/* Horizontal Resize Handle */}+ <div+ style={horizontalResizeHandleStyle}+ onMouseDown={handleHorizontalResizeStart}+ onMouseEnter={e => {+ if (!isHorizontalResizing) {+ e.currentTarget.style.backgroundColor = isDarkMode+ ? `${theme.dracula.purple}60`+ : `${theme.colors.primary}40`;+ }+ }}+ onMouseLeave={e => {+ if (!isHorizontalResizing) {+ e.currentTarget.style.backgroundColor = isDarkMode+ ? theme.dracula.selection+ : theme.colors.border;+ }+ }}+ />++ {/* Console/Terminal Section - Right */}+ <div style={consoleSectionStyle}><div style={sectionHeaderStyle}>- <span style={sectionTitleStyle}>- Console- {consoleLogs.length > 0 && (- <span style={badgeStyle}>{consoleLogs.length}</span>- )}- </span>+ {/* Tab Buttons */}+ <div style={{ display: 'flex', alignItems: 'center', gap: '4px' }}>+ <button+ onClick={() => setConsoleTab('console')}+ style={{+ ...buttonStyle,+ padding: '3px 8px',+ backgroundColor: consoleTab === 'console'+ ? (isDarkMode ? `${theme.dracula.purple}40` : `${theme.colors.primary}20`)+ : 'transparent',+ color: consoleTab === 'console'+ ? (isDarkMode ? theme.dracula.purple : theme.colors.primary)+ : theme.colors.textSecondary,+ fontWeight: consoleTab === 'console' ? '600' : '400',+ borderRadius: '4px'+ }}+ >+ Console+ {consoleLogs.length > 0 && (+ <span style={{ ...badgeStyle, marginLeft: '4px', padding: '0 4px' }}>{consoleLogs.length}</span>+ )}+ </button>+ <button+ onClick={() => setConsoleTab('terminal')}+ style={{+ ...buttonStyle,+ padding: '3px 8px',+ backgroundColor: consoleTab === 'terminal'+ ? (isDarkMode ? `${theme.dracula.cyan}40` : `${theme.colors.info}20`)+ : 'transparent',+ color: consoleTab === 'terminal'+ ? (isDarkMode ? theme.dracula.cyan : theme.colors.info)+ : theme.colors.textSecondary,+ fontWeight: consoleTab === 'terminal' ? '600' : '400',+ borderRadius: '4px'+ }}+ >+ Terminal+ {terminalLogs.length > 0 && (+ <span style={{+ ...badgeStyle,+ marginLeft: '4px',+ padding: '0 4px',+ backgroundColor: isDarkMode ? `${theme.dracula.cyan}40` : `${theme.colors.info}20`,+ color: isDarkMode ? theme.dracula.cyan : theme.colors.info+ }}>{terminalLogs.length}</span>+ )}+ </button>+ </div><div style={{ display: 'flex', alignItems: 'center', gap: '6px' }}>+ {consoleTab === 'terminal' && (+ <select+ value={terminalLogLevel}+ onChange={e => setTerminalLogLevel(e.target.value as 'all' | 'error' | 'warning' | 'info' | 'debug')}+ onClick={e => e.stopPropagation()}+ style={{+ padding: '2px 4px',+ fontSize: '10px',+ backgroundColor: isDarkMode ? theme.dracula.currentLine : theme.colors.background,+ color: theme.colors.text,+ border: `1px solid ${isDarkMode ? theme.dracula.selection : theme.colors.border}`,+ borderRadius: '4px',+ cursor: 'pointer',+ outline: 'none'+ }}+ >+ <option value="all">All Levels</option>+ <option value="error">Error</option>+ <option value="warning">Warning+</option>+ <option value="info">Info+</option>+ <option value="debug">Debug+</option>+ </select>+ )}<inputtype="text"placeholder="Filter..."- value={filter}- onChange={e => setFilter(e.target.value)}+ value={consoleTab === 'console' ? filter : terminalFilter}+ onChange={e => consoleTab === 'console' ? setFilter(e.target.value) : setTerminalFilter(e.target.value)}style={filterInputStyle}onClick={e => e.stopPropagation()}/>@@ -586,10 +790,10 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({/>Auto</label>- {consoleLogs.length > 0 && (+ {((consoleTab === 'console' && consoleLogs.length > 0) || (consoleTab === 'terminal' && terminalLogs.length > 0)) && (<buttonstyle={clearButtonStyle}- onClick={handleClearConsole}+ onClick={consoleTab === 'console' ? handleClearConsole : clearTerminalLogs}onMouseEnter={e => {e.currentTarget.style.backgroundColor = isDarkMode ? `${theme.dracula.red}35` : `${theme.colors.error}25`;}}@@ -603,38 +807,91 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({</div></div><div style={{ flex: 1, overflow: 'auto', fontFamily: 'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace', fontSize: '12px' }}>- {filteredLogs.length === 0 ? (- <div style={emptyStyle}>- {consoleLogs.length === 0- ? 'Add a Console node to see debug output'- : 'No logs match the filter'}- </div>- ) : (- filteredLogs.slice().reverse().map((log, index) => (- <div key={`${log.node_id}-${log.timestamp}-${index}`} style={logEntryStyle}>- <span style={timestampStyle}>{formatTimestamp(log.timestamp)}</span>- <span style={labelStyle} title={log.label || log.node_id}>- {log.label || log.node_id}- </span>- {log.source_node_label && (- <span style={nodeInfoStyle} title={`Source: ${log.source_node_type} (${log.source_node_id})`}>- {log.source_node_label}+ {consoleTab === 'console' ? (+ // Console Logs Tab+ filteredLogs.length === 0 ? (+ <div style={emptyStyle}>+ {consoleLogs.length === 0+ ? 'Add a Console node to see debug output'+ : 'No logs match the filter'}+ </div>+ ) : (+ filteredLogs.slice().reverse().map((log, index) => (+ <div key={`${log.node_id}-${log.timestamp}-${index}`} style={logEntryStyle}>+ <span style={timestampStyle}>{formatTimestamp(log.timestamp)}</span>+ <span style={labelStyle} title={log.label || log.node_id}>+ {log.label || log.node_id}</span>- )}- <pre- style={{- margin: 0,- flex: 1,- overflow: 'auto',- color: getFormatColor(log.format),- whiteSpace: log.format === 'json' ? 'pre-wrap' : 'pre',- wordBreak: 'break-word'- }}- >- {log.formatted}- </pre>+ {log.source_node_label && (+ <span style={nodeInfoStyle} title={`Source: ${log.source_node_type} (${log.source_node_id})`}>+ {log.source_node_label}+ </span>+ )}+ <pre+ style={{+ margin: 0,+ flex: 1,+ overflow: 'auto',+ color: getFormatColor(log.format),+ whiteSpace: log.format === 'json' ? 'pre-wrap' : 'pre',+ wordBreak: 'break-word'+ }}+ >+ {log.formatted}+ </pre>+ </div>+ ))+ )+ ) : (+ // Terminal Logs Tab+ filteredTerminalLogs.length === 0 ? (+ <div style={emptyStyle}>+ {terminalLogs.length === 0+ ? 'Server logs will appear here'+ : 'No logs match the filter'}+ </div>+ ) : (+ <div style={{ minWidth: 'max-content' }}>+ {filteredTerminalLogs.slice().reverse().map((log, index) => (+ <div key={`${log.timestamp}-${index}`} style={{+ padding: '3px 12px',+ borderBottom: `1px solid ${isDarkMode ? theme.dracula.selection : theme.colors.border}`,+ backgroundColor: log.level === 'error'+ ? (isDarkMode ? `${theme.dracula.red}10` : `${theme.colors.error}05`)+ : log.level === 'warning'+ ? (isDarkMode ? `${theme.dracula.orange}10` : `${theme.colors.warning}05`)+ : 'transparent',+ whiteSpace: 'nowrap'+ }}>+ <span style={{+ color: isDarkMode ? theme.dracula.comment : theme.colors.textMuted,+ fontSize: '11px'+ }}>{formatTimestamp(log.timestamp)}</span>+ {log.source && (+ <span style={{+ color: isDarkMode ? theme.dracula.cyan : theme.colors.info,+ fontSize: '11px',+ marginLeft: '8px'+ }}>+ [{log.source}]+ </span>+ )}+ <span style={{+ color: theme.colors.text,+ fontSize: '12px',+ marginLeft: '8px'+ }}>+ {log.message}+ {log.details && (+ <span style={{ color: isDarkMode ? theme.dracula.comment : theme.colors.textMuted, marginLeft: '8px' }}>+ {typeof log.details === 'string' ? log.details : JSON.stringify(log.details)}+ </span>+ )}+ </span>+ </div>+ ))}</div>- ))+ ))}<div ref={logsEndRef} /></div>diff --git a/client/src/components/ui/SettingsPanel.tsx b/client/src/components/ui/SettingsPanel.tsxindex 8b7b41a8..edbf04ed 100644--- a/client/src/components/ui/SettingsPanel.tsx+++ b/client/src/components/ui/SettingsPanel.tsx@@ -161,7 +161,7 @@ const SettingsPanel: React.FC<SettingsPanelProps> = ({isOpen={isOpen}onClose={onClose}title="Settings"- maxWidth="600px"+ maxWidth="95vw"maxHeight="95vh"headerActions={headerActions}>diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsxindex e6bc085b..f75eac69 100644--- a/client/src/contexts/WebSocketContext.tsx+++ b/client/src/contexts/WebSocketContext.tsx@@ -113,6 +113,15 @@ export interface ConsoleLogEntry {source_node_label?: string;}+// Terminal/server log entry+export interface TerminalLogEntry {+ timestamp: string;+ level: 'debug' | 'info' | 'warning' | 'error';+ message: string;+ source?: string; // e.g., 'workflow', 'ai', 'android', 'whatsapp'+ details?: any;+}+// Chat message for chatTrigger nodesexport interface ChatMessage {role: 'user' | 'assistant';@@ -170,6 +179,7 @@ interface WebSocketContextValue {lastWhatsAppMessage: WhatsAppMessage | null; // Most recent messageapiKeyStatuses: Record<string, ApiKeyStatus>;consoleLogs: ConsoleLogEntry[]; // Console node output logs+ terminalLogs: TerminalLogEntry[]; // Server/terminal logschatMessages: ChatMessage[]; // Chat messages for chatTriggernodeStatuses: Record<string, NodeStatus>; // Current workflow's node statusesnodeParameters: Record<string, NodeParameters>;@@ -186,6 +196,7 @@ interface WebSocketContextValue {clearNodeStatus: (nodeId: string) => Promise<void>;clearWhatsAppMessages: () => void;clearConsoleLogs: () => void;+ clearTerminalLogs: () => void;clearChatMessages: () => void;sendChatMessage: (message: string, sessionId?: string) => Promise<void>;@@ -313,6 +324,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chiconst [lastWhatsAppMessage, setLastWhatsAppMessage] = useState<WhatsAppMessage | null>(null);const [apiKeyStatuses, setApiKeyStatuses] = useState<Record<string, ApiKeyStatus>>({});const [consoleLogs, setConsoleLogs] = useState<ConsoleLogEntry[]>([]);+ const [terminalLogs, setTerminalLogs] = useState<TerminalLogEntry[]>([]);const [chatMessages, setChatMessages] = useState<ChatMessage[]>([]);// Per-workflow node statuses: workflow_id -> node_id -> NodeStatus (n8n pattern)const [allNodeStatuses, setAllNodeStatuses] = useState<Record<string, Record<string, NodeStatus>>>({});@@ -753,6 +765,29 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi}break;+ case 'terminal_log':+ // Handle terminal/server log entries+ if (data) {+ const terminalEntry: TerminalLogEntry = {+ timestamp: data.timestamp || new Date().toISOString(),+ level: data.level || 'info',+ message: data.message || '',+ source: data.source,+ details: data.details+ };+ // Add to logs (newest first, limit to 200 entries)+ setTerminalLogs(prev => {+ const updated = [terminalEntry, ...prev];+ return updated.slice(0, 200);+ });+ }+ break;++ case 'terminal_logs_cleared':+ // Handle terminal logs cleared from server+ setTerminalLogs([]);+ break;+case 'workflow_lock':// Handle workflow lock status updates (per-workflow locking - n8n pattern)// Only update lock state if it's for the current workflow or if unlocking@@ -846,6 +881,42 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi// Ignore errors during initial check}}++ // Load terminal log history+ try {+ const terminalResponse = await new Promise<any>((resolve, reject) => {+ const requestId = `terminal_logs_${Date.now()}`;+ const timeout = setTimeout(() => reject(new Error('Timeout')), 5000);++ const handler = (event: MessageEvent) => {+ try {+ const msg = JSON.parse(event.data);+ if (msg.request_id === requestId) {+ clearTimeout(timeout);+ ws.removeEventListener('message', handler);+ resolve(msg);+ }+ } catch {}+ };++ ws.addEventListener('message', handler);+ ws.send(JSON.stringify({ type: 'get_terminal_logs', request_id: requestId }));+ });++ if (terminalResponse.success && terminalResponse.logs) {+ // Map server logs to TerminalLogEntry format (newest first)+ const logs: TerminalLogEntry[] = terminalResponse.logs.map((log: any) => ({+ timestamp: log.timestamp || new Date().toISOString(),+ level: log.level || 'info',+ message: log.message || '',+ source: log.source,+ details: log.details+ })).reverse(); // Server stores oldest first, we want newest first+ setTerminalLogs(logs);+ }+ } catch {+ // Ignore errors loading terminal logs+ }};ws.onmessage = handleMessage;@@ -946,6 +1017,15 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chisetConsoleLogs([]);}, []);+ // Clear terminal logs (also clears on server)+ const clearTerminalLogs = useCallback(() => {+ setTerminalLogs([]);+ // Also notify server to clear its terminal log history+ if (wsRef.current?.readyState === WebSocket.OPEN) {+ wsRef.current.send(JSON.stringify({ type: 'clear_terminal_logs' }));+ }+ }, []);+// Clear chat messagesconst clearChatMessages = useCallback(() => {setChatMessages([]);@@ -1151,14 +1231,15 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chinode_type: nodeType,parameters,nodes,- edges+ edges,+ workflow_id: currentWorkflowId // Include workflow_id for per-workflow status scoping}, timeoutMs);return response;} catch (error) {console.error('[WebSocket] Failed to execute node:', error);throw error;}- }, [sendRequest]);+ }, [sendRequest, currentWorkflowId]);const getNodeOutputAsync = useCallback(async (nodeId: string,@@ -1673,6 +1754,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chilastWhatsAppMessage,apiKeyStatuses,consoleLogs,+ terminalLogs,chatMessages,nodeStatuses,nodeParameters,@@ -1689,6 +1771,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chiclearNodeStatus,clearWhatsAppMessages,clearConsoleLogs,+ clearTerminalLogs,clearChatMessages,sendChatMessage: sendChatMessageAsync,diff --git a/docker-compose.prod.yml b/docker-compose.prod.ymlindex c17fdd86..b32709f0 100644--- a/docker-compose.prod.yml+++ b/docker-compose.prod.yml@@ -1,8 +1,10 @@services:- # Redis Cache (production performance)+ # Redis Cache (optional - enable with REDIS_ENABLED=true)redis:image: redis:7-alpinecontainer_name: machina-redis+ profiles:+ - redisvolumes:- redis-data:/datanetworks:@@ -41,13 +43,11 @@ services:# Production overrides- PORT=${PYTHON_BACKEND_PORT:-3010}- ENV=production- - REDIS_ENABLED=true+ - REDIS_ENABLED=false- REDIS_URL=redis://redis:6379- WHATSAPP_RPC_URL=ws://whatsapp:${WHATSAPP_RPC_PORT:-9400}/ws/rpc- TZ=UTCdepends_on:- redis:- condition: service_healthywhatsapp:condition: service_healthydiff --git a/package.json b/package.jsonindex e57da2ea..963d25d9 100644--- a/package.json+++ b/package.json@@ -7,7 +7,7 @@"client"],"scripts": {- "dev": "concurrently -t -n client,python,whatsapp -c blue,green,yellow --kill-others-on-fail \"npm:client:dev\" \"npm:python:dev\" \"npm:whatsapp:api\"",+ "dev": "concurrently --raw --kill-others-on-fail \"npm:client:dev\" \"npm:python:dev\" \"npm:whatsapp:api\"","client:dev": "cd client && set NO_COLOR=1&& npm run dev","python:dev": "cd server && python -m uvicorn main:app --host 127.0.0.1 --port 3010 --reload --reload-dir . --reload-exclude \"*.pyc\" --reload-exclude \"__pycache__\" --log-level warning","whatsapp:start": "cd server/whatsapp-rpc && npm start",diff --git a/scripts/check-ports.ps1 b/scripts/check-ports.ps1new file mode 100644index 00000000..f1ac0c1c--- /dev/null+++ b/scripts/check-ports.ps1@@ -0,0 +1,33 @@+# Check port status - shows real vs orphaned TCP entries+param(+ [int[]]$Ports = @(3000, 3010, 9400)+)++Write-Host "Checking port status...`n"++foreach ($port in $Ports) {+ Write-Host "=== Port $port ===" -ForegroundColor Cyan+ $connections = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue++ if (-not $connections) {+ Write-Host " No connections" -ForegroundColor Green+ continue+ }++ $grouped = $connections | Group-Object State+ foreach ($group in $grouped) {+ Write-Host " $($group.Name): $($group.Count) connection(s)"+ }++ $processIds = $connections | Select-Object -ExpandProperty OwningProcess -Unique | Where-Object { $_ -ne 0 }+ foreach ($procId in $processIds) {+ $proc = Get-Process -Id $procId -ErrorAction SilentlyContinue+ if ($proc) {+ Write-Host " REAL: PID $procId ($($proc.ProcessName))" -ForegroundColor Yellow+ } else {+ Write-Host " ORPHAN: PID $procId (process dead, TCP entry will timeout)" -ForegroundColor DarkGray+ }+ }+}++Write-Host "`nNote: Orphaned entries auto-clear in 30-120 seconds. They don't block new processes."diff --git a/scripts/kill-port.ps1 b/scripts/kill-port.ps1new file mode 100644index 00000000..355985bd--- /dev/null+++ b/scripts/kill-port.ps1@@ -0,0 +1,154 @@+# Kill all processes using the specified ports (Windows)+# Usage: .\kill-port.ps1 [-Ports] 3000,3010,9400+# If no ports specified, uses default MachinaOs ports: 3000, 3010, 9400++param(+ [int[]]$Ports = @(3000, 3010, 9400)+)++$ErrorActionPreference = 'SilentlyContinue'++function Get-ProcessTree {+ param([int]$ParentId)++ $children = @()+ $childProcesses = Get-CimInstance Win32_Process -Filter "ParentProcessId=$ParentId" -ErrorAction SilentlyContinue++ foreach ($child in $childProcesses) {+ if ($child.ProcessId -and $child.ProcessId -ne 0) {+ $children += $child.ProcessId+ # Recursively get grandchildren+ $children += Get-ProcessTree -ParentId $child.ProcessId+ }+ }++ return $children+}++function Get-PortProcesses {+ param([int]$Port)++ $pids = @()++ # Method 1: Get-NetTCPConnection (preferred)+ $connections = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue |+ Where-Object { $_.State -eq 'Listen' -or $_.State -eq 'Established' }++ if ($connections) {+ $pids += $connections | Select-Object -ExpandProperty OwningProcess -Unique | Where-Object { $_ -ne 0 }+ }++ # Method 2: netstat fallback+ if ($pids.Count -eq 0) {+ $netstatOutput = netstat -ano 2>$null | Select-String ":$Port\s" | Select-String "LISTENING|ESTABLISHED"+ foreach ($line in $netstatOutput) {+ if ($line -match '\s+(\d+)\s*$') {+ $pid = [int]$Matches[1]+ if ($pid -ne 0 -and $pids -notcontains $pid) {+ $pids += $pid+ }+ }+ }+ }++ return $pids | Select-Object -Unique+}++function Stop-ProcessSafely {+ param([int]$ProcessId)++ $proc = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue+ if (-not $proc) {+ return $false+ }++ $procName = $proc.ProcessName++ # Try graceful stop first+ Stop-Process -Id $ProcessId -ErrorAction SilentlyContinue+ Start-Sleep -Milliseconds 200++ # Check if still running+ $proc = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue+ if ($proc) {+ # Force kill+ Stop-Process -Id $ProcessId -Force -ErrorAction SilentlyContinue+ Start-Sleep -Milliseconds 200+ }++ # Verify killed+ $proc = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue+ return (-not $proc)+}++Write-Host "Stopping MachinaOs services..." -ForegroundColor Cyan+Write-Host "Platform: Windows"+Write-Host "Ports: $($Ports -join ', ')`n"++$allStopped = $true++foreach ($port in $Ports) {+ $portPids = Get-PortProcesses -Port $port++ if ($portPids.Count -eq 0) {+ Write-Host "[OK] Port $port`: Free" -ForegroundColor Green+ continue+ }++ # Collect all PIDs including child processes+ $allPids = @()+ foreach ($pid in $portPids) {+ $allPids += $pid+ $children = Get-ProcessTree -ParentId $pid+ $allPids += $children+ }+ $allPids = $allPids | Select-Object -Unique | Where-Object { $_ -ne 0 }++ $killedPids = @()+ $failedPids = @()++ # Kill all processes (children first, then parents)+ $sortedPids = $allPids | Sort-Object -Descending++ foreach ($pid in $sortedPids) {+ $proc = Get-Process -Id $pid -ErrorAction SilentlyContinue+ if ($proc) {+ $procName = $proc.ProcessName+ $killed = Stop-ProcessSafely -ProcessId $pid+ if ($killed) {+ $killedPids += "$pid ($procName)"+ } else {+ $failedPids += "$pid ($procName)"+ }+ }+ }++ # Verify port is free+ Start-Sleep -Milliseconds 500+ $remainingPids = Get-PortProcesses -Port $port++ if ($remainingPids.Count -eq 0) {+ if ($killedPids.Count -gt 0) {+ Write-Host "[OK] Port $port`: Killed $($killedPids.Count) process(es)" -ForegroundColor Green+ Write-Host " PIDs: $($killedPids -join ', ')" -ForegroundColor DarkGray+ } else {+ Write-Host "[OK] Port $port`: Free" -ForegroundColor Green+ }+ } else {+ Write-Host "[!!] Port $port`: Warning - still in use" -ForegroundColor Yellow+ if ($killedPids.Count -gt 0) {+ Write-Host " Killed: $($killedPids -join ', ')" -ForegroundColor DarkGray+ }+ Write-Host " Still running: $($remainingPids -join ', ')" -ForegroundColor Red+ $allStopped = $false+ }+}++Write-Host ""+if ($allStopped) {+ Write-Host "All services stopped successfully." -ForegroundColor Green+} else {+ Write-Host "Warning: Some ports may still be in use." -ForegroundColor Yellow+ Write-Host "Try running as Administrator or manually kill the processes." -ForegroundColor Yellow+ exit 1+}diff --git a/scripts/start.js b/scripts/start.jsindex b02cf00f..c2a3b842 100644--- a/scripts/start.js+++ b/scripts/start.js@@ -1,5 +1,5 @@#!/usr/bin/env node-import { execSync, spawn } from 'child_process';+import { execSync, spawn, spawnSync } from 'child_process';import { fileURLToPath } from 'url';import { dirname, resolve } from 'path';import { readFileSync, existsSync, copyFileSync } from 'fs';@@ -8,6 +8,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');const START = Date.now();const isWindows = process.platform === 'win32';+const isMac = process.platform === 'darwin';// Timing helperconst elapsed = () => `${((Date.now() - START) / 1000).toFixed(2)}s`;@@ -20,8 +21,10 @@ function loadPorts() {const content = readFileSync(envPath, 'utf-8');const env = {};for (const line of content.split('\n')) {- const [key, val] = line.split('=');- if (key && val) env[key.trim()] = val.trim();+ const match = line.match(/^([^#=]+)=(.*)$/);+ if (match) {+ env[match[1].trim()] = match[2].trim();+ }}return [parseInt(env.VITE_CLIENT_PORT) || 3000,@@ -30,39 +33,123 @@ function loadPorts() {];}-// Kill process by port using native commands-function killPort(port) {+// Execute command and return output (suppressing errors)+function exec(cmd) {try {- if (isWindows) {- const output = execSync(`netstat -ano | findstr :${port} | findstr LISTENING`, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });- const lines = output.trim().split('\n');- const pids = new Set();- for (const line of lines) {- const parts = line.trim().split(/\s+/);- const pid = parts[parts.length - 1];- if (pid && !isNaN(parseInt(pid))) pids.add(pid);+ return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();+ } catch {+ return '';+ }+}++// Get PIDs using a port - Windows+function getPidsWindows(port) {+ const pids = new Set();+ const netstatOutput = exec(`netstat -ano | findstr :${port} | findstr LISTENING`);+ for (const line of netstatOutput.split('\n')) {+ const parts = line.trim().split(/\s+/);+ const pid = parts[parts.length - 1];+ if (pid && /^\d+$/.test(pid) && pid !== '0') {+ pids.add(pid);+ }+ }+ return Array.from(pids);+}++// Get PIDs using a port - Unix+function getPidsUnix(port) {+ const pids = new Set();+ const lsofOutput = exec(`lsof -ti:${port} -sTCP:LISTEN 2>/dev/null`);+ for (const pid of lsofOutput.split('\n')) {+ if (pid.trim() && /^\d+$/.test(pid.trim())) {+ pids.add(pid.trim());+ }+ }+ // Fallback to ss on Linux+ if (pids.size === 0 && !isMac) {+ const ssOutput = exec(`ss -tlnp 2>/dev/null | grep :${port}`);+ const matches = ssOutput.matchAll(/pid=(\d+)/g);+ for (const match of matches) {+ pids.add(match[1]);+ }+ }+ return Array.from(pids);+}++// Get child process PIDs - Windows+function getChildPidsWindows(parentPid) {+ const children = new Set();+ const wmicOutput = exec(`wmic process where (ParentProcessId=${parentPid}) get ProcessId 2>nul`);+ for (const line of wmicOutput.split('\n')) {+ const pid = line.trim();+ if (pid && /^\d+$/.test(pid)) {+ children.add(pid);+ for (const grandchild of getChildPidsWindows(pid)) {+ children.add(grandchild);}- for (const pid of pids) {- try {- execSync(`taskkill /PID ${pid} /F`, { stdio: 'pipe' });- } catch {}+ }+ }+ return Array.from(children);+}++// Get child process PIDs - Unix+function getChildPidsUnix(parentPid) {+ const children = new Set();+ const pgrepOutput = exec(`pgrep -P ${parentPid} 2>/dev/null`);+ for (const pid of pgrepOutput.split('\n')) {+ if (pid.trim() && /^\d+$/.test(pid.trim())) {+ children.add(pid.trim());+ for (const grandchild of getChildPidsUnix(pid.trim())) {+ children.add(grandchild);}+ }+ }+ return Array.from(children);+}++// Kill process by port thoroughly+function killPort(port) {+ const getPids = isWindows ? getPidsWindows : getPidsUnix;+ const getChildPids = isWindows ? getChildPidsWindows : getChildPidsUnix;++ let pids = getPids(port);+ if (pids.length === 0) return false;++ // Collect all PIDs including children+ const allPids = new Set(pids);+ for (const pid of pids) {+ for (const childPid of getChildPids(pid)) {+ allPids.add(childPid);+ }+ }++ // Kill all processes+ for (const pid of allPids) {+ if (isWindows) {+ exec(`taskkill /PID ${pid} /F /T 2>nul`);} else {- const output = execSync(`lsof -ti:${port}`, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });- const pids = output.trim().split('\n').filter(Boolean);- for (const pid of pids) {- try {- execSync(`kill -9 ${pid}`, { stdio: 'pipe' });- } catch {}- }+ exec(`kill -9 ${pid} 2>/dev/null`);}- } catch {}+ }++ // Wait for processes to die+ if (isWindows) {+ exec('ping -n 2 127.0.0.1 >nul');+ } else {+ spawnSync('sleep', ['0.5'], { stdio: 'pipe' });+ }++ // Verify port is free+ const remainingPids = getPids(port);+ return remainingPids.length === 0;}const PORTS = loadPorts();process.env.PYTHONUTF8 = '1';console.log('\n=== MachinaOS Starting ===\n');+log(`Platform: ${isWindows ? 'Windows' : isMac ? 'macOS' : 'Linux'}`);+log(`Ports: ${PORTS.join(', ')}`);// Step 1: Create .env if not existsconst envPath = resolve(ROOT, '.env');@@ -74,10 +161,26 @@ if (!existsSync(envPath) && existsSync(templatePath)) {// Step 2: Free portslog('Freeing ports...');+let allFree = true;for (const port of PORTS) {- killPort(port);+ const getPids = isWindows ? getPidsWindows : getPidsUnix;+ const pids = getPids(port);+ if (pids.length > 0) {+ const freed = killPort(port);+ if (freed) {+ log(` Port ${port}: Freed (killed PIDs: ${pids.join(', ')})`);+ } else {+ log(` Port ${port}: Warning - could not free`);+ allFree = false;+ }+ } else {+ log(` Port ${port}: Already free`);+ }+}++if (!allFree) {+ log('Warning: Some ports could not be freed. Services may fail to start.');}-log('Ports ready');// Step 3: Start dev serverlog('Starting services...');diff --git a/scripts/stop.js b/scripts/stop.jsindex 72d7150f..7020e872 100644--- a/scripts/stop.js+++ b/scripts/stop.js@@ -1,5 +1,5 @@#!/usr/bin/env node-import { execSync } from 'child_process';+import { execSync, spawnSync } from 'child_process';import { readFileSync, existsSync } from 'fs';import { resolve, dirname } from 'path';import { fileURLToPath } from 'url';@@ -7,6 +7,7 @@ import { fileURLToPath } from 'url';const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');const isWindows = process.platform === 'win32';+const isMac = process.platform === 'darwin';// Load ports from .envfunction loadPorts() {@@ -15,8 +16,10 @@ function loadPorts() {const content = readFileSync(envPath, 'utf-8');const env = {};for (const line of content.split('\n')) {- const [key, val] = line.split('=');- if (key && val) env[key.trim()] = val.trim();+ const match = line.match(/^([^#=]+)=(.*)$/);+ if (match) {+ env[match[1].trim()] = match[2].trim();+ }}return [parseInt(env.VITE_CLIENT_PORT) || 3000,@@ -25,68 +28,247 @@ function loadPorts() {];}-// Kill process by port using native commands-function killPort(port) {+// Execute command and return output (suppressing errors)+function exec(cmd, options = {}) {try {- if (isWindows) {- // Find PID using netstat and kill with taskkill- const output = execSync(`netstat -ano | findstr :${port} | findstr LISTENING`, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });- const lines = output.trim().split('\n');- const pids = new Set();- for (const line of lines) {- const parts = line.trim().split(/\s+/);- const pid = parts[parts.length - 1];- if (pid && !isNaN(parseInt(pid))) pids.add(pid);+ return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], ...options }).trim();+ } catch {+ return '';+ }+}++// Get PIDs using a port - Windows+function getPidsWindows(port) {+ const pids = new Set();++ // Method 1: netstat (most reliable)+ const netstatOutput = exec(`netstat -ano | findstr :${port} | findstr LISTENING`);+ for (const line of netstatOutput.split('\n')) {+ const parts = line.trim().split(/\s+/);+ const pid = parts[parts.length - 1];+ if (pid && /^\d+$/.test(pid) && pid !== '0') {+ pids.add(pid);+ }+ }++ // Method 2: PowerShell Get-NetTCPConnection (backup)+ if (pids.size === 0) {+ const psOutput = exec(`powershell -Command "Get-NetTCPConnection -LocalPort ${port} -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess"`);+ for (const pid of psOutput.split('\n')) {+ if (pid.trim() && /^\d+$/.test(pid.trim()) && pid.trim() !== '0') {+ pids.add(pid.trim());+ }+ }+ }++ return Array.from(pids);+}++// Get PIDs using a port - Unix (Linux/macOS)+function getPidsUnix(port) {+ const pids = new Set();++ // Method 1: lsof (most common)+ const lsofOutput = exec(`lsof -ti:${port} -sTCP:LISTEN 2>/dev/null`);+ for (const pid of lsofOutput.split('\n')) {+ if (pid.trim() && /^\d+$/.test(pid.trim())) {+ pids.add(pid.trim());+ }+ }++ // Method 2: ss (Linux, if lsof not available)+ if (pids.size === 0 && !isMac) {+ const ssOutput = exec(`ss -tlnp 2>/dev/null | grep :${port}`);+ const matches = ssOutput.matchAll(/pid=(\d+)/g);+ for (const match of matches) {+ pids.add(match[1]);+ }+ }++ // Method 3: fuser (Linux fallback)+ if (pids.size === 0 && !isMac) {+ const fuserOutput = exec(`fuser ${port}/tcp 2>/dev/null`);+ for (const pid of fuserOutput.split(/\s+/)) {+ if (pid.trim() && /^\d+$/.test(pid.trim())) {+ pids.add(pid.trim());}- for (const pid of pids) {- try {- execSync(`taskkill /PID ${pid} /F`, { stdio: 'pipe' });- } catch {}+ }+ }++ return Array.from(pids);+}++// Get child process PIDs - Windows+function getChildPidsWindows(parentPid) {+ const children = new Set();+ const wmicOutput = exec(`wmic process where (ParentProcessId=${parentPid}) get ProcessId 2>nul`);+ for (const line of wmicOutput.split('\n')) {+ const pid = line.trim();+ if (pid && /^\d+$/.test(pid)) {+ children.add(pid);+ // Recursively get grandchildren+ for (const grandchild of getChildPidsWindows(pid)) {+ children.add(grandchild);}- return pids.size > 0;- } else {- // Unix: use lsof and kill- const output = execSync(`lsof -ti:${port}`, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });- const pids = output.trim().split('\n').filter(Boolean);- for (const pid of pids) {- try {- execSync(`kill -9 ${pid}`, { stdio: 'pipe' });- } catch {}+ }+ }+ return Array.from(children);+}++// Get child process PIDs - Unix+function getChildPidsUnix(parentPid) {+ const children = new Set();+ // pgrep -P gets direct children+ const pgrepOutput = exec(`pgrep -P ${parentPid} 2>/dev/null`);+ for (const pid of pgrepOutput.split('\n')) {+ if (pid.trim() && /^\d+$/.test(pid.trim())) {+ children.add(pid.trim());+ // Recursively get grandchildren+ for (const grandchild of getChildPidsUnix(pid.trim())) {+ children.add(grandchild);}- return pids.length > 0;}- } catch {- return false;}+ return Array.from(children);}-// Kill processes by name-function killProcess(name) {- try {+// Kill a single PID - Windows+function killPidWindows(pid) {+ // First try graceful termination+ exec(`taskkill /PID ${pid} /T 2>nul`);+ // Then force kill+ exec(`taskkill /PID ${pid} /F /T 2>nul`);+}++// Kill a single PID - Unix+function killPidUnix(pid) {+ // First try SIGTERM (graceful)+ exec(`kill -15 ${pid} 2>/dev/null`);+ // Give it a moment+ spawnSync('sleep', ['0.1'], { stdio: 'pipe' });+ // Then SIGKILL (force)+ exec(`kill -9 ${pid} 2>/dev/null`);+}++// Check if process is still running+function isProcessRunning(pid) {+ if (isWindows) {+ const output = exec(`tasklist /FI "PID eq ${pid}" 2>nul`);+ return output.includes(pid);+ } else {+ const result = spawnSync('kill', ['-0', pid], { stdio: 'pipe' });+ return result.status === 0;+ }+}++// Kill all processes on a port with verification+function killPort(port) {+ const getPids = isWindows ? getPidsWindows : getPidsUnix;+ const getChildPids = isWindows ? getChildPidsWindows : getChildPidsUnix;+ const killPid = isWindows ? killPidWindows : killPidUnix;++ // Get all PIDs on this port+ let pids = getPids(port);+ if (pids.length === 0) {+ return { killed: false, pids: [], portFree: true, message: 'Free' };+ }++ // Collect all PIDs including children+ const allPids = new Set(pids);+ for (const pid of pids) {+ for (const childPid of getChildPids(pid)) {+ allPids.add(childPid);+ }+ }++ const pidsToKill = Array.from(allPids);+ const killedPids = [];++ // Kill all processes+ for (const pid of pidsToKill) {+ killPid(pid);+ }++ // Wait a bit for processes to die+ if (isWindows) {+ exec('ping -n 2 127.0.0.1 >nul'); // ~1 second delay+ } else {+ spawnSync('sleep', ['0.5'], { stdio: 'pipe' });+ }++ // Verify which ones are actually dead+ for (const pid of pidsToKill) {+ if (!isProcessRunning(pid)) {+ killedPids.push(pid);+ }+ }++ // Retry for stubborn processes+ const stillRunning = pidsToKill.filter(pid => isProcessRunning(pid));+ if (stillRunning.length > 0) {+ for (const pid of stillRunning) {+ // Force kill again+ if (isWindows) {+ exec(`taskkill /PID ${pid} /F /T 2>nul`);+ } else {+ exec(`kill -9 ${pid} 2>/dev/null`);+ }+ }++ // Final checkif (isWindows) {- execSync(`taskkill /IM ${name}.exe /F`, { stdio: 'pipe' });- return true;+ exec('ping -n 2 127.0.0.1 >nul');} else {- execSync(`pkill -9 ${name}`, { stdio: 'pipe' });- return true;+ spawnSync('sleep', ['0.5'], { stdio: 'pipe' });+ }++ for (const pid of stillRunning) {+ if (!isProcessRunning(pid)) {+ killedPids.push(pid);+ }}- } catch {- return false;}++ // Final verification - check if port is actually free+ const remainingPids = getPids(port);+ const portFree = remainingPids.length === 0;++ return {+ killed: killedPids.length > 0,+ pids: killedPids,+ portFree,+ message: portFree+ ? (killedPids.length > 0 ? `Killed ${killedPids.length} process(es)` : 'Free')+ : `Warning: Port still in use by PID(s): ${remainingPids.join(', ')}`+ };}+// Main executionconst PORTS = loadPorts();-console.log('Stopping all services...\n');+console.log('Stopping MachinaOs services...\n');+console.log(`Platform: ${isWindows ? 'Windows' : isMac ? 'macOS' : 'Linux'}`);+console.log(`Ports: ${PORTS.join(', ')}\n`);++let allStopped = true;-// Kill by portsfor (const port of PORTS) {- const killed = killPort(port);- console.log(`[Port ${port}] ${killed ? 'Killed' : 'Free'}`);+ const result = killPort(port);+ const status = result.portFree ? '[OK]' : '[!!]';+ console.log(`${status} Port ${port}: ${result.message}`);+ if (result.pids.length > 0) {+ console.log(` PIDs: ${result.pids.join(', ')}`);+ }+ if (!result.portFree) {+ allStopped = false;+ }}-// Kill Python/uvicorn-const pythonKilled = killProcess('python') || killProcess('uvicorn');-console.log(`[Python] ${pythonKilled ? 'Killed' : 'Not running'}`);--console.log('\nAll services stopped.');+console.log('');+if (allStopped) {+ console.log('All services stopped successfully.');+} else {+ console.log('Warning: Some ports may still be in use.');+ console.log('Try running the script again or manually kill the processes.');+ process.exit(1);+}diff --git a/server/core/logging.py b/server/core/logging.pyindex 6378d8a0..704020c7 100644--- a/server/core/logging.py+++ b/server/core/logging.py@@ -1,13 +1,152 @@-"""Modern structured logging configuration."""+"""Modern structured logging configuration with WebSocket broadcasting."""import sys+import asyncioimport structlogimport logging+from datetime import datetimefrom pathlib import Path-from typing import Any, Dict+from typing import Any, Dict, Optional+from queue import Queue+from threading import Threadfrom core.config import Settings+class WebSocketLogHandler(logging.Handler):+ """Logging handler that broadcasts logs to WebSocket clients.++ Uses a thread-safe queue to bridge sync logging with async WebSocket broadcasting.+ A background thread processes the queue and uses asyncio to broadcast.+ """++ _instance: Optional['WebSocketLogHandler'] = None++ def __init__(self, level: int = logging.INFO):+ super().__init__(level)+ self._queue: Queue = Queue(maxsize=1000) # Bounded queue to prevent memory issues+ self._running = False+ self._thread: Optional[Thread] = None+ self._loop: Optional[asyncio.AbstractEventLoop] = None++ # Source name mapping for cleaner display+ self._source_map = {+ 'services.workflow': 'workflow',+ 'services.ai': 'ai',+ 'services.android': 'android',+ 'routers.whatsapp': 'whatsapp',+ 'routers.android': 'android',+ 'routers.websocket': 'websocket',+ 'routers.workflow': 'workflow',+ 'services.execution': 'execution',+ 'services.deployment': 'deployment',+ '__main__': 'main',+ 'main': 'main',+ }++ @classmethod+ def get_instance(cls) -> Optional['WebSocketLogHandler']:+ """Get the singleton instance."""+ return cls._instance++ def emit(self, record: logging.LogRecord) -> None:+ """Queue log record for async broadcasting."""+ if not self._running:+ return++ try:+ # Get the raw message without structlog formatting+ message = record.getMessage()++ # Map source name+ source = record.name+ for prefix, mapped in self._source_map.items():+ if source.startswith(prefix):+ source = mapped+ break++ # Extract structured key-value pairs from structlog+ details = None+ if hasattr(record, '_logger') or hasattr(record, 'positional_args'):+ # Try to get extra kwargs from structlog+ extra_keys = set(record.__dict__.keys()) - {+ 'name', 'msg', 'args', 'created', 'filename', 'funcName',+ 'levelname', 'levelno', 'lineno', 'module', 'msecs',+ 'pathname', 'process', 'processName', 'relativeCreated',+ 'stack_info', 'exc_info', 'exc_text', 'thread', 'threadName',+ 'message', 'asctime', 'positional_args', '_logger'+ }+ if extra_keys:+ details = {k: record.__dict__[k] for k in extra_keys if not k.startswith('_')}++ # Create log entry+ log_data = {+ 'timestamp': datetime.now().isoformat(),+ 'level': record.levelname.lower(),+ 'message': message,+ 'source': source,+ }++ # Add details if present+ if details:+ log_data['details'] = details++ # Non-blocking put - drop if queue is full+ try:+ self._queue.put_nowait(log_data)+ except:+ pass # Drop log if queue is full++ except Exception:+ pass # Never fail in log handler++ def start(self, loop: asyncio.AbstractEventLoop) -> None:+ """Start the background thread for processing logs."""+ if self._running:+ return++ self._loop = loop+ self._running = True+ self._thread = Thread(target=self._process_queue, daemon=True)+ self._thread.start()+ WebSocketLogHandler._instance = self++ def stop(self) -> None:+ """Stop the background thread."""+ self._running = False+ WebSocketLogHandler._instance = None+ if self._thread:+ self._thread.join(timeout=1.0)++ def _process_queue(self) -> None:+ """Background thread that processes log queue and broadcasts."""+ while self._running:+ try:+ # Block for up to 0.1 seconds waiting for logs+ try:+ log_data = self._queue.get(timeout=0.1)+ except:+ continue++ # Schedule async broadcast on the event loop+ if self._loop and self._running:+ asyncio.run_coroutine_threadsafe(+ self._broadcast(log_data),+ self._loop+ )++ except Exception:+ pass # Never fail in background thread++ async def _broadcast(self, log_data: Dict[str, Any]) -> None:+ """Broadcast log to WebSocket clients."""+ try:+ from services.status_broadcaster import get_status_broadcaster+ broadcaster = get_status_broadcaster()+ await broadcaster.broadcast_terminal_log(log_data)+ except Exception:+ pass # Don't fail if broadcaster not ready++def configure_logging(settings: Settings) -> None:"""Configure structured logging based on settings."""@@ -113,4 +252,63 @@ def log_cache_operation(logger: structlog.BoundLogger, operation: str,if hit is not None:log_data["cache_hit"] = hit- logger.debug("Cache operation", **log_data)\ No newline at end of file+ logger.debug("Cache operation", **log_data)+++# Global WebSocket log handler instance+_ws_log_handler: Optional[WebSocketLogHandler] = None+++def setup_websocket_logging(loop: asyncio.AbstractEventLoop, level: int = logging.INFO) -> WebSocketLogHandler:+ """Setup and start the WebSocket log handler.++ Should be called during application startup after the event loop is running.++ Args:+ loop: The asyncio event loop to use for broadcasting+ level: Minimum log level to broadcast (default: INFO)++ Returns:+ The WebSocket log handler instance+ """+ global _ws_log_handler++ if _ws_log_handler is not None:+ return _ws_log_handler++ # Create handler+ _ws_log_handler = WebSocketLogHandler(level=level)++ # Add to root logger+ root_logger = logging.getLogger()+ root_logger.addHandler(_ws_log_handler)++ # Start the background processing thread+ _ws_log_handler.start(loop)++ return _ws_log_handler+++def shutdown_websocket_logging() -> None:+ """Shutdown the WebSocket log handler.++ Should be called during application shutdown.+ """+ global _ws_log_handler++ if _ws_log_handler is None:+ return++ # Stop the handler+ _ws_log_handler.stop()++ # Remove from root logger+ root_logger = logging.getLogger()+ root_logger.removeHandler(_ws_log_handler)++ _ws_log_handler = None+++def get_websocket_log_handler() -> Optional[WebSocketLogHandler]:+ """Get the current WebSocket log handler instance."""+ return _ws_log_handler\ No newline at end of filediff --git a/server/main.py b/server/main.pyindex 9177e998..2d2e19bc 100644--- a/server/main.py+++ b/server/main.py@@ -11,16 +11,52 @@ try:except ImportError:pass # Windows - uvloop not available, use default asyncio+import signal+import sys+import osfrom datetime import datetimefrom contextlib import asynccontextmanager+# Flag to track if shutdown was requested+_shutdown_requested = False+++def handle_signal(signum, frame):+ """Handle SIGTERM/SIGINT for clean shutdown.++ Note: We don't call sys.exit() here because it raises SystemExit+ in the middle of async operations, causing cascading errors.+ Instead, we set a flag and let uvicorn handle the graceful shutdown.+ """+ global _shutdown_requested+ if not _shutdown_requested:+ _shutdown_requested = True+ print(f"\nReceived signal {signum}, shutting down gracefully...")+ # Raise KeyboardInterrupt to trigger uvicorn's graceful shutdown+ # This is the proper way to signal shutdown in async context+ raise KeyboardInterrupt()+++# Register signal handlers for clean shutdown+# Note: uvicorn already handles SIGINT/SIGTERM, but we add our own+# to provide a cleaner message and avoid the "Terminate batch job" prompt on Windows+if sys.platform != 'win32':+ # Unix: handle both SIGTERM (docker stop) and SIGINT (Ctrl+C)+ signal.signal(signal.SIGTERM, handle_signal)+ # Don't override SIGINT on Unix - let uvicorn handle it+else:+ # Windows: Don't override signal handlers - let uvicorn handle Ctrl+C+ # The "Terminate batch job" prompt is a Windows cmd.exe behavior+ # that we can't avoid from Python+ pass+from fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import ORJSONResponsefrom core.container import containerfrom core.config import Settings-from core.logging import configure_logging, get_logger+from core.logging import configure_logging, get_logger, setup_websocket_logging, shutdown_websocket_loggingfrom routers import workflow, database, maps, nodejs_compat, android, websocket, webhook, auth# Initialize settings and logging@@ -92,10 +128,23 @@ async def lifespan(app: FastAPI):await recovery_sweeper.start()logger.info("Execution recovery sweeper started")+ # Start WebSocket logging handler to broadcast logs to frontend+ import asyncio+ loop = asyncio.get_running_loop()+ setup_websocket_logging(loop)+ logger.info("WebSocket logging handler started")+logger.info("Services started successfully")yield# Shutdown+ # Stop WebSocket logging handler+ shutdown_websocket_logging()++ # Close Android relay client (prevents "Unclosed client session" warning)+ from services.android.manager import close_relay_client+ await close_relay_client(clear_stored_session=False)+# Stop recovery sweeper firstif settings.redis_enabled:await recovery_sweeper.stop()diff --git a/server/requirements.txt b/server/requirements.txtindex 49dd077b..7a2312b1 100644--- a/server/requirements.txt+++ b/server/requirements.txt@@ -28,6 +28,9 @@ httpx>=0.27.0# WebSocket Client (for WhatsApp RPC)websockets>=12.0+# Async HTTP Client (for Android relay WebSocket)+aiohttp>=3.9.0+# QR Code Generation (for WhatsApp QR display)qrcode[pil]>=7.4.0@@ -61,3 +64,6 @@ bcrypt>=4.1.0python-jose[cryptography]>=3.3.0email-validator>=2.0.0+# Timezone support+pytz>=2024.1+diff --git a/server/routers/websocket.py b/server/routers/websocket.pyindex 155bb3e6..6f833941 100644--- a/server/routers/websocket.py+++ b/server/routers/websocket.py@@ -623,13 +623,21 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->edges = data.get("edges", [])session_id = data.get("session_id", "default")- # DEBUG: Log received edges to trace toolkit connection issues+ # DEBUG: Log received edges to trace tool connection issueslogger.info(f"[Deploy] Received {len(edges)} edges for workflow {workflow_id}")- toolkit_edges = [e for e in edges if 'androidTool' in (e.get('source', '') + e.get('target', ''))]- if toolkit_edges:- logger.info(f"[Deploy] Android Toolkit edges: {toolkit_edges}")+ for e in edges:+ target_handle = e.get('targetHandle')+ if target_handle and target_handle.startswith('input-') and target_handle != 'input-main':+ logger.info(f"[Deploy] Config edge: {e.get('source')} -> {e.get('target')} (handle={target_handle})")++ # Check for tool connections to AI Agent+ tool_edges = [e for e in edges if e.get('targetHandle') == 'input-tools']+ if tool_edges:+ logger.info(f"[Deploy] Tool edges found: {len(tool_edges)}")+ for te in tool_edges:+ logger.info(f"[Deploy] Tool edge: source={te.get('source')} -> target={te.get('target')}")else:- logger.info(f"[Deploy] No Android Toolkit edges found. All edges: {edges}")+ logger.info(f"[Deploy] No input-tools edges found")if not nodes:return {"success": False, "error": "No nodes provided"}@@ -1422,6 +1430,26 @@ async def handle_send_chat_message(data: Dict[str, Any], websocket: WebSocket) -}+# ============================================================================+# Terminal Logs Handlers+# ============================================================================++@ws_handler()+async def handle_get_terminal_logs(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Get terminal log history."""+ broadcaster = get_status_broadcaster()+ logs = broadcaster.get_terminal_logs()+ return {"success": True, "logs": logs}+++@ws_handler()+async def handle_clear_terminal_logs(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Clear terminal log history."""+ broadcaster = get_status_broadcaster()+ await broadcaster.clear_terminal_logs()+ return {"success": True, "message": "Terminal logs cleared"}++# ============================================================================# Message Router# ============================================================================@@ -1511,6 +1539,10 @@ MESSAGE_HANDLERS: Dict[str, MessageHandler] = {# Chat message (for chatTrigger nodes)"send_chat_message": handle_send_chat_message,++ # Terminal logs+ "get_terminal_logs": handle_get_terminal_logs,+ "clear_terminal_logs": handle_clear_terminal_logs,}diff --git a/server/services/ai.py b/server/services/ai.pyindex 4512688f..2ba763a8 100644--- a/server/services/ai.py+++ b/server/services/ai.py@@ -11,8 +11,15 @@ from langchain_openai import ChatOpenAIfrom langchain_anthropic import ChatAnthropicfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_groq import ChatGroq-from langchain_cerebras import ChatCerebrasfrom langchain_core.messages import HumanMessage, SystemMessage, AIMessage, BaseMessage, ToolMessage++# Conditional import for Cerebras (requires Python <3.13)+try:+ from langchain_cerebras import ChatCerebras+ CEREBRAS_AVAILABLE = True+except ImportError:+ ChatCerebras = None+ CEREBRAS_AVAILABLE = Falsefrom langchain_core.tools import StructuredToolfrom langgraph.graph import StateGraph, ENDfrom pydantic import BaseModel, Field, create_model@@ -122,7 +129,11 @@ PROVIDER_CONFIGS: Dict[str, ProviderConfig] = {models_endpoint='https://api.groq.com/openai/v1/models',models_header_fn=_groq_headers),- 'cerebras': ProviderConfig(+}++# Add Cerebras only if available (requires Python <3.13)+if CEREBRAS_AVAILABLE:+ PROVIDER_CONFIGS['cerebras'] = ProviderConfig(name='cerebras',model_class=ChatCerebras,api_key_param='api_key',@@ -131,8 +142,7 @@ PROVIDER_CONFIGS: Dict[str, ProviderConfig] = {default_model='llama-3.3-70b',models_endpoint='https://api.cerebras.ai/v1/models',models_header_fn=_cerebras_headers- ),-}+ )def detect_provider_from_model(model: str) -> str:@@ -367,11 +377,12 @@ def create_tool_node(tool_executor: Callable):tool_args = tool_call.get("args", {})tool_id = tool_call.get("id", "")- logger.debug(f"[LangGraph] Executing tool: {tool_name}")+ logger.info(f"[LangGraph] Executing tool: {tool_name} (args={tool_args})")try:# Directly await the async tool executor (proper async pattern)result = await tool_executor(tool_name, tool_args)+ logger.info(f"[LangGraph] Tool {tool_name} returned: {str(result)[:100]}")except Exception as e:logger.error(f"[LangGraph] Tool execution failed: {tool_name}", error=str(e))result = {"error": str(e)}@@ -383,7 +394,7 @@ def create_tool_node(tool_executor: Callable):name=tool_name))- logger.debug(f"[LangGraph] Tool {tool_name} completed")+ logger.info(f"[LangGraph] Tool {tool_name} completed, result added to messages")return {"messages": tool_messages,@@ -825,6 +836,12 @@ class AIService:provider = 'unknown'model = 'unknown'+ # EARLY LOG: Entry point for debugging+ logger.info(f"[AIAgent] execute_agent called: node_id={node_id}, workflow_id={workflow_id}, tool_data_count={len(tool_data) if tool_data else 0}")+ if tool_data:+ for i, td in enumerate(tool_data):+ logger.info(f"[AIAgent] Tool {i}: type={td.get('node_type')}, node_id={td.get('node_id')}")+# Helper to broadcast status updates with workflow_id for proper scopingasync def broadcast_status(phase: str, details: Dict[str, Any] = None):if broadcaster:@@ -936,6 +953,7 @@ class AIService:if tool:tools.append(tool)tool_configs[tool.name] = config+ logger.info(f"[LangGraph] Registered tool: name={tool.name}, node_id={config.get('node_id')}")logger.debug(f"[LangGraph] Built {len(tools)} tools")@@ -947,7 +965,7 @@ class AIService:config = tool_configs.get(tool_name, {})tool_node_id = config.get('node_id')- logger.debug(f"[LangGraph] tool_executor: {tool_name}")+ logger.info(f"[LangGraph] tool_executor called: tool_name={tool_name}, node_id={tool_node_id}, workflow_id={workflow_id}")# Broadcast executing status to the AI Agent nodeawait broadcast_status("executing_tool", {@@ -967,25 +985,46 @@ class AIService:# Include workflow_id in config so tool handlers can broadcast with proper scopingconfig['workflow_id'] = workflow_id- result = await execute_tool(tool_name, tool_args, config)-- # Broadcast completion to AI Agent node- await broadcast_status("tool_completed", {- "message": f"Tool completed: {tool_name}",- "tool_name": tool_name,- "result_preview": str(result)[:100]- })-- # Broadcast success status to the tool node- if tool_node_id and broadcaster:- await broadcaster.update_node_status(- tool_node_id,- "success",- {"message": f"{tool_name} completed", "result": result},- workflow_id=workflow_id- )- return result+ try:+ result = await execute_tool(tool_name, tool_args, config)++ # Broadcast completion to AI Agent node+ await broadcast_status("tool_completed", {+ "message": f"Tool completed: {tool_name}",+ "tool_name": tool_name,+ "result_preview": str(result)[:100]+ })++ # Broadcast success status to the tool node+ if tool_node_id and broadcaster:+ logger.info(f"[LangGraph] Broadcasting success to tool node: node_id={tool_node_id}, workflow_id={workflow_id}")+ await broadcaster.update_node_status(+ tool_node_id,+ "success",+ {"message": f"{tool_name} completed", "result": result},+ workflow_id=workflow_id+ )+ else:+ logger.warning(f"[LangGraph] Cannot broadcast success: tool_node_id={tool_node_id}, broadcaster={broadcaster is not None}")++ return result++ except Exception as e:+ error_msg = str(e)+ logger.error(f"[LangGraph] Tool execution failed: {tool_name}", error=error_msg)++ # Broadcast error status to the tool node so UI shows failure+ if tool_node_id and broadcaster:+ await broadcaster.update_node_status(+ tool_node_id,+ "error",+ {"message": f"{tool_name} failed", "error": error_msg},+ workflow_id=workflow_id+ )++ # Re-raise to let LangGraph handle the error+ raise# Broadcast: Building graphawait broadcast_status("building_graph", {@@ -1197,7 +1236,7 @@ class AIService:'connected_services': connected_services # Pass through for execution}- logger.debug(f"[LangGraph] Built tool '{tool_name}'")+ logger.debug(f"[LangGraph] Built tool '{tool_name}' with node_id={node_id}")return tool, configexcept Exception as e:diff --git a/server/services/handlers/ai.py b/server/services/handlers/ai.pyindex 812293b7..9070cb5b 100644--- a/server/services/handlers/ai.py+++ b/server/services/handlers/ai.py@@ -39,9 +39,19 @@ async def handle_ai_agent(memory_data = Nonetool_data: List[Dict[str, Any]] = [] # Collect connected tool nodes- logger.debug(f"[AI Agent] Processing node {node_id}, edges={len(edges) if edges else 0}, nodes={len(nodes) if nodes else 0}")+ logger.info(f"[AI Agent] Processing node {node_id}, edges={len(edges) if edges else 0}, nodes={len(nodes) if nodes else 0}, workflow_id={workflow_id}")if edges and nodes:+ # Log all edges targeting this AI Agent for debugging+ incoming_edges = [e for e in edges if e.get('target') == node_id]+ logger.info(f"[AI Agent] Incoming edges to {node_id}: {len(incoming_edges)}")+ for e in incoming_edges:+ logger.info(f"[AI Agent] Edge: source={e.get('source')}, targetHandle={e.get('targetHandle')}")++ # Check for tool edges specifically+ tool_incoming = [e for e in incoming_edges if e.get('targetHandle') == 'input-tools']+ logger.info(f"[AI Agent] Tool edges (input-tools handle): {len(tool_incoming)}")+for edge in edges:if edge.get('target') != node_id:continue@@ -70,6 +80,7 @@ async def handle_ai_agent(# Tool detection (new) - any node connected to input-tools becomes a toolelif target_handle == 'input-tools':tool_type = source_node.get('type')+ logger.info(f"[AI Agent] Found tool connected via input-tools: type={tool_type}, node_id={source_node_id}")tool_params = await database.get_node_parameters(source_node_id) or {}# Build base tool entry@@ -118,6 +129,11 @@ async def handle_ai_agent(tool_data.append(tool_entry)logger.debug(f"AI Agent connected tool: {tool_type}")+ # Log tool data collection results+ logger.info(f"[AI Agent Handler] Collected tools: count={len(tool_data)}, workflow_id={workflow_id}")+ for td in tool_data:+ logger.info(f"[AI Agent Handler] Tool: type={td.get('node_type')}, node_id={td.get('node_id')}")+# Get broadcaster for real-time status updatesfrom services.status_broadcaster import get_status_broadcasterbroadcaster = get_status_broadcaster()diff --git a/server/services/status_broadcaster.py b/server/services/status_broadcaster.pyindex aa4156bc..14143294 100644--- a/server/services/status_broadcaster.py+++ b/server/services/status_broadcaster.py@@ -702,6 +702,51 @@ class StatusBroadcaster:"workflow_id": workflow_id})+ # =========================================================================+ # Terminal Log Updates+ # =========================================================================++ async def broadcast_terminal_log(self, log_data: Dict[str, Any]):+ """Broadcast a terminal log entry to all connected clients.++ Used by the WebSocket logging handler to stream server logs to the frontend.++ Args:+ log_data: Dict containing:+ - timestamp: ISO timestamp+ - level: Log level (debug, info, warning, error)+ - message: The log message+ - source: Logger name/module (e.g., 'workflow', 'ai', 'android')+ - details: Optional additional context+ """+ # Initialize terminal logs if not present+ if "terminal_logs" not in self._status:+ self._status["terminal_logs"] = []++ # Add to terminal log history (keep last 200 entries)+ self._status["terminal_logs"].append(log_data)+ if len(self._status["terminal_logs"]) > 200:+ self._status["terminal_logs"] = self._status["terminal_logs"][-200:]++ # Broadcast to all clients+ await self.broadcast({+ "type": "terminal_log",+ "data": log_data+ })++ def get_terminal_logs(self) -> List[Dict[str, Any]]:+ """Get terminal log history."""+ if "terminal_logs" not in self._status:+ return []+ return list(self._status["terminal_logs"])++ async def clear_terminal_logs(self):+ """Clear terminal log history."""+ self._status["terminal_logs"] = []+ await self.broadcast({+ "type": "terminal_logs_cleared"+ })+# =========================================================================# Generic Updates# =========================================================================