Skip to content

新增 webhook 插件:外部触发入口 #3

Description

@v833

Suggested labels: enhancement

Background

Current behavior:

  • agent-os 任务只能由飞书消息(@ bot)触发,入口单一。
  • 外部系统(CI、监控报警、其他 Agent)无法编程式触发任务。

Problem:

  • "指挥台"缺少外部入口;无法与 CI/CD、告警系统联动。

Needed change:

  • 新增 webhook 插件:本地 HTTP server 接收 POST,鉴权后把请求体作为任务 prompt 启动一轮 CLI 任务,结果回传配置的话题(或 webhook 回调)。

Goal

实现一个独立的 webhook 插件,让外部系统通过 HTTP 触发 agent-os 任务。

User Stories

  • As a 运维, I want CI 失败时自动 POST 给 agent-os 让 bot 分析日志, so that 免人工介入。
  • As a 开发者, I want 用 curl 触发一个任务并拿到结果, so that 脚本可编程驱动。
  • As a 管理员, I want webhook 需要 token 鉴权, so that 外部无法随意触发。

Requirements

  1. webhook 插件(src/plugins/webhook.ts
    • HTTP server(Node 内置 http 模块即可,不新增框架)。
    • POST /webhook:body 含 botId / 话题或工作目录 / prompt,鉴权(Authorization: Bearer <token>,token 配置在插件 config)。
    • 触发:构造 StartTaskInput 交给 ctx.tasks.startTask
    • 结果:默认回传配置话题;若 body 带 callbackUrl 则任务完成时 POST 回执。
  2. 配置(cordis.yml
    • - name: webhook,config:{ port, token, botId?, topicId?, workspace? }
  3. 安全
    • token 必填(缺失拒绝启动,fail fast);请求体大小限制;简单限流(可选 P0)。
  4. 管理
    • 启动日志打印 server 地址;/webhook 命令显示状态(可选)。
  5. 插件装配
    • cordis.yml 增加 webhook 条目;src/plugins/loader.ts 登记;插件卸载时关闭 server。

Examples / Output

POST http://localhost:8787/webhook
Authorization: Bearer <token>
Content-Type: application/json
{ "prompt": "读取 data/logs/error.log 并总结最新错误", "botId": "developer", "callbackUrl": "https://ci.example.com/agent-os/callback" }

→ 200 { "taskId": "...", "status": "accepted" }
→ 完成时向 callbackUrl POST { "taskId": "...", "status": "done", "answer": "..." }

Acceptance Criteria

  • 未带有效 token 的请求返回 401,且不触发任务。
  • 合法 POST 触发一轮任务,结果回传配置话题。
  • callbackUrl 存在时任务完成回调。
  • 插件移除后 server 关闭,现有路径不受影响。
  • 启动阶段 token 缺失时报错(fail fast)。

Test Plan

  • Automated tests: webhook 插件单测(鉴权、请求解析、触发、回调、限流)——用临时端口起 server 测试。
  • Manual verification: curl 触发真实任务,观察卡片与回调。
  • Regression coverage: pnpm build && pnpm test 全绿。

Out of Scope

  • 公网穿透 / HTTPS(部署层处理)。
  • 多实例 / 横向扩展。
  • 与定时任务插件的联动(可后续做)。

Dependencies / Risks

  • 监听端口可能与本地服务冲突(可配置)。
  • 触发任务需映射到有效 bot / 话题;无效映射要明确报错。
  • token 放 .env 或 cordis.yml config,遵循"凭证不硬编码"约定。

Effort Estimate

  • Implementation: 1-2 天
  • Tests + docs: 0.5-1 天
  • Total: 1.5-3 天

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions