Documentation
¶
Overview ¶
Package handlers 提供 AgentFlow HTTP API 的处理器实现。
Index ¶
- func DecodeJSONBody(w http.ResponseWriter, r *http.Request, dst any, logger *zap.Logger) error
- func ValidateContentType(w http.ResponseWriter, r *http.Request, logger *zap.Logger) bool
- func WriteError(w http.ResponseWriter, err *types.Error, logger *zap.Logger)
- func WriteErrorMessage(w http.ResponseWriter, status int, code types.ErrorCode, message string, ...)
- func WriteJSON(w http.ResponseWriter, status int, data any)
- func WriteSuccess(w http.ResponseWriter, data any)
- type AgentExecuteRequest
- type AgentExecuteResponse
- type AgentHandler
- func (h *AgentHandler) HandleAgentHealth(w http.ResponseWriter, r *http.Request)
- func (h *AgentHandler) HandleExecuteAgent(w http.ResponseWriter, r *http.Request)
- func (h *AgentHandler) HandleGetAgent(w http.ResponseWriter, r *http.Request)
- func (h *AgentHandler) HandleListAgents(w http.ResponseWriter, r *http.Request)
- func (h *AgentHandler) HandlePlanAgent(w http.ResponseWriter, r *http.Request)
- type AgentHealthResponse
- type AgentInfo
- type ChatHandler
- type CheckResult
- type DatabaseHealthCheck
- type ErrorInfo
- type HealthCheck
- type HealthHandler
- func (h *HealthHandler) HandleHealth(w http.ResponseWriter, r *http.Request)
- func (h *HealthHandler) HandleHealthz(w http.ResponseWriter, r *http.Request)
- func (h *HealthHandler) HandleReady(w http.ResponseWriter, r *http.Request)
- func (h *HealthHandler) HandleVersion(version, buildTime, gitCommit string) http.HandlerFunc
- func (h *HealthHandler) RegisterCheck(check HealthCheck)
- type RedisHealthCheck
- type Response
- type ResponseWriter
- type ServiceHealthResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSONBody ¶
DecodeJSONBody 解码 JSON 请求体
func ValidateContentType ¶
ValidateContentType 验证 Content-Type
func WriteError ¶
WriteError 写入错误响应(从 types.Error)
func WriteErrorMessage ¶
func WriteErrorMessage(w http.ResponseWriter, status int, code types.ErrorCode, message string, logger *zap.Logger)
WriteErrorMessage 写入简单错误消息
Types ¶
type AgentExecuteRequest ¶
type AgentExecuteRequest struct {
AgentID string `json:"agent_id" binding:"required"`
Content string `json:"content" binding:"required"`
Context map[string]any `json:"context,omitempty"`
Variables map[string]string `json:"variables,omitempty"`
}
AgentExecuteRequest Agent execution request
type AgentExecuteResponse ¶
type AgentExecuteResponse struct {
TraceID string `json:"trace_id"`
Content string `json:"content"`
Metadata map[string]any `json:"metadata,omitempty"`
TokensUsed int `json:"tokens_used,omitempty"`
Cost float64 `json:"cost,omitempty"`
Duration string `json:"duration"`
FinishReason string `json:"finish_reason,omitempty"`
}
AgentExecuteResponse Agent execution response
type AgentHandler ¶
type AgentHandler struct {
// contains filtered or unexported fields
}
AgentHandler Agent management handler
func NewAgentHandler ¶
func NewAgentHandler(registry discovery.Registry, agentRegistry *agent.AgentRegistry, logger *zap.Logger) *AgentHandler
NewAgentHandler creates an Agent handler
func (*AgentHandler) HandleAgentHealth ¶
func (h *AgentHandler) HandleAgentHealth(w http.ResponseWriter, r *http.Request)
HandleAgentHealth checks agent health status @Summary Agent health check @Description Check if an agent is healthy and ready @Tags agent @Produce json @Param id query string true "Agent ID" @Success 200 {object} Response{data=AgentHealthResponse} "Agent health" @Failure 404 {object} Response "Agent not found" @Failure 503 {object} Response "Agent not ready" @Security ApiKeyAuth @Router /v1/agents/health [get]
func (*AgentHandler) HandleExecuteAgent ¶
func (h *AgentHandler) HandleExecuteAgent(w http.ResponseWriter, r *http.Request)
HandleExecuteAgent executes an agent @Summary Execute agent @Description Execute an agent with the given input @Tags agent @Accept json @Produce json @Param request body AgentExecuteRequest true "Execution request" @Success 200 {object} Response{data=AgentExecuteResponse} "Execution result" @Failure 400 {object} Response "Invalid request" @Failure 404 {object} Response "Agent not found" @Failure 500 {object} Response "Execution failed" @Security ApiKeyAuth @Router /v1/agents/execute [post]
func (*AgentHandler) HandleGetAgent ¶
func (h *AgentHandler) HandleGetAgent(w http.ResponseWriter, r *http.Request)
HandleGetAgent gets a single agent's information @Summary Get agent @Description Get information about a specific agent @Tags agent @Produce json @Param id path string true "Agent ID" @Success 200 {object} Response{data=AgentInfo} "Agent info" @Failure 404 {object} Response "Agent not found" @Security ApiKeyAuth @Router /v1/agents/{id} [get]
func (*AgentHandler) HandleListAgents ¶
func (h *AgentHandler) HandleListAgents(w http.ResponseWriter, r *http.Request)
HandleListAgents lists all registered agents @Summary List agents @Description Get a list of all registered agents @Tags agent @Produce json @Success 200 {object} Response{data=[]AgentInfo} "Agent list" @Failure 500 {object} Response "Internal error" @Security ApiKeyAuth @Router /v1/agents [get]
func (*AgentHandler) HandlePlanAgent ¶
func (h *AgentHandler) HandlePlanAgent(w http.ResponseWriter, r *http.Request)
HandlePlanAgent plans agent execution @Summary Plan agent execution @Description Get an execution plan for an agent @Tags agent @Accept json @Produce json @Param request body AgentExecuteRequest true "Plan request" @Success 200 {object} Response{data=map[string]any} "Execution plan" @Failure 400 {object} Response "Invalid request" @Failure 404 {object} Response "Agent not found" @Failure 500 {object} Response "Plan failed" @Security ApiKeyAuth @Router /v1/agents/plan [post]
type AgentHealthResponse ¶
type AgentHealthResponse struct {
AgentID string `json:"agent_id"`
Status string `json:"status"`
Healthy bool `json:"healthy"`
Endpoint string `json:"endpoint,omitempty"`
Load float64 `json:"load"`
CheckedAt string `json:"checked_at"`
}
AgentHealthResponse Agent health check response
type AgentInfo ¶
type AgentInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Type agent.AgentType `json:"type"`
State string `json:"state"`
Description string `json:"description,omitempty"`
Model string `json:"model,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
AgentInfo Agent information returned by the API
type ChatHandler ¶
type ChatHandler struct {
// contains filtered or unexported fields
}
ChatHandler 聊天接口处理器
func NewChatHandler ¶
func NewChatHandler(provider llm.Provider, logger *zap.Logger) *ChatHandler
NewChatHandler 创建聊天处理器
func (*ChatHandler) HandleCompletion ¶
func (h *ChatHandler) HandleCompletion(w http.ResponseWriter, r *http.Request)
HandleCompletion 处理聊天补全请求 @Summary 聊天完成 @Description 发送聊天完成请求 @Tags 聊天 @Accept json @Produce json @Param request body api.ChatRequest true "聊天请求" @Success 200 {object} api.ChatResponse "聊天响应" @Failure 400 {object} Response "无效请求" @Failure 500 {object} Response "内部错误" @Security ApiKeyAuth @Router /v1/chat/completions [post]
func (*ChatHandler) HandleStream ¶
func (h *ChatHandler) HandleStream(w http.ResponseWriter, r *http.Request)
HandleStream 处理流式聊天请求 @Summary 流式聊天完成 @Description 发送流式聊天完成请求 @Tags 聊天 @Accept json @Produce text/event-stream @Param request body api.ChatRequest true "聊天请求" @Success 200 {string} string "SSE 流" @Failure 400 {object} Response "无效请求" @Failure 500 {object} Response "内部错误" @Security ApiKeyAuth @Router /v1/chat/completions/stream [post]
type CheckResult ¶
type CheckResult struct {
Status string `json:"status"` // "pass", "fail"
Message string `json:"message,omitempty"`
Latency string `json:"latency,omitempty"`
}
CheckResult 单个检查结果
type DatabaseHealthCheck ¶
type DatabaseHealthCheck struct {
// contains filtered or unexported fields
}
DatabaseHealthCheck 数据库健康检查
func NewDatabaseHealthCheck ¶
func NewDatabaseHealthCheck(name string, ping func(ctx context.Context) error) *DatabaseHealthCheck
NewDatabaseHealthCheck 创建数据库健康检查
func (*DatabaseHealthCheck) Name ¶
func (c *DatabaseHealthCheck) Name() string
type ErrorInfo ¶
type ErrorInfo struct {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
Retryable bool `json:"retryable,omitempty"`
HTTPStatus int `json:"-"` // 不序列化到 JSON
}
ErrorInfo 错误信息结构
type HealthCheck ¶
HealthCheck 健康检查接口
type HealthHandler ¶
type HealthHandler struct {
// contains filtered or unexported fields
}
HealthHandler 健康检查处理器
func NewHealthHandler ¶
func NewHealthHandler(logger *zap.Logger) *HealthHandler
NewHealthHandler 创建健康检查处理器
func (*HealthHandler) HandleHealth ¶
func (h *HealthHandler) HandleHealth(w http.ResponseWriter, r *http.Request)
HandleHealth 处理 /health 请求(简单健康检查) @Summary 健康检查 @Description 简单的健康检查端点 @Tags 健康 @Produce json @Success 200 {object} ServiceHealthResponse "服务正常" @Failure 503 {object} ServiceHealthResponse "服务不健康" @Router /health [get]
func (*HealthHandler) HandleHealthz ¶
func (h *HealthHandler) HandleHealthz(w http.ResponseWriter, r *http.Request)
HandleHealthz 处理 /healthz 请求(Kubernetes 风格) @Summary Kubernetes 活跃度探针 @Description Kubernetes 的活跃度探针 @Tags 健康 @Produce json @Success 200 {object} ServiceHealthResponse "服务处于活动状态" @Router /healthz [get]
func (*HealthHandler) HandleReady ¶
func (h *HealthHandler) HandleReady(w http.ResponseWriter, r *http.Request)
HandleReady 处理 /ready 或 /readyz 请求(就绪检查) @Summary 准备情况检查 @Description 检查服务是否准备好接受流量 @Tags 健康 @Produce json @Success 200 {object} ServiceHealthResponse "服务已准备就绪" @Failure 503 {object} ServiceHealthResponse "服务尚未准备好" @Router /ready [get]
func (*HealthHandler) HandleVersion ¶
func (h *HealthHandler) HandleVersion(version, buildTime, gitCommit string) http.HandlerFunc
HandleVersion 处理 /version 请求 @Summary 版本信息 @Description 返回版本信息 @Tags 健康 @Produce json @Success 200 {object} map[string]string "版本信息" @Router /version [get]
func (*HealthHandler) RegisterCheck ¶
func (h *HealthHandler) RegisterCheck(check HealthCheck)
RegisterCheck 注册健康检查
type RedisHealthCheck ¶
type RedisHealthCheck struct {
// contains filtered or unexported fields
}
RedisHealthCheck Redis 健康检查
func NewRedisHealthCheck ¶
func NewRedisHealthCheck(name string, ping func(ctx context.Context) error) *RedisHealthCheck
NewRedisHealthCheck 创建 Redis 健康检查
func (*RedisHealthCheck) Name ¶
func (c *RedisHealthCheck) Name() string
type Response ¶
type Response struct {
Success bool `json:"success"`
Data any `json:"data,omitempty"`
Error *ErrorInfo `json:"error,omitempty"`
Timestamp time.Time `json:"timestamp"`
RequestID string `json:"request_id,omitempty"`
}
Response 统一 API 响应结构
type ResponseWriter ¶
type ResponseWriter struct {
http.ResponseWriter
StatusCode int
Written bool
}
ResponseWriter 包装 http.ResponseWriter 以捕获状态码
func NewResponseWriter ¶
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter
NewResponseWriter 创建新的 ResponseWriter
func (*ResponseWriter) Write ¶
func (rw *ResponseWriter) Write(b []byte) (int, error)
Write 重写 Write 以标记已写入
func (*ResponseWriter) WriteHeader ¶
func (rw *ResponseWriter) WriteHeader(code int)
WriteHeader 重写 WriteHeader 以捕获状态码
type ServiceHealthResponse ¶
type ServiceHealthResponse struct {
Status string `json:"status"` // "healthy", "degraded", "unhealthy"
Timestamp time.Time `json:"timestamp"`
Version string `json:"version,omitempty"`
Checks map[string]CheckResult `json:"checks,omitempty"`
}
ServiceHealthResponse 服务级别的健康状态响应(HTTP 健康端点 DTO)。 注意:这是 HTTP 健康检查端点的响应类型,与 llm.HealthStatus(Provider 级别) 和 agent.HealthStatus(Agent 级别)是不同概念。