@nutrient-sdk/document-authoring-ai
v1.0.2
Published
AI tool definitions and editor execution helpers for Nutrient Document Authoring.
Downloads
1,092
Readme
Nutrient Document Authoring AI
Document Authoring AI is the AI toolkit for Nutrient Document Authoring. It gives a Large Language Model (LLM) a list of document tools, validates the tool calls the model sends back, and applies accepted edits through a live Document Authoring editor.
The package is split by where the code runs. The root entrypoint is safe to use
on a server because it only exports prompts, schemas, tool metadata, and
validation helpers. The /editor entrypoint is for browser code that owns a
DocAuthEditor. The /vercel and /langchain entrypoints adapt the same tool
definitions for those AI frameworks.
Install
Install the AI package first. This is enough to call getAiPromptGuide(),
inspect tool definitions, validate tool calls, define workflows, use adapters,
and export tool metadata as JSON.
npm install @nutrient-sdk/document-authoring-aiIf you execute tools against a Document Authoring editor, install Document
Authoring SDK 1.15.0 or newer:
npm install @nutrient-sdk/document-authoring@^1.15.0You do not need @nutrient-sdk/document-authoring when you stay on the root,
/vercel, or /langchain entrypoints. You need it when you import
@nutrient-sdk/document-authoring-ai/editor, because that entrypoint talks to a
live DocAuthEditor.
If you use the Vercel AI SDK adapter, install the Vercel AI SDK too. The
adapter supports ai versions 6 and 7:
npm install aiThe LangChain adapter uses this package's built-in Zod dependency and does not need another peer dependency from this package.
Entrypoints
import { getAiPromptGuide, getAiToolDefinitions, getBuiltInWorkflow, parseAiToolCall } from '@nutrient-sdk/document-authoring-ai';
import { getAiToolkit } from '@nutrient-sdk/document-authoring-ai/editor';
import { toVercelAiTools } from '@nutrient-sdk/document-authoring-ai/vercel';
import { toLangChainTools } from '@nutrient-sdk/document-authoring-ai/langchain';Use the root entrypoint for model-facing setup:
getAiPromptGuide()returns instructions you can add to your model's system prompt.getAiToolDefinitions()returns built-in tool names, descriptions, input schemas, output schemas, and read/write metadata.parseAiToolCall(...)validates untrusted tool calls from a model.getBuiltInWorkflow(...)returns the built-in proofreading and translation workflows.createWorkflow(...)defines a custom workflow that uses the same structured replacement-fragment output.
A typical integration has two halves. Your model runtime gets prompts, tool
definitions, and schemas from the root entrypoint or an adapter entrypoint. Your
browser app receives tool calls and executes them through the /editor
entrypoint, where the live document exists.
Tool Definitions
Send tool definitions to the model. A tool definition names one document action and includes the JSON schema for its arguments. When the model returns a tool call, parse it before it reaches the editor.
import { getAiPromptGuide, getAiToolDefinitions, parseAiToolCall, type UnvalidatedAiToolCall } from '@nutrient-sdk/document-authoring-ai';
const definitions = getAiToolDefinitions({
tools: ['read_document', 'search_elements', 'read_element', 'replace_text'],
});
const systemPrompt = `${getAiPromptGuide()}\n\nUse only these document tools.`;
function parseModelToolCall(call: UnvalidatedAiToolCall) {
return parseAiToolCall(call, definitions);
}The read tools inspect the current document:
read_documentsearch_elementsread_element
The write tools edit document content:
add_paragraphsadd_section_breakreplace_paragraphreplace_textadd_tablereplace_tabledelete_blockformat_textformat_list
By default, write tools apply changes directly. At execution time you can switch
to tracked changes. You can also expose a reviewComment argument so the model
can leave a review comment with an edit.
const definitions = getAiToolDefinitions({
reviewComments: 'create',
});Editor Execution
Use getAiToolkit(editor) in browser code that already has a DocAuthEditor.
That is where validated tool calls turn into real document reads and writes.
This entrypoint requires @nutrient-sdk/document-authoring 1.15.0 or newer.
import { parseAiToolCall } from '@nutrient-sdk/document-authoring-ai';
import { getAiToolkit } from '@nutrient-sdk/document-authoring-ai/editor';
import type { DocAuthEditor } from '@nutrient-sdk/document-authoring';
import type { UnvalidatedAiToolCall } from '@nutrient-sdk/document-authoring-ai';
export async function executeDocumentTool(editor: DocAuthEditor, rawCall: UnvalidatedAiToolCall) {
const toolkit = getAiToolkit(editor);
try {
const call = parseAiToolCall(rawCall);
return await toolkit.executeTool(call, {
writeMode: 'track_changes',
reviewComments: 'create',
});
} finally {
toolkit.dispose();
}
}executeTool(...) returns the validated call, the tool result, and an execution
state. Read results have execution.kind === 'read'. Write results say whether
the edit was applied directly or with tracked changes.
Vercel AI SDK
@nutrient-sdk/document-authoring-ai/vercel converts built-in tool definitions
to Vercel AI SDK tool metadata. It needs the ai package.
import { getAiToolDefinitions, getBuiltInWorkflow } from '@nutrient-sdk/document-authoring-ai';
import { toVercelAiTools, toVercelAiWorkflowOutputSchema } from '@nutrient-sdk/document-authoring-ai/vercel';
const tools = toVercelAiTools(getAiToolDefinitions());
const workflow = getBuiltInWorkflow('translation', {
targetLanguage: 'german',
});
const outputSchema = toVercelAiWorkflowOutputSchema(workflow).schema;The adapter returns metadata and schemas. It does not execute document writes on
the model server. Pass accepted tool calls to getAiToolkit(editor) in the
browser.
LangChain
@nutrient-sdk/document-authoring-ai/langchain converts built-in tool
definitions to LangChain-style metadata with Zod schemas.
import { getAiToolDefinitions, getBuiltInWorkflow } from '@nutrient-sdk/document-authoring-ai';
import { toLangChainTools, toLangChainWorkflowOutputSchema } from '@nutrient-sdk/document-authoring-ai/langchain';
const tools = toLangChainTools(
getAiToolDefinitions({
tools: ['search_elements', 'read_element', 'replace_text'],
}),
);
const workflow = getBuiltInWorkflow('proofreading');
const outputSchema = toLangChainWorkflowOutputSchema(workflow);These tool definitions do not include execution functions. Keep execution in your application, then pass validated tool calls to the editor toolkit.
Workflows
Workflows are useful when the model should rewrite a whole selection or document fragment, for example during proofreading or translation. The toolkit reads the current scope as plain text plus a DocJSON fragment, which is the structured Document Authoring content that can be replaced. Your model returns a replacement fragment. The toolkit validates that output before applying it.
import { getBuiltInWorkflow } from '@nutrient-sdk/document-authoring-ai';
import { getAiToolkit } from '@nutrient-sdk/document-authoring-ai/editor';
import type { DocAuthEditor } from '@nutrient-sdk/document-authoring';
export async function runTranslationWorkflow(editor: DocAuthEditor, modelOutput: unknown) {
const toolkit = getAiToolkit(editor);
const workflow = getBuiltInWorkflow('translation', {
targetLanguage: 'spanish',
});
try {
const input = await toolkit.readWorkflowInput(workflow, {
scope: 'selection',
});
const result = await toolkit.applyWorkflowOutput(workflow, modelOutput, {
scope: input.scope,
writeMode: 'track_changes',
});
return { input, result };
} finally {
toolkit.dispose();
}
}Send input.inputText, input.inputFragment, workflow.systemPrompt, and
workflow.defaultTask to your model. Pass the model's structured output to
applyWorkflowOutput(...).
Built-in workflows:
proofreadingtranslation, withenglish,german,french, andspanishtargets
Use createWorkflow(...) for custom prompts that still return a structured
replacement fragment.
Exporting Tool Definitions
Backends that do not run TypeScript can use the built-in tool metadata as JSON.
npx document-authoring-ai-export > document-authoring-ai-tools.json
npx document-authoring-ai-export --tools search_elements,read_element,replace_textThe command prints a versioned JSON payload with tool names, descriptions, schemas, and read/write metadata.
License
This package uses the same license as
@nutrient-sdk/document-authoring: see the
Nutrient SDK User Evaluation Subscription Agreement.
