fix: normalize status auth cost handling and models header tests

This commit is contained in:
Peter Steinberger
2026-02-21 12:44:06 +01:00
parent e393d7aa5b
commit cb84c537f4
3 changed files with 48 additions and 17 deletions

View File

@@ -122,7 +122,7 @@ describe("directive behavior", () => {
},
},
});
expect(text).toContain("Models (minimax)");
expect(text).toContain("Models (minimax");
expect(text).toContain("minimax/MiniMax-M2.1");
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
});

View File

@@ -717,7 +717,7 @@ describe("/models command", () => {
const params = buildPolicyParams("/models anthropic", cfg, { Surface: "discord" });
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false);
expect(result.reply?.text).toContain("Models (anthropic)");
expect(result.reply?.text).toContain("Models (anthropic");
expect(result.reply?.text).toContain("page 1/");
expect(result.reply?.text).toContain("anthropic/claude-opus-4-5");
expect(result.reply?.text).toContain("Switch: /model <provider/model>");
@@ -729,7 +729,7 @@ describe("/models command", () => {
const params = buildPolicyParams("/models anthropic 3 all", cfg, { Surface: "discord" });
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false);
expect(result.reply?.text).toContain("Models (anthropic)");
expect(result.reply?.text).toContain("Models (anthropic");
expect(result.reply?.text).toContain("page 1/1");
expect(result.reply?.text).toContain("anthropic/claude-opus-4-5");
expect(result.reply?.text).not.toContain("Page out of range");
@@ -777,7 +777,7 @@ describe("/models command", () => {
buildPolicyParams("/models localai", customCfg, { Surface: "discord" }),
);
expect(result.shouldContinue).toBe(false);
expect(result.reply?.text).toContain("Models (localai)");
expect(result.reply?.text).toContain("Models (localai");
expect(result.reply?.text).toContain("localai/ultra-chat");
expect(result.reply?.text).not.toContain("Unknown provider");
});

View File

@@ -90,6 +90,34 @@ type StatusArgs = {
now?: number;
};
type NormalizedAuthMode = "api-key" | "oauth" | "token" | "aws-sdk" | "mixed" | "unknown";
function normalizeAuthMode(value?: string): NormalizedAuthMode | undefined {
const normalized = value?.trim().toLowerCase();
if (!normalized) {
return undefined;
}
if (normalized === "api-key" || normalized.startsWith("api-key ")) {
return "api-key";
}
if (normalized === "oauth" || normalized.startsWith("oauth ")) {
return "oauth";
}
if (normalized === "token" || normalized.startsWith("token ")) {
return "token";
}
if (normalized === "aws-sdk" || normalized.startsWith("aws-sdk ")) {
return "aws-sdk";
}
if (normalized === "mixed" || normalized.startsWith("mixed ")) {
return "mixed";
}
if (normalized === "unknown") {
return "unknown";
}
return undefined;
}
function resolveRuntimeLabel(
args: Pick<StatusArgs, "config" | "agent" | "sessionKey" | "sessionScope">,
): string {
@@ -498,17 +526,27 @@ export function buildStatusMessage(args: StatusArgs): string {
];
const activationLine = activationParts.filter(Boolean).join(" · ");
const activeAuthMode = resolveModelAuthMode(activeProvider, args.config);
const selectedAuthMode =
normalizeAuthMode(args.modelAuth) ?? resolveModelAuthMode(selectedProvider, args.config);
const selectedAuthLabelValue =
args.modelAuth ??
(() => {
const selectedAuthMode = resolveModelAuthMode(selectedProvider, args.config);
return selectedAuthMode && selectedAuthMode !== "unknown" ? selectedAuthMode : undefined;
})();
(selectedAuthMode && selectedAuthMode !== "unknown" ? selectedAuthMode : undefined);
const activeAuthMode =
normalizeAuthMode(args.activeModelAuth) ?? resolveModelAuthMode(activeProvider, args.config);
const activeAuthLabelValue =
args.activeModelAuth ??
(activeAuthMode && activeAuthMode !== "unknown" ? activeAuthMode : undefined);
const showCost = activeAuthLabelValue === "api-key" || activeAuthLabelValue === "mixed";
const selectedModelLabel = modelRefs.selected.label || "unknown";
const activeModelLabel = formatProviderModelRef(activeProvider, activeModel) || "unknown";
const fallbackState = resolveActiveFallbackState({
selectedModelRef: selectedModelLabel,
activeModelRef: activeModelLabel,
state: entry,
});
const effectiveCostAuthMode = fallbackState.active
? activeAuthMode
: (selectedAuthMode ?? activeAuthMode);
const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
const costConfig = showCost
? resolveModelCostConfig({
provider: activeProvider,
@@ -529,13 +567,6 @@ export function buildStatusMessage(args: StatusArgs): string {
: undefined;
const costLabel = showCost && hasUsage ? formatUsd(cost) : undefined;
const selectedModelLabel = modelRefs.selected.label || "unknown";
const activeModelLabel = formatProviderModelRef(activeProvider, activeModel) || "unknown";
const fallbackState = resolveActiveFallbackState({
selectedModelRef: selectedModelLabel,
activeModelRef: activeModelLabel,
state: entry,
});
const selectedAuthLabel = selectedAuthLabelValue ? ` · 🔑 ${selectedAuthLabelValue}` : "";
const channelModelNote = (() => {
if (!args.config || !entry) {