Files
sub2api/backend/internal/pkg/websearch/exa.go
T
Anankke bf292550a6 feat(websearch): add Exa as a web search emulation provider
The web search emulation answers a WebSearch tool call on accounts without native search by querying a configured provider. Only Brave and Tavily are available. This adds Exa as a third keyed provider, "exa", in the same place the others are registered.

ExaProvider posts {query, numResults, contents: {highlights: true}} to https://api.exa.ai/search with the key in the x-api-key header. The search type and highlight length are left to Exa's defaults. Each result maps url and title as they are, publishedDate to page_age, and its highlights joined by newlines to the snippet. A result without highlights has no snippet. A non-200 answer is returned as an error with the status and the start of the body, as for Tavily.

"exa" is accepted by the emulation config validator, built by the manager and offered in the admin settings provider dropdown.
2026-11-14 04:10:03 -08:00

113 lines
2.8 KiB
Go

package websearch
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
const (
exaSearchEndpoint = "https://api.exa.ai/search"
exaProviderName = "exa"
)
// ExaProvider implements web search via the Exa Search API.
type ExaProvider struct {
apiKey string
httpClient *http.Client
}
// NewExaProvider creates an Exa Search provider.
// The caller is responsible for configuring the http.Client with proxy/timeouts.
func NewExaProvider(apiKey string, httpClient *http.Client) *ExaProvider {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &ExaProvider{apiKey: apiKey, httpClient: httpClient}
}
func (e *ExaProvider) Name() string { return exaProviderName }
func (e *ExaProvider) Search(ctx context.Context, req SearchRequest) (*SearchResponse, error) {
numResults := req.MaxResults
if numResults <= 0 {
numResults = defaultMaxResults
}
// Search type and highlight length are left to Exa's defaults.
payload := exaRequest{
Query: req.Query,
NumResults: numResults,
Contents: exaContents{Highlights: true},
}
bodyBytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("exa: encode request: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, exaSearchEndpoint, bytes.NewReader(bodyBytes))
if err != nil {
return nil, fmt.Errorf("exa: build request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("x-api-key", e.apiKey)
resp, err := e.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("exa: request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseSize))
if err != nil {
return nil, fmt.Errorf("exa: read body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("exa: status %d: %s", resp.StatusCode, truncateBody(body))
}
var raw exaResponse
if err := json.Unmarshal(body, &raw); err != nil {
return nil, fmt.Errorf("exa: decode response: %w", err)
}
results := make([]SearchResult, 0, len(raw.Results))
for _, r := range raw.Results {
results = append(results, SearchResult{
URL: r.URL,
Title: r.Title,
Snippet: strings.Join(r.Highlights, "\n"),
PageAge: r.PublishedDate,
})
}
return &SearchResponse{Results: results, Query: req.Query}, nil
}
type exaRequest struct {
Query string `json:"query"`
NumResults int `json:"numResults"`
Contents exaContents `json:"contents"`
}
type exaContents struct {
Highlights bool `json:"highlights"`
}
type exaResponse struct {
Results []exaResult `json:"results"`
}
type exaResult struct {
URL string `json:"url"`
Title string `json:"title"`
PublishedDate string `json:"publishedDate"`
Highlights []string `json:"highlights"`
}