Skip to content

Commit bf32d80

Browse files
committed
feat: Optimize the tool use message logic
1 parent a30daef commit bf32d80

1 file changed

Lines changed: 71 additions & 11 deletions

File tree

src/services/ws/stable/claude.rs

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl RunSessionState {
360360
is_pending,
361361
} => {
362362
if is_pending {
363-
for tool in request {
363+
for (i, tool) in request.into_iter().enumerate() {
364364
if tool.done {
365365
continue;
366366
}
@@ -386,6 +386,13 @@ impl RunSessionState {
386386
);
387387
SendStateError::ClaudeError
388388
})?;
389+
if let cc_session::ClaudeCodeState::PreUseTool { request, .. } =
390+
&mut self.cc_session.state
391+
{
392+
request[i].submited = true;
393+
}
394+
395+
break;
389396
}
390397
Err(e) => {
391398
log::warn!(
@@ -400,8 +407,12 @@ impl RunSessionState {
400407
}
401408
}
402409
}
403-
cc_session::ClaudeCodeState::StopUseTool => {
404-
let _ = self.send_display(&"Tool use stopped.").await;
410+
cc_session::ClaudeCodeState::StopUseTool { is_error } => {
411+
if is_error {
412+
let _ = self.send_display(&"Tool use stopped with error.").await;
413+
} else {
414+
let _ = self.send_display(&"Tool use completed successfully.").await;
415+
}
405416
self.session.send_end_response().map_err(|e| {
406417
log::error!(
407418
"{}:{:x} error sending end response after tool use stop: {}",
@@ -511,6 +522,51 @@ impl RunSessionState {
511522
}
512523
}
513524

525+
fn update_state(
526+
cc_state: &mut cc_session::ClaudeCodeState,
527+
new_state: cc_session::ClaudeCodeState,
528+
) -> bool {
529+
match (cc_state, new_state) {
530+
(
531+
cc_session::ClaudeCodeState::PreUseTool {
532+
request,
533+
is_pending,
534+
},
535+
cc_session::ClaudeCodeState::PreUseTool {
536+
request: new_request,
537+
is_pending: new_is_pending,
538+
},
539+
) => {
540+
if *is_pending != new_is_pending {
541+
*request = new_request;
542+
*is_pending = new_is_pending;
543+
return true;
544+
}
545+
546+
if request.len() != new_request.len() {
547+
*request = new_request;
548+
return true;
549+
}
550+
551+
for (r, nr) in request.iter_mut().zip(new_request.into_iter()) {
552+
if r.submited && !nr.done {
553+
log::debug!("Received PreUseTool state without done tool after submited");
554+
return false;
555+
}
556+
*r = nr;
557+
}
558+
return true;
559+
}
560+
(cc_state, new_state) => {
561+
if cc_state != &new_state {
562+
*cc_state = new_state;
563+
return true;
564+
}
565+
}
566+
}
567+
false
568+
}
569+
514570
async fn run_session(
515571
id: uuid::Uuid,
516572
url: &str,
@@ -577,15 +633,15 @@ async fn run_session(
577633
session_id: _,
578634
current_state,
579635
} => {
580-
if current_state == run_session_state.cc_session.state {
581-
log::debug!(
582-
"Claude session {} state unchanged: {:?}",
583-
id,
584-
current_state
585-
);
636+
log::debug!(
637+
"Claude session {} received state update: {:?}",
638+
id,
639+
current_state
640+
);
641+
if !update_state(&mut run_session_state.cc_session.state, current_state) {
642+
log::debug!("Claude session {} state unchanged after update", id,);
586643
continue;
587644
}
588-
run_session_state.cc_session.state = current_state;
589645
}
590646
WsOutputMessage::SessionError { session_id, code } => {
591647
log::error!(
@@ -835,6 +891,8 @@ mod cc_session {
835891
pub name: String,
836892
pub input: serde_json::Value,
837893
pub done: bool,
894+
#[serde(default)]
895+
pub submited: bool,
838896
}
839897

840898
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -848,7 +906,9 @@ mod cc_session {
848906
output: String,
849907
is_thinking: bool,
850908
},
851-
StopUseTool,
909+
StopUseTool {
910+
is_error: bool,
911+
},
852912
Idle,
853913
}
854914

0 commit comments

Comments
 (0)