mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-15 11:43:10 +03:00
Compare commits
1 Commits
fix/10249-
...
fix/10096-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff6d465140 |
1
changelog.d/fixes/10096-kimi-coding-apikey-save.md
Normal file
1
changelog.d/fixes/10096-kimi-coding-apikey-save.md
Normal file
@@ -0,0 +1 @@
|
||||
- fix(dashboard): remap unified Kimi Code card API-key save to the admitted `kimi-coding-apikey` connection id, fixing 400 "Invalid provider" on Save (#10096)
|
||||
@@ -1 +0,0 @@
|
||||
- fix(open-sse): stop concurrent requests colliding on the same dedup hash for non-OpenAI target formats (#10249)
|
||||
@@ -36,19 +36,12 @@ const inflight = new Map<string, Promise<unknown>>();
|
||||
* Compute a deterministic hash for a request body.
|
||||
* Includes: model, messages, temperature, tools, tool_choice, max_tokens, response_format
|
||||
* Excludes: stream, user, metadata (don't affect LLM output)
|
||||
*
|
||||
* The prompt content can live under different keys depending on the target
|
||||
* provider format the body has already been translated to: OpenAI-style
|
||||
* bodies use `messages`, Gemini-translated bodies use `contents`, and
|
||||
* Responses-API-translated bodies use `input`. Falling back to only
|
||||
* `messages` made every non-OpenAI-format body hash the prompt as `null`,
|
||||
* colliding different prompts onto the same dedup hash (#10249).
|
||||
*/
|
||||
export function computeRequestHash(requestBody: unknown): string {
|
||||
const body = requestBody as Record<string, unknown>;
|
||||
const canonical = {
|
||||
model: body.model ?? null,
|
||||
messages: body.messages ?? body.contents ?? body.input ?? null,
|
||||
messages: body.messages ?? null,
|
||||
temperature: typeof body.temperature === "number" ? body.temperature : 1.0,
|
||||
tools: body.tools ?? null,
|
||||
tool_choice: body.tool_choice ?? null,
|
||||
|
||||
@@ -32,6 +32,19 @@ type UseApiKeySaveParams = {
|
||||
t: ProviderMessageTranslator;
|
||||
};
|
||||
|
||||
// Issue #10096: the unified Kimi Code dashboard card shares one page/providerId
|
||||
// ("kimi-coding") between OAuth and API-key auth. "kimi-coding" is an
|
||||
// OAuth-primary managed id and is NOT an admitted API-key/dual-auth connection
|
||||
// id (see isManagedProviderConnectionId in src/lib/providers/catalog.ts), so
|
||||
// posting it here 400s with "Invalid provider". The dedicated managed
|
||||
// API-key id "kimi-coding-apikey" IS admitted — remap only the POST payload
|
||||
// so the saved connection lands under the correct managed id. The OAuth flow
|
||||
// (handleOAuthSuccess in ProviderDetailPageClient.tsx) does not go through
|
||||
// this hook, so it keeps posting "kimi-coding" unchanged.
|
||||
export function resolveApiKeySaveProviderId(providerId: string): string {
|
||||
return providerId === "kimi-coding" ? "kimi-coding-apikey" : providerId;
|
||||
}
|
||||
|
||||
export function useApiKeySave({
|
||||
providerId,
|
||||
fetchConnections,
|
||||
@@ -48,7 +61,10 @@ export function useApiKeySave({
|
||||
const res = await fetch("/api/providers", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ provider: providerId, ...formData }),
|
||||
body: JSON.stringify({
|
||||
provider: resolveApiKeySaveProviderId(providerId),
|
||||
...formData,
|
||||
}),
|
||||
});
|
||||
if (res.ok) {
|
||||
const connectionData = await res.json();
|
||||
|
||||
37
tests/unit/bug-10096-kimi-coding-apikey-save.test.ts
Normal file
37
tests/unit/bug-10096-kimi-coding-apikey-save.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// Issue #10096: Kimi Code API key validates OK but Save returns 400 "Invalid provider".
|
||||
//
|
||||
// Root cause: the unified Kimi Code dashboard card's API-key branch posted
|
||||
// provider: "kimi-coding" (an OAuth-primary managed id, NOT an admitted
|
||||
// API-key connection id) to POST /api/providers, which the backend rejects.
|
||||
// The dedicated managed API-key id "kimi-coding-apikey" IS admitted.
|
||||
//
|
||||
// Fix: resolveApiKeySaveProviderId() in useApiKeySave.ts remaps the posted
|
||||
// provider id to "kimi-coding-apikey" for the API-key save flow only, while
|
||||
// the OAuth flow (which never calls this hook) keeps posting "kimi-coding".
|
||||
|
||||
const { isManagedProviderConnectionId } = await import("../../src/lib/providers/catalog.ts");
|
||||
const { resolveApiKeySaveProviderId } = await import(
|
||||
"../../src/app/(dashboard)/dashboard/providers/[id]/hooks/useApiKeySave.ts"
|
||||
);
|
||||
|
||||
test("Kimi Code API-key save flow remaps to the admitted managed API-key id", () => {
|
||||
assert.equal(
|
||||
resolveApiKeySaveProviderId("kimi-coding"),
|
||||
"kimi-coding-apikey",
|
||||
"the unified Kimi Code card's API-key save flow must post kimi-coding-apikey, not kimi-coding"
|
||||
);
|
||||
assert.equal(
|
||||
isManagedProviderConnectionId(resolveApiKeySaveProviderId("kimi-coding")),
|
||||
true,
|
||||
"the remapped id must be an admitted managed provider connection id (POST /api/providers accepts it)"
|
||||
);
|
||||
});
|
||||
|
||||
test("resolveApiKeySaveProviderId leaves every other provider id untouched", () => {
|
||||
assert.equal(resolveApiKeySaveProviderId("openai"), "openai");
|
||||
assert.equal(resolveApiKeySaveProviderId("kimi-coding-apikey"), "kimi-coding-apikey");
|
||||
assert.equal(resolveApiKeySaveProviderId("qoder"), "qoder");
|
||||
});
|
||||
@@ -1,91 +0,0 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { computeRequestHash, deduplicate, clearInflight } from "../../open-sse/services/requestDedup.ts";
|
||||
|
||||
// Regression tests for #10249: the dedup hash used to read only `body.messages`,
|
||||
// so translated (target-format) bodies that carry the prompt under a different
|
||||
// key (`contents` for Gemini, `input` for the Responses API) always hashed the
|
||||
// prompt as `null`. Concurrent requests with different prompts then collided on
|
||||
// the same dedup hash, joined the same in-flight promise, and the second caller
|
||||
// silently received the first caller's response.
|
||||
|
||||
test("Gemini-format translated bodies with different prompts must NOT collide on dedup hash", async () => {
|
||||
clearInflight();
|
||||
const bodyA = {
|
||||
contents: [{ role: "user", parts: [{ text: "Summarize the Q3 financial report attached." }] }],
|
||||
temperature: 0,
|
||||
};
|
||||
const bodyB = {
|
||||
contents: [{ role: "user", parts: [{ text: "Extract every invoice number from the attached PDF." }] }],
|
||||
temperature: 0,
|
||||
};
|
||||
const hashA = computeRequestHash({ ...bodyA, model: "gemini/gemini-2.5-flash", stream: false });
|
||||
const hashB = computeRequestHash({ ...bodyB, model: "gemini/gemini-2.5-flash", stream: false });
|
||||
assert.notEqual(hashA, hashB, "Different prompts must have different dedup hashes");
|
||||
|
||||
const [resA, resB] = await Promise.all([
|
||||
deduplicate(hashA, async () => "RESPONSE_A"),
|
||||
deduplicate(hashB, async () => "RESPONSE_B"),
|
||||
]);
|
||||
assert.equal(resA.result, "RESPONSE_A");
|
||||
assert.equal(resB.result, "RESPONSE_B");
|
||||
assert.equal(resB.wasDeduplicated, false);
|
||||
});
|
||||
|
||||
test("Responses-API input-format translated bodies with different prompts must NOT collide", async () => {
|
||||
clearInflight();
|
||||
const bodyA = {
|
||||
input: [{ role: "user", content: [{ type: "input_text", text: "What is the capital of France?" }] }],
|
||||
temperature: 0,
|
||||
};
|
||||
const bodyB = {
|
||||
input: [{ role: "user", content: [{ type: "input_text", text: "Explain quantum entanglement." }] }],
|
||||
temperature: 0,
|
||||
};
|
||||
const hashA = computeRequestHash({ ...bodyA, model: "openai/gpt-4.1", stream: false });
|
||||
const hashB = computeRequestHash({ ...bodyB, model: "openai/gpt-4.1", stream: false });
|
||||
assert.notEqual(hashA, hashB, "Different prompts must have different dedup hashes");
|
||||
|
||||
const [resA, resB] = await Promise.all([
|
||||
deduplicate(hashA, async () => "RESPONSE_A"),
|
||||
deduplicate(hashB, async () => "RESPONSE_B"),
|
||||
]);
|
||||
assert.equal(resA.result, "RESPONSE_A");
|
||||
assert.equal(resB.result, "RESPONSE_B");
|
||||
assert.equal(resB.wasDeduplicated, false);
|
||||
});
|
||||
|
||||
test("Sanity: OpenAI-format bodies with different prompts DO get distinct hashes (unchanged behavior)", () => {
|
||||
const bodyA = { messages: [{ role: "user", content: "Hello there" }], temperature: 0 };
|
||||
const bodyB = { messages: [{ role: "user", content: "Goodbye now" }], temperature: 0 };
|
||||
const hashA = computeRequestHash({ ...bodyA, model: "openai/gpt-4.1", stream: false });
|
||||
const hashB = computeRequestHash({ ...bodyB, model: "openai/gpt-4.1", stream: false });
|
||||
assert.notEqual(hashA, hashB);
|
||||
});
|
||||
|
||||
test("Genuinely identical requests still hash identically and get deduplicated (perf feature preserved)", async () => {
|
||||
clearInflight();
|
||||
const body = {
|
||||
contents: [{ role: "user", parts: [{ text: "Same prompt text every time" }] }],
|
||||
temperature: 0,
|
||||
};
|
||||
const hash1 = computeRequestHash({ ...body, model: "gemini/gemini-2.5-flash", stream: false });
|
||||
const hash2 = computeRequestHash({ ...body, model: "gemini/gemini-2.5-flash", stream: false });
|
||||
assert.equal(hash1, hash2, "Identical bodies must still produce the same hash");
|
||||
|
||||
let callCount = 0;
|
||||
const slowFn = async () => {
|
||||
callCount += 1;
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
return "SHARED_RESPONSE";
|
||||
};
|
||||
|
||||
const [resA, resB] = await Promise.all([
|
||||
deduplicate(hash1, slowFn),
|
||||
deduplicate(hash2, slowFn),
|
||||
]);
|
||||
assert.equal(resA.result, "SHARED_RESPONSE");
|
||||
assert.equal(resB.result, "SHARED_RESPONSE");
|
||||
assert.equal(callCount, 1, "Identical concurrent requests must share a single upstream call");
|
||||
assert.equal(resA.wasDeduplicated === true || resB.wasDeduplicated === true, true);
|
||||
});
|
||||
Reference in New Issue
Block a user