refactored the dir structure
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Sidekick struct {
|
||||
Config AgentConfig
|
||||
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 {
|
||||
model.Resolve()
|
||||
s := &Sidekick{
|
||||
Config: config,
|
||||
Model: model,
|
||||
ToolMapping: make(map[string]string),
|
||||
ToolRegistry: make(map[string]ToolDefinition),
|
||||
}
|
||||
for name, tool := range DefaultInternalTools() {
|
||||
if name == "shell_exec" && !s.Config.EnableShellExec {
|
||||
continue
|
||||
}
|
||||
s.registerTool(name, tool)
|
||||
}
|
||||
for _, tool := range extTools {
|
||||
if len(s.Config.Toolsets) > 0 && !contains(s.Config.Toolsets, tool.Toolset) {
|
||||
continue
|
||||
}
|
||||
s.registerTool(tool.Name, tool)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Sidekick) registerTool(realName string, tool ToolDefinition) {
|
||||
sanitized := SanitizeToolName(realName)
|
||||
s.ToolMapping[sanitized] = realName
|
||||
s.ToolRegistry[realName] = tool
|
||||
}
|
||||
|
||||
func (s *Sidekick) constructSystemMessage(agentPool map[string]*Sidekick) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString(s.Config.SystemPrompt + "\n\n")
|
||||
sb.WriteString(fmt.Sprintf("Role: %s\n\n", s.Config.Role))
|
||||
if len(s.Config.Goals) > 0 {
|
||||
sb.WriteString("Goals:\n")
|
||||
for _, g := range s.Config.Goals {
|
||||
sb.WriteString(fmt.Sprintf("- %s\n", g))
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
if len(s.ToolRegistry) > 0 {
|
||||
sb.WriteString("Available Tools:\n")
|
||||
for _, t := range s.ToolRegistry {
|
||||
sb.WriteString(fmt.Sprintf("- %s: %s\n", SanitizeToolName(t.Name), t.Description))
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
if len(s.Config.Subagents) > 0 {
|
||||
sb.WriteString("Available Subagents:\n")
|
||||
for _, sid := range s.Config.Subagents {
|
||||
if sub, exists := agentPool[sid]; exists {
|
||||
sb.WriteString(fmt.Sprintf("- %s: %s\n", sid, sub.Config.Description))
|
||||
}
|
||||
}
|
||||
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")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (s *Sidekick) Run(ctx context.Context, inCtx []Message, pool map[string]*Sidekick) (string, error) {
|
||||
history := make([]Message, len(inCtx))
|
||||
copy(history, inCtx)
|
||||
if len(history) == 0 || history[0].Role != MessageRoleSystem {
|
||||
history = append([]Message{{Role: MessageRoleSystem, Content: s.constructSystemMessage(pool)}}, history...)
|
||||
}
|
||||
var tFiles []string
|
||||
defer func() {
|
||||
for _, f := range tFiles {
|
||||
os.Remove(f)
|
||||
}
|
||||
}()
|
||||
iters := s.Config.MaxIterations
|
||||
if iters <= 0 {
|
||||
iters = 10
|
||||
}
|
||||
for i := 0; i < iters; i++ {
|
||||
done, res, err := s.runStep(ctx, &history, pool, &tFiles)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if done {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
return "Max iterations reached.", nil
|
||||
}
|
||||
|
||||
func (s *Sidekick) logIntermediate(msg Message) {
|
||||
if !s.Config.LogIntermediate || s.IntermediateHandler == nil {
|
||||
return
|
||||
}
|
||||
if msg.Reasoning != "" {
|
||||
s.IntermediateHandler(fmt.Sprintf("Reasoning: %s", msg.Reasoning))
|
||||
}
|
||||
if msg.Content != "" {
|
||||
s.IntermediateHandler(fmt.Sprintf("LLM Output: %s", msg.Content))
|
||||
}
|
||||
}
|
||||
|
||||
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 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 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 {
|
||||
sid, _ := p["subagent_id"].(string)
|
||||
task, _ := p["task"].(string)
|
||||
return s.executeDelegation(ctx, sid, task, pl)
|
||||
}
|
||||
return "", fmt.Errorf("unknown action: %s", a)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
tDef, ok := s.ToolRegistry[rName]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("tool %s not registered", rName)
|
||||
}
|
||||
if tDef.Internal {
|
||||
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)
|
||||
}
|
||||
sub, ok := pool[sid]
|
||||
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
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// BufferGate checks if a result is too large. If it is, it writes it to a temp file
|
||||
// and returns a pointer message instead. The path is added to tempFiles for later cleanup.
|
||||
func BufferGate(result string, tempFiles *[]string, threshold int) (string, error) {
|
||||
if len(result) <= threshold {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "sidekick-buf-*.txt")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temp file for buffering: %w", err)
|
||||
}
|
||||
|
||||
_, err = tmpFile.WriteString(result)
|
||||
if err != nil {
|
||||
tmpFile.Close()
|
||||
return "", fmt.Errorf("failed to write to temp file: %w", err)
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
*tempFiles = append(*tempFiles, tmpFile.Name())
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Tool result is available in file '%s'. File size: %d bytes. Use read_file or grep_file to access it.",
|
||||
tmpFile.Name(), len(result),
|
||||
)
|
||||
return msg, nil
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// ProjectConfig represents the root of the TOML configuration
|
||||
type ProjectConfig struct {
|
||||
ToolResponseThreshold int `toml:"tool_response_threshold"`
|
||||
MCPListener MCPListenerConfig `toml:"mcp_listener"`
|
||||
Models map[string]ModelConfig `toml:"models"`
|
||||
MCPServers map[string]MCPServerConfig `toml:"mcp_servers"`
|
||||
Agents map[string]AgentConfig `toml:"agents"`
|
||||
}
|
||||
|
||||
// MCPListenerConfig configures the MCP server exposed by Sidekick
|
||||
type MCPListenerConfig struct {
|
||||
Transport string `toml:"transport"` // "stdio" or "http"
|
||||
Port int `toml:"port"` // used if transport is "http"
|
||||
}
|
||||
|
||||
// ModelConfig holds configuration for the LLM
|
||||
type ModelConfig struct {
|
||||
ModelID string `toml:"model_id"`
|
||||
Endpoint string `toml:"endpoint"`
|
||||
APIKey string `toml:"api_key"`
|
||||
Temperature float32 `toml:"temperature"`
|
||||
MaxTokens int `toml:"max_tokens"`
|
||||
TimeoutSecs int `toml:"timeout_secs"`
|
||||
Timeout time.Duration `toml:"-"`
|
||||
}
|
||||
|
||||
// MCPServerConfig configures a single MCP server connection
|
||||
type MCPServerConfig struct {
|
||||
Transport string `toml:"transport"` // "stdio", "sse", "http", "websocket"
|
||||
Command string `toml:"command"` // for stdio
|
||||
Args []string `toml:"args"` // for stdio
|
||||
Env []string `toml:"env"` // for stdio, format "KEY=VALUE"
|
||||
URL string `toml:"url"` // for HTTP/SSE/WebSocket
|
||||
AuthToken string `toml:"auth_token"` // for HTTP Bearer authentication
|
||||
Headers map[string]string `toml:"headers"` // for custom headers
|
||||
}
|
||||
|
||||
// AgentConfig holds configuration for a specific agent instance
|
||||
type AgentConfig struct {
|
||||
ID string `toml:"-"`
|
||||
Role Role `toml:"role"`
|
||||
ModelID string `toml:"model_id"`
|
||||
Goals []string `toml:"goals"`
|
||||
SystemPrompt string `toml:"system_prompt"`
|
||||
Description string `toml:"description"`
|
||||
MaxIterations int `toml:"max_iterations"`
|
||||
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:"-"`
|
||||
}
|
||||
|
||||
// resolveEnv checks if a string starts with "env:" and resolves it
|
||||
func resolveEnv(val string) string {
|
||||
if strings.HasPrefix(val, "env:") {
|
||||
envVar := strings.TrimPrefix(val, "env:")
|
||||
return os.Getenv(envVar)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (s *MCPServerConfig) Resolve() {
|
||||
s.Command = resolveEnv(s.Command)
|
||||
for i, arg := range s.Args {
|
||||
s.Args[i] = resolveEnv(arg)
|
||||
}
|
||||
for i, e := range s.Env {
|
||||
parts := strings.SplitN(e, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
s.Env[i] = parts[0] + "=" + resolveEnv(parts[1])
|
||||
} else {
|
||||
s.Env[i] = resolveEnv(e)
|
||||
}
|
||||
}
|
||||
s.URL = resolveEnv(s.URL)
|
||||
s.AuthToken = resolveEnv(s.AuthToken)
|
||||
for k, v := range s.Headers {
|
||||
s.Headers[k] = resolveEnv(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve applies environment variable resolution to relevant fields
|
||||
func (m *ModelConfig) Resolve() {
|
||||
m.ModelID = resolveEnv(m.ModelID)
|
||||
m.Endpoint = resolveEnv(m.Endpoint)
|
||||
m.APIKey = resolveEnv(m.APIKey)
|
||||
if m.TimeoutSecs > 0 {
|
||||
m.Timeout = time.Duration(m.TimeoutSecs) * time.Second
|
||||
}
|
||||
if m.Timeout == 0 {
|
||||
m.Timeout = 120 * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
// LoadConfig parses a TOML configuration file into ProjectConfig
|
||||
func LoadConfig(path string) (*ProjectConfig, error) {
|
||||
var cfg ProjectConfig
|
||||
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.ToolResponseThreshold <= 0 {
|
||||
cfg.ToolResponseThreshold = 10000
|
||||
}
|
||||
for k, m := range cfg.Models {
|
||||
m.Resolve()
|
||||
cfg.Models[k] = m
|
||||
}
|
||||
for k, s := range cfg.MCPServers {
|
||||
s.Resolve()
|
||||
cfg.MCPServers[k] = s
|
||||
}
|
||||
for k, a := range cfg.Agents {
|
||||
a.ID = k
|
||||
a.ToolResponseThreshold = cfg.ToolResponseThreshold
|
||||
cfg.Agents[k] = a
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DefaultInternalTools returns a map of standard file I/O tools
|
||||
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').",
|
||||
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)"},
|
||||
"limit": map[string]interface{}{"type": "integer", "description": "Number of lines to read"},
|
||||
},
|
||||
"required": []string{"path"},
|
||||
},
|
||||
Internal: true,
|
||||
Handler: readFileHandler,
|
||||
},
|
||||
"write_file": {
|
||||
Name: "write_file",
|
||||
Description: "Overwrite a file with new content. Automatically strips 'line:hash:' prefixes if present.",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"path": map[string]interface{}{"type": "string"},
|
||||
"content": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"path", "content"},
|
||||
},
|
||||
Internal: true,
|
||||
Handler: writeFileHandler,
|
||||
},
|
||||
"grep_file": {
|
||||
Name: "grep_file",
|
||||
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{}{
|
||||
"path": map[string]interface{}{"type": "string"},
|
||||
"pattern": map[string]interface{}{"type": "string"},
|
||||
"context_lines": map[string]interface{}{"type": "integer"},
|
||||
},
|
||||
"required": []string{"path", "pattern"},
|
||||
},
|
||||
Internal: true,
|
||||
Handler: grepFileHandler,
|
||||
},
|
||||
"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.",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"path": map[string]interface{}{"type": "string"},
|
||||
"old_string": map[string]interface{}{"type": "string"},
|
||||
"new_string": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"path", "old_string", "new_string"},
|
||||
},
|
||||
Internal: true,
|
||||
Handler: editFileHandler,
|
||||
},
|
||||
"shell_exec": {
|
||||
Name: "shell_exec",
|
||||
Description: "Run a POSIX shell command and return its combined stdout and stderr.",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"command": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"command"},
|
||||
},
|
||||
Internal: true,
|
||||
Handler: shellExecHandler,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func shellExecHandler(args map[string]interface{}) (string, error) {
|
||||
cmdStr, ok := args["command"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("command is required")
|
||||
}
|
||||
|
||||
cmd := exec.Command("sh", "-c", cmdStr)
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
result := string(output)
|
||||
if err != nil {
|
||||
result = fmt.Sprintf("%s\nError: %v", result, err)
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
return "(empty output)", nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func readFileHandler(args map[string]interface{}) (string, error) {
|
||||
path, ok := args["path"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("path is required")
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
lines := strings.Split(string(content), "\n")
|
||||
|
||||
offset := 0
|
||||
if off, ok := args["offset"].(float64); ok {
|
||||
offset = int(off)
|
||||
}
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
limit := len(lines)
|
||||
if lim, ok := args["limit"].(float64); ok {
|
||||
limit = int(lim)
|
||||
}
|
||||
|
||||
end := offset + limit
|
||||
if end > len(lines) {
|
||||
end = len(lines)
|
||||
}
|
||||
if offset >= len(lines) {
|
||||
return "", nil // Empty reading window
|
||||
}
|
||||
|
||||
var outputLines []string
|
||||
for i, line := range lines[offset:end] {
|
||||
lineNum := offset + i + 1
|
||||
outputLines = append(outputLines, fmt.Sprintf("%d:%s:%s", lineNum, hashLine(line), line))
|
||||
}
|
||||
|
||||
return strings.Join(outputLines, "\n"), nil
|
||||
}
|
||||
|
||||
func writeFileHandler(args map[string]interface{}) (string, error) {
|
||||
path, ok := args["path"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("path is required")
|
||||
}
|
||||
content, ok := args["content"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("content is required")
|
||||
}
|
||||
|
||||
content = stripHashlines(content)
|
||||
|
||||
err := os.WriteFile(path, []byte(content), 0644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("Successfully wrote to %s", path), nil
|
||||
}
|
||||
|
||||
func grepFileHandler(args map[string]interface{}) (string, error) {
|
||||
path, ok := args["path"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("path is required")
|
||||
}
|
||||
pattern, ok := args["pattern"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("pattern is required")
|
||||
}
|
||||
|
||||
contextLines := 0
|
||||
if cl, ok := args["context_lines"].(float64); ok {
|
||||
contextLines = int(cl)
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
lines := strings.Split(string(content), "\n")
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid regex: %w", err)
|
||||
}
|
||||
|
||||
var results []string
|
||||
for i, line := range lines {
|
||||
if re.MatchString(line) {
|
||||
start := i - contextLines
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
end := i + contextLines + 1
|
||||
if end > len(lines) {
|
||||
end = len(lines)
|
||||
}
|
||||
|
||||
results = append(results, fmt.Sprintf("--- Match around line %d ---", i+1))
|
||||
for j := start; j < end; j++ {
|
||||
results = append(results, fmt.Sprintf("%d:%s:%s", j+1, hashLine(lines[j]), lines[j]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return "No matches found.", nil
|
||||
}
|
||||
return strings.Join(results, "\n"), nil
|
||||
}
|
||||
func editFileHandler(args map[string]interface{}) (string, error) {
|
||||
path, ok := args["path"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("path is required")
|
||||
}
|
||||
oldStr, ok := args["old_string"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("old_string is required")
|
||||
}
|
||||
newStr, ok := args["new_string"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("new_string is required")
|
||||
}
|
||||
|
||||
newStr = stripHashlines(newStr)
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
strContent := string(content)
|
||||
|
||||
hasHashlines, startLine, strippedOldStr := parseHashlines(oldStr)
|
||||
|
||||
if hasHashlines && startLine > 0 {
|
||||
fileLines := strings.Split(strContent, "\n")
|
||||
oldLines := strings.Split(strippedOldStr, "\n")
|
||||
|
||||
startIdx := startLine - 1
|
||||
endIdx := startIdx + len(oldLines)
|
||||
|
||||
if startIdx >= 0 && endIdx <= len(fileLines) {
|
||||
match := true
|
||||
for i := 0; i < len(oldLines); i++ {
|
||||
if fileLines[startIdx+i] != oldLines[i] {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if match {
|
||||
var finalLines []string
|
||||
finalLines = append(finalLines, fileLines[:startIdx]...)
|
||||
if newStr != "" {
|
||||
finalLines = append(finalLines, strings.Split(newStr, "\n")...)
|
||||
}
|
||||
finalLines = append(finalLines, fileLines[endIdx:]...)
|
||||
|
||||
newContent := strings.Join(finalLines, "\n")
|
||||
err = os.WriteFile(path, []byte(newContent), 0644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("Successfully edited %s", path), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to simple replacement
|
||||
strippedOldStr = stripHashlines(oldStr)
|
||||
if !strings.Contains(strContent, strippedOldStr) {
|
||||
return "", fmt.Errorf("old_string not found in file")
|
||||
}
|
||||
|
||||
// Replace first occurrence only
|
||||
newContent := strings.Replace(strContent, strippedOldStr, newStr, 1)
|
||||
|
||||
err = os.WriteFile(path, []byte(newContent), 0644)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Successfully edited %s", path), nil
|
||||
}
|
||||
|
||||
func parseHashlines(s string) (bool, int, string) {
|
||||
if s == "" {
|
||||
return false, 0, s
|
||||
}
|
||||
var result []string
|
||||
lines := strings.Split(s, "\n")
|
||||
startLine := -1
|
||||
for i, line := range lines {
|
||||
parts := strings.SplitN(line, ":", 3)
|
||||
if len(parts) == 3 {
|
||||
isDigit := len(parts[0]) > 0
|
||||
for _, c := range parts[0] {
|
||||
if c < '0' || c > '9' {
|
||||
isDigit = false
|
||||
break
|
||||
}
|
||||
}
|
||||
isHex := len(parts[1]) == 4
|
||||
for _, c := range parts[1] {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||
isHex = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isDigit && isHex {
|
||||
if i == 0 {
|
||||
fmt.Sscanf(parts[0], "%d", &startLine)
|
||||
}
|
||||
result = append(result, parts[2])
|
||||
continue
|
||||
}
|
||||
}
|
||||
return false, 0, s
|
||||
}
|
||||
return true, startLine, strings.Join(result, "\n")
|
||||
}
|
||||
|
||||
func stripHashlines(s string) string {
|
||||
var result []string
|
||||
lines := strings.Split(s, "\n")
|
||||
for _, line := range lines {
|
||||
parts := strings.SplitN(line, ":", 3)
|
||||
if len(parts) == 3 {
|
||||
isDigit := len(parts[0]) > 0
|
||||
for _, c := range parts[0] {
|
||||
if c < '0' || c > '9' {
|
||||
isDigit = false
|
||||
break
|
||||
}
|
||||
}
|
||||
isHex := len(parts[1]) == 4
|
||||
for _, c := range parts[1] {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||
isHex = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isDigit && isHex {
|
||||
result = append(result, parts[2])
|
||||
continue
|
||||
}
|
||||
}
|
||||
result = append(result, line)
|
||||
}
|
||||
return strings.Join(result, "\n")
|
||||
}
|
||||
|
||||
func hashLine(line string) string {
|
||||
var h uint32 = 2166136261
|
||||
for i := 0; i < len(line); i++ {
|
||||
h ^= uint32(line[i])
|
||||
h *= 16777619
|
||||
}
|
||||
return fmt.Sprintf("%04x", h&0xffff)
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashLine(t *testing.T) {
|
||||
line := "hello world"
|
||||
h1 := hashLine(line)
|
||||
h2 := hashLine(line)
|
||||
if h1 != h2 {
|
||||
t.Errorf("hashLine is not deterministic: %s != %s", h1, h2)
|
||||
}
|
||||
if len(h1) != 4 {
|
||||
t.Errorf("hashLine output length is not 4: %d", len(h1))
|
||||
}
|
||||
|
||||
h3 := hashLine("hello worle")
|
||||
if h1 == h3 {
|
||||
t.Errorf("hashLine collision for similar strings: %s == %s", h1, h3)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripHashlines(t *testing.T) {
|
||||
input := "1:abcd:line 1\n2:ef01:line 2\nnot a hashline\n3:ghij:line 3"
|
||||
// ghij is not valid hex, so it should NOT be stripped if we follow the code strictly
|
||||
// Wait, my code checks for hex: (c >= 'a' && c <= 'f')
|
||||
// So 'ghij' should not be stripped.
|
||||
|
||||
expected := "line 1\nline 2\nnot a hashline\n3:ghij:line 3"
|
||||
got := stripHashlines(input)
|
||||
if got != expected {
|
||||
t.Errorf("stripHashlines failed.\nGot:\n%s\nExpected:\n%s", got, expected)
|
||||
}
|
||||
|
||||
input2 := "10:1234:content with : colons"
|
||||
expected2 := "content with : colons"
|
||||
got2 := stripHashlines(input2)
|
||||
if got2 != expected2 {
|
||||
t.Errorf("stripHashlines failed for content with colons.\nGot: %s\nExpected: %s", got2, expected2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditFileHandler_ExactHashlines(t *testing.T) {
|
||||
tmpFile := "test_edit_exact.txt"
|
||||
initial := "line one\nline two\nline three\nline four"
|
||||
os.WriteFile(tmpFile, []byte(initial), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
h2 := hashLine("line two")
|
||||
h3 := hashLine("line three")
|
||||
|
||||
args := map[string]interface{}{
|
||||
"path": tmpFile,
|
||||
"old_string": "2:" + h2 + ":line two\n3:" + h3 + ":line three",
|
||||
"new_string": "replaced two\nreplaced three",
|
||||
}
|
||||
_, err := editFileHandler(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
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. Got: %s Expected: %s",
|
||||
string(got), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrepFileHandler_Hashlines(t *testing.T) {
|
||||
tmpFile := "test_read_hash.txt"
|
||||
content := "line one\nline two"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
args := map[string]interface{}{"path": tmpFile}
|
||||
got, err := readFileHandler(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lines := strings.Split(got, "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("expected 2 lines, got %d", len(lines))
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(lines[0], "1:") || !strings.HasSuffix(lines[0], ":line one") {
|
||||
t.Errorf("line 0 format wrong: %s", lines[0])
|
||||
}
|
||||
if !strings.HasPrefix(lines[1], "2:") || !strings.HasSuffix(lines[1], ":line two") {
|
||||
t.Errorf("line 1 format wrong: %s", lines[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileHandler_Strip(t *testing.T) {
|
||||
tmpFile := "test_write_strip.txt"
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
content := "1:abcd:clean line\n2:1234:another line"
|
||||
args := map[string]interface{}{
|
||||
"path": tmpFile,
|
||||
"content": content,
|
||||
}
|
||||
_, err := writeFileHandler(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, _ := os.ReadFile(tmpFile)
|
||||
expected := "clean line\nanother line"
|
||||
if string(got) != expected {
|
||||
t.Errorf("writeFileHandler did not strip hashlines.\nGot: %s\nExpected: %s", string(got), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditFileHandler_Strip(t *testing.T) {
|
||||
tmpFile := "test_edit_strip.txt"
|
||||
initial := "find me\nkeep me"
|
||||
os.WriteFile(tmpFile, []byte(initial), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
args := map[string]interface{}{
|
||||
"path": tmpFile,
|
||||
"old_string": "1:abcd:find me",
|
||||
"new_string": "1:beef:replaced",
|
||||
}
|
||||
_, err := editFileHandler(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, _ := os.ReadFile(tmpFile)
|
||||
expected := "replaced\nkeep me"
|
||||
if string(got) != expected {
|
||||
t.Errorf("editFileHandler did not strip hashlines.\nGot: %s\nExpected: %s", string(got), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrepFileHandler_HashlineAnchors(t *testing.T) {
|
||||
tmpFile := "test_grep_anchors.txt"
|
||||
content := "first line\nsecond line\nthird needle\nfourth line\nfifth needle"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
args := map[string]interface{}{
|
||||
"path": tmpFile,
|
||||
"pattern": "needle",
|
||||
"context_lines": 1.0,
|
||||
}
|
||||
got, err := grepFileHandler(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Lines in file:
|
||||
// 1: first line
|
||||
// 2: second line
|
||||
// 3: third needle (MATCH)
|
||||
// 4: fourth line
|
||||
// 5: fifth needle (MATCH)
|
||||
|
||||
// Expected output structure should contain:
|
||||
// --- Match around line 3 ---
|
||||
// 2:<hash>:second line
|
||||
// 3:<hash>:third needle
|
||||
// 4:<hash>:fourth line
|
||||
// --- Match around line 5 ---
|
||||
// 4:<hash>:fourth line
|
||||
// 5:<hash>:fifth needle
|
||||
|
||||
lines := strings.Split(got, "\n")
|
||||
|
||||
findLine := func(prefix string) bool {
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Check anchors
|
||||
if !findLine("--- Match around line 3 ---") {
|
||||
t.Errorf("missing match anchor for line 3")
|
||||
}
|
||||
if !findLine("--- Match around line 5 ---") {
|
||||
t.Errorf("missing match anchor for line 5")
|
||||
}
|
||||
|
||||
// Check specific hashline prefixes
|
||||
h2 := hashLine("second line")
|
||||
h3 := hashLine("third needle")
|
||||
h5 := hashLine("fifth needle")
|
||||
|
||||
if !findLine("2:" + h2 + ":second line") {
|
||||
t.Errorf("missing correctly hashed context line 2")
|
||||
}
|
||||
if !findLine("3:" + h3 + ":third needle") {
|
||||
t.Errorf("missing correctly hashed match line 3")
|
||||
}
|
||||
if !findLine("5:" + h5 + ":fifth needle") {
|
||||
t.Errorf("missing correctly hashed match line 5")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
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, 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, streamCallback, reasoningCallback func(string)) (Message, error) {
|
||||
client := &http.Client{
|
||||
Timeout: model.Timeout,
|
||||
}
|
||||
|
||||
reqBody := map[string]interface{}{
|
||||
"model": model.ModelID,
|
||||
"messages": msgs,
|
||||
"temperature": model.Temperature,
|
||||
}
|
||||
if model.MaxTokens > 0 {
|
||||
reqBody["max_tokens"] = model.MaxTokens
|
||||
}
|
||||
|
||||
if len(ts) > 0 {
|
||||
var tools []map[string]interface{}
|
||||
for _, t := range ts {
|
||||
tools = append(tools, t.ConvertToOpenAITool())
|
||||
}
|
||||
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))
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+model.APIKey)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("HTTP request failed: %w", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
var openAIResp struct {
|
||||
Choices []struct {
|
||||
Message Message `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &openAIResp); err != nil {
|
||||
return Message{}, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
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{}
|
||||
|
||||
// SetLLMClient allows overriding the LLM client (e.g. for testing)
|
||||
func SetLLMClient(client LLMClient) {
|
||||
defaultLLMClient = client
|
||||
}
|
||||
|
||||
// CallLLM invokes the configured LLM client
|
||||
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.
|
||||
type mockLLMClient struct {
|
||||
responses []Message
|
||||
calls int
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
msg := Message{
|
||||
Role: MessageRoleAssistant,
|
||||
Content: `{"action": "RESPOND", "params": {"response": "Mock LLM Response"}}`,
|
||||
}
|
||||
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 fullReasoning 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"`
|
||||
ReasoningContent string `json:"reasoning_content"`
|
||||
Reasoning string `json:"reasoning"`
|
||||
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
|
||||
|
||||
rContent := delta.ReasoningContent
|
||||
if rContent == "" {
|
||||
rContent = delta.Reasoning
|
||||
}
|
||||
if rContent != "" && reasoningCallback != nil {
|
||||
fullReasoning.WriteString(rContent)
|
||||
reasoningCallback(rContent)
|
||||
}
|
||||
|
||||
if delta.Content != "" && streamCallback != nil {
|
||||
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()
|
||||
finalMsg.Reasoning = fullReasoning.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
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type dashLoggerWriter struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func (d *dashLoggerWriter) Write(p []byte) (n int, err error) {
|
||||
tStr := time.Now().Format("2006-01-02 15:04:05")
|
||||
return fmt.Fprintf(d.w, "%s %s", tStr, p)
|
||||
}
|
||||
|
||||
// SetupLogging sets up the global log package to use YYYY-MM-DD format
|
||||
func SetupLogging(w io.Writer) {
|
||||
log.SetFlags(0)
|
||||
log.SetOutput(&dashLoggerWriter{w: w})
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mark3labs/mcp-go/client"
|
||||
"github.com/mark3labs/mcp-go/client/transport"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
// MCPClientInterface represents a scoped connection to an MCP server
|
||||
type MCPClientInterface interface {
|
||||
CallTool(ctx context.Context, name string, args map[string]interface{}) (string, error)
|
||||
ListTools(ctx context.Context) ([]ToolDefinition, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type mcpGoClientWrapper struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
res, err := w.client.CallTool(ctx, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if res.IsError {
|
||||
return "", fmt.Errorf("tool returned error")
|
||||
}
|
||||
|
||||
// Extract text from result content
|
||||
var output string
|
||||
for _, c := range res.Content {
|
||||
if textContent, ok := c.(mcp.TextContent); ok {
|
||||
output += textContent.Text + "\n"
|
||||
} else {
|
||||
output += fmt.Sprintf("%v\n", c)
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (w *mcpGoClientWrapper) ListTools(ctx context.Context) ([]ToolDefinition, error) {
|
||||
req := mcp.ListToolsRequest{}
|
||||
res, err := w.client.ListTools(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tools []ToolDefinition
|
||||
for _, t := range res.Tools {
|
||||
var params map[string]interface{}
|
||||
b, err := json.Marshal(t.InputSchema)
|
||||
if err == nil {
|
||||
_ = json.Unmarshal(b, ¶ms)
|
||||
}
|
||||
|
||||
tools = append(tools, ToolDefinition{
|
||||
Name: t.Name,
|
||||
Description: t.Description,
|
||||
Parameters: params,
|
||||
Internal: false,
|
||||
})
|
||||
}
|
||||
return tools, nil
|
||||
}
|
||||
|
||||
func (w *mcpGoClientWrapper) Close() error {
|
||||
if w.client != nil {
|
||||
return w.client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Global server configurations
|
||||
var mcpServers map[string]MCPServerConfig
|
||||
|
||||
// GlobalLogger is an optional logger for MCP connections
|
||||
var GlobalLogger func(format string, v ...interface{})
|
||||
|
||||
// InitMCPServers sets up the global registry for MCP factories
|
||||
func InitMCPServers(servers map[string]MCPServerConfig) {
|
||||
mcpServers = servers
|
||||
}
|
||||
|
||||
type sidekickLogger struct{}
|
||||
|
||||
func (l *sidekickLogger) Infof(format string, v ...any) {
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *sidekickLogger) Errorf(format string, v ...any) {
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("ERROR: "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// defaultMCPFactory creates a real MCP client based on the global configuration
|
||||
var defaultMCPFactory MCPClientFactory = func(toolset string) (MCPClientInterface, error) {
|
||||
cfg, ok := mcpServers[toolset]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown MCP server: %s", toolset)
|
||||
}
|
||||
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("Initializing MCP connection to %s via %s", toolset, cfg.Transport)
|
||||
}
|
||||
|
||||
var c *client.Client
|
||||
var err error
|
||||
|
||||
switch cfg.Transport {
|
||||
case "stdio":
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("Running stdio MCP: %s %v (env: %d vars)", cfg.Command, cfg.Args, len(cfg.Env))
|
||||
}
|
||||
|
||||
command := cfg.Command
|
||||
args := cfg.Args
|
||||
|
||||
// If using npx, try to be quiet to avoid stdout pollution
|
||||
if command == "npx" {
|
||||
quietFound := false
|
||||
for _, arg := range args {
|
||||
if arg == "--quiet" || arg == "-q" {
|
||||
quietFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !quietFound {
|
||||
// Prepend --quiet to args
|
||||
args = append([]string{"--quiet"}, args...)
|
||||
}
|
||||
}
|
||||
|
||||
env := cfg.Env
|
||||
if len(env) > 0 {
|
||||
foundPath := false
|
||||
for _, e := range env {
|
||||
if strings.HasPrefix(strings.ToUpper(e), "PATH=") {
|
||||
foundPath = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundPath {
|
||||
if path := os.Getenv("PATH"); path != "" {
|
||||
env = append(env, "PATH="+path)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
env = os.Environ()
|
||||
}
|
||||
|
||||
// Use WithCommandFunc to configure the command environment
|
||||
c, err = client.NewStdioMCPClientWithOptions(command, env, args,
|
||||
transport.WithCommandFunc(func(ctx context.Context, command string,
|
||||
env []string, args []string) (*exec.Cmd, error) {
|
||||
cmd := exec.CommandContext(ctx, command, args...)
|
||||
cmd.Env = env
|
||||
return cmd, nil
|
||||
}),
|
||||
)
|
||||
case "sse":
|
||||
var opts []transport.ClientOption
|
||||
headers := make(map[string]string)
|
||||
if cfg.AuthToken != "" {
|
||||
headers["Authorization"] = "Bearer " + cfg.AuthToken
|
||||
}
|
||||
for k, v := range cfg.Headers {
|
||||
headers[k] = v
|
||||
}
|
||||
if len(headers) > 0 {
|
||||
opts = append(opts, transport.WithHeaders(headers))
|
||||
}
|
||||
c, err = client.NewSSEMCPClient(cfg.URL, opts...)
|
||||
case "http":
|
||||
var opts []transport.StreamableHTTPCOption
|
||||
headers := make(map[string]string)
|
||||
if cfg.AuthToken != "" {
|
||||
headers["Authorization"] = "Bearer " + cfg.AuthToken
|
||||
}
|
||||
for k, v := range cfg.Headers {
|
||||
headers[k] = v
|
||||
}
|
||||
if len(headers) > 0 {
|
||||
opts = append(opts, transport.WithHTTPHeaders(headers))
|
||||
}
|
||||
c, err = client.NewStreamableHttpClient(cfg.URL, opts...)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported MCP transport: %s", cfg.Transport)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Redirect stderr from the stdio transport to os.Stderr
|
||||
if cfg.Transport == "stdio" {
|
||||
if stderr, ok := client.GetStderr(c); ok {
|
||||
go io.Copy(os.Stderr, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the client session
|
||||
initReq := mcp.InitializeRequest{}
|
||||
initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
|
||||
initReq.Params.ClientInfo = mcp.Implementation{
|
||||
Name: "Sidekick",
|
||||
Version: "1.0.0",
|
||||
}
|
||||
|
||||
initCtx, initCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer initCancel()
|
||||
_, err = c.Initialize(initCtx, initReq)
|
||||
if err != nil {
|
||||
_ = c.Close()
|
||||
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
|
||||
}
|
||||
|
||||
return &mcpGoClientWrapper{client: c}, nil
|
||||
}
|
||||
|
||||
// MCPClientFactory is a function that creates a new MCP client for a specific toolset
|
||||
type MCPClientFactory func(toolset string) (MCPClientInterface, error)
|
||||
|
||||
// SetMCPClientFactory allows overriding the factory for testing
|
||||
func SetMCPClientFactory(factory MCPClientFactory) {
|
||||
defaultMCPFactory = factory
|
||||
}
|
||||
|
||||
// LoadExternalTools connects to all registered MCP servers and fetches their tools
|
||||
func LoadExternalTools(ctx context.Context) []ToolDefinition {
|
||||
var extTools []ToolDefinition
|
||||
for toolset := range mcpServers {
|
||||
c, err := defaultMCPFactory(toolset)
|
||||
if err != nil {
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("Failed to connect to MCP server %s: %v", toolset, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
listCtx, listCancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
tools, err := c.ListTools(listCtx)
|
||||
listCancel()
|
||||
c.Close()
|
||||
if err != nil {
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("Failed to list tools from MCP server %s: %v", toolset, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Assign toolset to each tool
|
||||
for i := range tools {
|
||||
tools[i].Toolset = toolset
|
||||
}
|
||||
|
||||
if GlobalLogger != nil {
|
||||
GlobalLogger("Loaded %d tools from MCP server %s", len(tools), toolset)
|
||||
}
|
||||
extTools = append(extTools, tools...)
|
||||
}
|
||||
return extTools
|
||||
}
|
||||
|
||||
// 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) {
|
||||
c, err := defaultMCPFactory(toolset)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create MCP client for %s: %w", toolset, err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
return c.CallTool(ctx, toolName, args)
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
func TestMCPServerQueryHandler(t *testing.T) {
|
||||
// 1. Setup Agent with mock LLM
|
||||
ac := AgentConfig{ID: "test-agent", Role: RoleSpecialist, MaxIterations: 2}
|
||||
mCfg := ModelConfig{}
|
||||
agent := NewSidekick(ac, mCfg, nil)
|
||||
|
||||
mockLLM := &mockLLMClient{
|
||||
responses: []Message{
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "I am Sidekick. How can I help?"}}`},
|
||||
},
|
||||
}
|
||||
SetLLMClient(mockLLM)
|
||||
|
||||
pool := map[string]*Sidekick{"test-agent": agent}
|
||||
|
||||
// 2. Define the handler (mirrors logic in cmd/sidekick-mcp/main.go)
|
||||
handler := func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
msg, err := request.RequireString("message")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inCtx []Message
|
||||
args := request.GetArguments()
|
||||
if histInter, ok := args["history"]; ok {
|
||||
if histList, ok := histInter.([]interface{}); ok {
|
||||
for _, h := range histList {
|
||||
if hMap, ok := h.(map[string]interface{}); ok {
|
||||
role, _ := hMap["role"].(string)
|
||||
content, _ := hMap["content"].(string)
|
||||
if role != "" && content != "" {
|
||||
inCtx = append(inCtx, Message{
|
||||
Role: MessageRole(role),
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inCtx = append(inCtx, Message{
|
||||
Role: MessageRoleUser,
|
||||
Content: msg,
|
||||
})
|
||||
|
||||
responseStr, err := agent.Run(ctx, inCtx, pool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outHistory := append(inCtx, Message{
|
||||
Role: MessageRole("assistant"),
|
||||
Content: responseStr,
|
||||
})
|
||||
|
||||
resultObj := map[string]interface{}{
|
||||
"response": responseStr,
|
||||
"history": outHistory,
|
||||
}
|
||||
|
||||
resultBytes, _ := json.Marshal(resultObj)
|
||||
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
mcp.NewTextContent(string(resultBytes)),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 3. Test with simple message
|
||||
req := mcp.CallToolRequest{}
|
||||
req.Params.Name = "query"
|
||||
req.Params.Arguments = map[string]interface{}{
|
||||
"message": "Hello",
|
||||
}
|
||||
|
||||
res, err := handler(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("handler failed: %v", err)
|
||||
}
|
||||
|
||||
if len(res.Content) != 1 {
|
||||
t.Fatalf("expected 1 content item, got %d", len(res.Content))
|
||||
}
|
||||
|
||||
textContent, ok := res.Content[0].(mcp.TextContent)
|
||||
if !ok {
|
||||
t.Fatalf("expected TextContent")
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(textContent.Text), &result); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(result["response"].(string), "How can I help?") {
|
||||
t.Errorf("unexpected response: %v", result["response"])
|
||||
}
|
||||
|
||||
history, ok := result["history"].([]interface{})
|
||||
if !ok || len(history) != 2 {
|
||||
t.Errorf("expected history of length 2, got %v", len(history))
|
||||
}
|
||||
|
||||
// 4. Test with history
|
||||
reqWithHist := mcp.CallToolRequest{}
|
||||
reqWithHist.Params.Name = "query"
|
||||
reqWithHist.Params.Arguments = map[string]interface{}{
|
||||
"message": "What did I say?",
|
||||
"history": []interface{}{
|
||||
map[string]interface{}{"role": "user", "content": "I like cats"},
|
||||
map[string]interface{}{"role": "assistant", "content": "Me too"},
|
||||
},
|
||||
}
|
||||
|
||||
// Reset mock for next run
|
||||
mockLLM.calls = 0
|
||||
mockLLM.responses = []Message{
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "You said you like cats."}}`},
|
||||
}
|
||||
|
||||
resHist, err := handler(context.Background(), reqWithHist)
|
||||
if err != nil {
|
||||
t.Fatalf("handler with history failed: %v", err)
|
||||
}
|
||||
|
||||
var resultHist map[string]interface{}
|
||||
textContentHist := resHist.Content[0].(mcp.TextContent)
|
||||
json.Unmarshal([]byte(textContentHist.Text), &resultHist)
|
||||
|
||||
historyFinal, ok := resultHist["history"].([]interface{})
|
||||
if !ok || len(historyFinal) != 4 { // 2 old + 1 new user + 1 assistant response
|
||||
t.Errorf("expected history of length 4, got %v", len(historyFinal))
|
||||
}
|
||||
|
||||
lastMsg := historyFinal[3].(map[string]interface{})
|
||||
if lastMsg["content"] != "You said you like cats." {
|
||||
t.Errorf("unexpected last message: %v", lastMsg["content"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// LoadPrompts reads a TOML file containing multiline prompts and compiles them into a template registry.
|
||||
func LoadPrompts(path string) (*template.Template, error) {
|
||||
var prompts map[string]string
|
||||
if _, err := toml.DecodeFile(path, &prompts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmpl := template.New("prompts")
|
||||
for name, content := range prompts {
|
||||
_, err := tmpl.New(name).Parse(content)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse prompt template '%s': %w", name, err)
|
||||
}
|
||||
}
|
||||
return tmpl, nil
|
||||
}
|
||||
|
||||
// RenderPrompt executes a specific prompt template by name.
|
||||
func RenderPrompt(tmpl *template.Template, name string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.ExecuteTemplate(&buf, name, nil); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPromptsTemplating(t *testing.T) {
|
||||
content := `
|
||||
base_rules = "You are helpful."
|
||||
coordinator = "{{template \"base_rules\"}} Route tasks."
|
||||
`
|
||||
tmpFile, err := os.CreateTemp("", "prompts-*.toml")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
if _, err := tmpFile.WriteString(content); err != nil {
|
||||
t.Fatalf("failed to write temp file: %v", err)
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
tmpl, err := LoadPrompts(tmpFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("LoadPrompts failed: %v", err)
|
||||
}
|
||||
|
||||
if tmpl.Lookup("coordinator") == nil {
|
||||
t.Fatalf("template 'coordinator' not found")
|
||||
}
|
||||
|
||||
rendered, err := RenderPrompt(tmpl, "coordinator")
|
||||
if err != nil {
|
||||
t.Fatalf("RenderPrompt failed: %v", err)
|
||||
}
|
||||
|
||||
expected := "You are helpful. Route tasks."
|
||||
if rendered != expected {
|
||||
t.Errorf("expected %q, got %q", expected, rendered)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSanitizeToolName(t *testing.T) {
|
||||
tests := []struct{ i, e string }{
|
||||
{"mcp/tool", "mcp_tool"},
|
||||
{"my.tool-name", "my_tool-name"},
|
||||
{"a space", "a_space"},
|
||||
{"already_safe", "already_safe"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if a := SanitizeToolName(tt.i); a != tt.e {
|
||||
t.Errorf("expected %s, got %s", tt.e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferGate(t *testing.T) {
|
||||
var tfs []string
|
||||
threshold := 10
|
||||
s := "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")
|
||||
}
|
||||
c, _ := os.ReadFile(tfs[0])
|
||||
if string(c) != l {
|
||||
t.Errorf("content mismatch")
|
||||
}
|
||||
os.Remove(tfs[0])
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleAgentLoop(t *testing.T) {
|
||||
c := AgentConfig{ID: "a1", Role: RoleSpecialist, MaxIterations: 5}
|
||||
a := NewSidekick(c, ModelConfig{}, nil)
|
||||
mc := &mockLLMClient{responses: []Message{
|
||||
{Content: `{"action": "TOOL_CALL", "params": {"tool_name": "unknown", "arguments": {}}}`},
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "Done"}}`},
|
||||
}}
|
||||
SetLLMClient(mc)
|
||||
ans, _ := a.Run(context.Background(), nil, nil)
|
||||
if ans != "Done" {
|
||||
t.Errorf("expected Done, got %s", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelegation(t *testing.T) {
|
||||
sc := AgentConfig{ID: "s1", Role: RoleSpecialist, MaxIterations: 2}
|
||||
sa := NewSidekick(sc, ModelConfig{}, nil)
|
||||
cc := AgentConfig{ID: "c1", Role: RoleCoordinator, Subagents: []string{"s1"}, MaxIterations: 3}
|
||||
coord := NewSidekick(cc, ModelConfig{}, nil)
|
||||
mc := &mockLLMClient{responses: []Message{
|
||||
{Content: `{"action": "DELEGATE", "params": {"subagent_id": "s1", "task": "do"}}`},
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "Sub done"}}`},
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "Coord done"}}`},
|
||||
}}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellExecRegistration(t *testing.T) {
|
||||
// Scenario 1: Disabled (default)
|
||||
c1 := AgentConfig{ID: "a1", EnableShellExec: false}
|
||||
a1 := NewSidekick(c1, ModelConfig{}, nil)
|
||||
if _, ok := a1.ToolRegistry["shell_exec"]; ok {
|
||||
t.Errorf("shell_exec should be disabled by default")
|
||||
}
|
||||
|
||||
// Scenario 2: Enabled
|
||||
c2 := AgentConfig{ID: "a2", EnableShellExec: true}
|
||||
a2 := NewSidekick(c2, ModelConfig{}, nil)
|
||||
if _, ok := a2.ToolRegistry["shell_exec"]; !ok {
|
||||
t.Errorf("shell_exec should be enabled when EnableShellExec is true")
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
return h, nil
|
||||
}}
|
||||
a.ToolMapping["huge"] = "huge"
|
||||
mc := &mockLLMClient{responses: []Message{
|
||||
{Content: `{"action": "TOOL_CALL", "params": {"tool_name": "huge", "arguments": {}}}`},
|
||||
{Content: `{"action": "RESPOND", "params": {"response": "Ok"}}`},
|
||||
}}
|
||||
SetLLMClient(mc)
|
||||
bfs, _ := os.ReadDir(os.TempDir())
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package sidekick
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// ToolDefinition represents a registered tool
|
||||
type ToolDefinition struct {
|
||||
Name string
|
||||
Description string
|
||||
Parameters map[string]interface{} // JSON Schema
|
||||
Toolset string // MCP server name
|
||||
Internal bool // If true, bypasses MCP
|
||||
Handler ToolHandler // Used if Internal is true
|
||||
}
|
||||
|
||||
// ToolHandler is a function type for internal tools
|
||||
type ToolHandler func(args map[string]interface{}) (string, error)
|
||||
|
||||
var nonAlphaNumRegex = regexp.MustCompile(`[^a-zA-Z0-9_\-]`)
|
||||
|
||||
// SanitizeToolName converts arbitrary names (like "mcp/tool") into safe names ("mcp_tool")
|
||||
func SanitizeToolName(name string) string {
|
||||
return nonAlphaNumRegex.ReplaceAllString(name, "_")
|
||||
}
|
||||
|
||||
// ConvertToOpenAITool formats the tool for the LLM API
|
||||
func (t *ToolDefinition) ConvertToOpenAITool() map[string]interface{} {
|
||||
sanitized := SanitizeToolName(t.Name)
|
||||
|
||||
params := t.Parameters
|
||||
if params == nil {
|
||||
params = map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{},
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "function",
|
||||
"function": map[string]interface{}{
|
||||
"name": sanitized,
|
||||
"description": t.Description,
|
||||
"parameters": params,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ParseArgs parses JSON string arguments into a map
|
||||
func ParseArgs(argsStr string) (map[string]interface{}, error) {
|
||||
var args map[string]interface{}
|
||||
if argsStr == "" {
|
||||
return make(map[string]interface{}), nil
|
||||
}
|
||||
err := json.Unmarshal([]byte(argsStr), &args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse tool arguments: %w", err)
|
||||
}
|
||||
return args, nil
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package sidekick
|
||||
|
||||
// Role represents the role of an agent
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleCoordinator Role = "COORDINATOR"
|
||||
RoleSpecialist Role = "SPECIALIST"
|
||||
RoleCollector Role = "COLLECTOR"
|
||||
)
|
||||
|
||||
// MessageRole represents the role of a message sender
|
||||
type MessageRole string
|
||||
|
||||
const (
|
||||
MessageRoleSystem MessageRole = "system"
|
||||
MessageRoleUser MessageRole = "user"
|
||||
MessageRoleAssistant MessageRole = "assistant"
|
||||
MessageRoleTool MessageRole = "tool"
|
||||
)
|
||||
|
||||
// Message is a chat message in the context
|
||||
type Message struct {
|
||||
Role MessageRole `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Reasoning string `json:"reasoning_content,omitempty"`
|
||||
// For native tool calls from LLM
|
||||
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
||||
// For tool responses
|
||||
ToolCallID string `json:"tool_call_id,omitempty"`
|
||||
}
|
||||
|
||||
// ToolCall represents a tool invocation from the LLM
|
||||
type ToolCall struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"` // usually "function"
|
||||
Function FunctionCall `json:"function"`
|
||||
}
|
||||
|
||||
// FunctionCall contains the function name and arguments
|
||||
type FunctionCall struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"` // JSON string
|
||||
}
|
||||
|
||||
// ActionType represents the type of action the agent decided to take
|
||||
type ActionType string
|
||||
|
||||
const (
|
||||
ActionTypeToolCall ActionType = "TOOL_CALL"
|
||||
ActionTypeDelegate ActionType = "DELEGATE"
|
||||
ActionTypeRespond ActionType = "RESPOND"
|
||||
)
|
||||
|
||||
// ActionEnvelope is the JSON format contract expected from the LLM (if not using native tool calls)
|
||||
type ActionEnvelope struct {
|
||||
Action ActionType `json:"action"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
Reasoning string `json:"reasoning,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user