perf: speed up reset model tests

This commit is contained in:
Peter Steinberger
2026-04-28 00:52:03 +01:00
parent da3cf1c1a8
commit 7aeb7c2a14
3 changed files with 35 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ const FUZZY_VARIANT_TOKENS = [
"nano",
];
function modelKey(provider: string, model: string): string {
export function modelKey(provider: string, model: string): string {
const providerId = provider.trim();
const modelId = model.trim();
if (!providerId) {
@@ -48,7 +48,7 @@ function modelKey(provider: string, model: string): string {
: `${providerId}/${modelId}`;
}
function resolveModelRefFromDirectiveString(params: {
export function resolveModelRefFromDirectiveString(params: {
raw: string;
defaultProvider: string;
aliasIndex: ModelAliasIndex;

View File

@@ -1,8 +1,8 @@
import { describe, expect, it } from "vitest";
import type { ModelCatalogEntry } from "../../agents/model-catalog.js";
import { buildModelAliasIndex } from "../../agents/model-selection-shared.js";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions.js";
import type { ModelAliasIndex } from "./model-selection-directive.js";
import { applyResetModelOverride } from "./session-reset-model.js";
const modelCatalog: ModelCatalogEntry[] = [
@@ -12,7 +12,7 @@ const modelCatalog: ModelCatalogEntry[] = [
function createResetFixture(entry: Partial<SessionEntry> = {}) {
const cfg = {} as OpenClawConfig;
const aliasIndex = buildModelAliasIndex({ cfg, defaultProvider: "openai" });
const aliasIndex: ModelAliasIndex = { byAlias: new Map(), byKey: new Map() };
const sessionEntry: SessionEntry = {
sessionId: "s1",
updatedAt: Date.now(),

View File

@@ -1,10 +1,5 @@
import type { ModelCatalogEntry } from "../../agents/model-catalog.types.js";
import { modelKey, normalizeProviderId } from "../../agents/model-selection-normalize.js";
import {
buildAllowedModelSetWithFallbacks,
resolveModelRefFromString,
type ModelAliasIndex,
} from "../../agents/model-selection-shared.js";
import { normalizeProviderId } from "../../agents/provider-id.js";
import { resolveAgentModelFallbackValues } from "../../config/model-input.js";
import type { SessionEntry } from "../../config/sessions.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -12,7 +7,10 @@ import { applyModelOverrideToSessionEntry } from "../../sessions/model-overrides
import { normalizeOptionalString } from "../../shared/string-coerce.js";
import type { MsgContext, TemplateContext } from "../templating.js";
import {
modelKey,
resolveModelDirectiveSelection,
resolveModelRefFromDirectiveString,
type ModelAliasIndex,
type ModelDirectiveSelection,
} from "./model-selection-directive.js";
@@ -50,6 +48,31 @@ async function resolveResetFallbackModels(params: {
return resolveAgentModelFallbackValues(params.cfg.agents?.defaults?.model);
}
async function buildResetAllowedModelKeys(params: {
cfg: OpenClawConfig;
catalog: ModelCatalogEntry[];
defaultProvider: string;
defaultModel?: string;
fallbackModels: readonly string[];
}): Promise<Set<string>> {
const rawAllowlist = Object.keys(params.cfg.agents?.defaults?.models ?? {});
if (rawAllowlist.length > 0 || params.cfg.models?.providers) {
const { buildAllowedModelSetWithFallbacks } =
await import("../../agents/model-selection-shared.js");
return buildAllowedModelSetWithFallbacks(params).allowedKeys;
}
const allowedKeys = new Set<string>();
for (const entry of params.catalog) {
allowedKeys.add(modelKey(entry.provider, entry.id));
}
const defaultModel = params.defaultModel?.trim();
if (defaultModel) {
allowedKeys.add(modelKey(normalizeProviderId(params.defaultProvider), defaultModel));
}
return allowedKeys;
}
function buildSelectionFromExplicit(params: {
raw: string;
defaultProvider: string;
@@ -57,7 +80,7 @@ function buildSelectionFromExplicit(params: {
aliasIndex: ModelAliasIndex;
allowedModelKeys: Set<string>;
}): ModelDirectiveSelection | undefined {
const resolved = resolveModelRefFromString({
const resolved = resolveModelRefFromDirectiveString({
raw: params.raw,
defaultProvider: params.defaultProvider,
aliasIndex: params.aliasIndex,
@@ -141,7 +164,7 @@ export async function applyResetModelOverride(params: {
}
const catalog = params.modelCatalog ?? (await loadResetModelCatalog(params.cfg));
const allowed = buildAllowedModelSetWithFallbacks({
const allowedModelKeys = await buildResetAllowedModelKeys({
cfg: params.cfg,
catalog,
defaultProvider: params.defaultProvider,
@@ -151,7 +174,6 @@ export async function applyResetModelOverride(params: {
agentId: params.agentId,
}),
});
const allowedModelKeys = allowed.allowedKeys;
if (allowedModelKeys.size === 0) {
return {};
}