Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/search"

Package search is the seam WebSearch reaches a search engine through. Hosts inject a backend, such as a provider’s native search adapter, independently of the session model. Without a backend the tool is absent.

type Backend interface {
Search(ctx context.Context, q Query) (Response, error)
}

Backend searches the web. Implementations must be safe for concurrent use and honour ctx.

Example

var backend search.Backend = index{
{Title: "Go documentation", URL: "https://go.dev/doc/", Snippet: "Documentation for Go."},
{Title: "Other reference", URL: "https://example.org/reference", Snippet: "A different source."},
}
results, err := backend.Search(context.Background(), search.Query{Text: "Go documentation", Max: 1, AllowedDomains: []string{"go.dev"}})
if err != nil {
panic(err)
}
for _, result := range results.Results {
fmt.Println(result.Title, result.URL)
}
// Output: Go documentation https://go.dev/doc/

Output:

Go documentation https://go.dev/doc/
type Limiter interface {
Limit() event.Tokens
}

Limiter is an optional Backend capability for model-backed searches. Limit bounds all input (including server search context) and output tokens per call. Zero means unknown; hard budgets refuse unknown bounds. Implementations must be safe for concurrent use.

Example

var backend search.Backend = boundedIndex{}
if limiter, ok := backend.(search.Limiter); ok {
limit := limiter.Limit()
fmt.Println(limit.Input, limit.Output)
}
// Output: 200000 2048

Output:

200000 2048
type Query struct {
Text string
// Max is the most results wanted.
Max int
// AllowedDomains, if any, limits results to these domains;
// BlockedDomains excludes some.
AllowedDomains, BlockedDomains []string
}

Query is one search.

type Response struct {
Results []Result
Usage *event.Usage
}

Response is the results and usage of a search. Usage may be non-nil even when Search returns an error, if the provider charged for the request.

type Result struct {
Title, URL, Snippet string
}

Result is one search result.