import React, { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { ArrowLeft, Key, Globe } from "lucide-react"; import { useNotification } from "@/context/NotificationContext"; import { api } from "@/lib/api"; import langfuseIcon from "@/static/langfuse.png"; import langsmithIcon from "@/static/langsmith.png"; interface ObservabilityConnectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; editConnection?: { id: string; platform: string; status: string; connected_at: string; last_sync: string | null; }; } type Platform = "langfuse" | "langsmith"; interface LangfuseForm { publicKey: string; secretKey: string; host: string; } interface LangSmithForm { apiKey: string; } export function ObservabilityConnectionDialog({ open, onOpenChange, editConnection, }: ObservabilityConnectionDialogProps) { const [selectedPlatform, setSelectedPlatform] = useState( editConnection ? (editConnection.platform as Platform) : null ); const [isConnecting, setIsConnecting] = useState(false); const [langfuseForm, setLangfuseForm] = useState({ publicKey: "", secretKey: "", host: "https://cloud.langfuse.com", }); const [langsmithForm, setLangsmithForm] = useState({ apiKey: "", }); const { showNotification } = useNotification(); const handlePlatformSelect = (platform: Platform) => { setSelectedPlatform(platform); // Reset forms when switching platforms if (platform === "langfuse") { setLangfuseForm({ publicKey: "", secretKey: "", host: "https://cloud.langfuse.com", }); } else { setLangsmithForm({ apiKey: "", }); } }; const handleBack = () => { setSelectedPlatform(null); setLangfuseForm({ publicKey: "", secretKey: "", host: "https://cloud.langfuse.com", }); setLangsmithForm({ apiKey: "", }); }; const handleConnect = async () => { if (!selectedPlatform) return; // Get current form data based on platform let publicKey = ""; let secretKey = ""; let host = ""; if (selectedPlatform === "langfuse") { if (!langfuseForm.publicKey.trim() || !langfuseForm.secretKey.trim()) { showNotification({ type: "error", title: "Validation Error", message: "Please provide both public and secret keys", }); return; } publicKey = langfuseForm.publicKey.trim(); secretKey = langfuseForm.secretKey.trim(); host = langfuseForm.host.trim(); } else { if (!langsmithForm.apiKey.trim()) { showNotification({ type: "error", title: "Validation Error", message: "Please provide API key", }); return; } publicKey = langsmithForm.apiKey.trim(); secretKey = ""; host = ""; } setIsConnecting(true); try { const connectionData = { platform: selectedPlatform, publicKey: publicKey, secretKey: secretKey, host: host || undefined, }; if (editConnection) { // Update existing connection await api.observability.updateConnection( editConnection.id, connectionData ); showNotification({ type: "success", title: "Connection Updated", message: `Successfully updated ${ selectedPlatform === "langfuse" ? "Langfuse" : "LangSmith" } connection`, }); } else { // Create new connection await api.observability.connect(connectionData); showNotification({ type: "success", title: "Connection Successful", message: `Successfully connected to ${ selectedPlatform === "langfuse" ? "Langfuse" : "LangSmith" }`, }); } // Notify ObservabilitySection to refresh connections window.dispatchEvent(new CustomEvent("observability-connection-updated")); onOpenChange(false); handleBack(); } catch (error) { showNotification({ type: "error", title: editConnection ? "Update Failed" : "Connection Failed", message: error instanceof Error ? error.message : `Failed to ${editConnection ? "update" : "connect to"} platform`, }); } finally { setIsConnecting(false); } }; const handleClose = () => { if (!isConnecting) { onOpenChange(false); handleBack(); } }; const isFormValid = () => { if (selectedPlatform === "langfuse") { return langfuseForm.publicKey.trim() && langfuseForm.secretKey.trim(); } else { return langsmithForm.apiKey.trim(); } }; return ( {selectedPlatform && !editConnection && ( )} {editConnection ? `Edit ${ selectedPlatform === "langfuse" ? "Langfuse" : "LangSmith" } Connection` : "Connect to AI Observability"} {selectedPlatform ? `${editConnection ? "Update" : "Enter"} your ${ selectedPlatform === "langfuse" ? "Langfuse" : "LangSmith" } credentials ${ editConnection ? "to update the connection" : "to connect" }` : "Choose an AI observability platform to connect"} {!selectedPlatform ? (
handlePlatformSelect("langfuse")} >
Langfuse
Langfuse Open-source LLM observability
Connect to Langfuse Cloud or self-hosted instance
handlePlatformSelect("langsmith")} >
LangSmith
LangSmith LangChain's observability platform
Connect to LangSmith by LangChain
) : selectedPlatform === "langfuse" ? (
setLangfuseForm((prev) => ({ ...prev, publicKey: e.target.value, })) } disabled={isConnecting} />
setLangfuseForm((prev) => ({ ...prev, secretKey: e.target.value, })) } disabled={isConnecting} />
setLangfuseForm((prev) => ({ ...prev, host: e.target.value })) } disabled={isConnecting} />
Leave empty for Langfuse Cloud or enter your self-hosted URL
) : (
setLangsmithForm((prev) => ({ ...prev, apiKey: e.target.value, })) } disabled={isConnecting} />
)}
); }