Initial commit

This commit is contained in:
decolua
2026-01-05 09:58:59 +07:00
commit 3857598de4
159 changed files with 14537 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
"use client";
import { cn } from "@/shared/utils/cn";
export default function Avatar({
src,
alt = "Avatar",
name,
size = "md",
className,
}) {
const sizes = {
xs: "size-6 text-xs",
sm: "size-8 text-sm",
md: "size-10 text-base",
lg: "size-12 text-lg",
xl: "size-16 text-xl",
};
// Get initials from name
const getInitials = (name) => {
if (!name) return "?";
const parts = name.split(" ");
if (parts.length >= 2) {
return `${parts[0][0]}${parts[1][0]}`.toUpperCase();
}
return name.substring(0, 2).toUpperCase();
};
// Generate color from name
const getColorFromName = (name) => {
if (!name) return "bg-primary";
const colors = [
"bg-red-500",
"bg-orange-500",
"bg-amber-500",
"bg-yellow-500",
"bg-lime-500",
"bg-green-500",
"bg-emerald-500",
"bg-teal-500",
"bg-cyan-500",
"bg-sky-500",
"bg-blue-500",
"bg-indigo-500",
"bg-violet-500",
"bg-purple-500",
"bg-fuchsia-500",
"bg-pink-500",
"bg-rose-500",
];
const index = name.charCodeAt(0) % colors.length;
return colors[index];
};
if (src) {
return (
<div
className={cn(
"rounded-full bg-cover bg-center bg-no-repeat",
"ring-2 ring-white dark:ring-surface-dark shadow-sm",
sizes[size],
className
)}
style={{ backgroundImage: `url(${src})` }}
role="img"
aria-label={alt}
/>
);
}
return (
<div
className={cn(
"rounded-full flex items-center justify-center font-semibold text-white",
"ring-2 ring-white dark:ring-surface-dark shadow-sm",
sizes[size],
getColorFromName(name),
className
)}
role="img"
aria-label={alt}
>
{getInitials(name)}
</div>
);
}
+55
View File
@@ -0,0 +1,55 @@
"use client";
import { cn } from "@/shared/utils/cn";
const variants = {
default: "bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300",
primary: "bg-primary/10 text-primary",
success: "bg-green-50 dark:bg-green-900/20 text-green-700 dark:text-green-400 border border-green-100 dark:border-green-800/30",
warning: "bg-yellow-50 dark:bg-yellow-900/20 text-yellow-700 dark:text-yellow-500 border border-yellow-100 dark:border-yellow-800/30",
error: "bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-400 border border-red-100 dark:border-red-800/30",
info: "bg-blue-50 dark:bg-blue-900/20 text-blue-700 dark:text-blue-400 border border-blue-100 dark:border-blue-800/30",
};
const sizes = {
sm: "px-2 py-0.5 text-[10px]",
md: "px-2.5 py-1 text-xs",
lg: "px-3 py-1.5 text-sm",
};
export default function Badge({
children,
variant = "default",
size = "md",
dot = false,
icon,
className,
}) {
return (
<span
className={cn(
"inline-flex items-center gap-1.5 rounded-full font-semibold",
variants[variant],
sizes[size],
className
)}
>
{dot && (
<span
className={cn(
"size-1.5 rounded-full",
variant === "success" && "bg-green-500",
variant === "warning" && "bg-yellow-500",
variant === "error" && "bg-red-500",
variant === "info" && "bg-blue-500",
variant === "primary" && "bg-primary",
variant === "default" && "bg-gray-500"
)}
/>
)}
{icon && <span className="material-symbols-outlined text-[14px]">{icon}</span>}
{children}
</span>
);
}
+56
View File
@@ -0,0 +1,56 @@
"use client";
import { cn } from "@/shared/utils/cn";
const variants = {
primary: "bg-primary text-white hover:bg-primary-hover shadow-warm",
secondary: "bg-surface border border-border text-text-main hover:bg-black/5 shadow-sm",
outline: "border border-border text-text-main hover:bg-black/5",
ghost: "text-text-muted hover:bg-black/5 hover:text-text-main",
danger: "bg-red-500 text-white hover:bg-red-600 shadow-sm",
};
const sizes = {
sm: "h-8 px-3 text-xs rounded-md",
md: "h-10 px-5 text-sm rounded-lg",
lg: "h-12 px-8 text-base rounded-xl",
};
export default function Button({
children,
variant = "primary",
size = "md",
icon,
iconRight,
disabled = false,
loading = false,
fullWidth = false,
className,
...props
}) {
return (
<button
className={cn(
"inline-flex items-center justify-center gap-2 font-medium transition-all duration-200 cursor-pointer",
"active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed disabled:active:scale-100",
variants[variant],
sizes[size],
fullWidth && "w-full",
className
)}
disabled={disabled || loading}
{...props}
>
{loading ? (
<span className="material-symbols-outlined animate-spin text-[18px]">progress_activity</span>
) : icon ? (
<span className="material-symbols-outlined text-[18px]">{icon}</span>
) : null}
{children}
{iconRight && !loading && (
<span className="material-symbols-outlined text-[18px]">{iconRight}</span>
)}
</button>
);
}
+92
View File
@@ -0,0 +1,92 @@
"use client";
import { cn } from "@/shared/utils/cn";
export default function Card({
children,
title,
subtitle,
icon,
action,
padding = "md",
hover = false,
className,
...props
}) {
const paddings = {
none: "",
sm: "p-4",
md: "p-6",
lg: "p-8",
};
return (
<div
className={cn(
"bg-surface",
"border border-border",
"rounded-xl shadow-soft",
hover && "hover:shadow-warm hover:border-primary/30 transition-all cursor-pointer",
paddings[padding],
className
)}
{...props}
>
{(title || action) && (
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-3">
{icon && (
<div className="p-2 rounded-lg bg-bg text-text-muted">
<span className="material-symbols-outlined text-[20px]">{icon}</span>
</div>
)}
<div>
{title && (
<h3 className="text-text-main font-semibold">{title}</h3>
)}
{subtitle && (
<p className="text-sm text-text-muted">{subtitle}</p>
)}
</div>
</div>
{action}
</div>
)}
{children}
</div>
);
}
// Sub-component: Bordered section inside Card
Card.Section = function CardSection({ children, className, ...props }) {
return (
<div
className={cn(
"p-4 rounded-lg",
"bg-surface",
"border border-border",
className
)}
{...props}
>
{children}
</div>
);
};
// Sub-component: Hoverable row inside Card
Card.Row = function CardRow({ children, className, ...props }) {
return (
<div
className={cn(
"p-3 -mx-3 px-3 border-b border-border last:border-b-0 transition-colors",
"hover:bg-sidebar",
className
)}
{...props}
>
{children}
</div>
);
};
+132
View File
@@ -0,0 +1,132 @@
"use client";
import Link from "next/link";
import { APP_CONFIG } from "@/shared/constants/config";
const footerLinks = {
product: [
{ label: "Features", href: "#features" },
{ label: "Pricing", href: "#pricing" },
{ label: "Changelog", href: "#" },
],
resources: [
{ label: "Documentation", href: "#" },
{ label: "API Reference", href: "#" },
{ label: "Help Center", href: "#" },
],
company: [
{ label: "About", href: "#" },
{ label: "Blog", href: "#" },
{ label: "Contact", href: "#" },
],
};
export default function Footer() {
return (
<footer className="bg-bg border-t border-border pt-16 pb-12">
<div className="container mx-auto px-4 sm:px-6 lg:px-8">
<div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-5 gap-10 mb-12">
{/* Brand */}
<div className="col-span-2 lg:col-span-2">
<div className="flex items-center gap-2 mb-6">
<div className="size-6 text-primary">
<svg className="w-full h-full" fill="currentColor" viewBox="0 0 48 48">
<path
clipRule="evenodd"
d="M12.0799 24L4 19.2479L9.95537 8.75216L18.04 13.4961L18.0446 4H29.9554L29.96 13.4961L38.0446 8.75216L44 19.2479L35.92 24L44 28.7521L38.0446 39.2479L29.96 34.5039L29.9554 44H18.0446L18.04 34.5039L9.95537 39.2479L4 28.7521L12.0799 24Z"
fillRule="evenodd"
/>
</svg>
</div>
<span className="text-xl font-bold text-text-main">
{APP_CONFIG.name}
</span>
</div>
<p className="text-text-muted mb-6 max-w-sm font-light">
The unified interface for modern AI infrastructure. Secure, observable, and scalable.
</p>
{/* Social links */}
<div className="flex gap-4">
<a
href="#"
className="text-gray-400 hover:text-primary transition-colors"
aria-label="Twitter"
>
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
<path d="M22.46 6c-.77.35-1.6.58-2.46.69.88-.53 1.56-1.37 1.88-2.38-.83.5-1.75.85-2.72 1.05C18.37 4.5 17.26 4 16 4c-2.35 0-4.27 1.92-4.27 4.29 0 .34.04.67.11.98C8.28 9.09 5.11 7.38 3 4.79c-.37.63-.58 1.37-.58 2.15 0 1.49.75 2.81 1.91 3.56-.71 0-1.37-.2-1.95-.5v.03c0 2.08 1.48 3.82 3.44 4.21a4.22 4.22 0 0 1-1.93.07 4.28 4.28 0 0 0 4 2.98 8.521 8.521 0 0 1-5.33 1.84c-.34 0-.68-.02-1.02-.06C3.44 20.29 5.7 21 8.12 21 16 21 20.33 14.46 20.33 8.79c0-.19 0-.37-.01-.56.84-.6 1.56-1.36 2.14-2.23z" />
</svg>
</a>
<a
href="#"
className="text-gray-400 hover:text-primary transition-colors"
aria-label="GitHub"
>
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0 1 12 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0 0 22 12.017C22 6.484 17.522 2 12 2z" />
</svg>
</a>
</div>
</div>
{/* Product */}
<div>
<h4 className="font-semibold text-text-main mb-4">Product</h4>
<ul className="flex flex-col gap-3 text-sm text-text-muted font-light">
{footerLinks.product.map((link) => (
<li key={link.label}>
<Link href={link.href} className="hover:text-primary transition-colors">
{link.label}
</Link>
</li>
))}
</ul>
</div>
{/* Resources */}
<div>
<h4 className="font-semibold text-text-main mb-4">Resources</h4>
<ul className="flex flex-col gap-3 text-sm text-text-muted font-light">
{footerLinks.resources.map((link) => (
<li key={link.label}>
<Link href={link.href} className="hover:text-primary transition-colors">
{link.label}
</Link>
</li>
))}
</ul>
</div>
{/* Company */}
<div>
<h4 className="font-semibold text-text-main mb-4">Company</h4>
<ul className="flex flex-col gap-3 text-sm text-text-muted font-light">
{footerLinks.company.map((link) => (
<li key={link.label}>
<Link href={link.href} className="hover:text-primary transition-colors">
{link.label}
</Link>
</li>
))}
</ul>
</div>
</div>
{/* Bottom */}
<div className="border-t border-border pt-8 flex flex-col md:flex-row justify-between items-center gap-4">
<p className="text-sm text-text-muted">
© {new Date().getFullYear()} {APP_CONFIG.name} Inc. All rights reserved.
</p>
<div className="flex gap-6 text-sm text-text-muted">
<Link href="#" className="hover:text-primary transition-colors">
Privacy Policy
</Link>
<Link href="#" className="hover:text-primary transition-colors">
Terms of Service
</Link>
</div>
</div>
</div>
</footer>
);
}
+109
View File
@@ -0,0 +1,109 @@
"use client";
import { usePathname } from "next/navigation";
import Link from "next/link";
import { ThemeToggle } from "@/shared/components";
import { APP_CONFIG, OAUTH_PROVIDERS, APIKEY_PROVIDERS } from "@/shared/constants/config";
const getPageInfo = (pathname) => {
if (!pathname) return { title: "", description: "", breadcrumbs: [] };
// Provider detail page: /dashboard/providers/[id]
const providerMatch = pathname.match(/\/providers\/([^/]+)$/);
if (providerMatch) {
const providerId = providerMatch[1];
const providerInfo = OAUTH_PROVIDERS[providerId] || APIKEY_PROVIDERS[providerId];
if (providerInfo) {
return {
title: providerInfo.name,
description: "",
breadcrumbs: [
{ label: "Providers", href: "/dashboard/providers" },
{ label: providerInfo.name, image: `/providers/${providerInfo.id}.png` }
]
};
}
}
if (pathname.includes("/providers")) return { title: "Providers", description: "Manage your AI provider connections", breadcrumbs: [] };
if (pathname.includes("/combos")) return { title: "Combos", description: "Model combos with fallback", breadcrumbs: [] };
if (pathname.includes("/cli-tools")) return { title: "CLI Tools", description: "Configure CLI tools", breadcrumbs: [] };
if (pathname.includes("/endpoint")) return { title: "Endpoint", description: "API endpoint configuration", breadcrumbs: [] };
if (pathname.includes("/profile")) return { title: "Settings", description: "Manage your preferences", breadcrumbs: [] };
if (pathname === "/dashboard") return { title: "Endpoint", description: "API endpoint configuration", breadcrumbs: [] };
return { title: "", description: "", breadcrumbs: [] };
};
export default function Header({ onMenuClick, showMenuButton = true }) {
const pathname = usePathname();
const { title, description, breadcrumbs } = getPageInfo(pathname);
return (
<header className="flex items-center justify-between px-8 py-5 border-b border-border bg-bg/80 backdrop-blur-md z-10 sticky top-0">
{/* Mobile menu button */}
<div className="flex items-center gap-3 lg:hidden">
{showMenuButton && (
<button
onClick={onMenuClick}
className="text-text-main hover:text-primary transition-colors"
>
<span className="material-symbols-outlined">menu</span>
</button>
)}
</div>
{/* Page title with breadcrumbs - desktop */}
<div className="hidden lg:flex flex-col">
{breadcrumbs.length > 0 ? (
<div className="flex items-center gap-2">
{breadcrumbs.map((crumb, index) => (
<div key={index} className="flex items-center gap-2">
{index > 0 && (
<span className="material-symbols-outlined text-text-muted text-base">
chevron_right
</span>
)}
{crumb.href ? (
<Link
href={crumb.href}
className="text-text-muted hover:text-primary transition-colors"
>
{crumb.label}
</Link>
) : (
<div className="flex items-center gap-2">
{crumb.image && (
<img
src={crumb.image}
alt={crumb.label}
className="size-7 object-contain rounded"
onError={(e) => { e.target.style.display = "none"; }}
/>
)}
<h1 className="text-2xl font-semibold text-text-main tracking-tight">
{crumb.label}
</h1>
</div>
)}
</div>
))}
</div>
) : title ? (
<div>
<h1 className="text-2xl font-semibold text-text-main tracking-tight">{title}</h1>
{description && (
<p className="text-sm text-text-muted">{description}</p>
)}
</div>
) : null}
</div>
{/* Right actions */}
<div className="flex items-center gap-3 ml-auto">
{/* Theme toggle */}
<ThemeToggle />
</div>
</header>
);
}
+69
View File
@@ -0,0 +1,69 @@
"use client";
import { cn } from "@/shared/utils/cn";
export default function Input({
label,
type = "text",
placeholder,
value,
onChange,
error,
hint,
icon,
disabled = false,
required = false,
className,
inputClassName,
...props
}) {
return (
<div className={cn("flex flex-col gap-1.5", className)}>
{label && (
<label className="text-sm font-medium text-text-main">
{label}
{required && <span className="text-red-500 ml-1">*</span>}
</label>
)}
<div className="relative">
{icon && (
<div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-text-muted">
<span className="material-symbols-outlined text-[20px]">{icon}</span>
</div>
)}
<input
type={type}
placeholder={placeholder}
value={value}
onChange={onChange}
disabled={disabled}
className={cn(
"w-full py-2.5 px-4 text-sm text-text-main",
"bg-surface border rounded-lg",
"placeholder-text-muted/60",
"focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none",
"transition-all shadow-sm disabled:opacity-50 disabled:cursor-not-allowed",
// iOS zoom fix
"text-[16px] sm:text-sm",
icon && "pl-10",
error
? "border-red-500 focus:border-red-500 focus:ring-red-500/20"
: "border-border",
inputClassName
)}
{...props}
/>
</div>
{error && (
<p className="text-xs text-red-500 flex items-center gap-1">
<span className="material-symbols-outlined text-[14px]">error</span>
{error}
</p>
)}
{hint && !error && (
<p className="text-xs text-text-muted">{hint}</p>
)}
</div>
);
}
+77
View File
@@ -0,0 +1,77 @@
"use client";
import { cn } from "@/shared/utils/cn";
// Spinner loading
export function Spinner({ size = "md", className }) {
const sizes = {
sm: "size-4",
md: "size-6",
lg: "size-8",
xl: "size-12",
};
return (
<span
className={cn(
"material-symbols-outlined animate-spin text-primary",
sizes[size],
className
)}
>
progress_activity
</span>
);
}
// Full page loading
export function PageLoading({ message = "Loading..." }) {
return (
<div className="fixed inset-0 z-50 flex flex-col items-center justify-center bg-bg">
<Spinner size="xl" />
<p className="mt-4 text-text-muted">{message}</p>
</div>
);
}
// Skeleton loading
export function Skeleton({ className, ...props }) {
return (
<div
className={cn(
"animate-pulse rounded-lg bg-border",
className
)}
{...props}
/>
);
}
// Card skeleton
export function CardSkeleton() {
return (
<div className="p-6 rounded-xl border border-border bg-surface">
<div className="flex items-center justify-between mb-4">
<Skeleton className="h-4 w-24" />
<Skeleton className="size-10 rounded-lg" />
</div>
<Skeleton className="h-8 w-16 mb-2" />
<Skeleton className="h-3 w-20" />
</div>
);
}
// Default export
export default function Loading({ type = "spinner", ...props }) {
switch (type) {
case "page":
return <PageLoading {...props} />;
case "skeleton":
return <Skeleton {...props} />;
case "card":
return <CardSkeleton {...props} />;
default:
return <Spinner {...props} />;
}
}
+136
View File
@@ -0,0 +1,136 @@
"use client";
import { useEffect } from "react";
import { cn } from "@/shared/utils/cn";
import Button from "./Button";
export default function Modal({
isOpen,
onClose,
title,
children,
footer,
size = "md",
closeOnOverlay = true,
showCloseButton = true,
className,
}) {
const sizes = {
sm: "max-w-sm",
md: "max-w-md",
lg: "max-w-lg",
xl: "max-w-xl",
full: "max-w-4xl",
};
// Lock body scroll when modal is open
useEffect(() => {
if (isOpen) {
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = "";
}
return () => {
document.body.style.overflow = "";
};
}, [isOpen]);
// Handle escape key
useEffect(() => {
const handleEscape = (e) => {
if (e.key === "Escape" && isOpen) {
onClose();
}
};
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}, [isOpen, onClose]);
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
{/* Overlay */}
<div
className="absolute inset-0 bg-black/50 backdrop-blur-sm"
onClick={closeOnOverlay ? onClose : undefined}
/>
{/* Modal content */}
<div
className={cn(
"relative w-full bg-surface",
"border border-border",
"rounded-2xl shadow-elevated",
"animate-in fade-in zoom-in-95 duration-200",
sizes[size],
className
)}
>
{/* Header */}
{(title || showCloseButton) && (
<div className="flex items-center justify-between p-6 border-b border-border">
{title && (
<h2 className="text-lg font-semibold text-text-main">
{title}
</h2>
)}
{showCloseButton && (
<button
onClick={onClose}
className="p-1.5 rounded-lg text-text-muted hover:bg-black/5 transition-colors"
>
<span className="material-symbols-outlined text-[20px]">close</span>
</button>
)}
</div>
)}
{/* Body */}
<div className="p-6 max-h-[calc(80vh-140px)] overflow-y-auto">{children}</div>
{/* Footer */}
{footer && (
<div className="flex items-center justify-end gap-3 p-6 border-t border-border">
{footer}
</div>
)}
</div>
</div>
);
}
// Confirm Modal helper
export function ConfirmModal({
isOpen,
onClose,
onConfirm,
title = "Confirm",
message,
confirmText = "Confirm",
cancelText = "Cancel",
variant = "danger",
loading = false,
}) {
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={title}
size="sm"
footer={
<>
<Button variant="ghost" onClick={onClose} disabled={loading}>
{cancelText}
</Button>
<Button variant={variant} onClick={onConfirm} loading={loading}>
{confirmText}
</Button>
</>
}
>
<p className="text-text-muted">{message}</p>
</Modal>
);
}
+183
View File
@@ -0,0 +1,183 @@
"use client";
import { useState, useMemo } from "react";
import Modal from "./Modal";
import { getModelsByProviderId, PROVIDER_ID_TO_ALIAS } from "@/shared/constants/models";
import { AI_PROVIDERS } from "@/shared/constants/providers";
export default function ModelSelectModal({
isOpen,
onClose,
onSelect,
selectedModel,
activeProviders = [],
title = "Select Model",
modelAliases = {},
}) {
const [searchQuery, setSearchQuery] = useState("");
// Group models by provider
const groupedModels = useMemo(() => {
const groups = {};
// Get active provider IDs
const activeProviderIds = activeProviders.length > 0
? activeProviders.map(p => p.provider)
: Object.keys(AI_PROVIDERS);
activeProviderIds.forEach((providerId) => {
const alias = PROVIDER_ID_TO_ALIAS[providerId] || providerId;
const providerInfo = AI_PROVIDERS[providerId] || { name: providerId, color: "#666" };
// For passthrough providers, get models from aliases
if (providerInfo.passthroughModels) {
const aliasModels = Object.entries(modelAliases)
.filter(([, fullModel]) => fullModel.startsWith(`${alias}/`))
.map(([aliasName, fullModel]) => ({
id: fullModel.replace(`${alias}/`, ""),
name: aliasName,
value: fullModel,
}));
if (aliasModels.length > 0) {
groups[providerId] = {
name: providerInfo.name,
alias: alias,
color: providerInfo.color,
models: aliasModels,
};
}
} else {
const models = getModelsByProviderId(providerId);
if (models.length > 0) {
groups[providerId] = {
name: providerInfo.name,
alias: alias,
color: providerInfo.color,
models: models.map((m) => ({
id: m.id,
name: m.name,
value: `${alias}/${m.id}`,
})),
};
}
}
});
return groups;
}, [activeProviders, modelAliases]);
// Filter models by search query
const filteredGroups = useMemo(() => {
if (!searchQuery.trim()) return groupedModels;
const query = searchQuery.toLowerCase();
const filtered = {};
Object.entries(groupedModels).forEach(([providerId, group]) => {
const matchedModels = group.models.filter(
(m) =>
m.name.toLowerCase().includes(query) ||
m.id.toLowerCase().includes(query) ||
group.name.toLowerCase().includes(query)
);
if (matchedModels.length > 0) {
filtered[providerId] = {
...group,
models: matchedModels,
};
}
});
return filtered;
}, [groupedModels, searchQuery]);
const handleSelect = (model) => {
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">
{Object.entries(filteredGroups).map(([providerId, group]) => (
<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>
{/* Models as wrap chips - compact */}
<div className="flex flex-wrap gap-1.5">
{group.models.map((model) => {
const isSelected = selectedModel === 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"
: "bg-surface border-border text-text-main hover:border-primary/50 hover:bg-primary/5"
}
`}
>
{model.name}
</button>
);
})}
</div>
</div>
))}
{Object.keys(filteredGroups).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>
);
}
+419
View File
@@ -0,0 +1,419 @@
"use client";
import { useState, useEffect, useRef, useCallback } from "react";
import { Modal, Button, Input } from "@/shared/components";
import { useCopyToClipboard } from "@/shared/hooks/useCopyToClipboard";
/**
* OAuth Modal Component
* - Localhost: Auto callback via popup message
* - Remote: Manual paste callback URL
*/
export default function OAuthModal({ isOpen, provider, providerInfo, onSuccess, onClose }) {
const [step, setStep] = useState("waiting"); // waiting | input | success | error
const [authData, setAuthData] = useState(null);
const [callbackUrl, setCallbackUrl] = useState("");
const [error, setError] = useState(null);
const [isDeviceCode, setIsDeviceCode] = useState(false);
const [deviceData, setDeviceData] = useState(null);
const [polling, setPolling] = useState(false);
const popupRef = useRef(null);
const { copied, copy } = useCopyToClipboard();
// Detect if running on localhost
const isLocalhost = typeof window !== "undefined" &&
(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
// Reset state and start OAuth when modal opens
useEffect(() => {
if (isOpen && provider) {
setAuthData(null);
setCallbackUrl("");
setError(null);
setIsDeviceCode(false);
setDeviceData(null);
setPolling(false);
// Auto start OAuth
startOAuthFlow();
}
}, [isOpen, provider]);
// Listen for OAuth callback via multiple methods
const callbackProcessedRef = useRef(false);
useEffect(() => {
if (!authData) return;
callbackProcessedRef.current = false; // Reset when authData changes
// Handler for callback data - only process once
const handleCallback = async (data) => {
if (callbackProcessedRef.current) return; // Already processed
const { code, state, error: callbackError, errorDescription } = data;
if (callbackError) {
callbackProcessedRef.current = true;
setError(errorDescription || callbackError);
setStep("error");
return;
}
if (code) {
callbackProcessedRef.current = true;
await exchangeTokens(code, state);
}
};
// Method 1: postMessage from popup
const handleMessage = (event) => {
if (event.origin !== window.location.origin) return;
if (event.data?.type === "oauth_callback") {
handleCallback(event.data.data);
}
};
window.addEventListener("message", handleMessage);
// Method 2: BroadcastChannel
let channel;
try {
channel = new BroadcastChannel("oauth_callback");
channel.onmessage = (event) => handleCallback(event.data);
} catch (e) {
console.log("BroadcastChannel not supported");
}
// Method 3: localStorage event
const handleStorage = (event) => {
if (event.key === "oauth_callback" && event.newValue) {
try {
const data = JSON.parse(event.newValue);
handleCallback(data);
localStorage.removeItem("oauth_callback");
} catch (e) {
console.log("Failed to parse localStorage data");
}
}
};
window.addEventListener("storage", handleStorage);
// Also check localStorage on mount (in case callback already happened)
try {
const stored = localStorage.getItem("oauth_callback");
if (stored) {
const data = JSON.parse(stored);
// Only use if recent (within 30 seconds)
if (data.timestamp && Date.now() - data.timestamp < 30000) {
handleCallback(data);
localStorage.removeItem("oauth_callback");
}
}
} catch (e) {}
return () => {
window.removeEventListener("message", handleMessage);
window.removeEventListener("storage", handleStorage);
if (channel) channel.close();
};
}, [authData]);
// Exchange tokens
const exchangeTokens = async (code, state) => {
try {
const res = await fetch(`/api/oauth/${provider}/exchange`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
code,
redirectUri: authData.redirectUri,
codeVerifier: authData.codeVerifier,
state,
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error);
setStep("success");
onSuccess?.();
} catch (err) {
setError(err.message);
setStep("error");
}
};
// Start OAuth flow
const startOAuthFlow = async () => {
if (!provider) return;
try {
setError(null);
// Device code flow (GitHub, Qwen)
if (provider === "github" || provider === "qwen") {
setIsDeviceCode(true);
setStep("waiting");
const res = await fetch(`/api/oauth/${provider}/device-code`);
const data = await res.json();
if (!res.ok) throw new Error(data.error);
setDeviceData(data);
// Open verification URL
const verifyUrl = data.verification_uri_complete || data.verification_uri;
if (verifyUrl) window.open(verifyUrl, "_blank");
// Start polling
startPolling(data.device_code, data.codeVerifier, data.interval || 5);
return;
}
// Authorization code flow - always use localhost with current port (except Codex)
let redirectUri;
if (provider === "codex") {
// Codex requires fixed port 1455
redirectUri = "http://localhost:1455/auth/callback";
} else {
// Always use localhost with current port for OAuth callback
const port = window.location.port || (window.location.protocol === "https:" ? "443" : "80");
redirectUri = `http://localhost:${port}/callback`;
}
const res = await fetch(`/api/oauth/${provider}/authorize?redirect_uri=${encodeURIComponent(redirectUri)}`);
const data = await res.json();
if (!res.ok) throw new Error(data.error);
setAuthData({ ...data, redirectUri });
// For Codex, always use manual input since it requires fixed port 1455
if (provider === "codex") {
setStep("input");
window.open(data.authUrl, "_blank");
} else if (isLocalhost) {
// Other providers on localhost: Open popup and wait for message
setStep("waiting");
popupRef.current = window.open(data.authUrl, "oauth_popup", "width=600,height=700");
// Check if popup was blocked
if (!popupRef.current) {
setStep("input");
}
} else {
// Remote: Show manual input
setStep("input");
window.open(data.authUrl, "_blank");
}
} catch (err) {
setError(err.message);
setStep("error");
}
};
// Poll for device code token
const startPolling = async (deviceCode, codeVerifier, interval) => {
setPolling(true);
const maxAttempts = 60;
for (let i = 0; i < maxAttempts; i++) {
await new Promise((r) => setTimeout(r, interval * 1000));
try {
const res = await fetch(`/api/oauth/${provider}/poll`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ deviceCode, codeVerifier }),
});
const data = await res.json();
if (data.success) {
setStep("success");
setPolling(false);
onSuccess?.();
return;
}
if (data.error === "expired_token" || data.error === "access_denied") {
throw new Error(data.errorDescription || data.error);
}
if (data.error === "slow_down") {
interval = Math.min(interval + 5, 30);
}
} catch (err) {
setError(err.message);
setStep("error");
setPolling(false);
return;
}
}
setError("Authorization timeout");
setStep("error");
setPolling(false);
};
// Handle manual URL input
const handleManualSubmit = async () => {
try {
setError(null);
const url = new URL(callbackUrl);
const code = url.searchParams.get("code");
const state = url.searchParams.get("state");
const errorParam = url.searchParams.get("error");
if (errorParam) {
throw new Error(url.searchParams.get("error_description") || errorParam);
}
if (!code) {
throw new Error("No authorization code found in URL");
}
await exchangeTokens(code, state);
} catch (err) {
setError(err.message);
setStep("error");
}
};
if (!provider || !providerInfo) return null;
return (
<Modal isOpen={isOpen} title={`Connect ${providerInfo.name}`} onClose={onClose} size="lg">
<div className="flex flex-col gap-4">
{/* Waiting Step (Localhost - popup mode) */}
{step === "waiting" && !isDeviceCode && (
<div className="text-center py-6">
<div className="size-16 mx-auto mb-4 rounded-full bg-primary/10 flex items-center justify-center">
<span className="material-symbols-outlined text-3xl text-primary animate-spin">
progress_activity
</span>
</div>
<h3 className="text-lg font-semibold mb-2">Waiting for Authorization</h3>
<p className="text-sm text-text-muted mb-4">
Complete the authorization in the popup window.
</p>
<Button variant="ghost" onClick={() => setStep("input")}>
Popup blocked? Enter URL manually
</Button>
</div>
)}
{/* Device Code Flow - Waiting */}
{step === "waiting" && isDeviceCode && deviceData && (
<>
<div className="text-center py-4">
<p className="text-sm text-text-muted mb-4">
Visit the URL below and enter the code:
</p>
<div className="bg-sidebar p-4 rounded-lg mb-4">
<p className="text-xs text-text-muted mb-1">Verification URL</p>
<div className="flex items-center gap-2">
<code className="flex-1 text-sm break-all">{deviceData.verification_uri}</code>
<Button
size="sm"
variant="ghost"
icon={copied === "verify_url" ? "check" : "content_copy"}
onClick={() => copy(deviceData.verification_uri, "verify_url")}
/>
</div>
</div>
<div className="bg-primary/10 p-4 rounded-lg">
<p className="text-xs text-text-muted mb-1">Your Code</p>
<div className="flex items-center justify-center gap-2">
<p className="text-2xl font-mono font-bold text-primary">{deviceData.user_code}</p>
<Button
size="sm"
variant="ghost"
icon={copied === "user_code" ? "check" : "content_copy"}
onClick={() => copy(deviceData.user_code, "user_code")}
/>
</div>
</div>
</div>
{polling && (
<div className="flex items-center justify-center gap-2 text-sm text-text-muted">
<span className="material-symbols-outlined animate-spin">progress_activity</span>
Waiting for authorization...
</div>
)}
</>
)}
{/* Manual Input Step */}
{step === "input" && !isDeviceCode && (
<>
<div className="space-y-4">
<div>
<p className="text-sm font-medium mb-2">Step 1: Open this URL in your browser</p>
<div className="flex gap-2">
<Input value={authData?.authUrl || ""} readOnly className="flex-1 font-mono text-xs" />
<Button variant="secondary" icon={copied === "auth_url" ? "check" : "content_copy"} onClick={() => copy(authData?.authUrl, "auth_url")}>
Copy
</Button>
</div>
</div>
<div>
<p className="text-sm font-medium mb-2">Step 2: Paste the callback URL here</p>
<p className="text-xs text-text-muted mb-2">
After authorization, copy the full URL from your browser.
</p>
<Input
value={callbackUrl}
onChange={(e) => setCallbackUrl(e.target.value)}
placeholder={`${window.location.origin}/callback?code=...`}
className="font-mono text-xs"
/>
</div>
</div>
<div className="flex gap-2">
<Button onClick={handleManualSubmit} fullWidth disabled={!callbackUrl}>
Connect
</Button>
<Button onClick={onClose} variant="ghost" fullWidth>
Cancel
</Button>
</div>
</>
)}
{/* Success Step */}
{step === "success" && (
<div className="text-center py-6">
<div className="size-16 mx-auto mb-4 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center">
<span className="material-symbols-outlined text-3xl text-green-600">check_circle</span>
</div>
<h3 className="text-lg font-semibold mb-2">Connected Successfully!</h3>
<p className="text-sm text-text-muted mb-4">
Your {providerInfo.name} account has been connected.
</p>
<Button onClick={onClose} fullWidth>
Done
</Button>
</div>
)}
{/* Error Step */}
{step === "error" && (
<div className="text-center py-6">
<div className="size-16 mx-auto mb-4 rounded-full bg-red-100 dark:bg-red-900/30 flex items-center justify-center">
<span className="material-symbols-outlined text-3xl text-red-600">error</span>
</div>
<h3 className="text-lg font-semibold mb-2">Connection Failed</h3>
<p className="text-sm text-red-600 mb-4">{error}</p>
<div className="flex gap-2">
<Button onClick={startOAuthFlow} variant="secondary" fullWidth>
Try Again
</Button>
<Button onClick={onClose} variant="ghost" fullWidth>
Cancel
</Button>
</div>
</div>
)}
</div>
</Modal>
);
}
+70
View File
@@ -0,0 +1,70 @@
"use client";
import { cn } from "@/shared/utils/cn";
export default function Select({
label,
options = [],
value,
onChange,
placeholder = "Select an option",
error,
hint,
disabled = false,
required = false,
className,
selectClassName,
...props
}) {
return (
<div className={cn("flex flex-col gap-1.5", className)}>
{label && (
<label className="text-sm font-medium text-text-main">
{label}
{required && <span className="text-red-500 ml-1">*</span>}
</label>
)}
<div className="relative">
<select
value={value}
onChange={onChange}
disabled={disabled}
className={cn(
"w-full py-2.5 px-4 pr-10 text-sm text-text-main",
"bg-surface border rounded-lg appearance-none",
"focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none",
"transition-all shadow-sm disabled:opacity-50 disabled:cursor-not-allowed",
"text-[16px] sm:text-sm",
error
? "border-red-500 focus:border-red-500 focus:ring-red-500/20"
: "border-border",
selectClassName
)}
{...props}
>
<option value="" disabled>
{placeholder}
</option>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
<div className="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none text-text-muted">
<span className="material-symbols-outlined text-[20px]">expand_more</span>
</div>
</div>
{error && (
<p className="text-xs text-red-500 flex items-center gap-1">
<span className="material-symbols-outlined text-[14px]">error</span>
{error}
</p>
)}
{hint && !error && (
<p className="text-xs text-text-muted">{hint}</p>
)}
</div>
);
}
+171
View File
@@ -0,0 +1,171 @@
"use client";
import { useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/shared/utils/cn";
import { APP_CONFIG } from "@/shared/constants/config";
import Button from "./Button";
import { ConfirmModal } from "./Modal";
const navItems = [
{ href: "/dashboard/endpoint", label: "Endpoint", icon: "api" },
{ href: "/dashboard/providers", label: "Providers", icon: "dns" },
{ href: "/dashboard/combos", label: "Combos", icon: "layers" },
{ href: "/dashboard/cli-tools", label: "CLI Tools", icon: "terminal" },
];
const systemItems = [
{ href: "/dashboard/profile", label: "Settings", icon: "settings" },
];
export default function Sidebar({ onClose }) {
const pathname = usePathname();
const [showShutdownModal, setShowShutdownModal] = useState(false);
const [isShuttingDown, setIsShuttingDown] = useState(false);
const [isDisconnected, setIsDisconnected] = useState(false);
const isActive = (href) => {
if (href === "/dashboard/endpoint") {
return pathname === "/dashboard" || pathname.startsWith("/dashboard/endpoint");
}
return pathname.startsWith(href);
};
const handleShutdown = async () => {
setIsShuttingDown(true);
try {
await fetch("/api/shutdown", { method: "POST" });
} catch (e) {
// Expected to fail as server shuts down
}
setIsShuttingDown(false);
setShowShutdownModal(false);
setIsDisconnected(true);
};
return (
<>
<aside className="flex w-72 flex-col border-r border-border bg-sidebar transition-colors duration-300">
{/* Logo */}
<div className="p-8">
<Link href="/dashboard" className="flex items-center gap-3">
<div className="flex items-center justify-center size-9 rounded bg-gradient-to-br from-[#f97815] to-[#c2590a]">
<span className="material-symbols-outlined text-white text-[20px]">hub</span>
</div>
<h1 className="text-lg font-semibold tracking-tight text-text-main">
{APP_CONFIG.name}
</h1>
</Link>
</div>
{/* Navigation */}
<nav className="flex-1 px-4 py-2 space-y-1 overflow-y-auto custom-scrollbar">
{navItems.map((item) => (
<Link
key={item.href}
href={item.href}
onClick={onClose}
className={cn(
"flex items-center gap-3 px-4 py-3 rounded-lg transition-all group",
isActive(item.href)
? "bg-surface text-primary shadow-sm border border-border"
: "text-text-muted hover:bg-surface/50 hover:text-text-main"
)}
>
<span
className={cn(
"material-symbols-outlined text-[20px]",
isActive(item.href) ? "fill-1" : "group-hover:text-primary transition-colors"
)}
>
{item.icon}
</span>
<span className="text-sm font-medium">{item.label}</span>
</Link>
))}
{/* System section */}
<div className="pt-6 mt-2">
<p className="px-4 text-xs font-semibold text-text-muted/60 uppercase tracking-wider mb-3">
System
</p>
{systemItems.map((item) => (
<Link
key={item.href}
href={item.href}
onClick={onClose}
className={cn(
"flex items-center gap-3 px-4 py-3 rounded-lg transition-all group",
isActive(item.href)
? "bg-surface text-primary shadow-sm border border-border"
: "text-text-muted hover:bg-surface/50 hover:text-text-main"
)}
>
<span className="material-symbols-outlined text-[20px] group-hover:text-primary transition-colors">
{item.icon}
</span>
<span className="text-sm font-medium">{item.label}</span>
</Link>
))}
</div>
</nav>
{/* Footer section */}
<div className="p-4 border-t border-border">
{/* Info message */}
<div className="flex items-start gap-3 p-3 rounded-xl bg-surface border border-border mb-3">
<div className="flex items-center justify-center size-8 rounded-lg bg-blue-500/10 text-blue-500 shrink-0 mt-0.5">
<span className="material-symbols-outlined text-[18px]">info</span>
</div>
<div className="flex flex-col">
<span className="text-xs font-medium text-text-main leading-relaxed">
Service is running in terminal. You can close this web page. Shutdown will stop the service.
</span>
</div>
</div>
{/* Shutdown button */}
<Button
variant="outline"
fullWidth
icon="power_settings_new"
onClick={() => setShowShutdownModal(true)}
className="text-red-500 border-red-200 hover:bg-red-50 hover:border-red-300"
>
Shutdown
</Button>
</div>
</aside>
{/* Shutdown Confirmation Modal */}
<ConfirmModal
isOpen={showShutdownModal}
onClose={() => setShowShutdownModal(false)}
onConfirm={handleShutdown}
title="Close Proxy"
message="Are you sure you want to close the proxy server?"
confirmText="Close"
cancelText="Cancel"
variant="danger"
loading={isShuttingDown}
/>
{/* Disconnected Overlay */}
{isDisconnected && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm">
<div className="text-center p-8">
<div className="flex items-center justify-center size-16 rounded-full bg-red-500/20 text-red-500 mx-auto mb-4">
<span className="material-symbols-outlined text-[32px]">power_off</span>
</div>
<h2 className="text-xl font-semibold text-white mb-2">Server Disconnected</h2>
<p className="text-text-muted mb-6">The proxy server has been stopped.</p>
<Button variant="secondary" onClick={() => window.location.reload()}>
Reload Page
</Button>
</div>
</div>
)}
</>
);
}
+15
View File
@@ -0,0 +1,15 @@
"use client";
import { useEffect } from "react";
import useThemeStore from "@/store/themeStore";
export function ThemeProvider({ children }) {
const { initTheme } = useThemeStore();
useEffect(() => {
initTheme();
}, [initTheme]);
return <>{children}</>;
}
+47
View File
@@ -0,0 +1,47 @@
"use client";
import { useTheme } from "@/shared/hooks/useTheme";
import { cn } from "@/shared/utils/cn";
export default function ThemeToggle({ className, variant = "default" }) {
const { theme, toggleTheme, isDark } = useTheme();
const variants = {
default: cn(
"flex items-center justify-center size-10 rounded-full",
"text-text-muted",
"hover:bg-black/5",
"hover:text-text-main",
"transition-colors"
),
card: cn(
"flex items-center justify-center size-11 rounded-full",
"bg-surface/60",
"hover:bg-surface",
"border border-border",
"backdrop-blur-md shadow-sm hover:shadow-md",
"text-text-muted-light hover:text-primary",
"hover:text-primary",
"transition-all group"
),
};
return (
<button
onClick={toggleTheme}
className={cn(variants[variant], className)}
aria-label={`Switch to ${isDark ? "light" : "dark"} mode`}
title={`Switch to ${isDark ? "light" : "dark"} mode`}
>
<span
className={cn(
"material-symbols-outlined text-[22px]",
variant === "card" && "transition-transform duration-300 group-hover:rotate-12"
)}
>
{isDark ? "light_mode" : "dark_mode"}
</span>
</button>
);
}
+91
View File
@@ -0,0 +1,91 @@
"use client";
import { cn } from "@/shared/utils/cn";
export default function Toggle({
checked = false,
onChange,
label,
description,
disabled = false,
size = "md",
className,
}) {
const sizes = {
sm: {
track: "w-8 h-4",
thumb: "size-3",
translate: "translate-x-4",
},
md: {
track: "w-11 h-6",
thumb: "size-5",
translate: "translate-x-5",
},
lg: {
track: "w-14 h-7",
thumb: "size-6",
translate: "translate-x-7",
},
};
const handleClick = () => {
if (!disabled && onChange) {
onChange(!checked);
}
};
return (
<div
className={cn(
"flex items-center gap-3",
disabled && "opacity-50 cursor-not-allowed",
className
)}
>
<button
type="button"
role="switch"
aria-checked={checked}
disabled={disabled}
onClick={handleClick}
className={cn(
"relative inline-flex shrink-0 cursor-pointer rounded-full",
"transition-colors duration-200 ease-in-out",
"focus:outline-none focus:ring-2 focus:ring-primary/20 focus:ring-offset-2",
"dark:focus:ring-offset-surface-dark",
checked
? "bg-primary"
: "bg-border",
sizes[size].track,
disabled && "cursor-not-allowed"
)}
>
<span
className={cn(
"pointer-events-none inline-block rounded-full bg-white shadow-sm",
"transform transition duration-200 ease-in-out",
checked ? sizes[size].translate : "translate-x-0.5",
sizes[size].thumb,
"mt-0.5"
)}
/>
</button>
{(label || description) && (
<div className="flex flex-col">
{label && (
<span className="text-sm font-medium text-text-main">
{label}
</span>
)}
{description && (
<span className="text-xs text-text-muted">
{description}
</span>
)}
</div>
)}
</div>
);
}
+21
View File
@@ -0,0 +1,21 @@
// Shared Components - Export all
export { default as Button } from "./Button";
export { default as Input } from "./Input";
export { default as Select } from "./Select";
export { default as Card } from "./Card";
export { default as Modal, ConfirmModal } from "./Modal";
export { default as Loading, Spinner, PageLoading, Skeleton, CardSkeleton } from "./Loading";
export { default as Avatar } from "./Avatar";
export { default as Badge } from "./Badge";
export { default as Toggle } from "./Toggle";
export { default as ThemeToggle } from "./ThemeToggle";
export { ThemeProvider } from "./ThemeProvider";
export { default as Sidebar } from "./Sidebar";
export { default as Header } from "./Header";
export { default as Footer } from "./Footer";
export { default as OAuthModal } from "./OAuthModal";
export { default as ModelSelectModal } from "./ModelSelectModal";
// Layouts
export * from "./layouts";
@@ -0,0 +1,24 @@
"use client";
import { ThemeToggle } from "@/shared/components";
export default function AuthLayout({ children }) {
return (
<div className="min-h-screen flex flex-col relative bg-bg transition-colors duration-500 overflow-x-hidden selection:bg-primary/20 selection:text-primary">
{/* Background effects */}
<div className="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[800px] h-[800px] bg-primary/5 dark:bg-primary/5 rounded-full blur-[100px] pointer-events-none z-0" />
<div className="fixed bottom-0 right-0 w-[600px] h-[600px] bg-orange-200/20 dark:bg-orange-900/10 rounded-full blur-[120px] pointer-events-none z-0 translate-y-1/3 translate-x-1/3" />
{/* Theme toggle */}
<div className="absolute top-6 right-6 z-20">
<ThemeToggle variant="card" />
</div>
{/* Content */}
<main className="flex-1 flex flex-col items-center justify-center p-4 sm:p-6 z-10 w-full h-full">
{children}
</main>
</div>
);
}
@@ -0,0 +1,43 @@
"use client";
import { useState } from "react";
import Sidebar from "../Sidebar";
import Header from "../Header";
export default function DashboardLayout({ children }) {
const [sidebarOpen, setSidebarOpen] = useState(false);
return (
<div className="flex h-screen w-full overflow-hidden bg-bg">
{/* Mobile sidebar overlay */}
{sidebarOpen && (
<div
className="fixed inset-0 z-40 bg-black/50 lg:hidden"
onClick={() => setSidebarOpen(false)}
/>
)}
{/* Sidebar - Desktop */}
<div className="hidden lg:flex">
<Sidebar />
</div>
{/* Sidebar - Mobile */}
<div
className={`fixed inset-y-0 left-0 z-50 transform lg:hidden transition-transform duration-300 ease-in-out ${
sidebarOpen ? "translate-x-0" : "-translate-x-full"
}`}
>
<Sidebar onClose={() => setSidebarOpen(false)} />
</div>
{/* Main content */}
<main className="flex flex-col flex-1 h-full min-w-0 relative transition-colors duration-300">
<Header onMenuClick={() => setSidebarOpen(true)} />
<div className="flex-1 overflow-y-auto custom-scrollbar p-6 lg:p-10">
<div className="max-w-7xl mx-auto">{children}</div>
</div>
</main>
</div>
);
}
+4
View File
@@ -0,0 +1,4 @@
// Layout Components - Export all
export { default as DashboardLayout } from "./DashboardLayout";
export { default as AuthLayout } from "./AuthLayout";
+142
View File
@@ -0,0 +1,142 @@
// CLI Tools configuration
export const CLI_TOOLS = {
claude: {
id: "claude",
name: "Claude Code",
icon: "terminal",
color: "#D97757",
description: "Anthropic Claude Code CLI",
configType: "env",
envVars: {
baseUrl: "ANTHROPIC_BASE_URL",
model: "ANTHROPIC_MODEL",
opusModel: "ANTHROPIC_DEFAULT_OPUS_MODEL",
sonnetModel: "ANTHROPIC_DEFAULT_SONNET_MODEL",
haikuModel: "ANTHROPIC_DEFAULT_HAIKU_MODEL",
},
modelAliases: ["default", "sonnet", "opus", "haiku", "opusplan"],
settingsFile: "~/.claude/settings.json",
defaultModels: [
{ id: "opus", name: "Claude Opus", alias: "opus", envKey: "ANTHROPIC_DEFAULT_OPUS_MODEL", defaultValue: "cc/claude-opus-4-5-20251101" },
{ id: "sonnet", name: "Claude Sonnet", alias: "sonnet", envKey: "ANTHROPIC_DEFAULT_SONNET_MODEL", defaultValue: "cc/claude-sonnet-4-5-20250929" },
{ id: "haiku", name: "Claude Haiku", alias: "haiku", envKey: "ANTHROPIC_DEFAULT_HAIKU_MODEL", defaultValue: "cc/claude-haiku-4-5-20251001" },
],
},
codex: {
id: "codex",
name: "OpenAI Codex CLI",
image: "/providers/codex.png",
color: "#10A37F",
description: "OpenAI Codex CLI",
configType: "custom",
},
cursor: {
id: "cursor",
name: "Cursor",
image: "/providers/cursor.png",
color: "#000000",
description: "Cursor AI Code Editor",
configType: "guide",
requiresCloud: true,
notes: [
{ type: "warning", text: "Requires Cursor Pro account to use this feature." },
{ type: "cloudCheck", text: "Cursor routes requests through its own server, so local endpoint is not supported. Please enable Cloud Endpoint in Settings." },
],
guideSteps: [
{ step: 1, title: "Open Settings", desc: "Go to Settings → Models" },
{ step: 2, title: "Enable OpenAI API", desc: "Enable \"OpenAI API key\" option" },
{ step: 3, title: "Base URL", value: "{{baseUrl}}/v1", copyable: true },
{ step: 4, title: "API Key", type: "apiKeySelector" },
{ step: 5, title: "Add Custom Model", desc: "Click \"View All Model\" → \"Add Custom Model\"" },
{ step: 6, title: "Select Model", type: "modelSelector" },
],
},
cline: {
id: "cline",
name: "CLINE",
image: "/providers/cline.png",
color: "#00D1B2",
description: "CLINE AI Assistant",
configType: "guide",
guideSteps: [
{ step: 1, title: "Open Settings", desc: "Go to CLINE Settings panel" },
{ step: 2, title: "Select Provider", desc: "Choose API Provider → Ollama" },
{ step: 3, title: "Base URL", value: "{{baseUrl}}", copyable: true },
{ step: 4, title: "API Key", type: "apiKeySelector" },
{ step: 5, title: "Select Model", type: "modelSelector" },
],
},
roo: {
id: "roo",
name: "Roo",
image: "/providers/roo.png",
color: "#FF6B6B",
description: "Roo AI Assistant",
configType: "guide",
guideSteps: [
{ step: 1, title: "Open Settings", desc: "Go to Roo Settings panel" },
{ step: 2, title: "Select Provider", desc: "Choose API Provider → Ollama" },
{ step: 3, title: "Base URL", value: "{{baseUrl}}", copyable: true },
{ step: 4, title: "API Key", type: "apiKeySelector" },
{ step: 5, title: "Select Model", type: "modelSelector" },
],
},
continue: {
id: "continue",
name: "Continue",
image: "/providers/continue.png",
color: "#7C3AED",
description: "Continue AI Assistant",
configType: "guide",
guideSteps: [
{ step: 1, title: "Open Config", desc: "Open Continue configuration file" },
{ step: 2, title: "API Key", type: "apiKeySelector" },
{ step: 3, title: "Select Model", type: "modelSelector" },
{ step: 4, title: "Add Model Config", desc: "Add the following configuration to your models array:" },
],
codeBlock: {
language: "json",
code: `{
"apiBase": "{{baseUrl}}",
"title": "{{model}}",
"model": "{{model}}",
"provider": "openai",
"apiKey": "{{apiKey}}"
}`,
},
},
// HIDDEN: gemini-cli
// "gemini-cli": {
// id: "gemini-cli",
// name: "Gemini CLI",
// icon: "terminal",
// color: "#4285F4",
// description: "Google Gemini CLI",
// configType: "env",
// envVars: {
// baseUrl: "GEMINI_API_BASE_URL",
// model: "GEMINI_MODEL",
// },
// defaultModels: [
// { id: "gemini-2.5-pro", name: "Gemini 2.5 Pro", alias: "pro" },
// { id: "gemini-2.5-flash", name: "Gemini 2.5 Flash", alias: "flash" },
// ],
// },
};
// Get all provider models for mapping dropdown
export const getProviderModelsForMapping = (providers) => {
const result = [];
providers.forEach(conn => {
if (conn.isActive && (conn.testStatus === "active" || conn.testStatus === "success")) {
result.push({
connectionId: conn.id,
provider: conn.provider,
name: conn.name,
models: conn.models || [],
});
}
});
return result;
};
+77
View File
@@ -0,0 +1,77 @@
// Claude-inspired color palette for Endpoint Proxy
// Light theme: Warm beige/cream tones
// Dark theme: Deep charcoal/brown tones
export const COLORS = {
// Primary - Warm Coral/Terracotta (Claude-like)
primary: {
DEFAULT: "#D97757",
hover: "#C56243",
light: "#E8A58C",
dark: "#B0664D",
},
// Light theme backgrounds
light: {
bg: "#FBF9F6",
bgAlt: "#F5F1ED",
surface: "#FFFFFF",
sidebar: "#F0EFEC",
border: "#E6E4DD",
textMain: "#383733",
textMuted: "#75736E",
},
// Dark theme backgrounds
dark: {
bg: "#191918",
bgAlt: "#1F1F1E",
surface: "#242423",
sidebar: "#1F1F1E",
border: "#333331",
textMain: "#ECEBE8",
textMuted: "#9E9D99",
},
// Status colors
status: {
success: "#22C55E",
successLight: "#DCFCE7",
successDark: "#166534",
warning: "#F59E0B",
warningLight: "#FEF3C7",
warningDark: "#92400E",
error: "#EF4444",
errorLight: "#FEE2E2",
errorDark: "#991B1B",
info: "#3B82F6",
infoLight: "#DBEAFE",
infoDark: "#1E40AF",
},
};
// CSS Variables mapping for Tailwind
export const CSS_VARIABLES = {
light: {
"--color-primary": COLORS.primary.DEFAULT,
"--color-primary-hover": COLORS.primary.hover,
"--color-bg": COLORS.light.bg,
"--color-bg-alt": COLORS.light.bgAlt,
"--color-surface": COLORS.light.surface,
"--color-sidebar": COLORS.light.sidebar,
"--color-border": COLORS.light.border,
"--color-text-main": COLORS.light.textMain,
"--color-text-muted": COLORS.light.textMuted,
},
dark: {
"--color-primary": COLORS.primary.DEFAULT,
"--color-primary-hover": COLORS.primary.hover,
"--color-bg": COLORS.dark.bg,
"--color-bg-alt": COLORS.dark.bgAlt,
"--color-surface": COLORS.dark.surface,
"--color-sidebar": COLORS.dark.sidebar,
"--color-border": COLORS.dark.border,
"--color-text-main": COLORS.dark.textMain,
"--color-text-muted": COLORS.dark.textMuted,
},
};
+53
View File
@@ -0,0 +1,53 @@
// App configuration
export const APP_CONFIG = {
name: "Endpoint Proxy",
description: "AI Infrastructure Management",
version: "1.0.0",
};
// Theme configuration
export const THEME_CONFIG = {
storageKey: "theme",
defaultTheme: "system", // "light" | "dark" | "system"
};
// Subscription
export const SUBSCRIPTION_CONFIG = {
price: 1.0,
currency: "USD",
interval: "month",
planName: "Pro Plan",
};
// API endpoints
export const API_ENDPOINTS = {
users: "/api/users",
providers: "/api/providers",
payments: "/api/payments",
auth: "/api/auth",
};
// Provider API endpoints (for display only)
export const PROVIDER_ENDPOINTS = {
openrouter: "https://openrouter.ai/api/v1/chat/completions",
glm: "https://api.z.ai/api/anthropic/v1/messages",
kimi: "https://api.kimi.com/coding/v1/messages",
minimax: "https://api.minimax.io/anthropic/v1/messages",
openai: "https://api.openai.com/v1/chat/completions",
anthropic: "https://api.anthropic.com/v1/messages",
gemini: "https://generativelanguage.googleapis.com/v1beta/models",
};
// Re-export from providers.js for backward compatibility
export {
OAUTH_PROVIDERS,
APIKEY_PROVIDERS,
AI_PROVIDERS,
AUTH_METHODS,
} from "./providers.js";
// Re-export from models.js for backward compatibility
export {
PROVIDER_MODELS,
AI_MODELS,
} from "./models.js";
+4
View File
@@ -0,0 +1,4 @@
// Shared Constants - Export all
export * from "./colors";
export * from "./config";
+34
View File
@@ -0,0 +1,34 @@
// Re-export from open-sse (single source of truth)
export {
PROVIDER_MODELS,
getProviderModels,
getDefaultModel,
isValidModel as isValidModelCore,
findModelName,
getModelTargetFormat,
PROVIDER_ID_TO_ALIAS,
getModelsByProviderId
} from "open-sse";
import { AI_PROVIDERS } from "./providers.js";
import { PROVIDER_MODELS as MODELS } from "open-sse";
// Providers that accept any model (passthrough)
const PASSTHROUGH_PROVIDERS = new Set(
Object.entries(AI_PROVIDERS)
.filter(([, p]) => p.passthroughModels)
.map(([key]) => key)
);
// Wrap isValidModel with passthrough providers
export function isValidModel(aliasOrId, modelId) {
if (PASSTHROUGH_PROVIDERS.has(aliasOrId)) return true;
const models = MODELS[aliasOrId];
if (!models) return false;
return models.some(m => m.id === modelId);
}
// Legacy AI_MODELS for backward compatibility
export const AI_MODELS = Object.entries(MODELS).flatMap(([alias, models]) =>
models.map(m => ({ provider: alias, model: m.id, name: m.name }))
);
+65
View File
@@ -0,0 +1,65 @@
// Provider definitions
// OAuth Providers
export const OAUTH_PROVIDERS = {
claude: { id: "claude", alias: "cc", name: "Claude Code", icon: "smart_toy", color: "#D97757" },
antigravity: { id: "antigravity", alias: "ag", name: "Antigravity", icon: "rocket_launch", color: "#F59E0B" },
codex: { id: "codex", alias: "cx", name: "OpenAI Codex", icon: "code", color: "#3B82F6" },
iflow: { id: "iflow", alias: "if", name: "iFlow AI", icon: "water_drop", color: "#6366F1" },
qwen: { id: "qwen", alias: "qw", name: "Qwen Code", icon: "psychology", color: "#10B981" },
"gemini-cli": { id: "gemini-cli", alias: "gc", name: "Gemini CLI", icon: "terminal", color: "#4285F4" },
github: { id: "github", alias: "gh", name: "GitHub Copilot", icon: "code", color: "#333333" },
};
export const APIKEY_PROVIDERS = {
openrouter: { id: "openrouter", alias: "openrouter", name: "OpenRouter", icon: "router", color: "#6366F1", textIcon: "OR" , passthroughModels: true },
glm: { id: "glm", alias: "glm", name: "GLM Coding", icon: "code", color: "#2563EB", textIcon: "GL" },
kimi: { id: "kimi", alias: "kimi", name: "Kimi Coding", icon: "psychology", color: "#1E3A8A", textIcon: "KM" },
minimax: { id: "minimax", alias: "minimax", name: "Minimax Coding", icon: "memory", color: "#7C3AED", textIcon: "MM" },
openai: { id: "openai", alias: "openai", name: "OpenAI", icon: "auto_awesome", color: "#10A37F", textIcon: "OA" },
anthropic: { id: "anthropic", alias: "anthropic", name: "Anthropic", icon: "smart_toy", color: "#D97757", textIcon: "AN" },
gemini: { id: "gemini", alias: "gemini", name: "Gemini", icon: "diamond", color: "#4285F4", textIcon: "GE" },
};
// All providers (combined)
export const AI_PROVIDERS = { ...OAUTH_PROVIDERS, ...APIKEY_PROVIDERS };
// Auth methods
export const AUTH_METHODS = {
oauth: { id: "oauth", name: "OAuth", icon: "lock" },
apikey: { id: "apikey", name: "API Key", icon: "key" },
};
// Helper: Get provider by alias
export function getProviderByAlias(alias) {
for (const provider of Object.values(AI_PROVIDERS)) {
if (provider.alias === alias || provider.id === alias) {
return provider;
}
}
return null;
}
// Helper: Get provider ID from alias
export function resolveProviderId(aliasOrId) {
const provider = getProviderByAlias(aliasOrId);
return provider?.id || aliasOrId;
}
// Helper: Get alias from provider ID
export function getProviderAlias(providerId) {
const provider = AI_PROVIDERS[providerId];
return provider?.alias || providerId;
}
// Alias to ID mapping (for quick lookup)
export const ALIAS_TO_ID = Object.values(AI_PROVIDERS).reduce((acc, p) => {
acc[p.alias] = p.id;
return acc;
}, {});
// ID to Alias mapping
export const ID_TO_ALIAS = Object.values(AI_PROVIDERS).reduce((acc, p) => {
acc[p.id] = p.alias;
return acc;
}, {});
+2
View File
@@ -0,0 +1,2 @@
// Shared Hooks - Export all
export { useTheme } from "./useTheme";
+29
View File
@@ -0,0 +1,29 @@
"use client";
import { useState, useCallback, useRef } from "react";
/**
* Hook for copy to clipboard with feedback
* @param {number} resetDelay - Time in ms before resetting copied state (default: 2000)
* @returns {{ copied: string|null, copy: (text: string, id?: string) => void }}
*/
export function useCopyToClipboard(resetDelay = 2000) {
const [copied, setCopied] = useState(null);
const timeoutRef = useRef(null);
const copy = useCallback((text, id = "default") => {
navigator.clipboard.writeText(text);
setCopied(id);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
setCopied(null);
}, resetDelay);
}, [resetDelay]);
return { copied, copy };
}
+31
View File
@@ -0,0 +1,31 @@
"use client";
import { useEffect } from "react";
import useThemeStore from "@/store/themeStore";
export function useTheme() {
const { theme, setTheme, toggleTheme, initTheme } = useThemeStore();
useEffect(() => {
initTheme();
// Listen for system theme changes
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = () => {
if (theme === "system") {
initTheme();
}
};
mediaQuery.addEventListener("change", handleChange);
return () => mediaQuery.removeEventListener("change", handleChange);
}, [theme, initTheme]);
return {
theme,
setTheme,
toggleTheme,
isDark: theme === "dark" || (theme === "system" && typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches),
};
}
+117
View File
@@ -0,0 +1,117 @@
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { isCloudEnabled } from "@/lib/localDb";
/**
* Cloud sync scheduler
*/
export class CloudSyncScheduler {
constructor(machineId = null, intervalMinutes = 15) {
this.machineId = machineId;
this.intervalMinutes = intervalMinutes;
this.intervalId = null;
}
/**
* Initialize machine ID if not provided
*/
async initializeMachineId() {
if (!this.machineId) {
this.machineId = await getConsistentMachineId();
}
}
/**
* Start periodic sync (delays first sync to allow server to be ready)
*/
async start() {
if (this.intervalId) {
return;
}
await this.initializeMachineId();
// Delay first sync by 30 seconds to ensure server is ready
setTimeout(() => {
this.syncWithRetry().catch(() => {});
}, 30000);
// Then sync periodically
this.intervalId = setInterval(() => {
this.syncWithRetry().catch(() => {});
}, this.intervalMinutes * 60 * 1000);
}
/**
* Stop periodic sync
*/
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
/**
* Sync with retry logic (exponential backoff)
*/
async syncWithRetry(maxRetries = 1) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const result = await this.sync();
return result;
} catch (error) {
if (attempt === maxRetries) {
return null;
}
const delay = Math.min(1000 * Math.pow(2, attempt), 10000); // Max 10s
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
/**
* Perform sync via internal API route (handles token update to db.json)
*/
async sync() {
// Check if cloud is enabled
const enabled = await isCloudEnabled();
if (!enabled) {
return null;
}
await this.initializeMachineId();
// Call internal API route which handles both sync and token update
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000"}/api/sync/cloud`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ machineId: this.machineId, action: "sync" })
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.error || "Sync failed");
}
const result = await response.json();
return result;
}
/**
* Check if scheduler is running
*/
isRunning() {
return this.intervalId !== null;
}
}
// Export a singleton instance if needed
let cloudSyncScheduler = null;
export async function getCloudSyncScheduler(machineId = null, intervalMinutes = 15) {
if (!cloudSyncScheduler) {
cloudSyncScheduler = new CloudSyncScheduler(machineId, intervalMinutes);
}
return cloudSyncScheduler;
}
@@ -0,0 +1,32 @@
import { getCloudSyncScheduler } from "@/shared/services/cloudSyncScheduler";
import { isCloudEnabled, cleanupProviderConnections } from "@/lib/localDb";
/**
* Initialize cloud sync scheduler
* This should be called when the application starts
*/
export async function initializeCloudSync() {
try {
// Cleanup null fields from existing data
await cleanupProviderConnections();
// Create scheduler instance with default 15-minute interval
const scheduler = await getCloudSyncScheduler(null, 15);
// Start the scheduler
await scheduler.start();
return scheduler;
} catch (error) {
console.error("[CloudSync] Error initializing scheduler:", error);
throw error;
}
}
// For development/testing purposes
if (typeof require !== "undefined" && require.main === module) {
initializeCloudSync().catch(console.log);
}
export default initializeCloudSync;
+92
View File
@@ -0,0 +1,92 @@
/**
* API utility functions for making HTTP requests
*/
const DEFAULT_HEADERS = {
"Content-Type": "application/json",
};
/**
* Make a GET request
* @param {string} url - API endpoint
* @param {object} options - Fetch options
* @returns {Promise<object>}
*/
export async function get(url, options = {}) {
const response = await fetch(url, {
method: "GET",
headers: { ...DEFAULT_HEADERS, ...options.headers },
...options,
});
return handleResponse(response);
}
/**
* Make a POST request
* @param {string} url - API endpoint
* @param {object} data - Request body
* @param {object} options - Fetch options
* @returns {Promise<object>}
*/
export async function post(url, data, options = {}) {
const response = await fetch(url, {
method: "POST",
headers: { ...DEFAULT_HEADERS, ...options.headers },
body: JSON.stringify(data),
...options,
});
return handleResponse(response);
}
/**
* Make a PUT request
* @param {string} url - API endpoint
* @param {object} data - Request body
* @param {object} options - Fetch options
* @returns {Promise<object>}
*/
export async function put(url, data, options = {}) {
const response = await fetch(url, {
method: "PUT",
headers: { ...DEFAULT_HEADERS, ...options.headers },
body: JSON.stringify(data),
...options,
});
return handleResponse(response);
}
/**
* Make a DELETE request
* @param {string} url - API endpoint
* @param {object} options - Fetch options
* @returns {Promise<object>}
*/
export async function del(url, options = {}) {
const response = await fetch(url, {
method: "DELETE",
headers: { ...DEFAULT_HEADERS, ...options.headers },
...options,
});
return handleResponse(response);
}
/**
* Handle API response
* @param {Response} response - Fetch response
* @returns {Promise<object>}
*/
async function handleResponse(response) {
const data = await response.json();
if (!response.ok) {
const error = new Error(data.error || "An error occurred");
error.status = response.status;
error.data = data;
throw error;
}
return data;
}
export default { get, post, put, del };
+98
View File
@@ -0,0 +1,98 @@
import crypto from "crypto";
const API_KEY_SECRET = process.env.API_KEY_SECRET || "endpoint-proxy-api-key-secret";
/**
* Generate 6-char random keyId
*/
function generateKeyId() {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < 6; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
/**
* Generate CRC (8-char HMAC)
*/
function generateCrc(machineId, keyId) {
return crypto
.createHmac("sha256", API_KEY_SECRET)
.update(machineId + keyId)
.digest("hex")
.slice(0, 8);
}
/**
* Generate API key with machineId embedded
* Format: sk-{machineId}-{keyId}-{crc8}
* @param {string} machineId - 16-char machine ID
* @returns {{ key: string, keyId: string }}
*/
export function generateApiKeyWithMachine(machineId) {
const keyId = generateKeyId();
const crc = generateCrc(machineId, keyId);
const key = `sk-${machineId}-${keyId}-${crc}`;
return { key, keyId };
}
/**
* Parse API key and extract machineId + keyId
* Supports both formats:
* - New: sk-{machineId}-{keyId}-{crc8}
* - Old: sk-{random8}
* @param {string} apiKey
* @returns {{ machineId: string, keyId: string, isNewFormat: boolean } | null}
*/
export function parseApiKey(apiKey) {
if (!apiKey || !apiKey.startsWith("sk-")) return null;
const parts = apiKey.split("-");
// New format: sk-{machineId}-{keyId}-{crc8} = 4 parts
if (parts.length === 4) {
const [, machineId, keyId, crc] = parts;
// Validate CRC
const expectedCrc = generateCrc(machineId, keyId);
if (crc !== expectedCrc) return null;
return { machineId, keyId, isNewFormat: true };
}
// Old format: sk-{random8} = 2 parts
if (parts.length === 2) {
return { machineId: null, keyId: parts[1], isNewFormat: false };
}
return null;
}
/**
* Verify API key CRC (only for new format)
* @param {string} apiKey
* @returns {boolean}
*/
export function verifyApiKeyCrc(apiKey) {
const parsed = parseApiKey(apiKey);
if (!parsed) return false;
// Old format doesn't have CRC, always valid if parsed
if (!parsed.isNewFormat) return true;
// New format already verified in parseApiKey
return true;
}
/**
* Check if API key is new format (contains machineId)
* @param {string} apiKey
* @returns {boolean}
*/
export function isNewFormatKey(apiKey) {
const parsed = parseApiKey(apiKey);
return parsed?.isNewFormat === true;
}
+40
View File
@@ -0,0 +1,40 @@
import { getMachineId } from "@/shared/utils/machine";
// Function to get cloud URL with machine ID
export function getCloudUrl(machineId) {
// Get from environment or default to localhost:8787
const cloudUrl = process.env.NEXT_PUBLIC_CLOUD_URL || "http://localhost:8787";
return `${cloudUrl}/${machineId}/v1/chat/completions`;
}
// Function to call cloud with machine ID
export async function callCloudWithMachineId(request) {
const machineId = await getMachineId();
if (!machineId) {
throw new Error("Could not get machine ID");
}
const cloudUrl = getCloudUrl(machineId);
// Get the original request body and headers
const body = await request.json();
const headers = new Headers(request.headers);
// Remove authorization header since cloud won't need it (uses machineId instead)
headers.delete("authorization");
// Call the cloud with machine ID
const response = await fetch(cloudUrl, {
method: "POST",
headers: headers,
body: JSON.stringify(body)
});
return response;
}
// Function to periodically sync provider data to cloud (now a no-op)
export function startProviderSync(cloudUrl, intervalMs = 900000) { // Default 15 minutes
console.log("Frontend sync is disabled. Use backend sync instead.");
return null;
}
+11
View File
@@ -0,0 +1,11 @@
// Utility function to merge class names
// Handles conditional classes and removes duplicates
export function cn(...classes) {
return classes
.filter(Boolean)
.join(" ")
.replace(/\s+/g, " ")
.trim();
}
+32
View File
@@ -0,0 +1,32 @@
// Shared Utils - Export all
export { cn } from "./cn";
export * as api from "./api";
/**
* Extract error code from error message (401, 429, 503...)
* @param {string} lastError - Error message
* @returns {string|null} Error code or null
*/
export function getErrorCode(lastError) {
if (!lastError) return null;
const match = lastError.match(/\b([45]\d{2})\b/);
return match ? match[1] : "ERR";
}
/**
* Get relative time string (e.g. "5 min ago")
* @param {string} isoDate - ISO date string
* @returns {string} Relative time
*/
export function getRelativeTime(isoDate) {
if (!isoDate) return "";
const diff = Date.now() - new Date(isoDate).getTime();
const mins = Math.floor(diff / 60000);
if (mins < 1) return "just now";
if (mins < 60) return `${mins}m ago`;
const hours = Math.floor(mins / 60);
if (hours < 24) return `${hours}h ago`;
const days = Math.floor(hours / 24);
return `${days}d ago`;
}
+18
View File
@@ -0,0 +1,18 @@
import { getConsistentMachineId } from './machineId';
// Get machine ID using node-machine-id with salt
export async function getMachineId() {
return await getConsistentMachineId();
}
// Keep sync functions for backward compatibility but make them no-ops
// (Frontend sync is disabled - use backend sync instead)
export async function syncProviderDataToCloud(cloudUrl) {
console.log("Frontend sync is disabled. Use backend sync instead.");
return Promise.resolve(true);
}
export async function getProvidersNeedingRefresh() {
console.log("Frontend sync is disabled. Use backend sync instead.");
return Promise.resolve([]);
}
+58
View File
@@ -0,0 +1,58 @@
import { machineIdSync } from 'node-machine-id';
/**
* Get consistent machine ID using node-machine-id with salt
* This ensures the same physical machine gets the same ID across runs
*
* @param {string} salt - Optional salt to use (defaults to environment variable)
* @returns {Promise<string>} Machine ID (16-character base32)
*/
export async function getConsistentMachineId(salt = null) {
// For server-side, use node-machine-id with salt
const saltValue = salt || process.env.MACHINE_ID_SALT || 'endpoint-proxy-salt';
try {
const rawMachineId = machineIdSync();
// Create consistent ID using salt
const crypto = await import('crypto');
const hashedMachineId = crypto.createHash('sha256').update(rawMachineId + saltValue).digest('hex');
// Return only first 16 characters for brevity
return hashedMachineId.substring(0, 16);
} catch (error) {
console.log('Error getting machine ID:', error);
// Fallback to random ID if node-machine-id fails
return crypto.randomUUID ? crypto.randomUUID() :
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
}
/**
* Get raw machine ID without hashing (for debugging purposes)
* @returns {Promise<string>} Raw machine ID
*/
export async function getRawMachineId() {
// For server-side, use raw node-machine-id
try {
return machineIdSync();
} catch (error) {
console.log('Error getting raw machine ID:', error);
// Fallback to random ID if node-machine-id fails
return crypto.randomUUID ? crypto.randomUUID() :
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
}
/**
* Check if we're running in browser or server environment
* @returns {boolean} True if in browser, false if in server
*/
export function isBrowser() {
return typeof window !== 'undefined';
}