AnyBot/docs/restructure/notes/b04f-teach-pages.md

36 lines
5.7 KiB
Markdown
Raw 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.

# b04f:teach.rs(643 行)+ pages.rs(972 行)— 示範錄製與頁面圖表
## teach.rs(「使用者做給 Bot 看」)
常數:DOUBLE_CLICK_MS=400、SCROLL_MERGE_MS=1500、FRAMES_PER_SEC=2、FRAME_WIDTH=1280;修飾鍵位掩碼 `CMD=1<<20, SHIFT=1<<17, CTRL=1<<18, OPT=1<<19`(257–260,macOS 旗標格式,Linux 事件也翻成同一套)。
靜態:`EVENTS: Mutex<Vec<Raw>>`、`EVENT_FRAMES: Mutex<Vec<usize>>`、`FRAMES: AtomicUsize`、`FRAME_STOP: AtomicBool`、`FRAME_HANDLE: Mutex<Option<JoinHandle>>`、`REC_DIR: Mutex<Option<PathBuf>>`、RECORDING Mutex。
- `Raw`=Click/Key/Scroll 原始事件(只聽不改:listen-only tap)。
- `start(bot_name)`(57):已在錄→Err「已經在錄了」;清 EVENTS/EVENT_FRAMES;**FRAMES 歸零在開聽之前**(實讀註記:舊版前 0.5 秒事件帶到上一份的幀號,步驟對到舊圖)。`mac::start()?`(macOS 未給輸入監聽權限=Err)。
- `start_frames()`(74):demos_dir=<data>/demos(httpd::ensure_private_dir+tighten_dir_permissions);子資料夾=時間戳;執行緒每 1000/FRAMES_PER_SEC ms 抓 `computer::capture_screen`(screen_supported+screen_capture_ok 才抓)、>1280 寬就 Triangle 縮 → `frame_{n:05}.jpg`,存成功才 FRAMES+1。
- `stop_frames()`(113):FRAME_STOP→**join 線程**(實讀註記:舊固定 700ms 睡會贏慢抓圖→脏檔+卡窗);有 frame_00000 → **背景執行緒**跑 make_video(視窗不凍),mp4 寫成就刪 frame_*.jpg(被影片吃掉)。
- `make_video`(143):ffmpeg frame_*.jpg → recording.mp4(H.264)。
- `finish(name)`(164):mac::stop → **睡 300ms**(X11 監聽 150ms 輪播,最後事件會被判掉)→ compile(EVENTS, points_to_shot()) → 每步驟拷「該步前一張圖」frame_{frames_at[first]} → `step_{i:02}.jpg`;Demo{name,created,steps,video,shots}。
- `cancel()`(190):stop+清+remove_dir_all。
- 平台差:**macOS**(375)CGEventTapCreate/Enable/GetLocation(需輔助使用權限);**Linux**(501)desk::kind()!=X11 → 拒(Wayland 文案要人改選 Xorg 工作階段);x11::record 背景線程+**3 秒握手**(rx.recv_timeout;逾時設 STOP 關閘——留跑的線程=下次雙 RECORD context 雙事件+洩漏 X 連線)。**Windows 無此模組=錄示範在 Windows 不存在。**
- key_name(243)=macOS 鍵盤碼表(36 return…121 pagedown)。
- `view_demo_step` 工具 spec(201)+`view_step`(207)回 (text, ok, base64 JPEG):圖走 computer::fit+encode_jpeg;沒有該步圖→「(這一步沒有存到畫面)」;檔案不見→「(畫面檔案不見了)」;太大→「(畫面太大,沒辦法送出)」。
- `prompt_section(demos)`(349)=系統提示的示範段。
## pages.rs
常數:KEEP_VERSIONS=10、MAX_BODY=200_000、MAX_TITLE=120、`OTHERS_LISTED=30`(309,list 列別人頁面數上限)。
`NAMES = [write_page, read_page, list_pages]`(96)+`specs()`(78)/`label()`(98)/`kind_label()`(107)/`describe()`(120)。
- `Page{17}`:id,bot,title,summary,body,created,updated,revision,kind,kpis,verdict;`Kpi{54}`/`Version{63}`/`Written{142}`。
- kinds(实读 kind_label):data 数据分析/compare 比较评测/decision 决策备忘/howto 教学步骤/timeline 时间线/prose 长文/notes 笔记。
- `write_key`(150)=同 bot 同標題=覆寫(有 id 或同標題→找existing → `#{id}`;否则标题去空白小写 key)。`write` 實讀:有 id 無 content → 只動其他欄(title/summary/note/kind/kpis/verdict)——旧版回「頁面內容是空的」的修复。
- `write(pages, next_id, bot, args, now)`(162)=图表与块解析(实读 spec 全文+`Chart` 区块 663–792):页内块 `stats`/`steps`(勾选清单 [x]/[>]/[ ])/`chart`(**原生绘制** bar|line|pie,首行可写图型+标题,多系列首行 `| 1月 | 2月` 空格开头)/`details 标题`(默认收起)/`> [!NOTE]/[!TIP]/[!WARNING]/[!IMPORTANT]` call-outs/mermaid 块(pie, xychart-beta bar/line, timeline, gantt, flowchart)。渲染规则写进 spec(长条自动由大到小排序、自有顺序标签不排;单系列数字标图上不画 y 轴;多线线尾标系列名)。**存档时自动检查写法,被退回照列出改、同 id 带整页再送一次(第二次一定存)**——spec 逐字记。
- kpis 只放算得出的真数字(delta 如 +12%);verdict 只在有明确结论时写;kind 第一屏各有公式(data→结论+3 kpis+主图;compare→取舍+verdict+对比表;decision→verdict先讲+三理由+风险;howto→适合谁+steps;timeline→mermaid timeline;prose/notes→直接内文)。
- `read`(292,依 bot 分頁;name 閉包出 bot 名)/`list`(312)/`owner_of`(329)/`when/ago`(334/340)。
- `preview(p, lines)`(362)/`plain(s)`(391,去 markdown 裝飾)/`link_id(url)`(413,#page{id})。
- `card_json`(418)/`page_json`(422)/`version_of(p, revision)`(429)。
- `mermaid_blocks(body)`(434)=頁內 mermaid 圈块(diagram 渲染用,b05 diagram 互印)。
- `ids_in(calls)`(456)/`cards_by_message(messages)`(483)=訊息→卡片對應。
- `stats(src)`(502)=頁內資料表抽 (名,值,單位)。
- 副作用:Pages 存在 **State(跨 bot 共用一份)**;落盤隨 store(b01d)。
## 迁移
teach.rs → **platform::teach**(事件監聽=平台資產:mac CGEventTap / linux x11::record;ffmpeg 外部程式;Windows 缺=如實保留缺口)+core::teach::demo(Demo/compile/view_step 純邏輯)。pages.rs → **core::pages**(全邏輯,無平台依賴)。紅線:錄製常數(2fps/1280/400/1500/300ms/3s)逐字。