Plugin

Chat 插件

Chat 插件如何将 Agent 连接到聊天渠道

Chat 插件

Chat 插件将 Agent 连接到 Telegram、飞书、QQ 等聊天渠道。它处理收到的消息,将它们路由到 Agent 会话,并将回复发送回渠道。

功能

  • 监听已配置渠道的消息
  • 为每个对话创建或复用会话
  • 将用户消息路由到 Agent
  • 将 Agent 回复发送回渠道
  • 在消息进入 Agent 前执行按 Agent 隔离的 Chat Access 准入

配置

Chat 在 ~/.downcity/plugins/chat/config.toml 中拥有完整的命名 profile。一个 profile 同时包含队列行为和 Agent 使用的渠道:

schema_version = 1

[profiles.primary.queue]
max_concurrency = 4

[[profiles.primary.channels]]
id = "telegram_primary"
type = "telegram"
name = "Primary Bot"
bot_token = "123456:token"
city plugin config chat primary --interactive
city plugin enable chat <agent_id> --profile primary

渠道凭据

渠道凭据明文保存在 config.toml,通过仅当前用户可访问的目录与文件权限保护(07000600)。CLI 与 Desktop 展示时会脱敏 bot_tokenapp_secret 等字段。每个 Agent 在自己的 agent.json 中选择一个 profile,也可以显式复用同一 profile。

Chat Access

外部用户第一次发送消息时,Chat Plugin 会在当前 Agent 的 .downcity/chat/access.db 中记录 Principal,并在没有 Grant 时创建 pending Request。管理员使用 Request ID 批准或拒绝:

city plugin action chat access-snapshot <agent_id> --input '{}' --token <token>
city plugin action chat access-approve <agent_id> --input '{"request_id":"<request_id>"}' --token <token>

身份由所选 profile 中的渠道 ID 和平台用户 ID 共同确定。批准只对目标 Agent 生效,不会成为 City 全局权限,也不会传播到其他 Plugin。

支持的渠道

  • Telegram
  • 飞书 (Lark)
  • QQ

飞书依赖

@downcity/plugins 默认不再安装飞书 / Lark SDK。如果你在本地嵌入 SDK 宿主中启用飞书渠道,需要在宿主应用里安装:

npm install @larksuiteoapi/node-sdk@^1.66.0

使用 Chat plugin 的独立子路径:

import { ChatPlugin, FeishuChannel } from "@downcity/plugins/chat";

不启用飞书的应用不需要安装飞书 SDK。

继续阅读: