Get variables from tools, where each tool's code is used as the variable value. Args: tool_name (Optional[str]): Name of a specific tool. If None, returns variables for all tools. Returns: Dict[str, Variable]: Dictionary mapping tool name
(self, tool_name: Optional[str] = None)
| 1111 | logger.error(f"| ❌ Error during tool context manager cleanup: {e}") |
| 1112 | |
| 1113 | async def get_variables(self, tool_name: Optional[str] = None) -> Dict[str, 'Variable']: |
| 1114 | """Get variables from tools, where each tool's code is used as the variable value. |
| 1115 | |
| 1116 | Args: |
| 1117 | tool_name (Optional[str]): Name of a specific tool. If None, returns variables for all tools. |
| 1118 | |
| 1119 | Returns: |
| 1120 | Dict[str, Variable]: Dictionary mapping tool names to Variable objects. Each Variable has: |
| 1121 | - name: tool name |
| 1122 | - type: "tool_code" |
| 1123 | - description: tool description |
| 1124 | - require_grad: tool's require_grad value |
| 1125 | - variables: tool's code (as string value) |
| 1126 | """ |
| 1127 | # Lazy import to avoid circular dependency |
| 1128 | from src.optimizer.types import Variable |
| 1129 | |
| 1130 | variables: Dict[str, Variable] = {} |
| 1131 | |
| 1132 | if tool_name is not None: |
| 1133 | # Get specific tool |
| 1134 | tool_config = await self.get_info(tool_name) |
| 1135 | if tool_config is None: |
| 1136 | logger.warning(f"| ⚠️ Tool {tool_name} not found") |
| 1137 | return variables |
| 1138 | |
| 1139 | tool_configs = {tool_name: tool_config} |
| 1140 | else: |
| 1141 | # Get all tools |
| 1142 | tool_configs = self._tool_configs |
| 1143 | |
| 1144 | for name, tool_config in tool_configs.items(): |
| 1145 | # Get tool code |
| 1146 | tool_code = tool_config.code or "" |
| 1147 | |
| 1148 | # Create Variable for this tool |
| 1149 | variable = Variable( |
| 1150 | name=name, |
| 1151 | type="tool_code", |
| 1152 | description=tool_config.description or f"Code for tool {name}", |
| 1153 | require_grad=tool_config.require_grad, |
| 1154 | template=None, |
| 1155 | variables=tool_code # Store code as the variable value |
| 1156 | ) |
| 1157 | variables[name] = variable |
| 1158 | |
| 1159 | return variables |
| 1160 | |
| 1161 | async def get_trainable_variables(self, tool_name: Optional[str] = None) -> Dict[str, 'Variable']: |
| 1162 | """Get trainable variables from tools, filtering out tools with require_grad=False. |
no test coverage detected