This commit is contained in:
Cold
2025-11-27 17:38:42 +08:00
committed by 张斌
parent c75166912d
commit fb0cb27d1d
10 changed files with 437 additions and 92 deletions

132
ragflow/agent.go Normal file
View File

@@ -0,0 +1,132 @@
package ragflow
import (
"context"
"fmt"
)
// Agent AGENT 管理
// 参考: https://ragflow.com.cn/docs/dev/http_api_reference#agent-管理
// Agent Agent 结构体
type Agent struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
DSL map[string]interface{} `json:"dsl"` // Canvas DSL 对象
}
// CreateAgentReq 创建 Agent 请求
type CreateAgentReq struct {
Title string `json:"title"` // 必需
Description string `json:"description,omitempty"` // 可选,默认为 None
DSL map[string]interface{} `json:"dsl"` // 必需Canvas DSL 对象
}
// UpdateAgentReq 更新 Agent 请求
type UpdateAgentReq struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
DSL map[string]interface{} `json:"dsl,omitempty"`
}
// ListAgentsReq 列出 Agent 请求
type ListAgentsReq struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
OrderBy string `json:"orderby,omitempty"`
Desc bool `json:"desc,omitempty"`
Title string `json:"title,omitempty"`
ID string `json:"id,omitempty"`
}
// ListAgentsRes 列出 Agent 响应
type ListAgentsRes struct {
Code int `json:"code"`
Data []*Agent `json:"data"`
Total int `json:"total"`
}
// CreateAgent 创建 Agent
// POST /api/v1/agents
func (c *Client) CreateAgent(ctx context.Context, req *CreateAgentReq) error {
var res CommonResponse
if err := c.request(ctx, "POST", "/api/v1/agents", req, &res); err != nil {
return fmt.Errorf("create agent failed: %w", err)
}
if !res.IsSuccess() {
return fmt.Errorf("create agent failed: %s", res.Message)
}
return nil
}
// UpdateAgent 更新 Agent
// PUT /api/v1/agents/{agent_id}
func (c *Client) UpdateAgent(ctx context.Context, agentID string, req *UpdateAgentReq) error {
path := fmt.Sprintf("/api/v1/agents/%s", agentID)
var res CommonResponse
if err := c.request(ctx, "PUT", path, req, &res); err != nil {
return fmt.Errorf("update agent failed: %w", err)
}
if !res.IsSuccess() {
return fmt.Errorf("update agent failed: %s", res.Message)
}
return nil
}
// DeleteAgent 删除 Agent
// DELETE /api/v1/agents/{agent_id}
func (c *Client) DeleteAgent(ctx context.Context, agentID string) error {
path := fmt.Sprintf("/api/v1/agents/%s", agentID)
var res CommonResponse
if err := c.request(ctx, "DELETE", path, nil, &res); err != nil {
return fmt.Errorf("delete agent failed: %w", err)
}
if !res.IsSuccess() {
return fmt.Errorf("delete agent failed: %s", res.Message)
}
return nil
}
// ListAgents 列出 Agent
// GET /api/v1/agents
func (c *Client) ListAgents(ctx context.Context, req *ListAgentsReq) (*ListAgentsRes, error) {
path := "/api/v1/agents"
if req != nil {
params := map[string]interface{}{}
if req.Page > 0 {
params["page"] = req.Page
}
if req.PageSize > 0 {
params["page_size"] = req.PageSize
}
if req.OrderBy != "" {
params["orderby"] = req.OrderBy
}
if req.Desc {
params["desc"] = "true"
} else {
params["desc"] = "false"
}
if req.Title != "" {
params["title"] = req.Title
}
if req.ID != "" {
params["id"] = req.ID
}
query := buildQueryString(params)
if query != "" {
path += "?" + query
}
}
var res ListAgentsRes
if err := c.request(ctx, "GET", path, nil, &res); err != nil {
return nil, fmt.Errorf("list agents failed: %w", err)
}
if res.Code != 0 {
return nil, fmt.Errorf("list agents failed: code=%d", res.Code)
}
return &res, nil
}