MessageAugmentPower 场景
用一个 pipeline 场景说明 Power 如何对输入值做链式增强
场景
你希望在消息进入主执行链之前,自动补一点上下文:
- 当前页面标题
- 当前来源标记
这类“进链前增强值”的场景,非常适合 pipeline。
示例
import {
Power,
type PowerJsonObject,
type PowerHooks,
} from "@downcity/city/power";
class MessageAugmentPower extends Power {
readonly name = "message_augment";
readonly title = "Message Augment";
readonly description = "Adds extra fields to inbound message payloads.";
readonly hooks: PowerHooks = {
pipeline: {
"chat.enrich_message": [
async ({ value }) => {
const body = value as PowerJsonObject;
return {
...body,
source: body.source || "web",
pageTitle: body.pageTitle || "Untitled Page",
};
},
],
},
};
}
// City 已登记并把 message_augment 绑定到 entry 的 Agent/Workspace。
const message = await city.powers.scope({ agent_id: agent.id, workspace_id: workspace.id }).pipeline<PowerJsonObject>("chat.enrich_message", {
text: "Please summarize this page",
});这个例子想表达什么
- pipeline 的重点是“拿到一个值,返回一个新值”
- 多个 Power 可以继续往后接着改