glaip-sdk 0.0.20__py3-none-any.whl → 0.7.7__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.
Files changed (216) hide show
  1. glaip_sdk/__init__.py +44 -4
  2. glaip_sdk/_version.py +10 -3
  3. glaip_sdk/agents/__init__.py +27 -0
  4. glaip_sdk/agents/base.py +1250 -0
  5. glaip_sdk/branding.py +15 -6
  6. glaip_sdk/cli/account_store.py +540 -0
  7. glaip_sdk/cli/agent_config.py +2 -6
  8. glaip_sdk/cli/auth.py +271 -45
  9. glaip_sdk/cli/commands/__init__.py +2 -2
  10. glaip_sdk/cli/commands/accounts.py +746 -0
  11. glaip_sdk/cli/commands/agents/__init__.py +119 -0
  12. glaip_sdk/cli/commands/agents/_common.py +561 -0
  13. glaip_sdk/cli/commands/agents/create.py +151 -0
  14. glaip_sdk/cli/commands/agents/delete.py +64 -0
  15. glaip_sdk/cli/commands/agents/get.py +89 -0
  16. glaip_sdk/cli/commands/agents/list.py +129 -0
  17. glaip_sdk/cli/commands/agents/run.py +264 -0
  18. glaip_sdk/cli/commands/agents/sync_langflow.py +72 -0
  19. glaip_sdk/cli/commands/agents/update.py +112 -0
  20. glaip_sdk/cli/commands/common_config.py +104 -0
  21. glaip_sdk/cli/commands/configure.py +734 -143
  22. glaip_sdk/cli/commands/mcps/__init__.py +94 -0
  23. glaip_sdk/cli/commands/mcps/_common.py +459 -0
  24. glaip_sdk/cli/commands/mcps/connect.py +82 -0
  25. glaip_sdk/cli/commands/mcps/create.py +152 -0
  26. glaip_sdk/cli/commands/mcps/delete.py +73 -0
  27. glaip_sdk/cli/commands/mcps/get.py +212 -0
  28. glaip_sdk/cli/commands/mcps/list.py +69 -0
  29. glaip_sdk/cli/commands/mcps/tools.py +235 -0
  30. glaip_sdk/cli/commands/mcps/update.py +190 -0
  31. glaip_sdk/cli/commands/models.py +14 -12
  32. glaip_sdk/cli/commands/shared/__init__.py +21 -0
  33. glaip_sdk/cli/commands/shared/formatters.py +91 -0
  34. glaip_sdk/cli/commands/tools/__init__.py +69 -0
  35. glaip_sdk/cli/commands/tools/_common.py +80 -0
  36. glaip_sdk/cli/commands/tools/create.py +228 -0
  37. glaip_sdk/cli/commands/tools/delete.py +61 -0
  38. glaip_sdk/cli/commands/tools/get.py +103 -0
  39. glaip_sdk/cli/commands/tools/list.py +69 -0
  40. glaip_sdk/cli/commands/tools/script.py +49 -0
  41. glaip_sdk/cli/commands/tools/update.py +102 -0
  42. glaip_sdk/cli/commands/transcripts/__init__.py +90 -0
  43. glaip_sdk/cli/commands/transcripts/_common.py +9 -0
  44. glaip_sdk/cli/commands/transcripts/clear.py +5 -0
  45. glaip_sdk/cli/commands/transcripts/detail.py +5 -0
  46. glaip_sdk/cli/commands/transcripts_original.py +756 -0
  47. glaip_sdk/cli/commands/update.py +164 -23
  48. glaip_sdk/cli/config.py +49 -7
  49. glaip_sdk/cli/constants.py +38 -0
  50. glaip_sdk/cli/context.py +8 -0
  51. glaip_sdk/cli/core/__init__.py +79 -0
  52. glaip_sdk/cli/core/context.py +124 -0
  53. glaip_sdk/cli/core/output.py +851 -0
  54. glaip_sdk/cli/core/prompting.py +649 -0
  55. glaip_sdk/cli/core/rendering.py +187 -0
  56. glaip_sdk/cli/display.py +45 -32
  57. glaip_sdk/cli/entrypoint.py +20 -0
  58. glaip_sdk/cli/hints.py +57 -0
  59. glaip_sdk/cli/io.py +14 -17
  60. glaip_sdk/cli/main.py +344 -167
  61. glaip_sdk/cli/masking.py +21 -33
  62. glaip_sdk/cli/mcp_validators.py +5 -15
  63. glaip_sdk/cli/pager.py +15 -22
  64. glaip_sdk/cli/parsers/__init__.py +1 -3
  65. glaip_sdk/cli/parsers/json_input.py +11 -22
  66. glaip_sdk/cli/resolution.py +5 -10
  67. glaip_sdk/cli/rich_helpers.py +1 -3
  68. glaip_sdk/cli/slash/__init__.py +0 -9
  69. glaip_sdk/cli/slash/accounts_controller.py +580 -0
  70. glaip_sdk/cli/slash/accounts_shared.py +75 -0
  71. glaip_sdk/cli/slash/agent_session.py +65 -29
  72. glaip_sdk/cli/slash/prompt.py +24 -10
  73. glaip_sdk/cli/slash/remote_runs_controller.py +566 -0
  74. glaip_sdk/cli/slash/session.py +827 -232
  75. glaip_sdk/cli/slash/tui/__init__.py +34 -0
  76. glaip_sdk/cli/slash/tui/accounts.tcss +88 -0
  77. glaip_sdk/cli/slash/tui/accounts_app.py +933 -0
  78. glaip_sdk/cli/slash/tui/background_tasks.py +72 -0
  79. glaip_sdk/cli/slash/tui/clipboard.py +147 -0
  80. glaip_sdk/cli/slash/tui/context.py +59 -0
  81. glaip_sdk/cli/slash/tui/keybind_registry.py +235 -0
  82. glaip_sdk/cli/slash/tui/loading.py +58 -0
  83. glaip_sdk/cli/slash/tui/remote_runs_app.py +628 -0
  84. glaip_sdk/cli/slash/tui/terminal.py +402 -0
  85. glaip_sdk/cli/slash/tui/theme/__init__.py +15 -0
  86. glaip_sdk/cli/slash/tui/theme/catalog.py +79 -0
  87. glaip_sdk/cli/slash/tui/theme/manager.py +86 -0
  88. glaip_sdk/cli/slash/tui/theme/tokens.py +55 -0
  89. glaip_sdk/cli/slash/tui/toast.py +123 -0
  90. glaip_sdk/cli/transcript/__init__.py +12 -52
  91. glaip_sdk/cli/transcript/cache.py +258 -60
  92. glaip_sdk/cli/transcript/capture.py +72 -21
  93. glaip_sdk/cli/transcript/history.py +815 -0
  94. glaip_sdk/cli/transcript/launcher.py +1 -3
  95. glaip_sdk/cli/transcript/viewer.py +79 -329
  96. glaip_sdk/cli/update_notifier.py +385 -24
  97. glaip_sdk/cli/validators.py +16 -18
  98. glaip_sdk/client/__init__.py +3 -1
  99. glaip_sdk/client/_schedule_payloads.py +89 -0
  100. glaip_sdk/client/agent_runs.py +147 -0
  101. glaip_sdk/client/agents.py +370 -100
  102. glaip_sdk/client/base.py +78 -35
  103. glaip_sdk/client/hitl.py +136 -0
  104. glaip_sdk/client/main.py +25 -10
  105. glaip_sdk/client/mcps.py +166 -27
  106. glaip_sdk/client/payloads/agent/__init__.py +23 -0
  107. glaip_sdk/client/{_agent_payloads.py → payloads/agent/requests.py} +65 -74
  108. glaip_sdk/client/payloads/agent/responses.py +43 -0
  109. glaip_sdk/client/run_rendering.py +583 -79
  110. glaip_sdk/client/schedules.py +439 -0
  111. glaip_sdk/client/shared.py +21 -0
  112. glaip_sdk/client/tools.py +214 -56
  113. glaip_sdk/client/validators.py +20 -48
  114. glaip_sdk/config/constants.py +11 -0
  115. glaip_sdk/exceptions.py +1 -3
  116. glaip_sdk/hitl/__init__.py +48 -0
  117. glaip_sdk/hitl/base.py +64 -0
  118. glaip_sdk/hitl/callback.py +43 -0
  119. glaip_sdk/hitl/local.py +121 -0
  120. glaip_sdk/hitl/remote.py +523 -0
  121. glaip_sdk/icons.py +9 -3
  122. glaip_sdk/mcps/__init__.py +21 -0
  123. glaip_sdk/mcps/base.py +345 -0
  124. glaip_sdk/models/__init__.py +107 -0
  125. glaip_sdk/models/agent.py +47 -0
  126. glaip_sdk/models/agent_runs.py +117 -0
  127. glaip_sdk/models/common.py +42 -0
  128. glaip_sdk/models/mcp.py +33 -0
  129. glaip_sdk/models/schedule.py +224 -0
  130. glaip_sdk/models/tool.py +33 -0
  131. glaip_sdk/payload_schemas/__init__.py +1 -13
  132. glaip_sdk/payload_schemas/agent.py +1 -3
  133. glaip_sdk/registry/__init__.py +55 -0
  134. glaip_sdk/registry/agent.py +164 -0
  135. glaip_sdk/registry/base.py +139 -0
  136. glaip_sdk/registry/mcp.py +253 -0
  137. glaip_sdk/registry/tool.py +445 -0
  138. glaip_sdk/rich_components.py +58 -2
  139. glaip_sdk/runner/__init__.py +76 -0
  140. glaip_sdk/runner/base.py +84 -0
  141. glaip_sdk/runner/deps.py +112 -0
  142. glaip_sdk/runner/langgraph.py +872 -0
  143. glaip_sdk/runner/logging_config.py +77 -0
  144. glaip_sdk/runner/mcp_adapter/__init__.py +13 -0
  145. glaip_sdk/runner/mcp_adapter/base_mcp_adapter.py +43 -0
  146. glaip_sdk/runner/mcp_adapter/langchain_mcp_adapter.py +257 -0
  147. glaip_sdk/runner/mcp_adapter/mcp_config_builder.py +95 -0
  148. glaip_sdk/runner/tool_adapter/__init__.py +18 -0
  149. glaip_sdk/runner/tool_adapter/base_tool_adapter.py +44 -0
  150. glaip_sdk/runner/tool_adapter/langchain_tool_adapter.py +242 -0
  151. glaip_sdk/schedules/__init__.py +22 -0
  152. glaip_sdk/schedules/base.py +291 -0
  153. glaip_sdk/tools/__init__.py +22 -0
  154. glaip_sdk/tools/base.py +468 -0
  155. glaip_sdk/utils/__init__.py +59 -12
  156. glaip_sdk/utils/a2a/__init__.py +34 -0
  157. glaip_sdk/utils/a2a/event_processor.py +188 -0
  158. glaip_sdk/utils/agent_config.py +4 -14
  159. glaip_sdk/utils/bundler.py +403 -0
  160. glaip_sdk/utils/client.py +111 -0
  161. glaip_sdk/utils/client_utils.py +46 -28
  162. glaip_sdk/utils/datetime_helpers.py +58 -0
  163. glaip_sdk/utils/discovery.py +78 -0
  164. glaip_sdk/utils/display.py +25 -21
  165. glaip_sdk/utils/export.py +143 -0
  166. glaip_sdk/utils/general.py +1 -36
  167. glaip_sdk/utils/import_export.py +15 -16
  168. glaip_sdk/utils/import_resolver.py +524 -0
  169. glaip_sdk/utils/instructions.py +101 -0
  170. glaip_sdk/utils/rendering/__init__.py +115 -1
  171. glaip_sdk/utils/rendering/formatting.py +38 -23
  172. glaip_sdk/utils/rendering/layout/__init__.py +64 -0
  173. glaip_sdk/utils/rendering/{renderer → layout}/panels.py +10 -3
  174. glaip_sdk/utils/rendering/{renderer → layout}/progress.py +73 -12
  175. glaip_sdk/utils/rendering/layout/summary.py +74 -0
  176. glaip_sdk/utils/rendering/layout/transcript.py +606 -0
  177. glaip_sdk/utils/rendering/models.py +18 -8
  178. glaip_sdk/utils/rendering/renderer/__init__.py +9 -51
  179. glaip_sdk/utils/rendering/renderer/base.py +534 -882
  180. glaip_sdk/utils/rendering/renderer/config.py +4 -10
  181. glaip_sdk/utils/rendering/renderer/debug.py +30 -34
  182. glaip_sdk/utils/rendering/renderer/factory.py +138 -0
  183. glaip_sdk/utils/rendering/renderer/stream.py +13 -54
  184. glaip_sdk/utils/rendering/renderer/summary_window.py +79 -0
  185. glaip_sdk/utils/rendering/renderer/thinking.py +273 -0
  186. glaip_sdk/utils/rendering/renderer/toggle.py +182 -0
  187. glaip_sdk/utils/rendering/renderer/tool_panels.py +442 -0
  188. glaip_sdk/utils/rendering/renderer/transcript_mode.py +162 -0
  189. glaip_sdk/utils/rendering/state.py +204 -0
  190. glaip_sdk/utils/rendering/step_tree_state.py +100 -0
  191. glaip_sdk/utils/rendering/steps/__init__.py +34 -0
  192. glaip_sdk/utils/rendering/steps/event_processor.py +778 -0
  193. glaip_sdk/utils/rendering/steps/format.py +176 -0
  194. glaip_sdk/utils/rendering/{steps.py → steps/manager.py} +122 -26
  195. glaip_sdk/utils/rendering/timing.py +36 -0
  196. glaip_sdk/utils/rendering/viewer/__init__.py +21 -0
  197. glaip_sdk/utils/rendering/viewer/presenter.py +184 -0
  198. glaip_sdk/utils/resource_refs.py +29 -26
  199. glaip_sdk/utils/runtime_config.py +425 -0
  200. glaip_sdk/utils/serialization.py +32 -46
  201. glaip_sdk/utils/sync.py +162 -0
  202. glaip_sdk/utils/tool_detection.py +301 -0
  203. glaip_sdk/utils/tool_storage_provider.py +140 -0
  204. glaip_sdk/utils/validation.py +20 -28
  205. {glaip_sdk-0.0.20.dist-info → glaip_sdk-0.7.7.dist-info}/METADATA +78 -23
  206. glaip_sdk-0.7.7.dist-info/RECORD +213 -0
  207. {glaip_sdk-0.0.20.dist-info → glaip_sdk-0.7.7.dist-info}/WHEEL +2 -1
  208. glaip_sdk-0.7.7.dist-info/entry_points.txt +2 -0
  209. glaip_sdk-0.7.7.dist-info/top_level.txt +1 -0
  210. glaip_sdk/cli/commands/agents.py +0 -1412
  211. glaip_sdk/cli/commands/mcps.py +0 -1225
  212. glaip_sdk/cli/commands/tools.py +0 -597
  213. glaip_sdk/cli/utils.py +0 -1330
  214. glaip_sdk/models.py +0 -259
  215. glaip_sdk-0.0.20.dist-info/RECORD +0 -80
  216. glaip_sdk-0.0.20.dist-info/entry_points.txt +0 -3
@@ -0,0 +1,162 @@
1
+ """Agent and tool synchronization (create/update) operations.
2
+
3
+ This module provides convenience functions for tool classes that need bundling.
4
+
5
+ For direct upsert operations, use the client methods:
6
+ - client.agents.upsert_agent(identifier, **kwargs)
7
+ - client.tools.upsert_tool(identifier, code, **kwargs)
8
+ - client.mcps.upsert_mcp(identifier, **kwargs)
9
+
10
+ Authors:
11
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import TYPE_CHECKING, Any
17
+
18
+ from glaip_sdk.exceptions import ValidationError
19
+ from glaip_sdk.utils.bundler import ToolBundler
20
+ from glaip_sdk.utils.import_resolver import load_class
21
+ from gllm_core.utils import LoggerManager
22
+
23
+ if TYPE_CHECKING:
24
+ from glaip_sdk.models import Agent, Tool
25
+
26
+ logger = LoggerManager().get_logger(__name__)
27
+
28
+
29
+ def _extract_tool_name(tool_class: Any) -> str:
30
+ """Extract tool name from a class, handling Pydantic v2 models."""
31
+ # Direct attribute access (works for non-Pydantic classes)
32
+ if hasattr(tool_class, "name"):
33
+ name = getattr(tool_class, "name", None)
34
+ if isinstance(name, str):
35
+ return name
36
+
37
+ # Pydantic v2 model - check model_fields
38
+ if hasattr(tool_class, "model_fields"):
39
+ model_fields = getattr(tool_class, "model_fields", {})
40
+ if "name" in model_fields:
41
+ field_info = model_fields["name"]
42
+ if hasattr(field_info, "default") and isinstance(field_info.default, str):
43
+ return field_info.default
44
+
45
+ raise ValueError(f"Cannot extract name from tool class: {tool_class}")
46
+
47
+
48
+ def _extract_tool_description(tool_class: Any) -> str:
49
+ """Extract tool description from a class, handling Pydantic v2 models."""
50
+ # Direct attribute access
51
+ if hasattr(tool_class, "description"):
52
+ desc = getattr(tool_class, "description", None)
53
+ if isinstance(desc, str):
54
+ return desc
55
+
56
+ # Pydantic v2 model - check model_fields
57
+ if hasattr(tool_class, "model_fields"):
58
+ model_fields = getattr(tool_class, "model_fields", {})
59
+ if "description" in model_fields:
60
+ field_info = model_fields["description"]
61
+ if hasattr(field_info, "default") and isinstance(field_info.default, str):
62
+ return field_info.default
63
+
64
+ return ""
65
+
66
+
67
+ def update_or_create_tool(tool_ref: Any) -> Tool:
68
+ """Create or update a tool from a tool class with bundled source code.
69
+
70
+ This function takes a tool class (LangChain BaseTool), bundles its source
71
+ code with inlined imports, and creates/updates it in the backend.
72
+
73
+ Args:
74
+ tool_ref: A tool class (LangChain BaseTool subclass) or import path string.
75
+
76
+ Returns:
77
+ The created or updated tool.
78
+
79
+ Example:
80
+ >>> from glaip_sdk.utils.sync import update_or_create_tool
81
+ >>> from my_tools import WeatherAPITool
82
+ >>> tool = update_or_create_tool(WeatherAPITool)
83
+ """
84
+ from glaip_sdk.utils.client import get_client # noqa: PLC0415
85
+
86
+ client = get_client()
87
+
88
+ # Handle string import path
89
+ if isinstance(tool_ref, str):
90
+ tool_class = load_class(tool_ref)
91
+ else:
92
+ tool_class = tool_ref
93
+
94
+ # Get tool info - handle Pydantic v2 model classes
95
+ tool_name = _extract_tool_name(tool_class)
96
+ tool_description = _extract_tool_description(tool_class)
97
+
98
+ # Bundle source code - try without decorator first (for newer servers 0.1.85+)
99
+ # If validation fails, retry with decorator for older servers (< 0.1.85)
100
+ bundler = ToolBundler(tool_class)
101
+
102
+ try:
103
+ # Try without decorator first (for newer servers where it's optional)
104
+ bundled_source = bundler.bundle(add_tool_plugin_decorator=False)
105
+ logger.info("Tool info: name='%s', description='%s...'", tool_name, tool_description[:50])
106
+ logger.info("Bundled source code (without decorator): %d characters", len(bundled_source))
107
+
108
+ # Attempt upload without decorator
109
+ return client.tools.upsert_tool(
110
+ tool_name,
111
+ code=bundled_source,
112
+ description=tool_description,
113
+ )
114
+ except ValidationError as e:
115
+ # Check if error is about missing @tool_plugin decorator
116
+ error_message = str(e).lower()
117
+ if "@tool_plugin decorator" in error_message or "no classes found" in error_message:
118
+ # Retry with decorator for older servers (< 0.1.85)
119
+ logger.info("Server requires @tool_plugin decorator, retrying with decorator added")
120
+ bundled_source = bundler.bundle(add_tool_plugin_decorator=True)
121
+ logger.info("Bundled source code (with decorator): %d characters", len(bundled_source))
122
+
123
+ return client.tools.upsert_tool(
124
+ tool_name,
125
+ code=bundled_source,
126
+ description=tool_description,
127
+ )
128
+ # Re-raise if it's a different validation error
129
+ raise
130
+
131
+
132
+ def update_or_create_agent(agent_config: dict[str, Any]) -> Agent:
133
+ """Create or update an agent from configuration.
134
+
135
+ Args:
136
+ agent_config: Agent configuration dictionary containing:
137
+ - name (str): Agent name (required)
138
+ - description (str): Agent description
139
+ - instruction (str): Agent instruction
140
+ - tools (list, optional): List of tool IDs
141
+ - agents (list, optional): List of sub-agent IDs
142
+ - metadata (dict, optional): Additional metadata
143
+
144
+ Returns:
145
+ The created or updated agent.
146
+
147
+ Example:
148
+ >>> from glaip_sdk.utils.sync import update_or_create_agent
149
+ >>> config = {
150
+ ... "name": "weather_reporter",
151
+ ... "description": "Weather reporting agent",
152
+ ... "instruction": "You are a weather reporter.",
153
+ ... }
154
+ >>> agent = update_or_create_agent(config)
155
+ """
156
+ from glaip_sdk.utils.client import get_client # noqa: PLC0415
157
+
158
+ client = get_client()
159
+ agent_name = agent_config.pop("name")
160
+
161
+ # Use client's upsert method
162
+ return client.agents.upsert_agent(agent_name, **agent_config)
@@ -0,0 +1,301 @@
1
+ """Shared utilities for tool type detection.
2
+
3
+ Authors:
4
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import ast
10
+ import importlib
11
+ import inspect
12
+ import pkgutil
13
+ from functools import lru_cache
14
+ from typing import Any
15
+
16
+ # Constants for frequently used strings to avoid duplication (S1192)
17
+ _NAME = "name"
18
+ _AIP_AGENTS_TOOLS = "aip_agents.tools"
19
+ _BASE_TOOL = "BaseTool"
20
+
21
+ # Internal map to store all discovered tools in the session
22
+ _DISCOVERED_TOOLS: dict[str, type] | None = None
23
+
24
+
25
+ def _should_skip_module(module_name: str) -> bool:
26
+ """Check if module should be skipped during tool discovery."""
27
+ short_name = module_name.rsplit(".", 1)[-1]
28
+ return short_name.startswith("_") or "test" in short_name
29
+
30
+
31
+ def _get_pydantic_field_default(cls: type, attr_name: str, field_name: str) -> str | None:
32
+ """Extract default value from a Pydantic field."""
33
+ try:
34
+ fields = getattr(cls, attr_name, {})
35
+ field = fields.get(field_name)
36
+ # Broad exception handling needed because:
37
+ # - model_fields/__fields__ might be a descriptor that raises AttributeError
38
+ # - field.default might raise during access
39
+ # - Various Pydantic internals can raise unexpected exceptions
40
+ if field and hasattr(field, "default") and isinstance(field.default, str):
41
+ return field.default
42
+ except Exception: # pylint: disable=broad-except
43
+ pass
44
+ return None
45
+
46
+
47
+ def _get_name_from_pydantic_v2(cls: type) -> str | None:
48
+ """Extract name from Pydantic v2 model_fields."""
49
+ return _get_pydantic_field_default(cls, "model_fields", _NAME)
50
+
51
+
52
+ def _get_name_from_pydantic_v1(cls: type) -> str | None:
53
+ """Extract name from Pydantic v1 __fields__."""
54
+ return _get_pydantic_field_default(cls, "__fields__", _NAME)
55
+
56
+
57
+ def get_tool_name(ref: Any) -> str | None:
58
+ """Extract tool name from a tool class or instance.
59
+
60
+ Handles LangChain BaseTool (Pydantic v1/v2) and standard classes.
61
+
62
+ Args:
63
+ ref: Tool class or instance.
64
+
65
+ Returns:
66
+ The extracted tool name, or None if not found.
67
+ """
68
+ if ref is None:
69
+ return None
70
+
71
+ # 1. Try instance 'name' attribute
72
+ if not isinstance(ref, type):
73
+ try:
74
+ name = getattr(ref, _NAME, None)
75
+ if isinstance(name, str):
76
+ return name
77
+ except Exception: # pylint: disable=broad-except
78
+ pass
79
+
80
+ cls = ref if isinstance(ref, type) else type(ref)
81
+
82
+ # 2. Try class 'model_fields' (Pydantic v2)
83
+ # Check Pydantic v2 first for forward compatibility
84
+ name = _get_name_from_pydantic_v2(cls)
85
+ if name:
86
+ return name
87
+
88
+ # 3. Try class '__fields__' (Pydantic v1)
89
+ name = _get_name_from_pydantic_v1(cls)
90
+ if name:
91
+ return name
92
+
93
+ # 4. Try direct class attribute
94
+ if hasattr(cls, _NAME):
95
+ try:
96
+ name_attr = getattr(cls, _NAME)
97
+ if isinstance(name_attr, str):
98
+ return name_attr
99
+ except Exception: # pylint: disable=broad-except
100
+ pass
101
+
102
+ return None
103
+
104
+
105
+ def _check_langchain_standard(ref: Any) -> bool:
106
+ """Perform standard isinstance/issubclass check for LangChain tool."""
107
+ try:
108
+ from langchain_core.tools import BaseTool # noqa: PLC0415
109
+
110
+ # Check if BaseTool is actually a type to avoid TypeError in issubclass/isinstance
111
+ if isinstance(BaseTool, type):
112
+ if isinstance(ref, type) and issubclass(ref, BaseTool):
113
+ return True
114
+ if isinstance(ref, BaseTool):
115
+ return True
116
+ except (ImportError, TypeError):
117
+ pass
118
+ return False
119
+
120
+
121
+ def _check_langchain_fallback(ref: Any) -> bool:
122
+ """Perform name-based fallback check for LangChain tool (robust for mocks).
123
+
124
+ This fallback handles cases where:
125
+ - BaseTool is mocked in tests
126
+ - BaseTool is re-imported through internal modules (e.g., runner)
127
+ - isinstance/issubclass checks fail due to module reloading
128
+ """
129
+ try:
130
+ cls = ref if isinstance(ref, type) else getattr(ref, "__class__", None)
131
+ if cls and hasattr(cls, "__mro__"):
132
+ for c in cls.__mro__:
133
+ c_name = getattr(c, "__name__", None)
134
+ c_module = getattr(c, "__module__", "")
135
+ if c_name == _BASE_TOOL and ("langchain" in c_module or "runner" in c_module):
136
+ return True
137
+ except (AttributeError, TypeError):
138
+ pass
139
+ return False
140
+
141
+
142
+ def is_langchain_tool(ref: Any) -> bool:
143
+ """Check if ref is a LangChain BaseTool class or instance.
144
+
145
+ Shared by:
146
+ - ToolRegistry._is_custom_tool() (for upload detection)
147
+ - LangChainToolAdapter._is_langchain_tool() (for adaptation)
148
+
149
+ Args:
150
+ ref: Object to check.
151
+
152
+ Returns:
153
+ True if ref is a LangChain BaseTool class or instance.
154
+ """
155
+ if ref is None:
156
+ return False
157
+
158
+ # 1. Standard check (preferred)
159
+ if _check_langchain_standard(ref):
160
+ return True
161
+
162
+ # 2. Name-based check (robust fallback for mocks and re-imports)
163
+ return _check_langchain_fallback(ref)
164
+
165
+
166
+ def is_aip_agents_tool(ref: Any) -> bool:
167
+ """Check if ref is an aip-agents tool class or instance.
168
+
169
+ Args:
170
+ ref: Object to check.
171
+
172
+ Returns:
173
+ True if ref is from aip_agents.tools package.
174
+ """
175
+ try:
176
+ # Check class module
177
+ if isinstance(ref, type):
178
+ return ref.__module__.startswith(_AIP_AGENTS_TOOLS)
179
+
180
+ # Check instance class
181
+ if hasattr(ref, "__class__"):
182
+ return ref.__class__.__module__.startswith(_AIP_AGENTS_TOOLS)
183
+
184
+ return False
185
+ except (AttributeError, TypeError):
186
+ return False
187
+
188
+
189
+ def _get_discovered_classes_from_module(module: Any) -> list[type]:
190
+ """Extract BaseTool subclasses from a module."""
191
+ discovered_classes = []
192
+ for attr_name in dir(module):
193
+ if attr_name.startswith("_"):
194
+ continue
195
+
196
+ try:
197
+ attr = getattr(module, attr_name)
198
+ if inspect.isclass(attr) and is_langchain_tool(attr):
199
+ # Ensure it's not the BaseTool class itself
200
+ if getattr(attr, "__name__", None) != _BASE_TOOL:
201
+ discovered_classes.append(attr)
202
+ except Exception: # pylint: disable=broad-except
203
+ continue
204
+ return discovered_classes
205
+
206
+
207
+ def _import_and_map_module(module_name: str, tools_map: dict[str, type]) -> None:
208
+ """Import a single module and extract its tools."""
209
+ try:
210
+ module = importlib.import_module(module_name)
211
+ classes = _get_discovered_classes_from_module(module)
212
+ for tool_class in classes:
213
+ name = get_tool_name(tool_class)
214
+ if name:
215
+ tools_map[name] = tool_class
216
+ except Exception: # pylint: disable=broad-except
217
+ # Broad catch to skip broken modules during discovery
218
+ pass
219
+
220
+
221
+ def _walk_and_map_package(package: Any, tools_map: dict[str, type]) -> None:
222
+ """Walk through a package and map all tools found."""
223
+ try:
224
+ # Walk packages using the package's path and name
225
+ for _, module_name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
226
+ if _should_skip_module(module_name):
227
+ continue # pragma: no cover
228
+
229
+ _import_and_map_module(module_name, tools_map)
230
+ except Exception: # pylint: disable=broad-except
231
+ # Broad catch for walk_packages failure
232
+ pass
233
+
234
+
235
+ def _get_all_aip_agents_tools() -> dict[str, type]:
236
+ """Discover and map all tools in aip_agents.tools (once per session)."""
237
+ global _DISCOVERED_TOOLS # pylint: disable=global-statement
238
+ if _DISCOVERED_TOOLS is None:
239
+ _DISCOVERED_TOOLS = {}
240
+ try:
241
+ package = importlib.import_module(_AIP_AGENTS_TOOLS)
242
+ if hasattr(package, "__path__"):
243
+ _walk_and_map_package(package, _DISCOVERED_TOOLS)
244
+ except (ImportError, AttributeError):
245
+ pass
246
+ return _DISCOVERED_TOOLS
247
+
248
+
249
+ @lru_cache(maxsize=128)
250
+ def find_aip_agents_tool_class(name: str) -> type | None:
251
+ """Find and return a native tool class by tool name.
252
+
253
+ Searches aip_agents.tools submodules for BaseTool subclasses
254
+ with matching 'name' attribute. Uses caching to improve performance.
255
+
256
+ Note:
257
+ Results are discovered once per session and cached. If tools are
258
+ dynamically added to the path after the first call, they may not
259
+ be discovered until the session restarts.
260
+
261
+ Args:
262
+ name (str): The tool name to search for (e.g., "google_serper").
263
+
264
+ Returns:
265
+ type|None: The discovered tool class, or None if not found.
266
+
267
+ Examples:
268
+ >>> find_aip_agents_tool_class("google_serper")
269
+ <class 'aip_agents.tools.web_search.serper_tool.GoogleSerperTool'>
270
+
271
+ >>> find_aip_agents_tool_class("nonexistent")
272
+ None
273
+ """
274
+ return _get_all_aip_agents_tools().get(name)
275
+
276
+
277
+ def clear_discovery_cache() -> None:
278
+ """Clear the tool discovery cache (internal use for testing)."""
279
+ global _DISCOVERED_TOOLS # pylint: disable=global-statement
280
+ _DISCOVERED_TOOLS = None
281
+ find_aip_agents_tool_class.cache_clear()
282
+
283
+
284
+ def is_tool_plugin_decorator(decorator: ast.expr) -> bool:
285
+ """Check if an AST decorator node is @tool_plugin.
286
+
287
+ Shared by:
288
+ - ToolBundler._has_tool_plugin_decorator() (for bundling)
289
+ - ImportResolver._is_tool_plugin_decorator() (for import resolution)
290
+
291
+ Args:
292
+ decorator: AST decorator expression node to check.
293
+
294
+ Returns:
295
+ True if decorator is @tool_plugin.
296
+ """
297
+ if isinstance(decorator, ast.Name) and decorator.id == "tool_plugin":
298
+ return True
299
+ if isinstance(decorator, ast.Call) and isinstance(decorator.func, ast.Name) and decorator.func.id == "tool_plugin":
300
+ return True
301
+ return False
@@ -0,0 +1,140 @@
1
+ """Helpers for local tool output storage setup.
2
+
3
+ This module bridges agent_config.tool_output_sharing to ToolOutputManager
4
+ for local execution without modifying aip-agents.
5
+
6
+ Authors:
7
+ Fachriza Adhiatma (fachriza.d.adhiatma@gdplabs.id)
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import os
13
+ from typing import Any
14
+
15
+ from gllm_core.utils import LoggerManager
16
+
17
+ logger = LoggerManager().get_logger(__name__)
18
+
19
+
20
+ def build_tool_output_manager(agent_name: str, agent_config: dict[str, Any]) -> Any | None:
21
+ """Build a ToolOutputManager for local tool output sharing.
22
+
23
+ Args:
24
+ agent_name: Name of the agent whose tool outputs will be stored.
25
+ agent_config: Agent configuration that may enable tool output sharing and contain task_id.
26
+
27
+ Returns:
28
+ A ToolOutputManager instance when tool output sharing is enabled and
29
+ dependencies are available, otherwise ``None``.
30
+ """
31
+ tool_output_sharing_enabled = agent_config.get("tool_output_sharing", False)
32
+ if not tool_output_sharing_enabled:
33
+ return None
34
+
35
+ try:
36
+ from aip_agents.storage.clients.minio_client import MinioConfig, MinioObjectStorage # noqa: PLC0415
37
+ from aip_agents.storage.providers.memory import InMemoryStorageProvider # noqa: PLC0415
38
+ from aip_agents.storage.providers.object_storage import ObjectStorageProvider # noqa: PLC0415
39
+ from aip_agents.utils.langgraph.tool_output_management import ( # noqa: PLC0415
40
+ ToolOutputConfig,
41
+ ToolOutputManager,
42
+ )
43
+ except ImportError:
44
+ logger.warning("Tool output sharing requested but aip-agents is unavailable; skipping.")
45
+ return None
46
+
47
+ task_id = agent_config.get("task_id")
48
+
49
+ storage_provider = _build_tool_output_storage_provider(
50
+ agent_name=agent_name,
51
+ task_id=task_id,
52
+ minio_config_cls=MinioConfig,
53
+ minio_client_cls=MinioObjectStorage,
54
+ object_storage_provider_cls=ObjectStorageProvider,
55
+ memory_storage_provider_cls=InMemoryStorageProvider,
56
+ )
57
+ tool_output_config = _build_tool_output_config(storage_provider, ToolOutputConfig)
58
+ return ToolOutputManager(tool_output_config)
59
+
60
+
61
+ def _build_tool_output_storage_provider(
62
+ agent_name: str,
63
+ task_id: str | None,
64
+ minio_config_cls: Any,
65
+ minio_client_cls: Any,
66
+ object_storage_provider_cls: Any,
67
+ memory_storage_provider_cls: Any,
68
+ ) -> Any:
69
+ """Create a storage provider for tool output sharing.
70
+
71
+ Args:
72
+ agent_name: Name of the agent whose tool outputs are stored.
73
+ task_id: Optional task identifier for coordination context.
74
+ minio_config_cls: Class exposing a ``from_env`` constructor for MinIO config.
75
+ minio_client_cls: MinIO client class used to talk to the object store.
76
+ object_storage_provider_cls: Storage provider wrapping the MinIO client.
77
+ memory_storage_provider_cls: In-memory provider used as a fallback.
78
+
79
+ Returns:
80
+ An instance of ``object_storage_provider_cls`` when MinIO initialization
81
+ succeeds, otherwise an instance of ``memory_storage_provider_cls``.
82
+ """
83
+ try:
84
+ config_obj = minio_config_cls.from_env()
85
+ minio_client = minio_client_cls(config=config_obj)
86
+ prefix = _build_tool_output_prefix(agent_name, task_id)
87
+ return object_storage_provider_cls(client=minio_client, prefix=prefix, use_json=False)
88
+ except Exception as exc:
89
+ logger.warning("Failed to initialize MinIO for tool outputs: %s. Using in-memory storage.", exc)
90
+ return memory_storage_provider_cls()
91
+
92
+
93
+ def _build_tool_output_prefix(agent_name: str, task_id: str | None) -> str:
94
+ """Build object storage prefix for tool outputs in local mode.
95
+
96
+ Args:
97
+ agent_name: Name of the agent whose outputs are stored.
98
+ task_id: Optional task identifier for coordination context.
99
+
100
+ Returns:
101
+ Object storage key prefix dedicated to the provided agent.
102
+ """
103
+ if task_id:
104
+ return f"tool-outputs/tasks/{task_id}/agents/{agent_name}/"
105
+ return f"tool-outputs/agents/{agent_name}/"
106
+
107
+
108
+ def _build_tool_output_config(storage_provider: Any, config_cls: Any) -> Any:
109
+ """Build ToolOutputConfig using env vars, with safe defaults.
110
+
111
+ Args:
112
+ storage_provider: Provider that will persist tool outputs.
113
+ config_cls: Tool output configuration class to instantiate.
114
+
115
+ Returns:
116
+ A configured ``config_cls`` instance ready for ToolOutputManager use.
117
+ """
118
+
119
+ def safe_int_conversion(env_var: str, default: str) -> int:
120
+ """Convert an environment variable to int with a fallback default.
121
+
122
+ Args:
123
+ env_var: Environment variable name to read.
124
+ default: Default string value used when parsing fails.
125
+
126
+ Returns:
127
+ Integer representation of the environment variable or the default.
128
+ """
129
+ try:
130
+ return int(os.getenv(env_var, default))
131
+ except (ValueError, TypeError):
132
+ logger.warning("Invalid value for %s, using default: %s", env_var, default)
133
+ return int(default)
134
+
135
+ return config_cls(
136
+ max_stored_outputs=safe_int_conversion("TOOL_OUTPUT_MAX_STORED", "200"),
137
+ max_age_minutes=safe_int_conversion("TOOL_OUTPUT_MAX_AGE_MINUTES", str(24 * 60)),
138
+ cleanup_interval=safe_int_conversion("TOOL_OUTPUT_CLEANUP_INTERVAL", "50"),
139
+ storage_provider=storage_provider,
140
+ )