refactor: 实现 ConversionEngine 协议转换引擎,替代旧 protocol 包
引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
This commit is contained in:
@@ -11,15 +11,15 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"nex/backend/internal/protocol/openai"
|
||||
"nex/backend/internal/conversion"
|
||||
)
|
||||
|
||||
// StreamConfig 流式处理配置
|
||||
type StreamConfig struct {
|
||||
InitialBufferSize int // 初始缓冲区大小(字节),默认 4096
|
||||
MaxBufferSize int // 最大缓冲区大小(字节),默认 65536
|
||||
Timeout time.Duration // 流超时时间,默认 5 分钟
|
||||
ChannelBufferSize int // 事件通道缓冲区大小,默认 100
|
||||
InitialBufferSize int
|
||||
MaxBufferSize int
|
||||
Timeout time.Duration
|
||||
ChannelBufferSize int
|
||||
}
|
||||
|
||||
// DefaultStreamConfig 返回默认流式处理配置
|
||||
@@ -32,14 +32,6 @@ func DefaultStreamConfig() StreamConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// Client OpenAI 兼容供应商客户端
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
adapter *openai.Adapter
|
||||
logger *zap.Logger
|
||||
streamCfg StreamConfig
|
||||
}
|
||||
|
||||
// StreamEvent 流事件
|
||||
type StreamEvent struct {
|
||||
Data []byte
|
||||
@@ -47,10 +39,17 @@ type StreamEvent struct {
|
||||
Done bool
|
||||
}
|
||||
|
||||
// Client 协议无关的供应商客户端
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
logger *zap.Logger
|
||||
streamCfg StreamConfig
|
||||
}
|
||||
|
||||
// ProviderClient 供应商客户端接口
|
||||
type ProviderClient interface {
|
||||
SendRequest(ctx context.Context, req *openai.ChatCompletionRequest, apiKey, baseURL string) (*openai.ChatCompletionResponse, error)
|
||||
SendStreamRequest(ctx context.Context, req *openai.ChatCompletionRequest, apiKey, baseURL string) (<-chan StreamEvent, error)
|
||||
Send(ctx context.Context, spec conversion.HTTPRequestSpec) (*conversion.HTTPResponseSpec, error)
|
||||
SendStream(ctx context.Context, spec conversion.HTTPRequestSpec) (<-chan StreamEvent, error)
|
||||
}
|
||||
|
||||
// NewClient 创建供应商客户端
|
||||
@@ -59,97 +58,98 @@ func NewClient() *Client {
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
adapter: openai.NewAdapter(),
|
||||
logger: zap.L(),
|
||||
streamCfg: DefaultStreamConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
// SendRequest 发送非流式请求
|
||||
func (c *Client) SendRequest(ctx context.Context, req *openai.ChatCompletionRequest, apiKey, baseURL string) (*openai.ChatCompletionResponse, error) {
|
||||
// 准备请求
|
||||
httpReq, err := c.adapter.PrepareRequest(req, apiKey, baseURL)
|
||||
// Send 发送非流式请求
|
||||
func (c *Client) Send(ctx context.Context, spec conversion.HTTPRequestSpec) (*conversion.HTTPResponseSpec, error) {
|
||||
var bodyReader io.Reader
|
||||
if len(spec.Body) > 0 {
|
||||
bodyReader = bytes.NewReader(spec.Body)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(ctx, spec.Method, spec.URL, bodyReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("准备请求失败: %w", err)
|
||||
return nil, fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
|
||||
for k, v := range spec.Headers {
|
||||
httpReq.Header.Set(k, v)
|
||||
}
|
||||
|
||||
c.logger.Debug("发送请求",
|
||||
zap.String("url", httpReq.URL.String()),
|
||||
zap.String("method", httpReq.Method),
|
||||
zap.String("url", spec.URL),
|
||||
zap.String("method", spec.Method),
|
||||
)
|
||||
|
||||
// 设置上下文
|
||||
httpReq = httpReq.WithContext(ctx)
|
||||
|
||||
// 发送请求
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("发送请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// 解析错误响应
|
||||
errorResp, parseErr := c.adapter.ParseErrorResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, fmt.Errorf("供应商返回错误: HTTP %d", resp.StatusCode)
|
||||
}
|
||||
return nil, fmt.Errorf("供应商错误: %s", errorResp.Error.Message)
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
result, err := c.adapter.ParseResponse(resp)
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
return nil, fmt.Errorf("读取响应失败: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
respHeaders := make(map[string]string)
|
||||
for k, vs := range resp.Header {
|
||||
if len(vs) > 0 {
|
||||
respHeaders[k] = vs[0]
|
||||
}
|
||||
}
|
||||
|
||||
return &conversion.HTTPResponseSpec{
|
||||
StatusCode: resp.StatusCode,
|
||||
Headers: respHeaders,
|
||||
Body: respBody,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SendStreamRequest 发送流式请求
|
||||
func (c *Client) SendStreamRequest(ctx context.Context, req *openai.ChatCompletionRequest, apiKey, baseURL string) (<-chan StreamEvent, error) {
|
||||
// 确保请求设置为流式
|
||||
req.Stream = true
|
||||
|
||||
// 准备请求
|
||||
httpReq, err := c.adapter.PrepareRequest(req, apiKey, baseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("准备请求失败: %w", err)
|
||||
// SendStream 发送流式请求
|
||||
func (c *Client) SendStream(ctx context.Context, spec conversion.HTTPRequestSpec) (<-chan StreamEvent, error) {
|
||||
var bodyReader io.Reader
|
||||
if len(spec.Body) > 0 {
|
||||
bodyReader = bytes.NewReader(spec.Body)
|
||||
}
|
||||
|
||||
// 设置带超时的上下文
|
||||
streamCtx, cancel := context.WithTimeout(ctx, c.streamCfg.Timeout)
|
||||
_ = cancel // cancel 在流读取结束后由 ctx 传播处理
|
||||
httpReq = httpReq.WithContext(streamCtx)
|
||||
httpReq, err := http.NewRequestWithContext(streamCtx, spec.Method, spec.URL, bodyReader)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
|
||||
for k, v := range spec.Headers {
|
||||
httpReq.Header.Set(k, v)
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("发送请求失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
defer resp.Body.Close()
|
||||
cancel()
|
||||
errorResp, parseErr := c.adapter.ParseErrorResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, fmt.Errorf("供应商返回错误: HTTP %d", resp.StatusCode)
|
||||
errBody, _ := io.ReadAll(resp.Body)
|
||||
if len(errBody) > 0 {
|
||||
return nil, fmt.Errorf("供应商返回错误: HTTP %d: %s", resp.StatusCode, string(errBody))
|
||||
}
|
||||
return nil, fmt.Errorf("供应商错误: %s", errorResp.Error.Message)
|
||||
return nil, fmt.Errorf("供应商返回错误: HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 创建事件通道
|
||||
eventChan := make(chan StreamEvent, c.streamCfg.ChannelBufferSize)
|
||||
|
||||
// 启动 goroutine 读取流
|
||||
go c.readStream(streamCtx, cancel, resp.Body, eventChan)
|
||||
|
||||
return eventChan, nil
|
||||
}
|
||||
|
||||
// readStream 读取 SSE 流(支持动态缓冲区、超时控制和改进的错误处理)
|
||||
// readStream 读取 SSE 流
|
||||
func (c *Client) readStream(ctx context.Context, cancel context.CancelFunc, body io.ReadCloser, eventChan chan<- StreamEvent) {
|
||||
defer close(eventChan)
|
||||
defer body.Close()
|
||||
@@ -175,10 +175,8 @@ func (c *Client) readStream(ctx context.Context, cancel context.CancelFunc, body
|
||||
n, err := body.Read(buf)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
// 流正常结束
|
||||
return
|
||||
}
|
||||
// 区分网络错误和其他错误
|
||||
if isNetworkError(err) {
|
||||
c.logger.Error("流网络错误", zap.String("error", err.Error()))
|
||||
eventChan <- StreamEvent{Error: fmt.Errorf("网络错误: %w", err)}
|
||||
@@ -191,7 +189,6 @@ func (c *Client) readStream(ctx context.Context, cancel context.CancelFunc, body
|
||||
|
||||
dataBuf = append(dataBuf, buf[:n]...)
|
||||
|
||||
// 动态调整缓冲区大小:如果数据量大,增大缓冲区
|
||||
if len(dataBuf) > bufSize/2 && bufSize < c.streamCfg.MaxBufferSize {
|
||||
newSize := bufSize * 2
|
||||
if newSize > c.streamCfg.MaxBufferSize {
|
||||
@@ -201,34 +198,21 @@ func (c *Client) readStream(ctx context.Context, cancel context.CancelFunc, body
|
||||
bufSize = newSize
|
||||
}
|
||||
|
||||
// 处理完整的 SSE 事件
|
||||
for {
|
||||
// 查找事件边界(双换行)
|
||||
idx := bytes.Index(dataBuf, []byte("\n\n"))
|
||||
if idx == -1 {
|
||||
break
|
||||
}
|
||||
|
||||
// 提取事件
|
||||
event := dataBuf[:idx]
|
||||
rawEvent := dataBuf[:idx]
|
||||
dataBuf = dataBuf[idx+2:]
|
||||
|
||||
// 解析 data 行
|
||||
lines := strings.Split(string(event), "\n")
|
||||
for _, line := range lines {
|
||||
if strings.HasPrefix(line, "data: ") {
|
||||
data := strings.TrimPrefix(line, "data: ")
|
||||
|
||||
// 检查是否是结束标记
|
||||
if data == "[DONE]" {
|
||||
eventChan <- StreamEvent{Done: true}
|
||||
return
|
||||
}
|
||||
|
||||
// 发送数据
|
||||
eventChan <- StreamEvent{Data: []byte(data)}
|
||||
}
|
||||
if bytes.Contains(rawEvent, []byte("data: [DONE]")) {
|
||||
eventChan <- StreamEvent{Done: true}
|
||||
return
|
||||
}
|
||||
|
||||
eventChan <- StreamEvent{Data: rawEvent}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,4 +229,3 @@ func isNetworkError(err error) bool {
|
||||
strings.Contains(errStr, "timeout") ||
|
||||
strings.Contains(errStr, "EOF")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user