產物驗證(規格 §產物驗證 P2:大小、hash):每個附件存進 AnyBot 時就記 SHA-256 前 12 碼(大小本來就有)——手機預覽頁底下「檔案核對」、電腦檔案卡副標、附件 API、Bot 收到 send_file 結果都看得到;下載後 shasum -a 256 對前 12 碼就知道檔案完整、沒被動過。零新訊號系統:sha 在儲存時算,之後只讀。

已驗證: cargo test 816 全綠(新增 attach::a_stored_file_carries_its_size_and_hash:sha256("hi") 前 12 碼正確);featuremap:: 通過(deliver.md 壓回 8997);verify-anybot 實跑 deliver.md 傳檔步驟:手機預覽頁「檔案核對:大小 1 KB · SHA-256 1f2c19f57b8e」、API attachments.sha、與 shasum -a 256 對實際檔案獨立計算一致(target/verify-anybot/20261120-164422)
This commit is contained in:
王性驊 2026-11-20 16:46:28 +08:00
parent ba3bfeaea1
commit dd3ded5a24
7 changed files with 60 additions and 25 deletions

View File

@ -2563,7 +2563,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開)
- 新訊息 240 ms 淡入並往上浮;
- 表情回應 500 ms 彈出,帶一點回彈。
- **檔案**:
- Bot 用 `send_file` 把做好的檔案傳進聊天,你附加的檔案也一樣,顯示成檔案卡片:圖示依種類(試算表綠色、Markdown 紫色、網頁橘色、PDF 紅色、圖片、影片)、檔名(太長時保留副檔名)、大小。
- Bot 用 `send_file` 把做好的檔案傳進聊天,你附加的檔案也一樣,顯示成檔案卡片:圖示依種類(試算表綠色、Markdown 紫色、網頁橘色、PDF 紅色、圖片、影片)、檔名(太長時保留副檔名)、大小。每個附件存進 AnyBot 時就記下大小和 SHA-256(前 12 碼):手機預覽頁底下和電腦卡片上都有「檔案核對」,Bot 收到檔案時也看得到——下載後用 `shasum -a 256` 對前 12 碼,就知道檔案完整、沒被動過。
- 點卡片打開預覽(`shots/file-preview.png`):
- 上面是檔名和「22 列」,右上角有「用其他 App 打開」、下載、關閉。
- Excel(xlsx/xls/ods)和 CSV:表格,有列號和標題列,欄寬跟內容走,太長的格子滑過看全文,多個工作表可以切換。

View File

@ -1,53 +1,53 @@
# 交出結果:傳檔、打開、發佈網站
Bot 做好的東西用三種方式交給使用者:放進聊天變成檔案卡片、直接在使用者的螢幕上打開,或發佈成公開網站。傳進聊天的檔案,系統記下它在哪台裝置顯示過、使用者點開沒有(交付確認)。傳到帳號裡另一台裝置是 `send_to_device`,見 [computers](computers.md)。
Bot 交結果有三種方式:放進聊天變檔案卡片、在使用者的螢幕上打開、發佈成公開網站。傳進聊天的檔案:系統記它在哪家裝置顯示過、有沒有被點開(交付確認);傳到帳號裡另一台裝置見 [computers](computers.md)。
## 子功能
- `deliver-send` 傳檔:`send_file` 把檔案放進聊天變成檔案卡片,點一下預覽、可以下載。
- `deliver-send` 傳檔:`send_file` 把檔案放進聊天變成檔案卡片,點一下預覽、可以下載。每個附件存進 AnyBot 時就記大小和 SHA-256(前 12 碼):手機預覽頁底下、電腦檔案卡上都有「檔案核對」,下載後用 `shasum -a 256` 對前 12 碼就知道檔案完整。
- `deliver-confirm` 交付確認:Bot 傳來的檔案(`send_file`、給使用者看畫面的截圖)各有編號;每台裝置回報它做了什麼,記在大腦那則附件上:已在聊天 → 已在某台裝置顯示 → 使用者點開了,或「顯示失敗」加原因。每台每種只記第一次,最多 12 筆;記號和「已顯示/已點開」只看使用者自己的裝置。
- 誰回報:大腦自己的視窗(「這台電腦」:卡片在畫面裡、視窗在前面算顯示,點開算點開,檔案不見了算失敗);螢幕電腦(用它的名字,抓大腦的檔案被說找不到算失敗);手機(iPhone、iPad、Android 或「瀏覽器」,團隊成員是「名字 的 iPhone」:圖片真的載入而且在畫面裡算顯示,檔案卡片在畫面裡算顯示,預覽真的畫出來(文字、表格、圖、PDF 的頁、能播的錄音影片)或存到手機算點開,大腦回錯誤或圖片解不開算失敗)。網路斷了不算失敗,使用者還沒打開 App 也不算。
- 誰回報:大腦自己的視窗(「這台電腦」:卡片在畫面、視窗在前算顯示,點開算點開,檔案不見算失敗);螢幕電腦(用它的名字,抓大腦的檔案被說找不到算失敗);手機(iPhone、iPad、Android 或「瀏覽器」,團隊成員是「名字 的 iPhone」:圖片真的載入且在畫面裡算顯示,檔案卡片在畫面裡算顯示,預覽真的畫出來(文字、表格、圖、PDF 的頁、能播的錄音影片)或存到手機算點開,大腦回錯誤或圖片解不開算失敗)。斷線不算失敗,沒打開 App 也不算。
- Bot 讀得到:`send_file` 的結果只說「已放進聊天」,之後的回合結果後面帶一行〔交付狀態:…〕。
- 有裝置顯示失敗、而且還沒有任何裝置顯示過,Bot 被叫醒一次(團隊成員、管理員的裝置只記下、不叫醒,最多佔 4 筆;群組聊天、給使用者看畫面的截圖、團隊成員或訊息管道的人要的檔案都只記下、不叫醒)(「[交付]」開頭:附件編號和副檔名、哪台、為什麼;不寫檔名),換方式再交付;使用者上次說話之後最多兩次。
- 有裝置顯示失敗、而且還沒有任何裝置顯示過,Bot 被叫醒一次(團隊成員、管理員的裝置只記下、不叫醒,最多佔 4 筆;群組聊天、給使用者看畫面的截圖、團隊成員或訊息管道要的檔案都只記下、不叫醒)(「[交付]」開頭:附件編號、副檔名、哪台、為什麼),換方式再交付;使用者上次說話之後最多兩次。
- `deliver-open` 打開給使用者看:`open_for_person` 在使用者的螢幕上打開檔案或網址。
- `deliver-publish` 發佈網站:`publish_site` 把一個 .html 檔或有 index.html 的資料夾發佈成公開網站,回傳網址,同一個名字再發佈就取代。
- `deliver-publish` 發佈網站:`publish_site` 把 .html 檔(或含 index.html 的資料夾)發佈成公開網站,回網址,同名再發佈就取代。
## 使用者怎麼到
- `send_file` 的檔案卡片在回覆裡,點一下預覽(「下載檔案」「用其他 App 打開」);右側面板「資料庫」也列出這個對話裡的檔案。
- 交付確認:Bot 傳來的附件下面一個灰色小記號——✓ 已在聊天、✓✓ 已在某台裝置顯示、點開過的 ✓✓ 是強調色;滑過記號列出每台裝置、時間、顯示/點開/失敗和原因。使用者自己附的檔案沒有記號。
- `send_file` 的檔案卡片在回覆裡,點一下預覽(「下載檔案」「用其他 App 打開」);右側面板「資料庫」也列這個對話的檔案。
- 交付確認:Bot 傳來的附件下面一個灰色小記號——✓ 已在聊天、✓✓ 已在裝置顯示、點開過的用強調色;滑過記號列每台裝置、時間、做了什麼(顯示/點開/失敗加原因)。使用者自己附的檔案沒有記號。
- `publish_site` 要先在 設定 › 一般 ›「手機與帳號」登入帳號才有([account](account.md))。
- 手機:聊天裡的檔案卡片,點了在 App 裡預覽、右上角存到手機;聊天右上「⋯」›「對話詳細資訊」›「資料庫」([phone](phone.md))。附件下面同樣的記號,點一下跳出清單。
- 手機:聊天裡的檔案卡片點了在 App 裡預覽、右上角存到手機;「⋯」›「對話詳細資訊」›「資料庫」([phone](phone.md))。附件下面同樣的記號,點一下跳出清單。
## 怎麼操作(verify-anybot)
- Bot:交結果用 `send_file`,不要只說檔案在哪;傳了不等於使用者已收到:以結果後面的〔交付狀態〕為準,使用者問收到沒照它回答;被「[交付]」叫醒時換格式、縮小或用 `send_to_device` 再交一次;要使用者馬上看到用 `open_for_person`;要公開網址用 `publish_site`(path,name 只用英文字母、數字和 -)。`open_for_person`、`publish_site` 屬於「檔案與指令」能力,`send_file` 每個 Bot 都有。
- Bot:交結果用 `send_file`,不要只說檔案在哪;傳了不等於收到:以結果後面的〔交付狀態〕為準;被「[交付]」叫醒時換格式、縮小或 `send_to_device` 再交一次;要馬上看到用 `open_for_person`,要公開網址用 `publish_site`(name 只用英文、數字和 -)。`open_for_person`、`publish_site` 屬於「檔案與指令」能力,`send_file` 每個 Bot 都有。
前提:
- 基本前提和「手機已登入」(README),開著 Bot 1 的聊天:`$CTL phone click '.row[data-bot="1"]'`。
- scratch 的大腦是 `--serve`(AnyBot 在伺服器上)、沒有登入帳號;Bot 的檔案在它自己的電腦(沙盒資料夾)。
- **傳檔、預覽**(covers `deliver-send`):請 Bot 寫 report.md 傳給你,點卡片看,再看資料庫。指令:`$CTL phone send '寫報告 [[call edit_file {"path":"report.md","old_text":"","new_text":"# 本週報告\n\n一切順利。\n"}]]' && $CTL phone wait-text 已建立 --in '#msgs' && $CTL phone send '傳給我 [[call send_file {"path":"report.md"}]]' && $CTL phone wait 'a.filec[data-name="report.md"]' && $CTL phone shot deliver-card && $CTL phone click 'a.filec[data-name="report.md"]' && $CTL phone wait-text 一切順利 --in '#filebody' && $CTL phone shot deliver-preview && $CTL phone click '[data-back]' && $CTL phone click '#b-chatmore' && $CTL phone click '#sheet-in .opt' --text 對話詳細資訊 && $CTL phone click '[data-tab="library"]' && $CTL phone wait-text 'Bot 傳的' --in '#details' && $CTL api GET /api/bots/1 --jq '[.messages[].attachments[]? | {name, kind, bytes}]'`。看到:回覆下有「report.md · Markdown · 1 KB · 點一下開啟」的卡片;點開是排好的「本週報告/一切順利。」;資料庫的「檔案」有 report.md(Bot 傳的);API 的附件是 report.md、Markdown、32 bytes。
- **傳檔、預覽**(covers `deliver-send`):請 Bot 寫 report.md 傳給你,點卡片看,再看資料庫。指令:`$CTL phone send '寫報告 [[call edit_file {"path":"report.md","old_text":"","new_text":"# 本週報告\n\n一切順利。\n"}]]' && $CTL phone wait-text 已建立 --in '#msgs' && $CTL phone send '傳給我 [[call send_file {"path":"report.md"}]]' && $CTL phone wait 'a.filec[data-name="report.md"]' && $CTL phone shot deliver-card && $CTL phone click 'a.filec[data-name="report.md"]' && $CTL phone wait-text 一切順利 --in '#filebody' && $CTL phone wait-text 'SHA-256' --in '#file-meta' && $CTL phone shot deliver-preview && $CTL phone click '[data-back]' && $CTL phone click '#b-chatmore' && $CTL phone click '#sheet-in .opt' --text 對話詳細資訊 && $CTL phone click '[data-tab="library"]' && $CTL phone wait-text 'Bot 傳的' --in '#details' && $CTL api GET /api/bots/1 --jq '[.messages[].attachments[]? | {name, kind, bytes, sha}]'`。看到:回覆下有「report.md · Markdown · 1 KB · 點一下開啟」的卡片;點開是排好的「本週報告/一切順利。」,底下「檔案核對」有大小和 SHA-256 前 12 碼;資料庫的「檔案」有 report.md(Bot 傳的);API 的附件是 report.md、Markdown、32 bytes、sha 不為空。
- **機密檔案不傳**(covers `deliver-send`):請 Bot 把 .env 傳給你。指令:`$CTL phone click '[data-back]' && $CTL phone send '寫 [[call edit_file {"path":".env","old_text":"","new_text":"TOKEN=abc"}]]' && $CTL phone wait '.step.done' --text .env && $CTL phone send '傳 [[call send_file {"path":".env"}]]' && $CTL phone wait '.step.denied' --text 傳檔案給你 && $CTL api GET /api/bots/1 --jq '[.messages[].tools[]? | select(.name == "send_file") | .status]'`。看到:Bot 自己的電腦上寫 .env 照樣成功,但傳檔那一步被擋:「被安全規則擋下:「…/.env」是私密或系統檔案,Bot 不能讀取。」;兩個 send_file 是 done、denied,聊天沒有第二張卡片。
- **伺服器上打不開**(covers `deliver-open`):請 Bot 打開 report.md,再打開一個網址。指令:`$CTL phone send '打開 [[call open_for_person {"path":"report.md"}]]' && $CTL phone wait-text '改用 send_file' --in '#msgs' && $CTL phone send '打開網址 [[call open_for_person {"path":"https://example.com/"}]]' && $CTL phone wait-text '不知道使用者現在在用哪台電腦' --in '#msgs' && $CTL api GET /api/bots/1 --jq '[.messages[].tools[]? | select(.name == "open_for_person") | .status]'`。看到:「AnyBot 在伺服器上,檔案沒辦法直接在使用者的螢幕打開。改用 send_file…」;網址是「不知道使用者現在在用哪台電腦,沒辦法打開給他看。把網址直接寫在回覆裡。」;兩步都是 failed。
- **沒登入就沒有發佈**(covers `deliver-publish`):指令:`$CTL phone send '發佈 [[call publish_site {"path":"report.md","name":"report"}]]' && $CTL phone wait-text '這一輪沒有 publish_site 這個工具' --in '#msgs' && $CTL model 'select(.want) | .want'`。看到:Bot 拿到的工具裡沒有 `publish_site`(假模型列出它有的);模型那層記下想要的是 publish_site。
- **打開在螢幕上、真的發佈**(covers `deliver-open`、`deliver-publish`):指令:走不到——打開要「這台電腦」或使用者正在用的帳號裡的電腦,發佈要帳號伺服器(`relay/`)。看到:最接近的真路是上面兩步的拒絕,和自動測試裡在這台電腦上傳檔、打開(工作資料夾外先問、程式或指令檔不打開、機密檔案不傳)。
- **交付確認:顯示、點開、Bot 照狀態回答**(covers `deliver-confirm`):Bot 在自己的電腦畫一張 PNG 傳過來,看記號,點開,再問 Bot 收到沒。指令:`$CTL phone send '畫 [[call shell {"command":"printf iVBORw0KGgoAAAANSUhEUgAAAKAAAABkCAIAAACO1KzYAAABDUlEQVR42u3cMQ0AIAxE0QpDCUoQgU504KHDDc1L2C/5b22os1f7vdt/djO7JTRgoQELDRgwYLuA7QK2Cxiw0ICFBiw0YLuA7QK2Cxiw0ICFBiw0YMBRYLFm7wIGLDRgoQEDBmwXsF3AdgEDFhqw0ICFBmwXsF3AdgEDFhqw0ICFBgw4CyyWq0qhAQsNGDBgu4DtArYLGLDQgIUGLDRgu4DtArYLGLDQgIUGLDRgwIDt+ifLrqtKu4ABCw1YaMBCA7YL2C5gu4ABCw1YaMBCA7YL2C5gu4ABCw1YaMBC+ycLkqtKu4DtArYLGLDQgIUGLDRgu4DtArYLGLDQgIUGLDRgwIDtArYLGLDQo3c/yQl3/IVNoK0AAAAASUVORK5CYII= | base64 -d > photo.png"}]]' && $CTL phone wait-text '(完成,沒有輸出)' --in '#msgs' && $CTL phone send '傳給我 [[call send_file {"path":"photo.png"}]]' && $CTL phone wait '.attimg[data-name="photo.png"] + .dlv.shown' && $CTL phone shot deliver-shown && $CTL phone click '.attimg[data-name="photo.png"]' && $CTL phone wait '#filebody img' && $CTL phone click '[data-back]' && $CTL phone wait '.attimg[data-name="photo.png"] + .dlv.opened' && $CTL phone click '.attimg[data-name="photo.png"] + .dlv' && $CTL phone wait-text 點開了 --in '#sheet-in' && $CTL phone shot deliver-sheet && $CTL phone click '#sheet' && $CTL api GET /api/bots/1 --jq '[.messages[].attachments[]? | select(.name=="photo.png") | {delivery, seen: [.seen[] | {device, kind}]}]' && $CTL model-reply 那張圖收到了嗎 '照交付狀態回答' && $CTL phone send '那張圖收到了嗎' && $CTL phone wait-text 照交付狀態回答 --in '#msgs' && $CTL model 'select(.rule=="那張圖收到了嗎") | .tool_results'`。看到:圖片下面灰色 ✓✓;點開預覽、返回後 ✓✓ 變強調色;點記號跳出「已點開」,列「iPhone 顯示了」「iPhone 點開了」和時間;API 的 delivery 是 opened、seen 是 iPhone 的 shown、opened;模型讀到的 send_file 結果是「已把「photo.png」放進聊天(…附件 …)。…不要說使用者已經看到。」後面接〔交付狀態:「photo.png」已被點開——iPhone顯示了(…);iPhone點開了(…)〕。
- **交付確認:顯示失敗叫醒一次**(covers `deliver-confirm`):Bot 傳一個壞掉的 .png。指令:`$CTL phone send '寫 [[call shell {"command":"printf not-a-picture > broken.png"}]]' && $CTL phone wait '.step.done' --text broken.png && $CTL phone send '傳 [[call send_file {"path":"broken.png"}]]' && $CTL phone wait-text 這張圖讀不到 --in '.attimg[data-name="broken.png"]' && $CTL phone wait-text '交付沒顯示' --in '#msgs' && $CTL phone click '.attimg[data-name="broken.png"] + .dlv' && $CTL phone wait-text 沒顯示出來 --in '#sheet-in' && $CTL phone shot deliver-failed && $CTL phone click '#sheet' && $CTL api GET /api/bots/1 --jq '{broken: [.messages[].attachments[]? | select(.name=="broken.png") | .seen], wakes: [.messages[] | select(.wake) | .text]}' && $CTL restart && $CTL phone open --listed && $CTL phone wait '.row[data-bot="1"]' && $CTL phone click '.row[data-bot="1"]' && $CTL phone wait-text 這張圖讀不到 --in '.attimg[data-name="broken.png"]' && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.wake)] | length'`。看到:壞圖顯示「這張圖讀不到」,下面只有一個 ✓;聊天多一則標「交付沒顯示」的訊息:「你傳的附件 …(.png)沒有在iPhone上顯示出來:這台裝置讀不出這個檔案(格式壞了或不支援)。…換個方式再交付一次…」,Bot 接著回答;清單是「已放進聊天」、紅字「iPhone 沒顯示出來:…」;重開 AnyBot、重新打開聊天後,失敗還記著、叫醒還是 1 次。
- **交付確認:螢幕電腦、大腦自己的視窗**(covers `deliver-confirm`):指令:走不到——螢幕電腦要另一台登入同帳號的 AnyBot;大腦視窗要開視窗(`ABV_ALLOW_WINDOW=1 $CTL desktop-shot`,要使用者同意)。看到:最接近的真路是自動測試:螢幕電腦換成本機副本時編號不變、回報經 `api/sync/act` 只送一次;視窗裡記號滑過列出裝置;這台電腦的回報只記一次。
- **桌面的預覽**(covers `deliver-send`):指令:要開視窗——`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot deliver-preview ANYBOT_SHOT_VIEW=fileview`。看到:示範的試算表在預覽裡打開,上面有「下載檔案」「用其他 App 打開」。
- **交付確認:螢幕電腦、大腦自己的視窗**(covers `deliver-confirm`):指令:走不到——螢幕電腦要另一台登入同帳號的 AnyBot;大腦視窗要開視窗(`ABV_ALLOW_WINDOW=1 $CTL desktop-shot`,要使用者同意)。看到:自動測試代:螢幕電腦換成本機副本時編號不變、回報經 `api/sync/act` 只送一次;視窗裡滑過記號列出裝置;回報只記一次。
- **桌面的預覽**(covers `deliver-send`):指令:要開視窗——`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot deliver-preview ANYBOT_SHOT_VIEW=fileview`。看到:示範的試算表在預覽裡打開,上面有「下載檔案」「用其他 App 打開」;聊天裡的檔案卡副標是「大小 · SHA-256 前 12 碼」。
- 自動測試:`cargo test tools::`、`cargo test preview::`、`cargo test app::files_ui::`、`cargo test agent::`(`a_file_leaves_a_computer_only_past_the_questions_of_reading_it`)、`cargo test attach::`、`cargo test app::deliver_ui::`、`cargo test a_phone_tells_what_it_did_with_a_file`、`cargo test a_files_delivery_is_published`、`cargo test a_screen_keeps_the_files_id_and_tells_the_brain`、`cargo test the_bot_reads_how_its_file_stands`、`cargo test a_handed_files_mark_tells_where_it_was_seen`。
## 陷阱
- 機密檔案不能傳、打開或發佈([files](files.md))。
- `open_for_person` 不打開程式或指令檔(打開就會執行),改用 `send_file`;AnyBot 在伺服器上時,網址會在使用者正在用的那台電腦打開(不知道是哪台就打不開),檔案打不開,也改用 `send_file`。
- SSH 電腦上的檔案不能直接 `send_file`、`open_for_person` 或 `publish_site`;帳號裡其他電腦的檔案可以 `send_file`(最多 20 MB),但不能打開或發佈。先複製到 Bot 自己的電腦。
- SSH 電腦上的檔案不能直接傳出、打開或發佈;帳號裡其他電腦的檔案可 `send_file`(≤20 MB)但不能打開或發佈——先複製到 Bot 自己的電腦。
- `publish_site`:任何知道網址的人都看得到。每個網站名字第一次會問(工作資料夾外的每次都問);最多 500 個檔案、25 MB;點開頭的檔案、node_modules、機密檔案、指到資料夾外的連結會略過;一定要有 index.html。
- 「在這台電腦上執行」設成永不允許(或被管理員鎖住)時,這台的檔案也不能傳出去、打開或發佈。
- 支線、交辦帶回主聊天的檔案只有記號,不叫醒、主 Bot 也讀不到〔交付狀態〕;管理員要的檔案和擁有者的一樣會叫醒(管理員的裝置只記下)。
- 交付確認只看得到「有沒有在畫面上出現、有沒有點開」,看不到使用者有沒有真的看懂;`send_to_device`、`open_for_person`、`publish_site` 沒有交付確認。比這個功能早傳的檔案沒有編號,也沒有記號。
- (驗證)手機頁面存在手機上(Service Worker):重新建置、`$CTL restart` 之後,`$CTL phone open --listed` 可能還是舊頁面;要新頁面用重新整理(`$CTL phone js 'location.reload(), 1'`)。
- (驗證)1×1 的小圖在聊天裡幾乎看不到,`wait` 會說沒出現:用上面那張 160×100 的。
- (驗證)scratch 只有 Bot 自己的電腦:私密檔案的規則在那裡只擋傳出去(寫 .env 照樣成功),不能拿來證明「不能讀」。
- 支線、交辦帶回主聊天的檔案只有記號,不叫醒,主 Bot 也讀不到〔交付狀態〕;管理員要的檔案和擁有者一樣會叫醒(管理員裝置只記下)。
- 交付確認只看得到「有沒有出現在畫面上、有沒有點開」,看不到看懂沒有;`send_to_device`、`open_for_person`、`publish_site` 沒有交付確認;比這功能早傳的檔案沒有編號和記號。
- (驗證)手機頁面存在手機上(Service Worker):重新建置、`$CTL restart` 後,`$CTL phone open --listed` 可能是舊頁面;重新整理取新(`$CTL phone js 'location.reload(), 1'`)。
- (驗證)1×1 的小圖在聊天裡幾乎看不到,`wait` 會說沒出現:用上面那張 160×100 的。- (驗證)scratch 只有 Bot 自己的電腦:私密檔案的規則在那裡只擋傳出去(寫 .env 照樣成功),不能拿來證明「不能讀」。

View File

@ -727,6 +727,7 @@ button,.row,.ib,.island,.opt,.chip,.tab,label.sw,[role="button"]{-webkit-user-se
<button class="ib" id="b-filesave" title="儲存"></button>
</div>
<div class="scroll" id="filebody"><div class="empty">載入中…</div></div>
<div class="note" id="file-meta" style="display:none;padding:8px 14px"></div>
<div class="pager" id="filepager" style="display:none">
<button class="btn sm" id="f-prev">‹ 上一頁</button><span id="f-at"></span><button class="btn sm" id="f-next">下一頁 ›</button>
</div>
@ -3559,6 +3560,7 @@ async function openFile(path, name, att) {
S.file = f;
$("file-name").textContent = f.name;
$("filebody").innerHTML = `<div class="empty">載入中…</div>`;
$("file-meta").style.display = "none";
$("filepager").style.display = "none";
go("file", {});
try {
@ -3566,6 +3568,11 @@ async function openFile(path, name, att) {
if (S.file !== f) return; // the person left while it loaded
Object.assign(f, {kind: v.kind, pages: v.pages || 0, data: v});
drawFile(f);
// 產物核對(規格 §產物驗證):AnyBot 存副本時記的大小和 SHA-256,下載後可對。
const a = f.att ? (S.msgs || []).flatMap((m) => m.attachments || []).find((x) => x.id === f.att) : null;
const meta = a ? [a.bytes ? "大小 " + kb(a.bytes) : "", a.sha ? "SHA-256 " + a.sha + "(前 12 碼,下載後可用 shasum -a 256 核對)" : ""].filter(Boolean).join(" · ") : "";
$("file-meta").textContent = meta ? "檔案核對:" + meta : "";
$("file-meta").style.display = meta ? "" : "none";
// (Drawn here at once; a picture, PDF or recording says so when it is really shown, and a file
// with no preview only when it is saved to the phone.)
if (f.att && ["markdown", "html", "text", "sheet"].includes(f.kind)) seenTell(f.att, "opened", "", f.bot);

View File

@ -1203,7 +1203,7 @@ impl TurnTool for SendFile {
Ok(a) => {
// (Only that it is in the chat: whether a device showed it is told later, as
// its 交付狀態 — the bot used to say 「已完成」 for a picture nobody ever saw.)
let out = format!("已把「{}」放進聊天({},附件 {})。有沒有在使用者的裝置上顯示,系統會記下;不要說使用者已經看到。", a.name, crate::attach::human(a.bytes), a.id);
let out = format!("已把「{}」放進聊天({},附件 {},SHA-256 {})。有沒有在使用者的裝置上顯示,系統會記下;不要說使用者已經看到。", a.name, crate::attach::human(a.bytes), a.id, a.sha);
send(StreamEvent::Attach(a));
(out, true)
}

View File

@ -186,7 +186,9 @@ impl App {
let max_w = rect.right() - tx - 12.0;
let name = fit_name(ui, &a.name, max_w);
ui.painter().text(egui::pos2(tx, rect.top() + 16.0), egui::Align2::LEFT_TOP, name, egui::FontId::proportional(FONT_BASE), p.text);
let sub = if a.bytes > 0 { crate::attach::human(a.bytes) } else { preview::kind_label(&a.name) };
let size = if a.bytes > 0 { crate::attach::human(a.bytes) } else { preview::kind_label(&a.name) };
// 產物核對(規格 §產物驗證):存副本時記的 SHA-256 前 12 碼,下載後可對。
let sub = if a.sha.is_empty() { size } else { format!("{size} · SHA-256 {}", a.sha) };
ui.painter().text(egui::pos2(tx, rect.top() + 36.0), egui::Align2::LEFT_TOP, sub, egui::FontId::proportional(FONT_XS), p.text3);
let r = r.on_hover_text(format!("{}({})", a.name, t("點一下預覽"))).on_hover_cursor(egui::CursorIcon::PointingHand);
if r.clicked() {

View File

@ -28,6 +28,11 @@ pub struct Attachment {
/// local copy; this stays). Empty on files from before.
#[serde(default, skip_serializing_if = "String::is_empty")]
pub id: String,
/// The file's SHA-256 (first 12 hex), taken when AnyBot stores its copy: the person can
/// check a download on their device is the file the bot handed over, whole (規格 §產物驗證:
/// 大小、hash). Empty on files from before.
#[serde(default, skip_serializing_if = "String::is_empty")]
pub sha: String,
/// 交付確認: what the devices said about a file the bot handed over — shown, opened, or not
/// shown and why. The first of each kind per device, at most MAX_SIGHTINGS.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@ -232,6 +237,15 @@ fn new_id() -> String {
crate::httpd::random_hex(6)
}
/// SHA-256 of bytes, first 12 hex — enough to catch a broken copy, short enough to read out.
fn sha12(bytes: &[u8]) -> String {
ring::digest::digest(&ring::digest::SHA256, bytes).as_ref().iter().take(6).map(|b| format!("{b:02x}")).collect()
}
fn sha12_file(path: &Path) -> String {
std::fs::read(path).map(|b| sha12(&b)).unwrap_or_default()
}
/// Copy a file in as an attachment.
pub fn store(bot_id: u64, src: &Path) -> Result<Attachment, String> {
let real = std::fs::canonicalize(src).map_err(|e| format!("讀不到 {}:{e}", src.display()))?;
@ -253,7 +267,7 @@ pub fn store(bot_id: u64, src: &Path) -> Result<Attachment, String> {
let dst = dir.join(format!("{}-{name}", chrono::Local::now().format("%Y%m%d%H%M%S%3f")));
std::fs::copy(&real, &dst).map_err(|e| format!("複製失敗:{e}"))?;
crate::httpd::tighten_permissions(&dst);
Ok(Attachment { image: is_image_name(&name), name, path: dst.display().to_string(), bytes: meta.len(), on_box: String::new(), id: new_id(), seen: vec![] })
Ok(Attachment { image: is_image_name(&name), name, path: dst.display().to_string(), bytes: meta.len(), on_box: String::new(), id: new_id(), sha: sha12_file(&dst), seen: vec![] })
}
/// A picture or file the bot hands over (a screen it shows, a file fetched from another computer).
@ -266,7 +280,7 @@ pub fn store_bytes(bot_id: u64, name: &str, bytes: &[u8]) -> Result<Attachment,
crate::httpd::ensure_private_dir(&dir);
let dst = dir.join(format!("{}-{safe_name}", chrono::Local::now().format("%Y%m%d%H%M%S%3f")));
crate::httpd::write_private(&dst, bytes).map_err(|e| format!("存不下來:{e}"))?;
Ok(Attachment { image: is_image_name(&safe_name), name: safe_name, path: dst.display().to_string(), bytes: bytes.len() as u64, on_box: String::new(), id: new_id(), seen: vec![] })
Ok(Attachment { image: is_image_name(&safe_name), name: safe_name, path: dst.display().to_string(), bytes: bytes.len() as u64, on_box: String::new(), id: new_id(), sha: sha12(bytes), seen: vec![] })
}
/// The picture a paste brought (⌘V with a picture and no text on the clipboard: the window is
@ -287,7 +301,7 @@ pub fn paste_image(bot_id: u64) -> Option<Attachment> {
if !cfg!(target_os = "macos") {
let png = crate::platform::clipboard_png_linux()?;
std::fs::write(&dst, &png).ok()?;
return Some(Attachment { name, path: dst.display().to_string(), image: true, bytes: png.len() as u64, ..Default::default() });
return Some(Attachment { name, path: dst.display().to_string(), image: true, bytes: png.len() as u64, sha: sha12(&png), ..Default::default() });
}
let script = r#"ObjC.import('AppKit');
function run(argv) {
@ -302,7 +316,7 @@ function run(argv) {
return None;
}
let bytes = std::fs::metadata(&dst).map(|m| m.len()).unwrap_or(0);
Some(Attachment { name, path: dst.display().to_string(), image: true, bytes, ..Default::default() })
Some(Attachment { name, path: dst.display().to_string(), image: true, bytes, sha: sha12_file(&dst), ..Default::default() })
}
/// The Finder file picker (several files at once).
@ -413,6 +427,15 @@ pub fn typed_part(text: &str) -> &str {
mod tests {
use super::*;
#[test]
fn a_stored_file_carries_its_size_and_hash() {
// 產物核對(規格 §產物驗證):AnyBot 存的副本知道自己位元組與 SHA-256 前 12 碼,
// 下載後可用 shasum -a 256 核對(sha256("hi") 的前 12 碼 = 8f434346648f)。
let a = store_bytes(987654, "備忘.txt", b"hi").unwrap();
assert_eq!((a.bytes, a.sha.as_str()), (2, "8f434346648f"));
std::fs::remove_dir_all(dir_for(987654)).ok();
}
#[test]
fn the_words_typed_come_back_from_the_models_copy() {
let a = |name: &str| Attachment { name: name.into(), path: format!("/nowhere/{name}"), image: name.ends_with(".png"), bytes: 3, ..Default::default() };

View File

@ -486,6 +486,9 @@ pub fn message_json(m: &crate::model::Message, names: &HashMap<u64, String>, pag
/// and how its delivery stands (交付確認: the ✓/✓✓ under it and the sheet behind them).
pub fn attachment_json(a: &crate::attach::Attachment, by_bot: bool) -> Value {
let mut v = json!({"name": a.name, "image": a.image, "path": a.path, "bytes": a.bytes, "kind": crate::preview::kind_label(&a.name)});
if !a.sha.is_empty() {
v["sha"] = json!(a.sha);
}
if by_bot && !a.id.is_empty() {
use crate::attach::Delivery;
v["id"] = json!(a.id);