package api import ( "encoding/json" "net/http" "strings" "github.com/muyue/muyue/internal/agent" "github.com/muyue/muyue/internal/config" "github.com/muyue/muyue/internal/scanner" "github.com/muyue/muyue/internal/workflow" ) type Server struct { config *config.MuyueConfig scanResult *scanner.ScanResult mux *http.ServeMux convStore *ConversationStore agentRegistry *agent.Registry agentToolsJSON json.RawMessage workflowEngine *workflow.Engine } func NewServer(cfg *config.MuyueConfig) *Server { s := &Server{ config: cfg, mux: http.NewServeMux(), } s.scanResult = scanner.ScanSystem() s.convStore = NewConversationStore() s.agentRegistry = agent.DefaultRegistry() tools := s.agentRegistry.OpenAITools() toolsJSON, _ := json.Marshal(tools) s.agentToolsJSON = json.RawMessage(toolsJSON) s.workflowEngine, _ = workflow.NewEngine(s.agentRegistry) s.routes() return s } func (s *Server) routes() { s.mux.HandleFunc("/api/info", s.handleInfo) s.mux.HandleFunc("/api/system", s.handleSystem) s.mux.HandleFunc("/api/tools", s.handleTools) s.mux.HandleFunc("/api/config", s.handleConfig) s.mux.HandleFunc("/api/providers", s.handleProviders) s.mux.HandleFunc("/api/skills", s.handleSkills) s.mux.HandleFunc("/api/lsp", s.handleLSP) s.mux.HandleFunc("/api/mcp", s.handleMCP) s.mux.HandleFunc("/api/updates", s.handleUpdates) s.mux.HandleFunc("/api/install", s.handleInstall) s.mux.HandleFunc("/api/scan", s.handleScan) s.mux.HandleFunc("/api/editors", s.handleEditors) s.mux.HandleFunc("/api/preferences", s.handleUpdatePreferences) s.mux.HandleFunc("/api/terminal", s.handleTerminal) s.mux.HandleFunc("/api/ws/terminal", s.handleTerminalWS) s.mux.HandleFunc("/api/terminal/sessions", s.handleTerminalSessions) s.mux.HandleFunc("/api/terminal/sessions/", s.handleTerminalSessionsDelete) s.mux.HandleFunc("/api/terminal/themes", s.handleGetTerminalThemes) s.mux.HandleFunc("/api/terminal/settings", s.handleSaveTerminalSettings) s.mux.HandleFunc("/api/mcp/configure", s.handleMCPConfigure) s.mux.HandleFunc("/api/config/profile", s.handleSaveProfile) s.mux.HandleFunc("/api/config/provider", s.handleSaveProvider) s.mux.HandleFunc("/api/config/reset", s.handleResetConfig) s.mux.HandleFunc("/api/starship/apply-theme", s.handleApplyStarshipTheme) s.mux.HandleFunc("/api/providers/validate", s.handleValidateProvider) s.mux.HandleFunc("/api/update/run", s.handleRunUpdate) s.mux.HandleFunc("/api/chat", s.handleChat) s.mux.HandleFunc("/api/chat/history", s.handleChatHistory) s.mux.HandleFunc("/api/chat/clear", s.handleChatClear) s.mux.HandleFunc("/api/tool/call", s.handleToolCall) s.mux.HandleFunc("/api/tools/list", s.handleToolList) s.mux.HandleFunc("/api/shell/chat", s.handleShellChat) s.mux.HandleFunc("/api/workflow", s.handleWorkflowCreate) s.mux.HandleFunc("/api/workflow/list", s.handleWorkflowList) s.mux.HandleFunc("/api/workflow/", s.handleWorkflowGet) s.mux.HandleFunc("/api/workflow/plan", s.handleWorkflowPlan) s.mux.HandleFunc("/api/workflow/execute/", s.handleWorkflowExecute) s.mux.HandleFunc("/api/workflow/approve/", s.handleWorkflowApprove) s.mux.HandleFunc("/api/conversations", s.handleListConversations) s.mux.HandleFunc("/api/conversations/search", s.handleSearchConversations) s.mux.HandleFunc("/api/conversations/export", s.handleExportConversation) s.mux.HandleFunc("/api/conversations/", s.handleDeleteConversation) s.mux.HandleFunc("/api/lsp/install", s.handleLSPInstall) s.mux.HandleFunc("/api/skills/deploy", s.handleSkillsDeploy) s.mux.HandleFunc("/api/ssh/connections", s.handleSSHConnections) s.mux.HandleFunc("/api/ssh/test", s.handleSSHTest) s.mux.HandleFunc("/api/mcp/status", s.handleMCPStatus) s.mux.HandleFunc("/api/mcp/registry", s.handleMCPRegistry) s.mux.HandleFunc("/api/lsp/health", s.handleLSPHealth) s.mux.HandleFunc("/api/lsp/auto-install", s.handleLSPAutoInstall) s.mux.HandleFunc("/api/lsp/editor-config", s.handleLSPEditorConfig) s.mux.HandleFunc("/api/skills/validate", s.handleSkillValidate) s.mux.HandleFunc("/api/skills/test", s.handleSkillTest) s.mux.HandleFunc("/api/skills/export", s.handleSkillExport) s.mux.HandleFunc("/api/skills/import", s.handleSkillImport) s.mux.HandleFunc("/api/dashboard/status", s.handleDashboardStatus) } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/api/ws/") { s.mux.ServeHTTP(w, r) return } w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") if r.Method == "OPTIONS" { w.WriteHeader(http.StatusOK) return } s.mux.ServeHTTP(w, r) }