99 "log/slog"
1010 "os"
1111 "path/filepath"
12- "sort"
1312 "strings"
1413 "time"
1514
@@ -21,7 +20,6 @@ import (
2120 "github.com/docker/cagent/pkg/aliases"
2221 "github.com/docker/cagent/pkg/app"
2322 "github.com/docker/cagent/pkg/chat"
24- "github.com/docker/cagent/pkg/config"
2523 "github.com/docker/cagent/pkg/content"
2624 "github.com/docker/cagent/pkg/evaluation"
2725 "github.com/docker/cagent/pkg/input"
4240 useTUI bool
4341 remoteAddress string
4442 dryRun bool
45- commandName string
4643 modelOverrides []string
4744)
4845
49- const commandListSentinel = "__LIST__"
50-
5146// NewRunCmd creates a new run command
5247func NewRunCmd () * cobra.Command {
5348 cmd := & cobra.Command {
@@ -68,12 +63,7 @@ func NewRunCmd() *cobra.Command {
6863 cmd .PersistentFlags ().StringVar (& attachmentPath , "attach" , "" , "Attach an image file to the message" )
6964 cmd .PersistentFlags ().BoolVar (& useTUI , "tui" , true , "Run the agent with a Terminal User Interface (TUI)" )
7065 cmd .PersistentFlags ().StringVar (& remoteAddress , "remote" , "" , "Use remote runtime with specified address (only supported with TUI)" )
71- cmd .PersistentFlags ().StringVarP (& commandName , "command" , "c" , "" , "Run a named command from the agent's commands section" )
7266 cmd .PersistentFlags ().StringArrayVar (& modelOverrides , "model" , nil , "Override agent model: [agent=]provider/model (repeatable)" )
73- if f := cmd .PersistentFlags ().Lookup ("command" ); f != nil {
74- // Allow `-c` without value to list available commands
75- f .NoOptDefVal = commandListSentinel
76- }
7767 addGatewayFlags (cmd )
7868 addRuntimeConfigFlags (cmd )
7969
@@ -208,52 +198,6 @@ func doRunCommand(ctx context.Context, args []string, exec bool) error {
208198 slog .Debug ("Skipping local agent file loading for remote runtime" , "filename" , agentFilename )
209199 }
210200
211- // Resolve --command/-c into a first message if provided
212- var commandFirstMessage * string
213- if trimmed := strings .TrimSpace (commandName ); trimmed != "" {
214- // Handle listing commands when -c is provided without a value
215- if trimmed == commandListSentinel {
216- // If the next positional arg looks like a value (not a flag), treat it as the command value.
217- if len (args ) == 2 && ! strings .HasPrefix (args [1 ], "-" ) {
218- trimmed = args [1 ]
219- // consume the positional so it won't be treated as a message later
220- args = args [:1 ]
221- } else {
222- cmds , err := getCommandsForAgent (agentFilename , remoteAddress != "" , agents , agentName )
223- if err != nil {
224- return err
225- }
226- if len (cmds ) == 0 {
227- return fmt .Errorf ("no commands defined for agent '%s'" , agentName )
228- }
229- printAvailableCommands (agentName , cmds )
230- fmt .Println ()
231- return nil
232- }
233- }
234-
235- if len (args ) == 2 {
236- return fmt .Errorf ("cannot use --command (-c) together with a message argument" )
237- }
238-
239- cmds , err := getCommandsForAgent (agentFilename , remoteAddress != "" , agents , agentName )
240- if err != nil {
241- return err
242- }
243- if len (cmds ) == 0 {
244- return fmt .Errorf ("agent '%s' has no commands" , agentName )
245- }
246- if msg , ok := cmds [trimmed ]; ok {
247- commandFirstMessage = & msg
248- } else {
249- var names []string
250- for k := range cmds {
251- names = append (names , k )
252- }
253- return fmt .Errorf ("'%s' is an unknown command.\n \n Available: %s" , trimmed , strings .Join (names , ", " ))
254- }
255- }
256-
257201 // Validate remote flag usage
258202 if remoteAddress != "" && (! useTUI || exec ) {
259203 return fmt .Errorf ("--remote flag can only be used with TUI mode" )
@@ -329,10 +273,6 @@ func doRunCommand(ctx context.Context, args []string, exec bool) error {
329273
330274 // For `cagent run --tui=false`
331275 if ! useTUI {
332- // Inject first message for non-TUI if --command was used
333- if commandFirstMessage != nil {
334- args = []string {args [0 ], * commandFirstMessage }
335- }
336276 return runWithoutTUI (ctx , agentFilename , rt , sess , args )
337277 }
338278
@@ -352,11 +292,6 @@ func doRunCommand(ctx context.Context, args []string, exec bool) error {
352292 }
353293 }
354294
355- // Override firstMessage if --command was provided (cannot be combined with a message arg)
356- if commandFirstMessage != nil {
357- firstMessage = commandFirstMessage
358- }
359-
360295 a := app .New ("cagent" , agentFilename , rt , agents , sess , firstMessage )
361296 m := tui .New (a )
362297
@@ -397,11 +332,12 @@ func runWithoutTUI(ctx context.Context, agentFilename string, rt runtime.Runtime
397332 return nil
398333 }
399334
335+ userInput = runtime .ResolveCommand (ctx , rt , userInput )
336+
400337 handled , err := runUserCommand (userInput , sess , rt , ctx )
401338 if err != nil {
402339 return err
403340 }
404-
405341 if handled {
406342 return nil
407343 }
@@ -834,52 +770,3 @@ func fileToDataURL(filePath string) (string, error) {
834770
835771 return dataURL , nil
836772}
837-
838- // getCommandsForAgent returns the commands map for the selected agent,
839- // loading from the in-memory team for local runs or from the YAML file for remote runs.
840- func getCommandsForAgent (agentFilename string , isRemote bool , agents * team.Team , agentName string ) (map [string ]string , error ) {
841- if ! isRemote {
842- if agents == nil {
843- return nil , fmt .Errorf ("failed to load agent team" )
844- }
845-
846- ag , err := agents .Agent (agentName )
847- if err != nil {
848- return nil , err
849- }
850-
851- return ag .Commands (), nil
852- }
853-
854- parentDir := filepath .Dir (agentFilename )
855- fileName := filepath .Base (agentFilename )
856- root , err := os .OpenRoot (parentDir )
857- if err != nil {
858- return nil , fmt .Errorf ("failed to open root: %w" , err )
859- }
860- defer func () {
861- if err := root .Close (); err != nil {
862- slog .Error ("Failed to close root" , "error" , err )
863- }
864- }()
865-
866- cfg , err := config .LoadConfig (fileName , root )
867- if err != nil {
868- return nil , fmt .Errorf ("failed to load agent config: %w" , err )
869- }
870-
871- return cfg .Agents [agentName ].Commands , nil
872- }
873-
874- // printAvailableCommands pretty-prints the agent's commands sorted by name.
875- func printAvailableCommands (agentName string , cmds map [string ]string ) {
876- fmt .Printf ("Available commands for agent '%s':\n " , agentName )
877- var names []string
878- for k := range cmds {
879- names = append (names , k )
880- }
881- sort .Strings (names )
882- for _ , n := range names {
883- fmt .Printf (" - %s: %s\n " , n , cmds [n ])
884- }
885- }
0 commit comments