Changeset 0.13.1 (#65)
This commit is contained in:
73
server/tools/websearch.go
Normal file
73
server/tools/websearch.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/tools/search"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register(&WebSearchTool{})
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// web_search
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
type WebSearchTool struct{}
|
||||
|
||||
func (t *WebSearchTool) Definition() ToolDef {
|
||||
return ToolDef{
|
||||
Name: "web_search",
|
||||
DisplayName: "Search",
|
||||
Description: "Search the web for current information. Use this when you need up-to-date facts, news, or information you're not confident about. Returns titles, URLs, and text snippets from web pages.",
|
||||
Category: "search",
|
||||
Parameters: JSONSchema(map[string]interface{}{
|
||||
"query": Prop("string", "Search query — be specific and concise"),
|
||||
"max_results": Prop("integer", "Maximum number of results to return (1-10, default 5)"),
|
||||
}, []string{"query"}),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *WebSearchTool) Execute(ctx context.Context, execCtx ExecutionContext, argsJSON string) (string, error) {
|
||||
var args struct {
|
||||
Query string `json:"query"`
|
||||
MaxResults int `json:"max_results"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(argsJSON), &args); err != nil {
|
||||
return "", fmt.Errorf("invalid arguments: %w", err)
|
||||
}
|
||||
|
||||
if args.Query == "" {
|
||||
return "", fmt.Errorf("query is required")
|
||||
}
|
||||
|
||||
if args.MaxResults <= 0 {
|
||||
args.MaxResults = 5
|
||||
}
|
||||
if args.MaxResults > 10 {
|
||||
args.MaxResults = 10
|
||||
}
|
||||
|
||||
provider := search.Active()
|
||||
if provider == nil {
|
||||
return "", fmt.Errorf("no search provider configured")
|
||||
}
|
||||
|
||||
results, err := provider.Search(ctx, args.Query, args.MaxResults)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("search failed: %w", err)
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"provider": provider.Name(),
|
||||
"query": args.Query,
|
||||
"result_count": len(results),
|
||||
"results": results,
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(response)
|
||||
return string(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user