Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

7.6 KB
Raw Blame
/** Top-aligned agent messages with end-following for overflowing transcripts. */
import { ScrollToBottomOverlay } from '@channel/Channel/ScrollToBottomOverlay';
import {
  ThreadList,
  type ThreadListNavigation,
  type ThreadListScrollState,
} from '@channel/Channel/ThreadList';
import { FloatRegions } from '@components/app/mobile/float-regions/float-region-state';
import { useSplitPanel } from '@components/app/split-layout/layoutUtils';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import {
  createEffect,
  createMemo,
  createSignal,
  on,
  onCleanup,
  Show,
} from 'solid-js';
import { match } from 'ts-pattern';
import { SessionEventRow } from '../components/session-event-row';
import { useAgentSession } from '../context/AgentSessionContext';
import type { AgentMessageTarget } from '../core/search-location';
import { isLiveTurn, liveTurnMessage } from '../state/live-turn';
import { timelineNotifications } from '../state/session-notifications';
import { WorkingLine } from '../ui/WorkingLine';
import { Message } from './AgentMessage';
import { ReplyToSelection } from './ReplyToSelection';

export function Transcript(props: { searchTarget?: AgentMessageTarget }) {
  const { messages, metadata, quoteSelection, sessionId, turn, userId } =
    useAgentSession();
  // At most one turn runs, and the fold's `turn` is the one word on whether
  // it does - so which message shimmers is decided here, once, not by each
  // message from its own `stop` (see `state/live-turn`). A speculated stop
  // reads as `stopping`, which settles the live message before the log
  // closes it.
  const working = () => {
    const state = turn();
    return state === 'starting' || state === 'running' || state === 'blocked';
  };
  const liveTurn = () => liveTurnMessage(messages(), working());
  const initialTarget = props.searchTarget;
  const initialSessionId = sessionId();
  const splitPanel = useSplitPanel();
  const [transcriptEl, setTranscriptEl] = createSignal<HTMLDivElement>();
  const [scrollState, setScrollState] = createSignal<ThreadListScrollState>();
  const [navigation, setNavigation] = createSignal<ThreadListNavigation>();
  const [highlightedId, setHighlightedId] = createSignal<string>();

  // Object identity and array positions can change as the live fold reconciles.
  const messageById = createMemo(
    () =>
      new Map(
        messages().map((message) => [
          `${message.agentSessionId}:${message.key}`,
          message,
        ])
      )
  );
  // The turn is open and the agent has said nothing yet: the newest message
  // is the user's, so no agent row exists to carry the working line. One
  // extra row at the tail says the wait is work, not a stall - a prompt still
  // on the wire (`starting`) included.
  const workingKey = () => `${sessionId() ?? ''}:working`;
  const showsWorking = createMemo(() => {
    const state = turn();
    if (state !== 'starting' && state !== 'running') return false;
    const last = messages().at(-1);
    return last?.author.kind === 'user';
  });
  // What the wait is: the prompt still on the wire, or the agent at work.
  const workingLabel = () =>
    match(turn())
      .with('starting', () => 'Sending')
      .otherwise(() => 'Working');
  const eventsById = createMemo(
    () =>
      new Map(
        timelineNotifications(messages(), metadata()?.notifications ?? []).map(
          (event) => [`notification:${event.id}`, event]
        )
      )
  );
  const keys = createMemo(() => {
    const rows = [
      ...[...messageById()].map(([key, message]) => ({
        key,
        turn: message.turn,
        event: false,
      })),
      ...[...eventsById()].map(([key, event]) => ({
        key,
        turn: event.turn,
        event: true,
      })),
    ];
    rows.sort((a, b) => a.turn - b.turn || Number(b.event) - Number(a.event));
    return [
      ...rows.map((row) => row.key),
      ...(showsWorking() ? [workingKey()] : []),
    ];
  });
  let positionedTarget: AgentMessageTarget | undefined;
  // Navigation is an external effect. Wait for both log hydration and the
  // virtual list's layout; subsequent live folds must not repeat the jump.
  createEffect(
    on(
      [() => props.searchTarget, messageById, navigation, scrollState],
      ([target, byId, handle, state]) => {
        if (
          !target ||
          target === positionedTarget ||
          !handle ||
          !state?.didInitialScroll
        )
          return;
        const entry = [...byId].find(
          ([, message]) =>
            message.turn === target.messageTurn &&
            message.author.kind === target.author
        );
        if (!entry) return;
        // The ready notification can run within initial layout, before its
        // scroll-to-latest finishes. Navigate after that layout has committed.
        const frame = requestAnimationFrame(() => {
          if (!handle.scrollToMessage(entry[0])) return;
          positionedTarget = target;
          setHighlightedId(entry[0]);
        });
        onCleanup(() => cancelAnimationFrame(frame));
      }
    )
  );
  // Insets belong in virtual measurements, not CSS padding outside the sizer.
  // ThreadList preserves the end pin across keyboard/viewport and inset resizes.
  const insets = () =>
    isTouchDevice()
      ? {
          start: (splitPanel?.contentOffsetTop() ?? 0) + 16,
          end: FloatRegions.hostHeight(),
        }
      : { start: 16, end: 0 };

  return (
    <div class="relative flex-1 min-h-0" ref={setTranscriptEl}>
      <ThreadList
        keys={keys}
        shortListAlign="start"
        initialPosition={
          initialTarget && initialSessionId
            ? {
                type: 'element',
                id: `${initialSessionId}:${initialTarget.messageTurn}:${initialTarget.author}`,
              }
            : { type: 'latest' }
        }
        insets={insets()}
        targetId={highlightedId()}
        onUserNavigation={() => setHighlightedId(undefined)}
        onReady={(handle) => {
          setNavigation(handle);
          return () => {
            setNavigation(undefined);
          };
        }}
        onScroll={(state) => setScrollState(state)}
      >
        {({ id }) => (
          <Show
            when={id !== workingKey()}
            fallback={
              <div class="macro-message-width mx-auto px-4 pb-4 min-w-0">
                <WorkingLine label={workingLabel()} />
              </div>
            }
          >
            <Show when={eventsById().get(id)}>
              {(event) => (
                <div class="macro-message-width mx-auto px-4 pb-2 min-w-0">
                  <SessionEventRow event={event()} viewerId={userId()} />
                </div>
              )}
            </Show>
            <Show when={messageById().get(id)}>
              {(message) => (
                <div
                  class="macro-message-width mx-auto px-4 pb-4 min-w-0 rounded-lg"
                  classList={{ 'bg-accent/10': highlightedId() === id }}
                  data-search-target={
                    highlightedId() === id ? 'true' : undefined
                  }
                >
                  <Message
                    message={message()}
                    inFlight={isLiveTurn(message(), liveTurn())}
                  />
                </div>
              )}
            </Show>
          </Show>
        )}
      </ThreadList>
      <ScrollToBottomOverlay
        scrollState={scrollState}
        onScrollToBottom={(behavior) => {
          setHighlightedId(undefined);
          navigation()?.scrollToLatest(behavior);
        }}
      />
      <ReplyToSelection container={transcriptEl()} onReply={quoteSelection} />
    </div>
  );
}