Skip to content

Commit 8d3929e

Browse files
committed
fix: honor Google auth order for Gemini CLI
1 parent eb92a0b commit 8d3929e

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/agents/command/attempt-execution.cli.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,80 @@ describe("CLI attempt execution", () => {
877877
expect(firstRunCliAgentArg().authProfileId).toBe("google:api-key");
878878
});
879879

880+
it("selects canonical Google API-key auth order for Google models routed through Gemini CLI", async () => {
881+
const sessionKey = "agent:main:direct:gemini-cli-google-api-key-order";
882+
const sessionEntry: SessionEntry = {
883+
sessionId: "openclaw-session-gemini-api-key-order",
884+
updatedAt: Date.now(),
885+
};
886+
const sessionStore: Record<string, SessionEntry> = { [sessionKey]: sessionEntry };
887+
await fs.writeFile(storePath, JSON.stringify(sessionStore, null, 2), "utf-8");
888+
saveAuthProfileStore(
889+
{
890+
version: 1,
891+
profiles: {
892+
"google:api-key": {
893+
type: "api_key",
894+
provider: "google",
895+
key: "gemini-api-key",
896+
},
897+
},
898+
},
899+
tmpDir,
900+
{ filterExternalAuthProfiles: false, syncExternalCli: false },
901+
);
902+
runCliAgentMock.mockResolvedValueOnce(makeCliResult("gemini cli api-key response"));
903+
904+
await runAgentAttempt({
905+
providerOverride: "google",
906+
originalProvider: "google",
907+
modelOverride: "gemini-3.1-pro-preview",
908+
cfg: {
909+
auth: {
910+
order: {
911+
google: ["google:api-key"],
912+
},
913+
},
914+
agents: {
915+
defaults: {
916+
models: {
917+
"google/gemini-3.1-pro-preview": {
918+
agentRuntime: { id: "google-gemini-cli" },
919+
},
920+
},
921+
},
922+
},
923+
} as OpenClawConfig,
924+
sessionEntry,
925+
sessionId: sessionEntry.sessionId,
926+
sessionKey,
927+
sessionAgentId: "main",
928+
sessionFile: path.join(tmpDir, "session.jsonl"),
929+
workspaceDir: tmpDir,
930+
body: "continue",
931+
isFallbackRetry: false,
932+
resolvedThinkLevel: "medium",
933+
timeoutMs: 1_000,
934+
runId: "run-gemini-cli-google-api-key-order",
935+
opts: {} as Parameters<typeof runAgentAttempt>[0]["opts"],
936+
runContext: {} as Parameters<typeof runAgentAttempt>[0]["runContext"],
937+
spawnedBy: undefined,
938+
messageChannel: undefined,
939+
skillsSnapshot: undefined,
940+
resolvedVerboseLevel: undefined,
941+
agentDir: tmpDir,
942+
onAgentEvent: vi.fn(),
943+
authProfileProvider: "google",
944+
sessionStore,
945+
storePath,
946+
sessionHasHistory: false,
947+
});
948+
949+
expect(runCliAgentMock).toHaveBeenCalledTimes(1);
950+
expect(firstRunCliAgentArg().provider).toBe("google-gemini-cli");
951+
expect(firstRunCliAgentArg().authProfileId).toBe("google:api-key");
952+
});
953+
880954
it("persists CLI replies into the session transcript", async () => {
881955
const sessionKey = "agent:main:subagent:cli-transcript";
882956
const sessionFile = path.join(tmpDir, "session-cli-transcript.jsonl");

‎src/agents/command/attempt-execution.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ const ACP_TRANSCRIPT_USAGE = {
9393
total: 0,
9494
},
9595
} as const;
96+
const GOOGLE_GEMINI_CLI_PROVIDER_ID = "google-gemini-cli";
97+
const GOOGLE_PROVIDER_ID = "google";
9698

9799
type TranscriptUsage = {
98100
input?: number;
@@ -237,11 +239,30 @@ function resolveCliExecutionAuthProfileId(params: {
237239
allowKeychainPrompt: false,
238240
externalCliProviderIds: [params.cliExecutionProvider],
239241
});
240-
return resolveAuthProfileOrder({
242+
const cliProfileId = resolveAuthProfileOrder({
241243
cfg: params.config,
242244
store,
243245
provider: params.cliExecutionProvider,
244246
})[0];
247+
if (cliProfileId) {
248+
return cliProfileId;
249+
}
250+
251+
if (
252+
params.cliExecutionProvider !== GOOGLE_GEMINI_CLI_PROVIDER_ID ||
253+
params.selected.authProfileProvider !== GOOGLE_PROVIDER_ID
254+
) {
255+
return undefined;
256+
}
257+
258+
return resolveAuthProfileOrder({
259+
cfg: params.config,
260+
store,
261+
provider: GOOGLE_PROVIDER_ID,
262+
}).find((profileId) => {
263+
const credential = store.profiles[profileId];
264+
return credential?.provider === GOOGLE_PROVIDER_ID && credential.type === "api_key";
265+
});
245266
}
246267

247268
function resolveTranscriptUsage(usage: PersistTextTurnTranscriptParams["assistant"]["usage"]) {

0 commit comments

Comments
 (0)