| name | mainframe-bash |
| description | Use when writing bash scripts, shell automation, CLI tools, or any bash programming task with Kimi CLI. |
Kimi CLI - Mainframe Integration
Instructions for using Mainframe (3,821+ pure bash functions) with Moonshot AI's Kimi CLI.
Overview
Mainframe provides 3,821+ pure bash functions across 152 libraries with zero external dependencies. When using Kimi CLI for bash scripting tasks, always source Mainframe first to access high-performance utilities (20-72x faster than spawning sed/awk/jq).
Installation
git clone https://github.com/gtwatts/mainframe.git ~/.mainframe
export MAINFRAME_ROOT="$HOME/.mainframe"
Configuration
Method 1: Kimi CLI Config File
Add Mainframe sourcing to your Kimi CLI configuration at ~/.kimi/config:
export MAINFRAME_ROOT="$HOME/.mainframe"
Method 2: Environment Variables
Kimi CLI inherits environment from your shell. Set these in your shell profile:
export MAINFRAME_ROOT="$HOME/.mainframe"
export PATH="$MAINFRAME_ROOT/bin:$PATH"
Method 3: Per-Session Loading
Include the source command at the start of generated scripts:
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
Available Commands
Core Library Loading
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
JSON Operations (No jq Required)
json_object "name=John" "age:number=30" "active:bool=true"
json_array "apple" "banana" "cherry"
json_get '{"name":"John","age":30}' "name"
json_keys '{"a":1,"b":2}'
json_valid '{"a":1}'
json_pretty '{"a":1,"b":2}'
String Manipulation
trim_string " hello "
to_lower "HELLO"
to_upper "hello"
replace_all "foo bar foo" "foo" "baz"
split_string "a,b,c" ","
urlencode "hello world"
pad_left "42" 5 "0"
Array Operations
arr=(5 3 1 4 2)
array_join ", " "${arr[@]}"
array_sort "${arr[@]}"
array_unique 1 2 2 3 3
array_contains "3" "${arr[@]}"
array_length "${arr[@]}"
Validation & Security
validate_email "user@domain.com"
validate_url "https://example.com"
validate_ipv4 "192.168.1.1"
validate_json '{"a":1}'
validate_path_safe "$user_path" "/allowed/base"
validate_filename "report.pdf"
sanitize_shell_arg "$user_input"
sanitize_filename "a/b<c>.txt"
sanitize_json 'say "hi"'
File Operations
read_file "$path"
file_write "$path" "content"
file_exists "$path"
dir_exists "/tmp"
file_head "$path" 10
file_tail "$path" 5
path_join "/base" "sub" "file"
Git Operations
git_branch
git_is_dirty
git_files_changed
git_commit_hash
git_summary
HTTP Requests (Pure Bash)
http_get "http://api.example.com/data"
http_post "http://api.example.com" '{"name":"test"}'
http_status
http_body
http_is_success
USOP Output with Kimi CLI
Mainframe supports Universal Structured Output Protocol (USOP) for standardized AI communication.
Enabling USOP Output
export MAINFRAME_OUTPUT_FORMAT="usop"
mainframe output usop
USOP Response Format
mainframe usop json_object \
--status "success" \
--action "file_created" \
--data '{"path":"/tmp/output.txt"}'
Parsing USOP in Kimi CLI
When Kimi CLI receives USOP-formatted output, it can automatically:
- Extract structured data
- Determine operation status
- Parse error conditions
- Handle multi-step workflows
Multi-Turn Conversation Support
Kimi CLI maintains conversation context across turns. Use Mainframe's Agent Working Memory (AWM) for persistent state:
sid=$(awm_init "kimi-task-$(date +%s)")
awm_checkpoint "current_step" "3"
awm_checkpoint "processed_files" "file1.txt,file2.txt"
awm_discovery "API rate limit is 100 req/min"
sid=$(awm_resume "$sid")
last_step=$(awm_get "current_step" "0")
Examples
Example 1: Process JSON Data
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
input='{"users":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}'
names=$(echo "$input" | json_get "" "users" | json_array_map "name")
avg_age=$(echo "$input" | json_get "" "users" | json_array_map "age" | array_avg)
json_object \
"names=$names" \
"average_age:number=$avg_age" \
"count:number=$(array_length $names)"
Example 2: Batch File Processing
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
files=("$@")
header "Processing ${#files[@]} files"
for file in "${files[@]}"; do
if validate_path_safe "$file" "$(pwd)"; then
content=$(read_file "$file")
processed=$(to_lower "$content" | replace_all "old" "new")
file_write "processed_$(path_base "$file")" "$processed"
log_info "Processed: $file"
else
log_error "Invalid path: $file"
fi
done
success "Batch complete"
Example 3: Git Workflow Automation
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
if git_is_dirty; then
branch=$(git_branch)
files=$(git_files_changed)
header "Committing to $branch"
log_info "Files changed: $(array_length $files)"
msg="Update: $(array_join ', ' $files)"
git add .
git commit -m "$msg"
success "Committed: $msg"
else
log_info "No changes to commit"
fi
Example 4: API Client with Error Handling
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
response=$(http_json_get "https://api.example.com/data")
if http_is_success; then
count=$(echo "$response" | json_get "" "count")
items=$(echo "$response" | json_get "" "items")
json_object \
"status=success" \
"count:number=$count" \
"items:raw=$items"
else
json_object \
"status=error" \
"code:number=$(http_status)" \
"message=Request failed"
fi
Example 5: Multi-Agent Coordination
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
agent_register "kimi-session-1" code.analyze typescript
analyzers=$(agent_discover "code.analyze")
log_info "Found analyzers: $analyzers"
agent_send "claude-reviewer-1" '{
"task": "code_review",
"file": "src/main.ts",
"priority": "high"
}'
response=$(agent_receive 60)
log_info "Received: $response"
agent_unregister
Quick Reference
mainframe quickref
mainframe quickref --search json
mainframe help json_object
Kimi CLI Specific Notes
- Context Window: Kimi CLI has large context - you can include substantial Mainframe function references
- Environment Inheritance: Variables set in
~/.kimi/config are available in sessions
- Output Parsing: Use USOP format for structured data exchange
- Long-Running Tasks: Combine with AWM for persistence across context boundaries
Reference Files
~/.mainframe/CHEATSHEET.md - All 3,821+ function signatures
~/.mainframe/FUNCTIONS.json - Machine-readable function index
~/.mainframe/DECISION_TREES.md - "I need X" workflow guidance
~/.mainframe/docs/ORCHESTRATION.md - Multi-agent coordination
Repository