improved tool debugging
This commit is contained in:
@@ -9,10 +9,14 @@ MCP_SRC = ./cmd/sidekick-mcp
|
||||
TUI_BIN = $(BIN_DIR)/sidekick-tui
|
||||
MCP_BIN = $(BIN_DIR)/sidekick-mcp
|
||||
|
||||
.PHONY: all build test clean install uninstall
|
||||
.PHONY: all build test clean install uninstall fmt
|
||||
|
||||
all: build
|
||||
|
||||
fmt:
|
||||
@./fmt.sh
|
||||
@echo "Formatting complete according to AGENTS.md guidelines (gofmt + 2-space expansion + 112-char check)."
|
||||
|
||||
build:
|
||||
@mkdir -p $(BIN_DIR)
|
||||
go build -o $(TUI_BIN) $(TUI_SRC)
|
||||
|
||||
@@ -116,6 +116,8 @@ headers = { "X-Custom-Header" = "value" } # Optional custom headers
|
||||
role = "COORDINATOR"
|
||||
model_id = "default"
|
||||
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"]
|
||||
toolsets = ["filesystem"]
|
||||
```
|
||||
|
||||
@@ -13,6 +13,9 @@ type Sidekick struct {
|
||||
Model ModelConfig
|
||||
ToolMapping map[string]string
|
||||
ToolRegistry map[string]ToolDefinition
|
||||
StreamHandler func(string)
|
||||
ReasoningHandler func(string)
|
||||
IntermediateHandler func(string)
|
||||
}
|
||||
|
||||
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("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()
|
||||
}
|
||||
|
||||
@@ -84,42 +88,101 @@ func (s *Sidekick) Run(ctx context.Context, inCtx []Message, pool map[string]*Si
|
||||
}
|
||||
var tFiles []string
|
||||
defer func() {
|
||||
for _, f := range tFiles { os.Remove(f) }
|
||||
for _, f := range tFiles {
|
||||
os.Remove(f)
|
||||
}
|
||||
}()
|
||||
iters := s.Config.MaxIterations
|
||||
if iters <= 0 { iters = 10 }
|
||||
if iters <= 0 {
|
||||
iters = 10
|
||||
}
|
||||
for i := 0; i < iters; i++ {
|
||||
msg, err := CallLLM(ctx, s.Model, history, s.ToolRegistry)
|
||||
if err != nil { return "", fmt.Errorf("LLM call failed: %w", err) }
|
||||
history = append(history, msg)
|
||||
act, params, parsed, err := s.parseAction(msg)
|
||||
done, res, err := s.runStep(ctx, &history, pool, &tFiles)
|
||||
if err != nil {
|
||||
if !parsed {
|
||||
act = ActionTypeRespond
|
||||
params = map[string]interface{}{"response": msg.Content}
|
||||
} else {
|
||||
history = append(history, Message{Role: MessageRoleUser, Content: fmt.Sprintf("Error: %v", err)})
|
||||
continue
|
||||
return "", err
|
||||
}
|
||||
if done {
|
||||
return res, nil
|
||||
}
|
||||
if act == ActionTypeRespond {
|
||||
if r, ok := params["response"].(string); ok { return r, 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
|
||||
}
|
||||
|
||||
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 {
|
||||
tName, _ := p["tool_name"].(string)
|
||||
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)
|
||||
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)
|
||||
}
|
||||
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) {
|
||||
if len(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
|
||||
c := strings.TrimSpace(msg.Content)
|
||||
if strings.HasPrefix(c, "```json") && strings.HasSuffix(c, "```") {
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Sidekick) executeTool(ctx context.Context, sName string, args map[string]interface{}) (string, error) {
|
||||
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]
|
||||
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.Handler == nil { return "", fmt.Errorf("missing handler") }
|
||||
if tDef.Handler == nil {
|
||||
return "", fmt.Errorf("missing handler")
|
||||
}
|
||||
return tDef.Handler(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) {
|
||||
if !contains(s.Config.Subagents, sid) { return "", fmt.Errorf("subagent %s not allowed", sid) }
|
||||
func (s *Sidekick) executeDelegation(ctx context.Context, sid string,
|
||||
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]
|
||||
if !ok { return "", fmt.Errorf("subagent %s not found", sid) }
|
||||
return sub.Run(ctx, []Message{{Role: MessageRoleUser, Content: task}}, pool)
|
||||
if !ok {
|
||||
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 {
|
||||
for _, i := range s { if i == v { return true } }
|
||||
for _, i := range s {
|
||||
if i == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
+143
-8
@@ -39,6 +39,7 @@ var (
|
||||
userStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6")).Bold(true)
|
||||
agentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true)
|
||||
sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Italic(true)
|
||||
bannerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFF00")).Bold(true)
|
||||
|
||||
banner = ` _ __ __ _ __
|
||||
___ (_) ___/ / ___ / /__ (_) ____ / /__
|
||||
@@ -53,6 +54,9 @@ type model struct {
|
||||
agent *sidekick.Sidekick
|
||||
pool map[string]*sidekick.Sidekick
|
||||
history []sidekick.Message
|
||||
transientSteps []string
|
||||
currentStream string
|
||||
currentReasoning string
|
||||
isThinking bool
|
||||
err error
|
||||
ready bool
|
||||
@@ -66,6 +70,32 @@ type agentResponseMsg struct {
|
||||
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 {
|
||||
ta := textarea.New()
|
||||
ta.Placeholder = "Type a message..."
|
||||
@@ -108,15 +138,41 @@ func initialModel() model {
|
||||
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{
|
||||
textarea: ta,
|
||||
agent: agent,
|
||||
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 {
|
||||
label := ""
|
||||
@@ -132,13 +188,38 @@ func (m model) renderMessage(msg sidekick.Message) string {
|
||||
}
|
||||
|
||||
content := msg.Content
|
||||
if m.renderer != nil && msg.Role != sidekick.MessageRoleSystem {
|
||||
rendered, err := m.renderer.Render(content)
|
||||
if err == nil {
|
||||
content = strings.TrimSpace(rendered)
|
||||
reasoning := msg.Reasoning
|
||||
|
||||
// If this is the banner message, handle it specially to preserve ASCII art
|
||||
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
|
||||
}
|
||||
|
||||
@@ -147,6 +228,32 @@ func (m *model) updateViewportContent() {
|
||||
for _, msg := range m.history {
|
||||
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"))
|
||||
}
|
||||
|
||||
@@ -212,7 +319,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
Role: sidekick.MessageRoleUser,
|
||||
Content: v,
|
||||
})
|
||||
|
||||
m.currentStream = ""
|
||||
m.currentReasoning = ""
|
||||
m.transientSteps = nil
|
||||
m.updateViewportContent()
|
||||
m.textarea.Reset()
|
||||
m.viewport.GotoBottom()
|
||||
@@ -220,8 +329,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
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:
|
||||
m.isThinking = false
|
||||
m.currentStream = ""
|
||||
m.currentReasoning = ""
|
||||
m.transientSteps = nil
|
||||
if msg.err != nil {
|
||||
m.history = append(m.history, sidekick.Message{
|
||||
Role: sidekick.MessageRoleSystem,
|
||||
@@ -280,7 +416,6 @@ func (m model) View() string {
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
if _, err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -57,6 +57,8 @@ type AgentConfig struct {
|
||||
Toolsets []string `toml:"toolsets"` // Allowed tool namespaces (MCP servers)
|
||||
Subagents []string `toml:"subagents"` // Allowed subagent IDs
|
||||
EnableShellExec bool `toml:"enable_shell_exec"`
|
||||
Streaming bool `toml:"streaming"`
|
||||
LogIntermediate bool `toml:"log_intermediate"`
|
||||
ToolResponseThreshold int `toml:"-"`
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ port = 8080
|
||||
role = "COORDINATOR"
|
||||
model_id = "default"
|
||||
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."
|
||||
description = "Lead Architect and project coordinator"
|
||||
max_iterations = 15
|
||||
@@ -51,6 +53,8 @@ port = 8080
|
||||
role = "SPECIALIST"
|
||||
model_id = "default"
|
||||
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."
|
||||
description = "Senior Developer focused on implementation and refactoring"
|
||||
max_iterations = 10
|
||||
@@ -64,6 +68,8 @@ port = 8080
|
||||
[agents.reviewer]
|
||||
role = "SPECIALIST"
|
||||
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."
|
||||
description = "Code Reviewer and Quality Assurance specialist"
|
||||
max_iterations = 8
|
||||
@@ -78,6 +84,8 @@ port = 8080
|
||||
role = "SPECIALIST"
|
||||
model_id = "fast"
|
||||
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."
|
||||
description = "Test Engineer focused on automated testing and verification"
|
||||
max_iterations = 10
|
||||
|
||||
+10
-4
@@ -13,12 +13,14 @@ func DefaultInternalTools() map[string]ToolDefinition {
|
||||
return map[string]ToolDefinition{
|
||||
"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{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"path": map[string]interface{}{"type": "string"},
|
||||
"offset": map[string]interface{}{"type": "integer", "description": "Starting line number (0-indexed)"},
|
||||
"offset": map[string]interface{}{"type": "integer",
|
||||
"description": "Starting line number (0-indexed)"},
|
||||
"limit": map[string]interface{}{"type": "integer", "description": "Number of lines to read"},
|
||||
},
|
||||
"required": []string{"path"},
|
||||
@@ -42,7 +44,8 @@ func DefaultInternalTools() map[string]ToolDefinition {
|
||||
},
|
||||
"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{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
@@ -57,7 +60,10 @@ func DefaultInternalTools() map[string]ToolDefinition {
|
||||
},
|
||||
"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{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
|
||||
+23
-8
@@ -9,18 +9,32 @@ import (
|
||||
func TestShellExecHandler(t *testing.T) {
|
||||
// Success
|
||||
got, err := shellExecHandler(map[string]interface{}{"command": "echo hello"})
|
||||
if err != nil { t.Fatal(err) }
|
||||
if strings.TrimSpace(got) != "hello" { t.Errorf("expected hello, got %s", got) }
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.TrimSpace(got) != "hello" {
|
||||
t.Errorf("expected hello, got %s", got)
|
||||
}
|
||||
|
||||
// Failure
|
||||
got, err = shellExecHandler(map[string]interface{}{"command": "ls /nonexistent-file-path-that-should-not-exist"})
|
||||
if err != nil { t.Fatal(err) }
|
||||
if !strings.Contains(got, "Error:") { t.Errorf("expected error in output, got %s", got) }
|
||||
got, err = shellExecHandler(map[string]interface{}{
|
||||
"command": "ls /nonexistent-file-path-that-should-not-exist",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(got, "Error:") {
|
||||
t.Errorf("expected error in output, got %s", got)
|
||||
}
|
||||
|
||||
// Empty
|
||||
got, err = shellExecHandler(map[string]interface{}{"command": "true"})
|
||||
if err != nil { t.Fatal(err) }
|
||||
if got != "(empty output)" { t.Errorf("expected empty output, got %s", got) }
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "(empty output)" {
|
||||
t.Errorf("expected empty output, got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashLine(t *testing.T) {
|
||||
@@ -82,7 +96,8 @@ func TestEditFileHandler_ExactHashlines(t *testing.T) {
|
||||
got, _ := os.ReadFile(tmpFile)
|
||||
expected := "line one\nreplaced two\nreplaced three\nline four"
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LLMClient represents an abstraction over the LLM provider
|
||||
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
|
||||
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,
|
||||
ts map[string]ToolDefinition, streamCallback, reasoningCallback func(string)) (Message, error) {
|
||||
client := &http.Client{
|
||||
Timeout: model.Timeout,
|
||||
}
|
||||
@@ -39,12 +43,17 @@ func (s *standardLLMClient) Call(ctx context.Context, model ModelConfig, msgs []
|
||||
reqBody["tools"] = tools
|
||||
}
|
||||
|
||||
if streamCallback != nil {
|
||||
reqBody["stream"] = true
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", model.Endpoint+"/chat/completions", bytes.NewBuffer(jsonBody))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST",
|
||||
model.Endpoint+"/chat/completions", bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -58,15 +67,20 @@ func (s *standardLLMClient) Call(ctx context.Context, model ModelConfig, msgs []
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return Message{}, fmt.Errorf("API returned error (%d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
if streamCallback != nil {
|
||||
return s.handleStream(resp.Body, streamCallback, reasoningCallback)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return Message{}, fmt.Errorf("API returned error (%d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var openAIResp struct {
|
||||
Choices []struct {
|
||||
Message Message `json:"message"`
|
||||
@@ -99,8 +113,9 @@ func SetLLMClient(client LLMClient) {
|
||||
}
|
||||
|
||||
// CallLLM invokes the configured LLM client
|
||||
func CallLLM(ctx context.Context, model ModelConfig, msgs []Message, tools map[string]ToolDefinition) (Message, error) {
|
||||
return defaultLLMClient.Call(ctx, model, msgs, tools)
|
||||
func CallLLM(ctx context.Context, model ModelConfig, msgs []Message,
|
||||
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.
|
||||
@@ -109,14 +124,94 @@ type mockLLMClient struct {
|
||||
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) {
|
||||
resp := m.responses[m.calls]
|
||||
m.calls++
|
||||
if streamCallback != nil {
|
||||
streamCallback(resp.Content)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
return Message{
|
||||
msg := Message{
|
||||
Role: MessageRoleAssistant,
|
||||
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
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ type mcpGoClientWrapper struct {
|
||||
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.Params.Name = name
|
||||
req.Params.Arguments = args
|
||||
@@ -130,7 +131,8 @@ func SetMCPClientFactory(factory MCPClientFactory) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create MCP client for %s: %w", toolset, err)
|
||||
|
||||
+46
-14
@@ -25,14 +25,24 @@ func TestBufferGate(t *testing.T) {
|
||||
var tfs []string
|
||||
threshold := 10
|
||||
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)
|
||||
o, err := BufferGate(l, &tfs, threshold)
|
||||
if err != nil { t.Fatalf("err: %v", err) }
|
||||
if !strings.Contains(o, "file '") { t.Errorf("no file pointer") }
|
||||
if len(tfs) != 1 { t.Fatalf("no temp file") }
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if !strings.Contains(o, "file '") {
|
||||
t.Errorf("no file pointer")
|
||||
}
|
||||
if len(tfs) != 1 {
|
||||
t.Fatalf("no temp file")
|
||||
}
|
||||
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])
|
||||
}
|
||||
|
||||
@@ -40,15 +50,21 @@ func TestJSONActionParsing(t *testing.T) {
|
||||
a := &Sidekick{}
|
||||
m := Message{Content: `{"action": "RESPOND", "params": {"response": "Hi"}}`}
|
||||
act, p, ok, err := a.parseAction(m)
|
||||
if err != nil || !ok || act != ActionTypeRespond { t.Errorf("parse fail") }
|
||||
if p["response"] != "Hi" { t.Errorf("param mismatch") }
|
||||
if err != nil || !ok || act != ActionTypeRespond {
|
||||
t.Errorf("parse fail")
|
||||
}
|
||||
if p["response"] != "Hi" {
|
||||
t.Errorf("param mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDualResponseMode(t *testing.T) {
|
||||
a := &Sidekick{}
|
||||
m := Message{ToolCalls: []ToolCall{{Function: FunctionCall{Name: "rf", Arguments: "{}"}}}}
|
||||
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) {
|
||||
@@ -60,7 +76,9 @@ func TestSingleAgentLoop(t *testing.T) {
|
||||
}}
|
||||
SetLLMClient(mc)
|
||||
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) {
|
||||
@@ -76,7 +94,9 @@ func TestDelegation(t *testing.T) {
|
||||
SetLLMClient(mc)
|
||||
p := map[string]*Sidekick{"s1": sa}
|
||||
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) {
|
||||
@@ -99,7 +119,9 @@ func TestTempFileCleanup(t *testing.T) {
|
||||
threshold := 10
|
||||
a := NewSidekick(AgentConfig{MaxIterations: 2, ToolResponseThreshold: threshold}, ModelConfig{}, nil)
|
||||
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{
|
||||
Name: "huge", Internal: true,
|
||||
Handler: func(m map[string]interface{}) (string, error) {
|
||||
return h, nil
|
||||
}}
|
||||
a.ToolMapping["huge"] = "huge"
|
||||
@@ -112,7 +134,17 @@ func TestTempFileCleanup(t *testing.T) {
|
||||
a.Run(context.Background(), nil, nil)
|
||||
afs, _ := os.ReadDir(os.TempDir())
|
||||
bc, ac := 0, 0
|
||||
for _, f := range bfs { if strings.HasPrefix(f.Name(), "sidekick-buf-") { 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) }
|
||||
for _, f := range bfs {
|
||||
if strings.HasPrefix(f.Name(), "sidekick-buf-") {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user