GreatEugenius commented on code in PR #854: URL: https://github.com/apache/flink-agents/pull/854#discussion_r3465784316
########## python/flink_agents/api/tools/tool_parameter_injection.py: ########## @@ -0,0 +1,89 @@ +################################################################################ +# 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. +################################################################################# +from enum import Enum + +from pydantic import BaseModel + + +class ToolParameterSource(str, Enum): + """Source for a framework-injected tool parameter.""" + + CONFIG = "config" + SENSORY_MEMORY = "sensory_memory" + SHORT_TERM_MEMORY = "short_term_memory" + + @classmethod + def _missing_(cls, value: object) -> "ToolParameterSource | None": + if not isinstance(value, str): + return None + normalized = value.lower() + if normalized == "action_config": + return cls.CONFIG + for source in cls: + if source.value == normalized: + return source + return None + + +class InjectedArg(BaseModel): + """Declarative source binding for a framework-injected tool parameter.""" + + source: ToolParameterSource = ToolParameterSource.SENSORY_MEMORY + key: str | None = None + + @staticmethod + def from_config(key: str) -> "InjectedArg": + """Create an injected argument read from global agent config.""" + return InjectedArg(source=ToolParameterSource.CONFIG, key=key) + + @staticmethod + def from_sensory_memory(path: str) -> "InjectedArg": + """Create an injected argument read from sensory memory.""" + return InjectedArg(source=ToolParameterSource.SENSORY_MEMORY, key=path) + + @staticmethod + def from_short_term_memory(path: str) -> "InjectedArg": + """Create an injected argument read from short-term memory.""" + return InjectedArg(source=ToolParameterSource.SHORT_TERM_MEMORY, key=path) + + def with_default_key(self, key: str) -> "InjectedArg": + """Use the tool parameter name when no explicit key is configured.""" + if self.key: + return self + return InjectedArg(source=self.source, key=key) + + +def normalize_injected_args( Review Comment: Yes, agreed. I added upfront validation for declarative injected argument names. On the Python side, `@tool(...)` / `Tool.from_callable(...)` validate immediately because the callable is already available. For descriptor-style paths such as YAML/API `FunctionTool`, the YAML loader now stays side-effect-free and does not import/inspect user functions just for validation; the check is performed when building the `AgentPlan`, where the callable is materialized anyway. The validator also allows `**kwargs`, since those tools can intentionally accept framework-provided extra arguments. The Java side now validates at the same AgentPlan boundary via `ToolParameterInjectionValidator`. It validates when parameter names are reliable, for example from `@ToolParam(name = ...)` or `-parameters`, and avoids false positives when bytecode only exposes synthetic names like `arg0`. -- 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]
