mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-11 08:10:48 +08:00
重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
This commit is contained in:
parent
4a23b715a2
commit
dbbe3add6a
14438 changed files with 2693620 additions and 2546 deletions
14
openclaw/extensions/kilocode/api.ts
Normal file
14
openclaw/extensions/kilocode/api.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export { buildKilocodeProvider, buildKilocodeProviderWithDiscovery } from "./provider-catalog.js";
|
||||
export {
|
||||
buildKilocodeModelDefinition,
|
||||
KILOCODE_BASE_URL,
|
||||
KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
KILOCODE_DEFAULT_COST,
|
||||
KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
KILOCODE_DEFAULT_MODEL_ID,
|
||||
KILOCODE_DEFAULT_MODEL_NAME,
|
||||
KILOCODE_DEFAULT_MODEL_REF,
|
||||
KILOCODE_MODELS_URL,
|
||||
KILOCODE_MODEL_CATALOG,
|
||||
discoverKilocodeModels,
|
||||
} from "./provider-models.js";
|
||||
12
openclaw/extensions/kilocode/implicit-provider.test.ts
Normal file
12
openclaw/extensions/kilocode/implicit-provider.test.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { buildKilocodeProvider } from "./provider-catalog.js";
|
||||
|
||||
describe("Kilo Gateway implicit provider", () => {
|
||||
it("publishes the Kilo static provider catalog used by implicit provider setup", () => {
|
||||
const provider = buildKilocodeProvider();
|
||||
|
||||
expect(provider.baseUrl).toBe("https://api.kilo.ai/api/gateway/");
|
||||
expect(provider.api).toBe("openai-completions");
|
||||
expect(provider.models?.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
106
openclaw/extensions/kilocode/index.test.ts
Normal file
106
openclaw/extensions/kilocode/index.test.ts
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
||||
import type { Context, Model } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { registerSingleProviderPlugin } from "../../test/helpers/plugins/plugin-registration.js";
|
||||
import { expectPassthroughReplayPolicy } from "../../test/helpers/provider-replay-policy.ts";
|
||||
import plugin from "./index.js";
|
||||
|
||||
describe("kilocode provider plugin", () => {
|
||||
it("owns passthrough-gemini replay policy for Gemini-backed models", async () => {
|
||||
await expectPassthroughReplayPolicy({
|
||||
plugin,
|
||||
providerId: "kilocode",
|
||||
modelId: "gemini-2.5-pro",
|
||||
sanitizeThoughtSignatures: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("wires kilocode-thinking stream hooks", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload = { config: { thinkingConfig: { thinkingBudget: -1 } } } as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
capturedPayload = payload;
|
||||
return {} as never;
|
||||
};
|
||||
|
||||
const wrappedReasoning = provider.wrapStreamFn?.({
|
||||
provider: "kilocode",
|
||||
modelId: "openai/gpt-5.4",
|
||||
thinkingLevel: "high",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrappedReasoning?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "kilocode",
|
||||
id: "openai/gpt-5.4",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload).toMatchObject({
|
||||
reasoning: { effort: "high" },
|
||||
});
|
||||
|
||||
const wrappedAuto = provider.wrapStreamFn?.({
|
||||
provider: "kilocode",
|
||||
modelId: "kilo/auto",
|
||||
thinkingLevel: "high",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrappedAuto?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "kilocode",
|
||||
id: "kilo/auto",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload).not.toHaveProperty("reasoning");
|
||||
});
|
||||
|
||||
it("publishes configured Kilo models through plugin-owned catalog augmentation", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
expect(
|
||||
provider.augmentModelCatalog?.({
|
||||
config: {
|
||||
models: {
|
||||
providers: {
|
||||
kilocode: {
|
||||
models: [
|
||||
{
|
||||
id: "google/gemini-3-pro-preview",
|
||||
name: "Gemini 3 Pro Preview",
|
||||
input: ["text", "image"],
|
||||
reasoning: true,
|
||||
contextWindow: 1048576,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as never),
|
||||
).toEqual([
|
||||
{
|
||||
provider: "kilocode",
|
||||
id: "google/gemini-3-pro-preview",
|
||||
name: "Gemini 3 Pro Preview",
|
||||
input: ["text", "image"],
|
||||
reasoning: true,
|
||||
contextWindow: 1048576,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
42
openclaw/extensions/kilocode/index.ts
Normal file
42
openclaw/extensions/kilocode/index.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { readConfiguredProviderCatalogEntries } from "openclaw/plugin-sdk/provider-catalog-shared";
|
||||
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
|
||||
import { PASSTHROUGH_GEMINI_REPLAY_HOOKS } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
import { KILOCODE_THINKING_STREAM_HOOKS } from "openclaw/plugin-sdk/provider-stream-family";
|
||||
import { applyKilocodeConfig, KILOCODE_DEFAULT_MODEL_REF } from "./onboard.js";
|
||||
import { buildKilocodeProviderWithDiscovery } from "./provider-catalog.js";
|
||||
|
||||
const PROVIDER_ID = "kilocode";
|
||||
|
||||
export default defineSingleProviderPluginEntry({
|
||||
id: PROVIDER_ID,
|
||||
name: "Kilo Gateway Provider",
|
||||
description: "Bundled Kilo Gateway provider plugin",
|
||||
provider: {
|
||||
label: "Kilo Gateway",
|
||||
docsPath: "/providers/kilocode",
|
||||
auth: [
|
||||
{
|
||||
methodId: "api-key",
|
||||
label: "Kilo Gateway API key",
|
||||
hint: "API key (OpenRouter-compatible)",
|
||||
optionKey: "kilocodeApiKey",
|
||||
flagName: "--kilocode-api-key",
|
||||
envVar: "KILOCODE_API_KEY",
|
||||
promptMessage: "Enter Kilo Gateway API key",
|
||||
defaultModel: KILOCODE_DEFAULT_MODEL_REF,
|
||||
applyConfig: (cfg) => applyKilocodeConfig(cfg),
|
||||
},
|
||||
],
|
||||
catalog: {
|
||||
buildProvider: buildKilocodeProviderWithDiscovery,
|
||||
},
|
||||
augmentModelCatalog: ({ config }) =>
|
||||
readConfiguredProviderCatalogEntries({
|
||||
config,
|
||||
providerId: PROVIDER_ID,
|
||||
}),
|
||||
...PASSTHROUGH_GEMINI_REPLAY_HOOKS,
|
||||
...KILOCODE_THINKING_STREAM_HOOKS,
|
||||
isCacheTtlEligible: (ctx) => ctx.modelId.startsWith("anthropic/"),
|
||||
},
|
||||
});
|
||||
170
openclaw/extensions/kilocode/onboard.test.ts
Normal file
170
openclaw/extensions/kilocode/onboard.test.ts
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { resolveEnvApiKey } from "openclaw/plugin-sdk/provider-auth-runtime";
|
||||
import { resolveAgentModelPrimaryValue } from "openclaw/plugin-sdk/provider-onboard";
|
||||
import { captureEnv } from "openclaw/plugin-sdk/testing";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildKilocodeModelDefinition,
|
||||
KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
KILOCODE_DEFAULT_COST,
|
||||
KILOCODE_DEFAULT_MODEL_ID,
|
||||
} from "./api.js";
|
||||
import {
|
||||
applyKilocodeProviderConfig,
|
||||
applyKilocodeConfig,
|
||||
KILOCODE_BASE_URL,
|
||||
KILOCODE_DEFAULT_MODEL_REF,
|
||||
} from "./onboard.js";
|
||||
|
||||
const emptyCfg: OpenClawConfig = {};
|
||||
const KILOCODE_MODEL_IDS = ["kilo/auto"];
|
||||
|
||||
describe("Kilo Gateway provider config", () => {
|
||||
describe("constants", () => {
|
||||
it("KILOCODE_BASE_URL points to kilo openrouter endpoint", () => {
|
||||
expect(KILOCODE_BASE_URL).toBe("https://api.kilo.ai/api/gateway/");
|
||||
});
|
||||
|
||||
it("KILOCODE_DEFAULT_MODEL_REF includes provider prefix", () => {
|
||||
expect(KILOCODE_DEFAULT_MODEL_REF).toBe("kilocode/kilo/auto");
|
||||
});
|
||||
|
||||
it("KILOCODE_DEFAULT_MODEL_ID is kilo/auto", () => {
|
||||
expect(KILOCODE_DEFAULT_MODEL_ID).toBe("kilo/auto");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildKilocodeModelDefinition", () => {
|
||||
it("returns correct model shape", () => {
|
||||
const model = buildKilocodeModelDefinition();
|
||||
expect(model.id).toBe(KILOCODE_DEFAULT_MODEL_ID);
|
||||
expect(model.name).toBe("Kilo Auto");
|
||||
expect(model.reasoning).toBe(true);
|
||||
expect(model.input).toEqual(["text", "image"]);
|
||||
expect(model.contextWindow).toBe(KILOCODE_DEFAULT_CONTEXT_WINDOW);
|
||||
expect(model.maxTokens).toBe(KILOCODE_DEFAULT_MAX_TOKENS);
|
||||
expect(model.cost).toEqual(KILOCODE_DEFAULT_COST);
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyKilocodeProviderConfig", () => {
|
||||
it("registers kilocode provider with correct baseUrl and api", () => {
|
||||
const result = applyKilocodeProviderConfig(emptyCfg);
|
||||
const provider = result.models?.providers?.kilocode;
|
||||
expect(provider).toBeDefined();
|
||||
expect(provider?.baseUrl).toBe(KILOCODE_BASE_URL);
|
||||
expect(provider?.api).toBe("openai-completions");
|
||||
});
|
||||
|
||||
it("includes the default model in the provider model list", () => {
|
||||
const result = applyKilocodeProviderConfig(emptyCfg);
|
||||
const provider = result.models?.providers?.kilocode;
|
||||
const models = provider?.models;
|
||||
expect(Array.isArray(models)).toBe(true);
|
||||
const modelIds = models?.map((m) => m.id) ?? [];
|
||||
expect(modelIds).toContain(KILOCODE_DEFAULT_MODEL_ID);
|
||||
});
|
||||
|
||||
it("surfaces the full Kilo model catalog", () => {
|
||||
const result = applyKilocodeProviderConfig(emptyCfg);
|
||||
const provider = result.models?.providers?.kilocode;
|
||||
const modelIds = provider?.models?.map((m) => m.id) ?? [];
|
||||
for (const modelId of KILOCODE_MODEL_IDS) {
|
||||
expect(modelIds).toContain(modelId);
|
||||
}
|
||||
});
|
||||
|
||||
it("appends missing catalog models to existing Kilo provider config", () => {
|
||||
const result = applyKilocodeProviderConfig({
|
||||
models: {
|
||||
providers: {
|
||||
kilocode: {
|
||||
baseUrl: KILOCODE_BASE_URL,
|
||||
api: "openai-completions",
|
||||
models: [buildKilocodeModelDefinition()],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const modelIds = result.models?.providers?.kilocode?.models?.map((m) => m.id) ?? [];
|
||||
for (const modelId of KILOCODE_MODEL_IDS) {
|
||||
expect(modelIds).toContain(modelId);
|
||||
}
|
||||
});
|
||||
|
||||
it("sets Kilo Gateway alias in agent default models", () => {
|
||||
const result = applyKilocodeProviderConfig(emptyCfg);
|
||||
const agentModel = result.agents?.defaults?.models?.[KILOCODE_DEFAULT_MODEL_REF];
|
||||
expect(agentModel).toBeDefined();
|
||||
expect(agentModel?.alias).toBe("Kilo Gateway");
|
||||
});
|
||||
|
||||
it("preserves existing alias if already set", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
models: {
|
||||
[KILOCODE_DEFAULT_MODEL_REF]: { alias: "My Custom Alias" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = applyKilocodeProviderConfig(cfg);
|
||||
const agentModel = result.agents?.defaults?.models?.[KILOCODE_DEFAULT_MODEL_REF];
|
||||
expect(agentModel?.alias).toBe("My Custom Alias");
|
||||
});
|
||||
|
||||
it("does not change the default model selection", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
model: { primary: "openai/gpt-5" },
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = applyKilocodeProviderConfig(cfg);
|
||||
expect(resolveAgentModelPrimaryValue(result.agents?.defaults?.model)).toBe("openai/gpt-5");
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyKilocodeConfig", () => {
|
||||
it("sets kilocode as the default model", () => {
|
||||
const result = applyKilocodeConfig(emptyCfg);
|
||||
expect(resolveAgentModelPrimaryValue(result.agents?.defaults?.model)).toBe(
|
||||
KILOCODE_DEFAULT_MODEL_REF,
|
||||
);
|
||||
const provider = result.models?.providers?.kilocode;
|
||||
expect(provider).toBeDefined();
|
||||
expect(provider?.baseUrl).toBe(KILOCODE_BASE_URL);
|
||||
});
|
||||
});
|
||||
|
||||
describe("env var resolution", () => {
|
||||
it("resolves KILOCODE_API_KEY from env", () => {
|
||||
const envSnapshot = captureEnv(["KILOCODE_API_KEY"]);
|
||||
process.env.KILOCODE_API_KEY = "test-kilo-key";
|
||||
|
||||
try {
|
||||
const result = resolveEnvApiKey("kilocode");
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.apiKey).toBe("test-kilo-key");
|
||||
expect(result?.source).toContain("KILOCODE_API_KEY");
|
||||
} finally {
|
||||
envSnapshot.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns null when KILOCODE_API_KEY is not set", () => {
|
||||
const envSnapshot = captureEnv(["KILOCODE_API_KEY"]);
|
||||
delete process.env.KILOCODE_API_KEY;
|
||||
|
||||
try {
|
||||
const result = resolveEnvApiKey("kilocode");
|
||||
expect(result).toBeNull();
|
||||
} finally {
|
||||
envSnapshot.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
27
openclaw/extensions/kilocode/onboard.ts
Normal file
27
openclaw/extensions/kilocode/onboard.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import {
|
||||
createModelCatalogPresetAppliers,
|
||||
type OpenClawConfig,
|
||||
} from "openclaw/plugin-sdk/provider-onboard";
|
||||
import { buildKilocodeProvider } from "./provider-catalog.js";
|
||||
import { KILOCODE_BASE_URL, KILOCODE_DEFAULT_MODEL_REF } from "./provider-models.js";
|
||||
|
||||
export { KILOCODE_BASE_URL, KILOCODE_DEFAULT_MODEL_REF };
|
||||
|
||||
const kilocodePresetAppliers = createModelCatalogPresetAppliers({
|
||||
primaryModelRef: KILOCODE_DEFAULT_MODEL_REF,
|
||||
resolveParams: (_cfg: OpenClawConfig) => ({
|
||||
providerId: "kilocode",
|
||||
api: "openai-completions",
|
||||
baseUrl: KILOCODE_BASE_URL,
|
||||
catalogModels: buildKilocodeProvider().models ?? [],
|
||||
aliases: [{ modelRef: KILOCODE_DEFAULT_MODEL_REF, alias: "Kilo Gateway" }],
|
||||
}),
|
||||
});
|
||||
|
||||
export function applyKilocodeProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
return kilocodePresetAppliers.applyProviderConfig(cfg);
|
||||
}
|
||||
|
||||
export function applyKilocodeConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
return kilocodePresetAppliers.applyConfig(cfg);
|
||||
}
|
||||
29
openclaw/extensions/kilocode/openclaw.plugin.json
Normal file
29
openclaw/extensions/kilocode/openclaw.plugin.json
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"id": "kilocode",
|
||||
"enabledByDefault": true,
|
||||
"providers": ["kilocode"],
|
||||
"providerAuthEnvVars": {
|
||||
"kilocode": ["KILOCODE_API_KEY"]
|
||||
},
|
||||
"providerAuthChoices": [
|
||||
{
|
||||
"provider": "kilocode",
|
||||
"method": "api-key",
|
||||
"choiceId": "kilocode-api-key",
|
||||
"choiceLabel": "Kilo Gateway API key",
|
||||
"choiceHint": "API key (OpenRouter-compatible)",
|
||||
"groupId": "kilocode",
|
||||
"groupLabel": "Kilo Gateway",
|
||||
"groupHint": "API key (OpenRouter-compatible)",
|
||||
"optionKey": "kilocodeApiKey",
|
||||
"cliFlag": "--kilocode-api-key",
|
||||
"cliOption": "--kilocode-api-key <key>",
|
||||
"cliDescription": "Kilo Gateway API key"
|
||||
}
|
||||
],
|
||||
"configSchema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {}
|
||||
}
|
||||
}
|
||||
15
openclaw/extensions/kilocode/package.json
Normal file
15
openclaw/extensions/kilocode/package.json
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "@openclaw/kilocode-provider",
|
||||
"version": "2026.4.20",
|
||||
"private": true,
|
||||
"description": "OpenClaw Kilo Gateway provider plugin",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@openclaw/plugin-sdk": "workspace:*"
|
||||
},
|
||||
"openclaw": {
|
||||
"extensions": [
|
||||
"./index.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
34
openclaw/extensions/kilocode/provider-catalog.ts
Normal file
34
openclaw/extensions/kilocode/provider-catalog.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { type ModelProviderConfig } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
import {
|
||||
discoverKilocodeModels,
|
||||
KILOCODE_BASE_URL as LOCAL_KILOCODE_BASE_URL,
|
||||
KILOCODE_DEFAULT_CONTEXT_WINDOW as LOCAL_KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
KILOCODE_DEFAULT_COST as LOCAL_KILOCODE_DEFAULT_COST,
|
||||
KILOCODE_DEFAULT_MAX_TOKENS as LOCAL_KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
KILOCODE_MODEL_CATALOG as LOCAL_KILOCODE_MODEL_CATALOG,
|
||||
} from "./provider-models.js";
|
||||
|
||||
export function buildKilocodeProvider(): ModelProviderConfig {
|
||||
return {
|
||||
baseUrl: LOCAL_KILOCODE_BASE_URL,
|
||||
api: "openai-completions",
|
||||
models: LOCAL_KILOCODE_MODEL_CATALOG.map((model) => ({
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
reasoning: model.reasoning,
|
||||
input: model.input,
|
||||
cost: LOCAL_KILOCODE_DEFAULT_COST,
|
||||
contextWindow: model.contextWindow ?? LOCAL_KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: model.maxTokens ?? LOCAL_KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export async function buildKilocodeProviderWithDiscovery(): Promise<ModelProviderConfig> {
|
||||
const models = await discoverKilocodeModels();
|
||||
return {
|
||||
baseUrl: LOCAL_KILOCODE_BASE_URL,
|
||||
api: "openai-completions",
|
||||
models,
|
||||
};
|
||||
}
|
||||
216
openclaw/extensions/kilocode/provider-models.test.ts
Normal file
216
openclaw/extensions/kilocode/provider-models.test.ts
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
import { describe, expect, it, vi } from "vitest";
|
||||
import { discoverKilocodeModels, KILOCODE_MODELS_URL } from "./provider-models.js";
|
||||
|
||||
function makeGatewayModel(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
id: "anthropic/claude-sonnet-4",
|
||||
name: "Anthropic: Claude Sonnet 4",
|
||||
created: 1700000000,
|
||||
description: "A model",
|
||||
context_length: 200000,
|
||||
architecture: {
|
||||
input_modalities: ["text", "image"],
|
||||
output_modalities: ["text"],
|
||||
tokenizer: "Claude",
|
||||
},
|
||||
top_provider: {
|
||||
is_moderated: false,
|
||||
max_completion_tokens: 8192,
|
||||
},
|
||||
pricing: {
|
||||
prompt: "0.000003",
|
||||
completion: "0.000015",
|
||||
input_cache_read: "0.0000003",
|
||||
input_cache_write: "0.00000375",
|
||||
},
|
||||
supported_parameters: ["max_tokens", "temperature", "tools", "reasoning"],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeAutoModel(overrides: Record<string, unknown> = {}) {
|
||||
return makeGatewayModel({
|
||||
id: "kilo/auto",
|
||||
name: "Kilo: Auto",
|
||||
context_length: 1000000,
|
||||
architecture: {
|
||||
input_modalities: ["text", "image"],
|
||||
output_modalities: ["text"],
|
||||
tokenizer: "Other",
|
||||
},
|
||||
top_provider: {
|
||||
is_moderated: false,
|
||||
max_completion_tokens: 128000,
|
||||
},
|
||||
pricing: {
|
||||
prompt: "0.000005",
|
||||
completion: "0.000025",
|
||||
},
|
||||
supported_parameters: ["max_tokens", "temperature", "tools", "reasoning", "include_reasoning"],
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
async function withFetchPathTest(
|
||||
mockFetch: ReturnType<typeof vi.fn>,
|
||||
runAssertions: () => Promise<void>,
|
||||
) {
|
||||
const origNodeEnv = process.env.NODE_ENV;
|
||||
const origVitest = process.env.VITEST;
|
||||
delete process.env.NODE_ENV;
|
||||
delete process.env.VITEST;
|
||||
|
||||
vi.stubGlobal("fetch", mockFetch);
|
||||
|
||||
try {
|
||||
await runAssertions();
|
||||
} finally {
|
||||
if (origNodeEnv === undefined) {
|
||||
delete process.env.NODE_ENV;
|
||||
} else {
|
||||
process.env.NODE_ENV = origNodeEnv;
|
||||
}
|
||||
if (origVitest === undefined) {
|
||||
delete process.env.VITEST;
|
||||
} else {
|
||||
process.env.VITEST = origVitest;
|
||||
}
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
}
|
||||
|
||||
describe("discoverKilocodeModels", () => {
|
||||
it("returns static catalog in test environment", async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
expect(models.length).toBeGreaterThan(0);
|
||||
expect(models.some((m) => m.id === "kilo/auto")).toBe(true);
|
||||
});
|
||||
|
||||
it("static catalog has correct defaults for kilo/auto", async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
const auto = models.find((m) => m.id === "kilo/auto");
|
||||
expect(auto).toBeDefined();
|
||||
expect(auto?.name).toBe("Kilo Auto");
|
||||
expect(auto?.reasoning).toBe(true);
|
||||
expect(auto?.input).toEqual(["text", "image"]);
|
||||
expect(auto?.contextWindow).toBe(1000000);
|
||||
expect(auto?.maxTokens).toBe(128000);
|
||||
expect(auto?.cost).toEqual({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("discoverKilocodeModels (fetch path)", () => {
|
||||
it("parses gateway models with correct pricing conversion", async () => {
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
data: [makeAutoModel(), makeGatewayModel()],
|
||||
}),
|
||||
});
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
KILOCODE_MODELS_URL,
|
||||
expect.objectContaining({
|
||||
headers: { Accept: "application/json" },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(models.length).toBe(2);
|
||||
|
||||
const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
|
||||
expect(sonnet).toBeDefined();
|
||||
expect(sonnet?.cost.input).toBeCloseTo(3.0);
|
||||
expect(sonnet?.cost.output).toBeCloseTo(15.0);
|
||||
expect(sonnet?.cost.cacheRead).toBeCloseTo(0.3);
|
||||
expect(sonnet?.cost.cacheWrite).toBeCloseTo(3.75);
|
||||
expect(sonnet?.input).toEqual(["text", "image"]);
|
||||
expect(sonnet?.reasoning).toBe(true);
|
||||
expect(sonnet?.contextWindow).toBe(200000);
|
||||
expect(sonnet?.maxTokens).toBe(8192);
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to static catalog on network error", async () => {
|
||||
const mockFetch = vi.fn().mockRejectedValue(new Error("network error"));
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
expect(models.length).toBeGreaterThan(0);
|
||||
expect(models.some((m) => m.id === "kilo/auto")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to static catalog on HTTP error", async () => {
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
});
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
expect(models.length).toBeGreaterThan(0);
|
||||
expect(models.some((m) => m.id === "kilo/auto")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("ensures kilo/auto is present even when API doesn't return it", async () => {
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
data: [makeGatewayModel()],
|
||||
}),
|
||||
});
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
expect(models.some((m) => m.id === "kilo/auto")).toBe(true);
|
||||
expect(models.some((m) => m.id === "anthropic/claude-sonnet-4")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("detects text-only models without image modality", async () => {
|
||||
const textOnlyModel = makeGatewayModel({
|
||||
id: "some/text-model",
|
||||
architecture: {
|
||||
input_modalities: ["text"],
|
||||
output_modalities: ["text"],
|
||||
},
|
||||
supported_parameters: ["max_tokens", "temperature"],
|
||||
});
|
||||
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ data: [textOnlyModel] }),
|
||||
});
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
const textModel = models.find((m) => m.id === "some/text-model");
|
||||
expect(textModel?.input).toEqual(["text"]);
|
||||
expect(textModel?.reasoning).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps a later valid duplicate when an earlier entry is malformed", async () => {
|
||||
const malformedAutoModel = makeAutoModel({
|
||||
name: "Broken Kilo Auto",
|
||||
pricing: undefined,
|
||||
});
|
||||
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
data: [malformedAutoModel, makeAutoModel(), makeGatewayModel()],
|
||||
}),
|
||||
});
|
||||
await withFetchPathTest(mockFetch, async () => {
|
||||
const models = await discoverKilocodeModels();
|
||||
const auto = models.find((m) => m.id === "kilo/auto");
|
||||
expect(auto).toBeDefined();
|
||||
expect(auto?.name).toBe("Kilo: Auto");
|
||||
expect(auto?.cost.input).toBeCloseTo(5.0);
|
||||
expect(models.some((m) => m.id === "anthropic/claude-sonnet-4")).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
189
openclaw/extensions/kilocode/provider-models.ts
Normal file
189
openclaw/extensions/kilocode/provider-models.ts
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
import type { KilocodeModelCatalogEntry } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
|
||||
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
|
||||
|
||||
const log = createSubsystemLogger("kilocode-models");
|
||||
|
||||
export const KILOCODE_BASE_URL = "https://api.kilo.ai/api/gateway/";
|
||||
export const KILOCODE_DEFAULT_MODEL_ID = "kilo/auto";
|
||||
export const KILOCODE_DEFAULT_MODEL_REF = `kilocode/${KILOCODE_DEFAULT_MODEL_ID}`;
|
||||
export const KILOCODE_DEFAULT_MODEL_NAME = "Kilo Auto";
|
||||
|
||||
export const KILOCODE_MODEL_CATALOG: KilocodeModelCatalogEntry[] = [
|
||||
{
|
||||
id: KILOCODE_DEFAULT_MODEL_ID,
|
||||
name: KILOCODE_DEFAULT_MODEL_NAME,
|
||||
input: ["text", "image"],
|
||||
reasoning: true,
|
||||
},
|
||||
];
|
||||
|
||||
export const KILOCODE_DEFAULT_CONTEXT_WINDOW = 1000000;
|
||||
export const KILOCODE_DEFAULT_MAX_TOKENS = 128000;
|
||||
export const KILOCODE_DEFAULT_COST = {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
};
|
||||
|
||||
export const KILOCODE_MODELS_URL = `${KILOCODE_BASE_URL}models`;
|
||||
|
||||
const DISCOVERY_TIMEOUT_MS = 5000;
|
||||
|
||||
interface GatewayModelPricing {
|
||||
prompt: string;
|
||||
completion: string;
|
||||
image?: string;
|
||||
request?: string;
|
||||
input_cache_read?: string;
|
||||
input_cache_write?: string;
|
||||
web_search?: string;
|
||||
internal_reasoning?: string;
|
||||
}
|
||||
|
||||
interface GatewayModelEntry {
|
||||
id: string;
|
||||
name: string;
|
||||
context_length: number;
|
||||
architecture?: {
|
||||
input_modalities?: string[];
|
||||
output_modalities?: string[];
|
||||
};
|
||||
top_provider?: {
|
||||
max_completion_tokens?: number | null;
|
||||
};
|
||||
pricing: GatewayModelPricing;
|
||||
supported_parameters?: string[];
|
||||
}
|
||||
|
||||
interface GatewayModelsResponse {
|
||||
data: GatewayModelEntry[];
|
||||
}
|
||||
|
||||
function toPricePerMillion(perToken: string | undefined): number {
|
||||
if (!perToken) {
|
||||
return 0;
|
||||
}
|
||||
const num = Number(perToken);
|
||||
if (!Number.isFinite(num) || num < 0) {
|
||||
return 0;
|
||||
}
|
||||
return num * 1_000_000;
|
||||
}
|
||||
|
||||
function parseModality(entry: GatewayModelEntry): Array<"text" | "image"> {
|
||||
const modalities = entry.architecture?.input_modalities;
|
||||
if (!Array.isArray(modalities)) {
|
||||
return ["text"];
|
||||
}
|
||||
const hasImage = modalities.some(
|
||||
(m) => typeof m === "string" && normalizeLowercaseStringOrEmpty(m) === "image",
|
||||
);
|
||||
return hasImage ? ["text", "image"] : ["text"];
|
||||
}
|
||||
|
||||
function parseReasoning(entry: GatewayModelEntry): boolean {
|
||||
const params = entry.supported_parameters;
|
||||
if (!Array.isArray(params)) {
|
||||
return false;
|
||||
}
|
||||
return params.includes("reasoning") || params.includes("include_reasoning");
|
||||
}
|
||||
|
||||
function toModelDefinition(entry: GatewayModelEntry): ModelDefinitionConfig {
|
||||
return {
|
||||
id: entry.id,
|
||||
name: entry.name || entry.id,
|
||||
reasoning: parseReasoning(entry),
|
||||
input: parseModality(entry),
|
||||
cost: {
|
||||
input: toPricePerMillion(entry.pricing.prompt),
|
||||
output: toPricePerMillion(entry.pricing.completion),
|
||||
cacheRead: toPricePerMillion(entry.pricing.input_cache_read),
|
||||
cacheWrite: toPricePerMillion(entry.pricing.input_cache_write),
|
||||
},
|
||||
contextWindow: entry.context_length || KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: entry.top_provider?.max_completion_tokens ?? KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
};
|
||||
}
|
||||
|
||||
function buildStaticCatalog(): ModelDefinitionConfig[] {
|
||||
return KILOCODE_MODEL_CATALOG.map((model) => ({
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
reasoning: model.reasoning,
|
||||
input: model.input,
|
||||
cost: KILOCODE_DEFAULT_COST,
|
||||
contextWindow: model.contextWindow ?? KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: model.maxTokens ?? KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function discoverKilocodeModels(): Promise<ModelDefinitionConfig[]> {
|
||||
if (process.env.NODE_ENV === "test" || process.env.VITEST) {
|
||||
return buildStaticCatalog();
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(KILOCODE_MODELS_URL, {
|
||||
headers: { Accept: "application/json" },
|
||||
signal: AbortSignal.timeout(DISCOVERY_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
log.warn(`Failed to discover models: HTTP ${response.status}, using static catalog`);
|
||||
return buildStaticCatalog();
|
||||
}
|
||||
|
||||
const data = (await response.json()) as GatewayModelsResponse;
|
||||
if (!Array.isArray(data.data) || data.data.length === 0) {
|
||||
log.warn("No models found from gateway API, using static catalog");
|
||||
return buildStaticCatalog();
|
||||
}
|
||||
|
||||
const models: ModelDefinitionConfig[] = [];
|
||||
const discoveredIds = new Set<string>();
|
||||
|
||||
for (const entry of data.data) {
|
||||
if (!entry || typeof entry !== "object") {
|
||||
continue;
|
||||
}
|
||||
const id = typeof entry.id === "string" ? entry.id.trim() : "";
|
||||
if (!id || discoveredIds.has(id)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
models.push(toModelDefinition(entry));
|
||||
discoveredIds.add(id);
|
||||
} catch (e) {
|
||||
log.warn(`Skipping malformed model entry "${id}": ${String(e)}`);
|
||||
}
|
||||
}
|
||||
|
||||
const staticModels = buildStaticCatalog();
|
||||
for (const staticModel of staticModels) {
|
||||
if (!discoveredIds.has(staticModel.id)) {
|
||||
models.unshift(staticModel);
|
||||
}
|
||||
}
|
||||
|
||||
return models.length > 0 ? models : buildStaticCatalog();
|
||||
} catch (error) {
|
||||
log.warn(`Discovery failed: ${String(error)}, using static catalog`);
|
||||
return buildStaticCatalog();
|
||||
}
|
||||
}
|
||||
|
||||
export function buildKilocodeModelDefinition(): ModelDefinitionConfig {
|
||||
return {
|
||||
id: KILOCODE_DEFAULT_MODEL_ID,
|
||||
name: KILOCODE_DEFAULT_MODEL_NAME,
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: KILOCODE_DEFAULT_COST,
|
||||
contextWindow: KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
};
|
||||
}
|
||||
12
openclaw/extensions/kilocode/shared.ts
Normal file
12
openclaw/extensions/kilocode/shared.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
export {
|
||||
KILOCODE_BASE_URL,
|
||||
KILOCODE_DEFAULT_CONTEXT_WINDOW,
|
||||
KILOCODE_DEFAULT_COST,
|
||||
KILOCODE_DEFAULT_MAX_TOKENS,
|
||||
KILOCODE_DEFAULT_MODEL_ID,
|
||||
KILOCODE_DEFAULT_MODEL_NAME,
|
||||
KILOCODE_DEFAULT_MODEL_REF,
|
||||
KILOCODE_MODEL_CATALOG,
|
||||
} from "./provider-models.js";
|
||||
|
||||
export type { KilocodeModelCatalogEntry } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
16
openclaw/extensions/kilocode/tsconfig.json
Normal file
16
openclaw/extensions/kilocode/tsconfig.json
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"extends": "../tsconfig.package-boundary.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "."
|
||||
},
|
||||
"include": ["./*.ts", "./src/**/*.ts"],
|
||||
"exclude": [
|
||||
"./**/*.test.ts",
|
||||
"./dist/**",
|
||||
"./node_modules/**",
|
||||
"./src/test-support/**",
|
||||
"./src/**/*test-helpers.ts",
|
||||
"./src/**/*test-harness.ts",
|
||||
"./src/**/*test-support.ts"
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue