Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
270 lines
8.6 KiB
TypeScript
270 lines
8.6 KiB
TypeScript
// Venice plugin module implements models behavior.
|
|
import {
|
|
getCachedLiveProviderModelRows,
|
|
LiveModelCatalogHttpError,
|
|
} from "openclaw/plugin-sdk/provider-catalog-live-runtime";
|
|
import { buildManifestModelProviderConfig } from "openclaw/plugin-sdk/provider-catalog-shared";
|
|
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
|
|
import { createSubsystemLogger, retryAsync } from "openclaw/plugin-sdk/runtime-env";
|
|
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import manifest from "./openclaw.plugin.json" with { type: "json" };
|
|
|
|
const log = createSubsystemLogger("venice-models");
|
|
|
|
const VENICE_MANIFEST_PROVIDER = buildManifestModelProviderConfig({
|
|
providerId: "venice",
|
|
catalog: manifest.modelCatalog.providers.venice,
|
|
});
|
|
|
|
export const VENICE_BASE_URL = VENICE_MANIFEST_PROVIDER.baseUrl;
|
|
const VENICE_DEFAULT_MODEL_ID = "kimi-k2-5";
|
|
export const VENICE_DEFAULT_MODEL_REF = `venice/${VENICE_DEFAULT_MODEL_ID}`;
|
|
const VENICE_ALLOWED_HOSTNAMES = ["api.venice.ai"];
|
|
|
|
const VENICE_DEFAULT_COST = {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
};
|
|
|
|
const VENICE_DEFAULT_CONTEXT_WINDOW = 128_000;
|
|
const VENICE_DEFAULT_MAX_TOKENS = 4096;
|
|
const VENICE_DISCOVERY_HARD_MAX_TOKENS = 131_072;
|
|
const VENICE_DISCOVERY_TIMEOUT_MS = 10_000;
|
|
const VENICE_DISCOVERY_CACHE_TTL_MS = 60_000;
|
|
const VENICE_DISCOVERY_RETRYABLE_HTTP_STATUS = new Set([408, 425, 429, 500, 502, 503, 504]);
|
|
const VENICE_DISCOVERY_RETRYABLE_NETWORK_CODES = new Set([
|
|
"ECONNABORTED",
|
|
"ECONNREFUSED",
|
|
"ECONNRESET",
|
|
"EAI_AGAIN",
|
|
"ENETDOWN",
|
|
"ENETUNREACH",
|
|
"ENOTFOUND",
|
|
"ETIMEDOUT",
|
|
"UND_ERR_BODY_TIMEOUT",
|
|
"UND_ERR_CONNECT_TIMEOUT",
|
|
"UND_ERR_CONNECT_ERROR",
|
|
"UND_ERR_HEADERS_TIMEOUT",
|
|
"UND_ERR_SOCKET",
|
|
]);
|
|
|
|
export const VENICE_MODEL_CATALOG: ModelDefinitionConfig[] = VENICE_MANIFEST_PROVIDER.models;
|
|
|
|
type VeniceCatalogEntry = ModelDefinitionConfig;
|
|
|
|
export function buildVeniceModelDefinition(entry: VeniceCatalogEntry): ModelDefinitionConfig {
|
|
return {
|
|
id: entry.id,
|
|
name: entry.name,
|
|
reasoning: entry.reasoning,
|
|
input: [...entry.input],
|
|
cost: VENICE_DEFAULT_COST,
|
|
contextWindow: entry.contextWindow,
|
|
maxTokens: entry.maxTokens,
|
|
compat: {
|
|
supportsUsageInStreaming: false,
|
|
...entry.compat,
|
|
},
|
|
};
|
|
}
|
|
|
|
interface VeniceModelSpec {
|
|
name: string;
|
|
privacy: "private" | "anonymized";
|
|
availableContextTokens?: number;
|
|
maxCompletionTokens?: number;
|
|
capabilities?: {
|
|
supportsReasoning?: boolean;
|
|
supportsVision?: boolean;
|
|
supportsFunctionCalling?: boolean;
|
|
};
|
|
}
|
|
|
|
interface VeniceModel {
|
|
id: string;
|
|
model_spec?: VeniceModelSpec;
|
|
}
|
|
|
|
function staticVeniceModelDefinitions(): ModelDefinitionConfig[] {
|
|
return VENICE_MODEL_CATALOG.map(buildVeniceModelDefinition);
|
|
}
|
|
|
|
function hasRetryableNetworkCode(err: unknown): boolean {
|
|
const queue: unknown[] = [err];
|
|
const seen = new Set<unknown>();
|
|
while (queue.length > 0) {
|
|
const current = queue.shift();
|
|
if (!current || typeof current !== "object" || seen.has(current)) {
|
|
continue;
|
|
}
|
|
seen.add(current);
|
|
const candidate = current as {
|
|
cause?: unknown;
|
|
errors?: unknown;
|
|
code?: unknown;
|
|
errno?: unknown;
|
|
};
|
|
const code =
|
|
typeof candidate.code === "string"
|
|
? candidate.code
|
|
: typeof candidate.errno === "string"
|
|
? candidate.errno
|
|
: undefined;
|
|
if (code && VENICE_DISCOVERY_RETRYABLE_NETWORK_CODES.has(code)) {
|
|
return true;
|
|
}
|
|
if (candidate.cause) {
|
|
queue.push(candidate.cause);
|
|
}
|
|
if (Array.isArray(candidate.errors)) {
|
|
queue.push(...candidate.errors);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isRetryableVeniceDiscoveryError(err: unknown): boolean {
|
|
if (err instanceof LiveModelCatalogHttpError) {
|
|
return VENICE_DISCOVERY_RETRYABLE_HTTP_STATUS.has(err.status);
|
|
}
|
|
if (err instanceof Error && err.name === "AbortError") {
|
|
return true;
|
|
}
|
|
if (err instanceof TypeError && normalizeLowercaseStringOrEmpty(err.message) === "fetch failed") {
|
|
return true;
|
|
}
|
|
return hasRetryableNetworkCode(err);
|
|
}
|
|
|
|
function normalizePositiveInt(value: unknown): number | undefined {
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
return undefined;
|
|
}
|
|
return Math.floor(value);
|
|
}
|
|
|
|
function resolveApiMaxCompletionTokens(params: {
|
|
apiModel: VeniceModel;
|
|
knownMaxTokens?: number;
|
|
}): number | undefined {
|
|
const raw = normalizePositiveInt(params.apiModel.model_spec?.maxCompletionTokens);
|
|
if (!raw) {
|
|
return undefined;
|
|
}
|
|
const contextWindow = normalizePositiveInt(params.apiModel.model_spec?.availableContextTokens);
|
|
const knownMaxTokens =
|
|
typeof params.knownMaxTokens === "number" && Number.isFinite(params.knownMaxTokens)
|
|
? Math.floor(params.knownMaxTokens)
|
|
: undefined;
|
|
const hardCap = knownMaxTokens ?? VENICE_DISCOVERY_HARD_MAX_TOKENS;
|
|
const fallbackContextWindow = knownMaxTokens ?? VENICE_DEFAULT_CONTEXT_WINDOW;
|
|
return Math.min(raw, contextWindow ?? fallbackContextWindow, hardCap);
|
|
}
|
|
|
|
function resolveApiSupportsTools(apiModel: VeniceModel): boolean | undefined {
|
|
const supportsFunctionCalling = apiModel.model_spec?.capabilities?.supportsFunctionCalling;
|
|
return typeof supportsFunctionCalling === "boolean" ? supportsFunctionCalling : undefined;
|
|
}
|
|
|
|
type VeniceModelDiscoveryOptions = {
|
|
retryDelayMs?: number;
|
|
};
|
|
|
|
export async function discoverVeniceModels(
|
|
options: VeniceModelDiscoveryOptions = {},
|
|
): Promise<ModelDefinitionConfig[]> {
|
|
if (process.env.NODE_ENV === "test" || process.env.VITEST) {
|
|
return staticVeniceModelDefinitions();
|
|
}
|
|
|
|
try {
|
|
const data = await retryAsync(
|
|
async () =>
|
|
await getCachedLiveProviderModelRows({
|
|
providerId: "venice",
|
|
endpoint: `${VENICE_BASE_URL}/models`,
|
|
timeoutMs: VENICE_DISCOVERY_TIMEOUT_MS,
|
|
ttlMs: VENICE_DISCOVERY_CACHE_TTL_MS,
|
|
policy: { allowedHostnames: VENICE_ALLOWED_HOSTNAMES },
|
|
auditContext: "venice-model-discovery",
|
|
}),
|
|
{
|
|
attempts: 3,
|
|
minDelayMs: options.retryDelayMs ?? 300,
|
|
maxDelayMs: options.retryDelayMs ?? 2000,
|
|
jitter: options.retryDelayMs === undefined ? 0.2 : 0,
|
|
label: "venice-model-discovery",
|
|
shouldRetry: isRetryableVeniceDiscoveryError,
|
|
},
|
|
);
|
|
|
|
if (data.length === 0) {
|
|
log.warn("No models found from API, using static catalog");
|
|
return staticVeniceModelDefinitions();
|
|
}
|
|
|
|
const catalogById = new Map<string, VeniceCatalogEntry>(
|
|
VENICE_MODEL_CATALOG.map((m) => [m.id, m]),
|
|
);
|
|
const models: ModelDefinitionConfig[] = [];
|
|
|
|
for (const apiModel of data as VeniceModel[]) {
|
|
const catalogEntry = catalogById.get(apiModel.id);
|
|
const apiMaxTokens = resolveApiMaxCompletionTokens({
|
|
apiModel,
|
|
knownMaxTokens: catalogEntry?.maxTokens,
|
|
});
|
|
const apiSupportsTools = resolveApiSupportsTools(apiModel);
|
|
if (catalogEntry) {
|
|
const definition = buildVeniceModelDefinition(catalogEntry);
|
|
if (apiMaxTokens !== undefined) {
|
|
definition.maxTokens = apiMaxTokens;
|
|
}
|
|
if (apiSupportsTools === false) {
|
|
definition.compat = {
|
|
...definition.compat,
|
|
supportsTools: false,
|
|
};
|
|
}
|
|
models.push(definition);
|
|
} else {
|
|
const apiSpec = apiModel.model_spec;
|
|
const lowerModelId = normalizeLowercaseStringOrEmpty(apiModel.id);
|
|
const isReasoning =
|
|
apiSpec?.capabilities?.supportsReasoning ||
|
|
lowerModelId.includes("thinking") ||
|
|
lowerModelId.includes("reason") ||
|
|
lowerModelId.includes("r1");
|
|
|
|
const hasVision = apiSpec?.capabilities?.supportsVision === true;
|
|
|
|
models.push({
|
|
id: apiModel.id,
|
|
name: apiSpec?.name || apiModel.id,
|
|
reasoning: isReasoning,
|
|
input: hasVision ? ["text", "image"] : ["text"],
|
|
cost: VENICE_DEFAULT_COST,
|
|
contextWindow:
|
|
normalizePositiveInt(apiSpec?.availableContextTokens) ?? VENICE_DEFAULT_CONTEXT_WINDOW,
|
|
maxTokens: apiMaxTokens ?? VENICE_DEFAULT_MAX_TOKENS,
|
|
compat: {
|
|
supportsUsageInStreaming: false,
|
|
...(apiSupportsTools === false ? { supportsTools: false } : {}),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
return models.length > 0 ? models : staticVeniceModelDefinitions();
|
|
} catch (error) {
|
|
if (error instanceof LiveModelCatalogHttpError) {
|
|
log.warn(`Failed to discover models: HTTP ${error.status}, using static catalog`);
|
|
return staticVeniceModelDefinitions();
|
|
}
|
|
log.warn(`Discovery failed: ${String(error)}, using static catalog`);
|
|
return staticVeniceModelDefinitions();
|
|
}
|
|
}
|