1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-08 17:06:42 +00:00
Files
whiskr/search.go

179 lines
4.0 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-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",
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",
2025-08-26 00:22:32 +02:00
"description": "Number of results to return (3-10). Default to 6.",
"minimum": 3,
2025-08-14 17:08:45 +02:00
"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-25 18:37:30 +02:00
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "github_repository",
Description: "Get a quick overview of a GitHub repository without cloning: repo info, up to 20 branches (popular first), top-level files/dirs, and the README.",
Parameters: map[string]any{
"type": "object",
"required": []string{"owner", "repo"},
"properties": map[string]any{
"owner": map[string]any{
"type": "string",
"description": "GitHub username or organization (e.g., 'torvalds').",
},
"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
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
}
2025-08-25 18:37:30 +02:00
func HandleGitHubRepositoryTool(ctx context.Context, tool *ToolCall) error {
var arguments GitHubRepositoryArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
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
}