-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSimplifier.sh
More file actions
executable file
·161 lines (137 loc) · 3.71 KB
/
CodeSimplifier.sh
File metadata and controls
executable file
·161 lines (137 loc) · 3.71 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
#!/usr/bin/env bash
set -euo pipefail
AGENT_NAME="CodeSimplifier"
COMMAND_NAME="simplify"
MODEL="${CODESIMPLIFIER_MODEL:-openai/gpt-5.3-codex}"
CONFIG_DIR="${OPENCODE_CONFIG_DIR:-${HOME}/.config/opencode}"
AGENT_DIR="${CONFIG_DIR}/agents"
COMMAND_DIR="${CONFIG_DIR}/commands"
LEGACY_COMMAND_DIR="${CONFIG_DIR}/command"
AGENT_FILE="${AGENT_DIR}/${AGENT_NAME}.md"
COMMAND_FILE="${COMMAND_DIR}/${COMMAND_NAME}.md"
LEGACY_COMMAND_FILE="${LEGACY_COMMAND_DIR}/${COMMAND_NAME}.md"
usage() {
cat <<'EOF'
Usage:
CodeSimplifier.sh [install]
CodeSimplifier.sh uninstall
CodeSimplifier.sh --uninstall
Description:
Installs or removes a global OpenCode CodeSimplifier setup:
- Subagent: @CodeSimplifier
- Command: /simplify (subtask routed to CodeSimplifier)
Environment variables:
OPENCODE_CONFIG_DIR Override ~/.config/opencode target directory
CODESIMPLIFIER_MODEL Override model (default: openai/gpt-5.3-codex)
EOF
}
write_agent_file() {
mkdir -p "$AGENT_DIR"
cat > "$AGENT_FILE" <<EOF
---
description: Finds overly verbose and complex code, then proposes practical simplifications
mode: subagent
model: ${MODEL}
temperature: 0.1
tools:
write: false
edit: false
permission:
edit: deny
webfetch: deny
bash: deny
---
You are a read-only code simplification specialist.
Focus on:
- overly verbose implementations
- unnecessary abstraction or indirection
- deeply nested or branch-heavy logic
- avoidable mutation and side-effect-heavy flows
- repetitive logic that can be composed cleanly
Workflow:
1. Inspect existing project conventions and utilities before suggesting changes.
2. Prioritize simplifications with high readability impact and low behavior risk.
3. Preserve behavior: call out risk when a simplification may alter semantics.
4. Give concrete findings with file paths, severity, and minimal remediation steps.
5. Use concise before/after guidance for each recommendation.
Output format:
- Critical simplifications (high impact, high urgency)
- High-value simplifications
- Optional polish
EOF
}
write_command_files() {
mkdir -p "$COMMAND_DIR"
cat > "$COMMAND_FILE" <<'EOF'
---
description: Find code simplification opportunities
agent: CodeSimplifier
subtask: true
---
Run a focused simplification review.
Scope: $ARGUMENTS
If scope is empty, review the current repository for highest-impact simplification opportunities.
Look for:
- over-verbose code
- complex control flow
- unnecessary abstractions
- repetitive logic
- avoidable mutation or side effects
Return:
1) top simplification opportunities (ranked)
2) low-risk remediation steps
3) optional follow-up refactors
EOF
if [[ -d "$LEGACY_COMMAND_DIR" ]]; then
cp "$COMMAND_FILE" "$LEGACY_COMMAND_FILE"
fi
}
install() {
write_agent_file
write_command_files
echo "Installed @${AGENT_NAME} and /${COMMAND_NAME}"
echo "Config directory: ${CONFIG_DIR}"
echo "Agent file: ${AGENT_FILE}"
echo "Command file: ${COMMAND_FILE}"
}
cleanup_dir_if_empty() {
local path="$1"
if [[ -d "$path" ]] && [[ -z "$(ls -A "$path")" ]]; then
rmdir "$path"
fi
}
uninstall() {
rm -f "$AGENT_FILE" "$COMMAND_FILE" "$LEGACY_COMMAND_FILE"
cleanup_dir_if_empty "$AGENT_DIR"
cleanup_dir_if_empty "$COMMAND_DIR"
echo "Removed @${AGENT_NAME} and /${COMMAND_NAME}"
echo "Config directory: ${CONFIG_DIR}"
}
main() {
local action="install"
if [[ $# -gt 0 ]]; then
case "$1" in
install)
action="install"
;;
uninstall|--uninstall|-u)
action="uninstall"
;;
-h|--help|help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
fi
if [[ "$action" == "install" ]]; then
install
else
uninstall
fi
}
main "$@"