bazel start + mcp start
This commit is contained in:
1 parent
a90653260a
commit
4c082aca7c
33 files changed
+1728
-79
No files matched your search
@@ -27,6 +27,7 @@ import (
|
||||
"slackerbot/internal/node"
|
||||
"slackerbot/internal/state"
|
||||
"slackerbot/internal/storage"
|
||||
"slackerbot/internal/tools"
|
||||
"slackerbot/internal/units"
|
||||
"slackerbot/internal/util"
|
||||
)
|
||||
@@ -84,6 +85,7 @@ type incoming struct {
|
||||
user, channel, ts, threadTS, text string
|
||||
dm bool
|
||||
heard bool
|
||||
files []slack.File
|
||||
}
|
||||
|
||||
func (m incoming) owner() (string, model.OwnerKind) {
|
||||
@@ -152,17 +154,17 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
|
||||
if e.BotID != "" {
|
||||
return
|
||||
}
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false})
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false, eventFiles(ev)})
|
||||
case *slackevents.MessageEvent:
|
||||
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||
if e.BotID != "" || (e.SubType != "" && e.SubType != "file_share") || e.User == "" || e.User == b.botID {
|
||||
return
|
||||
}
|
||||
text := b.stripMention(e.Text)
|
||||
switch {
|
||||
case e.ChannelType == "im":
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false, eventFiles(ev)})
|
||||
case b.listening(e.Channel, e.ThreadTimeStamp, e.Text):
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true})
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true, eventFiles(ev)})
|
||||
}
|
||||
case *slackevents.ReactionAddedEvent:
|
||||
b.onReaction(e)
|
||||
@@ -178,15 +180,17 @@ func (b *Bot) stripMention(text string) string {
|
||||
}
|
||||
|
||||
func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
if b.listenCommand(ctx, m) {
|
||||
// A message with files always goes to the agent, so a file with no text isn't read as `help`.
|
||||
commandable := len(m.files) == 0
|
||||
if commandable && b.listenCommand(ctx, m) {
|
||||
return
|
||||
}
|
||||
// Every assistant-pane chat is a DM thread, so its first message counts as new.
|
||||
fresh := m.threadTS == "" || m.dm && b.store.Session(m.channel, m.threadTS) == nil
|
||||
if fresh && b.command(ctx, m) {
|
||||
if commandable && fresh && b.command(ctx, m) {
|
||||
return
|
||||
}
|
||||
if m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
|
||||
if commandable && m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
|
||||
return
|
||||
}
|
||||
root := m.threadTS
|
||||
@@ -297,6 +301,16 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
if m.dm {
|
||||
text = b.viewingNote(ctx, m.channel, root) + text
|
||||
}
|
||||
if len(m.files) > 0 {
|
||||
edit(":paperclip: Copying the attachments into the container…")
|
||||
note, problems := b.attach(ctx, u, m)
|
||||
if note != "" {
|
||||
text += "\n" + note
|
||||
}
|
||||
if len(problems) > 0 {
|
||||
b.say(ctx, m.channel, root, ":warning: "+strings.Join(problems, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
edit("_working…_")
|
||||
pctx, cancel := context.WithCancel(ctx)
|
||||
@@ -311,11 +325,16 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
defer b.setAssistantStatus(ctx, m.channel, root, "")
|
||||
}
|
||||
st := NewStreamer(b.api, m.channel, root, ph)
|
||||
stop, err := acp.Prompt(pctx, id, text, st.Update)
|
||||
var sends tools.Calls
|
||||
stop, err := acp.Prompt(pctx, id, text, func(up acpUpdate) {
|
||||
sends.Update(up)
|
||||
st.Update(up)
|
||||
})
|
||||
if pctx.Err() != nil && ctx.Err() == nil {
|
||||
stop, err = "cancelled", nil
|
||||
}
|
||||
st.Finish(stop, err)
|
||||
b.upload(ctx, u, m.channel, root, sends.Files())
|
||||
_ = b.store.Touch(u.ID)
|
||||
switch {
|
||||
case stop == "cancelled":
|
||||
@@ -1043,6 +1062,7 @@ func (b *Bot) client(ctx context.Context, u *model.Owner) (*acp.ACP, error) {
|
||||
if c, err = acp.New(initCtx, conn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Servers = b.mcpServers()
|
||||
b.acpMu.Lock()
|
||||
b.acps[key] = c
|
||||
b.acpMu.Unlock()
|
||||
|
||||
@@ -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