feat(platforms): 增加能力推导与 /api/platforms capabilities 字段

新增 platforms/contract.py:生命周期 ↔ 槽位 ↔ 能力名映射,capabilities_of 按非空槽位推导;Platform.capabilities() 供 /api/platforms 返回 capabilities。行为不变,现有流程与前端未改。同步 architecture.md(生命周期与能力、广东公需课)、todo 第二/三步、OPTIMIZATION 平台列表。
This commit is contained in:
study-tool
2026-10-31 13:54:28 +08:00
parent 0e2923e675
commit d702830470
6 changed files with 68 additions and 5 deletions
+6
View File
@@ -19,6 +19,8 @@ Platform.study 的签名约定:
from dataclasses import dataclass
from typing import Callable, Optional
from study_tool.platforms.contract import capabilities_of
@dataclass
class Platform:
@@ -39,3 +41,7 @@ class Platform:
"""登录入口或学习入口命中任意片段,就认为页面属于本平台。"""
text = "%s\n%s" % (url or "", html or "")
return any(e and e in text for e in self.entries)
def capabilities(self):
"""已实现的能力名(见 contract.CAPABILITIES),/api/platforms 用它。"""
return capabilities_of(self)
+29
View File
@@ -0,0 +1,29 @@
"""平台插件契约:生命周期、能力与槽位对应关系。
插件只实现自己平台**确实有**的阶段,缺的阶段不要实现,也不要抛 NotImplementedError;
引擎按能力判断是否走该分支(例如没有 `login` 的平台不显示密码登录)。
生命周期 槽位 能力名
登录 / 会话 login / session / renew_session / profile login / session / renew / profile
查询课程 list_courses courses
课程详情 discover discover
学习 study study
测验 (待 quiz 下沉为平台能力后加入) quiz
流程(按什么顺序调用)只有一个实现,在引擎里;平台只负责「这一步在这个站点怎么做」。
"""
CAPABILITIES = {
"login": ("login",),
"session": ("session",),
"renew": ("renew_session",),
"profile": ("profile",),
"courses": ("list_courses",),
"discover": ("discover",),
"study": ("study",),
}
def capabilities_of(platform) -> list:
"""按已实现的槽位推导能力名,顺序固定(即 CAPABILITIES 的顺序)。"""
return [name for name, slots in CAPABILITIES.items()
if all(getattr(platform, slot, None) for slot in slots)]
+4 -2
View File
@@ -20,8 +20,10 @@ from study_tool.settings_store import load_settings
@app.get("/api/platforms")
def api_platforms():
"""已注册平台;前端用 label 展示,例如「莞易学」。"""
return {"ok": True, "platforms": [{"id": p.id, "label": p.label} for p in registry.all()]}
"""已注册平台;label 给界面展示,capabilities 说明该平台实现了哪些阶段。"""
return {"ok": True, "platforms": [{"id": p.id, "label": p.label,
"capabilities": p.capabilities()}
for p in registry.all()]}
# ------------------------------------------------------------------ 运行日志 / 快照