Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package web_search
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"academy/internal/types"
|
|
"academy/internal/types/interfaces"
|
|
)
|
|
|
|
const braveSearchURL = "https://api.search.brave.com/res/v1/web/search"
|
|
|
|
// BraveProvider queries the Brave Web Search API.
|
|
type BraveProvider struct {
|
|
client *http.Client
|
|
apiKey string
|
|
}
|
|
|
|
// NewBraveProvider creates a guarded, credential-scoped Brave search client.
|
|
func NewBraveProvider(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
|
|
if strings.TrimSpace(params.APIKey) == "" {
|
|
return nil, fmt.Errorf("API key is required for Brave provider")
|
|
}
|
|
client, err := NewSearchHTTPClient(30*time.Second, params.ProxyURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The official endpoint does not need redirects. Never forward its subscription
|
|
// token to a redirect destination, even another public host.
|
|
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
|
return &BraveProvider{client: client, apiKey: strings.TrimSpace(params.APIKey)}, nil
|
|
}
|
|
|
|
// Name returns the provider registry identifier.
|
|
func (p *BraveProvider) Name() string { return "brave" }
|
|
|
|
// Search applies the default region and result limit.
|
|
func (p *BraveProvider) Search(
|
|
ctx context.Context, query string, maxResults int, includeDate bool,
|
|
) ([]*types.WebSearchResult, error) {
|
|
return p.SearchWithFilters(ctx, query, maxResults, includeDate, types.WebSearchFilters{})
|
|
}
|
|
|
|
// SearchWithFilters sends country and freshness to the official Brave endpoint.
|
|
func (p *BraveProvider) SearchWithFilters(
|
|
ctx context.Context, query string, maxResults int, _ bool, filters types.WebSearchFilters,
|
|
) ([]*types.WebSearchResult, error) {
|
|
if err := filters.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.TrimSpace(query) == "" {
|
|
return nil, fmt.Errorf("query is empty")
|
|
}
|
|
if maxResults <= 0 {
|
|
maxResults = 5
|
|
}
|
|
maxResults = min(maxResults, 20)
|
|
params := url.Values{"q": {query}, "count": {strconv.Itoa(maxResults)}}
|
|
if country := strings.ToUpper(filters.Country); country != "" {
|
|
params.Set("country", country)
|
|
}
|
|
if filters.Freshness != "" {
|
|
params.Set("freshness", filters.Freshness)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, braveSearchURL+"?"+params.Encode(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("X-Subscription-Token", p.apiKey)
|
|
resp, err := p.client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("brave search request failed: %w", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return nil, fmt.Errorf("brave search returned HTTP %d", resp.StatusCode)
|
|
}
|
|
const maxResponse = 4 << 20
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, maxResponse+1))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read Brave response: %w", err)
|
|
}
|
|
if len(body) > maxResponse {
|
|
return nil, fmt.Errorf("brave response exceeds %d bytes", maxResponse)
|
|
}
|
|
var response struct {
|
|
Web struct {
|
|
Results []struct {
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
Description string `json:"description"`
|
|
Age string `json:"age"`
|
|
PageAge string `json:"page_age"`
|
|
} `json:"results"`
|
|
} `json:"web"`
|
|
}
|
|
if err := json.Unmarshal(body, &response); err != nil {
|
|
return nil, fmt.Errorf("decode Brave response: %w", err)
|
|
}
|
|
results := make([]*types.WebSearchResult, 0, min(maxResults, len(response.Web.Results)))
|
|
for _, row := range response.Web.Results {
|
|
age := row.Age
|
|
if age == "" {
|
|
age = row.PageAge
|
|
}
|
|
results = append(results, &types.WebSearchResult{
|
|
Title: row.Title, URL: row.URL, Snippet: row.Description, Source: p.Name(), Age: age,
|
|
})
|
|
if len(results) == maxResults {
|
|
break
|
|
}
|
|
}
|
|
return results, nil
|
|
}
|