feat: implement IM channel management in agent editor

- Introduced a new IMChannelPanel component for managing WeCom and Feishu channels.
- Added CRUD operations for IM channels, including create, update, delete, and list functionalities.
- Enhanced the backend with new API endpoints for IM channel management.
- Updated documentation to reflect changes in IM integration and channel management.
- Improved localization support for new IM-related UI elements across multiple languages.
This commit is contained in:
wizardchen
2026-03-16 02:23:22 +08:00
parent e5a397faeb
commit 6159e8e3f7
18 changed files with 1571 additions and 518 deletions
-44
View File
@@ -27,50 +27,6 @@ type Config struct {
ExtractManager *ExtractManagerConfig `yaml:"extract" json:"extract"`
WebSearch *WebSearchConfig `yaml:"web_search" json:"web_search"`
PromptTemplates *PromptTemplatesConfig `yaml:"prompt_templates" json:"prompt_templates"`
IM *IMConfig `yaml:"im" json:"im"`
}
// IMConfig IM 集成配置
type IMConfig struct {
WeCom *WeComIMConfig `yaml:"wecom" json:"wecom"`
Feishu *FeishuIMConfig `yaml:"feishu" json:"feishu"`
}
// WeComIMConfig 企业微信配置
type WeComIMConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
TenantID uint64 `yaml:"tenant_id" json:"tenant_id"`
AgentID string `yaml:"agent_id" json:"agent_id"`
KnowledgeBases []string `yaml:"knowledge_base_ids" json:"knowledge_base_ids"`
// Mode: "webhook" (default, requires public domain) or "websocket" (long connection via intelligent bot, no public domain needed)
Mode string `yaml:"mode" json:"mode"`
// OutputMode: "stream" (default, real-time streaming) or "full" (wait for complete answer then send)
OutputMode string `yaml:"output_mode" json:"output_mode"`
// --- Webhook mode fields (self-built app callback) ---
CorpID string `yaml:"corp_id" json:"corp_id"`
AgentSecret string `yaml:"agent_secret" json:"agent_secret"`
Token string `yaml:"token" json:"token"`
EncodingAESKey string `yaml:"encoding_aes_key" json:"encoding_aes_key"`
CorpAgentID int `yaml:"corp_agent_id" json:"corp_agent_id"`
// --- WebSocket mode fields (intelligent bot long connection) ---
BotID string `yaml:"bot_id" json:"bot_id"`
BotSecret string `yaml:"bot_secret" json:"bot_secret"`
}
// FeishuIMConfig 飞书配置
type FeishuIMConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
TenantID uint64 `yaml:"tenant_id" json:"tenant_id"`
AgentID string `yaml:"agent_id" json:"agent_id"`
KnowledgeBases []string `yaml:"knowledge_base_ids" json:"knowledge_base_ids"`
AppID string `yaml:"app_id" json:"app_id"`
AppSecret string `yaml:"app_secret" json:"app_secret"`
VerificationToken string `yaml:"verification_token" json:"verification_token"`
EncryptKey string `yaml:"encrypt_key" json:"encrypt_key"`
// Mode: "websocket" (default, long connection, no public domain needed) or "webhook" (requires public domain)
Mode string `yaml:"mode" json:"mode"`
// OutputMode: "stream" (default, real-time streaming) or "full" (wait for complete answer then send)
OutputMode string `yaml:"output_mode" json:"output_mode"`
}
// DocReaderConfig configures the document parser client (gRPC or HTTP).
+118 -106
View File
@@ -6,6 +6,7 @@ package container
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/url"
"os"
@@ -242,7 +243,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
// IM integration
logger.Debugf(ctx, "[Container] Registering IM integration...")
must(container.Provide(imPkg.NewService))
must(container.Invoke(registerIMAdapters))
must(container.Invoke(registerIMAdapterFactories))
must(container.Provide(handler.NewIMHandler))
logger.Debugf(ctx, "[Container] HTTP handlers registered")
@@ -947,122 +948,133 @@ func registerWebSearchProviders(registry *web_search.Registry) {
})
}
// registerIMAdapters registers IM platform adapters based on configuration.
// For "websocket" mode, it also starts a long connection client in a goroutine.
func registerIMAdapters(cfg *config.Config, imService *imPkg.Service) {
if cfg.IM == nil {
logger.Infof(context.Background(), "[IM] No IM configuration found, skipping adapter registration")
return
}
// registerIMAdapterFactories registers adapter factories for each IM platform
// and loads enabled channels from the database.
func registerIMAdapterFactories(imService *imPkg.Service) {
ctx := context.Background()
// Register WeCom
if cfg.IM.WeCom != nil && cfg.IM.WeCom.Enabled {
registerWeComAdapter(ctx, cfg.IM.WeCom, imService)
if cfg.IM.WeCom.OutputMode == "full" {
imService.SetStreamDisabled(imPkg.PlatformWeCom, true)
logger.Infof(ctx, "[IM] WeCom streaming disabled (output_mode=full)")
}
}
// Register Feishu
if cfg.IM.Feishu != nil && cfg.IM.Feishu.Enabled {
registerFeishuAdapter(ctx, cfg.IM.Feishu, imService)
if cfg.IM.Feishu.OutputMode == "full" {
imService.SetStreamDisabled(imPkg.PlatformFeishu, true)
logger.Infof(ctx, "[IM] Feishu streaming disabled (output_mode=full)")
}
}
}
func registerWeComAdapter(ctx context.Context, cfg *config.WeComIMConfig, imService *imPkg.Service) {
mode := cfg.Mode
if mode == "" {
mode = "websocket"
}
switch mode {
case "webhook":
adapter, err := wecom.NewWebhookAdapter(
cfg.CorpID,
cfg.AgentSecret,
cfg.Token,
cfg.EncodingAESKey,
cfg.CorpAgentID,
)
// Register WeCom adapter factory
imService.RegisterAdapterFactory("wecom", func(factoryCtx context.Context, channel *imPkg.IMChannel, msgHandler func(context.Context, *imPkg.IncomingMessage) error) (imPkg.Adapter, context.CancelFunc, error) {
creds, err := parseCredentials(channel.Credentials)
if err != nil {
logger.Warnf(ctx, "[IM] Failed to create WeCom webhook adapter: %v", err)
return
}
imService.RegisterAdapter(adapter)
logger.Infof(ctx, "[IM] WeCom adapter registered (mode=webhook, corp_id=%s)", cfg.CorpID)
case "websocket":
// Build the message handler that delegates to imService.HandleMessage
handler := func(msgCtx context.Context, msg *imPkg.IncomingMessage) error {
return imService.HandleMessage(msgCtx, msg, cfg.TenantID, cfg.AgentID, cfg.KnowledgeBases)
return nil, nil, fmt.Errorf("parse wecom credentials: %w", err)
}
client := wecom.NewLongConnClient(cfg.BotID, cfg.BotSecret, handler)
mode := channel.Mode
if mode == "" {
mode = "websocket"
}
// Register a BotAdapter so the service can send replies via WebSocket
imService.RegisterAdapter(wecom.NewWSAdapter(client))
logger.Infof(ctx, "[IM] WeCom adapter registered (mode=websocket, bot_id=%s)", cfg.BotID)
// Start the long connection in a goroutine
go func() {
if err := client.Start(context.Background()); err != nil {
logger.Errorf(context.Background(), "[IM] WeCom long connection stopped: %v", err)
switch mode {
case "webhook":
corpAgentID := 0
if v, ok := creds["corp_agent_id"]; ok {
switch val := v.(type) {
case float64:
corpAgentID = int(val)
case int:
corpAgentID = val
}
}
}()
adapter, err := wecom.NewWebhookAdapter(
getString(creds, "corp_id"),
getString(creds, "agent_secret"),
getString(creds, "token"),
getString(creds, "encoding_aes_key"),
corpAgentID,
)
if err != nil {
return nil, nil, err
}
return adapter, nil, nil
default:
logger.Warnf(ctx, "[IM] Unknown WeCom mode: %s (expected 'webhook' or 'websocket')", mode)
case "websocket":
client := wecom.NewLongConnClient(
getString(creds, "bot_id"),
getString(creds, "bot_secret"),
msgHandler,
)
wsCtx, wsCancel := context.WithCancel(context.Background())
go func() {
if err := client.Start(wsCtx); err != nil && wsCtx.Err() == nil {
logger.Errorf(context.Background(), "[IM] WeCom long connection stopped for channel %s: %v", channel.ID, err)
}
}()
adapter := wecom.NewWSAdapter(client)
return adapter, wsCancel, nil
default:
return nil, nil, fmt.Errorf("unknown WeCom mode: %s", mode)
}
})
// Register Feishu adapter factory
imService.RegisterAdapterFactory("feishu", func(factoryCtx context.Context, channel *imPkg.IMChannel, msgHandler func(context.Context, *imPkg.IncomingMessage) error) (imPkg.Adapter, context.CancelFunc, error) {
creds, err := parseCredentials(channel.Credentials)
if err != nil {
return nil, nil, fmt.Errorf("parse feishu credentials: %w", err)
}
appID := getString(creds, "app_id")
appSecret := getString(creds, "app_secret")
verificationToken := getString(creds, "verification_token")
encryptKey := getString(creds, "encrypt_key")
// Always create the HTTP adapter (needed for SendReply in both modes)
adapter := feishu.NewAdapter(appID, appSecret, verificationToken, encryptKey)
mode := channel.Mode
if mode == "" {
mode = "websocket"
}
switch mode {
case "webhook":
return adapter, nil, nil
case "websocket":
client := feishu.NewLongConnClient(appID, appSecret, msgHandler)
wsCtx, wsCancel := context.WithCancel(context.Background())
go func() {
if err := client.Start(wsCtx); err != nil && wsCtx.Err() == nil {
logger.Errorf(context.Background(), "[IM] Feishu long connection stopped for channel %s: %v", channel.ID, err)
}
}()
return adapter, wsCancel, nil
default:
return nil, nil, fmt.Errorf("unknown Feishu mode: %s", mode)
}
})
// Load and start all enabled channels from database
if err := imService.LoadAndStartChannels(); err != nil {
logger.Warnf(ctx, "[IM] Failed to load channels from database: %v", err)
}
}
func registerFeishuAdapter(ctx context.Context, cfg *config.FeishuIMConfig, imService *imPkg.Service) {
mode := cfg.Mode
if mode == "" {
mode = "websocket"
// parseCredentials parses the JSONB credentials field into a map.
func parseCredentials(data []byte) (map[string]interface{}, error) {
if len(data) == 0 {
return map[string]interface{}{}, nil
}
// Always register the HTTP adapter (needed for SendReply in both modes)
adapter := feishu.NewAdapter(
cfg.AppID,
cfg.AppSecret,
cfg.VerificationToken,
cfg.EncryptKey,
)
imService.RegisterAdapter(adapter)
switch mode {
case "webhook":
logger.Infof(ctx, "[IM] Feishu adapter registered (mode=webhook, app_id=%s)", cfg.AppID)
case "websocket":
logger.Infof(ctx, "[IM] Feishu adapter registered (mode=websocket, app_id=%s)", cfg.AppID)
// Build the message handler
handler := func(msgCtx context.Context, msg *imPkg.IncomingMessage) error {
return imService.HandleMessage(msgCtx, msg, cfg.TenantID, cfg.AgentID, cfg.KnowledgeBases)
}
client := feishu.NewLongConnClient(
cfg.AppID,
cfg.AppSecret,
handler,
)
// Start the long connection in a goroutine
go func() {
if err := client.Start(context.Background()); err != nil {
logger.Errorf(context.Background(), "[IM] Feishu long connection stopped: %v", err)
}
}()
default:
logger.Warnf(ctx, "[IM] Unknown Feishu mode: %s (expected 'webhook' or 'websocket')", mode)
var creds map[string]interface{}
if err := json.Unmarshal(data, &creds); err != nil {
return nil, err
}
return creds, nil
}
// getString safely extracts a string value from a credentials map.
func getString(creds map[string]interface{}, key string) string {
if v, ok := creds[key]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
+208 -76
View File
@@ -4,45 +4,233 @@ import (
"context"
"net/http"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/im"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
)
// IMHandler handles IM platform callback requests.
// IMHandler handles IM platform callback requests and channel CRUD.
type IMHandler struct {
imService *im.Service
config *config.Config
}
// NewIMHandler creates a new IM handler.
func NewIMHandler(imService *im.Service, cfg *config.Config) *IMHandler {
func NewIMHandler(imService *im.Service) *IMHandler {
return &IMHandler{
imService: imService,
config: cfg,
}
}
// WeComCallback handles WeCom callback requests (both URL verification and message events).
func (h *IMHandler) WeComCallback(c *gin.Context) {
ctx := c.Request.Context()
// ── Channel CRUD handlers ──
adapter, ok := h.imService.GetAdapter(im.PlatformWeCom)
if !ok {
logger.Error(ctx, "[IM] WeCom adapter not registered")
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "WeCom integration not enabled"})
// CreateIMChannel creates a new IM channel for an agent.
func (h *IMHandler) CreateIMChannel(c *gin.Context) {
agentID := c.Param("id")
if agentID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "agent_id is required"})
return
}
// Handle URL verification (GET request)
tenantID, ok := c.Request.Context().Value(types.TenantIDContextKey).(uint64)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
var req struct {
Platform string `json:"platform" binding:"required"`
Name string `json:"name"`
Mode string `json:"mode"`
OutputMode string `json:"output_mode"`
Credentials types.JSON `json:"credentials"`
Enabled *bool `json:"enabled"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.Platform != "wecom" && req.Platform != "feishu" {
c.JSON(http.StatusBadRequest, gin.H{"error": "platform must be 'wecom' or 'feishu'"})
return
}
channel := &im.IMChannel{
TenantID: tenantID,
AgentID: agentID,
Platform: req.Platform,
Name: req.Name,
Mode: req.Mode,
OutputMode: req.OutputMode,
Credentials: req.Credentials,
Enabled: true,
}
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
if channel.Mode == "" {
channel.Mode = "websocket"
}
if channel.OutputMode == "" {
channel.OutputMode = "stream"
}
if channel.Credentials == nil {
channel.Credentials = types.JSON("{}")
}
if err := h.imService.CreateChannel(channel); err != nil {
logger.Errorf(c.Request.Context(), "[IM] Create channel failed: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create channel"})
return
}
c.JSON(http.StatusOK, gin.H{"data": channel})
}
// ListIMChannels lists all IM channels for an agent.
func (h *IMHandler) ListIMChannels(c *gin.Context) {
agentID := c.Param("id")
if agentID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "agent_id is required"})
return
}
channels, err := h.imService.ListChannelsByAgent(agentID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list channels"})
return
}
c.JSON(http.StatusOK, gin.H{"data": channels})
}
// UpdateIMChannel updates an IM channel.
func (h *IMHandler) UpdateIMChannel(c *gin.Context) {
channelID := c.Param("id")
if channelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "channel id is required"})
return
}
channel, err := h.imService.GetChannelByID(channelID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "channel not found"})
return
}
var req struct {
Name *string `json:"name"`
Mode *string `json:"mode"`
OutputMode *string `json:"output_mode"`
Credentials types.JSON `json:"credentials"`
Enabled *bool `json:"enabled"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.Name != nil {
channel.Name = *req.Name
}
if req.Mode != nil {
channel.Mode = *req.Mode
}
if req.OutputMode != nil {
channel.OutputMode = *req.OutputMode
}
if req.Credentials != nil {
channel.Credentials = req.Credentials
}
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
if err := h.imService.UpdateChannel(channel); err != nil {
logger.Errorf(c.Request.Context(), "[IM] Update channel failed: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update channel"})
return
}
c.JSON(http.StatusOK, gin.H{"data": channel})
}
// DeleteIMChannel deletes an IM channel.
func (h *IMHandler) DeleteIMChannel(c *gin.Context) {
channelID := c.Param("id")
if channelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "channel id is required"})
return
}
if err := h.imService.DeleteChannel(channelID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete channel"})
return
}
c.JSON(http.StatusOK, gin.H{"success": true})
}
// ToggleIMChannel toggles the enabled state of an IM channel.
func (h *IMHandler) ToggleIMChannel(c *gin.Context) {
channelID := c.Param("id")
if channelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "channel id is required"})
return
}
channel, err := h.imService.ToggleChannel(channelID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to toggle channel"})
return
}
c.JSON(http.StatusOK, gin.H{"data": channel})
}
// ── Callback handlers ──
// IMCallback handles IM platform callback requests for a specific channel.
// Route: POST /api/v1/im/callback/:channel_id
func (h *IMHandler) IMCallback(c *gin.Context) {
ctx := c.Request.Context()
channelID := c.Param("channel_id")
adapter, channel, ok := h.imService.GetChannelAdapter(channelID)
if !ok {
// Try loading from DB
ch, err := h.imService.GetChannelByID(channelID)
if err != nil {
logger.Errorf(ctx, "[IM] Channel not found for callback: %s", channelID)
c.JSON(http.StatusNotFound, gin.H{"error": "channel not found"})
return
}
if err := h.imService.StartChannel(ch); err != nil {
logger.Errorf(ctx, "[IM] Failed to start channel for callback: %v", err)
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "channel not available"})
return
}
adapter, channel, ok = h.imService.GetChannelAdapter(channelID)
if !ok {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "channel not available"})
return
}
}
if !channel.Enabled {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "channel is disabled"})
return
}
// Handle URL verification
if adapter.HandleURLVerification(c) {
return
}
// Verify callback signature
if err := adapter.VerifyCallback(c); err != nil {
logger.Errorf(ctx, "[IM] WeCom callback verification failed: %v", err)
logger.Errorf(ctx, "[IM] Callback verification failed for channel %s: %v", channelID, err)
c.JSON(http.StatusForbidden, gin.H{"error": "verification failed"})
return
}
@@ -50,83 +238,27 @@ func (h *IMHandler) WeComCallback(c *gin.Context) {
// Parse the callback message
msg, err := adapter.ParseCallback(c)
if err != nil {
logger.Errorf(ctx, "[IM] WeCom parse callback failed: %v", err)
logger.Errorf(ctx, "[IM] Parse callback failed for channel %s: %v", channelID, err)
c.JSON(http.StatusBadRequest, gin.H{"error": "parse failed"})
return
}
// If nil, it's a non-message event (e.g., system event) - just acknowledge
// If nil, it's a non-message event - just acknowledge
if msg == nil {
c.JSON(http.StatusOK, gin.H{"success": true})
return
}
// Respond immediately to avoid WeCom timeout, process asynchronously
// Respond immediately to avoid platform timeout
c.JSON(http.StatusOK, gin.H{"success": true})
// Get config for this platform
wecomCfg := h.config.IM.WeCom
// Detach from gin request context to prevent cancellation after HTTP response.
// Detach from gin request context
asyncCtx := context.WithoutCancel(ctx)
// Process message asynchronously
go func() {
if err := h.imService.HandleMessage(asyncCtx, msg, wecomCfg.TenantID, wecomCfg.AgentID, wecomCfg.KnowledgeBases); err != nil {
logger.Errorf(asyncCtx, "[IM] WeCom handle message error: %v", err)
}
}()
}
// FeishuCallback handles Feishu callback requests (both URL verification and message events).
func (h *IMHandler) FeishuCallback(c *gin.Context) {
ctx := c.Request.Context()
adapter, ok := h.imService.GetAdapter(im.PlatformFeishu)
if !ok {
logger.Error(ctx, "[IM] Feishu adapter not registered")
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Feishu integration not enabled"})
return
}
// Handle URL verification (challenge)
if adapter.HandleURLVerification(c) {
return
}
// Verify callback
if err := adapter.VerifyCallback(c); err != nil {
logger.Errorf(ctx, "[IM] Feishu callback verification failed: %v", err)
c.JSON(http.StatusForbidden, gin.H{"error": "verification failed"})
return
}
// Parse the callback message
msg, err := adapter.ParseCallback(c)
if err != nil {
logger.Errorf(ctx, "[IM] Feishu parse callback failed: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "parse failed"})
return
}
if msg == nil {
c.JSON(http.StatusOK, gin.H{"success": true})
return
}
// Respond immediately
c.JSON(http.StatusOK, gin.H{"success": true})
// Get config
feishuCfg := h.config.IM.Feishu
// Detach from gin request context to prevent cancellation after HTTP response.
asyncCtx := context.WithoutCancel(ctx)
// Process asynchronously
go func() {
if err := h.imService.HandleMessage(asyncCtx, msg, feishuCfg.TenantID, feishuCfg.AgentID, feishuCfg.KnowledgeBases); err != nil {
logger.Errorf(asyncCtx, "[IM] Feishu handle message error: %v", err)
if err := h.imService.HandleMessage(asyncCtx, msg, channelID); err != nil {
logger.Errorf(asyncCtx, "[IM] Handle message error for channel %s: %v", channelID, err)
}
}()
}
+240 -63
View File
@@ -2,7 +2,7 @@ package im
import (
"context"
"fmt"
"fmt"
"strings"
"sync"
"time"
@@ -29,6 +29,17 @@ const (
streamFlushInterval = 300 * time.Millisecond
)
// channelState holds runtime state for a running IM channel.
type channelState struct {
Channel *IMChannel
Adapter Adapter
Cancel context.CancelFunc // for stopping websocket goroutines
}
// AdapterFactory creates an Adapter from an IMChannel configuration.
// The second return value is an optional cleanup function (e.g., for stopping websocket connections).
type AdapterFactory func(ctx context.Context, channel *IMChannel, msgHandler func(ctx context.Context, msg *IncomingMessage) error) (Adapter, context.CancelFunc, error)
// Service orchestrates IM message handling:
// 1. Receives a unified IncomingMessage from an Adapter
// 2. Resolves or creates a WeKnora session for the IM channel
@@ -41,15 +52,16 @@ type Service struct {
tenantService interfaces.TenantService
agentService interfaces.CustomAgentService
adapters map[Platform]Adapter
// channels maps channel ID -> running channel state
channels map[string]*channelState
mu sync.RWMutex
// adapterFactories maps platform name -> factory function
adapterFactories map[string]AdapterFactory
// processedMsgs tracks recently processed message IDs to prevent duplicate handling.
processedMsgs sync.Map
// streamDisabled tracks platforms where streaming is explicitly disabled via output_mode config.
streamDisabled map[Platform]bool
stopCh chan struct{}
}
@@ -62,14 +74,14 @@ func NewService(
agentService interfaces.CustomAgentService,
) *Service {
s := &Service{
db: db,
sessionService: sessionService,
messageService: messageService,
tenantService: tenantService,
agentService: agentService,
adapters: make(map[Platform]Adapter),
streamDisabled: make(map[Platform]bool),
stopCh: make(chan struct{}),
db: db,
sessionService: sessionService,
messageService: messageService,
tenantService: tenantService,
agentService: agentService,
channels: make(map[string]*channelState),
adapterFactories: make(map[string]AdapterFactory),
stopCh: make(chan struct{}),
}
// Start periodic dedup cleanup instead of per-message goroutines
@@ -78,9 +90,24 @@ func NewService(
return s
}
// Stop gracefully shuts down the service, stopping background goroutines.
// RegisterAdapterFactory registers a factory for creating adapters for a given platform.
func (s *Service) RegisterAdapterFactory(platform string, factory AdapterFactory) {
s.mu.Lock()
defer s.mu.Unlock()
s.adapterFactories[platform] = factory
}
// Stop gracefully shuts down the service, stopping all channels and background goroutines.
func (s *Service) Stop() {
close(s.stopCh)
s.mu.Lock()
defer s.mu.Unlock()
for id, cs := range s.channels {
if cs.Cancel != nil {
cs.Cancel()
}
delete(s.channels, id)
}
}
// dedupCleanupLoop periodically cleans up expired entries from the dedup map.
@@ -103,33 +130,102 @@ func (s *Service) dedupCleanupLoop() {
}
}
// RegisterAdapter registers an IM platform adapter.
func (s *Service) RegisterAdapter(adapter Adapter) {
s.mu.Lock()
defer s.mu.Unlock()
s.adapters[adapter.Platform()] = adapter
// LoadAndStartChannels loads all enabled channels from the database and starts them.
func (s *Service) LoadAndStartChannels() error {
ctx := context.Background()
var channels []IMChannel
if err := s.db.Where("enabled = ? AND deleted_at IS NULL", true).Find(&channels).Error; err != nil {
return fmt.Errorf("load im channels: %w", err)
}
for i := range channels {
ch := channels[i]
if err := s.StartChannel(&ch); err != nil {
logger.Warnf(ctx, "[IM] Failed to start channel %s (%s/%s): %v", ch.ID, ch.Platform, ch.Name, err)
} else {
logger.Infof(ctx, "[IM] Started channel: id=%s platform=%s name=%s mode=%s agent=%s",
ch.ID, ch.Platform, ch.Name, ch.Mode, ch.AgentID)
}
}
logger.Infof(ctx, "[IM] Loaded %d enabled channels", len(channels))
return nil
}
// SetStreamDisabled disables streaming output for a specific platform.
// When disabled, the service will always collect the full answer before sending,
// even if the adapter implements StreamSender.
func (s *Service) SetStreamDisabled(platform Platform, disabled bool) {
// StartChannel creates and registers an adapter for the given channel.
func (s *Service) StartChannel(channel *IMChannel) error {
s.mu.Lock()
defer s.mu.Unlock()
s.streamDisabled[platform] = disabled
factory, ok := s.adapterFactories[channel.Platform]
if !ok {
s.mu.Unlock()
return fmt.Errorf("no adapter factory for platform: %s", channel.Platform)
}
// Stop existing channel if running
if existing, ok := s.channels[channel.ID]; ok {
if existing.Cancel != nil {
existing.Cancel()
}
delete(s.channels, channel.ID)
}
s.mu.Unlock()
// Build the message handler that delegates to HandleMessage with this channel's config
msgHandler := func(msgCtx context.Context, msg *IncomingMessage) error {
return s.HandleMessage(msgCtx, msg, channel.ID)
}
ctx := context.Background()
adapter, cancelFn, err := factory(ctx, channel, msgHandler)
if err != nil {
return fmt.Errorf("create adapter: %w", err)
}
s.mu.Lock()
s.channels[channel.ID] = &channelState{
Channel: channel,
Adapter: adapter,
Cancel: cancelFn,
}
s.mu.Unlock()
return nil
}
// GetAdapter returns the adapter for a given platform.
func (s *Service) GetAdapter(platform Platform) (Adapter, bool) {
// StopChannel stops and removes a running channel.
func (s *Service) StopChannel(channelID string) {
s.mu.Lock()
defer s.mu.Unlock()
if cs, ok := s.channels[channelID]; ok {
if cs.Cancel != nil {
cs.Cancel()
}
delete(s.channels, channelID)
logger.Infof(context.Background(), "[IM] Stopped channel: id=%s", channelID)
}
}
// GetChannelAdapter returns the adapter and channel config for a given channel ID.
func (s *Service) GetChannelAdapter(channelID string) (Adapter, *IMChannel, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
a, ok := s.adapters[platform]
return a, ok
cs, ok := s.channels[channelID]
if !ok {
return nil, nil, false
}
return cs.Adapter, cs.Channel, true
}
// HandleMessage processes an incoming IM message end-to-end:
// resolves session, runs QA, sends reply.
func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string, kbIDs []string) error {
// GetChannelByID loads a channel from the database.
func (s *Service) GetChannelByID(channelID string) (*IMChannel, error) {
var ch IMChannel
if err := s.db.Where("id = ? AND deleted_at IS NULL", channelID).First(&ch).Error; err != nil {
return nil, err
}
return &ch, nil
}
// HandleMessage processes an incoming IM message end-to-end using channel config.
func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, channelID string) error {
// Dedup: skip if this message was already processed (IM platforms may retry)
if msg.MessageID != "" {
if _, loaded := s.processedMsgs.LoadOrStore(msg.MessageID, time.Now()); loaded {
@@ -145,10 +241,31 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, tenan
msg.Content = string(contentRunes[:maxContentLength])
}
logger.Infof(ctx, "[IM] HandleMessage: platform=%s user=%s chat=%s content_len=%d",
msg.Platform, msg.UserID, msg.ChatID, len(msg.Content))
// Get channel config
adapter, channel, ok := s.GetChannelAdapter(channelID)
if !ok {
// Try loading from DB (channel might have been created after service start)
ch, err := s.GetChannelByID(channelID)
if err != nil {
return fmt.Errorf("channel not found: %s", channelID)
}
// Start it dynamically
if err := s.StartChannel(ch); err != nil {
return fmt.Errorf("start channel %s: %w", channelID, err)
}
adapter, channel, ok = s.GetChannelAdapter(channelID)
if !ok {
return fmt.Errorf("channel adapter not available after start: %s", channelID)
}
}
// 1. Get tenant (once, shared across resolve + QA)
tenantID := channel.TenantID
agentID := channel.AgentID
logger.Infof(ctx, "[IM] HandleMessage: channel=%s platform=%s user=%s chat=%s content_len=%d",
channelID, msg.Platform, msg.UserID, msg.ChatID, len(msg.Content))
// 1. Get tenant
tenant, err := s.tenantService.GetTenantByID(ctx, tenantID)
if err != nil {
return fmt.Errorf("get tenant: %w", err)
@@ -157,7 +274,7 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, tenan
sessionCtx = context.WithValue(sessionCtx, types.TenantInfoContextKey, tenant)
// 2. Resolve or create a WeKnora session
channelSession, err := s.resolveSession(sessionCtx, msg, tenantID, agentID)
channelSession, err := s.resolveSession(sessionCtx, msg, tenantID, agentID, channelID)
if err != nil {
return fmt.Errorf("resolve session: %w", err)
}
@@ -179,20 +296,17 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, tenan
}
}
// 5. Get the platform adapter
adapter, ok := s.GetAdapter(msg.Platform)
if !ok {
return fmt.Errorf("no adapter for platform: %s", msg.Platform)
// 5. Resolve knowledge base IDs from agent config
var kbIDs []string
if customAgent != nil {
kbIDs = customAgent.Config.KnowledgeBases
}
// 6. If the adapter supports streaming and streaming is not disabled, use streaming mode;
// otherwise collect full answer.
s.mu.RLock()
disabled := s.streamDisabled[msg.Platform]
s.mu.RUnlock()
if !disabled {
// 6. If the adapter supports streaming and output_mode is not "full", use streaming
streamDisabled := channel.OutputMode == "full"
if !streamDisabled {
if streamer, ok := adapter.(StreamSender); ok {
return s.handleMessageStream(sessionCtx, msg, session, customAgent, kbIDs, streamer)
return s.handleMessageStream(sessionCtx, msg, session, customAgent, kbIDs, streamer, adapter)
}
}
@@ -211,13 +325,14 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, tenan
return fmt.Errorf("send reply: %w", err)
}
logger.Infof(ctx, "[IM] Reply sent: platform=%s user=%s answer_len=%d", msg.Platform, msg.UserID, len(answer))
logger.Infof(ctx, "[IM] Reply sent: channel=%s platform=%s user=%s answer_len=%d",
channelID, msg.Platform, msg.UserID, len(answer))
return nil
}
// resolveSession finds or creates a ChannelSession for the given IM message.
// ctx must already carry TenantIDContextKey and TenantInfoContextKey.
func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string) (*ChannelSession, error) {
func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string, imChannelID string) (*ChannelSession, error) {
var cs ChannelSession
result := s.db.Where("platform = ? AND user_id = ? AND chat_id = ? AND tenant_id = ? AND deleted_at IS NULL",
string(msg.Platform), msg.UserID, msg.ChatID, tenantID).
@@ -251,12 +366,13 @@ func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tena
// Create the channel-session mapping; use a unique constraint fallback
// to handle concurrent creation attempts for the same channel.
cs = ChannelSession{
Platform: string(msg.Platform),
UserID: msg.UserID,
ChatID: msg.ChatID,
SessionID: createdSession.ID,
TenantID: tenantID,
AgentID: agentID,
Platform: string(msg.Platform),
UserID: msg.UserID,
ChatID: msg.ChatID,
SessionID: createdSession.ID,
TenantID: tenantID,
AgentID: agentID,
IMChannelID: imChannelID,
}
if err := s.db.Create(&cs).Error; err != nil {
// If the insert failed due to unique constraint (concurrent request),
@@ -279,12 +395,12 @@ func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tena
// handleMessageStream runs the QA pipeline and streams answer chunks to the IM platform
// in real-time via the StreamSender interface. Chunks are batched at streamFlushInterval
// to avoid API rate-limiting.
func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage, session *types.Session, customAgent *types.CustomAgent, kbIDs []string, streamer StreamSender) error {
func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage, session *types.Session, customAgent *types.CustomAgent, kbIDs []string, streamer StreamSender, adapter Adapter) error {
// Start the stream on the IM platform (e.g., create Feishu streaming card)
streamID, err := streamer.StartStream(ctx, msg)
if err != nil {
logger.Warnf(ctx, "[IM] StartStream failed, falling back to non-streaming: %v", err)
return s.fallbackNonStream(ctx, msg, session, customAgent, kbIDs)
return s.fallbackNonStream(ctx, msg, session, customAgent, kbIDs, adapter)
}
// Prepare the QA pipeline
@@ -457,18 +573,13 @@ loop:
}
// fallbackNonStream is used when streaming initialization fails.
func (s *Service) fallbackNonStream(ctx context.Context, msg *IncomingMessage, session *types.Session, customAgent *types.CustomAgent, kbIDs []string) error {
func (s *Service) fallbackNonStream(ctx context.Context, msg *IncomingMessage, session *types.Session, customAgent *types.CustomAgent, kbIDs []string, adapter Adapter) error {
answer, err := s.runQA(ctx, session, msg.Content, customAgent, kbIDs)
if err != nil {
logger.Errorf(ctx, "[IM] QA fallback failed: %v", err)
answer = "抱歉,处理您的问题时出现了异常,请稍后再试。"
}
adapter, ok := s.GetAdapter(msg.Platform)
if !ok {
return fmt.Errorf("no adapter for platform: %s", msg.Platform)
}
return adapter.SendReply(ctx, msg, &ReplyMessage{Content: answer, IsFinal: true})
}
@@ -599,3 +710,69 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
return answer, nil
}
// ── CRUD operations for IM channels ──
// ListChannelsByAgent returns all channels for a given agent.
func (s *Service) ListChannelsByAgent(agentID string) ([]IMChannel, error) {
var channels []IMChannel
if err := s.db.Where("agent_id = ? AND deleted_at IS NULL", agentID).
Order("created_at DESC").Find(&channels).Error; err != nil {
return nil, err
}
return channels, nil
}
// CreateChannel creates a new IM channel and optionally starts it.
func (s *Service) CreateChannel(channel *IMChannel) error {
if err := s.db.Create(channel).Error; err != nil {
return err
}
if channel.Enabled {
if err := s.StartChannel(channel); err != nil {
logger.Warnf(context.Background(), "[IM] Created channel %s but failed to start: %v", channel.ID, err)
}
}
return nil
}
// UpdateChannel updates a channel and restarts it if needed.
func (s *Service) UpdateChannel(channel *IMChannel) error {
if err := s.db.Save(channel).Error; err != nil {
return err
}
// Restart channel: stop old, start new if enabled
s.StopChannel(channel.ID)
if channel.Enabled {
if err := s.StartChannel(channel); err != nil {
logger.Warnf(context.Background(), "[IM] Updated channel %s but failed to restart: %v", channel.ID, err)
}
}
return nil
}
// DeleteChannel soft-deletes a channel and stops it.
func (s *Service) DeleteChannel(channelID string) error {
s.StopChannel(channelID)
return s.db.Where("id = ?", channelID).Delete(&IMChannel{}).Error
}
// ToggleChannel enables or disables a channel.
func (s *Service) ToggleChannel(channelID string) (*IMChannel, error) {
var ch IMChannel
if err := s.db.Where("id = ? AND deleted_at IS NULL", channelID).First(&ch).Error; err != nil {
return nil, err
}
ch.Enabled = !ch.Enabled
if err := s.db.Save(&ch).Error; err != nil {
return nil, err
}
if ch.Enabled {
if err := s.StartChannel(&ch); err != nil {
logger.Warnf(context.Background(), "[IM] Failed to start channel %s after enable: %v", ch.ID, err)
}
} else {
s.StopChannel(channelID)
}
return &ch, nil
}
+47 -12
View File
@@ -8,21 +8,56 @@ import (
"gorm.io/gorm"
)
// IMChannel represents an IM channel configuration stored in the database.
// Each channel binds to an agent and contains platform-specific credentials.
type IMChannel struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey;default:uuid_generate_v4()"`
TenantID uint64 `json:"tenant_id" gorm:"not null;index:idx_im_channels_tenant"`
AgentID string `json:"agent_id" gorm:"type:varchar(36);not null;index:idx_im_channels_agent"`
Platform string `json:"platform" gorm:"type:varchar(20);not null"`
Name string `json:"name" gorm:"type:varchar(255);not null;default:''"`
Enabled bool `json:"enabled" gorm:"not null;default:true"`
Mode string `json:"mode" gorm:"type:varchar(20);not null;default:'websocket'"`
OutputMode string `json:"output_mode" gorm:"type:varchar(20);not null;default:'stream'"`
Credentials types.JSON `json:"credentials" gorm:"type:jsonb;not null;default:'{}'"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}
func (IMChannel) TableName() string {
return "im_channels"
}
func (ch *IMChannel) BeforeCreate(tx *gorm.DB) error {
if ch.ID == "" {
ch.ID = uuid.New().String()
}
if ch.Mode == "" {
ch.Mode = "websocket"
}
if ch.OutputMode == "" {
ch.OutputMode = "stream"
}
return nil
}
// ChannelSession maps an IM channel (user+chat combination) to a WeKnora session.
// This allows the IM integration to maintain conversation continuity.
type ChannelSession struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey;default:uuid_generate_v4()"`
Platform string `json:"platform" gorm:"type:varchar(20);not null"`
UserID string `json:"user_id" gorm:"type:varchar(128);not null"`
ChatID string `json:"chat_id" gorm:"type:varchar(128);not null;default:''"`
SessionID string `json:"session_id" gorm:"type:varchar(36);not null;index"`
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
AgentID string `json:"agent_id" gorm:"type:varchar(36);default:''"`
Status string `json:"status" gorm:"type:varchar(20);not null;default:'active'"`
Metadata types.JSON `json:"metadata" gorm:"type:jsonb;default:'{}'"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
ID string `json:"id" gorm:"type:varchar(36);primaryKey;default:uuid_generate_v4()"`
Platform string `json:"platform" gorm:"type:varchar(20);not null"`
UserID string `json:"user_id" gorm:"type:varchar(128);not null"`
ChatID string `json:"chat_id" gorm:"type:varchar(128);not null;default:''"`
SessionID string `json:"session_id" gorm:"type:varchar(36);not null;index"`
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
AgentID string `json:"agent_id" gorm:"type:varchar(36);default:''"`
IMChannelID string `json:"im_channel_id" gorm:"type:varchar(36);default:''"`
Status string `json:"status" gorm:"type:varchar(20);not null;default:'active'"`
Metadata types.JSON `json:"metadata" gorm:"type:jsonb;default:'{}'"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}
func (ChannelSession) TableName() string {
+21 -5
View File
@@ -138,6 +138,7 @@ func NewRouter(params RouterParams) *gin.Engine {
RegisterCustomAgentRoutes(v1, params.CustomAgentHandler)
RegisterSkillRoutes(v1, params.SkillHandler)
RegisterOrganizationRoutes(v1, params.OrganizationHandler)
RegisterIMChannelRoutes(v1, params.IMHandler)
}
return r
@@ -586,11 +587,26 @@ func RegisterOrganizationRoutes(r *gin.RouterGroup, orgHandler *handler.Organiza
func RegisterIMRoutes(r *gin.Engine, imHandler *handler.IMHandler) {
im := r.Group("/api/v1/im")
{
// WeCom callback (supports both GET for URL verification and POST for message events)
im.GET("/callback/wecom", imHandler.WeComCallback)
im.POST("/callback/wecom", imHandler.WeComCallback)
// Feishu callback (POST for both URL verification challenge and message events)
im.POST("/callback/feishu", imHandler.FeishuCallback)
im.GET("/callback/:channel_id", imHandler.IMCallback)
im.POST("/callback/:channel_id", imHandler.IMCallback)
}
}
// RegisterIMChannelRoutes registers IM channel CRUD routes (requires authentication).
func RegisterIMChannelRoutes(r *gin.RouterGroup, imHandler *handler.IMHandler) {
// Channel CRUD under agents
agentChannels := r.Group("/agents/:id/im-channels")
{
agentChannels.POST("", imHandler.CreateIMChannel)
agentChannels.GET("", imHandler.ListIMChannels)
}
// Channel operations by channel ID
channels := r.Group("/im-channels")
{
channels.PUT("/:id", imHandler.UpdateIMChannel)
channels.DELETE("/:id", imHandler.DeleteIMChannel)
channels.POST("/:id/toggle", imHandler.ToggleIMChannel)
}
}