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

@@ -16,13 +16,15 @@ import (
type ChatService struct {
pythonURL string
agentRepo *repository.AgentRepository
agentRepo *repository.AgentRepository
modelRepo *repository.ModelRepository
}
func NewChatService(pythonURL string, agentRepo *repository.AgentRepository) *ChatService {
func NewChatService(pythonURL string, agentRepo *repository.AgentRepository, modelRepo *repository.ModelRepository) *ChatService {
return &ChatService{
pythonURL: pythonURL,
agentRepo: agentRepo,
agentRepo: agentRepo,
modelRepo: modelRepo,
}
}
@@ -30,9 +32,19 @@ type ChatRequest struct {
AgentID string `json:"agent_id"`
Message string `json:"message"`
SessionID string `json:"session_id"`
ModelID string `json:"model_id"`
Context map[string]interface{} `json:"context"`
}
// ModelConfig 模型配置,用于传递给 Python 服务
type ModelConfig struct {
Provider string `json:"provider"`
Model string `json:"model"`
APIKey string `json:"api_key"`
BaseURL string `json:"base_url"`
APIEndpoint string `json:"api_endpoint"`
}
type ChatResponse struct {
Reply string `json:"reply"`
SessionID string `json:"session_id"`
@@ -59,14 +71,40 @@ func (s *ChatService) Chat(ctx context.Context, userID string, req model.AgentRe
sessionID = uuid.New().String()
}
// 4. 调用 Python 服务
// 4. 如果提供了 ModelID获取模型配置
var modelConfig *ModelConfig
if req.ModelID != "" {
modelInfo, err := s.modelRepo.FindByID(req.ModelID)
if err != nil {
return nil, fmt.Errorf("model not found: %w", err)
}
modelConfig = &ModelConfig{
Provider: modelInfo.Provider,
Model: modelInfo.Model,
APIKey: modelInfo.APIKey,
BaseURL: modelInfo.BaseURL,
APIEndpoint: modelInfo.APIEndpoint,
}
}
// 5. 调用 Python 服务
pythonReq := ChatRequest{
AgentID: req.AgentID,
Message: req.Message,
SessionID: sessionID,
ModelID: req.ModelID,
Context: req.Context,
}
// 将模型配置放入 Context 中传递给 Python 服务
if modelConfig != nil {
pythonReq.Context = make(map[string]interface{})
for k, v := range req.Context {
pythonReq.Context[k] = v
}
pythonReq.Context["model_config"] = modelConfig
}
pythonResp, err := s.callPythonChat(ctx, pythonReq)
if err != nil {
return nil, fmt.Errorf("failed to call python service: %w", err)