Examples
RoleResolverPlugin 场景
用一个 resolve 场景说明为什么某些点必须只有一个最终答案
RoleResolverPlugin 场景
场景
你希望某条链路里有人可以问:
- “当前这个用户的角色是什么?”
这种问题不是多插件协同改值,而是必须有一个唯一答案。
这就是 resolve 的典型场景。
示例
import {
Agent,
BasePlugin,
type PluginResolveHook,
} from "@downcity/agent";
class RoleResolverPlugin extends BasePlugin {
readonly name = "role_resolver";
readonly title = "Role Resolver";
readonly description = "Resolves one final role for a user.";
private readonly resolveRole: PluginResolveHook = async ({ value }) => {
const body = value as { userId?: unknown };
const userId = String(body.userId || "").trim();
if (!userId) {
throw new Error("userId is required");
}
if (userId === "owner") return "admin";
return "member";
};
readonly resolves = {
"auth.resolve_role": this.resolveRole,
};
}
const agent = new Agent({
id: "repo-helper",
path: "/path/to/project",
tools: {},
plugins: [new RoleResolverPlugin()],
});
const role = await agent.plugins.resolve<{ userId: string }, string>(
"auth.resolve_role",
{ userId: "owner" },
);为什么不能有两个 resolver
因为这类点的语义不是:
- 大家都来补一点
而是:
- 最终到底谁说了算
所以当前实现里,同一个 resolve 点重复注册会直接报错。