/* Copyright (C) 2023-2026 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { useMemo, useState } from 'react' import { ChevronRight, Gauge, KeyRound, ScrollText, Sigma, Zap, } from 'lucide-react' import { useTranslation } from 'react-i18next' import type { BundledLanguage } from 'shiki/bundle/web' import { useStatus } from '@/hooks/use-status' import { Badge } from '@/components/ui/badge' import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs' import { CodeBlock, CodeBlockCopyButton, } from '@/components/ai-elements/code-block' import { StaticDataTable, staticDataTableClassNames as tableStyles, } from '@/components/data-table' import { buildRateLimits, buildSupportedParameters, formatRateLimit, type SupportedParameter, } from '../lib/mock-stats' import { replaceModelInPath } from '../lib/model-helpers' import type { PricingModel } from '../types' // --------------------------------------------------------------------------- // Code-sample registry // --------------------------------------------------------------------------- // // Each sample is keyed by language and endpoint type. The endpoint type comes // from the model's `supported_endpoint_types`; we render samples only for the // types the model actually supports. This keeps copy-pasted code accurate and // provider-shaped (OpenAI vs Anthropic vs Gemini, etc.). type Lang = 'curl' | 'python' | 'typescript' | 'javascript' const LANG_LABELS: Record = { curl: 'cURL', python: 'Python', typescript: 'TypeScript', javascript: 'JavaScript', } const LANG_HIGHLIGHT: Record = { curl: 'bash', python: 'python', typescript: 'typescript', javascript: 'javascript', } type SampleContext = { baseUrl: string apiKeyEnv: string modelName: string endpointType: string endpointPath: string } function buildChatSample(lang: Lang, ctx: SampleContext): string { const url = `${ctx.baseUrl}${ctx.endpointPath}` const isResponses = ctx.endpointType === 'openai-response' const isReasoning = /^o[1-4]|reasoning|thinking|deepseek-r/i.test( ctx.modelName ) const userMessage = 'Explain quantum entanglement in one paragraph.' const bodyJson = isResponses ? JSON.stringify({ model: ctx.modelName, input: userMessage }, null, 2) : JSON.stringify( { model: ctx.modelName, messages: [{ role: 'user', content: userMessage }], ...(isReasoning ? {} : { temperature: 0.7 }), }, null, 2 ) const fnCall = isResponses ? 'responses.create' : 'chat.completions.create' if (lang === 'curl') { return [ `curl ${url} \\`, ` -H "Authorization: Bearer $${ctx.apiKeyEnv}" \\`, ` -H "Content-Type: application/json" \\`, ` -d '${bodyJson.replace(/\n/g, '\n ')}'`, ].join('\n') } if (lang === 'python') { return [ 'from openai import OpenAI', '', 'client = OpenAI(', ` base_url="${ctx.baseUrl}/v1",`, ` api_key="",`, ')', '', isResponses ? `response = client.${fnCall}(\n model="${ctx.modelName}",\n input="${userMessage}",\n)\n\nprint(response.output_text)` : `completion = client.${fnCall}(\n model="${ctx.modelName}",\n messages=[\n {"role": "user", "content": "${userMessage}"}\n ],\n)\n\nprint(completion.choices[0].message.content)`, ].join('\n') } if (lang === 'typescript') { return [ `import OpenAI from 'openai'`, '', `const client = new OpenAI({`, ` baseURL: '${ctx.baseUrl}/v1',`, ` apiKey: process.env.${ctx.apiKeyEnv},`, `})`, '', isResponses ? `const response = await client.${fnCall}({\n model: '${ctx.modelName}',\n input: '${userMessage}',\n})\n\nconsole.log(response.output_text)` : `const completion = await client.${fnCall}({\n model: '${ctx.modelName}',\n messages: [{ role: 'user', content: '${userMessage}' }],\n})\n\nconsole.log(completion.choices[0].message.content)`, ].join('\n') } return [ `const response = await fetch('${url}', {`, ` method: 'POST',`, ` headers: {`, ` Authorization: \`Bearer \${process.env.${ctx.apiKeyEnv}}\`,`, ` 'Content-Type': 'application/json',`, ` },`, ` body: JSON.stringify(${bodyJson}),`, `})`, '', `const data = await response.json()`, `console.log(data)`, ].join('\n') } function buildAnthropicSample(lang: Lang, ctx: SampleContext): string { const url = `${ctx.baseUrl}${ctx.endpointPath}` const userMessage = 'Explain quantum entanglement in one paragraph.' if (lang === 'curl') { const body = JSON.stringify( { model: ctx.modelName, max_tokens: 1024, messages: [{ role: 'user', content: userMessage }], }, null, 2 ) return [ `curl ${url} \\`, ` -H "x-api-key: $${ctx.apiKeyEnv}" \\`, ` -H "anthropic-version: 2023-06-01" \\`, ` -H "Content-Type: application/json" \\`, ` -d '${body.replace(/\n/g, '\n ')}'`, ].join('\n') } if (lang === 'python') { return [ 'import anthropic', '', 'client = anthropic.Anthropic(', ` base_url="${ctx.baseUrl}",`, ` api_key="",`, ')', '', `message = client.messages.create(`, ` model="${ctx.modelName}",`, ` max_tokens=1024,`, ` messages=[{"role": "user", "content": "${userMessage}"}],`, ')', '', 'print(message.content[0].text)', ].join('\n') } if (lang === 'typescript') { return [ `import Anthropic from '@anthropic-ai/sdk'`, '', `const client = new Anthropic({`, ` baseURL: '${ctx.baseUrl}',`, ` apiKey: process.env.${ctx.apiKeyEnv},`, `})`, '', `const message = await client.messages.create({`, ` model: '${ctx.modelName}',`, ` max_tokens: 1024,`, ` messages: [{ role: 'user', content: '${userMessage}' }],`, `})`, '', `console.log(message.content[0].text)`, ].join('\n') } return [ `const response = await fetch('${url}', {`, ` method: 'POST',`, ` headers: {`, ` 'x-api-key': process.env.${ctx.apiKeyEnv},`, ` 'anthropic-version': '2023-06-01',`, ` 'Content-Type': 'application/json',`, ` },`, ` body: JSON.stringify({`, ` model: '${ctx.modelName}',`, ` max_tokens: 1024,`, ` messages: [{ role: 'user', content: '${userMessage}' }],`, ` }),`, `})`, '', `const data = await response.json()`, `console.log(data.content[0].text)`, ].join('\n') } function buildGeminiSample(lang: Lang, ctx: SampleContext): string { const url = `${ctx.baseUrl}${ctx.endpointPath}?key=$${ctx.apiKeyEnv}` const userMessage = 'Explain quantum entanglement in one paragraph.' if (lang === 'curl') { const body = JSON.stringify( { contents: [{ parts: [{ text: userMessage }] }] }, null, 2 ) return [ `curl '${url}' \\`, ` -H 'Content-Type: application/json' \\`, ` -d '${body.replace(/\n/g, '\n ')}'`, ].join('\n') } if (lang === 'python') { return [ 'import google.generativeai as genai', '', `genai.configure(api_key="")`, '', `model = genai.GenerativeModel("${ctx.modelName}")`, `response = model.generate_content("${userMessage}")`, '', `print(response.text)`, ].join('\n') } if (lang === 'typescript') { return [ `import { GoogleGenerativeAI } from '@google/generative-ai'`, '', `const genAI = new GoogleGenerativeAI(process.env.${ctx.apiKeyEnv}!)`, `const model = genAI.getGenerativeModel({ model: '${ctx.modelName}' })`, '', `const result = await model.generateContent('${userMessage}')`, `console.log(result.response.text())`, ].join('\n') } return [ `const response = await fetch('${url}', {`, ` method: 'POST',`, ` headers: { 'Content-Type': 'application/json' },`, ` body: JSON.stringify({`, ` contents: [{ parts: [{ text: '${userMessage}' }] }],`, ` }),`, `})`, '', `const data = await response.json()`, `console.log(data.candidates[0].content.parts[0].text)`, ].join('\n') } function buildEmbeddingSample(lang: Lang, ctx: SampleContext): string { const url = `${ctx.baseUrl}${ctx.endpointPath}` const text = 'The food was delicious and the waiter…' if (lang === 'curl') { const body = JSON.stringify({ model: ctx.modelName, input: text }, null, 2) return [ `curl ${url} \\`, ` -H "Authorization: Bearer $${ctx.apiKeyEnv}" \\`, ` -H "Content-Type: application/json" \\`, ` -d '${body.replace(/\n/g, '\n ')}'`, ].join('\n') } if (lang === 'python') { return [ 'from openai import OpenAI', '', `client = OpenAI(base_url="${ctx.baseUrl}/v1", api_key="")`, '', 'response = client.embeddings.create(', ` model="${ctx.modelName}",`, ` input="${text}",`, ')', '', 'print(response.data[0].embedding[:8])', ].join('\n') } if (lang === 'typescript') { return [ `import OpenAI from 'openai'`, '', `const client = new OpenAI({`, ` baseURL: '${ctx.baseUrl}/v1',`, ` apiKey: process.env.${ctx.apiKeyEnv},`, `})`, '', `const response = await client.embeddings.create({`, ` model: '${ctx.modelName}',`, ` input: '${text}',`, `})`, '', `console.log(response.data[0].embedding.slice(0, 8))`, ].join('\n') } return [ `const response = await fetch('${url}', {`, ` method: 'POST',`, ` headers: {`, ` Authorization: \`Bearer \${process.env.${ctx.apiKeyEnv}}\`,`, ` 'Content-Type': 'application/json',`, ` },`, ` body: JSON.stringify({`, ` model: '${ctx.modelName}',`, ` input: '${text}',`, ` }),`, `})`, '', `const data = await response.json()`, `console.log(data.data[0].embedding.slice(0, 8))`, ].join('\n') } function buildImageSample(lang: Lang, ctx: SampleContext): string { const url = `${ctx.baseUrl}${ctx.endpointPath}` const prompt = 'A serene koi pond at sunset, ukiyo-e style.' if (lang === 'curl') { const body = JSON.stringify( { model: ctx.modelName, prompt, size: '1024x1024', n: 1 }, null, 2 ) return [ `curl ${url} \\`, ` -H "Authorization: Bearer $${ctx.apiKeyEnv}" \\`, ` -H "Content-Type: application/json" \\`, ` -d '${body.replace(/\n/g, '\n ')}'`, ].join('\n') } if (lang === 'python') { return [ 'from openai import OpenAI', '', `client = OpenAI(base_url="${ctx.baseUrl}/v1", api_key="")`, '', 'response = client.images.generate(', ` model="${ctx.modelName}",`, ` prompt="${prompt}",`, ` size="1024x1024",`, ` n=1,`, ')', '', 'print(response.data[0].url)', ].join('\n') } if (lang === 'typescript') { return [ `import OpenAI from 'openai'`, '', `const client = new OpenAI({`, ` baseURL: '${ctx.baseUrl}/v1',`, ` apiKey: process.env.${ctx.apiKeyEnv},`, `})`, '', `const response = await client.images.generate({`, ` model: '${ctx.modelName}',`, ` prompt: '${prompt}',`, ` size: '1024x1024',`, ` n: 1,`, `})`, '', `console.log(response.data[0].url)`, ].join('\n') } return [ `const response = await fetch('${url}', {`, ` method: 'POST',`, ` headers: {`, ` Authorization: \`Bearer \${process.env.${ctx.apiKeyEnv}}\`,`, ` 'Content-Type': 'application/json',`, ` },`, ` body: JSON.stringify({`, ` model: '${ctx.modelName}',`, ` prompt: '${prompt}',`, ` size: '1024x1024',`, ` n: 1,`, ` }),`, `})`, '', `const data = await response.json()`, `console.log(data.data[0].url)`, ].join('\n') } function buildSample( lang: Lang, endpointType: string, ctx: SampleContext ): string { if (endpointType === 'anthropic') return buildAnthropicSample(lang, ctx) if (endpointType === 'gemini') return buildGeminiSample(lang, ctx) if (endpointType === 'embeddings' || endpointType === 'jina-rerank') return buildEmbeddingSample(lang, ctx) if (endpointType === 'image-generation') return buildImageSample(lang, ctx) return buildChatSample(lang, ctx) } // --------------------------------------------------------------------------- // Code samples section // --------------------------------------------------------------------------- function CodeSamplesSection(props: { model: PricingModel endpointMap: Record }) { const { t } = useTranslation() const { status } = useStatus() const baseUrl = useMemo(() => { const candidate = (status as Record | null)?.server_address ?? (status as Record | null)?.serverAddress ?? (status?.data as Record | undefined)?.server_address ?? (status?.data as Record | undefined)?.serverAddress if (candidate && typeof candidate === 'string') { return candidate.replace(/\/$/, '') } if (typeof window !== 'undefined') return window.location.origin return 'https://api.example.com' }, [status]) const endpoints = useMemo(() => { const types = props.model.supported_endpoint_types || [] return types .map((type) => { const info = props.endpointMap[type] || {} let path = info.path || '' if (path && path.includes('{model}')) { path = replaceModelInPath(path, props.model.model_name || '') } return { type, path, method: info.method || 'POST' } }) .filter((e) => Boolean(e.path)) }, [props.model, props.endpointMap]) const [endpointType, setEndpointType] = useState( endpoints[0]?.type ?? '' ) const [lang, setLang] = useState('curl') const activeEndpoint = useMemo(() => { return endpoints.find((e) => e.type === endpointType) ?? endpoints[0] }, [endpointType, endpoints]) if (endpoints.length === 0 || !activeEndpoint) { return null } const code = buildSample(lang, activeEndpoint.type, { baseUrl, apiKeyEnv: 'NEW_API_KEY', modelName: props.model.model_name || '', endpointType: activeEndpoint.type, endpointPath: activeEndpoint.path, }) return (
{t('Code samples')}
{endpoints.length > 1 && ( {endpoints.map((ep) => ( {ep.type} ))} )} setLang(v as Lang)} className='ml-auto' > {(Object.keys(LANG_LABELS) as Lang[]).map((l) => ( {LANG_LABELS[l]} ))}

{t('Replace')}{' '} {''} {' '} {t('with the API key from your token settings.')}

) } // --------------------------------------------------------------------------- // Supported parameters table // --------------------------------------------------------------------------- function SupportedParametersSection(props: { model: PricingModel }) { const { t } = useTranslation() const params = useMemo( () => buildSupportedParameters(props.model), [props.model] ) if (params.length === 0) return null return (
{t('Supported parameters')} param.name} getRowClassName={() => 'hover:bg-muted/20'} columns={[ { id: 'parameter', header: t('Parameter'), className: 'h-9 w-44', cellClassName: tableStyles.topCell, cell: (p) => (
{p.name} {p.required && ( {t('required')} )}
), }, { id: 'type', header: t('Type'), className: 'h-9 w-24', cellClassName: tableStyles.topCell, cell: (p) => ( {p.type} ), }, { id: 'range', header: t('Default / range'), className: 'h-9 w-32', cellClassName: tableStyles.topCell, cell: (p) => , }, { id: 'description', header: t('Description'), className: 'h-9', cellClassName: tableStyles.topMutedCell, cell: (p) => t(p.descriptionKey), }, ]} />
) } function ParamRangeCell(props: { param: SupportedParameter }) { const { defaultValue, range, enumValues } = props.param if (defaultValue !== undefined) { return (
= {String(defaultValue)} {range && ( {range} )}
) } if (range) { return ( {range} ) } if (enumValues && enumValues.length > 0) { return (
{enumValues.map((v) => ( {v} ))}
) } return — } // --------------------------------------------------------------------------- // Rate-limits table // --------------------------------------------------------------------------- function RateLimitsSection(props: { model: PricingModel }) { const { t } = useTranslation() const limits = useMemo(() => buildRateLimits(props.model), [props.model]) if (limits.length === 0) return null return (
{t('Rate limits')} limit.group} getRowClassName={() => 'hover:bg-muted/20'} columns={[ { id: 'group', header: t('Group'), className: 'h-9', cellClassName: 'py-2 font-mono', cell: (limit) => limit.group, }, { id: 'rpm', header: 'RPM', className: 'h-9 text-right', cellClassName: tableStyles.topNumericCell, cell: (limit) => formatRateLimit(limit.rpm), }, { id: 'tpm', header: 'TPM', className: 'h-9 text-right', cellClassName: tableStyles.topNumericCell, cell: (limit) => formatRateLimit(limit.tpm), }, { id: 'rpd', header: 'RPD', className: 'h-9 text-right', cellClassName: tableStyles.topNumericCell, cell: (limit) => formatRateLimit(limit.rpd), }, ]} />

{t( 'RPM = requests per minute, TPM = tokens per minute, RPD = requests per day. Limits apply per token group.' )}

) } // --------------------------------------------------------------------------- // Authentication preview // --------------------------------------------------------------------------- function AuthSection() { const { t } = useTranslation() return (
{t('Authentication')}

{t('All requests must include')}{' '} Authorization: Bearer <TOKEN> {' '} {t('header. Anthropic-formatted endpoints accept the')}{' '} x-api-key {' '} {t('header instead.')}

{t( 'Generate tokens from the Tokens page; you can scope them to specific models, groups, IPs, and rate-limits.' )}

) } // --------------------------------------------------------------------------- // Composite API tab // --------------------------------------------------------------------------- export function ModelDetailsApi(props: { model: PricingModel endpointMap: Record }) { return (
) } // --------------------------------------------------------------------------- // Local UI helpers // --------------------------------------------------------------------------- function SectionTitle(props: { children: React.ReactNode icon: React.ComponentType<{ className?: string }> }) { const Icon = props.icon return (

{props.children}

) } // Re-export so the parent can keep its own SectionTitle if it wants: export { Zap as ApiTabIcon }