"use client"; import React, { useState, useEffect, useCallback } from "react"; interface ApiEndpoint { method: string; path: string; description: string; tag: string; } const API_ENDPOINTS: ApiEndpoint[] = [ { method: "POST", path: "/v1/chat/completions", description: "OpenAI-compatible chat completions with streaming support", tag: "Chat", }, { method: "POST", path: "/v1/responses", description: "OpenAI Responses API format", tag: "Responses", }, { method: "GET", path: "/v1/models", description: "List available models across all providers", tag: "Models", }, { method: "POST", path: "/v1/embeddings", description: "Generate text embeddings", tag: "Embeddings", }, { method: "POST", path: "/v1/images/generations", description: "Generate images from text prompts", tag: "Images", }, { method: "POST", path: "/v1/audio/transcriptions", description: "Transcribe audio files", tag: "Audio", }, { method: "POST", path: "/v1/audio/speech", description: "Text-to-speech generation", tag: "Audio", }, { method: "POST", path: "/v1/moderations", description: "Content moderation check", tag: "Moderations", }, { method: "POST", path: "/v1/rerank", description: "Re-rank documents by relevance", tag: "Rerank", }, { method: "POST", path: "/v1/search", description: "Web search across 5 providers", tag: "Search", }, { method: "POST", path: "/v1/videos/generations", description: "Generate videos from prompts", tag: "Video", }, { method: "POST", path: "/v1/music/generations", description: "Generate music from prompts", tag: "Music", }, ]; const METHOD_COLORS: Record = { GET: "bg-emerald-500/10 text-emerald-600 border-emerald-500/20", POST: "bg-blue-500/10 text-blue-600 border-blue-500/20", PUT: "bg-amber-500/10 text-amber-600 border-amber-500/20", DELETE: "bg-red-500/10 text-red-600 border-red-500/20", PATCH: "bg-purple-500/10 text-purple-600 border-purple-500/20", }; const EXAMPLE_BODIES: Record = { "/v1/chat/completions": JSON.stringify( { model: "openai/gpt-4o-mini", messages: [{ role: "user", content: "Hello!" }], stream: true }, null, 2 ), "/v1/models": "", "/v1/embeddings": JSON.stringify( { model: "openai/text-embedding-3-small", input: "Hello world" }, null, 2 ), "/v1/images/generations": JSON.stringify( { model: "openai/gpt-image-2", prompt: "A sunset over mountains", n: 1 }, null, 2 ), "/v1/responses": JSON.stringify( { model: "openai/gpt-4o-mini", input: "What is OmniRoute?" }, null, 2 ), }; export function ApiExplorerClient() { const [selected, setSelected] = useState(null); const [baseUrl, setBaseUrl] = useState("http://localhost:20128"); const [apiKey, setApiKey] = useState(""); const [requestBody, setRequestBody] = useState(""); const [response, setResponse] = useState(null); const [loading, setLoading] = useState(false); const [filterTag, setFilterTag] = useState(null); const allTags = [...new Set(API_ENDPOINTS.map((e) => e.tag))]; const filteredEndpoints = filterTag ? API_ENDPOINTS.filter((e) => e.tag === filterTag) : API_ENDPOINTS; const handleSelect = useCallback((endpoint: ApiEndpoint) => { setSelected(endpoint); setResponse(null); const example = EXAMPLE_BODIES[endpoint.path] || ""; setRequestBody(example); }, []); const handleTryIt = async () => { if (!selected) return; setLoading(true); setResponse(null); try { const headers: Record = { "Content-Type": "application/json" }; if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`; const opts: RequestInit = { method: selected.method, headers }; if (selected.method !== "GET" && requestBody.trim()) { opts.body = requestBody; } const res = await fetch(`${baseUrl}${selected.path}`, opts); const contentType = res.headers.get("content-type") || ""; if (contentType.includes("text/event-stream")) { setResponse("SSE stream started — check the terminal/devtools for real-time output."); } else { const data = await res.json(); setResponse(JSON.stringify(data, null, 2)); } } catch (err) { setResponse(`Error: ${err instanceof Error ? err.message : "Request failed"}`); } finally { setLoading(false); } }; return (
{allTags.map((tag) => ( ))}
{filteredEndpoints.map((endpoint) => ( ))}
{selected ? (
{selected.method} {selected.path}

{selected.description}

setBaseUrl(e.target.value)} className="w-full px-3 py-2 text-sm bg-bg-subtle border border-border rounded-lg focus:outline-none focus:ring-1 focus:ring-primary" />
setApiKey(e.target.value)} placeholder="sk-..." className="w-full px-3 py-2 text-sm bg-bg-subtle border border-border rounded-lg focus:outline-none focus:ring-1 focus:ring-primary" />
{selected.method !== "GET" && (