Feat(shell): cd and pipes support
This commit is contained in:
3
bot.go
3
bot.go
@@ -1116,11 +1116,10 @@ func findCall(msg, toolCall string) bool {
|
|||||||
}
|
}
|
||||||
// Store tool call info in the assistant message
|
// Store tool call info in the assistant message
|
||||||
// Convert Args map to JSON string for storage
|
// Convert Args map to JSON string for storage
|
||||||
argsJSON, _ := json.Marshal(lastToolCall.Args)
|
|
||||||
chatBody.Messages[lastMsgIdx].ToolCall = &models.ToolCall{
|
chatBody.Messages[lastMsgIdx].ToolCall = &models.ToolCall{
|
||||||
ID: lastToolCall.ID,
|
ID: lastToolCall.ID,
|
||||||
Name: lastToolCall.Name,
|
Name: lastToolCall.Name,
|
||||||
Args: string(argsJSON),
|
Args: mapToString(lastToolCall.Args),
|
||||||
}
|
}
|
||||||
// call a func
|
// call a func
|
||||||
_, ok := fnMap[fc.Name]
|
_, ok := fnMap[fc.Name]
|
||||||
|
|||||||
107
helpfuncs.go
107
helpfuncs.go
@@ -66,6 +66,14 @@ func isASCII(s string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapToString[V any](m map[string]V) string {
|
||||||
|
rs := strings.Builder{}
|
||||||
|
for k, v := range m {
|
||||||
|
fmt.Fprintf(&rs, "%v: %v\n", k, v)
|
||||||
|
}
|
||||||
|
return rs.String()
|
||||||
|
}
|
||||||
|
|
||||||
// stripThinkingFromMsg removes thinking blocks from assistant messages.
|
// stripThinkingFromMsg removes thinking blocks from assistant messages.
|
||||||
// Skips user, tool, and system messages as they may contain thinking examples.
|
// Skips user, tool, and system messages as they may contain thinking examples.
|
||||||
func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
|
func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
|
||||||
@@ -453,9 +461,8 @@ func updateFlexLayout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func executeCommandAndDisplay(cmdText string) {
|
func executeCommandAndDisplay(cmdText string) {
|
||||||
// Parse the command (split by spaces, but handle quoted arguments)
|
cmdText = strings.TrimSpace(cmdText)
|
||||||
cmdParts := parseCommand(cmdText)
|
if cmdText == "" {
|
||||||
if len(cmdParts) == 0 {
|
|
||||||
fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n")
|
fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n")
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
@@ -463,14 +470,58 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
colorText()
|
colorText()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
command := cmdParts[0]
|
workingDir := cfg.FilePickerDir
|
||||||
args := []string{}
|
// Handle cd command specially to update working directory
|
||||||
if len(cmdParts) > 1 {
|
if strings.HasPrefix(cmdText, "cd ") {
|
||||||
args = cmdParts[1:]
|
newDir := strings.TrimPrefix(cmdText, "cd ")
|
||||||
|
newDir = strings.TrimSpace(newDir)
|
||||||
|
// Handle cd ~ or cdHOME
|
||||||
|
if strings.HasPrefix(newDir, "~") {
|
||||||
|
home := os.Getenv("HOME")
|
||||||
|
newDir = strings.Replace(newDir, "~", home, 1)
|
||||||
}
|
}
|
||||||
// Create the command execution
|
// Check if directory exists
|
||||||
cmd := exec.Command(command, args...)
|
if _, err := os.Stat(newDir); err == nil {
|
||||||
cmd.Dir = cfg.FilePickerDir
|
workingDir = newDir
|
||||||
|
cfg.FilePickerDir = workingDir
|
||||||
|
// Update shell input label with new directory
|
||||||
|
shellInput.SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir))
|
||||||
|
outputContent := workingDir
|
||||||
|
// Add the command being executed to the chat
|
||||||
|
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
||||||
|
len(chatBody.Messages), cfg.ToolRole, cmdText)
|
||||||
|
fmt.Fprintf(textView, "%s\n", outputContent)
|
||||||
|
combinedMsg := models.RoleMsg{
|
||||||
|
Role: cfg.ToolRole,
|
||||||
|
Content: "$ " + cmdText + "\n\n" + outputContent,
|
||||||
|
}
|
||||||
|
chatBody.Messages = append(chatBody.Messages, combinedMsg)
|
||||||
|
if scrollToEndEnabled {
|
||||||
|
textView.ScrollToEnd()
|
||||||
|
}
|
||||||
|
colorText()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
outputContent := "cd: " + newDir + ": No such file or directory"
|
||||||
|
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
||||||
|
len(chatBody.Messages), cfg.ToolRole, cmdText)
|
||||||
|
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputContent)
|
||||||
|
combinedMsg := models.RoleMsg{
|
||||||
|
Role: cfg.ToolRole,
|
||||||
|
Content: "$ " + cmdText + "\n\n" + outputContent,
|
||||||
|
}
|
||||||
|
chatBody.Messages = append(chatBody.Messages, combinedMsg)
|
||||||
|
if scrollToEndEnabled {
|
||||||
|
textView.ScrollToEnd()
|
||||||
|
}
|
||||||
|
colorText()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use /bin/sh to support pipes, redirects, etc.
|
||||||
|
cmd := exec.Command("/bin/sh", "-c", cmdText)
|
||||||
|
cmd.Dir = workingDir
|
||||||
// Execute the command and get output
|
// Execute the command and get output
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
// Add the command being executed to the chat
|
// Add the command being executed to the chat
|
||||||
@@ -519,42 +570,6 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
shellHistoryPos = -1
|
shellHistoryPos = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseCommand splits command string handling quotes properly
|
|
||||||
func parseCommand(cmd string) []string {
|
|
||||||
var args []string
|
|
||||||
var current string
|
|
||||||
var inQuotes bool
|
|
||||||
var quoteChar rune
|
|
||||||
for _, r := range cmd {
|
|
||||||
switch r {
|
|
||||||
case '"', '\'':
|
|
||||||
if inQuotes {
|
|
||||||
if r == quoteChar {
|
|
||||||
inQuotes = false
|
|
||||||
} else {
|
|
||||||
current += string(r)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
inQuotes = true
|
|
||||||
quoteChar = r
|
|
||||||
}
|
|
||||||
case ' ', '\t':
|
|
||||||
if inQuotes {
|
|
||||||
current += string(r)
|
|
||||||
} else if current != "" {
|
|
||||||
args = append(args, current)
|
|
||||||
current = ""
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
current += string(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if current != "" {
|
|
||||||
args = append(args, current)
|
|
||||||
}
|
|
||||||
return args
|
|
||||||
}
|
|
||||||
|
|
||||||
// == search ==
|
// == search ==
|
||||||
|
|
||||||
// Global variables for search state
|
// Global variables for search state
|
||||||
|
|||||||
Reference in New Issue
Block a user