bazel start + mcp start
This commit is contained in:
1 parent
a90653260a
commit
4c082aca7c
33 files changed
+1728
-79
No files matched your search
@@ -0,0 +1,145 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
"slackerbot/internal/manager"
|
||||
"slackerbot/internal/model"
|
||||
"slackerbot/internal/node"
|
||||
"slackerbot/internal/tools"
|
||||
)
|
||||
|
||||
const (
|
||||
maxFilesPerTurn = 5
|
||||
uploadsDir = "slack-uploads"
|
||||
)
|
||||
|
||||
// onMessage's local `acp` client shadows the package name.
|
||||
type acpUpdate = acp.Update
|
||||
|
||||
func (b *Bot) mcpServers() []acp.MCPServer {
|
||||
return []acp.MCPServer{tools.Spec(b.cfg.AgentCwd, b.cfg.MaxSendFileMB)}
|
||||
}
|
||||
|
||||
// slack-go's app_mention event has no files field, so they're read from the raw event.
|
||||
func eventFiles(ev slackevents.EventsAPIEvent) []slack.File {
|
||||
cb, ok := ev.Data.(*slackevents.EventsAPICallbackEvent)
|
||||
if !ok || cb.InnerEvent == nil {
|
||||
return nil
|
||||
}
|
||||
var inner struct {
|
||||
Files []slack.File `json:"files"`
|
||||
}
|
||||
_ = json.Unmarshal(*cb.InnerEvent, &inner)
|
||||
return inner.Files
|
||||
}
|
||||
|
||||
func uploadName(f slack.File, i int, seen map[string]bool) string {
|
||||
name := strings.NewReplacer("/", "_", "\x00", "").Replace(path.Base(f.Name))
|
||||
if name == "" || name == "." || name == ".." {
|
||||
name = "file-" + strconv.Itoa(i+1)
|
||||
}
|
||||
if seen[name] {
|
||||
name = strconv.Itoa(i+1) + "-" + name
|
||||
}
|
||||
seen[name] = true
|
||||
return name
|
||||
}
|
||||
|
||||
// attach copies the message's files into the container and says where they went.
|
||||
func (b *Bot) attach(ctx context.Context, u *model.Owner, m incoming) (note string, problems []string) {
|
||||
maxBytes := b.cfg.MaxSendFileMB << 20
|
||||
dir := path.Join(b.cfg.AgentCwd, uploadsDir, m.ts)
|
||||
seen := map[string]bool{}
|
||||
var paths []string
|
||||
for i, f := range m.files {
|
||||
name := uploadName(f, i, seen)
|
||||
if f.Mode == "external" || f.Mode == "tombstone" || f.URLPrivateDownload == "" {
|
||||
problems = append(problems, fmt.Sprintf("%s isn't a file I can download.", name))
|
||||
continue
|
||||
}
|
||||
if int64(f.Size) > maxBytes {
|
||||
problems = append(problems, fmt.Sprintf("%s is bigger than the %d MB limit, so I left it out.", name, b.cfg.MaxSendFileMB))
|
||||
continue
|
||||
}
|
||||
dst := path.Join(dir, name)
|
||||
if err := b.copyIn(ctx, u, f, dst, maxBytes); err != nil {
|
||||
if errors.Is(err, node.ErrTooOld) {
|
||||
return "", []string{"This container's node can't take attachments yet, so I left them out. It needs a newer slackerbot."}
|
||||
}
|
||||
problems = append(problems, fmt.Sprintf("Couldn't copy %s into the container: %v.", name, err))
|
||||
continue
|
||||
}
|
||||
paths = append(paths, dst)
|
||||
}
|
||||
if len(paths) > 0 {
|
||||
note = "(Attached: " + strings.Join(paths, ", ") + ")"
|
||||
}
|
||||
return note, problems
|
||||
}
|
||||
|
||||
func (b *Bot) copyIn(ctx context.Context, u *model.Owner, f slack.File, dst string, maxBytes int64) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
pr, pw := io.Pipe()
|
||||
go func() { pw.CloseWithError(b.api.GetFileContext(ctx, f.URLPrivateDownload, pw)) }()
|
||||
err := b.mgr.WriteFile(ctx, u, dst, maxBytes, pr)
|
||||
pr.CloseWithError(err)
|
||||
if errors.Is(err, manager.ErrFileTooBig) {
|
||||
return fmt.Errorf("it's bigger than the %d MB limit", b.cfg.MaxSendFileMB)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *Bot) upload(ctx context.Context, u *model.Owner, channel, thread string, reqs []tools.FileRequest) {
|
||||
if len(reqs) > maxFilesPerTurn {
|
||||
b.say(ctx, channel, thread, fmt.Sprintf(":warning: The agent tried to send %d files. I only send %d per reply, so the rest were skipped.", len(reqs), maxFilesPerTurn))
|
||||
reqs = reqs[:maxFilesPerTurn]
|
||||
}
|
||||
for _, r := range reqs {
|
||||
if err := b.uploadOne(ctx, u, channel, thread, r); err != nil {
|
||||
name := path.Base(r.Path)
|
||||
switch {
|
||||
case errors.Is(err, node.ErrTooOld):
|
||||
b.say(ctx, channel, thread, "This container's node is too old to send files. It needs a newer slackerbot.")
|
||||
return
|
||||
case errors.Is(err, manager.ErrFileTooBig):
|
||||
b.say(ctx, channel, thread, fmt.Sprintf(":warning: Couldn't send %s: it's bigger than the %d MB limit.", name, b.cfg.MaxSendFileMB))
|
||||
default:
|
||||
log.Printf("send file %s from %s: %v", r.Path, u.Machine, err)
|
||||
b.say(ctx, channel, thread, fmt.Sprintf(":warning: Couldn't send %s: %v", name, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The file is buffered on the master, because files.uploadV2 needs the size up front.
|
||||
func (b *Bot) uploadOne(ctx context.Context, u *model.Owner, channel, thread string, r tools.FileRequest) error {
|
||||
var buf bytes.Buffer
|
||||
n, err := b.mgr.ReadFile(ctx, u, r.Path, b.cfg.MaxSendFileMB<<20, &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return errors.New("the file is empty")
|
||||
}
|
||||
name := path.Base(r.Path)
|
||||
_, err = b.api.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{
|
||||
Reader: &buf, FileSize: int(n), Filename: name, Title: name,
|
||||
Channel: channel, ThreadTimestamp: thread, InitialComment: r.Comment,
|
||||
})
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user