- Refactored the SearchAPI interface to replace the Parallel method with All, Any, and Race methods, inspired by JavaScript Promise patterns. - Updated the Searcher struct to implement these new parallel search methods, improving flexibility and performance in executing multiple searches. - Revised the JSAPI implementation to support the new parallel search methods, ensuring consistency across the API. - Enhanced documentation in DESIGN.md to detail the new parallel search functionalities and provide usage examples, clarifying their behavior and expected outcomes.
154 lines
4.6 KiB
Go
154 lines
4.6 KiB
Go
package search
|
|
|
|
import (
|
|
"github.com/yaoapp/yao/agent/context"
|
|
"github.com/yaoapp/yao/agent/search/types"
|
|
)
|
|
|
|
// JSAPI implements context.SearchAPI interface
|
|
// Provides ctx.search.Web(), ctx.search.KB(), ctx.search.DB(), ctx.search.All(), ctx.search.Any(), ctx.search.Race()
|
|
type JSAPI struct {
|
|
ctx *context.Context
|
|
config *types.Config
|
|
uses *Uses
|
|
}
|
|
|
|
// NewJSAPI creates a new search JSAPI instance
|
|
func NewJSAPI(ctx *context.Context, config *types.Config, uses *Uses) *JSAPI {
|
|
return &JSAPI{
|
|
ctx: ctx,
|
|
config: config,
|
|
uses: uses,
|
|
}
|
|
}
|
|
|
|
// Web executes web search
|
|
// Options:
|
|
// - limit: int - max results (default: 10)
|
|
// - sites: []string - restrict to specific sites
|
|
// - time_range: string - "day", "week", "month", "year"
|
|
// - rerank: map[string]interface{} - rerank options
|
|
func (api *JSAPI) Web(query string, opts map[string]interface{}) interface{} {
|
|
// TODO: Implement web search
|
|
// 1. Build Request from query and opts
|
|
// 2. Call web handler
|
|
// 3. Return Result or error
|
|
return &types.Result{
|
|
Type: types.SearchTypeWeb,
|
|
Query: query,
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
|
|
// KB executes knowledge base search
|
|
// Options:
|
|
// - collections: []string - collection IDs
|
|
// - threshold: float64 - similarity threshold (0-1)
|
|
// - limit: int - max results
|
|
// - graph: bool - enable graph association
|
|
// - rerank: map[string]interface{} - rerank options
|
|
func (api *JSAPI) KB(query string, opts map[string]interface{}) interface{} {
|
|
// TODO: Implement KB search
|
|
// 1. Build Request from query and opts
|
|
// 2. Call KB handler
|
|
// 3. Return Result or error
|
|
return &types.Result{
|
|
Type: types.SearchTypeKB,
|
|
Query: query,
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
|
|
// DB executes database search
|
|
// Options:
|
|
// - models: []string - model IDs
|
|
// - wheres: []map[string]interface{} - pre-defined filters (GOU QueryDSL Where format)
|
|
// - orders: []map[string]interface{} - sort orders (GOU QueryDSL Order format)
|
|
// - select: []string - fields to return
|
|
// - limit: int - max results
|
|
// - rerank: map[string]interface{} - rerank options
|
|
func (api *JSAPI) DB(query string, opts map[string]interface{}) interface{} {
|
|
// TODO: Implement DB search
|
|
// 1. Build Request from query and opts
|
|
// 2. Call DB handler
|
|
// 3. Return Result or error
|
|
return &types.Result{
|
|
Type: types.SearchTypeDB,
|
|
Query: query,
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
|
|
// All executes all searches and waits for all to complete (like Promise.all)
|
|
// Each request should have:
|
|
// - type: string - "web", "kb", or "db"
|
|
// - query: string - search query
|
|
// - ... other type-specific options
|
|
func (api *JSAPI) All(requests []interface{}) []interface{} {
|
|
// TODO: Implement All search
|
|
// 1. Parse requests into []Request
|
|
// 2. Call Searcher.All()
|
|
// 3. Return []Result
|
|
results := make([]interface{}, len(requests))
|
|
for i := range requests {
|
|
results[i] = &types.Result{
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// Any returns as soon as any search succeeds with results (like Promise.any)
|
|
// Each request should have:
|
|
// - type: string - "web", "kb", or "db"
|
|
// - query: string - search query
|
|
// - ... other type-specific options
|
|
func (api *JSAPI) Any(requests []interface{}) []interface{} {
|
|
// TODO: Implement Any search
|
|
// 1. Parse requests into []Request
|
|
// 2. Call Searcher.Any()
|
|
// 3. Return []Result
|
|
results := make([]interface{}, len(requests))
|
|
for i := range requests {
|
|
results[i] = &types.Result{
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// Race returns as soon as any search completes (like Promise.race)
|
|
// Each request should have:
|
|
// - type: string - "web", "kb", or "db"
|
|
// - query: string - search query
|
|
// - ... other type-specific options
|
|
func (api *JSAPI) Race(requests []interface{}) []interface{} {
|
|
// TODO: Implement Race search
|
|
// 1. Parse requests into []Request
|
|
// 2. Call Searcher.Race()
|
|
// 3. Return []Result
|
|
results := make([]interface{}, len(requests))
|
|
for i := range requests {
|
|
results[i] = &types.Result{
|
|
Error: "not implemented",
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// init registers the JSAPI factory with context package
|
|
func init() {
|
|
// Note: The actual factory is set by assistant package during initialization
|
|
// This avoids circular dependency: context -> search -> context
|
|
// See: assistant/assistant.go init()
|
|
}
|
|
|
|
// SetJSAPIFactory sets the factory function for creating SearchAPI instances
|
|
// Called by assistant package during initialization
|
|
func SetJSAPIFactory() {
|
|
context.SearchAPIFactory = func(ctx *context.Context) context.SearchAPI {
|
|
// Get config and uses from context or use defaults
|
|
// TODO: Get actual config from assistant
|
|
return NewJSAPI(ctx, nil, nil)
|
|
}
|
|
}
|