🦀 ClawHub
Multi User Privacy
by @lz84
Automatically identifies users, isolates memories, filters sensitive content, manages sessions and sub-agents, and enforces role-based privacy and quota cont...
TERMINAL
clawhub install multi-user-privacy📖 About This Skill
Multi-User Privacy 技能
> 版本:v0.9.0 - Gateway 自动创建子代理版 > 定位: ClawHub 首个多用户隐私保护技能 > 状态: ✅ 生产环境验证 > 更新: 2026-03-12 - 实现 Gateway 自动创建子代理功能
🌟 核心功能
1. 身份识别 ✅
2. 记忆隔离 ✅
3. 隐私检查 ✅
4. 项目权限 ✅
5. 异常检测 ✅
6. 实时告警 ✅
7. 配置热更新 ✅
8. 性能优化 ✅
9. Session 管理 ✅
10. 子代理自动创建 🆕 (v0.9.0)
11. 配额管理 🆕
🎯 隔离架构
完整隔离方案
┌─────────────────────────────────────────────────────────┐
│ 主代理 (路由器) │
├─────────────────────────────────────────────────────────┤
│ │
│ 1️⃣ 身份识别层 │
│ └── 读取 sender_id │
│ ├── 管理员 (ou_b96f5424607baf3a0455b55e0f4a2213)│
│ └── 普通用户 │
│ ↓ │
│ 2️⃣ Session 管理层 │
│ └── 查找或创建用户专属 session │
│ ├── session_laoliu │
│ ├── session_sun │
│ └── session_xie │
│ ↓ │
│ 3️⃣ 记忆隔离层 │
│ └── 加载对应记忆文件 │
│ ├── 管理员:MEMORY.md + 当日记忆 │
│ └── 普通用户:users/{user_id}.md + 当日记忆 │
│ ↓ │
│ 4️⃣ 隐私检查层 │
│ └── 回复前检查 │
│ ├── 敏感词检查 │
│ ├── 账号 ID 检查 │
│ └── 角色称呼检查 │
│ ↓ │
│ 5️⃣ 子代理路由层 │
│ └── 转发到对应子代理 │
│ ├── 子代理处理 │
│ └── 返回回复 │
│ │
└─────────────────────────────────────────────────────────┘
📋 技术实现
1. 身份识别
// privacy-guard.js
function getUserIdFromContext(context) {
const chatId = context.chat_id || context.user_id || context.sender_id;
if (!chatId) return null;
const match = chatId.match(/(ou_[a-z0-9]+)/);
return match ? match[1] : null;
}function isAdmin(userId) {
return userId === ADMIN_ID; // ou_b96f5424607baf3a0455b55e0f4a2213
}
2. 记忆隔离
// privacy-guard.js
function loadMemory(userId) {
const results = { loaded: [], missing: [] };
const today = new Date().toISOString().split('T')[0];
const USER_MEMORY_DIR = path.join(MEMORY_DIR, 'users');
// 管理员:加载完整记忆
if (isAdmin(userId)) {
if (fs.existsSync(MEMORY_PATH)) {
results.loaded.push(MEMORY_PATH);
}
} else {
// 普通用户:只能访问自己的记忆
const userMemoryPath = path.join(USER_MEMORY_DIR, ${userId}.md);
if (fs.existsSync(userMemoryPath)) {
results.loaded.push(userMemoryPath);
}
}
// 所有用户都可以访问当日记忆
const todayPath = path.join(MEMORY_DIR, ${today}.md);
if (fs.existsSync(todayPath)) {
results.loaded.push(todayPath);
}
return results;
}
3. 隐私检查
// privacy-guard.js
function enhancedCheck(message, userId) {
const issues = [];
// 如果不是管理员,检查敏感词
if (!isAdmin(userId)) {
const forbiddenTerms = ['老刘', '主人', '管理员', '主账号'];
for (const term of forbiddenTerms) {
if (message.includes(term)) {
issues.push({
type: 'forbidden_term',
value: term,
severity: 'high',
message: 检测到敏感词汇:${term}
});
}
}
// 检查是否暗示其他账号存在
const otherAccountPatterns = [
/其他账号/g,
/另一个用户/g,
/还有人/g,
/除了你之外/g
];
for (const pattern of otherAccountPatterns) {
if (pattern.test(message)) {
issues.push({
type: 'isolation_breach',
value: pattern.toString(),
severity: 'medium',
message: '暗示其他账号存在'
});
}
}
}
return {
passed: issues.length === 0,
issues,
blocked: issues.some(i => i.severity === 'high')
};
}
4. Session 管理
// session-guard.js
class SessionManager {
constructor() {
this.sessions = this.loadSessionDB();
}
async getSession(userId) {
// 检查是否已有 session
if (this.sessions[userId]) {
return this.sessions[userId];
}
// 创建新 session
const session = {
userId,
createdAt: new Date().toISOString(),
lastActiveAt: new Date().toISOString(),
messageCount: 0
};
this.sessions[userId] = session;
this.saveSessionDB();
return session;
}
checkSessionMatch(currentUserId, expectedUserId) {
return currentUserId === expectedUserId;
}
}
5. 子代理路由
// subagent-router.js
class SubAgentRouter {
constructor() {
this.userSessions = this.loadRouterDB();
}
async getUserSession(userId) {
// 检查是否已有子代理
if (this.userSessions[userId]) {
return this.userSessions[userId];
}
// 创建新子代理
const session = await sessions_spawn({
label: userId,
task: ${userId} 的专属子代理,
mode: 'session',
thread: true
});
this.userSessions[userId] = session;
this.saveRouterDB();
return session;
}
}
🚀 快速开始
安装
# 通过 ClawHub 安装
npx clawhub install multi-user-privacy或手动安装
git clone https://github.com/your-repo/multi-user-privacy.git
cp -r multi-user-privacy ~/.openclaw/skills/
配置
1. 创建隐私配置文件
cat > ~/.openclaw/workspace/.privacy-config.json << 'EOF'
{
"admin": {
"id": "ou_b96f5424607baf3a0455b55e0f4a2213",
"name": "管理员",
"role": "admin"
},
"privacy": {
"mode": "strict",
"forbiddenTerms": ["敏感词 1", "敏感词 2"]
}
}
EOF
2. 创建项目配置文件
cat > ~/.openclaw/workspace/.projects-config.json << 'EOF'
{
"projects": {
"your-project": {
"name": "你的项目",
"owner": "your_user_id",
"collaborators": ["collab_id_1", "collab_id_2"]
}
}
}
EOF
3. 启用自动注入
在 OpenClaw 主入口添加:
// ~/.openclaw/runtime/main.js 开头
require('./skills/multi-user-privacy/scripts/auto-inject');
📖 使用方式
查看 Session 统计
node scripts/session-guard.js stats
查看日志
node scripts/view-logs.js
查看子代理路由
node scripts/subagent-router.js list
清空 Session 数据库
node scripts/session-guard.js clear
📊 版本历史
v0.9.0 (2026-03-12) - Gateway 自动创建子代理版 🆕
v0.8.0 (2026-03-11) - 生产就绪版
v0.7.0 (2026-03-11) - 子代理自动路由
v0.6.0 (2026-03-11) - Session 会话管理
v0.5.0 (2026-03-11) - P2 改进完整版
📞 支持
📄 许可证
MIT License