OpenClaw工作空间完全指南:workspace隔离、多租户配置、团队权限管理、环境变量分离,让企业级AI Agent井井有条。">

🏢 OpenClaw 工作空间完全指南

世界上有一种秩序,叫做每个团队有自己的AI Agent、自己的知识库、自己的配置,互不打扰...

这篇文章教你用OpenClaw的工作空间功能,打造企业级的多团队AI协作平台。

什么是工作空间?

工作空间(Workspace)是OpenClaw的逻辑隔离单位,每个工作空间可以拥有:

基本配置

创建工作空间

# 创建新工作空间
openclaw workspace create --name product-team

# 查看所有工作空间
openclaw workspace list

# 输出:
# NAME              AGENT              STATUS
# default           default-agent      active
# product-team      product-agent      active
# support-team      support-agent      active

配置工作空间

// ~/.openclaw/workspaces/product-team.json
{
  "name": "product-team",
  "agent": {
    "model": "claude-3-5-sonnet",
    "provider": "anthropic",
    "temperature": 0.7,
    "maxTokens": 4096
  },
  "knowledgeBase": {
    "enabled": true,
    "index": "product-docs"
  },
  "tools": [
    "web_search",
    "web_fetch",
    "browser",
    "github"
  ],
  "channels": [
    "discord-product",
    "slack-product"
  ]
}

消息路由到工作空间

配置消息如何路由到不同的工作空间:

基于渠道路由

// openclaw.json
{
  "routing": {
    "workspaceRules": [
      {
        "match": { "channel": "discord-product" },
        "workspace": "product-team"
      },
      {
        "match": { "channel": "discord-support" },
        "workspace": "support-team"
      },
      {
        "match": { "channel": "discord-sales" },
        "workspace": "sales-team"
      }
    ]
  }
}

基于用户/群组路由

{
  "routing": {
    "workspaceRules": [
      {
        "match": { 
          "senderId": ["user-alice", "user-bob"],
          "workspace": "product-team"
        },
        {
        "match": { 
          "groupId": "premium-customers",
          "workspace": "vip-team"
        }
      }
    ]
  }
}

基于关键词路由

{
  "routing": {
    "workspaceRules": [
      {
        "match": { "message": "bug|error|crash" },
        "workspace": "support-team"
      },
      {
        "match": { "message": "price|quote|discount" },
        "workspace": "sales-team"
      }
    ]
  }
}

数据隔离

会话隔离

{
  "workspace": {
    "sessionIsolation": {
      "enabled": true,
      "mode": "per-workspace",  // 每个工作空间独立的会话池
      "maxSessionsPerWorkspace": 100
    }
  }
}

知识库隔离

{
  "knowledgeBase": {
    "isolation": "strict",  // strict | shared
    "workspaces": {
      "product-team": {
        "indexes": ["product-docs", "api-docs"],
        "embeddingModel": "text-embedding-3-small"
      },
      "support-team": {
        "indexes": ["kb-articles", "faq"],
        "embeddingModel": "text-embedding-3-small"
      }
    }
  }
}

工具隔离

{
  "workspace": {
    "tools": {
      "product-team": {
        "allowed": ["web_search", "web_fetch", "github", "custom-product-tools"],
        "blocked": ["file_write", "exec"]
      },
      "support-team": {
        "allowed": ["web_search", "knowledge_base", "ticket_system"],
        "blocked": ["github", "file_write", "exec"]
      }
    }
  }
}

环境变量隔离

每个工作空间可以有不同的环境变量,特别适合API Key管理:

// ~/.openclaw/workspaces/product-team/env.json
{
  "ANTHROPIC_API_KEY": "sk-ant-***-product",
  "GITHUB_TOKEN": "ghp_***-product",
  "CUSTOM_API": "https://api.product.internal"
}

// ~/.openclaw/workspaces/support-team/env.json
{
  "ANTHROPIC_API_KEY": "sk-ant-***-support", 
  "GITHUB_TOKEN": "ghp_***-support",
  "ZENDESK_API_KEY": "zd_***-support"
}
💡 环境变量使用

在Tool或Skill中通过process.env访问:

// 示例:自定义Tool
const apiKey = process.env.ANTHROPIC_API_KEY;
const response = await fetch(process.env.CUSTOM_API, {
  headers: { 'Authorization': `Bearer ${apiKey}` }
});

多租户场景

为不同客户提供独立工作空间

// openclaw.json - 多租户配置
{
  "multiTenant": {
    "enabled": true,
    "isolationLevel": "workspace",
    "tenantResolver": "subdomain"  // 或 header, query
  },
  "tenants": {
    "acme-corp": {
      "workspace": "acme-workspace",
      "domain": "acme.example.com",
      "channels": ["telegram-acme", "slack-acme"]
    },
    "globex-inc": {
      "workspace": "globex-workspace", 
      "domain": "globex.example.com",
      "channels": ["telegram-globex", "slack-globex"]
    }
  }
}

基于子域名的路由

// 请求: https://acme.example.com/api/chat
// 自动路由到 acme-workspace

// 请求: https://globex.example.com/api/chat  
// 自动路由到 globex-workspace

监控和日志

工作空间级日志

{
  "logging": {
    "workspaceLevel": true,
    "output": {
      "product-team": "~/.openclaw/logs/product-team.log",
      "support-team": "~/.openclaw/logs/support-team.log"
    }
  }
}

使用统计

# 查看工作空间使用统计
openclaw workspace stats product-team

# 输出:
# Workspace: product-team
# Active Sessions: 12
# Messages Today: 1,847
# Tokens Used: 2.3M
# API Cost: $23.45
# Avg Response Time: 2.3s

数据备份和迁移

导出工作空间

# 导出指定工作空间
openclaw workspace export product-team --output product-team-backup.tar.gz

# 导出内容:
# - 会话历史
# - 知识库索引
# - 配置文件
# - 环境变量(可选包含敏感信息)

导入工作空间

# 导入到新环境
openclaw workspace import product-team-backup.tar.gz --name product-team-v2

最佳实践

💡 建议的工作空间划分
工作空间 Agent类型 典型工具
product-team 产品专家 知识库、GitHub
support-team 客服专家 知识库、工单系统
sales-team 销售专家 CRM、邮件
dev-team 开发助手 GitHub、Docker
⚠️ 注意事项
  • 工作空间数量会影响Gateway资源占用,合理规划
  • 敏感环境变量建议加密存储
  • 跨工作空间共享数据需要额外配置
  • 定期备份工作空间数据
🎓 小结

工作空间是OpenClaw的企业级能力:

  • 逻辑隔离:每个团队有独立的Agent和配置
  • 灵活路由:消息自动分流到正确的工作空间
  • 数据安全:API Key等敏感信息完全隔离
  • 多租户支持:可服务多个客户

世界上的AI千千万,但会分工合作的,才叫真正的智能。