dsh-zju-lecture-note 2.2.7 → 2.2.9
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.
- package/ATTRIBUTION.md +1 -1
- package/CHANGELOG.md +108 -2
- package/README.md +57 -25
- package/SECURITY.md +96 -51
- package/docs/technical.md +98 -40
- package/lib/bridge.js +12 -2
- package/lib/client.js +142 -36
- package/lib/index.js +130 -22
- package/lib/tools.js +14 -11
- package/lib/tools.schema.json +14 -11
- package/mcp.json +21 -0
- package/mcp_server.py +258 -43
- package/package.json +5 -2
- package/plugin.json +19 -0
- package/server.mjs +118 -0
- package/skills/scholia-notes.manifest.json +1 -1
- package/skills/zju-lecture-summarizer/SKILL.md +46 -9
- package/skills/zju-lecture-summarizer/SLIDES.md +58 -14
- package/skills/zju-lecture-summarizer-help/SKILL.md +2 -2
- package/skills/zju-lecture-summarizer-help.manifest.json +4 -4
- package/skills/zju-lecture-summarizer-login.manifest.json +1 -1
- package/skills/zju-lecture-summarizer.manifest.json +6 -6
- package/zju_lecture/__init__.py +6 -0
- package/zju_lecture/browser_login.py +176 -222
- package/zju_lecture/cli.py +24 -2
- package/zju_lecture/client.py +325 -116
- package/zju_lecture/cookies.py +43 -4
- package/zju_lecture/credential_store.py +621 -0
- package/zju_lecture/deps.py +16 -0
- package/zju_lecture/formatter.py +201 -3
- package/zju_lecture/handout.py +29 -3
- package/zju_lecture/slides.py +9 -7
- package/zju_lecture/study_notes.py +4 -0
package/zju_lecture/formatter.py
CHANGED
|
@@ -5,6 +5,9 @@ Formats timestamps, groups text by minutes, slices by time window,
|
|
|
5
5
|
and prepares structured text for agent consumption.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
+
import datetime
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
8
11
|
from typing import Any, Dict, List, Optional, Tuple
|
|
9
12
|
|
|
10
13
|
|
|
@@ -277,18 +280,213 @@ def format_quality_line(quality: Dict[str, Any]) -> str:
|
|
|
277
280
|
return line
|
|
278
281
|
|
|
279
282
|
|
|
283
|
+
def _slide_seconds(slide: Dict[str, Any]) -> Optional[int]:
|
|
284
|
+
"""Return the transcript-relative second for one slide, if available.
|
|
285
|
+
|
|
286
|
+
Use explicit ``is not None`` checks: page 1 is legitimately ``time=0`` and
|
|
287
|
+
must not be skipped by a truthiness test.
|
|
288
|
+
"""
|
|
289
|
+
for key in ("time", "time_point", "sec"):
|
|
290
|
+
if key not in slide:
|
|
291
|
+
continue
|
|
292
|
+
value = slide.get(key)
|
|
293
|
+
if value is None:
|
|
294
|
+
continue
|
|
295
|
+
try:
|
|
296
|
+
return max(0, int(round(float(value))))
|
|
297
|
+
except (TypeError, ValueError):
|
|
298
|
+
continue
|
|
299
|
+
return None
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _minute_label(seconds: int) -> str:
|
|
303
|
+
"""``HH:MM`` label aligned with the transcript's ``[HH:MM]`` lines."""
|
|
304
|
+
seconds = max(0, int(seconds))
|
|
305
|
+
return f"{seconds // 3600:02d}:{(seconds % 3600) // 60:02d}"
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _suggest_pages(pages: List[int], budget: int = 24) -> List[int]:
|
|
309
|
+
"""Evenly spaced page fallback when the model has no key-point map."""
|
|
310
|
+
unique = sorted({int(page) for page in pages if page})
|
|
311
|
+
if not unique:
|
|
312
|
+
return []
|
|
313
|
+
if len(unique) <= budget:
|
|
314
|
+
return unique
|
|
315
|
+
if budget <= 1:
|
|
316
|
+
return [unique[0]]
|
|
317
|
+
step = (len(unique) - 1) / float(budget - 1)
|
|
318
|
+
picked = {unique[0], unique[-1]}
|
|
319
|
+
for index in range(1, budget - 1):
|
|
320
|
+
picked.add(unique[int(round(index * step))])
|
|
321
|
+
return sorted(picked)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _capture_date(slides_list: List[Dict[str, Any]]) -> str:
|
|
325
|
+
values: List[int] = []
|
|
326
|
+
for slide in slides_list:
|
|
327
|
+
value = slide.get("capture_ms")
|
|
328
|
+
if value is None:
|
|
329
|
+
continue
|
|
330
|
+
try:
|
|
331
|
+
values.append(int(value))
|
|
332
|
+
except (TypeError, ValueError):
|
|
333
|
+
continue
|
|
334
|
+
if not values:
|
|
335
|
+
return ""
|
|
336
|
+
try:
|
|
337
|
+
moment = datetime.datetime.fromtimestamp(
|
|
338
|
+
min(values) / 1000.0,
|
|
339
|
+
tz=datetime.timezone(datetime.timedelta(hours=8)),
|
|
340
|
+
)
|
|
341
|
+
except (OverflowError, OSError, ValueError):
|
|
342
|
+
return ""
|
|
343
|
+
weekdays = ("周一", "周二", "周三", "周四", "周五", "周六", "周日")
|
|
344
|
+
return f"{moment:%Y-%m-%d}({weekdays[moment.weekday()]})"
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _format_compact_slides(
|
|
348
|
+
slides_list: List[Dict[str, Any]],
|
|
349
|
+
pdf_path: str = "",
|
|
350
|
+
download_attempted: bool = False,
|
|
351
|
+
) -> str:
|
|
352
|
+
"""A page-time index instead of 150 × two absolute-path lines.
|
|
353
|
+
|
|
354
|
+
The model only needs the directory, the file-name pattern, and the
|
|
355
|
+
page→time axis to pick the 15–25 pages worth reading. Repeating every
|
|
356
|
+
local path for a 150-page deck is pure context cost.
|
|
357
|
+
"""
|
|
358
|
+
total = len(slides_list)
|
|
359
|
+
pages: List[int] = []
|
|
360
|
+
timed: List[Tuple[int, int]] = []
|
|
361
|
+
for idx, slide in enumerate(slides_list, start=1):
|
|
362
|
+
page = int(slide.get("page") or idx)
|
|
363
|
+
pages.append(page)
|
|
364
|
+
seconds = _slide_seconds(slide)
|
|
365
|
+
if seconds is not None:
|
|
366
|
+
timed.append((page, seconds))
|
|
367
|
+
|
|
368
|
+
span = ""
|
|
369
|
+
if timed:
|
|
370
|
+
span = f",时间轴 {_minute_label(timed[0][1])}–{_minute_label(timed[-1][1])}"
|
|
371
|
+
lines = [f"### 课堂投影 PPT 截图(共 {total} 页{span})"]
|
|
372
|
+
|
|
373
|
+
captured = _capture_date(slides_list)
|
|
374
|
+
if captured:
|
|
375
|
+
lines.append(f"拍摄日期: {captured}")
|
|
376
|
+
|
|
377
|
+
local = [
|
|
378
|
+
slide for slide in slides_list
|
|
379
|
+
if str(slide.get("local_path") or "").strip()
|
|
380
|
+
]
|
|
381
|
+
if local:
|
|
382
|
+
first = local[0]
|
|
383
|
+
last = local[-1]
|
|
384
|
+
relative = str(first.get("workspace_rel") or "").strip()
|
|
385
|
+
if relative and "/" in relative:
|
|
386
|
+
directory = relative.rsplit("/", 1)[0]
|
|
387
|
+
elif relative:
|
|
388
|
+
directory = "."
|
|
389
|
+
else:
|
|
390
|
+
directory = os.path.dirname(str(first.get("local_path") or ""))
|
|
391
|
+
lines.append(f"截图目录: {directory}")
|
|
392
|
+
first_name = os.path.basename(str(first.get("local_path") or ""))
|
|
393
|
+
last_name = os.path.basename(str(last.get("local_path") or ""))
|
|
394
|
+
stem, extension = os.path.splitext(first_name)
|
|
395
|
+
pattern = re.sub(r"\d+$", "XXXX", stem) + extension
|
|
396
|
+
lines.append(f"文件命名: {first_name} … {last_name}(模式 {pattern})")
|
|
397
|
+
else:
|
|
398
|
+
urls = [
|
|
399
|
+
str(slide.get("image_url") or "").strip()
|
|
400
|
+
for slide in slides_list
|
|
401
|
+
if str(slide.get("image_url") or "").strip()
|
|
402
|
+
]
|
|
403
|
+
if urls:
|
|
404
|
+
lines.append(f"截图地址示例: {urls[0]}")
|
|
405
|
+
if download_attempted:
|
|
406
|
+
lines.append(
|
|
407
|
+
"说明: 本次 download=true,但没有可用的本地截图;"
|
|
408
|
+
"请检查上方下载失败诊断,或重试一次下载。"
|
|
409
|
+
)
|
|
410
|
+
else:
|
|
411
|
+
lines.append(
|
|
412
|
+
"说明: 本次 download=false,未下载截图;"
|
|
413
|
+
"需要本地文件/PDF 时再按工具提示执行下载。"
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
if pdf_path:
|
|
417
|
+
lines.append(f"完整 PDF: {pdf_path}")
|
|
418
|
+
|
|
419
|
+
if timed:
|
|
420
|
+
shown = timed[:240]
|
|
421
|
+
axis = " ".join(
|
|
422
|
+
f"{page}={_minute_label(seconds)}" for page, seconds in shown
|
|
423
|
+
)
|
|
424
|
+
if len(timed) > len(shown):
|
|
425
|
+
axis += f" ...(仅列前 {len(shown)} 页,共 {len(timed)} 页)"
|
|
426
|
+
lines.append("页时间轴(相对第 1 页): " + axis)
|
|
427
|
+
else:
|
|
428
|
+
lines.append("页时间轴: 接口未提供可用时间戳,按页码顺序抽样。")
|
|
429
|
+
|
|
430
|
+
suggested = _suggest_pages(pages, budget=24)
|
|
431
|
+
if suggested:
|
|
432
|
+
lines.append(
|
|
433
|
+
"建议抽查(无明确关键页时): "
|
|
434
|
+
+ ", ".join(str(page) for page in suggested)
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
texts: List[Tuple[int, str]] = []
|
|
438
|
+
for idx, slide in enumerate(slides_list, start=1):
|
|
439
|
+
page = int(slide.get("page") or idx)
|
|
440
|
+
value = str(slide.get("text") or slide.get("ocr_content") or "").strip()
|
|
441
|
+
if value:
|
|
442
|
+
texts.append((page, " ".join(value.split())))
|
|
443
|
+
if texts:
|
|
444
|
+
rendered = [f"{page}={value[:80]}" for page, value in texts[:20]]
|
|
445
|
+
lines.append("页面文字(接口返回时): " + " | ".join(rendered))
|
|
446
|
+
if len(texts) > 20:
|
|
447
|
+
lines.append(f"(另有 {len(texts) - 20} 页有文字,未展开)")
|
|
448
|
+
|
|
449
|
+
example = next(
|
|
450
|
+
(
|
|
451
|
+
slide for slide in slides_list
|
|
452
|
+
if str(slide.get("workspace_rel") or "").strip()
|
|
453
|
+
),
|
|
454
|
+
None,
|
|
455
|
+
)
|
|
456
|
+
if example:
|
|
457
|
+
page = int(example.get("page") or 1)
|
|
458
|
+
relative = str(example.get("workspace_rel") or "").strip()
|
|
459
|
+
lines.append(
|
|
460
|
+
f"引用示例: "
|
|
461
|
+
"(讲义在子目录时按层级补 ../)"
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
return "\n".join(lines).rstrip() + "\n"
|
|
465
|
+
|
|
466
|
+
|
|
280
467
|
def format_slides_as_markdown(
|
|
281
468
|
slides_list: Optional[List[Dict[str, Any]]],
|
|
282
469
|
pdf_path: str = "",
|
|
470
|
+
*,
|
|
471
|
+
compact: bool = False,
|
|
472
|
+
download_attempted: bool = False,
|
|
283
473
|
) -> str:
|
|
284
|
-
"""Format normalized slide records into a
|
|
474
|
+
"""Format normalized slide records into a truthful outline.
|
|
285
475
|
|
|
286
476
|
``search-ppt`` returns screenshots, not OCR text. A slide therefore shows
|
|
287
477
|
whatever the API actually provided: text (rare), a remote image URL, and
|
|
288
478
|
the local file when ``zju_fetch_slides(download=true)`` saved it.
|
|
479
|
+
|
|
480
|
+
``compact=True`` is the model-facing index: one directory, the file-name
|
|
481
|
+
pattern, a page→time axis, and a small suggested sample. The legacy
|
|
482
|
+
per-page form is kept for the CLI and for callers that need every path.
|
|
289
483
|
"""
|
|
290
484
|
if not slides_list:
|
|
291
485
|
return ""
|
|
486
|
+
if compact:
|
|
487
|
+
return _format_compact_slides(
|
|
488
|
+
slides_list, pdf_path=pdf_path, download_attempted=download_attempted
|
|
489
|
+
)
|
|
292
490
|
|
|
293
491
|
lines = ["### 课堂投影 PPT 截图"]
|
|
294
492
|
if pdf_path:
|
|
@@ -296,8 +494,8 @@ def format_slides_as_markdown(
|
|
|
296
494
|
lines.append("")
|
|
297
495
|
for idx, slide in enumerate(slides_list, start=1):
|
|
298
496
|
page = slide.get("page") or idx
|
|
299
|
-
time_point =
|
|
300
|
-
ts_str = f" [{format_timestamp(
|
|
497
|
+
time_point = _slide_seconds(slide)
|
|
498
|
+
ts_str = f" [{format_timestamp(time_point)}]" if time_point is not None else ""
|
|
301
499
|
title = slide.get("title") or f"第 {page} 页"
|
|
302
500
|
lines.append(f"#### {title}{ts_str}")
|
|
303
501
|
|
package/zju_lecture/handout.py
CHANGED
|
@@ -32,6 +32,7 @@ import os
|
|
|
32
32
|
import re
|
|
33
33
|
import shutil
|
|
34
34
|
import subprocess
|
|
35
|
+
import tempfile
|
|
35
36
|
from dataclasses import dataclass, field
|
|
36
37
|
from typing import Callable, Dict, List, Optional, Sequence, Tuple
|
|
37
38
|
|
|
@@ -1554,6 +1555,33 @@ def _pdf_page_count(path: str) -> int:
|
|
|
1554
1555
|
return count
|
|
1555
1556
|
|
|
1556
1557
|
|
|
1558
|
+
def _atomic_copy(source: str, target: str) -> None:
|
|
1559
|
+
"""Copy ``source`` onto ``target`` through an unpredictable temp file.
|
|
1560
|
+
|
|
1561
|
+
``target + ".tmp"`` was a fixed name and ``shutil.copyfile`` follows a
|
|
1562
|
+
symlink planted at that path, so a pre-created link could redirect the PDF
|
|
1563
|
+
write. ``mkstemp`` creates the temp file with ``O_EXCL`` and ``os.replace``
|
|
1564
|
+
swaps it in atomically — the same rule SECURITY.md §7.6 states for the
|
|
1565
|
+
session file, backups and screenshots.
|
|
1566
|
+
"""
|
|
1567
|
+
directory = os.path.dirname(os.path.abspath(target)) or "."
|
|
1568
|
+
temporary = ""
|
|
1569
|
+
try:
|
|
1570
|
+
descriptor, temporary = tempfile.mkstemp(
|
|
1571
|
+
dir=directory, prefix=".pdf-", suffix=".tmp"
|
|
1572
|
+
)
|
|
1573
|
+
with os.fdopen(descriptor, "wb") as out, open(source, "rb") as src:
|
|
1574
|
+
shutil.copyfileobj(src, out, length=1 << 20)
|
|
1575
|
+
os.replace(temporary, target)
|
|
1576
|
+
temporary = ""
|
|
1577
|
+
finally:
|
|
1578
|
+
if temporary:
|
|
1579
|
+
try:
|
|
1580
|
+
os.unlink(temporary)
|
|
1581
|
+
except OSError:
|
|
1582
|
+
pass
|
|
1583
|
+
|
|
1584
|
+
|
|
1557
1585
|
def compile_handout(
|
|
1558
1586
|
typst_source: str,
|
|
1559
1587
|
output_pdf: str,
|
|
@@ -1626,9 +1654,7 @@ def compile_handout(
|
|
|
1626
1654
|
|
|
1627
1655
|
try:
|
|
1628
1656
|
os.makedirs(os.path.dirname(output_pdf), exist_ok=True)
|
|
1629
|
-
|
|
1630
|
-
shutil.copyfile(built_pdf, temporary)
|
|
1631
|
-
os.replace(temporary, output_pdf)
|
|
1657
|
+
_atomic_copy(built_pdf, output_pdf)
|
|
1632
1658
|
except OSError as exc:
|
|
1633
1659
|
return HandoutCompileResult(
|
|
1634
1660
|
ok=False,
|
package/zju_lecture/slides.py
CHANGED
|
@@ -9,8 +9,9 @@ inflated (FlateDecode), and no image is re-encoded.
|
|
|
9
9
|
|
|
10
10
|
Security boundaries:
|
|
11
11
|
|
|
12
|
-
*
|
|
13
|
-
|
|
12
|
+
* image URLs must be HTTPS on ``*.zju.edu.cn`` or the official image CDN
|
|
13
|
+
allowlist (``chinamcloud`` / ``chinamcache``); everything else is refused;
|
|
14
|
+
* plain ``http`` is upgraded to HTTPS for those hosts;
|
|
14
15
|
* the login Cookie is only sent to ZJU hosts (never to arbitrary URLs the
|
|
15
16
|
API may return), and ``Authorization`` is never sent to image hosts;
|
|
16
17
|
* image bytes are validated by magic number before anything is written.
|
|
@@ -30,8 +31,9 @@ from .client import (
|
|
|
30
31
|
USER_AGENT,
|
|
31
32
|
_fetch_bytes,
|
|
32
33
|
extract_classroom_token,
|
|
34
|
+
is_allowed_image_host,
|
|
33
35
|
is_zju_host,
|
|
34
|
-
|
|
36
|
+
secure_image_url,
|
|
35
37
|
)
|
|
36
38
|
from .paths import primary_workspace
|
|
37
39
|
from .security import sanitize_cookie_header
|
|
@@ -52,14 +54,14 @@ def _record(diagnostics: Optional[List[str]], message: str) -> None:
|
|
|
52
54
|
|
|
53
55
|
|
|
54
56
|
def _safe_image_url(url: str) -> Optional[str]:
|
|
55
|
-
"""Return the HTTPS ZJU URL to fetch, or ``None``
|
|
56
|
-
value =
|
|
57
|
+
"""Return the HTTPS ZJU/official-CDN URL to fetch, or ``None`` if unsafe."""
|
|
58
|
+
value = secure_image_url(url)
|
|
57
59
|
parsed = urllib.parse.urlparse(value)
|
|
58
60
|
if parsed.scheme != "https":
|
|
59
61
|
return None
|
|
60
62
|
if parsed.username or parsed.password:
|
|
61
63
|
return None
|
|
62
|
-
if not
|
|
64
|
+
if not is_allowed_image_host(parsed.hostname):
|
|
63
65
|
return None
|
|
64
66
|
return value
|
|
65
67
|
|
|
@@ -87,7 +89,7 @@ def _download_one(
|
|
|
87
89
|
return index, None, "该页没有截图地址"
|
|
88
90
|
safe_url = _safe_image_url(url)
|
|
89
91
|
if safe_url is None:
|
|
90
|
-
return index, None, "截图地址不是 HTTPS
|
|
92
|
+
return index, None, "截图地址不是 HTTPS 浙大主机或官方图片 CDN,已拒绝下载"
|
|
91
93
|
|
|
92
94
|
host = (urllib.parse.urlparse(safe_url).hostname or "").lower()
|
|
93
95
|
headers = {
|
|
@@ -2533,6 +2533,8 @@ def _prelude_text() -> str:
|
|
|
2533
2533
|
|
|
2534
2534
|
|
|
2535
2535
|
def _run_typst(typst: str, args: Sequence[str], cwd: str, timeout: int) -> Tuple[int, str]:
|
|
2536
|
+
from .deps import typst_env
|
|
2537
|
+
|
|
2536
2538
|
try:
|
|
2537
2539
|
completed = subprocess.run(
|
|
2538
2540
|
[typst, *args],
|
|
@@ -2541,6 +2543,8 @@ def _run_typst(typst: str, args: Sequence[str], cwd: str, timeout: int) -> Tuple
|
|
|
2541
2543
|
stderr=subprocess.STDOUT,
|
|
2542
2544
|
timeout=timeout,
|
|
2543
2545
|
check=False,
|
|
2546
|
+
# Never hand the classroom cookie to the compiler subprocess.
|
|
2547
|
+
env=typst_env(),
|
|
2544
2548
|
)
|
|
2545
2549
|
except subprocess.TimeoutExpired:
|
|
2546
2550
|
return 124, f"typst 编译超时({timeout} 秒)。"
|