UNPKG

@ai-stack/payloadcms

Version:

<p align="center"> <img alt="Payload AI Plugin" src="assets/payload-ai-intro.gif" width="100%" /> </p>

49 lines (48 loc) 2.69 kB
import type { LexicalEditor } from 'lexical'; type SetSafeLexicalStateOptions = { logErrors?: boolean; /** * Pre-generation snapshot of `root` (as returned by `editorState.toJSON().root`), used to * find and reinsert preserved custom blocks. Passing this explicitly (rather than reading the * live editor state at call time) matters for streaming generations, which call * `setSafeLexicalState` repeatedly as partial content arrives - see `useGenerate.ts` for why * that would otherwise compound block position errors on every call. */ originalRoot?: null | Record<string, unknown>; }; type LexicalNodeJSON = { children?: LexicalNodeJSON[]; type?: string; } & Record<string, unknown>; /** * The generation JSON schema has no entry for Payload's custom `block` node type * (BlocksFeature), and the caller always fully replaces `root.children` with the model's * output - so any custom blocks in the document would otherwise be silently dropped by every * Compose/Translate/Expand/etc. action. Since the model was never given a schema to reproduce * these nodes, the only way to keep them is to reinsert them ourselves before committing the * new state. * * Counterpart of the placeholder substitution in `lexicalToHTML.ts`: each preserved block is * rendered as a `"[[[BLOCK_<n>]]]"` marker paragraph in the model's prompt context (see * `buildBlockPlaceholderInstruction` in `endpoints/index.ts` for the accompanying instruction to * echo it back unchanged). If the model cooperated, that marker is found here in the generated * content and swapped for the real block at that exact position. If the model altered, * translated, or dropped the marker (not guaranteed, since it's only a prompt instruction, not a * schema constraint), position falls back to an approximation proportional to the block's * original relative index, since the regenerated content no longer has a 1:1 correspondence * with the original paragraph/heading count. Only top-level `root.children` are handled - blocks * nested inside other node types are out of scope for this pass. */ export declare const reinsertPreservedBlocks: (currentRoot: null | Record<string, unknown> | undefined, nextChildren: LexicalNodeJSON[]) => LexicalNodeJSON[]; export declare const normalizeLexicalState: (state: unknown) => { root: { type: string; children: any[]; direction: {} | null; format: string; indent: number; version: number; }; } | null; export declare const setSafeLexicalState: (state: unknown, editorInstance?: LexicalEditor | null, options?: SetSafeLexicalStateOptions) => boolean; export {};