Tools for your agent
Connect any LLM to Revise's JSON-schema document tools in the browser or Node.
The SDK exposes JSON-schema document tools that operate on document structure without relying on React state or browser focus.
Tool loop#
const definitions = editor.tools.getDefinitions();
const response = await yourModel.createMessage({
messages,
tools: definitions.map((tool) => ({
name: tool.name,
description: tool.description,
input_schema: tool.inputSchema,
})),
});
for (const call of response.toolCalls) {
const result = await editor.tools.executeDynamic(call.name, call.input);
messages.push({
role: "tool",
tool_call_id: call.id,
content: result.ok
? [
result.value.message ?? JSON.stringify(result.value.data),
result.value.context?.html, // document HTML with block IDs
]
.filter(Boolean)
.join("\n\n")
: `${result.error.code}: ${result.error.message}`,
});
}Always return both message (summary) and context.html (block-ID document content) on read and search results.
Result shape#
Both editor.tools and revise/server's doc.tools use the same result envelope:
type ReviseToolResult<Name> =
| { ok: true; value: ReviseToolResponse<Name> }
| { ok: false; error: ReviseToolFailure<Name> }; // { callId, tool, code, message, details? }
interface ReviseToolResponse<Name> {
callId: string;
tool: Name;
message: string | null;
data: ReviseToolData<Name>;
context: { format: "revise-html"; html: string } | null;
suggestionIds: string[] | null;
}editor.tools.execute()andeditor.tools.executeDynamic()return{ ok: false, error }for expected rejections; passerror.messageback to the model.editor.tools.call()(browser) returnsReviseToolResponsedirectly and throwsReviseToolErroron rejection. (Onrevise/server,doc.tools.call()returnsReviseToolResultand does not throw.)- Mutations in suggesting mode populate
suggestionIds, which can be passed toeditor.review.acceptSuggestions().
Working with blocks#
const read = await editor.tools.call("read_blocks_from_index", {
index: 0,
context_notes: "Looking for the liability clause",
});
await editor.tools.call("replace", {
id: "b12",
replacements: [
{
find: "capped at the fees paid",
replace: "capped at two years of fees paid",
occurrence: "unique",
},
],
});Model schemas require context_notes on read/search calls. Typed execute() and call() default it to "N/A" when omitted; executeDynamic() validates model inputs strictly.
replace targets one block per call unless given a search_result_id.
Bulk edits with search_result_id#
search_document returns a search_result_id representing the complete match set across all pages. Pass it to replace, replace_block, style_blocks, or remove_blocks to act on all matched blocks in one call:
const search = await editor.tools.call("search_document", {
queries: ["Acme Corp."],
page: 0,
context_notes: "Renaming counterparty",
});
await editor.tools.call("replace", {
search_result_id: search.data.search_result_id,
replacements: [
{ find: "Acme Corp.", replace: "Acme Holdings Ltd.", occurrence: "all" },
],
});search_result_id is scoped to the current document session and invalidated if matched blocks change (re-run search_document to obtain a fresh ID).
Document routing#
Every browser tool schema accepts optional document_id (defaulting to the active document). Application code can pass { documentId } or bind a scoped controller:
await editor.tools.call("set_title", { title: "Exhibit A" }, {
documentId: "exhibit-a",
});
const exhibit = editor.tools.forDocument("exhibit-a");
await exhibit.execute("measure_document", {});Routing never falls back to the active document when a target is unavailable. Rejection codes:
document_not_open: never opened or already closeddocument_not_ready: still openingdocument_open_failed: open failed (close the ID before reopening)document_scope_mismatch:document_iddiffers from aforDocument(id)controllerinvalid_input: non-object input, empty/non-stringdocument_id, or conflictingdocumentIdanddocument_id
Suggestions and direct mode#
Tool mutations create tracked changes by default regardless of the editor's typing mode. Pass { directMode: true } to apply edits directly:
await editor.tools.call("style_blocks", input, { directMode: true });Scope#
UI operations (review navigation, comment panels, focus, viewport, zoom, canvas rendering) are host handle APIs, not agent tools. The sole exception is render_document_pages, available internally to the delegated agent. See the tool reference.
Server-side editing#
revise/server runs the headless tool catalog in Node on a ReviseDocument opened from a file or Y.Doc. A ReviseDocument is single-document (schemas omit document_id), persists search_result_id across calls, serializes concurrent calls, and must be disposed with doc.dispose() (disposing a fromYDoc instance leaves your Y.Doc alive).
Both doc.tools.call() (typed) and doc.tools.run() (untyped model input) return ReviseToolResult and do not throw on rejected edits. Switch modes with await doc.setMode("editing" | "suggesting") or per-call { directMode: true } / { direct: true }.
import { readFile, writeFile } from "node:fs/promises";
import { ReviseDocument } from "revise/server";
const doc = await ReviseDocument.open("agreement.docx", {
documentId: "agreement-1",
mode: "suggesting",
author: "Contract bot",
});
const read = await doc.tools.call("read_blocks_from_index", {
index: 0,
context_notes: "Locating liability clause",
});
if (read.ok) console.log(read.value.context?.html);
const search = await doc.tools.call("search_document", {
queries: ["fees paid"],
page: 0,
context_notes: "Liability cap",
});
const searchResultId = search.ok ? search.value.data.search_result_id : null;
if (!searchResultId) throw new Error("No editable matches");
await doc.tools.call(
"replace",
{
search_result_id: searchResultId,
replacements: [{ find: "fees paid", replace: "fees paid or payable" }],
},
{ directMode: true },
);
const governingLaw = await doc.tools.call("search_document", {
queries: ["Delaware"],
page: 0,
context_notes: "Governing law",
});
const governingLawId = governingLaw.ok
? governingLaw.value.data.search_result_id
: null;
if (!governingLawId) throw new Error("Governing law text not found");
const edit = await doc.tools.call("replace", {
search_result_id: governingLawId,
replacements: [{ find: "Delaware", replace: "New York" }],
});
const created = edit.ok ? (edit.value.suggestionIds ?? []) : [];
await doc.accept({ ids: created });
const mine = (await doc.changes()).filter((c) => c.author === "Contract bot");
await doc.reject({ ids: mine.map((c) => c.id) });
await writeFile("agreement.revise", await doc.snapshot());
const restored = await ReviseDocument.restore(
await readFile("agreement.revise"),
);
const { bytes } = await restored.export({ format: "docx" });
await writeFile("agreement-amended.docx", bytes);
restored.dispose();
doc.dispose();For Anthropic and OpenAI loops, use agentTools from revise/server.
Revise semantic HTML#
Read and search tools return context.html in Revise semantic HTML: block markup with stable IDs for paragraphs, runs, tables, comments, and notes. Structural insertion and replacement tools accept the same markup. It is not general browser HTML/CSS; the Yjs document model is the source of truth, and tool calls do not reparse unchanged blocks. Do not mutate DocRoot directly—use doc.tools.