Examples

AuditEffectPlugin 场景

用一个 effect 场景说明 plugin 如何只做副作用,而不改主值

AuditEffectPlugin 场景

场景

你想记录某类事件:

  • 某条消息进来了
  • 某个动作被触发了

但你不想:

  • 改写主值
  • 阻断流程

这时最适合 effect

示例

import { Agent, BasePlugin, type PluginHooks } from "@downcity/agent";

class AuditEffectPlugin extends BasePlugin {
  readonly name = "audit_effect";
  readonly title = "Audit Effect";
  readonly description = "Writes one audit line for every observed event.";

  readonly hooks: PluginHooks = {
    effect: {
      "audit.observe": [
        async ({ value, plugin }) => {
          console.log(`[${plugin}] observed`, value);
        },
      ],
    },
  };
}

const agent = new Agent({
  id: "repo-helper",
  path: "/path/to/project",
  tools: {},
  plugins: [new AuditEffectPlugin()],
});

await agent.plugins.effect("audit.observe", {
  event: "incoming_message",
  sessionId: "sess_001",
});

为什么这里不是 pipeline

因为你并不关心返回一个新值,只关心副作用本身。

相关文档