
























@@ -1,3 +1,4 @@
1+/** Shared helpers for model commands that read or mutate model config. */
12import { listAgentIds } from "../../agents/agent-scope.js";
23import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../../agents/defaults.js";
34import {
@@ -28,6 +29,7 @@ export const ensureFlagCompatibility = (opts: { json?: boolean; plain?: boolean
2829}
2930};
303132+/** Formats token counts as compact K-suffixed labels. */
3133export const formatTokenK = (value?: number | null) => {
3234if (!value || !Number.isFinite(value)) {
3335return "-";
@@ -38,6 +40,7 @@ export const formatTokenK = (value?: number | null) => {
3840return `${Math.round(value / 1024)}k`;
3941};
404243+/** Formats millisecond durations for model command output. */
4144export const formatMs = (value?: number | null) => {
4245if (value === null || value === undefined) {
4346return "-";
@@ -51,6 +54,7 @@ export const formatMs = (value?: number | null) => {
5154return `${Math.round(value / 100) / 10}s`;
5255};
535657+/** Loads config from disk and throws a formatted error when validation fails. */
5458export async function loadValidConfigOrThrow(): Promise<OpenClawConfig> {
5559const snapshot = await readConfigFileSnapshot();
5660if (!snapshot.valid) {
@@ -60,10 +64,12 @@ export async function loadValidConfigOrThrow(): Promise<OpenClawConfig> {
6064return snapshot.runtimeConfig ?? snapshot.config;
6165}
626667+/** Runtime config snapshot supplied to model config mutators. */
6368export type UpdateConfigContext = {
6469runtimeConfig: OpenClawConfig;
6570};
667172+/** Reads source config, applies a mutator, and writes only the source-form config. */
6773export async function updateConfig(
6874mutator: (cfg: OpenClawConfig, context: UpdateConfigContext) => OpenClawConfig,
6975): Promise<OpenClawConfig> {
@@ -74,6 +80,8 @@ export async function updateConfig(
7480}
7581const sourceConfig = structuredClone(snapshot.sourceConfig ?? snapshot.config);
7682const runtimeConfig = structuredClone(snapshot.runtimeConfig ?? snapshot.config);
83+// Mutate source config so SecretRefs and unresolved placeholders do not get
84+// overwritten by runtime-resolved secret values.
7785const next = mutator(sourceConfig, { runtimeConfig });
7886await replaceConfigFile({
7987nextConfig: next,
@@ -82,6 +90,7 @@ export async function updateConfig(
8290return next;
8391}
849293+/** Resolves a CLI model reference through aliases and catalog provider aliases. */
8594export function resolveModelTarget(params: { raw: string; cfg: OpenClawConfig }): {
8695provider: string;
8796model: string;
@@ -117,6 +126,7 @@ function resolveAuthoredModelAliasTarget(params: {
117126return resolved?.alias ? resolved.ref : undefined;
118127}
119128129+/** Resolves model reference strings to canonical provider/model keys. */
120130export function resolveModelKeysFromEntries(params: {
121131cfg: OpenClawConfig;
122132entries: readonly string[];
@@ -137,6 +147,7 @@ export function resolveModelKeysFromEntries(params: {
137147.map((entry) => modelKey(entry.ref.provider, entry.ref.model));
138148}
139149150+/** Builds the configured model allowlist from agents.defaults.models keys. */
140151export function buildAllowlistSet(cfg: OpenClawConfig): Set<string> {
141152const allowed = new Set<string>();
142153const models = cfg.agents?.defaults?.models ?? {};
@@ -150,6 +161,7 @@ export function buildAllowlistSet(cfg: OpenClawConfig): Set<string> {
150161return allowed;
151162}
152163164+/** Validates an optional agent id against configured agents. */
153165export function resolveKnownAgentId(params: {
154166cfg: OpenClawConfig;
155167rawAgentId?: string | null;
@@ -168,8 +180,10 @@ export function resolveKnownAgentId(params: {
168180return agentId;
169181}
170182183+/** Normalized primary/fallback config shape used by text and image defaults. */
171184export type PrimaryFallbackConfig = { primary?: string; fallbacks?: string[] };
172185186+/** Upserts the canonical model entry and folds legacy key metadata into it. */
173187export function upsertCanonicalModelConfigEntry(
174188models: Record<string, AgentModelEntryConfig>,
175189params: { provider: string; model: string },
@@ -196,6 +210,7 @@ export function upsertCanonicalModelConfigEntry(
196210}
197211198212if (legacyEntry) {
213+// Preserve legacy per-model params while moving the entry to provider/model.
199214models[key] = {
200215 ...legacyEntry,
201216 ...models[key],
@@ -213,6 +228,7 @@ export function upsertCanonicalModelConfigEntry(
213228return key;
214229}
215230231+/** Merges primary/fallback patches while normalizing refs for config storage. */
216232export function mergePrimaryFallbackConfig(
217233existing: PrimaryFallbackConfig | undefined,
218234patch: { primary?: string; fallbacks?: string[] },
@@ -230,6 +246,7 @@ export function mergePrimaryFallbackConfig(
230246return next;
231247}
232248249+/** Applies a default text/image primary-model update and ensures the model entry exists. */
233250export function applyDefaultModelPrimaryUpdate(params: {
234251cfg: OpenClawConfig;
235252resolveCfg?: OpenClawConfig;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。