From 75e9029418f9f591df8fd5bbf84357184ad4d772 Mon Sep 17 00:00:00 2001 From: mrcaidev Date: Mon, 30 Nov 2026 16:53:41 +0800 Subject: [PATCH 1/2] feat: support search_mode in VeSkillRegistry --- .../docs/framework/agent/skills.en.mdx | 2 + docs/content/docs/framework/agent/skills.mdx | 2 + tests/skills/test_adk_skill_registry.py | 64 ++++++++++++++++++- tests/skills/test_skill_space_policy.py | 14 ++-- veadk/skills/registry.py | 30 ++++++++- veadk/skills/utils.py | 22 +++++-- 6 files changed, 117 insertions(+), 17 deletions(-) diff --git a/docs/content/docs/framework/agent/skills.en.mdx b/docs/content/docs/framework/agent/skills.en.mdx index 82964cc30..e96d87e1f 100644 --- a/docs/content/docs/framework/agent/skills.en.mdx +++ b/docs/content/docs/framework/agent/skills.en.mdx @@ -34,6 +34,8 @@ agent = Agent( ) ``` +`VeSkillRegistry` accepts a `search_mode` parameter. The default value is `"all"`, which loads the frontmatter of every available skill under that skill space into the agent context. For AgentKit Skill Space, `"exact"` performs keyword-based exact search, `"semantic"` performs vector-based semantic search, and `"rrf"` performs hybrid search. SkillHub Skill Space does not support specifying a search mode. + This path does not download every skill at initialization time. The cache only stores concrete downloaded skill versions; remote metadata is fetched again on every search / get, so remote version changes are picked up before loading. ## Skill Directory Structure diff --git a/docs/content/docs/framework/agent/skills.mdx b/docs/content/docs/framework/agent/skills.mdx index 81dd1e92e..5a2e87363 100644 --- a/docs/content/docs/framework/agent/skills.mdx +++ b/docs/content/docs/framework/agent/skills.mdx @@ -34,6 +34,8 @@ agent = Agent( ) ``` +`VeSkillRegistry` 接受一个 `search_mode` 参数。其默认值为 `"all"`,即一次性加载空间中所有技能的 frontmatter 到 agent 的上下文中。对于 AgentKit SkillSpace 技能空间,可以选择 `"exact"` 进行关键词精确搜索、`"semantic"` 进行向量语义搜索,或 `"rrf"` 进行混合搜索。SkillHub 的技能空间暂不支持指定搜索模式。 + 该方式不会在初始化阶段全量下载技能;缓存仅用于已下载的具体技能版本。远端 metadata 每次 search / get 都会重新拉取,因此远端版本变化后会按新版本重新下载。 ## 技能目录结构 diff --git a/tests/skills/test_adk_skill_registry.py b/tests/skills/test_adk_skill_registry.py index f9fc42753..471e41834 100644 --- a/tests/skills/test_adk_skill_registry.py +++ b/tests/skills/test_adk_skill_registry.py @@ -16,10 +16,11 @@ import asyncio from pathlib import Path +from unittest.mock import Mock import pytest -from veadk.skills import VeSkillRegistry +from veadk.skills import VeSkillRegistry, utils from veadk.skills import registry as registry_module from veadk.skills.skill import Skill as VeADKSkill @@ -70,7 +71,10 @@ def test_registry_search_ignores_query_and_fetches_all_remote_skills( ), ] - def load_skills_from_cloud(skill_source_id: str) -> list[VeADKSkill]: + def load_skills_from_cloud( + skill_source_id: str, *, search_filter=None + ) -> list[VeADKSkill]: + assert search_filter is None calls.append(skill_source_id) return remote_skills @@ -296,3 +300,59 @@ def test_registry_get_skill_raises_when_name_is_missing( with pytest.raises(ValueError, match="Skill 'missing' not found"): asyncio.run(registry.get_skill(name="missing")) + + +@pytest.mark.parametrize("mode", [None, "all", "exact", "semantic", "rrf"]) +@pytest.mark.parametrize("query", ["", " find skill "]) +def test_space_search_request(monkeypatch, mode, query): + monkeypatch.delenv("SKILL_SPACE_POLICY", raising=False) + monkeypatch.setattr(utils, "_get_cloud_credentials", lambda: ("", "", "")) + request = Mock( + return_value={ + "Items": [ + {"Name": "beta", "Description": "Beta skill"}, + {"Name": "alpha", "Description": "Alpha skill"}, + ] + } + ) + monkeypatch.setattr(utils, "ve_request", request) + options = {"search_mode": mode} if mode is not None else {} + registry = VeSkillRegistry(skill_source_id="ss-test", **options) + + skills = asyncio.run(registry.search_skills(query=query)) + assert [skill.name for skill in skills] == ["beta", "alpha"] + body = {"SkillSpaceId": "ss-test", "InnerTags": {"source": "sandbox"}} + if mode not in (None, "all"): + body["Filter"] = {"Query": query, "QueryMode": mode} + assert request.call_args.kwargs["request_body"] == body + + with pytest.raises(ValueError, match="not found"): + asyncio.run(registry.get_skill(name="missing")) + assert "Filter" not in request.call_args.kwargs["request_body"] + + +@pytest.mark.parametrize("mode", ["all", "exact", "semantic", "rrf"]) +def test_skillhub_search_request_ignores_mode(monkeypatch, mode): + monkeypatch.delenv("SKILL_SPACE_POLICY", raising=False) + request = Mock(return_value={"Items": [], "TotalCount": 0}) + monkeypatch.setattr(utils, "_skillhub_request", request) + registry = VeSkillRegistry(skill_source_id="sp-test", search_mode=mode) + asyncio.run(registry.search_skills(query="hello")) + assert request.call_args.args[1]["Filter"] == {"SkillSpaceId": "sp-test"} + assert "query is ignored" in registry.search_tool_description() + + +def test_registry_rejects_unknown_search_mode(): + with pytest.raises(ValueError, match="unknown"): + VeSkillRegistry(skill_source_id="ss-test", search_mode="unknown") + + +def test_filtered_search_returns_empty_on_request_error(monkeypatch): + monkeypatch.delenv("SKILL_SPACE_POLICY", raising=False) + monkeypatch.setattr(utils, "_get_cloud_credentials", lambda: ("", "", "")) + request = Mock(side_effect=RuntimeError("offline")) + monkeypatch.setattr(utils, "ve_request", request) + registry = VeSkillRegistry(skill_source_id="ss-test", search_mode="exact") + + assert asyncio.run(registry.search_skills(query="hello")) == [] + request.assert_called_once() diff --git a/tests/skills/test_skill_space_policy.py b/tests/skills/test_skill_space_policy.py index cbed9da4c..2a51e4b46 100644 --- a/tests/skills/test_skill_space_policy.py +++ b/tests/skills/test_skill_space_policy.py @@ -128,7 +128,7 @@ def test_load_skills_from_cloud_applies_policy( monkeypatch.setattr( utils, "_load_skills_from_space_id", - lambda _space_id, *, raise_on_error=False: [ + lambda _space_id, *, raise_on_error=False, search_filter=None: [ _skill("skill-1", "one"), _skill("skill-2", "two"), ], @@ -168,7 +168,7 @@ def fake_load(space_id: str, *, raise_on_error=False): monkeypatch.setattr( utils, "_load_skills_from_space_id", - lambda space_id, *, raise_on_error=False: { + lambda space_id, *, raise_on_error=False, search_filter=None: { "ss-one": [ Skill( id="skill-1", @@ -219,7 +219,7 @@ def test_missing_policy_keeps_all_remote_skills(monkeypatch: pytest.MonkeyPatch) monkeypatch.setattr( utils, "_load_skills_from_space_id", - lambda _space_id, *, raise_on_error=False: [ + lambda _space_id, *, raise_on_error=False, search_filter=None: [ _skill("skill-1", "one"), _skill("skill-2", "two"), ], @@ -240,7 +240,9 @@ def test_policy_excludes_remote_skills_without_stable_ids( monkeypatch.setattr( utils, "_load_skills_from_space_id", - lambda _space_id, *, raise_on_error=False: [_skill(None, "missing-id")], + lambda _space_id, *, raise_on_error=False, search_filter=None: [ + _skill(None, "missing-id") + ], ) assert utils.load_skills_from_cloud("ss-test") == [] @@ -269,7 +271,9 @@ def test_registry_get_skill_cannot_bypass_deny_policy( monkeypatch.setattr( utils, "_load_skills_from_space_id", - lambda _space_id, *, raise_on_error=False: [_skill("skill-1", "one")], + lambda _space_id, *, raise_on_error=False, search_filter=None: [ + _skill("skill-1", "one") + ], ) monkeypatch.setattr( registry_module, diff --git a/veadk/skills/registry.py b/veadk/skills/registry.py index ba60be78f..d64aed7b6 100644 --- a/veadk/skills/registry.py +++ b/veadk/skills/registry.py @@ -17,13 +17,14 @@ from __future__ import annotations import asyncio +from enum import Enum from pathlib import Path from typing import Any from google.adk.skills import Frontmatter from google.adk.skills import Skill as ADKSkill -from google.adk.skills import models as adk_skill_models from google.adk.skills import _utils as adk_skill_utils +from google.adk.skills import models as adk_skill_models try: from google.adk.skills import SkillRegistry @@ -39,6 +40,13 @@ class SkillRegistry: # type: ignore[no-redef] from veadk.skills.utils import load_skills_from_cloud +class SkillSearchMode(str, Enum): + ALL = "all" + EXACT = "exact" + SEMANTIC = "semantic" + RRF = "rrf" + + class VeSkillRegistry(SkillRegistry): """ADK ``SkillRegistry`` implementation for one remote VeADK skill space.""" @@ -47,6 +55,7 @@ def __init__( *, skill_source_id: str, cache_dir: Path | None = None, + search_mode: SkillSearchMode | str = SkillSearchMode.ALL, ) -> None: normalized_skill_source_id = skill_source_id.strip() if not normalized_skill_source_id or "," in normalized_skill_source_id: @@ -54,13 +63,26 @@ def __init__( self.skill_source_id = normalized_skill_source_id self.cache_dir = cache_dir + self.search_mode = SkillSearchMode(search_mode) + + @property + def _uses_search_filter(self) -> bool: + return ( + self.search_mode != SkillSearchMode.ALL + and not self.skill_source_id.startswith("sp-") + ) async def search_skills(self, *, query: str) -> list[Frontmatter]: - """Return all remote skills in this skill space, ignoring ``query``.""" - del query + """Search the remote skill space using the configured search mode.""" + search_filter = ( + {"Query": query, "QueryMode": self.search_mode.value} + if self._uses_search_filter + else None + ) skills = await asyncio.to_thread( load_skills_from_cloud, self.skill_source_id, + search_filter=search_filter, ) return [self._to_frontmatter(skill) for skill in skills] @@ -87,6 +109,8 @@ async def get_skill(self, *, name: str) -> ADKSkill: return loaded def search_tool_description(self) -> str | None: + if self._uses_search_filter: + return f"Search the remote skill space using the query in {self.search_mode.value} mode." return ( "Search all skills available in the configured VeADK remote skill space. " "The query is ignored; every remote skill is returned for discovery." diff --git a/veadk/skills/utils.py b/veadk/skills/utils.py index 520e15aaa..ce208fd83 100644 --- a/veadk/skills/utils.py +++ b/veadk/skills/utils.py @@ -13,22 +13,21 @@ # limitations under the License. import json -from pathlib import Path import os -import frontmatter -from typing import Literal +from pathlib import Path +from typing import Any, Callable, Dict, Literal, Optional from urllib.parse import urlparse +import frontmatter from google.adk.tools import BaseTool, ToolContext -from typing import Any, Dict, Optional, Callable -from veadk.skills.skill import Skill from veadk.skills.policy import ( SKILL_SPACE_POLICY_ENV, SkillSpacePolicyError, SkillSpacePolicySet, parse_skill_space_policy, ) +from veadk.skills.skill import Skill from veadk.utils.logger import get_logger from veadk.utils.volcengine_sign import ve_request, volcengine_signed_request @@ -142,7 +141,10 @@ def load_skills_from_directory(skills_directory: Path) -> list[Skill]: def load_skills_from_cloud( - skill_space_ids: str, *, raise_on_error: bool = False + skill_space_ids: str, + *, + raise_on_error: bool = False, + search_filter: dict[str, str] | None = None, ) -> list[Skill]: skill_space_ids_list = [x.strip() for x in skill_space_ids.split(",") if x.strip()] logger.info(f"Load skills from cloud skill sources: {skill_space_ids_list}") @@ -169,6 +171,7 @@ def load_skills_from_cloud( skills.extend( _load_skills_from_space_id( skill_space_id, + search_filter=search_filter, raise_on_error=raise_on_error, ) ) @@ -345,7 +348,10 @@ def _build_skill_from_skillhub_item( def _load_skills_from_space_id( - skill_space_id: str, *, raise_on_error: bool = False + skill_space_id: str, + *, + raise_on_error: bool = False, + search_filter: dict[str, str] | None = None, ) -> list[Skill]: logger.info(f"Load skills from skill space: {skill_space_id}") @@ -359,6 +365,8 @@ def _load_skills_from_space_id( "SkillSpaceId": skill_space_id, "InnerTags": {"source": "sandbox"}, } + if search_filter is not None: + request_body["Filter"] = search_filter logger.debug(f"ListSkillsBySpaceId request body: {request_body}") scheme = os.getenv("AGENTKIT_TOP_SCHEME", "https").lower() From a3ad511de1d1a29ebf1f60edf4ba8d5a3b99d873 Mon Sep 17 00:00:00 2001 From: mrcaidev Date: Mon, 30 Nov 2026 16:55:32 +0800 Subject: [PATCH 2/2] docs: wording --- docs/content/docs/framework/agent/skills.en.mdx | 2 +- docs/content/docs/framework/agent/skills.mdx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/docs/framework/agent/skills.en.mdx b/docs/content/docs/framework/agent/skills.en.mdx index e96d87e1f..db96b04c5 100644 --- a/docs/content/docs/framework/agent/skills.en.mdx +++ b/docs/content/docs/framework/agent/skills.en.mdx @@ -34,7 +34,7 @@ agent = Agent( ) ``` -`VeSkillRegistry` accepts a `search_mode` parameter. The default value is `"all"`, which loads the frontmatter of every available skill under that skill space into the agent context. For AgentKit Skill Space, `"exact"` performs keyword-based exact search, `"semantic"` performs vector-based semantic search, and `"rrf"` performs hybrid search. SkillHub Skill Space does not support specifying a search mode. +`VeSkillRegistry` accepts a `search_mode` parameter. The default value is `"all"`, which loads the frontmatter of every available skill under that skill space into the agent context. For AgentKit Skill Space, `"exact"` performs keyword-based exact search, `"semantic"` performs vector-based semantic search, and `"rrf"` performs hybrid search. SkillHub Skill Space does not yet support specifying a search mode. This path does not download every skill at initialization time. The cache only stores concrete downloaded skill versions; remote metadata is fetched again on every search / get, so remote version changes are picked up before loading. diff --git a/docs/content/docs/framework/agent/skills.mdx b/docs/content/docs/framework/agent/skills.mdx index 5a2e87363..f72948fd4 100644 --- a/docs/content/docs/framework/agent/skills.mdx +++ b/docs/content/docs/framework/agent/skills.mdx @@ -34,7 +34,7 @@ agent = Agent( ) ``` -`VeSkillRegistry` 接受一个 `search_mode` 参数。其默认值为 `"all"`,即一次性加载空间中所有技能的 frontmatter 到 agent 的上下文中。对于 AgentKit SkillSpace 技能空间,可以选择 `"exact"` 进行关键词精确搜索、`"semantic"` 进行向量语义搜索,或 `"rrf"` 进行混合搜索。SkillHub 的技能空间暂不支持指定搜索模式。 +`VeSkillRegistry` 接受一个 `search_mode` 参数。其默认值为 `"all"`,即一次性加载空间中所有技能的 frontmatter 到 agent 的上下文中。对于 AgentKit 的技能空间,可以选择 `"exact"` 进行关键词精确搜索、`"semantic"` 进行向量语义搜索,或 `"rrf"` 进行混合搜索。SkillHub 的技能空间暂不支持指定搜索模式。 该方式不会在初始化阶段全量下载技能;缓存仅用于已下载的具体技能版本。远端 metadata 每次 search / get 都会重新拉取,因此远端版本变化后会按新版本重新下载。