aip-agents-binary 0.5.23__py3-none-any.whl → 0.5.25__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aip_agents/agent/base_langgraph_agent.py +6 -0
- aip_agents/agent/langgraph_react_agent.py +96 -14
- aip_agents/agent/langgraph_react_agent.pyi +6 -1
- aip_agents/guardrails/__init__.py +83 -0
- aip_agents/guardrails/__init__.pyi +6 -0
- aip_agents/guardrails/engines/__init__.py +69 -0
- aip_agents/guardrails/engines/__init__.pyi +4 -0
- aip_agents/guardrails/engines/base.py +90 -0
- aip_agents/guardrails/engines/base.pyi +61 -0
- aip_agents/guardrails/engines/nemo.py +101 -0
- aip_agents/guardrails/engines/nemo.pyi +46 -0
- aip_agents/guardrails/engines/phrase_matcher.py +113 -0
- aip_agents/guardrails/engines/phrase_matcher.pyi +48 -0
- aip_agents/guardrails/exceptions.py +39 -0
- aip_agents/guardrails/exceptions.pyi +23 -0
- aip_agents/guardrails/manager.py +163 -0
- aip_agents/guardrails/manager.pyi +42 -0
- aip_agents/guardrails/middleware.py +199 -0
- aip_agents/guardrails/middleware.pyi +87 -0
- aip_agents/guardrails/schemas.py +63 -0
- aip_agents/guardrails/schemas.pyi +43 -0
- aip_agents/guardrails/utils.py +45 -0
- aip_agents/guardrails/utils.pyi +19 -0
- aip_agents/mcp/client/persistent_session.py +6 -3
- aip_agents/middleware/base.py +8 -0
- aip_agents/middleware/base.pyi +4 -0
- aip_agents/middleware/manager.py +22 -0
- aip_agents/middleware/manager.pyi +4 -0
- {aip_agents_binary-0.5.23.dist-info → aip_agents_binary-0.5.25.dist-info}/METADATA +3 -1
- {aip_agents_binary-0.5.23.dist-info → aip_agents_binary-0.5.25.dist-info}/RECORD +32 -12
- {aip_agents_binary-0.5.23.dist-info → aip_agents_binary-0.5.25.dist-info}/WHEEL +0 -0
- {aip_agents_binary-0.5.23.dist-info → aip_agents_binary-0.5.25.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"""GuardrailMiddleware for integrating guardrails into agent execution.
|
|
2
|
+
|
|
3
|
+
This module provides GuardrailMiddleware that hooks into the agent execution
|
|
4
|
+
flow to automatically check content before and after model invocations.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Reinhart Linanda (reinhart.linanda@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import TYPE_CHECKING, Any
|
|
13
|
+
|
|
14
|
+
from langchain_core.messages import AIMessage, HumanMessage
|
|
15
|
+
|
|
16
|
+
from aip_agents.guardrails.exceptions import GuardrailViolationError
|
|
17
|
+
from aip_agents.guardrails.schemas import GuardrailInput
|
|
18
|
+
from aip_agents.middleware.base import AgentMiddleware, ModelRequest
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from aip_agents.guardrails.manager import GuardrailManager
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class GuardrailMiddleware(AgentMiddleware):
|
|
25
|
+
"""Middleware that integrates guardrails into agent execution.
|
|
26
|
+
|
|
27
|
+
This middleware wraps a GuardrailManager and automatically checks content
|
|
28
|
+
at appropriate points during agent execution:
|
|
29
|
+
|
|
30
|
+
- Before model invocation: checks user input from messages
|
|
31
|
+
- After model invocation: checks AI output from messages
|
|
32
|
+
|
|
33
|
+
If unsafe content is detected, raises GuardrailViolationError to stop execution.
|
|
34
|
+
|
|
35
|
+
Attributes:
|
|
36
|
+
guardrail_manager: The GuardrailManager to use for content checking
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, guardrail_manager: GuardrailManager) -> None:
|
|
40
|
+
"""Initialize the GuardrailMiddleware.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
guardrail_manager: GuardrailManager instance to use for checking
|
|
44
|
+
"""
|
|
45
|
+
self.guardrail_manager = guardrail_manager
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def tools(self) -> list:
|
|
49
|
+
"""Guardrails are passive filters and don't contribute tools."""
|
|
50
|
+
return []
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def system_prompt_additions(self) -> str | None:
|
|
54
|
+
"""Guardrails are passive filters and don't modify system prompts."""
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
58
|
+
"""Asynchronously check user input before model invocation.
|
|
59
|
+
|
|
60
|
+
Extracts the last user message from state and checks it with guardrails.
|
|
61
|
+
If unsafe, raises GuardrailViolationError to stop execution.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
state: Current agent state containing messages and context
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Empty dict (no state modifications needed)
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
GuardrailViolationError: If user input violates safety policies
|
|
71
|
+
"""
|
|
72
|
+
# Extract last user message from state
|
|
73
|
+
messages = state.get("messages", [])
|
|
74
|
+
user_input = self._extract_last_user_message(messages)
|
|
75
|
+
|
|
76
|
+
if user_input is not None:
|
|
77
|
+
# Check input content
|
|
78
|
+
result = await self.guardrail_manager.check_content(user_input)
|
|
79
|
+
|
|
80
|
+
if not result.is_safe:
|
|
81
|
+
raise GuardrailViolationError(result)
|
|
82
|
+
|
|
83
|
+
return {}
|
|
84
|
+
|
|
85
|
+
def before_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
86
|
+
"""Check user input before model invocation (synchronous wrapper).
|
|
87
|
+
|
|
88
|
+
Note:
|
|
89
|
+
This is a synchronous wrapper for the async `abefore_model()` method.
|
|
90
|
+
LangGraph agents primarily use `abefore_model()` in async contexts.
|
|
91
|
+
This method should rarely be called directly. If called from an async
|
|
92
|
+
context with a running event loop, it will attempt to handle it,
|
|
93
|
+
but `abefore_model()` should be preferred.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
state: Current agent state containing messages and context
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
Empty dict (no state modifications needed)
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
GuardrailViolationError: If user input violates safety policies
|
|
103
|
+
"""
|
|
104
|
+
import asyncio
|
|
105
|
+
|
|
106
|
+
user_input = self._extract_last_user_message(state.get("messages", []))
|
|
107
|
+
if user_input is None:
|
|
108
|
+
return {}
|
|
109
|
+
|
|
110
|
+
# Check if we're in an async context with a running loop
|
|
111
|
+
try:
|
|
112
|
+
loop = asyncio.get_running_loop()
|
|
113
|
+
if loop.is_running():
|
|
114
|
+
# We're in an async context with a running loop
|
|
115
|
+
# Use nest_asyncio to allow nested event loops
|
|
116
|
+
# This enables calling asyncio.run() from within a running loop
|
|
117
|
+
import nest_asyncio
|
|
118
|
+
|
|
119
|
+
nest_asyncio.apply()
|
|
120
|
+
result = asyncio.run(self.guardrail_manager.check_content(user_input))
|
|
121
|
+
else:
|
|
122
|
+
# Loop exists but not running - safe to use asyncio.run()
|
|
123
|
+
result = asyncio.run(self.guardrail_manager.check_content(user_input))
|
|
124
|
+
except RuntimeError:
|
|
125
|
+
# No running loop - safe to use asyncio.run()
|
|
126
|
+
result = asyncio.run(self.guardrail_manager.check_content(user_input))
|
|
127
|
+
|
|
128
|
+
if not result.is_safe:
|
|
129
|
+
raise GuardrailViolationError(result)
|
|
130
|
+
|
|
131
|
+
return {}
|
|
132
|
+
|
|
133
|
+
def modify_model_request(self, request: ModelRequest, state: dict[str, Any]) -> ModelRequest:
|
|
134
|
+
"""Guardrails don't modify model requests."""
|
|
135
|
+
return request
|
|
136
|
+
|
|
137
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
138
|
+
"""Asynchronously check AI output after model invocation.
|
|
139
|
+
|
|
140
|
+
Extracts the last AI message from state and checks it with guardrails.
|
|
141
|
+
If unsafe, raises GuardrailViolationError to stop execution.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
state: Current agent state after model invocation
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
Empty dict (no state modifications needed)
|
|
148
|
+
|
|
149
|
+
Raises:
|
|
150
|
+
GuardrailViolationError: If AI output violates safety policies
|
|
151
|
+
"""
|
|
152
|
+
# Extract last AI message from state
|
|
153
|
+
messages = state.get("messages", [])
|
|
154
|
+
ai_output = self._extract_last_ai_message(messages)
|
|
155
|
+
|
|
156
|
+
if ai_output is not None:
|
|
157
|
+
# Check output content
|
|
158
|
+
result = await self.guardrail_manager.check_content(GuardrailInput(input=None, output=ai_output))
|
|
159
|
+
|
|
160
|
+
if not result.is_safe:
|
|
161
|
+
raise GuardrailViolationError(result)
|
|
162
|
+
|
|
163
|
+
return {}
|
|
164
|
+
|
|
165
|
+
def after_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
166
|
+
"""Check AI output after model invocation (synchronous wrapper)."""
|
|
167
|
+
return {}
|
|
168
|
+
|
|
169
|
+
def _extract_last_user_message(self, messages: list) -> str | None:
|
|
170
|
+
"""Extract the last user message from a list of messages.
|
|
171
|
+
|
|
172
|
+
Searches backwards through messages to find the most recent HumanMessage.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
messages: List of message objects
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
Content of the last user message, or None if not found
|
|
179
|
+
"""
|
|
180
|
+
for message in reversed(messages):
|
|
181
|
+
if isinstance(message, HumanMessage) and message.content:
|
|
182
|
+
return str(message.content)
|
|
183
|
+
return None
|
|
184
|
+
|
|
185
|
+
def _extract_last_ai_message(self, messages: list) -> str | None:
|
|
186
|
+
"""Extract the last AI message from a list of messages.
|
|
187
|
+
|
|
188
|
+
Searches backwards through messages to find the most recent AIMessage.
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
messages: List of message objects
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
Content of the last AI message, or None if not found
|
|
195
|
+
"""
|
|
196
|
+
for message in reversed(messages):
|
|
197
|
+
if isinstance(message, AIMessage) and message.content:
|
|
198
|
+
return str(message.content)
|
|
199
|
+
return None
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from aip_agents.guardrails.exceptions import GuardrailViolationError as GuardrailViolationError
|
|
3
|
+
from aip_agents.guardrails.manager import GuardrailManager as GuardrailManager
|
|
4
|
+
from aip_agents.guardrails.schemas import GuardrailInput as GuardrailInput
|
|
5
|
+
from aip_agents.middleware.base import AgentMiddleware as AgentMiddleware, ModelRequest as ModelRequest
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
class GuardrailMiddleware(AgentMiddleware):
|
|
9
|
+
"""Middleware that integrates guardrails into agent execution.
|
|
10
|
+
|
|
11
|
+
This middleware wraps a GuardrailManager and automatically checks content
|
|
12
|
+
at appropriate points during agent execution:
|
|
13
|
+
|
|
14
|
+
- Before model invocation: checks user input from messages
|
|
15
|
+
- After model invocation: checks AI output from messages
|
|
16
|
+
|
|
17
|
+
If unsafe content is detected, raises GuardrailViolationError to stop execution.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
guardrail_manager: The GuardrailManager to use for content checking
|
|
21
|
+
"""
|
|
22
|
+
guardrail_manager: Incomplete
|
|
23
|
+
def __init__(self, guardrail_manager: GuardrailManager) -> None:
|
|
24
|
+
"""Initialize the GuardrailMiddleware.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
guardrail_manager: GuardrailManager instance to use for checking
|
|
28
|
+
"""
|
|
29
|
+
@property
|
|
30
|
+
def tools(self) -> list:
|
|
31
|
+
"""Guardrails are passive filters and don't contribute tools."""
|
|
32
|
+
@property
|
|
33
|
+
def system_prompt_additions(self) -> str | None:
|
|
34
|
+
"""Guardrails are passive filters and don't modify system prompts."""
|
|
35
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
36
|
+
"""Asynchronously check user input before model invocation.
|
|
37
|
+
|
|
38
|
+
Extracts the last user message from state and checks it with guardrails.
|
|
39
|
+
If unsafe, raises GuardrailViolationError to stop execution.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
state: Current agent state containing messages and context
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Empty dict (no state modifications needed)
|
|
46
|
+
|
|
47
|
+
Raises:
|
|
48
|
+
GuardrailViolationError: If user input violates safety policies
|
|
49
|
+
"""
|
|
50
|
+
def before_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
51
|
+
"""Check user input before model invocation (synchronous wrapper).
|
|
52
|
+
|
|
53
|
+
Note:
|
|
54
|
+
This is a synchronous wrapper for the async `abefore_model()` method.
|
|
55
|
+
LangGraph agents primarily use `abefore_model()` in async contexts.
|
|
56
|
+
This method should rarely be called directly. If called from an async
|
|
57
|
+
context with a running event loop, it will attempt to handle it,
|
|
58
|
+
but `abefore_model()` should be preferred.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
state: Current agent state containing messages and context
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
Empty dict (no state modifications needed)
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
GuardrailViolationError: If user input violates safety policies
|
|
68
|
+
"""
|
|
69
|
+
def modify_model_request(self, request: ModelRequest, state: dict[str, Any]) -> ModelRequest:
|
|
70
|
+
"""Guardrails don't modify model requests."""
|
|
71
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
72
|
+
"""Asynchronously check AI output after model invocation.
|
|
73
|
+
|
|
74
|
+
Extracts the last AI message from state and checks it with guardrails.
|
|
75
|
+
If unsafe, raises GuardrailViolationError to stop execution.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
state: Current agent state after model invocation
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
Empty dict (no state modifications needed)
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
GuardrailViolationError: If AI output violates safety policies
|
|
85
|
+
"""
|
|
86
|
+
def after_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
87
|
+
"""Check AI output after model invocation (synchronous wrapper)."""
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Schemas for guardrail input, output, and configuration.
|
|
2
|
+
|
|
3
|
+
This module defines the data structures used throughout the guardrails system,
|
|
4
|
+
including input/output schemas and configuration objects.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Reinhart Linanda (reinhart.linanda@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from enum import StrEnum
|
|
11
|
+
|
|
12
|
+
from pydantic import BaseModel, ConfigDict
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class GuardrailMode(StrEnum):
|
|
16
|
+
"""Modes determining what content an engine checks."""
|
|
17
|
+
|
|
18
|
+
INPUT_ONLY = "input_only"
|
|
19
|
+
OUTPUT_ONLY = "output_only"
|
|
20
|
+
INPUT_OUTPUT = "input_output"
|
|
21
|
+
DISABLED = "disabled"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class GuardrailInput(BaseModel):
|
|
25
|
+
"""Input schema for guardrail checks.
|
|
26
|
+
|
|
27
|
+
Attributes:
|
|
28
|
+
input: User input content to check (queries, prompts, context)
|
|
29
|
+
output: AI output content to check (responses, generated text)
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
model_config = ConfigDict(extra="forbid")
|
|
33
|
+
|
|
34
|
+
input: str | None = None
|
|
35
|
+
output: str | None = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class GuardrailResult(BaseModel):
|
|
39
|
+
"""Result schema returned by guardrail engines and managers.
|
|
40
|
+
|
|
41
|
+
Attributes:
|
|
42
|
+
is_safe: Whether the content passed all checks
|
|
43
|
+
reason: Explanation when content is blocked (None if safe)
|
|
44
|
+
filtered_content: Cleaned/sanitized content if engine provides it
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
model_config = ConfigDict(extra="forbid")
|
|
48
|
+
|
|
49
|
+
is_safe: bool
|
|
50
|
+
reason: str | None = None
|
|
51
|
+
filtered_content: str | None = None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class BaseGuardrailEngineConfig(BaseModel):
|
|
55
|
+
"""Base configuration for guardrail engines.
|
|
56
|
+
|
|
57
|
+
Attributes:
|
|
58
|
+
guardrail_mode: What content this engine should check
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
model_config = ConfigDict(extra="forbid")
|
|
62
|
+
|
|
63
|
+
guardrail_mode: GuardrailMode = GuardrailMode.INPUT_OUTPUT
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from enum import StrEnum
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
class GuardrailMode(StrEnum):
|
|
6
|
+
"""Modes determining what content an engine checks."""
|
|
7
|
+
INPUT_ONLY: str
|
|
8
|
+
OUTPUT_ONLY: str
|
|
9
|
+
INPUT_OUTPUT: str
|
|
10
|
+
DISABLED: str
|
|
11
|
+
|
|
12
|
+
class GuardrailInput(BaseModel):
|
|
13
|
+
"""Input schema for guardrail checks.
|
|
14
|
+
|
|
15
|
+
Attributes:
|
|
16
|
+
input: User input content to check (queries, prompts, context)
|
|
17
|
+
output: AI output content to check (responses, generated text)
|
|
18
|
+
"""
|
|
19
|
+
model_config: Incomplete
|
|
20
|
+
input: str | None
|
|
21
|
+
output: str | None
|
|
22
|
+
|
|
23
|
+
class GuardrailResult(BaseModel):
|
|
24
|
+
"""Result schema returned by guardrail engines and managers.
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
is_safe: Whether the content passed all checks
|
|
28
|
+
reason: Explanation when content is blocked (None if safe)
|
|
29
|
+
filtered_content: Cleaned/sanitized content if engine provides it
|
|
30
|
+
"""
|
|
31
|
+
model_config: Incomplete
|
|
32
|
+
is_safe: bool
|
|
33
|
+
reason: str | None
|
|
34
|
+
filtered_content: str | None
|
|
35
|
+
|
|
36
|
+
class BaseGuardrailEngineConfig(BaseModel):
|
|
37
|
+
"""Base configuration for guardrail engines.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
guardrail_mode: What content this engine should check
|
|
41
|
+
"""
|
|
42
|
+
model_config: Incomplete
|
|
43
|
+
guardrail_mode: GuardrailMode
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Utility functions for guardrail mode conversion.
|
|
2
|
+
|
|
3
|
+
This module provides utilities for converting between aip-agents GuardrailMode
|
|
4
|
+
and gllm-guardrail GuardrailMode enums.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Reinhart Linanda (reinhart.linanda@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from aip_agents.guardrails.schemas import GuardrailMode
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def convert_guardrail_mode_to_gl_sdk(mode: GuardrailMode) -> Any:
|
|
16
|
+
"""Convert aip-agents GuardrailMode to gllm-guardrail GuardrailMode.
|
|
17
|
+
|
|
18
|
+
This function performs lazy import of gllm-guardrail to support optional
|
|
19
|
+
dependencies. The conversion is necessary because we maintain our own
|
|
20
|
+
GuardrailMode enum for API consistency while wrapping the external library.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
mode: The aip-agents GuardrailMode to convert
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
The corresponding gllm-guardrail GuardrailMode enum value
|
|
27
|
+
|
|
28
|
+
Raises:
|
|
29
|
+
ImportError: If gllm-guardrail is not installed
|
|
30
|
+
"""
|
|
31
|
+
try:
|
|
32
|
+
from gllm_guardrail.constants import GuardrailMode as GLGuardrailMode # pragma: no cover
|
|
33
|
+
except ImportError as e: # pragma: no cover
|
|
34
|
+
raise ImportError(
|
|
35
|
+
"gllm-guardrail is required for guardrails. Install with: pip install 'aip-agents[guardrails]'"
|
|
36
|
+
) from e # pragma: no cover
|
|
37
|
+
|
|
38
|
+
mode_mapping = {
|
|
39
|
+
GuardrailMode.INPUT_ONLY: GLGuardrailMode.INPUT_ONLY,
|
|
40
|
+
GuardrailMode.OUTPUT_ONLY: GLGuardrailMode.OUTPUT_ONLY,
|
|
41
|
+
GuardrailMode.INPUT_OUTPUT: GLGuardrailMode.BOTH,
|
|
42
|
+
GuardrailMode.DISABLED: GLGuardrailMode.DISABLED,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return mode_mapping[mode]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from aip_agents.guardrails.schemas import GuardrailMode as GuardrailMode
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
def convert_guardrail_mode_to_gl_sdk(mode: GuardrailMode) -> Any:
|
|
5
|
+
"""Convert aip-agents GuardrailMode to gllm-guardrail GuardrailMode.
|
|
6
|
+
|
|
7
|
+
This function performs lazy import of gllm-guardrail to support optional
|
|
8
|
+
dependencies. The conversion is necessary because we maintain our own
|
|
9
|
+
GuardrailMode enum for API consistency while wrapping the external library.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
mode: The aip-agents GuardrailMode to convert
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
The corresponding gllm-guardrail GuardrailMode enum value
|
|
16
|
+
|
|
17
|
+
Raises:
|
|
18
|
+
ImportError: If gllm-guardrail is not installed
|
|
19
|
+
"""
|
|
@@ -115,9 +115,12 @@ class PersistentMCPSession:
|
|
|
115
115
|
|
|
116
116
|
# Discover resources (for future use)
|
|
117
117
|
if result.capabilities.resources:
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
try:
|
|
119
|
+
resources_result = await self.client_session.list_resources()
|
|
120
|
+
if resources_result and resources_result.resources:
|
|
121
|
+
logger.debug(f"Found {len(resources_result.resources)} resources for {self.server_name}")
|
|
122
|
+
except Exception:
|
|
123
|
+
logger.debug(f"Could not list resources for {self.server_name}, skipping")
|
|
121
124
|
|
|
122
125
|
self._initialized = True
|
|
123
126
|
logger.info(f"Session initialization complete for {self.server_name}")
|
aip_agents/middleware/base.py
CHANGED
|
@@ -86,3 +86,11 @@ class AgentMiddleware(Protocol):
|
|
|
86
86
|
if no updates are needed.
|
|
87
87
|
"""
|
|
88
88
|
return {} # pragma: no cover # Protocol default - cannot be executed directly
|
|
89
|
+
|
|
90
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
91
|
+
"""Asynchronous version of before_model hook."""
|
|
92
|
+
return self.before_model(state)
|
|
93
|
+
|
|
94
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
95
|
+
"""Asynchronous version of after_model hook."""
|
|
96
|
+
return self.after_model(state)
|
aip_agents/middleware/base.pyi
CHANGED
|
@@ -69,3 +69,7 @@ class AgentMiddleware(Protocol):
|
|
|
69
69
|
Dict of state updates to merge into the agent state. Return empty dict
|
|
70
70
|
if no updates are needed.
|
|
71
71
|
"""
|
|
72
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
73
|
+
"""Asynchronous version of before_model hook."""
|
|
74
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
75
|
+
"""Asynchronous version of after_model hook."""
|
aip_agents/middleware/manager.py
CHANGED
|
@@ -126,3 +126,25 @@ class MiddlewareManager:
|
|
|
126
126
|
updates.update(mw_updates)
|
|
127
127
|
|
|
128
128
|
return updates
|
|
129
|
+
|
|
130
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
131
|
+
"""Asynchronously execute before_model hooks for all middleware."""
|
|
132
|
+
state_updates: dict[str, Any] = {}
|
|
133
|
+
|
|
134
|
+
for mw in self.middleware:
|
|
135
|
+
mw_updates = await mw.abefore_model(state)
|
|
136
|
+
if mw_updates:
|
|
137
|
+
state_updates.update(mw_updates)
|
|
138
|
+
|
|
139
|
+
return state_updates
|
|
140
|
+
|
|
141
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
142
|
+
"""Asynchronously execute after_model hooks for all middleware in reverse order."""
|
|
143
|
+
updates: dict[str, Any] = {}
|
|
144
|
+
|
|
145
|
+
for mw in reversed(self.middleware):
|
|
146
|
+
mw_updates = await mw.aafter_model(state)
|
|
147
|
+
if mw_updates:
|
|
148
|
+
updates.update(mw_updates)
|
|
149
|
+
|
|
150
|
+
return updates
|
|
@@ -78,3 +78,7 @@ class MiddlewareManager:
|
|
|
78
78
|
Merged dictionary of all state updates from all middleware.
|
|
79
79
|
Updates are accumulated in reverse order of execution.
|
|
80
80
|
"""
|
|
81
|
+
async def abefore_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
82
|
+
"""Asynchronously execute before_model hooks for all middleware."""
|
|
83
|
+
async def aafter_model(self, state: dict[str, Any]) -> dict[str, Any]:
|
|
84
|
+
"""Asynchronously execute after_model hooks for all middleware in reverse order."""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aip-agents-binary
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.25
|
|
4
4
|
Summary: A library for managing agents in Gen AI applications.
|
|
5
5
|
Author-email: Raymond Christopher <raymond.christopher@gdplabs.id>
|
|
6
6
|
Requires-Python: <3.13,>=3.11
|
|
@@ -31,6 +31,8 @@ Provides-Extra: memory
|
|
|
31
31
|
Requires-Dist: gllm-memory-binary[mem0ai]<0.2.0,>=0.1.1; extra == "memory"
|
|
32
32
|
Provides-Extra: privacy
|
|
33
33
|
Requires-Dist: gllm-privacy-binary<0.5.0,>=0.4.12; extra == "privacy"
|
|
34
|
+
Provides-Extra: guardrails
|
|
35
|
+
Requires-Dist: gllm-guardrail-binary<0.1.0,>=0.0.1; extra == "guardrails"
|
|
34
36
|
Provides-Extra: gl-connector
|
|
35
37
|
Requires-Dist: bosa-connectors-binary<0.4.0,>=0.3.1; extra == "gl-connector"
|
|
36
38
|
Provides-Extra: local
|
|
@@ -20,7 +20,7 @@ aip_agents/agent/__init__.py,sha256=noyxX-rddnulFTp9CLOPmlpsysteku3Q2r_nBbfECvc,
|
|
|
20
20
|
aip_agents/agent/__init__.pyi,sha256=N-XcIaJOUMl6-ZVEHlZNN6PcahotUSeLRYMWxC2Juj0,864
|
|
21
21
|
aip_agents/agent/base_agent.py,sha256=-8TyVGRUqPBwREoDoU14UbSJ3qdtfQ_a6VxdU8P4BCM,40639
|
|
22
22
|
aip_agents/agent/base_agent.pyi,sha256=piwAfkyD4Zw1IESdrx4lIrR8oBABrYai88EzabKTtZ0,11339
|
|
23
|
-
aip_agents/agent/base_langgraph_agent.py,sha256=
|
|
23
|
+
aip_agents/agent/base_langgraph_agent.py,sha256=XqsSQtxHCiAqQoi5ib8RW00BbpeujNONEAdkqQQYLV4,122974
|
|
24
24
|
aip_agents/agent/base_langgraph_agent.pyi,sha256=vfRtRAHT7Hb4Pq2uprmFUgQcsK_Tac2fv1Xoht5GPiU,11801
|
|
25
25
|
aip_agents/agent/google_adk_agent.py,sha256=8zNLEnWlvwh0qxY65bZKuxBipsTFT-sbmb6zD12cMDA,38062
|
|
26
26
|
aip_agents/agent/google_adk_agent.pyi,sha256=O7DDlLAHLFBLpBKt1wMIqWX9GqI-_x2mGpkIACyXqf4,6389
|
|
@@ -34,8 +34,8 @@ aip_agents/agent/langflow_agent.py,sha256=3KjB4ZBAIAqPKK5pML3DuiQ_yixx2L4dTQSW2p
|
|
|
34
34
|
aip_agents/agent/langflow_agent.pyi,sha256=HOzMWIkga8F_ZFrzT62JdX9vhh8JhQVg-ylrr2Tglyo,5800
|
|
35
35
|
aip_agents/agent/langgraph_memory_enhancer_agent.py,sha256=1AvvqvFfeLf80Jw2W2Mm7TdpwO3o_OC7r-30M4EIWeU,17859
|
|
36
36
|
aip_agents/agent/langgraph_memory_enhancer_agent.pyi,sha256=6n6cd3udUhSaVTxqulnjCCgR-eUROnBEW2lFkAQCBQ4,2644
|
|
37
|
-
aip_agents/agent/langgraph_react_agent.py,sha256=
|
|
38
|
-
aip_agents/agent/langgraph_react_agent.pyi,sha256=
|
|
37
|
+
aip_agents/agent/langgraph_react_agent.py,sha256=tp5luLsB9_Of00yNidcX6Q9QWbZFwZcVuzYC6ck3Egg,108894
|
|
38
|
+
aip_agents/agent/langgraph_react_agent.pyi,sha256=bVzM5w98biRwV6GTzaIeKu_rwIQ8d-q7EugZk9N-0vQ,8487
|
|
39
39
|
aip_agents/agent/system_instruction_context.py,sha256=6lQAJ6DJv1aF8wjalxdsNWqEDZmomHwnhnPikLCfzj4,1220
|
|
40
40
|
aip_agents/agent/system_instruction_context.pyi,sha256=gpYUr_CjWU_qVFobX_sduKDQN_wwtRL9BdnYv0WZ0Mc,424
|
|
41
41
|
aip_agents/agent/hitl/__init__.py,sha256=qyxPIfSJ08MrvkE1qBZxdiom4VxSJSGEkrjqUZY2kNg,850
|
|
@@ -280,6 +280,26 @@ aip_agents/examples/tools/weather_forecast_tool.py,sha256=woFXShDC3vw2lzW6Yu3jGV
|
|
|
280
280
|
aip_agents/examples/tools/weather_forecast_tool.pyi,sha256=UR5XFHI7yWpSJ-TjLfXdy0P-DsfHRvdXJwWKBSMYoxk,389
|
|
281
281
|
aip_agents/executor/agent_executor.py,sha256=glEhBLj12lkfJZ0BkWim6DM0hwnrt4XbLgjeBv_h-Sc,18735
|
|
282
282
|
aip_agents/executor/base.py,sha256=8DJpBjMeyA5MrgSdPnIkDSEbxDSK8PHsDstuRKxP3Kw,1584
|
|
283
|
+
aip_agents/guardrails/__init__.py,sha256=4MIklFY6cHH3ePgAAQ0g8sqZRlIW0qjmHL0rIS1DkS8,2789
|
|
284
|
+
aip_agents/guardrails/__init__.pyi,sha256=IfiFF1riNwswSiupAebschpsbw73HDMRi5KChDOqBa8,633
|
|
285
|
+
aip_agents/guardrails/exceptions.py,sha256=AkJ_RxrL6fvCflpktk-VH50l5QrmAJigzI5cT96H4Vw,1309
|
|
286
|
+
aip_agents/guardrails/exceptions.pyi,sha256=_v3Ma6fA7wOSvwN-LyIcms4VPwiUQWdB7m_gEc8hobk,882
|
|
287
|
+
aip_agents/guardrails/manager.py,sha256=pNSUrO1jILSic7WoV254y_VhE8ezbAN3u7iqftr2IfU,5750
|
|
288
|
+
aip_agents/guardrails/manager.pyi,sha256=5AdR21UkEAD7JPVfkIDsll5_py8iTCStPeySc6buNUQ,1812
|
|
289
|
+
aip_agents/guardrails/middleware.py,sha256=IewMtyEzdd0fY2jdKUAQbNRTWd_6rC_ywqYJhV0ibEM,7356
|
|
290
|
+
aip_agents/guardrails/middleware.pyi,sha256=77OsASwBkoB9dZMqZ3K-mMYzqTmHcwYAQnBENL939N0,3763
|
|
291
|
+
aip_agents/guardrails/schemas.py,sha256=SmMUw8bsDQDNVSqgwTaqByA2HKpMwa3d9eX52Ya9fGw,1711
|
|
292
|
+
aip_agents/guardrails/schemas.pyi,sha256=Yqjis0PbZLy53VkLFYFCwWvJLhTwQEewLJeUM_Ki8Co,1288
|
|
293
|
+
aip_agents/guardrails/utils.py,sha256=Rp9zwLWUydvZmZE4xvAXhIdN05LX-aTkPqWRaQb7-YE,1587
|
|
294
|
+
aip_agents/guardrails/utils.pyi,sha256=b7-XBFiv7QS0NG5AGvExmglp4UQJGpq-972dtgyMbhE,709
|
|
295
|
+
aip_agents/guardrails/engines/__init__.py,sha256=j-H1-rOKe-R-WHyq1tjAi0usEUA7UjhLCwywhom-mo0,2239
|
|
296
|
+
aip_agents/guardrails/engines/__init__.pyi,sha256=W7UjIO5tMaH8PbHs0AIKGV6A2OBNXYJPXsM_xhiQfw4,252
|
|
297
|
+
aip_agents/guardrails/engines/base.py,sha256=wD-VW962WsqDMVEpLZdaKi0HNls3HcUX7pA7nQFRHJ8,2713
|
|
298
|
+
aip_agents/guardrails/engines/base.pyi,sha256=tTxR1zOL-YMEdWkXkU_Pj9y6gshR7mrcyHdjedkK41Y,2297
|
|
299
|
+
aip_agents/guardrails/engines/nemo.py,sha256=1LtevUmghKbHmoj1wJKXVhPZd4ETRXD-X7DJo7xSiZw,3902
|
|
300
|
+
aip_agents/guardrails/engines/nemo.pyi,sha256=Jh5Yma4uOXGrmJO_cshCKVkPd4wjXSmV-XxNuj-pjxc,2027
|
|
301
|
+
aip_agents/guardrails/engines/phrase_matcher.py,sha256=PoY7mQVWdlBxLCldIsdBwFtKr5P1PIdYlRe9q4Fkh9Y,4076
|
|
302
|
+
aip_agents/guardrails/engines/phrase_matcher.pyi,sha256=idJLcLT4R8ZY0jsU55ECajFURLu5hYntSoaQJma29nA,2108
|
|
283
303
|
aip_agents/mcp/__init__.py,sha256=n9E7iz5diKd_yKkPZfATHkfKy0Hdm40NYjDMWPmWImU,61
|
|
284
304
|
aip_agents/mcp/__init__.pyi,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
285
305
|
aip_agents/mcp/client/__init__.py,sha256=Kp0iOfmk2Vdo9wwKu3plcxmJT6HlWvpXiTm9MTXaODU,507
|
|
@@ -288,7 +308,7 @@ aip_agents/mcp/client/base_mcp_client.py,sha256=0S10MIi5rSlL350b9MiNLVPe8Tj3ytaG
|
|
|
288
308
|
aip_agents/mcp/client/base_mcp_client.pyi,sha256=LWnmN1IH1REdL0EebZN00gfZsico8jj0WDAI5vsJYEk,6029
|
|
289
309
|
aip_agents/mcp/client/connection_manager.py,sha256=MwhJF5yyzQFWSkVPGnQ1OcMr0GTnBQFr5A4pU4zCNuU,7680
|
|
290
310
|
aip_agents/mcp/client/connection_manager.pyi,sha256=CO190fuGjRxYLVK79ZJbtNhFiPqlQyv31RJvtjmqeqQ,1820
|
|
291
|
-
aip_agents/mcp/client/persistent_session.py,sha256=
|
|
311
|
+
aip_agents/mcp/client/persistent_session.py,sha256=yC9PhBsPqDO27-yN3EUxS5Hri33hqwa9h9h_9hpQUF4,14773
|
|
292
312
|
aip_agents/mcp/client/persistent_session.pyi,sha256=eMqoGE84JJtDDwiJJSNIsjwWrldB9luaVA7UeXlBGrc,4021
|
|
293
313
|
aip_agents/mcp/client/session_pool.py,sha256=qYxtalGyT1Z4a5LIDe2X9QinduOz4f_U9yDzBH_qjKI,12968
|
|
294
314
|
aip_agents/mcp/client/session_pool.pyi,sha256=dUXl8g4G4asZ1CplaeZaR24suyU4gAgKzvQNOlriQA0,3680
|
|
@@ -326,10 +346,10 @@ aip_agents/memory/adapters/mem0.py,sha256=eGVPvqF2TFx-_gmxpfC08n5FZZW_BDKIKOwCO4
|
|
|
326
346
|
aip_agents/memory/adapters/mem0.pyi,sha256=p82Xg3yG1jr_ogfY5D6cH93uR-EjigBk-TDl4Ba-okM,1115
|
|
327
347
|
aip_agents/middleware/__init__.py,sha256=VqqQmOQKyYwV9bUNTB2ZN-_IpBRevQaWp4Q2sTe2LHA,502
|
|
328
348
|
aip_agents/middleware/__init__.pyi,sha256=hSSkK1QpAGwCs9c-hVzWDh_R_xi3mh7mUVfzQBJwdS4,404
|
|
329
|
-
aip_agents/middleware/base.py,sha256=
|
|
330
|
-
aip_agents/middleware/base.pyi,sha256=
|
|
331
|
-
aip_agents/middleware/manager.py,sha256=
|
|
332
|
-
aip_agents/middleware/manager.pyi,sha256=
|
|
349
|
+
aip_agents/middleware/base.py,sha256=7PW5FEBbRDUdvd56yNTVuQDxJrX-ymBjxUPUePIRH4U,3711
|
|
350
|
+
aip_agents/middleware/base.pyi,sha256=1f4pKaxq9dDZgLcDr1G93KXmWoRfsezXuMkVluXXwWY,3062
|
|
351
|
+
aip_agents/middleware/manager.py,sha256=cTSzJN-3ty2o7x7iS1xd__hdBsQiq2Rqb5bnLPvWr4Y,5327
|
|
352
|
+
aip_agents/middleware/manager.pyi,sha256=UbQ-5sehP7G0sokwxCSBe4q8JLub7acFrmr0P-z65vE,3661
|
|
333
353
|
aip_agents/middleware/todolist.py,sha256=YxE1kcaP6Ov1WnfxM5z8cv5cNsJViG0NRPpbAGfpeMQ,10788
|
|
334
354
|
aip_agents/middleware/todolist.pyi,sha256=nkRSDplMiQPS0qhNrMoDB1ksOtarEclaGErvIZH7QQ4,4320
|
|
335
355
|
aip_agents/schema/__init__.py,sha256=Pxqd6kvSJkKHpmkBjy3N9ncxK7aEyYFf60STfNuHgkQ,1767
|
|
@@ -540,7 +560,7 @@ aip_agents/utils/pii/pii_helper.py,sha256=8QGVC9lb7dic_vSfLDUaDvqm45BUbYyPeQTVva
|
|
|
540
560
|
aip_agents/utils/pii/pii_helper.pyi,sha256=wEgOasJxwKObtQ9Cb1UsiyIaqq2JUYmdRwOZR9PnIjA,3017
|
|
541
561
|
aip_agents/utils/pii/uuid_deanonymizer_mapping.py,sha256=X9zeX1bhb3rlCc8P5QnbHCILx2AIhGmZwjsjh_2G4ZQ,7543
|
|
542
562
|
aip_agents/utils/pii/uuid_deanonymizer_mapping.pyi,sha256=6H1xRV2Nr0LpP5K6fbz2uCobehTpM2626v8kiOd9W9Y,3157
|
|
543
|
-
aip_agents_binary-0.5.
|
|
544
|
-
aip_agents_binary-0.5.
|
|
545
|
-
aip_agents_binary-0.5.
|
|
546
|
-
aip_agents_binary-0.5.
|
|
563
|
+
aip_agents_binary-0.5.25.dist-info/METADATA,sha256=lSp9fH3seSvB9hXwjbf_zOMe44-tc3KkZsuZ2W9Jx3E,22998
|
|
564
|
+
aip_agents_binary-0.5.25.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
565
|
+
aip_agents_binary-0.5.25.dist-info/top_level.txt,sha256=PEz8vcwC1bH4UrkhF0LkIYCNfXGWZUHdSklbvkBe25E,11
|
|
566
|
+
aip_agents_binary-0.5.25.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|