The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
164 lines
4.5 KiB
Go
164 lines
4.5 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package search
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"go.probo.inc/probo/pkg/agent"
|
|
)
|
|
|
|
const firecrawlBaseURL = "https://api.firecrawl.dev/v2"
|
|
|
|
type (
|
|
searchResult struct {
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
Snippet string `json:"snippet"`
|
|
}
|
|
|
|
firecrawlParams struct {
|
|
Query string `json:"query" jsonschema:"The search query to execute"`
|
|
MaxResults int `json:"max_results" jsonschema:"Maximum number of results to return (default 5, max 10)"`
|
|
}
|
|
|
|
firecrawlRequest struct {
|
|
Query string `json:"query"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
|
|
firecrawlResponse struct {
|
|
Success bool `json:"success"`
|
|
Data struct {
|
|
Web []firecrawlWebResult `json:"web"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
firecrawlWebResult struct {
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
URL string `json:"url"`
|
|
}
|
|
)
|
|
|
|
// FirecrawlSearchTool creates a tool that searches the web using the Firecrawl
|
|
// API. The apiKey is used for Bearer authentication.
|
|
func FirecrawlSearchTool(apiKey string) agent.Tool {
|
|
client := newHTTPClient()
|
|
|
|
return agent.FunctionTool(
|
|
"web_search",
|
|
"Search the web for information about a topic. Returns a list of results with title, URL, and snippet. Use this to find news, reviews, breach reports, regulatory actions, and other external information about a vendor.",
|
|
func(ctx context.Context, p firecrawlParams) (agent.ToolResult, error) {
|
|
maxResults := p.MaxResults
|
|
if maxResults <= 0 {
|
|
maxResults = 5
|
|
}
|
|
|
|
if maxResults > 10 {
|
|
maxResults = 10
|
|
}
|
|
|
|
results, err := firecrawlSearch(ctx, client, apiKey, p.Query, maxResults)
|
|
if err != nil {
|
|
return agent.ResultErrorf("search request failed: %s", err), nil
|
|
}
|
|
|
|
return agent.ResultJSON(results), nil
|
|
},
|
|
)
|
|
}
|
|
|
|
func firecrawlSearch(
|
|
ctx context.Context,
|
|
client *http.Client,
|
|
apiKey, query string,
|
|
maxResults int,
|
|
) ([]searchResult, error) {
|
|
u, err := url.JoinPath(firecrawlBaseURL, "search")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot build search URL: %w", err)
|
|
}
|
|
|
|
body, err := json.Marshal(
|
|
firecrawlRequest{
|
|
Query: query,
|
|
Limit: maxResults,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot marshal request: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot create request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot execute search request: %w", err)
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot read response body: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("search returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
var fcResp firecrawlResponse
|
|
if err := json.Unmarshal(respBody, &fcResp); err != nil {
|
|
return nil, fmt.Errorf("cannot unmarshal response: %w", err)
|
|
}
|
|
|
|
if !fcResp.Success {
|
|
return nil, fmt.Errorf("search returned success=false")
|
|
}
|
|
|
|
results := make([]searchResult, 0, len(fcResp.Data.Web))
|
|
for _, r := range fcResp.Data.Web {
|
|
results = append(
|
|
results,
|
|
searchResult{
|
|
Title: r.Title,
|
|
URL: r.URL,
|
|
Snippet: r.Description,
|
|
},
|
|
)
|
|
}
|
|
|
|
return results, nil
|
|
}
|