fix(claude-cli): --resume for continuing turns (reusing --session-id errored on turn 2)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -59,6 +59,7 @@ import { createInterface } from 'readline';
|
|||||||
* @param {function} [opts.onEvent] Called for each normalized event
|
* @param {function} [opts.onEvent] Called for each normalized event
|
||||||
* @param {string} [opts.claudeExe] Path or name of claude binary (default: CLAUDE_EXE env or 'claude')
|
* @param {string} [opts.claudeExe] Path or name of claude binary (default: CLAUDE_EXE env or 'claude')
|
||||||
* @param {string[]} [opts.tools] Exclusive available-tools allowlist (--tools); removes built-ins
|
* @param {string[]} [opts.tools] Exclusive available-tools allowlist (--tools); removes built-ins
|
||||||
|
* @param {boolean} [opts.resume] Continue an existing session (--resume) vs create (--session-id)
|
||||||
* @param {string} [opts.home] If set, overrides HOME in child env (for service-user creds)
|
* @param {string} [opts.home] If set, overrides HOME in child env (for service-user creds)
|
||||||
* @param {string} [opts.cwd] Working directory for the child process
|
* @param {string} [opts.cwd] Working directory for the child process
|
||||||
* @param {number} [opts.timeoutMs] Milliseconds before SIGTERM (default: 600000)
|
* @param {number} [opts.timeoutMs] Milliseconds before SIGTERM (default: 600000)
|
||||||
@@ -73,6 +74,7 @@ export async function runClaudeTurn(opts) {
|
|||||||
mcpConfigPath,
|
mcpConfigPath,
|
||||||
allowedTools = [],
|
allowedTools = [],
|
||||||
tools = [],
|
tools = [],
|
||||||
|
resume = false,
|
||||||
onEvent,
|
onEvent,
|
||||||
claudeExe = process.env.CLAUDE_EXE || 'claude',
|
claudeExe = process.env.CLAUDE_EXE || 'claude',
|
||||||
home = process.env.VOID_CLAUDE_HOME,
|
home = process.env.VOID_CLAUDE_HOME,
|
||||||
@@ -89,7 +91,9 @@ export async function runClaudeTurn(opts) {
|
|||||||
'--verbose',
|
'--verbose',
|
||||||
'--include-partial-messages',
|
'--include-partial-messages',
|
||||||
'--append-system-prompt', systemPrompt,
|
'--append-system-prompt', systemPrompt,
|
||||||
'--session-id', sessionId,
|
// First turn creates the session (--session-id); later turns MUST continue
|
||||||
|
// it with --resume (reusing --session-id on an existing session errors).
|
||||||
|
...(resume ? ['--resume', sessionId] : ['--session-id', sessionId]),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (mcpConfigPath) {
|
if (mcpConfigPath) {
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ spacesScopedRouter.post('/turn',
|
|||||||
const { agent, convo } = await resolveConversation(req.params.space_id);
|
const { agent, convo } = await resolveConversation(req.params.space_id);
|
||||||
const { text, view } = req.body;
|
const { text, view } = req.body;
|
||||||
|
|
||||||
|
// Resume the claude session if this conversation already had turns (the CLI
|
||||||
|
// keys session history by --session-id; first turn creates it, rest --resume).
|
||||||
|
const priorTurns = (await messages.listByConversation(convo.id)).length;
|
||||||
|
const resume = priorTurns > 0;
|
||||||
|
|
||||||
await messages.append(convo.id, { role: 'user', body: text });
|
await messages.append(convo.id, { role: 'user', body: text });
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
@@ -105,6 +110,7 @@ spacesScopedRouter.post('/turn',
|
|||||||
try {
|
try {
|
||||||
result = await runClaudeTurn({
|
result = await runClaudeTurn({
|
||||||
sessionId: convo.id,
|
sessionId: convo.id,
|
||||||
|
resume,
|
||||||
systemPrompt: SYSTEM,
|
systemPrompt: SYSTEM,
|
||||||
userText: text,
|
userText: text,
|
||||||
mcpConfigPath,
|
mcpConfigPath,
|
||||||
|
|||||||
Reference in New Issue
Block a user