nikeshn commited on
Commit
525c5dc
Β·
verified Β·
1 Parent(s): 31edef1

Update src/api/agent.py

Browse files
Files changed (1) hide show
  1. src/api/agent.py +235 -151
src/api/agent.py CHANGED
@@ -1230,7 +1230,7 @@ Input: "find articles on global warming and renewable energy"
1230
 
1231
  Input: "peer reviewed articles last 5 years on machine learning healthcare"
1232
  β†’ boolean:("machine learning" OR "deep learning" OR "AI") AND ("healthcare" OR "medicine" OR "clinical")
1233
- β†’ year_from:"{CURRENT_YEAR - 5}", peer_reviewed:true
1234
 
1235
  Input: "impact of global warming in Maldives"
1236
  β†’ boolean:("global warming" OR "climate change") AND ("Maldives" OR "sea level rise" OR "small island")
@@ -1242,7 +1242,7 @@ Input: "water scarcity solutions in UAE"
1242
 
1243
  Input: "summarize shift in personalized genomic medicine from N-of-1 trials last 24 months"
1244
  β†’ boolean:("personalized genomic medicine" OR "precision medicine") AND ("N-of-1 trials" OR "scalable precision")
1245
- β†’ year_from:"{CURRENT_YEAR - 2}", peer_reviewed:true
1246
 
1247
  Return ONLY valid JSON:
1248
  {{"corrected":"...","natural":"...","boolean":"(\"A\" OR \"B\") AND (\"C\" OR \"D\")","year_from":"","year_to":"","peer_reviewed":false,"open_access":false}}"""
@@ -1410,24 +1410,16 @@ async def _semantic_scholar_search(topic: str, limit: int = 8) -> List[dict]:
1410
 
1411
  async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1412
  """
1413
- Search OpenAlex using semantic search β€” passes the corrected natural language
1414
- query directly, matching the search.semantic format the user sees in the
1415
- OpenAlex browser link:
1416
- https://openalex.org/works?search.semantic=QUERY&sort=relevance_score:desc
1417
- The API equivalent uses ?search=QUERY which triggers the same semantic ranking.
1418
  """
1419
  try:
1420
  from urllib.parse import quote as _oa_quote
1421
-
1422
- # Strip only boolean operators β€” keep the full natural language query intact
1423
- # including proper nouns, locations, and all keywords
1424
- clean = re.sub(r'\b(AND|OR|NOT)\b|[()"\']', ' ', topic, flags=re.IGNORECASE)
1425
  clean = re.sub(r'\s+', ' ', clean).strip()[:200]
1426
  encoded = _oa_quote(clean, safe='')
1427
-
1428
  select = "id,title,authorships,publication_year,primary_location,abstract_inverted_index,doi,cited_by_count,open_access"
1429
-
1430
- # Use ?search= β€” API equivalent of search.semantic in the browser link
1431
  url = (
1432
  f"https://api.openalex.org/works"
1433
  f"?search={encoded}"
@@ -1437,13 +1429,11 @@ async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1437
  f"&select={select}"
1438
  )
1439
  headers = {"User-Agent": "LibBee/3.2 (mailto:libse@ku.ac.ae)"}
1440
-
1441
  async with httpx.AsyncClient(timeout=10) as client:
1442
  r = await client.get(url, headers=headers)
1443
  if r.status_code != 200:
1444
  logger.warning(f"OpenAlex search failed: {r.status_code}")
1445
  return []
1446
-
1447
  works = r.json().get("results", [])
1448
 
1449
  results = []
@@ -1805,66 +1795,209 @@ def _search_trace_block(question: str, context: SearchContextPayload) -> str:
1805
  return block
1806
 
1807
 
1808
- def _ai_tools_footer(context: SearchContextPayload) -> str:
1809
- """
1810
- AI tools recommendation panel β€” always rendered at the very end of the answer.
1811
- Structured as KU-subscribed tools first, then freemium, then more results.
1812
- """
1813
- urls = _tool_urls(context)
1814
- topic_enc = _escape(context.ai_tool_query or context.display_topic or context.topic)
1815
- is_medical = context.intent == "search_medical"
1816
-
1817
- ku_tools = [
1818
- ("πŸ”¬ LeapSpace", urls["leapspace"], "leapspace", "#9a3412"),
1819
- ("πŸ”­ Scopus AI", urls["scopus_ai"], "scopus", "#1e40af"),
1820
- ("πŸ“° EBSCO AI", urls["ebsco_ai"], "ebscoai", "#9d174d"),
1821
- ("⭐ PRIMO AI Assistant", urls["primo_ai"], "primoai", "#5b21b6"),
1822
- ]
1823
- free_tools = [
1824
- ("🧬 Consensus", urls["consensus"], "consensus", "#86198f"),
1825
- ("🌐 OpenAlex", f"https://openalex.org/works?page=1&sort=relevance_score:desc&search={topic_enc}", "openalex", "#0c4a6e"),
1826
- ("πŸ”Ž Perplexity", f"https://www.perplexity.ai/search?q={topic_enc}", "perplexity", "#4c1d95"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1827
  ]
1828
- more = [("πŸ“š PRIMO Library Discovery", urls["primo_discovery"], "primoai", "#003366")]
1829
- if is_medical and urls.get("pubmed"):
1830
- more.append(("πŸ₯ PubMed", urls["pubmed"], "pubmed_badge", "#065f46"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1831
 
1832
- def chip(label, url, cls, color):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1833
  return (
1834
- f'<a href="{url}" target="_blank" '
1835
- f'style="display:inline-flex;align-items:center;padding:5px 11px;margin:3px;'
1836
- f'border-radius:20px;font-size:.72rem;font-weight:700;text-decoration:none;'
1837
- f'border:1.5px solid {color}33;color:{color};background:{color}11;'
1838
- f'transition:all .2s;white-space:nowrap">{label}</a>'
 
 
 
1839
  )
1840
 
1841
- html = (
1842
- '<div style="margin-top:18px;padding:12px 14px;border:1px solid #c4b5fd;'
1843
- 'border-radius:12px;background:linear-gradient(135deg,#f5f3ff,#fdf4ff)">'
1844
- '<div style="font-size:.75rem;font-weight:800;color:#5b21b6;margin-bottom:8px">'
1845
- '✨ For deeper research β€” AI tools</div>'
1846
- '<div style="font-size:.67rem;font-weight:700;color:#6b7280;text-transform:uppercase;'
1847
- 'letter-spacing:.5px;margin-bottom:4px">πŸ” KU subscribed</div>'
1848
- '<div style="line-height:2">'
1849
- )
1850
- for label, url, cls, color in ku_tools:
1851
- html += chip(label, url, cls, color)
1852
- html += (
1853
- '</div><div style="font-size:.67rem;font-weight:700;color:#6b7280;text-transform:uppercase;'
1854
- 'letter-spacing:.5px;margin:8px 0 4px">πŸ†“ Freemium tools</div>'
1855
- '<div style="line-height:2">'
1856
- )
1857
- for label, url, cls, color in free_tools:
1858
- html += chip(label, url, cls, color)
1859
- html += (
1860
- '</div><div style="font-size:.67rem;font-weight:700;color:#6b7280;text-transform:uppercase;'
1861
- 'letter-spacing:.5px;margin:8px 0 4px">πŸ“š More results</div>'
1862
- '<div style="line-height:2">'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1863
  )
1864
- for label, url, cls, color in more:
1865
- html += chip(label, url, cls, color)
1866
- html += '</div></div>'
1867
- return html
1868
 
1869
 
1870
  def _is_database_recommendation_question(question: str) -> bool:
@@ -2374,46 +2507,32 @@ def _highly_cited_note(topic_escaped: str) -> str:
2374
 
2375
  async def _research_snapshot(context: SearchContextPayload, model: str) -> Tuple[str, List[dict], List[dict]]:
2376
  """
2377
- Research snapshot using OpenAlex semantic search only.
2378
- PRIMO API removed β€” PRIMO boolean is used only for the browser link shown to users.
2379
  Falls back to Semantic Scholar if OpenAlex returns nothing.
2380
  """
2381
  import asyncio as _asyncio
2382
  topic = context.topic
2383
  references: List[dict] = []
2384
-
2385
- # OpenAlex semantic search β€” passes corrected natural language query
2386
  papers = await _openalex_search(context.ai_tool_query or context.topic, limit=8)
2387
-
2388
- # Silent fallback to Semantic Scholar if OpenAlex returned nothing
2389
  if not papers:
2390
- logger.info(f"OpenAlex returned no papers for '{topic}' β€” trying Semantic Scholar")
2391
  papers = await _semantic_scholar_search(topic, limit=8)
2392
-
2393
- # ── Top: reasoning block ──────────────────────────────────────────────────
2394
  q_display = context.ai_tool_query or f"Research on {topic}"
2395
  answer = _search_trace_block(q_display, context)
2396
-
2397
  if papers:
2398
  summary = await _generate_cited_summary(topic, papers, model)
2399
  if not summary:
2400
  _safe_metrics_increment("summary_fallbacks")
2401
  summary = _extractive_snapshot_from_papers(topic, papers)
2402
-
2403
  source_label = papers[0].get("_source", "OpenAlex")
2404
  if source_label == "OpenAlex":
2405
- oa_source_url = "https://openalex.org"
2406
- source_key = "openalex_badge"
2407
  elif source_label == "Consensus":
2408
- oa_source_url = "https://consensus.app"
2409
- source_key = "consensus_badge"
2410
  else:
2411
- oa_source_url = "https://www.semanticscholar.org/"
2412
- source_key = "semantic"
2413
-
2414
- # ── Middle: cited summary ─────────────────────────────────────────────
2415
  answer += f"<strong>πŸ“– Research Snapshot: {_escape(topic)}</strong><br><br>{summary}"
2416
-
2417
  answer += "<br><br><strong>Selected references:</strong><br>"
2418
  for i, paper in enumerate(papers[:6], 1):
2419
  answer += (
@@ -2423,16 +2542,13 @@ async def _research_snapshot(context: SearchContextPayload, model: str) -> Tuple
2423
  f'{_escape(paper["authors"])} Β· {paper["year"]} Β· {_escape(paper["venue"] or "")}'
2424
  f"</span>"
2425
  )
2426
-
2427
- answer += _source_badge(source_key, oa_source_url)
2428
  answer += _ai_tools_footer(context)
2429
  references = [{"title": p["title"], "url": p["link"]} for p in papers[:6]]
2430
  return answer, [], references
2431
-
2432
- # OpenAlex and Semantic Scholar both returned nothing
2433
  answer += (
2434
  f"<strong>πŸ“š Research starting points: {_escape(topic)}</strong><br><br>"
2435
- "I didn't find direct results right now β€” try the AI research tools below."
2436
  )
2437
  answer += _ai_tools_footer(context)
2438
  return answer, [], []
@@ -2782,40 +2898,28 @@ async def _run_search_mode(
2782
  question: str, context: SearchContextPayload, model: str, is_follow_up: bool
2783
  ) -> Tuple[str, List[dict], List[dict], str]:
2784
  context = await _prepare_queries(question, context, model, is_follow_up)
2785
-
2786
- # OpenAlex semantic search only β€” PRIMO API removed.
2787
- # PRIMO boolean is preserved for the browser link shown to users.
2788
- # Skip OpenAlex for books-only searches β€” it only indexes journal articles.
2789
  use_openalex = context.resource_type != "books"
2790
-
2791
  import asyncio as _aio
2792
  if context.source == "pubmed":
2793
  if use_openalex:
2794
- (result, pub_source_url), oa_papers = await _aio.gather(
2795
  _search_pubmed_articles(context, limit=6),
2796
  _openalex_search(context.ai_tool_query or context.topic, limit=5),
2797
  )
2798
  else:
2799
- result, pub_source_url = await _search_pubmed_articles(context, limit=6)
2800
  oa_papers = []
2801
- source_name = "pubmed"
2802
- source_url = pub_source_url
2803
  search_results = result.get("results", []) or []
2804
  total = result.get("total", len(search_results))
2805
  for r in search_results:
2806
  if "_source" not in r:
2807
  r["_source"] = "PubMed"
2808
  else:
2809
- # PRIMO API removed β€” use OpenAlex only for article results
2810
- if use_openalex:
2811
- oa_papers = await _openalex_search(context.ai_tool_query or context.topic, limit=8)
2812
- else:
2813
- oa_papers = []
2814
- source_name = "openalex"
2815
- source_url = _primo_clean_url(context) # PRIMO link still shown to user
2816
- search_results = []
2817
- total = 0
2818
-
2819
  # Tag OpenAlex results
2820
  for paper in oa_papers:
2821
  paper["_source"] = "OpenAlex"
@@ -2824,29 +2928,22 @@ async def _run_search_mode(
2824
  paper["date"] = paper.get("year", "")
2825
  paper["type"] = "Article"
2826
  from urllib.parse import quote as _pq
2827
- primo_title_url = (
2828
  "https://khalifa.primo.exlibrisgroup.com/discovery/search"
2829
  f"?query=title,contains,{_pq(paper['title'][:120])},AND"
2830
  "&tab=Everything&search_scope=MyInst_and_CI"
2831
  "&vid=971KUOSTAR_INST:KU&lang=en&mode=advanced"
2832
  )
2833
- paper["primo_url"] = primo_title_url
2834
-
2835
- # ── Top: reasoning / how-I-searched block ────────────────────────────────
2836
  answer = _search_trace_block(question, context)
2837
-
2838
- # ── Middle: answer sentence ────────────────────────────────────────────────
2839
  answer += _search_answer_intro(context, is_follow_up)
2840
-
2841
  filters = _filters_summary(context)
2842
  if filters:
2843
  answer += f"<br><br><strong>Active filters:</strong> {_escape(filters)}."
2844
-
2845
  if context.source == "pubmed":
2846
  if search_results:
2847
  answer += f"<br><br>Found <strong>{total:,}</strong> result(s) β€” top {len(search_results)} shown below."
2848
  else:
2849
- answer += "<br><br>No PubMed results found β€” try the AI tools below."
2850
  answer += _source_badge(source_name, source_url)
2851
  if oa_papers:
2852
  answer += _source_badge("openalex_badge", "https://openalex.org")
@@ -2854,28 +2951,19 @@ async def _run_search_mode(
2854
  if oa_papers:
2855
  answer += f"<br><br>Found <strong>{len(oa_papers)}</strong> open access result(s) via OpenAlex."
2856
  else:
2857
- answer += "<br><br>No results found right now β€” try the AI tools or the PRIMO link below."
2858
  answer += _source_badge("openalex_badge", "https://openalex.org")
2859
-
2860
  if context.intent == "search_medical" and context.source != "pubmed":
2861
- answer += "<br><br>For the strongest medical evidence trail, also check <strong>PubMed</strong> or <strong>Embase</strong>."
2862
-
2863
- # ── Bottom: AI tools recommendation panel ─────────────────────────────────
2864
  answer += _ai_tools_footer(context)
2865
-
2866
- # Combined results: PubMed (if medical) + OpenAlex
2867
- combined_results = search_results + oa_papers[:8]
2868
-
2869
  sources = []
2870
  for item in search_results[:6]:
2871
- link = item.get("primo_url") or item.get("link")
2872
- if link:
2873
- sources.append({"title": item.get("title", "Result"), "url": link})
2874
  for paper in oa_papers[:6]:
2875
- if paper.get("link"):
2876
- sources.append({"title": paper["title"], "url": paper["link"]})
2877
-
2878
- return answer, combined_results, sources, source_url
2879
 
2880
  def _sort_and_trim_contexts(contexts: List[SearchContextPayload]) -> List[SearchContextPayload]:
2881
  dedup: Dict[str, SearchContextPayload] = {}
@@ -3482,38 +3570,34 @@ async def _agent_query_inner(
3482
  _prepare_queries(question, _ctx_articles, model, False),
3483
  _prepare_queries(question, _ctx_books, model, False),
3484
  )
3485
- # PRIMO API removed β€” use OpenAlex for articles, PRIMO link for books
3486
- _ctx_articles_prepared = await _prepare_queries(question, _ctx_articles, model, False)
3487
- _oa_articles = await _openalex_search(_ctx_articles_prepared.ai_tool_query or topic, limit=6)
3488
- for _p in _oa_articles:
3489
  _p["_source"] = "OpenAlex"
3490
  _p["description"] = _p.get("abstract", "")[:300]
3491
  _p["creator"] = _p.get("authors", "")
3492
  _p["date"] = _p.get("year", "")
3493
  _p["type"] = "Article"
3494
- _results_a = _oa_articles
3495
- _results_b = [] # books: direct user to PRIMO link
3496
  _src_url_a = _primo_clean_url(_ctx_articles)
3497
  _src_url_b = _primo_clean_url(_ctx_books)
3498
 
3499
  _dual_answer = _search_trace_block(question, _ctx_articles)
3500
- _dual_answer += (
3501
- f"<strong>πŸ“„ Articles on {_escape(topic)}</strong>"
3502
- )
3503
  if _results_a:
3504
  _dual_answer += f" β€” {len(_results_a)} open access result(s) shown below.<br>"
3505
  else:
3506
  _dual_answer += "<br><em>No article results found β€” try the PRIMO link below.</em><br>"
3507
  _dual_answer += _source_badge("openalex_badge", "https://openalex.org")
3508
-
3509
  _dual_answer += f"<br><br><strong>πŸ“š Books on {_escape(topic)}</strong><br>"
3510
  _dual_answer += (
3511
- "For books, search the KU Library catalogue directly: "
3512
  f'<a href="{_src_url_b}" target="_blank" style="color:#003366;font-weight:700">Search PRIMO for books β†’</a><br>'
3513
  )
3514
  _dual_answer += _source_badge("primo", _src_url_b)
3515
  _dual_answer += _ai_tools_footer(_ctx_articles)
3516
-
3517
  _combined = _results_a
3518
  follow_up_question, follow_up_suggestions = await _search_follow_up(_ctx_articles, model)
3519
  return AgentResponse(
 
1230
 
1231
  Input: "peer reviewed articles last 5 years on machine learning healthcare"
1232
  β†’ boolean:("machine learning" OR "deep learning" OR "AI") AND ("healthcare" OR "medicine" OR "clinical")
1233
+ β†’ year_from:"2021", peer_reviewed:true
1234
 
1235
  Input: "impact of global warming in Maldives"
1236
  β†’ boolean:("global warming" OR "climate change") AND ("Maldives" OR "sea level rise" OR "small island")
 
1242
 
1243
  Input: "summarize shift in personalized genomic medicine from N-of-1 trials last 24 months"
1244
  β†’ boolean:("personalized genomic medicine" OR "precision medicine") AND ("N-of-1 trials" OR "scalable precision")
1245
+ β†’ year_from:"2024", peer_reviewed:true
1246
 
1247
  Return ONLY valid JSON:
1248
  {{"corrected":"...","natural":"...","boolean":"(\"A\" OR \"B\") AND (\"C\" OR \"D\")","year_from":"","year_to":"","peer_reviewed":false,"open_access":false}}"""
 
1410
 
1411
  async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1412
  """
1413
+ Search OpenAlex using ?search= β€” same relevance engine as the browser link.
1414
+ Passes the full corrected natural language query, strips only boolean operators.
 
 
 
1415
  """
1416
  try:
1417
  from urllib.parse import quote as _oa_quote
1418
+ # Strip boolean operators only β€” keep all keywords including proper nouns
1419
+ clean = re.sub(r'\b(AND|OR|NOT)\b|[()"\'\\]', ' ', topic, flags=re.IGNORECASE)
 
 
1420
  clean = re.sub(r'\s+', ' ', clean).strip()[:200]
1421
  encoded = _oa_quote(clean, safe='')
 
1422
  select = "id,title,authorships,publication_year,primary_location,abstract_inverted_index,doi,cited_by_count,open_access"
 
 
1423
  url = (
1424
  f"https://api.openalex.org/works"
1425
  f"?search={encoded}"
 
1429
  f"&select={select}"
1430
  )
1431
  headers = {"User-Agent": "LibBee/3.2 (mailto:libse@ku.ac.ae)"}
 
1432
  async with httpx.AsyncClient(timeout=10) as client:
1433
  r = await client.get(url, headers=headers)
1434
  if r.status_code != 200:
1435
  logger.warning(f"OpenAlex search failed: {r.status_code}")
1436
  return []
 
1437
  works = r.json().get("results", [])
1438
 
1439
  results = []
 
1795
  return block
1796
 
1797
 
1798
+ def _ai_tools_footer(context):
1799
+ from urllib.parse import quote as _q
1800
+
1801
+ ai_q = _q(context.ai_tool_query or context.display_topic or context.topic)
1802
+ bool_q = context.primo_boolean_query or ""
1803
+ primo_url = _primo_clean_url(context)
1804
+ primo_ai = PRIMO_AI_URL + ai_q
1805
+ is_med = context.intent == "search_medical"
1806
+ pubmed_url = _shared_build_pubmed_url(
1807
+ context.topic, context.year_from, context.year_to, context.peer_reviewed
1808
+ ) if is_med else ""
1809
+
1810
+ KU = "https://khalifa.idm.oclc.org/login?url="
1811
+
1812
+ platforms = [
1813
+ ("PRIMO Library Discovery", primo_url,
1814
+ "https://exlibrisgroup.com/favicon.ico"),
1815
+ ("PRIMO AI Assistant", primo_ai,
1816
+ "https://exlibrisgroup.com/favicon.ico"),
1817
+ ("LeapSpace",
1818
+ "https://www.sciencedirect.com/leapspace?query=" + ai_q,
1819
+ "https://www.sciencedirect.com/favicon.ico"),
1820
+ ("Scopus AI",
1821
+ "https://www-scopus-com.khalifa.idm.oclc.org/pages/ai?query=" + ai_q,
1822
+ "https://www.scopus.com/favicon.ico"),
1823
+ ("EBSCO Research AI",
1824
+ "https://research-ebsco-com.khalifa.idm.oclc.org/c/yst6t2/search/results?q=" + ai_q
1825
+ + "&autocorrect=y&expanders=concept&limiters=None&searchMode=enhanced&searchSegment=all-results",
1826
+ "https://research.ebsco.com/favicon.ico"),
1827
+ ("Consensus",
1828
+ "https://consensus.app/results/?q=" + ai_q,
1829
+ "https://consensus.app/favicon.ico"),
1830
+ ("OpenAlex",
1831
+ "https://openalex.org/works?search.semantic=" + ai_q + "&page=1&sort=relevance_score:desc",
1832
+ "https://openalex.org/favicon.ico"),
1833
+ ("Perplexity",
1834
+ "https://www.perplexity.ai/search?q=" + ai_q,
1835
+ "https://www.perplexity.ai/favicon.ico"),
1836
  ]
1837
+ if is_med and pubmed_url:
1838
+ platforms.append(("PubMed", pubmed_url,
1839
+ "https://pubmed.ncbi.nlm.nih.gov/favicon.ico"))
1840
+
1841
+ def _btn(label, url, favicon):
1842
+ img = (
1843
+ '<img src="' + favicon + '" width="16" height="16" '
1844
+ 'style="border-radius:3px;flex-shrink:0" '
1845
+ 'onerror="this.style.display=\'none\'" />'
1846
+ )
1847
+ style = (
1848
+ 'display:inline-flex;align-items:center;gap:7px;'
1849
+ 'padding:8px 14px;border-radius:10px;text-decoration:none;'
1850
+ 'font-size:.8rem;font-weight:600;color:#1a1a2e;'
1851
+ 'background:#fff;border:1.5px solid #d1d5db;'
1852
+ 'transition:box-shadow .2s;width:100%;box-sizing:border-box'
1853
+ )
1854
+ return '<a href="' + url + '" target="_blank" style="' + style + '">' + img + label + '</a>'
1855
+
1856
+ btn_rows = ""
1857
+ for k in range(0, len(platforms), 2):
1858
+ pair = platforms[k:k+2]
1859
+ cells = ""
1860
+ for lbl, url, fav in pair:
1861
+ cells += '<td style="width:50%;padding:3px 4px 3px 0">' + _btn(lbl, url, fav) + '</td>'
1862
+ if len(pair) == 1:
1863
+ cells += '<td style="width:50%"></td>'
1864
+ btn_rows += "<tr>" + cells + "</tr>"
1865
+
1866
+ platforms_html = (
1867
+ '<div style="margin-top:18px;padding:14px 16px;border:1px solid #c4b5fd;'
1868
+ 'border-radius:12px;background:linear-gradient(135deg,#f5f3ff,#fdf4ff)">'
1869
+ '<div style="font-size:.8rem;font-weight:800;color:#5b21b6;margin-bottom:4px">'
1870
+ '\U0001f50d Search your query across platforms</div>'
1871
+ '<div style="font-size:.73rem;color:#6b7280;margin-bottom:10px">'
1872
+ 'Your query is pre-loaded. Click any platform to search instantly.</div>'
1873
+ '<table style="width:100%;border-collapse:collapse">' + btn_rows + '</table>'
1874
+ '</div>'
1875
+ )
1876
 
1877
+ if bool_q:
1878
+ and_count = bool_q.upper().count(" AND ")
1879
+ if and_count >= 1:
1880
+ bool_label = "Use this in PRIMO Advanced Search or any database for precise results:"
1881
+ else:
1882
+ bool_label = "Try adding synonyms with OR to find more relevant results:"
1883
+ bool_html = (
1884
+ '<div style="margin-top:12px;padding:12px 14px;border:1px solid #fde68a;'
1885
+ 'border-radius:10px;background:#fffbeb">'
1886
+ '<div style="font-size:.78rem;font-weight:700;color:#92400e;margin-bottom:6px">'
1887
+ '\U0001f4a1 Boolean search tip</div>'
1888
+ '<div style="font-size:.74rem;color:#6b7280;margin-bottom:6px">' + bool_label + '</div>'
1889
+ '<code style="display:block;padding:8px 10px;background:#1a1a2e;color:#C8A951;'
1890
+ 'border-radius:7px;font-size:.76rem;word-break:break-all;white-space:pre-wrap">'
1891
+ + _escape(bool_q) +
1892
+ '</code>'
1893
+ '<div style="font-size:.71rem;color:#92400e;margin-top:6px">'
1894
+ 'Paste into the Advanced Search box of any database below.</div>'
1895
+ '</div>'
1896
+ )
1897
+ else:
1898
+ bool_html = ""
1899
+
1900
+ all_lower = ((context.ai_tool_query or "") + " " + (context.topic or "")).lower()
1901
+
1902
+ def _db_row(name, url, desc, favicon):
1903
+ img = (
1904
+ '<img src="' + favicon + '" width="16" height="16" '
1905
+ 'style="border-radius:3px;flex-shrink:0" '
1906
+ 'onerror="this.style.display=\'none\'" />'
1907
+ )
1908
  return (
1909
+ '<div style="display:flex;align-items:center;gap:8px;'
1910
+ 'padding:7px 0;border-bottom:1px solid #f3f4f6">'
1911
+ + img +
1912
+ '<div><a href="' + url + '" target="_blank" '
1913
+ 'style="font-size:.8rem;font-weight:700;color:#003366;text-decoration:none">'
1914
+ + name + '</a>'
1915
+ '<span style="font-size:.73rem;color:#6b7280"> \u2014 ' + desc + '</span></div>'
1916
+ '</div>'
1917
  )
1918
 
1919
+ if any(k in all_lower for k in ["medicine","medical","clinical","nursing","pharmacy","health","pubmed","embase","cinahl","cochrane","uptodate"]):
1920
+ db_subject = "Medicine & Health Sciences"
1921
+ dbs = [
1922
+ ("PubMed@KU","http://www.ncbi.nlm.nih.gov/pubmed/?otool=iaekustlib","Core biomedical literature","https://pubmed.ncbi.nlm.nih.gov/favicon.ico"),
1923
+ ("Embase",KU+"https://www.embase.com","Biomedical, drugs, devices, clinical medicine","https://www.embase.com/favicon.ico"),
1924
+ ("CINAHL Ultimate",KU+"https://search.ebscohost.com/login.aspx?authtype=ip,uid&profile=ehost&defaultdb=cul","Nursing and allied health","https://research.ebsco.com/favicon.ico"),
1925
+ ("Cochrane Library",KU+"https://www.cochranelibrary.com","Systematic reviews and evidence-based medicine","https://www.cochranelibrary.com/favicon.ico"),
1926
+ ("UpToDate",KU+"https://www.uptodate.com","Point-of-care clinical decision support","https://www.uptodate.com/favicon.ico"),
1927
+ ]
1928
+ elif any(k in all_lower for k in ["machine learning","deep learning","artificial intelligence","neural network","computer science","software","algorithm","cybersecurity","robotics","nlp"]):
1929
+ db_subject = "Computer Science & AI"
1930
+ dbs = [
1931
+ ("IEEE Xplore",KU+"https://ieeexplore.ieee.org","Core CS, AI, electronics and systems","https://ieeexplore.ieee.org/favicon.ico"),
1932
+ ("ACM Digital Library",KU+"https://dl.acm.org","Computer science journals, proceedings and books","https://dl.acm.org/favicon.ico"),
1933
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Full text across CS and applied computing","https://www.sciencedirect.com/favicon.ico"),
1934
+ ("Scopus",KU+"https://www.scopus.com","Discovery and citation tracking","https://www.scopus.com/favicon.ico"),
1935
+ ("Web of Science",KU+"https://www.webofscience.com","Citation index","https://www.webofscience.com/favicon.ico"),
1936
+ ]
1937
+ elif any(k in all_lower for k in ["climate","global warming","environment","sustainability","ecology","carbon","emission","renewable","sea level","maldives","island"]):
1938
+ db_subject = "Environment & Climate"
1939
+ dbs = [
1940
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Full text across science, environment and engineering","https://www.sciencedirect.com/favicon.ico"),
1941
+ ("Scopus",KU+"https://www.scopus.com","Broad discovery and citation tracking","https://www.scopus.com/favicon.ico"),
1942
+ ("Web of Science",KU+"https://www.webofscience.com","High-impact journal coverage and citation index","https://www.webofscience.com/favicon.ico"),
1943
+ ("SpringerLink",KU+"https://link.springer.com","Springer journals and books","https://link.springer.com/favicon.ico"),
1944
+ ]
1945
+ elif any(k in all_lower for k in ["engineering","mechanical","civil","electrical","aerospace","structural","petroleum","chemical","materials"]):
1946
+ db_subject = "Engineering"
1947
+ dbs = [
1948
+ ("IEEE Xplore",KU+"https://ieeexplore.ieee.org","Electrical, electronics and systems engineering","https://ieeexplore.ieee.org/favicon.ico"),
1949
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Full text across engineering disciplines","https://www.sciencedirect.com/favicon.ico"),
1950
+ ("Knovel",KU+"https://app.knovel.com","Engineering handbooks and reference data","https://app.knovel.com/favicon.ico"),
1951
+ ("Scopus",KU+"https://www.scopus.com","Discovery and citation tracking","https://www.scopus.com/favicon.ico"),
1952
+ ("Web of Science",KU+"https://www.webofscience.com","Citation index","https://www.webofscience.com/favicon.ico"),
1953
+ ]
1954
+ elif any(k in all_lower for k in ["business","management","finance","economics","leadership","marketing","strategy","entrepreneurship"]):
1955
+ db_subject = "Business & Management"
1956
+ dbs = [
1957
+ ("Business Source Complete",KU+"https://search.ebscohost.com","Core business and management literature","https://research.ebsco.com/favicon.ico"),
1958
+ ("Emerald Insight",KU+"https://www.emerald.com/insight","Management, strategy and leadership","https://www.emerald.com/favicon.ico"),
1959
+ ("Scopus",KU+"https://www.scopus.com","Broad discovery and citation tracking","https://www.scopus.com/favicon.ico"),
1960
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Business and social sciences full text","https://www.sciencedirect.com/favicon.ico"),
1961
+ ]
1962
+ elif any(k in all_lower for k in ["physics","chemistry","quantum","nuclear","materials science","nanotechnology","optics"]):
1963
+ db_subject = "Physics, Chemistry & Materials"
1964
+ dbs = [
1965
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Full text across physical sciences","https://www.sciencedirect.com/favicon.ico"),
1966
+ ("ACS Publications",KU+"https://pubs.acs.org","Core chemistry and chemical engineering","https://pubs.acs.org/favicon.ico"),
1967
+ ("APS Journals",KU+"https://journals.aps.org","American Physical Society core physics journals","https://journals.aps.org/favicon.ico"),
1968
+ ("Scopus",KU+"https://www.scopus.com","Discovery and citation tracking","https://www.scopus.com/favicon.ico"),
1969
+ ("Web of Science",KU+"https://www.webofscience.com","Citation index","https://www.webofscience.com/favicon.ico"),
1970
+ ]
1971
+ else:
1972
+ db_subject = "General"
1973
+ dbs = [
1974
+ ("EBSCO",KU+"https://search.ebscohost.com","Multidisciplinary full text","https://research.ebsco.com/favicon.ico"),
1975
+ ("ScienceDirect",KU+"https://www.sciencedirect.com","Science, engineering and medicine","https://www.sciencedirect.com/favicon.ico"),
1976
+ ("SpringerLink",KU+"https://link.springer.com","Journals and books across all disciplines","https://link.springer.com/favicon.ico"),
1977
+ ("Taylor & Francis",KU+"https://www.tandfonline.com","Humanities, social sciences and STEM","https://www.tandfonline.com/favicon.ico"),
1978
+ ("Emerald Insight",KU+"https://www.emerald.com/insight","Management and social sciences","https://www.emerald.com/favicon.ico"),
1979
+ ("Scopus",KU+"https://www.scopus.com","Citation discovery, all disciplines","https://www.scopus.com/favicon.ico"),
1980
+ ("Web of Science",KU+"https://www.webofscience.com","Citation index, all disciplines","https://www.webofscience.com/favicon.ico"),
1981
+ ]
1982
+
1983
+ dbs_html_rows = ""
1984
+ for name, url, desc, favicon in dbs:
1985
+ dbs_html_rows += _db_row(name, url, desc, favicon)
1986
+
1987
+ db_section = (
1988
+ '<div style="margin-top:12px;padding:12px 14px;border:1px solid #d1d5db;'
1989
+ 'border-radius:10px;background:#f9fafb">'
1990
+ '<div style="font-size:.78rem;font-weight:700;color:#003366;margin-bottom:8px">'
1991
+ '\U0001f4c2 Recommended KU databases \u2014 ' + db_subject + '</div>'
1992
+ + dbs_html_rows +
1993
+ '<div style="padding-top:8px;font-size:.74rem">'
1994
+ '<a href="https://library.ku.ac.ae/eresources" target="_blank" '
1995
+ 'style="color:#003366;font-weight:700">'
1996
+ '\U0001f5c4\ufe0f Browse all 50+ KU databases \u2192</a></div>'
1997
+ '</div>'
1998
  )
1999
+
2000
+ return platforms_html + bool_html + db_section
 
 
2001
 
2002
 
2003
  def _is_database_recommendation_question(question: str) -> bool:
 
2507
 
2508
  async def _research_snapshot(context: SearchContextPayload, model: str) -> Tuple[str, List[dict], List[dict]]:
2509
  """
2510
+ Research snapshot using OpenAlex only.
2511
+ PRIMO API removed β€” PRIMO boolean used only for the browser link shown to users.
2512
  Falls back to Semantic Scholar if OpenAlex returns nothing.
2513
  """
2514
  import asyncio as _asyncio
2515
  topic = context.topic
2516
  references: List[dict] = []
 
 
2517
  papers = await _openalex_search(context.ai_tool_query or context.topic, limit=8)
 
 
2518
  if not papers:
2519
+ logger.info(f"OpenAlex returned nothing for '{topic}' β€” trying Semantic Scholar")
2520
  papers = await _semantic_scholar_search(topic, limit=8)
 
 
2521
  q_display = context.ai_tool_query or f"Research on {topic}"
2522
  answer = _search_trace_block(q_display, context)
 
2523
  if papers:
2524
  summary = await _generate_cited_summary(topic, papers, model)
2525
  if not summary:
2526
  _safe_metrics_increment("summary_fallbacks")
2527
  summary = _extractive_snapshot_from_papers(topic, papers)
 
2528
  source_label = papers[0].get("_source", "OpenAlex")
2529
  if source_label == "OpenAlex":
2530
+ oa_src_url, src_key = "https://openalex.org", "openalex_badge"
 
2531
  elif source_label == "Consensus":
2532
+ oa_src_url, src_key = "https://consensus.app", "consensus_badge"
 
2533
  else:
2534
+ oa_src_url, src_key = "https://www.semanticscholar.org/", "semantic"
 
 
 
2535
  answer += f"<strong>πŸ“– Research Snapshot: {_escape(topic)}</strong><br><br>{summary}"
 
2536
  answer += "<br><br><strong>Selected references:</strong><br>"
2537
  for i, paper in enumerate(papers[:6], 1):
2538
  answer += (
 
2542
  f'{_escape(paper["authors"])} Β· {paper["year"]} Β· {_escape(paper["venue"] or "")}'
2543
  f"</span>"
2544
  )
2545
+ answer += _source_badge(src_key, oa_src_url)
 
2546
  answer += _ai_tools_footer(context)
2547
  references = [{"title": p["title"], "url": p["link"]} for p in papers[:6]]
2548
  return answer, [], references
 
 
2549
  answer += (
2550
  f"<strong>πŸ“š Research starting points: {_escape(topic)}</strong><br><br>"
2551
+ "No direct results found β€” try the AI research tools and databases below."
2552
  )
2553
  answer += _ai_tools_footer(context)
2554
  return answer, [], []
 
2898
  question: str, context: SearchContextPayload, model: str, is_follow_up: bool
2899
  ) -> Tuple[str, List[dict], List[dict], str]:
2900
  context = await _prepare_queries(question, context, model, is_follow_up)
 
 
 
 
2901
  use_openalex = context.resource_type != "books"
 
2902
  import asyncio as _aio
2903
  if context.source == "pubmed":
2904
  if use_openalex:
2905
+ (result, pub_url), oa_papers = await _aio.gather(
2906
  _search_pubmed_articles(context, limit=6),
2907
  _openalex_search(context.ai_tool_query or context.topic, limit=5),
2908
  )
2909
  else:
2910
+ result, pub_url = await _search_pubmed_articles(context, limit=6)
2911
  oa_papers = []
2912
+ source_name, source_url = "pubmed", pub_url
 
2913
  search_results = result.get("results", []) or []
2914
  total = result.get("total", len(search_results))
2915
  for r in search_results:
2916
  if "_source" not in r:
2917
  r["_source"] = "PubMed"
2918
  else:
2919
+ # PRIMO API removed β€” OpenAlex for articles, PRIMO link shown to user
2920
+ oa_papers = await _openalex_search(context.ai_tool_query or context.topic, limit=8) if use_openalex else []
2921
+ source_name, source_url = "openalex", _primo_clean_url(context)
2922
+ search_results, total = [], 0
 
 
 
 
 
 
2923
  # Tag OpenAlex results
2924
  for paper in oa_papers:
2925
  paper["_source"] = "OpenAlex"
 
2928
  paper["date"] = paper.get("year", "")
2929
  paper["type"] = "Article"
2930
  from urllib.parse import quote as _pq
2931
+ paper["primo_url"] = (
2932
  "https://khalifa.primo.exlibrisgroup.com/discovery/search"
2933
  f"?query=title,contains,{_pq(paper['title'][:120])},AND"
2934
  "&tab=Everything&search_scope=MyInst_and_CI"
2935
  "&vid=971KUOSTAR_INST:KU&lang=en&mode=advanced"
2936
  )
 
 
 
2937
  answer = _search_trace_block(question, context)
 
 
2938
  answer += _search_answer_intro(context, is_follow_up)
 
2939
  filters = _filters_summary(context)
2940
  if filters:
2941
  answer += f"<br><br><strong>Active filters:</strong> {_escape(filters)}."
 
2942
  if context.source == "pubmed":
2943
  if search_results:
2944
  answer += f"<br><br>Found <strong>{total:,}</strong> result(s) β€” top {len(search_results)} shown below."
2945
  else:
2946
+ answer += "<br><br>No PubMed results found β€” try the tools below."
2947
  answer += _source_badge(source_name, source_url)
2948
  if oa_papers:
2949
  answer += _source_badge("openalex_badge", "https://openalex.org")
 
2951
  if oa_papers:
2952
  answer += f"<br><br>Found <strong>{len(oa_papers)}</strong> open access result(s) via OpenAlex."
2953
  else:
2954
+ answer += "<br><br>No results found β€” use the platforms and databases below."
2955
  answer += _source_badge("openalex_badge", "https://openalex.org")
 
2956
  if context.intent == "search_medical" and context.source != "pubmed":
2957
+ answer += "<br><br>For medical evidence, also check <strong>PubMed</strong> or <strong>Embase</strong>."
 
 
2958
  answer += _ai_tools_footer(context)
2959
+ combined = search_results + oa_papers[:8]
 
 
 
2960
  sources = []
2961
  for item in search_results[:6]:
2962
+ lnk = item.get("primo_url") or item.get("link")
2963
+ if lnk: sources.append({"title": item.get("title", "Result"), "url": lnk})
 
2964
  for paper in oa_papers[:6]:
2965
+ if paper.get("link"): sources.append({"title": paper["title"], "url": paper["link"]})
2966
+ return answer, combined, sources, source_url
 
 
2967
 
2968
  def _sort_and_trim_contexts(contexts: List[SearchContextPayload]) -> List[SearchContextPayload]:
2969
  dedup: Dict[str, SearchContextPayload] = {}
 
3570
  _prepare_queries(question, _ctx_articles, model, False),
3571
  _prepare_queries(question, _ctx_books, model, False),
3572
  )
3573
+ # PRIMO API removed β€” OpenAlex for articles, PRIMO link for books
3574
+ _ctx_articles = await _prepare_queries(question, _ctx_articles, model, False)
3575
+ _oa_arts = await _openalex_search(_ctx_articles.ai_tool_query or topic, limit=6)
3576
+ for _p in _oa_arts:
3577
  _p["_source"] = "OpenAlex"
3578
  _p["description"] = _p.get("abstract", "")[:300]
3579
  _p["creator"] = _p.get("authors", "")
3580
  _p["date"] = _p.get("year", "")
3581
  _p["type"] = "Article"
3582
+ _results_a = _oa_arts
3583
+ _results_b = []
3584
  _src_url_a = _primo_clean_url(_ctx_articles)
3585
  _src_url_b = _primo_clean_url(_ctx_books)
3586
 
3587
  _dual_answer = _search_trace_block(question, _ctx_articles)
3588
+ _dual_answer += f"<strong>πŸ“„ Articles on {_escape(topic)}</strong>"
 
 
3589
  if _results_a:
3590
  _dual_answer += f" β€” {len(_results_a)} open access result(s) shown below.<br>"
3591
  else:
3592
  _dual_answer += "<br><em>No article results found β€” try the PRIMO link below.</em><br>"
3593
  _dual_answer += _source_badge("openalex_badge", "https://openalex.org")
 
3594
  _dual_answer += f"<br><br><strong>πŸ“š Books on {_escape(topic)}</strong><br>"
3595
  _dual_answer += (
3596
+ "Search the KU Library catalogue for books: "
3597
  f'<a href="{_src_url_b}" target="_blank" style="color:#003366;font-weight:700">Search PRIMO for books β†’</a><br>'
3598
  )
3599
  _dual_answer += _source_badge("primo", _src_url_b)
3600
  _dual_answer += _ai_tools_footer(_ctx_articles)
 
3601
  _combined = _results_a
3602
  follow_up_question, follow_up_suggestions = await _search_follow_up(_ctx_articles, model)
3603
  return AgentResponse(