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

fix: merge accessibility tree link nodes into OCR text positions for URLs

Louis Beaumont5874893d3207
diff --git a/crates/screenpipe-server/src/routes/frames.rs b/crates/screenpipe-server/src/routes/frames.rs
index 1da412cbe..b0514f685 100644
--- a/crates/screenpipe-server/src/routes/frames.rs
+++ b/crates/screenpipe-server/src/routes/frames.rs
@@ -631,66 +631,101 @@ pub async fn get_frame_ocr_data(
State(state): State<Arc<AppState>>,
Path(frame_id): Path<i64>,
) -> Result<JsonResponse<FrameOcrResponse>, (StatusCode, JsonResponse<Value>)> {
- // Try OCR data first (has bounding boxes from Apple Vision)
- match state.db.get_frame_text_positions(frame_id).await {
- Ok(text_positions) if !text_positions.is_empty() => {
- return Ok(JsonResponse(FrameOcrResponse {
- frame_id,
- text_positions,
- }));
- }
+ // Get OCR data (bounding boxes from Apple Vision)
+ let mut text_positions = match state.db.get_frame_text_positions(frame_id).await {
+ Ok(tp) => tp,
Err(e) => {
error!("Failed to get OCR data for frame {}: {}", frame_id, e);
+ Vec::new()
}
- _ => {}
- }
+ };
- // Fallback: extract text positions from accessibility tree node bounds
+ // Merge accessibility tree link nodes — they have complete URLs with proper
+ // bounds, unlike OCR which often splits URLs across multiple text blocks.
if let Ok((_, Some(tree_json))) = state.db.get_frame_accessibility_data(frame_id).await {
- if let Ok(nodes) =
- serde_json::from_str::<Vec<serde_json::Value>>(&tree_json)
- {
- let text_positions: Vec<TextPosition> = nodes
- .iter()
- .filter_map(|n| {
- let text = n.get("text")?.as_str()?;
- if text.trim().is_empty() {
- return None;
- }
- let b = n.get("bounds")?;
- let left = b.get("left")?.as_f64()? as f32;
- let top = b.get("top")?.as_f64()? as f32;
- let width = b.get("width")?.as_f64()? as f32;
- let height = b.get("height")?.as_f64()? as f32;
- if width <= 0.0 || height <= 0.0 {
- return None;
- }
- Some(TextPosition {
- text: text.to_string(),
- confidence: 1.0,
- bounds: screenpipe_db::TextBounds {
- left,
- top,
- width,
- height,
- },
- })
- })
- .collect();
+ if let Ok(nodes) = serde_json::from_str::<Vec<serde_json::Value>>(&tree_json) {
+ for n in &nodes {
+ let role = n.get("role").and_then(|v| v.as_str()).unwrap_or("");
+ let role_lower = role.to_lowercase();
+ // Only merge link/hyperlink nodes (they carry full URLs)
+ if !role_lower.contains("link") && !role_lower.contains("hyperlink") {
+ continue;
+ }
+ let text = match n.get("text").and_then(|v| v.as_str()) {
+ Some(t) if !t.trim().is_empty() => t,
+ _ => continue,
+ };
+ // Must look like a URL
+ if !text.trim().starts_with("http://")
+ && !text.trim().starts_with("https://")
+ && !text.trim().starts_with("www.")
+ {
+ continue;
+ }
+ let b = match n.get("bounds") {
+ Some(b) => b,
+ None => continue,
+ };
+ let left = b.get("left").and_then(|v| v.as_f64()).unwrap_or(-1.0) as f32;
+ let top = b.get("top").and_then(|v| v.as_f64()).unwrap_or(-1.0) as f32;
+ let width = b.get("width").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
+ let height = b.get("height").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
+ if width <= 0.0 || height <= 0.0 {
+ continue;
+ }
+ text_positions.push(TextPosition {
+ text: text.to_string(),
+ confidence: 1.0,
+ bounds: screenpipe_db::TextBounds {
+ left,
+ top,
+ width,
+ height,
+ },
+ });
+ }
+ }
+ }
- if !text_positions.is_empty() {
- return Ok(JsonResponse(FrameOcrResponse {
- frame_id,
- text_positions,
- }));
+ // Pure a11y fallback for frames with no OCR at all (already handled above
+ // for link nodes, but also grab all text nodes when OCR is empty)
+ if text_positions.is_empty() {
+ if let Ok((_, Some(tree_json))) = state.db.get_frame_accessibility_data(frame_id).await {
+ if let Ok(nodes) = serde_json::from_str::<Vec<serde_json::Value>>(&tree_json) {
+ text_positions = nodes
+ .iter()
+ .filter_map(|n| {
+ let text = n.get("text")?.as_str()?;
+ if text.trim().is_empty() {
+ return None;
+ }
+ let b = n.get("bounds")?;
+ let left = b.get("left")?.as_f64()? as f32;
+ let top = b.get("top")?.as_f64()? as f32;
+ let width = b.get("width")?.as_f64()? as f32;
+ let height = b.get("height")?.as_f64()? as f32;
+ if width <= 0.0 || height <= 0.0 {
+ return None;
+ }
+ Some(TextPosition {
+ text: text.to_string(),
+ confidence: 1.0,
+ bounds: screenpipe_db::TextBounds {
+ left,
+ top,
+ width,
+ height,
+ },
+ })
+ })
+ .collect();
}
}
}
- // No data from either source
Ok(JsonResponse(FrameOcrResponse {
frame_id,
- text_positions: Vec::new(),
+ text_positions,
}))
}