p3: map Brave LLM Context; RRF merge; per-source no_rerank

Live llm/context hits live in grounding.generic plus a URL-keyed
sources map, not a sources array. Fan-in uses reciprocal-rank fusion
instead of cosine. no_rerank keeps that source's own order as a prefix.
This commit is contained in:
2026-09-23 16:02:21 -07:00
parent eb4e46d036
commit b6e1cdf825
10 changed files with 264 additions and 58 deletions
+59 -19
View File
@@ -167,14 +167,34 @@ func mapBrave(raw []byte) []Hit {
if err := json.Unmarshal(raw, &top); err != nil {
return nil
}
var buckets []any
for _, key := range []string{"sources", "results", "web"} {
if v, ok := top[key]; ok {
buckets = append(buckets, v)
}
}
var hits []Hit
seen := map[string]bool{}
add := func(u, title, snippet string) {
if u == "" || seen[u] {
return
}
seen[u] = true
hits = append(hits, Hit{URL: u, Title: title, Snippet: snippet, Source: NameBraveLLM})
}
// Live LLM Context: grounding.generic[] (and poi) with snippets[].
if g, ok := top["grounding"].(map[string]any); ok {
for _, key := range []string{"generic", "poi"} {
arr, _ := g[key].([]any)
for _, item := range arr {
m, _ := item.(map[string]any)
if m == nil {
continue
}
snip := joinSnippets(m["snippets"])
if snip == "" {
snip = strField(m, "snippet", "description", "content", "text")
}
add(strField(m, "url", "link", "href"), strField(m, "title", "name"), snip)
}
}
}
var walk func(any)
walk = func(v any) {
switch t := v.(type) {
@@ -185,29 +205,49 @@ func mapBrave(raw []byte) []Hit {
case map[string]any:
u := strField(t, "url", "link", "href")
if u == "" {
for _, x := range t {
// URL-keyed object: sources["https://…"] = {title, hostname}
for k, x := range t {
if strings.HasPrefix(k, "http://") || strings.HasPrefix(k, "https://") {
title, snip := "", ""
if m, ok := x.(map[string]any); ok {
title = strField(m, "title", "name", "site_name")
snip = strField(m, "description", "snippet")
}
add(k, title, snip)
continue
}
walk(x)
}
return
}
if seen[u] {
return
}
seen[u] = true
hits = append(hits, Hit{
URL: u,
Title: strField(t, "title", "name"),
Snippet: strField(t, "snippet", "description", "content", "text"),
Source: NameBraveLLM,
})
add(u, strField(t, "title", "name"), strField(t, "snippet", "description", "content", "text"))
}
}
for _, b := range buckets {
walk(b)
for _, key := range []string{"sources", "results", "web"} {
if v, ok := top[key]; ok {
walk(v)
}
}
return hits
}
func joinSnippets(v any) string {
switch t := v.(type) {
case []any:
parts := make([]string, 0, len(t))
for _, x := range t {
if s, ok := x.(string); ok && s != "" {
parts = append(parts, s)
}
}
return strings.Join(parts, "\n\n")
case string:
return t
default:
return ""
}
}
func strField(m map[string]any, keys ...string) string {
for _, k := range keys {
if v, ok := m[k]; ok {