protoconv

package
v0.0.1-beta.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package protoconv 提供网关级协议转换(W5,只补差语义):客户端协议请求体 → 模板协议请求体(ConvertRequest)、模板协议响应 → 客户端协议响应 (ConvertResponse 非流式 JSON / StreamMapper 流式 SSE 事件映射)。

边界(用户拍板):转换器是网关能力,标准库实现(encoding/json),与 OpenAI/Anthropic SDK 零耦合(缺名帧事件名推断与 sserelay 共用 InferEventName——热路径零分配锚定 + 回退全量解码);四方向分派按 groups.protocol_convert 快照值(off 不经过本包——热路径分支在 internal/proxy 判定);WS 帧流转换不做(resp-ws 1:1 透传,W3 范围)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertRequest

func ConvertRequest(body []byte, dir domain.ProtocolConvert) ([]byte, error)

ConvertRequest 把客户端协议请求体转换为 dir 指向的模板协议请求体(返回的 字节即模板协议上游请求体,可直接转发)。转换器按目标协议规范映射字段; 目标协议无对应参数的字段(如 chat 的 frequency_penalty → resp 无此参数) 按规范丢弃。model 恒保持(调度器 ModelMapping 在转换后改写)。

func ConvertResponse

func ConvertResponse(body []byte, dir domain.ProtocolConvert) ([]byte, error)

ConvertResponse 把模板协议的非流式响应 JSON 转换为客户端协议响应 JSON。

func EncodeFrame

func EncodeFrame(name string, data any) []byte

EncodeFrame 组装 SSE 帧字节:name 非空 → "event: name\n" 行;data 为 JSON 载荷(marshal 为单行 data)。载荷不可 marshal(转换器仅产出 map/string 等 可序列化值)→ 返回 nil,调用方按丢弃处理。

Types

type StreamMapper

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

StreamMapper 按方向把模板协议的 SSE 事件映射为客户端协议帧([]byte 即完整 帧字节,含 event/data 行与结尾空行)。映射帧复用 mapper 内缓冲(buf/dbuf), 生命周期仅限本次 Map 调用——调用方(sserelay Mapper 契约)立即写出后丢弃, 下一帧复用同一缓冲,逐帧零分配。chat→resp 方向为字节级组装,其余方向仍 为 map 组装(EncodeFrame)。

func NewStreamMapper

func NewStreamMapper(dir domain.ProtocolConvert) *StreamMapper

NewStreamMapper 构造有状态的流式响应事件映射器(每 SSE 流一个实例; 跨事件状态:id/model、用量累积、mess→resp 的块级输出累积)。块级累积 map(blockStarted 等)懒初始化(ensureBlocks,评审 I-4)——chat→resp 等 方向从不使用,免每流 6 个 map 分配。

func (*StreamMapper) Map

func (m *StreamMapper) Map(name string, data []byte) ([]byte, bool)

Map 把一个模板协议 SSE 事件映射为客户端协议帧;drop=true 丢弃该帧。 缺 event: 名(data-only)帧不丢(P3):data 为 JSON 对象且含字符串 type 字段时按该值推断事件名(resp/messages 帧 type 与事件名同值约定,非规范 上游如仓库 fakeupstream /v1/responses 缺 event: 行),推断出 → 与具名帧 同分派;无法推断(非 JSON / 无 type 字段)→ 原样透传 data 帧保留字节。 空帧(无字节可透传)→ 丢弃。chat 的 [DONE] 等终止帧由映射器自行产出, 透传仅兜底非规范上游。

Jump to

Keyboard shortcuts

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