Personal Telegram developer console for GitHub

feat: add policy enforcing plugin router

davoodmehraban89 committed Oct 3, 2026 at 01:29 UTC 00fcd1041e2f8074dc34727635395896eace0cba
1 file changed +89
src/core/router.js new
+89
@@ -0,0 +1,89 @@
1 +export class PluginRouter {
2 + constructor({ registry, policy, audit } = {}) {
3 + if (!registry) throw new Error('PluginRouter registry is required');
4 + if (!policy) throw new Error('PluginRouter policy is required');
5 + if (!audit) throw new Error('PluginRouter audit logger is required');
6 +
7 + this.registry = registry;
8 + this.policy = policy;
9 + this.audit = audit;
10 + }
11 +
12 + async dispatchCommand(command, context = {}) {
13 + return this.#dispatch('resolveCommand', command, context);
14 + }
15 +
16 + async dispatchCallback(data, context = {}) {
17 + return this.#dispatch('resolveCallback', data, context);
18 + }
19 +
20 + async #dispatch(resolverName, input, context) {
21 + for (const plugin of this.registry.getPlugins()) {
22 + const resolver = plugin?.[resolverName];
23 + if (typeof resolver !== 'function') continue;
24 +
25 + const route = await resolver.call(plugin, input, context);
26 + if (!route) continue;
27 +
28 + const pluginId = plugin.manifest.id;
29 + const actionId = route.actionId;
30 + const registeredAction = this.registry.getAction(actionId);
31 + const action = registeredAction?.pluginId === pluginId ? registeredAction : null;
32 + const decision = this.policy.evaluate({
33 + action,
34 + isOwner: context.isOwner === true
35 + });
36 +
37 + this.audit.log({
38 + event: 'action_attempt',
39 + pluginId,
40 + actionId,
41 + risk: decision.risk,
42 + outcome: decision.allowed
43 + ? 'allowed'
44 + : (decision.requiresApproval ? 'approval_required' : 'denied'),
45 + reason: decision.reason
46 + });
47 +
48 + if (!decision.allowed) {
49 + return {
50 + ok: false,
51 + pluginId,
52 + actionId,
53 + risk: decision.risk,
54 + requiresApproval: decision.requiresApproval,
55 + denied: !decision.requiresApproval,
56 + reason: decision.reason
57 + };
58 + }
59 +
60 + if (typeof route.execute !== 'function') {
61 + throw new Error(`Plugin route execute function is required for ${actionId}`);
62 + }
63 +
64 + try {
65 + const result = await route.execute();
66 + this.audit.log({
67 + event: 'action_result',
68 + pluginId,
69 + actionId,
70 + risk: decision.risk,
71 + outcome: 'success'
72 + });
73 + return result;
74 + } catch (error) {
75 + this.audit.log({
76 + event: 'action_result',
77 + pluginId,
78 + actionId,
79 + risk: decision.risk,
80 + outcome: 'error',
81 + error: error?.message || 'Unknown plugin error'
82 + });
83 + throw error;
84 + }
85 + }
86 +
87 + return null;
88 + }
89 +}