Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

9.2 KB
Raw Blame
import React, { useState } from "react";
import { Button } from "@/src/components/ui/button";
import { Badge } from "@/src/components/design-system/Badge/Badge";
import { Wrench, Braces, Variable, type IconComponent } from "@langfuse/icons";
import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/src/components/ui/popover";
import { usePlaygroundContext } from "../context/PlaygroundProvider";
import { usePlaygroundWindowSize } from "../hooks/usePlaygroundWindowSize";
import {
  PlaygroundTools,
  PlaygroundToolsPopover,
  type PlaygroundToolDialogRequest,
} from "./PlaygroundTools";
import {
  StructuredOutputSchemaSection,
  StructuredOutputSchemaPopover,
  type StructuredOutputSchemaDialogRequest,
} from "./StructuredOutputSchemaSection";
import { Variables } from "./Variables";
import { MessagePlaceholders } from "./MessagePlaceholders";
import { CreateOrEditLLMToolDialog } from "./CreateOrEditLLMToolDialog";
import { CreateOrEditLLMSchemaDialog } from "./CreateOrEditLLMSchemaDialog";
import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";

function usePopoverToDialog<T>() {
  const [popoverOpen, setPopoverOpen] = useState(false);
  const [dialogOpen, setDialogOpen] = useState(false);
  const [request, setRequest] = useState<{ id: number; payload: T }>();

  const openDialog = (payload: T) => {
    setPopoverOpen(false);
    setRequest((previous) => ({ id: (previous?.id ?? 0) + 1, payload }));
    setDialogOpen(true);
  };

  const handleDialogOpenChange = (open: boolean) => {
    if (open) return;
    setDialogOpen(false);
    setPopoverOpen(true);
  };

  return {
    popoverOpen,
    setPopoverOpen,
    request,
    dialogOpen,
    openDialog,
    handleDialogOpenChange,
  };
}

export const ConfigurationDropdowns: React.FC = () => {
  const { containerRef, width, isVeryCompact, isCompact } =
    usePlaygroundWindowSize();
  const {
    tools,
    structuredOutputSchema,
    promptVariables,
    messagePlaceholders,
  } = usePlaygroundContext();
  const projectId = useProjectIdFromURL();
  const toolsOverlay = usePopoverToDialog<PlaygroundToolDialogRequest>();
  const schemaOverlay =
    usePopoverToDialog<StructuredOutputSchemaDialogRequest>();

  const toolsCount = tools.length;
  const hasSchema = structuredOutputSchema ? 1 : 0;
  const variablesCount = promptVariables.length + messagePlaceholders.length;
  const toolsPopoverWidth =
    width > 0 ? Math.min(Math.max(width - 24, 0), 320) : undefined;

  // Helper function to get responsive content (text or icon)
  const getResponsiveContent = (
    fullText: string,
    Icon: IconComponent,
    abbreviation?: string,
  ) => {
    if (isVeryCompact) {
      return <Icon size="base" />;
    }
    if (isCompact) {
      return (
        <>
          <Icon size="base" />
          <span className="text-sm">{abbreviation ?? fullText}</span>
        </>
      );
    }
    return (
      <>
        <Icon size="base" />
        <span className="text-sm">{fullText}</span>
      </>
    );
  };

  return (
    <div ref={containerRef} className="bg-muted/25 shrink-0 border-b px-3 py-2">
      <div className="flex items-center justify-start gap-2">
        {/* Tools Dropdown */}
        <Popover
          open={toolsOverlay.popoverOpen}
          onOpenChange={toolsOverlay.setPopoverOpen}
        >
          <PopoverTrigger asChild>
            <Button variant="outline" size="sm" className="h-8 gap-2">
              {getResponsiveContent("Tools", Wrench)}
              {toolsCount > 0 && (
                <Badge color="filled" size="sm" text={String(toolsCount)} />
              )}
              <DropdownIndicator size="sm" nudge />
            </Button>
          </PopoverTrigger>
          <PopoverContent
            className="w-80 max-w-[calc(100vw-1rem)] min-w-0 overflow-hidden p-4"
            align="start"
            style={toolsPopoverWidth ? { width: toolsPopoverWidth } : undefined}
          >
            <div className="mb-3">
              <h4 className="mb-1 text-sm font-bold">Tools</h4>
              <p className="text-muted-foreground text-xs">
                Configure tools for your model to use.
              </p>
            </div>
            {toolsCount > 0 ? (
              <div className="mb-3">
                <PlaygroundTools onOpenToolDialog={toolsOverlay.openDialog} />
              </div>
            ) : (
              <div className="mb-3">
                <p className="text-muted-foreground text-xs">
                  No tools attached.
                </p>
              </div>
            )}
            <div className="border-t pt-3">
              <PlaygroundToolsPopover
                onOpenToolDialog={toolsOverlay.openDialog}
              />
            </div>
          </PopoverContent>
        </Popover>
        {toolsOverlay.request && projectId && (
          <CreateOrEditLLMToolDialog
            key={`tool-${toolsOverlay.request.id}`}
            projectId={projectId}
            open={toolsOverlay.dialogOpen}
            onOpenChange={toolsOverlay.handleDialogOpenChange}
            onSave={toolsOverlay.request.payload.onSave}
            onDelete={toolsOverlay.request.payload.onDelete}
            existingLlmTool={toolsOverlay.request.payload.existingLlmTool}
            defaultValues={toolsOverlay.request.payload.defaultValues}
          />
        )}

        {/* Structured Output Dropdown */}
        <Popover
          open={schemaOverlay.popoverOpen}
          onOpenChange={schemaOverlay.setPopoverOpen}
        >
          <PopoverTrigger asChild>
            <Button variant="outline" size="sm" className="h-8 gap-2">
              {getResponsiveContent("Schema", Braces)}
              {hasSchema > 0 && (
                <Badge color="filled" size="sm" text={String(hasSchema)} />
              )}
              <DropdownIndicator size="sm" nudge />
            </Button>
          </PopoverTrigger>
          <PopoverContent className="w-80 p-4" align="start">
            <div className="mb-3">
              <h4 className="mb-1 text-sm font-bold">Structured Output</h4>
              <p className="text-muted-foreground text-xs">
                Configure JSON schema for structured output.
              </p>
            </div>
            {structuredOutputSchema ? (
              <div className="mb-3">
                <StructuredOutputSchemaSection
                  onOpenSchemaDialog={schemaOverlay.openDialog}
                />
              </div>
            ) : (
              <div className="mb-3">
                <p className="text-muted-foreground text-xs">
                  No schema provided.
                </p>
              </div>
            )}
            <div className="border-t pt-3">
              <StructuredOutputSchemaPopover
                onOpenSchemaDialog={schemaOverlay.openDialog}
              />
            </div>
          </PopoverContent>
        </Popover>
        {schemaOverlay.request && projectId && (
          <CreateOrEditLLMSchemaDialog
            key={`schema-${schemaOverlay.request.id}`}
            projectId={projectId}
            open={schemaOverlay.dialogOpen}
            onOpenChange={schemaOverlay.handleDialogOpenChange}
            onSave={schemaOverlay.request.payload.onSave}
            onDelete={schemaOverlay.request.payload.onDelete}
            existingLlmSchema={schemaOverlay.request.payload.existingLlmSchema}
            defaultValues={schemaOverlay.request.payload.defaultValues}
          />
        )}

        {/* Variables & Placeholders Dropdown */}
        <Popover>
          <PopoverTrigger asChild>
            <Button variant="outline" size="sm" className="h-8 gap-2">
              {getResponsiveContent("Variables", Variable, "Vars")}
              {variablesCount > 0 && (
                <Badge color="filled" size="sm" text={String(variablesCount)} />
              )}
              <DropdownIndicator size="sm" nudge />
            </Button>
          </PopoverTrigger>
          <PopoverContent className="w-80 p-4" align="start">
            <div className="mb-3">
              <h4 className="mb-1 text-sm font-bold">
                Variables & Message Placeholders
              </h4>
              <p className="text-muted-foreground text-xs">
                Configure variables and message placeholders for your prompts.
              </p>
            </div>
            {variablesCount > 0 ? (
              <div
                className="mb-3"
                style={{ maxHeight: "50vh", overflowY: "auto" }}
              >
                <div className="space-y-4">
                  <div>
                    <h5 className="mb-2 text-xs font-bold">Variables</h5>
                    <Variables />
                  </div>
                  <div>
                    <h5 className="mb-2 text-xs font-bold">
                      Message Placeholders
                    </h5>
                    <MessagePlaceholders />
                  </div>
                </div>
              </div>
            ) : (
              <div className="mb-3">
                <p className="text-muted-foreground text-xs">
                  No variables or message placeholders defined.
                </p>
              </div>
            )}
          </PopoverContent>
        </Popover>
      </div>
    </div>
  );
};