From d42b5278c11834603806610c54074c54aefb1995 Mon Sep 17 00:00:00 2001 From: Gregory Gauthier Date: Wed, 4 Mar 2026 12:05:01 +0000 Subject: [PATCH] refactor(chat): integrate tool calls with Cobra command execution Refactor handleToolCall to set RunE on existing Cobra commands (edit, scaffold, testgen, lint) and execute them for better integration and reuse of CLI logic. Update commit tool to ignore output. Remove unused model parameter and adjust comments for consistency. --- cmd/chat.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/chat.go b/cmd/chat.go index bbfa2f8..6a9c900 100644 --- a/cmd/chat.go +++ b/cmd/chat.go @@ -150,9 +150,9 @@ func runChat(cmd *cobra.Command, args []string) { color.Green("Grok > ") reply := client.Stream(history, model) - // === AGENT TOOL CALL HANDLING === + // Agent tool call handling if agentMode && strings.Contains(reply, "```tool") { - handleToolCall(reply, &history, model) + handleToolCall(reply, &history) continue } @@ -163,8 +163,7 @@ func runChat(cmd *cobra.Command, args []string) { } } -// handleToolCall is now inside cmd package — no import cycle -func handleToolCall(reply string, history *[]map[string]string, model string) { +func handleToolCall(reply string, history *[]map[string]string) { start := strings.Index(reply, "```tool") if start == -1 { return @@ -186,23 +185,40 @@ func handleToolCall(reply string, history *[]map[string]string, model string) { switch tc.Tool { case "edit": if tc.File != "" && tc.Instruction != "" { - RunEditWithInstruction(tc.File, tc.Instruction) + // Reuse the existing edit command's logic (preview + confirm) + editCmd.RunE = func(cmd *cobra.Command, args []string) error { + RunEditWithInstruction(tc.File, tc.Instruction) // we'll define this helper next + return nil + } + _ = editCmd.Execute() } case "scaffold": if tc.Path != "" && tc.Description != "" { - RunScaffoldWithDescription(tc.Path, tc.Description) + scaffoldCmd.RunE = func(cmd *cobra.Command, args []string) error { + RunScaffoldWithDescription(tc.Path, tc.Description) + return nil + } + _ = scaffoldCmd.Execute() } case "testgen": if tc.File != "" { - RunTestgenWithFile(tc.File) + testgenCmd.RunE = func(cmd *cobra.Command, args []string) error { + RunTestgenWithFile(tc.File) + return nil + } + _ = testgenCmd.Execute() } case "lint": if tc.File != "" { - RunLintWithFile(tc.File) + lintCmd.RunE = func(cmd *cobra.Command, args []string) error { + RunLintWithFile(tc.File) + return nil + } + _ = lintCmd.Execute() } case "commit": if tc.Message != "" { - git.Run([]string{"commit", "-m", tc.Message}) + _, _ = git.Run([]string{"commit", "-m", tc.Message}) } default: color.Red("Unknown tool: %s", tc.Tool)