"use client";
import { useAtom, useAtomValue } from "jotai";
import { Dot } from "lucide-react";
import { updateModelRolesMutationAtom } from "@/atoms/model-connections/model-connections-mutation.atoms";
import {
globalModelConnectionsAtom,
modelConnectionsAtom,
modelRolesAtom,
} from "@/atoms/model-connections/model-connections-query.atoms";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Separator } from "@/components/ui/separator";
import type { ConnectionRead, ModelRead } from "@/contracts/types/model-connections.types";
import { ModelProviderConnectionsPanel } from "./model-connections/model-provider-connections-panel";
import { capability, modelLabel } from "./model-connections/model-utils";
import { providerDisplay, providerIcon } from "./model-connections/provider-metadata";
function flattenModels(connections: ConnectionRead[]) {
return connections.flatMap((connection) =>
connection.models.map((model) => ({
...model,
connectionName: providerDisplay(connection.provider).name,
connectionId: connection.id,
provider: connection.provider,
}))
);
}
function roleSelectValue(modelId: number | null | undefined, models: Array<{ id: number }>) {
if (!modelId) return "0";
return models.some((model) => model.id === modelId) ? String(modelId) : "0";
}
export function ModelConnectionsSettings({ searchSpaceId }: { searchSpaceId: number }) {
const [{ data: globalConnections = [] }] = useAtom(globalModelConnectionsAtom);
const [{ data: connections = [] }] = useAtom(modelConnectionsAtom);
const [{ data: roles }] = useAtom(modelRolesAtom);
const updateRoles = useAtomValue(updateModelRolesMutationAtom);
const allConnections = [...globalConnections, ...connections];
const enabledModels = flattenModels(allConnections).filter((model) => model.enabled);
const chatModels = enabledModels.filter((model) => capability(model, "chat"));
const visionModels = enabledModels.filter((model) => capability(model, "vision"));
const imageModels = enabledModels.filter((model) => capability(model, "image_gen"));
function renderModelOption(model: ModelRead & { connectionName: string; provider: string }) {
return (
Pick which enabled model powers chat, vision, and image generation for this search space.
Primary model for chat responses and agent tasks. You can also change it from the chat.
Used to understand images in uploads, documents, connectors, and automations. Falls back to chat model when possible.
Used when generating images in chat.