@eigenpal/docx-editor-agents/server
Server entry for API routes, Node.js, serverless functions, and Workers.
Import the toolkit here without pulling in React peer deps. Use this from Next.js routes, a FastAPI bridge, a Cloudflare Worker, or any other backend that streams an LLM call with tool definitions.
Functions (4)
createReviewerBridgefunctionSource ↗
Create an EditorBridge backed by a DocxReviewer. The agent (or MCP client) can read, comment, propose changes, etc., against a parsed DOCX file on disk. Call reviewer.toBuffer() afterwards to get the modified DOCX.
declare function createReviewerBridge(reviewer: DocxReviewer): EditorBridge;executeToolCallfunctionSource ↗
Execute a tool call against an EditorBridge. Returns the result (never throws).
declare function executeToolCall(toolName: string, input: Record<string, unknown>, bridge: EditorBridge): AgentToolResult;getToolDisplayNamefunctionSource ↗
Friendly UI label for a tool — sourced from the registry's displayName, falling back to a sentence-case version of the snake_case name. Used by <AgentTimeline> and any other UI that lists running / completed tools.
declare function getToolDisplayName(name: string): string;getToolDisplayName('add_comment') // → 'Adding comment'getToolDisplayName('fetch_clause_template') // → 'Fetch clause template'getToolSchemasfunctionSource ↗
Get tool schemas in OpenAI function-calling format. Works directly with the OpenAI SDK and Anthropic's tools API. For Vercel AI SDK, LangChain, or other agent runtimes, transform this output to that runtime's required shape — see examples/agent-chat-demo/ for a Vercel AI SDK example. The package stays runtime-agnostic.
declare function getToolSchemas(): {
type: "function";
function: {
name: string;
description: string;
parameters: Record<string, unknown>;
};
}[];Classes (1)
DocxReviewerclassSource ↗
Headless DOCX reviewer — parse a file, read/comment/track changes against the document model, write the modified DOCX back out. No DOM, no editor instance. Pair with createReviewerBridge() to drive the built-in agent tools against a file on disk.
declare class DocxReviewer```ts
const reviewer = await DocxReviewer.fromBuffer(buffer, 'AI Reviewer');
reviewer.addComment(5, 'Fix this paragraph.');
reviewer.replace(5, '$50k', '$500k');
const output = await reviewer.toBuffer();
```| Member | Type | Summary |
|---|---|---|
| (constructor) | | Create a reviewer from a parsed Document. |
| acceptAll | | Accept all tracked changes in the body. Pass `{ includeFootnotes, includeEndnotes }` to also accept changes inside note bodies. Returns count. |
| acceptChange | | Accept a tracked change. Pass a revision id to accept a change in the document body, or pass a [ReviewChange](ReviewChange) from [getChanges](getChanges) to accept it wherever it lives — a change carrying `noteId`/`noteType` is resolved inside that footnote/endnote and persists on [toBuffer](toBuffer). |
| addComment | | Add a comment on a paragraph. |
| addComment | | Add a comment with full options (custom author, anchored to specific text). |
| applyReview | | Apply multiple review operations in one call. Uses the reviewer's default author. Individual failures are collected, not thrown. |
| author | string | Default author for comments and tracked changes. Set once, used everywhere. |
| fromBuffer | | Create a reviewer from a DOCX file buffer. |
| getChanges | | Get all tracked changes in the document. Pass `includeFootnotes` / `includeEndnotes` in the filter to also report changes inside note bodies (each such change carries `noteId` / `noteType`). |
| getComments | | Get all comments with their replies. |
| getContent | | Get document content as structured blocks (headings, paragraphs, tables, lists). |
| getContentAsText | | Get document content as plain text for LLM prompts. Each paragraph is prefixed with its index: `[0] text`, `[1] text`, etc. Table cells include position: `[5] (table, row 1, col 2) cell text`. Avoids JSON quote-escaping issues — LLMs can copy text verbatim. |
| proposeDeletion | | Delete text as a tracked change. |
| proposeInsertion | | Insert text as a tracked change. |
| proposeReplacement | | |
| rejectAll | | Reject all tracked changes. See [acceptAll](acceptAll) for the note opt-in. |
| rejectChange | | Reject a tracked change. See [acceptChange](acceptChange) for body-vs-note targeting. |
| removeComment | | Remove a comment by ID. Removing a top-level comment also removes its replies and the anchored range markers. Removing a reply only removes that reply. |
| replace | | Replace text in a paragraph. Creates a tracked change (deletion + insertion). |
| replace | | Replace text with full options. |
| replyTo | | Reply to an existing comment. |
| replyTo | | Reply to an existing comment with full options. |
| toBuffer | | Serialize back to a DOCX buffer. Requires the original buffer. |
| toDocument | | Get the modified Document model. |
Interfaces (5)
AgentContextSnapshotinterfaceSource ↗
Snapshot of what the user is looking at — pass this to your agent's system prompt so it knows the current selection / page without an extra read_selection round-trip.
interface AgentContextSnapshot| Member | Type | Summary |
|---|---|---|
| currentPage | number | 1-indexed page the cursor / selection is on. 0 if unknown. |
| selection | SelectionInfo | null | User's current selection or cursor (null if editor isn't focused). |
| totalPages | number | Total number of pages currently rendered. |
AgentToolDefinitioninterfaceSource ↗
Definition of an agent tool — name, JSON-schema input, handler. Use this to build custom tools alongside the built-in agentTools.
interface AgentToolDefinition<TInput = Record<string, unknown>>| Member | Type | Summary |
|---|---|---|
| description | string | Human-readable description for the LLM |
| displayName? | string | Friendly UI label for the tool. Shown in the agent panel's timeline (e.g. "Reading document"). Falls back to a sentence-case version of `name` if omitted, so consumer-defined tools render readably without specifying this. |
| handler | (input: TInput, bridge: EditorBridge) => AgentToolResult | Handler — receives parsed input + bridge, returns result |
| inputSchema | Record<string, unknown> | JSON Schema for the input parameters |
| name | string | Tool name (used in tool_use blocks) |
AgentToolResultinterfaceSource ↗
Result returned by a tool handler. success: false carries an error message; success: true may carry tool-specific data.
interface AgentToolResult| Member | Type | Summary |
|---|---|---|
| data? | unknown | |
| error? | string | |
| success | boolean |
EditorBridgeinterfaceSource ↗
High-level agent surface over a live editor (or a headless reviewer). Every built-in tool calls into this contract — implement it once and the agent toolkit works against your editor.
interface EditorBridge| Member | Type | Summary |
|---|---|---|
| addComment | | Add a comment, anchored to a paragraph by paraId. |
| applyFormatting | | Apply character formatting (bold / italic / color / size / font / etc.) to a paragraph, or to a unique phrase within it. This is a direct edit — not a tracked change. |
| findText | | Locate text in the document. Returns one handle per matching paragraph. |
| getChanges | | Get all tracked changes in the document. |
| getComments | | Get all comments in the document. |
| getContent | | Get document content as structured blocks (each paragraph carries its `paraId`). |
| getContentAsText | | Get document content as paraId-tagged text lines for LLM prompts. |
| getCurrentPage | | 1-indexed page the user's cursor / selection is on. 0 if unknown. |
| getPage | | Read a single page (1-indexed). Returns null if the page does not exist. |
| getPages | | Read a range of pages (1-indexed, inclusive). Out-of-range pages are skipped. |
| getSelection | | Read the user's current cursor / selection. |
| getTotalPages | | Total number of pages currently rendered in the editor. |
| insertBreak | | Insert a page or section break after a paragraph, by paraId. `page` adds a page break; `sectionNextPage` / `sectionContinuous` start a new section on a new page / the same page. Direct edit, not a tracked change. |
| onContentChange | | Subscribe to document content changes. Returns an unsubscribe function. |
| onSelectionChange | | Subscribe to selection changes (cursor moves / selection changes). Returns an unsubscribe function. |
| proposeChange | | Suggest a tracked change. `replaceWith=''` deletes; `search=''` inserts at paragraph end. |
| replyTo | | Reply to an existing comment. Returns the reply ID or null. |
| resolveComment | | Resolve a comment (mark as done). |
| scrollTo | | Scroll the editor to a paragraph by paraId, optionally flashing it. |
| setParagraphStyle | | Apply a paragraph style by styleId (e.g. `'Heading1'`, `'Quote'`). Direct edit, not a tracked change. |
SelectionInfointerfaceSource ↗
interface SelectionInfo| Member | Type | Summary |
|---|---|---|
| after | string | |
| before | string | |
| paragraphText | string | |
| paraId | string | null | |
| selectedText | string |
Variables (1)
docxAgentToolsconst
All built-in agent tools — read/write document content, comments, and tracked changes. Use getToolSchemas() to feed them to an LLM and executeToolCall() to run the handlers against an EditorBridge.
agentTools: AgentToolDefinition<any>[]