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

131 lines
2.8 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 {
Query string `json:"query"`
NumResults int `json:"num_results"`
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-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",
Description: "Search the web via Exa in auto mode. Returns up to 10 results with short summaries.",
2025-08-14 03:53:14 +02:00
Parameters: map[string]any{
"type": "object",
2025-08-14 17:08:45 +02:00
"required": []string{"query", "num_results"},
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-14 17:08:45 +02:00
"description": "A concise, specific search query in natural language.",
},
"num_results": map[string]any{
"type": "integer",
"description": "Number of results to return (1-10). Default 10.",
"minimum": 1,
"maximum": 10,
},
},
"additionalProperties": false,
},
Strict: true,
},
},
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "fetch_contents",
Description: "Fetch page contents for one or more URLs via Exa /contents.",
Parameters: map[string]any{
"type": "object",
"required": []string{"urls"},
"properties": map[string]any{
"urls": map[string]any{
"type": "array",
"description": "List of URLs (1..N) to fetch.",
"items": map[string]any{"type": "string"},
2025-08-14 03:53:14 +02:00
},
},
"additionalProperties": false,
},
Strict: true,
},
},
}
}
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
err := json.Unmarshal([]byte(tool.Args), &arguments)
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
}
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
err := json.Unmarshal([]byte(tool.Args), &arguments)
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-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
}