- 流式转换补齐 chat→responses / responses→chat / messages→responses 的工具调用(此前只转文本,SDK 客户端拿不到 tool_calls);completed 事件携带完整 output 数组 - Anthropic 协议上游同时发 x-api-key(官方 API 不接受 API key 走 Bearer)并透传 anthropic-beta - 请求侧校验模型存在且启用:禁用模型 404 model_not_found,不再只是从 /v1/models 消失、实际照发照扣费;空模型名 400 - 模型在请求中途被删时按 0 记账留痕,不再按硬编码示例价扣用户的钱 - finishUsage 新增一行式请求日志(trace/user/model/channel/tokens/cost/status/latency),记账是异步的,日志是唯一即时排障依据
180 lines
4.8 KiB
Go
180 lines
4.8 KiB
Go
package proxy
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/openteam/server/internal/proxy/convert"
|
||
"github.com/openteam/server/internal/store"
|
||
)
|
||
|
||
// failBodyRead 读取请求体失败时的报错:超限单独回 413,让客户端能区分"体太大"和"体畸形"。
|
||
func (g *Gateway) failBodyRead(c *gin.Context, err error) {
|
||
var mb *http.MaxBytesError
|
||
if errors.As(err, &mb) {
|
||
apiError(c, http.StatusRequestEntityTooLarge, "request_too_large",
|
||
fmt.Sprintf("请求体超过网关上限 %d MB", g.maxBodyMB))
|
||
return
|
||
}
|
||
apiError(c, http.StatusBadRequest, "invalid_request_error", "failed to read request body")
|
||
}
|
||
|
||
// chatCompletions POST /v1/chat/completions
|
||
func (g *Gateway) chatCompletions(c *gin.Context) {
|
||
u, ok := g.resolveUser(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
if !g.checkBalance(c, u) {
|
||
return
|
||
}
|
||
br, body, err := parseBody(c)
|
||
if err != nil {
|
||
g.failBodyRead(c, err)
|
||
return
|
||
}
|
||
c.Set("protocol", convert.ProtoChat)
|
||
c.Set("model_name", br.Model)
|
||
g.recordRawRequest(c, u, body)
|
||
if !g.checkModelExists(c, br.Model) {
|
||
return
|
||
}
|
||
if !g.checkModelAllowed(c, u, br.Model) {
|
||
apiError(c, http.StatusForbidden, "model_not_allowed", "模型未对你开放,请联系管理员")
|
||
return
|
||
}
|
||
|
||
cands := g.candidateChannels(br.Model)
|
||
if len(cands) == 0 {
|
||
apiError(c, http.StatusServiceUnavailable, "no_channel", "No available upstream channel")
|
||
g.recordError(c, nil, nil, now(), "no_channel")
|
||
return
|
||
}
|
||
sink := &usageSink{}
|
||
c.Set("usage_raw", &sinkHolder{sink: sink})
|
||
g.doProxy(c, cands, convert.ProtoChat, body, br.Stream, sink)
|
||
}
|
||
|
||
// responses POST /v1/responses(OpenAI Responses API)
|
||
func (g *Gateway) responses(c *gin.Context) {
|
||
u, ok := g.resolveUser(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
if !g.checkBalance(c, u) {
|
||
return
|
||
}
|
||
br, body, err := parseBody(c)
|
||
if err != nil {
|
||
g.failBodyRead(c, err)
|
||
return
|
||
}
|
||
c.Set("protocol", convert.ProtoResponses)
|
||
c.Set("model_name", br.Model)
|
||
g.recordRawRequest(c, u, body)
|
||
if !g.checkModelExists(c, br.Model) {
|
||
return
|
||
}
|
||
if !g.checkModelAllowed(c, u, br.Model) {
|
||
apiError(c, http.StatusForbidden, "model_not_allowed", "模型未对你开放,请联系管理员")
|
||
return
|
||
}
|
||
|
||
cands := g.candidateChannels(br.Model)
|
||
if len(cands) == 0 {
|
||
apiError(c, http.StatusServiceUnavailable, "no_channel", "No available upstream channel")
|
||
g.recordError(c, nil, nil, now(), "no_channel")
|
||
return
|
||
}
|
||
sink := &usageSink{}
|
||
c.Set("usage_raw", &sinkHolder{sink: sink})
|
||
g.doProxy(c, cands, convert.ProtoResponses, body, br.Stream, sink)
|
||
}
|
||
|
||
// messages POST /v1/messages(Anthropic Messages API)
|
||
func (g *Gateway) messages(c *gin.Context) {
|
||
u, ok := g.resolveUser(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
if !g.checkBalance(c, u) {
|
||
return
|
||
}
|
||
br, body, err := parseBody(c)
|
||
if err != nil {
|
||
g.failBodyRead(c, err)
|
||
return
|
||
}
|
||
c.Set("protocol", convert.ProtoMessages)
|
||
c.Set("model_name", br.Model)
|
||
g.recordRawRequest(c, u, body)
|
||
if !g.checkModelExists(c, br.Model) {
|
||
return
|
||
}
|
||
if !g.checkModelAllowed(c, u, br.Model) {
|
||
apiError(c, http.StatusForbidden, "model_not_allowed", "模型未对你开放,请联系管理员")
|
||
return
|
||
}
|
||
|
||
cands := g.candidateChannels(br.Model)
|
||
if len(cands) == 0 {
|
||
apiError(c, http.StatusServiceUnavailable, "no_channel", "No available upstream channel")
|
||
g.recordError(c, nil, nil, now(), "no_channel")
|
||
return
|
||
}
|
||
sink := &usageSink{}
|
||
c.Set("usage_raw", &sinkHolder{sink: sink})
|
||
g.doProxy(c, cands, convert.ProtoMessages, body, br.Stream, sink)
|
||
}
|
||
|
||
// usageSinkHolder 桥接:gin context 里保存 sink 引用,供 finishUsage 读取最终 usage。
|
||
type sinkHolder struct {
|
||
sink *usageSink
|
||
}
|
||
|
||
// recordRawRequest 记录管理员原始请求体到 context(供 finishUsage 落库)。
|
||
// 仅当开关开启且用户为管理员时记录;响应侧以 c.Get("raw_request") 是否非空判断是否需要捕获响应。
|
||
func (g *Gateway) recordRawRequest(c *gin.Context, u *store.User, body []byte) {
|
||
if g.logRaw && u.Role == store.RoleAdmin {
|
||
c.Set("raw_request", string(body))
|
||
}
|
||
}
|
||
|
||
// apiError 按客户端协议返回错误体(PLANNING §5.1.4)。
|
||
func apiError(c *gin.Context, status int, code, message string) {
|
||
if p, _ := c.Get("protocol"); p == convert.ProtoMessages {
|
||
// Anthropic 格式
|
||
c.AbortWithStatusJSON(status, gin.H{
|
||
"type": "error",
|
||
"error": gin.H{"type": errorTypeFor(status), "message": message},
|
||
})
|
||
return
|
||
}
|
||
// OpenAI 格式
|
||
c.AbortWithStatusJSON(status, gin.H{
|
||
"error": gin.H{
|
||
"message": message,
|
||
"type": errorTypeFor(status),
|
||
"param": nil,
|
||
"code": code,
|
||
},
|
||
})
|
||
}
|
||
|
||
func errorTypeFor(status int) string {
|
||
switch status {
|
||
case http.StatusUnauthorized:
|
||
return "authentication_error"
|
||
case http.StatusForbidden, http.StatusPaymentRequired:
|
||
return "permission_error"
|
||
case http.StatusNotFound, http.StatusBadRequest:
|
||
return "invalid_request_error"
|
||
case http.StatusTooManyRequests:
|
||
return "rate_limit_error"
|
||
default:
|
||
return "api_error"
|
||
}
|
||
}
|