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.

20.0 KB
Raw Blame
import { t } from '@app/lib/i18n';
import { getAttachmentKindFromFile } from '@channel/Input/utils/file-helpers';
import { toast } from '@core/component/Toast/Toast';
import { createResizeObserver } from '@solid-primitives/resize-observer';
import { IMAGE_INPUT_WARNING } from '../state/image-input';
/**
 * The agent block's composer: the chat input's look and its markdown editing
 * surface (`MarkdownShell` over a lean `EditorConfigBuilder`), including `@`
 * mentions so users can attach Macro items the same way they do in chat, and
 * file attachments - drop, paste, or the paperclip - shown as the channel
 * composer's chips. Uploading itself stays out: the parent owns the
 * attachment list and hands files back through `onAttachFiles`. Model
 * plumbing arrives through the `modelControl` slot. Visual chrome mirrors
 * `@core/component/AI/component/input/ChatInput.tsx`.
 */

import {
  DictationButton,
  DictationFeedback,
  DictationPanel,
} from '@app/features/dictation/components/dictation-controls';
import { createComposerDictation } from '@app/features/dictation/composer-dictation';
import {
  ComposerAddDrawer,
  ComposerAddMenu,
} from '@channel/Input/ComposerAddMenu';
import { InputProvider } from '@channel/Input/context';
import { Input } from '@channel/Input/Input';
import type { InputAttachmentData, InputCommands } from '@channel/Input/types';
import { useMacroMentionLinkResolver } from '@components/app/split-layout/split-router/mention-links';
import { preloadAgentFold } from '@core/agent-fold/client';
import { buildConfig } from '@core/component/LexicalMarkdown/builder/MarkdownConfigBuilder';
import { ComposerEditor } from '@core/component/LexicalMarkdown/component/ComposerEditor';
import type { AgentCommandItem } from '@core/component/LexicalMarkdown/plugins';
import { createComposerLayout } from '@core/component/LexicalMarkdown/utils/create-composer-layout';
import { isMobile } from '@core/mobile/isMobile';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { useTouchOutsideToDismissKeyboard } from '@core/mobile/useTouchOutsideToDismissKeyboard';
import { fireAndForget } from '@core/util/fireAndForget';
import { handleFileFolderDrop } from '@core/util/upload';
import { $insertReferencedPaste } from '@macro-inc/lexical-core';
import { Button, ComposerSurface, SendButton } from '@ui';
import {
  createEffect,
  createSignal,
  type JSX,
  onCleanup,
  onMount,
  Show,
} from 'solid-js';
import { agentAttachmentItems } from './attachment-menu';

/**
 * Id of the agent input's text-area wrapper. Exposed so callers (e.g. the
 * mobile Create menu) can arm focus on the contenteditable before it mounts.
 */
export const AGENT_INPUT_TEXT_AREA_ID = 'agent-input-text-area';

/** Quote text into the composer as a referenced paste chip. */
export type QuoteInsert = (text: string) => void;

export interface AgentInputProps {
  /** Image capability of the selected model; unknown runtimes omit it. */
  supportsImages?: boolean | null;
  modelName?: string;
  placeholder?: string;
  /** Context to seed in the composer without sending it. */
  initialInput?: string;
  /**
   * Controlled composer text. Set with `onDraftChange` when a parent keeps
   * the unsent message (an agent session draft).
   */
  draft?: string;
  onDraftChange?: (draft: string) => void;
  /** The agent is working: the send button becomes a stop square. */
  busy?: boolean;
  disabled?: boolean;
  /** View-only sessions cannot edit drafts; pending sessions still can. */
  readOnly?: boolean;
  autofocus?: boolean;
  /**
   * Slash commands the harness advertises (ACP `available_commands_update`);
   * shown alongside skills and pull requests in the `/` menu.
   */
  commands?: () => AgentCommandItem[];
  /**
   * Receives the composed markdown, including any `<m-document-mention>`
   * tags, and the attachments as they stood when Send was pressed. Either
   * may be empty, never both.
   */
  onSend: (markdown: string, attachments: InputAttachmentData[]) => void;
  onStop?: () => void;
  /**
   * Files attached so far, uploaded or still uploading. Owned by the parent
   * (an `InputAttachmentTracker`), which is what lets the composer clear
   * them after a send.
   */
  attachments?: InputAttachmentData[];
  /** Files the user dropped, pasted, or picked. Absent means no attaching. */
  onAttachFiles?: (files: File[]) => void;
  onRemoveAttachment?: (attachment: InputAttachmentData) => void;
  /** Model control: a pill above the box on desktop, footer-left on touch. */
  modelControl?: JSX.Element;
  /**
   * Ref-style: receives the quote-insert function once the editor mounts
   * (and `undefined` again on unmount), so the transcript's "Reply to this"
   * chip can quote selected text into this composer.
   */
  registerQuoteInsert?: (insert: QuoteInsert | undefined) => void;
  /**
   * Up (or Shift+Tab/Left, the app's focus-leave convention) at the very
   * start of the input: focus moves to whatever sits above — the queued
   * prompt about to dispatch. Ordinary in-text cursor movement never
   * triggers it.
   */
  onNavigateUp?: () => void;
  /** Ref-style: how the queue's Down-past-the-end refocuses this input;
   *  `undefined` again on unmount. */
  registerFocus?: (focus: (() => void) | undefined) => void;
}

export function AgentInput(props: AgentInputProps) {
  const [attachDrawerOpen, setAttachDrawerOpen] = createSignal(false);
  const imageUnsupportedReason = () =>
    props.supportsImages === false
      ? t('common.imagesUnsupported', {
          model: props.modelName ?? t('common.selectedModel'),
        })
      : undefined;
  const [owned, setOwned] = createSignal(props.initialInput ?? '');
  const controlled = () => props.onDraftChange !== undefined;
  const markdown = () => (controlled() ? (props.draft ?? '') : owned());
  const setMarkdown = (value: string) => {
    if (props.onDraftChange) props.onDraftChange(value);
    else setOwned(value);
  };
  const [isDraggedOver, setIsDraggedOver] = createSignal(false);
  let containerRef: HTMLDivElement | undefined;
  const [layout, setLayout] = createSignal<HTMLDivElement>();
  const [content, setContent] = createSignal<HTMLDivElement>();
  const [height, setHeight] = createSignal<number>();
  // The surface is pinned to its content's height, so the measured element has
  // to enclose the attachment chips as well as the editor row — they sit inside
  // the surface, and a row-only measurement clips them.
  createResizeObserver(content, (_, element) => {
    setHeight(element.getBoundingClientRect().height);
  });
  useTouchOutsideToDismissKeyboard(() => containerRef);
  const dictation = createComposerDictation(() => editor.lexical);

  const attachments = () => props.attachments ?? [];
  const canAttach = () =>
    props.onAttachFiles !== undefined && !props.disabled && !props.readOnly;
  const hasPendingAttachments = () =>
    attachments().some((attachment) => attachment.pending);
  const attachFiles = (files: File[]) => {
    if (!canAttach() || files.length === 0) return;
    const accepted =
      props.supportsImages === false
        ? files.filter((file) => getAttachmentKindFromFile(file) !== 'image')
        : files;
    if (accepted.length !== files.length) toast.failure(IMAGE_INPUT_WARNING);
    if (accepted.length) props.onAttachFiles?.(accepted);
  };

  // Sending while busy is allowed — the service queues prompts behind the
  // running turn. A file still uploading holds the send: its URL is not
  // known yet, and the agent gets exactly what the chips show.
  const canSend = () =>
    (markdown().trim().length > 0 || attachments().length > 0) &&
    !hasPendingAttachments() &&
    !(
      props.supportsImages === false &&
      attachments().some((file) => file.kind === 'image')
    ) &&
    !props.disabled &&
    !props.readOnly &&
    !dictation.active();

  // The channel composer's chips, drop zone, and overlay read their state
  // from `Input.Root`'s context; this is that context, over this composer's
  // props. Only attaching and removing do anything - there is no channel
  // send or format ribbon behind these slots.
  const inputCommands: InputCommands = {
    send: async () => false,
    attachFiles: async (files) => attachFiles(files),
    toggleFormatRibbon: () => {},
    close: () => {},
    removeAttachment: (attachment) => {
      if (!props.readOnly) props.onRemoveAttachment?.(attachment);
    },
  };

  const send = () => {
    if (!canSend()) return;
    const content = markdown().trim();
    const attached = attachments();
    setMarkdown('');
    editor.controls.clear();
    props.onSend(content, attached);
  };

  const editor = buildConfig('chat')
    .withAppLinkResolver(useMacroMentionLinkResolver())
    .namespace('agent-input')
    .withMentions({
      showOpenTabs: true,
      block: 'agent',
    })
    .withEmojis()
    .withLinks({ floatingMenu: true, autoLinkMatchMode: 'common-tlds' })
    .withHistory({ timeGap: 400 })
    .withCode()
    .withRestoreFocus()
    .withAgentCommands({ commands: () => props.commands?.() ?? [] })
    // Pasted files (and, on iOS, recovered clipboard images) become
    // attachments through the same door as a drop.
    .withFilePaste({
      onPasteFilesAndDirs: (files, directories) => {
        fireAndForget(
          handleFileFolderDrop(files, directories, (entries) =>
            attachFiles(entries.map((entry) => entry.file))
          ),
          'agent-composer'
        );
      },
    })
    .onEnter(() => {
      // On a virtual keyboard Enter is a newline, as in channels; the send
      // button is the only way to submit.
      if (isTouchDevice()) return false;
      if (canSend()) send();
      return true;
    })
    .onFocusLeave({
      onStart: (event) => {
        if (!props.onNavigateUp) return;
        event.preventDefault();
        props.onNavigateUp();
      },
      // Nothing sits below the input; the key keeps its default behavior.
      onEnd: () => {},
    })
    .onChange(setMarkdown);

  // A parent can restore or clear the draft after the editor has mounted.
  createEffect(() => {
    const next = markdown();
    if (next !== editor.controls.getMarkdown())
      editor.controls.setMarkdown(next);
  });

  const { isCompact, hasMultilineContent } = createComposerLayout(
    editor.buildHandle().lexical,
    {
      container: layout,
      mode: () => (isTouchDevice() ? 'expanded' : 'auto'),
    }
  );

  onMount(() => {
    props.registerFocus?.(() => editor.controls.focus());
    onCleanup(() => props.registerFocus?.(undefined));
    props.registerQuoteInsert?.((text) => {
      if (props.readOnly) return;
      // Discrete so the chip is committed to the DOM before focus moves in.
      editor.lexical.update(() => $insertReferencedPaste(text), {
        discrete: true,
      });
      editor.controls.focus();
    });
    onCleanup(() => props.registerQuoteInsert?.(undefined));
  });

  // MarkdownShell only focuses on click when !isMobile(), so padding taps
  // on a phone miss the empty contenteditable. Focus from this gesture
  // (channel EditorShell / chat surface) so the whole box is tappable,
  // including on touch — pointerdown stays inside the user gesture that
  // iOS needs to raise the keyboard.
  //
  // The tap's own default must not run afterwards: a mousedown on a target
  // with nothing focusable above it blurs the active element — the editor
  // just focused — and the keyboard drops again. Cancel pointerdown, and
  // mousedown too because a real iPhone still synthesises it after a
  // cancelled pointerdown (see `keepEditorFocus` in TouchSelectionToolbar).
  // Taps inside the contenteditable keep their defaults so the caret lands
  // under the finger. So do presses in popups the editor portals out of the
  // composer (the paste viewer): Solid still delegates their events through
  // here, and cancelling them blocks selecting their text.
  const focusEditor = (event: Event) => {
    if (props.readOnly) return;
    const target = event.target as HTMLElement | null;
    if (!target || target.closest('button')) return;
    if (!(event.currentTarget as Node).contains(target)) return;
    if (editor.lexical.getRootElement()?.contains(target)) return;
    event.preventDefault();
    if (event.type === 'pointerdown') editor.controls.focus();
  };

  return (
    <InputProvider
      value={{
        view: () => ({
          mode: 'channel',
          attachments: attachments(),
          isDraggedOver: isDraggedOver(),
          hasPendingAttachments: hasPendingAttachments(),
        }),
        commands: inputCommands,
      }}
    >
      <div
        ref={containerRef}
        data-keep-keyboard
        onFocusIn={() => fireAndForget(preloadAgentFold(), 'agent-composer')}
        class="flex flex-col gap-1.5"
        classList={{ 'opacity-50': props.readOnly }}
      >
        {/* Desktop: the model pill sits above the box, as it always has. */}
        <Show when={!isTouchDevice() && props.modelControl}>
          <div class="flex items-center px-0.5">{props.modelControl}</div>
        </Show>
        {/* h-auto beats Surface's size-full so the in-flow controls are not
            clipped over the editor (that was Auto sitting on the placeholder). */}
        <ComposerSurface
          class="relative h-auto transition-[height] duration-150 ease-out motion-reduce:transition-none"
          style={{
            height: height() === undefined ? undefined : `${height()}px`,
          }}
        >
          <Input.DropZone
            onDragStart={(valid) => canAttach() && setIsDraggedOver(valid)}
            onDragEnd={() => setIsDraggedOver(false)}
          >
            <Show when={canAttach()}>
              {/* Matches ComposerSurface's own radius so the overlay's rim
                  sits on the composer's edge, not inside it. */}
              <Input.DropOverlay
                class="rounded-[26.25px] touch:rounded-3xl"
                hint="Drop files here to send them to the agent"
              />
            </Show>
            <div
              ref={setContent}
              data-composer-content
              inert={dictation.active()}
              classList={{ invisible: dictation.active() }}
            >
              {/* Chips above the text, media and documents in their own rows,
                  exactly as the channel composer lays them out. */}
              <Input.Attachments kind="media" class="pb-0" />
              <Input.Attachments kind="document" class="pb-0" />
              {/* Desktop: one row, send right of the text. Touch: the text gets
                  the whole width and the controls drop to a footer row (model
                  left, send right) — the chat-tall / channel footer shape. */}
              <div
                ref={setLayout}
                data-composer-compact={isCompact()}
                class="group/composer flex items-end data-[composer-compact=false]:flex-col data-[composer-compact=false]:items-stretch gap-[3.75px] p-[7.5px] min-h-[48.75px] touch:min-h-0 touch:flex-col touch:items-stretch touch:gap-0 touch:p-0"
                onPointerDown={focusEditor}
                onMouseDown={focusEditor}
              >
                <div
                  id={AGENT_INPUT_TEXT_AREA_ID}
                  class="min-w-0 flex-1 group-data-[composer-compact=false]/composer:flex-none text-base text-ink not-touch:px-[9.375px] not-touch:py-[4.6875px] not-touch:leading-[24.375px] not-touch:min-h-[24.375px] not-touch:text-composer-ink touch:px-3 touch:py-2"
                  classList={{
                    // While empty only the placeholder renders; keep it to one clipped
                    // line so it doesn't wrap into the single-line height.
                    'overflow-hidden whitespace-nowrap':
                      markdown().trim().length === 0,
                    // Long drafts must not eat the mobile viewport above the dock.
                    'max-h-[calc(32*var(--dvh,1dvh))] overflow-y-auto':
                      hasMultilineContent() && isMobile(),
                  }}
                >
                  <ComposerEditor
                    config={editor}
                    initialValue={markdown()}
                    disabled={props.readOnly}
                    placeholder={
                      props.placeholder ??
                      'Message the agent, @mention anything'
                    }
                    autofocus={
                      !props.readOnly &&
                      !isMobile() &&
                      !isTouchDevice() &&
                      props.autofocus
                    }
                  />
                </div>

                {/* In-flow — never absolute over the text. */}
                <div class="flex shrink-0 items-center gap-[3.75px] touch:h-8 touch:gap-2 touch:p-2 touch:mb-2">
                  <Show when={isTouchDevice() && props.modelControl}>
                    <div class="min-w-0">{props.modelControl}</div>
                  </Show>
                  <Show when={props.onAttachFiles}>
                    {/* The picker accepts whatever the drop zone does: an
                        agent's reason to attach a file is usually a source
                        file, and the static upload stores any type. */}
                    <Input.AttachFilesAction
                      accept={null}
                      disabled={props.disabled || props.readOnly}
                      trigger={(openFiles, openImages) => {
                        const groups = () => [
                          {
                            items: agentAttachmentItems(
                              openFiles,
                              openImages,
                              imageUnsupportedReason()
                            ),
                          },
                        ];
                        return (
                          <>
                            <ComposerAddMenu
                              groups={groups()}
                              disabled={props.disabled || props.readOnly}
                              onOpenDrawer={() => setAttachDrawerOpen(true)}
                            />
                            <ComposerAddDrawer
                              groups={groups()}
                              open={attachDrawerOpen()}
                              onOpenChange={setAttachDrawerOpen}
                              getEditor={() => editor.lexical.getRootElement()}
                            />
                          </>
                        );
                      }}
                    />
                  </Show>
                  <div class="ml-auto flex shrink-0 items-center gap-[3.75px]">
                    <DictationButton
                      dictation={dictation}
                      disabled={props.disabled || props.readOnly}
                    />
                    <Show
                      when={props.busy && props.onStop && !canSend()}
                      fallback={
                        <SendButton
                          appearance="composer"
                          tooltip="Send"
                          disabled={!canSend()}
                          onClick={send}
                        />
                      }
                    >
                      <Button
                        aria-label="Stop"
                        tooltip="Stop"
                        variant={isTouchDevice() ? 'ghost' : 'secondary'}
                        size="icon-md"
                        disabled={props.disabled || props.readOnly}
                        onClick={() => props.onStop?.()}
                        class={
                          isTouchDevice()
                            ? 'size-7.5 text-ink-extra-muted not-disabled:bg-ink/5 not-disabled:hover:bg-ink/10'
                            : undefined
                        }
                      >
                        <div class="size-3.5 not-touch:size-[13.125px] rounded-sm bg-current" />
                      </Button>
                    </Show>
                  </div>
                </div>
              </div>
            </div>
          </Input.DropZone>
          <DictationPanel dictation={dictation} />
        </ComposerSurface>
        <DictationFeedback dictation={dictation} />
      </div>
    </InputProvider>
  );
}