Skip to content

Add OpenRouter as a configurable AI provider with Gemini fallback#191

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/use-openrouter-api-keys
Draft

Add OpenRouter as a configurable AI provider with Gemini fallback#191
Copilot wants to merge 7 commits intomainfrom
copilot/use-openrouter-api-keys

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

chatActions.ts was hardwired to Gemini. Add OpenRouter as an alternative provider, selected automatically via environment variables.

Changes

app/actions/gemini.ts

  • Merged the OpenRouter logic into this file alongside the existing Gemini logic
  • A single exported generateContent function now handles both providers internally:
    • If OPENROUTER_API_KEY and OPENROUTER_MODEL are both set → calls OpenRouter via direct fetch to https://openrouter.ai/api/v1/chat/completions
    • Otherwise → calls Gemini as before (with the existing proxy fallback for unsupported regions)
  • OPENROUTER_MODEL accepts a semicolon-separated list of models (e.g. model-a;model-b;model-c); the list is passed as the models array to OpenRouter, which automatically falls back to the next model on error
  • Returns { text: string } consistently from both paths

app/actions/chatActions.ts

  • Simplified to a single import of generateContent from ./gemini; no local selector function needed

app/actions/openrouter.ts (removed)

  • Logic consolidated into gemini.ts

Set both OPENROUTER_API_KEY and OPENROUTER_MODEL to switch to OpenRouter; omit either to keep using Gemini unchanged.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for OpenRouter API keys and model Add OpenRouter as a configurable AI provider with Gemini fallback Mar 13, 2026
Copilot AI requested a review from na-trium-144 March 13, 2026 13:08
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 13, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
my-code f7185cd Commit Preview URL

Branch Preview URL
Mar 13 2026, 03:54 PM

@na-trium-144
Copy link
Contributor

@copilot openrouterのapiに直接fetchするのではなく、sdkライブラリを使って。

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 13, 2026

@copilot openrouterのapiに直接fetchするのではなく、sdkライブラリを使って。

Done in 0e190c7. openrouter.ts now uses the openai npm SDK with OpenRouter's OpenAI-compatible base URL (https://openrouter.ai/api/v1), replacing the manual fetch.

@na-trium-144
Copy link
Contributor

@copilot gemini.tsとopenrouter.tsの内容を1つのファイルの1つのgenerateContent関数にまとめて、環境変数で分岐するのもそこで行うようにしてほしい。

… function

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 13, 2026

@copilot gemini.tsとopenrouter.tsの内容を1つのファイルの1つのgenerateContent関数にまとめて、環境変数で分岐するのもそこで行うようにしてほ��...

Done in 9a28d39. openrouter.ts has been deleted and its logic merged into gemini.ts — there is now a single exported generateContent function that branches on OPENROUTER_API_KEY/OPENROUTER_MODEL internally (OpenRouter path when both are set, Gemini otherwise). chatActions.ts and questionExample.ts both import directly from ./gemini without any local wrapper.

@na-trium-144
Copy link
Contributor

@copilot 環境変数でセミコロン区切りとかでモデルを複数指定できるようにし、エラー時にフォールバックされるようにしてほしい。openrouterのapiはmodelsパラメーターにarrayを渡すと自動でフォールバックしてくれるようですが、openai sdkはそれに対応していない気がするのでsdkは使わず直接fetchするようにしてよいです。

…atic fallback

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 13, 2026

@copilot 環境変数でセミコロン区切りとかでモデルを複数指定できるようにし、エラー時にフォールバックされるようにしてほしい。openr...

Done in a259c8b. The OpenRouter path in gemini.ts now:

  • Parses OPENROUTER_MODEL by splitting on ; to get an array of models (e.g. model-a;model-b;model-c)
  • Uses direct fetch (no openai SDK) to POST to https://openrouter.ai/api/v1/chat/completions with the models array, which lets OpenRouter handle automatic fallback
  • Includes the response body in the error message for easier debugging

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • openrouter.ai
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

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.

2 participants