返回 Social Auto Upload
2026-03-25-browser-cli-unification-implementation.md
根目录 / docs / superpowers / plans / 2026-03-25-browser-cli-unification-implementation.md
1 # Browser CLI Unification Implementation Plan
2
3 > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
5 **Goal:** 为抖音、快手、小红书三家浏览器平台统一 `sau` CLI 契约,补齐小红书 CLI/skill,并把视频正文统一为 `desc`、图文正文统一为 `note`。
6
7 **Architecture:** 保持现有 `sau_cli.py` 单文件主入口,不引入新的 CLI 框架。`sau_cli.py` 负责 parser、request dataclass、dispatch 和账号文件解析;各平台 uploader 只做最小字段接线;skill、README、CLI/install/update 文档、examples 同步切到统一契约,避免对外同时暴露多套命名。
8
9 **Tech Stack:** Python 3.10+, `argparse`, `asyncio`, `dataclasses`, `pathlib`, `unittest`, existing Patchright-based uploaders, repository markdown docs
10
11 ---
12
13 ## File Structure
14
15 ### New files
16
17 - `tests/test_sau_browser_cli.py`
18 - 覆盖三家浏览器平台统一 CLI parser / dispatch / request 映射
19 - `skills/xiaohongshu-upload/SKILL.md`
20 - 小红书 CLI skill 主说明
21 - `skills/xiaohongshu-upload/references/runtime-requirements.md`
22 - 小红书运行前提
23 - `skills/xiaohongshu-upload/references/cli-contract.md`
24 - 小红书 CLI 契约
25 - `skills/xiaohongshu-upload/references/troubleshooting.md`
26 - 小红书排障文档
27 - PowerShell 示例命令
28 - shell 示例命令
29 - Python 命令模板
30
31 ### Modified files
32
33 - `sau_cli.py`
34 - 新增 `xiaohongshu` parser / dispatch
35 - 改造抖音、快手 request dataclass 和参数模型
36 - `uploader/douyin_uploader/main.py`
37 - 给视频接入 `desc`
38 - 给图文接入 `title + note`
39 - `uploader/ks_uploader/main.py`
40 - 给视频接入 `desc`
41 - 给图文接入 `title + note`
42 - `uploader/xiaohongshu_uploader/main.py`
43 - 只补 CLI 接线所需的最小字段适配
44 - `README.md`
45 - 补小红书 CLI / skill,并统一三家参数说明
46 - `docs/CLI.md`
47 - 统一三家浏览器平台命令契约
48 - `docs/install.md`
49 - 补小红书 CLI 示例和统一参数说明
50 - `docs/update.md`
51 - 补小红书检查项与 skill 路径
52 - `skills/douyin-upload/SKILL.md`
53 - `skills/douyin-upload/references/cli-contract.md`
54 - `skills/kuaishou-upload/SKILL.md`
55 - `skills/kuaishou-upload/references/cli-contract.md`
56 - `examples/get_xiaohongshu_cookie.py`
57 - `examples/upload_video_to_xiaohongshu.py`
58
59 ## Task 1: 用测试锁定统一 CLI 契约
60
61 **Files:**
62 - Create: `tests/test_sau_browser_cli.py`
63 - Reference: `sau_cli.py`
64
65 - [ ] **Step 1: 写统一 CLI parser 测试**
66
67 在 `tests/test_sau_browser_cli.py` 里覆盖最小命令契约:
68
69 ```python
70 import asyncio
71 import unittest
72 from argparse import Namespace
73 from pathlib import Path
74 from unittest.mock import AsyncMock, patch
75
76 import sau_cli
77
78
79 class BrowserCliParserTests(unittest.TestCase):
80 def test_build_parser_accepts_xiaohongshu_login(self):
81 parser = sau_cli.build_parser()
82 args = parser.parse_args(["xiaohongshu", "login", "--account", "creator"])
83 self.assertEqual(args.platform, "xiaohongshu")
84 self.assertEqual(args.action, "login")
85
86 def test_douyin_upload_video_accepts_desc(self):
87 parser = sau_cli.build_parser()
88 args = parser.parse_args([
89 "douyin", "upload-video",
90 "--account", "creator",
91 "--file", "demo.mp4",
92 "--title", "标题",
93 "--desc", "视频简介",
94 ])
95 self.assertEqual(args.desc, "视频简介")
96
97 def test_kuaishou_upload_note_accepts_title_and_note(self):
98 parser = sau_cli.build_parser()
99 args = parser.parse_args([
100 "kuaishou", "upload-note",
101 "--account", "creator",
102 "--images", "1.png",
103 "--title", "图文标题",
104 "--note", "图文正文",
105 ])
106 self.assertEqual(args.title, "图文标题")
107 self.assertEqual(args.note, "图文正文")
108 ```
109
110 - [ ] **Step 2: 写 dispatch 路由测试**
111
112 继续在 `tests/test_sau_browser_cli.py` 中补最小 dispatch 验证:
113
114 ```python
115 class BrowserCliDispatchTests(unittest.TestCase):
116 def test_dispatch_xiaohongshu_check_prints_valid(self):
117 args = Namespace(platform="xiaohongshu", action="check", account="creator")
118 with patch("sau_cli.check_xiaohongshu_account", new=AsyncMock(return_value=True)):
119 code = asyncio.run(sau_cli.dispatch(args))
120 self.assertEqual(code, 0)
121
122 def test_dispatch_douyin_upload_note_uses_new_request_fields(self):
123 args = Namespace(
124 platform="douyin",
125 action="upload-note",
126 account="creator",
127 images=[Path("1.png")],
128 title="图文标题",
129 note="图文正文",
130 tags="测试,图文",
131 schedule=0,
132 debug=False,
133 headless=True,
134 )
135 with patch("sau_cli.upload_note", new=AsyncMock()) as mock_upload:
136 asyncio.run(sau_cli.dispatch(args))
137 request = mock_upload.await_args.args[0]
138 self.assertEqual(request.title, "图文标题")
139 self.assertEqual(request.note, "图文正文")
140 ```
141
142 - [ ] **Step 3: 先跑测试确认失败**
143
144 Run:
145
146 ```powershell
147 py -3 -m unittest tests.test_sau_browser_cli -v
148 ```
149
150 Expected:
151
152 - 因为 `sau_cli.py` 还没有 `xiaohongshu` 分支和统一字段而失败
153
154 - [ ] **Step 4: 提交测试脚手架**
155
156 ```powershell
157 git add tests/test_sau_browser_cli.py
158 git commit -m "test: define browser cli unification contract"
159 ```
160
161 ## Task 2: 改 `sau_cli.py`,补齐统一参数与小红书路由
162
163 **Files:**
164 - Modify: `sau_cli.py`
165 - Reference: `uploader/xiaohongshu_uploader/main.py`
166 - Reference: `uploader/douyin_uploader/main.py`
167 - Reference: `uploader/ks_uploader/main.py`
168
169 - [ ] **Step 1: 增加小红书 request dataclass**
170
171 在 `sau_cli.py` 中新增:
172
173 ```python
174 @dataclass(slots=True)
175 class XiaohongshuVideoUploadRequest:
176 account_name: str
177 video_file: Path
178 title: str
179 description: str
180 tags: list[str]
181 publish_date: datetime | int
182 thumbnail_file: Path | None = None
183 publish_strategy: str = XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE
184 debug: bool = True
185 headless: bool = True
186
187
188 @dataclass(slots=True)
189 class XiaohongshuNoteUploadRequest:
190 account_name: str
191 image_files: list[Path]
192 title: str
193 note: str
194 tags: list[str]
195 publish_date: datetime | int
196 publish_strategy: str = XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE
197 debug: bool = True
198 headless: bool = True
199 ```
200
201 - [ ] **Step 2: 改造抖音、快手 request dataclass**
202
203 把字段统一到以下模型:
204
205 ```python
206 class DouyinVideoUploadRequest:
207 title: str
208 description: str
209
210 class DouyinNoteUploadRequest:
211 title: str
212 note: str
213
214 class KuaishouVideoUploadRequest:
215 title: str
216 description: str
217
218 class KuaishouNoteUploadRequest:
219 title: str
220 note: str
221 ```
222
223 要求:
224
225 - 视频用 `description`
226 - 图文用 `note`
227 - 不再把图文 request 设计成只有历史 `note` 语义但没有 `title`
228
229 - [ ] **Step 3: 改 parser**
230
231 在 `build_parser()` 里完成:
232
233 - `douyin upload-video` 增加 `--desc`
234 - `kuaishou upload-video` 增加 `--desc`
235 - `douyin upload-note` 改成 `--title --note`
236 - `kuaishou upload-note` 改成 `--title --note`
237 - 新增整套 `xiaohongshu`:
238 - `login`
239 - `check`
240 - `upload-video --title --desc --tags --thumbnail`
241 - `upload-note --images --title --note --tags`
242
243 - [ ] **Step 4: 补小红书账号操作函数**
244
245 在 `sau_cli.py` 中新增:
246
247 ```python
248 async def login_xiaohongshu_account(account_name: str, headless: bool = True) -> dict: ...
249 async def check_xiaohongshu_account(account_name: str) -> bool: ...
250 async def upload_xiaohongshu_video(request: XiaohongshuVideoUploadRequest) -> Path: ...
251 async def upload_xiaohongshu_note(request: XiaohongshuNoteUploadRequest) -> Path: ...
252 ```
253
254 要求:
255
256 - 账号路径继续走 `resolve_account_file("xiaohongshu", account_name)`
257 - 登录 / 检查分别复用 `xiaohongshu_setup` 与 `cookie_auth`
258 - 上传前先做 `setup(handle=False)` 校验
259
260 - [ ] **Step 5: 改 dispatch**
261
262 要求:
263
264 - 三家浏览器平台都统一输出:
265 - `login` 成功时打印账号文件路径
266 - `check` 输出 `valid` / `invalid`
267 - `upload-video` 打印简洁摘要
268 - `upload-note` 打印图片数量摘要
269 - request 构造统一映射:
270 - 视频:`title + description`
271 - 图文:`title + note`
272
273 - [ ] **Step 6: 跑 CLI 测试确认通过**
274
275 Run:
276
277 ```powershell
278 py -3 -m unittest tests.test_sau_browser_cli -v
279 ```
280
281 Expected:
282
283 - `BrowserCliParserTests`
284 - `BrowserCliDispatchTests`
285
286 全部通过
287
288 - [ ] **Step 7: 跑最小 help 自检**
289
290 Run:
291
292 ```powershell
293 py -3 sau_cli.py douyin --help
294 py -3 sau_cli.py kuaishou --help
295 py -3 sau_cli.py xiaohongshu --help
296 py -3 sau_cli.py xiaohongshu upload-note --help
297 ```
298
299 Expected:
300
301 - 小红书子命令存在
302 - 图文命令展示 `--title`、`--note`
303 - 视频命令展示 `--desc`
304
305 - [ ] **Step 8: 提交 CLI 主线**
306
307 ```powershell
308 git add sau_cli.py tests/test_sau_browser_cli.py
309 git commit -m "feat: unify browser cli contracts"
310 ```
311
312 ## Task 3: 给 uploader 做最小字段接线
313
314 **Files:**
315 - Modify: `uploader/douyin_uploader/main.py`
316 - Modify: `uploader/ks_uploader/main.py`
317 - Modify: `uploader/xiaohongshu_uploader/main.py`
318 - Modify: `tests/test_xiaohongshu_uploader.py`
319
320 - [ ] **Step 1: 给抖音视频接入 `desc`**
321
322 在 `DouYinVideo.__init__()` 中补 `desc` 参数和 `self.desc`,并把发布页填写逻辑改成:
323
324 ```python
325 await self.fill_title_and_description(page, self.title, self.desc or self.title, self.tags)
326 ```
327
328 - [ ] **Step 2: 给抖音图文接入 `title + note`**
329
330 在 `DouYinNote.__init__()` 中补 `title` 参数,保留 `note` 作为图文正文,发布页填写改成:
331
332 ```python
333 await self.fill_title_and_description(page, self.title, self.note, self.tags)
334 ```
335
336 要求:
337
338 - `title` 必填
339 - `note` 可选但建议非空;如果现有逻辑要求非空,就继续保留校验
340
341 - [ ] **Step 3: 给快手视频接入 `desc`**
342
343 在 `KSVideo.__init__()` 中补 `desc` 参数和 `self.desc`,填写“描述”区域时改成:
344
345 ```python
346 await page.keyboard.type(self.desc or self.title)
347 ```
348
349 - [ ] **Step 4: 给快手图文接入 `title + note`**
350
351 在 `KSNote.__init__()` 中补 `title` 参数与 `self.title`,保留 `self.note` 作为正文。
352
353 要求:
354
355 - 图文上传校验里增加 `title` 必填
356 - 如果页面当前只有正文输入区,没有独立标题区,仍然要在 CLI / request / 构造函数层保持 `title` 字段,以便后续平台对齐
357
358 - [ ] **Step 5: 检查小红书 CLI 接线是否需要补充适配**
359
360 确认 `XiaoHongShuVideo` / `XiaoHongShuNote` 只需要以下映射即可:
361
362 ```python
363 title=request.title
364 desc=request.description # 视频
365 desc=request.note # 图文
366 ```
367
368 如果 `XiaoHongShuNote` 里还保留历史 `note` 兼容,不要删,只保持 CLI 层优先走 `title + note + tags`。
369
370 - [ ] **Step 6: 补一条小红书映射测试**
371
372 在 `tests/test_xiaohongshu_uploader.py` 中增加最小断言:
373
374 ```python
375 def test_note_title_defaults_do_not_override_explicit_title(self):
376 app = xhs_main.XiaoHongShuNote(
377 image_paths=["a.png"],
378 note="正文",
379 tags=[],
380 publish_date=0,
381 account_file="account.json",
382 title="显式标题",
383 desc="图文正文",
384 )
385 self.assertEqual(app.title, "显式标题")
386 self.assertEqual(app.desc, "图文正文")
387 ```
388
389 - [ ] **Step 7: 跑 uploader 相关测试**
390
391 Run:
392
393 ```powershell
394 py -3 -m unittest tests.test_xiaohongshu_uploader -v
395 ```
396
397 Expected:
398
399 - 小红书 uploader 相关单测通过
400
401 - [ ] **Step 8: 提交 uploader 接线**
402
403 ```powershell
404 git add uploader/douyin_uploader/main.py uploader/ks_uploader/main.py uploader/xiaohongshu_uploader/main.py tests/test_xiaohongshu_uploader.py
405 git commit -m "feat: align browser uploader metadata fields"
406 ```
407
408 ## Task 4: 新增小红书 skill,并同步更新抖音、快手 skill
409
410 **Files:**
411 - Create: `skills/xiaohongshu-upload/SKILL.md`
412 - Create: `skills/xiaohongshu-upload/references/runtime-requirements.md`
413 - Create: `skills/xiaohongshu-upload/references/cli-contract.md`
414 - Create: `skills/xiaohongshu-upload/references/troubleshooting.md`
415 - Modify: `skills/douyin-upload/SKILL.md`
416 - Modify: `skills/douyin-upload/references/cli-contract.md`
417 - Modify: `skills/kuaishou-upload/SKILL.md`
418 - Modify: `skills/kuaishou-upload/references/cli-contract.md`
419
420 - [ ] **Step 1: 复制现有 skill 目录结构作为小红书骨架**
421
422 要求:
423
424 - 风格对齐 `skills/douyin-upload/`
425 - 默认优先走 `sau xiaohongshu ...`
426 - 明确小红书支持:
427 - `login`
428 - `check`
429 - `upload-video`
430 - `upload-note`
431
432 - [ ] **Step 2: 写小红书 CLI 契约**
433
434 至少写清:
435
436 ```bash
437 sau xiaohongshu login --account <account>
438 sau xiaohongshu check --account <account>
439 sau xiaohongshu upload-video --account <account> --file <video> --title "<title>" [--desc "..."] [--tags ...]
440 sau xiaohongshu upload-note --account <account> --images <img...> --title "<title>" [--note "..."] [--tags ...]
441 ```
442
443 - [ ] **Step 3: 更新抖音、快手 skill 契约**
444
445 要求:
446
447 - 视频示例命令补 `--desc`
448 - 图文示例命令改成 `--title --note`
449 - 所有模板文件同步更新,不要只改文档不改脚本
450
451 - [ ] **Step 4: 做 skill 文件级自检**
452
453 Run:
454
455 ```powershell
456 Get-ChildItem skills\xiaohongshu-upload -Recurse
457 Get-Content skills\douyin-upload\references\cli-contract.md
458 Get-Content skills\kuaishou-upload\references\cli-contract.md
459 ```
460
461 Expected:
462
463 - 小红书 skill 目录完整
464 - 抖音、快手 skill 契约都已经切到新参数模型
465
466 - [ ] **Step 5: 提交 skill 变更**
467
468 ```powershell
469 git add skills/xiaohongshu-upload skills/douyin-upload skills/kuaishou-upload
470 git commit -m "feat: add xiaohongshu skill and align browser skill contracts"
471 ```
472
473 ## Task 5: 更新 examples 和 README / docs
474
475 **Files:**
476 - Modify: `examples/get_xiaohongshu_cookie.py`
477 - Modify: `examples/upload_video_to_xiaohongshu.py`
478 - Modify: `README.md`
479 - Modify: `docs/CLI.md`
480 - Modify: `docs/install.md`
481 - Modify: `docs/update.md`
482
483 - [ ] **Step 1: 改小红书 examples**
484
485 要求:
486
487 - `examples/get_xiaohongshu_cookie.py` 明确指向 `sau xiaohongshu login --account <account_name>` 这一主线
488 - `examples/upload_video_to_xiaohongshu.py` 说明当前主线优先走 CLI
489 - 如果继续保留 uploader 直连示例,注释里标明“调试入口 / 历史直连路径”
490
491 - [ ] **Step 2: 更新 README 平台表和快速开始**
492
493 README 至少要改这些位置:
494
495 - 平台能力表里把小红书改成:
496 - `CLI ✅`
497 - `Skill ✅`
498 - 快速开始里的浏览器平台命令改成统一契约
499 - 抖音、快手图文示例改成:
500
501 ```bash
502 sau douyin upload-note --account <account_name> --images videos/1.png videos/2.png --title "图文标题" --note "图文正文"
503 sau kuaishou upload-note --account <account_name> --images videos/1.png videos/2.png --title "图文标题" --note "图文正文"
504 sau xiaohongshu upload-note --account <account_name> --images videos/1.png videos/2.png --title "图文标题" --note "图文正文"
505 ```
506
507 - [ ] **Step 3: 更新 `docs/CLI.md`**
508
509 要求:
510
511 - 补 `xiaohongshu` 小节
512 - 把三家浏览器平台整理成一致说明:
513 - 视频:`title + desc + tags`
514 - 图文:`title + note + tags`
515 - 登录二维码说明补小红书
516
517 - [ ] **Step 4: 更新 `docs/install.md` 和 `docs/update.md`**
518
519 要求:
520
521 - 安装文档里补 `sau xiaohongshu --help`
522 - 更新文档里补小红书自检命令和 skill 路径
523 - 文档统一写 `account_name`
524
525 - [ ] **Step 5: 做文档与示例核对**
526
527 Run:
528
529 ```powershell
530 Get-Content README.md | Select-String -Pattern "xiaohongshu|upload-note|--note|--desc" -Context 1,2
531 Get-Content docs\CLI.md | Select-String -Pattern "xiaohongshu|--note|--desc" -Context 1,2
532 Get-Content docs\install.md | Select-String -Pattern "xiaohongshu" -Context 1,2
533 Get-Content docs\update.md | Select-String -Pattern "xiaohongshu" -Context 1,2
534 ```
535
536 Expected:
537
538 - README、CLI、install、update 都出现小红书 CLI
539 - 图文正文写成 `--note`
540 - 视频描述写成 `--desc`
541
542 - [ ] **Step 6: 跑最终最小验证**
543
544 Run:
545
546 ```powershell
547 py -3 -m unittest tests.test_sau_browser_cli tests.test_xiaohongshu_uploader -v
548 py -3 sau_cli.py xiaohongshu --help
549 py -3 sau_cli.py xiaohongshu upload-video --help
550 py -3 sau_cli.py xiaohongshu upload-note --help
551 ```
552
553 Expected:
554
555 - 单测通过
556 - 小红书 CLI 帮助存在
557 - 视频命令显示 `--desc`
558 - 图文命令显示 `--note`
559
560 - [ ] **Step 7: 提交收尾**
561
562 ```powershell
563 git add examples/get_xiaohongshu_cookie.py examples/upload_video_to_xiaohongshu.py README.md docs/CLI.md docs/install.md docs/update.md
564 git commit -m "docs: align browser cli docs and examples"
565 ```
566
566 lines MARKDOWN