Files
common/ragflow/agent.go

142 lines
4.5 KiB
Go
Raw Permalink Normal View History

2025-11-27 17:38:42 +08:00
package ragflow
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
2025-11-27 17:38:42 +08:00
)
// Agent AGENT 管理
// 参考: https://ragflow.com.cn/docs/dev/http_api_reference#agent-管理
// Agent Agent 结构体
type Agent struct {
2025-12-02 14:59:07 +08:00
ID string `json:"id"` // Agent ID
Title string `json:"title"` // Agent 标题
Description string `json:"description"` // Agent 描述
Avatar string `json:"avatar"` // 头像Base64 编码)
CanvasType string `json:"canvas_type"` // 画布类型
CreateDate string `json:"create_date"` // 创建日期(格式化字符串)
CreateTime int64 `json:"create_time"` // 创建时间Unix 时间戳)
UpdateDate string `json:"update_date"` // 更新日期(格式化字符串)
UpdateTime int64 `json:"update_time"` // 更新时间Unix 时间戳)
UserID string `json:"user_id"` // 用户 ID
DSL map[string]interface{} `json:"dsl"` // Canvas DSL 对象,定义 Agent 的工作流
2025-11-27 17:38:42 +08:00
}
// 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 响应
2025-12-02 14:59:07 +08:00
// 注意API 不返回 total 字段,仅返回 data 数组
2025-11-27 17:38:42 +08:00
type ListAgentsRes struct {
2025-12-02 14:59:07 +08:00
Code int `json:"code"` // 状态码0 表示成功
Data []*Agent `json:"data"` // Agent 列表
2025-11-27 17:38:42 +08:00
}
// CreateAgent 创建 Agent
// POST /api/v1/agents
func (c *Client) CreateAgent(ctx context.Context, req *CreateAgentReq) (err error) {
2025-11-27 17:38:42 +08:00
var res CommonResponse
if err = c.request(ctx, "POST", "/api/v1/agents", req, &res); err != nil {
return gerror.Newf("create agent failed: %v", err)
2025-11-27 17:38:42 +08:00
}
if !res.IsSuccess() {
return gerror.Newf("create agent failed: %s", res.Message)
2025-11-27 17:38:42 +08:00
}
return
2025-11-27 17:38:42 +08:00
}
// UpdateAgent 更新 Agent
// PUT /api/v1/agents/{agent_id}
func (c *Client) UpdateAgent(ctx context.Context, agentID string, req *UpdateAgentReq) (err error) {
path := "/api/v1/agents/" + agentID
2025-11-27 17:38:42 +08:00
var res CommonResponse
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
return gerror.Newf("update agent failed: %v", err)
2025-11-27 17:38:42 +08:00
}
if !res.IsSuccess() {
return gerror.Newf("update agent failed: %s", res.Message)
2025-11-27 17:38:42 +08:00
}
return
2025-11-27 17:38:42 +08:00
}
// DeleteAgent 删除 Agent
// DELETE /api/v1/agents/{agent_id}
func (c *Client) DeleteAgent(ctx context.Context, agentID string) (err error) {
path := "/api/v1/agents/" + agentID
2025-11-27 17:38:42 +08:00
var res CommonResponse
2025-12-02 14:59:07 +08:00
// 官方文档要求传空对象,不是 nil
if err = c.request(ctx, "DELETE", path, map[string]interface{}{}, &res); err != nil {
return gerror.Newf("delete agent failed: %v", err)
2025-11-27 17:38:42 +08:00
}
if !res.IsSuccess() {
return gerror.Newf("delete agent failed: %s", res.Message)
2025-11-27 17:38:42 +08:00
}
return
2025-11-27 17:38:42 +08:00
}
// 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, gerror.Newf("list agents failed: %v", err)
2025-11-27 17:38:42 +08:00
}
if res.Code != 0 {
return nil, gerror.Newf("list agents failed: code=%d", res.Code)
2025-11-27 17:38:42 +08:00
}
return &res, nil
}