import { Loader2 } from "lucide-react"; import { useEffect, useState } from "react"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "../../select"; import { useClientConfig } from "../hooks/use-config"; type LLamaCloudPipeline = { id: string; name: string; }; type LLamaCloudProject = { id: string; organization_id: string; name: string; is_default: boolean; pipelines: Array; }; type PipelineConfig = { project: string; // project name pipeline: string; // pipeline name }; type LlamaCloudConfig = { projects?: LLamaCloudProject[]; pipeline?: PipelineConfig; }; export interface LlamaCloudSelectorProps { setRequestData: React.Dispatch; } export function LlamaCloudSelector({ setRequestData, }: LlamaCloudSelectorProps) { const { backend } = useClientConfig(); const [config, setConfig] = useState(); useEffect(() => { if (process.env.NEXT_PUBLIC_USE_LLAMACLOUD === "true" && !config) { fetch(`${backend}/api/chat/config/llamacloud`) .then((response) => response.json()) .then((data) => { setConfig(data); setRequestData({ llamaCloudPipeline: data.pipeline, }); }) .catch((error) => console.error("Error fetching config", error)); } }, [backend, config, setRequestData]); const setPipeline = (pipelineConfig?: PipelineConfig) => { setConfig((prevConfig: any) => ({ ...prevConfig, pipeline: pipelineConfig, })); setRequestData((prevData: any) => { if (!prevData) return { llamaCloudPipeline: pipelineConfig }; return { ...prevData, llamaCloudPipeline: pipelineConfig, }; }); }; const handlePipelineSelect = async (value: string) => { setPipeline(JSON.parse(value) as PipelineConfig); }; if (!config) { return (
); } if (!isValid(config)) { return (

Invalid LlamaCloud configuration. Check console logs.

); } const { projects, pipeline } = config; return ( ); } function isValid(config: LlamaCloudConfig): boolean { const { projects, pipeline } = config; if (!projects?.length) return false; if (!pipeline) return false; const matchedProject = projects.find( (project: LLamaCloudProject) => project.name === pipeline.project, ); if (!matchedProject) { console.error( `LlamaCloud project ${pipeline.project} not found. Check LLAMA_CLOUD_PROJECT_NAME variable`, ); return false; } const pipelineExists = matchedProject.pipelines.some( (p) => p.name === pipeline.pipeline, ); if (!pipelineExists) { console.error( `LlamaCloud pipeline ${pipeline.pipeline} not found. Check LLAMA_CLOUD_INDEX_NAME variable`, ); return false; } return true; }