Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

rust fix (#1588)

Matthew Diakonov0873544ca29d
diff --git a/screenpipe-audio/src/transcription/deepgram/streaming.rs b/screenpipe-audio/src/transcription/deepgram/streaming.rs
index b5404c2a2..138fd1fd1 100644
--- a/screenpipe-audio/src/transcription/deepgram/streaming.rs
+++ b/screenpipe-audio/src/transcription/deepgram/streaming.rs
@@ -19,6 +19,8 @@ use tokio::sync::broadcast::Receiver;
use tokio::sync::oneshot;
use tracing::debug;
use tracing::info;
+use serde_json;
+use serde_json::Value;
// Add this near other static/global variables
static LAST_DISPLAY_AUDIO_ACTIVITY: AtomicI64 = AtomicI64::new(0);
@@ -80,6 +82,20 @@ pub async fn start_deepgram_stream(
return Err(anyhow::anyhow!("Deepgram API key not found"));
}
+ // Remove the API key logging
+ info!("initializing deepgram transcription service");
+
+ // Log the WebSocket URL
+ let ws_url = if DEEPGRAM_WEBSOCKET_URL.as_str().is_empty() {
+ "default deepgram api".to_string()
+ } else {
+ DEEPGRAM_WEBSOCKET_URL.as_str().to_string()
+ };
+ debug!("connecting to deepgram at: {}", ws_url);
+
+ // Log the audio configuration
+ debug!("audio configuration: sample_rate={}, channels=1, device={}", sample_rate, device);
+
let (shutdown_tx, mut shutdown_rx) = oneshot::channel();
// Spawn shutdown monitor
@@ -90,40 +106,76 @@ pub async fn start_deepgram_stream(
let _ = shutdown_tx.send(()); // Ignore send errors if receiver is dropped
});
- info!("Starting deepgram stream for device: {}", device);
+ debug!("Starting deepgram stream for device: {}", device);
let deepgram = match DEEPGRAM_WEBSOCKET_URL.as_str().is_empty() {
true => deepgram::Deepgram::new(api_key)?,
false => {
+ debug!("using custom deepgram websocket url: {}", DEEPGRAM_WEBSOCKET_URL.as_str());
deepgram::Deepgram::with_base_url_and_api_key(DEEPGRAM_WEBSOCKET_URL.as_str(), api_key)?
}
};
let deepgram_transcription = deepgram.transcription();
+ let options = deepgram::common::options::OptionsBuilder::new()
+ .model(deepgram::common::options::Model::Nova2)
+ .smart_format(true)
+ .diarize(true)
+ .build();
+
+ debug!("deepgram options: {:?}", options);
+
let req = deepgram_transcription
- .stream_request_with_options(
- deepgram::common::options::OptionsBuilder::new()
- .model(deepgram::common::options::Model::Nova2)
- .smart_format(true)
- .diarize(true)
- .build(),
- )
+ .stream_request_with_options(options)
.keep_alive()
.channels(1)
.sample_rate(sample_rate)
.encoding(Encoding::Linear16);
+ debug!("sending deepgram request with encoding=Linear16, channels=1, sample_rate={}", sample_rate);
+
let mut handle = req.clone().handle().await?;
+ debug!("deepgram handle created successfully");
+
let mut results = req
.stream(get_stream(stream, device.device_type.clone()))
.await?;
+ debug!("deepgram stream started successfully");
+
let device_clone = device.clone();
+ // Add a sample of the expected response format for debugging
+ let sample_response = r#"
+ {
+ "type": "Results",
+ "channel_index": [0,1],
+ "duration": 3.02,
+ "is_final": true,
+ "speech_final": true,
+ "channel": {
+ "alternatives": [{"transcript": "sample", "confidence": 0.9, "words": []}]
+ }
+ }
+ "#;
+
+ // Try to parse the sample to see if our expected format matches
+ match serde_json::from_str::<StreamResponse>(sample_response) {
+ Ok(_) => debug!("sample response format is valid"),
+ Err(e) => {
+ debug!("expected format example error: {}", e);
+
+ // Try to parse as generic JSON to see what we can extract
+ if let Ok(value) = serde_json::from_str::<Value>(sample_response) {
+ debug!("sample can be parsed as generic json: {}", value["type"]);
+ }
+ }
+ };
+
loop {
tokio::select! {
Ok(()) = &mut shutdown_rx => {
- info!("Shutting down deepgram stream for device: {}", device);
+ debug!("Shutting down deepgram stream for device: {}", device);
break;
}
result = results.try_next() => {
@@ -133,8 +185,38 @@ pub async fn start_deepgram_stream(
}
Ok(None) => break, // Stream ended
Err(e) => {
- info!("Error in deepgram stream: {}", e);
- break;
+ // Log the error
+ debug!("Error in deepgram stream: {}", e);
+
+ // Try to extract and process the raw JSON from the error
+ if let Some(raw_data) = e.to_string().find("data:") {
+ let raw_str = &e.to_string()[raw_data..];
+ debug!("Raw data excerpt: {}", raw_str);
+
+ // Try to extract and process the raw JSON
+ if let Some(json_start) = raw_str.find('{') {
+ let json_str = &raw_str[json_start..];
+ if let Ok(value) = serde_json::from_str::<Value>(json_str) {
+ debug!("Processing raw JSON from error: {}", value);
+
+ // Try to handle as a Results type
+ if value.get("type").and_then(|t| t.as_str()) == Some("Results") {
+ debug!("Found Results type in error data, processing...");
+ // Process the transcription data
+ // ...
+ }
+ }
+ }
+ }
+
+ // Don't break the loop for deserialization errors
+ if e.to_string().contains("deserialization") {
+ debug!("Continuing despite deserialization error");
+ continue;
+ } else {
+ debug!("Breaking stream due to non-deserialization error");
+ break;
+ }
}
}
}
@@ -152,6 +234,9 @@ fn get_stream(
let (mut tx, rx) = mpsc::channel(1);
tokio::spawn(async move {
+ let mut packet_count = 0;
+ let mut total_bytes = 0;
+
while let Ok(data) = stream.recv().await {
if device_type == DeviceType::Output {
let sum_squares: f32 = data.iter().map(|&x| x * x).sum();
@@ -165,6 +250,9 @@ fn get_stream(
.as_millis() as i64,
Ordering::SeqCst,
);
+
+ // Log display audio activity
+ debug!("display audio activity detected: rms={:.6}", rms);
}
} else if SystemTime::now()
.duration_since(UNIX_EPOCH)
@@ -173,6 +261,7 @@ fn get_stream(
- LAST_DISPLAY_AUDIO_ACTIVITY.load(Ordering::SeqCst)
< 100
{
+ debug!("skipping input audio due to recent display activity");
continue;
}
@@ -180,7 +269,17 @@ fn get_stream(
for sample in data {
bytes.put_i16_le((sample * i16::MAX as f32) as i16);
}
+
+ packet_count += 1;
+ total_bytes += bytes.len();
+
+ if packet_count % 100 == 0 {
+ debug!("sent {} audio packets ({} bytes) to deepgram for device_type={:?}",
+ packet_count, total_bytes, device_type);
+ }
+
if tx.send(Ok(bytes.freeze())).await.is_err() {
+ debug!("stream receiver dropped, stopping audio transmission");
break; // Stop if receiver is dropped
}
}
@@ -190,36 +289,95 @@ fn get_stream(
}
async fn handle_transcription(result: StreamResponse, device: Arc<AudioDevice>) {
- if let StreamResponse::TranscriptResponse {
- channel, is_final, ..
- } = result
- {
- debug!(
- "handling realtime transcription for device: {}",
- device.name
- );
- let res = channel.alternatives.first().unwrap();
- let text = res.transcript.clone();
- let is_input = device.device_type == DeviceType::Input;
-
- let speaker = res
- .words
- .first()
- .and_then(|w| w.speaker)
- .map(|s| s.to_string());
-
- if !text.is_empty() {
- let _ = send_event(
- "transcription",
- RealtimeTranscriptionEvent {
- timestamp: chrono::Utc::now(),
- device: device.to_string(),
- transcription: text.to_string(),
- is_final,
- is_input,
- speaker,
- },
+ // Try to handle the result as a StreamResponse enum variant first
+ match result {
+ StreamResponse::TranscriptResponse { channel, is_final, .. } => {
+ debug!(
+ "received transcription for device: {}, is_final: {}",
+ device.name, is_final
);
+ let res = channel.alternatives.first().unwrap();
+ let text = res.transcript.clone();
+ let is_input = device.device_type == DeviceType::Input;
+
+ let speaker = res
+ .words
+ .first()
+ .and_then(|w| w.speaker)
+ .map(|s| s.to_string());
+
+ if !text.is_empty() {
+ debug!("transcription text: {}", text);
+ if let Some(speaker_id) = &speaker {
+ debug!("speaker identified: {}", speaker_id);
+ }
+
+ let _ = send_event(
+ "transcription",
+ RealtimeTranscriptionEvent {
+ timestamp: chrono::Utc::now(),
+ device: device.to_string(),
+ transcription: text.to_string(),
+ is_final,
+ is_input,
+ speaker,
+ },
+ );
+ }
+ },
+ // Add a custom handler for the "Results" type that Deepgram actually sends
+ _ => {
+ // Convert the StreamResponse to a JSON Value to handle it generically
+ if let Ok(value) = serde_json::to_value(&result) {
+ debug!("handling raw response: {}", value);
+
+ // Check if this is a "Results" type response
+ if value.get("type").and_then(|t| t.as_str()) == Some("Results") {
+ let is_final = value.get("is_final").and_then(|v| v.as_bool()).unwrap_or(false);
+
+ // Extract the transcript from the channel alternatives
+ if let Some(channel) = value.get("channel") {
+ if let Some(alternatives) = channel.get("alternatives") {
+ if let Some(first_alt) = alternatives.get(0) {
+ if let Some(transcript) = first_alt.get("transcript").and_then(|t| t.as_str()) {
+ if !transcript.is_empty() {
+ debug!("extracted transcript from results: {}", transcript);
+
+ // Extract speaker if available
+ let speaker = if let Some(words) = first_alt.get("words") {
+ if let Some(first_word) = words.get(0) {
+ first_word.get("speaker").and_then(|s| s.as_u64()).map(|s| s.to_string())
+ } else {
+ None
+ }
+ } else {
+ None
+ };
+
+ let is_input = device.device_type == DeviceType::Input;
+
+ let _ = send_event(
+ "transcription",
+ RealtimeTranscriptionEvent {
+ timestamp: chrono::Utc::now(),
+ device: device.to_string(),
+ transcription: transcript.to_string(),
+ is_final,
+ is_input,
+ speaker,
+ },
+ );
+ }
+ }
+ }
+ }
+ }
+ } else {
+ debug!("unhandled response type: {:?}", value.get("type"));
+ }
+ } else {
+ debug!("failed to convert response to json: {:?}", result);
+ }
}
}
}
diff --git a/screenpipe-js/ai-proxy/src/handlers/transcription.ts b/screenpipe-js/ai-proxy/src/handlers/transcription.ts
index e79e52d2a..048dd1cd5 100644
--- a/screenpipe-js/ai-proxy/src/handlers/transcription.ts
+++ b/screenpipe-js/ai-proxy/src/handlers/transcription.ts
@@ -15,7 +15,7 @@ export async function handleFileTranscription(request: Request, env: Env): Promi
const sampleRate = request.headers.get('sample_rate') || '16000';
const deepgramResponse = await fetch(
- 'https://api.deepgram.com/v1/listen?model=nova-2&smart_format=true&sample_rate=' +
+ 'https://api.deepgram.com/v1/listen?model=nova-3&smart_format=true&sample_rate=' +
sampleRate +
(languages.length > 0 ? '&' + languages.map((lang) => `detect_language=${lang}`).join('&') : ''),
{