mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-13 04:59:51 +08:00
Every fact about a provider lives in one record in lib/providers.ts (SDK kind, credential kind, env var prefix, default URL, picker group, four-language notes, and a short model list for the six providers that cannot list models). The thirteen tables keyed by provider name are gone. resolveEndpoint decides whose key a call uses and where it goes; usesServerCredentials reads its answer instead of mirroring getAIModel's branches; serverCredentialError holds the server credential check once. The UI, the connection test and the routes read the record's credential and sdk fields instead of comparing provider names (44 such checks before, 6 after, in the admin panel's fixed-variable branches). Intended behavior changes: providers that can list their models have no hand-written list; the settings dialog fetches the live list in the background once credentials are entered for a provider without models and offers it behind "Browse models". Azure has no placeholder default URL (old entries that hold it still count as unset); the base URL field of Azure and of the new "custom" OpenAI-compatible preset (CUSTOM_API_KEY, CUSTOM_BASE_URL) is labelled required. The connection test answers 400 for an unknown provider name; prototype names are refused everywhere. Provider lists in error messages and the admin "add provider" dropdown follow the picker's order. Also fixed, from before the refactor: the redirect guard drops x-access-code on cross-origin redirects, and edgeOneEndpoint accepts the Origin header only when it matches Host or X-Forwarded-Host. Verification: a behavior snapshot of getAIModel's SDK factory calls and the quota functions over 25 providers, 22 client override shapes and 9 server environments (5843 rows), recorded on the old code and unchanged apart from the intended changes; 752 unit tests, 65 provider e2e tests, tsc, biome, next build. Reviewed by five models from one brief; their findings and the resulting fixes are recorded in docs/plans/2026-10-provider-refactor.md.
154 lines
5.2 KiB
TypeScript
154 lines
5.2 KiB
TypeScript
// @vitest-environment node
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
|
|
vi.mock("@/lib/dynamo-quota-manager", () => ({
|
|
isQuotaEnabled: () => false,
|
|
checkAndIncrementRequest: async () => ({ allowed: true }),
|
|
recordTokenUsage: async () => {},
|
|
}))
|
|
|
|
import { POST as chat } from "@/app/api/chat/route"
|
|
|
|
const calls: Array<{ url: string; headers: Headers }> = []
|
|
|
|
beforeEach(() => {
|
|
vi.stubEnv("AI_PROVIDER", undefined)
|
|
vi.stubEnv("AI_MODEL", undefined)
|
|
calls.length = 0
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async (url: string, init: RequestInit) => {
|
|
calls.push({ url: String(url), headers: new Headers(init.headers) })
|
|
throw new Error("no network in tests")
|
|
}),
|
|
)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
describe("EdgeOne as a server model", () => {
|
|
it("calls the site's Edge AI function with the cookies", async () => {
|
|
// An admin panel or ai-models.json provider: the client sends the
|
|
// provider name's slug, not "edgeone"
|
|
vi.stubEnv(
|
|
"AI_MODELS_CONFIG",
|
|
JSON.stringify({
|
|
providers: [
|
|
{
|
|
name: "Edge Pages",
|
|
provider: "edgeone",
|
|
models: ["@tx/deepseek-ai/deepseek-v3-0324"],
|
|
},
|
|
],
|
|
}),
|
|
)
|
|
const res = await chat(
|
|
new Request("http://localhost/api/chat", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"x-ai-provider": "edge-pages",
|
|
"x-selected-model-id":
|
|
"server:edge-pages:@tx/deepseek-ai/deepseek-v3-0324",
|
|
cookie: "eo_token=t; eo_time=1",
|
|
},
|
|
body: JSON.stringify({
|
|
messages: [
|
|
{
|
|
id: "u1",
|
|
role: "user",
|
|
parts: [{ type: "text", text: "Draw two boxes" }],
|
|
},
|
|
],
|
|
xml: "",
|
|
}),
|
|
}),
|
|
)
|
|
await res.text()
|
|
expect(calls[0]?.url).toBe(
|
|
"http://localhost/api/edgeai/chat/completions",
|
|
)
|
|
expect(calls[0]?.headers.get("cookie")).toBe("eo_token=t; eo_time=1")
|
|
})
|
|
})
|
|
|
|
const send = (headers: Record<string, string>) =>
|
|
chat(
|
|
new Request("http://localhost/api/chat", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", ...headers },
|
|
body: JSON.stringify({
|
|
messages: [
|
|
{
|
|
id: "u1",
|
|
role: "user",
|
|
parts: [{ type: "text", text: "Draw two boxes" }],
|
|
},
|
|
],
|
|
xml: "",
|
|
}),
|
|
}),
|
|
).then((r) => r.text())
|
|
|
|
describe("EdgeOne endpoints", () => {
|
|
it("works when the deployment names EdgeOne only in AI_PROVIDER", async () => {
|
|
vi.stubEnv("AI_PROVIDER", "edgeone")
|
|
vi.stubEnv("AI_MODEL", "@tx/deepseek-ai/deepseek-v3-0324")
|
|
await send({})
|
|
expect(calls[0]?.url).toBe(
|
|
"http://localhost/api/edgeai/chat/completions",
|
|
)
|
|
})
|
|
|
|
it("always calls the site's own function, whatever URL the request names", async () => {
|
|
// Another host would get the user's EdgeOne cookies
|
|
await send({
|
|
"x-ai-provider": "edgeone",
|
|
"x-ai-model": "@tx/deepseek-ai/deepseek-v3-0324",
|
|
"x-ai-base-url": "https://elsewhere.example/api/edgeai",
|
|
cookie: "eo_token=t",
|
|
})
|
|
expect(calls[0]?.url).toBe(
|
|
"http://localhost/api/edgeai/chat/completions",
|
|
)
|
|
})
|
|
|
|
it("calls the function at the site root, also with a base path", async () => {
|
|
vi.stubEnv("NEXT_PUBLIC_BASE_PATH", "/draw")
|
|
await send({
|
|
"x-ai-provider": "edgeone",
|
|
"x-ai-model": "@tx/deepseek-ai/deepseek-v3-0324",
|
|
})
|
|
expect(calls[0]?.url).toBe(
|
|
"http://localhost/api/edgeai/chat/completions",
|
|
)
|
|
})
|
|
})
|
|
|
|
describe("the EdgeOne endpoint's origin", () => {
|
|
it("takes the browser's Origin for the host the request was addressed to", async () => {
|
|
vi.stubEnv("AI_PROVIDER", "edgeone")
|
|
vi.stubEnv("AI_MODEL", "@tx/deepseek-ai/deepseek-v3-0324")
|
|
// Behind a proxy the request URL is internal; Origin names the site
|
|
await send({ origin: "https://draw.example", host: "draw.example" })
|
|
expect(calls[0]?.url).toBe(
|
|
"https://draw.example/api/edgeai/chat/completions",
|
|
)
|
|
})
|
|
|
|
it("ignores another site's Origin, so the cookies stay on this site", async () => {
|
|
vi.stubEnv("AI_PROVIDER", "edgeone")
|
|
vi.stubEnv("AI_MODEL", "@tx/deepseek-ai/deepseek-v3-0324")
|
|
await send({
|
|
origin: "https://attacker.example",
|
|
host: "localhost",
|
|
cookie: "eo_token=t",
|
|
})
|
|
expect(calls[0]?.url).toBe(
|
|
"http://localhost/api/edgeai/chat/completions",
|
|
)
|
|
})
|
|
})
|