A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Let the desktop app move its data location from DS Bot settings
Fei2411ee491ca60cf2
diff --git a/app/src/first-run.mjs b/app/src/first-run.mjsindex 37093bb1..1252b71f 100644--- a/app/src/first-run.mjs+++ b/app/src/first-run.mjs@@ -9,13 +9,23 @@ import { findTeam, newerSessions, teamInUse, userDshHome, within } from './imporconst TEXT = {en: {welcome: 'Welcome to DeepSeek Bot',- places: (data, workspace) => `Data: ${data}\nFiles that Bots make: ${workspace}`,+ places: (data, workspace) => `Data: ${data}\nTeam workspace: ${workspace}`,start: 'Start', change: 'Change…',- pickData: 'Folder for DeepSeek Bot data', pickWorkspace: 'Folder for files that Bots make',- dataInWorkspace: 'The data folder cannot be inside the folder for Bot files.',- workspaceInData: 'The folder for Bot files cannot be inside the data folder.',+ pickData: 'Folder for DeepSeek Bot data', pickWorkspace: 'Folder for the team workspace',+ dataInWorkspace: 'The data folder cannot be inside the team workspace.',+ workspaceInData: 'The team workspace cannot be inside the data folder.',inDsh: home => `Choose a folder outside the DeepSeek Harness data (${home}).`,inProgram: 'Choose a folder outside the program folder: uninstalling removes it.',+ pickMove: 'New folder for DeepSeek Bot data',+ moveDrive: 'Choose a folder, not a whole drive.',+ moveNested: 'Choose a folder outside the current data folder.',+ moveWorkspace: 'The data folder and the team workspace cannot hold each other.',+ moveNotEmpty: 'Choose an empty folder or a new one.',+ moveConfirm: to => `Move DeepSeek Bot's data to ${to}?`,+ moveDetail: from => `DeepSeek Bot copies its data there and starts again. ${from} stays as it is; you can delete it once everything works.`,+ move: 'Move and restart', cancel: 'Cancel',+ moving: 'Copying DeepSeek Bot\'s data…',+ moveFailed: message => `The data could not be copied: ${message}\n\nDeepSeek Bot goes on with its data where it was.`,found: (bots, rooms) => `DeepSeek Harness has a Bot team: ${bots} Bot${bots === 1 ? '' : 's'}${rooms > 0 ? `, ${rooms} group${rooms === 1 ? '' : 's'}` : ''}. Copy it into DeepSeek Bot?`,foundDetail: home => `The copy takes the Bots, groups, memories, chats, files, keys and model settings. Nothing in ${home} changes, and the two teams do not stay in step.`,copy: 'Copy', skip: 'Do not copy',@@ -28,13 +38,23 @@ const TEXT = {},zh: {welcome: '欢迎使用 DeepSeek Bot',- places: (data, workspace) => `数据:${data}\nBot 写出的文件:${workspace}`,+ places: (data, workspace) => `数据:${data}\n总工作区:${workspace}`,start: '开始使用', change: '更改位置…',- pickData: '选择 DeepSeek Bot 的数据文件夹', pickWorkspace: '选择 Bot 写出的文件放在哪个文件夹',- dataInWorkspace: '数据文件夹不能放在 Bot 文件夹里面。',- workspaceInData: 'Bot 文件夹不能放在数据文件夹里面。',+ pickData: '选择 DeepSeek Bot 的数据文件夹', pickWorkspace: '选择总工作区的文件夹',+ dataInWorkspace: '数据文件夹不能放在总工作区里面。',+ workspaceInData: '总工作区不能放在数据文件夹里面。',inDsh: home => `请选择 DeepSeek Harness 数据(${home})以外的文件夹。`,inProgram: '请选择程序文件夹以外的位置:卸载时会删掉程序文件夹。',+ pickMove: '选择 DeepSeek Bot 数据的新文件夹',+ moveDrive: '请选择一个文件夹,不要选整个磁盘。',+ moveNested: '请选择现在的数据文件夹以外的位置。',+ moveWorkspace: '数据文件夹和总工作区不能互相包含。',+ moveNotEmpty: '请选择一个空文件夹,或者新建一个。',+ moveConfirm: to => `把 DeepSeek Bot 的数据移到 ${to} 吗?`,+ moveDetail: from => `DeepSeek Bot 会把数据复制过去,然后重新启动。${from} 会原样保留,确认一切正常后可以自己删掉。`,+ move: '移动并重启', cancel: '取消',+ moving: '正在复制 DeepSeek Bot 的数据…',+ moveFailed: message => `数据没有复制成功:${message}\n\nDeepSeek Bot 会继续使用原来位置的数据。`,found: (bots, rooms) => `DeepSeek Harness 里有一个 Bot 团队:${bots} 个 Bot${rooms > 0 ? `、${rooms} 个群` : ''}。要复制到 DeepSeek Bot 吗?`,foundDetail: home => `会复制 Bot、群、记忆、对话、文件、密钥和模型设置。${home} 里的内容不会改变;复制以后两个团队各自独立。`,copy: '复制', skip: '不复制',diff --git a/app/src/main.mjs b/app/src/main.mjsindex 79daa83a..fb06d211 100644--- a/app/src/main.mjs+++ b/app/src/main.mjs@@ -6,14 +6,15 @@ import { mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'import { rm } from 'node:fs/promises'import { createRequire } from 'node:module'import { release } from 'node:os'-import { dirname, join } from 'node:path'+import { dirname, isAbsolute, join, resolve } from 'node:path'import { setTimeout as sleep } from 'node:timers/promises'import { fileURLToPath } from 'node:url'import { getDshRuntimeVersion } from '@deepseek-ai/dsh-app-boot'import electronUpdater from 'electron-updater'import { chooseLocations, localeText, saveLocations, text } from './first-run.mjs'import { HostProcess } from './host-process.mjs'-import { copySessions, findTeam, importTeam } from './import.mjs'+import { copySessions, findTeam, importTeam, userDshHome } from './import.mjs'+import { copyData, moveProblem } from './move-data.mjs'import { CHECK_PROFILE, PROFILE } from './profile.mjs'import { UPDATE_TEXT, watchReleases, watchUpdates } from './updates.mjs'import { APP_ORIGIN, authenticateWebHost, forwardWebRequest, serveWebDocument } from './web.mjs'@@ -24,7 +25,9 @@ const SCHEME = 'dsh-app'const IPC = {boot: 'ds-bot:boot', bootFailed: 'ds-bot:boot-failed',caption: 'ds-bot:caption-colors', nativeTheme: 'ds-bot:native-theme', fullscreen: 'ds-bot:fullscreen',+ dataLocation: 'ds-bot:data-location', moveData: 'ds-bot:move-data', teamWorkspace: 'ds-bot:team-workspace',}+const MOVE_PROBLEMS = { drive: 'moveDrive', nested: 'moveNested', workspace: 'moveWorkspace', program: 'inProgram', notEmpty: 'moveNotEmpty' }const STATIC_PATHS = new Set(['/', '/index.html', '/manifest.webmanifest'])const UPDATE_CHECK_WAIT_MS = 15_000const UPDATE_DOWNLOAD_WAIT_MS = 15 * 60_000@@ -50,8 +53,10 @@ let updateslet stopping = falselet stopped = falselet failed = false-// Quitting while a first-run dialog is open stops the first run, and during the copy it waits-// for the copy to stop at a safe point, so that it leaves no half team and no temporary copy.+let places+// Quitting while a first-run dialog is open stops the first run, and during the copy (of the+// first run or of a data move) it waits for the copy to stop at a safe point, so that it leaves+// no half team and no temporary copy.let choosinglet copyingconst copyAbort = new AbortController()@@ -92,6 +97,7 @@ async function start() {choosing = new AbortController()const where = await chooseLocations(choosing.signal).finally(() => { choosing = undefined })if (where.quit) { app.exit(0); return }+ places = { dataDir: where.dataDir, workspace: where.workspace }const pnpm = join(app.getAppPath(), 'node_modules', 'pnpm', 'bin', 'pnpm.mjs')const profileDir = join(where.dataDir, 'profiles', PROFILE)mkdirSync(profileDir, { recursive: true })@@ -145,6 +151,22 @@ async function start() {assertWindow(event)fail(new Error(String(message)))})+ // A throwaway run (DS_BOT_HOME) keeps its folders where it was told.+ const fixed = Boolean(process.env.DS_BOT_HOME)+ ipcMain.handle(IPC.dataLocation, (event) => {+ assertWindow(event)+ return { path: places.dataDir, fixed }+ })+ ipcMain.handle(IPC.moveData, (event, workspace) => {+ assertWindow(event)+ return fixed ? false : moveData(typeof workspace === 'string' && isAbsolute(workspace) ? workspace : places.workspace)+ })+ // The plugin keeps the team workspace; the app remembers it for its own checks.+ ipcMain.on(IPC.teamWorkspace, (event, path) => {+ if (fixed || !fromWindow(event) || typeof path !== 'string' || !isAbsolute(path)) return+ places = { ...places, workspace: path }+ saveLocations(places)+ })if (process.platform === 'win32') {// Windows ignores the alpha of the caption buttons' color, so they take the opaque color// of the surface under them, which the page measures.@@ -251,6 +273,49 @@ async function checkTeam(where, newCheck, signal) {}}+// The data moves as a copy while the Host is stopped, and the app starts again on the new+// folder; after a failed copy it starts again on the old one. The old folder stays.+async function moveData(workspace) {+ if (copying !== undefined || stopping || mainWindow === undefined) return false+ const t = text()+ const owner = mainWindow+ const { canceled, filePaths } = await dialog.showOpenDialog(owner, { title: t.pickMove, defaultPath: dirname(places.dataDir), properties: ['openDirectory', 'createDirectory', 'promptToCreate'] })+ if (canceled || filePaths[0] === undefined) return false+ const to = resolve(filePaths[0])+ const dshHome = userDshHome()+ const problem = moveProblem({ from: places.dataDir, to, workspace, dshHome, program: app.isPackaged ? dirname(app.getPath('exe')) : undefined })+ if (problem !== undefined) {+ await dialog.showMessageBox(owner, { type: 'warning', title: 'DeepSeek Bot', message: problem === 'dsh' ? t.inDsh(dshHome) : t[MOVE_PROBLEMS[problem]], buttons: ['OK'] })+ return false+ }+ const { response } = await dialog.showMessageBox(owner, { type: 'question', title: 'DeepSeek Bot', noLink: true, message: t.moveConfirm(to), detail: t.moveDetail(places.dataDir), buttons: [t.move, t.cancel], defaultId: 0, cancelId: 1 })+ if (response !== 0 || stopping) return false+ const signal = copyAbort.signal+ copying = (async () => {+ await stopHost()+ void mainWindow?.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(movingPage(t.moving))}`)+ mainWindow?.setProgressBar(2)+ try {+ await copyData(places.dataDir, to, signal)+ saveLocations({ dataDir: to, workspace })+ } catch (error) {+ if (signal.aborted) return+ console.error(error)+ await dialog.showMessageBox({ type: 'warning', title: 'DeepSeek Bot', message: t.moveFailed(error instanceof Error ? error.message : String(error)) })+ } finally {+ mainWindow?.setProgressBar(-1)+ }+ app.relaunch()+ })()+ await copying+ copying = undefined+ app.quit()+ return true+}++const movingPage = message => `<!doctype html><meta charset="utf-8"><meta name="color-scheme" content="light dark">+<body style="margin:0;height:100vh;display:grid;place-items:center;font:14px system-ui,sans-serif;background:Canvas;color:CanvasText">${message.replace(/[&<>]/gu, char => `&#${String(char.charCodeAt(0))};`)}</body>`+// Windows updates itself; macOS asks to download the new version. Throwaway runs check only a// given feed and never ask.function startUpdates() {diff --git a/app/src/move-data.mjs b/app/src/move-data.mjsnew file mode 100644index 00000000..90144d8c--- /dev/null+++ b/app/src/move-data.mjs@@ -0,0 +1,58 @@+// Moving the app's data to another folder. The copy is put together in a folder beside the new+// one and takes its name only once it is whole, so a copy that stopped or failed leaves nothing+// that looks like data. The old folder stays as it was.+import { existsSync, readdirSync, statSync } from 'node:fs'+import { cp, mkdir, rename, rm, rmdir } from 'node:fs/promises'+import { basename, dirname, join, relative, resolve } from 'node:path'+import { within } from './import.mjs'++// The engine's lock on the team belongs to the old folder; the first-run copy and its check+// leave their temporary folders only when they were cut short.+const LEFT_BEHIND = [/^bot[\\/]state\.lock$/u, /^\.import(-check)?-\d+$/u]++/**+ * What keeps `to` from being the new data location, or undefined.+ * @param {{ from: string, to: string, workspace?: string, dshHome: string, program?: string }} places+ * @returns {'drive' | 'nested' | 'workspace' | 'dsh' | 'program' | 'notEmpty' | undefined}+ */+export function moveProblem({ from, to, workspace, dshHome, program }) {+ if (dirname(resolve(to)) === resolve(to)) return 'drive'+ if (within(to, from) || within(from, to)) return 'nested'+ if (workspace !== undefined && (within(to, workspace) || within(workspace, to))) return 'workspace'+ if (within(to, dshHome) || within(dshHome, to)) return 'dsh'+ if (program !== undefined && within(to, program)) return 'program'+ if (existsSync(to) && (!statSync(to).isDirectory() || readdirSync(to).length > 0)) return 'notEmpty'+ return undefined+}++const stagePrefix = to => `.${basename(to)}.moving-`++/**+ * Copy the data in `from` to `to`, an empty or new folder. The signal stops it between files.+ * @param {string} from @param {string} to @param {AbortSignal} [signal]+ */+export async function copyData(from, to, signal) {+ const parent = dirname(resolve(to))+ await mkdir(parent, { recursive: true })+ // A run that was killed can leave its staging folder behind.+ for (const name of readdirSync(parent).filter(entry => entry.startsWith(stagePrefix(to)))) {+ await rm(join(parent, name), { recursive: true, force: true, maxRetries: 5 })+ }+ const stage = join(parent, `${stagePrefix(to)}${String(Date.now())}`)+ try {+ await cp(from, stage, {+ recursive: true,+ filter: (source) => {+ signal?.throwIfAborted()+ const path = relative(from, source)+ return !LEFT_BEHIND.some(pattern => pattern.test(path))+ },+ })+ signal?.throwIfAborted()+ if (existsSync(to)) await rmdir(to)+ await rename(stage, to)+ } catch (error) {+ await rm(stage, { recursive: true, force: true, maxRetries: 5 })+ throw error+ }+}diff --git a/app/src/preload.cjs b/app/src/preload.cjsindex a4663502..22790459 100644--- a/app/src/preload.cjs+++ b/app/src/preload.cjs@@ -10,6 +10,12 @@ if (location.protocol === 'dsh-app:' && location.hostname === 'app') {ready: () => ipcRenderer.invoke('ds-bot:boot'),failed: (message) => ipcRenderer.invoke('ds-bot:boot-failed', String(message)),})+ // DS Bot's settings show and move the app's data location through this.+ contextBridge.exposeInMainWorld('dsBotApp', {+ dataLocation: () => ipcRenderer.invoke('ds-bot:data-location'),+ moveData: (workspace) => ipcRenderer.invoke('ds-bot:move-data', typeof workspace === 'string' ? workspace : undefined),+ teamWorkspaceChanged: (path) => { ipcRenderer.send('ds-bot:team-workspace', String(path)) },+ })const caption = process.argv.find(value => value.startsWith('--ds-bot-caption='))if (caption !== undefined) {ipcRenderer.on('ds-bot:fullscreen', (_event, fullscreen) => {diff --git a/app/test/move-data.test.mjs b/app/test/move-data.test.mjsnew file mode 100644index 00000000..48630e62--- /dev/null+++ b/app/test/move-data.test.mjs@@ -0,0 +1,95 @@+import assert from 'node:assert/strict'+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { dirname, join, parse } from 'node:path'+import { test } from 'node:test'+import { copyData, moveProblem } from '../src/move-data.mjs'++const write = (path, text) => {+ mkdirSync(dirname(path), { recursive: true })+ writeFileSync(path, text)+}++// The app's data with a team, a chat and a key, plus what stays behind: the engine's lock and+// the temporary folders of a first-run copy and check that were cut short.+function setup(t) {+ const root = mkdtempSync(join(tmpdir(), 'ds-bot-move-'))+ t.after(() => { rmSync(root, { recursive: true, force: true, maxRetries: 5 }) })+ const from = join(root, 'data')+ write(join(from, 'bot', 'state.json'), '{"bots":{"a":{}}}')+ write(join(from, 'bot', 'state.lock'), '{}')+ write(join(from, 'sessions', 'g', 's1', 'log.jsonl'), 'chat')+ write(join(from, '.credentials.yaml'), 'key: test')+ write(join(from, '.import-12', 'bot', 'state.json'), '{}')+ write(join(from, '.import-check-34', 'check.json'), '[]')+ return { root, from }+}++const files = (dir) => {+ const found = []+ const walk = (at, prefix) => {+ for (const entry of readdirSync(at, { withFileTypes: true })) {+ if (entry.isDirectory()) walk(join(at, entry.name), `${prefix}${entry.name}/`)+ else found.push(`${prefix}${entry.name}`)+ }+ }+ walk(dir, '')+ return found.sort()+}++test('the new data location is an empty or new folder apart from the data, the team workspace, DSH and the program', (t) => {+ const { root, from } = setup(t)+ const places = { from, workspace: join(root, 'work'), dshHome: join(root, 'dsh'), program: join(root, 'program') }+ const problem = to => moveProblem({ ...places, to })+ assert.equal(problem(join(root, 'new')), undefined)+ mkdirSync(join(root, 'empty'))+ assert.equal(problem(join(root, 'empty')), undefined)+ assert.equal(problem(parse(root).root), 'drive')+ assert.equal(problem(from), 'nested')+ assert.equal(problem(join(from, 'inside')), 'nested')+ assert.equal(problem(root), 'nested')+ assert.equal(problem(join(root, 'work', 'data')), 'workspace')+ assert.equal(moveProblem({ ...places, workspace: join(root, 'new', 'work'), to: join(root, 'new') }), 'workspace')+ assert.equal(problem(join(root, 'dsh', 'data')), 'dsh')+ assert.equal(moveProblem({ ...places, dshHome: join(root, 'new', '.dsh'), to: join(root, 'new') }), 'dsh')+ assert.equal(problem(join(root, 'program', 'data')), 'program')+ write(join(root, 'full', 'note.txt'), 'x')+ assert.equal(problem(join(root, 'full')), 'notEmpty')+ assert.equal(problem(join(root, 'full', 'note.txt')), 'notEmpty')+})++test('the data is copied whole, without the lock and the temporary folders, and the old folder stays', async (t) => {+ const { root, from } = setup(t)+ const before = files(from)+ for (const to of [join(root, 'new', 'data'), join(root, 'empty')]) {+ mkdirSync(join(root, 'empty'), { recursive: true })+ await copyData(from, to)+ assert.deepEqual(files(to), ['.credentials.yaml', 'bot/state.json', 'sessions/g/s1/log.jsonl'])+ assert.equal(readFileSync(join(to, 'sessions', 'g', 's1', 'log.jsonl'), 'utf8'), 'chat')+ assert.deepEqual(readdirSync(dirname(to)).filter(name => name.includes('.moving-')), [])+ }+ assert.deepEqual(files(from), before)+})++test('a copy that is stopped or fails leaves no new folder and no staging folder', async (t) => {+ const { root, from } = setup(t)+ const to = join(root, 'new')+ const stopped = new AbortController()+ stopped.abort()+ await assert.rejects(copyData(from, to, stopped.signal), { name: 'AbortError' })+ // Stopped part way through, after some files are copied.+ let seen = 0+ const midway = new AbortController()+ const signal = { throwIfAborted: () => { if (++seen === 4) midway.abort(); midway.signal.throwIfAborted() } }+ await assert.rejects(copyData(from, to, signal), { name: 'AbortError' })+ assert.ok(seen >= 4)+ assert.equal(existsSync(to), false)+ assert.deepEqual(readdirSync(root).filter(name => name.includes('.moving-')), [])+})++test('a staging folder that a killed copy left behind goes before the next copy', async (t) => {+ const { root, from } = setup(t)+ write(join(root, '.new.moving-1', 'bot', 'state.json'), '{}')+ await copyData(from, join(root, 'new'))+ assert.deepEqual(readdirSync(root).sort(), ['data', 'new'])+})