| 1842 | |
| 1843 | #[allow(dead_code)] |
| 1844 | fn process_response(response: &ChatResponse) -> SessionMessage { |
| 1845 | let now = chrono::Utc::now(); |
| 1846 | |
| 1847 | let content = response |
| 1848 | .choices |
| 1849 | .first() |
| 1850 | .map(|c| c.message.content.clone()) |
| 1851 | .unwrap_or(Content::Text(String::new())); |
| 1852 | |
| 1853 | let finish_reason = response |
| 1854 | .choices |
| 1855 | .first() |
| 1856 | .and_then(|c| c.finish_reason.clone()); |
| 1857 | |
| 1858 | let parts = match content { |
| 1859 | Content::Text(text) => vec![crate::MessagePart { |
| 1860 | id: format!("prt_{}", uuid::Uuid::new_v4()), |
| 1861 | part_type: PartType::Text { text, synthetic: None, ignored: None }, |
| 1862 | created_at: now, |
| 1863 | message_id: None, |
| 1864 | }], |
| 1865 | Content::Parts(content_parts) => content_parts |
| 1866 | .into_iter() |
| 1867 | .filter_map(|p| match p.content_type.as_str() { |
| 1868 | "text" => p.text.map(|text| crate::MessagePart { |
| 1869 | id: format!("prt_{}", uuid::Uuid::new_v4()), |
| 1870 | part_type: PartType::Text { text, synthetic: None, ignored: None }, |
| 1871 | created_at: now, |
| 1872 | message_id: None, |
| 1873 | }), |
| 1874 | "tool_use" => p.tool_use.map(|tu| crate::MessagePart { |
| 1875 | id: format!("prt_{}", uuid::Uuid::new_v4()), |
| 1876 | part_type: PartType::ToolCall { |
| 1877 | id: tu.id, |
| 1878 | name: tu.name, |
| 1879 | input: tu.input, |
| 1880 | }, |
| 1881 | created_at: now, |
| 1882 | message_id: None, |
| 1883 | }), |
| 1884 | _ => None, |
| 1885 | }) |
| 1886 | .collect(), |
| 1887 | }; |
| 1888 | |
| 1889 | SessionMessage { |
| 1890 | id: format!("msg_{}", uuid::Uuid::new_v4()), |
| 1891 | session_id: String::new(), |
| 1892 | role: MessageRole::Assistant, |
| 1893 | parts, |
| 1894 | created_at: now, |
| 1895 | metadata: { |
| 1896 | let mut m = HashMap::new(); |
| 1897 | if let Some(usage) = &response.usage { |
| 1898 | m.insert( |
| 1899 | "tokens_input".to_string(), |
| 1900 | serde_json::json!(usage.prompt_tokens), |
| 1901 | ); |