监视器(change 监视器)

🔵 监视器是一个 pull 触发器:它监视外部来源,仅在出现新元素时启动 agent 运行(不同于 cron,后者按计划触发,与是否有新内容无关)。文件为 config/monitors.yaml,在启动时读取。

monitors:
  - name: news-watch
    source:
      type: rss
      url: "https://lenta.ru/rss"
    template: rss-agent
    prompt: "Кратко резюмируй эти новые статьи:\n{{items}}"
    notify: [{ type: telegram, chat_id: 123456 }]
    poll_secs: 300
字段 含义
name 唯一 id(同时也是游标键和 monitor:<name> 会话)
source.type 变更来源:rss、yandex_disk 或 plugin
source.url 订阅源 URL(用于 rss)
source.token_file Yandex refresh token 文件(用于 yandex_disk;由 yandex_tool 登录写入)
source.client_id / source.client_id_env OAuth client id 或环境变量名(用于 yandex_disk)
source.client_secret / source.client_secret_env OAuth client secret 或环境变量名(用于 yandex_disk)
source.path 要监视的 Disk 文件夹,默认 /(用于 yandex_disk)
source.limit 每次轮询的最大记录数,默认 50(用于 yandex_disk)
source.kind / source.config 原生来源的 kind 和配置(用于 plugin,参见原生插件)
template 在新元素上运行的 agent 模板
prompt prompt 模板;{{items}} 会被替换为新元素
notify 结果 sink(类型与 cron 相同,参见 Cron)
poll_secs 轮询间隔,最小 10(默认 300)

游标。 监视器将位置保存在 logs/monitors.db 中。首次轮询只是“追赶”(保存游标,不启动任何内容);之后 agent 仅在比游标更新的元素上启动。运行在 monitor:<name> 会话中进行,因此预算、hooks、工具和事件存储的行为与普通运行相同。

来源。 内置来源为 rss 和 yandex_disk。原生插件可以通过 type: plugin + kind 添加自己的 source.type(参见原生插件)。

Yandex.Disk

yandex_disk 会在文件夹中出现或变更文件时触发。需要由 Yandex 登录保存的 refresh token(与 yandex_tool 使用的 token_file 相同)。游标是最新的 modified 标记;每次轮询都会返回所有在此之后发生变更的内容,最新的在最上面。

monitors:
  - name: yandex-disk-watch
    source:
      type: yandex_disk
      token_file: data/yandex-oauth/disk_token.json
      client_id_env: YANDEX_OAUTH_CLIENT_ID
      client_secret_env: YANDEX_OAUTH_CLIENT_SECRET
      path: "/"
      limit: 50
    template: yandex-agent
    prompt: "На Яндекс.Диске появились новые/изменённые файлы:\n{{items}}\nКратко опиши их."
    poll_secs: 300

监视器是订阅事件的方式之一;其他方式见事件订阅。