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
/**
* SYNTX.ai (unofficial) video generation — POST /api/v1/video/generate.
*/
import { saveCallLog } from "@/lib/usageDb";
import { sanitizeErrorMessage } from "../../utils/error.ts";
import {
SyntxMediaError,
fetchSyntxMediaBytes,
firstMediaUrl,
mapSyntxVideoRequestSettings,
resolveSyntxMediaTarget,
resolveSyntxToken,
runSyntxVideoGeneration,
uploadSyntxMediaFile,
} from "../../services/syntxMedia.ts";
import { looksLikeJwt } from "../../services/syntxAuth.ts";
function collectUrls(value: unknown): string[] {
if (typeof value === "string" && value.trim()) return [value.trim()];
if (Array.isArray(value)) {
return value.flatMap((item) => collectUrls(item));
}
return [];
}
function decodeDataUrl(dataUrl: string): { bytes: Uint8Array; mime: string; name: string } | null {
const match = /^data:([^;,]+);base64,(.+)$/i.exec(dataUrl.trim());
if (!match) return null;
try {
const mime = match[1] || "image/png";
const bytes = Buffer.from(match[2], "base64");
const ext = mime.startsWith("video/") ? "mp4" : mime.includes("jpeg") ? "jpg" : "png";
return { bytes, mime, name: `frame.${ext}` };
} catch {
return null;
}
}
async function uploadVideoRefs(
token: string,
body: Record<string, unknown>,
fetchImpl: typeof fetch
): Promise<string[]> {
const rawFiles = [
...collectUrls(body.image),
...collectUrls(body.image_url),
...collectUrls(body.image_urls),
...collectUrls(body.file_urls),
];
const fileUrls: string[] = [];
for (const ref of rawFiles) {
if (ref.startsWith("data:")) {
const decoded = decodeDataUrl(ref);
if (!decoded) continue;
const url = await uploadSyntxMediaFile({
token,
bytes: decoded.bytes,
filename: decoded.name,
mimeType: decoded.mime,
fetchImpl,
});
if (url) fileUrls.push(url);
} else if (/^https?:\/\//i.test(ref)) {
fileUrls.push(ref);
}
}
return fileUrls;
}
async function runSyntxVideoJob(options: {
token: string;
model: string;
provider: string;
body: Record<string, unknown>;
prompt: string;
startTime: number;
log?: { info?: (...args: unknown[]) => void; error?: (...args: unknown[]) => void };
fetchImpl: typeof fetch;
}) {
const fileUrls = await uploadVideoRefs(options.token, options.body, options.fetchImpl);
const { aiName, modelType } = resolveSyntxMediaTarget(options.model, "video");
const settings = mapSyntxVideoRequestSettings(aiName, modelType, options.body);
const result = await runSyntxVideoGeneration({
token: options.token,
model: options.model,
prompt: options.prompt,
settings,
fileUrls: fileUrls.length > 0 ? fileUrls : undefined,
audioUrl: typeof options.body.audio_url === "string" ? options.body.audio_url : undefined,
timeoutMs: typeof options.body.timeout_ms === "number" ? options.body.timeout_ms : undefined,
fetchImpl: options.fetchImpl,
});
const url = firstMediaUrl(result, "video") || result.media[0]?.url;
if (!url) {
return {
success: false,
status: 502,
error: "SYNTX video generation completed without a video URL",
};
}
const wantsBase64 = String(options.body.response_format || "").toLowerCase() === "b64_json";
const item = wantsBase64
? {
b64_json: (await fetchSyntxMediaBytes(url, options.fetchImpl)).bytes.toString("base64"),
format: "mp4",
}
: { url, format: "mp4" };
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 200,
model: `${options.provider}/${options.model}`,
provider: options.provider,
duration: Date.now() - options.startTime,
}).catch(() => {});
options.log?.info?.("VIDEO", `SYNTX generated video via ${options.model}`);
return { success: true, data: { created: Math.floor(Date.now() / 1000), data: [item] } };
}
export async function handleSyntxVideoGeneration({
model,
provider,
body,
credentials,
log,
fetchImpl = fetch,
}: {
model: string;
provider: string;
providerConfig?: { baseUrl?: string };
body: Record<string, unknown>;
credentials: {
apiKey?: string;
accessToken?: string;
providerSpecificData?: unknown;
};
log?: { info?: (...args: unknown[]) => void; error?: (...args: unknown[]) => void };
fetchImpl?: typeof fetch;
}) {
const startTime = Date.now();
const prompt = typeof body.prompt === "string" ? body.prompt.trim() : "";
if (!prompt) {
return { success: false, status: 400, error: "Prompt is required for SYNTX video generation" };
}
const token = resolveSyntxToken({
apiKey: credentials?.apiKey,
accessToken: credentials?.accessToken,
providerSpecificData: credentials?.providerSpecificData,
});
if (!looksLikeJwt(token)) {
return {
success: false,
status: 401,
error: "Missing SYNTX JWT — paste the Authorization Bearer token from syntx.ai",
};
}
try {
return await runSyntxVideoJob({
token,
model,
provider,
body,
prompt,
startTime,
log,
fetchImpl,
});
} catch (error) {
const status = error instanceof SyntxMediaError ? error.status : 502;
const message = sanitizeErrorMessage(error instanceof Error ? error.message : error);
log?.error?.("VIDEO", `SYNTX video generation failed: ${message}`);
return { success: false, status, error: message || "SYNTX video generation failed" };
}
}