Skip to content

Commit 717822d

Browse files
lishengzxcclaude
andcommitted
fix: infer API protocol from base-url and add codex auth.json
- OpenCode/OpenClaw/Hermes: dynamically choose API protocol based on whether the base-url is an anthropic endpoint or openai-compatible - Codex: write ~/.codex/auth.json alongside config.toml so users don't need to manually export OPENAI_API_KEY - Add shared isAnthropicEndpoint() helper in writers/utils.ts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e1dea36 commit 717822d

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

packages/cli/src/commands/agent/writers/codex.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { homedir } from "os";
22
import { join } from "path";
33
import { mkdirSync, writeFileSync, renameSync } from "fs";
4-
import { backup, type AgentDef } from "./utils.ts";
4+
import { backup, readJson, writeJsonAtomic, type AgentDef } from "./utils.ts";
55

66
export default {
77
label: "Codex",
@@ -27,16 +27,16 @@ export default {
2727
writeFileSync(tmp, toml, { mode: 0o600 });
2828
renameSync(tmp, configPath);
2929

30-
// Also hint about OPENAI_API_KEY env var
31-
const shell = process.platform === "win32" ? "powershell" : "shell";
32-
const envHint =
33-
shell === "powershell"
34-
? `Set env: [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "${apiKey}", "User")`
35-
: `Set env: export OPENAI_API_KEY="${apiKey}"`;
30+
// auth.json — store API key for Codex to read
31+
const authPath = join(homedir(), ".codex", "auth.json");
32+
backup(authPath);
33+
const auth = readJson(authPath);
34+
auth.OPENAI_API_KEY = apiKey;
35+
writeJsonAtomic(authPath, auth);
3636

3737
return {
38-
paths: [configPath],
39-
nextStep: `${envHint}\n Then run \`codex\` to start using Codex with DashScope.`,
38+
paths: [configPath, authPath],
39+
nextStep: "Run `codex` to start using Codex with DashScope.",
4040
};
4141
},
4242
} satisfies AgentDef;

packages/cli/src/commands/agent/writers/hermes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { homedir } from "os";
22
import { join } from "path";
33
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from "fs";
44
import yaml from "yaml";
5-
import { backup, type AgentDef } from "./utils.ts";
5+
import { backup, isAnthropicEndpoint, type AgentDef } from "./utils.ts";
66

77
export default {
88
label: "Hermes Agent",
@@ -20,11 +20,12 @@ export default {
2020
}
2121
}
2222

23+
const apiMode = isAnthropicEndpoint(baseUrl) ? "anthropic_messages" : "chat_completions";
2324
config.model = {
2425
default: model,
2526
provider: "custom",
2627
base_url: baseUrl,
27-
api_mode: "anthropic_messages",
28+
api_mode: apiMode,
2829
api_key: apiKey,
2930
};
3031

packages/cli/src/commands/agent/writers/openclaw.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { homedir } from "os";
22
import { join } from "path";
3-
import { backup, readJson, writeJsonAtomic, type AgentDef } from "./utils.ts";
3+
import { backup, readJson, writeJsonAtomic, isAnthropicEndpoint, type AgentDef } from "./utils.ts";
44

55
export default {
66
label: "OpenClaw",
@@ -14,10 +14,11 @@ export default {
1414
const models = (config.models ?? {}) as Record<string, unknown>;
1515
models.mode = "merge";
1616
const providers = (models.providers ?? {}) as Record<string, unknown>;
17+
const api = isAnthropicEndpoint(baseUrl) ? "anthropic-messages" : "openai-completions";
1718
providers.bailian = {
1819
baseUrl,
1920
apiKey,
20-
api: "anthropic-messages",
21+
api,
2122
models: [
2223
{
2324
id: model,

packages/cli/src/commands/agent/writers/opencode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { homedir } from "os";
22
import { join } from "path";
3-
import { backup, readJson, writeJsonAtomic, type AgentDef } from "./utils.ts";
3+
import { backup, readJson, writeJsonAtomic, isAnthropicEndpoint, type AgentDef } from "./utils.ts";
44

55
export default {
66
label: "OpenCode",
@@ -13,8 +13,9 @@ export default {
1313
if (!config.$schema) config.$schema = "https://opencode.ai/config.json";
1414

1515
const provider = (config.provider ?? {}) as Record<string, unknown>;
16+
const npm = isAnthropicEndpoint(baseUrl) ? "@ai-sdk/anthropic" : "@ai-sdk/openai-compatible";
1617
provider.bailian = {
17-
npm: "@ai-sdk/anthropic",
18+
npm,
1819
name: "Alibaba Cloud Model Studio",
1920
options: { baseURL: baseUrl, apiKey },
2021
models: { [model]: { name: model } },

packages/cli/src/commands/agent/writers/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ export function backup(path: string): void {
3838
const ts = Math.floor(Date.now() / 1000);
3939
copyFileSync(path, `${path}.bak.${ts}`);
4040
}
41+
42+
export function isAnthropicEndpoint(baseUrl: string): boolean {
43+
return baseUrl.includes("/apps/anthropic");
44+
}

0 commit comments

Comments
 (0)