bots

package
v2.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 26 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUsageRestriction = errors.New("usage restriction")

Functions

func ResolveAgentUsernames added in v2.11.0

func ResolveAgentUsernames(resolver UsernameResolver, usernames []string) (resolved map[string]string, unresolved []string)

ResolveAgentUsernames maps each legacy username reference to the bot user ID of the agent whose account currently holds it, and returns the usernames no agent holds. It reads bot accounts through the Mattermost API, so callers must not hold a database transaction open around it.

func ResolveBridgeFallbacks added in v2.8.0

func ResolveBridgeFallbacks(services []llm.ServiceConfig, primary llm.ServiceConfig) ([]llm.ServiceConfig, error)

ResolveBridgeFallbacks resolves the complete fallback chain of primary from the given service snapshot, and verifies that every fallback is itself able to serve a completion. It is kept separate from ServiceCanServeCompletions so a broken chain surfaces as a descriptive error instead of silently making the primary ineligible. llm.ResolveFallbackChain already rejects a member that fails llm.IsValidService, so the checks below only have to cover what ServiceCanServeCompletions adds on top of it.

primary is expected to come from services, so the whole chain resolves from one consistent view. Duplicate service IDs in the snapshot resolve to the first entry, matching how the configuration itself is read.

func ServiceCanServeCompletions added in v2.8.0

func ServiceCanServeCompletions(svc llm.ServiceConfig) bool

ServiceCanServeCompletions reports whether completions can be run against this service directly, without an agent in front of it.

The service path has no agent to supply a model override, so a service without a default model cannot serve a completion at all. `scale` passes llm.IsValidService but has no Bifrost provider mapping, so it is excluded here even though it remains a valid stored configuration.

func TeamMemberActive added in v2.10.0

func TeamMemberActive(client *pluginapi.Client, teamID, userID string) (bool, error)

TeamMemberActive reports whether userID has a non-deleted membership in teamID. A missing membership is reported as false rather than an error.

func UsageRestrictionsForUserConfig

func UsageRestrictionsForUserConfig(client *pluginapi.Client, cfg llm.BotConfig, requestingUserID string) error

UsageRestrictionsForUserConfig returns nil if userID is allowed by cfg's UserAccessLevel / UserIDs / TeamIDs, otherwise an error wrapping ErrUsageRestriction. This is the shared source of truth for user-scope access checks. Callers without an MMBots instance (e.g. API code when bots may be nil) should use this with the plugin client; MMBots.CheckUsageRestrictionsForUserConfig delegates here.

Types

type AgentStore

type AgentStore interface {
	ListAgents() ([]*llm.BotConfig, error)
}

AgentStore provides read access to user-created agents from the database. This is a subset of the full store.AgentStore — only read methods needed here.

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot represents an AI bot instance with its configuration and dependencies.

Source of truth for bot fields:

  • cfg: The bot's configuration (name, display name, permissions, etc.)
  • service: The RESOLVED service configuration (use GetService() to access). DO NOT use cfg.Service or cfg.ServiceID directly - those are internal references.
  • mmBot: The Mattermost bot user
  • llm: The initialized language model instance
  • providerServices: resolved from the concrete client before wrapping (see llm.ProviderServices)

Bot instances should be created via EnsureBots() which properly resolves service references and initializes all fields.

func NewBot

func NewBot(cfg llm.BotConfig, service llm.ServiceConfig, mmBot *model.Bot, llmInstance llm.LanguageModel) *Bot

NewBot has no provider services. Use EnsureBots for a wired bot, or WithConfig to derive one.

func (*Bot) BotUserID added in v2.7.0

func (b *Bot) BotUserID() string

BotUserID returns the Mattermost user ID of the bot's user account, or "" when no bot user exists.

func (*Bot) CanAttachSandboxFiles added in v2.11.0

func (b *Bot) CanAttachSandboxFiles() bool

CanAttachSandboxFiles gates the attach path: the sandbox is enabled and some service in the fallback chain can serve its files. The attach path takes whatever files the hop that served the turn produced.

func (*Bot) GetConfig

func (b *Bot) GetConfig() llm.BotConfig

func (*Bot) GetMMBot

func (b *Bot) GetMMBot() *model.Bot

func (*Bot) GetService

func (b *Bot) GetService() llm.ServiceConfig

func (*Bot) HasNativeCodeExecutionEnabled added in v2.7.0

func (b *Bot) HasNativeCodeExecutionEnabled() bool

HasNativeCodeExecutionEnabled is independent of file retrieval. OpenAI can run the sandbox but cannot serve its files; use CanAttachSandboxFiles and ShouldPromiseSandboxFiles for attach and prompt gating.

func (*Bot) HasNativeWebSearchEnabled

func (b *Bot) HasNativeWebSearchEnabled() bool

HasNativeWebSearchEnabled is used to suppress Mattermost's built-in web search fallback.

func (*Bot) LLM

func (b *Bot) LLM() llm.LanguageModel

func (*Bot) ProviderServices added in v2.7.0

func (b *Bot) ProviderServices() *llm.ProviderServices

ProviderServices is nil-safe; callers must tolerate nil (bots built outside EnsureBots).

func (*Bot) SetLLMForTest

func (b *Bot) SetLLMForTest(llm llm.LanguageModel)

func (*Bot) SetProviderServicesForTest added in v2.7.0

func (b *Bot) SetProviderServicesForTest(services *llm.ProviderServices)

func (*Bot) SetServiceForTest

func (b *Bot) SetServiceForTest(service llm.ServiceConfig)

func (*Bot) ShouldPromiseSandboxFiles added in v2.11.0

func (b *Bot) ShouldPromiseSandboxFiles() bool

ShouldPromiseSandboxFiles gates the prompt text telling the model that sandbox output files are attached automatically. The prompt may only promise what the primary can deliver: the primary serves most turns, so a fallback that can serve files must not make every turn promise files that never arrive.

func (*Bot) WithConfig added in v2.7.0

func (b *Bot) WithConfig(cfg llm.BotConfig) *Bot

WithConfig copies the bot with a new config. Use this instead of NewBot so resolved LLM and provider services are not silently dropped.

type Config

type Config interface {
	GetBots() []llm.BotConfig
	GetServiceByID(id string) (llm.ServiceConfig, bool)
	GetServices() []llm.ServiceConfig
	GetDefaultBotName() string
	EnableTokenUsageLogging() bool
	EnableTokenUsageLogToPlugin() bool
	EnableTokenUsageLogToFile() bool
	GetTranscriptGenerator() string
	GetTranscriptBotUserID() string
}

type MMBots

type MMBots struct {
	// contains filtered or unexported fields
}

func New

func New(mutexPluginAPI cluster.MutexPluginAPI, pluginAPI *pluginapi.Client, licenseChecker *enterprise.LicenseChecker, config Config, agentStore AgentStore, accessChecker *accesscontrol.Checker, llmUpstreamHTTPClient *http.Client, metrics llm.MetricsObserver) *MMBots

New builds the bot registry. accessChecker must be non-nil; tests that want no-policies-anywhere pass accesscontrol.New with PassthroughClient.

func (*MMBots) AcquireServiceLLM added in v2.8.0

func (b *MMBots) AcquireServiceLLM(svc llm.ServiceConfig, fallbacks []llm.ServiceConfig) (llm.LanguageModel, func(), error)

AcquireServiceLLM returns a language model for direct (agent-less) calls against svc. The caller must invoke release when the request is done; the model may be shut down shortly afterwards if the configuration changed in the meantime.

func (*MMBots) CheckUsageRestrictions

func (b *MMBots) CheckUsageRestrictions(ctx context.Context, requestingUserID string, bot *Bot, channel *model.Channel) error

func (*MMBots) CheckUsageRestrictionsForChannel added in v2.6.0

func (b *MMBots) CheckUsageRestrictionsForChannel(bot *Bot, channel *model.Channel) error

CheckUsageRestrictionsForChannel returns nil when the bot's ChannelAccessLevel configuration allows use in the given channel, otherwise an error wrapping ErrUsageRestriction. Unlike CheckUsageRestrictions it does not consider user-scope restrictions, so callers can validate channel-level settings (e.g. auto-reply) independent of any requesting user.

func (*MMBots) CheckUsageRestrictionsForUser

func (b *MMBots) CheckUsageRestrictionsForUser(ctx context.Context, bot *Bot, requestingUserID string) error

func (*MMBots) CheckUsageRestrictionsForUserConfig

func (b *MMBots) CheckUsageRestrictionsForUserConfig(ctx context.Context, cfg llm.BotConfig, requestingUserID string) error

CheckUsageRestrictionsForUserConfig is the composite per-request user gate: ABAC agent decision (legacy UserAccessLevel as legacyCheck), then ABAC service for cfg.ServiceID. Denied fallback hops are dropped per request rather than hiding the agent; see allowedFallbackServiceIDs.

func (*MMBots) EnsureBots

func (b *MMBots) EnsureBots() error

func (*MMBots) ForceRefreshOnNextEnsure

func (b *MMBots) ForceRefreshOnNextEnsure()

ForceRefreshOnNextEnsure clears the optimistic ensure snapshot and sets forceRefresh so the next EnsureBots() cannot take the fast path. DB-backed agents are not part of the config-file bot slice used for botConfigsEqual, so we must invalidate when agents change.

func (*MMBots) GetAllBotUserIDs

func (b *MMBots) GetAllBotUserIDs() []string

GetAllBotUserIDs returns a list of all bot user IDs

func (*MMBots) GetAllBots

func (b *MMBots) GetAllBots() []*Bot

GetAllBots returns all bots

func (*MMBots) GetBotByID

func (b *MMBots) GetBotByID(botID string) *Bot

GetBotByID retrieves the bot associated with the given bot ID

func (*MMBots) GetBotByIDOrUsername added in v2.11.0

func (b *MMBots) GetBotByIDOrUsername(botUserID, legacyUsername string) *Bot

GetBotByIDOrUsername resolves a stored or requested agent reference: by its bot user ID when one is set, otherwise by the legacy username, which only selects the agent whose account currently holds it. A missing agent returns nil rather than another agent, which would run with that agent's model, instructions, tools, and identity.

func (*MMBots) GetBotByUsername

func (b *MMBots) GetBotByUsername(botUsername string) *Bot

GetBotByUsername retrieves the bot whose Mattermost account currently holds the given username. A name the registry does not know is resolved through the bot account that holds it, so an agent stays reachable under its new name while the registry still lists it under the old one after a rename.

func (*MMBots) GetBotByUsernameOrFirst

func (b *MMBots) GetBotByUsernameOrFirst(botUsername string) *Bot

GetBotByUsernameOrFirst retrieves the bot with the given username, or the first bot when no username is given. A named bot that is not available returns nil: serving another agent instead would run the request with that agent's model, instructions, tools, and identity.

func (*MMBots) GetBotConfigByID

func (b *MMBots) GetBotConfigByID(botID string) (bool, int64, bool)

GetBotConfigByID returns the bot's EnableVision and MaxFileSize. ok is false when botID is unknown.

func (*MMBots) GetBotForDMChannel

func (b *MMBots) GetBotForDMChannel(channel *model.Channel) *Bot

GetBotForDMChannel returns the bot for the given DM channel.

func (*MMBots) GetBotMentioned

func (b *MMBots) GetBotMentioned(text string) *Bot

GetBotMentioned returns the bot mentioned in the text, if any. A bot is mentioned only under a username its Mattermost account currently holds.

func (*MMBots) GetTranscribe

func (b *MMBots) GetTranscribe() Transcriber

TODO: This really doesn't belong here. Figure out where to put this.

func (*MMBots) IsAnyBot

func (b *MMBots) IsAnyBot(userID string) bool

IsAnyBot returns true if the given user is an AI bot.

func (*MMBots) ReconcileServiceLLMs added in v2.8.0

func (b *MMBots) ReconcileServiceLLMs(services []llm.ServiceConfig)

ReconcileServiceLLMs drops cached models whose service or fallback chain changed or disappeared. Entries still in use are retired and shut down once their last lease is released.

func (*MMBots) RefreshAgent added in v2.10.0

func (b *MMBots) RefreshAgent(agentID string) error

RefreshAgent rebuilds the registry entry of one DB-backed agent from the store and swaps in just that entry, so a renamed agent is listed under its new name and account without waiting for a full rebuild. When the agent cannot be rebuilt, or is no longer active, its entry is removed instead. Name-based lookups do not depend on it having run: they check the live account. It never creates bot accounts (EnsureBots does, under the cluster mutex), and it leaves the next EnsureBots to reconcile every other bot.

func (*MMBots) SetBaseLLMBuilderForTest added in v2.8.0

func (b *MMBots) SetBaseLLMBuilderForTest(builder func(svc llm.ServiceConfig, fallbacks []llm.ServiceConfig) (llm.LanguageModel, func(), error))

SetBaseLLMBuilderForTest installs a test-only provider client builder. The wrapper chain around it stays the production one.

func (*MMBots) SetBotsForTesting

func (b *MMBots) SetBotsForTesting(bots []*Bot)

SetBotsForTesting sets bots directly for testing purposes only

func (*MMBots) ShutdownServiceLLMs added in v2.8.0

func (b *MMBots) ShutdownServiceLLMs()

ShutdownServiceLLMs shuts every provider client down, cached or retired, regardless of outstanding leases. Agents use these clients too. Called on plugin deactivation.

type Transcriber

type Transcriber interface {
	Transcribe(file io.Reader) (*subtitles.Subtitles, error)
}

Transcriber interface defines the contract for transcription services

type UsernameResolver added in v2.11.0

type UsernameResolver interface {
	GetBotByUsername(username string) *Bot
}

UsernameResolver selects the agent whose account currently holds a username.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL