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:
parent
a5fda5bbfd
commit
d42b5278c1
34
cmd/chat.go
34
cmd/chat.go
@ -150,9 +150,9 @@ func runChat(cmd *cobra.Command, args []string) {
|
|||||||
color.Green("Grok > ")
|
color.Green("Grok > ")
|
||||||
reply := client.Stream(history, model)
|
reply := client.Stream(history, model)
|
||||||
|
|
||||||
// === AGENT TOOL CALL HANDLING ===
|
// Agent tool call handling
|
||||||
if agentMode && strings.Contains(reply, "```tool") {
|
if agentMode && strings.Contains(reply, "```tool") {
|
||||||
handleToolCall(reply, &history, model)
|
handleToolCall(reply, &history)
|
||||||
continue
|
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) {
|
||||||
func handleToolCall(reply string, history *[]map[string]string, model string) {
|
|
||||||
start := strings.Index(reply, "```tool")
|
start := strings.Index(reply, "```tool")
|
||||||
if start == -1 {
|
if start == -1 {
|
||||||
return
|
return
|
||||||
@ -186,23 +185,40 @@ func handleToolCall(reply string, history *[]map[string]string, model string) {
|
|||||||
switch tc.Tool {
|
switch tc.Tool {
|
||||||
case "edit":
|
case "edit":
|
||||||
if tc.File != "" && tc.Instruction != "" {
|
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":
|
case "scaffold":
|
||||||
if tc.Path != "" && tc.Description != "" {
|
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":
|
case "testgen":
|
||||||
if tc.File != "" {
|
if tc.File != "" {
|
||||||
RunTestgenWithFile(tc.File)
|
testgenCmd.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
|
RunTestgenWithFile(tc.File)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_ = testgenCmd.Execute()
|
||||||
}
|
}
|
||||||
case "lint":
|
case "lint":
|
||||||
if tc.File != "" {
|
if tc.File != "" {
|
||||||
RunLintWithFile(tc.File)
|
lintCmd.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
|
RunLintWithFile(tc.File)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_ = lintCmd.Execute()
|
||||||
}
|
}
|
||||||
case "commit":
|
case "commit":
|
||||||
if tc.Message != "" {
|
if tc.Message != "" {
|
||||||
git.Run([]string{"commit", "-m", tc.Message})
|
_, _ = git.Run([]string{"commit", "-m", tc.Message})
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
color.Red("Unknown tool: %s", tc.Tool)
|
color.Red("Unknown tool: %s", tc.Tool)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user