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 insertoptions?(InsertTextOptions): Options for theinsertTextmethodposition?(Range | number): Where to insert. ARange({ from, to }) replaces that range; anumberinserts 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 insertoptions?(InsertHtmlOptions): Options for theinsertHtmlmethodposition?(Range | number): Where to insert. ARange({ from, to }) replaces that range; anumberinserts 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 sliceoptions?(InsertJsonOptions): Options for theinsertJsonmethodposition?(Range | number): Where to insert. ARange({ from, to }) replaces that range; anumberinserts 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 thestreamTextmethodposition?(Range | number): Where to insert. ARange({ from, to }) replaces that range; anumberinserts 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. Returnstrueif the chunk has an error,falseotherwise. Iftrue, the stream will be stopped and aStreamingChunkCheckFailedErrorwill 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 thestreamHtmlmethodposition?(Range | number): Where to insert. ARange({ from, to }) replaces that range; anumberinserts 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. Returnstrueif the chunk has an error,falseotherwise. Iftrue, the stream will be stopped and aStreamingChunkCheckFailedErrorwill 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)