File size: 1,258 Bytes
32f259e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

import time
from typing import List

from ..state import AgentState, ResearchSnippet
from ..tools import normalize_snippets, get_logger, log_state_summary
from ..config import settings

logger = get_logger()


def _rank_snippets(snippets: List[ResearchSnippet]) -> List[ResearchSnippet]:
    """
    Very simple ranking: prefer snippets that mention the topic more often (if present),
    otherwise keep original order.
    """
    return snippets


def aggregator_node(state: AgentState) -> AgentState:
    start = time.time()

    snippets = state.get("research_snippets", []) or []
    logger.info("Aggregator: normalizing %d snippets...", len(snippets))

    normalized = normalize_snippets(snippets)
    ranked = _rank_snippets(normalized)

    # Trim to a maximum number for LLM context
    max_keep = max(3, settings.max_search_results)
    trimmed = ranked[:max_keep]

    meta = state.get("meta", {}) or {}
    meta["aggregator_time_sec"] = round(time.time() - start, 3)
    meta["aggregated_snippets_count"] = len(trimmed)

    new_state: AgentState = {
        **state,
        "research_snippets": trimmed,
        "meta": meta,
    }
    log_state_summary(new_state, prefix="Aggregator")
    return new_state