|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + cliContext "github.com/mudler/LocalAI/core/cli/context" |
| 12 | + "github.com/mudler/LocalAI/core/config" |
| 13 | + "github.com/mudler/LocalAI/core/services" |
| 14 | + "github.com/mudler/LocalAGI/core/state" |
| 15 | + coreTypes "github.com/mudler/LocalAGI/core/types" |
| 16 | + "github.com/mudler/xlog" |
| 17 | +) |
| 18 | + |
| 19 | +type AgentCMD struct { |
| 20 | + Run AgentRunCMD `cmd:"" help:"Run an agent standalone (without the full LocalAI server)"` |
| 21 | + List AgentListCMD `cmd:"" help:"List agents in the pool registry"` |
| 22 | +} |
| 23 | + |
| 24 | +type AgentRunCMD struct { |
| 25 | + Name string `arg:"" optional:"" help:"Agent name to run from the pool registry (pool.json)"` |
| 26 | + |
| 27 | + Config string `short:"c" help:"Path to a JSON agent config file (alternative to loading by name)" type:"path"` |
| 28 | + Prompt string `short:"p" help:"Run in foreground mode: send a single prompt and print the response"` |
| 29 | + |
| 30 | + // Agent pool settings (mirrors RunCMD agent flags) |
| 31 | + APIURL string `env:"LOCALAI_AGENT_POOL_API_URL" help:"API URL for the agent to call (e.g. http://127.0.0.1:8080)" group:"agents"` |
| 32 | + APIKey string `env:"LOCALAI_AGENT_POOL_API_KEY" help:"API key for the agent" group:"agents"` |
| 33 | + DefaultModel string `env:"LOCALAI_AGENT_POOL_DEFAULT_MODEL" help:"Default model for the agent" group:"agents"` |
| 34 | + MultimodalModel string `env:"LOCALAI_AGENT_POOL_MULTIMODAL_MODEL" help:"Multimodal model for the agent" group:"agents"` |
| 35 | + TranscriptionModel string `env:"LOCALAI_AGENT_POOL_TRANSCRIPTION_MODEL" help:"Transcription model for the agent" group:"agents"` |
| 36 | + TranscriptionLanguage string `env:"LOCALAI_AGENT_POOL_TRANSCRIPTION_LANGUAGE" help:"Transcription language for the agent" group:"agents"` |
| 37 | + TTSModel string `env:"LOCALAI_AGENT_POOL_TTS_MODEL" help:"TTS model for the agent" group:"agents"` |
| 38 | + StateDir string `env:"LOCALAI_AGENT_POOL_STATE_DIR" default:"agents" help:"State directory containing pool.json" type:"path" group:"agents"` |
| 39 | + Timeout string `env:"LOCALAI_AGENT_POOL_TIMEOUT" default:"5m" help:"Agent timeout" group:"agents"` |
| 40 | + EnableSkills bool `env:"LOCALAI_AGENT_POOL_ENABLE_SKILLS" default:"false" help:"Enable skills service" group:"agents"` |
| 41 | + EnableLogs bool `env:"LOCALAI_AGENT_POOL_ENABLE_LOGS" default:"false" help:"Enable agent logging" group:"agents"` |
| 42 | + CustomActionsDir string `env:"LOCALAI_AGENT_POOL_CUSTOM_ACTIONS_DIR" help:"Custom actions directory" group:"agents"` |
| 43 | +} |
| 44 | + |
| 45 | +func (r *AgentRunCMD) Run(ctx *cliContext.Context) error { |
| 46 | + if r.Name == "" && r.Config == "" { |
| 47 | + return fmt.Errorf("either an agent name or --config must be provided") |
| 48 | + } |
| 49 | + |
| 50 | + agentConfig, err := r.loadAgentConfig() |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + // Override agent config fields from CLI flags when provided |
| 56 | + r.applyOverrides(agentConfig) |
| 57 | + |
| 58 | + xlog.Info("Starting standalone agent", "name", agentConfig.Name) |
| 59 | + |
| 60 | + appConfig := r.buildAppConfig() |
| 61 | + |
| 62 | + poolService, err := services.NewAgentPoolService(appConfig) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to create agent pool service: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + if err := poolService.Start(appConfig.Context); err != nil { |
| 68 | + return fmt.Errorf("failed to start agent pool service: %w", err) |
| 69 | + } |
| 70 | + defer poolService.Stop() |
| 71 | + |
| 72 | + pool := poolService.Pool() |
| 73 | + |
| 74 | + // Start the agent standalone (does not persist to pool.json) |
| 75 | + if err := pool.StartAgentStandalone(agentConfig.Name, agentConfig); err != nil { |
| 76 | + return fmt.Errorf("failed to start agent %q: %w", agentConfig.Name, err) |
| 77 | + } |
| 78 | + |
| 79 | + ag := pool.GetAgent(agentConfig.Name) |
| 80 | + if ag == nil { |
| 81 | + return fmt.Errorf("agent %q not found after start", agentConfig.Name) |
| 82 | + } |
| 83 | + |
| 84 | + // Foreground mode: send a single prompt and exit |
| 85 | + if r.Prompt != "" { |
| 86 | + xlog.Info("Sending prompt to agent", "agent", agentConfig.Name) |
| 87 | + result := ag.Ask(coreTypes.WithText(r.Prompt)) |
| 88 | + if result == nil { |
| 89 | + return fmt.Errorf("agent returned no result") |
| 90 | + } |
| 91 | + if result.Error != nil { |
| 92 | + return fmt.Errorf("agent error: %w", result.Error) |
| 93 | + } |
| 94 | + fmt.Println(result.Response) |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + // Background mode: run until interrupted |
| 99 | + xlog.Info("Agent running in background mode. Press Ctrl+C to stop.", "agent", agentConfig.Name) |
| 100 | + |
| 101 | + sigCh := make(chan os.Signal, 1) |
| 102 | + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 103 | + <-sigCh |
| 104 | + |
| 105 | + xlog.Info("Shutting down agent", "agent", agentConfig.Name) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (r *AgentRunCMD) loadAgentConfig() (*state.AgentConfig, error) { |
| 110 | + // Load from JSON config file |
| 111 | + if r.Config != "" { |
| 112 | + data, err := os.ReadFile(r.Config) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("failed to read config file %q: %w", r.Config, err) |
| 115 | + } |
| 116 | + var cfg state.AgentConfig |
| 117 | + if err := json.Unmarshal(data, &cfg); err != nil { |
| 118 | + return nil, fmt.Errorf("failed to parse config file %q: %w", r.Config, err) |
| 119 | + } |
| 120 | + if cfg.Name == "" { |
| 121 | + return nil, fmt.Errorf("agent config must have a name") |
| 122 | + } |
| 123 | + return &cfg, nil |
| 124 | + } |
| 125 | + |
| 126 | + // Load from pool.json by name |
| 127 | + poolFile := r.StateDir + "/pool.json" |
| 128 | + data, err := os.ReadFile(poolFile) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("failed to read pool registry %q: %w", poolFile, err) |
| 131 | + } |
| 132 | + |
| 133 | + var pool map[string]state.AgentConfig |
| 134 | + if err := json.Unmarshal(data, &pool); err != nil { |
| 135 | + return nil, fmt.Errorf("failed to parse pool registry %q: %w", poolFile, err) |
| 136 | + } |
| 137 | + |
| 138 | + cfg, ok := pool[r.Name] |
| 139 | + if !ok { |
| 140 | + available := make([]string, 0, len(pool)) |
| 141 | + for name := range pool { |
| 142 | + available = append(available, name) |
| 143 | + } |
| 144 | + return nil, fmt.Errorf("agent %q not found in pool registry. Available agents: %v", r.Name, available) |
| 145 | + } |
| 146 | + |
| 147 | + cfg.Name = r.Name |
| 148 | + return &cfg, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (r *AgentRunCMD) applyOverrides(cfg *state.AgentConfig) { |
| 152 | + if r.APIURL != "" { |
| 153 | + cfg.APIURL = r.APIURL |
| 154 | + } |
| 155 | + if r.APIKey != "" { |
| 156 | + cfg.APIKey = r.APIKey |
| 157 | + } |
| 158 | + if r.DefaultModel != "" && cfg.Model == "" { |
| 159 | + cfg.Model = r.DefaultModel |
| 160 | + } |
| 161 | + if r.MultimodalModel != "" && cfg.MultimodalModel == "" { |
| 162 | + cfg.MultimodalModel = r.MultimodalModel |
| 163 | + } |
| 164 | + if r.TranscriptionModel != "" && cfg.TranscriptionModel == "" { |
| 165 | + cfg.TranscriptionModel = r.TranscriptionModel |
| 166 | + } |
| 167 | + if r.TranscriptionLanguage != "" && cfg.TranscriptionLanguage == "" { |
| 168 | + cfg.TranscriptionLanguage = r.TranscriptionLanguage |
| 169 | + } |
| 170 | + if r.TTSModel != "" && cfg.TTSModel == "" { |
| 171 | + cfg.TTSModel = r.TTSModel |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func (r *AgentRunCMD) buildAppConfig() *config.ApplicationConfig { |
| 176 | + appConfig := &config.ApplicationConfig{ |
| 177 | + Context: context.Background(), |
| 178 | + } |
| 179 | + appConfig.AgentPool = config.AgentPoolConfig{ |
| 180 | + Enabled: true, |
| 181 | + APIURL: r.APIURL, |
| 182 | + APIKey: r.APIKey, |
| 183 | + DefaultModel: r.DefaultModel, |
| 184 | + MultimodalModel: r.MultimodalModel, |
| 185 | + TranscriptionModel: r.TranscriptionModel, |
| 186 | + TranscriptionLanguage: r.TranscriptionLanguage, |
| 187 | + TTSModel: r.TTSModel, |
| 188 | + StateDir: r.StateDir, |
| 189 | + Timeout: r.Timeout, |
| 190 | + EnableSkills: r.EnableSkills, |
| 191 | + EnableLogs: r.EnableLogs, |
| 192 | + CustomActionsDir: r.CustomActionsDir, |
| 193 | + } |
| 194 | + return appConfig |
| 195 | +} |
| 196 | + |
| 197 | +type AgentListCMD struct { |
| 198 | + StateDir string `env:"LOCALAI_AGENT_POOL_STATE_DIR" default:"agents" help:"State directory containing pool.json" type:"path" group:"agents"` |
| 199 | +} |
| 200 | + |
| 201 | +func (r *AgentListCMD) Run(ctx *cliContext.Context) error { |
| 202 | + poolFile := r.StateDir + "/pool.json" |
| 203 | + data, err := os.ReadFile(poolFile) |
| 204 | + if err != nil { |
| 205 | + if os.IsNotExist(err) { |
| 206 | + fmt.Println("No agents found (pool.json does not exist)") |
| 207 | + return nil |
| 208 | + } |
| 209 | + return fmt.Errorf("failed to read pool registry %q: %w", poolFile, err) |
| 210 | + } |
| 211 | + |
| 212 | + var pool map[string]state.AgentConfig |
| 213 | + if err := json.Unmarshal(data, &pool); err != nil { |
| 214 | + return fmt.Errorf("failed to parse pool registry %q: %w", poolFile, err) |
| 215 | + } |
| 216 | + |
| 217 | + if len(pool) == 0 { |
| 218 | + fmt.Println("No agents found in pool registry") |
| 219 | + return nil |
| 220 | + } |
| 221 | + |
| 222 | + fmt.Printf("Agents in %s:\n", poolFile) |
| 223 | + for name, cfg := range pool { |
| 224 | + model := cfg.Model |
| 225 | + if model == "" { |
| 226 | + model = "(default)" |
| 227 | + } |
| 228 | + desc := cfg.Description |
| 229 | + if desc == "" { |
| 230 | + desc = "(no description)" |
| 231 | + } |
| 232 | + fmt.Printf(" - %s [model: %s] %s\n", name, model, desc) |
| 233 | + } |
| 234 | + return nil |
| 235 | +} |
0 commit comments