(user_id: str, hits: List[Dict[str, Any]])
| 154 | |
| 155 | |
| 156 | def _build_citations(user_id: str, hits: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
| 157 | citations_by_node = wiki_db.get_citations_for_nodes(user_id, [hit["node_id"] for hit in hits if hit.get("node_id")]) |
| 158 | citations = [] |
| 159 | for index, hit in enumerate(hits, start=1): |
| 160 | metadata = hit.get("metadata") or {} |
| 161 | display_node = hit |
| 162 | section_title = "" |
| 163 | if str(hit.get("node_type") or "") == "section" and metadata.get("parent_paper_id"): |
| 164 | parent = wiki_db.get_node(user_id, str(metadata.get("parent_paper_id"))) |
| 165 | if parent: |
| 166 | display_node = parent |
| 167 | section_title = str(hit.get("title") or "") |
| 168 | node_citations = citations_by_node.get(hit["node_id"], []) |
| 169 | first = node_citations[0] if node_citations else {} |
| 170 | display_metadata = dict(display_node.get("metadata") or {}) |
| 171 | if metadata.get("parent_paper_id"): |
| 172 | display_metadata.setdefault("parent_paper_id", metadata.get("parent_paper_id")) |
| 173 | if section_title: |
| 174 | display_metadata["section_title"] = section_title |
| 175 | display_metadata["evidence_node_id"] = hit["node_id"] |
| 176 | citations.append( |
| 177 | { |
| 178 | "index": index, |
| 179 | "node_id": display_node["node_id"], |
| 180 | "title": display_node["title"], |
| 181 | "node_type": display_node["node_type"], |
| 182 | "excerpt": first.get("excerpt") or _snippet(hit, max_chars=260), |
| 183 | "source_type": first.get("source"), |
| 184 | "source_id": first.get("source_id"), |
| 185 | "anchor": first.get("anchor"), |
| 186 | "metadata": display_metadata, |
| 187 | } |
| 188 | ) |
| 189 | return citations |
| 190 | |
| 191 | |
| 192 | def _empty_answer(started: float, response_language: str = "zh") -> Dict[str, Any]: |
no test coverage detected