Skip to content

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

3.1 KB
Raw Blame
/* eslint-disable @repo/no-style-props */
import React from "react";
import { ArrowUpRight } from "@langfuse/icons";
import { Button } from "@/src/components/ui/button";
import { CodeMirrorEditor } from "@/src/components/editor/CodeMirrorEditor";
import { showErrorToast } from "@/src/features/notifications/showErrorToast";
import { assertUnreachable } from "@langfuse/shared";

type JSONSchemaEditorMode = "json"; // Future: "json" | "builder"

type JSONSchemaEditorProps = {
  /**
   * The JSON Schema as a string
   */
  value: string;
  /**
   * Callback when schema changes
   */
  onChange: (value: string) => void;
  /**
   * Editor mode - currently only "json", future: visual "builder"
   */
  mode?: JSONSchemaEditorMode;
  /**
   * Maximum height CSS class
   */
  className?: string;
  /**
   * Whether the editor is disabled
   */
  disabled?: boolean;
  /**
   * Show help text with link to JSON Schema docs
   */
  showHelp?: boolean;
};

/**
 * Reusable JSON Schema editor component
 * Currently supports JSON text editing mode
 * Designed to be extended with visual schema builder in the future
 */
export const JSONSchemaEditor: React.FC<JSONSchemaEditorProps> = ({
  value,
  onChange,
  mode = "json",
  className = "max-h-[25vh]",
  disabled = false,
  showHelp = true,
}) => {
  const prettifyJson = () => {
    try {
      const parsedJson = JSON.parse(value);
      const prettified = JSON.stringify(parsedJson, null, 2);
      onChange(prettified);
    } catch {
      showErrorToast(
        "Failed to prettify JSON",
        "Please verify your input is valid JSON",
        {
          operation: "json_schema.prettify",
          errorOrigin: "frontend",
          errorCategory: "user_input",
        },
        "WARNING",
      );
    }
  };

  // Future: Add builder mode UI here
  if (mode === "json") {
    return (
      <div className="flex flex-col gap-2">
        {showHelp && (
          <p className="text-muted-foreground text-sm">
            Define the structure using JSON Schema format.{" "}
            <a
              href="https://json-schema.org/learn/miscellaneous-examples"
              target="_blank"
              rel="noopener noreferrer"
              className="hover:text-foreground inline-flex items-center underline"
            >
              See JSON Schema examples
              <ArrowUpRight size="sm" __rawClassName="ml-0.5" />
            </a>
          </p>
        )}
        <div className="relative flex flex-col gap-1">
          <CodeMirrorEditor
            value={value}
            onChange={onChange}
            mode="json"
            minHeight={100}
            className={className}
            editable={!disabled}
          />
          <Button
            type="button"
            variant="outline"
            size="sm"
            onClick={prettifyJson}
            disabled={disabled}
            className="absolute top-3 right-3 text-xs"
          >
            Prettify
          </Button>
        </div>
        <p className="text-muted-foreground text-xs">
          Must be a valid JSON Schema object
        </p>
      </div>
    );
  }

  // Future mode implementations go here
  return assertUnreachable(mode);
};