feat: 更新后端agent和skill服务
- agent_handler.go: 新增ListAgents、CreateAgent接口 - skill_handler.go: 更新skill内容获取和保存功能 - agent_service.go: 新增agent服务逻辑 - main.go: 新增agent路由 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -279,3 +279,118 @@ func (s *AgentService) ChatStream(c interface{}, agentID int, message, sessionID
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateAgentRequest 创建智能体请求
|
||||
type CreateAgentRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar"`
|
||||
SkillsMode string `json:"skills_mode"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// CreateAgentResponse 创建智能体响应
|
||||
type CreateAgentResponse struct {
|
||||
AgentID int `json:"agent_id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// CreateAgent 创建智能体
|
||||
func (s *AgentService) CreateAgent(req CreateAgentRequest, userID int) (*CreateAgentResponse, error) {
|
||||
url := fmt.Sprintf("%s/agent/create", s.pythonURL)
|
||||
|
||||
// 构建请求体
|
||||
pythonReq := CreateAgentRequest{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Avatar: req.Avatar,
|
||||
SkillsMode: req.SkillsMode,
|
||||
Skills: req.Skills,
|
||||
Knowledge: req.Knowledge,
|
||||
Prompt: req.Prompt,
|
||||
ModelProvider: req.ModelProvider,
|
||||
ModelName: req.ModelName,
|
||||
UserID: userID,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(pythonReq)
|
||||
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 CreateAgentResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[AgentService] Agent created: %s (ID: %d)", result.Name, result.AgentID)
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// ListAgentsResponse 获取智能体列表响应
|
||||
type ListAgentsResponse struct {
|
||||
Agents []interface{} `json:"agents"`
|
||||
}
|
||||
|
||||
// ListAgents 获取智能体列表
|
||||
func (s *AgentService) ListAgents() (*ListAgentsResponse, error) {
|
||||
url := fmt.Sprintf("%s/agent/list", s.pythonURL)
|
||||
|
||||
httpReq, err := http.NewRequest("GET", url, nil)
|
||||
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 ListAgentsResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[AgentService] Listed agents: %d", len(result.Agents))
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user