Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

12.0 KB
Raw Blame
import { AsyncLocalStorage } from 'node:async_hooks';

import {
  canApplyAsReviewDiff,
  describeLiteXMLEditStep,
  findLiteXMLEditStepProblem,
  findMalformedLiteXML,
  indexLiteXMLDocument,
  normalizeLiteXMLFragment,
  planLiteXMLEditSteps,
  RichListDocumentPlugin,
} from '@lobechat/editor-runtime';
import type { HeadlessEditorOptions, HeadlessLiteXMLOperation } from '@lobehub/editor/headless';
import { createHeadlessEditor, DEFAULT_HEADLESS_EDITOR_PLUGINS } from '@lobehub/editor/headless';
import type { SerializedEditorState, SerializedLexicalNode } from 'lexical';

import { EMPTY_EDITOR_STATE } from '@/libs/editor/constants';
import { isValidEditorData } from '@/libs/editor/isValidEditorData';

import { AgentDocumentEditRejectedError } from './editRejectedError';

export type AgentDocumentEditorData = Record<string, any>;

export type AgentDocumentLiteXMLOperation =
  | {
      action: 'insert';
      afterId: string;
      litexml: string;
    }
  | {
      action: 'insert';
      beforeId: string;
      litexml: string;
    }
  | {
      action: 'modify';
      litexml: string | string[];
    }
  | {
      action: 'remove';
      id: string;
    };

const toHeadlessLiteXMLOperation = (
  operation: AgentDocumentLiteXMLOperation,
  delay: boolean,
): HeadlessLiteXMLOperation => {
  switch (operation.action) {
    case 'insert': {
      return 'beforeId' in operation
        ? {
            action: 'insert',
            beforeId: operation.beforeId,
            delay,
            litexml: normalizeLiteXMLFragment(operation.litexml),
          }
        : {
            action: 'insert',
            afterId: operation.afterId,
            delay,
            litexml: normalizeLiteXMLFragment(operation.litexml),
          };
    }

    case 'modify': {
      return {
        action: 'replace',
        delay,
        litexml: operation.litexml,
      };
    }

    case 'remove': {
      return {
        action: 'remove',
        delay,
        id: operation.id,
      };
    }
  }
};

const NOTHING_SAVED_HINT =
  'No operations were saved. Call readDocument to get the current node ids, then retry the whole batch.';

export interface AgentDocumentEditorSnapshot {
  content: string;
  editorData: AgentDocumentEditorData;
  litexml?: string;
  recoveredFromMarkdown?: true;
}

export interface AgentDocumentEditSnapshot extends AgentDocumentEditorSnapshot {
  previousEditorData: AgentDocumentEditorData;
}

interface LoadEditorStateParams {
  editorData?: AgentDocumentEditorData | null;
  fallbackContent?: string;
}

// @lobehub/editor's headless Lexical runtime keeps process-global node state while
// hydrating snapshots with stable ids. Concurrent document reads can therefore
// corrupt one another (or observe a partially initialized HeadlessEditor). Keep
// the complete create/hydrate/export/destroy lifecycle serialized.
// Documents render in PageEditor, which keeps `$...$` as plain text because
// business writing uses dollar amounts far more than inline formulas. Parse
// Markdown the same way here so CLI and agent writes don't mint inline math
// nodes the editor itself would never create. Block `$$` math is unaffected.
type HeadlessEditorPlugin = NonNullable<HeadlessEditorOptions['plugins']>[number];

const DOCUMENT_HEADLESS_PLUGINS = DEFAULT_HEADLESS_EDITOR_PLUGINS.map(
  (plugin): HeadlessEditorPlugin =>
    !Array.isArray(plugin) && plugin.pluginName === 'MathPlugin'
      ? [plugin, { enableInlineMath: false }]
      : plugin,
);

// Pages hold what PageEditor's own plugins put in them. A headless editor that
// lacks one of those exports Markdown that disagrees with the page, and drops
// the plugin's node state when it seeds a collaboration room.
const PAGE_DOCUMENT_PLUGINS = [RichListDocumentPlugin as unknown as HeadlessEditorPlugin];

export const createDocumentHeadlessEditor = (
  options?: Pick<HeadlessEditorOptions, 'additionalPlugins'>,
) =>
  createHeadlessEditor({
    additionalPlugins: [...PAGE_DOCUMENT_PLUGINS, ...(options?.additionalPlugins ?? [])],
    plugins: DOCUMENT_HEADLESS_PLUGINS,
  });

let headlessEditorTail: Promise<void> = Promise.resolve();
const insideHeadlessEditorLock = new AsyncLocalStorage<true>();

/**
 * Runs `run` while no other headless editor exists in this process (see above).
 * Re-entrant: a caller already inside the lock runs straight through, so a step
 * that takes it can be composed into a larger one that does too.
 */
export const withHeadlessEditorLock = async <T>(run: () => Promise<T> | T): Promise<T> => {
  if (insideHeadlessEditorLock.getStore()) return run();

  const previous = headlessEditorTail;
  let release: () => void = () => {};
  headlessEditorTail = new Promise<void>((resolve) => {
    release = resolve;
  });

  await previous;
  try {
    return await insideHeadlessEditorLock.run(true, run);
  } finally {
    release();
  }
};

const exportSnapshot = (
  editor: ReturnType<typeof createHeadlessEditor>,
  litexml = false,
): AgentDocumentEditorSnapshot => {
  const snapshot = editor.export({ litexml });

  return {
    content: snapshot.markdown,
    editorData: snapshot.editorData as SerializedEditorState<SerializedLexicalNode>,
    litexml: snapshot.litexml,
  };
};

const hydrateMarkdownOrEmptyState = (
  editor: ReturnType<typeof createHeadlessEditor>,
  content: string,
  options?: { keepId?: boolean },
) => {
  if (content.trim().length === 0) {
    editor.hydrateEditorData(
      EMPTY_EDITOR_STATE as unknown as SerializedEditorState<SerializedLexicalNode>,
      options,
    );
    return;
  }

  editor.hydrateMarkdown(content, options);
};

const createEditorWithState = (
  createEditor: typeof createHeadlessEditor,
  { editorData, fallbackContent = '' }: LoadEditorStateParams,
) => {
  let editor = createEditor();

  if (isValidEditorData(editorData)) {
    try {
      editor.hydrateEditorData(
        editorData as unknown as SerializedEditorState<SerializedLexicalNode>,
        {
          keepId: true,
        },
      );

      const hydratedContent = editor.export().markdown;
      if (fallbackContent.trim().length === 0 || hydratedContent.trim().length > 0) {
        return { editor, recoveredFromMarkdown: false };
      }
    } catch (error) {
      console.error('[AgentDocumentsService] Failed to hydrate editorData:', error);
    }

    // Some editor schema/version mismatches fail without throwing and leave the
    // editor at an empty root. Recreate the editor before hydrating Markdown so
    // no partially parsed Lexical state can leak into the fallback snapshot.
    editor.destroy();
    editor = createEditor();
  }

  hydrateMarkdownOrEmptyState(editor, fallbackContent, { keepId: true });
  // Node ids minted from Markdown differ on every parse, so any snapshot that
  // exposes them must be persisted — including editorData in a non-Lexical shape
  // (older `lh doc` builds wrote `{ type: 'doc', content }`) or none at all.
  return {
    editor,
    recoveredFromMarkdown: isValidEditorData(editorData) || fallbackContent.trim().length > 0,
  };
};

export const createMarkdownEditorSnapshot = async (
  content: string,
): Promise<AgentDocumentEditorSnapshot> =>
  withHeadlessEditorLock(() => {
    const editor = createDocumentHeadlessEditor();

    try {
      hydrateMarkdownOrEmptyState(editor, content);
      return exportSnapshot(editor);
    } finally {
      editor.destroy();
    }
  });

const LITEXML_DOCUMENT_PATTERN = /^\s*(?:<\?xml[\s?]|<root[\s>])/;

const isJsonValueDocument = (content: string): boolean => {
  const trimmed = content.trim();
  if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) return false;

  try {
    JSON.parse(trimmed);
    return true;
  } catch {
    return false;
  }
};

/**
 * Markdown snapshot for content written by an agent. Rejects input that would
 * silently become an empty document — most often LiteXML sent to a Markdown
 * write API — instead of saving the empty result and reporting success.
 */
export const createAgentMarkdownSnapshot = async (
  content: string,
): Promise<AgentDocumentEditorSnapshot> => {
  if (LITEXML_DOCUMENT_PATTERN.test(content)) {
    throw new Error(
      'Document content looks like LiteXML, but this API expects Markdown. Send Markdown, or use modifyNodes to edit nodes by id.',
    );
  }

  // Markdown consumes backslash escapes (`\"` → `"`) and reads `__…__` / `*…*`
  // as formatting, so a raw JSON body is silently rewritten into text that no
  // longer parses. Fenced code is stored verbatim.
  if (isJsonValueDocument(content)) {
    throw new Error(
      'Document content is raw JSON, but this API stores Markdown: Markdown parsing would drop backslash escapes and turn `__` or `*` into formatting, so the stored JSON would no longer parse. Nothing was saved. Wrap the JSON in a fenced code block (```json … ```); code blocks are stored exactly as written.',
    );
  }

  const snapshot = await createMarkdownEditorSnapshot(content);

  if (content.trim().length > 0 && snapshot.content.trim().length === 0) {
    throw new Error(
      'Document content produced an empty document after Markdown parsing; nothing was saved. Send the document body as Markdown.',
    );
  }

  return snapshot;
};

export const exportEditorDataSnapshot = async (
  params: LoadEditorStateParams & { litexml?: boolean },
): Promise<AgentDocumentEditorSnapshot> =>
  withHeadlessEditorLock(() => {
    const { editor, recoveredFromMarkdown } = createEditorWithState(
      createDocumentHeadlessEditor,
      params,
    );

    try {
      const snapshot = exportSnapshot(editor, params.litexml);

      return recoveredFromMarkdown ? { ...snapshot, recoveredFromMarkdown: true } : snapshot;
    } finally {
      editor.destroy();
    }
  });

export const applyLiteXMLOperations = async ({
  editorData,
  fallbackContent,
  operations,
}: LoadEditorStateParams & {
  operations: AgentDocumentLiteXMLOperation[];
}): Promise<AgentDocumentEditSnapshot> =>
  withHeadlessEditorLock(async () => {
    const { editor } = createEditorWithState(createDocumentHeadlessEditor, {
      editorData,
      fallbackContent,
    });

    try {
      const beforeSnapshot = exportSnapshot(editor, true);
      let current = beforeSnapshot;

      // Apply in array order, one operation at a time, so every operation is
      // checked on its own: an unknown id or an operation the editor silently
      // drops fails the whole batch instead of being counted as applied.
      for (const step of planLiteXMLEditSteps(
        operations,
        indexLiteXMLDocument(beforeSnapshot.litexml ?? ''),
      )) {
        const { operation } = step;
        const label = describeLiteXMLEditStep(step, operations.length);
        const document = indexLiteXMLDocument(current.litexml ?? '');

        const malformed = 'litexml' in operation && findMalformedLiteXML(operation.litexml);
        if (malformed) {
          throw new AgentDocumentEditRejectedError(
            `${label} failed: ${malformed}. No operations were saved; fix the litexml and retry the whole batch.`,
          );
        }

        const problem = findLiteXMLEditStepProblem(operation, document);
        if (problem) {
          throw new AgentDocumentEditRejectedError(
            `${label} failed: ${problem}. ${NOTHING_SAVED_HINT}`,
          );
        }

        await editor.applyLiteXML(
          toHeadlessLiteXMLOperation(operation, canApplyAsReviewDiff(operation, document)),
        );
        const next = exportSnapshot(editor, true);

        if (
          next.litexml === current.litexml &&
          JSON.stringify(next.editorData) === JSON.stringify(current.editorData)
        ) {
          throw new AgentDocumentEditRejectedError(
            `${label} did not change the document; the editor rejected it. ${NOTHING_SAVED_HINT}`,
          );
        }

        current = next;
      }

      const snapshot = current;

      if (fallbackContent?.trim().length && snapshot.content.trim().length === 0) {
        throw new Error('Agent document node edit unexpectedly produced empty content');
      }

      return { ...snapshot, previousEditorData: beforeSnapshot.editorData };
    } finally {
      editor.destroy();
    }
  });