Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(runtime): preserve provider apiStyle for foreign catalog models

Totopo27d66ae6067184
diff --git a/docs/spec/03-runtime/11-provider-model-system.md b/docs/spec/03-runtime/11-provider-model-system.md
index da4b3c674..c4a72930e 100644
--- a/docs/spec/03-runtime/11-provider-model-system.md
+++ b/docs/spec/03-runtime/11-provider-model-system.md
@@ -676,9 +676,12 @@ role; this override is model-scoped and does not change other providers.
A catalog entry may additionally pin a model-level wire API (for example,
`api: "openai-responses"`). When present it wins over the provider-wide
-`apiStyle`, so responses-only models under an `opencode_go` provider are sent
-through the Responses adapter instead of Chat Completions. Without a
-model-level pin the provider-wide style applies unchanged.
+`apiStyle` if compatible with the provider's protocol family (so responses-only
+models under an `opencode_go` provider are sent through the Responses adapter
+instead of Chat Completions). Foreign catalog wire APIs (such as
+`google-generative-ai` or `anthropic-messages` matched on relay endpoints) do
+not overwrite an OpenAI-compatible provider's configured wire style (issue #1310).
+Without a model-level pin the provider-wide style applies unchanged.
This is the **universal escape hatch** guaranteeing market coverage beyond native integrations.
diff --git a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
index 5336bb26c..3599471fe 100644
--- a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
+++ b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
@@ -571,7 +571,7 @@ UI 可能会显示层级提示,但默认情况下不得硬阻止未知模型
这是**通用逃生舱**,保证超出原生集成之外的市场覆盖范围。
-目录条目还可以额外固定模型级 wire API(例如 `api: "openai-responses"`)。存在时它优先于 provider 级 `apiStyle`,因此 `opencode_go` 下的 responses-only 模型会走 Responses adapter 而非 Chat Completions;没有模型级固定时保持 provider 级风格不变。
+目录条目还可以额外固定模型级 wire API(例如 `api: "openai-responses"`)。当该 API 与 provider 的协议族兼容时,它优先于 provider 级 `apiStyle`(因此 `opencode_go` 下的 responses-only 模型会走 Responses adapter 而非 Chat Completions)。外部 catalog 的异构 wire API(例如 relay 端点上匹配到的 `google-generative-ai` 或 `anthropic-messages`)不会覆盖 OpenAI-compatible provider 配置的 wire style(issue #1310);没有模型级固定时保持 provider 级风格不变。
### 16.1 Responses 流终止(pi-ai 补丁)
diff --git a/packages/agent-runtime/src/provider-binding.test.ts b/packages/agent-runtime/src/provider-binding.test.ts
index ed92aba4c..46c2d10a6 100644
--- a/packages/agent-runtime/src/provider-binding.test.ts
+++ b/packages/agent-runtime/src/provider-binding.test.ts
@@ -849,6 +849,48 @@ describe("buildProviderModel model-level wire API", () => {
expect(model.api).toBe("openai-completions");
});
+ it("does not overwrite OpenAI completions with foreign catalog APIs such as Google or Anthropic (issue #1310)", () => {
+ const geminiRelay = buildProviderModel({
+ ...keyedProvider,
+ id: "cliproxy",
+ name: "CliProxy",
+ baseUrl: "http://127.0.0.1:8317/v1",
+ modelId: "gemini-3.8-flash",
+ apiStyle: "chat_completions",
+ modelConfig: {
+ source: "models.dev",
+ name: "Gemini 3.8 Flash",
+ baseUrl: "http://127.0.0.1:8317/v1",
+ api: "google-generative-ai",
+ reasoning: true,
+ input: ["text", "image"],
+ contextWindow: 1048576,
+ maxTokens: 65536,
+ },
+ }) as any;
+ expect(geminiRelay.api).toBe("openai-completions");
+
+ const claudeRelay = buildProviderModel({
+ ...keyedProvider,
+ id: "openai-proxy",
+ name: "OpenAI Proxy",
+ baseUrl: "http://127.0.0.1:8317/v1",
+ modelId: "claude-sonnet-4-6",
+ apiStyle: "chat_completions",
+ modelConfig: {
+ source: "models.dev",
+ name: "Claude Sonnet 4.6",
+ baseUrl: "http://127.0.0.1:8317/v1",
+ api: "anthropic-messages",
+ reasoning: true,
+ input: ["text", "image"],
+ contextWindow: 200000,
+ maxTokens: 64000,
+ },
+ }) as any;
+ expect(claudeRelay.api).toBe("openai-completions");
+ });
+
it("posts responses models to the responses endpoint", async () => {
const provider = responsesCatalogProvider;
const model = buildProviderModel(provider);
diff --git a/packages/agent-runtime/src/provider-binding.ts b/packages/agent-runtime/src/provider-binding.ts
index 2d494403b..a8135e18a 100644
--- a/packages/agent-runtime/src/provider-binding.ts
+++ b/packages/agent-runtime/src/provider-binding.ts
@@ -33,6 +33,7 @@ import {
OPENCODE_GO_API_STYLE,
OPENCODE_GO_BASE_URL,
resolveApiStyle,
+ normalizeApiStyle,
resolveNativeWebSearch,
nativeWebSearchTransport,
deepseekRequestCompat,
@@ -184,15 +185,30 @@ export function providerRequestKey(provider: RuntimeProviderConfig): string {
* Resolve the wire API for one provider row. A catalog entry may pin a wire
* API that differs from the provider-wide style (e.g. responses-only models
* under an opencode_go provider, which defaults to Chat Completions). Honor
- * the model-level api when present so such models are not sent through the
- * wrong adapter (the gateway answers 500, see #105).
+ * the model-level api when compatible or when the provider has no explicit
+ * wire style; never let a foreign-family catalog match (e.g. Google Generative AI
+ * on a Gemini model served by an OpenAI-compatible relay) overwrite the provider's
+ * wire protocol (see #105, #1310).
*/
export function apiBindingForProviderModel(provider: RuntimeProviderConfig): ApiBinding {
return apiBindingForStyle(providerRequestTransport(provider).apiStyle);
}
function providerRequestTransport(provider: RuntimeProviderConfig) {
- const apiStyle = resolveApiStyle(provider.modelConfig?.api) ?? provider.apiStyle;
+ const modelStyle = resolveApiStyle(provider.modelConfig?.api);
+ const resolvedProviderStyle = resolveApiStyle(provider.apiStyle);
+ const providerStyle = resolvedProviderStyle ?? (provider.apiStyle ? normalizeApiStyle(provider.apiStyle) : undefined);
+ const isOpenAiStyle = (style: string | undefined) =>
+ style === "chat_completions" ||
+ style === "responses" ||
+ style === "openai_codex_responses" ||
+ style === OPENCODE_GO_API_STYLE;
+ const isCompatible =
+ !providerStyle ||
+ !modelStyle ||
+ modelStyle === providerStyle ||
+ (isOpenAiStyle(providerStyle) && isOpenAiStyle(modelStyle));
+ const apiStyle = (isCompatible ? modelStyle : undefined) ?? provider.apiStyle;
return nativeWebSearchTransport({
apiStyle,
baseUrl: provider.baseUrl ?? provider.modelConfig?.baseUrl ?? apiBindingForStyle(apiStyle).defaultBaseUrl,