Files
slacker-bot/slackerbot/internal/tools/session.go
T
2026-11-01 14:55:33 -05:00

84 lines
2.0 KiB
Go

package tools
import (
"encoding/json"
"flag"
"os"
"strconv"
"strings"
"sync"
"slackerbot/internal/acp"
)
// Installed by nix/container.nix.
const Command = "/run/current-system/sw/bin/slackerbot-tools"
// Main runs the server, for `slackerbot tools`.
func Main(args []string) error {
fs := flag.NewFlagSet("tools", flag.ExitOnError)
home := fs.String("home", "/root", "the folder files may be sent from")
maxMB := fs.Int64("max-file-mb", 100, "the largest file send_file accepts")
_ = fs.Parse(args)
s := &Server{Home: *home, MaxMB: *maxMB}
return s.Serve(os.Stdin, os.Stdout)
}
// Spec is how goose starts the server, passed in session/new and session/load.
func Spec(home string, maxMB int64) acp.MCPServer {
return acp.MCPServer{
Name: ServerName,
Command: Command,
Args: []string{"tools", "-home", home, "-max-file-mb", strconv.FormatInt(maxMB, 10)},
Env: []any{},
}
}
type FileRequest struct {
Path string `json:"path"`
Comment string `json:"comment"`
}
// Calls watches a turn's ACP updates and collects send_file calls that completed.
type Calls struct {
mu sync.Mutex
pending map[string]FileRequest
done []FileRequest
}
func isSendFile(u acp.Update) bool {
return u.ToolName == ServerName+"__"+SendFile ||
strings.HasPrefix(u.Title, ServerName+": "+strings.ReplaceAll(SendFile, "_", " "))
}
func (c *Calls) Update(u acp.Update) {
c.mu.Lock()
defer c.mu.Unlock()
switch u.Kind {
case "tool_call":
var req FileRequest
if u.ToolID == "" || !isSendFile(u) || json.Unmarshal(u.RawInput, &req) != nil || req.Path == "" {
return
}
if c.pending == nil {
c.pending = map[string]FileRequest{}
}
c.pending[u.ToolID] = req
case "tool_call_update":
req, ok := c.pending[u.ToolID]
if !ok || u.Status != "completed" && u.Status != "failed" {
return
}
delete(c.pending, u.ToolID)
if u.Status == "completed" {
c.done = append(c.done, req)
}
}
}
func (c *Calls) Files() []FileRequest {
c.mu.Lock()
defer c.mu.Unlock()
return c.done
}