fix: promote selected auth profile order

This commit is contained in:
Peter Steinberger
2026-01-09 05:36:14 +00:00
parent 8e35ad5484
commit 48a1b07097
2 changed files with 43 additions and 4 deletions

View File

@@ -5,7 +5,10 @@ import path from "node:path";
import type { OAuthCredentials } from "@mariozechner/pi-ai"; import type { OAuthCredentials } from "@mariozechner/pi-ai";
import { afterEach, describe, expect, it } from "vitest"; import { afterEach, describe, expect, it } from "vitest";
import { writeOAuthCredentials } from "./onboard-auth.js"; import {
applyAuthProfileConfig,
writeOAuthCredentials,
} from "./onboard-auth.js";
describe("writeOAuthCredentials", () => { describe("writeOAuthCredentials", () => {
const previousStateDir = process.env.CLAWDBOT_STATE_DIR; const previousStateDir = process.env.CLAWDBOT_STATE_DIR;
@@ -77,3 +80,28 @@ describe("writeOAuthCredentials", () => {
).rejects.toThrow(); ).rejects.toThrow();
}); });
}); });
describe("applyAuthProfileConfig", () => {
it("promotes the newly selected profile to the front of auth.order", () => {
const next = applyAuthProfileConfig(
{
auth: {
profiles: {
"anthropic:default": { provider: "anthropic", mode: "api_key" },
},
order: { anthropic: ["anthropic:default"] },
},
},
{
profileId: "anthropic:claude-cli",
provider: "anthropic",
mode: "oauth",
},
);
expect(next.auth?.order?.anthropic).toEqual([
"anthropic:claude-cli",
"anthropic:default",
]);
});
});

View File

@@ -53,6 +53,7 @@ export function applyAuthProfileConfig(
provider: string; provider: string;
mode: "api_key" | "oauth"; mode: "api_key" | "oauth";
email?: string; email?: string;
preferProfileFirst?: boolean;
}, },
): ClawdbotConfig { ): ClawdbotConfig {
const profiles = { const profiles = {
@@ -67,13 +68,23 @@ export function applyAuthProfileConfig(
// Only maintain `auth.order` when the user explicitly configured it. // Only maintain `auth.order` when the user explicitly configured it.
// Default behavior: no explicit order -> resolveAuthProfileOrder can round-robin by lastUsed. // Default behavior: no explicit order -> resolveAuthProfileOrder can round-robin by lastUsed.
const existingProviderOrder = cfg.auth?.order?.[params.provider]; const existingProviderOrder = cfg.auth?.order?.[params.provider];
const preferProfileFirst = params.preferProfileFirst ?? true;
const reorderedProviderOrder =
existingProviderOrder && preferProfileFirst
? [
params.profileId,
...existingProviderOrder.filter(
(profileId) => profileId !== params.profileId,
),
]
: existingProviderOrder;
const order = const order =
existingProviderOrder !== undefined existingProviderOrder !== undefined
? { ? {
...cfg.auth?.order, ...cfg.auth?.order,
[params.provider]: existingProviderOrder.includes(params.profileId) [params.provider]: reorderedProviderOrder?.includes(params.profileId)
? existingProviderOrder ? reorderedProviderOrder
: [...existingProviderOrder, params.profileId], : [...(reorderedProviderOrder ?? []), params.profileId],
} }
: cfg.auth?.order; : cfg.auth?.order;
return { return {