Power 开发指南
使用 @downcity/city/power 构建由 City 提供的统一 Power。
Power 作者 API 位于 @downcity/city/power。Power 是一个长期实例,不使用按 Agent 创建实例的
工厂函数;需要区分执行范围时读取 PowerContext。
import { Power, create_action } from "@downcity/city/power";
export default class ExamplePower extends Power {
readonly name = "example";
readonly title = "Example";
readonly description = "An example Power.";
readonly actions = {
status: create_action({
description: "Return the current scope.",
execute: async ({ context }) => ({
success: true,
data: {
agent_id: context.agent.id,
workspace_id: context.workspace.id,
session_id: context.session?.id ?? null,
tool_call_id: context.call.tool_call_id ?? null,
},
}),
}),
};
}Workspace 文件使用 context.workspace.files;Power 私有状态使用 context.storage.files;
context.session 可以直接 prompt()、stop()、context() 或订阅变化。需要调用其他 Power
时使用 context.city.powers。
宿主可以直接把实例交给 City:
const city = new City({ powers: [new ExamplePower()], workspaces: [workspace] });
city.agents.add(agent);可安装 Power 的 main 默认导出实例或 { power, main },不再提供 module.create()。Renderer
只通过宿主 action gateway 通信,不能访问 Node、Electron 或 Agent 全量对象。
测试时重点验证:City 内实例唯一、initialize/dispose 各一次、每次调用获得正确且独立的
PowerContext,以及 Hook 作用域在 Step 结束后释放。