Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 接入界面标注工具 Agentation,本机构建、开关加载

longmeidao47b7a91ec9c3
diff --git a/docs/FRONTEND.md b/docs/FRONTEND.md
index 247bfa8e3..8c4949d75 100644
--- a/docs/FRONTEND.md
+++ b/docs/FRONTEND.md
@@ -171,6 +171,30 @@ React 子树读到 BoardUI 的 token 原值、持久警示是状态色块、一
BoardUI 组件上的间距或外观,处理办法是在组件外面套一层普通元素,不给规则加例外。
`src/react/boardui/` 只加不改,`UPSTREAM.sha256` 记着复制时每个文件的哈希,由 `tests/test_frontend_build.py` 比对。
+## 界面标注
+
+指认界面上的某一块时,用 [Agentation](https://agentation.com) 在页面上点中元素、写下意见,
+复制出的 markdown 带 CSS 选择器、class、计算样式和附近文字,直接贴给智能体,不必再用话描述
+「侧栏那块光斑」。
+
+```powershell
+npm --prefix frontend run build:agentation # 在要用它的那份检出里构建一次
+```
+
+然后在页面地址后加 `?agentation=on`(关闭用 `?agentation=off`),开关存在这台设备的
+`localStorage` 里,之后每次开页都会带上右下角的工具栏。
+
+- 产物写到 `build/agentation/peach-agentation.js`,由 `/dev/agentation.js` 提供,口令同 `/dist/`。
+ 它不进 Git、不进独立包:Agentation 是 PolyForm Shield 许可,只许自用,不随 Peach 分发,
+ 所以不放在会被打包整个带走的 `web/` 下。没构建过的检出里这条路由是 404,`app.js` 静默跳过。
+- 产物是本机构建的:换检出、升级依赖之后重跑一次,刷新页面即生效。
+- 标注里拿不到源文件路径:它靠 React 的 `_debugSource`,React 19 已移除这个字段;组件名也被
+ `peach-react.js` 的压缩改掉了。要在 Tailwind 类名之外给智能体更稳的抓手,给区块加
+ `data-component`(Agentation 默认采集的属性之一)。
+- Agentation 自己的全局快捷键已关闭,避免与 Peach 的 Esc、方向键冲突,只用工具栏按钮操作。
+- 没有接 MCP:标注靠复制粘贴交接。要让智能体直接读标注,另行注册 `agentation-mcp`
+ 并给组件传 `endpoint`。
+
## 挂载契约
遗留路由怎样把一个容器交给 React 页、又怎样收回来,下面每条都是为了不出现两段等待态或离场后还在轮询的根。
@@ -364,6 +388,7 @@ vendor 到 `web/vendor/` 的四个包(video.js、swiper、lucide-static、heal
| `@remixicon/react` | BoardUI 组件内置的图标 |
| `tailwindcss`、`@tailwindcss/vite` | 按 `src/react/` 里实际用到的类名生成 `peach-react.css` |
| `@types/react`、`@types/react-dom` | React 子树的类型检查 |
+| `agentation` | 本机开发用的界面标注工具栏,单独构建、不进产物与独立包(见「界面标注」) |
React 子树单独构建(`vite.react.config.ts`)。`peach-react.js` 只在页面挂 React 子树时由
island 动态加载;`peach-react.css` 由 `index.html` 在旧样式表之前引入;`peach-ui.js` 只剩挂载
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 15cc878fa..e74fb2924 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -26,6 +26,7 @@
"@types/d3-sankey": "0.12.5",
"@types/react": "19.3.0",
"@types/react-dom": "19.3.0",
+ "agentation": "3.1.2",
"happy-dom": "20.14.5",
"oxlint": "1.85.0",
"playwright-core": "1.63.0",
@@ -2445,6 +2446,25 @@
"url": "https://opencollective.com/vitest"
}
},
+ "node_modules/agentation": {
+ "version": "3.1.2",
+ "resolved": "https://registry.npmjs.org/agentation/-/agentation-3.1.2.tgz",
+ "integrity": "sha512-ZZr6PRgX3ussGbvC9NVh5vFfKKFkQyHL6KuxOfBAAM/mJS3bM2XyhZ7wLPakDwU3pqkzq7u6vEfLikFsCOu5+Q==",
+ "dev": true,
+ "license": "PolyForm-Shield-1.0.0",
+ "peerDependencies": {
+ "react": ">=18.0.0",
+ "react-dom": ">=18.0.0"
+ },
+ "peerDependenciesMeta": {
+ "react": {
+ "optional": true
+ },
+ "react-dom": {
+ "optional": true
+ }
+ }
+ },
"node_modules/aria-hidden": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/aria-hidden/-/aria-hidden-1.2.6.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index ad00cab27..9230ac0d9 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -9,6 +9,7 @@
"dev": "vite build --watch",
"dev:react": "vite build -c vite.react.config.ts --watch",
"build": "vite build && vite build -c vite.react.config.ts",
+ "build:agentation": "vite build -c vite.agentation.config.ts",
"typecheck": "tsc --noEmit && tsc --noEmit -p src/react",
"lint": "oxlint -c .oxlintrc.json src/react",
"test": "vitest run",
@@ -36,6 +37,7 @@
"@types/d3-sankey": "0.12.5",
"@types/react": "19.3.0",
"@types/react-dom": "19.3.0",
+ "agentation": "3.1.2",
"happy-dom": "20.14.5",
"oxlint": "1.85.0",
"playwright-core": "1.63.0",
diff --git a/frontend/src/dev/agentation.tsx b/frontend/src/dev/agentation.tsx
new file mode 100644
index 000000000..dcd1dbcba
--- /dev/null
+++ b/frontend/src/dev/agentation.tsx
@@ -0,0 +1,13 @@
+/* 界面标注工具 Agentation 的挂载入口,只由 `vite.agentation.config.ts` 构建。
+ *
+ * 产物落在 `build/agentation/`,不进 Git 也不进独立包;`app.js` 在本机开关打开时才
+ * `import('/dev/agentation.js')`。它自带一份 React,挂在 `<body>` 末尾自己的根上,
+ * 和页面里的 React 子树互不相干。用法见 docs/FRONTEND.md「界面标注」。 */
+import { Agentation } from 'agentation';
+import { createRoot } from 'react-dom/client';
+
+const host = document.createElement('div');
+host.dataset.agentation = '';
+document.body.append(host);
+// Peach 自己占着全局快捷键(Esc 收起面板、方向键翻图),标注工具只走工具栏按钮。
+createRoot(host).render(<Agentation appName="Peach" enableKeyboardShortcuts={false} />);
diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json
index 9f8691416..cebe68a52 100644
--- a/frontend/tsconfig.json
+++ b/frontend/tsconfig.json
@@ -26,7 +26,7 @@
"@peach/react": ["./src/react/bundle.d.ts"]
}
},
- "include": ["src", "test", "vite.config.ts", "vite.react.config.ts", "vitest.config.ts"],
+ "include": ["src", "test", "vite.config.ts", "vite.react.config.ts", "vite.agentation.config.ts", "vitest.config.ts"],
// React 子树另有一份放宽过的配置,入口是 src/react/tsconfig.json(`npm run typecheck` 两份都跑)。
"exclude": ["src/react", "test/react"]
}
diff --git a/frontend/vite.agentation.config.ts b/frontend/vite.agentation.config.ts
new file mode 100644
index 000000000..0aab9b9e8
--- /dev/null
+++ b/frontend/vite.agentation.config.ts
@@ -0,0 +1,24 @@
+/* 界面标注工具的构建配置:`build/agentation/peach-agentation.js`。
+ *
+ * 只由 `npm run build:agentation` 触发,不在 `npm run build` 里:产物不进 Git、不进独立包,
+ * 由服务端的 `/dev/agentation.js` 从本机 `build/` 下读取(docs/FRONTEND.md「界面标注」)。
+ * React 打进产物里,不借页面那份:`peach-react.js` 不导出 React,也不该为一件开发工具改契约。 */
+import { defineConfig } from 'vite';
+
+export default defineConfig({
+ // 库模式不替换 `process.env.NODE_ENV`,同 `vite.react.config.ts`。
+ define: { 'process.env.NODE_ENV': JSON.stringify('production') },
+ build: {
+ outDir: '../build/agentation',
+ emptyOutDir: true,
+ target: 'es2022',
+ minify: 'oxc',
+ sourcemap: false,
+ lib: {
+ entry: 'src/dev/agentation.tsx',
+ formats: ['es'],
+ fileName: () => 'peach-agentation.js',
+ },
+ rollupOptions: { output: { codeSplitting: false } },
+ },
+});
diff --git a/src/peach/config.py b/src/peach/config.py
index 700a2cfef..b87911f53 100644
--- a/src/peach/config.py
+++ b/src/peach/config.py
@@ -82,6 +82,9 @@ class PeachSettings:
db_path: Path = DATABASE_PATH
page_path: Path = PROJECT_ROOT / "web" / "index.html"
vendor_path: Path = PROJECT_ROOT / "web" / "vendor"
+ #: 界面标注工具的本机构建产物,由 `/dev/agentation.js` 提供。落在 `build/` 而不是 `web/`:
+ #: 打包带走整个 `web/`,这份第三方工具不能跟着进独立包(docs/FRONTEND.md「界面标注」)。
+ agentation_path: Path = PROJECT_ROOT / "build" / "agentation" / "peach-agentation.js"
token: str = ""
access_path: Path | None = None
docs_enabled: bool = False
diff --git a/src/peach/routes_pages.py b/src/peach/routes_pages.py
index 5d8cae13b..efd15b718 100644
--- a/src/peach/routes_pages.py
+++ b/src/peach/routes_pages.py
@@ -4,7 +4,7 @@
和直接粘地址都能进来。所以 `client_route` 的那一长串装饰器不是重复,是「前端有哪些
路由」的声明,新增页面必须在这里补一行,否则刷新就是 404。
-`index` 的 401 走跳登录页,`/app.css`、`/app.js`、`/js/`、`/dist/` 走 PlainText 提示:
+`index` 的 401 走跳登录页,`/app.css`、`/app.js`、`/js/`、`/dist/`、`/dev/` 走 PlainText 提示:
资产被浏览器直接请求,重定向到登录页只会让它把 HTML 当脚本解析。
缓存也分两档:`index.html` 是 `no-store`,它是所有资产 URL 的来源;四类资产走
@@ -725,6 +725,19 @@ def app_bundle(request: Request, name: str,
return asset_response(request, path, media)
[email protected]_route("/dev/agentation.js", methods=["GET", "HEAD"])
+def agentation_bundle(request: Request, args: dict[str, str] = Depends(require_asset_auth)):
+ """界面标注工具 Agentation 的本机构建产物,口令与缓存口径同 `/dist/`。
+
+ 产物不进 Git、不进独立包,只在跑过 `npm --prefix frontend run build:agentation`
+ 的检出里存在;其余部署一律 404。`app.js` 只在本机开关打开时才请求它。
+ """
+ path = request.app.state.settings.agentation_path
+ if not path.is_file():
+ return PlainTextResponse("missing", status_code=404)
+ return asset_response(request, path, "text/javascript")
+
+
@router.api_route("/robots.txt", methods=["GET", "HEAD"])
def robots_txt():
"""不要求登录:爬虫拿不到会话,被 401 挡住就等于没读到这份声明。
diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.py
index 07fda9d39..dc80e7876 100644
--- a/tests/test_fastapi_api.py
+++ b/tests/test_fastapi_api.py
@@ -284,6 +284,7 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
con.close()
self.settings = PeachSettings(
db_path=self.db, configured=True, token="secret", page_path=self.page, vendor_path=self.vendor_root,
+ agentation_path=self.root / "build" / "peach-agentation.js",
allowed_media_roots=(self.media_root,), snapshot_root=self.snapshot_root,
poster_root=self.poster_root, avatar_root=self.avatar_root, logo_root=self.logo_root,
cover_root=self.cover_root, stream_root=self.stream_root,
@@ -477,6 +478,22 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
unauthorized = await self.client.get("/dist/peach-ui.js")
self.assertEqual(unauthorized.status_code, 401)
+ async def test_annotation_tool_is_served_only_where_it_was_built(self):
+ """`/dev/agentation.js` 只在本机构建过的检出里有东西,其余部署一律 404。"""
+ missing = await self.client.get("/dev/agentation.js?t=secret")
+ self.assertEqual(missing.status_code, 404)
+
+ bundle = self.settings.agentation_path
+ bundle.parent.mkdir(parents=True)
+ bundle.write_text("export {};\n", encoding="utf-8")
+ served = await self.client.get("/dev/agentation.js?t=secret")
+ self.assertEqual(served.status_code, 200)
+ self.assertTrue(served.headers["content-type"].startswith("text/javascript"))
+ self.assertEqual(served.headers["cache-control"], "no-cache")
+
+ unauthorized = await self.client.get("/dev/agentation.js")
+ self.assertEqual(unauthorized.status_code, 401)
+
def _swap_http_client(self, upstream):
fake = httpx.Client(transport=httpx.MockTransport(upstream), follow_redirects=True)
original = self.app.state.http_transport.client
diff --git a/web/app.js b/web/app.js
index da1e28a85..2962a4be1 100644
--- a/web/app.js
+++ b/web/app.js
@@ -1326,6 +1326,18 @@ if(cameFromSetup){
const clean=new URL(location.href);clean.searchParams.delete('onboarding');
history.replaceState(history.state,'',clean.pathname+(clean.search||'')+clean.hash);
}
+/* 界面标注工具(docs/FRONTEND.md「界面标注」):`?agentation=on|off` 写这台设备的开关并从
+ 地址里去掉;开关开着才请求 `/dev/agentation.js`,没在本机构建过就是 404,静默不装。 */
+{
+ const clean=new URL(location.href),flag=clean.searchParams.get('agentation');
+ if(flag==='on')localStorage.setItem('peach.agentation','on');
+ if(flag==='off')localStorage.removeItem('peach.agentation');
+ if(flag!==null){
+ clean.searchParams.delete('agentation');
+ history.replaceState(history.state,'',clean.pathname+(clean.search||'')+clean.hash);
+ }
+ if(localStorage.getItem('peach.agentation')==='on')import('/dev/agentation.js').catch(()=>{});
+}
const postSetupTutorialTasks=async()=>{
const {library,scraping,taste,follow,credentials,review}=await api('/api/post-setup-tutorial');
const scrapingCredentialSources=(scraping.sources||[]).filter(source=>source.accepts_cookie);