1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-09 09:19:54 +00:00
Files
whiskr/search.go

222 lines
5.3 KiB
Go
Raw Normal View History

2025-08-14 03:53:14 +02:00
package main
import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"github.com/revrost/go-openrouter"
)
2025-08-14 17:08:45 +02:00
type SearchWebArguments struct {
2025-08-31 00:25:03 +02:00
Query string `json:"query"`
NumResults int `json:"num_results,omitempty"`
Intent string `json:"intent,omitempty"`
Recency string `json:"recency,omitempty"`
Domains []string `json:"domains,omitempty"`
2025-08-14 03:53:14 +02:00
}
2025-08-14 17:08:45 +02:00
type FetchContentsArguments struct {
URLs []string `json:"urls"`
}
2025-08-14 03:53:14 +02:00
2025-08-25 18:37:30 +02:00
type GitHubRepositoryArguments struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
}
2025-08-14 17:08:45 +02:00
func GetSearchTools() []openrouter.Tool {
2025-08-14 03:53:14 +02:00
return []openrouter.Tool{
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
2025-08-14 17:08:45 +02:00
Name: "search_web",
2025-08-31 00:25:03 +02:00
Description: "Search the live web (via Exa /search) and return summaries, highlights, and optionally full text for the top results.",
2025-08-14 03:53:14 +02:00
Parameters: map[string]any{
"type": "object",
2025-08-31 00:25:03 +02:00
"required": []string{"query"},
2025-08-14 03:53:14 +02:00
"properties": map[string]any{
2025-08-14 17:08:45 +02:00
"query": map[string]any{
2025-08-14 03:53:14 +02:00
"type": "string",
2025-08-31 00:25:03 +02:00
"description": "A concise, specific search query in natural language. Include month/year if recency matters (e.g., 'august 2025').",
2025-08-14 17:08:45 +02:00
},
"num_results": map[string]any{
"type": "integer",
2025-08-31 00:25:03 +02:00
"description": "Number of results to return (3-12). Default is 6.",
2025-08-26 00:22:32 +02:00
"minimum": 3,
2025-08-14 17:08:45 +02:00
"maximum": 10,
},
2025-08-31 00:25:03 +02:00
"intent": map[string]any{
"type": "string",
"enum": []string{"auto", "news", "docs", "papers", "code", "deep_read"},
"description": "Search profile. Use 'news' for breaking topics, 'docs' for official docs/changelogs, 'papers' for research, 'code' for repos, 'deep_read' when you need exact quotes/numbers (adds full text). Default 'auto'.",
},
"recency": map[string]any{
"type": "string",
"enum": []string{"auto", "month", "year", "range"},
"description": "Time filter hint. 'month' ~ last 30 days, 'year' ~ last 365 days. Default 'auto'.",
},
"domains": map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"description": "Restrict to these domains (e.g., ['europa.eu', 'who.int']).",
},
2025-08-14 17:08:45 +02:00
},
"additionalProperties": false,
},
},
},
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "fetch_contents",
2025-08-31 00:25:03 +02:00
Description: "Fetch and summarize page contents for one or more URLs (via Exa /contents). Use when the user provides specific links.",
2025-08-14 17:08:45 +02:00
Parameters: map[string]any{
"type": "object",
"required": []string{"urls"},
"properties": map[string]any{
"urls": map[string]any{
"type": "array",
2025-08-31 00:25:03 +02:00
"description": "List of URLs to fetch.",
"items": map[string]any{
"type": "string",
},
"minItems": 1,
"maxItems": 5,
2025-08-14 03:53:14 +02:00
},
},
"additionalProperties": false,
},
Strict: true,
},
},
2025-08-25 18:37:30 +02:00
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "github_repository",
2025-08-31 00:25:03 +02:00
Description: "Fetch repository metadata and README from GitHub.",
2025-08-25 18:37:30 +02:00
Parameters: map[string]any{
"type": "object",
"required": []string{"owner", "repo"},
"properties": map[string]any{
"owner": map[string]any{
"type": "string",
2025-08-31 00:25:03 +02:00
"description": "Repository owner (e.g., 'torvalds').",
2025-08-25 18:37:30 +02:00
},
"repo": map[string]any{
"type": "string",
"description": "Repository name (e.g., 'linux').",
},
},
"additionalProperties": false,
},
Strict: true,
},
},
2025-08-14 03:53:14 +02:00
}
}
2025-08-14 17:08:45 +02:00
func HandleSearchWebTool(ctx context.Context, tool *ToolCall) error {
var arguments SearchWebArguments
2025-08-14 03:53:14 +02:00
2025-08-31 00:25:03 +02:00
err := ParseAndUpdateArgs(tool, &arguments)
2025-08-14 03:53:14 +02:00
if err != nil {
return err
}
if arguments.Query == "" {
return errors.New("no search query")
}
2025-08-14 17:08:45 +02:00
results, err := ExaRunSearch(ctx, arguments)
if err != nil {
tool.Result = fmt.Sprintf("error: %v", err)
return nil
}
2025-08-28 14:46:28 +02:00
tool.Cost = results.Cost.Total
2025-08-14 17:08:45 +02:00
if len(results.Results) == 0 {
tool.Result = "error: no search results"
return nil
}
tool.Result = results.String()
return nil
}
func HandleFetchContentsTool(ctx context.Context, tool *ToolCall) error {
var arguments FetchContentsArguments
2025-08-31 00:25:03 +02:00
err := ParseAndUpdateArgs(tool, &arguments)
2025-08-14 17:08:45 +02:00
if err != nil {
return err
}
if len(arguments.URLs) == 0 {
return errors.New("no urls")
2025-08-14 03:53:14 +02:00
}
2025-08-14 17:08:45 +02:00
results, err := ExaRunContents(ctx, arguments)
2025-08-14 03:53:14 +02:00
if err != nil {
tool.Result = fmt.Sprintf("error: %v", err)
return nil
}
2025-08-28 14:46:28 +02:00
tool.Cost = results.Cost.Total
2025-08-14 17:08:45 +02:00
if len(results.Results) == 0 {
tool.Result = "error: no search results"
2025-08-14 03:53:14 +02:00
return nil
}
2025-08-14 17:08:45 +02:00
tool.Result = results.String()
2025-08-14 03:53:14 +02:00
return nil
}
2025-08-25 18:37:30 +02:00
func HandleGitHubRepositoryTool(ctx context.Context, tool *ToolCall) error {
var arguments GitHubRepositoryArguments
2025-08-31 00:25:03 +02:00
err := ParseAndUpdateArgs(tool, &arguments)
2025-08-25 18:37:30 +02:00
if err != nil {
return err
}
result, err := RepoOverview(ctx, arguments)
if err != nil {
tool.Result = fmt.Sprintf("error: %v", err)
return nil
}
tool.Result = result
return nil
}
2025-08-31 00:25:03 +02:00
func ParseAndUpdateArgs(tool *ToolCall, arguments any) error {
err := json.Unmarshal([]byte(tool.Args), arguments)
if err != nil {
return err
}
b, err := json.Marshal(arguments)
if err != nil {
return err
}
tool.Args = string(b)
return nil
}