WorkBuddy、CodeBuddy 桌面端增强助手:签到猫猫旅行成长计划查询、多账号独立备份、点切即用;免打扰模式让 AI 无人值守跑长任务;跨账号会话迁移、异常中断自动续接;暂存/快捷提示词;毛玻璃主题与更多实用功能。
/**
* WorkBuddy 多账号切换器 - 共享逻辑
*
* 原理:WorkBuddy 桌面端的登录信息保存在
* ~/Library/Application Support/CodeBuddyExtension/Data/Public/auth/workbuddy-desktop.info
* 其中 account.uid 是用户唯一 ID。本插件把该文件按 <uid>.info 分文件备份到稳定目录,
* 切换登录时把对应备份复制回原文件即可。
*
* 环境变量(均可覆盖默认值):
* WBSWITCH_AUTH_FILE 登录信息文件路径
* WBSWITCH_DATA_DIR 备份数据目录
*/
'use strict';
const { accountIdentity } = require('./account-identity');
const fs = require('fs');
const os = require('os');
const path = require('path');
const crypto = require('crypto');
const { getProfile, profileDataDir, sharedDataDir } = require('./profiles.js');
const plat = require('./platform.js');
const IS_WIN = plat.IS_WIN;
const IS_MAC = plat.IS_MAC;
const IS_LINUX = plat.IS_LINUX;
// 备份数据目录:macOS ~/Library/Application Support/WorkDaddy
// Linux $XDG_CONFIG_HOME/WorkDaddy (~/.config/WorkDaddy)
// Windows %APPDATA%\WorkDaddy
const PLATFORM_DATA_DIR = path.join(plat.appSupport, 'WorkDaddy');
// 旧版 HelloBuddy 目录只存在于 macOS 历史版本,仅 macOS 需要做隐式迁移
const LEGACY_DATA_DIR = IS_MAC
? path.join(os.homedir(), 'Library', 'Application Support', 'HelloBuddy')
: null;
// ===================== [wd-compat] WorkBuddy 5.6+ $wbEncrypted 字段信封解密适配 =====================
// WorkBuddy 5.6 起对 workbuddy.cn 域账号启用 at-rest 字段级加密(编译期策略,无用户开关):
// auth/account 文件中 nickname/phoneNumber/accessToken/refreshToken 等写为
// {"$wbEncrypted":1,"envelope":"<base64(JSON)>"},内层 AES-256-GCM(sym-v1 帧 + AAD)。
// 方案:读取端解密——经 WorkBuddy 自带 Electron(ELECTRON_RUN_AS_NODE=1)子进程调用原生
// 绑定取密钥负载并派生密钥;密钥只经管道回传、内存缓存、绝不落盘、绝不写日志。
// 失败语义:任何一步失败保留原值、60s 后允许重试——不阻断账号管理,界面以「(已加密)」占位降级。
const WD_COMPAT = { key: null, keyFailAt: 0, keyFailReason: '', decOk: 0, decFail: 0 };
const WD_COMPAT_KEY_RETRY_MS = 60000;
// 被 wdCompatStaticKey 以 ELECTRON_RUN_AS_NODE=1 方式再次执行本文件时派生密钥并退出。
// 密钥只写父子进程管道;该绑定仅存在于 WorkBuddy 的 Electron 运行时内。
if (process.argv.includes('--wd-compat-print-key')) {
try {
const binding = process._linkedBinding('electron_browser_workbuddy_storage');
const payload = JSON.parse(binding.loggerGet());
if (payload && payload.version === 1 && typeof payload.atRestSecretKey === 'string') {
const key = crypto.createHash('sha256').update(payload.atRestSecretKey, 'utf8').digest();
process.stdout.write(`WD_COMPAT_KEY ${key.toString('base64')}\n`);
key.fill(0);
process.exit(0);
}
process.stderr.write('payload schema mismatch');
process.exit(4);
} catch (e) { process.stderr.write(String(e.message || e).slice(0, 200)); process.exit(3); }
}
function isWbEncryptedEnvelope(v) {
return !!v && typeof v === 'object' && !Array.isArray(v) && v.$wbEncrypted === 1 && typeof v.envelope === 'string';
}
function wdCompatContainsEncryptedFields(value) {
if (isWbEncryptedEnvelope(value)) return true;
if (Array.isArray(value)) return value.some(wdCompatContainsEncryptedFields);
if (!value || typeof value !== 'object') return false;
return Object.values(value).some(wdCompatContainsEncryptedFields);
}
/** 信封字段的展示兜底:取钥不可用时显示占位而非整段密文/[object Object]。 */
function wdCompatText(v) {
if (typeof v === 'string') return v;
return isWbEncryptedEnvelope(v) ? '(已加密)' : '';
}
/** 只返回可直接用于 HTTP 的明文 token;信封不可用时返回空字符串。 */
function wdCompatAuthToken(auth) {
if (!auth || typeof auth !== 'object') return '';
const value = auth.accessToken ?? auth.access_token ?? auth.token;
return typeof value === 'string' ? value.trim() : '';
}
/**
* WorkBuddy 5.8 may redact account.phoneNumber while the access-token claims
* still carry the account's verified mobile as preferred_username. Keep only
* a strict mainland mobile shape; never expose arbitrary token claims.
*/
function wdCompatTokenPhone(token) {
const parts = String(token || '').split('.');
if (parts.length < 2) return '';
try {
const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8'));
const candidates = [payload.phone_number, payload.phoneNumber, payload.mobile, payload.preferred_username];
return candidates.map((value) => String(value == null ? '' : value).trim()).find((value) => /^1\d{10}$/.test(value)) || '';
} catch (_) {
return '';
}
}
function wdCompatHasAuthCredential(auth) {
if (!auth || typeof auth !== 'object') return false;
const value = auth.accessToken ?? auth.access_token ?? auth.token;
return typeof value === 'string' ? value.trim().length > 0 : isWbEncryptedEnvelope(value);
}
function wdCompatLog(msg) {
try { fs.appendFileSync(path.join(PLATFORM_DATA_DIR, 'daemon.log'), `[wd-compat] ${msg}\n`); } catch (_) {}
}
// Windows 安装目录可以由用户选择,不能只依赖默认的 %LOCALAPPDATA% 路径。
// profiles.js 会读取当前 profile 的 workbuddy-target.json,并返回已经过 profile
// 校验的主程序路径;只在 Node/daemon 侧读取,注入到 renderer 的 compat 脚本不会触发。
function wdCompatConfiguredExe() {
if (!IS_WIN || typeof module === 'undefined' || !module.exports) return '';
try {
const { getProfile } = require('./profiles.js');
const profile = getProfile(process.env.WBSWITCH_PROFILE || 'workbuddy-cn', {
dataDir: process.env.WBSWITCH_DATA_DIR || undefined,
env: process.env,
platform: process.platform,
});
return profile && typeof profile.appPath === 'string' ? profile.appPath : '';
} catch (_) {
return '';
}
}
function wdCompatWindowsExeCandidates(options = {}) {
const pathApi = path.win32;
const localAppData = String(options.localAppData || process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'));
const profileId = String(options.profileId || process.env.WBSWITCH_PROFILE || 'workbuddy-cn').trim().toLowerCase();
const configured = String(options.configured || '').trim();
const candidates = [];
// CodeBuddy cannot expose WorkBuddy's storage binding. Keep both official
// WorkBuddy binaries available as key providers when importing their
// encrypted account backups into a CodeBuddy profile.
if (profileId === 'workbuddy-cn' || profileId === 'workbuddy-ai') {
if (configured) candidates.push(configured);
}
candidates.push(
pathApi.join(localAppData, 'Programs', 'WorkBuddy', 'WorkBuddy.exe'),
pathApi.join(localAppData, 'Programs', 'WorkBuddyAI', 'WorkBuddyAI.exe'),
);
return [...new Set(candidates.filter(Boolean))];
}
function wdCompatExeCandidates() {
if (process.env.WORKDADDY_WB_EXE) return [process.env.WORKDADDY_WB_EXE];
const home = os.homedir();
if (IS_MAC) {
const apps = [];
for (const root of ['/Applications', path.join(home, 'Applications')]) {
for (const name of ['WorkBuddy.app', 'WorkBuddy AI.app']) {
apps.push(path.join(root, name, 'Contents', 'MacOS', 'Electron'));
}
}
return apps;
}
if (IS_WIN) {
const configured = wdCompatConfiguredExe();
return wdCompatWindowsExeCandidates({
localAppData: process.env.LOCALAPPDATA || '',
profileId: process.env.WBSWITCH_PROFILE || 'workbuddy-cn',
configured,
});
}
return ['/opt/WorkBuddy/workbuddy', '/opt/WorkBuddy/workbuddy-ai'];
}
function wdCompatStaticKey() {
if (WD_COMPAT.key) return WD_COMPAT.key;
if (WD_COMPAT.keyFailAt && Date.now() - WD_COMPAT.keyFailAt < WD_COMPAT_KEY_RETRY_MS) {
throw new Error(WD_COMPAT.keyFailReason || '取钥暂不可用');
}
let lastErr = '未找到 WorkBuddy 可执行文件';
for (const exe of wdCompatExeCandidates()) {
if (!fs.existsSync(exe)) continue;
const r = require('child_process').spawnSync(exe, [__filename, '--wd-compat-print-key'], {
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
timeout: 15000, encoding: 'utf8',
});
const m = r.stdout && r.stdout.match(/WD_COMPAT_KEY ([A-Za-z0-9+/=]+)/);
if (m) {
WD_COMPAT.key = Buffer.from(m[1], 'base64');
WD_COMPAT.keyFailAt = 0;
WD_COMPAT.keyFailReason = '';
return WD_COMPAT.key;
}
lastErr = (r.stderr && String(r.stderr).trim().slice(0, 120)) || `exit=${r.status}`;
}
WD_COMPAT.keyFailAt = Date.now();
WD_COMPAT.keyFailReason = lastErr;
wdCompatLog(`取钥失败(60s 后重试): ${lastErr}`);
throw new Error(lastErr);
}
function wdCompatEnvelopeAAD(env) {
const FRAMING_FIELD = 2, FMT_FIELD = 'WBEV1', AAD_DOMAIN = Buffer.from('WB-AAD\0', 'ascii');
const lp = (s) => { const b = Buffer.from(s, 'utf8'); const l = Buffer.alloc(4); l.writeUInt32BE(b.length); return Buffer.concat([l, b]); };
const u32 = (n) => { const b = Buffer.alloc(4); b.writeUInt32BE(n); return b; };
return Buffer.concat([
AAD_DOMAIN, Buffer.from([1]), lp(FMT_FIELD), lp('sym-v1'), u32(env.suite || 1),
lp(env.keyId || ''), Buffer.from([FRAMING_FIELD]), Buffer.from([0]), Buffer.from([0]),
]);
}
function wdCompatOpenEnvelope(env, key) {
const d = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(env.nonce, 'base64'), { authTagLength: 16 });
d.setAAD(wdCompatEnvelopeAAD(env));
d.setAuthTag(Buffer.from(env.authTag, 'base64'));
return Buffer.concat([d.update(Buffer.from(env.ciphertext, 'base64')), d.final()]).toString('utf8');
}
/** 原地解密 JSON 中的全部 $wbEncrypted 信封字段;失败字段保留原值,不抛错。 */
function wdCompatDecryptAuthJson(json) {
if (!json || typeof json !== 'object') return json;
const walk = (node) => {
if (Array.isArray(node)) { node.forEach(walk); return; }
if (!node || typeof node !== 'object') return;
for (const k of Object.keys(node)) {
const v = node[k];
if (isWbEncryptedEnvelope(v)) {
try {
const env = JSON.parse(Buffer.from(v.envelope, 'base64').toString('utf8'));
node[k] = wdCompatOpenEnvelope(env, wdCompatStaticKey());
WD_COMPAT.decOk++;
} catch (e) {
WD_COMPAT.decFail++;
wdCompatLog(`解密失败 ${k}: ${String(e.message || e).slice(0, 80)}`);
}
} else if (v && typeof v === 'object') walk(v);
}
};
walk(json);
return json;
}
/** 按原备份的字段信封加密刷新结果;密钥或模板不可用时拒绝写入,绝不降级明文。 */
function wdCompatEncryptAuthJson(json, template) {
if (!wdCompatContainsEncryptedFields(template)) throw new Error('missing-encryption-template');
const key = wdCompatStaticKey();
let fallbackEnvelope = null;
const envelopes = new Map();
const inspect = (node) => {
if (isWbEncryptedEnvelope(node)) {
const env = JSON.parse(Buffer.from(node.envelope, 'base64').toString('utf8'));
if (env.suite !== 1) throw new Error('unsupported-encryption-suite');
wdCompatOpenEnvelope(env, key); // 先验证原信封及密钥,再允许刷新/写入。
envelopes.set(node, env);
if (!fallbackEnvelope) fallbackEnvelope = env;
} else if (node && typeof node === 'object') Object.values(node).forEach(inspect);
};
inspect(template);
const seal = (value, env) => {
const nonce = crypto.randomBytes(12);
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce, { authTagLength: 16 });
cipher.setAAD(wdCompatEnvelopeAAD(env));
const ciphertext = Buffer.concat([cipher.update(value, 'utf8'), cipher.final()]);
return { $wbEncrypted: 1, envelope: Buffer.from(JSON.stringify({
suite: env.suite, keyId: env.keyId || '', nonce: nonce.toString('base64'),
authTag: cipher.getAuthTag().toString('base64'), ciphertext: ciphertext.toString('base64'),
})).toString('base64') };
};
const credentialKeys = new Set(['accessToken', 'access_token', 'refreshToken', 'refresh_token', 'idToken', 'id_token', 'token']);
const walk = (value, original, inAuth = false, field = '') => {
if (isWbEncryptedEnvelope(original)) {
if (isWbEncryptedEnvelope(value) && value.envelope === original.envelope) return { ...original };
if (typeof value !== 'string') throw new Error('invalid-encrypted-field-type');
return seal(value, envelopes.get(original));
}
if (inAuth && credentialKeys.has(field) && typeof value === 'string') return seal(value, fallbackEnvelope);
if (Array.isArray(value)) return value.map((item, index) => walk(item, original && original[index], inAuth));
if (value && typeof value === 'object') {
const result = {};
for (const name of Object.keys(value)) result[name] = walk(value[name], original && original[name], inAuth || name === 'auth', name);
return result;
}
return value;
};
return walk(json, template);
}
// ===================== [wd-compat] 适配层结束 =====================
function samePath(a, b) {
return !!a && !!b && path.resolve(a) === path.resolve(b);
}
function isLegacyDataDir(dataDir) {
return !!LEGACY_DATA_DIR && samePath(dataDir, LEGACY_DATA_DIR);
}
// 登录凭据文件:<扩展数据根>/CodeBuddyExtension/Data/Public/auth/workbuddy-desktop.info
// macOS: ~/Library/Application Support/... Windows: %LOCALAPPDATA%\... Linux: ~/.local/share/...
// 正常路径已由 profile 给出;此处的兜底仅在 profile 未提供 authFile 时使用。
const ACTIVE_PROFILE = getProfile();
const AUTH_FILE = process.env.WBSWITCH_AUTH_FILE !== undefined
? process.env.WBSWITCH_AUTH_FILE
: (ACTIVE_PROFILE.authFile === null ? null : (ACTIVE_PROFILE.authFile || path.join(
plat.extensionAuth,
'workbuddy-desktop.info'
)));
const LOGOUT_MARKER = `${AUTH_FILE}.logged-out`;
const EXPLICIT_AUTH_FILE = process.env.WBSWITCH_AUTH_FILE !== undefined;
const DYNAMIC_AUTH_DISCOVERY = ACTIVE_PROFILE.kind !== 'codebuddy' && !EXPLICIT_AUTH_FILE && !!ACTIVE_PROFILE.authFile && !!ACTIVE_PROFILE.capabilities.accounts;
function authDir(file = AUTH_FILE) {
return file ? path.dirname(path.resolve(file)) : null;
}
function safeAuthFileName(name) {
const value = String(name || '');
return value.length > 0 && value.length <= 255 && path.basename(value) === value &&
!value.includes('\0') && !value.endsWith('.tmp') && /\.info$/i.test(value);
}
function normalizeAuthDomain(value) {
const raw = String(value || '').trim();
if (!raw) return '';
try {
const url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`);
return url.origin.toLowerCase().replace(/\/$/, '');
} catch (_) {
return '';
}
}
function tokenIssuerOrigin(accessToken) {
try {
const part = String(accessToken || '').split('.')[1];
if (!part) return '';
const padded = part.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - (part.length % 4)) % 4);
const payload = JSON.parse(Buffer.from(padded, 'base64').toString('utf8'));
return normalizeAuthDomain(payload.iss);
} catch (_) {
return '';
}
}
function allowedAuthOrigins(profile = ACTIVE_PROFILE) {
const origins = new Set();
const profileOrigin = normalizeAuthDomain(profile.apiHost);
if (profileOrigin) origins.add(profileOrigin);
if (profile.region === 'cn') {
origins.add('https://www.workbuddy.cn');
origins.add('https://www.codebuddy.cn');
// 国内版新版 Keycloak issuer;保留旧域名以兼容已有账号备份。
origins.add('https://copilot.tencent.com');
} else if (profile.region === 'intl') {
origins.add('https://www.workbuddy.ai');
origins.add('https://www.codebuddy.ai');
}
return origins;
}
function authRecordFromJson(file, json, { strict = DYNAMIC_AUTH_DISCOVERY } = {}) {
if (!json || typeof json !== 'object' || Array.isArray(json)) return null;
const acct = json.account || (Array.isArray(json.accounts) && json.accounts[0]) || null;
if (!acct || !acct.uid) return null;
const auth = json.auth && typeof json.auth === 'object' ? json.auth : {};
const rawToken = auth.accessToken ?? auth.access_token ?? auth.token;
// [wd-compat] 信封对象不能 String()——避免 "[object Object]" 污染 issuer 校验
const accessToken = wdCompatAuthToken(auth);
const encryptedToken = isWbEncryptedEnvelope(rawToken);
const authDomain = normalizeAuthDomain(auth.domain || auth.issuer || '');
const authIssuer = tokenIssuerOrigin(accessToken);
if (strict) {
if (!wdCompatHasAuthCredential(auth)) return null;
const allowed = allowedAuthOrigins();
// 无法取钥时无法从信封解析 issuer;此时只接受明确属于当前客户端的 domain。
if (![authDomain, authIssuer].some((origin) => origin && allowed.has(origin))) return null;
}
return {
uid: String(acct.uid),
nickname: wdCompatText(acct.nickname),
uin: typeof acct.uin === 'string' || typeof acct.uin === 'number' ? acct.uin : '',
phone: wdCompatText(acct.phoneNumber),
type: typeof acct.type === 'string' ? acct.type : '',
enterpriseId: String(acct.enterpriseId || '').trim(),
accountId: accountIdentity(acct),
raw: json,
file,
authFileName: file ? path.basename(file) : '',
authDomain,
authIssuer,
tokenEncrypted: encryptedToken,
lastLogin: acct.lastLogin === true,
lastRefreshTime: Number(auth.lastRefreshTime) || 0,
};
}
function parseAuthFile(file, options = {}) {
if (!file || !fs.existsSync(file)) return null;
try {
const json = JSON.parse(fs.readFileSync(file, 'utf8'));
wdCompatDecryptAuthJson(json); // [wd-compat] 5.6+ 字段信封读取端解密
return authRecordFromJson(file, json, options);
} catch (_) {
return null;
}
}
function parseAuthJson(json, options = {}) {
json = wdCompatDecryptAuthJson(json); // [wd-compat]
return authRecordFromJson(null, json, options);
}
/**
* Normalize a user-supplied account JSON without destroying encrypted fields.
* Validation runs on a deep clone because parseAuthJson may decrypt in place.
*/
function normalizeAccountImportJson(candidate) {
const source = candidate && typeof candidate === 'object' && !Array.isArray(candidate) ? candidate : null;
if (!source) return null;
const acct = source.account && typeof source.account === 'object' ? source.account : source;
const auth = source.auth && typeof source.auth === 'object' ? source.auth : null;
const uid = String(acct && acct.uid || '').trim();
const rawToken = auth && (auth.accessToken ?? auth.access_token ?? auth.token);
const accessToken = typeof rawToken === 'string' ? rawToken.trim() : rawToken;
if (!/^[A-Za-z0-9_-]{1,128}$/.test(uid)) return null;
if (!(typeof accessToken === 'string' ? accessToken : isWbEncryptedEnvelope(accessToken))) return null;
const normalized = {
account: { ...acct, uid },
auth: { ...auth, accessToken },
};
let authRecord;
try {
authRecord = parseAuthJson(JSON.parse(JSON.stringify(normalized)));
} catch (_) {
return null;
}
return authRecord && authRecord.uid === uid ? { uid, normalized, authRecord } : null;
}
function listAuthRecords() {
if (!AUTH_FILE) return [];
if (!DYNAMIC_AUTH_DISCOVERY) {
const record = parseAuthFile(AUTH_FILE, { strict: false });
return record ? [record] : [];
}
const dir = authDir();
let names;
try { names = fs.readdirSync(dir); } catch (_) { return []; }
return names
.filter(safeAuthFileName)
.map((name) => parseAuthFile(path.join(dir, name)))
.filter(Boolean);
}
function resolveCurrentAuth() {
if (!DYNAMIC_AUTH_DISCOVERY) return { file: AUTH_FILE, record: parseAuthFile(AUTH_FILE, { strict: false }), ambiguous: false };
const records = listAuthRecords();
if (records.length === 1) return { file: records[0].file, record: records[0], ambiguous: false };
// 官方固定文件(workbuddy-desktop.info / workbuddy-desktop-ai.info)是官方实际读取的
// 当前登录文件:多份历史备份可能都残留 lastLogin 标记,只有固定文件是可信的「当前」。
// 固定文件有效时优先采用,避免被历史标记拖入 ambiguous 而拒绝切换/备份。
const canonical = records.find((record) => AUTH_FILE && path.resolve(record.file) === path.resolve(AUTH_FILE));
if (canonical) return { file: canonical.file, record: canonical, ambiguous: false };
const marked = records.filter((record) => record.lastLogin);
if (records.length > 1 && marked.length === 1) return { file: marked[0].file, record: marked[0], ambiguous: false };
return { file: null, record: null, ambiguous: records.length > 1, records };
}
function currentAuthFile() {
return resolveCurrentAuth().file;
}
function resolveLogoutAuth() {
const resolution = resolveCurrentAuth();
if (resolution.file || resolution.ambiguous || !DYNAMIC_AUTH_DISCOVERY) return resolution;
// 假退出已删掉登录文件,扫码取消后可以再次打开登录页。仅在认证目录
// 可读且没有任何 info 文件时使用官方固定路径;未知/损坏文件仍拒绝猜测。
try {
if (!fs.readdirSync(authDir()).some(safeAuthFileName)) {
return { file: AUTH_FILE, record: null, ambiguous: false };
}
} catch (_) { /* 不把权限或路径错误当成未登录 */ }
return resolution;
}
function defaultDataDir() {
// 旧版 launchd 可能把 WBSWITCH_DATA_DIR 设成 HelloBuddy;新版本始终落到 WorkDaddy,
// 避免旧服务被新 daemon 拉起后继续写入旧目录。
const configured = process.env.WBSWITCH_DATA_DIR;
return configured && !isLegacyDataDir(configured) ? configured : profileDataDir(ACTIVE_PROFILE);
}
function accountsDir(dataDir) {
return path.join(dataDir, 'accounts');
}
function metaFile(dataDir) {
return path.join(dataDir, 'meta.json');
}
function workbuddyModelsFile() {
return ACTIVE_PROFILE.modelsFile || path.join(os.homedir(), '.workbuddy', 'models.json');
}
function isManagedProfileDataDir(dataDir) {
const root = path.resolve(sharedDataDir());
const current = path.resolve(dataDir || '');
return current === root || current.startsWith(root + path.sep + 'profiles' + path.sep);
}
function migrateLegacyModelBackups(dataDir) {
if (!isManagedProfileDataDir(dataDir)) return;
const root = sharedDataDir();
const target = path.join(root, 'models');
const marker = path.join(target, '.legacy-migrated-v1');
fs.mkdirSync(target, { recursive: true, mode: 0o700 });
// 迁移只允许发生一次。旧 profile 目录保留作兼容/恢复,但不能在用户删除
// 共享备份后再次把同一个文件复制回来。
if (fs.existsSync(marker)) return;
let profileDirs = [];
try { profileDirs = fs.readdirSync(path.join(root, 'profiles'), { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => path.join(root, 'profiles', entry.name, 'models')); } catch (_) {}
for (const source of profileDirs) {
let names = [];
try { names = fs.readdirSync(source).filter((name) => /^[A-Za-z0-9_-]{8,100}\.json$/.test(name)); } catch (_) { continue; }
for (const name of names) {
const from = path.join(source, name);
const to = path.join(target, name);
if (fs.existsSync(to)) continue;
try { fs.copyFileSync(from, to); fs.chmodSync(to, 0o600); } catch (_) {}
}
}
try {
fs.writeFileSync(marker, JSON.stringify({ version: 1, migratedAt: new Date().toISOString(), deleted: [] }) + '\n', { mode: 0o600 });
fs.chmodSync(marker, 0o600);
} catch (_) {}
}
function modelBackupsDir(dataDir) {
if (isManagedProfileDataDir(dataDir)) {
migrateLegacyModelBackups(dataDir);
return path.join(sharedDataDir(), 'models');
}
return path.join(dataDir, 'models');
}
function maskApiKey(apiKey) {
const value = String(apiKey || '');
if (!value) return '';
if (value.length <= 8) return '••••••';
const prefix = value.slice(0, Math.min(3, value.length - 4));
const suffix = value.slice(-4);
const middleLength = Math.max(1, value.length - prefix.length - suffix.length);
return `${prefix}${'•'.repeat(middleLength)}${suffix}`;
}
// 模型列表摘要。默认脱敏 apiKey;UI 需要明文展示(模型页 cell / 编辑弹窗)时传 { revealKey: true }。
function sanitizeModel(model, opts) {
const value = model && typeof model === 'object' && !Array.isArray(model) ? model : {};
const revealKey = !!(opts && opts.revealKey);
return {
id: String(value.id || value.name || ''),
name: String(value.name || value.id || ''),
vendor: String(value.vendor || ''),
url: String(value.url || '').split('?')[0].split('#')[0],
apiKey: revealKey ? String(value.apiKey || '') : maskApiKey(value.apiKey),
supportsToolCall: !!value.supportsToolCall,
supportsImages: !!value.supportsImages,
supportsReasoning: !!value.supportsReasoning,
};
}
function checkinDisplayValue(record, today) {
if (!record || record.date !== today || !record.ok) return null;
return { ok: !!record.ok, already: !!record.already, code: record.code, message: record.message };
}
function readModelsFile(file = workbuddyModelsFile()) {
// 文件不存在视为"还没有模型",返回空列表而不是抛错——模型页应显示
// "当前还未添加模型"占位,而不是"当前模型加载失败: 未找到模型配置文件"。
if (!fs.existsSync(file)) return { file, format: 'array', models: [] };
let parsed;
try {
parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw new Error(`模型配置文件不是有效 JSON: ${e.message}`);
}
if (Array.isArray(parsed)) return { file, format: 'array', models: parsed };
if (parsed && typeof parsed === 'object' && Array.isArray(parsed.models)) {
return { file, format: 'object', models: parsed.models, wrapper: parsed };
}
throw new Error('模型配置文件格式不受支持:应为数组或包含 models 数组的对象');
}
function writeModelsFile(parsed, models) {
const output = parsed.format === 'array' ? models : Object.assign({}, parsed.wrapper, { models });
const file = parsed.file;
const dir = path.dirname(file);
fs.mkdirSync(dir, { recursive: true });
let mode = 0o600;
try { mode = fs.statSync(file).mode & 0o777; } catch (_) {}
const tmp = `${file}.tmp.${process.pid}`;
fs.writeFileSync(tmp, JSON.stringify(output, null, 2) + '\n', { mode });
fs.renameSync(tmp, file);
try { fs.chmodSync(file, mode); } catch (_) {}
}
function modelBackupPath(dataDir, backupId) {
const id = String(backupId || '');
if (!/^[A-Za-z0-9_-]{8,100}$/.test(id)) throw new Error('非法模型备份标识');
return path.join(modelBackupsDir(dataDir), `${id}.json`);
}
function readModelBackup(dataDir, backupId) {
const file = modelBackupPath(dataDir, backupId);
if (!fs.existsSync(file)) throw new Error('模型备份不存在');
let record;
try { record = JSON.parse(fs.readFileSync(file, 'utf8')); } catch (e) { throw new Error(`模型备份损坏: ${e.message}`); }
if (!record || record.schema !== 1 || !record.model || typeof record.model !== 'object' || Array.isArray(record.model)) {
throw new Error('模型备份格式不受支持');
}
return { file, record };
}
function listModelBackups(dataDir) {
const dir = modelBackupsDir(dataDir);
let names = [];
try { names = fs.readdirSync(dir).filter((name) => /^[A-Za-z0-9_-]{8,100}\.json$/.test(name)); } catch (_) {}
const records = [];
for (const name of names) {
const backupId = name.slice(0, -5);
try {
const { record } = readModelBackup(dataDir, backupId);
const model = record.model;
const id = typeof model.id === 'string' ? model.id.trim() : '';
const summary = sanitizeModel(model, { revealKey: true });
records.push({ backupId, createdAt: record.createdAt || null, ...summary, id });
} catch (_) {
// Ignore damaged files in the list; an explicit enable/delete still reports an error.
}
}
records.sort((a, b) => String(b.createdAt || '').localeCompare(String(a.createdAt || '')));
const groups = new Map();
for (const record of records) {
// 分组严格依据原始模型配置的 id(模型名),不能使用用户自定义 name。
const key = record.id || '(未命名模型)';
if (!groups.has(key)) groups.set(key, { id: key, items: [] });
groups.get(key).items.push(record);
}
return Array.from(groups.values());
}
function listOfficialModels(file = workbuddyModelsFile()) {
const parsed = readModelsFile(file);
return parsed.models.map((model, index) => Object.assign({ index }, sanitizeModel(model, { revealKey: true })));
}
function readOfficialModel(file = workbuddyModelsFile(), index) {
const parsed = readModelsFile(file);
const position = Number(index);
if (!Number.isInteger(position) || position < 0 || position >= parsed.models.length) throw new Error('模型索引无效');
const model = parsed.models[position];
if (!model || typeof model !== 'object' || Array.isArray(model)) throw new Error('模型配置无效');
return model;
}
function deleteOfficialModels(file = workbuddyModelsFile(), indexes) {
const parsed = readModelsFile(file);
const positions = Array.isArray(indexes)
? Array.from(new Set(indexes.map((value) => Number(value)).filter((value) => Number.isInteger(value) && value >= 0 && value < parsed.models.length)))
: [];
if (!positions.length) throw new Error('未选择当前模型');
const selected = new Set(positions);
const next = parsed.models.filter((_, index) => !selected.has(index));
writeModelsFile(parsed, next);
return { deleted: positions.length, official: next.map((model, index) => Object.assign({ index }, sanitizeModel(model))) };
}
function backupOfficialModel(dataDir, index, modelsFile = workbuddyModelsFile()) {
const parsed = readModelsFile(modelsFile);
const position = Number(index);
if (!Number.isInteger(position) || position < 0 || position >= parsed.models.length) throw new Error('模型索引无效');
const model = parsed.models[position];
if (!model || typeof model !== 'object' || Array.isArray(model)) throw new Error('模型配置无效');
const backupId = crypto.randomUUID();
const createdAt = new Date().toISOString();
fs.mkdirSync(modelBackupsDir(dataDir), { recursive: true, mode: 0o700 });
const file = modelBackupPath(dataDir, backupId);
fs.writeFileSync(file, JSON.stringify({ schema: 1, backupId, createdAt, model }, null, 2) + '\n', { mode: 0o600 });
return { backupId, createdAt, ...sanitizeModel(model) };
}
function writeModelBackup(dataDir, record) {
fs.mkdirSync(modelBackupsDir(dataDir), { recursive: true, mode: 0o700 });
const file = modelBackupPath(dataDir, record.backupId);
const tmp = `${file}.tmp.${process.pid}`;
fs.writeFileSync(tmp, JSON.stringify(record, null, 2) + '\n', { mode: 0o600 });
fs.renameSync(tmp, file);
try { fs.chmodSync(file, 0o600); } catch (_) {}
}
function copyModelBackup(dataDir, backupId) {
const { record } = readModelBackup(dataDir, backupId);
const copied = Object.assign({}, record, {
backupId: crypto.randomUUID(),
createdAt: new Date().toISOString(),
model: Object.assign({}, record.model),
});
writeModelBackup(dataDir, copied);
return { backupId: copied.backupId, createdAt: copied.createdAt, ...sanitizeModel(copied.model) };
}
function editModelBackup(dataDir, backupId, patch) {
const { record } = readModelBackup(dataDir, backupId);
const input = patch && typeof patch === 'object' && !Array.isArray(patch) ? patch : {};
const model = Object.assign({}, record.model);
// 模型名对应配置里的 id(例如 deepseek-v4-flash),name 是用户自定义的显示名称;两者都允许编辑。
for (const field of ['id', 'name', 'url', 'apiKey']) {
if (!Object.prototype.hasOwnProperty.call(input, field)) continue;
if (typeof input[field] !== 'string' || input[field].length > 20000) throw new Error(`模型${field}格式无效`);
model[field] = input[field];
}
const modelId = String(model.id || model.name || '').trim();
if (!modelId) throw new Error('模型备份缺少 id/name,无法保存');
model.id = modelId;
if (!String(model.name || '').trim()) model.name = modelId;
const updated = Object.assign({}, record, { model });
writeModelBackup(dataDir, updated);
return { backupId: updated.backupId, createdAt: updated.createdAt || null, ...sanitizeModel(model) };
}
function deleteModelBackups(dataDir, backupIds) {
const ids = Array.isArray(backupIds) ? backupIds : [];
let deleted = 0;
const removed = [];
for (const id of ids) {
try {
const file = modelBackupPath(dataDir, id);
if (fs.existsSync(file)) { fs.unlinkSync(file); deleted++; removed.push(String(id)); }
} catch (_) {}
}
if (removed.length && isManagedProfileDataDir(dataDir)) {
const marker = path.join(sharedDataDir(), 'models', '.legacy-migrated-v1');
try {
const state = JSON.parse(fs.readFileSync(marker, 'utf8'));
const deletedIds = new Set(Array.isArray(state.deleted) ? state.deleted : []);
removed.forEach((id) => deletedIds.add(id));
fs.writeFileSync(marker, JSON.stringify(Object.assign({}, state, { deleted: Array.from(deletedIds) })) + '\n', { mode: 0o600 });
fs.chmodSync(marker, 0o600);
} catch (_) {}
}
return deleted;
}
function enableModelBackup(dataDir, backupId, file = workbuddyModelsFile()) {
const { record } = readModelBackup(dataDir, backupId);
const modelId = String(record.model.id || record.model.name || '').trim();
if (!modelId) throw new Error('模型备份缺少 id/name,无法启用');
const parsed = readModelsFile(file);
const models = parsed.models.slice();
const first = models.findIndex((model) => String(model && (model.id || model.name) || '') === modelId);
const next = [];
let inserted = false;
for (const model of models) {
const id = String(model && (model.id || model.name) || '');
if (id === modelId) {
if (!inserted) { next.push(record.model); inserted = true; }
} else next.push(model);
}
if (!inserted) next.push(record.model);
writeModelsFile(parsed, next);
return { backupId, id: modelId, replaced: first >= 0, ...sanitizeModel(record.model) };
}
function modelImportName(model) {
return String(model && (model.name || model.id) || '').trim();
}
function importModels(targetFile, sourceFile) {
const target = readModelsFile(targetFile);
const source = readModelsFile(sourceFile);
if (path.resolve(target.file) === path.resolve(source.file)) throw new Error('不能从当前客户端导入模型');
const existing = new Set(target.models.map(modelImportName).filter(Boolean));
const imported = [];
const skipped = [];
for (const model of source.models) {
if (!model || typeof model !== 'object' || Array.isArray(model)) continue;
const name = modelImportName(model);
if (!name || existing.has(name)) { if (name) skipped.push(name); continue; }
target.models.push(model);
existing.add(name);
imported.push(name);
}
if (imported.length) writeModelsFile(target, target.models);
return { imported, skipped, official: target.models.map((model, index) => Object.assign({ index }, sanitizeModel(model, { revealKey: true }))) };
}
function readMeta(dataDir) {
let meta = { accounts: {} };
try {
meta = JSON.parse(fs.readFileSync(metaFile(dataDir), 'utf8'));
} catch (_) {
/* 首次运行或旧版本没有 meta.json */
}
if (!meta || typeof meta !== 'object' || Array.isArray(meta)) meta = {};
if (!meta.accounts || typeof meta.accounts !== 'object' || Array.isArray(meta.accounts)) meta.accounts = {};
return meta;
}
function writeMeta(dataDir, meta) {
ensureDirs(dataDir);
const mf = metaFile(dataDir);
const tmp = `${mf}.tmp.${process.pid}`;
fs.writeFileSync(tmp, JSON.stringify(meta, null, 2), { mode: 0o600 });
fs.renameSync(tmp, mf);
try { fs.chmodSync(mf, 0o600); } catch (_) {}
}
/** 使用稳定路径键,不要求路径当前存在(空间可能已被移动或卸载)。 */
function canonicalWorkspace(cwd) {
let value = String(cwd || '').trim();
if (!value) return '';
value = value.replace(/\\/g, '/');
value = path.posix.normalize(value);
if (value === '.') return '';
if (value.length > 1) value = value.replace(/\/+$/, '');
// Windows filesystem matching is case-insensitive, but this key is also
// displayed to users and must retain WorkBuddy's original path spelling.
return value;
}
function ensureAutoCopyMeta(meta) {
const current = meta.autoCopy;
if (current && current.version === 2 && current.sessions && current.sessionIndex && current.workspaces && current.copies) {
return current;
}
// 1.0.15 stored rules under sourceUid. Convert them once to global session lineages
// and global workspace paths so a migration/copy keeps the same shared identity.
const legacy = current && typeof current === 'object' && !Array.isArray(current) ? current : {};
const next = { version: 2, allSessions: false, sessions: {}, sessionIndex: {}, workspaces: {}, copies: {} };
const legacySessions = legacy.sessions && typeof legacy.sessions === 'object' ? legacy.sessions : {};
for (const sourceUid of Object.keys(legacySessions)) {
const bucket = legacySessions[sourceUid];
if (!bucket || typeof bucket !== 'object' || Array.isArray(bucket)) continue;
for (const sessionId of Object.keys(bucket)) {
const legacyRule = bucket[sessionId];
if (legacyRule === false || (legacyRule && typeof legacyRule === 'object' && legacyRule.enabled === false)) continue;
const lineageId = crypto.randomUUID();
next.sessions[lineageId] = { enabled: true, members: [{ uid: sourceUid, id: sessionId }], createdAt: Date.now() };
if (!next.sessionIndex[sourceUid]) next.sessionIndex[sourceUid] = {};
next.sessionIndex[sourceUid][sessionId] = lineageId;
}
}
const legacyWorkspaces = legacy.workspaces && typeof legacy.workspaces === 'object' ? legacy.workspaces : {};
for (const sourceUid of Object.keys(legacyWorkspaces)) {
const bucket = legacyWorkspaces[sourceUid];
if (!bucket || typeof bucket !== 'object' || Array.isArray(bucket)) continue;
for (const cwd of Object.keys(bucket)) {
if (bucket[cwd] === false) continue;
const canonical = canonicalWorkspace(cwd);
if (canonical) next.workspaces[canonical] = String(bucket[cwd] || cwd);
}
}
const legacyCopies = legacy.copies && typeof legacy.copies === 'object' ? legacy.copies : {};
for (const oldKey of Object.keys(legacyCopies)) {
try {
const parts = JSON.parse(oldKey);
if (!Array.isArray(parts) || parts.length !== 3) continue;
const lineageId = next.sessionIndex[String(parts[0] || '')] && next.sessionIndex[String(parts[0] || '')][String(parts[2] || '')];
if (lineageId) next.copies[JSON.stringify([lineageId, String(parts[1] || '')])] = legacyCopies[oldKey];
} catch (_) {}
}
meta.autoCopy = next;
return next;
}
// Auto-copy rules are read several times per session during a sync batch.
// Memoize on the meta file fingerprint; all writes go through writeMeta
// (tmp + rename), which always bumps mtime, so a stale view lasts at most one
// call and self-corrects on the next stat.
const autoCopyConfigCache = new Map();
function readAutoCopyConfig(dataDir) {
let fingerprint = null;
try { const stat = fs.statSync(metaFile(dataDir)); fingerprint = stat.mtimeMs + ':' + stat.size; } catch (_) {}
const cached = autoCopyConfigCache.get(dataDir);
if (cached && cached.fingerprint === fingerprint) return cached.value;
const meta = readMeta(dataDir);
const wasCurrent = !!(meta.autoCopy && meta.autoCopy.version === 2);
const autoCopy = ensureAutoCopyMeta(meta);
if (!wasCurrent) {
writeMeta(dataDir, meta);
try { const stat = fs.statSync(metaFile(dataDir)); fingerprint = stat.mtimeMs + ':' + stat.size; } catch (_) { fingerprint = null; }
}
const value = {
allSessions: autoCopy.allSessions === true,
sessions: autoCopy.sessions,
sessionIndex: autoCopy.sessionIndex,
workspaces: autoCopy.workspaces,
copies: autoCopy.copies,
};
autoCopyConfigCache.set(dataDir, { fingerprint, value });
return value;
}
function autoCopyRuleKey(lineageId, targetUid) {
return JSON.stringify([String(lineageId || ''), String(targetUid || '')]);
}
function getAutoCopyRules(dataDir, uid) {
const config = readAutoCopyConfig(dataDir);
const sourceUid = String(uid || '').trim();
const index = config.sessionIndex[sourceUid] || {};
const sessionIds = [];
const lineages = {};
const allLineages = {};
const branchSessionIds = [];
for (const sessionId of Object.keys(index)) {
const lineageId = index[sessionId];
const lineage = config.sessions[lineageId];
if (lineage) {
allLineages[sessionId] = lineageId;
if ((Array.isArray(lineage.members) ? lineage.members : []).some(member => member && member.uid === sourceUid && member.id === sessionId && member.branchCopy === true)) branchSessionIds.push(sessionId);
}
if (lineage && lineage.enabled !== false) {
sessionIds.push(sessionId);
lineages[sessionId] = lineageId;
}
}
return {
allSessions: config.allSessions === true,
sessionIds,
lineages,
allLineages,
branchSessionIds,
workspaces: Object.keys(config.workspaces),
};
}
// Hide redundant legacy copies in lists, but keep explicit branch copies
// visible beside the original. Sync plans still inspect every physical row.
function dedupeAutoCopySessionRows(rows, lineagesByUid, branchesByUid = {}) {
if (!Array.isArray(rows)) return [];
const seen = new Set();
return rows.filter((row) => {
const uid = String(row && row.user_id || '').trim();
const id = String(row && row.id || '').trim();
const lineageId = lineagesByUid && lineagesByUid[uid] && lineagesByUid[uid][id];
if (!lineageId || branchesByUid[uid]?.has(id)) return true;
const key = uid + '::' + String(lineageId);
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
function setAutoCopyAllSessions(dataDir, enabled) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
config.allSessions = enabled === true;
writeMeta(dataDir, meta);
return { allSessions: config.allSessions };
}
function isAutoCopySessionSelected(rules, session) {
if (rules && rules.allSessions === true) return true;
const sessionId = String(session && session.id || '');
const workspace = canonicalWorkspace(session && session.cwd);
const sessionIds = rules && Array.isArray(rules.sessionIds) ? rules.sessionIds : [];
const workspaces = rules && Array.isArray(rules.workspaces) ? rules.workspaces : [];
return sessionIds.some((id) => String(id) === sessionId)
|| workspaces.some((cwd) => canonicalWorkspace(cwd) === workspace);
}
function setAutoCopyRule(dataDir, { uid, kind, key, enabled }) {
const sourceUid = String(uid || '').trim();
if (kind !== 'session' && kind !== 'workspace') throw new Error('无效的自动复制规则类型');
const value = kind === 'workspace' ? canonicalWorkspace(key) : String(key || '').trim();
if (!value) throw new Error('缺少自动复制规则标识');
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
if (kind === 'workspace') {
if (enabled) config.workspaces[value] = String(key || '').trim();
else delete config.workspaces[value];
writeMeta(dataDir, meta);
return getAutoCopyRules(dataDir, sourceUid);
}
if (!sourceUid) throw new Error('缺少源账号 uid');
if (!config.sessionIndex[sourceUid]) config.sessionIndex[sourceUid] = {};
const lineageId = config.sessionIndex[sourceUid][value];
if (enabled) {
const lineage = lineageId && config.sessions[lineageId]
? config.sessions[lineageId]
: { enabled: true, members: [], createdAt: Date.now() };
if (!lineageId) {
const createdId = crypto.randomUUID();
config.sessions[createdId] = lineage;
config.sessionIndex[sourceUid][value] = createdId;
addLineageMember(lineage, sourceUid, value);
} else {
lineage.enabled = true;
addLineageMember(lineage, sourceUid, value);
}
} else {
if (lineageId && config.sessions[lineageId]) config.sessions[lineageId].enabled = false;
}
writeMeta(dataDir, meta);
return getAutoCopyRules(dataDir, sourceUid);
}
function addLineageMember(lineage, uid, id) {
if (!Array.isArray(lineage.members)) lineage.members = [];
if (!lineage.members.some((member) => member && member.uid === uid && member.id === id)) {
lineage.members.push({ uid, id });
}
}
function getAutoCopySession(dataDir, uid, sessionId) {
const config = readAutoCopyConfig(dataDir);
const lineageId = config.sessionIndex[String(uid || '').trim()] && config.sessionIndex[String(uid || '').trim()][String(sessionId || '').trim()];
const lineage = lineageId ? config.sessions[lineageId] : null;
return { lineageId: lineageId || null, enabled: !!(lineage && lineage.enabled !== false) };
}
// Return all persisted session ids for one account in a lineage. A lineage
// should have at most one live session per uid, but keeping the full list lets
// the copier repair stale mappings without creating another row.
function getAutoCopySessionMembers(dataDir, lineageId, uid) {
const config = readAutoCopyConfig(dataDir);
const lineage = config.sessions[String(lineageId || '')];
if (!lineage || !Array.isArray(lineage.members)) return [];
const targetUid = uid === undefined || uid === null ? null : String(uid);
const seen = new Set();
const result = [];
for (const member of lineage.members) {
if (!member || (targetUid !== null && String(member.uid || '') !== targetUid)) continue;
const id = String(member.id || '').trim();
if (id && !seen.has(id)) {
seen.add(id);
result.push(id);
}
}
return result;
}
// Return the de-duplicated account/session pairs for a lineage. The copier
// needs the uid as well as the id so it can refresh every live account member,
// not only the account that happened to be active during the switch.
function getAutoCopySessionMemberRecords(dataDir, lineageId) {
const config = readAutoCopyConfig(dataDir);
const lineage = config.sessions[String(lineageId || '')];
if (!lineage || !Array.isArray(lineage.members)) return [];
const seen = new Set();
const result = [];
for (const member of lineage.members) {
const uid = String(member && member.uid || '').trim();
const id = String(member && member.id || '').trim();
if (!uid || !id) continue;
const key = JSON.stringify([uid, id]);
if (seen.has(key)) continue;
seen.add(key);
result.push({ uid, id });
}
return result;
}
// Pick the freshest member snapshot. Message files are authoritative; the
// database timestamp is only a deterministic fallback for legacy/missing
// files. Ties are resolved by member order so repeated switches stay stable.
function selectLatestAutoCopyMember(members) {
if (!Array.isArray(members) || !members.length) return null;
let best = null;
members.forEach((member, index) => {
if (!member || !member.id) return;
const contentMtime = Number(member.contentMtime || 0);
const updatedAt = Number(member.updatedAt || 0);
if (!best || contentMtime > best.contentMtime ||
(contentMtime === best.contentMtime && updatedAt > best.updatedAt)) {
best = Object.assign({ memberIndex: index }, member, { contentMtime, updatedAt });
}
});
return best ? members[best.memberIndex] : null;
}
function ensureAutoCopySessions(dataDir, uid, sessionIds, options) {
const meta = readMeta(dataDir);
const previousConfig = meta.autoCopy;
const config = ensureAutoCopyMeta(meta);
let changed = config !== previousConfig;
const sourceUid = String(uid || '').trim();
const ids = Array.from(new Set((Array.isArray(sessionIds) ? sessionIds : [sessionIds])
.map((id) => String(id || '').trim())
.filter(Boolean)));
if (!sourceUid || !ids.length) throw new Error('缺少共享会话标识');
if (!config.sessionIndex[sourceUid]) {
config.sessionIndex[sourceUid] = {};
changed = true;
}
const lineages = {};
ids.forEach((id) => {
let lineageId = config.sessionIndex[sourceUid][id];
if (!lineageId || !config.sessions[lineageId]) {
lineageId = crypto.randomUUID();
config.sessions[lineageId] = { enabled: !(options && options.enabled === false), members: [], createdAt: Date.now() };
config.sessionIndex[sourceUid][id] = lineageId;
changed = true;
}
const lineage = config.sessions[lineageId];
const hasMember = Array.isArray(lineage.members)
&& lineage.members.some((member) => member && member.uid === sourceUid && member.id === id);
addLineageMember(lineage, sourceUid, id);
if (!hasMember) changed = true;
lineages[id] = lineageId;
});
if (changed) writeMeta(dataDir, meta);
return lineages;
}
function ensureAutoCopySession(dataDir, uid, sessionId, options) {
const id = String(sessionId || '').trim();
if (!id) throw new Error('缺少共享会话标识');
return ensureAutoCopySessions(dataDir, uid, [id], options)[id];
}
// Audit duplicate physical sessions without changing their lineage. Splitting
// them here makes the next copy treat the detached row as a new logical
// session, which can create another duplicate on every account switch.
function collectAutoCopyDuplicates(config) {
const duplicates = [];
for (const lineageId of Object.keys(config.sessions)) {
const lineage = config.sessions[lineageId];
if (!lineage || !Array.isArray(lineage.members)) continue;
const seenUids = new Set();
for (const member of lineage.members) {
const uid = String(member && member.uid || '').trim();
const id = String(member && member.id || '').trim();
if (!uid || !id) continue;
if (!seenUids.has(uid)) { seenUids.add(uid); continue; }
duplicates.push({ lineageId, uid, id });
}
}
return duplicates;
}
function normalizeAutoCopyLineages(dataDir) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const duplicates = collectAutoCopyDuplicates(config);
const previous = Array.isArray(config.duplicates) ? config.duplicates : [];
const unchanged = previous.length === duplicates.length && previous.every((item, index) => item
&& String(item.lineageId) === duplicates[index].lineageId
&& String(item.uid) === duplicates[index].uid
&& String(item.id) === duplicates[index].id);
if (unchanged) return false;
config.duplicates = duplicates;
writeMeta(dataDir, meta);
return true;
}
function mergeAutoCopyLineages(dataDir, fromLineageId, intoLineageId) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const from = String(fromLineageId || '').trim();
const into = String(intoLineageId || '').trim();
if (!from || !into || from === into) return { ok: false, reason: 'invalid' };
const fromLineage = config.sessions[from];
const intoLineage = config.sessions[into];
if (!fromLineage || !intoLineage) return { ok: false, reason: 'missing' };
if (fromLineage.excludedUids || intoLineage.excludedUids) {
intoLineage.excludedUids = Array.from(new Set([...(intoLineage.excludedUids || []), ...(fromLineage.excludedUids || [])]));
}
const knownMembers = new Set((intoLineage.members || []).map((member) =>
JSON.stringify([String(member && member.uid || ''), String(member && member.id || '')])));
let movedMembers = 0;
for (const member of fromLineage.members || []) {
const uid = String(member && member.uid || '').trim();
const id = String(member && member.id || '').trim();
const key = JSON.stringify([uid, id]);
if (!uid || !id || knownMembers.has(key)) continue;
intoLineage.members = intoLineage.members || [];
intoLineage.members.push({ uid, id });
knownMembers.add(key);
movedMembers++;
}
for (const uid of Object.keys(config.sessionIndex)) {
const index = config.sessionIndex[uid];
for (const sessionId of Object.keys(index || {})) {
if (index[sessionId] === from) index[sessionId] = into;
}
}
for (const key of Object.keys(config.copies || {})) {
let lineageKey;
let targetUid;
try { [lineageKey, targetUid] = JSON.parse(key); } catch (_) { continue; }
if (lineageKey !== from) continue;
const destinationKey = autoCopyRuleKey(into, targetUid);
if (!config.copies[destinationKey]) config.copies[destinationKey] = config.copies[key];
delete config.copies[key];
}
delete config.sessions[from];
config.duplicates = collectAutoCopyDuplicates(config);
writeMeta(dataDir, meta);
return { ok: true, movedMembers };
}
function addAutoCopySessionMember(dataDir, lineageId, uid, sessionId, options = {}) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const lineage = config.sessions[String(lineageId || '')];
if (!lineage) return false;
const sourceUid = String(uid || '').trim();
const id = String(sessionId || '').trim();
if (!sourceUid || !id) return false;
if (!config.sessionIndex[sourceUid]) config.sessionIndex[sourceUid] = {};
const previousLineageId = config.sessionIndex[sourceUid][id];
if (previousLineageId && previousLineageId !== String(lineageId) && config.sessions[previousLineageId]) {
config.sessions[previousLineageId].members = (config.sessions[previousLineageId].members || [])
.filter((member) => !(member && member.uid === sourceUid && member.id === id));
}
config.sessionIndex[sourceUid][id] = String(lineageId);
addLineageMember(lineage, sourceUid, id);
if (options.branchCopy === true) lineage.members.find(member => member.uid === sourceUid && member.id === id).branchCopy = true;
writeMeta(dataDir, meta);
return true;
}
// Remove only one member from a lineage. Unlike removeAutoCopySession this
// intentionally leaves the lineage and other account members intact.
function removeAutoCopySessionMember(dataDir, lineageId, uid, sessionId) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const id = String(lineageId || '').trim();
const sourceUid = String(uid || '').trim();
const sessionIdValue = String(sessionId || '').trim();
const lineage = config.sessions[id];
if (!lineage || !sourceUid || !sessionIdValue) return false;
const before = Array.isArray(lineage.members) ? lineage.members.length : 0;
lineage.members = (lineage.members || []).filter((member) => !(member && String(member.uid || '') === sourceUid && String(member.id || '') === sessionIdValue));
if (config.sessionIndex[sourceUid] && config.sessionIndex[sourceUid][sessionIdValue] === id) {
delete config.sessionIndex[sourceUid][sessionIdValue];
if (!Object.keys(config.sessionIndex[sourceUid]).length) delete config.sessionIndex[sourceUid];
}
const mappingKey = autoCopyRuleKey(id, sourceUid);
if (config.copies[mappingKey] && String(config.copies[mappingKey].targetId || '') === sessionIdValue) delete config.copies[mappingKey];
if (before === lineage.members.length) return false;
writeMeta(dataDir, meta);
return true;
}
function moveAutoCopySession(dataDir, fromUid, toUid, sessionId) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const from = String(fromUid || '').trim();
const to = String(toUid || '').trim();
const id = String(sessionId || '').trim();
if (!from || !to || !id || from === to) return false;
const lineageId = config.sessionIndex[from] && config.sessionIndex[from][id];
if (!lineageId || !config.sessions[lineageId]) return false;
if (config.sessionIndex[from]) delete config.sessionIndex[from][id];
if (!config.sessionIndex[to]) config.sessionIndex[to] = {};
config.sessionIndex[to][id] = lineageId;
const lineage = config.sessions[lineageId];
lineage.members = (lineage.members || []).filter((member) => !(member && member.uid === from && member.id === id));
addLineageMember(lineage, to, id);
writeMeta(dataDir, meta);
return true;
}
function removeAutoCopySession(dataDir, uid, sessionId) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const sourceUid = String(uid || '').trim();
const id = String(sessionId || '').trim();
const lineageId = config.sessionIndex[sourceUid] && config.sessionIndex[sourceUid][id];
if (!lineageId) return false;
delete config.sessionIndex[sourceUid][id];
const lineage = config.sessions[lineageId];
if (lineage) {
lineage.members = (lineage.members || []).filter((member) => !(member && member.uid === sourceUid && member.id === id));
if (!lineage.members.length) {
delete config.sessions[lineageId];
for (const key of Object.keys(config.copies)) {
try {
const parts = JSON.parse(key);
if (Array.isArray(parts) && parts[0] === lineageId) delete config.copies[key];
} catch (_) {
// Ignore malformed legacy mapping keys; they cannot match a valid lineage.
}
}
}
}
writeMeta(dataDir, meta);
return true;
}
// #511: a local deletion is a destination exclusion, never permission to erase
// surviving siblings. Keep provenance so an all/workspace sync cannot recreate
// the deleted copy. Explicit manual copy can still restore it.
function recordSessionDeletion(dataDir, rows, options = {}) {
const families = rows.map(row => {
const lineageIds = new Set();
collectLineageMembersForDelete(dataDir, [row.id], lineageIds);
return { row, lineageIds };
});
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
for (const { row, lineageIds } of families) {
const uid = String(row.user_id || '').trim();
const id = String(row.id || '').trim();
if (!uid || !id) continue;
for (const lineageId of lineageIds) {
const lineage = config.sessions[lineageId];
if (!lineage) continue;
lineage.excludedUids = Array.from(new Set([...(Array.isArray(lineage.excludedUids) ? lineage.excludedUids : []), uid]));
}
if (options.preserveMembers) continue;
for (const lineage of Object.values(config.sessions)) {
lineage.members = (lineage.members || []).filter(member => !(member.uid === uid && member.id === id));
}
if (config.sessionIndex[uid]) delete config.sessionIndex[uid][id];
for (const [key, mapping] of Object.entries(config.copies)) {
if (mapping && String(mapping.targetId || '') === id) delete config.copies[key];
}
}
if (!options.preserveMembers) config.duplicates = collectAutoCopyDuplicates(config);
writeMeta(dataDir, meta);
return rows.length;
}
function isAutoCopyTargetExcluded(dataDir, lineageId, uid) {
const lineage = readAutoCopyConfig(dataDir).sessions[String(lineageId || '')];
return !!(lineage && Array.isArray(lineage.excludedUids) && lineage.excludedUids.includes(String(uid || '').trim()));
}
// 仅供显式「所有账号」删除及来源关系检查使用;普通删除不展开。
// 按 lineage 扩展到全部已关联物理副本;无 lineage 的会话映射回自身。
// 返回 [{ uid, id, lineageId }];uid 可能为空(脏索引),id 必有值。
// Copy guards consult provenance repeatedly. Rebuild its graph only after the
// meta-file fingerprint changes, rather than scanning every account per guard.
const lineageDeleteIndexes = new WeakMap();
function collectLineageMembersForDelete(dataDir, sessionIds, relatedLineageIds) {
const config = readAutoCopyConfig(dataDir);
const ids = new Set(
(Array.isArray(sessionIds) ? sessionIds : [])
.map((s) => String(s || '').trim())
.filter(Boolean)
);
if (!ids.size) return [];
// 成员表、反向索引及历史复制映射共同证明同源关系。只查成员表会漏掉
// 旧版复制记录,以及 normalizeAutoCopyLineages 拆出的重复物理会话。
// 不使用标题、工作目录或消息相似度推断,避免删除独立会话。
let indexes = lineageDeleteIndexes.get(config);
if (!indexes) {
const membersByLineage = new Map();
const lineagesBySession = new Map();
const lineagesByOrigin = new Map();
const addLink = (lineageId, memberUid, memberId) => {
if (!config.sessions[lineageId]) return;
const uid = String(memberUid || '').trim();
const id = String(memberId || '').trim();
if (!id) return;
if (!membersByLineage.has(lineageId)) membersByLineage.set(lineageId, new Map());
membersByLineage.get(lineageId).set(id, { uid, id, lineageId });
if (!lineagesBySession.has(id)) lineagesBySession.set(id, new Set());
lineagesBySession.get(id).add(lineageId);
};
for (const [lineageId, lineage] of Object.entries(config.sessions)) {
if (!lineage) continue;
const origin = String(lineage.originLineageId || lineageId);
if (!lineagesByOrigin.has(origin)) lineagesByOrigin.set(origin, []);
lineagesByOrigin.get(origin).push(lineageId);
for (const member of (Array.isArray(lineage.members) ? lineage.members : [])) {
addLink(lineageId, member && member.uid, member && member.id);
}
}
for (const [uid, index] of Object.entries(config.sessionIndex)) {
for (const [id, lineageId] of Object.entries(index || {})) addLink(String(lineageId || ''), uid, id);
}
for (const [key, mapping] of Object.entries(config.copies)) {
try {
const parts = JSON.parse(key);
if (Array.isArray(parts) && parts.length === 2) {
addLink(String(parts[0] || ''), parts[1], mapping && mapping.targetId);
}
} catch (_) { /* Malformed legacy keys cannot establish a copy relationship. */ }
}
indexes = { membersByLineage, lineagesBySession, lineagesByOrigin };
lineageDeleteIndexes.set(config, indexes);
}
const { membersByLineage, lineagesBySession, lineagesByOrigin } = indexes;
const members = [];
const seenIds = new Set();
const seenLineages = new Set();
for (const id of ids) {
const pending = Array.from(lineagesBySession.get(id) || []);
if (!pending.length && !seenIds.has(id)) {
seenIds.add(id);
members.push({ uid: '', id, lineageId: '' });
}
for (let i = 0; i < pending.length; i++) {
const lineageId = pending[i];
if (seenLineages.has(lineageId)) continue;
seenLineages.add(lineageId);
if (relatedLineageIds) relatedLineageIds.add(lineageId);
const lineage = config.sessions[lineageId];
const origin = String(lineage.originLineageId || lineageId);
for (const related of lineagesByOrigin.get(origin) || []) {
if (!seenLineages.has(related)) pending.push(related);
}
for (const member of (membersByLineage.get(lineageId) || new Map()).values()) {
if (!seenIds.has(member.id)) {
seenIds.add(member.id);
members.push(member);
}
for (const related of lineagesBySession.get(member.id) || []) {
if (!seenLineages.has(related)) pending.push(related);
}
}
}
}
return members;
}
function removeAutoCopyAccount(dataDir, uid) {
const sourceUid = String(uid || '').trim();
if (!sourceUid) return 0;
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const index = config.sessionIndex[sourceUid] || {};
const entries = Object.keys(index).map((sessionId) => ({ sessionId, lineageId: index[sessionId] }));
let removed = 0;
for (const entry of entries) {
delete index[entry.sessionId];
const lineage = config.sessions[entry.lineageId];
if (!lineage) continue;
lineage.members = (lineage.members || []).filter((member) => !(member && member.uid === sourceUid && member.id === entry.sessionId));
if (!lineage.members.length) {
delete config.sessions[entry.lineageId];
for (const key of Object.keys(config.copies)) {
try {
const parts = JSON.parse(key);
if (Array.isArray(parts) && parts[0] === entry.lineageId) delete config.copies[key];
} catch (_) {}
}
}
removed++;
}
if (entries.length) {
delete config.sessionIndex[sourceUid];
writeMeta(dataDir, meta);
}
return removed;
}
function resolveMappingLineage(config, lineageOrUid, maybeSessionId) {
if (maybeSessionId === undefined) return String(lineageOrUid || '');
const sourceUid = String(lineageOrUid || '').trim();
const sessionId = String(maybeSessionId || '').trim();
return config.sessionIndex[sourceUid] && config.sessionIndex[sourceUid][sessionId]
? config.sessionIndex[sourceUid][sessionId]
: '';
}
// The optional legacy sessionId argument keeps 1.0.15 local callers compatible
// while all persisted keys use lineageId + targetUid.
function getAutoCopyMapping(dataDir, lineageOrUid, targetUid, maybeSessionId) {
const config = readAutoCopyConfig(dataDir);
const lineageId = resolveMappingLineage(config, lineageOrUid, maybeSessionId);
return config.copies[autoCopyRuleKey(lineageId, targetUid)] || null;
}
// Load the mappings for one target account from the memoized auto-copy config
// in one pass. Automatic switching can have hundreds of selected sessions;
// callers should not reparse the same metadata JSON once per session.
function getAutoCopyMappings(dataDir, lineageIds, targetUid) {
const config = readAutoCopyConfig(dataDir);
const result = new Map();
for (const lineageId of new Set(Array.isArray(lineageIds) ? lineageIds : [])) {
const mapping = config.copies[autoCopyRuleKey(lineageId, targetUid)];
if (mapping) result.set(String(lineageId || ''), mapping);
}
return result;
}
function autoCopyTargetRowRevision(row) {
return JSON.stringify([
String(row && row.id || ''), String(row && row.user_id || ''),
Number(row && row.updated_at || 0), Number(row && row.last_activity_at || 0),
String(row && row.status || ''), String(row && row.title || ''), String(row && row.custom_title || ''),
]);
}
function autoCopyTargetStateRevision(row) {
return JSON.stringify([
Number(row && row.updated_at || 0), Number(row && row.last_activity_at || 0),
String(row && row.status || ''), String(row && row.title || ''), String(row && row.custom_title || ''),
]);
}
// Older daemons persisted the source row's timestamps as the target baseline.
// Repair those cheap row-level baselines in one metadata write; file snapshots
// remain the authority for mappings that are actually marked dirty.
function migrateAutoCopyTargetRevisions(dataDir, rowsByKey) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const changed = [];
for (const [key, mapping] of Object.entries(config.copies || {})) {
if (!mapping || typeof mapping !== 'object' || !mapping.targetId) continue;
let parts;
try { parts = JSON.parse(key); } catch (_) { continue; }
if (!Array.isArray(parts) || parts.length !== 2) continue;
const rowKey = JSON.stringify([String(mapping.targetId), String(parts[1] || '')]);
const row = rowsByKey && typeof rowsByKey.get === 'function' ? rowsByKey.get(rowKey) : null;
if (!row) continue;
const targetRevision = autoCopyTargetRowRevision(row);
const targetStateRevision = autoCopyTargetStateRevision(row);
if (mapping.targetRevision === targetRevision && mapping.targetStateRevision === targetStateRevision) continue;
mapping.targetRevision = targetRevision;
mapping.targetStateRevision = targetStateRevision;
changed.push(key);
}
if (changed.length) writeMeta(dataDir, meta);
return changed.length;
}
function setAutoCopyMapping(dataDir, lineageOrUid, targetUid, mappingOrSessionId, maybeMapping) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const legacyCall = arguments.length >= 5;
const lineageId = resolveMappingLineage(config, lineageOrUid, legacyCall ? mappingOrSessionId : undefined);
const mapping = legacyCall ? maybeMapping : mappingOrSessionId;
const key = autoCopyRuleKey(lineageId, targetUid);
config.copies[key] = Object.assign({}, mapping, { updatedAt: Date.now() });
writeMeta(dataDir, meta);
return config.copies[key];
}
function deleteAutoCopyMapping(dataDir, lineageOrUid, targetUid, maybeSessionId) {
const meta = readMeta(dataDir);
const config = ensureAutoCopyMeta(meta);
const lineageId = resolveMappingLineage(config, lineageOrUid, maybeSessionId);
delete config.copies[autoCopyRuleKey(lineageId, targetUid)];
writeMeta(dataDir, meta);
}
function logFile(dataDir) {
return path.join(dataDir, 'daemon.log');
}
function migrateAccountIdentities(dataDir) {
const dir = accountsDir(dataDir);
let names;
try { names = fs.readdirSync(dir).filter(name => name.endsWith('.info')); } catch (_) { return; }
let meta = null;
for (const name of names) {
const file = path.join(dir, name);
const info = parseAuthFile(file, { strict: false });
if (!info || name !== info.uid + '.info' || info.accountId === info.uid) continue;
const target = path.join(dir, info.accountId + '.info');
// Keep a pre-existing scoped backup and preserve the old bytes for recovery.
if (fs.existsSync(target)) fs.renameSync(file, file + '.legacy.' + crypto.randomUUID());
else fs.renameSync(file, target);
if (!meta) meta = readMeta(dataDir);
rememberAccountIdentity(meta, info.uid, info.accountId);
meta.accounts[info.accountId] = Object.assign({}, meta.accounts[info.uid], meta.accounts[info.accountId], {
uid: info.accountId, rawUid: info.uid, type: info.type, enterpriseId: info.enterpriseId,
});
delete meta.accounts[info.uid];
// Historical uid-only session links have no reliable login-context owner.
// Retain a recovery snapshot, then retire just this owner's links so a later
// personal login cannot silently inherit the enterprise's synchronization.
if (meta.autoCopy) {
if (!meta.retiredAccountSyncRules) meta.retiredAccountSyncRules = {};
if (!meta.retiredAccountSyncRules[info.accountId]) meta.retiredAccountSyncRules[info.accountId] = JSON.parse(JSON.stringify(meta.autoCopy));
const config = ensureAutoCopyMeta(meta);
delete config.sessionIndex[info.uid];
for (const lineage of Object.values(config.sessions)) {
lineage.members = (lineage.members || []).filter(member => member.uid !== info.uid);
}
for (const key of Object.keys(config.copies)) {
try { if (JSON.parse(key)[1] === info.uid) delete config.copies[key]; } catch (_) {}
}
}
}
if (meta) writeMeta(dataDir, meta);
}
function backupPath(dataDir, uid) {
return path.join(accountsDir(dataDir), `${uid}.info`);
}
/** WorkBuddy ignores auth files while this marker exists; retire it after a switch. */
function retireLogoutMarker(log = () => {}, file = AUTH_FILE) {
const marker = file ? `${file}.logged-out` : LOGOUT_MARKER;
if (!marker || !fs.existsSync(marker)) return false;
try {
const retired = `${marker}.retired.${process.pid}.${Date.now()}`;
fs.renameSync(marker, retired);
try {
fs.unlinkSync(retired);
} catch (_) {
// A leftover retired marker is harmless and keeps the operation recoverable.
}
log('[switch] 已清理 WorkBuddy 登录退出标记');
return true;
} catch (e) {
// 只有 macOS 能用 osascript 委托 GUI 会话清理;Windows 目录本就可写,
// Linux 无 osascript,两者都如实报错而不是走注定失败的回退。
if (!IS_MAC) {
throw new Error(`清理登录退出标记失败(${e.code || ''}): ${(e.message || e).toString().slice(0, 200)}`);
}
// WorkBuddy may launch the daemon in a sandbox that cannot unlink auth files.
try {
const { execFileSync } = require('child_process');
const markerQ = marker.replace(/"/g, '\\"');
execFileSync('osascript', ['-e', `do shell script "rm -f \\\"${markerQ}\\\""`], {
timeout: 15000,
stdio: 'pipe',
});
if (fs.existsSync(LOGOUT_MARKER)) throw new Error('标记仍然存在');
log('[switch] 已通过系统授权清理 WorkBuddy 登录退出标记');
return true;
} catch (e2) {
throw new Error(`清理登录退出标记失败: ${(e2.message || e2).toString().slice(0, 200)}`);
}
}
}
/**
* 兼容旧版账号备份:把 HelloBuddy/accounts 中尚未存在于 WorkDaddy 的账号复制过来。
* 只对平台默认 WorkDaddy 目录执行,显式自定义数据目录不做隐式迁移。
* 源目录和文件均保留,重复调用幂等。
*/
function migrateLegacyDataDir(dataDir, log = () => {}) {
// 旧版 HelloBuddy 目录仅存在于 macOS;其他平台直接跳过(LEGACY_DATA_DIR 为 null)
if (!LEGACY_DATA_DIR || !samePath(dataDir, PLATFORM_DATA_DIR)) {
return { migrated: 0, skipped: 0, source: null, target: dataDir };
}
const sourceAccounts = accountsDir(LEGACY_DATA_DIR);
if (!fs.existsSync(sourceAccounts)) {
return { migrated: 0, skipped: 0, source: LEGACY_DATA_DIR, target: dataDir };
}
let names;
try {
names = fs
.readdirSync(sourceAccounts)
.filter((name) => name.endsWith('.info') && !name.endsWith('.tmp'));
} catch (_) {
return { migrated: 0, skipped: 0, source: LEGACY_DATA_DIR, target: dataDir };
}
const targetAccounts = accountsDir(dataDir);
fs.mkdirSync(targetAccounts, { recursive: true, mode: 0o700 });
let migrated = 0;
let skipped = 0;
for (const name of names) {
const source = path.join(sourceAccounts, name);
const target = path.join(targetAccounts, name);
if (fs.existsSync(target)) {
skipped += 1;
continue;
}
try {
fs.copyFileSync(source, target);
fs.chmodSync(target, 0o600);
migrated += 1;
} catch (e) {
log(`[migration] 迁移账号 ${name} 失败: ${e.message}`);
}
}
if (migrated) {
log(`[migration] 已从 ${LEGACY_DATA_DIR}/accounts 迁移 ${migrated} 个账号到 ${dataDir}/accounts`);
}
return { migrated, skipped, source: LEGACY_DATA_DIR, target: dataDir };
}
function ensureDirs(dataDir, log = () => {}) {
migrateLegacyDataDir(dataDir, log);
fs.mkdirSync(accountsDir(dataDir), { recursive: true, mode: 0o700 });
try {
fs.chmodSync(dataDir, 0o700);
} catch (_) {
/* 已存在时可能失败,忽略 */
}
}
/** 读取登录信息文件并抽取账号关键字段(不返回令牌内容) */
function readAuthFile(file = currentAuthFile()) {
if (!ACTIVE_PROFILE.authFile && !process.env.WBSWITCH_AUTH_FILE) {
throw new Error(`${ACTIVE_PROFILE.name} 没有可读取的明文认证文件`);
}
if (!file) throw new Error('未找到唯一的当前登录信息文件');
const info = parseAuthFile(file, { strict: DYNAMIC_AUTH_DISCOVERY });
if (!info) throw new Error(`auth 文件无效或不属于当前客户端: ${path.basename(file)}`);
return info;
}
/** 更新 meta.json(uid -> nickname/uin/phone/时间)。preserveBinding:备份扫描路径
* 不漂移「账号 -> 登录文件」绑定——auth 目录里的个性化历史存档(带时间戳)即使
* 残留 lastLogin 标记,也不得覆盖切换路径建立的绑定关系。 */
function knownAccountIdentities(dataDir) { return readMeta(dataDir).accountIdentities || {}; }
function rememberAccountIdentity(meta, rawUid, uid) {
if (!meta.accountIdentities || typeof meta.accountIdentities !== 'object') meta.accountIdentities = {};
if (!Array.isArray(meta.accountIdentities[rawUid])) meta.accountIdentities[rawUid] = [];
if (!meta.accountIdentities[rawUid].includes(uid)) meta.accountIdentities[rawUid].push(uid);
}
function updateMeta(dataDir, info, { preserveBinding = false } = {}) {
const meta = readMeta(dataDir);
const now = Date.now();
const id = info.accountId || accountIdentity(info);
rememberAccountIdentity(meta, info.rawUid || info.uid, id);
const prev = meta.accounts[id] || {};
let authFileName = info.authFileName || prev.authFileName || '';
if (preserveBinding && prev.authFileName && info.authFileName && prev.authFileName !== info.authFileName) {
authFileName = prev.authFileName;
}
meta.accounts[id] = {
uid: id,
rawUid: info.uid,
type: info.type || '',
enterpriseId: info.enterpriseId || '',
nickname: info.nickname || prev.nickname || '',
uin: info.uin || prev.uin || '',
phone: info.phone || prev.phone || '',
authFileName,
authDomain: info.authDomain || prev.authDomain || '',
authIssuer: info.authIssuer || prev.authIssuer || '',
sort: Number.isSafeInteger(prev.sort) && prev.sort > 0 ? prev.sort : 0,
note: typeof prev.note === 'string' ? prev.note : '',
firstSeen: prev.firstSeen || now,
lastSeen: now,
};
writeMeta(dataDir, meta);
return meta;
}
function backupAuthFile(dataDir, file, log = () => {}) {
migrateAccountIdentities(dataDir);
const info = readAuthFile(file);
const dest = backupPath(dataDir, info.accountId);
const tmp = dest + '.tmp';
// [wd-compat] 密文信封原样落盘;需要查询/刷新时才在内存中解密。
// 这样不会在 WorkDaddy 目录制造明文 token 副本,也能保留旧版明文 auth 文件。
fs.copyFileSync(file, tmp);
fs.chmodSync(tmp, 0o600);
fs.renameSync(tmp, dest);
fs.chmodSync(dest, 0o600);
updateMeta(dataDir, info, { preserveBinding: true });
log(`[sync] 已备份账号 ${info.nickname || info.uid} (${info.uid}) -> ${dest}`);
return info;
}
/** 把活动登录信息备份到 accounts/<uid>.info(原子写入,0600)。
* 已有同名备份的账号只接受「官方权威登录位」(固定文件/当前登录位)作为更新源:
* auth 目录里的个性化历史存档(同 uid、老 token、残留 lastLogin 标记)不允许
* 覆盖有效备份——否则切换会写入早已失效的旧 refresh token,导致官方身份过期
* (真实事故:s 账号备份曾被 2026-08-21 存档覆盖成 8-19 的 token)。 */
function backupCurrent(dataDir, log = () => {}) {
if (!ACTIVE_PROFILE.capabilities.accounts) throw new Error(`${ACTIVE_PROFILE.name} 暂不支持账号文件备份`);
ensureDirs(dataDir, log);
migrateAccountIdentities(dataDir);
const records = listAuthRecords();
if (!records.length) throw new Error('未找到有效的登录信息文件');
const current = resolveCurrentAuth();
let result = null;
let backedUp = 0;
for (const record of records) {
const authoritative = current.file && samePath(record.file, current.file);
const existingBackup = fs.existsSync(backupPath(dataDir, record.accountId));
if (existingBackup && !authoritative) continue; // 历史存档不覆盖已有备份
const info = backupAuthFile(dataDir, record.file, log);
backedUp += 1;
if (!result || authoritative) result = info;
}
return Object.assign(result || {}, { backedUp, ambiguous: !!current.ambiguous });
}
function resolveAuthTarget(dataDir, uid, authJson) {
if (!DYNAMIC_AUTH_DISCOVERY) return AUTH_FILE;
const meta = readMeta(dataDir);
const record = meta.accounts[String(uid)] || {};
const backup = authRecordFromJson(null, authJson);
if (!backup) throw new Error('备份文件认证数据无效,拒绝切换');
const sameChannel = (candidate) => {
const expected = new Set([backup.authIssuer, backup.authDomain].filter(Boolean));
return [candidate && candidate.authIssuer, candidate && candidate.authDomain]
.some((origin) => origin && expected.has(origin));
};
// 官方固定登录位(workbuddy-desktop.info / workbuddy-desktop-ai.info)是官方实际
// 读取的当前登录文件。它存在时,所有显式切换一律写这里——个性化历史存档(带时间戳
// 的 info)只是官方多账号机制的存档,写了官方也不读。切换后 updateMeta 会同步修正
// 「账号 -> 登录文件」绑定,历史上被备份扫描漂移到旧文件的记录在此自愈。
if (AUTH_FILE && parseAuthFile(AUTH_FILE)) return AUTH_FILE;
const targetName = safeAuthFileName(record.authFileName) ? record.authFileName : '';
if (targetName) {
const target = path.join(authDir(), targetName);
if (path.dirname(path.resolve(target)) !== path.resolve(authDir())) throw new Error('登录文件目标路径无效');
const existing = fs.existsSync(target) ? parseAuthFile(target) : null;
if (fs.existsSync(target) && !existing) throw new Error('登录文件目标不是当前客户端的有效认证文件,拒绝覆盖');
if (existing && !sameChannel(existing)) throw new Error('登录文件目标属于其他认证通道,拒绝覆盖');
return target;
}
const records = listAuthRecords();
const matching = records.filter((item) => item.accountId === String(uid));
if (matching.length === 1) return matching[0].file;
const channelMatches = records.filter(sameChannel);
const canonical = channelMatches.find((item) => AUTH_FILE && samePath(item.file, AUTH_FILE));
if (canonical) return canonical.file;
if (channelMatches.length === 1) return channelMatches[0].file;
const legacyRecord = String(record.accountId || record.uid || '') === String(uid) &&
!record.authFileName && !record.authDomain && !record.authIssuer;
// legacy 账号(动态发现上线前备份)没有属于自己的文件记录,其唯一正确的落点就是
// 官方固定登录文件(AUTH_FILE);无论该文件当前是否被占用,用户显式切换即意图覆盖。
if (legacyRecord && AUTH_FILE) return AUTH_FILE;
throw new Error('账号缺少已确认的登录文件名,拒绝猜测写入目标');
}
/** 账号展示顺序保存在 profile 元数据中,不改写登录备份格式。0 表示未排序,排在末尾。 */
function getAccountOrder(dataDir) {
return { mode: readMeta(dataDir).accountOrderMode === 'fixed' ? 'fixed' : 'expiry' };
}
function setAccountOrder(dataDir, value) {
if (!value || !['expiry', 'fixed'].includes(value.mode) || !Array.isArray(value.uids) ||
value.uids.length > 10000 || value.uids.some(uid => typeof uid !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(uid) || ['__proto__', 'prototype', 'constructor'].includes(uid)) ||
new Set(value.uids).size !== value.uids.length) throw new Error('无效的账号排序设置');
const current = new Set(listAccounts(dataDir).map(account => account.uid));
const meta = readMeta(dataDir);
for (const account of Object.values(meta.accounts)) {
if (account && typeof account === 'object') delete account.sort;
}
let sort = 0;
for (const uid of value.uids) {
if (!current.has(uid)) continue; // 弹窗打开后删除的账号不能复活。
meta.accounts[uid] = Object.assign({}, meta.accounts[uid], { sort: ++sort });
}
meta.accountOrderMode = value.mode;
writeMeta(dataDir, meta);
return getAccountOrder(dataDir);
}
/** 备注仅写入当前 profile 的元数据,保留认证备份和其他账号设置。 */
function setAccountNote(dataDir, value) {
if (!value || typeof value.uid !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(value.uid) ||
['__proto__', 'prototype', 'constructor'].includes(value.uid)) throw new Error('无效的账号');
if (typeof value.note !== 'string' || value.note.length > 2000) throw new Error('备注不能超过 2000 个字符');
if (!fs.existsSync(backupPath(dataDir, value.uid))) throw new Error('账号不存在或已删除');
const meta = readMeta(dataDir);
meta.accounts[value.uid] = Object.assign({}, meta.accounts[value.uid], { note: value.note });
writeMeta(dataDir, meta);
return { uid: value.uid, note: value.note };
}
/** 列出所有已备份账号(直接读备份文件提取展示字段,按最近刷新时间倒序) */
function listAccounts(dataDir) {
if (!ACTIVE_PROFILE.capabilities.accounts) return [];
migrateLegacyDataDir(dataDir);
migrateAccountIdentities(dataDir);
const dir = accountsDir(dataDir);
let names = [];
try {
names = fs
.readdirSync(dir)
.filter((f) => f.endsWith('.info') && !f.endsWith('.tmp'));
} catch (_) {
/* 目录不存在 */
}
const orderMeta = readMeta(dataDir);
const list = names.map((n) => {
const uid = n.replace(/\.info$/, '');
const savedSort = orderMeta.accounts[uid] && orderMeta.accounts[uid].sort;
const item = {
uid,
sort: Number.isSafeInteger(savedSort) && savedSort > 0 ? savedSort : 0,
note: typeof (orderMeta.accounts[uid] || {}).note === 'string' ? orderMeta.accounts[uid].note : '',
nickname: '',
phone: '',
uin: '',
tokenExpiresAt: null,
refreshExpiresAt: null,
lastRefreshTime: null,
lastSeen: null,
authValid: false,
};
try {
const j = wdCompatDecryptAuthJson(JSON.parse(fs.readFileSync(path.join(dir, n), 'utf8'))); // [wd-compat]
item.authValid = !!parseAuthJson(j);
const acct = j.account || (Array.isArray(j.accounts) && j.accounts[0]);
if (acct) {
item.nickname = wdCompatText(acct.nickname);
item.phone = wdCompatText(acct.phoneNumber);
item.uin = typeof acct.uin === 'string' || typeof acct.uin === 'number' ? acct.uin : '';
item.rawUid = String(acct.uid || uid);
item.enterpriseId = String(acct.enterpriseId || '').trim();
item.type = typeof acct.type === 'string' ? acct.type : '';
item.enterpriseName = typeof acct.enterpriseName === 'string' ? acct.enterpriseName.trim() : '';
}
if (j.auth) {
item.tokenExpiresAt = j.auth.expiresAt || null;
item.refreshExpiresAt = j.auth.refreshExpiresAt || null;
item.lastRefreshTime = j.auth.lastRefreshTime || null;
const tokenPhone = wdCompatTokenPhone(wdCompatAuthToken(j.auth));
if (tokenPhone) item.phone = tokenPhone;
}
} catch (_) {
/* 文件损坏则显示空字段 */
}
return item;
});
return list.sort(
(a, b) => (b.lastRefreshTime || 0) - (a.lastRefreshTime || 0)
);
}
/** 删除 auth 目录中属于该 uid 的全部登录文件(官方固定文件 + 带时间戳的历史存档)。
* 只删 listAuthRecords 能发现(parseAuthFile 可解析并匹配)的记录,确保之后的
* backupCurrent 扫描不会再把这个账号重新备份回来——这是「删除账号重启后又出现」
* 的根因:删除只清了 backups 目录,auth 目录里残留的存档会在下一次扫描时复活账号。 */
function deleteAuthFilesForUid(uid, log = () => {}) {
if (!AUTH_FILE) return { removed: 0 };
if (!DYNAMIC_AUTH_DISCOVERY) {
const record = parseAuthFile(AUTH_FILE, { strict: false });
if (record && record.accountId === uid && fs.existsSync(AUTH_FILE)) {
fs.unlinkSync(AUTH_FILE);
log(`[delete] 已删除固定登录文件 ${path.basename(AUTH_FILE)}`);
return { removed: 1 };
}
return { removed: 0 };
}
const dir = authDir();
let names;
try { names = fs.readdirSync(dir); } catch (_) { return { removed: 0 }; }
let removed = 0;
for (const name of names) {
if (!safeAuthFileName(name)) continue;
const file = path.join(dir, name);
const record = parseAuthFile(file);
if (record && record.accountId === uid && fs.existsSync(file)) {
try {
fs.unlinkSync(file);
removed += 1;
} catch (e) {
log(`[delete] 删除认证存档 ${name} 失败: ${e.message}`);
}
}
}
return { removed };
}
/** 永久删除某个账号的备份文件(不影响当前登录) */
function deleteAccount(dataDir, uid, log = () => {}) {
if (!ACTIVE_PROFILE.capabilities.accounts) throw new Error(`${ACTIVE_PROFILE.name} 暂不支持账号切换`);
// 防御:面板已对当前登录账号隐藏删除按钮;走到这里说明状态异常,
// 拒绝直接删官方正在读取的登录位(删了 WorkBuddy 也会重新写回,删不干净)。
const current = resolveCurrentAuth();
if (current.file && !current.ambiguous) {
const record = parseAuthFile(current.file, { strict: false });
if (record && record.accountId === uid) {
throw new Error('不能删除当前登录的账号(请先退出登录或切换到其他账号)');
}
}
migrateLegacyDataDir(dataDir);
// 关键:先清掉 auth 目录里该 uid 的全部登录文件(固定文件 + 历史存档),
// 否则 backupCurrent 的下一次扫描会把账号重新备份回来(删除后复活的根因)。
const authResult = deleteAuthFilesForUid(uid, log);
const files = [backupPath(dataDir, uid)];
// 旧版 HelloBuddy 目录仍会在每次启动时迁移缺失的账号备份。删除新目录
// 的文件后若留下旧源文件,下一次 daemon 启动就会把账号重新复制回来。
if (LEGACY_DATA_DIR && samePath(dataDir, PLATFORM_DATA_DIR)) {
files.push(backupPath(LEGACY_DATA_DIR, uid));
}
let deletedFile = false;
for (const file of files) {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
deletedFile = true;
}
}
const mf = metaFile(dataDir);
try {
const meta = JSON.parse(fs.readFileSync(mf, 'utf8'));
if (meta.accounts && meta.accounts[uid]) {
delete meta.accounts[uid];
fs.writeFileSync(mf, JSON.stringify(meta, null, 2), { mode: 0o600 });
}
} catch (_) {
/* meta 不存在则忽略 */
}
return { deleted: deletedFile, uid, authFilesRemoved: authResult.removed };
}
/** 切换登录账号:把备份文件复制回登录信息文件(先校验 uid 匹配) */
function switchTo(dataDir, uid, log = () => {}) {
if (!ACTIVE_PROFILE.capabilities.accounts) throw new Error(`${ACTIVE_PROFILE.name} 暂不支持账号切换`);
migrateLegacyDataDir(dataDir, log);
migrateAccountIdentities(dataDir);
// 不再用「当前 auth 目录是否 ambiguous」一票否决:切换写入的目标文件由
// resolveAuthTarget 精确决定(meta.json 记录 / 唯一 uid 匹配),目标无法唯一
// 确定时它自己会拒绝,避免历史 lastLogin 残留导致已登录账号切不回去。
const src = backupPath(dataDir, uid);
if (!fs.existsSync(src)) {
throw new Error(`未找到账号 ${uid} 的备份文件`);
}
const raw = fs.readFileSync(src, 'utf8');
const json = wdCompatDecryptAuthJson(JSON.parse(raw)); // [wd-compat] 校验前解密;写回仍用 raw 原字节
const acct = json.account || (Array.isArray(json.accounts) && json.accounts[0]);
if (!acct || accountIdentity(acct) !== uid) {
throw new Error('备份文件校验失败:uid 不匹配,已中止切换');
}
const target = resolveAuthTarget(dataDir, uid, json);
if (!target) throw new Error('未找到该账号已记录的登录文件名,拒绝猜测写入目标');
const tmp = target + '.wbswitch.tmp';
try {
fs.writeFileSync(tmp, raw, { mode: 0o600 });
fs.renameSync(tmp, target);
fs.chmodSync(target, 0o600);
} catch (e) {
// 沙箱环境(如从 WorkBuddy 托管后台运行)直接写系统目录会 EPERM。
// macOS 回退:osascript 委托 GUI 会话复制(不涉及内容转义,只传路径)。
// Windows:目录在 %LOCALAPPDATA% 用户可写区;Linux:目录在 ~/.local/share 用户可写区。
// 后两者直写失败即如实报错,不走 osascript(该命令在 Linux 上不存在)。
if (!IS_MAC) {
throw new Error(
`写入登录文件失败(${e.code || ''}): ${(e.message || e).toString().slice(0, 200)}`
);
}
log(`[switch] 直写失败(${e.code}),改用 osascript 委托写入`);
const bridge = path.join(dataDir, '.auth-switch-bridge.tmp');
const authBridge = target + '.wbswitch.tmp';
const bridgeQ = bridge.replace(/"/g, '\\"');
const authQ = target.replace(/"/g, '\\"');
const tmpQ = authBridge.replace(/"/g, '\\"');
try {
// 1) 本进程写 bridge(数据目录可写)
fs.writeFileSync(bridge, raw, { mode: 0o600 });
// 2) osascript 委托:bridge -> auth 目录
const script = `do shell script "cp \\"${bridgeQ}\\" \\"${tmpQ}\\" && mv \\"${tmpQ}\\" \\"${authQ}\\" && chmod 600 \\"${authQ}\\" && rm -f \\"${bridgeQ}\\" && echo OK"`;
const { execFileSync } = require('child_process');
execFileSync('osascript', ['-e', script], { timeout: 15000, stdio: 'pipe' });
} catch (e2) {
try { fs.unlinkSync(bridge); } catch (_) {}
throw new Error(`写入登录文件失败: ${(e2.message || e2).toString().slice(0, 200)}`);
}
}
// Legacy call shape retained for source-compatible launchers: retireLogoutMarker(log);
retireLogoutMarker(log, target);
updateMeta(dataDir, {
uid: String(acct.uid),
type: acct.type,
enterpriseId: acct.enterpriseId,
nickname: wdCompatText(acct.nickname),
uin: typeof acct.uin === 'string' || typeof acct.uin === 'number' ? acct.uin : '',
phone: wdCompatText(acct.phoneNumber),
authFileName: path.basename(target),
authDomain: normalizeAuthDomain(json.auth && json.auth.domain),
authIssuer: tokenIssuerOrigin(json.auth && (json.auth.accessToken || json.auth.access_token)),
});
const switchName = wdCompatText(acct.nickname) || uid;
log(`[switch] 已切换登录账号为 ${switchName} (${uid})`);
return { uid, rawUid: String(acct.uid), type: acct.type, enterpriseId: acct.enterpriseId, nickname: wdCompatText(acct.nickname), uin: typeof acct.uin === 'string' || typeof acct.uin === 'number' ? acct.uin : '', authFile: target };
}
module.exports = {
isWbEncryptedEnvelope, // [wd-compat]
wdCompatExeCandidates, // [wd-compat]
wdCompatWindowsExeCandidates, // [wd-compat]
wdCompatContainsEncryptedFields, // [wd-compat]
wdCompatText, // [wd-compat]
wdCompatAuthToken, // [wd-compat]
wdCompatTokenPhone, // [wd-compat]
wdCompatHasAuthCredential, // [wd-compat]
normalizeAccountImportJson, // [wd-compat]
wdCompatDecryptAuthJson, // [wd-compat]
wdCompatEncryptAuthJson, // [wd-compat]
getAccountOrder,
setAccountOrder,
setAccountNote,
readModelsFile,
writeModelsFile,
writeModelBackup,
AUTH_FILE,
authDir,
safeAuthFileName,
normalizeAuthDomain,
tokenIssuerOrigin,
parseAuthJson,
parseAuthFile,
listAuthRecords,
resolveCurrentAuth,
resolveLogoutAuth,
currentAuthFile,
resolveAuthTarget,
ACTIVE_PROFILE,
defaultDataDir,
migrateLegacyDataDir,
accountsDir,
metaFile,
workbuddyModelsFile,
modelBackupsDir,
maskApiKey,
sanitizeModel,
checkinDisplayValue,
listOfficialModels,
readOfficialModel,
readModelBackup,
deleteOfficialModels,
listModelBackups,
backupOfficialModel,
copyModelBackup,
editModelBackup,
deleteModelBackups,
enableModelBackup,
importModels,
logFile,
backupPath,
migrateAccountIdentities,
knownAccountIdentities,
backupAuthFile,
retireLogoutMarker,
ensureDirs,
readAuthFile,
updateMeta,
canonicalWorkspace,
getAutoCopyRules,
dedupeAutoCopySessionRows,
setAutoCopyRule,
setAutoCopyAllSessions,
isAutoCopySessionSelected,
getAutoCopySession,
getAutoCopySessionMembers,
getAutoCopySessionMemberRecords,
selectLatestAutoCopyMember,
ensureAutoCopySessions,
ensureAutoCopySession,
normalizeAutoCopyLineages,
mergeAutoCopyLineages,
addAutoCopySessionMember,
removeAutoCopySessionMember,
moveAutoCopySession,
removeAutoCopySession,
removeAutoCopyAccount,
collectLineageMembersForDelete,
recordSessionDeletion,
isAutoCopyTargetExcluded,
getAutoCopyMapping,
getAutoCopyMappings,
migrateAutoCopyTargetRevisions,
setAutoCopyMapping,
deleteAutoCopyMapping,
backupCurrent,
listAccounts,
switchTo,
deleteAccount,
};