Skip to main content

Witty-Service

License: MIT PyPI version Python

AI Agent 全生命周期管理服务。Witty-Service 提供智能体的创建、沙箱运行、会话管理、消息交互等核心能力,通过统一的 REST API 屏蔽底层沙箱(Docker / Local Process / E2B)与运行时适配器(OpenClaw / OpenCode)的差异,让你以一致的方式编排和管理 AI Agent。

目录


项目介绍

Witty-Service 是一个面向 AI Agent 场景的后端服务,核心职责是将 Agent 生命周期管理、沙箱隔离运行、会话与消息交互 三者统一封装,对外提供简洁的 RESTful API。它作为上层应用(如 PolyMind)与底层 Agent 运行时之间的桥梁,使得前端无需关心 Agent 是运行在 Docker 容器、本地进程还是云端沙箱中。

典型应用场景:

  • AI 编程助手平台 — 为每个用户创建独立沙箱中的 Agent,隔离执行代码生成、文件操作等任务
  • 多模型对话服务 — 统一管理 OpenAI、Anthropic、DeepSeek 等多家 LLM 提供商的模型配置,按需切换
  • Agent 技能市场 — 通过技能仓库为 Agent 动态注入专业能力(如 CVE 分析等)
  • 企业级 Agent 编排 — 支持定时任务、会话暂停/恢复、运行时备份等企业级运维能力

功能特性

  • 🤖 Agent 全生命周期管理 — 创建、暂停、恢复、删除 Agent,支持运行时备份与恢复
  • 📦 多沙箱隔离 — 支持 Docker、Local Process、E2B 三种沙箱类型,按需选择隔离级别
  • 💬 会话与消息 — 多会话管理,支持 REST 非流式和 SSE 流式两种消息交互模式
  • 🔌 多运行时适配 — 通过 Adapter 层对接 OpenClaw、OpenCode 等不同 Agent 运行时
  • 🧠 多模型管理 — 统一配置 OpenAI、Anthropic、Google、DeepSeek、GLM、Kimi 等 10+ 模型提供商
  • 🛠️ 技能市场 — 内置技能仓库同步,支持从市场安装和上传自定义技能包
  • 🔐 安全认证 — 基于 Bearer Token 的 API 认证机制
  • 📊 CVE 与 Backport — 内置 CVE 漏洞分析和Backport服务

技术架构

层级 技术栈
Web 框架 FastAPI
ASGI 服务器 Uvicorn
数据库 SQLAlchemy + Alembic(迁移)
通信协议 WebSocket + REST + SSE
沙箱管理 Docker SDK / subprocess / E2B SDK
包管理 uv
语言 Python 3.11+

整体框架图

                              ┌─────────────────────────────────┐
                              │      上层应用(如 PolyMind)      │
                              └───────────────┬─────────────────┘
                                              │
                                     REST API / SSE
                                              │
                                              ▼
┌──────────────────────────────────────────────────────────────────────┐
│                           Witty-Service                              │
│                                                                      │
│  ┌─ API Layer ───────────────────────────────────────────────────┐   │
│  │  /agents  ·  /models  ·  /skills  ·  /cve  ·  /backport       │   │
│  │  Auth (Bearer Token)  ·  Error Handler  ·  Schemas            │   │
│  └──────────────────────────┬────────────────────────────────────┘   │
│                             │                                        │
│  ┌─ Application Layer ──────▼────────────────────────────────────┐   │
│  │  AgentManager · SessionManager · SkillManager                 │   │
│  │  CVEService  · BackportService                                │   │
│  └──┬────────────────────┬──────────────────────┬────────────────┘   │
│     │                    │                      │                    │
│  ┌──▼─────────────┐  ┌──▼──────────────┐  ┌───▼──────────────┐       │
│  │ Adapter Layer  │  │Persistence Layer│  │  Storage Layer   │       │
│  │ WebSocket客户端│  │ SQLAlchemy ORM  │  │  WorkspaceStore  │       │
│  │ HTTP 客户端    │  │ SQLite+Alembic  │  │  RuntimeBackup   │       │
│  │ 连接池/协议     │  │ Repository      │  │                  │       │
│  └──┬─────────────┘  └─────────────────┘  └──────────────────┘       │
│     │                                                                │
│  ┌──▼─────────────────────────────────────────────────────────────┐  │
│  │  Sandbox Layer                                                 │  │
│  │  ┌──────────┐  ┌──────────────┐  ┌──────────┐                  │  │
│  │  │  Docker  │  │Local Process │  │   E2B    │                  │  │
│  │  └────┬─────┘  └──────┬───────┘  └────┬─────┘                  │  │
│  │       └────────┬──────┘               │                        │  │
│  │                │                      │                        │  │
│  │       AdapterEndpoint                 │                        │  │
│  └────────────────┼──────────────────────┼────────────────────────┘  │
│                   │                      │                           │
│         ┌─────────────────────┐  ┌───────▼──────────┐                │
│         │ Domain (Enums/Errors)│  │   Config         │               │
│         └─────────────────────┘  └──────────────────┘               │
└──────────────────┼──────────────────────┼───────────────────────────┘
                   │                      │
         ┌─────────▼──────────┐  ┌────────▼─────────┐
         │   HTTP REST        │  │   HTTP REST       │
         │ (生命周期/技能管理)  │  │ (E2B Cloud API)  │
         └─────────┬──────────┘  └──────────────────┘
                   │
         ┌─────────▼──────────┐
         │   WebSocket        │
         │ (流式消息/事件推送)  │
         └─────────┬──────────┘
                   │
                   ▼
┌──────────────────────────────────────────────────────────────────────┐
│                       Witty-Agent-Server                             │
│                                                                      │
│  ┌─ API Layer ───────────────────────────────────────────────────┐   │
│  │  AgentRouter  ·  SessionRouter  ·  SessionWSRouter            │   │
│  └──────────────────────────┬────────────────────────────────────┘   │
│                             │                                        │
│  ┌─ Application Layer ──────▼────────────────────────────────────┐   │
│  │  AgentService  ·  SessionService  ·  SkillService             │   │
│  │  SessionWSOrchestrator  ·  TaskPool                           │   │
│  │  Materialization                                              │   │
│  └──────────────────────────┬────────────────────────────────────┘   │
│                             │                                        │
│  ┌─ Runtime Layer ──────────▼────────────────────────────────────┐   │
│  │  RuntimeBase (ABC)                                            │   │
│  │  ├─ OpenClawGatewayRuntime                                    │   │
│  │  └─ OpenCodeRuntime (WIP)                                     │   │
│  └───────────────────────────┬────────────────────────────────────┘  │
│                              │                                       │
│  ┌─ Adapter Layer ──────────▼──────────────┐   ┌─ Infra Layer ───┐   │
│  │ OpenClawAdapter  ·  RuntimeRegistry      │  │  GatewayClient  │   │
│  └──────────────────────────────────────────┘  │  (WS RPC)       │   │
│                                                 └────────┬────────┘  │
└──────────────────────────────────────────────────────────┼───────────┘
                                                           │
                                                     WebSocket RPC
                                                           │
                                                           ▼
                                           ┌──────────────────────────┐
                                           │    OpenClaw Gateway      │
                                           │  (Agent Runtime Platform)│
                                           └──────────────────────────┘

安装指南

方式一:pip安装

如果你只需要运行 Witty-Service,无需参与开发,可以直接通过 pip 安装:

pip install witty-service

安装完成后即可使用 CLI 启动服务:

witty-service --host 0.0.0.0 --port 8000

前置条件: Python 3.11 或更高版本

方式二:从源码安装

如果你需要参与开发或自定义构建,请从源码安装:

前置条件:

依赖 说明 安装方式
Python 3.11+ python.org
uv Python 包管理器 docs.astral.sh/uv
Docker 沙箱运行时(可选) docker.com

安装步骤:

  1. 克隆仓库:
git clone https://gitcode.com/openeuler/witty-service.git
cd witty-service
  1. 创建虚拟环境并安装依赖:
uv venv
source .venv/bin/activate
uv pip install -e ".[dev]"
  1. 验证安装:
witty-service --help

快速开始

1. 启动服务

witty-service --host 0.0.0.0 --port 8000

2. 验证服务是否正常运行

curl http://127.0.0.1:8000/healthz

期望返回:

{"status": "ok"}

配置说明

Witty-Service 通过环境变量进行配置,无需额外配置文件。

核心配置

环境变量 说明 默认值
AUTH_TOKEN API 认证 Token dev-token
WITTY_AGENT_SERVER_APP_DIR Local Process 模式下 witty-agent-server 代码目录 空
PATCHFLOW_STATE_ROOT Patchflow 锁与日志的统一根目录 ~/.patchflow

Witty Insight 集成配置

环境变量 说明 默认值
WITTY_INSIGHT_ENABLED 是否启用 witty-insight 集成 true
WITTY_INSIGHT_BASE_URL witty-insight API 地址 http://127.0.0.1:7396
WITTY_INSIGHT_TIMEOUT_SECONDS witty-insight 请求超时(秒) 10
WITTY_INSIGHT_BEARER_TOKEN 调用 witty-insight 时附带的 Bearer Token(可选) 空

默认推荐同机部署并通过 127.0.0.1 连接 witty-insight。witty-service 默认启用 Insight 集成;如果上游 witty-insight 当前未启动,witty-service 仍可正常启动。若需关闭该集成,可显式设置 WITTY_INSIGHT_ENABLED=false。 修改上述环境变量后,需要重启 witty-service 进程才能生效。

同机部署示例:

export WITTY_INSIGHT_BASE_URL=http://127.0.0.1:7396
export WITTY_INSIGHT_TIMEOUT_SECONDS=10

witty-service --host 0.0.0.0 --port 8000

分开部署示例:

export WITTY_INSIGHT_BASE_URL=http://10.0.0.8:7396
export WITTY_INSIGHT_TIMEOUT_SECONDS=10

witty-service --host 0.0.0.0 --port 8000

显式关闭 Insight 集成:

export WITTY_INSIGHT_ENABLED=false

witty-service --host 0.0.0.0 --port 8000

Docker 沙箱配置

环境变量 说明 默认值
WITTY_DOCKER_HOST Docker 服务监听地址 127.0.0.1
WITTY_DOCKER_IMAGE 镜像名(不含 tag) witty-agent-server
WITTY_DOCKER_IMAGE_TAG 镜像 tag latest
WITTY_DOCKER_CONTAINER_PORT 容器内服务端口 8080
WITTY_DOCKER_CONTAINER_WORKSPACE_PATH 容器内工作区路径 /witty-workspace
WITTY_DOCKER_STOP_TIMEOUT 容器停止超时(秒) 10

IM Channel 渠道配置

环境变量 说明 默认值
WITTY_CHANNEL_ENABLED 渠道网关总开关 true
WITTY_CHANNEL_CREDENTIALS_DIR 渠道凭据目录(机器人 Secret 以 0600 文件存放于此) ~/.witty/channel-credentials
WITTY_CHANNEL_STALL_WINDOW_SECONDS 停滞窗口长度(秒) 90
WITTY_CHANNEL_HEALTH_INTERVAL_SECONDS 连接健康复查间隔(秒) 15
WITTY_CHANNEL_QUEUE_DEPTH 每会话队列深度 3
WITTY_CHANNEL_EDIT_THROTTLE_MS 占位消息节流更新间隔(毫秒) 0
WITTY_CHANNEL_INBOUND_RETENTION_DAYS 入站去重记录保留天数 7

渠道凭据放在哪里。 机器人 Secret 不写入数据库:主库 channel_instances 只保存一个不透明的 引用,凭据本体是 $WITTY_CHANNEL_CREDENTIALS_DIR/<引用>.json,目录 0700、文件 0600。 服务启动时创建该目录;若目录已存在但对 group/other 可读(或所有者不是运行用户), 渠道网关拒绝启动并把实例标为 disabled——错误日志里会直接给出 chmod 命令。 这样设计的取舍见 docs/adr/0004-channel-credentials-outside-the-database.md。

运维要点:凭据目录必须与主库一起备份(它就是凭据,主库无法恢复它), 且备份介质本身应当加密——凭据文件是明文。 边界说明:文件权限能挡住同机其他用户,挡不住以同一 OS 用户运行的 agent 工具进程。要让凭据远离自身 agent,需要把 agent 放到另一个 UID 下运行。

升级说明:早期版本用 WITTY_CHANNEL_SECRET_KEY(Fernet 主密钥)加密凭据后落库。 该变量与那批密文都已废弃;升级后存量渠道实例会被标记为 error,需要重新接入。


部署流程

测试环境

适用于集成测试和功能验证:

# 构建 pip 包
uv build

# 安装构建产物
uv pip install dist/witty_service-0.1.0-py3-none-any.whl

# 初始化数据库 schema(推荐)
alembic upgrade head

# 启动服务
witty-service --host 0.0.0.0 --port 8000

若仅做临时本地验证,也可显式设置 WITTY_DATABASE_AUTO_CREATE=true 让服务启动时自动建表;默认不会自动建表。

运行测试:

# 单元测试
uv run pytest tests/unit/ -q

# E2E 测试
uv run pytest tests/e2e/ -q

# 全量测试
uv run pytest tests/ -q

生产环境

方式一:pip 安装(推荐)

pip install witty-service

# 部署新版本前先执行数据库迁移
alembic upgrade head

# 多 worker 启动
witty-service --host 0.0.0.0 --port 8000 --workers 4

方式二:从源码构建

git clone https://gitcode.com/openeuler/witty-service.git
cd witty-service
uv build
uv pip install dist/witty_service-0.1.0-py3-none-any.whl

alembic upgrade head

witty-service --host 0.0.0.0 --port 8000 --workers 4

启动参数说明

参数 说明 默认值
--host 绑定的主机地址 0.0.0.0
--port 绑定的端口 8000
--log-level 日志级别(debug/info/warning/error/critical) info
--reload 开发模式自动重载 False
--workers 工作进程数 1

生产环境注意事项

  • 认证 Token — 务必通过 AUTH_TOKEN 环境变量修改默认值,使用强随机字符串
  • Worker 数量 — 根据 CPU 核心数合理设置 --workers
  • 反向代理 — 推荐在 Witty-Service 前部署 Nginx 等反向代理,配置 HTTPS 证书
  • 进程管理 — 建议使用 systemd 或 Supervisor 管理服务进程,实现自动重启
  • 数据库迁移 — 默认不会在启动时自动建表;请优先执行 alembic upgrade head,仅开发/测试临时场景才显式设置 WITTY_DATABASE_AUTO_CREATE=true

本地开发

环境搭建

# 1. 克隆仓库
git clone https://gitcode.com/openeuler/witty-service.git
cd witty-service

# 2. 创建虚拟环境并安装依赖
uv venv
source .venv/bin/activate
uv pip install -e ".[dev]"

# 3. 初始化数据库
alembic upgrade head

# 4. 启动开发服务器
uv run uvicorn src.witty_service.main:create_app --factory --host 0.0.0.0 --port 8000 --reload

项目结构

witty-service/
├── src/
│   ├── witty_service/                # 主服务包
│   │   ├── main.py                   # FastAPI 应用入口
│   │   ├── cli.py                    # CLI 入口
│   │   ├── config.py                 # 配置管理
│   │   ├── api/                      # API 路由层
│   │   │   ├── agents.py             # Agent 相关接口
│   │   │   ├── models.py             # 模型配置接口
│   │   │   ├── skills.py             # 技能管理接口
│   │   │   ├── cve.py                # CVE 漏洞接口
│   │   │   ├── backport.py           # 代码回溯接口
│   │   │   ├── auth.py               # 认证中间件
│   │   │   ├── errors.py             # 统一错误处理
│   │   │   ├── channels.py           # IM 渠道接口(框架设计 §9)
│   │   │   ├── channel_schemas.py    # 渠道接口的请求/响应模型
│   │   │   └── schemas.py            # 请求/响应模型
│   │   ├── application/              # 业务逻辑层
│   │   │   ├── agent_manager.py      # Agent 生命周期管理
│   │   │   ├── session_manager.py    # 会话管理
│   │   │   └── skill_manager.py      # 技能管理
│   │   ├── adapter/                  # 适配器层(与 witty-agent-server 通信)
│   │   │   ├── websocket_client.py   # WebSocket 客户端
│   │   │   ├── websocket_protocol.py # WebSocket 协议定义
│   │   │   └── http_client.py        # HTTP 客户端
│   │   ├── sandbox/                  # 沙箱层
│   │   │   ├── base.py               # 沙箱基类
│   │   │   ├── docker.py             # Docker 沙箱
│   │   │   ├── local_process.py      # 本地进程沙箱
│   │   │   ├── e2b.py                # E2B 云沙箱
│   │   │   └── factory.py            # 沙箱工厂
│   │   ├── channels/                 # IM 渠道层(企微/QQ/钉钉/飞书私聊入口)
│   │   │   ├── contracts.py          # 渠道契约、能力声明与适配器注册表
│   │   │   ├── router.py             # 会话路由:队列、占位消息、绑定闸门、出站唯一出口
│   │   │   ├── access_policy.py      # 准入策略(纯函数,fail-closed)
│   │   │   ├── delivery.py           # 呈现规划(纯函数:分段与呈现方式)
│   │   │   ├── commands.py           # 命令解析与用户可见文案
│   │   │   ├── dedup.py              # 入站去重与周期清理
│   │   │   ├── turn_gateway.py       # 调用 AgentManager 的窄接口
│   │   │   ├── crypto.py             # 凭据加密(Fernet)
│   │   │   ├── gateway.py            # 渠道网关:worker 守卫、连接监督、入站分发
│   │   │   ├── provisioning/         # 扫码接入与手填凭据旁路
│   │   │   └── adapters/             # 各渠道适配器(自注册进 ADAPTER_REGISTRY)
│   │   ├── domain/                   # 领域模型
│   │   ├── persistence/              # 数据持久化(含渠道仓储 channel_repository.py)
│   │   └── storage/                  # 文件存储
│   └── witty_agent_server/           # Agent 运行时服务
│       ├── app.py                    # FastAPI 应用
│       ├── api/routers/              # API 路由
│       ├── application/services/     # 业务服务
│       │   ├── agent/                # Agent 服务
│       │   ├── session/              # Session 服务
│       │   └── skill/                # 技能服务
│       ├── runtimes/                 # 运行时实现
│       ├── adapters/                 # 运行时适配器
│       └── infra/                    # 基础设施
├── tests/
│   ├── unit/                         # 单元测试
│   └── e2e/                          # 端到端测试
├── alembic/                          # 数据库迁移
├── docs/                             # 文档
├── pyproject.toml                    # 项目配置
└── .github/workflows/                # CI/CD 工作流

常用开发命令

命令 说明
uv run uvicorn src.witty_service.main:create_app --factory --reload 启动开发服务器(热重载)
uv run pytest tests/unit/ -q 运行单元测试
uv run pytest tests/e2e/ -q 运行 E2E 测试
uv run pytest tests/ -q 运行全量测试
uv build 构建 pip 包
alembic revision --autogenerate -m "description" 生成数据库迁移脚本
alembic upgrade head 执行数据库迁移

代码贡献流程

  1. Fork 本仓库
  2. 创建功能分支:git checkout -b feature/your-feature
  3. 提交更改:git commit -m 'feat: add your feature'
  4. 推送分支:git push origin feature/your-feature
  5. 提交 Pull Request

开发规范

  • 代码风格 — 使用 Ruff(lint + format,line-length=88)替代 Black/Flake8,配置见 pyproject.toml 的 [tool.ruff]
  • 类型检查 — 使用 mypy 进行静态类型检查,配置为 strict 模式
  • 提交规范 — 使用语义化提交信息(如 feat:、fix:、docs:、refactor:),pre-commit 的 gitlint 钩子会校验格式
  • 测试覆盖 — 新增功能需编写对应的单元测试,确保测试通过
  • 数据库迁移 — 涉及模型变更时,需生成对应的 Alembic 迁移脚本

代码检查(pre-commit)

  1. 安装 pre-commit:uv tool install pre-commit
  2. 安装 git 钩子:pre-commit install --hook-type pre-commit --hook-type commit-msg(或沿用仓库习惯 cp .githooks/pre-commit .git/hooks/pre-commit 后,再执行 pre-commit install --hook-type commit-msg 启用 gitlint 提交信息校验)
  3. 提交时自动检查暂存文件;全量检查需先收口存量:uv run ruff check --fix . && uv run ruff format .,再执行 pre-commit run --all-files
  4. 检查项:Ruff(lint/format)、Bandit(安全)、Codespell(拼写)、pre-commit-hooks(基础格式与密钥)、markdownlint、ShellCheck(本机安装时生效)、gitlint(提交信息);Gitleaks(密钥深扫)在网络就绪后启用

许可证

本项目基于 MIT 许可证 开源。

支持与反馈

Metadata

Release files for witty-service 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for witty-service 0.12.0
File Size Uploaded Provenance Actions
witty_service-0.12.0.tar.gz 501.8 kB Without provenance Details for witty_service-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for witty-service 0.12.0
File Interpreter ABI Platform Actions
witty_service-0.12.0-py3-none-any.whl Python 3 none any Details for witty_service-0.12.0-py3-none-any.whl

Total release size: 1.1 MB

Release files / witty_service-0.12.0.tar.gz

Download URL witty_service-0.12.0.tar.gz
Size 501.8 kB
Tags Source
SHA-256 checksum
How to use checksums
296ac15f16fe20f206ea5648617f7f14ea38f3235b8c12bc61ababb39dcbd8e7
BLAKE2b-256 checksum
How to use checksums
8b104988bb0fcc9753df15ef84ed3503803fecb12540f34ca3dc1d5367e73639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / witty_service-0.12.0-py3-none-any.whl

Download URL witty_service-0.12.0-py3-none-any.whl
Size 596.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb382b18a0c1eb3eab8a519e346532dfc2429bdf960b2db9dc01f946b30805d9
BLAKE2b-256 checksum
How to use checksums
7a45947e10d2f59271c10da2de40fbb5b8b80e1173dcde4b6b549c1d72a34b9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for witty-service 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page