Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
229e927
feat(gateway): add provider retry with fallback
steebchen Feb 12, 2026
80852e8
test(gateway): add fallback behavior and error status code tests
steebchen Feb 12, 2026
0377ce9
fix(gateway): address PR review feedback for retry-with-fallback
steebchen Feb 12, 2026
b52b905
chore: sync with latest origin/main
steebchen Feb 12, 2026
15e9cf4
feat(ui): display routing and failed attempt info in log cards
steebchen Feb 12, 2026
6328f39
feat(gateway): include all routing attempts (succeeded + failed) in r…
steebchen Feb 12, 2026
9d4b967
chore: merge origin/main for unified reasoning config and Alibaba fix
steebchen Feb 12, 2026
f905c1a
feat: mark retried logs and link to successful log
steebchen Feb 12, 2026
f857226
chore(autofix): apply diff
steebchen Feb 12, 2026
51c6cba
chore: reset migrations to origin/main state
steebchen Feb 12, 2026
254d5d5
Merge remote-tracking branch 'origin/main' into gateway-retry-fallback
steebchen Feb 12, 2026
c67a85d
chore: sync with origin/main and regenerate migrations
steebchen Feb 12, 2026
bc31df6
Merge branch 'gateway-retry-fallback' of https://github.com/theopenco…
steebchen Feb 12, 2026
d01ce73
chore: deduplicate migration, keep remote version
steebchen Feb 12, 2026
41bf6ee
docs: add changelog entry and update routing docs for retry/fallback
steebchen Feb 12, 2026
a4ef782
Optimised images with calibre/image-actions
steebchen Feb 12, 2026
ecaee7f
fix: use per-attempt duration for failed retry logs
steebchen Feb 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,431 changes: 1,455 additions & 976 deletions apps/gateway/src/chat/chat.ts

Large diffs are not rendered by default.

363 changes: 363 additions & 0 deletions apps/gateway/src/chat/tools/resolve-provider-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,363 @@
import { HTTPException } from "hono/http-exception";

import {
findCustomProviderKey,
findProviderKey,
} from "@/lib/cached-queries.js";

import {
getProviderEndpoint,
getProviderHeaders,
prepareRequestBody,
} from "@llmgateway/actions";
import {
type BaseMessage,
hasMaxTokens,
type ModelDefinition,
type OpenAIRequestBody,
type OpenAIToolInput,
type Provider,
type ProviderModelMapping,
type ProviderRequestBody,
providers,
type ToolChoiceType,
type WebSearchTool,
} from "@llmgateway/models";

import { getProviderEnv } from "./get-provider-env.js";

import type { InferSelectModel, tables } from "@llmgateway/db";

export interface ProviderContext {
usedProvider: Provider;
usedModel: string;
usedModelFormatted: string;
usedModelMapping: string;
baseModelName: string;
usedToken: string;
providerKey: InferSelectModel<typeof tables.providerKey> | undefined;
configIndex: number;
envVarName: string | undefined;
url: string;
requestBody: ProviderRequestBody;
useResponsesApi: boolean;
requestCanBeCanceled: boolean;
isImageGeneration: boolean;
supportsReasoning: boolean;
temperature: number | undefined;
max_tokens: number | undefined;
top_p: number | undefined;
frequency_penalty: number | undefined;
presence_penalty: number | undefined;
headers: Record<string, string>;
}

export interface OriginalRequestParams {
temperature: number | undefined;
max_tokens: number | undefined;
top_p: number | undefined;
frequency_penalty: number | undefined;
presence_penalty: number | undefined;
}

export interface ProviderContextOptions {
stream: boolean;
effectiveStream: boolean;
messages: BaseMessage[];
response_format: OpenAIRequestBody["response_format"];
tools: OpenAIToolInput[] | undefined;
tool_choice: ToolChoiceType | undefined;
reasoning_effort: "minimal" | "low" | "medium" | "high" | undefined;
effort: "low" | "medium" | "high" | undefined;
webSearchTool: WebSearchTool | undefined;
image_config:
| {
aspect_ratio?: string;
image_size?: string;
n?: number;
seed?: number;
}
| undefined;
sensitive_word_check: { status: "DISABLE" | "ENABLE" } | undefined;
maxImageSizeMB: number;
userPlan: "free" | "pro" | "enterprise" | null;
hasExistingToolCalls: boolean;
customProviderName: string | undefined;
webSearchEnabled: boolean;
}

interface ProjectInfo {
mode: string;
organizationId: string;
}

interface OrgInfo {
id: string;
credits: string | null;
devPlan: string;
devPlanCreditsLimit: string | null;
devPlanCreditsUsed: string | null;
devPlanExpiresAt: Date | null;
}

/**
* Resolves all provider-dependent context needed to make a fetch request.
* This includes token resolution, URL building, parameter stripping,
* request body preparation, and header construction.
*
* Used by the retry loop to quickly set up a new provider context on fallback.
*/
export async function resolveProviderContext(
providerMapping: { providerId: string; modelName: string },
project: ProjectInfo,
organization: OrgInfo,
modelInfo: ModelDefinition,
originalParams: OriginalRequestParams,
options: ProviderContextOptions,
): Promise<ProviderContext> {
const usedProvider = providerMapping.providerId as Provider;
const usedModel = providerMapping.modelName;
const baseModelName = modelInfo.id || usedModel;
const usedModelMapping = usedModel;
const usedModelFormatted = `${usedProvider}/${baseModelName}`;

// --- Token resolution ---
let providerKey: InferSelectModel<typeof tables.providerKey> | undefined;
let usedToken: string | undefined;
let configIndex = 0;
let envVarName: string | undefined;

if (project.mode === "api-keys") {
if (usedProvider === "custom" && options.customProviderName) {
providerKey = await findCustomProviderKey(
project.organizationId,
options.customProviderName,
);
} else {
providerKey = await findProviderKey(project.organizationId, usedProvider);
}

if (!providerKey) {
throw new HTTPException(400, {
message: `No API key set for provider: ${usedProvider}`,
});
}

usedToken = providerKey.token;
} else if (project.mode === "credits") {
const envResult = getProviderEnv(usedProvider as Provider);
usedToken = envResult.token;
configIndex = envResult.configIndex;
envVarName = envResult.envVarName;
} else if (project.mode === "hybrid") {
if (usedProvider === "custom" && options.customProviderName) {
providerKey = await findCustomProviderKey(
project.organizationId,
options.customProviderName,
);
} else {
providerKey = await findProviderKey(project.organizationId, usedProvider);
}

if (providerKey) {
usedToken = providerKey.token;
} else {
const envResult = getProviderEnv(usedProvider as Provider);
usedToken = envResult.token;
configIndex = envResult.configIndex;
envVarName = envResult.envVarName;
}
Comment on lines +148 to +170

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Credits validation is skipped when falling back to env tokens.

In credits/hybrid mode, if no provider key exists, resolveProviderContext falls back to env tokens without the credit/dev-plan checks that the main flow enforces. Retries can therefore consume credits even when the balance is zero. Consider factoring the credit-availability check from chat.ts into a shared helper and applying it before using env tokens.

🤖 Prompt for AI Agents
In `@apps/gateway/src/chat/tools/resolve-provider-context.ts` around lines 147 -
169, resolveProviderContext currently falls back to env tokens (via
getProviderEnv) in credits/hybrid mode without performing the same credit/plan
validation used in the main provider path, allowing retries to consume credits
when balance is zero; extract the credit-availability check from chat.ts into a
shared helper (e.g., ensureSufficientCreditsOrPlan) and call it inside
resolveProviderContext before assigning usedToken from getProviderEnv in both
the credits branch and the hybrid fallback branch (the code paths around
findCustomProviderKey/findProviderKey and getProviderEnv) so env-token usage is
gated by the same validation.

}

if (!usedToken) {
throw new HTTPException(500, { message: "No token" });
}

// --- Look up the specific provider mapping for the selected provider ---
const providerMappingForSelected = modelInfo.providers.find(
(p) => p.providerId === usedProvider && p.modelName === usedModel,
);

// --- Check if model supports reasoning (from selected provider, not any) ---
const supportsReasoning =
(providerMappingForSelected as ProviderModelMapping)?.reasoning === true;

// --- Image generation check ---
const isImageGeneration =
(providerMappingForSelected as ProviderModelMapping)?.imageGenerations ===
true;

// --- URL resolution ---
const url = getProviderEndpoint(
usedProvider as Provider,
providerKey?.baseUrl || undefined,
usedModel,
usedProvider === "google-ai-studio" || usedProvider === "google-vertex"
? usedToken
: undefined,
options.stream,
supportsReasoning,
options.hasExistingToolCalls,
providerKey?.options || undefined,
configIndex,
isImageGeneration,
);
Comment on lines +201 to +205

Copilot AI Feb 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getProviderEndpoint(..., false) hard-codes imageGenerations to false. For image generation models this can produce the wrong base URL/endpoint (e.g., providers like Alibaba/ZAI switch URLs when imageGenerations is true), breaking retries for image models. Compute isImageGeneration before calling getProviderEndpoint and pass it through (and/or reuse providerMappingInfo.imageGenerations).

Copilot uses AI. Check for mistakes.

if (!url) {
throw new HTTPException(400, {
message: `No base URL set for provider: ${usedProvider}`,
});
}

const useResponsesApi = url.includes("/responses");

// --- Parameter stripping ---
// Work with copies of original params to avoid mutation
let temperature = originalParams.temperature;
let max_tokens = originalParams.max_tokens;
let top_p = originalParams.top_p;
let frequency_penalty = originalParams.frequency_penalty;
let presence_penalty = originalParams.presence_penalty;

if (providerMappingForSelected) {
const supported = (providerMappingForSelected as ProviderModelMapping)
.supportedParameters;
if (supported && supported.length > 0) {
if (temperature !== undefined && !supported.includes("temperature")) {
temperature = undefined;
}
if (top_p !== undefined && !supported.includes("top_p")) {
top_p = undefined;
}
if (
frequency_penalty !== undefined &&
!supported.includes("frequency_penalty")
) {
frequency_penalty = undefined;
}
if (
presence_penalty !== undefined &&
!supported.includes("presence_penalty")
) {
presence_penalty = undefined;
}
if (max_tokens !== undefined && !supported.includes("max_tokens")) {
max_tokens = undefined;
}
}
}

// Anthropic does not allow temperature and top_p simultaneously
if (usedProvider === "anthropic") {
if (temperature !== undefined && top_p !== undefined) {
top_p = undefined;
}
}

// --- max_tokens validation ---
if (max_tokens !== undefined && providerMappingForSelected) {
if (
"maxOutput" in providerMappingForSelected &&
providerMappingForSelected.maxOutput !== undefined
) {
if (max_tokens > providerMappingForSelected.maxOutput) {
// Silently cap to max output instead of throwing on retry
max_tokens = providerMappingForSelected.maxOutput;
}
}
}

// --- requestCanBeCanceled ---
const requestCanBeCanceled =
providers.find((p) => p.id === usedProvider)?.cancellation === true;

// --- Request body preparation ---
const requestBody: ProviderRequestBody = await prepareRequestBody(
usedProvider as Provider,
usedModel,
options.messages as BaseMessage[],
options.effectiveStream,
temperature,
max_tokens,
top_p,
frequency_penalty,
presence_penalty,
options.response_format,
options.tools,
options.tool_choice,
options.reasoning_effort,
supportsReasoning,
process.env.NODE_ENV === "production",
options.maxImageSizeMB,
options.userPlan,
options.sensitive_word_check,
options.image_config,
options.effort,
isImageGeneration,
options.webSearchTool,
useResponsesApi,
);

// Post-validation of max_tokens in request body
if (
hasMaxTokens(requestBody) &&
requestBody.max_tokens !== undefined &&
providerMappingForSelected
) {
if (
"maxOutput" in providerMappingForSelected &&
providerMappingForSelected.maxOutput !== undefined
) {
if (requestBody.max_tokens > providerMappingForSelected.maxOutput) {
requestBody.max_tokens = providerMappingForSelected.maxOutput;
}
}
}

// --- Headers ---
const headers = getProviderHeaders(usedProvider as Provider, usedToken, {
webSearchEnabled: options.webSearchEnabled,
});
headers["Content-Type"] = "application/json";

if (usedProvider === "anthropic" && options.effort !== undefined) {
const currentBeta = headers["anthropic-beta"];
headers["anthropic-beta"] = currentBeta
? `${currentBeta},effort-2025-11-24`
: "effort-2025-11-24";
}

if (
usedProvider === "anthropic" &&
options.response_format?.type === "json_schema"
) {
const currentBeta = headers["anthropic-beta"];
headers["anthropic-beta"] = currentBeta
? `${currentBeta},structured-outputs-2025-11-13`
: "structured-outputs-2025-11-13";
}

return {
usedProvider,
usedModel,
usedModelFormatted,
usedModelMapping,
baseModelName,
usedToken,
providerKey,
configIndex,
envVarName,
url,
requestBody,
useResponsesApi,
requestCanBeCanceled,
isImageGeneration,
supportsReasoning,
temperature,
max_tokens,
top_p,
frequency_penalty,
presence_penalty,
headers,
};
}
Loading
Loading