mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-01-30 06:12:06 +00:00
feat: add ccr activate command for Agent SDK integration
This commit adds a new `ccr activate` command that outputs environment variables in shell-friendly format, making it easy to integrate claude-code-router with Agent SDK applications. Usage: eval $(ccr activate) This sets the following environment variables: - ANTHROPIC_AUTH_TOKEN - ANTHROPIC_API_KEY - ANTHROPIC_BASE_URL - NO_PROXY - DISABLE_TELEMETRY - DISABLE_COST_WARNINGS - API_TIMEOUT_MS - CLAUDE_CODE_USE_BEDROCK The implementation refactors the environment variable creation logic from `ccr code` into a shared `createEnvVariables()` function in `src/utils/createEnvVariables.ts`, ensuring consistency between both commands. Related: #855
This commit is contained in:
26
README.md
26
README.md
@@ -256,6 +256,32 @@ This command provides an interactive interface to:
|
||||
|
||||
The CLI tool validates all inputs and provides helpful prompts to guide you through the configuration process, making it easy to manage complex setups without editing JSON files manually.
|
||||
|
||||
### 6. Activate Command (Environment Variables Setup)
|
||||
|
||||
The `activate` command allows you to set up environment variables globally in your shell, enabling you to use the `claude` command directly or integrate Claude Code Router with applications built using the Agent SDK.
|
||||
|
||||
To activate the environment variables, run:
|
||||
|
||||
```shell
|
||||
eval "$(ccr activate)"
|
||||
```
|
||||
|
||||
This command outputs the necessary environment variables in shell-friendly format, which are then set in your current shell session. After activation, you can:
|
||||
|
||||
- **Use `claude` command directly**: Run `claude` commands without needing to use `ccr code`. The `claude` command will automatically route requests through Claude Code Router.
|
||||
- **Integrate with Agent SDK applications**: Applications built with the Anthropic Agent SDK will automatically use the configured router and models.
|
||||
|
||||
The `activate` command sets the following environment variables:
|
||||
|
||||
- `ANTHROPIC_AUTH_TOKEN`: API key from your configuration
|
||||
- `ANTHROPIC_BASE_URL`: The local router endpoint (default: `http://127.0.0.1:3456`)
|
||||
- `NO_PROXY`: Set to `127.0.0.1` to prevent proxy interference
|
||||
- `DISABLE_TELEMETRY`: Disables telemetry
|
||||
- `DISABLE_COST_WARNINGS`: Disables cost warnings
|
||||
- `API_TIMEOUT_MS`: API timeout from your configuration
|
||||
|
||||
> **Note**: Make sure the Claude Code Router service is running (`ccr start`) before using the activated environment variables. The environment variables are only valid for the current shell session. To make them persistent, you can add `eval "$(ccr activate)"` to your shell configuration file (e.g., `~/.zshrc` or `~/.bashrc`).
|
||||
|
||||
#### Providers
|
||||
|
||||
The `Providers` array is where you define the different model providers you want to use. Each provider object requires:
|
||||
|
||||
26
README_zh.md
26
README_zh.md
@@ -207,6 +207,32 @@ ccr ui
|
||||
|
||||

|
||||
|
||||
### 5. Activate 命令(环境变量设置)
|
||||
|
||||
`activate` 命令允许您在 shell 中全局设置环境变量,使您能够直接使用 `claude` 命令或将 Claude Code Router 与使用 Agent SDK 构建的应用程序集成。
|
||||
|
||||
要激活环境变量,请运行:
|
||||
|
||||
```shell
|
||||
eval "$(ccr activate)"
|
||||
```
|
||||
|
||||
此命令会以 shell 友好的格式输出必要的环境变量,这些变量将在当前的 shell 会话中设置。激活后,您可以:
|
||||
|
||||
- **直接使用 `claude` 命令**:无需使用 `ccr code` 即可运行 `claude` 命令。`claude` 命令将自动通过 Claude Code Router 路由请求。
|
||||
- **与 Agent SDK 应用程序集成**:使用 Anthropic Agent SDK 构建的应用程序将自动使用配置的路由器和模型。
|
||||
|
||||
`activate` 命令设置以下环境变量:
|
||||
|
||||
- `ANTHROPIC_AUTH_TOKEN`: 来自配置的 API 密钥
|
||||
- `ANTHROPIC_BASE_URL`: 本地路由器端点(默认:`http://127.0.0.1:3456`)
|
||||
- `NO_PROXY`: 设置为 `127.0.0.1` 以防止代理干扰
|
||||
- `DISABLE_TELEMETRY`: 禁用遥测
|
||||
- `DISABLE_COST_WARNINGS`: 禁用成本警告
|
||||
- `API_TIMEOUT_MS`: 来自配置的 API 超时时间
|
||||
|
||||
> **注意**:在使用激活的环境变量之前,请确保 Claude Code Router 服务正在运行(`ccr start`)。环境变量仅在当前 shell 会话中有效。要使其持久化,您可以将 `eval "$(ccr activate)"` 添加到您的 shell 配置文件(例如 `~/.zshrc` 或 `~/.bashrc`)中。
|
||||
|
||||
#### Providers
|
||||
|
||||
`Providers` 数组是您定义要使用的不同模型提供商的地方。每个提供商对象都需要:
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
getServiceInfo,
|
||||
} from "./utils/processCheck";
|
||||
import { runModelSelector } from "./utils/modelSelector"; // ADD THIS LINE
|
||||
import { activateCommand } from "./utils/activateCommand";
|
||||
import { version } from "../package.json";
|
||||
import { spawn, exec } from "child_process";
|
||||
import { PID_FILE, REFERENCE_COUNT_FILE } from "./constants";
|
||||
@@ -21,13 +22,14 @@ const HELP_TEXT = `
|
||||
Usage: ccr [command]
|
||||
|
||||
Commands:
|
||||
start Start server
|
||||
start Start server
|
||||
stop Stop server
|
||||
restart Restart server
|
||||
status Show server status
|
||||
statusline Integrated statusline
|
||||
code Execute claude command
|
||||
model Interactive model selection and configuration
|
||||
activate Output environment variables for shell integration
|
||||
ui Open the web UI in browser
|
||||
-v, version Show version information
|
||||
-h, help Show help information
|
||||
@@ -36,6 +38,7 @@ Example:
|
||||
ccr start
|
||||
ccr code "Write a Hello World"
|
||||
ccr model
|
||||
eval "$(ccr activate)" # Set environment variables globally
|
||||
ccr ui
|
||||
`;
|
||||
|
||||
@@ -116,6 +119,10 @@ async function main() {
|
||||
case "model":
|
||||
await runModelSelector();
|
||||
break;
|
||||
case "activate":
|
||||
case "env":
|
||||
await activateCommand();
|
||||
break;
|
||||
case "code":
|
||||
if (!isRunning) {
|
||||
console.log("Service not running, starting service...");
|
||||
|
||||
19
src/utils/activateCommand.ts
Normal file
19
src/utils/activateCommand.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createEnvVariables } from "./createEnvVariables";
|
||||
|
||||
/**
|
||||
* Execute the env command
|
||||
*/
|
||||
export const activateCommand = async () => {
|
||||
const envVars = await createEnvVariables();
|
||||
|
||||
// Output in shell-friendly format for eval
|
||||
for (const [key, value] of Object.entries(envVars)) {
|
||||
if (value === "") {
|
||||
console.log(`export ${key}=""`);
|
||||
} else if (value === undefined) {
|
||||
console.log(`unset ${key}`);
|
||||
} else {
|
||||
console.log(`export ${key}="${value}"`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,23 +7,13 @@ import {
|
||||
} from "./processCheck";
|
||||
import { quote } from 'shell-quote';
|
||||
import minimist from "minimist";
|
||||
import { createEnvVariables } from "./createEnvVariables";
|
||||
|
||||
|
||||
export async function executeCodeCommand(args: string[] = []) {
|
||||
// Set environment variables
|
||||
// Set environment variables using shared function
|
||||
const config = await readConfigFile();
|
||||
const port = config.PORT || 3456;
|
||||
const env: Record<string, string> = {
|
||||
ANTHROPIC_AUTH_TOKEN: config?.APIKEY || "test",
|
||||
ANTHROPIC_API_KEY: '',
|
||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`,
|
||||
NO_PROXY: `127.0.0.1`,
|
||||
DISABLE_TELEMETRY: 'true',
|
||||
DISABLE_COST_WARNINGS: 'true',
|
||||
API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000), // Default to 10 minutes if not set
|
||||
// Reset CLAUDE_CODE_USE_BEDROCK when running with ccr code
|
||||
CLAUDE_CODE_USE_BEDROCK: undefined,
|
||||
};
|
||||
const env = await createEnvVariables();
|
||||
const settingsFlag = {
|
||||
env
|
||||
};
|
||||
|
||||
23
src/utils/createEnvVariables.ts
Normal file
23
src/utils/createEnvVariables.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { readConfigFile } from ".";
|
||||
|
||||
/**
|
||||
* Get environment variables for Agent SDK/Claude Code integration
|
||||
* This function is shared between `ccr env` and `ccr code` commands
|
||||
*/
|
||||
export const createEnvVariables = async () => {
|
||||
const config = await readConfigFile();
|
||||
const port = config.PORT || 3456;
|
||||
const apiKey = config.APIKEY || "test";
|
||||
|
||||
return {
|
||||
ANTHROPIC_AUTH_TOKEN: apiKey,
|
||||
ANTHROPIC_API_KEY: "",
|
||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`,
|
||||
NO_PROXY: "127.0.0.1",
|
||||
DISABLE_TELEMETRY: "true",
|
||||
DISABLE_COST_WARNINGS: "true",
|
||||
API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000),
|
||||
// Reset CLAUDE_CODE_USE_BEDROCK when running with ccr
|
||||
CLAUDE_CODE_USE_BEDROCK: undefined,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user