roam-research-mcp
Version:
MCP server and CLI for Roam Research
391 lines (390 loc) • 13.9 kB
JavaScript
/**
* Translator: converts CLI-style commands to Roam batch actions
*/
import { resolveParentRef, generatePlaceholder } from './resolver.js';
import { buildTableActions } from '../../tools/operations/table.js';
import { formatMemoryText } from '../../tools/operations/memory.js';
/**
* Translate a single command to batch actions
* May return multiple actions (e.g., table, outline)
*/
export function translateCommand(command, context) {
switch (command.command) {
case 'create':
return translateCreate(command, context);
case 'update':
return translateUpdate(command);
case 'delete':
return translateDelete(command);
case 'move':
return translateMove(command, context);
case 'todo':
return translateTodo(command, context);
case 'table':
return translateTable(command, context);
case 'outline':
return translateOutline(command, context);
case 'remember':
return translateRemember(command, context);
case 'page':
return translatePage(command, context);
case 'codeblock':
return translateCodeblock(command, context);
default:
throw new Error(`Unknown command: ${command.command}`);
}
}
/**
* Resolve parent UID from command params
*/
function getParentUid(params, context) {
// Direct parent UID or placeholder
if (params.parent) {
const resolved = resolveParentRef(params.parent, context);
if (resolved)
return resolved;
throw new Error(`Parent "${params.parent}" could not be resolved to a UID. Page may not exist.`);
}
// Page UID
if (params.pageUid) {
return params.pageUid;
}
// Page title -> resolved UID
if (params.page) {
const uid = context.pageUids.get(params.page);
if (!uid) {
throw new Error(`Page "${params.page}" not found`);
}
return uid;
}
// Default to daily page
if (context.dailyPageUid) {
return context.dailyPageUid;
}
throw new Error('No parent specified and daily page not resolved');
}
/**
* Register a placeholder in the context
*/
function registerPlaceholder(name, context) {
const placeholder = generatePlaceholder(name);
context.placeholders.set(name, placeholder);
return placeholder;
}
/**
* A name for a block the caller did not name with `as`, unique in this batch.
*
* These were stamped with `Date.now()`. Commands are translated in one
* synchronous pass, so two of a kind got the same stamp and the same names,
* and the batch created the second command's blocks under the first one's
* UIDs. A count held by the batch cannot repeat.
*/
function internalName(kind, context) {
context.internalNames += 1;
return `_${kind}_${context.internalNames}`;
}
/**
* Update level stack for hierarchy tracking
*/
function updateLevelStack(level, uid, context) {
// Ensure stack is long enough
while (context.levelStack.length < level) {
context.levelStack.push('');
}
context.levelStack[level - 1] = uid;
// Truncate stack above current level
context.levelStack.length = level;
}
/**
* Get parent from level stack for hierarchical nesting
*/
function getParentFromLevel(level, context) {
if (level <= 1) {
return context.currentParent;
}
const parentLevel = level - 1;
if (parentLevel <= context.levelStack.length && context.levelStack[parentLevel - 1]) {
return context.levelStack[parentLevel - 1];
}
return context.currentParent;
}
// --- Command translators ---
function translateCreate(cmd, context) {
const { params } = cmd;
let parentUid;
// Handle level-based hierarchy
if (params.level !== undefined && params.level > 1) {
const levelParent = getParentFromLevel(params.level, context);
if (levelParent) {
parentUid = levelParent;
}
else {
parentUid = getParentUid(params, context);
}
}
else {
parentUid = getParentUid(params, context);
// Set as current parent for level-based children
if (params.level === 1 || params.level === undefined) {
context.currentParent = parentUid;
}
}
const action = {
action: 'create-block',
string: params.text,
location: {
'parent-uid': parentUid,
order: params.order ?? 'last'
}
};
// Register placeholder if 'as' is specified
if (params.as) {
action.uid = registerPlaceholder(params.as, context);
}
// Optional properties
if (params.heading) {
action.heading = params.heading;
}
if (params['children-view-type']) {
action['children-view-type'] = params['children-view-type'];
}
// Update level stack if level is specified
if (params.level !== undefined && params.as) {
updateLevelStack(params.level, `{{uid:${params.as}}}`, context);
}
return [action];
}
function translateUpdate(cmd) {
const { params } = cmd;
const action = {
action: 'update-block',
uid: params.uid,
string: params.text
};
if (params.heading !== undefined) {
action.heading = params.heading;
}
if (params.open !== undefined) {
action.open = params.open;
}
if (params['text-align']) {
action['text-align'] = params['text-align'];
}
if (params['children-view-type']) {
action['children-view-type'] = params['children-view-type'];
}
return [action];
}
function translateDelete(cmd) {
return [{
action: 'delete-block',
uid: cmd.params.uid
}];
}
function translateMove(cmd, context) {
const { params } = cmd;
return [{
action: 'move-block',
uid: params.uid,
location: {
// Resolved like every other parent. Passed through raw, a page title or
// "daily" reached the batch as a "UID" and was refused there.
'parent-uid': getParentUid({ parent: params.parent }, context),
order: params.order ?? 'last'
}
}];
}
function translateTodo(cmd, context) {
const { params } = cmd;
const parentUid = getParentUid(params, context);
const action = {
action: 'create-block',
string: `{{[[TODO]]}} ${params.text}`,
location: {
'parent-uid': parentUid,
order: params.order ?? 'last'
}
};
if (params.as) {
action.uid = registerPlaceholder(params.as, context);
}
return [action];
}
function translateTable(cmd, context) {
const { params } = cmd;
const parentUid = getParentUid(params, context);
// The layout is roam_create_table's: one builder, so the CLI and the MCP
// tool cannot disagree about it. This used to hang every cell under its
// column's header, which Roam reads as a table turned on its side (#40).
const scope = internalName('table', context);
const tableName = params.as ?? scope;
registerPlaceholder(tableName, context);
return buildTableActions({
parent_uid: parentUid,
order: params.order,
headers: params.headers,
rows: params.rows
}, { table: tableName, cellPrefix: `${scope}_` });
}
/**
* Check an outline's `items` and bring each one to `{text, level, ...}`.
*
* A plain string is a level-1 block, which is what the help has always shown,
* and a missing `level` is 1. Anything else is refused by name: a level that
* is not a whole number of 1 or more used to reach `levelStack.length = level`
* and surface as a bare "Invalid array length" (issue #34).
*
* Throws with the item's position, e.g. `items[2].level must be ...`.
*/
export function normalizeOutlineItems(items) {
if (!Array.isArray(items)) {
throw new Error(`'items' must be an array of strings or {text, level?} objects`);
}
return items.map((item, i) => {
if (typeof item === 'string') {
return { text: item, level: 1 };
}
if (!item || typeof item !== 'object' || Array.isArray(item)) {
throw new Error(`items[${i}] must be a string or a {text, level?} object (got ${JSON.stringify(item)})`);
}
const { text, level: given } = item;
if (typeof text !== 'string') {
throw new Error(`items[${i}].text must be a string (got ${JSON.stringify(text)})`);
}
// "2" worked before by coercion, so it still does.
const level = given == null ? 1
: typeof given === 'string' && given.trim() !== '' ? Number(given)
: given;
if (typeof level !== 'number' || !Number.isInteger(level) || level < 1) {
throw new Error(`items[${i}].level must be a whole number, 1 or more (got ${JSON.stringify(given)})`);
}
return { ...item, text, level };
});
}
function translateOutline(cmd, context) {
const { params } = cmd;
const parentUid = getParentUid(params, context);
const actions = [];
// Reset level stack with outline parent
context.currentParent = parentUid;
context.levelStack = [];
for (const item of normalizeOutlineItems(params.items)) {
let itemParentUid;
if (item.level === 1) {
itemParentUid = parentUid;
}
else {
const levelParent = getParentFromLevel(item.level, context);
if (!levelParent) {
throw new Error(`Invalid outline hierarchy: level ${item.level} has no parent`);
}
itemParentUid = levelParent;
}
const action = {
action: 'create-block',
string: item.text,
location: {
'parent-uid': itemParentUid,
order: 'last'
}
};
// Register placeholder
const placeholderName = item.as || internalName('outline', context);
action.uid = registerPlaceholder(placeholderName, context);
if (item.heading) {
action.heading = item.heading;
}
// Update level stack
updateLevelStack(item.level, action.uid, context);
actions.push(action);
}
return actions;
}
function translateRemember(cmd, context) {
const { params } = cmd;
// The same text roam_remember writes: categories, then the memories tag
// roam_recall finds the memory by.
const memoryText = formatMemoryText(params.text, params.categories, params.include_memories_tag === false ? null : context.memoriesTag);
let parentUid;
// If heading is specified, we'd need to look it up or create it
// For simplicity, require parent UID when heading is used
// TODO: Add heading resolution support
if (params.heading && !params.parent) {
throw new Error('remember with --heading requires --parent or heading resolution not yet implemented');
}
parentUid = getParentUid(params, context);
const action = {
action: 'create-block',
string: memoryText,
location: {
'parent-uid': parentUid,
order: params.order ?? 'last'
}
};
if (params.as) {
action.uid = registerPlaceholder(params.as, context);
}
return [action];
}
function translatePage(cmd, context) {
// Note: Page creation uses create-page API, not batch actions
// We'll handle this specially in the batch command
// For now, throw an error to indicate this needs special handling
throw new Error('page command requires special handling outside batch actions');
}
function translateCodeblock(cmd, context) {
const { params } = cmd;
const parentUid = getParentUid(params, context);
// Format code with triple backticks. `language` is optional: left out, it
// used to be written into the fence as the word "undefined".
const codeContent = '```' + (params.language ?? '') + '\n' + params.code + '\n```';
const action = {
action: 'create-block',
string: codeContent,
location: {
'parent-uid': parentUid,
order: params.order ?? 'last'
}
};
if (params.as) {
action.uid = registerPlaceholder(params.as, context);
}
return [action];
}
/**
* Point actions at pages created by `page` commands.
*
* A `page` command's `as` name stands for a page UID, and that UID exists only
* once the page does, which is after translation. Until then a parent of
* `{{name}}` is carried as `{{uid:name}}`, the form block placeholders use;
* left like that, the batch takes it for a block nobody defined and refuses
* the whole run. Called with the UIDs once the pages exist.
*/
export function bindPagePlaceholders(actions, pageUidsByName) {
const uidByPlaceholder = new Map(Array.from(pageUidsByName, ([name, uid]) => [generatePlaceholder(name), uid]));
return actions.map(action => {
const parent = action.location?.['parent-uid'];
const pageUid = parent === undefined ? undefined : uidByPlaceholder.get(parent);
if (!action.location || pageUid === undefined)
return action;
return { ...action, location: { ...action.location, 'parent-uid': pageUid } };
});
}
/**
* Translate all commands to batch actions
*/
export function translateAllCommands(commands, context) {
const actions = [];
const pageCommands = [];
for (const cmd of commands) {
if (cmd.command === 'page') {
// Collect page commands for special handling
pageCommands.push(cmd);
}
else {
const cmdActions = translateCommand(cmd, context);
actions.push(...cmdActions);
}
}
return { actions, pageCommands };
}