mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 20:20:42 +00:00
perf: speed up reset model tests
This commit is contained in:
@@ -32,7 +32,7 @@ const FUZZY_VARIANT_TOKENS = [
|
|||||||
"nano",
|
"nano",
|
||||||
];
|
];
|
||||||
|
|
||||||
function modelKey(provider: string, model: string): string {
|
export function modelKey(provider: string, model: string): string {
|
||||||
const providerId = provider.trim();
|
const providerId = provider.trim();
|
||||||
const modelId = model.trim();
|
const modelId = model.trim();
|
||||||
if (!providerId) {
|
if (!providerId) {
|
||||||
@@ -48,7 +48,7 @@ function modelKey(provider: string, model: string): string {
|
|||||||
: `${providerId}/${modelId}`;
|
: `${providerId}/${modelId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveModelRefFromDirectiveString(params: {
|
export function resolveModelRefFromDirectiveString(params: {
|
||||||
raw: string;
|
raw: string;
|
||||||
defaultProvider: string;
|
defaultProvider: string;
|
||||||
aliasIndex: ModelAliasIndex;
|
aliasIndex: ModelAliasIndex;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import type { ModelCatalogEntry } from "../../agents/model-catalog.js";
|
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 { OpenClawConfig } from "../../config/config.js";
|
||||||
import type { SessionEntry } from "../../config/sessions.js";
|
import type { SessionEntry } from "../../config/sessions.js";
|
||||||
|
import type { ModelAliasIndex } from "./model-selection-directive.js";
|
||||||
import { applyResetModelOverride } from "./session-reset-model.js";
|
import { applyResetModelOverride } from "./session-reset-model.js";
|
||||||
|
|
||||||
const modelCatalog: ModelCatalogEntry[] = [
|
const modelCatalog: ModelCatalogEntry[] = [
|
||||||
@@ -12,7 +12,7 @@ const modelCatalog: ModelCatalogEntry[] = [
|
|||||||
|
|
||||||
function createResetFixture(entry: Partial<SessionEntry> = {}) {
|
function createResetFixture(entry: Partial<SessionEntry> = {}) {
|
||||||
const cfg = {} as OpenClawConfig;
|
const cfg = {} as OpenClawConfig;
|
||||||
const aliasIndex = buildModelAliasIndex({ cfg, defaultProvider: "openai" });
|
const aliasIndex: ModelAliasIndex = { byAlias: new Map(), byKey: new Map() };
|
||||||
const sessionEntry: SessionEntry = {
|
const sessionEntry: SessionEntry = {
|
||||||
sessionId: "s1",
|
sessionId: "s1",
|
||||||
updatedAt: Date.now(),
|
updatedAt: Date.now(),
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
import type { ModelCatalogEntry } from "../../agents/model-catalog.types.js";
|
import type { ModelCatalogEntry } from "../../agents/model-catalog.types.js";
|
||||||
import { modelKey, normalizeProviderId } from "../../agents/model-selection-normalize.js";
|
import { normalizeProviderId } from "../../agents/provider-id.js";
|
||||||
import {
|
|
||||||
buildAllowedModelSetWithFallbacks,
|
|
||||||
resolveModelRefFromString,
|
|
||||||
type ModelAliasIndex,
|
|
||||||
} from "../../agents/model-selection-shared.js";
|
|
||||||
import { resolveAgentModelFallbackValues } from "../../config/model-input.js";
|
import { resolveAgentModelFallbackValues } from "../../config/model-input.js";
|
||||||
import type { SessionEntry } from "../../config/sessions.js";
|
import type { SessionEntry } from "../../config/sessions.js";
|
||||||
import type { OpenClawConfig } from "../../config/types.openclaw.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 { normalizeOptionalString } from "../../shared/string-coerce.js";
|
||||||
import type { MsgContext, TemplateContext } from "../templating.js";
|
import type { MsgContext, TemplateContext } from "../templating.js";
|
||||||
import {
|
import {
|
||||||
|
modelKey,
|
||||||
resolveModelDirectiveSelection,
|
resolveModelDirectiveSelection,
|
||||||
|
resolveModelRefFromDirectiveString,
|
||||||
|
type ModelAliasIndex,
|
||||||
type ModelDirectiveSelection,
|
type ModelDirectiveSelection,
|
||||||
} from "./model-selection-directive.js";
|
} from "./model-selection-directive.js";
|
||||||
|
|
||||||
@@ -50,6 +48,31 @@ async function resolveResetFallbackModels(params: {
|
|||||||
return resolveAgentModelFallbackValues(params.cfg.agents?.defaults?.model);
|
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: {
|
function buildSelectionFromExplicit(params: {
|
||||||
raw: string;
|
raw: string;
|
||||||
defaultProvider: string;
|
defaultProvider: string;
|
||||||
@@ -57,7 +80,7 @@ function buildSelectionFromExplicit(params: {
|
|||||||
aliasIndex: ModelAliasIndex;
|
aliasIndex: ModelAliasIndex;
|
||||||
allowedModelKeys: Set<string>;
|
allowedModelKeys: Set<string>;
|
||||||
}): ModelDirectiveSelection | undefined {
|
}): ModelDirectiveSelection | undefined {
|
||||||
const resolved = resolveModelRefFromString({
|
const resolved = resolveModelRefFromDirectiveString({
|
||||||
raw: params.raw,
|
raw: params.raw,
|
||||||
defaultProvider: params.defaultProvider,
|
defaultProvider: params.defaultProvider,
|
||||||
aliasIndex: params.aliasIndex,
|
aliasIndex: params.aliasIndex,
|
||||||
@@ -141,7 +164,7 @@ export async function applyResetModelOverride(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const catalog = params.modelCatalog ?? (await loadResetModelCatalog(params.cfg));
|
const catalog = params.modelCatalog ?? (await loadResetModelCatalog(params.cfg));
|
||||||
const allowed = buildAllowedModelSetWithFallbacks({
|
const allowedModelKeys = await buildResetAllowedModelKeys({
|
||||||
cfg: params.cfg,
|
cfg: params.cfg,
|
||||||
catalog,
|
catalog,
|
||||||
defaultProvider: params.defaultProvider,
|
defaultProvider: params.defaultProvider,
|
||||||
@@ -151,7 +174,6 @@ export async function applyResetModelOverride(params: {
|
|||||||
agentId: params.agentId,
|
agentId: params.agentId,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
const allowedModelKeys = allowed.allowedKeys;
|
|
||||||
if (allowedModelKeys.size === 0) {
|
if (allowedModelKeys.size === 0) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user