newma
v0.1.2
Published
AI Agent Gateway — 把 Telegram/WeCom 消息路由到 Claude 或 CodeBuddy Agent SDK。
Readme
Newma
AI Agent Gateway — 把 Telegram/WeCom 消息路由到 Claude 或 CodeBuddy Agent SDK。
Telegram / WeCom
│
▼
┌─────────────┐ ┌──────────┐ ┌─────────────────┐
│ Channel │────▶│ Bot │────▶│ SDK Adapter │
│ Adapter │ │ Router │ │ (Claude/ │
│ (TG/WeCom) │◀────│ │◀────│ CodeBuddy) │
└─────────────┘ └──────────┘ └─────────────────┘快速开始
环境要求
- Bun >= 1.0
- Claude CLI 或 CodeBuddy CLI(已登录)
- pm2(生产部署用)
安装
git clone <repo-url> newma && cd newma
bun install配置
复制示例配置并编辑:
mkdir -p ~/.agent-gateway
cp config.local.toml ~/.agent-gateway/config.toml在项目根目录创建 .env 文件(Bun 自动加载):
TG_BOT_TOKEN=your-telegram-bot-token配置文件支持 ${ENV_VAR} 语法引用环境变量。
配置示例
timezone = "Asia/Shanghai"
[agents.coder]
model = "claude-sonnet-4-20250514"
permission_mode = "bypassPermissions"
system_prompt = "You are a helpful coding assistant."
cwd = "/home/user/projects/my-app" # agent 工作目录
disallowed_tools = ["AskUserQuestion"]
[agents.buddy]
sdk = "codebuddy" # 使用 CodeBuddy SDK
model = "claude-sonnet-4.6"
permission_mode = "bypassPermissions"
system_prompt = "你是一个友好的 AI 助手。"
[bots.my-bot]
channel = "tg"
chat_ids = [] # 空 = 接受所有 chat
agents = ["coder", "buddy"]
default_agent = "buddy"
session_mode = "chat" # chat = 保持会话上下文
[channels.tg]
type = "telegram"
bot_token = "${TG_BOT_TOKEN}"运行
# 开发(前台运行)
bun run dev
# 生产(pm2 后台管理)
bun run start # 启动
bun run stop # 停止
bun run restart # 重启
bun run status # 查看状态
bun run logs # 查看日志Agent 配置
| 字段 | 类型 | 说明 |
|------|------|------|
| model | string | 模型 ID(如 claude-sonnet-4-20250514) |
| sdk | "claude" | "codebuddy" | SDK 类型,默认 claude |
| permission_mode | string | default / acceptEdits / bypassPermissions / plan |
| system_prompt | string | 系统提示词 |
| cwd | string | Agent 工作目录,决定可访问的文件和 CLAUDE.md |
| max_turns | number | 最大对话轮次 |
| allowed_tools | string[] | 允许的工具白名单 |
| disallowed_tools | string[] | 禁止的工具黑名单 |
cwd
cwd 设置后,agent 会在该目录下工作,自动拾取:
CLAUDE.md— 项目级指令.mcp.json— MCP server 配置.claude/commands/— 自定义命令.claude/settings.json— 项目设置~/.claude/— 全局 skills
Bot 命令
在 Telegram 中可用的命令:
| 命令 | 说明 |
|------|------|
| /help | 显示帮助 |
| /agents | 列出可用 agent |
| /<agent名> | 切换到指定 agent |
| /status | 当前会话状态 |
| /reset | 重置当前会话 |
| /cancel | 取消正在执行的任务 |
| /topic <agent> | 创建 Forum topic |
也可以直接在消息中 @agent名 消息内容 来临时使用某个 agent。
Channel 支持
Telegram
长轮询模式,支持:
- 流式输出(逐字显示)
- Forum topic
- Markdown 格式化
- 消息分片(超长消息自动拆分)
WeCom(企业微信)
收消息用自建应用回调,发消息用群机器人 Webhook。
配置步骤:
- 企业微信管理后台 → 应用管理 → 创建自建应用
- 进入应用 → 接收消息 → 设置 API 接收:
- URL:
http://你的服务器:端口/callback - Token 和 EncodingAESKey: 自动生成,填入下方配置
- URL:
- 在目标群添加群机器人,获取 Webhook URL 中的
key参数
[channels.wecom]
type = "wecom"
webhook_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # 群机器人 Webhook key
token = "your-callback-token" # 应用回调 Token
encoding_aes_key = "your-43-char-aes-key" # 应用回调 EncodingAESKey
callback_port = 8080 # 回调监听端口
callback_path = "/callback" # 可选,默认 /callback注意:
- 回调服务需要外网可访问(服务器部署,或 ngrok 调试)
- 不支持流式输出,agent 执行完成后一次性发送
- 内置令牌桶限流(~100 条/分钟)和消息去重
定时任务
[[cron]]
id = "daily-report"
schedule = "0 9 * * *" # 每天 9:00
agent = "coder"
bot = "my-bot" # 输出到这个 bot 的 chat
prompt = "生成今日代码变更摘要"
enabled = true日志
结构化 JSON 日志(pino),默认 debug 级别。通过 LOG_LEVEL 环境变量调整:
LOG_LEVEL=info # debug / info / warn / errorpm2 模式下日志自动存储在 ~/.pm2/logs/。
开发
bun test # 运行测试
bun run typecheck # TypeScript 类型检查项目结构
src/
├── agents/ # SDK Adapters (Claude, CodeBuddy)
├── bots/ # Bot 逻辑、会话管理、命令系统
├── channels/ # Channel Adapters (Telegram, WeCom)
├── config/ # 配置加载与 Zod 验证
├── cron/ # 定时任务调度
├── utils/ # Logger, Lifecycle, Lock
└── index.ts # 入口