Files
common/ragflow/agent.go
2026-03-12 08:50:26 +08:00

133 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}