Spaces:
Build error
Build error
Download langgraph_node.py from sp103107/agent-system: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/spaces/sp103107/agent-system/resolve/main/langgraph_node.py
- Command line
-
hf download hf://spaces/sp103107/agent-system/langgraph_node.py
-
curl -L -o langgraph_node.py https://huggingface.co/spaces/sp103107/agent-system/resolve/main/langgraph_node.py
5.99 kB
| from base_agent import BaseAgent | |
| from models import ExecuteRequest, ExecuteResponse, NetworkInput, NetworkOutput | |
| from typing import Dict, Any, List | |
| import logging | |
| from datetime import datetime | |
| import json | |
| from pathlib import Path | |
| class LangGraphNode(BaseAgent): | |
| def __init__(self, name: str = "langgraph_node", port: int = None): | |
| super().__init__(name, port) | |
| self.logger = logging.getLogger(name) | |
| # Initialize state storage | |
| self.state_dir = Path(__file__).parent / "data" / "state" | |
| self.state_dir.mkdir(parents=True, exist_ok=True) | |
| def process_request(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| """Process incoming execute requests""" | |
| try: | |
| request = ExecuteRequest(**data) | |
| # Route request to appropriate handler | |
| if request.action == "get_state": | |
| return self.handle_get_state(request) | |
| elif request.action == "set_state": | |
| return self.handle_set_state(request) | |
| elif request.action == "update_state": | |
| return self.handle_update_state(request) | |
| elif request.action == "delete_state": | |
| return self.handle_delete_state(request) | |
| else: | |
| return ExecuteResponse( | |
| status="error", | |
| error=f"Unknown action: {request.action}" | |
| ).dict() | |
| except Exception as e: | |
| self.logger.error(f"Error processing request: {str(e)}") | |
| return ExecuteResponse( | |
| status="error", | |
| error=str(e) | |
| ).dict() | |
| def handle_get_state(self, request: ExecuteRequest) -> Dict[str, Any]: | |
| """Handle state retrieval requests""" | |
| try: | |
| key = request.parameters.get("key") | |
| if not key: | |
| raise ValueError("State key not specified") | |
| # Load state from file | |
| state_file = self.state_dir / f"{key}.json" | |
| if not state_file.exists(): | |
| return ExecuteResponse( | |
| status="success", | |
| result={"state": None} | |
| ).dict() | |
| with open(state_file) as f: | |
| state = json.load(f) | |
| return ExecuteResponse( | |
| status="success", | |
| result={"state": state} | |
| ).dict() | |
| except Exception as e: | |
| self.logger.error(f"Error getting state: {str(e)}") | |
| return ExecuteResponse( | |
| status="error", | |
| error=str(e) | |
| ).dict() | |
| def handle_set_state(self, request: ExecuteRequest) -> Dict[str, Any]: | |
| """Handle state setting requests""" | |
| try: | |
| key = request.parameters.get("key") | |
| state = request.parameters.get("state") | |
| if not key or state is None: | |
| raise ValueError("Key and state required") | |
| # Save state to file | |
| state_file = self.state_dir / f"{key}.json" | |
| with open(state_file, 'w') as f: | |
| json.dump(state, f, indent=2) | |
| return ExecuteResponse( | |
| status="success", | |
| result={"message": f"State {key} set successfully"} | |
| ).dict() | |
| except Exception as e: | |
| self.logger.error(f"Error setting state: {str(e)}") | |
| return ExecuteResponse( | |
| status="error", | |
| error=str(e) | |
| ).dict() | |
| def handle_update_state(self, request: ExecuteRequest) -> Dict[str, Any]: | |
| """Handle state update requests""" | |
| try: | |
| key = request.parameters.get("key") | |
| updates = request.parameters.get("updates") | |
| if not key or not updates: | |
| raise ValueError("Key and updates required") | |
| # Load existing state | |
| state_file = self.state_dir / f"{key}.json" | |
| if state_file.exists(): | |
| with open(state_file) as f: | |
| state = json.load(f) | |
| else: | |
| state = {} | |
| # Apply updates | |
| if isinstance(state, dict) and isinstance(updates, dict): | |
| state.update(updates) | |
| else: | |
| state = updates | |
| # Save updated state | |
| with open(state_file, 'w') as f: | |
| json.dump(state, f, indent=2) | |
| return ExecuteResponse( | |
| status="success", | |
| result={"message": f"State {key} updated successfully"} | |
| ).dict() | |
| except Exception as e: | |
| self.logger.error(f"Error updating state: {str(e)}") | |
| return ExecuteResponse( | |
| status="error", | |
| error=str(e) | |
| ).dict() | |
| def handle_delete_state(self, request: ExecuteRequest) -> Dict[str, Any]: | |
| """Handle state deletion requests""" | |
| try: | |
| key = request.parameters.get("key") | |
| if not key: | |
| raise ValueError("State key not specified") | |
| # Delete state file if it exists | |
| state_file = self.state_dir / f"{key}.json" | |
| if state_file.exists(): | |
| state_file.unlink() | |
| return ExecuteResponse( | |
| status="success", | |
| result={"message": f"State {key} deleted successfully"} | |
| ).dict() | |
| except Exception as e: | |
| self.logger.error(f"Error deleting state: {str(e)}") | |
| return ExecuteResponse( | |
| status="error", | |
| error=str(e) | |
| ).dict() | |
| if __name__ == "__main__": | |
| agent = LangGraphNode() | |
| agent.run() |