handlers

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 35 Imported by: 0

README

API Handlers

这个包提供了 AgentFlow HTTP API 的处理器实现。

📁 文件结构

api/handlers/
├── common.go   # 通用响应函数和错误处理
├── health.go   # 健康检查处理器
├── chat.go     # 聊天接口处理器
├── agent.go    # Agent 管理处理器
└── README.md   # 本文档

🎯 设计原则

1. 统一错误处理

所有 handler 使用 types.Error 进行错误处理,通过 WriteError() 函数统一返回错误响应。

err := types.NewError(types.ErrInvalidRequest, "model is required")
WriteError(w, err, logger)
2. 统一响应格式

所有 API 响应使用统一的 Response 结构:

{
  "success": true,
  "data": {...},
  "timestamp": "2026-02-20T10:00:00Z"
}

错误响应:

{
  "success": false,
  "error": {
    "code": "INVALID_REQUEST",
    "message": "model is required",
    "retryable": false
  },
  "timestamp": "2026-02-20T10:00:00Z"
}
3. 类型安全
  • 使用 DecodeJSONBody() 解码请求,自动验证 JSON 格式
  • 使用 ValidateContentType() 验证 Content-Type
  • 所有请求/响应都有明确的类型定义

📖 使用示例

健康检查
healthHandler := handlers.NewHealthHandler(logger)

// 注册健康检查
healthHandler.RegisterCheck(handlers.NewDatabaseHealthCheck("postgres", db.Ping))
healthHandler.RegisterCheck(handlers.NewRedisHealthCheck("redis", redis.Ping))

// 注册路由
http.HandleFunc("/health", healthHandler.HandleHealth)
http.HandleFunc("/healthz", healthHandler.HandleHealthz)
http.HandleFunc("/ready", healthHandler.HandleReady)
http.HandleFunc("/version", healthHandler.HandleVersion(version, buildTime, gitCommit))
聊天接口
chatHandler := handlers.NewChatHandler(provider, logger)

// 注册路由
http.HandleFunc("/v1/chat/completions", chatHandler.HandleCompletion)
http.HandleFunc("/v1/chat/completions/stream", chatHandler.HandleStream)
Agent 管理
agentHandler := handlers.NewAgentHandler(discoveryRegistry, agentRegistry, logger)

// 注册路由
http.HandleFunc("/v1/agents", agentHandler.HandleListAgents)
http.HandleFunc("/v1/agents/execute", agentHandler.HandleExecuteAgent)
http.HandleFunc("/v1/agents/plan", agentHandler.HandlePlanAgent)
http.HandleFunc("/v1/agents/health", agentHandler.HandleAgentHealth)

🔧 辅助函数

WriteJSON

写入 JSON 响应(带正确的 Content-Type 和安全头)

WriteJSON(w, http.StatusOK, data)
WriteSuccess

写入成功响应(自动包装为 Response 结构)

WriteSuccess(w, data)
WriteError

写入错误响应(从 types.Error 转换)

err := types.NewError(types.ErrInvalidRequest, "invalid input")
WriteError(w, err, logger)
WriteErrorMessage

写入简单错误消息

WriteErrorMessage(w, http.StatusBadRequest, types.ErrInvalidRequest, "invalid input", logger)
DecodeJSONBody

解码 JSON 请求体(带验证)

var req ChatRequest
if err := DecodeJSONBody(w, r, &req, logger); err != nil {
    return // 错误已自动写入响应
}
ValidateContentType

验证 Content-Type 是否为 application/json

if !ValidateContentType(w, r, logger) {
    return // 错误已自动写入响应
}

🎨 最佳实践

1. Handler 结构

每个 handler 应该包含:

  • 依赖注入(logger, provider, registry 等)
  • 请求验证
  • 业务逻辑调用
  • 响应转换
  • 错误处理
func (h *ChatHandler) HandleCompletion(w http.ResponseWriter, r *http.Request) {
    // 1. 验证 Content-Type
    if !ValidateContentType(w, r, h.logger) {
        return
    }

    // 2. 解码请求
    var req api.ChatRequest
    if err := DecodeJSONBody(w, r, &req, h.logger); err != nil {
        return
    }

    // 3. 验证请求
    if err := h.validateChatRequest(&req); err != nil {
        WriteError(w, err, h.logger)
        return
    }

    // 4. 调用业务逻辑
    resp, err := h.provider.Completion(ctx, llmReq)
    if err != nil {
        h.handleProviderError(w, err)
        return
    }

    // 5. 返回响应
    WriteSuccess(w, resp)
}
2. 错误处理
  • 使用 types.Error 而不是 fmt.Errorf
  • 设置正确的 HTTP 状态码
  • 标记是否可重试
  • 记录详细日志
err := types.NewError(types.ErrInvalidRequest, "model is required").
    WithHTTPStatus(http.StatusBadRequest).
    WithRetryable(false)
WriteError(w, err, h.logger)
3. 日志记录
  • 使用结构化日志(zap)
  • 记录关键信息(请求 ID、耗时、Token 使用等)
  • 错误日志包含完整上下文
h.logger.Info("chat completion",
    zap.String("model", req.Model),
    zap.Int("tokens_used", resp.Usage.TotalTokens),
    zap.Duration("duration", duration),
)
4. 类型转换
  • API 类型 ↔ 内部类型转换应该在 handler 层完成
  • 使用专门的转换函数(如 convertToLLMRequest
  • 保持类型安全
func (h *ChatHandler) convertToLLMRequest(req *api.ChatRequest) *llm.ChatRequest {
    messages := make([]types.Message, len(req.Messages))
    for i, msg := range req.Messages {
        messages[i] = types.Message(msg)
    }
    return &llm.ChatRequest{
        Model:    req.Model,
        Messages: messages,
        // ...
    }
}

🔒 安全考虑

  1. 输入验证:所有输入都应该验证
  2. Content-Type 检查:防止 MIME 类型混淆攻击
  3. 未知字段拒绝DisallowUnknownFields() 防止参数污染
  4. 安全响应头X-Content-Type-Options: nosniff
  5. 错误信息脱敏:不暴露内部实现细节

📊 性能优化

  1. 响应包装器:使用 ResponseWriter 捕获状态码,避免重复写入
  2. 流式响应:大数据量使用 SSE 流式传输
  3. 上下文超时:所有请求都应该设置超时
  4. 连接复用:HTTP/2 支持

🧪 测试

每个 handler 都应该有对应的测试文件:

handlers/
├── common_test.go
├── health_test.go
├── chat_test.go
└── agent_test.go

测试应该覆盖:

  • 正常流程
  • 错误处理
  • 边界条件
  • 并发安全

📝 TODO

  • 添加单元测试
  • 添加集成测试
  • 添加 OpenAPI 文档生成
  • 添加请求限流
  • 添加请求追踪(OpenTelemetry)
  • 添加指标收集(Prometheus)

🔗 相关文档

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSONBody

func DecodeJSONBody(w http.ResponseWriter, r *http.Request, dst any, logger *zap.Logger) error

DecodeJSONBody 解码 JSON 请求体

func ValidateContentType

func ValidateContentType(w http.ResponseWriter, r *http.Request, logger *zap.Logger) bool

ValidateContentType 验证 Content-Type 使用 mime.ParseMediaType 进行宽松解析,正确处理大小写变体 (如 "application/json; charset=UTF-8")和额外参数。

func ValidateEnum added in v1.0.0

func ValidateEnum(value string, allowed []string) bool

ValidateEnum checks whether value is one of the allowed values.

func ValidateNonNegative added in v1.0.0

func ValidateNonNegative(value float64) bool

ValidateNonNegative checks that value is >= 0.

func ValidateURL added in v1.0.0

func ValidateURL(s string) bool

ValidateURL validates that s is a well-formed HTTP or HTTPS URL.

func WriteError

func WriteError(w http.ResponseWriter, err *types.Error, logger *zap.Logger)

WriteError 写入错误响应(从 types.Error)

func WriteErrorMessage

func WriteErrorMessage(w http.ResponseWriter, status int, code types.ErrorCode, message string, logger *zap.Logger)

WriteErrorMessage 写入简单错误消息

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data any)

WriteJSON 写入 JSON 响应

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, data any)

WriteSuccess 写入成功响应

Types

type APIKeyHandler added in v1.0.0

type APIKeyHandler struct {
	// contains filtered or unexported fields
}

APIKeyHandler 处理 API Key 管理的 CRUD 操作

func NewAPIKeyHandler added in v1.0.0

func NewAPIKeyHandler(store APIKeyStore, logger *zap.Logger) *APIKeyHandler

NewAPIKeyHandler 创建 APIKeyHandler

func (*APIKeyHandler) HandleAPIKeyStats added in v1.0.0

func (h *APIKeyHandler) HandleAPIKeyStats(w http.ResponseWriter, r *http.Request)

HandleAPIKeyStats GET /api/v1/providers/{id}/api-keys/stats

func (*APIKeyHandler) HandleCreateAPIKey added in v1.0.0

func (h *APIKeyHandler) HandleCreateAPIKey(w http.ResponseWriter, r *http.Request)

HandleCreateAPIKey POST /api/v1/providers/{id}/api-keys

func (*APIKeyHandler) HandleDeleteAPIKey added in v1.0.0

func (h *APIKeyHandler) HandleDeleteAPIKey(w http.ResponseWriter, r *http.Request)

HandleDeleteAPIKey DELETE /api/v1/providers/{id}/api-keys/{keyId}

func (*APIKeyHandler) HandleListAPIKeys added in v1.0.0

func (h *APIKeyHandler) HandleListAPIKeys(w http.ResponseWriter, r *http.Request)

HandleListAPIKeys GET /api/v1/providers/{id}/api-keys

func (*APIKeyHandler) HandleListProviders added in v1.0.0

func (h *APIKeyHandler) HandleListProviders(w http.ResponseWriter, r *http.Request)

HandleListProviders GET /api/v1/providers

func (*APIKeyHandler) HandleUpdateAPIKey added in v1.0.0

func (h *APIKeyHandler) HandleUpdateAPIKey(w http.ResponseWriter, r *http.Request)

HandleUpdateAPIKey PUT /api/v1/providers/{id}/api-keys/{keyId}

type APIKeyStore added in v1.0.0

type APIKeyStore interface {
	// ListProviders returns all LLM providers ordered by ID.
	ListProviders() ([]llm.LLMProvider, error)

	// ListAPIKeys returns all API keys for a given provider, ordered by priority then ID.
	ListAPIKeys(providerID uint) ([]llm.LLMProviderAPIKey, error)

	// CreateAPIKey persists a new API key record.
	CreateAPIKey(key *llm.LLMProviderAPIKey) error

	// GetAPIKey retrieves a single API key by key ID and provider ID.
	GetAPIKey(keyID, providerID uint) (llm.LLMProviderAPIKey, error)

	// UpdateAPIKey applies partial updates to an existing API key.
	UpdateAPIKey(key *llm.LLMProviderAPIKey, updates map[string]any) error

	// ReloadAPIKey refreshes the API key record from the database.
	ReloadAPIKey(key *llm.LLMProviderAPIKey) error

	// DeleteAPIKey removes an API key by key ID and provider ID.
	// Returns the number of rows affected.
	DeleteAPIKey(keyID, providerID uint) (int64, error)
}

APIKeyStore defines the data access operations required by APIKeyHandler.

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, resolver ...AgentResolver) *AgentHandler

NewAgentHandler creates an Agent handler. The resolver parameter is optional — if nil, execute/stream endpoints return 501.

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) HandleAgentStream added in v1.0.0

func (h *AgentHandler) HandleAgentStream(w http.ResponseWriter, r *http.Request)

HandleAgentStream executes an agent with streaming SSE output. The agent's RuntimeStreamEmitter is wired to write SSE events to the response. SSE event types: token, tool_call, tool_result, error, and [DONE] terminator. @Summary Stream agent execution @Description Execute an agent and stream results via SSE @Tags agent @Accept json @Produce text/event-stream @Param request body AgentExecuteRequest true "Execution request" @Success 200 {string} string "SSE stream" @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/stream [post]

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 AgentResolver added in v1.0.0

type AgentResolver func(ctx context.Context, agentID string) (agent.Agent, error)

AgentResolver resolves an agent ID to a live Agent instance. This decouples the handler from how agents are stored/managed at runtime.

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 /api/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 /api/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) Check

func (c *DatabaseHealthCheck) Check(ctx context.Context) error

func (*DatabaseHealthCheck) Name

func (c *DatabaseHealthCheck) Name() string

type ErrorInfo

type ErrorInfo = api.ErrorInfo

ErrorInfo is a type alias for api.ErrorInfo — the canonical error structure. The canonical definition lives in api/types.go (§38).

type GormAPIKeyStore added in v1.0.0

type GormAPIKeyStore struct {
	// contains filtered or unexported fields
}

GormAPIKeyStore implements APIKeyStore using gorm.DB.

func NewGormAPIKeyStore added in v1.0.0

func NewGormAPIKeyStore(db *gorm.DB) *GormAPIKeyStore

NewGormAPIKeyStore creates a new GormAPIKeyStore.

func (*GormAPIKeyStore) CreateAPIKey added in v1.0.0

func (s *GormAPIKeyStore) CreateAPIKey(key *llm.LLMProviderAPIKey) error

func (*GormAPIKeyStore) DeleteAPIKey added in v1.0.0

func (s *GormAPIKeyStore) DeleteAPIKey(keyID, providerID uint) (int64, error)

func (*GormAPIKeyStore) GetAPIKey added in v1.0.0

func (s *GormAPIKeyStore) GetAPIKey(keyID, providerID uint) (llm.LLMProviderAPIKey, error)

func (*GormAPIKeyStore) ListAPIKeys added in v1.0.0

func (s *GormAPIKeyStore) ListAPIKeys(providerID uint) ([]llm.LLMProviderAPIKey, error)

func (*GormAPIKeyStore) ListProviders added in v1.0.0

func (s *GormAPIKeyStore) ListProviders() ([]llm.LLMProvider, error)

func (*GormAPIKeyStore) ReloadAPIKey added in v1.0.0

func (s *GormAPIKeyStore) ReloadAPIKey(key *llm.LLMProviderAPIKey) error

func (*GormAPIKeyStore) UpdateAPIKey added in v1.0.0

func (s *GormAPIKeyStore) UpdateAPIKey(key *llm.LLMProviderAPIKey, updates map[string]any) error

type HealthCheck

type HealthCheck interface {
	Name() string
	Check(ctx context.Context) error
}

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 MultimodalHandler added in v1.2.0

type MultimodalHandler struct {
	// contains filtered or unexported fields
}

MultimodalHandler 处理多模态 API 请求

func NewMultimodalHandler added in v1.2.0

func NewMultimodalHandler(router *multimodal.Router, logger *zap.Logger) *MultimodalHandler

NewMultimodalHandler 创建多模态 handler

func (*MultimodalHandler) HandleAnalyzeVideo added in v1.2.0

func (h *MultimodalHandler) HandleAnalyzeVideo(w http.ResponseWriter, r *http.Request)

HandleAnalyzeVideo 处理视频分析请求

func (*MultimodalHandler) HandleEditImage added in v1.2.0

func (h *MultimodalHandler) HandleEditImage(w http.ResponseWriter, r *http.Request)

HandleEditImage 处理图像编辑请求

func (*MultimodalHandler) HandleGenerate3D added in v1.2.0

func (h *MultimodalHandler) HandleGenerate3D(w http.ResponseWriter, r *http.Request)

HandleGenerate3D 处理 3D 模型生成请求

func (*MultimodalHandler) HandleGenerateImage added in v1.2.0

func (h *MultimodalHandler) HandleGenerateImage(w http.ResponseWriter, r *http.Request)

HandleGenerateImage 处理图像生成请求

func (*MultimodalHandler) HandleGenerateMusic added in v1.2.0

func (h *MultimodalHandler) HandleGenerateMusic(w http.ResponseWriter, r *http.Request)

HandleGenerateMusic 处理音乐生成请求

func (*MultimodalHandler) HandleGenerateVideo added in v1.2.0

func (h *MultimodalHandler) HandleGenerateVideo(w http.ResponseWriter, r *http.Request)

HandleGenerateVideo 处理视频生成请求

func (*MultimodalHandler) HandleListProviders added in v1.2.0

func (h *MultimodalHandler) HandleListProviders(w http.ResponseWriter, r *http.Request)

HandleListProviders 返回已注册的 provider 列表

func (*MultimodalHandler) HandleModerate added in v1.2.0

func (h *MultimodalHandler) HandleModerate(w http.ResponseWriter, r *http.Request)

HandleModerate 处理内容审核请求

func (*MultimodalHandler) HandleSynthesize added in v1.2.0

func (h *MultimodalHandler) HandleSynthesize(w http.ResponseWriter, r *http.Request)

HandleSynthesize 处理文本转语音 (TTS) 请求

func (*MultimodalHandler) HandleTranscribe added in v1.2.0

func (h *MultimodalHandler) HandleTranscribe(w http.ResponseWriter, r *http.Request)

HandleTranscribe 处理语音转文本 (STT) 请求

type ProtocolHandler added in v1.2.0

type ProtocolHandler struct {
	// contains filtered or unexported fields
}

ProtocolHandler handles MCP and A2A protocol API requests.

func NewProtocolHandler added in v1.2.0

func NewProtocolHandler(mcpServer mcp.MCPServer, a2aServer a2a.A2AServer, logger *zap.Logger) *ProtocolHandler

NewProtocolHandler creates a new protocol handler.

func (*ProtocolHandler) HandleA2AAgentCard added in v1.2.0

func (h *ProtocolHandler) HandleA2AAgentCard(w http.ResponseWriter, r *http.Request)

HandleA2AAgentCard handles GET /api/v1/a2a/.well-known/agent.json

func (*ProtocolHandler) HandleA2ASendTask added in v1.2.0

func (h *ProtocolHandler) HandleA2ASendTask(w http.ResponseWriter, r *http.Request)

HandleA2ASendTask handles POST /api/v1/a2a/tasks

func (*ProtocolHandler) HandleMCPCallTool added in v1.2.0

func (h *ProtocolHandler) HandleMCPCallTool(w http.ResponseWriter, r *http.Request)

HandleMCPCallTool handles POST /api/v1/mcp/tools/{name}

func (*ProtocolHandler) HandleMCPGetResource added in v1.2.0

func (h *ProtocolHandler) HandleMCPGetResource(w http.ResponseWriter, r *http.Request)

HandleMCPGetResource handles GET /api/v1/mcp/resources/{uri}

func (*ProtocolHandler) HandleMCPListResources added in v1.2.0

func (h *ProtocolHandler) HandleMCPListResources(w http.ResponseWriter, r *http.Request)

HandleMCPListResources handles GET /api/v1/mcp/resources

func (*ProtocolHandler) HandleMCPListTools added in v1.2.0

func (h *ProtocolHandler) HandleMCPListTools(w http.ResponseWriter, r *http.Request)

HandleMCPListTools handles GET /api/v1/mcp/tools

type RAGHandler added in v1.2.0

type RAGHandler struct {
	// contains filtered or unexported fields
}

RAGHandler handles RAG (Retrieval-Augmented Generation) API requests.

func NewRAGHandler added in v1.2.0

func NewRAGHandler(store rag.VectorStore, embedding rag.EmbeddingProvider, logger *zap.Logger) *RAGHandler

NewRAGHandler creates a new RAG handler.

func (*RAGHandler) HandleIndex added in v1.2.0

func (h *RAGHandler) HandleIndex(w http.ResponseWriter, r *http.Request)

HandleIndex handles POST /api/v1/rag/index

func (*RAGHandler) HandleQuery added in v1.2.0

func (h *RAGHandler) HandleQuery(w http.ResponseWriter, r *http.Request)

HandleQuery handles POST /api/v1/rag/query

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) Check

func (c *RedisHealthCheck) Check(ctx context.Context) error

func (*RedisHealthCheck) Name

func (c *RedisHealthCheck) Name() string

type Response

type Response = api.Response

Response is a type alias for api.Response — the canonical API envelope. The canonical definition lives in api/types.go (§38).

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) Hijack added in v1.1.0

func (rw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker so WebSocket upgrades work through wrapped ResponseWriters.

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 级别)是不同概念。

type WorkflowHandler added in v1.2.0

type WorkflowHandler struct {
	// contains filtered or unexported fields
}

WorkflowHandler handles workflow API requests.

func NewWorkflowHandler added in v1.2.0

func NewWorkflowHandler(executor *workflow.DAGExecutor, parser *dsl.Parser, logger *zap.Logger) *WorkflowHandler

NewWorkflowHandler creates a new workflow handler.

func (*WorkflowHandler) HandleExecute added in v1.2.0

func (h *WorkflowHandler) HandleExecute(w http.ResponseWriter, r *http.Request)

HandleExecute handles POST /api/v1/workflows/execute

func (*WorkflowHandler) HandleList added in v1.2.0

func (h *WorkflowHandler) HandleList(w http.ResponseWriter, r *http.Request)

HandleList handles GET /api/v1/workflows

func (*WorkflowHandler) HandleParse added in v1.2.0

func (h *WorkflowHandler) HandleParse(w http.ResponseWriter, r *http.Request)

HandleParse handles POST /api/v1/workflows/parse (validate DSL)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL