@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
102 lines • 5.16 kB
JavaScript
import { getSupportedThinkingLevels } from "../thinking-levels.js";
import { baseSelector, parseFallbackSelector } from "./chains.js";
import { matchesFamily, parseBareSelector } from "./expansion.js";
function isPlainObject(value) {
if (value === null || typeof value !== "object" || Array.isArray(value))
return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
function invalidSelectorWarning(kind, selector, key) {
const subject = kind === "key" ? `Fallback chain key "${selector}"` : `Fallback chain entry "${selector}" for "${key}"`;
return `${subject} is not a valid or known model selector.`;
}
function validateThinkingLevel(selector, model, subject, warnings) {
if (selector.thinkingLevel && !getSupportedThinkingLevels(model).includes(selector.thinkingLevel)) {
warnings.push(`${subject} uses thinking level "${selector.thinkingLevel}", which is unsupported by ${selector.provider}/${selector.id}.`);
}
}
/**
* A same-model entry is only a duplicate when it resolves to the same rung: the
* same model at the same level, an entry without one inheriting the key's. The
* same model at another level is a deliberate second rung (thinking on -> off)
* and stays valid.
*/
function isRungDuplicate(entry, key) {
if (baseSelector(entry) !== baseSelector(key))
return false;
return (entry.thinkingLevel ?? key.thinkingLevel) === key.thinkingLevel;
}
/**
* Names the offending value so a single log line identifies the producer. The
* bare form of this warning fired repeatedly in the wild with no way to tell
* which settings value caused it.
*/
function describeValue(value) {
if (value === null)
return "null";
if (Array.isArray(value))
return "an array";
return `a ${typeof value}`;
}
/** Returns configuration warnings without changing malformed fallback-chain settings. */
export function validateFallbackChains(chains, registry) {
if (chains === undefined)
return [];
if (!isPlainObject(chains))
return [`Fallback chains must be a plain object, but got ${describeValue(chains)}.`];
const warnings = [];
const models = registry.getAll();
for (const [key, entries] of Object.entries(chains)) {
const hasProvider = key.includes("/");
const hasWildcard = key.includes("*");
// A bare key names a model family and applies to every provider serving it.
const bareKey = hasWildcard ? undefined : parseBareSelector(key);
const bareKeyMatches = bareKey ? models.some((model) => matchesFamily(model, bareKey.family)) : false;
if (!hasProvider && !bareKeyMatches && !hasWildcard) {
// A bare key naming no registered model is indistinguishable from the old
// role syntax, so it keeps the message that tells the user what to write.
warnings.push(`Fallback chain key "${key}" must use a provider/model selector; roles are unsupported.`);
}
if (hasWildcard && key !== "*")
warnings.push(`Fallback chain key "${key}" cannot contain wildcards.`);
const parsedKey = hasProvider && !hasWildcard ? parseFallbackSelector(key, models) : undefined;
const keyModel = parsedKey ? registry.find(parsedKey.provider, parsedKey.id) : undefined;
if (hasProvider && !hasWildcard && (!parsedKey || !keyModel)) {
warnings.push(invalidSelectorWarning("key", key));
}
if (parsedKey && keyModel) {
validateThinkingLevel(parsedKey, keyModel, `Fallback chain key "${key}"`, warnings);
}
if (!Array.isArray(entries) || entries.some((entry) => typeof entry !== "string")) {
warnings.push(`Fallback chain "${key}" entries must be an array of strings.`);
continue;
}
// An empty array is the documented opt-out that removes a shipped default
// chain, so it is a valid instruction rather than a malformed entry.
if (entries.length === 0) {
continue;
}
for (const entry of entries) {
const bareEntry = parseBareSelector(entry);
if (bareEntry) {
if (!models.some((model) => matchesFamily(model, bareEntry.family))) {
warnings.push(invalidSelectorWarning("entry", entry, key));
}
continue;
}
const parsedEntry = parseFallbackSelector(entry, models);
const entryModel = parsedEntry ? registry.find(parsedEntry.provider, parsedEntry.id) : undefined;
if (!parsedEntry || !entryModel) {
warnings.push(invalidSelectorWarning("entry", entry, key));
continue;
}
if (parsedKey && isRungDuplicate(parsedEntry, parsedKey)) {
warnings.push(`Fallback chain entry "${entry}" for "${key}" cannot reference the same model.`);
}
validateThinkingLevel(parsedEntry, entryModel, `Fallback chain entry "${entry}"`, warnings);
}
}
return warnings;
}
//# sourceMappingURL=validate.js.map