aip-agents-binary 0.5.21__py3-none-macosx_13_0_arm64.whl → 0.6.8__py3-none-macosx_13_0_arm64.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.
Files changed (149) hide show
  1. aip_agents/agent/__init__.py +44 -4
  2. aip_agents/agent/base_langgraph_agent.py +169 -74
  3. aip_agents/agent/base_langgraph_agent.pyi +3 -2
  4. aip_agents/agent/langgraph_memory_enhancer_agent.py +368 -34
  5. aip_agents/agent/langgraph_memory_enhancer_agent.pyi +3 -2
  6. aip_agents/agent/langgraph_react_agent.py +424 -35
  7. aip_agents/agent/langgraph_react_agent.pyi +46 -2
  8. aip_agents/examples/{hello_world_langgraph_bosa_twitter.py → hello_world_langgraph_gl_connector_twitter.py} +10 -7
  9. aip_agents/examples/hello_world_langgraph_gl_connector_twitter.pyi +5 -0
  10. aip_agents/examples/hello_world_ptc.py +49 -0
  11. aip_agents/examples/hello_world_ptc.pyi +5 -0
  12. aip_agents/examples/hello_world_ptc_custom_tools.py +83 -0
  13. aip_agents/examples/hello_world_ptc_custom_tools.pyi +7 -0
  14. aip_agents/examples/hello_world_sentry.py +2 -2
  15. aip_agents/examples/hello_world_tool_output_client.py +9 -0
  16. aip_agents/examples/tools/multiply_tool.py +43 -0
  17. aip_agents/examples/tools/multiply_tool.pyi +18 -0
  18. aip_agents/guardrails/__init__.py +83 -0
  19. aip_agents/guardrails/__init__.pyi +6 -0
  20. aip_agents/guardrails/engines/__init__.py +69 -0
  21. aip_agents/guardrails/engines/__init__.pyi +4 -0
  22. aip_agents/guardrails/engines/base.py +90 -0
  23. aip_agents/guardrails/engines/base.pyi +61 -0
  24. aip_agents/guardrails/engines/nemo.py +101 -0
  25. aip_agents/guardrails/engines/nemo.pyi +46 -0
  26. aip_agents/guardrails/engines/phrase_matcher.py +113 -0
  27. aip_agents/guardrails/engines/phrase_matcher.pyi +48 -0
  28. aip_agents/guardrails/exceptions.py +39 -0
  29. aip_agents/guardrails/exceptions.pyi +23 -0
  30. aip_agents/guardrails/manager.py +163 -0
  31. aip_agents/guardrails/manager.pyi +42 -0
  32. aip_agents/guardrails/middleware.py +199 -0
  33. aip_agents/guardrails/middleware.pyi +87 -0
  34. aip_agents/guardrails/schemas.py +63 -0
  35. aip_agents/guardrails/schemas.pyi +43 -0
  36. aip_agents/guardrails/utils.py +45 -0
  37. aip_agents/guardrails/utils.pyi +19 -0
  38. aip_agents/mcp/client/__init__.py +38 -2
  39. aip_agents/mcp/client/connection_manager.py +36 -1
  40. aip_agents/mcp/client/connection_manager.pyi +3 -0
  41. aip_agents/mcp/client/persistent_session.py +318 -65
  42. aip_agents/mcp/client/persistent_session.pyi +9 -0
  43. aip_agents/mcp/client/transports.py +52 -4
  44. aip_agents/mcp/client/transports.pyi +9 -0
  45. aip_agents/memory/adapters/base_adapter.py +98 -0
  46. aip_agents/memory/adapters/base_adapter.pyi +25 -0
  47. aip_agents/middleware/base.py +8 -0
  48. aip_agents/middleware/base.pyi +4 -0
  49. aip_agents/middleware/manager.py +22 -0
  50. aip_agents/middleware/manager.pyi +4 -0
  51. aip_agents/ptc/__init__.py +87 -0
  52. aip_agents/ptc/__init__.pyi +14 -0
  53. aip_agents/ptc/custom_tools.py +473 -0
  54. aip_agents/ptc/custom_tools.pyi +184 -0
  55. aip_agents/ptc/custom_tools_payload.py +400 -0
  56. aip_agents/ptc/custom_tools_payload.pyi +31 -0
  57. aip_agents/ptc/custom_tools_templates/__init__.py +1 -0
  58. aip_agents/ptc/custom_tools_templates/__init__.pyi +0 -0
  59. aip_agents/ptc/custom_tools_templates/custom_build_function.py.template +23 -0
  60. aip_agents/ptc/custom_tools_templates/custom_init.py.template +15 -0
  61. aip_agents/ptc/custom_tools_templates/custom_invoke.py.template +60 -0
  62. aip_agents/ptc/custom_tools_templates/custom_registry.py.template +87 -0
  63. aip_agents/ptc/custom_tools_templates/custom_sources_init.py.template +7 -0
  64. aip_agents/ptc/custom_tools_templates/custom_wrapper.py.template +19 -0
  65. aip_agents/ptc/doc_gen.py +122 -0
  66. aip_agents/ptc/doc_gen.pyi +40 -0
  67. aip_agents/ptc/exceptions.py +57 -0
  68. aip_agents/ptc/exceptions.pyi +37 -0
  69. aip_agents/ptc/executor.py +261 -0
  70. aip_agents/ptc/executor.pyi +99 -0
  71. aip_agents/ptc/mcp/__init__.py +45 -0
  72. aip_agents/ptc/mcp/__init__.pyi +7 -0
  73. aip_agents/ptc/mcp/sandbox_bridge.py +668 -0
  74. aip_agents/ptc/mcp/sandbox_bridge.pyi +47 -0
  75. aip_agents/ptc/mcp/templates/__init__.py +1 -0
  76. aip_agents/ptc/mcp/templates/__init__.pyi +0 -0
  77. aip_agents/ptc/mcp/templates/mcp_client.py.template +239 -0
  78. aip_agents/ptc/naming.py +196 -0
  79. aip_agents/ptc/naming.pyi +85 -0
  80. aip_agents/ptc/payload.py +26 -0
  81. aip_agents/ptc/payload.pyi +15 -0
  82. aip_agents/ptc/prompt_builder.py +673 -0
  83. aip_agents/ptc/prompt_builder.pyi +59 -0
  84. aip_agents/ptc/ptc_helper.py +16 -0
  85. aip_agents/ptc/ptc_helper.pyi +1 -0
  86. aip_agents/ptc/sandbox_bridge.py +256 -0
  87. aip_agents/ptc/sandbox_bridge.pyi +38 -0
  88. aip_agents/ptc/template_utils.py +33 -0
  89. aip_agents/ptc/template_utils.pyi +13 -0
  90. aip_agents/ptc/templates/__init__.py +1 -0
  91. aip_agents/ptc/templates/__init__.pyi +0 -0
  92. aip_agents/ptc/templates/ptc_helper.py.template +134 -0
  93. aip_agents/ptc/tool_def_helpers.py +101 -0
  94. aip_agents/ptc/tool_def_helpers.pyi +38 -0
  95. aip_agents/ptc/tool_enrichment.py +163 -0
  96. aip_agents/ptc/tool_enrichment.pyi +60 -0
  97. aip_agents/sandbox/__init__.py +43 -0
  98. aip_agents/sandbox/__init__.pyi +5 -0
  99. aip_agents/sandbox/defaults.py +205 -0
  100. aip_agents/sandbox/defaults.pyi +30 -0
  101. aip_agents/sandbox/e2b_runtime.py +295 -0
  102. aip_agents/sandbox/e2b_runtime.pyi +57 -0
  103. aip_agents/sandbox/template_builder.py +131 -0
  104. aip_agents/sandbox/template_builder.pyi +36 -0
  105. aip_agents/sandbox/types.py +24 -0
  106. aip_agents/sandbox/types.pyi +14 -0
  107. aip_agents/sandbox/validation.py +50 -0
  108. aip_agents/sandbox/validation.pyi +20 -0
  109. aip_agents/sentry/__init__.py +1 -1
  110. aip_agents/sentry/sentry.py +33 -12
  111. aip_agents/sentry/sentry.pyi +5 -4
  112. aip_agents/tools/__init__.py +20 -3
  113. aip_agents/tools/__init__.pyi +4 -2
  114. aip_agents/tools/browser_use/browser_use_tool.py +8 -0
  115. aip_agents/tools/browser_use/streaming.py +2 -0
  116. aip_agents/tools/code_sandbox/e2b_cloud_sandbox_extended.py +80 -31
  117. aip_agents/tools/code_sandbox/e2b_cloud_sandbox_extended.pyi +25 -9
  118. aip_agents/tools/code_sandbox/e2b_sandbox_tool.py +6 -6
  119. aip_agents/tools/constants.py +24 -12
  120. aip_agents/tools/constants.pyi +14 -11
  121. aip_agents/tools/date_range_tool.py +554 -0
  122. aip_agents/tools/date_range_tool.pyi +21 -0
  123. aip_agents/tools/execute_ptc_code.py +357 -0
  124. aip_agents/tools/execute_ptc_code.pyi +90 -0
  125. aip_agents/tools/gl_connector/__init__.py +1 -1
  126. aip_agents/tools/gl_connector/tool.py +62 -30
  127. aip_agents/tools/gl_connector/tool.pyi +3 -3
  128. aip_agents/tools/gl_connector_tools.py +119 -0
  129. aip_agents/tools/gl_connector_tools.pyi +39 -0
  130. aip_agents/tools/memory_search/__init__.py +8 -1
  131. aip_agents/tools/memory_search/__init__.pyi +3 -3
  132. aip_agents/tools/memory_search/mem0.py +114 -1
  133. aip_agents/tools/memory_search/mem0.pyi +11 -1
  134. aip_agents/tools/memory_search/schema.py +33 -0
  135. aip_agents/tools/memory_search/schema.pyi +10 -0
  136. aip_agents/tools/memory_search_tool.py +8 -0
  137. aip_agents/tools/memory_search_tool.pyi +2 -2
  138. aip_agents/utils/langgraph/tool_managers/delegation_tool_manager.py +26 -1
  139. aip_agents/utils/langgraph/tool_output_management.py +80 -0
  140. aip_agents/utils/langgraph/tool_output_management.pyi +37 -0
  141. {aip_agents_binary-0.5.21.dist-info → aip_agents_binary-0.6.8.dist-info}/METADATA +14 -22
  142. {aip_agents_binary-0.5.21.dist-info → aip_agents_binary-0.6.8.dist-info}/RECORD +144 -58
  143. {aip_agents_binary-0.5.21.dist-info → aip_agents_binary-0.6.8.dist-info}/WHEEL +1 -1
  144. aip_agents/examples/demo_memory_recall.py +0 -401
  145. aip_agents/examples/demo_memory_recall.pyi +0 -58
  146. aip_agents/examples/hello_world_langgraph_bosa_twitter.pyi +0 -5
  147. aip_agents/tools/bosa_tools.py +0 -105
  148. aip_agents/tools/bosa_tools.pyi +0 -37
  149. {aip_agents_binary-0.5.21.dist-info → aip_agents_binary-0.6.8.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,122 @@
1
+ """Documentation generation utilities for PTC.
2
+
3
+ Shared constants and helpers for generating tool documentation in sandbox payloads.
4
+
5
+ Authors:
6
+ Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
7
+ """
8
+
9
+ from typing import Any
10
+
11
+ from aip_agents.ptc.naming import sanitize_function_name
12
+
13
+ # Documentation limits (fixed constants per plan)
14
+ DOC_DESC_LIMIT = 120 # Tool description trim limit
15
+ DOC_PARAM_DESC_LIMIT = 80 # Parameter description trim limit
16
+
17
+
18
+ def json_type_to_display(json_type: Any) -> str:
19
+ """Convert JSON type to display string.
20
+
21
+ Args:
22
+ json_type: JSON schema type.
23
+
24
+ Returns:
25
+ Human-readable type string.
26
+ """
27
+ if isinstance(json_type, list):
28
+ return "any"
29
+ type_map = {
30
+ "string": "str",
31
+ "integer": "int",
32
+ "number": "float",
33
+ "boolean": "bool",
34
+ "array": "list",
35
+ "object": "dict",
36
+ "null": "None",
37
+ }
38
+ return type_map.get(str(json_type), "any")
39
+
40
+
41
+ def trim_text(text: str | None, limit: int) -> str:
42
+ """Trim text to limit with ellipsis if needed.
43
+
44
+ Args:
45
+ text: Text to trim.
46
+ limit: Maximum length before trimming.
47
+
48
+ Returns:
49
+ Trimmed text.
50
+ """
51
+ if not text:
52
+ return ""
53
+ if len(text) > limit:
54
+ return text[:limit] + "..."
55
+ return text
56
+
57
+
58
+ def render_tool_doc(
59
+ func_name: str,
60
+ signature: str,
61
+ description: str,
62
+ schema: dict[str, Any],
63
+ is_stub: bool = False,
64
+ example_code: str | None = None,
65
+ example_heading: str = "## Example",
66
+ ) -> str:
67
+ """Render markdown documentation for a tool.
68
+
69
+ Args:
70
+ func_name: Sanitized function name.
71
+ signature: Full function signature.
72
+ description: Tool description.
73
+ schema: Input schema for parameters.
74
+ is_stub: Whether this is a stub documentation.
75
+ example_code: Optional example code block content (without ```python).
76
+ example_heading: Heading for the example section.
77
+
78
+ Returns:
79
+ Markdown documentation string.
80
+ """
81
+ if is_stub:
82
+ desc = "Details unavailable because tool definitions are not loaded yet."
83
+ else:
84
+ desc = trim_text(description, DOC_DESC_LIMIT) or "No description available."
85
+
86
+ lines = [
87
+ f"# {func_name}",
88
+ "",
89
+ f"**Description:** {desc}",
90
+ "",
91
+ f"**Signature:** `{signature}`",
92
+ "",
93
+ ]
94
+
95
+ # Add parameters section
96
+ properties = schema.get("properties", {})
97
+ required = set(schema.get("required", []))
98
+
99
+ if properties:
100
+ lines.append("## Parameters")
101
+ lines.append("")
102
+ for prop_name, prop_schema in sorted(properties.items()):
103
+ safe_param = sanitize_function_name(prop_name)
104
+ prop_type = json_type_to_display(prop_schema.get("type", "any"))
105
+ is_required = "required" if prop_name in required else "optional"
106
+
107
+ # Trim param description
108
+ raw_param_desc = prop_schema.get("description", "")
109
+ param_desc = trim_text(raw_param_desc, DOC_PARAM_DESC_LIMIT)
110
+
111
+ lines.append(f"- **{safe_param}** ({prop_type}, {is_required}): {param_desc}")
112
+ lines.append("")
113
+
114
+ # Add example section
115
+ if example_code:
116
+ lines.append(example_heading)
117
+ lines.append("")
118
+ lines.append("```python")
119
+ lines.append(example_code)
120
+ lines.append("```")
121
+
122
+ return "\n".join(lines)
@@ -0,0 +1,40 @@
1
+ from aip_agents.ptc.naming import sanitize_function_name as sanitize_function_name
2
+ from typing import Any
3
+
4
+ DOC_DESC_LIMIT: int
5
+ DOC_PARAM_DESC_LIMIT: int
6
+
7
+ def json_type_to_display(json_type: Any) -> str:
8
+ """Convert JSON type to display string.
9
+
10
+ Args:
11
+ json_type: JSON schema type.
12
+
13
+ Returns:
14
+ Human-readable type string.
15
+ """
16
+ def trim_text(text: str | None, limit: int) -> str:
17
+ """Trim text to limit with ellipsis if needed.
18
+
19
+ Args:
20
+ text: Text to trim.
21
+ limit: Maximum length before trimming.
22
+
23
+ Returns:
24
+ Trimmed text.
25
+ """
26
+ def render_tool_doc(func_name: str, signature: str, description: str, schema: dict[str, Any], is_stub: bool = False, example_code: str | None = None, example_heading: str = '## Example') -> str:
27
+ """Render markdown documentation for a tool.
28
+
29
+ Args:
30
+ func_name: Sanitized function name.
31
+ signature: Full function signature.
32
+ description: Tool description.
33
+ schema: Input schema for parameters.
34
+ is_stub: Whether this is a stub documentation.
35
+ example_code: Optional example code block content (without ```python).
36
+ example_heading: Heading for the example section.
37
+
38
+ Returns:
39
+ Markdown documentation string.
40
+ """
@@ -0,0 +1,57 @@
1
+ """PTC-specific exceptions.
2
+
3
+ This module defines exceptions for Programmatic Tool Calling operations.
4
+
5
+ Authors:
6
+ Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
7
+ """
8
+
9
+
10
+ class PTCError(Exception):
11
+ """Base exception for PTC errors."""
12
+
13
+ pass
14
+
15
+
16
+ class PTCToolError(PTCError):
17
+ """Error during tool execution.
18
+
19
+ Attributes:
20
+ server_name: The MCP server where the error occurred.
21
+ tool_name: The tool that failed.
22
+ """
23
+
24
+ def __init__(
25
+ self,
26
+ message: str,
27
+ server_name: str | None = None,
28
+ tool_name: str | None = None,
29
+ ) -> None:
30
+ """Initialize PTCToolError.
31
+
32
+ Args:
33
+ message: Error message.
34
+ server_name: The MCP server name (optional).
35
+ tool_name: The tool name (optional).
36
+ """
37
+ super().__init__(message)
38
+ self.server_name = server_name
39
+ self.tool_name = tool_name
40
+
41
+
42
+ class PTCPayloadConflictError(PTCError):
43
+ """Error when merging payloads with conflicting file paths.
44
+
45
+ Attributes:
46
+ conflicts: Set of conflicting file paths.
47
+ """
48
+
49
+ def __init__(self, message: str, conflicts: set[str]) -> None:
50
+ """Initialize PTCPayloadConflictError.
51
+
52
+ Args:
53
+ message: Error message.
54
+ conflicts: Set of conflicting file paths.
55
+ """
56
+ super().__init__(message)
57
+ self.conflicts = conflicts
@@ -0,0 +1,37 @@
1
+ from _typeshed import Incomplete
2
+
3
+ class PTCError(Exception):
4
+ """Base exception for PTC errors."""
5
+
6
+ class PTCToolError(PTCError):
7
+ """Error during tool execution.
8
+
9
+ Attributes:
10
+ server_name: The MCP server where the error occurred.
11
+ tool_name: The tool that failed.
12
+ """
13
+ server_name: Incomplete
14
+ tool_name: Incomplete
15
+ def __init__(self, message: str, server_name: str | None = None, tool_name: str | None = None) -> None:
16
+ """Initialize PTCToolError.
17
+
18
+ Args:
19
+ message: Error message.
20
+ server_name: The MCP server name (optional).
21
+ tool_name: The tool name (optional).
22
+ """
23
+
24
+ class PTCPayloadConflictError(PTCError):
25
+ """Error when merging payloads with conflicting file paths.
26
+
27
+ Attributes:
28
+ conflicts: Set of conflicting file paths.
29
+ """
30
+ conflicts: Incomplete
31
+ def __init__(self, message: str, conflicts: set[str]) -> None:
32
+ """Initialize PTCPayloadConflictError.
33
+
34
+ Args:
35
+ message: Error message.
36
+ conflicts: Set of conflicting file paths.
37
+ """
@@ -0,0 +1,261 @@
1
+ """PTC Executor implementations.
2
+
3
+ This module provides the sandboxed executor for Programmatic Tool Calling.
4
+
5
+ Authors:
6
+ Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+ from dataclasses import dataclass, field
13
+ from typing import TYPE_CHECKING
14
+
15
+ from aip_agents.mcp.client.base_mcp_client import BaseMCPClient
16
+ from aip_agents.ptc.custom_tools import PTCCustomToolConfig
17
+ from aip_agents.ptc.exceptions import PTCToolError
18
+ from aip_agents.ptc.prompt_builder import PromptConfig
19
+ from aip_agents.sandbox.defaults import DEFAULT_PTC_PACKAGES, DEFAULT_PTC_TEMPLATE
20
+ from aip_agents.utils.logger import get_logger
21
+
22
+ # Lazy import to avoid circular dependencies
23
+ # These are only needed for PTCSandboxExecutor
24
+ try:
25
+ from aip_agents.ptc.sandbox_bridge import build_sandbox_payload, wrap_ptc_code
26
+ from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime
27
+ from aip_agents.sandbox.types import SandboxExecutionResult
28
+
29
+ _SANDBOX_DEPS_AVAILABLE = True
30
+ except ImportError:
31
+ _SANDBOX_DEPS_AVAILABLE = False
32
+
33
+ logger = get_logger(__name__)
34
+
35
+ if TYPE_CHECKING:
36
+ from aip_agents.ptc.payload import SandboxPayload
37
+
38
+
39
+ @dataclass
40
+ class PTCSandboxConfig:
41
+ """Configuration for PTC sandbox executor.
42
+
43
+ Attributes:
44
+ enabled: Whether PTC is enabled. When False, PTC is disabled.
45
+ default_tool_timeout: Default timeout per tool call in seconds.
46
+ sandbox_template: Optional E2B sandbox template ID.
47
+ sandbox_timeout: Sandbox execution timeout in seconds (hard cap/TTL).
48
+ ptc_packages: List of packages to install in sandbox. Defaults to DEFAULT_PTC_PACKAGES.
49
+ prompt: Prompt configuration for PTC usage guidance.
50
+ custom_tools: Configuration for custom LangChain tools in sandbox.
51
+ """
52
+
53
+ enabled: bool = False
54
+ default_tool_timeout: float = 60.0
55
+ sandbox_template: str | None = DEFAULT_PTC_TEMPLATE
56
+ sandbox_timeout: float = 300.0
57
+ ptc_packages: list[str] | None = field(default_factory=lambda: list(DEFAULT_PTC_PACKAGES))
58
+ prompt: PromptConfig = field(default_factory=PromptConfig)
59
+ custom_tools: PTCCustomToolConfig = field(default_factory=PTCCustomToolConfig)
60
+
61
+
62
+ class PTCSandboxExecutor:
63
+ r"""Executes PTC code inside an E2B sandbox.
64
+
65
+ This executor is used for LLM-generated code that requires sandboxing.
66
+ It builds a sandbox payload (MCP server config + generated tool modules)
67
+ and executes the code using the E2B runtime.
68
+
69
+ Static bundle caching:
70
+ The executor tracks whether the static bundle (wrappers, registry, sources)
71
+ has been uploaded. On the first run, it uploads the full payload. On subsequent
72
+ runs, it only uploads per-run files (e.g., tools/custom_defaults.json) to
73
+ reduce upload overhead.
74
+
75
+ If the sandbox is destroyed/recreated, call reset_static_bundle() to force
76
+ a full re-upload on the next execution.
77
+
78
+ Thread-safety:
79
+ The static bundle upload is guarded by an asyncio.Lock. Concurrent calls will
80
+ serialize while the initial upload completes. After the bundle is cached,
81
+ executions proceed without locking.
82
+
83
+ Example:
84
+ runtime = E2BSandboxRuntime()
85
+ executor = PTCSandboxExecutor(mcp_client, runtime)
86
+ result = await executor.execute_code("from tools.yfinance import get_stock\\nprint(get_stock('AAPL'))")
87
+ """
88
+
89
+ def __init__(
90
+ self,
91
+ mcp_client: BaseMCPClient | None,
92
+ runtime: E2BSandboxRuntime,
93
+ config: PTCSandboxConfig | None = None,
94
+ ) -> None:
95
+ """Initialize PTCSandboxExecutor.
96
+
97
+ Args:
98
+ mcp_client: The MCP client with configured servers. Can be None for custom-only configs.
99
+ runtime: The E2B sandbox runtime instance.
100
+ config: Optional sandbox executor configuration.
101
+
102
+ Raises:
103
+ ImportError: If sandbox dependencies are not available.
104
+ """
105
+ if not _SANDBOX_DEPS_AVAILABLE:
106
+ raise ImportError(
107
+ "Sandbox dependencies not available. "
108
+ "PTCSandboxExecutor requires sandbox_bridge and e2b_runtime modules."
109
+ )
110
+
111
+ self._mcp_client = mcp_client
112
+ self._runtime = runtime
113
+ self._config = config or PTCSandboxConfig()
114
+ self._static_bundle_uploaded = False
115
+ self._bundle_lock = asyncio.Lock()
116
+
117
+ def _reset_bundle_state_if_inactive(self) -> None:
118
+ """Reset cached bundle state when the runtime is inactive."""
119
+ if self._runtime.is_active:
120
+ return
121
+ if not self._static_bundle_uploaded:
122
+ return
123
+
124
+ logger.info("Runtime is inactive, resetting static bundle state")
125
+ self._static_bundle_uploaded = False
126
+
127
+ def _should_include_packages_path(self) -> bool:
128
+ """Check if the packages path should be added to sys.path."""
129
+ custom_tools = self._config.custom_tools
130
+ if not custom_tools.enabled or not custom_tools.tools:
131
+ return False
132
+
133
+ return any(tool.get("package_path") or tool.get("kind") == "file" for tool in custom_tools.tools)
134
+
135
+ async def _build_payload(self, tool_configs: dict[str, dict] | None) -> SandboxPayload:
136
+ """Build the sandbox payload for execution."""
137
+ logger.info("Building sandbox payload")
138
+ return await build_sandbox_payload(
139
+ self._mcp_client,
140
+ self._config.default_tool_timeout,
141
+ custom_tools_config=self._config.custom_tools,
142
+ tool_configs=tool_configs,
143
+ )
144
+
145
+ def _wrap_code(self, code: str) -> str:
146
+ """Wrap code with required imports and setup."""
147
+ logger.info("Wrapping PTC code")
148
+ return wrap_ptc_code(code, include_packages_path=self._should_include_packages_path())
149
+
150
+ async def _execute_payload(
151
+ self,
152
+ payload: SandboxPayload,
153
+ wrapped_code: str,
154
+ *,
155
+ upload_static_bundle: bool,
156
+ ) -> SandboxExecutionResult:
157
+ """Execute the wrapped code with the provided payload."""
158
+ if upload_static_bundle:
159
+ logger.info("Uploading static bundle and per-run files")
160
+ files_to_upload = {**payload.files, **payload.per_run_files}
161
+ else:
162
+ logger.debug("Static bundle already uploaded, uploading only per-run files")
163
+ files_to_upload = payload.per_run_files
164
+
165
+ logger.info(f"Executing code in sandbox (timeout: {self._config.sandbox_timeout}s)")
166
+ return await self._runtime.execute(
167
+ code=wrapped_code,
168
+ timeout=self._config.sandbox_timeout,
169
+ files=files_to_upload if files_to_upload else None,
170
+ env=payload.env,
171
+ template=self._config.sandbox_template,
172
+ )
173
+
174
+ async def _execute_with_bundle(
175
+ self,
176
+ payload: SandboxPayload,
177
+ wrapped_code: str,
178
+ ) -> SandboxExecutionResult:
179
+ """Execute code using the cached static bundle when possible."""
180
+ if self._static_bundle_uploaded:
181
+ return await self._execute_payload(
182
+ payload,
183
+ wrapped_code,
184
+ upload_static_bundle=False,
185
+ )
186
+
187
+ async with self._bundle_lock:
188
+ if self._static_bundle_uploaded:
189
+ return await self._execute_payload(
190
+ payload,
191
+ wrapped_code,
192
+ upload_static_bundle=False,
193
+ )
194
+
195
+ result = await self._execute_payload(
196
+ payload,
197
+ wrapped_code,
198
+ upload_static_bundle=True,
199
+ )
200
+ self._update_static_bundle_cache(result)
201
+ return result
202
+
203
+ def _update_static_bundle_cache(self, result: SandboxExecutionResult) -> None:
204
+ """Update cached bundle state after a successful upload."""
205
+ if result.exit_code != 0:
206
+ return
207
+
208
+ self._static_bundle_uploaded = True
209
+ logger.debug("Static bundle successfully uploaded and cached")
210
+
211
+ def _log_execution_result(self, result: SandboxExecutionResult) -> None:
212
+ """Log execution results based on exit status."""
213
+ if result.exit_code == 0:
214
+ logger.info("Sandbox execution completed successfully")
215
+ else:
216
+ logger.warning(f"Sandbox execution failed with exit code {result.exit_code}")
217
+
218
+ async def execute_code(
219
+ self,
220
+ code: str,
221
+ tool_configs: dict[str, dict] | None = None,
222
+ ) -> SandboxExecutionResult:
223
+ """Execute code inside the sandbox with MCP access.
224
+
225
+ This method:
226
+ 1. Builds the sandbox payload (MCP config + generated tool modules + custom tools)
227
+ 2. Wraps the user code with necessary imports and setup
228
+ 3. Executes the code in the E2B sandbox
229
+ 4. Returns the execution result (stdout/stderr/exit_code)
230
+
231
+ Args:
232
+ code: Python code to execute in the sandbox.
233
+ tool_configs: Optional per-tool config values for custom LangChain tools.
234
+ These are merged with agent defaults and written to custom_defaults.json.
235
+
236
+ Returns:
237
+ SandboxExecutionResult with stdout, stderr, and exit_code.
238
+
239
+ Raises:
240
+ PTCToolError: If sandbox execution fails.
241
+ """
242
+ try:
243
+ self._reset_bundle_state_if_inactive()
244
+ payload = await self._build_payload(tool_configs)
245
+ wrapped_code = self._wrap_code(code)
246
+ result = await self._execute_with_bundle(payload, wrapped_code)
247
+ self._log_execution_result(result)
248
+ return result
249
+
250
+ except Exception as exc:
251
+ logger.error(f"Sandbox execution failed: {exc}")
252
+ raise PTCToolError(f"Sandbox execution failed: {exc}") from exc
253
+
254
+ def reset_static_bundle(self) -> None:
255
+ """Reset the static bundle upload state.
256
+
257
+ Call this method when the sandbox is destroyed/recreated to force
258
+ a full re-upload of the static bundle on the next execution.
259
+ """
260
+ self._static_bundle_uploaded = False
261
+ logger.debug("Static bundle state reset, next execution will re-upload full payload")
@@ -0,0 +1,99 @@
1
+ from _typeshed import Incomplete
2
+ from aip_agents.mcp.client.base_mcp_client import BaseMCPClient as BaseMCPClient
3
+ from aip_agents.ptc.custom_tools import PTCCustomToolConfig as PTCCustomToolConfig
4
+ from aip_agents.ptc.exceptions import PTCToolError as PTCToolError
5
+ from aip_agents.ptc.payload import SandboxPayload as SandboxPayload
6
+ from aip_agents.ptc.prompt_builder import PromptConfig as PromptConfig
7
+ from aip_agents.ptc.sandbox_bridge import build_sandbox_payload as build_sandbox_payload, wrap_ptc_code as wrap_ptc_code
8
+ from aip_agents.sandbox.defaults import DEFAULT_PTC_PACKAGES as DEFAULT_PTC_PACKAGES, DEFAULT_PTC_TEMPLATE as DEFAULT_PTC_TEMPLATE
9
+ from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime as E2BSandboxRuntime
10
+ from aip_agents.sandbox.types import SandboxExecutionResult as SandboxExecutionResult
11
+ from aip_agents.utils.logger import get_logger as get_logger
12
+ from dataclasses import dataclass, field
13
+
14
+ logger: Incomplete
15
+
16
+ @dataclass
17
+ class PTCSandboxConfig:
18
+ """Configuration for PTC sandbox executor.
19
+
20
+ Attributes:
21
+ enabled: Whether PTC is enabled. When False, PTC is disabled.
22
+ default_tool_timeout: Default timeout per tool call in seconds.
23
+ sandbox_template: Optional E2B sandbox template ID.
24
+ sandbox_timeout: Sandbox execution timeout in seconds (hard cap/TTL).
25
+ ptc_packages: List of packages to install in sandbox. Defaults to DEFAULT_PTC_PACKAGES.
26
+ prompt: Prompt configuration for PTC usage guidance.
27
+ custom_tools: Configuration for custom LangChain tools in sandbox.
28
+ """
29
+ enabled: bool = ...
30
+ default_tool_timeout: float = ...
31
+ sandbox_template: str | None = ...
32
+ sandbox_timeout: float = ...
33
+ ptc_packages: list[str] | None = field(default_factory=Incomplete)
34
+ prompt: PromptConfig = field(default_factory=PromptConfig)
35
+ custom_tools: PTCCustomToolConfig = field(default_factory=PTCCustomToolConfig)
36
+
37
+ class PTCSandboxExecutor:
38
+ '''Executes PTC code inside an E2B sandbox.
39
+
40
+ This executor is used for LLM-generated code that requires sandboxing.
41
+ It builds a sandbox payload (MCP server config + generated tool modules)
42
+ and executes the code using the E2B runtime.
43
+
44
+ Static bundle caching:
45
+ The executor tracks whether the static bundle (wrappers, registry, sources)
46
+ has been uploaded. On the first run, it uploads the full payload. On subsequent
47
+ runs, it only uploads per-run files (e.g., tools/custom_defaults.json) to
48
+ reduce upload overhead.
49
+
50
+ If the sandbox is destroyed/recreated, call reset_static_bundle() to force
51
+ a full re-upload on the next execution.
52
+
53
+ Thread-safety:
54
+ The static bundle upload is guarded by an asyncio.Lock. Concurrent calls will
55
+ serialize while the initial upload completes. After the bundle is cached,
56
+ executions proceed without locking.
57
+
58
+ Example:
59
+ runtime = E2BSandboxRuntime()
60
+ executor = PTCSandboxExecutor(mcp_client, runtime)
61
+ result = await executor.execute_code("from tools.yfinance import get_stock\\\\nprint(get_stock(\'AAPL\'))")
62
+ '''
63
+ def __init__(self, mcp_client: BaseMCPClient | None, runtime: E2BSandboxRuntime, config: PTCSandboxConfig | None = None) -> None:
64
+ """Initialize PTCSandboxExecutor.
65
+
66
+ Args:
67
+ mcp_client: The MCP client with configured servers. Can be None for custom-only configs.
68
+ runtime: The E2B sandbox runtime instance.
69
+ config: Optional sandbox executor configuration.
70
+
71
+ Raises:
72
+ ImportError: If sandbox dependencies are not available.
73
+ """
74
+ async def execute_code(self, code: str, tool_configs: dict[str, dict] | None = None) -> SandboxExecutionResult:
75
+ """Execute code inside the sandbox with MCP access.
76
+
77
+ This method:
78
+ 1. Builds the sandbox payload (MCP config + generated tool modules + custom tools)
79
+ 2. Wraps the user code with necessary imports and setup
80
+ 3. Executes the code in the E2B sandbox
81
+ 4. Returns the execution result (stdout/stderr/exit_code)
82
+
83
+ Args:
84
+ code: Python code to execute in the sandbox.
85
+ tool_configs: Optional per-tool config values for custom LangChain tools.
86
+ These are merged with agent defaults and written to custom_defaults.json.
87
+
88
+ Returns:
89
+ SandboxExecutionResult with stdout, stderr, and exit_code.
90
+
91
+ Raises:
92
+ PTCToolError: If sandbox execution fails.
93
+ """
94
+ def reset_static_bundle(self) -> None:
95
+ """Reset the static bundle upload state.
96
+
97
+ Call this method when the sandbox is destroyed/recreated to force
98
+ a full re-upload of the static bundle on the next execution.
99
+ """
@@ -0,0 +1,45 @@
1
+ """Programmatic Tool Calling (PTC) module for MCP tools.
2
+
3
+ This module provides programmatic tool calling capabilities for MCP tools,
4
+ allowing code-based tool invocation instead of JSON tool calls.
5
+
6
+ Authors:
7
+ Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
8
+ """
9
+
10
+ from aip_agents.ptc.exceptions import PTCError, PTCToolError
11
+ from aip_agents.ptc.mcp.sandbox_bridge import (
12
+ ServerConfig,
13
+ build_mcp_payload,
14
+ )
15
+ from aip_agents.ptc.naming import (
16
+ json_type_to_python,
17
+ sanitize_function_name,
18
+ sanitize_module_name,
19
+ sanitize_param_name,
20
+ schema_to_params,
21
+ )
22
+ from aip_agents.ptc.payload import SandboxPayload
23
+ from aip_agents.ptc.prompt_builder import (
24
+ build_ptc_prompt,
25
+ compute_ptc_prompt_hash,
26
+ )
27
+
28
+ __all__ = [
29
+ # Exceptions
30
+ "PTCError",
31
+ "PTCToolError",
32
+ # Naming utilities
33
+ "json_type_to_python",
34
+ "sanitize_function_name",
35
+ "sanitize_module_name",
36
+ "sanitize_param_name",
37
+ "schema_to_params",
38
+ # Prompt builder
39
+ "build_ptc_prompt",
40
+ "compute_ptc_prompt_hash",
41
+ # Sandbox Bridge
42
+ "SandboxPayload",
43
+ "ServerConfig",
44
+ "build_mcp_payload",
45
+ ]
@@ -0,0 +1,7 @@
1
+ from aip_agents.ptc.exceptions import PTCError as PTCError, PTCToolError as PTCToolError
2
+ from aip_agents.ptc.mcp.sandbox_bridge import ServerConfig as ServerConfig, build_mcp_payload as build_mcp_payload
3
+ from aip_agents.ptc.naming import json_type_to_python as json_type_to_python, sanitize_function_name as sanitize_function_name, sanitize_module_name as sanitize_module_name, sanitize_param_name as sanitize_param_name, schema_to_params as schema_to_params
4
+ from aip_agents.ptc.payload import SandboxPayload as SandboxPayload
5
+ from aip_agents.ptc.prompt_builder import build_ptc_prompt as build_ptc_prompt, compute_ptc_prompt_hash as compute_ptc_prompt_hash
6
+
7
+ __all__ = ['PTCError', 'PTCToolError', 'json_type_to_python', 'sanitize_function_name', 'sanitize_module_name', 'sanitize_param_name', 'schema_to_params', 'build_ptc_prompt', 'compute_ptc_prompt_hash', 'SandboxPayload', 'ServerConfig', 'build_mcp_payload']