yao/agent/search/citation.go
Max 6714ef9813 Refactor Citation Handling and Enhance Search Result Storage
- Updated the CitationGenerator to produce simple integer IDs instead of formatted strings, improving clarity and consistency in citation references.
- Enhanced the executeAutoSearch method to save both successful and failed search results, capturing detailed execution data for better traceability.
- Introduced a new SearchExecutionResult type to structure search result data, including query, keywords, configuration, duration, and error information.
- Updated related tests to reflect changes in citation ID format and ensure proper functionality of the new storage mechanisms.
- Revised documentation to clarify the new citation format and search result handling processes.
2025-12-15 17:17:51 +08:00

52 lines
1.2 KiB
Go

package search
import (
"sync/atomic"
)
// CitationGenerator generates unique citation IDs (1-based integers)
// Thread-safe for concurrent use within a single request
type CitationGenerator struct {
counter uint64
}
// NewCitationGenerator creates a new citation generator
func NewCitationGenerator() *CitationGenerator {
return &CitationGenerator{}
}
// Next generates the next citation ID (1, 2, 3, ...)
func (g *CitationGenerator) Next() string {
n := atomic.AddUint64(&g.counter, 1)
return uint64ToString(n)
}
// NextInt generates the next citation ID as integer
func (g *CitationGenerator) NextInt() int {
return int(atomic.AddUint64(&g.counter, 1))
}
// Current returns the current counter value without incrementing
func (g *CitationGenerator) Current() int {
return int(atomic.LoadUint64(&g.counter))
}
// Reset resets the counter (for testing)
func (g *CitationGenerator) Reset() {
atomic.StoreUint64(&g.counter, 0)
}
// uint64ToString converts uint64 to string without fmt package
func uint64ToString(n uint64) string {
if n == 0 {
return "0"
}
var buf [20]byte // max uint64 is 20 digits
i := len(buf)
for n > 0 {
i--
buf[i] = byte('0' + n%10)
n /= 10
}
return string(buf[i:])
}