fix(web-search): polish Keenable provider integration after #1946

Expose optional API key in settings UI, map longer API snippets into Content
for RAG, align maxResults fallback with other providers, sync Swagger enum,
and add follow-up unit tests.
This commit is contained in:
wizardchen
2026-07-07 17:06:56 +08:00
committed by lyingbug
parent 8cf9f1c882
commit 77cf6a5789
13 changed files with 104 additions and 14 deletions
+17 -1
View File
@@ -20,6 +20,9 @@ const (
defaultKeenableBaseURL = "https://api.keenable.ai"
// keenableTitle is the attribution tag Keenable segments integration traffic by.
keenableTitle = "WeKnora"
// defaultKeenableResults matches the fallback other providers use when the
// caller does not specify a positive maxResults.
defaultKeenableResults = 5
)
var (
@@ -64,6 +67,9 @@ func (p *KeenableProvider) Search(
if len(query) == 0 {
return nil, fmt.Errorf("query is empty")
}
if maxResults <= 0 {
maxResults = defaultKeenableResults
}
// Keyless by default; a configured key switches to the authenticated path.
path := "/v1/search/public"
@@ -116,10 +122,19 @@ func (p *KeenableProvider) Search(
if maxResults > 0 && len(results) >= maxResults {
break
}
// Keenable returns both a short "description" and a longer "snippet"
// excerpt. Prefer the short description as the summary snippet and keep
// the longer excerpt as Content (used by RAG compression). Fall back to
// whichever is present so we never drop the only available text.
snippet := item.Description
if snippet == "" {
snippet = item.Snippet
}
result := &types.WebSearchResult{
Title: item.Title,
URL: item.URL,
Snippet: item.Description,
Snippet: snippet,
Content: item.Snippet,
Source: "keenable",
}
if includeDate && item.PublishedAt != "" {
@@ -146,6 +161,7 @@ type keenableSearchResponse struct {
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description"`
Snippet string `json:"snippet,omitempty"`
PublishedAt string `json:"published_at,omitempty"`
AcquiredAt string `json:"acquired_at,omitempty"`
} `json:"results"`
@@ -62,6 +62,38 @@ func TestKeenableProvider_Search_Keyless(t *testing.T) {
}
}
func TestKeenableProvider_Search_SnippetMapping(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"results": []map[string]any{
// Has both: description is the short summary, snippet the longer excerpt.
{"title": "T1", "url": "https://e/1", "description": "short", "snippet": "long excerpt"},
// Only snippet present: it must fall back into Snippet so we don't drop text.
{"title": "T2", "url": "https://e/2", "snippet": "only long"},
},
})
}))
defer srv.Close()
p := &KeenableProvider{client: srv.Client(), baseURL: srv.URL}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
results, err := p.Search(ctx, "q", 5, false)
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
if results[0].Snippet != "short" || results[0].Content != "long excerpt" {
t.Errorf("result[0] snippet/content mapping wrong: %+v", results[0])
}
if results[1].Snippet != "only long" {
t.Errorf("result[1] should fall back to snippet when description is empty: %+v", results[1])
}
}
func TestKeenableProvider_Search_Keyed(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// A configured key => authenticated endpoint + X-API-Key.
@@ -52,6 +52,20 @@ func TestEmptyTestResultsError_DuckDuckGo(t *testing.T) {
}
}
func TestEmptyTestResultsError_Keenable(t *testing.T) {
err := EmptyTestResultsError(string(types.WebSearchProviderTypeKeenable), nil)
if err == nil {
t.Fatal("expected error")
}
msg := err.Error()
if !strings.Contains(msg, "keenable returned 0 results") {
t.Fatalf("unexpected message: %q", msg)
}
if !strings.Contains(msg, "rate-limited") {
t.Fatalf("keenable message should hint at the keyless rate limit: %q", msg)
}
}
func TestEmptyTestResultsError_APIKeyProviders(t *testing.T) {
err := EmptyTestResultsError(string(types.WebSearchProviderTypeBing), nil)
if err == nil {