All files / web/src/components/ai-providers providers-and-models-view.tsx

0% Statements 0/354
100% Branches 1/1
100% Functions 1/1
0% Lines 0/354

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client';
 
import { type AIProvider, PrettyAIProvider } from '@shared/types/constants';
import type { Model } from '@shared/types/data/model';
import { deleteModel } from '@web/api/v1/super-agents/models';
import { AIProvidersListView } from '@web/components/ai-providers/ai-providers-list';
import { DeleteModelDialog } from '@web/components/ai-providers/delete-model-dialog';
import { Badge } from '@web/components/ui/badge';
import { Button } from '@web/components/ui/button';
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@web/components/ui/card';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '@web/components/ui/dropdown-menu';
import { Input } from '@web/components/ui/input';
import { Skeleton } from '@web/components/ui/skeleton';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@web/components/ui/table';
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from '@web/components/ui/tooltip';
import { usePermissiveNavigate } from '@web/hooks/use-permissive-navigate';
import { useSettingsValidation } from '@web/hooks/use-settings-validation';
import { useToast } from '@web/hooks/use-toast';
import { useAIProviders } from '@web/providers/ai-providers';
import { useModels } from '@web/providers/models';
import { compareModels } from '@web/utils/model-sorting';
import { format } from 'date-fns';
import {
  AlertCircleIcon,
  CalendarIcon,
  CpuIcon,
  MoreHorizontalIcon,
  PlusIcon,
  SearchIcon,
  TrashIcon,
} from 'lucide-react';
import { nanoid } from 'nanoid';
import type { ReactElement } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
 
interface ProvidersAndModelsViewProps {
  selectedProviderId?: string;
}
 
export function ProvidersAndModelsView({
  selectedProviderId,
}: ProvidersAndModelsViewProps): ReactElement {
  const navigate = usePermissiveNavigate();
  const { toast } = useToast();
  const { aiProviderConfigs: apiKeys } = useAIProviders();
  const { models, isLoading, setQueryParams, refetch } = useModels();
  const {
    hasTextModels,
    hasEmbedModels,
    hasRequiredModelTypes,
    isLoading: isLoadingValidation,
  } = useSettingsValidation();
 
  const [searchQuery, setSearchQuery] = useState('');
  const [isDeleting, setIsDeleting] = useState<string | null>(null);
  const [activeProvider, setActiveProvider] = useState<string | null>(
    selectedProviderId || null,
  );
  const [modelToDelete, setModelToDelete] = useState<Model | null>(null);
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
 
  const modelsRef = useRef<HTMLDivElement>(null);
 
  // Initialize models query
  useEffect(() => {
    setQueryParams({});
  }, [setQueryParams]);
 
  // Set active provider from prop and scroll to models
  useEffect(() => {
    if (selectedProviderId) {
      setActiveProvider(selectedProviderId);
      // Scroll to models section
      setTimeout(() => {
        modelsRef.current?.scrollIntoView({
          behavior: 'smooth',
          block: 'start',
        });
      }, 100);
    }
  }, [selectedProviderId]);
 
  // Auto-select first provider if none is selected
  useEffect(() => {
    if (!activeProvider && apiKeys.length > 0) {
      setActiveProvider(apiKeys[0].id);
    }
  }, [activeProvider, apiKeys]);
 
  // Filter models by active provider and sort alphabetically
  const filteredModels = models
    .filter((model) => {
      // Filter by active provider
      if (activeProvider && model.ai_provider_id !== activeProvider) {
        return false;
      }
 
      // Filter by search query
      if (searchQuery) {
        const searchLower = searchQuery.toLowerCase();
        const apiKey = apiKeys.find((key) => key.id === model.ai_provider_id);
        return (
          model.model_name.toLowerCase().includes(searchLower) ||
          apiKey?.ai_provider?.toLowerCase().includes(searchLower) ||
          apiKey?.name?.toLowerCase().includes(searchLower)
        );
      }
 
      return true;
    })
    .sort((a, b) => {
      const providerA = apiKeys.find((k) => k.id === a.ai_provider_id);
      const providerB = apiKeys.find((k) => k.id === b.ai_provider_id);
      return compareModels(
        { modelName: a.model_name, providerName: providerA?.name || '' },
        { modelName: b.model_name, providerName: providerB?.name || '' },
      );
    });
 
  const handleDeleteClick = (model: Model) => {
    setModelToDelete(model);
    setIsDeleteDialogOpen(true);
  };
 
  const handleDeleteConfirm = async () => {
    if (!modelToDelete || isDeleting) return;
 
    try {
      setIsDeleting(modelToDelete.id);
      await deleteModel(modelToDelete.id);
 
      toast({
        title: 'Model deleted',
        description: `Model "${modelToDelete.model_name}" has been deleted successfully.`,
      });
 
      await refetch();
    } catch (error) {
      toast({
        title: 'Failed to delete model',
        description:
          error instanceof Error
            ? error.message
            : 'An unexpected error occurred.',
        variant: 'destructive',
      });
    } finally {
      setIsDeleting(null);
    }
  };
 
  const getProviderInfo = (apiKeyId: string) => {
    const apiKey = apiKeys.find((key) => key.id === apiKeyId);
    const rawProvider = apiKey?.ai_provider as AIProvider;
    return {
      provider: rawProvider
        ? PrettyAIProvider[rawProvider] || rawProvider
        : 'Unknown',
      name: apiKey?.name || 'Unknown',
    };
  };
 
  const activeProviderInfo = activeProvider
    ? apiKeys.find((key) => key.id === activeProvider)
    : null;
 
  // Compute missing model types
  const missingModelTypes = useMemo(() => {
    if (isLoadingValidation) return [];
    const missing: string[] = [];
    if (!hasTextModels) missing.push('text');
    if (!hasEmbedModels) missing.push('embedding');
    return missing;
  }, [isLoadingValidation, hasTextModels, hasEmbedModels]);
 
  return (
    <div className="flex flex-col gap-6">
      {/* AI Providers Section */}
      <AIProvidersListView
        onProviderSelect={setActiveProvider}
        selectedProviderId={activeProvider}
      />
 
      {/* Warning Banner for Missing Model Types */}
      {!isLoadingValidation && !hasRequiredModelTypes && models.length > 0 && (
        <div className="px-6">
          <Card className="border-amber-500 bg-amber-50 dark:bg-amber-950/20">
            <CardContent className="pt-6">
              <div className="flex items-start gap-3">
                <AlertCircleIcon className="h-5 w-5 text-amber-500 mt-0.5" />
                <div className="space-y-2">
                  <p className="font-medium">Missing model types</p>
                  <p className="text-sm text-muted-foreground">
                    To use all features, you need at least one{' '}
                    {missingModelTypes.join(' and one ')} model. Add more models
                    to your AI providers to unlock full functionality.
                  </p>
                  {activeProvider && (
                    <Button
                      variant="outline"
                      size="sm"
                      className="mt-2"
                      onClick={() =>
                        navigate({
                          to: '/ai-providers/$id/add-models',
                          params: { id: activeProvider },
                        })
                      }
                    >
                      <PlusIcon className="h-4 w-4 mr-2" />
                      Add Models
                    </Button>
                  )}
                </div>
              </div>
            </CardContent>
          </Card>
        </div>
      )}
 
      {/* Models Section */}
      <div ref={modelsRef} className="px-6 pb-6">
        <Card>
          <CardHeader className="flex w-full">
            <div className="flex items-start justify-between w-full">
              <div className="w-full">
                <CardTitle className="flex items-center gap-2 w-full p-0 m-0">
                  <div className="flex items-center gap-2 w-full">
                    <CpuIcon className="h-5 w-5" />
                    Models
                    {activeProviderInfo && (
                      <>
                        <span className="text-muted-foreground">for</span>
                        <Badge variant="outline" className="text-lg">
                          {PrettyAIProvider[
                            activeProviderInfo.ai_provider as AIProvider
                          ] || activeProviderInfo.ai_provider}
                        </Badge>
                      </>
                    )}
                  </div>
                  {activeProvider && (
                    <Button
                      onClick={() =>
                        navigate({
                          to: '/ai-providers/$id/add-models',
                          params: { id: activeProvider },
                        })
                      }
                    >
                      <PlusIcon className="h-4 w-4 mr-2" />
                      Add Models
                    </Button>
                  )}
                </CardTitle>
                <CardDescription className="p-0 m-0">
                  {activeProvider
                    ? `Models using the selected AI provider (${filteredModels.length})`
                    : 'Select a provider above to view its models'}
                </CardDescription>
              </div>
            </div>
          </CardHeader>
          <CardContent>
            {/* Search */}
            {activeProvider && (
              <div className="mb-4">
                <div className="relative">
                  <SearchIcon className="absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4" />
                  <Input
                    placeholder="Search models..."
                    value={searchQuery}
                    onChange={(e) => setSearchQuery(e.target.value)}
                    className="pl-10"
                  />
                </div>
              </div>
            )}
 
            {/* Models Table */}
            {!activeProvider ? (
              <div className="text-center py-12">
                <CpuIcon className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
                <p className="text-muted-foreground">
                  Select an AI provider above to view and manage its models
                </p>
              </div>
            ) : isLoading ? (
              <div className="space-y-3">
                {Array.from({ length: 5 }).map(() => (
                  <div key={nanoid()} className="flex items-center space-x-4">
                    <Skeleton className="h-12 w-12 rounded-full" />
                    <div className="space-y-2 flex-1">
                      <Skeleton className="h-4 w-3/4" />
                      <Skeleton className="h-3 w-1/2" />
                    </div>
                  </div>
                ))}
              </div>
            ) : filteredModels.length === 0 ? (
              <div className="text-center py-12">
                <CpuIcon className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
                <h3 className="text-lg font-semibold mb-2">No models found</h3>
                <p className="text-muted-foreground mb-4">
                  {searchQuery
                    ? 'No models match your search criteria.'
                    : 'This provider has no models configured yet.'}
                </p>
                <Button
                  onClick={() =>
                    navigate({
                      to: '/ai-providers/$id/add-models',
                      params: { id: activeProvider },
                    })
                  }
                >
                  <PlusIcon className="h-4 w-4 mr-2" />
                  Add your first model
                </Button>
              </div>
            ) : (
              <div className="overflow-x-auto">
                <Table>
                  <TableHeader>
                    <TableRow>
                      <TableHead>Model</TableHead>
                      <TableHead>Type</TableHead>
                      <TableHead>Provider</TableHead>
                      <TableHead>Added</TableHead>
                      <TableHead className="w-20">Actions</TableHead>
                    </TableRow>
                  </TableHeader>
                  <TableBody>
                    {filteredModels.map((model) => {
                      const providerInfo = getProviderInfo(
                        model.ai_provider_id,
                      );
                      return (
                        <TableRow key={model.id}>
                          <TableCell>
                            <div className="flex items-center space-x-3">
                              <div className="flex h-10 w-10 items-center justify-center rounded-full bg-muted">
                                <CpuIcon className="h-5 w-5" />
                              </div>
                              <div>
                                <div className="font-medium">
                                  {model.model_name}
                                </div>
                                <div className="text-sm text-muted-foreground">
                                  ID: {model.id.slice(0, 8)}...
                                </div>
                              </div>
                            </div>
                          </TableCell>
                          <TableCell>
                            <Badge variant="outline">
                              {model.model_type === 'embed'
                                ? `Embed (${model.embedding_dimensions})`
                                : 'Text'}
                            </Badge>
                          </TableCell>
                          <TableCell>
                            <Badge variant="outline">
                              {providerInfo.provider}
                            </Badge>
                          </TableCell>
                          <TableCell>
                            <Tooltip>
                              <TooltipTrigger>
                                <div className="flex items-center text-sm text-muted-foreground">
                                  <CalendarIcon className="h-4 w-4 mr-1" />
                                  {format(
                                    new Date(model.created_at),
                                    'MMM dd, yyyy',
                                  )}
                                </div>
                              </TooltipTrigger>
                              <TooltipContent>
                                <p>
                                  {format(new Date(model.created_at), 'PPpp')}
                                </p>
                              </TooltipContent>
                            </Tooltip>
                          </TableCell>
                          <TableCell>
                            <DropdownMenu>
                              <DropdownMenuTrigger asChild>
                                <Button
                                  variant="ghost"
                                  size="icon"
                                  className="h-8 w-8"
                                >
                                  <MoreHorizontalIcon className="h-4 w-4" />
                                </Button>
                              </DropdownMenuTrigger>
                              <DropdownMenuContent align="end">
                                <DropdownMenuItem
                                  onClick={() => handleDeleteClick(model)}
                                  disabled={isDeleting === model.id}
                                  className="text-destructive focus:text-destructive"
                                >
                                  <TrashIcon className="h-4 w-4 mr-2" />
                                  {isDeleting === model.id
                                    ? 'Deleting...'
                                    : 'Delete'}
                                </DropdownMenuItem>
                              </DropdownMenuContent>
                            </DropdownMenu>
                          </TableCell>
                        </TableRow>
                      );
                    })}
                  </TableBody>
                </Table>
              </div>
            )}
          </CardContent>
        </Card>
      </div>
 
      <DeleteModelDialog
        model={modelToDelete}
        open={isDeleteDialogOpen}
        onOpenChange={setIsDeleteDialogOpen}
        onConfirm={handleDeleteConfirm}
      />
    </div>
  );
}