kcode_k1_chat_thread_web_code_check_state/
lib.rs1#![forbid(unsafe_code)]
2
3use kcode_k1_chat_boxes::ToolCallId;
4use kcode_k1_chat_web_code_output::WebCodeOutput;
5use kcode_k1_web_code_ktool_service::CheckEvidence;
6
7#[derive(Clone, Default)]
8pub struct LatestCheckState {
9 state: State,
10}
11
12#[derive(Clone, Default)]
13enum State {
14 #[default]
15 Empty,
16 Successful(CheckEvidence),
17 Failed(WebCodeOutput),
18}
19
20impl LatestCheckState {
21 pub fn new() -> Self {
22 Self::default()
23 }
24
25 pub fn clear(&mut self) {
26 self.state = State::Empty;
27 }
28
29 pub fn record_success(&mut self, evidence: CheckEvidence) {
30 self.state = State::Successful(evidence);
31 }
32
33 pub fn record_failure(&mut self, output: WebCodeOutput) {
34 self.state = State::Failed(output);
35 }
36
37 pub fn successful_evidence(&self) -> Option<&CheckEvidence> {
38 match &self.state {
39 State::Successful(evidence) => Some(evidence),
40 State::Empty | State::Failed(_) => None,
41 }
42 }
43
44 pub fn failed_for_publish(&self, tool_call_id: ToolCallId) -> Option<WebCodeOutput> {
45 let State::Failed(output) = &self.state else {
46 return None;
47 };
48 let mut output = output.clone();
49 output.tool_call_id = tool_call_id;
50 Some(output)
51 }
52}