From dd3ded5a24e6a9bcee7730921eb3a1aca70cce18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Fri, 20 Nov 2026 16:46:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A2=E7=89=A9=E9=A9=97=E8=AD=89=EF=BC=88?= =?UTF-8?q?=E8=A6=8F=E6=A0=BC=20=C2=A7=E7=94=A2=E7=89=A9=E9=A9=97=E8=AD=89?= =?UTF-8?q?=20P2=EF=BC=9A=E5=A4=A7=E5=B0=8F=E3=80=81hash=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E6=AF=8F=E5=80=8B=E9=99=84=E4=BB=B6=E5=AD=98=E9=80=B2=20AnyBot?= =?UTF-8?q?=20=E6=99=82=E5=B0=B1=E8=A8=98=20SHA-256=20=E5=89=8D=2012=20?= =?UTF-8?q?=E7=A2=BC=EF=BC=88=E5=A4=A7=E5=B0=8F=E6=9C=AC=E4=BE=86=E5=B0=B1?= =?UTF-8?q?=E6=9C=89=EF=BC=89=E2=80=94=E2=80=94=E6=89=8B=E6=A9=9F=E9=A0=90?= =?UTF-8?q?=E8=A6=BD=E9=A0=81=E5=BA=95=E4=B8=8B=E3=80=8C=E6=AA=94=E6=A1=88?= =?UTF-8?q?=E6=A0=B8=E5=B0=8D=E3=80=8D=E3=80=81=E9=9B=BB=E8=85=A6=E6=AA=94?= =?UTF-8?q?=E6=A1=88=E5=8D=A1=E5=89=AF=E6=A8=99=E3=80=81=E9=99=84=E4=BB=B6?= =?UTF-8?q?=20API=E3=80=81Bot=20=E6=94=B6=E5=88=B0=20send=5Ffile=20?= =?UTF-8?q?=E7=B5=90=E6=9E=9C=E9=83=BD=E7=9C=8B=E5=BE=97=E5=88=B0=EF=BC=9B?= =?UTF-8?q?=E4=B8=8B=E8=BC=89=E5=BE=8C=20shasum=20-a=20256=20=E5=B0=8D?= =?UTF-8?q?=E5=89=8D=2012=20=E7=A2=BC=E5=B0=B1=E7=9F=A5=E9=81=93=E6=AA=94?= =?UTF-8?q?=E6=A1=88=E5=AE=8C=E6=95=B4=E3=80=81=E6=B2=92=E8=A2=AB=E5=8B=95?= =?UTF-8?q?=E9=81=8E=E3=80=82=E9=9B=B6=E6=96=B0=E8=A8=8A=E8=99=9F=E7=B3=BB?= =?UTF-8?q?=E7=B5=B1=EF=BC=9Asha=20=E5=9C=A8=E5=84=B2=E5=AD=98=E6=99=82?= =?UTF-8?q?=E7=AE=97=EF=BC=8C=E4=B9=8B=E5=BE=8C=E5=8F=AA=E8=AE=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已驗證: 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) --- FEATURES.md | 2 +- docs/feature-map/deliver.md | 36 ++++++++++++++++++------------------ mobile/index.html | 7 +++++++ src/agent/steps.rs | 2 +- src/app/files_ui.rs | 4 +++- src/attach.rs | 31 +++++++++++++++++++++++++++---- src/remote.rs | 3 +++ 7 files changed, 60 insertions(+), 25 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index 3ce37c1..30a7bab 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -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:表格,有列號和標題列,欄寬跟內容走,太長的格子滑過看全文,多個工作表可以切換。 diff --git a/docs/feature-map/deliver.md b/docs/feature-map/deliver.md index 96e1c70..2b5bff0 100644 --- a/docs/feature-map/deliver.md +++ b/docs/feature-map/deliver.md @@ -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 照樣成功),不能拿來證明「不能讀」。 + diff --git a/mobile/index.html b/mobile/index.html index bc024d9..bb39f23 100644 --- a/mobile/index.html +++ b/mobile/index.html @@ -727,6 +727,7 @@ button,.row,.ib,.island,.opt,.chip,.tab,label.sw,[role="button"]{-webkit-user-se
載入中…
+ @@ -3559,6 +3560,7 @@ async function openFile(path, name, att) { S.file = f; $("file-name").textContent = f.name; $("filebody").innerHTML = `
載入中…
`; + $("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); diff --git a/src/agent/steps.rs b/src/agent/steps.rs index db92414..d870895 100644 --- a/src/agent/steps.rs +++ b/src/agent/steps.rs @@ -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) } diff --git a/src/app/files_ui.rs b/src/app/files_ui.rs index fbebd1b..97be2aa 100644 --- a/src/app/files_ui.rs +++ b/src/app/files_ui.rs @@ -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() { diff --git a/src/attach.rs b/src/attach.rs index 6b980b0..14cca4c 100644 --- a/src/attach.rs +++ b/src/attach.rs @@ -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 { 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 { 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 Option { 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() }; diff --git a/src/remote.rs b/src/remote.rs index 9e8bb43..3954d52 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -486,6 +486,9 @@ pub fn message_json(m: &crate::model::Message, names: &HashMap, 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);