-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtk-run.sh
More file actions
executable file
·162 lines (143 loc) · 5.92 KB
/
Copy pathrtk-run.sh
File metadata and controls
executable file
·162 lines (143 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# -----------------------------------------------------------------------------
# Script: rtk-run.sh
# Description: Enhances the global 'rtk' (Run Tool Kit) binary with 'run' and
# 'list' commands by parsing 'rtk.tools' from package.json.
# Supports both project-local and Tech-Lead Stack global tools.
#
# Usage:
# rtk run <tool_name> [args...] - Executes a defined tool
# rtk list - Lists all available tools
#
# Requirements:
# - Node.js (used for JSON parsing and path resolution)
# - rtk global binary
# -----------------------------------------------------------------------------
# Use BASH_SOURCE to reliably find the script's directory even when called via alias/symlink
SCRIPT_PATH="${BASH_SOURCE[0]}"
if [ -z "$SCRIPT_PATH" ]; then
SCRIPT_PATH=$(realpath "$0" 2>/dev/null || echo "$0")
fi
SCRIPT_DIR=$(cd "$(dirname "$SCRIPT_PATH")" && pwd)
STACK_PKG="$SCRIPT_DIR/../package.json"
# --- Compliance Check (Phase 0 Verification) ---
# Enforce mission alignment before allowing tool execution to ensure telemetry integrity.
CHECK_FILE=".ai/.mission-alignment.json"
if [[ ! -f "$CHECK_FILE" ]]; then
echo "🛑 COMPLIANCE ERROR: Mission Alignment Missing."
echo "----------------------------------------------------------------------"
echo "You are attempting to execute a Tech-Lead Stack tool without proper"
echo "Phase 0 Skill Acquisition and Mission Alignment."
echo ""
echo "👉 MANDATORY ACTION: "
echo "You MUST call the MCP tool 'verify_mission_alignment' before any"
echo "further actions. This ensures that you have read 'operational-boundaries.md'"
echo "and your turn is correctly instrumented for telemetry."
echo ""
echo "Note: Direct file access to .ai/skills/ is strictly FORBIDDEN."
echo "----------------------------------------------------------------------"
exit 1
fi
# --- Command: rtk run <tool_name> ---
if [[ "$1" == "run" ]]; then
TOOL_NAME=$2
# Help handling
if [[ "$TOOL_NAME" == "--help" || "$TOOL_NAME" == "-h" ]]; then
echo "📖 RTK Tool Runner"
echo "Usage: rtk run <tool_name> [args...]"
echo ""
echo "Execute a tool from 'rtk.tools' (checks project and Tech-Lead Stack)."
echo "Use 'rtk list' to see all available tools."
exit 0
fi
# Validation
if [[ -z "$TOOL_NAME" ]]; then
echo "❌ Error: Please specify a tool name. Use 'rtk list' to see available tools."
exit 1
fi
# Logic: Extract command from package.json using Node.js
# Priority:
# 1. Current working directory package.json (Project-local tools)
# 2. Tech-Lead Stack package.json (Global framework tools)
CMD=$(node -e "
let toolCmd;
let isFromStack = false;
// 1. Check local project
try {
const pkg = require(process.cwd() + '/package.json');
toolCmd = pkg.rtk && pkg.rtk.tools && pkg.rtk.tools['$TOOL_NAME'];
} catch (e) {}
// 2. Check Tech-Lead Stack (Fallback)
if (!toolCmd) {
try {
const stackPkg = require('$STACK_PKG');
toolCmd = stackPkg.rtk && stackPkg.rtk.tools && stackPkg.rtk.tools['$TOOL_NAME'];
if (toolCmd) isFromStack = true;
} catch (e) {}
}
if (toolCmd) {
// Path Resolution Strategy:
// If the tool is from the Stack, we must resolve its relative paths
// (like .scripts/ or templates/) to absolute paths relative to the Stack root.
if (isFromStack) {
const tlsRoot = require('path').resolve('$SCRIPT_DIR', '..');
toolCmd = toolCmd.replace(/(^|\s)(\.ai\/|\.agents\/|\.?scripts\/|templates\/)/g, (match, prefix, path) => {
return prefix + tlsRoot + '/' + path;
});
}
console.log(toolCmd);
} else {
process.exit(1);
}
" 2>/dev/null)
if [[ $? -ne 0 || -z "$CMD" ]]; then
echo "❌ Diagnosis: Tool '$TOOL_NAME' not found in package.json 'rtk.tools' section."
echo "👉 Action: Check 'rtk list' for available commands or add it to your project root package.json."
exit 1
fi
# Execution Phase
echo "🚀 Executing RTK tool: $TOOL_NAME"
# Argument handling: Pass everything after the tool name
TOOL_ARGS="${@:3}"
# Optimization: Use 'eval' for simple 'cat' commands to avoid subshell overhead,
# otherwise use 'rtk sh -c' to benefit from rtk's token-saving output compression.
if [[ "$CMD" == cat* ]]; then
eval "$CMD $TOOL_ARGS"
else
rtk sh -c "$CMD $TOOL_ARGS"
fi
# --- Command: rtk list ---
elif [[ "$1" == "list" ]]; then
echo "📋 Available Agent Skills & Tools:"
# Logic: Merge and display tools from both local and stack package.json
node -e "
let tools = {};
// Load Stack tools
try {
const stackPkg = require('$STACK_PKG');
if (stackPkg.rtk && stackPkg.rtk.tools) {
Object.assign(tools, stackPkg.rtk.tools);
}
} catch (e) {}
// Load and override with local project tools
try {
const localPkg = require(process.cwd() + '/package.json');
if (localPkg.rtk && localPkg.rtk.tools) {
Object.assign(tools, localPkg.rtk.tools);
}
} catch (e) {}
// Pretty print results
if (Object.keys(tools).length > 0) {
Object.entries(tools).forEach(([name, cmd]) => {
console.log(' - ' + name.padEnd(15) + ' -> ' + cmd);
});
} else {
console.log(' (No tools defined in package.json)');
}
" 2>/dev/null
# --- Proxy: All other rtk commands ---
else
# Allow rtk-run.sh to act as a transparent wrapper for other rtk commands
# (e.g., rtk chat, rtk help)
rtk "$@"
fi