-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat(gateway): add Merge Gateway provider #2152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MatthewFeroz
wants to merge
1
commit into
Twigpine:main
Choose a base branch
from
MatthewFeroz:feat/merge-gateway-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Merge Gateway setup | ||
|
|
||
| OpenClaude connects to [Merge Gateway](https://gateway.merge.dev) through its OpenAI-compatible endpoint at `https://api-gateway.merge.dev/v1/openai`. The route supports Merge's multi-provider model catalog and routing policies with a dedicated `MERGE_GATEWAY_API_KEY`. | ||
|
|
||
| ## Guided setup | ||
|
|
||
| 1. Create an API key in the [Merge Gateway dashboard](https://gateway.merge.dev/api-keys). | ||
| 2. Start OpenClaude and run `/provider`. | ||
| 3. Choose **Add provider**, then **Merge Gateway**. | ||
| 4. Paste the API key when prompted. | ||
| 5. Choose a discovered model, or select **Default routing policy** to let the policy attached to the key choose the provider and model. | ||
| 6. Choose **Chat Completions** or **Responses** for the API format supported by the selected model and route. | ||
|
|
||
| The model picker keeps a cached catalog for one day and can be refreshed manually. If discovery is temporarily unavailable, the routing-policy and GPT-5.5 fallback entries remain selectable. | ||
|
|
||
| ## CLI setup | ||
|
|
||
| ```bash | ||
| export MERGE_GATEWAY_API_KEY="your-api-key" | ||
| openclaude --provider merge-gateway --model openai/gpt-5.5 | ||
| ``` | ||
|
|
||
| To delegate model selection to a Merge Gateway routing policy: | ||
|
|
||
| ```bash | ||
| openclaude --provider merge-gateway --model default_routing | ||
| ``` | ||
|
|
||
| The dedicated Merge credential is not replaced by `OPENAI_API_KEY`. | ||
|
|
||
| ## Verify | ||
|
|
||
| Inside OpenClaude: | ||
|
|
||
| 1. Run `/status` and confirm the active provider is **Merge Gateway** and the base URL is `https://api-gateway.merge.dev/v1/openai`. | ||
| 2. Run `/model`, refresh the catalog, and confirm Merge Gateway models appear. | ||
| 3. Send a short prompt and confirm the request appears in the [Gateway dashboard](https://gateway.merge.dev). | ||
|
|
||
| For direct catalog troubleshooting, use the authenticated public model endpoint: | ||
|
|
||
| ```bash | ||
| curl --fail --silent --show-error \ | ||
| -H "Authorization: Bearer $MERGE_GATEWAY_API_KEY" \ | ||
| "https://api-gateway.merge.dev/v1/models?limit=5" | ||
| ``` | ||
|
|
||
| Never commit the API key or paste it into issue or pull-request text. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { describe, expect, mock, test } from 'bun:test' | ||
|
|
||
| import '../index.js' | ||
| import { | ||
| getRouteCredentialEnvVars, | ||
| resolveActiveRouteIdFromEnv, | ||
| } from '../routeMetadata.js' | ||
| import { applyProviderFlag } from '../../utils/providerFlag.js' | ||
| import { createOpenAIShimClient } from '../../services/api/openaiShim.js' | ||
| import { | ||
| acquireSharedMutationLock, | ||
| releaseSharedMutationLock, | ||
| } from '../../test/sharedMutationLock.js' | ||
| import { asMockFetch } from '../../test/typedMocks.js' | ||
| import mergeGateway from './merge-gateway.js' | ||
|
|
||
| const mapModel = mergeGateway.catalog?.discovery?.mapModel | ||
| type OpenAIShimClient = { | ||
| beta: { | ||
| messages: { | ||
| create: (params: Record<string, unknown>) => Promise<unknown> | ||
| } | ||
| } | ||
| } | ||
|
|
||
| describe('Merge Gateway', () => { | ||
| test('uses a dedicated OpenAI-compatible gateway route', () => { | ||
| expect(mergeGateway.id).toBe('merge-gateway') | ||
| expect(mergeGateway.defaultBaseUrl).toBe( | ||
| 'https://api-gateway.merge.dev/v1/openai', | ||
| ) | ||
| expect(mergeGateway.setup.credentialEnvVars).toEqual([ | ||
| 'MERGE_GATEWAY_API_KEY', | ||
| ]) | ||
| expect(mergeGateway.setup.dedicatedCredentialsOnly).toBe(true) | ||
| expect(mergeGateway.transportConfig.kind).toBe('openai-compatible') | ||
| expect( | ||
| mergeGateway.transportConfig.openaiShim?.supportsApiFormatSelection, | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| test('keeps routing policy and concrete model fallbacks in the catalog', () => { | ||
| expect(mergeGateway.catalog?.source).toBe('hybrid') | ||
| expect(mergeGateway.catalog?.discovery?.requiresAuth).toBe(true) | ||
| expect(mergeGateway.catalog?.models?.map(model => model.apiName)).toEqual([ | ||
| 'default_routing', | ||
| 'openai/gpt-5.5', | ||
| ]) | ||
| }) | ||
|
|
||
| test('--provider selects the route and uses only its dedicated credential', async () => { | ||
| await acquireSharedMutationLock('merge-gateway.test.ts') | ||
| expect(getRouteCredentialEnvVars('merge-gateway')).toEqual([ | ||
| 'MERGE_GATEWAY_API_KEY', | ||
| ]) | ||
|
|
||
| const previousEnv = { ...process.env } | ||
| const originalFetch = globalThis.fetch | ||
| try { | ||
| process.env.MERGE_GATEWAY_API_KEY = 'merge-key' | ||
| process.env.OPENAI_API_KEY = 'unrelated-openai-key' | ||
| delete process.env.OPENAI_BASE_URL | ||
|
|
||
| expect( | ||
| applyProviderFlag('merge-gateway', [ | ||
| '--provider', | ||
| 'merge-gateway', | ||
| '--model', | ||
| 'default_routing', | ||
| ]), | ||
| ).toEqual({}) | ||
| expect(String(process.env.OPENAI_BASE_URL)).toBe( | ||
| 'https://api-gateway.merge.dev/v1/openai', | ||
| ) | ||
| expect(String(process.env.OPENAI_MODEL)).toBe('default_routing') | ||
| expect(resolveActiveRouteIdFromEnv(process.env)).toBe('merge-gateway') | ||
|
|
||
| let authorization: string | null = null | ||
| globalThis.fetch = asMockFetch(mock((_input, init) => { | ||
| authorization = new Headers(init?.headers).get('authorization') | ||
| return Promise.resolve( | ||
| new Response( | ||
| JSON.stringify({ | ||
| id: 'chatcmpl-merge', | ||
| model: 'default_routing', | ||
| choices: [ | ||
| { | ||
| message: { role: 'assistant', content: 'ok' }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| }), | ||
| { headers: { 'Content-Type': 'application/json' } }, | ||
| ), | ||
| ) | ||
| })) | ||
|
|
||
| const client = createOpenAIShimClient({}) as OpenAIShimClient | ||
| await client.beta.messages.create({ | ||
| model: 'default_routing', | ||
| messages: [{ role: 'user', content: 'hello' }], | ||
| max_tokens: 32, | ||
| stream: false, | ||
| }) | ||
|
|
||
| expect<string | null>(authorization).toBe('Bearer merge-key') | ||
| } finally { | ||
| mock.restore() | ||
| globalThis.fetch = originalFetch | ||
| for (const name of Object.keys(process.env)) { | ||
| if (!(name in previousEnv)) delete process.env[name] | ||
| } | ||
| Object.assign(process.env, previousEnv) | ||
| releaseSharedMutationLock() | ||
| } | ||
| }) | ||
|
|
||
| test('maps native and OpenAI-compatible model list shapes', () => { | ||
| if (!mapModel) throw new Error('mapModel missing') | ||
|
|
||
| expect( | ||
| mapModel({ | ||
| model: 'anthropic/claude-sonnet-5', | ||
| display_name: 'Claude Sonnet 5', | ||
| }), | ||
| ).toEqual({ | ||
| id: 'merge-gateway-anthropic/claude-sonnet-5', | ||
| apiName: 'anthropic/claude-sonnet-5', | ||
| label: 'Claude Sonnet 5 (via Merge Gateway)', | ||
| }) | ||
| expect(mapModel({ id: 'openai/gpt-5.5' })).toEqual({ | ||
| id: 'merge-gateway-openai/gpt-5.5', | ||
| apiName: 'openai/gpt-5.5', | ||
| label: 'openai/gpt-5.5 (via Merge Gateway)', | ||
| }) | ||
| expect(mapModel({})).toBeNull() | ||
| expect(mapModel(null)).toBeNull() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.