Accept story 39: Persistent Claude Code Sessions in Web UI

Use --resume <session_id> with claude -p so the web UI claude-code-pty
provider maintains full conversation context across messages, identical
to a long-running terminal Claude Code session.

Changes:
- Capture session_id from claude -p stream-json system event
- Pass --resume on subsequent messages in same chat session
- Thread session_id through ProviderConfig, ChatResult, WsResponse
- Frontend stores sessionId per chat, clears on New Session
- Unset CLAUDECODE env to allow nested spawning from server
- Wait for clean process exit to ensure transcript flush to disk

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dave
2026-02-20 11:51:19 +00:00
parent cff7f5fe7f
commit cde75bd7fb
11 changed files with 9524 additions and 61 deletions

View File

@@ -14,6 +14,16 @@ pub struct ProviderConfig {
pub model: String,
pub base_url: Option<String>,
pub enable_tools: Option<bool>,
/// Claude Code session ID for conversation resumption.
pub session_id: Option<String>,
}
/// Result of a chat call, including messages and optional metadata.
#[allow(dead_code)]
pub struct ChatResult {
pub messages: Vec<Message>,
/// Session ID returned by Claude Code for resumption.
pub session_id: Option<String>,
}
fn get_anthropic_api_key_exists_impl(store: &dyn StoreOps) -> bool {
@@ -172,7 +182,7 @@ pub async fn chat<F, U>(
store: &dyn StoreOps,
mut on_update: F,
mut on_token: U,
) -> Result<Vec<Message>, String>
) -> Result<ChatResult, String>
where
F: FnMut(&[Message]) + Send,
U: FnMut(&str) + Send,
@@ -219,6 +229,7 @@ where
.chat_stream(
&user_message,
&project_root.to_string_lossy(),
config.session_id.as_deref(),
&mut cancel_rx,
|token| on_token(token),
)
@@ -235,7 +246,10 @@ where
let mut result = messages.clone();
result.push(assistant_msg);
on_update(&result);
return Ok(result);
return Ok(ChatResult {
messages: result,
session_id: response.session_id,
});
}
let tool_defs = get_tool_definitions();
@@ -353,7 +367,10 @@ where
}
}
Ok(new_messages)
Ok(ChatResult {
messages: new_messages,
session_id: None,
})
}
async fn execute_tool(call: &ToolCall, state: &SessionState) -> String {