sourcegraph / cody

AI that knows your entire codebase
https://cody.dev
Apache License 2.0
2.22k stars 213 forks source link

CLI: add `auth` subcommand #4724

Closed olafurpg closed 2 days ago

olafurpg commented 2 days ago

Fixes CODY-2633

Previously, users had to set the environment variables SRC_ACCESS_TOKEN and SRC_ENDPOINT to authenticate with the Cody CLI. Now, users can use the cody auth login --web command instead.

cody auth login --web
cody auth logout
cody auth accounts

The login --web command allows users to authenticate through the browser instead. The non-secret configuration settings (username and server endpoint) are stored in a JSON file in the users Cody "config directory", and the access token is stored in the operating systems secret storage (Keychain on macOS, Secret Service API/libsecret on Linux, and Credential Vault on Windows).

Implementation note: from past experience, I decided to implement multi-account support from the start. Most casual users don't need this functionality but it's super convenient for internal testing and it's not much more effort compared to only supporting a single account.

Test plan

Manually tested these flows:

# Without SRC_ACCESS_TOKEN
cody auth login # errors
cody auth login --web # browser redirect works
cody auth login --web # already logged in
cody auth login # already logged in
cody auth login # prints current account
cody auth accounts # lists one account
cody auth logout # logs out
cody auth logout # errors, already logged out

# With SRC_ACCESS
cody auth login # Success, uses token
cody auth login # In separate terminal without SRC_ACCESS_TOKEN, works
olafurpg commented 2 days ago

The failing Windows check is unrelated to this PR

 FAIL  |agent| src/chat-response-quality.test.ts [ agent/src/chat-response-quality.test.ts ]
Error: Command failed: pnpm run build:agent
 ❯ checkExecSyncError ../node:child_process:890:11
 ❯ Proxy.execSync ../node:child_process:962:15
 ❯ buildAgentBinary src/TestClient.ts:103:5
    101|     //

    102|     //   pnpm test agent/src/index.test.ts
olafurpg commented 2 days ago

mkdir'ing that dir fixed it.

Fixed!

I was surprised that it signed me in automatically, though! It didn't show me an auth screen in my browser:

Good point. I added a confirmation dialog when both --web and --access-token (SRC_ACCESS_TOKEN) are present. CleanShot 2024-06-28 at 16 22 31@2x

olafurpg commented 2 days ago

The Windows CI failure is suspicious because it has failed consistently on every check in this PR and I haven't seen this failure in other CI jobs on main. I don't understand why it always fails on agent/src/chat-response-quality.test.ts 🤔 will wait with merging until I get all the checks green

jtibshirani commented 2 days ago

I don't understand why it always fails on agent/src/chat-response-quality.test.ts

I haven't reviewed this PR carefully, but just chiming in to say -- we should delete this test! It is fragile, and it's fully superseded by the Chat questions in cody-leaderboard.

olafurpg commented 2 days ago

I'll try that and see if it helps. Thank you for chiming in!