Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
"use client";
import { useState, useEffect } from "react";
import { useTranslations } from "next-intl";
import {
AI_PROVIDERS,
CLAUDE_CODE_COMPATIBLE_PREFIX,
OPENAI_COMPATIBLE_PREFIX,
ANTHROPIC_COMPATIBLE_PREFIX,
} from "@/shared/constants/providers";
import { compareTr } from "@/shared/utils/turkishText";
/**
* Hook to fetch and manage provider options for the Translator tools.
* Fetches active providers from the API and builds a sorted list of options.
* Falls back to the static AI_PROVIDERS list if the API is unreachable.
*
* @param {string} [initialProvider="openai"] - Initial provider value
* @returns {{ provider: string, setProvider: Function, providerOptions: Array<{value: string, label: string}>, loading: boolean }}
*/
export function useProviderOptions(initialProvider = "openai") {
const t = useTranslations("translator");
const [provider, setProvider] = useState(initialProvider);
const [providerOptions, setProviderOptions] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchProviders = async () => {
try {
const [connRes, nodesRes] = await Promise.all([
fetch("/api/providers"),
fetch("/api/provider-nodes"),
]);
const [connData, nodesData] = await Promise.all([connRes.json(), nodesRes.json()]);
const nodeMap = new Map((nodesData.nodes || []).map((n) => [n.id, n]));
const activeProviders = new Set(
(connData.connections || []).filter((c) => c.isActive !== false).map((c) => c.provider)
);
const options = [...activeProviders]
.map((pid) => {
const info = (AI_PROVIDERS as any)[pid as string];
const node: any = nodeMap.get(pid);
let label = info?.name || node?.name || pid;
if (!info && (pid as string).startsWith(CLAUDE_CODE_COMPATIBLE_PREFIX))
label = node?.name || "CC Compatible";
if (!info && (pid as string).startsWith(OPENAI_COMPATIBLE_PREFIX))
label = node?.name || t("openaiCompatibleLabel");
if (!info && (pid as string).startsWith(ANTHROPIC_COMPATIBLE_PREFIX))
label = node?.name || t("anthropicCompatibleLabel");
// #3505: compatible providers emit catalog models under the node's custom prefix
// (e.g. "myprefix/gpt-4o"), not under the connection id (`value`). Expose the prefix
// so model-filter consumers (playground) can match; `value` stays the id for
// connection lookups (translator send/translate, ApiTab).
const modelPrefix = !info && node?.prefix ? String(node.prefix) : undefined;
return { value: pid, label, modelPrefix };
})
.sort((a, b) => compareTr(a.label, b.label));
const nextOptions =
options.length > 0
? options
: Object.entries(AI_PROVIDERS).map(([id, info]: [string, any]) => ({
value: id,
label: info.name,
}));
setProviderOptions(nextOptions);
if (nextOptions.length > 0) {
setProvider((current: string): string =>
nextOptions.some((opt: any) => opt.value === current)
? current
: ((nextOptions[0] as any).value as string)
);
}
} catch {
const fallbackOptions = Object.entries(AI_PROVIDERS).map(([id, info]) => ({
value: id,
label: info.name,
}));
setProviderOptions(fallbackOptions);
if (fallbackOptions.length > 0) {
setProvider((current) =>
fallbackOptions.some((opt) => opt.value === current)
? current
: fallbackOptions[0].value
);
}
} finally {
setLoading(false);
}
};
fetchProviders();
}, [t]);
return { provider, setProvider, providerOptions, loading };
}