Skip to content

Commit a896a5a

Browse files
committed
fix(onboard): preserve workspace in provider choice normalization
1 parent 42d6e08 commit a896a5a

4 files changed

Lines changed: 148 additions & 16 deletions

File tree

‎src/commands/auth-choice.apply.api-providers.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
// API-provider auth-choice tests cover built-in provider config, API keys, and provider plugin setup.
22
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import * as providerAuthChoices from "../plugins/provider-auth-choices.js";
34
import type { ProviderPlugin } from "../plugins/types.js";
45
import { normalizeApiKeyTokenProviderAuthChoice } from "./auth-choice.apply.api-providers.js";
6+
import { prepareAuthChoice } from "./auth-choice.apply.js";
57

68
const resolvePluginProviders = vi.hoisted(() =>
79
vi.fn<typeof import("../plugins/provider-auth-choice.runtime.js").resolvePluginProviders>(),
810
);
911
const resolvePluginSetupProvider = vi.hoisted(() => vi.fn(() => undefined));
12+
const prepareAuthChoiceLoadedPluginProvider = vi.hoisted(() =>
13+
vi.fn<
14+
typeof import("../plugins/provider-auth-choice.js").prepareAuthChoiceLoadedPluginProvider
15+
>(),
16+
);
17+
const resolveDeprecatedProviderInstallCatalogEntry = vi.hoisted(() =>
18+
vi.fn<
19+
typeof import("../plugins/provider-install-catalog.js").resolveDeprecatedProviderInstallCatalogEntry
20+
>(),
21+
);
1022

1123
vi.mock("../plugins/provider-auth-choice.runtime.js", () => ({
1224
resolvePluginProviders,
1325
resolvePluginSetupProvider,
1426
}));
27+
vi.mock("../plugins/provider-auth-choice.js", () => ({
28+
prepareAuthChoiceLoadedPluginProvider,
29+
}));
30+
vi.mock("../plugins/provider-install-catalog.js", () => ({
31+
resolveDeprecatedProviderInstallCatalogEntry,
32+
}));
1533

1634
function createProvider(params: {
1735
id: string;
@@ -39,6 +57,9 @@ function createProvider(params: {
3957
describe("normalizeApiKeyTokenProviderAuthChoice", () => {
4058
afterEach(() => {
4159
resolvePluginProviders.mockReset();
60+
prepareAuthChoiceLoadedPluginProvider.mockReset();
61+
resolveDeprecatedProviderInstallCatalogEntry.mockReset();
62+
vi.restoreAllMocks();
4263
});
4364

4465
it("maps token provider auth through plugin token methods", () => {
@@ -89,4 +110,94 @@ describe("normalizeApiKeyTokenProviderAuthChoice", () => {
89110
}),
90111
).toBe("token");
91112
});
113+
114+
it.each([
115+
{ authChoice: "apiKey", kind: "api_key" },
116+
{ authChoice: "token", kind: "token" },
117+
{ authChoice: "setup-token", kind: "token" },
118+
] as const)(
119+
"resolves workspace-only $authChoice providers through interactive setup",
120+
async (choice) => {
121+
const workspaceDir = "/tmp/selected-agent-workspace";
122+
const provider = createProvider({
123+
id: "workspace-provider",
124+
auth: [{ id: "workspace-auth", kind: choice.kind, choiceId: "workspace-provider-auth" }],
125+
});
126+
resolvePluginProviders.mockImplementation((params) =>
127+
params?.workspaceDir === workspaceDir ? [provider] : [],
128+
);
129+
prepareAuthChoiceLoadedPluginProvider.mockImplementation(async (params) =>
130+
params.authChoice === "workspace-provider-auth"
131+
? {
132+
config: params.config,
133+
authProfiles: [
134+
{
135+
profileId: "workspace-provider:default",
136+
credential: {
137+
type: "api_key",
138+
provider: "workspace-provider",
139+
key: "fixture-workspace-key",
140+
},
141+
},
142+
],
143+
persistAuthProfiles: async () => {},
144+
}
145+
: null,
146+
);
147+
148+
const prepared = await prepareAuthChoice({
149+
authChoice: choice.authChoice,
150+
config: {},
151+
workspaceDir,
152+
prompter: {} as never,
153+
runtime: {} as never,
154+
setDefaultModel: false,
155+
opts: { tokenProvider: provider.id },
156+
});
157+
158+
expect(prepared.authProfiles).toEqual([
159+
{
160+
profileId: "workspace-provider:default",
161+
credential: {
162+
type: "api_key",
163+
provider: "workspace-provider",
164+
key: "fixture-workspace-key",
165+
},
166+
},
167+
]);
168+
},
169+
);
170+
171+
it.each(["manifest", "install catalog"] as const)(
172+
"resolves workspace-only deprecated auth choices from the %s",
173+
async (source) => {
174+
const workspaceDir = "/tmp/selected-agent-workspace";
175+
vi.spyOn(
176+
providerAuthChoices,
177+
"resolveManifestDeprecatedProviderAuthChoice",
178+
).mockImplementation((_choice, params) =>
179+
source === "manifest" && params?.workspaceDir === workspaceDir
180+
? ({ choiceId: "workspace-modern-auth" } as never)
181+
: undefined,
182+
);
183+
resolveDeprecatedProviderInstallCatalogEntry.mockImplementation((_choice, params) =>
184+
source === "install catalog" &&
185+
params?.workspaceDir === workspaceDir &&
186+
params.includeUntrustedWorkspacePlugins === false
187+
? ({ choiceId: "workspace-modern-auth" } as never)
188+
: undefined,
189+
);
190+
191+
await expect(
192+
prepareAuthChoice({
193+
authChoice: "workspace-legacy-auth",
194+
config: {},
195+
workspaceDir,
196+
prompter: {} as never,
197+
runtime: {} as never,
198+
setDefaultModel: false,
199+
}),
200+
).rejects.toThrow('Use "workspace-modern-auth" instead');
201+
},
202+
);
92203
});

‎src/commands/auth-choice.apply.ts‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { AuthChoice } from "./onboard-types.js";
1010

1111
async function normalizeLegacyChoice(
1212
authChoice: AuthChoice | undefined,
13-
params: Pick<ApplyAuthChoiceParams, "config" | "env">,
13+
params: Pick<ApplyAuthChoiceParams, "config" | "env" | "workspaceDir">,
1414
): Promise<AuthChoice | undefined> {
1515
if (authChoice === "oauth") {
1616
return "setup-token";
@@ -42,31 +42,28 @@ async function normalizeTokenProviderChoice(params: {
4242
authChoice: params.authChoice,
4343
tokenProvider: params.source.opts.tokenProvider,
4444
config: params.source.config,
45+
workspaceDir: params.source.workspaceDir,
4546
env: params.source.env,
4647
});
4748
}
4849

4950
async function formatDeprecatedProviderChoiceError(
5051
authChoice: AuthChoice | undefined,
51-
params: Pick<ApplyAuthChoiceParams, "config" | "env">,
52+
params: Pick<ApplyAuthChoiceParams, "config" | "env" | "workspaceDir">,
5253
): Promise<string | undefined> {
5354
if (typeof authChoice !== "string") {
5455
return undefined;
5556
}
5657
const { resolveManifestDeprecatedProviderAuthChoice } =
5758
await import("../plugins/provider-auth-choices.js");
58-
const deprecatedChoice = resolveManifestDeprecatedProviderAuthChoice(authChoice, {
59-
config: params.config,
60-
env: params.env,
61-
});
59+
const deprecatedChoice = resolveManifestDeprecatedProviderAuthChoice(authChoice, params);
6260
if (deprecatedChoice) {
6361
return `Auth choice ${JSON.stringify(authChoice)} is no longer supported. Use ${JSON.stringify(deprecatedChoice.choiceId)} instead, or run ${formatCliCommand("openclaw onboard")} to choose interactively.`;
6462
}
6563
const { resolveDeprecatedProviderInstallCatalogEntry } =
6664
await import("../plugins/provider-install-catalog.js");
6765
const externalDeprecatedChoice = resolveDeprecatedProviderInstallCatalogEntry(authChoice, {
68-
config: params.config,
69-
env: params.env,
66+
...params,
7067
includeUntrustedWorkspacePlugins: false,
7168
});
7269
if (!externalDeprecatedChoice) {
@@ -80,10 +77,7 @@ export async function prepareAuthChoice(
8077
params: ApplyAuthChoiceParams,
8178
): Promise<PreparedAuthChoiceResult> {
8279
const normalizedAuthChoice =
83-
(await normalizeLegacyChoice(params.authChoice, {
84-
config: params.config,
85-
env: params.env,
86-
})) ?? params.authChoice;
80+
(await normalizeLegacyChoice(params.authChoice, params)) ?? params.authChoice;
8781
const normalizedProviderAuthChoice = await normalizeTokenProviderChoice({
8882
authChoice: normalizedAuthChoice,
8983
source: params,
@@ -99,10 +93,7 @@ export async function prepareAuthChoice(
9993

10094
const deprecatedProviderChoiceError = await formatDeprecatedProviderChoiceError(
10195
normalizedParams.authChoice,
102-
{
103-
config: normalizedParams.config,
104-
env: normalizedParams.env,
105-
},
96+
normalizedParams,
10697
);
10798
if (deprecatedProviderChoiceError) {
10899
throw new Error(deprecatedProviderChoiceError);

‎src/commands/onboard-non-interactive/local/auth-choice.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import type { OpenClawConfig } from "../../../config/config.js";
44
import { resolveAgentModelPrimaryValue } from "../../../config/model-input.js";
5+
import * as apiProviderAuthChoices from "../../auth-choice.apply.api-providers.js";
56
import { commitNonInteractiveOnboardConfig } from "../config-write.js";
67
import { applyNonInteractiveAuthChoice } from "./auth-choice.js";
78

@@ -107,6 +108,34 @@ describe("applyNonInteractiveAuthChoice", () => {
107108
expect(runtime.exit).not.toHaveBeenCalled();
108109
});
109110

111+
it("resolves generic provider auth from the selected agent workspace", async () => {
112+
const runtime = createRuntime();
113+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
114+
const resolvedConfig = { auth: { profiles: { "demo-provider:default": { mode: "api_key" } } } };
115+
applyNonInteractivePluginProviderChoice.mockResolvedValueOnce(resolvedConfig as never);
116+
const normalize = vi
117+
.spyOn(apiProviderAuthChoices, "normalizeApiKeyTokenProviderAuthChoice")
118+
.mockImplementation((params) =>
119+
params.workspaceDir === target.workspaceDir ? "demo-provider-api-key" : params.authChoice,
120+
);
121+
122+
try {
123+
const result = await applyNonInteractiveAuthChoice({
124+
nextConfig,
125+
authChoice: "apiKey",
126+
opts: { tokenProvider: "demo-provider" },
127+
runtime: runtime as never,
128+
baseConfig: nextConfig,
129+
target,
130+
});
131+
132+
expect(result).toBe(resolvedConfig);
133+
expect(runtime.error).not.toHaveBeenCalled();
134+
} finally {
135+
normalize.mockRestore();
136+
}
137+
});
138+
110139
it("resolves plugin provider auth before builtin custom-provider handling", async () => {
111140
const runtime = createRuntime();
112141
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;

‎src/commands/onboard-non-interactive/local/auth-choice.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export async function applyNonInteractiveAuthChoice(params: {
5050
authChoice: params.authChoice,
5151
tokenProvider: opts.tokenProvider,
5252
config: params.nextConfig,
53+
workspaceDir: params.target.workspaceDir,
5354
env: process.env,
5455
});
5556
const nextConfig = params.nextConfig;

0 commit comments

Comments
 (0)