Skip to content

feat(cli): add OpenRouter support for AI and embedding models#1

Open
Neysixx wants to merge 48 commits intodevelopfrom
fix/cli-openrouter-embedding
Open

feat(cli): add OpenRouter support for AI and embedding models#1
Neysixx wants to merge 48 commits intodevelopfrom
fix/cli-openrouter-embedding

Conversation

@Neysixx
Copy link
Owner

@Neysixx Neysixx commented Nov 11, 2025

Relates to

Improves OpenRouter integration in ElizaOS CLI by adding native embedding support, eliminating the need for users to configure a separate embedding provider when using OpenRouter.

Risks

Low Risk - Well-contained changes following existing patterns

  • User Impact: No breaking changes. Existing OpenRouter configurations continue to work unchanged.
  • Test Coverage: Unit tests updated to reflect new behavior (embedding support enabled for OpenRouter)
  • Configuration: Follows the same pattern as existing providers (OpenAI, Google, Ollama) for both interactive and non-interactive modes
  • Validation: Uses existing isValidOpenRouterKey function to validate API key format (sk-or- prefix)

Background

What does this PR do?

This PR adds comprehensive OpenRouter support for embeddings in the ElizaOS CLI:

  1. Updates hasEmbeddingSupport to include 'openrouter' as a model with built-in embedding support
  2. Adds OpenRouter to getAvailableEmbeddingModels as a fallback option when the primary AI model doesn't support embeddings (e.g., Claude)
  3. Implements setup configuration in both setupAIModelConfig and setupEmbeddingModelConfig to handle:
    • Interactive mode: Prompts users for OpenRouter API key
    • Non-interactive mode: Writes placeholder configuration with helpful comments
  4. Updates unit tests to reflect that OpenRouter now has embedding support

Technical Details:

  • Adds OpenRouter configuration with OPENROUTER_API_KEY, OPENROUTER_EMBEDDING_MODEL, and OPENROUTER_EMBEDDING_DIMENSIONS
  • Leverages OpenRouter's unified API access to multiple embedding models (including OpenAI's text-embedding-3-small)
  • Reuses existing promptAndStoreOpenRouterKey helper function from get-config.ts

What kind of change is this?

  • Features (non-breaking change which adds functionality)
  • Improvements (misc. changes to existing features)
  • Updates (test updates to reflect new behavior)

Why are we doing this? Any context or related work?

Problem:

  • ElizaOS requires embeddings for agent memory, semantic search, and knowledge base functionality
  • OpenRouter was only available as an AI text generation provider, not for embeddings
  • Users selecting OpenRouter for AI had to configure a second provider (OpenAI, Google, or Ollama) solely for embeddings

Solution:

  • OpenRouter provides access to multiple embedding models through its unified API
  • This PR enables users to use a single OpenRouter API key for both text generation and embeddings
  • Reduces configuration complexity and API key management overhead

Documentation changes needed?

  • My changes require a change to the project documentation.
  • Documentation partially exists: The .env template in packages/cli/src/utils/get-config.ts already includes OpenRouter environment variables (lines 79-82)

Recommended documentation updates:

  1. Add OpenRouter to the list of recommended providers supporting both AI + embeddings in README
  2. Document the three OpenRouter environment variables with example values:
    • OPENROUTER_API_KEY=sk-or-v1-...
    • OPENROUTER_EMBEDDING_MODEL=openai/text-embedding-3-small
    • OPENROUTER_EMBEDDING_DIMENSIONS=1536
  3. Optional: Add migration note for existing users who can now simplify their configuration

Testing

Where should a reviewer start?

Review Priority:

  1. Core logic changes - packages/cli/src/commands/create/utils/selection.ts

    • Line 50: hasEmbeddingSupport now includes 'openrouter'
    • Lines 140-144: OpenRouter added to getAvailableEmbeddingModels
  2. Configuration setup - packages/cli/src/commands/create/actions/setup.ts

    • Lines 113-136: setupAIModelConfig OpenRouter case
    • Lines 288-362: setupEmbeddingModelConfig OpenRouter case
  3. Test updates - packages/cli/tests/unit/utils/selection.test.ts

    • Line 20: OpenRouter added to models with embedding support
    • Lines 71-72: Correctly categorizes OpenRouter as having embeddings

Detailed testing steps

1. Unit Tests ✅

cd packages/cli
bun run test tests/unit/utils/selection.test.ts

Expected: All tests pass

  • hasEmbeddingSupport('openrouter') returns true
  • ✅ OpenRouter correctly identified as model with embedding support

2. Interactive Mode - OpenRouter as Primary AI

elizaos create test-openrouter-primary
# Select: OpenRouter as AI model
# Expected: Should NOT be prompted for embedding model

Verify:

  • .env contains OPENROUTER_API_KEY=
  • User is prompted for OpenRouter API key interactively
  • No separate embedding model configuration is added

3. Interactive Mode - OpenRouter as Embedding Fallback

elizaos create test-claude-openrouter
# Select: Claude as AI model
# Select: OpenRouter as embedding model

Verify:

  • .env contains ANTHROPIC_API_KEY=
  • .env contains OPENROUTER_API_KEY=
  • .env contains OPENROUTER_EMBEDDING_MODEL= and OPENROUTER_EMBEDDING_DIMENSIONS=

4. Non-Interactive Mode

elizaos create test-noninteractive --ai-model openrouter --non-interactive

Verify:

  • .env created with placeholder: OPENROUTER_API_KEY=your_openrouter_api_key_here
  • Comment included: # Get your API key from: https://openrouter.ai/keys
  • Default embedding model and dimensions added

5. Regression Testing - Other Providers

# Verify other providers still work correctly
elizaos create test-openai --ai-model openai --non-interactive
elizaos create test-ollama --ai-model local --non-interactive
elizaos create test-google --ai-model google --non-interactive

Expected: All providers create projects successfully with correct .env configurations

6. Optional: E2E Testing with Real API Key

# With a real OpenRouter API key
export OPENROUTER_API_KEY=sk-or-v1-xxx...
export OPENROUTER_EMBEDDING_MODEL=openai/text-embedding-3-small
export OPENROUTER_EMBEDDING_DIMENSIONS=1536
cd packages/cli
bun run test:e2e

Discord username

neysixx

letmehateu and others added 30 commits October 8, 2025 11:59
- Updated `hasEmbeddingSupport` to include 'openrouter' as a supported model.
- Enhanced `getAvailableEmbeddingModels` to provide options for OpenRouter.
- Added configuration options for OpenRouter in the `.env` file setup, including API key and embedding model details.
- Implemented setup functions for AI and embedding model configurations to handle OpenRouter in both interactive and non-interactive modes.
- Added 'openrouter' to the list of models with embedding support in the `hasEmbeddingSupport` function.
- Adjusted the model selection flow to reflect the correct categorization of models needing separate embedding providers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments