mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
The legacy /zen/v1 free-tier gate needs only a single tool named "read": probing showed [read] alone passes while bash/shell/edit/glob/grep alone (or any combination without read) return 403 FreeTierError, and neither the description nor the parameter schema is validated. The v1.3.7-style pre-injection of the other core tools was therefore unnecessary traffic and repeated client schemas on the wire. Legacy lane: - Drop the bash/edit/glob/grep pre-injection entirely. - If the client already declares an exact "read" tool, the body is left untouched. - Otherwise, a read-related client tool (case-insensitive match such as "reAd", "read_file", "ReadFile", "file_read", "view_file") is renamed in place to "read" with its client description and schema preserved, and the matching tool_choice plus any historical tool call in the request are rewritten so the outbound body stays self-consistent. - If no read-related tool exists, a single "read" stub is injected whose description marks it deprecated, disabled, and not to be called. Round trip: - Add protocol.RenameResponseTool for chat tool_calls, Responses function_call items, and Anthropic tool_use blocks. - Add protocol.RenameStreamReader, a pull-based SSE rewriter (no goroutine or io.Pipe, so a disconnected client cannot strand a writer) that restores the client's original name in streaming replies. Chat deltas may split or repeat a name fragment, so "read" prefixes are held until the name resolves, and a continuation past "read" is passed through verbatim. - Carry the alias through the new telemetry.RequestMeta.ToolAlias field and shapeKeyBody's new return value; the /inference lane and non-legacy upstreams are untouched.
521 lines
15 KiB
Go
521 lines
15 KiB
Go
package protocol
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"opencode2api/internal/jsonutil"
|
|
)
|
|
|
|
var errStreamUpstreamFailure = errors.New("upstream stream failure delivered")
|
|
|
|
var errStreamNormalTermination = errors.New("upstream stream terminated normally")
|
|
|
|
var errSSEUnexpectedEOF = errors.New("unexpected end of SSE stream")
|
|
|
|
type streamTermination uint8
|
|
|
|
const (
|
|
streamOpen streamTermination = iota
|
|
streamNormalTermination
|
|
streamErrorTermination
|
|
)
|
|
|
|
type bridgeStreamEvent struct {
|
|
Kind string
|
|
ResponseID string
|
|
Model string
|
|
Text string
|
|
Signature string
|
|
ToolKey string
|
|
ToolID string
|
|
ToolName string
|
|
Stop string
|
|
Error string
|
|
ErrorType string
|
|
Encrypted string
|
|
Usage *Usage
|
|
}
|
|
|
|
// TranscodeStream is the request-aware form used by the
|
|
// gateway. A cancelled client must not receive a synthetic upstream error
|
|
// after its connection has gone away.
|
|
func TranscodeStream(ctx context.Context, w http.ResponseWriter, reader io.Reader, from, to Protocol, model string) (Usage, bool, error) {
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
return Usage{}, false, fmt.Errorf("response writer does not support streaming")
|
|
}
|
|
parser := &bridgeStreamParser{
|
|
protocol: from,
|
|
tools: map[string]bool{},
|
|
toolIDs: map[string]string{},
|
|
toolNames: map[string]string{},
|
|
responseArgs: map[string]bool{},
|
|
responseReasoning: map[string]bool{},
|
|
}
|
|
emitter := newBridgeStreamEmitter(w, flusher, to, model)
|
|
termination := streamOpen
|
|
readErr := readSSE(reader, func(eventName, data string) error {
|
|
events, err := parser.Parse(eventName, data)
|
|
if err != nil {
|
|
termination = streamErrorTermination
|
|
if emitErr := emitter.Emit(bridgeStreamEvent{Kind: "error", Error: err.Error(), ErrorType: "upstream_error"}); emitErr != nil {
|
|
return emitErr
|
|
}
|
|
return errStreamUpstreamFailure
|
|
}
|
|
for _, event := range events {
|
|
switch event.Kind {
|
|
case "done":
|
|
termination = streamNormalTermination
|
|
case "error":
|
|
termination = streamErrorTermination
|
|
}
|
|
if err := emitter.Emit(event); err != nil {
|
|
return err
|
|
}
|
|
if event.Kind == "done" {
|
|
return errStreamNormalTermination
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if readErr != nil {
|
|
if errors.Is(readErr, errStreamNormalTermination) {
|
|
return emitter.usage, emitter.usageReported, nil
|
|
}
|
|
if ClientCanceled(ctx, readErr) {
|
|
return emitter.usage, emitter.usageReported, readErr
|
|
}
|
|
if termination == streamOpen {
|
|
if emitErr := emitUnexpectedStreamError(emitter, readErr); emitErr != nil {
|
|
return emitter.usage, emitter.usageReported, emitErr
|
|
}
|
|
}
|
|
return emitter.usage, emitter.usageReported, readErr
|
|
}
|
|
if termination == streamNormalTermination {
|
|
return emitter.usage, emitter.usageReported, nil
|
|
}
|
|
if termination == streamErrorTermination {
|
|
return emitter.usage, emitter.usageReported, errStreamUpstreamFailure
|
|
}
|
|
if ClientCanceled(ctx, nil) {
|
|
return emitter.usage, emitter.usageReported, ctx.Err()
|
|
}
|
|
if err := emitUnexpectedStreamError(emitter, errSSEUnexpectedEOF); err != nil {
|
|
return emitter.usage, emitter.usageReported, err
|
|
}
|
|
return emitter.usage, emitter.usageReported, errSSEUnexpectedEOF
|
|
}
|
|
|
|
func emitUnexpectedStreamError(emitter *bridgeStreamEmitter, cause error) error {
|
|
message := "upstream SSE stream ended before a terminal event"
|
|
if cause != nil && !errors.Is(cause, errSSEUnexpectedEOF) {
|
|
message = fmt.Sprintf("upstream SSE stream failed: %v", cause)
|
|
}
|
|
err := emitter.Emit(bridgeStreamEvent{Kind: "error", Error: message, ErrorType: "upstream_error"})
|
|
if errors.Is(err, errStreamUpstreamFailure) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func ClientCanceled(ctx context.Context, streamErr error) bool {
|
|
if errors.Is(streamErr, context.Canceled) {
|
|
return true
|
|
}
|
|
return ctx != nil && ctx.Err() != nil
|
|
}
|
|
|
|
// sseFlushWriter preserves an upstream SSE byte stream while making each
|
|
// successful write visible to the client immediately. io.Copy is free to
|
|
// choose large writes, so flushing in Write is the only reliable place to
|
|
// keep same-protocol streams live.
|
|
type sseFlushWriter struct {
|
|
writer io.Writer
|
|
flusher http.Flusher
|
|
}
|
|
|
|
func (writer *sseFlushWriter) Write(data []byte) (int, error) {
|
|
n, err := writer.writer.Write(data)
|
|
if n > 0 {
|
|
writer.flusher.Flush()
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
func ForwardStream(ctx context.Context, w http.ResponseWriter, reader io.Reader, protocol Protocol, model string) (Usage, bool, error) {
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
return Usage{}, false, fmt.Errorf("response writer does not support streaming")
|
|
}
|
|
observer := newStreamUsageObserver(protocol)
|
|
_, copyErr := io.Copy(&sseFlushWriter{writer: w, flusher: flusher}, io.TeeReader(reader, observer))
|
|
usage := observer.Finish()
|
|
if observer.ErrorTermination() {
|
|
if copyErr != nil {
|
|
return usage, observer.Reported(), copyErr
|
|
}
|
|
return usage, observer.Reported(), errStreamUpstreamFailure
|
|
}
|
|
if observer.NormalTermination() && observer.ParseError() == nil {
|
|
return usage, observer.Reported(), copyErr
|
|
}
|
|
if ClientCanceled(ctx, copyErr) {
|
|
if copyErr == nil {
|
|
copyErr = ctx.Err()
|
|
}
|
|
return usage, observer.Reported(), copyErr
|
|
}
|
|
cause := observer.ParseError()
|
|
if copyErr != nil {
|
|
cause = copyErr
|
|
}
|
|
if cause == nil {
|
|
cause = errSSEUnexpectedEOF
|
|
}
|
|
emitter := newBridgeStreamEmitter(w, flusher, protocol, model)
|
|
if err := emitUnexpectedStreamError(emitter, cause); err != nil {
|
|
return usage, observer.Reported(), err
|
|
}
|
|
return usage, observer.Reported(), cause
|
|
}
|
|
|
|
// RenameStreamReader wraps an SSE stream reader and rewrites tool call names
|
|
// matching fromName back to toName before downstream parsers or writers consume
|
|
// the stream. When fromName or toName is empty (or identical), reader is
|
|
// returned unchanged.
|
|
//
|
|
// It is a pull-based transform instead of a goroutine plus io.Pipe so a client
|
|
// that disconnects mid-stream cannot strand a blocked writer.
|
|
func RenameStreamReader(reader io.Reader, protocol Protocol, fromName, toName string) io.Reader {
|
|
if fromName == "" || toName == "" || fromName == toName {
|
|
return reader
|
|
}
|
|
return &renameStreamReader{
|
|
buffer: bufio.NewReaderSize(reader, 64<<10),
|
|
renamer: &streamToolRenamer{protocol: protocol, fromName: fromName, toName: toName, tools: map[string]*streamToolRenameState{}},
|
|
}
|
|
}
|
|
|
|
type renameStreamReader struct {
|
|
buffer *bufio.Reader
|
|
renamer *streamToolRenamer
|
|
pending []byte
|
|
err error
|
|
}
|
|
|
|
func (reader *renameStreamReader) Read(data []byte) (int, error) {
|
|
for len(reader.pending) == 0 {
|
|
if reader.err != nil {
|
|
return 0, reader.err
|
|
}
|
|
// ReadString either returns a non-empty chunk with a nil error or
|
|
// returns the trailing chunk together with EOF, so this loop can only
|
|
// repeat while a rewritten chunk is still being drained.
|
|
line, err := reader.buffer.ReadString('\n')
|
|
if len(line) > 0 {
|
|
reader.pending = reader.rewriteLine(line)
|
|
}
|
|
if err != nil {
|
|
// The final chunk is drained before the error is reported so a
|
|
// truncated stream still delivers every byte the upstream sent.
|
|
reader.err = err
|
|
if len(reader.pending) == 0 {
|
|
return 0, err
|
|
}
|
|
}
|
|
}
|
|
n := copy(data, reader.pending)
|
|
reader.pending = reader.pending[n:]
|
|
return n, nil
|
|
}
|
|
|
|
// rewriteLine transforms one complete SSE line, preserving its original bytes
|
|
// and terminator whenever nothing needs to be rewritten.
|
|
func (reader *renameStreamReader) rewriteLine(line string) []byte {
|
|
body, terminator := line, ""
|
|
if trimmed := strings.TrimSuffix(line, "\n"); trimmed != line {
|
|
terminator = "\n"
|
|
body = trimmed
|
|
if trimmed = strings.TrimSuffix(body, "\r"); trimmed != body {
|
|
terminator = "\r\n"
|
|
body = trimmed
|
|
}
|
|
}
|
|
if !strings.HasPrefix(body, "data:") {
|
|
return []byte(line)
|
|
}
|
|
raw := strings.TrimSpace(strings.TrimPrefix(body, "data:"))
|
|
if raw == "" || raw == "[DONE]" {
|
|
return []byte(line)
|
|
}
|
|
rewritten := reader.renamer.rewriteFrame(raw)
|
|
if string(rewritten) == raw {
|
|
return []byte(line)
|
|
}
|
|
return []byte("data: " + string(rewritten) + terminator)
|
|
}
|
|
|
|
// streamToolRenameState tracks how one tool call's name was resolved while a
|
|
// stream is rewritten. Chat deltas may repeat a growing name prefix or append
|
|
// fragments, so a value that is still a prefix of fromName is held back until
|
|
// it either completes the match or diverges.
|
|
type streamToolRenameState struct {
|
|
decided bool
|
|
renamed bool
|
|
pending string
|
|
}
|
|
|
|
type streamToolRenamer struct {
|
|
protocol Protocol
|
|
fromName string
|
|
toName string
|
|
tools map[string]*streamToolRenameState
|
|
}
|
|
|
|
// rewriteFrame rewrites one decoded SSE data object.
|
|
func (renamer *streamToolRenamer) rewriteFrame(raw string) []byte {
|
|
if renamer.protocol != Chat {
|
|
// Anthropic and Responses streams always carry a complete tool name in
|
|
// a single event, so the stateless object rewrite is exact.
|
|
if !strings.Contains(raw, renamer.fromName) {
|
|
return []byte(raw)
|
|
}
|
|
return RenameResponseTool([]byte(raw), renamer.protocol, renamer.fromName, renamer.toName)
|
|
}
|
|
if !strings.Contains(raw, `"tool_calls"`) {
|
|
return []byte(raw)
|
|
}
|
|
return renamer.rewriteChatFrame(raw)
|
|
}
|
|
|
|
func (renamer *streamToolRenamer) rewriteChatFrame(raw string) []byte {
|
|
var payload map[string]any
|
|
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
|
|
return []byte(raw)
|
|
}
|
|
changed := false
|
|
for _, rawChoice := range jsonutil.SliceAt(payload, "choices") {
|
|
choice, _ := rawChoice.(map[string]any)
|
|
for _, container := range []string{"delta", "message"} {
|
|
message, _ := choice[container].(map[string]any)
|
|
if message == nil {
|
|
continue
|
|
}
|
|
for callIndex, rawCall := range jsonutil.SliceAt(message, "tool_calls") {
|
|
call, _ := rawCall.(map[string]any)
|
|
function, _ := call["function"].(map[string]any)
|
|
if function == nil {
|
|
continue
|
|
}
|
|
state := renamer.toolState(fmt.Sprint(jsonutil.FirstAny(call["index"], jsonutil.StringAt(call, "id"), callIndex)))
|
|
if name, ok := function["name"].(string); ok && name != "" {
|
|
if renamer.applyNameFragment(state, function, name) {
|
|
changed = true
|
|
}
|
|
}
|
|
if _, ok := function["arguments"]; ok && !state.decided {
|
|
// Arguments can only start once the name is complete: flush
|
|
// whatever was held back, verbatim.
|
|
if state.pending != "" {
|
|
function["name"] = state.pending
|
|
changed = true
|
|
}
|
|
state.decided = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if !changed {
|
|
return []byte(raw)
|
|
}
|
|
encoded, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return []byte(raw)
|
|
}
|
|
return encoded
|
|
}
|
|
|
|
// applyNameFragment folds one name fragment into a tool's rename state and
|
|
// reports whether the frame changed.
|
|
func (renamer *streamToolRenamer) applyNameFragment(state *streamToolRenameState, function map[string]any, fragment string) bool {
|
|
if state.decided {
|
|
if !state.renamed {
|
|
return false
|
|
}
|
|
// A resolved alias accepts no further fragments: any continuation
|
|
// belongs to a longer name the client never declared.
|
|
if _, exists := function["name"]; exists {
|
|
delete(function, "name")
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
state.pending = mergeToolName(state.pending, fragment)
|
|
switch {
|
|
case state.pending == renamer.fromName:
|
|
function["name"] = renamer.toName
|
|
state.decided, state.renamed = true, true
|
|
return true
|
|
case strings.HasPrefix(renamer.fromName, state.pending):
|
|
// Still a prefix of the alias: hold the fragment until a later frame
|
|
// either completes the match or diverges.
|
|
delete(function, "name")
|
|
return true
|
|
default:
|
|
function["name"] = state.pending
|
|
state.decided = true
|
|
return true
|
|
}
|
|
}
|
|
|
|
func (renamer *streamToolRenamer) toolState(key string) *streamToolRenameState {
|
|
if state := renamer.tools[key]; state != nil {
|
|
return state
|
|
}
|
|
state := &streamToolRenameState{}
|
|
renamer.tools[key] = state
|
|
return state
|
|
}
|
|
|
|
type streamUsageObserver struct {
|
|
parser *bridgeStreamParser
|
|
buffer []byte
|
|
usage Usage
|
|
reported bool
|
|
normal bool
|
|
error bool
|
|
parseErr error
|
|
}
|
|
|
|
func newStreamUsageObserver(protocol Protocol) *streamUsageObserver {
|
|
return &streamUsageObserver{parser: &bridgeStreamParser{
|
|
protocol: protocol, tools: map[string]bool{}, toolIDs: map[string]string{}, toolNames: map[string]string{},
|
|
responseArgs: map[string]bool{}, responseReasoning: map[string]bool{},
|
|
}}
|
|
}
|
|
|
|
func (observer *streamUsageObserver) Write(data []byte) (int, error) {
|
|
observer.buffer = append(observer.buffer, data...)
|
|
for {
|
|
index, width := nextSSEBoundary(observer.buffer)
|
|
if index < 0 {
|
|
break
|
|
}
|
|
frame := append([]byte(nil), observer.buffer[:index+width]...)
|
|
observer.buffer = observer.buffer[index+width:]
|
|
observer.consume(frame)
|
|
}
|
|
return len(data), nil
|
|
}
|
|
|
|
func (observer *streamUsageObserver) Finish() Usage {
|
|
// An unterminated final line is not an SSE frame. In particular, do not
|
|
// count a usage object from a response that was cut off at EOF.
|
|
observer.buffer = nil
|
|
return observer.usage
|
|
}
|
|
|
|
func (observer *streamUsageObserver) Reported() bool { return observer.reported }
|
|
|
|
func (observer *streamUsageObserver) NormalTermination() bool { return observer.normal }
|
|
|
|
func (observer *streamUsageObserver) ErrorTermination() bool { return observer.error }
|
|
|
|
func (observer *streamUsageObserver) ParseError() error { return observer.parseErr }
|
|
|
|
func (observer *streamUsageObserver) consume(frame []byte) {
|
|
_ = readSSE(strings.NewReader(string(frame)), func(eventName, data string) error {
|
|
events, err := observer.parser.Parse(eventName, data)
|
|
if err != nil {
|
|
if observer.parseErr == nil {
|
|
observer.parseErr = err
|
|
}
|
|
return nil
|
|
}
|
|
for _, event := range events {
|
|
switch event.Kind {
|
|
case "done":
|
|
observer.normal = true
|
|
case "error":
|
|
observer.error = true
|
|
}
|
|
if event.Usage != nil {
|
|
observer.reported = true
|
|
mergeBridgeUsage(&observer.usage, *event.Usage)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func nextSSEBoundary(data []byte) (int, int) {
|
|
lf := bytes.Index(data, []byte("\n\n"))
|
|
crlf := bytes.Index(data, []byte("\r\n\r\n"))
|
|
if lf < 0 {
|
|
if crlf < 0 {
|
|
return -1, 0
|
|
}
|
|
return crlf, 4
|
|
}
|
|
if crlf >= 0 && crlf < lf {
|
|
return crlf, 4
|
|
}
|
|
return lf, 2
|
|
}
|
|
|
|
func readSSE(reader io.Reader, handler func(eventName, data string) error) error {
|
|
scanner := bufio.NewScanner(reader)
|
|
scanner.Buffer(make([]byte, 64<<10), 16<<20)
|
|
var eventName string
|
|
var dataLines []string
|
|
flush := func() error {
|
|
if len(dataLines) == 0 {
|
|
eventName = ""
|
|
return nil
|
|
}
|
|
err := handler(eventName, strings.Join(dataLines, "\n"))
|
|
eventName = ""
|
|
dataLines = dataLines[:0]
|
|
return err
|
|
}
|
|
for scanner.Scan() {
|
|
line := strings.TrimSuffix(scanner.Text(), "\r")
|
|
if line == "" {
|
|
if err := flush(); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, ":") {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, "event:") {
|
|
eventName = strings.TrimSpace(strings.TrimPrefix(line, "event:"))
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, "data:") {
|
|
dataLines = append(dataLines, strings.TrimSpace(strings.TrimPrefix(line, "data:")))
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
return err
|
|
}
|
|
if len(dataLines) > 0 {
|
|
// A blank line is the SSE record delimiter. Do not parse a final
|
|
// unterminated record as a complete frame; an EOF without a terminal
|
|
// event is handled by the caller as a truncated stream.
|
|
return errSSEUnexpectedEOF
|
|
}
|
|
return nil
|
|
}
|