feat(im): integrate IM service for custom agent deletion

- Add IM service dependency to CustomAgentHandler for managing agent-related IM channels.
- Implement DeleteChannelsByAgent method to stop and soft-delete IM channels associated with a custom agent upon its removal.
- Update ListChannelsByTenant to exclude soft-deleted custom agents, ensuring consistent overview lists after agent removal.
This commit is contained in:
wizardchen
2026-06-22 17:14:49 +08:00
committed by lyingbug
parent ddc5460988
commit 632cd55018
2 changed files with 49 additions and 6 deletions
+18
View File
@@ -8,6 +8,7 @@ import (
"github.com/Tencent/WeKnora/internal/application/service"
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/im"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -18,6 +19,7 @@ import (
// CustomAgentHandler defines the HTTP handler for custom agent operations
type CustomAgentHandler struct {
service interfaces.CustomAgentService
imService *im.Service
disabledRepo interfaces.TenantDisabledSharedAgentRepository
// userService 仅用于 list 接口批量回填 creator_name,作用见
// KnowledgeBaseHandler.userService。
@@ -27,11 +29,13 @@ type CustomAgentHandler struct {
// NewCustomAgentHandler creates a new custom agent handler instance
func NewCustomAgentHandler(
service interfaces.CustomAgentService,
imService *im.Service,
disabledRepo interfaces.TenantDisabledSharedAgentRepository,
userService interfaces.UserService,
) *CustomAgentHandler {
return &CustomAgentHandler{
service: service,
imService: imService,
disabledRepo: disabledRepo,
userService: userService,
}
@@ -375,6 +379,20 @@ func (h *CustomAgentHandler) DeleteAgent(c *gin.Context) {
logger.Infof(ctx, "Deleting custom agent, ID: %s", secutils.SanitizeForLog(id))
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
c.Error(errors.NewUnauthorizedError("Unauthorized"))
return
}
if err := h.imService.DeleteChannelsByAgent(id, tenantID); err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"agent_id": id,
})
c.Error(errors.NewInternalServerError("Failed to delete agent IM channels"))
return
}
// Delete the agent
err := h.service.DeleteAgent(ctx, id)
if err != nil {