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 } from "react";
import { useTranslations } from "next-intl";
import { useApiKey } from "../../providers/hooks/useApiKey";
import { useProviderModels } from "../../providers/hooks/useProviderModels";
import { buildCurl } from "../../providers/utils/buildCurl";
import { PLAYGROUND_KEY_ID_HEADER, resolvePlaygroundKeyId } from "../../providers/utils/playgroundAuth";
import { PlaygroundCard } from "./PlaygroundCard";
interface Props {
providerId: string;
}
const ENDPOINT_PATH = "/api/v1/embeddings";
function extractError(data: unknown): string | null {
if (!data || typeof data !== "object") return null;
const d = data as Record<string, unknown>;
const err = d.error as Record<string, unknown> | undefined;
if (err?.message) return String(err.message);
if (typeof d.message === "string") return d.message;
return null;
}
export function EmbeddingExampleCard({ providerId }: Props) {
const t = useTranslations("miniPlayground");
const { apiKey, keys } = useApiKey();
const { models } = useProviderModels(providerId);
const firstModel = models[0]?.id ?? "";
const [model, setModel] = useState<string>("");
const [inputText, setInputText] = useState<string>(() => t("embeddingSample"));
const [running, setRunning] = useState<boolean>(false);
const [result, setResult] = useState<{ data: unknown; latencyMs: number } | undefined>();
const [error, setError] = useState<string | null>(null);
const effectiveModel = model || firstModel;
const buildBody = () => ({ model: effectiveModel, input: inputText });
const curlSnippet = buildCurl({
endpoint:
(typeof window !== "undefined" ? window.location.origin : "http://localhost:20128") +
ENDPOINT_PATH,
headers: {
Authorization: "Bearer <your-api-key>",
"Content-Type": "application/json",
},
body: buildBody(),
});
const handleRun = async () => {
setRunning(true);
setError(null);
setResult(undefined);
const t0 = performance.now();
try {
// Authenticate via the dashboard session cookie — never send the masked
// apiKey as a Bearer token (it is not a real credential; see #9935).
const headers: Record<string, string> = {
"Content-Type": "application/json",
"x-connection-id": providerId,
};
const playgroundKeyId = resolvePlaygroundKeyId(apiKey, keys);
if (playgroundKeyId) headers[PLAYGROUND_KEY_ID_HEADER] = playgroundKeyId;
const res = await fetch(ENDPOINT_PATH, {
method: "POST",
credentials: "same-origin",
headers,
body: JSON.stringify(buildBody()),
});
const data: unknown = await res.json();
const latencyMs = performance.now() - t0;
const errMsg = extractError(data);
if (!res.ok || errMsg) {
setError(errMsg ?? `HTTP ${res.status}`);
} else {
setResult({ data, latencyMs });
}
} catch (err) {
setError(err instanceof Error ? err.message : t("requestFailed"));
} finally {
setRunning(false);
}
};
const modelOptions = models.length > 0 ? models : [{ id: "text-embedding-3-small" }];
return (
<PlaygroundCard
kindLabel={t("embedding")}
apiEndpoint={ENDPOINT_PATH}
onRun={handleRun}
curlSnippet={curlSnippet}
running={running}
result={result}
error={error}
>
{/* Model select */}
<div>
<label className="block text-xs text-text-muted mb-1">{t("model")}</label>
<select
value={model || firstModel}
onChange={(e) => setModel(e.target.value)}
className="w-full rounded-md border border-border bg-bg-subtle text-sm px-2 py-1.5 text-text-main focus:outline-none focus:ring-1 focus:ring-primary"
>
{modelOptions.map((m) => (
<option key={m.id} value={m.id}>
{m.id}
</option>
))}
</select>
</div>
{/* Input text */}
<div>
<label className="block text-xs text-text-muted mb-1">{t("input")}</label>
<input
type="text"
value={inputText}
onChange={(e) => setInputText(e.target.value)}
placeholder={t("embeddingSample")}
className="w-full rounded-md border border-border bg-bg-subtle text-sm px-2 py-1.5 text-text-main focus:outline-none focus:ring-1 focus:ring-primary"
/>
</div>
</PlaygroundCard>
);
}