mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
fix: stabilize todo state and responses streams
This commit is contained in:
1 parent
95fb9be967
commit
eb26b17ba0
3 files changed
+177
-11
No files matched your search
@@ -27,7 +27,9 @@ pub fn fold_derived_state(messages: &[CanonicalMessage]) -> DerivedState {
|
||||
continue;
|
||||
};
|
||||
match normalize(&name).as_str() {
|
||||
"todowrite" | "updatetodos" => state.todos = Some(input),
|
||||
"todowrite" | "updatetodos" => {
|
||||
state.todos = Some(apply_todo_write(state.todos.take(), input));
|
||||
}
|
||||
"createplan" | "updateplan" | "writeplan" => state.plan = Some(input),
|
||||
_ => {}
|
||||
}
|
||||
@@ -38,6 +40,39 @@ pub fn fold_derived_state(messages: &[CanonicalMessage]) -> DerivedState {
|
||||
state
|
||||
}
|
||||
|
||||
fn apply_todo_write(current: Option<Value>, mut input: Value) -> Value {
|
||||
if !input.get("merge").and_then(Value::as_bool).unwrap_or(false) {
|
||||
return input;
|
||||
}
|
||||
let mut todos = current
|
||||
.as_ref()
|
||||
.and_then(|value| value.get("todos"))
|
||||
.and_then(Value::as_array)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let patches = input
|
||||
.get("todos")
|
||||
.and_then(Value::as_array)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
for patch in patches {
|
||||
let existing = patch.get("id").and_then(Value::as_str).and_then(|id| {
|
||||
todos
|
||||
.iter_mut()
|
||||
.find(|todo| todo.get("id").and_then(Value::as_str) == Some(id))
|
||||
});
|
||||
match (existing, patch) {
|
||||
(Some(Value::Object(todo)), Value::Object(patch)) => todo.extend(patch),
|
||||
(_, patch) => todos.push(patch),
|
||||
}
|
||||
}
|
||||
if let Some(object) = input.as_object_mut() {
|
||||
object.insert("merge".into(), Value::Bool(false));
|
||||
object.insert("todos".into(), Value::Array(todos));
|
||||
}
|
||||
input
|
||||
}
|
||||
|
||||
fn normalize(value: &str) -> String {
|
||||
value
|
||||
.chars()
|
||||
@@ -45,3 +80,87 @@ fn normalize(value: &str) -> String {
|
||||
.flat_map(char::to_lowercase)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::{Origin, Role, ToolCallContent, ToolResultContent};
|
||||
|
||||
#[test]
|
||||
fn todo_write_merge_materializes_complete_existing_items_and_appends_new_ids() {
|
||||
let messages = vec![
|
||||
assistant_call(
|
||||
"create",
|
||||
serde_json::json!({
|
||||
"merge": false,
|
||||
"todos": [
|
||||
{"id": "first", "content": "First", "status": "in_progress"},
|
||||
{"id": "second", "content": "Second", "status": "pending"}
|
||||
]
|
||||
}),
|
||||
),
|
||||
successful_result("create"),
|
||||
assistant_call(
|
||||
"merge",
|
||||
serde_json::json!({
|
||||
"merge": true,
|
||||
"todos": [
|
||||
{"id": "first", "status": "completed"},
|
||||
{"id": "second", "content": "Second updated"},
|
||||
{"id": "third", "content": "Third", "status": "cancelled"}
|
||||
]
|
||||
}),
|
||||
),
|
||||
successful_result("merge"),
|
||||
];
|
||||
|
||||
let state = fold_derived_state(&messages);
|
||||
|
||||
assert_eq!(
|
||||
state.todos.unwrap()["todos"],
|
||||
serde_json::json!([
|
||||
{"id": "first", "content": "First", "status": "completed"},
|
||||
{"id": "second", "content": "Second updated", "status": "pending"},
|
||||
{"id": "third", "content": "Third", "status": "cancelled"}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
fn assistant_call(call_id: &str, arguments: Value) -> CanonicalMessage {
|
||||
CanonicalMessage {
|
||||
message_id: format!("assistant-{call_id}"),
|
||||
role: Role::Assistant,
|
||||
origin: Origin::Assistant,
|
||||
content: MessageContent::Assistant {
|
||||
text: String::new(),
|
||||
thinking: String::new(),
|
||||
tool_round_id: Some(format!("round-{call_id}").into()),
|
||||
replay_state: None,
|
||||
tool_calls: vec![ToolCallContent {
|
||||
index: 0,
|
||||
call_id: call_id.into(),
|
||||
name: "TodoWrite".into(),
|
||||
arguments,
|
||||
}],
|
||||
},
|
||||
runtime_event_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn successful_result(call_id: &str) -> CanonicalMessage {
|
||||
CanonicalMessage {
|
||||
message_id: format!("result-{call_id}"),
|
||||
role: Role::Tool,
|
||||
origin: Origin::Tool,
|
||||
content: MessageContent::ToolResult(ToolResultContent {
|
||||
call_id: call_id.into(),
|
||||
name: "TodoWrite".into(),
|
||||
content: "{}".into(),
|
||||
is_error: false,
|
||||
image: None,
|
||||
provider_parts: Vec::new(),
|
||||
}),
|
||||
runtime_event_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user