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.

1.9 KB
Raw Blame
import { createHeadlessEditor } from '@lexical/headless';
import { $convertToMarkdownString } from '@lexical/markdown';
import { $getRoot } from 'lexical';
import { NodeReplacements, SupportedNodeTypes } from '../node-list';
import {
  $createMagicChipNode,
  type MagicChipData,
} from '../nodes/MagicChipNode';
import {
  $createReplyTargetNode,
  type ReplyTargetData,
  stripLeadingReplyTargetMarkdown,
} from '../nodes/ReplyTargetNode';
import { ALL_TRANSFORMERS } from '../transformers';

/** Everything the announcement for one agent-session prompt is built from. */
export type AgentSessionAnnouncement = {
  /**
   * Message the agent announcement explicitly replies to. Absent when the
   * conversation cannot be referenced by a reply target yet, in which case
   * the announcement is the chip alone.
   */
  replyTarget?: ReplyTargetData;
  /** The Magic Chip anchoring the session's live response. */
  chip: MagicChipData;
};

/**
 * Compose the message that announces an agent session: a structured reply
 * target followed by the session's Magic Chip. Built headlessly from real
 * Lexical nodes so the serialized markdown always matches what the editor
 * itself produces.
 */
export function composeAgentSessionAnnouncement(
  announcement: AgentSessionAnnouncement
): string {
  const replyTarget = announcement.replyTarget && {
    ...announcement.replyTarget,
    displayText: stripLeadingReplyTargetMarkdown(
      announcement.replyTarget.displayText
    )
      .trim()
      .replace(/\s+/g, ' '),
  };
  const editor = createHeadlessEditor({
    nodes: [...SupportedNodeTypes, ...NodeReplacements],
  });

  editor.update(
    () => {
      if (replyTarget?.displayText) {
        $getRoot().append($createReplyTargetNode(replyTarget));
      }
      $getRoot().append($createMagicChipNode(announcement.chip));
    },
    { discrete: true }
  );

  return editor
    .getEditorState()
    .read(() => $convertToMarkdownString(ALL_TRANSFORMERS));
}