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
import { parseTraeApiHost, resolveTraeApiHost } from "@omniroute/open-sse/utils/traeHost.ts";
/**
* Pure parser for the Trae SOLO /authorize callback query string. Extracted
* from route.ts so it can be unit-tested without touching the DB layer.
*
* Returns the credential bundle that the route hands to createProviderConnection,
* or a structured error if the payload is missing/malformed.
*/
export type ParsedTraeCallback = {
ok: true;
record: {
provider: "trae";
authType: "oauth";
accessToken: string;
refreshToken: string | null;
expiresAt: string | null;
email: string | null;
providerSpecificData: {
userId: string;
tenantId: string;
bizUserId: string;
userUniqueId: string;
webId: string;
scope: "marscode-us";
tenant: "marscode";
region: string;
aiRegion: string;
host: string;
screenName: string | null;
clientId: string;
refreshExpireAt: number | null;
authMethod: "oauth_callback";
userRegion?: string;
userTimezone?: string;
};
testStatus: "active";
};
};
export type ParseError = { ok: false; error: string };
export function parseTraeCallbackQuery(q: URLSearchParams): ParsedTraeCallback | ParseError {
const userJwtRaw = q.get("userJwt");
if (!userJwtRaw) return { ok: false, error: "Missing userJwt in callback" };
let userJwt: Record<string, unknown>;
try {
userJwt = JSON.parse(userJwtRaw);
} catch {
return { ok: false, error: "Malformed userJwt payload" };
}
const token = userJwt.Token as string | undefined;
if (!token) return { ok: false, error: "userJwt.Token missing" };
const refresh = (userJwt.RefreshToken as string) || q.get("refreshToken") || null;
const tokenExpiresAtMs = Number(userJwt.TokenExpireAt) || 0;
const refreshExpiresAtMs = Number(userJwt.RefreshExpireAt || q.get("refreshExpireAt")) || 0;
let info: Record<string, unknown> = {};
const userInfoRaw = q.get("userInfo");
if (userInfoRaw) {
try {
info = JSON.parse(userInfoRaw);
} catch {
// userInfo is best-effort metadata — fall back to defaults silently.
}
}
// Trae sends its own API host; one outside trae.ai means the callback did not come from
// Trae, and storing a different region would only fail later at token refresh.
const hostParam = q.get("host");
if (hostParam && !parseTraeApiHost(hostParam)) {
return { ok: false, error: "Unexpected Trae API host in callback" };
}
const userId = (info.UserID as string) || "";
const region = (info.Region as string) || "US-East";
// Best-effort: the /authorize callback's userInfo payload has not been
// observed to carry a distinct x-user-region/timezone value distinct from
// Region — if Trae ever adds one under these names it propagates
// automatically; otherwise buildHeaders() falls back to "US"/no timezone
// header exactly as it does today (#12190).
const userRegion = (info.UserRegion as string) || undefined;
const userTimezone = (info.Timezone as string) || undefined;
return {
ok: true,
record: {
provider: "trae",
authType: "oauth",
accessToken: token,
refreshToken: refresh,
expiresAt: tokenExpiresAtMs ? new Date(tokenExpiresAtMs).toISOString() : null,
email: (info.NonPlainTextEmail as string) || null,
providerSpecificData: {
userId,
tenantId: (info.TenantID as string) || "",
bizUserId: userId,
userUniqueId: userId,
webId: userId,
scope: "marscode-us",
tenant: "marscode",
region,
aiRegion: (info.AIRegion as string) || region,
host: resolveTraeApiHost(hostParam),
screenName: (info.ScreenName as string) || null,
clientId: (userJwt.ClientID as string) || "en1oxy7wnw8j9n",
refreshExpireAt: refreshExpiresAtMs || null,
authMethod: "oauth_callback",
...(userRegion ? { userRegion } : {}),
...(userTimezone ? { userTimezone } : {}),
},
testStatus: "active",
},
};
}