feat: 工作空间切换校验 + 初始化检测 + 继承基础设置
新增功能: - workspace:check IPC — 校验路径有效性 + 检测 4 个必需文件状态 - workspace:inheritFiles IPC — 从旧工作空间继承 SOUL/AGENTS/USERS 到新工作空间 - app:restart IPC — 渲染进程触发应用重启(app.relaunch + app.exit) 前端 WorkspaceSettings 改造: - 选择文件夹后立即校验路径(是否存在、是否目录、4 个必需文件状态) - 显示工作空间状态: · 新工作空间 → 提示将自动创建 4 个文件 · 部分文件缺失 → 提示缺失文件将自动创建 · 已有工作空间 → 提示将直接加载现有配置 - 继承选项:当切换到新工作空间时,可勾选继承 SOUL/AGENTS/USERS.md · MEMORY.md 不继承(记忆与工作空间项目上下文绑定) · 继承白名单:仅允许 SOUL.md、AGENTS.md、USERS.md(防路径遍历) - 确认切换按钮 + 取消按钮 - 切换完成后弹出重启确认对话框(立即重启 / 稍后手动重启) 安全考虑: - workspace:inheritFiles 使用文件名白名单,防止路径遍历攻击 - workspace:check 返回 resolvedPath,前端只展示不直接用于文件操作 - app:restart 延迟 200ms 让 IPC 响应先返回
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
import { ipcMain, BrowserWindow, shell, app, dialog } from 'electron';
|
||||
import { join } from 'path';
|
||||
import type { SessionService } from '../services/session.service';
|
||||
import type { ConfigService } from '../services/config.service';
|
||||
import type { WorkspaceService } from '../services/workspace.service';
|
||||
@@ -551,6 +552,117 @@ export function registerAllIPCHandlers(
|
||||
return { canceled: false, path: result.filePaths[0] };
|
||||
});
|
||||
|
||||
// 重启应用(工作空间切换后调用)
|
||||
ipcMain.handle('app:restart', async () => {
|
||||
log.info('[APP] Restart requested');
|
||||
// 延迟 200ms 让 IPC 响应先返回
|
||||
setTimeout(() => {
|
||||
app.relaunch();
|
||||
app.exit(0);
|
||||
}, 200);
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// ===== 工作空间校验与继承 =====
|
||||
|
||||
// 校验工作空间路径:检测路径有效性 + 4 个必需文件状态
|
||||
ipcMain.handle('workspace:check', async (_event, targetPath: string) => {
|
||||
if (!targetPath || typeof targetPath !== 'string') {
|
||||
return { valid: false, reason: '路径不能为空' };
|
||||
}
|
||||
|
||||
const { existsSync, statSync } = await import('fs');
|
||||
const { resolve } = await import('path');
|
||||
|
||||
const resolvedPath = resolve(targetPath);
|
||||
|
||||
// 校验 1: 路径是否存在
|
||||
if (!existsSync(resolvedPath)) {
|
||||
return {
|
||||
valid: true,
|
||||
path: resolvedPath,
|
||||
exists: false,
|
||||
missingFiles: ['SOUL.md', 'AGENTS.md', 'MEMORY.md', 'USERS.md'],
|
||||
isNewWorkspace: true,
|
||||
reason: '目录不存在,将在切换后自动创建',
|
||||
};
|
||||
}
|
||||
|
||||
// 校验 2: 是否为目录
|
||||
try {
|
||||
const stat = statSync(resolvedPath);
|
||||
if (!stat.isDirectory()) {
|
||||
return { valid: false, reason: '路径不是目录' };
|
||||
}
|
||||
} catch {
|
||||
return { valid: false, reason: '无法访问路径' };
|
||||
}
|
||||
|
||||
// 校验 3: 检测 4 个必需文件状态
|
||||
const requiredFiles = ['SOUL.md', 'AGENTS.md', 'MEMORY.md', 'USERS.md'];
|
||||
const missingFiles: string[] = [];
|
||||
for (const f of requiredFiles) {
|
||||
if (!existsSync(join(resolvedPath, f))) missingFiles.push(f);
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
path: resolvedPath,
|
||||
exists: true,
|
||||
missingFiles,
|
||||
isNewWorkspace: missingFiles.length === requiredFiles.length,
|
||||
};
|
||||
});
|
||||
|
||||
// 从旧工作空间继承文件到新工作空间
|
||||
ipcMain.handle('workspace:inheritFiles', async (_event, params: {
|
||||
targetPath: string;
|
||||
sourcePath: string;
|
||||
files: string[];
|
||||
}) => {
|
||||
const { targetPath, sourcePath, files } = params;
|
||||
if (!targetPath || !sourcePath || !Array.isArray(files)) {
|
||||
return { success: false, error: '参数无效' };
|
||||
}
|
||||
|
||||
const { existsSync, mkdirSync, copyFileSync } = await import('fs');
|
||||
const { resolve } = await import('path');
|
||||
|
||||
const resolvedTarget = resolve(targetPath);
|
||||
const resolvedSource = resolve(sourcePath);
|
||||
|
||||
// 确保目标目录存在
|
||||
if (!existsSync(resolvedTarget)) {
|
||||
mkdirSync(resolvedTarget, { recursive: true });
|
||||
}
|
||||
|
||||
const inherited: string[] = [];
|
||||
const failed: Array<{ file: string; error: string }> = [];
|
||||
|
||||
for (const fileName of files) {
|
||||
// 仅允许继承白名单文件(防止路径遍历)
|
||||
if (!['SOUL.md', 'AGENTS.md', 'USERS.md'].includes(fileName)) {
|
||||
failed.push({ file: fileName, error: '不在继承白名单中' });
|
||||
continue;
|
||||
}
|
||||
const srcFile = join(resolvedSource, fileName);
|
||||
const dstFile = join(resolvedTarget, fileName);
|
||||
try {
|
||||
if (existsSync(srcFile)) {
|
||||
copyFileSync(srcFile, dstFile);
|
||||
inherited.push(fileName);
|
||||
log.info(`[WORKSPACE] Inherited ${fileName}: ${resolvedSource} → ${resolvedTarget}`);
|
||||
} else {
|
||||
failed.push({ file: fileName, error: '源文件不存在' });
|
||||
}
|
||||
} catch (err) {
|
||||
failed.push({ file: fileName, error: (err as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true, inherited, failed };
|
||||
});
|
||||
|
||||
// ===== 工具管理 =====
|
||||
|
||||
ipcMain.handle('tools:list', async () => {
|
||||
|
||||
Reference in New Issue
Block a user