refactor: optimize ACP agent initialization and remove dead code

- Remove unreachable custom agent command validation from ACPExecutor.Execute()
- Add IsBuiltinAgent() utility function for cleaner agent name resolution
- Replace inline agent list iteration with IsBuiltinAgent() check in agent_chat.go
- Add explicit stdin pipe cleanup in RunAgentACP() defer block
- Simplify CLI output handling by removing redundant stream output fallback
This commit is contained in:
j3ssie
2026-03-07 14:13:19 +08:00
parent 9bdb3260b6
commit ecc77b9a30
3 changed files with 12 additions and 47 deletions
+9 -37
View File
@@ -102,39 +102,8 @@ func (e *ACPExecutor) Execute(ctx context.Context, step *core.Step, execCtx *cor
Exports: make(map[string]interface{}),
}
// Resolve agent name for the standalone function
agentName := step.Agent
var customCommand string
var customArgs []string
if agentName == "" && step.ACPConfig != nil && step.ACPConfig.Command != "" {
customCommand = step.ACPConfig.Command
customArgs = step.ACPConfig.Args
}
// For custom commands, use ResolveAgent + the old path
// For built-in agents, delegate to RunAgentACP
if customCommand != "" {
// Custom agent — validate command exists
_, err := exec.LookPath(customCommand)
if err != nil {
err = fmt.Errorf("agent command %q not found in PATH: %w", customCommand, err)
result.Status = core.StepStatusFailed
result.Error = err
result.EndTime = time.Now()
result.Duration = result.EndTime.Sub(result.StartTime)
return result, err
}
// Custom agents not supported by RunAgentACP — keep original error
_ = customArgs
err = fmt.Errorf("custom agent commands should use 'agent' field with a built-in agent name; use acp_config for custom agents via workflow steps only")
result.Status = core.StepStatusFailed
result.Error = err
result.EndTime = time.Now()
result.Duration = result.EndTime.Sub(result.StartTime)
return result, err
}
// Validate we have an agent
agentName := step.Agent
if agentName == "" {
err := fmt.Errorf("agent-acp step requires 'agent' field or 'acp_config.command'")
result.Status = core.StepStatusFailed
@@ -298,6 +267,7 @@ func RunAgentACP(ctx context.Context, prompt, agentName string, cfg *RunAgentACP
}()
defer func() {
_ = stdinPipe.Close()
_ = stderrWriter.Close()
stderrWg.Wait()
if cmd.Process != nil {
@@ -426,11 +396,13 @@ func resolveAgentName(step *core.Step) string {
return "unknown"
}
// IsBuiltinAgent returns true if the given name is a built-in ACP agent.
func IsBuiltinAgent(name string) bool {
_, ok := builtinACPAgents[name]
return ok
}
// availableAgentNames returns a comma-separated list of built-in agent names.
func availableAgentNames() string {
names := make([]string, 0, len(builtinACPAgents))
for name := range builtinACPAgents {
names = append(names, name)
}
return strings.Join(names, ", ")
return strings.Join(ListAgentNames(), ", ")
}
+1 -6
View File
@@ -75,17 +75,12 @@ func runAgent(cmd *cobra.Command, args []string) error {
StreamWriter: os.Stdout,
}
output, _, err := executor.RunAgentACP(ctx, message, agentName, cfg)
_, _, err = executor.RunAgentACP(ctx, message, agentName, cfg)
if err != nil {
printer.Error("Agent failed: %s", err)
return err
}
// If there was no stream writer, print output at the end
if cfg.StreamWriter == nil && output != "" {
fmt.Println(output)
}
return nil
}
+2 -4
View File
@@ -165,10 +165,8 @@ func resolveAgentName(model string) string {
if model == "" {
return "claude-code"
}
for _, name := range executor.ListAgentNames() {
if name == model {
return model
}
if executor.IsBuiltinAgent(model) {
return model
}
return "claude-code"
}