Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Add AI thinking/reasoning support for Claude, Gemini, OpenAI and Groq models
trohitgf16e87612d78
diff --git a/client/src/components/parameterPanel/InputSection.tsx b/client/src/components/parameterPanel/InputSection.tsxindex d341ed40..f573c9b0 100644--- a/client/src/components/parameterPanel/InputSection.tsx+++ b/client/src/components/parameterPanel/InputSection.tsx@@ -152,7 +152,7 @@ const InputSection: React.FC<InputSectionProps> = ({ nodeId, visible = true }) =} else {const sampleSchemas: Record<string, Record<string, any>> = {location: { latitude: 'number', longitude: 'number', accuracy: 'number', provider: 'string', altitude: 'number' },- ai: { response: 'string', model: 'string', provider: 'string', finish_reason: 'string', timestamp: 'string' },+ ai: { response: 'string', thinking: 'string', model: 'string', provider: 'string', finish_reason: 'string', timestamp: 'string' },file: { fileName: 'string', fileSize: 'number' },whatsapp: {message_id: 'string',diff --git a/client/src/components/ui/NodeOutputPanel.tsx b/client/src/components/ui/NodeOutputPanel.tsxindex b3b94f1a..9714b4bc 100644--- a/client/src/components/ui/NodeOutputPanel.tsx+++ b/client/src/components/ui/NodeOutputPanel.tsx@@ -21,6 +21,83 @@ const highlightJSON = (json: string, dracula: any): React.ReactNode => {});};+// Collapsible thinking/reasoning block for AI model responses+interface ThinkingBlockProps {+ thinking: string;+ provider?: string;+ theme: ReturnType<typeof useAppTheme>;+}++const ThinkingBlock: React.FC<ThinkingBlockProps> = ({ thinking, provider, theme }) => {+ const [isExpanded, setIsExpanded] = useState(true);++ if (!thinking || !thinking.trim()) return null;++ return (+ <div style={{+ marginBottom: theme.spacing.md,+ backgroundColor: theme.colors.backgroundElevated,+ border: `1px solid ${theme.dracula.purple}40`,+ borderRadius: theme.borderRadius.md,+ borderLeft: `3px solid ${theme.dracula.purple}`,+ overflow: 'hidden',+ }}>+ <div+ onClick={() => setIsExpanded(!isExpanded)}+ style={{+ padding: theme.spacing.md,+ cursor: 'pointer',+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'space-between',+ backgroundColor: `${theme.dracula.purple}10`,+ transition: theme.transitions.fast,+ }}+ >+ <div style={{ display: 'flex', alignItems: 'center', gap: theme.spacing.sm }}>+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={theme.dracula.purple} strokeWidth="2">+ <circle cx="12" cy="12" r="10"/>+ <path d="M12 6v6l4 2"/>+ </svg>+ <span style={{+ color: theme.dracula.purple,+ fontWeight: theme.fontWeight.semibold,+ fontSize: theme.fontSize.xs,+ textTransform: 'uppercase',+ letterSpacing: '0.1em',+ }}>+ Thinking Process {provider && `(${provider})`}+ </span>+ </div>+ <svg+ width="12" height="12" viewBox="0 0 24 24" fill="none"+ stroke={theme.dracula.purple} strokeWidth="2"+ style={{ transform: isExpanded ? 'rotate(90deg)' : 'rotate(0deg)', transition: 'transform 0.2s ease' }}+ >+ <polyline points="9 18 15 12 9 6"/>+ </svg>+ </div>+ {isExpanded && (+ <pre style={{+ padding: theme.spacing.md,+ margin: 0,+ whiteSpace: 'pre-wrap',+ wordBreak: 'break-word',+ fontSize: theme.fontSize.sm,+ color: theme.colors.textSecondary,+ fontFamily: '"Fira Code", Monaco, Menlo, monospace',+ lineHeight: 1.6,+ maxHeight: '300px',+ overflow: 'auto',+ backgroundColor: theme.colors.background,+ }}>+ {thinking}+ </pre>+ )}+ </div>+ );+};+interface NodeOutputPanelProps {results: ExecutionResult[];onClear?: () => void;@@ -78,6 +155,20 @@ const NodeOutputPanel: React.FC<NodeOutputPanelProps> = ({return parsed;};+ // Extract thinking content from result data+ const getThinkingContent = (result: ExecutionResult): { thinking: string | null; provider?: string } => {+ const data = getOutputData(result);+ // Check nested result structure first (from backend)+ if (data?.result?.thinking) {+ return { thinking: data.result.thinking, provider: data.result.provider };+ }+ // Then top-level+ if (data?.thinking) {+ return { thinking: data.thinking, provider: data?.provider };+ }+ return { thinking: null };+ };+// Structured output types for different node responsestype AndroidOutput = { type: 'android'; data: any; service?: string; action?: string };type WhatsAppHistoryOutput = { type: 'whatsapp_history'; messages: any[]; total: number; count: number; hasMore: boolean };@@ -89,6 +180,7 @@ const NodeOutputPanel: React.FC<NodeOutputPanelProps> = ({// Extract the main response text from execution resultsconst getMainResponse = (result: ExecutionResult): string | StructuredOutput | null => {const data = getOutputData(result);+// Python node outputif (data?.output !== undefined) {return typeof data.output === 'string' ? data.output : JSON.stringify(data.output, null, 2);@@ -378,6 +470,12 @@ const NodeOutputPanel: React.FC<NodeOutputPanelProps> = ({</div>)}+ {/* Thinking/Reasoning Block (shown before main response) */}+ {(() => {+ const { thinking, provider } = getThinkingContent(latestResult);+ return thinking ? <ThinkingBlock thinking={thinking} provider={provider} theme={theme} /> : null;+ })()}+{/* Main Response */}{mainResponse && typeof mainResponse === 'object' && (mainResponse as any).type === 'android' ? (/* Android Service Response */diff --git a/client/src/factories/baseChatModelFactory.ts b/client/src/factories/baseChatModelFactory.tsindex 457d3cdb..1391757d 100644--- a/client/src/factories/baseChatModelFactory.ts+++ b/client/src/factories/baseChatModelFactory.ts@@ -191,5 +191,66 @@ export const STANDARD_PARAMETERS = {default: 0,typeOptions: { minValue: -2, maxValue: 2, numberStepSize: 0.1 },description: 'Penalty for presence of tokens'+ },++ // Thinking/Reasoning parameters+ thinkingEnabled: {+ displayName: 'Thinking/Reasoning Mode',+ name: 'thinkingEnabled',+ type: 'boolean' as const,+ default: false,+ description: 'Enable extended thinking for supported models'+ },++ thinkingBudget: {+ displayName: 'Thinking Budget (Tokens)',+ name: 'thinkingBudget',+ type: 'number' as const,+ default: 2048,+ typeOptions: { minValue: 1024, maxValue: 16000 },+ description: 'Maximum tokens for thinking (Claude, Gemini 2.5)',+ displayOptions: { show: { thinkingEnabled: [true] } }+ },++ reasoningEffort: {+ displayName: 'Reasoning Effort',+ name: 'reasoningEffort',+ type: 'options' as const,+ options: [+ { name: 'Minimal', value: 'minimal' },+ { name: 'Low', value: 'low' },+ { name: 'Medium', value: 'medium' },+ { name: 'High', value: 'high' }+ ],+ default: 'medium',+ description: 'Reasoning effort level (OpenAI o-series)',+ displayOptions: { show: { thinkingEnabled: [true] } }+ },++ thinkingLevel: {+ displayName: 'Thinking Level',+ name: 'thinkingLevel',+ type: 'options' as const,+ options: [+ { name: 'Low', value: 'low' },+ { name: 'Medium', value: 'medium' },+ { name: 'High', value: 'high' }+ ],+ default: 'medium',+ description: 'Thinking depth (Gemini 3+)',+ displayOptions: { show: { thinkingEnabled: [true] } }+ },++ reasoningFormat: {+ displayName: 'Reasoning Format',+ name: 'reasoningFormat',+ type: 'options' as const,+ options: [+ { name: 'Parsed (show reasoning)', value: 'parsed' },+ { name: 'Hidden (final answer only)', value: 'hidden' }+ ],+ default: 'parsed',+ description: 'How to return reasoning content (Groq)',+ displayOptions: { show: { thinkingEnabled: [true] } }}};\ No newline at end of filediff --git a/client/src/nodeDefinitions/aiAgentNodes.ts b/client/src/nodeDefinitions/aiAgentNodes.tsindex f49d2cff..7553233b 100644--- a/client/src/nodeDefinitions/aiAgentNodes.ts+++ b/client/src/nodeDefinitions/aiAgentNodes.ts@@ -118,6 +118,35 @@ export const aiAgentNodes: Record<string, INodeTypeDescription> = {default: 1000,typeOptions: { minValue: 1, maxValue: 8192 },description: 'Maximum number of tokens to generate'+ },+ {+ displayName: 'Thinking/Reasoning',+ name: 'thinkingEnabled',+ type: 'boolean',+ default: false,+ description: 'Enable extended thinking for supported providers (Anthropic, OpenAI o-series, Gemini, Groq, Cerebras)'+ },+ {+ displayName: 'Thinking Budget',+ name: 'thinkingBudget',+ type: 'number',+ default: 2048,+ typeOptions: { minValue: 1024, maxValue: 16000 },+ description: 'Token budget for thinking (Claude, Gemini, Cerebras)',+ displayOptions: { show: { thinkingEnabled: [true] } }+ },+ {+ displayName: 'Reasoning Effort',+ name: 'reasoningEffort',+ type: 'options',+ options: [+ { name: 'Low', value: 'low' },+ { name: 'Medium', value: 'medium' },+ { name: 'High', value: 'high' }+ ],+ default: 'medium',+ description: 'Reasoning effort level (OpenAI o-series, Groq)',+ displayOptions: { show: { thinkingEnabled: [true] } }}] as any}diff --git a/client/src/nodeDefinitions/aiModelNodes.ts b/client/src/nodeDefinitions/aiModelNodes.tsindex 62e8d7da..41822c68 100644--- a/client/src/nodeDefinitions/aiModelNodes.ts+++ b/client/src/nodeDefinitions/aiModelNodes.ts@@ -16,7 +16,12 @@ const openaiConfig: ChatModelConfig = {models: [], // Models fetched dynamically via APIparameters: [STANDARD_PARAMETERS.frequencyPenalty,- STANDARD_PARAMETERS.maxTokens,+ {+ ...STANDARD_PARAMETERS.maxTokens,+ default: 4096,+ typeOptions: { minValue: 1, maxValue: 128000 },+ description: 'Maximum tokens to generate'+ },{displayName: 'Response Format',name: 'responseFormat',@@ -35,7 +40,10 @@ const openaiConfig: ChatModelConfig = {description: 'Format of the response'},STANDARD_PARAMETERS.presencePenalty,- STANDARD_PARAMETERS.temperature,+ {+ ...STANDARD_PARAMETERS.temperature,+ description: 'Controls randomness (0-2). Note: O-series models (o1, o3, o4) only support temperature=1, which is set automatically.'+ },{displayName: 'Timeout',name: 'timeout',@@ -58,7 +66,10 @@ const openaiConfig: ChatModelConfig = {},description: 'Maximum number of retries'},- STANDARD_PARAMETERS.topP+ STANDARD_PARAMETERS.topP,+ // Thinking/Reasoning for o-series models (o1, o3, o4)+ STANDARD_PARAMETERS.thinkingEnabled,+ STANDARD_PARAMETERS.reasoningEffort]};@@ -73,11 +84,14 @@ const claudeConfig: ChatModelConfig = {parameters: [{...STANDARD_PARAMETERS.maxTokens,- typeOptions: { minValue: 1, maxValue: 8192 }+ default: 4096, // Higher default for Claude to accommodate thinking mode+ typeOptions: { minValue: 1, maxValue: 64000 },+ description: 'Maximum tokens to generate. Must be greater than Thinking Budget when thinking is enabled.'},{...STANDARD_PARAMETERS.temperature,- typeOptions: { minValue: 0, maxValue: 1, numberStepSize: 0.1 }+ typeOptions: { minValue: 0, maxValue: 1, numberStepSize: 0.1 },+ description: 'Controls randomness (0-1). Note: Automatically set to 1 when thinking mode is enabled.'},STANDARD_PARAMETERS.topP,STANDARD_PARAMETERS.topK,@@ -102,6 +116,12 @@ const claudeConfig: ChatModelConfig = {maxValue: 5},description: 'Maximum number of retries'+ },+ // Extended thinking for Claude models+ STANDARD_PARAMETERS.thinkingEnabled,+ {+ ...STANDARD_PARAMETERS.thinkingBudget,+ description: 'Token budget for thinking (1024-16000). Must be less than Maximum Tokens.'}]};@@ -115,7 +135,12 @@ const geminiConfig: ChatModelConfig = {description: 'Google Gemini models for multimodal AI capabilities',models: [], // Models fetched dynamically via APIparameters: [- STANDARD_PARAMETERS.maxTokens,+ {+ ...STANDARD_PARAMETERS.maxTokens,+ default: 4096,+ typeOptions: { minValue: 1, maxValue: 65536 },+ description: 'Maximum tokens to generate'+ },{...STANDARD_PARAMETERS.temperature,default: 0.9,@@ -168,6 +193,12 @@ const geminiConfig: ChatModelConfig = {maxValue: 5},description: 'Maximum number of retries'+ },+ // Thinking for Gemini models - uses thinking_budget (token count)+ STANDARD_PARAMETERS.thinkingEnabled,+ {+ ...STANDARD_PARAMETERS.thinkingBudget,+ description: 'Token budget for thinking. Works with Gemini 2.5 Flash/Pro and Flash Thinking models.'}]};@@ -244,7 +275,10 @@ const groqConfig: ChatModelConfig = {maxValue: 5},description: 'Maximum number of retries'- }+ },+ // Reasoning for Groq models (Qwen3, QwQ)+ STANDARD_PARAMETERS.thinkingEnabled,+ STANDARD_PARAMETERS.reasoningFormat]};@@ -281,7 +315,10 @@ const cerebrasConfig: ChatModelConfig = {maxValue: 5},description: 'Maximum number of retries'- }+ },+ // Thinking for Cerebras models (Qwen, Llama)+ STANDARD_PARAMETERS.thinkingEnabled,+ STANDARD_PARAMETERS.thinkingBudget]};diff --git a/scripts/start.js b/scripts/start.jsindex c2a3b842..acaa7656 100644--- a/scripts/start.js+++ b/scripts/start.js@@ -7,7 +7,10 @@ import { readFileSync, existsSync, copyFileSync } from 'fs';const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');const START = Date.now();-const isWindows = process.platform === 'win32';++// Detect environment: Git Bash on Windows reports win32 but uses Unix commands+const isGitBash = process.platform === 'win32' && (process.env.MSYSTEM || process.env.SHELL?.includes('bash'));+const isWindows = process.platform === 'win32' && !isGitBash;const isMac = process.platform === 'darwin';// Timing helper@@ -123,10 +126,10 @@ function killPort(port) {}}- // Kill all processes+ // Kill all processes (no /T flag to avoid killing parent shell)for (const pid of allPids) {if (isWindows) {- exec(`taskkill /PID ${pid} /F /T 2>nul`);+ exec(`taskkill /PID ${pid} /F 2>nul`);} else {exec(`kill -9 ${pid} 2>/dev/null`);}@@ -148,7 +151,8 @@ const PORTS = loadPorts();process.env.PYTHONUTF8 = '1';console.log('\n=== MachinaOS Starting ===\n');-log(`Platform: ${isWindows ? 'Windows' : isMac ? 'macOS' : 'Linux'}`);+const platformName = isWindows ? 'Windows' : isGitBash ? 'Git Bash' : isMac ? 'macOS' : 'Linux';+log(`Platform: ${platformName}`);log(`Ports: ${PORTS.join(', ')}`);// Step 1: Create .env if not exists@@ -184,4 +188,37 @@ if (!allFree) {// Step 3: Start dev serverlog('Starting services...');-spawn('npm', ['run', 'dev'], { cwd: ROOT, stdio: 'inherit', shell: true });+log('Press Ctrl+C to stop (use npm run stop to kill all services)');+log('');++// On Git Bash/mintty, Ctrl+C doesn't propagate properly to Node.js child processes+// due to Cygwin pseudo-terminal limitations. The workaround is to run concurrently+// directly without a Node.js wrapper.+//+// This script's job is done - it freed ports and set up env.+// Now exec into npm run dev so the terminal controls it directly.++if (isGitBash) {+ // For Git Bash: Use exec to replace this process with npm+ // This way Ctrl+C goes directly to npm/concurrently, not through Node.js+ const { execFileSync } = await import('child_process');+ try {+ // Use bash -c to run npm, replacing this process+ execFileSync('bash', ['-c', 'exec npm run dev'], {+ cwd: ROOT,+ stdio: 'inherit',+ windowsHide: false+ });+ } catch {+ // Normal exit from Ctrl+C+ }+} else {+ // For native Windows cmd or Unix: execSync works fine+ try {+ execSync('npm run dev', { cwd: ROOT, stdio: 'inherit' });+ } catch {+ // Ctrl+C causes non-zero exit, which is normal+ }+}++console.log('\nDev server stopped.');diff --git a/scripts/stop.js b/scripts/stop.jsindex 7020e872..5701f585 100644--- a/scripts/stop.js+++ b/scripts/stop.js@@ -1,12 +1,15 @@#!/usr/bin/env node-import { execSync, spawnSync } from 'child_process';+import { execSync } from 'child_process';import { readFileSync, existsSync } from 'fs';import { resolve, dirname } from 'path';import { fileURLToPath } from 'url';const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');-const isWindows = process.platform === 'win32';++// Detect environment: Git Bash on Windows reports win32 but uses Unix commands+const isGitBash = process.platform === 'win32' && (process.env.MSYSTEM || process.env.SHELL?.includes('bash'));+const isWindows = process.platform === 'win32' && !isGitBash;const isMac = process.platform === 'darwin';// Load ports from .env@@ -29,9 +32,15 @@ function loadPorts() {}// Execute command and return output (suppressing errors)+// Added timeout to prevent hangingfunction exec(cmd, options = {}) {try {- return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], ...options }).trim();+ return execSync(cmd, {+ encoding: 'utf-8',+ stdio: ['pipe', 'pipe', 'pipe'],+ timeout: 10000, // 10 second timeout to prevent hanging+ ...options+ }).trim();} catch {return '';}@@ -133,11 +142,18 @@ function getChildPidsUnix(parentPid) {}// Kill a single PID - Windows+// NOTE: Removed /T flag (tree kill) as it can kill the parent PowerShell terminal+// Child processes are killed explicitly via getChildPidsWindows() insteadfunction killPidWindows(pid) {- // First try graceful termination- exec(`taskkill /PID ${pid} /T 2>nul`);- // Then force kill- exec(`taskkill /PID ${pid} /F /T 2>nul`);+ // First try graceful termination (no /T)+ exec(`taskkill /PID ${pid} 2>nul`);+ // Then force kill (no /T)+ exec(`taskkill /PID ${pid} /F 2>nul`);+}++// Cross-platform sleep using Atomics (efficient, no busy wait)+function sleepMs(ms) {+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);}// Kill a single PID - Unix@@ -145,7 +161,7 @@ function killPidUnix(pid) {// First try SIGTERM (graceful)exec(`kill -15 ${pid} 2>/dev/null`);// Give it a moment- spawnSync('sleep', ['0.1'], { stdio: 'pipe' });+ sleepMs(100);// Then SIGKILL (force)exec(`kill -9 ${pid} 2>/dev/null`);}@@ -156,8 +172,9 @@ function isProcessRunning(pid) {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;+ // Use kill -0 via exec (returns empty string on error = process dead)+ const output = exec(`kill -0 ${pid} 2>&1 && echo "running"`);+ return output.includes('running');}}@@ -190,11 +207,7 @@ function killPort(port) {}// 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' });- }+ sleepMs(500);// Verify which ones are actually deadfor (const pid of pidsToKill) {@@ -207,20 +220,16 @@ function killPort(port) {const stillRunning = pidsToKill.filter(pid => isProcessRunning(pid));if (stillRunning.length > 0) {for (const pid of stillRunning) {- // Force kill again+ // Force kill again (no /T flag to avoid killing parent shell)if (isWindows) {- exec(`taskkill /PID ${pid} /F /T 2>nul`);+ exec(`taskkill /PID ${pid} /F 2>nul`);} else {exec(`kill -9 ${pid} 2>/dev/null`);}}// Final check- if (isWindows) {- exec('ping -n 2 127.0.0.1 >nul');- } else {- spawnSync('sleep', ['0.5'], { stdio: 'pipe' });- }+ sleepMs(500);for (const pid of stillRunning) {if (!isProcessRunning(pid)) {@@ -247,7 +256,8 @@ function killPort(port) {const PORTS = loadPorts();console.log('Stopping MachinaOs services...\n');-console.log(`Platform: ${isWindows ? 'Windows' : isMac ? 'macOS' : 'Linux'}`);+const platformName = isWindows ? 'Windows' : isGitBash ? 'Git Bash' : isMac ? 'macOS' : 'Linux';+console.log(`Platform: ${platformName}`);console.log(`Ports: ${PORTS.join(', ')}\n`);let allStopped = true;diff --git a/server/routers/websocket.py b/server/routers/websocket.pyindex 73af2ee0..91d94ba1 100644--- a/server/routers/websocket.py+++ b/server/routers/websocket.py@@ -256,7 +256,7 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dicawait broadcaster.update_node_status(node_id, "error", {"error": result.get("error")}, workflow_id=workflow_id)# Explicitly pass through success status (don't let decorator default to True)- return {+ ws_result = {"success": result.get("success", False),"node_id": node_id,"result": result.get("result"),@@ -264,6 +264,10 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dic"execution_time": result.get("execution_time"),"timestamp": time.time()}+ # Debug: Log what we're returning to WebSocket+ result_data = result.get("result")+ logger.debug(f"[WS execute_node] Returning: success={ws_result['success']}, result.response={repr(result_data.get('response', 'MISSING')[:100] if result_data and result_data.get('response') else 'None')}")+ return ws_result@ws_handler("node_id")diff --git a/server/routers/workflow.py b/server/routers/workflow.pyindex 1e7f37ee..6fda0291 100644--- a/server/routers/workflow.py+++ b/server/routers/workflow.py@@ -127,4 +127,37 @@ async def workflow_health_check():return {"status": "OK","service": "workflow"- }\ No newline at end of file+ }+++class TemporalNodeExecuteRequest(BaseModel):+ """Request from Temporal service to execute a single node."""+ node_id: str+ node_type: str+ data: Dict[str, Any] = {}+ context: Dict[str, Any] = {}++[email protected]("/node/execute")+async def execute_node_for_temporal(+ request: TemporalNodeExecuteRequest,+ workflow_service: WorkflowService = Depends(lambda: container.workflow_service())+):+ """Execute a single node - called by Temporal service.++ This is a simplified endpoint for Temporal activities to call.+ It extracts context and delegates to the existing execute_node method.+ """+ logger.info(f"[Temporal] Executing node: {request.node_id} (type={request.node_type})")++ context = request.context+ result = await workflow_service.execute_node(+ node_id=request.node_id,+ node_type=request.node_type,+ parameters=request.data,+ nodes=context.get("nodes", []),+ edges=context.get("edges", []),+ session_id=context.get("session_id", "temporal"),+ )++ return result\ No newline at end of filediff --git a/server/services/ai.py b/server/services/ai.pyindex 2ba763a8..0a1532ed 100644--- a/server/services/ai.py+++ b/server/services/ai.py@@ -1,5 +1,6 @@"""AI service for managing language models with LangGraph state machine support."""+import reimport timeimport httpxfrom dataclasses import dataclass@@ -49,6 +50,24 @@ class ProviderConfig:models_header_fn: Callable[[str], dict] # Function to create headers+@dataclass+class ThinkingConfig:+ """Unified thinking/reasoning configuration across AI providers.++ LangChain parameters per provider (Jan 2026):+ - Claude: thinking={"type": "enabled", "budget_tokens": budget}, temp must be 1+ - OpenAI o-series: reasoning_effort ('minimal', 'low', 'medium', 'high')+ - Gemini 3+: thinking_level ('low', 'medium', 'high')+ - Gemini 2.5: thinking_budget (int tokens)+ - Groq: reasoning_format ('parsed', 'hidden')+ """+ enabled: bool = False+ budget: int = 2048 # Token budget (Claude, Gemini 2.5)+ effort: str = 'medium' # Effort level: 'minimal', 'low', 'medium', 'high' (OpenAI o-series)+ level: str = 'medium' # Thinking level: 'low', 'medium', 'high' (Gemini 3+)+ format: str = 'parsed' # Output format: 'parsed', 'hidden' (Groq)++def _openai_headers(api_key: str) -> dict:return {'Authorization': f'Bearer {api_key}'}@@ -296,6 +315,112 @@ class AgentState(TypedDict):iteration: intmax_iterations: intshould_continue: bool+ # Thinking/reasoning content accumulated across iterations+ thinking_content: Optional[str]+++def extract_thinking_from_response(response) -> tuple:+ """Extract text and thinking content from LLM response.++ Handles multiple formats:+ - LangChain content_blocks API (Claude, Gemini)+ - OpenAI responses/v1 format (content list with reasoning blocks containing summary)+ - Groq additional_kwargs.reasoning_content+ - Raw string content++ Returns:+ Tuple of (text_content: str, thinking_content: Optional[str])+ """+ text_parts = []+ thinking_parts = []++ logger.debug(f"[extract_thinking] Starting extraction, response type: {type(response).__name__}")+ logger.debug(f"[extract_thinking] has content_blocks: {hasattr(response, 'content_blocks')}, value: {getattr(response, 'content_blocks', None)}")+ logger.debug(f"[extract_thinking] has content: {hasattr(response, 'content')}, type: {type(getattr(response, 'content', None))}")+ logger.debug(f"[extract_thinking] has additional_kwargs: {hasattr(response, 'additional_kwargs')}, value: {getattr(response, 'additional_kwargs', None)}")+ logger.debug(f"[extract_thinking] has response_metadata: {hasattr(response, 'response_metadata')}, keys: {list(getattr(response, 'response_metadata', {}).keys()) if hasattr(response, 'response_metadata') else None}")++ # Use content_blocks API (LangChain 1.0+) for Claude/Gemini+ if hasattr(response, 'content_blocks') and response.content_blocks:+ for block in response.content_blocks:+ if isinstance(block, dict):+ block_type = block.get("type", "")+ if block_type == "reasoning":+ thinking_parts.append(block.get("reasoning", ""))+ elif block_type == "thinking":+ thinking_parts.append(block.get("thinking", ""))+ elif block_type == "text":+ text_parts.append(block.get("text", ""))++ # Check additional_kwargs for reasoning_content (Groq, older OpenAI responses)+ if not thinking_parts and hasattr(response, 'additional_kwargs'):+ reasoning = response.additional_kwargs.get('reasoning_content')+ if reasoning:+ thinking_parts.append(reasoning)++ # Check response_metadata for OpenAI o-series reasoning (responses/v1 format)+ # The output array contains reasoning items with summaries+ if not thinking_parts and hasattr(response, 'response_metadata'):+ metadata = response.response_metadata+ output = metadata.get('output', [])+ if isinstance(output, list):+ for item in output:+ if isinstance(item, dict) and item.get('type') == 'reasoning':+ summary = item.get('summary', [])+ if isinstance(summary, list):+ for s in summary:+ if isinstance(s, dict):+ # Handle both summary_text and text types+ text = s.get('text', '')+ if text:+ thinking_parts.append(text)+ elif isinstance(s, str):+ thinking_parts.append(s)++ # Check raw content for OpenAI responses/v1 format and other list formats+ if hasattr(response, 'content'):+ content = response.content+ if isinstance(content, str):+ if not text_parts:+ text_parts.append(content)+ elif isinstance(content, list):+ for block in content:+ if isinstance(block, dict):+ block_type = block.get('type', '')+ if block_type == 'text' or block_type == 'output_text':+ # Handle both 'text' and 'output_text' (responses/v1 format)+ if not text_parts: # Only add if not already extracted+ text_parts.append(block.get('text', ''))+ elif block_type == 'reasoning':+ # OpenAI responses/v1 format: reasoning block with summary array+ # Format: {"type": "reasoning", "summary": [{"type": "text", "text": "..."}, {"type": "summary_text", "text": "..."}]}+ summary = block.get('summary', [])+ if isinstance(summary, list):+ for s in summary:+ if isinstance(s, dict):+ s_type = s.get('type', '')+ if s_type in ('text', 'summary_text'):+ thinking_parts.append(s.get('text', ''))+ elif isinstance(s, str):+ thinking_parts.append(s)+ elif isinstance(summary, str):+ thinking_parts.append(summary)+ # Also check direct reasoning field+ if block.get('reasoning'):+ thinking_parts.append(block.get('reasoning', ''))+ elif block_type == 'thinking':+ thinking_parts.append(block.get('thinking', ''))+ elif isinstance(block, str) and not text_parts:+ text_parts.append(block)++ text = '\n'.join(filter(None, text_parts))+ thinking = '\n'.join(filter(None, thinking_parts)) if thinking_parts else None++ logger.debug(f"[extract_thinking] Final text_parts: {text_parts}")+ logger.debug(f"[extract_thinking] Final thinking_parts: {thinking_parts}")+ logger.debug(f"[extract_thinking] Returning text={repr(text[:100] if text else None)}, thinking={repr(thinking[:100] if thinking else None)}")++ return text, thinkingdef create_agent_node(chat_model):@@ -304,14 +429,16 @@ def create_agent_node(chat_model):The agent node:1. Receives current state with messages2. Invokes the LLM- 3. Checks for tool calls in response- 4. Returns updated state with new AI message and pending tool calls+ 3. Extracts thinking content if present+ 4. Checks for tool calls in response+ 5. Returns updated state with new AI message, thinking, and pending tool calls"""def agent_node(state: AgentState) -> Dict[str, Any]:"""Process messages through the LLM and return response."""messages = state["messages"]iteration = state.get("iteration", 0)max_iterations = state.get("max_iterations", 10)+ existing_thinking = state.get("thinking_content") or ""logger.debug(f"[LangGraph] Agent node invoked, iteration={iteration}, messages={len(messages)}")@@ -327,6 +454,18 @@ def create_agent_node(chat_model):logger.debug(f"[LangGraph] LLM response type: {type(response).__name__}")+ # Extract thinking content from response+ _, new_thinking = extract_thinking_from_response(response)++ # Accumulate thinking across iterations (for multi-step tool usage)+ accumulated_thinking = existing_thinking+ if new_thinking:+ if accumulated_thinking:+ accumulated_thinking = f"{accumulated_thinking}\n\n--- Iteration {iteration + 1} ---\n{new_thinking}"+ else:+ accumulated_thinking = new_thinking+ logger.debug(f"[LangGraph] Extracted thinking content ({len(new_thinking)} chars)")+# Check for Gemini-specific response attributes (safety ratings, block reason)if hasattr(response, 'response_metadata'):meta = response.response_metadata@@ -351,7 +490,8 @@ def create_agent_node(chat_model):"pending_tool_calls": pending_tool_calls,"iteration": iteration + 1,"max_iterations": max_iterations,- "should_continue": should_continue+ "should_continue": should_continue,+ "thinking_content": accumulated_thinking or None}return agent_node@@ -571,9 +711,33 @@ class AIService:logger.warning(f"[LangGraph] Empty response with no error details. Content type: {type(content)}, value: {content}")raise ValueError("AI generated empty response. Try rephrasing your prompt or using a different model.")+ def _is_reasoning_model(self, model: str) -> bool:+ """Check if model supports reasoning (OpenAI o-series).++ O-series models: o1, o1-mini, o1-preview, o3, o3-mini, o4-mini, etc.+ NOT gpt-4o (which contains 'o' but is not a reasoning model).+ """+ model_lower = model.lower()+ # Check for o-series pattern: starts with 'o' followed by digit+ # e.g., o1, o1-mini, o3, o3-mini, o4-mini+ return bool(re.match(r'^o[134](-|$)', model_lower))+def create_model(self, provider: str, api_key: str, model: str,- temperature: float, max_tokens: int):- """Create LangChain model instance using provider registry."""+ temperature: float, max_tokens: int,+ thinking: Optional[ThinkingConfig] = None):+ """Create LangChain model instance using provider registry.++ Args:+ provider: AI provider name (openai, anthropic, gemini, groq, openrouter)+ api_key: Provider API key+ model: Model name/ID+ temperature: Sampling temperature+ max_tokens: Maximum response tokens+ thinking: Optional thinking/reasoning configuration++ Returns:+ Configured LangChain chat model instance+ """config = PROVIDER_CONFIGS.get(provider)if not config:raise ValueError(f"Unsupported provider: {provider}")@@ -594,6 +758,78 @@ class AIService:'X-Title': 'MachinaOS'}+ # OpenAI o-series reasoning models ALWAYS require temperature=1+ # This applies regardless of whether thinking mode is enabled+ if provider == 'openai' and self._is_reasoning_model(model):+ if kwargs.get('temperature', 1) != 1:+ logger.info(f"[AI] OpenAI o-series model '{model}': forcing temperature to 1 (was {kwargs.get('temperature')})")+ kwargs['temperature'] = 1++ # Apply thinking/reasoning configuration per provider (per LangChain docs Jan 2026)+ if thinking and thinking.enabled:+ if provider == 'anthropic':+ # Claude extended thinking: thinking={"type": "enabled", "budget_tokens": N}+ # Requires temperature=1, budget min 1024 tokens+ # IMPORTANT: max_tokens must be greater than budget_tokens+ budget = max(1024, thinking.budget)+ # Ensure max_tokens > budget_tokens (add buffer for response)+ if max_tokens <= budget:+ # Set max_tokens to budget + reasonable response space (at least 1024 more)+ kwargs[config.max_tokens_param] = budget + max(1024, max_tokens)+ logger.info(f"[AI] Claude thinking: adjusted max_tokens from {max_tokens} to {kwargs[config.max_tokens_param]} (budget={budget})")+ kwargs['thinking'] = {"type": "enabled", "budget_tokens": budget}+ kwargs['temperature'] = 1 # Required for Claude thinking mode+ elif provider == 'openai' and self._is_reasoning_model(model):+ # OpenAI o-series: Use reasoning_effort parameter+ # Note: reasoning.summary requires organization verification on OpenAI+ # So we just use reasoning_effort for now+ kwargs['reasoning_effort'] = thinking.effort+ # O-series models only support temperature=1+ kwargs['temperature'] = 1+ logger.info(f"[AI] OpenAI o-series: reasoning_effort={thinking.effort}, temperature=1")+ elif provider == 'gemini':+ # Gemini thinking support varies by model:+ # - Gemini 2.5 Flash/Pro: Use thinking_budget (int tokens, 0=off, -1=dynamic)+ # - Gemini 2.0 Flash Thinking: Built-in thinking, use thinking_budget+ # - Gemini 3 models: Limited/experimental thinking support+ model_lower = model.lower()++ # Gemini 2.5 models support thinking_budget+ if 'gemini-2.5' in model_lower or '2.5' in model_lower:+ kwargs['thinking_budget'] = thinking.budget+ kwargs['include_thoughts'] = True+ logger.info(f"[AI] Gemini 2.5 thinking: budget={thinking.budget}")+ # Gemini 2.0 Flash Thinking model+ elif 'gemini-2.0-flash-thinking' in model_lower or 'flash-thinking' in model_lower:+ kwargs['thinking_budget'] = thinking.budget+ kwargs['include_thoughts'] = True+ logger.info(f"[AI] Gemini Flash Thinking: budget={thinking.budget}")+ # Gemini 3 preview models - thinking support is experimental/limited+ # Only 'low' and 'high' levels may be supported, not 'medium'+ elif 'gemini-3' in model_lower:+ # For Gemini 3, try thinking_budget instead of thinking_level+ # as level support is inconsistent across preview models+ kwargs['thinking_budget'] = thinking.budget+ kwargs['include_thoughts'] = True+ logger.info(f"[AI] Gemini 3 thinking: using budget={thinking.budget} (level support varies)")+ # Other Gemini 2.x models - try thinking_budget+ elif 'gemini-2' in model_lower:+ kwargs['thinking_budget'] = thinking.budget+ kwargs['include_thoughts'] = True+ logger.info(f"[AI] Gemini 2.x thinking: budget={thinking.budget}")+ else:+ # For other/older Gemini models, thinking may not be supported+ logger.warning(f"[AI] Gemini model '{model}' may not support thinking mode")+ elif provider == 'groq':+ # Groq: reasoning_format ('parsed' or 'hidden')+ # 'parsed' includes reasoning in additional_kwargs, 'hidden' suppresses it+ format_val = thinking.format if thinking.format in ('parsed', 'hidden') else 'parsed'+ kwargs['reasoning_format'] = format_val+ elif provider == 'cerebras':+ # Cerebras: No official LangChain thinking support yet+ # Passing through as model_kwargs if supported+ kwargs['thinking_budget'] = thinking.budget+return config.model_class(**kwargs)async def fetch_models(self, provider: str, api_key: str) -> List[str]:@@ -607,17 +843,36 @@ class AIService:response.raise_for_status()data = response.json()- # Filter for chat models+ # Filter for chat models including o-series reasoning modelsmodels = []for model in data.get('data', []):model_id = model['id'].lower()- if (('gpt' in model_id or 'o1' in model_id) and- 'instruct' not in model_id and 'embedding' not in model_id):+ # Include GPT models and o-series reasoning models (o1, o3, o4)+ is_gpt = 'gpt' in model_id+ is_o_series = any(f'o{n}' in model_id for n in ['1', '3', '4'])+ is_excluded = 'instruct' in model_id or 'embedding' in model_id or 'realtime' in model_id+ if (is_gpt or is_o_series) and not is_excluded:models.append(model['id'])- # Sort by priority- priority = {'gpt-4o': 1, 'gpt-4o-mini': 2, 'gpt-4-turbo': 3, 'gpt-4': 4}- return sorted(models, key=lambda x: priority.get(x, 99))+ # Sort by priority - o-series reasoning models at top+ def get_priority(model_name: str) -> int:+ m = model_name.lower()+ # O-series reasoning models first+ if 'o4-mini' in m: return 1+ if 'o4' in m: return 2+ if 'o3-mini' in m: return 3+ if 'o3' in m: return 4+ if 'o1-mini' in m: return 5+ if 'o1' in m: return 6+ # Then GPT models+ if 'gpt-4o-mini' in m: return 10+ if 'gpt-4o' in m: return 11+ if 'gpt-4-turbo' in m: return 12+ if 'gpt-4' in m: return 13+ if 'gpt-3.5' in m: return 20+ return 99++ return sorted(models, key=get_priority)elif provider == 'anthropic':response = await client.get(@@ -759,7 +1014,19 @@ class AIService:provider = 'cerebras'else:provider = self.detect_provider(model)- chat_model = self.create_model(provider, api_key, model, temperature, max_tokens)++ # Build thinking config from parameters+ thinking_config = None+ if flattened.get('thinkingEnabled'):+ thinking_config = ThinkingConfig(+ enabled=True,+ budget=int(flattened.get('thinkingBudget', 2048)),+ effort=flattened.get('reasoningEffort', 'medium'),+ level=flattened.get('thinkingLevel', 'medium'),+ format=flattened.get('reasoningFormat', 'parsed'),+ )++ chat_model = self.create_model(provider, api_key, model, temperature, max_tokens, thinking_config)# Prepare messagesmessages = []@@ -773,8 +1040,34 @@ class AIService:# Executeresponse = chat_model.invoke(filtered_messages)+ # Debug: Log response structure for o-series reasoning models+ if thinking_config and thinking_config.enabled:+ logger.info(f"[AI Debug] Response type: {type(response).__name__}")+ logger.info(f"[AI Debug] Response content type: {type(response.content)}")+ logger.info(f"[AI Debug] Response content: {response.content[:500] if isinstance(response.content, str) else response.content}")+ if hasattr(response, 'content_blocks'):+ logger.info(f"[AI Debug] content_blocks: {response.content_blocks}")+ if hasattr(response, 'additional_kwargs'):+ logger.info(f"[AI Debug] additional_kwargs: {response.additional_kwargs}")+ if hasattr(response, 'response_metadata'):+ logger.info(f"[AI Debug] response_metadata: {response.response_metadata}")++ # Extract text and thinking content from response+ text_content, thinking_content = extract_thinking_from_response(response)++ # Debug: Log extraction results+ logger.info(f"[AI Debug] Extracted text_content: {repr(text_content[:200] if text_content else None)}")+ logger.info(f"[AI Debug] Extracted thinking_content: {repr(thinking_content[:200] if thinking_content else None)}")++ # Use extracted text if available, fall back to raw content+ response_text = text_content if text_content else response.content++ logger.info(f"[AI Debug] Final response_text: {repr(response_text[:200] if response_text else None)}")+result = {- "response": response.content,+ "response": response_text,+ "thinking": thinking_content,+ "thinking_enabled": thinking_config.enabled if thinking_config else False,"model": model,"provider": provider,"finish_reason": "stop",@@ -788,13 +1081,15 @@ class AIService:log_execution_time(logger, "ai_chat", start_time, time.time())log_api_call(logger, provider, model, "chat", True)- return {+ final_result = {"success": True,"node_id": node_id,"node_type": node_type,"result": result,"execution_time": time.time() - start_time}+ logger.info(f"[AI Debug] Returning final_result: success={final_result['success']}, result.response={repr(result.get('response', 'MISSING')[:100] if result.get('response') else 'None')}")+ return final_resultexcept Exception as e:logger.error("AI execution failed", node_id=node_id, error=str(e))@@ -881,6 +1176,18 @@ class AIService:if not api_key:raise ValueError("API key is required for AI Agent")+ # Build thinking config from parameters+ thinking_config = None+ if flattened.get('thinkingEnabled'):+ thinking_config = ThinkingConfig(+ enabled=True,+ budget=int(flattened.get('thinkingBudget', 2048)),+ effort=flattened.get('reasoningEffort', 'medium'),+ level=flattened.get('thinkingLevel', 'medium'),+ format=flattened.get('reasoningFormat', 'parsed'),+ )+ logger.info(f"[LangGraph] Thinking enabled: budget={thinking_config.budget}, effort={thinking_config.effort}")+# Broadcast: Initializing modelawait broadcast_status("initializing", {"message": f"Initializing {provider} model...",@@ -890,7 +1197,7 @@ class AIService:# Create LLM using the provider from node configurationlogger.debug(f"[LangGraph] Creating {provider} model: {model}")- chat_model = self.create_model(provider, api_key, model, temperature, max_tokens)+ chat_model = self.create_model(provider, api_key, model, temperature, max_tokens, thinking_config)# Build initial messages for stateinitial_messages: List[BaseMessage] = []@@ -1043,14 +1350,15 @@ class AIService:tool_executor=tool_executor if tools else None)- # Create initial state+ # Create initial state with thinking_content for reasoning modelsinitial_state: AgentState = {"messages": initial_messages,"tool_outputs": {},"pending_tool_calls": [],"iteration": 0,"max_iterations": 10,- "should_continue": False+ "should_continue": False,+ "thinking_content": None}# Broadcast: Executing graph@@ -1079,7 +1387,10 @@ class AIService:response_content = self._extract_text_content(raw_content, ai_response)iterations = final_state.get("iteration", 1)- logger.info(f"[LangGraph] Agent completed in {iterations} iteration(s)")+ # Get accumulated thinking content from state+ thinking_content = final_state.get("thinking_content")++ logger.info(f"[LangGraph] Agent completed in {iterations} iteration(s), thinking={'yes' if thinking_content else 'no'}")# Save to memory if connected (persist to database)# Only save non-empty messages using standardized validation@@ -1098,6 +1409,8 @@ class AIService:result = {"response": response_content,+ "thinking": thinking_content,+ "thinking_enabled": thinking_config.enabled if thinking_config else False,"model": model,"provider": provider,"agent_type": "langgraph",