Files
yunke/docs/superpowers/specs/2026-08-10-yunke-wa-sync-wiring.md
2026-08-10 16:27:27 +08:00

39 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 接 WA 自动同步(accounts + friends)
> 2026-08-10|scraper/orchestrator plumbing|WA accounts/friends scraper 存在但未接 orchestrator/scheduler
> WA messages(per-account 遍历)留后续(Phase 2)。
## 现状
- `scrape_whatsapp_accounts(client, page_size)` / `scrape_whatsapp_friends(client, department_id, page_size)` 存在(app/scraper/whatsapp.py)。
- `upsert_whatsapp_account`(repos.py:440)存在。检查 `upsert_whatsapp_friend` 是否存在;若不存在需加(镜像 upsert_wechat_friend)。
- orchestrator 有 `sync_wechat_accounts`/`sync_wechat_friends`(thin wrappers→`_sync_full_rescrape`),但**无 sync_whatsapp_***。
- scheduler ENTITY_FUNCS 有 `wa_voice_calls` 但**无 wa_accounts/wa_friends**。
- ENTITY 常量缺 `ENTITY_WHATSAPP_ACCOUNT` / `ENTITY_WHATSAPP_FRIEND`。
## 改动
### `app/sync/orchestrator.py`
1. 加 `ENTITY_WHATSAPP_ACCOUNT = "whatsapp_account"` + `ENTITY_WHATSAPP_FRIEND = "whatsapp_friend"` 常量(line ~108 附近)。
2. 加 `sync_whatsapp_accounts(session, client)` → `_sync_full_rescrape(entity_type=ENTITY_WHATSAPP_ACCOUNT, channel="whatsapp", scrape=lambda: scrape_whatsapp_accounts(client), upsert=upsert_whatsapp_account)`。
> WA scraper 不需要 department_id(只需 page_size);channel 标记 whatsapp。
3. 加 `sync_whatsapp_friends(session, client, department_id=None)` → 镜像 `sync_wechat_friends`,调 `scrape_whatsapp_friends(client, department_id=resolved)`。
> 若 `upsert_whatsapp_friend` 不存在,加一个(镜像 `upsert_wechat_friend`——left join raw_json→structured)。
4. import `scrape_whatsapp_accounts`/`scrape_whatsapp_friends` from app.scraper.whatsapp。
### `app/sync/scheduler.py`
1. 加 `_run_wa_accounts` + `_run_wa_friends`(thin wrappers,镜像 `_run_wa_voice_calls`)。
2. `ENTITY_FUNCS` 加 `"wa_accounts": _run_wa_accounts` + `"wa_friends": _run_wa_friends`。
3. 日常 `all` job 加 wa_accounts + wa_friends(在 wa_voice_calls 附近)。
### 测试
```bash
cd yunke-server && DB_URL="sqlite+aiosqlite:///./smoke.db" .venv/Scripts/python.exe -m app.sync.cli sync --entity wa_accounts
cd yunke-server && DB_URL="sqlite+aiosqlite:///./smoke.db" .venv/Scripts/python.exe -m app.sync.cli sync --entity wa_friends
```
+ `pytest -q` 全绿。
### 验收
- `sync --entity wa_accounts` / `sync --entity wa_friends` 成功(fetched>0)。
- `pytest -q` 全绿。
- scheduler `all` job 包含 WA accounts/friends。