fix(cli): load .env files in agent commands for authentication#6481
fix(cli): load .env files in agent commands for authentication#6481lalalune wants to merge 2 commits into
Conversation
This commit fixes an issue where the `elizaos agent` commands would fail when connecting to a remote server that uses ELIZA_SERVER_AUTH_TOKEN. The problem was that environment variables from .env files were not being loaded before making API requests, causing authentication failures with "Missing or invalid X-API-KEY" errors. Changes: - Added loadEnvironment() call in getAgents() function - This ensures ELIZA_SERVER_AUTH_TOKEN is read from .env files - Fixes all agent commands (list, get, stop, remove, etc.) Closes #5707
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Claude finished @lalalune's task in 1m 24s —— View job Reviewing PR #6481
Review SummaryThis PR adds a ❌ CRITICAL: No Tests for This ChangeThe PR adds behavior without corresponding test coverage. The existing Fix: Add a unit test that verifies:
Example test location:
|
| export async function getAgents(opts: OptionValues): Promise<AgentBasic[]> { | ||
| // Load environment variables to ensure ELIZA_SERVER_AUTH_TOKEN is available | ||
| await loadEnvironment(); | ||
|
|
There was a problem hiding this comment.
Loads wrong .env location
getAgents() now calls loadEnvironment() with no args, which defaults to process.cwd() (packages/cli/src/utils/get-config.ts:1436). If agent commands are invoked from a directory other than the target project root, this will load the wrong .env (or none) and ELIZA_SERVER_AUTH_TOKEN still won’t be available. This is especially likely since CLI commands often run from the user’s shell cwd, not the repo root.
Also, because resolveAgentId() calls getAgents() (and other callers may too), this triggers .env loading on each call; consider loading once at command entrypoint or passing an explicit project dir into getAgents()/createApiClientConfig.
Reopened from #6376 (was merged then rolled back)
Made with Cursor
Greptile Overview
Greptile Summary
This PR updates the agent command validation utilities to load environment variables (via
loadEnvironment()) before constructing the API client config, soELIZA_SERVER_AUTH_TOKENcan be picked up from a.envfile when listing agents.The change is localized to
getAgents()inpackages/cli/src/commands/agent/utils/validation.ts, which is used byresolveAgentId()to fetch agents and resolve IDs/names/indices.Confidence Score: 3/5
loadEnvironment()defaults toprocess.cwd(), which can be different from the target project directory; in those cases the auth token still won’t be loaded and the original bug persists. There are no obvious type/runtime errors introduced.Important Files Changed
Sequence Diagram
sequenceDiagram participant Cmd as agent CLI command participant Val as validation.getAgents participant Env as loadEnvironment participant Cfg as createApiClientConfig participant API as AgentsService Cmd->>Val: resolveAgentId() / getAgents(opts) Val->>Env: loadEnvironment(process.cwd()) Env-->>Val: process.env populated (maybe) Val->>Cfg: createApiClientConfig(opts) Cfg-->>Val: config (auth token from env) Val->>API: listAgents() API-->>Val: {agents: ...} Val-->>Cmd: agents / agentId