Skip to content

Commit c4c1694

Browse files
fix: update variable names in tests and enhance session handling in start script
1 parent e34320c commit c4c1694

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

registry/coder/modules/claude-code/main.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe("claude-code", async () => {
167167
const { id } = await setup({
168168
moduleVariables: {
169169
permission_mode: mode,
170-
task_prompt: "test prompt",
170+
ai_prompt: "test prompt",
171171
},
172172
});
173173
await execModuleScript(id);
@@ -185,7 +185,7 @@ describe("claude-code", async () => {
185185
const { id } = await setup({
186186
moduleVariables: {
187187
model: model,
188-
task_prompt: "test prompt",
188+
ai_prompt: "test prompt",
189189
},
190190
});
191191
await execModuleScript(id);
@@ -202,7 +202,7 @@ describe("claude-code", async () => {
202202
const { id } = await setup({
203203
moduleVariables: {
204204
continue: "true",
205-
task_prompt: "test prompt",
205+
ai_prompt: "test prompt",
206206
},
207207
});
208208
await execModuleScript(id);

registry/coder/modules/claude-code/scripts/start.sh

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ function validate_claude_installation() {
4444
fi
4545
}
4646

47+
has_session_for_workdir() {
48+
local workdir="$1"
49+
local workdir_abs=$(realpath "$workdir" 2> /dev/null || echo "$workdir")
50+
51+
if [ -f "$HOME/.claude.json" ]; then
52+
if jq -e ".projects[\"$workdir_abs\"]" "$HOME/.claude.json" > /dev/null 2>&1; then
53+
return 0
54+
fi
55+
fi
56+
57+
local project_dir_name=$(echo "$workdir_abs" | sed 's|/|-|g')
58+
local project_sessions_dir="$HOME/.claude/projects/$project_dir_name"
59+
60+
if [ -d "$project_sessions_dir" ] && [ -n "$(ls -A "$project_sessions_dir" 2> /dev/null)" ]; then
61+
return 0
62+
fi
63+
64+
return 1
65+
}
66+
4767
ARGS=()
4868

4969
function build_claude_args() {
@@ -68,13 +88,43 @@ function build_claude_args() {
6888
function start_agentapi() {
6989
mkdir -p "$ARG_WORKDIR"
7090
cd "$ARG_WORKDIR"
71-
if [ -n "$ARG_AI_PROMPT" ]; then
72-
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
73-
else
91+
92+
if [ -n "$ARG_RESUME_SESSION_ID" ]; then
93+
echo "Using explicit resume_session_id: $ARG_RESUME_SESSION_ID"
94+
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
95+
ARGS+=(--dangerously-skip-permissions)
96+
fi
97+
elif [ "$ARG_CONTINUE" = "true" ]; then
98+
echo "Using explicit continue flag"
7499
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
75100
ARGS+=(--dangerously-skip-permissions)
76101
fi
102+
else
103+
local has_existing_session=false
104+
if has_session_for_workdir "$ARG_WORKDIR"; then
105+
has_existing_session=true
106+
echo "Session detected for workdir: $ARG_WORKDIR"
107+
else
108+
echo "No existing session for workdir: $ARG_WORKDIR"
109+
fi
110+
111+
if [ -n "$ARG_AI_PROMPT" ] && [ "$has_existing_session" = "false" ]; then
112+
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
113+
echo "Starting new session with prompt"
114+
elif [ "$has_existing_session" = "true" ]; then
115+
ARGS+=(--continue)
116+
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
117+
ARGS+=(--dangerously-skip-permissions)
118+
fi
119+
echo "Resuming existing session"
120+
else
121+
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
122+
ARGS+=(--dangerously-skip-permissions)
123+
fi
124+
echo "Starting claude code session"
125+
fi
77126
fi
127+
78128
printf "Running claude code with args: %s\n" "$(printf '%q ' "${ARGS[@]}")"
79129
agentapi server --type claude --term-width 67 --term-height 1190 -- claude "${ARGS[@]}"
80130
}

0 commit comments

Comments
 (0)