|
| 1 | +import type { Operation } from '../core/types.js'; |
| 2 | +import type { ContextCoreLoader, Holon } from './loader.js'; |
| 3 | + |
| 4 | +interface ChainNode { |
| 5 | + id: string; |
| 6 | + title: string; |
| 7 | + kind: string; |
| 8 | + depth: number; |
| 9 | + via_edge?: { relation: string; confidence: number }; |
| 10 | +} |
| 11 | + |
| 12 | +function bfsChain( |
| 13 | + loader: ContextCoreLoader, |
| 14 | + startId: string, |
| 15 | + maxDepth: number, |
| 16 | + minConf: number, |
| 17 | +): ChainNode[] { |
| 18 | + const start = loader.byId(startId); |
| 19 | + if (!start) return []; |
| 20 | + const visited = new Set<string>(); |
| 21 | + const queue: Array<{ holon: Holon; depth: number; via?: { relation: string; confidence: number } }> = [ |
| 22 | + { holon: start, depth: 0 }, |
| 23 | + ]; |
| 24 | + const result: ChainNode[] = []; |
| 25 | + while (queue.length > 0) { |
| 26 | + const item = queue.shift()!; |
| 27 | + if (visited.has(item.holon.id)) continue; |
| 28 | + visited.add(item.holon.id); |
| 29 | + result.push({ |
| 30 | + id: item.holon.id, |
| 31 | + title: item.holon.title, |
| 32 | + kind: item.holon.kind, |
| 33 | + depth: item.depth, |
| 34 | + ...(item.via ? { via_edge: item.via } : {}), |
| 35 | + }); |
| 36 | + if (item.depth < maxDepth) { |
| 37 | + for (const edge of item.holon.causal_edges) { |
| 38 | + if (edge.confidence < minConf) continue; |
| 39 | + const target = loader.byId(edge.target_id); |
| 40 | + if (target && !visited.has(target.id)) { |
| 41 | + queue.push({ |
| 42 | + holon: target, |
| 43 | + depth: item.depth + 1, |
| 44 | + via: { relation: edge.relation, confidence: edge.confidence }, |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + return result; |
| 51 | +} |
| 52 | + |
| 53 | +function notReady(path: string) { |
| 54 | + return { error: 'context-core.json not found', hint: `Run: python -m compiler <vault_path> -o ${path}` }; |
| 55 | +} |
| 56 | + |
| 57 | +export function makeCausalOps(loader: ContextCoreLoader): Operation[] { |
| 58 | + return [ |
| 59 | + { |
| 60 | + name: 'causal.chain', |
| 61 | + namespace: 'causal' as Operation['namespace'], |
| 62 | + description: 'BFS-traverse the causal graph outward from a starting holon', |
| 63 | + mutating: false, |
| 64 | + params: { |
| 65 | + id: { type: 'string', required: true, description: 'Starting holon ID' }, |
| 66 | + max_depth: { type: 'number', required: false, description: 'Max traversal depth (default: 3)', default: 3 }, |
| 67 | + min_confidence: { type: 'number', required: false, description: 'Min edge confidence 0–1 (default: 0)', default: 0 }, |
| 68 | + }, |
| 69 | + handler: async (_ctx, params) => { |
| 70 | + if (!loader.get()) return notReady(loader.path); |
| 71 | + const id = params.id as string; |
| 72 | + const maxDepth = (params.max_depth as number | undefined) ?? 3; |
| 73 | + const minConf = (params.min_confidence as number | undefined) ?? 0; |
| 74 | + if (!loader.byId(id)) return { error: `Holon not found: ${id}` }; |
| 75 | + const nodes = bfsChain(loader, id, maxDepth, minConf); |
| 76 | + return { start_id: id, node_count: nodes.length, nodes }; |
| 77 | + }, |
| 78 | + }, |
| 79 | + |
| 80 | + { |
| 81 | + name: 'causal.neighbors', |
| 82 | + namespace: 'causal' as Operation['namespace'], |
| 83 | + description: 'Get direct causal neighbors (depth 1) of a holon', |
| 84 | + mutating: false, |
| 85 | + params: { |
| 86 | + id: { type: 'string', required: true, description: 'Holon ID' }, |
| 87 | + direction: { |
| 88 | + type: 'string', required: false, |
| 89 | + description: 'outbound | inbound | both (default: outbound)', |
| 90 | + enum: ['outbound', 'inbound', 'both'], |
| 91 | + default: 'outbound', |
| 92 | + }, |
| 93 | + }, |
| 94 | + handler: async (_ctx, params) => { |
| 95 | + const cc = loader.get(); |
| 96 | + if (!cc) return notReady(loader.path); |
| 97 | + const id = params.id as string; |
| 98 | + const dir = (params.direction as string | undefined) ?? 'outbound'; |
| 99 | + const h = loader.byId(id); |
| 100 | + if (!h) return { error: `Holon not found: ${id}` }; |
| 101 | + |
| 102 | + const outbound = (dir === 'outbound' || dir === 'both') |
| 103 | + ? h.causal_edges.map(e => ({ |
| 104 | + target_id: e.target_id, |
| 105 | + target_title: loader.byId(e.target_id)?.title ?? e.target_id, |
| 106 | + relation: e.relation, |
| 107 | + confidence: e.confidence, |
| 108 | + })) |
| 109 | + : []; |
| 110 | + |
| 111 | + const inbound = (dir === 'inbound' || dir === 'both') |
| 112 | + ? cc.holons.flatMap(src => |
| 113 | + src.causal_edges |
| 114 | + .filter(e => e.target_id === id) |
| 115 | + .map(e => ({ |
| 116 | + source_id: src.id, |
| 117 | + source_title: src.title, |
| 118 | + relation: e.relation, |
| 119 | + confidence: e.confidence, |
| 120 | + })) |
| 121 | + ) |
| 122 | + : []; |
| 123 | + |
| 124 | + return { id, outbound, inbound }; |
| 125 | + }, |
| 126 | + }, |
| 127 | + ]; |
| 128 | +} |
0 commit comments