Commit dd0879a

mo khan <mo@mokhan.ca>
2025-06-22 03:14:14
fix: render shell output correctly
1 parent c98e237
Changed files (1)
internal
internal/del/assistant.go
@@ -36,9 +36,9 @@ func NewDel(provider AIProvider) *Del {
 			Name:        "run_command",
 			Description: "Execute a shell command and return the output",
 			Handler: func(args map[string]interface{}) (interface{}, error) {
-				cmd, ok := args["cmd"].(string)
+				cmd, ok := args["command"].(string)
 				if !ok {
-					return nil, fmt.Errorf("missing 'cmd' string argument")
+					return nil, fmt.Errorf("missing 'command' string argument")
 				}
 				out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
 				if err != nil {
@@ -104,8 +104,36 @@ func NewDel(provider AIProvider) *Del {
 		mcpServers: make(map[string]string),
 		chatHistory: []api.Message{
 			{
-				Role:    "system",
-				Content: "You are Del, a code assistant. For any request that involves system interaction, respond using: TOOL_USE: tool_name {json}. Available tools: run_command, read_file, write_file, list_dir. Example: TOOL_USE: list_dir {\"path\": \".\"}",
+				Role: "system",
+				Content: `You are Del, an assistant that can run system tools using TOOL_USE blocks.
+
+To use a tool, respond exactly like this:
+TOOL_USE: tool_name {json_args}
+
+Supported tools:
+
+1. run_command
+  - Runs a shell command.
+  - Args:
+    - "command": string (e.g., "ls -alh")
+
+2. read_file
+  - Reads the contents of a file.
+  - Args:
+    - "path": string
+
+3. write_file
+  - Writes content to a file.
+  - Args:
+    - "path": string
+    - "content": string
+
+4. list_dir
+  - Lists files in a directory.
+  - Args:
+    - "path": string (optional; default is ".")
+
+Only emit a TOOL_USE block if needed. Do not explain it. Only one TOOL_USE per response.`,
 			},
 		},
 	}
@@ -172,8 +200,16 @@ func (d *Del) handleToolCalls(ctx context.Context, response string) string {
 			continue
 		}
 
-		outBytes, _ := json.MarshalIndent(result, "", "  ")
-		finalOutput.WriteString(string(outBytes) + "\n")
+		switch val := result.(type) {
+		case string:
+			finalOutput.WriteString(val)
+			if !strings.HasSuffix(val, "\n") {
+				finalOutput.WriteString("\n")
+			}
+		default:
+			outBytes, _ := json.MarshalIndent(val, "", "  ")
+			finalOutput.WriteString(string(outBytes) + "\n")
+		}
 	}
 	return finalOutput.String()
 }