rtm_find_optimal_depth
Determine the optimal traversal depth to achieve a target number of recall clauses for narrative text analysis using the Random Tree Model.
Instructions
Find the optimal traversal depth to achieve a target recall length
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The narrative text to analyze | |
| title | Yes | Title of the narrative | |
| targetRecallLength | Yes | Target number of clauses to recall | |
| maxBranchingFactor | No | Maximum number of child nodes (K parameter) | |
| maxRecallDepth | No | Maximum depth for recall (D parameter) |
Implementation Reference
- src/tools/findOptimalDepth.ts:16-84 (handler)The main handler function that implements the rtm_find_optimal_depth tool logic: builds RTM tree from narrative, finds optimal traversal depth for target recall length, computes neighboring depths comparison, and returns structured results.export default async function findOptimalDepth(params: FindOptimalDepthParams) { try { // Create narrative and build tree const narrative = createNarrative(params.text, params.title); const parameters = { ...createDefaultParameters(), maxBranchingFactor: params.maxBranchingFactor || 4, maxRecallDepth: params.maxRecallDepth || 6 }; const builder = new RTMTreeBuilder(parameters); const tree = builder.buildTree(narrative); // Create clause map for traversal const clauseMap = new Map( narrative.clauses.map(c => [c.id, c]) ); // Find optimal depth const traversal = createTraversal(tree, clauseMap); const optimalDepth = traversal.findOptimalDepth(params.targetRecallLength); // Get results at optimal depth const optimalResult = traversal.traverseToDepth(optimalDepth); // Also get results at neighboring depths for comparison const depthComparison = []; for (let d = Math.max(1, optimalDepth - 1); d <= Math.min(optimalDepth + 1, parameters.maxRecallDepth); d++) { const result = traversal.traverseToDepth(d); depthComparison.push({ depth: d, recallLength: result.totalClauses, compressionRatio: result.compressionRatio, nodeCount: result.nodes.length }); } const accuracy = (1 - Math.abs(optimalResult.totalClauses - params.targetRecallLength) / params.targetRecallLength) * 100; return { content: [{ type: "text", text: JSON.stringify({ success: true, treeId: tree.id, targetRecallLength: params.targetRecallLength, optimalDepth: optimalDepth, actualRecallLength: optimalResult.totalClauses, accuracy: accuracy, depthComparison: depthComparison, message: `Found optimal depth ${optimalDepth} yielding ${optimalResult.totalClauses} clauses (${accuracy.toFixed(1)}% accuracy)` }, null, 2) }], }; } catch (error) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : "Unknown error occurred" }, null, 2) }], }; } }
- Zod input schema defining parameters: text, title, targetRecallLength, optional maxBranchingFactor and maxRecallDepth.export const findOptimalDepthSchema = z.object({ text: z.string().describe('The narrative text to analyze'), title: z.string().describe('Title of the narrative'), targetRecallLength: z.number().min(1).describe('Target number of clauses to recall'), maxBranchingFactor: z.number().default(4).describe('Maximum number of child nodes (K parameter)'), maxRecallDepth: z.number().default(6).describe('Maximum depth for recall (D parameter)'), });
- src/index.ts:47-52 (registration)Tool registry mapping 'rtm_find_optimal_depth' to the findOptimalDepth handler function for execution.const tools = { rtm_create_narrative_tree: createNarrativeTree, rtm_generate_ensemble: generateEnsemble, rtm_traverse_narrative: traverseNarrative, rtm_find_optimal_depth: findOptimalDepth, };
- src/index.ts:71-75 (registration)Tool definition registration providing name, description, and input schema for ListTools response.{ name: 'rtm_find_optimal_depth', description: 'Find the optimal traversal depth to achieve a target recall length', inputSchema: findOptimalDepthSchema, },