mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
* fix: tool description null sanitization, clipboard HTTP fallback fixes T10 - Sanitize tool.description null in claude-to-openai translator - claude-to-openai.ts: tool.description defaults to empty string when null/undefined - claude-to-openai.ts: filter out tools with empty/missing names - Prevents 400 validation errors on providers like NVIDIA NIM (issue #276) T11 - Fix copy buttons to work on HTTP/non-HTTPS deployments - Add src/shared/utils/clipboard.ts with HTTPS+HTTP (execCommand) dual fallback - Migrate useCopyToClipboard.ts to use shared utility - Migrate ConsoleLogViewer.tsx, RequestLoggerV2.tsx to shared utility - Migrate HomePageClient.tsx, endpoint/page.tsx, GetStarted.tsx - Migrate DefaultToolCard.tsx to shared utility - Fixes copy buttons when OmniRoute runs behind HTTP proxy (issue #296) T02 - Verified SSE [DONE] sentinel handling already correct - sseParser.ts filters [DONE] on line 13 (no change needed) - stream.ts uses doneSent flag to prevent duplicate sentinel - bypassHandler.ts correctly separates streaming/non-streaming responses Issue triage comments posted to #340, #341, #344 * feat: DB read cache + Accept header stream negotiation (T09/T01) T09 - In-memory TTL cache for hot DB read paths - Add src/lib/db/readCache.ts with TTL cache (5s settings/connections, 30s pricing) - Eliminates redundant SQLite reads on concurrent requests - Integrate invalidation in settings.ts updateSettings() and updatePricing() - Integrate invalidation in providers.ts create/update/delete operations - Export getCachedSettings, getCachedPricing, getCachedProviderConnections, invalidateDbCache via localDb.ts for consumer migration - Cache auto-busts on any write, preserving data consistency T01 - Accept header stream negotiation - src/sse/handlers/chat.ts: detect Accept: text/event-stream header - Override body.stream=true when Accept header indicates streaming client - Enables curl, httpx and SDK clients that use HTTP headers instead of JSON body field to trigger streaming responses - Logs Accept override at DEBUG level for observability * fix: auto-advance quota window on expiry to prevent stale blocking (T08) T08 - Quota Window Rolling Auto-Advance - quotaCache.ts: add windowDurationMs field to QuotaCacheEntry interface (optional field that callers can set when they know the window duration) - Add advancedWindowResetAt() helper: if entry.nextResetAt is in the past, eagerly returns { exhausted: false } so requests are unblocked immediately - isAccountQuotaExhausted() now uses advancedWindowResetAt() instead of the previous inline date check, and optimistically clears entry.exhausted flag to avoid re-checking the same stale entry on the next request Before: exhausted accounts with an expired resetAt would wait up to 5 minutes for the background refresh before accepting new requests. After: the first request after resetAt passes will be immediately accepted and will trigger a quota refresh on the next background tick. * feat: manual OAuth token refresh UI (T12) T12 - Manual Token Refresh UI - Add POST /api/providers/[id]/refresh endpoint - Validates connection exists and is OAuth type - Calls getAccessToken() (same helper used in auto-refresh) - Persists new credentials via updateProviderCredentials() - Returns { success, expiresAt, refreshedAt } on success - Update providers/[id]/page.tsx - handleRefreshToken() with loading state (refreshingId) - Pass onRefreshToken + isRefreshing props to ConnectionRow - ConnectionRow: add optional onRefreshToken/isRefreshing props - ConnectionRow: tokenMinsLeft state via lazy init (Date.now() in getter fn, not in render body - satisfies react-hooks/purity) - Token expiry badge: red 'expired' | amber '~Xm' (<30min) | hidden - 'Token' button (amber) next to 'Retest' for OAuth connections - Add en.json i18n: tokenRefreshed, tokenRefreshFailed * Initial plan * feat: integrate wildcardRouter into model alias resolution (T13) T13 - Wildcard Model Routing - Import resolveWildcardAlias from wildcardRouter.ts into model.ts - In getModelInfoCore(), after exact alias check fails, try glob wildcard alias matching (e.g., 'claude-sonnet-*' alias → 'anthropic/claude-sonnet-4') - Returns { provider, model, extendedContext, wildcardPattern } on match - Falls back to MODEL_TO_PROVIDERS lookup and openai default as before * fix: clipboard cleanup and tool validation * feat: media page UX + T04 playground uploads + T03 HuggingFace/Vertex AI Media Page (MediaPageClient.tsx): - Render images inline (img tags from b64_json or url) - Show transcription as plain readable text (not raw JSON) - Amber banner for credential errors with link to /dashboard/providers - Detect empty transcription result and show credentials hint - Provider credential hint below selector for non-local providers - Extended provider/model lists: HuggingFace, Qwen TTS, Inworld, Cartesia, PlayHT, AssemblyAI T04 - Playground File Uploads (playground/page.tsx): - Audio file upload panel for transcription endpoint (multipart/form-data) - Image upload panel for vision models (gpt-4o, claude-3, gemini, pixtral, llava...) - Auto-detect vision models by name heuristic - Inject uploaded images as base64 image_url in chat messages - Inline image rendering for image generation results - Readable text view for transcription results with copy button - Preview thumbnails for attached images with individual remove T03 - HuggingFace + Vertex AI Providers: - HuggingFace: frontend providers.ts + backend providerRegistry.ts Uses HuggingFace Router OpenAI-compatible endpoint - Vertex AI: frontend providers.ts + backend providerRegistry.ts Uses gemini format with generateContent API (urlBuilder fallback) T07 - API Key Round-Robin: VERIFIED already implemented in auth.ts fill-first, round-robin, p2c, random, least-used, cost-optimized strategies * feat: T05 task-aware routing + fix #302 stream override + fix #73 claude provider fallback T05 - Task-Aware Smart Routing: - New open-sse/services/taskAwareRouter.ts: Detects 7 task types: coding, creative, analysis, vision, summarization, background, chat from system/user message content and images Configurable taskModelMap per task type, stats tracking applyTaskAwareRouting() integrates with existing chat pipeline - New src/app/api/settings/task-routing/route.ts: GET/PUT/POST API for task routing config + reset-stats + detect action Persists config via updateSettings('taskRouting') - Integration in src/sse/handlers/chat.ts: applyTaskAwareRouting() called after policy enforcement, before combo resolve Logs task type detection and model overrides Fix #302 - OpenAI SDK stream=False drops tool_calls: - src/sse/handlers/chat.ts T01 Accept header negotiation: Changed condition from 'body.stream !== true' to 'body.stream === undefined' OpenAI Python SDK sends 'Accept: application/json, text/event-stream' in every request, even stream=False — the old code was incorrectly forcing stream=true, causing tool_calls to be dropped from non-streaming responses Fix #73 - Claude Haiku routed to OpenAI provider instead of Antigravity: - open-sse/services/model.ts getModelInfoCore(): Added heuristic prefix detection before the blind 'openai' fallback: claude-* models → antigravity (Anthropic) provider gemini-*/gemma-* models → gemini provider Closes: #73, partially addresses #302 * fix: token counts 0 (#74), model import dup (#180), model route fallback (#73) fix #74 - Token counts always 0 for Antigravity/Claude streaming: - open-sse/utils/usageTracking.ts extractUsage(): Add handler for 'message_start' SSE event which carries INPUT tokens in Antigravity/Claude streaming: { type: 'message_start', message: { usage: { input_tokens: N } } } This event was completely unhandled, causing ALL input token counts to be dropped for every Antigravity/Claude streaming request fix #180 - Model import shows duplicates with no visual feedback: - src/shared/components/ModelSelectModal.tsx: Added addedModelValues prop (string[]) to receive already-added model values Models already in the combo now shown with ✓ indicator + green highlight Makes it visually clear which models are already added vs new - src/app/(dashboard)/dashboard/combos/page.tsx: Pass addedModelValues={models.map(m => m.model)} to ModelSelectModal * Harden clipboard UX and Claude tool normalization (#360) * Initial plan * chore: plan updates for clipboard and translator fixes * fix: clipboard cleanup, copy feedback, and claude tool validation --------- Co-authored-by: openai-code-agent[bot] <242516109+Codex@users.noreply.github.com> Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> Co-authored-by: openai-code-agent[bot] <242516109+Codex@users.noreply.github.com>
385 lines
13 KiB
TypeScript
385 lines
13 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useMemo, useEffect } from "react";
|
|
import PropTypes from "prop-types";
|
|
import Modal from "./Modal";
|
|
import { getModelsByProviderId, PROVIDER_ID_TO_ALIAS } from "@/shared/constants/models";
|
|
import {
|
|
OAUTH_PROVIDERS,
|
|
FREE_PROVIDERS,
|
|
APIKEY_PROVIDERS,
|
|
isOpenAICompatibleProvider,
|
|
isAnthropicCompatibleProvider,
|
|
} from "@/shared/constants/providers";
|
|
|
|
// Provider order: OAuth first, then Free, then API Key (matches dashboard/providers)
|
|
const PROVIDER_ORDER = [
|
|
...Object.keys(OAUTH_PROVIDERS),
|
|
...Object.keys(FREE_PROVIDERS),
|
|
...Object.keys(APIKEY_PROVIDERS),
|
|
];
|
|
|
|
export default function ModelSelectModal({
|
|
isOpen,
|
|
onClose,
|
|
onSelect,
|
|
selectedModel,
|
|
activeProviders = [],
|
|
title = "Select Model",
|
|
modelAliases = {},
|
|
addedModelValues = [],
|
|
}) {
|
|
const [searchQuery, setSearchQuery] = useState("");
|
|
const [combos, setCombos] = useState<any[]>([]);
|
|
const [providerNodes, setProviderNodes] = useState<any[]>([]);
|
|
const [customModels, setCustomModels] = useState<Record<string, any>>({});
|
|
|
|
const fetchCombos = async () => {
|
|
try {
|
|
const res = await fetch("/api/combos");
|
|
if (!res.ok) throw new Error(`Failed to fetch combos: ${res.status}`);
|
|
const data = await res.json();
|
|
setCombos(data.combos || []);
|
|
} catch (error) {
|
|
console.error("Error fetching combos:", error);
|
|
setCombos([]);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (isOpen) fetchCombos();
|
|
}, [isOpen]);
|
|
|
|
const fetchProviderNodes = async () => {
|
|
try {
|
|
const res = await fetch("/api/provider-nodes");
|
|
if (!res.ok) throw new Error(`Failed to fetch provider nodes: ${res.status}`);
|
|
const data = await res.json();
|
|
setProviderNodes(data.nodes || []);
|
|
} catch (error) {
|
|
console.error("Error fetching provider nodes:", error);
|
|
setProviderNodes([]);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (isOpen) fetchProviderNodes();
|
|
}, [isOpen]);
|
|
|
|
const fetchCustomModels = async () => {
|
|
try {
|
|
const res = await fetch("/api/provider-models");
|
|
if (!res.ok) throw new Error(`Failed to fetch custom models: ${res.status}`);
|
|
const data = await res.json();
|
|
setCustomModels(data.models || {});
|
|
} catch (error) {
|
|
console.error("Error fetching custom models:", error);
|
|
setCustomModels({});
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (isOpen) fetchCustomModels();
|
|
}, [isOpen]);
|
|
|
|
const allProviders = useMemo(
|
|
() => ({ ...OAUTH_PROVIDERS, ...FREE_PROVIDERS, ...APIKEY_PROVIDERS }),
|
|
[]
|
|
);
|
|
|
|
// Group models by provider with priority order
|
|
const groupedModels = useMemo(() => {
|
|
const groups: Record<string, any> = {};
|
|
|
|
// Get all active provider IDs from connections
|
|
const activeConnectionIds = activeProviders.map((p) => p.provider);
|
|
|
|
// Only show connected providers (including both standard and custom)
|
|
const providerIdsToShow = new Set([
|
|
...activeConnectionIds, // Only connected providers
|
|
]);
|
|
|
|
// Sort by PROVIDER_ORDER
|
|
const sortedProviderIds = [...providerIdsToShow].sort((a, b) => {
|
|
const indexA = PROVIDER_ORDER.indexOf(a);
|
|
const indexB = PROVIDER_ORDER.indexOf(b);
|
|
return (indexA === -1 ? 999 : indexA) - (indexB === -1 ? 999 : indexB);
|
|
});
|
|
|
|
sortedProviderIds.forEach((providerId) => {
|
|
const alias = PROVIDER_ID_TO_ALIAS[providerId] || providerId;
|
|
const providerInfo = allProviders[providerId] || { name: providerId, color: "#666" };
|
|
const isCustomProvider =
|
|
isOpenAICompatibleProvider(providerId) || isAnthropicCompatibleProvider(providerId);
|
|
|
|
// Get user-added custom models for this provider (if any)
|
|
const providerCustomModels = customModels[providerId] || [];
|
|
|
|
if (providerInfo.passthroughModels) {
|
|
const aliasModels = Object.entries(modelAliases as Record<string, string>)
|
|
.filter(([, fullModel]: [string, string]) => fullModel.startsWith(`${alias}/`))
|
|
.map(([aliasName, fullModel]: [string, string]) => ({
|
|
id: fullModel.replace(`${alias}/`, ""),
|
|
name: aliasName,
|
|
value: fullModel,
|
|
}));
|
|
|
|
// Merge custom models for passthrough providers
|
|
const customEntries = providerCustomModels
|
|
.filter((cm) => !aliasModels.some((am) => am.id === cm.id))
|
|
.map((cm) => ({
|
|
id: cm.id,
|
|
name: cm.name || cm.id,
|
|
value: `${alias}/${cm.id}`,
|
|
isCustom: true,
|
|
}));
|
|
|
|
const allModels = [...aliasModels, ...customEntries];
|
|
|
|
if (allModels.length > 0) {
|
|
const matchedNode = providerNodes.find((node) => node.id === providerId);
|
|
const displayName = matchedNode?.name || providerInfo.name;
|
|
|
|
groups[providerId] = {
|
|
name: displayName,
|
|
alias: alias,
|
|
color: providerInfo.color,
|
|
models: allModels,
|
|
};
|
|
}
|
|
} else if (isCustomProvider) {
|
|
const matchedNode = providerNodes.find((node) => node.id === providerId);
|
|
const displayName = matchedNode?.name || providerInfo.name;
|
|
const nodePrefix = matchedNode?.prefix || providerId; // Consider a more user-friendly fallback if providerId is a UUID
|
|
|
|
const nodeModels = Object.entries(modelAliases as Record<string, string>)
|
|
.filter(([, fullModel]: [string, string]) => fullModel.startsWith(`${providerId}/`))
|
|
.map(([aliasName, fullModel]: [string, string]) => ({
|
|
id: fullModel.replace(`${providerId}/`, ""),
|
|
name: aliasName,
|
|
value: `${nodePrefix}/${fullModel.replace(`${providerId}/`, "")}`,
|
|
}));
|
|
|
|
// Merge custom models for custom providers
|
|
const customEntries = providerCustomModels
|
|
.filter((cm) => !nodeModels.some((nm) => nm.id === cm.id))
|
|
.map((cm) => ({
|
|
id: cm.id,
|
|
name: cm.name || cm.id,
|
|
value: `${nodePrefix}/${cm.id}`,
|
|
isCustom: true,
|
|
}));
|
|
|
|
const allModels = [...nodeModels, ...customEntries];
|
|
|
|
if (allModels.length > 0) {
|
|
groups[providerId] = {
|
|
name: displayName,
|
|
alias: nodePrefix,
|
|
color: providerInfo.color,
|
|
models: allModels,
|
|
isCustom: true,
|
|
hasModels: true,
|
|
};
|
|
}
|
|
} else {
|
|
const systemModels = getModelsByProviderId(providerId);
|
|
|
|
// Merge system models with user-added custom models
|
|
const systemEntries = systemModels.map((m) => ({
|
|
id: m.id,
|
|
name: m.name,
|
|
value: `${alias}/${m.id}`,
|
|
}));
|
|
|
|
const customEntries = providerCustomModels
|
|
.filter((cm) => !systemModels.some((sm) => sm.id === cm.id))
|
|
.map((cm) => ({
|
|
id: cm.id,
|
|
name: cm.name || cm.id,
|
|
value: `${alias}/${cm.id}`,
|
|
isCustom: true,
|
|
}));
|
|
|
|
const allModels = [...systemEntries, ...customEntries];
|
|
|
|
if (allModels.length > 0) {
|
|
groups[providerId] = {
|
|
name: providerInfo.name,
|
|
alias: alias,
|
|
color: providerInfo.color,
|
|
models: allModels,
|
|
};
|
|
}
|
|
}
|
|
});
|
|
|
|
return groups;
|
|
}, [activeProviders, modelAliases, allProviders, providerNodes, customModels]);
|
|
|
|
// Filter combos by search query
|
|
const filteredCombos = useMemo(() => {
|
|
if (!searchQuery.trim()) return combos;
|
|
const query = searchQuery.toLowerCase();
|
|
return combos.filter((c) => c.name.toLowerCase().includes(query));
|
|
}, [combos, searchQuery]);
|
|
|
|
// Filter models by search query
|
|
const filteredGroups = useMemo(() => {
|
|
if (!searchQuery.trim()) return groupedModels;
|
|
|
|
const query = searchQuery.toLowerCase();
|
|
const filtered: Record<string, any> = {};
|
|
|
|
Object.entries(groupedModels).forEach(([providerId, group]: [string, any]) => {
|
|
const matchedModels = group.models.filter(
|
|
(m) => m.name.toLowerCase().includes(query) || m.id.toLowerCase().includes(query)
|
|
);
|
|
|
|
const providerNameMatches = group.name.toLowerCase().includes(query);
|
|
|
|
if (matchedModels.length > 0 || providerNameMatches) {
|
|
filtered[providerId] = {
|
|
...group,
|
|
models: matchedModels,
|
|
};
|
|
}
|
|
});
|
|
|
|
return filtered;
|
|
}, [groupedModels, searchQuery]);
|
|
|
|
const handleSelect = (model: any) => {
|
|
onSelect(model);
|
|
onClose();
|
|
setSearchQuery("");
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
isOpen={isOpen}
|
|
onClose={() => {
|
|
onClose();
|
|
setSearchQuery("");
|
|
}}
|
|
title={title}
|
|
size="md"
|
|
className="p-4!"
|
|
>
|
|
{/* Search - compact */}
|
|
<div className="mb-3">
|
|
<div className="relative">
|
|
<span className="material-symbols-outlined absolute left-2.5 top-1/2 -translate-y-1/2 text-text-muted text-[16px]">
|
|
search
|
|
</span>
|
|
<input
|
|
type="text"
|
|
placeholder="Search..."
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="w-full pl-8 pr-3 py-1.5 bg-surface border border-border rounded text-xs focus:outline-none focus:ring-1 focus:ring-primary/50"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Models grouped by provider - compact */}
|
|
<div className="max-h-[300px] overflow-y-auto space-y-3">
|
|
{/* Combos section - always first */}
|
|
{filteredCombos.length > 0 && (
|
|
<div>
|
|
<div className="flex items-center gap-1.5 mb-1.5 sticky top-0 bg-surface py-0.5">
|
|
<span className="material-symbols-outlined text-primary text-[14px]">layers</span>
|
|
<span className="text-xs font-medium text-primary">Combos</span>
|
|
<span className="text-[10px] text-text-muted">({filteredCombos.length})</span>
|
|
</div>
|
|
<div className="flex flex-wrap gap-1.5">
|
|
{filteredCombos.map((combo) => {
|
|
const isSelected = selectedModel === combo.name;
|
|
return (
|
|
<button
|
|
key={combo.id}
|
|
onClick={() =>
|
|
handleSelect({ id: combo.name, name: combo.name, value: combo.name })
|
|
}
|
|
className={`
|
|
px-2 py-1 rounded-xl text-xs font-medium transition-all border hover:cursor-pointer
|
|
${
|
|
isSelected
|
|
? "bg-primary text-white border-primary"
|
|
: "bg-surface border-border text-text-main hover:border-primary/50 hover:bg-primary/5"
|
|
}
|
|
`}
|
|
>
|
|
{combo.name}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Provider models */}
|
|
{Object.entries(filteredGroups).map(([providerId, group]: [string, any]) => (
|
|
<div key={providerId}>
|
|
{/* Provider header */}
|
|
<div className="flex items-center gap-1.5 mb-1.5 sticky top-0 bg-surface py-0.5">
|
|
<div className="w-2 h-2 rounded-full" style={{ backgroundColor: group.color }} />
|
|
<span className="text-xs font-medium text-primary">{group.name}</span>
|
|
<span className="text-[10px] text-text-muted">({group.models.length})</span>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap gap-1.5">
|
|
{group.models.map((model) => {
|
|
const isSelected = selectedModel === model.value;
|
|
const isAdded = addedModelValues.includes(model.value);
|
|
return (
|
|
<button
|
|
key={model.id}
|
|
onClick={() => handleSelect(model)}
|
|
className={`
|
|
px-2 py-1 rounded-xl text-xs font-medium transition-all border hover:cursor-pointer
|
|
${
|
|
isSelected
|
|
? "bg-primary text-white border-primary"
|
|
: isAdded
|
|
? "bg-emerald-500/15 border-emerald-500/30 text-emerald-700 dark:text-emerald-400"
|
|
: "bg-surface border-border text-text-main hover:border-primary/50 hover:bg-primary/5"
|
|
}
|
|
`}
|
|
>
|
|
{isAdded && <span className="mr-0.5 opacity-70">✓</span>}
|
|
{model.name}
|
|
{model.isCustom ? " ★" : ""}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
))}
|
|
|
|
{Object.keys(filteredGroups).length === 0 && filteredCombos.length === 0 && (
|
|
<div className="text-center py-4 text-text-muted">
|
|
<span className="material-symbols-outlined text-2xl mb-1 block">search_off</span>
|
|
<p className="text-xs">No models found</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
ModelSelectModal.propTypes = {
|
|
isOpen: PropTypes.bool.isRequired,
|
|
onClose: PropTypes.func.isRequired,
|
|
onSelect: PropTypes.func.isRequired,
|
|
selectedModel: PropTypes.string,
|
|
activeProviders: PropTypes.arrayOf(
|
|
PropTypes.shape({
|
|
provider: PropTypes.string.isRequired,
|
|
})
|
|
),
|
|
title: PropTypes.string,
|
|
modelAliases: PropTypes.object,
|
|
addedModelValues: PropTypes.arrayOf(PropTypes.string),
|
|
};
|