UNPKG

@stll/folio-agents

Version:

Framework-neutral LLM tool layer over folio's ai-edits engine: function-calling tools so an agent can read and mutate .docx documents through @stll/folio-core.

87 lines (86 loc) • 3.04 kB
import { registerDecodedCommentHandlers, registerOperationSession } from "../bridge.js"; import { decodeCommentId } from "../codecs.js"; import { toAgentChange } from "./shared.js"; // src/bridges/reviewer.ts const toAgentCommentReply = (reply) => ({ id: String(reply.id), author: reply.author, text: reply.text }); const toAgentComment = (comment) => ({ id: String(comment.id), author: comment.author, text: comment.text, resolved: comment.done, blockId: comment.blockId, quote: comment.anchoredText, replies: comment.replies.map(toAgentCommentReply) }); /** * Build a {@link FolioAgentBridge} over a headless {@link FolioDocxReviewer} * (`@stll/folio-core/server`). A headless document has no live page, selection, * or scroll surface, so `read_page`, `read_selection`, and `scroll_to_block` report an * unsupported-capability error via {@link executeFolioToolCall} on this * bridge. */ const createReviewerBridge = (reviewer, options = {}) => { const mode = options.mode ?? "tracked-changes"; const { revisionStamp } = options; const applyOptions = revisionStamp === void 0 ? {} : { revisionStamp }; const replyDate = revisionStamp === void 0 ? {} : { date: revisionStamp.date }; const withMode = (batch) => batch.mode === void 0 ? { ...batch, mode } : batch; const operationOptions = { ...applyOptions, undefinedReferences: "refuse" }; const registerBridge = (bridge) => { registerOperationSession(bridge, reviewer); return registerDecodedCommentHandlers(bridge, { replyToComment: ({ numeric }, text) => reviewer.replyTo(numeric, { text, ...replyDate }) !== null, resolveComment: ({ raw }, resolved) => reviewer.resolveComment(raw, { resolved }) }); }; const bridge = { snapshot: () => reviewer.snapshot(), documentOperationMode: mode, applyDocumentOperations: (batch) => reviewer.applyDocumentOperations(withMode(batch), operationOptions), undoDocumentOperations: (undoHandle) => reviewer.undoDocumentOperations(undoHandle), getComments: () => reviewer.getComments().map(toAgentComment), getChanges: () => reviewer.getChanges().map(toAgentChange), listStories: () => reviewer.listStories(), readStory: (handle) => reviewer.readStory(handle), forStory: (story) => { const snapshot = reviewer.snapshotStory(story); if (snapshot === null) return null; return registerBridge({ ...bridge, snapshot: () => snapshot, applyDocumentOperations: (batch) => reviewer.applyDocumentOperationsToStory({ ...operationOptions, story, batch: withMode(batch), snapshot }) }); }, replyToComment: (commentId, text) => { const decoded = decodeCommentId(commentId); return decoded !== null && reviewer.replyTo(decoded.numeric, { text, ...replyDate }) !== null; }, resolveComment: (commentId, resolved) => { const decoded = decodeCommentId(commentId); return decoded !== null && reviewer.resolveComment(decoded.raw, { resolved }); } }; return registerBridge(bridge); }; export { createReviewerBridge };