@@ -64,6 +64,41 @@ has_session_for_workdir() {
6464 return 1
6565}
6666
67+ get_latest_session_id () {
68+ local workdir=" $1 "
69+ local workdir_abs=$( realpath " $workdir " 2> /dev/null || echo " $workdir " )
70+ local project_dir_name=$( echo " $workdir_abs " | sed ' s|/|-|g' )
71+ local project_sessions_dir=" $HOME /.claude/projects/$project_dir_name "
72+
73+ if [ ! -d " $project_sessions_dir " ]; then
74+ return 1
75+ fi
76+
77+ local latest_session_id=" "
78+ local latest_time=0
79+
80+ for file in " $project_sessions_dir " /* .jsonl; do
81+ [ -f " $file " ] || continue
82+
83+ if grep -q ' "type":"user"' " $file " 2> /dev/null; then
84+ if grep -q ' "isSidechain":false' " $file " 2> /dev/null; then
85+ local file_time=$( stat -c %Y " $file " 2> /dev/null || stat -f %m " $file " 2> /dev/null || echo 0)
86+ if [ " $file_time " -gt " $latest_time " ]; then
87+ latest_time=$file_time
88+ latest_session_id=$( grep ' "isSidechain":false' " $file " | grep ' "sessionId"' | head -1 | grep -o ' "sessionId":"[^"]*"' | cut -d' "' -f4)
89+ fi
90+ fi
91+ fi
92+ done
93+
94+ if [ -n " $latest_session_id " ]; then
95+ echo " $latest_session_id "
96+ return 0
97+ else
98+ return 1
99+ fi
100+ }
101+
67102ARGS=()
68103
69104function start_agentapi() {
@@ -81,24 +116,39 @@ function start_agentapi() {
81116 if [ -n " $ARG_RESUME_SESSION_ID " ]; then
82117 echo " Using explicit resume_session_id: $ARG_RESUME_SESSION_ID "
83118 ARGS+=(--resume " $ARG_RESUME_SESSION_ID " )
84- if [ -n " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " ]; then
119+ if [ " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " = " true " ]; then
85120 ARGS+=(--dangerously-skip-permissions)
86121 fi
87122 elif [ " $ARG_CONTINUE " = " true" ]; then
88123 if has_session_for_workdir " $ARG_WORKDIR " ; then
89- echo " Session detected for workdir: $ARG_WORKDIR "
90- ARGS+=(--continue)
91- if [ -n " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " ]; then
92- ARGS+=(--dangerously-skip-permissions)
124+ local session_id=$( get_latest_session_id " $ARG_WORKDIR " )
125+ if [ -n " $session_id " ]; then
126+ echo " Session detected for workdir: $ARG_WORKDIR "
127+ echo " Latest session ID: $session_id "
128+ ARGS+=(--resume " $session_id " )
129+ if [ " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " = " true" ]; then
130+ ARGS+=(--dangerously-skip-permissions)
131+ fi
132+ echo " Resuming existing session with explicit session ID"
133+ else
134+ echo " Could not extract session ID, starting new session"
135+ if [ -n " $ARG_AI_PROMPT " ]; then
136+ ARGS+=(--dangerously-skip-permissions " $ARG_AI_PROMPT " )
137+ echo " Starting new session with prompt"
138+ else
139+ if [ " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " = " true" ]; then
140+ ARGS+=(--dangerously-skip-permissions)
141+ fi
142+ echo " Starting claude code session"
143+ fi
93144 fi
94- echo " Resuming existing session"
95145 else
96146 echo " No existing session for workdir: $ARG_WORKDIR "
97147 if [ -n " $ARG_AI_PROMPT " ]; then
98148 ARGS+=(--dangerously-skip-permissions " $ARG_AI_PROMPT " )
99149 echo " Starting new session with prompt"
100150 else
101- if [ -n " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " ]; then
151+ if [ " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " = " true " ]; then
102152 ARGS+=(--dangerously-skip-permissions)
103153 fi
104154 echo " Starting claude code session"
@@ -110,7 +160,7 @@ function start_agentapi() {
110160 ARGS+=(--dangerously-skip-permissions " $ARG_AI_PROMPT " )
111161 echo " Starting new session with prompt"
112162 else
113- if [ -n " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " ]; then
163+ if [ " $ARG_DANGEROUSLY_SKIP_PERMISSIONS " = " true " ]; then
114164 ARGS+=(--dangerously-skip-permissions)
115165 fi
116166 echo " Starting claude code session"
0 commit comments