runSearchChain tries each provider in order, returning the first non-empty result set plus the provider that produced it. lastErr holds the final error when every backend failed.
(ctx context.Context, query string, maxResults int, onOutput func(string))
| 192 | // non-empty result set plus the provider that produced it. lastErr holds |
| 193 | // the final error when every backend failed. |
| 194 | func runSearchChain(ctx context.Context, query string, maxResults int, onOutput func(string)) ([]searchResult, SearchProvider, error) { |
| 195 | chain := SelectSearchChain() |
| 196 | var lastErr error |
| 197 | |
| 198 | for i, p := range chain { |
| 199 | if onOutput != nil { |
| 200 | onOutput(fmt.Sprintf("Searching %s: %s...", p.name, query)) |
| 201 | } |
| 202 | res, err := p.search(ctx, query, maxResults) |
| 203 | if err == nil && len(res) > 0 { |
| 204 | return res, p.name, nil |
| 205 | } |
| 206 | lastErr = err |
| 207 | if onOutput != nil && i < len(chain)-1 { |
| 208 | if err != nil { |
| 209 | onOutput(fmt.Sprintf("%s failed (%v), falling back to %s...", p.name, err, chain[i+1].name)) |
| 210 | } else { |
| 211 | onOutput(fmt.Sprintf("%s returned no results, falling back to %s...", p.name, chain[i+1].name)) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return nil, "", lastErr |
| 216 | } |
| 217 | |
| 218 | // formatSearchResults renders the result set for the agent and streams a |
| 219 | // one-line-per-hit preview through onOutput. |
no test coverage detected