glaip-sdk 0.1.2__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.
- glaip_sdk/__init__.py +44 -4
- glaip_sdk/_version.py +9 -0
- glaip_sdk/agents/__init__.py +27 -0
- glaip_sdk/agents/base.py +1413 -0
- glaip_sdk/branding.py +126 -2
- glaip_sdk/cli/account_store.py +555 -0
- glaip_sdk/cli/auth.py +260 -15
- glaip_sdk/cli/commands/__init__.py +2 -2
- glaip_sdk/cli/commands/accounts.py +746 -0
- glaip_sdk/cli/commands/agents/__init__.py +116 -0
- glaip_sdk/cli/commands/agents/_common.py +562 -0
- glaip_sdk/cli/commands/agents/create.py +155 -0
- glaip_sdk/cli/commands/agents/delete.py +64 -0
- glaip_sdk/cli/commands/agents/get.py +89 -0
- glaip_sdk/cli/commands/agents/list.py +129 -0
- glaip_sdk/cli/commands/agents/run.py +264 -0
- glaip_sdk/cli/commands/agents/sync_langflow.py +72 -0
- glaip_sdk/cli/commands/agents/update.py +112 -0
- glaip_sdk/cli/commands/common_config.py +104 -0
- glaip_sdk/cli/commands/configure.py +728 -113
- glaip_sdk/cli/commands/mcps/__init__.py +94 -0
- glaip_sdk/cli/commands/mcps/_common.py +459 -0
- glaip_sdk/cli/commands/mcps/connect.py +82 -0
- glaip_sdk/cli/commands/mcps/create.py +152 -0
- glaip_sdk/cli/commands/mcps/delete.py +73 -0
- glaip_sdk/cli/commands/mcps/get.py +212 -0
- glaip_sdk/cli/commands/mcps/list.py +69 -0
- glaip_sdk/cli/commands/mcps/tools.py +235 -0
- glaip_sdk/cli/commands/mcps/update.py +190 -0
- glaip_sdk/cli/commands/models.py +12 -8
- glaip_sdk/cli/commands/shared/__init__.py +21 -0
- glaip_sdk/cli/commands/shared/formatters.py +91 -0
- glaip_sdk/cli/commands/tools/__init__.py +69 -0
- glaip_sdk/cli/commands/tools/_common.py +80 -0
- glaip_sdk/cli/commands/tools/create.py +228 -0
- glaip_sdk/cli/commands/tools/delete.py +61 -0
- glaip_sdk/cli/commands/tools/get.py +103 -0
- glaip_sdk/cli/commands/tools/list.py +69 -0
- glaip_sdk/cli/commands/tools/script.py +49 -0
- glaip_sdk/cli/commands/tools/update.py +102 -0
- glaip_sdk/cli/commands/transcripts/__init__.py +90 -0
- glaip_sdk/cli/commands/transcripts/_common.py +9 -0
- glaip_sdk/cli/commands/transcripts/clear.py +5 -0
- glaip_sdk/cli/commands/transcripts/detail.py +5 -0
- glaip_sdk/cli/commands/transcripts_original.py +756 -0
- glaip_sdk/cli/commands/update.py +163 -17
- glaip_sdk/cli/config.py +49 -4
- glaip_sdk/cli/constants.py +38 -0
- glaip_sdk/cli/context.py +8 -0
- glaip_sdk/cli/core/__init__.py +79 -0
- glaip_sdk/cli/core/context.py +124 -0
- glaip_sdk/cli/core/output.py +851 -0
- glaip_sdk/cli/core/prompting.py +649 -0
- glaip_sdk/cli/core/rendering.py +187 -0
- glaip_sdk/cli/display.py +41 -20
- glaip_sdk/cli/entrypoint.py +20 -0
- glaip_sdk/cli/hints.py +57 -0
- glaip_sdk/cli/io.py +6 -3
- glaip_sdk/cli/main.py +340 -143
- glaip_sdk/cli/masking.py +21 -33
- glaip_sdk/cli/pager.py +12 -13
- glaip_sdk/cli/parsers/__init__.py +1 -3
- glaip_sdk/cli/resolution.py +2 -1
- glaip_sdk/cli/slash/__init__.py +0 -9
- glaip_sdk/cli/slash/accounts_controller.py +580 -0
- glaip_sdk/cli/slash/accounts_shared.py +75 -0
- glaip_sdk/cli/slash/agent_session.py +62 -21
- glaip_sdk/cli/slash/prompt.py +21 -0
- glaip_sdk/cli/slash/remote_runs_controller.py +568 -0
- glaip_sdk/cli/slash/session.py +1105 -153
- glaip_sdk/cli/slash/tui/__init__.py +36 -0
- glaip_sdk/cli/slash/tui/accounts.tcss +177 -0
- glaip_sdk/cli/slash/tui/accounts_app.py +1853 -0
- glaip_sdk/cli/slash/tui/background_tasks.py +72 -0
- glaip_sdk/cli/slash/tui/clipboard.py +195 -0
- glaip_sdk/cli/slash/tui/context.py +92 -0
- glaip_sdk/cli/slash/tui/indicators.py +341 -0
- glaip_sdk/cli/slash/tui/keybind_registry.py +235 -0
- glaip_sdk/cli/slash/tui/layouts/__init__.py +14 -0
- glaip_sdk/cli/slash/tui/layouts/harlequin.py +184 -0
- glaip_sdk/cli/slash/tui/loading.py +80 -0
- glaip_sdk/cli/slash/tui/remote_runs_app.py +760 -0
- glaip_sdk/cli/slash/tui/terminal.py +407 -0
- glaip_sdk/cli/slash/tui/theme/__init__.py +15 -0
- glaip_sdk/cli/slash/tui/theme/catalog.py +79 -0
- glaip_sdk/cli/slash/tui/theme/manager.py +112 -0
- glaip_sdk/cli/slash/tui/theme/tokens.py +55 -0
- glaip_sdk/cli/slash/tui/toast.py +388 -0
- glaip_sdk/cli/transcript/__init__.py +12 -52
- glaip_sdk/cli/transcript/cache.py +255 -44
- glaip_sdk/cli/transcript/capture.py +66 -1
- glaip_sdk/cli/transcript/history.py +815 -0
- glaip_sdk/cli/transcript/viewer.py +72 -463
- glaip_sdk/cli/tui_settings.py +125 -0
- glaip_sdk/cli/update_notifier.py +227 -10
- glaip_sdk/cli/validators.py +5 -6
- glaip_sdk/client/__init__.py +3 -1
- glaip_sdk/client/_schedule_payloads.py +89 -0
- glaip_sdk/client/agent_runs.py +147 -0
- glaip_sdk/client/agents.py +576 -44
- glaip_sdk/client/base.py +26 -0
- glaip_sdk/client/hitl.py +136 -0
- glaip_sdk/client/main.py +25 -14
- glaip_sdk/client/mcps.py +165 -24
- glaip_sdk/client/payloads/agent/__init__.py +23 -0
- glaip_sdk/client/{_agent_payloads.py → payloads/agent/requests.py} +63 -47
- glaip_sdk/client/payloads/agent/responses.py +43 -0
- glaip_sdk/client/run_rendering.py +546 -92
- glaip_sdk/client/schedules.py +439 -0
- glaip_sdk/client/shared.py +21 -0
- glaip_sdk/client/tools.py +206 -32
- glaip_sdk/config/constants.py +33 -2
- glaip_sdk/guardrails/__init__.py +80 -0
- glaip_sdk/guardrails/serializer.py +89 -0
- glaip_sdk/hitl/__init__.py +48 -0
- glaip_sdk/hitl/base.py +64 -0
- glaip_sdk/hitl/callback.py +43 -0
- glaip_sdk/hitl/local.py +121 -0
- glaip_sdk/hitl/remote.py +523 -0
- glaip_sdk/mcps/__init__.py +21 -0
- glaip_sdk/mcps/base.py +345 -0
- glaip_sdk/models/__init__.py +136 -0
- glaip_sdk/models/_provider_mappings.py +101 -0
- glaip_sdk/models/_validation.py +97 -0
- glaip_sdk/models/agent.py +48 -0
- glaip_sdk/models/agent_runs.py +117 -0
- glaip_sdk/models/common.py +42 -0
- glaip_sdk/models/constants.py +141 -0
- glaip_sdk/models/mcp.py +33 -0
- glaip_sdk/models/model.py +170 -0
- glaip_sdk/models/schedule.py +224 -0
- glaip_sdk/models/tool.py +33 -0
- glaip_sdk/payload_schemas/__init__.py +1 -13
- glaip_sdk/payload_schemas/agent.py +1 -0
- glaip_sdk/payload_schemas/guardrails.py +34 -0
- glaip_sdk/registry/__init__.py +55 -0
- glaip_sdk/registry/agent.py +164 -0
- glaip_sdk/registry/base.py +139 -0
- glaip_sdk/registry/mcp.py +253 -0
- glaip_sdk/registry/tool.py +445 -0
- glaip_sdk/rich_components.py +58 -2
- glaip_sdk/runner/__init__.py +76 -0
- glaip_sdk/runner/base.py +84 -0
- glaip_sdk/runner/deps.py +115 -0
- glaip_sdk/runner/langgraph.py +1055 -0
- glaip_sdk/runner/logging_config.py +77 -0
- glaip_sdk/runner/mcp_adapter/__init__.py +13 -0
- glaip_sdk/runner/mcp_adapter/base_mcp_adapter.py +43 -0
- glaip_sdk/runner/mcp_adapter/langchain_mcp_adapter.py +257 -0
- glaip_sdk/runner/mcp_adapter/mcp_config_builder.py +116 -0
- glaip_sdk/runner/tool_adapter/__init__.py +18 -0
- glaip_sdk/runner/tool_adapter/base_tool_adapter.py +44 -0
- glaip_sdk/runner/tool_adapter/langchain_tool_adapter.py +242 -0
- glaip_sdk/schedules/__init__.py +22 -0
- glaip_sdk/schedules/base.py +291 -0
- glaip_sdk/tools/__init__.py +22 -0
- glaip_sdk/tools/base.py +488 -0
- glaip_sdk/utils/__init__.py +59 -12
- glaip_sdk/utils/a2a/__init__.py +34 -0
- glaip_sdk/utils/a2a/event_processor.py +188 -0
- glaip_sdk/utils/agent_config.py +8 -2
- glaip_sdk/utils/bundler.py +403 -0
- glaip_sdk/utils/client.py +111 -0
- glaip_sdk/utils/client_utils.py +39 -7
- glaip_sdk/utils/datetime_helpers.py +58 -0
- glaip_sdk/utils/discovery.py +78 -0
- glaip_sdk/utils/display.py +23 -15
- glaip_sdk/utils/export.py +143 -0
- glaip_sdk/utils/general.py +0 -33
- glaip_sdk/utils/import_export.py +12 -7
- glaip_sdk/utils/import_resolver.py +524 -0
- glaip_sdk/utils/instructions.py +101 -0
- glaip_sdk/utils/rendering/__init__.py +115 -1
- glaip_sdk/utils/rendering/formatting.py +5 -30
- glaip_sdk/utils/rendering/layout/__init__.py +64 -0
- glaip_sdk/utils/rendering/{renderer → layout}/panels.py +9 -0
- glaip_sdk/utils/rendering/{renderer → layout}/progress.py +70 -1
- glaip_sdk/utils/rendering/layout/summary.py +74 -0
- glaip_sdk/utils/rendering/layout/transcript.py +606 -0
- glaip_sdk/utils/rendering/models.py +1 -0
- glaip_sdk/utils/rendering/renderer/__init__.py +9 -47
- glaip_sdk/utils/rendering/renderer/base.py +299 -1434
- glaip_sdk/utils/rendering/renderer/config.py +1 -5
- glaip_sdk/utils/rendering/renderer/debug.py +26 -20
- glaip_sdk/utils/rendering/renderer/factory.py +138 -0
- glaip_sdk/utils/rendering/renderer/stream.py +4 -33
- glaip_sdk/utils/rendering/renderer/summary_window.py +79 -0
- glaip_sdk/utils/rendering/renderer/thinking.py +273 -0
- glaip_sdk/utils/rendering/renderer/tool_panels.py +442 -0
- glaip_sdk/utils/rendering/renderer/transcript_mode.py +162 -0
- glaip_sdk/utils/rendering/state.py +204 -0
- glaip_sdk/utils/rendering/steps/__init__.py +34 -0
- glaip_sdk/utils/rendering/{steps.py → steps/event_processor.py} +53 -440
- glaip_sdk/utils/rendering/steps/format.py +176 -0
- glaip_sdk/utils/rendering/steps/manager.py +387 -0
- glaip_sdk/utils/rendering/timing.py +36 -0
- glaip_sdk/utils/rendering/viewer/__init__.py +21 -0
- glaip_sdk/utils/rendering/viewer/presenter.py +184 -0
- glaip_sdk/utils/resource_refs.py +25 -13
- glaip_sdk/utils/runtime_config.py +426 -0
- glaip_sdk/utils/serialization.py +18 -0
- glaip_sdk/utils/sync.py +162 -0
- glaip_sdk/utils/tool_detection.py +301 -0
- glaip_sdk/utils/tool_storage_provider.py +140 -0
- glaip_sdk/utils/validation.py +16 -24
- {glaip_sdk-0.1.2.dist-info → glaip_sdk-0.7.17.dist-info}/METADATA +69 -23
- glaip_sdk-0.7.17.dist-info/RECORD +224 -0
- {glaip_sdk-0.1.2.dist-info → glaip_sdk-0.7.17.dist-info}/WHEEL +2 -1
- glaip_sdk-0.7.17.dist-info/entry_points.txt +2 -0
- glaip_sdk-0.7.17.dist-info/top_level.txt +1 -0
- glaip_sdk/cli/commands/agents.py +0 -1369
- glaip_sdk/cli/commands/mcps.py +0 -1187
- glaip_sdk/cli/commands/tools.py +0 -584
- glaip_sdk/cli/utils.py +0 -1278
- glaip_sdk/models.py +0 -240
- glaip_sdk-0.1.2.dist-info/RECORD +0 -82
- glaip_sdk-0.1.2.dist-info/entry_points.txt +0 -3
glaip_sdk/utils/sync.py
ADDED
|
@@ -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
|
+
)
|
glaip_sdk/utils/validation.py
CHANGED
|
@@ -18,6 +18,16 @@ from glaip_sdk.utils.resource_refs import validate_name_format
|
|
|
18
18
|
RESERVED_NAMES = ["admin", "root", "system", "api", "test", "demo"]
|
|
19
19
|
|
|
20
20
|
|
|
21
|
+
def _validate_named_resource(name: str, resource_type: str) -> str:
|
|
22
|
+
"""Shared validator that prevents reserved-name duplication."""
|
|
23
|
+
cleaned_name = validate_name_format(name, resource_type)
|
|
24
|
+
|
|
25
|
+
if cleaned_name.lower() in RESERVED_NAMES:
|
|
26
|
+
raise ValueError(f"{resource_type.capitalize()} name '{cleaned_name}' is reserved and cannot be used")
|
|
27
|
+
|
|
28
|
+
return cleaned_name
|
|
29
|
+
|
|
30
|
+
|
|
21
31
|
def validate_agent_name(name: str) -> str:
|
|
22
32
|
"""Validate agent name and return cleaned version.
|
|
23
33
|
|
|
@@ -30,13 +40,7 @@ def validate_agent_name(name: str) -> str:
|
|
|
30
40
|
Raises:
|
|
31
41
|
ValueError: If name is invalid
|
|
32
42
|
"""
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
# Check for reserved names
|
|
36
|
-
if cleaned_name.lower() in RESERVED_NAMES:
|
|
37
|
-
raise ValueError(f"'{cleaned_name}' is a reserved name and cannot be used")
|
|
38
|
-
|
|
39
|
-
return cleaned_name
|
|
43
|
+
return _validate_named_resource(name, "agent")
|
|
40
44
|
|
|
41
45
|
|
|
42
46
|
def validate_agent_instruction(instruction: str) -> str:
|
|
@@ -74,13 +78,7 @@ def validate_tool_name(name: str) -> str:
|
|
|
74
78
|
Raises:
|
|
75
79
|
ValueError: If name is invalid
|
|
76
80
|
"""
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
# Check for reserved names
|
|
80
|
-
if cleaned_name.lower() in RESERVED_NAMES:
|
|
81
|
-
raise ValueError(f"'{cleaned_name}' is a reserved name and cannot be used")
|
|
82
|
-
|
|
83
|
-
return cleaned_name
|
|
81
|
+
return _validate_named_resource(name, "tool")
|
|
84
82
|
|
|
85
83
|
|
|
86
84
|
def validate_mcp_name(name: str) -> str:
|
|
@@ -95,13 +93,7 @@ def validate_mcp_name(name: str) -> str:
|
|
|
95
93
|
Raises:
|
|
96
94
|
ValueError: If name is invalid
|
|
97
95
|
"""
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
# Check for reserved names
|
|
101
|
-
if cleaned_name.lower() in RESERVED_NAMES:
|
|
102
|
-
raise ValueError(f"'{cleaned_name}' is a reserved name and cannot be used")
|
|
103
|
-
|
|
104
|
-
return cleaned_name
|
|
96
|
+
return _validate_named_resource(name, "mcp")
|
|
105
97
|
|
|
106
98
|
|
|
107
99
|
def validate_timeout(timeout: int) -> int:
|
|
@@ -213,7 +205,7 @@ def validate_directory_path(dir_path: str | Path, must_exist: bool = True) -> Pa
|
|
|
213
205
|
|
|
214
206
|
|
|
215
207
|
def validate_url(url: str) -> str:
|
|
216
|
-
"""Validate URL format.
|
|
208
|
+
"""Validate URL format (HTTPS only).
|
|
217
209
|
|
|
218
210
|
Args:
|
|
219
211
|
url: URL to validate
|
|
@@ -225,7 +217,7 @@ def validate_url(url: str) -> str:
|
|
|
225
217
|
ValueError: If URL is invalid
|
|
226
218
|
"""
|
|
227
219
|
url_pattern = re.compile(
|
|
228
|
-
r"^https
|
|
220
|
+
r"^https://" # https:// only
|
|
229
221
|
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|" # domain...
|
|
230
222
|
r"localhost|" # localhost...
|
|
231
223
|
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
|
|
@@ -235,7 +227,7 @@ def validate_url(url: str) -> str:
|
|
|
235
227
|
)
|
|
236
228
|
|
|
237
229
|
if not url_pattern.match(url):
|
|
238
|
-
raise ValueError(
|
|
230
|
+
raise ValueError("API URL must start with https:// and be a valid host.")
|
|
239
231
|
|
|
240
232
|
return url
|
|
241
233
|
|