da9c8334d8
- SSE Keepalive Ping (15s心跳防止代理断连) - Timing HTTP 头 (X-Timing-Queue/Inference/Total-Ms) - Adapter Request-ID 传播到后端 - Session 清理日志回调 - Server 安全加固 (ReadHeaderTimeout/MaxHeaderBytes 防 slowloris) - Usage Tracker 数据保留清理 (retentionDays + 定期清理) - Config Reload 后 Adapter Registry 更新 (RegisterIfAbsent + RWMutex) - Rate Limiter 空闲 Bucket 清理 (30分钟过期) - Shutdown Drain 超时可配置 (ShutdownDrainSeconds) - Config 模型字段校验增强 (provider/endpoint/actual_model) - Auth 过期 Key 自动清理 (5分钟扫描) - Admin API Rate Limiting - Adapter Health Check 独立超时 (每个 adapter 3s) - TCP 连接阶段超时 (DialContext 5s + KeepAlive 30s) - 幂等键缓存、审计日志、Gzip 中间件、CORS Expose Headers - Backpressure 响应头、熔断器 Prometheus 指标 - 连接池优化、Trace-ID 全链路传播
114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
package api
|
|
|
|
// ChatRequest is the request body for POST /v1/chat/completions.
|
|
type ChatRequest struct {
|
|
Model string `json:"model"`
|
|
Messages []Message `json:"messages"`
|
|
Stream bool `json:"stream,omitempty"`
|
|
SessionID string `json:"session_id,omitempty"`
|
|
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
|
Priority string `json:"priority,omitempty"`
|
|
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
|
|
ContextPolicy string `json:"context_policy,omitempty"`
|
|
Timeouts *RequestTimeouts `json:"timeouts,omitempty"`
|
|
Routing *RoutingOptions `json:"routing,omitempty"`
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
TopP *float64 `json:"top_p,omitempty"`
|
|
}
|
|
|
|
type Message struct {
|
|
Role string `json:"role"`
|
|
Content any `json:"content"` // string or []ContentPart for vision
|
|
}
|
|
|
|
type RequestTimeouts struct {
|
|
QueueMs int `json:"queue_ms,omitempty"`
|
|
FirstTokenMs int `json:"first_token_ms,omitempty"`
|
|
InferenceMs int `json:"inference_ms,omitempty"`
|
|
TotalMs int `json:"total_ms,omitempty"`
|
|
}
|
|
|
|
type RoutingOptions struct {
|
|
LocalOnly bool `json:"local_only,omitempty"`
|
|
AllowSmallerModel bool `json:"allow_smaller_model,omitempty"`
|
|
}
|
|
|
|
// ChatResponse is the non-streaming response.
|
|
type ChatResponse struct {
|
|
RequestID string `json:"request_id"`
|
|
TaskID string `json:"task_id"`
|
|
SessionID string `json:"session_id,omitempty"`
|
|
Status string `json:"status"`
|
|
Model string `json:"model"`
|
|
Choices []Choice `json:"choices"`
|
|
LogicalModel string `json:"logical_model"`
|
|
ActualModel string `json:"actual_model"`
|
|
NodeID string `json:"node_id,omitempty"`
|
|
Usage *Usage `json:"usage,omitempty"`
|
|
Timing *Timing `json:"timing,omitempty"`
|
|
Degraded bool `json:"degraded,omitempty"`
|
|
}
|
|
|
|
type Choice struct {
|
|
Index int `json:"index"`
|
|
Message *Message `json:"message,omitempty"`
|
|
Delta *Message `json:"delta,omitempty"`
|
|
FinishReason string `json:"finish_reason,omitempty"`
|
|
}
|
|
|
|
type Usage struct {
|
|
InputTokens int `json:"input_tokens"`
|
|
OutputTokens int `json:"output_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
}
|
|
|
|
type Timing struct {
|
|
QueueMs int `json:"queue_ms"`
|
|
FirstTokenMs int `json:"first_token_ms"`
|
|
InferenceMs int `json:"inference_ms"`
|
|
TotalMs int `json:"total_ms"`
|
|
}
|
|
|
|
// ErrorResponse is the unified error response format.
|
|
type ErrorResponse struct {
|
|
Error ErrorBody `json:"error"`
|
|
}
|
|
|
|
type ErrorBody struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
}
|
|
|
|
// ModelListResponse is the response for GET /v1/models.
|
|
type ModelListResponse struct {
|
|
Object string `json:"object"`
|
|
Data []ModelInfo `json:"data"`
|
|
}
|
|
|
|
type ModelInfo struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object"`
|
|
OwnedBy string `json:"owned_by"`
|
|
Provider string `json:"provider,omitempty"`
|
|
ContextWindow int `json:"context_window,omitempty"`
|
|
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
|
|
}
|
|
|
|
// SessionRequest is the request body for POST /v1/sessions.
|
|
type SessionRequest struct {
|
|
ApplicationID string `json:"application_id"`
|
|
UserID string `json:"user_id,omitempty"`
|
|
Config map[string]any `json:"config,omitempty"`
|
|
}
|
|
|
|
// SessionResponse is the response for session operations.
|
|
type SessionResponse struct {
|
|
SessionID string `json:"session_id"`
|
|
ApplicationID string `json:"application_id"`
|
|
UserID string `json:"user_id,omitempty"`
|
|
CreatedAt string `json:"created_at"`
|
|
LastActive string `json:"last_active"`
|
|
}
|