All files / src/components/chat/CommandForm SentCommand.tsx

100% Statements 13/13
100% Branches 27/27
100% Functions 3/3
100% Lines 10/10

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                          14x 25x 20x 16x 15x                   14x       13x 13x 13x                                     10x                                                      
import type { ChatCommandRecord } from "@/objects/ChatCommand";
import { Workflow } from "lucide-react";
import React from "react";
 
/**
 * A sent command in the thread, from the record on its message
 * (`meta.command`): the line in code type, as the token showed it, then the
 * values its form was given and the free text. The record is what svc-core
 * stored, after the input rules' redaction, so a reload shows the same thing
 * the model was given; the message's own content is the composed task.
 */
 
/** A form value as text: lists joined, objects as JSON. */
export const formatInputValue = (value: unknown): string => {
  if (typeof value === "boolean") return value ? "Yes" : "No";
  if (Array.isArray(value)) return value.map(formatInputValue).join(", ");
  if (value !== null && typeof value === "object") return JSON.stringify(value);
  return value === null || value === undefined ? "" : String(value);
};
 
export interface SentCommandProps {
  record: ChatCommandRecord;
  /** The message's content: the request, for a record stored before
   *  records carried one. */
  content: string;
}
 
export const SentCommand: React.FC<SentCommandProps> = ({
  record,
  content,
}) => {
  const inputs = Object.entries(record.inputs ?? {});
  const request = (record.request ?? content).trim();
  return (
    <>
      <div className="flex justify-end mb-1.5">
        <code
          title={record.target_name}
          className="inline-flex max-w-full items-center gap-1 rounded-md border border-indigo-200 dark:border-indigo-800 bg-indigo-50 dark:bg-indigo-950/50 px-1.5 py-0.5 font-mono text-xs text-indigo-700 dark:text-indigo-300"
        >
          <Workflow className="size-3 shrink-0" aria-hidden="true" />
          <span className="truncate">{record.line}</span>
        </code>
      </div>
      {(inputs.length > 0 || request) && (
        <div className="bg-indigo-500 text-white px-4 py-3 rounded-2xl rounded-tr-sm text-sm leading-relaxed shadow-sm space-y-2">
          {inputs.length > 0 && (
            <dl
              aria-label={`${record.target_name} inputs`}
              className="grid grid-cols-[auto_minmax(0,1fr)] gap-x-3 gap-y-1"
            >
              {inputs.map(([name, value]) => (
                <React.Fragment key={name}>
                  <dt className="font-mono text-xs leading-5 text-indigo-100">
                    {name}
                  </dt>
                  <dd className="min-w-0 whitespace-pre-wrap wrap-anywhere">
                    {formatInputValue(value)}
                  </dd>
                </React.Fragment>
              ))}
            </dl>
          )}
          {request && (
            <p
              className={`whitespace-pre-wrap ${
                inputs.length ? "border-t border-white/20 pt-2" : ""
              }`}
            >
              {request}
            </p>
          )}
        </div>
      )}
    </>
  );
};
 
export default SentCommand;