Skip to content

feat(providers): add openai provider with custom base_url support#385

Open
liuhaibin0528 wants to merge 1 commit into
rohitg00:mainfrom
liuhaibin0528:feat/openai-provider
Open

feat(providers): add openai provider with custom base_url support#385
liuhaibin0528 wants to merge 1 commit into
rohitg00:mainfrom
liuhaibin0528:feat/openai-provider

Conversation

@liuhaibin0528
Copy link
Copy Markdown

@liuhaibin0528 liuhaibin0528 commented May 15, 2026

Problem

agentmemory currently does not support as a summary/compression LLM provider. Users who want to use OpenAI-compatible endpoints (including self-hosted proxies like Ollama, vLLM, etc.) have no way to configure a custom base URL for the summary LLM.

The embedding provider already supports — but the summary LLM provider does not.

Solution

Add as a first-class provider type that reuses the existing implementation (which already handles OpenAI-compatible chat completions API).

Environment Variables

Variable Required Default
OPENAI_API_KEY Yes
OPENAI_BASE_URL No https://api.openai.com/v1/chat/completions
OPENAI_MODEL No gpt-4o-mini

Files Changed

  • src/types.ts — add 'openai' to ProviderType union
  • src/config.tsdetectProvider() openai branch + detectLlmProviderKind() + VALID_PROVIDERS
  • src/providers/index.tscreateBaseProvider() case 'openai'
  • src/providers/openrouter.ts — name detection from ternary to multi-branch (openrouter/gemini/openai)
  • src/functions/summarize.ts — error message includes OPENAI

Testing

  • All existing tests pass (82/84 test files — 2 failures are pre-existing upstream issues unrelated to this change)
  • No new test failures introduced by this PR

Usage Example

# Use official OpenAI
export OPENAI_API_KEY="sk-..."

# Use custom endpoint (e.g. local proxy)
export OPENAI_API_KEY="your-key"
export OPENAI_BASE_URL="http://localhost:8080/v1/chat/completions"
export OPENAI_MODEL="qwen2.5-72b"

Summary by CodeRabbit

  • New Features
    • OpenAI is now supported as an LLM provider option. Configure it using the OPENAI_API_KEY environment variable. You can optionally customize the API endpoint with OPENAI_BASE_URL; if not set, it defaults to OpenAI's official Chat Completions endpoint. System error messages have been updated to list OpenAI among available LLM provider options.

Review Change Stack

Add OPENAI_API_KEY as a first-class LLM provider option for summary/compression.
Uses OpenRouterProvider under the hood with OPENAI_BASE_URL for custom endpoints.

Environment variables:
  OPENAI_API_KEY     — required API key
  OPENAI_BASE_URL    — optional, defaults to api.openai.com/v1/chat/completions
  OPENAI_MODEL       — optional, defaults to gpt-4o-mini

Changes:
- src/types.ts: add 'openai' to ProviderType union
- src/config.ts: detectProvider() openai branch + detectLlmProviderKind() + VALID_PROVIDERS
- src/providers/index.ts: createBaseProvider() case 'openai'
- src/providers/openrouter.ts: name detection from ternary to multi-branch
- src/functions/summarize.ts: error message includes OPENAI
@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

@liuhaibin0528 is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

📝 Walkthrough

Walkthrough

This PR adds OpenAI API key support to the agent memory system. The changes span type definitions, environment detection, provider factory wiring, and provider identification to enable OpenAI as a first-class LLM provider alongside Anthropic, Gemini, and OpenRouter.

Changes

OpenAI provider integration

Layer / File(s) Summary
Provider type contract
src/types.ts
ProviderType union adds "openai" literal to supported provider identifiers.
OpenAI detection and configuration
src/config.ts
detectProvider() recognizes OPENAI_API_KEY and returns provider config with model and baseURL (defaulting to OpenAI chat-completions endpoint). Startup warning message updated to include OPENAI_API_KEY. detectLlmProviderKind() treats OPENAI_API_KEY presence as "llm" provider kind. loadFallbackConfig() adds "openai" to allowed fallback providers.
Provider factory instantiation
src/providers/index.ts
createBaseProvider adds "openai" case that reads OPENAI_API_KEY and OPENAI_BASE_URL environment variables and instantiates OpenRouterProvider with resolved key, model, maxTokens, and base URL.
Provider name mapping and error messages
src/providers/openrouter.ts, src/functions/summarize.ts
OpenRouterProvider constructor updates this.name logic to map URLs to provider identifiers: returns "openrouter" for openrouter URLs, "gemini" for generativelanguage URLs, and "openai" by default. summarize function error message adds OPENAI to guidance text listing supported provider environment variables.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A new provider hops into view,
OpenAI joins the crew,
From config to factory, types align so true,
Detection and mapping—the hop-full debut! 🔑

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding OpenAI provider support with custom base_url configuration.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/providers/index.ts (1)

68-79: ⚡ Quick win

Consider respecting config.baseURL for consistency.

The current implementation re-reads OPENAI_BASE_URL from the environment, ignoring config.baseURL. This differs from the anthropic case (line 85) which uses config.baseURL. While re-reading env works for fallback providers, it prevents programmatic baseURL configuration from being respected.

♻️ Proposed refactor to align with anthropic pattern
 case "openai": {
   const openaiKey = requireEnvVar("OPENAI_API_KEY");
-  const openaiBase =
-    getEnvVar("OPENAI_BASE_URL") ||
-    "https://api.openai.com/v1/chat/completions";
+  const openaiBase =
+    config.baseURL ||
+    getEnvVar("OPENAI_BASE_URL") ||
+    "https://api.openai.com/v1/chat/completions";
   return new OpenRouterProvider(
     openaiKey,
     config.model,
     config.maxTokens,
     openaiBase,
   );
 }

This preserves fallback support while respecting config.baseURL when set.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/index.ts` around lines 68 - 79, The OpenAI provider branch
currently ignores config.baseURL and re-reads OPENAI_BASE_URL from the
environment; change the base URL selection in the case "openai" branch so it
prefers config.baseURL first, then falls back to getEnvVar("OPENAI_BASE_URL"),
then the hardcoded default (same approach used in the anthropic branch). Update
the OpenRouterProvider constructor call to pass this computed base URL (refer to
the case "openai" block, OpenRouterProvider, requireEnvVar, getEnvVar, and
config.baseURL).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/providers/index.ts`:
- Around line 68-79: The OpenAI provider branch currently ignores config.baseURL
and re-reads OPENAI_BASE_URL from the environment; change the base URL selection
in the case "openai" branch so it prefers config.baseURL first, then falls back
to getEnvVar("OPENAI_BASE_URL"), then the hardcoded default (same approach used
in the anthropic branch). Update the OpenRouterProvider constructor call to pass
this computed base URL (refer to the case "openai" block, OpenRouterProvider,
requireEnvVar, getEnvVar, and config.baseURL).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b46c8e08-07e9-4148-9a30-c3e6e01123bb

📥 Commits

Reviewing files that changed from the base of the PR and between 4b354b7 and b387ae1.

📒 Files selected for processing (5)
  • src/config.ts
  • src/functions/summarize.ts
  • src/providers/index.ts
  • src/providers/openrouter.ts
  • src/types.ts

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.

1 participant