UNPKG

roam-research-mcp

Version:
209 lines (208 loc) • 11.6 kB
import { rethrowRoamError } from '../../shared/errors.js'; import { q } from '@roam-research/roam-api-sdk'; import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js'; import { resolveBlockRefs } from '../helpers/refs.js'; import { fetchChildrenByDepth } from '../helpers/fetch-children.js'; import { collectHiddenUids, pruneHiddenBlocks, isHiddenBlockString } from '../helpers/hidden.js'; import { fetchBlockVersions } from '../helpers/versions.js'; import { ednString } from '../../shared/datalog.js'; export class BlockRetrievalOperations { constructor(graph) { this.graph = graph; } /** Read a set of roots together, preserving requested order and missing slots. */ async fetchBlocks(uids, depth = 4, includeAncestors = false) { if (!uids.length) return []; if (!Number.isSafeInteger(depth) || depth < 0 || uids.some(uid => !uid)) { throw new McpError(ErrorCode.InvalidParams, 'Block UIDs are required and depth must be a non-negative integer.'); } const requested = uids.map(uid => uid.replace(/^\(\((.*)\)\)$/, '$1')); const unique = [...new Set(requested)]; const rows = await q(this.graph, `[:find ?uid ?string ?order ?heading :in $ [?uid ...] :where [?b :block/uid ?uid] [?b :block/string ?string] [?b :block/order ?order] [(get-else $ ?b :block/heading 0) ?heading]]`, [unique]); const hidden = await collectHiddenUids(this.graph); const visibleRows = rows.filter(([uid, string]) => !hidden.has(uid) && !isHiddenBlockString(string)); const children = await fetchChildrenByDepth(this.graph, visibleRows.map(([uid]) => uid), depth, 0, hidden); const roots = new Map(visibleRows.map(([uid, string, order, heading]) => [uid, { uid, string, order, heading: heading || undefined, children: pruneHiddenBlocks(children[uid] ?? []), }])); if (includeAncestors && roots.size > 0) { const ancestors = await q(this.graph, `[:find ?rootUid ?uid ?string ?title :in $ [?rootUid ...] :where [?root :block/uid ?rootUid] [?root :block/parents ?parent] [?parent :block/uid ?uid] [(get-else $ ?parent :block/string "") ?string] [(get-else $ ?parent :node/title "") ?title]]`, [[...roots.keys()]]); const byRoot = new Map(); for (const [rootUid, uid, string, title] of ancestors) { if (!byRoot.has(rootUid)) byRoot.set(rootUid, new Map()); byRoot.get(rootUid).set(uid, { uid, ...(title ? { title, is_page: true } : { string }), depth: 0 }); } const links = await q(this.graph, `[:find ?childUid ?parentUid :in $ [?childUid ...] :where [?child :block/uid ?childUid] [?parent :block/children ?child] [?parent :block/uid ?parentUid]]`, [[...new Set([...roots.keys(), ...ancestors.map(row => row[1])])]]); const parents = new Map(links); for (const [uid, root] of roots) { const ordered = []; const seen = new Set([uid]); let current = parents.get(uid); while (current && !seen.has(current)) { seen.add(current); const ancestor = byRoot.get(uid)?.get(current); if (!ancestor) break; ordered.push({ ...ancestor }); current = parents.get(current); } ordered.forEach((ancestor, index) => { ancestor.depth = ancestor.is_page ? 0 : ordered.length - index; }); root.ancestors = ordered; root.page_title = ordered.find(ancestor => ancestor.is_page)?.title; } } const allBlocks = []; const visit = (block) => { allBlocks.push(block); block.children.forEach(visit); }; roots.forEach(visit); await resolveBlockRefs(this.graph, allBlocks, 2, hidden); // One query for all the roots; a set with no versioned block stops there. for (const [uid, versions] of await fetchBlockVersions(this.graph, [...roots.keys()], hidden)) { roots.get(uid).versions = versions; } return requested.map(uid => roots.get(uid) ?? null); } async fetchBlock(block_uid_raw, depth = 4, include_ancestors = false) { if (!block_uid_raw) { throw new McpError(ErrorCode.InvalidRequest, 'block_uid is required.'); } const block_uid = block_uid_raw.replace(/^\(\((.*)\)\)$/, '$1'); try { const rootBlockQuery = `[:find ?string ?order ?heading :in $ ?blockUid :where [?b :block/uid ?blockUid] [?b :block/string ?string] [?b :block/order ?order] [(get-else $ ?b :block/heading 0) ?heading]]`; const rootBlockResults = await q(this.graph, rootBlockQuery, [block_uid]); if (!rootBlockResults || rootBlockResults.length === 0) { return null; } const [rootString, rootOrder, rootHeading] = rootBlockResults[0]; const childrenMap = await fetchChildrenByDepth(this.graph, [block_uid], depth); // A block the user tagged #.rm-hide / #.rm-private is withheld outright, // as is anything nested under it — same rule as the page read paths. if (isHiddenBlockString(rootString)) { return null; } const rootBlock = { uid: block_uid, string: rootString, order: rootOrder, heading: rootHeading || undefined, children: pruneHiddenBlocks(childrenMap[block_uid] || []), }; // Fetch ancestors if requested if (include_ancestors) { const ancestorQuery = `[:find (pull ?b [:block/uid :block/string {:block/parents [:block/uid :block/string :node/title]}]) :where [?b :block/uid ${ednString(block_uid)}]]`; const ancestorResults = await q(this.graph, ancestorQuery, []); if (ancestorResults && ancestorResults.length > 0) { const blockData = ancestorResults[0][0] || ancestorResults[0]; const parents = blockData[':block/parents'] || blockData.parents || []; const ancestors = []; for (const parent of parents) { const uid = parent[':block/uid'] || parent.uid; const str = parent[':block/string'] || parent.string; const title = parent[':node/title'] || parent.title; if (title) { ancestors.push({ uid, title, is_page: true, depth: 0 }); } else { ancestors.push({ uid, string: str, depth: -1 }); // depth computed below } } // Compute depths: page root is 0, work backward // The parents array from Roam is unordered, so we need to establish order. // Strategy: the page (is_page=true) is depth 0. Build the parent chain // by querying direct parent relationships. if (ancestors.length > 1) { // Query to get direct parent mapping for ordering const parentUids = ancestors.map(a => a.uid); const orderQuery = `[:find ?childUid ?parentUid :in $ [?childUid ...] :where [?c :block/uid ?childUid] [?c :block/parents ?p] [?p :block/uid ?parentUid] [?p :block/children ?c]]`; const orderResults = await q(this.graph, orderQuery, [[block_uid, ...parentUids]]); // Build child→parent map const childToParent = {}; for (const [childUid, parentUid] of orderResults) { childToParent[childUid] = parentUid; } // Walk from target block up to page root to determine order and depth const orderedAncestors = []; let current = block_uid; let depthCounter = ancestors.length; // start high, count down while (childToParent[current]) { const parentUid = childToParent[current]; const ancestor = ancestors.find(a => a.uid === parentUid); if (ancestor) { orderedAncestors.push(ancestor); } current = parentUid; } // Assign depths: first ancestor (direct parent) gets highest depth, page root gets 0 for (let i = 0; i < orderedAncestors.length; i++) { orderedAncestors[i].depth = orderedAncestors.length - i; } // Page root should be depth 0 const pageRoot = orderedAncestors.find(a => a.is_page); if (pageRoot) pageRoot.depth = 0; rootBlock.ancestors = orderedAncestors; } else { // Single ancestor = the page itself ancestors.forEach(a => { if (a.depth === -1) a.depth = 1; }); rootBlock.ancestors = ancestors; } // Set page_title from the page ancestor const page = (rootBlock.ancestors || ancestors).find(a => a.is_page); if (page?.title) { rootBlock.page_title = page.title; } } } // Gather all blocks in the tree to scan for references const allBlocks = []; const traverse = (b) => { allBlocks.push(b); b.children.forEach(traverse); }; traverse(rootBlock); // Resolve references (max depth 2) await resolveBlockRefs(this.graph, allBlocks, 2); // Nothing else in a fetch says a block has versions: the text above is // the one in use if this UID is, and an old one if it is not. const versions = (await fetchBlockVersions(this.graph, [block_uid], await collectHiddenUids(this.graph))).get(block_uid); if (versions) rootBlock.versions = versions; return rootBlock; } catch (error) { rethrowRoamError(error); throw new McpError(ErrorCode.InternalError, `Failed to fetch block: ${error instanceof Error ? error.message : String(error)}`); } } /** @deprecated Use fetchBlock() instead */ async fetchBlockWithChildren(block_uid, depth) { return this.fetchBlock(block_uid, depth); } }