mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 03:20:46 +00:00
fix: honor agent for models auth writes (#71933)
Honor the parent `models auth --agent <id>` flag across auth write commands: `add`, `login`, `setup-token`, `paste-token`, and `login-github-copilot`. The auth helpers now resolve the requested configured agent before choosing the auth-profile store and provider workspace, while preserving default-agent behavior when `--agent` is omitted. Validation: - `pnpm test src/cli/models-cli.test.ts src/commands/models/auth.test.ts` - `pnpm test src/commands/models/auth.test.ts` - `pnpm docs:check-mdx` - `pnpm check:changed` - `pnpm check` - `pnpm build` - `pnpm test src/cli/run-main.test.ts` Full `pnpm test` was also run; it failed in unrelated `src/cli/run-main.test.ts` assertions during the full-suite order, while the exact file passes on both latest main and this branch. The PR diff only touches models auth CLI/auth files, docs, and changelog. Fixes #71864. Thanks @neeravmakwana.
This commit is contained in:
@@ -6,10 +6,19 @@ import { registerModelsCli } from "./models-cli.js";
|
||||
const mocks = vi.hoisted(() => ({
|
||||
modelsStatusCommand: vi.fn().mockResolvedValue(undefined),
|
||||
noopAsync: vi.fn(async () => undefined),
|
||||
modelsAuthAddCommand: vi.fn().mockResolvedValue(undefined),
|
||||
modelsAuthLoginCommand: vi.fn().mockResolvedValue(undefined),
|
||||
modelsAuthPasteTokenCommand: vi.fn().mockResolvedValue(undefined),
|
||||
modelsAuthSetupTokenCommand: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const { modelsStatusCommand, modelsAuthLoginCommand } = mocks;
|
||||
const {
|
||||
modelsAuthAddCommand,
|
||||
modelsAuthLoginCommand,
|
||||
modelsAuthPasteTokenCommand,
|
||||
modelsAuthSetupTokenCommand,
|
||||
modelsStatusCommand,
|
||||
} = mocks;
|
||||
|
||||
vi.mock("../commands/models.js", () => ({
|
||||
modelsStatusCommand: mocks.modelsStatusCommand,
|
||||
@@ -41,10 +50,10 @@ vi.mock("../commands/models/list.js", () => ({
|
||||
modelsStatusCommand: mocks.modelsStatusCommand,
|
||||
}));
|
||||
vi.mock("../commands/models/auth.js", () => ({
|
||||
modelsAuthAddCommand: mocks.noopAsync,
|
||||
modelsAuthAddCommand: mocks.modelsAuthAddCommand,
|
||||
modelsAuthLoginCommand: mocks.modelsAuthLoginCommand,
|
||||
modelsAuthPasteTokenCommand: mocks.noopAsync,
|
||||
modelsAuthSetupTokenCommand: mocks.noopAsync,
|
||||
modelsAuthPasteTokenCommand: mocks.modelsAuthPasteTokenCommand,
|
||||
modelsAuthSetupTokenCommand: mocks.modelsAuthSetupTokenCommand,
|
||||
}));
|
||||
vi.mock("../commands/models/auth-order.js", () => ({
|
||||
modelsAuthOrderClearCommand: mocks.noopAsync,
|
||||
@@ -80,7 +89,10 @@ vi.mock("../commands/models/set-image.js", () => ({
|
||||
|
||||
describe("models cli", () => {
|
||||
beforeEach(() => {
|
||||
modelsAuthAddCommand.mockClear();
|
||||
modelsAuthLoginCommand.mockClear();
|
||||
modelsAuthPasteTokenCommand.mockClear();
|
||||
modelsAuthSetupTokenCommand.mockClear();
|
||||
modelsStatusCommand.mockClear();
|
||||
});
|
||||
|
||||
@@ -108,9 +120,10 @@ describe("models cli", () => {
|
||||
const login = auth?.commands.find((cmd) => cmd.name() === "login-github-copilot");
|
||||
expect(login).toBeTruthy();
|
||||
|
||||
await program.parseAsync(["models", "auth", "login-github-copilot", "--yes"], {
|
||||
from: "user",
|
||||
});
|
||||
await program.parseAsync(
|
||||
["models", "auth", "--agent", "poe", "login-github-copilot", "--yes"],
|
||||
{ from: "user" },
|
||||
);
|
||||
|
||||
expect(modelsAuthLoginCommand).toHaveBeenCalledTimes(1);
|
||||
expect(modelsAuthLoginCommand).toHaveBeenCalledWith(
|
||||
@@ -118,6 +131,7 @@ describe("models cli", () => {
|
||||
provider: "github-copilot",
|
||||
method: "device",
|
||||
yes: true,
|
||||
agent: "poe",
|
||||
}),
|
||||
expect.any(Object),
|
||||
);
|
||||
@@ -134,6 +148,43 @@ describe("models cli", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
label: "add",
|
||||
args: ["models", "auth", "--agent", "poe", "add"],
|
||||
command: modelsAuthAddCommand,
|
||||
expected: { agent: "poe" },
|
||||
},
|
||||
{
|
||||
label: "login",
|
||||
args: ["models", "auth", "--agent", "poe", "login", "--provider", "openai-codex"],
|
||||
command: modelsAuthLoginCommand,
|
||||
expected: { agent: "poe", provider: "openai-codex" },
|
||||
},
|
||||
{
|
||||
label: "setup-token",
|
||||
args: ["models", "auth", "--agent", "poe", "setup-token", "--provider", "anthropic"],
|
||||
command: modelsAuthSetupTokenCommand,
|
||||
expected: { agent: "poe", provider: "anthropic" },
|
||||
},
|
||||
{
|
||||
label: "paste-token",
|
||||
args: ["models", "auth", "--agent", "poe", "paste-token", "--provider", "anthropic"],
|
||||
command: modelsAuthPasteTokenCommand,
|
||||
expected: { agent: "poe", provider: "anthropic" },
|
||||
},
|
||||
{
|
||||
label: "login-github-copilot",
|
||||
args: ["models", "auth", "--agent", "poe", "login-github-copilot", "--yes"],
|
||||
command: modelsAuthLoginCommand,
|
||||
expected: { agent: "poe", provider: "github-copilot", method: "device", yes: true },
|
||||
},
|
||||
])("passes parent --agent to models auth $label", async ({ args, command, expected }) => {
|
||||
await runModelsCommand(args);
|
||||
|
||||
expect(command).toHaveBeenCalledWith(expect.objectContaining(expected), expect.any(Object));
|
||||
});
|
||||
|
||||
it("shows help for models auth without error exit", async () => {
|
||||
const program = new Command();
|
||||
program.exitOverride();
|
||||
|
||||
@@ -282,7 +282,7 @@ export function registerModelsCli(program: Command) {
|
||||
});
|
||||
|
||||
const auth = models.command("auth").description("Manage model auth profiles");
|
||||
auth.option("--agent <id>", "Agent id for auth order get/set/clear");
|
||||
auth.option("--agent <id>", "Agent id for auth commands");
|
||||
auth.action(() => {
|
||||
auth.help();
|
||||
});
|
||||
@@ -290,10 +290,13 @@ export function registerModelsCli(program: Command) {
|
||||
auth
|
||||
.command("add")
|
||||
.description("Interactive auth helper (provider auth or paste token)")
|
||||
.action(async () => {
|
||||
.action(async (command) => {
|
||||
const agent =
|
||||
resolveOptionFromCommand<string>(command, "agent") ??
|
||||
resolveOptionFromCommand<string>(auth, "agent");
|
||||
await runModelsCommand(async () => {
|
||||
const { modelsAuthAddCommand } = await import("../commands/models/auth.js");
|
||||
await modelsAuthAddCommand({}, defaultRuntime);
|
||||
await modelsAuthAddCommand({ agent }, defaultRuntime);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -303,7 +306,8 @@ export function registerModelsCli(program: Command) {
|
||||
.option("--provider <id>", "Provider id registered by a plugin")
|
||||
.option("--method <id>", "Provider auth method id")
|
||||
.option("--set-default", "Apply the provider's default model recommendation", false)
|
||||
.action(async (opts) => {
|
||||
.action(async (opts, command) => {
|
||||
const agent = resolveOptionFromCommand<string>(command, "agent");
|
||||
await runModelsCommand(async () => {
|
||||
const { modelsAuthLoginCommand } = await import("../commands/models/auth.js");
|
||||
await modelsAuthLoginCommand(
|
||||
@@ -311,6 +315,7 @@ export function registerModelsCli(program: Command) {
|
||||
provider: opts.provider as string | undefined,
|
||||
method: opts.method as string | undefined,
|
||||
setDefault: Boolean(opts.setDefault),
|
||||
agent,
|
||||
},
|
||||
defaultRuntime,
|
||||
);
|
||||
@@ -322,13 +327,15 @@ export function registerModelsCli(program: Command) {
|
||||
.description("Run a provider CLI to create/sync a token (TTY required)")
|
||||
.option("--provider <name>", "Provider id")
|
||||
.option("--yes", "Skip confirmation", false)
|
||||
.action(async (opts) => {
|
||||
.action(async (opts, command) => {
|
||||
const agent = resolveOptionFromCommand<string>(command, "agent");
|
||||
await runModelsCommand(async () => {
|
||||
const { modelsAuthSetupTokenCommand } = await import("../commands/models/auth.js");
|
||||
await modelsAuthSetupTokenCommand(
|
||||
{
|
||||
provider: opts.provider as string | undefined,
|
||||
yes: Boolean(opts.yes),
|
||||
agent,
|
||||
},
|
||||
defaultRuntime,
|
||||
);
|
||||
@@ -344,7 +351,8 @@ export function registerModelsCli(program: Command) {
|
||||
"--expires-in <duration>",
|
||||
"Optional expiry duration (e.g. 365d, 12h). Stored as absolute expiresAt.",
|
||||
)
|
||||
.action(async (opts) => {
|
||||
.action(async (opts, command) => {
|
||||
const agent = resolveOptionFromCommand<string>(command, "agent");
|
||||
await runModelsCommand(async () => {
|
||||
const { modelsAuthPasteTokenCommand } = await import("../commands/models/auth.js");
|
||||
await modelsAuthPasteTokenCommand(
|
||||
@@ -352,6 +360,7 @@ export function registerModelsCli(program: Command) {
|
||||
provider: opts.provider as string | undefined,
|
||||
profileId: opts.profileId as string | undefined,
|
||||
expiresIn: opts.expiresIn as string | undefined,
|
||||
agent,
|
||||
},
|
||||
defaultRuntime,
|
||||
);
|
||||
@@ -362,7 +371,8 @@ export function registerModelsCli(program: Command) {
|
||||
.command("login-github-copilot")
|
||||
.description("Login to GitHub Copilot via GitHub device flow (TTY required)")
|
||||
.option("--yes", "Overwrite existing profile without prompting", false)
|
||||
.action(async (opts) => {
|
||||
.action(async (opts, command) => {
|
||||
const agent = resolveOptionFromCommand<string>(command, "agent");
|
||||
await runModelsCommand(async () => {
|
||||
const { modelsAuthLoginCommand } = await import("../commands/models/auth.js");
|
||||
await modelsAuthLoginCommand(
|
||||
@@ -370,6 +380,7 @@ export function registerModelsCli(program: Command) {
|
||||
provider: "github-copilot",
|
||||
method: "device",
|
||||
yes: Boolean(opts.yes),
|
||||
agent,
|
||||
},
|
||||
defaultRuntime,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user