feat: Agent 服务日志功能和后端更新

- agent/main.py: 添加日志记录功能
- agent/llm: 更新 anthropic, openai, factory
- agent/core/agent.py: 更新
- server: agent_handler, agent_service, chat_service 更新

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-11 17:22:40 +08:00
parent f9660a3d7b
commit 25eb277a2a
10 changed files with 260 additions and 25 deletions

View File

@@ -5,16 +5,24 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"x-agents/server/internal/repository"
)
// AgentChatRequest Python Agent 对话请求
type AgentChatRequest struct {
AgentID int `json:"agent_id"`
Message string `json:"message"`
UserID int `json:"user_id"`
SessionID string `json:"session_id,omitempty"`
AgentID int `json:"agent_id"`
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"`
}
// AgentChatResponse Python Agent 对话响应
@@ -51,20 +59,55 @@ type TeamChatResponse struct {
type AgentService struct {
pythonURL string
client *http.Client
modelRepo *repository.ModelRepository
}
// NewAgentService 创建 Agent 服务
func NewAgentService(pythonURL string) *AgentService {
func NewAgentService(pythonURL string, modelRepo *repository.ModelRepository) *AgentService {
return &AgentService{
pythonURL: pythonURL,
client: &http.Client{
Timeout: 120 * time.Second, // Agent 可能需要较长时间
},
modelRepo: modelRepo,
}
}
// Chat 单智能体对话
func (s *AgentService) Chat(req AgentChatRequest) (*AgentChatResponse, error) {
// 如果传入了 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)
url := fmt.Sprintf("%s/agent/chat", s.pythonURL)
jsonData, err := json.Marshal(req)