-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.sh
More file actions
executable file
·78 lines (62 loc) · 2.3 KB
/
Copy pathcode.sh
File metadata and controls
executable file
·78 lines (62 loc) · 2.3 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
#!/usr/bin/env bash
# Base projects directory
PROJECTS_BASE="$HOME/Projects"
# Defaults
FOLDER_NAME=""
SESSION_ID=""
# Parse positional arguments
if [[ $# -ge 1 ]]; then
FOLDER_NAME="$1"
fi
if [[ $# -ge 2 ]]; then
SESSION_ID="$2"
fi
# Fall back to default folder if none was provided
FOLDER_NAME="${FOLDER_NAME:-fido-and-kitch}"
# Construct full project path
PROJECT_DIR="$PROJECTS_BASE/$FOLDER_NAME"
# Build the opencode command dynamically
OPENCODE_CMD="opencode"
if [[ -n "$SESSION_ID" ]]; then
OPENCODE_CMD="opencode -s $SESSION_ID"
fi
# Name of the tmux session and log location
SESSION_NAME="opencode"
LOG_FILE="$HOME/opencode-retry.log"
if [ ! -d "$PROJECT_DIR" ]; then
echo "[Error] Directory '$PROJECT_DIR' does not exist."
exit 1
fi
# Function to log messages with timestamps
log_event() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
log_event "[Info] Starting script session wrapper..."
# 1. Start tmux session in the background targeting PROJECT_DIR
if ! tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
log_event "[Info] Starting tmux session '$SESSION_NAME' in '$PROJECT_DIR' with command: '$OPENCODE_CMD'..."
tmux new-session -d -s "$SESSION_NAME" -c "$PROJECT_DIR" "$OPENCODE_CMD"
fi
# 2. Start background watcher loop
(
while tmux has-session -t "$SESSION_NAME" 2>/dev/null; do
if tmux capture-pane -pt "$SESSION_NAME" -S -10 2>/dev/null | grep -q "Streaming response failed"; then # capture last 10 lines of the pane
log_event "[DETECTED] 'Streaming response failed' string found in pane!"
log_event "[WAITING] Sleeping 5 minutes (300s) before resending continue..."
sleep 300
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
log_event "[ACTION] Sending 'continue' to tmux session..."
tmux send-keys -t "$SESSION_NAME" "continue" Enter
else
log_event "[WARN] Session died during wait. Skipping send."
fi
# Prevent immediate re-triggering while output streams
sleep 30
fi
sleep 5
done
log_event "[Info] Session '$SESSION_NAME' ended. Watcher exiting."
) &
# 3. Attach to the session
log_event "[Info] Attaching to session '$SESSION_NAME'..."
tmux attach-session -t "$SESSION_NAME"