making use of the extended perms and added future work
This commit is contained in:
1 parent
296e465f48
commit
22fd8fbce8
19 files changed
+617
-40
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -34,6 +35,9 @@ const helpText = "I pass your messages to an agent running in its own container.
|
||||
"• In a DM, the container is yours alone. In a channel, it belongs to the channel " +
|
||||
"and anyone here can use it by @mentioning me.\n" +
|
||||
"• Every new message starts a new session. Reply in its thread to keep going.\n" +
|
||||
"• In a channel thread I only read replies that @mention me. `/listen all-messages` in the thread makes me read them all, " +
|
||||
"`/listen channel all-messages` sets the channel's default.\n" +
|
||||
"• React with :x: to stop a reply part-way.\n" +
|
||||
"*Commands* (send as a new message, not in a thread):\n" +
|
||||
"• `status`: state, size and idle limit, with buttons\n" +
|
||||
"• `sessions`: past sessions, with links\n" +
|
||||
@@ -67,6 +71,11 @@ type Bot struct {
|
||||
busy map[string]int
|
||||
names sync.Map
|
||||
|
||||
channels sync.Map // channel ID → *slack.Channel
|
||||
running sync.Map // "channel/ts" of a prompt's message or reply → its cancel func
|
||||
viewing sync.Map // assistant "channel/thread" → channel the user has open
|
||||
told sync.Map // assistant "channel/thread" → last channel the agent was told about
|
||||
|
||||
waitMu sync.Mutex
|
||||
waiting map[string]map[string]bool
|
||||
}
|
||||
@@ -74,6 +83,7 @@ type Bot struct {
|
||||
type incoming struct {
|
||||
user, channel, ts, threadTS, text string
|
||||
dm bool
|
||||
heard bool
|
||||
}
|
||||
|
||||
func (m incoming) owner() (string, model.OwnerKind) {
|
||||
@@ -143,20 +153,37 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
|
||||
return
|
||||
}
|
||||
text := strings.TrimSpace(strings.ReplaceAll(e.Text, "<@"+b.botID+">", ""))
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false})
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, false})
|
||||
case *slackevents.MessageEvent:
|
||||
if e.ChannelType != "im" || e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||
return
|
||||
}
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, strings.TrimSpace(e.Text), true})
|
||||
text := strings.TrimSpace(e.Text)
|
||||
switch {
|
||||
case e.ChannelType == "im":
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
|
||||
case b.listening(e.Channel, e.ThreadTimeStamp, e.Text):
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true})
|
||||
}
|
||||
case *slackevents.ReactionAddedEvent:
|
||||
b.onReaction(e)
|
||||
case *slackevents.AssistantThreadStartedEvent:
|
||||
b.assistantStarted(ctx, e.AssistantThread)
|
||||
case *slackevents.AssistantThreadContextChangedEvent:
|
||||
b.setViewing(e.AssistantThread)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
if m.threadTS == "" && b.command(ctx, m) {
|
||||
if b.listenCommand(ctx, m) {
|
||||
return
|
||||
}
|
||||
if m.threadTS != "" && b.moveCommand(ctx, m, m.threadTS) {
|
||||
// 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) {
|
||||
return
|
||||
}
|
||||
if m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
|
||||
return
|
||||
}
|
||||
root := m.threadTS
|
||||
@@ -179,6 +206,14 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
edit := func(text string) {
|
||||
_, _, _, _ = b.api.UpdateMessageContext(ctx, m.channel, ph, slack.MsgOptionText(text, false))
|
||||
}
|
||||
b.react(ctx, m.channel, m.ts, "eyes")
|
||||
outcome := "warning"
|
||||
defer func() {
|
||||
b.unreact(ctx, m.channel, m.ts, "eyes")
|
||||
if outcome != "" {
|
||||
b.react(ctx, m.channel, m.ts, outcome)
|
||||
}
|
||||
}()
|
||||
|
||||
ownerID, kind := m.owner()
|
||||
var off *node.NodeOfflineError
|
||||
@@ -220,6 +255,10 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
|
||||
var id string
|
||||
text := m.text
|
||||
if !m.dm {
|
||||
text = b.displayName(ctx, m.user) + ": " + text
|
||||
}
|
||||
if sess == nil {
|
||||
if id, err = acp.NewSession(ctx, b.cfg.AgentCwd); err != nil {
|
||||
edit(":warning: Couldn't start a session: " + err.Error())
|
||||
@@ -229,6 +268,12 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
Channel: m.channel, ThreadTS: root, OwnerID: u.ID,
|
||||
ACPID: id, Title: title(m.text), CreatedAt: time.Now(),
|
||||
})
|
||||
if w := b.where(ctx, m.channel); w != "" {
|
||||
text = "(You're in " + w + ". Each message starts with the sender's name.)\n" + text
|
||||
}
|
||||
if m.dm && m.threadTS != "" {
|
||||
b.setAssistantTitle(ctx, m.channel, root, title(m.text))
|
||||
}
|
||||
} else {
|
||||
id = sess.ACPID
|
||||
if err := acp.LoadSession(ctx, id, b.cfg.AgentCwd); err != nil {
|
||||
@@ -237,16 +282,35 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
}
|
||||
|
||||
text := m.text
|
||||
if !m.dm {
|
||||
text = b.displayName(ctx, m.user) + ": " + text
|
||||
if m.dm {
|
||||
text = b.viewingNote(ctx, m.channel, root) + text
|
||||
}
|
||||
|
||||
edit("_working…_")
|
||||
pctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for _, ts := range []string{m.ts, ph} {
|
||||
key := m.channel + "/" + ts
|
||||
b.running.Store(key, cancel)
|
||||
defer b.running.Delete(key)
|
||||
}
|
||||
if m.dm && m.threadTS != "" {
|
||||
b.setAssistantStatus(ctx, m.channel, root, "is working…")
|
||||
defer b.setAssistantStatus(ctx, m.channel, root, "")
|
||||
}
|
||||
st := NewStreamer(b.api, m.channel, root, ph)
|
||||
stop, err := acp.Prompt(ctx, id, text, st.Update)
|
||||
stop, err := acp.Prompt(pctx, id, text, st.Update)
|
||||
if pctx.Err() != nil && ctx.Err() == nil {
|
||||
stop, err = "cancelled", nil
|
||||
}
|
||||
st.Finish(stop, err)
|
||||
_ = b.store.Touch(u.ID)
|
||||
switch {
|
||||
case stop == "cancelled":
|
||||
outcome = ""
|
||||
case err == nil:
|
||||
outcome = "white_check_mark"
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
@@ -256,16 +320,16 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
args = strings.TrimRight(strings.TrimSpace(args), " .!?")
|
||||
|
||||
if verb == "move" || verb == "migrate" {
|
||||
return b.moveCommand(ctx, m, m.ts)
|
||||
return b.moveCommand(ctx, m, cmp.Or(m.threadTS, m.ts))
|
||||
}
|
||||
if verb == "grant" || verb == "revoke" || verb == "dm" || verb == "managers" || (verb == "permissions" && args != "") {
|
||||
return b.manageCommand(ctx, m, verb, args)
|
||||
}
|
||||
if need := commands.NeededPermission(verb, args); need != "" && !b.permitted(ctx, m.channel, "", m.user, kind, need) {
|
||||
if need := commands.NeededPermission(verb, args); need != "" && !b.permitted(ctx, m.channel, m.threadTS, m.user, kind, need) {
|
||||
return true
|
||||
}
|
||||
if verb == "stop" || verb == "scale" || verb == "storage" || verb == "disk" {
|
||||
if b.blocked(ctx, m.channel, "", ownerID) {
|
||||
if b.blocked(ctx, m.channel, m.threadTS, ownerID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -277,7 +341,7 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
case verb == "sessions" && args == "":
|
||||
reply = b.sessionList(ctx, ownerID)
|
||||
case verb == "status" && args == "":
|
||||
b.postStatus(ctx, m.channel, ownerID, kind)
|
||||
b.postStatus(ctx, m.channel, m.threadTS, ownerID, kind)
|
||||
return true
|
||||
case verb == "stop" && args == "":
|
||||
reply = b.stopFor(ctx, ownerID)
|
||||
@@ -303,7 +367,7 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
default:
|
||||
return false
|
||||
}
|
||||
b.say(ctx, m.channel, "", reply)
|
||||
b.say(ctx, m.channel, m.threadTS, reply)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -311,6 +375,7 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
if cb.Type != slack.InteractionTypeBlockActions {
|
||||
return
|
||||
}
|
||||
thread := cb.Message.ThreadTimestamp
|
||||
for _, a := range cb.ActionCallback.BlockActions {
|
||||
ownerID := a.Value
|
||||
if ownerID != cb.User.ID && ownerID != cb.Channel.ID {
|
||||
@@ -322,7 +387,7 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
}
|
||||
u := b.store.Owner(ownerID)
|
||||
if need := map[string]model.Action{"recreate": model.ActMove, "scale": model.ActScale}[a.ActionID]; need != "" &&
|
||||
!b.permitted(ctx, cb.Channel.ID, "", cb.User.ID, kind, need) {
|
||||
!b.permitted(ctx, cb.Channel.ID, thread, cb.User.ID, kind, need) {
|
||||
continue
|
||||
}
|
||||
switch a.ActionID {
|
||||
@@ -331,10 +396,10 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
b.dropClient(u)
|
||||
}
|
||||
if err := b.mgr.Recreate(ownerID); err != nil {
|
||||
b.say(ctx, cb.Channel.ID, "", ":warning: "+err.Error())
|
||||
b.say(ctx, cb.Channel.ID, thread, ":warning: "+err.Error())
|
||||
continue
|
||||
}
|
||||
b.say(ctx, cb.Channel.ID, "", "OK. Your next message starts a fresh container on a healthy node. "+
|
||||
b.say(ctx, cb.Channel.ID, thread, "OK. Your next message starts a fresh container on a healthy node. "+
|
||||
"Old threads start new sessions when you reply in them.")
|
||||
case "wait":
|
||||
if u == nil || u.Node == "" {
|
||||
@@ -346,17 +411,17 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
}
|
||||
b.waiting[u.Node][cb.Channel.ID] = true
|
||||
b.waitMu.Unlock()
|
||||
b.say(ctx, cb.Channel.ID, "", fmt.Sprintf("OK, waiting for %s. I'll post here when it's back up.", u.Node))
|
||||
b.say(ctx, cb.Channel.ID, thread, fmt.Sprintf("OK, waiting for %s. I'll post here when it's back up.", u.Node))
|
||||
case "stop", "scale":
|
||||
if b.mgr.State(u) == manager.StateOffline {
|
||||
b.deadSlave(ctx, cb.Channel.ID, "", ownerID, u.Node)
|
||||
continue
|
||||
}
|
||||
if a.ActionID == "stop" {
|
||||
b.say(ctx, cb.Channel.ID, "", b.stopFor(ctx, ownerID))
|
||||
b.say(ctx, cb.Channel.ID, thread, b.stopFor(ctx, ownerID))
|
||||
} else {
|
||||
reply, _ := b.scaleFor(ctx, ownerID, kind, "up")
|
||||
b.say(ctx, cb.Channel.ID, "", reply)
|
||||
b.say(ctx, cb.Channel.ID, thread, reply)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -380,7 +445,7 @@ func (b *Bot) sessionList(ctx context.Context, ownerID string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, ownerID string, kind model.OwnerKind) {
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, thread, ownerID string, kind model.OwnerKind) {
|
||||
u := b.store.Owner(ownerID)
|
||||
state := b.mgr.State(u)
|
||||
size := b.cfg.Container
|
||||
@@ -426,7 +491,11 @@ func (b *Bot) postStatus(ctx context.Context, channel, ownerID string, kind mode
|
||||
blocks = append(blocks, slack.NewActionBlock("container", buttons...))
|
||||
}
|
||||
|
||||
if _, _, err := b.api.PostMessageContext(ctx, channel, slack.MsgOptionText(text, false), slack.MsgOptionBlocks(blocks...)); err != nil {
|
||||
opts := []slack.MsgOption{slack.MsgOptionText(text, false), slack.MsgOptionBlocks(blocks...)}
|
||||
if thread != "" {
|
||||
opts = append(opts, slack.MsgOptionTS(thread))
|
||||
}
|
||||
if _, _, err := b.api.PostMessageContext(ctx, channel, opts...); err != nil {
|
||||
log.Printf("slack post: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user