feat(api): enhance statistics storage management with scope options

- Introduced a new type `StatisticsStorageScope` to specify the scope for clearing statistics.
- Updated the `clearStatisticsStorage` API method to accept a scope parameter, allowing for selective clearing of detailed records or all statistics.
- Modified the demo API to handle the new scope parameter appropriately.
- Updated the SettingsPage component to include a selection for clearing scope, enhancing user control over statistics management.
- Added new translations for the updated messages related to statistics clearing in both English and Chinese.
This commit is contained in:
leookun committed 2026-08-28 10:18:11 +08:00
1 parent 217f984112
commit 71ddf71ec1
15 files changed
+514 -111

No files matched your search

+25 -2
View File
@@ -64,6 +64,7 @@ impl CursorActor {
};
match command {
CursorCommand::Abort => {
handle.mark_conversation_cancelled();
handle.cancel();
}
CursorCommand::Finished => {
@@ -76,6 +77,24 @@ impl CursorActor {
Some(pb::agent_client_message::Message::RunRequest(
request,
)) => {
if let Some(conversation_id) =
request.conversation_id.as_deref()
{
if let Err(error) =
handle.set_conversation_id(conversation_id)
{
tracing::error!(
request_id = handle.request_id(),
%error,
"invalid Cursor conversation id"
);
let _ =
crate::cursor::lifecycle::fail(&handle, &error);
let _ =
handle.command(CursorCommand::Finished).await;
return;
}
}
if let Some((results, runtime_actions, dependencies)) =
run_resources.take()
{
@@ -357,8 +376,12 @@ impl CursorActor {
) => match action.action {
Some(
pb::conversation_action::Action::UserMessageAction(_),
)
| Some(pb::conversation_action::Action::CancelAction(_)) => {
) => {
handle.mark_conversation_cancelled();
handle.cancel();
}
Some(pb::conversation_action::Action::CancelAction(_)) => {
handle.mark_conversation_cancelled();
handle.cancel();
}
Some(
+66
View File
@@ -43,6 +43,33 @@ impl DecodedAppend {
request.conversation_id.as_deref()
}
pub fn is_background_task_completion(&self) -> bool {
let Some(agent::agent_client_message::Message::RunRequest(request)) =
self.message.message.as_ref()
else {
return false;
};
matches!(
request
.action
.as_ref()
.and_then(|action| action.action.as_ref()),
Some(agent::conversation_action::Action::BackgroundTaskCompletionAction(_))
)
}
fn is_runtime_cancellation(&self) -> bool {
matches!(
self.message.message.as_ref(),
Some(agent::agent_client_message::Message::ConversationAction(action))
if matches!(
action.action.as_ref(),
Some(agent::conversation_action::Action::CancelAction(_))
| Some(agent::conversation_action::Action::UserMessageAction(_))
)
)
}
pub fn trace_metadata(&self) -> serde_json::Value {
let Some(message) = self.message.message.as_ref() else {
return serde_json::json!({
@@ -163,7 +190,29 @@ pub async fn append(
request: DecodedAppend,
parent: Option<CursorParent>,
) -> Result<ai::BidiAppendResponse> {
if let Some(conversation_id) = request.conversation_id() {
if request.is_background_task_completion()
&& registry.conversation_cancelled(conversation_id)
{
tracing::info!(
request_id = %request.request_id,
%conversation_id,
"dropping background task completion for cancelled conversation"
);
return Ok(ai::BidiAppendResponse {});
}
if !request.is_background_task_completion() {
registry.clear_conversation_cancelled(conversation_id);
}
}
let handle = registry.get_or_create(&request.request_id).await?;
if let Some(conversation_id) = request.conversation_id() {
handle.set_conversation_id(conversation_id)?;
}
if request.is_runtime_cancellation() {
handle.mark_conversation_cancelled();
handle.cancel();
}
if let Some(parent) = parent {
handle.set_parent(parent)?;
}
@@ -225,4 +274,21 @@ mod tests {
.unwrap();
assert_eq!(decoded.model_id(), Some("grok-4.6"));
}
#[test]
fn detects_background_task_completion_actions() {
let decoded = decode(&encoded(agent::AgentRunRequest {
action: Some(agent::ConversationAction {
action: Some(
agent::conversation_action::Action::BackgroundTaskCompletionAction(
agent::BackgroundTaskCompletionAction::default(),
),
),
..Default::default()
}),
..Default::default()
}))
.unwrap();
assert!(decoded.is_background_task_completion());
}
}
+50 -1
View File
@@ -1,5 +1,5 @@
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
sync::{Arc, OnceLock},
};
@@ -29,6 +29,8 @@ pub struct CursorSessionHandle {
commands: mpsc::Sender<CursorCommand>,
output: Arc<OutputHub>,
cancellation: CancellationToken,
conversation_id: Arc<OnceLock<String>>,
cancelled_conversations: Arc<parking_lot::Mutex<HashSet<String>>>,
parent: Arc<OnceLock<CursorParent>>,
trace: Option<CursorTraceRecorder>,
}
@@ -43,6 +45,35 @@ impl CursorSessionHandle {
pub fn request_id(&self) -> &str {
&self.request_id
}
pub fn set_conversation_id(&self, conversation_id: &str) -> Result<()> {
if conversation_id.is_empty() {
return Err(crate::Error::Protocol(
"Cursor conversation id is required".into(),
));
}
if self
.conversation_id
.get()
.is_some_and(|current| current != conversation_id)
{
return Err(crate::Error::Protocol(format!(
"conflicting conversation ids for request {}",
self.request_id
)));
}
let _ = self.conversation_id.set(conversation_id.into());
Ok(())
}
pub fn conversation_id(&self) -> Option<&str> {
self.conversation_id.get().map(String::as_str)
}
pub fn mark_conversation_cancelled(&self) {
if let Some(conversation_id) = self.conversation_id() {
self.cancelled_conversations
.lock()
.insert(conversation_id.to_owned());
}
}
pub fn subscribe(&self) -> mpsc::UnboundedReceiver<Bytes> {
self.output.subscribe()
}
@@ -160,6 +191,7 @@ struct RegistryInner {
store: Store,
provider: Arc<dyn Provider>,
compiler: PromptCompiler,
cancelled_conversations: Arc<parking_lot::Mutex<HashSet<String>>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -188,6 +220,7 @@ impl CursorSessionRegistry {
store,
provider,
compiler,
cancelled_conversations: Arc::new(parking_lot::Mutex::new(HashSet::new())),
}),
}
}
@@ -205,6 +238,8 @@ impl CursorSessionRegistry {
commands,
output,
cancellation,
conversation_id: Arc::new(OnceLock::new()),
cancelled_conversations: self.inner.cancelled_conversations.clone(),
parent: Arc::new(OnceLock::new()),
trace,
};
@@ -262,6 +297,20 @@ impl CursorSessionRegistry {
.contains_key(request_id)
}
pub(crate) fn conversation_cancelled(&self, conversation_id: &str) -> bool {
self.inner
.cancelled_conversations
.lock()
.contains(conversation_id)
}
pub(crate) fn clear_conversation_cancelled(&self, conversation_id: &str) {
self.inner
.cancelled_conversations
.lock()
.remove(conversation_id);
}
pub(crate) async fn wait_route(&self, request_id: &str) -> CursorRoute {
loop {
let changed = self.inner.route_changed.notified();