Package Manager Replacement with Bun
This guide shows how to use Terraphim's knowledge graph and replace functionality to automatically convert package manager commands (pnpm, npm, yarn) to Bun.
Overview
The knowledge graph in docs/src/kg/bun.md defines synonyms for package managers that all map to "Bun". The terraphim-tui's /replace command uses this knowledge graph to perform text replacements.
Knowledge Graph Entry
File: docs/src/kg/bun.md
This creates a thesaurus mapping:
pnpm installβbun installnpm installβbun installyarn installβbun installpnpmβbunnpmβbunyarnβbun
Usage with TUI REPL
Start the Server
# Terminal 1: Start terraphim server
The server will build a knowledge graph from all markdown files in docs/src/kg/.
Start TUI REPL
# Terminal 2: Start TUI REPL in offline mode
Use Replace Command
# In the REPL prompt:
# Output: bun install dependencies
# Output: bun install && bun build
# Output: bun install (case insensitive)Replace Command Options
The /replace command supports different output formats:
# Plain text replacement (default)
# Output: bun install
# Markdown links
# Output: [bun install](url)
# Wiki links
# Output: [[bun install]]
# HTML links
# Output: <a href="url">bun install</a>Usage from CLI
Direct Text Replacement
# Pipe text through replace command
| Process Files
# Replace in a file
How It Works
1. Knowledge Graph Building
When terraphim starts, it:
- Scans
docs/src/kg/*.mdfiles - Extracts
synonyms::lines using Logseq builder - Creates a thesaurus mapping synonyms to normalized terms
- Builds Aho-Corasick automata for fast pattern matching
2. Replace Process
When you run /replace:
- Loads the thesaurus for the current role
- Uses Aho-Corasick to find all synonym matches in text
- Replaces matches with normalized term based on link type
- Returns the transformed text
3. Pattern Matching
The replacement is:
- Case insensitive: "pnpm" and "PNPM" both match
- Leftmost longest: Matches longest pattern first ("pnpm install" over "pnpm")
- Non-overlapping: Each position matched only once
Examples
Example 1: Convert Scripts
# Input script
#!/bin/bash
# After /replace
#!/bin/bash
Example 2: Convert package.json Scripts
After replacement:
Example 3: Convert Documentation
After replacement:
Adding More Synonyms
To add more package manager commands, edit docs/src/kg/bun.md:
Then restart the terraphim server to rebuild the knowledge graph.
Testing
Manual Testing
# Test basic replacement
# Expected: bun install
# Test multiple replacements
# Expected: bun install && bun build && bun test
# Test case insensitivity
# Expected: bun installAutomated Testing
Run the test script:
Troubleshooting
Knowledge Graph Not Loading
# Check if bun.md exists
# Verify config points to correct path
# Check server logs for KG building
# Should see: "Building knowledge graph from docs/src/kg"Replace Not Working
# Verify role is selected
# In REPL:
# Try again:
Case Sensitivity Issues
The Aho-Corasick matcher is case insensitive by default. If you need exact case matching, you would need to modify the matcher configuration in crates/terraphim_automata/src/matcher.rs.
Performance
- Pattern matching: ~10-50ms for typical text (uses Aho-Corasick)
- Knowledge graph loading: ~100-500ms on startup
- Memory usage: ~5-10MB for thesaurus automata
For large files (>1MB), consider processing in chunks.
Advanced Usage
Multiple Knowledge Graphs
You can have multiple KG files for different domains:
All will be loaded and used for replacement.
Custom Normalized Terms
In bun.md, you can customize the normalized term:
This would replace all synonyms with "Bun Install" instead of "bun".