(
&self,
line: &str,
current_event: &mut String,
agent_run_id: &str,
token_sink: &S,
output: &mut String,
pending_calls: &mut HashMap<usize, Str
| 1462 | |
| 1463 | #[allow(clippy::too_many_arguments)] |
| 1464 | fn parse_anthropic_sse_line<S: TokenSink + Sync>( |
| 1465 | &self, |
| 1466 | line: &str, |
| 1467 | current_event: &mut String, |
| 1468 | agent_run_id: &str, |
| 1469 | token_sink: &S, |
| 1470 | output: &mut String, |
| 1471 | pending_calls: &mut HashMap<usize, StreamingToolCall>, |
| 1472 | stop_reason: &mut Option<String>, |
| 1473 | ) -> AppResult<bool> { |
| 1474 | let trimmed = line.trim(); |
| 1475 | if trimmed.is_empty() { |
| 1476 | return Ok(false); |
| 1477 | } |
| 1478 | |
| 1479 | if let Some(event_name) = trimmed.strip_prefix("event:") { |
| 1480 | let event_name = event_name.trim(); |
| 1481 | *current_event = event_name.to_string(); |
| 1482 | if event_name == "message_stop" { |
| 1483 | return Ok(true); |
| 1484 | } |
| 1485 | return Ok(false); |
| 1486 | } |
| 1487 | |
| 1488 | let Some(payload_raw) = trimmed.strip_prefix("data:") else { |
| 1489 | return Ok(false); |
| 1490 | }; |
| 1491 | let payload = payload_raw.trim(); |
| 1492 | |
| 1493 | let value: Value = match serde_json::from_str(payload) { |
| 1494 | Ok(value) => value, |
| 1495 | Err(_) => return Ok(false), |
| 1496 | }; |
| 1497 | |
| 1498 | // Prefer `"type"` from JSON payload; fall back to the preceding |
| 1499 | // `event:` line when the gateway strips it. |
| 1500 | let event_type = value |
| 1501 | .get("type") |
| 1502 | .and_then(Value::as_str) |
| 1503 | .unwrap_or(current_event.as_str()); |
| 1504 | |
| 1505 | match event_type { |
| 1506 | "content_block_start" => { |
| 1507 | let is_tool_use = value |
| 1508 | .get("content_block") |
| 1509 | .and_then(|block| block.get("type")) |
| 1510 | .and_then(Value::as_str) |
| 1511 | == Some("tool_use"); |
| 1512 | |
| 1513 | if is_tool_use { |
| 1514 | let index_u64 = value |
| 1515 | .get("index") |
| 1516 | .and_then(Value::as_u64) |
| 1517 | .ok_or_else(|| { |
| 1518 | AppError::Json( |
| 1519 | "Anthropic tool_use content_block_start missing index".to_string(), |
| 1520 | ) |
| 1521 | })?; |
no test coverage detected