Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

8.2 KB
Raw Blame
import React, { useEffect, useId, useState } from 'react';
import { ChevronRight } from 'lucide-react';
import { localClock } from '../clock';
import { RouteInfo, useRouteChip } from './RouteChip';
import { EngineChip } from './Signature';
import { SentToTelegram } from './TelegramMarks';
import { DoneMarker } from './DoneMarker';
import { ranOnWords } from '../modelUsage';
import type { ReplyDone } from '../replyDone';
import { detailsLabel, type ReplyOutcome } from '../replyOutcome';

export interface ReplyFacts {
  /** "Claude · Opus 5.5": the engine and model the reply ran on. */
  ranOn?: string | null;
  account?: string | null;
  /** 30e: a CLI reply's effort in words ("medium effort"), shown after Ran on; never invented when absent. */
  effort?: string | null;
  tokens?: { label: string; detail: string } | null;
  /** Details' Cost row in words (replyCostWords); `tokens.detail` is its tooltip. */
  cost?: { text: string; note: string | null } | null;
}

export interface ReplyMetaProps extends ReplyFacts {
  name: string;
  /** The message's created_at; no time is shown without one (a reply still streaming). */
  at?: string | null;
  /** Drawn before the name on phones, where the avatar column is hidden. */
  avatar?: React.ReactNode;
  /** Whether a step failed: the Details toggle says so while it's folded. Superseded by `outcome` when given. */
  failed?: boolean;
  /** 26d: what the steps add up to (replyOutcome); the folded toggle says which step failed or didn't run. */
  outcome?: ReplyOutcome | null;
  /** 26d: changes each time the line under the reply asks Details to open (a nonce; 0 or absent does nothing). */
  openRequest?: number;
  /** One muted line under the name, e.g. "Claude is out until 03:00 · answered on Codex (GPT-6.1)". */
  note?: string | null;
  /** Why this bot answered: Details' first row, in words, with "Wrong bot?" after it. */
  route?: RouteInfo | null;
  /** A job's final reply (replyDone.doneOf): the quiet marker after the name. */
  done?: ReplyDone | null;
  /** Set after a failover: the engines the reply moved between, with the reason on hover. Only then is
   *  the engine on the name line; otherwise it waits in Details. */
  handoff?: { from: string; to: string; why: string } | null;
  now?: number;
  /** The reply also went out to Telegram (G4): "· sent to Telegram too" after the time. */
  sentToTelegram?: boolean;
  /** Steps and the reasoning item; shown with the facts when Details is open. */
  children?: React.ReactNode;
}

/** The quiet line for a reply that ran on another account. It wraps rather than clips, so it fits at 390 px. */
// The reply's name line, shared with the live row in ChatView (17c) so the name line keeps its type and height
// when the reply lands. replyNameLine(extra) puts extra between the box and the type classes: the finished line
// passes 'sm:flex-wrap' (its class string is unchanged), the live line REPLY_NAME_LINE_LIVE_HEIGHT.
export const REPLY_NAME = 'min-w-0 truncate text-[13px] font-semibold text-zinc-50';
const REPLY_NAME_LINE_BOX = 'mb-1.5 flex min-w-0 max-w-full flex-nowrap';
const REPLY_NAME_LINE_TYPE = 'items-center gap-x-2 gap-y-1 text-xs text-zinc-400 font-medium';
// The Details button's height: min-h-6, and 44 px where modern.css makes every control a 44 px tap target
// (touch screens below 768 px), so the live line is as tall as the finished one there too.
export const REPLY_NAME_LINE_LIVE_HEIGHT = 'min-h-6 [@media(max-width:767px)_and_(pointer:coarse)]:min-h-11';
export const replyNameLine = (extra: string, wrap = false): string =>
  `${wrap ? REPLY_NAME_LINE_BOX.replace('flex-nowrap', 'flex-wrap') : REPLY_NAME_LINE_BOX} ${extra} ${REPLY_NAME_LINE_TYPE}`;

export const FailoverNote: React.FC<{ text: string }> = ({ text }) => (
  <p data-failover-note="" className="-mt-0.5 mb-1.5 max-w-full text-xs leading-snug text-zinc-500 break-words">
    {text}
  </p>
);

const hasContent = (n: React.ReactNode) => React.Children.toArray(n).length > 0;

/** A bot reply's header: one line, "Name · [Done] · 3:42 PM · Details", and a Details toggle holding the rest in words. */
export const ReplyMeta: React.FC<ReplyMetaProps> = ({ name, at, avatar, ranOn, account, effort, tokens, cost, failed = false, outcome, openRequest, note, route, done, handoff, now, sentToTelegram = false, children }) => {
  const [open, setOpen] = useState(false);
  useEffect(() => {
    if (openRequest) setOpen(true);
  }, [openRequest]);
  const told: ReplyOutcome | null = outcome !== undefined ? outcome : failed ? { kind: 'failed', stepId: '' } : null;
  // Rose for a step that didn't work for good, amber for one nobody answered, quiet for "failed, then worked".
  const tone = !told || open ? 'text-zinc-400' : told.kind === 'failed' ? 'text-rose-300' : told.kind === 'unanswered' ? 'text-amber-300' : 'text-zinc-300';
  const why = useRouteChip(route);
  const panelId = useId();
  const time = localClock(at, now);
  const facts: Array<[string, React.ReactNode]> = [];
  if (why.reason)
    facts.push([
      `Why ${name}`,
      <>
        {why.reason}
        {why.wrongBot}
      </>,
    ]);
  const ran = ranOnWords(ranOn, account);
  // 30l: "· medium effort" stays one unit when Ran on wraps on a phone; the space before it is still a break point.
  if (ran)
    facts.push([
      'Ran on',
      effort ? (
        <>
          {ran} <span className="whitespace-nowrap" data-ran-on-effort="">· {effort}</span>
        </>
      ) : (
        ran
      ),
    ]);
  else if (effort) facts.push(['Effort', effort]);
  if (cost)
    facts.push([
      'Cost',
      <span title={tokens?.detail}>
        {cost.text}
        {cost.note && <span className="text-zinc-400"> {cost.note}</span>}
      </span>,
    ]);
  else if (tokens) facts.push(['Cost', <span title={tokens.detail}>{tokens.label}</span>]);
  const hasDetails = facts.length > 0 || hasContent(children) || !!why.panel;
  return (
    <div className="w-full max-w-full min-w-0">
      {/* 26d: a Details label that names an outcome is long; on a phone it moves to its own row rather than clip. */}
      <div className={replyNameLine('sm:flex-wrap', !!told)} data-name-line-wraps={told ? '' : undefined}>
        {avatar && <span className="sm:hidden shrink-0 inline-flex">{avatar}</span>}
        <span className={REPLY_NAME}>{name}</span>
        {done && <DoneMarker done={done} />}
        {handoff && <EngineChip label={[ranOn, account].filter(Boolean).join(' · ')} short={ranOn ? ranOn.split(' · ').pop() : null} handoff={handoff} />}
        {time && (
          <span className="shrink-0 whitespace-nowrap text-zinc-400" data-reply-time="">
            <span aria-hidden="true" className="sm:hidden">· </span>
            {time}
          </span>
        )}
        {sentToTelegram && <SentToTelegram />}
        {hasDetails && (
          <button
            type="button"
            onClick={() => setOpen((o) => !o)}
            aria-expanded={open}
            aria-controls={open ? panelId : undefined}
            className={`shrink-0 inline-flex items-center gap-0.5 whitespace-nowrap rounded-md px-1.5 min-h-6 text-xs hover:text-zinc-100 hover:bg-zinc-800 transition-colors motion-reduce:transition-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-400 ${tone}`}
            data-details-outcome={told && !open ? told.kind : undefined}
          >
            {open ? 'Details' : detailsLabel(told)}
            <ChevronRight size={12} className={`transition-transform motion-reduce:transition-none ${open ? 'rotate-90' : ''}`} aria-hidden="true" />
          </button>
        )}
      </div>
      {note && <FailoverNote text={note} />}
      {open && hasDetails && (
        <div id={panelId} className="mb-2 w-full max-w-2xl" data-reply-details="">
          {facts.length > 0 && (
            <dl className="mb-2 grid grid-cols-1 sm:grid-cols-[auto_1fr] gap-x-3 gap-y-0 sm:gap-y-1 text-xs">
              {facts.map(([k, v]) => (
                <React.Fragment key={k}>
                  <dt className="text-zinc-400">{k}</dt>
                  <dd className="mb-1.5 sm:mb-0 text-zinc-200 min-w-0 break-words leading-snug">{v}</dd>
                </React.Fragment>
              ))}
            </dl>
          )}
          {why.panel}
          {children}
        </div>
      )}
    </div>
  );
};