Files
upstream-mattermost/server/channels/app/agents_bridge.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

248 lines
7.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"github.com/blang/semver/v4"
agentclient "github.com/mattermost/mattermost-plugin-ai/public/bridgeclient"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
)
const (
aiPluginID = "mattermost-ai"
minAIPluginVersionForBridge = "1.5.0"
)
type BridgeOperation string
const (
BridgeOperationAutoTranslate BridgeOperation = "auto_translate"
BridgeOperationRecapSummary BridgeOperation = "recap_summary"
BridgeOperationRewrite BridgeOperation = "rewrite"
)
type BridgeMessage struct {
Role string
Message string
FileIDs []string
}
type BridgeCompletionRequest struct {
Operation BridgeOperation
ClientOperation string
OperationSubType string
Messages []BridgeMessage
JSONOutputFormat map[string]any
UserID string
ChannelID string
}
type AgentsBridge interface {
Status(rctx request.CTX) (bool, string)
GetAgents(sessionUserID, userID string) ([]model.BridgeAgentInfo, error)
GetServices(sessionUserID, userID string) ([]model.BridgeServiceInfo, error)
AgentCompletion(sessionUserID, agentID string, req BridgeCompletionRequest) (string, error)
ServiceCompletion(sessionUserID, serviceID string, req BridgeCompletionRequest) (string, error)
}
type liveAgentsBridge struct {
ch *Channels
}
func newLiveAgentsBridge(ch *Channels) AgentsBridge {
return &liveAgentsBridge{ch: ch}
}
func (b *liveAgentsBridge) Status(rctx request.CTX) (bool, string) {
return b.getLiveStatus(rctx)
}
func (b *liveAgentsBridge) GetAgents(sessionUserID, userID string) ([]model.BridgeAgentInfo, error) {
if available, _ := b.getLiveStatus(request.EmptyContext(b.ch.srv.Log())); !available {
return []model.BridgeAgentInfo{}, nil
}
client := agentclient.NewClientFromApp(New(ServerConnector(b.ch)), sessionUserID)
agents, err := client.GetAgents(userID)
if err != nil {
return nil, err
}
return toModelBridgeAgents(agents), nil
}
func (b *liveAgentsBridge) GetServices(sessionUserID, userID string) ([]model.BridgeServiceInfo, error) {
if available, _ := b.getLiveStatus(request.EmptyContext(b.ch.srv.Log())); !available {
return []model.BridgeServiceInfo{}, nil
}
client := agentclient.NewClientFromApp(New(ServerConnector(b.ch)), sessionUserID)
services, err := client.GetServices(userID)
if err != nil {
return nil, err
}
return toModelBridgeServices(services), nil
}
func (b *liveAgentsBridge) AgentCompletion(sessionUserID, agentID string, req BridgeCompletionRequest) (string, error) {
client := agentclient.NewClientFromApp(New(ServerConnector(b.ch)), sessionUserID)
return client.AgentCompletion(agentID, toClientCompletionRequest(req))
}
func (b *liveAgentsBridge) ServiceCompletion(sessionUserID, serviceID string, req BridgeCompletionRequest) (string, error) {
client := agentclient.NewClientFromApp(New(ServerConnector(b.ch)), sessionUserID)
return client.ServiceCompletion(serviceID, toClientCompletionRequest(req))
}
func (b *liveAgentsBridge) getLiveStatus(rctx request.CTX) (bool, string) {
pluginsEnvironment := b.ch.GetPluginsEnvironment()
if pluginsEnvironment == nil {
rctx.Logger().Debug("AI plugin bridge not available - plugin environment not initialized")
return false, "app.agents.bridge.not_available.plugin_env_not_initialized"
}
if !pluginsEnvironment.IsActive(aiPluginID) {
rctx.Logger().Debug("AI plugin bridge not available - plugin is not active or not installed",
mlog.String("plugin_id", aiPluginID),
)
return false, "app.agents.bridge.not_available.plugin_not_active"
}
plugins := pluginsEnvironment.Active()
for _, plugin := range plugins {
if plugin.Manifest == nil || plugin.Manifest.Id != aiPluginID {
continue
}
pluginVersion, err := semver.Parse(plugin.Manifest.Version)
if err != nil {
rctx.Logger().Debug("AI plugin bridge not available - failed to parse plugin version",
mlog.String("plugin_id", aiPluginID),
mlog.String("version", plugin.Manifest.Version),
mlog.Err(err),
)
return false, "app.agents.bridge.not_available.plugin_version_parse_failed"
}
minVersion, err := semver.Parse(minAIPluginVersionForBridge)
if err != nil {
return false, "app.agents.bridge.not_available.min_version_parse_failed"
}
if pluginVersion.LT(minVersion) {
rctx.Logger().Debug("AI plugin bridge not available - plugin version is too old",
mlog.String("plugin_id", aiPluginID),
mlog.String("current_version", plugin.Manifest.Version),
mlog.String("minimum_version", minAIPluginVersionForBridge),
)
return false, "app.agents.bridge.not_available.plugin_version_too_old"
}
return true, ""
}
return false, "app.agents.bridge.not_available.plugin_not_registered"
}
func toModelBridgeAgents(agents []agentclient.BridgeAgentInfo) []model.BridgeAgentInfo {
if len(agents) == 0 {
return []model.BridgeAgentInfo{}
}
converted := make([]model.BridgeAgentInfo, 0, len(agents))
for _, agent := range agents {
converted = append(converted, model.BridgeAgentInfo{
ID: agent.ID,
DisplayName: agent.DisplayName,
Username: agent.Username,
ServiceID: agent.ServiceID,
ServiceType: agent.ServiceType,
IsDefault: agent.IsDefault,
})
}
return converted
}
func toModelBridgeServices(services []agentclient.BridgeServiceInfo) []model.BridgeServiceInfo {
if len(services) == 0 {
return []model.BridgeServiceInfo{}
}
converted := make([]model.BridgeServiceInfo, 0, len(services))
for _, service := range services {
converted = append(converted, model.BridgeServiceInfo{
ID: service.ID,
Name: service.Name,
Type: service.Type,
})
}
return converted
}
func toBridgeClientPosts(messages []BridgeMessage) []agentclient.Post {
posts := make([]agentclient.Post, 0, len(messages))
for _, message := range messages {
posts = append(posts, agentclient.Post{
Role: message.Role,
Message: message.Message,
FileIDs: append([]string(nil), message.FileIDs...),
})
}
return posts
}
func toClientCompletionRequest(req BridgeCompletionRequest) agentclient.CompletionRequest {
return agentclient.CompletionRequest{
Posts: toBridgeClientPosts(req.Messages),
JSONOutputFormat: cloneJSONOutputFormat(req.JSONOutputFormat),
UserID: req.UserID,
ChannelID: req.ChannelID,
Operation: req.ClientOperation,
OperationSubType: req.OperationSubType,
}
}
func cloneJSONOutputFormat(jsonOutputFormat map[string]any) map[string]any {
if jsonOutputFormat == nil {
return nil
}
cloned := make(map[string]any, len(jsonOutputFormat))
for key, value := range jsonOutputFormat {
cloned[key] = cloneJSONValue(value)
}
return cloned
}
func cloneJSONValue(value any) any {
switch v := value.(type) {
case map[string]any:
cloned := make(map[string]any, len(v))
for key, child := range v {
cloned[key] = cloneJSONValue(child)
}
return cloned
case []any:
cloned := make([]any, len(v))
for i, child := range v {
cloned[i] = cloneJSONValue(child)
}
return cloned
case []string:
return append([]string(nil), v...)
default:
return v
}
}
var _ AgentsBridge = (*liveAgentsBridge)(nil)