Documentation
¶
Overview ¶
Package slackbot provides Slack Socket Mode integration for Kojo agents.
Index ¶
- func DeleteTokens(tp TokenProvider, agentID string) error
- func FetchChannelHistory(ctx context.Context, api *slack.Client, agentDataDir, channelID string, ...) []chathistory.HistoryMessage
- func FetchThreadHistory(ctx context.Context, api *slack.Client, ...) []chathistory.HistoryMessage
- func LoadTokens(tp TokenProvider, agentID string) (appToken, botToken string, err error)
- func PlainToSlack(text string) string
- func SlackToPlain(text string, resolve UserResolver) string
- func SplitMessage(text string, maxLen int) []string
- func StoreTokens(tp TokenProvider, agentID, appToken, botToken string) error
- func StripBotMention(text, botUserID string) string
- func TestConnection(ctx context.Context, appToken, botToken string) (team, botUser string, err error)
- type AgentDataDirFunc
- type Bot
- type ChatManager
- type Hub
- type TokenProvider
- type UserResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteTokens ¶
func DeleteTokens(tp TokenProvider, agentID string) error
DeleteTokens removes all Slack tokens for an agent.
func FetchChannelHistory ¶
func FetchChannelHistory(ctx context.Context, api *slack.Client, agentDataDir, channelID string, limit int, resolve UserResolver, logger *slog.Logger) []chathistory.HistoryMessage
FetchChannelHistory retrieves recent channel messages from Slack. This is a sliding window that overwrites the previous channel history file.
func FetchThreadHistory ¶
func FetchThreadHistory(ctx context.Context, api *slack.Client, agentDataDir, channelID, threadTS string, resolve UserResolver, logger *slog.Logger) []chathistory.HistoryMessage
FetchThreadHistory retrieves thread messages from Slack.
- First call (no history file): fetches the entire thread including the parent message, and writes the file.
- Subsequent calls: uses the last real Slack timestamp in the file as a cursor to fetch only new messages (delta), appending them to the file.
The history file may also contain locally-appended bot response entries (with IDs like "1234567890.bot") used by shouldAutoReply. These are ignored when determining the delta cursor.
func LoadTokens ¶
func LoadTokens(tp TokenProvider, agentID string) (appToken, botToken string, err error)
LoadTokens retrieves the app and bot tokens from the credential store.
func PlainToSlack ¶
PlainToSlack converts standard Markdown from agent output to Slack mrkdwn. It preserves code blocks and inline code from being transformed.
func SlackToPlain ¶
func SlackToPlain(text string, resolve UserResolver) string
SlackToPlain converts Slack mrkdwn to plain text suitable for the agent. It resolves links and strips mention formatting. If resolve is non-nil, user mentions <@U12345> are resolved to display names.
func SplitMessage ¶
SplitMessage splits a long message into chunks that fit within Slack's message length limit (approximately 3000 chars per chunk for safety).
func StoreTokens ¶
func StoreTokens(tp TokenProvider, agentID, appToken, botToken string) error
StoreTokens saves the app and bot tokens to the credential store. If the second write fails, the first is rolled back to its previous value so the store never contains a half-updated pair.
func StripBotMention ¶
StripBotMention removes all @bot mentions from the message text.
Types ¶
type AgentDataDirFunc ¶
AgentDataDirFunc resolves an agent ID to its data directory path.
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot manages a single Slack Socket Mode connection for one agent.
func NewBot ¶
func NewBot(parentCtx context.Context, agentID string, agentDataDir string, cfg agent.SlackBotConfig, appToken, botToken string, mgr ChatManager, logger *slog.Logger, extraSlackOpts ...slack.Option) *Bot
NewBot creates a new Bot instance. Call Run() to start it. agentDataDir is the agent's data directory used for storing conversation history files. parentCtx controls the Bot's lifetime: cancelling it will stop the event loop.
func (*Bot) Done ¶
func (b *Bot) Done() <-chan struct{}
Done returns a channel that is closed when the bot exits.
type ChatManager ¶
type ChatManager interface {
Chat(ctx context.Context, agentID, message, role string, attachments []agent.MessageAttachment, source ...agent.BusySource) (<-chan agent.ChatEvent, error)
ChatOneShot(ctx context.Context, agentID, message string) (<-chan agent.ChatEvent, error)
}
ChatManager is the interface the bot uses to interact with agents. agent.Manager satisfies this interface directly — no adapter needed.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages all SlackBot instances across agents. All operations are serialized through a single event loop goroutine, eliminating lock juggling and TOCTOU races.
func NewHub ¶
func NewHub(mgr ChatManager, tokens TokenProvider, agentDataDir AgentDataDirFunc, logger *slog.Logger) *Hub
NewHub creates a new Hub and starts its event loop. Call Stop() on shutdown. agentDataDir resolves an agent ID to its data directory path for history file storage.
func (*Hub) Reconfigure ¶
func (h *Hub) Reconfigure(agentID string, cfg agent.SlackBotConfig)
Reconfigure stops and restarts the bot with new configuration. If the config is disabled, it only stops the bot.
func (*Hub) StartBot ¶
func (h *Hub) StartBot(agentID string, cfg agent.SlackBotConfig)
StartBot starts a Slack bot for the given agent. If one is already running, it is stopped first.
type TokenProvider ¶
type TokenProvider interface {
GetToken(provider, agentID, sourceID, key string) (string, error)
SetToken(provider, agentID, sourceID, key, value string, expiresAt time.Time) error
DeleteToken(provider, agentID, sourceID, key string) error
DeleteTokensBySource(provider, agentID, sourceID string) error
}
TokenProvider reads/writes encrypted Slack tokens from a credential store.
type UserResolver ¶
UserResolver resolves a Slack user ID to a display name. Return the original ID if resolution fails.