mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: harden cross-protocol streaming semantics
This commit is contained in:
1 parent
4f0f26711a
commit
3776da76ec
3 files changed
+185
-27
No files matched your search
@@ -328,6 +328,10 @@ socks5://127.0.0.1:1080 # 备用代理
|
||||
|
||||
跨协议桥接会区分 Chat/Responses 的 system 与 developer 指令,在 Anthropic 目标中按顺序合并为 system 内容;reasoning effort 会转换为兼容 thinking 预算。工具选择、空参数 `{}`、停止原因,以及 SSE 中延迟到达的工具名称、参数分片和完成事件也会转换到目标协议的对应形态。
|
||||
|
||||
流式响应会兼容 Chat 上游的 `delta.reasoning_content` 与 `delta.reasoning`。Anthropic 上游的 `event: error`、Responses 上游的 `response.failed` 以及 Chat 的错误类 `finish_reason` 会转换为目标协议的结构化错误事件,不会伪装成正常结束或静默断流。`redacted_thinking` 在支持加密 reasoning 的 Responses 目标中保留 `encrypted_content`,在无法表达加密块的 Chat/Anthropic 目标中使用 `[redacted thinking]` 明确占位。
|
||||
|
||||
协议文档未定义或当前 bridge 无法无损表达的输入 content block(例如未实现的音频/文件类型)会返回明确的转换错误,不再静默丢弃内容。
|
||||
|
||||
### `performance`
|
||||
|
||||
| 字段 | 含义 |
|
||||
|
||||
+105
-21
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -84,6 +85,7 @@ type bridgeResponse struct {
|
||||
Reasoning []bridgeBlock
|
||||
Tools []bridgeBlock
|
||||
Stop string
|
||||
Error string
|
||||
Usage bridgeUsage
|
||||
Created int64
|
||||
}
|
||||
@@ -295,7 +297,10 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
return request, fmt.Errorf("messages[%d] must be an object", i)
|
||||
}
|
||||
role := stringAt(message, "role")
|
||||
blocks := decodeOpenAIBlocks(message["content"])
|
||||
blocks, err := decodeOpenAIBlocksChecked(message["content"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("messages[%d]: %w", i, err)
|
||||
}
|
||||
if role == "assistant" {
|
||||
blocks = append(decodeChatReasoning(message), blocks...)
|
||||
}
|
||||
@@ -348,7 +353,11 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
request.MaxTokens = input["max_output_tokens"]
|
||||
request.Stop = input["stop"]
|
||||
request.Reasoning = firstAny(input["reasoning"], input["reasoning_effort"])
|
||||
request.System = append(request.System, decodeOpenAIBlocks(input["instructions"])...)
|
||||
instructions, err := decodeOpenAIBlocksChecked(input["instructions"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("instructions: %w", err)
|
||||
}
|
||||
request.System = append(request.System, instructions...)
|
||||
switch value := input["input"].(type) {
|
||||
case string:
|
||||
request.Messages = append(request.Messages, bridgeMessage{Role: "user", Blocks: []bridgeBlock{{Kind: "text", Text: value}}})
|
||||
@@ -378,7 +387,10 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
})
|
||||
case "message", "":
|
||||
role := stringAt(item, "role")
|
||||
blocks := decodeOpenAIBlocks(item["content"])
|
||||
blocks, err := decodeOpenAIBlocksChecked(item["content"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("input[%d]: %w", i, err)
|
||||
}
|
||||
if role == "system" {
|
||||
request.System = append(request.System, blocks...)
|
||||
} else if role == "developer" {
|
||||
@@ -409,7 +421,11 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
request.MaxTokens = input["max_tokens"]
|
||||
request.Stop = input["stop_sequences"]
|
||||
request.Reasoning = firstAny(input["thinking"], anyAt(input, "output_config", "effort"), input["effort"])
|
||||
request.System = decodeAnthropicBlocks(input["system"])
|
||||
blocks, err := decodeAnthropicBlocksChecked(input["system"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("system: %w", err)
|
||||
}
|
||||
request.System = blocks
|
||||
for i, raw := range sliceAt(input, "messages") {
|
||||
message, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
@@ -420,17 +436,29 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
// into the messages array instead of using the top-level system
|
||||
// field. Fold them into the system prompt rather than rejecting.
|
||||
if role == "system" {
|
||||
request.System = append(request.System, decodeAnthropicBlocks(message["content"])...)
|
||||
blocks, err := decodeAnthropicBlocksChecked(message["content"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("messages[%d]: %w", i, err)
|
||||
}
|
||||
request.System = append(request.System, blocks...)
|
||||
continue
|
||||
}
|
||||
if role == "developer" {
|
||||
request.Developer = append(request.Developer, decodeAnthropicBlocks(message["content"])...)
|
||||
blocks, err := decodeAnthropicBlocksChecked(message["content"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("messages[%d]: %w", i, err)
|
||||
}
|
||||
request.Developer = append(request.Developer, blocks...)
|
||||
continue
|
||||
}
|
||||
if role != "user" && role != "assistant" {
|
||||
return request, fmt.Errorf("messages[%d] has unsupported role %q", i, role)
|
||||
}
|
||||
request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: decodeAnthropicBlocks(message["content"])})
|
||||
blocks, err := decodeAnthropicBlocksChecked(message["content"])
|
||||
if err != nil {
|
||||
return request, fmt.Errorf("messages[%d]: %w", i, err)
|
||||
}
|
||||
request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: blocks})
|
||||
}
|
||||
for i, raw := range sliceAt(input, "tools") {
|
||||
tool, ok := raw.(map[string]any)
|
||||
@@ -820,18 +848,26 @@ func encodeAnthropicRequest(request bridgeRequest) map[string]any {
|
||||
}
|
||||
|
||||
func decodeOpenAIBlocks(value any) []bridgeBlock {
|
||||
blocks, _ := decodeOpenAIBlocksChecked(value)
|
||||
return blocks
|
||||
}
|
||||
|
||||
func decodeOpenAIBlocksChecked(value any) ([]bridgeBlock, error) {
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
if value == "" {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
return []bridgeBlock{{Kind: "text", Text: value}}
|
||||
return []bridgeBlock{{Kind: "text", Text: value}}, nil
|
||||
case map[string]any:
|
||||
return decodeOpenAIBlocks([]any{value})
|
||||
return decodeOpenAIBlocksChecked([]any{value})
|
||||
case []any:
|
||||
blocks := make([]bridgeBlock, 0, len(value))
|
||||
for _, raw := range value {
|
||||
part, _ := raw.(map[string]any)
|
||||
part, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil, errors.New("OpenAI content block must be an object")
|
||||
}
|
||||
switch stringAt(part, "type") {
|
||||
case "text", "input_text", "output_text":
|
||||
blocks = append(blocks, bridgeBlock{Kind: "text", Text: stringAt(part, "text")})
|
||||
@@ -839,24 +875,37 @@ func decodeOpenAIBlocks(value any) []bridgeBlock {
|
||||
blocks = append(blocks, bridgeBlock{Kind: "image", URL: firstString(stringAt(part, "image_url", "url"), stringAt(part, "image_url"))})
|
||||
case "input_image":
|
||||
blocks = append(blocks, bridgeBlock{Kind: "image", URL: stringAt(part, "image_url")})
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported OpenAI content block type %q", stringAt(part, "type"))
|
||||
}
|
||||
}
|
||||
return blocks
|
||||
return blocks, nil
|
||||
default:
|
||||
return nil
|
||||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.New("unsupported OpenAI content value")
|
||||
}
|
||||
}
|
||||
|
||||
func decodeAnthropicBlocks(value any) []bridgeBlock {
|
||||
blocks, _ := decodeAnthropicBlocksChecked(value)
|
||||
return blocks
|
||||
}
|
||||
|
||||
func decodeAnthropicBlocksChecked(value any) ([]bridgeBlock, error) {
|
||||
if text, ok := value.(string); ok {
|
||||
if text == "" {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
return []bridgeBlock{{Kind: "text", Text: text}}
|
||||
return []bridgeBlock{{Kind: "text", Text: text}}, nil
|
||||
}
|
||||
var blocks []bridgeBlock
|
||||
for _, raw := range asSlice(value) {
|
||||
part, _ := raw.(map[string]any)
|
||||
part, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil, errors.New("Anthropic content block must be an object")
|
||||
}
|
||||
switch stringAt(part, "type") {
|
||||
case "thinking":
|
||||
blocks = append(blocks, bridgeBlock{
|
||||
@@ -889,9 +938,11 @@ func decodeAnthropicBlocks(value any) []bridgeBlock {
|
||||
Result: part["content"],
|
||||
IsError: boolAt(part, "is_error"),
|
||||
})
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported Anthropic content block type %q", stringAt(part, "type"))
|
||||
}
|
||||
}
|
||||
return blocks
|
||||
return blocks, nil
|
||||
}
|
||||
|
||||
func decodeChatReasoning(message map[string]any) []bridgeBlock {
|
||||
@@ -959,7 +1010,11 @@ func bridgeReasoningText(blocks []bridgeBlock) (string, bool) {
|
||||
}
|
||||
var text strings.Builder
|
||||
for _, block := range blocks {
|
||||
text.WriteString(block.Text)
|
||||
if block.Text != "" {
|
||||
text.WriteString(block.Text)
|
||||
} else if block.Encrypted != "" {
|
||||
text.WriteString(anthropicRedactedThinkingPlaceholder)
|
||||
}
|
||||
}
|
||||
return text.String(), true
|
||||
}
|
||||
@@ -1156,6 +1211,9 @@ func convertResponse(from, to Protocol, body []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if response.Error != "" || response.Stop == "error" {
|
||||
return nil, fmt.Errorf("upstream response failed: %s", firstString(response.Error, "upstream response failed"))
|
||||
}
|
||||
return json.Marshal(encodeBridgeResponse(to, response))
|
||||
}
|
||||
|
||||
@@ -1173,6 +1231,12 @@ func decodeBridgeResponse(protocol Protocol, input map[string]any) (bridgeRespon
|
||||
}
|
||||
switch protocol {
|
||||
case ProtocolChat:
|
||||
if message := stringAt(input, "error", "message"); message != "" {
|
||||
response.Error = message
|
||||
}
|
||||
if response.Error != "" {
|
||||
return response, nil
|
||||
}
|
||||
choices := sliceAt(input, "choices")
|
||||
if len(choices) == 0 {
|
||||
return response, fmt.Errorf("chat response contains no choices")
|
||||
@@ -1180,7 +1244,11 @@ func decodeBridgeResponse(protocol Protocol, input map[string]any) (bridgeRespon
|
||||
choice, _ := choices[0].(map[string]any)
|
||||
message := mapAt(choice, "message")
|
||||
response.Reasoning = decodeChatReasoning(message)
|
||||
response.Text = bridgeBlocksText(decodeOpenAIBlocks(message["content"]))
|
||||
blocks, err := decodeOpenAIBlocksChecked(message["content"])
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("chat response content: %w", err)
|
||||
}
|
||||
response.Text = bridgeBlocksText(blocks)
|
||||
for _, raw := range sliceAt(message, "tool_calls") {
|
||||
call, _ := raw.(map[string]any)
|
||||
function := mapAt(call, "function")
|
||||
@@ -1200,7 +1268,11 @@ func decodeBridgeResponse(protocol Protocol, input map[string]any) (bridgeRespon
|
||||
case "reasoning":
|
||||
response.Reasoning = append(response.Reasoning, decodeResponsesReasoning(item)...)
|
||||
case "message":
|
||||
response.Text += bridgeBlocksText(decodeOpenAIBlocks(item["content"]))
|
||||
blocks, err := decodeOpenAIBlocksChecked(item["content"])
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Responses response content: %w", err)
|
||||
}
|
||||
response.Text += bridgeBlocksText(blocks)
|
||||
case "function_call":
|
||||
response.Tools = append(response.Tools, bridgeBlock{
|
||||
Kind: "tool_call",
|
||||
@@ -1218,10 +1290,18 @@ func decodeBridgeResponse(protocol Protocol, input map[string]any) (bridgeRespon
|
||||
response.Stop = canonicalResponsesIncomplete(stringAt(input, "incomplete_details", "reason"))
|
||||
} else if stringAt(input, "status") == "failed" {
|
||||
response.Stop = "error"
|
||||
response.Error = firstString(stringAt(input, "error", "message"), "upstream Responses request failed")
|
||||
}
|
||||
response.Usage = decodeOpenAIUsage(mapAt(input, "usage"))
|
||||
case ProtocolAnthropic:
|
||||
for _, block := range decodeAnthropicBlocks(input["content"]) {
|
||||
if stringAt(input, "type") == "error" {
|
||||
response.Error = firstString(stringAt(input, "error", "message"), "upstream Anthropic request failed")
|
||||
}
|
||||
blocks, err := decodeAnthropicBlocksChecked(input["content"])
|
||||
if err != nil {
|
||||
return response, fmt.Errorf("Anthropic response content: %w", err)
|
||||
}
|
||||
for _, block := range blocks {
|
||||
if block.Kind == "reasoning" {
|
||||
response.Reasoning = append(response.Reasoning, block)
|
||||
} else if block.Kind == "text" {
|
||||
@@ -1530,6 +1610,8 @@ func canonicalChatStop(stop string) string {
|
||||
return "length"
|
||||
case "content_filter":
|
||||
return "content_filter"
|
||||
case "error", "network_error", "server_error":
|
||||
return "error"
|
||||
default:
|
||||
return "stop"
|
||||
}
|
||||
@@ -1543,6 +1625,8 @@ func canonicalAnthropicStop(stop string) string {
|
||||
return "length"
|
||||
case "stop_sequence", "pause_turn", "refusal":
|
||||
return stop
|
||||
case "error", "network_error", "server_error":
|
||||
return "error"
|
||||
default:
|
||||
return "stop"
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -11,6 +12,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var errStreamUpstreamFailure = errors.New("upstream stream failure delivered")
|
||||
|
||||
type bridgeStreamEvent struct {
|
||||
Kind string
|
||||
ResponseID string
|
||||
@@ -21,6 +24,9 @@ type bridgeStreamEvent struct {
|
||||
ToolID string
|
||||
ToolName string
|
||||
Stop string
|
||||
Error string
|
||||
ErrorType string
|
||||
Encrypted string
|
||||
Usage *bridgeUsage
|
||||
}
|
||||
|
||||
@@ -57,7 +63,7 @@ func transcodeStreamWithUsage(w http.ResponseWriter, reader io.Reader, from, to
|
||||
if err := readSSE(reader, func(eventName, data string) error {
|
||||
events, err := parser.Parse(eventName, data)
|
||||
if err != nil {
|
||||
return err
|
||||
return emitter.Emit(bridgeStreamEvent{Kind: "error", Error: err.Error(), ErrorType: "upstream_error"})
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := emitter.Emit(event); err != nil {
|
||||
@@ -66,6 +72,9 @@ func transcodeStreamWithUsage(w http.ResponseWriter, reader io.Reader, from, to
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
if errors.Is(err, errStreamUpstreamFailure) {
|
||||
return emitter.usage, emitter.usageReported, nil
|
||||
}
|
||||
return emitter.usage, emitter.usageReported, err
|
||||
}
|
||||
return emitter.usage, emitter.usageReported, emitter.Finish()
|
||||
@@ -215,7 +224,7 @@ func (parser *bridgeStreamParser) parseChat(value map[string]any) []bridgeStream
|
||||
for _, raw := range sliceAt(value, "choices") {
|
||||
choice, _ := raw.(map[string]any)
|
||||
delta := mapAt(choice, "delta")
|
||||
if reasoning := stringAt(delta, "reasoning_content"); reasoning != "" {
|
||||
if reasoning := firstString(stringAt(delta, "reasoning_content"), stringAt(delta, "reasoning")); reasoning != "" {
|
||||
events = append(events, bridgeStreamEvent{Kind: "reasoning", Text: reasoning})
|
||||
}
|
||||
if text := stringAt(delta, "content"); text != "" {
|
||||
@@ -245,6 +254,10 @@ func (parser *bridgeStreamParser) parseChat(value map[string]any) []bridgeStream
|
||||
}
|
||||
}
|
||||
if stop := stringAt(choice, "finish_reason"); stop != "" {
|
||||
if isStreamErrorFinish(stop) {
|
||||
events = append(events, bridgeStreamEvent{Kind: "error", Error: firstString(stringAt(choice, "error", "message"), "upstream Chat stream failed"), ErrorType: "upstream_error"})
|
||||
continue
|
||||
}
|
||||
for _, key := range parser.toolOrder {
|
||||
if !parser.tools[key] && parser.toolNames[key] != "" {
|
||||
parser.tools[key] = true
|
||||
@@ -280,6 +293,8 @@ func (parser *bridgeStreamParser) parseAnthropic(value map[string]any) ([]bridge
|
||||
events = append(events, bridgeStreamEvent{Kind: "reasoning_signature", Signature: signature})
|
||||
}
|
||||
return events, nil
|
||||
case "redacted_thinking":
|
||||
return []bridgeStreamEvent{{Kind: "reasoning", Encrypted: stringAt(block, "data")}}, nil
|
||||
case "tool_use":
|
||||
parser.tools[key] = true
|
||||
return []bridgeStreamEvent{{Kind: "tool_start", ToolKey: key, ToolID: stringAt(block, "id"), ToolName: stringAt(block, "name")}}, nil
|
||||
@@ -315,7 +330,7 @@ func (parser *bridgeStreamParser) parseAnthropic(value map[string]any) ([]bridge
|
||||
return []bridgeStreamEvent{{Kind: "done"}}, nil
|
||||
case "error":
|
||||
message := firstString(stringAt(value, "error", "message"), "upstream Anthropic stream error")
|
||||
return nil, fmt.Errorf("%s", message)
|
||||
return []bridgeStreamEvent{{Kind: "error", Error: message, ErrorType: firstString(stringAt(value, "error", "type"), "upstream_error")}}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@@ -364,7 +379,7 @@ func (parser *bridgeStreamParser) parseResponses(eventName string, value map[str
|
||||
}
|
||||
blocks := decodeResponsesReasoning(item)
|
||||
if len(blocks) > 0 {
|
||||
return []bridgeStreamEvent{{Kind: "reasoning", Text: blocks[0].Text}}
|
||||
return []bridgeStreamEvent{{Kind: "reasoning", Text: blocks[0].Text, Encrypted: blocks[0].Encrypted}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -386,17 +401,28 @@ func (parser *bridgeStreamParser) parseResponses(eventName string, value map[str
|
||||
case "response.completed", "response.incomplete", "response.failed", "response.done":
|
||||
response := mapAt(value, "response")
|
||||
usage := decodeOpenAIUsage(mapAt(response, "usage"))
|
||||
if typeName == "response.failed" || stringAt(response, "status") == "failed" {
|
||||
message := firstString(stringAt(response, "error", "message"), stringAt(value, "error", "message"), "upstream Responses request failed")
|
||||
return []bridgeStreamEvent{{Kind: "usage", Usage: &usage}, {Kind: "error", Error: message, ErrorType: firstString(stringAt(response, "error", "code"), "upstream_error")}}
|
||||
}
|
||||
stop := "stop"
|
||||
if typeName == "response.incomplete" {
|
||||
stop = canonicalResponsesIncomplete(stringAt(response, "incomplete_details", "reason"))
|
||||
} else if typeName == "response.failed" {
|
||||
stop = "error"
|
||||
}
|
||||
return []bridgeStreamEvent{{Kind: "usage", Usage: &usage}, {Kind: "finish", Stop: stop}, {Kind: "done"}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isStreamErrorFinish(stop string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(stop)) {
|
||||
case "error", "network_error", "server_error":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (parser *bridgeStreamParser) rememberTool(key, id, name string) {
|
||||
if _, seen := parser.tools[key]; !seen {
|
||||
parser.tools[key] = false
|
||||
@@ -456,6 +482,7 @@ type bridgeStreamEmitter struct {
|
||||
stop string
|
||||
usage bridgeUsage
|
||||
usageReported bool
|
||||
reasoningEncrypted string
|
||||
text strings.Builder
|
||||
reasoning strings.Builder
|
||||
reasoningSignature strings.Builder
|
||||
@@ -503,6 +530,12 @@ func (emitter *bridgeStreamEmitter) Emit(event bridgeStreamEvent) error {
|
||||
}
|
||||
switch event.Kind {
|
||||
case "reasoning":
|
||||
if event.Encrypted != "" {
|
||||
emitter.reasoningEncrypted = event.Encrypted
|
||||
if emitter.target != ProtocolResponses && event.Text == "" {
|
||||
event.Text = anthropicRedactedThinkingPlaceholder
|
||||
}
|
||||
}
|
||||
if event.Text == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -552,12 +585,46 @@ func (emitter *bridgeStreamEmitter) Emit(event bridgeStreamEvent) error {
|
||||
}
|
||||
case "finish":
|
||||
emitter.stop = event.Stop
|
||||
case "error":
|
||||
emitter.done = true
|
||||
if err := emitter.emitError(firstString(event.Error, "upstream stream failed"), firstString(event.ErrorType, "upstream_error")); err != nil {
|
||||
return err
|
||||
}
|
||||
return errStreamUpstreamFailure
|
||||
case "done":
|
||||
return emitter.Finish()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (emitter *bridgeStreamEmitter) emitError(message, errorType string) error {
|
||||
switch emitter.target {
|
||||
case ProtocolAnthropic:
|
||||
return emitter.sse("error", map[string]any{
|
||||
"type": "error",
|
||||
"error": map[string]any{"type": errorType, "message": message},
|
||||
})
|
||||
case ProtocolResponses:
|
||||
response := emitter.responsesBase("failed", []any{})
|
||||
response["error"] = map[string]any{"code": errorType, "message": message}
|
||||
return emitter.sse("response.failed", map[string]any{
|
||||
"type": "response.failed",
|
||||
"response": response,
|
||||
"sequence_number": emitter.nextSequence(),
|
||||
})
|
||||
default:
|
||||
if err := emitter.sse("", map[string]any{"error": map[string]any{
|
||||
"message": message,
|
||||
"type": errorType,
|
||||
"param": nil,
|
||||
"code": nil,
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
return emitter.rawSSE("", "[DONE]")
|
||||
}
|
||||
}
|
||||
|
||||
func (emitter *bridgeStreamEmitter) tool(key string) *bridgeStreamTool {
|
||||
if key == "" {
|
||||
key = fmt.Sprintf("tool-%d", len(emitter.order))
|
||||
@@ -751,6 +818,9 @@ func (emitter *bridgeStreamEmitter) finishReasoning() error {
|
||||
return err
|
||||
}
|
||||
item := map[string]any{"id": emitter.reasoningItemID, "type": "reasoning", "status": "completed", "summary": []any{part}}
|
||||
if emitter.reasoningEncrypted != "" {
|
||||
item["encrypted_content"] = emitter.reasoningEncrypted
|
||||
}
|
||||
return emitter.sse("response.output_item.done", map[string]any{"type": "response.output_item.done", "output_index": emitter.reasoningOutput, "item": item, "sequence_number": emitter.nextSequence()})
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user