305 lines
10 KiB
TypeScript
305 lines
10 KiB
TypeScript
import { Global } from "../global"
|
|
import { Log } from "../util/log"
|
|
import path from "path"
|
|
import z from "zod"
|
|
import { Installation } from "../installation"
|
|
import { Flag } from "../flag/flag"
|
|
import { lazy } from "@/util/lazy"
|
|
import { Config } from "../config/config" // kilocode_change
|
|
import { ModelCache } from "./model-cache" // kilocode_change
|
|
import { Auth } from "../auth" // kilocode_change
|
|
import { AI_SDK_PROVIDERS, KILO_OPENROUTER_BASE, PROMPTS } from "@kilocode/kilo-gateway" // kilocode_change
|
|
import { Filesystem } from "../util/filesystem"
|
|
import { Flock } from "@/util/flock"
|
|
import { Hash } from "@/util/hash"
|
|
|
|
// Try to import bundled snapshot (generated at build time)
|
|
// Falls back to undefined in dev mode when snapshot doesn't exist
|
|
/* @ts-ignore */
|
|
|
|
// kilocode_change start
|
|
const normalizeKiloBaseURL = (baseURL: string | undefined, orgId: string | undefined): string | undefined => {
|
|
if (!baseURL) return undefined
|
|
const trimmed = baseURL.replace(/\/+$/, "")
|
|
if (orgId) {
|
|
if (trimmed.includes("/api/organizations/")) return trimmed
|
|
if (trimmed.endsWith("/api")) return `${trimmed}/organizations/${orgId}`
|
|
return `${trimmed}/api/organizations/${orgId}`
|
|
}
|
|
if (trimmed.includes("/openrouter")) return trimmed
|
|
if (trimmed.endsWith("/api")) return `${trimmed}/openrouter`
|
|
return `${trimmed}/api/openrouter`
|
|
}
|
|
|
|
export const Prompt = z.enum(PROMPTS)
|
|
|
|
export const AiSdkProvider = z.enum(AI_SDK_PROVIDERS)
|
|
// kilocode_change end
|
|
|
|
export namespace ModelsDev {
|
|
const log = Log.create({ service: "models.dev" })
|
|
const source = url()
|
|
const filepath = path.join(
|
|
Global.Path.cache,
|
|
source === "https://models.dev" ? "models.json" : `models-${Hash.fast(source)}.json`,
|
|
)
|
|
const ttl = 5 * 60 * 1000
|
|
|
|
type JsonValue = string | number | boolean | null | { [key: string]: JsonValue } | JsonValue[]
|
|
|
|
const JsonValue: z.ZodType<JsonValue> = z.lazy(() =>
|
|
z.union([z.string(), z.number(), z.boolean(), z.null(), z.array(JsonValue), z.record(z.string(), JsonValue)]),
|
|
)
|
|
|
|
const Cost = z.object({
|
|
input: z.number(),
|
|
output: z.number(),
|
|
cache_read: z.number().optional(),
|
|
cache_write: z.number().optional(),
|
|
context_over_200k: z
|
|
.object({
|
|
input: z.number(),
|
|
output: z.number(),
|
|
cache_read: z.number().optional(),
|
|
cache_write: z.number().optional(),
|
|
})
|
|
.optional(),
|
|
})
|
|
|
|
export const Model = z.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
family: z.string().optional(),
|
|
release_date: z.string(),
|
|
attachment: z.boolean(),
|
|
reasoning: z.boolean(),
|
|
temperature: z.boolean(),
|
|
tool_call: z.boolean(),
|
|
interleaved: z
|
|
.union([
|
|
z.literal(true),
|
|
z
|
|
.object({
|
|
field: z.enum(["reasoning_content", "reasoning_details"]),
|
|
})
|
|
.strict(),
|
|
])
|
|
.optional(),
|
|
cost: Cost.optional(),
|
|
limit: z.object({
|
|
context: z.number(),
|
|
input: z.number().optional(),
|
|
output: z.number(),
|
|
}),
|
|
modalities: z
|
|
.object({
|
|
input: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
|
|
output: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
|
|
})
|
|
.optional(),
|
|
|
|
// kilocode_change start
|
|
recommendedIndex: z.number().optional(),
|
|
prompt: Prompt.optional().catch(undefined),
|
|
isFree: z.boolean().optional(),
|
|
ai_sdk_provider: AiSdkProvider.optional().catch(undefined),
|
|
// kilocode_change end
|
|
|
|
experimental: z
|
|
.object({
|
|
modes: z
|
|
.record(
|
|
z.string(),
|
|
z.object({
|
|
cost: Cost.optional(),
|
|
provider: z
|
|
.object({
|
|
body: z.record(z.string(), JsonValue).optional(),
|
|
headers: z.record(z.string(), z.string()).optional(),
|
|
})
|
|
.optional(),
|
|
}),
|
|
)
|
|
.optional(),
|
|
})
|
|
.optional(),
|
|
status: z.enum(["alpha", "beta", "deprecated"]).optional(),
|
|
provider: z.object({ npm: z.string().optional(), api: z.string().optional() }).optional(),
|
|
})
|
|
export type Model = z.infer<typeof Model>
|
|
|
|
export const Provider = z.object({
|
|
api: z.string().optional(),
|
|
name: z.string(),
|
|
env: z.array(z.string()),
|
|
id: z.string(),
|
|
npm: z.string().optional(),
|
|
models: z.record(z.string(), Model),
|
|
})
|
|
|
|
export type Provider = z.infer<typeof Provider>
|
|
|
|
function url() {
|
|
return Flag.KILO_MODELS_URL || "https://models.dev"
|
|
}
|
|
|
|
function fresh() {
|
|
return Date.now() - Number(Filesystem.stat(filepath)?.mtimeMs ?? 0) < ttl
|
|
}
|
|
|
|
function skip(force: boolean) {
|
|
return !force && fresh()
|
|
}
|
|
|
|
const fetchApi = async () => {
|
|
const result = await fetch(`${url()}/api.json`, {
|
|
headers: { "User-Agent": Installation.USER_AGENT },
|
|
signal: AbortSignal.timeout(10000),
|
|
})
|
|
return { ok: result.ok, text: await result.text() }
|
|
}
|
|
|
|
export const Data = lazy(async () => {
|
|
const result = await Filesystem.readJson(Flag.KILO_MODELS_PATH ?? filepath).catch(() => {})
|
|
if (result) return result
|
|
// @ts-ignore
|
|
const snapshot = await import("./models-snapshot.js")
|
|
.then((m) => m.snapshot as Record<string, unknown>)
|
|
.catch(() => undefined)
|
|
if (snapshot) return snapshot
|
|
if (Flag.KILO_DISABLE_MODELS_FETCH) return {}
|
|
return Flock.withLock(`models-dev:${filepath}`, async () => {
|
|
const result = await Filesystem.readJson(Flag.KILO_MODELS_PATH ?? filepath).catch(() => {})
|
|
if (result) return result
|
|
const result2 = await fetchApi()
|
|
if (result2.ok) {
|
|
await Filesystem.write(filepath, result2.text).catch((e) => {
|
|
log.error("Failed to write models cache", { error: e })
|
|
})
|
|
}
|
|
return JSON.parse(result2.text)
|
|
})
|
|
})
|
|
|
|
export async function get() {
|
|
const result = await Data()
|
|
// kilocode_change start
|
|
const providers = result as Record<string, Provider>
|
|
|
|
if (providers["kilo"]) {
|
|
delete providers["kilo"]
|
|
}
|
|
|
|
// Inject kilo provider with dynamic model fetching
|
|
// Skip injection entirely when enabled_providers is set and doesn't include "kilo",
|
|
// or when "kilo" is in disabled_providers. This prevents unnecessary network calls
|
|
// to the Kilo API for teams using only their own providers (e.g. LiteLLM).
|
|
const config = await Config.get()
|
|
const disabled = new Set(config.disabled_providers ?? [])
|
|
const enabled = config.enabled_providers ? new Set(config.enabled_providers) : null
|
|
const kiloAllowed = (!enabled || enabled.has("kilo")) && !disabled.has("kilo")
|
|
|
|
if (kiloAllowed && !providers["kilo"]) {
|
|
const kiloOptions = config.provider?.kilo?.options
|
|
// resolve org ID from auth (OAuth accountId) not just config
|
|
const kiloAuth = await Auth.get("kilo")
|
|
const kiloOrgId =
|
|
kiloOptions?.kilocodeOrganizationId ?? (kiloAuth?.type === "oauth" ? kiloAuth.accountId : undefined)
|
|
const normalizedBaseURL = normalizeKiloBaseURL(kiloOptions?.baseURL, kiloOrgId)
|
|
const kiloFetchOptions = {
|
|
...(normalizedBaseURL ? { baseURL: normalizedBaseURL } : {}),
|
|
...(kiloOrgId ? { kilocodeOrganizationId: kiloOrgId } : {}),
|
|
}
|
|
const defaultBaseURL = kiloOrgId
|
|
? `https://api.kilo.ai/api/organizations/${kiloOrgId}`
|
|
: "https://api.kilo.ai/api/openrouter"
|
|
const providerBaseURL = normalizedBaseURL ?? defaultBaseURL
|
|
const ensureTrailingSlash = (value: string): string => (value.endsWith("/") ? value : `${value}/`)
|
|
const apertisConfig = config.provider?.apertis?.options
|
|
const apertisBaseURL = apertisConfig?.baseURL ?? "https://api.apertis.ai/v1"
|
|
const apertisFetchOptions = {
|
|
...(apertisConfig?.baseURL ? { baseURL: apertisConfig.baseURL } : {}),
|
|
}
|
|
|
|
const [kiloModels, apertisModels] = await Promise.all([
|
|
ModelCache.fetch("kilo", kiloFetchOptions).catch(() => ({})),
|
|
!providers["apertis"]
|
|
? ModelCache.fetch("apertis", apertisFetchOptions).catch(() => ({}))
|
|
: Promise.resolve(null),
|
|
])
|
|
|
|
providers["kilo"] = {
|
|
id: "kilo",
|
|
name: "Kilo Gateway",
|
|
env: ["KILO_API_KEY"],
|
|
api: ensureTrailingSlash(KILO_OPENROUTER_BASE),
|
|
npm: "@kilocode/kilo-gateway",
|
|
models: kiloModels,
|
|
}
|
|
if (Object.keys(kiloModels).length === 0) {
|
|
ModelCache.refresh("kilo", kiloFetchOptions).catch(() => {})
|
|
}
|
|
|
|
if (!providers["apertis"] && apertisModels !== null) {
|
|
providers["apertis"] = {
|
|
id: "apertis",
|
|
name: "Apertis",
|
|
env: ["APERTIS_API_KEY"],
|
|
api: apertisBaseURL,
|
|
npm: "@ai-sdk/openai-compatible",
|
|
models: apertisModels,
|
|
}
|
|
if (Object.keys(apertisModels).length === 0) {
|
|
ModelCache.refresh("apertis", apertisFetchOptions).catch(() => {})
|
|
}
|
|
}
|
|
} else if (!providers["apertis"]) {
|
|
const apertisConfig = config.provider?.apertis?.options
|
|
const apertisBaseURL = apertisConfig?.baseURL ?? "https://api.apertis.ai/v1"
|
|
const apertisFetchOptions = {
|
|
...(apertisConfig?.baseURL ? { baseURL: apertisConfig.baseURL } : {}),
|
|
}
|
|
const apertisModels = await ModelCache.fetch("apertis", apertisFetchOptions).catch(() => ({}))
|
|
providers["apertis"] = {
|
|
id: "apertis",
|
|
name: "Apertis",
|
|
env: ["APERTIS_API_KEY"],
|
|
api: apertisBaseURL,
|
|
npm: "@ai-sdk/openai-compatible",
|
|
models: apertisModels,
|
|
}
|
|
if (Object.keys(apertisModels).length === 0) {
|
|
ModelCache.refresh("apertis", apertisFetchOptions).catch(() => {})
|
|
}
|
|
}
|
|
|
|
return providers
|
|
// kilocode_change end
|
|
}
|
|
|
|
export async function refresh(force = false) {
|
|
if (skip(force)) return ModelsDev.Data.reset()
|
|
await Flock.withLock(`models-dev:${filepath}`, async () => {
|
|
if (skip(force)) return ModelsDev.Data.reset()
|
|
const result = await fetchApi()
|
|
if (!result.ok) return
|
|
await Filesystem.write(filepath, result.text)
|
|
ModelsDev.Data.reset()
|
|
}).catch((e) => {
|
|
log.error("Failed to fetch models.dev", {
|
|
error: e,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
if (!Flag.KILO_DISABLE_MODELS_FETCH && !process.argv.includes("--get-yargs-completions")) {
|
|
ModelsDev.refresh()
|
|
setInterval(
|
|
async () => {
|
|
await ModelsDev.refresh()
|
|
},
|
|
60 * 1000 * 60,
|
|
).unref()
|
|
}
|