forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjected.js
More file actions
237 lines (214 loc) · 6.85 KB
/
injected.js
File metadata and controls
237 lines (214 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Agent OS Chrome Extension - Injected Page Script
*
* Runs in the page context to hook into Agent OS instances.
* Communicates with the content script via window.postMessage.
*/
(function() {
'use strict';
// Wait for Agent OS to be available
function waitForAgentOS(callback, maxAttempts = 50) {
let attempts = 0;
const check = () => {
attempts++;
if (window.__AGENT_OS__) {
callback(window.__AGENT_OS__);
} else if (attempts < maxAttempts) {
setTimeout(check, 100);
} else {
console.log('[Agent OS DevTools] No Agent OS instance found after', maxAttempts * 100, 'ms');
}
};
check();
}
// Hook into Agent OS
function hookAgentOS(agentOS) {
console.log('[Agent OS DevTools] Agent OS detected, hooking...');
// Hook message bus if available
if (agentOS.messageBus) {
hookMessageBus(agentOS.messageBus);
}
// Hook trust registry if available
if (agentOS.trustRegistry) {
hookTrustRegistry(agentOS.trustRegistry);
}
// Hook kernel if available
if (agentOS.kernel) {
hookKernel(agentOS.kernel);
}
// Add emit function if not present
if (!agentOS.emit) {
agentOS.emit = emitToDevTools;
} else {
// Wrap existing emit
const originalEmit = agentOS.emit.bind(agentOS);
agentOS.emit = function(event, data) {
emitToDevTools(event, data);
return originalEmit(event, data);
};
}
console.log('[Agent OS DevTools] Hooks installed');
emitToDevTools('connected', { version: agentOS.version });
}
// Hook into message bus
function hookMessageBus(messageBus) {
// Hook publish method
if (messageBus.publish) {
const originalPublish = messageBus.publish.bind(messageBus);
messageBus.publish = function(topic, message) {
emitToDevTools('amb-message', {
direction: 'outbound',
topic: topic,
type: 'publish',
sender: message.sender,
recipient: message.recipient || 'broadcast',
content: message.content,
signature: message.signature,
timestamp: Date.now()
});
return originalPublish(topic, message);
};
}
// Hook subscribe method to intercept incoming messages
if (messageBus.subscribe) {
const originalSubscribe = messageBus.subscribe.bind(messageBus);
messageBus.subscribe = function(topic, handler) {
const wrappedHandler = (message) => {
emitToDevTools('amb-message', {
direction: 'inbound',
topic: topic,
type: 'receive',
sender: message.sender,
recipient: message.recipient,
content: message.content,
signature: message.signature,
timestamp: Date.now()
});
return handler(message);
};
return originalSubscribe(topic, wrappedHandler);
};
}
}
// Hook into trust registry
function hookTrustRegistry(registry) {
// Hook register method
if (registry.register) {
const originalRegister = registry.register.bind(registry);
registry.register = function(agent, trustLevel) {
emitToDevTools('agent-registered', {
agentId: agent.agent_id || agent.id,
name: agent.name,
trustLevel: trustLevel?.name || trustLevel || 'MEDIUM',
publicKey: agent.public_key?.substring(0, 32),
capabilities: agent.capabilities,
timestamp: Date.now()
});
return originalRegister(agent, trustLevel);
};
}
// Hook verify method
if (registry.verify) {
const originalVerify = registry.verify.bind(registry);
registry.verify = function(message) {
const result = originalVerify(message);
emitToDevTools('iatp-verification', {
agentId: message.sender_id,
success: result.is_valid,
reason: result.rejection_reason,
timestamp: Date.now()
});
return result;
};
}
// Hook revoke method
if (registry.revoke) {
const originalRevoke = registry.revoke.bind(registry);
registry.revoke = function(agentId, reason) {
emitToDevTools('agent-removed', {
agentId: agentId,
reason: reason,
timestamp: Date.now()
});
return originalRevoke(agentId, reason);
};
}
}
// Hook into kernel
function hookKernel(kernel) {
// Hook policy violations
if (kernel.on) {
kernel.on('violation', (violation) => {
emitToDevTools('policy-violation', {
agentId: violation.agentId,
policy: violation.policy,
action: violation.action,
reason: violation.reason,
signal: violation.signal,
timestamp: Date.now()
});
});
}
// Hook signal dispatch
if (kernel.signalDispatcher && kernel.signalDispatcher.signal) {
const originalSignal = kernel.signalDispatcher.signal.bind(kernel.signalDispatcher);
kernel.signalDispatcher.signal = function(agentId, signal) {
emitToDevTools('signal-sent', {
agentId: agentId,
signal: signal.name || signal,
timestamp: Date.now()
});
return originalSignal(agentId, signal);
};
}
}
// Send event to DevTools via content script
function emitToDevTools(event, data) {
window.postMessage({
source: 'agent-os-page',
event: event,
data: data
}, '*');
}
// Expose API for DevTools console
window.__AGENT_OS_DEVTOOLS__ = {
getMessages: function() {
return '[Call from DevTools panel]';
},
getAgents: function() {
if (window.__AGENT_OS__?.trustRegistry) {
return window.__AGENT_OS__.trustRegistry.listAgents();
}
return {};
},
exportLogs: function() {
// Trigger export in DevTools panel
emitToDevTools('export-request', {});
},
clear: function() {
emitToDevTools('clear-request', {});
}
};
// Listen for DevTools connection
window.addEventListener('message', (event) => {
if (event.data.source === 'agent-os-extension' && event.data.type === 'devtools-connected') {
console.log('[Agent OS DevTools] DevTools panel connected');
// Resend current state
if (window.__AGENT_OS__?.trustRegistry) {
const agents = window.__AGENT_OS__.trustRegistry.listAgents();
Object.entries(agents).forEach(([id, agent]) => {
emitToDevTools('agent-registered', {
agentId: id,
name: agent.name,
trustLevel: agent.trustLevel,
publicKey: agent.publicKey?.substring(0, 32)
});
});
}
}
});
// Start watching for Agent OS
waitForAgentOS(hookAgentOS);
})();