Files
slacker-bot/slackerbot/internal/bot/assistant.go
T

72 lines
2.2 KiB
Go

package bot
import (
"context"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
// where describes a conversation for the agent, or "" for a DM.
func (b *Bot) where(ctx context.Context, channel string) string {
info, ok := b.channels.Load(channel)
if !ok {
c, err := b.api.GetConversationInfoContext(ctx, &slack.GetConversationInfoInput{ChannelID: channel})
if err != nil {
return ""
}
info, _ = b.channels.LoadOrStore(channel, c)
}
c := info.(*slack.Channel)
switch {
case c.IsIM:
return ""
case c.IsMpIM:
return "a Slack group DM"
}
return "the Slack channel #" + c.Name
}
func (b *Bot) assistantStarted(ctx context.Context, t slackevents.AssistantThread) {
b.setViewing(t)
params := slack.AssistantThreadsSetSuggestedPromptsParameters{ChannelID: t.ChannelID, ThreadTS: t.ThreadTimeStamp}
params.AddPrompt("Container status", "status")
params.AddPrompt("Past sessions", "sessions")
params.AddPrompt("What can you do?", "help")
_ = b.api.SetAssistantThreadsSuggestedPromptsContext(ctx, params)
}
func (b *Bot) setViewing(t slackevents.AssistantThread) {
if t.Context.ChannelID != "" {
b.viewing.Store(t.ChannelID+"/"+t.ThreadTimeStamp, t.Context.ChannelID)
}
}
// viewingNote tells the agent which channel the user has open next to the
// assistant pane, once per change.
func (b *Bot) viewingNote(ctx context.Context, channel, threadTS string) string {
key := channel + "/" + threadTS
v, ok := b.viewing.Load(key)
if !ok {
return ""
}
if told, _ := b.told.Swap(key, v); told == v {
return ""
}
if w := b.where(ctx, v.(string)); w != "" {
return "(The user is looking at " + w + ".)\n"
}
return ""
}
// Assistant thread calls fail harmlessly in ordinary DM threads.
func (b *Bot) setAssistantStatus(ctx context.Context, channel, threadTS, status string) {
_ = b.api.SetAssistantThreadsStatusContext(ctx, slack.AssistantThreadsSetStatusParameters{
ChannelID: channel, ThreadTS: threadTS, Status: status})
}
func (b *Bot) setAssistantTitle(ctx context.Context, channel, threadTS, text string) {
_ = b.api.SetAssistantThreadsTitleContext(ctx, slack.AssistantThreadsSetTitleParameters{
ChannelID: channel, ThreadTS: threadTS, Title: text})
}