glaip-sdk 0.6.19__py3-none-any.whl → 0.7.27__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 (135) hide show
  1. glaip_sdk/agents/base.py +283 -30
  2. glaip_sdk/agents/component.py +233 -0
  3. glaip_sdk/branding.py +113 -2
  4. glaip_sdk/cli/account_store.py +15 -0
  5. glaip_sdk/cli/auth.py +14 -8
  6. glaip_sdk/cli/commands/accounts.py +1 -1
  7. glaip_sdk/cli/commands/agents/__init__.py +116 -0
  8. glaip_sdk/cli/commands/agents/_common.py +562 -0
  9. glaip_sdk/cli/commands/agents/create.py +155 -0
  10. glaip_sdk/cli/commands/agents/delete.py +64 -0
  11. glaip_sdk/cli/commands/agents/get.py +89 -0
  12. glaip_sdk/cli/commands/agents/list.py +129 -0
  13. glaip_sdk/cli/commands/agents/run.py +264 -0
  14. glaip_sdk/cli/commands/agents/sync_langflow.py +72 -0
  15. glaip_sdk/cli/commands/agents/update.py +112 -0
  16. glaip_sdk/cli/commands/common_config.py +1 -1
  17. glaip_sdk/cli/commands/configure.py +1 -2
  18. glaip_sdk/cli/commands/mcps/__init__.py +94 -0
  19. glaip_sdk/cli/commands/mcps/_common.py +459 -0
  20. glaip_sdk/cli/commands/mcps/connect.py +82 -0
  21. glaip_sdk/cli/commands/mcps/create.py +152 -0
  22. glaip_sdk/cli/commands/mcps/delete.py +73 -0
  23. glaip_sdk/cli/commands/mcps/get.py +212 -0
  24. glaip_sdk/cli/commands/mcps/list.py +69 -0
  25. glaip_sdk/cli/commands/mcps/tools.py +235 -0
  26. glaip_sdk/cli/commands/mcps/update.py +190 -0
  27. glaip_sdk/cli/commands/models.py +2 -4
  28. glaip_sdk/cli/commands/shared/__init__.py +21 -0
  29. glaip_sdk/cli/commands/shared/formatters.py +91 -0
  30. glaip_sdk/cli/commands/tools/__init__.py +69 -0
  31. glaip_sdk/cli/commands/tools/_common.py +80 -0
  32. glaip_sdk/cli/commands/tools/create.py +228 -0
  33. glaip_sdk/cli/commands/tools/delete.py +61 -0
  34. glaip_sdk/cli/commands/tools/get.py +103 -0
  35. glaip_sdk/cli/commands/tools/list.py +69 -0
  36. glaip_sdk/cli/commands/tools/script.py +49 -0
  37. glaip_sdk/cli/commands/tools/update.py +102 -0
  38. glaip_sdk/cli/commands/transcripts/__init__.py +90 -0
  39. glaip_sdk/cli/commands/transcripts/_common.py +9 -0
  40. glaip_sdk/cli/commands/transcripts/clear.py +5 -0
  41. glaip_sdk/cli/commands/transcripts/detail.py +5 -0
  42. glaip_sdk/cli/commands/{transcripts.py → transcripts_original.py} +2 -1
  43. glaip_sdk/cli/commands/update.py +163 -17
  44. glaip_sdk/cli/config.py +1 -0
  45. glaip_sdk/cli/entrypoint.py +20 -0
  46. glaip_sdk/cli/main.py +112 -35
  47. glaip_sdk/cli/pager.py +3 -3
  48. glaip_sdk/cli/resolution.py +2 -1
  49. glaip_sdk/cli/slash/accounts_controller.py +3 -1
  50. glaip_sdk/cli/slash/agent_session.py +1 -1
  51. glaip_sdk/cli/slash/remote_runs_controller.py +3 -1
  52. glaip_sdk/cli/slash/session.py +343 -20
  53. glaip_sdk/cli/slash/tui/__init__.py +29 -1
  54. glaip_sdk/cli/slash/tui/accounts.tcss +97 -6
  55. glaip_sdk/cli/slash/tui/accounts_app.py +1117 -126
  56. glaip_sdk/cli/slash/tui/clipboard.py +316 -0
  57. glaip_sdk/cli/slash/tui/context.py +92 -0
  58. glaip_sdk/cli/slash/tui/indicators.py +341 -0
  59. glaip_sdk/cli/slash/tui/keybind_registry.py +235 -0
  60. glaip_sdk/cli/slash/tui/layouts/__init__.py +14 -0
  61. glaip_sdk/cli/slash/tui/layouts/harlequin.py +184 -0
  62. glaip_sdk/cli/slash/tui/loading.py +43 -21
  63. glaip_sdk/cli/slash/tui/remote_runs_app.py +178 -20
  64. glaip_sdk/cli/slash/tui/terminal.py +407 -0
  65. glaip_sdk/cli/slash/tui/theme/__init__.py +15 -0
  66. glaip_sdk/cli/slash/tui/theme/catalog.py +79 -0
  67. glaip_sdk/cli/slash/tui/theme/manager.py +112 -0
  68. glaip_sdk/cli/slash/tui/theme/tokens.py +55 -0
  69. glaip_sdk/cli/slash/tui/toast.py +388 -0
  70. glaip_sdk/cli/transcript/history.py +1 -1
  71. glaip_sdk/cli/transcript/viewer.py +1 -1
  72. glaip_sdk/cli/tui_settings.py +125 -0
  73. glaip_sdk/cli/update_notifier.py +215 -7
  74. glaip_sdk/cli/validators.py +1 -1
  75. glaip_sdk/client/__init__.py +2 -1
  76. glaip_sdk/client/_schedule_payloads.py +89 -0
  77. glaip_sdk/client/agents.py +293 -17
  78. glaip_sdk/client/base.py +25 -0
  79. glaip_sdk/client/hitl.py +136 -0
  80. glaip_sdk/client/main.py +7 -5
  81. glaip_sdk/client/mcps.py +44 -13
  82. glaip_sdk/client/payloads/agent/__init__.py +23 -0
  83. glaip_sdk/client/{_agent_payloads.py → payloads/agent/requests.py} +28 -48
  84. glaip_sdk/client/payloads/agent/responses.py +43 -0
  85. glaip_sdk/client/run_rendering.py +109 -30
  86. glaip_sdk/client/schedules.py +439 -0
  87. glaip_sdk/client/tools.py +52 -23
  88. glaip_sdk/config/constants.py +22 -2
  89. glaip_sdk/guardrails/__init__.py +80 -0
  90. glaip_sdk/guardrails/serializer.py +91 -0
  91. glaip_sdk/hitl/__init__.py +35 -2
  92. glaip_sdk/hitl/base.py +64 -0
  93. glaip_sdk/hitl/callback.py +43 -0
  94. glaip_sdk/hitl/local.py +1 -31
  95. glaip_sdk/hitl/remote.py +523 -0
  96. glaip_sdk/models/__init__.py +47 -1
  97. glaip_sdk/models/_provider_mappings.py +101 -0
  98. glaip_sdk/models/_validation.py +97 -0
  99. glaip_sdk/models/agent.py +2 -1
  100. glaip_sdk/models/agent_runs.py +2 -1
  101. glaip_sdk/models/constants.py +141 -0
  102. glaip_sdk/models/model.py +170 -0
  103. glaip_sdk/models/schedule.py +224 -0
  104. glaip_sdk/payload_schemas/agent.py +1 -0
  105. glaip_sdk/payload_schemas/guardrails.py +34 -0
  106. glaip_sdk/ptc.py +145 -0
  107. glaip_sdk/registry/tool.py +270 -57
  108. glaip_sdk/runner/__init__.py +20 -3
  109. glaip_sdk/runner/deps.py +4 -1
  110. glaip_sdk/runner/langgraph.py +251 -27
  111. glaip_sdk/runner/logging_config.py +77 -0
  112. glaip_sdk/runner/mcp_adapter/mcp_config_builder.py +30 -9
  113. glaip_sdk/runner/ptc_adapter.py +98 -0
  114. glaip_sdk/runner/tool_adapter/langchain_tool_adapter.py +25 -2
  115. glaip_sdk/schedules/__init__.py +22 -0
  116. glaip_sdk/schedules/base.py +291 -0
  117. glaip_sdk/tools/base.py +67 -14
  118. glaip_sdk/utils/__init__.py +1 -0
  119. glaip_sdk/utils/agent_config.py +8 -2
  120. glaip_sdk/utils/bundler.py +138 -2
  121. glaip_sdk/utils/import_resolver.py +427 -49
  122. glaip_sdk/utils/runtime_config.py +3 -2
  123. glaip_sdk/utils/sync.py +31 -11
  124. glaip_sdk/utils/tool_detection.py +274 -6
  125. {glaip_sdk-0.6.19.dist-info → glaip_sdk-0.7.27.dist-info}/METADATA +22 -8
  126. glaip_sdk-0.7.27.dist-info/RECORD +227 -0
  127. {glaip_sdk-0.6.19.dist-info → glaip_sdk-0.7.27.dist-info}/WHEEL +1 -1
  128. glaip_sdk-0.7.27.dist-info/entry_points.txt +2 -0
  129. glaip_sdk/cli/commands/agents.py +0 -1509
  130. glaip_sdk/cli/commands/mcps.py +0 -1356
  131. glaip_sdk/cli/commands/tools.py +0 -576
  132. glaip_sdk/cli/utils.py +0 -263
  133. glaip_sdk-0.6.19.dist-info/RECORD +0 -163
  134. glaip_sdk-0.6.19.dist-info/entry_points.txt +0 -2
  135. {glaip_sdk-0.6.19.dist-info → glaip_sdk-0.7.27.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,34 @@
1
+ """Guardrail payload schemas for API communication.
2
+
3
+ Authors:
4
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
5
+ """
6
+
7
+ from collections.abc import Mapping, Sequence
8
+ from typing import Any
9
+
10
+ from pydantic import BaseModel, Field
11
+
12
+
13
+ class GuardrailEnginePayload(BaseModel):
14
+ """Payload schema for a single guardrail engine configuration.
15
+
16
+ This model defines the structure for individual safety engines (e.g., phrase_matcher, nemo)
17
+ when communicating with the GL AIP backend.
18
+ """
19
+
20
+ type: str = Field(..., description="The type of guardrail engine (e.g., 'phrase_matcher', 'nemo')")
21
+ config: Mapping[str, Any] = Field(..., description="Engine-specific configuration parameters")
22
+
23
+
24
+ class GuardrailPayload(BaseModel):
25
+ """Payload schema for global guardrail settings.
26
+
27
+ This model acts as the container for all guardrail configurations within the agent_config.
28
+ """
29
+
30
+ enabled: bool = Field(default=True, description="Global toggle to enable or disable all guardrails")
31
+ engines: Sequence[GuardrailEnginePayload] = Field(
32
+ default_factory=list,
33
+ description="List of configured guardrail engines",
34
+ )
glaip_sdk/ptc.py ADDED
@@ -0,0 +1,145 @@
1
+ """Programmatic Tool Calling (PTC) configuration for local SDK runs.
2
+
3
+ This module provides the PTC class for configuring sandboxed code execution
4
+ in local agent runs. PTC enables agents to call execute_ptc_code and use MCP
5
+ tools programmatically in an E2B sandbox.
6
+
7
+ Authors:
8
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from typing import Any
14
+
15
+ from glaip_sdk.exceptions import ValidationError
16
+
17
+
18
+ class PTC:
19
+ """Configuration for Programmatic Tool Calling in local runs.
20
+
21
+ PTC allows agents to execute Python code in a sandboxed E2B environment
22
+ with access to registered MCP tools. This is only supported for local
23
+ runs (local=True) and requires the glaip-sdk[local] installation.
24
+
25
+ Example:
26
+ >>> from glaip_sdk.ptc import PTC
27
+ >>> from glaip_sdk.agents import Agent
28
+ >>>
29
+ >>> ptc = PTC(enabled=True)
30
+ >>> agent = Agent(
31
+ ... name="ptc_demo",
32
+ ... instruction="Use execute_ptc_code for multi-tool workflows.",
33
+ ... mcps=[my_mcp],
34
+ ... ptc=ptc,
35
+ ... )
36
+ >>> agent.run("Analyze the repo", local=True)
37
+
38
+ Custom timeouts and prompts:
39
+ >>> ptc = PTC(
40
+ ... enabled=True,
41
+ ... sandbox_timeout=180.0,
42
+ ... prompt={"mode": "full", "include_example": False},
43
+ ... )
44
+
45
+ Args:
46
+ enabled: Whether PTC is enabled. Must be True to activate PTC.
47
+ When False, all other fields are ignored (toggle-friendly).
48
+ sandbox_timeout: Maximum execution time in seconds for sandbox code.
49
+ Defaults to 120.0 seconds.
50
+ prompt: Prompt configuration for PTC tool description.
51
+ Can be a dict with "mode" and "include_example" keys.
52
+ Defaults to None (uses aip-agents default).
53
+ custom_tools: NOT SUPPORTED in v1. Raises ValidationError if provided
54
+ when enabled=True.
55
+ ptc_packages: NOT SUPPORTED in v1. Raises ValidationError if provided
56
+ when enabled=True.
57
+
58
+ Raises:
59
+ ValidationError: If custom_tools or ptc_packages are provided when
60
+ enabled=True (v1 only supports MCP tools).
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ *,
66
+ enabled: bool = False,
67
+ sandbox_timeout: float = 120.0,
68
+ prompt: dict[str, Any] | None = None,
69
+ custom_tools: list[Any] | None = None,
70
+ ptc_packages: list[str] | None = None,
71
+ ):
72
+ """Initialize PTC configuration.
73
+
74
+ Args:
75
+ enabled: Whether PTC is enabled. Must be True to activate.
76
+ sandbox_timeout: Sandbox execution timeout in seconds.
77
+ prompt: Prompt configuration dict.
78
+ custom_tools: Custom tools (NOT SUPPORTED in v1).
79
+ ptc_packages: Sandbox packages (NOT SUPPORTED in v1).
80
+
81
+ Raises:
82
+ ValidationError: If unsupported features are used when enabled=True.
83
+ """
84
+ self.enabled = enabled
85
+ self.sandbox_timeout = sandbox_timeout
86
+ self.prompt = prompt
87
+ self._custom_tools = custom_tools
88
+ self._ptc_packages = ptc_packages
89
+
90
+ if self.enabled:
91
+ self._validate_v1_constraints()
92
+
93
+ def _validate_v1_constraints(self) -> None:
94
+ """Validate that v1-unsupported features are not used.
95
+
96
+ Raises:
97
+ ValidationError: If custom_tools or ptc_packages are provided.
98
+ """
99
+ if self._custom_tools is not None:
100
+ msg = (
101
+ "PTC custom_tools are not supported in v1. "
102
+ "Only MCP tools are available in the sandbox. "
103
+ "Please remove the custom_tools parameter or wait for "
104
+ "custom tool support in a future release."
105
+ )
106
+ raise ValidationError(msg)
107
+
108
+ if self._ptc_packages is not None:
109
+ msg = (
110
+ "PTC ptc_packages are not supported in v1. "
111
+ "The sandbox uses a fixed template to maintain local/remote parity. "
112
+ "Please remove the ptc_packages parameter or wait for "
113
+ "package installation support in a future release."
114
+ )
115
+ raise ValidationError(msg)
116
+
117
+ def to_dict(self) -> dict[str, Any]:
118
+ """Convert PTC config to dictionary format.
119
+
120
+ Returns:
121
+ Dictionary representation of PTC config.
122
+ """
123
+ result: dict[str, Any] = {
124
+ "enabled": self.enabled,
125
+ }
126
+
127
+ if self.enabled:
128
+ result["sandbox_timeout"] = self.sandbox_timeout
129
+ if self.prompt is not None:
130
+ result["prompt"] = self.prompt
131
+
132
+ return result
133
+
134
+ def __repr__(self) -> str:
135
+ """Return string representation of PTC config."""
136
+ if not self.enabled:
137
+ return "PTC(enabled=False)"
138
+
139
+ parts = [f"enabled={self.enabled}"]
140
+ if abs(self.sandbox_timeout - 120.0) > 1e-9:
141
+ parts.append(f"sandbox_timeout={self.sandbox_timeout}")
142
+ if self.prompt is not None:
143
+ parts.append(f"prompt={self.prompt!r}")
144
+
145
+ return f"PTC({', '.join(parts)})"
@@ -1,6 +1,6 @@
1
1
  """Tool registry for glaip_sdk.
2
2
 
3
- This module provides the ToolRegistry that caches deployed tools
3
+ This module provides a ToolRegistry that caches deployed tools
4
4
  to avoid redundant API calls when deploying agents with tools.
5
5
 
6
6
  Authors:
@@ -54,6 +54,32 @@ class ToolRegistry(BaseRegistry["Tool"]):
54
54
  value = getattr(obj, attr, None)
55
55
  return value if isinstance(value, str) else None
56
56
 
57
+ def _extract_name_from_instance(self, ref: Any) -> str | None:
58
+ """Extract name from a non-type instance.
59
+
60
+ Args:
61
+ ref: The instance to extract name from.
62
+
63
+ Returns:
64
+ The extracted name, or None if not found.
65
+ """
66
+ if isinstance(ref, type):
67
+ return None
68
+ return self._get_string_attr(ref, "name")
69
+
70
+ def _extract_name_from_class(self, ref: Any) -> str | None:
71
+ """Extract name from a class.
72
+
73
+ Args:
74
+ ref: The class to extract name from.
75
+
76
+ Returns:
77
+ The extracted name, or None if not found.
78
+ """
79
+ if not isinstance(ref, type):
80
+ return None
81
+ return self._get_string_attr(ref, "name") or self._get_name_from_model_fields(ref)
82
+
57
83
  def _extract_name(self, ref: Any) -> str:
58
84
  """Extract tool name from a reference.
59
85
 
@@ -66,90 +92,269 @@ class ToolRegistry(BaseRegistry["Tool"]):
66
92
  Raises:
67
93
  ValueError: If name cannot be extracted from the reference.
68
94
  """
95
+ # Lazy import to avoid circular dependency: Tool -> ToolRegistry -> Tool
96
+ from glaip_sdk.tools.base import Tool # noqa: PLC0415
97
+
69
98
  if isinstance(ref, str):
70
99
  return ref
71
100
 
101
+ # Tool instance (from Tool.from_langchain() or Tool.from_native())
102
+ if isinstance(ref, Tool):
103
+ return ref.get_name()
104
+
72
105
  # Dict from API response - extract name or id
73
106
  if isinstance(ref, dict):
74
107
  return ref.get("name") or ref.get("id") or ""
75
108
 
76
109
  # Tool instance (not a class) with name attribute
77
- if not isinstance(ref, type):
78
- name = self._get_string_attr(ref, "name")
79
- if name:
80
- return name
110
+ name = self._extract_name_from_instance(ref)
111
+ if name:
112
+ return name
81
113
 
82
114
  # Tool class - try direct attribute first, then model_fields
83
- if isinstance(ref, type):
84
- name = self._get_string_attr(ref, "name") or self._get_name_from_model_fields(ref)
85
- if name:
86
- return name
115
+ name = self._extract_name_from_class(ref)
116
+ if name:
117
+ return name
87
118
 
88
119
  raise ValueError(f"Cannot extract name from: {ref}")
89
120
 
90
- def _resolve_and_cache(self, ref: Any, name: str) -> Tool:
91
- """Resolve tool reference - upload if class, find if string/native.
121
+ def _cache_tool(self, tool: Tool, name: str) -> None:
122
+ """Cache a tool by name and ID if available.
92
123
 
93
124
  Args:
94
- ref: The tool reference to resolve.
95
- name: The extracted tool name.
125
+ tool: The tool to cache.
126
+ name: The tool name.
127
+ """
128
+ self._cache[name] = tool
129
+ if hasattr(tool, "id") and tool.id:
130
+ self._cache[tool.id] = tool
131
+
132
+ def _resolve_native_platform_tool(self, name: str, tool_class: type | None = None) -> Tool:
133
+ """Find a native tool on the platform and cache it.
134
+
135
+ Args:
136
+ name: The tool name to look up.
137
+ tool_class: Optional local implementation to preserve.
96
138
 
97
139
  Returns:
98
- The resolved glaip_sdk.models.Tool object.
140
+ The resolved Tool object.
99
141
 
100
142
  Raises:
101
- ValueError: If the tool cannot be resolved.
143
+ ValueError: If the tool is not found on the platform.
102
144
  """
103
- # Lazy imports to avoid circular dependency
104
145
  from glaip_sdk.utils.discovery import find_tool # noqa: PLC0415
146
+
147
+ logger.info("Looking up native tool: %s", name)
148
+ tool = find_tool(name)
149
+ if tool:
150
+ # Preserve local implementation if provided
151
+ if tool_class:
152
+ tool.tool_class = tool_class
153
+ self._cache_tool(tool, name)
154
+ return tool
155
+
156
+ raise ValueError(
157
+ f"Native tool '{name}' not found on platform. Ensure the tool is deployed or check for name mismatches."
158
+ )
159
+
160
+ def _resolve_tool_instance(self, ref: Any, name: str) -> Tool | None:
161
+ """Resolve a ToolClass instance.
162
+
163
+ Args:
164
+ ref: The ToolClass instance to resolve.
165
+ name: The extracted tool name.
166
+
167
+ Returns:
168
+ The resolved tool, or None if not a ToolClass instance.
169
+ """
170
+ # Lazy imports to avoid circular dependency: Tool -> ToolRegistry -> Tool
171
+ from glaip_sdk.tools.base import Tool as ToolClass # noqa: PLC0415
172
+ from glaip_sdk.tools.base import ToolType # noqa: PLC0415
173
+
174
+ # Use try/except to handle mocked Tool class in tests
175
+ try:
176
+ is_tool_instance = isinstance(ref, ToolClass)
177
+ except TypeError:
178
+ return None
179
+
180
+ if not is_tool_instance:
181
+ return None
182
+
183
+ # If Tool has an ID, it's already deployed - return as-is
184
+ if ref.id is not None:
185
+ logger.debug("Caching already deployed tool: %s", name)
186
+ self._cache_tool(ref, name)
187
+ return ref
188
+
189
+ # Tool.from_native() - look up on platform
190
+ if ref.tool_type == ToolType.NATIVE:
191
+ return self._resolve_native_platform_tool(name, tool_class=getattr(ref, "tool_class", None))
192
+
193
+ # Tool.from_langchain() - resolve the inner tool_class (promoted or uploaded)
194
+ if ref.tool_class is not None:
195
+ return self._resolve_custom_tool(ref.tool_class, name)
196
+
197
+ # Unresolvable Tool instance - neither native nor has tool_class
198
+ raise ValueError(
199
+ f"Cannot resolve Tool instance: {ref}. "
200
+ f"Tool has no id, is not NATIVE type, and has no tool_class. "
201
+ f"Ensure Tool is created via Tool.from_native() or Tool.from_langchain()."
202
+ )
203
+
204
+ def _resolve_deployed_tool(self, ref: Any, name: str) -> Tool | None:
205
+ """Resolve an already deployed tool (has id/name attributes).
206
+
207
+ Args:
208
+ ref: The tool reference to resolve.
209
+ name: The extracted tool name.
210
+
211
+ Returns:
212
+ The resolved tool, or None if not a deployed tool.
213
+ """
214
+ # Already deployed tool (not a ToolClass, but has id/name)
215
+ # This handles API response objects and backward compatibility
216
+ if not (hasattr(ref, "id") and hasattr(ref, "name") and not isinstance(ref, type)):
217
+ return None
218
+
219
+ if ref.id is not None:
220
+ logger.debug("Caching already deployed tool: %s", name)
221
+ # Use _cache_tool to cache by both name and ID for consistency
222
+ self._cache_tool(ref, name)
223
+ return ref
224
+
225
+ # Tool without ID (backward compatibility) - look up on platform
226
+ return self._resolve_native_platform_tool(name)
227
+
228
+ def _resolve_custom_tool(self, ref: Any, name: str) -> Tool | None:
229
+ """Resolve a custom tool class, promoting aip_agents.tools classes to NATIVE.
230
+
231
+ This method handles two main paths:
232
+ 1. **Promotion**: If the tool class is from `aip_agents.tools`, it is automatically
233
+ promoted to a `NATIVE` tool type. It then performs a platform lookup to link it
234
+ with the deployed native tool while preserving the local `tool_class` for local execution.
235
+ 2. **Upload**: If it is a standard LangChain tool, it is uploaded to the platform
236
+ as a custom tool.
237
+
238
+ Args:
239
+ ref: The tool reference (usually a class) to resolve.
240
+ name: The extracted tool name.
241
+
242
+ Returns:
243
+ The resolved tool, or None if not a custom tool.
244
+ """
245
+ # aip_agents tools are automatically promoted to NATIVE
246
+ if self._is_aip_agents_tool(ref):
247
+ from glaip_sdk.utils.tool_detection import get_tool_name # noqa: PLC0415
248
+
249
+ # Get name from class attribute or field
250
+ tool_name = get_tool_name(ref)
251
+ if tool_name is None:
252
+ raise ValueError(f"Tool class {ref.__name__} has no 'name' attribute")
253
+
254
+ return self._resolve_native_platform_tool(tool_name, tool_class=ref)
255
+
256
+ if not self._is_custom_tool(ref):
257
+ return None
258
+
259
+ # Regular custom tools - upload to platform
105
260
  from glaip_sdk.utils.sync import update_or_create_tool # noqa: PLC0415
106
261
 
107
- # Already deployed tool (glaip_sdk.models.Tool with ID) - just cache and return
108
- if hasattr(ref, "id") and hasattr(ref, "name") and not isinstance(ref, type):
109
- if ref.id is not None:
110
- logger.debug("Caching already deployed tool: %s", name)
111
- self._cache[name] = ref
112
- return ref
262
+ logger.info("Uploading custom tool: %s", name)
263
+ tool = update_or_create_tool(ref)
264
+
265
+ # Cache the resolved tool
266
+ self._cache_tool(tool, name)
267
+ if hasattr(tool, "id") and tool.id:
268
+ self._cache[tool.id] = tool
269
+
270
+ return tool
113
271
 
114
- # Tool without ID (e.g., Tool.from_native()) - look up on platform
115
- logger.info("Looking up native tool: %s", name)
116
- tool = find_tool(name)
117
- if tool:
118
- self._cache[name] = tool
119
- return tool
120
- raise ValueError(f"Native tool not found on platform: {name}")
121
-
122
- # Custom tool class - upload it
123
- if self._is_custom_tool(ref):
124
- logger.info("Uploading custom tool: %s", name)
125
- tool = update_or_create_tool(ref)
126
- self._cache[name] = tool
127
- if tool.id:
128
- self._cache[tool.id] = tool
272
+ def _resolve_dict_tool(self, ref: Any, name: str) -> Tool | None:
273
+ """Resolve a tool from a dict (API response).
274
+
275
+ Args:
276
+ ref: The dict to resolve.
277
+ name: The extracted tool name.
278
+
279
+ Returns:
280
+ The resolved tool, or None if not a dict.
281
+ """
282
+ # Lazy imports to avoid circular dependency
283
+ from glaip_sdk.tools.base import Tool as ToolClass # noqa: PLC0415
284
+
285
+ if not isinstance(ref, dict):
286
+ return None
287
+
288
+ tool_id = ref.get("id")
289
+ if tool_id:
290
+ tool = ToolClass(id=tool_id, name=ref.get("name", ""))
291
+ # Use _cache_tool to cache by both name and ID for consistency
292
+ self._cache_tool(tool, name)
129
293
  return tool
294
+ raise ValueError(f"Tool dict missing 'id': {ref}")
130
295
 
131
- # Dict from API response - use ID directly if available
132
- if isinstance(ref, dict):
133
- tool_id = ref.get("id")
134
- if tool_id:
135
- from glaip_sdk.tools.base import Tool # noqa: PLC0415
296
+ def _resolve_string_tool(self, ref: Any, name: str) -> Tool | None:
297
+ """Resolve a tool from a string name.
136
298
 
137
- tool = Tool(id=tool_id, name=ref.get("name", ""))
138
- self._cache[name] = tool
139
- return tool
140
- raise ValueError(f"Tool dict missing 'id': {ref}")
299
+ Args:
300
+ ref: The string to resolve.
301
+ name: The extracted tool name.
141
302
 
142
- # String name - look up on platform (could be native or existing tool)
143
- if isinstance(ref, str):
144
- logger.info("Looking up tool by name: %s", name)
145
- tool = find_tool(name)
146
- if tool:
147
- self._cache[name] = tool
148
- return tool
149
- raise ValueError(f"Tool not found on platform: {name}")
303
+ Returns:
304
+ The resolved tool, or None if not a string.
305
+ """
306
+ if not isinstance(ref, str):
307
+ return None
308
+
309
+ return self._resolve_native_platform_tool(name)
310
+
311
+ def _resolve_and_cache(self, ref: Any, name: str) -> Tool:
312
+ """Resolve tool reference - upload if class, find if string/native.
313
+
314
+ Args:
315
+ ref: The tool reference to resolve.
316
+ name: The extracted tool name.
317
+
318
+ Returns:
319
+ The resolved glaip_sdk.models.Tool object.
320
+
321
+ Raises:
322
+ ValueError: If tool cannot be resolved.
323
+ """
324
+ # Try each resolution strategy in order
325
+ resolvers = [
326
+ self._resolve_tool_instance,
327
+ self._resolve_deployed_tool,
328
+ self._resolve_custom_tool,
329
+ self._resolve_dict_tool,
330
+ self._resolve_string_tool,
331
+ ]
332
+
333
+ for resolver in resolvers:
334
+ result = resolver(ref, name)
335
+ if result is not None:
336
+ return result
150
337
 
151
338
  raise ValueError(f"Could not resolve tool reference: {ref}")
152
339
 
340
+ def _is_aip_agents_tool(self, ref: Any) -> bool:
341
+ """Check if reference is an aip-agents tool.
342
+
343
+ Args:
344
+ ref: The reference to check.
345
+
346
+ Returns:
347
+ True if ref is from aip_agents.tools package.
348
+ """
349
+ try:
350
+ from glaip_sdk.utils.tool_detection import ( # noqa: PLC0415
351
+ is_aip_agents_tool,
352
+ )
353
+ except ImportError:
354
+ return False
355
+
356
+ return is_aip_agents_tool(ref)
357
+
153
358
  def _is_custom_tool(self, ref: Any) -> bool:
154
359
  """Check if reference is a custom tool class/instance.
155
360
 
@@ -163,10 +368,18 @@ class ToolRegistry(BaseRegistry["Tool"]):
163
368
  from glaip_sdk.utils.tool_detection import ( # noqa: PLC0415
164
369
  is_langchain_tool,
165
370
  )
371
+
372
+ is_tool = is_langchain_tool(ref)
166
373
  except ImportError:
374
+ is_tool = hasattr(ref, "args_schema") or hasattr(ref, "_run")
375
+ if is_tool:
376
+ logger.warning("tool_detection module missing; identifying tool via fallback attributes.")
377
+
378
+ # aip_agents tools are NOT custom - they're native
379
+ if is_tool and self._is_aip_agents_tool(ref):
167
380
  return False
168
381
 
169
- return is_langchain_tool(ref)
382
+ return is_tool
170
383
 
171
384
  def resolve(self, ref: Any) -> Tool:
172
385
  """Resolve a tool reference to a platform Tool object.
@@ -185,9 +398,9 @@ class ToolRegistry(BaseRegistry["Tool"]):
185
398
  if ref.id is not None:
186
399
  name = self._extract_name(ref)
187
400
  if name not in self._cache:
188
- self._cache[name] = ref
401
+ # Use _cache_tool to cache by both name and ID for consistency
402
+ self._cache_tool(ref, name)
189
403
  return ref
190
-
191
404
  # Tool without ID (e.g., from Tool.from_native()) - needs platform lookup
192
405
  # Fall through to normal resolution
193
406
 
@@ -19,18 +19,19 @@ Example:
19
19
  >>> result = runner.run(agent, "Hello!")
20
20
  """
21
21
 
22
+ from typing import TYPE_CHECKING, Any
23
+
22
24
  from glaip_sdk.runner.deps import (
23
25
  LOCAL_RUNTIME_AVAILABLE,
24
26
  check_local_runtime_available,
25
27
  get_local_runtime_missing_message,
26
28
  )
27
- from glaip_sdk.runner.langgraph import LangGraphRunner
28
29
 
29
30
  # Default runner instance
30
- _default_runner: LangGraphRunner | None = None
31
+ _default_runner: Any | None = None
31
32
 
32
33
 
33
- def get_default_runner() -> LangGraphRunner:
34
+ def get_default_runner() -> Any:
34
35
  """Get the default runner instance for local agent execution.
35
36
 
36
37
  Returns:
@@ -45,11 +46,17 @@ def get_default_runner() -> LangGraphRunner:
45
46
  raise RuntimeError(get_local_runtime_missing_message())
46
47
 
47
48
  if _default_runner is None:
49
+ # Lazy import to avoid requiring aip-agents when runner is not used
50
+ from glaip_sdk.runner.langgraph import LangGraphRunner # noqa: PLC0415
51
+
48
52
  _default_runner = LangGraphRunner()
49
53
 
50
54
  return _default_runner
51
55
 
52
56
 
57
+ if TYPE_CHECKING:
58
+ from glaip_sdk.runner.langgraph import LangGraphRunner
59
+
53
60
  __all__ = [
54
61
  "LOCAL_RUNTIME_AVAILABLE",
55
62
  "LangGraphRunner",
@@ -57,3 +64,13 @@ __all__ = [
57
64
  "get_default_runner",
58
65
  "get_local_runtime_missing_message",
59
66
  ]
67
+
68
+
69
+ def __getattr__(name: str) -> Any:
70
+ """Lazy import for LangGraphRunner to avoid requiring aip-agents when not used."""
71
+ if name == "LangGraphRunner":
72
+ from glaip_sdk.runner.langgraph import LangGraphRunner # noqa: PLC0415
73
+
74
+ globals()["LangGraphRunner"] = LangGraphRunner
75
+ return LangGraphRunner
76
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
glaip_sdk/runner/deps.py CHANGED
@@ -31,7 +31,10 @@ def _probe_aip_agents_import() -> bool:
31
31
  Returns:
32
32
  True if aip_agents appears importable, False otherwise.
33
33
  """
34
- return importlib.util.find_spec("aip_agents") is not None
34
+ try:
35
+ return importlib.util.find_spec("aip_agents") is not None
36
+ except (ImportError, ValueError):
37
+ return False
35
38
 
36
39
 
37
40
  def check_local_runtime_available() -> bool: