mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
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:
1 parent
5a0bc2e0e9
commit
5cc2401ce2
20 files changed
+461
-52
No files matched your search
@@ -24,7 +24,7 @@ use crate::{
|
||||
ModelRequest, ModelSpec, ModelType, Overview, ProjectedContent, ProjectedMessage,
|
||||
PromptSpec, ProviderType, Role,
|
||||
},
|
||||
provider::{ModelEvent, Provider},
|
||||
provider::{is_valid_response_event, ModelEvent, Provider},
|
||||
store::{
|
||||
DesktopSettings, PortSettings, ProxySettings, ProxySettingsInput, StatisticsStorage, Store,
|
||||
TabSettings,
|
||||
@@ -95,7 +95,7 @@ fn empty_json_object_ref() -> &'static serde_json::Value {
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ModelConnectivityResult {
|
||||
pub duration_ms: u64,
|
||||
pub first_text_ms: Option<u64>,
|
||||
pub first_valid_response_ms: Option<u64>,
|
||||
pub output_tokens: u64,
|
||||
pub tokens_per_second: f64,
|
||||
pub tokens_estimated: bool,
|
||||
@@ -322,7 +322,7 @@ impl ControlService {
|
||||
},
|
||||
};
|
||||
let started = Instant::now();
|
||||
let mut first_text_at = None;
|
||||
let mut first_valid_response_at = None;
|
||||
let mut output_tokens = None;
|
||||
let mut output = String::new();
|
||||
let stream = self.provider.stream(invocation, cancellation.clone());
|
||||
@@ -330,11 +330,12 @@ impl ControlService {
|
||||
futures_util::pin_mut!(stream);
|
||||
let mut finished = false;
|
||||
while let Some(event) = stream.next().await {
|
||||
match event? {
|
||||
let event = event?;
|
||||
if first_valid_response_at.is_none() && is_valid_response_event(&event) {
|
||||
first_valid_response_at = Some(Instant::now());
|
||||
}
|
||||
match event {
|
||||
ModelEvent::TextDelta(delta) => {
|
||||
if first_text_at.is_none() && !delta.trim().is_empty() {
|
||||
first_text_at = Some(Instant::now());
|
||||
}
|
||||
output.push_str(&delta);
|
||||
}
|
||||
ModelEvent::Usage(usage) => {
|
||||
@@ -380,16 +381,16 @@ impl ControlService {
|
||||
}
|
||||
let elapsed = started.elapsed();
|
||||
let output = output.trim().to_string();
|
||||
if first_text_at.is_none() {
|
||||
if first_valid_response_at.is_none() {
|
||||
return Err(Error::Provider(
|
||||
"model connectivity test received no text output".into(),
|
||||
"model connectivity test received no valid response".into(),
|
||||
));
|
||||
}
|
||||
let tokens_estimated = output_tokens.is_none();
|
||||
let output_tokens = output_tokens.unwrap_or_else(|| estimate_output_tokens(&output));
|
||||
Ok(ModelConnectivityResult {
|
||||
duration_ms: elapsed.as_millis().min(u128::from(u64::MAX)) as u64,
|
||||
first_text_ms: first_text_at.map(|first| {
|
||||
first_valid_response_ms: first_valid_response_at.map(|first| {
|
||||
first
|
||||
.duration_since(started)
|
||||
.as_millis()
|
||||
@@ -628,10 +629,12 @@ fn official_call(trace: CursorRunTraceSummary) -> CallSummary {
|
||||
response_headers_at_ms: trace.first_response_at_ms,
|
||||
first_event_at_ms: trace.first_response_at_ms,
|
||||
first_text_at_ms: None,
|
||||
first_valid_response_at_ms: None,
|
||||
finished_at_ms: trace.finished_at_ms,
|
||||
queue_ms: None,
|
||||
ttfb_ms: ttfb,
|
||||
ttft_ms: None,
|
||||
ttfr_ms: None,
|
||||
duration_ms: duration,
|
||||
input_tokens: None,
|
||||
output_tokens: None,
|
||||
@@ -1132,6 +1135,7 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.output, "OK");
|
||||
assert!(result.first_valid_response_ms.is_some());
|
||||
assert_eq!(result.output_tokens, 2);
|
||||
assert!(!result.tokens_estimated);
|
||||
assert!(result.tokens_per_second > 0.0);
|
||||
|
||||
Reference in new issue
Block a user