Repository navigation
fix(onboarding): unbreak the first API call #3524
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import { app } from "@/index.js"; | ||
| import { createTestUser, deleteAll } from "@/testing.js"; | ||
|
|
||
| import { db, eq, tables } from "@llmgateway/db"; | ||
| import { ONBOARDING_MAX_TOKENS, ONBOARDING_MODEL } from "@llmgateway/shared"; | ||
|
|
||
| // The onboarding wizard's "try it now" request runs through this proxy: the | ||
| // gateway refuses cross-origin browser calls, so the UI posts here with its | ||
| // session cookie and the server forwards to the gateway. A brand new | ||
| // organization has no credits, so that first call is sponsored by a | ||
| // platform-owned key — which means the endpoint has to stay session-gated (the | ||
| // UI must send its cookie), and the sponsored path has to pin what it spends | ||
| // rather than trusting the request body. | ||
|
|
||
| describe("chat completion proxy", () => { | ||
| let token: string; | ||
| let gatewayRequests: { | ||
| url: string; | ||
| authorization: string | null; | ||
| body: Record<string, unknown>; | ||
| }[] = []; | ||
|
|
||
| beforeEach(async () => { | ||
| token = await createTestUser(); | ||
| gatewayRequests = []; | ||
| process.env.ONBOARDING_CHAT_API_KEY = "platform-onboarding-key"; | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
| vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { | ||
| const url = | ||
| typeof input === "string" | ||
| ? input | ||
| : input instanceof URL | ||
| ? input.toString() | ||
| : input.url; | ||
|
|
||
| if (!url.includes("/chat/completions")) { | ||
| return await originalFetch(input, init); | ||
| } | ||
|
|
||
| gatewayRequests.push({ | ||
| url, | ||
| authorization: new Headers(init?.headers).get("authorization"), | ||
| body: JSON.parse(String(init?.body ?? "{}")), | ||
| }); | ||
|
|
||
| return new Response( | ||
| JSON.stringify({ | ||
| id: "chatcmpl-proxy", | ||
| model: ONBOARDING_MODEL, | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { role: "assistant", content: "Hello from the gateway" }, | ||
| finish_reason: "stop", | ||
| }, | ||
| ], | ||
| }), | ||
| { status: 200, headers: { "Content-Type": "application/json" } }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| vi.restoreAllMocks(); | ||
| delete process.env.ONBOARDING_CHAT_API_KEY; | ||
| await deleteAll(); | ||
| }); | ||
|
|
||
| function request(body: Record<string, unknown>, cookie?: string) { | ||
| return app.request("/chat/completion", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| ...(cookie ? { Cookie: cookie } : {}), | ||
| }, | ||
| body: JSON.stringify({ | ||
| messages: [{ role: "user", content: "Hello!" }], | ||
| ...body, | ||
| }), | ||
| }); | ||
| } | ||
|
|
||
| test("rejects an unauthenticated request", async () => { | ||
| const res = await request({ model: "auto", apiKey: "test-token" }); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| expect(gatewayRequests).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("sponsors the first onboarding call with the platform key", async () => { | ||
| const res = await request( | ||
| { model: ONBOARDING_MODEL, apiKey: "user-token", onboarding: true }, | ||
| token, | ||
| ); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(gatewayRequests).toHaveLength(1); | ||
| expect(gatewayRequests[0].url).toContain("/v1/chat/completions"); | ||
| expect(gatewayRequests[0].authorization).toBe( | ||
| "Bearer platform-onboarding-key", | ||
| ); | ||
| expect(gatewayRequests[0].body).toMatchObject({ | ||
| model: ONBOARDING_MODEL, | ||
| onboarding: true, | ||
| max_tokens: ONBOARDING_MAX_TOKENS, | ||
| }); | ||
| }); | ||
|
|
||
| test("pins the sponsored model instead of trusting the request", async () => { | ||
| await request( | ||
| { model: "gpt-4o", apiKey: "user-token", onboarding: true }, | ||
| token, | ||
| ); | ||
|
|
||
| expect(gatewayRequests[0].body.model).toBe(ONBOARDING_MODEL); | ||
| }); | ||
|
|
||
| test("stops sponsoring once onboarding is complete", async () => { | ||
| await db | ||
| .update(tables.user) | ||
| .set({ onboardingCompleted: true }) | ||
| .where(eq(tables.user.id, "test-user-id")); | ||
|
|
||
| await request( | ||
| { model: ONBOARDING_MODEL, apiKey: "user-token", onboarding: true }, | ||
| token, | ||
| ); | ||
|
|
||
| expect(gatewayRequests[0].authorization).toBe("Bearer user-token"); | ||
| expect(gatewayRequests[0].body).not.toHaveProperty("max_tokens"); | ||
| }); | ||
|
|
||
| test("falls back to the caller's key when no platform key is configured", async () => { | ||
| delete process.env.ONBOARDING_CHAT_API_KEY; | ||
|
|
||
| await request( | ||
| { model: ONBOARDING_MODEL, apiKey: "user-token", onboarding: true }, | ||
| token, | ||
| ); | ||
|
|
||
| expect(gatewayRequests[0].authorization).toBe("Bearer user-token"); | ||
| }); | ||
|
|
||
| test("leaves non-onboarding requests on the caller's key", async () => { | ||
| await request({ model: "gpt-4o", apiKey: "user-token" }, token); | ||
|
|
||
| expect(gatewayRequests[0].authorization).toBe("Bearer user-token"); | ||
| expect(gatewayRequests[0].body.model).toBe("gpt-4o"); | ||
| expect(gatewayRequests[0].body).not.toHaveProperty("onboarding"); | ||
| }); | ||
| }); | ||
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,40 @@ | ||
| import { afterEach, describe, expect, test } from "vitest"; | ||
|
|
||
| import { getGatewayUrl } from "@/utils/playground-key.js"; | ||
|
|
||
| describe("getGatewayUrl", () => { | ||
| const previousGatewayUrl = process.env.GATEWAY_URL; | ||
| const previousNodeEnv = process.env.NODE_ENV; | ||
|
|
||
| afterEach(() => { | ||
| if (previousGatewayUrl === undefined) { | ||
| delete process.env.GATEWAY_URL; | ||
| } else { | ||
| process.env.GATEWAY_URL = previousGatewayUrl; | ||
| } | ||
| process.env.NODE_ENV = previousNodeEnv; | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Deployments and local `.envrc` blocks write GATEWAY_URL both ways, and | ||
| // every caller appends a `/v1` path to what this returns. | ||
| test("appends the /v1 suffix when GATEWAY_URL omits it", () => { | ||
| process.env.GATEWAY_URL = "http://localhost:4001"; | ||
| expect(getGatewayUrl()).toBe("http://localhost:4001/v1"); | ||
| }); | ||
|
|
||
| test("keeps a single /v1 suffix when GATEWAY_URL already has it", () => { | ||
| process.env.GATEWAY_URL = "https://api.llmgateway.io/v1"; | ||
| expect(getGatewayUrl()).toBe("https://api.llmgateway.io/v1"); | ||
| }); | ||
|
|
||
| test("tolerates a trailing slash", () => { | ||
| process.env.GATEWAY_URL = "https://api.llmgateway.io/v1/"; | ||
| expect(getGatewayUrl()).toBe("https://api.llmgateway.io/v1"); | ||
| }); | ||
|
|
||
| test("falls back to the local gateway in development", () => { | ||
| delete process.env.GATEWAY_URL; | ||
| process.env.NODE_ENV = "development"; | ||
| expect(getGatewayUrl()).toBe("http://localhost:4001/v1"); | ||
| }); | ||
| }); | ||
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 |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
|
|
||
| # testing | ||
| /coverage | ||
| /test-results | ||
| /playwright-report | ||
|
|
||
| # next.js | ||
| /.next/ | ||
|
|
||
Oops, something went wrong.
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.