All files / src/apps/user/pages/agents/skills/run RunValue.tsx

12.5% Statements 4/32
0% Branches 0/31
0% Functions 0/6
13.33% Lines 4/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                        1x     1x                                                 1x           1x                                                                                      
import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import React from "react";
 
/**
 * One value from a run — an input, a declared output, a field of a
 * structured node result — drawn by its shape rather than dumped as JSON.
 *
 * A report is prose, so long or multi-line text is rendered as markdown; a
 * list of findings is a list; a flat object is a table of its fields. Only
 * what has no better form (nested structures) falls back to JSON.
 */
 
const isScalar = (v: unknown) =>
  v === null || ["string", "number", "boolean"].includes(typeof v);
 
const Scalar: React.FC<{ value: unknown }> = ({ value }) => {
  if (value === null || value === undefined)
    return <span className="text-gray-400 italic">empty</span>;
  if (typeof value === "boolean")
    return (
      <span className="font-mono text-sm text-gray-800 dark:text-gray-200">
        {value ? "true" : "false"}
      </span>
    );
  if (typeof value === "number")
    return (
      <span className="font-mono text-sm text-gray-800 dark:text-gray-200">
        {value.toLocaleString()}
      </span>
    );
  const text = String(value);
  if (text.includes("\n") || text.length > 160)
    return <RichMarkdown content={text} className="text-sm" />;
  return (
    <span className="text-sm text-gray-800 dark:text-gray-200 break-words">
      {text}
    </span>
  );
};
 
const Json: React.FC<{ value: unknown }> = ({ value }) => (
  <pre className="text-[11px] font-mono bg-gray-50 dark:bg-gray-900/50 border border-gray-100 dark:border-gray-700 rounded-lg p-3 overflow-x-auto max-h-80 text-gray-700 dark:text-gray-300">
    {JSON.stringify(value, null, 2)}
  </pre>
);
 
const RunValue: React.FC<{ value: unknown; depth?: number }> = ({
  value,
  depth = 0,
}) => {
  if (isScalar(value) || value === undefined) return <Scalar value={value} />;
 
  if (Array.isArray(value)) {
    if (value.length === 0)
      return <span className="text-gray-400 italic text-sm">none</span>;
    if (value.every(isScalar))
      return (
        <ul className="list-disc pl-5 space-y-0.5 text-sm text-gray-800 dark:text-gray-200">
          {value.map((item, i) => (
            <li key={i}>
              <Scalar value={item} />
            </li>
          ))}
        </ul>
      );
    return <Json value={value} />;
  }
 
  const entries = Object.entries(value as Record<string, unknown>);
  if (entries.length === 0)
    return <span className="text-gray-400 italic text-sm">empty</span>;
  if (depth > 0) return <Json value={value} />;
  return (
    <dl className="divide-y divide-gray-100 dark:divide-gray-700 rounded-lg border border-gray-100 dark:border-gray-700">
      {entries.map(([k, v]) => (
        <div key={k} className="grid sm:grid-cols-[10rem_1fr] gap-1 sm:gap-3 px-3 py-2">
          <dt className="text-[11px] font-mono text-gray-500 dark:text-gray-400 pt-0.5 break-all">
            {k}
          </dt>
          <dd className="min-w-0">
            <RunValue value={v} depth={depth + 1} />
          </dd>
        </div>
      ))}
    </dl>
  );
};
 
export default RunValue;