|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + cliContext "github.com/mudler/LocalAI/core/cli/context" |
| 10 | + "github.com/mudler/LocalAI/core/services" |
| 11 | + "github.com/mudler/LocalAI/core/config" |
| 12 | + "github.com/mudler/LocalAGI/core/state" |
| 13 | + "github.com/mudler/xlog" |
| 14 | +) |
| 15 | + |
| 16 | +// AgentCMD provides CLI commands for agent management |
| 17 | +type AgentCMD struct { |
| 18 | + Run RunAgentCMD `cmd:"" name:"run" help:"Run an agent in standalone mode"` |
| 19 | +} |
| 20 | + |
| 21 | +// RunAgentCMD represents the 'agent run' command |
| 22 | +// It accepts an agent name and optional config file path to run an agent standalone |
| 23 | +type RunAgentCMD struct { |
| 24 | + Name string `arg:"" optional:"false" help:"Name of the agent to run"` |
| 25 | + ConfigFile string `arg:"" optional:"true" help:"Path to JSON configuration file for the agent"` |
| 26 | + DataPath string `env:"LOCALAI_DATA_PATH" type:"path" default:"${basepath}/data" help:"Path for persistent data (agent state, tasks, jobs)"` |
| 27 | +} |
| 28 | + |
| 29 | +func (r *RunAgentCMD) Run(ctx *cliContext.Context) error { |
| 30 | + xlog.Info("Starting agent in standalone mode", "agent_name", r.Name) |
| 31 | + |
| 32 | + // Determine data path |
| 33 | + dataPath := r.DataPath |
| 34 | + if dataPath == "" { |
| 35 | + // Try to get basepath from context or use default |
| 36 | + basepath := "." |
| 37 | + if ctx != nil && ctx.BasePath != "" { |
| 38 | + basepath = ctx.BasePath |
| 39 | + } |
| 40 | + dataPath = filepath.Join(basepath, "data") |
| 41 | + } |
| 42 | + |
| 43 | + // Create state directory for agent |
| 44 | + stateDir := filepath.Join(dataPath, "agents", r.Name) |
| 45 | + if err := os.MkdirAll(stateDir, 0750); err != nil { |
| 46 | + return fmt.Errorf("failed to create agent state directory: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + xlog.Info("Agent state directory", "path", stateDir) |
| 50 | + |
| 51 | + // Load agent configuration if provided |
| 52 | + var agentConfig state.AgentConfig |
| 53 | + |
| 54 | + if r.ConfigFile != "" { |
| 55 | + xlog.Info("Loading agent configuration from file", "file", r.ConfigFile) |
| 56 | + |
| 57 | + configData, err := os.ReadFile(r.ConfigFile) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to read configuration file: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + if err := json.Unmarshal(configData, &agentConfig); err != nil { |
| 63 | + return fmt.Errorf("failed to parse configuration file: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + xlog.Info("Agent configuration loaded successfully") |
| 67 | + } else { |
| 68 | + // Use default configuration with agent name |
| 69 | + agentConfig.Name = r.Name |
| 70 | + xlog.Info("Using default agent configuration", "agent_name", r.Name) |
| 71 | + } |
| 72 | + |
| 73 | + // Ensure agent name is set |
| 74 | + if agentConfig.Name == "" { |
| 75 | + agentConfig.Name = r.Name |
| 76 | + } |
| 77 | + |
| 78 | + xlog.Info("Preparing to run agent", |
| 79 | + "agent_name", agentConfig.Name, |
| 80 | + "state_dir", stateDir) |
| 81 | + |
| 82 | + // Full implementation would: |
| 83 | + // 1. Create application config |
| 84 | + // 2. Initialize AgentPoolService |
| 85 | + // 3. Create/retrieve agent instance |
| 86 | + // 4. Start agent in standalone mode |
| 87 | + // 5. Block until completion or interrupt |
| 88 | + |
| 89 | + fmt.Printf("\n=== Agent Standalone Mode ===\n") |
| 90 | + fmt.Printf("Agent Name: %s\n", agentConfig.Name) |
| 91 | + fmt.Printf("State Directory: %s\n", stateDir) |
| 92 | + if r.ConfigFile != "" { |
| 93 | + fmt.Printf("Configuration File: %s\n", r.ConfigFile) |
| 94 | + } |
| 95 | + fmt.Printf("\nNote: This is the CLI entry point for standalone agent execution.\n") |
| 96 | + fmt.Printf("Full integration with AgentPoolService enables actual agent execution.\n") |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// Helper function to create a minimal app config for standalone agent mode |
| 102 | +func createStandaloneAppConfig(dataPath string) *config.ApplicationConfig { |
| 103 | + return &config.ApplicationConfig{ |
| 104 | + DataPath: dataPath, |
| 105 | + AgentPool: config.AgentPoolConfig{ |
| 106 | + StateDir: filepath.Join(dataPath, "agents"), |
| 107 | + Timeout: "5m", |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
0 commit comments