(mut raw: Value, tool_result: Option<&Value>)
| 357 | } |
| 358 | |
| 359 | fn normalize_tool_raw(mut raw: Value, tool_result: Option<&Value>) -> Value { |
| 360 | // Mirror camelCase tool fields into the snake_case keys downstream expects. |
| 361 | if let Some(obj) = raw.as_object_mut() { |
| 362 | if let Some(name) = obj.get("toolName").cloned() { |
| 363 | obj.entry("tool_name".to_string()).or_insert(name); |
| 364 | } |
| 365 | if let Some(id) = obj.get("toolUseId").cloned() { |
| 366 | obj.entry("tool_use_id".to_string()).or_insert(id); |
| 367 | } |
| 368 | if let Some(input) = obj.get("toolInput").cloned() { |
| 369 | obj.entry("tool_input".to_string()).or_insert(input); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | let result = tool_result |
| 374 | .cloned() |
| 375 | .or_else(|| raw.get("toolResult").cloned()) |
| 376 | .or_else(|| raw.get("tool_response").cloned()); |
| 377 | |
| 378 | if let Some(response) = result { |
| 379 | if let Some(output) = extract_tool_output(&response) { |
| 380 | insert_if_missing(&mut raw, "tool_output", Value::String(output)); |
| 381 | } |
| 382 | if response.get("error").is_some() { |
| 383 | insert_if_missing(&mut raw, "status", Value::String("error".to_string())); |
| 384 | } else { |
| 385 | insert_if_missing(&mut raw, "status", Value::String("completed".to_string())); |
| 386 | } |
| 387 | // Keep a snake_case alias of the result for shared tooling. |
| 388 | insert_if_missing(&mut raw, "tool_response", response); |
| 389 | } |
| 390 | |
| 391 | // File path from tool input when present. |
| 392 | if let Some(path) = extract_file_path(raw.get("toolInput").or_else(|| raw.get("tool_input"))) { |
| 393 | insert_if_missing(&mut raw, "file_path", Value::String(path)); |
| 394 | } |
| 395 | |
| 396 | raw |
| 397 | } |
| 398 | |
| 399 | fn extract_tool_output(response: &Value) -> Option<String> { |
| 400 | if let Some(text) = response.as_str() { |
no test coverage detected