glaip-sdk 0.6.5b5__py3-none-any.whl → 0.6.5b9__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.
glaip_sdk/agents/base.py CHANGED
@@ -50,6 +50,11 @@ from pathlib import Path
50
50
  from typing import TYPE_CHECKING, Any
51
51
 
52
52
  from glaip_sdk.registry import get_agent_registry, get_mcp_registry, get_tool_registry
53
+ from glaip_sdk.runner import get_default_runner
54
+ from glaip_sdk.runner.deps import (
55
+ check_local_runtime_available,
56
+ get_local_runtime_missing_message,
57
+ )
53
58
  from glaip_sdk.utils.discovery import find_agent
54
59
  from glaip_sdk.utils.resource_refs import is_uuid
55
60
  from glaip_sdk.utils.runtime_config import normalize_runtime_config_keys
@@ -893,13 +898,20 @@ class Agent:
893
898
  message: str,
894
899
  verbose: bool = False,
895
900
  runtime_config: dict[str, Any] | None = None,
901
+ chat_history: list[dict[str, str]] | None = None,
896
902
  **kwargs: Any,
897
903
  ) -> str:
898
904
  """Run the agent synchronously with a message.
899
905
 
906
+ Supports two execution modes:
907
+ - **Server-backed**: When the agent is deployed (has an ID), execution
908
+ happens via the AIP backend server.
909
+ - **Local**: When the agent is not deployed and glaip-sdk[local] is installed,
910
+ execution happens locally via aip-agents (no server required).
911
+
900
912
  Args:
901
913
  message: The message to send to the agent.
902
- verbose: If True, print streaming output to console.
914
+ verbose: If True, print streaming output to console. Defaults to False.
903
915
  runtime_config: Optional runtime configuration for tools, MCPs, and agents.
904
916
  Keys can be SDK objects, UUIDs, or names. Example:
905
917
  {
@@ -907,19 +919,43 @@ class Agent:
907
919
  "mcp_configs": {"mcp-id": {"setting": "on"}},
908
920
  "agent_config": {"planning": True},
909
921
  }
922
+ Defaults to None.
923
+ chat_history: Optional list of prior conversation messages for context.
924
+ Each message is a dict with "role" and "content" keys.
925
+ Defaults to None.
910
926
  **kwargs: Additional arguments to pass to the run API.
911
927
 
912
928
  Returns:
913
929
  The agent's response as a string.
914
930
 
915
931
  Raises:
916
- ValueError: If the agent hasn't been deployed yet.
917
- RuntimeError: If client is not available.
932
+ ValueError: If the agent is not deployed and local runtime is not available.
933
+ RuntimeError: If server-backed execution fails due to client issues.
918
934
  """
919
- agent_client, call_kwargs = self._prepare_run_kwargs(
920
- message, verbose, runtime_config or kwargs.get("runtime_config"), **kwargs
921
- )
922
- return agent_client.run_agent(**call_kwargs)
935
+ # Backend routing: deployed agents use server, undeployed use local (if available)
936
+ if self.id:
937
+ # Server-backed execution path (agent is deployed)
938
+ agent_client, call_kwargs = self._prepare_run_kwargs(
939
+ message, verbose, runtime_config or kwargs.get("runtime_config"), **kwargs
940
+ )
941
+ if chat_history is not None:
942
+ call_kwargs["chat_history"] = chat_history
943
+ return agent_client.run_agent(**call_kwargs)
944
+
945
+ # Local execution path (agent is not deployed)
946
+ if check_local_runtime_available():
947
+ runner = get_default_runner()
948
+ return runner.run(
949
+ agent=self,
950
+ message=message,
951
+ verbose=verbose,
952
+ runtime_config=runtime_config,
953
+ chat_history=chat_history,
954
+ **kwargs,
955
+ )
956
+
957
+ # Neither deployed nor local runtime available - provide actionable error
958
+ raise ValueError(f"{_AGENT_NOT_DEPLOYED_MSG}\n\n{get_local_runtime_missing_message()}")
923
959
 
924
960
  async def arun(
925
961
  self,
@@ -160,19 +160,12 @@ class ToolRegistry(BaseRegistry["Tool"]):
160
160
  True if ref is a custom tool that needs uploading.
161
161
  """
162
162
  try:
163
- from langchain_core.tools import BaseTool # noqa: PLC0415
163
+ from glaip_sdk.utils.tool_detection import is_langchain_tool # noqa: PLC0415
164
164
 
165
- # LangChain BaseTool class
166
- if isinstance(ref, type) and issubclass(ref, BaseTool):
167
- return True
168
-
169
- # LangChain BaseTool instance
170
- if isinstance(ref, BaseTool):
171
- return True
165
+ return is_langchain_tool(ref)
172
166
  except ImportError:
173
- pass
174
-
175
- return False
167
+ # Handle case where langchain_core is not available or tool_detection import fails
168
+ return False
176
169
 
177
170
  def resolve(self, ref: Any) -> Tool:
178
171
  """Resolve a tool reference to a platform Tool object.
@@ -0,0 +1,59 @@
1
+ """Local agent execution runners.
2
+
3
+ This module provides runners for executing glaip-sdk agents locally
4
+ without requiring the AIP backend server. The primary runner is
5
+ LangGraphRunner which uses the aip-agents library.
6
+
7
+ To use local execution, install with the [local] extra:
8
+ pip install "glaip-sdk[local]"
9
+
10
+ Authors:
11
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
12
+
13
+ Example:
14
+ >>> from glaip_sdk.runner import get_default_runner
15
+ >>> from glaip_sdk.agents import Agent
16
+ >>>
17
+ >>> agent = Agent(name="my-agent", instruction="You are helpful.")
18
+ >>> runner = get_default_runner()
19
+ >>> result = runner.run(agent, "Hello!")
20
+ """
21
+
22
+ from glaip_sdk.runner.deps import (
23
+ LOCAL_RUNTIME_AVAILABLE,
24
+ check_local_runtime_available,
25
+ get_local_runtime_missing_message,
26
+ )
27
+ from glaip_sdk.runner.langgraph import LangGraphRunner
28
+
29
+ # Default runner instance
30
+ _default_runner: LangGraphRunner | None = None
31
+
32
+
33
+ def get_default_runner() -> LangGraphRunner:
34
+ """Get the default runner instance for local agent execution.
35
+
36
+ Returns:
37
+ The default LangGraphRunner instance.
38
+
39
+ Raises:
40
+ RuntimeError: If local runtime dependencies are not available.
41
+ """
42
+ global _default_runner
43
+
44
+ if not check_local_runtime_available():
45
+ raise RuntimeError(get_local_runtime_missing_message())
46
+
47
+ if _default_runner is None:
48
+ _default_runner = LangGraphRunner()
49
+
50
+ return _default_runner
51
+
52
+
53
+ __all__ = [
54
+ "LOCAL_RUNTIME_AVAILABLE",
55
+ "LangGraphRunner",
56
+ "check_local_runtime_available",
57
+ "get_default_runner",
58
+ "get_local_runtime_missing_message",
59
+ ]
@@ -0,0 +1,84 @@
1
+ """Abstract base class for agent execution runners.
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
+ from abc import ABC, abstractmethod
10
+ from typing import TYPE_CHECKING, Any
11
+
12
+ if TYPE_CHECKING:
13
+ from glaip_sdk.agents.base import Agent
14
+
15
+
16
+ class BaseRunner(ABC):
17
+ """Abstract base class for agent execution runners.
18
+
19
+ Runners are responsible for executing glaip-sdk Agent instances
20
+ and returning results. Different runner implementations may use
21
+ different execution backends (LangGraph, Google ADK, etc.).
22
+ """
23
+
24
+ @abstractmethod
25
+ def run(
26
+ self,
27
+ agent: Agent,
28
+ message: str,
29
+ verbose: bool = False,
30
+ runtime_config: dict[str, Any] | None = None,
31
+ chat_history: list[dict[str, str]] | None = None,
32
+ **kwargs: Any,
33
+ ) -> str:
34
+ """Execute agent synchronously and return final response text.
35
+
36
+ Args:
37
+ agent: The glaip_sdk Agent to execute.
38
+ message: The user message to send to the agent.
39
+ verbose: If True, emit debug trace output during execution.
40
+ Defaults to False.
41
+ runtime_config: Optional runtime configuration for tools, MCPs, etc.
42
+ Defaults to None.
43
+ chat_history: Optional list of prior conversation messages.
44
+ Defaults to None.
45
+ **kwargs: Additional keyword arguments passed to the backend.
46
+
47
+ Returns:
48
+ The final response text from the agent.
49
+
50
+ Raises:
51
+ RuntimeError: If execution fails.
52
+ """
53
+ ...
54
+
55
+ @abstractmethod
56
+ async def arun(
57
+ self,
58
+ agent: Agent,
59
+ message: str,
60
+ verbose: bool = False,
61
+ runtime_config: dict[str, Any] | None = None,
62
+ chat_history: list[dict[str, str]] | None = None,
63
+ **kwargs: Any,
64
+ ) -> str:
65
+ """Execute agent asynchronously and return final response text.
66
+
67
+ Args:
68
+ agent: The glaip_sdk Agent to execute.
69
+ message: The user message to send to the agent.
70
+ verbose: If True, emit debug trace output during execution.
71
+ Defaults to False.
72
+ runtime_config: Optional runtime configuration for tools, MCPs, etc.
73
+ Defaults to None.
74
+ chat_history: Optional list of prior conversation messages.
75
+ Defaults to None.
76
+ **kwargs: Additional keyword arguments passed to the backend.
77
+
78
+ Returns:
79
+ The final response text from the agent.
80
+
81
+ Raises:
82
+ RuntimeError: If execution fails.
83
+ """
84
+ ...
@@ -0,0 +1,115 @@
1
+ """Dependency detection utilities for the runner module.
2
+
3
+ This module provides helpers to detect whether the local runtime dependencies
4
+ (aip-agents) are installed and to generate actionable error messages when they
5
+ are not available.
6
+
7
+ Authors:
8
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
9
+
10
+ Example:
11
+ >>> from glaip_sdk.runner.deps import check_local_runtime_available
12
+ >>> if not check_local_runtime_available():
13
+ ... print(get_local_runtime_missing_message())
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ from gllm_core.utils import LoggerManager
19
+
20
+ logger = LoggerManager().get_logger(__name__)
21
+
22
+ # Module-level cache for import availability check
23
+ _local_runtime_available: bool | None = None
24
+
25
+
26
+ def _probe_aip_agents_import() -> bool:
27
+ """Attempt to import aip_agents and return success status.
28
+
29
+ Returns:
30
+ True if aip_agents can be imported successfully, False otherwise.
31
+ """
32
+ try:
33
+ import aip_agents # noqa: F401, PLC0415
34
+
35
+ return True
36
+ except ImportError:
37
+ return False
38
+
39
+
40
+ def check_local_runtime_available() -> bool:
41
+ """Check if the local runtime dependencies are installed and importable.
42
+
43
+ This function probes for the aip_agents module which provides the
44
+ LangGraphReactAgent for local execution. Results are cached for efficiency.
45
+
46
+ Returns:
47
+ True if local runtime is available, False otherwise.
48
+ """
49
+ global _local_runtime_available
50
+
51
+ if _local_runtime_available is None:
52
+ _local_runtime_available = _probe_aip_agents_import()
53
+ if _local_runtime_available:
54
+ logger.debug("Local runtime dependencies (aip-agents) detected")
55
+ else:
56
+ logger.debug("Local runtime dependencies (aip-agents) not available")
57
+
58
+ return _local_runtime_available
59
+
60
+
61
+ # Cached availability flag for use in conditions without function call overhead
62
+ LOCAL_RUNTIME_AVAILABLE: bool = check_local_runtime_available()
63
+
64
+
65
+ def get_local_runtime_missing_message() -> str:
66
+ """Generate an actionable error message when local runtime is not available.
67
+
68
+ Returns:
69
+ A user-friendly message explaining how to install local runtime dependencies
70
+ or how to use server-backed execution as an alternative.
71
+ """
72
+ return (
73
+ "Local runtime dependencies are not installed. "
74
+ "To run agents locally without an AIP server, install with:\n\n"
75
+ ' pip install "glaip-sdk[local]"\n\n'
76
+ "Alternatively, call deploy() to run this agent on the AIP server."
77
+ )
78
+
79
+
80
+ def get_local_mode_not_supported_for_tool_message(tool_ref: str) -> str:
81
+ """Generate an error message for tools that cannot be used in local mode.
82
+
83
+ Args:
84
+ tool_ref: A string identifier for the tool (name, ID, or type description).
85
+
86
+ Returns:
87
+ Error message explaining that the tool type is not supported locally.
88
+ """
89
+ return (
90
+ f"Tool '{tool_ref}' cannot be used in local mode. "
91
+ "Local runtime only supports LangChain-compatible tool classes/instances "
92
+ "and Tool.from_langchain(...) references. "
93
+ "Native platform tools (Tool.from_native()) require server-backed execution "
94
+ "via deploy()."
95
+ )
96
+
97
+
98
+ def get_uuid_not_supported_message(key_type: str, uuid_value: str) -> str:
99
+ """Generate an error message when a UUID is used in local mode.
100
+
101
+ In local mode, runtime_config keys must be tool/agent/MCP objects or names,
102
+ not UUIDs which require platform registry resolution.
103
+
104
+ Args:
105
+ key_type: The type of key (e.g., "tool", "agent", "mcp").
106
+ uuid_value: The UUID that was incorrectly provided.
107
+
108
+ Returns:
109
+ Error message explaining that UUIDs are not supported in local mode.
110
+ """
111
+ return (
112
+ f"UUID-like {key_type} key '{uuid_value}' is not supported in local mode. "
113
+ f"Local runtime cannot resolve {key_type} IDs - pass the {key_type} "
114
+ f"class/instance or name instead."
115
+ )