improved tool debugging

This commit is contained in:
Luxferre
2026-03-22 12:01:29 +02:00
parent 77d0d17c89
commit cd2de78c2c
16 changed files with 986 additions and 592 deletions
+5 -1
View File
@@ -9,10 +9,14 @@ MCP_SRC = ./cmd/sidekick-mcp
TUI_BIN = $(BIN_DIR)/sidekick-tui TUI_BIN = $(BIN_DIR)/sidekick-tui
MCP_BIN = $(BIN_DIR)/sidekick-mcp MCP_BIN = $(BIN_DIR)/sidekick-mcp
.PHONY: all build test clean install uninstall .PHONY: all build test clean install uninstall fmt
all: build all: build
fmt:
@./fmt.sh
@echo "Formatting complete according to AGENTS.md guidelines (gofmt + 2-space expansion + 112-char check)."
build: build:
@mkdir -p $(BIN_DIR) @mkdir -p $(BIN_DIR)
go build -o $(TUI_BIN) $(TUI_SRC) go build -o $(TUI_BIN) $(TUI_SRC)
+2
View File
@@ -116,6 +116,8 @@ headers = { "X-Custom-Header" = "value" } # Optional custom headers
role = "COORDINATOR" role = "COORDINATOR"
model_id = "default" model_id = "default"
system_prompt = "You are the primary coordinator agent." # This can be overridden by prompts.toml system_prompt = "You are the primary coordinator agent." # This can be overridden by prompts.toml
streaming = false # Set to true to enable real-time token streaming
log_intermediate = false # Set to true to log intermediate reasoning and tool calls
subagents = ["researcher"] subagents = ["researcher"]
toolsets = ["filesystem"] toolsets = ["filesystem"]
``` ```
+129 -37
View File
@@ -9,10 +9,13 @@ import (
) )
type Sidekick struct { type Sidekick struct {
Config AgentConfig Config AgentConfig
Model ModelConfig Model ModelConfig
ToolMapping map[string]string ToolMapping map[string]string
ToolRegistry map[string]ToolDefinition ToolRegistry map[string]ToolDefinition
StreamHandler func(string)
ReasoningHandler func(string)
IntermediateHandler func(string)
} }
func NewSidekick(config AgentConfig, model ModelConfig, extTools []ToolDefinition) *Sidekick { func NewSidekick(config AgentConfig, model ModelConfig, extTools []ToolDefinition) *Sidekick {
@@ -72,7 +75,8 @@ func (s *Sidekick) constructSystemMessage(agentPool map[string]*Sidekick) string
sb.WriteString("\n") sb.WriteString("\n")
} }
sb.WriteString("Action Format:\nJSON object with action. Example:\n") sb.WriteString("Action Format:\nJSON object with action. Example:\n")
sb.WriteString(`{"action": "TOOL_CALL", "params": {"tool_name": "x", "arguments": {}}, "reasoning": "..."}` + "\n") sb.WriteString(`{"action": "TOOL_CALL", "params": {"tool_name": "x", "arguments": {}}, "reasoning": "..."}` +
"\n")
return sb.String() return sb.String()
} }
@@ -84,42 +88,101 @@ func (s *Sidekick) Run(ctx context.Context, inCtx []Message, pool map[string]*Si
} }
var tFiles []string var tFiles []string
defer func() { defer func() {
for _, f := range tFiles { os.Remove(f) } for _, f := range tFiles {
os.Remove(f)
}
}() }()
iters := s.Config.MaxIterations iters := s.Config.MaxIterations
if iters <= 0 { iters = 10 } if iters <= 0 {
iters = 10
}
for i := 0; i < iters; i++ { for i := 0; i < iters; i++ {
msg, err := CallLLM(ctx, s.Model, history, s.ToolRegistry) done, res, err := s.runStep(ctx, &history, pool, &tFiles)
if err != nil { return "", fmt.Errorf("LLM call failed: %w", err) }
history = append(history, msg)
act, params, parsed, err := s.parseAction(msg)
if err != nil { if err != nil {
if !parsed { return "", err
act = ActionTypeRespond
params = map[string]interface{}{"response": msg.Content}
} else {
history = append(history, Message{Role: MessageRoleUser, Content: fmt.Sprintf("Error: %v", err)})
continue
}
} }
if act == ActionTypeRespond { if done {
if r, ok := params["response"].(string); ok { return r, nil } return res, nil
return msg.Content, nil
} }
res, err := s.dispatch(ctx, act, params, pool, &tFiles)
if err != nil { res = fmt.Sprintf("Error: %v", err) }
history = append(history, Message{Role: MessageRoleUser, Content: res})
} }
return "Max iterations reached.", nil return "Max iterations reached.", nil
} }
func (s *Sidekick) dispatch(ctx context.Context, a ActionType, p map[string]interface{}, pl map[string]*Sidekick, tf *[]string) (string, error) { func (s *Sidekick) logIntermediate(msg Message) {
if !s.Config.LogIntermediate || s.IntermediateHandler == nil {
return
}
if msg.Content != "" {
s.IntermediateHandler(fmt.Sprintf("LLM Output: %s", msg.Content))
} else if len(msg.ToolCalls) > 0 {
s.IntermediateHandler(fmt.Sprintf("Tool Call: %s", msg.ToolCalls[0].Function.Name))
}
}
func (s *Sidekick) runStep(ctx context.Context, history *[]Message,
pool map[string]*Sidekick, tFiles *[]string) (bool, string, error) {
var sc, rc func(string)
if s.Config.Streaming {
if s.StreamHandler != nil {
sc = s.StreamHandler
}
if s.ReasoningHandler != nil {
rc = s.ReasoningHandler
}
}
msg, err := CallLLM(ctx, s.Model, *history, s.ToolRegistry, sc, rc)
if err != nil {
return false, "", fmt.Errorf("LLM call failed: %w", err)
}
s.logIntermediate(msg)
*history = append(*history, msg)
act, params, parsed, err := s.parseAction(msg)
if err != nil {
if !parsed {
act, params = ActionTypeRespond, map[string]interface{}{"response": msg.Content}
} else {
*history = append(*history, Message{Role: MessageRoleUser, Content: fmt.Sprintf("Error: %v", err)})
return false, "", nil
}
}
if act == ActionTypeRespond {
if r, ok := params["response"].(string); ok {
return true, r, nil
}
return true, msg.Content, nil
}
res, err := s.dispatch(ctx, act, params, pool, tFiles)
if err != nil {
res = fmt.Sprintf("Error: %v", err)
}
*history = append(*history, Message{Role: MessageRoleUser, Content: res})
return false, "", nil
}
func (s *Sidekick) dispatch(ctx context.Context, a ActionType, p map[string]interface{},
pl map[string]*Sidekick, tf *[]string) (string, error) {
if a == ActionTypeToolCall { if a == ActionTypeToolCall {
tName, _ := p["tool_name"].(string) tName, _ := p["tool_name"].(string)
args, _ := p["arguments"].(map[string]interface{}) args, _ := p["arguments"].(map[string]interface{})
if aStr, ok := p["arguments"].(string); ok && args == nil { args, _ = ParseArgs(aStr) } if aStr, ok := p["arguments"].(string); ok && args == nil {
args, _ = ParseArgs(aStr)
}
if s.Config.LogIntermediate && s.IntermediateHandler != nil {
argsJSON, _ := json.Marshal(args)
s.IntermediateHandler(fmt.Sprintf("Tool Call: %s(%s)", tName, string(argsJSON)))
}
res, err := s.executeTool(ctx, tName, args) res, err := s.executeTool(ctx, tName, args)
if err != nil { return "", err } if err != nil {
return "", err
}
if s.Config.LogIntermediate && s.IntermediateHandler != nil {
truncRes := res
if len(truncRes) > 200 {
truncRes = truncRes[:200] + "..."
}
s.IntermediateHandler(fmt.Sprintf("Tool Result: %s", truncRes))
}
return BufferGate(res, tf, s.Config.ToolResponseThreshold) return BufferGate(res, tf, s.Config.ToolResponseThreshold)
} }
if a == ActionTypeDelegate { if a == ActionTypeDelegate {
@@ -133,37 +196,66 @@ func (s *Sidekick) dispatch(ctx context.Context, a ActionType, p map[string]inte
func (s *Sidekick) parseAction(msg Message) (ActionType, map[string]interface{}, bool, error) { func (s *Sidekick) parseAction(msg Message) (ActionType, map[string]interface{}, bool, error) {
if len(msg.ToolCalls) > 0 { if len(msg.ToolCalls) > 0 {
c := msg.ToolCalls[0] c := msg.ToolCalls[0]
return ActionTypeToolCall, map[string]interface{}{"tool_name": c.Function.Name, "arguments": c.Function.Arguments}, true, nil return ActionTypeToolCall,
map[string]interface{}{"tool_name": c.Function.Name, "arguments": c.Function.Arguments}, true, nil
} }
var env ActionEnvelope var env ActionEnvelope
c := strings.TrimSpace(msg.Content) c := strings.TrimSpace(msg.Content)
if strings.HasPrefix(c, "```json") && strings.HasSuffix(c, "```") { if strings.HasPrefix(c, "```json") && strings.HasSuffix(c, "```") {
c = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(c, "```json"), "```")) c = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(c, "```json"), "```"))
} }
if err := json.Unmarshal([]byte(c), &env); err != nil { return "", nil, false, err } if err := json.Unmarshal([]byte(c), &env); err != nil {
return "", nil, false, err
}
return env.Action, env.Params, true, nil return env.Action, env.Params, true, nil
} }
func (s *Sidekick) executeTool(ctx context.Context, sName string, args map[string]interface{}) (string, error) { func (s *Sidekick) executeTool(ctx context.Context, sName string, args map[string]interface{}) (string, error) {
rName, ok := s.ToolMapping[sName] rName, ok := s.ToolMapping[sName]
if !ok { return "", fmt.Errorf("tool %s not found", sName) } if !ok {
return "", fmt.Errorf("tool %s not found", sName)
}
tDef, ok := s.ToolRegistry[rName] tDef, ok := s.ToolRegistry[rName]
if !ok { return "", fmt.Errorf("tool %s not registered", rName) } if !ok {
return "", fmt.Errorf("tool %s not registered", rName)
}
if tDef.Internal { if tDef.Internal {
if tDef.Handler == nil { return "", fmt.Errorf("missing handler") } if tDef.Handler == nil {
return "", fmt.Errorf("missing handler")
}
return tDef.Handler(args) return tDef.Handler(args)
} }
return CallExternalTool(ctx, tDef.Toolset, rName, args) return CallExternalTool(ctx, tDef.Toolset, rName, args)
} }
func (s *Sidekick) executeDelegation(ctx context.Context, sid string, task string, pool map[string]*Sidekick) (string, error) { func (s *Sidekick) executeDelegation(ctx context.Context, sid string,
if !contains(s.Config.Subagents, sid) { return "", fmt.Errorf("subagent %s not allowed", sid) } task string, pool map[string]*Sidekick) (string, error) {
if !contains(s.Config.Subagents, sid) {
return "", fmt.Errorf("subagent %s not allowed", sid)
}
sub, ok := pool[sid] sub, ok := pool[sid]
if !ok { return "", fmt.Errorf("subagent %s not found", sid) } if !ok {
return sub.Run(ctx, []Message{{Role: MessageRoleUser, Content: task}}, pool) return "", fmt.Errorf("subagent %s not found", sid)
}
if s.Config.LogIntermediate && s.IntermediateHandler != nil {
s.IntermediateHandler(fmt.Sprintf("Delegating to %s: %s", sid, task))
}
res, err := sub.Run(ctx, []Message{{Role: MessageRoleUser, Content: task}}, pool)
if err == nil && s.Config.LogIntermediate && s.IntermediateHandler != nil {
truncRes := res
if len(truncRes) > 200 {
truncRes = truncRes[:200] + "..."
}
s.IntermediateHandler(fmt.Sprintf("Subagent %s Result: %s", sid, truncRes))
}
return res, err
} }
func contains(s []string, v string) bool { func contains(s []string, v string) bool {
for _, i := range s { if i == v { return true } } for _, i := range s {
if i == v {
return true
}
}
return false return false
} }
+137 -137
View File
@@ -1,162 +1,162 @@
package main package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os" "os"
"sidekick" "sidekick"
"github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server" "github.com/mark3labs/mcp-go/server"
) )
func main() { func main() {
cfg, err := sidekick.LoadConfig("config.toml") cfg, err := sidekick.LoadConfig("config.toml")
if err != nil { if err != nil {
log.Fatalf("Failed to load config: %v", err) log.Fatalf("Failed to load config: %v", err)
} }
// Disable standard logging to stdout if stdio transport is used, // Disable standard logging to stdout if stdio transport is used,
// so it doesn't corrupt the JSON-RPC stream. // so it doesn't corrupt the JSON-RPC stream.
if cfg.MCPListener.Transport == "stdio" || cfg.MCPListener.Transport == "" { if cfg.MCPListener.Transport == "stdio" || cfg.MCPListener.Transport == "" {
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
} }
tmpl, _ := sidekick.LoadPrompts("prompts.toml") tmpl, _ := sidekick.LoadPrompts("prompts.toml")
sidekick.InitMCPServers(cfg.MCPServers) sidekick.InitMCPServers(cfg.MCPServers)
pool := make(map[string]*sidekick.Sidekick) pool := make(map[string]*sidekick.Sidekick)
for id, aCfg := range cfg.Agents { for id, aCfg := range cfg.Agents {
if tmpl != nil && tmpl.Lookup(id) != nil { if tmpl != nil && tmpl.Lookup(id) != nil {
rendered, rErr := sidekick.RenderPrompt(tmpl, id) rendered, rErr := sidekick.RenderPrompt(tmpl, id)
if rErr == nil { if rErr == nil {
aCfg.SystemPrompt = rendered aCfg.SystemPrompt = rendered
} }
} }
mCfg := cfg.Models[aCfg.ModelID] mCfg := cfg.Models[aCfg.ModelID]
pool[id] = sidekick.NewSidekick(aCfg, mCfg, nil) pool[id] = sidekick.NewSidekick(aCfg, mCfg, nil)
} }
entryAgent := "coordinator" entryAgent := "coordinator"
if _, ok := pool[entryAgent]; !ok { if _, ok := pool[entryAgent]; !ok {
for id := range pool { for id := range pool {
entryAgent = id entryAgent = id
break break
} }
} }
agent := pool[entryAgent] agent := pool[entryAgent]
mcpServer := server.NewMCPServer("Sidekick MCP", "1.0.0") mcpServer := server.NewMCPServer("Sidekick MCP", "1.0.0")
mcpServer.AddTool( mcpServer.AddTool(
mcp.NewToolWithRawSchema( mcp.NewToolWithRawSchema(
"query", "query",
"Ask Sidekick a question or provide a message, optionally with conversation history", "Ask Sidekick a question or provide a message, optionally with conversation history",
json.RawMessage(`{ json.RawMessage(`{
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "type": "string", "description": "The user's message" }, "message": { "type": "string", "description": "The user's message" },
"history": { "history": {
"type": "array", "type": "array",
"description": "Optional list of previous messages", "description": "Optional list of previous messages",
"items": { "items": {
"type": "object", "type": "object",
"properties": { "properties": {
"role": { "type": "string" }, "role": { "type": "string" },
"content": { "type": "string" } "content": { "type": "string" }
}, },
"required": ["role", "content"] "required": ["role", "content"]
} }
} }
}, },
"required": ["message"] "required": ["message"]
}`), }`),
), ),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
msg, err := request.RequireString("message") msg, err := request.RequireString("message")
if err != nil { if err != nil {
return nil, fmt.Errorf("message argument is required and must be a string: %v", err) return nil, fmt.Errorf("message argument is required and must be a string: %v", err)
} }
var inCtx []sidekick.Message var inCtx []sidekick.Message
args := request.GetArguments() args := request.GetArguments()
if histInter, ok := args["history"]; ok { if histInter, ok := args["history"]; ok {
if histList, ok := histInter.([]interface{}); ok { if histList, ok := histInter.([]interface{}); ok {
for _, h := range histList { for _, h := range histList {
if hMap, ok := h.(map[string]interface{}); ok { if hMap, ok := h.(map[string]interface{}); ok {
roleInter, okRole := hMap["role"] roleInter, okRole := hMap["role"]
contentInter, okContent := hMap["content"] contentInter, okContent := hMap["content"]
if okRole && okContent { if okRole && okContent {
if roleStr, isStr := roleInter.(string); isStr { if roleStr, isStr := roleInter.(string); isStr {
if contentStr, isStrContent := contentInter.(string); isStrContent { if contentStr, isStrContent := contentInter.(string); isStrContent {
inCtx = append(inCtx, sidekick.Message{ inCtx = append(inCtx, sidekick.Message{
Role: sidekick.MessageRole(roleStr), Role: sidekick.MessageRole(roleStr),
Content: contentStr, Content: contentStr,
}) })
} }
} }
} }
} }
} }
} }
} }
inCtx = append(inCtx, sidekick.Message{ inCtx = append(inCtx, sidekick.Message{
Role: sidekick.MessageRoleUser, Role: sidekick.MessageRoleUser,
Content: msg, Content: msg,
}) })
responseStr, err := agent.Run(ctx, inCtx, pool) responseStr, err := agent.Run(ctx, inCtx, pool)
if err != nil { if err != nil {
return nil, fmt.Errorf("agent run failed: %w", err) return nil, fmt.Errorf("agent run failed: %w", err)
} }
// Add the final response to the history structure to be returned // Add the final response to the history structure to be returned
outHistory := append(inCtx, sidekick.Message{ outHistory := append(inCtx, sidekick.Message{
Role: sidekick.MessageRole("assistant"), Role: sidekick.MessageRole("assistant"),
Content: responseStr, Content: responseStr,
}) })
resultObj := map[string]interface{}{ resultObj := map[string]interface{}{
"response": responseStr, "response": responseStr,
"history": outHistory, "history": outHistory,
} }
resultBytes, err := json.Marshal(resultObj) resultBytes, err := json.Marshal(resultObj)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to marshal result: %w", err) return nil, fmt.Errorf("failed to marshal result: %w", err)
} }
return &mcp.CallToolResult{ return &mcp.CallToolResult{
Content: []mcp.Content{ Content: []mcp.Content{
mcp.NewTextContent(string(resultBytes)), mcp.NewTextContent(string(resultBytes)),
}, },
}, nil }, nil
}, },
) )
transport := cfg.MCPListener.Transport transport := cfg.MCPListener.Transport
if transport == "stdio" || transport == "" { if transport == "stdio" || transport == "" {
if err := server.ServeStdio(mcpServer); err != nil { if err := server.ServeStdio(mcpServer); err != nil {
log.Fatalf("MCP Server (stdio) error: %v", err) log.Fatalf("MCP Server (stdio) error: %v", err)
} }
} else if transport == "http" || transport == "sse" { // mcp-go supports SSE, which is Streamable HTTP } else if transport == "http" || transport == "sse" { // mcp-go supports SSE, which is Streamable HTTP
port := cfg.MCPListener.Port port := cfg.MCPListener.Port
if port == 0 { if port == 0 {
port = 8080 port = 8080
} }
addr := fmt.Sprintf(":%d", port) addr := fmt.Sprintf(":%d", port)
log.Printf("Starting MCP Streamable HTTP server on %s", addr) log.Printf("Starting MCP Streamable HTTP server on %s", addr)
srv := server.NewStreamableHTTPServer(mcpServer) srv := server.NewStreamableHTTPServer(mcpServer)
if err := srv.Start(addr); err != nil { if err := srv.Start(addr); err != nil {
log.Fatalf("MCP Server (http) error: %v", err) log.Fatalf("MCP Server (http) error: %v", err)
} }
} else { } else {
log.Fatalf("Unsupported MCP listener transport: %s", transport) log.Fatalf("Unsupported MCP listener transport: %s", transport)
} }
} }
+69 -69
View File
@@ -1,10 +1,10 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"testing" "testing"
"github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/mcp"
) )
// Since we cannot easily test the main() function without significant refactoring // Since we cannot easily test the main() function without significant refactoring
@@ -12,84 +12,84 @@ import (
// This mirrors the logic in main.go but allows for unit testing. // This mirrors the logic in main.go but allows for unit testing.
func TestQueryToolHandler(t *testing.T) { func TestQueryToolHandler(t *testing.T) {
// In a real scenario, we might want to refactor main.go to export a // In a real scenario, we might want to refactor main.go to export a
// function that creates the handler. For now, we'll verify the logic // function that creates the handler. For now, we'll verify the logic
// we've implemented in the main.go file by testing the expected // we've implemented in the main.go file by testing the expected
// behavior of a similar handler. // behavior of a similar handler.
t.Run("ValidRequest", func(t *testing.T) { t.Run("ValidRequest", func(t *testing.T) {
// Mock arguments // Mock arguments
args := map[string]interface{}{ args := map[string]interface{}{
"message": "Hello Sidekick", "message": "Hello Sidekick",
"history": []interface{}{ "history": []interface{}{
map[string]interface{}{"role": "user", "content": "Hi"}, map[string]interface{}{"role": "user", "content": "Hi"},
map[string]interface{}{"role": "assistant", "content": "Hello! How can I help?"}, map[string]interface{}{"role": "assistant", "content": "Hello! How can I help?"},
}, },
} }
req := mcp.CallToolRequest{} req := mcp.CallToolRequest{}
req.Params.Name = "query" req.Params.Name = "query"
req.Params.Arguments = args req.Params.Arguments = args
// We verify the RequireString and GetArguments logic here // We verify the RequireString and GetArguments logic here
msg, err := req.RequireString("message") msg, err := req.RequireString("message")
if err != nil || msg != "Hello Sidekick" { if err != nil || msg != "Hello Sidekick" {
t.Errorf("RequireString failed: %v", err) t.Errorf("RequireString failed: %v", err)
} }
rawArgs := req.GetArguments() rawArgs := req.GetArguments()
histInter, ok := rawArgs["history"] histInter, ok := rawArgs["history"]
if !ok { if !ok {
t.Fatal("history missing from arguments") t.Fatal("history missing from arguments")
} }
histList, ok := histInter.([]interface{}) histList, ok := histInter.([]interface{})
if !ok || len(histList) != 2 { if !ok || len(histList) != 2 {
t.Errorf("history list invalid: %v", histInter) t.Errorf("history list invalid: %v", histInter)
} }
}) })
t.Run("MissingMessage", func(t *testing.T) { t.Run("MissingMessage", func(t *testing.T) {
req := mcp.CallToolRequest{} req := mcp.CallToolRequest{}
req.Params.Name = "query" req.Params.Name = "query"
req.Params.Arguments = map[string]interface{}{} req.Params.Arguments = map[string]interface{}{}
_, err := req.RequireString("message") _, err := req.RequireString("message")
if err == nil { if err == nil {
t.Error("expected error for missing message") t.Error("expected error for missing message")
} }
}) })
} }
func TestResultMarshalling(t *testing.T) { func TestResultMarshalling(t *testing.T) {
// Verify the format of the response as requested: {"response", "history"} // Verify the format of the response as requested: {"response", "history"}
type msg struct { type msg struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"content"` Content string `json:"content"`
} }
result := map[string]interface{}{ result := map[string]interface{}{
"response": "Agent response", "response": "Agent response",
"history": []msg{ "history": []msg{
{Role: "user", Content: "User message"}, {Role: "user", Content: "User message"},
{Role: "assistant", Content: "Agent response"}, {Role: "assistant", Content: "Agent response"},
}, },
} }
data, err := json.Marshal(result) data, err := json.Marshal(result)
if err != nil { if err != nil {
t.Fatalf("marshal failed: %v", err) t.Fatalf("marshal failed: %v", err)
} }
var decoded map[string]interface{} var decoded map[string]interface{}
if err := json.Unmarshal(data, &decoded); err != nil { if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("unmarshal failed: %v", err) t.Fatalf("unmarshal failed: %v", err)
} }
if _, ok := decoded["response"]; !ok { if _, ok := decoded["response"]; !ok {
t.Error("response key missing") t.Error("response key missing")
} }
if _, ok := decoded["history"]; !ok { if _, ok := decoded["history"]; !ok {
t.Error("history key missing") t.Error("history key missing")
} }
} }
+173 -38
View File
@@ -16,29 +16,30 @@ import (
var ( var (
titleStyle = lipgloss.NewStyle(). titleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")). Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#25A065")). Background(lipgloss.Color("#25A065")).
Padding(0, 1). Padding(0, 1).
Bold(true) Bold(true)
statusStyle = lipgloss.NewStyle(). statusStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")). Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#3C3C3C")). Background(lipgloss.Color("#3C3C3C")).
Padding(0, 1) Padding(0, 1)
viewportStyle = lipgloss.NewStyle(). viewportStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")). BorderForeground(lipgloss.Color("62")).
Padding(0, 1) Padding(0, 1)
textareaStyle = lipgloss.NewStyle(). textareaStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240")). BorderForeground(lipgloss.Color("240")).
Padding(0, 1) Padding(0, 1)
textareaFocusStyle = lipgloss.NewStyle(). textareaFocusStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")). BorderForeground(lipgloss.Color("62")).
Padding(0, 1) Padding(0, 1)
userStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6")).Bold(true) userStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6")).Bold(true)
agentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true) agentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true)
sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Italic(true) sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Italic(true)
bannerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFF00")).Bold(true)
banner = ` _ __ __ _ __ banner = ` _ __ __ _ __
___ (_) ___/ / ___ / /__ (_) ____ / /__ ___ (_) ___/ / ___ / /__ (_) ____ / /__
@@ -48,17 +49,20 @@ var (
) )
type model struct { type model struct {
viewport viewport.Model viewport viewport.Model
textarea textarea.Model textarea textarea.Model
agent *sidekick.Sidekick agent *sidekick.Sidekick
pool map[string]*sidekick.Sidekick pool map[string]*sidekick.Sidekick
history []sidekick.Message history []sidekick.Message
isThinking bool transientSteps []string
err error currentStream string
ready bool currentReasoning string
width int isThinking bool
height int err error
renderer *glamour.TermRenderer ready bool
width int
height int
renderer *glamour.TermRenderer
} }
type agentResponseMsg struct { type agentResponseMsg struct {
@@ -66,6 +70,32 @@ type agentResponseMsg struct {
err error err error
} }
type streamMsg string
type reasoningMsg string
type intermediateMsg string
var streamChan = make(chan string, 100)
var reasoningChan = make(chan string, 100)
var intermediateChan = make(chan string, 100)
func waitForStream() tea.Cmd {
return func() tea.Msg {
return streamMsg(<-streamChan)
}
}
func waitForReasoning() tea.Cmd {
return func() tea.Msg {
return reasoningMsg(<-reasoningChan)
}
}
func waitForIntermediate() tea.Cmd {
return func() tea.Msg {
return intermediateMsg(<-intermediateChan)
}
}
func initialModel() model { func initialModel() model {
ta := textarea.New() ta := textarea.New()
ta.Placeholder = "Type a message..." ta.Placeholder = "Type a message..."
@@ -108,15 +138,41 @@ func initialModel() model {
agent = pool[entryAgent] agent = pool[entryAgent]
} }
// Force configs on all pool agents, including fallback 'agent'
agentsToUpdate := []*sidekick.Sidekick{agent}
for _, a := range pool {
agentsToUpdate = append(agentsToUpdate, a)
}
for _, a := range agentsToUpdate {
if a != nil {
a.Config.Streaming = true
a.Config.LogIntermediate = true
a.StreamHandler = func(s string) {
streamChan <- s
}
a.ReasoningHandler = func(s string) {
reasoningChan <- s
}
a.IntermediateHandler = func(s string) {
intermediateChan <- s
}
}
}
return model{ return model{
textarea: ta, textarea: ta,
agent: agent, agent: agent,
pool: pool, pool: pool,
history: []sidekick.Message{{Role: sidekick.MessageRoleSystem, Content: banner + "\n\nSidekick initialized. Type a message below."}}, history: []sidekick.Message{
{Role: sidekick.MessageRoleSystem, Content: banner + "\n\nSidekick initialized. Type a message below."},
},
} }
} }
func (m model) Init() tea.Cmd { return textarea.Blink } func (m model) Init() tea.Cmd {
return tea.Batch(textarea.Blink, waitForStream(), waitForReasoning(), waitForIntermediate())
}
func (m model) renderMessage(msg sidekick.Message) string { func (m model) renderMessage(msg sidekick.Message) string {
label := "" label := ""
@@ -132,13 +188,38 @@ func (m model) renderMessage(msg sidekick.Message) string {
} }
content := msg.Content content := msg.Content
if m.renderer != nil && msg.Role != sidekick.MessageRoleSystem { reasoning := msg.Reasoning
rendered, err := m.renderer.Render(content)
if err == nil { // If this is the banner message, handle it specially to preserve ASCII art
content = strings.TrimSpace(rendered) if strings.Contains(content, "----") && msg.Role == sidekick.MessageRoleSystem {
if strings.HasPrefix(content, banner) {
bannerPart := bannerStyle.Render(banner)
otherPart := strings.TrimPrefix(content, banner)
if m.renderer != nil && strings.TrimSpace(otherPart) != "" {
if r, err := m.renderer.Render(otherPart); err == nil {
otherPart = "\n" + strings.TrimSpace(r)
}
}
return label + "\n" + bannerPart + otherPart
} }
} }
if m.renderer != nil {
if reasoning != "" {
if r, err := m.renderer.Render("> *Thinking:*\n>\n> " + reasoning); err == nil {
reasoning = strings.TrimSpace(r) + "\n"
}
}
if content != "" {
if r, err := m.renderer.Render(content); err == nil {
content = strings.TrimSpace(r)
}
}
}
if reasoning != "" {
return label + "\n" + reasoning + content
}
return label + "\n" + content return label + "\n" + content
} }
@@ -147,6 +228,32 @@ func (m *model) updateViewportContent() {
for _, msg := range m.history { for _, msg := range m.history {
rendered = append(rendered, m.renderMessage(msg)) rendered = append(rendered, m.renderMessage(msg))
} }
for _, step := range m.transientSteps {
wrapped := step
if m.renderer != nil {
if r, err := m.renderer.Render(step); err == nil {
wrapped = strings.TrimSpace(r)
}
}
rendered = append(rendered, sysStyle.Render("System")+"\n"+wrapped)
}
if m.currentReasoning != "" || m.currentStream != "" {
content := m.currentStream
reasoning := m.currentReasoning
if m.renderer != nil {
if reasoning != "" {
if r, err := m.renderer.Render("> *Thinking:*\n>\n> " + reasoning); err == nil {
reasoning = strings.TrimSpace(r) + "\n"
}
}
if content != "" {
if r, err := m.renderer.Render(content); err == nil {
content = strings.TrimSpace(r)
}
}
}
rendered = append(rendered, agentStyle.Render("Sidekick")+"\n"+reasoning+content)
}
m.viewport.SetContent(strings.Join(rendered, "\n\n")) m.viewport.SetContent(strings.Join(rendered, "\n\n"))
} }
@@ -212,7 +319,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Role: sidekick.MessageRoleUser, Role: sidekick.MessageRoleUser,
Content: v, Content: v,
}) })
m.currentStream = ""
m.currentReasoning = ""
m.transientSteps = nil
m.updateViewportContent() m.updateViewportContent()
m.textarea.Reset() m.textarea.Reset()
m.viewport.GotoBottom() m.viewport.GotoBottom()
@@ -220,8 +329,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.runAgent() return m, m.runAgent()
} }
case streamMsg:
if !m.isThinking {
return m, waitForStream()
}
m.currentStream += string(msg)
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForStream()
case reasoningMsg:
if !m.isThinking {
return m, waitForReasoning()
}
m.currentReasoning += string(msg)
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForReasoning()
case intermediateMsg:
if !m.isThinking {
return m, waitForIntermediate()
}
m.transientSteps = append(m.transientSteps, string(msg))
m.updateViewportContent()
m.viewport.GotoBottom()
return m, waitForIntermediate()
case agentResponseMsg: case agentResponseMsg:
m.isThinking = false m.isThinking = false
m.currentStream = ""
m.currentReasoning = ""
m.transientSteps = nil
if msg.err != nil { if msg.err != nil {
m.history = append(m.history, sidekick.Message{ m.history = append(m.history, sidekick.Message{
Role: sidekick.MessageRoleSystem, Role: sidekick.MessageRoleSystem,
@@ -280,7 +416,6 @@ func (m model) View() string {
) )
} }
func main() { func main() {
if _, err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Run(); err != nil { if _, err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Run(); err != nil {
log.Fatal(err) log.Fatal(err)
+57 -57
View File
@@ -1,82 +1,82 @@
package main package main
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
// Since sidekick-tui/main.go uses global variables and is a tea.Model, // Since sidekick-tui/main.go uses global variables and is a tea.Model,
// we test basic model behavior. // we test basic model behavior.
func TestInitialModel(t *testing.T) { func TestInitialModel(t *testing.T) {
// For testing, sidekick.LoadConfig would fail or look in its default dir. // For testing, sidekick.LoadConfig would fail or look in its default dir.
// This ensures that the model can be initialized even without a config. // This ensures that the model can be initialized even without a config.
m := initialModel() m := initialModel()
if m.agent == nil { if m.agent == nil {
t.Error("expected default agent to be initialized even without config") t.Error("expected default agent to be initialized even without config")
} }
if m.textarea.Value() != "" { if m.textarea.Value() != "" {
t.Errorf("expected empty textarea, got %s", m.textarea.Value()) t.Errorf("expected empty textarea, got %s", m.textarea.Value())
} }
if len(m.history) != 1 { if len(m.history) != 1 {
t.Errorf("expected 1 initial message in history, got %d", len(m.history)) t.Errorf("expected 1 initial message in history, got %d", len(m.history))
} }
if !strings.Contains(m.history[0].Content, "Sidekick initialized") { if !strings.Contains(m.history[0].Content, "Sidekick initialized") {
t.Error("expected initial message content to contain 'Sidekick initialized'") t.Error("expected initial message content to contain 'Sidekick initialized'")
} }
} }
func TestModelUpdate(t *testing.T) { func TestModelUpdate(t *testing.T) {
m := initialModel() m := initialModel()
m.ready = true m.ready = true
m.width = 80 m.width = 80
m.height = 24 m.height = 24
// Test a key message (Enter) // Test a key message (Enter)
m.textarea.SetValue("Hello") m.textarea.SetValue("Hello")
newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
tm := newModel.(model) tm := newModel.(model)
if tm.isThinking != true { if tm.isThinking != true {
t.Error("expected model to be in thinking state after Enter") t.Error("expected model to be in thinking state after Enter")
} }
if tm.textarea.Value() != "" { if tm.textarea.Value() != "" {
t.Error("expected textarea to be reset after Enter") t.Error("expected textarea to be reset after Enter")
} }
if cmd == nil { if cmd == nil {
t.Error("expected a command after Enter") t.Error("expected a command after Enter")
} }
// Test an agent response message // Test an agent response message
respModel, _ := tm.Update(agentResponseMsg{response: "Hi"}) respModel, _ := tm.Update(agentResponseMsg{response: "Hi"})
rm := respModel.(model) rm := respModel.(model)
if rm.isThinking != false { if rm.isThinking != false {
t.Error("expected model to stop thinking after response") t.Error("expected model to stop thinking after response")
} }
if len(rm.history) < 2 { if len(rm.history) < 2 {
t.Error("expected history list to grow after response") t.Error("expected history list to grow after response")
} }
} }
func TestMessageWrapping(t *testing.T) { func TestMessageWrapping(t *testing.T) {
m := initialModel() m := initialModel()
m.ready = true m.ready = true
m.width = 10 m.width = 10
m.height = 20 m.height = 20
m.viewport = viewport.New(10, 5) // Very narrow m.viewport = viewport.New(10, 5) // Very narrow
longMsg := "This is a very long message that should be wrapped." longMsg := "This is a very long message that should be wrapped."
respModel, _ := m.Update(agentResponseMsg{response: longMsg}) respModel, _ := m.Update(agentResponseMsg{response: longMsg})
rm := respModel.(model) rm := respModel.(model)
content := rm.viewport.View() content := rm.viewport.View()
if !strings.Contains(content, "\n") { if !strings.Contains(content, "\n") {
t.Errorf("expected long message to be wrapped, but no newline found in viewport") t.Errorf("expected long message to be wrapped, but no newline found in viewport")
} }
} }
+2
View File
@@ -57,6 +57,8 @@ type AgentConfig struct {
Toolsets []string `toml:"toolsets"` // Allowed tool namespaces (MCP servers) Toolsets []string `toml:"toolsets"` // Allowed tool namespaces (MCP servers)
Subagents []string `toml:"subagents"` // Allowed subagent IDs Subagents []string `toml:"subagents"` // Allowed subagent IDs
EnableShellExec bool `toml:"enable_shell_exec"` EnableShellExec bool `toml:"enable_shell_exec"`
Streaming bool `toml:"streaming"`
LogIntermediate bool `toml:"log_intermediate"`
ToolResponseThreshold int `toml:"-"` ToolResponseThreshold int `toml:"-"`
} }
+8
View File
@@ -36,6 +36,8 @@ port = 8080
role = "COORDINATOR" role = "COORDINATOR"
model_id = "default" model_id = "default"
enable_shell_exec = true enable_shell_exec = true
streaming = false
log_intermediate = false
system_prompt = "You are the Lead Architect. You oversee the software development lifecycle, plan features, and delegate implementation to specialized subagents." system_prompt = "You are the Lead Architect. You oversee the software development lifecycle, plan features, and delegate implementation to specialized subagents."
description = "Lead Architect and project coordinator" description = "Lead Architect and project coordinator"
max_iterations = 15 max_iterations = 15
@@ -51,6 +53,8 @@ port = 8080
role = "SPECIALIST" role = "SPECIALIST"
model_id = "default" model_id = "default"
enable_shell_exec = true enable_shell_exec = true
streaming = false
log_intermediate = false
system_prompt = "You are a Senior Software Engineer specializing in implementation. Your goal is to write clean, efficient, and well-documented code based on the architect's instructions." system_prompt = "You are a Senior Software Engineer specializing in implementation. Your goal is to write clean, efficient, and well-documented code based on the architect's instructions."
description = "Senior Developer focused on implementation and refactoring" description = "Senior Developer focused on implementation and refactoring"
max_iterations = 10 max_iterations = 10
@@ -64,6 +68,8 @@ port = 8080
[agents.reviewer] [agents.reviewer]
role = "SPECIALIST" role = "SPECIALIST"
model_id = "fast" model_id = "fast"
streaming = false
log_intermediate = false
system_prompt = "You are a Quality Assurance Specialist and Code Reviewer. Your role is to analyze code for potential bugs, security vulnerabilities, and style violations." system_prompt = "You are a Quality Assurance Specialist and Code Reviewer. Your role is to analyze code for potential bugs, security vulnerabilities, and style violations."
description = "Code Reviewer and Quality Assurance specialist" description = "Code Reviewer and Quality Assurance specialist"
max_iterations = 8 max_iterations = 8
@@ -78,6 +84,8 @@ port = 8080
role = "SPECIALIST" role = "SPECIALIST"
model_id = "fast" model_id = "fast"
enable_shell_exec = true enable_shell_exec = true
streaming = false
log_intermediate = false
system_prompt = "You are a Test Engineer. Your primary responsibility is to write and execute tests to ensure software reliability and correctness." system_prompt = "You are a Test Engineer. Your primary responsibility is to write and execute tests to ensure software reliability and correctness."
description = "Test Engineer focused on automated testing and verification" description = "Test Engineer focused on automated testing and verification"
max_iterations = 10 max_iterations = 10
+15 -9
View File
@@ -12,14 +12,16 @@ import (
func DefaultInternalTools() map[string]ToolDefinition { func DefaultInternalTools() map[string]ToolDefinition {
return map[string]ToolDefinition{ return map[string]ToolDefinition{
"read_file": { "read_file": {
Name: "read_file", Name: "read_file",
Description: "Read a file, optionally with line windowing. Every line is prefixed with 'line:hash:' (e.g. '1:abcd:content').", Description: "Read a file, optionally with line windowing. " +
"Every line is prefixed with 'line:hash:' (e.g. '1:abcd:content').",
Parameters: map[string]interface{}{ Parameters: map[string]interface{}{
"type": "object", "type": "object",
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"path": map[string]interface{}{"type": "string"}, "path": map[string]interface{}{"type": "string"},
"offset": map[string]interface{}{"type": "integer", "description": "Starting line number (0-indexed)"}, "offset": map[string]interface{}{"type": "integer",
"limit": map[string]interface{}{"type": "integer", "description": "Number of lines to read"}, "description": "Starting line number (0-indexed)"},
"limit": map[string]interface{}{"type": "integer", "description": "Number of lines to read"},
}, },
"required": []string{"path"}, "required": []string{"path"},
}, },
@@ -41,8 +43,9 @@ func DefaultInternalTools() map[string]ToolDefinition {
Handler: writeFileHandler, Handler: writeFileHandler,
}, },
"grep_file": { "grep_file": {
Name: "grep_file", Name: "grep_file",
Description: "Regex search with surrounding context. Every line is prefixed with 'line:hash:' (e.g. '1:abcd:content').", Description: "Regex search with surrounding context. " +
"Every line is prefixed with 'line:hash:' (e.g. '1:abcd:content').",
Parameters: map[string]interface{}{ Parameters: map[string]interface{}{
"type": "object", "type": "object",
"properties": map[string]interface{}{ "properties": map[string]interface{}{
@@ -56,8 +59,11 @@ func DefaultInternalTools() map[string]ToolDefinition {
Handler: grepFileHandler, Handler: grepFileHandler,
}, },
"edit_file": { "edit_file": {
Name: "edit_file", Name: "edit_file",
Description: "Replace a string or a block of lines in a file. If 'old_string' contains 'line:hash:' prefixes for every line, it performs a precise replacement at the specified line numbers. Otherwise, it performs a standard first-occurrence replacement. Both multiline strings and 'line:hash:' stripping are supported.", Description: "Replace a string or a block of lines in a file. " +
"If 'old_string' contains 'line:hash:' prefixes for every line, it performs a precise replacement " +
"at the specified line numbers. Otherwise, it performs a standard first-occurrence replacement. " +
"Both multiline strings and 'line:hash:' stripping are supported.",
Parameters: map[string]interface{}{ Parameters: map[string]interface{}{
"type": "object", "type": "object",
"properties": map[string]interface{}{ "properties": map[string]interface{}{
+23 -8
View File
@@ -9,18 +9,32 @@ import (
func TestShellExecHandler(t *testing.T) { func TestShellExecHandler(t *testing.T) {
// Success // Success
got, err := shellExecHandler(map[string]interface{}{"command": "echo hello"}) got, err := shellExecHandler(map[string]interface{}{"command": "echo hello"})
if err != nil { t.Fatal(err) } if err != nil {
if strings.TrimSpace(got) != "hello" { t.Errorf("expected hello, got %s", got) } t.Fatal(err)
}
if strings.TrimSpace(got) != "hello" {
t.Errorf("expected hello, got %s", got)
}
// Failure // Failure
got, err = shellExecHandler(map[string]interface{}{"command": "ls /nonexistent-file-path-that-should-not-exist"}) got, err = shellExecHandler(map[string]interface{}{
if err != nil { t.Fatal(err) } "command": "ls /nonexistent-file-path-that-should-not-exist",
if !strings.Contains(got, "Error:") { t.Errorf("expected error in output, got %s", got) } })
if err != nil {
t.Fatal(err)
}
if !strings.Contains(got, "Error:") {
t.Errorf("expected error in output, got %s", got)
}
// Empty // Empty
got, err = shellExecHandler(map[string]interface{}{"command": "true"}) got, err = shellExecHandler(map[string]interface{}{"command": "true"})
if err != nil { t.Fatal(err) } if err != nil {
if got != "(empty output)" { t.Errorf("expected empty output, got %s", got) } t.Fatal(err)
}
if got != "(empty output)" {
t.Errorf("expected empty output, got %s", got)
}
} }
func TestHashLine(t *testing.T) { func TestHashLine(t *testing.T) {
@@ -82,7 +96,8 @@ func TestEditFileHandler_ExactHashlines(t *testing.T) {
got, _ := os.ReadFile(tmpFile) got, _ := os.ReadFile(tmpFile)
expected := "line one\nreplaced two\nreplaced three\nline four" expected := "line one\nreplaced two\nreplaced three\nline four"
if string(got) != expected { if string(got) != expected {
t.Errorf("editFileHandler did not precisely replace using hashlines.\nGot: %s\nExpected: %s", string(got), expected) t.Errorf("editFileHandler did not precisely replace using hashlines. Got: %s Expected: %s",
string(got), expected)
} }
} }
+170 -75
View File
@@ -1,122 +1,217 @@
package sidekick package sidekick
import ( import (
"bytes" "bufio"
"context" "bytes"
"encoding/json" "context"
"fmt" "encoding/json"
"io" "fmt"
"net/http" "io"
"net/http"
"strings"
) )
// LLMClient represents an abstraction over the LLM provider // LLMClient represents an abstraction over the LLM provider
type LLMClient interface { type LLMClient interface {
Call(ctx context.Context, model ModelConfig, messages []Message, tools map[string]ToolDefinition) (Message, error) Call(ctx context.Context, model ModelConfig, messages []Message,
tools map[string]ToolDefinition, streamCallback, reasoningCallback func(string)) (Message, error)
} }
// standardLLMClient is an OpenAI-compatible implementation // standardLLMClient is an OpenAI-compatible implementation
type standardLLMClient struct{} type standardLLMClient struct{}
func (s *standardLLMClient) Call(ctx context.Context, model ModelConfig, msgs []Message, ts map[string]ToolDefinition) (Message, error) { func (s *standardLLMClient) Call(ctx context.Context, model ModelConfig, msgs []Message,
client := &http.Client{ ts map[string]ToolDefinition, streamCallback, reasoningCallback func(string)) (Message, error) {
Timeout: model.Timeout, client := &http.Client{
} Timeout: model.Timeout,
}
reqBody := map[string]interface{}{ reqBody := map[string]interface{}{
"model": model.ModelID, "model": model.ModelID,
"messages": msgs, "messages": msgs,
"temperature": model.Temperature, "temperature": model.Temperature,
} }
if model.MaxTokens > 0 { if model.MaxTokens > 0 {
reqBody["max_tokens"] = model.MaxTokens reqBody["max_tokens"] = model.MaxTokens
} }
if len(ts) > 0 { if len(ts) > 0 {
var tools []map[string]interface{} var tools []map[string]interface{}
for _, t := range ts { for _, t := range ts {
tools = append(tools, t.ConvertToOpenAITool()) tools = append(tools, t.ConvertToOpenAITool())
} }
reqBody["tools"] = tools reqBody["tools"] = tools
} }
jsonBody, err := json.Marshal(reqBody) if streamCallback != nil {
if err != nil { reqBody["stream"] = true
return Message{}, fmt.Errorf("failed to marshal request: %w", err) }
}
req, err := http.NewRequestWithContext(ctx, "POST", model.Endpoint+"/chat/completions", bytes.NewBuffer(jsonBody)) jsonBody, err := json.Marshal(reqBody)
if err != nil { if err != nil {
return Message{}, fmt.Errorf("failed to create request: %w", err) return Message{}, fmt.Errorf("failed to marshal request: %w", err)
} }
req.Header.Set("Content-Type", "application/json") req, err := http.NewRequestWithContext(ctx, "POST",
req.Header.Set("Authorization", "Bearer "+model.APIKey) model.Endpoint+"/chat/completions", bytes.NewBuffer(jsonBody))
if err != nil {
return Message{}, fmt.Errorf("failed to create request: %w", err)
}
resp, err := client.Do(req) req.Header.Set("Content-Type", "application/json")
if err != nil { req.Header.Set("Authorization", "Bearer "+model.APIKey)
return Message{}, fmt.Errorf("HTTP request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) resp, err := client.Do(req)
if err != nil { if err != nil {
return Message{}, fmt.Errorf("failed to read response body: %w", err) return Message{}, fmt.Errorf("HTTP request failed: %w", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return Message{}, fmt.Errorf("API returned error (%d): %s", resp.StatusCode, string(body)) body, _ := io.ReadAll(resp.Body)
} return Message{}, fmt.Errorf("API returned error (%d): %s", resp.StatusCode, string(body))
}
var openAIResp struct { if streamCallback != nil {
Choices []struct { return s.handleStream(resp.Body, streamCallback, reasoningCallback)
Message Message `json:"message"` }
} `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
if err := json.Unmarshal(body, &openAIResp); err != nil { body, err := io.ReadAll(resp.Body)
return Message{}, fmt.Errorf("failed to unmarshal response: %w", err) if err != nil {
} return Message{}, fmt.Errorf("failed to read response body: %w", err)
}
if openAIResp.Error != nil { var openAIResp struct {
return Message{}, fmt.Errorf("API error: %s", openAIResp.Error.Message) Choices []struct {
} Message Message `json:"message"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
if len(openAIResp.Choices) == 0 { if err := json.Unmarshal(body, &openAIResp); err != nil {
return Message{}, fmt.Errorf("API returned no choices") return Message{}, fmt.Errorf("failed to unmarshal response: %w", err)
} }
return openAIResp.Choices[0].Message, nil if openAIResp.Error != nil {
return Message{}, fmt.Errorf("API error: %s", openAIResp.Error.Message)
}
if len(openAIResp.Choices) == 0 {
return Message{}, fmt.Errorf("API returned no choices")
}
return openAIResp.Choices[0].Message, nil
} }
var defaultLLMClient LLMClient = &standardLLMClient{} var defaultLLMClient LLMClient = &standardLLMClient{}
// SetLLMClient allows overriding the LLM client (e.g. for testing) // SetLLMClient allows overriding the LLM client (e.g. for testing)
func SetLLMClient(client LLMClient) { func SetLLMClient(client LLMClient) {
defaultLLMClient = client defaultLLMClient = client
} }
// CallLLM invokes the configured LLM client // CallLLM invokes the configured LLM client
func CallLLM(ctx context.Context, model ModelConfig, msgs []Message, tools map[string]ToolDefinition) (Message, error) { func CallLLM(ctx context.Context, model ModelConfig, msgs []Message,
return defaultLLMClient.Call(ctx, model, msgs, tools) tools map[string]ToolDefinition, streamCallback, reasoningCallback func(string)) (Message, error) {
return defaultLLMClient.Call(ctx, model, msgs, tools, streamCallback, reasoningCallback)
} }
// mockLLMClient is used as a fallback if no actual HTTP client is injected. // mockLLMClient is used as a fallback if no actual HTTP client is injected.
type mockLLMClient struct { type mockLLMClient struct {
responses []Message responses []Message
calls int calls int
} }
func (m *mockLLMClient) Call(ctx context.Context, model ModelConfig, msgs []Message, ts map[string]ToolDefinition) (Message, error) { func (m *mockLLMClient) Call(ctx context.Context, model ModelConfig, msgs []Message,
ts map[string]ToolDefinition, streamCallback, reasoningCallback func(string)) (Message, error) {
if m.calls < len(m.responses) { if m.calls < len(m.responses) {
resp := m.responses[m.calls] resp := m.responses[m.calls]
m.calls++ m.calls++
if streamCallback != nil {
streamCallback(resp.Content)
}
return resp, nil return resp, nil
} }
return Message{ msg := Message{
Role: MessageRoleAssistant, Role: MessageRoleAssistant,
Content: `{"action": "RESPOND", "params": {"response": "Mock LLM Response"}}`, Content: `{"action": "RESPOND", "params": {"response": "Mock LLM Response"}}`,
}, nil }
if streamCallback != nil {
streamCallback(msg.Content)
}
return msg, nil
}
func (s *standardLLMClient) handleStream(body io.ReadCloser, streamCallback,
reasoningCallback func(string)) (Message, error) {
defer body.Close()
var fullContent strings.Builder
var finalMsg Message
finalMsg.Role = MessageRoleAssistant
type deltaToolCall struct {
Index int `json:"index"`
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
toolCallsMap := make(map[int]*ToolCall)
maxIndex := -1
scanner := bufio.NewScanner(body)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
break
}
var chunk struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
ToolCalls []deltaToolCall `json:"tool_calls"`
} `json:"delta"`
} `json:"choices"`
}
if err := json.Unmarshal([]byte(data), &chunk); err == nil && len(chunk.Choices) > 0 {
delta := chunk.Choices[0].Delta
if delta.Content != "" {
fullContent.WriteString(delta.Content)
streamCallback(delta.Content)
}
for _, tc := range delta.ToolCalls {
if _, exists := toolCallsMap[tc.Index]; !exists {
toolCallsMap[tc.Index] = &ToolCall{ID: tc.ID, Type: tc.Type}
if tc.Index > maxIndex {
maxIndex = tc.Index
}
}
toolCallsMap[tc.Index].Function.Name += tc.Function.Name
toolCallsMap[tc.Index].Function.Arguments += tc.Function.Arguments
}
}
}
if err := scanner.Err(); err != nil {
return Message{}, fmt.Errorf("error reading stream: %w", err)
}
finalMsg.Content = fullContent.String()
if maxIndex >= 0 {
for i := 0; i <= maxIndex; i++ {
if tc, ok := toolCallsMap[i]; ok {
finalMsg.ToolCalls = append(finalMsg.ToolCalls, *tc)
}
}
}
return finalMsg, nil
} }
+4 -2
View File
@@ -19,7 +19,8 @@ type mcpGoClientWrapper struct {
client *client.Client client *client.Client
} }
func (w *mcpGoClientWrapper) CallTool(ctx context.Context, name string, args map[string]interface{}) (string, error) { func (w *mcpGoClientWrapper) CallTool(ctx context.Context, name string,
args map[string]interface{}) (string, error) {
req := mcp.CallToolRequest{} req := mcp.CallToolRequest{}
req.Params.Name = name req.Params.Name = name
req.Params.Arguments = args req.Params.Arguments = args
@@ -130,7 +131,8 @@ func SetMCPClientFactory(factory MCPClientFactory) {
} }
// CallExternalTool creates a scoped MCP connection, calls the tool, and cleans up // CallExternalTool creates a scoped MCP connection, calls the tool, and cleans up
func CallExternalTool(ctx context.Context, toolset string, toolName string, args map[string]interface{}) (string, error) { func CallExternalTool(ctx context.Context, toolset string, toolName string,
args map[string]interface{}) (string, error) {
c, err := defaultMCPFactory(toolset) c, err := defaultMCPFactory(toolset)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to create MCP client for %s: %w", toolset, err) return "", fmt.Errorf("failed to create MCP client for %s: %w", toolset, err)
+121 -121
View File
@@ -1,152 +1,152 @@
package sidekick package sidekick
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"strings" "strings"
"testing" "testing"
"github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/mcp"
) )
func TestMCPServerQueryHandler(t *testing.T) { func TestMCPServerQueryHandler(t *testing.T) {
// 1. Setup Agent with mock LLM // 1. Setup Agent with mock LLM
ac := AgentConfig{ID: "test-agent", Role: RoleSpecialist, MaxIterations: 2} ac := AgentConfig{ID: "test-agent", Role: RoleSpecialist, MaxIterations: 2}
mCfg := ModelConfig{} mCfg := ModelConfig{}
agent := NewSidekick(ac, mCfg, nil) agent := NewSidekick(ac, mCfg, nil)
mockLLM := &mockLLMClient{ mockLLM := &mockLLMClient{
responses: []Message{ responses: []Message{
{Content: `{"action": "RESPOND", "params": {"response": "I am Sidekick. How can I help?"}}`}, {Content: `{"action": "RESPOND", "params": {"response": "I am Sidekick. How can I help?"}}`},
}, },
} }
SetLLMClient(mockLLM) SetLLMClient(mockLLM)
pool := map[string]*Sidekick{"test-agent": agent} pool := map[string]*Sidekick{"test-agent": agent}
// 2. Define the handler (mirrors logic in cmd/sidekick-mcp/main.go) // 2. Define the handler (mirrors logic in cmd/sidekick-mcp/main.go)
handler := func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { handler := func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
msg, err := request.RequireString("message") msg, err := request.RequireString("message")
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inCtx []Message var inCtx []Message
args := request.GetArguments() args := request.GetArguments()
if histInter, ok := args["history"]; ok { if histInter, ok := args["history"]; ok {
if histList, ok := histInter.([]interface{}); ok { if histList, ok := histInter.([]interface{}); ok {
for _, h := range histList { for _, h := range histList {
if hMap, ok := h.(map[string]interface{}); ok { if hMap, ok := h.(map[string]interface{}); ok {
role, _ := hMap["role"].(string) role, _ := hMap["role"].(string)
content, _ := hMap["content"].(string) content, _ := hMap["content"].(string)
if role != "" && content != "" { if role != "" && content != "" {
inCtx = append(inCtx, Message{ inCtx = append(inCtx, Message{
Role: MessageRole(role), Role: MessageRole(role),
Content: content, Content: content,
}) })
} }
} }
} }
} }
} }
inCtx = append(inCtx, Message{ inCtx = append(inCtx, Message{
Role: MessageRoleUser, Role: MessageRoleUser,
Content: msg, Content: msg,
}) })
responseStr, err := agent.Run(ctx, inCtx, pool) responseStr, err := agent.Run(ctx, inCtx, pool)
if err != nil { if err != nil {
return nil, err return nil, err
} }
outHistory := append(inCtx, Message{ outHistory := append(inCtx, Message{
Role: MessageRole("assistant"), Role: MessageRole("assistant"),
Content: responseStr, Content: responseStr,
}) })
resultObj := map[string]interface{}{ resultObj := map[string]interface{}{
"response": responseStr, "response": responseStr,
"history": outHistory, "history": outHistory,
} }
resultBytes, _ := json.Marshal(resultObj) resultBytes, _ := json.Marshal(resultObj)
return &mcp.CallToolResult{ return &mcp.CallToolResult{
Content: []mcp.Content{ Content: []mcp.Content{
mcp.NewTextContent(string(resultBytes)), mcp.NewTextContent(string(resultBytes)),
}, },
}, nil }, nil
} }
// 3. Test with simple message // 3. Test with simple message
req := mcp.CallToolRequest{} req := mcp.CallToolRequest{}
req.Params.Name = "query" req.Params.Name = "query"
req.Params.Arguments = map[string]interface{}{ req.Params.Arguments = map[string]interface{}{
"message": "Hello", "message": "Hello",
} }
res, err := handler(context.Background(), req) res, err := handler(context.Background(), req)
if err != nil { if err != nil {
t.Fatalf("handler failed: %v", err) t.Fatalf("handler failed: %v", err)
} }
if len(res.Content) != 1 { if len(res.Content) != 1 {
t.Fatalf("expected 1 content item, got %d", len(res.Content)) t.Fatalf("expected 1 content item, got %d", len(res.Content))
} }
textContent, ok := res.Content[0].(mcp.TextContent) textContent, ok := res.Content[0].(mcp.TextContent)
if !ok { if !ok {
t.Fatalf("expected TextContent") t.Fatalf("expected TextContent")
} }
var result map[string]interface{} var result map[string]interface{}
if err := json.Unmarshal([]byte(textContent.Text), &result); err != nil { if err := json.Unmarshal([]byte(textContent.Text), &result); err != nil {
t.Fatalf("failed to unmarshal result: %v", err) t.Fatalf("failed to unmarshal result: %v", err)
} }
if !strings.Contains(result["response"].(string), "How can I help?") { if !strings.Contains(result["response"].(string), "How can I help?") {
t.Errorf("unexpected response: %v", result["response"]) t.Errorf("unexpected response: %v", result["response"])
} }
history, ok := result["history"].([]interface{}) history, ok := result["history"].([]interface{})
if !ok || len(history) != 2 { if !ok || len(history) != 2 {
t.Errorf("expected history of length 2, got %v", len(history)) t.Errorf("expected history of length 2, got %v", len(history))
} }
// 4. Test with history // 4. Test with history
reqWithHist := mcp.CallToolRequest{} reqWithHist := mcp.CallToolRequest{}
reqWithHist.Params.Name = "query" reqWithHist.Params.Name = "query"
reqWithHist.Params.Arguments = map[string]interface{}{ reqWithHist.Params.Arguments = map[string]interface{}{
"message": "What did I say?", "message": "What did I say?",
"history": []interface{}{ "history": []interface{}{
map[string]interface{}{"role": "user", "content": "I like cats"}, map[string]interface{}{"role": "user", "content": "I like cats"},
map[string]interface{}{"role": "assistant", "content": "Me too"}, map[string]interface{}{"role": "assistant", "content": "Me too"},
}, },
} }
// Reset mock for next run // Reset mock for next run
mockLLM.calls = 0 mockLLM.calls = 0
mockLLM.responses = []Message{ mockLLM.responses = []Message{
{Content: `{"action": "RESPOND", "params": {"response": "You said you like cats."}}`}, {Content: `{"action": "RESPOND", "params": {"response": "You said you like cats."}}`},
} }
resHist, err := handler(context.Background(), reqWithHist) resHist, err := handler(context.Background(), reqWithHist)
if err != nil { if err != nil {
t.Fatalf("handler with history failed: %v", err) t.Fatalf("handler with history failed: %v", err)
} }
var resultHist map[string]interface{} var resultHist map[string]interface{}
textContentHist := resHist.Content[0].(mcp.TextContent) textContentHist := resHist.Content[0].(mcp.TextContent)
json.Unmarshal([]byte(textContentHist.Text), &resultHist) json.Unmarshal([]byte(textContentHist.Text), &resultHist)
historyFinal, ok := resultHist["history"].([]interface{}) historyFinal, ok := resultHist["history"].([]interface{})
if !ok || len(historyFinal) != 4 { // 2 old + 1 new user + 1 assistant response if !ok || len(historyFinal) != 4 { // 2 old + 1 new user + 1 assistant response
t.Errorf("expected history of length 4, got %v", len(historyFinal)) t.Errorf("expected history of length 4, got %v", len(historyFinal))
} }
lastMsg := historyFinal[3].(map[string]interface{}) lastMsg := historyFinal[3].(map[string]interface{})
if lastMsg["content"] != "You said you like cats." { if lastMsg["content"] != "You said you like cats." {
t.Errorf("unexpected last message: %v", lastMsg["content"]) t.Errorf("unexpected last message: %v", lastMsg["content"])
} }
} }
+48 -16
View File
@@ -25,14 +25,24 @@ func TestBufferGate(t *testing.T) {
var tfs []string var tfs []string
threshold := 10 threshold := 10
s := "small" s := "small"
if o, _ := BufferGate(s, &tfs, threshold); o != s { t.Errorf("expected small") } if o, _ := BufferGate(s, &tfs, threshold); o != s {
t.Errorf("expected small")
}
l := strings.Repeat("A", threshold+1) l := strings.Repeat("A", threshold+1)
o, err := BufferGate(l, &tfs, threshold) o, err := BufferGate(l, &tfs, threshold)
if err != nil { t.Fatalf("err: %v", err) } if err != nil {
if !strings.Contains(o, "file '") { t.Errorf("no file pointer") } t.Fatalf("err: %v", err)
if len(tfs) != 1 { t.Fatalf("no temp file") } }
if !strings.Contains(o, "file '") {
t.Errorf("no file pointer")
}
if len(tfs) != 1 {
t.Fatalf("no temp file")
}
c, _ := os.ReadFile(tfs[0]) c, _ := os.ReadFile(tfs[0])
if string(c) != l { t.Errorf("content mismatch") } if string(c) != l {
t.Errorf("content mismatch")
}
os.Remove(tfs[0]) os.Remove(tfs[0])
} }
@@ -40,15 +50,21 @@ func TestJSONActionParsing(t *testing.T) {
a := &Sidekick{} a := &Sidekick{}
m := Message{Content: `{"action": "RESPOND", "params": {"response": "Hi"}}`} m := Message{Content: `{"action": "RESPOND", "params": {"response": "Hi"}}`}
act, p, ok, err := a.parseAction(m) act, p, ok, err := a.parseAction(m)
if err != nil || !ok || act != ActionTypeRespond { t.Errorf("parse fail") } if err != nil || !ok || act != ActionTypeRespond {
if p["response"] != "Hi" { t.Errorf("param mismatch") } t.Errorf("parse fail")
}
if p["response"] != "Hi" {
t.Errorf("param mismatch")
}
} }
func TestDualResponseMode(t *testing.T) { func TestDualResponseMode(t *testing.T) {
a := &Sidekick{} a := &Sidekick{}
m := Message{ToolCalls: []ToolCall{{Function: FunctionCall{Name: "rf", Arguments: "{}"}}}} m := Message{ToolCalls: []ToolCall{{Function: FunctionCall{Name: "rf", Arguments: "{}"}}}}
act, _, ok, _ := a.parseAction(m) act, _, ok, _ := a.parseAction(m)
if !ok || act != ActionTypeToolCall { t.Errorf("dual fail") } if !ok || act != ActionTypeToolCall {
t.Errorf("dual fail")
}
} }
func TestSingleAgentLoop(t *testing.T) { func TestSingleAgentLoop(t *testing.T) {
@@ -60,7 +76,9 @@ func TestSingleAgentLoop(t *testing.T) {
}} }}
SetLLMClient(mc) SetLLMClient(mc)
ans, _ := a.Run(context.Background(), nil, nil) ans, _ := a.Run(context.Background(), nil, nil)
if ans != "Done" { t.Errorf("expected Done, got %s", ans) } if ans != "Done" {
t.Errorf("expected Done, got %s", ans)
}
} }
func TestDelegation(t *testing.T) { func TestDelegation(t *testing.T) {
@@ -76,7 +94,9 @@ func TestDelegation(t *testing.T) {
SetLLMClient(mc) SetLLMClient(mc)
p := map[string]*Sidekick{"s1": sa} p := map[string]*Sidekick{"s1": sa}
ans, _ := coord.Run(context.Background(), nil, p) ans, _ := coord.Run(context.Background(), nil, p)
if ans != "Coord done" { t.Errorf("expected Coord done, got %s", ans) } if ans != "Coord done" {
t.Errorf("expected Coord done, got %s", ans)
}
} }
func TestShellExecRegistration(t *testing.T) { func TestShellExecRegistration(t *testing.T) {
@@ -99,9 +119,11 @@ func TestTempFileCleanup(t *testing.T) {
threshold := 10 threshold := 10
a := NewSidekick(AgentConfig{MaxIterations: 2, ToolResponseThreshold: threshold}, ModelConfig{}, nil) a := NewSidekick(AgentConfig{MaxIterations: 2, ToolResponseThreshold: threshold}, ModelConfig{}, nil)
h := strings.Repeat("B", threshold+1) h := strings.Repeat("B", threshold+1)
a.ToolRegistry["huge"] = ToolDefinition{Name: "huge", Internal: true, Handler: func(m map[string]interface{}) (string, error) { a.ToolRegistry["huge"] = ToolDefinition{
return h, nil Name: "huge", Internal: true,
}} Handler: func(m map[string]interface{}) (string, error) {
return h, nil
}}
a.ToolMapping["huge"] = "huge" a.ToolMapping["huge"] = "huge"
mc := &mockLLMClient{responses: []Message{ mc := &mockLLMClient{responses: []Message{
{Content: `{"action": "TOOL_CALL", "params": {"tool_name": "huge", "arguments": {}}}`}, {Content: `{"action": "TOOL_CALL", "params": {"tool_name": "huge", "arguments": {}}}`},
@@ -112,7 +134,17 @@ func TestTempFileCleanup(t *testing.T) {
a.Run(context.Background(), nil, nil) a.Run(context.Background(), nil, nil)
afs, _ := os.ReadDir(os.TempDir()) afs, _ := os.ReadDir(os.TempDir())
bc, ac := 0, 0 bc, ac := 0, 0
for _, f := range bfs { if strings.HasPrefix(f.Name(), "sidekick-buf-") { bc++ } } for _, f := range bfs {
for _, f := range afs { if strings.HasPrefix(f.Name(), "sidekick-buf-") { ac++ } } if strings.HasPrefix(f.Name(), "sidekick-buf-") {
if ac > bc { t.Errorf("leak: before %d, after %d", bc, ac) } bc++
}
}
for _, f := range afs {
if strings.HasPrefix(f.Name(), "sidekick-buf-") {
ac++
}
}
if ac > bc {
t.Errorf("leak: before %d, after %d", bc, ac)
}
} }
+3 -2
View File
@@ -21,8 +21,9 @@ const (
// Message is a chat message in the context // Message is a chat message in the context
type Message struct { type Message struct {
Role MessageRole `json:"role"` Role MessageRole `json:"role"`
Content string `json:"content"` Content string `json:"content"`
Reasoning string `json:"reasoning_content,omitempty"`
// For native tool calls from LLM // For native tool calls from LLM
ToolCalls []ToolCall `json:"tool_calls,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// For tool responses // For tool responses