Connect an agent to a document

Choose how an agent edits a DOCX and how readers see its updates: browser tools, shared server edits, or complete file refresh.

Your model can run on any backend. Choose where document edits execute and how readers see them.

Edit through a browser tool

The following tool edits the open editor with complete, validated arguments:

import { DocxEditor } from '@docx-editor.dev/editor-api/browser';

const runtime = DocxEditor.createBrowser(editor, { author: 'Assistant' });

async function replaceText(args: { find: string; replace: string }) {
  return runtime.run(async (context) => {
    const matches = context.document.body.search(args.find, { matchCase: true });
    matches.load('items');
    await context.sync();
    if (matches.items.length !== 1) return { applied: false, matches: matches.items.length };
    matches.items[0]!.insertText(args.replace, 'Replace');
    await context.sync(); // The edit is now visible.
    return { applied: true };
  });
}
  • Return plain data to the model.
  • The API does not consume raw token streams or partial tool arguments.
  • Show partial output as progress, then call the tool when the arguments are complete.

For setup, see Drive an open browser editor.

Choose an integration

ApproachUse whenEntry pointGuide
Browser toolsThe agent edits the document a user has openDocxEditor.createBrowser(editor)Runtime and setup
Shared server editsAn agent and readers share a collaboration roomDocxEditor.createCollaborative(room.document, room.session)Let a server agent propose redlines
Complete file refreshThe agent or a processor returns a whole DOCXcreateDocumentRefresh(editor)Document refresh API

For refresh, call capture() before processing, then pass the result to refresh.applyUpdate(). With mode: 'edit', the result arrives as one undoable edit. Refresh does not create tracked changes. See the interactive effects preview.

Publish progress safely

  • A completed context.sync() confirms a local commit, not remote acknowledgment or persistence.
  • Before a server worker disconnects, wait for the provider to acknowledge outgoing updates. Persistence needs its own storage policy.
  • Keep job status, room authorization, and job lifetime in your backend. Never replace an active room with an exported DOCX snapshot.
  • Serialize agent operations. Use tool-call IDs to prevent duplicate execution, including calls still running.

Show review suggestions

Set context.document.changeTrackingMode = 'TrackMineOnly' and use ordinary range edits. Commit each completed suggestion in its own sync so readers can review it while the agent continues.

Tracking has these requirements:

  • Tracked edits require an explicit author.
  • Browser tracking also requires the Pro review module and an editable editor.
  • Tracking records the edits you submit. It does not split a paragraph rewrite into smaller suggestions.

To propose a rewrite word by word, see Propose an AI rewrite as tracked changes. For supported operations, see Supported tracked edits.

Limits

  • If the document changes after the agent reads it, handle StaleDocument by reading again and reconsidering. Never replay the old edit automatically.
  • A reloading refresh resets selection and undo history and refuses collaborative sessions. To keep history, apply the result as one undoable edit with mode: 'edit'.
  • Refresh refuses results captured before a later edit.
  • The editor does not watch a file URL. Your application receives or fetches each result.
  • The editing API and Pro review module require the EigenPal Pro License.

See also