- 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.
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package kb
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/yaoapp/yao/agent/search/types"
|
|
)
|
|
|
|
// Handler implements KB search
|
|
type Handler struct {
|
|
config *types.KBConfig // KB search configuration
|
|
}
|
|
|
|
// NewHandler creates a new KB search handler
|
|
func NewHandler(cfg *types.KBConfig) *Handler {
|
|
return &Handler{config: cfg}
|
|
}
|
|
|
|
// Type returns the search type this handler supports
|
|
func (h *Handler) Type() types.SearchType {
|
|
return types.SearchTypeKB
|
|
}
|
|
|
|
// Search executes vector search and optional graph association
|
|
// TODO: Implement actual vector search and graph association logic
|
|
func (h *Handler) Search(req *types.Request) (*types.Result, error) {
|
|
start := time.Now()
|
|
|
|
// Validate request
|
|
if req.Query == "" {
|
|
return &types.Result{
|
|
Type: types.SearchTypeKB,
|
|
Query: req.Query,
|
|
Source: req.Source,
|
|
Items: []*types.ResultItem{},
|
|
Total: 0,
|
|
Duration: time.Since(start).Milliseconds(),
|
|
Error: "query is required",
|
|
}, nil
|
|
}
|
|
|
|
// Get collections from request or config
|
|
collections := req.Collections
|
|
if len(collections) == 0 && h.config != nil {
|
|
collections = h.config.Collections
|
|
}
|
|
|
|
// If no collections specified, return empty result
|
|
if len(collections) == 0 {
|
|
return &types.Result{
|
|
Type: types.SearchTypeKB,
|
|
Query: req.Query,
|
|
Source: req.Source,
|
|
Items: []*types.ResultItem{},
|
|
Total: 0,
|
|
Duration: time.Since(start).Milliseconds(),
|
|
}, nil
|
|
}
|
|
|
|
// Get threshold from request or config
|
|
threshold := req.Threshold
|
|
if threshold == 0 && h.config != nil && h.config.Threshold > 0 {
|
|
threshold = h.config.Threshold
|
|
}
|
|
if threshold == 0 {
|
|
threshold = 0.7 // default
|
|
}
|
|
|
|
// Get limit
|
|
limit := req.Limit
|
|
if limit == 0 {
|
|
limit = 10 // default
|
|
}
|
|
|
|
// TODO: Implement actual vector search
|
|
// 1. Generate embedding for query using collection's embedding config
|
|
// 2. Search each collection with vector similarity
|
|
// 3. If req.Graph is true, perform graph association
|
|
// 4. Merge and return results
|
|
|
|
// For now, return empty result (skeleton)
|
|
result := &types.Result{
|
|
Type: types.SearchTypeKB,
|
|
Query: req.Query,
|
|
Source: req.Source,
|
|
Items: []*types.ResultItem{},
|
|
Total: 0,
|
|
Duration: time.Since(start).Milliseconds(),
|
|
}
|
|
|
|
// Store threshold in result metadata for debugging
|
|
_ = threshold
|
|
|
|
return result, nil
|
|
}
|