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
@@ -0,0 +1,999 @@
1
+ """Local HTTP backend for the LitLoop desktop app.
2
+
3
+ The Tauri shell starts this as a child process:
4
+
5
+ python -m litloop.desktop.server --token <random>
6
+
7
+ It listens on 127.0.0.1 only, on a free port it picks itself, and prints one
8
+ line to stdout before serving, so the shell knows where to connect:
9
+
10
+ {"litloop_app_server": 1, "port": 51234}
11
+
12
+ Every request must carry the token (header ``X-LitLoop-Token``), so another
13
+ web page in the user's browser cannot call it. Data comes from the files
14
+ described in docs/APP_DATA.md, read and written through ``store``.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import argparse
20
+ import json
21
+ import re
22
+ import secrets
23
+ import socket
24
+ import sys
25
+ import time
26
+ from typing import Any
27
+
28
+ from fastapi import FastAPI, HTTPException, Request
29
+ from fastapi.middleware.cors import CORSMiddleware
30
+ from fastapi.responses import JSONResponse
31
+ from pydantic import BaseModel
32
+
33
+ from .. import __version__, db, store
34
+ from ..search import screening
35
+
36
+ # Where the app's pages are served from: the Vite dev server, and Tauri's
37
+ # own origins (Windows uses http://tauri.localhost, macOS/Linux tauri://localhost).
38
+ ORIGINS = ["http://localhost:1420", "http://tauri.localhost", "https://tauri.localhost", "tauri://localhost"]
39
+ _SESSION_ID = re.compile(r"^[\w一-鿿.-]{1,200}$")
40
+
41
+
42
+ class Rating(BaseModel):
43
+ doi: str | None = None
44
+ title: str | None = None
45
+ relevance: str | None = None # None clears the user's rating: the agent's counts again
46
+
47
+
48
+ class Setting(BaseModel):
49
+ key: str
50
+ value: Any = None
51
+
52
+
53
+ class Paper(BaseModel):
54
+ doi: str
55
+ title: str | None = None
56
+ year: int | None = None
57
+ journal: str | None = None
58
+
59
+
60
+ class SkipPaper(BaseModel):
61
+ doi: str
62
+
63
+
64
+ class StartQueue(BaseModel):
65
+ publisher: str
66
+
67
+
68
+ class NewSearch(BaseModel):
69
+ """The app's search form; agent_runner.build_prompt turns it into the message."""
70
+ question: str
71
+ criteria: str = ""
72
+ mode: str = "quick"
73
+ limit: int = 20
74
+ batches: int = 0
75
+ year_from: int | None = None
76
+ year_to: int | None = None
77
+ max_cas_zone: int | None = None
78
+ max_jcr_quartile: int | None = None
79
+ exclude_warning: bool = False
80
+ seeds: str = ""
81
+ seed_collection: str = ""
82
+
83
+
84
+ class ResumeSearch(BaseModel):
85
+ batches: int = 0
86
+
87
+
88
+ class NewDownload(BaseModel):
89
+ papers: list[Paper]
90
+ title: str
91
+ session: str | None = None
92
+ zotero_target: str | None = None
93
+
94
+
95
+ class PastedList(BaseModel):
96
+ text: str
97
+
98
+
99
+ class ReferenceLines(BaseModel):
100
+ lines: list[str]
101
+
102
+
103
+ class ZoteroCheck(BaseModel):
104
+ key: str
105
+
106
+
107
+ class ZoteroRepair(BaseModel):
108
+ item_key: str
109
+ doi: str | None = None
110
+ trash: list[str] = []
111
+ attach: bool = True # False: only move the empty attachments to the Trash
112
+
113
+
114
+ class ZoteroPush(BaseModel):
115
+ collection: str | None = None # None: <zotero_collection_root>/<job title>
116
+ tags: list[str] = []
117
+
118
+
119
+ class OpenPath(BaseModel):
120
+ path: str
121
+
122
+
123
+ class Onboarding(BaseModel):
124
+ done: bool = True
125
+
126
+
127
+ class GlossaryEntry(BaseModel):
128
+ terms: list[str] | None = None
129
+ note: str | None = None
130
+ confirm: bool = False # a suggestion becomes an entry in use
131
+
132
+
133
+ class BuiltinSwitch(BaseModel):
134
+ id: str # "element:<symbol>" or "spelling:<American word>"
135
+ enabled: bool
136
+
137
+
138
+ class Translate(BaseModel):
139
+ doi: str | None = None
140
+ title: str | None = None
141
+
142
+
143
+ class Export(BaseModel):
144
+ format: str = "bibtex" # bibtex | ris | doi
145
+ which: str = "selected" # selected | high
146
+
147
+
148
+ class Watch(BaseModel):
149
+ session: str
150
+ query: str = ""
151
+
152
+
153
+ class Decision(BaseModel):
154
+ doi: str | None = None
155
+ title: str | None = None
156
+ decision: str | None = None # "selected", "rejected" or None
157
+ reason: str | None = None # why, when rejecting; optional
158
+
159
+
160
+ class BatchOp(BaseModel):
161
+ doi: str | None = None
162
+ title: str | None = None
163
+ relevance: str | None = None
164
+ decision: str | None = None
165
+ reject_reason: str | None = None
166
+ clear_user_relevance: bool = False
167
+ delete_if_unrated: bool = False
168
+
169
+
170
+ class BatchUpdate(BaseModel):
171
+ ops: list[BatchOp]
172
+
173
+
174
+ _OPENALEX: dict[str, Any] = {"at": 0.0, "data": None} # see /api/quota/openalex
175
+ _APP_UPDATE: dict[str, Any] = {"at": 0.0, "data": None} # see /api/app-update
176
+ _APP_UPDATE_URL = "https://api.github.com/repos/dysprosium231/litloop-paper-agent/releases/latest"
177
+ _WRITE_MODE: dict[str, Any] = {} # zotero-mcp's write mode, cached until the user authorizes
178
+
179
+
180
+ def _parse_version_tuple(version: str) -> tuple[int, ...] | None:
181
+ """Extract numeric version parts (e.g. 'v1.18.3' -> (1, 18, 3))."""
182
+ cleaned = version.strip().lstrip("vV")
183
+ if not cleaned:
184
+ return None
185
+ parts: list[int] = []
186
+ for piece in re.split(r"[.+_-]", cleaned):
187
+ if piece.isdigit():
188
+ parts.append(int(piece))
189
+ else:
190
+ m = re.match(r"^(\d+)", piece)
191
+ if m:
192
+ parts.append(int(m.group(1)))
193
+ break
194
+ return tuple(parts) if parts else None
195
+
196
+
197
+ def _explain(d: dict[str, Any] | None, action: str = "下载", full: bool = False) -> None:
198
+ """Add ``error_text`` (docs/UI_COPY.md section 7) next to a raw ``error``; the raw text stays for the tooltip."""
199
+ from . import error_text
200
+ if d and d.get("error"):
201
+ d["error_text"] = error_text.message(d["error"], action) if full else error_text.reason(d["error"])
202
+ if not full:
203
+ hint = error_text.next_step(d["error"])
204
+ if hint:
205
+ d["error_hint"] = hint
206
+ act = error_text.action_for(d["error"])
207
+ if act:
208
+ d["error_action"] = act
209
+
210
+
211
+ def _fail(status: int, raw: str | None, action: str, fallback: str) -> HTTPException:
212
+ from . import error_text
213
+ return HTTPException(status, error_text.message(raw or fallback, action))
214
+
215
+
216
+ def _record_path(session: str) -> str:
217
+ if not _SESSION_ID.match(session) or session.startswith("."):
218
+ raise HTTPException(400, "无法打开检索会话:会话编号不正确")
219
+ if not db.session_exists(session):
220
+ raise HTTPException(404, "无法打开检索会话:此会话已不存在")
221
+ return session
222
+
223
+
224
+ def _find(items: list[dict[str, Any]], doi: str | None, title: str | None) -> dict[str, Any]:
225
+ key = screening._key({"doi": doi, "title": title})
226
+ for it in items:
227
+ if screening._key(it) == key:
228
+ return it
229
+ raise HTTPException(404, "此检索会话中没有这篇文献")
230
+
231
+
232
+ def create_app(token: str) -> FastAPI:
233
+ app = FastAPI(title="LitLoop app backend", version=__version__, docs_url=None, redoc_url=None)
234
+ app.add_middleware(CORSMiddleware, allow_origins=ORIGINS, allow_methods=["GET", "POST"],
235
+ allow_headers=["X-LitLoop-Token", "Content-Type"])
236
+
237
+ @app.middleware("http")
238
+ async def require_token(request: Request, call_next):
239
+ if request.method != "OPTIONS" and not secrets.compare_digest(
240
+ request.headers.get("X-LitLoop-Token", ""), token):
241
+ return JSONResponse({"detail": "missing or wrong token"}, status_code=401)
242
+ return await call_next(request)
243
+
244
+ @app.get("/api/health")
245
+ def health() -> dict[str, Any]:
246
+ return {"version": __version__, "schema": store.SCHEMA}
247
+
248
+ @app.get("/api/sessions")
249
+ def sessions(limit: int = 50) -> list[dict[str, Any]]:
250
+ from ..search import deep as deep_search
251
+ out = screening.list_sessions(limit)
252
+ for s in out:
253
+ s["deep"] = (db.load_summary(s["session"]) is not None) or deep_search.has_pool(s["session"])
254
+ return out
255
+
256
+ @app.get("/api/sessions/{session}")
257
+ def session(session: str) -> dict[str, Any]:
258
+ from ..search import deep as deep_search
259
+ _record_path(session)
260
+ record = db.load_screening(session)
261
+ summary = db.load_summary(session)
262
+ if summary is not None:
263
+ # The file keeps the agent's English notes; the app shows today's, in Chinese.
264
+ from .. import quota
265
+ summary["api_warnings"] = quota.warnings("zh")
266
+ return {"record": record, "summary": summary, "resumable": deep_search.has_pool(session)}
267
+
268
+ @app.get("/api/sessions/{session}/progress")
269
+ def progress(session: str) -> dict[str, Any] | None:
270
+ _record_path(session)
271
+ p = store.read_progress(session)
272
+ if p and p.get("error"):
273
+ _explain(p, "检索", full=True)
274
+ return p
275
+
276
+ @app.get("/api/sessions/{session}/prisma")
277
+ def prisma_view(session: str) -> dict[str, Any]:
278
+ _record_path(session)
279
+ return screening.prisma(session)
280
+
281
+ @app.post("/api/sessions/{session}/rating")
282
+ def rate(session: str, body: Rating) -> dict[str, Any]:
283
+ if body.relevance is not None and body.relevance not in screening.RELEVANCE:
284
+ raise HTTPException(400, f"无法评分:评分只能是{'、'.join(screening.RELEVANCE)}")
285
+ _record_path(session)
286
+ res = db.update_item_rating(session, body.doi, body.title, body.relevance)
287
+ if res is None:
288
+ if body.relevance is None:
289
+ raise HTTPException(404, "此检索会话中没有这篇文献")
290
+ from ..search import deep as deep_search
291
+ try:
292
+ paper = deep_search.batch_paper(session, body.doi, body.title)
293
+ except FileNotFoundError:
294
+ paper = None
295
+ if not paper:
296
+ raise HTTPException(404, "此检索会话中没有这篇文献")
297
+ res = db.update_item_rating(
298
+ session, body.doi, body.title, body.relevance, create_paper=paper
299
+ )
300
+ if res is None:
301
+ raise HTTPException(404, "此检索会话中没有这篇文献")
302
+ return res
303
+
304
+ @app.post("/api/sessions/{session}/next")
305
+ def next_round(session: str) -> dict[str, Any]:
306
+ """The app's continue button: serve the next batch here, without the agent (it rates the rest when it is back)."""
307
+ import threading
308
+ import time
309
+
310
+ from ..search import deep as deep_search
311
+ _record_path(session)
312
+ if not deep_search.has_pool(session):
313
+ raise HTTPException(400, "无法继续检索:这不是深度检索会话。")
314
+ p = store.read_progress(session)
315
+ if p and p.get("state") == "running" and (p.get("pid") or time.time() - float(p.get("updated") or 0) < 300):
316
+ raise HTTPException(409, "正在生成下一批,完成后再继续。")
317
+
318
+ def run() -> None:
319
+ try:
320
+ deep_search.next_batch(session, force=True)
321
+ except Exception: # the progress file says it failed; the app shows that
322
+ pass
323
+ threading.Thread(target=run, name=f"next-{session}", daemon=True).start()
324
+ return {"started": True}
325
+
326
+ @app.post("/api/sessions/{session}/resume")
327
+ def resume_session(session: str, body: ResumeSearch | None = None) -> dict[str, Any]:
328
+ """Resume a deep-search session through the configured agent."""
329
+ from . import agent_runner
330
+ _record_path(session)
331
+ try:
332
+ return agent_runner.resume(session, batches=body.batches if body else 0)
333
+ except ValueError as exc:
334
+ raise HTTPException(400, str(exc)) from exc
335
+ except RuntimeError as exc:
336
+ raise HTTPException(409, str(exc)) from exc
337
+
338
+ @app.get("/api/updates")
339
+ def updates_available() -> list[dict[str, Any]]:
340
+ """Components with a newer version (from the weekly check; a due check starts in the background)."""
341
+ from .. import updates
342
+ return updates.available()
343
+
344
+ @app.get("/api/history")
345
+ def history() -> list[dict[str, Any]]:
346
+ """Every screening session with its counts and its final picks (the app's history page)."""
347
+ return screening.history()
348
+
349
+ @app.get("/api/watches")
350
+ def watches() -> list[dict[str, Any]]:
351
+ """Saved searches, newest check first (定期追踪)."""
352
+ from ..search import watches as saved
353
+ return saved.list_watches()
354
+
355
+ @app.post("/api/watches")
356
+ def add_watch(body: Watch) -> dict[str, Any]:
357
+ """Remember this session's last search so later checks report only new papers."""
358
+ from ..search import watches as saved
359
+ record = screening.load(body.session)
360
+ searches = record.get("searches") or []
361
+ last = searches[-1] if searches else {}
362
+ core = list((last.get("params") or {}).get("core_terms") or [])
363
+ query = body.query or str(last.get("query") or "") or str(record.get("question") or "")
364
+ try:
365
+ return saved.add(record.get("question", ""), query,
366
+ core, record.get("items") or [], body.session)
367
+ except ValueError as exc:
368
+ raise HTTPException(400, str(exc)) from exc
369
+
370
+ @app.delete("/api/watches/{wid}")
371
+ def delete_watch(wid: str) -> dict[str, Any]:
372
+ from ..search import watches as saved
373
+ if not saved.remove(wid):
374
+ raise HTTPException(404, "没有这个追踪")
375
+ return {"removed": True}
376
+
377
+ @app.post("/api/watches/check-due")
378
+ def check_due_watches() -> dict[str, Any]:
379
+ """Check the watches that are older than watch_check_days (the app calls this once on open)."""
380
+ from ..search import watches
381
+ return watches.check_due()
382
+
383
+ @app.post("/api/watches/{wid}/check")
384
+ def check_watch(wid: str) -> dict[str, Any]:
385
+ """Run the saved search now; only papers this watch has not seen come back."""
386
+ from ..search import watches as saved
387
+ try:
388
+ return saved.check(wid)
389
+ except ValueError as exc:
390
+ raise HTTPException(404, str(exc)) from exc
391
+ except Exception as exc: # network, quota, source outage
392
+ raise HTTPException(502, f"无法检查追踪:{exc}") from exc
393
+
394
+ @app.post("/api/sessions/{session}/prisma/export")
395
+ def export_prisma(session: str) -> dict[str, Any]:
396
+ """Write the session's flow counts as Markdown under <output_dir>/exports."""
397
+ from ..citations import session_export as export
398
+ _record_path(session)
399
+ return export.prisma_export(session)
400
+
401
+ @app.post("/api/sessions/{session}/export")
402
+ def export_session(session: str, body: Export) -> dict[str, Any]:
403
+ """Write the session's picks (or its 高 papers) as BibTeX / RIS / a DOI list under <output_dir>/exports."""
404
+ from ..citations import session_export as export
405
+ _record_path(session)
406
+ if body.which not in ("selected", "high"):
407
+ raise HTTPException(400, "无法导出:只能导出待下载或评为高的文献")
408
+ try:
409
+ return export.export(session, body.format, body.which)
410
+ except ValueError as exc:
411
+ raise HTTPException(400, "无法导出:没有可导出的文献" if "no papers" in str(exc) else "无法导出:不支持此格式") from exc
412
+
413
+ @app.get("/api/translation")
414
+ def translation() -> dict[str, Any]:
415
+ """What the detail pane needs: who translates, which ratings without asking, whether it can."""
416
+ from . import translate
417
+ from ..config import load_config
418
+ config = load_config()
419
+ name = translate.provider(config)
420
+ return {"provider": name, "auto": screening.translate_wanted(config),
421
+ "ready": name != "agent" and bool(str(config.get(f"{name}_api_key") or "").strip())}
422
+
423
+ @app.post("/api/sessions/{session}/translate")
424
+ def translate_abstract(session: str, body: Translate) -> dict[str, Any]:
425
+ """Translate one paper's abstract with DeepSeek / DeepL and keep it in the record."""
426
+ from . import translate
427
+ _record_path(session)
428
+ item = _find(db.load_screening(session)["items"], body.doi, body.title)
429
+ if item.get("abstract_zh"):
430
+ return {"abstract_zh": item["abstract_zh"]}
431
+ if not item.get("abstract"):
432
+ raise HTTPException(400, "无法翻译:此文献暂无摘要")
433
+ try:
434
+ text = translate.translate_text(item["abstract"])
435
+ except translate.TranslationError as exc:
436
+ raise HTTPException(502, f"无法翻译:{exc}。") from exc
437
+ if not db.set_item_abstract_zh(session, body.doi, body.title, text):
438
+ raise HTTPException(404, "此检索会话中没有这篇文献")
439
+ return {"abstract_zh": text}
440
+
441
+ @app.post("/api/sessions/{session}/decision")
442
+ def decide(session: str, body: Decision) -> dict[str, Any]:
443
+ if body.decision not in (None, "selected", "rejected"):
444
+ raise HTTPException(400, "无法保存:只能加入下载或排除")
445
+ _record_path(session)
446
+ res = db.update_item_decision(session, body.doi, body.title, body.decision, body.reason)
447
+ if res is None:
448
+ raise HTTPException(404, "此检索会话中没有这篇文献")
449
+ return res
450
+
451
+ @app.post("/api/sessions/{session}/batch")
452
+ def batch_update(session: str, body: BatchUpdate) -> dict[str, Any]:
453
+ _record_path(session)
454
+ if not body.ops:
455
+ raise HTTPException(400, "无法批量保存:未选择文献")
456
+ for op in body.ops:
457
+ if "relevance" in op.model_fields_set and op.relevance is not None and op.relevance not in screening.RELEVANCE:
458
+ raise HTTPException(400, f"无法评分:评分只能是{'、'.join(screening.RELEVANCE)}")
459
+ if "decision" in op.model_fields_set and op.decision not in (None, "selected", "rejected"):
460
+ raise HTTPException(400, "无法保存:只能加入下载或排除")
461
+
462
+ record = db.load_screening(session)
463
+ existing_keys = {screening._key(it) for it in record["items"]}
464
+ create_papers: dict[str, dict[str, Any]] = {}
465
+ for op in body.ops:
466
+ if op.delete_if_unrated or op.clear_user_relevance:
467
+ continue
468
+ if "relevance" not in op.model_fields_set or op.relevance is None:
469
+ continue
470
+ key = screening._key({"doi": op.doi, "title": op.title})
471
+ if key in existing_keys or key in create_papers:
472
+ continue
473
+ from ..search import deep as deep_search
474
+ try:
475
+ paper = deep_search.batch_paper(session, op.doi, op.title)
476
+ except FileNotFoundError:
477
+ paper = None
478
+ if paper:
479
+ create_papers[key] = paper
480
+
481
+ return db.batch_update_items(session, body.ops, create_papers=create_papers)
482
+
483
+ @app.get("/api/glossary")
484
+ def glossary_list() -> dict[str, Any]:
485
+ from ..terms import glossary
486
+ return {"active": glossary.entries("active"), "suggested": glossary.entries("suggested")}
487
+
488
+ @app.post("/api/glossary")
489
+ def glossary_add(body: GlossaryEntry) -> dict[str, Any]:
490
+ from ..terms import glossary
491
+ try:
492
+ return glossary.add(body.terms or [], note=body.note or "")
493
+ except ValueError as exc:
494
+ raise HTTPException(400, str(exc)) from exc
495
+
496
+ @app.get("/api/glossary/builtin")
497
+ def glossary_builtin(q: str = "") -> dict[str, Any]:
498
+ from ..terms import glossary
499
+ return glossary.builtin(q)
500
+
501
+ @app.post("/api/glossary/builtin")
502
+ def glossary_builtin_switch(body: BuiltinSwitch) -> dict[str, Any]:
503
+ from ..terms import glossary
504
+ try:
505
+ glossary.set_builtin(body.id, body.enabled)
506
+ except KeyError as exc:
507
+ raise HTTPException(404, str(exc).strip("'\"")) from exc
508
+ return {"id": body.id, "disabled": not body.enabled}
509
+
510
+ @app.post("/api/glossary/{entry_id}")
511
+ def glossary_update(entry_id: str, body: GlossaryEntry) -> dict[str, Any]:
512
+ from ..terms import glossary
513
+ try:
514
+ return glossary.update(entry_id, terms=body.terms, note=body.note, confirm=body.confirm)
515
+ except KeyError as exc:
516
+ raise HTTPException(404, str(exc).strip("'\"")) from exc
517
+ except ValueError as exc:
518
+ raise HTTPException(400, str(exc)) from exc
519
+
520
+ @app.post("/api/glossary/{entry_id}/delete")
521
+ def glossary_delete(entry_id: str) -> dict[str, Any]:
522
+ """POST: the app's CORS allows GET and POST only. A deleted suggestion is not suggested again."""
523
+ from ..terms import glossary
524
+ try:
525
+ glossary.delete(entry_id)
526
+ except KeyError as exc:
527
+ raise HTTPException(404, str(exc).strip("'\"")) from exc
528
+ return {"deleted": entry_id}
529
+
530
+ @app.get("/api/quota")
531
+ def quota_summary() -> dict[str, Any]:
532
+ from .. import quota
533
+ return quota.summary()
534
+
535
+ @app.get("/api/quota/openalex")
536
+ def quota_openalex(fresh: bool = False) -> dict[str, Any] | None:
537
+ """OpenAlex's own count (costs no credits, but takes seconds): kept two minutes."""
538
+ import time
539
+
540
+ from .. import quota
541
+ if fresh or time.time() - _OPENALEX["at"] > 120:
542
+ _OPENALEX.update(at=time.time(), data=quota.openalex_live())
543
+ return _OPENALEX["data"]
544
+
545
+ @app.get("/api/settings")
546
+ def settings_list() -> list[dict[str, Any]]:
547
+ from . import settings as app_settings
548
+ return app_settings.settings()
549
+
550
+ @app.post("/api/settings")
551
+ def settings_update(body: Setting) -> dict[str, Any]:
552
+ from . import settings as app_settings
553
+ try:
554
+ return app_settings.update(body.key, body.value)
555
+ except KeyError as exc:
556
+ raise HTTPException(400, "无法保存:此项暂不支持在 app 中修改") from exc
557
+ except ValueError as exc:
558
+ raise HTTPException(400, str(exc)) from exc
559
+
560
+ @app.get("/api/onboarding")
561
+ def onboarding() -> dict[str, Any]:
562
+ from . import settings as app_settings
563
+ return app_settings.onboarding()
564
+
565
+ @app.post("/api/onboarding")
566
+ def onboarding_done(body: Onboarding) -> dict[str, Any]:
567
+ """done=true: finished or skipped; done=false: show the guide again (settings page)."""
568
+ from . import settings as app_settings
569
+ return app_settings.finish_onboarding(body.done)
570
+
571
+ @app.post("/api/settings/test/{group}")
572
+ def settings_test(group: str) -> dict[str, Any]:
573
+ from . import settings as app_settings
574
+ try:
575
+ return app_settings.run_test(group)
576
+ except KeyError as exc:
577
+ raise HTTPException(404, "无法完成检测:此分组没有检测项") from exc
578
+
579
+ @app.get("/api/components/{key}/status")
580
+ def component_status(key: str) -> dict[str, Any]:
581
+ """Return the current install progress and state for an optional component."""
582
+ from .. import optional
583
+ try:
584
+ return optional.install_status(key)
585
+ except KeyError as exc:
586
+ raise HTTPException(404, "没有这个可选组件") from exc
587
+
588
+ @app.post("/api/components/{key}/install")
589
+ def component_install(key: str) -> dict[str, Any]:
590
+ """Download an optional component (the layout model) into the data dir."""
591
+ from .. import optional
592
+ if key not in optional.COMPONENTS:
593
+ raise HTTPException(404, "没有这个组件")
594
+ from ..config import load_config
595
+ try:
596
+ return optional.install(key, index_url=str(load_config().get("package_index_url") or ""))
597
+ except KeyError as exc:
598
+ raise HTTPException(404, "没有这个组件") from exc
599
+
600
+ @app.get("/api/app-update")
601
+ def app_update(fresh: bool = False) -> dict[str, Any]:
602
+ """Check GitHub Releases for a newer LitLoop version (cached 10 minutes)."""
603
+ now = time.time()
604
+ cached = _APP_UPDATE.get("data")
605
+ if not fresh and cached is not None and now - float(_APP_UPDATE.get("at") or 0.0) <= 600:
606
+ return cached
607
+
608
+ base: dict[str, Any] = {
609
+ "current": __version__,
610
+ "latest": "",
611
+ "available": False,
612
+ "url": "",
613
+ "notes": "",
614
+ "error": "",
615
+ }
616
+ import requests
617
+ try:
618
+ resp = requests.get(
619
+ _APP_UPDATE_URL,
620
+ headers={
621
+ "Accept": "application/vnd.github+json",
622
+ "User-Agent": f"LitLoop/{__version__}",
623
+ },
624
+ timeout=5,
625
+ )
626
+ except requests.Timeout:
627
+ return {**base, "error": "无法检查更新:请求超时"}
628
+ except requests.RequestException:
629
+ return {**base, "error": "无法检查更新:网络连接失败,请检查网络或代理设置"}
630
+
631
+ if resp.status_code != 200:
632
+ return {**base, "error": f"无法检查更新:GitHub 返回 HTTP {resp.status_code}"}
633
+
634
+ try:
635
+ payload = resp.json()
636
+ except ValueError:
637
+ return {**base, "error": "无法检查更新:返回数据格式异常"}
638
+ if not isinstance(payload, dict):
639
+ return {**base, "error": "无法检查更新:返回数据格式异常"}
640
+
641
+ tag = str(payload.get("tag_name") or "").strip()
642
+ latest = tag.lstrip("vV")
643
+ if not latest:
644
+ return {**base, "error": "无法检查更新:未找到版本号"}
645
+
646
+ cur_ver = _parse_version_tuple(__version__)
647
+ lat_ver = _parse_version_tuple(latest)
648
+ if cur_ver is not None and lat_ver is not None:
649
+ available = lat_ver > cur_ver
650
+ else:
651
+ available = latest != __version__
652
+
653
+ result = {
654
+ "current": __version__,
655
+ "latest": latest,
656
+ "available": available,
657
+ "url": str(payload.get("html_url") or ""),
658
+ "notes": str(payload.get("body") or ""),
659
+ "error": "",
660
+ }
661
+ _APP_UPDATE.update(at=time.time(), data=result)
662
+ return result
663
+
664
+ @app.get("/api/data-dir")
665
+ def data_dir() -> dict[str, Any]:
666
+ """Where the papers, sessions and settings live (not inside the install folder)."""
667
+ from ..config import DATA_DIR
668
+ return {"path": str(DATA_DIR), "exists": DATA_DIR.exists()}
669
+
670
+ @app.post("/api/diagnostics")
671
+ def diagnostics_export() -> dict[str, Any]:
672
+ """Write the diagnostic bundle and return where it landed."""
673
+ from ..diagnostics import write_bundle
674
+ try:
675
+ path = write_bundle()
676
+ except OSError as exc:
677
+ raise HTTPException(500, f"无法写诊断包:{exc}") from exc
678
+ return {"path": str(path), "size": path.stat().st_size if path.exists() else 0}
679
+
680
+ # Downloads: jobs started from the app (download_jobs), one at a time.
681
+ @app.get("/api/downloads")
682
+ def downloads() -> dict[str, Any]:
683
+ from . import download_jobs
684
+ from ..download import progress as progress_reporter
685
+ from ..config import load_config
686
+ live = progress_reporter.read_state() if download_jobs.running() else None
687
+ # A download the agent or the command line runs: shown on the page too.
688
+ external = None if download_jobs.running() else progress_reporter.running_elsewhere()
689
+ return {"jobs": download_jobs.list_jobs(), "running": download_jobs.running(), "live": live,
690
+ "external": external,
691
+ "pushing": download_jobs.pushing(), "output_dir": str(load_config().get("output_dir") or "")}
692
+
693
+ @app.get("/api/downloads/notice")
694
+ def downloads_notice() -> dict[str, Any] | None:
695
+ """Polled on every page for the system notifications (the app's own jobs and the agent's)."""
696
+ from ..download import progress as progress_reporter
697
+ return progress_reporter.latest_notice()
698
+
699
+ @app.get("/api/screening/notice")
700
+ def screening_notice() -> dict[str, Any] | None:
701
+ """Polled on every page for the system notifications (a deep-search round finished)."""
702
+ return store.latest_round_notice()
703
+
704
+ @app.get("/api/downloads/pending")
705
+ def downloads_pending() -> list[dict[str, Any]]:
706
+ """Papers picked 选中下载 in screening that no job has downloaded yet (or holds in a queue), by session."""
707
+ from . import download_jobs
708
+ got = {k for d in download_jobs.downloaded_dois() | download_jobs.queued_dois()
709
+ for k in download_jobs.same_paper(d)}
710
+ out = []
711
+ for s in db.pending_selected_by_session(): # newest first: a paper picked twice shows in the newest
712
+ papers = []
713
+ for it in s["papers"]:
714
+ keys = download_jobs.same_paper(it["doi"])
715
+ if keys & got:
716
+ continue
717
+ got |= keys
718
+ papers.append(it)
719
+ if papers:
720
+ out.append({"session": s["session"], "question": s["question"], "papers": papers})
721
+ return out
722
+
723
+ @app.get("/api/downloads/{job}")
724
+ def download_job(job: str) -> dict[str, Any]:
725
+ from . import download_jobs
726
+ if not re.fullmatch(r"[\w-]{1,40}", job):
727
+ raise HTTPException(400, "无法打开下载任务:任务编号不正确")
728
+ try:
729
+ d = download_jobs.get(job)
730
+ except FileNotFoundError as exc:
731
+ raise HTTPException(404, "无法打开下载任务:此任务已不存在") from exc
732
+ d["zotero_default"] = download_jobs.default_collection(d["title"])
733
+ _explain(d, full=True)
734
+ _explain(d.get("zotero"), "推送", full=True)
735
+ for it in d.get("items") or []:
736
+ _explain(it)
737
+ _explain(it.get("zotero"), "推送")
738
+ return d
739
+
740
+ @app.post("/api/downloads/{job}/queue")
741
+ def download_queue(job: str, body: StartQueue) -> dict[str, Any]:
742
+ """Start one publisher's queue of a long list (download_jobs.start_queue)."""
743
+ from . import download_jobs
744
+ if not re.fullmatch(r"[\w-]{1,40}", job):
745
+ raise HTTPException(400, "无法开始:任务编号不正确")
746
+ try:
747
+ return download_jobs.start_queue(job, body.publisher)
748
+ except FileNotFoundError as exc:
749
+ raise HTTPException(404, "无法开始:此下载任务已不存在") from exc
750
+ except download_jobs.Busy as exc:
751
+ raise HTTPException(409, "已有下载任务在进行,完成后再开始新的下载") from exc
752
+ except ValueError as exc:
753
+ raise HTTPException(400, str(exc)) from exc
754
+
755
+ @app.post("/api/downloads/{job}/zotero")
756
+ def download_push(job: str, body: ZoteroPush) -> dict[str, Any]:
757
+ from . import download_jobs
758
+ if not re.fullmatch(r"[\w-]{1,40}", job):
759
+ raise HTTPException(400, "无法推送:任务编号不正确")
760
+ try:
761
+ return download_jobs.push_zotero(job, body.collection, body.tags)
762
+ except FileNotFoundError as exc:
763
+ raise HTTPException(404, "无法推送:此下载任务已不存在") from exc
764
+ except download_jobs.Busy as exc:
765
+ raise HTTPException(409, "已有推送任务在进行,完成后再开始新的推送") from exc
766
+ except ValueError as exc:
767
+ raise HTTPException(400, str(exc)) from exc
768
+
769
+ @app.get("/api/zotero")
770
+ def zotero_overview() -> dict[str, Any]:
771
+ """Is Zotero up, which zotero-mcp is used, what waits to be pushed, what was pushed."""
772
+ import requests
773
+
774
+ from . import download_jobs
775
+ from ..config import load_config
776
+ from ..zotero.client import discover_zotero_mcp
777
+ try:
778
+ running = requests.get("http://127.0.0.1:23119/connector/ping", timeout=2,
779
+ proxies={"http": None, "https": None}).status_code == 200
780
+ except requests.RequestException:
781
+ running = False
782
+ found = discover_zotero_mcp(load_config())
783
+ history = download_jobs.zotero_history()
784
+ for h in history:
785
+ _explain(h, "推送", full=True)
786
+ return {"running": running, "mcp": found["python"] if found else None,
787
+ "pending": download_jobs.zotero_pending(), "history": history,
788
+ "pushing": download_jobs.pushing()}
789
+
790
+ @app.get("/api/zotero/write-mode")
791
+ def zotero_write_mode() -> dict[str, Any]:
792
+ from ..config import load_config
793
+ from ..zotero.client import write_mode
794
+ # Asking costs a zotero-mcp start (2-3 s) and the mode only changes when the
795
+ # user authorizes, so the first good answer is kept until then.
796
+ if "mode" not in _WRITE_MODE:
797
+ r = write_mode(load_config())
798
+ if not r.get("success"):
799
+ return {"mode": None, "local_supported": None}
800
+ _WRITE_MODE.update(mode=r.get("mode"), local_supported=r.get("local_supported"))
801
+ return dict(_WRITE_MODE)
802
+
803
+ @app.post("/api/zotero/authorize")
804
+ def zotero_authorize() -> dict[str, Any]:
805
+ """Zotero asks the user (a dialog in the Zotero app); the answer decides the mode."""
806
+ from ..config import load_config
807
+ from ..zotero.client import authorize_local_writes
808
+ r = authorize_local_writes(load_config())
809
+ _WRITE_MODE.clear()
810
+ from . import error_text
811
+ ok = bool(r.get("success"))
812
+ return {"mode": r.get("mode"), "ok": ok,
813
+ "message": r.get("message") if ok else error_text.message(r.get("error") or r.get("message"), "授权")}
814
+
815
+ @app.post("/api/zotero/check")
816
+ def zotero_check(body: ZoteroCheck) -> dict[str, Any]:
817
+ """The PDF state of every item in a collection, and for problem items a local PDF if one exists."""
818
+ from . import download_jobs
819
+ from ..config import load_config
820
+ from ..zotero.client import check_collection
821
+ if not re.fullmatch(r"[A-Z0-9]{8}", body.key):
822
+ raise HTTPException(400, "无法检查附件:分类编号不正确")
823
+ r = check_collection(body.key, load_config())
824
+ if not r.get("success"):
825
+ raise _fail(503, r.get("error"), "检查附件", "Zotero is not reachable")
826
+ for it in r["items"]:
827
+ pdf = download_jobs.find_local_pdf(it["doi"]) if it["state"] != "ok" and it["doi"] else None
828
+ it["local_pdf"] = str(pdf) if pdf else None
829
+ return {"items": r["items"]}
830
+
831
+ @app.post("/api/zotero/repair")
832
+ def zotero_repair(body: ZoteroRepair) -> dict[str, Any]:
833
+ """Repair one item: trash its empty attachments, attach the local PDF found for its DOI."""
834
+ from . import download_jobs
835
+ from ..config import load_config
836
+ from ..zotero.client import repair_attachment
837
+ if not re.fullmatch(r"[A-Z0-9]{8}", body.item_key) or any(not re.fullmatch(r"[A-Z0-9]{8}", k) for k in body.trash):
838
+ raise HTTPException(400, "无法重新附加:条目编号不正确")
839
+ pdf = download_jobs.find_local_pdf(body.doi or "") if body.attach else None # never a path from the client
840
+ if body.attach and not pdf:
841
+ raise _fail(404, "no local PDF", "重新附加", "")
842
+ r = repair_attachment(body.item_key, pdf, body.trash, load_config())
843
+ if not r.get("success"):
844
+ raise _fail(502, r.get("error"), "重新附加", "repair did not complete")
845
+ return r
846
+
847
+ @app.get("/api/zotero/collections")
848
+ def zotero_collections() -> list[dict[str, Any]]:
849
+ """The library's collections as '/'-paths (read through zotero-mcp)."""
850
+ from ..config import load_config
851
+ from ..zotero.client import zotero_collections as read
852
+ r = read(load_config())
853
+ if not r.get("success"):
854
+ raise _fail(503, r.get("error"), "读取分类", "Zotero is not reachable")
855
+ return r["collections"]
856
+
857
+ @app.post("/api/downloads")
858
+ def download_start(body: NewDownload) -> dict[str, Any]:
859
+ from . import download_jobs
860
+ try:
861
+ return download_jobs.start([p.model_dump() for p in body.papers], body.title, body.session,
862
+ body.zotero_target)
863
+ except download_jobs.Busy as exc:
864
+ raise HTTPException(409, "已有下载任务在进行,完成后再开始新的下载") from exc
865
+ except ValueError as exc:
866
+ raise HTTPException(400, str(exc)) from exc
867
+
868
+ @app.post("/api/searches")
869
+ def search_start(body: NewSearch) -> dict[str, Any]:
870
+ from . import agent_runner
871
+ try:
872
+ return agent_runner.start(body.model_dump())
873
+ except ValueError as exc:
874
+ raise HTTPException(400, str(exc)) from exc
875
+ except RuntimeError as exc:
876
+ raise HTTPException(409, str(exc)) from exc
877
+
878
+ @app.get("/api/searches")
879
+ def search_list() -> list[dict[str, Any]]:
880
+ from . import agent_runner
881
+ return agent_runner.list_runs()
882
+
883
+ @app.get("/api/searches/{run}")
884
+ def search_get(run: str) -> dict[str, Any]:
885
+ from . import agent_runner
886
+ try:
887
+ d = agent_runner.get(run)
888
+ except ValueError as exc:
889
+ raise HTTPException(400, str(exc)) from exc
890
+ if not d:
891
+ raise HTTPException(404, "没有这次检索")
892
+ return d
893
+
894
+ @app.post("/api/searches/{run}/cancel")
895
+ def search_cancel(run: str) -> dict[str, Any]:
896
+ from . import agent_runner
897
+ return {"cancelled": agent_runner.cancel(run)}
898
+
899
+ @app.post("/api/downloads/parse")
900
+ def downloads_parse(body: PastedList) -> dict[str, Any]:
901
+ """DOIs, doi.org links and arXiv ids in pasted text, for a download list.
902
+
903
+ ``downloaded``: those whose PDF is still in the save folder; the job skips
904
+ exactly these (2026-10-20: the job history counted too, so the app had to
905
+ say "downloaded before, skipped if the file is still there")."""
906
+ from . import download_jobs
907
+ from ..identifiers import extract_identifiers
908
+ dois, unrecognized = extract_identifiers(body.text[:200_000])
909
+ got = set(download_jobs.local_files())
910
+ return {"papers": [{"doi": d} for d in dois[:2000]], "unrecognized": unrecognized[:100],
911
+ "unrecognized_count": len(unrecognized), "too_many": len(dois) > 2000,
912
+ "downloaded": [d for d in dois if download_jobs.same_paper(d) & got]}
913
+
914
+ @app.post("/api/downloads/resolve")
915
+ def downloads_resolve(body: ReferenceLines) -> dict[str, Any]:
916
+ """Pasted lines with no DOI (reference-list entries, titles): look each up
917
+ on Crossref. Only when the user asks (a button), at most 100 lines."""
918
+ from . import download_jobs
919
+ from ..config import load_config
920
+ from ..search.title_to_doi import resolve_references
921
+ lines = [ln.strip() for ln in body.lines if ln.strip()][:100]
922
+ rows = resolve_references(lines, load_config())
923
+ got = set(download_jobs.local_files())
924
+ for r in rows:
925
+ if r.get("doi"):
926
+ r["downloaded"] = bool(download_jobs.same_paper(r["doi"]) & got)
927
+ return {"results": rows}
928
+
929
+ @app.post("/api/downloads/skip")
930
+ def download_skip(body: SkipPaper) -> dict[str, Any]:
931
+ """The user will not log in for this paper: give it up, go on with the next."""
932
+ from ..sources import skip_download
933
+ return {"skipped": skip_download(body.doi)}
934
+
935
+ @app.post("/api/downloads/cancel")
936
+ def download_cancel() -> dict[str, Any]:
937
+ from . import download_jobs
938
+ return {"cancelled": download_jobs.cancel()}
939
+
940
+ @app.post("/api/open")
941
+ def open_path(body: OpenPath) -> dict[str, Any]:
942
+ """Open a downloaded PDF or a job's folder with the system's default program.
943
+
944
+ Only paths a download job recorded (its files and output folder) and
945
+ the configured save folder are opened, never an arbitrary path:
946
+ opening runs whatever handles it.
947
+ """
948
+ from pathlib import Path
949
+
950
+ from . import download_jobs
951
+ from ..config import load_config
952
+ target = Path(body.path).resolve()
953
+ allowed = set()
954
+ if load_config().get("output_dir"):
955
+ allowed.add(Path(load_config()["output_dir"]).expanduser().resolve())
956
+ allowed.add((Path(load_config()["output_dir"]).expanduser() / "exports").resolve())
957
+ for j in download_jobs.list_jobs(200):
958
+ d = download_jobs.get(j["job"])
959
+ if d.get("output_dir"):
960
+ allowed.add(Path(d["output_dir"]).resolve())
961
+ allowed |= {Path(i["file"]).resolve() for i in d["items"] if i.get("file")}
962
+ ok_file = target in allowed and target.suffix.lower() == ".pdf" and target.is_file()
963
+ ok_dir = target.is_dir() and (target in allowed or any(target == a.parent for a in allowed if a.suffix))
964
+ if not (ok_file or ok_dir):
965
+ raise HTTPException(403, "无法打开:只能打开 LitLoop 下载的 PDF 和它们的文件夹")
966
+ _open_with_system(target)
967
+ return {"opened": str(target)}
968
+
969
+ return app
970
+
971
+
972
+ def _open_with_system(path) -> None:
973
+ import os
974
+ import subprocess
975
+ if os.name == "nt":
976
+ os.startfile(str(path)) # noqa: S606 - a vetted PDF or folder
977
+ else:
978
+ subprocess.Popen(["open" if sys.platform == "darwin" else "xdg-open", str(path)])
979
+
980
+
981
+ def _free_port() -> int:
982
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
983
+ s.bind(("127.0.0.1", 0))
984
+ return s.getsockname()[1]
985
+
986
+
987
+ def main(argv: list[str] | None = None) -> None:
988
+ ap = argparse.ArgumentParser(description="LitLoop desktop app backend (127.0.0.1 only)")
989
+ ap.add_argument("--token", required=True, help="random token every request must carry")
990
+ ap.add_argument("--port", type=int, default=0, help="0 = pick a free port")
991
+ args = ap.parse_args(argv)
992
+ import uvicorn
993
+ port = args.port or _free_port()
994
+ print(json.dumps({"litloop_app_server": 1, "port": port}), flush=True)
995
+ uvicorn.run(create_app(args.token), host="127.0.0.1", port=port, log_level="warning")
996
+
997
+
998
+ if __name__ == "__main__":
999
+ main(sys.argv[1:])