tool

package
v0.0.0-...-cf5b36a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the default global registry.

Functions

func FormatResults

func FormatResults(results []SearchResult, query string) string

FormatResults formats search results into a readable string. It limits the output size to avoid exceeding token limits.

func RegisterDefaultTools

func RegisterDefaultTools() error

RegisterDefaultTools registers all default search tools.

Types

type Article

type Article struct {
	Title       string
	SogouURL    string
	RealURL     string
	PublishTime string
	Content     string
}

Article represents a WeChat article search result.

type BaseSearchTool

type BaseSearchTool struct {
	ToolName        string
	ToolDescription string
	UserAgent       string
}

BaseSearchTool provides common functionality for search tools.

func (*BaseSearchTool) GetDefaultUserAgent

func (b *BaseSearchTool) GetDefaultUserAgent() string

GetDefaultUserAgent returns the default user agent.

func (*BaseSearchTool) SetUserAgent

func (b *BaseSearchTool) SetUserAgent(ua string)

SetUserAgent sets a custom user agent.

type Category

type Category string

Category represents a search source category.

const (
	CategorySocial   Category = "social"   // 社交媒体和社区 (知乎、微信)
	CategoryCode     Category = "code"     // 代码和开发 (GitHub)
	CategoryAcademic Category = "academic" // 学术和科研 (Google Scholar)
	CategoryGeneral  Category = "general"  // 通用搜索
	CategoryAll      Category = "all"      // 所有分类
)

type GitHubOption

type GitHubOption func(*GitHubSearch)

GitHubOption is a function type for configuring GitHubSearch.

func WithGitHubTimeout

func WithGitHubTimeout(timeout time.Duration) GitHubOption

WithGitHubTimeout sets the request timeout.

func WithGitHubToken

func WithGitHubToken(token string) GitHubOption

WithGitHubToken sets the GitHub Personal Access Token.

func WithGitHubUserAgent

func WithGitHubUserAgent(ua string) GitHubOption

WithGitHubUserAgent sets a custom User-Agent header.

type GitHubSearch

type GitHubSearch struct {
	UserAgent string
	Timeout   time.Duration
	Token     string // GitHub Personal Access Token (optional but recommended)
}

GitHubSearch is a tool for searching GitHub repositories, code, and issues.

func NewGitHubSearch

func NewGitHubSearch(opts ...GitHubOption) (*GitHubSearch, error)

NewGitHubSearch creates a new GitHubSearch tool.

func (*GitHubSearch) Call

func (g *GitHubSearch) Call(ctx context.Context, input string) (string, error)

Call executes the search and returns formatted results.

func (*GitHubSearch) Description

func (g *GitHubSearch) Description() string

Description returns the description of the tool.

func (*GitHubSearch) Name

func (g *GitHubSearch) Name() string

Name returns the name of the tool.

func (*GitHubSearch) Search

func (g *GitHubSearch) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search implements the SearchTool interface.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages all available search tools.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new tool registry.

func (*Registry) DescribeAll

func (r *Registry) DescribeAll() string

DescribeAll returns descriptions of all registered tools.

func (*Registry) Get

func (r *Registry) Get(name string) (SearchTool, bool)

Get retrieves a tool by name.

func (*Registry) GetByCategory

func (r *Registry) GetByCategory(category Category) []SearchTool

GetByCategory returns all tools in a specific category.

func (*Registry) List

func (r *Registry) List() []string

List returns all registered tool names.

func (*Registry) Register

func (r *Registry) Register(category Category, tool SearchTool) error

Register adds a search tool to the registry.

func (*Registry) SearchAll

func (r *Registry) SearchAll(ctx context.Context, category Category, query string, limitPerTool int) ([]SearchResult, error)

SearchAll searches using all tools in the specified category.

type ScholarOption

type ScholarOption func(*ScholarSearch)

ScholarOption is a function type for configuring ScholarSearch.

func WithScholarTimeout

func WithScholarTimeout(timeout time.Duration) ScholarOption

WithScholarTimeout sets the request timeout.

func WithScholarUserAgent

func WithScholarUserAgent(ua string) ScholarOption

WithScholarUserAgent sets a custom User-Agent header.

type ScholarPaper

type ScholarPaper struct {
	Title     string
	URL       string
	Authors   string
	Year      string
	Abstract  string
	Source    string
	Citations int
}

ScholarPaper represents a paper from Google Scholar.

type ScholarSearch

type ScholarSearch struct {
	UserAgent string
	Timeout   time.Duration
}

ScholarSearch is a tool for searching academic papers via Google Scholar.

func NewScholarSearch

func NewScholarSearch(opts ...ScholarOption) (*ScholarSearch, error)

NewScholarSearch creates a new ScholarSearch tool.

func (*ScholarSearch) Call

func (s *ScholarSearch) Call(ctx context.Context, input string) (string, error)

Call executes the search and returns formatted results.

func (*ScholarSearch) Description

func (s *ScholarSearch) Description() string

Description returns the description of the tool.

func (*ScholarSearch) Name

func (s *ScholarSearch) Name() string

Name returns the name of the tool.

func (*ScholarSearch) Search

func (s *ScholarSearch) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search implements the SearchTool interface.

type SearchResult

type SearchResult struct {
	Title       string // 标题
	URL         string // 链接
	Author      string // 作者/发布者
	PublishTime string // 发布时间
	Summary     string // 内容摘要
	Content     string // 完整内容(可选)
	Source      string // 来源平台
}

SearchResult represents a unified search result across different sources.

type SearchTool

type SearchTool interface {
	// Name returns the tool name
	Name() string

	// Description returns what this tool does
	Description() string

	// Call executes the search with the given input query
	// Returns formatted results as a string
	Call(ctx context.Context, input string) (string, error)

	// Search performs the actual search and returns structured results
	// This can be used when you need programmatic access to the results
	Search(ctx context.Context, query string, limit int) ([]SearchResult, error)
}

SearchTool is the unified interface for all search tools.

type SogouSearchResult

type SogouSearchResult struct {
	Title       string
	SogouURL    string
	PublishTime string
}

SogouSearchResult represents a search result from Sogou WeChat search.

type TavilyOption

type TavilyOption func(*TavilySearch)

TavilyOption is a function type for configuring TavilySearch.

func WithTavilyAPIKey

func WithTavilyAPIKey(apiKey string) TavilyOption

WithTavilyAPIKey sets the Tavily API key.

func WithTavilyMaxResults

func WithTavilyMaxResults(maxResults int) TavilyOption

WithTavilyMaxResults sets the maximum number of results to return.

func WithTavilySearchDepth

func WithTavilySearchDepth(depth string) TavilyOption

WithTavilySearchDepth sets the search depth ("basic" or "advanced").

func WithTavilyTimeout

func WithTavilyTimeout(timeout time.Duration) TavilyOption

WithTavilyTimeout sets the request timeout.

func WithTavilyUserAgent

func WithTavilyUserAgent(ua string) TavilyOption

WithTavilyUserAgent sets a custom User-Agent header.

type TavilyResponse

type TavilyResponse struct {
	Answer  string         `json:"answer"`
	Query   string         `json:"query"`
	Results []TavilyResult `json:"results"`
}

TavilyResponse represents the response from Tavily API.

type TavilyResult

type TavilyResult struct {
	Title         string  `json:"title"`
	URL           string  `json:"url"`
	Content       string  `json:"content"`
	Score         float64 `json:"score"`
	RawContent    string  `json:"raw_content"`
	Snippet       string  `json:"snippet"`
	PublishedDate string  `json:"published_date"`
	Author        string  `json:"author"`
}

TavilyResult represents a single search result from Tavily.

type TavilySearch

type TavilySearch struct {
	APIKey      string
	UserAgent   string
	Timeout     time.Duration
	MaxResults  int
	SearchDepth string // "basic" or "advanced"
}

TavilySearch is a tool for searching using Tavily API. Tavily is a search API specifically designed for AI agents.

func NewTavilySearch

func NewTavilySearch(opts ...TavilyOption) (*TavilySearch, error)

NewTavilySearch creates a new TavilySearch tool.

func (*TavilySearch) Call

func (t *TavilySearch) Call(ctx context.Context, input string) (string, error)

Call executes the search and returns formatted results.

func (*TavilySearch) Description

func (t *TavilySearch) Description() string

Description returns the description of the tool.

func (*TavilySearch) Name

func (t *TavilySearch) Name() string

Name returns the name of the tool.

func (*TavilySearch) Search

func (t *TavilySearch) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search implements the SearchTool interface.

type WeChatOption

type WeChatOption func(*WeChatSearch)

WeChatOption is a function type for configuring WeChatSearch.

func WithWeChatCount

func WithWeChatCount(count int) WeChatOption

WithWeChatCount sets the number of articles to fetch (1-10).

func WithWeChatUserAgent

func WithWeChatUserAgent(ua string) WeChatOption

WithWeChatUserAgent sets a custom User-Agent header.

type WeChatSearch

type WeChatSearch struct {
	Count     int
	UserAgent string
}

WeChatSearch is a tool for searching WeChat official account articles via Sogou.

func NewWeChatSearch

func NewWeChatSearch(opts ...WeChatOption) (*WeChatSearch, error)

NewWeChatSearch creates a new WeChatSearch tool.

func (*WeChatSearch) Call

func (w *WeChatSearch) Call(ctx context.Context, input string) (string, error)

Call executes the search and returns formatted article results.

func (*WeChatSearch) Description

func (w *WeChatSearch) Description() string

Description returns the description of the tool.

func (*WeChatSearch) Name

func (w *WeChatSearch) Name() string

Name returns the name of the tool.

func (*WeChatSearch) Search

func (w *WeChatSearch) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search implements the SearchTool interface.

func (*WeChatSearch) SearchArticles

func (w *WeChatSearch) SearchArticles(ctx context.Context, query string, count int) ([]Article, error)

SearchArticles searches WeChat articles via Sogou and returns the results.

type ZhihuOption

type ZhihuOption func(*ZhihuSearch)

ZhihuOption is a function type for configuring ZhihuSearch.

func WithZhihuTimeout

func WithZhihuTimeout(timeout time.Duration) ZhihuOption

WithZhihuTimeout sets the request timeout.

func WithZhihuUserAgent

func WithZhihuUserAgent(ua string) ZhihuOption

WithZhihuUserAgent sets a custom User-Agent header.

type ZhihuSearch

type ZhihuSearch struct {
	UserAgent string
	Timeout   time.Duration
}

ZhihuSearch is a tool for searching Zhihu questions and answers.

func NewZhihuSearch

func NewZhihuSearch(opts ...ZhihuOption) (*ZhihuSearch, error)

NewZhihuSearch creates a new ZhihuSearch tool.

func (*ZhihuSearch) Call

func (z *ZhihuSearch) Call(ctx context.Context, input string) (string, error)

Call executes the search and returns formatted results.

func (*ZhihuSearch) Description

func (z *ZhihuSearch) Description() string

Description returns the description of the tool.

func (*ZhihuSearch) Name

func (z *ZhihuSearch) Name() string

Name returns the name of the tool.

func (*ZhihuSearch) Search

func (z *ZhihuSearch) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search implements the SearchTool interface.

type ZhihuSearchResult

type ZhihuSearchResult struct {
	Title       string
	URL         string
	Summary     string
	PublishTime string
}

ZhihuSearchResult represents a search result from Zhihu.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL