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.

14.7 KB
Raw Blame
/**
 * Renders one folded agent-session message. Pure composition: each part kind
 * has its own component under `parts/` (the chat block's handler-per-tool
 * split), user prompts get the chat block's bubble treatment, and the tail
 * thought shimmers while the turn is in flight.
 *
 * Whether the turn is in flight is the caller's to say (`state/live-turn`):
 * a message's own `stop` reads several settled turns as live, and a
 * transcript that let each message decide showed every one of them working.
 */

import { useUserId } from '@core/context/user';
import { idToDisplayName } from '@core/user/util';
import { messageSendMotion } from '@core/util/message-send-motion';
import { openExternalUrl } from '@core/util/url';
import { stripAgentContext } from '@macro-inc/lexical-core/utils/parsers';
import type {
  FoldedMessage,
  MessagePart,
} from '@service-agent-fold/generated/types';
import { UserMessageBubble } from '@ui';
import {
  createMemo,
  For,
  Index,
  type JSX,
  Match,
  Show,
  Switch,
} from 'solid-js';
import { match } from 'ts-pattern';
import { useOptionalAgentSession } from '../context/AgentSessionContext';
import { useOpenPromptOrigin } from '../open-prompt-origin';
import { isControlMessage } from '../state/control-message';
import { isNotificationMessage } from '../state/notification-message';
import { agentContextText, promptOrigin } from '../state/prompt-origin';
import { thoughtIsStreaming } from '../state/thought-streaming';
import { segmentParts } from '../state/tool-groups';
import {
  ActionLine,
  FailureNoticeCard,
  isToolActive,
  Thought,
  WorkingLine,
} from '../ui';
import { LiveToolGroup } from '../views/LiveToolGroup';
import { PromptFooter } from './PromptFooter';
import { AttachmentPart } from './parts/AttachmentPart';
import { ControlPart } from './parts/ControlPart';
import { ElicitationPart } from './parts/ElicitationPart';
import { PermissionPart } from './parts/PermissionPart';
import { PlanPart } from './parts/PlanPart';
import { type ToolUsePart, toolUsedAfter } from './parts/shared';
import { TaskNotificationPart } from './parts/TaskNotificationPart';
import { TextPart } from './parts/TextPart';
import { ToolApprovalPart } from './parts/ToolApprovalPart';
import { ToolCallPart } from './parts/ToolCallPart';

/**
 * What a turn the runtime errored asks of the reader. Every such failure
 * leaves the session usable - the next message starts a fresh turn - so the
 * instruction is the same whatever the runtime said went wrong.
 */
const TURN_FAILED_LABEL =
  'An error was encountered with your session. Send another message to continue';

function AgentMessagePart(props: {
  part: MessagePart;
  message: FoldedMessage;
  /** The part's index within its message, for the tool render context. */
  index: number;
  /** The turn is still in flight — the tail thought reads "Thinking". */
  inFlight: boolean;
}): JSX.Element {
  const session = useOptionalAgentSession();
  // Match accessors keep a part's renderer mounted when a streamed snapshot
  // replaces the object, preserving disclosures while updating their contents.
  return (
    <Switch>
      <Match when={props.part.kind === 'text' && props.part}>
        {(part) => (
          <TextPart
            text={
              props.message.author.kind === 'user'
                ? stripAgentContext(part().text)
                : part().text
            }
            inFlight={props.inFlight}
            hideIncompleteXml={props.message.author.kind === 'agent'}
            accentMentions={props.message.author.kind === 'agent'}
            observeRender={
              props.message.author.kind === 'agent'
                ? (element) =>
                    session?.observeRenderedText?.(
                      props.message.agentSessionId,
                      props.message.turn,
                      element
                    )
                : undefined
            }
          />
        )}
      </Match>
      <Match when={props.part.kind === 'attachment' && props.part}>
        {(part) => <AttachmentPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'thought' && props.part}>
        {(part) => (
          <Thought
            text={part().text}
            active={thoughtIsStreaming(
              props.inFlight,
              props.index,
              props.message.parts.length
            )}
          />
        )}
      </Match>
      <Match when={props.part.kind === 'tool_use' && props.part}>
        {(part) => (
          <ToolCallPart
            part={part()}
            context={{
              sessionId: props.message.agentSessionId,
              messageId: `${props.message.agentSessionId}:${props.message.key}`,
              partIndex: props.index,
              inFlight: props.inFlight,
              followedBy: toolUsedAfter(props.message.parts, props.index),
            }}
          />
        )}
      </Match>
      <Match when={props.part.kind === 'permission' && props.part}>
        {(part) => <PermissionPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'tool_approval' && props.part}>
        {(part) => <ToolApprovalPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'plan' && props.part}>
        {(part) => <PlanPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'control' && props.part}>
        {(part) => <ControlPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'task_notification' && props.part}>
        {(part) => <TaskNotificationPart part={part()} />}
      </Match>
      <Match when={props.part.kind === 'elicitation' && props.part}>
        {(part) => <ElicitationPart part={part()} turn={props.message.turn} />}
      </Match>
    </Switch>
  );
}

/**
 * A run of consecutive tool calls and their accompanying thoughts (see
 * `segmentParts`), folded to one row that opens to those parts, each at
 * its original index.
 */
function ToolGroupPart(props: {
  message: FoldedMessage;
  start: number;
  /** Exclusive. */
  end: number;
  inFlight: boolean;
}): JSX.Element {
  const parts = () => props.message.parts.slice(props.start, props.end);
  const calls = () =>
    parts().filter((part): part is ToolUsePart => part.kind === 'tool_use');
  // A call the log left running in a finished turn is over (see
  // `settledToolStatus`), so a settled turn's run is never "Calling".
  const activeIndex = () =>
    props.inFlight
      ? parts().findIndex(
          (part) => part.kind === 'tool_use' && isToolActive(part.status)
        )
      : -1;
  const active = () => activeIndex() !== -1;
  const renderParts = () => (
    <Index each={parts()}>
      {(part, offset) => (
        <div class="min-h-8 shrink-0">
          <AgentMessagePart
            part={part()}
            message={props.message}
            index={props.start + offset}
            inFlight={props.inFlight}
          />
        </div>
      )}
    </Index>
  );

  return (
    <Show when={calls().length > 0} fallback={renderParts()}>
      <LiveToolGroup
        count={calls().length}
        active={active()}
        activeIndex={active() ? activeIndex() : undefined}
      >
        {renderParts()}
      </LiveToolGroup>
    </Show>
  );
}

/**
 * Whether an open turn should show the working row at its tail.
 *
 * Skipped wherever the transcript already shows the turn is alive — prose
 * streaming in, a thought shimmering — and wherever it is not: a permission
 * or elicitation prompt is waiting on the reader, not working.
 */
function showsWorkingLine(message: FoldedMessage): boolean {
  if (
    message.parts.some(
      (part) => part.kind === 'tool_use' && isToolActive(part.status)
    )
  )
    return false;
  const last = message.parts[message.parts.length - 1];
  if (last === undefined) return true;
  return match(last)
    .with(
      { kind: 'text' },
      { kind: 'thought' },
      { kind: 'permission' },
      { kind: 'tool_approval' },
      { kind: 'elicitation' },
      () => false
    )
    .otherwise(() => true);
}

/**
 * What the working row says: the last part names the work — a tool call or
 * a plan — and a bare turn is just working.
 */
function workingLabel(message: FoldedMessage): string {
  const last = message.parts.at(-1);
  if (last === undefined) return 'Working';
  return match(last)
    .with({ kind: 'tool_use' }, () => 'Running tools')
    .with({ kind: 'plan' }, () => 'Planning')
    .otherwise(() => 'Working');
}

/**
 * The display name of whoever sent a prompt, when that is somebody other
 * than the viewer. A session is shared, so a prompt may be another
 * participant's; one's own prompts (and unattributed ones) need no byline,
 * matching the queued-prompt list in `AgentComposer`. Attribution waits
 * until the viewer id is known — `useUserId` is undefined while user-info
 * is still loading, and a missing viewer must not look like another person.
 */
function promptAuthorName(
  author: FoldedMessage['author'],
  viewerId: string | undefined
): string | undefined {
  if (
    author.kind !== 'user' ||
    author.userId === null ||
    viewerId === undefined
  )
    return undefined;
  return author.userId === viewerId
    ? undefined
    : idToDisplayName(author.userId);
}

/**
 * A prompt, in the chat block's user-bubble treatment
 * (`@core/component/AI/component/message/UserMessage.tsx`): right-aligned,
 * rounded, filled surface shared with production chat. A prompt another
 * participant sent carries their name above the bubble; under it, when it was
 * posted and, when the harness delivered it from elsewhere, where from.
 */
function UserMessage(props: { message: FoldedMessage }) {
  const userId = useUserId();
  const authorName = () => promptAuthorName(props.message.author, userId());
  const openOrigin = useOpenPromptOrigin();
  // Memoized: reading the origin parses the context's XML.
  const origin = createMemo(() => {
    const first = props.message.parts.find((part) => part.kind === 'text');
    const context =
      first?.kind === 'text' ? agentContextText(first.text) : undefined;
    return context === undefined ? undefined : promptOrigin(context);
  });

  return (
    <div
      class="flex w-full flex-col items-end gap-0.5"
      // Still on the wire: the fold shows the prompt before the log confirms
      // it. It reads as sent at once - dimming it made the wait feel longer.
      aria-busy={props.message.pending || undefined}
      ref={(el) =>
        messageSendMotion(el, () =>
          props.message.requestId
            ? `agent:${props.message.agentSessionId}:${props.message.requestId}`
            : undefined
        )
      }
    >
      <Show when={authorName()}>
        {(name) => (
          <div class="text-xs text-ink-extra-muted" data-testid="prompt-author">
            {name()}
          </div>
        )}
      </Show>
      <UserMessageBubble>
        <For each={props.message.parts}>
          {(part, index) => (
            <AgentMessagePart
              part={part}
              message={props.message}
              index={index()}
              inFlight={false}
            />
          )}
        </For>
      </UserMessageBubble>
      <Show when={origin() || props.message.postedAt}>
        <PromptFooter
          origin={origin()}
          postedAt={props.message.postedAt ?? undefined}
          onOpen={
            origin()?.destination
              ? (options) => {
                  const destination = origin()?.destination;
                  if (destination) openOrigin(destination, options);
                }
              : undefined
          }
        />
      </Show>
    </div>
  );
}

export function Message(props: {
  message: FoldedMessage;
  /** This is the running turn's reply, by the session's one `working` truth. */
  inFlight: boolean;
}) {
  const inFlight = () => props.inFlight;
  const failure = () =>
    props.message.stop?.kind === 'failed' ? props.message.stop : undefined;

  return (
    // A control and an event notification are both user-authored by the
    // fold's account and neither is a prompt: they take the full-width
    // treatment, where the notification renders as its own card.
    <Show
      when={
        props.message.author.kind === 'user' &&
        !isControlMessage(props.message) &&
        !isNotificationMessage(props.message)
      }
      fallback={
        <div class="flex flex-col gap-1 min-w-0">
          {/* Segments are positional, and a run at the tail grows as calls
              stream in — so rows are keyed by index, not by segment value,
              and a group keeps its open state while it fills. */}
          <Index each={segmentParts(props.message.parts)}>
            {(segment) => (
              <Show
                when={segment().kind === 'tools'}
                fallback={
                  <Show when={props.message.parts[segment().start]}>
                    {(part) => (
                      <AgentMessagePart
                        part={part()}
                        message={props.message}
                        index={segment().start}
                        inFlight={inFlight()}
                      />
                    )}
                  </Show>
                }
              >
                <ToolGroupPart
                  message={props.message}
                  start={segment().start}
                  end={segment().end}
                  inFlight={inFlight()}
                />
              </Show>
            )}
          </Index>
          {/* The turn is open with nothing to read yet — a ripple and a label
              naming the work, so the wait reads as work rather than a stall. */}
          <Show when={inFlight() && showsWorkingLine(props.message)}>
            <WorkingLine label={workingLabel(props.message)} />
          </Show>
          {/* A turn the runtime errored is something that happened to the
              session, like a model change or a stop — so it reads as one,
              at the foot of whatever the agent managed to say first. The
              line says what to do about it; the runtime's own account of
              what happened is the detail. A failure the runtime wrote in the
              person's terms is theirs to act on, so it gets a card instead. */}
          <Show when={failure()}>
            {(failed) => (
              <Show
                when={failed().notice}
                fallback={
                  <ActionLine
                    label={TURN_FAILED_LABEL}
                    detail={failed().message}
                    failed
                  />
                }
              >
                {(notice) => (
                  <FailureNoticeCard
                    notice={notice()}
                    onOpenLink={openExternalUrl}
                  />
                )}
              </Show>
            )}
          </Show>
        </div>
      }
    >
      <UserMessage message={props.message} />
    </Show>
  );
}