mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(model): share normalized provider map lookups
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import type { AuthProfileStore } from "./types.js";
|
||||
import { normalizeProviderId } from "../model-selection.js";
|
||||
import { findNormalizedProviderValue, normalizeProviderId } from "../model-selection.js";
|
||||
import { dedupeProfileIds, listProfilesForProvider } from "./profiles.js";
|
||||
import { clearExpiredCooldowns, isProfileInCooldown } from "./usage.js";
|
||||
|
||||
@@ -31,30 +31,8 @@ export function resolveAuthProfileOrder(params: {
|
||||
// get a fresh error count and are not immediately re-penalized on the
|
||||
// next transient failure. See #3604.
|
||||
clearExpiredCooldowns(store, now);
|
||||
const storedOrder = (() => {
|
||||
const order = store.order;
|
||||
if (!order) {
|
||||
return undefined;
|
||||
}
|
||||
for (const [key, value] of Object.entries(order)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
})();
|
||||
const configuredOrder = (() => {
|
||||
const order = cfg?.auth?.order;
|
||||
if (!order) {
|
||||
return undefined;
|
||||
}
|
||||
for (const [key, value] of Object.entries(order)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
})();
|
||||
const storedOrder = findNormalizedProviderValue(store.order, providerKey);
|
||||
const configuredOrder = findNormalizedProviderValue(cfg?.auth?.order, providerKey);
|
||||
const explicitOrder = storedOrder ?? configuredOrder;
|
||||
const explicitProfiles = cfg?.auth?.profiles
|
||||
? Object.entries(cfg.auth.profiles)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import type { AuthProfileConfig } from "../../config/types.js";
|
||||
import type { AuthProfileIdRepairResult, AuthProfileStore } from "./types.js";
|
||||
import { normalizeProviderId } from "../model-selection.js";
|
||||
import { findNormalizedProviderKey, normalizeProviderId } from "../model-selection.js";
|
||||
import { dedupeProfileIds, listProfilesForProvider } from "./profiles.js";
|
||||
|
||||
function getProfileSuffix(profileId: string): string {
|
||||
@@ -128,7 +128,7 @@ export function repairOAuthProfileIdMismatch(params: {
|
||||
if (!order) {
|
||||
return undefined;
|
||||
}
|
||||
const resolvedKey = Object.keys(order).find((key) => normalizeProviderId(key) === providerKey);
|
||||
const resolvedKey = findNormalizedProviderKey(order, providerKey);
|
||||
if (!resolvedKey) {
|
||||
return order;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,33 @@ export function normalizeProviderId(provider: string): string {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function findNormalizedProviderValue<T>(
|
||||
entries: Record<string, T> | undefined,
|
||||
provider: string,
|
||||
): T | undefined {
|
||||
if (!entries) {
|
||||
return undefined;
|
||||
}
|
||||
const providerKey = normalizeProviderId(provider);
|
||||
for (const [key, value] of Object.entries(entries)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function findNormalizedProviderKey(
|
||||
entries: Record<string, unknown> | undefined,
|
||||
provider: string,
|
||||
): string | undefined {
|
||||
if (!entries) {
|
||||
return undefined;
|
||||
}
|
||||
const providerKey = normalizeProviderId(provider);
|
||||
return Object.keys(entries).find((key) => normalizeProviderId(key) === providerKey);
|
||||
}
|
||||
|
||||
export function isCliProvider(provider: string, cfg?: OpenClawConfig): boolean {
|
||||
const normalized = normalizeProviderId(provider);
|
||||
if (normalized === "claude-cli") {
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
resolveAuthProfileOrder,
|
||||
resolveEnvApiKey,
|
||||
} from "../../agents/model-auth.js";
|
||||
import { normalizeProviderId } from "../../agents/model-selection.js";
|
||||
import { findNormalizedProviderValue, normalizeProviderId } from "../../agents/model-selection.js";
|
||||
import { shortenHomePath } from "../../utils.js";
|
||||
|
||||
export type ModelAuthDetailMode = "compact" | "verbose";
|
||||
@@ -39,18 +39,7 @@ export const resolveAuthLabel = async (
|
||||
});
|
||||
const order = resolveAuthProfileOrder({ cfg, store, provider });
|
||||
const providerKey = normalizeProviderId(provider);
|
||||
const lastGood = (() => {
|
||||
const map = store.lastGood;
|
||||
if (!map) {
|
||||
return undefined;
|
||||
}
|
||||
for (const [key, value] of Object.entries(map)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
})();
|
||||
const lastGood = findNormalizedProviderValue(store.lastGood, providerKey);
|
||||
const nextProfileId = order[0];
|
||||
const now = Date.now();
|
||||
|
||||
|
||||
@@ -12,7 +12,11 @@ import {
|
||||
import { describeFailoverError } from "../../agents/failover-error.js";
|
||||
import { getCustomProviderApiKey, resolveEnvApiKey } from "../../agents/model-auth.js";
|
||||
import { loadModelCatalog } from "../../agents/model-catalog.js";
|
||||
import { normalizeProviderId, parseModelRef } from "../../agents/model-selection.js";
|
||||
import {
|
||||
findNormalizedProviderValue,
|
||||
normalizeProviderId,
|
||||
parseModelRef,
|
||||
} from "../../agents/model-selection.js";
|
||||
import { runEmbeddedPiAgent } from "../../agents/pi-embedded.js";
|
||||
import { resolveDefaultAgentWorkspaceDir } from "../../agents/workspace.js";
|
||||
import {
|
||||
@@ -164,23 +168,10 @@ function buildProbeTargets(params: {
|
||||
|
||||
const profileIds = listProfilesForProvider(store, providerKey);
|
||||
const explicitOrder = (() => {
|
||||
const order = store.order;
|
||||
if (order) {
|
||||
for (const [key, value] of Object.entries(order)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
const cfgOrder = cfg?.auth?.order;
|
||||
if (cfgOrder) {
|
||||
for (const [key, value] of Object.entries(cfgOrder)) {
|
||||
if (normalizeProviderId(key) === providerKey) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
return (
|
||||
findNormalizedProviderValue(store.order, providerKey) ??
|
||||
findNormalizedProviderValue(cfg?.auth?.order, providerKey)
|
||||
);
|
||||
})();
|
||||
const allowedProfiles =
|
||||
explicitOrder && explicitOrder.length > 0
|
||||
|
||||
Reference in New Issue
Block a user