Skip to content

The platform for continuously improving AI agents.

7.3 KB
Raw Blame
import { InspectorDisclosure } from "@/components/ai-components/step-inspector";
import { ModelProviderChip } from "@/components/model-provider-chip";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import type { EditorBlock } from "./canvas-document";
import type { ExperimentDocument, NodeReceipt } from "./document";
import { modelCandidates, nodeFacts, nodeInputs, outputFacts } from "./node-overview-data";
import { nodeSummary } from "./node-summary";
import { TrainingNode } from "./training-node";

const portLabel = (port: string) =>
  ({
    baseline: "Baseline",
    data: "Dataset",
    development: "Validation",
    final: "Holdout",
    model: "Model",
    predictions: "Predictions",
    scores: "Scores",
    suite: "Benchmark",
    train: "Training",
  })[port] ?? port.replaceAll("_", " ");

export function NodeOverview({
  block,
  document,
  receipt,
  attempts,
  onSelect,
  onInspect,
  onEdit,
  onChange,
  locked,
}: {
  block: EditorBlock;
  document: ExperimentDocument;
  receipt?: NodeReceipt;
  attempts: number;
  onInspect: () => void;
  onSelect: (id: string) => void;
  onEdit: () => void;
  onChange: (block: EditorBlock) => void;
  locked: boolean;
}) {
  if (block.kind === "train")
    return (
      <TrainingNode
        block={block}
        document={document}
        locked={locked}
        onChange={onChange}
        onEdit={onEdit}
        onInspect={onInspect}
        receipt={receipt}
      />
    );
  const models = modelCandidates(document, block.id);
  const inputs = nodeInputs(document, block.id).filter((input) => input.port !== "model");
  const outputs = receipt?.current ? Object.entries(receipt.outputs) : [];
  const facts = nodeFacts(block);
  const results = outputFacts(receipt).filter(
    ([label, value]) => label !== "Checkpoint" && value !== "Artifact retained"
  );

  return (
    <div className="nodrag nopan space-y-3">
      {models.length > 0 && (
        <div className="space-y-1">
          {models.map((model) => {
            const target = model.nodeId ?? model.id;
            const role = [
              model.kind === "trained" ? "Fine-tuned" : model.kind === "foundation" ? "Base" : null,
              model.baseline && "Baseline",
            ]
              .filter(Boolean)
              .join(" · ");
            return (
              <button
                aria-label={`Inspect model: ${model.model ?? model.title}${role ? ` (${role})` : ""}`}
                className="flex w-full min-w-0 items-center gap-2 rounded-sm py-1 text-left outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring/50"
                key={model.id}
                onClick={() => (target === block.id ? onEdit() : onSelect(target))}
                type="button"
              >
                {model.model ? (
                  <ModelProviderChip
                    className="h-auto min-w-0 flex-1 justify-start border-0 bg-transparent p-0 text-sm"
                    model={model.model}
                    showProviderName={false}
                  />
                ) : (
                  <span className="min-w-0 flex-1 truncate text-sm">{model.title}</span>
                )}
                {role && <span className="shrink-0 text-xs text-muted-foreground">{role}</span>}
              </button>
            );
          })}
        </div>
      )}
      {inputs.length > 0 && (
        <dl aria-label="Connected inputs" className="space-y-1">
          {inputs.map((input) => (
            <div
              className="grid grid-cols-[76px_minmax(0,1fr)] items-center gap-2 text-xs"
              key={`${input.port}:${input.id}`}
            >
              <dt className="text-muted-foreground">{portLabel(input.port)}</dt>
              <dd className="min-w-0">
                <button
                  aria-label={`Show ${portLabel(input.port).toLowerCase()} input: ${input.title}`}
                  className="flex h-6 w-full min-w-0 items-center gap-1.5 rounded-sm px-1 text-left outline-none transition-colors duration-150 hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring/50"
                  onClick={() => onSelect(input.id)}
                  type="button"
                >
                  <Icon.link className="size-3 shrink-0 text-muted-foreground" />
                  <span className="truncate" title={`${input.title} · ${input.output}`}>
                    {input.title}
                  </span>
                </button>
              </dd>
            </div>
          ))}
        </dl>
      )}
      {facts.length > 0 ? (
        <dl className="space-y-1">
          {facts.map(([label, value]) => (
            <div className="flex items-baseline justify-between gap-3 text-xs" key={label}>
              <dt className="text-muted-foreground">{label}</dt>
              <dd className="truncate text-right tabular-nums" title={value}>
                {value}
              </dd>
            </div>
          ))}
        </dl>
      ) : null}
      {block.kind === "split" && outputs.length > 0 && (
        <dl aria-label="Partition outputs" className="space-y-1 rounded-md bg-background p-2">
          {outputs.map(([port, output]) => (
            <div className="flex min-h-6 items-center justify-between gap-2 text-xs" key={port}>
              <dt className="flex items-center gap-1.5">
                <Icon.dataset className="size-3 text-cat-1" />
                {portLabel(port)}
              </dt>
              <dd className="flex items-center gap-1 text-muted-foreground">
                {typeof output.rows === "number"
                  ? `${output.rows.toLocaleString()} rows`
                  : output.sealed
                    ? "Sealed"
                    : "Retained"}
                {output.sealed && <Icon.lock className="size-3" />}
              </dd>
            </div>
          ))}
        </dl>
      )}
      {!models.length && !inputs.length && !facts.length && (
        <p className="text-xs text-muted-foreground">{nodeSummary(block, receipt)}</p>
      )}
      {block.notes && (
        <InspectorDisclosure title="Notes">
          <p className="whitespace-pre-wrap text-xs text-muted-foreground">{block.notes}</p>
        </InspectorDisclosure>
      )}
      {results.length > 0 && !["train", "evaluate"].includes(block.kind) && (
        <dl className="space-y-1">
          {results.map(([label, value]) => (
            <div className="flex justify-between gap-2 text-xs" key={label}>
              <dt className="text-muted-foreground">{label}</dt>
              <dd className="tabular-nums">{value}</dd>
            </div>
          ))}
        </dl>
      )}
      <div className="flex items-center justify-between gap-1 pt-1">
        <Button onClick={onEdit} size="xs" variant="secondary">
          <Icon.settings />
          {locked ? "View settings" : "Settings"}
        </Button>
        {outputs.length > 0 && (
          <Button onClick={onInspect} size="xs" variant="secondary">
            {["evaluate", "qualify"].includes(block.kind) ? "Results" : "Outputs"}
            <Icon.chevronRight />
          </Button>
        )}
      </div>
      {(receipt?.reused_from || attempts > 1) && (
        <p className="text-xs text-muted-foreground">
          {[receipt?.reused_from && "Reused result", attempts > 1 && `${attempts} attempts`]
            .filter(Boolean)
            .join(" · ")}
        </p>
      )}
    </div>
  );
}