feat: add first valid response timing and related metrics

- Introduced `first_valid_response_ms` and `ttfr_ms` to track the timing of the first valid response in LLM calls.
- Updated relevant interfaces and components to display and utilize the new metrics, including CallDetails, CallTable, and LatencyChart.
- Enhanced the database schema to accommodate the new timing fields.
- Implemented logic in the service layer to record the first valid response during model interactions.
This commit is contained in:
leookun committed 2026-08-28 22:56:39 +08:00
1 parent 5a0bc2e0e9
commit 5cc2401ce2
20 files changed
+461 -52

No files matched your search

+20 -5
View File
@@ -44,7 +44,8 @@ impl CursorActor {
tokio::spawn(async move {
let mut inbox = OrderedInbox::starting_at(next_append_seqno);
let (results_tx, results_rx) = tool_result_channel();
let (runtime_actions_tx, runtime_actions_rx) = mpsc::unbounded_channel();
let (runtime_actions_tx, runtime_actions_rx) =
mpsc::unbounded_channel::<super::request::RuntimeAction>();
let tool_runtime = CursorToolRuntime::default();
let context_sync =
RequestContextSynchronizer::new(handle.clone(), dependencies.store.clone());
@@ -375,10 +376,21 @@ impl CursorActor {
),
) => match action.action {
Some(
pb::conversation_action::Action::UserMessageAction(_),
pb::conversation_action::Action::UserMessageAction(
action,
),
) => {
handle.mark_conversation_cancelled();
handle.cancel();
if runtime_actions_tx
.send(super::request::RuntimeAction::UserMessage(
action,
))
.is_err()
{
results_tx.send_error(crate::Error::Protocol(
"UserMessageAction arrived without an active Run"
.into(),
));
}
}
Some(pb::conversation_action::Action::CancelAction(_)) => {
handle.mark_conversation_cancelled();
@@ -389,7 +401,10 @@ impl CursorActor {
action,
),
) => {
if runtime_actions_tx.send(action).is_err() {
if runtime_actions_tx
.send(super::request::RuntimeAction::Inject(action))
.is_err()
{
results_tx.send_error(crate::Error::Protocol(
"InjectContextAction arrived without an active Run"
.into(),
-1
View File
@@ -65,7 +65,6 @@ impl DecodedAppend {
if matches!(
action.action.as_ref(),
Some(agent::conversation_action::Action::CancelAction(_))
| Some(agent::conversation_action::Action::UserMessageAction(_))
)
)
}
+1 -1
View File
@@ -6,4 +6,4 @@ mod prepare;
mod runtime;
pub use prepare::*;
pub(crate) use runtime::compile_injection;
pub(crate) use runtime::{compile_injection, compile_user_message_action, RuntimeAction};
+51
View File
@@ -16,6 +16,57 @@ use crate::{
use super::{context, images};
pub(crate) enum RuntimeAction {
Inject(pb::InjectContextAction),
UserMessage(pb::UserMessageAction),
}
pub(crate) async fn compile_user_message_action(
action: &pb::UserMessageAction,
current_mode: i32,
compiler: &PromptCompiler,
blobs: &BlobSynchronizer,
) -> Result<CanonicalMessage> {
let user = action
.user_message
.as_ref()
.ok_or_else(|| Error::Protocol("Cursor user message action has no UserMessage".into()))?;
if user.message_id.is_empty() {
return Err(Error::Protocol(
"Cursor user message action has no message_id".into(),
));
}
let mode = if user.mode == pb::AgentMode::Unspecified as i32 {
current_mode
} else {
user.mode
};
let mut action_context = action
.prepend_user_messages
.iter()
.map(|message| message.text.trim())
.filter(|text| !text.is_empty())
.map(str::to_string)
.collect::<Vec<_>>();
action_context.extend(
user.subagent_system_reminder
.iter()
.filter(|text| !text.is_empty())
.cloned(),
);
let empty_context = pb::RequestContext::default();
compile(
format!("user-message:{}", user.message_id),
super::prepare::mode_from_proto(mode)?,
user,
action.request_context.as_ref().unwrap_or(&empty_context),
&action_context.join("\\n\\n"),
compiler,
blobs,
)
.await
}
pub(crate) async fn compile_injection(
injection: &pb::InjectContextAction,
mode: i32,
+102 -27
View File
@@ -14,7 +14,9 @@ use crate::{
presentation::Presentation,
prompting::PromptCompiler,
proto::agent::v1 as pb,
request::CursorRunContext,
request::{
compile_injection, compile_user_message_action, CursorRunContext, RuntimeAction,
},
tools::{
codec,
result::{ToolCompletion, ToolResultReceiver},
@@ -40,7 +42,7 @@ pub struct CursorSession {
results: ToolResultReceiver,
checkpoint: CheckpointBuilder,
tool_runtime: CursorToolRuntime,
runtime_actions: mpsc::UnboundedReceiver<pb::InjectContextAction>,
runtime_actions: mpsc::UnboundedReceiver<RuntimeAction>,
compiler: PromptCompiler,
blob_sync: BlobSynchronizer,
injection_ids: HashSet<String>,
@@ -52,12 +54,20 @@ struct PendingInjection {
delivery_batch_id: String,
}
struct InjectionState<'a> {
active_round: Option<&'a ToolRoundId>,
active_tool_calls: &'a HashSet<String>,
completions: &'a HashMap<String, ToolCompletion>,
interrupted_rounds: &'a mut HashSet<ToolRoundId>,
interrupted_tool_calls: &'a mut HashSet<String>,
}
pub(crate) struct CursorSessionRuntime {
pub tools: ToolDispatcher,
pub results: ToolResultReceiver,
pub checkpoint: CheckpointBuilder,
pub tool_runtime: CursorToolRuntime,
pub runtime_actions: mpsc::UnboundedReceiver<pb::InjectContextAction>,
pub runtime_actions: mpsc::UnboundedReceiver<RuntimeAction>,
pub compiler: PromptCompiler,
pub blob_sync: BlobSynchronizer,
}
@@ -170,17 +180,30 @@ impl CursorSession {
Input::CompletionResult(None) => {
return Err(Error::Protocol("tool result channel closed".into()));
}
Input::RuntimeAction(Some(action)) => {
self.forward_injection(
*action,
active_round.as_ref(),
&active_tool_calls,
&completions,
&mut interrupted_rounds,
&mut interrupted_tool_calls,
)
.await?;
}
Input::RuntimeAction(Some(action)) => match *action {
RuntimeAction::Inject(action) => {
self.forward_injection(
action,
active_round.as_ref(),
&active_tool_calls,
&completions,
&mut interrupted_rounds,
&mut interrupted_tool_calls,
)
.await?;
}
RuntimeAction::UserMessage(action) => {
self.forward_user_message(
action,
active_round.as_ref(),
&active_tool_calls,
&completions,
&mut interrupted_rounds,
&mut interrupted_tool_calls,
)
.await?;
}
},
Input::RuntimeAction(None) => {
return Err(Error::Protocol("runtime action channel closed".into()));
}
@@ -679,6 +702,41 @@ impl CursorSession {
Ok(dispatched.completion)
}
async fn forward_user_message(
&mut self,
action: pb::UserMessageAction,
active_round: Option<&ToolRoundId>,
active_tool_calls: &HashSet<String>,
completions: &HashMap<String, ToolCompletion>,
interrupted_rounds: &mut HashSet<ToolRoundId>,
interrupted_tool_calls: &mut HashSet<String>,
) -> Result<()> {
let user_message = action.user_message.clone().ok_or_else(|| {
Error::Protocol("Cursor user message action has no UserMessage".into())
})?;
let injection_id = format!("user-message:{}", user_message.message_id);
let message = compile_user_message_action(
&action,
self.context.mode,
&self.compiler,
&self.blob_sync,
)
.await?;
self.queue_injection(
injection_id,
Some(user_message),
message,
InjectionState {
active_round,
active_tool_calls,
completions,
interrupted_rounds,
interrupted_tool_calls,
},
)
.await
}
async fn forward_injection(
&mut self,
action: pb::InjectContextAction,
@@ -714,14 +772,30 @@ impl CursorSession {
}
_ => None,
};
let message = crate::cursor::request::compile_injection(
&action,
self.context.mode,
&self.compiler,
&self.blob_sync,
let message =
compile_injection(&action, self.context.mode, &self.compiler, &self.blob_sync).await?;
self.queue_injection(
action.injection_id,
user_message,
message,
InjectionState {
active_round,
active_tool_calls,
completions,
interrupted_rounds,
interrupted_tool_calls,
},
)
.await?;
let injection_id = action.injection_id;
.await
}
async fn queue_injection(
&mut self,
injection_id: String,
user_message: Option<pb::UserMessage>,
message: crate::model::CanonicalMessage,
state: InjectionState<'_>,
) -> Result<()> {
let delivery_batch_id = injection_id.clone();
self.injection_ids.insert(injection_id.clone());
self.pending_injections.insert(
@@ -733,14 +807,15 @@ impl CursorSession {
);
self.handle
.emit(&interaction::context_injection_queued(injection_id.clone()))?;
interrupted_tool_calls.extend(
active_tool_calls
state.interrupted_tool_calls.extend(
state
.active_tool_calls
.iter()
.filter(|call_id| !completions.contains_key(*call_id))
.filter(|call_id| !state.completions.contains_key(*call_id))
.cloned(),
);
if let Some(round_id) = active_round {
interrupted_rounds.insert(round_id.clone());
if let Some(round_id) = state.active_round {
state.interrupted_rounds.insert(round_id.clone());
}
self.interrupt_execs().await;
if self
@@ -780,7 +855,7 @@ enum Input {
Event(Option<ClientEvent>),
Completion(ToolCompletion),
CompletionResult(Option<Result<ToolCompletion>>),
RuntimeAction(Option<Box<pb::InjectContextAction>>),
RuntimeAction(Option<Box<RuntimeAction>>),
CheckpointFailure(Option<Error>),
}