UNPKG

roam-research-mcp

Version:
63 lines (62 loc) • 2.09 kB
/** * CLI graph resolution utilities for multi-graph support */ import { createRegistryFromEnv } from '../../config/graph-registry.js'; import { validateEnvironment } from '../../config/environment.js'; let registry = null; /** * Get or create the GraphRegistry singleton */ export function getRegistry() { if (!registry) { validateEnvironment(); registry = createRegistryFromEnv(); } return registry; } /** * Resolve a Graph instance for CLI use * Validates write access for write operations * * @param options - CLI options containing graph and writeKey * @param isWriteOp - Whether this is a write operation */ export function resolveGraph(options, isWriteOp = false) { const reg = getRegistry(); if (isWriteOp) { // For write operations, validate write access const graphKey = options.graph ?? reg.defaultKey; if (!reg.isWriteAllowed(graphKey, options.writeKey)) { const config = reg.getConfig(graphKey); if (config?.protected) { const systemWriteKey = process.env.ROAM_SYSTEM_WRITE_KEY; if (!systemWriteKey) { throw new Error(`Write to protected graph "${graphKey}" failed: ROAM_SYSTEM_WRITE_KEY not configured.`); } // Never echo the key itself — CLI output lands in scrollback, logs and // transcripts. Name the variable so the shell can expand it instead. throw new Error(`Write to protected graph "${graphKey}" requires --write-key confirmation.\n` + `Use: --write-key "$ROAM_SYSTEM_WRITE_KEY"`); } } } return reg.getGraph(options.graph); } /** * Get available graph names for help text */ export function getAvailableGraphs() { return getRegistry().getAvailableGraphs(); } /** * Get the default graph key */ export function getDefaultGraphKey() { return getRegistry().defaultKey; } /** * Check if running in multi-graph mode */ export function isMultiGraphMode() { return getRegistry().isMultiGraph; }