Kilo Gateway(网关) 提供商集成设计
Kilo Gateway(网关) 提供商集成设计
Section titled “Kilo Gateway(网关) 提供商集成设计”本文档概述了将“Kilo Gateway(网关)”作为一等提供商集成到 OpenClaw 中的设计,该设计以现有的 OpenRouter 实现为蓝本。Kilo Gateway(网关) 使用与 OpenAI 兼容的补全 API,但具有不同的基础 URL。
1. 提供商命名
Section titled “1. 提供商命名”建议:kilocode
基本原理:
- 与提供的用户配置示例匹配(
kilocode提供商密钥) - 与现有的提供商命名模式一致(例如
openrouter、opencode、moonshot) - 简短且易于记忆
- 避免与通用的“kilo”或“gateway”术语混淆
考虑过的替代方案:kilo-gateway - 已拒绝,因为带连字符的名称在代码库中不太常见,且 kilocode 更简洁。
2. 默认模型参考
Section titled “2. 默认模型参考”建议:kilocode/anthropic/claude-opus-4.6
基本原理:
- 基于用户配置示例
- Claude Opus 4.5 是一个功能强大的默认模型
- 显式模型选择避免依赖自动路由
3. 基础 URL 配置
Section titled “3. 基础 URL 配置”建议:硬编码默认值,允许配置覆盖
- 默认基础 URL:
https://api.kilo.ai/api/gateway/ - 可配置: 是,通过
models.providers.kilocode.baseUrl
这与 Moonshot、Venice 和 Synthetic 等其他提供商使用的模式相匹配。
4. 模型扫描
Section titled “4. 模型扫描”建议:最初不设置专门的模型扫描端点
基本原理:
- Kilo Gateway(网关) 代理到 OpenRouter,因此模型是动态的
- 用户可以在其配置中手动配置模型
- 如果 Kilo Gateway(网关) 在将来暴露
/models端点,则可以添加扫描功能
5. 特殊处理
Section titled “5. 特殊处理”建议:继承 OpenRouter 对 Anthropic 模型的行为
由于 Kilo Gateway(网关) 代理到 OpenRouter,因此应应用相同的特殊处理:
anthropic/*模型的缓存 TTL 资格anthropic/*模型的额外参数(cacheControlTtl)- 转录策略遵循 OpenRouter 模式
需修改的文件
Section titled “需修改的文件”核心凭据管理
Section titled “核心凭据管理”1. src/commands/onboard-auth.credentials.ts
Section titled “1. src/commands/onboard-auth.credentials.ts”添加:
export const KILOCODE_DEFAULT_MODEL_REF = "kilocode/anthropic/claude-opus-4.6";
export async function setKilocodeApiKey(key: string, agentDir?: string) { upsertAuthProfile({ profileId: "kilocode:default", credential: { type: "api_key", provider: "kilocode", key, }, agentDir: resolveAuthAgentDir(agentDir), });}2. src/agents/model-auth.ts
Section titled “2. src/agents/model-auth.ts”添加到 resolveEnvApiKey() 中的 envMap:
const envMap: Record<string, string> = { // ... existing entries kilocode: "KILOCODE_API_KEY",};3. src/config/io.ts
Section titled “3. src/config/io.ts”添加到 SHELL_ENV_EXPECTED_KEYS:
const SHELL_ENV_EXPECTED_KEYS = [ // ... existing entries "KILOCODE_API_KEY",];4. src/commands/onboard-auth.config-core.ts
Section titled “4. src/commands/onboard-auth.config-core.ts”添加新函数:
export const KILOCODE_BASE_URL = "https://api.kilo.ai/api/gateway/";
export function applyKilocodeProviderConfig(cfg: OpenClawConfig): OpenClawConfig { const models = { ...cfg.agents?.defaults?.models }; models[KILOCODE_DEFAULT_MODEL_REF] = { ...models[KILOCODE_DEFAULT_MODEL_REF], alias: models[KILOCODE_DEFAULT_MODEL_REF]?.alias ?? "Kilo Gateway", };
const providers = { ...cfg.models?.providers }; const existingProvider = providers.kilocode; const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<string, unknown> as { apiKey?: string }; const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined; const normalizedApiKey = resolvedApiKey?.trim();
providers.kilocode = { ...existingProviderRest, baseUrl: KILOCODE_BASE_URL, api: "openai-completions", ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), };
return { ...cfg, agents: { ...cfg.agents, defaults: { ...cfg.agents?.defaults, models, }, }, models: { mode: cfg.models?.mode ?? "merge", providers, }, };}
export function applyKilocodeConfig(cfg: OpenClawConfig): OpenClawConfig { const next = applyKilocodeProviderConfig(cfg); const existingModel = next.agents?.defaults?.model; return { ...next, agents: { ...next.agents, defaults: { ...next.agents?.defaults, model: { ...(existingModel && "fallbacks" in (existingModel as Record<string, unknown>) ? { fallbacks: (existingModel as { fallbacks?: string[] }).fallbacks, } : undefined), primary: KILOCODE_DEFAULT_MODEL_REF, }, }, }, };}认证选择系统
Section titled “认证选择系统”5. src/commands/onboard-types.ts
Section titled “5. src/commands/onboard-types.ts”添加到 AuthChoice 类型:
export type AuthChoice = // ... existing choices "kilocode-api-key";// ...添加到 OnboardOptions:
export type OnboardOptions = { // ... existing options kilocodeApiKey?: string; // ...};6. src/commands/auth-choice-options.ts
Section titled “6. src/commands/auth-choice-options.ts”添加到 AuthChoiceGroupId:
export type AuthChoiceGroupId = // ... existing groups "kilocode";// ...添加到 AUTH_CHOICE_GROUP_DEFS:
{ value: "kilocode", label: "Kilo Gateway", hint: "API key (OpenRouter-compatible)", choices: ["kilocode-api-key"],},添加到 buildAuthChoiceOptions():
options.push({ value: "kilocode-api-key", label: "Kilo Gateway API key", hint: "OpenRouter-compatible gateway",});7. src/commands/auth-choice.preferred-provider.ts
Section titled “7. src/commands/auth-choice.preferred-provider.ts”添加映射:
const PREFERRED_PROVIDER_BY_AUTH_CHOICE: Partial<Record<AuthChoice, string>> = { // ... existing mappings "kilocode-api-key": "kilocode",};认证选择应用
Section titled “认证选择应用”8. src/commands/auth-choice.apply.api-providers.ts
Section titled “8. src/commands/auth-choice.apply.api-providers.ts”添加导入:
import { // ... existing imports applyKilocodeConfig, applyKilocodeProviderConfig, KILOCODE_DEFAULT_MODEL_REF, setKilocodeApiKey,} from "./onboard-auth.js";添加 kilocode-api-key 的处理逻辑:
if (authChoice === "kilocode-api-key") { const store = ensureAuthProfileStore(params.agentDir, { allowKeychainPrompt: false, }); const profileOrder = resolveAuthProfileOrder({ cfg: nextConfig, store, provider: "kilocode", }); const existingProfileId = profileOrder.find((profileId) => Boolean(store.profiles[profileId])); const existingCred = existingProfileId ? store.profiles[existingProfileId] : undefined; let profileId = "kilocode:default"; let mode: "api_key" | "oauth" | "token" = "api_key"; let hasCredential = false;
if (existingProfileId && existingCred?.type) { profileId = existingProfileId; mode = existingCred.type === "oauth" ? "oauth" : existingCred.type === "token" ? "token" : "api_key"; hasCredential = true; }
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "kilocode") { await setKilocodeApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir); hasCredential = true; }
if (!hasCredential) { const envKey = resolveEnvApiKey("kilocode"); if (envKey) { const useExisting = await params.prompter.confirm({ message: `Use existing KILOCODE_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`, initialValue: true, }); if (useExisting) { await setKilocodeApiKey(envKey.apiKey, params.agentDir); hasCredential = true; } } }
if (!hasCredential) { const key = await params.prompter.text({ message: "Enter Kilo Gateway API key", validate: validateApiKeyInput, }); await setKilocodeApiKey(normalizeApiKeyInput(String(key)), params.agentDir); hasCredential = true; }
if (hasCredential) { nextConfig = applyAuthProfileConfig(nextConfig, { profileId, provider: "kilocode", mode, }); } { const applied = await applyDefaultModelChoice({ config: nextConfig, setDefaultModel: params.setDefaultModel, defaultModel: KILOCODE_DEFAULT_MODEL_REF, applyDefaultConfig: applyKilocodeConfig, applyProviderConfig: applyKilocodeProviderConfig, noteDefault: KILOCODE_DEFAULT_MODEL_REF, noteAgentModel, prompter: params.prompter, }); nextConfig = applied.config; agentModelOverride = applied.agentModelOverride ?? agentModelOverride; } return { config: nextConfig, agentModelOverride };}同时在函数顶部添加 tokenProvider 映射:
if (params.opts.tokenProvider === "kilocode") { authChoice = "kilocode-api-key";}CLI 注册
Section titled “CLI 注册”9. src/cli/program/register.onboard.ts
Section titled “9. src/cli/program/register.onboard.ts”添加 CLI 选项:
.option("--kilocode-api-key <key>", "Kilo Gateway API key")添加到操作处理程序:
kilocodeApiKey: opts.kilocodeApiKey as string | undefined,更新 auth-choice 帮助文本:
.option( "--auth-choice <choice>", "Auth: setup-token|token|chutes|openai-codex|openai-api-key|openrouter-api-key|kilocode-api-key|ai-gateway-api-key|...",)非交互式新手引导
Section titled “非交互式新手引导”10. src/commands/onboard-non-interactive/local/auth-choice.ts
Section titled “10. src/commands/onboard-non-interactive/local/auth-choice.ts”添加 kilocode-api-key 的处理逻辑:
if (authChoice === "kilocode-api-key") { const resolved = await resolveNonInteractiveApiKey({ provider: "kilocode", cfg: baseConfig, flagValue: opts.kilocodeApiKey, flagName: "--kilocode-api-key", envVar: "KILOCODE_API_KEY", }); await setKilocodeApiKey(resolved.apiKey, agentDir); nextConfig = applyAuthProfileConfig(nextConfig, { profileId: "kilocode:default", provider: "kilocode", mode: "api_key", }); // ... apply default model}11. src/commands/onboard-auth.ts
Section titled “11. src/commands/onboard-auth.ts”添加导出:
export { // ... existing exports applyKilocodeConfig, applyKilocodeProviderConfig, KILOCODE_BASE_URL,} from "./onboard-auth.config-core.js";
export { // ... existing exports KILOCODE_DEFAULT_MODEL_REF, setKilocodeApiKey,} from "./onboard-auth.credentials.js";特殊处理(可选)
Section titled “特殊处理(可选)”12. src/agents/pi-embedded-runner/cache-ttl.ts
Section titled “12. src/agents/pi-embedded-runner/cache-ttl.ts”为 Gateway(网关) 模型添加 Kilo Anthropic 支持:
export function isCacheTtlEligibleProvider(provider: string, modelId: string): boolean { const normalizedProvider = provider.toLowerCase(); const normalizedModelId = modelId.toLowerCase(); if (normalizedProvider === "anthropic") return true; if (normalizedProvider === "openrouter" && normalizedModelId.startsWith("anthropic/")) return true; if (normalizedProvider === "kilocode" && normalizedModelId.startsWith("anthropic/")) return true; return false;}13. src/agents/transcript-policy.ts
Section titled “13. src/agents/transcript-policy.ts”添加 Kilo Gateway(网关) 处理(类似于 OpenRouter):
const isKilocodeGemini = provider === "kilocode" && modelId.toLowerCase().includes("gemini");
// Include in needsNonImageSanitize checkconst needsNonImageSanitize = isGoogle || isAnthropic || isMistral || isOpenRouterGemini || isKilocodeGemini;用户配置示例
Section titled “用户配置示例”{ "models": { "mode": "merge", "providers": { "kilocode": { "baseUrl": "https://api.kilo.ai/api/gateway/", "apiKey": "xxxxx", "api": "openai-completions", "models": [ { "id": "anthropic/claude-opus-4.6", "name": "Anthropic: Claude Opus 4.6" }, { "id": "minimax/minimax-m2.5:free", "name": "Minimax: Minimax M2.5" } ] } } }}认证配置文件结构
Section titled “认证配置文件结构”{ "profiles": { "kilocode:default": { "type": "api_key", "provider": "kilocode", "key": "xxxxx" } }}测试注意事项
Section titled “测试注意事项”-
单元测试:
- 测试
setKilocodeApiKey()写入正确的配置文件 - 测试
applyKilocodeConfig()设置正确的默认值 - 测试
resolveEnvApiKey("kilocode")返回正确的环境变量
- 测试
-
集成测试:
- 使用
--auth-choice kilocode-api-key测试设置流程 - 使用
--kilocode-api-key测试非交互式设置 - 使用
kilocode/前缀测试模型选择
- 使用
-
端到端 (E2E) 测试:
- 通过 Kilo API 测试实际的 Gateway(网关) 调用(实时测试)
- 现有用户无需迁移
- 新用户可以立即使用
kilocode-api-key认证选择 - 现有的使用
kilocode提供商的手动配置将继续有效
未来考虑事项
Section titled “未来考虑事项”-
模型目录: 如果 Kilo Gateway(网关) 暴露了
/models端点,请添加类似于scanOpenRouterModels()的扫描支持 -
OAuth 支持: 如果 Kilo Gateway(网关) 添加了 OAuth,请相应地扩展身份验证系统
-
速率限制: 如果需要,考虑添加针对 Kilo Gateway(网关) 的特定速率限制处理
-
文档: 在
docs/providers/kilocode.md添加文档以解释设置和用法
| 文件 | 更改类型 | 描述 |
|---|---|---|
src/commands/onboard-auth.credentials.ts | 添加 | KILOCODE_DEFAULT_MODEL_REF, setKilocodeApiKey() |
src/agents/model-auth.ts | 修改 | 将 kilocode 添加到 envMap |
src/config/io.ts | 修改 | 将 KILOCODE_API_KEY 添加到 shell 环境变量键 |
src/commands/onboard-auth.config-core.ts | 添加 | applyKilocodeProviderConfig(), applyKilocodeConfig() |
src/commands/onboard-types.ts | 修改 | 将 kilocode-api-key 添加到 AuthChoice,将 kilocodeApiKey 添加到选项 |
src/commands/auth-choice-options.ts | 修改 | 添加 kilocode 组和选项 |
src/commands/auth-choice.preferred-provider.ts | 修改 | 添加 kilocode-api-key 映射 |
src/commands/auth-choice.apply.api-providers.ts | 修改 | 添加 kilocode-api-key 处理 |
src/cli/program/register.onboard.ts | 修改 | 添加 --kilocode-api-key 选项 |
src/commands/onboard-non-interactive/local/auth-choice.ts | 修改 | 添加非交互式处理 |
src/commands/onboard-auth.ts | 修改 | 导出新函数 |
src/agents/pi-embedded-runner/cache-ttl.ts | 修改 | 添加 kilocode 支持 |
src/agents/transcript-policy.ts | 修改 | 添加 kilocode Gemini 处理 |