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.
This commit is contained in:
Gregory Gauthier 2026-03-04 12:05:01 +00:00
parent a5fda5bbfd
commit d42b5278c1

View File

@ -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)