feat: add The Grid as an ai-proxy provider - #13920
Open
CastilloLuis wants to merge 1 commit into
Open
CastilloLuis wants to merge 1 commit into
CastilloLuis wants to merge 1 commit into
Conversation
The Grid is an OpenAI-compatible inference API. Add it as a provider module for ai-proxy, ai-proxy-multi and ai-request-rewrite, covering the openai-chat and openai-responses capabilities, and map the plugin-level max_tokens override onto max_completion_tokens and max_output_tokens respectively, matching the openai provider. The Grid's Consumption API validates a request and then answers with a 307 to its routing layer on the same host, where inference is fulfilled. Returning that redirect downstream would leave the Plugin blind to the real response, so ai-rate-limiting would meter zero tokens and ai-proxy-multi would never fall back. Add opt-in redirect following to the HTTP transport for this: a provider sets follow_redirects, and one 307/308 hop is replayed with the method, body and headers preserved. Only same-origin redirects are followed, since a cross-origin hop would replay the provider credentials against a host named by the response rather than by the configuration. Signed-off-by: CastilloLuis <luisedcastillog@gmail.com>
Author
|
A note on verification, since I said above that I could not run Test::Nginx locally. To avoid leaving the transport change resting only on
That harness is a local scratch tool, not something I have added to the PR — the committed coverage is |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Adds The Grid as an
ai-proxyprovider, and adds the opt-in redirect following in the HTTP transport that it needs in order to work through the gateway.The Grid is an OpenAI-compatible inference API whose model ids are market instruments rather than fixed models: a task type and a quality tier (
text-standard,code-prime,agent-max), or a lab-scoped market (claude-opus-latest,kimi-latest). It serves both/v1/chat/completionsand/v1/responses, so it maps onto the existingopenai-chatandopenai-responsescapabilities with no new protocol.apisix/plugins/ai-providers/thegrid.luamirrorsopenai.lua, including themax_tokensoverride mapping — verified against the live API rather than assumed:/v1/chat/completionsmax_completion_tokens: 64200,usage.completion_tokens = 64/v1/responsesmax_output_tokens: 64200,usage.output_tokens = 64,status: incompleteThe routing redirect
The Grid's Consumption API validates a request and then answers
307with aLocationpointing at its routing layer on the same host (api.thegrid.ai/v1/...→api.thegrid.ai/r/v1/...), where inference is fulfilled. It is documented and intentional, not an error condition.lua-resty-httpdoes not follow redirects, so without this changeai-proxywould return the307downstream and:ai-rate-limitingwould meter zero tokens on every request, since the gateway never sees a body carryingusage;ai-proxy-multifallback would never trigger, because every request looks successful;LocationURL is self-authenticating — I confirmed it returns200when replayed with noAuthorizationheader — so the gateway would be turning its configured provider credentials into a bearer URL handed to the caller.So the hop is followed inside the transport instead:
follow_redirects = true. Every existing provider is untouched and keeps today's behaviour.307/308only.301/302/303are deliberately excluded — they permit rewriting the method toGET, which would silently drop the prompt.auth.queryproviders stay authenticated when it does not.Authorizationagainst a host named by the response rather than by the configuration, so it is refused and surfaces as a request error.I am happy to split the transport change into its own PR if you would rather review it separately — the provider is not functional without it, which is why they are together here.
Tests
t/plugin/ai-proxy-thegrid.tcovers the provider through bothai-proxyandai-proxy-multi, rejection of an unknown provider name, the same-origin redirect being followed through to the model output, and the cross-origin redirect being refused. The mock endpoints added tot/lib/server.luaassert that the replay preserved the method, the body, the target query string and the request headers, so a regression that drops any of them fails rather than passing quietly.I could not run the Test::Nginx suite locally on macOS; it will run here in CI. Locally I verified
luacheckis clean on every changed Lua file and that all of them compile.Which issue(s) this PR fixes:
Fixes #13919
Checklist