diff --git a/README.md b/README.md index 72471612..05b4d06e 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ The MiniMax Code Agent communicates through the host MCP client. Business reques | MiniApp | What it does | Author | | --- | --- | --- | +| [DeepSeek Chat Migration](plugins/danielwalnut/deepseek-chat-migration/) | Preview official DeepSeek web exports locally, restore branches and attachments, and import selected conversations as native MiniMax Code sessions with a bundled importer | [DanielWalnut](https://github.com/DanielWalnut) | | [OpenFilm](plugins/cristinamadureira353-svg/openfilm/) | Run OpenFilm inside MiniMax Code with the desktop interface: Studio editor, chat column and local agents, with a web-Studio fallback | [最爱双皮奶](https://github.com/cristinamadureira353-svg) | | [Dynamic Workflow Console](plugins/stevenjj33/workflow-console/) | Run and supervise MiniMax Code dynamic workflows from one page: live status, a stage-and-dependency flow graph, per-node output, approval and control, templates, and report export | [stevenjj33](https://github.com/stevenjj33) | | [MiniMax Photo](plugins/webguhui/minimax-photo/) | Layered image editing with an in-app AI assistant, local recovery and programmatic editing tools | [WebGuHui](https://github.com/WebGuHui) | diff --git a/README.zh-CN.md b/README.zh-CN.md index 911e8dcd..9b3aa93b 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -57,6 +57,7 @@ MiniMax Code Agent 通过宿主 MCP 客户端与 MiniApp 协作。业务请求 | MiniApp | 功能 | 作者 | | --- | --- | --- | +| [DeepSeek 会话迁移](plugins/danielwalnut/deepseek-chat-migration/README_ZH.md) | 在本机预览 DeepSeek 网页版官方导出包,恢复分支和附件,并用自带的导入脚本把选中对话导入成 MiniMax Code 原生会话 | [DanielWalnut](https://github.com/DanielWalnut) | | [OpenFilm](plugins/cristinamadureira353-svg/openfilm/) | 把 OpenFilm 装进 MiniMax Code:桌面版界面(Studio 编辑器 + 聊天栏 + 本地 agent),桌面模式不可用时回落网页版 | [最爱双皮奶](https://github.com/cristinamadureira353-svg) | | [动态工作流控制台](plugins/stevenjj33/workflow-console/README.zh-CN.md) | 在一个页面里运行并盯住 MiniMax Code 动态工作流:实时状态、按阶段与依赖画出的流程图、节点输出、审核与控制、模板与报告导出 | [stevenjj33](https://github.com/stevenjj33) | | [MiniMax Photo](plugins/webguhui/minimax-photo/) | 图层图片编辑、应用内 AI 助手、本地恢复与程序化编辑工具 | [WebGuHui](https://github.com/WebGuHui) | diff --git a/plugins/danielwalnut/deepseek-chat-migration/.minimax-plugin/plugin.json b/plugins/danielwalnut/deepseek-chat-migration/.minimax-plugin/plugin.json new file mode 100644 index 00000000..ff509ac3 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/.minimax-plugin/plugin.json @@ -0,0 +1,18 @@ +{ + "schemaVersion": 1, + "name": "deepseek-chat-migration", + "displayName": "DeepSeek Chat Migration", + "version": "0.5.0", + "description": "Preview official DeepSeek chat exports and import selected conversations as native MiniMax Code sessions with a bundled importer; no other plugin needed.", + "author": "DanielWalnut", + "icon": "icon.png", + "category": "Productivity", + "exampleQueries": [ + "Open DeepSeek Chat Migration", + "导入我的 DeepSeek 对话为 MCode 会话", + "Import selected DeepSeek conversations as MCode sessions" + ], + "apps": [], + "mcpServers": [], + "skills": [] +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/LICENSE b/plugins/danielwalnut/deepseek-chat-migration/LICENSE new file mode 100644 index 00000000..0f6b094b --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 DanielWalnut + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/plugins/danielwalnut/deepseek-chat-migration/README.md b/plugins/danielwalnut/deepseek-chat-migration/README.md new file mode 100644 index 00000000..bad87991 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/README.md @@ -0,0 +1,152 @@ +# DeepSeek Chat Migration + +English | [简体中文](README_ZH.md) + +DeepSeek Chat Migration previews the official DeepSeek web-app export, restores regenerated +branches, reasoning, sources and attachments, then imports selected conversations as native +MiniMax Code sessions with its own bundled importer. No other plugin is needed. + +## Export from DeepSeek + +1. Open `chat.deepseek.com`. +2. Go to **Settings → Data → Export** and download the official ZIP. +3. Keep the ZIP unchanged. A current export contains `manifest.json` with + `export_format_version: 3`, `user.json`, `chat_session_index.json`, + `chat_sessions/.json`, and optional files under `chat_sessions/_files/`. + +The app never logs in to DeepSeek, reads browser sessions or scrapes the website. + +## Install and open + +1. Download this `deepseek-chat-migration` directory. Python 3.9 or later must be available + as `python3` (`python` on Windows); the importer uses only the standard library. +2. In MiniMax Code, ask the Agent to install this Mini App directory. +3. Open **DeepSeek Chat Migration**, then choose the ZIP downloaded from DeepSeek. +4. Review the restored conversations and select those to migrate. The **全选** checkbox in + the toolbar selects or clears every conversation not yet imported. +5. Sessions are always imported into `~/.minimax` for the `mavis` agent and grouped under a + `DeepSeek` project (`~/DeepSeek`, created empty if missing); there is nothing to configure. +6. Choose **Import as MCode sessions**. While the plan is generated the button shows + 「生成预览中…」. Review the plan, then choose **Confirm import**. +7. While importing, the dialog and the footer show a progress bar and 「导入中 12/50」; each row + flips to imported as soon as its batch is written. The result lists every selected session as + imported, already imported (skipped) or failed. +8. Choose **打开 DeepSeek 项目** to go straight to what you imported. The button sits in the + page header at all times, even on a fresh page before any upload, and shows how many DeepSeek + sessions MiniMax Code already has; the result dialog has the same button. On MiniMax Code + 3.1.11 or later the Host opens the most recent imported conversation, which sits in the + `DeepSeek` project of the sidebar. With nothing imported yet, on older clients, or if the Host + cannot open it, the Mini App opens the `~/DeepSeek` folder (creating it if needed) in Finder, + Explorer or the Linux file manager instead. +9. Changed your mind? Right after an import, **撤销本次导入** in the result dialog removes exactly + the sessions that run added, after a confirmation. If any of them was used since (say, you + continued the chat), nothing is removed and the dialog asks again before **仍然撤销**. + +### What an imported session contains + +- The title becomes `[From DeepSeek] `; creation, update and message times are kept. +- Only the branch DeepSeek last showed (root to `current_message_id`) is displayed and becomes the + model's continuation history. Regenerated or edited-away branches are kept in a per-session + source archive next to the history, not in the conversation. +- `THINK` reasoning is shown in the transcript but never sent back to the model. +- Web searches become read-only historical tool records with queries and results. +- Uploaded files are kept as references (name, size, path in the export); bytes are not copied. +- Replies that timed out or were cut off carry a short historical error note. + +An import no longer copies `runtime-state.sqlite` first (on a 3.5 GB database that copy was most +of the wait and needed as much free disk again). Instead each run keeps a small undo journal, +`~/.minimax/deepseek-migration/<run>/undo.json`, listing the rows it inserted, the before-image of +the one existing row it updates (the `DeepSeek` project's visibility and restore marker) and the +files and folders it created. Sessions are written in batches of 10, each in one transaction +together with its journal entry, with a read-back check after every commit. A session that cannot be imported is reported on its own and +never blocks the others. Target sessions are +keyed by a hash of the DeepSeek account id plus the session id, so importing the same export +again (even after re-downloading it) skips what is already there. + +Conversation order and titles come from `chat_session_index.json` when present. Messages are +restored from `parent_id` and `current_message_id`; regenerated answers remain visible as alternate +branches. `THINK` fragments render as collapsible reasoning, and search fragments render as +collapsible sources. Already imported conversations are matched by source ID plus a stable content +hash and skipped when the same export is opened again. + +The Markdown archive button remains available as a secondary backup. Native-session import is the +primary workflow. + +## Tested environment + +- MiniMax Code 3.1.11 on Windows 11 +- Node.js 22 parser and unit tests on Linux + +The Mini App requires MiniMax Code 3.1.2 or later because it declares a pop-out window. Opening +the imported conversation from the Mini App needs MiniMax Code 3.1.11 or later; older clients get +the `~/DeepSeek` folder in the file manager instead. + +## Data & access + +- **DeepSeek files read:** only the official DeepSeek ZIP or a compatible DeepSeek JSON the user + explicitly picks in the page. Parsing happens locally. The app does not inspect browser sessions, + scrape `chat.deepseek.com` or upload the export. +- **MiniMax Code files read and written:** the bundled importer works directly on MiniMax Code's + own storage under `~/.minimax` (fixed path, not configurable). There is no supported API for + this, so it relies on an unspecified, version-dependent layout: + - `v2/sqlite/runtime-state.sqlite`. Opening the page runs one read-only query (SQLite + `mode=ro`) for the number of sessions in the `DeepSeek` project and the latest one. **Preview** + reads it read-only too. Only **Confirm import** writes: it inserts rows into + `local_runtime_sessions`, `local_runtime_message_rows`, `local_runtime_pi_history_file_migrations`, + `local_runtime_session_fts_keys` and `local_runtime_sessions_fts`, inserts the `~/DeepSeek` row + into `local_runtime_projects` if it is missing, and otherwise updates only that project row + (`hidden` and its sidebar-restore marker). + - `v2/sessions/<date>/<time>-session_<id>/`: one new directory per imported session with + `messages.jsonl`, `manifest.json`, `deepseek-migration.json` (provenance) and + `deepseek-source.json` (the full source conversation, including other branches). Existing + session directories are never modified. + - `deepseek-migration/<run>/`: the undo journal and batch reports; `zcode-migration/writer.lock`: + the write lock shared with the MiniMax Code migration plugin. + - **撤销本次导入** deletes exactly the rows and directories its journal lists and restores the + project row's before-image if that row is still as the import left it; it refuses when any of those sessions changed since the import, + unless the user confirms a second time. +- **Mini App state:** the selected ZIP is streamed to a temporary directory under the Host-owned, + opaque `context.dataDir`; it is replaced on the next upload and removed when the runtime stops. + Successful import hashes and the undoable runs are written atomically to + `context.dataDir/state.json`. +- **Process spawned:** `python3 miniapp/python/import_deepseek.py` (bundled with this Mini App), + first with `plan` and then, after confirmation, with `apply` on the same report. Arguments are + passed as an array without a shell: `--source <temporary ZIP>`, one `--session <id>` per + selection, `--report <temporary report>`, `--data-dir ~/.minimax`, `--agent mavis`, + `--workspace ~/DeepSeek` and `--restore-projects` (re-shows the DeepSeek project if it was + removed from the sidebar), plus `--batch-size 10` for apply. Its progress lines on stderr drive + the progress bar. Each run's undo journal and batch reports go to + `~/.minimax/deepseek-migration/<run>/`; the write lock is shared with the MiniMax Code migration + plugin in `~/.minimax/zcode-migration/`. **撤销本次导入** runs the same script with + `undo --run <undo.json> --data-dir ~/.minimax` (plus `--force` after the second confirmation), + and the header button asks it (`project`) for the DeepSeek session count and the latest one. +- **Folder opener (fallback only):** when nothing is imported yet or the Host cannot open the + imported conversation, choosing **打开 DeepSeek 项目** creates `~/DeepSeek` if missing and runs + `open ~/DeepSeek` on macOS, `explorer.exe ~/DeepSeek` on Windows or `xdg-open ~/DeepSeek` on + Linux, with an argument array and no shell. +- **Agent and conversations:** the Mini App runs no Agent turns, reads no MiniMax Code + conversations, and does not use `message.append`. The bundled importer creates native sessions. +- **Network:** no direct network requests and no external hosts. +- **Host navigation:** **打开 DeepSeek 项目** calls `window.mcode.miniapp.host.navigation.open` + with `{ id: 'session', sessionId, source: 'local' }` for the latest session in the DeepSeek + project, only from that click. It does not declare `hostAccess` and never reads which page you are on. +- **Other capabilities:** apart from the disclosed processes, it calls no MCP or Connector tools + and uses no microphone, camera or notifications. + +## Prototype limits + +- The official DeepSeek version-3 ZIP layout is the primary supported format. Two older DeepSeek + JSON shapes remain accepted for compatibility. +- ZIP input supports STORE and DEFLATE entries, including entries that use data descriptors. +- Attachment metadata is preserved. Binaries present under `chat_sessions/<id>_files/` are included + when the selected conversation is exported; missing binaries remain references. +- Attachment bytes are not copied into MiniMax Code; imported sessions keep file references. +- The target is fixed to the `~/.minimax` data directory and the `mavis` agent. + +## Credits + +The importer in `miniapp/python/` is adapted from the MiniMax Code team's +`agent-session-migration` plugin (author: Insider): its storage core (lock, transaction, +read-back, history initialization) and checkpointed job, reduced to the DeepSeek source. Target +identities and marker files match that plugin's DeepSeek source, so sessions imported by either +tool are recognized by the other. diff --git a/plugins/danielwalnut/deepseek-chat-migration/README_ZH.md b/plugins/danielwalnut/deepseek-chat-migration/README_ZH.md new file mode 100644 index 00000000..475428c4 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/README_ZH.md @@ -0,0 +1,79 @@ +# DeepSeek 会话迁移 + +[English](README.md) | 简体中文 + +DeepSeek 会话迁移专门处理 DeepSeek 网页版的官方导出包。它会在本机还原标题、时间、分支、思考过程、搜索来源和附件,再用自带的导入脚本把选中的对话导入成 MiniMax Code 原生会话,不需要另装插件。 + +## 从 DeepSeek 导出 + +1. 打开 `chat.deepseek.com`。 +2. 进入「设置 → 数据 → 导出」,下载官方 ZIP。 +3. 不用解压,也不用改文件名,直接把 ZIP 交给应用即可。 + +当前官方包里有 `manifest.json`(`export_format_version: 3`)、`user.json`、 +`chat_session_index.json`、`chat_sessions/<id>.json`,附件则放在 +`chat_sessions/<id>_files/`。应用不会登录 DeepSeek,不读取浏览器会话,也不会抓取网页。 + +## 安装与使用 + +1. 下载整个 `deepseek-chat-migration` 目录。电脑上要有 Python 3.9 或更新版本(macOS/Linux 用 `python3`,Windows 用 `python`),导入脚本只用标准库。 +2. 在 MiniMax Code 里让 Agent 安装这个 Mini App。 +3. 打开 **DeepSeek Chat Migration**,选择刚才下载的 DeepSeek ZIP。 +4. 预览恢复结果,勾选要迁移的会话。列表顶部的「全选」复选框可以一次勾上或清空所有还没导入的对话。 +5. 会话固定导入到 `~/.minimax` 下的 `mavis` agent,统一归到侧栏的 `DeepSeek` 项目(对应 `~/DeepSeek`,不存在时自动建一个空文件夹),不需要额外配置。 +6. 点 **导入为 MCode 会话**。生成计划时按钮会显示「生成预览中…」,看完计划再点 **Confirm import**。 +7. 导入过程中,弹窗和底栏都会显示进度条和「导入中 12/50」这样的进度,每一批写完,对应的行马上变成 imported。结束后逐条标出已导入、之前导入过(跳过)或失败。 +8. 点 **打开 DeepSeek 项目** 直接去看导入的会话。这个按钮一直在页面顶部,刚打开页面、还没上传导出时也能用,旁边的数字是 MiniMax Code 里已有的 DeepSeek 会话数;导入结果对话框里也有同样的按钮。MiniMax Code 3.1.11 及以上会由宿主打开最近导入的那个会话,它就在侧栏的 `DeepSeek` 项目里。还没导入过、版本更旧或宿主打不开时,会改为在访达、资源管理器或 Linux 文件管理器里打开 `~/DeepSeek` 文件夹(没有就先建一个)。 +9. 导完反悔了?导入结果对话框里有 **撤销本次导入**,确认后只删掉这次导入新增的会话。如果其中有会话导入后又用过(比如接着聊了),这次什么都不删,并提示你,你再点 **仍然撤销** 才会继续。 + +### 导入后的会话里有什么 + +- 标题变成 `[From DeepSeek] 原标题`,会话和每条消息的时间都保留。 +- 只取 DeepSeek 最后显示的那条分支(从开头到 `current_message_id`)展示,也只有这条分支进入后续对话的上下文。重新生成或改写掉的分支不进对话,单独存在会话目录的来源存档里。 +- 深度思考(`THINK`)只在界面显示,不会再发给模型。 +- 联网搜索变成只读的「历史工具记录」,保留搜索词和结果。 +- 上传的文件只保留引用(文件名、大小、导出包里的路径),不复制文件本身。 +- 超时或中断的回答后面会附一条历史错误说明。 + +导入前不再整份复制 `runtime-state.sqlite`。数据库有 3.5 GB 时,这次复制几乎就是全部等待时间,还要再占同样大的磁盘空间。现在每次导入只记一份很小的撤销记录 `~/.minimax/deepseek-migration/<run>/undo.json`:这次插入了哪些行、唯一会改动的已有行(`DeepSeek` 项目的显示状态和恢复标记)改之前是什么样、新建了哪些文件和文件夹。会话每 10 个一批,每批和对应的撤销记录在同一个事务里写入,提交后马上回读核对。某个会话导入失败只影响它自己。目标会话按 DeepSeek 账号 ID(哈希后使用)和会话 ID 计算,同一份导出重新下载、重新上传后再导入,已经导入过的会直接跳过。 + +有索引文件时,列表顺序和标题以 `chat_session_index.json` 为准。对话分支按 +`parent_id` 和 `current_message_id` 还原,重新生成过的回答会保留成候选版本;`THINK` +会单独折叠显示,搜索内容会列成来源。应用用会话 ID 和稳定内容哈希识别已经导入过的记录,再次打开同一份导出不会生成副本。 + +Markdown 档案导出仍然保留,方便额外备份;主要入口是原生会话导入。 + +## Tested environment + +- MiniMax Code 3.1.11,Windows 11 +- Node.js 22,Linux(解析器与单元测试) + +应用声明了独立窗口,因此需要 MiniMax Code 3.1.2 或更高版本。从 Mini App 直接打开导入的会话需要 3.1.11 或更高版本,更旧的版本会改为在文件管理器里打开 `~/DeepSeek`。 + +## Data & access + +- **读取的 DeepSeek 文件:** 只读你亲自选择的 DeepSeek 官方 ZIP,或兼容的 DeepSeek JSON,全程在本机解析。不会读取浏览器登录状态,不会抓取 `chat.deepseek.com`,也不会上传导出包。 +- **读写的 MiniMax Code 文件:** 自带的导入脚本直接操作 MiniMax Code 自己在 `~/.minimax` 下的存储(路径固定,不能配置)。这件事没有官方接口,依赖的是没有公开约定、可能随版本变化的目录和表结构: + - `v2/sqlite/runtime-state.sqlite`。打开页面时只做一次只读查询(SQLite `mode=ro`),读出 `DeepSeek` 项目里的会话数和最近的那个会话;生成预览也是只读。只有点 **Confirm import** 才会写入:向 `local_runtime_sessions`、`local_runtime_message_rows`、`local_runtime_pi_history_file_migrations`、`local_runtime_session_fts_keys` 和 `local_runtime_sessions_fts` 插入新行;`local_runtime_projects` 里没有 `~/DeepSeek` 这一行就插入,有的话只改这一行(`hidden` 和侧栏恢复标记)。 + - `v2/sessions/<日期>/<时间>-session_<id>/`:每个导入的会话新建一个目录,里面有 `messages.jsonl`、`manifest.json`、`deepseek-migration.json`(来源记录)和 `deepseek-source.json`(完整的原始对话,包括其他分支)。已有的会话目录一概不动。 + - `deepseek-migration/<run>/` 放撤销记录和各批次报告;`zcode-migration/writer.lock` 是和 MiniMax Code 迁移插件共用的写锁。 + - **撤销本次导入** 只删除撤销记录里列出的行和目录,项目那一行如果还是导入后的样子,就恢复成导入前的样子;其中任何一个会话导入后有改动,就拒绝撤销,除非你再确认一次。 +- **Mini App 自己的状态:** 选中的 ZIP 会流式写入宿主管理的私有 `context.dataDir` 临时目录,再次上传时替换,运行时停止时删除。成功导入的哈希和可撤销的导入记录原子写入 `context.dataDir/state.json`。 +- **启动进程:** 先后运行 Mini App 自带的 `python3 miniapp/python/import_deepseek.py plan` 和 `apply`,两步用同一份报告。参数用数组传递,不经过 shell;包含临时 ZIP、所选会话 ID、临时报告、`--data-dir ~/.minimax`、`--agent mavis`、`--workspace ~/DeepSeek` 和 `--restore-projects`(DeepSeek 项目被从侧栏移除过时,导入会让它重新显示),`apply` 另加 `--batch-size 10`。进度条读的是脚本写到 stderr 的进度行。每次导入的撤销记录和各批次报告在 `~/.minimax/deepseek-migration/<run>/`,写锁和 MiniMax Code 迁移插件共用 `~/.minimax/zcode-migration/`。**撤销本次导入** 运行同一个脚本的 `undo --run <undo.json> --data-dir ~/.minimax`(第二次确认后加 `--force`);顶部按钮用 `project` 子命令读取 DeepSeek 会话数和最近的那个会话。 +- **打开文件夹(仅作兜底):** 还没导入过,或者宿主打不开导入的会话时,点 **打开 DeepSeek 项目** 会先确保 `~/DeepSeek` 存在,再在 macOS 上运行 `open ~/DeepSeek`,Windows 上运行 `explorer.exe ~/DeepSeek`,Linux 上运行 `xdg-open ~/DeepSeek`。参数用数组传递,不经过 shell。 +- **Agent 与现有对话:** Mini App 不运行 Agent 回合,不读取 MiniMax Code 历史会话,也不调用 `message.append`。原生会话由自带的导入脚本创建。 +- **网络:** 应用不直接联网,也不访问任何外部域名。 +- **宿主导航:** 只有你点 **打开 DeepSeek 项目** 时,才会用 `{ id: 'session', sessionId, source: 'local' }` 调用 `window.mcode.miniapp.host.navigation.open`,打开 DeepSeek 项目里最近的会话。应用没有声明 `hostAccess`,也不读取你当前在哪个页面。 +- **其他能力:** 除了上面列出的进程,不调用 MCP 或 Connector 工具,也不使用麦克风、摄像头或系统通知。 + +## 原型限制 + +- 主要支持 DeepSeek 官方 version 3 ZIP;另外保留两种旧 DeepSeek JSON 结构的兼容解析。 +- ZIP 支持 STORE、DEFLATE 和使用 data descriptor 的条目。 +- 附件在原始导出包中存在时会进入新档案;原包只有元数据时,应用只能保留引用。 +- 附件本身不会复制进 MiniMax Code,导入的会话只保留文件引用。 +- 导入目标固定为 `~/.minimax` 数据目录和 `mavis` agent。 + +## 致谢 + +`miniapp/python/` 里的导入脚本改编自 MiniMax Code 团队的 `agent-session-migration` 插件(作者 Insider):沿用它的存储核心(写锁、事务、读回校验、历史初始化)和可断点续跑的导入流程,只保留 DeepSeek 来源。目标会话 ID 和标记文件与该插件的 DeepSeek 来源一致,用哪个工具导入过,另一个都能认出来。 diff --git a/plugins/danielwalnut/deepseek-chat-migration/docs/schema-notes.md b/plugins/danielwalnut/deepseek-chat-migration/docs/schema-notes.md new file mode 100644 index 00000000..3e730067 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/docs/schema-notes.md @@ -0,0 +1,123 @@ +# DeepSeek export schema notes + +Research date: 2026-11-17. + +## Primary source + +The parser's version-3 path follows DeepSeek's open-source `deepseek-ai/chat-demo` implementation: + +- [`src/services/chat/history/format.ts`](https://github.com/deepseek-ai/chat-demo/blob/main/src/services/chat/history/format.ts) +- [`src/services/chat/history/schema.ts`](https://github.com/deepseek-ai/chat-demo/blob/main/src/services/chat/history/schema.ts) +- [`src/services/chat/history/README.md`](https://github.com/deepseek-ai/chat-demo/blob/main/src/services/chat/history/README.md) + +That source defines `manifest.json` with `export_format_version: 3`, +`chat_sessions/{session_id}.json`, `chat_session_index.json`, and optional image entries. +Session files contain an `id`/`parent_id` message tree and `current_message_id`. Message fragments +include `REQUEST`, `RESPONSE`, `THINK`, `TOOL_SEARCH`, `TOOL_OPEN`, `TOOL_FIND`, `READ_LINK`, +`FILE`, `TOOL_IMAGE_SEARCH`, notices, template responses, and partial-file markers. + +## Corroborating community sources + +- [`urumchi/deepseek-keeper`](https://github.com/urumchi/deepseek-keeper), especially + `src/dskeeper/parser.py`, confirms an older official `conversations.json` export with a + `mapping` tree, message `fragments`, ZIP/JSON inputs, and conversation-ID dedupe. +- [`Lumjiel/deepseek-mindtrace`](https://github.com/Lumjiel/deepseek-mindtrace) documents the + flat API-era `chat_session` + `chat_messages` shape and `message_id`/`parent_id` branches. +- [`niushuanan/dsh-chat-migration`](https://github.com/niushuanan/dsh-chat-migration) confirms + selective preview, duplicate skipping, original ordering, and preservation of exported thinking + as practical migration behavior. + +The app does not use the scraping or authenticated-fetch paths found in some community tools. + +## Native MiniMax Code session migration + +The Mini App runtime context documented by this repository exposes `context.dataDir`, but that is +the plugin's private state directory and is not the MiniMax Code target data directory. It exposes +no active Agent name. The migration therefore always targets the current user's `~/.minimax` +directory and the `mavis` agent; neither is user-configurable. + +The importer is bundled in `miniapp/python/` (listed under `artifacts.node`), so no other plugin is +needed. It is adapted from the MiniMax Code team's `agent-session-migration` plugin (author: +Insider): `deepseek_core.py` keeps that plugin's storage core, `import_deepseek.py` its +checkpointed job, and `deepseek_export.py` reads the export. This app streams the uploaded ZIP to a +temporary path under `context.dataDir`, runs `import_deepseek.py plan` into a fresh report, +displays it, and runs `apply` on the same report only after explicit confirmation. The command is +spawned directly as `python3` with an argument array and no shell. + +`apply` does not copy the database. Each run opens an undo journal at +`<data-dir>/deepseek-migration/<run>/undo.json` (format `deepseek-undo/1`) on its first write, then +commits batches of `--batch-size` sessions (the Mini App passes 10). A preview, and +each batch's re-check, share one open ZIP and one read snapshot of the target; the snapshot is +closed before the batch writes. stderr carries one JSON line per step: +`{"event":"start","total":N}` and, after every batch, +`{"event":"progress","done":n,"total":N,"sessions":[{"sourceId","status"}]}`. The Node runtime folds +these into `GET /api/migration/progress?planId=`, which the page polls every 500 ms, and records +rows in `state.json` as they land. + +### What one import writes, and how undo reverses it + +Per new session, inside the batch transaction: + +| Target | Change | Undo | +| --- | --- | --- | +| `local_runtime_projects` | INSERT the `~/DeepSeek` project only if it does not exist; otherwise UPDATE its `hidden` (to 0, `--restore-projects`) and `extra_data_json` (restore marker) | delete it if this run created it and it is empty; otherwise put back the journaled before-image of `hidden` and `extra_data_json`, unless something else changed them since | +| `local_runtime_sessions` | INSERT | DELETE | +| trigger `trg_local_runtime_project_session_insert_v5` | sets the session's `project_id`, updates the project's `latest_activity_at_ms` and `session_count` | the delete trigger recomputes both | +| `local_runtime_session_fts_keys`, `local_runtime_sessions_fts` | INSERT one key row and one FTS row | DELETE by the journaled rowids | +| `local_runtime_message_rows` | INSERT the history rows | DELETE by the journaled ids | +| `local_runtime_pi_history_file_migrations` | INSERT | DELETE | +| `v2/sessions/<dated dir>/` | `messages.jsonl`, `manifest.json`, `deepseek-source.json`, `deepseek-migration.json` (marker, with `undoRun`), plus any parent folders it had to create | removed when only these files remain; anything else is moved to `deepseek-migration/<run>/undone/` | + +No other existing row is updated. `sqlite_sequence` counters are left advanced. Before each batch +commits, its journal entries are `pending`; after the read-back they become `committed` with the +message-row and FTS rowids and a fingerprint (record hash, `updated_at_ms`, title, archive state, +message-row count and last id, history hash, file list). A failed batch marks its entries +`rolled-back`. + +`import_deepseek.py undo --report <job.json>` (or `--run <undo.json>`) takes the same write lock, +compares every session with its fingerprint, and refuses with exit code 3 and the changed fields +if any was used since the import, unless `--force`. It deletes in one transaction, reads back that +no row is left, cleans up files, then marks the journal `undone` and the report `undone`, so a +stale report cannot be applied again. Running it twice reports everything as already absent. A +session re-imported by a later run carries that run's `undoRun` and is left alone. + +### Opening the DeepSeek project + +`docs/client.md` (MiniMax Code 3.1.11+) lists `host.navigation.open` targets `home`, `plugins`, +`schedule`, `settings` (with a section) and `{ id: 'session', sessionId, source: 'local' }`. There +is no project or workspace target and no session-list filter. So **打开 DeepSeek 项目**, in the +header at all times and in the result dialog, opens the most recent session of the DeepSeek +project from the click handler. `GET /api/project` finds it with `import_deepseek.py project` +(a read-only query on the `mavis` agent and the `~/DeepSeek` workspace), falling back to the +latest row in `state.json`. With nothing imported, without that API, or when it rejects (other +than `result_unknown`), the page calls `POST /api/workspace/open`, which creates `~/DeepSeek` if +needed and opens it with `open`, `explorer.exe` or `xdg-open`. + +Report entries are `{sourceId, status, error?, plan}` with status `ready`, `already-imported`, +`imported`, `incompatible`, `failed` or `running`; the UI shows them as ready, skipped, imported +or failed, and reads the target session id from `plan.targetId`. + +The target is the runtime's `v2/sqlite/runtime-state.sqlite` (columnar version 3, session data +version 4) plus `v2/sessions/<dated dir>/messages.jsonl`. The target agent must already have at +least one native session. Sessions without a working directory are attached to the `~/DeepSeek` +workspace project. + +## Observed production export + +An export inspected on 2026-11-17 contained 177 sessions, 326 user turns and seven sessions with +branches. It included request, response, thinking, search and file fragments. ZIP local headers +used the data-descriptor flag (`0x08`), so the reader takes compressed sizes and offsets from the +central directory. No user data from that export is included in this repository. + +## Unconfirmed or intentionally unsupported + +- Whether every production account has moved from `conversations.json` to format version 3. +- Whether production ZIPs ever use data descriptors, ZIP64, encryption, or compression methods + beyond STORE and DEFLATE. The official implementation currently writes STORE. +- Semantics of unknown future fragment types. They are ignored rather than mixed into answers. +- Attachment availability outside version-3 image entries. Metadata is preserved; binaries found + under the official session `_files` path are copied into generated archives, while absent + binaries remain references. +- The exact meaning and stability of API-era `status`, `feedback`, token-usage, and file fields. +- Legacy `mapping` exports do not consistently expose an explicit role; the parser infers it from + `REQUEST` and `RESPONSE` fragments when needed. diff --git a/plugins/danielwalnut/deepseek-chat-migration/icon.png b/plugins/danielwalnut/deepseek-chat-migration/icon.png new file mode 100644 index 00000000..9b40adbf Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/icon.png differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/app.js b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/app.js new file mode 100644 index 00000000..f4b65386 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/app.js @@ -0,0 +1,446 @@ +import { readInput, buildArchive } from './archive.js'; + +const state = { + conversations: [], + imported: new Set(), + selected: new Set(), + active: null, + planId: null, + busy: false, + openTarget: null, + project: null, + lastRunId: null, +}; +const POLL_MS = 500; +const LABELS = { planned: 'ready', success: 'imported', skipped: 'already imported · skipped', failed: 'failed' }; +const $ = (selector) => document.querySelector(selector); +const esc = (value) => String(value ?? '').replace(/[&<>"']/g, (char) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]); + +async function api(path, options = {}) { + const response = await fetch(path, { ...options, headers: { 'content-type': 'application/json', ...(options.headers || {}) } }); + const body = await response.json(); + if (!response.ok) throw new Error(body.message || body.error || `Request failed (${response.status})`); + return body; +} + +async function loadState() { + try { + const saved = await api('/api/state'); + state.imported = new Set(saved.imported || []); + $('#import').disabled = false; + } catch (error) { + setStatus(error.message, true); + } +} + +async function openFile(file) { + setStatus('Parsing DeepSeek export locally…'); + const parsed = await readInput(file); + setStatus('Saving the DeepSeek ZIP to a private temporary path…'); + const upload = await fetch('/api/source', { + method: 'POST', + headers: { 'content-type': 'application/zip' }, + body: file, + }); + const uploadResult = await upload.json(); + if (!upload.ok) throw new Error(uploadResult.message || uploadResult.error || 'Could not stage the ZIP.'); + state.conversations = parsed; + state.selected = new Set(parsed.filter((item) => !state.imported.has(item.stableId)).map((item) => item.stableId)); + state.active = parsed[0]?.stableId || null; + $('#workspace').classList.remove('hidden'); + $('#count').textContent = `${parsed.length} conversation${parsed.length === 1 ? '' : 's'}`; + $('#meta').textContent = `${parsed.filter((item) => state.imported.has(item.stableId)).length} already imported`; + render(); + setStatus(`Parsed ${parsed.length} DeepSeek conversations. Choose what to migrate.`); +} + +function filtered() { + const query = $('#search').value.toLowerCase().trim(); + if (!query) return state.conversations; + return state.conversations.filter((conversation) => JSON.stringify(conversation).toLowerCase().includes(query)); +} + +function selectable() { + return state.conversations.filter((item) => !state.imported.has(item.stableId)); +} + +function renderSelectAll() { + const total = selectable().length; + const count = selectable().filter((item) => state.selected.has(item.stableId)).length; + const box = $('#select-all'); + box.checked = total > 0 && count === total; + box.indeterminate = count > 0 && count < total; + box.disabled = total === 0; + $('#select-label').textContent = count ? `已选 ${count}` : '全选'; +} + +function render({ preview = true } = {}) { + renderSelectAll(); + $('#list').innerHTML = filtered().map((conversation) => { + const imported = state.imported.has(conversation.stableId); + const date = conversation.updatedAt ? new Date(conversation.updatedAt).toLocaleDateString() : 'Unknown date'; + return `<label class="row ${state.active === conversation.stableId ? 'active' : ''}" data-id="${esc(conversation.stableId)}"> + <input type="checkbox" ${state.selected.has(conversation.stableId) ? 'checked' : ''} ${imported ? 'disabled' : ''}> + <span><b>${esc(conversation.title)} ${conversation.branchCount ? `<i class="pill">${conversation.branchCount} branches</i>` : ''} ${imported ? '<i class="pill imported">imported</i>' : ''}</b> + <small>${date} · ${conversation.turnCount} turns · ${conversation.approxChars.toLocaleString()} chars</small></span> + </label>`; + }).join(''); + for (const row of document.querySelectorAll('.row')) { + row.addEventListener('click', (event) => { + const id = row.dataset.id; + state.active = id; + if (event.target.matches('input')) { + if (event.target.checked) state.selected.add(id); else state.selected.delete(id); + } + render(); + }); + } + if (preview) renderPreview(); +} + +function renderPreview() { + const conversation = state.conversations.find((item) => item.stableId === state.active); + if (!conversation) return; + const byParent = new Map(); + for (const message of conversation.messages) { + const key = message.parentId ?? 'root'; + byParent.set(key, [...(byParent.get(key) || []), message]); + } + $('#preview').innerHTML = `<h2>${esc(conversation.title)}</h2><p class="eyebrow">${conversation.messages.length} MESSAGES · ${conversation.branchCount} ALTERNATE BRANCHES</p>` + + conversation.messages.map((message) => { + const siblings = byParent.get(message.parentId ?? 'root') || []; + const blocks = message.blocks.map(renderBlock).join(''); + return `<section class="message ${esc(message.role)}"><h3>${esc(message.role)} ${siblings.length > 1 ? `· VERSION ${siblings.indexOf(message) + 1}/${siblings.length}` : ''}</h3><div class="bubble">${blocks}</div></section>`; + }).join(''); +} + +function renderBlock(block) { + if (block.type === 'text') return `<div>${esc(block.text)}</div>`; + if (block.type === 'reasoning') return `<details><summary>Reasoning${block.elapsedSecs ? ` · ${block.elapsedSecs}s` : ''}</summary><div>${esc(block.text)}</div></details>`; + if (block.type === 'search') return `<details><summary>Sources · ${(block.results || []).length} results</summary>${(block.results || []).map((result) => `<p><b>${esc(result.title || result.url)}</b><br>${esc(result.snippet)}</p>`).join('')}</details>`; + if (block.type === 'attachment') return `<details><summary>Attachments · ${(block.files || []).length}</summary>${(block.files || []).map((file) => `<p>${esc(file.name || file.id)} · ${file.size || 0} bytes · ${file.available ? 'included in export' : 'reference only'}</p>`).join('')}</details>`; + return `<details><summary>${esc(block.label || 'Notice')}</summary>${esc(block.text)}</details>`; +} + +/** Spinner + a short label while a step runs; the original label comes back after. */ +function setBusy(button, busy, label) { + if (busy) { + button.dataset.label ??= button.textContent; + button.textContent = label; + } else if (button.dataset.label !== undefined) { + button.textContent = button.dataset.label; + delete button.dataset.label; + } + button.classList.toggle('busy', busy); + button.setAttribute('aria-busy', String(busy)); +} + +async function planImport() { + const selected = state.conversations.filter((item) => state.selected.has(item.stableId)); + if (!selected.length) return setStatus('Select at least one conversation to import.', true); + if (state.busy) return; + state.busy = true; + $('#import').disabled = true; + setBusy($('#import'), true, '生成预览中…'); + showOpenProject(false); + try { + setStatus(`生成预览中… · ${selected.length} 个会话`); + const result = await api('/api/migration/plan', { + method: 'POST', + body: JSON.stringify({ + sessions: selected.map((conversation) => ({ id: conversation.sourceId, stableId: conversation.stableId })), + }), + }); + state.planId = result.planId; + renderMigrationResult('Import plan', result); + renderProgress(null); + $('#confirm-import').disabled = !result.summary.planned; + $('#confirm-import').hidden = false; + showUndo(false); + $('#migration-result').showModal(); + setStatus(result.summary.planned + ? `${result.summary.planned} ready to import. Review the plan before importing.` + : 'Nothing new to import in this selection.', result.summary.failed > 0); + } catch (error) { + setStatus(error.message, true); + } finally { + state.busy = false; + setBusy($('#import'), false); + $('#import').disabled = false; + } +} + +function progressLabel(progress) { + if (progress.phase === 'importing' || progress.phase === 'done') return `导入中 ${progress.done}/${progress.total}`; + return '准备导入…'; +} + +/** Dialog bar + footer status while apply runs; null hides both bars. */ +function renderProgress(progress) { + for (const id of ['#progress', '#footer-progress']) $(id).hidden = !progress; + if (!progress) return; + const ratio = progress.total ? progress.done / progress.total : 0; + const waiting = progress.phase !== 'importing' && progress.phase !== 'done'; + for (const bar of document.querySelectorAll('.progress')) { + bar.classList.toggle('waiting', waiting); + bar.querySelector('i').style.width = waiting ? '' : `${Math.round(ratio * 100)}%`; + bar.setAttribute('aria-valuenow', String(Math.round(ratio * 100))); + } + const label = progressLabel(progress); + $('#progress-label').textContent = label; + setStatus(label); +} + +function markLanded(progress) { + for (const item of progress.sessions || []) { + const row = document.querySelector(`#result-list li[data-id="${CSS.escape(item.sessionId)}"]`); + if (!row || row.classList.contains(item.status)) continue; + row.className = item.status; + row.querySelector('span').textContent = LABELS[item.status] || item.status; + } + let changed = false; + for (const stableId of progress.imported || []) { + if (state.imported.has(stableId)) continue; + state.imported.add(stableId); + state.selected.delete(stableId); + changed = true; + } + if (changed) render({ preview: false }); +} + +async function applyImport() { + if (!state.planId || state.busy) return; + const planId = state.planId; + state.busy = true; + $('#confirm-import').disabled = true; + $('#import').disabled = true; + setBusy($('#confirm-import'), true, '导入中…'); + $('#result-title').textContent = 'Importing…'; + renderProgress({ phase: 'starting', done: 0, total: 0 }); + let polling = true; + const poll = async () => { + while (polling) { + try { + const progress = await api(`/api/migration/progress?planId=${encodeURIComponent(planId)}`); + if (polling) { + renderProgress(progress); + markLanded(progress); + } + } catch { + // Not started yet or already finished; the apply response is authoritative. + } + await new Promise((resolve) => setTimeout(resolve, POLL_MS)); + } + }; + const poller = poll(); + try { + const result = await api('/api/migration/apply', { + method: 'POST', + body: JSON.stringify({ planId }), + }); + polling = false; + await poller; + for (const stableId of result.imported || []) state.imported.add(stableId); + state.selected = new Set([...state.selected].filter((stableId) => !state.imported.has(stableId))); + state.planId = null; + render({ preview: false }); + renderMigrationResult('Import result', result); + renderProgress(null); + $('#confirm-import').hidden = true; + const landed = (result.sessions || []).find((item) => item.status === 'success' && item.targetSessionId) + || (result.sessions || []).find((item) => item.status === 'skipped' && item.targetSessionId); + state.openTarget = landed?.targetSessionId || null; + showOpenProject(Boolean(landed)); + state.lastRunId = result.runId || null; + showUndo(Boolean(state.lastRunId), result.summary.succeeded); + void refreshProject(); + const skipped = result.summary.skipped ? `, ${result.summary.skipped} already imported` : ''; + setStatus(result.summary.failed + ? `Imported ${result.summary.succeeded}${skipped}, ${result.summary.failed} failed.` + : `Imported ${result.summary.succeeded} MCode session${result.summary.succeeded === 1 ? '' : 's'}${skipped}.`, result.summary.failed > 0); + } catch (error) { + polling = false; + await poller; + renderProgress(null); + $('#confirm-import').disabled = false; + setStatus(error.message, true); + } finally { + state.busy = false; + setBusy($('#confirm-import'), false); + $('#import').disabled = false; + } +} + +function showUndo(visible, count = 0) { + $('#undo-import').hidden = !visible; + $('#undo-confirm').hidden = true; + $('#undo-import').dataset.count = String(count); +} + +function askUndo() { + const count = Number($('#undo-import').dataset.count || 0); + $('#undo-text').textContent = `会从 MiniMax Code 里删掉这次导入的 ${count} 个会话,其他会话不受影响。确定撤销吗?`; + $('#undo-force').hidden = true; + $('#undo-yes').hidden = false; + $('#undo-confirm').hidden = false; + $('#undo-import').hidden = true; +} + +async function undoImport(force = false) { + if (!state.lastRunId || state.busy) return; + state.busy = true; + const button = force ? $('#undo-force') : $('#undo-yes'); + setBusy(button, true, '撤销中…'); + button.disabled = true; + try { + const response = await fetch('/api/migration/undo', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ runId: state.lastRunId, force }), + }); + const body = await response.json(); + if (response.status === 409 && body.error === 'undo_refused') { + $('#undo-text').textContent = `${body.message}仍然撤销会把这些会话导入后的新内容一起删掉(新增的文件会另存在撤销记录里)。`; + $('#undo-yes').hidden = true; + $('#undo-force').hidden = false; + return; + } + if (!response.ok) throw new Error(body.message || body.error || `Request failed (${response.status})`); + for (const stableId of body.undone || []) state.imported.delete(stableId); + state.lastRunId = null; + state.openTarget = null; + render({ preview: false }); + $('#undo-confirm').hidden = true; + showOpenProject(false); + $('#result-title').textContent = 'Import undone'; + $('#result-summary').textContent = `已撤销本次导入:删除了 ${body.removed} 个会话`; + for (const row of document.querySelectorAll('#result-list li.success')) { + row.className = 'undone'; + row.querySelector('span').textContent = 'undone'; + } + setStatus(`已撤销本次导入(${body.removed} 个会话)。`); + void refreshProject(); + } catch (error) { + setStatus(error.message, true); + $('#undo-text').textContent = error.message; + } finally { + setBusy(button, false); + button.disabled = false; + state.busy = false; + } +} + +function showOpenProject(visible) { + for (const id of ['#open-project', '#footer-open']) $(id).hidden = !visible; +} + +async function openWorkspaceFolder() { + try { + const result = await api('/api/workspace/open', { method: 'POST' }); + setStatus(`已在文件管理器中打开 ${result.path}`); + } catch (error) { + setStatus(error.message, true); + } +} + +/** + * Opens an imported conversation, which lives in the DeepSeek project, through the + * Host (MiniMax Code 3.1.11+). Must run straight from the click: the Host checks the + * user gesture. Older clients, or a Host that cannot open it, get ~/DeepSeek in the + * file manager instead. + */ +async function refreshProject() { + try { + state.project = await api('/api/project'); + } catch { + state.project = null; + } + const count = state.project?.sessionCount || 0; + const hostCanOpen = typeof window.mcode?.miniapp?.host?.navigation?.open === 'function'; + $('#header-count').textContent = count ? String(count) : ''; + $('#header-count').hidden = !count; + $('#header-open').title = count && hostCanOpen + ? `在 MiniMax Code 里打开 DeepSeek 项目中最近的会话(共 ${count} 个)` + : `在文件管理器里打开 ${state.project?.workspace || '~/DeepSeek'}`; +} + +/** + * Opens the DeepSeek project, from the header at any time or after an import: the + * Host (MiniMax Code 3.1.11+) opens the latest imported session, which lives in that + * project. Must run straight from the click: the Host checks the user gesture. With + * nothing imported, an older client, or a Host that cannot open it, ~/DeepSeek opens + * in the file manager instead. + */ +function openProject() { + const host = window.mcode?.miniapp?.host; + const sessionId = state.openTarget || state.project?.latestSessionId || null; + if (typeof host?.navigation?.open !== 'function' || !sessionId) return void openWorkspaceFolder(); + host.navigation.open({ id: 'session', sessionId, source: 'local' }).then((result) => { + if (result?.status === 'opened') { + if ($('#migration-result').open) $('#migration-result').close(); + setStatus('已打开 DeepSeek 项目里导入的会话。'); + } + }, (error) => { + // result_unknown: the Host may still open it; do not open a second place. + if (error?.code === 'result_unknown') return setStatus('MiniMax Code 没有回应打开请求,请在侧栏的 DeepSeek 项目里查看。', true); + return openWorkspaceFolder(); + }); +} + +function renderMigrationResult(title, result) { + $('#result-title').textContent = title; + const { total, planned, succeeded, skipped, failed } = result.summary; + const parts = [`${total} total`]; + if (planned) parts.push(`${planned} ready`); + if (succeeded) parts.push(`${succeeded} imported`); + if (skipped) parts.push(`${skipped} already imported`); + if (failed) parts.push(`${failed} failed`); + $('#result-summary').textContent = parts.join(' · '); + $('#result-list').innerHTML = (result.sessions || []).map((item) => { + const conversation = state.conversations.find((entry) => entry.sourceId === item.sessionId); + return `<li class="${esc(item.status)}" data-id="${esc(item.sessionId)}"><b>${esc(conversation?.title || item.sessionId)}</b><span>${esc(LABELS[item.status] || item.status)}${item.message ? ` · ${esc(item.message)}` : ''}</span></li>`; + }).join(''); + $('#result-stderr').textContent = result.error || ''; + $('#result-stderr').hidden = !result.error; +} + +function exportArchive() { + const chosen = state.conversations.filter((item) => state.selected.has(item.stableId)); + const conversations = chosen.length ? chosen : state.conversations; + if (!conversations.length) return; + const link = document.createElement('a'); + link.href = URL.createObjectURL(buildArchive(conversations)); + link.download = 'deepseek-chat-archive.zip'; + link.click(); + setTimeout(() => URL.revokeObjectURL(link.href), 1000); +} + +function setStatus(message, error = false) { + $('#status').textContent = message; + $('#status').style.color = error ? '#a64232' : ''; +} + +$('#file').addEventListener('change', (event) => event.target.files[0] && openFile(event.target.files[0]).catch((error) => setStatus(error.message, true))); +$('#search').addEventListener('input', render); +$('#select-all').addEventListener('change', (event) => { + state.selected = event.target.checked + ? new Set(selectable().map((item) => item.stableId)) + : new Set(); + render(); +}); +$('#import').addEventListener('click', planImport); +$('#archive').addEventListener('click', exportArchive); +$('#confirm-import').addEventListener('click', applyImport); +$('#open-project').addEventListener('click', openProject); +$('#footer-open').addEventListener('click', openProject); +$('#header-open').addEventListener('click', openProject); +$('#undo-import').addEventListener('click', askUndo); +$('#undo-yes').addEventListener('click', () => undoImport(false)); +$('#undo-force').addEventListener('click', () => undoImport(true)); +$('#undo-no').addEventListener('click', () => showUndo(true, Number($('#undo-import').dataset.count || 0))); +for (const eventName of ['dragenter', 'dragover']) $('#drop').addEventListener(eventName, (event) => { event.preventDefault(); $('#drop').classList.add('drag'); }); +for (const eventName of ['dragleave', 'drop']) $('#drop').addEventListener(eventName, (event) => { event.preventDefault(); $('#drop').classList.remove('drag'); }); +$('#drop').addEventListener('drop', (event) => event.dataTransfer.files[0] && openFile(event.dataTransfer.files[0]).catch((error) => setStatus(error.message, true))); + +await loadState(); +await refreshProject(); diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/archive.js b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/archive.js new file mode 100644 index 00000000..2e710b04 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/archive.js @@ -0,0 +1,239 @@ +import { parseDeepSeekExport } from './deepseek.js'; + +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); + +function u16(view, offset) { return view.getUint16(offset, true); } +function u32(view, offset) { return view.getUint32(offset, true); } + +async function inflate(bytes) { + const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('deflate-raw')); + return new Uint8Array(await new Response(stream).arrayBuffer()); +} + +export async function readInput(file) { + const bytes = new Uint8Array(await file.arrayBuffer()); + if (file.name.toLowerCase().endsWith('.json')) return parseDeepSeekExport(JSON.parse(decoder.decode(bytes))); + if (!file.name.toLowerCase().endsWith('.zip')) throw new Error('Choose the ZIP or JSON exported by DeepSeek.'); + const entries = await readZip(bytes); + const byName = new Map(entries); + const manifest = byName.get('manifest.json'); + if (manifest) { + const version = JSON.parse(decoder.decode(manifest))?.export_format_version; + if (version !== 3) throw new Error(`Unsupported DeepSeek export format version: ${version ?? 'missing'}.`); + } + const sessionEntries = entries.filter(([name]) => /^chat_sessions\/[^/]+\.json$/.test(name)); + if (sessionEntries.length) { + const sessions = new Map(); + for (const [, content] of sessionEntries) { + for (const conversation of parseDeepSeekExport(JSON.parse(decoder.decode(content)))) { + sessions.set(conversation.sourceId, conversation); + } + } + const indexBytes = byName.get('chat_session_index.json'); + const indexValue = indexBytes ? JSON.parse(decoder.decode(indexBytes)) : []; + const index = Array.isArray(indexValue) ? indexValue : Array.isArray(indexValue?.sessions) ? indexValue.sessions : []; + const ordered = []; + for (const item of index) { + const conversation = sessions.get(String(item?.id ?? '')); + if (!conversation) continue; + if (typeof item.title === 'string' && item.title) conversation.title = item.title; + if (typeof item.inserted_at === 'string') conversation.createdAt = item.inserted_at; + if (typeof item.updated_at === 'string') conversation.updatedAt = item.updated_at; + ordered.push(conversation); + sessions.delete(conversation.sourceId); + } + ordered.push(...sessions.values()); + for (const conversation of ordered) attachFiles(conversation, byName); + return ordered; + } + const legacy = entries.find(([name]) => name === 'conversations.json'); + if (!legacy) throw new Error('No DeepSeek conversation JSON was found in this ZIP.'); + return parseDeepSeekExport(JSON.parse(decoder.decode(legacy[1]))); +} + +function attachFiles(conversation, entries) { + const prefix = `chat_sessions/${conversation.sourceId}_files/`; + const archived = []; + const used = new Set(); + for (const message of conversation.messages) { + for (const block of message.blocks) { + if (block.type !== 'attachment') continue; + for (const file of block.files) { + let sourcePath = file.sourcePath && entries.has(file.sourcePath) ? file.sourcePath : ''; + if (!sourcePath) { + sourcePath = [...entries.keys()].find((name) => + name.startsWith(prefix) && !used.has(name) + && ((file.id && name.includes(file.id)) || (file.name && name.endsWith(`_${file.name}`))), + ) || ''; + } + if (!sourcePath) continue; + const data = entries.get(sourcePath); + const archiveName = `${safeName(file.id || 'file')}-${safeName(file.name || sourcePath.split('/').at(-1))}`; + file.available = true; + file.archivePath = `attachments/${safeName(conversation.sourceId)}/${archiveName}`; + archived.push({ name: file.archivePath, data }); + used.add(sourcePath); + } + } + } + Object.defineProperty(conversation, 'archiveFiles', { + value: archived, + enumerable: false, + configurable: false, + writable: false, + }); +} + +export async function readZip(bytes) { + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + // Locate the end-of-central-directory record; sizes there are reliable even when + // local headers defer them to data descriptors (flag 0x08), as real exports do. + let eocd = -1; + for (let offset = bytes.length - 22; offset >= Math.max(0, bytes.length - 22 - 0xffff); offset -= 1) { + if (u32(view, offset) === 0x06054b50) { eocd = offset; break; } + } + if (eocd < 0) throw new Error('This file is not a valid ZIP archive.'); + const count = u16(view, eocd + 10); + let cursor = u32(view, eocd + 16); + const files = []; + for (let index = 0; index < count; index += 1) { + if (u32(view, cursor) !== 0x02014b50) throw new Error('Corrupt ZIP central directory.'); + const method = u16(view, cursor + 10); + const compressedSize = u32(view, cursor + 20); + const nameLength = u16(view, cursor + 28); + const extraLength = u16(view, cursor + 30); + const commentLength = u16(view, cursor + 32); + const localOffset = u32(view, cursor + 42); + const name = decoder.decode(bytes.subarray(cursor + 46, cursor + 46 + nameLength)); + cursor += 46 + nameLength + extraLength + commentLength; + if (name.endsWith('/')) continue; + if (u32(view, localOffset) !== 0x04034b50) throw new Error(`Corrupt ZIP entry ${name}.`); + const dataStart = localOffset + 30 + u16(view, localOffset + 26) + u16(view, localOffset + 28); + const compressed = bytes.subarray(dataStart, dataStart + compressedSize); + const content = method === 0 ? compressed : method === 8 ? await inflate(compressed) : null; + if (!content) throw new Error(`Unsupported ZIP compression method ${method}.`); + files.push([name.normalize('NFC'), content]); + } + return files; +} + +export function markdownFor(conversation) { + const lines = [ + `# ${conversation.title}`, '', + `- Imported: ${conversation.updatedAt || conversation.createdAt || 'unknown'}`, + `- Source ID: ${conversation.sourceId}`, '', + ]; + lines.push('## Transcript', ''); + const activeIds = new Set(activePath(conversation)); + for (const message of conversation.messages) { + const branch = activeIds.has(message.id) ? '' : ' · alternate branch'; + lines.push(`### ${message.role === 'user' ? 'User' : 'Assistant'}${branch}`, ''); + for (const block of message.blocks) { + if (block.type === 'text') lines.push(block.text, ''); + if (block.type === 'reasoning') lines.push('<details><summary>Reasoning</summary>', '', block.text, '', '</details>', ''); + if (block.type === 'search') { + lines.push('<details><summary>Sources</summary>', ''); + for (const result of block.results || []) lines.push(`- [${result.title || result.url}](${result.url || '#'}) — ${result.snippet || ''}`); + lines.push('', '</details>', ''); + } + if (block.type === 'attachment') { + lines.push('<details><summary>Attachment references</summary>', ''); + for (const file of block.files || []) { + const label = file.name || file.id; + const linked = file.available && file.archivePath ? `[${label}](../${file.archivePath})` : label; + lines.push(`- ${linked} (${file.size || 0} bytes; binary ${file.available ? 'included' : 'not available in the export'})`); + } + lines.push('', '</details>', ''); + } + if (block.type === 'notice') lines.push(`> ${block.label}: ${block.text}`, ''); + } + } + return lines.join('\n'); +} + +function activePath(conversation) { + const byId = new Map(conversation.messages.map((message) => [message.id, message])); + let current = byId.get(conversation.currentMessageId) || conversation.messages.at(-1); + const ids = []; + while (current) { + ids.push(current.id); + current = current.parentId == null ? null : byId.get(current.parentId); + } + return ids; +} + +function crc32(bytes) { + let crc = 0xffffffff; + for (const byte of bytes) { + crc ^= byte; + for (let bit = 0; bit < 8; bit += 1) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); + } + return (crc ^ 0xffffffff) >>> 0; +} + +function safeName(value) { + const clean = value.normalize('NFKC').replace(/[<>:"/\\|?*\u0000-\u001f]/g, '-').replace(/\s+/g, ' ').trim(); + return (clean || 'conversation').slice(0, 80); +} + +export function buildArchive(conversations) { + const conversationFiles = conversations.map((conversation, index) => ({ + name: `conversations/${String(index + 1).padStart(3, '0')}-${safeName(conversation.title)}-${conversation.sourceId.slice(0, 8)}.md`, + data: encoder.encode(markdownFor(conversation)), + })); + const index = [ + '# DeepSeek conversation archive', '', + 'Open `search.html` for local full-text search.', '', + ...conversationFiles.map((file, index) => `- [${conversations[index].title}](${file.name})`), + ].join('\n'); + const searchData = conversations.map((conversation, index) => ({ + title: conversation.title, file: conversationFiles[index].name, + text: markdownFor(conversation), + })); + const files = [{ name: 'index.md', data: encoder.encode(index) }, ...conversationFiles]; + const attachmentNames = new Set(); + for (const conversation of conversations) { + for (const attachment of conversation.archiveFiles || []) { + if (attachmentNames.has(attachment.name)) continue; + files.push(attachment); + attachmentNames.add(attachment.name); + } + } + files.push({ name: 'search.html', data: encoder.encode(searchPage(searchData)) }); + return zipStore(files); +} + +function searchPage(data) { + const safe = JSON.stringify(data).replace(/</g, '\\u003c'); + return `<!doctype html><meta charset="utf-8"><title>DeepSeek archive search

DeepSeek archive search

`; +} + +function zipStore(files) { + const chunks = []; + const central = []; + let offset = 0; + for (const file of files) { + const name = encoder.encode(file.name); + const crc = crc32(file.data); + const local = new Uint8Array(30 + name.length); + const view = new DataView(local.buffer); + view.setUint32(0, 0x04034b50, true); view.setUint16(4, 20, true); view.setUint16(6, 0x0800, true); + view.setUint32(14, crc, true); view.setUint32(18, file.data.length, true); view.setUint32(22, file.data.length, true); + view.setUint16(26, name.length, true); local.set(name, 30); + chunks.push(local, file.data); + const dir = new Uint8Array(46 + name.length); + const d = new DataView(dir.buffer); + d.setUint32(0, 0x02014b50, true); d.setUint16(4, 20, true); d.setUint16(6, 20, true); d.setUint16(8, 0x0800, true); + d.setUint32(16, crc, true); d.setUint32(20, file.data.length, true); d.setUint32(24, file.data.length, true); + d.setUint16(28, name.length, true); d.setUint32(42, offset, true); dir.set(name, 46); + central.push(dir); + offset += local.length + file.data.length; + } + const centralSize = central.reduce((sum, item) => sum + item.length, 0); + const end = new Uint8Array(22); + const e = new DataView(end.buffer); + e.setUint32(0, 0x06054b50, true); e.setUint16(8, files.length, true); e.setUint16(10, files.length, true); + e.setUint32(12, centralSize, true); e.setUint32(16, offset, true); + return new Blob([...chunks, ...central, end], { type: 'application/zip' }); +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/deepseek.js b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/deepseek.js new file mode 100644 index 00000000..6bef75ac --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/deepseek.js @@ -0,0 +1,170 @@ +const text = (value) => (typeof value === 'string' ? value : ''); +const numberTime = (value) => { + if (typeof value === 'number') return new Date(value < 1e12 ? value * 1000 : value).toISOString(); + return text(value); +}; + +export function stableHash(value) { + const source = typeof value === 'string' ? value : JSON.stringify(value); + let first = 0x811c9dc5; + let second = 0x9e3779b9; + for (let index = 0; index < source.length; index += 1) { + const code = source.charCodeAt(index); + first = Math.imul(first ^ code, 0x01000193); + second = Math.imul(second ^ code, 0x85ebca6b); + } + return `${(first >>> 0).toString(16).padStart(8, '0')}${(second >>> 0).toString(16).padStart(8, '0')}`; +} + +function fragmentBlock(fragment) { + const type = text(fragment?.type).toUpperCase(); + if (type === 'REQUEST' || type === 'RESPONSE') return { type: 'text', text: text(fragment.content) }; + if (type === 'THINK') return { type: 'reasoning', text: text(fragment.content), elapsedSecs: fragment.elapsed_secs ?? null }; + if (type === 'TOOL_SEARCH' || type === 'TOOL_IMAGE_SEARCH' || type === 'SEARCH') { + return { + type: 'search', + queries: Array.isArray(fragment.queries) ? fragment.queries.map(text) : [], + results: Array.isArray(fragment.results) ? fragment.results.map((item) => ({ + title: text(item?.title), + url: text(item?.url || item?.source_page_url), + snippet: text(item?.snippet), + })) : [], + }; + } + if (type === 'TOOL_OPEN' || type === 'READ_LINK') return { type: 'search', queries: [], results: fragment.results ?? [fragment] }; + if (type === 'FILE') { + return { type: 'attachment', files: Array.isArray(fragment.files) ? fragment.files.map((file) => ({ + id: text(file?.id), name: text(file?.file_name || file?.name), size: Number(file?.file_size || 0), + sourcePath: text(file?.export_path), + available: false, + })) : [] }; + } + if (fragment?.content != null) return { type: 'notice', label: type || 'UNKNOWN', text: text(fragment.content) }; + return null; +} + +function normalizeMessage(raw, fallbackId) { + const fragments = Array.isArray(raw?.fragments) ? raw.fragments : []; + const blocks = fragments.map(fragmentBlock).filter(Boolean); + if (!blocks.length && raw?.content != null) blocks.push({ type: 'text', text: text(raw.content) }); + if (raw?.thinking_content) blocks.unshift({ type: 'reasoning', text: text(raw.thinking_content), elapsedSecs: raw.thinking_elapsed_secs ?? null }); + if (Array.isArray(raw?.search_results) && raw.search_results.length) { + blocks.push({ type: 'search', queries: [], results: raw.search_results.map((item) => ({ + title: text(item?.title), url: text(item?.url), snippet: text(item?.snippet), + })) }); + } + if (Array.isArray(raw?.files) && raw.files.length) { + blocks.push({ type: 'attachment', files: raw.files.map((file) => ({ + id: text(file?.id), name: text(file?.file_name || file?.name), size: Number(file?.file_size || file?.size || 0), available: false, + })) }); + } + const inferred = fragments.some((item) => text(item?.type).toUpperCase() === 'REQUEST') ? 'user' + : fragments.some((item) => text(item?.type).toUpperCase() === 'RESPONSE') ? 'assistant' : ''; + return { + id: String(raw?.id ?? raw?.message_id ?? fallbackId), + parentId: raw?.parent_id == null ? null : String(raw.parent_id), + role: text(raw?.role).toLowerCase() || inferred || 'assistant', + status: text(raw?.status || 'FINISHED'), + createdAt: numberTime(raw?.inserted_at || raw?.created_at), + blocks, + }; +} + +function finalize(raw, messages, sourceSchema) { + const id = text(raw?.id || raw?.chat_session?.id) || stableHash(raw); + const currentMessageId = raw?.current_message_id ?? raw?.chat_session?.current_message_id ?? null; + const conversation = { + sourceSchema, + sourceId: id, + title: text(raw?.title || raw?.chat_session?.title) || 'Untitled conversation', + createdAt: numberTime(raw?.inserted_at || raw?.created_at || raw?.chat_session?.inserted_at), + updatedAt: numberTime(raw?.updated_at || raw?.chat_session?.updated_at), + currentMessageId: currentMessageId == null ? null : String(currentMessageId), + messages, + }; + conversation.stableId = `${sourceSchema}:${id}:${stableHash(messages.map((message) => [message.id, message.parentId, message.blocks]))}`; + conversation.approxChars = messages.reduce((sum, message) => sum + JSON.stringify(message.blocks).length, 0); + conversation.turnCount = messages.filter((message) => message.role === 'user').length; + conversation.branchCount = countBranches(messages); + return conversation; +} + +function countBranches(messages) { + const counts = new Map(); + for (const message of messages) { + if (message.parentId != null) counts.set(message.parentId, (counts.get(message.parentId) || 0) + 1); + } + return [...counts.values()].reduce((sum, value) => sum + Math.max(0, value - 1), 0); +} + +export function activeBranch(conversation) { + if (!conversation.messages.length) return []; + const byId = new Map(conversation.messages.map((message) => [message.id, message])); + let leaf = conversation.currentMessageId && byId.get(conversation.currentMessageId); + if (!leaf) { + const parents = new Set(conversation.messages.map((message) => message.parentId).filter(Boolean)); + leaf = [...conversation.messages].reverse().find((message) => !parents.has(message.id)) || conversation.messages.at(-1); + } + const path = []; + const seen = new Set(); + while (leaf && !seen.has(leaf.id)) { + path.push(leaf); + seen.add(leaf.id); + leaf = leaf.parentId == null ? null : byId.get(leaf.parentId); + } + return path.reverse(); +} + +const officialV3Parser = { + id: 'deepseek-v3', + detect(value) { + return value?.export_format_version === 3 || (value?.id && Array.isArray(value?.messages) && value.messages.some((item) => Array.isArray(item?.fragments))); + }, + parse(value) { + const sessions = Array.isArray(value) ? value : value?.sessions ?? [value]; + return sessions.filter((item) => item?.id && Array.isArray(item?.messages)) + .map((item) => finalize(item, item.messages.map(normalizeMessage), this.id)); + }, +}; + +const legacyMappingParser = { + id: 'deepseek-mapping', + detect(value) { + const sample = Array.isArray(value) ? value[0] : value?.conversations?.[0] ?? value; + return Boolean(sample?.mapping); + }, + parse(value) { + const conversations = Array.isArray(value) ? value : value?.conversations ?? [value]; + return conversations.filter((item) => item?.id && item?.mapping).map((item) => { + const nodes = Object.values(item.mapping).filter((node) => node?.message); + const messages = nodes.map((node, index) => normalizeMessage({ + ...node.message, id: node.id ?? node.message.id, parent_id: node.parent ?? node.message.parent_id, + }, index + 1)); + const current = item.current_node ?? item.current_message_id; + return finalize({ ...item, current_message_id: current }, messages, this.id); + }); + }, +}; + +const legacyFlatParser = { + id: 'deepseek-flat', + detect(value) { + const sample = Array.isArray(value) ? value[0]?.data ?? value[0] : value?.data?.biz_data ?? value; + return Boolean(sample?.chat_session && Array.isArray(sample?.chat_messages || sample?.messages)); + }, + parse(value) { + const entries = Array.isArray(value) ? value : [value]; + return entries.map((entry) => entry?.data?.biz_data ?? entry?.data ?? entry) + .filter((item) => item?.chat_session) + .map((item) => finalize(item, (item.chat_messages || item.messages || []).map(normalizeMessage), this.id)); + }, +}; + +const deepSeekParsers = [officialV3Parser, legacyMappingParser, legacyFlatParser]; + +export function parseDeepSeekExport(value) { + for (const parser of deepSeekParsers) { + if (parser.detect(value)) return parser.parse(value); + } + throw new Error('This JSON does not match a supported DeepSeek conversation export.'); +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/icon.png b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/icon.png new file mode 100644 index 00000000..b98b78cc Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/icon.png differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/index.html b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/index.html new file mode 100644 index 00000000..98289818 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/index.html @@ -0,0 +1,74 @@ + + + + + + DeepSeek Chat Migration + + + +
+
DeepSeek Chat MigrationDeepSeek 会话迁移
+
+
DeepSeek export · local parsing
+ +
+
+
+
+

DEEPSEEK CONVERSATION MIGRATION

+

Bring your DeepSeek history.
Continue as native MCode sessions.

+

Open DeepSeek's official export, inspect restored branches and sources, then import selected conversations into MiniMax Code.

+ +
+ + +
+ +
+

NATIVE SESSION IMPORT · ~/.minimax · mavis

+

Import plan

+

+ +
    + + +
    + +
    + + +
    +
    + + + diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/styles.css b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/styles.css new file mode 100644 index 00000000..6dce4e52 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/client/styles.css @@ -0,0 +1,4 @@ +:root{color-scheme:light;--ink:#17211d;--muted:#66706b;--paper:#f3f0e8;--card:#fffdf8;--line:#d8d5cc;--green:#1f6757;--lime:#d8ef9b;--orange:#ee7657;font-family:Inter,ui-sans-serif,system-ui,sans-serif}*{box-sizing:border-box}body{margin:0;background:var(--paper);color:var(--ink)}header{height:68px;padding:0 4vw;display:flex;align-items:center;justify-content:space-between;border-bottom:1px solid var(--line)}.brand{display:flex;align-items:center;gap:12px}.brand div{display:grid}.brand small{color:var(--muted);font-size:11px}.mark{width:32px;height:32px;display:block}.privacy{font:12px ui-monospace,monospace;color:var(--green)}main{max-width:1280px;margin:auto;padding:48px 4vw}.hero{max-width:820px;margin:auto;text-align:center}.eyebrow{letter-spacing:.17em;font:700 11px ui-monospace,monospace;color:var(--green)}h1{font:600 clamp(38px,6vw,72px)/.98 Georgia,serif;margin:20px 0}h1 em{font-weight:400;color:var(--green)}.hero>p:not(.eyebrow){max-width:620px;margin:0 auto;color:var(--muted);line-height:1.6}.drop{margin:36px auto;min-height:150px;border:1.5px dashed #9ea69d;border-radius:18px;background:rgba(255,255,255,.55);display:grid;place-items:center;align-content:center;gap:8px;cursor:pointer;transition:.2s}.drop:hover,.drop.drag{border-color:var(--green);background:var(--card);transform:translateY(-2px)}.drop input{display:none}.drop-icon{font-size:28px;color:var(--orange)}.drop small{color:var(--muted)}.workspace{background:var(--card);border:1px solid var(--line);border-radius:20px;overflow:hidden;box-shadow:0 24px 60px #16201812}.hidden{display:none}.toolbar,footer{min-height:72px;padding:14px 18px;display:flex;align-items:center;gap:12px;border-bottom:1px solid var(--line)}.toolbar>div{display:grid;margin-right:auto}.toolbar small{color:var(--muted)}input[type=search]{min-width:260px;border:1px solid var(--line);border-radius:10px;padding:10px 12px;background:white}.grid{display:grid;grid-template-columns:minmax(290px,35%) 1fr;height:600px}.grid aside{border-right:1px solid var(--line);overflow:auto}.row{padding:16px 18px;border-bottom:1px solid var(--line);display:grid;grid-template-columns:auto 1fr;gap:12px;cursor:pointer}.row:hover,.row.active{background:#edf3e7}.row input{margin-top:4px}.row b{display:block;font-size:14px}.row small{color:var(--muted);line-height:1.8}.pill{display:inline-block;color:var(--green);background:var(--lime);padding:1px 7px;border-radius:999px;font-size:10px;margin-left:5px}.imported{background:#e6e5df;color:#6b6b65}.grid article{padding:24px 28px;overflow:auto}.empty{color:var(--muted);display:grid;place-items:center;height:100%}.message{margin:0 0 22px;max-width:82%}.message.assistant{margin-left:auto}.message h3{font:700 10px ui-monospace,monospace;letter-spacing:.12em;color:var(--muted)}.bubble{padding:14px 16px;background:#edeae1;border-radius:3px 15px 15px 15px;white-space:pre-wrap;line-height:1.55}.assistant .bubble{background:#e9f1e7;border-radius:15px 3px 15px 15px}details{margin-top:8px;padding:10px 12px;border:1px solid var(--line);border-radius:9px;color:var(--muted)}summary{cursor:pointer;color:var(--green);font-size:12px;font-weight:700}footer{border-top:1px solid var(--line);border-bottom:0}footer #status{margin-right:auto;color:var(--muted);font-size:13px}button{border:0;border-radius:10px;padding:11px 16px;background:var(--green);color:white;font-weight:700;cursor:pointer}button:disabled{opacity:.45;cursor:not-allowed}.quiet{background:transparent;color:var(--green);border:1px solid #a9b9b1}dialog{width:min(760px,90vw);max-height:86vh;border:1px solid var(--line);border-radius:18px;padding:28px;background:var(--card);color:var(--ink)}dialog::backdrop{background:#10231cb8}.close{float:right;background:transparent;color:var(--ink);font-size:24px;padding:0}dialog h2{font:500 32px Georgia,serif}pre{white-space:pre-wrap;font:12px/1.5 ui-monospace,monospace;max-height:180px;overflow:auto;background:#f2efe7;padding:14px;border-radius:12px}.dialog-actions{display:flex;justify-content:flex-end;gap:10px}.result-list{list-style:none;padding:0;max-height:45vh;overflow:auto}.result-list li{display:flex;justify-content:space-between;gap:20px;padding:12px;border-bottom:1px solid var(--line)}.result-list li span{color:var(--muted);text-align:right}.result-list .success span{color:#2f6b3a}.result-list .failed span{color:#a64232}.result-list .success span{color:var(--green)}@media(max-width:760px){header .privacy{display:none}main{padding:24px 12px}.toolbar{flex-wrap:wrap}.toolbar input{order:3;width:100%}.grid{grid-template-columns:1fr;height:auto}.grid aside{height:300px;border-right:0;border-bottom:1px solid var(--line)}.grid article{height:500px}footer{flex-wrap:wrap}.message{max-width:94%}} +.select-all{display:flex;align-items:center;gap:8px;padding:6px 10px 6px 4px;border-right:1px solid var(--line);margin-right:4px;font-size:12px;font-weight:600;color:var(--muted);cursor:pointer;white-space:nowrap;user-select:none}.select-all input{width:16px;height:16px;margin:0;accent-color:var(--green);cursor:pointer}.select-all:hover{color:var(--green)} +[hidden]{display:none!important}#status-wrap{margin-right:auto;display:grid;gap:6px;min-width:0}footer #status{margin-right:0}.busy::before{content:"";display:inline-block;width:11px;height:11px;margin-right:8px;border:2px solid currentColor;border-right-color:transparent;border-radius:50%;vertical-align:-1px;animation:spin .8s linear infinite}button.busy:disabled{opacity:.75;cursor:progress}@keyframes spin{to{transform:rotate(360deg)}}.progress-block{display:grid;gap:6px;margin:4px 0 8px}.progress-block small{font-size:12px;font-weight:600;color:var(--muted)}.progress{display:block;position:relative;height:6px;border-radius:999px;background:#e6e5df;overflow:hidden}.progress i{display:block;height:100%;width:0;border-radius:inherit;background:var(--green);transition:width .3s ease}.progress.waiting i{width:30%;animation:slide 1.2s ease-in-out infinite}.progress.mini{width:160px;height:4px}@keyframes slide{from{transform:translateX(-100%)}to{transform:translateX(340%)}}@media(prefers-reduced-motion:reduce){.busy::before,.progress.waiting i{animation:none}} +.header-actions{display:flex;align-items:center;gap:14px}.open-project{display:flex;align-items:center;gap:6px;padding:6px 10px;border:1px solid var(--line);border-radius:8px;background:transparent;color:var(--muted);font-size:12px;font-weight:600;white-space:nowrap}.open-project:hover{color:var(--green);border-color:#a9b9b1}.open-project i{font-style:normal;min-width:18px;padding:0 5px;border-radius:999px;background:var(--lime);color:var(--green);font-size:10px;line-height:16px;text-align:center}.link{background:transparent;color:var(--muted);padding:6px 4px;font-weight:600;font-size:12px}.link:hover{color:var(--ink)}.dialog-actions .undo{margin-right:auto}.undo-confirm{display:flex;align-items:center;gap:10px;margin:6px 0 12px;padding:10px 12px;border:1px solid var(--line);border-radius:10px;font-size:12px;color:var(--muted)}.undo-confirm span{margin-right:auto;line-height:1.5}.quiet.danger{color:#a64232;border-color:#d9b3ab;padding:7px 12px;font-size:12px}.result-list .undone span{color:var(--muted);text-decoration:line-through}@media(max-width:760px){header .header-actions .privacy{display:none}} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/miniapp.json b/plugins/danielwalnut/deepseek-chat-migration/miniapp/miniapp.json new file mode 100644 index 00000000..ed20c023 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/miniapp.json @@ -0,0 +1,28 @@ +{ + "schemaVersion": 1, + "artifacts": { + "client": [ + "./miniapp/client" + ], + "node": [ + "./miniapp/node", + "./miniapp/python" + ] + }, + "runtime": { + "kind": "process", + "entry": "./miniapp/node/server.mjs", + "lifecycle": "on-demand" + }, + "surface": { + "path": "/deepseek-chat-migration", + "window": { + "width": 1100, + "height": 760, + "minWidth": 620, + "minHeight": 480, + "resizable": true + } + }, + "mcpEndpoints": [] +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/migration.mjs b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/migration.mjs new file mode 100644 index 00000000..f356a173 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/migration.mjs @@ -0,0 +1,371 @@ +// @ts-check + +import { spawn } from 'node:child_process'; +import { readFile } from 'node:fs/promises'; +import { homedir } from 'node:os'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const MAX_OUTPUT = 64 * 1024; +const MAX_LINE = 1024 * 1024; +/** Sessions per write transaction; each batch commit is one progress step in the UI. */ +export const BATCH_SIZE = 10; + +/** The importer ships inside this Mini App; no other plugin is needed. */ +export const IMPORTER_SCRIPT = fileURLToPath(new URL('../python/import_deepseek.py', import.meta.url)); +export const TARGET_AGENT = 'mavis'; + +export function defaultTargetDataDir(home = homedir()) { + return join(home, '.minimax'); +} + +export function defaultWorkspace(home = homedir()) { + return join(home, 'DeepSeek'); +} + +export function defaultPython(platform = process.platform) { + return platform === 'win32' ? 'python' : 'python3'; +} + +export function buildMigrationArgs({ + scriptPath = IMPORTER_SCRIPT, + action, + sourcePath, + sessionIds, + reportPath, + targetDataDir, + agentName = TARGET_AGENT, + workspace, + batchSize = BATCH_SIZE, +}) { + if (action !== 'plan' && action !== 'apply') throw migrationError('invalid_action'); + if (!Array.isArray(sessionIds) || !sessionIds.length || sessionIds.length > 1000) { + throw migrationError('invalid_sessions'); + } + for (const [name, value] of [['sourcePath', sourcePath], ['reportPath', reportPath], ['targetDataDir', targetDataDir], ['workspace', workspace]]) { + if (typeof value !== 'string' || !value) throw migrationError('invalid_arguments', { message: `${name} is required` }); + } + return [ + scriptPath, + action, + '--source', sourcePath, + ...sessionIds.map(validateSessionId).flatMap((id) => ['--session', id]), + '--report', reportPath, + '--data-dir', targetDataDir, + '--agent', validateAgentName(agentName), + '--workspace', workspace, + '--batch-size', String(validateBatchSize(batchSize)), + // Imports go to the DeepSeek project; if it was removed from the sidebar, + // importing selected chats into it brings it back. + '--restore-projects', + ]; +} + +/** + * Runs the bundled importer. `plan` writes a fresh report; `apply` must be given the + * same report path, which binds it to the previewed sessions and options. + */ +export async function runMigrationCommand({ + python = defaultPython(), + scriptPath = IMPORTER_SCRIPT, + action, + sourcePath, + sessionIds, + reportPath, + targetDataDir = defaultTargetDataDir(), + agentName = TARGET_AGENT, + workspace = defaultWorkspace(), + batchSize = BATCH_SIZE, + signal, + onProgress, +}) { + const args = buildMigrationArgs({ scriptPath, action, sourcePath, sessionIds, reportPath, targetDataDir, agentName, workspace, batchSize }); + const onLine = typeof onProgress === 'function' + ? (line) => { + const event = parseProgressLine(line); + if (event) onProgress(event); + } + : null; + const processResult = await spawnAndWait(python, args, signal, onLine); + let report; + try { + report = JSON.parse(await readFile(reportPath, 'utf8')); + } catch (error) { + const detail = importerError(processResult.stderr); + if (error?.code === 'ENOENT') { + throw migrationError('migration_failed', { + message: detail || `导入脚本没有生成报告(exit ${processResult.exitCode})。`, + exitCode: processResult.exitCode, + stderr: processResult.stderr, + }); + } + throw migrationError('migration_report_invalid', { message: detail || undefined }); + } + const parsed = parseMigrationReport(report, sessionIds); + const failedRun = processResult.exitCode !== 0 && processResult.exitCode !== 2; + return { + ...parsed, + exitCode: processResult.exitCode, + error: failedRun ? importerError(processResult.stderr) || `exit ${processResult.exitCode}` : '', + ok: processResult.exitCode === 0 && parsed.summary.failed === 0, + }; +} + +const STATUS = { + ready: 'planned', + imported: 'success', + 'already-imported': 'skipped', + incompatible: 'failed', + failed: 'failed', + running: 'failed', +}; + +/** Maps the importer's report entries onto the UI's planned/success/skipped/failed. */ +export function parseMigrationReport(report, requestedIds = []) { + const byId = new Map(); + for (const entry of Array.isArray(report?.sessions) ? report.sessions : []) { + if (!entry || typeof entry.sourceId !== 'string') continue; + const status = STATUS[entry.status] || 'failed'; + byId.set(entry.sourceId, { + sessionId: entry.sourceId, + status, + rawStatus: String(entry.status ?? ''), + message: entry.error ? String(entry.error) + : status === 'skipped' ? '已导入过,跳过' + : entry.status === 'running' ? '上次导入被中断,重新导入即可继续' : '', + targetSessionId: typeof entry.plan?.targetId === 'string' ? entry.plan.targetId : null, + title: typeof entry.plan?.title === 'string' ? entry.plan.title : null, + messageCount: Number.isInteger(entry.plan?.messageCount) ? entry.plan.messageCount : null, + warnings: Array.isArray(entry.plan?.warnings) ? entry.plan.warnings.length : 0, + }); + } + const sessions = requestedIds.map((id) => byId.get(String(id)) || { + sessionId: String(id), + status: 'failed', + rawStatus: 'missing', + message: '报告里没有这个会话', + targetSessionId: null, + title: null, + messageCount: null, + warnings: 0, + }); + const count = (status) => sessions.filter((item) => item.status === status).length; + return { + sessions, + summary: { + total: sessions.length, + planned: count('planned'), + succeeded: count('success'), + skipped: count('skipped'), + failed: count('failed'), + }, + state: typeof report?.state === 'string' ? report.state : '', + // No database copy: one small undo journal per apply run. + undo: typeof report?.undo === 'string' && report.undo ? report.undo : null, + }; +} + +/** + * One stderr line from `apply`: `{"event":"start"|"progress", ...}`. + * Anything else (summaries, tracebacks) is not progress and yields null. + */ +export function parseProgressLine(line) { + let value; + try { + value = JSON.parse(String(line).trim()); + } catch { + return null; + } + if (!value || typeof value !== 'object') return null; + const count = (n) => Number.isInteger(n) && n >= 0 ? n : 0; + if (value.event === 'start') return { event: 'start', total: count(value.total) }; + if (value.event !== 'progress') return null; + const sessions = (Array.isArray(value.sessions) ? value.sessions : []) + .filter((item) => item && typeof item.sourceId === 'string') + .map((item) => ({ sessionId: item.sourceId, status: STATUS[item.status] || 'failed' })); + return { event: 'progress', done: count(value.done), total: count(value.total), sessions }; +} + +/** Folds progress events into what the page polls: phase, done/total, landed rows. */ +export function createProgress(total = 0) { + return { phase: 'starting', done: 0, total, sessions: {} }; +} + +export function applyProgressEvent(progress, event) { + if (!event) return progress; + if (event.event === 'start') { + progress.phase = 'starting'; + progress.total = event.total; + } else if (event.event === 'progress') { + progress.phase = 'importing'; + progress.done = Math.min(event.done, event.total); + progress.total = event.total; + for (const item of event.sessions) progress.sessions[item.sessionId] = item.status; + } + return progress; +} + +/** The importer prints `{"error": ...}` as JSON on stderr for contract errors. */ +export function importerError(stderr = '') { + const lines = String(stderr).trim().split('\n').filter(Boolean); + for (const line of lines.reverse()) { + try { + const value = JSON.parse(line); + if (value && typeof value.error === 'string') return value.error; + } catch { + // Progress or interpreter output; fall through to the raw last line. + } + } + return lines[0] || ''; +} + +function validateSessionId(value) { + if (typeof value !== 'string' || !value || value.startsWith('-') || value.length > 256 + || /[\u0000-\u001f\u007f]/.test(value)) { + throw migrationError('invalid_session_id'); + } + return value; +} + +function validateBatchSize(value) { + if (!Number.isInteger(value) || value < 1 || value > 100) throw migrationError('invalid_arguments', { message: 'batchSize must be 1-100' }); + return value; +} + +function validateAgentName(value) { + if (typeof value !== 'string' || !value.trim() || value.trim().startsWith('-') || value.length > 128 + || /[\u0000-\u001f\u007f/\\]/.test(value)) { + throw migrationError('invalid_agent'); + } + return value.trim(); +} + +/** + * Undoes one apply run from its journal. Resolves with the importer's summary; rejects + * with `undo_refused` (and the changed sessions) when sessions were used since import. + */ +export async function runUndoCommand({ + python = defaultPython(), + scriptPath = IMPORTER_SCRIPT, + journalPath, + targetDataDir = defaultTargetDataDir(), + force = false, + signal, +}) { + if (typeof journalPath !== 'string' || !journalPath || journalPath.startsWith('-')) throw migrationError('invalid_arguments', { message: 'journalPath is required' }); + const args = [scriptPath, 'undo', '--run', journalPath, '--data-dir', targetDataDir, ...(force ? ['--force'] : [])]; + const result = await spawnAndWait(python, args, signal, null, true); + if (result.exitCode === 3) { + const detail = lastJson(result.stderr); + const modified = Array.isArray(detail?.modified) ? detail.modified : []; + throw migrationError('undo_refused', { + message: `有 ${modified.length} 个导入的会话在导入后又用过(比如继续聊了),这次没有撤销任何内容。`, + modified: modified.map((item) => ({ targetSessionId: String(item?.targetId ?? ''), sessionId: String(item?.sourceId ?? ''), changed: Array.isArray(item?.changed) ? item.changed.map(String) : [] })), + }); + } + if (result.exitCode !== 0) { + throw migrationError('undo_failed', { message: importerError(result.stderr) || `撤销失败(exit ${result.exitCode})。` }); + } + const summary = lastJson(result.stdout); + if (!summary || summary.status !== 'undone') throw migrationError('undo_failed', { message: '撤销脚本没有返回结果。' }); + return { + removed: Number.isInteger(summary.removed) ? summary.removed : 0, + alreadyAbsent: Number.isInteger(summary.alreadyAbsent) ? summary.alreadyAbsent : 0, + forced: Number.isInteger(summary.forced) ? summary.forced : 0, + keptDirectories: Array.isArray(summary.keptDirectories) ? summary.keptDirectories.map(String) : [], + warnings: Array.isArray(summary.warnings) ? summary.warnings.map(String) : [], + sessions: (Array.isArray(summary.sessions) ? summary.sessions : []).map((item) => ({ sessionId: String(item?.sourceId ?? ''), targetSessionId: String(item?.targetId ?? '') })), + }; +} + +/** + * The DeepSeek project as the runtime DB sees it: how many sessions it holds and the + * most recently active one, so the page can open it before any upload. + */ +export async function readProject({ + python = defaultPython(), + scriptPath = IMPORTER_SCRIPT, + targetDataDir = defaultTargetDataDir(), + workspace = defaultWorkspace(), + signal, +} = {}) { + const result = await spawnAndWait(python, [scriptPath, 'project', '--data-dir', targetDataDir, '--workspace', workspace], signal, null, true); + const summary = lastJson(result.stdout); + if (result.exitCode !== 0 || !summary) throw migrationError('project_unavailable', { message: importerError(result.stderr) || 'project lookup failed' }); + return { + sessionCount: Number.isInteger(summary.sessionCount) ? summary.sessionCount : 0, + latestSessionId: typeof summary.latestSessionId === 'string' ? summary.latestSessionId : null, + }; +} + +function lastJson(text = '') { + const lines = String(text).trim().split('\n').filter(Boolean).reverse(); + for (const line of lines) { + try { + const value = JSON.parse(line); + if (value && typeof value === 'object') return value; + } catch { + // not JSON + } + } + return null; +} + +function spawnAndWait(command, args, signal, onLine, captureStdout = false) { + return new Promise((resolvePromise, reject) => { + const child = spawn(command, args, { + shell: false, + stdio: ['ignore', captureStdout ? 'pipe' : 'ignore', 'pipe'], + windowsHide: true, + signal, + }); + let stderr = ''; + let stdout = ''; + let pending = ''; + if (captureStdout) { + child.stdout.setEncoding('utf8'); + child.stdout.on('data', (chunk) => { + if (stdout.length < MAX_OUTPUT) stdout += chunk.slice(0, MAX_OUTPUT - stdout.length); + }); + } + const deliver = (line) => { + if (!onLine || !line) return; + try { + onLine(line); + } catch { + // A broken listener must not break the import it is watching. + } + }; + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (chunk) => { + if (stderr.length < MAX_OUTPUT) stderr += chunk.slice(0, MAX_OUTPUT - stderr.length); + if (!onLine) return; + pending += chunk; + const lines = pending.split('\n'); + pending = lines.pop() ?? ''; + if (pending.length > MAX_LINE) pending = ''; + for (const line of lines) deliver(line); + }); + child.once('error', (error) => { + if (error?.code === 'ENOENT') { + reject(migrationError('python3_not_found', { + message: `没有找到 ${command}。导入需要 Python 3.9 或更新版本,安装后重新打开这个 Mini App。`, + })); + return; + } + reject(migrationError(error?.name === 'AbortError' ? 'migration_cancelled' : 'migration_spawn_failed')); + }); + child.once('close', (exitCode) => { + deliver(pending); + resolvePromise({ exitCode: exitCode ?? -1, stderr: stderr.trim(), stdout: stdout.trim() }); + }); + }); +} + +function migrationError(code, details = {}) { + const { message, ...rest } = details; + const error = new Error(message || code); + error.code = code; + Object.assign(error, rest); + return error; +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/open-folder.mjs b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/open-folder.mjs new file mode 100644 index 00000000..3c891786 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/open-folder.mjs @@ -0,0 +1,35 @@ +// @ts-check + +import { spawn } from 'node:child_process'; +import { stat } from 'node:fs/promises'; + +/** + * The OS file-manager command for a folder: Finder, Explorer, or the desktop's + * default on Linux. Arguments are an array; no shell is involved. + */ +export function folderOpener(path, platform = process.platform) { + if (platform === 'darwin') return { command: 'open', args: [path] }; + if (platform === 'win32') return { command: 'explorer.exe', args: [path] }; + return { command: 'xdg-open', args: [path] }; +} + +/** Opens an existing directory in the file manager. Resolves once the opener started. */ +export async function openFolder(path, { platform = process.platform, spawnImpl = spawn } = {}) { + const info = await stat(path).catch(() => null); + if (!info?.isDirectory()) throw openError('folder_missing', `没有找到文件夹 ${path}`); + const { command, args } = folderOpener(path, platform); + await new Promise((resolve, reject) => { + const child = spawnImpl(command, args, { shell: false, stdio: 'ignore', detached: true, windowsHide: true }); + child.once('error', (error) => reject(openError('open_failed', `无法打开文件夹:${error?.code || 'unknown error'}`))); + child.once('spawn', () => { + child.unref(); + resolve(undefined); + }); + }); +} + +function openError(code, message) { + const error = new Error(message); + /** @type {any} */ (error).code = code; + return error; +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/server.mjs b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/server.mjs new file mode 100644 index 00000000..3fa747b1 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/node/server.mjs @@ -0,0 +1,360 @@ +// @ts-check + +import { createWriteStream } from 'node:fs'; +import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises'; +import { createServer } from 'node:http'; +import { extname, join } from 'node:path'; +import { Transform } from 'node:stream'; +import { pipeline } from 'node:stream/promises'; +import { randomUUID } from 'node:crypto'; +import { + applyProgressEvent, + createProgress, + defaultTargetDataDir, + defaultWorkspace, + readProject, + runMigrationCommand, + runUndoCommand, + TARGET_AGENT, +} from './migration.mjs'; +import { openFolder } from './open-folder.mjs'; + +const MAX_JSON_BODY = 1024 * 1024; +const MAX_ZIP_BODY = 1024 * 1024 * 1024; +const ASSETS = new Set(['app.js', 'styles.css', 'deepseek.js', 'archive.js', 'icon.png']); + +export async function start(context) { + await mkdir(context.dataDir, { recursive: true }); + const statePath = join(context.dataDir, 'state.json'); + const state = await loadState(statePath); + const clientRoot = join(context.pluginRoot, 'miniapp/client'); + let source = null; + const pendingPlans = new Map(); + // planId -> live apply progress; the page polls it while apply runs. + const progressByPlan = new Map(); + let persisting = Promise.resolve(); + let undoing = false; + const persist = () => { + persisting = persisting.then(() => saveState(statePath, state)).catch((error) => { + context.logger.warn(`state save failed: ${error instanceof Error ? error.message : 'unknown error'}`); + }); + return persisting; + }; + const remember = (selection, item) => { + state.imported[selection.stableId] = { + sourceId: selection.id, + targetSessionId: item.targetSessionId ?? state.imported[selection.stableId]?.targetSessionId ?? null, + importedAt: new Date().toISOString(), + }; + }; + + const server = createServer(async (request, response) => { + try { + const url = new URL(request.url ?? '/', 'http://miniapp.local'); + if (request.method === 'GET' && url.pathname === '/deepseek-chat-migration') return sendFile(response, join(clientRoot, 'index.html')); + if (request.method === 'GET' && url.pathname.startsWith('/assets/')) { + const name = url.pathname.slice('/assets/'.length); + if (!ASSETS.has(name)) return json(response, 404, { error: 'not_found' }); + return sendFile(response, join(clientRoot, name)); + } + if (request.method === 'GET' && url.pathname === '/api/state') { + return json(response, 200, { + imported: Object.keys(state.imported), + targetDataDir: defaultTargetDataDir(), + agentName: TARGET_AGENT, + workspace: defaultWorkspace(), + lastRun: lastUndoableRun(state), + }); + } + if (request.method === 'GET' && url.pathname === '/api/project') { + // What 「打开 DeepSeek 项目」 opens: the runtime DB is authoritative, state.json + // (sessions this Mini App imported) is the fallback when Python is unavailable. + let project = null; + try { + project = await readProject({ signal: context.signal }); + } catch { + project = null; + } + const remembered = latestImported(state); + return json(response, 200, { + workspace: defaultWorkspace(), + sessionCount: project ? project.sessionCount : Object.keys(state.imported).length, + latestSessionId: project ? project.latestSessionId : remembered, + source: project ? 'database' : 'state', + }); + } + if (request.method === 'POST' && url.pathname === '/api/source') { + if (source?.dir) await rm(source.dir, { recursive: true, force: true }); + const dir = await mkdtemp(join(context.dataDir, 'migration-')); + const path = join(dir, 'deepseek-export.zip'); + try { + await saveRequestBody(request, path, MAX_ZIP_BODY); + } catch (error) { + await rm(dir, { recursive: true, force: true }); + throw error; + } + source = { dir, path }; + pendingPlans.clear(); + progressByPlan.clear(); + return json(response, 200, { uploaded: true }); + } + if (request.method === 'POST' && url.pathname === '/api/migration/plan') { + if (!source?.path) return json(response, 409, { error: 'source_not_uploaded' }); + const body = await readJson(request); + const selections = readSelections(body.sessions); + const reportPath = join(source.dir, `job-${randomUUID()}.json`); + const result = await runMigrationCommand({ + action: 'plan', + sourcePath: source.path, + sessionIds: selections.map((item) => item.id), + reportPath, + signal: context.signal, + }); + const planId = randomUUID(); + pendingPlans.set(planId, { selections, reportPath }); + return json(response, 200, { planId, ...result }); + } + if (request.method === 'POST' && url.pathname === '/api/migration/apply') { + if (!source?.path) return json(response, 409, { error: 'source_not_uploaded' }); + const body = await readJson(request); + const plan = typeof body.planId === 'string' ? pendingPlans.get(body.planId) : null; + if (!plan) return json(response, 409, { error: 'plan_not_found' }); + if (plan.running) return json(response, 409, { error: 'apply_running', message: '这批会话正在导入。' }); + plan.running = true; + const bySource = new Map(plan.selections.map((item) => [item.id, item])); + const progress = createProgress(); + progress.imported = []; + progressByPlan.set(body.planId, progress); + let result; + try { + // Apply resumes the previewed report: same sessions, same options. + result = await runMigrationCommand({ + action: 'apply', + sourcePath: source.path, + sessionIds: plan.selections.map((item) => item.id), + reportPath: plan.reportPath, + signal: context.signal, + onProgress: (event) => { + applyProgressEvent(progress, event); + if (event.event !== 'progress') return; + let landed = false; + for (const item of event.sessions) { + const selection = bySource.get(item.sessionId); + if (!selection || (item.status !== 'success' && item.status !== 'skipped')) continue; + if (!progress.imported.includes(selection.stableId)) progress.imported.push(selection.stableId); + remember(selection, item); + landed = true; + } + // Rows that landed stay imported even if the runtime stops mid-run. + if (landed) void persist(); + }, + }); + } catch (error) { + progress.phase = 'failed'; + plan.running = false; + throw error; + } + const imported = []; + for (const item of result.sessions) { + const selection = bySource.get(item.sessionId); + if (selection && (item.status === 'success' || item.status === 'skipped')) { + remember(selection, item); + imported.push(selection.stableId); + } + } + await persist(); + progress.phase = 'done'; + progress.done = progress.total; + progress.imported = imported; + pendingPlans.delete(body.planId); + let runId = null; + if (result.undo && result.summary.succeeded > 0) { + runId = body.planId; + state.runs.push({ + runId, + journal: result.undo, + at: new Date().toISOString(), + stableIds: result.sessions + .filter((item) => item.status === 'success') + .map((item) => bySource.get(item.sessionId)?.stableId) + .filter(Boolean), + }); + state.runs = state.runs.slice(-20); + await persist(); + } + return json(response, 200, { imported, runId, ...result }); + } + if (request.method === 'POST' && url.pathname === '/api/migration/undo') { + const body = await readJson(request); + const run = state.runs.find((item) => item.runId === body.runId && !item.undoneAt); + if (!run) return json(response, 409, { error: 'run_not_found', message: '找不到这次导入,可能已经撤销过了。' }); + if (undoing) return json(response, 409, { error: 'undo_running', message: '正在撤销。' }); + undoing = true; + try { + const result = await runUndoCommand({ journalPath: run.journal, force: body.force === true, signal: context.signal }); + for (const stableId of run.stableIds) delete state.imported[stableId]; + run.undoneAt = new Date().toISOString(); + await persist(); + return json(response, 200, { undone: run.stableIds, ...result }); + } catch (error) { + if (error?.code === 'undo_refused') return json(response, 409, { error: 'undo_refused', message: error.message, modified: error.modified }); + throw error; + } finally { + undoing = false; + } + } + if (request.method === 'GET' && url.pathname === '/api/migration/progress') { + const progress = progressByPlan.get(url.searchParams.get('planId') ?? ''); + if (!progress) return json(response, 404, { error: 'progress_not_found' }); + return json(response, 200, { + phase: progress.phase, + done: progress.done, + total: progress.total, + sessions: Object.entries(progress.sessions).map(([sessionId, status]) => ({ sessionId, status })), + imported: progress.imported, + }); + } + if (request.method === 'POST' && url.pathname === '/api/workspace/open') { + // Fallback when the Host cannot open an imported session itself. The importer + // creates ~/DeepSeek anyway, so a fresh install gets the (empty) folder too. + await mkdir(defaultWorkspace(), { recursive: true }); + await openFolder(defaultWorkspace()); + return json(response, 200, { opened: true, path: defaultWorkspace() }); + } + return json(response, 404, { error: 'not_found' }); + } catch (error) { + context.logger.warn(`request failed: ${error instanceof Error ? error.message : 'unknown error'}`); + if (!response.headersSent) { + const code = typeof error?.code === 'string' ? error.code : 'request_failed'; + const message = error instanceof Error && error.message !== code ? error.message : undefined; + json(response, code === 'BODY_TOO_LARGE' || code === 'body_too_large' ? 413 : 400, { error: code, message }); + } + else response.end(); + } + }); + + await listen(server, context.listen.host, context.listen.port); + context.logger.info('deepseek-chat-migration.runtime.listening'); + let disposed = false; + const dispose = async () => { + if (disposed) return; + disposed = true; + context.signal.removeEventListener('abort', onAbort); + await close(server); + if (source?.dir) await rm(source.dir, { recursive: true, force: true }); + }; + const onAbort = () => void dispose(); + context.signal.addEventListener('abort', onAbort, { once: true }); + if (context.signal.aborted) await dispose(); + return { dispose }; +} + +function readSelections(value) { + if (!Array.isArray(value) || !value.length || value.length > 1000) throw requestError('invalid_sessions'); + return value.map((item) => { + if (!item || typeof item.id !== 'string' || !item.id || item.id.startsWith('-') || item.id.length > 256 + || typeof item.stableId !== 'string' || !item.stableId || item.stableId.length > 256 + || /[\u0000-\u001f\u007f]/.test(item.id)) { + throw requestError('invalid_sessions'); + } + return { id: item.id, stableId: item.stableId }; + }); +} + +async function loadState(path) { + try { + const value = JSON.parse(await readFile(path, 'utf8')); + return { + imported: value.imported && typeof value.imported === 'object' ? value.imported : {}, + runs: Array.isArray(value.runs) ? value.runs.filter((run) => run && typeof run.runId === 'string' && typeof run.journal === 'string' && Array.isArray(run.stableIds)) : [], + }; + } catch (error) { + if (error?.code !== 'ENOENT') throw error; + return { imported: {}, runs: [] }; + } +} + +function latestImported(state) { + let latest = null; + for (const entry of Object.values(state.imported)) { + if (!entry?.targetSessionId) continue; + if (!latest || String(entry.importedAt) > String(latest.importedAt)) latest = entry; + } + return latest?.targetSessionId ?? null; +} + +function lastUndoableRun(state) { + const run = [...state.runs].reverse().find((item) => !item.undoneAt); + return run ? { runId: run.runId, at: run.at, count: run.stableIds.length } : null; +} + +async function saveState(path, state) { + const temporary = `${path}.tmp`; + await writeFile(temporary, JSON.stringify(state), { encoding: 'utf8', mode: 0o600 }); + await rename(temporary, path); +} + +async function readJson(request) { + const chunks = []; + let size = 0; + for await (const chunk of request) { + size += chunk.length; + if (size > MAX_JSON_BODY) { + const error = new Error('body too large'); + error.code = 'BODY_TOO_LARGE'; + throw error; + } + chunks.push(chunk); + } + return JSON.parse(Buffer.concat(chunks).toString('utf8')); +} + +async function saveRequestBody(request, path, limit) { + let size = 0; + const limiter = new Transform({ + transform(chunk, _encoding, callback) { + size += chunk.length; + if (size > limit) { + callback(requestError('body_too_large')); + return; + } + callback(null, chunk); + }, + }); + await pipeline(request, limiter, createWriteStream(path, { flags: 'wx', mode: 0o600 })); + if (!size) throw requestError('empty_source'); +} + +function requestError(code) { + const error = new Error(code); + error.code = code; + return error; +} + +async function sendFile(response, path) { + const body = await readFile(path); + const type = ({ '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.png': 'image/png' })[extname(path)] || 'application/octet-stream'; + const charset = type.startsWith('text/') ? '; charset=utf-8' : ''; + response.writeHead(200, { 'content-type': `${type}${charset}`, 'x-content-type-options': 'nosniff' }); + response.end(body); +} + +function json(response, status, body) { + response.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'x-content-type-options': 'nosniff' }); + response.end(JSON.stringify(body)); +} + +function listen(server, host, port) { + return new Promise((resolve, reject) => { + const onError = (error) => reject(error); + server.once('error', onError); + server.listen(port, host, () => { + server.off('error', onError); + resolve(); + }); + }); +} + +function close(server) { + return new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_core.cpython-313.pyc b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_core.cpython-313.pyc new file mode 100644 index 00000000..7f22f8b0 Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_core.cpython-313.pyc differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_export.cpython-313.pyc b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_export.cpython-313.pyc new file mode 100644 index 00000000..72d94365 Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_export.cpython-313.pyc differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_undo.cpython-313.pyc b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_undo.cpython-313.pyc new file mode 100644 index 00000000..43d210e8 Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/__pycache__/deepseek_undo.cpython-313.pyc differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_core.py b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_core.py new file mode 100644 index 00000000..e6b18577 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_core.py @@ -0,0 +1,1135 @@ +"""Native MiniMax Code session writer for the DeepSeek Chat Migration Mini App. + +Python 3.9+, standard library only. Adapted from the storage core of MiniMax Code's +agent-session-migration plugin (author: Insider), reduced to the DeepSeek source: +the migration lock, single write transaction, rollback, post-commit read-back and +first-turn history initialization are kept as they were. The plugin's full database +backup is replaced by a per-run undo journal (deepseek_undo.py). +Target identities, marker files and the lock directory match that plugin's DeepSeek +source so imports from either tool are recognized by the other. +""" + +import base64 +import contextlib +import hashlib +import json +import os +import sqlite3 +import time +import unicodedata +from datetime import datetime, timezone +from pathlib import Path + +FORMAT = 1 +ADAPTER_VERSION = 2 +SOURCE_TYPE = "deepseek" +LABEL = "DeepSeek" +MARKER = "deepseek-migration.json" +ARCHIVE = "deepseek-source.json" +ID_PREFIX = "msg-deepseek-" +TURN_PREFIX = "deepseek-" +PROVIDER = "deepseek-history" +DISPLAY_KIND = "deepseek_history_archive" +# Shared with agent-session-migration so the two tools never write concurrently. +LOCK_DIR = "zcode-migration" +MAX_RECORD = 16 * 1024 * 1024 +TARGET_COLUMNS = { + "local_runtime_pi_history_file_migrations": { + "session_id", + "migrated_at_ms", + "source", + "message_count", + "target_revision", + }, + "local_runtime_sessions": { + "session_id", + "record_json", + "updated_at_ms", + "columnar_version", + "agent_name", + "runtime", + "session_type", + "status", + "archived", + "visibility", + "session_kind", + "purpose_kind", + "workspace_dir", + "project_workspace_dir", + "is_default_workspace", + "title", + "created_at_ms", + "extra_data_json", + "project_id", + "history_relative_dir", + }, + "local_runtime_message_rows": { + "session_id", + "msg_id", + "role", + "turn_id", + "source", + "created_at_ms", + "data_json", + }, + "local_runtime_projects": { + "project_id", + "project_kind", + "workspace_dir", + "created_at_ms", + "updated_at_ms", + "hidden", + "extra_data_json", + }, + "local_runtime_session_fts_keys": {"fts_rowid", "session_id"}, + "local_runtime_sessions_fts": { + "session_id", + "session_id_terms", + "agent_name_terms", + "title_terms", + "workspace_dir_terms", + "purpose_terms", + "status_terms", + "session_type_terms", + }, +} + + +class MigrationError(Exception): + pass + + +def compact(value): + return json.dumps(value, ensure_ascii=False, separators=(",", ":"), allow_nan=False) + + +def digest(value): + return hashlib.sha256(compact(value).encode()).hexdigest() + + +def history_revision(records): + # The importer emits canonical envelopes with ASCII keys and integer numbers. + # Match Runtime's recursively key-sorted JSON semantic revision exactly. + raw = json.dumps( + records, + ensure_ascii=False, + separators=(",", ":"), + sort_keys=True, + allow_nan=False, + ) + return "sha256:" + hashlib.sha256(raw.encode("utf-8")).hexdigest() + + +def initialize_history(db, sid, records): + # 'empty' describes the legacy MCode source. The imported JSONL is already + # authoritative; first-turn materialization must not compare it to no history. + insert( + db, + "local_runtime_pi_history_file_migrations", + { + "session_id": sid, + "migrated_at_ms": int(time.time() * 1000), + "source": "empty", + "message_count": len(records), + "target_revision": history_revision(records), + }, + ) + + +def is_link(path): + # Windows junctions are reparse points too; Python 3.9 has no Path.is_junction. + import stat + + try: + return path.is_symlink() or bool( + getattr(path.lstat(), "st_file_attributes", 0) + & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400) + ) + except FileNotFoundError: + return False + + +def regular(path): + path = Path(path).expanduser().absolute() + if is_link(path) or not path.is_file(): + raise MigrationError("Expected an existing regular file: " + str(path)) + return path + + +@contextlib.contextmanager +def connect(path, writable=False): + path = regular(path).resolve() + db = sqlite3.connect( + path.as_uri() + ("?mode=rw" if writable else "?mode=ro"), uri=True, timeout=10 + ) + try: + db.row_factory = sqlite3.Row + db.execute("PRAGMA foreign_keys=ON") + if not writable: + db.execute("PRAGMA query_only=ON") + db.execute("BEGIN") + yield db + except sqlite3.OperationalError as exc: + if not writable and "unable to open database file" in str(exc): + raise MigrationError( + "Cannot read SQLite database: " + + str(path) + + "; verify directory access and open the owning app so SQLite can initialize WAL sidecars. " + "Do not delete WAL/SHM files or bypass them with immutable mode." + ) from exc + raise + finally: + db.close() + + +def schema(db, required): + for table, columns in required.items(): + found = {r["name"] for r in db.execute('PRAGMA table_info("' + table + '")')} + if not columns <= found: + raise MigrationError( + "Unsupported schema for " + + table + + ": missing " + + ", ".join(sorted(columns - found)) + ) + + +def parse(raw, location): + if not isinstance(raw, str) or len(raw.encode()) > MAX_RECORD: + raise MigrationError("Oversized/invalid JSON at " + location) + try: + result = json.loads(raw) + except (ValueError, TypeError) as exc: + raise MigrationError("Malformed JSON at " + location) from exc + if not isinstance(result, dict): + raise MigrationError("Expected JSON object at " + location) + return result + + +def timestamp(value): + if ( + isinstance(value, bool) + or not isinstance(value, int) + or not 0 <= value <= 253402300799999 + ): + raise MigrationError("Expected Unix milliseconds") + return value + + +def target_path(value): + if not value: + raise MigrationError( + "An explicit --data-dir (MiniMax Code data directory) is required" + ) + root = Path(value).expanduser().absolute() + if is_link(root) or not root.is_dir(): + raise MigrationError("Target must be an existing non-symlink data directory") + root = root.resolve() + safe_directory(root, "v2/sqlite") + regular(root / "v2/sqlite/runtime-state.sqlite") + return root + + +def safe_directory(root, relative): + path = root + for part in Path(relative).parts: + if part in ("..", ".", "") or Path(part).is_absolute(): + raise MigrationError("Unsafe output path") + path = path / part + if is_link(path): + raise MigrationError("Symlink in output path: " + str(path)) + if path.exists() and not path.is_dir(): + raise MigrationError("Not a directory: " + str(path)) + return path + + +def private_write(path, value): + data = value.encode("utf-8") + with open(path, "xb") as out: + os.chmod(path, 0o600) + out.write(data) + out.flush() + os.fsync(out.fileno()) + + +def identity(root, source_id): + """root is the stable account key from deepseek_export.identity_root, not a path: + every upload lands at a fresh temporary path, and the same chat must map to the + same target session each time.""" + return "mvs_" + hashlib.sha256((root + "\0" + source_id).encode()).hexdigest()[:32] + + +def history_dir(sid, created): + dt = datetime.fromtimestamp(created / 1000, timezone.utc) + encoded = base64.urlsafe_b64encode(sid.encode()).decode().rstrip("=") + return ( + dt.strftime("%Y/%m/%d/%H-%M-%S-") + + str(created % 1000).zfill(3) + + "-session_" + + encoded + ) + + +def message_identity(sid, msg): + raw = hashlib.sha256((sid + "\0" + msg["id"]).encode()).digest() + if msg["data"].get("role") == "user": + return "msg-user-v1-" + base64.urlsafe_b64encode(raw).decode().rstrip("=") + return ID_PREFIX + raw.hex()[:32] + + +def historical_detail(value): + """Plain code-panel text with real line breaks, not a quoted JSON blob.""" + if isinstance(value, str): + return value + if isinstance(value, dict): + return "\n\n".join( + str(k) + ":\n" + historical_detail(v) for k, v in value.items() + ) + if isinstance(value, list): + return "\n\n".join( + "[" + str(i + 1) + "]\n" + historical_detail(v) for i, v in enumerate(value) + ) + return compact(value) + + +def historical_tool_view(evidence, mid, index, label): + """Display-only protocol record. Never a canonical/provider tool invocation.""" + # Do not dispatch arbitrary source names to native interactive tool renderers. + # The original status is metadata; even unfinished source calls are inert here. + args = { + "来源": label, + "工具": evidence.get("tool"), + "原始状态": evidence.get("status"), + "说明": "历史记录,仅供阅读,不重新执行", + "输入": evidence.get("input"), + } + result = {k: v for k, v in evidence.items() if k not in ("tool", "input")} + return { + "tool_name": "历史工具记录", + "tool_call_id": mid + "-history-" + str(index), + "tool_call_status": 2, + "tool_call_args": compact(historical_detail(args)), + "tool_call_result_data": compact( + {"content": [{"type": "text", "text": historical_detail(result)}]} + ), + } + + +def historical_file_text(part, label): + """A reference is not an uploaded attachment and must not imply copied bytes.""" + name = part.get("filename") or part.get("name") or "附件" + path = part.get("url") or part.get("path") or part.get("file_path") + return ( + "[" + + label + + " 历史附件引用,未复制文件]\n" + + str(name) + + ("\n" + str(path) if path else "\n" + compact(part)) + ) + + +def select_histories(item): + """The reader names the displayed and the model-context message ids explicitly.""" + messages = item["messages"] + by_id = {msg["id"]: msg for msg in messages} + if len(by_id) != len(messages): + raise MigrationError("Duplicate source message identity") + histories = item.get("histories") + if not isinstance(histories, dict) or set(histories) != {"display", "active"}: + raise MigrationError("Invalid source history selection") + selected = [] + for key in ("display", "active"): + values = histories[key] + if not isinstance(values, list) or len(values) != len(set(values)): + raise MigrationError("Invalid source history selection") + for value in values: + if value not in by_id: + raise MigrationError( + "Missing/invalid source history boundary reference: " + str(value) + ) + selected.append([by_id[value] for value in values]) + return tuple(selected) + + +def convert(item, sid): + """Normalized DeepSeek item -> (display rows, canonical history, warnings). + + Display rows feed the UI; canonical records are the model's continuation history. + Reasoning is display-only, tools are inert historical records, files are references. + """ + label = LABEL + display, canonical, warnings = [], [], list(item.get("warnings") or []) + turn = TURN_PREFIX + sid + zero_usage = { + "input": 0, + "output": 0, + "cacheRead": 0, + "cacheWrite": 0, + "totalTokens": 0, + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "total": 0}, + } + display_messages, active_messages = select_histories(item) + visible_ids = {msg["id"] for msg in display_messages} + active_ids = {msg["id"] for msg in active_messages} + canonical_by_id = {} + for msg in item["messages"]: + if msg["id"] not in visible_ids and msg["id"] not in active_ids: + continue + data = msg["data"] + role = data.get("role") + if role not in ("user", "assistant"): + raise MigrationError("Unsupported normalized role: " + str(role)) + mid = message_identity(sid, msg) + if role == "user": + turn = TURN_PREFIX + msg["id"] + texts, thoughts = [], [] + view_texts, view_tools = [], [] + for part in msg["parts"]: + p = part["data"] + typ = p.get("type") + if typ == "text": + if not isinstance(p.get("text"), str): + raise MigrationError("Invalid text part: " + part["id"]) + texts.append(p["text"]) + view_texts.append(p["text"]) + elif typ == "reasoning": + if isinstance(p.get("text"), str): + thoughts.append(p["text"]) + elif typ == "tool": + state = p.get("state") + if not isinstance(state, dict): + raise MigrationError("Invalid tool state: " + part["id"]) + evidence = { + "tool": p.get("tool"), + "callID": p.get("callID"), + "status": state.get("status"), + "input": state.get("input"), + } + for key in ("output", "error", "title"): + if key in state: + evidence[key] = state[key] + texts.append( + "[" + + label + + " 历史工具记录;仅供阅读,不重新执行]\n" + + compact(evidence) + ) + view_tools.append( + historical_tool_view(evidence, mid, len(view_tools), label) + ) + elif typ == "file": + view_texts.append(historical_file_text(p, label)) + texts.append("[" + label + " 历史附件引用]\n" + compact(p)) + warnings.append( + "Attachment reference retained; bytes not copied: " + part["id"] + ) + else: + raise MigrationError("Unsupported normalized part: " + str(typ)) + if data.get("error"): + texts.append("[" + label + " 历史错误]\n" + compact(data["error"])) + view_texts.append(texts[-1]) + text = "\n\n".join(texts) + ts = timestamp(msg["createdAtMs"]) + if msg["id"] in visible_ids and (text or thoughts): + view = { + "msg_id": mid, + "role": role, + "msg_type": 1, + "msg_content": "\n\n".join(view_texts), + "timestamp": ts, + "turnId": turn, + "turn_id": turn, + "query_key": turn, + "source": "desktop", + "canonical_message_id": mid, + } + if msg["id"] not in active_ids or not text: + view["displayKind"] = DISPLAY_KIND + if view_tools: + view["tool_calls"] = view_tools + if thoughts: + view["thinking_content"] = "\n\n".join(thoughts) + if role == "assistant": + view["finish_reason"] = "stop" + display.append(view) + if not text or msg["id"] not in active_ids: + continue + native = { + "role": role, + "content": [{"type": "text", "text": text}], + "timestamp": ts, + } + if role == "assistant": + native.update( + { + "api": "openai-completions", + "provider": PROVIDER, + "model": "historical-transcript", + "usage": zero_usage, + "stopReason": "stop", + } + ) + canonical_by_id[msg["id"]] = { + "message_id": mid, + "turn_id": turn, + "message": native, + } + canonical = [ + canonical_by_id[msg["id"]] + for msg in active_messages + if msg["id"] in canonical_by_id + ] + return display, canonical, warnings + + +def check_target(db, agent): + schema(db, TARGET_COLUMNS) + if not agent or any(x in agent for x in ("/", "\\", "\0")): + raise MigrationError("Invalid target agent name") + versions = { + r[0] + for r in db.execute( + "SELECT DISTINCT columnar_version FROM local_runtime_sessions" + ) + } + if versions - {0, 3}: + raise MigrationError("Unsupported target session columnar version") + for row in db.execute( + "SELECT session_id,record_json FROM local_runtime_sessions WHERE columnar_version=0" + ): + record = parse(row["record_json"], "legacy session " + row["session_id"]) + if record.get("sessionId") != row["session_id"]: + raise MigrationError( + "Invalid legacy session identity; retain it for runtime recovery" + ) + legacy_version = record.get("sessionDataVersion", 0) + if ( + isinstance(legacy_version, bool) + or not isinstance(legacy_version, int) + or legacy_version < 0 + ): + raise MigrationError("Invalid legacy session data version") + if legacy_version > 4: + raise MigrationError("Newer legacy session data contract detected") + if not db.execute( + "SELECT 1 FROM local_runtime_sessions WHERE agent_name=? AND columnar_version=3 LIMIT 1", + (agent,), + ).fetchone(): + raise MigrationError( + "Target agent has no existing session; initialize it in MiniMax Code first" + ) + future = db.execute( + "SELECT 1 FROM local_runtime_sessions WHERE json_extract(extra_data_json,'$.sessionDataVersion') > 4 LIMIT 1" + ).fetchone() + if future: + raise MigrationError( + "Newer session data contract detected; update the DeepSeek Chat Migration Mini App" + ) + return db.execute( + "SELECT COUNT(*) FROM local_runtime_sessions WHERE columnar_version=0" + ).fetchone()[0] + + +def plan(source, key, target, agent, items, workspace=None, restore_projects=False): + """Read-only preview of normalized items against the target. key: stable identity root.""" + with connect(target / "v2/sqlite/runtime-state.sqlite") as db: + legacy_count = check_target(db, agent) + return plan_in( + db, legacy_count, key, target, agent, items, workspace, restore_projects + ) + + +def plan_in( + db, legacy_count, key, target, agent, items, workspace=None, restore_projects=False +): + """plan() on an open read connection whose target was already checked; lets a + preview of many sessions share one snapshot instead of reconnecting per session.""" + planned = [] + for item in items: + s = item["session"] + sid = identity(key, s["id"]) + created = timestamp(s["time_created"]) + timestamp(s["time_updated"]) + cwd = str(Path(workspace or s["directory"]).expanduser().resolve()) + if not Path(cwd).is_dir(): + raise MigrationError("Missing workspace; use --workspace: " + cwd) + rel = history_dir(sid, created) + directory = safe_directory(target, "v2/sessions/" + rel) + display, canonical, warnings = [], [], [] + status = "new" + existing = db.execute( + "SELECT title,history_relative_dir,agent_name,created_at_ms,updated_at_ms FROM local_runtime_sessions WHERE session_id=?", + (sid,), + ).fetchone() + if existing: + marker = directory / MARKER + if not marker.is_file() or is_link(marker): + raise MigrationError( + "Existing target identity has no migration provenance: " + sid + ) + provenance = parse(marker.read_text(encoding="utf-8"), str(marker)) + if provenance.get("fingerprint") != item["fingerprint"]: + raise MigrationError( + "Source changed after migration; refusing to overwrite continued target session: " + + s["id"] + ) + if ( + existing["history_relative_dir"] != rel + or existing["agent_name"] != agent + or provenance.get("workspace") != cwd + ): + raise MigrationError("Existing migration target differs: " + sid) + regular(directory / "messages.jsonl") + # Readback counts describe persisted target state, including continued turns. + display = [ + dict(row) + for row in db.execute( + "SELECT msg_id FROM local_runtime_message_rows WHERE session_id=?", + (sid,), + ) + ] + with open(directory / "messages.jsonl", encoding="utf-8") as history: + canonical = [ + parse(line, str(directory / "messages.jsonl")) + for line in history + if line.strip() + ] + created = timestamp(existing["created_at_ms"]) + if provenance.get("adapterVersion", 1) < ADAPTER_VERSION: + warnings.append( + "Imported by an older adapter; existing history is unchanged and is not automatically repaired" + ) + if not db.execute( + "SELECT 1 FROM local_runtime_pi_history_file_migrations WHERE session_id=?", + (sid,), + ).fetchone(): + raise MigrationError( + "Existing import needs history repair (agent-session-migration repair --target-session): " + + sid + ) + status = "already-imported" + elif directory.exists(): + raise MigrationError( + "Uncommitted history directory exists; inspect recovery manifest before retry: " + + str(directory) + ) + project_state = None + restore_boundary = None + source_created = timestamp(s["time_created"]) + updated = timestamp( + existing["updated_at_ms"] if existing else s["time_updated"] + ) + if status == "new": + display, canonical, warnings = convert(item, sid) + project = db.execute( + "SELECT project_id,hidden,extra_data_json,updated_at_ms FROM local_runtime_projects WHERE project_kind='workspace' AND workspace_dir=?", + (cwd,), + ).fetchone() + if project: + project_state = dict(project) + removal = parse(project["extra_data_json"], "project extra data").get( + "sidebarRemoval" + ) + cutoff = ( + timestamp(removal["atMs"]) + if isinstance(removal, dict) and "atMs" in removal + else None + ) + if removal is not None and cutoff is None: + raise MigrationError("Unsupported project removal boundary") + if project["hidden"]: + latest = ( + db.execute( + "SELECT MAX(created_at_ms) FROM local_runtime_sessions WHERE project_id=?", + (project["project_id"],), + ).fetchone()[0] + or 0 + ) + cutoff = max( + int(time.time() * 1000), + project["updated_at_ms"], + cutoff or 0, + latest, + ) + at_boundary = [ + r[0] + for r in db.execute( + "SELECT session_id FROM local_runtime_sessions WHERE project_id=? AND created_at_ms=?", + (project["project_id"], cutoff), + ) + ] + restore_boundary = {"atMs": cutoff, "boundaryIds": at_boundary} + removed = cutoff is not None and ( + created < cutoff + or created == cutoff + and sid in (restore_boundary or removal).get("boundaryIds", []) + ) + if project["hidden"] or removed: + if not restore_projects: + raise MigrationError( + "Workspace project was removed; use --restore-projects only after explicitly accepting restoration: " + + cwd + ) + if removed: + created = max(int(time.time() * 1000), cutoff + 1) + updated = max(updated, created) + warnings.append( + "Project explicitly restored; original source timestamps remain in provenance and source archive" + ) + if legacy_count: + warnings.append( + "Preserved existing legacy columnar_version=0 rows: " + + str(legacy_count) + ) + source_title = item["desktop"]["title"] if item["desktop"] else s["title"] + prefix = "[From " + LABEL + "]" + title = source_title + if existing: + title = existing["title"] + elif not title.startswith(prefix): + title = prefix + " " + title + planned.append( + { + "sourceCreatedAtMs": source_created, + "sourceUpdatedAtMs": timestamp(s["time_updated"]), + "projectState": project_state, + "restoreBoundary": restore_boundary, + "canonicalMessageCount": len(canonical), + "sourceId": s["id"], + "targetId": sid, + "title": title, + "archived": bool( + item["desktop"]["archived"] + if item["desktop"] + else dict(s).get("time_archived") + ), + "workspace": cwd, + "historyRelativeDir": rel, + "createdAtMs": created, + "updatedAtMs": updated, + "fingerprint": item["fingerprint"], + "messageCount": len(display), + "warnings": warnings, + "status": status, + "item": item, + "display": display, + "canonical": canonical, + } + ) + return planned + + +def public_plan(rows): + return [ + { + k: v + for k, v in r.items() + if k + not in ("item", "display", "canonical", "projectState", "restoreBoundary") + } + for r in rows + ] + + +def insert(db, table, record): + columns = list(record) + db.execute( + "INSERT INTO " + + table + + " (" + + ",".join(columns) + + ") VALUES (" + + ",".join("?" for _ in columns) + + ")", + [record[x] for x in columns], + ) + + +@contextlib.contextmanager +def migration_lock(target): + root = safe_directory(target, LOCK_DIR) + root.mkdir(mode=0o700, exist_ok=True) + # Legacy releases use an exclusive-create lock; never remove their lock. + if (root / "apply.lock").exists(): + raise MigrationError( + "Legacy migration lock requires inspection: " + str(root / "apply.lock") + ) + path = root / "writer.lock" + if is_link(path): + raise MigrationError("Linked migration lock") + if path.exists(): + regular(path) + fd = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) + try: + if os.fstat(fd).st_size == 0: + os.write(fd, b"0") + os.lseek(fd, 0, os.SEEK_SET) + try: + if os.name == "nt": + import msvcrt + + msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) + else: + import fcntl + + fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError as exc: + raise MigrationError("Migration lock is held by another writer") from exc + yield + finally: + # Closing releases OS locks even after an abrupt process exit. Do not unlink + # the inode: another process may already be waiting on this same file. + os.close(fd) + + +def apply( + source, + key, + target, + agent, + items, + workspace=None, + restore_projects=False, + fail_after=None, +): + with migration_lock(target): + return _apply_locked( + source, key, target, agent, items, workspace, restore_projects, fail_after + ) + + +def _apply_locked( + source, + key, + target, + agent, + items, + workspace=None, + restore_projects=False, + fail_after=None, + journal=None, +): + # journal: the run's undo journal (deepseek_undo.Journal). No full database copy: + # an import only adds rows keyed by its own target ids, so the journal records + # those ids and the before-image of the one existing row it may update. + # Files precede the DB commit. A failed/crashed import never publishes partial DB rows. + # Orphan files are retained with an explicit recovery report, never mistaken for success. + lock = safe_directory(target, LOCK_DIR) + lock.mkdir(mode=0o700, exist_ok=True) + created_dirs = [] + committed = False + committing = False + prepared = False + report_file = None + new = [] + try: + rows = plan(source, key, target, agent, items, workspace, restore_projects) + new = [r for r in rows if r["status"] == "new"] + # DeepSeek attachments are migrated as references (name, size, export path); + # the Mini App accepts that by default and reports it as a warning. + if not new: + return {"status": "unchanged", "sessions": public_plan(rows)} + if journal is None: + import deepseek_undo + + journal = deepseek_undo.Journal.create(target, agent, source) + batch = journal.batch_dir() + report_file = batch / "report.json" + report = { + "format": FORMAT, + "state": "prepared", + "source": str(source), + "target": str(target), + "sessions": public_plan(rows), + "createdDirectories": [], + } + private_write(report_file, compact(report) + "\n") + dbfile = target / "v2/sqlite/runtime-state.sqlite" + with connect(dbfile, True) as db: + db.execute("BEGIN IMMEDIATE") + check_target(db, agent) + # Under the write lock and before any write: intent and before-images. + journal.prepare(db, target, new) + prepared = True + checked_projects = set() + for r in new: + if db.execute( + "SELECT 1 FROM local_runtime_sessions WHERE session_id=?", + (r["targetId"],), + ).fetchone(): + raise MigrationError("Concurrent target identity conflict") + directory = safe_directory( + target, "v2/sessions/" + r["historyRelativeDir"] + ) + directory.mkdir(mode=0o700, parents=True, exist_ok=False) + created_dirs.append(directory) + provenance = { + k: r[k] + for k in ( + "sourceId", + "targetId", + "fingerprint", + "workspace", + "sourceCreatedAtMs", + "sourceUpdatedAtMs", + ) + } + provenance.update( + { + "format": FORMAT, + "adapterVersion": ADAPTER_VERSION, + "sourceType": SOURCE_TYPE, + "source": str(source), + "importedAtMs": int(time.time() * 1000), + "undoRun": journal.run_id, + } + ) + private_write(directory / MARKER, compact(provenance) + "\n") + private_write(directory / ARCHIVE, compact(r["item"]) + "\n") + private_write( + directory / "messages.jsonl", + "".join(compact(x) + "\n" for x in r["canonical"]), + ) + private_write( + directory / "manifest.json", + compact( + { + "schemaVersion": 1, + "sessionId": r["targetId"], + "createdAtMs": r["createdAtMs"], + "updatedAtMs": r["updatedAtMs"], + "source": "local-runtime", + "layout": "v2-final-dated-session", + "paths": { + "sessionDir": str(directory), + "messages": str(directory / "messages.jsonl"), + "reports": str(directory / "reports"), + }, + } + ) + + "\n", + ) + extra = { + "appMode": "coding", + "sessionDataVersion": 4, + "sessionOrigin": "local-runtime", + "origin": "user", + } + session = { + "sessionId": r["targetId"], + "agentName": agent, + "workspaceDir": r["workspace"], + "runtime": "pi-agent", + "sessionType": "branch", + "sessionKind": "conversation", + "archived": r["archived"], + "visibility": "visible", + "title": r["title"], + "status": "idle", + "createdAtMs": r["createdAtMs"], + "updatedAtMs": r["updatedAtMs"], + "historyRelativeDir": r["historyRelativeDir"], + **extra, + } + project = db.execute( + "SELECT project_id FROM local_runtime_projects WHERE project_kind='workspace' AND workspace_dir=?", + (r["workspace"],), + ).fetchone() + if ( + r["workspace"] not in checked_projects + and r["projectState"] is not None + ): + current = db.execute( + "SELECT project_id,hidden,extra_data_json,updated_at_ms FROM local_runtime_projects WHERE project_id=?", + (r["projectState"]["project_id"],), + ).fetchone() + if current is None or dict(current) != r["projectState"]: + raise MigrationError( + "Project changed during migration; preview again" + ) + elif r["workspace"] not in checked_projects and project: + raise MigrationError( + "Project appeared during migration; preview again" + ) + if not project: + insert( + db, + "local_runtime_projects", + { + "project_kind": "workspace", + "workspace_dir": r["workspace"], + "created_at_ms": r["createdAtMs"], + "updated_at_ms": r["updatedAtMs"], + }, + ) + project = db.execute( + "SELECT project_id FROM local_runtime_projects WHERE project_kind='workspace' AND workspace_dir=?", + (r["workspace"],), + ).fetchone() + if ( + r["workspace"] not in checked_projects + and r["restoreBoundary"] is not None + ): + project_extra = parse( + r["projectState"]["extra_data_json"], "project extra data" + ) + project_extra["sidebarRemoval"] = r["restoreBoundary"] + db.execute( + "UPDATE local_runtime_projects SET extra_data_json=? WHERE project_id=?", + (compact(project_extra), project[0]), + ) + checked_projects.add(r["workspace"]) + if restore_projects: + db.execute( + "UPDATE local_runtime_projects SET hidden=0 WHERE project_id=? AND hidden=1", + (project[0],), + ) + insert( + db, + "local_runtime_sessions", + { + "session_id": r["targetId"], + "record_json": compact(session), + "updated_at_ms": r["updatedAtMs"], + "columnar_version": 3, + "agent_name": agent, + "runtime": "pi-agent", + "session_type": "branch", + "status": "idle", + "archived": int(r["archived"]), + "visibility": "visible", + "session_kind": "conversation", + "purpose_kind": "", + "workspace_dir": r["workspace"], + "project_workspace_dir": r["workspace"], + "is_default_workspace": 0, + "title": r["title"], + "created_at_ms": r["createdAtMs"], + "extra_data_json": compact(extra), + "project_id": project[0], + "history_relative_dir": r["historyRelativeDir"], + }, + ) + write_search_document(db, r, agent) + for msg in r["display"]: + insert( + db, + "local_runtime_message_rows", + { + "session_id": r["targetId"], + "msg_id": msg["msg_id"], + "role": msg["role"], + "turn_id": msg["turnId"], + "source": "desktop", + "created_at_ms": msg["timestamp"], + "data_json": compact(msg), + }, + ) + initialize_history(db, r["targetId"], r["canonical"]) + if fail_after is not None and len(created_dirs) >= fail_after: + raise MigrationError("Injected transaction failure") + committing = True + db.commit() + committed = True + report.update( + {"state": "committed", "createdDirectories": [str(p) for p in created_dirs]} + ) + temp = report_file.with_suffix(".complete") + private_write(temp, compact(report) + "\n") + os.replace(temp, report_file) + # Read-back is independent from the write transaction. + with connect(dbfile) as check: + for row in new: + count = check.execute( + "SELECT COUNT(*) FROM local_runtime_message_rows WHERE session_id=?", + (row["targetId"],), + ).fetchone()[0] + if count < row["messageCount"]: + raise MigrationError( + "Committed import failed message readback; see " + + str(report_file) + ) + if not check.execute( + "SELECT 1 FROM local_runtime_sessions WHERE session_id=?", + (row["targetId"],), + ).fetchone(): + raise MigrationError("Committed session missing during readback") + row["status"] = "already-imported" + journal.committed(check, target, new) + verified = rows + return { + "status": "imported", + "imported": len(new), + "undo": str(journal.path), + "report": str(report_file), + "sessions": public_plan(verified), + } + except BaseException as exc: + if prepared and not committed and not committing: + try: + journal.rolled_back(new) + except OSError: + pass + if not committed and not committing: + # Only remove files this invocation just created, never historical/user-owned dirs. + for directory in reversed(created_dirs): + for filename in ( + MARKER, + ARCHIVE, + "messages.jsonl", + "manifest.json", + ): + (directory / filename).unlink(missing_ok=True) + directory.rmdir() + if committed: + raise MigrationError( + "Import COMMITTED, but final readback/report failed; inspect " + + str(report_file) + + "; do not restore the whole DB: " + + str(exc) + ) from exc + if committing: + raise MigrationError( + "Commit outcome uncertain; history files preserved. Inspect " + + str(report_file) + ) from exc + raise + + +def search_terms(value): + return " ".join( + "c" + format(ord(char), "x") + for char in unicodedata.normalize("NFKC", value or "").strip().lower() + ) + + +def write_search_document(db, row, agent): + db.execute( + "INSERT INTO local_runtime_session_fts_keys(session_id) VALUES (?)", + (row["targetId"],), + ) + key = db.execute( + "SELECT fts_rowid FROM local_runtime_session_fts_keys WHERE session_id=?", + (row["targetId"],), + ).fetchone()[0] + values = [ + row["targetId"], + agent, + row["title"], + row["workspace"], + "", + "idle", + "branch", + ] + db.execute( + "INSERT INTO local_runtime_sessions_fts(rowid,session_id,session_id_terms,agent_name_terms,title_terms,workspace_dir_terms,purpose_terms,status_terms,session_type_terms) VALUES (?,?,?,?,?,?,?,?,?)", + [key, row["targetId"]] + [search_terms(value) for value in values], + ) + + +def print_result(value, file=None): + # JSON escapes round-trip UTF-8 text even through a legacy GBK PowerShell pipe. + print( + json.dumps(value, ensure_ascii=True, separators=(",", ":"), allow_nan=False), + file=file, + ) diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_export.py b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_export.py new file mode 100644 index 00000000..5573fdae --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_export.py @@ -0,0 +1,573 @@ +"""DeepSeek official export reader for the DeepSeek Chat Migration Mini App. Python 3.9+. + +Source layout (DeepSeek web "export data", export_format_version 3), either the +downloaded zip itself or a folder it was unpacked into: + manifest.json {"export_format_version": 3} + user.json account record; only user_id is read (stable identity) + chat_session_index.json [{id, title, pinned, message_count, inserted_at, updated_at}] + chat_sessions/.json {id, title, pinned, current_message_id, inserted_at, + updated_at, messages[]} + chat_sessions/_files/... uploaded attachment bytes (never copied) + +Each message is a tree node {id, parent_id, role USER|ASSISTANT, status, fragments[]}. +Regenerated answers and edited questions are sibling branches; the branch the user +last looked at ends at current_message_id. This module only reads and normalizes. It +emits the ZCode-shaped item the shared `migrate.convert` consumes: + + REQUEST -> user text + RESPONSE / TEMPLATE_RESPONSE -> assistant text + THINK -> reasoning part (display only, never model context) + TOOL_SEARCH -> read-only historical tool record (queries + results) + CONTENT_RISK -> labeled text notice + FILE -> attachment reference; bytes are not copied + status != FINISHED -> historical error note on that message + +Only the active branch (root to current_message_id) is shown and becomes model +context, matching what DeepSeek displayed last; regenerated answers and edited +questions on other branches stay in the per-session source archive. +The zip is never extracted or modified. Sessions have no working directory: they +are associated with the `~/DeepSeek` workspace project unless --workspace is given. +Adapted from the DeepSeek source written for agent-session-migration; ids and +fingerprints are identical so either tool recognizes the other's imports. +""" + +import json +import zipfile +from datetime import datetime, timezone +from pathlib import Path + +from deepseek_core import MigrationError, digest, is_link + +FORMAT_VERSION = 3 +MAX_ENTRY = ( + 64 * 1024 * 1024 +) # per JSON entry; larger entries are refused, not truncated +MAX_TEXT = 1024 * 1024 # per text field; longer values are truncated with a marker +WORKSPACE_NAME = "DeepSeek" +TEXT_FRAGMENTS = {"REQUEST", "RESPONSE", "TEMPLATE_RESPONSE"} +SEARCH_RESULT_KEYS = ("title", "url", "site_name", "published_at", "snippet") + + +def ms(value, fallback=0): + """ISO-8601 or epoch -> unix milliseconds. Degrades to fallback, never raises.""" + if isinstance(value, bool): + return fallback + if isinstance(value, (int, float)): + number = int(value) + return number if number > 10_000_000_000 else number * 1000 + if not isinstance(value, str) or not value: + return fallback + try: + parsed = datetime.fromisoformat(value.replace("Z", "+00:00")) + except ValueError: + return fallback + if parsed.tzinfo is None: + parsed = parsed.replace(tzinfo=timezone.utc) + return int(parsed.timestamp() * 1000) + + +def clip(text, counter=None): + if not isinstance(text, str): + return text + if len(text) > MAX_TEXT: + if counter is not None: + counter[0] += 1 + return text[:MAX_TEXT] + "\n[truncated by migration]" + return text + + +def default_workspace(): + return str(Path.home() / WORKSPACE_NAME) + + +class Export: + """Read-only view over the zip or its unpacked folder.""" + + def __init__(self, root): + self.root = root + self.zip = zipfile.ZipFile(root) if root.is_file() else None + + def close(self): + if self.zip is not None: + self.zip.close() + + def __enter__(self): + return self + + def __exit__(self, *exc): + self.close() + + def names(self): + if self.zip is not None: + return [i.filename for i in self.zip.infolist() if not i.is_dir()] + base = self.root / "chat_sessions" + found = ["manifest.json", "user.json", "chat_session_index.json"] + if base.is_dir() and not is_link(base): + found += [ + "chat_sessions/" + p.name + for p in base.iterdir() + if p.is_file() and not is_link(p) + ] + return found + + def size(self, name): + if self.zip is not None: + try: + return self.zip.getinfo(name).file_size + except KeyError: + return None + path = self.root / name + return path.stat().st_size if path.is_file() and not is_link(path) else None + + def has(self, name): + if self.zip is not None: + try: + self.zip.getinfo(name) + return True + except KeyError: + return False + path = self.root / name + return path.exists() and not is_link(path) + + def json(self, name, required=True): + size = self.size(name) + if size is None: + if required: + raise MigrationError("DeepSeek export is missing " + name) + return None + if size > MAX_ENTRY: + raise MigrationError("Oversized DeepSeek export entry: " + name) + try: + if self.zip is not None: + raw = self.zip.read(name) + else: + raw = (self.root / name).read_bytes() + return json.loads(raw.decode("utf-8")) + except (ValueError, UnicodeDecodeError, zipfile.BadZipFile) as exc: + raise MigrationError("Malformed JSON in DeepSeek export: " + name) from exc + + +def source_path(value=None): + """The DeepSeek source is the official export zip (or its unpacked folder).""" + if not value: + raise MigrationError( + "DeepSeek requires --source " + ) + root = Path(value).expanduser().absolute() + if is_link(root) or not (root.is_file() or root.is_dir()): + raise MigrationError( + "DeepSeek export must be an existing non-symlink zip file or folder: " + + str(root) + ) + root = root.resolve() + if root.is_file() and not zipfile.is_zipfile(root): + raise MigrationError("DeepSeek export file is not a zip archive: " + str(root)) + with Export(root) as export: + manifest = export.json("manifest.json") + if not isinstance(manifest, dict): + raise MigrationError("Invalid DeepSeek manifest.json") + version = manifest.get("export_format_version") + if version != FORMAT_VERSION: + raise MigrationError( + "Unsupported DeepSeek export_format_version: %r (supported: %d)" + % (version, FORMAT_VERSION) + ) + return root + + +def identity_root(source): + """Stable target identity across re-uploads of the same account's exports. + + The Mini App stores every uploaded zip under a fresh temporary path; keying target + identity on that path would let the same conversation import twice. The account + id is hashed into the target id and never written in clear text. + """ + with Export(source) as export: + user = export.json("user.json", required=False) + uid = user.get("user_id") if isinstance(user, dict) else None + if isinstance(uid, (str, int)) and not isinstance(uid, bool) and str(uid): + return "deepseek-account:" + str(uid) + return str(source.resolve()) + + +def session_entry(sid): + if ( + not isinstance(sid, str) + or not sid + or len(sid) > 256 + or any(c in sid for c in "/\\\0") + or sid in (".", "..") + ): + raise MigrationError("Invalid DeepSeek session identity: " + repr(sid)[:80]) + return "chat_sessions/" + sid + ".json" + + +def discover(export, errors=None): + """Index ids plus any session files the index omits. Returns {id: index row}.""" + index = export.json("chat_session_index.json") + if not isinstance(index, list): + raise MigrationError("Invalid DeepSeek chat_session_index.json") + rows, duplicates = {}, set() + for row in index: + if not isinstance(row, dict): + raise MigrationError("Invalid DeepSeek chat_session_index.json row") + sid = row.get("id") + session_entry(sid) + if sid in rows: + duplicates.add(sid) + rows[sid] = row + for name in export.names(): + if ( + name.startswith("chat_sessions/") + and name.endswith(".json") + and name.count("/") == 1 + ): + sid = name[len("chat_sessions/") : -len(".json")] + if sid and sid not in rows: + rows[sid] = {"id": sid} + for sid in sorted(duplicates): + error = "Duplicate DeepSeek session identity in index: " + sid + if errors is None: + raise MigrationError(error) + errors.append({"sourceId": sid, "error": error}) + rows.pop(sid, None) + return rows + + +def active_branch(session, by_id): + """current_message_id -> root, returned root-first. Cycles and gaps fail closed.""" + current = session.get("current_message_id") + chain, seen = [], set() + while current is not None: + if current in seen: + raise MigrationError("DeepSeek message tree has a cycle") + if current not in by_id: + raise MigrationError( + "DeepSeek current branch references a missing message: " + str(current) + ) + seen.add(current) + chain.append(current) + current = by_id[current].get("parent_id") + chain.reverse() + return chain + + +def search_output(results): + lines = [] + for number, result in enumerate(results or [], 1): + if not isinstance(result, dict): + continue + # site_icon is an image URL/payload with no reading value; it is not migrated. + head = "[%d] %s" % (number, result.get("title") or "") + body = [head.rstrip()] + for key in SEARCH_RESULT_KEYS[1:]: + value = result.get(key) + if value not in (None, ""): + body.append("%s: %s" % (key, value)) + lines.append("\n".join(body)) + return "\n\n".join(lines) + + +def read_session(export, sid, index_row=None): + """One DeepSeek session -> (session meta, normalized messages, histories, warnings).""" + name = session_entry(sid) + raw = export.json(name) + if not isinstance(raw, dict): + raise MigrationError("Invalid DeepSeek session record: " + sid) + if raw.get("id") != sid: + raise MigrationError( + "DeepSeek session identity does not match its file: " + sid + ) + source_messages = raw.get("messages") + if not isinstance(source_messages, list): + raise MigrationError("DeepSeek session has no message list: " + sid) + by_id = {} + for msg in source_messages: + if not isinstance(msg, dict): + raise MigrationError("Invalid DeepSeek message record") + mid = msg.get("id") + if isinstance(mid, bool) or not isinstance(mid, (int, str)) or mid == "": + raise MigrationError("Invalid DeepSeek message identity") + if mid in by_id: + raise MigrationError("Duplicate DeepSeek message identity: " + str(mid)) + if msg.get("role") not in ("USER", "ASSISTANT"): + raise MigrationError( + "Unsupported DeepSeek message role: " + str(msg.get("role")) + ) + if not isinstance(msg.get("fragments"), list): + raise MigrationError("DeepSeek message has no fragment list: " + str(mid)) + parent = msg.get("parent_id") + if parent is not None and ( + isinstance(parent, bool) or not isinstance(parent, (int, str)) + ): + raise MigrationError("Invalid DeepSeek parent identity: " + str(mid)) + by_id[mid] = msg + for msg in source_messages: + parent = msg.get("parent_id") + if parent is not None and parent not in by_id: + raise MigrationError( + "DeepSeek message references a missing parent: " + str(msg["id"]) + ) + chain = active_branch(raw, by_id) + created = ms(raw.get("inserted_at")) + warnings, counters = [], {"truncated": [0], "files": 0, "unknown": {}} + messages = [] + # Active branch first, root to leaf, so the shared converter assigns every reply + # to its own question. Inactive branches follow chronologically, archive-only. + on_branch = set(chain) + rest = sorted( + (pair for pair in enumerate(source_messages) if pair[1]["id"] not in on_branch), + key=lambda pair: (ms(pair[1].get("inserted_at"), created), pair[0]), + ) + ordered = [by_id[x] for x in chain] + [msg for _, msg in rest] + for msg in ordered: + key = str(msg["id"]) + at = ms(msg.get("inserted_at"), created) + role = "user" if msg["role"] == "USER" else "assistant" + parts = [] + for number, fragment in enumerate(msg["fragments"]): + pid = key + "-" + str(number) + if not isinstance(fragment, dict): + raise MigrationError("Invalid DeepSeek fragment in message " + key) + typ = fragment.get("type") + content = fragment.get("content") + if typ in TEXT_FRAGMENTS: + if not isinstance(content, str): + raise MigrationError("Invalid DeepSeek text fragment: " + pid) + data = {"type": "text", "text": clip(content, counters["truncated"])} + elif typ == "THINK": + if not isinstance(content, str): + raise MigrationError("Invalid DeepSeek thinking fragment: " + pid) + data = { + "type": "reasoning", + "text": clip(content, counters["truncated"]), + } + elif typ == "TOOL_SEARCH": + queries = fragment.get("queries") + results = fragment.get("results") + if queries is not None and not isinstance(queries, list): + raise MigrationError("Invalid DeepSeek search queries: " + pid) + if results is not None and not isinstance(results, list): + raise MigrationError("Invalid DeepSeek search results: " + pid) + data = { + "type": "tool", + "tool": "web_search", + "callID": "deepseek-search-" + pid, + "state": { + "status": str(fragment.get("status") or "completed").lower(), + "input": {"queries": queries or []}, + "output": clip(search_output(results), counters["truncated"]), + }, + } + elif typ == "CONTENT_RISK": + notice = "[DeepSeek 内容风险提示" + if fragment.get("style"): + notice += "(" + str(fragment["style"]) + ")" + notice += "]" + if isinstance(content, str) and content: + notice += "\n" + clip(content, counters["truncated"]) + data = {"type": "text", "text": notice} + elif typ == "FILE": + files = fragment.get("files") + if not isinstance(files, list): + raise MigrationError("Invalid DeepSeek file fragment: " + pid) + for index, item in enumerate(files): + if not isinstance(item, dict): + raise MigrationError("Invalid DeepSeek file record: " + pid) + reference = { + "type": "file", + "filename": item.get("file_name") or "附件", + "mime": "image" if item.get("is_image") else None, + "size": item.get("file_size"), + "path": item.get("export_path"), + "inExport": bool( + isinstance(item.get("export_path"), str) + and export.has(item["export_path"]) + ), + } + parts.append( + { + "id": pid + "-" + str(index), + "createdAtMs": ms(item.get("inserted_at"), at), + "data": { + k: v for k, v in reference.items() if v is not None + }, + } + ) + counters["files"] += 1 + continue + else: + counters["unknown"][str(typ)] = counters["unknown"].get(str(typ), 0) + 1 + continue + parts.append({"id": pid, "createdAtMs": at, "data": data}) + data = {"role": role} + status = msg.get("status") + if status not in (None, "FINISHED"): + data["error"] = {"status": status} + messages.append({"id": key, "data": data, "createdAtMs": at, "parts": parts}) + active = [str(x) for x in chain] + active_set = set(active) + off_branch = sum(1 for msg in messages if msg["id"] not in active_set) + if off_branch: + warnings.append( + "DeepSeek inactive branches retained only in source archive: %d messages" + % off_branch + ) + if counters["truncated"][0]: + warnings.append( + "Truncated %d oversized DeepSeek text fields" % counters["truncated"][0] + ) + for typ, count in sorted(counters["unknown"].items()): + warnings.append( + "Unsupported DeepSeek fragment retained only in source archive: %s x%d" + % (typ, count) + ) + index_row = index_row or {} + title = raw.get("title") if isinstance(raw.get("title"), str) else None + title = (title or index_row.get("title") or "").strip() + if not title: + for msg in messages: + if msg["data"]["role"] != "user": + continue + text = next( + ( + p["data"]["text"] + for p in msg["parts"] + if p["data"].get("type") == "text" and p["data"]["text"].strip() + ), + "", + ) + if text: + title = text.strip().splitlines()[0][:80] + break + meta = { + "id": sid, + "title": (title or "DeepSeek session " + sid[:8])[:200], + "created": created, + "updated": ms(raw.get("updated_at"), created), + "pinned": bool(raw.get("pinned")), + } + return meta, messages, {"display": active, "active": active}, warnings + + +def inventory(root): + root = source_path(root) + errors = [] + with Export(root) as export: + rows = discover(export, errors) + ids, sizes = [], {} + for sid in sorted(rows): + try: + size = export.size(session_entry(sid)) + except MigrationError as exc: + errors.append({"sourceId": sid, "error": str(exc)}) + continue + if size is None: + errors.append( + { + "sourceId": sid, + "error": "DeepSeek session file is missing: " + sid, + } + ) + continue + ids.append(sid) + sizes[sid] = size + return { + "root": root, + "ids": ids, + "index": rows, + "errors": errors, + "bytes": sum(sizes.values()), + } + + +def load_source(root, ids=None): + """Same contract as migrate.load_source: a list of fingerprinted items.""" + data = inventory(root) + if ids: + unknown = sorted(set(ids) - set(data["ids"])) + if unknown: + raise MigrationError("Unknown DeepSeek session IDs: " + ", ".join(unknown)) + wanted = list(ids) + else: + failed = {e.get("sourceId") for e in data["errors"]} + wanted = [sid for sid in data["ids"] if sid not in failed] + return load_inventory(data, wanted, selected=bool(ids)) + + +def load_inventory(data, ids, selected=True, export=None): + """export: an open Export of data["root"] to reuse; otherwise one is opened here.""" + if export is None: + with Export(data["root"]) as opened: + return load_inventory(data, ids, selected, opened) + result = [] + for sid in sorted(set(ids)): + if sid not in data["index"]: + raise MigrationError("Unknown DeepSeek session ID: " + sid) + meta, messages, histories, warnings = read_session( + export, sid, data["index"].get(sid) + ) + if not messages or not histories["active"]: + if selected: + raise MigrationError( + "Selected DeepSeek session has no readable messages: " + sid + ) + continue + updated = max( + [meta["updated"], meta["created"]] + + [msg["createdAtMs"] for msg in messages] + ) + item = { + "session": { + "id": sid, + "directory": default_workspace(), + "title": meta["title"], + "time_created": meta["created"] or messages[0]["createdAtMs"], + "time_updated": updated, + "pinned": meta["pinned"], + }, + "messages": messages, + "histories": histories, + "desktop": None, + "warnings": warnings, + } + item["fingerprint"] = digest(item) + result.append(item) + result.sort(key=lambda x: (x["session"]["time_created"], x["session"]["id"])) + return result + + +def overview(root, ids=None): + """Cheap listing for `scan`: metadata from chat_session_index.json only.""" + root = source_path(root) + workspace = default_workspace() + rows = [] + with Export(root) as export: + index = discover(export) + wanted = set(ids or index) + unknown = sorted(wanted - set(index)) + if unknown: + raise MigrationError("Unknown DeepSeek session IDs: " + ", ".join(unknown)) + for sid in sorted(wanted): + row = index[sid] + title = row.get("title") + size = export.size(session_entry(sid)) + rows.append( + { + "id": sid, + "title": title.strip() + if isinstance(title, str) and title.strip() + else None, + "workspace": workspace, + "workspaceExists": Path(workspace).is_dir(), + "createdAt": ms(row.get("inserted_at")), + "updatedAt": ms(row.get("updated_at")), + "messageCount": row.get("message_count"), + "pinned": bool(row.get("pinned")), + "archived": False, + "bytes": size or 0, + "missing": size is None, + } + ) + rows.sort(key=lambda row: row["updatedAt"] or 0, reverse=True) + return rows diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_undo.py b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_undo.py new file mode 100644 index 00000000..2e728a65 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/deepseek_undo.py @@ -0,0 +1,441 @@ +"""Undo journal for DeepSeek imports: what one apply run added, and how to take it back. + +An import only adds rows keyed by its own target session ids, so instead of copying the +whole database first (gigabytes on a long-used install) each run keeps a small journal: + +- before a batch writes, inside its write transaction: the target ids, the history + directories it will create, and the before-image of the one pre-existing row it may + update (the DeepSeek workspace project: hidden, extra_data_json, counters); +- after the batch commits and is read back: the ids of every inserted row and a + fingerprint of each session (record, message rows, history file) to detect later use. + +`undo` removes exactly those sessions and rows, restores the project before-image when +the project still looks the way the import left it, deletes the history files it wrote, +and refuses when a session changed after import (the user kept chatting) unless forced. +""" + +import hashlib +import os +import time +import uuid +from pathlib import Path + +import deepseek_core as m + +UNDO_DIR = "deepseek-migration" +JOURNAL = "undo.json" +JOURNAL_FORMAT = "deepseek-undo/1" +OWN_FILES = {m.MARKER, m.ARCHIVE, "messages.jsonl", "manifest.json"} +PROJECT_COLUMNS = ( + "project_id", + "hidden", + "extra_data_json", + "latest_activity_at_ms", + "session_count", + "updated_at_ms", +) + + +class UndoRefused(m.MigrationError): + def __init__(self, message, sessions): + super().__init__(message) + self.sessions = sessions + + +def sha256_file(path): + digest = hashlib.sha256() + with open(path, "rb") as handle: + for chunk in iter(lambda: handle.read(1 << 20), b""): + digest.update(chunk) + return digest.hexdigest() + + +def session_state(db, target, sid, relative): + """What later use of a session would change: its record, rows and history file.""" + row = db.execute( + "SELECT record_json,updated_at_ms,title,archived,status FROM local_runtime_sessions WHERE session_id=?", + (sid,), + ).fetchone() + if row is None: + return None + count, highest = db.execute( + "SELECT COUNT(*),MAX(id) FROM local_runtime_message_rows WHERE session_id=?", + (sid,), + ).fetchone() + directory = target / "v2/sessions" / relative + history = directory / "messages.jsonl" + files = sorted(p.name for p in directory.iterdir()) if directory.is_dir() else None + return { + "record": hashlib.sha256(row["record_json"].encode("utf-8")).hexdigest(), + "updatedAtMs": row["updated_at_ms"], + "title": row["title"], + "archived": row["archived"], + "status": row["status"], + "messageRows": count, + "maxMessageRowId": highest, + "history": sha256_file(history) if history.is_file() else None, + "files": files, + } + + +def project_row(db, workspace): + present = { + r["name"] for r in db.execute('PRAGMA table_info("local_runtime_projects")') + } + columns = [c for c in PROJECT_COLUMNS if c in present] + row = db.execute( + "SELECT " + + ",".join(columns) + + " FROM local_runtime_projects WHERE project_kind='workspace' AND workspace_dir=?", + (workspace,), + ).fetchone() + return dict(row) if row else None + + +class Journal: + def __init__(self, path, data): + self.path = path + self.dir = path.parent + self.data = data + + @classmethod + def create(cls, target, agent, source): + root = m.safe_directory(target, UNDO_DIR) + root.mkdir(mode=0o700, exist_ok=True) + run = str(int(time.time() * 1000)) + "-" + uuid.uuid4().hex[:8] + directory = m.safe_directory(target, UNDO_DIR + "/" + run) + directory.mkdir(mode=0o700) + journal = cls( + directory / JOURNAL, + { + "format": JOURNAL_FORMAT, + "runId": run, + "state": "active", + "target": str(target), + "agent": agent, + "source": str(source), + "createdAtMs": int(time.time() * 1000), + "projects": {}, + "sessions": [], + }, + ) + journal.save() + return journal + + @classmethod + def load(cls, path, target): + path = Path(path).expanduser().absolute() + if path.is_dir(): + path = path / JOURNAL + path = m.regular(path) + data = m.parse(path.read_text(encoding="utf-8"), str(path)) + if data.get("format") != JOURNAL_FORMAT or data.get("target") != str(target): + raise m.MigrationError( + "Undo journal does not belong to this data directory" + ) + if path.parent.parent != m.safe_directory(target, UNDO_DIR): + raise m.MigrationError("Undo journal is outside " + UNDO_DIR) + return cls(path, data) + + @property + def run_id(self): + return self.data["runId"] + + def save(self): + temporary = self.path.with_name( + self.path.name + "." + uuid.uuid4().hex + ".tmp" + ) + try: + m.private_write(temporary, m.compact(self.data) + "\n") + os.replace(temporary, self.path) + finally: + temporary.unlink(missing_ok=True) + + def batch_dir(self): + directory = self.dir / ( + "batch-" + str(int(time.time() * 1000)) + "-" + uuid.uuid4().hex[:8] + ) + directory.mkdir(mode=0o700) + return directory + + def prepare(self, db, target, rows): + """Inside the batch's write transaction, before any write: intent + before-images.""" + sessions_root = target / "v2/sessions" + for r in rows: + workspace = r["workspace"] + if workspace not in self.data["projects"]: + before = project_row(db, workspace) + self.data["projects"][workspace] = { + "existed": before is not None, + "before": before, + "after": None, + } + created = [] + path = sessions_root + for part in Path(r["historyRelativeDir"]).parts: + path = path / part + if not path.exists(): + created.append(str(path)) + self.data["sessions"].append( + { + "targetId": r["targetId"], + "sourceId": r["sourceId"], + "historyRelativeDir": r["historyRelativeDir"], + "workspace": workspace, + "createdDirs": created, + "status": "pending", + } + ) + self.save() + + def _entries(self, rows): + ids = {r["targetId"] for r in rows} + return [ + e + for e in self.data["sessions"] + if e["targetId"] in ids and e["status"] == "pending" + ] + + def committed(self, db, target, rows): + """After commit and read-back: inserted row ids and a fingerprint per session.""" + for entry in self._entries(rows): + sid = entry["targetId"] + entry["rows"] = { + "messageRowIds": [ + r[0] + for r in db.execute( + "SELECT id FROM local_runtime_message_rows WHERE session_id=? ORDER BY id", + (sid,), + ) + ], + "ftsRowids": [ + r[0] + for r in db.execute( + "SELECT fts_rowid FROM local_runtime_session_fts_keys WHERE session_id=?", + (sid,), + ) + ], + "projectId": db.execute( + "SELECT project_id FROM local_runtime_sessions WHERE session_id=?", + (sid,), + ).fetchone()[0], + "historyFileMigration": True, + } + entry["fingerprint"] = session_state( + db, target, sid, entry["historyRelativeDir"] + ) + entry["status"] = "committed" + for workspace, project in self.data["projects"].items(): + after = project_row(db, workspace) + if after is not None: + project["after"] = { + k: after[k] for k in ("project_id", "hidden", "extra_data_json") + } + self.save() + + def rolled_back(self, rows): + for entry in self._entries(rows): + entry["status"] = "rolled-back" + self.save() + + +def changes(saved, current): + return sorted(k for k in saved if saved.get(k) != current.get(k)) + + +def undo(target, journal_path, force=False): + """Remove what one apply run added. Idempotent; refuses on later use unless forced.""" + target = m.target_path(target) + with m.migration_lock(target): + journal = Journal.load(journal_path, target) + return _undo_locked(target, journal, force) + + +def _undo_locked(target, journal, force): + dbfile = target / "v2/sqlite/runtime-state.sqlite" + entries = [ + e + for e in journal.data["sessions"] + if e["status"] in ("committed", "pending", "undone") + ] + removed, absent, foreign, modified, warnings = [], [], [], [], [] + with m.connect(dbfile, True) as db: + db.execute("BEGIN IMMEDIATE") + for entry in entries: + sid = entry["targetId"] + row = db.execute( + "SELECT history_relative_dir,agent_name FROM local_runtime_sessions WHERE session_id=?", + (sid,), + ).fetchone() + if row is None: + absent.append(entry) + continue + if ( + row["history_relative_dir"] != entry["historyRelativeDir"] + or row["agent_name"] != journal.data["agent"] + ): + raise m.MigrationError("Undo target identity conflict: " + sid) + marker = target / "v2/sessions" / entry["historyRelativeDir"] / m.MARKER + owner = None + if marker.is_file(): + owner = m.parse(marker.read_text(encoding="utf-8"), str(marker)).get( + "undoRun" + ) + if owner not in (None, journal.run_id): + if entry["status"] == "undone": + # Undone here, then imported again by a later run: not ours now. + foreign.append(entry) + continue + raise m.MigrationError("Session belongs to another import run: " + sid) + current = session_state(db, target, sid, entry["historyRelativeDir"]) + if entry["status"] != "committed": + modified.append( + { + "targetId": sid, + "sourceId": entry["sourceId"], + "changed": ["unverified"], + } + ) + else: + changed = changes(entry["fingerprint"], current) + if changed: + modified.append( + { + "targetId": sid, + "sourceId": entry["sourceId"], + "changed": changed, + } + ) + removed.append(entry) + if modified and not force: + db.rollback() + raise UndoRefused( + str(len(modified)) + + " imported session(s) changed after import (for example the conversation " + "was continued); nothing was undone. Repeat with --force to remove them anyway", + modified, + ) + for entry in removed: + sid = entry["targetId"] + db.execute( + "DELETE FROM local_runtime_message_rows WHERE session_id=?", (sid,) + ) + db.execute( + "DELETE FROM local_runtime_pi_history_file_migrations WHERE session_id=?", + (sid,), + ) + for (rowid,) in db.execute( + "SELECT fts_rowid FROM local_runtime_session_fts_keys WHERE session_id=?", + (sid,), + ).fetchall(): + db.execute( + "DELETE FROM local_runtime_sessions_fts WHERE rowid=?", (rowid,) + ) + db.execute( + "DELETE FROM local_runtime_session_fts_keys WHERE session_id=?", (sid,) + ) + # The session delete trigger recomputes the project's counters. + db.execute("DELETE FROM local_runtime_sessions WHERE session_id=?", (sid,)) + for workspace, project in journal.data["projects"].items(): + after = project.get("after") + if not after: + continue + current = db.execute( + "SELECT project_id,hidden,extra_data_json FROM local_runtime_projects WHERE project_id=?", + (after["project_id"],), + ).fetchone() + if current is None: + continue + if not project["existed"]: + left = db.execute( + "SELECT COUNT(*) FROM local_runtime_sessions WHERE project_id=?", + (after["project_id"],), + ).fetchone()[0] + if left: + warnings.append( + "Kept the DeepSeek project: it now holds sessions not from this import" + ) + else: + db.execute( + "DELETE FROM local_runtime_projects WHERE project_id=?", + (after["project_id"],), + ) + continue + if dict(current) == after: + before = project["before"] + db.execute( + "UPDATE local_runtime_projects SET hidden=?,extra_data_json=? WHERE project_id=?", + (before["hidden"], before["extra_data_json"], after["project_id"]), + ) + elif { + "hidden": current["hidden"], + "extra_data_json": current["extra_data_json"], + } != { + "hidden": project["before"]["hidden"], + "extra_data_json": project["before"]["extra_data_json"], + }: + warnings.append( + "The DeepSeek project changed after import; its settings were left as they are" + ) + db.commit() + with m.connect(dbfile) as check: + for entry in removed: + sid = entry["targetId"] + for table in ( + "local_runtime_sessions", + "local_runtime_message_rows", + "local_runtime_session_fts_keys", + "local_runtime_pi_history_file_migrations", + ): + if check.execute( + "SELECT 1 FROM " + table + " WHERE session_id=? LIMIT 1", (sid,) + ).fetchone(): + raise m.MigrationError("Undo read-back found rows left in " + table) + for rowid in entry.get("rows", {}).get("ftsRowids", []): + if check.execute( + "SELECT 1 FROM local_runtime_sessions_fts WHERE rowid=?", (rowid,) + ).fetchone(): + raise m.MigrationError("Undo read-back found a search entry left") + kept = [] + for entry in removed + absent: + directory = target / "v2/sessions" / entry["historyRelativeDir"] + marker = directory / m.MARKER + if marker.is_file() and m.parse( + marker.read_text(encoding="utf-8"), str(marker) + ).get("undoRun") not in (None, journal.run_id): + continue + if directory.is_dir() and not m.is_link(directory): + names = {p.name for p in directory.iterdir()} + if names <= OWN_FILES: + for name in names: + (directory / name).unlink() + directory.rmdir() + else: + # Later writes (e.g. a continued chat, forced undo): keep them aside. + keep = journal.dir / "undone" / entry["targetId"] + keep.parent.mkdir(mode=0o700, exist_ok=True) + os.replace(directory, keep) + kept.append(str(keep)) + for parent in reversed(entry.get("createdDirs", [])[:-1]): + path = Path(parent) + try: + path.rmdir() + except OSError: + pass + entry["status"] = "undone" + journal.data["state"] = "undone" + journal.data["undoneAtMs"] = int(time.time() * 1000) + journal.save() + return { + "status": "undone", + "journal": str(journal.path), + "removed": len(removed), + "alreadyAbsent": len(absent) + len(foreign), + "forced": len(modified), + "keptDirectories": kept, + "warnings": warnings, + "sessions": [ + {"sourceId": e["sourceId"], "targetId": e["targetId"]} + for e in removed + absent + ], + } diff --git a/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/import_deepseek.py b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/import_deepseek.py new file mode 100644 index 00000000..6d010f31 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/miniapp/python/import_deepseek.py @@ -0,0 +1,601 @@ +#!/usr/bin/env python3 +"""Import selected DeepSeek conversations as native MiniMax Code sessions. + +Bundled with the DeepSeek Chat Migration Mini App; Python 3.9+, standard library only. +The checkpointed job below is adapted from the team's agent-session-migration plugin. + + import_deepseek.py scan --source + import_deepseek.py plan --source (--session ID... | --all) --report + import_deepseek.py apply --source (--session ID... | --all) --report + import_deepseek.py undo (--report | --run ) [--force] + import_deepseek.py project (read-only: DeepSeek project session count + latest id) + +Defaults: --data-dir ~/.minimax, --agent mavis, --workspace ~/DeepSeek (created when +missing; mkdir only). `plan` writes a preview report and changes nothing else. +`apply` resumes that report; given a report path that does not exist yet, it previews +into it first. There is no full database copy: before each batch writes, inside its +transaction, the run's undo journal (~/.minimax/deepseek-migration//undo.json) +records what the batch will add and the before-image of the one existing row it may +update; each batch commits in its own transaction and is read back, a failed session +never blocks the others, and re-running is idempotent. `undo` removes exactly what one +run added and refuses if those sessions were used since (unless --force). + +Exit codes: 0 completed, 2 completed with per-session errors, 1 configuration or +contract error, 3 undo refused (sessions used since import), 130 interrupted. A one-line JSON summary goes to stdout, progress +and errors to stderr; the report file holds per-session status. During apply, stderr +carries one JSON line per step: {"event":"start","total":N} and, after each batch, {"event":"progress","done":n,"total":N,"sessions":[{sourceId,status}]}. +Statuses: + ready | already-imported | imported | incompatible | failed | running +""" + +import argparse +import contextlib +import json +import os +import sqlite3 +import sys +import uuid +from pathlib import Path + +if sys.version_info < (3, 9): + sys.stderr.write( + '{"error":"Python 3.9 or newer is required","type":"RuntimeError"}\n' + ) + sys.exit(1) + +sys.path.insert(0, str(Path(__file__).resolve().parent)) + +import deepseek_core as m # noqa: E402 +import deepseek_export as ds # noqa: E402 +import deepseek_undo as undo_journal # noqa: E402 + +VERSION = 1 +DONE = {"imported", "already-imported"} +ERRORS = (m.MigrationError, sqlite3.Error, OSError, ValueError) + + +def save(path, report, create=False): + if create: + m.private_write(path, m.compact(report) + "\n") + return + m.regular(path) + temporary = path.with_name(path.name + "." + uuid.uuid4().hex + ".tmp") + try: + m.private_write(temporary, m.compact(report) + "\n") + os.replace(temporary, path) + finally: + temporary.unlink(missing_ok=True) + + +def summary(report, path): + counts = {} + for entry in report["sessions"]: + counts[entry["status"]] = counts.get(entry["status"], 0) + 1 + return { + "status": report["state"], + "report": str(path), + "total": len(report["sessions"]), + "counts": counts, + "discoveryErrors": len(report["discoveryErrors"]), + "sourceBytes": report["sourceBytes"], + "undo": report.get("undo"), + } + + +def emit(event, **fields): + """One progress line on stderr; the Mini App reads these while apply runs.""" + m.print_result(dict(event=event, **fields), file=sys.stderr) + sys.stderr.flush() + + +@contextlib.contextmanager +def reader(job, data): + """One open export and one read snapshot of the checked target, shared by every + session previewed or re-checked in a step. Close it before writing.""" + with ds.Export(data["root"]) as export: + with m.connect(job["target"] / "v2/sqlite/runtime-state.sqlite") as db: + legacy = m.check_target(db, job["config"]["agent"]) + yield {"export": export, "db": db, "legacy": legacy} + + +def compatible(job, data, sid, view): + try: + items = ds.load_inventory(data, [sid], export=view["export"]) + if len(items) != 1 or not items[0]["messages"]: + raise m.MigrationError("Selected session has no readable messages: " + sid) + rows = m.plan_in( + view["db"], + view["legacy"], + job["key"], + job["target"], + job["config"]["agent"], + items, + workspace=job["config"]["workspace"], + restore_projects=job["config"]["restoreProjects"], + ) + return items, m.public_plan(rows)[0] + except (TypeError, KeyError, AttributeError, OverflowError) as exc: + raise m.MigrationError( + "Invalid source record structure for " + sid + ": " + str(exc) + ) from exc + + +def recover(job, entry, committed_only=False): + """Reconcile only an in-flight transaction, under the migration lock.""" + source, target, config = job["source"], job["target"], job["config"] + row = entry["plan"] + sid = m.identity(job["key"], entry["sourceId"]) + relative = m.history_dir(sid, row["sourceCreatedAtMs"]) + if row["targetId"] != sid or row["historyRelativeDir"] != relative: + raise m.MigrationError("Invalid recovery identity") + directory = m.safe_directory(target, "v2/sessions/" + relative) + with m.connect(target / "v2/sqlite/runtime-state.sqlite") as db: + existing = db.execute( + "SELECT history_relative_dir,agent_name FROM local_runtime_sessions WHERE session_id=?", + (sid,), + ).fetchone() + if existing: + if ( + existing["history_relative_dir"] != relative + or existing["agent_name"] != config["agent"] + ): + raise m.MigrationError("Recovery target identity conflict") + marker = m.parse( + m.regular(directory / m.MARKER).read_text(encoding="utf-8"), + "migration marker", + ) + if any( + marker.get(key) != row[key] + for key in ("fingerprint", "sourceId", "targetId", "workspace") + ): + raise m.MigrationError("Recovery provenance mismatch") + # A session found already imported at preview time may come from an + # earlier upload of the same export at another temporary path; identity + # and fingerprint above already prove it. Only this job's own writes must + # name this exact source file. + own_write = entry["status"] != "already-imported" + if marker.get("sourceType") != m.SOURCE_TYPE or ( + own_write and marker.get("source") != str(source) + ): + raise m.MigrationError("Recovery source mismatch") + m.regular(directory / "messages.jsonl") + count = db.execute( + "SELECT COUNT(*) FROM local_runtime_message_rows WHERE session_id=?", + (sid,), + ).fetchone()[0] + initialized = db.execute( + "SELECT 1 FROM local_runtime_pi_history_file_migrations WHERE session_id=?", + (sid,), + ).fetchone() + if count < row["messageCount"] or not initialized: + raise m.MigrationError( + "Recovery found an incomplete committed session; inspect target" + ) + entry["status"] = "imported" + entry.pop("error", None) + return + if committed_only: + raise m.MigrationError( + "Previously completed target session is missing; inspect target before retry" + ) + if directory.exists(): + # The running intent is saved before mkdir and the DB transaction. Keep this + # uncommitted attempt aside; never delete history or restore the whole DB. + quarantine = m.safe_directory( + target, m.LOCK_DIR + "/recovered-" + uuid.uuid4().hex + ) + os.replace(directory, quarantine) + entry.setdefault("recoveredDirectories", []).append(str(quarantine)) + entry["status"] = "ready" + + +def preview(job, path, ids, batch_size): + with m.connect(job["target"] / "v2/sqlite/runtime-state.sqlite") as db: + m.check_target(db, job["config"]["agent"]) + data = ds.inventory(job["source"]) + selected = set(ids or []) + preview_ids = sorted(data["ids"] if not ids else selected) + discovery_errors = [ + e for e in data["errors"] if not ids or e.get("sourceId") in selected + ] + report = { + "version": VERSION, + "config": job["config"], + "state": "previewing", + "sessions": [], + "discoveryErrors": discovery_errors, + "batches": [], + "sourceBytes": data["bytes"], + } + save(path, report, create=True) + failures = {e.get("sourceId"): e["error"] for e in discovery_errors} + known = set(data["ids"]) + with reader(job, data) as view: + for sid in preview_ids: + entry = {"sourceId": sid} + try: + if sid in failures: + raise m.MigrationError(failures[sid]) + if sid not in known: + raise m.MigrationError("Unknown DeepSeek session ID: " + sid) + _, row = compatible(job, data, sid, view) + entry.update( + status="already-imported" + if row["status"] == "already-imported" + else "ready", + plan=row, + ) + except ERRORS as exc: + entry.update(status="incompatible", error=str(exc)) + report["sessions"].append(entry) + report["state"] = "planned" + save(path, report) + return report + + +def load_report(job, path): + report = json.loads(m.regular(path).read_text(encoding="utf-8")) + if ( + not isinstance(report, dict) + or report.get("version") != VERSION + or report.get("config") != job["config"] + ): + raise m.MigrationError( + "Report source/target/options mismatch; preview again with the same options" + ) + if report.get("state") == "undone": + raise m.MigrationError("This import was undone; generate a new preview") + if report.get("state") not in ( + "planned", + "running", + "completed", + "completed-with-errors", + ): + raise m.MigrationError("Preview is incomplete; generate a new preview first") + entries = report.get("sessions") + if not isinstance(entries, list) or not isinstance( + report.get("discoveryErrors"), list + ): + raise m.MigrationError("Invalid report lists") + seen = set() + for entry in entries: + if ( + not isinstance(entry, dict) + or not isinstance(entry.get("sourceId"), str) + or entry["sourceId"] in seen + ): + raise m.MigrationError("Invalid or duplicate report identity") + seen.add(entry["sourceId"]) + if entry.get("status") not in DONE | { + "ready", + "running", + "failed", + "incompatible", + }: + raise m.MigrationError("Invalid report status") + if entry["status"] in DONE | {"ready", "running"} and not isinstance( + entry.get("plan"), dict + ): + raise m.MigrationError("Missing report plan") + report.setdefault("batches", []) + return report + + +def run_apply(job, path, report, batch_size, retry_failed, fail_after=None): + config = job["config"] + with m.migration_lock(job["target"]): + with m.connect(job["target"] / "v2/sqlite/runtime-state.sqlite") as db: + m.check_target(db, config["agent"]) + for entry in report["sessions"]: + if entry["status"] == "running" or entry["status"] in DONE: + previous = entry["status"] + recover(job, entry, committed_only=previous in DONE) + if previous in DONE: + entry["status"] = previous + report["state"] = "running" + save(path, report) + data = ds.inventory(job["source"]) + errors = {e.get("sourceId"): e["error"] for e in data["errors"]} + pending = [ + e + for e in report["sessions"] + if e["status"] == "ready" + or retry_failed + and e["status"] in ("failed", "incompatible") + ] + + emit("start", total=len(pending)) + run_journal = [] + + def journal(): + # One undo journal per apply run, created right before its first write. + if not run_journal: + run_journal.append( + undo_journal.Journal.create( + job["target"], config["agent"], job["source"] + ) + ) + report["undo"] = str(run_journal[0].path) + return run_journal[0] + + def commit(entries, items): + for entry in entries: + entry["status"] = "running" + save(path, report) + try: + result = m._apply_locked( + job["source"], + job["key"], + job["target"], + config["agent"], + items, + workspace=config["workspace"], + restore_projects=config["restoreProjects"], + fail_after=fail_after, + journal=journal(), + ) + report["batches"].append( + {k: result[k] for k in ("status", "undo", "report") if k in result} + ) + for entry in entries: + entry["status"] = ( + "imported" + if result["status"] == "imported" + else "already-imported" + ) + entry.pop("error", None) + except ERRORS as exc: + # A commit can succeed before its report is saved. Reconcile against + # SQLite before isolating failed members of a rolled-back batch. + retry = [] + for entry, item in zip(entries, items): + try: + recover(job, entry) + if entry["status"] not in DONE: + retry.append((entry, item)) + except ERRORS as recovery_error: + entry.update(status="failed", error=str(recovery_error)) + if len(entries) == 1: + for entry, _ in retry: + entry.update(status="failed", error=str(exc)) + else: + for entry, item in retry: + commit([entry], [item]) + save(path, report) + m.print_result(summary(report, path), file=sys.stderr) + + done = 0 + for offset in range(0, len(pending), batch_size): + chunk = pending[offset : offset + batch_size] + entries, items = [], [] + # Re-check this batch against a fresh snapshot, then close it before writing. + with reader(job, data) as view: + for entry in chunk: + sid = entry["sourceId"] + try: + if sid in errors: + raise m.MigrationError(errors[sid]) + loaded, row = compatible(job, data, sid, view) + if ( + "plan" in entry + and row["fingerprint"] != entry["plan"]["fingerprint"] + ): + raise m.MigrationError( + "Source changed since preview; generate a new preview" + ) + entry["plan"] = row + if row["status"] == "already-imported": + entry["status"] = "already-imported" + entry.pop("error", None) + continue + entries.append(entry) + items.extend(loaded) + except ERRORS as exc: + entry.update(status="failed", error=str(exc)) + save(path, report) + if entries: + commit(entries, items) + done += len(chunk) + emit( + "progress", + done=done, + total=len(pending), + sessions=[ + {"sourceId": e["sourceId"], "status": e["status"]} for e in chunk + ], + ) + report["state"] = ( + "completed-with-errors" + if report["discoveryErrors"] + or any(e["status"] not in DONE for e in report["sessions"]) + else "completed" + ) + save(path, report) + return report + + +def prepare_workspace(value): + """~/DeepSeek groups imported chats as one sidebar project. mkdir only.""" + path = Path(value).expanduser().absolute() + if m.is_link(path): + raise m.MigrationError("Workspace must not be a symlink: " + str(path)) + if path.exists() and not path.is_dir(): + raise m.MigrationError("Workspace exists but is not a directory: " + str(path)) + path.mkdir(parents=True, exist_ok=True) + return str(path.resolve()) + + +def run_undo(args): + target = m.target_path(args.data_dir) + if bool(args.report) == bool(args.run): + raise m.MigrationError( + "undo needs exactly one of --report or --run " + ) + report_path = None + if args.report: + report_path = Path(args.report).expanduser().absolute() + report = json.loads(m.regular(report_path).read_text(encoding="utf-8")) + journal_path = report.get("undo") if isinstance(report, dict) else None + if not journal_path: + raise m.MigrationError("This report has no import to undo") + else: + journal_path = args.run + result = undo_journal.undo(target, journal_path, force=args.force) + if report_path is not None: + report["state"] = "undone" + save(report_path, report) + return 0, result + + +def project(args): + """Read-only: the DeepSeek project's session count and most recently active session.""" + target = m.target_path(args.data_dir) + workspace = str(Path(args.workspace).expanduser().absolute().resolve()) + with m.connect(target / "v2/sqlite/runtime-state.sqlite") as db: + where = ( + " FROM local_runtime_sessions WHERE agent_name=? AND workspace_dir=?" + " AND columnar_version=3 AND archived=0" + ) + count = db.execute( + "SELECT COUNT(*)" + where, (args.agent, workspace) + ).fetchone()[0] + latest = db.execute( + "SELECT session_id" + where + " ORDER BY updated_at_ms DESC LIMIT 1", + (args.agent, workspace), + ).fetchone() + return 0, { + "workspace": workspace, + "sessionCount": count, + "latestSessionId": latest[0] if latest else None, + } + + +def execute(args, fail_after=None): + if args.command == "undo": + return run_undo(args) + if args.command == "project": + return project(args) + if not args.source: + raise m.MigrationError("scan/plan/apply require --source ") + source = ds.source_path(args.source) + if args.command == "scan": + return 0, {"source": str(source), "sessions": ds.overview(source, args.session)} + if bool(args.all) == bool(args.session): + raise m.MigrationError( + "Choose exactly one scope: --all or one/more --session IDs" + ) + if not args.report: + raise m.MigrationError("plan/apply require --report ") + if not 1 <= args.batch_size <= 100: + raise m.MigrationError("--batch-size must be between 1 and 100") + if args.retry_failed and args.command != "apply": + raise m.MigrationError("--retry-failed requires apply") + target = m.target_path(args.data_dir) + path = Path(args.report).expanduser().absolute() + if m.is_link(path): + raise m.MigrationError("Linked report path") + path = path.resolve() + if path == source or source in path.parents: + raise m.MigrationError("Report must be outside the DeepSeek export") + workspace = prepare_workspace(args.workspace) + job = { + "source": source, + "key": ds.identity_root(source), + "target": target, + "config": { + "source": str(source), + "target": str(target), + "agent": args.agent, + "sourceType": m.SOURCE_TYPE, + "adapterVersion": m.ADAPTER_VERSION, + "restoreProjects": args.restore_projects, + "selection": {"mode": "all"} + if args.all + else {"mode": "selected", "ids": sorted(set(args.session))}, + "workspace": workspace, + }, + } + if args.command == "plan": + if path.exists(): + raise m.MigrationError( + "Preview report already exists; use a new report path or run apply" + ) + report = preview(job, path, args.session, args.batch_size) + else: + if path.exists(): + report = load_report(job, path) + else: + report = preview(job, path, args.session, args.batch_size) + report = run_apply( + job, path, report, args.batch_size, args.retry_failed, fail_after + ) + result = summary(report, path) + result["sourceType"] = m.SOURCE_TYPE + return (2 if result["status"] == "completed-with-errors" else 0), result + + +def parser(): + p = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + p.add_argument("command", choices=["scan", "plan", "apply", "undo", "project"]) + p.add_argument("--source", help="DeepSeek export zip or unpacked folder") + p.add_argument( + "--session", action="append", help="DeepSeek session id (repeatable)" + ) + p.add_argument("--all", action="store_true", help="Every session in the export") + p.add_argument("--report", help="Preview and progress JSON path (plan/apply)") + p.add_argument("--data-dir", default=str(Path.home() / ".minimax")) + p.add_argument("--agent", default="mavis") + p.add_argument("--workspace", default=str(Path.home() / "DeepSeek")) + p.add_argument("--batch-size", type=int, default=20) + p.add_argument("--retry-failed", action="store_true") + p.add_argument("--run", help="undo: the run's undo.json (or its directory)") + p.add_argument( + "--force", + action="store_true", + help="undo: also remove sessions that were used after import", + ) + p.add_argument( + "--restore-projects", + action="store_true", + help="Restore the workspace project if it was removed from the sidebar", + ) + return p + + +def main(argv=None): + args = parser().parse_args(argv) + try: + code, result = execute(args) + except undo_journal.UndoRefused as exc: + m.print_result( + {"error": str(exc), "type": "UndoRefused", "modified": exc.sessions}, + file=sys.stderr, + ) + return 3 + except ERRORS as exc: + m.print_result({"error": str(exc), "type": type(exc).__name__}, file=sys.stderr) + return 1 + m.print_result(result) + return code + + +if __name__ == "__main__": + import signal + + def interrupted(signum, frame): + raise KeyboardInterrupt + + signal.signal(signal.SIGTERM, interrupted) + try: + sys.exit(main()) + except KeyboardInterrupt: + m.print_result( + { + "status": "interrupted", + "resume": "Repeat apply with the same --report and options", + }, + file=sys.stderr, + ) + sys.exit(130) diff --git a/plugins/danielwalnut/deepseek-chat-migration/package.json b/plugins/danielwalnut/deepseek-chat-migration/package.json new file mode 100644 index 00000000..2c4496ec --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/package.json @@ -0,0 +1,14 @@ +{ + "name": "deepseek-chat-migration", + "version": "0.5.0", + "private": true, + "type": "module", + "scripts": { + "test": "node --test tests/*.test.mjs", + "test:python": "python3 -B -m unittest discover -s tests/python -t tests/python" + }, + "mcode": { + "schemaVersion": 2, + "miniApp": "./miniapp/miniapp.json" + } +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-export.zip b/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-export.zip new file mode 100644 index 00000000..b0bf4cc1 Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-export.zip differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-session.json b/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-session.json new file mode 100644 index 00000000..e8db170f --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/fixtures/official-v3-session.json @@ -0,0 +1,100 @@ +{ + "id": "fixture-session-001", + "title": "Launch brief", + "pinned": false, + "current_message_id": 5, + "inserted_at": "2026-10-01T09:00:00.000Z", + "updated_at": "2026-10-01T09:08:00.000Z", + "messages": [ + { + "id": 1, + "parent_id": null, + "role": "USER", + "status": "FINISHED", + "thinking_enabled": true, + "search_enabled": false, + "accumulated_token_usage": 10, + "inserted_at": "2026-10-01T09:00:00.000Z", + "fragments": [ + { "type": "REQUEST", "content": "Plan a launch page for a quiet note-taking tool." }, + { + "type": "FILE", + "files": [ + { + "id": "fixture-file", + "file_name": "brief.pdf", + "file_size": 2048, + "token_usage": 30, + "inserted_at": "2026-10-01T09:00:00.000Z", + "export_path": null, + "is_image": false, + "width": null, + "height": null + } + ] + } + ] + }, + { + "id": 2, + "parent_id": 1, + "role": "ASSISTANT", + "status": "FINISHED", + "thinking_enabled": true, + "search_enabled": true, + "accumulated_token_usage": 90, + "inserted_at": "2026-10-01T09:02:00.000Z", + "fragments": [ + { "type": "THINK", "content": "Separate the audience promise from implementation detail.", "elapsed_secs": 4.2 }, + { + "type": "TOOL_SEARCH", + "queries": ["accessible landing page patterns"], + "results": [ + { + "url": "https://example.test/accessibility", + "title": "Accessible pages", + "snippet": "Use clear landmarks and sufficient contrast.", + "published_at": null, + "site_name": "Example" + } + ], + "status": "FINISHED" + }, + { "type": "RESPONSE", "content": "Lead with calm focus and show three concrete workflows." } + ] + }, + { + "id": 3, + "parent_id": 2, + "role": "USER", + "status": "FINISHED", + "thinking_enabled": false, + "search_enabled": false, + "accumulated_token_usage": 100, + "inserted_at": "2026-10-01T09:04:00.000Z", + "fragments": [{ "type": "REQUEST", "content": "Avoid gradients and testimonials." }] + }, + { + "id": 4, + "parent_id": 3, + "role": "ASSISTANT", + "status": "FINISHED", + "thinking_enabled": false, + "search_enabled": false, + "accumulated_token_usage": 140, + "inserted_at": "2026-10-01T09:06:00.000Z", + "fragments": [{ "type": "RESPONSE", "content": "First version: a dense comparison table." }] + }, + { + "id": 5, + "parent_id": 3, + "role": "ASSISTANT", + "status": "FINISHED", + "thinking_enabled": false, + "search_enabled": false, + "accumulated_token_usage": 145, + "inserted_at": "2026-10-01T09:08:00.000Z", + "fragments": [{ "type": "RESPONSE", "content": "Regenerated version: a sparse three-step story." }] + } + ] +} diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/migration.test.mjs b/plugins/danielwalnut/deepseek-chat-migration/tests/migration.test.mjs new file mode 100644 index 00000000..b93b3e43 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/migration.test.mjs @@ -0,0 +1,221 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { EventEmitter } from 'node:events'; +import { mkdir, mkdtemp, readFile, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; + +import { + applyProgressEvent, + BATCH_SIZE, + buildMigrationArgs, + createProgress, + defaultTargetDataDir, + defaultWorkspace, + IMPORTER_SCRIPT, + importerError, + parseMigrationReport, + parseProgressLine, + readProject, + runMigrationCommand, + runUndoCommand, +} from '../miniapp/node/migration.mjs'; +import { folderOpener, openFolder } from '../miniapp/node/open-folder.mjs'; + +const python = process.env.PYTHON || 'python3'; +const FIXTURE = fileURLToPath(new URL('./python/fixture.py', import.meta.url)); +const BRANCHY = '11111111-aaaa-4bbb-8ccc-000000000001'; +const FILES = '22222222-aaaa-4bbb-8ccc-000000000002'; + +test('builds the bundled importer command without a shell or plugin lookup', () => { + assert.match(IMPORTER_SCRIPT, /miniapp[\\/]python[\\/]import_deepseek\.py$/); + const args = buildMigrationArgs({ + action: 'plan', + sourcePath: '/tmp/deepseek export.zip', + sessionIds: ['session-a', 'session-b'], + reportPath: '/tmp/job.json', + targetDataDir: '/home/user/.minimax', + workspace: '/home/user/DeepSeek', + }); + assert.deepEqual(args, [ + IMPORTER_SCRIPT, + 'plan', + '--source', '/tmp/deepseek export.zip', + '--session', 'session-a', + '--session', 'session-b', + '--report', '/tmp/job.json', + '--data-dir', '/home/user/.minimax', + '--agent', 'mavis', + '--workspace', '/home/user/DeepSeek', + '--batch-size', String(BATCH_SIZE), + '--restore-projects', + ]); + assert.throws(() => buildMigrationArgs({ action: 'apply', sourcePath: '/x', sessionIds: ['a'], reportPath: '/r', targetDataDir: '/t', workspace: '/w', batchSize: 0 })); + assert.throws(() => buildMigrationArgs({ action: 'plan', sourcePath: '/x', sessionIds: ['--all'], reportPath: '/r', targetDataDir: '/t', workspace: '/w' })); + assert.equal(defaultTargetDataDir('/home/u'), join('/home/u', '.minimax')); + assert.equal(defaultWorkspace('/home/u'), join('/home/u', 'DeepSeek')); +}); + +test('maps importer statuses and reads the target id from the plan', () => { + const result = parseMigrationReport({ + state: 'completed-with-errors', + undo: '/d/deepseek-migration/run/undo.json', + batches: [{ status: 'imported', undo: '/d/deepseek-migration/run/undo.json' }], + sessions: [ + { sourceId: 'new', status: 'imported', plan: { targetId: 'mvs_new', title: '[From DeepSeek] a', messageCount: 4, warnings: ['w'] } }, + { sourceId: 'old', status: 'already-imported', plan: { targetId: 'mvs_old' } }, + { sourceId: 'ready', status: 'ready', plan: { targetId: 'mvs_ready' } }, + { sourceId: 'bad', status: 'incompatible', error: 'invalid branch' }, + { sourceId: 'boom', status: 'failed', error: 'disk full' }, + ], + }, ['new', 'old', 'ready', 'bad', 'boom', 'absent']); + assert.deepEqual(result.sessions.map((item) => item.status), ['success', 'skipped', 'planned', 'failed', 'failed', 'failed']); + assert.equal(result.sessions[0].targetSessionId, 'mvs_new'); + assert.equal(result.sessions[0].warnings, 1); + assert.equal(result.sessions[1].targetSessionId, 'mvs_old'); + assert.equal(result.sessions[3].message, 'invalid branch'); + assert.deepEqual(result.summary, { total: 6, planned: 1, succeeded: 1, skipped: 1, failed: 3 }); + assert.equal(result.undo, '/d/deepseek-migration/run/undo.json'); + assert.equal(parseMigrationReport({ sessions: [] }).undo, null); +}); + +test('parses importer progress lines and folds them into page progress', () => { + assert.equal(parseProgressLine('{"status":"running","report":"/r"}'), null); + assert.equal(parseProgressLine('Traceback (most recent call last):'), null); + assert.equal(parseProgressLine('{"event":"progress","sessions":"nope"}').sessions.length, 0); + const progress = createProgress(); + applyProgressEvent(progress, parseProgressLine('{"event":"start","total":3}')); + assert.deepEqual([progress.phase, progress.done, progress.total], ['starting', 0, 3]); + // Old importers printed a backup step; it is no longer a phase. + assert.equal(parseProgressLine('{"event":"backup"}'), null); + applyProgressEvent(progress, parseProgressLine('{"event":"progress","done":2,"total":3,"sessions":[{"sourceId":"a","status":"imported"},{"sourceId":"b","status":"already-imported"}]}')); + assert.deepEqual([progress.phase, progress.done, progress.total], ['importing', 2, 3]); + applyProgressEvent(progress, parseProgressLine('{"event":"progress","done":9,"total":3,"sessions":[{"sourceId":"c","status":"failed"}]}')); + assert.equal(progress.done, 3); + assert.deepEqual(progress.sessions, { a: 'success', b: 'skipped', c: 'failed' }); +}); + +test('opens the DeepSeek folder with the platform file manager, no shell', async (context) => { + assert.deepEqual(folderOpener('/Users/u/DeepSeek', 'darwin'), { command: 'open', args: ['/Users/u/DeepSeek'] }); + assert.deepEqual(folderOpener('C:\\Users\\u\\DeepSeek', 'win32'), { command: 'explorer.exe', args: ['C:\\Users\\u\\DeepSeek'] }); + assert.deepEqual(folderOpener('/home/u/DeepSeek', 'linux'), { command: 'xdg-open', args: ['/home/u/DeepSeek'] }); + const root = await mkdtemp(join(tmpdir(), 'deepseek-open-')); + context.after(() => rm(root, { recursive: true, force: true })); + const calls = []; + const spawnImpl = (command, args, options) => { + calls.push({ command, args, shell: options.shell }); + const child = new EventEmitter(); + child.unref = () => {}; + queueMicrotask(() => child.emit('spawn')); + return child; + }; + await openFolder(root, { platform: 'darwin', spawnImpl }); + assert.deepEqual(calls, [{ command: 'open', args: [root], shell: false }]); + await assert.rejects(openFolder(join(root, 'missing'), { platform: 'darwin', spawnImpl }), (error) => error.code === 'folder_missing'); + assert.equal(calls.length, 1); +}); + +test('extracts the importer JSON error line from stderr', () => { + assert.equal(importerError('{"status":"running"}\n{"error":"Target agent has no existing session","type":"MigrationError"}'), 'Target agent has no existing session'); + assert.equal(importerError('Traceback\nValueError: x'), 'ValueError: x'); +}); + +test('reports a missing Python interpreter clearly', async () => { + await assert.rejects(runMigrationCommand({ + python: 'definitely-not-python3-here', + action: 'plan', + sourcePath: '/tmp/x.zip', + sessionIds: ['a'], + reportPath: '/tmp/never.json', + targetDataDir: '/tmp', + workspace: '/tmp', + }), (error) => error.code === 'python3_not_found' && /Python 3\.9/.test(error.message)); +}); + +test('plans and applies a synthetic export through the bundled importer', async (context) => { + const root = await mkdtemp(join(tmpdir(), 'deepseek-import-')); + context.after(() => rm(root, { recursive: true, force: true })); + const sourcePath = join(root, 'upload', 'deepseek-export.zip'); + const targetDataDir = join(root, 'data'); + const workspace = join(root, 'home', 'DeepSeek'); + await mkdir(join(root, 'upload')); + await mkdir(join(targetDataDir, 'v2', 'sqlite'), { recursive: true }); + execFileSync(python, ['-c', ` +import sqlite3, sys +sys.path.insert(0, sys.argv[1]) +import fixture +from pathlib import Path +fixture.write_export(Path(sys.argv[2]), fixture.sessions()) +db = sqlite3.connect(sys.argv[3]); db.executescript(fixture.TARGET_SCHEMA); db.close() +`, join(FIXTURE, '..'), sourcePath, join(targetDataDir, 'v2', 'sqlite', 'runtime-state.sqlite')]); + const reportPath = join(root, 'upload', 'job.json'); + const common = { python, sourcePath, sessionIds: [BRANCHY, FILES], reportPath, targetDataDir, workspace }; + + const planned = await runMigrationCommand({ ...common, action: 'plan' }); + assert.equal(planned.exitCode, 0); + assert.deepEqual(planned.summary, { total: 2, planned: 2, succeeded: 0, skipped: 0, failed: 0 }); + assert.equal(planned.sessions[0].title, '[From DeepSeek] 量子计算入门'); + + const events = []; + const applied = await runMigrationCommand({ ...common, action: 'apply', batchSize: 1, onProgress: (event) => events.push(event) }); + assert.equal(applied.ok, true); + // Each row is reported as soon as its batch commits; there is no backup step. + assert.deepEqual(events.map((event) => event.event), ['start', 'progress', 'progress']); + assert.deepEqual(events.slice(1).map((event) => [event.done, event.total]), [[1, 2], [2, 2]]); + assert.deepEqual(events.slice(1).flatMap((event) => event.sessions.map((item) => item.status)), ['success', 'success']); + assert.equal(applied.state, 'completed'); + assert.deepEqual(applied.summary, { total: 2, planned: 0, succeeded: 2, skipped: 0, failed: 0 }); + assert.match(applied.sessions[0].targetSessionId, /^mvs_[0-9a-f]{32}$/); + assert.match(applied.undo, /deepseek-migration[\\/][^\\/]+[\\/]undo\.json$/); + const project = await readProject({ python, targetDataDir, workspace }); + assert.equal(project.sessionCount, 2); + assert.ok(applied.sessions.map((item) => item.targetSessionId).includes(project.latestSessionId)); + + // A second upload of the same export gets a fresh report and is skipped, not failed. + const again = await runMigrationCommand({ ...common, action: 'apply', reportPath: join(root, 'upload', 'job-2.json') }); + assert.equal(again.ok, true); + assert.deepEqual(again.summary, { total: 2, planned: 0, succeeded: 0, skipped: 2, failed: 0 }); + assert.deepEqual(again.sessions.map((item) => item.targetSessionId), applied.sessions.map((item) => item.targetSessionId)); + assert.match(await readFile(reportPath, 'utf8'), /"state":"completed"/); + + // Undo the run from its journal: both sessions go, a second undo is a no-op. + const undone = await runUndoCommand({ python, journalPath: applied.undo, targetDataDir }); + assert.deepEqual([undone.removed, undone.alreadyAbsent, undone.forced], [2, 0, 0]); + assert.equal((await readProject({ python, targetDataDir, workspace })).sessionCount, 0); + const twice = await runUndoCommand({ python, journalPath: applied.undo, targetDataDir }); + assert.deepEqual([twice.removed, twice.alreadyAbsent], [0, 2]); + + // A continued session blocks undo unless forced. + const redo = await runMigrationCommand({ ...common, action: 'apply', reportPath: join(root, 'upload', 'job-3.json') }); + assert.equal(redo.summary.succeeded, 2); + execFileSync(python, ['-c', 'import sqlite3,sys; d=sqlite3.connect(sys.argv[1]); d.execute("UPDATE local_runtime_sessions SET title=? WHERE session_id=?", ("continued", sys.argv[2])); d.commit()', + join(targetDataDir, 'v2', 'sqlite', 'runtime-state.sqlite'), redo.sessions[0].targetSessionId]); + await assert.rejects(runUndoCommand({ python, journalPath: redo.undo, targetDataDir }), + (error) => error.code === 'undo_refused' && error.modified.length === 1 && error.modified[0].changed.includes('title')); + const forced = await runUndoCommand({ python, journalPath: redo.undo, targetDataDir, force: true }); + assert.deepEqual([forced.removed, forced.forced], [2, 1]); +}); + +test('surfaces contract errors as a readable message', async (context) => { + const root = await mkdtemp(join(tmpdir(), 'deepseek-import-err-')); + context.after(() => rm(root, { recursive: true, force: true })); + const sourcePath = join(root, 'export.zip'); + execFileSync(python, ['-c', ` +import sys +sys.path.insert(0, sys.argv[1]) +import fixture +from pathlib import Path +fixture.write_export(Path(sys.argv[2]), fixture.sessions()) +`, join(FIXTURE, '..'), sourcePath]); + await assert.rejects(runMigrationCommand({ + python, + action: 'plan', + sourcePath, + sessionIds: [BRANCHY], + reportPath: join(root, 'job.json'), + targetDataDir: join(root, 'missing-data-dir'), + workspace: join(root, 'DeepSeek'), + }), (error) => error.code === 'migration_failed' && /data directory/.test(error.message)); +}); diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/parser.test.mjs b/plugins/danielwalnut/deepseek-chat-migration/tests/parser.test.mjs new file mode 100644 index 00000000..68b2348f --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/parser.test.mjs @@ -0,0 +1,56 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; + +import { activeBranch, parseDeepSeekExport, stableHash } from '../miniapp/client/deepseek.js'; + +const fixture = JSON.parse(await readFile(new URL('./fixtures/official-v3-session.json', import.meta.url), 'utf8')); + +test('parses official v3 blocks without mixing reasoning and search into answers', () => { + const [conversation] = parseDeepSeekExport(fixture); + assert.equal(conversation.title, 'Launch brief'); + assert.equal(conversation.turnCount, 2); + assert.equal(conversation.branchCount, 1); + assert.deepEqual(conversation.messages[1].blocks.map((block) => block.type), ['reasoning', 'search', 'text']); + assert.equal(conversation.messages[0].blocks[1].files[0].name, 'brief.pdf'); +}); + +test('reconstructs the selected branch and preserves regenerated alternatives', () => { + const [conversation] = parseDeepSeekExport(fixture); + assert.deepEqual(activeBranch(conversation).map((message) => message.id), ['1', '2', '3', '5']); + assert.equal(conversation.messages.find((message) => message.id === '4').blocks[0].text, 'First version: a dense comparison table.'); +}); + +test('stable identity dedupes identical exports and changes with content', () => { + const first = parseDeepSeekExport(fixture)[0].stableId; + const second = parseDeepSeekExport(structuredClone(fixture))[0].stableId; + const changed = structuredClone(fixture); + changed.messages[4].fragments[0].content += ' Updated.'; + assert.equal(first, second); + assert.notEqual(first, parseDeepSeekExport(changed)[0].stableId); + assert.equal(stableHash('same'), stableHash('same')); +}); + +test('parses legacy DeepSeek mapping and flat message schemas', () => { + const legacy = { + id: 'legacy-1', title: 'Legacy', current_node: 'a2', + mapping: { + root: { id: 'root', parent: null, children: ['u1'], message: null }, + u1: { id: 'u1', parent: 'root', children: ['a1', 'a2'], message: { fragments: [{ type: 'REQUEST', content: 'Question' }] } }, + a1: { id: 'a1', parent: 'u1', children: [], message: { fragments: [{ type: 'RESPONSE', content: 'Old' }] } }, + a2: { id: 'a2', parent: 'u1', children: [], message: { fragments: [{ type: 'THINK', content: 'Reason' }, { type: 'RESPONSE', content: 'New' }] } } + } + }; + const mapped = parseDeepSeekExport([legacy])[0]; + assert.equal(mapped.messages[0].role, 'user'); + assert.deepEqual(activeBranch(mapped).map((message) => message.id), ['u1', 'a2']); + + const flat = parseDeepSeekExport({ + chat_session: { id: 'flat-1', title: 'Flat', current_message_id: 'm2' }, + chat_messages: [ + { message_id: 'm1', parent_id: null, role: 'user', content: 'Hi' }, + { message_id: 'm2', parent_id: 'm1', role: 'assistant', content: 'Hello', thinking_content: 'Greet briefly', search_results: [{ title: 'Source', url: 'https://example.test', snippet: 'Synthetic' }] } + ] + })[0]; + assert.deepEqual(flat.messages[1].blocks.map((block) => block.type), ['reasoning', 'text', 'search']); +}); diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/python.test.mjs b/plugins/danielwalnut/deepseek-chat-migration/tests/python.test.mjs new file mode 100644 index 00000000..28df163f --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/python.test.mjs @@ -0,0 +1,14 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; + +// Runs the bundled importer's Python suite as part of `npm test`. +test('bundled importer Python tests pass', () => { + const dir = fileURLToPath(new URL('./python', import.meta.url)); + const python = process.env.PYTHON || 'python3'; + const result = spawnSync(python, ['-B', '-m', 'unittest', 'discover', '-s', dir, '-t', dir], { encoding: 'utf8', timeout: 300_000 }); + assert.equal(result.error, undefined, String(result.error)); + assert.equal(result.status, 0, result.stderr.slice(-4000)); + assert.match(result.stderr, /\nOK/); +}); diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/python/__pycache__/fixture.cpython-313.pyc b/plugins/danielwalnut/deepseek-chat-migration/tests/python/__pycache__/fixture.cpython-313.pyc new file mode 100644 index 00000000..cbd13ebb Binary files /dev/null and b/plugins/danielwalnut/deepseek-chat-migration/tests/python/__pycache__/fixture.cpython-313.pyc differ diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/python/fixture.py b/plugins/danielwalnut/deepseek-chat-migration/tests/python/fixture.py new file mode 100644 index 00000000..199bba0c --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/python/fixture.py @@ -0,0 +1,235 @@ +"""Synthetic DeepSeek export_format_version 3 fixture shared by the importer tests.""" + +import io +import json +import zipfile +from datetime import datetime + +TARGET_SCHEMA = """CREATE TABLE local_runtime_sessions(session_id TEXT PRIMARY KEY,record_json TEXT,updated_at_ms INTEGER,columnar_version INTEGER,agent_name TEXT,runtime TEXT,session_type TEXT,status TEXT,archived INTEGER,visibility TEXT,session_kind TEXT,purpose_kind TEXT,workspace_dir TEXT,project_workspace_dir TEXT,is_default_workspace INTEGER,title TEXT,created_at_ms INTEGER,extra_data_json TEXT,project_id INTEGER,history_relative_dir TEXT); +CREATE TABLE local_runtime_message_rows(id INTEGER PRIMARY KEY,session_id TEXT,msg_id TEXT,role TEXT,turn_id TEXT,source TEXT,created_at_ms INTEGER,data_json TEXT,UNIQUE(session_id,msg_id)); +CREATE TABLE local_runtime_pi_history_file_migrations(session_id TEXT PRIMARY KEY,migrated_at_ms INTEGER NOT NULL,source TEXT NOT NULL,message_count INTEGER NOT NULL,target_revision TEXT NOT NULL); +CREATE TABLE local_runtime_projects(project_id INTEGER PRIMARY KEY,project_kind TEXT,workspace_dir TEXT,created_at_ms INTEGER,updated_at_ms INTEGER,hidden INTEGER NOT NULL DEFAULT 0,extra_data_json TEXT NOT NULL DEFAULT '{}',latest_activity_at_ms INTEGER NOT NULL DEFAULT 0,session_count INTEGER NOT NULL DEFAULT 0); +CREATE TRIGGER trg_project_counts_insert AFTER INSERT ON local_runtime_sessions WHEN NEW.project_id > 0 BEGIN UPDATE local_runtime_projects SET session_count=(SELECT COUNT(*) FROM local_runtime_sessions s WHERE s.project_id=local_runtime_projects.project_id),latest_activity_at_ms=COALESCE((SELECT MAX(s.updated_at_ms) FROM local_runtime_sessions s WHERE s.project_id=local_runtime_projects.project_id),0) WHERE project_id=NEW.project_id; END; +CREATE TRIGGER trg_project_counts_delete AFTER DELETE ON local_runtime_sessions WHEN OLD.project_id > 0 BEGIN UPDATE local_runtime_projects SET session_count=(SELECT COUNT(*) FROM local_runtime_sessions s WHERE s.project_id=local_runtime_projects.project_id),latest_activity_at_ms=COALESCE((SELECT MAX(s.updated_at_ms) FROM local_runtime_sessions s WHERE s.project_id=local_runtime_projects.project_id),0) WHERE project_id=OLD.project_id; END; +CREATE TABLE local_runtime_session_fts_keys(fts_rowid INTEGER PRIMARY KEY AUTOINCREMENT,session_id TEXT NOT NULL UNIQUE REFERENCES local_runtime_sessions(session_id) ON DELETE CASCADE); +CREATE VIRTUAL TABLE local_runtime_sessions_fts USING fts5(session_id UNINDEXED,session_id_terms,agent_name_terms,title_terms,workspace_dir_terms,purpose_terms,status_terms,session_type_terms,tokenize='unicode61'); +INSERT INTO local_runtime_sessions(session_id,columnar_version,agent_name,extra_data_json) VALUES('existing',3,'mavis','{}');""" + + +def epoch_ms(text): + return int(datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() * 1000) + + +BRANCHY = "11111111-aaaa-4bbb-8ccc-000000000001" # regenerate + THINK + search +FILES = "22222222-aaaa-4bbb-8ccc-000000000002" # FILE attachment +CUT = "33333333-aaaa-4bbb-8ccc-000000000003" # timed-out reply, untitled + + +def msg(mid, parent, role, at, fragments, status="FINISHED", **extra): + record = { + "id": mid, + "parent_id": parent, + "role": role, + "status": status, + "thinking_enabled": any(f["type"] == "THINK" for f in fragments), + "search_enabled": any(f["type"] == "TOOL_SEARCH" for f in fragments), + "accumulated_token_usage": 10 * mid, + "inserted_at": "2026-10-01T08:00:%02d.%03dZ" % (at, mid), + "fragments": fragments, + } + record.update(extra) + return record + + +def sessions(): + return { + BRANCHY: { + "id": BRANCHY, + "title": "量子计算入门", + "pinned": True, + "current_message_id": 5, + "inserted_at": "2026-10-01T08:00:00.000Z", + "updated_at": "2026-10-01T09:30:00.000Z", + "messages": [ + msg( + 1, + None, + "USER", + 1, + [{"type": "REQUEST", "content": "什么是量子比特?"}], + ), + msg( + 2, + 1, + "ASSISTANT", + 2, + [{"type": "RESPONSE", "content": "OLD_ANSWER_DISCARDED"}], + ), + msg( + 3, + 1, + "ASSISTANT", + 3, + [ + { + "type": "THINK", + "content": "HIDDEN_REASONING_ONLY", + "elapsed_secs": 1.5, + }, + { + "type": "TOOL_SEARCH", + "status": "FINISHED", + "queries": ["qubit definition"], + "results": [ + { + "url": "https://example.org/qubit", + "title": "Qubit - Example", + "snippet": "A qubit is a two-state system.", + "published_at": "2025-01-01", + "site_icon": "ICON_PAYLOAD_NOT_MIGRATED", + "site_name": "Example", + } + ], + }, + {"type": "RESPONSE", "content": "量子比特是 NEW_ANSWER_KEPT。"}, + { + "type": "CONTENT_RISK", + "content": "RISK_NOTICE", + "style": "WARNING", + }, + ], + ), + msg(4, 3, "USER", 4, [{"type": "REQUEST", "content": "再举个例子"}]), + msg( + 5, + 4, + "ASSISTANT", + 5, + [{"type": "TEMPLATE_RESPONSE", "content": "TEMPLATE_KEPT"}], + ), + ], + }, + FILES: { + "id": FILES, + "title": "总结这份 PDF", + "pinned": False, + "current_message_id": 2, + "inserted_at": "2026-10-02T08:00:00.000Z", + "updated_at": "2026-10-02T08:05:00.000Z", + "messages": [ + msg( + 1, + None, + "USER", + 1, + [ + { + "type": "FILE", + "files": [ + { + "id": "file-1", + "file_name": "report.pdf", + "file_size": 9, + "token_usage": 3, + "inserted_at": "2026-10-02T08:00:00.500Z", + "export_path": "chat_sessions/" + + FILES + + "_files/report.pdf", + "is_image": False, + "width": None, + "height": None, + } + ], + }, + {"type": "REQUEST", "content": "帮我总结"}, + ], + ), + msg( + 2, + 1, + "ASSISTANT", + 2, + [{"type": "RESPONSE", "content": "SUMMARY_TEXT"}], + ), + ], + }, + CUT: { + "id": CUT, + "title": "", + "pinned": False, + "current_message_id": 2, + "inserted_at": "2026-10-03T08:00:00.000Z", + "updated_at": "2026-10-03T08:01:00.000Z", + "messages": [ + msg( + 1, + None, + "USER", + 1, + [{"type": "REQUEST", "content": "写一首长诗\n第二行"}], + ), + msg(2, 1, "ASSISTANT", 2, [], status="TIMEOUT"), + ], + }, + } + + +class Unseekable(io.RawIOBase): + """Forces zipfile to stream entries with data descriptors, like DeepSeek's export.""" + + def __init__(self, sink): + self.sink = sink + + def writable(self): + return True + + def write(self, data): + return self.sink.write(data) + + def seekable(self): + return False + + def tell(self): + raise OSError("unseekable") + + +def write_export(path, records, user_id="user-42", version=3): + with open(path, "wb") as raw: + with zipfile.ZipFile(Unseekable(raw), "w", zipfile.ZIP_DEFLATED) as out: + + def put(name, value): + with out.open(name, "w") as handle: + handle.write( + value + if isinstance(value, bytes) + else json.dumps(value, ensure_ascii=False).encode() + ) + + put("manifest.json", {"export_format_version": version}) + put( + "user.json", + { + "user_id": user_id, + "email": None, + "mobile": None, + "oauth_profiles": [], + }, + ) + put( + "chat_session_index.json", + [ + { + "id": s["id"], + "title": s["title"], + "pinned": s["pinned"], + "message_count": len(s["messages"]), + "inserted_at": s["inserted_at"], + "updated_at": s["updated_at"], + } + for s in records.values() + ], + ) + for s in records.values(): + put("chat_sessions/" + s["id"] + ".json", s) + put("chat_sessions/" + FILES + "_files/report.pdf", b"%PDF-1.4\n") + return path diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/python/test_import_deepseek.py b/plugins/danielwalnut/deepseek-chat-migration/tests/python/test_import_deepseek.py new file mode 100644 index 00000000..c7fe6665 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/python/test_import_deepseek.py @@ -0,0 +1,637 @@ +"""Behavior tests for the bundled DeepSeek importer (miniapp/python). + +Ported from the DeepSeek source tests written for the team's agent-session-migration +plugin. The fixture is synthetic (see fixture.py); no real user data is used. +""" + +import contextlib +import io +import json +import os +import shutil +import sqlite3 +import subprocess +import sys +import tempfile +import unittest +import zipfile +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +SCRIPT = ROOT / "miniapp/python/import_deepseek.py" +sys.path.insert(0, str(SCRIPT.parent)) +sys.path.insert(0, str(HERE)) + +import deepseek_core as m # noqa: E402 +import deepseek_export as ds # noqa: E402 +import import_deepseek as importer # noqa: E402 +from fixture import ( # noqa: E402 + BRANCHY, + CUT, + FILES, + TARGET_SCHEMA, + epoch_ms, + sessions, + write_export, +) + + +class ImporterTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.root = Path(self.tmp.name) + self.home = self.root / "home" + self.home.mkdir() + self.env = patch.dict( + os.environ, {"HOME": str(self.home), "USERPROFILE": str(self.home)} + ) + self.env.start() + self.zip = write_export(self.root / "deepseek_data-2026-10-03.zip", sessions()) + self.target = self.root / "data" + (self.target / "v2/sqlite").mkdir(parents=True) + with self.db() as db: + db.executescript(TARGET_SCHEMA) + self.reports = 0 + + def tearDown(self): + self.env.stop() + self.tmp.cleanup() + + def db(self): + return contextlib.closing( + sqlite3.connect(self.target / "v2/sqlite/runtime-state.sqlite") + ) + + def args(self, command, ids=None, report=None, **options): + if report is None: + self.reports += 1 + report = self.root / ("job-%d.json" % self.reports) + values = dict( + command=command, + source=str(self.zip), + session=list(ids) if ids else None, + all=not ids, + report=str(report), + data_dir=str(self.target), + agent="mavis", + workspace=str(self.home / "DeepSeek"), + batch_size=20, + retry_failed=False, + restore_projects=False, + ) + values.update(options) + return SimpleNamespace(**values) + + def run_job(self, command, ids=None, **options): + with contextlib.redirect_stderr(io.StringIO()): + code, result = importer.execute(self.args(command, ids, **options)) + report = json.loads(Path(result["report"]).read_text()) + return code, result, report + + def imported(self, sid=BRANCHY): + code, result, report = self.run_job("apply", [sid]) + self.assertEqual(code, 0, report) + row = report["sessions"][0]["plan"] + directory = self.target / "v2/sessions" / row["historyRelativeDir"] + canonical = [ + json.loads(x) + for x in (directory / "messages.jsonl").read_text().splitlines() + ] + with self.db() as db: + display = [ + json.loads(r[0]) + for r in db.execute( + "SELECT data_json FROM local_runtime_message_rows WHERE session_id=? ORDER BY id", + (row["targetId"],), + ) + ] + return row, directory, display, canonical + + def cli(self, *argv): + return subprocess.run( + [sys.executable, str(SCRIPT), *argv], + capture_output=True, + timeout=60, + env=dict(os.environ), + ) + + def test_fixture_uses_data_descriptors_like_the_official_export(self): + with zipfile.ZipFile(self.zip) as archive: + self.assertTrue(all(info.flag_bits & 0x08 for info in archive.infolist())) + + def test_scan_lists_sessions_without_writes(self): + before = self.zip.read_bytes() + out = self.cli("scan", "--source", str(self.zip)) + self.assertEqual(out.returncode, 0, out.stderr) + rows = {r["id"]: r for r in json.loads(out.stdout)["sessions"]} + self.assertEqual(set(rows), {BRANCHY, FILES, CUT}) + self.assertEqual(rows[BRANCHY]["title"], "量子计算入门") + self.assertEqual(rows[BRANCHY]["messageCount"], 5) + self.assertIsNone(rows[CUT]["title"]) + self.assertEqual(before, self.zip.read_bytes()) + self.assertFalse((self.home / "DeepSeek").exists()) + + def test_zip_and_unpacked_folder_are_equivalent(self): + folder = self.root / "unpacked" + with zipfile.ZipFile(self.zip) as archive: + archive.extractall(folder) + zipped = ds.load_source(ds.source_path(str(self.zip)), [BRANCHY])[0] + unpacked = ds.load_source(ds.source_path(str(folder)), [BRANCHY])[0] + self.assertEqual(zipped["fingerprint"], unpacked["fingerprint"]) + + def test_source_contract_is_checked(self): + bad = write_export(self.root / "v2.zip", sessions(), version=2) + with self.assertRaisesRegex(m.MigrationError, "export_format_version"): + ds.source_path(str(bad)) + text = self.root / "not.zip" + text.write_text("nope") + with self.assertRaises(m.MigrationError): + ds.source_path(str(text)) + link = self.root / "link.zip" + link.symlink_to(self.zip) + with self.assertRaises(m.MigrationError): + ds.source_path(str(link)) + + def test_plan_is_preview_only_and_creates_the_workspace(self): + code, result, report = self.run_job("plan", [BRANCHY, FILES, CUT]) + self.assertEqual((code, result["counts"]), (0, {"ready": 3})) + self.assertTrue((self.home / "DeepSeek").is_dir()) + plans = {e["sourceId"]: e["plan"] for e in report["sessions"]} + self.assertEqual(plans[BRANCHY]["title"], "[From DeepSeek] 量子计算入门") + self.assertEqual(plans[CUT]["title"], "[From DeepSeek] 写一首长诗") + self.assertEqual( + plans[BRANCHY]["createdAtMs"], epoch_ms("2026-10-01T08:00:00.000Z") + ) + self.assertEqual( + plans[BRANCHY]["sourceUpdatedAtMs"], epoch_ms("2026-10-01T09:30:00.000Z") + ) + self.assertEqual( + plans[BRANCHY]["workspace"], str((self.home / "DeepSeek").resolve()) + ) + with self.db() as db: + self.assertEqual( + db.execute("SELECT COUNT(*) FROM local_runtime_sessions").fetchone()[0], + 1, + ) + self.assertFalse((self.target / "v2/sessions").exists()) + + def test_workspace_must_be_a_real_directory(self): + (self.home / "DeepSeek").write_text("file") + with self.assertRaisesRegex(m.MigrationError, "not a directory"): + self.run_job("plan", [BRANCHY]) + + def test_only_active_branch_is_displayed_and_continued(self): + row, directory, display, canonical = self.imported() + self.assertEqual((row["messageCount"], row["canonicalMessageCount"]), (4, 4)) + dumped = json.dumps([display, canonical], ensure_ascii=False) + self.assertNotIn("OLD_ANSWER_DISCARDED", dumped) + self.assertIn("NEW_ANSWER_KEPT", dumped) + self.assertIn("TEMPLATE_KEPT", dumped) + self.assertEqual( + [r["message"]["role"] for r in canonical], + ["user", "assistant", "user", "assistant"], + ) + archive = (directory / "deepseek-source.json").read_text(encoding="utf-8") + self.assertIn("OLD_ANSWER_DISCARDED", archive) + self.assertTrue(any("inactive branches" in w for w in row["warnings"])) + self.assertEqual(display[0]["turn_id"], display[1]["turn_id"]) + self.assertEqual(display[2]["turn_id"], display[3]["turn_id"]) + self.assertNotEqual(display[0]["turn_id"], display[2]["turn_id"]) + + def test_thinking_is_display_only(self): + _, _, display, canonical = self.imported() + self.assertEqual(display[1]["thinking_content"], "HIDDEN_REASONING_ONLY") + self.assertNotIn( + "HIDDEN_REASONING_ONLY", json.dumps(canonical, ensure_ascii=False) + ) + + def test_search_results_are_historical_reference_text(self): + _, _, display, canonical = self.imported() + tools = display[1]["tool_calls"] + self.assertEqual(len(tools), 1) + self.assertEqual(tools[0]["tool_name"], "历史工具记录") + self.assertIn("web_search", tools[0]["tool_call_args"]) + context = canonical[1]["message"]["content"][0]["text"] + self.assertIn("[DeepSeek 历史工具记录;仅供阅读,不重新执行]", context) + self.assertIn("qubit definition", context) + self.assertIn("https://example.org/qubit", context) + self.assertIn("RISK_NOTICE", context) + self.assertNotIn("ICON_PAYLOAD_NOT_MIGRATED", json.dumps([display, canonical])) + + def test_message_timestamps_and_identity_scheme(self): + _, _, display, canonical = self.imported() + self.assertEqual(display[0]["timestamp"], epoch_ms("2026-10-01T08:00:01.001Z")) + self.assertEqual( + canonical[3]["message"]["timestamp"], epoch_ms("2026-10-01T08:00:05.005Z") + ) + self.assertTrue(canonical[0]["message_id"].startswith("msg-user-v1-")) + self.assertTrue(canonical[1]["message_id"].startswith("msg-deepseek-")) + self.assertEqual(canonical[1]["message"]["provider"], "deepseek-history") + + def test_attachments_are_reference_only_by_default(self): + row, directory, display, canonical = self.imported(FILES) + self.assertIn( + "[DeepSeek 历史附件引用,未复制文件]\nreport.pdf", display[0]["msg_content"] + ) + self.assertIn("report.pdf", canonical[0]["message"]["content"][0]["text"]) + self.assertTrue(any("Attachment reference" in w for w in row["warnings"])) + self.assertEqual( + sorted(p.name for p in directory.iterdir()), + [ + "deepseek-migration.json", + "deepseek-source.json", + "manifest.json", + "messages.jsonl", + ], + ) + self.assertNotIn("%PDF", (directory / "deepseek-source.json").read_text()) + + def test_incomplete_reply_keeps_error_note(self): + _, _, _, canonical = self.imported(CUT) + text = canonical[1]["message"]["content"][0]["text"] + self.assertIn("[DeepSeek 历史错误]", text) + self.assertIn("TIMEOUT", text) + + def test_history_initialized_searchable_and_in_deepseek_project(self): + row, _, _, canonical = self.imported() + with self.db() as db: + checkpoint = db.execute( + "SELECT message_count,target_revision FROM local_runtime_pi_history_file_migrations WHERE session_id=?", + (row["targetId"],), + ).fetchone() + self.assertEqual(checkpoint, (4, m.history_revision(canonical))) + hits = db.execute( + "SELECT session_id FROM local_runtime_sessions_fts WHERE local_runtime_sessions_fts MATCH ?", + ('"' + m.search_terms("From DeepSeek") + '"',), + ).fetchall() + self.assertEqual(hits, [(row["targetId"],)]) + project = db.execute( + "SELECT p.workspace_dir FROM local_runtime_projects p JOIN local_runtime_sessions s USING(project_id) WHERE s.session_id=?", + (row["targetId"],), + ).fetchone()[0] + self.assertEqual(Path(project).name, "DeepSeek") + + def test_provenance_is_deepseek_scoped_and_hides_account_id(self): + _, directory, _, _ = self.imported() + marker = json.loads((directory / "deepseek-migration.json").read_text()) + self.assertEqual( + (marker["sourceType"], marker["sourceId"]), ("deepseek", BRANCHY) + ) + self.assertNotIn("user-42", json.dumps(marker)) + + def counts(self): + tables = ( + "local_runtime_sessions", + "local_runtime_message_rows", + "local_runtime_session_fts_keys", + "local_runtime_sessions_fts", + "local_runtime_pi_history_file_migrations", + ) + with self.db() as db: + result = { + t: db.execute("SELECT COUNT(*) FROM " + t).fetchone()[0] for t in tables + } + result["projects"] = [ + tuple(r) + for r in db.execute( + "SELECT project_id,workspace_dir,hidden,extra_data_json,session_count,latest_activity_at_ms FROM local_runtime_projects ORDER BY project_id" + ) + ] + result["integrity"] = db.execute("PRAGMA integrity_check").fetchone()[0] + result["files"] = ( + sorted( + str(p.relative_to(self.target)) + for p in (self.target / "v2/sessions").rglob("*") + ) + if (self.target / "v2/sessions").exists() + else [] + ) + return result + + def undo(self, report, **options): + values = dict( + command="undo", + source=None, + session=None, + all=False, + report=str(report), + run=None, + data_dir=str(self.target), + force=False, + ) + values.update(options) + with contextlib.redirect_stderr(io.StringIO()): + return importer.execute(SimpleNamespace(**values)) + + def test_no_database_copy_one_undo_journal_per_run(self): + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + code, result = importer.execute( + self.args("apply", [BRANCHY, FILES, CUT], batch_size=1) + ) + self.assertEqual(code, 0) + self.assertEqual(result["counts"], {"imported": 3}) + report = json.loads(Path(result["report"]).read_text()) + self.assertEqual(len(report["batches"]), 3) + self.assertEqual({b["undo"] for b in report["batches"]}, {report["undo"]}) + journal = Path(report["undo"]) + self.assertEqual(journal.parent.parent, self.target / "deepseek-migration") + data = json.loads(journal.read_text()) + self.assertEqual([e["status"] for e in data["sessions"]], ["committed"] * 3) + self.assertTrue(all(e["rows"]["messageRowIds"] for e in data["sessions"])) + self.assertTrue(all(e["rows"]["ftsRowids"] for e in data["sessions"])) + # The project did not exist before: no before-image, created by this run. + project = data["projects"][str((self.home / "DeepSeek").resolve())] + self.assertFalse(project["existed"]) + databases = [ + p + for p in self.target.rglob("*") + if p.suffix == ".sqlite" and p.name != "runtime-state.sqlite" + ] + self.assertEqual(databases, []) + + def test_undo_removes_exactly_the_run_and_is_idempotent(self): + before = self.counts() + _, result, report = self.run_job("apply", [BRANCHY, FILES, CUT]) + self.assertEqual(result["counts"], {"imported": 3}) + self.assertNotEqual(self.counts(), before) + code, undone = self.undo(result["report"]) + self.assertEqual(code, 0) + self.assertEqual((undone["removed"], undone["alreadyAbsent"]), (3, 0)) + self.assertEqual(self.counts(), before) + again = self.undo(result["report"])[1] + self.assertEqual((again["removed"], again["alreadyAbsent"]), (0, 3)) + self.assertEqual(self.counts(), before) + # The undone report cannot be resumed; a fresh preview imports again. + with self.assertRaises(m.MigrationError): + importer.execute(self.args("apply", [BRANCHY], report=result["report"])) + old_report = result["report"] + _, result, _ = self.run_job("apply", [BRANCHY, FILES, CUT]) + self.assertEqual(result["counts"], {"imported": 3}) + imported = self.counts() + # Undoing the old run again must not touch the later run's sessions. + stale = self.undo(old_report)[1] + self.assertEqual((stale["removed"], stale["alreadyAbsent"]), (0, 3)) + self.assertEqual(self.counts(), imported) + + def test_undo_restores_the_project_before_image(self): + workspace = str((self.home / "DeepSeek").resolve()) + (self.home / "DeepSeek").mkdir() + with self.db() as db: + db.execute( + "INSERT INTO local_runtime_projects(project_kind,workspace_dir,created_at_ms,updated_at_ms,hidden,extra_data_json) VALUES('workspace',?,1,1,1,'{\"note\":1}')", + (workspace,), + ) + db.commit() + before = self.counts() + _, result, _ = self.run_job("apply", [BRANCHY, CUT], restore_projects=True) + self.assertEqual(result["counts"], {"imported": 2}) + with self.db() as db: + hidden, count = db.execute( + "SELECT hidden,session_count FROM local_runtime_projects" + ).fetchone() + self.assertEqual((hidden, count), (0, 2)) + self.undo(result["report"]) + self.assertEqual(self.counts(), before) + + def test_undo_refuses_sessions_used_after_import_unless_forced(self): + before = self.counts() + _, result, report = self.run_job("apply", [BRANCHY, CUT]) + plan = report["sessions"][0]["plan"] + directory = self.target / "v2/sessions" / plan["historyRelativeDir"] + # Continue the conversation: one more row and history line, plus a new file. + with self.db() as db: + db.execute( + "INSERT INTO local_runtime_message_rows(session_id,msg_id,role,created_at_ms,data_json) VALUES(?,?,?,?,?)", + (plan["targetId"], "msg-later", "user", 1, "{}"), + ) + db.commit() + with open(directory / "messages.jsonl", "a") as history: + history.write('{"later":true}\n') + (directory / "reports").mkdir() + with self.assertRaises(importer.undo_journal.UndoRefused) as refused: + self.undo(result["report"]) + self.assertEqual( + [s["targetId"] for s in refused.exception.sessions], [plan["targetId"]] + ) + self.assertIn("messageRows", refused.exception.sessions[0]["changed"]) + self.assertTrue(directory.exists()) + with self.db() as db: + self.assertEqual( + db.execute( + "SELECT COUNT(*) FROM local_runtime_sessions WHERE title LIKE '[From DeepSeek]%'" + ).fetchone()[0], + 2, + ) + _, forced = self.undo(result["report"], force=True) + self.assertEqual((forced["removed"], forced["forced"]), (2, 1)) + # Later writes are kept aside in the run directory, never deleted. + self.assertEqual(len(forced["keptDirectories"]), 1) + self.assertTrue(Path(forced["keptDirectories"][0], "reports").is_dir()) + after = self.counts() + self.assertEqual( + {k: v for k, v in after.items() if k != "files"}, + {k: v for k, v in before.items() if k != "files"}, + ) + + def test_cli_undo_exit_codes(self): + _, result, report = self.run_job("apply", [BRANCHY]) + plan = report["sessions"][0]["plan"] + with self.db() as db: + db.execute( + "UPDATE local_runtime_sessions SET title='renamed' WHERE session_id=?", + (plan["targetId"],), + ) + db.commit() + base = ["undo", "--report", result["report"], "--data-dir", str(self.target)] + refused = self.cli(*base) + self.assertEqual(refused.returncode, 3) + error = json.loads(refused.stderr.decode().strip().splitlines()[-1]) + self.assertEqual(error["type"], "UndoRefused") + self.assertEqual(error["modified"][0]["changed"], ["title"]) + forced = self.cli(*base, "--force") + self.assertEqual(forced.returncode, 0, forced.stderr) + self.assertEqual(json.loads(forced.stdout)["removed"], 1) + + def test_apply_reports_progress_after_every_batch(self): + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + importer.execute(self.args("apply", [BRANCHY, FILES, CUT], batch_size=2)) + events = [] + for line in stderr.getvalue().splitlines(): + value = json.loads(line) + if "event" in value: + events.append(value) + self.assertEqual( + [e["event"] for e in events], ["start", "progress", "progress"] + ) + self.assertEqual(events[0]["total"], 3) + self.assertEqual( + [(e["done"], e["total"]) for e in events[1:]], [(2, 3), (3, 3)] + ) + landed = [s for e in events[1:] for s in e["sessions"]] + self.assertEqual( + sorted(s["sourceId"] for s in landed), sorted([BRANCHY, FILES, CUT]) + ) + self.assertEqual({s["status"] for s in landed}, {"imported"}) + + def test_nothing_to_write_opens_no_journal(self): + self.run_job("apply", [BRANCHY]) + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + _, result = importer.execute(self.args("apply", [BRANCHY])) + report = json.loads(Path(result["report"]).read_text()) + self.assertEqual(result["counts"], {"already-imported": 1}) + self.assertIsNone(report.get("undo")) + # Only the first import's journal exists. + self.assertEqual(len(list((self.target / "deepseek-migration").iterdir())), 1) + + def test_idempotent_even_when_the_same_export_is_uploaded_again(self): + first, _, _, _ = self.imported() + code, result, _ = self.run_job("apply", [BRANCHY]) + self.assertEqual(result["counts"], {"already-imported": 1}) + copy = self.root / "upload-2" / "deepseek-export.zip" + copy.parent.mkdir() + shutil.copyfile(self.zip, copy) + self.zip = copy + _, result, report = self.run_job("apply", [BRANCHY]) + self.assertEqual(result["counts"], {"already-imported": 1}) + self.assertEqual(report["sessions"][0]["plan"]["targetId"], first["targetId"]) + self.zip = write_export(self.root / "other.zip", sessions(), user_id="user-43") + _, _, report = self.run_job("plan", [BRANCHY]) + self.assertNotEqual( + report["sessions"][0]["plan"]["targetId"], first["targetId"] + ) + + def test_matches_agent_session_migration_identity(self): + # Same hash input as the plugin's DeepSeek source: "deepseek-account:". + key = ds.identity_root(ds.source_path(str(self.zip))) + self.assertEqual(key, "deepseek-account:user-42") + self.assertRegex(m.identity(key, BRANCHY), r"^mvs_[0-9a-f]{32}$") + + def test_changed_source_is_isolated_not_overwritten(self): + self.imported() + records = sessions() + records[BRANCHY]["messages"][4]["fragments"][0]["content"] = "REWRITTEN" + self.zip = write_export(self.root / "later-export.zip", records) + code, result, report = self.run_job("apply", [BRANCHY, CUT]) + self.assertEqual(code, 2) + status = {e["sourceId"]: e for e in report["sessions"]} + self.assertEqual(status[BRANCHY]["status"], "incompatible") + self.assertIn("Source changed", status[BRANCHY]["error"]) + self.assertEqual(status[CUT]["status"], "imported") + + def test_broken_branch_pointer_fails_only_that_session(self): + records = sessions() + records[CUT]["current_message_id"] = 99 + self.zip = write_export(self.root / "broken.zip", records) + code, result, _ = self.run_job("apply", [BRANCHY, FILES, CUT]) + self.assertEqual(code, 2) + self.assertEqual(result["counts"], {"imported": 2, "incompatible": 1}) + + def test_failed_batch_is_split_and_retried_per_session(self): + with contextlib.redirect_stderr(io.StringIO()): + code, result = importer.execute( + self.args("apply", [BRANCHY, FILES, CUT]), fail_after=2 + ) + # The 3-session batch rolls back; single-session retries then succeed until + # the injected failure point is no longer reachable. + report = json.loads(Path(result["report"]).read_text()) + self.assertEqual( + {e["status"] for e in report["sessions"]} - {"imported", "failed"}, set() + ) + with self.db() as db: + rows = db.execute("SELECT COUNT(*) FROM local_runtime_sessions").fetchone()[ + 0 + ] + self.assertEqual(rows - 1, result["counts"].get("imported", 0)) + + def test_cli_plan_then_apply_reuses_the_report(self): + report = self.root / "cli.json" + base = [ + "--source", + str(self.zip), + "--session", + BRANCHY, + "--session", + FILES, + "--report", + str(report), + "--data-dir", + str(self.target), + "--agent", + "mavis", + "--workspace", + str(self.home / "DeepSeek"), + ] + planned = self.cli("plan", *base) + self.assertEqual(planned.returncode, 0, planned.stderr) + self.assertEqual(json.loads(planned.stdout)["counts"], {"ready": 2}) + self.assertEqual(self.cli("plan", *base).returncode, 1) # never overwritten + applied = self.cli("apply", *base) + self.assertEqual(applied.returncode, 0, applied.stderr) + result = json.loads(applied.stdout) + self.assertEqual( + (result["status"], result["counts"], result["sourceType"]), + ("completed", {"imported": 2}, "deepseek"), + ) + again = self.cli("apply", *base) + self.assertEqual(json.loads(again.stdout)["counts"], {"imported": 2}) + # Changing options against the same report is refused, not silently widened. + mismatch = self.cli("apply", *base, "--restore-projects") + self.assertEqual(mismatch.returncode, 1) + + def test_cli_apply_without_preview_previews_first(self): + report = self.root / "direct.json" + out = self.cli( + "apply", + "--source", + str(self.zip), + "--session", + CUT, + "--report", + str(report), + "--data-dir", + str(self.target), + ) + self.assertEqual(out.returncode, 0, out.stderr) + self.assertEqual(json.loads(out.stdout)["counts"], {"imported": 1}) + self.assertTrue((self.home / "DeepSeek").is_dir()) # default workspace + + def test_cli_reports_contract_errors(self): + out = self.cli( + "plan", + "--source", + str(self.zip), + "--session", + BRANCHY, + "--report", + str(self.root / "x.json"), + "--data-dir", + str(self.root / "missing"), + ) + self.assertEqual(out.returncode, 1) + self.assertIn(b'"error"', out.stderr) + with self.db() as db: + db.execute("DELETE FROM local_runtime_sessions") + db.commit() + out = self.cli( + "plan", + "--source", + str(self.zip), + "--session", + BRANCHY, + "--report", + str(self.root / "y.json"), + "--data-dir", + str(self.target), + ) + self.assertEqual(out.returncode, 1) + self.assertIn(b"initialize it in MiniMax Code first", out.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/server.test.mjs b/plugins/danielwalnut/deepseek-chat-migration/tests/server.test.mjs new file mode 100644 index 00000000..048d9c9a --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/server.test.mjs @@ -0,0 +1,127 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { mkdir, mkdtemp, readFile, rm } from 'node:fs/promises'; +import { createServer } from 'node:net'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; + +import { start } from '../miniapp/node/server.mjs'; + +const python = process.env.PYTHON || 'python3'; +const PACKAGE = fileURLToPath(new URL('..', import.meta.url)); +const FIXTURES = fileURLToPath(new URL('./python', import.meta.url)); +const BRANCHY = '11111111-aaaa-4bbb-8ccc-000000000001'; +const FILES = '22222222-aaaa-4bbb-8ccc-000000000002'; + +function freePort() { + return new Promise((resolve, reject) => { + const probe = createServer(); + probe.once('error', reject); + probe.listen(0, '127.0.0.1', () => { + const { port } = probe.address(); + probe.close(() => resolve(port)); + }); + }); +} + +test('apply exposes live progress and landed rows; the project lookup and undo work through the runtime', async (context) => { + const root = await mkdtemp(join(tmpdir(), 'deepseek-server-')); + const home = join(root, 'home'); + const saved = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE, PATH: process.env.PATH }; + process.env.HOME = home; + process.env.USERPROFILE = home; + const controller = new AbortController(); + context.after(async () => { + controller.abort(); + Object.assign(process.env, saved); + await rm(root, { recursive: true, force: true }); + }); + await mkdir(join(home, '.minimax', 'v2', 'sqlite'), { recursive: true }); + const zip = join(root, 'export.zip'); + execFileSync(python, ['-c', ` +import sqlite3, sys +sys.path.insert(0, sys.argv[1]) +import fixture +from pathlib import Path +fixture.write_export(Path(sys.argv[2]), fixture.sessions()) +db = sqlite3.connect(sys.argv[3]); db.executescript(fixture.TARGET_SCHEMA); db.close() +`, FIXTURES, zip, join(home, '.minimax', 'v2', 'sqlite', 'runtime-state.sqlite')]); + + const port = await freePort(); + const dataDir = join(root, 'miniapp-data'); + const logs = []; + const runtime = await start({ + dataDir, + pluginRoot: PACKAGE, + listen: { host: '127.0.0.1', port }, + signal: controller.signal, + logger: { info: () => {}, warn: (message) => logs.push(message), error: () => {} }, + }); + context.after(() => runtime.dispose()); + const base = `http://127.0.0.1:${port}`; + const call = async (path, options = {}) => { + const response = await fetch(base + path, options); + return { status: response.status, body: await response.json() }; + }; + + const icon = await fetch(`${base}/assets/icon.png`); + assert.equal(icon.status, 200); + assert.equal(icon.headers.get('content-type'), 'image/png'); + + // On a fresh page load, before any upload, the header button already has a target. + const fresh = await call('/api/project'); + assert.equal(fresh.status, 200); + assert.deepEqual([fresh.body.sessionCount, fresh.body.latestSessionId, fresh.body.source], [0, null, 'database']); + + assert.equal((await call('/api/source', { method: 'POST', headers: { 'content-type': 'application/zip' }, body: await readFile(zip) })).status, 200); + const sessions = [{ id: BRANCHY, stableId: 'stable-a' }, { id: FILES, stableId: 'stable-b' }]; + const plan = await call('/api/migration/plan', { method: 'POST', body: JSON.stringify({ sessions }) }); + assert.equal(plan.status, 200); + assert.equal(plan.body.summary.planned, 2); + assert.equal((await call(`/api/migration/progress?planId=${plan.body.planId}`)).status, 404); + + const applying = call('/api/migration/apply', { method: 'POST', body: JSON.stringify({ planId: plan.body.planId }) }); + const seen = []; + let applied; + applying.then((value) => { applied = value; }); + while (!applied) { + const progress = await call(`/api/migration/progress?planId=${plan.body.planId}`); + if (progress.status === 200) seen.push(progress.body.phase); + const second = await call('/api/migration/apply', { method: 'POST', body: JSON.stringify({ planId: plan.body.planId }) }); + if (!applied) assert.ok(second.status === 409, 'a second apply of a running plan is refused'); + await new Promise((resolve) => setTimeout(resolve, 20)); + } + assert.equal(applied.status, 200); + assert.deepEqual(applied.body.imported.sort(), ['stable-a', 'stable-b']); + assert.equal(applied.body.summary.succeeded, 2); + assert.ok(seen.length > 0); + for (const phase of seen) assert.ok(['starting', 'backup', 'importing', 'done'].includes(phase), phase); + + const final = await call(`/api/migration/progress?planId=${plan.body.planId}`); + assert.equal(final.body.phase, 'done'); + assert.equal(final.body.done, final.body.total); + assert.deepEqual(final.body.imported.sort(), ['stable-a', 'stable-b']); + assert.deepEqual(final.body.sessions.map((item) => item.status), ['success', 'success']); + const state = JSON.parse(await readFile(join(dataDir, 'state.json'), 'utf8')); + assert.match(state.imported['stable-a'].targetSessionId, /^mvs_[0-9a-f]{32}$/); + assert.deepEqual((await call('/api/state')).body.imported.sort(), ['stable-a', 'stable-b']); + assert.ok(applied.body.runId); + assert.match(applied.body.undo, /undo\.json$/); + + const project = await call('/api/project'); + assert.equal(project.body.sessionCount, 2); + assert.ok(applied.body.sessions.map((item) => item.targetSessionId).includes(project.body.latestSessionId)); + + const undone = await call('/api/migration/undo', { method: 'POST', body: JSON.stringify({ runId: applied.body.runId }) }); + assert.equal(undone.status, 200); + assert.equal(undone.body.removed, 2); + assert.deepEqual(undone.body.undone.sort(), ['stable-a', 'stable-b']); + assert.deepEqual((await call('/api/state')).body.imported, []); + assert.equal((await call('/api/project')).body.sessionCount, 0); + const again = await call('/api/migration/undo', { method: 'POST', body: JSON.stringify({ runId: applied.body.runId }) }); + assert.equal(again.status, 409); + assert.equal(again.body.error, 'run_not_found'); + assert.deepEqual(logs, []); +}); diff --git a/plugins/danielwalnut/deepseek-chat-migration/tests/zip.test.mjs b/plugins/danielwalnut/deepseek-chat-migration/tests/zip.test.mjs new file mode 100644 index 00000000..74c2caf0 --- /dev/null +++ b/plugins/danielwalnut/deepseek-chat-migration/tests/zip.test.mjs @@ -0,0 +1,40 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { buildArchive, readInput, readZip } from '../miniapp/client/archive.js'; +import { activeBranch } from '../miniapp/client/deepseek.js'; + +const fixtureBytes = readFileSync(new URL('./fixtures/official-v3-export.zip', import.meta.url)); + +test('reads the official v3 layout through data-descriptor ZIP entries', async () => { + const view = new DataView(fixtureBytes.buffer, fixtureBytes.byteOffset, fixtureBytes.byteLength); + assert.equal(view.getUint16(6, true) & 0x08, 0x08); + const bytes = fixtureBytes; + const file = { name: 'deepseek_data.zip', arrayBuffer: async () => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) }; + const conversations = await readInput(file); + assert.equal(conversations.length, 2); + assert.deepEqual(conversations.map((conversation) => conversation.title), [ + 'Indexed sources conversation', + 'Indexed branched conversation', + ]); + assert.deepEqual(activeBranch(conversations[1]).map((message) => message.id), ['1', '3', '4']); + assert.equal(conversations[1].branchCount, 1); + assert.deepEqual(conversations[0].messages[1].blocks.map((block) => block.type), ['reasoning', 'search', 'text']); + const attachment = conversations[1].messages[0].blocks.find((block) => block.type === 'attachment').files[0]; + assert.equal(attachment.available, true); + assert.equal(attachment.archivePath, 'attachments/session-branch/file-brief-brief.txt'); +}); + +test('exports the selected conversation with its available attachment', async () => { + const bytes = fixtureBytes; + const file = { name: 'deepseek_data.zip', arrayBuffer: async () => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) }; + const conversations = await readInput(file); + const branch = conversations[1]; + const archiveBytes = new Uint8Array(await buildArchive([branch]).arrayBuffer()); + const entries = new Map(await readZip(archiveBytes)); + assert.equal(new TextDecoder().decode(entries.get('attachments/session-branch/file-brief-brief.txt')), 'Synthetic attachment data'); + const markdownName = [...entries.keys()].find((name) => name.startsWith('conversations/')); + const markdown = new TextDecoder().decode(entries.get(markdownName)); + assert.match(markdown, /\[brief\.txt\]\(\.\.\/attachments\/session-branch\/file-brief-brief\.txt\)/); + assert.equal([...entries.keys()].some((name) => name.includes('session-sources')), false); +});