引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
157 lines
4.5 KiB
Go
157 lines
4.5 KiB
Go
package canonical
|
|
|
|
// StreamEventType 流式事件类型枚举
|
|
type StreamEventType string
|
|
|
|
const (
|
|
EventMessageStart StreamEventType = "message_start"
|
|
EventContentBlockStart StreamEventType = "content_block_start"
|
|
EventContentBlockDelta StreamEventType = "content_block_delta"
|
|
EventContentBlockStop StreamEventType = "content_block_stop"
|
|
EventMessageDelta StreamEventType = "message_delta"
|
|
EventMessageStop StreamEventType = "message_stop"
|
|
EventError StreamEventType = "error"
|
|
EventPing StreamEventType = "ping"
|
|
)
|
|
|
|
// DeltaType 增量类型枚举
|
|
type DeltaType string
|
|
|
|
const (
|
|
DeltaTypeText DeltaType = "text_delta"
|
|
DeltaTypeInputJSON DeltaType = "input_json_delta"
|
|
DeltaTypeThinking DeltaType = "thinking_delta"
|
|
)
|
|
|
|
// StreamDelta 流式增量联合体
|
|
type StreamDelta struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text,omitempty"`
|
|
PartialJSON string `json:"partial_json,omitempty"`
|
|
Thinking string `json:"thinking,omitempty"`
|
|
}
|
|
|
|
// StreamContentBlock 流式内容块联合体
|
|
type StreamContentBlock struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text,omitempty"`
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Thinking string `json:"thinking,omitempty"`
|
|
}
|
|
|
|
// CanonicalStreamEvent 规范流式事件联合体
|
|
type CanonicalStreamEvent struct {
|
|
Type StreamEventType `json:"type"`
|
|
|
|
// MessageStartEvent
|
|
Message *StreamMessage `json:"message,omitempty"`
|
|
|
|
// ContentBlockStartEvent / ContentBlockDeltaEvent / ContentBlockStopEvent
|
|
Index *int `json:"index,omitempty"`
|
|
ContentBlock *StreamContentBlock `json:"content_block,omitempty"`
|
|
Delta *StreamDelta `json:"delta,omitempty"`
|
|
|
|
// MessageDeltaEvent
|
|
StopReason *StopReason `json:"stop_reason,omitempty"`
|
|
Usage *CanonicalUsage `json:"usage,omitempty"`
|
|
|
|
// ErrorEvent
|
|
Error *StreamError `json:"error,omitempty"`
|
|
}
|
|
|
|
// StreamMessage 流式消息摘要
|
|
type StreamMessage struct {
|
|
ID string `json:"id"`
|
|
Model string `json:"model"`
|
|
Usage *CanonicalUsage `json:"usage,omitempty"`
|
|
}
|
|
|
|
// StreamError 流式错误
|
|
type StreamError struct {
|
|
Type string `json:"type"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// NewMessageStartEvent 创建消息开始事件
|
|
func NewMessageStartEvent(id, model string) CanonicalStreamEvent {
|
|
return CanonicalStreamEvent{
|
|
Type: EventMessageStart,
|
|
Message: &StreamMessage{ID: id, Model: model},
|
|
}
|
|
}
|
|
|
|
// NewMessageStartEventWithUsage 创建带用量的消息开始事件
|
|
func NewMessageStartEventWithUsage(id, model string, usage *CanonicalUsage) CanonicalStreamEvent {
|
|
return CanonicalStreamEvent{
|
|
Type: EventMessageStart,
|
|
Message: &StreamMessage{ID: id, Model: model, Usage: usage},
|
|
}
|
|
}
|
|
|
|
// NewContentBlockStartEvent 创建内容块开始事件
|
|
func NewContentBlockStartEvent(index int, block StreamContentBlock) CanonicalStreamEvent {
|
|
idx := index
|
|
return CanonicalStreamEvent{
|
|
Type: EventContentBlockStart,
|
|
Index: &idx,
|
|
ContentBlock: &block,
|
|
}
|
|
}
|
|
|
|
// NewContentBlockDeltaEvent 创建内容块增量事件
|
|
func NewContentBlockDeltaEvent(index int, delta StreamDelta) CanonicalStreamEvent {
|
|
idx := index
|
|
return CanonicalStreamEvent{
|
|
Type: EventContentBlockDelta,
|
|
Index: &idx,
|
|
Delta: &delta,
|
|
}
|
|
}
|
|
|
|
// NewContentBlockStopEvent 创建内容块结束事件
|
|
func NewContentBlockStopEvent(index int) CanonicalStreamEvent {
|
|
idx := index
|
|
return CanonicalStreamEvent{
|
|
Type: EventContentBlockStop,
|
|
Index: &idx,
|
|
}
|
|
}
|
|
|
|
// NewMessageDeltaEvent 创建消息增量事件
|
|
func NewMessageDeltaEvent(stopReason StopReason) CanonicalStreamEvent {
|
|
sr := stopReason
|
|
return CanonicalStreamEvent{
|
|
Type: EventMessageDelta,
|
|
StopReason: &sr,
|
|
}
|
|
}
|
|
|
|
// NewMessageDeltaEventWithUsage 创建带用量的消息增量事件
|
|
func NewMessageDeltaEventWithUsage(stopReason StopReason, usage *CanonicalUsage) CanonicalStreamEvent {
|
|
sr := stopReason
|
|
return CanonicalStreamEvent{
|
|
Type: EventMessageDelta,
|
|
StopReason: &sr,
|
|
Usage: usage,
|
|
}
|
|
}
|
|
|
|
// NewMessageStopEvent 创建消息结束事件
|
|
func NewMessageStopEvent() CanonicalStreamEvent {
|
|
return CanonicalStreamEvent{Type: EventMessageStop}
|
|
}
|
|
|
|
// NewErrorEvent 创建错误事件
|
|
func NewErrorEvent(errType, message string) CanonicalStreamEvent {
|
|
return CanonicalStreamEvent{
|
|
Type: EventError,
|
|
Error: &StreamError{Type: errType, Message: message},
|
|
}
|
|
}
|
|
|
|
// NewPingEvent 创建心跳事件
|
|
func NewPingEvent() CanonicalStreamEvent {
|
|
return CanonicalStreamEvent{Type: EventPing}
|
|
}
|