litloop 1.18.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. litloop/__init__.py +89 -0
  2. litloop/__main__.py +6 -0
  3. litloop/_source_hygiene.py +158 -0
  4. litloop/agents/__init__.py +13 -0
  5. litloop/agents/client.py +164 -0
  6. litloop/agents/runner.py +123 -0
  7. litloop/agents/system.py +54 -0
  8. litloop/agents/tools.py +142 -0
  9. litloop/app_server.py +8 -0
  10. litloop/browser/__init__.py +1 -0
  11. litloop/browser/backend.py +1009 -0
  12. litloop/browser/camofox_api.py +368 -0
  13. litloop/browser/chrome_copy.py +136 -0
  14. litloop/browser/cloakbrowser_compat.py +72 -0
  15. litloop/browser/cookies.py +347 -0
  16. litloop/browser/engine.py +965 -0
  17. litloop/browser/login.py +636 -0
  18. litloop/browser/pdf.py +263 -0
  19. litloop/browser/profile_health.py +167 -0
  20. litloop/browser/runtime.py +210 -0
  21. litloop/cache.py +104 -0
  22. litloop/citations/__init__.py +1 -0
  23. litloop/citations/bib_import.py +102 -0
  24. litloop/citations/bibtex_crossref.py +143 -0
  25. litloop/citations/formats.py +225 -0
  26. litloop/citations/paper_list.py +200 -0
  27. litloop/citations/session_export.py +94 -0
  28. litloop/cli/__init__.py +12 -0
  29. litloop/cli/_utils.py +267 -0
  30. litloop/cli/commands.py +1098 -0
  31. litloop/cli/doctor.py +141 -0
  32. litloop/cli/main.py +1173 -0
  33. litloop/cli/web.py +190 -0
  34. litloop/config.py +413 -0
  35. litloop/data/VARCON-LICENSE.txt +62 -0
  36. litloop/data/elements.tsv +119 -0
  37. litloop/data/institutional_identity_policy.json +165 -0
  38. litloop/data/publisher_access_catalog.json +969 -0
  39. litloop/data/publisher_carsi.json +105 -0
  40. litloop/data/spelling_search.tsv.gz +0 -0
  41. litloop/data/spelling_us.tsv.gz +0 -0
  42. litloop/data/webvpn.dat +0 -0
  43. litloop/data/webvpn.json +603 -0
  44. litloop/db.py +1922 -0
  45. litloop/deps.py +128 -0
  46. litloop/desktop/__init__.py +1 -0
  47. litloop/desktop/agent_runner.py +537 -0
  48. litloop/desktop/download_jobs.py +571 -0
  49. litloop/desktop/error_text.py +192 -0
  50. litloop/desktop/server.py +999 -0
  51. litloop/desktop/settings.py +552 -0
  52. litloop/desktop/translate.py +103 -0
  53. litloop/diagnostics.py +126 -0
  54. litloop/download/__init__.py +1 -0
  55. litloop/download/doi_check.py +44 -0
  56. litloop/download/fetcher.py +975 -0
  57. litloop/download/markdown.py +130 -0
  58. litloop/download/pdf.py +454 -0
  59. litloop/download/pipeline.py +1166 -0
  60. litloop/download/progress.py +312 -0
  61. litloop/download/rename.py +211 -0
  62. litloop/extractors/__init__.py +0 -0
  63. litloop/extractors/html_extractor.py +31 -0
  64. litloop/extractors/pdf_extractor.py +151 -0
  65. litloop/extractors/publisher_adapters/__init__.py +0 -0
  66. litloop/extractors/publisher_adapters/acs.py +167 -0
  67. litloop/extractors/publisher_adapters/elsevier.py +178 -0
  68. litloop/extractors/publisher_adapters/generic.py +191 -0
  69. litloop/extractors/publisher_adapters/nature.py +204 -0
  70. litloop/extractors/publisher_adapters/rsc.py +154 -0
  71. litloop/extractors/publisher_adapters/tandfonline.py +154 -0
  72. litloop/extractors/publisher_adapters/wiley.py +160 -0
  73. litloop/http_utils.py +57 -0
  74. litloop/identifiers.py +134 -0
  75. litloop/institutional/__init__.py +1 -0
  76. litloop/institutional/auth.py +576 -0
  77. litloop/institutional/carsi_login.py +569 -0
  78. litloop/institutional/config_adapter.py +63 -0
  79. litloop/institutional/proxy_check.py +98 -0
  80. litloop/institutional/remote_assist.py +274 -0
  81. litloop/institutional/schools.py +102 -0
  82. litloop/institutional/session_heal.py +73 -0
  83. litloop/institutional/session_store.py +105 -0
  84. litloop/log.py +47 -0
  85. litloop/models.py +269 -0
  86. litloop/network.py +417 -0
  87. litloop/optional.py +310 -0
  88. litloop/publishers/__init__.py +1 -0
  89. litloop/publishers/access.py +232 -0
  90. litloop/publishers/pdf_router.py +451 -0
  91. litloop/publishers/profiles.py +899 -0
  92. litloop/publishers/strategies/__init__.py +122 -0
  93. litloop/publishers/strategies/_core.py +2340 -0
  94. litloop/publishers/strategies/_elsevier_api.py +578 -0
  95. litloop/publishers/strategies/acm.py +27 -0
  96. litloop/publishers/strategies/acs.py +42 -0
  97. litloop/publishers/strategies/aip.py +27 -0
  98. litloop/publishers/strategies/aps.py +29 -0
  99. litloop/publishers/strategies/asce.py +28 -0
  100. litloop/publishers/strategies/base.py +108 -0
  101. litloop/publishers/strategies/copernicus.py +24 -0
  102. litloop/publishers/strategies/elsevier.py +66 -0
  103. litloop/publishers/strategies/generic.py +39 -0
  104. litloop/publishers/strategies/ieee.py +28 -0
  105. litloop/publishers/strategies/iop.py +27 -0
  106. litloop/publishers/strategies/nature.py +27 -0
  107. litloop/publishers/strategies/oxford.py +27 -0
  108. litloop/publishers/strategies/registry.py +83 -0
  109. litloop/publishers/strategies/royalsociety.py +28 -0
  110. litloop/publishers/strategies/rsc.py +27 -0
  111. litloop/publishers/strategies/sage.py +30 -0
  112. litloop/publishers/strategies/science.py +27 -0
  113. litloop/publishers/strategies/springer.py +34 -0
  114. litloop/publishers/strategies/tandfonline.py +32 -0
  115. litloop/publishers/strategies/wiley.py +32 -0
  116. litloop/publishers/supplementary.py +219 -0
  117. litloop/quota.py +202 -0
  118. litloop/search/__init__.py +1 -0
  119. litloop/search/abstracts.py +291 -0
  120. litloop/search/deep.py +1216 -0
  121. litloop/search/quick.py +1102 -0
  122. litloop/search/scansci_find.py +410 -0
  123. litloop/search/screening.py +225 -0
  124. litloop/search/title_to_doi.py +163 -0
  125. litloop/search/watches.py +124 -0
  126. litloop/server.py +1731 -0
  127. litloop/sources/__init__.py +1339 -0
  128. litloop/sources/_batch.py +529 -0
  129. litloop/sources/_index.py +221 -0
  130. litloop/sources/_routes.py +290 -0
  131. litloop/sources/altcha.py +90 -0
  132. litloop/sources/arxiv.py +45 -0
  133. litloop/sources/attempt.py +91 -0
  134. litloop/sources/carsi.py +74 -0
  135. litloop/sources/core_api.py +46 -0
  136. litloop/sources/crossref.py +162 -0
  137. litloop/sources/elsevier_api.py +620 -0
  138. litloop/sources/elsevier_check.py +274 -0
  139. litloop/sources/europepmc.py +81 -0
  140. litloop/sources/ezproxy.py +317 -0
  141. litloop/sources/instsci.py +1295 -0
  142. litloop/sources/libgen.py +116 -0
  143. litloop/sources/nature.py +39 -0
  144. litloop/sources/oa_discovery.py +135 -0
  145. litloop/sources/openaire.py +122 -0
  146. litloop/sources/openalex.py +100 -0
  147. litloop/sources/publishers.py +684 -0
  148. litloop/sources/sage_cn.py +139 -0
  149. litloop/sources/scibban.py +10 -0
  150. litloop/sources/scihub.py +1379 -0
  151. litloop/sources/scihub_domains.py +203 -0
  152. litloop/sources/scihub_speed.py +187 -0
  153. litloop/sources/scoring.py +250 -0
  154. litloop/sources/semantic_scholar.py +146 -0
  155. litloop/sources/springer_tdm.py +172 -0
  156. litloop/sources/unpaywall.py +116 -0
  157. litloop/sources/vpnsci.py +38 -0
  158. litloop/store.py +264 -0
  159. litloop/templates/index.html +347 -0
  160. litloop/terms/__init__.py +1 -0
  161. litloop/terms/glossary.py +422 -0
  162. litloop/terms/spelling.py +89 -0
  163. litloop/terms/text.py +106 -0
  164. litloop/tor/__init__.py +1 -0
  165. litloop/tor/embedded.py +371 -0
  166. litloop/tor/proxy.py +110 -0
  167. litloop/updates.py +148 -0
  168. litloop/zotero/__init__.py +1 -0
  169. litloop/zotero/bridge.py +309 -0
  170. litloop/zotero/client.py +317 -0
  171. litloop-1.18.3.dist-info/METADATA +347 -0
  172. litloop-1.18.3.dist-info/RECORD +176 -0
  173. litloop-1.18.3.dist-info/WHEEL +5 -0
  174. litloop-1.18.3.dist-info/entry_points.txt +2 -0
  175. litloop-1.18.3.dist-info/licenses/LICENSE +197 -0
  176. litloop-1.18.3.dist-info/top_level.txt +1 -0
litloop/log.py ADDED
@@ -0,0 +1,47 @@
1
+ """Logging setup for LitLoop. Routes to stderr to avoid corrupting MCP stdio."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import collections
6
+ import logging
7
+ import sys
8
+
9
+ _logger: logging.Logger | None = None
10
+
11
+ # The last few hundred lines, kept in memory so the diagnostics bundle can
12
+ # include what happened without the user having to redirect stderr.
13
+ _recent: collections.deque[str] = collections.deque(maxlen=400)
14
+
15
+
16
+ class _RingHandler(logging.Handler):
17
+ def emit(self, record: logging.LogRecord) -> None:
18
+ try:
19
+ _recent.append(self.format(record))
20
+ except Exception: # a broken formatter must not break logging
21
+ pass
22
+
23
+
24
+ def recent(limit: int = 200) -> list[str]:
25
+ """The most recent log lines, oldest first."""
26
+ lines = list(_recent)
27
+ return lines[-limit:] if limit > 0 else []
28
+
29
+
30
+ def get_logger() -> logging.Logger:
31
+ global _logger
32
+ if _logger is None:
33
+ _logger = logging.getLogger("litloop")
34
+ if not _logger.handlers:
35
+ handler = logging.StreamHandler(sys.stderr)
36
+ handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%H:%M:%S"))
37
+ _logger.addHandler(handler)
38
+ _logger.setLevel(logging.INFO)
39
+ # The entry points also basicConfig() a stderr handler on the root
40
+ # logger; propagating there printed every message twice.
41
+ _logger.propagate = False
42
+ if not any(isinstance(h, _RingHandler) for h in _logger.handlers):
43
+ ring = _RingHandler()
44
+ ring.setLevel(logging.INFO)
45
+ ring.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S"))
46
+ _logger.addHandler(ring)
47
+ return _logger
litloop/models.py ADDED
@@ -0,0 +1,269 @@
1
+ """Data models for LitLoop."""
2
+
3
+ import json
4
+ from dataclasses import asdict, dataclass, field
5
+ from typing import Any
6
+
7
+
8
+ @dataclass
9
+ class Paper:
10
+ """Represents a fetched academic paper."""
11
+
12
+ doi: str = ""
13
+ title: str = ""
14
+ authors: list[str] = field(default_factory=list)
15
+ journal: str = ""
16
+ year: int | None = None
17
+ abstract: str = ""
18
+ full_text: str = ""
19
+ figures: list[str] = field(default_factory=list)
20
+ references: list[str] = field(default_factory=list)
21
+ source: str = "" # "institutional" | "open_access" | "arxiv"
22
+ pdf_path: str = ""
23
+ url: str = ""
24
+
25
+ def to_dict(self) -> dict[str, Any]:
26
+ """Serialize to a JSON-compatible dict."""
27
+ return asdict(self)
28
+
29
+ def to_json(self, indent: int = 2) -> str:
30
+ """Serialize to JSON string."""
31
+ return json.dumps(self.to_dict(), indent=indent, ensure_ascii=False)
32
+
33
+ def to_markdown(self, include_pdf_path: bool = False) -> str:
34
+ """Render as Markdown for Claude consumption."""
35
+ lines = []
36
+ lines.append(f"# {self.title or 'Untitled'}")
37
+ lines.append("")
38
+ if self.authors:
39
+ lines.append(f"**Authors:** {', '.join(self.authors)}")
40
+ if self.journal:
41
+ lines.append(f"**Journal:** {self.journal}")
42
+ if self.year:
43
+ lines.append(f"**Year:** {self.year}")
44
+ if self.doi:
45
+ lines.append(f"**DOI:** {self.doi}")
46
+ if self.source:
47
+ lines.append(f"**Source:** {self.source}")
48
+ if include_pdf_path and self.pdf_path:
49
+ lines.append(f"**PDF saved to:** {self.pdf_path}")
50
+ lines.append("")
51
+
52
+ if self.abstract:
53
+ lines.append("## Abstract")
54
+ lines.append("")
55
+ lines.append(self.abstract)
56
+ lines.append("")
57
+
58
+ if self.full_text:
59
+ lines.append("## Full Text")
60
+ lines.append("")
61
+ lines.append(self.full_text)
62
+ lines.append("")
63
+
64
+ if self.figures:
65
+ lines.append("## Figures")
66
+ lines.append("")
67
+ for i, fig in enumerate(self.figures, 1):
68
+ lines.append(f"**Figure {i}:** {fig}")
69
+ lines.append("")
70
+
71
+ if self.references:
72
+ lines.append("## References")
73
+ lines.append("")
74
+ for ref in self.references:
75
+ lines.append(f"- {ref}")
76
+ lines.append("")
77
+
78
+ return "\n".join(lines)
79
+
80
+ def to_text(self) -> str:
81
+ """Plain text output (minimal tokens for Claude)."""
82
+ parts = []
83
+ if self.title:
84
+ parts.append(self.title)
85
+ parts.append("")
86
+ if self.abstract:
87
+ parts.append(self.abstract)
88
+ parts.append("")
89
+ if self.full_text:
90
+ parts.append(self.full_text)
91
+ return "\n".join(parts)
92
+
93
+ @classmethod
94
+ def from_json(cls, data: str | dict) -> "Paper":
95
+ """Deserialize from JSON string or dict."""
96
+ if isinstance(data, str):
97
+ data = json.loads(data)
98
+ return cls(**{k: v for k, v in data.items() if k in cls.__dataclass_fields__})
99
+
100
+
101
+ @dataclass
102
+ class NextAction:
103
+ """Actionable next step for users or agents."""
104
+
105
+ kind: str
106
+ message: str
107
+ command: str = ""
108
+
109
+ def to_dict(self) -> dict[str, str]:
110
+ return asdict(self)
111
+
112
+
113
+ @dataclass
114
+ class FetchResult:
115
+ """Structured fetch outcome for agent-facing workflows."""
116
+
117
+ status: str
118
+ quality: str
119
+ paper: Paper = field(default_factory=Paper)
120
+ reason: str = ""
121
+ next_action: NextAction | None = None
122
+ attempts: list[dict[str, str]] = field(default_factory=list)
123
+
124
+ @classmethod
125
+ def from_paper(
126
+ cls,
127
+ paper: Paper,
128
+ *,
129
+ min_fulltext_len: int = 1000,
130
+ institution_configured: bool = False,
131
+ identifier: str = "",
132
+ ) -> "FetchResult":
133
+ """Classify a fetched paper into a stable agent-facing result."""
134
+ full_text_len = len(paper.full_text or "")
135
+ if full_text_len >= min_fulltext_len:
136
+ return cls(status="success", quality="full_text", paper=paper)
137
+
138
+ quality = _content_quality(paper)
139
+ if quality == "pdf_only":
140
+ return cls(
141
+ status="blocked",
142
+ quality="pdf_only",
143
+ paper=paper,
144
+ reason="pdf_extraction_failed",
145
+ next_action=_inspect_pdf_action(paper.pdf_path),
146
+ )
147
+
148
+ if quality == "none" and not institution_configured:
149
+ return cls(
150
+ status="config_needed",
151
+ quality="none",
152
+ paper=paper,
153
+ reason="institution_not_configured",
154
+ next_action=_configure_institution_action(),
155
+ )
156
+
157
+ if quality == "none":
158
+ return cls(
159
+ status="auth_required",
160
+ quality="none",
161
+ paper=paper,
162
+ reason="institution_login_required",
163
+ next_action=_login_action(identifier or paper.doi or paper.url),
164
+ )
165
+
166
+ next_action = (
167
+ _login_action(identifier or paper.doi or paper.url)
168
+ if institution_configured
169
+ else _configure_institution_action()
170
+ )
171
+ return cls(
172
+ status="partial",
173
+ quality=quality,
174
+ paper=paper,
175
+ reason="insufficient_full_text",
176
+ next_action=next_action,
177
+ )
178
+
179
+ def to_dict(self) -> dict[str, Any]:
180
+ return {
181
+ "status": self.status,
182
+ "quality": self.quality,
183
+ "reason": self.reason,
184
+ "paper": self.paper.to_dict(),
185
+ "next_action": self.next_action.to_dict() if self.next_action else None,
186
+ "attempts": self.attempts,
187
+ }
188
+
189
+ def to_json(self, indent: int = 2) -> str:
190
+ return json.dumps(self.to_dict(), indent=indent, ensure_ascii=False)
191
+
192
+ def to_markdown(self, include_pdf_path: bool = False) -> str:
193
+ lines = [
194
+ f"**Status:** {self.status}",
195
+ f"**Quality:** {self.quality}",
196
+ ]
197
+ if self.reason:
198
+ lines.append(f"**Reason:** {self.reason}")
199
+ if self.next_action:
200
+ lines.append(f"**Next action:** {self.next_action.message}")
201
+ if self.next_action.command:
202
+ lines.append(f"**Command:** `{self.next_action.command}`")
203
+ if self.attempts:
204
+ lines.append("**Attempts:**")
205
+ for attempt in self.attempts:
206
+ item = f"- {attempt.get('stage', 'unknown')}: {attempt.get('status', 'unknown')}"
207
+ if attempt.get("reason"):
208
+ item += f" ({attempt['reason']})"
209
+ if attempt.get("detail"):
210
+ item += f" - {attempt['detail']}"
211
+ lines.append(item)
212
+ lines.append("")
213
+ lines.append(self.paper.to_markdown(include_pdf_path=include_pdf_path))
214
+ return "\n".join(lines)
215
+
216
+ def to_text(self) -> str:
217
+ text = self.paper.to_text()
218
+ if self.status == "success":
219
+ return text
220
+ # Paper.to_text() ends with a newline when it only has a title; without
221
+ # this strip the joined text gets a blank line in the middle.
222
+ text = text.strip()
223
+ parts = [text] if text else []
224
+ if self.next_action:
225
+ parts.append(f"Next action: {self.next_action.message}")
226
+ if self.next_action.command:
227
+ parts.append(f"Command: {self.next_action.command}")
228
+ return "\n\n".join(parts)
229
+
230
+
231
+ def _content_quality(paper: Paper) -> str:
232
+ if paper.full_text:
233
+ return "short_text"
234
+ if paper.pdf_path:
235
+ return "pdf_only"
236
+ if paper.abstract:
237
+ return "abstract_only"
238
+ if paper.title or paper.authors or paper.journal or paper.year:
239
+ return "metadata_only"
240
+ return "none"
241
+
242
+
243
+ def _configure_institution_action() -> NextAction:
244
+ return NextAction(
245
+ kind="configure_institution",
246
+ command="litloop config set instsci_school YOUR_SCHOOL",
247
+ message="Configure your school or institution before retrying institutional access.",
248
+ )
249
+
250
+
251
+ def _login_action(identifier: str = "") -> NextAction:
252
+ command = "litloop instsci-login"
253
+ if identifier:
254
+ command = f"{command} # then retry {identifier}"
255
+ return NextAction(
256
+ kind="login",
257
+ command=command,
258
+ message="Complete or refresh institutional login, then retry the paper.",
259
+ )
260
+
261
+
262
+ def _inspect_pdf_action(pdf_path: str) -> NextAction:
263
+ return NextAction(
264
+ kind="inspect_pdf",
265
+ message=(
266
+ f"PDF was saved to {pdf_path}, but no usable full text was extracted. "
267
+ "Inspect the PDF or retry with another source/version."
268
+ ),
269
+ )