-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathopenai_agents_sdk.py
More file actions
406 lines (331 loc) · 13.2 KB
/
openai_agents_sdk.py
File metadata and controls
406 lines (331 loc) · 13.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
OpenAI Agents SDK Integration for Agent-OS
============================================
Provides kernel-level governance for OpenAI Agents SDK workflows.
Features:
- Policy enforcement for agent tool calls
- Guardrails integration with Agent-OS policies
- Handoff monitoring and approval
- Streaming response governance
Example:
>>> from agent_os.integrations.openai_agents import OpenAIAgentsKernel
>>> from agent_os.policies import GovernancePolicy
>>> from agents import Agent, Runner
>>>
>>> # Create governed kernel
>>> policy = GovernancePolicy(
... max_tool_calls=10,
... allowed_tools=["file_search", "code_interpreter"],
... blocked_patterns=["rm -rf", "DROP TABLE"],
... )
>>> kernel = OpenAIAgentsKernel(policy=policy)
>>>
>>> # Wrap agent
>>> agent = Agent(name="assistant", model="gpt-4o")
>>> governed_agent = kernel.wrap(agent)
>>>
>>> # Run with governance
>>> result = await Runner.run(governed_agent, "Analyze this data")
"""
from __future__ import annotations
import asyncio
import logging
import time
from dataclasses import dataclass, field
from datetime import datetime, timezone
from functools import wraps
from typing import Any, Callable
logger = logging.getLogger(__name__)
@dataclass
class GovernancePolicy:
"""Policy configuration for OpenAI Agents."""
# Tool limits
max_tool_calls: int = 50
max_handoffs: int = 5
timeout_seconds: int = 300
# Tool filtering
allowed_tools: list[str] = field(default_factory=list)
blocked_tools: list[str] = field(default_factory=list)
# Content filtering
blocked_patterns: list[str] = field(default_factory=list)
pii_detection: bool = True
# Approval flows
require_human_approval: bool = False
approval_threshold: float = 0.8
# Audit
log_all_calls: bool = True
checkpoint_frequency: int = 5
@dataclass
class ExecutionContext:
"""Runtime context for governed execution."""
session_id: str
agent_id: str
policy: GovernancePolicy
# Counters
tool_calls: int = 0
handoffs: int = 0
# Timing
started_at: datetime = field(default_factory=datetime.utcnow)
# Audit trail
events: list[dict[str, Any]] = field(default_factory=list)
def record_event(self, event_type: str, data: dict[str, Any]) -> None:
self.events.append({
"type": event_type,
"timestamp": datetime.now(timezone.utc).isoformat(),
"data": data,
})
class PolicyViolationError(Exception):
"""Raised when a policy violation is detected."""
def __init__(self, policy_name: str, description: str, severity: str = "high"):
self.policy_name = policy_name
self.description = description
self.severity = severity
super().__init__(f"Policy violation ({policy_name}): {description}")
class OpenAIAgentsKernel:
"""
Governance kernel for OpenAI Agents SDK.
Wraps Agent and Runner to enforce policies during execution.
"""
def __init__(
self,
policy: GovernancePolicy | None = None,
on_violation: Callable[[PolicyViolationError], None] | None = None,
) -> None:
self.policy: GovernancePolicy = policy or GovernancePolicy()
self.on_violation: Callable[[PolicyViolationError], None] = (
on_violation or self._default_violation_handler
)
self._contexts: dict[str, ExecutionContext] = {}
self._wrapped_agents: dict[str, Any] = {}
self._start_time: float = time.monotonic()
self._last_error: str | None = None
def _default_violation_handler(self, error: PolicyViolationError) -> None:
logger.error(f"Policy violation: {error}")
def _create_context(self, agent_id: str) -> ExecutionContext:
"""Create execution context for an agent."""
import uuid
session_id = str(uuid.uuid4())[:8]
ctx = ExecutionContext(
session_id=session_id,
agent_id=agent_id,
policy=self.policy,
)
self._contexts[session_id] = ctx
return ctx
def _check_tool_allowed(self, tool_name: str) -> tuple[bool, str]:
"""Check if tool is allowed by policy."""
# Check blocked list
if tool_name in self.policy.blocked_tools:
return False, f"Tool '{tool_name}' is blocked by policy"
# Check allowed list (if specified)
if self.policy.allowed_tools:
if tool_name not in self.policy.allowed_tools:
return False, f"Tool '{tool_name}' not in allowed list"
return True, ""
def _check_content(self, content: str) -> tuple[bool, str]:
"""Check content against blocked patterns."""
content_lower = content.lower()
for pattern in self.policy.blocked_patterns:
if pattern.lower() in content_lower:
return False, f"Content matches blocked pattern: {pattern}"
return True, ""
def wrap(self, agent: Any) -> Any:
"""
Wrap an OpenAI Agent with governance.
Args:
agent: OpenAI Agents SDK Agent instance
Returns:
Governed agent wrapper
"""
agent_id = getattr(agent, "name", str(id(agent)))
# Create wrapper class
class GovernedAgent:
def __init__(wrapper_self, original: Any, kernel: OpenAIAgentsKernel):
wrapper_self._original = original
wrapper_self._kernel = kernel
wrapper_self._context = kernel._create_context(agent_id)
# Copy attributes
for attr in ["name", "model", "instructions", "tools"]:
if hasattr(original, attr):
setattr(wrapper_self, attr, getattr(original, attr))
@property
def original(wrapper_self) -> Any:
return wrapper_self._original
def __getattr__(wrapper_self, name: str) -> Any:
return getattr(wrapper_self._original, name)
wrapped = GovernedAgent(agent, self)
self._wrapped_agents[agent_id] = wrapped
logger.info(f"Wrapped agent '{agent_id}' with governance kernel")
return wrapped
def unwrap(self, governed_agent: Any) -> Any:
"""Remove governance wrapper."""
if hasattr(governed_agent, "_original"):
return governed_agent._original
return governed_agent
def wrap_runner(self, runner_class: Any) -> Any:
"""
Wrap the Runner class to intercept executions.
Args:
runner_class: OpenAI Agents SDK Runner class
Returns:
Governed Runner class
"""
kernel = self
class GovernedRunner:
@classmethod
async def run(
cls,
agent: Any,
input_text: str,
**kwargs,
) -> Any:
# Get context
ctx = None
if hasattr(agent, "_context"):
ctx = agent._context
# Pre-execution checks
if ctx:
# Check content
ok, reason = kernel._check_content(input_text)
if not ok:
error = PolicyViolationError("content_filter", reason)
kernel.on_violation(error)
if kernel.policy.require_human_approval:
raise error
ctx.record_event("run_start", {"input_length": len(input_text)})
# Get original agent
original_agent = agent
if hasattr(agent, "_original"):
original_agent = agent._original
# Run with monitoring
try:
result = await runner_class.run(original_agent, input_text, **kwargs)
if ctx:
ctx.record_event("run_complete", {"success": True})
return result
except Exception as e:
if ctx:
ctx.record_event("run_error", {"error": str(e)})
raise
@classmethod
def run_sync(cls, agent: Any, input_text: str, **kwargs) -> Any:
return asyncio.run(cls.run(agent, input_text, **kwargs))
return GovernedRunner
def create_tool_guard(self) -> Callable:
"""
Create a tool execution guard.
Use as a decorator or wrapper for tool functions.
"""
kernel = self
def guard(func: Callable) -> Callable:
@wraps(func)
async def wrapper(*args, **kwargs):
tool_name = func.__name__
# Check if tool is allowed
ok, reason = kernel._check_tool_allowed(tool_name)
if not ok:
error = PolicyViolationError("tool_filter", reason)
kernel.on_violation(error)
raise error
# Check content in arguments
for arg in args:
if isinstance(arg, str):
ok, reason = kernel._check_content(arg)
if not ok:
error = PolicyViolationError("content_filter", reason)
kernel.on_violation(error)
raise error
for value in kwargs.values():
if isinstance(value, str):
ok, reason = kernel._check_content(value)
if not ok:
error = PolicyViolationError("content_filter", reason)
kernel.on_violation(error)
raise error
# Execute
if asyncio.iscoroutinefunction(func):
return await func(*args, **kwargs)
return func(*args, **kwargs)
return wrapper
return guard
def create_guardrail(self) -> Any:
"""
Create an OpenAI Agents SDK compatible guardrail.
Returns a guardrail that can be added to an agent.
"""
kernel = self
class PolicyGuardrail:
"""Agent-OS policy guardrail for OpenAI Agents SDK."""
async def __call__(
self,
context: Any,
agent: Any,
input_text: str,
) -> str | None:
"""
Check input against policies.
Returns None if allowed, or a rejection message if blocked.
"""
# Check content patterns
ok, reason = kernel._check_content(input_text)
if not ok:
logger.warning(f"Guardrail blocked: {reason}")
return f"Request blocked by policy: {reason}"
# Check tool calls if in context
if hasattr(context, "tool_calls"):
for tool_call in context.tool_calls:
tool_name = getattr(tool_call, "name", "")
ok, reason = kernel._check_tool_allowed(tool_name)
if not ok:
logger.warning(f"Guardrail blocked tool: {reason}")
return f"Tool blocked by policy: {reason}"
return None # Allowed
return PolicyGuardrail()
def get_context(self, session_id: str) -> ExecutionContext | None:
"""Get execution context by session ID."""
return self._contexts.get(session_id)
def get_audit_log(self, session_id: str) -> list[dict[str, Any]]:
"""Get audit log for a session."""
ctx = self._contexts.get(session_id)
if ctx:
return ctx.events
return []
def get_stats(self) -> dict[str, Any]:
"""Get governance statistics."""
total_tool_calls: int = sum(ctx.tool_calls for ctx in self._contexts.values())
total_handoffs: int = sum(ctx.handoffs for ctx in self._contexts.values())
return {
"total_sessions": len(self._contexts),
"wrapped_agents": len(self._wrapped_agents),
"total_tool_calls": total_tool_calls,
"total_handoffs": total_handoffs,
"policy": {
"max_tool_calls": self.policy.max_tool_calls,
"max_handoffs": self.policy.max_handoffs,
"blocked_tools": self.policy.blocked_tools,
},
}
def health_check(self) -> dict[str, Any]:
"""Return adapter health status.
Returns:
A dict with ``status``, ``backend``, ``last_error``, and
``uptime_seconds`` keys.
"""
uptime: float = time.monotonic() - self._start_time
status: str = "degraded" if self._last_error else "healthy"
return {
"status": status,
"backend": "openai_agents_sdk",
"backend_connected": bool(self._wrapped_agents),
"last_error": self._last_error,
"uptime_seconds": round(uptime, 2),
}
# Convenience exports
__all__ = [
"OpenAIAgentsKernel",
"GovernancePolicy",
"ExecutionContext",
"PolicyViolationError",
]