-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatecode.sh
More file actions
executable file
·284 lines (247 loc) · 7.94 KB
/
matecode.sh
File metadata and controls
executable file
·284 lines (247 loc) · 7.94 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# =============================================================================
# MateCode - Claude Code Telegram Bridge
# 一键启动脚本
# =============================================================================
set -e
# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Configuration
TMUX_SESSION="claude"
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOKS_DIR="$HOME/.claude/hooks"
SETTINGS_FILE="$HOME/.claude/settings.json"
print_success() { echo -e "${GREEN}[✓]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[!]${NC} $1"; }
print_error() { echo -e "${RED}[✗]${NC} $1"; }
print_info() { echo -e "${YELLOW}[→]${NC} $1"; }
check_prerequisites() {
print_info "检查环境依赖..."
local missing_deps=()
if ! command -v tmux &>/dev/null; then missing_deps+=("tmux"); fi
if ! command -v python3 &>/dev/null; then missing_deps+=("python3"); fi
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "缺少依赖: ${missing_deps[*]}"
echo "请安装: tmux, python3"
exit 1
fi
print_success "所有依赖已满足"
}
setup_directories() {
print_info "创建必要目录..."
mkdir -p "$HOOKS_DIR"
mkdir -p "$HOME/.matecode"
print_success "目录已创建"
}
setup_claude_hooks() {
print_info "配置 Claude 钩子..."
mkdir -p "$HOOKS_DIR"
if [ -f "$PROJECT_DIR/hooks/send-to-telegram.sh" ]; then
cp "$PROJECT_DIR/hooks/send-to-telegram.sh" "$HOOKS_DIR/"
chmod +x "$HOOKS_DIR/send-to-telegram.sh"
print_success "钩子脚本已安装"
else
print_error "钩子脚本不存在"
exit 1
fi
# Update bot token in hook script
if [ -n "$TELEGRAM_BOT_TOKEN" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/TELEGRAM_BOT_TOKEN=.*/TELEGRAM_BOT_TOKEN=\"$TELEGRAM_BOT_TOKEN\"/" "$HOOKS_DIR/send-to-telegram.sh"
else
sed -i "s/TELEGRAM_BOT_TOKEN=.*/TELEGRAM_BOT_TOKEN=\"$TELEGRAM_BOT_TOKEN\"/" "$HOOKS_DIR/send-to-telegram.sh"
fi
print_success "Bot token 已配置"
fi
# Create settings.json if not exists
if [ ! -f "$SETTINGS_FILE" ]; then
mkdir -p "$(dirname "$SETTINGS_FILE")"
cat > "$SETTINGS_FILE" << EOF
{
"hooks": {
"Stop": [{"hooks": [{"type": "command", "command": "$HOOKS_DIR/send-to-telegram.sh"}]}]
}
}
EOF
print_success "Claude 配置文件已创建"
fi
}
start_tmux_claude() {
print_info "启动 tmux + Claude Code..."
if tmux has-session -t "$TMUX_SESSION" 2>/dev/null; then
print_success "tmux 会话 '$TMUX_SESSION' 已存在"
else
tmux new-session -d -s "$TMUX_SESSION"
sleep 1
tmux send-keys -t "$TMUX_SESSION" "~/.opencode/bin/opencode" C-m
sleep 2
print_success "Claude Code 已启动"
fi
}
start_bridge() {
print_info "启动 Bridge..."
# Register bot commands
if [ -n "$TELEGRAM_BOT_TOKEN" ]; then
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setMyCommands" \
-H "Content-Type: application/json" \
-d @- <<EOF >/dev/null 2>&1
{
"commands": [
{"command": "clear", "description": "清空对话"},
{"command": "resume", "description": "恢复会话"},
{"command": "continue_", "description": "继续最近会话"},
{"command": "loop", "description": "Ralph 循环"},
{"command": "stop", "description": "中断 Claude"},
{"command": "status", "description": "检查 tmux 状态"},
{"command": "remember", "description": "保存到记忆"},
{"command": "recall", "description": "搜索记忆"},
{"command": "forget", "description": "删除记忆"}
]
}
EOF
print_success "Bot 命令已注册"
fi
# Start bridge
nohup python3 "$PROJECT_DIR/bridge.py" >"$PROJECT_DIR/bridge.log" 2>&1 &
BRIDGE_PID=$!
echo $BRIDGE_PID > "$PROJECT_DIR/bridge.pid"
sleep 2
if kill -0 $BRIDGE_PID 2>/dev/null; then
print_success "Bridge 已启动 (PID: $BRIDGE_PID)"
else
print_error "Bridge 启动失败"
exit 1
fi
}
show_status() {
echo ""
echo "==================================="
echo " 服务状态"
echo "==================================="
if tmux has-session -t "$TMUX_SESSION" 2>/dev/null; then
print_success "tmux 会话: 运行中"
else
print_error "tmux 会话: 未运行"
fi
if [ -f "$PROJECT_DIR/bridge.pid" ] && kill -0 "$(cat "$PROJECT_DIR/bridge.pid")" 2>/dev/null; then
print_success "Bridge: 运行中 (PID: $(cat "$PROJECT_DIR/bridge.pid"))"
else
print_error "Bridge: 未运行"
fi
}
stop_services() {
print_info "停止服务..."
if [ -f "$PROJECT_DIR/bridge.pid" ]; then
BRIDGE_PID=$(cat "$PROJECT_DIR/bridge.pid")
if kill -0 "$BRIDGE_PID" 2>/dev/null; then
kill "$BRIDGE_PID" 2>/dev/null
print_success "Bridge 已停止"
fi
rm -f "$PROJECT_DIR/bridge.pid"
pkill -f "bridge\.py|bridge-polling\.py"
fi
rm -f ~/.claude/telegram_pending
rm -f ~/.claude/telegram_chat_id
if tmux has-session -t "$TMUX_SESSION" 2>/dev/null; then
print_warning "tmux 会话仍然运行 (如需关闭: tmux kill-session -t $TMUX_SESSION)"
fi
}
show_logs() {
echo "=== Bridge 日志 ==="
if [ -f "$PROJECT_DIR/bridge.log" ]; then
tail -n 20 "$PROJECT_DIR/bridge.log"
else
print_warning "bridge.log 不存在"
fi
}
usage() {
cat << EOF
使用方法: ./matecode.sh [命令]
命令:
start 启动服务 (默认)
stop 停止服务
restart 重启服务
status 显示状态
logs 查看日志
--help 显示帮助
环境变量:
TELEGRAM_BOT_TOKEN Telegram Bot Token (必需)
MEMORY_ENABLED 启用记忆功能 (默认: true)
MEMORY_MAX_RESULTS 每次查询最大记忆数 (默认: 5)
示例:
export TELEGRAM_BOT_TOKEN="your_token"
./matecode.sh start
EOF
}
main() {
echo ""
echo "=========================================="
echo " MateCode - Claude Telegram Bridge"
echo "=========================================="
echo ""
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
print_error "TELEGRAM_BOT_TOKEN 未设置"
echo "请先设置: export TELEGRAM_BOT_TOKEN='your_token'"
exit 1
fi
case "${1:-start}" in
start)
check_prerequisites
setup_directories
setup_claude_hooks
start_tmux_claude
start_bridge
show_status
;;
stop)
stop_services
;;
restart)
stop_services
sleep 2
check_prerequisites
setup_directories
setup_claude_hooks
start_tmux_claude
start_bridge
show_status
;;
status)
show_status
;;
logs)
show_logs
;;
--help|-h)
usage
;;
*)
print_error "未知命令: $1"
usage
exit 1
;;
esac
echo ""
echo "=========================================="
if [ "${1:-start}" = "start" ] || [ "${1:-start}" = "restart" ]; then
echo ""
print_info "启动完成!在 Telegram 中给 bot 发送消息即可使用"
echo ""
echo "常用命令:"
echo " /clear - 清空对话"
echo " /continue_ - 继续最近会话"
echo " /resume - 选择会话恢复"
echo " /status - 检查状态"
echo " /remember - 保存到记忆 (例: /remember 我喜欢Python)"
echo " /recall - 搜索记忆 (例: /recall Python)"
echo " /forget - 删除记忆 (例: /forget all 或 /forget Python)"
echo ""
# Attach to tmux session
tmux attach-session -t "$TMUX_SESSION"
fi
echo ""
}
main "$@"