protoconv

package
v0.0.1-beta.8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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

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

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)——chat→resp 等 方向从不使用,免每流 6 个 map 分配。

func (*StreamMapper) BindableID

func (m *StreamMapper) BindableID() string

BindableID 显式返回当前 mapper 已产出的**客户端可续接响应 id**(即映射帧 中作为 response.id 内嵌的那个 id;resp→mess 方向取自上游 message.id),在首个 携带 id 的事件(message_start)之前为空串。续接绑定的调用方在写出接缝处 读取本方法,而非反向解析映射帧字节——映射帧的 response_id/嵌套结构不构成 可绑定 id 的权威(连续绑定键必须与真实响应对象 id 同源)。

func (*StreamMapper) Done

func (m *StreamMapper) Done() bool

Done 报告 mapper 是否已产出目标协议终止帧(completed / failed 事件置位—— 见各方向 map 的 m.done 去重)。codex converted 适配层在流末据此判定是否需 补发终止帧(恰补一次:源协议仅 [DONE] 或无 [DONE] 正常 EOF 时 mapper 未产 终止帧 → 补发)。

func (*StreamMapper) Finish

func (m *StreamMapper) Finish() []byte

Finish 按方向返回**与目标 mapper 自产终止相同**的终止帧(流末补发用)。 仅 codex converted 命中的两方向有定义:

  • MessToResp(client=Messages)→ mirror mapRespToMess 的 response.completed 收尾:message_delta{stop_reason:"", stop_sequence:nil, usage:respUsageToMess(nil)} ++ message_stop;
  • ChatToResp(client=Chat)→ mirror mapRespToChat 终止:`data: [DONE]\n\n`。

其余方向 → nil(非 codex native converted 不在适配层覆盖内)。调用方须先查 Done(),仅在目标终止帧尚未产生时补发。

func (*StreamMapper) Map

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

Map 把一个模板协议 SSE 事件映射为客户端协议帧;drop=true 丢弃该帧。 缺 event: 名(data-only)帧不丢: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