glaip-sdk 0.6.5b3__py3-none-any.whl → 0.7.17__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 (145) hide show
  1. glaip_sdk/__init__.py +42 -5
  2. glaip_sdk/agents/base.py +362 -39
  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 +15 -12
  17. glaip_sdk/cli/commands/configure.py +2 -3
  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/core/output.py +12 -7
  46. glaip_sdk/cli/entrypoint.py +20 -0
  47. glaip_sdk/cli/main.py +127 -39
  48. glaip_sdk/cli/pager.py +3 -3
  49. glaip_sdk/cli/resolution.py +2 -1
  50. glaip_sdk/cli/slash/accounts_controller.py +112 -32
  51. glaip_sdk/cli/slash/agent_session.py +5 -2
  52. glaip_sdk/cli/slash/prompt.py +11 -0
  53. glaip_sdk/cli/slash/remote_runs_controller.py +3 -1
  54. glaip_sdk/cli/slash/session.py +375 -25
  55. glaip_sdk/cli/slash/tui/__init__.py +28 -1
  56. glaip_sdk/cli/slash/tui/accounts.tcss +97 -6
  57. glaip_sdk/cli/slash/tui/accounts_app.py +1107 -126
  58. glaip_sdk/cli/slash/tui/clipboard.py +195 -0
  59. glaip_sdk/cli/slash/tui/context.py +92 -0
  60. glaip_sdk/cli/slash/tui/indicators.py +341 -0
  61. glaip_sdk/cli/slash/tui/keybind_registry.py +235 -0
  62. glaip_sdk/cli/slash/tui/layouts/__init__.py +14 -0
  63. glaip_sdk/cli/slash/tui/layouts/harlequin.py +184 -0
  64. glaip_sdk/cli/slash/tui/loading.py +43 -21
  65. glaip_sdk/cli/slash/tui/remote_runs_app.py +152 -20
  66. glaip_sdk/cli/slash/tui/terminal.py +407 -0
  67. glaip_sdk/cli/slash/tui/theme/__init__.py +15 -0
  68. glaip_sdk/cli/slash/tui/theme/catalog.py +79 -0
  69. glaip_sdk/cli/slash/tui/theme/manager.py +112 -0
  70. glaip_sdk/cli/slash/tui/theme/tokens.py +55 -0
  71. glaip_sdk/cli/slash/tui/toast.py +388 -0
  72. glaip_sdk/cli/transcript/history.py +1 -1
  73. glaip_sdk/cli/transcript/viewer.py +5 -3
  74. glaip_sdk/cli/tui_settings.py +125 -0
  75. glaip_sdk/cli/update_notifier.py +215 -7
  76. glaip_sdk/cli/validators.py +1 -1
  77. glaip_sdk/client/__init__.py +2 -1
  78. glaip_sdk/client/_schedule_payloads.py +89 -0
  79. glaip_sdk/client/agents.py +290 -16
  80. glaip_sdk/client/base.py +25 -0
  81. glaip_sdk/client/hitl.py +136 -0
  82. glaip_sdk/client/main.py +7 -5
  83. glaip_sdk/client/mcps.py +44 -13
  84. glaip_sdk/client/payloads/agent/__init__.py +23 -0
  85. glaip_sdk/client/{_agent_payloads.py → payloads/agent/requests.py} +28 -48
  86. glaip_sdk/client/payloads/agent/responses.py +43 -0
  87. glaip_sdk/client/run_rendering.py +414 -3
  88. glaip_sdk/client/schedules.py +439 -0
  89. glaip_sdk/client/tools.py +57 -26
  90. glaip_sdk/config/constants.py +22 -2
  91. glaip_sdk/guardrails/__init__.py +80 -0
  92. glaip_sdk/guardrails/serializer.py +89 -0
  93. glaip_sdk/hitl/__init__.py +48 -0
  94. glaip_sdk/hitl/base.py +64 -0
  95. glaip_sdk/hitl/callback.py +43 -0
  96. glaip_sdk/hitl/local.py +121 -0
  97. glaip_sdk/hitl/remote.py +523 -0
  98. glaip_sdk/models/__init__.py +47 -1
  99. glaip_sdk/models/_provider_mappings.py +101 -0
  100. glaip_sdk/models/_validation.py +97 -0
  101. glaip_sdk/models/agent.py +2 -1
  102. glaip_sdk/models/agent_runs.py +2 -1
  103. glaip_sdk/models/constants.py +141 -0
  104. glaip_sdk/models/model.py +170 -0
  105. glaip_sdk/models/schedule.py +224 -0
  106. glaip_sdk/payload_schemas/agent.py +1 -0
  107. glaip_sdk/payload_schemas/guardrails.py +34 -0
  108. glaip_sdk/registry/tool.py +273 -66
  109. glaip_sdk/runner/__init__.py +76 -0
  110. glaip_sdk/runner/base.py +84 -0
  111. glaip_sdk/runner/deps.py +115 -0
  112. glaip_sdk/runner/langgraph.py +1055 -0
  113. glaip_sdk/runner/logging_config.py +77 -0
  114. glaip_sdk/runner/mcp_adapter/__init__.py +13 -0
  115. glaip_sdk/runner/mcp_adapter/base_mcp_adapter.py +43 -0
  116. glaip_sdk/runner/mcp_adapter/langchain_mcp_adapter.py +257 -0
  117. glaip_sdk/runner/mcp_adapter/mcp_config_builder.py +116 -0
  118. glaip_sdk/runner/tool_adapter/__init__.py +18 -0
  119. glaip_sdk/runner/tool_adapter/base_tool_adapter.py +44 -0
  120. glaip_sdk/runner/tool_adapter/langchain_tool_adapter.py +242 -0
  121. glaip_sdk/schedules/__init__.py +22 -0
  122. glaip_sdk/schedules/base.py +291 -0
  123. glaip_sdk/tools/base.py +67 -14
  124. glaip_sdk/utils/__init__.py +1 -0
  125. glaip_sdk/utils/a2a/__init__.py +34 -0
  126. glaip_sdk/utils/a2a/event_processor.py +188 -0
  127. glaip_sdk/utils/agent_config.py +8 -2
  128. glaip_sdk/utils/bundler.py +138 -2
  129. glaip_sdk/utils/import_resolver.py +43 -11
  130. glaip_sdk/utils/rendering/renderer/base.py +58 -0
  131. glaip_sdk/utils/runtime_config.py +120 -0
  132. glaip_sdk/utils/sync.py +31 -11
  133. glaip_sdk/utils/tool_detection.py +301 -0
  134. glaip_sdk/utils/tool_storage_provider.py +140 -0
  135. {glaip_sdk-0.6.5b3.dist-info → glaip_sdk-0.7.17.dist-info}/METADATA +49 -38
  136. glaip_sdk-0.7.17.dist-info/RECORD +224 -0
  137. {glaip_sdk-0.6.5b3.dist-info → glaip_sdk-0.7.17.dist-info}/WHEEL +2 -1
  138. glaip_sdk-0.7.17.dist-info/entry_points.txt +2 -0
  139. glaip_sdk-0.7.17.dist-info/top_level.txt +1 -0
  140. glaip_sdk/cli/commands/agents.py +0 -1509
  141. glaip_sdk/cli/commands/mcps.py +0 -1356
  142. glaip_sdk/cli/commands/tools.py +0 -576
  143. glaip_sdk/cli/utils.py +0 -263
  144. glaip_sdk-0.6.5b3.dist-info/RECORD +0 -145
  145. glaip_sdk-0.6.5b3.dist-info/entry_points.txt +0 -3
@@ -0,0 +1,34 @@
1
+ """A2A (Agent-to-Agent) event processing utilities.
2
+
3
+ This module provides utilities for processing A2A stream events emitted by
4
+ agent execution backends. Used by the runner module and CLI rendering.
5
+
6
+ Authors:
7
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
8
+ """
9
+
10
+ from glaip_sdk.utils.a2a.event_processor import (
11
+ EVENT_TYPE_ERROR,
12
+ EVENT_TYPE_FINAL_RESPONSE,
13
+ EVENT_TYPE_STATUS_UPDATE,
14
+ EVENT_TYPE_TOOL_CALL,
15
+ EVENT_TYPE_TOOL_RESULT,
16
+ A2AEventStreamProcessor,
17
+ extract_final_response,
18
+ get_event_type,
19
+ is_error_event,
20
+ is_tool_event,
21
+ )
22
+
23
+ __all__ = [
24
+ "A2AEventStreamProcessor",
25
+ "EVENT_TYPE_ERROR",
26
+ "EVENT_TYPE_FINAL_RESPONSE",
27
+ "EVENT_TYPE_STATUS_UPDATE",
28
+ "EVENT_TYPE_TOOL_CALL",
29
+ "EVENT_TYPE_TOOL_RESULT",
30
+ "extract_final_response",
31
+ "get_event_type",
32
+ "is_error_event",
33
+ "is_tool_event",
34
+ ]
@@ -0,0 +1,188 @@
1
+ """A2A event stream processing utilities.
2
+
3
+ This module provides helpers for processing the A2AEvent stream emitted by
4
+ agent execution backends (e.g., `arun_a2a_stream()`).
5
+
6
+ The MVP implementation focuses on extracting final response text;
7
+ full A2AConnector-equivalent normalization is deferred to follow-up PRs.
8
+
9
+ Authors:
10
+ Christian Trisno Sen Long Chen (christian.t.s.l.chen@gdplabs.id)
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from dataclasses import dataclass
16
+ from typing import Any
17
+
18
+ from gllm_core.utils import LoggerManager
19
+
20
+ logger = LoggerManager().get_logger(__name__)
21
+
22
+ # A2A event type constants (matching aip_agents.schema.a2a.A2AStreamEventType)
23
+ EVENT_TYPE_FINAL_RESPONSE = "final_response"
24
+ EVENT_TYPE_STATUS_UPDATE = "status_update"
25
+ EVENT_TYPE_TOOL_CALL = "tool_call"
26
+ EVENT_TYPE_TOOL_RESULT = "tool_result"
27
+ EVENT_TYPE_ERROR = "error"
28
+
29
+
30
+ @dataclass(frozen=True, slots=True)
31
+ class A2AEventStreamProcessor:
32
+ """Processor for `arun_a2a_stream()` event dictionaries.
33
+
34
+ The SDK uses lightweight dictionaries to represent A2A stream events.
35
+ This helper centralizes event-type normalization and MVP final-text extraction.
36
+
37
+ Example:
38
+ >>> processor = A2AEventStreamProcessor()
39
+ >>> events = [{"event_type": "final_response", "content": "Hello!", "is_final": True}]
40
+ >>> result = processor.extract_final_response(events)
41
+ >>> print(result)
42
+ Hello!
43
+ """
44
+
45
+ def extract_final_response(self, events: list[dict[str, Any]]) -> str:
46
+ """Extract the final response text from a list of A2AEvents.
47
+
48
+ Scans the event list for the final_response event and returns its content.
49
+ If no final_response is found, raises a RuntimeError.
50
+
51
+ Args:
52
+ events: List of A2AEvent dictionaries from arun_a2a_stream().
53
+
54
+ Returns:
55
+ The content string from the final_response event.
56
+
57
+ Raises:
58
+ RuntimeError: If no final_response event is found in the stream.
59
+ """
60
+ for event in reversed(events):
61
+ if self._is_final_response_event(event):
62
+ content = event.get("content", "")
63
+ logger.debug("Extracted final response: %d characters", len(str(content)))
64
+ return str(content)
65
+
66
+ # Fallback: check for events with is_final=True
67
+ for event in reversed(events):
68
+ if event.get("is_final", False):
69
+ content = event.get("content", "")
70
+ if content:
71
+ logger.debug("Extracted final from is_final flag: %d chars", len(str(content)))
72
+ return str(content)
73
+
74
+ raise RuntimeError(
75
+ "No final response received from the agent. The agent execution completed without producing a final answer."
76
+ )
77
+
78
+ def get_event_type(self, event: dict[str, Any]) -> str:
79
+ """Get the normalized event type string from an A2AEvent.
80
+
81
+ Args:
82
+ event: An A2AEvent dictionary.
83
+
84
+ Returns:
85
+ The event type as a lowercase string.
86
+ """
87
+ event_type = event.get("event_type", "unknown")
88
+ if isinstance(event_type, str):
89
+ return event_type.lower()
90
+ # Handle enum types (A2AStreamEventType)
91
+ return getattr(event_type, "value", str(event_type)).lower()
92
+
93
+ def is_tool_event(self, event: dict[str, Any]) -> bool:
94
+ """Check if an event is a tool-related event.
95
+
96
+ Args:
97
+ event: An A2AEvent dictionary.
98
+
99
+ Returns:
100
+ True if this is a tool_call or tool_result event.
101
+ """
102
+ event_type = self.get_event_type(event)
103
+ return event_type in (EVENT_TYPE_TOOL_CALL, EVENT_TYPE_TOOL_RESULT)
104
+
105
+ def is_error_event(self, event: dict[str, Any]) -> bool:
106
+ """Check if an event is an error event.
107
+
108
+ Args:
109
+ event: An A2AEvent dictionary.
110
+
111
+ Returns:
112
+ True if this is an error event.
113
+ """
114
+ return self.get_event_type(event) == EVENT_TYPE_ERROR
115
+
116
+ def _is_final_response_event(self, event: dict[str, Any]) -> bool:
117
+ """Check if an event is a final_response event.
118
+
119
+ Args:
120
+ event: An A2AEvent dictionary.
121
+
122
+ Returns:
123
+ True if this is a final_response event, False otherwise.
124
+ """
125
+ return self.get_event_type(event) == EVENT_TYPE_FINAL_RESPONSE
126
+
127
+
128
+ # Default processor instance for convenience functions
129
+ _DEFAULT_PROCESSOR = A2AEventStreamProcessor()
130
+
131
+
132
+ def extract_final_response(events: list[dict[str, Any]]) -> str:
133
+ """Extract the final response text from a list of A2AEvents.
134
+
135
+ Convenience function that uses the default A2AEventStreamProcessor.
136
+
137
+ Args:
138
+ events: List of A2AEvent dictionaries from arun_a2a_stream().
139
+
140
+ Returns:
141
+ The content string from the final_response event.
142
+
143
+ Raises:
144
+ RuntimeError: If no final_response event is found in the stream.
145
+ """
146
+ return _DEFAULT_PROCESSOR.extract_final_response(events)
147
+
148
+
149
+ def get_event_type(event: dict[str, Any]) -> str:
150
+ """Get the normalized event type string from an A2AEvent.
151
+
152
+ Convenience function that uses the default A2AEventStreamProcessor.
153
+
154
+ Args:
155
+ event: An A2AEvent dictionary.
156
+
157
+ Returns:
158
+ The event type as a lowercase string.
159
+ """
160
+ return _DEFAULT_PROCESSOR.get_event_type(event)
161
+
162
+
163
+ def is_tool_event(event: dict[str, Any]) -> bool:
164
+ """Check if an event is a tool-related event.
165
+
166
+ Convenience function that uses the default A2AEventStreamProcessor.
167
+
168
+ Args:
169
+ event: An A2AEvent dictionary.
170
+
171
+ Returns:
172
+ True if this is a tool_call or tool_result event.
173
+ """
174
+ return _DEFAULT_PROCESSOR.is_tool_event(event)
175
+
176
+
177
+ def is_error_event(event: dict[str, Any]) -> bool:
178
+ """Check if an event is an error event.
179
+
180
+ Convenience function that uses the default A2AEventStreamProcessor.
181
+
182
+ Args:
183
+ event: An A2AEvent dictionary.
184
+
185
+ Returns:
186
+ True if this is an error event.
187
+ """
188
+ return _DEFAULT_PROCESSOR.is_error_event(event)
@@ -83,7 +83,9 @@ def resolve_language_model_selection(merged_data: dict[str, Any], cli_model: str
83
83
  """
84
84
  # Priority 1: CLI --model flag
85
85
  if cli_model:
86
- return {"model": cli_model}, False
86
+ from glaip_sdk.models._validation import _validate_model # noqa: PLC0415
87
+
88
+ return {"model": _validate_model(cli_model)}, False
87
89
 
88
90
  # Priority 2: language_model_id from import
89
91
  if merged_data.get("language_model_id"):
@@ -92,7 +94,11 @@ def resolve_language_model_selection(merged_data: dict[str, Any], cli_model: str
92
94
  # Priority 3: Legacy lm_name from agent_config
93
95
  agent_config = merged_data.get("agent_config") or {}
94
96
  if isinstance(agent_config, dict) and agent_config.get("lm_name"):
95
- return {"model": agent_config["lm_name"]}, True # Strip LM identity when extracting from agent_config
97
+ from glaip_sdk.models._validation import _validate_model # noqa: PLC0415
98
+
99
+ return {
100
+ "model": _validate_model(agent_config["lm_name"])
101
+ }, True # Strip LM identity when extracting from agent_config
96
102
 
97
103
  # No LM selection found
98
104
  return {}, False
@@ -14,6 +14,7 @@ import inspect
14
14
  from pathlib import Path
15
15
 
16
16
  from glaip_sdk.utils.import_resolver import ImportResolver
17
+ from glaip_sdk.utils.tool_detection import is_tool_plugin_decorator
17
18
 
18
19
 
19
20
  class ToolBundler:
@@ -50,9 +51,14 @@ class ToolBundler:
50
51
  self.tool_dir = self.tool_file.parent
51
52
  self._import_resolver = ImportResolver(self.tool_dir)
52
53
 
53
- def bundle(self) -> str:
54
+ def bundle(self, add_tool_plugin_decorator: bool = True) -> str:
54
55
  """Bundle tool source code with inlined local imports.
55
56
 
57
+ Args:
58
+ add_tool_plugin_decorator: If True, add @tool_plugin decorator to BaseTool classes.
59
+ Set to False for newer servers (0.1.85+) where decorator is optional.
60
+ Defaults to True for backward compatibility with older servers.
61
+
56
62
  Returns:
57
63
  Bundled source code with all local dependencies inlined.
58
64
  """
@@ -62,6 +68,16 @@ class ToolBundler:
62
68
  tree = ast.parse(full_source)
63
69
  local_imports, external_imports = self._import_resolver.categorize_imports(tree)
64
70
 
71
+ # NOTE: The @tool_plugin decorator is REQUIRED by older servers (< 0.1.85) for remote execution.
72
+ # Newer servers (0.1.85+) make the decorator optional.
73
+ # The server validates uploaded tool code and will reject tools without the decorator
74
+ # with error: "No classes found with @tool_plugin decorator".
75
+ # See: docs/resources/reference/schemas/tools.md - "Plugin Requirements"
76
+ # TESTED: Commenting out this decorator addition causes HTTP 400 ValidationError from older servers.
77
+ # We try without decorator first (for new servers), then retry with decorator if validation fails.
78
+ if add_tool_plugin_decorator:
79
+ self._add_tool_plugin_decorator(tree)
80
+
65
81
  # Extract main code nodes (excluding imports, docstrings, glaip_sdk.Tool subclasses)
66
82
  main_code_nodes = self._extract_main_code_nodes(tree)
67
83
 
@@ -71,6 +87,13 @@ class ToolBundler:
71
87
  # Merge all external imports
72
88
  all_external_imports = external_imports + inlined_external_imports
73
89
 
90
+ # NOTE: The gllm_plugin.tools import is REQUIRED when decorator is added.
91
+ # Without this import, the decorator will cause a NameError when the server executes the code.
92
+ # TESTED: Commenting out this import causes NameError when server tries to use the decorator.
93
+ # This import is added automatically during bundling so source files can remain clean.
94
+ if add_tool_plugin_decorator:
95
+ self._ensure_tool_plugin_import(all_external_imports)
96
+
74
97
  # Build bundled code
75
98
  bundled_code = ["# Bundled tool with inlined local imports\n"]
76
99
  bundled_code.extend(self._import_resolver.format_external_imports(all_external_imports))
@@ -109,6 +132,103 @@ class ToolBundler:
109
132
  main_code_nodes.append(ast.unparse(node))
110
133
  return main_code_nodes
111
134
 
135
+ @staticmethod
136
+ def _add_tool_plugin_decorator(tree: ast.AST) -> None:
137
+ """Add @tool_plugin decorator to BaseTool classes that don't have it.
138
+
139
+ This allows tools to be clean (without decorator) for local use,
140
+ while the decorator is automatically added during bundling for remote execution.
141
+
142
+ Args:
143
+ tree: AST tree to modify in-place.
144
+ """
145
+ for node in ast.walk(tree):
146
+ if not isinstance(node, ast.ClassDef):
147
+ continue
148
+
149
+ if not ToolBundler._inherits_from_base_tool(node):
150
+ continue
151
+
152
+ if ToolBundler._has_tool_plugin_decorator(node):
153
+ continue
154
+
155
+ decorator_call = ToolBundler._create_tool_plugin_decorator()
156
+ node.decorator_list.insert(0, decorator_call)
157
+
158
+ @staticmethod
159
+ def _inherits_from_base_tool(class_node: ast.ClassDef) -> bool:
160
+ """Check if a class inherits from BaseTool.
161
+
162
+ Args:
163
+ class_node: AST ClassDef node to check.
164
+
165
+ Returns:
166
+ True if class inherits from BaseTool.
167
+ """
168
+ for base in class_node.bases:
169
+ if isinstance(base, ast.Name) and base.id == "BaseTool":
170
+ return True
171
+ if isinstance(base, ast.Attribute) and base.attr == "BaseTool":
172
+ # Handle nested attributes like langchain_core.tools.BaseTool
173
+ # Check if the value chain leads to langchain_core
174
+ value = base.value
175
+ while isinstance(value, ast.Attribute):
176
+ value = value.value
177
+ if isinstance(value, ast.Name) and value.id == "langchain_core":
178
+ return True
179
+ return False
180
+
181
+ @staticmethod
182
+ def _has_tool_plugin_decorator(class_node: ast.ClassDef) -> bool:
183
+ """Check if a class already has the @tool_plugin decorator.
184
+
185
+ Args:
186
+ class_node: AST ClassDef node to check.
187
+
188
+ Returns:
189
+ True if decorator already exists.
190
+ """
191
+ for decorator in class_node.decorator_list:
192
+ if is_tool_plugin_decorator(decorator):
193
+ return True
194
+ return False
195
+
196
+ @staticmethod
197
+ def _create_tool_plugin_decorator() -> ast.Call:
198
+ """Create a @tool_plugin decorator AST node.
199
+
200
+ Returns:
201
+ AST Call node representing @tool_plugin(version="1.0.0").
202
+ """
203
+ return ast.Call(
204
+ func=ast.Name(id="tool_plugin", ctx=ast.Load()),
205
+ args=[],
206
+ keywords=[ast.keyword(arg="version", value=ast.Constant(value="1.0.0"))],
207
+ )
208
+
209
+ @staticmethod
210
+ def _ensure_tool_plugin_import(external_imports: list) -> None:
211
+ """Ensure gllm_plugin.tools import is present in external imports.
212
+
213
+ Args:
214
+ external_imports: List of external import nodes (modified in-place).
215
+ """
216
+ # Check if import already exists
217
+ for import_node in external_imports:
218
+ if isinstance(import_node, ast.ImportFrom) and import_node.module == "gllm_plugin.tools":
219
+ # Check if tool_plugin is in the names
220
+ for alias in import_node.names:
221
+ if alias.name == "tool_plugin":
222
+ return # Import already present
223
+
224
+ # Add the import
225
+ import_node = ast.ImportFrom(
226
+ module="gllm_plugin.tools",
227
+ names=[ast.alias(name="tool_plugin")],
228
+ level=0,
229
+ )
230
+ external_imports.append(import_node)
231
+
112
232
  @staticmethod
113
233
  def _is_sdk_tool_subclass(node: ast.ClassDef) -> bool:
114
234
  """Check if AST class definition inherits from Tool.
@@ -135,7 +255,7 @@ class ToolBundler:
135
255
  return False
136
256
 
137
257
  @classmethod
138
- def bundle_from_source(cls, file_path: Path) -> tuple[str, str, str]:
258
+ def bundle_from_source(cls, file_path: Path, add_tool_plugin_decorator: bool = True) -> tuple[str, str, str]:
139
259
  """Extract tool info directly from source file without importing.
140
260
 
141
261
  This is used as a fallback when the tool class cannot be imported
@@ -143,6 +263,9 @@ class ToolBundler:
143
263
 
144
264
  Args:
145
265
  file_path: Path to the tool source file.
266
+ add_tool_plugin_decorator: If True, add @tool_plugin decorator to BaseTool classes.
267
+ Set to False for newer servers (0.1.85+) where decorator is optional.
268
+ Defaults to True for backward compatibility with older servers.
146
269
 
147
270
  Returns:
148
271
  Tuple of (name, description, bundled_source_code).
@@ -160,6 +283,12 @@ class ToolBundler:
160
283
  tool_dir = file_path.parent
161
284
  import_resolver = ImportResolver(tool_dir)
162
285
 
286
+ # NOTE: The @tool_plugin decorator is REQUIRED by older servers (< 0.1.85) for remote execution.
287
+ # Newer servers (0.1.85+) make the decorator optional.
288
+ # See bundle() method for detailed explanation.
289
+ if add_tool_plugin_decorator:
290
+ cls._add_tool_plugin_decorator(tree)
291
+
163
292
  # Find tool name and description from class definitions
164
293
  tool_name, tool_description = cls._extract_tool_metadata(tree, file_path.stem)
165
294
 
@@ -180,6 +309,13 @@ class ToolBundler:
180
309
 
181
310
  # Build bundled code
182
311
  all_external_imports = external_imports + inlined_external_imports
312
+
313
+ # NOTE: The gllm_plugin.tools import is REQUIRED when decorator is added.
314
+ # See bundle() method for detailed explanation.
315
+ # TESTED: Commenting out this import causes NameError when server tries to use the decorator.
316
+ if add_tool_plugin_decorator:
317
+ cls._ensure_tool_plugin_import(all_external_imports)
318
+
183
319
  bundled_code = ["# Bundled tool with inlined local imports\n"]
184
320
  bundled_code.extend(import_resolver.format_external_imports(all_external_imports))
185
321
 
@@ -13,6 +13,8 @@ import ast
13
13
  import importlib
14
14
  from pathlib import Path
15
15
 
16
+ from glaip_sdk.utils.tool_detection import is_tool_plugin_decorator
17
+
16
18
 
17
19
  class ImportResolver:
18
20
  """Resolves and categorizes Python imports for tool bundling.
@@ -215,11 +217,49 @@ class ImportResolver:
215
217
  True if import should be skipped.
216
218
  """
217
219
  if isinstance(node, ast.ImportFrom):
218
- return node.module and any(node.module.startswith(m) for m in self.EXCLUDED_MODULES)
220
+ return self._should_skip_import_from(node)
219
221
  if isinstance(node, ast.Import):
220
- return any(alias.name.startswith(m) for m in self.EXCLUDED_MODULES for alias in node.names)
222
+ return self._should_skip_regular_import(node)
221
223
  return False
222
224
 
225
+ def _should_skip_import_from(self, node: ast.ImportFrom) -> bool:
226
+ """Check if ImportFrom node should be skipped.
227
+
228
+ Args:
229
+ node: ImportFrom node to check.
230
+
231
+ Returns:
232
+ True if import should be skipped.
233
+ """
234
+ if not node.module:
235
+ return False
236
+ return self._is_module_excluded(node.module)
237
+
238
+ def _should_skip_regular_import(self, node: ast.Import) -> bool:
239
+ """Check if Import node should be skipped.
240
+
241
+ Args:
242
+ node: Import node to check.
243
+
244
+ Returns:
245
+ True if any alias should be skipped.
246
+ """
247
+ return any(self._is_module_excluded(alias.name) for alias in node.names)
248
+
249
+ def _is_module_excluded(self, module_name: str) -> bool:
250
+ """Check if a module name should be excluded.
251
+
252
+ Args:
253
+ module_name: Module name to check.
254
+
255
+ Returns:
256
+ True if module is excluded.
257
+ """
258
+ # Exact match for glaip_sdk or match excluded submodules with boundary
259
+ if module_name == "glaip_sdk":
260
+ return True
261
+ return any(module_name == m or module_name.startswith(m + ".") for m in self.EXCLUDED_MODULES)
262
+
223
263
  @staticmethod
224
264
  def _build_import_strings(future_imports: list, regular_imports: list) -> list[str]:
225
265
  """Build formatted import strings from import nodes.
@@ -444,15 +484,7 @@ class ImportResolver:
444
484
  Returns:
445
485
  True if decorator is @tool_plugin.
446
486
  """
447
- if isinstance(decorator, ast.Name) and decorator.id == "tool_plugin":
448
- return True
449
- if (
450
- isinstance(decorator, ast.Call)
451
- and isinstance(decorator.func, ast.Name)
452
- and decorator.func.id == "tool_plugin"
453
- ):
454
- return True
455
- return False
487
+ return is_tool_plugin_decorator(decorator)
456
488
 
457
489
  @staticmethod
458
490
  def _filter_bases(bases: list) -> list:
@@ -8,6 +8,7 @@ from __future__ import annotations
8
8
 
9
9
  import json
10
10
  import logging
11
+ import sys
11
12
  from datetime import datetime, timezone
12
13
  from time import monotonic
13
14
  from typing import Any
@@ -349,6 +350,9 @@ class RichStreamRenderer(TranscriptModeMixin):
349
350
  self._handle_status_event(ev)
350
351
  elif kind == "content":
351
352
  self._handle_content_event(content)
353
+ elif kind == "token":
354
+ # Token events should stream content incrementally with immediate console output
355
+ self._handle_token_event(content)
352
356
  elif kind == "final_response":
353
357
  self._handle_final_response_event(content, metadata)
354
358
  elif kind in {"agent_step", "agent_thinking_step"}:
@@ -368,6 +372,31 @@ class RichStreamRenderer(TranscriptModeMixin):
368
372
  self.state.append_transcript_text(content)
369
373
  self._ensure_live()
370
374
 
375
+ def _handle_token_event(self, content: str) -> None:
376
+ """Handle token streaming events - print immediately for real-time streaming."""
377
+ if content:
378
+ self.state.append_transcript_text(content)
379
+ # Print token content directly to stdout for immediate visibility when not verbose
380
+ # This bypasses Rich's Live display which has refresh rate limitations
381
+ if not self.verbose:
382
+ try:
383
+ # Mark that we're streaming tokens directly to prevent Live display from starting
384
+ self._streaming_tokens_directly = True
385
+ # Stop Live display if active to prevent it from intercepting stdout
386
+ # and causing each token to appear on a new line
387
+ if self.live is not None:
388
+ self._stop_live_display()
389
+ # Write directly to stdout - tokens will stream on the same line
390
+ # since we're bypassing Rich's console which adds newlines
391
+ sys.stdout.write(content)
392
+ sys.stdout.flush()
393
+ except Exception:
394
+ # Fallback to live display if direct write fails
395
+ self._ensure_live()
396
+ else:
397
+ # In verbose mode, use normal live display (debug panels handle the output)
398
+ self._ensure_live()
399
+
371
400
  def _handle_final_response_event(self, content: str, metadata: dict[str, Any]) -> None:
372
401
  """Handle final response events."""
373
402
  if content:
@@ -521,6 +550,18 @@ class RichStreamRenderer(TranscriptModeMixin):
521
550
  if getattr(self, "_transcript_mode_enabled", False):
522
551
  return
523
552
 
553
+ # When verbose=False and tokens were streamed directly, skip final panel
554
+ # The user's script will print the final result, avoiding duplication
555
+ if not self.verbose and getattr(self, "_streaming_tokens_directly", False):
556
+ # Add a newline after streaming tokens for clean separation
557
+ try:
558
+ sys.stdout.write("\n")
559
+ sys.stdout.flush()
560
+ except Exception:
561
+ pass
562
+ self.state.printed_final_output = True
563
+ return
564
+
524
565
  if self.verbose:
525
566
  panel = build_final_panel(
526
567
  self.state,
@@ -597,6 +638,19 @@ class RichStreamRenderer(TranscriptModeMixin):
597
638
 
598
639
  def _finalize_display(self) -> None:
599
640
  """Finalize live display and render final output."""
641
+ # When verbose=False and tokens were streamed directly, skip live display updates
642
+ # to avoid showing duplicate final result
643
+ if not self.verbose and getattr(self, "_streaming_tokens_directly", False):
644
+ # Just add a newline after streaming tokens for clean separation
645
+ try:
646
+ sys.stdout.write("\n")
647
+ sys.stdout.flush()
648
+ except Exception:
649
+ pass
650
+ self._stop_live_display()
651
+ self.state.printed_final_output = True
652
+ return
653
+
600
654
  # Final refresh
601
655
  self._ensure_live()
602
656
 
@@ -629,6 +683,10 @@ class RichStreamRenderer(TranscriptModeMixin):
629
683
  """Ensure live display is updated."""
630
684
  if getattr(self, "_transcript_mode_enabled", False):
631
685
  return
686
+ # When verbose=False, don't start Live display if we're streaming tokens directly
687
+ # This prevents Live from intercepting stdout and causing tokens to appear on separate lines
688
+ if not self.verbose and getattr(self, "_streaming_tokens_directly", False):
689
+ return
632
690
  if not self._ensure_live_stack():
633
691
  return
634
692