Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

4.4 KB
Raw Blame
import { useMemo, useState } from 'react';
import MarkdownOutput from './MarkdownOutput';

export const isToolLine = (line) =>
  line.startsWith('🔧') || line.startsWith('  →') || line.startsWith('  ↳') || line.startsWith('[stderr]');

// Lifecycle marker lines (emitted from agentTuiSpawning.js for TUI agents)
// should render as their own one-line blocks instead of getting collapsed into
// the surrounding markdown — markdown joins consecutive single-`\n` lines
// onto one rendered line, which made the 📟 / 💡 / ✅ events run together
// in the CoS agent output panel.
const LIFECYCLE_PREFIXES = ['📟', '💡', '✅'];
export const isLifecycleLine = (line) =>
  LIFECYCLE_PREFIXES.some(p => line.startsWith(p));

const INITIAL_BLOCKS = 80;
const LOAD_MORE_BLOCKS = 120;

function renderBlock(block, i, linkifyText) {
  if (block.type === 'tool') {
    const line = block.line;
    if (line.startsWith('🔧')) {
      return <div key={i} className="py-0.5 text-xs font-mono text-port-accent break-all">{line}</div>;
    }
    if (line.startsWith('  →')) {
      return <div key={i} className="py-0.5 text-xs font-mono text-gray-500 pl-4 break-all">{line.substring(4)}</div>;
    }
    if (line.startsWith('  ↳')) {
      return <div key={i} className="py-0.5 text-xs font-mono text-gray-600 pl-4 break-all">{line.substring(4)}</div>;
    }
    return <div key={i} className="py-0.5 text-xs font-mono text-yellow-500 break-all">{line}</div>;
  }
  if (block.type === 'lifecycle') {
    return <div key={i} className="py-0.5 text-xs text-gray-400 break-words">{block.line}</div>;
  }
  return <MarkdownOutput key={i} content={block.content} linkifyText={linkifyText} preserveLineBreaks />;
}

const PAGE_BUTTON_CLASS = 'w-full py-2 text-xs text-port-accent hover:text-white bg-port-border/30 hover:bg-port-border/50 rounded transition-colors min-h-[40px]';

// `linkifyText` is forwarded to MarkdownOutput so a caller that knows the
// record (a CoS run and its tracker) can link the bare `#7640` in its prose.
//
// `anchor` picks which end of a long transcript the bounded window shows.
// `head` (finished transcripts) pages forward from the start. `tail` is for a
// RUNNING agent, whose caller feeds a sliding buffer that appends at the end
// and drops from the front: a head window stopped changing after the first
// page and hid every new line under "Show more", so a working agent read as
// stalled (#12028). The tail window always holds the newest blocks and pages
// backwards with "Show earlier".
export default function OutputBlocks({ output, linkifyText = null, anchor = 'head' }) {
  const [visibleCount, setVisibleCount] = useState(INITIAL_BLOCKS);

  // Group consecutive lines: tool lines render as monospace, lifecycle markers
  // render as their own one-line blocks, content lines fold into a single
  // markdown block.
  const blocks = useMemo(() => {
    const result = [];
    let mdLines = [];

    const flushMd = () => {
      if (mdLines.length > 0) {
        result.push({ type: 'md', content: mdLines.join('\n') });
        mdLines = [];
      }
    };

    for (const o of output) {
      const line = o.line || '';
      if (isToolLine(line)) {
        flushMd();
        result.push({ type: 'tool', line });
      } else if (isLifecycleLine(line)) {
        flushMd();
        result.push({ type: 'lifecycle', line });
      } else {
        mdLines.push(line);
      }
    }
    flushMd();
    return result;
  }, [output]);

  const hasMore = blocks.length > visibleCount;
  const tail = anchor === 'tail';
  const start = tail && hasMore ? blocks.length - visibleCount : 0;
  const visible = tail ? blocks.slice(start) : blocks.slice(0, visibleCount);
  const hidden = blocks.length - visible.length;
  const revealMore = () => setVisibleCount(prev => prev + LOAD_MORE_BLOCKS);

  return (
    <div className="space-y-0.5 min-w-0 overflow-hidden">
      {tail && hasMore && (
        <button type="button" onClick={revealMore} className={PAGE_BUTTON_CLASS}>
          Show earlier ({hidden})
        </button>
      )}
      {/* Keyed by absolute block index so a sliding tail window keeps each
          block's element rather than remounting the whole page per line. */}
      {visible.map((block, i) => renderBlock(block, start + i, linkifyText))}
      {!tail && hasMore && (
        <button type="button" onClick={revealMore} className={PAGE_BUTTON_CLASS}>
          Show more ({hidden} remaining)
        </button>
      )}
    </div>
  );
}