Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.it.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ L'iniezione dipende dal protocollo: i messaggi system e developer di Chat vengon

### Normalizzazione degli strumenti

Su ogni richiesta di un modello gratuito già normalizzata il gateway rinomina ogni strumento nominato del client in `clint_<name>` — i caratteri fuori da `A-Za-z0-9_-` diventano `_`, i nomi sono limitati a 64 byte e le collisioni ricevono il suffisso `_2`, `_3`, … — e aggiunge l'intero set ufficiale del canale: le definizioni 1.x (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) su `/zen` e il set di 13 strumenti 2.x su `/inference` e `/zen/go`. Le richieste senza `tools`, con un array vuoto o con il vecchio array `functions` ricevono lo stesso set; `tool_choice`, il vecchio `function_call` e la cronologia delle tool call vengono riscritti di conseguenza. Gli strumenti integrati di Responses e quelli server di Anthropic restano invariati.
Su ogni richiesta di un modello gratuito già normalizzata il gateway rinomina ogni strumento nominato del client in un nome opaco fatto di un prefisso di namespace più i primi 12 caratteri esadecimali dello SHA-256 del nome dello strumento — i caratteri fuori da `A-Za-z0-9_-` diventano `_`, i nomi sono limitati a 64 byte e le collisioni ricevono il suffisso `_2`, `_3`, … — e aggiunge l'intero set ufficiale del canale: le definizioni 1.x (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) su `/zen` e il set di 13 strumenti 2.x su `/inference` e `/zen/go`. Le richieste senza `tools`, con un array vuoto o con il vecchio array `functions` ricevono lo stesso set; `tool_choice`, il vecchio `function_call` e la cronologia delle tool call vengono riscritti di conseguenza. Gli strumenti integrati di Responses e quelli server di Anthropic restano invariati.

Le risposte vengono riportate ai nomi dichiarati dal client, sia in streaming sia non, incluso un nome Chat suddiviso su più chunk. Se l'upstream chiama uno strumento ufficiale che il client non ha dichiarato, il nome passa invariato: il gateway non riproduce né nasconde le chiamate ufficiali.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ The override applies only to the gateway-injected block; a client's existing off

### Tool shaping

On every shaped free-model request the gateway replaces each named client tool with an opaque wire name, `clint_` plus the first 12 hex characters of the client name's SHA-256 (collisions get a `_2`, `_3`, … suffix), and appends the complete official tool set for the lane: the 1.x definitions (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) on `/zen`, and the 13-tool 2.x set on `/inference` and `/zen/go`. The digest is not cosmetic: the free tier refuses a shaped body that still carries another client's vocabulary — a Claude Code request that declares both `ReportFindings` and `ScheduleWakeup` is answered with `FreeUsageLimitError`, and removing either name lifts the refusal — so no client tool name reaches the wire. Requests without a `tools` array, with an empty one, or with a legacy `functions` array get the same official set; `tool_choice`, legacy `function_call`, and the tool-call history are rewritten to match. Responses built-in tools and Anthropic server tools are left untouched.
On every shaped free-model request the gateway replaces each named client tool with an opaque wire name — a namespace prefix plus the first 12 hex characters of the client name's SHA-256 (collisions get a `_2`, `_3`, … suffix) — and appends the complete official tool set for the lane: the 1.x definitions (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) on `/zen`, and the 13-tool 2.x set on `/inference` and `/zen/go`. The prefix must stay a plain namespace a tool name could carry: the free tier refuses a prefix it has learned to recognise, so it must not become a gateway marker. The digest is not cosmetic: the free tier refuses a shaped body that still carries another client's vocabulary — a Claude Code request that declares both `ReportFindings` and `ScheduleWakeup` is answered with `FreeUsageLimitError`, and removing either name lifts the refusal — so no client tool name reaches the wire. Requests without a `tools` array, with an empty one, or with a legacy `functions` array get the same official set; `tool_choice`, legacy `function_call`, and the tool-call history are rewritten to match. Responses built-in tools and Anthropic server tools are left untouched.

Responses are restored to client-declared names in streaming and non-streaming replies, including Chat tool names split across chunks. An official name is mapped when the client declared an equivalent tool; otherwise it remains unchanged in the response. In request history, unknown tool calls and official calls without a declared equivalent are removed with their paired results, and a named `tool_choice` targeting one of those dropped calls is removed too.

Expand Down
2 changes: 1 addition & 1 deletion README.vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Việc chèn phụ thuộc giao thức: các message system và developer của

### Định hình công cụ

Trên mỗi request mô hình miễn phí đã được định hình, gateway đổi tên từng công cụ có tên của client thành `clint_<name>` — ký tự ngoài `A-Za-z0-9_-` thành `_`, tên tối đa 64 byte, trùng tên thì thêm hậu tố `_2`, `_3`, … — rồi thêm toàn bộ bộ công cụ chính thức của làn: định nghĩa 1.x (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) trên `/zen`, và bộ 13 công cụ 2.x trên `/inference` và `/zen/go`. Request không có `tools`, có mảng rỗng, hoặc dùng mảng `functions` kiểu cũ cũng nhận bộ chính thức; `tool_choice`, `function_call` kiểu cũ và lịch sử tool call được viết lại tương ứng. Công cụ tích hợp của Responses và công cụ máy chủ của Anthropic được giữ nguyên.
Trên mỗi request mô hình miễn phí đã được định hình, gateway đổi tên từng công cụ có tên của client thành một tên không lộ nội dung: tiền tố namespace cộng 12 ký tự hex đầu của mã SHA-256 tính trên tên công cụ — ký tự ngoài `A-Za-z0-9_-` thành `_`, tên tối đa 64 byte, trùng tên thì thêm hậu tố `_2`, `_3`, … — rồi thêm toàn bộ bộ công cụ chính thức của làn: định nghĩa 1.x (`bash`, `edit`, `glob`, `grep`, `read`, `skill`, `task`, `todowrite`, `webfetch`, `websearch`, `write`) trên `/zen`, và bộ 13 công cụ 2.x trên `/inference` và `/zen/go`. Request không có `tools`, có mảng rỗng, hoặc dùng mảng `functions` kiểu cũ cũng nhận bộ chính thức; `tool_choice`, `function_call` kiểu cũ và lịch sử tool call được viết lại tương ứng. Công cụ tích hợp của Responses và công cụ máy chủ của Anthropic được giữ nguyên.

Phản hồi được khôi phục về tên client đã khai báo, cho cả luồng và không luồng, kể cả tên công cụ Chat bị chia qua nhiều chunk. Nếu upstream gọi một công cụ chính thức mà client chưa khai báo, tên đó được truyền nguyên: gateway không phát lại hay che các lời gọi công cụ chính thức.

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ System One 将自由文本状态与类型化问题配对,并返回类型化答

### 工具整形

每个经过整形的免费模型请求中,网关会把客户端声明的每个具名工具改名为不透明的线上名称 `clint_` 加上客户端名称 SHA-256 的前 12 个十六进制字符(冲突时追加 `_2`、`_3` 等后缀),并追加该通道的完整官方工具集:`/zen` 使用 1.x 定义(`bash`、`edit`、`glob`、`grep`、`read`、`skill`、`task`、`todowrite`、`webfetch`、`websearch`、`write`),`/inference` 与 `/zen/go` 使用 13 个工具的 2.x 定义。没有 `tools`、`tools` 为空数组或使用旧版 `functions` 数组的请求同样会补全官方工具集;`tool_choice`、旧版 `function_call` 和工具调用历史会同步改写。Responses 内置工具和 Anthropic 服务端工具保持不变。
每个经过整形的免费模型请求中,网关会把客户端声明的每个具名工具改名为不透明的线上名称:命名空间前缀加上客户端名称 SHA-256 的前 12 个十六进制字符(冲突时追加 `_2`、`_3` 等后缀),并追加该通道的完整官方工具集:`/zen` 使用 1.x 定义(`bash`、`edit`、`glob`、`grep`、`read`、`skill`、`task`、`todowrite`、`webfetch`、`websearch`、`write`),`/inference` 与 `/zen/go` 使用 13 个工具的 2.x 定义。没有 `tools`、`tools` 为空数组或使用旧版 `functions` 数组的请求同样会补全官方工具集;`tool_choice`、旧版 `function_call` 和工具调用历史会同步改写。Responses 内置工具和 Anthropic 服务端工具保持不变。

响应(包括流式与非流式,以及拆分到多个 Chat 分块中的工具名)会恢复为客户端声明的名称。只有在客户端声明了等价工具时,官方工具名才会映射到客户端名称;否则响应中的名称保持原样。整形请求历史时,未知工具调用以及没有客户端等价工具的官方调用会连同配对的结果一起移除;若具名 `tool_choice` 指向被移除的工具,也会一并移除。

Expand Down
2 changes: 1 addition & 1 deletion internal/gateway/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func (g *Gateway) inferenceClient(proxy *proxyTransport, baseURL, model string)
// tools are renamed and completed for the lane v1 selects; an unchanged body is
// returned as is.
//
// Every named client tool becomes an opaque clint_<digest> (see
// Every named client tool becomes an opaque wire name (see
// protocol.ClientToolWireName: a client tool name that survives on the wire is
// what the free tier refuses) and the full official set for the lane is
// appended, so the request looks like an official CLI session on both the v1
Expand Down
4 changes: 2 additions & 2 deletions internal/protocol/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func ResponseUsage(protocol Protocol, body []byte) (Usage, bool) {
// that mentions neither a client alias nor a tool-call marker cannot name a
// tool, so it is handed on without decoding. The markers are the protocol's
// own spellings; folding an unprefixed official name (todowrite) needs the
// frame decoded even when no clint_ alias is present.
// frame decoded even when no wire-name alias is present.
func mayCarryToolName(body []byte) bool {
return bytes.Contains(body, []byte(ClientToolPrefix)) ||
bytes.Contains(body, []byte(`"tool_calls"`)) ||
Expand All @@ -520,7 +520,7 @@ func mayCarryToolName(body []byte) bool {
// RenameResponseTool rewrites tool call names that carry a client alias back to
// the name the client declared, inside a serialized JSON response or SSE event
// data object for the given protocol. It is the counterpart of the outbound
// clint_ renaming applied to shaped free-tier requests.
// wire-name renaming applied to shaped free-tier requests.
//
// mayCarryToolName is only a fast path that skips the unmarshal for frames that
// cannot contain a tool name; every rewrite is a map lookup after decoding.
Expand Down
11 changes: 9 additions & 2 deletions internal/protocol/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import (
// OpenCode set the gateway appends, and lets responses be restored to the names
// the client declared. What follows it is a digest, never the client name: the
// free tier refuses bodies that still read as another client's session.
const ClientToolPrefix = "clint_"
//
// The free tier refuses the literal "clint_" (case-insensitively) in a tool
// name while serving any other prefix, so this must stay a plain namespace a
// tool name could carry. mcp_ also prefixes MCP clients' mcp__<server>__<tool>
// names, so refusing it would refuse their traffic too.
// shortcut: upstream may learn mcp_ as well; rotate this value when 429s return
// only on tool-bearing requests.
const ClientToolPrefix = "mcp_"

// clientToolDigestLength is how many hex characters of the client name digest
// a wire name carries. Twelve is far past the collision point for the handful
Expand Down Expand Up @@ -184,7 +191,7 @@ func (r *ToolRenames) HasLongerPrefix(prefix string) bool {
// Resolve maps a tool name the upstream reported back to the name the client
// declared. It is the response-side counterpart of the wire rename: an exact
// wire name restores, an official alias maps to the matching client tool, a
// clint_ prefix is stripped as a best-effort recovery (for example after a
// wire-name prefix is stripped as a best-effort recovery (for example after a
// configuration reload mid-stream), and a name that
// differs from exactly one declared client tool only in case, or else only in
// case and separators (todowrite → todo_write), is folded onto that tool. A
Expand Down
2 changes: 1 addition & 1 deletion internal/telemetry/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type RequestMeta struct {
// and must be collapsed like anonymous ones.
Shaped bool
// ToolRenames counts the client tools the free-tier shaper renamed with
// the clint_ prefix. The upstream response is rewritten back to the
// the wire-name prefix. The upstream response is rewritten back to the
// client's names before it is returned.
ToolRenames int
Usage protocol.Usage
Expand Down
Loading