2026-03-11 16:25:48 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
2026-03-11 17:22:40 +08:00
|
|
|
|
"log"
|
2026-03-11 16:25:48 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"time"
|
2026-03-11 17:22:40 +08:00
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
"x-agents/server/internal/model"
|
2026-03-11 17:22:40 +08:00
|
|
|
|
"x-agents/server/internal/repository"
|
2026-03-12 10:49:44 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-03-12 23:18:46 +08:00
|
|
|
|
"github.com/google/uuid"
|
2026-03-11 16:25:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// AgentChatRequest Python Agent 对话请求
|
|
|
|
|
|
type AgentChatRequest struct {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
AgentID string `json:"agent_id"` // 支持 UUID 字符串
|
2026-03-11 17:22:40 +08:00
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
UserID int `json:"user_id"`
|
|
|
|
|
|
SessionID string `json:"session_id,omitempty"`
|
|
|
|
|
|
ModelID string `json:"model_id,omitempty"`
|
|
|
|
|
|
ModelName string `json:"model_name,omitempty"`
|
|
|
|
|
|
ModelProvider string `json:"model_provider,omitempty"`
|
|
|
|
|
|
APIKey string `json:"api_key,omitempty"`
|
|
|
|
|
|
BaseURL string `json:"base_url,omitempty"`
|
2026-03-12 10:49:44 +08:00
|
|
|
|
UseXBot bool `json:"use_xbot"`
|
2026-03-11 16:25:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AgentChatResponse Python Agent 对话响应
|
|
|
|
|
|
type AgentChatResponse struct {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
AgentID string `json:"agent_id"` // 支持 UUID 字符串
|
2026-03-11 16:25:48 +08:00
|
|
|
|
Response string `json:"response"`
|
|
|
|
|
|
ToolCalls []interface{} `json:"tool_calls"`
|
|
|
|
|
|
TokensUsed int `json:"tokens_used"`
|
|
|
|
|
|
DurationMs int `json:"duration_ms"`
|
|
|
|
|
|
SessionID string `json:"session_id"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TeamChatRequest 多智能体群聊请求
|
|
|
|
|
|
type TeamChatRequest struct {
|
|
|
|
|
|
SupervisorAgentID int `json:"supervisor_agent_id"`
|
|
|
|
|
|
MemberAgentIDs []int `json:"member_agent_ids"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
UserID int `json:"user_id"`
|
|
|
|
|
|
SessionID string `json:"session_id,omitempty"`
|
|
|
|
|
|
Strategy string `json:"strategy,omitempty"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TeamChatResponse 多智能体群聊响应
|
|
|
|
|
|
type TeamChatResponse struct {
|
|
|
|
|
|
SupervisorAgentID int `json:"supervisor_agent_id"`
|
|
|
|
|
|
Response string `json:"response"`
|
|
|
|
|
|
SubtaskResults []interface{} `json:"subtask_results"`
|
|
|
|
|
|
Strategy string `json:"strategy"`
|
|
|
|
|
|
DurationMs int `json:"duration_ms"`
|
|
|
|
|
|
SessionID string `json:"session_id"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AgentService Python Agent 服务
|
|
|
|
|
|
type AgentService struct {
|
|
|
|
|
|
pythonURL string
|
|
|
|
|
|
client *http.Client
|
2026-03-11 17:22:40 +08:00
|
|
|
|
modelRepo *repository.ModelRepository
|
2026-03-12 23:18:46 +08:00
|
|
|
|
agentRepo *repository.AgentRepository
|
2026-03-15 19:49:27 +08:00
|
|
|
|
chatRepo *repository.ChatRepository
|
2026-03-11 16:25:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewAgentService 创建 Agent 服务
|
2026-03-15 19:49:27 +08:00
|
|
|
|
func NewAgentService(pythonURL string, modelRepo *repository.ModelRepository, agentRepo *repository.AgentRepository, chatRepo *repository.ChatRepository) *AgentService {
|
2026-03-11 16:25:48 +08:00
|
|
|
|
return &AgentService{
|
|
|
|
|
|
pythonURL: pythonURL,
|
|
|
|
|
|
client: &http.Client{
|
|
|
|
|
|
Timeout: 120 * time.Second, // Agent 可能需要较长时间
|
|
|
|
|
|
},
|
2026-03-11 17:22:40 +08:00
|
|
|
|
modelRepo: modelRepo,
|
2026-03-12 23:18:46 +08:00
|
|
|
|
agentRepo: agentRepo,
|
2026-03-15 19:49:27 +08:00
|
|
|
|
chatRepo: chatRepo,
|
2026-03-11 16:25:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Chat 单智能体对话
|
|
|
|
|
|
func (s *AgentService) Chat(req AgentChatRequest) (*AgentChatResponse, error) {
|
2026-03-11 17:22:40 +08:00
|
|
|
|
// 如果传入了 model_id,查询模型配置获取 api_key 和 base_url
|
|
|
|
|
|
log.Printf("[AgentService] Chat called, model_id: %s, modelRepo: %v", req.ModelID, s.modelRepo != nil)
|
|
|
|
|
|
|
|
|
|
|
|
if req.ModelID != "" && s.modelRepo != nil {
|
|
|
|
|
|
model, err := s.modelRepo.FindByID(req.ModelID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("[AgentService] Error finding model: %v", err)
|
|
|
|
|
|
} else if model != nil {
|
|
|
|
|
|
log.Printf("[AgentService] Found model: id=%s, provider=%s, model=%s, base_url=%s, api_key_len=%d",
|
|
|
|
|
|
model.ID, model.Provider, model.Model, model.BaseURL, len(model.APIKey))
|
|
|
|
|
|
req.APIKey = model.APIKey
|
|
|
|
|
|
req.BaseURL = model.BaseURL
|
|
|
|
|
|
req.ModelProvider = model.Provider
|
|
|
|
|
|
req.ModelName = model.Model
|
|
|
|
|
|
log.Printf("[AgentService] Set req.APIKey=%s, req.BaseURL=%s", req.APIKey[:10]+"...", req.BaseURL)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.Printf("[AgentService] Model not found for id: %s", req.ModelID)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if s.modelRepo == nil {
|
|
|
|
|
|
log.Printf("[AgentService] WARNING: modelRepo is nil!")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 打印传给 Python 的请求内容
|
|
|
|
|
|
apiKeyPreview := ""
|
|
|
|
|
|
if req.APIKey != "" {
|
|
|
|
|
|
apiKeyPreview = req.APIKey
|
|
|
|
|
|
if len(apiKeyPreview) > 10 {
|
|
|
|
|
|
apiKeyPreview = apiKeyPreview[:10] + "..."
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Printf("[AgentService] Sending to Python: model_id=%s, api_key=%s, base_url=%s, provider=%s, model=%s",
|
|
|
|
|
|
req.ModelID, apiKeyPreview, req.BaseURL, req.ModelProvider, req.ModelName)
|
|
|
|
|
|
|
2026-03-16 10:27:07 +08:00
|
|
|
|
url := fmt.Sprintf("%s/agent/chat", s.pythonURL)
|
2026-03-11 16:25:48 +08:00
|
|
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := s.client.Do(httpReq)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to call python agent: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
|
return nil, fmt.Errorf("python agent error: %s", string(body))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result AgentChatResponse
|
|
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TeamChat 多智能体群聊
|
|
|
|
|
|
func (s *AgentService) TeamChat(req TeamChatRequest) (*TeamChatResponse, error) {
|
2026-03-16 10:27:07 +08:00
|
|
|
|
url := fmt.Sprintf("%s/agent/team/chat", s.pythonURL)
|
2026-03-11 16:25:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置默认策略
|
|
|
|
|
|
if req.Strategy == "" {
|
|
|
|
|
|
req.Strategy = "parallel"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := s.client.Do(httpReq)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to call python agent: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
|
return nil, fmt.Errorf("python agent error: %s", string(body))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result TeamChatResponse
|
|
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
|
|
}
|
2026-03-12 10:49:44 +08:00
|
|
|
|
|
|
|
|
|
|
// ChatStream 流式对话
|
2026-03-15 19:49:27 +08:00
|
|
|
|
func (s *AgentService) ChatStream(c interface{}, agentID string, message, sessionID, modelID string, userID int) error {
|
2026-03-12 10:49:44 +08:00
|
|
|
|
// 获取 gin.Context
|
|
|
|
|
|
ginCtx, ok := c.(*gin.Context)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return fmt.Errorf("invalid context type")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 19:49:27 +08:00
|
|
|
|
log.Printf("[ChatStream] Request: agentID=%s, message=%s, sessionID=%s, modelID=%s, userID=%d",
|
|
|
|
|
|
agentID, message, sessionID, modelID, userID)
|
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
|
// 初始化请求体
|
|
|
|
|
|
reqBody := map[string]interface{}{
|
2026-03-15 19:49:27 +08:00
|
|
|
|
"agent_id": agentID, // 传递字符串类型的 agent_id,支持 UUID
|
2026-03-12 10:49:44 +08:00
|
|
|
|
"message": message,
|
|
|
|
|
|
"user_id": userID,
|
|
|
|
|
|
"session_id": sessionID,
|
|
|
|
|
|
"use_xbot": false,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果传入了 model_id,查询模型配置获取 api_key 和 base_url
|
|
|
|
|
|
if modelID != "" && s.modelRepo != nil {
|
|
|
|
|
|
model, err := s.modelRepo.FindByID(modelID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("[ChatStream] Model not found: %s, error: %v", modelID, err)
|
|
|
|
|
|
} else if model != nil {
|
|
|
|
|
|
log.Printf("[ChatStream] Using model: provider=%s, model=%s, base_url=%s", model.Provider, model.Model, model.BaseURL)
|
|
|
|
|
|
// 将模型配置添加到请求体
|
|
|
|
|
|
reqBody["model_provider"] = model.Provider
|
|
|
|
|
|
reqBody["model_name"] = model.Model
|
|
|
|
|
|
reqBody["api_key"] = model.APIKey
|
|
|
|
|
|
reqBody["base_url"] = model.BaseURL
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
log.Printf("[ChatStream] modelID is empty or modelRepo is nil: modelID=%s, modelRepo=%v", modelID, s.modelRepo != nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-16 10:27:07 +08:00
|
|
|
|
streamURL := fmt.Sprintf("%s/agent/chat/stream", s.pythonURL)
|
2026-03-12 10:49:44 +08:00
|
|
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(reqBody)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to marshal request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 HTTP 请求,设置不缓冲
|
|
|
|
|
|
httpReq, err := http.NewRequest("POST", streamURL, bytes.NewBuffer(jsonData))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
httpReq.Header.Set("Accept", "text/event-stream")
|
|
|
|
|
|
httpReq.Header.Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
|
|
|
|
// 创建不缓冲的 HTTP 客户端
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
|
Transport: &http.Transport{
|
|
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
resp, err := client.Do(httpReq)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to call python agent: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 SSE 响应头
|
|
|
|
|
|
ginCtx.Header("Content-Type", "text/event-stream")
|
|
|
|
|
|
ginCtx.Header("Cache-Control", "no-cache")
|
|
|
|
|
|
ginCtx.Header("Connection", "keep-alive")
|
|
|
|
|
|
ginCtx.Header("X-Accel-Buffering", "no")
|
|
|
|
|
|
|
|
|
|
|
|
// 分块读取并转发,使用小 buffer 减少延迟
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
for {
|
|
|
|
|
|
n, err := resp.Body.Read(buf)
|
|
|
|
|
|
if n > 0 {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
log.Printf("[ChatStream] Received %d bytes from Python", n)
|
2026-03-12 10:49:44 +08:00
|
|
|
|
_, writeErr := ginCtx.Writer.Write(buf[:n])
|
|
|
|
|
|
if writeErr != nil {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
log.Printf("[ChatStream] Write error: %v", writeErr)
|
2026-03-12 10:49:44 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
// 强制刷新到客户端
|
|
|
|
|
|
if flusher, ok := ginCtx.Writer.(interface{ Flush() }); ok {
|
|
|
|
|
|
flusher.Flush()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
log.Printf("[ChatStream] Done reading from Python, err: %v", err)
|
2026-03-12 10:49:44 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
|
|
|
|
|
// CreateAgentRequest 创建智能体请求
|
|
|
|
|
|
type CreateAgentRequest struct {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
|
Avatar string `json:"avatar"`
|
|
|
|
|
|
SkillsMode string `json:"skillsMode"`
|
|
|
|
|
|
Skills []string `json:"skills"`
|
|
|
|
|
|
Knowledge string `json:"knowledge"`
|
|
|
|
|
|
Prompt string `json:"prompt"`
|
|
|
|
|
|
ModelProvider string `json:"model_provider"`
|
|
|
|
|
|
ModelName string `json:"model_name"`
|
|
|
|
|
|
UserID int `json:"user_id"`
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreateAgentResponse 创建智能体响应
|
|
|
|
|
|
type CreateAgentResponse struct {
|
2026-03-15 19:49:27 +08:00
|
|
|
|
AgentID int `json:"agent_id"` // 保留兼容性
|
|
|
|
|
|
AgentIDStr string `json:"agent_id_str"` // 返回实际的 UUID
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Message string `json:"message"`
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreateAgent 创建智能体
|
|
|
|
|
|
func (s *AgentService) CreateAgent(req CreateAgentRequest, userID int) (*CreateAgentResponse, error) {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
log.Printf("[AgentService] CreateAgent: agentRepo is nil!")
|
|
|
|
|
|
return nil, fmt.Errorf("agent repository not initialized")
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] CreateAgent: %s (userID: %d), skillsMode: %s, skills: %v", req.Name, userID, req.SkillsMode, req.Skills)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
// 处理 skills:根据 skillsMode 决定
|
|
|
|
|
|
var skills []string
|
|
|
|
|
|
if req.SkillsMode == "all" {
|
|
|
|
|
|
// "all" 模式用 "*" 表示
|
|
|
|
|
|
skills = []string{"*"}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
skills = req.Skills
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
// 创建 Agent 模型并保存到数据库
|
|
|
|
|
|
agent := &model.Agent{
|
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
OwnerID: fmt.Sprintf("%d", userID),
|
2026-03-15 19:49:27 +08:00
|
|
|
|
Avatar: req.Avatar,
|
2026-03-12 23:18:46 +08:00
|
|
|
|
Skills: skills,
|
|
|
|
|
|
RoleDescription: req.Prompt,
|
|
|
|
|
|
ModelProvider: req.ModelProvider,
|
|
|
|
|
|
ModelName: req.ModelName,
|
|
|
|
|
|
IsActive: true,
|
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
|
UpdatedAt: time.Now(),
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] CreateAgent: calling Create, agent = %+v", agent)
|
|
|
|
|
|
if err := s.agentRepo.Create(agent); err != nil {
|
|
|
|
|
|
log.Printf("[AgentService] CreateAgent: Create error: %v", err)
|
|
|
|
|
|
return nil, fmt.Errorf("failed to create agent: %w", err)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] CreateAgent: created successfully")
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] Agent created in database: %s (ID: %s)", agent.Name, agent.ID)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
2026-03-15 19:49:27 +08:00
|
|
|
|
// 返回数据库中实际的 Agent ID (UUID字符串)
|
2026-03-12 23:18:46 +08:00
|
|
|
|
return &CreateAgentResponse{
|
2026-03-15 19:49:27 +08:00
|
|
|
|
AgentIDStr: agent.ID,
|
|
|
|
|
|
Name: agent.Name,
|
|
|
|
|
|
Message: "Agent created successfully",
|
2026-03-12 23:18:46 +08:00
|
|
|
|
}, nil
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-16 10:27:07 +08:00
|
|
|
|
// TeamMemberInitRequest 团队成员初始化请求
|
|
|
|
|
|
type TeamMemberInitRequest struct {
|
|
|
|
|
|
Name string
|
|
|
|
|
|
Description string
|
|
|
|
|
|
Avatar string
|
|
|
|
|
|
Skills []string
|
|
|
|
|
|
RoleDescription string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// InitTeamMembers 初始化团队成员智能体
|
|
|
|
|
|
func (s *AgentService) InitTeamMembers() error {
|
|
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
log.Printf("[AgentService] InitTeamMembers: agentRepo is nil!")
|
|
|
|
|
|
return fmt.Errorf("agent repository not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 骚人开发组团队成员配置
|
|
|
|
|
|
teamMembers := []TeamMemberInitRequest{
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "小荣",
|
|
|
|
|
|
Description: "前端开发工程师 - 骚人开发组成员",
|
|
|
|
|
|
Avatar: "👨💻",
|
|
|
|
|
|
Skills: []string{"Vue 3", "TypeScript", "Element Plus", "Tailwind CSS"},
|
|
|
|
|
|
RoleDescription: `你叫小荣,是骚人开发组的前端开发工程师。你细心认真,善于沟通。
|
|
|
|
|
|
|
|
|
|
|
|
技能专长:
|
|
|
|
|
|
- Vue 3 框架开发
|
|
|
|
|
|
- TypeScript 类型系统
|
|
|
|
|
|
- Element Plus 组件库
|
|
|
|
|
|
- Tailwind CSS 样式框架
|
|
|
|
|
|
|
|
|
|
|
|
性格特点:
|
|
|
|
|
|
- 细心认真,注重代码质量
|
|
|
|
|
|
- 善于与团队成员沟通协作
|
|
|
|
|
|
- 积极解决前端技术难题`,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否已存在同名智能体
|
|
|
|
|
|
for _, member := range teamMembers {
|
|
|
|
|
|
existingAgents, err := s.agentRepo.FindAll()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("[AgentService] InitTeamMembers: failed to list agents: %v", err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exists := false
|
|
|
|
|
|
for _, a := range existingAgents {
|
|
|
|
|
|
if a.Name == member.Name {
|
|
|
|
|
|
exists = true
|
|
|
|
|
|
log.Printf("[AgentService] InitTeamMembers: agent %s already exists, skipping", member.Name)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
|
|
// 创建智能体
|
|
|
|
|
|
agent := &model.Agent{
|
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
|
Name: member.Name,
|
|
|
|
|
|
Description: member.Description,
|
|
|
|
|
|
OwnerID: "1", // 系统管理员
|
|
|
|
|
|
Avatar: member.Avatar,
|
|
|
|
|
|
Skills: member.Skills,
|
|
|
|
|
|
RoleDescription: member.RoleDescription,
|
|
|
|
|
|
ModelProvider: "anthropic",
|
|
|
|
|
|
ModelName: "claude-sonnet-4-20250514",
|
|
|
|
|
|
IsActive: true,
|
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := s.agentRepo.Create(agent); err != nil {
|
|
|
|
|
|
log.Printf("[AgentService] InitTeamMembers: failed to create agent %s: %v", member.Name, err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Printf("[AgentService] InitTeamMembers: created agent %s (ID: %s)", member.Name, agent.ID)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 17:16:53 +08:00
|
|
|
|
// ListAgentsResponse 获取智能体列表响应
|
|
|
|
|
|
type ListAgentsResponse struct {
|
|
|
|
|
|
Agents []interface{} `json:"agents"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
// ListAgents 获取智能体列表(从数据库获取)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
func (s *AgentService) ListAgents() (*ListAgentsResponse, error) {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
log.Printf("[AgentService] ListAgents: agentRepo is nil!")
|
|
|
|
|
|
return nil, fmt.Errorf("agent repository not initialized")
|
|
|
|
|
|
}
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] ListAgents: calling FindAll")
|
|
|
|
|
|
agents, err := s.agentRepo.FindAll()
|
2026-03-12 17:16:53 +08:00
|
|
|
|
if err != nil {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] ListAgents: FindAll error: %v", err)
|
|
|
|
|
|
return nil, fmt.Errorf("failed to list agents: %w", err)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] ListAgents: found %d agents", len(agents))
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为 interface{} 切片
|
|
|
|
|
|
agentsList := make([]interface{}, len(agents))
|
|
|
|
|
|
for i, agent := range agents {
|
|
|
|
|
|
agentsList[i] = agent
|
|
|
|
|
|
log.Printf("[AgentService] ListAgents: agent[%d] = %+v", i, agent)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &ListAgentsResponse{
|
|
|
|
|
|
Agents: agentsList,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateAgentStatus 更新智能体状态
|
|
|
|
|
|
func (s *AgentService) UpdateAgentStatus(agentID string, isActive bool) error {
|
|
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
return fmt.Errorf("agent repository not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("[AgentService] UpdateAgentStatus: id=%s, isActive=%v", agentID, isActive)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否存在
|
|
|
|
|
|
agent, err := s.agentRepo.FindByID(agentID)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
if err != nil {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
return fmt.Errorf("agent not found: %w", err)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
agent.IsActive = isActive
|
|
|
|
|
|
agent.UpdatedAt = time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
if err := s.agentRepo.Update(agent); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to update agent: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("[AgentService] Agent status updated: id=%s, isActive=%v", agentID, isActive)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteAgent 删除智能体
|
|
|
|
|
|
func (s *AgentService) DeleteAgent(agentID string) error {
|
|
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
return fmt.Errorf("agent repository not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("[AgentService] DeleteAgent: id=%s", agentID)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否存在
|
|
|
|
|
|
_, err := s.agentRepo.FindByID(agentID)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
if err != nil {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
return fmt.Errorf("agent not found: %w", err)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 19:49:27 +08:00
|
|
|
|
// 先删除该智能体的所有会话和消息
|
|
|
|
|
|
if s.chatRepo != nil {
|
|
|
|
|
|
if err := s.chatRepo.DeleteSessionsByAgentID(agentID); err != nil {
|
|
|
|
|
|
log.Printf("[AgentService] DeleteAgent: failed to delete sessions: %v", err)
|
|
|
|
|
|
// 继续尝试删除 agent,不因为 session 删除失败而中止
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
if err := s.agentRepo.Delete(agentID); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to delete agent: %w", err)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] Agent deleted: id=%s", agentID)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateAgent 更新智能体
|
2026-03-15 19:49:27 +08:00
|
|
|
|
func (s *AgentService) UpdateAgent(agentID, name, description, avatar string, skills []string, roleDescription, modelProvider, modelName string) error {
|
2026-03-12 23:18:46 +08:00
|
|
|
|
if s.agentRepo == nil {
|
|
|
|
|
|
return fmt.Errorf("agent repository not initialized")
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
log.Printf("[AgentService] UpdateAgent: id=%s, name=%s", agentID, name)
|
2026-03-12 17:16:53 +08:00
|
|
|
|
|
2026-03-12 23:18:46 +08:00
|
|
|
|
// 检查是否存在
|
|
|
|
|
|
agent, err := s.agentRepo.FindByID(agentID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("agent not found: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新字段
|
|
|
|
|
|
if name != "" {
|
|
|
|
|
|
agent.Name = name
|
|
|
|
|
|
}
|
|
|
|
|
|
if description != "" {
|
|
|
|
|
|
agent.Description = description
|
|
|
|
|
|
}
|
2026-03-15 19:49:27 +08:00
|
|
|
|
if avatar != "" {
|
|
|
|
|
|
agent.Avatar = avatar
|
|
|
|
|
|
}
|
2026-03-12 23:18:46 +08:00
|
|
|
|
if skills != nil {
|
|
|
|
|
|
agent.Skills = skills
|
|
|
|
|
|
}
|
|
|
|
|
|
if roleDescription != "" {
|
|
|
|
|
|
agent.RoleDescription = roleDescription
|
|
|
|
|
|
}
|
|
|
|
|
|
if modelProvider != "" {
|
|
|
|
|
|
agent.ModelProvider = modelProvider
|
|
|
|
|
|
}
|
|
|
|
|
|
if modelName != "" {
|
|
|
|
|
|
agent.ModelName = modelName
|
|
|
|
|
|
}
|
|
|
|
|
|
agent.UpdatedAt = time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
if err := s.agentRepo.Update(agent); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to update agent: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("[AgentService] Agent updated: id=%s", agentID)
|
|
|
|
|
|
return nil
|
2026-03-12 17:16:53 +08:00
|
|
|
|
}
|