xintongsong commented on code in PR #596: URL: https://github.com/apache/flink-agents/pull/596#discussion_r3078417913
########## python/flink_agents/runtime/skill/skill_tools.py: ########## @@ -0,0 +1,253 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +"""Built-in tools for skill loading and shell execution. + +These are internal runtime tools registered automatically when skills are +configured. They access the SkillManager through the runtime ResourceContext. +""" + +from __future__ import annotations + +import logging +import subprocess +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from flink_agents.runtime.skill.skill_manager import SkillManager + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.tools.tool import Tool, ToolMetadata, ToolType + +logger = logging.getLogger(__name__) + + +class LoadSkillArgs(BaseModel): + """Arguments for LoadSkillTool.""" + + name: str = Field( + ..., description="The name of the skill to load (e.g., 'pdf-processing')." + ) + path: str | None = Field( + default="SKILL.md", + description=( + "Optional path to a specific resource within the skill. " + "If not provided, returns the full SKILL.md content." + ), + ) + + +class LoadSkillTool(Tool): + """Tool for loading skill content and resources. + + Accesses the SkillManager through the runtime ResourceContext + (not the public ResourceContext interface). + """ + + metadata: ToolMetadata = Field(exclude=True) + + def __init__(self, **kwargs: Any) -> None: + """Initialize the load skill tool.""" + super().__init__( + metadata=ToolMetadata( + name="load_skill", + description=( + "Load a skill's content or a specific resource. " + "Use this to access skill instructions and resources. " + ), + args_schema=LoadSkillArgs, + ), + **kwargs, + ) + + @classmethod + def tool_type(cls) -> ToolType: + """Return tool type of class.""" + return ToolType.FUNCTION + + def call(self, *args: Any, **kwargs: Any) -> str: + """Call the tool to load a skill.""" + if args: + parsed_args = LoadSkillArgs(name=args[0], **kwargs) + else: + parsed_args = LoadSkillArgs(**kwargs) + + skill_name = parsed_args.name + resource_path = parsed_args.path + logger.debug(f"Loading skill resource {resource_path} for {skill_name}.") + + manager = self._get_skill_manager() + if manager is None: + return "Skill manager not available. No skills have been registered." + + try: + skill = manager.get_skill(skill_name) + except ValueError: + available = manager.get_all_skill_names() + available_str = ( + ", ".join(available) if available else "No skills available." + ) + return f"Skill '{skill_name}' not found. Available skills: {available_str}" + + if resource_path is None or resource_path == "SKILL.md": + return skill.content + + content = skill.get_resource(resource_path) + if content is None: + available = sorted(skill.get_resource_paths()) + return f"Resource '{resource_path}' not found in skill '{skill_name}', Available resources: {available}" + return content + + def _get_skill_manager(self) -> SkillManager | None: + from flink_agents.runtime.resource_context import ResourceContextImpl + + ctx = self.resource_context + if isinstance(ctx, ResourceContextImpl): + return ctx.get_skill_manager() + return None + + +class ExecuteCommandArgs(BaseModel): + """Arguments for ExecuteCommandTool.""" + + skill_name: str = Field( Review Comment: Not sure about requiring a skill name for executing a command. Skills are not executable programs like tools, but are a set of knowledges, scripts and resource for doing something. It's the LLM/agent that learns a skill and perform actions. You cannot execute a skill, nor saying an action belongs to a certain skill. ########## python/flink_agents/api/skills.py: ########## @@ -0,0 +1,83 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +"""Skills configuration resource for agent skills discovery. + +Example usage:: + + @skills + @staticmethod + def my_skills() -> Skills: + return Skills(paths=["./skills"]) + + @skills + @staticmethod + def combined() -> Skills: + return Skills(paths=["./local"], urls=["https://example.com/skills"]) +""" +from __future__ import annotations + +from typing import List + +from pydantic import Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType, SerializableResource + + +class Skills(SerializableResource): + """A resource that stores skill location configuration. + + This is the user-facing API for defining where skills are located. + The runtime will use this configuration to discover and load skills + at initialization time, creating an internal SkillManager. + + Example:: + + Skills(paths=["./skills"], allowed_commands=["gh", "git"]) + Skills(paths=["./local"], urls=["https://example.com/skills"]) + + Attributes: + paths: List of filesystem paths to load skills from. + urls: List of URLs to load skills from. + resources: List of Python package resources to load skills from. + allowed_script_types: Script types that are allowed to execute + from skill directories. Defaults to ``["shell", "python"]``. + Supported types: ``"shell"`` (.sh, .bash), ``"python"`` (.py). + allowed_commands: Whitelist of executable command names that skills + are permitted to run when the command is not a skill script. + Only the first token of a command is checked against this list. + """ + + paths: List[str] = Field(default_factory=list) Review Comment: I'd suggest to provide some factory methods, like `Skills.from_local_dir()` or `Skills.from_url()`. This aligns with `Prompt.from_text() / from_messages()`, and is more easy-to-use. ########## python/flink_agents/api/resource.py: ########## @@ -42,6 +42,7 @@ class ResourceType(Enum): VECTOR_STORE = "vector_store" PROMPT = "prompt" MCP_SERVER = "mcp_server" + SKILL = "skill" Review Comment: `SKILLS` ########## python/flink_agents/api/resource_context.py: ########## @@ -33,3 +33,10 @@ class ResourceContext(ABC): @abstractmethod def get_resource(self, name: str, resource_type: "ResourceType") -> "Resource": """Get another resource declared in the same Agent.""" + + @abstractmethod + def generate_skill_discovery_prompt(self, *skill_names: str) -> str: Review Comment: I'd suggest the name `generate_available_skills_prompt` ########## python/flink_agents/e2e_tests/e2e_tests_integration/long_term_memory_test.py: ########## @@ -218,7 +218,7 @@ async def retrieve_items(event: Event, ctx: RunnerContext): # noqa D102 "flink-agent doesn't allow get resource in async thread. We will deprecate VectorStoreLongTermMemory in 0.3.0," "so we will not fix this issue for now." ) -def test_long_term_memory_async_execution_in_action(tmp_path: Path) -> None: # noqa: D103 +def test_long_term_memory_async_execution_in_action(tmp_path: Path) -> None: Review Comment: wrong commit ########## python/flink_agents/runtime/skill/skill_manager.py: ########## @@ -0,0 +1,285 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +import shlex +from pathlib import Path +from typing import ClassVar, Dict, List, Set + +from flink_agents.api.skills import Skills +from flink_agents.runtime.skill.agent_skill import AgentSkill +from flink_agents.runtime.skill.repository.filesystem_repository import ( + FileSystemSkillRepository, +) +from flink_agents.runtime.skill.skill_prompt_provider import SkillPromptProvider +from flink_agents.runtime.skill.skill_repository import SkillRepository + + +class RegisteredSkill: Review Comment: Why do we need both `AgentSkill` and `RegisteredSkill` ? ########## python/flink_agents/api/decorators.py: ########## @@ -189,6 +189,24 @@ def vector_store(func: Callable) -> Callable: func._is_vector_store = True return func + +def skills(func: Callable) -> Callable: Review Comment: wrong commit ########## python/flink_agents/api/skills.py: ########## @@ -0,0 +1,83 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +"""Skills configuration resource for agent skills discovery. + +Example usage:: + + @skills + @staticmethod + def my_skills() -> Skills: + return Skills(paths=["./skills"]) + + @skills + @staticmethod + def combined() -> Skills: + return Skills(paths=["./local"], urls=["https://example.com/skills"]) +""" +from __future__ import annotations + +from typing import List + +from pydantic import Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType, SerializableResource + + +class Skills(SerializableResource): + """A resource that stores skill location configuration. + + This is the user-facing API for defining where skills are located. + The runtime will use this configuration to discover and load skills + at initialization time, creating an internal SkillManager. + + Example:: + + Skills(paths=["./skills"], allowed_commands=["gh", "git"]) + Skills(paths=["./local"], urls=["https://example.com/skills"]) + + Attributes: + paths: List of filesystem paths to load skills from. + urls: List of URLs to load skills from. + resources: List of Python package resources to load skills from. + allowed_script_types: Script types that are allowed to execute + from skill directories. Defaults to ``["shell", "python"]``. + Supported types: ``"shell"`` (.sh, .bash), ``"python"`` (.py). + allowed_commands: Whitelist of executable command names that skills + are permitted to run when the command is not a skill script. + Only the first token of a command is checked against this list. + """ + + paths: List[str] = Field(default_factory=list) + urls: List[str] = Field(default_factory=list) + resources: List[str] = Field(default_factory=list) + allowed_script_types: List[str] = Field( + default_factory=lambda: ["shell", "python"] + ) + allowed_commands: List[str] = Field(default_factory=list) Review Comment: This doesn't sounds right. How is allowed script types / commands an attribute of the skills? If the model decide to execute something, how do we know it's following the instructions of a skill or not? ########## python/flink_agents/api/skills.py: ########## @@ -0,0 +1,83 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +"""Skills configuration resource for agent skills discovery. + +Example usage:: + + @skills + @staticmethod + def my_skills() -> Skills: + return Skills(paths=["./skills"]) + + @skills + @staticmethod + def combined() -> Skills: + return Skills(paths=["./local"], urls=["https://example.com/skills"]) +""" +from __future__ import annotations + +from typing import List + +from pydantic import Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType, SerializableResource + + +class Skills(SerializableResource): + """A resource that stores skill location configuration. + + This is the user-facing API for defining where skills are located. + The runtime will use this configuration to discover and load skills + at initialization time, creating an internal SkillManager. + + Example:: + + Skills(paths=["./skills"], allowed_commands=["gh", "git"]) + Skills(paths=["./local"], urls=["https://example.com/skills"]) + + Attributes: + paths: List of filesystem paths to load skills from. + urls: List of URLs to load skills from. + resources: List of Python package resources to load skills from. + allowed_script_types: Script types that are allowed to execute + from skill directories. Defaults to ``["shell", "python"]``. + Supported types: ``"shell"`` (.sh, .bash), ``"python"`` (.py). + allowed_commands: Whitelist of executable command names that skills + are permitted to run when the command is not a skill script. + Only the first token of a command is checked against this list. + """ + + paths: List[str] = Field(default_factory=list) Review Comment: And maybe hide the implementation of `Skills` from the users. ########## api/src/main/java/org/apache/flink/agents/api/resource/ResourceType.java: ########## @@ -31,7 +31,8 @@ public enum ResourceType { VECTOR_STORE("vector_store"), PROMPT("prompt"), TOOL("tool"), - MCP_SERVER("mcp_server"); + MCP_SERVER("mcp_server"), + SKILL("skill"); Review Comment: `SKILLS` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
