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/store.py ADDED
@@ -0,0 +1,264 @@
1
+ """Files shared by the agent (through MCP) and the desktop app.
2
+
3
+ Both sides run in different processes and read and write the same files
4
+ under ``<litloop home>/screening``:
5
+
6
+ * ``<session>.json`` screening record (ratings, reasons, the user's picks)
7
+ * ``<session>.summary.json`` deep-search overview for the app (small; the pool is not)
8
+ * ``<session>.progress.json`` what a running deep-search round is doing right now
9
+ * ``<session>.pool.json`` deep-search candidate pool (agent only; can be 100+ MB)
10
+
11
+ Rules: a writer holds the file's lock (``filelock``, works across processes on
12
+ Windows, macOS and Linux) for the whole read-modify-write; every write goes
13
+ to a temporary file first and replaces the old one in one step, so a reader
14
+ never sees half a file. Readers need no lock. Each shared file carries
15
+ ``schema``; a reader that finds a newer schema than it knows asks for an
16
+ update instead of misreading it (the app ships its own copy of this code).
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import json
22
+ import os
23
+ import time
24
+ from contextlib import contextmanager
25
+ from pathlib import Path
26
+ from typing import Any, Iterator
27
+
28
+ from filelock import FileLock
29
+
30
+ SCHEMA = 1
31
+ LOCK_TIMEOUT = 60 # seconds; a deep-search round holds a session lock only while saving
32
+
33
+
34
+ class SchemaTooNew(RuntimeError):
35
+ """The file was written by a newer LitLoop; this copy must be updated to read it."""
36
+
37
+
38
+ def pid_alive(pid: int) -> bool:
39
+ """Windows-safe liveness probe. NEVER use os.kill(pid, 0) on Windows —
40
+ any signal value there calls TerminateProcess and would kill the target."""
41
+ if pid <= 0:
42
+ return False
43
+ try:
44
+ if os.name != "nt":
45
+ os.kill(pid, 0)
46
+ return True
47
+ import ctypes
48
+
49
+ SYNCHRONIZE = 0x00100000
50
+ kernel32 = ctypes.windll.kernel32
51
+ handle = kernel32.OpenProcess(SYNCHRONIZE, False, int(pid))
52
+ if not handle:
53
+ return False
54
+ kernel32.CloseHandle(int(handle))
55
+ return True
56
+ except Exception:
57
+ return False
58
+
59
+
60
+ def lock(path: Path) -> FileLock:
61
+ return FileLock(str(path) + ".lock", timeout=LOCK_TIMEOUT)
62
+
63
+
64
+ def _replace(tmp: Path, path: Path) -> None:
65
+ # On Windows the replace fails while another process has the target open
66
+ # for reading; readers hold it only for a moment, so try again briefly.
67
+ for attempt in range(50):
68
+ try:
69
+ os.replace(tmp, path)
70
+ return
71
+ except PermissionError:
72
+ if attempt == 49:
73
+ raise
74
+ time.sleep(0.1)
75
+
76
+
77
+ def write_text(path: Path, text: str) -> None:
78
+ path = Path(path)
79
+ tmp = path.with_name(f".{path.name}.{os.getpid()}.tmp")
80
+ tmp.write_text(text, encoding="utf-8")
81
+ _replace(tmp, path)
82
+
83
+
84
+ def write_json(path: Path, data: Any, *, indent: int | None = None) -> None:
85
+ if isinstance(data, dict):
86
+ data.setdefault("schema", SCHEMA)
87
+ write_text(path, json.dumps(data, ensure_ascii=False, indent=indent))
88
+
89
+
90
+ def _read_text(path: Path) -> str:
91
+ # On Windows, opening a file at the moment a writer replaces it fails with
92
+ # PermissionError (about 1 read in 1000 while a file is rewritten in a loop,
93
+ # 2026-10-26; it failed a CI run). The replace takes a moment: try again.
94
+ for attempt in range(100):
95
+ try:
96
+ return Path(path).read_text(encoding="utf-8")
97
+ except PermissionError:
98
+ if attempt == 99:
99
+ raise
100
+ time.sleep(0.02)
101
+ raise AssertionError("unreachable")
102
+
103
+
104
+ def read_json(path: Path) -> Any:
105
+ data = json.loads(_read_text(path))
106
+ if isinstance(data, dict) and (data.get("schema") or 0) > SCHEMA:
107
+ raise SchemaTooNew(f"{Path(path).name} was written by a newer LitLoop (schema {data['schema']}, "
108
+ f"this copy knows {SCHEMA}); update LitLoop")
109
+ return data
110
+
111
+
112
+ @contextmanager
113
+ def update(path: Path, *, indent: int | None = None) -> Iterator[Any]:
114
+ """Read, let the caller change, and write back ``path`` under its lock."""
115
+ with lock(path):
116
+ data = read_json(path)
117
+ yield data
118
+ write_json(path, data, indent=indent)
119
+
120
+
121
+ # --------------------------------------------------------------------------- deep-search progress
122
+
123
+ def _screening_dir() -> Path:
124
+ from .search.screening import screening_dir
125
+ return screening_dir()
126
+
127
+
128
+ def progress_path(session: str) -> Path:
129
+ return _screening_dir() / f"{session}.progress.json"
130
+
131
+
132
+ def summary_path(session: str) -> Path:
133
+ return _screening_dir() / f"{session}.summary.json"
134
+
135
+
136
+ class Progress:
137
+ """What a deep-search round is doing, for the app to show while it runs.
138
+
139
+ ``phase(...)`` starts a step (expand, abstracts, rank), ``step(done)``
140
+ advances it, ``finish()`` marks the round idle. Writes are cheap and at
141
+ most a few per second, so the app can poll the file.
142
+ """
143
+
144
+ MIN_INTERVAL = 0.5
145
+
146
+ def __init__(self, session: str, event: str):
147
+ self.path = progress_path(session)
148
+ self.data = {"session": session, "event": event, "state": "running",
149
+ "pid": os.getpid(), "started": time.time(), "phase": None, "phases": []}
150
+ self._last = 0.0
151
+
152
+ def _write(self, force: bool = False) -> None:
153
+ now = time.time()
154
+ if not force and now - self._last < self.MIN_INTERVAL:
155
+ return
156
+ self._last = now
157
+ self.data["updated"] = now
158
+ try:
159
+ write_json(self.path, self.data)
160
+ except OSError:
161
+ pass # progress is a convenience; never fail a search over it
162
+
163
+ def phase(self, name: str, total: int | None = None) -> None:
164
+ if self.data["phase"]:
165
+ self.data["phases"].append({"name": self.data["phase"]["name"],
166
+ "seconds": round(time.time() - self.data["phase"]["started"], 1)})
167
+ self.data["phase"] = {"name": name, "done": 0, "total": total, "started": time.time()}
168
+ self._write(force=True)
169
+
170
+ def step(self, done: int, total: int | None = None) -> None:
171
+ if self.data["phase"]:
172
+ self.data["phase"]["done"] = done
173
+ if total is not None:
174
+ self.data["phase"]["total"] = total
175
+ self._write()
176
+
177
+ def finish(self, state: str = "idle", error: str | None = None) -> None:
178
+ last_phase = (self.data.get("phase") or {}).get("name") if isinstance(self.data.get("phase"), dict) else None
179
+ self.phase("done")
180
+ self.data["phase"] = None
181
+ self.data["state"] = state
182
+ self.data["seconds"] = round(time.time() - self.data["started"], 1)
183
+ if state != "idle" and last_phase:
184
+ self.data["stopped_phase"] = last_phase
185
+ else:
186
+ self.data.pop("stopped_phase", None)
187
+ if error:
188
+ self.data["error"] = str(error)[:500]
189
+ else:
190
+ self.data.pop("error", None)
191
+ self._write(force=True)
192
+
193
+
194
+ def _normalize_progress(data: dict[str, Any]) -> dict[str, Any]:
195
+ """Turn a ``running`` file whose writer process has died into ``interrupted``."""
196
+ if data.get("state") == "running":
197
+ raw_pid = data.get("pid")
198
+ pid = 0
199
+ if raw_pid is not None and not isinstance(raw_pid, bool):
200
+ try:
201
+ pid = int(raw_pid)
202
+ except (TypeError, ValueError):
203
+ pid = 0
204
+ if pid > 0 and not pid_alive(pid):
205
+ out = dict(data)
206
+ out["state"] = "interrupted"
207
+ phase = data.get("phase")
208
+ if isinstance(phase, dict) and phase.get("name"):
209
+ out["stopped_phase"] = phase["name"]
210
+ return out
211
+ return data
212
+
213
+
214
+ def read_progress(session: str) -> dict[str, Any] | None:
215
+ try:
216
+ data = read_json(progress_path(session))
217
+ except (OSError, ValueError):
218
+ return None
219
+ return _normalize_progress(data) if isinstance(data, dict) else None
220
+
221
+
222
+ def latest_round_notice() -> dict[str, Any] | None:
223
+ """The most recently touched deep-search round, for the app's system notices.
224
+
225
+ A round the agent (or the app) runs writes ``<session>.progress.json``;
226
+ the desktop app watches it and tells the user when a new batch is ready,
227
+ the same way it does for downloads. The session's question and batch
228
+ count come along so the notice can name what finished.
229
+ """
230
+ best: tuple[float, dict[str, Any]] | None = None
231
+ try:
232
+ paths = list(_screening_dir().glob("*.progress.json"))
233
+ except OSError:
234
+ return None
235
+ for path in paths:
236
+ try:
237
+ data = json.loads(path.read_text(encoding="utf-8"))
238
+ stamp = float(data.get("updated") or data.get("started") or 0)
239
+ except (OSError, ValueError, TypeError):
240
+ continue
241
+ if not isinstance(data, dict) or not data.get("session"):
242
+ continue
243
+ if best is None or stamp > best[0]:
244
+ best = (stamp, data)
245
+ if best is None:
246
+ return None
247
+ data = _normalize_progress(best[1])
248
+ session = str(data["session"])
249
+ try:
250
+ from . import db
251
+ summary = db.load_summary(session) or {}
252
+ except (OSError, ValueError):
253
+ summary = {}
254
+ return {
255
+ "session": session,
256
+ "question": summary.get("question") or "",
257
+ "batches": len(summary.get("batches") or []),
258
+ "event": data.get("event"),
259
+ "state": data.get("state"),
260
+ "phase": (data.get("phase") or {}).get("name") if isinstance(data.get("phase"), dict) else None,
261
+ "started": data.get("started"),
262
+ "updated": data.get("updated"),
263
+ "seconds": data.get("seconds"),
264
+ }
@@ -0,0 +1,347 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>LitLoop — Academic Paper Downloader</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
9
+ <style>
10
+ [x-cloak] { display: none !important; }
11
+ .fade-in { animation: fadeIn 0.3s ease-in; }
12
+ @keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
13
+ .spinner { border: 3px solid #e5e7eb; border-top: 3px solid #3b82f6; border-radius: 50%; width: 24px; height: 24px; animation: spin 0.8s linear infinite; }
14
+ @keyframes spin { to { transform: rotate(360deg); } }
15
+ </style>
16
+ </head>
17
+ <body class="bg-gray-50 min-h-screen" x-data="app()" x-cloak>
18
+
19
+ <!-- Header -->
20
+ <header class="bg-white border-b border-gray-200 shadow-sm">
21
+ <div class="max-w-4xl mx-auto px-4 py-4 flex items-center justify-between">
22
+ <div class="flex items-center gap-3">
23
+ <div class="w-9 h-9 bg-blue-600 rounded-lg flex items-center justify-center">
24
+ <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
25
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/>
26
+ </svg>
27
+ </div>
28
+ <div>
29
+ <h1 class="text-lg font-bold text-gray-900">LitLoop</h1>
30
+ <p class="text-xs text-gray-500">Academic Paper Downloader</p>
31
+ </div>
32
+ </div>
33
+ <div class="flex items-center gap-2">
34
+ <span class="text-xs px-2 py-1 rounded-full"
35
+ :class="status === 'ok' ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500'"
36
+ x-text="status === 'ok' ? 'Online' : 'Checking...'"></span>
37
+ <!-- Source status badges -->
38
+ <template x-if="sources.cloakbrowser !== undefined">
39
+ <div class="flex items-center gap-1 ml-2">
40
+ <span class="text-xs px-1.5 py-0.5 rounded"
41
+ :class="sources.cloakbrowser ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-600'"
42
+ x-text="'Browser: ' + (sources.cloakbrowser ? 'OK' : 'Off')"></span>
43
+ <span class="text-xs px-1.5 py-0.5 rounded"
44
+ :class="sources.tor ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500'"
45
+ x-text="'Tor: ' + (sources.tor ? 'OK' : 'Off')"></span>
46
+ <span x-show="sources.webvpn" class="text-xs px-1.5 py-0.5 rounded bg-blue-100 text-blue-700">WebVPN</span>
47
+ <span x-show="sources.carsi" class="text-xs px-1.5 py-0.5 rounded bg-blue-100 text-blue-700">CARSI</span>
48
+ </div>
49
+ </template>
50
+ </div>
51
+ </div>
52
+ </header>
53
+
54
+ <main class="max-w-4xl mx-auto px-4 py-8">
55
+
56
+ <!-- Search Box -->
57
+ <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
58
+ <form @submit.prevent="doAction()" class="space-y-4">
59
+ <div>
60
+ <label class="block text-sm font-medium text-gray-700 mb-2">
61
+ Enter DOI, title, arXiv ID, or search keywords
62
+ </label>
63
+ <div class="relative">
64
+ <input type="text"
65
+ x-model="query"
66
+ @input="onInput()"
67
+ @keydown.enter.prevent="doAction()"
68
+ placeholder="e.g. 10.1038/nature12373, or paper title..."
69
+ class="w-full px-4 py-3 pr-12 border border-gray-300 rounded-lg text-base
70
+ focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none
71
+ placeholder:text-gray-400 transition">
72
+ <button type="submit"
73
+ :disabled="loading || !query.trim()"
74
+ class="absolute right-2 top-1/2 -translate-y-1/2 p-2 rounded-lg
75
+ bg-blue-600 text-white hover:bg-blue-700 disabled:bg-gray-300
76
+ disabled:cursor-not-allowed transition">
77
+ <svg x-show="!loading" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
78
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
79
+ </svg>
80
+ <div x-show="loading" class="spinner" style="width:20px;height:20px;border-width:2px;"></div>
81
+ </button>
82
+ </div>
83
+ </div>
84
+
85
+ <!-- Action buttons -->
86
+ <div class="flex items-center gap-3">
87
+ <button type="button"
88
+ @click="mode = 'search'; doAction()"
89
+ :disabled="loading || !query.trim()"
90
+ class="px-4 py-2 text-sm rounded-lg border transition"
91
+ :class="mode === 'search' ? 'bg-blue-600 text-white border-blue-600' : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'">
92
+ Search Papers
93
+ </button>
94
+ <button type="button"
95
+ @click="mode = 'download'; doAction()"
96
+ :disabled="loading || !query.trim()"
97
+ class="px-4 py-2 text-sm rounded-lg border transition"
98
+ :class="mode === 'download' ? 'bg-emerald-600 text-white border-emerald-600' : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'">
99
+ Direct Download
100
+ </button>
101
+ </div>
102
+ </form>
103
+ </div>
104
+
105
+ <!-- Error -->
106
+ <div x-show="error" class="bg-red-50 border border-red-200 rounded-lg p-4 mb-6 fade-in">
107
+ <div class="flex items-start gap-3">
108
+ <svg class="w-5 h-5 text-red-500 mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
109
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
110
+ </svg>
111
+ <div class="flex-1">
112
+ <p class="text-sm text-red-800 font-medium" x-text="error"></p>
113
+ <p x-show="errorHint" class="text-xs text-red-600 mt-1" x-text="errorHint"></p>
114
+ <ul x-show="errorGuidance.length > 0" class="mt-2 space-y-1">
115
+ <template x-for="(g, i) in errorGuidance" :key="i">
116
+ <li class="text-xs text-red-600 flex items-start gap-1.5">
117
+ <span class="text-red-400 mt-0.5">&#8226;</span>
118
+ <span x-text="g"></span>
119
+ </li>
120
+ </template>
121
+ </ul>
122
+ </div>
123
+ </div>
124
+ </div>
125
+
126
+ <!-- Download Progress -->
127
+ <div x-show="downloading" class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6 fade-in">
128
+ <div class="flex items-center gap-3">
129
+ <div class="spinner"></div>
130
+ <div class="flex-1">
131
+ <p class="text-sm text-blue-800 font-medium">Downloading PDF...</p>
132
+ <p class="text-xs text-blue-600" x-text="downloadMsg"></p>
133
+ <p class="text-xs text-blue-400 mt-1" x-show="downloadElapsed > 0"
134
+ x-text="'Elapsed: ' + downloadElapsed + 's — racing 13+ sources in parallel'"></p>
135
+ </div>
136
+ </div>
137
+ </div>
138
+
139
+ <!-- Search Results -->
140
+ <div x-show="results.length > 0" class="space-y-3 mb-6">
141
+ <div class="flex items-center justify-between">
142
+ <h2 class="text-sm font-semibold text-gray-700">
143
+ <span x-text="results.length"></span> results found
144
+ </h2>
145
+ </div>
146
+
147
+ <template x-for="(paper, idx) in results" :key="idx">
148
+ <div class="bg-white rounded-lg border border-gray-200 p-4 hover:border-blue-300 hover:shadow-sm transition fade-in"
149
+ :style="'animation-delay:' + (idx * 50) + 'ms'">
150
+ <div class="flex items-start justify-between gap-4">
151
+ <div class="flex-1 min-w-0">
152
+ <h3 class="text-sm font-semibold text-gray-900 leading-snug"
153
+ x-text="paper.title || paper.doi || 'Unknown'"></h3>
154
+ <p class="text-xs text-gray-500 mt-1"
155
+ x-show="paper.authors && paper.authors.length > 0"
156
+ x-text="(paper.authors || []).slice(0, 3).join(', ') + (paper.authors && paper.authors.length > 3 ? ' et al.' : '')"></p>
157
+ <div class="flex items-center gap-3 mt-2 flex-wrap">
158
+ <span x-show="paper.year" class="text-xs text-gray-400" x-text="paper.year"></span>
159
+ <span x-show="paper.doi" class="text-xs font-mono text-blue-600 truncate max-w-xs" x-text="paper.doi"></span>
160
+ <span x-show="paper.is_oa" class="text-xs px-1.5 py-0.5 bg-green-100 text-green-700 rounded">Open Access</span>
161
+ <span x-show="paper.cited_by_count > 0" class="text-xs text-gray-400"
162
+ x-text="'Cited: ' + paper.cited_by_count"></span>
163
+ </div>
164
+ <p x-show="paper.abstract" class="text-xs text-gray-500 mt-2 line-clamp-2"
165
+ x-text="(paper.abstract || '').substring(0, 200)"></p>
166
+ </div>
167
+ <button @click="downloadPaper(paper.doi || paper.identifier)"
168
+ :disabled="downloading"
169
+ class="flex-shrink-0 px-3 py-2 text-sm rounded-lg bg-emerald-600 text-white
170
+ hover:bg-emerald-700 disabled:bg-gray-300 disabled:cursor-not-allowed
171
+ transition flex items-center gap-1.5">
172
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
173
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
174
+ </svg>
175
+ PDF
176
+ </button>
177
+ </div>
178
+ </div>
179
+ </template>
180
+ </div>
181
+
182
+ <!-- Download History -->
183
+ <div x-show="history.length > 0" class="mt-8">
184
+ <h2 class="text-sm font-semibold text-gray-500 mb-3">Recent Downloads</h2>
185
+ <div class="space-y-2">
186
+ <template x-for="(item, idx) in history" :key="idx">
187
+ <div class="flex items-center gap-3 text-sm bg-white rounded-lg border border-gray-100 px-4 py-2.5">
188
+ <svg class="w-4 h-4 text-green-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
189
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
190
+ </svg>
191
+ <span class="text-gray-900 truncate flex-1" x-text="item.title || item.identifier"></span>
192
+ <span class="text-xs text-gray-400 flex-shrink-0" x-text="item.source"></span>
193
+ </div>
194
+ </template>
195
+ </div>
196
+ </div>
197
+
198
+ <!-- Empty state -->
199
+ <div x-show="!loading && !downloading && results.length === 0 && !error && !history.length"
200
+ class="text-center py-16 text-gray-400">
201
+ <svg class="w-16 h-16 mx-auto mb-4 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
202
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/>
203
+ </svg>
204
+ <p class="text-lg font-medium">Enter a DOI, title, or keywords to start</p>
205
+ <p class="text-sm mt-1">Supports 13+ sources including Open Access, Sci-Hub, and institutional proxies</p>
206
+ </div>
207
+ </main>
208
+
209
+ <!-- Footer -->
210
+ <footer class="max-w-4xl mx-auto px-4 py-6 text-center text-xs text-gray-400 border-t border-gray-100 mt-8">
211
+ LitLoop &mdash; 13+ sources, parallel racing, institutional proxy support
212
+ </footer>
213
+
214
+ <script>
215
+ function app() {
216
+ return {
217
+ query: '',
218
+ mode: 'search',
219
+ results: [],
220
+ history: [],
221
+ loading: false,
222
+ downloading: false,
223
+ downloadMsg: '',
224
+ downloadElapsed: 0,
225
+ error: '',
226
+ errorHint: '',
227
+ errorGuidance: [],
228
+ status: 'checking',
229
+ sources: {},
230
+
231
+ async init() {
232
+ try {
233
+ const r = await fetch('/api/status');
234
+ const d = await r.json();
235
+ this.status = d.status;
236
+ this.sources = d.sources || {};
237
+ } catch { this.status = 'offline'; }
238
+ },
239
+
240
+ onInput() {
241
+ this.error = '';
242
+ this.errorHint = '';
243
+ this.errorGuidance = [];
244
+ // Auto-detect mode
245
+ const q = this.query.trim();
246
+ if (/^10\.\d{4,}\//.test(q) || /^https?:\/\/doi\.org\//.test(q) || /^\d{4}\.\d{4,}/.test(q)) {
247
+ this.mode = 'download';
248
+ }
249
+ },
250
+
251
+ doAction() {
252
+ if (!this.query.trim()) return;
253
+ if (this.mode === 'download') {
254
+ this.downloadPaper(this.query.trim());
255
+ } else {
256
+ this.searchPapers();
257
+ }
258
+ },
259
+
260
+ async searchPapers() {
261
+ this.loading = true;
262
+ this.error = '';
263
+ this.errorHint = '';
264
+ this.errorGuidance = [];
265
+ this.results = [];
266
+
267
+ try {
268
+ const resp = await fetch('/api/search', {
269
+ method: 'POST',
270
+ headers: {'Content-Type': 'application/json'},
271
+ body: JSON.stringify({query: this.query.trim(), limit: 10}),
272
+ });
273
+ if (!resp.ok) {
274
+ const d = await resp.json();
275
+ this.error = d.error || 'Search failed';
276
+ return;
277
+ }
278
+ this.results = await resp.json();
279
+ if (this.results.length === 0) {
280
+ this.error = 'No results found. Try different keywords.';
281
+ }
282
+ } catch (e) {
283
+ this.error = 'Network error: ' + e.message;
284
+ } finally {
285
+ this.loading = false;
286
+ }
287
+ },
288
+
289
+ async downloadPaper(identifier) {
290
+ if (!identifier) return;
291
+ this.downloading = true;
292
+ this.downloadMsg = identifier;
293
+ this.downloadElapsed = 0;
294
+ this.error = '';
295
+ this.errorHint = '';
296
+ this.errorGuidance = [];
297
+
298
+ // Elapsed timer
299
+ const timer = setInterval(() => { this.downloadElapsed++; }, 1000);
300
+
301
+ try {
302
+ const resp = await fetch('/api/download', {
303
+ method: 'POST',
304
+ headers: {'Content-Type': 'application/json'},
305
+ body: JSON.stringify({identifier}),
306
+ });
307
+
308
+ if (resp.ok && resp.headers.get('content-type')?.includes('application/pdf')) {
309
+ // Trigger browser download
310
+ const blob = await resp.blob();
311
+ const cd = resp.headers.get('content-disposition') || '';
312
+ let filename = 'paper.pdf';
313
+ const m = cd.match(/filename[*]?=(?:UTF-8''|"?)([^";]+)/i);
314
+ if (m) filename = decodeURIComponent(m[1].replace(/"/g, ''));
315
+ const url = URL.createObjectURL(blob);
316
+ const a = document.createElement('a');
317
+ a.href = url; a.download = filename;
318
+ document.body.appendChild(a); a.click();
319
+ document.body.removeChild(a);
320
+ URL.revokeObjectURL(url);
321
+
322
+ const realSource = resp.headers.get('x-litloop-source') || 'unknown';
323
+ this.history.unshift({title: filename.replace('.pdf',''), identifier, source: realSource});
324
+ if (this.history.length > 20) this.history.pop();
325
+ } else {
326
+ const d = await resp.json();
327
+ this.error = d.error || d.reason || 'Download failed';
328
+ this.errorHint = d.hint?.message || '';
329
+ this.errorGuidance = d.guidance || [];
330
+ // Update source status if returned
331
+ if (d.sources) this.sources = d.sources;
332
+ }
333
+ } catch (e) {
334
+ this.error = 'Network error: ' + e.message;
335
+ } finally {
336
+ clearInterval(timer);
337
+ this.downloading = false;
338
+ this.downloadMsg = '';
339
+ this.downloadElapsed = 0;
340
+ }
341
+ }
342
+ };
343
+ }
344
+ </script>
345
+
346
+ </body>
347
+ </html>
@@ -0,0 +1 @@
1
+ """Search vocabulary: normalisation, spellings, the glossary."""