| 21 | } |
| 22 | |
| 23 | function parseSearchResults(result: string): SearchResult[] { |
| 24 | // Try JSON first |
| 25 | try { |
| 26 | const data = JSON.parse(result); |
| 27 | if (Array.isArray(data)) { |
| 28 | return data.map((item) => ({ |
| 29 | title: item.title ?? item.name ?? "(no title)", |
| 30 | url: item.url ?? item.link ?? "", |
| 31 | snippet: item.snippet ?? item.description ?? item.content ?? "", |
| 32 | })); |
| 33 | } |
| 34 | if (data.results) return parseSearchResults(JSON.stringify(data.results)); |
| 35 | } catch { |
| 36 | // not JSON |
| 37 | } |
| 38 | |
| 39 | // Fallback: treat raw text as a single result |
| 40 | return [{ title: "Search Result", url: "", snippet: result }]; |
| 41 | } |
| 42 | |
| 43 | export function ToolWebSearch({ |
| 44 | input, |