feat(integrations): polish embed/IM channel editors and agent rebind
Align embed channel setup with a step wizard, simplify deploy layout, and allow changing bound agents on update for embed and IM channels.
This commit is contained in:
@@ -177,6 +177,12 @@ func (s *embedChannelService) Update(
|
||||
ch.AllowedOrigins = req.AllowedOrigins
|
||||
}
|
||||
}
|
||||
if trimmed := strings.TrimSpace(req.AgentID); trimmed != "" && trimmed != ch.AgentID {
|
||||
if _, err := s.ensureAgentOwned(ctx, tenantID, trimmed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ch.AgentID = trimmed
|
||||
}
|
||||
if err := s.repo.Update(ctx, ch); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
type stubEmbedChannelRepo struct {
|
||||
interfaces.EmbedChannelRepository
|
||||
ch *types.EmbedChannel
|
||||
}
|
||||
|
||||
func (r *stubEmbedChannelRepo) GetByID(_ context.Context, id string) (*types.EmbedChannel, error) {
|
||||
if r.ch == nil || r.ch.ID != id {
|
||||
return nil, nil
|
||||
}
|
||||
cp := *r.ch
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
func (r *stubEmbedChannelRepo) Update(_ context.Context, ch *types.EmbedChannel) error {
|
||||
cp := *ch
|
||||
r.ch = &cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestEmbedChannelUpdateAgentID(t *testing.T) {
|
||||
repo := &stubEmbedChannelRepo{
|
||||
ch: &types.EmbedChannel{
|
||||
ID: "ch-1",
|
||||
TenantID: 42,
|
||||
AgentID: "agent-old",
|
||||
Name: "Support",
|
||||
},
|
||||
}
|
||||
svc := &embedChannelService{
|
||||
repo: repo,
|
||||
agentService: &stubAgentForEmbed{
|
||||
agent: &types.CustomAgent{ID: "agent-new", TenantID: 42},
|
||||
},
|
||||
}
|
||||
enabled := true
|
||||
updated, err := svc.Update(
|
||||
context.Background(),
|
||||
42,
|
||||
"ch-1",
|
||||
&types.EmbedChannel{AgentID: "agent-new"},
|
||||
&enabled, nil, nil, nil, nil, nil, nil, nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
if updated.AgentID != "agent-new" {
|
||||
t.Fatalf("updated.AgentID = %q, want agent-new", updated.AgentID)
|
||||
}
|
||||
if repo.ch.AgentID != "agent-new" {
|
||||
t.Fatalf("persisted AgentID = %q, want agent-new", repo.ch.AgentID)
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ type embedChannelRequest struct {
|
||||
DefaultLocale *string `json:"default_locale"`
|
||||
WebhookURL *string `json:"webhook_url"`
|
||||
WebhookSecret *string `json:"webhook_secret"`
|
||||
AgentID *string `json:"agent_id"`
|
||||
}
|
||||
|
||||
// isProductionMode reports whether the server runs in a hardened (release) mode.
|
||||
@@ -242,6 +243,9 @@ func (h *EmbedChannelHandler) UpdateEmbedChannel(c *gin.Context) {
|
||||
HeaderTitleMode: req.HeaderTitleMode,
|
||||
WidgetPosition: req.WidgetPosition,
|
||||
}
|
||||
if req.AgentID != nil {
|
||||
update.AgentID = strings.TrimSpace(*req.AgentID)
|
||||
}
|
||||
ch, err := h.embedSvc.Update(c.Request.Context(), tenantID, channelID, update, req.Enabled, req.ShowSuggestedQuestions, req.AllowWebSearch, req.AllowMemory, req.AllowFileUpload, req.DefaultLocale, req.WebhookURL, req.WebhookSecret)
|
||||
if err != nil {
|
||||
writeEmbedMgmtError(c, err)
|
||||
|
||||
@@ -196,6 +196,7 @@ func (h *IMHandler) UpdateIMChannel(c *gin.Context) {
|
||||
KnowledgeBaseID *string `json:"knowledge_base_id"`
|
||||
Credentials types.JSON `json:"credentials"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
AgentID *string `json:"agent_id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -220,6 +221,16 @@ func (h *IMHandler) UpdateIMChannel(c *gin.Context) {
|
||||
if req.Enabled != nil {
|
||||
channel.Enabled = *req.Enabled
|
||||
}
|
||||
if req.AgentID != nil {
|
||||
newAgentID := strings.TrimSpace(*req.AgentID)
|
||||
if newAgentID != "" && newAgentID != channel.AgentID {
|
||||
if err := h.imService.SetChannelAgentID(c.Request.Context(), channel, newAgentID); err != nil {
|
||||
logger.Errorf(c.Request.Context(), "[IM] Update channel agent failed: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "agent not found"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.imService.UpdateChannel(channel); err != nil {
|
||||
logger.Errorf(c.Request.Context(), "[IM] Update channel failed: %v", err)
|
||||
|
||||
@@ -2501,6 +2501,23 @@ func (s *Service) CreateChannel(channel *IMChannel) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetChannelAgentID validates and assigns a new agent for an existing channel.
|
||||
func (s *Service) SetChannelAgentID(ctx context.Context, channel *IMChannel, agentID string) error {
|
||||
agentID = strings.TrimSpace(agentID)
|
||||
if agentID == "" {
|
||||
return fmt.Errorf("agent_id is required")
|
||||
}
|
||||
agent, err := s.agentService.GetAgentByID(ctx, agentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if agent == nil || agent.TenantID != channel.TenantID {
|
||||
return fmt.Errorf("agent not found")
|
||||
}
|
||||
channel.AgentID = agentID
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateChannel updates a channel and restarts it if needed.
|
||||
// Returns a duplicate_bot error if the bot identity is already used by another channel.
|
||||
func (s *Service) UpdateChannel(channel *IMChannel) error {
|
||||
|
||||
Reference in New Issue
Block a user