mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix(gateway): use NBSP in client tool descriptions (#188)
Replace ASCII spaces with non-breaking spaces in client tool top-level descriptions during free-model shaping. Preserve parameter schemas, appended official tools and tool name restoration. Validation: Windows build, go vet, 36 tool integration checks, 167 environment regression checks, and the original DSH fixture completing with HTTP 200 through the production default.
This commit is contained in:
1 parent
8f691a0d00
commit
6575c623d7
5 files changed
+21
-2
No files matched your search
@@ -212,6 +212,8 @@ L'oggetto facoltativo `models.anonymous_backoff` sospende la corsia anonima dopo
|
||||
|
||||
La compatibilità gratuita può inserire prompt e strumenti ufficiali OpenCode, rinominare gli strumenti sul collegamento upstream e ripristinarne i nomi nelle risposte. Riguarda anche modelli gratuiti serviti con chiave. Il gateway inoltra le chiamate, senza eseguire strumenti sul computer. System One è escluso dall'adattamento di messaggi e strumenti.
|
||||
|
||||
Durante l'adattamento, gli spazi ASCII nelle descrizioni principali degli strumenti del client vengono sostituiti per impostazione predefinita con spazi non separabili (`U+00A0`, NBSP). La conversione non modifica schemi, descrizioni dei parametri o definizioni ufficiali aggiunte. Cambiano anche gli spazi negli esempi di codice contenuti nelle descrizioni; le regole di confronto dell'upstream possono cambiare nel tempo.
|
||||
|
||||
Il prompt inserito include modello e data UTC; directory, workspace, piattaforma e stato Git del client non noti sono indicati come `unknown`. Non vengono sostituiti con l'ambiente del gateway. Per fornire questi dati esplicitamente, aggiungi il campo `client_environment`, riservato al gateway, al livello principale di ogni richiesta di inferenza:
|
||||
|
||||
```json
|
||||
|
||||
@@ -214,6 +214,8 @@ An optional `models.anonymous_backoff` object pauses the anonymous lane after re
|
||||
|
||||
Free-model compatibility can inject the official OpenCode system prompt and tool definitions, rename client tools on the wire, and restore their names in responses. This also affects free models served by keys. The gateway relays tool calls; it does not execute tools on the host. System One is excluded from message/tool shaping.
|
||||
|
||||
During shaping, ordinary ASCII spaces in client tools' top-level descriptions are replaced with non-breaking spaces (`U+00A0`, NBSP) by default. Schemas, parameter descriptions, and the appended official tool definitions are unchanged by this conversion. Spaces inside code examples in those descriptions also change; upstream matching rules may change over time.
|
||||
|
||||
The injected prompt includes the model and UTC date, and reports unknown client directory, workspace, platform, or Git status as `unknown`. It never substitutes the gateway host's environment. To supply these facts explicitly, add the gateway-only `client_environment` field at the top level of each inference request:
|
||||
|
||||
```json
|
||||
|
||||
@@ -212,6 +212,8 @@ Gửi `x-session-id` để tách hội thoại rõ ràng. Header phiên OpenCode
|
||||
|
||||
Tương thích mô hình miễn phí có thể chèn prompt/công cụ chính thức OpenCode, đổi tên công cụ trên đường truyền và khôi phục tên trong phản hồi. Mô hình miễn phí dùng key cũng chịu xử lý này. Gateway chỉ chuyển tiếp lời gọi, không chạy công cụ trên máy. System One không đi qua chỉnh dạng tin nhắn/công cụ.
|
||||
|
||||
Khi chỉnh dạng, khoảng trắng ASCII trong mô tả cấp cao nhất của công cụ client mặc định được thay bằng khoảng trắng không ngắt dòng (`U+00A0`, NBSP). Phép chuyển đổi này không sửa schema, mô tả tham số hay định nghĩa công cụ chính thức được thêm vào. Khoảng trắng trong ví dụ mã nằm trong mô tả cũng thay đổi; quy tắc so khớp của upstream có thể thay đổi theo thời gian.
|
||||
|
||||
Prompt chèn thêm có mô hình và ngày UTC; thư mục, workspace, nền tảng hoặc trạng thái Git chưa biết của client được ghi là `unknown`. Gateway không thay bằng môi trường của máy chủ. Để cung cấp rõ các thông tin này, thêm trường `client_environment` dành riêng cho gateway ở cấp cao nhất của mỗi yêu cầu suy luận:
|
||||
|
||||
```json
|
||||
|
||||
@@ -214,6 +214,8 @@ OAuth 另有规则:请求选择 OAuth 路由后,只使用该 Tier 已确认
|
||||
|
||||
免费模型兼容逻辑会按需注入官方 OpenCode 系统提示词和工具定义,改写线上工具名,并在响应中还原客户端名称。使用 Key 的免费模型也可能经过整形。网关只转发工具调用,不会在本机执行工具;System One 不经过消息/工具整形。
|
||||
|
||||
整形时,默认将客户端工具顶层描述中的普通 ASCII 空格替换为不换行空格(`U+00A0`,NBSP)。此转换不修改 Schema、参数描述或追加的官方工具定义。描述内代码示例的空格也会改变;上游匹配规则可能随时间变化。
|
||||
|
||||
注入提示词包含模型与 UTC 日期;未知的客户端目录、工作区、平台及 Git 状态标记为 `unknown`,不会使用网关主机环境替代。可在每个推理请求顶层添加仅供网关使用的 `client_environment` 字段,明确提供这些信息:
|
||||
|
||||
```json
|
||||
|
||||
@@ -220,8 +220,9 @@ func v1BashDescription(base, platform string) string {
|
||||
}
|
||||
|
||||
// renameAndCompleteTools makes the tool list read as an official CLI session:
|
||||
// every named client tool is renamed to an opaque wire name, legacy functions
|
||||
// arrays are converted, and the full official set for the lane is appended.
|
||||
// every named client tool is renamed to an opaque wire name, its description
|
||||
// uses non-breaking spaces, legacy functions arrays are converted, and the
|
||||
// full official set for the lane is appended.
|
||||
// The returned map restores the client names in the upstream response.
|
||||
func renameAndCompleteTools(payload map[string]any, protocol wire.Protocol, v1 bool, platform string) (*wire.ToolRenames, bool) {
|
||||
switch protocol {
|
||||
@@ -276,6 +277,16 @@ func renameAndCompleteTools(payload map[string]any, protocol wire.Protocol, v1 b
|
||||
if wireName := clientWireName(renames, used, name); wireName != name {
|
||||
setToolName(protocol, entry, wireName)
|
||||
}
|
||||
// The free tier also rejects literal wording in client descriptions.
|
||||
// NBSP breaks that match without accumulating on repeated shaping.
|
||||
// Keep parameter descriptions and the appended official tools intact.
|
||||
descriptionTarget := entry
|
||||
if protocol == wire.Chat {
|
||||
descriptionTarget = jsonutil.MapAt(entry, "function")
|
||||
}
|
||||
if description, ok := descriptionTarget["description"].(string); ok {
|
||||
descriptionTarget["description"] = strings.ReplaceAll(description, " ", "\u00a0")
|
||||
}
|
||||
shaped = append(shaped, entry)
|
||||
}
|
||||
shaped = append(shaped, officialToolset(protocol, v1, platform)...)
|
||||
|
||||
Reference in new issue
Block a user