| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/url" |
| 6 | ) |
| 7 | |
| 8 | const SourcesAvailable = "available" |
| 9 | const SourcesNotProvided = "not_provided" |
| 10 | |
| 11 | func HasUsableSearchSources(hits []ServerSearchHit) bool { |
| 12 | for _, hit := range hits { |
| 13 | u, err := url.Parse(hit.URL) |
| 14 | if err == nil && u.Host != "" && u.User == nil && (u.Scheme == "https" || u.Scheme == "http") { |
| 15 | return true |
| 16 | } |
| 17 | } |
| 18 | return false |
| 19 | } |
| 20 | |
| 21 | func ServerSearchSourcesStatus(call ServerSearchCall) string { |
| 22 | if HasUsableSearchSources(call.Results) { |
| 23 | return SourcesAvailable |
| 24 | } |
| 25 | var raw any |
| 26 | if json.Unmarshal(call.Raw, &raw) != nil { |
| 27 | return "" |
| 28 | } |
| 29 | switch value := raw.(type) { |
| 30 | case []any: |
| 31 | return SourcesNotProvided |
| 32 | case map[string]any: |
| 33 | if value["type"] == "web_search_call" && value["status"] == "completed" { |
| 34 | return SourcesNotProvided |
| 35 | } |
| 36 | } |
| 37 | return "" |
| 38 | } |
| 39 | |
| 40 | // ServerSearchDisplayOutput is presentation data, never a raw replay item. |
| 41 | func ServerSearchDisplayOutput(call ServerSearchCall) string { |
| 42 | if call.SourcesStatus == SourcesNotProvided { |
| 43 | return `{"sources":[],"sources_status":"not_provided"}` |
| 44 | } |
| 45 | return FormatServerSearchOutput(call.Results) |
| 46 | } |
| 47 |