Edit the document

The AI Toolkit provides methods to edit the document in different formats.

insertText

Insert plain text into the editor.

Parameters

  • content (string): Text to insert
  • options? (InsertTextOptions): Options for the insertText method
    • position? (Range | number): Where to insert. A Range ({ from, to }) replaces that range; a number inserts at that position. Default: entire document range.
    • reviewOptions? (ReviewOptions): Control preview/review behavior. See available options.

Returns

void

Example

// Replace the entire document and show review markers
toolkit.insertText('AI content', { reviewOptions: { mode: 'review' } })

insertHtml

Insert HTML into the editor.

Parameters

  • content (string): HTML to insert
  • options? (InsertHtmlOptions): Options for the insertHtml method
    • position? (Range | number): Where to insert. A Range ({ from, to }) replaces that range; a number inserts at that position. Default: entire document range.
    • reviewOptions? (ReviewOptions): Control preview/review behavior. See available options.

Returns

void

Example

// Insert a paragraph at the end of the document
toolkit.insertHtml('<p>AI paragraph</p>', { position: editor.state.doc.content.size })

insertJson

Insert ProseMirror JSON into the editor.

Parameters

  • content (any): ProseMirror JSON node or slice
  • options? (InsertJsonOptions): Options for the insertJson method
    • position? (Range | number): Where to insert. A Range ({ from, to }) replaces that range; a number inserts at that position. Default: entire document range.
    • reviewOptions? (ReviewOptions): Control preview/review behavior. See available options.

Returns

void

Example

// Replace the entire document with a paragraph node
toolkit.insertJson({ type: 'paragraph', content: [{ type: 'text', text: 'AI' }] })

streamText

Stream plain text content into the editor in real-time.

Parameters

  • stream (AsyncIterable<string | Uint8Array>): The stream of text content. It can be any object that implements the Async Iterable protocol, such as a generator.
  • options? (StreamTextOptions): Options for the streamText method
    • position? (Range | number): Where to insert. A Range ({ from, to }) replaces that range; a number inserts at that position. Default: entire document range.
    • reviewOptions? (ReviewOptions): Control preview/review behavior. See available options.
    • checkChunkForError? ((status: { chunk: string; content: string; range: Range }) => boolean): A function to check if the chunk contains an error. Returns true if the chunk has an error, false otherwise. If true, the stream will be stopped and a StreamingChunkCheckFailedError will be thrown.
    • onError? ((event: { error: unknown; chunk: string; content: string; range: Range }) => void): A function to handle errors that occur during streaming.
    • onChunkInserted? ((event: { chunk: string; content: string; range: Range }) => void): A function to handle when a chunk is streamed. Called after the chunk is inserted into the editor.

Returns

Promise<void>: A promise that resolves when the stream is complete.

Example

const response = await fetch('/api/generate-text')
// The API endpoint returns a stream of text content
const stream = response.body

toolkit.streamText(stream)

streamHtml

Stream HTML content into the editor in real-time.

Parameters

  • stream (AsyncIterable<string | Uint8Array> | ReadableStream<string | Uint8Array>): The stream of HTML content. It can be any object that implements the Async Iterable protocol, such as a ReadableStream or a generator.
  • options? (StreamHtmlOptions): Options for the streamHtml method
    • position? (Range | number): Where to insert. A Range ({ from, to }) replaces that range; a number inserts at that position. Default: entire document range.
    • reviewOptions? (ReviewOptions): Control preview/review behavior. See available options.
    • checkChunkForError? ((status: { chunk: string; content: string; range: Range }) => boolean): A function to check if the chunk contains an error. Returns true if the chunk has an error, false otherwise. If true, the stream will be stopped and a StreamingChunkCheckFailedError will be thrown.
    • onError? ((event: { error: unknown; chunk: string; content: string; range: Range }) => void): A function to handle errors that occur during streaming.
    • onChunkInserted? ((event: { chunk: string; content: string; range: Range }) => void): A function to handle when a chunk is streamed. Called after the chunk is inserted into the editor.

Returns

Promise<void>: A promise that resolves when the stream is complete.

Example

const response = await fetch('/api/generate-html')
// The API endpoint returns a stream of HTML content
const stream = response.body
toolkit.streamHtml(stream)