diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index ccf87e0..26f1889 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -257,6 +257,10 @@ func (g *Gateway) handleInference(external wire.Protocol) http.HandlerFunc { copyErrorResponse(w, external, resp, ids.Request) return } + var toolAlias string + if meta != nil { + toolAlias = meta.ToolAlias + } if stream { if meta != nil { meta.Stream = true @@ -269,12 +273,13 @@ func (g *Gateway) handleInference(external wire.Protocol) http.HandlerFunc { w.Header().Set("Cache-Control", "no-cache") w.Header().Set("X-Accel-Buffering", "no") w.WriteHeader(resp.StatusCode) + streamBody := wire.RenameStreamReader(resp.Body, upstreamRoute.Protocol, legacyAnonymousReadTool, toolAlias) var usage wire.Usage var usageReported bool if external == upstreamRoute.Protocol { - usage, usageReported, err = wire.ForwardStream(r.Context(), w, resp.Body, upstreamRoute.Protocol, model) + usage, usageReported, err = wire.ForwardStream(r.Context(), w, streamBody, upstreamRoute.Protocol, model) } else { - usage, usageReported, err = wire.TranscodeStream(r.Context(), w, resp.Body, upstreamRoute.Protocol, external, model) + usage, usageReported, err = wire.TranscodeStream(r.Context(), w, streamBody, upstreamRoute.Protocol, external, model) } if meta != nil { meta.Usage, meta.UsageReported = usage, usageReported @@ -309,6 +314,7 @@ func (g *Gateway) handleInference(external wire.Protocol) http.HandlerFunc { } responseBody = collapsed } + responseBody = wire.RenameResponseTool(responseBody, upstreamRoute.Protocol, legacyAnonymousReadTool, toolAlias) if usage, reported := wire.ResponseUsage(upstreamRoute.Protocol, responseBody); meta != nil { meta.Usage, meta.UsageReported = usage, reported } diff --git a/internal/gateway/probe.go b/internal/gateway/probe.go index d384127..cdd9243 100644 --- a/internal/gateway/probe.go +++ b/internal/gateway/probe.go @@ -61,7 +61,8 @@ func probePayload(model string, protocol wire.Protocol, effort string, legacy bo if err != nil { return nil, err } - return prepareAnonymousBody(b, protocol, legacy), nil + shaped, _ := prepareAnonymousBody(b, protocol, legacy) + return shaped, nil } func (g *Gateway) probeFreeModel(ctx context.Context, model string) probeResult { diff --git a/internal/gateway/rotation.go b/internal/gateway/rotation.go index c3845cb..9f1f6dd 100644 --- a/internal/gateway/rotation.go +++ b/internal/gateway/rotation.go @@ -181,7 +181,10 @@ func (g *Gateway) rotationAttempt(parent context.Context, w http.ResponseWriter, result.reason = "request_conversion_error" return result } - body, shaped := g.shapeKeyBody(bodies[config.TierGo], route, config.TierGo) + body, toolAlias, shaped := g.shapeKeyBody(bodies[config.TierGo], route, config.TierGo) + if meta := telemetry.MetaFromContext(parent); meta != nil { + meta.ToolAlias = toolAlias + } cursor := g.goNodes.CursorFor(ids.Session) var node *upstreamNode // Each group attempt is exactly one HTTP call. Key selection may change, @@ -261,7 +264,8 @@ func (g *Gateway) rotationAttempt(parent context.Context, w http.ResponseWriter, g.monitor.BeginStream() defer g.monitor.EndStream() } - usage, reported, committed, err := wire.RotationStream(original.Context(), w, resp.Body, route.Protocol, external, alias, ready) + streamBody := wire.RenameStreamReader(resp.Body, route.Protocol, legacyAnonymousReadTool, toolAlias) + usage, reported, committed, err := wire.RotationStream(original.Context(), w, streamBody, route.Protocol, external, alias, ready) if meta != nil { meta.Usage = usage meta.UsageReported = reported @@ -297,6 +301,9 @@ func (g *Gateway) rotationAttempt(parent context.Context, w http.ResponseWriter, if shaped { b, err = wire.CollapseStream(bytes.NewReader(b), route.Protocol, route.ID) } + if err == nil { + b = wire.RenameResponseTool(b, route.Protocol, legacyAnonymousReadTool, toolAlias) + } var document map[string]any if err == nil { err = json.Unmarshal(b, &document) diff --git a/internal/gateway/upstream.go b/internal/gateway/upstream.go index dfe32aa..3a14ee1 100644 --- a/internal/gateway/upstream.go +++ b/internal/gateway/upstream.go @@ -283,15 +283,16 @@ func (g *Gateway) doAnonymousUpstream(ctx context.Context, route models.Route, b return nil, errors.New("no prepared Zen request body"), 0 } // The anonymous free tier only serves agent-shaped streaming requests - // carrying the core tool definitions (anything else is rejected with 403 + // carrying the required tool definitions (anything else is rejected with 403 // FreeTierError). Normalize the wire body per upstream gate (legacy /zen/v1 // vs /inference); the gateway collapses the stream back when the downstream // client asked for a plain JSON reply. Key tiers keep their original bodies. protocol := route.ProtocolFor(config.TierZen) upstreams := g.anonymousUpstreams() preparedBodies := make([][]byte, len(upstreams)) + toolAliases := make([]string, len(upstreams)) for i, baseURL := range upstreams { - preparedBodies[i] = prepareAnonymousBody(rawBody, protocol, isLegacyZenUpstream(baseURL)) + preparedBodies[i], toolAliases[i] = prepareAnonymousBody(rawBody, protocol, isLegacyZenUpstream(baseURL)) } for proxies < limit { if ctx.Err() != nil { @@ -321,6 +322,7 @@ func (g *Gateway) doAnonymousUpstream(ctx context.Context, route models.Route, b if meta := telemetry.MetaFromContext(ctx); meta != nil { meta.Attempts = attemptOffset + attempts meta.Tier = string(config.TierZen) + meta.ToolAlias = toolAliases[index] } if lastResponse != nil { httpx.DrainAndClose(lastResponse.Body) @@ -407,10 +409,17 @@ func isLegacyZenUpstream(baseURL string) bool { return strings.HasSuffix(trimmed, "/zen") } -// legacyAnonymousCoreTools are the pre-2.x OpenCode 1.18.x tool names expected -// by the legacy /zen/v1 free-tier gate ("bash" + "read" required; minimal stub -// definitions pass without schema validation, matching v1.3.7). -var legacyAnonymousCoreTools = []string{"bash", "edit", "glob", "grep", "read"} +// legacyAnonymousReadTool is the single tool name required by the legacy +// /zen/v1 free-tier gate. Unlike /inference, the /zen/v1 gate does not require +// a shell/edit/glob/grep toolset and does not validate tool descriptions or +// parameter schemas: a single tool named "read" is sufficient. +const legacyAnonymousReadTool = "read" + +// legacyDeprecatedReadDescription is attached when the gateway must inject a +// placeholder "read" tool on the legacy /zen/v1 lane because the client +// request declared no read-related tool of its own. It explicitly tells the +// model that the tool is deprecated and must never be invoked. +const legacyDeprecatedReadDescription = "DEPRECATED: This tool is deprecated and disabled. Do NOT call or reference this tool under any circumstances; ignore it completely." // anonymousCoreTools are the tool names the /inference free-tier gate expects // on an agent-shaped request. Requests without a shell tool and a read tool are @@ -462,20 +471,26 @@ func knownOpencodeTool(name string) (opencodeToolDefinition, bool) { } // prepareAnonymousBody returns a copy of body normalized for the anonymous -// free tier: streaming enabled plus the core agent tools present (using -// v1.3.7 minimal stubs with legacyAnonymousCoreTools for the legacy /zen/v1 -// gate, and anonymousCoreTools with genuine definitions for the /inference -// gate). Bodies that already satisfy all of that (or are not JSON objects) are -// returned unchanged. System One payloads are decision requests, not agent -// traffic, so they are forwarded verbatim; injecting streaming or tool -// definitions would make the upstream reject them. -func prepareAnonymousBody(body []byte, protocol wire.Protocol, legacy bool) []byte { +// free tier alongside any original client tool name that was aliased to "read" +// for the legacy /zen/v1 gate. +// +// On the legacy /zen/v1 gate (legacy == true), pre-injection of other tools is +// dropped: if the client declared a read-related tool (case-insensitive, e.g. +// "reAd", "Read_file", "readFile"), it is renamed in-place to "read" while +// keeping its original description and parameters, and its original name is +// returned so the gateway can restore it in the downstream response; if no +// read-related tool is present, a single deprecated "read" stub is injected. +// +// On the /inference gate (legacy == false), anonymousCoreTools with genuine +// CLI 2.x definitions are ensured. System One payloads are decision requests, +// not agent traffic, so they are forwarded verbatim. +func prepareAnonymousBody(body []byte, protocol wire.Protocol, legacy bool) ([]byte, string) { if protocol == wire.SystemOne { - return body + return body, "" } var payload map[string]any if err := json.Unmarshal(body, &payload); err != nil { - return body + return body, "" } changed := false if streaming, ok := payload["stream"].(bool); !ok || !streaming { @@ -485,17 +500,26 @@ func prepareAnonymousBody(body []byte, protocol wire.Protocol, legacy bool) []by if ensureAnonymousChatUsage(payload, protocol) { changed = true } - if ensureAnonymousTools(payload, protocol, legacy) { + var alias string + if legacy { + var toolChanged bool + alias, toolChanged = ensureLegacyAnonymousReadTool(payload, protocol) + if toolChanged { + changed = true + } + } else if ensureAnonymousTools(payload, protocol) { changed = true } if !changed { - return body + return body, alias } encoded, err := json.Marshal(payload) if err != nil { - return body + // The alias is dropped with the failed re-encode: the body never left + // the gateway, so there is nothing downstream to restore. + return body, "" } - return encoded + return encoded, alias } // ensureAnonymousChatUsage keeps token usage available when a non-streaming @@ -518,25 +542,184 @@ func ensureAnonymousChatUsage(payload map[string]any, protocol wire.Protocol) bo return true } -// ensureAnonymousTools makes the tool list read as an agent session. -// -// On the legacy /zen/v1 gate (legacy == true), only tool names matter ("bash" + -// "read" required): client-declared tools are preserved untouched so downstream -// function calling schemas for tools like "read", "edit", "write", or "bash" -// are never clobbered, and lightweight v1.3.7 stubs are appended for any -// missing legacy core tools. -// -// On the newer /inference gate (legacy == false), every recognised OpenCode -// tool must carry its genuine CLI 2.x definition and missing core tools are -// appended with genuine schemas; unrecognised client tools are left untouched. -func ensureAnonymousTools(payload map[string]any, protocol wire.Protocol, legacy bool) bool { - coreTools := anonymousCoreTools - if legacy { - coreTools = legacyAnonymousCoreTools - } +// ensureLegacyAnonymousReadTool satisfies the legacy /zen/v1 free-tier gate +// using only the single "read" tool requirement: +// 1. If the client already has an exact "read" tool, nothing is injected or +// renamed. +// 2. If the client has a read-related tool (case-insensitive match such as +// "reAd", "read_file", "ReadFile", etc.), that tool is renamed to "read" +// (preserving its client description and schema), any matching tool_choice +// or historical tool call in the request is rewritten to "read", and the +// client's original tool name is returned so responses can be restored. +// 3. If the client has no read-related tool at all, a single "read" stub tool +// is appended with a deprecated/ignore description so the model never +// invokes it. +func ensureLegacyAnonymousReadTool(payload map[string]any, protocol wire.Protocol) (string, bool) { raw, exists := payload["tools"] if !exists { - payload["tools"] = anonymousToolset(protocol, coreTools, legacy) + payload["tools"] = []any{legacyDeprecatedReadTool(protocol)} + return "", true + } + items, ok := raw.([]any) + if !ok { + return "", false + } + candidateIndex := -1 + candidateName := "" + for index, item := range items { + name := anonymousToolName(protocol, item) + if name == "" { + continue + } + if name == legacyAnonymousReadTool { + return "", false + } + if strings.EqualFold(name, legacyAnonymousReadTool) { + candidateIndex = index + candidateName = name + break + } + if candidateIndex < 0 && isLegacyReadToolCandidate(name) { + candidateIndex = index + candidateName = name + } + } + if candidateIndex >= 0 { + setAnonymousToolName(protocol, items[candidateIndex], legacyAnonymousReadTool) + rewriteLegacyRequestToolReferences(payload, protocol, candidateName, legacyAnonymousReadTool) + payload["tools"] = items + return candidateName, true + } + items = append(items, legacyDeprecatedReadTool(protocol)) + payload["tools"] = items + return "", true +} + +// isLegacyReadToolCandidate reports whether a client tool name represents a +// read-related tool (case-insensitive), such as "reAd", "read_file", +// "ReadFile", "file_read", or "view_file". +func isLegacyReadToolCandidate(name string) bool { + lower := strings.ToLower(strings.TrimSpace(name)) + if lower == "" { + return false + } + if lower == "read" { + return true + } + normalized := strings.NewReplacer("_", "", "-", "", ".", "").Replace(lower) + switch normalized { + case "readfile", "fileread", "readfiles", "readtext", "readtextfile", "readcontent", "viewfile": + return true + } + return strings.HasPrefix(lower, "read_") || strings.HasPrefix(lower, "read-") || strings.HasSuffix(lower, "_read") || strings.HasSuffix(lower, "-read") +} + +// setAnonymousToolName updates the function name of a tool definition in-place +// while keeping all other client fields (description, parameters/input_schema, +// strict, etc.) intact. +func setAnonymousToolName(protocol wire.Protocol, item any, name string) { + entry, ok := item.(map[string]any) + if !ok { + return + } + if protocol == wire.Chat { + if function, ok := entry["function"].(map[string]any); ok { + function["name"] = name + } + return + } + entry["name"] = name +} + +// rewriteLegacyRequestToolReferences renames any named tool_choice and +// assistant tool-call history entries from fromName to toName so the outbound +// request stays self-consistent after a client read tool is aliased to "read". +func rewriteLegacyRequestToolReferences(payload map[string]any, protocol wire.Protocol, fromName, toName string) { + if fromName == "" || fromName == toName { + return + } + switch protocol { + case wire.Chat: + if choice, ok := payload["tool_choice"].(map[string]any); ok { + if function, ok := choice["function"].(map[string]any); ok && jsonutil.StringAt(function, "name") == fromName { + function["name"] = toName + } + } + for _, rawMsg := range jsonutil.SliceAt(payload, "messages") { + msg, _ := rawMsg.(map[string]any) + for _, rawCall := range jsonutil.SliceAt(msg, "tool_calls") { + call, _ := rawCall.(map[string]any) + if function, ok := call["function"].(map[string]any); ok && jsonutil.StringAt(function, "name") == fromName { + function["name"] = toName + } + } + } + case wire.Responses: + if choice, ok := payload["tool_choice"].(map[string]any); ok && jsonutil.StringAt(choice, "name") == fromName { + choice["name"] = toName + } + for _, rawItem := range jsonutil.SliceAt(payload, "input") { + item, _ := rawItem.(map[string]any) + if jsonutil.StringAt(item, "type") == "function_call" && jsonutil.StringAt(item, "name") == fromName { + item["name"] = toName + } + } + case wire.Anthropic: + if choice, ok := payload["tool_choice"].(map[string]any); ok && jsonutil.StringAt(choice, "name") == fromName { + choice["name"] = toName + } + for _, rawMsg := range jsonutil.SliceAt(payload, "messages") { + msg, _ := rawMsg.(map[string]any) + for _, rawBlock := range jsonutil.SliceAt(msg, "content") { + block, _ := rawBlock.(map[string]any) + if jsonutil.StringAt(block, "type") == "tool_use" && jsonutil.StringAt(block, "name") == fromName { + block["name"] = toName + } + } + } + } +} + +// legacyDeprecatedReadTool builds the single placeholder "read" tool injected +// on the legacy /zen/v1 lane when the client request has no read-related tool +// of its own. Its description marks the tool as deprecated and instructs the +// model to ignore it completely. +func legacyDeprecatedReadTool(protocol wire.Protocol) map[string]any { + parameters := map[string]any{"type": "object", "properties": map[string]any{}} + switch protocol { + case wire.Chat: + return map[string]any{ + "type": "function", + "function": map[string]any{ + "name": legacyAnonymousReadTool, + "description": legacyDeprecatedReadDescription, + "parameters": parameters, + }, + } + case wire.Anthropic: + return map[string]any{ + "name": legacyAnonymousReadTool, + "description": legacyDeprecatedReadDescription, + "input_schema": parameters, + } + default: + return map[string]any{ + "type": "function", + "name": legacyAnonymousReadTool, + "description": legacyDeprecatedReadDescription, + "parameters": parameters, + } + } +} + +// ensureAnonymousTools makes the tool list read as an OpenCode 2.x agent +// session on the /inference gate: every recognised OpenCode tool carries its +// genuine CLI 2.x definition and missing core tools are appended with genuine +// schemas; unrecognised client tools are left untouched. +func ensureAnonymousTools(payload map[string]any, protocol wire.Protocol) bool { + raw, exists := payload["tools"] + if !exists { + payload["tools"] = anonymousToolset(protocol, anonymousCoreTools) return true } items, ok := raw.([]any) @@ -551,24 +734,19 @@ func ensureAnonymousTools(payload map[string]any, protocol wire.Protocol, legacy continue } present[name] = true - // Only the /inference gate validates tool schemas byte-for-byte; on the - // legacy /zen/v1 lane, preserve client-declared tool definitions as in - // v1.3.7 so downstream tools sharing OpenCode names keep their schemas. - if !legacy { - if _, known := knownOpencodeTool(name); known { - items[index] = anonymousTool(protocol, name, false) - changed = true - } + if _, known := knownOpencodeTool(name); known { + items[index] = anonymousTool(protocol, name) + changed = true } } - missing := make([]string, 0, len(coreTools)) - for _, name := range coreTools { + missing := make([]string, 0, len(anonymousCoreTools)) + for _, name := range anonymousCoreTools { if !present[name] { missing = append(missing, name) } } if len(missing) > 0 { - items = append(items, anonymousToolset(protocol, missing, legacy)...) + items = append(items, anonymousToolset(protocol, missing)...) changed = true } if !changed { @@ -589,41 +767,32 @@ func anonymousToolName(protocol wire.Protocol, item any) string { return jsonutil.StringAt(entry, "name") } -// anonymousToolset returns the wire-shaped definitions for the given tool names -// (using minimal v1.3.7 stubs when legacy is true, or genuine CLI definitions -// for the /inference gate when legacy is false). -func anonymousToolset(protocol wire.Protocol, names []string, legacy bool) []any { +// anonymousToolset returns the wire-shaped genuine CLI 2.x definitions for the +// given tool names on the /inference gate. +func anonymousToolset(protocol wire.Protocol, names []string) []any { if names == nil { - if legacy { - names = legacyAnonymousCoreTools - } else { - names = anonymousCoreTools - } + names = anonymousCoreTools } tools := make([]any, 0, len(names)) for _, name := range names { - if tool := anonymousTool(protocol, name, legacy); tool != nil { + if tool := anonymousTool(protocol, name); tool != nil { tools = append(tools, tool) } } return tools } -// anonymousTool renders one core tool in the shape the given protocol expects. -// On the legacy /zen/v1 lane (legacy == true) it emits the lightweight v1.3.7 -// stub without embedding the large CLI 2.x schema; on the /inference lane -// (legacy == false) it uses the genuine OpenCode definition whenever one is -// known. SystemOne payloads never reach this path (prepareAnonymousBody -// forwards them verbatim), so it falls back to the Responses/default shape. -func anonymousTool(protocol wire.Protocol, name string, legacy bool) map[string]any { +// anonymousTool renders one core tool in the shape the given protocol expects, +// using the genuine OpenCode CLI 2.x definition whenever one is known. +// SystemOne payloads never reach this path (prepareAnonymousBody forwards them +// verbatim), so it falls back to the Responses/default shape. +func anonymousTool(protocol wire.Protocol, name string) map[string]any { description := "Agent tool " + name parameters := map[string]any{"type": "object", "properties": map[string]any{}} - if !legacy { - if definition, ok := knownOpencodeTool(name); ok { - description = definition.Description - if definition.Parameters != nil { - parameters = definition.Parameters - } + if definition, ok := knownOpencodeTool(name); ok { + description = definition.Description + if definition.Parameters != nil { + parameters = definition.Parameters } } switch protocol { @@ -694,21 +863,22 @@ func (g *Gateway) dumpOutboundBodies(route models.Route, bodies map[config.Tier] } // shapeKeyBody normalizes key-tier free-model wire bodies to agent shape -// (stream + core tools), mirroring the anonymous lane. Upstream now +// (stream + required tools), mirroring the anonymous lane. Upstream now // rejects non-agent-shaped free-tier requests on every lane with 403 -// FreeTierError; paid models keep their original bodies. It reports -// whether the body changed so the gateway can collapse the SSE stream a -// non-streaming client receives back. -func (g *Gateway) shapeKeyBody(body []byte, route models.Route, tier config.Tier) ([]byte, bool) { +// FreeTierError; paid models keep their original bodies. It reports the +// shaped body, any legacy "read" tool alias that must be restored in the +// response, and whether the body changed so the gateway can collapse the SSE +// stream a non-streaming client receives back. +func (g *Gateway) shapeKeyBody(body []byte, route models.Route, tier config.Tier) ([]byte, string, bool) { if !g.catalog.IsFreeModel(route.ID) { - return body, false + return body, "", false } baseURL := g.cfg.Upstream.Zen if tier == config.TierGo { baseURL = g.cfg.Upstream.Go } - shaped := prepareAnonymousBody(body, route.ProtocolFor(tier), isLegacyZenUpstream(baseURL)) - return shaped, !bytes.Equal(shaped, body) + shaped, alias := prepareAnonymousBody(body, route.ProtocolFor(tier), isLegacyZenUpstream(baseURL)) + return shaped, alias, !bytes.Equal(shaped, body) } // key. It performs no failover at all — no anonymous lane, no other key and no @@ -731,11 +901,13 @@ func (g *Gateway) doSelectedKeyUpstream(ctx context.Context, route models.Route, if len(body) == 0 { return nil, fmt.Errorf("no prepared %s request body", override.Tier), 0 } - if shaped, changed := g.shapeKeyBody(body, route, override.Tier); changed { + shaped, alias, changed := g.shapeKeyBody(body, route, override.Tier) + if changed { body = shaped - if meta := telemetry.MetaFromContext(ctx); meta != nil { - meta.Shaped = true - } + } + if meta := telemetry.MetaFromContext(ctx); meta != nil { + meta.Shaped = changed + meta.ToolAlias = alias } proxy := nodes.Proxy(node) if proxy == nil { @@ -782,11 +954,13 @@ func (g *Gateway) doKeyUpstream(ctx context.Context, route models.Route, bodies if len(body) == 0 { return nil, fmt.Errorf("no prepared %s request body", route.Tier), 0 } - if shaped, changed := g.shapeKeyBody(body, route, route.Tier); changed { + shaped, alias, changed := g.shapeKeyBody(body, route, route.Tier) + if changed { body = shaped - if meta := telemetry.MetaFromContext(ctx); meta != nil { - meta.Shaped = true - } + } + if meta := telemetry.MetaFromContext(ctx); meta != nil { + meta.Shaped = changed + meta.ToolAlias = alias } for attempts < g.cfg.Retry.MaxAttempts { if ctx.Err() != nil { diff --git a/internal/protocol/response.go b/internal/protocol/response.go index 8976e95..bd8ca78 100644 --- a/internal/protocol/response.go +++ b/internal/protocol/response.go @@ -1,6 +1,7 @@ package protocol import ( + "bytes" "encoding/json" "fmt" "strings" @@ -455,3 +456,92 @@ func ResponseUsage(protocol Protocol, body []byte) (Usage, bool) { } return decodeOpenAIUsage(usage), true } + +// RenameResponseTool rewrites tool call names matching fromName to toName inside +// a serialized JSON response or SSE event data object for the given protocol. +// It is used to restore a client's original read-related tool name (e.g. +// "reAd" or "read_file") after the legacy /zen/v1 shaper aliased it to "read" +// on the outbound wire. +// +// fromName is guaranteed not to occur as a substring of any other text in a +// response (tool names are never echoed back inside content), so a byte-level +// occurrence check is enough to skip the re-marshal for the vast majority of +// frames. +func RenameResponseTool(body []byte, protocol Protocol, fromName, toName string) []byte { + if fromName == "" || toName == "" || fromName == toName || len(body) == 0 { + return body + } + if !bytes.Contains(body, []byte(fromName)) { + return body + } + var payload map[string]any + if err := json.Unmarshal(body, &payload); err != nil { + return body + } + if !renameToolInMap(payload, protocol, fromName, toName) { + return body + } + encoded, err := json.Marshal(payload) + if err != nil { + return body + } + return encoded +} + +func renameToolInMap(payload map[string]any, protocol Protocol, fromName, toName string) bool { + changed := false + switch protocol { + case Chat: + for _, rawChoice := range jsonutil.SliceAt(payload, "choices") { + choice, _ := rawChoice.(map[string]any) + for _, key := range []string{"message", "delta"} { + container := jsonutil.MapAt(choice, key) + for _, rawCall := range jsonutil.SliceAt(container, "tool_calls") { + call, _ := rawCall.(map[string]any) + if function, ok := call["function"].(map[string]any); ok && jsonutil.StringAt(function, "name") == fromName { + function["name"] = toName + changed = true + } + } + } + } + case Responses: + if item, ok := payload["item"].(map[string]any); ok { + if jsonutil.StringAt(item, "type") == "function_call" && jsonutil.StringAt(item, "name") == fromName { + item["name"] = toName + changed = true + } + } + for _, rawItem := range jsonutil.SliceAt(payload, "output") { + item, _ := rawItem.(map[string]any) + if jsonutil.StringAt(item, "type") == "function_call" && jsonutil.StringAt(item, "name") == fromName { + item["name"] = toName + changed = true + } + } + if resp, ok := payload["response"].(map[string]any); ok { + for _, rawItem := range jsonutil.SliceAt(resp, "output") { + item, _ := rawItem.(map[string]any) + if jsonutil.StringAt(item, "type") == "function_call" && jsonutil.StringAt(item, "name") == fromName { + item["name"] = toName + changed = true + } + } + } + case Anthropic: + if block, ok := payload["content_block"].(map[string]any); ok { + if jsonutil.StringAt(block, "type") == "tool_use" && jsonutil.StringAt(block, "name") == fromName { + block["name"] = toName + changed = true + } + } + for _, rawBlock := range jsonutil.SliceAt(payload, "content") { + block, _ := rawBlock.(map[string]any) + if jsonutil.StringAt(block, "type") == "tool_use" && jsonutil.StringAt(block, "name") == fromName { + block["name"] = toName + changed = true + } + } + } + return changed +} diff --git a/internal/protocol/stream.go b/internal/protocol/stream.go index e25b0bf..221c404 100644 --- a/internal/protocol/stream.go +++ b/internal/protocol/stream.go @@ -4,11 +4,14 @@ import ( "bufio" "bytes" "context" + "encoding/json" "errors" "fmt" "io" "net/http" "strings" + + "opencode2api/internal/jsonutil" ) var errStreamUpstreamFailure = errors.New("upstream stream failure delivered") @@ -186,6 +189,204 @@ func ForwardStream(ctx context.Context, w http.ResponseWriter, reader io.Reader, 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 diff --git a/internal/telemetry/http.go b/internal/telemetry/http.go index bafd796..418eba9 100644 --- a/internal/telemetry/http.go +++ b/internal/telemetry/http.go @@ -36,10 +36,15 @@ type RequestMeta struct { // upstream account that served the request. ServerKeyID string // Shaped marks a key-tier free-model request whose wire body was - // normalized to agent shape (stream + core tools) like the anonymous + // normalized to agent shape (stream + required tools) like the anonymous // lane. Non-streaming responses for shaped requests arrive as SSE // and must be collapsed like anonymous ones. - Shaped bool + Shaped bool + // ToolAlias holds the client's original read-related tool name (e.g. + // "reAd" or "read_file") when the legacy /zen/v1 shaper renamed it to + // "read" on the outbound wire. When non-empty, "read" tool calls in the + // upstream response are rewritten back to ToolAlias before returning. + ToolAlias string Usage protocol.Usage UsageReported bool AttemptOutcome string