mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
166 lines
6.3 KiB
JavaScript
166 lines
6.3 KiB
JavaScript
// Ensure proxyFetch is loaded to patch globalThis.fetch
|
|
import "open-sse/index.js";
|
|
|
|
import { getProviderConnectionById, updateProviderConnection } from "@/lib/localDb";
|
|
import { requireUsageDashboardUser } from "@/lib/auth/currentUser";
|
|
import { resolveConnectionProxyConfig } from "@/lib/network/connectionProxy";
|
|
import { USAGE_APIKEY_PROVIDERS } from "@/shared/constants/providers";
|
|
import { getExecutor } from "open-sse/executors/index.js";
|
|
import { getUsageForProvider } from "open-sse/services/usage.js";
|
|
|
|
const AUTH_EXPIRED_PATTERNS = [
|
|
"expired",
|
|
"authentication",
|
|
"unauthorized",
|
|
"401",
|
|
"re-authorize",
|
|
];
|
|
|
|
function isAuthExpiredMessage(usage) {
|
|
if (!usage?.message) return false;
|
|
const message = usage.message.toLowerCase();
|
|
return AUTH_EXPIRED_PATTERNS.some((pattern) => message.includes(pattern));
|
|
}
|
|
|
|
/**
|
|
* Refresh connection credentials when required and persist the result.
|
|
* @param {object} connection Provider connection.
|
|
* @param {boolean} force Refresh even if the executor considers the token valid.
|
|
* @param {object|null} proxyOptions Connection proxy configuration.
|
|
* @returns {Promise<{ connection: object, refreshed: boolean }>}
|
|
*/
|
|
export async function refreshAndUpdateCredentials(connection, force = false, proxyOptions = null) {
|
|
const executor = getExecutor(connection.provider);
|
|
const credentials = {
|
|
accessToken: connection.accessToken,
|
|
refreshToken: connection.refreshToken,
|
|
idToken: connection.idToken,
|
|
expiresAt: connection.expiresAt || connection.tokenExpiresAt,
|
|
lastRefreshAt: connection.lastRefreshAt,
|
|
connectionId: connection.id,
|
|
providerSpecificData: connection.providerSpecificData,
|
|
copilotToken: connection.providerSpecificData?.copilotToken,
|
|
copilotTokenExpiresAt: connection.providerSpecificData?.copilotTokenExpiresAt,
|
|
};
|
|
|
|
if (!force && !executor.needsRefresh(credentials)) {
|
|
return { connection, refreshed: false };
|
|
}
|
|
|
|
const refreshResult = await executor.refreshCredentials(credentials, console, proxyOptions);
|
|
if (!refreshResult) {
|
|
if (connection.accessToken) return { connection, refreshed: false };
|
|
throw new Error("Failed to refresh credentials. Please re-authorize the connection.");
|
|
}
|
|
|
|
const updateData = { updatedAt: new Date().toISOString() };
|
|
if (refreshResult.accessToken) updateData.accessToken = refreshResult.accessToken;
|
|
if (refreshResult.refreshToken) updateData.refreshToken = refreshResult.refreshToken;
|
|
if (refreshResult.idToken) updateData.idToken = refreshResult.idToken;
|
|
if (refreshResult.lastRefreshAt) updateData.lastRefreshAt = refreshResult.lastRefreshAt;
|
|
|
|
if (refreshResult.expiresIn) {
|
|
updateData.expiresAt = new Date(Date.now() + refreshResult.expiresIn * 1000).toISOString();
|
|
updateData.expiresIn = refreshResult.expiresIn;
|
|
} else if (refreshResult.expiresAt) {
|
|
updateData.expiresAt = refreshResult.expiresAt;
|
|
}
|
|
|
|
const providerSpecificUpdates = {
|
|
...(refreshResult.providerSpecificData || {}),
|
|
...(refreshResult.copilotToken ? { copilotToken: refreshResult.copilotToken } : {}),
|
|
...(refreshResult.copilotTokenExpiresAt
|
|
? { copilotTokenExpiresAt: refreshResult.copilotTokenExpiresAt }
|
|
: {}),
|
|
};
|
|
if (Object.keys(providerSpecificUpdates).length > 0) {
|
|
updateData.providerSpecificData = {
|
|
...(connection.providerSpecificData || {}),
|
|
...providerSpecificUpdates,
|
|
};
|
|
}
|
|
|
|
await updateProviderConnection(connection.id, updateData);
|
|
|
|
return {
|
|
connection: {
|
|
...connection,
|
|
...updateData,
|
|
providerSpecificData: updateData.providerSpecificData || connection.providerSpecificData,
|
|
},
|
|
refreshed: true,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* GET /api/usage/[connectionId] - Get quota data for one provider connection.
|
|
*/
|
|
export async function GET(_request, { params }) {
|
|
let connection;
|
|
try {
|
|
const { connectionId } = await params;
|
|
const user = await requireUsageDashboardUser();
|
|
|
|
connection = await getProviderConnectionById(
|
|
connectionId,
|
|
user.role === "admin" ? null : user.id,
|
|
);
|
|
if (!connection) {
|
|
return Response.json({ error: "Connection not found" }, { status: 404 });
|
|
}
|
|
|
|
const isOAuth = connection.authType === "oauth";
|
|
const isApikeyAuth =
|
|
connection.authType === "apikey" || connection.authType === "api_key";
|
|
const isApikeyEligible =
|
|
isApikeyAuth && USAGE_APIKEY_PROVIDERS.includes(connection.provider);
|
|
if (!isOAuth && !isApikeyEligible) {
|
|
return Response.json({ message: "Usage not available for this connection" });
|
|
}
|
|
|
|
const proxyConfig = await resolveConnectionProxyConfig(connection.providerSpecificData);
|
|
const proxyOptions = {
|
|
connectionProxyEnabled: proxyConfig.connectionProxyEnabled === true,
|
|
connectionProxyUrl: proxyConfig.connectionProxyUrl || "",
|
|
connectionNoProxy: proxyConfig.connectionNoProxy || "",
|
|
vercelRelayUrl: proxyConfig.vercelRelayUrl || "",
|
|
strictProxy: false,
|
|
};
|
|
|
|
if (isOAuth) {
|
|
try {
|
|
const result = await refreshAndUpdateCredentials(connection, false, proxyOptions);
|
|
connection = result.connection;
|
|
} catch (refreshError) {
|
|
console.error("[Usage API] Credential refresh failed:", refreshError);
|
|
return Response.json(
|
|
{ error: `Credential refresh failed: ${refreshError.message}` },
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
}
|
|
|
|
let usage = await getUsageForProvider(connection, proxyOptions);
|
|
|
|
if (isOAuth && isAuthExpiredMessage(usage) && connection.refreshToken) {
|
|
try {
|
|
const retryResult = await refreshAndUpdateCredentials(connection, true, proxyOptions);
|
|
connection = retryResult.connection;
|
|
usage = await getUsageForProvider(connection, proxyOptions);
|
|
} catch (retryError) {
|
|
console.warn(`[Usage] ${connection.provider}: force refresh failed: ${retryError.message}`);
|
|
}
|
|
}
|
|
|
|
return Response.json(usage);
|
|
} catch (error) {
|
|
if (error?.message === "Unauthorized") {
|
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const provider = connection?.provider ?? "unknown";
|
|
console.warn(`[Usage] ${provider}: ${error.message}`);
|
|
return Response.json({ error: error.message }, { status: 500 });
|
|
}
|
|
}
|