Parse a JSON array of strings from potentially messy LLM output.
(text: &str)
| 140 | |
| 141 | /// Parse a JSON array of strings from potentially messy LLM output. |
| 142 | fn parse_json_string_array(text: &str) -> GraphBitResult<Vec<String>> { |
| 143 | // Try to find the JSON array in the response. |
| 144 | let trimmed = text.trim(); |
| 145 | |
| 146 | // First try direct parse. |
| 147 | if let Ok(arr) = serde_json::from_str::<Vec<String>>(trimmed) { |
| 148 | return Ok(arr); |
| 149 | } |
| 150 | |
| 151 | // Try to extract the first JSON array from the text. |
| 152 | if let Some(start) = trimmed.find('[') { |
| 153 | if let Some(end) = trimmed.rfind(']') { |
| 154 | let slice = &trimmed[start..=end]; |
| 155 | if let Ok(arr) = serde_json::from_str::<Vec<String>>(slice) { |
| 156 | return Ok(arr); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Fallback: return empty array if we can't parse. |
| 162 | Ok(Vec::new()) |
| 163 | } |
| 164 | |
| 165 | /// Parse the decision JSON from the LLM response. |
| 166 | fn parse_decisions(text: &str) -> GraphBitResult<Vec<MemoryDecision>> { |