SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
Merge branch 'dev' into runtime
Forhad Hosain80a6568f1752
diff --cc packages/runtime/README.mdindex 35db993f,38722dab..544b4a08--- a/packages/runtime/README.md+++ b/packages/runtime/README.md@@@ -68,43 -73,48 +68,43 @@@ src```bash# Install dependencies-npm install+pnpm install-# Link smyth-runtime (if needed)-npm link smyth-runtime+# Start development server with auto-reload+pnpm run dev-# Development mode with auto-reload-npm run dev+# The server will start on http://localhost:5053+```-# Build for development-npm run build:dev+### Production-# Build for production-npm run build+```bash+# Build the application+pnpm run build-# Start the server-npm run start+# Start production server+pnpm run start:prod```-## Available Scripts+### Available Scripts- | Script | Description |- |--------|-------------|- | `pnpm run dev` | Development mode with auto-reload |- | `pnpm run build` | Build for production |- | `pnpm run start:prod` | Start production server |- | `pnpm run test` | Run tests |- | `pnpm run lint` | Lint TypeScript files |- | `pnpm run clean` | Clean build directory |-### Core Scripts (Minimal)-- `npm run dev` - Development mode with auto-reload using nodemon-- `npm run build` - Build for production-- `npm run start:prod` - Start server in production mode++| Script | Description |++| --------------------- | --------------------------------- |++| `pnpm run dev` | Development mode with auto-reload |++| `pnpm run build` | Build for production |++| `pnpm run start:prod` | Start production server |++| `pnpm run test` | Run tests |++| `pnpm run lint` | Lint TypeScript files |++| `pnpm run clean` | Clean build directory |-### Additional Scripts-- `npm run test` - Run tests-- `npm run clean` - Clean the dist directory-- `npm run lint` - Lint TypeScript files-- `npm run lint-fix` - Lint and auto-fix TypeScript files-- `npm run ts-error-scan` - Check for TypeScript errors without emitting files-- `npm run format` - Format TypeScript files with Prettier-- `npm run prettier:format` - Format all supported files with Prettier-- `npm run bump` - Bump patch version+## 🔧 Configuration-## 🔧 Environment Setup+### Required Environment Variables-### Required (for both development and production):```bash-# Authentication (get these from your LogTo setup)+# Authentication (LogTo configuration)LOGTO_M2M_APP_SECRET="your-m2m-app-secret"--LOGTO_M2M_APP_ID="your-m2m-app-id"++LOGTO_M2M_APP_ID="your-m2m-app-id"LOGTO_SERVER="https://auth.yourdomain.com"LOGTO_API_RESOURCE="https://api.yourdomain.com"MIDDLEWARE_API_BASE_URL="http://middleware.yourdomain.com"@@@ -130,165 -149,409 +130,178 @@@ DEFAULT_AGENT_DOMAIN="localagent.stage.PROD_AGENT_DOMAIN="agentid.yourdomain.com"```-**Example:**-```bash-# ✅ Correct - domain contains 'localagent'-DEFAULT_AGENT_DOMAIN="localagent.stage.yourdomain.ai"+> **⚠️ Local Development**: For local agent execution, ensure your domain contains `localagent`-# ❌ Incorrect - missing 'localagent' keyword-DEFAULT_AGENT_DOMAIN="localhost:3000"-```+## 🌐 API Modules-## 🌐 Deployment+### Agent Runner (`/api/*`)+-### Environment Requirements+- **OAuth Authentication**: `/oauth/*` - Handle OAuth flows+- **Agent Execution**: `/agent/*` - Execute and manage agents+- **File Operations**: File upload and processing capabilities-**Development:**-- Node.js 18+-- pnpm 8+-- LogTo authentication server+### Debugger (`/models/*`)+-**Production:**-- All development requirements-- Redis (for session management)-- SSL certificates (for production domains)-- Load balancer (recommended)+- **Model Management**: Team-based access control for AI models+- **Debug Sessions**: Real-time agent debugging and monitoring+- **Component Tracking**: Async component execution tracking-### Quick Deploy+### Embodiment (Various endpoints)+-1. **Build everything:**- ```bash- npm run build- ```+- **ChatGPT Plugin**: `/chatgpt/*` - ChatGPT plugin integration+- **OpenAI Proxy**: `/openai/*` - OpenAI API proxy+- **Alexa Skills**: `/alexa/*` - Amazon Alexa integration+- **MCP Protocol**: `/mcp/*` - Model Context Protocol support+- **API Documentation**: `/swagger/*` - Interactive API docs+- **Postman Collections**: `/postman/*` - Generate API collections-2. **Set production environment:**- ```bash- NODE_ENV=production- PORT=5053- # Add your production environment variables- ```+## 🚀 Smart Routing-3. **Start production server:**- ```bash- npm run start:prod- ```--### Production Checklist--- [ ] Environment variables configured-- [ ] Authentication server running-- [ ] Database connected-- [ ] Redis connected (for sessions)-- [ ] SSL certificates installed-- [ ] Domain routing configured-- [ ] Health checks passing (`http://your-domain/health`)--## 🔍 Troubleshooting+The runtime server uses intelligent routing to direct requests to the appropriate module based on HTTP headers:-### Common Issues:+### Quick Examples-**"Agent not routing correctly"**```bash-# Check routing with explicit headers-curl -H "X-FORCE-DEBUGGER: true" http://localhost:5053/api/test-```+# Default behavior (Agent Runner)+curl http://localhost:5053/api/test-**"Authentication failing"**-- Verify LogTo environment variables-- Check LogTo server is accessible-- Confirm M2M app is configured correctly--**"Development server not starting"**-```bash-# Clean and reinstall-rm -rf node_modules-npm install-```+# Force specific modules+curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5053/api/test+curl -H "X-FORCE-DEBUGGER: true" http://localhost:5053/api/test-**"Build failing"**-```bash-# Increase memory for builds-export NODE_OPTIONS='--max-old-space-size=4096'-npm run build+# Debug session+curl -H "X-DEBUG-RUN: true" http://localhost:5053/api/test```-## Health Check--Once running, check server health at:--- `http://localhost:5000/health`--The health endpoint shows:--- Server mode-- Available functionality-- Version information--## Conditional Routing--The server automatically detects the mode and enables appropriate routes:--### Agent Mode Routes--- `/oauth/*` - OAuth authentication-- `/agent/*` - Agent execution-- `/debugger-proxy/*` - Debugger proxy--### Debugger Mode Routes--- `/models/*` - Model management with team access control-- All agent routes (debugger extends agent functionality)--### Embodiment Mode Routes--- `/static/*` - Static file serving-- `/swagger/*` - Swagger UI-- Various embodiment modules:- - `/chatgpt/*`- - `/openai/*`- - `/alexa/*`- - `/mcp/*`- - And more...--## Mode Detection--The server uses several methods to detect the appropriate functionality:--1. **Environment Variable**: `SERVER_MODE` setting-2. **Header Detection**: `x-smyth-debug: true` for debugger mode-3. **Path Detection**: Certain paths automatically enable specific modes-4. **User Agent**: SmythOS-Agent user agent for agent mode--## Migration from Individual Servers--This Runtime Server replaces the need for running three separate servers:--- Port 5000 now handles all functionality-- Environment variables from all three servers are supported-- All routes and features are preserved--## Features by Mode--### Agent Runner Features--- OAuth authentication-- Agent execution and management-- File uploads and processing-- Rate limiting and usage tracking--### Debugger Features--- Model management with team access-- Component async context tracking-- Debug mode detection via headers-- Extended agent functionality--### Embodiment Features--- Session management with Redis-- Static file serving-- Multiple integration modules:- - ChatGPT Plugin support- - OpenAI API proxy- - Alexa Skills integration- - MCP (Model Context Protocol)- - Postman collection generation- - Swagger documentation- - Form preview functionality- - Agent chat interface--## 🚀 Smart Routing System--The Runtime Server features an intelligent routing system that automatically directs requests to the appropriate service (Agent Runner or Debugger) based on HTTP headers. This provides **predictable, header-based routing** that works consistently across all environments.+### Routing Headers- | Header | Purpose | Routes To |- |--------|---------|-----------|- | `X-FORCE-AGENT-RUNNER` | Force agent execution | Agent Runner |- | `X-FORCE-DEBUGGER` | Force debug mode | Debugger |- | `X-DEBUG-RUN` | Start debug session | Debugger |- | `X-MONITOR-ID` | Monitor agent execution | Debugger |-### 🎯 Quick Start++| Header | Purpose | Routes To |++| ---------------------- | ----------------------- | ------------ |++| `X-FORCE-AGENT-RUNNER` | Force agent execution | Agent Runner |++| `X-FORCE-DEBUGGER` | Force debug mode | Debugger |++| `X-DEBUG-RUN` | Start debug session | Debugger |++| `X-MONITOR-ID` | Monitor agent execution | Debugger |-**Default Behavior (No Headers)**+### Server Modes-```bash-curl http://localhost:5000/api/test-# → Routes to Agent Runner (production-safe default)-```--**Force Agent Runner**+Control server functionality with the `SERVER_MODE` environment variable:-```bash-curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5000/api/test-# → Always routes to Agent Runner-```+- `runtime` (default): Full functionality+- `agent`: Agent runner only- - `debugger`: Debugger only++- `debugger`: Debugger only+- `embodiment`: Integrations only-**Force Debugger**+## 🌐 Deployment-```bash-curl -H "X-FORCE-DEBUGGER: true" http://localhost:5000/api/test-# → Always routes to Debugger-```+### Environment Requirements-**Debug Session**+**Development:**+-```bash-curl -H "X-DEBUG-RUN: true" http://localhost:5000/api/test-# → Routes to Debugger for debugging-```+- Node.js 18++- pnpm 8++- LogTo authentication server-### 📋 Routing Priority (Highest to Lowest)+**Production:**+-| Priority | Header | Routes To | Use Case |-| -------- | --------------------------- | ------------ | ------------------------- |-| 1 | `X-FORCE-AGENT-RUNNER` | Agent Runner | Force production behavior |-| 2 | `X-DEBUG-*`, `X-MONITOR-ID` | Debugger | Debug sessions |-| 3 | `X-FORCE-DEBUGGER` | Debugger | Force debug behavior |-| 4 | Deployed versions | Agent Runner | Production safety |-| 5 | `X-ROUTING-MODE` | As specified | Explicit routing |-| 6 | No headers | Agent Runner | Safe default |+- All development requirements+- Redis (for session management)+- SSL certificates+- Load balancer (recommended)-### 🔧 Available Headers+### Production Deployment-#### Force Routing Headers+1. **Build the application:**+-```bash-# Force Agent Runner (highest priority)-X-FORCE-AGENT-RUNNER: true+ ```bash+ pnpm run build+ ```-# Force Debugger-X-FORCE-DEBUGGER: true-```+2. **Set environment:**+-#### Debug Session Headers+ ```bash+ NODE_ENV=production+ PORT=5053+ # Add production environment variables+ ```-```bash-# Any of these will route to Debugger-X-DEBUG-RUN: true-X-DEBUG-STOP: true-X-DEBUG-READ: true-X-DEBUG-INJ: true-X-DEBUG-SKIP: true-X-MONITOR-ID: your-monitor-id-```+3. **Start server:**+ ```bash+ pnpm run start:prod+ ```-#### Explicit Mode Headers+### Health Monitoring-```bash-# Explicit routing mode-X-ROUTING-MODE: debugger # Routes to Debugger-X-ROUTING-MODE: agent-runner # Routes to Agent Runner-```+Check server health at: `http://localhost:5053/health`-### 🧪 Testing Different Environments+The health endpoint provides:+-#### Test Agent Runner from Localhost+- Server status and mode+- Available modules+- Version information-```bash-# Without header (old behavior might route to debugger)-curl http://localhost:5000/api/test+## 🔍 Troubleshooting-# With header (guaranteed agent runner)-curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5000/api/test-```+### Common Issues-#### Test Debugger from Production+**Authentication Failing**+-```bash-# Force debugger even on production domains-curl -H "X-FORCE-DEBUGGER: true" https://your-agent.com/api/test-```+- Verify LogTo environment variables+- Ensure LogTo server is accessible+- Check M2M app configuration-#### Debug Sessions+**Routing Issues**+```bash-# Start a debug session-curl -H "X-DEBUG-RUN: true" http://localhost:5000/api/test--# Monitor agent execution-curl -H "X-MONITOR-ID: session-123" http://localhost:5000/api/test+# Test with explicit headers+curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5053/api/test```-### 🛠️ For Developers--#### Example Scripts--We provide ready-to-use test scripts:+**Build Problems**+```bash-# Test routing logic programmatically-node examples/test-smart-routing.js--# Test routing with actual HTTP requests (server must be running)-./examples/test-routing-http.sh-```--#### Testing Routing Logic--```typescript-import { testRoutingDecision, RoutingHeaders } from "./core/smart-agent-router";--const agent = { agentVersion: "development" };--// Test different scenarios-const scenarios = [- { headers: RoutingHeaders.FORCE_AGENT_RUNNER, expected: "agent-runner" },- { headers: RoutingHeaders.DEBUG_RUN, expected: "debugger" },- { headers: RoutingHeaders.NONE, expected: "agent-runner" },-];--scenarios.forEach(({ headers, expected }) => {- const result = testRoutingDecision(agent, headers);- console.log(`Headers: ${JSON.stringify(headers)}`);- console.log(`Routes to: ${result.useDebugger ? "debugger" : "agent-runner"}`);- console.log(`Reason: ${result.reason}\n`);-});-```--#### Available Test Headers--```typescript-import { RoutingHeaders } from "./core/smart-agent-router";--// Pre-defined header combinations-RoutingHeaders.FORCE_AGENT_RUNNER; // { "X-FORCE-AGENT-RUNNER": "true" }-RoutingHeaders.FORCE_DEBUGGER; // { "X-FORCE-DEBUGGER": "true" }-RoutingHeaders.DEBUG_RUN; // { "X-DEBUG-RUN": "true" }-RoutingHeaders.DEBUG_STOP; // { "X-DEBUG-STOP": "true" }-RoutingHeaders.MODE_DEBUGGER; // { "X-ROUTING-MODE": "debugger" }-RoutingHeaders.MODE_AGENT_RUNNER; // { "X-ROUTING-MODE": "agent-runner" }-RoutingHeaders.NONE; // {} (no headers)+# Increase memory for builds+export NODE_OPTIONS='--max-old-space-size=4096'+pnpm run build```-### 🔍 Troubleshooting--#### Request Not Routing as Expected?--1. **Check your headers**: Use browser dev tools or `curl -v` to verify headers-2. **Check the logs**: The server logs routing decisions with reasons-3. **Test with explicit headers**: Use `X-FORCE-*` headers to override behavior--#### Common Issues--**Issue**: "Agent always routes to debugger from localhost"+**Development Server Issues**+```bash-# Solution: Use explicit header-curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5000/api/test+# Clean reinstall+rm -rf node_modules+pnpm install```-**Issue**: "Can't debug from production domain"--```bash-# Solution: Use debug header-curl -H "X-FORCE-DEBUGGER: true" https://production.com/api/test-```+### Debug Mode-**Issue**: "Routing is inconsistent"+Enable debug logging by setting routing headers:```bash-# Solution: Use explicit routing mode-curl -H "X-ROUTING-MODE: agent-runner" http://your-domain.com/api/test-```--### 📊 Monitoring--The server logs all routing decisions:+# Enable debug mode+curl -H "X-DEBUG-RUN: true" http://localhost:5053/api/test+# Monitor specific session+curl -H "X-MONITOR-ID: session-123" http://localhost:5053/api/test```-Routing decision: AGENT-RUNNER - X-FORCE-AGENT-RUNNER header present-Routing decision: DEBUGGER - Debug headers present-Routing decision: AGENT-RUNNER - Default production-safe routing-```--### 🎯 Best Practices-1. **Production**: No headers = safe default (Agent Runner)-2. **Development**: Use explicit headers for predictable behavior-3. **Testing**: Use `X-FORCE-*` headers to test specific services-4. **Debugging**: Use `X-DEBUG-*` headers for debug sessions-5. **CI/CD**: Use `X-ROUTING-MODE` for explicit test scenarios+## 📊 Monitoring & Logging-### 🔄 Migration from Domain-Based Routing--If you were relying on domain-based routing:--**Old Way** (unreliable):--```bash-# Localhost = debugger, production = agent-runner (unpredictable)-curl http://localhost:5000/api/test-```--**New Way** (explicit):--```bash-# Explicit header-based routing (predictable)-curl -H "X-FORCE-AGENT-RUNNER: true" http://localhost:5000/api/test-curl -H "X-FORCE-DEBUGGER: true" http://production.com/api/test-```+The server provides comprehensive logging for:+----+- Request routing decisions+- Authentication events+- Agent execution status+- Error tracking and debugging-## 📁 Examples Directory+Logs are formatted for easy consumption and include routing reasons for troubleshooting.-The `examples/` directory contains helpful scripts for testing and understanding the Smart Routing system:+## 🔐 Security Features-- **`test-smart-routing.js`** - Programmatic testing of routing logic-- **`test-routing-http.sh`** - HTTP request testing with curl-- Run these scripts to validate your routing configuration+- **Rate Limiting**: Built-in rate limiting for API endpoints+- **CORS Protection**: Configurable CORS policies+- **Authentication**: OAuth 2.0 with LogTo integration+- **Input Validation**: Request validation with Joi schemas+- **Error Handling**: Secure error responses without sensitive data exposure## 🤝 Contributing@@@ -302,4 -564,4 +315,4 @@@ When contributing to the runtime server## 📄 License- This project is part of the SmythOS ecosystem.-This project is part of the SmythOS Runtime system.++This project is part of the SmythOS ecosystem.