| 758 | |
| 759 | |
| 760 | def resolve_reference(value: str) -> dict[str, Any]: |
| 761 | source_type = infer_source_type(value) |
| 762 | stripped = (value or "").strip() |
| 763 | if source_type == "local_pdf": |
| 764 | path = Path(stripped).expanduser().resolve() |
| 765 | hints = extract_local_pdf_hints(path) |
| 766 | paper = { |
| 767 | "status": "ok", |
| 768 | "source_type": "local_pdf", |
| 769 | "source_url": str(path), |
| 770 | "local_pdf_path": str(path), |
| 771 | "title": normalize_whitespace(str(hints.get("title", ""))) or clean_local_pdf_stem(path.stem) or path.stem.replace("_", " "), |
| 772 | "metadata_sources": ["local_pdf"], |
| 773 | } |
| 774 | if hints.get("local_pdf_title_source"): |
| 775 | paper["local_pdf_title_source"] = hints["local_pdf_title_source"] |
| 776 | if hints.get("local_pdf_artifact_title"): |
| 777 | paper["local_pdf_artifact_title"] = True |
| 778 | doi = normalize_whitespace(str(hints.get("doi", ""))) |
| 779 | arxiv_id = normalize_whitespace(str(hints.get("arxiv_id", ""))) |
| 780 | if doi: |
| 781 | paper["doi"] = doi |
| 782 | if arxiv_id: |
| 783 | paper["arxiv_id"] = arxiv_id |
| 784 | paper["paper_id"] = paper_id_for_record(paper) |
| 785 | return apply_identity_confidence(paper) |
| 786 | if source_type == "arxiv_id": |
| 787 | arxiv_id = extract_arxiv_id(stripped) or "" |
| 788 | papers = safe_fetch_arxiv_entries(id_list=arxiv_id, max_results=1) |
| 789 | if papers: |
| 790 | paper = papers[0] |
| 791 | paper["paper_id"] = paper_id_for_record(paper) |
| 792 | paper["status"] = "ok" |
| 793 | return apply_identity_confidence(paper) |
| 794 | if arxiv_id: |
| 795 | return fallback_arxiv_record(arxiv_id, "arxiv_id") |
| 796 | if source_type == "arxiv_url": |
| 797 | arxiv_id = extract_arxiv_id(stripped) or "" |
| 798 | papers = safe_fetch_arxiv_entries(id_list=arxiv_id, max_results=1) |
| 799 | if papers: |
| 800 | paper = papers[0] |
| 801 | paper["paper_id"] = paper_id_for_record(paper) |
| 802 | paper["status"] = "ok" |
| 803 | return apply_identity_confidence(paper) |
| 804 | if arxiv_id: |
| 805 | return fallback_arxiv_record(arxiv_id, "arxiv_url", source_url=stripped) |
| 806 | if source_type in {"doi", "doi_url"}: |
| 807 | doi = extract_doi(stripped) or "" |
| 808 | paper = fetch_crossref_by_doi(doi) or {"doi": doi, "source_url": f"https://doi.org/{doi}"} |
| 809 | paper["source_type"] = "doi" |
| 810 | paper["source_url"] = paper.get("source_url") or f"https://doi.org/{doi}" |
| 811 | paper["status"] = "ok" |
| 812 | paper["paper_id"] = paper_id_for_record(paper) |
| 813 | return apply_identity_confidence(paper) |
| 814 | if source_type == "pdf_url": |
| 815 | filename = Path(urllib.parse.urlparse(stripped).path).stem or "paper" |
| 816 | paper = { |
| 817 | "status": "ok", |