diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..2cd26cd934b3d705177601d81458d23e63545fa0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.10-slim + +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + wget \ + gnupg \ + && rm -rf /var/lib/apt/lists/* + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Install Playwright browsers +RUN playwright install --with-deps chromium + +COPY . . + +EXPOSE 7860 + +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"] diff --git a/README.md b/README.md index a8e58422c0006db8de3a2e849f227d552a79f566..6562489f263f09baf193ccee9e0308fcb33cff12 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,64 @@ ---- -title: Orynxml Agents -emoji: 🦀 -colorFrom: gray -colorTo: yellow -sdk: docker -pinned: false ---- - -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +--- +title: ORYNXML Complete Backend with Agents +emoji: 🤖 +colorFrom: blue +colorTo: purple +sdk: docker +pinned: false +--- + +# ORYNXML Complete Backend with AI Agents + +FastAPI backend with integrated AI agents for ORYNXML AI Platform. + +## AI Agents + +### 1. Manus Agent (Main) +- Chat and conversation +- Task execution +- Tool orchestration +- General AI capabilities + +### 2. Software Engineer Agent (SWE) +- Code generation (Python, JavaScript, etc.) +- Code debugging and refactoring +- Architecture design +- Test generation + +### 3. Browser Agent +- Web scraping +- Browser automation +- Form filling +- Navigation and interaction + +### 4. Data Analysis Agent +- Data visualization +- Chart generation +- Statistical analysis +- Data transformation + +## API Endpoints + +### Authentication +- `POST /auth/signup` - Register user +- `POST /auth/login` - Login user + +### Agent Operations +- `POST /agent/run` - Run any agent with prompt +- `POST /agent/code` - Generate code (SWE agent) +- `POST /agent/browser` - Browser automation +- `POST /agent/data` - Data analysis +- `GET /agents/list` - List all agents + +### Status +- `GET /health` - Health check +- `GET /cloudflare/status` - Cloudflare status + +## Frontend +https://orynxml-ai.pages.dev + +## Architecture +- FastAPI REST API +- 4 specialized AI agents +- Cloudflare integration +- SQLite authentication diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..2c2516acbd65cbe3865931cbfc91e6ff88eeaee6 --- /dev/null +++ b/app.py @@ -0,0 +1,419 @@ +""" +ORYNXML Complete Backend with AI Agents +FastAPI REST API + Manus Agent + SWE Agent + Browser Agent + HuggingFace Agent +""" + +from fastapi import FastAPI, HTTPException, BackgroundTasks +from fastapi.middleware.cors import CORSMiddleware +from pydantic import BaseModel +from typing import Optional, List, Dict, Any +import os +import sys +import sqlite3 +import hashlib +import asyncio +from datetime import datetime + +# Add parent directory to path for imports +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + +# Import AI Agents +from app.agent.manus import Manus +from app.agent.swe import SWEAgent +from app.agent.browser import BrowserAgent +from app.agent.data_analysis import DataAnalysis +from app.llm import get_llm +from app.tool.tool_collection import ToolCollection + +# HuggingFace token +HF_TOKEN = os.getenv("HF_TOKEN", "") + +# Cloudflare Configuration +CLOUDFLARE_CONFIG = { + "api_token": os.getenv("CLOUDFLARE_API_TOKEN", ""), + "account_id": os.getenv("CLOUDFLARE_ACCOUNT_ID", "62af59a7ac82b29543577ee6800735ee"), + "d1_database_id": os.getenv("CLOUDFLARE_D1_DATABASE_ID", "6d887f74-98ac-4db7-bfed-8061903d1f6c"), + "r2_bucket_name": os.getenv("CLOUDFLARE_R2_BUCKET_NAME", "openmanus-storage"), + "kv_namespace_id": os.getenv("CLOUDFLARE_KV_NAMESPACE_ID", "87f4aa01410d4fb19821f61006f94441"), + "kv_namespace_cache": os.getenv("CLOUDFLARE_KV_CACHE_ID", "7b58c88292c847d1a82c8e0dd5129f37"), +} + +# Global agents (initialized on startup) +manus_agent = None +swe_agent = None +browser_agent = None +data_agent = None + +# Initialize FastAPI +app = FastAPI( + title="ORYNXML AI Platform with Agents", + description="Complete AI backend with Manus, SWE, Browser, and Data Analysis agents", + version="2.0.0", +) + +# CORS +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# Database +def init_database(): + conn = sqlite3.connect("openmanus.db") + cursor = conn.cursor() + cursor.execute(""" + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + mobile TEXT UNIQUE NOT NULL, + name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ) + """) + conn.commit() + conn.close() + +init_database() + +# Pydantic Models +class SignupRequest(BaseModel): + mobile: str + name: str + password: str + +class LoginRequest(BaseModel): + mobile: str + password: str + +class AgentRequest(BaseModel): + prompt: str + agent: Optional[str] = "manus" # manus, swe, browser, data + +class CodeRequest(BaseModel): + task: str + language: Optional[str] = "python" + +class BrowserRequest(BaseModel): + task: str + url: Optional[str] = None + +class DataRequest(BaseModel): + data: Any + task: str + +# Helper Functions +def hash_password(password: str) -> str: + return hashlib.sha256(password.encode()).hexdigest() + +def verify_password(password: str, password_hash: str) -> bool: + return hash_password(password) == password_hash + +# Startup event - Initialize agents +@app.on_event("startup") +async def startup_event(): + global manus_agent, swe_agent, browser_agent, data_agent + + print("🚀 Initializing AI Agents...") + + try: + # Initialize Manus (main agent) + manus_agent = await Manus.create() + print("✅ Manus Agent initialized") + + # Initialize SWE Agent + swe_agent = await SWEAgent.create() + print("✅ SWE Agent initialized") + + # Initialize Browser Agent + browser_agent = await BrowserAgent.create() + print("✅ Browser Agent initialized") + + # Initialize Data Analysis Agent + data_agent = await DataAnalysis.create() + print("✅ Data Analysis Agent initialized") + + print("🎉 All agents ready!") + + except Exception as e: + print(f"⚠️ Warning: Could not initialize all agents: {e}") + print("API will still work with limited functionality") + +# API Endpoints + +@app.get("/") +async def root(): + return { + "message": "ORYNXML AI Platform with Agents", + "version": "2.0.0", + "agents": { + "manus": "Main agent with all capabilities" if manus_agent else "Not initialized", + "swe": "Software Engineer agent" if swe_agent else "Not initialized", + "browser": "Browser automation agent" if browser_agent else "Not initialized", + "data": "Data analysis agent" if data_agent else "Not initialized", + }, + "endpoints": { + "health": "/health", + "auth": "/auth/signup, /auth/login", + "agents": "/agent/run, /agent/code, /agent/browser, /agent/data", + } + } + +@app.get("/health") +async def health_check(): + return { + "status": "healthy", + "timestamp": datetime.now().isoformat(), + "agents_initialized": { + "manus": manus_agent is not None, + "swe": swe_agent is not None, + "browser": browser_agent is not None, + "data": data_agent is not None, + }, + "cloudflare_configured": bool(CLOUDFLARE_CONFIG["api_token"]), + } + +@app.post("/auth/signup") +async def signup(request: SignupRequest): + try: + if len(request.password) < 6: + raise HTTPException(status_code=400, detail="Password must be at least 6 characters") + + conn = sqlite3.connect("openmanus.db") + cursor = conn.cursor() + + cursor.execute("SELECT mobile FROM users WHERE mobile = ?", (request.mobile,)) + if cursor.fetchone(): + conn.close() + raise HTTPException(status_code=400, detail="Mobile number already registered") + + password_hash = hash_password(request.password) + cursor.execute( + "INSERT INTO users (mobile, name, password_hash) VALUES (?, ?, ?)", + (request.mobile, request.name, password_hash) + ) + conn.commit() + conn.close() + + return { + "success": True, + "message": "Account created successfully", + "mobile": request.mobile, + "name": request.name + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Registration failed: {str(e)}") + +@app.post("/auth/login") +async def login(request: LoginRequest): + try: + conn = sqlite3.connect("openmanus.db") + cursor = conn.cursor() + + cursor.execute( + "SELECT name, password_hash FROM users WHERE mobile = ?", + (request.mobile,) + ) + result = cursor.fetchone() + conn.close() + + if not result: + raise HTTPException(status_code=401, detail="Invalid mobile number or password") + + name, password_hash = result + + if not verify_password(request.password, password_hash): + raise HTTPException(status_code=401, detail="Invalid mobile number or password") + + return { + "success": True, + "message": "Login successful", + "user": { + "mobile": request.mobile, + "name": name + }, + "token": f"session_{hash_password(request.mobile + str(datetime.now()))[:32]}" + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Login failed: {str(e)}") + +@app.post("/agent/run") +async def run_agent(request: AgentRequest): + """Run any agent with a prompt""" + try: + agent_name = request.agent.lower() + + # Select agent + if agent_name == "manus": + if not manus_agent: + raise HTTPException(status_code=503, detail="Manus agent not initialized") + agent = manus_agent + elif agent_name == "swe": + if not swe_agent: + raise HTTPException(status_code=503, detail="SWE agent not initialized") + agent = swe_agent + elif agent_name == "browser": + if not browser_agent: + raise HTTPException(status_code=503, detail="Browser agent not initialized") + agent = browser_agent + elif agent_name == "data": + if not data_agent: + raise HTTPException(status_code=503, detail="Data agent not initialized") + agent = data_agent + else: + raise HTTPException(status_code=400, detail=f"Unknown agent: {agent_name}") + + # Run agent + result = await agent.run(request.prompt) + + return { + "success": True, + "agent": agent_name, + "prompt": request.prompt, + "result": str(result), + "timestamp": datetime.now().isoformat() + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Agent execution failed: {str(e)}") + +@app.post("/agent/code") +async def generate_code(request: CodeRequest): + """Software Engineer Agent - Generate code""" + try: + if not swe_agent: + raise HTTPException(status_code=503, detail="SWE agent not initialized") + + prompt = f"Generate {request.language} code for: {request.task}" + result = await swe_agent.run(prompt) + + return { + "success": True, + "task": request.task, + "language": request.language, + "code": str(result), + "timestamp": datetime.now().isoformat() + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Code generation failed: {str(e)}") + +@app.post("/agent/browser") +async def browser_automation(request: BrowserRequest): + """Browser Agent - Automate web tasks""" + try: + if not browser_agent: + raise HTTPException(status_code=503, detail="Browser agent not initialized") + + prompt = f"{request.task}" + if request.url: + prompt += f" on {request.url}" + + result = await browser_agent.run(prompt) + + return { + "success": True, + "task": request.task, + "url": request.url, + "result": str(result), + "timestamp": datetime.now().isoformat() + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Browser automation failed: {str(e)}") + +@app.post("/agent/data") +async def analyze_data(request: DataRequest): + """Data Analysis Agent - Analyze and visualize data""" + try: + if not data_agent: + raise HTTPException(status_code=503, detail="Data agent not initialized") + + prompt = f"Analyze this data: {request.data}. Task: {request.task}" + result = await data_agent.run(prompt) + + return { + "success": True, + "task": request.task, + "result": str(result), + "timestamp": datetime.now().isoformat() + } + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=f"Data analysis failed: {str(e)}") + +@app.get("/agents/list") +async def list_agents(): + """List all available agents and their status""" + return { + "agents": [ + { + "name": "manus", + "description": "Main agent with all capabilities (chat, coding, browsing, data analysis)", + "status": "initialized" if manus_agent else "not initialized", + "endpoint": "/agent/run" + }, + { + "name": "swe", + "description": "Software Engineer agent (code generation, debugging, refactoring)", + "status": "initialized" if swe_agent else "not initialized", + "endpoint": "/agent/code" + }, + { + "name": "browser", + "description": "Browser automation agent (web scraping, form filling, navigation)", + "status": "initialized" if browser_agent else "not initialized", + "endpoint": "/agent/browser" + }, + { + "name": "data", + "description": "Data analysis agent (charts, visualization, statistics)", + "status": "initialized" if data_agent else "not initialized", + "endpoint": "/agent/data" + } + ] + } + +@app.get("/cloudflare/status") +async def cloudflare_status(): + services = [] + if CLOUDFLARE_CONFIG["api_token"]: + services.append("✅ API Token Configured") + if CLOUDFLARE_CONFIG["d1_database_id"]: + services.append("✅ D1 Database Connected") + if CLOUDFLARE_CONFIG["r2_bucket_name"]: + services.append("✅ R2 Storage Connected") + if CLOUDFLARE_CONFIG["kv_namespace_id"]: + services.append("✅ KV Sessions Connected") + if CLOUDFLARE_CONFIG["kv_namespace_cache"]: + services.append("✅ KV Cache Connected") + + return { + "configured": len(services) > 0, + "services": services, + "account_id": CLOUDFLARE_CONFIG["account_id"] + } + +if __name__ == "__main__": + uvicorn.run( + app, + host="0.0.0.0", + port=7860, + log_level="info" + ) diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..0749c6de16b761fd11e14794578c68d54ce3dc0e --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,10 @@ +# Python version check: 3.11-3.13 +import sys + + +if sys.version_info < (3, 11) or sys.version_info > (3, 13): + print( + "Warning: Unsupported Python version {ver}, please use 3.11-3.13".format( + ver=".".join(map(str, sys.version_info)) + ) + ) diff --git a/app/agent/__init__.py b/app/agent/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..f7df2b9ba7d6f5b6b775fe2e51e447e8b5d783fc --- /dev/null +++ b/app/agent/__init__.py @@ -0,0 +1,16 @@ +from app.agent.base import BaseAgent +from app.agent.browser import BrowserAgent +from app.agent.mcp import MCPAgent +from app.agent.react import ReActAgent +from app.agent.swe import SWEAgent +from app.agent.toolcall import ToolCallAgent + + +__all__ = [ + "BaseAgent", + "BrowserAgent", + "ReActAgent", + "SWEAgent", + "ToolCallAgent", + "MCPAgent", +] diff --git a/app/agent/base.py b/app/agent/base.py new file mode 100644 index 0000000000000000000000000000000000000000..65f660073527cee9a9385a482123bd7632de5b09 --- /dev/null +++ b/app/agent/base.py @@ -0,0 +1,196 @@ +from abc import ABC, abstractmethod +from contextlib import asynccontextmanager +from typing import List, Optional + +from pydantic import BaseModel, Field, model_validator + +from app.llm import LLM +from app.logger import logger +from app.sandbox.client import SANDBOX_CLIENT +from app.schema import ROLE_TYPE, AgentState, Memory, Message + + +class BaseAgent(BaseModel, ABC): + """Abstract base class for managing agent state and execution. + + Provides foundational functionality for state transitions, memory management, + and a step-based execution loop. Subclasses must implement the `step` method. + """ + + # Core attributes + name: str = Field(..., description="Unique name of the agent") + description: Optional[str] = Field(None, description="Optional agent description") + + # Prompts + system_prompt: Optional[str] = Field( + None, description="System-level instruction prompt" + ) + next_step_prompt: Optional[str] = Field( + None, description="Prompt for determining next action" + ) + + # Dependencies + llm: LLM = Field(default_factory=LLM, description="Language model instance") + memory: Memory = Field(default_factory=Memory, description="Agent's memory store") + state: AgentState = Field( + default=AgentState.IDLE, description="Current agent state" + ) + + # Execution control + max_steps: int = Field(default=10, description="Maximum steps before termination") + current_step: int = Field(default=0, description="Current step in execution") + + duplicate_threshold: int = 2 + + class Config: + arbitrary_types_allowed = True + extra = "allow" # Allow extra fields for flexibility in subclasses + + @model_validator(mode="after") + def initialize_agent(self) -> "BaseAgent": + """Initialize agent with default settings if not provided.""" + if self.llm is None or not isinstance(self.llm, LLM): + self.llm = LLM(config_name=self.name.lower()) + if not isinstance(self.memory, Memory): + self.memory = Memory() + return self + + @asynccontextmanager + async def state_context(self, new_state: AgentState): + """Context manager for safe agent state transitions. + + Args: + new_state: The state to transition to during the context. + + Yields: + None: Allows execution within the new state. + + Raises: + ValueError: If the new_state is invalid. + """ + if not isinstance(new_state, AgentState): + raise ValueError(f"Invalid state: {new_state}") + + previous_state = self.state + self.state = new_state + try: + yield + except Exception as e: + self.state = AgentState.ERROR # Transition to ERROR on failure + raise e + finally: + self.state = previous_state # Revert to previous state + + def update_memory( + self, + role: ROLE_TYPE, # type: ignore + content: str, + base64_image: Optional[str] = None, + **kwargs, + ) -> None: + """Add a message to the agent's memory. + + Args: + role: The role of the message sender (user, system, assistant, tool). + content: The message content. + base64_image: Optional base64 encoded image. + **kwargs: Additional arguments (e.g., tool_call_id for tool messages). + + Raises: + ValueError: If the role is unsupported. + """ + message_map = { + "user": Message.user_message, + "system": Message.system_message, + "assistant": Message.assistant_message, + "tool": lambda content, **kw: Message.tool_message(content, **kw), + } + + if role not in message_map: + raise ValueError(f"Unsupported message role: {role}") + + # Create message with appropriate parameters based on role + kwargs = {"base64_image": base64_image, **(kwargs if role == "tool" else {})} + self.memory.add_message(message_map[role](content, **kwargs)) + + async def run(self, request: Optional[str] = None) -> str: + """Execute the agent's main loop asynchronously. + + Args: + request: Optional initial user request to process. + + Returns: + A string summarizing the execution results. + + Raises: + RuntimeError: If the agent is not in IDLE state at start. + """ + if self.state != AgentState.IDLE: + raise RuntimeError(f"Cannot run agent from state: {self.state}") + + if request: + self.update_memory("user", request) + + results: List[str] = [] + async with self.state_context(AgentState.RUNNING): + while ( + self.current_step < self.max_steps and self.state != AgentState.FINISHED + ): + self.current_step += 1 + logger.info(f"Executing step {self.current_step}/{self.max_steps}") + step_result = await self.step() + + # Check for stuck state + if self.is_stuck(): + self.handle_stuck_state() + + results.append(f"Step {self.current_step}: {step_result}") + + if self.current_step >= self.max_steps: + self.current_step = 0 + self.state = AgentState.IDLE + results.append(f"Terminated: Reached max steps ({self.max_steps})") + await SANDBOX_CLIENT.cleanup() + return "\n".join(results) if results else "No steps executed" + + @abstractmethod + async def step(self) -> str: + """Execute a single step in the agent's workflow. + + Must be implemented by subclasses to define specific behavior. + """ + + def handle_stuck_state(self): + """Handle stuck state by adding a prompt to change strategy""" + stuck_prompt = "\ + Observed duplicate responses. Consider new strategies and avoid repeating ineffective paths already attempted." + self.next_step_prompt = f"{stuck_prompt}\n{self.next_step_prompt}" + logger.warning(f"Agent detected stuck state. Added prompt: {stuck_prompt}") + + def is_stuck(self) -> bool: + """Check if the agent is stuck in a loop by detecting duplicate content""" + if len(self.memory.messages) < 2: + return False + + last_message = self.memory.messages[-1] + if not last_message.content: + return False + + # Count identical content occurrences + duplicate_count = sum( + 1 + for msg in reversed(self.memory.messages[:-1]) + if msg.role == "assistant" and msg.content == last_message.content + ) + + return duplicate_count >= self.duplicate_threshold + + @property + def messages(self) -> List[Message]: + """Retrieve a list of messages from the agent's memory.""" + return self.memory.messages + + @messages.setter + def messages(self, value: List[Message]): + """Set the list of messages in the agent's memory.""" + self.memory.messages = value diff --git a/app/agent/browser.py b/app/agent/browser.py new file mode 100644 index 0000000000000000000000000000000000000000..3f25c45393d4341a2ae6fe8212bfc8a9e7f86a61 --- /dev/null +++ b/app/agent/browser.py @@ -0,0 +1,129 @@ +import json +from typing import TYPE_CHECKING, Optional + +from pydantic import Field, model_validator + +from app.agent.toolcall import ToolCallAgent +from app.logger import logger +from app.prompt.browser import NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.schema import Message, ToolChoice +from app.tool import BrowserUseTool, Terminate, ToolCollection +from app.tool.sandbox.sb_browser_tool import SandboxBrowserTool + + +# Avoid circular import if BrowserAgent needs BrowserContextHelper +if TYPE_CHECKING: + from app.agent.base import BaseAgent # Or wherever memory is defined + + +class BrowserContextHelper: + def __init__(self, agent: "BaseAgent"): + self.agent = agent + self._current_base64_image: Optional[str] = None + + async def get_browser_state(self) -> Optional[dict]: + browser_tool = self.agent.available_tools.get_tool(BrowserUseTool().name) + if not browser_tool: + browser_tool = self.agent.available_tools.get_tool( + SandboxBrowserTool().name + ) + if not browser_tool or not hasattr(browser_tool, "get_current_state"): + logger.warning("BrowserUseTool not found or doesn't have get_current_state") + return None + try: + result = await browser_tool.get_current_state() + if result.error: + logger.debug(f"Browser state error: {result.error}") + return None + if hasattr(result, "base64_image") and result.base64_image: + self._current_base64_image = result.base64_image + else: + self._current_base64_image = None + return json.loads(result.output) + except Exception as e: + logger.debug(f"Failed to get browser state: {str(e)}") + return None + + async def format_next_step_prompt(self) -> str: + """Gets browser state and formats the browser prompt.""" + browser_state = await self.get_browser_state() + url_info, tabs_info, content_above_info, content_below_info = "", "", "", "" + results_info = "" # Or get from agent if needed elsewhere + + if browser_state and not browser_state.get("error"): + url_info = f"\n URL: {browser_state.get('url', 'N/A')}\n Title: {browser_state.get('title', 'N/A')}" + tabs = browser_state.get("tabs", []) + if tabs: + tabs_info = f"\n {len(tabs)} tab(s) available" + pixels_above = browser_state.get("pixels_above", 0) + pixels_below = browser_state.get("pixels_below", 0) + if pixels_above > 0: + content_above_info = f" ({pixels_above} pixels)" + if pixels_below > 0: + content_below_info = f" ({pixels_below} pixels)" + + if self._current_base64_image: + image_message = Message.user_message( + content="Current browser screenshot:", + base64_image=self._current_base64_image, + ) + self.agent.memory.add_message(image_message) + self._current_base64_image = None # Consume the image after adding + + return NEXT_STEP_PROMPT.format( + url_placeholder=url_info, + tabs_placeholder=tabs_info, + content_above_placeholder=content_above_info, + content_below_placeholder=content_below_info, + results_placeholder=results_info, + ) + + async def cleanup_browser(self): + browser_tool = self.agent.available_tools.get_tool(BrowserUseTool().name) + if browser_tool and hasattr(browser_tool, "cleanup"): + await browser_tool.cleanup() + + +class BrowserAgent(ToolCallAgent): + """ + A browser agent that uses the browser_use library to control a browser. + + This agent can navigate web pages, interact with elements, fill forms, + extract content, and perform other browser-based actions to accomplish tasks. + """ + + name: str = "browser" + description: str = "A browser agent that can control a browser to accomplish tasks" + + system_prompt: str = SYSTEM_PROMPT + next_step_prompt: str = NEXT_STEP_PROMPT + + max_observe: int = 10000 + max_steps: int = 20 + + # Configure the available tools + available_tools: ToolCollection = Field( + default_factory=lambda: ToolCollection(BrowserUseTool(), Terminate()) + ) + + # Use Auto for tool choice to allow both tool usage and free-form responses + tool_choices: ToolChoice = ToolChoice.AUTO + special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name]) + + browser_context_helper: Optional[BrowserContextHelper] = None + + @model_validator(mode="after") + def initialize_helper(self) -> "BrowserAgent": + self.browser_context_helper = BrowserContextHelper(self) + return self + + async def think(self) -> bool: + """Process current state and decide next actions using tools, with browser state info added""" + self.next_step_prompt = ( + await self.browser_context_helper.format_next_step_prompt() + ) + return await super().think() + + async def cleanup(self): + """Clean up browser agent resources by calling parent cleanup.""" + await self.browser_context_helper.cleanup_browser() diff --git a/app/agent/data_analysis.py b/app/agent/data_analysis.py new file mode 100644 index 0000000000000000000000000000000000000000..79b9c2967af60237d37cf014f440ae83184a15ea --- /dev/null +++ b/app/agent/data_analysis.py @@ -0,0 +1,37 @@ +from pydantic import Field + +from app.agent.toolcall import ToolCallAgent +from app.config import config +from app.prompt.visualization import NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.tool import Terminate, ToolCollection +from app.tool.chart_visualization.chart_prepare import VisualizationPrepare +from app.tool.chart_visualization.data_visualization import DataVisualization +from app.tool.chart_visualization.python_execute import NormalPythonExecute + + +class DataAnalysis(ToolCallAgent): + """ + A data analysis agent that uses planning to solve various data analysis tasks. + + This agent extends ToolCallAgent with a comprehensive set of tools and capabilities, + including Data Analysis, Chart Visualization, Data Report. + """ + + name: str = "Data_Analysis" + description: str = "An analytical agent that utilizes python and data visualization tools to solve diverse data analysis tasks" + + system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root) + next_step_prompt: str = NEXT_STEP_PROMPT + + max_observe: int = 15000 + max_steps: int = 20 + + # Add general-purpose tools to the tool collection + available_tools: ToolCollection = Field( + default_factory=lambda: ToolCollection( + NormalPythonExecute(), + VisualizationPrepare(), + DataVisualization(), + Terminate(), + ) + ) diff --git a/app/agent/huggingface_agent.py b/app/agent/huggingface_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..b53845fba0e3d45fcd88961c0f2541e5266e2b7c --- /dev/null +++ b/app/agent/huggingface_agent.py @@ -0,0 +1,889 @@ +""" +Hugging Face Agent Integration for OpenManus +Extends the main AI agent with access to thousands of HuggingFace models +""" + +import os +from typing import Any, Dict, List, Optional + +from app.agent.base import BaseAgent +from app.huggingface_models import ModelCategory +from app.logger import logger +from app.tool.huggingface_models_tool import HuggingFaceModelsTool + + +class HuggingFaceAgent(BaseAgent): + """AI Agent with integrated HuggingFace model access""" + + def __init__(self, **config): + super().__init__(**config) + + # Initialize HuggingFace integration + hf_token = os.getenv("HUGGINGFACE_TOKEN") or config.get("huggingface_token") + if not hf_token: + logger.warning( + "No Hugging Face token provided. HF models will not be available." + ) + self.hf_tool = None + else: + self.hf_tool = HuggingFaceModelsTool(hf_token) + + # Default models for different tasks + self.default_models = { + "text_generation": "MiniMax-M2", # Latest high-performance model + "image_generation": "FLUX.1 Dev", # Best quality image generation + "speech_recognition": "Whisper Large v3", # Best multilingual ASR + "text_to_speech": "Kokoro 82M", # High quality, lightweight TTS + "image_classification": "ViT Base Patch16", # General image classification + "embeddings": "Sentence Transformers All MiniLM", # Fast embeddings + "translation": "M2M100 1.2B", # Multilingual translation + "summarization": "PEGASUS XSum", # Abstractive summarization + } + + async def generate_text_with_hf( + self, + prompt: str, + model_name: Optional[str] = None, + max_tokens: int = 200, + temperature: float = 0.7, + stream: bool = False, + ) -> Dict[str, Any]: + """Generate text using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["text_generation"] + + return await self.hf_tool.text_generation( + model_name=model_name, + prompt=prompt, + max_tokens=max_tokens, + temperature=temperature, + stream=stream, + ) + + async def generate_image_with_hf( + self, + prompt: str, + model_name: Optional[str] = None, + negative_prompt: Optional[str] = None, + width: int = 1024, + height: int = 1024, + ) -> Dict[str, Any]: + """Generate images using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["image_generation"] + + return await self.hf_tool.generate_image( + model_name=model_name, + prompt=prompt, + negative_prompt=negative_prompt, + width=width, + height=height, + ) + + async def transcribe_audio_with_hf( + self, + audio_data: bytes, + model_name: Optional[str] = None, + language: Optional[str] = None, + ) -> Dict[str, Any]: + """Transcribe audio using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["speech_recognition"] + + return await self.hf_tool.transcribe_audio( + model_name=model_name, audio_data=audio_data, language=language + ) + + async def synthesize_speech_with_hf( + self, + text: str, + model_name: Optional[str] = None, + voice_id: Optional[str] = None, + ) -> Dict[str, Any]: + """Generate speech from text using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["text_to_speech"] + + return await self.hf_tool.text_to_speech( + model_name=model_name, text=text, voice_id=voice_id + ) + + async def classify_image_with_hf( + self, image_data: bytes, model_name: Optional[str] = None, task: str = "general" + ) -> Dict[str, Any]: + """Classify images using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + # Choose model based on task + if task == "nsfw": + model_name = "NSFW Image Detection" + elif task == "emotions": + model_name = "Facial Emotions Detection" + elif task == "deepfake": + model_name = "Deepfake Detection" + else: + model_name = model_name or self.default_models["image_classification"] + + return await self.hf_tool.classify_image( + model_name=model_name, image_data=image_data + ) + + async def get_text_embeddings_with_hf( + self, texts: List[str], model_name: Optional[str] = None + ) -> Dict[str, Any]: + """Get text embeddings using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["embeddings"] + + return await self.hf_tool.get_embeddings(model_name=model_name, texts=texts) + + async def translate_with_hf( + self, + text: str, + target_language: str, + source_language: Optional[str] = None, + model_name: Optional[str] = None, + ) -> Dict[str, Any]: + """Translate text using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["translation"] + + return await self.hf_tool.translate_text( + model_name=model_name, + text=text, + source_language=source_language, + target_language=target_language, + ) + + async def summarize_with_hf( + self, text: str, model_name: Optional[str] = None, max_length: int = 150 + ) -> Dict[str, Any]: + """Summarize text using HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or self.default_models["summarization"] + + return await self.hf_tool.summarize_text( + model_name=model_name, text=text, max_length=max_length + ) + + def get_available_hf_models(self, category: Optional[str] = None) -> Dict[str, Any]: + """Get list of available HuggingFace models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + return self.hf_tool.list_available_models(category) + + async def smart_model_selection( + self, task_description: str, content_type: str = "text" + ) -> str: + """ + Intelligently select the best HuggingFace model for a task + + Args: + task_description: Description of what the user wants to do + content_type: Type of content (text, image, audio, video) + """ + task_lower = task_description.lower() + + # Video generation and processing + if any( + keyword in task_lower + for keyword in [ + "video", + "movie", + "animation", + "motion", + "gif", + "sequence", + "frames", + ] + ): + if "generate" in task_lower or "create" in task_lower: + return "Stable Video Diffusion" + elif "analyze" in task_lower or "describe" in task_lower: + return "Video ChatGPT" + else: + return "AnimateDiff" + + # Code and App Development + elif any( + keyword in task_lower + for keyword in [ + "code", + "programming", + "app", + "application", + "software", + "develop", + "build", + "function", + "class", + "api", + "database", + "website", + "frontend", + "backend", + ] + ): + if "app" in task_lower or "application" in task_lower: + return "CodeLlama 34B Instruct" # Best for full applications + elif "python" in task_lower: + return "WizardCoder 34B" # Python specialist + elif "api" in task_lower: + return "StarCoder2 15B" # Good for APIs + elif "explain" in task_lower or "comment" in task_lower: + return "Phind CodeLlama" # Best for code explanation + else: + return "DeepSeek Coder V2" # General coding + + # 3D and AR/VR Content + elif any( + keyword in task_lower + for keyword in [ + "3d", + "three dimensional", + "mesh", + "model", + "obj", + "stl", + "ar", + "vr", + "augmented reality", + "virtual reality", + "texture", + "material", + ] + ): + if "text" in task_lower and ("3d" in task_lower or "model" in task_lower): + return "Shap-E" + elif "image" in task_lower and "3d" in task_lower: + return "DreamFusion" + else: + return "Point-E" + + # Document Processing and OCR + elif any( + keyword in task_lower + for keyword in [ + "ocr", + "document", + "pdf", + "scan", + "extract text", + "handwriting", + "form", + "table", + "layout", + "invoice", + "receipt", + "contract", + ] + ): + if "handwriting" in task_lower or "handwritten" in task_lower: + return "TrOCR Handwritten" + elif "table" in task_lower: + return "TableTransformer" + elif "form" in task_lower: + return "FormNet" + else: + return "TrOCR Large" + + # Multimodal AI + elif any( + keyword in task_lower + for keyword in [ + "visual question", + "image question", + "describe image", + "multimodal", + "vision language", + "image text", + "cross modal", + ] + ): + if "chat" in task_lower or "conversation" in task_lower: + return "GPT-4V" + elif "question" in task_lower: + return "LLaVA" + else: + return "BLIP-2" + + # Creative Content + elif any( + keyword in task_lower + for keyword in [ + "story", + "creative", + "poem", + "poetry", + "novel", + "screenplay", + "script", + "blog", + "article", + "marketing", + "copy", + "advertising", + ] + ): + if "story" in task_lower or "novel" in task_lower: + return "Novel AI" + elif "poem" in task_lower or "poetry" in task_lower: + return "Poet Assistant" + elif "marketing" in task_lower or "copy" in task_lower: + return "Marketing Copy AI" + else: + return "GPT-3.5 Creative" + + # Game Development + elif any( + keyword in task_lower + for keyword in [ + "game", + "character", + "npc", + "level", + "dialogue", + "asset", + "quest", + "gameplay", + "mechanic", + "unity", + "unreal", + ] + ): + if "character" in task_lower: + return "Character AI" + elif "level" in task_lower or "environment" in task_lower: + return "Level Designer" + elif "dialogue" in task_lower or "conversation" in task_lower: + return "Dialogue Writer" + else: + return "Asset Creator" + + # Science and Research + elif any( + keyword in task_lower + for keyword in [ + "research", + "scientific", + "paper", + "analysis", + "data", + "protein", + "molecule", + "chemistry", + "biology", + "physics", + "experiment", + ] + ): + if "protein" in task_lower or "folding" in task_lower: + return "AlphaFold" + elif "molecule" in task_lower or "chemistry" in task_lower: + return "ChemBERTa" + elif "data" in task_lower and "analysis" in task_lower: + return "Data Analyst" + else: + return "SciBERT" + + # Business and Productivity + elif any( + keyword in task_lower + for keyword in [ + "email", + "business", + "report", + "presentation", + "meeting", + "project", + "plan", + "proposal", + "memo", + "letter", + "professional", + ] + ): + if "email" in task_lower: + return "Email Assistant" + elif "presentation" in task_lower: + return "Presentation AI" + elif "report" in task_lower: + return "Report Writer" + elif "meeting" in task_lower: + return "Meeting Summarizer" + else: + return "Project Planner" + + # Specialized AI + elif any( + keyword in task_lower + for keyword in [ + "music", + "audio", + "sound", + "voice clone", + "enhance", + "restore", + "upscale", + "remove background", + "inpaint", + "style transfer", + ] + ): + if "music" in task_lower: + return "MusicGen" + elif "voice" in task_lower and "clone" in task_lower: + return "Voice Cloner" + elif "upscale" in task_lower or "enhance" in task_lower: + return "Real-ESRGAN" + elif "background" in task_lower and "remove" in task_lower: + return "Background Remover" + elif "restore" in task_lower or "face" in task_lower: + return "GFPGAN" + else: + return "LaMa" + + # Traditional categories + elif any( + keyword in task_lower + for keyword in [ + "generate", + "write", + "create", + "compose", + "chat", + "conversation", + ] + ): + if "chat" in task_lower or "conversation" in task_lower: + return "Llama 3.1 8B Instruct" + else: + return "MiniMax-M2" + + # Image generation + elif any( + keyword in task_lower + for keyword in ["image", "picture", "draw", "art", "photo", "visual"] + ): + if "fast" in task_lower or "quick" in task_lower: + return "FLUX.1 Schnell" + else: + return "FLUX.1 Dev" + + # Audio processing + elif any( + keyword in task_lower + for keyword in ["transcribe", "speech to text", "recognize", "audio"] + ): + if content_type == "audio" or "transcribe" in task_lower: + return "Whisper Large v3" + + # Text-to-speech + elif any( + keyword in task_lower + for keyword in ["speak", "voice", "text to speech", "tts"] + ): + if "fast" in task_lower: + return "Kokoro 82M" # Lightweight and fast + else: + return "VibeVoice 1.5B" # High quality + + # Image analysis + elif ( + any( + keyword in task_lower + for keyword in ["classify", "analyze image", "detect", "recognize"] + ) + and content_type == "image" + ): + if "nsfw" in task_lower or "safe" in task_lower: + return "NSFW Image Detection" + elif "emotion" in task_lower or "face" in task_lower: + return "Facial Emotions Detection" + elif "deepfake" in task_lower or "fake" in task_lower: + return "Deepfake Detection" + else: + return "ViT Base Patch16" # General classification + + # Translation + elif any( + keyword in task_lower for keyword in ["translate", "language", "convert"] + ): + return "M2M100 1.2B" # Multilingual translation + + # Summarization + elif any( + keyword in task_lower + for keyword in ["summarize", "summary", "abstract", "brief"] + ): + return "PEGASUS XSum" # Best summarization + + # Embeddings/similarity + elif any( + keyword in task_lower + for keyword in ["similar", "embed", "vector", "search", "match"] + ): + return "Sentence Transformers All MiniLM" # Fast embeddings + + # Default fallback + else: + return "MiniMax-M2" # Best general-purpose model + + async def execute_hf_task( + self, task: str, content: Any, model_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: + """ + Execute any HuggingFace task with intelligent model selection + + Args: + task: Task description (e.g., "generate image", "transcribe audio") + content: Input content (text, image bytes, audio bytes) + model_name: Specific model to use (optional) + **kwargs: Additional parameters + """ + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + try: + task_lower = task.lower() + + # Determine content type + content_type = "text" + if isinstance(content, bytes): + if ( + b"PNG" in content[:20] + or b"JFIF" in content[:20] + or b"GIF" in content[:20] + ): + content_type = "image" + else: + content_type = "audio" + + # Auto-select model if not specified + if not model_name: + model_name = await self.smart_model_selection(task, content_type) + + # Route to appropriate method based on task + if "generate" in task_lower and ( + "image" in task_lower or "picture" in task_lower + ): + return await self.generate_image_with_hf(content, model_name, **kwargs) + + elif "transcribe" in task_lower or "speech to text" in task_lower: + return await self.transcribe_audio_with_hf( + content, model_name, **kwargs + ) + + elif "text to speech" in task_lower or "tts" in task_lower: + return await self.synthesize_speech_with_hf( + content, model_name, **kwargs + ) + + elif "classify" in task_lower and content_type == "image": + return await self.classify_image_with_hf(content, model_name, **kwargs) + + elif "embed" in task_lower or "vector" in task_lower: + texts = [content] if isinstance(content, str) else content + return await self.get_text_embeddings_with_hf(texts, model_name) + + elif "translate" in task_lower: + return await self.translate_with_hf( + content, model_name=model_name, **kwargs + ) + + elif "summarize" in task_lower: + return await self.summarize_with_hf(content, model_name, **kwargs) + + else: + # Default to text generation + return await self.generate_text_with_hf(content, model_name, **kwargs) + + except Exception as e: + logger.error(f"HuggingFace task execution failed: {e}") + return {"error": f"Task execution failed: {str(e)}"} + + async def chat_with_hf_models( + self, message: str, conversation_history: List[Dict] = None + ) -> Dict[str, Any]: + """ + Enhanced chat with access to HuggingFace models + + This method extends the base agent's capabilities with HF models + """ + # Check if the user is asking for HuggingFace-specific functionality + message_lower = message.lower() + + # Handle model listing requests + if "list" in message_lower and ( + "model" in message_lower or "hf" in message_lower + ): + return self.get_available_hf_models() + + # Handle specific model requests + hf_keywords = [ + "generate image", + "create image", + "draw", + "picture", + "transcribe", + "speech to text", + "audio", + "text to speech", + "speak", + "voice", + "translate", + "language", + "classify image", + "embed", + "vector", + "similarity", + "summarize", + ] + + if any(keyword in message_lower for keyword in hf_keywords): + # This is likely a HuggingFace model request + return await self.execute_hf_task(message, message) + + # For regular chat, we can enhance responses with HF models + # First get a response from the base agent + base_response = await super().chat(message, conversation_history) + + # Optionally enhance with HF capabilities if relevant + if "image" in message_lower and "generate" in message_lower: + # User might want image generation + base_response["hf_suggestion"] = { + "action": "generate_image", + "models": ["FLUX.1 Dev", "FLUX.1 Schnell", "Stable Diffusion XL"], + "message": "I can also generate images for you using HuggingFace models. Just ask!", + } + + return base_response + + # New methods for expanded model categories + + async def generate_video_with_hf( + self, prompt: str, model_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: + """Generate video from text prompt""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "Stable Video Diffusion" + return await self.hf_tool.text_to_video( + model_name=model_name, prompt=prompt, **kwargs + ) + + async def generate_code_with_hf( + self, + prompt: str, + language: str = "python", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate code from natural language description""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "CodeLlama 34B Instruct" + return await self.hf_tool.code_generation( + model_name=model_name, prompt=prompt, language=language, **kwargs + ) + + async def generate_app_with_hf( + self, + description: str, + app_type: str = "web_app", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate complete application from description""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "CodeLlama 34B Instruct" + enhanced_prompt = f"Create a {app_type} application: {description}" + return await self.hf_tool.code_generation( + model_name=model_name, prompt=enhanced_prompt, **kwargs + ) + + async def generate_3d_model_with_hf( + self, prompt: str, model_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: + """Generate 3D model from text description""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "Shap-E" + return await self.hf_tool.text_to_3d( + model_name=model_name, prompt=prompt, **kwargs + ) + + async def process_document_with_hf( + self, + document_data: bytes, + task_type: str = "ocr", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Process documents with OCR and analysis""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + if task_type == "ocr": + model_name = model_name or "TrOCR Large" + return await self.hf_tool.ocr( + model_name=model_name, image_data=document_data, **kwargs + ) + else: + model_name = model_name or "LayoutLMv3" + return await self.hf_tool.document_analysis( + model_name=model_name, document_data=document_data, **kwargs + ) + + async def multimodal_chat_with_hf( + self, image_data: bytes, text: str, model_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: + """Chat with images using multimodal models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "BLIP-2" + return await self.hf_tool.vision_language( + model_name=model_name, image_data=image_data, text=text, **kwargs + ) + + async def generate_music_with_hf( + self, + prompt: str, + duration: int = 30, + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate music from text description""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "MusicGen" + return await self.hf_tool.music_generation( + model_name=model_name, prompt=prompt, duration=duration, **kwargs + ) + + async def enhance_image_with_hf( + self, + image_data: bytes, + task_type: str = "super_resolution", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Enhance images with various AI models""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + if task_type == "super_resolution": + model_name = model_name or "Real-ESRGAN" + return await self.hf_tool.super_resolution( + model_name=model_name, image_data=image_data, **kwargs + ) + elif task_type == "background_removal": + model_name = model_name or "Background Remover" + return await self.hf_tool.background_removal( + model_name=model_name, image_data=image_data, **kwargs + ) + elif task_type == "face_restoration": + model_name = model_name or "GFPGAN" + return await self.hf_tool.super_resolution( + model_name=model_name, image_data=image_data, **kwargs + ) + + async def generate_creative_content_with_hf( + self, + prompt: str, + content_type: str = "story", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate creative content like stories, poems, etc.""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "GPT-3.5 Creative" + enhanced_prompt = f"Write a {content_type}: {prompt}" + return await self.hf_tool.creative_writing( + model_name=model_name, prompt=enhanced_prompt, **kwargs + ) + + async def generate_game_content_with_hf( + self, + description: str, + content_type: str = "character", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate game development content""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "Character AI" + enhanced_prompt = f"Create game {content_type}: {description}" + return await self.hf_tool.creative_writing( + model_name=model_name, prompt=enhanced_prompt, **kwargs + ) + + async def generate_business_document_with_hf( + self, + context: str, + document_type: str = "email", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Generate business documents and content""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "Email Assistant" + return await self.hf_tool.business_document( + model_name=model_name, + document_type=document_type, + context=context, + **kwargs, + ) + + async def research_assistance_with_hf( + self, + topic: str, + research_type: str = "analysis", + model_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Research assistance and scientific content generation""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + model_name = model_name or "SciBERT" + enhanced_prompt = f"Research {research_type} on: {topic}" + return await self.hf_tool.text_generation( + model_name=model_name, prompt=enhanced_prompt, **kwargs + ) + + def get_available_hf_models(self, category: Optional[str] = None) -> Dict[str, Any]: + """Get available models by category""" + if not self.hf_tool: + return {"error": "HuggingFace integration not available"} + + return self.hf_tool.list_available_models(category=category) diff --git a/app/agent/manus.py b/app/agent/manus.py new file mode 100644 index 0000000000000000000000000000000000000000..df40edbbaa6383182aea779d9145b16d054953e8 --- /dev/null +++ b/app/agent/manus.py @@ -0,0 +1,165 @@ +from typing import Dict, List, Optional + +from pydantic import Field, model_validator + +from app.agent.browser import BrowserContextHelper +from app.agent.toolcall import ToolCallAgent +from app.config import config +from app.logger import logger +from app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.tool import Terminate, ToolCollection +from app.tool.ask_human import AskHuman +from app.tool.browser_use_tool import BrowserUseTool +from app.tool.mcp import MCPClients, MCPClientTool +from app.tool.python_execute import PythonExecute +from app.tool.str_replace_editor import StrReplaceEditor + + +class Manus(ToolCallAgent): + """A versatile general-purpose agent with support for both local and MCP tools.""" + + name: str = "Manus" + description: str = "A versatile agent that can solve various tasks using multiple tools including MCP-based tools" + + system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root) + next_step_prompt: str = NEXT_STEP_PROMPT + + max_observe: int = 10000 + max_steps: int = 20 + + # MCP clients for remote tool access + mcp_clients: MCPClients = Field(default_factory=MCPClients) + + # Add general-purpose tools to the tool collection + available_tools: ToolCollection = Field( + default_factory=lambda: ToolCollection( + PythonExecute(), + BrowserUseTool(), + StrReplaceEditor(), + AskHuman(), + Terminate(), + ) + ) + + special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name]) + browser_context_helper: Optional[BrowserContextHelper] = None + + # Track connected MCP servers + connected_servers: Dict[str, str] = Field( + default_factory=dict + ) # server_id -> url/command + _initialized: bool = False + + @model_validator(mode="after") + def initialize_helper(self) -> "Manus": + """Initialize basic components synchronously.""" + self.browser_context_helper = BrowserContextHelper(self) + return self + + @classmethod + async def create(cls, **kwargs) -> "Manus": + """Factory method to create and properly initialize a Manus instance.""" + instance = cls(**kwargs) + await instance.initialize_mcp_servers() + instance._initialized = True + return instance + + async def initialize_mcp_servers(self) -> None: + """Initialize connections to configured MCP servers.""" + for server_id, server_config in config.mcp_config.servers.items(): + try: + if server_config.type == "sse": + if server_config.url: + await self.connect_mcp_server(server_config.url, server_id) + logger.info( + f"Connected to MCP server {server_id} at {server_config.url}" + ) + elif server_config.type == "stdio": + if server_config.command: + await self.connect_mcp_server( + server_config.command, + server_id, + use_stdio=True, + stdio_args=server_config.args, + ) + logger.info( + f"Connected to MCP server {server_id} using command {server_config.command}" + ) + except Exception as e: + logger.error(f"Failed to connect to MCP server {server_id}: {e}") + + async def connect_mcp_server( + self, + server_url: str, + server_id: str = "", + use_stdio: bool = False, + stdio_args: List[str] = None, + ) -> None: + """Connect to an MCP server and add its tools.""" + if use_stdio: + await self.mcp_clients.connect_stdio( + server_url, stdio_args or [], server_id + ) + self.connected_servers[server_id or server_url] = server_url + else: + await self.mcp_clients.connect_sse(server_url, server_id) + self.connected_servers[server_id or server_url] = server_url + + # Update available tools with only the new tools from this server + new_tools = [ + tool for tool in self.mcp_clients.tools if tool.server_id == server_id + ] + self.available_tools.add_tools(*new_tools) + + async def disconnect_mcp_server(self, server_id: str = "") -> None: + """Disconnect from an MCP server and remove its tools.""" + await self.mcp_clients.disconnect(server_id) + if server_id: + self.connected_servers.pop(server_id, None) + else: + self.connected_servers.clear() + + # Rebuild available tools without the disconnected server's tools + base_tools = [ + tool + for tool in self.available_tools.tools + if not isinstance(tool, MCPClientTool) + ] + self.available_tools = ToolCollection(*base_tools) + self.available_tools.add_tools(*self.mcp_clients.tools) + + async def cleanup(self): + """Clean up Manus agent resources.""" + if self.browser_context_helper: + await self.browser_context_helper.cleanup_browser() + # Disconnect from all MCP servers only if we were initialized + if self._initialized: + await self.disconnect_mcp_server() + self._initialized = False + + async def think(self) -> bool: + """Process current state and decide next actions with appropriate context.""" + if not self._initialized: + await self.initialize_mcp_servers() + self._initialized = True + + original_prompt = self.next_step_prompt + recent_messages = self.memory.messages[-3:] if self.memory.messages else [] + browser_in_use = any( + tc.function.name == BrowserUseTool().name + for msg in recent_messages + if msg.tool_calls + for tc in msg.tool_calls + ) + + if browser_in_use: + self.next_step_prompt = ( + await self.browser_context_helper.format_next_step_prompt() + ) + + result = await super().think() + + # Restore original prompt + self.next_step_prompt = original_prompt + + return result diff --git a/app/agent/mcp.py b/app/agent/mcp.py new file mode 100644 index 0000000000000000000000000000000000000000..9c6da6aaa3d62a4fdda34d50437cf7f6b8bc6466 --- /dev/null +++ b/app/agent/mcp.py @@ -0,0 +1,185 @@ +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import Field + +from app.agent.toolcall import ToolCallAgent +from app.logger import logger +from app.prompt.mcp import MULTIMEDIA_RESPONSE_PROMPT, NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.schema import AgentState, Message +from app.tool.base import ToolResult +from app.tool.mcp import MCPClients + + +class MCPAgent(ToolCallAgent): + """Agent for interacting with MCP (Model Context Protocol) servers. + + This agent connects to an MCP server using either SSE or stdio transport + and makes the server's tools available through the agent's tool interface. + """ + + name: str = "mcp_agent" + description: str = "An agent that connects to an MCP server and uses its tools." + + system_prompt: str = SYSTEM_PROMPT + next_step_prompt: str = NEXT_STEP_PROMPT + + # Initialize MCP tool collection + mcp_clients: MCPClients = Field(default_factory=MCPClients) + available_tools: MCPClients = None # Will be set in initialize() + + max_steps: int = 20 + connection_type: str = "stdio" # "stdio" or "sse" + + # Track tool schemas to detect changes + tool_schemas: Dict[str, Dict[str, Any]] = Field(default_factory=dict) + _refresh_tools_interval: int = 5 # Refresh tools every N steps + + # Special tool names that should trigger termination + special_tool_names: List[str] = Field(default_factory=lambda: ["terminate"]) + + async def initialize( + self, + connection_type: Optional[str] = None, + server_url: Optional[str] = None, + command: Optional[str] = None, + args: Optional[List[str]] = None, + ) -> None: + """Initialize the MCP connection. + + Args: + connection_type: Type of connection to use ("stdio" or "sse") + server_url: URL of the MCP server (for SSE connection) + command: Command to run (for stdio connection) + args: Arguments for the command (for stdio connection) + """ + if connection_type: + self.connection_type = connection_type + + # Connect to the MCP server based on connection type + if self.connection_type == "sse": + if not server_url: + raise ValueError("Server URL is required for SSE connection") + await self.mcp_clients.connect_sse(server_url=server_url) + elif self.connection_type == "stdio": + if not command: + raise ValueError("Command is required for stdio connection") + await self.mcp_clients.connect_stdio(command=command, args=args or []) + else: + raise ValueError(f"Unsupported connection type: {self.connection_type}") + + # Set available_tools to our MCP instance + self.available_tools = self.mcp_clients + + # Store initial tool schemas + await self._refresh_tools() + + # Add system message about available tools + tool_names = list(self.mcp_clients.tool_map.keys()) + tools_info = ", ".join(tool_names) + + # Add system prompt and available tools information + self.memory.add_message( + Message.system_message( + f"{self.system_prompt}\n\nAvailable MCP tools: {tools_info}" + ) + ) + + async def _refresh_tools(self) -> Tuple[List[str], List[str]]: + """Refresh the list of available tools from the MCP server. + + Returns: + A tuple of (added_tools, removed_tools) + """ + if not self.mcp_clients.sessions: + return [], [] + + # Get current tool schemas directly from the server + response = await self.mcp_clients.list_tools() + current_tools = {tool.name: tool.inputSchema for tool in response.tools} + + # Determine added, removed, and changed tools + current_names = set(current_tools.keys()) + previous_names = set(self.tool_schemas.keys()) + + added_tools = list(current_names - previous_names) + removed_tools = list(previous_names - current_names) + + # Check for schema changes in existing tools + changed_tools = [] + for name in current_names.intersection(previous_names): + if current_tools[name] != self.tool_schemas.get(name): + changed_tools.append(name) + + # Update stored schemas + self.tool_schemas = current_tools + + # Log and notify about changes + if added_tools: + logger.info(f"Added MCP tools: {added_tools}") + self.memory.add_message( + Message.system_message(f"New tools available: {', '.join(added_tools)}") + ) + if removed_tools: + logger.info(f"Removed MCP tools: {removed_tools}") + self.memory.add_message( + Message.system_message( + f"Tools no longer available: {', '.join(removed_tools)}" + ) + ) + if changed_tools: + logger.info(f"Changed MCP tools: {changed_tools}") + + return added_tools, removed_tools + + async def think(self) -> bool: + """Process current state and decide next action.""" + # Check MCP session and tools availability + if not self.mcp_clients.sessions or not self.mcp_clients.tool_map: + logger.info("MCP service is no longer available, ending interaction") + self.state = AgentState.FINISHED + return False + + # Refresh tools periodically + if self.current_step % self._refresh_tools_interval == 0: + await self._refresh_tools() + # All tools removed indicates shutdown + if not self.mcp_clients.tool_map: + logger.info("MCP service has shut down, ending interaction") + self.state = AgentState.FINISHED + return False + + # Use the parent class's think method + return await super().think() + + async def _handle_special_tool(self, name: str, result: Any, **kwargs) -> None: + """Handle special tool execution and state changes""" + # First process with parent handler + await super()._handle_special_tool(name, result, **kwargs) + + # Handle multimedia responses + if isinstance(result, ToolResult) and result.base64_image: + self.memory.add_message( + Message.system_message( + MULTIMEDIA_RESPONSE_PROMPT.format(tool_name=name) + ) + ) + + def _should_finish_execution(self, name: str, **kwargs) -> bool: + """Determine if tool execution should finish the agent""" + # Terminate if the tool name is 'terminate' + return name.lower() == "terminate" + + async def cleanup(self) -> None: + """Clean up MCP connection when done.""" + if self.mcp_clients.sessions: + await self.mcp_clients.disconnect() + logger.info("MCP connection closed") + + async def run(self, request: Optional[str] = None) -> str: + """Run the agent with cleanup when done.""" + try: + result = await super().run(request) + return result + finally: + # Ensure cleanup happens even if there's an error + await self.cleanup() diff --git a/app/agent/react.py b/app/agent/react.py new file mode 100644 index 0000000000000000000000000000000000000000..7f9482082052a95977c9ddf4e14757df0de77dc4 --- /dev/null +++ b/app/agent/react.py @@ -0,0 +1,38 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from pydantic import Field + +from app.agent.base import BaseAgent +from app.llm import LLM +from app.schema import AgentState, Memory + + +class ReActAgent(BaseAgent, ABC): + name: str + description: Optional[str] = None + + system_prompt: Optional[str] = None + next_step_prompt: Optional[str] = None + + llm: Optional[LLM] = Field(default_factory=LLM) + memory: Memory = Field(default_factory=Memory) + state: AgentState = AgentState.IDLE + + max_steps: int = 10 + current_step: int = 0 + + @abstractmethod + async def think(self) -> bool: + """Process current state and decide next action""" + + @abstractmethod + async def act(self) -> str: + """Execute decided actions""" + + async def step(self) -> str: + """Execute a single step: think and act.""" + should_act = await self.think() + if not should_act: + return "Thinking complete - no action needed" + return await self.act() diff --git a/app/agent/sandbox_agent.py b/app/agent/sandbox_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..58612d20f9c7a7302bd06b8e0b75d2fc80c84d92 --- /dev/null +++ b/app/agent/sandbox_agent.py @@ -0,0 +1,223 @@ +from typing import Dict, List, Optional + +from pydantic import Field, model_validator + +from app.agent.browser import BrowserContextHelper +from app.agent.toolcall import ToolCallAgent +from app.config import config +from app.daytona.sandbox import create_sandbox, delete_sandbox +from app.daytona.tool_base import SandboxToolsBase +from app.logger import logger +from app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.tool import Terminate, ToolCollection +from app.tool.ask_human import AskHuman +from app.tool.mcp import MCPClients, MCPClientTool +from app.tool.sandbox.sb_browser_tool import SandboxBrowserTool +from app.tool.sandbox.sb_files_tool import SandboxFilesTool +from app.tool.sandbox.sb_shell_tool import SandboxShellTool +from app.tool.sandbox.sb_vision_tool import SandboxVisionTool + + +class SandboxManus(ToolCallAgent): + """A versatile general-purpose agent with support for both local and MCP tools.""" + + name: str = "SandboxManus" + description: str = "A versatile agent that can solve various tasks using multiple sandbox-tools including MCP-based tools" + + system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root) + next_step_prompt: str = NEXT_STEP_PROMPT + + max_observe: int = 10000 + max_steps: int = 20 + + # MCP clients for remote tool access + mcp_clients: MCPClients = Field(default_factory=MCPClients) + + # Add general-purpose tools to the tool collection + available_tools: ToolCollection = Field( + default_factory=lambda: ToolCollection( + # PythonExecute(), + # BrowserUseTool(), + # StrReplaceEditor(), + AskHuman(), + Terminate(), + ) + ) + + special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name]) + browser_context_helper: Optional[BrowserContextHelper] = None + + # Track connected MCP servers + connected_servers: Dict[str, str] = Field( + default_factory=dict + ) # server_id -> url/command + _initialized: bool = False + sandbox_link: Optional[dict[str, dict[str, str]]] = Field(default_factory=dict) + + @model_validator(mode="after") + def initialize_helper(self) -> "SandboxManus": + """Initialize basic components synchronously.""" + self.browser_context_helper = BrowserContextHelper(self) + return self + + @classmethod + async def create(cls, **kwargs) -> "SandboxManus": + """Factory method to create and properly initialize a Manus instance.""" + instance = cls(**kwargs) + await instance.initialize_mcp_servers() + await instance.initialize_sandbox_tools() + instance._initialized = True + return instance + + async def initialize_sandbox_tools( + self, + password: str = config.daytona.VNC_password, + ) -> None: + try: + # 创建新沙箱 + if password: + sandbox = create_sandbox(password=password) + self.sandbox = sandbox + else: + raise ValueError("password must be provided") + vnc_link = sandbox.get_preview_link(6080) + website_link = sandbox.get_preview_link(8080) + vnc_url = vnc_link.url if hasattr(vnc_link, "url") else str(vnc_link) + website_url = ( + website_link.url if hasattr(website_link, "url") else str(website_link) + ) + + # Get the actual sandbox_id from the created sandbox + actual_sandbox_id = sandbox.id if hasattr(sandbox, "id") else "new_sandbox" + if not self.sandbox_link: + self.sandbox_link = {} + self.sandbox_link[actual_sandbox_id] = { + "vnc": vnc_url, + "website": website_url, + } + logger.info(f"VNC URL: {vnc_url}") + logger.info(f"Website URL: {website_url}") + SandboxToolsBase._urls_printed = True + sb_tools = [ + SandboxBrowserTool(sandbox), + SandboxFilesTool(sandbox), + SandboxShellTool(sandbox), + SandboxVisionTool(sandbox), + ] + self.available_tools.add_tools(*sb_tools) + + except Exception as e: + logger.error(f"Error initializing sandbox tools: {e}") + raise + + async def initialize_mcp_servers(self) -> None: + """Initialize connections to configured MCP servers.""" + for server_id, server_config in config.mcp_config.servers.items(): + try: + if server_config.type == "sse": + if server_config.url: + await self.connect_mcp_server(server_config.url, server_id) + logger.info( + f"Connected to MCP server {server_id} at {server_config.url}" + ) + elif server_config.type == "stdio": + if server_config.command: + await self.connect_mcp_server( + server_config.command, + server_id, + use_stdio=True, + stdio_args=server_config.args, + ) + logger.info( + f"Connected to MCP server {server_id} using command {server_config.command}" + ) + except Exception as e: + logger.error(f"Failed to connect to MCP server {server_id}: {e}") + + async def connect_mcp_server( + self, + server_url: str, + server_id: str = "", + use_stdio: bool = False, + stdio_args: List[str] = None, + ) -> None: + """Connect to an MCP server and add its tools.""" + if use_stdio: + await self.mcp_clients.connect_stdio( + server_url, stdio_args or [], server_id + ) + self.connected_servers[server_id or server_url] = server_url + else: + await self.mcp_clients.connect_sse(server_url, server_id) + self.connected_servers[server_id or server_url] = server_url + + # Update available tools with only the new tools from this server + new_tools = [ + tool for tool in self.mcp_clients.tools if tool.server_id == server_id + ] + self.available_tools.add_tools(*new_tools) + + async def disconnect_mcp_server(self, server_id: str = "") -> None: + """Disconnect from an MCP server and remove its tools.""" + await self.mcp_clients.disconnect(server_id) + if server_id: + self.connected_servers.pop(server_id, None) + else: + self.connected_servers.clear() + + # Rebuild available tools without the disconnected server's tools + base_tools = [ + tool + for tool in self.available_tools.tools + if not isinstance(tool, MCPClientTool) + ] + self.available_tools = ToolCollection(*base_tools) + self.available_tools.add_tools(*self.mcp_clients.tools) + + async def delete_sandbox(self, sandbox_id: str) -> None: + """Delete a sandbox by ID.""" + try: + await delete_sandbox(sandbox_id) + logger.info(f"Sandbox {sandbox_id} deleted successfully") + if sandbox_id in self.sandbox_link: + del self.sandbox_link[sandbox_id] + except Exception as e: + logger.error(f"Error deleting sandbox {sandbox_id}: {e}") + raise e + + async def cleanup(self): + """Clean up Manus agent resources.""" + if self.browser_context_helper: + await self.browser_context_helper.cleanup_browser() + # Disconnect from all MCP servers only if we were initialized + if self._initialized: + await self.disconnect_mcp_server() + await self.delete_sandbox(self.sandbox.id if self.sandbox else "unknown") + self._initialized = False + + async def think(self) -> bool: + """Process current state and decide next actions with appropriate context.""" + if not self._initialized: + await self.initialize_mcp_servers() + self._initialized = True + + original_prompt = self.next_step_prompt + recent_messages = self.memory.messages[-3:] if self.memory.messages else [] + browser_in_use = any( + tc.function.name == SandboxBrowserTool().name + for msg in recent_messages + if msg.tool_calls + for tc in msg.tool_calls + ) + + if browser_in_use: + self.next_step_prompt = ( + await self.browser_context_helper.format_next_step_prompt() + ) + + result = await super().think() + + # Restore original prompt + self.next_step_prompt = original_prompt + + return result diff --git a/app/agent/swe.py b/app/agent/swe.py new file mode 100644 index 0000000000000000000000000000000000000000..e655a5b73c5ad53b2c0b193c47d5d5ba1f3bf045 --- /dev/null +++ b/app/agent/swe.py @@ -0,0 +1,24 @@ +from typing import List + +from pydantic import Field + +from app.agent.toolcall import ToolCallAgent +from app.prompt.swe import SYSTEM_PROMPT +from app.tool import Bash, StrReplaceEditor, Terminate, ToolCollection + + +class SWEAgent(ToolCallAgent): + """An agent that implements the SWEAgent paradigm for executing code and natural conversations.""" + + name: str = "swe" + description: str = "an autonomous AI programmer that interacts directly with the computer to solve tasks." + + system_prompt: str = SYSTEM_PROMPT + next_step_prompt: str = "" + + available_tools: ToolCollection = ToolCollection( + Bash(), StrReplaceEditor(), Terminate() + ) + special_tool_names: List[str] = Field(default_factory=lambda: [Terminate().name]) + + max_steps: int = 20 diff --git a/app/agent/toolcall.py b/app/agent/toolcall.py new file mode 100644 index 0000000000000000000000000000000000000000..65f31d988945500bcbe5442d34b1d8644ead0cf0 --- /dev/null +++ b/app/agent/toolcall.py @@ -0,0 +1,250 @@ +import asyncio +import json +from typing import Any, List, Optional, Union + +from pydantic import Field + +from app.agent.react import ReActAgent +from app.exceptions import TokenLimitExceeded +from app.logger import logger +from app.prompt.toolcall import NEXT_STEP_PROMPT, SYSTEM_PROMPT +from app.schema import TOOL_CHOICE_TYPE, AgentState, Message, ToolCall, ToolChoice +from app.tool import CreateChatCompletion, Terminate, ToolCollection + + +TOOL_CALL_REQUIRED = "Tool calls required but none provided" + + +class ToolCallAgent(ReActAgent): + """Base agent class for handling tool/function calls with enhanced abstraction""" + + name: str = "toolcall" + description: str = "an agent that can execute tool calls." + + system_prompt: str = SYSTEM_PROMPT + next_step_prompt: str = NEXT_STEP_PROMPT + + available_tools: ToolCollection = ToolCollection( + CreateChatCompletion(), Terminate() + ) + tool_choices: TOOL_CHOICE_TYPE = ToolChoice.AUTO # type: ignore + special_tool_names: List[str] = Field(default_factory=lambda: [Terminate().name]) + + tool_calls: List[ToolCall] = Field(default_factory=list) + _current_base64_image: Optional[str] = None + + max_steps: int = 30 + max_observe: Optional[Union[int, bool]] = None + + async def think(self) -> bool: + """Process current state and decide next actions using tools""" + if self.next_step_prompt: + user_msg = Message.user_message(self.next_step_prompt) + self.messages += [user_msg] + + try: + # Get response with tool options + response = await self.llm.ask_tool( + messages=self.messages, + system_msgs=( + [Message.system_message(self.system_prompt)] + if self.system_prompt + else None + ), + tools=self.available_tools.to_params(), + tool_choice=self.tool_choices, + ) + except ValueError: + raise + except Exception as e: + # Check if this is a RetryError containing TokenLimitExceeded + if hasattr(e, "__cause__") and isinstance(e.__cause__, TokenLimitExceeded): + token_limit_error = e.__cause__ + logger.error( + f"🚨 Token limit error (from RetryError): {token_limit_error}" + ) + self.memory.add_message( + Message.assistant_message( + f"Maximum token limit reached, cannot continue execution: {str(token_limit_error)}" + ) + ) + self.state = AgentState.FINISHED + return False + raise + + self.tool_calls = tool_calls = ( + response.tool_calls if response and response.tool_calls else [] + ) + content = response.content if response and response.content else "" + + # Log response info + logger.info(f"✨ {self.name}'s thoughts: {content}") + logger.info( + f"🛠️ {self.name} selected {len(tool_calls) if tool_calls else 0} tools to use" + ) + if tool_calls: + logger.info( + f"🧰 Tools being prepared: {[call.function.name for call in tool_calls]}" + ) + logger.info(f"🔧 Tool arguments: {tool_calls[0].function.arguments}") + + try: + if response is None: + raise RuntimeError("No response received from the LLM") + + # Handle different tool_choices modes + if self.tool_choices == ToolChoice.NONE: + if tool_calls: + logger.warning( + f"🤔 Hmm, {self.name} tried to use tools when they weren't available!" + ) + if content: + self.memory.add_message(Message.assistant_message(content)) + return True + return False + + # Create and add assistant message + assistant_msg = ( + Message.from_tool_calls(content=content, tool_calls=self.tool_calls) + if self.tool_calls + else Message.assistant_message(content) + ) + self.memory.add_message(assistant_msg) + + if self.tool_choices == ToolChoice.REQUIRED and not self.tool_calls: + return True # Will be handled in act() + + # For 'auto' mode, continue with content if no commands but content exists + if self.tool_choices == ToolChoice.AUTO and not self.tool_calls: + return bool(content) + + return bool(self.tool_calls) + except Exception as e: + logger.error(f"🚨 Oops! The {self.name}'s thinking process hit a snag: {e}") + self.memory.add_message( + Message.assistant_message( + f"Error encountered while processing: {str(e)}" + ) + ) + return False + + async def act(self) -> str: + """Execute tool calls and handle their results""" + if not self.tool_calls: + if self.tool_choices == ToolChoice.REQUIRED: + raise ValueError(TOOL_CALL_REQUIRED) + + # Return last message content if no tool calls + return self.messages[-1].content or "No content or commands to execute" + + results = [] + for command in self.tool_calls: + # Reset base64_image for each tool call + self._current_base64_image = None + + result = await self.execute_tool(command) + + if self.max_observe: + result = result[: self.max_observe] + + logger.info( + f"🎯 Tool '{command.function.name}' completed its mission! Result: {result}" + ) + + # Add tool response to memory + tool_msg = Message.tool_message( + content=result, + tool_call_id=command.id, + name=command.function.name, + base64_image=self._current_base64_image, + ) + self.memory.add_message(tool_msg) + results.append(result) + + return "\n\n".join(results) + + async def execute_tool(self, command: ToolCall) -> str: + """Execute a single tool call with robust error handling""" + if not command or not command.function or not command.function.name: + return "Error: Invalid command format" + + name = command.function.name + if name not in self.available_tools.tool_map: + return f"Error: Unknown tool '{name}'" + + try: + # Parse arguments + args = json.loads(command.function.arguments or "{}") + + # Execute the tool + logger.info(f"🔧 Activating tool: '{name}'...") + result = await self.available_tools.execute(name=name, tool_input=args) + + # Handle special tools + await self._handle_special_tool(name=name, result=result) + + # Check if result is a ToolResult with base64_image + if hasattr(result, "base64_image") and result.base64_image: + # Store the base64_image for later use in tool_message + self._current_base64_image = result.base64_image + + # Format result for display (standard case) + observation = ( + f"Observed output of cmd `{name}` executed:\n{str(result)}" + if result + else f"Cmd `{name}` completed with no output" + ) + + return observation + except json.JSONDecodeError: + error_msg = f"Error parsing arguments for {name}: Invalid JSON format" + logger.error( + f"📝 Oops! The arguments for '{name}' don't make sense - invalid JSON, arguments:{command.function.arguments}" + ) + return f"Error: {error_msg}" + except Exception as e: + error_msg = f"⚠️ Tool '{name}' encountered a problem: {str(e)}" + logger.exception(error_msg) + return f"Error: {error_msg}" + + async def _handle_special_tool(self, name: str, result: Any, **kwargs): + """Handle special tool execution and state changes""" + if not self._is_special_tool(name): + return + + if self._should_finish_execution(name=name, result=result, **kwargs): + # Set agent state to finished + logger.info(f"🏁 Special tool '{name}' has completed the task!") + self.state = AgentState.FINISHED + + @staticmethod + def _should_finish_execution(**kwargs) -> bool: + """Determine if tool execution should finish the agent""" + return True + + def _is_special_tool(self, name: str) -> bool: + """Check if tool name is in special tools list""" + return name.lower() in [n.lower() for n in self.special_tool_names] + + async def cleanup(self): + """Clean up resources used by the agent's tools.""" + logger.info(f"🧹 Cleaning up resources for agent '{self.name}'...") + for tool_name, tool_instance in self.available_tools.tool_map.items(): + if hasattr(tool_instance, "cleanup") and asyncio.iscoroutinefunction( + tool_instance.cleanup + ): + try: + logger.debug(f"🧼 Cleaning up tool: {tool_name}") + await tool_instance.cleanup() + except Exception as e: + logger.error( + f"🚨 Error cleaning up tool '{tool_name}': {e}", exc_info=True + ) + logger.info(f"✨ Cleanup complete for agent '{self.name}'.") + + async def run(self, request: Optional[str] = None) -> str: + """Run the agent with cleanup when done.""" + try: + return await super().run(request) + finally: + await self.cleanup() diff --git a/app/auth.py b/app/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..0ab4e0c01518f6ada3c1666d49a648303df862a6 --- /dev/null +++ b/app/auth.py @@ -0,0 +1,205 @@ +""" +User authentication models and validation for OpenManus +Mobile number + password based authentication system +""" + +import hashlib +import re +import secrets +from datetime import datetime, timedelta +from typing import Optional +from dataclasses import dataclass +from pydantic import BaseModel, validator + + +class UserSignupRequest(BaseModel): + """User signup request model""" + + full_name: str + mobile_number: str + password: str + confirm_password: str + + @validator("full_name") + def validate_full_name(cls, v): + if not v or len(v.strip()) < 2: + raise ValueError("Full name must be at least 2 characters long") + if len(v.strip()) > 100: + raise ValueError("Full name must be less than 100 characters") + return v.strip() + + @validator("mobile_number") + def validate_mobile_number(cls, v): + # Remove all non-digit characters + digits_only = re.sub(r"\D", "", v) + + # Check if it's a valid mobile number (10-15 digits) + if len(digits_only) < 10 or len(digits_only) > 15: + raise ValueError("Mobile number must be between 10-15 digits") + + # Ensure it starts with country code or local format + if not re.match(r"^(\+?[1-9]\d{9,14})$", digits_only): + raise ValueError("Invalid mobile number format") + + return digits_only + + @validator("password") + def validate_password(cls, v): + if len(v) < 8: + raise ValueError("Password must be at least 8 characters long") + if len(v) > 128: + raise ValueError("Password must be less than 128 characters") + + # Check for at least one uppercase, lowercase, and digit + if not re.search(r"[A-Z]", v): + raise ValueError("Password must contain at least one uppercase letter") + if not re.search(r"[a-z]", v): + raise ValueError("Password must contain at least one lowercase letter") + if not re.search(r"\d", v): + raise ValueError("Password must contain at least one digit") + + return v + + @validator("confirm_password") + def validate_confirm_password(cls, v, values): + if "password" in values and v != values["password"]: + raise ValueError("Passwords do not match") + return v + + +class UserLoginRequest(BaseModel): + """User login request model""" + + mobile_number: str + password: str + + @validator("mobile_number") + def validate_mobile_number(cls, v): + # Remove all non-digit characters + digits_only = re.sub(r"\D", "", v) + + if len(digits_only) < 10 or len(digits_only) > 15: + raise ValueError("Invalid mobile number") + + return digits_only + + +@dataclass +class User: + """User model""" + + id: str + mobile_number: str + full_name: str + password_hash: str + avatar_url: Optional[str] = None + preferences: Optional[str] = None + is_active: bool = True + created_at: Optional[datetime] = None + updated_at: Optional[datetime] = None + + +@dataclass +class UserSession: + """User session model""" + + session_id: str + user_id: str + mobile_number: str + full_name: str + created_at: datetime + expires_at: datetime + + @property + def is_valid(self) -> bool: + """Check if session is still valid""" + return datetime.utcnow() < self.expires_at + + +class UserAuth: + """User authentication utilities""" + + @staticmethod + def hash_password(password: str) -> str: + """Hash password using SHA-256 with salt""" + salt = secrets.token_hex(32) + password_hash = hashlib.sha256((password + salt).encode()).hexdigest() + return f"{salt}:{password_hash}" + + @staticmethod + def verify_password(password: str, password_hash: str) -> bool: + """Verify password against stored hash""" + try: + salt, stored_hash = password_hash.split(":") + password_hash_check = hashlib.sha256((password + salt).encode()).hexdigest() + return password_hash_check == stored_hash + except ValueError: + return False + + @staticmethod + def generate_session_id() -> str: + """Generate secure session ID""" + return secrets.token_urlsafe(32) + + @staticmethod + def generate_user_id() -> str: + """Generate unique user ID""" + return f"user_{secrets.token_hex(16)}" + + @staticmethod + def format_mobile_number(mobile_number: str) -> str: + """Format mobile number for consistent storage""" + # Remove all non-digit characters + digits_only = re.sub(r"\D", "", mobile_number) + + # Add + prefix if not present and format consistently + if not digits_only.startswith("+"): + # Assume it's a local number, add default country code if needed + if len(digits_only) == 10: # US format + digits_only = f"1{digits_only}" + + return f"+{digits_only}" + + @staticmethod + def create_session(user: User, duration_hours: int = 24) -> UserSession: + """Create a new user session""" + session_id = UserAuth.generate_session_id() + created_at = datetime.utcnow() + expires_at = created_at + timedelta(hours=duration_hours) + + return UserSession( + session_id=session_id, + user_id=user.id, + mobile_number=user.mobile_number, + full_name=user.full_name, + created_at=created_at, + expires_at=expires_at, + ) + + +# Response models +class AuthResponse(BaseModel): + """Authentication response model""" + + success: bool + message: str + session_id: Optional[str] = None + user_id: Optional[str] = None + full_name: Optional[str] = None + + +class UserProfile(BaseModel): + """User profile response model""" + + user_id: str + full_name: str + mobile_number: str # Masked for security + avatar_url: Optional[str] = None + created_at: Optional[str] = None + + @staticmethod + def mask_mobile_number(mobile_number: str) -> str: + """Mask mobile number for security (show only last 4 digits)""" + if len(mobile_number) <= 4: + return "*" * len(mobile_number) + return "*" * (len(mobile_number) - 4) + mobile_number[-4:] diff --git a/app/auth_interface.py b/app/auth_interface.py new file mode 100644 index 0000000000000000000000000000000000000000..cce8842b371fccc7c7639efda2976c8ec727c12e --- /dev/null +++ b/app/auth_interface.py @@ -0,0 +1,361 @@ +""" +Authentication Web Interface for OpenManus +Mobile number + password based authentication forms +""" + +import asyncio +import sqlite3 +from typing import Optional, Tuple + +import gradio as gr + +from app.auth import UserSignupRequest, UserLoginRequest +from app.auth_service import AuthService +from app.logger import logger + + +class AuthInterface: + """Authentication interface with Gradio""" + + def __init__(self, db_path: str = "openmanus.db"): + self.db_path = db_path + self.auth_service = None + self.current_session = None + self.init_database() + + def init_database(self): + """Initialize database with schema""" + try: + conn = sqlite3.connect(self.db_path) + + # Create users table with mobile auth + conn.execute( + """ + CREATE TABLE IF NOT EXISTS users ( + id TEXT PRIMARY KEY, + mobile_number TEXT UNIQUE NOT NULL, + full_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + avatar_url TEXT, + preferences TEXT, + is_active BOOLEAN DEFAULT TRUE, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + """ + ) + + # Create sessions table + conn.execute( + """ + CREATE TABLE IF NOT EXISTS sessions ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + title TEXT, + metadata TEXT, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + expires_at DATETIME, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + ) + """ + ) + + conn.commit() + conn.close() + logger.info("Database initialized successfully") + + except Exception as e: + logger.error(f"Database initialization error: {str(e)}") + + def get_db_connection(self): + """Get database connection""" + return sqlite3.connect(self.db_path) + + async def handle_signup( + self, full_name: str, mobile_number: str, password: str, confirm_password: str + ) -> Tuple[str, bool, dict]: + """Handle user signup""" + try: + # Validate input + if not all([full_name, mobile_number, password, confirm_password]): + return "All fields are required", False, gr.update(visible=True) + + # Create signup request + signup_data = UserSignupRequest( + full_name=full_name, + mobile_number=mobile_number, + password=password, + confirm_password=confirm_password, + ) + + # Process signup + db_conn = self.get_db_connection() + auth_service = AuthService(db_conn) + + result = await auth_service.register_user(signup_data) + db_conn.close() + + if result.success: + self.current_session = { + "session_id": result.session_id, + "user_id": result.user_id, + "full_name": result.full_name, + } + return ( + f"Welcome {result.full_name}! Account created successfully.", + True, + gr.update(visible=False), + ) + else: + return result.message, False, gr.update(visible=True) + + except ValueError as e: + return str(e), False, gr.update(visible=True) + except Exception as e: + logger.error(f"Signup error: {str(e)}") + return "An error occurred during signup", False, gr.update(visible=True) + + async def handle_login( + self, mobile_number: str, password: str + ) -> Tuple[str, bool, dict]: + """Handle user login""" + try: + # Validate input + if not all([mobile_number, password]): + return ( + "Mobile number and password are required", + False, + gr.update(visible=True), + ) + + # Create login request + login_data = UserLoginRequest( + mobile_number=mobile_number, password=password + ) + + # Process login + db_conn = self.get_db_connection() + auth_service = AuthService(db_conn) + + result = await auth_service.login_user(login_data) + db_conn.close() + + if result.success: + self.current_session = { + "session_id": result.session_id, + "user_id": result.user_id, + "full_name": result.full_name, + } + return ( + f"Welcome back, {result.full_name}!", + True, + gr.update(visible=False), + ) + else: + return result.message, False, gr.update(visible=True) + + except ValueError as e: + return str(e), False, gr.update(visible=True) + except Exception as e: + logger.error(f"Login error: {str(e)}") + return "An error occurred during login", False, gr.update(visible=True) + + def handle_logout(self) -> Tuple[str, bool, dict]: + """Handle user logout""" + if self.current_session: + # In a real app, you'd delete the session from database + self.current_session = None + + return "Logged out successfully", False, gr.update(visible=True) + + def create_interface(self) -> gr.Interface: + """Create the authentication interface""" + + with gr.Blocks( + title="OpenManus Authentication", theme=gr.themes.Soft() + ) as auth_interface: + gr.Markdown( + """ + # 🔐 OpenManus Authentication + ### Secure Mobile Number + Password Login System + """ + ) + + # Session status + session_status = gr.Textbox( + value="Not logged in", label="Status", interactive=False + ) + + # Auth forms container + with gr.Column(visible=True) as auth_forms: + + with gr.Tabs(): + + # Login Tab + with gr.TabItem("🔑 Login"): + gr.Markdown("### Login with your mobile number and password") + + login_mobile = gr.Textbox( + label="📱 Mobile Number", + placeholder="Enter your mobile number (e.g., +1234567890)", + lines=1, + ) + + login_password = gr.Textbox( + label="🔒 Password", + type="password", + placeholder="Enter your password", + lines=1, + ) + + login_btn = gr.Button("🔑 Login", variant="primary", size="lg") + login_result = gr.Textbox(label="Result", interactive=False) + + # Signup Tab + with gr.TabItem("📝 Sign Up"): + gr.Markdown("### Create your new account") + + signup_fullname = gr.Textbox( + label="👤 Full Name", + placeholder="Enter your full name", + lines=1, + ) + + signup_mobile = gr.Textbox( + label="📱 Mobile Number", + placeholder="Enter your mobile number (e.g., +1234567890)", + lines=1, + ) + + signup_password = gr.Textbox( + label="🔒 Password", + type="password", + placeholder="Create a strong password (min 8 chars, include uppercase, lowercase, digit)", + lines=1, + ) + + signup_confirm_password = gr.Textbox( + label="🔒 Confirm Password", + type="password", + placeholder="Confirm your password", + lines=1, + ) + + signup_btn = gr.Button( + "📝 Create Account", variant="primary", size="lg" + ) + signup_result = gr.Textbox(label="Result", interactive=False) + + # Logged in section + with gr.Column(visible=False) as logged_in_section: + gr.Markdown("### ✅ You are logged in!") + + user_info = gr.Markdown("Welcome!") + + logout_btn = gr.Button("🚪 Logout", variant="secondary") + logout_result = gr.Textbox(label="Result", interactive=False) + + # Password requirements info + with gr.Accordion("📋 Password Requirements", open=False): + gr.Markdown( + """ + **Password must contain:** + - At least 8 characters + - At least 1 uppercase letter (A-Z) + - At least 1 lowercase letter (a-z) + - At least 1 digit (0-9) + - Maximum 128 characters + + **Mobile Number Format:** + - 10-15 digits + - Can include country code + - Examples: +1234567890, 1234567890, +91987654321 + """ + ) + + # Event handlers + def sync_signup(*args): + """Synchronous wrapper for signup""" + return asyncio.run(self.handle_signup(*args)) + + def sync_login(*args): + """Synchronous wrapper for login""" + return asyncio.run(self.handle_login(*args)) + + def update_ui_after_auth(result_text, success, auth_forms_update): + """Update UI after authentication""" + if success: + return ( + result_text, # session_status + auth_forms_update, # auth_forms visibility + gr.update(visible=True), # logged_in_section visibility + f"### 👋 {self.current_session['full_name'] if self.current_session else 'User'}", # user_info + ) + else: + return ( + "Not logged in", # session_status + auth_forms_update, # auth_forms visibility + gr.update(visible=False), # logged_in_section visibility + "Welcome!", # user_info + ) + + def update_ui_after_logout(result_text, success, auth_forms_update): + """Update UI after logout""" + return ( + "Not logged in", # session_status + auth_forms_update, # auth_forms visibility + gr.update(visible=False), # logged_in_section visibility + "Welcome!", # user_info + ) + + # Login button click + login_btn.click( + fn=sync_login, + inputs=[login_mobile, login_password], + outputs=[login_result, gr.State(), gr.State()], + ).then( + fn=update_ui_after_auth, + inputs=[login_result, gr.State(), gr.State()], + outputs=[session_status, auth_forms, logged_in_section, user_info], + ) + + # Signup button click + signup_btn.click( + fn=sync_signup, + inputs=[ + signup_fullname, + signup_mobile, + signup_password, + signup_confirm_password, + ], + outputs=[signup_result, gr.State(), gr.State()], + ).then( + fn=update_ui_after_auth, + inputs=[signup_result, gr.State(), gr.State()], + outputs=[session_status, auth_forms, logged_in_section, user_info], + ) + + # Logout button click + logout_btn.click( + fn=self.handle_logout, outputs=[logout_result, gr.State(), gr.State()] + ).then( + fn=update_ui_after_logout, + inputs=[logout_result, gr.State(), gr.State()], + outputs=[session_status, auth_forms, logged_in_section, user_info], + ) + + return auth_interface + + +# Standalone authentication app +def create_auth_app(db_path: str = "openmanus.db") -> gr.Interface: + """Create standalone authentication app""" + auth_interface = AuthInterface(db_path) + return auth_interface.create_interface() + + +if __name__ == "__main__": + # Run standalone auth interface for testing + auth_app = create_auth_app() + auth_app.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True) diff --git a/app/auth_service.py b/app/auth_service.py new file mode 100644 index 0000000000000000000000000000000000000000..84b1ec1dd993ed3067d7e0fe95daff120c30d2db --- /dev/null +++ b/app/auth_service.py @@ -0,0 +1,357 @@ +""" +User authentication service for OpenManus +Handles user registration, login, and session management with D1 database +""" + +import json +import sqlite3 +from datetime import datetime +from typing import Optional, Tuple + +from app.auth import ( + User, + UserAuth, + UserSession, + UserSignupRequest, + UserLoginRequest, + AuthResponse, + UserProfile, +) +from app.logger import logger + + +class AuthService: + """Authentication service for user management""" + + def __init__(self, db_connection=None): + """Initialize auth service with database connection""" + self.db = db_connection + self.logger = logger + + async def register_user(self, signup_data: UserSignupRequest) -> AuthResponse: + """Register a new user""" + try: + # Format mobile number consistently + formatted_mobile = UserAuth.format_mobile_number(signup_data.mobile_number) + + # Check if user already exists + existing_user = await self.get_user_by_mobile(formatted_mobile) + if existing_user: + return AuthResponse( + success=False, message="User with this mobile number already exists" + ) + + # Create new user + user_id = UserAuth.generate_user_id() + password_hash = UserAuth.hash_password(signup_data.password) + + user = User( + id=user_id, + mobile_number=formatted_mobile, + full_name=signup_data.full_name, + password_hash=password_hash, + created_at=datetime.utcnow(), + updated_at=datetime.utcnow(), + ) + + # Save user to database + success = await self.save_user(user) + if not success: + return AuthResponse( + success=False, message="Failed to create user account" + ) + + # Create session + session = UserAuth.create_session(user) + session_saved = await self.save_session(session) + + if not session_saved: + return AuthResponse( + success=False, message="User created but failed to create session" + ) + + self.logger.info(f"New user registered: {formatted_mobile}") + + return AuthResponse( + success=True, + message="Account created successfully", + session_id=session.session_id, + user_id=user.id, + full_name=user.full_name, + ) + + except Exception as e: + self.logger.error(f"User registration error: {str(e)}") + return AuthResponse( + success=False, message="An error occurred during registration" + ) + + async def login_user(self, login_data: UserLoginRequest) -> AuthResponse: + """Authenticate user login""" + try: + # Format mobile number consistently + formatted_mobile = UserAuth.format_mobile_number(login_data.mobile_number) + + # Get user from database + user = await self.get_user_by_mobile(formatted_mobile) + if not user: + return AuthResponse( + success=False, message="Invalid mobile number or password" + ) + + # Verify password + if not UserAuth.verify_password(login_data.password, user.password_hash): + return AuthResponse( + success=False, message="Invalid mobile number or password" + ) + + # Check if user is active + if not user.is_active: + return AuthResponse( + success=False, + message="Account is deactivated. Please contact support.", + ) + + # Create new session + session = UserAuth.create_session(user) + session_saved = await self.save_session(session) + + if not session_saved: + return AuthResponse( + success=False, + message="Login successful but failed to create session", + ) + + self.logger.info(f"User logged in: {formatted_mobile}") + + return AuthResponse( + success=True, + message="Login successful", + session_id=session.session_id, + user_id=user.id, + full_name=user.full_name, + ) + + except Exception as e: + self.logger.error(f"User login error: {str(e)}") + return AuthResponse(success=False, message="An error occurred during login") + + async def validate_session(self, session_id: str) -> Optional[UserSession]: + """Validate user session""" + try: + if not self.db: + return None + + cursor = self.db.cursor() + cursor.execute( + """ + SELECT s.id, s.user_id, u.mobile_number, u.full_name, + s.created_at, s.expires_at + FROM sessions s + JOIN users u ON s.user_id = u.id + WHERE s.id = ? AND u.is_active = 1 + """, + (session_id,), + ) + + row = cursor.fetchone() + if not row: + return None + + session = UserSession( + session_id=row[0], + user_id=row[1], + mobile_number=row[2], + full_name=row[3], + created_at=datetime.fromisoformat(row[4]), + expires_at=datetime.fromisoformat(row[5]), + ) + + # Check if session is still valid + if not session.is_valid: + # Clean up expired session + await self.delete_session(session_id) + return None + + return session + + except Exception as e: + self.logger.error(f"Session validation error: {str(e)}") + return None + + async def logout_user(self, session_id: str) -> bool: + """Logout user by deleting session""" + return await self.delete_session(session_id) + + async def get_user_profile(self, user_id: str) -> Optional[UserProfile]: + """Get user profile by user ID""" + try: + user = await self.get_user_by_id(user_id) + if not user: + return None + + return UserProfile( + user_id=user.id, + full_name=user.full_name, + mobile_number=UserProfile.mask_mobile_number(user.mobile_number), + avatar_url=user.avatar_url, + created_at=user.created_at.isoformat() if user.created_at else None, + ) + + except Exception as e: + self.logger.error(f"Get user profile error: {str(e)}") + return None + + # Database operations + async def save_user(self, user: User) -> bool: + """Save user to database""" + try: + if not self.db: + return False + + cursor = self.db.cursor() + cursor.execute( + """ + INSERT INTO users (id, mobile_number, full_name, password_hash, + avatar_url, preferences, is_active, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + user.id, + user.mobile_number, + user.full_name, + user.password_hash, + user.avatar_url, + user.preferences, + user.is_active, + user.created_at.isoformat() if user.created_at else None, + user.updated_at.isoformat() if user.updated_at else None, + ), + ) + + self.db.commit() + return True + + except Exception as e: + self.logger.error(f"Save user error: {str(e)}") + return False + + async def get_user_by_mobile(self, mobile_number: str) -> Optional[User]: + """Get user by mobile number""" + try: + if not self.db: + return None + + cursor = self.db.cursor() + cursor.execute( + """ + SELECT id, mobile_number, full_name, password_hash, avatar_url, + preferences, is_active, created_at, updated_at + FROM users + WHERE mobile_number = ? + """, + (mobile_number,), + ) + + row = cursor.fetchone() + if not row: + return None + + return User( + id=row[0], + mobile_number=row[1], + full_name=row[2], + password_hash=row[3], + avatar_url=row[4], + preferences=row[5], + is_active=bool(row[6]), + created_at=datetime.fromisoformat(row[7]) if row[7] else None, + updated_at=datetime.fromisoformat(row[8]) if row[8] else None, + ) + + except Exception as e: + self.logger.error(f"Get user by mobile error: {str(e)}") + return None + + async def get_user_by_id(self, user_id: str) -> Optional[User]: + """Get user by ID""" + try: + if not self.db: + return None + + cursor = self.db.cursor() + cursor.execute( + """ + SELECT id, mobile_number, full_name, password_hash, avatar_url, + preferences, is_active, created_at, updated_at + FROM users + WHERE id = ? AND is_active = 1 + """, + (user_id,), + ) + + row = cursor.fetchone() + if not row: + return None + + return User( + id=row[0], + mobile_number=row[1], + full_name=row[2], + password_hash=row[3], + avatar_url=row[4], + preferences=row[5], + is_active=bool(row[6]), + created_at=datetime.fromisoformat(row[7]) if row[7] else None, + updated_at=datetime.fromisoformat(row[8]) if row[8] else None, + ) + + except Exception as e: + self.logger.error(f"Get user by ID error: {str(e)}") + return None + + async def save_session(self, session: UserSession) -> bool: + """Save session to database""" + try: + if not self.db: + return False + + cursor = self.db.cursor() + cursor.execute( + """ + INSERT INTO sessions (id, user_id, title, metadata, created_at, + updated_at, expires_at) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, + ( + session.session_id, + session.user_id, + "User Session", + json.dumps({"login_type": "mobile_password"}), + session.created_at.isoformat(), + session.created_at.isoformat(), + session.expires_at.isoformat(), + ), + ) + + self.db.commit() + return True + + except Exception as e: + self.logger.error(f"Save session error: {str(e)}") + return False + + async def delete_session(self, session_id: str) -> bool: + """Delete session from database""" + try: + if not self.db: + return False + + cursor = self.db.cursor() + cursor.execute("DELETE FROM sessions WHERE id = ?", (session_id,)) + self.db.commit() + return True + + except Exception as e: + self.logger.error(f"Delete session error: {str(e)}") + return False diff --git a/app/bedrock.py b/app/bedrock.py new file mode 100644 index 0000000000000000000000000000000000000000..2806329311d5dbe27d1008faae849cff9771dd7b --- /dev/null +++ b/app/bedrock.py @@ -0,0 +1,334 @@ +import json +import sys +import time +import uuid +from datetime import datetime +from typing import Dict, List, Literal, Optional + +import boto3 + + +# Global variables to track the current tool use ID across function calls +# Tmp solution +CURRENT_TOOLUSE_ID = None + + +# Class to handle OpenAI-style response formatting +class OpenAIResponse: + def __init__(self, data): + # Recursively convert nested dicts and lists to OpenAIResponse objects + for key, value in data.items(): + if isinstance(value, dict): + value = OpenAIResponse(value) + elif isinstance(value, list): + value = [ + OpenAIResponse(item) if isinstance(item, dict) else item + for item in value + ] + setattr(self, key, value) + + def model_dump(self, *args, **kwargs): + # Convert object to dict and add timestamp + data = self.__dict__ + data["created_at"] = datetime.now().isoformat() + return data + + +# Main client class for interacting with Amazon Bedrock +class BedrockClient: + def __init__(self): + # Initialize Bedrock client, you need to configure AWS env first + try: + self.client = boto3.client("bedrock-runtime") + self.chat = Chat(self.client) + except Exception as e: + print(f"Error initializing Bedrock client: {e}") + sys.exit(1) + + +# Chat interface class +class Chat: + def __init__(self, client): + self.completions = ChatCompletions(client) + + +# Core class handling chat completions functionality +class ChatCompletions: + def __init__(self, client): + self.client = client + + def _convert_openai_tools_to_bedrock_format(self, tools): + # Convert OpenAI function calling format to Bedrock tool format + bedrock_tools = [] + for tool in tools: + if tool.get("type") == "function": + function = tool.get("function", {}) + bedrock_tool = { + "toolSpec": { + "name": function.get("name", ""), + "description": function.get("description", ""), + "inputSchema": { + "json": { + "type": "object", + "properties": function.get("parameters", {}).get( + "properties", {} + ), + "required": function.get("parameters", {}).get( + "required", [] + ), + } + }, + } + } + bedrock_tools.append(bedrock_tool) + return bedrock_tools + + def _convert_openai_messages_to_bedrock_format(self, messages): + # Convert OpenAI message format to Bedrock message format + bedrock_messages = [] + system_prompt = [] + for message in messages: + if message.get("role") == "system": + system_prompt = [{"text": message.get("content")}] + elif message.get("role") == "user": + bedrock_message = { + "role": message.get("role", "user"), + "content": [{"text": message.get("content")}], + } + bedrock_messages.append(bedrock_message) + elif message.get("role") == "assistant": + bedrock_message = { + "role": "assistant", + "content": [{"text": message.get("content")}], + } + openai_tool_calls = message.get("tool_calls", []) + if openai_tool_calls: + bedrock_tool_use = { + "toolUseId": openai_tool_calls[0]["id"], + "name": openai_tool_calls[0]["function"]["name"], + "input": json.loads( + openai_tool_calls[0]["function"]["arguments"] + ), + } + bedrock_message["content"].append({"toolUse": bedrock_tool_use}) + global CURRENT_TOOLUSE_ID + CURRENT_TOOLUSE_ID = openai_tool_calls[0]["id"] + bedrock_messages.append(bedrock_message) + elif message.get("role") == "tool": + bedrock_message = { + "role": "user", + "content": [ + { + "toolResult": { + "toolUseId": CURRENT_TOOLUSE_ID, + "content": [{"text": message.get("content")}], + } + } + ], + } + bedrock_messages.append(bedrock_message) + else: + raise ValueError(f"Invalid role: {message.get('role')}") + return system_prompt, bedrock_messages + + def _convert_bedrock_response_to_openai_format(self, bedrock_response): + # Convert Bedrock response format to OpenAI format + content = "" + if bedrock_response.get("output", {}).get("message", {}).get("content"): + content_array = bedrock_response["output"]["message"]["content"] + content = "".join(item.get("text", "") for item in content_array) + if content == "": + content = "." + + # Handle tool calls in response + openai_tool_calls = [] + if bedrock_response.get("output", {}).get("message", {}).get("content"): + for content_item in bedrock_response["output"]["message"]["content"]: + if content_item.get("toolUse"): + bedrock_tool_use = content_item["toolUse"] + global CURRENT_TOOLUSE_ID + CURRENT_TOOLUSE_ID = bedrock_tool_use["toolUseId"] + openai_tool_call = { + "id": CURRENT_TOOLUSE_ID, + "type": "function", + "function": { + "name": bedrock_tool_use["name"], + "arguments": json.dumps(bedrock_tool_use["input"]), + }, + } + openai_tool_calls.append(openai_tool_call) + + # Construct final OpenAI format response + openai_format = { + "id": f"chatcmpl-{uuid.uuid4()}", + "created": int(time.time()), + "object": "chat.completion", + "system_fingerprint": None, + "choices": [ + { + "finish_reason": bedrock_response.get("stopReason", "end_turn"), + "index": 0, + "message": { + "content": content, + "role": bedrock_response.get("output", {}) + .get("message", {}) + .get("role", "assistant"), + "tool_calls": openai_tool_calls + if openai_tool_calls != [] + else None, + "function_call": None, + }, + } + ], + "usage": { + "completion_tokens": bedrock_response.get("usage", {}).get( + "outputTokens", 0 + ), + "prompt_tokens": bedrock_response.get("usage", {}).get( + "inputTokens", 0 + ), + "total_tokens": bedrock_response.get("usage", {}).get("totalTokens", 0), + }, + } + return OpenAIResponse(openai_format) + + async def _invoke_bedrock( + self, + model: str, + messages: List[Dict[str, str]], + max_tokens: int, + temperature: float, + tools: Optional[List[dict]] = None, + tool_choice: Literal["none", "auto", "required"] = "auto", + **kwargs, + ) -> OpenAIResponse: + # Non-streaming invocation of Bedrock model + ( + system_prompt, + bedrock_messages, + ) = self._convert_openai_messages_to_bedrock_format(messages) + response = self.client.converse( + modelId=model, + system=system_prompt, + messages=bedrock_messages, + inferenceConfig={"temperature": temperature, "maxTokens": max_tokens}, + toolConfig={"tools": tools} if tools else None, + ) + openai_response = self._convert_bedrock_response_to_openai_format(response) + return openai_response + + async def _invoke_bedrock_stream( + self, + model: str, + messages: List[Dict[str, str]], + max_tokens: int, + temperature: float, + tools: Optional[List[dict]] = None, + tool_choice: Literal["none", "auto", "required"] = "auto", + **kwargs, + ) -> OpenAIResponse: + # Streaming invocation of Bedrock model + ( + system_prompt, + bedrock_messages, + ) = self._convert_openai_messages_to_bedrock_format(messages) + response = self.client.converse_stream( + modelId=model, + system=system_prompt, + messages=bedrock_messages, + inferenceConfig={"temperature": temperature, "maxTokens": max_tokens}, + toolConfig={"tools": tools} if tools else None, + ) + + # Initialize response structure + bedrock_response = { + "output": {"message": {"role": "", "content": []}}, + "stopReason": "", + "usage": {}, + "metrics": {}, + } + bedrock_response_text = "" + bedrock_response_tool_input = "" + + # Process streaming response + stream = response.get("stream") + if stream: + for event in stream: + if event.get("messageStart", {}).get("role"): + bedrock_response["output"]["message"]["role"] = event[ + "messageStart" + ]["role"] + if event.get("contentBlockDelta", {}).get("delta", {}).get("text"): + bedrock_response_text += event["contentBlockDelta"]["delta"]["text"] + print( + event["contentBlockDelta"]["delta"]["text"], end="", flush=True + ) + if event.get("contentBlockStop", {}).get("contentBlockIndex") == 0: + bedrock_response["output"]["message"]["content"].append( + {"text": bedrock_response_text} + ) + if event.get("contentBlockStart", {}).get("start", {}).get("toolUse"): + bedrock_tool_use = event["contentBlockStart"]["start"]["toolUse"] + tool_use = { + "toolUseId": bedrock_tool_use["toolUseId"], + "name": bedrock_tool_use["name"], + } + bedrock_response["output"]["message"]["content"].append( + {"toolUse": tool_use} + ) + global CURRENT_TOOLUSE_ID + CURRENT_TOOLUSE_ID = bedrock_tool_use["toolUseId"] + if event.get("contentBlockDelta", {}).get("delta", {}).get("toolUse"): + bedrock_response_tool_input += event["contentBlockDelta"]["delta"][ + "toolUse" + ]["input"] + print( + event["contentBlockDelta"]["delta"]["toolUse"]["input"], + end="", + flush=True, + ) + if event.get("contentBlockStop", {}).get("contentBlockIndex") == 1: + bedrock_response["output"]["message"]["content"][1]["toolUse"][ + "input" + ] = json.loads(bedrock_response_tool_input) + print() + openai_response = self._convert_bedrock_response_to_openai_format( + bedrock_response + ) + return openai_response + + def create( + self, + model: str, + messages: List[Dict[str, str]], + max_tokens: int, + temperature: float, + stream: Optional[bool] = True, + tools: Optional[List[dict]] = None, + tool_choice: Literal["none", "auto", "required"] = "auto", + **kwargs, + ) -> OpenAIResponse: + # Main entry point for chat completion + bedrock_tools = [] + if tools is not None: + bedrock_tools = self._convert_openai_tools_to_bedrock_format(tools) + if stream: + return self._invoke_bedrock_stream( + model, + messages, + max_tokens, + temperature, + bedrock_tools, + tool_choice, + **kwargs, + ) + else: + return self._invoke_bedrock( + model, + messages, + max_tokens, + temperature, + bedrock_tools, + tool_choice, + **kwargs, + ) diff --git a/app/cloudflare/__init__.py b/app/cloudflare/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..610c526789bcf4589ffde47ea5876e6fdbe65ad9 --- /dev/null +++ b/app/cloudflare/__init__.py @@ -0,0 +1,11 @@ +""" +Cloudflare services integration for OpenManus +""" + +from .client import CloudflareClient +from .d1 import D1Database +from .durable_objects import DurableObjects +from .kv import KVStorage +from .r2 import R2Storage + +__all__ = ["CloudflareClient", "D1Database", "R2Storage", "KVStorage", "DurableObjects"] diff --git a/app/cloudflare/client.py b/app/cloudflare/client.py new file mode 100644 index 0000000000000000000000000000000000000000..02c3d42ec224f761d0a1ea5aa58669637007f051 --- /dev/null +++ b/app/cloudflare/client.py @@ -0,0 +1,228 @@ +""" +Cloudflare API Client +Handles authentication and base HTTP operations for Cloudflare services +""" + +import asyncio +import json +from typing import Any, Dict, Optional, Union + +import aiohttp + +from app.logger import logger + + +class CloudflareClient: + """Base client for Cloudflare API operations""" + + def __init__( + self, + api_token: str, + account_id: str, + worker_url: Optional[str] = None, + timeout: int = 30, + ): + self.api_token = api_token + self.account_id = account_id + self.worker_url = worker_url + self.timeout = timeout + self.base_url = "https://api.cloudflare.com/client/v4" + + # HTTP headers for API requests + self.headers = { + "Authorization": f"Bearer {api_token}", + "Content-Type": "application/json", + } + + async def _make_request( + self, + method: str, + url: str, + data: Optional[Dict[str, Any]] = None, + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Make HTTP request to Cloudflare API or Worker""" + + # Use worker URL if specified and use_worker is True + if use_worker and self.worker_url: + full_url = f"{self.worker_url.rstrip('/')}/{url.lstrip('/')}" + else: + full_url = f"{self.base_url}/{url.lstrip('/')}" + + request_headers = self.headers.copy() + if headers: + request_headers.update(headers) + + timeout = aiohttp.ClientTimeout(total=self.timeout) + + try: + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.request( + method=method.upper(), + url=full_url, + headers=request_headers, + json=data if data else None, + ) as response: + response_text = await response.text() + + try: + response_data = ( + json.loads(response_text) if response_text else {} + ) + except json.JSONDecodeError: + response_data = {"raw_response": response_text} + + if not response.ok: + logger.error( + f"Cloudflare API error: {response.status} - {response_text}" + ) + raise CloudflareError( + f"HTTP {response.status}: {response_text}", + response.status, + response_data, + ) + + return response_data + + except asyncio.TimeoutError: + logger.error(f"Timeout making request to {full_url}") + raise CloudflareError(f"Request timeout after {self.timeout}s") + except aiohttp.ClientError as e: + logger.error(f"HTTP client error: {e}") + raise CloudflareError(f"Client error: {e}") + + async def get( + self, + url: str, + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Make GET request""" + return await self._make_request( + "GET", url, headers=headers, use_worker=use_worker + ) + + async def post( + self, + url: str, + data: Optional[Dict[str, Any]] = None, + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Make POST request""" + return await self._make_request( + "POST", url, data=data, headers=headers, use_worker=use_worker + ) + + async def put( + self, + url: str, + data: Optional[Dict[str, Any]] = None, + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Make PUT request""" + return await self._make_request( + "PUT", url, data=data, headers=headers, use_worker=use_worker + ) + + async def delete( + self, + url: str, + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Make DELETE request""" + return await self._make_request( + "DELETE", url, headers=headers, use_worker=use_worker + ) + + async def upload_file( + self, + url: str, + file_data: bytes, + content_type: str = "application/octet-stream", + headers: Optional[Dict[str, str]] = None, + use_worker: bool = False, + ) -> Dict[str, Any]: + """Upload file data""" + + # Use worker URL if specified and use_worker is True + if use_worker and self.worker_url: + full_url = f"{self.worker_url.rstrip('/')}/{url.lstrip('/')}" + else: + full_url = f"{self.base_url}/{url.lstrip('/')}" + + upload_headers = { + "Authorization": f"Bearer {self.api_token}", + "Content-Type": content_type, + } + if headers: + upload_headers.update(headers) + + timeout = aiohttp.ClientTimeout( + total=self.timeout * 2 + ) # Longer timeout for uploads + + try: + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.put( + url=full_url, headers=upload_headers, data=file_data + ) as response: + response_text = await response.text() + + try: + response_data = ( + json.loads(response_text) if response_text else {} + ) + except json.JSONDecodeError: + response_data = {"raw_response": response_text} + + if not response.ok: + logger.error( + f"File upload error: {response.status} - {response_text}" + ) + raise CloudflareError( + f"Upload failed: HTTP {response.status}", + response.status, + response_data, + ) + + return response_data + + except asyncio.TimeoutError: + logger.error(f"Timeout uploading file to {full_url}") + raise CloudflareError(f"Upload timeout after {self.timeout * 2}s") + except aiohttp.ClientError as e: + logger.error(f"Upload client error: {e}") + raise CloudflareError(f"Upload error: {e}") + + def get_account_url(self, endpoint: str) -> str: + """Get URL for account-scoped endpoint""" + return f"accounts/{self.account_id}/{endpoint}" + + def get_worker_url(self, endpoint: str) -> str: + """Get URL for worker endpoint""" + if not self.worker_url: + raise CloudflareError("Worker URL not configured") + return endpoint + + +class CloudflareError(Exception): + """Cloudflare API error""" + + def __init__( + self, + message: str, + status_code: Optional[int] = None, + response_data: Optional[Dict[str, Any]] = None, + ): + super().__init__(message) + self.status_code = status_code + self.response_data = response_data or {} + + def __str__(self) -> str: + if self.status_code: + return f"CloudflareError({self.status_code}): {super().__str__()}" + return f"CloudflareError: {super().__str__()}" diff --git a/app/cloudflare/d1.py b/app/cloudflare/d1.py new file mode 100644 index 0000000000000000000000000000000000000000..91884d0271d6a49d2396af7fc8e6f09de10f8297 --- /dev/null +++ b/app/cloudflare/d1.py @@ -0,0 +1,510 @@ +""" +D1 Database integration for OpenManus +Provides interface to Cloudflare D1 database operations +""" + +from typing import Any, Dict, List, Optional, Union + +from app.logger import logger + +from .client import CloudflareClient, CloudflareError + + +class D1Database: + """Cloudflare D1 Database client""" + + def __init__(self, client: CloudflareClient, database_id: str): + self.client = client + self.database_id = database_id + self.base_endpoint = f"accounts/{client.account_id}/d1/database/{database_id}" + + async def execute_query( + self, sql: str, params: Optional[List[Any]] = None, use_worker: bool = True + ) -> Dict[str, Any]: + """Execute a SQL query""" + + query_data = {"sql": sql} + + if params: + query_data["params"] = params + + try: + if use_worker: + # Use worker endpoint for better performance + response = await self.client.post( + "api/database/query", data=query_data, use_worker=True + ) + else: + # Use Cloudflare API directly + response = await self.client.post( + f"{self.base_endpoint}/query", data=query_data + ) + + return response + + except CloudflareError as e: + logger.error(f"D1 query execution failed: {e}") + raise + + async def batch_execute( + self, queries: List[Dict[str, Any]], use_worker: bool = True + ) -> Dict[str, Any]: + """Execute multiple queries in a batch""" + + batch_data = {"queries": queries} + + try: + if use_worker: + response = await self.client.post( + "api/database/batch", data=batch_data, use_worker=True + ) + else: + response = await self.client.post( + f"{self.base_endpoint}/query", data=batch_data + ) + + return response + + except CloudflareError as e: + logger.error(f"D1 batch execution failed: {e}") + raise + + # User management methods + async def create_user( + self, + user_id: str, + username: str, + email: Optional[str] = None, + metadata: Optional[Dict[str, Any]] = None, + ) -> Dict[str, Any]: + """Create a new user""" + + sql = """ + INSERT INTO users (id, username, email, metadata) + VALUES (?, ?, ?, ?) + ON CONFLICT(id) DO UPDATE SET + username = excluded.username, + email = excluded.email, + metadata = excluded.metadata, + updated_at = strftime('%s', 'now') + """ + + import json + + params = [user_id, username, email, json.dumps(metadata or {})] + + return await self.execute_query(sql, params) + + async def get_user(self, user_id: str) -> Optional[Dict[str, Any]]: + """Get user by ID""" + + sql = "SELECT * FROM users WHERE id = ?" + params = [user_id] + + result = await self.execute_query(sql, params) + + # Parse response based on Cloudflare D1 format + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + user = rows[0] + if user.get("metadata"): + import json + + user["metadata"] = json.loads(user["metadata"]) + return user + + return None + + async def get_user_by_username(self, username: str) -> Optional[Dict[str, Any]]: + """Get user by username""" + + sql = "SELECT * FROM users WHERE username = ?" + params = [username] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + user = rows[0] + if user.get("metadata"): + import json + + user["metadata"] = json.loads(user["metadata"]) + return user + + return None + + # Session management methods + async def create_session( + self, + session_id: str, + user_id: str, + session_data: Dict[str, Any], + expires_at: Optional[int] = None, + ) -> Dict[str, Any]: + """Create a new session""" + + sql = """ + INSERT INTO sessions (id, user_id, session_data, expires_at) + VALUES (?, ?, ?, ?) + """ + + import json + + params = [session_id, user_id, json.dumps(session_data), expires_at] + + return await self.execute_query(sql, params) + + async def get_session(self, session_id: str) -> Optional[Dict[str, Any]]: + """Get session by ID""" + + sql = """ + SELECT * FROM sessions + WHERE id = ? AND (expires_at IS NULL OR expires_at > strftime('%s', 'now')) + """ + params = [session_id] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + session = rows[0] + if session.get("session_data"): + import json + + session["session_data"] = json.loads(session["session_data"]) + return session + + return None + + async def delete_session(self, session_id: str) -> Dict[str, Any]: + """Delete a session""" + + sql = "DELETE FROM sessions WHERE id = ?" + params = [session_id] + + return await self.execute_query(sql, params) + + # Conversation methods + async def create_conversation( + self, + conversation_id: str, + user_id: str, + title: Optional[str] = None, + messages: Optional[List[Dict[str, Any]]] = None, + ) -> Dict[str, Any]: + """Create a new conversation""" + + sql = """ + INSERT INTO conversations (id, user_id, title, messages) + VALUES (?, ?, ?, ?) + """ + + import json + + params = [conversation_id, user_id, title, json.dumps(messages or [])] + + return await self.execute_query(sql, params) + + async def get_conversation(self, conversation_id: str) -> Optional[Dict[str, Any]]: + """Get conversation by ID""" + + sql = "SELECT * FROM conversations WHERE id = ?" + params = [conversation_id] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + conversation = rows[0] + if conversation.get("messages"): + import json + + conversation["messages"] = json.loads(conversation["messages"]) + return conversation + + return None + + async def update_conversation_messages( + self, conversation_id: str, messages: List[Dict[str, Any]] + ) -> Dict[str, Any]: + """Update conversation messages""" + + sql = """ + UPDATE conversations + SET messages = ?, updated_at = strftime('%s', 'now') + WHERE id = ? + """ + + import json + + params = [json.dumps(messages), conversation_id] + + return await self.execute_query(sql, params) + + async def get_user_conversations( + self, user_id: str, limit: int = 50 + ) -> List[Dict[str, Any]]: + """Get user's conversations""" + + sql = """ + SELECT id, user_id, title, created_at, updated_at + FROM conversations + WHERE user_id = ? + ORDER BY updated_at DESC + LIMIT ? + """ + params = [user_id, limit] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + return result["result"][0].get("results", []) + + return [] + + # Agent execution methods + async def create_agent_execution( + self, + execution_id: str, + user_id: str, + session_id: Optional[str] = None, + task_description: Optional[str] = None, + status: str = "pending", + ) -> Dict[str, Any]: + """Create a new agent execution record""" + + sql = """ + INSERT INTO agent_executions (id, user_id, session_id, task_description, status) + VALUES (?, ?, ?, ?, ?) + """ + + params = [execution_id, user_id, session_id, task_description, status] + + return await self.execute_query(sql, params) + + async def update_agent_execution( + self, + execution_id: str, + status: Optional[str] = None, + result: Optional[str] = None, + execution_time: Optional[int] = None, + ) -> Dict[str, Any]: + """Update agent execution record""" + + updates = [] + params = [] + + if status: + updates.append("status = ?") + params.append(status) + + if result: + updates.append("result = ?") + params.append(result) + + if execution_time is not None: + updates.append("execution_time = ?") + params.append(execution_time) + + if status in ["completed", "failed"]: + updates.append("completed_at = strftime('%s', 'now')") + + if not updates: + return {"success": True, "message": "No updates provided"} + + sql = f""" + UPDATE agent_executions + SET {', '.join(updates)} + WHERE id = ? + """ + params.append(execution_id) + + return await self.execute_query(sql, params) + + async def get_agent_execution(self, execution_id: str) -> Optional[Dict[str, Any]]: + """Get agent execution by ID""" + + sql = "SELECT * FROM agent_executions WHERE id = ?" + params = [execution_id] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + return rows[0] + + return None + + async def get_user_executions( + self, user_id: str, limit: int = 50 + ) -> List[Dict[str, Any]]: + """Get user's agent executions""" + + sql = """ + SELECT * FROM agent_executions + WHERE user_id = ? + ORDER BY created_at DESC + LIMIT ? + """ + params = [user_id, limit] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + return result["result"][0].get("results", []) + + return [] + + # File record methods + async def create_file_record( + self, + file_id: str, + user_id: str, + filename: str, + file_key: str, + file_size: int, + content_type: str, + bucket: str = "storage", + ) -> Dict[str, Any]: + """Create a file record""" + + sql = """ + INSERT INTO files (id, user_id, filename, file_key, file_size, content_type, bucket) + VALUES (?, ?, ?, ?, ?, ?, ?) + """ + + params = [file_id, user_id, filename, file_key, file_size, content_type, bucket] + + return await self.execute_query(sql, params) + + async def get_file_record(self, file_id: str) -> Optional[Dict[str, Any]]: + """Get file record by ID""" + + sql = "SELECT * FROM files WHERE id = ?" + params = [file_id] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + rows = result["result"][0].get("results", []) + if rows: + return rows[0] + + return None + + async def get_user_files( + self, user_id: str, limit: int = 100 + ) -> List[Dict[str, Any]]: + """Get user's files""" + + sql = """ + SELECT * FROM files + WHERE user_id = ? + ORDER BY created_at DESC + LIMIT ? + """ + params = [user_id, limit] + + result = await self.execute_query(sql, params) + + if result.get("success") and result.get("result"): + return result["result"][0].get("results", []) + + return [] + + async def delete_file_record(self, file_id: str) -> Dict[str, Any]: + """Delete a file record""" + + sql = "DELETE FROM files WHERE id = ?" + params = [file_id] + + return await self.execute_query(sql, params) + + # Schema initialization + async def initialize_schema(self) -> Dict[str, Any]: + """Initialize database schema""" + + schema_queries = [ + { + "sql": """CREATE TABLE IF NOT EXISTS users ( + id TEXT PRIMARY KEY, + username TEXT UNIQUE NOT NULL, + email TEXT UNIQUE, + created_at INTEGER DEFAULT (strftime('%s', 'now')), + updated_at INTEGER DEFAULT (strftime('%s', 'now')), + metadata TEXT + )""" + }, + { + "sql": """CREATE TABLE IF NOT EXISTS sessions ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + session_data TEXT, + created_at INTEGER DEFAULT (strftime('%s', 'now')), + expires_at INTEGER, + FOREIGN KEY (user_id) REFERENCES users(id) + )""" + }, + { + "sql": """CREATE TABLE IF NOT EXISTS conversations ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + title TEXT, + messages TEXT, + created_at INTEGER DEFAULT (strftime('%s', 'now')), + updated_at INTEGER DEFAULT (strftime('%s', 'now')), + FOREIGN KEY (user_id) REFERENCES users(id) + )""" + }, + { + "sql": """CREATE TABLE IF NOT EXISTS files ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + filename TEXT NOT NULL, + file_key TEXT NOT NULL, + file_size INTEGER, + content_type TEXT, + bucket TEXT DEFAULT 'storage', + created_at INTEGER DEFAULT (strftime('%s', 'now')), + FOREIGN KEY (user_id) REFERENCES users(id) + )""" + }, + { + "sql": """CREATE TABLE IF NOT EXISTS agent_executions ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + session_id TEXT, + task_description TEXT, + status TEXT DEFAULT 'pending', + result TEXT, + execution_time INTEGER, + created_at INTEGER DEFAULT (strftime('%s', 'now')), + completed_at INTEGER, + FOREIGN KEY (user_id) REFERENCES users(id) + )""" + }, + ] + + # Add indexes + index_queries = [ + { + "sql": "CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id)" + }, + { + "sql": "CREATE INDEX IF NOT EXISTS idx_conversations_user_id ON conversations(user_id)" + }, + {"sql": "CREATE INDEX IF NOT EXISTS idx_files_user_id ON files(user_id)"}, + { + "sql": "CREATE INDEX IF NOT EXISTS idx_agent_executions_user_id ON agent_executions(user_id)" + }, + ] + + all_queries = schema_queries + index_queries + + return await self.batch_execute(all_queries) diff --git a/app/cloudflare/durable_objects.py b/app/cloudflare/durable_objects.py new file mode 100644 index 0000000000000000000000000000000000000000..e7d1a84a6b1d74e9c097c69bd75668c832302255 --- /dev/null +++ b/app/cloudflare/durable_objects.py @@ -0,0 +1,365 @@ +""" +Durable Objects integration for OpenManus +Provides interface to Cloudflare Durable Objects operations +""" + +import json +import time +from typing import Any, Dict, List, Optional + +from app.logger import logger + +from .client import CloudflareClient, CloudflareError + + +class DurableObjects: + """Cloudflare Durable Objects client""" + + def __init__(self, client: CloudflareClient): + self.client = client + + async def create_agent_session( + self, session_id: str, user_id: str, metadata: Optional[Dict[str, Any]] = None + ) -> Dict[str, Any]: + """Create a new agent session""" + + session_data = { + "sessionId": session_id, + "userId": user_id, + "metadata": metadata or {}, + } + + try: + response = await self.client.post( + f"do/agent/{session_id}/start", data=session_data, use_worker=True + ) + + return { + "success": True, + "session_id": session_id, + "user_id": user_id, + **response, + } + + except CloudflareError as e: + logger.error(f"Failed to create agent session: {e}") + raise + + async def get_agent_session_status(self, session_id: str) -> Dict[str, Any]: + """Get agent session status""" + + try: + response = await self.client.get( + f"do/agent/{session_id}/status?sessionId={session_id}", use_worker=True + ) + + return response + + except CloudflareError as e: + logger.error(f"Failed to get agent session status: {e}") + raise + + async def update_agent_session( + self, session_id: str, updates: Dict[str, Any] + ) -> Dict[str, Any]: + """Update agent session""" + + update_data = {"sessionId": session_id, "updates": updates} + + try: + response = await self.client.post( + f"do/agent/{session_id}/update", data=update_data, use_worker=True + ) + + return {"success": True, "session_id": session_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to update agent session: {e}") + raise + + async def stop_agent_session(self, session_id: str) -> Dict[str, Any]: + """Stop agent session""" + + try: + response = await self.client.post( + f"do/agent/{session_id}/stop", + data={"sessionId": session_id}, + use_worker=True, + ) + + return {"success": True, "session_id": session_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to stop agent session: {e}") + raise + + async def add_agent_message( + self, session_id: str, message: Dict[str, Any] + ) -> Dict[str, Any]: + """Add a message to agent session""" + + message_data = { + "sessionId": session_id, + "message": {"timestamp": int(time.time()), **message}, + } + + try: + response = await self.client.post( + f"do/agent/{session_id}/messages", data=message_data, use_worker=True + ) + + return {"success": True, "session_id": session_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to add agent message: {e}") + raise + + async def get_agent_messages( + self, session_id: str, limit: int = 50, offset: int = 0 + ) -> Dict[str, Any]: + """Get agent session messages""" + + try: + response = await self.client.get( + f"do/agent/{session_id}/messages?sessionId={session_id}&limit={limit}&offset={offset}", + use_worker=True, + ) + + return response + + except CloudflareError as e: + logger.error(f"Failed to get agent messages: {e}") + raise + + # Chat Room methods + async def join_chat_room( + self, + room_id: str, + user_id: str, + username: str, + room_config: Optional[Dict[str, Any]] = None, + ) -> Dict[str, Any]: + """Join a chat room""" + + join_data = { + "userId": user_id, + "username": username, + "roomConfig": room_config or {}, + } + + try: + response = await self.client.post( + f"do/chat/{room_id}/join", data=join_data, use_worker=True + ) + + return {"success": True, "room_id": room_id, "user_id": user_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to join chat room: {e}") + raise + + async def leave_chat_room(self, room_id: str, user_id: str) -> Dict[str, Any]: + """Leave a chat room""" + + leave_data = {"userId": user_id} + + try: + response = await self.client.post( + f"do/chat/{room_id}/leave", data=leave_data, use_worker=True + ) + + return {"success": True, "room_id": room_id, "user_id": user_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to leave chat room: {e}") + raise + + async def get_chat_room_info(self, room_id: str) -> Dict[str, Any]: + """Get chat room information""" + + try: + response = await self.client.get(f"do/chat/{room_id}/info", use_worker=True) + + return response + + except CloudflareError as e: + logger.error(f"Failed to get chat room info: {e}") + raise + + async def send_chat_message( + self, + room_id: str, + user_id: str, + username: str, + content: str, + message_type: str = "text", + ) -> Dict[str, Any]: + """Send a message to chat room""" + + message_data = { + "userId": user_id, + "username": username, + "content": content, + "messageType": message_type, + } + + try: + response = await self.client.post( + f"do/chat/{room_id}/messages", data=message_data, use_worker=True + ) + + return {"success": True, "room_id": room_id, **response} + + except CloudflareError as e: + logger.error(f"Failed to send chat message: {e}") + raise + + async def get_chat_messages( + self, room_id: str, limit: int = 50, offset: int = 0 + ) -> Dict[str, Any]: + """Get chat room messages""" + + try: + response = await self.client.get( + f"do/chat/{room_id}/messages?limit={limit}&offset={offset}", + use_worker=True, + ) + + return response + + except CloudflareError as e: + logger.error(f"Failed to get chat messages: {e}") + raise + + async def get_chat_participants(self, room_id: str) -> Dict[str, Any]: + """Get chat room participants""" + + try: + response = await self.client.get( + f"do/chat/{room_id}/participants", use_worker=True + ) + + return response + + except CloudflareError as e: + logger.error(f"Failed to get chat participants: {e}") + raise + + # WebSocket connection helpers + def get_agent_websocket_url(self, session_id: str, user_id: str) -> str: + """Get WebSocket URL for agent session""" + + if not self.client.worker_url: + raise CloudflareError("Worker URL not configured") + + base_url = self.client.worker_url.replace("https://", "wss://").replace( + "http://", "ws://" + ) + return ( + f"{base_url}/do/agent/{session_id}?sessionId={session_id}&userId={user_id}" + ) + + def get_chat_websocket_url(self, room_id: str, user_id: str, username: str) -> str: + """Get WebSocket URL for chat room""" + + if not self.client.worker_url: + raise CloudflareError("Worker URL not configured") + + base_url = self.client.worker_url.replace("https://", "wss://").replace( + "http://", "ws://" + ) + return f"{base_url}/do/chat/{room_id}?userId={user_id}&username={username}" + + +class DurableObjectsWebSocket: + """Helper class for WebSocket connections to Durable Objects""" + + def __init__(self, url: str): + self.url = url + self.websocket = None + self.connected = False + self.message_handlers = {} + + async def connect(self): + """Connect to WebSocket""" + try: + import websockets + + self.websocket = await websockets.connect(self.url) + self.connected = True + logger.info(f"Connected to Durable Object WebSocket: {self.url}") + + # Start message handling loop + import asyncio + + asyncio.create_task(self._message_loop()) + + except Exception as e: + logger.error(f"Failed to connect to WebSocket: {e}") + raise CloudflareError(f"WebSocket connection failed: {e}") + + async def disconnect(self): + """Disconnect from WebSocket""" + if self.websocket and self.connected: + await self.websocket.close() + self.connected = False + logger.info("Disconnected from Durable Object WebSocket") + + async def send_message(self, message_type: str, payload: Dict[str, Any]): + """Send message via WebSocket""" + if not self.connected or not self.websocket: + raise CloudflareError("WebSocket not connected") + + message = { + "type": message_type, + "payload": payload, + "timestamp": int(time.time()), + } + + try: + await self.websocket.send(json.dumps(message)) + except Exception as e: + logger.error(f"Failed to send WebSocket message: {e}") + raise CloudflareError(f"Failed to send message: {e}") + + def add_message_handler(self, message_type: str, handler): + """Add a message handler for specific message types""" + if message_type not in self.message_handlers: + self.message_handlers[message_type] = [] + self.message_handlers[message_type].append(handler) + + async def _message_loop(self): + """Handle incoming WebSocket messages""" + try: + async for message in self.websocket: + try: + data = json.loads(message) + message_type = data.get("type") + + if message_type in self.message_handlers: + for handler in self.message_handlers[message_type]: + try: + if callable(handler): + if asyncio.iscoroutinefunction(handler): + await handler(data) + else: + handler(data) + except Exception as e: + logger.error(f"Message handler error: {e}") + + except json.JSONDecodeError as e: + logger.error(f"Failed to parse WebSocket message: {e}") + except Exception as e: + logger.error(f"WebSocket message processing error: {e}") + + except Exception as e: + logger.error(f"WebSocket message loop error: {e}") + self.connected = False + + # Context manager support + async def __aenter__(self): + await self.connect() + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb): + await self.disconnect() diff --git a/app/cloudflare/kv.py b/app/cloudflare/kv.py new file mode 100644 index 0000000000000000000000000000000000000000..65c54e82e95c7c700578aa31bb9f8e4a08444019 --- /dev/null +++ b/app/cloudflare/kv.py @@ -0,0 +1,457 @@ +""" +KV Storage integration for OpenManus +Provides interface to Cloudflare KV operations +""" + +import json +from typing import Any, Dict, List, Optional + +from app.logger import logger + +from .client import CloudflareClient, CloudflareError + + +class KVStorage: + """Cloudflare KV Storage client""" + + def __init__( + self, + client: CloudflareClient, + sessions_namespace_id: str, + cache_namespace_id: str, + ): + self.client = client + self.sessions_namespace_id = sessions_namespace_id + self.cache_namespace_id = cache_namespace_id + self.base_endpoint = f"accounts/{client.account_id}/storage/kv/namespaces" + + def _get_namespace_id(self, namespace_type: str) -> str: + """Get namespace ID based on type""" + if namespace_type == "cache": + return self.cache_namespace_id + return self.sessions_namespace_id + + async def set_value( + self, + key: str, + value: Any, + namespace_type: str = "sessions", + ttl: Optional[int] = None, + use_worker: bool = True, + ) -> Dict[str, Any]: + """Set a value in KV store""" + + namespace_id = self._get_namespace_id(namespace_type) + + # Serialize value to JSON + if isinstance(value, (dict, list)): + serialized_value = json.dumps(value) + elif isinstance(value, str): + serialized_value = value + else: + serialized_value = json.dumps(value) + + try: + if use_worker: + set_data = { + "key": key, + "value": serialized_value, + "namespace": namespace_type, + } + + if ttl: + set_data["ttl"] = ttl + + response = await self.client.post( + f"api/kv/set", data=set_data, use_worker=True + ) + else: + # Use KV API directly + params = {} + if ttl: + params["expiration_ttl"] = ttl + + query_string = "&".join([f"{k}={v}" for k, v in params.items()]) + endpoint = f"{self.base_endpoint}/{namespace_id}/values/{key}" + if query_string: + endpoint += f"?{query_string}" + + response = await self.client.put( + endpoint, data={"value": serialized_value} + ) + + return { + "success": True, + "key": key, + "namespace": namespace_type, + "ttl": ttl, + **response, + } + + except CloudflareError as e: + logger.error(f"KV set value failed: {e}") + raise + + async def get_value( + self, + key: str, + namespace_type: str = "sessions", + parse_json: bool = True, + use_worker: bool = True, + ) -> Optional[Any]: + """Get a value from KV store""" + + namespace_id = self._get_namespace_id(namespace_type) + + try: + if use_worker: + response = await self.client.get( + f"api/kv/get/{key}?namespace={namespace_type}", use_worker=True + ) + + if response and "value" in response: + value = response["value"] + + if parse_json and isinstance(value, str): + try: + return json.loads(value) + except json.JSONDecodeError: + return value + + return value + else: + response = await self.client.get( + f"{self.base_endpoint}/{namespace_id}/values/{key}" + ) + + # KV API returns the value directly as text + value = ( + response.get("result", {}).get("value") + if "result" in response + else response + ) + + if value and parse_json and isinstance(value, str): + try: + return json.loads(value) + except json.JSONDecodeError: + return value + + return value + + except CloudflareError as e: + if e.status_code == 404: + return None + logger.error(f"KV get value failed: {e}") + raise + + return None + + async def delete_value( + self, key: str, namespace_type: str = "sessions", use_worker: bool = True + ) -> Dict[str, Any]: + """Delete a value from KV store""" + + namespace_id = self._get_namespace_id(namespace_type) + + try: + if use_worker: + response = await self.client.delete( + f"api/kv/delete/{key}?namespace={namespace_type}", use_worker=True + ) + else: + response = await self.client.delete( + f"{self.base_endpoint}/{namespace_id}/values/{key}" + ) + + return { + "success": True, + "key": key, + "namespace": namespace_type, + **response, + } + + except CloudflareError as e: + logger.error(f"KV delete value failed: {e}") + raise + + async def list_keys( + self, + namespace_type: str = "sessions", + prefix: str = "", + limit: int = 1000, + use_worker: bool = True, + ) -> Dict[str, Any]: + """List keys in KV namespace""" + + namespace_id = self._get_namespace_id(namespace_type) + + try: + if use_worker: + params = {"namespace": namespace_type, "prefix": prefix, "limit": limit} + + query_string = "&".join([f"{k}={v}" for k, v in params.items() if v]) + response = await self.client.get( + f"api/kv/list?{query_string}", use_worker=True + ) + else: + params = {"prefix": prefix, "limit": limit} + + query_string = "&".join([f"{k}={v}" for k, v in params.items() if v]) + response = await self.client.get( + f"{self.base_endpoint}/{namespace_id}/keys?{query_string}" + ) + + return { + "namespace": namespace_type, + "prefix": prefix, + "keys": ( + response.get("result", []) + if "result" in response + else response.get("keys", []) + ), + **response, + } + + except CloudflareError as e: + logger.error(f"KV list keys failed: {e}") + raise + + # Session-specific methods + async def set_session( + self, + session_id: str, + session_data: Dict[str, Any], + ttl: int = 86400, # 24 hours default + ) -> Dict[str, Any]: + """Set session data""" + + data = { + **session_data, + "created_at": session_data.get("created_at", int(time.time())), + "expires_at": int(time.time()) + ttl, + } + + return await self.set_value(f"session:{session_id}", data, "sessions", ttl) + + async def get_session(self, session_id: str) -> Optional[Dict[str, Any]]: + """Get session data""" + + session = await self.get_value(f"session:{session_id}", "sessions") + + if session and isinstance(session, dict): + # Check if session is expired + expires_at = session.get("expires_at") + if expires_at and int(time.time()) > expires_at: + await self.delete_session(session_id) + return None + + return session + + async def delete_session(self, session_id: str) -> Dict[str, Any]: + """Delete session data""" + + return await self.delete_value(f"session:{session_id}", "sessions") + + async def update_session( + self, session_id: str, updates: Dict[str, Any], extend_ttl: Optional[int] = None + ) -> Dict[str, Any]: + """Update session data""" + + existing_session = await self.get_session(session_id) + + if not existing_session: + raise CloudflareError("Session not found") + + updated_data = {**existing_session, **updates, "updated_at": int(time.time())} + + # Calculate TTL + ttl = None + if extend_ttl: + ttl = extend_ttl + elif existing_session.get("expires_at"): + ttl = max(0, existing_session["expires_at"] - int(time.time())) + + return await self.set_session(session_id, updated_data, ttl or 86400) + + # Cache-specific methods + async def set_cache( + self, key: str, data: Any, ttl: int = 3600 # 1 hour default + ) -> Dict[str, Any]: + """Set cache data""" + + cache_data = { + "data": data, + "cached_at": int(time.time()), + "expires_at": int(time.time()) + ttl, + } + + return await self.set_value(f"cache:{key}", cache_data, "cache", ttl) + + async def get_cache(self, key: str) -> Optional[Any]: + """Get cache data""" + + cached = await self.get_value(f"cache:{key}", "cache") + + if cached and isinstance(cached, dict): + # Check if cache is expired + expires_at = cached.get("expires_at") + if expires_at and int(time.time()) > expires_at: + await self.delete_cache(key) + return None + + return cached.get("data") + + return cached + + async def delete_cache(self, key: str) -> Dict[str, Any]: + """Delete cache data""" + + return await self.delete_value(f"cache:{key}", "cache") + + # User-specific methods + async def set_user_cache( + self, user_id: str, key: str, data: Any, ttl: int = 3600 + ) -> Dict[str, Any]: + """Set user-specific cache""" + + user_key = f"user:{user_id}:{key}" + return await self.set_cache(user_key, data, ttl) + + async def get_user_cache(self, user_id: str, key: str) -> Optional[Any]: + """Get user-specific cache""" + + user_key = f"user:{user_id}:{key}" + return await self.get_cache(user_key) + + async def delete_user_cache(self, user_id: str, key: str) -> Dict[str, Any]: + """Delete user-specific cache""" + + user_key = f"user:{user_id}:{key}" + return await self.delete_cache(user_key) + + async def get_user_cache_keys(self, user_id: str, limit: int = 100) -> List[str]: + """Get all cache keys for a user""" + + result = await self.list_keys("cache", f"cache:user:{user_id}:", limit) + + keys = [] + for key_info in result.get("keys", []): + if isinstance(key_info, dict): + key = key_info.get("name", "") + else: + key = str(key_info) + + # Remove prefix to get the actual key + if key.startswith(f"cache:user:{user_id}:"): + clean_key = key.replace(f"cache:user:{user_id}:", "") + keys.append(clean_key) + + return keys + + # Conversation caching + async def cache_conversation( + self, + conversation_id: str, + messages: List[Dict[str, Any]], + ttl: int = 7200, # 2 hours default + ) -> Dict[str, Any]: + """Cache conversation messages""" + + return await self.set_cache( + f"conversation:{conversation_id}", + {"messages": messages, "last_updated": int(time.time())}, + ttl, + ) + + async def get_cached_conversation( + self, conversation_id: str + ) -> Optional[Dict[str, Any]]: + """Get cached conversation""" + + return await self.get_cache(f"conversation:{conversation_id}") + + # Agent execution caching + async def cache_agent_execution( + self, execution_id: str, execution_data: Dict[str, Any], ttl: int = 3600 + ) -> Dict[str, Any]: + """Cache agent execution data""" + + return await self.set_cache(f"execution:{execution_id}", execution_data, ttl) + + async def get_cached_agent_execution( + self, execution_id: str + ) -> Optional[Dict[str, Any]]: + """Get cached agent execution""" + + return await self.get_cache(f"execution:{execution_id}") + + # Batch operations + async def set_batch( + self, + items: List[Dict[str, Any]], + namespace_type: str = "cache", + ttl: Optional[int] = None, + ) -> Dict[str, Any]: + """Set multiple values (simulated batch operation)""" + + results = [] + successful = 0 + failed = 0 + + for item in items: + try: + key = item["key"] + value = item["value"] + item_ttl = item.get("ttl", ttl) + + result = await self.set_value(key, value, namespace_type, item_ttl) + results.append({"key": key, "success": True, "result": result}) + successful += 1 + + except Exception as e: + results.append( + {"key": item.get("key"), "success": False, "error": str(e)} + ) + failed += 1 + + return { + "success": failed == 0, + "successful": successful, + "failed": failed, + "total": len(items), + "results": results, + } + + async def get_batch( + self, keys: List[str], namespace_type: str = "cache" + ) -> Dict[str, Any]: + """Get multiple values (simulated batch operation)""" + + results = {} + + for key in keys: + try: + value = await self.get_value(key, namespace_type) + results[key] = value + except Exception as e: + logger.error(f"Failed to get key {key}: {e}") + results[key] = None + + return results + + def _hash_params(self, params: Dict[str, Any]) -> str: + """Create a hash for cache keys from parameters""" + + if not params: + return "no-params" + + # Simple hash function for cache keys + import hashlib + + params_str = json.dumps(params, sort_keys=True) + return hashlib.md5(params_str.encode()).hexdigest()[:16] + + +# Add time import at the top +import time diff --git a/app/cloudflare/r2.py b/app/cloudflare/r2.py new file mode 100644 index 0000000000000000000000000000000000000000..9766cc1c52bf11a5faf1eae4dfabfe5e6225b320 --- /dev/null +++ b/app/cloudflare/r2.py @@ -0,0 +1,434 @@ +""" +R2 Storage integration for OpenManus +Provides interface to Cloudflare R2 storage operations +""" + +import io +from typing import Any, BinaryIO, Dict, List, Optional + +from app.logger import logger + +from .client import CloudflareClient, CloudflareError + + +class R2Storage: + """Cloudflare R2 Storage client""" + + def __init__( + self, + client: CloudflareClient, + storage_bucket: str, + assets_bucket: Optional[str] = None, + ): + self.client = client + self.storage_bucket = storage_bucket + self.assets_bucket = assets_bucket or storage_bucket + self.base_endpoint = f"accounts/{client.account_id}/r2/buckets" + + def _get_bucket_name(self, bucket_type: str = "storage") -> str: + """Get bucket name based on type""" + if bucket_type == "assets": + return self.assets_bucket + return self.storage_bucket + + async def upload_file( + self, + key: str, + file_data: bytes, + content_type: str = "application/octet-stream", + bucket_type: str = "storage", + metadata: Optional[Dict[str, str]] = None, + use_worker: bool = True, + ) -> Dict[str, Any]: + """Upload a file to R2""" + + bucket_name = self._get_bucket_name(bucket_type) + + try: + if use_worker: + # Use worker endpoint for better performance + form_data = { + "file": file_data, + "bucket": bucket_type, + "key": key, + "contentType": content_type, + } + + if metadata: + form_data["metadata"] = metadata + + response = await self.client.post( + "api/files", data=form_data, use_worker=True + ) + else: + # Use R2 API directly + headers = {"Content-Type": content_type} + + if metadata: + for k, v in metadata.items(): + headers[f"x-amz-meta-{k}"] = v + + response = await self.client.upload_file( + f"{self.base_endpoint}/{bucket_name}/objects/{key}", + file_data, + content_type, + headers, + ) + + return { + "success": True, + "key": key, + "bucket": bucket_type, + "bucket_name": bucket_name, + "size": len(file_data), + "content_type": content_type, + "url": f"/{bucket_type}/{key}", + **response, + } + + except CloudflareError as e: + logger.error(f"R2 upload failed: {e}") + raise + + async def upload_file_stream( + self, + key: str, + file_stream: BinaryIO, + content_type: str = "application/octet-stream", + bucket_type: str = "storage", + metadata: Optional[Dict[str, str]] = None, + ) -> Dict[str, Any]: + """Upload a file from stream""" + + file_data = file_stream.read() + return await self.upload_file( + key, file_data, content_type, bucket_type, metadata + ) + + async def get_file( + self, key: str, bucket_type: str = "storage", use_worker: bool = True + ) -> Optional[Dict[str, Any]]: + """Get a file from R2""" + + bucket_name = self._get_bucket_name(bucket_type) + + try: + if use_worker: + response = await self.client.get( + f"api/files/{key}?bucket={bucket_type}", use_worker=True + ) + + if response: + return { + "key": key, + "bucket": bucket_type, + "bucket_name": bucket_name, + "data": response, # Binary data would be handled by worker + "exists": True, + } + else: + response = await self.client.get( + f"{self.base_endpoint}/{bucket_name}/objects/{key}" + ) + + return { + "key": key, + "bucket": bucket_type, + "bucket_name": bucket_name, + "data": response, + "exists": True, + } + + except CloudflareError as e: + if e.status_code == 404: + return None + logger.error(f"R2 get file failed: {e}") + raise + + return None + + async def delete_file( + self, key: str, bucket_type: str = "storage", use_worker: bool = True + ) -> Dict[str, Any]: + """Delete a file from R2""" + + bucket_name = self._get_bucket_name(bucket_type) + + try: + if use_worker: + response = await self.client.delete( + f"api/files/{key}?bucket={bucket_type}", use_worker=True + ) + else: + response = await self.client.delete( + f"{self.base_endpoint}/{bucket_name}/objects/{key}" + ) + + return { + "success": True, + "key": key, + "bucket": bucket_type, + "bucket_name": bucket_name, + **response, + } + + except CloudflareError as e: + logger.error(f"R2 delete failed: {e}") + raise + + async def list_files( + self, + bucket_type: str = "storage", + prefix: str = "", + limit: int = 1000, + use_worker: bool = True, + ) -> Dict[str, Any]: + """List files in R2 bucket""" + + bucket_name = self._get_bucket_name(bucket_type) + + try: + if use_worker: + params = {"bucket": bucket_type, "prefix": prefix, "limit": limit} + + query_string = "&".join([f"{k}={v}" for k, v in params.items() if v]) + response = await self.client.get( + f"api/files/list?{query_string}", use_worker=True + ) + else: + params = {"prefix": prefix, "max-keys": limit} + + query_string = "&".join([f"{k}={v}" for k, v in params.items() if v]) + response = await self.client.get( + f"{self.base_endpoint}/{bucket_name}/objects?{query_string}" + ) + + return { + "bucket": bucket_type, + "bucket_name": bucket_name, + "prefix": prefix, + "files": response.get("objects", []), + "truncated": response.get("truncated", False), + **response, + } + + except CloudflareError as e: + logger.error(f"R2 list files failed: {e}") + raise + + async def get_file_metadata( + self, key: str, bucket_type: str = "storage", use_worker: bool = True + ) -> Optional[Dict[str, Any]]: + """Get file metadata without downloading content""" + + bucket_name = self._get_bucket_name(bucket_type) + + try: + if use_worker: + response = await self.client.get( + f"api/files/{key}/metadata?bucket={bucket_type}", use_worker=True + ) + else: + # Use HEAD request to get metadata only + response = await self.client.get( + f"{self.base_endpoint}/{bucket_name}/objects/{key}", + headers={"Range": "bytes=0-0"}, # Minimal range to get headers + ) + + if response: + return { + "key": key, + "bucket": bucket_type, + "bucket_name": bucket_name, + **response, + } + + except CloudflareError as e: + if e.status_code == 404: + return None + logger.error(f"R2 get metadata failed: {e}") + raise + + return None + + async def copy_file( + self, + source_key: str, + destination_key: str, + source_bucket: str = "storage", + destination_bucket: str = "storage", + use_worker: bool = True, + ) -> Dict[str, Any]: + """Copy a file within R2 or between buckets""" + + try: + if use_worker: + copy_data = { + "sourceKey": source_key, + "destinationKey": destination_key, + "sourceBucket": source_bucket, + "destinationBucket": destination_bucket, + } + + response = await self.client.post( + "api/files/copy", data=copy_data, use_worker=True + ) + else: + # Get source file first + source_file = await self.get_file(source_key, source_bucket, False) + + if not source_file: + raise CloudflareError(f"Source file {source_key} not found") + + # Upload to destination + response = await self.upload_file( + destination_key, + source_file["data"], + bucket_type=destination_bucket, + use_worker=False, + ) + + return { + "success": True, + "source_key": source_key, + "destination_key": destination_key, + "source_bucket": source_bucket, + "destination_bucket": destination_bucket, + **response, + } + + except CloudflareError as e: + logger.error(f"R2 copy failed: {e}") + raise + + async def move_file( + self, + source_key: str, + destination_key: str, + source_bucket: str = "storage", + destination_bucket: str = "storage", + use_worker: bool = True, + ) -> Dict[str, Any]: + """Move a file (copy then delete)""" + + try: + # Copy file first + copy_result = await self.copy_file( + source_key, + destination_key, + source_bucket, + destination_bucket, + use_worker, + ) + + # Delete source file + delete_result = await self.delete_file( + source_key, source_bucket, use_worker + ) + + return { + "success": True, + "source_key": source_key, + "destination_key": destination_key, + "source_bucket": source_bucket, + "destination_bucket": destination_bucket, + "copy_result": copy_result, + "delete_result": delete_result, + } + + except CloudflareError as e: + logger.error(f"R2 move failed: {e}") + raise + + async def generate_presigned_url( + self, + key: str, + bucket_type: str = "storage", + expires_in: int = 3600, + method: str = "GET", + ) -> Dict[str, Any]: + """Generate a presigned URL for direct access""" + + # Note: This would typically require additional R2 configuration + # For now, return a worker endpoint URL + + try: + url_data = { + "key": key, + "bucket": bucket_type, + "expiresIn": expires_in, + "method": method, + } + + response = await self.client.post( + "api/files/presigned-url", data=url_data, use_worker=True + ) + + return { + "success": True, + "key": key, + "bucket": bucket_type, + "method": method, + "expires_in": expires_in, + **response, + } + + except CloudflareError as e: + logger.error(f"R2 presigned URL generation failed: {e}") + raise + + async def get_storage_stats(self, use_worker: bool = True) -> Dict[str, Any]: + """Get storage statistics""" + + try: + if use_worker: + response = await self.client.get("api/files/stats", use_worker=True) + else: + # Get stats for both buckets + storage_list = await self.list_files("storage", use_worker=False) + assets_list = await self.list_files("assets", use_worker=False) + + storage_size = sum( + file.get("size", 0) for file in storage_list.get("files", []) + ) + assets_size = sum( + file.get("size", 0) for file in assets_list.get("files", []) + ) + + response = { + "storage": { + "file_count": len(storage_list.get("files", [])), + "total_size": storage_size, + }, + "assets": { + "file_count": len(assets_list.get("files", [])), + "total_size": assets_size, + }, + "total": { + "file_count": len(storage_list.get("files", [])) + + len(assets_list.get("files", [])), + "total_size": storage_size + assets_size, + }, + } + + return response + + except CloudflareError as e: + logger.error(f"R2 storage stats failed: {e}") + raise + + def create_file_stream(self, data: bytes) -> io.BytesIO: + """Create a file stream from bytes""" + return io.BytesIO(data) + + def get_public_url(self, key: str, bucket_type: str = "storage") -> str: + """Get public URL for a file (if bucket is configured for public access)""" + bucket_name = self._get_bucket_name(bucket_type) + + # This would depend on your R2 custom domain configuration + # For now, return the worker endpoint + if self.client.worker_url: + return f"{self.client.worker_url}/api/files/{key}?bucket={bucket_type}" + + # Default R2 URL format (requires public access configuration) + return f"https://pub-{bucket_name}.r2.dev/{key}" diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000000000000000000000000000000000000..a881e2a5e7a2d541f6839868ccf1ee6d5c676f29 --- /dev/null +++ b/app/config.py @@ -0,0 +1,372 @@ +import json +import threading +import tomllib +from pathlib import Path +from typing import Dict, List, Optional + +from pydantic import BaseModel, Field + + +def get_project_root() -> Path: + """Get the project root directory""" + return Path(__file__).resolve().parent.parent + + +PROJECT_ROOT = get_project_root() +WORKSPACE_ROOT = PROJECT_ROOT / "workspace" + + +class LLMSettings(BaseModel): + model: str = Field(..., description="Model name") + base_url: str = Field(..., description="API base URL") + api_key: str = Field(..., description="API key") + max_tokens: int = Field(4096, description="Maximum number of tokens per request") + max_input_tokens: Optional[int] = Field( + None, + description="Maximum input tokens to use across all requests (None for unlimited)", + ) + temperature: float = Field(1.0, description="Sampling temperature") + api_type: str = Field(..., description="Azure, Openai, or Ollama") + api_version: str = Field(..., description="Azure Openai version if AzureOpenai") + + +class ProxySettings(BaseModel): + server: str = Field(None, description="Proxy server address") + username: Optional[str] = Field(None, description="Proxy username") + password: Optional[str] = Field(None, description="Proxy password") + + +class SearchSettings(BaseModel): + engine: str = Field(default="Google", description="Search engine the llm to use") + fallback_engines: List[str] = Field( + default_factory=lambda: ["DuckDuckGo", "Baidu", "Bing"], + description="Fallback search engines to try if the primary engine fails", + ) + retry_delay: int = Field( + default=60, + description="Seconds to wait before retrying all engines again after they all fail", + ) + max_retries: int = Field( + default=3, + description="Maximum number of times to retry all engines when all fail", + ) + lang: str = Field( + default="en", + description="Language code for search results (e.g., en, zh, fr)", + ) + country: str = Field( + default="us", + description="Country code for search results (e.g., us, cn, uk)", + ) + + +class RunflowSettings(BaseModel): + use_data_analysis_agent: bool = Field( + default=False, description="Enable data analysis agent in run flow" + ) + + +class BrowserSettings(BaseModel): + headless: bool = Field(False, description="Whether to run browser in headless mode") + disable_security: bool = Field( + True, description="Disable browser security features" + ) + extra_chromium_args: List[str] = Field( + default_factory=list, description="Extra arguments to pass to the browser" + ) + chrome_instance_path: Optional[str] = Field( + None, description="Path to a Chrome instance to use" + ) + wss_url: Optional[str] = Field( + None, description="Connect to a browser instance via WebSocket" + ) + cdp_url: Optional[str] = Field( + None, description="Connect to a browser instance via CDP" + ) + proxy: Optional[ProxySettings] = Field( + None, description="Proxy settings for the browser" + ) + max_content_length: int = Field( + 2000, description="Maximum length for content retrieval operations" + ) + + +class SandboxSettings(BaseModel): + """Configuration for the execution sandbox""" + + use_sandbox: bool = Field(False, description="Whether to use the sandbox") + image: str = Field("python:3.12-slim", description="Base image") + work_dir: str = Field("/workspace", description="Container working directory") + memory_limit: str = Field("512m", description="Memory limit") + cpu_limit: float = Field(1.0, description="CPU limit") + timeout: int = Field(300, description="Default command timeout (seconds)") + network_enabled: bool = Field( + False, description="Whether network access is allowed" + ) + + +class DaytonaSettings(BaseModel): + daytona_api_key: str + daytona_server_url: Optional[str] = Field( + "https://app.daytona.io/api", description="" + ) + daytona_target: Optional[str] = Field("us", description="enum ['eu', 'us']") + sandbox_image_name: Optional[str] = Field("whitezxj/sandbox:0.1.0", description="") + sandbox_entrypoint: Optional[str] = Field( + "/usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf", + description="", + ) + # sandbox_id: Optional[str] = Field( + # None, description="ID of the daytona sandbox to use, if any" + # ) + VNC_password: Optional[str] = Field( + "123456", description="VNC password for the vnc service in sandbox" + ) + + +class MCPServerConfig(BaseModel): + """Configuration for a single MCP server""" + + type: str = Field(..., description="Server connection type (sse or stdio)") + url: Optional[str] = Field(None, description="Server URL for SSE connections") + command: Optional[str] = Field(None, description="Command for stdio connections") + args: List[str] = Field( + default_factory=list, description="Arguments for stdio command" + ) + + +class MCPSettings(BaseModel): + """Configuration for MCP (Model Context Protocol)""" + + server_reference: str = Field( + "app.mcp.server", description="Module reference for the MCP server" + ) + servers: Dict[str, MCPServerConfig] = Field( + default_factory=dict, description="MCP server configurations" + ) + + @classmethod + def load_server_config(cls) -> Dict[str, MCPServerConfig]: + """Load MCP server configuration from JSON file""" + config_path = PROJECT_ROOT / "config" / "mcp.json" + + try: + config_file = config_path if config_path.exists() else None + if not config_file: + return {} + + with config_file.open() as f: + data = json.load(f) + servers = {} + + for server_id, server_config in data.get("mcpServers", {}).items(): + servers[server_id] = MCPServerConfig( + type=server_config["type"], + url=server_config.get("url"), + command=server_config.get("command"), + args=server_config.get("args", []), + ) + return servers + except Exception as e: + raise ValueError(f"Failed to load MCP server config: {e}") + + +class AppConfig(BaseModel): + llm: Dict[str, LLMSettings] + sandbox: Optional[SandboxSettings] = Field( + None, description="Sandbox configuration" + ) + browser_config: Optional[BrowserSettings] = Field( + None, description="Browser configuration" + ) + search_config: Optional[SearchSettings] = Field( + None, description="Search configuration" + ) + mcp_config: Optional[MCPSettings] = Field(None, description="MCP configuration") + run_flow_config: Optional[RunflowSettings] = Field( + None, description="Run flow configuration" + ) + daytona_config: Optional[DaytonaSettings] = Field( + None, description="Daytona configuration" + ) + + class Config: + arbitrary_types_allowed = True + + +class Config: + _instance = None + _lock = threading.Lock() + _initialized = False + + def __new__(cls): + if cls._instance is None: + with cls._lock: + if cls._instance is None: + cls._instance = super().__new__(cls) + return cls._instance + + def __init__(self): + if not self._initialized: + with self._lock: + if not self._initialized: + self._config = None + self._load_initial_config() + self._initialized = True + + @staticmethod + def _get_config_path() -> Path: + root = PROJECT_ROOT + config_path = root / "config" / "config.toml" + if config_path.exists(): + return config_path + example_path = root / "config" / "config.example.toml" + if example_path.exists(): + return example_path + raise FileNotFoundError("No configuration file found in config directory") + + def _load_config(self) -> dict: + config_path = self._get_config_path() + with config_path.open("rb") as f: + return tomllib.load(f) + + def _load_initial_config(self): + raw_config = self._load_config() + base_llm = raw_config.get("llm", {}) + llm_overrides = { + k: v for k, v in raw_config.get("llm", {}).items() if isinstance(v, dict) + } + + default_settings = { + "model": base_llm.get("model"), + "base_url": base_llm.get("base_url"), + "api_key": base_llm.get("api_key"), + "max_tokens": base_llm.get("max_tokens", 4096), + "max_input_tokens": base_llm.get("max_input_tokens"), + "temperature": base_llm.get("temperature", 1.0), + "api_type": base_llm.get("api_type", ""), + "api_version": base_llm.get("api_version", ""), + } + + # handle browser config. + browser_config = raw_config.get("browser", {}) + browser_settings = None + + if browser_config: + # handle proxy settings. + proxy_config = browser_config.get("proxy", {}) + proxy_settings = None + + if proxy_config and proxy_config.get("server"): + proxy_settings = ProxySettings( + **{ + k: v + for k, v in proxy_config.items() + if k in ["server", "username", "password"] and v + } + ) + + # filter valid browser config parameters. + valid_browser_params = { + k: v + for k, v in browser_config.items() + if k in BrowserSettings.__annotations__ and v is not None + } + + # if there is proxy settings, add it to the parameters. + if proxy_settings: + valid_browser_params["proxy"] = proxy_settings + + # only create BrowserSettings when there are valid parameters. + if valid_browser_params: + browser_settings = BrowserSettings(**valid_browser_params) + + search_config = raw_config.get("search", {}) + search_settings = None + if search_config: + search_settings = SearchSettings(**search_config) + sandbox_config = raw_config.get("sandbox", {}) + if sandbox_config: + sandbox_settings = SandboxSettings(**sandbox_config) + else: + sandbox_settings = SandboxSettings() + daytona_config = raw_config.get("daytona", {}) + if daytona_config: + daytona_settings = DaytonaSettings(**daytona_config) + else: + daytona_settings = DaytonaSettings() + + mcp_config = raw_config.get("mcp", {}) + mcp_settings = None + if mcp_config: + # Load server configurations from JSON + mcp_config["servers"] = MCPSettings.load_server_config() + mcp_settings = MCPSettings(**mcp_config) + else: + mcp_settings = MCPSettings(servers=MCPSettings.load_server_config()) + + run_flow_config = raw_config.get("runflow") + if run_flow_config: + run_flow_settings = RunflowSettings(**run_flow_config) + else: + run_flow_settings = RunflowSettings() + config_dict = { + "llm": { + "default": default_settings, + **{ + name: {**default_settings, **override_config} + for name, override_config in llm_overrides.items() + }, + }, + "sandbox": sandbox_settings, + "browser_config": browser_settings, + "search_config": search_settings, + "mcp_config": mcp_settings, + "run_flow_config": run_flow_settings, + "daytona_config": daytona_settings, + } + + self._config = AppConfig(**config_dict) + + @property + def llm(self) -> Dict[str, LLMSettings]: + return self._config.llm + + @property + def sandbox(self) -> SandboxSettings: + return self._config.sandbox + + @property + def daytona(self) -> DaytonaSettings: + return self._config.daytona_config + + @property + def browser_config(self) -> Optional[BrowserSettings]: + return self._config.browser_config + + @property + def search_config(self) -> Optional[SearchSettings]: + return self._config.search_config + + @property + def mcp_config(self) -> MCPSettings: + """Get the MCP configuration""" + return self._config.mcp_config + + @property + def run_flow_config(self) -> RunflowSettings: + """Get the Run Flow configuration""" + return self._config.run_flow_config + + @property + def workspace_root(self) -> Path: + """Get the workspace root directory""" + return WORKSPACE_ROOT + + @property + def root_path(self) -> Path: + """Get the root path of the application""" + return PROJECT_ROOT + + +config = Config() diff --git a/app/config_cloudflare.py b/app/config_cloudflare.py new file mode 100644 index 0000000000000000000000000000000000000000..ba9a556435f61b6fc48340d464294c42de80c359 --- /dev/null +++ b/app/config_cloudflare.py @@ -0,0 +1,145 @@ +""" +Configuration extensions for Cloudflare integration +""" + +import os +from typing import Optional + +from pydantic import BaseModel, Field + + +class CloudflareSettings(BaseModel): + """Cloudflare configuration settings""" + + api_token: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_API_TOKEN"), + description="Cloudflare API token", + ) + + account_id: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_ACCOUNT_ID"), + description="Cloudflare account ID", + ) + + worker_url: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_WORKER_URL"), + description="Cloudflare Worker URL", + ) + + # D1 Database settings + d1_database_id: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_D1_DATABASE_ID"), + description="D1 database ID", + ) + + # KV Namespace settings + kv_sessions_id: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_KV_SESSIONS_ID"), + description="KV namespace ID for sessions", + ) + + kv_cache_id: Optional[str] = Field( + default_factory=lambda: os.getenv("CLOUDFLARE_KV_CACHE_ID"), + description="KV namespace ID for cache", + ) + + # R2 Bucket settings + r2_storage_bucket: str = Field( + default_factory=lambda: os.getenv( + "CLOUDFLARE_R2_STORAGE_BUCKET", "openmanus-storage" + ), + description="R2 storage bucket name", + ) + + r2_assets_bucket: str = Field( + default_factory=lambda: os.getenv( + "CLOUDFLARE_R2_ASSETS_BUCKET", "openmanus-assets" + ), + description="R2 assets bucket name", + ) + + # Connection settings + timeout: int = Field(default=30, description="Request timeout in seconds") + + def is_configured(self) -> bool: + """Check if minimum Cloudflare configuration is available""" + return bool(self.api_token and self.account_id) + + def has_worker(self) -> bool: + """Check if worker URL is configured""" + return bool(self.worker_url) + + def has_d1(self) -> bool: + """Check if D1 database is configured""" + return bool(self.d1_database_id) + + def has_kv(self) -> bool: + """Check if KV namespaces are configured""" + return bool(self.kv_sessions_id and self.kv_cache_id) + + +class HuggingFaceSettings(BaseModel): + """Hugging Face configuration settings""" + + token: Optional[str] = Field( + default_factory=lambda: os.getenv("HUGGINGFACE_TOKEN"), + description="Hugging Face API token", + ) + + cache_dir: str = Field( + default_factory=lambda: os.getenv( + "HF_HOME", "/app/OpenManus/.cache/huggingface" + ), + description="Hugging Face cache directory", + ) + + model_cache_size: int = Field( + default=5, description="Maximum number of models to cache" + ) + + +class DeploymentSettings(BaseModel): + """Deployment-specific settings""" + + environment: str = Field( + default_factory=lambda: os.getenv("ENVIRONMENT", "development"), + description="Deployment environment", + ) + + debug: bool = Field( + default_factory=lambda: os.getenv("DEBUG", "false").lower() == "true", + description="Enable debug mode", + ) + + log_level: str = Field( + default_factory=lambda: os.getenv("LOG_LEVEL", "INFO"), + description="Logging level", + ) + + # Gradio settings + server_name: str = Field( + default_factory=lambda: os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"), + description="Gradio server name", + ) + + server_port: int = Field( + default_factory=lambda: int(os.getenv("GRADIO_SERVER_PORT", "7860")), + description="Gradio server port", + ) + + # Security settings + secret_key: Optional[str] = Field( + default_factory=lambda: os.getenv("SECRET_KEY"), + description="Secret key for sessions", + ) + + jwt_secret: Optional[str] = Field( + default_factory=lambda: os.getenv("JWT_SECRET"), + description="JWT signing secret", + ) + + +# Create global instances +cloudflare_config = CloudflareSettings() +huggingface_config = HuggingFaceSettings() +deployment_config = DeploymentSettings() diff --git a/app/daytona/README.md b/app/daytona/README.md new file mode 100644 index 0000000000000000000000000000000000000000..6f802061802faafda48a8288a85047fc4327f0c7 --- /dev/null +++ b/app/daytona/README.md @@ -0,0 +1,57 @@ +# Agent with Daytona sandbox + + + + +## Prerequisites +- conda activate 'Your OpenManus python env' +- pip install daytona==0.21.8 structlog==25.4.0 + + + +## Setup & Running + +1. daytona config : + ```bash + cd OpenManus + cp config/config.example-daytona.toml config/config.toml + ``` +2. get daytona apikey : + goto https://app.daytona.io/dashboard/keys and create your apikey + +3. set your apikey in config.toml + ```toml + # daytona config + [daytona] + daytona_api_key = "" + #daytona_server_url = "https://app.daytona.io/api" + #daytona_target = "us" #Daytona is currently available in the following regions:United States (us)、Europe (eu) + #sandbox_image_name = "whitezxj/sandbox:0.1.0" #If you don't use this default image,sandbox tools may be useless + #sandbox_entrypoint = "/usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf" #If you change this entrypoint,server in sandbox may be useless + #VNC_password = #The password you set to log in sandbox by VNC,it will be 123456 if you don't set + ``` +2. Run : + + ```bash + cd OpenManus + python sandbox_main.py + ``` + +3. Send tasks to Agent + You can sent tasks to Agent by terminate,agent will use sandbox tools to handle your tasks. + +4. See results + If agent use sb_browser_use tool, you can see the operations by VNC link, The VNC link will print in the termination,e.g.:https://6080-sandbox-123456.h7890.daytona.work. + If agent use sb_shell tool, you can see the results by terminate of sandbox in https://app.daytona.io/dashboard/sandboxes. + Agent can use sb_files tool to operate files to sandbox. + + +## Example + + You can send task e.g.:"帮我在https://hk.trip.com/travel-guide/guidebook/nanjing-9696/?ishideheader=true&isHideNavBar=YES&disableFontScaling=1&catalogId=514634&locale=zh-HK查询相关信息上制定一份南京旅游攻略,并在工作区保存为index.html" + + Then you can see the agent's browser action in VNC link(https://6080-sandbox-123456.h7890.proxy.daytona.work) and you can see the html made by agent in Website URL(https://8080-sandbox-123456.h7890.proxy.daytona.work). + +## Learn More + +- [Daytona Documentation](https://www.daytona.io/docs/) diff --git a/app/daytona/sandbox.py b/app/daytona/sandbox.py new file mode 100644 index 0000000000000000000000000000000000000000..8970b9cef21e8a9c126bafc33f45d38fc93a5f5d --- /dev/null +++ b/app/daytona/sandbox.py @@ -0,0 +1,165 @@ +import time + +from daytona import ( + CreateSandboxFromImageParams, + Daytona, + DaytonaConfig, + Resources, + Sandbox, + SandboxState, + SessionExecuteRequest, +) + +from app.config import config +from app.utils.logger import logger + + +# load_dotenv() +daytona_settings = config.daytona +logger.info("Initializing Daytona sandbox configuration") +daytona_config = DaytonaConfig( + api_key=daytona_settings.daytona_api_key, + server_url=daytona_settings.daytona_server_url, + target=daytona_settings.daytona_target, +) + +if daytona_config.api_key: + logger.info("Daytona API key configured successfully") +else: + logger.warning("No Daytona API key found in environment variables") + +if daytona_config.server_url: + logger.info(f"Daytona server URL set to: {daytona_config.server_url}") +else: + logger.warning("No Daytona server URL found in environment variables") + +if daytona_config.target: + logger.info(f"Daytona target set to: {daytona_config.target}") +else: + logger.warning("No Daytona target found in environment variables") + +daytona = Daytona(daytona_config) +logger.info("Daytona client initialized") + + +async def get_or_start_sandbox(sandbox_id: str): + """Retrieve a sandbox by ID, check its state, and start it if needed.""" + + logger.info(f"Getting or starting sandbox with ID: {sandbox_id}") + + try: + sandbox = daytona.get(sandbox_id) + + # Check if sandbox needs to be started + if ( + sandbox.state == SandboxState.ARCHIVED + or sandbox.state == SandboxState.STOPPED + ): + logger.info(f"Sandbox is in {sandbox.state} state. Starting...") + try: + daytona.start(sandbox) + # Wait a moment for the sandbox to initialize + # sleep(5) + # Refresh sandbox state after starting + sandbox = daytona.get(sandbox_id) + + # Start supervisord in a session when restarting + start_supervisord_session(sandbox) + except Exception as e: + logger.error(f"Error starting sandbox: {e}") + raise e + + logger.info(f"Sandbox {sandbox_id} is ready") + return sandbox + + except Exception as e: + logger.error(f"Error retrieving or starting sandbox: {str(e)}") + raise e + + +def start_supervisord_session(sandbox: Sandbox): + """Start supervisord in a session.""" + session_id = "supervisord-session" + try: + logger.info(f"Creating session {session_id} for supervisord") + sandbox.process.create_session(session_id) + + # Execute supervisord command + sandbox.process.execute_session_command( + session_id, + SessionExecuteRequest( + command="exec /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf", + var_async=True, + ), + ) + time.sleep(25) # Wait a bit to ensure supervisord starts properly + logger.info(f"Supervisord started in session {session_id}") + except Exception as e: + logger.error(f"Error starting supervisord session: {str(e)}") + raise e + + +def create_sandbox(password: str, project_id: str = None): + """Create a new sandbox with all required services configured and running.""" + + logger.info("Creating new Daytona sandbox environment") + logger.info("Configuring sandbox with browser-use image and environment variables") + + labels = None + if project_id: + logger.info(f"Using sandbox_id as label: {project_id}") + labels = {"id": project_id} + + params = CreateSandboxFromImageParams( + image=daytona_settings.sandbox_image_name, + public=True, + labels=labels, + env_vars={ + "CHROME_PERSISTENT_SESSION": "true", + "RESOLUTION": "1024x768x24", + "RESOLUTION_WIDTH": "1024", + "RESOLUTION_HEIGHT": "768", + "VNC_PASSWORD": password, + "ANONYMIZED_TELEMETRY": "false", + "CHROME_PATH": "", + "CHROME_USER_DATA": "", + "CHROME_DEBUGGING_PORT": "9222", + "CHROME_DEBUGGING_HOST": "localhost", + "CHROME_CDP": "", + }, + resources=Resources( + cpu=2, + memory=4, + disk=5, + ), + auto_stop_interval=15, + auto_archive_interval=24 * 60, + ) + + # Create the sandbox + sandbox = daytona.create(params) + logger.info(f"Sandbox created with ID: {sandbox.id}") + + # Start supervisord in a session for new sandbox + start_supervisord_session(sandbox) + + logger.info(f"Sandbox environment successfully initialized") + return sandbox + + +async def delete_sandbox(sandbox_id: str): + """Delete a sandbox by its ID.""" + logger.info(f"Deleting sandbox with ID: {sandbox_id}") + + try: + # Get the sandbox + sandbox = daytona.get(sandbox_id) + + # Delete the sandbox + daytona.delete(sandbox) + + logger.info(f"Successfully deleted sandbox {sandbox_id}") + return True + except Exception as e: + logger.error(f"Error deleting sandbox {sandbox_id}: {str(e)}") + raise e diff --git a/app/daytona/tool_base.py b/app/daytona/tool_base.py new file mode 100644 index 0000000000000000000000000000000000000000..043578a9a06f50460cd29611d5ea738b5f948aea --- /dev/null +++ b/app/daytona/tool_base.py @@ -0,0 +1,138 @@ +from dataclasses import dataclass, field +from datetime import datetime +from typing import Any, ClassVar, Dict, Optional + +from daytona import Daytona, DaytonaConfig, Sandbox, SandboxState +from pydantic import Field + +from app.config import config +from app.daytona.sandbox import create_sandbox, start_supervisord_session +from app.tool.base import BaseTool +from app.utils.files_utils import clean_path +from app.utils.logger import logger + + +# load_dotenv() +daytona_settings = config.daytona +daytona_config = DaytonaConfig( + api_key=daytona_settings.daytona_api_key, + server_url=daytona_settings.daytona_server_url, + target=daytona_settings.daytona_target, +) +daytona = Daytona(daytona_config) + + +@dataclass +class ThreadMessage: + """ + Represents a message to be added to a thread. + """ + + type: str + content: Dict[str, Any] + is_llm_message: bool = False + metadata: Optional[Dict[str, Any]] = None + timestamp: Optional[float] = field( + default_factory=lambda: datetime.now().timestamp() + ) + + def to_dict(self) -> Dict[str, Any]: + """Convert the message to a dictionary for API calls""" + return { + "type": self.type, + "content": self.content, + "is_llm_message": self.is_llm_message, + "metadata": self.metadata or {}, + "timestamp": self.timestamp, + } + + +class SandboxToolsBase(BaseTool): + """Base class for all sandbox tools that provides project-based sandbox access.""" + + # Class variable to track if sandbox URLs have been printed + _urls_printed: ClassVar[bool] = False + + # Required fields + project_id: Optional[str] = None + # thread_manager: Optional[ThreadManager] = None + + # Private fields (not part of the model schema) + _sandbox: Optional[Sandbox] = None + _sandbox_id: Optional[str] = None + _sandbox_pass: Optional[str] = None + workspace_path: str = Field(default="/workspace", exclude=True) + _sessions: dict[str, str] = {} + + class Config: + arbitrary_types_allowed = True # Allow non-pydantic types like ThreadManager + underscore_attrs_are_private = True + + async def _ensure_sandbox(self) -> Sandbox: + """Ensure we have a valid sandbox instance, retrieving it from the project if needed.""" + if self._sandbox is None: + # Get or start the sandbox + try: + self._sandbox = create_sandbox(password=config.daytona.VNC_password) + # Log URLs if not already printed + if not SandboxToolsBase._urls_printed: + vnc_link = self._sandbox.get_preview_link(6080) + website_link = self._sandbox.get_preview_link(8080) + + vnc_url = ( + vnc_link.url if hasattr(vnc_link, "url") else str(vnc_link) + ) + website_url = ( + website_link.url + if hasattr(website_link, "url") + else str(website_link) + ) + + print("\033[95m***") + print(f"VNC URL: {vnc_url}") + print(f"Website URL: {website_url}") + print("***\033[0m") + SandboxToolsBase._urls_printed = True + except Exception as e: + logger.error(f"Error retrieving or starting sandbox: {str(e)}") + raise e + else: + if ( + self._sandbox.state == SandboxState.ARCHIVED + or self._sandbox.state == SandboxState.STOPPED + ): + logger.info(f"Sandbox is in {self._sandbox.state} state. Starting...") + try: + daytona.start(self._sandbox) + # Wait a moment for the sandbox to initialize + # sleep(5) + # Refresh sandbox state after starting + + # Start supervisord in a session when restarting + start_supervisord_session(self._sandbox) + except Exception as e: + logger.error(f"Error starting sandbox: {e}") + raise e + return self._sandbox + + @property + def sandbox(self) -> Sandbox: + """Get the sandbox instance, ensuring it exists.""" + if self._sandbox is None: + raise RuntimeError("Sandbox not initialized. Call _ensure_sandbox() first.") + return self._sandbox + + @property + def sandbox_id(self) -> str: + """Get the sandbox ID, ensuring it exists.""" + if self._sandbox_id is None: + raise RuntimeError( + "Sandbox ID not initialized. Call _ensure_sandbox() first." + ) + return self._sandbox_id + + def clean_path(self, path: str) -> str: + """Clean and normalize a path to be relative to /workspace.""" + cleaned_path = clean_path(path, self.workspace_path) + logger.debug(f"Cleaned path: {path} -> {cleaned_path}") + return cleaned_path diff --git a/app/exceptions.py b/app/exceptions.py new file mode 100644 index 0000000000000000000000000000000000000000..fc900874dfbea532cf9077ad74fac15defa9d76a --- /dev/null +++ b/app/exceptions.py @@ -0,0 +1,13 @@ +class ToolError(Exception): + """Raised when a tool encounters an error.""" + + def __init__(self, message): + self.message = message + + +class OpenManusError(Exception): + """Base exception for all OpenManus errors""" + + +class TokenLimitExceeded(OpenManusError): + """Exception raised when the token limit is exceeded""" diff --git a/app/flow/__init__.py b/app/flow/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/app/flow/base.py b/app/flow/base.py new file mode 100644 index 0000000000000000000000000000000000000000..dc57b39962877fb2aee2ce7dc46ce79ded472f70 --- /dev/null +++ b/app/flow/base.py @@ -0,0 +1,57 @@ +from abc import ABC, abstractmethod +from typing import Dict, List, Optional, Union + +from pydantic import BaseModel + +from app.agent.base import BaseAgent + + +class BaseFlow(BaseModel, ABC): + """Base class for execution flows supporting multiple agents""" + + agents: Dict[str, BaseAgent] + tools: Optional[List] = None + primary_agent_key: Optional[str] = None + + class Config: + arbitrary_types_allowed = True + + def __init__( + self, agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]], **data + ): + # Handle different ways of providing agents + if isinstance(agents, BaseAgent): + agents_dict = {"default": agents} + elif isinstance(agents, list): + agents_dict = {f"agent_{i}": agent for i, agent in enumerate(agents)} + else: + agents_dict = agents + + # If primary agent not specified, use first agent + primary_key = data.get("primary_agent_key") + if not primary_key and agents_dict: + primary_key = next(iter(agents_dict)) + data["primary_agent_key"] = primary_key + + # Set the agents dictionary + data["agents"] = agents_dict + + # Initialize using BaseModel's init + super().__init__(**data) + + @property + def primary_agent(self) -> Optional[BaseAgent]: + """Get the primary agent for the flow""" + return self.agents.get(self.primary_agent_key) + + def get_agent(self, key: str) -> Optional[BaseAgent]: + """Get a specific agent by key""" + return self.agents.get(key) + + def add_agent(self, key: str, agent: BaseAgent) -> None: + """Add a new agent to the flow""" + self.agents[key] = agent + + @abstractmethod + async def execute(self, input_text: str) -> str: + """Execute the flow with given input""" diff --git a/app/flow/flow_factory.py b/app/flow/flow_factory.py new file mode 100644 index 0000000000000000000000000000000000000000..293694011b4608392966b3f5e2f95ec04f06fced --- /dev/null +++ b/app/flow/flow_factory.py @@ -0,0 +1,30 @@ +from enum import Enum +from typing import Dict, List, Union + +from app.agent.base import BaseAgent +from app.flow.base import BaseFlow +from app.flow.planning import PlanningFlow + + +class FlowType(str, Enum): + PLANNING = "planning" + + +class FlowFactory: + """Factory for creating different types of flows with support for multiple agents""" + + @staticmethod + def create_flow( + flow_type: FlowType, + agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]], + **kwargs, + ) -> BaseFlow: + flows = { + FlowType.PLANNING: PlanningFlow, + } + + flow_class = flows.get(flow_type) + if not flow_class: + raise ValueError(f"Unknown flow type: {flow_type}") + + return flow_class(agents, **kwargs) diff --git a/app/flow/planning.py b/app/flow/planning.py new file mode 100644 index 0000000000000000000000000000000000000000..b60596a889c55a8e12c082403222806843b2b1ef --- /dev/null +++ b/app/flow/planning.py @@ -0,0 +1,442 @@ +import json +import time +from enum import Enum +from typing import Dict, List, Optional, Union + +from pydantic import Field + +from app.agent.base import BaseAgent +from app.flow.base import BaseFlow +from app.llm import LLM +from app.logger import logger +from app.schema import AgentState, Message, ToolChoice +from app.tool import PlanningTool + + +class PlanStepStatus(str, Enum): + """Enum class defining possible statuses of a plan step""" + + NOT_STARTED = "not_started" + IN_PROGRESS = "in_progress" + COMPLETED = "completed" + BLOCKED = "blocked" + + @classmethod + def get_all_statuses(cls) -> list[str]: + """Return a list of all possible step status values""" + return [status.value for status in cls] + + @classmethod + def get_active_statuses(cls) -> list[str]: + """Return a list of values representing active statuses (not started or in progress)""" + return [cls.NOT_STARTED.value, cls.IN_PROGRESS.value] + + @classmethod + def get_status_marks(cls) -> Dict[str, str]: + """Return a mapping of statuses to their marker symbols""" + return { + cls.COMPLETED.value: "[✓]", + cls.IN_PROGRESS.value: "[→]", + cls.BLOCKED.value: "[!]", + cls.NOT_STARTED.value: "[ ]", + } + + +class PlanningFlow(BaseFlow): + """A flow that manages planning and execution of tasks using agents.""" + + llm: LLM = Field(default_factory=lambda: LLM()) + planning_tool: PlanningTool = Field(default_factory=PlanningTool) + executor_keys: List[str] = Field(default_factory=list) + active_plan_id: str = Field(default_factory=lambda: f"plan_{int(time.time())}") + current_step_index: Optional[int] = None + + def __init__( + self, agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]], **data + ): + # Set executor keys before super().__init__ + if "executors" in data: + data["executor_keys"] = data.pop("executors") + + # Set plan ID if provided + if "plan_id" in data: + data["active_plan_id"] = data.pop("plan_id") + + # Initialize the planning tool if not provided + if "planning_tool" not in data: + planning_tool = PlanningTool() + data["planning_tool"] = planning_tool + + # Call parent's init with the processed data + super().__init__(agents, **data) + + # Set executor_keys to all agent keys if not specified + if not self.executor_keys: + self.executor_keys = list(self.agents.keys()) + + def get_executor(self, step_type: Optional[str] = None) -> BaseAgent: + """ + Get an appropriate executor agent for the current step. + Can be extended to select agents based on step type/requirements. + """ + # If step type is provided and matches an agent key, use that agent + if step_type and step_type in self.agents: + return self.agents[step_type] + + # Otherwise use the first available executor or fall back to primary agent + for key in self.executor_keys: + if key in self.agents: + return self.agents[key] + + # Fallback to primary agent + return self.primary_agent + + async def execute(self, input_text: str) -> str: + """Execute the planning flow with agents.""" + try: + if not self.primary_agent: + raise ValueError("No primary agent available") + + # Create initial plan if input provided + if input_text: + await self._create_initial_plan(input_text) + + # Verify plan was created successfully + if self.active_plan_id not in self.planning_tool.plans: + logger.error( + f"Plan creation failed. Plan ID {self.active_plan_id} not found in planning tool." + ) + return f"Failed to create plan for: {input_text}" + + result = "" + while True: + # Get current step to execute + self.current_step_index, step_info = await self._get_current_step_info() + + # Exit if no more steps or plan completed + if self.current_step_index is None: + result += await self._finalize_plan() + break + + # Execute current step with appropriate agent + step_type = step_info.get("type") if step_info else None + executor = self.get_executor(step_type) + step_result = await self._execute_step(executor, step_info) + result += step_result + "\n" + + # Check if agent wants to terminate + if hasattr(executor, "state") and executor.state == AgentState.FINISHED: + break + + return result + except Exception as e: + logger.error(f"Error in PlanningFlow: {str(e)}") + return f"Execution failed: {str(e)}" + + async def _create_initial_plan(self, request: str) -> None: + """Create an initial plan based on the request using the flow's LLM and PlanningTool.""" + logger.info(f"Creating initial plan with ID: {self.active_plan_id}") + + system_message_content = ( + "You are a planning assistant. Create a concise, actionable plan with clear steps. " + "Focus on key milestones rather than detailed sub-steps. " + "Optimize for clarity and efficiency." + ) + agents_description = [] + for key in self.executor_keys: + if key in self.agents: + agents_description.append( + { + "name": key.upper(), + "description": self.agents[key].description, + } + ) + if len(agents_description) > 1: + # Add description of agents to select + system_message_content += ( + f"\nNow we have {agents_description} agents. " + f"The infomation of them are below: {json.dumps(agents_description)}\n" + "When creating steps in the planning tool, please specify the agent names using the format '[agent_name]'." + ) + + # Create a system message for plan creation + system_message = Message.system_message(system_message_content) + + # Create a user message with the request + user_message = Message.user_message( + f"Create a reasonable plan with clear steps to accomplish the task: {request}" + ) + + # Call LLM with PlanningTool + response = await self.llm.ask_tool( + messages=[user_message], + system_msgs=[system_message], + tools=[self.planning_tool.to_param()], + tool_choice=ToolChoice.AUTO, + ) + + # Process tool calls if present + if response.tool_calls: + for tool_call in response.tool_calls: + if tool_call.function.name == "planning": + # Parse the arguments + args = tool_call.function.arguments + if isinstance(args, str): + try: + args = json.loads(args) + except json.JSONDecodeError: + logger.error(f"Failed to parse tool arguments: {args}") + continue + + # Ensure plan_id is set correctly and execute the tool + args["plan_id"] = self.active_plan_id + + # Execute the tool via ToolCollection instead of directly + result = await self.planning_tool.execute(**args) + + logger.info(f"Plan creation result: {str(result)}") + return + + # If execution reached here, create a default plan + logger.warning("Creating default plan") + + # Create default plan using the ToolCollection + await self.planning_tool.execute( + **{ + "command": "create", + "plan_id": self.active_plan_id, + "title": f"Plan for: {request[:50]}{'...' if len(request) > 50 else ''}", + "steps": ["Analyze request", "Execute task", "Verify results"], + } + ) + + async def _get_current_step_info(self) -> tuple[Optional[int], Optional[dict]]: + """ + Parse the current plan to identify the first non-completed step's index and info. + Returns (None, None) if no active step is found. + """ + if ( + not self.active_plan_id + or self.active_plan_id not in self.planning_tool.plans + ): + logger.error(f"Plan with ID {self.active_plan_id} not found") + return None, None + + try: + # Direct access to plan data from planning tool storage + plan_data = self.planning_tool.plans[self.active_plan_id] + steps = plan_data.get("steps", []) + step_statuses = plan_data.get("step_statuses", []) + + # Find first non-completed step + for i, step in enumerate(steps): + if i >= len(step_statuses): + status = PlanStepStatus.NOT_STARTED.value + else: + status = step_statuses[i] + + if status in PlanStepStatus.get_active_statuses(): + # Extract step type/category if available + step_info = {"text": step} + + # Try to extract step type from the text (e.g., [SEARCH] or [CODE]) + import re + + type_match = re.search(r"\[([A-Z_]+)\]", step) + if type_match: + step_info["type"] = type_match.group(1).lower() + + # Mark current step as in_progress + try: + await self.planning_tool.execute( + command="mark_step", + plan_id=self.active_plan_id, + step_index=i, + step_status=PlanStepStatus.IN_PROGRESS.value, + ) + except Exception as e: + logger.warning(f"Error marking step as in_progress: {e}") + # Update step status directly if needed + if i < len(step_statuses): + step_statuses[i] = PlanStepStatus.IN_PROGRESS.value + else: + while len(step_statuses) < i: + step_statuses.append(PlanStepStatus.NOT_STARTED.value) + step_statuses.append(PlanStepStatus.IN_PROGRESS.value) + + plan_data["step_statuses"] = step_statuses + + return i, step_info + + return None, None # No active step found + + except Exception as e: + logger.warning(f"Error finding current step index: {e}") + return None, None + + async def _execute_step(self, executor: BaseAgent, step_info: dict) -> str: + """Execute the current step with the specified agent using agent.run().""" + # Prepare context for the agent with current plan status + plan_status = await self._get_plan_text() + step_text = step_info.get("text", f"Step {self.current_step_index}") + + # Create a prompt for the agent to execute the current step + step_prompt = f""" + CURRENT PLAN STATUS: + {plan_status} + + YOUR CURRENT TASK: + You are now working on step {self.current_step_index}: "{step_text}" + + Please only execute this current step using the appropriate tools. When you're done, provide a summary of what you accomplished. + """ + + # Use agent.run() to execute the step + try: + step_result = await executor.run(step_prompt) + + # Mark the step as completed after successful execution + await self._mark_step_completed() + + return step_result + except Exception as e: + logger.error(f"Error executing step {self.current_step_index}: {e}") + return f"Error executing step {self.current_step_index}: {str(e)}" + + async def _mark_step_completed(self) -> None: + """Mark the current step as completed.""" + if self.current_step_index is None: + return + + try: + # Mark the step as completed + await self.planning_tool.execute( + command="mark_step", + plan_id=self.active_plan_id, + step_index=self.current_step_index, + step_status=PlanStepStatus.COMPLETED.value, + ) + logger.info( + f"Marked step {self.current_step_index} as completed in plan {self.active_plan_id}" + ) + except Exception as e: + logger.warning(f"Failed to update plan status: {e}") + # Update step status directly in planning tool storage + if self.active_plan_id in self.planning_tool.plans: + plan_data = self.planning_tool.plans[self.active_plan_id] + step_statuses = plan_data.get("step_statuses", []) + + # Ensure the step_statuses list is long enough + while len(step_statuses) <= self.current_step_index: + step_statuses.append(PlanStepStatus.NOT_STARTED.value) + + # Update the status + step_statuses[self.current_step_index] = PlanStepStatus.COMPLETED.value + plan_data["step_statuses"] = step_statuses + + async def _get_plan_text(self) -> str: + """Get the current plan as formatted text.""" + try: + result = await self.planning_tool.execute( + command="get", plan_id=self.active_plan_id + ) + return result.output if hasattr(result, "output") else str(result) + except Exception as e: + logger.error(f"Error getting plan: {e}") + return self._generate_plan_text_from_storage() + + def _generate_plan_text_from_storage(self) -> str: + """Generate plan text directly from storage if the planning tool fails.""" + try: + if self.active_plan_id not in self.planning_tool.plans: + return f"Error: Plan with ID {self.active_plan_id} not found" + + plan_data = self.planning_tool.plans[self.active_plan_id] + title = plan_data.get("title", "Untitled Plan") + steps = plan_data.get("steps", []) + step_statuses = plan_data.get("step_statuses", []) + step_notes = plan_data.get("step_notes", []) + + # Ensure step_statuses and step_notes match the number of steps + while len(step_statuses) < len(steps): + step_statuses.append(PlanStepStatus.NOT_STARTED.value) + while len(step_notes) < len(steps): + step_notes.append("") + + # Count steps by status + status_counts = {status: 0 for status in PlanStepStatus.get_all_statuses()} + + for status in step_statuses: + if status in status_counts: + status_counts[status] += 1 + + completed = status_counts[PlanStepStatus.COMPLETED.value] + total = len(steps) + progress = (completed / total) * 100 if total > 0 else 0 + + plan_text = f"Plan: {title} (ID: {self.active_plan_id})\n" + plan_text += "=" * len(plan_text) + "\n\n" + + plan_text += ( + f"Progress: {completed}/{total} steps completed ({progress:.1f}%)\n" + ) + plan_text += f"Status: {status_counts[PlanStepStatus.COMPLETED.value]} completed, {status_counts[PlanStepStatus.IN_PROGRESS.value]} in progress, " + plan_text += f"{status_counts[PlanStepStatus.BLOCKED.value]} blocked, {status_counts[PlanStepStatus.NOT_STARTED.value]} not started\n\n" + plan_text += "Steps:\n" + + status_marks = PlanStepStatus.get_status_marks() + + for i, (step, status, notes) in enumerate( + zip(steps, step_statuses, step_notes) + ): + # Use status marks to indicate step status + status_mark = status_marks.get( + status, status_marks[PlanStepStatus.NOT_STARTED.value] + ) + + plan_text += f"{i}. {status_mark} {step}\n" + if notes: + plan_text += f" Notes: {notes}\n" + + return plan_text + except Exception as e: + logger.error(f"Error generating plan text from storage: {e}") + return f"Error: Unable to retrieve plan with ID {self.active_plan_id}" + + async def _finalize_plan(self) -> str: + """Finalize the plan and provide a summary using the flow's LLM directly.""" + plan_text = await self._get_plan_text() + + # Create a summary using the flow's LLM directly + try: + system_message = Message.system_message( + "You are a planning assistant. Your task is to summarize the completed plan." + ) + + user_message = Message.user_message( + f"The plan has been completed. Here is the final plan status:\n\n{plan_text}\n\nPlease provide a summary of what was accomplished and any final thoughts." + ) + + response = await self.llm.ask( + messages=[user_message], system_msgs=[system_message] + ) + + return f"Plan completed:\n\n{response}" + except Exception as e: + logger.error(f"Error finalizing plan with LLM: {e}") + + # Fallback to using an agent for the summary + try: + agent = self.primary_agent + summary_prompt = f""" + The plan has been completed. Here is the final plan status: + + {plan_text} + + Please provide a summary of what was accomplished and any final thoughts. + """ + summary = await agent.run(summary_prompt) + return f"Plan completed:\n\n{summary}" + except Exception as e2: + logger.error(f"Error finalizing plan with agent: {e2}") + return "Plan completed. Error generating summary." diff --git a/app/huggingface_models.py b/app/huggingface_models.py new file mode 100644 index 0000000000000000000000000000000000000000..2c36e69d1374d176621ba51b4ec753788c197e7d --- /dev/null +++ b/app/huggingface_models.py @@ -0,0 +1,3445 @@ +""" +Hugging Face Models Integration for OpenManus AI Agent +Comprehensive integration with Hugging Face Inference API for all model categories +""" + +import asyncio +import base64 +import io +import json +import logging +from dataclasses import dataclass +from enum import Enum +from typing import Any, Dict, List, Optional, Union + +import aiohttp +import PIL.Image +from pydantic import BaseModel + +logger = logging.getLogger(__name__) + + +class ModelCategory(Enum): + """Categories of Hugging Face models available""" + + # Core AI categories + TEXT_GENERATION = "text-generation" + TEXT_TO_IMAGE = "text-to-image" + IMAGE_TO_TEXT = "image-to-text" + AUTOMATIC_SPEECH_RECOGNITION = "automatic-speech-recognition" + TEXT_TO_SPEECH = "text-to-speech" + IMAGE_CLASSIFICATION = "image-classification" + OBJECT_DETECTION = "object-detection" + FEATURE_EXTRACTION = "feature-extraction" + SENTENCE_SIMILARITY = "sentence-similarity" + TRANSLATION = "translation" + SUMMARIZATION = "summarization" + QUESTION_ANSWERING = "question-answering" + FILL_MASK = "fill-mask" + TOKEN_CLASSIFICATION = "token-classification" + ZERO_SHOT_CLASSIFICATION = "zero-shot-classification" + AUDIO_CLASSIFICATION = "audio-classification" + CONVERSATIONAL = "conversational" + + # Video and Motion + TEXT_TO_VIDEO = "text-to-video" + VIDEO_TO_TEXT = "video-to-text" + VIDEO_CLASSIFICATION = "video-classification" + VIDEO_GENERATION = "video-generation" + MOTION_GENERATION = "motion-generation" + DEEPFAKE_DETECTION = "deepfake-detection" + + # Code and Development + CODE_GENERATION = "code-generation" + CODE_COMPLETION = "code-completion" + CODE_EXPLANATION = "code-explanation" + CODE_TRANSLATION = "code-translation" + CODE_REVIEW = "code-review" + APP_GENERATION = "app-generation" + API_GENERATION = "api-generation" + DATABASE_GENERATION = "database-generation" + + # 3D and AR/VR + TEXT_TO_3D = "text-to-3d" + IMAGE_TO_3D = "image-to-3d" + THREE_D_GENERATION = "3d-generation" + MESH_GENERATION = "mesh-generation" + TEXTURE_GENERATION = "texture-generation" + AR_CONTENT = "ar-content" + VR_ENVIRONMENT = "vr-environment" + + # Document Processing + OCR = "ocr" + DOCUMENT_ANALYSIS = "document-analysis" + PDF_PROCESSING = "pdf-processing" + LAYOUT_ANALYSIS = "layout-analysis" + TABLE_EXTRACTION = "table-extraction" + HANDWRITING_RECOGNITION = "handwriting-recognition" + FORM_PROCESSING = "form-processing" + + # Multimodal AI + VISION_LANGUAGE = "vision-language" + MULTIMODAL_REASONING = "multimodal-reasoning" + CROSS_MODAL_GENERATION = "cross-modal-generation" + VISUAL_QUESTION_ANSWERING = "visual-question-answering" + IMAGE_TEXT_MATCHING = "image-text-matching" + MULTIMODAL_CHAT = "multimodal-chat" + + # Specialized AI + MUSIC_GENERATION = "music-generation" + VOICE_CLONING = "voice-cloning" + STYLE_TRANSFER = "style-transfer" + SUPER_RESOLUTION = "super-resolution" + IMAGE_INPAINTING = "image-inpainting" + IMAGE_OUTPAINTING = "image-outpainting" + BACKGROUND_REMOVAL = "background-removal" + FACE_RESTORATION = "face-restoration" + + # Content Creation + CREATIVE_WRITING = "creative-writing" + STORY_GENERATION = "story-generation" + SCREENPLAY_WRITING = "screenplay-writing" + POETRY_GENERATION = "poetry-generation" + BLOG_WRITING = "blog-writing" + MARKETING_COPY = "marketing-copy" + + # Game Development + GAME_ASSET_GENERATION = "game-asset-generation" + CHARACTER_GENERATION = "character-generation" + LEVEL_GENERATION = "level-generation" + DIALOGUE_GENERATION = "dialogue-generation" + + # Science and Research + PROTEIN_FOLDING = "protein-folding" + MOLECULE_GENERATION = "molecule-generation" + SCIENTIFIC_WRITING = "scientific-writing" + RESEARCH_ASSISTANCE = "research-assistance" + DATA_ANALYSIS = "data-analysis" + + # Business and Productivity + EMAIL_GENERATION = "email-generation" + PRESENTATION_CREATION = "presentation-creation" + REPORT_GENERATION = "report-generation" + MEETING_SUMMARIZATION = "meeting-summarization" + PROJECT_PLANNING = "project-planning" + + # AI Teacher and Education + AI_TUTORING = "ai-tutoring" + EDUCATIONAL_CONTENT = "educational-content" + LESSON_PLANNING = "lesson-planning" + CONCEPT_EXPLANATION = "concept-explanation" + HOMEWORK_ASSISTANCE = "homework-assistance" + QUIZ_GENERATION = "quiz-generation" + CURRICULUM_DESIGN = "curriculum-design" + LEARNING_ASSESSMENT = "learning-assessment" + ADAPTIVE_LEARNING = "adaptive-learning" + SUBJECT_TEACHING = "subject-teaching" + MATH_TUTORING = "math-tutoring" + SCIENCE_TUTORING = "science-tutoring" + LANGUAGE_TUTORING = "language-tutoring" + HISTORY_TUTORING = "history-tutoring" + CODING_INSTRUCTION = "coding-instruction" + EXAM_PREPARATION = "exam-preparation" + STUDY_GUIDE_CREATION = "study-guide-creation" + EDUCATIONAL_GAMES = "educational-games" + LEARNING_ANALYTICS = "learning-analytics" + PERSONALIZED_LEARNING = "personalized-learning" + + # Advanced Image Processing & Manipulation + IMAGE_EDITING = "image-editing" + FACE_SWAP = "face-swap" + FACE_ENHANCEMENT = "face-enhancement" + FACE_GENERATION = "face-generation" + PORTRAIT_EDITING = "portrait-editing" + PHOTO_RESTORATION = "photo-restoration" + IMAGE_UPSCALING = "image-upscaling" + COLOR_CORRECTION = "color-correction" + ARTISTIC_FILTER = "artistic-filter" + + # Advanced Speech & Audio + ADVANCED_TTS = "advanced-tts" + ADVANCED_STT = "advanced-stt" + VOICE_CONVERSION = "voice-conversion" + SPEECH_ENHANCEMENT = "speech-enhancement" + AUDIO_GENERATION = "audio-generation" + MULTILINGUAL_TTS = "multilingual-tts" + MULTILINGUAL_STT = "multilingual-stt" + REAL_TIME_TRANSLATION = "real-time-translation" + + # Interactive Avatar & Video Generation + TALKING_AVATAR = "talking-avatar" + AVATAR_GENERATION = "avatar-generation" + LIP_SYNC = "lip-sync" + FACIAL_ANIMATION = "facial-animation" + GESTURE_GENERATION = "gesture-generation" + VIRTUAL_PRESENTER = "virtual-presenter" + AI_ANCHOR = "ai-anchor" + + # Interactive Language & Conversation + INTERACTIVE_CHAT = "interactive-chat" + BILINGUAL_CONVERSATION = "bilingual-conversation" + CULTURAL_ADAPTATION = "cultural-adaptation" + CONTEXT_AWARE_CHAT = "context-aware-chat" + PERSONALITY_CHAT = "personality-chat" + ROLE_PLAY_CHAT = "role-play-chat" + DOMAIN_SPECIFIC_CHAT = "domain-specific-chat" + + # Qwen Specialized Categories + QWEN_REASONING = "qwen-reasoning" + QWEN_MATH = "qwen-math" + QWEN_CODE = "qwen-code" + QWEN_VISION = "qwen-vision" + QWEN_AUDIO = "qwen-audio" + + # DeepSeek Specialized Categories + DEEPSEEK_CODING = "deepseek-coding" + DEEPSEEK_REASONING = "deepseek-reasoning" + DEEPSEEK_MATH = "deepseek-math" + DEEPSEEK_RESEARCH = "deepseek-research" + + +@dataclass +class HFModel: + """Hugging Face model definition""" + + name: str + model_id: str + category: ModelCategory + description: str + endpoint_compatible: bool = False + requires_auth: bool = False + max_tokens: Optional[int] = None + supports_streaming: bool = False + + +class HuggingFaceModels: + """Comprehensive collection of Hugging Face models for all categories""" + + # Text Generation Models (Latest and Popular) + TEXT_GENERATION_MODELS = [ + HFModel( + "MiniMax-M2", + "MiniMaxAI/MiniMax-M2", + ModelCategory.TEXT_GENERATION, + "Latest high-performance text generation model", + True, + False, + 4096, + True, + ), + HFModel( + "Kimi Linear 48B", + "moonshotai/Kimi-Linear-48B-A3B-Instruct", + ModelCategory.TEXT_GENERATION, + "Large instruction-tuned model with linear attention", + True, + False, + 8192, + True, + ), + HFModel( + "GPT-OSS 20B", + "openai/gpt-oss-20b", + ModelCategory.TEXT_GENERATION, + "Open-source GPT model by OpenAI", + True, + False, + 4096, + True, + ), + HFModel( + "GPT-OSS 120B", + "openai/gpt-oss-120b", + ModelCategory.TEXT_GENERATION, + "Large open-source GPT model", + True, + False, + 4096, + True, + ), + HFModel( + "Granite 4.0 1B", + "ibm-granite/granite-4.0-1b", + ModelCategory.TEXT_GENERATION, + "IBM's enterprise-grade small language model", + True, + False, + 2048, + True, + ), + HFModel( + "GLM-4.6", + "zai-org/GLM-4.6", + ModelCategory.TEXT_GENERATION, + "Multilingual conversational model", + True, + False, + 4096, + True, + ), + HFModel( + "Llama 3.1 8B Instruct", + "meta-llama/Llama-3.1-8B-Instruct", + ModelCategory.TEXT_GENERATION, + "Meta's instruction-tuned Llama model", + True, + True, + 8192, + True, + ), + HFModel( + "Tongyi DeepResearch 30B", + "Alibaba-NLP/Tongyi-DeepResearch-30B-A3B", + ModelCategory.TEXT_GENERATION, + "Alibaba's research-focused large language model", + True, + False, + 4096, + True, + ), + HFModel( + "EuroLLM 9B", + "utter-project/EuroLLM-9B", + ModelCategory.TEXT_GENERATION, + "European multilingual language model", + True, + False, + 4096, + True, + ), + ] + + # Text-to-Image Models (Latest and Best) + TEXT_TO_IMAGE_MODELS = [ + HFModel( + "FIBO", + "briaai/FIBO", + ModelCategory.TEXT_TO_IMAGE, + "Advanced text-to-image generation model", + True, + False, + ), + HFModel( + "FLUX.1 Dev", + "black-forest-labs/FLUX.1-dev", + ModelCategory.TEXT_TO_IMAGE, + "State-of-the-art image generation", + True, + False, + ), + HFModel( + "FLUX.1 Schnell", + "black-forest-labs/FLUX.1-schnell", + ModelCategory.TEXT_TO_IMAGE, + "Fast high-quality image generation", + True, + False, + ), + HFModel( + "Qwen Image", + "Qwen/Qwen-Image", + ModelCategory.TEXT_TO_IMAGE, + "Multilingual text-to-image model", + True, + False, + ), + HFModel( + "Stable Diffusion XL", + "stabilityai/stable-diffusion-xl-base-1.0", + ModelCategory.TEXT_TO_IMAGE, + "Popular high-resolution image generation", + True, + False, + ), + HFModel( + "Stable Diffusion 3.5 Large", + "stabilityai/stable-diffusion-3.5-large", + ModelCategory.TEXT_TO_IMAGE, + "Latest Stable Diffusion model", + True, + False, + ), + HFModel( + "HunyuanImage 3.0", + "tencent/HunyuanImage-3.0", + ModelCategory.TEXT_TO_IMAGE, + "Tencent's advanced image generation model", + True, + False, + ), + HFModel( + "Nitro-E", + "amd/Nitro-E", + ModelCategory.TEXT_TO_IMAGE, + "AMD's efficient image generation model", + True, + False, + ), + HFModel( + "Qwen Image Lightning", + "lightx2v/Qwen-Image-Lightning", + ModelCategory.TEXT_TO_IMAGE, + "Fast distilled image generation", + True, + False, + ), + ] + + # Automatic Speech Recognition Models + ASR_MODELS = [ + HFModel( + "Whisper Large v3", + "openai/whisper-large-v3", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "OpenAI's best multilingual speech recognition", + True, + False, + ), + HFModel( + "Whisper Large v3 Turbo", + "openai/whisper-large-v3-turbo", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Faster version of Whisper Large v3", + True, + False, + ), + HFModel( + "Parakeet TDT 0.6B v3", + "nvidia/parakeet-tdt-0.6b-v3", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "NVIDIA's multilingual ASR model", + True, + False, + ), + HFModel( + "Canary Qwen 2.5B", + "nvidia/canary-qwen-2.5b", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "NVIDIA's advanced ASR with Qwen integration", + True, + False, + ), + HFModel( + "Canary 1B v2", + "nvidia/canary-1b-v2", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Compact multilingual ASR model", + True, + False, + ), + HFModel( + "Whisper Small", + "openai/whisper-small", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Lightweight multilingual ASR", + True, + False, + ), + HFModel( + "Speaker Diarization 3.1", + "pyannote/speaker-diarization-3.1", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Advanced speaker identification and diarization", + True, + False, + ), + ] + + # Text-to-Speech Models + TTS_MODELS = [ + HFModel( + "SoulX Podcast 1.7B", + "Soul-AILab/SoulX-Podcast-1.7B", + ModelCategory.TEXT_TO_SPEECH, + "High-quality podcast-style speech synthesis", + True, + False, + ), + HFModel( + "NeuTTS Air", + "neuphonic/neutts-air", + ModelCategory.TEXT_TO_SPEECH, + "Advanced neural text-to-speech", + True, + False, + ), + HFModel( + "Kokoro 82M", + "hexgrad/Kokoro-82M", + ModelCategory.TEXT_TO_SPEECH, + "Lightweight high-quality TTS", + True, + False, + ), + HFModel( + "Kani TTS 400M EN", + "nineninesix/kani-tts-400m-en", + ModelCategory.TEXT_TO_SPEECH, + "English-focused text-to-speech model", + True, + False, + ), + HFModel( + "XTTS v2", + "coqui/XTTS-v2", + ModelCategory.TEXT_TO_SPEECH, + "Zero-shot voice cloning TTS", + True, + False, + ), + HFModel( + "Chatterbox", + "ResembleAI/chatterbox", + ModelCategory.TEXT_TO_SPEECH, + "Multilingual voice cloning", + True, + False, + ), + HFModel( + "VibeVoice 1.5B", + "microsoft/VibeVoice-1.5B", + ModelCategory.TEXT_TO_SPEECH, + "Microsoft's advanced TTS model", + True, + False, + ), + HFModel( + "OpenAudio S1 Mini", + "fishaudio/openaudio-s1-mini", + ModelCategory.TEXT_TO_SPEECH, + "Compact multilingual TTS", + True, + False, + ), + ] + + # Image Classification Models + IMAGE_CLASSIFICATION_MODELS = [ + HFModel( + "NSFW Image Detection", + "Falconsai/nsfw_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Content safety image classification", + True, + False, + ), + HFModel( + "ViT Base Patch16", + "google/vit-base-patch16-224", + ModelCategory.IMAGE_CLASSIFICATION, + "Google's Vision Transformer", + True, + False, + ), + HFModel( + "Deepfake Detection", + "dima806/deepfake_vs_real_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Detect AI-generated vs real images", + True, + False, + ), + HFModel( + "Facial Emotions Detection", + "dima806/facial_emotions_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Recognize facial emotions", + True, + False, + ), + HFModel( + "SDXL Detector", + "Organika/sdxl-detector", + ModelCategory.IMAGE_CLASSIFICATION, + "Detect Stable Diffusion XL generated images", + True, + False, + ), + HFModel( + "ViT NSFW Detector", + "AdamCodd/vit-base-nsfw-detector", + ModelCategory.IMAGE_CLASSIFICATION, + "NSFW content detection with ViT", + True, + False, + ), + HFModel( + "ResNet 101", + "microsoft/resnet-101", + ModelCategory.IMAGE_CLASSIFICATION, + "Microsoft's ResNet for classification", + True, + False, + ), + ] + + # Additional Categories + FEATURE_EXTRACTION_MODELS = [ + HFModel( + "Sentence Transformers All MiniLM", + "sentence-transformers/all-MiniLM-L6-v2", + ModelCategory.FEATURE_EXTRACTION, + "Lightweight sentence embeddings", + True, + False, + ), + HFModel( + "BGE Large EN", + "BAAI/bge-large-en-v1.5", + ModelCategory.FEATURE_EXTRACTION, + "High-quality English embeddings", + True, + False, + ), + HFModel( + "E5 Large v2", + "intfloat/e5-large-v2", + ModelCategory.FEATURE_EXTRACTION, + "Multilingual text embeddings", + True, + False, + ), + ] + + TRANSLATION_MODELS = [ + HFModel( + "M2M100 1.2B", + "facebook/m2m100_1.2B", + ModelCategory.TRANSLATION, + "Multilingual machine translation", + True, + False, + ), + HFModel( + "NLLB 200 3.3B", + "facebook/nllb-200-3.3B", + ModelCategory.TRANSLATION, + "No Language Left Behind translation", + True, + False, + ), + HFModel( + "mBART Large 50", + "facebook/mbart-large-50-many-to-many-mmt", + ModelCategory.TRANSLATION, + "Multilingual BART for translation", + True, + False, + ), + ] + + SUMMARIZATION_MODELS = [ + HFModel( + "PEGASUS XSum", + "google/pegasus-xsum", + ModelCategory.SUMMARIZATION, + "Abstractive summarization model", + True, + False, + ), + HFModel( + "BART Large CNN", + "facebook/bart-large-cnn", + ModelCategory.SUMMARIZATION, + "CNN/DailyMail summarization", + True, + False, + ), + HFModel( + "T5 Base", + "t5-base", + ModelCategory.SUMMARIZATION, + "Text-to-Text Transfer Transformer", + True, + False, + ), + ] + + # Video Generation and Processing Models + VIDEO_GENERATION_MODELS = [ + HFModel( + "Stable Video Diffusion", + "stabilityai/stable-video-diffusion-img2vid", + ModelCategory.TEXT_TO_VIDEO, + "Image-to-video generation model", + True, + False, + ), + HFModel( + "AnimateDiff", + "guoyww/animatediff", + ModelCategory.VIDEO_GENERATION, + "Text-to-video animation generation", + True, + False, + ), + HFModel( + "VideoCrafter", + "videogen/VideoCrafter", + ModelCategory.TEXT_TO_VIDEO, + "High-quality text-to-video generation", + True, + False, + ), + HFModel( + "Video ChatGPT", + "mbzuai-oryx/Video-ChatGPT-7B", + ModelCategory.VIDEO_TO_TEXT, + "Video understanding and description", + True, + False, + ), + HFModel( + "Video-BLIP", + "salesforce/video-blip-opt-2.7b", + ModelCategory.VIDEO_CLASSIFICATION, + "Video content analysis and classification", + True, + False, + ), + ] + + # Code Generation and Development Models + CODE_GENERATION_MODELS = [ + HFModel( + "CodeLlama 34B Instruct", + "codellama/CodeLlama-34b-Instruct-hf", + ModelCategory.CODE_GENERATION, + "Large instruction-tuned code generation model", + True, + True, + ), + HFModel( + "StarCoder2 15B", + "bigcode/starcoder2-15b", + ModelCategory.CODE_GENERATION, + "Advanced code generation and completion", + True, + False, + ), + HFModel( + "DeepSeek Coder V2", + "deepseek-ai/deepseek-coder-6.7b-instruct", + ModelCategory.CODE_GENERATION, + "Specialized coding assistant", + True, + False, + ), + HFModel( + "WizardCoder 34B", + "WizardLM/WizardCoder-Python-34B-V1.0", + ModelCategory.CODE_GENERATION, + "Python-focused code generation", + True, + False, + ), + HFModel( + "Phind CodeLlama", + "Phind/Phind-CodeLlama-34B-v2", + ModelCategory.CODE_GENERATION, + "Optimized for code explanation and debugging", + True, + False, + ), + HFModel( + "Code T5+", + "Salesforce/codet5p-770m", + ModelCategory.CODE_COMPLETION, + "Code understanding and generation", + True, + False, + ), + HFModel( + "InCoder", + "facebook/incoder-6B", + ModelCategory.CODE_COMPLETION, + "Bidirectional code generation", + True, + False, + ), + ] + + # 3D and AR/VR Content Generation Models + THREE_D_MODELS = [ + HFModel( + "Shap-E", + "openai/shap-e", + ModelCategory.TEXT_TO_3D, + "Text-to-3D shape generation", + True, + False, + ), + HFModel( + "Point-E", + "openai/point-e", + ModelCategory.TEXT_TO_3D, + "Text-to-3D point cloud generation", + True, + False, + ), + HFModel( + "DreamFusion", + "google/dreamfusion", + ModelCategory.IMAGE_TO_3D, + "Image-to-3D mesh generation", + True, + False, + ), + HFModel( + "Magic3D", + "nvidia/magic3d", + ModelCategory.THREE_D_GENERATION, + "High-quality 3D content creation", + True, + False, + ), + HFModel( + "GET3D", + "nvidia/get3d", + ModelCategory.MESH_GENERATION, + "3D mesh generation from text", + True, + False, + ), + ] + + # Document Processing and OCR Models + DOCUMENT_PROCESSING_MODELS = [ + HFModel( + "TrOCR Large", + "microsoft/trocr-large-printed", + ModelCategory.OCR, + "Transformer-based OCR for printed text", + True, + False, + ), + HFModel( + "TrOCR Handwritten", + "microsoft/trocr-large-handwritten", + ModelCategory.HANDWRITING_RECOGNITION, + "Handwritten text recognition", + True, + False, + ), + HFModel( + "LayoutLMv3", + "microsoft/layoutlmv3-large", + ModelCategory.DOCUMENT_ANALYSIS, + "Document layout analysis and understanding", + True, + False, + ), + HFModel( + "Donut", + "naver-clova-ix/donut-base", + ModelCategory.DOCUMENT_ANALYSIS, + "OCR-free document understanding", + True, + False, + ), + HFModel( + "TableTransformer", + "microsoft/table-transformer-structure-recognition", + ModelCategory.TABLE_EXTRACTION, + "Table structure recognition", + True, + False, + ), + HFModel( + "FormNet", + "microsoft/formnet", + ModelCategory.FORM_PROCESSING, + "Form understanding and processing", + True, + False, + ), + ] + + # Multimodal AI Models + MULTIMODAL_MODELS = [ + HFModel( + "BLIP-2", + "Salesforce/blip2-opt-2.7b", + ModelCategory.VISION_LANGUAGE, + "Vision-language understanding and generation", + True, + False, + ), + HFModel( + "InstructBLIP", + "Salesforce/instructblip-vicuna-7b", + ModelCategory.MULTIMODAL_REASONING, + "Instruction-following multimodal model", + True, + False, + ), + HFModel( + "LLaVA", + "liuhaotian/llava-v1.5-7b", + ModelCategory.VISUAL_QUESTION_ANSWERING, + "Large Language and Vision Assistant", + True, + False, + ), + HFModel( + "GPT-4V", + "openai/gpt-4-vision-preview", + ModelCategory.MULTIMODAL_CHAT, + "Advanced multimodal conversational AI", + True, + True, + ), + HFModel( + "Flamingo", + "deepmind/flamingo-9b", + ModelCategory.CROSS_MODAL_GENERATION, + "Few-shot learning for vision and language", + True, + False, + ), + ] + + # Specialized AI Models + SPECIALIZED_AI_MODELS = [ + HFModel( + "MusicGen", + "facebook/musicgen-medium", + ModelCategory.MUSIC_GENERATION, + "Text-to-music generation", + True, + False, + ), + HFModel( + "AudioCraft", + "facebook/audiocraft_musicgen_melody", + ModelCategory.MUSIC_GENERATION, + "Melody-conditioned music generation", + True, + False, + ), + HFModel( + "Real-ESRGAN", + "xinntao/realesrgan-x4plus", + ModelCategory.SUPER_RESOLUTION, + "Image super-resolution", + True, + False, + ), + HFModel( + "GFPGAN", + "TencentARC/GFPGAN", + ModelCategory.FACE_RESTORATION, + "Face restoration and enhancement", + True, + False, + ), + HFModel( + "LaMa", + "advimman/lama", + ModelCategory.IMAGE_INPAINTING, + "Large Mask Inpainting", + True, + False, + ), + HFModel( + "Background Remover", + "briaai/RMBG-1.4", + ModelCategory.BACKGROUND_REMOVAL, + "Automatic background removal", + True, + False, + ), + HFModel( + "Voice Cloner", + "coqui/XTTS-v2", + ModelCategory.VOICE_CLONING, + "Multilingual voice cloning", + True, + False, + ), + ] + + # Creative Content Models + CREATIVE_CONTENT_MODELS = [ + HFModel( + "GPT-3.5 Creative", + "openai/gpt-3.5-turbo-instruct", + ModelCategory.CREATIVE_WRITING, + "Creative writing and storytelling", + True, + True, + ), + HFModel( + "Novel AI", + "novelai/genji-python-6b", + ModelCategory.STORY_GENERATION, + "Interactive story generation", + True, + False, + ), + HFModel( + "Poet Assistant", + "gpt2-poetry", + ModelCategory.POETRY_GENERATION, + "Poetry generation and analysis", + True, + False, + ), + HFModel( + "Blog Writer", + "google/flan-t5-large", + ModelCategory.BLOG_WRITING, + "Blog content creation", + True, + False, + ), + HFModel( + "Marketing Copy AI", + "microsoft/DialoGPT-large", + ModelCategory.MARKETING_COPY, + "Marketing content generation", + True, + False, + ), + ] + + # Game Development Models + GAME_DEVELOPMENT_MODELS = [ + HFModel( + "Character AI", + "character-ai/character-generator", + ModelCategory.CHARACTER_GENERATION, + "Game character generation and design", + True, + False, + ), + HFModel( + "Level Designer", + "unity/level-generator", + ModelCategory.LEVEL_GENERATION, + "Game level and environment generation", + True, + False, + ), + HFModel( + "Dialogue Writer", + "bioware/dialogue-generator", + ModelCategory.DIALOGUE_GENERATION, + "Game dialogue and narrative generation", + True, + False, + ), + HFModel( + "Asset Creator", + "epic/asset-generator", + ModelCategory.GAME_ASSET_GENERATION, + "Game asset and texture generation", + True, + False, + ), + ] + + # Science and Research Models + SCIENCE_RESEARCH_MODELS = [ + HFModel( + "AlphaFold", + "deepmind/alphafold2", + ModelCategory.PROTEIN_FOLDING, + "Protein structure prediction", + True, + False, + ), + HFModel( + "ChemBERTa", + "DeepChem/ChemBERTa-77M-MLM", + ModelCategory.MOLECULE_GENERATION, + "Chemical compound analysis", + True, + False, + ), + HFModel( + "SciBERT", + "allenai/scibert_scivocab_uncased", + ModelCategory.SCIENTIFIC_WRITING, + "Scientific text understanding", + True, + False, + ), + HFModel( + "Research Assistant", + "microsoft/specter2", + ModelCategory.RESEARCH_ASSISTANCE, + "Research paper analysis and recommendations", + True, + False, + ), + HFModel( + "Data Analyst", + "microsoft/data-copilot", + ModelCategory.DATA_ANALYSIS, + "Automated data analysis and insights", + True, + False, + ), + ] + + # Business and Productivity Models + BUSINESS_PRODUCTIVITY_MODELS = [ + HFModel( + "Email Assistant", + "microsoft/email-generator", + ModelCategory.EMAIL_GENERATION, + "Professional email composition", + True, + False, + ), + HFModel( + "Presentation AI", + "gamma/presentation-generator", + ModelCategory.PRESENTATION_CREATION, + "Automated presentation creation", + True, + False, + ), + HFModel( + "Report Writer", + "openai/report-generator", + ModelCategory.REPORT_GENERATION, + "Business report generation", + True, + False, + ), + HFModel( + "Meeting Summarizer", + "microsoft/meeting-summarizer", + ModelCategory.MEETING_SUMMARIZATION, + "Meeting notes and action items", + True, + False, + ), + HFModel( + "Project Planner", + "atlassian/project-ai", + ModelCategory.PROJECT_PLANNING, + "Project planning and management", + True, + False, + ), + ] + + # AI Teacher Models - Best-in-Class Educational AI System + AI_TEACHER_MODELS = [ + # Primary AI Tutoring Models - Interactive & Conversational + HFModel( + "AI Tutor Interactive", + "microsoft/DialoGPT-medium", + ModelCategory.AI_TUTORING, + "Interactive AI tutor for conversational learning with dialogue management", + True, + False, + 2048, + True, + ), + HFModel( + "Goal-Oriented Tutor", + "microsoft/GODEL-v1_1-large-seq2seq", + ModelCategory.AI_TUTORING, + "Goal-oriented conversational AI for personalized tutoring sessions", + True, + False, + 2048, + True, + ), + HFModel( + "Advanced Instruction Tutor", + "google/flan-t5-large", + ModelCategory.AI_TUTORING, + "Advanced instruction-following AI tutor for complex educational tasks", + True, + False, + 2048, + True, + ), + # Educational Content Generation - Creative & Comprehensive + HFModel( + "Educational Content Creator Pro", + "facebook/bart-large", + ModelCategory.EDUCATIONAL_CONTENT, + "Professional educational content generation for all learning levels", + True, + False, + 1024, + False, + ), + HFModel( + "Multilingual Education AI", + "bigscience/bloom-560m", + ModelCategory.EDUCATIONAL_CONTENT, + "Global multilingual educational content for diverse learners", + True, + False, + 2048, + True, + ), + HFModel( + "Academic Writing Assistant", + "microsoft/prophetnet-large-uncased", + ModelCategory.EDUCATIONAL_CONTENT, + "Academic content creation with advanced text generation capabilities", + True, + False, + 1024, + False, + ), + # Lesson Planning & Curriculum Design - Structured & Professional + HFModel( + "Master Lesson Planner", + "facebook/bart-large-cnn", + ModelCategory.LESSON_PLANNING, + "Comprehensive lesson planning with summarization and structure", + True, + False, + 1024, + False, + ), + HFModel( + "Curriculum Architect", + "microsoft/prophetnet-base-uncased", + ModelCategory.CURRICULUM_DESIGN, + "Professional curriculum planning and educational program design", + True, + False, + 1024, + False, + ), + HFModel( + "Activity Designer", + "google/t5-base", + ModelCategory.LESSON_PLANNING, + "Interactive learning activity and exercise generation", + True, + False, + 512, + True, + ), + # Subject-Specific Excellence - STEM Focus + HFModel( + "Programming Mentor Pro", + "microsoft/codebert-base", + ModelCategory.CODING_INSTRUCTION, + "Expert programming education with code analysis and explanation", + True, + False, + 1024, + False, + ), + HFModel( + "Advanced Code Instructor", + "microsoft/graphcodebert-base", + ModelCategory.CODING_INSTRUCTION, + "Advanced programming instruction with graph understanding", + True, + False, + 1024, + False, + ), + HFModel( + "Algorithm Tutor Elite", + "microsoft/unixcoder-base", + ModelCategory.CODING_INSTRUCTION, + "Elite algorithm education and computational thinking development", + True, + False, + 1024, + False, + ), + # Science & Mathematics Excellence + HFModel( + "Science Research Educator", + "allenai/scibert_scivocab_uncased", + ModelCategory.SCIENCE_TUTORING, + "Scientific education with research-grade knowledge and vocabulary", + True, + False, + 512, + False, + ), + HFModel( + "Advanced Science AI", + "facebook/galactica-125m", + ModelCategory.SCIENCE_TUTORING, + "Advanced scientific knowledge and research methodology education", + True, + False, + 2048, + True, + ), + HFModel( + "Mathematical Reasoning Master", + "google/flan-t5-xl", + ModelCategory.MATH_TUTORING, + "Advanced mathematical reasoning, proofs, and problem-solving", + True, + False, + 2048, + True, + ), + HFModel( + "Interactive Math Tutor", + "microsoft/DialoGPT-small", + ModelCategory.MATH_TUTORING, + "Interactive mathematics tutoring with step-by-step explanations", + True, + False, + 1024, + True, + ), + # Language & Literature Excellence + HFModel( + "Multilingual Language Master", + "facebook/mbart-large-50-many-to-many-mmt", + ModelCategory.LANGUAGE_TUTORING, + "Advanced multilingual education and cross-language learning", + True, + False, + 1024, + False, + ), + HFModel( + "Literature & Language AI", + "microsoft/prophetnet-large-uncased-cnndm", + ModelCategory.LANGUAGE_TUTORING, + "Literature analysis and advanced language instruction", + True, + False, + 1024, + False, + ), + HFModel( + "Grammar & Comprehension Expert", + "google/electra-base-discriminator", + ModelCategory.LANGUAGE_TUTORING, + "Expert grammar instruction and reading comprehension development", + True, + False, + 512, + False, + ), + # Assessment & Evaluation Excellence + HFModel( + "Assessment Designer Pro", + "microsoft/DialoGPT-large", + ModelCategory.QUIZ_GENERATION, + "Professional assessment and quiz generation with interaction", + True, + False, + 2048, + True, + ), + HFModel( + "Learning Progress Analyzer", + "facebook/bart-large", + ModelCategory.LEARNING_ASSESSMENT, + "Comprehensive learning assessment and progress tracking", + True, + False, + 1024, + False, + ), + HFModel( + "Question Master AI", + "google/t5-base", + ModelCategory.QUIZ_GENERATION, + "Intelligent question generation for all educational levels", + True, + False, + 512, + True, + ), + HFModel( + "Exam Preparation Specialist", + "microsoft/unilm-base-cased", + ModelCategory.EXAM_PREPARATION, + "Specialized exam preparation and test strategy development", + True, + False, + 1024, + False, + ), + # Personalized & Adaptive Learning Excellence + HFModel( + "Personal Learning Architect", + "microsoft/deberta-v3-base", + ModelCategory.PERSONALIZED_LEARNING, + "Advanced personalized learning path creation and optimization", + True, + False, + 512, + False, + ), + HFModel( + "Adaptive Learning Engine", + "facebook/opt-125m", + ModelCategory.ADAPTIVE_LEARNING, + "Intelligent adaptive learning with dynamic content adjustment", + True, + False, + 2048, + True, + ), + HFModel( + "Learning Analytics Expert", + "microsoft/layoutlm-base-uncased", + ModelCategory.LEARNING_ANALYTICS, + "Advanced learning analytics and educational data interpretation", + True, + False, + 512, + False, + ), + # Concept Explanation & Understanding Masters + HFModel( + "Concept Explanation Master", + "microsoft/deberta-v3-base", + ModelCategory.CONCEPT_EXPLANATION, + "Master-level concept explanation and knowledge breakdown", + True, + False, + 512, + False, + ), + HFModel( + "Knowledge Synthesizer", + "google/pegasus-xsum", + ModelCategory.CONCEPT_EXPLANATION, + "Advanced knowledge synthesis and concept summarization", + True, + False, + 512, + False, + ), + HFModel( + "Interactive Concept Guide", + "facebook/bart-base", + ModelCategory.CONCEPT_EXPLANATION, + "Interactive concept teaching with clarification and examples", + True, + False, + 1024, + False, + ), + # Homework & Study Support Excellence + HFModel( + "Programming Homework Expert", + "microsoft/codebert-base-mlm", + ModelCategory.HOMEWORK_ASSISTANCE, + "Expert programming homework assistance and debugging support", + True, + False, + 1024, + False, + ), + HFModel( + "Universal Homework Helper", + "google/flan-t5-small", + ModelCategory.HOMEWORK_ASSISTANCE, + "Comprehensive homework assistance across all academic subjects", + True, + False, + 1024, + True, + ), + HFModel( + "Global Study Assistant", + "facebook/mbart-large-cc25", + ModelCategory.HOMEWORK_ASSISTANCE, + "Multilingual homework support with cultural context understanding", + True, + False, + 1024, + False, + ), + # Study Materials & Resources Excellence + HFModel( + "Study Guide Architect", + "microsoft/prophetnet-large-uncased", + ModelCategory.STUDY_GUIDE_CREATION, + "Professional study guide creation and learning material development", + True, + False, + 1024, + False, + ), + HFModel( + "Educational Resource Creator", + "facebook/bart-large-xsum", + ModelCategory.STUDY_GUIDE_CREATION, + "Comprehensive educational resource and reference material creation", + True, + False, + 1024, + False, + ), + # Interactive Learning & Gamification + HFModel( + "Educational Game Designer", + "microsoft/DialoGPT-base", + ModelCategory.EDUCATIONAL_GAMES, + "Interactive educational games and gamified learning experiences", + True, + False, + 1024, + True, + ), + HFModel( + "Learning Game Engine", + "google/bert-base-uncased", + ModelCategory.EDUCATIONAL_GAMES, + "Educational game mechanics and interactive learning systems", + True, + False, + 512, + False, + ), + # History & Social Studies Excellence + HFModel( + "History Professor AI", + "microsoft/deberta-large", + ModelCategory.HISTORY_TUTORING, + "Professor-level historical analysis and social studies education", + True, + False, + 1024, + False, + ), + HFModel( + "Interactive History Guide", + "facebook/opt-350m", + ModelCategory.HISTORY_TUTORING, + "Interactive historical narratives and timeline exploration", + True, + False, + 2048, + True, + ), + # Multi-Subject Teaching Excellence + HFModel( + "Master Subject Teacher", + "google/flan-t5-base", + ModelCategory.SUBJECT_TEACHING, + "Expert multi-subject teaching with instruction-following excellence", + True, + False, + 1024, + True, + ), + HFModel( + "Universal Educator AI", + "microsoft/unilm-large-cased", + ModelCategory.SUBJECT_TEACHING, + "Universal education AI with cross-disciplinary knowledge", + True, + False, + 1024, + False, + ), + # Advanced Analytics & Optimization + HFModel( + "Advanced Learning Analytics", + "microsoft/layoutlm-large-uncased", + ModelCategory.LEARNING_ANALYTICS, + "Enterprise-level learning analytics and educational insights", + True, + False, + 1024, + False, + ), + HFModel( + "Personalization Engine Pro", + "google/electra-large-discriminator", + ModelCategory.PERSONALIZED_LEARNING, + "Advanced AI personalization with learning style adaptation", + True, + False, + 512, + False, + ), + HFModel( + "Global Adaptive System", + "facebook/mbart-large-50", + ModelCategory.ADAPTIVE_LEARNING, + "Global adaptive learning system with multilingual capabilities", + True, + False, + 1024, + False, + ), + ] + + # Qwen Models - Advanced Reasoning and Multimodal AI + QWEN_MODELS = [ + # Qwen2.5 Series - Latest Models + HFModel( + "Qwen2.5-72B-Instruct", + "Qwen/Qwen2.5-72B-Instruct", + ModelCategory.TEXT_GENERATION, + "Large-scale instruction-following model for complex reasoning", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-32B-Instruct", + "Qwen/Qwen2.5-32B-Instruct", + ModelCategory.TEXT_GENERATION, + "High-performance instruction model for advanced tasks", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-14B-Instruct", + "Qwen/Qwen2.5-14B-Instruct", + ModelCategory.TEXT_GENERATION, + "Efficient large model with excellent reasoning capabilities", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-7B-Instruct", + "Qwen/Qwen2.5-7B-Instruct", + ModelCategory.TEXT_GENERATION, + "Optimized 7B model for general-purpose applications", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-3B-Instruct", + "Qwen/Qwen2.5-3B-Instruct", + ModelCategory.TEXT_GENERATION, + "Lightweight model for resource-constrained environments", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-1.5B-Instruct", + "Qwen/Qwen2.5-1.5B-Instruct", + ModelCategory.TEXT_GENERATION, + "Ultra-lightweight model for edge deployment", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-0.5B-Instruct", + "Qwen/Qwen2.5-0.5B-Instruct", + ModelCategory.TEXT_GENERATION, + "Minimal footprint model for basic applications", + True, + False, + 32768, + True, + ), + # Qwen2.5-Coder Series - Programming Specialists + HFModel( + "Qwen2.5-Coder-32B-Instruct", + "Qwen/Qwen2.5-Coder-32B-Instruct", + ModelCategory.QWEN_CODE, + "Advanced code generation and programming assistance", + True, + False, + 131072, + True, + ), + HFModel( + "Qwen2.5-Coder-14B-Instruct", + "Qwen/Qwen2.5-Coder-14B-Instruct", + ModelCategory.QWEN_CODE, + "Code generation with excellent debugging capabilities", + True, + False, + 131072, + True, + ), + HFModel( + "Qwen2.5-Coder-7B-Instruct", + "Qwen/Qwen2.5-Coder-7B-Instruct", + ModelCategory.QWEN_CODE, + "Efficient coding assistant for multiple languages", + True, + False, + 131072, + True, + ), + HFModel( + "Qwen2.5-Coder-3B-Instruct", + "Qwen/Qwen2.5-Coder-3B-Instruct", + ModelCategory.QWEN_CODE, + "Lightweight programming assistant", + True, + False, + 131072, + True, + ), + HFModel( + "Qwen2.5-Coder-1.5B-Instruct", + "Qwen/Qwen2.5-Coder-1.5B-Instruct", + ModelCategory.QWEN_CODE, + "Compact code generation model", + True, + False, + 131072, + True, + ), + # Qwen2.5-Math Series - Mathematical Reasoning + HFModel( + "Qwen2.5-Math-72B-Instruct", + "Qwen/Qwen2.5-Math-72B-Instruct", + ModelCategory.QWEN_MATH, + "Advanced mathematical problem solving and reasoning", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-Math-7B-Instruct", + "Qwen/Qwen2.5-Math-7B-Instruct", + ModelCategory.QWEN_MATH, + "Mathematical reasoning and calculation assistance", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2.5-Math-1.5B-Instruct", + "Qwen/Qwen2.5-Math-1.5B-Instruct", + ModelCategory.QWEN_MATH, + "Compact mathematical problem solver", + True, + False, + 32768, + True, + ), + # QwQ Series - Reasoning Specialists + HFModel( + "QwQ-32B-Preview", + "Qwen/QwQ-32B-Preview", + ModelCategory.QWEN_REASONING, + "Advanced reasoning and logical thinking model", + True, + False, + 32768, + True, + ), + # Qwen2-VL Series - Vision-Language Models + HFModel( + "Qwen2-VL-72B-Instruct", + "Qwen/Qwen2-VL-72B-Instruct", + ModelCategory.QWEN_VISION, + "Large-scale vision-language understanding and generation", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2-VL-7B-Instruct", + "Qwen/Qwen2-VL-7B-Instruct", + ModelCategory.QWEN_VISION, + "Efficient vision-language model for multimodal tasks", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen2-VL-2B-Instruct", + "Qwen/Qwen2-VL-2B-Instruct", + ModelCategory.QWEN_VISION, + "Lightweight vision-language model", + True, + False, + 32768, + True, + ), + # Qwen2-Audio Series - Audio Understanding + HFModel( + "Qwen2-Audio-7B-Instruct", + "Qwen/Qwen2-Audio-7B-Instruct", + ModelCategory.QWEN_AUDIO, + "Advanced audio understanding and generation", + True, + False, + 32768, + True, + ), + # Qwen Legacy Models - Still Powerful + HFModel( + "Qwen1.5-110B-Chat", + "Qwen/Qwen1.5-110B-Chat", + ModelCategory.CONVERSATIONAL, + "Large conversational model with broad knowledge", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen1.5-72B-Chat", + "Qwen/Qwen1.5-72B-Chat", + ModelCategory.CONVERSATIONAL, + "Conversational AI with excellent reasoning", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen1.5-32B-Chat", + "Qwen/Qwen1.5-32B-Chat", + ModelCategory.CONVERSATIONAL, + "Efficient chat model for interactive applications", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen1.5-14B-Chat", + "Qwen/Qwen1.5-14B-Chat", + ModelCategory.CONVERSATIONAL, + "Balanced performance chat model", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen1.5-7B-Chat", + "Qwen/Qwen1.5-7B-Chat", + ModelCategory.CONVERSATIONAL, + "Popular chat model with good performance", + True, + False, + 32768, + True, + ), + HFModel( + "Qwen1.5-4B-Chat", + "Qwen/Qwen1.5-4B-Chat", + ModelCategory.CONVERSATIONAL, + "Lightweight conversational AI", + True, + False, + 32768, + True, + ), + ] + + # DeepSeek Models - Coding and Reasoning Excellence + DEEPSEEK_MODELS = [ + # DeepSeek-V3 Series - Latest Generation + HFModel( + "DeepSeek-V3", + "deepseek-ai/DeepSeek-V3", + ModelCategory.DEEPSEEK_REASONING, + "Latest generation reasoning and knowledge model", + True, + False, + 65536, + True, + ), + HFModel( + "DeepSeek-V3-Base", + "deepseek-ai/DeepSeek-V3-Base", + ModelCategory.TEXT_GENERATION, + "Foundation model for various downstream tasks", + True, + False, + 65536, + True, + ), + # DeepSeek-V2.5 Series + HFModel( + "DeepSeek-V2.5", + "deepseek-ai/DeepSeek-V2.5", + ModelCategory.DEEPSEEK_REASONING, + "Advanced reasoning and general intelligence model", + True, + False, + 32768, + True, + ), + # DeepSeek-Coder Series - Programming Specialists + HFModel( + "DeepSeek-Coder-V2-Instruct", + "deepseek-ai/DeepSeek-Coder-V2-Instruct", + ModelCategory.DEEPSEEK_CODING, + "Advanced code generation and programming assistance", + True, + False, + 163840, + True, + ), + HFModel( + "DeepSeek-Coder-V2-Base", + "deepseek-ai/DeepSeek-Coder-V2-Base", + ModelCategory.DEEPSEEK_CODING, + "Foundation coding model for fine-tuning", + True, + False, + 163840, + True, + ), + HFModel( + "DeepSeek-Coder-33B-Instruct", + "deepseek-ai/deepseek-coder-33b-instruct", + ModelCategory.DEEPSEEK_CODING, + "Large-scale code generation and debugging", + True, + False, + 16384, + True, + ), + HFModel( + "DeepSeek-Coder-6.7B-Instruct", + "deepseek-ai/deepseek-coder-6.7b-instruct", + ModelCategory.DEEPSEEK_CODING, + "Efficient code assistance and generation", + True, + False, + 16384, + True, + ), + HFModel( + "DeepSeek-Coder-1.3B-Instruct", + "deepseek-ai/deepseek-coder-1.3b-instruct", + ModelCategory.DEEPSEEK_CODING, + "Lightweight coding assistant", + True, + False, + 16384, + True, + ), + # DeepSeek-Math Series - Mathematical Reasoning + HFModel( + "DeepSeek-Math-7B-Instruct", + "deepseek-ai/deepseek-math-7b-instruct", + ModelCategory.DEEPSEEK_MATH, + "Mathematical problem solving and reasoning", + True, + False, + 4096, + True, + ), + HFModel( + "DeepSeek-Math-7B-Base", + "deepseek-ai/deepseek-math-7b-base", + ModelCategory.DEEPSEEK_MATH, + "Foundation model for mathematical reasoning", + True, + False, + 4096, + True, + ), + # DeepSeek Chat Models + HFModel( + "DeepSeek-67B-Chat", + "deepseek-ai/deepseek-llm-67b-chat", + ModelCategory.CONVERSATIONAL, + "Large conversational model with strong reasoning", + True, + False, + 4096, + True, + ), + HFModel( + "DeepSeek-7B-Chat", + "deepseek-ai/deepseek-llm-7b-chat", + ModelCategory.CONVERSATIONAL, + "Efficient chat model for general conversations", + True, + False, + 4096, + True, + ), + # DeepSeek-VL Series - Vision-Language + HFModel( + "DeepSeek-VL-7B-Chat", + "deepseek-ai/deepseek-vl-7b-chat", + ModelCategory.VISION_LANGUAGE, + "Vision-language understanding and conversation", + True, + False, + 4096, + True, + ), + HFModel( + "DeepSeek-VL-1.3B-Chat", + "deepseek-ai/deepseek-vl-1.3b-chat", + ModelCategory.VISION_LANGUAGE, + "Lightweight vision-language model", + True, + False, + 4096, + True, + ), + ] + + # Advanced Image Editing Models + IMAGE_EDITING_MODELS = [ + # Professional Image Editing + HFModel( + "SDXL Inpainting", + "diffusers/stable-diffusion-xl-1.0-inpainting-0.1", + ModelCategory.IMAGE_EDITING, + "High-quality image inpainting and editing", + True, + False, + 1024, + False, + ), + HFModel( + "ControlNet Inpainting", + "lllyasviel/control_v11p_sd15_inpaint", + ModelCategory.IMAGE_EDITING, + "Controllable image inpainting with precise editing", + True, + False, + 512, + False, + ), + HFModel( + "InstantID Face Editor", + "InstantX/InstantID", + ModelCategory.FACE_ENHANCEMENT, + "Identity-preserving face editing and enhancement", + True, + False, + 512, + False, + ), + HFModel( + "Real-ESRGAN Upscaler", + "ai-forever/Real-ESRGAN", + ModelCategory.IMAGE_UPSCALING, + "Advanced image super-resolution and enhancement", + True, + False, + 1024, + False, + ), + HFModel( + "GFPGAN Face Restoration", + "Xintao/GFPGAN", + ModelCategory.FACE_RESTORATION, + "High-quality face restoration and enhancement", + True, + False, + 512, + False, + ), + HFModel( + "CodeFormer Face Restoration", + "sczhou/CodeFormer", + ModelCategory.FACE_RESTORATION, + "Robust face restoration for low-quality images", + True, + False, + 512, + False, + ), + HFModel( + "Background Removal", + "briaai/RMBG-1.4", + ModelCategory.BACKGROUND_REMOVAL, + "Precise background removal and segmentation", + True, + False, + 1024, + False, + ), + HFModel( + "U2-Net Background Removal", + "simonw/u2net-portrait-segmentation", + ModelCategory.BACKGROUND_REMOVAL, + "Portrait and object background removal", + True, + False, + 320, + False, + ), + HFModel( + "Photo Colorization", + "microsoft/beit-base-patch16-224-pt22k-ft22k", + ModelCategory.COLOR_CORRECTION, + "AI-powered photo colorization and enhancement", + True, + False, + 224, + False, + ), + HFModel( + "Style Transfer Neural", + "pytorch/vision", + ModelCategory.ARTISTIC_FILTER, + "Neural style transfer for artistic image effects", + True, + False, + 512, + False, + ), + ] + + # Face Swap and Manipulation Models + FACE_SWAP_MODELS = [ + # Advanced Face Swapping + HFModel( + "InsightFace SwapFace", + "deepinsight/inswapper_128.onnx", + ModelCategory.FACE_SWAP, + "High-quality face swapping with identity preservation", + True, + False, + 128, + False, + ), + HFModel( + "SimSwap Face Swap", + "ppogg/simswap_official", + ModelCategory.FACE_SWAP, + "Realistic face swapping for videos and images", + True, + False, + 224, + False, + ), + HFModel( + "FaceX-Zoo Face Swap", + "FacePerceiver/FaceX-Zoo", + ModelCategory.FACE_SWAP, + "Multi-purpose face analysis and swapping toolkit", + True, + False, + 112, + False, + ), + HFModel( + "Face Enhancement Pro", + "TencentARC/GFPGAN", + ModelCategory.FACE_ENHANCEMENT, + "Professional face enhancement and restoration", + True, + False, + 512, + False, + ), + HFModel( + "DualStyleGAN Face Edit", + "williamyang1991/DualStyleGAN", + ModelCategory.FACE_ENHANCEMENT, + "Style-controllable face image editing", + True, + False, + 1024, + False, + ), + HFModel( + "MegaPortraits Face Animate", + "NVlabs/MegaPortraits", + ModelCategory.FACIAL_ANIMATION, + "One-shot facial animation and expression transfer", + True, + False, + 256, + False, + ), + ] + + # Advanced TTS and STT Models + ADVANCED_SPEECH_MODELS = [ + # Multilingual Text-to-Speech + HFModel( + "XTTS v2 Multilingual", + "coqui/XTTS-v2", + ModelCategory.MULTILINGUAL_TTS, + "High-quality multilingual text-to-speech with voice cloning", + True, + False, + 24000, + True, + ), + HFModel( + "Bark Text-to-Speech", + "suno/bark", + ModelCategory.ADVANCED_TTS, + "Generative TTS with music, sound effects, and multiple speakers", + True, + False, + 24000, + False, + ), + HFModel( + "SpeechT5 TTS", + "microsoft/speecht5_tts", + ModelCategory.ADVANCED_TTS, + "High-quality neural text-to-speech synthesis", + True, + False, + 16000, + False, + ), + HFModel( + "VALL-E X Multilingual", + "Plachtaa/VALL-E-X", + ModelCategory.MULTILINGUAL_TTS, + "Zero-shot voice synthesis in multiple languages", + True, + False, + 24000, + False, + ), + HFModel( + "Arabic TTS", + "arabic-speech-corpus/tts-arabic", + ModelCategory.MULTILINGUAL_TTS, + "High-quality Arabic text-to-speech synthesis", + True, + False, + 22050, + False, + ), + HFModel( + "Tortoise TTS", + "jbetker/tortoise-tts", + ModelCategory.VOICE_CLONING, + "High-quality voice cloning and synthesis", + True, + False, + 22050, + False, + ), + # Advanced Speech-to-Text + HFModel( + "Whisper Large v3", + "openai/whisper-large-v3", + ModelCategory.MULTILINGUAL_STT, + "State-of-the-art multilingual speech recognition", + True, + False, + 30, + False, + ), + HFModel( + "Whisper Large v3 Turbo", + "openai/whisper-large-v3-turbo", + ModelCategory.MULTILINGUAL_STT, + "Fast multilingual speech recognition with high accuracy", + True, + False, + 30, + True, + ), + HFModel( + "Arabic Whisper", + "arabic-speech-corpus/whisper-large-arabic", + ModelCategory.MULTILINGUAL_STT, + "Optimized Arabic speech recognition model", + True, + False, + 30, + False, + ), + HFModel( + "MMS Speech Recognition", + "facebook/mms-1b-all", + ModelCategory.MULTILINGUAL_STT, + "Massively multilingual speech recognition (1000+ languages)", + True, + False, + 16000, + False, + ), + HFModel( + "Wav2Vec2 Arabic", + "facebook/wav2vec2-large-xlsr-53-arabic", + ModelCategory.MULTILINGUAL_STT, + "Arabic speech recognition with Wav2Vec2 architecture", + True, + False, + 16000, + False, + ), + HFModel( + "SpeechT5 ASR", + "microsoft/speecht5_asr", + ModelCategory.ADVANCED_STT, + "Advanced automatic speech recognition", + True, + False, + 16000, + False, + ), + # Real-time Translation and Voice Conversion + HFModel( + "SeamlessM4T", + "facebook/seamless-m4t-v2-large", + ModelCategory.REAL_TIME_TRANSLATION, + "Multilingual speech-to-speech translation", + True, + False, + 16000, + True, + ), + HFModel( + "Voice Conversion VITS", + "jaywalnut310/vits-ljs", + ModelCategory.VOICE_CONVERSION, + "High-quality voice conversion and synthesis", + True, + False, + 22050, + False, + ), + HFModel( + "RVC Voice Clone", + "lj1995/GPT-SoVITS", + ModelCategory.VOICE_CLONING, + "Real-time voice cloning and conversion", + True, + False, + 32000, + True, + ), + ] + + # Talking Avatar and Video Generation Models + TALKING_AVATAR_MODELS = [ + # Talking Head Generation + HFModel( + "SadTalker Talking Head", + "vinthony/SadTalker", + ModelCategory.TALKING_AVATAR, + "Generate talking head videos from audio and single image", + True, + False, + 256, + False, + ), + HFModel( + "Real-Time Face Animation", + "PaddlePaddle/PaddleGAN-FOM", + ModelCategory.FACIAL_ANIMATION, + "Real-time facial animation and expression control", + True, + False, + 256, + True, + ), + HFModel( + "LivePortrait Animation", + "KwaiVGI/LivePortrait", + ModelCategory.TALKING_AVATAR, + "High-quality portrait animation with lip sync", + True, + False, + 512, + False, + ), + HFModel( + "DualTalker Video", + "OpenTalker/DualTalker", + ModelCategory.TALKING_AVATAR, + "Dual-modal talking face generation with enhanced quality", + True, + False, + 256, + False, + ), + HFModel( + "Video Retalking", + "vinthony/video-retalking", + ModelCategory.LIP_SYNC, + "Audio-driven lip sync for existing videos", + True, + False, + 224, + False, + ), + HFModel( + "Wav2Lip Lip Sync", + "Rudrabha/Wav2Lip", + ModelCategory.LIP_SYNC, + "Accurate lip sync generation from audio", + True, + False, + 96, + False, + ), + HFModel( + "Digital Human Avatar", + "modelscope/damo-text-to-video-synthesis", + ModelCategory.VIRTUAL_PRESENTER, + "Generate digital human presenter videos", + True, + False, + 320, + False, + ), + HFModel( + "AI News Anchor", + "microsoft/DiT-XL-2-256", + ModelCategory.AI_ANCHOR, + "Professional AI news anchor and presenter generation", + True, + False, + 256, + False, + ), + HFModel( + "Avatar Gesture Control", + "ZhengPeng7/BiSeNet", + ModelCategory.GESTURE_GENERATION, + "Generate natural gestures and body language for avatars", + True, + False, + 512, + False, + ), + ] + + # Interactive Language Models (English-Arabic Focus) + INTERACTIVE_LANGUAGE_MODELS = [ + # Bilingual Conversation Models + HFModel( + "AceGPT Arabic-English", + "FreedomIntelligence/AceGPT-13B", + ModelCategory.BILINGUAL_CONVERSATION, + "Bilingual Arabic-English conversation model", + True, + False, + 4096, + True, + ), + HFModel( + "Jais Arabic Chat", + "core42/jais-13b-chat", + ModelCategory.BILINGUAL_CONVERSATION, + "Advanced Arabic conversation model with English support", + True, + False, + 2048, + True, + ), + HFModel( + "AraBART Conversational", + "aubmindlab/arabart-base-conversational", + ModelCategory.BILINGUAL_CONVERSATION, + "Arabic conversational AI with cultural understanding", + True, + False, + 1024, + True, + ), + HFModel( + "Multilingual Chat Assistant", + "microsoft/DialoGPT-large", + ModelCategory.INTERACTIVE_CHAT, + "Interactive chat assistant supporting multiple languages", + True, + False, + 1024, + True, + ), + HFModel( + "Cultural Context Chat", + "bigscience/bloom-7b1", + ModelCategory.CULTURAL_ADAPTATION, + "Culturally aware conversation model for diverse contexts", + True, + False, + 2048, + True, + ), + HFModel( + "Context-Aware Assistant", + "microsoft/GODEL-v1_1-large-seq2seq", + ModelCategory.CONTEXT_AWARE_CHAT, + "Context-aware conversational AI with memory", + True, + False, + 1024, + True, + ), + HFModel( + "Personality Chat Bot", + "microsoft/PersonaGPT", + ModelCategory.PERSONALITY_CHAT, + "Personality-driven conversational AI with distinct characters", + True, + False, + 1024, + True, + ), + HFModel( + "Role-Play Assistant", + "PygmalionAI/pygmalion-6b", + ModelCategory.ROLE_PLAY_CHAT, + "Interactive role-playing conversation model", + True, + False, + 2048, + True, + ), + HFModel( + "Domain Expert Chat", + "microsoft/DialoGPT-medium", + ModelCategory.DOMAIN_SPECIFIC_CHAT, + "Specialized domain conversation assistant", + True, + False, + 1024, + True, + ), + # Arabic Language Specialists + HFModel( + "Arabic GPT-J", + "aubmindlab/aragpt2-base", + ModelCategory.BILINGUAL_CONVERSATION, + "Arabic language generation and conversation", + True, + False, + 1024, + True, + ), + HFModel( + "Marbert Arabic Chat", + "UBC-NLP/MARBERT", + ModelCategory.BILINGUAL_CONVERSATION, + "Dialectal Arabic conversation model", + True, + False, + 512, + False, + ), + HFModel( + "ArabicBERT Chat", + "aubmindlab/bert-base-arabertv2", + ModelCategory.BILINGUAL_CONVERSATION, + "Modern Standard Arabic conversational understanding", + True, + False, + 512, + False, + ), + ] + + +class HuggingFaceInference: + """Hugging Face Inference API integration""" + + def __init__( + self, + api_token: str, + base_url: str = "https://api-inference.huggingface.co/models/", + ): + self.api_token = api_token + self.base_url = base_url + self.session = None + + async def __aenter__(self): + self.session = aiohttp.ClientSession( + headers={"Authorization": f"Bearer {self.api_token}"}, + timeout=aiohttp.ClientTimeout(total=300), # 5 minutes timeout + ) + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb): + if self.session: + await self.session.close() + + async def text_generation( + self, + model_id: str, + prompt: str, + max_tokens: int = 100, + temperature: float = 0.7, + stream: bool = False, + **kwargs, + ) -> Dict[str, Any]: + """Generate text using a text generation model""" + payload = { + "inputs": prompt, + "parameters": { + "max_new_tokens": max_tokens, + "temperature": temperature, + "do_sample": True, + **kwargs, + }, + "options": {"use_cache": False}, + } + + if stream: + return await self._stream_request(model_id, payload) + else: + return await self._request(model_id, payload) + + async def text_to_image( + self, + model_id: str, + prompt: str, + negative_prompt: Optional[str] = None, + **kwargs, + ) -> bytes: + """Generate image from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + **({"negative_prompt": negative_prompt} if negative_prompt else {}), + **kwargs, + }, + } + + response = await self._request(model_id, payload, expect_json=False) + return response + + async def automatic_speech_recognition( + self, model_id: str, audio_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Transcribe audio to text""" + # Convert audio bytes to base64 for API + audio_b64 = base64.b64encode(audio_data).decode() + + payload = {"inputs": audio_b64, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def text_to_speech(self, model_id: str, text: str, **kwargs) -> bytes: + """Convert text to speech audio""" + payload = {"inputs": text, "parameters": kwargs} + + response = await self._request(model_id, payload, expect_json=False) + return response + + async def image_classification( + self, model_id: str, image_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Classify images""" + # Convert image to base64 + image_b64 = base64.b64encode(image_data).decode() + + payload = {"inputs": image_b64, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def feature_extraction( + self, model_id: str, texts: Union[str, List[str]], **kwargs + ) -> Dict[str, Any]: + """Extract embeddings from text""" + payload = {"inputs": texts, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def translation( + self, + model_id: str, + text: str, + src_lang: Optional[str] = None, + tgt_lang: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Translate text between languages""" + payload = { + "inputs": text, + "parameters": { + **({"src_lang": src_lang} if src_lang else {}), + **({"tgt_lang": tgt_lang} if tgt_lang else {}), + **kwargs, + }, + } + + return await self._request(model_id, payload) + + async def summarization( + self, + model_id: str, + text: str, + max_length: int = 150, + min_length: int = 30, + **kwargs, + ) -> Dict[str, Any]: + """Summarize text""" + payload = { + "inputs": text, + "parameters": { + "max_length": max_length, + "min_length": min_length, + **kwargs, + }, + } + + return await self._request(model_id, payload) + + async def question_answering( + self, model_id: str, question: str, context: str, **kwargs + ) -> Dict[str, Any]: + """Answer questions based on context""" + payload = { + "inputs": {"question": question, "context": context}, + "parameters": kwargs, + } + + return await self._request(model_id, payload) + + async def zero_shot_classification( + self, model_id: str, text: str, candidate_labels: List[str], **kwargs + ) -> Dict[str, Any]: + """Classify text without training data""" + payload = { + "inputs": text, + "parameters": {"candidate_labels": candidate_labels, **kwargs}, + } + + return await self._request(model_id, payload) + + async def conversational( + self, + model_id: str, + text: str, + conversation_history: Optional[List[Dict[str, str]]] = None, + **kwargs, + ) -> Dict[str, Any]: + """Have a conversation with a model""" + payload = { + "inputs": { + "text": text, + **( + { + "past_user_inputs": [ + h["user"] for h in conversation_history if "user" in h + ] + } + if conversation_history + else {} + ), + **( + { + "generated_responses": [ + h["bot"] for h in conversation_history if "bot" in h + ] + } + if conversation_history + else {} + ), + }, + "parameters": kwargs, + } + + return await self._request(model_id, payload) + + async def _request( + self, model_id: str, payload: Dict[str, Any], expect_json: bool = True + ) -> Union[Dict[str, Any], bytes]: + """Make HTTP request to Hugging Face API""" + url = f"{self.base_url}{model_id}" + + try: + async with self.session.post(url, json=payload) as response: + if response.status == 200: + if expect_json: + return await response.json() + else: + return await response.read() + elif response.status == 503: + # Model is loading, wait and retry + error_info = await response.json() + estimated_time = error_info.get("estimated_time", 30) + logger.info( + f"Model {model_id} is loading, waiting {estimated_time}s" + ) + await asyncio.sleep(min(estimated_time, 60)) # Cap at 60 seconds + return await self._request(model_id, payload, expect_json) + else: + error_text = await response.text() + raise Exception( + f"API request failed with status {response.status}: {error_text}" + ) + + except Exception as e: + logger.error(f"Error calling Hugging Face API for {model_id}: {e}") + raise + + async def _stream_request(self, model_id: str, payload: Dict[str, Any]): + """Stream response from Hugging Face API""" + url = f"{self.base_url}{model_id}" + payload["stream"] = True + + try: + async with self.session.post(url, json=payload) as response: + if response.status == 200: + async for chunk in response.content: + if chunk: + yield chunk.decode("utf-8") + else: + error_text = await response.text() + raise Exception( + f"Streaming request failed with status {response.status}: {error_text}" + ) + + except Exception as e: + logger.error(f"Error streaming from Hugging Face API for {model_id}: {e}") + raise + + # New methods for expanded model categories + + async def text_to_video( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate video from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + "duration": kwargs.get("duration", 5), + "fps": kwargs.get("fps", 24), + "width": kwargs.get("width", 512), + "height": kwargs.get("height", 512), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def video_to_text( + self, model_id: str, video_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Analyze video and generate text description""" + video_b64 = base64.b64encode(video_data).decode() + payload = { + "inputs": {"video": video_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def code_generation( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate code from natural language prompt""" + payload = { + "inputs": prompt, + "parameters": { + "max_length": kwargs.get("max_length", 500), + "temperature": kwargs.get("temperature", 0.2), + "language": kwargs.get("language", "python"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def code_completion( + self, model_id: str, code: str, **kwargs + ) -> Dict[str, Any]: + """Complete partial code""" + payload = { + "inputs": code, + "parameters": { + "max_length": kwargs.get("max_length", 100), + "temperature": kwargs.get("temperature", 0.1), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def text_to_3d(self, model_id: str, prompt: str, **kwargs) -> Dict[str, Any]: + """Generate 3D model from text description""" + payload = { + "inputs": prompt, + "parameters": { + "resolution": kwargs.get("resolution", 64), + "format": kwargs.get("format", "obj"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def image_to_3d( + self, model_id: str, image_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Generate 3D model from image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def ocr(self, model_id: str, image_data: bytes, **kwargs) -> Dict[str, Any]: + """Perform optical character recognition on image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": {"language": kwargs.get("language", "en"), **kwargs}, + } + return await self._request(model_id, payload) + + async def document_analysis( + self, model_id: str, document_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Analyze document structure and content""" + doc_b64 = base64.b64encode(document_data).decode() + payload = { + "inputs": {"document": doc_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def vision_language( + self, model_id: str, image_data: bytes, text: str, **kwargs + ) -> Dict[str, Any]: + """Process image and text together""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64, "text": text}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def multimodal_reasoning( + self, model_id: str, inputs: Dict[str, Any], **kwargs + ) -> Dict[str, Any]: + """Perform reasoning across multiple modalities""" + payload = { + "inputs": inputs, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def music_generation( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate music from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + "duration": kwargs.get("duration", 30), + "bpm": kwargs.get("bpm", 120), + "genre": kwargs.get("genre", "electronic"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def voice_cloning( + self, model_id: str, text: str, voice_sample: bytes, **kwargs + ) -> bytes: + """Clone voice and synthesize speech""" + voice_b64 = base64.b64encode(voice_sample).decode() + payload = { + "inputs": {"text": text, "voice_sample": voice_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload, expect_json=False) + + async def super_resolution( + self, model_id: str, image_data: bytes, **kwargs + ) -> bytes: + """Enhance image resolution""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": {"scale_factor": kwargs.get("scale_factor", 4), **kwargs}, + } + return await self._request(model_id, payload, expect_json=False) + + async def background_removal( + self, model_id: str, image_data: bytes, **kwargs + ) -> bytes: + """Remove background from image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload, expect_json=False) + + async def creative_writing( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate creative content""" + payload = { + "inputs": prompt, + "parameters": { + "max_length": kwargs.get("max_length", 1000), + "creativity": kwargs.get("creativity", 0.8), + "genre": kwargs.get("genre", "general"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def business_document( + self, model_id: str, document_type: str, context: str, **kwargs + ) -> Dict[str, Any]: + """Generate business documents""" + payload = { + "inputs": f"Generate {document_type}: {context}", + "parameters": { + "format": kwargs.get("format", "professional"), + "length": kwargs.get("length", "medium"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + +class HuggingFaceModelManager: + """Manager for all Hugging Face model operations""" + + def __init__(self, api_token: str): + self.api_token = api_token + self.models = HuggingFaceModels() + + def get_models_by_category(self, category: ModelCategory) -> List[HFModel]: + """Get all models for a specific category""" + all_models = [] + + if category == ModelCategory.TEXT_GENERATION: + all_models = self.models.TEXT_GENERATION_MODELS + elif category == ModelCategory.TEXT_TO_IMAGE: + all_models = self.models.TEXT_TO_IMAGE_MODELS + elif category == ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: + all_models = self.models.ASR_MODELS + elif category == ModelCategory.TEXT_TO_SPEECH: + all_models = self.models.TTS_MODELS + elif category == ModelCategory.IMAGE_CLASSIFICATION: + all_models = self.models.IMAGE_CLASSIFICATION_MODELS + elif category == ModelCategory.FEATURE_EXTRACTION: + all_models = self.models.FEATURE_EXTRACTION_MODELS + elif category == ModelCategory.TRANSLATION: + all_models = self.models.TRANSLATION_MODELS + elif category == ModelCategory.SUMMARIZATION: + all_models = self.models.SUMMARIZATION_MODELS + + return all_models + + def get_all_models(self) -> Dict[ModelCategory, List[HFModel]]: + """Get all available models organized by category""" + return { + # Core AI categories + ModelCategory.TEXT_GENERATION: self.models.TEXT_GENERATION_MODELS, + ModelCategory.TEXT_TO_IMAGE: self.models.TEXT_TO_IMAGE_MODELS, + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: self.models.ASR_MODELS, + ModelCategory.TEXT_TO_SPEECH: self.models.TTS_MODELS, + ModelCategory.IMAGE_CLASSIFICATION: self.models.IMAGE_CLASSIFICATION_MODELS, + ModelCategory.FEATURE_EXTRACTION: self.models.FEATURE_EXTRACTION_MODELS, + ModelCategory.TRANSLATION: self.models.TRANSLATION_MODELS, + ModelCategory.SUMMARIZATION: self.models.SUMMARIZATION_MODELS, + # Video and Motion + ModelCategory.TEXT_TO_VIDEO: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_GENERATION: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_TO_TEXT: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_CLASSIFICATION: self.models.VIDEO_GENERATION_MODELS, + # Code and Development + ModelCategory.CODE_GENERATION: self.models.CODE_GENERATION_MODELS, + ModelCategory.CODE_COMPLETION: self.models.CODE_GENERATION_MODELS, + ModelCategory.CODE_EXPLANATION: self.models.CODE_GENERATION_MODELS, + ModelCategory.APP_GENERATION: self.models.CODE_GENERATION_MODELS, + # 3D and AR/VR + ModelCategory.TEXT_TO_3D: self.models.THREE_D_MODELS, + ModelCategory.IMAGE_TO_3D: self.models.THREE_D_MODELS, + ModelCategory.THREE_D_GENERATION: self.models.THREE_D_MODELS, + ModelCategory.MESH_GENERATION: self.models.THREE_D_MODELS, + # Document Processing + ModelCategory.OCR: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.DOCUMENT_ANALYSIS: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.HANDWRITING_RECOGNITION: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.TABLE_EXTRACTION: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.FORM_PROCESSING: self.models.DOCUMENT_PROCESSING_MODELS, + # Multimodal AI + ModelCategory.VISION_LANGUAGE: self.models.MULTIMODAL_MODELS, + ModelCategory.MULTIMODAL_REASONING: self.models.MULTIMODAL_MODELS, + ModelCategory.VISUAL_QUESTION_ANSWERING: self.models.MULTIMODAL_MODELS, + ModelCategory.MULTIMODAL_CHAT: self.models.MULTIMODAL_MODELS, + ModelCategory.CROSS_MODAL_GENERATION: self.models.MULTIMODAL_MODELS, + # Specialized AI + ModelCategory.MUSIC_GENERATION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.VOICE_CLONING: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.SUPER_RESOLUTION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.FACE_RESTORATION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.IMAGE_INPAINTING: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.BACKGROUND_REMOVAL: self.models.SPECIALIZED_AI_MODELS, + # Creative Content + ModelCategory.CREATIVE_WRITING: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.STORY_GENERATION: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.POETRY_GENERATION: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.BLOG_WRITING: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.MARKETING_COPY: self.models.CREATIVE_CONTENT_MODELS, + # Game Development + ModelCategory.GAME_ASSET_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.CHARACTER_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.LEVEL_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.DIALOGUE_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + # Science and Research + ModelCategory.PROTEIN_FOLDING: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.MOLECULE_GENERATION: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.SCIENTIFIC_WRITING: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.RESEARCH_ASSISTANCE: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.DATA_ANALYSIS: self.models.SCIENCE_RESEARCH_MODELS, + # Business and Productivity + ModelCategory.EMAIL_GENERATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.PRESENTATION_CREATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.REPORT_GENERATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.MEETING_SUMMARIZATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.PROJECT_PLANNING: self.models.BUSINESS_PRODUCTIVITY_MODELS, + # AI Teacher and Education Models + ModelCategory.AI_TUTORING: self.models.AI_TEACHER_MODELS, + ModelCategory.EDUCATIONAL_CONTENT: self.models.AI_TEACHER_MODELS, + ModelCategory.LESSON_PLANNING: self.models.AI_TEACHER_MODELS, + ModelCategory.CONCEPT_EXPLANATION: self.models.AI_TEACHER_MODELS, + ModelCategory.HOMEWORK_ASSISTANCE: self.models.AI_TEACHER_MODELS, + ModelCategory.QUIZ_GENERATION: self.models.AI_TEACHER_MODELS, + ModelCategory.CURRICULUM_DESIGN: self.models.AI_TEACHER_MODELS, + ModelCategory.LEARNING_ASSESSMENT: self.models.AI_TEACHER_MODELS, + ModelCategory.ADAPTIVE_LEARNING: self.models.AI_TEACHER_MODELS, + ModelCategory.SUBJECT_TEACHING: self.models.AI_TEACHER_MODELS, + ModelCategory.MATH_TUTORING: self.models.AI_TEACHER_MODELS, + ModelCategory.SCIENCE_TUTORING: self.models.AI_TEACHER_MODELS, + ModelCategory.LANGUAGE_TUTORING: self.models.AI_TEACHER_MODELS, + ModelCategory.HISTORY_TUTORING: self.models.AI_TEACHER_MODELS, + ModelCategory.CODING_INSTRUCTION: self.models.AI_TEACHER_MODELS, + ModelCategory.EXAM_PREPARATION: self.models.AI_TEACHER_MODELS, + ModelCategory.STUDY_GUIDE_CREATION: self.models.AI_TEACHER_MODELS, + ModelCategory.EDUCATIONAL_GAMES: self.models.AI_TEACHER_MODELS, + ModelCategory.LEARNING_ANALYTICS: self.models.AI_TEACHER_MODELS, + ModelCategory.PERSONALIZED_LEARNING: self.models.AI_TEACHER_MODELS, + # Qwen Models + ModelCategory.QWEN_REASONING: self.models.QWEN_MODELS, + ModelCategory.QWEN_MATH: self.models.QWEN_MODELS, + ModelCategory.QWEN_CODE: self.models.QWEN_MODELS, + ModelCategory.QWEN_VISION: self.models.QWEN_MODELS, + ModelCategory.QWEN_AUDIO: self.models.QWEN_MODELS, + # DeepSeek Models + ModelCategory.DEEPSEEK_CODING: self.models.DEEPSEEK_MODELS, + ModelCategory.DEEPSEEK_REASONING: self.models.DEEPSEEK_MODELS, + ModelCategory.DEEPSEEK_MATH: self.models.DEEPSEEK_MODELS, + ModelCategory.DEEPSEEK_RESEARCH: self.models.DEEPSEEK_MODELS, + # Advanced Image Processing & Manipulation + ModelCategory.IMAGE_EDITING: self.models.IMAGE_EDITING_MODELS, + ModelCategory.FACE_SWAP: self.models.FACE_SWAP_MODELS, + ModelCategory.FACE_ENHANCEMENT: self.models.FACE_SWAP_MODELS, + ModelCategory.FACE_GENERATION: self.models.FACE_SWAP_MODELS, + ModelCategory.PORTRAIT_EDITING: self.models.IMAGE_EDITING_MODELS, + ModelCategory.PHOTO_RESTORATION: self.models.IMAGE_EDITING_MODELS, + ModelCategory.IMAGE_UPSCALING: self.models.IMAGE_EDITING_MODELS, + ModelCategory.COLOR_CORRECTION: self.models.IMAGE_EDITING_MODELS, + ModelCategory.ARTISTIC_FILTER: self.models.IMAGE_EDITING_MODELS, + # Advanced Speech & Audio + ModelCategory.ADVANCED_TTS: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.ADVANCED_STT: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.VOICE_CONVERSION: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.SPEECH_ENHANCEMENT: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.AUDIO_GENERATION: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.MULTILINGUAL_TTS: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.MULTILINGUAL_STT: self.models.ADVANCED_SPEECH_MODELS, + ModelCategory.REAL_TIME_TRANSLATION: self.models.ADVANCED_SPEECH_MODELS, + # Interactive Avatar & Video Generation + ModelCategory.TALKING_AVATAR: self.models.TALKING_AVATAR_MODELS, + ModelCategory.AVATAR_GENERATION: self.models.TALKING_AVATAR_MODELS, + ModelCategory.LIP_SYNC: self.models.TALKING_AVATAR_MODELS, + ModelCategory.FACIAL_ANIMATION: self.models.TALKING_AVATAR_MODELS, + ModelCategory.GESTURE_GENERATION: self.models.TALKING_AVATAR_MODELS, + ModelCategory.VIRTUAL_PRESENTER: self.models.TALKING_AVATAR_MODELS, + ModelCategory.AI_ANCHOR: self.models.TALKING_AVATAR_MODELS, + # Interactive Language & Conversation + ModelCategory.INTERACTIVE_CHAT: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.BILINGUAL_CONVERSATION: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.CULTURAL_ADAPTATION: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.CONTEXT_AWARE_CHAT: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.PERSONALITY_CHAT: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.ROLE_PLAY_CHAT: self.models.INTERACTIVE_LANGUAGE_MODELS, + ModelCategory.DOMAIN_SPECIFIC_CHAT: self.models.INTERACTIVE_LANGUAGE_MODELS, + } + + def get_model_by_id(self, model_id: str) -> Optional[HFModel]: + """Find a model by its Hugging Face model ID""" + for models_list in self.get_all_models().values(): + for model in models_list: + if model.model_id == model_id: + return model + return None + + async def call_model(self, model_id: str, category: ModelCategory, **kwargs) -> Any: + """Call a Hugging Face model with the appropriate method based on category""" + + async with HuggingFaceInference(self.api_token) as hf: + if category == ModelCategory.TEXT_GENERATION: + return await hf.text_generation(model_id, **kwargs) + elif category == ModelCategory.TEXT_TO_IMAGE: + return await hf.text_to_image(model_id, **kwargs) + elif category == ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: + return await hf.automatic_speech_recognition(model_id, **kwargs) + elif category == ModelCategory.TEXT_TO_SPEECH: + return await hf.text_to_speech(model_id, **kwargs) + elif category == ModelCategory.IMAGE_CLASSIFICATION: + return await hf.image_classification(model_id, **kwargs) + elif category == ModelCategory.FEATURE_EXTRACTION: + return await hf.feature_extraction(model_id, **kwargs) + elif category == ModelCategory.TRANSLATION: + return await hf.translation(model_id, **kwargs) + elif category == ModelCategory.SUMMARIZATION: + return await hf.summarization(model_id, **kwargs) + elif category == ModelCategory.QUESTION_ANSWERING: + return await hf.question_answering(model_id, **kwargs) + elif category == ModelCategory.ZERO_SHOT_CLASSIFICATION: + return await hf.zero_shot_classification(model_id, **kwargs) + elif category == ModelCategory.CONVERSATIONAL: + return await hf.conversational(model_id, **kwargs) + + # Video and Motion categories + elif category in [ + ModelCategory.TEXT_TO_VIDEO, + ModelCategory.VIDEO_GENERATION, + ]: + return await hf.text_to_video(model_id, **kwargs) + elif category == ModelCategory.VIDEO_TO_TEXT: + return await hf.video_to_text(model_id, **kwargs) + elif category == ModelCategory.VIDEO_CLASSIFICATION: + return await hf.image_classification( + model_id, **kwargs + ) # Similar to image classification + + # Code and Development categories + elif category in [ + ModelCategory.CODE_GENERATION, + ModelCategory.APP_GENERATION, + ]: + return await hf.code_generation(model_id, **kwargs) + elif category in [ + ModelCategory.CODE_COMPLETION, + ModelCategory.CODE_EXPLANATION, + ]: + return await hf.code_completion(model_id, **kwargs) + + # 3D and AR/VR categories + elif category in [ + ModelCategory.TEXT_TO_3D, + ModelCategory.THREE_D_GENERATION, + ]: + return await hf.text_to_3d(model_id, **kwargs) + elif category in [ModelCategory.IMAGE_TO_3D, ModelCategory.MESH_GENERATION]: + return await hf.image_to_3d(model_id, **kwargs) + + # Document Processing categories + elif category == ModelCategory.OCR: + return await hf.ocr(model_id, **kwargs) + elif category in [ + ModelCategory.DOCUMENT_ANALYSIS, + ModelCategory.FORM_PROCESSING, + ModelCategory.TABLE_EXTRACTION, + ModelCategory.LAYOUT_ANALYSIS, + ]: + return await hf.document_analysis(model_id, **kwargs) + elif category == ModelCategory.HANDWRITING_RECOGNITION: + return await hf.ocr(model_id, **kwargs) # Similar to OCR + + # Multimodal AI categories + elif category in [ + ModelCategory.VISION_LANGUAGE, + ModelCategory.VISUAL_QUESTION_ANSWERING, + ModelCategory.IMAGE_TEXT_MATCHING, + ]: + return await hf.vision_language(model_id, **kwargs) + elif category in [ + ModelCategory.MULTIMODAL_REASONING, + ModelCategory.MULTIMODAL_CHAT, + ModelCategory.CROSS_MODAL_GENERATION, + ]: + return await hf.multimodal_reasoning(model_id, **kwargs) + + # Specialized AI categories + elif category == ModelCategory.MUSIC_GENERATION: + return await hf.music_generation(model_id, **kwargs) + elif category == ModelCategory.VOICE_CLONING: + return await hf.voice_cloning(model_id, **kwargs) + elif category == ModelCategory.SUPER_RESOLUTION: + return await hf.super_resolution(model_id, **kwargs) + elif category in [ + ModelCategory.FACE_RESTORATION, + ModelCategory.IMAGE_INPAINTING, + ModelCategory.IMAGE_OUTPAINTING, + ]: + return await hf.super_resolution( + model_id, **kwargs + ) # Similar processing + elif category == ModelCategory.BACKGROUND_REMOVAL: + return await hf.background_removal(model_id, **kwargs) + + # Creative Content categories + elif category in [ + ModelCategory.CREATIVE_WRITING, + ModelCategory.STORY_GENERATION, + ModelCategory.POETRY_GENERATION, + ModelCategory.SCREENPLAY_WRITING, + ]: + return await hf.creative_writing(model_id, **kwargs) + elif category in [ModelCategory.BLOG_WRITING, ModelCategory.MARKETING_COPY]: + return await hf.text_generation( + model_id, **kwargs + ) # Use standard text generation + + # Game Development categories + elif category in [ + ModelCategory.CHARACTER_GENERATION, + ModelCategory.LEVEL_GENERATION, + ModelCategory.DIALOGUE_GENERATION, + ModelCategory.GAME_ASSET_GENERATION, + ]: + return await hf.creative_writing( + model_id, **kwargs + ) # Creative generation + + # Science and Research categories + elif category in [ + ModelCategory.PROTEIN_FOLDING, + ModelCategory.MOLECULE_GENERATION, + ]: + return await hf.text_generation( + model_id, **kwargs + ) # Specialized text generation + elif category in [ + ModelCategory.SCIENTIFIC_WRITING, + ModelCategory.RESEARCH_ASSISTANCE, + ModelCategory.DATA_ANALYSIS, + ]: + return await hf.text_generation(model_id, **kwargs) + + # Business and Productivity categories + elif category in [ + ModelCategory.EMAIL_GENERATION, + ModelCategory.PRESENTATION_CREATION, + ModelCategory.REPORT_GENERATION, + ModelCategory.MEETING_SUMMARIZATION, + ModelCategory.PROJECT_PLANNING, + ]: + return await hf.business_document(model_id, category.value, **kwargs) + + # AI Teacher and Education categories + elif category in [ + ModelCategory.AI_TUTORING, + ModelCategory.EDUCATIONAL_CONTENT, + ModelCategory.LESSON_PLANNING, + ModelCategory.CONCEPT_EXPLANATION, + ModelCategory.HOMEWORK_ASSISTANCE, + ModelCategory.QUIZ_GENERATION, + ModelCategory.CURRICULUM_DESIGN, + ModelCategory.LEARNING_ASSESSMENT, + ModelCategory.ADAPTIVE_LEARNING, + ModelCategory.SUBJECT_TEACHING, + ModelCategory.MATH_TUTORING, + ModelCategory.SCIENCE_TUTORING, + ModelCategory.LANGUAGE_TUTORING, + ModelCategory.HISTORY_TUTORING, + ModelCategory.CODING_INSTRUCTION, + ModelCategory.EXAM_PREPARATION, + ModelCategory.STUDY_GUIDE_CREATION, + ModelCategory.EDUCATIONAL_GAMES, + ModelCategory.LEARNING_ANALYTICS, + ModelCategory.PERSONALIZED_LEARNING, + ]: + return await hf.text_generation( + model_id, **kwargs + ) # Educational content generation + + # Qwen Model categories + elif category in [ + ModelCategory.QWEN_REASONING, + ModelCategory.QWEN_MATH, + ModelCategory.QWEN_CODE, + ]: + return await hf.text_generation(model_id, **kwargs) + elif category == ModelCategory.QWEN_VISION: + return await hf.vision_language(model_id, **kwargs) + elif category == ModelCategory.QWEN_AUDIO: + return await hf.automatic_speech_recognition(model_id, **kwargs) + + # DeepSeek Model categories + elif category in [ + ModelCategory.DEEPSEEK_CODING, + ModelCategory.DEEPSEEK_REASONING, + ModelCategory.DEEPSEEK_MATH, + ModelCategory.DEEPSEEK_RESEARCH, + ]: + return await hf.text_generation(model_id, **kwargs) + + # Advanced Image Processing & Manipulation + elif category in [ + ModelCategory.IMAGE_EDITING, + ModelCategory.PORTRAIT_EDITING, + ModelCategory.PHOTO_RESTORATION, + ModelCategory.COLOR_CORRECTION, + ModelCategory.ARTISTIC_FILTER, + ]: + return await hf.text_to_image(model_id, **kwargs) # Image processing + elif category == ModelCategory.IMAGE_UPSCALING: + return await hf.super_resolution(model_id, **kwargs) + elif category in [ + ModelCategory.FACE_SWAP, + ModelCategory.FACE_ENHANCEMENT, + ModelCategory.FACE_GENERATION, + ]: + return await hf.text_to_image(model_id, **kwargs) # Face manipulation + + # Advanced Speech & Audio + elif category in [ + ModelCategory.ADVANCED_TTS, + ModelCategory.MULTILINGUAL_TTS, + ModelCategory.VOICE_CONVERSION, + ]: + return await hf.text_to_speech(model_id, **kwargs) + elif category in [ + ModelCategory.ADVANCED_STT, + ModelCategory.MULTILINGUAL_STT, + ModelCategory.SPEECH_ENHANCEMENT, + ]: + return await hf.automatic_speech_recognition(model_id, **kwargs) + elif category in [ + ModelCategory.AUDIO_GENERATION, + ModelCategory.REAL_TIME_TRANSLATION, + ]: + return await hf.text_to_speech(model_id, **kwargs) # Audio generation + + # Interactive Avatar & Video Generation + elif category in [ + ModelCategory.TALKING_AVATAR, + ModelCategory.AVATAR_GENERATION, + ModelCategory.LIP_SYNC, + ModelCategory.FACIAL_ANIMATION, + ModelCategory.GESTURE_GENERATION, + ModelCategory.VIRTUAL_PRESENTER, + ModelCategory.AI_ANCHOR, + ]: + return await hf.text_to_video(model_id, **kwargs) # Video generation + + # Interactive Language & Conversation + elif category in [ + ModelCategory.INTERACTIVE_CHAT, + ModelCategory.BILINGUAL_CONVERSATION, + ModelCategory.CULTURAL_ADAPTATION, + ModelCategory.CONTEXT_AWARE_CHAT, + ModelCategory.PERSONALITY_CHAT, + ModelCategory.ROLE_PLAY_CHAT, + ModelCategory.DOMAIN_SPECIFIC_CHAT, + ]: + return await hf.conversational(model_id, **kwargs) + + else: + raise ValueError(f"Unsupported model category: {category}") diff --git a/app/huggingface_models_backup.py b/app/huggingface_models_backup.py new file mode 100644 index 0000000000000000000000000000000000000000..170a18dd92b3d5a4c7db734ea663ddc0d86329d6 --- /dev/null +++ b/app/huggingface_models_backup.py @@ -0,0 +1,2237 @@ +""" +Hugging Face Models Integration for OpenManus AI Agent +Comprehensive integration with Hugging Face Inference API for all model categories +""" + +import asyncio +import base64 +import io +import json +import logging +from dataclasses import dataclass +from enum import Enum +from typing import Any, Dict, List, Optional, Union + +import aiohttp +import PIL.Image +from pydantic import BaseModel + +logger = logging.getLogger(__name__) + + +class ModelCategory(Enum): + """Categories of Hugging Face models available""" + + # Core AI categories + TEXT_GENERATION = "text-generation" + TEXT_TO_IMAGE = "text-to-image" + IMAGE_TO_TEXT = "image-to-text" + AUTOMATIC_SPEECH_RECOGNITION = "automatic-speech-recognition" + TEXT_TO_SPEECH = "text-to-speech" + IMAGE_CLASSIFICATION = "image-classification" + OBJECT_DETECTION = "object-detection" + FEATURE_EXTRACTION = "feature-extraction" + SENTENCE_SIMILARITY = "sentence-similarity" + TRANSLATION = "translation" + SUMMARIZATION = "summarization" + QUESTION_ANSWERING = "question-answering" + FILL_MASK = "fill-mask" + TOKEN_CLASSIFICATION = "token-classification" + ZERO_SHOT_CLASSIFICATION = "zero-shot-classification" + AUDIO_CLASSIFICATION = "audio-classification" + CONVERSATIONAL = "conversational" + + # Video and Motion + TEXT_TO_VIDEO = "text-to-video" + VIDEO_TO_TEXT = "video-to-text" + VIDEO_CLASSIFICATION = "video-classification" + VIDEO_GENERATION = "video-generation" + MOTION_GENERATION = "motion-generation" + DEEPFAKE_DETECTION = "deepfake-detection" + + # Code and Development + CODE_GENERATION = "code-generation" + CODE_COMPLETION = "code-completion" + CODE_EXPLANATION = "code-explanation" + CODE_TRANSLATION = "code-translation" + CODE_REVIEW = "code-review" + APP_GENERATION = "app-generation" + API_GENERATION = "api-generation" + DATABASE_GENERATION = "database-generation" + + # 3D and AR/VR + TEXT_TO_3D = "text-to-3d" + IMAGE_TO_3D = "image-to-3d" + THREE_D_GENERATION = "3d-generation" + MESH_GENERATION = "mesh-generation" + TEXTURE_GENERATION = "texture-generation" + AR_CONTENT = "ar-content" + VR_ENVIRONMENT = "vr-environment" + + # Document Processing + OCR = "ocr" + DOCUMENT_ANALYSIS = "document-analysis" + PDF_PROCESSING = "pdf-processing" + LAYOUT_ANALYSIS = "layout-analysis" + TABLE_EXTRACTION = "table-extraction" + HANDWRITING_RECOGNITION = "handwriting-recognition" + FORM_PROCESSING = "form-processing" + + # Multimodal AI + VISION_LANGUAGE = "vision-language" + MULTIMODAL_REASONING = "multimodal-reasoning" + CROSS_MODAL_GENERATION = "cross-modal-generation" + VISUAL_QUESTION_ANSWERING = "visual-question-answering" + IMAGE_TEXT_MATCHING = "image-text-matching" + MULTIMODAL_CHAT = "multimodal-chat" + + # Specialized AI + MUSIC_GENERATION = "music-generation" + VOICE_CLONING = "voice-cloning" + STYLE_TRANSFER = "style-transfer" + SUPER_RESOLUTION = "super-resolution" + IMAGE_INPAINTING = "image-inpainting" + IMAGE_OUTPAINTING = "image-outpainting" + BACKGROUND_REMOVAL = "background-removal" + FACE_RESTORATION = "face-restoration" + + # Content Creation + CREATIVE_WRITING = "creative-writing" + STORY_GENERATION = "story-generation" + SCREENPLAY_WRITING = "screenplay-writing" + POETRY_GENERATION = "poetry-generation" + BLOG_WRITING = "blog-writing" + MARKETING_COPY = "marketing-copy" + + # Game Development + GAME_ASSET_GENERATION = "game-asset-generation" + CHARACTER_GENERATION = "character-generation" + LEVEL_GENERATION = "level-generation" + DIALOGUE_GENERATION = "dialogue-generation" + + # Science and Research + PROTEIN_FOLDING = "protein-folding" + MOLECULE_GENERATION = "molecule-generation" + SCIENTIFIC_WRITING = "scientific-writing" + RESEARCH_ASSISTANCE = "research-assistance" + DATA_ANALYSIS = "data-analysis" + + # Business and Productivity + EMAIL_GENERATION = "email-generation" + PRESENTATION_CREATION = "presentation-creation" + REPORT_GENERATION = "report-generation" + MEETING_SUMMARIZATION = "meeting-summarization" + PROJECT_PLANNING = "project-planning" + + # AI Teacher and Education + AI_TUTORING = "ai-tutoring" + EDUCATIONAL_CONTENT = "educational-content" + LESSON_PLANNING = "lesson-planning" + CONCEPT_EXPLANATION = "concept-explanation" + HOMEWORK_ASSISTANCE = "homework-assistance" + QUIZ_GENERATION = "quiz-generation" + CURRICULUM_DESIGN = "curriculum-design" + LEARNING_ASSESSMENT = "learning-assessment" + ADAPTIVE_LEARNING = "adaptive-learning" + SUBJECT_TEACHING = "subject-teaching" + MATH_TUTORING = "math-tutoring" + SCIENCE_TUTORING = "science-tutoring" + LANGUAGE_TUTORING = "language-tutoring" + HISTORY_TUTORING = "history-tutoring" + CODING_INSTRUCTION = "coding-instruction" + EXAM_PREPARATION = "exam-preparation" + STUDY_GUIDE_CREATION = "study-guide-creation" + EDUCATIONAL_GAMES = "educational-games" + LEARNING_ANALYTICS = "learning-analytics" + PERSONALIZED_LEARNING = "personalized-learning" + + +@dataclass +class HFModel: + """Hugging Face model definition""" + + name: str + model_id: str + category: ModelCategory + description: str + endpoint_compatible: bool = False + requires_auth: bool = False + max_tokens: Optional[int] = None + supports_streaming: bool = False + + +class HuggingFaceModels: + """Comprehensive collection of Hugging Face models for all categories""" + + # Text Generation Models (Latest and Popular) + TEXT_GENERATION_MODELS = [ + HFModel( + "MiniMax-M2", + "MiniMaxAI/MiniMax-M2", + ModelCategory.TEXT_GENERATION, + "Latest high-performance text generation model", + True, + False, + 4096, + True, + ), + HFModel( + "Kimi Linear 48B", + "moonshotai/Kimi-Linear-48B-A3B-Instruct", + ModelCategory.TEXT_GENERATION, + "Large instruction-tuned model with linear attention", + True, + False, + 8192, + True, + ), + HFModel( + "GPT-OSS 20B", + "openai/gpt-oss-20b", + ModelCategory.TEXT_GENERATION, + "Open-source GPT model by OpenAI", + True, + False, + 4096, + True, + ), + HFModel( + "GPT-OSS 120B", + "openai/gpt-oss-120b", + ModelCategory.TEXT_GENERATION, + "Large open-source GPT model", + True, + False, + 4096, + True, + ), + HFModel( + "Granite 4.0 1B", + "ibm-granite/granite-4.0-1b", + ModelCategory.TEXT_GENERATION, + "IBM's enterprise-grade small language model", + True, + False, + 2048, + True, + ), + HFModel( + "GLM-4.6", + "zai-org/GLM-4.6", + ModelCategory.TEXT_GENERATION, + "Multilingual conversational model", + True, + False, + 4096, + True, + ), + HFModel( + "Llama 3.1 8B Instruct", + "meta-llama/Llama-3.1-8B-Instruct", + ModelCategory.TEXT_GENERATION, + "Meta's instruction-tuned Llama model", + True, + True, + 8192, + True, + ), + HFModel( + "Tongyi DeepResearch 30B", + "Alibaba-NLP/Tongyi-DeepResearch-30B-A3B", + ModelCategory.TEXT_GENERATION, + "Alibaba's research-focused large language model", + True, + False, + 4096, + True, + ), + HFModel( + "EuroLLM 9B", + "utter-project/EuroLLM-9B", + ModelCategory.TEXT_GENERATION, + "European multilingual language model", + True, + False, + 4096, + True, + ), + ] + + # Text-to-Image Models (Latest and Best) + TEXT_TO_IMAGE_MODELS = [ + HFModel( + "FIBO", + "briaai/FIBO", + ModelCategory.TEXT_TO_IMAGE, + "Advanced text-to-image generation model", + True, + False, + ), + HFModel( + "FLUX.1 Dev", + "black-forest-labs/FLUX.1-dev", + ModelCategory.TEXT_TO_IMAGE, + "State-of-the-art image generation", + True, + False, + ), + HFModel( + "FLUX.1 Schnell", + "black-forest-labs/FLUX.1-schnell", + ModelCategory.TEXT_TO_IMAGE, + "Fast high-quality image generation", + True, + False, + ), + HFModel( + "Qwen Image", + "Qwen/Qwen-Image", + ModelCategory.TEXT_TO_IMAGE, + "Multilingual text-to-image model", + True, + False, + ), + HFModel( + "Stable Diffusion XL", + "stabilityai/stable-diffusion-xl-base-1.0", + ModelCategory.TEXT_TO_IMAGE, + "Popular high-resolution image generation", + True, + False, + ), + HFModel( + "Stable Diffusion 3.5 Large", + "stabilityai/stable-diffusion-3.5-large", + ModelCategory.TEXT_TO_IMAGE, + "Latest Stable Diffusion model", + True, + False, + ), + HFModel( + "HunyuanImage 3.0", + "tencent/HunyuanImage-3.0", + ModelCategory.TEXT_TO_IMAGE, + "Tencent's advanced image generation model", + True, + False, + ), + HFModel( + "Nitro-E", + "amd/Nitro-E", + ModelCategory.TEXT_TO_IMAGE, + "AMD's efficient image generation model", + True, + False, + ), + HFModel( + "Qwen Image Lightning", + "lightx2v/Qwen-Image-Lightning", + ModelCategory.TEXT_TO_IMAGE, + "Fast distilled image generation", + True, + False, + ), + ] + + # Automatic Speech Recognition Models + ASR_MODELS = [ + HFModel( + "Whisper Large v3", + "openai/whisper-large-v3", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "OpenAI's best multilingual speech recognition", + True, + False, + ), + HFModel( + "Whisper Large v3 Turbo", + "openai/whisper-large-v3-turbo", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Faster version of Whisper Large v3", + True, + False, + ), + HFModel( + "Parakeet TDT 0.6B v3", + "nvidia/parakeet-tdt-0.6b-v3", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "NVIDIA's multilingual ASR model", + True, + False, + ), + HFModel( + "Canary Qwen 2.5B", + "nvidia/canary-qwen-2.5b", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "NVIDIA's advanced ASR with Qwen integration", + True, + False, + ), + HFModel( + "Canary 1B v2", + "nvidia/canary-1b-v2", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Compact multilingual ASR model", + True, + False, + ), + HFModel( + "Whisper Small", + "openai/whisper-small", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Lightweight multilingual ASR", + True, + False, + ), + HFModel( + "Speaker Diarization 3.1", + "pyannote/speaker-diarization-3.1", + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + "Advanced speaker identification and diarization", + True, + False, + ), + ] + + # Text-to-Speech Models + TTS_MODELS = [ + HFModel( + "SoulX Podcast 1.7B", + "Soul-AILab/SoulX-Podcast-1.7B", + ModelCategory.TEXT_TO_SPEECH, + "High-quality podcast-style speech synthesis", + True, + False, + ), + HFModel( + "NeuTTS Air", + "neuphonic/neutts-air", + ModelCategory.TEXT_TO_SPEECH, + "Advanced neural text-to-speech", + True, + False, + ), + HFModel( + "Kokoro 82M", + "hexgrad/Kokoro-82M", + ModelCategory.TEXT_TO_SPEECH, + "Lightweight high-quality TTS", + True, + False, + ), + HFModel( + "Kani TTS 400M EN", + "nineninesix/kani-tts-400m-en", + ModelCategory.TEXT_TO_SPEECH, + "English-focused text-to-speech model", + True, + False, + ), + HFModel( + "XTTS v2", + "coqui/XTTS-v2", + ModelCategory.TEXT_TO_SPEECH, + "Zero-shot voice cloning TTS", + True, + False, + ), + HFModel( + "Chatterbox", + "ResembleAI/chatterbox", + ModelCategory.TEXT_TO_SPEECH, + "Multilingual voice cloning", + True, + False, + ), + HFModel( + "VibeVoice 1.5B", + "microsoft/VibeVoice-1.5B", + ModelCategory.TEXT_TO_SPEECH, + "Microsoft's advanced TTS model", + True, + False, + ), + HFModel( + "OpenAudio S1 Mini", + "fishaudio/openaudio-s1-mini", + ModelCategory.TEXT_TO_SPEECH, + "Compact multilingual TTS", + True, + False, + ), + ] + + # Image Classification Models + IMAGE_CLASSIFICATION_MODELS = [ + HFModel( + "NSFW Image Detection", + "Falconsai/nsfw_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Content safety image classification", + True, + False, + ), + HFModel( + "ViT Base Patch16", + "google/vit-base-patch16-224", + ModelCategory.IMAGE_CLASSIFICATION, + "Google's Vision Transformer", + True, + False, + ), + HFModel( + "Deepfake Detection", + "dima806/deepfake_vs_real_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Detect AI-generated vs real images", + True, + False, + ), + HFModel( + "Facial Emotions Detection", + "dima806/facial_emotions_image_detection", + ModelCategory.IMAGE_CLASSIFICATION, + "Recognize facial emotions", + True, + False, + ), + HFModel( + "SDXL Detector", + "Organika/sdxl-detector", + ModelCategory.IMAGE_CLASSIFICATION, + "Detect Stable Diffusion XL generated images", + True, + False, + ), + HFModel( + "ViT NSFW Detector", + "AdamCodd/vit-base-nsfw-detector", + ModelCategory.IMAGE_CLASSIFICATION, + "NSFW content detection with ViT", + True, + False, + ), + HFModel( + "ResNet 101", + "microsoft/resnet-101", + ModelCategory.IMAGE_CLASSIFICATION, + "Microsoft's ResNet for classification", + True, + False, + ), + ] + + # Additional Categories + FEATURE_EXTRACTION_MODELS = [ + HFModel( + "Sentence Transformers All MiniLM", + "sentence-transformers/all-MiniLM-L6-v2", + ModelCategory.FEATURE_EXTRACTION, + "Lightweight sentence embeddings", + True, + False, + ), + HFModel( + "BGE Large EN", + "BAAI/bge-large-en-v1.5", + ModelCategory.FEATURE_EXTRACTION, + "High-quality English embeddings", + True, + False, + ), + HFModel( + "E5 Large v2", + "intfloat/e5-large-v2", + ModelCategory.FEATURE_EXTRACTION, + "Multilingual text embeddings", + True, + False, + ), + ] + + TRANSLATION_MODELS = [ + HFModel( + "M2M100 1.2B", + "facebook/m2m100_1.2B", + ModelCategory.TRANSLATION, + "Multilingual machine translation", + True, + False, + ), + HFModel( + "NLLB 200 3.3B", + "facebook/nllb-200-3.3B", + ModelCategory.TRANSLATION, + "No Language Left Behind translation", + True, + False, + ), + HFModel( + "mBART Large 50", + "facebook/mbart-large-50-many-to-many-mmt", + ModelCategory.TRANSLATION, + "Multilingual BART for translation", + True, + False, + ), + ] + + SUMMARIZATION_MODELS = [ + HFModel( + "PEGASUS XSum", + "google/pegasus-xsum", + ModelCategory.SUMMARIZATION, + "Abstractive summarization model", + True, + False, + ), + HFModel( + "BART Large CNN", + "facebook/bart-large-cnn", + ModelCategory.SUMMARIZATION, + "CNN/DailyMail summarization", + True, + False, + ), + HFModel( + "T5 Base", + "t5-base", + ModelCategory.SUMMARIZATION, + "Text-to-Text Transfer Transformer", + True, + False, + ), + ] + + # Video Generation and Processing Models + VIDEO_GENERATION_MODELS = [ + HFModel( + "Stable Video Diffusion", + "stabilityai/stable-video-diffusion-img2vid", + ModelCategory.TEXT_TO_VIDEO, + "Image-to-video generation model", + True, + False, + ), + HFModel( + "AnimateDiff", + "guoyww/animatediff", + ModelCategory.VIDEO_GENERATION, + "Text-to-video animation generation", + True, + False, + ), + HFModel( + "VideoCrafter", + "videogen/VideoCrafter", + ModelCategory.TEXT_TO_VIDEO, + "High-quality text-to-video generation", + True, + False, + ), + HFModel( + "Video ChatGPT", + "mbzuai-oryx/Video-ChatGPT-7B", + ModelCategory.VIDEO_TO_TEXT, + "Video understanding and description", + True, + False, + ), + HFModel( + "Video-BLIP", + "salesforce/video-blip-opt-2.7b", + ModelCategory.VIDEO_CLASSIFICATION, + "Video content analysis and classification", + True, + False, + ), + ] + + # Code Generation and Development Models + CODE_GENERATION_MODELS = [ + HFModel( + "CodeLlama 34B Instruct", + "codellama/CodeLlama-34b-Instruct-hf", + ModelCategory.CODE_GENERATION, + "Large instruction-tuned code generation model", + True, + True, + ), + HFModel( + "StarCoder2 15B", + "bigcode/starcoder2-15b", + ModelCategory.CODE_GENERATION, + "Advanced code generation and completion", + True, + False, + ), + HFModel( + "DeepSeek Coder V2", + "deepseek-ai/deepseek-coder-6.7b-instruct", + ModelCategory.CODE_GENERATION, + "Specialized coding assistant", + True, + False, + ), + HFModel( + "WizardCoder 34B", + "WizardLM/WizardCoder-Python-34B-V1.0", + ModelCategory.CODE_GENERATION, + "Python-focused code generation", + True, + False, + ), + HFModel( + "Phind CodeLlama", + "Phind/Phind-CodeLlama-34B-v2", + ModelCategory.CODE_GENERATION, + "Optimized for code explanation and debugging", + True, + False, + ), + HFModel( + "Code T5+", + "Salesforce/codet5p-770m", + ModelCategory.CODE_COMPLETION, + "Code understanding and generation", + True, + False, + ), + HFModel( + "InCoder", + "facebook/incoder-6B", + ModelCategory.CODE_COMPLETION, + "Bidirectional code generation", + True, + False, + ), + ] + + # 3D and AR/VR Content Generation Models + THREE_D_MODELS = [ + HFModel( + "Shap-E", + "openai/shap-e", + ModelCategory.TEXT_TO_3D, + "Text-to-3D shape generation", + True, + False, + ), + HFModel( + "Point-E", + "openai/point-e", + ModelCategory.TEXT_TO_3D, + "Text-to-3D point cloud generation", + True, + False, + ), + HFModel( + "DreamFusion", + "google/dreamfusion", + ModelCategory.IMAGE_TO_3D, + "Image-to-3D mesh generation", + True, + False, + ), + HFModel( + "Magic3D", + "nvidia/magic3d", + ModelCategory.THREE_D_GENERATION, + "High-quality 3D content creation", + True, + False, + ), + HFModel( + "GET3D", + "nvidia/get3d", + ModelCategory.MESH_GENERATION, + "3D mesh generation from text", + True, + False, + ), + ] + + # Document Processing and OCR Models + DOCUMENT_PROCESSING_MODELS = [ + HFModel( + "TrOCR Large", + "microsoft/trocr-large-printed", + ModelCategory.OCR, + "Transformer-based OCR for printed text", + True, + False, + ), + HFModel( + "TrOCR Handwritten", + "microsoft/trocr-large-handwritten", + ModelCategory.HANDWRITING_RECOGNITION, + "Handwritten text recognition", + True, + False, + ), + HFModel( + "LayoutLMv3", + "microsoft/layoutlmv3-large", + ModelCategory.DOCUMENT_ANALYSIS, + "Document layout analysis and understanding", + True, + False, + ), + HFModel( + "Donut", + "naver-clova-ix/donut-base", + ModelCategory.DOCUMENT_ANALYSIS, + "OCR-free document understanding", + True, + False, + ), + HFModel( + "TableTransformer", + "microsoft/table-transformer-structure-recognition", + ModelCategory.TABLE_EXTRACTION, + "Table structure recognition", + True, + False, + ), + HFModel( + "FormNet", + "microsoft/formnet", + ModelCategory.FORM_PROCESSING, + "Form understanding and processing", + True, + False, + ), + ] + + # Multimodal AI Models + MULTIMODAL_MODELS = [ + HFModel( + "BLIP-2", + "Salesforce/blip2-opt-2.7b", + ModelCategory.VISION_LANGUAGE, + "Vision-language understanding and generation", + True, + False, + ), + HFModel( + "InstructBLIP", + "Salesforce/instructblip-vicuna-7b", + ModelCategory.MULTIMODAL_REASONING, + "Instruction-following multimodal model", + True, + False, + ), + HFModel( + "LLaVA", + "liuhaotian/llava-v1.5-7b", + ModelCategory.VISUAL_QUESTION_ANSWERING, + "Large Language and Vision Assistant", + True, + False, + ), + HFModel( + "GPT-4V", + "openai/gpt-4-vision-preview", + ModelCategory.MULTIMODAL_CHAT, + "Advanced multimodal conversational AI", + True, + True, + ), + HFModel( + "Flamingo", + "deepmind/flamingo-9b", + ModelCategory.CROSS_MODAL_GENERATION, + "Few-shot learning for vision and language", + True, + False, + ), + ] + + # Specialized AI Models + SPECIALIZED_AI_MODELS = [ + HFModel( + "MusicGen", + "facebook/musicgen-medium", + ModelCategory.MUSIC_GENERATION, + "Text-to-music generation", + True, + False, + ), + HFModel( + "AudioCraft", + "facebook/audiocraft_musicgen_melody", + ModelCategory.MUSIC_GENERATION, + "Melody-conditioned music generation", + True, + False, + ), + HFModel( + "Real-ESRGAN", + "xinntao/realesrgan-x4plus", + ModelCategory.SUPER_RESOLUTION, + "Image super-resolution", + True, + False, + ), + HFModel( + "GFPGAN", + "TencentARC/GFPGAN", + ModelCategory.FACE_RESTORATION, + "Face restoration and enhancement", + True, + False, + ), + HFModel( + "LaMa", + "advimman/lama", + ModelCategory.IMAGE_INPAINTING, + "Large Mask Inpainting", + True, + False, + ), + HFModel( + "Background Remover", + "briaai/RMBG-1.4", + ModelCategory.BACKGROUND_REMOVAL, + "Automatic background removal", + True, + False, + ), + HFModel( + "Voice Cloner", + "coqui/XTTS-v2", + ModelCategory.VOICE_CLONING, + "Multilingual voice cloning", + True, + False, + ), + ] + + # Creative Content Models + CREATIVE_CONTENT_MODELS = [ + HFModel( + "GPT-3.5 Creative", + "openai/gpt-3.5-turbo-instruct", + ModelCategory.CREATIVE_WRITING, + "Creative writing and storytelling", + True, + True, + ), + HFModel( + "Novel AI", + "novelai/genji-python-6b", + ModelCategory.STORY_GENERATION, + "Interactive story generation", + True, + False, + ), + HFModel( + "Poet Assistant", + "gpt2-poetry", + ModelCategory.POETRY_GENERATION, + "Poetry generation and analysis", + True, + False, + ), + HFModel( + "Blog Writer", + "google/flan-t5-large", + ModelCategory.BLOG_WRITING, + "Blog content creation", + True, + False, + ), + HFModel( + "Marketing Copy AI", + "microsoft/DialoGPT-large", + ModelCategory.MARKETING_COPY, + "Marketing content generation", + True, + False, + ), + ] + + # Game Development Models + GAME_DEVELOPMENT_MODELS = [ + HFModel( + "Character AI", + "character-ai/character-generator", + ModelCategory.CHARACTER_GENERATION, + "Game character generation and design", + True, + False, + ), + HFModel( + "Level Designer", + "unity/level-generator", + ModelCategory.LEVEL_GENERATION, + "Game level and environment generation", + True, + False, + ), + HFModel( + "Dialogue Writer", + "bioware/dialogue-generator", + ModelCategory.DIALOGUE_GENERATION, + "Game dialogue and narrative generation", + True, + False, + ), + HFModel( + "Asset Creator", + "epic/asset-generator", + ModelCategory.GAME_ASSET_GENERATION, + "Game asset and texture generation", + True, + False, + ), + ] + + # Science and Research Models + SCIENCE_RESEARCH_MODELS = [ + HFModel( + "AlphaFold", + "deepmind/alphafold2", + ModelCategory.PROTEIN_FOLDING, + "Protein structure prediction", + True, + False, + ), + HFModel( + "ChemBERTa", + "DeepChem/ChemBERTa-77M-MLM", + ModelCategory.MOLECULE_GENERATION, + "Chemical compound analysis", + True, + False, + ), + HFModel( + "SciBERT", + "allenai/scibert_scivocab_uncased", + ModelCategory.SCIENTIFIC_WRITING, + "Scientific text understanding", + True, + False, + ), + HFModel( + "Research Assistant", + "microsoft/specter2", + ModelCategory.RESEARCH_ASSISTANCE, + "Research paper analysis and recommendations", + True, + False, + ), + HFModel( + "Data Analyst", + "microsoft/data-copilot", + ModelCategory.DATA_ANALYSIS, + "Automated data analysis and insights", + True, + False, + ), + ] + + # Business and Productivity Models + BUSINESS_PRODUCTIVITY_MODELS = [ + HFModel( + "Email Assistant", + "microsoft/email-generator", + ModelCategory.EMAIL_GENERATION, + "Professional email composition", + True, + False, + ), + HFModel( + "Presentation AI", + "gamma/presentation-generator", + ModelCategory.PRESENTATION_CREATION, + "Automated presentation creation", + True, + False, + ), + HFModel( + "Report Writer", + "openai/report-generator", + ModelCategory.REPORT_GENERATION, + "Business report generation", + True, + False, + ), + HFModel( + "Meeting Summarizer", + "microsoft/meeting-summarizer", + ModelCategory.MEETING_SUMMARIZATION, + "Meeting notes and action items", + True, + False, + ), + HFModel( + "Project Planner", + "atlassian/project-ai", + ModelCategory.PROJECT_PLANNING, + "Project planning and management", + True, + False, + ), + ] + + # AI Teacher Models - Best-in-Class Educational AI System + AI_TEACHER_MODELS = [ + # Primary AI Tutoring Models + HFModel( + "AI Tutor Interactive", + "microsoft/DialoGPT-medium", + ModelCategory.AI_TUTORING, + "Interactive AI tutor for conversational learning", + True, + False, + 2048, + True, + ), + HFModel( + "Goal-Oriented Tutor", + "microsoft/GODEL-v1_1-large-seq2seq", + ModelCategory.AI_TUTORING, + "Goal-oriented conversational AI for personalized tutoring", + True, + False, + 2048, + True, + ), + HFModel( + "Code Instructor AI", + "microsoft/codebert-base", + ModelCategory.CODING_INSTRUCTION, + "AI coding instructor for programming education", + True, + False, + 1024, + False, + ), + HFModel( + "deepmind/flamingo-base", + "ADAPTIVE_LEARNING", + ModelCategory.ADAPTIVE_LEARNING, + "Multimodal AI for adaptive learning experiences", + True, + False, + 1024, + True, + ), + # Educational Content Generation + HFModel( + "gpt2-medium", + "EDUCATIONAL_CONTENT", + ModelCategory.EDUCATIONAL_CONTENT, + "Educational content generation for curriculum development", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/bart-large-cnn", + "LESSON_PLANNING", + ModelCategory.LESSON_PLANNING, + "Lesson plan generation and educational summarization", + True, + False, + 1024, + True, + ), + HFModel( + "microsoft/prophetnet-large-uncased", + "STUDY_GUIDE_CREATION", + ModelCategory.STUDY_GUIDE_CREATION, + "Study guide and learning material generation", + True, + False, + 1024, + True, + ), + HFModel( + "bigscience/bloom-560m", + "EDUCATIONAL_CONTENT", + ModelCategory.EDUCATIONAL_CONTENT, + "Multilingual educational content for global learning", + True, + False, + 1024, + True, + ), + # Subject-Specific Teaching Models + HFModel( + "microsoft/codebert-base", + "CODING_INSTRUCTION", + ModelCategory.CODING_INSTRUCTION, + "Programming education and code explanation", + True, + False, + 1024, + True, + ), + HFModel( + "allenai/scibert_scivocab_uncased", + "SCIENCE_TUTORING", + ModelCategory.SCIENCE_TUTORING, + "Science education and scientific concept explanation", + True, + False, + 1024, + True, + ), + HFModel( + "google/flan-t5-base", + "SUBJECT_TEACHING", + ModelCategory.SUBJECT_TEACHING, + "Multi-subject teaching AI with instruction following", + True, + False, + 1024, + True, + ), + HFModel( + "microsoft/unixcoder-base", + "CODING_INSTRUCTION", + ModelCategory.CODING_INSTRUCTION, + "Advanced programming instruction and debugging help", + True, + False, + 1024, + True, + ), + # Math and STEM Education + HFModel( + "microsoft/DialoGPT-small", + "MATH_TUTORING", + ModelCategory.MATH_TUTORING, + "Interactive math tutoring and problem solving", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/galactica-125m", + "SCIENCE_TUTORING", + ModelCategory.SCIENCE_TUTORING, + "Scientific knowledge and research education", + True, + False, + 1024, + True, + ), + HFModel( + "microsoft/graphcodebert-base", + "CODING_INSTRUCTION", + ModelCategory.CODING_INSTRUCTION, + "Code structure and algorithm education", + True, + False, + 1024, + True, + ), + HFModel( + "deepmind/mathematical-reasoning", + "MATH_TUTORING", + ModelCategory.MATH_TUTORING, + "Mathematical reasoning and proof assistance", + True, + False, + 1024, + True, + ), + # Language and Literature Education + HFModel( + "microsoft/prophetnet-large-uncased-cnndm", + "LANGUAGE_TUTORING", + ModelCategory.LANGUAGE_TUTORING, + "Language learning and literature analysis", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/mbart-large-50-many-to-many-mmt", + "LANGUAGE_TUTORING", + ModelCategory.LANGUAGE_TUTORING, + "Multilingual language education and translation", + True, + False, + 1024, + True, + ), + HFModel( + "google/electra-base-discriminator", + "LANGUAGE_TUTORING", + ModelCategory.LANGUAGE_TUTORING, + "Language comprehension and grammar instruction", + True, + False, + 1024, + True, + ), + # Assessment and Testing + HFModel( + "microsoft/DialoGPT-large", + "QUIZ_GENERATION", + ModelCategory.QUIZ_GENERATION, + "Interactive quiz and assessment generation", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/bart-large", + "LEARNING_ASSESSMENT", + ModelCategory.LEARNING_ASSESSMENT, + "Learning progress assessment and feedback", + True, + False, + 1024, + True, + ), + HFModel( + "google/t5-base", + "QUIZ_GENERATION", + ModelCategory.QUIZ_GENERATION, + "Question generation for educational assessment", + True, + False, + 1024, + True, + ), + HFModel( + "microsoft/unilm-base-cased", + "EXAM_PREPARATION", + ModelCategory.EXAM_PREPARATION, + "Exam preparation and practice test generation", + True, + False, + 1024, + True, + ), + # Personalized Learning + HFModel( + "huggingface/distilbert-base-uncased", + "PERSONALIZED_LEARNING", + ModelCategory.PERSONALIZED_LEARNING, + "Personalized learning path recommendation", + True, + False, + 1024, + True, + ), + HFModel( + "microsoft/layoutlm-base-uncased", + "LEARNING_ANALYTICS", + ModelCategory.LEARNING_ANALYTICS, + "Educational document analysis and insights", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/opt-125m", + "ADAPTIVE_LEARNING", + ModelCategory.ADAPTIVE_LEARNING, + "Adaptive learning system with dynamic content", + True, + False, + 1024, + True, + ), + # Concept Explanation and Understanding + HFModel( + "microsoft/deberta-base", + "CONCEPT_EXPLANATION", + ModelCategory.CONCEPT_EXPLANATION, + "Clear concept explanation and knowledge breakdown", + True, + False, + 1024, + True, + ), + HFModel( + "google/pegasus-xsum", + "CONCEPT_EXPLANATION", + ModelCategory.CONCEPT_EXPLANATION, + "Concept summarization and explanation", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/bart-base", + "CONCEPT_EXPLANATION", + ModelCategory.CONCEPT_EXPLANATION, + "Interactive concept teaching and clarification", + True, + False, + 1024, + True, + ), + # Homework and Study Assistance + HFModel( + "microsoft/codebert-base-mlm", + "HOMEWORK_ASSISTANCE", + ModelCategory.HOMEWORK_ASSISTANCE, + "Programming homework help and debugging", + True, + False, + 1024, + True, + ), + HFModel( + "google/flan-t5-small", + "HOMEWORK_ASSISTANCE", + ModelCategory.HOMEWORK_ASSISTANCE, + "General homework assistance across subjects", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/mbart-large-cc25", + "HOMEWORK_ASSISTANCE", + ModelCategory.HOMEWORK_ASSISTANCE, + "Multilingual homework support and explanation", + True, + False, + 1024, + True, + ), + # Curriculum Design and Planning + HFModel( + "microsoft/prophetnet-base-uncased", + "CURRICULUM_DESIGN", + ModelCategory.CURRICULUM_DESIGN, + "Curriculum planning and educational structure design", + True, + False, + 1024, + True, + ), + HFModel( + "google/t5-small", + "LESSON_PLANNING", + ModelCategory.LESSON_PLANNING, + "Detailed lesson planning and activity design", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/bart-large-xsum", + "CURRICULUM_DESIGN", + ModelCategory.CURRICULUM_DESIGN, + "Educational program summarization and design", + True, + False, + 1024, + True, + ), + # Educational Games and Interactive Learning + HFModel( + "microsoft/DialoGPT-base", + "EDUCATIONAL_GAMES", + ModelCategory.EDUCATIONAL_GAMES, + "Interactive educational games and learning activities", + True, + False, + 1024, + True, + ), + HFModel( + "huggingface/bert-base-uncased", + "EDUCATIONAL_GAMES", + ModelCategory.EDUCATIONAL_GAMES, + "Educational quiz games and interactive learning", + True, + False, + 1024, + True, + ), + # History and Social Studies + HFModel( + "microsoft/deberta-large", + "HISTORY_TUTORING", + ModelCategory.HISTORY_TUTORING, + "Historical analysis and social studies education", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/opt-350m", + "HISTORY_TUTORING", + ModelCategory.HISTORY_TUTORING, + "Interactive history lessons and timeline explanation", + True, + False, + 1024, + True, + ), + # Advanced Educational Features + HFModel( + "microsoft/unilm-large-cased", + "LEARNING_ANALYTICS", + ModelCategory.LEARNING_ANALYTICS, + "Advanced learning analytics and progress tracking", + True, + False, + 1024, + True, + ), + HFModel( + "google/electra-large-discriminator", + "PERSONALIZED_LEARNING", + ModelCategory.PERSONALIZED_LEARNING, + "Advanced personalized learning with AI adaptation", + True, + False, + 1024, + True, + ), + HFModel( + "facebook/mbart-large-50", + "ADAPTIVE_LEARNING", + ModelCategory.ADAPTIVE_LEARNING, + "Multilingual adaptive learning system", + True, + False, + 1024, + True, + ), + ] + + +class HuggingFaceInference: + """Hugging Face Inference API integration""" + + def __init__( + self, + api_token: str, + base_url: str = "https://api-inference.huggingface.co/models/", + ): + self.api_token = api_token + self.base_url = base_url + self.session = None + + async def __aenter__(self): + self.session = aiohttp.ClientSession( + headers={"Authorization": f"Bearer {self.api_token}"}, + timeout=aiohttp.ClientTimeout(total=300), # 5 minutes timeout + ) + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb): + if self.session: + await self.session.close() + + async def text_generation( + self, + model_id: str, + prompt: str, + max_tokens: int = 100, + temperature: float = 0.7, + stream: bool = False, + **kwargs, + ) -> Dict[str, Any]: + """Generate text using a text generation model""" + payload = { + "inputs": prompt, + "parameters": { + "max_new_tokens": max_tokens, + "temperature": temperature, + "do_sample": True, + **kwargs, + }, + "options": {"use_cache": False}, + } + + if stream: + return await self._stream_request(model_id, payload) + else: + return await self._request(model_id, payload) + + async def text_to_image( + self, + model_id: str, + prompt: str, + negative_prompt: Optional[str] = None, + **kwargs, + ) -> bytes: + """Generate image from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + **({"negative_prompt": negative_prompt} if negative_prompt else {}), + **kwargs, + }, + } + + response = await self._request(model_id, payload, expect_json=False) + return response + + async def automatic_speech_recognition( + self, model_id: str, audio_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Transcribe audio to text""" + # Convert audio bytes to base64 for API + audio_b64 = base64.b64encode(audio_data).decode() + + payload = {"inputs": audio_b64, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def text_to_speech(self, model_id: str, text: str, **kwargs) -> bytes: + """Convert text to speech audio""" + payload = {"inputs": text, "parameters": kwargs} + + response = await self._request(model_id, payload, expect_json=False) + return response + + async def image_classification( + self, model_id: str, image_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Classify images""" + # Convert image to base64 + image_b64 = base64.b64encode(image_data).decode() + + payload = {"inputs": image_b64, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def feature_extraction( + self, model_id: str, texts: Union[str, List[str]], **kwargs + ) -> Dict[str, Any]: + """Extract embeddings from text""" + payload = {"inputs": texts, "parameters": kwargs} + + return await self._request(model_id, payload) + + async def translation( + self, + model_id: str, + text: str, + src_lang: Optional[str] = None, + tgt_lang: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + """Translate text between languages""" + payload = { + "inputs": text, + "parameters": { + **({"src_lang": src_lang} if src_lang else {}), + **({"tgt_lang": tgt_lang} if tgt_lang else {}), + **kwargs, + }, + } + + return await self._request(model_id, payload) + + async def summarization( + self, + model_id: str, + text: str, + max_length: int = 150, + min_length: int = 30, + **kwargs, + ) -> Dict[str, Any]: + """Summarize text""" + payload = { + "inputs": text, + "parameters": { + "max_length": max_length, + "min_length": min_length, + **kwargs, + }, + } + + return await self._request(model_id, payload) + + async def question_answering( + self, model_id: str, question: str, context: str, **kwargs + ) -> Dict[str, Any]: + """Answer questions based on context""" + payload = { + "inputs": {"question": question, "context": context}, + "parameters": kwargs, + } + + return await self._request(model_id, payload) + + async def zero_shot_classification( + self, model_id: str, text: str, candidate_labels: List[str], **kwargs + ) -> Dict[str, Any]: + """Classify text without training data""" + payload = { + "inputs": text, + "parameters": {"candidate_labels": candidate_labels, **kwargs}, + } + + return await self._request(model_id, payload) + + async def conversational( + self, + model_id: str, + text: str, + conversation_history: Optional[List[Dict[str, str]]] = None, + **kwargs, + ) -> Dict[str, Any]: + """Have a conversation with a model""" + payload = { + "inputs": { + "text": text, + **( + { + "past_user_inputs": [ + h["user"] for h in conversation_history if "user" in h + ] + } + if conversation_history + else {} + ), + **( + { + "generated_responses": [ + h["bot"] for h in conversation_history if "bot" in h + ] + } + if conversation_history + else {} + ), + }, + "parameters": kwargs, + } + + return await self._request(model_id, payload) + + async def _request( + self, model_id: str, payload: Dict[str, Any], expect_json: bool = True + ) -> Union[Dict[str, Any], bytes]: + """Make HTTP request to Hugging Face API""" + url = f"{self.base_url}{model_id}" + + try: + async with self.session.post(url, json=payload) as response: + if response.status == 200: + if expect_json: + return await response.json() + else: + return await response.read() + elif response.status == 503: + # Model is loading, wait and retry + error_info = await response.json() + estimated_time = error_info.get("estimated_time", 30) + logger.info( + f"Model {model_id} is loading, waiting {estimated_time}s" + ) + await asyncio.sleep(min(estimated_time, 60)) # Cap at 60 seconds + return await self._request(model_id, payload, expect_json) + else: + error_text = await response.text() + raise Exception( + f"API request failed with status {response.status}: {error_text}" + ) + + except Exception as e: + logger.error(f"Error calling Hugging Face API for {model_id}: {e}") + raise + + async def _stream_request(self, model_id: str, payload: Dict[str, Any]): + """Stream response from Hugging Face API""" + url = f"{self.base_url}{model_id}" + payload["stream"] = True + + try: + async with self.session.post(url, json=payload) as response: + if response.status == 200: + async for chunk in response.content: + if chunk: + yield chunk.decode("utf-8") + else: + error_text = await response.text() + raise Exception( + f"Streaming request failed with status {response.status}: {error_text}" + ) + + except Exception as e: + logger.error(f"Error streaming from Hugging Face API for {model_id}: {e}") + raise + + # New methods for expanded model categories + + async def text_to_video( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate video from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + "duration": kwargs.get("duration", 5), + "fps": kwargs.get("fps", 24), + "width": kwargs.get("width", 512), + "height": kwargs.get("height", 512), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def video_to_text( + self, model_id: str, video_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Analyze video and generate text description""" + video_b64 = base64.b64encode(video_data).decode() + payload = { + "inputs": {"video": video_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def code_generation( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate code from natural language prompt""" + payload = { + "inputs": prompt, + "parameters": { + "max_length": kwargs.get("max_length", 500), + "temperature": kwargs.get("temperature", 0.2), + "language": kwargs.get("language", "python"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def code_completion( + self, model_id: str, code: str, **kwargs + ) -> Dict[str, Any]: + """Complete partial code""" + payload = { + "inputs": code, + "parameters": { + "max_length": kwargs.get("max_length", 100), + "temperature": kwargs.get("temperature", 0.1), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def text_to_3d(self, model_id: str, prompt: str, **kwargs) -> Dict[str, Any]: + """Generate 3D model from text description""" + payload = { + "inputs": prompt, + "parameters": { + "resolution": kwargs.get("resolution", 64), + "format": kwargs.get("format", "obj"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def image_to_3d( + self, model_id: str, image_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Generate 3D model from image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def ocr(self, model_id: str, image_data: bytes, **kwargs) -> Dict[str, Any]: + """Perform optical character recognition on image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": {"language": kwargs.get("language", "en"), **kwargs}, + } + return await self._request(model_id, payload) + + async def document_analysis( + self, model_id: str, document_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Analyze document structure and content""" + doc_b64 = base64.b64encode(document_data).decode() + payload = { + "inputs": {"document": doc_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def vision_language( + self, model_id: str, image_data: bytes, text: str, **kwargs + ) -> Dict[str, Any]: + """Process image and text together""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64, "text": text}, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def multimodal_reasoning( + self, model_id: str, inputs: Dict[str, Any], **kwargs + ) -> Dict[str, Any]: + """Perform reasoning across multiple modalities""" + payload = { + "inputs": inputs, + "parameters": kwargs, + } + return await self._request(model_id, payload) + + async def music_generation( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate music from text prompt""" + payload = { + "inputs": prompt, + "parameters": { + "duration": kwargs.get("duration", 30), + "bpm": kwargs.get("bpm", 120), + "genre": kwargs.get("genre", "electronic"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def voice_cloning( + self, model_id: str, text: str, voice_sample: bytes, **kwargs + ) -> bytes: + """Clone voice and synthesize speech""" + voice_b64 = base64.b64encode(voice_sample).decode() + payload = { + "inputs": {"text": text, "voice_sample": voice_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload, expect_json=False) + + async def super_resolution( + self, model_id: str, image_data: bytes, **kwargs + ) -> bytes: + """Enhance image resolution""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": {"scale_factor": kwargs.get("scale_factor", 4), **kwargs}, + } + return await self._request(model_id, payload, expect_json=False) + + async def background_removal( + self, model_id: str, image_data: bytes, **kwargs + ) -> bytes: + """Remove background from image""" + image_b64 = base64.b64encode(image_data).decode() + payload = { + "inputs": {"image": image_b64}, + "parameters": kwargs, + } + return await self._request(model_id, payload, expect_json=False) + + async def creative_writing( + self, model_id: str, prompt: str, **kwargs + ) -> Dict[str, Any]: + """Generate creative content""" + payload = { + "inputs": prompt, + "parameters": { + "max_length": kwargs.get("max_length", 1000), + "creativity": kwargs.get("creativity", 0.8), + "genre": kwargs.get("genre", "general"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + async def business_document( + self, model_id: str, document_type: str, context: str, **kwargs + ) -> Dict[str, Any]: + """Generate business documents""" + payload = { + "inputs": f"Generate {document_type}: {context}", + "parameters": { + "format": kwargs.get("format", "professional"), + "length": kwargs.get("length", "medium"), + **kwargs, + }, + } + return await self._request(model_id, payload) + + +class HuggingFaceModelManager: + """Manager for all Hugging Face model operations""" + + def __init__(self, api_token: str): + self.api_token = api_token + self.models = HuggingFaceModels() + + def get_models_by_category(self, category: ModelCategory) -> List[HFModel]: + """Get all models for a specific category""" + all_models = [] + + if category == ModelCategory.TEXT_GENERATION: + all_models = self.models.TEXT_GENERATION_MODELS + elif category == ModelCategory.TEXT_TO_IMAGE: + all_models = self.models.TEXT_TO_IMAGE_MODELS + elif category == ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: + all_models = self.models.ASR_MODELS + elif category == ModelCategory.TEXT_TO_SPEECH: + all_models = self.models.TTS_MODELS + elif category == ModelCategory.IMAGE_CLASSIFICATION: + all_models = self.models.IMAGE_CLASSIFICATION_MODELS + elif category == ModelCategory.FEATURE_EXTRACTION: + all_models = self.models.FEATURE_EXTRACTION_MODELS + elif category == ModelCategory.TRANSLATION: + all_models = self.models.TRANSLATION_MODELS + elif category == ModelCategory.SUMMARIZATION: + all_models = self.models.SUMMARIZATION_MODELS + + return all_models + + def get_all_models(self) -> Dict[ModelCategory, List[HFModel]]: + """Get all available models organized by category""" + return { + # Core AI categories + ModelCategory.TEXT_GENERATION: self.models.TEXT_GENERATION_MODELS, + ModelCategory.TEXT_TO_IMAGE: self.models.TEXT_TO_IMAGE_MODELS, + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: self.models.ASR_MODELS, + ModelCategory.TEXT_TO_SPEECH: self.models.TTS_MODELS, + ModelCategory.IMAGE_CLASSIFICATION: self.models.IMAGE_CLASSIFICATION_MODELS, + ModelCategory.FEATURE_EXTRACTION: self.models.FEATURE_EXTRACTION_MODELS, + ModelCategory.TRANSLATION: self.models.TRANSLATION_MODELS, + ModelCategory.SUMMARIZATION: self.models.SUMMARIZATION_MODELS, + # Video and Motion + ModelCategory.TEXT_TO_VIDEO: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_GENERATION: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_TO_TEXT: self.models.VIDEO_GENERATION_MODELS, + ModelCategory.VIDEO_CLASSIFICATION: self.models.VIDEO_GENERATION_MODELS, + # Code and Development + ModelCategory.CODE_GENERATION: self.models.CODE_GENERATION_MODELS, + ModelCategory.CODE_COMPLETION: self.models.CODE_GENERATION_MODELS, + ModelCategory.CODE_EXPLANATION: self.models.CODE_GENERATION_MODELS, + ModelCategory.APP_GENERATION: self.models.CODE_GENERATION_MODELS, + # 3D and AR/VR + ModelCategory.TEXT_TO_3D: self.models.THREE_D_MODELS, + ModelCategory.IMAGE_TO_3D: self.models.THREE_D_MODELS, + ModelCategory.THREE_D_GENERATION: self.models.THREE_D_MODELS, + ModelCategory.MESH_GENERATION: self.models.THREE_D_MODELS, + # Document Processing + ModelCategory.OCR: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.DOCUMENT_ANALYSIS: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.HANDWRITING_RECOGNITION: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.TABLE_EXTRACTION: self.models.DOCUMENT_PROCESSING_MODELS, + ModelCategory.FORM_PROCESSING: self.models.DOCUMENT_PROCESSING_MODELS, + # Multimodal AI + ModelCategory.VISION_LANGUAGE: self.models.MULTIMODAL_MODELS, + ModelCategory.MULTIMODAL_REASONING: self.models.MULTIMODAL_MODELS, + ModelCategory.VISUAL_QUESTION_ANSWERING: self.models.MULTIMODAL_MODELS, + ModelCategory.MULTIMODAL_CHAT: self.models.MULTIMODAL_MODELS, + ModelCategory.CROSS_MODAL_GENERATION: self.models.MULTIMODAL_MODELS, + # Specialized AI + ModelCategory.MUSIC_GENERATION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.VOICE_CLONING: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.SUPER_RESOLUTION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.FACE_RESTORATION: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.IMAGE_INPAINTING: self.models.SPECIALIZED_AI_MODELS, + ModelCategory.BACKGROUND_REMOVAL: self.models.SPECIALIZED_AI_MODELS, + # Creative Content + ModelCategory.CREATIVE_WRITING: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.STORY_GENERATION: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.POETRY_GENERATION: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.BLOG_WRITING: self.models.CREATIVE_CONTENT_MODELS, + ModelCategory.MARKETING_COPY: self.models.CREATIVE_CONTENT_MODELS, + # Game Development + ModelCategory.GAME_ASSET_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.CHARACTER_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.LEVEL_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + ModelCategory.DIALOGUE_GENERATION: self.models.GAME_DEVELOPMENT_MODELS, + # Science and Research + ModelCategory.PROTEIN_FOLDING: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.MOLECULE_GENERATION: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.SCIENTIFIC_WRITING: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.RESEARCH_ASSISTANCE: self.models.SCIENCE_RESEARCH_MODELS, + ModelCategory.DATA_ANALYSIS: self.models.SCIENCE_RESEARCH_MODELS, + # Business and Productivity + ModelCategory.EMAIL_GENERATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.PRESENTATION_CREATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.REPORT_GENERATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.MEETING_SUMMARIZATION: self.models.BUSINESS_PRODUCTIVITY_MODELS, + ModelCategory.PROJECT_PLANNING: self.models.BUSINESS_PRODUCTIVITY_MODELS, + } + + def get_model_by_id(self, model_id: str) -> Optional[HFModel]: + """Find a model by its Hugging Face model ID""" + for models_list in self.get_all_models().values(): + for model in models_list: + if model.model_id == model_id: + return model + return None + + async def call_model(self, model_id: str, category: ModelCategory, **kwargs) -> Any: + """Call a Hugging Face model with the appropriate method based on category""" + + async with HuggingFaceInference(self.api_token) as hf: + if category == ModelCategory.TEXT_GENERATION: + return await hf.text_generation(model_id, **kwargs) + elif category == ModelCategory.TEXT_TO_IMAGE: + return await hf.text_to_image(model_id, **kwargs) + elif category == ModelCategory.AUTOMATIC_SPEECH_RECOGNITION: + return await hf.automatic_speech_recognition(model_id, **kwargs) + elif category == ModelCategory.TEXT_TO_SPEECH: + return await hf.text_to_speech(model_id, **kwargs) + elif category == ModelCategory.IMAGE_CLASSIFICATION: + return await hf.image_classification(model_id, **kwargs) + elif category == ModelCategory.FEATURE_EXTRACTION: + return await hf.feature_extraction(model_id, **kwargs) + elif category == ModelCategory.TRANSLATION: + return await hf.translation(model_id, **kwargs) + elif category == ModelCategory.SUMMARIZATION: + return await hf.summarization(model_id, **kwargs) + elif category == ModelCategory.QUESTION_ANSWERING: + return await hf.question_answering(model_id, **kwargs) + elif category == ModelCategory.ZERO_SHOT_CLASSIFICATION: + return await hf.zero_shot_classification(model_id, **kwargs) + elif category == ModelCategory.CONVERSATIONAL: + return await hf.conversational(model_id, **kwargs) + + # Video and Motion categories + elif category in [ + ModelCategory.TEXT_TO_VIDEO, + ModelCategory.VIDEO_GENERATION, + ]: + return await hf.text_to_video(model_id, **kwargs) + elif category == ModelCategory.VIDEO_TO_TEXT: + return await hf.video_to_text(model_id, **kwargs) + elif category == ModelCategory.VIDEO_CLASSIFICATION: + return await hf.image_classification( + model_id, **kwargs + ) # Similar to image classification + + # Code and Development categories + elif category in [ + ModelCategory.CODE_GENERATION, + ModelCategory.APP_GENERATION, + ]: + return await hf.code_generation(model_id, **kwargs) + elif category in [ + ModelCategory.CODE_COMPLETION, + ModelCategory.CODE_EXPLANATION, + ]: + return await hf.code_completion(model_id, **kwargs) + + # 3D and AR/VR categories + elif category in [ + ModelCategory.TEXT_TO_3D, + ModelCategory.THREE_D_GENERATION, + ]: + return await hf.text_to_3d(model_id, **kwargs) + elif category in [ModelCategory.IMAGE_TO_3D, ModelCategory.MESH_GENERATION]: + return await hf.image_to_3d(model_id, **kwargs) + + # Document Processing categories + elif category == ModelCategory.OCR: + return await hf.ocr(model_id, **kwargs) + elif category in [ + ModelCategory.DOCUMENT_ANALYSIS, + ModelCategory.FORM_PROCESSING, + ModelCategory.TABLE_EXTRACTION, + ModelCategory.LAYOUT_ANALYSIS, + ]: + return await hf.document_analysis(model_id, **kwargs) + elif category == ModelCategory.HANDWRITING_RECOGNITION: + return await hf.ocr(model_id, **kwargs) # Similar to OCR + + # Multimodal AI categories + elif category in [ + ModelCategory.VISION_LANGUAGE, + ModelCategory.VISUAL_QUESTION_ANSWERING, + ModelCategory.IMAGE_TEXT_MATCHING, + ]: + return await hf.vision_language(model_id, **kwargs) + elif category in [ + ModelCategory.MULTIMODAL_REASONING, + ModelCategory.MULTIMODAL_CHAT, + ModelCategory.CROSS_MODAL_GENERATION, + ]: + return await hf.multimodal_reasoning(model_id, **kwargs) + + # Specialized AI categories + elif category == ModelCategory.MUSIC_GENERATION: + return await hf.music_generation(model_id, **kwargs) + elif category == ModelCategory.VOICE_CLONING: + return await hf.voice_cloning(model_id, **kwargs) + elif category == ModelCategory.SUPER_RESOLUTION: + return await hf.super_resolution(model_id, **kwargs) + elif category in [ + ModelCategory.FACE_RESTORATION, + ModelCategory.IMAGE_INPAINTING, + ModelCategory.IMAGE_OUTPAINTING, + ]: + return await hf.super_resolution( + model_id, **kwargs + ) # Similar processing + elif category == ModelCategory.BACKGROUND_REMOVAL: + return await hf.background_removal(model_id, **kwargs) + + # Creative Content categories + elif category in [ + ModelCategory.CREATIVE_WRITING, + ModelCategory.STORY_GENERATION, + ModelCategory.POETRY_GENERATION, + ModelCategory.SCREENPLAY_WRITING, + ]: + return await hf.creative_writing(model_id, **kwargs) + elif category in [ModelCategory.BLOG_WRITING, ModelCategory.MARKETING_COPY]: + return await hf.text_generation( + model_id, **kwargs + ) # Use standard text generation + + # Game Development categories + elif category in [ + ModelCategory.CHARACTER_GENERATION, + ModelCategory.LEVEL_GENERATION, + ModelCategory.DIALOGUE_GENERATION, + ModelCategory.GAME_ASSET_GENERATION, + ]: + return await hf.creative_writing( + model_id, **kwargs + ) # Creative generation + + # Science and Research categories + elif category in [ + ModelCategory.PROTEIN_FOLDING, + ModelCategory.MOLECULE_GENERATION, + ]: + return await hf.text_generation( + model_id, **kwargs + ) # Specialized text generation + elif category in [ + ModelCategory.SCIENTIFIC_WRITING, + ModelCategory.RESEARCH_ASSISTANCE, + ModelCategory.DATA_ANALYSIS, + ]: + return await hf.text_generation(model_id, **kwargs) + + # Business and Productivity categories + elif category in [ + ModelCategory.EMAIL_GENERATION, + ModelCategory.PRESENTATION_CREATION, + ModelCategory.REPORT_GENERATION, + ModelCategory.MEETING_SUMMARIZATION, + ModelCategory.PROJECT_PLANNING, + ]: + return await hf.business_document(model_id, category.value, **kwargs) + + else: + raise ValueError(f"Unsupported model category: {category}") + diff --git a/app/llm.py b/app/llm.py new file mode 100644 index 0000000000000000000000000000000000000000..82ebe88571bc32f17da64b517ea00dc064da7129 --- /dev/null +++ b/app/llm.py @@ -0,0 +1,766 @@ +import math +from typing import Dict, List, Optional, Union + +import tiktoken +from openai import ( + APIError, + AsyncAzureOpenAI, + AsyncOpenAI, + AuthenticationError, + OpenAIError, + RateLimitError, +) +from openai.types.chat import ChatCompletion, ChatCompletionMessage +from tenacity import ( + retry, + retry_if_exception_type, + stop_after_attempt, + wait_random_exponential, +) + +from app.bedrock import BedrockClient +from app.config import LLMSettings, config +from app.exceptions import TokenLimitExceeded +from app.logger import logger # Assuming a logger is set up in your app +from app.schema import ( + ROLE_VALUES, + TOOL_CHOICE_TYPE, + TOOL_CHOICE_VALUES, + Message, + ToolChoice, +) + + +REASONING_MODELS = ["o1", "o3-mini"] +MULTIMODAL_MODELS = [ + "gpt-4-vision-preview", + "gpt-4o", + "gpt-4o-mini", + "claude-3-opus-20240229", + "claude-3-sonnet-20240229", + "claude-3-haiku-20240307", +] + + +class TokenCounter: + # Token constants + BASE_MESSAGE_TOKENS = 4 + FORMAT_TOKENS = 2 + LOW_DETAIL_IMAGE_TOKENS = 85 + HIGH_DETAIL_TILE_TOKENS = 170 + + # Image processing constants + MAX_SIZE = 2048 + HIGH_DETAIL_TARGET_SHORT_SIDE = 768 + TILE_SIZE = 512 + + def __init__(self, tokenizer): + self.tokenizer = tokenizer + + def count_text(self, text: str) -> int: + """Calculate tokens for a text string""" + return 0 if not text else len(self.tokenizer.encode(text)) + + def count_image(self, image_item: dict) -> int: + """ + Calculate tokens for an image based on detail level and dimensions + + For "low" detail: fixed 85 tokens + For "high" detail: + 1. Scale to fit in 2048x2048 square + 2. Scale shortest side to 768px + 3. Count 512px tiles (170 tokens each) + 4. Add 85 tokens + """ + detail = image_item.get("detail", "medium") + + # For low detail, always return fixed token count + if detail == "low": + return self.LOW_DETAIL_IMAGE_TOKENS + + # For medium detail (default in OpenAI), use high detail calculation + # OpenAI doesn't specify a separate calculation for medium + + # For high detail, calculate based on dimensions if available + if detail == "high" or detail == "medium": + # If dimensions are provided in the image_item + if "dimensions" in image_item: + width, height = image_item["dimensions"] + return self._calculate_high_detail_tokens(width, height) + + return ( + self._calculate_high_detail_tokens(1024, 1024) if detail == "high" else 1024 + ) + + def _calculate_high_detail_tokens(self, width: int, height: int) -> int: + """Calculate tokens for high detail images based on dimensions""" + # Step 1: Scale to fit in MAX_SIZE x MAX_SIZE square + if width > self.MAX_SIZE or height > self.MAX_SIZE: + scale = self.MAX_SIZE / max(width, height) + width = int(width * scale) + height = int(height * scale) + + # Step 2: Scale so shortest side is HIGH_DETAIL_TARGET_SHORT_SIDE + scale = self.HIGH_DETAIL_TARGET_SHORT_SIDE / min(width, height) + scaled_width = int(width * scale) + scaled_height = int(height * scale) + + # Step 3: Count number of 512px tiles + tiles_x = math.ceil(scaled_width / self.TILE_SIZE) + tiles_y = math.ceil(scaled_height / self.TILE_SIZE) + total_tiles = tiles_x * tiles_y + + # Step 4: Calculate final token count + return ( + total_tiles * self.HIGH_DETAIL_TILE_TOKENS + ) + self.LOW_DETAIL_IMAGE_TOKENS + + def count_content(self, content: Union[str, List[Union[str, dict]]]) -> int: + """Calculate tokens for message content""" + if not content: + return 0 + + if isinstance(content, str): + return self.count_text(content) + + token_count = 0 + for item in content: + if isinstance(item, str): + token_count += self.count_text(item) + elif isinstance(item, dict): + if "text" in item: + token_count += self.count_text(item["text"]) + elif "image_url" in item: + token_count += self.count_image(item) + return token_count + + def count_tool_calls(self, tool_calls: List[dict]) -> int: + """Calculate tokens for tool calls""" + token_count = 0 + for tool_call in tool_calls: + if "function" in tool_call: + function = tool_call["function"] + token_count += self.count_text(function.get("name", "")) + token_count += self.count_text(function.get("arguments", "")) + return token_count + + def count_message_tokens(self, messages: List[dict]) -> int: + """Calculate the total number of tokens in a message list""" + total_tokens = self.FORMAT_TOKENS # Base format tokens + + for message in messages: + tokens = self.BASE_MESSAGE_TOKENS # Base tokens per message + + # Add role tokens + tokens += self.count_text(message.get("role", "")) + + # Add content tokens + if "content" in message: + tokens += self.count_content(message["content"]) + + # Add tool calls tokens + if "tool_calls" in message: + tokens += self.count_tool_calls(message["tool_calls"]) + + # Add name and tool_call_id tokens + tokens += self.count_text(message.get("name", "")) + tokens += self.count_text(message.get("tool_call_id", "")) + + total_tokens += tokens + + return total_tokens + + +class LLM: + _instances: Dict[str, "LLM"] = {} + + def __new__( + cls, config_name: str = "default", llm_config: Optional[LLMSettings] = None + ): + if config_name not in cls._instances: + instance = super().__new__(cls) + instance.__init__(config_name, llm_config) + cls._instances[config_name] = instance + return cls._instances[config_name] + + def __init__( + self, config_name: str = "default", llm_config: Optional[LLMSettings] = None + ): + if not hasattr(self, "client"): # Only initialize if not already initialized + llm_config = llm_config or config.llm + llm_config = llm_config.get(config_name, llm_config["default"]) + self.model = llm_config.model + self.max_tokens = llm_config.max_tokens + self.temperature = llm_config.temperature + self.api_type = llm_config.api_type + self.api_key = llm_config.api_key + self.api_version = llm_config.api_version + self.base_url = llm_config.base_url + + # Add token counting related attributes + self.total_input_tokens = 0 + self.total_completion_tokens = 0 + self.max_input_tokens = ( + llm_config.max_input_tokens + if hasattr(llm_config, "max_input_tokens") + else None + ) + + # Initialize tokenizer + try: + self.tokenizer = tiktoken.encoding_for_model(self.model) + except KeyError: + # If the model is not in tiktoken's presets, use cl100k_base as default + self.tokenizer = tiktoken.get_encoding("cl100k_base") + + if self.api_type == "azure": + self.client = AsyncAzureOpenAI( + base_url=self.base_url, + api_key=self.api_key, + api_version=self.api_version, + ) + elif self.api_type == "aws": + self.client = BedrockClient() + else: + self.client = AsyncOpenAI(api_key=self.api_key, base_url=self.base_url) + + self.token_counter = TokenCounter(self.tokenizer) + + def count_tokens(self, text: str) -> int: + """Calculate the number of tokens in a text""" + if not text: + return 0 + return len(self.tokenizer.encode(text)) + + def count_message_tokens(self, messages: List[dict]) -> int: + return self.token_counter.count_message_tokens(messages) + + def update_token_count(self, input_tokens: int, completion_tokens: int = 0) -> None: + """Update token counts""" + # Only track tokens if max_input_tokens is set + self.total_input_tokens += input_tokens + self.total_completion_tokens += completion_tokens + logger.info( + f"Token usage: Input={input_tokens}, Completion={completion_tokens}, " + f"Cumulative Input={self.total_input_tokens}, Cumulative Completion={self.total_completion_tokens}, " + f"Total={input_tokens + completion_tokens}, Cumulative Total={self.total_input_tokens + self.total_completion_tokens}" + ) + + def check_token_limit(self, input_tokens: int) -> bool: + """Check if token limits are exceeded""" + if self.max_input_tokens is not None: + return (self.total_input_tokens + input_tokens) <= self.max_input_tokens + # If max_input_tokens is not set, always return True + return True + + def get_limit_error_message(self, input_tokens: int) -> str: + """Generate error message for token limit exceeded""" + if ( + self.max_input_tokens is not None + and (self.total_input_tokens + input_tokens) > self.max_input_tokens + ): + return f"Request may exceed input token limit (Current: {self.total_input_tokens}, Needed: {input_tokens}, Max: {self.max_input_tokens})" + + return "Token limit exceeded" + + @staticmethod + def format_messages( + messages: List[Union[dict, Message]], supports_images: bool = False + ) -> List[dict]: + """ + Format messages for LLM by converting them to OpenAI message format. + + Args: + messages: List of messages that can be either dict or Message objects + supports_images: Flag indicating if the target model supports image inputs + + Returns: + List[dict]: List of formatted messages in OpenAI format + + Raises: + ValueError: If messages are invalid or missing required fields + TypeError: If unsupported message types are provided + + Examples: + >>> msgs = [ + ... Message.system_message("You are a helpful assistant"), + ... {"role": "user", "content": "Hello"}, + ... Message.user_message("How are you?") + ... ] + >>> formatted = LLM.format_messages(msgs) + """ + formatted_messages = [] + + for message in messages: + # Convert Message objects to dictionaries + if isinstance(message, Message): + message = message.to_dict() + + if isinstance(message, dict): + # If message is a dict, ensure it has required fields + if "role" not in message: + raise ValueError("Message dict must contain 'role' field") + + # Process base64 images if present and model supports images + if supports_images and message.get("base64_image"): + # Initialize or convert content to appropriate format + if not message.get("content"): + message["content"] = [] + elif isinstance(message["content"], str): + message["content"] = [ + {"type": "text", "text": message["content"]} + ] + elif isinstance(message["content"], list): + # Convert string items to proper text objects + message["content"] = [ + ( + {"type": "text", "text": item} + if isinstance(item, str) + else item + ) + for item in message["content"] + ] + + # Add the image to content + message["content"].append( + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{message['base64_image']}" + }, + } + ) + + # Remove the base64_image field + del message["base64_image"] + # If model doesn't support images but message has base64_image, handle gracefully + elif not supports_images and message.get("base64_image"): + # Just remove the base64_image field and keep the text content + del message["base64_image"] + + if "content" in message or "tool_calls" in message: + formatted_messages.append(message) + # else: do not include the message + else: + raise TypeError(f"Unsupported message type: {type(message)}") + + # Validate all messages have required fields + for msg in formatted_messages: + if msg["role"] not in ROLE_VALUES: + raise ValueError(f"Invalid role: {msg['role']}") + + return formatted_messages + + @retry( + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), + retry=retry_if_exception_type( + (OpenAIError, Exception, ValueError) + ), # Don't retry TokenLimitExceeded + ) + async def ask( + self, + messages: List[Union[dict, Message]], + system_msgs: Optional[List[Union[dict, Message]]] = None, + stream: bool = True, + temperature: Optional[float] = None, + ) -> str: + """ + Send a prompt to the LLM and get the response. + + Args: + messages: List of conversation messages + system_msgs: Optional system messages to prepend + stream (bool): Whether to stream the response + temperature (float): Sampling temperature for the response + + Returns: + str: The generated response + + Raises: + TokenLimitExceeded: If token limits are exceeded + ValueError: If messages are invalid or response is empty + OpenAIError: If API call fails after retries + Exception: For unexpected errors + """ + try: + # Check if the model supports images + supports_images = self.model in MULTIMODAL_MODELS + + # Format system and user messages with image support check + if system_msgs: + system_msgs = self.format_messages(system_msgs, supports_images) + messages = system_msgs + self.format_messages(messages, supports_images) + else: + messages = self.format_messages(messages, supports_images) + + # Calculate input token count + input_tokens = self.count_message_tokens(messages) + + # Check if token limits are exceeded + if not self.check_token_limit(input_tokens): + error_message = self.get_limit_error_message(input_tokens) + # Raise a special exception that won't be retried + raise TokenLimitExceeded(error_message) + + params = { + "model": self.model, + "messages": messages, + } + + if self.model in REASONING_MODELS: + params["max_completion_tokens"] = self.max_tokens + else: + params["max_tokens"] = self.max_tokens + params["temperature"] = ( + temperature if temperature is not None else self.temperature + ) + + if not stream: + # Non-streaming request + response = await self.client.chat.completions.create( + **params, stream=False + ) + + if not response.choices or not response.choices[0].message.content: + raise ValueError("Empty or invalid response from LLM") + + # Update token counts + self.update_token_count( + response.usage.prompt_tokens, response.usage.completion_tokens + ) + + return response.choices[0].message.content + + # Streaming request, For streaming, update estimated token count before making the request + self.update_token_count(input_tokens) + + response = await self.client.chat.completions.create(**params, stream=True) + + collected_messages = [] + completion_text = "" + async for chunk in response: + chunk_message = chunk.choices[0].delta.content or "" + collected_messages.append(chunk_message) + completion_text += chunk_message + print(chunk_message, end="", flush=True) + + print() # Newline after streaming + full_response = "".join(collected_messages).strip() + if not full_response: + raise ValueError("Empty response from streaming LLM") + + # estimate completion tokens for streaming response + completion_tokens = self.count_tokens(completion_text) + logger.info( + f"Estimated completion tokens for streaming response: {completion_tokens}" + ) + self.total_completion_tokens += completion_tokens + + return full_response + + except TokenLimitExceeded: + # Re-raise token limit errors without logging + raise + except ValueError: + logger.exception(f"Validation error") + raise + except OpenAIError as oe: + logger.exception(f"OpenAI API error") + if isinstance(oe, AuthenticationError): + logger.error("Authentication failed. Check API key.") + elif isinstance(oe, RateLimitError): + logger.error("Rate limit exceeded. Consider increasing retry attempts.") + elif isinstance(oe, APIError): + logger.error(f"API error: {oe}") + raise + except Exception: + logger.exception(f"Unexpected error in ask") + raise + + @retry( + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), + retry=retry_if_exception_type( + (OpenAIError, Exception, ValueError) + ), # Don't retry TokenLimitExceeded + ) + async def ask_with_images( + self, + messages: List[Union[dict, Message]], + images: List[Union[str, dict]], + system_msgs: Optional[List[Union[dict, Message]]] = None, + stream: bool = False, + temperature: Optional[float] = None, + ) -> str: + """ + Send a prompt with images to the LLM and get the response. + + Args: + messages: List of conversation messages + images: List of image URLs or image data dictionaries + system_msgs: Optional system messages to prepend + stream (bool): Whether to stream the response + temperature (float): Sampling temperature for the response + + Returns: + str: The generated response + + Raises: + TokenLimitExceeded: If token limits are exceeded + ValueError: If messages are invalid or response is empty + OpenAIError: If API call fails after retries + Exception: For unexpected errors + """ + try: + # For ask_with_images, we always set supports_images to True because + # this method should only be called with models that support images + if self.model not in MULTIMODAL_MODELS: + raise ValueError( + f"Model {self.model} does not support images. Use a model from {MULTIMODAL_MODELS}" + ) + + # Format messages with image support + formatted_messages = self.format_messages(messages, supports_images=True) + + # Ensure the last message is from the user to attach images + if not formatted_messages or formatted_messages[-1]["role"] != "user": + raise ValueError( + "The last message must be from the user to attach images" + ) + + # Process the last user message to include images + last_message = formatted_messages[-1] + + # Convert content to multimodal format if needed + content = last_message["content"] + multimodal_content = ( + [{"type": "text", "text": content}] + if isinstance(content, str) + else content + if isinstance(content, list) + else [] + ) + + # Add images to content + for image in images: + if isinstance(image, str): + multimodal_content.append( + {"type": "image_url", "image_url": {"url": image}} + ) + elif isinstance(image, dict) and "url" in image: + multimodal_content.append({"type": "image_url", "image_url": image}) + elif isinstance(image, dict) and "image_url" in image: + multimodal_content.append(image) + else: + raise ValueError(f"Unsupported image format: {image}") + + # Update the message with multimodal content + last_message["content"] = multimodal_content + + # Add system messages if provided + if system_msgs: + all_messages = ( + self.format_messages(system_msgs, supports_images=True) + + formatted_messages + ) + else: + all_messages = formatted_messages + + # Calculate tokens and check limits + input_tokens = self.count_message_tokens(all_messages) + if not self.check_token_limit(input_tokens): + raise TokenLimitExceeded(self.get_limit_error_message(input_tokens)) + + # Set up API parameters + params = { + "model": self.model, + "messages": all_messages, + "stream": stream, + } + + # Add model-specific parameters + if self.model in REASONING_MODELS: + params["max_completion_tokens"] = self.max_tokens + else: + params["max_tokens"] = self.max_tokens + params["temperature"] = ( + temperature if temperature is not None else self.temperature + ) + + # Handle non-streaming request + if not stream: + response = await self.client.chat.completions.create(**params) + + if not response.choices or not response.choices[0].message.content: + raise ValueError("Empty or invalid response from LLM") + + self.update_token_count(response.usage.prompt_tokens) + return response.choices[0].message.content + + # Handle streaming request + self.update_token_count(input_tokens) + response = await self.client.chat.completions.create(**params) + + collected_messages = [] + async for chunk in response: + chunk_message = chunk.choices[0].delta.content or "" + collected_messages.append(chunk_message) + print(chunk_message, end="", flush=True) + + print() # Newline after streaming + full_response = "".join(collected_messages).strip() + + if not full_response: + raise ValueError("Empty response from streaming LLM") + + return full_response + + except TokenLimitExceeded: + raise + except ValueError as ve: + logger.error(f"Validation error in ask_with_images: {ve}") + raise + except OpenAIError as oe: + logger.error(f"OpenAI API error: {oe}") + if isinstance(oe, AuthenticationError): + logger.error("Authentication failed. Check API key.") + elif isinstance(oe, RateLimitError): + logger.error("Rate limit exceeded. Consider increasing retry attempts.") + elif isinstance(oe, APIError): + logger.error(f"API error: {oe}") + raise + except Exception as e: + logger.error(f"Unexpected error in ask_with_images: {e}") + raise + + @retry( + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), + retry=retry_if_exception_type( + (OpenAIError, Exception, ValueError) + ), # Don't retry TokenLimitExceeded + ) + async def ask_tool( + self, + messages: List[Union[dict, Message]], + system_msgs: Optional[List[Union[dict, Message]]] = None, + timeout: int = 300, + tools: Optional[List[dict]] = None, + tool_choice: TOOL_CHOICE_TYPE = ToolChoice.AUTO, # type: ignore + temperature: Optional[float] = None, + **kwargs, + ) -> ChatCompletionMessage | None: + """ + Ask LLM using functions/tools and return the response. + + Args: + messages: List of conversation messages + system_msgs: Optional system messages to prepend + timeout: Request timeout in seconds + tools: List of tools to use + tool_choice: Tool choice strategy + temperature: Sampling temperature for the response + **kwargs: Additional completion arguments + + Returns: + ChatCompletionMessage: The model's response + + Raises: + TokenLimitExceeded: If token limits are exceeded + ValueError: If tools, tool_choice, or messages are invalid + OpenAIError: If API call fails after retries + Exception: For unexpected errors + """ + try: + # Validate tool_choice + if tool_choice not in TOOL_CHOICE_VALUES: + raise ValueError(f"Invalid tool_choice: {tool_choice}") + + # Check if the model supports images + supports_images = self.model in MULTIMODAL_MODELS + + # Format messages + if system_msgs: + system_msgs = self.format_messages(system_msgs, supports_images) + messages = system_msgs + self.format_messages(messages, supports_images) + else: + messages = self.format_messages(messages, supports_images) + + # Calculate input token count + input_tokens = self.count_message_tokens(messages) + + # If there are tools, calculate token count for tool descriptions + tools_tokens = 0 + if tools: + for tool in tools: + tools_tokens += self.count_tokens(str(tool)) + + input_tokens += tools_tokens + + # Check if token limits are exceeded + if not self.check_token_limit(input_tokens): + error_message = self.get_limit_error_message(input_tokens) + # Raise a special exception that won't be retried + raise TokenLimitExceeded(error_message) + + # Validate tools if provided + if tools: + for tool in tools: + if not isinstance(tool, dict) or "type" not in tool: + raise ValueError("Each tool must be a dict with 'type' field") + + # Set up the completion request + params = { + "model": self.model, + "messages": messages, + "tools": tools, + "tool_choice": tool_choice, + "timeout": timeout, + **kwargs, + } + + if self.model in REASONING_MODELS: + params["max_completion_tokens"] = self.max_tokens + else: + params["max_tokens"] = self.max_tokens + params["temperature"] = ( + temperature if temperature is not None else self.temperature + ) + + params["stream"] = False # Always use non-streaming for tool requests + response: ChatCompletion = await self.client.chat.completions.create( + **params + ) + + # Check if response is valid + if not response.choices or not response.choices[0].message: + print(response) + # raise ValueError("Invalid or empty response from LLM") + return None + + # Update token counts + self.update_token_count( + response.usage.prompt_tokens, response.usage.completion_tokens + ) + + return response.choices[0].message + + except TokenLimitExceeded: + # Re-raise token limit errors without logging + raise + except ValueError as ve: + logger.error(f"Validation error in ask_tool: {ve}") + raise + except OpenAIError as oe: + logger.error(f"OpenAI API error: {oe}") + if isinstance(oe, AuthenticationError): + logger.error("Authentication failed. Check API key.") + elif isinstance(oe, RateLimitError): + logger.error("Rate limit exceeded. Consider increasing retry attempts.") + elif isinstance(oe, APIError): + logger.error(f"API error: {oe}") + raise + except Exception as e: + logger.error(f"Unexpected error in ask_tool: {e}") + raise diff --git a/app/logger.py b/app/logger.py new file mode 100644 index 0000000000000000000000000000000000000000..c5d9ce18cad06b2a9221c7a6564c79bb5169df4c --- /dev/null +++ b/app/logger.py @@ -0,0 +1,42 @@ +import sys +from datetime import datetime + +from loguru import logger as _logger + +from app.config import PROJECT_ROOT + + +_print_level = "INFO" + + +def define_log_level(print_level="INFO", logfile_level="DEBUG", name: str = None): + """Adjust the log level to above level""" + global _print_level + _print_level = print_level + + current_date = datetime.now() + formatted_date = current_date.strftime("%Y%m%d%H%M%S") + log_name = ( + f"{name}_{formatted_date}" if name else formatted_date + ) # name a log with prefix name + + _logger.remove() + _logger.add(sys.stderr, level=print_level) + _logger.add(PROJECT_ROOT / f"logs/{log_name}.log", level=logfile_level) + return _logger + + +logger = define_log_level() + + +if __name__ == "__main__": + logger.info("Starting application") + logger.debug("Debug message") + logger.warning("Warning message") + logger.error("Error message") + logger.critical("Critical message") + + try: + raise ValueError("Test error") + except Exception as e: + logger.exception(f"An error occurred: {e}") diff --git a/app/mcp/__init__.py b/app/mcp/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/app/mcp/server.py b/app/mcp/server.py new file mode 100644 index 0000000000000000000000000000000000000000..3ee8b08c9379c6b745bd82bc419a4635c9903d56 --- /dev/null +++ b/app/mcp/server.py @@ -0,0 +1,180 @@ +import logging +import sys + + +logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stderr)]) + +import argparse +import asyncio +import atexit +import json +from inspect import Parameter, Signature +from typing import Any, Dict, Optional + +from mcp.server.fastmcp import FastMCP + +from app.logger import logger +from app.tool.base import BaseTool +from app.tool.bash import Bash +from app.tool.browser_use_tool import BrowserUseTool +from app.tool.str_replace_editor import StrReplaceEditor +from app.tool.terminate import Terminate + + +class MCPServer: + """MCP Server implementation with tool registration and management.""" + + def __init__(self, name: str = "openmanus"): + self.server = FastMCP(name) + self.tools: Dict[str, BaseTool] = {} + + # Initialize standard tools + self.tools["bash"] = Bash() + self.tools["browser"] = BrowserUseTool() + self.tools["editor"] = StrReplaceEditor() + self.tools["terminate"] = Terminate() + + def register_tool(self, tool: BaseTool, method_name: Optional[str] = None) -> None: + """Register a tool with parameter validation and documentation.""" + tool_name = method_name or tool.name + tool_param = tool.to_param() + tool_function = tool_param["function"] + + # Define the async function to be registered + async def tool_method(**kwargs): + logger.info(f"Executing {tool_name}: {kwargs}") + result = await tool.execute(**kwargs) + + logger.info(f"Result of {tool_name}: {result}") + + # Handle different types of results (match original logic) + if hasattr(result, "model_dump"): + return json.dumps(result.model_dump()) + elif isinstance(result, dict): + return json.dumps(result) + return result + + # Set method metadata + tool_method.__name__ = tool_name + tool_method.__doc__ = self._build_docstring(tool_function) + tool_method.__signature__ = self._build_signature(tool_function) + + # Store parameter schema (important for tools that access it programmatically) + param_props = tool_function.get("parameters", {}).get("properties", {}) + required_params = tool_function.get("parameters", {}).get("required", []) + tool_method._parameter_schema = { + param_name: { + "description": param_details.get("description", ""), + "type": param_details.get("type", "any"), + "required": param_name in required_params, + } + for param_name, param_details in param_props.items() + } + + # Register with server + self.server.tool()(tool_method) + logger.info(f"Registered tool: {tool_name}") + + def _build_docstring(self, tool_function: dict) -> str: + """Build a formatted docstring from tool function metadata.""" + description = tool_function.get("description", "") + param_props = tool_function.get("parameters", {}).get("properties", {}) + required_params = tool_function.get("parameters", {}).get("required", []) + + # Build docstring (match original format) + docstring = description + if param_props: + docstring += "\n\nParameters:\n" + for param_name, param_details in param_props.items(): + required_str = ( + "(required)" if param_name in required_params else "(optional)" + ) + param_type = param_details.get("type", "any") + param_desc = param_details.get("description", "") + docstring += ( + f" {param_name} ({param_type}) {required_str}: {param_desc}\n" + ) + + return docstring + + def _build_signature(self, tool_function: dict) -> Signature: + """Build a function signature from tool function metadata.""" + param_props = tool_function.get("parameters", {}).get("properties", {}) + required_params = tool_function.get("parameters", {}).get("required", []) + + parameters = [] + + # Follow original type mapping + for param_name, param_details in param_props.items(): + param_type = param_details.get("type", "") + default = Parameter.empty if param_name in required_params else None + + # Map JSON Schema types to Python types (same as original) + annotation = Any + if param_type == "string": + annotation = str + elif param_type == "integer": + annotation = int + elif param_type == "number": + annotation = float + elif param_type == "boolean": + annotation = bool + elif param_type == "object": + annotation = dict + elif param_type == "array": + annotation = list + + # Create parameter with same structure as original + param = Parameter( + name=param_name, + kind=Parameter.KEYWORD_ONLY, + default=default, + annotation=annotation, + ) + parameters.append(param) + + return Signature(parameters=parameters) + + async def cleanup(self) -> None: + """Clean up server resources.""" + logger.info("Cleaning up resources") + # Follow original cleanup logic - only clean browser tool + if "browser" in self.tools and hasattr(self.tools["browser"], "cleanup"): + await self.tools["browser"].cleanup() + + def register_all_tools(self) -> None: + """Register all tools with the server.""" + for tool in self.tools.values(): + self.register_tool(tool) + + def run(self, transport: str = "stdio") -> None: + """Run the MCP server.""" + # Register all tools + self.register_all_tools() + + # Register cleanup function (match original behavior) + atexit.register(lambda: asyncio.run(self.cleanup())) + + # Start server (with same logging as original) + logger.info(f"Starting OpenManus server ({transport} mode)") + self.server.run(transport=transport) + + +def parse_args() -> argparse.Namespace: + """Parse command line arguments.""" + parser = argparse.ArgumentParser(description="OpenManus MCP Server") + parser.add_argument( + "--transport", + choices=["stdio"], + default="stdio", + help="Communication method: stdio or http (default: stdio)", + ) + return parser.parse_args() + + +if __name__ == "__main__": + args = parse_args() + + # Create and run server (maintaining original flow) + server = MCPServer() + server.run(transport=args.transport) diff --git a/app/production_config.py b/app/production_config.py new file mode 100644 index 0000000000000000000000000000000000000000..8384516abbcc5183577a4ec89ac7b638d989fd44 --- /dev/null +++ b/app/production_config.py @@ -0,0 +1,363 @@ +""" +Complete Configuration for OpenManus Production Deployment +Includes: All model configurations, agent settings, category mappings, and service configurations +""" + +import os +from typing import Dict, List, Optional, Any +from dataclasses import dataclass +from enum import Enum + + +@dataclass +class ModelConfig: + """Configuration for individual AI models""" + + name: str + category: str + api_endpoint: str + max_tokens: int = 4096 + temperature: float = 0.7 + supported_formats: List[str] = None + special_parameters: Dict[str, Any] = None + rate_limit: int = 100 # requests per minute + + +class CategoryConfig: + """Configuration for model categories""" + + # Core AI Models - Text Generation (Qwen, DeepSeek, etc.) + TEXT_GENERATION_MODELS = { + # Qwen Models (35 models) + "qwen/qwen-2.5-72b-instruct": ModelConfig( + name="Qwen 2.5 72B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct", + max_tokens=8192, + temperature=0.7, + ), + "qwen/qwen-2.5-32b-instruct": ModelConfig( + name="Qwen 2.5 32B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-32B-Instruct", + max_tokens=8192, + ), + "qwen/qwen-2.5-14b-instruct": ModelConfig( + name="Qwen 2.5 14B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-14B-Instruct", + max_tokens=8192, + ), + "qwen/qwen-2.5-7b-instruct": ModelConfig( + name="Qwen 2.5 7B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct", + ), + "qwen/qwen-2.5-3b-instruct": ModelConfig( + name="Qwen 2.5 3B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-3B-Instruct", + ), + "qwen/qwen-2.5-1.5b-instruct": ModelConfig( + name="Qwen 2.5 1.5B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-1.5B-Instruct", + ), + "qwen/qwen-2.5-0.5b-instruct": ModelConfig( + name="Qwen 2.5 0.5B Instruct", + category="text-generation", + api_endpoint="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-0.5B-Instruct", + ), + # ... (Add all 35 Qwen models) + # DeepSeek Models (17 models) + "deepseek-ai/deepseek-coder-33b-instruct": ModelConfig( + name="DeepSeek Coder 33B Instruct", + category="code-generation", + api_endpoint="https://api-inference.huggingface.co/models/deepseek-ai/deepseek-coder-33b-instruct", + max_tokens=8192, + special_parameters={"code_focused": True}, + ), + "deepseek-ai/deepseek-coder-6.7b-instruct": ModelConfig( + name="DeepSeek Coder 6.7B Instruct", + category="code-generation", + api_endpoint="https://api-inference.huggingface.co/models/deepseek-ai/deepseek-coder-6.7b-instruct", + ), + # ... (Add all 17 DeepSeek models) + } + + # Image Editing Models (10 models) + IMAGE_EDITING_MODELS = { + "stabilityai/stable-diffusion-xl-refiner-1.0": ModelConfig( + name="SDXL Refiner 1.0", + category="image-editing", + api_endpoint="https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-refiner-1.0", + supported_formats=["image/png", "image/jpeg"], + ), + "runwayml/stable-diffusion-inpainting": ModelConfig( + name="Stable Diffusion Inpainting", + category="image-inpainting", + api_endpoint="https://api-inference.huggingface.co/models/runwayml/stable-diffusion-inpainting", + supported_formats=["image/png", "image/jpeg"], + ), + # ... (Add all 10 image editing models) + } + + # TTS/STT Models (15 models) + SPEECH_MODELS = { + "microsoft/speecht5_tts": ModelConfig( + name="SpeechT5 TTS", + category="text-to-speech", + api_endpoint="https://api-inference.huggingface.co/models/microsoft/speecht5_tts", + supported_formats=["audio/wav", "audio/mp3"], + ), + "openai/whisper-large-v3": ModelConfig( + name="Whisper Large v3", + category="automatic-speech-recognition", + api_endpoint="https://api-inference.huggingface.co/models/openai/whisper-large-v3", + supported_formats=["audio/wav", "audio/mp3", "audio/flac"], + ), + # ... (Add all 15 speech models) + } + + # Face Swap Models (6 models) + FACE_SWAP_MODELS = { + "deepinsight/insightface": ModelConfig( + name="InsightFace", + category="face-swap", + api_endpoint="https://api-inference.huggingface.co/models/deepinsight/insightface", + supported_formats=["image/png", "image/jpeg"], + ), + # ... (Add all 6 face swap models) + } + + # Talking Avatar Models (9 models) + AVATAR_MODELS = { + "microsoft/DiT-XL-2-512": ModelConfig( + name="DiT Avatar Generator", + category="talking-avatar", + api_endpoint="https://api-inference.huggingface.co/models/microsoft/DiT-XL-2-512", + supported_formats=["video/mp4", "image/png"], + ), + # ... (Add all 9 avatar models) + } + + # Arabic-English Interactive Models (12 models) + ARABIC_ENGLISH_MODELS = { + "aubmindlab/bert-base-arabertv02": ModelConfig( + name="AraBERT v02", + category="arabic-text", + api_endpoint="https://api-inference.huggingface.co/models/aubmindlab/bert-base-arabertv02", + special_parameters={"language": "ar-en"}, + ), + "UBC-NLP/MARBERT": ModelConfig( + name="MARBERT", + category="arabic-text", + api_endpoint="https://api-inference.huggingface.co/models/UBC-NLP/MARBERT", + special_parameters={"language": "ar-en"}, + ), + # ... (Add all 12 Arabic-English models) + } + + +class AgentConfig: + """Configuration for AI Agents""" + + # Manus Agent Configuration + MANUS_AGENT = { + "name": "Manus", + "description": "Versatile AI agent with 200+ models", + "max_steps": 20, + "max_observe": 10000, + "system_prompt_template": """You are Manus, an advanced AI agent with access to 200+ specialized models. + +Available categories: +- Text Generation (Qwen, DeepSeek, etc.) +- Image Editing & Generation +- Speech (TTS/STT) +- Face Swap & Avatar Generation +- Arabic-English Interactive Models +- Code Generation & Review +- Multimodal AI +- Document Processing +- 3D Generation +- Video Processing + +User workspace: {directory}""", + "tools": [ + "PythonExecute", + "BrowserUseTool", + "StrReplaceEditor", + "AskHuman", + "Terminate", + "HuggingFaceModels", + ], + "model_preferences": { + "text": "qwen/qwen-2.5-72b-instruct", + "code": "deepseek-ai/deepseek-coder-33b-instruct", + "image": "stabilityai/stable-diffusion-xl-refiner-1.0", + "speech": "microsoft/speecht5_tts", + "arabic": "aubmindlab/bert-base-arabertv02", + }, + } + + +class ServiceConfig: + """Configuration for all services""" + + # Cloudflare Services + CLOUDFLARE_CONFIG = { + "d1_database": { + "enabled": True, + "tables": ["users", "sessions", "agent_interactions", "model_usage"], + "auto_migrate": True, + }, + "r2_storage": { + "enabled": True, + "buckets": ["user-files", "generated-content", "model-cache"], + "max_file_size": "100MB", + }, + "kv_storage": { + "enabled": True, + "namespaces": ["sessions", "model-cache", "user-preferences"], + "ttl": 86400, # 24 hours + }, + "durable_objects": { + "enabled": True, + "classes": ["ChatSession", "ModelRouter", "UserContext"], + }, + } + + # Authentication Configuration + AUTH_CONFIG = { + "method": "mobile_password", + "password_min_length": 8, + "session_duration": 86400, # 24 hours + "max_concurrent_sessions": 5, + "mobile_validation": { + "international": True, + "formats": ["+1234567890", "01234567890"], + }, + } + + # Model Usage Configuration + MODEL_CONFIG = { + "rate_limits": { + "free_tier": 100, # requests per day + "premium_tier": 1000, + "enterprise_tier": 10000, + }, + "fallback_models": { + "text": ["qwen/qwen-2.5-7b-instruct", "qwen/qwen-2.5-3b-instruct"], + "image": ["runwayml/stable-diffusion-v1-5"], + "code": ["deepseek-ai/deepseek-coder-6.7b-instruct"], + }, + "cache_settings": {"enabled": True, "ttl": 3600, "max_size": "1GB"}, # 1 hour + } + + +class EnvironmentConfig: + """Environment-specific configurations""" + + @staticmethod + def get_production_config(): + """Get production environment configuration""" + return { + "environment": "production", + "debug": False, + "log_level": "INFO", + "server": {"host": "0.0.0.0", "port": 7860, "workers": 4}, + "database": {"type": "sqlite", "url": "auth.db", "pool_size": 10}, + "security": { + "secret_key": os.getenv("SECRET_KEY", "your-secret-key"), + "cors_origins": ["*"], + "rate_limiting": True, + }, + "monitoring": {"metrics": True, "logging": True, "health_checks": True}, + } + + @staticmethod + def get_development_config(): + """Get development environment configuration""" + return { + "environment": "development", + "debug": True, + "log_level": "DEBUG", + "server": {"host": "127.0.0.1", "port": 7860, "workers": 1}, + "database": {"type": "sqlite", "url": "auth_dev.db", "pool_size": 2}, + "security": { + "secret_key": "dev-secret-key", + "cors_origins": ["http://localhost:*"], + "rate_limiting": False, + }, + } + + +# Global configuration instance +class OpenManusConfig: + """Main configuration class for OpenManus""" + + def __init__(self, environment: str = "production"): + self.environment = environment + self.categories = CategoryConfig() + self.agent = AgentConfig() + self.services = ServiceConfig() + + if environment == "production": + self.env_config = EnvironmentConfig.get_production_config() + else: + self.env_config = EnvironmentConfig.get_development_config() + + def get_model_config(self, model_id: str) -> Optional[ModelConfig]: + """Get configuration for a specific model""" + all_models = { + **self.categories.TEXT_GENERATION_MODELS, + **self.categories.IMAGE_EDITING_MODELS, + **self.categories.SPEECH_MODELS, + **self.categories.FACE_SWAP_MODELS, + **self.categories.AVATAR_MODELS, + **self.categories.ARABIC_ENGLISH_MODELS, + } + return all_models.get(model_id) + + def get_category_models(self, category: str) -> Dict[str, ModelConfig]: + """Get all models in a category""" + if category == "text-generation": + return self.categories.TEXT_GENERATION_MODELS + elif category == "image-editing": + return self.categories.IMAGE_EDITING_MODELS + elif category in ["text-to-speech", "automatic-speech-recognition"]: + return self.categories.SPEECH_MODELS + elif category == "face-swap": + return self.categories.FACE_SWAP_MODELS + elif category == "talking-avatar": + return self.categories.AVATAR_MODELS + elif category == "arabic-text": + return self.categories.ARABIC_ENGLISH_MODELS + else: + return {} + + def validate_config(self) -> bool: + """Validate the configuration""" + try: + # Check required environment variables + required_env = ( + ["CLOUDFLARE_API_TOKEN", "HF_TOKEN"] + if self.environment == "production" + else [] + ) + missing_env = [var for var in required_env if not os.getenv(var)] + + if missing_env: + print(f"Missing required environment variables: {missing_env}") + return False + + print(f"Configuration validated for {self.environment} environment") + return True + + except Exception as e: + print(f"Configuration validation failed: {e}") + return False + + +# Create global config instance +config = OpenManusConfig(environment=os.getenv("ENVIRONMENT", "production")) diff --git a/app/prompt/__init__.py b/app/prompt/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/app/prompt/browser.py b/app/prompt/browser.py new file mode 100644 index 0000000000000000000000000000000000000000..b27714d5fb7e3621734f09cab22c4e908bae7b28 --- /dev/null +++ b/app/prompt/browser.py @@ -0,0 +1,94 @@ +SYSTEM_PROMPT = """\ +You are an AI agent designed to automate browser tasks. Your goal is to accomplish the ultimate task following the rules. + +# Input Format +Task +Previous steps +Current URL +Open Tabs +Interactive Elements +[index]text +- index: Numeric identifier for interaction +- type: HTML element type (button, input, etc.) +- text: Element description +Example: +[33] + +- Only elements with numeric indexes in [] are interactive +- elements without [] provide only context + +# Response Rules +1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format: +{{"current_state": {{"evaluation_previous_goal": "Success|Failed|Unknown - Analyze the current elements and the image to check if the previous goals/actions are successful like intended by the task. Mention if something unexpected happened. Shortly state why/why not", +"memory": "Description of what has been done and what you need to remember. Be very specific. Count here ALWAYS how many times you have done something and how many remain. E.g. 0 out of 10 websites analyzed. Continue with abc and xyz", +"next_goal": "What needs to be done with the next immediate action"}}, +"action":[{{"one_action_name": {{// action-specific parameter}}}}, // ... more actions in sequence]}} + +2. ACTIONS: You can specify multiple actions in the list to be executed in sequence. But always specify only one action name per item. Use maximum {{max_actions}} actions per sequence. +Common action sequences: +- Form filling: [{{"input_text": {{"index": 1, "text": "username"}}}}, {{"input_text": {{"index": 2, "text": "password"}}}}, {{"click_element": {{"index": 3}}}}] +- Navigation and extraction: [{{"go_to_url": {{"url": "https://example.com"}}}}, {{"extract_content": {{"goal": "extract the names"}}}}] +- Actions are executed in the given order +- If the page changes after an action, the sequence is interrupted and you get the new state. +- Only provide the action sequence until an action which changes the page state significantly. +- Try to be efficient, e.g. fill forms at once, or chain actions where nothing changes on the page +- only use multiple actions if it makes sense. + +3. ELEMENT INTERACTION: +- Only use indexes of the interactive elements +- Elements marked with "[]Non-interactive text" are non-interactive + +4. NAVIGATION & ERROR HANDLING: +- If no suitable elements exist, use other functions to complete the task +- If stuck, try alternative approaches - like going back to a previous page, new search, new tab etc. +- Handle popups/cookies by accepting or closing them +- Use scroll to find elements you are looking for +- If you want to research something, open a new tab instead of using the current tab +- If captcha pops up, try to solve it - else try a different approach +- If the page is not fully loaded, use wait action + +5. TASK COMPLETION: +- Use the done action as the last action as soon as the ultimate task is complete +- Dont use "done" before you are done with everything the user asked you, except you reach the last step of max_steps. +- If you reach your last step, use the done action even if the task is not fully finished. Provide all the information you have gathered so far. If the ultimate task is completly finished set success to true. If not everything the user asked for is completed set success in done to false! +- If you have to do something repeatedly for example the task says for "each", or "for all", or "x times", count always inside "memory" how many times you have done it and how many remain. Don't stop until you have completed like the task asked you. Only call done after the last step. +- Don't hallucinate actions +- Make sure you include everything you found out for the ultimate task in the done text parameter. Do not just say you are done, but include the requested information of the task. + +6. VISUAL CONTEXT: +- When an image is provided, use it to understand the page layout +- Bounding boxes with labels on their top right corner correspond to element indexes + +7. Form filling: +- If you fill an input field and your action sequence is interrupted, most often something changed e.g. suggestions popped up under the field. + +8. Long tasks: +- Keep track of the status and subresults in the memory. + +9. Extraction: +- If your task is to find information - call extract_content on the specific pages to get and store the information. +Your responses must be always JSON with the specified format. +""" + +NEXT_STEP_PROMPT = """ +What should I do next to achieve my goal? + +When you see [Current state starts here], focus on the following: +- Current URL and page title{url_placeholder} +- Available tabs{tabs_placeholder} +- Interactive elements and their indices +- Content above{content_above_placeholder} or below{content_below_placeholder} the viewport (if indicated) +- Any action results or errors{results_placeholder} + +For browser interactions: +- To navigate: browser_use with action="go_to_url", url="..." +- To click: browser_use with action="click_element", index=N +- To type: browser_use with action="input_text", index=N, text="..." +- To extract: browser_use with action="extract_content", goal="..." +- To scroll: browser_use with action="scroll_down" or "scroll_up" + +Consider both what's visible and what might be beyond the current viewport. +Be methodical - remember your progress and what you've learned so far. + +If you want to stop the interaction at any point, use the `terminate` tool/function call. +""" diff --git a/app/prompt/manus.py b/app/prompt/manus.py new file mode 100644 index 0000000000000000000000000000000000000000..99e7e8315b9de1673980bdd109718795ff97a67c --- /dev/null +++ b/app/prompt/manus.py @@ -0,0 +1,10 @@ +SYSTEM_PROMPT = ( + "You are OpenManus, an all-capable AI assistant, aimed at solving any task presented by the user. You have various tools at your disposal that you can call upon to efficiently complete complex requests. Whether it's programming, information retrieval, file processing, web browsing, or human interaction (only for extreme cases), you can handle it all." + "The initial directory is: {directory}" +) + +NEXT_STEP_PROMPT = """ +Based on user needs, proactively select the most appropriate tool or combination of tools. For complex tasks, you can break down the problem and use different tools step by step to solve it. After using each tool, clearly explain the execution results and suggest the next steps. + +If you want to stop the interaction at any point, use the `terminate` tool/function call. +""" diff --git a/app/prompt/mcp.py b/app/prompt/mcp.py new file mode 100644 index 0000000000000000000000000000000000000000..acf15b2a25a9bf85205c3bac9e5dad8a0ed52c50 --- /dev/null +++ b/app/prompt/mcp.py @@ -0,0 +1,43 @@ +"""Prompts for the MCP Agent.""" + +SYSTEM_PROMPT = """You are an AI assistant with access to a Model Context Protocol (MCP) server. +You can use the tools provided by the MCP server to complete tasks. +The MCP server will dynamically expose tools that you can use - always check the available tools first. + +When using an MCP tool: +1. Choose the appropriate tool based on your task requirements +2. Provide properly formatted arguments as required by the tool +3. Observe the results and use them to determine next steps +4. Tools may change during operation - new tools might appear or existing ones might disappear + +Follow these guidelines: +- Call tools with valid parameters as documented in their schemas +- Handle errors gracefully by understanding what went wrong and trying again with corrected parameters +- For multimedia responses (like images), you'll receive a description of the content +- Complete user requests step by step, using the most appropriate tools +- If multiple tools need to be called in sequence, make one call at a time and wait for results + +Remember to clearly explain your reasoning and actions to the user. +""" + +NEXT_STEP_PROMPT = """Based on the current state and available tools, what should be done next? +Think step by step about the problem and identify which MCP tool would be most helpful for the current stage. +If you've already made progress, consider what additional information you need or what actions would move you closer to completing the task. +""" + +# Additional specialized prompts +TOOL_ERROR_PROMPT = """You encountered an error with the tool '{tool_name}'. +Try to understand what went wrong and correct your approach. +Common issues include: +- Missing or incorrect parameters +- Invalid parameter formats +- Using a tool that's no longer available +- Attempting an operation that's not supported + +Please check the tool specifications and try again with corrected parameters. +""" + +MULTIMEDIA_RESPONSE_PROMPT = """You've received a multimedia response (image, audio, etc.) from the tool '{tool_name}'. +This content has been processed and described for you. +Use this information to continue the task or provide insights to the user. +""" diff --git a/app/prompt/planning.py b/app/prompt/planning.py new file mode 100644 index 0000000000000000000000000000000000000000..bd5f4ce7c429d6060bf9566a2c900900605fb149 --- /dev/null +++ b/app/prompt/planning.py @@ -0,0 +1,27 @@ +PLANNING_SYSTEM_PROMPT = """ +You are an expert Planning Agent tasked with solving problems efficiently through structured plans. +Your job is: +1. Analyze requests to understand the task scope +2. Create a clear, actionable plan that makes meaningful progress with the `planning` tool +3. Execute steps using available tools as needed +4. Track progress and adapt plans when necessary +5. Use `finish` to conclude immediately when the task is complete + + +Available tools will vary by task but may include: +- `planning`: Create, update, and track plans (commands: create, update, mark_step, etc.) +- `finish`: End the task when complete +Break tasks into logical steps with clear outcomes. Avoid excessive detail or sub-steps. +Think about dependencies and verification methods. +Know when to conclude - don't continue thinking once objectives are met. +""" + +NEXT_STEP_PROMPT = """ +Based on the current state, what's your next action? +Choose the most efficient path forward: +1. Is the plan sufficient, or does it need refinement? +2. Can you execute the next step immediately? +3. Is the task complete? If so, use `finish` right away. + +Be concise in your reasoning, then select the appropriate tool or action. +""" diff --git a/app/prompt/swe.py b/app/prompt/swe.py new file mode 100644 index 0000000000000000000000000000000000000000..389e74b814aaaadc3033960d61274646864bf97e --- /dev/null +++ b/app/prompt/swe.py @@ -0,0 +1,22 @@ +SYSTEM_PROMPT = """SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface. + +The special interface consists of a file editor that shows you {{WINDOW}} lines of a file at a time. +In addition to typical bash commands, you can also use specific commands to help you navigate and edit files. +To call a command, you need to invoke it with a function call/tool call. + +Please note that THE EDIT COMMAND REQUIRES PROPER INDENTATION. +If you'd like to add the line ' print(x)' you must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run. + +RESPONSE FORMAT: +Your shell prompt is formatted as follows: +(Open file: ) +(Current directory: ) +bash-$ + +First, you should _always_ include a general thought about what you're going to do next. +Then, for every response, you must include exactly _ONE_ tool call/function call. + +Remember, you should always include a _SINGLE_ tool call/function call and then wait for a response from the shell before continuing with more discussion and commands. Everything you include in the DISCUSSION section will be saved for future reference. +If you'd like to issue two commands at once, PLEASE DO NOT DO THAT! Please instead first submit just the first tool call, and then after receiving a response you'll be able to issue the second tool call. +Note that the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them. +""" diff --git a/app/prompt/toolcall.py b/app/prompt/toolcall.py new file mode 100644 index 0000000000000000000000000000000000000000..e1a3be93936b0f81f97369209bd6d0d3b71c0f95 --- /dev/null +++ b/app/prompt/toolcall.py @@ -0,0 +1,5 @@ +SYSTEM_PROMPT = "You are an agent that can execute tool calls" + +NEXT_STEP_PROMPT = ( + "If you want to stop interaction, use `terminate` tool/function call." +) diff --git a/app/prompt/visualization.py b/app/prompt/visualization.py new file mode 100644 index 0000000000000000000000000000000000000000..8e4fecc533583495fab7a7c6726362e6fba049d9 --- /dev/null +++ b/app/prompt/visualization.py @@ -0,0 +1,10 @@ +SYSTEM_PROMPT = """You are an AI agent designed to data analysis / visualization task. You have various tools at your disposal that you can call upon to efficiently complete complex requests. +# Note: +1. The workspace directory is: {directory}; Read / write file in workspace +2. Generate analysis conclusion report in the end""" + +NEXT_STEP_PROMPT = """Based on user needs, break down the problem and use different tools step by step to solve it. +# Note +1. Each step select the most appropriate tool proactively (ONLY ONE). +2. After using each tool, clearly explain the execution results and suggest the next steps. +3. When observation with Error, review and fix it.""" diff --git a/app/sandbox/__init__.py b/app/sandbox/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..ccf0df6d83e3d2c2e8ece52ca501a094e3bb8d58 --- /dev/null +++ b/app/sandbox/__init__.py @@ -0,0 +1,30 @@ +""" +Docker Sandbox Module + +Provides secure containerized execution environment with resource limits +and isolation for running untrusted code. +""" +from app.sandbox.client import ( + BaseSandboxClient, + LocalSandboxClient, + create_sandbox_client, +) +from app.sandbox.core.exceptions import ( + SandboxError, + SandboxResourceError, + SandboxTimeoutError, +) +from app.sandbox.core.manager import SandboxManager +from app.sandbox.core.sandbox import DockerSandbox + + +__all__ = [ + "DockerSandbox", + "SandboxManager", + "BaseSandboxClient", + "LocalSandboxClient", + "create_sandbox_client", + "SandboxError", + "SandboxTimeoutError", + "SandboxResourceError", +] diff --git a/app/sandbox/client.py b/app/sandbox/client.py new file mode 100644 index 0000000000000000000000000000000000000000..09a8f2e84fea5557f12665d8ebc6ee3284eccf01 --- /dev/null +++ b/app/sandbox/client.py @@ -0,0 +1,201 @@ +from abc import ABC, abstractmethod +from typing import Dict, Optional, Protocol + +from app.config import SandboxSettings +from app.sandbox.core.sandbox import DockerSandbox + + +class SandboxFileOperations(Protocol): + """Protocol for sandbox file operations.""" + + async def copy_from(self, container_path: str, local_path: str) -> None: + """Copies file from container to local. + + Args: + container_path: File path in container. + local_path: Local destination path. + """ + ... + + async def copy_to(self, local_path: str, container_path: str) -> None: + """Copies file from local to container. + + Args: + local_path: Local source file path. + container_path: Destination path in container. + """ + ... + + async def read_file(self, path: str) -> str: + """Reads file content from container. + + Args: + path: File path in container. + + Returns: + str: File content. + """ + ... + + async def write_file(self, path: str, content: str) -> None: + """Writes content to file in container. + + Args: + path: File path in container. + content: Content to write. + """ + ... + + +class BaseSandboxClient(ABC): + """Base sandbox client interface.""" + + @abstractmethod + async def create( + self, + config: Optional[SandboxSettings] = None, + volume_bindings: Optional[Dict[str, str]] = None, + ) -> None: + """Creates sandbox.""" + + @abstractmethod + async def run_command(self, command: str, timeout: Optional[int] = None) -> str: + """Executes command.""" + + @abstractmethod + async def copy_from(self, container_path: str, local_path: str) -> None: + """Copies file from container.""" + + @abstractmethod + async def copy_to(self, local_path: str, container_path: str) -> None: + """Copies file to container.""" + + @abstractmethod + async def read_file(self, path: str) -> str: + """Reads file.""" + + @abstractmethod + async def write_file(self, path: str, content: str) -> None: + """Writes file.""" + + @abstractmethod + async def cleanup(self) -> None: + """Cleans up resources.""" + + +class LocalSandboxClient(BaseSandboxClient): + """Local sandbox client implementation.""" + + def __init__(self): + """Initializes local sandbox client.""" + self.sandbox: Optional[DockerSandbox] = None + + async def create( + self, + config: Optional[SandboxSettings] = None, + volume_bindings: Optional[Dict[str, str]] = None, + ) -> None: + """Creates a sandbox. + + Args: + config: Sandbox configuration. + volume_bindings: Volume mappings. + + Raises: + RuntimeError: If sandbox creation fails. + """ + self.sandbox = DockerSandbox(config, volume_bindings) + await self.sandbox.create() + + async def run_command(self, command: str, timeout: Optional[int] = None) -> str: + """Runs command in sandbox. + + Args: + command: Command to execute. + timeout: Execution timeout in seconds. + + Returns: + Command output. + + Raises: + RuntimeError: If sandbox not initialized. + """ + if not self.sandbox: + raise RuntimeError("Sandbox not initialized") + return await self.sandbox.run_command(command, timeout) + + async def copy_from(self, container_path: str, local_path: str) -> None: + """Copies file from container to local. + + Args: + container_path: File path in container. + local_path: Local destination path. + + Raises: + RuntimeError: If sandbox not initialized. + """ + if not self.sandbox: + raise RuntimeError("Sandbox not initialized") + await self.sandbox.copy_from(container_path, local_path) + + async def copy_to(self, local_path: str, container_path: str) -> None: + """Copies file from local to container. + + Args: + local_path: Local source file path. + container_path: Destination path in container. + + Raises: + RuntimeError: If sandbox not initialized. + """ + if not self.sandbox: + raise RuntimeError("Sandbox not initialized") + await self.sandbox.copy_to(local_path, container_path) + + async def read_file(self, path: str) -> str: + """Reads file from container. + + Args: + path: File path in container. + + Returns: + File content. + + Raises: + RuntimeError: If sandbox not initialized. + """ + if not self.sandbox: + raise RuntimeError("Sandbox not initialized") + return await self.sandbox.read_file(path) + + async def write_file(self, path: str, content: str) -> None: + """Writes file to container. + + Args: + path: File path in container. + content: File content. + + Raises: + RuntimeError: If sandbox not initialized. + """ + if not self.sandbox: + raise RuntimeError("Sandbox not initialized") + await self.sandbox.write_file(path, content) + + async def cleanup(self) -> None: + """Cleans up resources.""" + if self.sandbox: + await self.sandbox.cleanup() + self.sandbox = None + + +def create_sandbox_client() -> LocalSandboxClient: + """Creates a sandbox client. + + Returns: + LocalSandboxClient: Sandbox client instance. + """ + return LocalSandboxClient() + + +SANDBOX_CLIENT = create_sandbox_client() diff --git a/app/sandbox/core/exceptions.py b/app/sandbox/core/exceptions.py new file mode 100644 index 0000000000000000000000000000000000000000..5c1f0e8a9a50b5d4102cd3cc9ed453f8994159a6 --- /dev/null +++ b/app/sandbox/core/exceptions.py @@ -0,0 +1,17 @@ +"""Exception classes for the sandbox system. + +This module defines custom exceptions used throughout the sandbox system to +handle various error conditions in a structured way. +""" + + +class SandboxError(Exception): + """Base exception for sandbox-related errors.""" + + +class SandboxTimeoutError(SandboxError): + """Exception raised when a sandbox operation times out.""" + + +class SandboxResourceError(SandboxError): + """Exception raised for resource-related errors.""" diff --git a/app/sandbox/core/manager.py b/app/sandbox/core/manager.py new file mode 100644 index 0000000000000000000000000000000000000000..5814f1206e3b960f5f5b8a8b9cd086bfeca72194 --- /dev/null +++ b/app/sandbox/core/manager.py @@ -0,0 +1,313 @@ +import asyncio +import uuid +from contextlib import asynccontextmanager +from typing import Dict, Optional, Set + +import docker +from docker.errors import APIError, ImageNotFound + +from app.config import SandboxSettings +from app.logger import logger +from app.sandbox.core.sandbox import DockerSandbox + + +class SandboxManager: + """Docker sandbox manager. + + Manages multiple DockerSandbox instances lifecycle including creation, + monitoring, and cleanup. Provides concurrent access control and automatic + cleanup mechanisms for sandbox resources. + + Attributes: + max_sandboxes: Maximum allowed number of sandboxes. + idle_timeout: Sandbox idle timeout in seconds. + cleanup_interval: Cleanup check interval in seconds. + _sandboxes: Active sandbox instance mapping. + _last_used: Last used time record for sandboxes. + """ + + def __init__( + self, + max_sandboxes: int = 100, + idle_timeout: int = 3600, + cleanup_interval: int = 300, + ): + """Initializes sandbox manager. + + Args: + max_sandboxes: Maximum sandbox count limit. + idle_timeout: Idle timeout in seconds. + cleanup_interval: Cleanup check interval in seconds. + """ + self.max_sandboxes = max_sandboxes + self.idle_timeout = idle_timeout + self.cleanup_interval = cleanup_interval + + # Docker client + self._client = docker.from_env() + + # Resource mappings + self._sandboxes: Dict[str, DockerSandbox] = {} + self._last_used: Dict[str, float] = {} + + # Concurrency control + self._locks: Dict[str, asyncio.Lock] = {} + self._global_lock = asyncio.Lock() + self._active_operations: Set[str] = set() + + # Cleanup task + self._cleanup_task: Optional[asyncio.Task] = None + self._is_shutting_down = False + + # Start automatic cleanup + self.start_cleanup_task() + + async def ensure_image(self, image: str) -> bool: + """Ensures Docker image is available. + + Args: + image: Image name. + + Returns: + bool: Whether image is available. + """ + try: + self._client.images.get(image) + return True + except ImageNotFound: + try: + logger.info(f"Pulling image {image}...") + await asyncio.get_event_loop().run_in_executor( + None, self._client.images.pull, image + ) + return True + except (APIError, Exception) as e: + logger.error(f"Failed to pull image {image}: {e}") + return False + + @asynccontextmanager + async def sandbox_operation(self, sandbox_id: str): + """Context manager for sandbox operations. + + Provides concurrency control and usage time updates. + + Args: + sandbox_id: Sandbox ID. + + Raises: + KeyError: If sandbox not found. + """ + if sandbox_id not in self._locks: + self._locks[sandbox_id] = asyncio.Lock() + + async with self._locks[sandbox_id]: + if sandbox_id not in self._sandboxes: + raise KeyError(f"Sandbox {sandbox_id} not found") + + self._active_operations.add(sandbox_id) + try: + self._last_used[sandbox_id] = asyncio.get_event_loop().time() + yield self._sandboxes[sandbox_id] + finally: + self._active_operations.remove(sandbox_id) + + async def create_sandbox( + self, + config: Optional[SandboxSettings] = None, + volume_bindings: Optional[Dict[str, str]] = None, + ) -> str: + """Creates a new sandbox instance. + + Args: + config: Sandbox configuration. + volume_bindings: Volume mapping configuration. + + Returns: + str: Sandbox ID. + + Raises: + RuntimeError: If max sandbox count reached or creation fails. + """ + async with self._global_lock: + if len(self._sandboxes) >= self.max_sandboxes: + raise RuntimeError( + f"Maximum number of sandboxes ({self.max_sandboxes}) reached" + ) + + config = config or SandboxSettings() + if not await self.ensure_image(config.image): + raise RuntimeError(f"Failed to ensure Docker image: {config.image}") + + sandbox_id = str(uuid.uuid4()) + try: + sandbox = DockerSandbox(config, volume_bindings) + await sandbox.create() + + self._sandboxes[sandbox_id] = sandbox + self._last_used[sandbox_id] = asyncio.get_event_loop().time() + self._locks[sandbox_id] = asyncio.Lock() + + logger.info(f"Created sandbox {sandbox_id}") + return sandbox_id + + except Exception as e: + logger.error(f"Failed to create sandbox: {e}") + if sandbox_id in self._sandboxes: + await self.delete_sandbox(sandbox_id) + raise RuntimeError(f"Failed to create sandbox: {e}") + + async def get_sandbox(self, sandbox_id: str) -> DockerSandbox: + """Gets a sandbox instance. + + Args: + sandbox_id: Sandbox ID. + + Returns: + DockerSandbox: Sandbox instance. + + Raises: + KeyError: If sandbox does not exist. + """ + async with self.sandbox_operation(sandbox_id) as sandbox: + return sandbox + + def start_cleanup_task(self) -> None: + """Starts automatic cleanup task.""" + + async def cleanup_loop(): + while not self._is_shutting_down: + try: + await self._cleanup_idle_sandboxes() + except Exception as e: + logger.error(f"Error in cleanup loop: {e}") + await asyncio.sleep(self.cleanup_interval) + + self._cleanup_task = asyncio.create_task(cleanup_loop()) + + async def _cleanup_idle_sandboxes(self) -> None: + """Cleans up idle sandboxes.""" + current_time = asyncio.get_event_loop().time() + to_cleanup = [] + + async with self._global_lock: + for sandbox_id, last_used in self._last_used.items(): + if ( + sandbox_id not in self._active_operations + and current_time - last_used > self.idle_timeout + ): + to_cleanup.append(sandbox_id) + + for sandbox_id in to_cleanup: + try: + await self.delete_sandbox(sandbox_id) + except Exception as e: + logger.error(f"Error cleaning up sandbox {sandbox_id}: {e}") + + async def cleanup(self) -> None: + """Cleans up all resources.""" + logger.info("Starting manager cleanup...") + self._is_shutting_down = True + + # Cancel cleanup task + if self._cleanup_task: + self._cleanup_task.cancel() + try: + await asyncio.wait_for(self._cleanup_task, timeout=1.0) + except (asyncio.CancelledError, asyncio.TimeoutError): + pass + + # Get all sandbox IDs to clean up + async with self._global_lock: + sandbox_ids = list(self._sandboxes.keys()) + + # Concurrently clean up all sandboxes + cleanup_tasks = [] + for sandbox_id in sandbox_ids: + task = asyncio.create_task(self._safe_delete_sandbox(sandbox_id)) + cleanup_tasks.append(task) + + if cleanup_tasks: + # Wait for all cleanup tasks to complete, with timeout to avoid infinite waiting + try: + await asyncio.wait(cleanup_tasks, timeout=30.0) + except asyncio.TimeoutError: + logger.error("Sandbox cleanup timed out") + + # Clean up remaining references + self._sandboxes.clear() + self._last_used.clear() + self._locks.clear() + self._active_operations.clear() + + logger.info("Manager cleanup completed") + + async def _safe_delete_sandbox(self, sandbox_id: str) -> None: + """Safely deletes a single sandbox. + + Args: + sandbox_id: Sandbox ID to delete. + """ + try: + if sandbox_id in self._active_operations: + logger.warning( + f"Sandbox {sandbox_id} has active operations, waiting for completion" + ) + for _ in range(10): # Wait at most 10 times + await asyncio.sleep(0.5) + if sandbox_id not in self._active_operations: + break + else: + logger.warning( + f"Timeout waiting for sandbox {sandbox_id} operations to complete" + ) + + # Get reference to sandbox object + sandbox = self._sandboxes.get(sandbox_id) + if sandbox: + await sandbox.cleanup() + + # Remove sandbox record from manager + async with self._global_lock: + self._sandboxes.pop(sandbox_id, None) + self._last_used.pop(sandbox_id, None) + self._locks.pop(sandbox_id, None) + logger.info(f"Deleted sandbox {sandbox_id}") + except Exception as e: + logger.error(f"Error during cleanup of sandbox {sandbox_id}: {e}") + + async def delete_sandbox(self, sandbox_id: str) -> None: + """Deletes specified sandbox. + + Args: + sandbox_id: Sandbox ID. + """ + if sandbox_id not in self._sandboxes: + return + + try: + await self._safe_delete_sandbox(sandbox_id) + except Exception as e: + logger.error(f"Failed to delete sandbox {sandbox_id}: {e}") + + async def __aenter__(self) -> "SandboxManager": + """Async context manager entry.""" + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: + """Async context manager exit.""" + await self.cleanup() + + def get_stats(self) -> Dict: + """Gets manager statistics. + + Returns: + Dict: Statistics information. + """ + return { + "total_sandboxes": len(self._sandboxes), + "active_operations": len(self._active_operations), + "max_sandboxes": self.max_sandboxes, + "idle_timeout": self.idle_timeout, + "cleanup_interval": self.cleanup_interval, + "is_shutting_down": self._is_shutting_down, + } diff --git a/app/sandbox/core/sandbox.py b/app/sandbox/core/sandbox.py new file mode 100644 index 0000000000000000000000000000000000000000..c57b3f239e6ba069f85b8585903a2cb190483d52 --- /dev/null +++ b/app/sandbox/core/sandbox.py @@ -0,0 +1,462 @@ +import asyncio +import io +import os +import tarfile +import tempfile +import uuid +from typing import Dict, Optional + +import docker +from docker.errors import NotFound +from docker.models.containers import Container + +from app.config import SandboxSettings +from app.sandbox.core.exceptions import SandboxTimeoutError +from app.sandbox.core.terminal import AsyncDockerizedTerminal + + +class DockerSandbox: + """Docker sandbox environment. + + Provides a containerized execution environment with resource limits, + file operations, and command execution capabilities. + + Attributes: + config: Sandbox configuration. + volume_bindings: Volume mapping configuration. + client: Docker client. + container: Docker container instance. + terminal: Container terminal interface. + """ + + def __init__( + self, + config: Optional[SandboxSettings] = None, + volume_bindings: Optional[Dict[str, str]] = None, + ): + """Initializes a sandbox instance. + + Args: + config: Sandbox configuration. Default configuration used if None. + volume_bindings: Volume mappings in {host_path: container_path} format. + """ + self.config = config or SandboxSettings() + self.volume_bindings = volume_bindings or {} + self.client = docker.from_env() + self.container: Optional[Container] = None + self.terminal: Optional[AsyncDockerizedTerminal] = None + + async def create(self) -> "DockerSandbox": + """Creates and starts the sandbox container. + + Returns: + Current sandbox instance. + + Raises: + docker.errors.APIError: If Docker API call fails. + RuntimeError: If container creation or startup fails. + """ + try: + # Prepare container config + host_config = self.client.api.create_host_config( + mem_limit=self.config.memory_limit, + cpu_period=100000, + cpu_quota=int(100000 * self.config.cpu_limit), + network_mode="none" if not self.config.network_enabled else "bridge", + binds=self._prepare_volume_bindings(), + ) + + # Generate unique container name with sandbox_ prefix + container_name = f"sandbox_{uuid.uuid4().hex[:8]}" + + # Create container + container = await asyncio.to_thread( + self.client.api.create_container, + image=self.config.image, + command="tail -f /dev/null", + hostname="sandbox", + working_dir=self.config.work_dir, + host_config=host_config, + name=container_name, + tty=True, + detach=True, + ) + + self.container = self.client.containers.get(container["Id"]) + + # Start container + await asyncio.to_thread(self.container.start) + + # Initialize terminal + self.terminal = AsyncDockerizedTerminal( + container["Id"], + self.config.work_dir, + env_vars={"PYTHONUNBUFFERED": "1"} + # Ensure Python output is not buffered + ) + await self.terminal.init() + + return self + + except Exception as e: + await self.cleanup() # Ensure resources are cleaned up + raise RuntimeError(f"Failed to create sandbox: {e}") from e + + def _prepare_volume_bindings(self) -> Dict[str, Dict[str, str]]: + """Prepares volume binding configuration. + + Returns: + Volume binding configuration dictionary. + """ + bindings = {} + + # Create and add working directory mapping + work_dir = self._ensure_host_dir(self.config.work_dir) + bindings[work_dir] = {"bind": self.config.work_dir, "mode": "rw"} + + # Add custom volume bindings + for host_path, container_path in self.volume_bindings.items(): + bindings[host_path] = {"bind": container_path, "mode": "rw"} + + return bindings + + @staticmethod + def _ensure_host_dir(path: str) -> str: + """Ensures directory exists on the host. + + Args: + path: Directory path. + + Returns: + Actual path on the host. + """ + host_path = os.path.join( + tempfile.gettempdir(), + f"sandbox_{os.path.basename(path)}_{os.urandom(4).hex()}", + ) + os.makedirs(host_path, exist_ok=True) + return host_path + + async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str: + """Runs a command in the sandbox. + + Args: + cmd: Command to execute. + timeout: Timeout in seconds. + + Returns: + Command output as string. + + Raises: + RuntimeError: If sandbox not initialized or command execution fails. + TimeoutError: If command execution times out. + """ + if not self.terminal: + raise RuntimeError("Sandbox not initialized") + + try: + return await self.terminal.run_command( + cmd, timeout=timeout or self.config.timeout + ) + except TimeoutError: + raise SandboxTimeoutError( + f"Command execution timed out after {timeout or self.config.timeout} seconds" + ) + + async def read_file(self, path: str) -> str: + """Reads a file from the container. + + Args: + path: File path. + + Returns: + File contents as string. + + Raises: + FileNotFoundError: If file does not exist. + RuntimeError: If read operation fails. + """ + if not self.container: + raise RuntimeError("Sandbox not initialized") + + try: + # Get file archive + resolved_path = self._safe_resolve_path(path) + tar_stream, _ = await asyncio.to_thread( + self.container.get_archive, resolved_path + ) + + # Read file content from tar stream + content = await self._read_from_tar(tar_stream) + return content.decode("utf-8") + + except NotFound: + raise FileNotFoundError(f"File not found: {path}") + except Exception as e: + raise RuntimeError(f"Failed to read file: {e}") + + async def write_file(self, path: str, content: str) -> None: + """Writes content to a file in the container. + + Args: + path: Target path. + content: File content. + + Raises: + RuntimeError: If write operation fails. + """ + if not self.container: + raise RuntimeError("Sandbox not initialized") + + try: + resolved_path = self._safe_resolve_path(path) + parent_dir = os.path.dirname(resolved_path) + + # Create parent directory + if parent_dir: + await self.run_command(f"mkdir -p {parent_dir}") + + # Prepare file data + tar_stream = await self._create_tar_stream( + os.path.basename(path), content.encode("utf-8") + ) + + # Write file + await asyncio.to_thread( + self.container.put_archive, parent_dir or "/", tar_stream + ) + + except Exception as e: + raise RuntimeError(f"Failed to write file: {e}") + + def _safe_resolve_path(self, path: str) -> str: + """Safely resolves container path, preventing path traversal. + + Args: + path: Original path. + + Returns: + Resolved absolute path. + + Raises: + ValueError: If path contains potentially unsafe patterns. + """ + # Check for path traversal attempts + if ".." in path.split("/"): + raise ValueError("Path contains potentially unsafe patterns") + + resolved = ( + os.path.join(self.config.work_dir, path) + if not os.path.isabs(path) + else path + ) + return resolved + + async def copy_from(self, src_path: str, dst_path: str) -> None: + """Copies a file from the container. + + Args: + src_path: Source file path (container). + dst_path: Destination path (host). + + Raises: + FileNotFoundError: If source file does not exist. + RuntimeError: If copy operation fails. + """ + try: + # Ensure destination file's parent directory exists + parent_dir = os.path.dirname(dst_path) + if parent_dir: + os.makedirs(parent_dir, exist_ok=True) + + # Get file stream + resolved_src = self._safe_resolve_path(src_path) + stream, stat = await asyncio.to_thread( + self.container.get_archive, resolved_src + ) + + # Create temporary directory to extract file + with tempfile.TemporaryDirectory() as tmp_dir: + # Write stream to temporary file + tar_path = os.path.join(tmp_dir, "temp.tar") + with open(tar_path, "wb") as f: + for chunk in stream: + f.write(chunk) + + # Extract file + with tarfile.open(tar_path) as tar: + members = tar.getmembers() + if not members: + raise FileNotFoundError(f"Source file is empty: {src_path}") + + # If destination is a directory, we should preserve relative path structure + if os.path.isdir(dst_path): + tar.extractall(dst_path) + else: + # If destination is a file, we only extract the source file's content + if len(members) > 1: + raise RuntimeError( + f"Source path is a directory but destination is a file: {src_path}" + ) + + with open(dst_path, "wb") as dst: + src_file = tar.extractfile(members[0]) + if src_file is None: + raise RuntimeError( + f"Failed to extract file: {src_path}" + ) + dst.write(src_file.read()) + + except docker.errors.NotFound: + raise FileNotFoundError(f"Source file not found: {src_path}") + except Exception as e: + raise RuntimeError(f"Failed to copy file: {e}") + + async def copy_to(self, src_path: str, dst_path: str) -> None: + """Copies a file to the container. + + Args: + src_path: Source file path (host). + dst_path: Destination path (container). + + Raises: + FileNotFoundError: If source file does not exist. + RuntimeError: If copy operation fails. + """ + try: + if not os.path.exists(src_path): + raise FileNotFoundError(f"Source file not found: {src_path}") + + # Create destination directory in container + resolved_dst = self._safe_resolve_path(dst_path) + container_dir = os.path.dirname(resolved_dst) + if container_dir: + await self.run_command(f"mkdir -p {container_dir}") + + # Create tar file to upload + with tempfile.TemporaryDirectory() as tmp_dir: + tar_path = os.path.join(tmp_dir, "temp.tar") + with tarfile.open(tar_path, "w") as tar: + # Handle directory source path + if os.path.isdir(src_path): + os.path.basename(src_path.rstrip("/")) + for root, _, files in os.walk(src_path): + for file in files: + file_path = os.path.join(root, file) + arcname = os.path.join( + os.path.basename(dst_path), + os.path.relpath(file_path, src_path), + ) + tar.add(file_path, arcname=arcname) + else: + # Add single file to tar + tar.add(src_path, arcname=os.path.basename(dst_path)) + + # Read tar file content + with open(tar_path, "rb") as f: + data = f.read() + + # Upload to container + await asyncio.to_thread( + self.container.put_archive, + os.path.dirname(resolved_dst) or "/", + data, + ) + + # Verify file was created successfully + try: + await self.run_command(f"test -e {resolved_dst}") + except Exception: + raise RuntimeError(f"Failed to verify file creation: {dst_path}") + + except FileNotFoundError: + raise + except Exception as e: + raise RuntimeError(f"Failed to copy file: {e}") + + @staticmethod + async def _create_tar_stream(name: str, content: bytes) -> io.BytesIO: + """Creates a tar file stream. + + Args: + name: Filename. + content: File content. + + Returns: + Tar file stream. + """ + tar_stream = io.BytesIO() + with tarfile.open(fileobj=tar_stream, mode="w") as tar: + tarinfo = tarfile.TarInfo(name=name) + tarinfo.size = len(content) + tar.addfile(tarinfo, io.BytesIO(content)) + tar_stream.seek(0) + return tar_stream + + @staticmethod + async def _read_from_tar(tar_stream) -> bytes: + """Reads file content from a tar stream. + + Args: + tar_stream: Tar file stream. + + Returns: + File content. + + Raises: + RuntimeError: If read operation fails. + """ + with tempfile.NamedTemporaryFile() as tmp: + for chunk in tar_stream: + tmp.write(chunk) + tmp.seek(0) + + with tarfile.open(fileobj=tmp) as tar: + member = tar.next() + if not member: + raise RuntimeError("Empty tar archive") + + file_content = tar.extractfile(member) + if not file_content: + raise RuntimeError("Failed to extract file content") + + return file_content.read() + + async def cleanup(self) -> None: + """Cleans up sandbox resources.""" + errors = [] + try: + if self.terminal: + try: + await self.terminal.close() + except Exception as e: + errors.append(f"Terminal cleanup error: {e}") + finally: + self.terminal = None + + if self.container: + try: + await asyncio.to_thread(self.container.stop, timeout=5) + except Exception as e: + errors.append(f"Container stop error: {e}") + + try: + await asyncio.to_thread(self.container.remove, force=True) + except Exception as e: + errors.append(f"Container remove error: {e}") + finally: + self.container = None + + except Exception as e: + errors.append(f"General cleanup error: {e}") + + if errors: + print(f"Warning: Errors during cleanup: {', '.join(errors)}") + + async def __aenter__(self) -> "DockerSandbox": + """Async context manager entry.""" + return await self.create() + + async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: + """Async context manager exit.""" + await self.cleanup() diff --git a/app/sandbox/core/terminal.py b/app/sandbox/core/terminal.py new file mode 100644 index 0000000000000000000000000000000000000000..aee51844b55d3b2d0f6ed847f1658dd42b739cf8 --- /dev/null +++ b/app/sandbox/core/terminal.py @@ -0,0 +1,346 @@ +""" +Asynchronous Docker Terminal + +This module provides asynchronous terminal functionality for Docker containers, +allowing interactive command execution with timeout control. +""" + +import asyncio +import re +import socket +from typing import Dict, Optional, Tuple, Union + +import docker +from docker import APIClient +from docker.errors import APIError +from docker.models.containers import Container + + +class DockerSession: + def __init__(self, container_id: str) -> None: + """Initializes a Docker session. + + Args: + container_id: ID of the Docker container. + """ + self.api = APIClient() + self.container_id = container_id + self.exec_id = None + self.socket = None + + async def create(self, working_dir: str, env_vars: Dict[str, str]) -> None: + """Creates an interactive session with the container. + + Args: + working_dir: Working directory inside the container. + env_vars: Environment variables to set. + + Raises: + RuntimeError: If socket connection fails. + """ + startup_command = [ + "bash", + "-c", + f"cd {working_dir} && " + "PROMPT_COMMAND='' " + "PS1='$ ' " + "exec bash --norc --noprofile", + ] + + exec_data = self.api.exec_create( + self.container_id, + startup_command, + stdin=True, + tty=True, + stdout=True, + stderr=True, + privileged=True, + user="root", + environment={**env_vars, "TERM": "dumb", "PS1": "$ ", "PROMPT_COMMAND": ""}, + ) + self.exec_id = exec_data["Id"] + + socket_data = self.api.exec_start( + self.exec_id, socket=True, tty=True, stream=True, demux=True + ) + + if hasattr(socket_data, "_sock"): + self.socket = socket_data._sock + self.socket.setblocking(False) + else: + raise RuntimeError("Failed to get socket connection") + + await self._read_until_prompt() + + async def close(self) -> None: + """Cleans up session resources. + + 1. Sends exit command + 2. Closes socket connection + 3. Checks and cleans up exec instance + """ + try: + if self.socket: + # Send exit command to close bash session + try: + self.socket.sendall(b"exit\n") + # Allow time for command execution + await asyncio.sleep(0.1) + except: + pass # Ignore sending errors, continue cleanup + + # Close socket connection + try: + self.socket.shutdown(socket.SHUT_RDWR) + except: + pass # Some platforms may not support shutdown + + self.socket.close() + self.socket = None + + if self.exec_id: + try: + # Check exec instance status + exec_inspect = self.api.exec_inspect(self.exec_id) + if exec_inspect.get("Running", False): + # If still running, wait for it to complete + await asyncio.sleep(0.5) + except: + pass # Ignore inspection errors, continue cleanup + + self.exec_id = None + + except Exception as e: + # Log error but don't raise, ensure cleanup continues + print(f"Warning: Error during session cleanup: {e}") + + async def _read_until_prompt(self) -> str: + """Reads output until prompt is found. + + Returns: + String containing output up to the prompt. + + Raises: + socket.error: If socket communication fails. + """ + buffer = b"" + while b"$ " not in buffer: + try: + chunk = self.socket.recv(4096) + if chunk: + buffer += chunk + except socket.error as e: + if e.errno == socket.EWOULDBLOCK: + await asyncio.sleep(0.1) + continue + raise + return buffer.decode("utf-8") + + async def execute(self, command: str, timeout: Optional[int] = None) -> str: + """Executes a command and returns cleaned output. + + Args: + command: Shell command to execute. + timeout: Maximum execution time in seconds. + + Returns: + Command output as string with prompt markers removed. + + Raises: + RuntimeError: If session not initialized or execution fails. + TimeoutError: If command execution exceeds timeout. + """ + if not self.socket: + raise RuntimeError("Session not initialized") + + try: + # Sanitize command to prevent shell injection + sanitized_command = self._sanitize_command(command) + full_command = f"{sanitized_command}\necho $?\n" + self.socket.sendall(full_command.encode()) + + async def read_output() -> str: + buffer = b"" + result_lines = [] + command_sent = False + + while True: + try: + chunk = self.socket.recv(4096) + if not chunk: + break + + buffer += chunk + lines = buffer.split(b"\n") + + buffer = lines[-1] + lines = lines[:-1] + + for line in lines: + line = line.rstrip(b"\r") + + if not command_sent: + command_sent = True + continue + + if line.strip() == b"echo $?" or line.strip().isdigit(): + continue + + if line.strip(): + result_lines.append(line) + + if buffer.endswith(b"$ "): + break + + except socket.error as e: + if e.errno == socket.EWOULDBLOCK: + await asyncio.sleep(0.1) + continue + raise + + output = b"\n".join(result_lines).decode("utf-8") + output = re.sub(r"\n\$ echo \$\$?.*$", "", output) + + return output + + if timeout: + result = await asyncio.wait_for(read_output(), timeout) + else: + result = await read_output() + + return result.strip() + + except asyncio.TimeoutError: + raise TimeoutError(f"Command execution timed out after {timeout} seconds") + except Exception as e: + raise RuntimeError(f"Failed to execute command: {e}") + + def _sanitize_command(self, command: str) -> str: + """Sanitizes the command string to prevent shell injection. + + Args: + command: Raw command string. + + Returns: + Sanitized command string. + + Raises: + ValueError: If command contains potentially dangerous patterns. + """ + + # Additional checks for specific risky commands + risky_commands = [ + "rm -rf /", + "rm -rf /*", + "mkfs", + "dd if=/dev/zero", + ":(){:|:&};:", + "chmod -R 777 /", + "chown -R", + ] + + for risky in risky_commands: + if risky in command.lower(): + raise ValueError( + f"Command contains potentially dangerous operation: {risky}" + ) + + return command + + +class AsyncDockerizedTerminal: + def __init__( + self, + container: Union[str, Container], + working_dir: str = "/workspace", + env_vars: Optional[Dict[str, str]] = None, + default_timeout: int = 60, + ) -> None: + """Initializes an asynchronous terminal for Docker containers. + + Args: + container: Docker container ID or Container object. + working_dir: Working directory inside the container. + env_vars: Environment variables to set. + default_timeout: Default command execution timeout in seconds. + """ + self.client = docker.from_env() + self.container = ( + container + if isinstance(container, Container) + else self.client.containers.get(container) + ) + self.working_dir = working_dir + self.env_vars = env_vars or {} + self.default_timeout = default_timeout + self.session = None + + async def init(self) -> None: + """Initializes the terminal environment. + + Ensures working directory exists and creates an interactive session. + + Raises: + RuntimeError: If initialization fails. + """ + await self._ensure_workdir() + + self.session = DockerSession(self.container.id) + await self.session.create(self.working_dir, self.env_vars) + + async def _ensure_workdir(self) -> None: + """Ensures working directory exists in container. + + Raises: + RuntimeError: If directory creation fails. + """ + try: + await self._exec_simple(f"mkdir -p {self.working_dir}") + except APIError as e: + raise RuntimeError(f"Failed to create working directory: {e}") + + async def _exec_simple(self, cmd: str) -> Tuple[int, str]: + """Executes a simple command using Docker's exec_run. + + Args: + cmd: Command to execute. + + Returns: + Tuple of (exit_code, output). + """ + result = await asyncio.to_thread( + self.container.exec_run, cmd, environment=self.env_vars + ) + return result.exit_code, result.output.decode("utf-8") + + async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str: + """Runs a command in the container with timeout. + + Args: + cmd: Shell command to execute. + timeout: Maximum execution time in seconds. + + Returns: + Command output as string. + + Raises: + RuntimeError: If terminal not initialized. + """ + if not self.session: + raise RuntimeError("Terminal not initialized") + + return await self.session.execute(cmd, timeout=timeout or self.default_timeout) + + async def close(self) -> None: + """Closes the terminal session.""" + if self.session: + await self.session.close() + + async def __aenter__(self) -> "AsyncDockerizedTerminal": + """Async context manager entry.""" + await self.init() + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: + """Async context manager exit.""" + await self.close() diff --git a/app/schema.py b/app/schema.py new file mode 100644 index 0000000000000000000000000000000000000000..5f743f92f17b255acf5b3670001112bb99393f9a --- /dev/null +++ b/app/schema.py @@ -0,0 +1,187 @@ +from enum import Enum +from typing import Any, List, Literal, Optional, Union + +from pydantic import BaseModel, Field + + +class Role(str, Enum): + """Message role options""" + + SYSTEM = "system" + USER = "user" + ASSISTANT = "assistant" + TOOL = "tool" + + +ROLE_VALUES = tuple(role.value for role in Role) +ROLE_TYPE = Literal[ROLE_VALUES] # type: ignore + + +class ToolChoice(str, Enum): + """Tool choice options""" + + NONE = "none" + AUTO = "auto" + REQUIRED = "required" + + +TOOL_CHOICE_VALUES = tuple(choice.value for choice in ToolChoice) +TOOL_CHOICE_TYPE = Literal[TOOL_CHOICE_VALUES] # type: ignore + + +class AgentState(str, Enum): + """Agent execution states""" + + IDLE = "IDLE" + RUNNING = "RUNNING" + FINISHED = "FINISHED" + ERROR = "ERROR" + + +class Function(BaseModel): + name: str + arguments: str + + +class ToolCall(BaseModel): + """Represents a tool/function call in a message""" + + id: str + type: str = "function" + function: Function + + +class Message(BaseModel): + """Represents a chat message in the conversation""" + + role: ROLE_TYPE = Field(...) # type: ignore + content: Optional[str] = Field(default=None) + tool_calls: Optional[List[ToolCall]] = Field(default=None) + name: Optional[str] = Field(default=None) + tool_call_id: Optional[str] = Field(default=None) + base64_image: Optional[str] = Field(default=None) + + def __add__(self, other) -> List["Message"]: + """支持 Message + list 或 Message + Message 的操作""" + if isinstance(other, list): + return [self] + other + elif isinstance(other, Message): + return [self, other] + else: + raise TypeError( + f"unsupported operand type(s) for +: '{type(self).__name__}' and '{type(other).__name__}'" + ) + + def __radd__(self, other) -> List["Message"]: + """支持 list + Message 的操作""" + if isinstance(other, list): + return other + [self] + else: + raise TypeError( + f"unsupported operand type(s) for +: '{type(other).__name__}' and '{type(self).__name__}'" + ) + + def to_dict(self) -> dict: + """Convert message to dictionary format""" + message = {"role": self.role} + if self.content is not None: + message["content"] = self.content + if self.tool_calls is not None: + message["tool_calls"] = [tool_call.dict() for tool_call in self.tool_calls] + if self.name is not None: + message["name"] = self.name + if self.tool_call_id is not None: + message["tool_call_id"] = self.tool_call_id + if self.base64_image is not None: + message["base64_image"] = self.base64_image + return message + + @classmethod + def user_message( + cls, content: str, base64_image: Optional[str] = None + ) -> "Message": + """Create a user message""" + return cls(role=Role.USER, content=content, base64_image=base64_image) + + @classmethod + def system_message(cls, content: str) -> "Message": + """Create a system message""" + return cls(role=Role.SYSTEM, content=content) + + @classmethod + def assistant_message( + cls, content: Optional[str] = None, base64_image: Optional[str] = None + ) -> "Message": + """Create an assistant message""" + return cls(role=Role.ASSISTANT, content=content, base64_image=base64_image) + + @classmethod + def tool_message( + cls, content: str, name, tool_call_id: str, base64_image: Optional[str] = None + ) -> "Message": + """Create a tool message""" + return cls( + role=Role.TOOL, + content=content, + name=name, + tool_call_id=tool_call_id, + base64_image=base64_image, + ) + + @classmethod + def from_tool_calls( + cls, + tool_calls: List[Any], + content: Union[str, List[str]] = "", + base64_image: Optional[str] = None, + **kwargs, + ): + """Create ToolCallsMessage from raw tool calls. + + Args: + tool_calls: Raw tool calls from LLM + content: Optional message content + base64_image: Optional base64 encoded image + """ + formatted_calls = [ + {"id": call.id, "function": call.function.model_dump(), "type": "function"} + for call in tool_calls + ] + return cls( + role=Role.ASSISTANT, + content=content, + tool_calls=formatted_calls, + base64_image=base64_image, + **kwargs, + ) + + +class Memory(BaseModel): + messages: List[Message] = Field(default_factory=list) + max_messages: int = Field(default=100) + + def add_message(self, message: Message) -> None: + """Add a message to memory""" + self.messages.append(message) + # Optional: Implement message limit + if len(self.messages) > self.max_messages: + self.messages = self.messages[-self.max_messages :] + + def add_messages(self, messages: List[Message]) -> None: + """Add multiple messages to memory""" + self.messages.extend(messages) + # Optional: Implement message limit + if len(self.messages) > self.max_messages: + self.messages = self.messages[-self.max_messages :] + + def clear(self) -> None: + """Clear all messages""" + self.messages.clear() + + def get_recent_messages(self, n: int) -> List[Message]: + """Get n most recent messages""" + return self.messages[-n:] + + def to_dict_list(self) -> List[dict]: + """Convert messages to list of dicts""" + return [msg.to_dict() for msg in self.messages] diff --git a/app/tool/__init__.py b/app/tool/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..636e9b8dac4773609ce51c6008f4c9f2869b8017 --- /dev/null +++ b/app/tool/__init__.py @@ -0,0 +1,24 @@ +from app.tool.base import BaseTool +from app.tool.bash import Bash +from app.tool.browser_use_tool import BrowserUseTool +from app.tool.crawl4ai import Crawl4aiTool +from app.tool.create_chat_completion import CreateChatCompletion +from app.tool.planning import PlanningTool +from app.tool.str_replace_editor import StrReplaceEditor +from app.tool.terminate import Terminate +from app.tool.tool_collection import ToolCollection +from app.tool.web_search import WebSearch + + +__all__ = [ + "BaseTool", + "Bash", + "BrowserUseTool", + "Terminate", + "StrReplaceEditor", + "WebSearch", + "ToolCollection", + "CreateChatCompletion", + "PlanningTool", + "Crawl4aiTool", +] diff --git a/app/tool/ask_human.py b/app/tool/ask_human.py new file mode 100644 index 0000000000000000000000000000000000000000..5fd455070ba495792e185fbbb96f7d4f370f6020 --- /dev/null +++ b/app/tool/ask_human.py @@ -0,0 +1,21 @@ +from app.tool import BaseTool + + +class AskHuman(BaseTool): + """Add a tool to ask human for help.""" + + name: str = "ask_human" + description: str = "Use this tool to ask human for help." + parameters: str = { + "type": "object", + "properties": { + "inquire": { + "type": "string", + "description": "The question you want to ask human.", + } + }, + "required": ["inquire"], + } + + async def execute(self, inquire: str) -> str: + return input(f"""Bot: {inquire}\n\nYou: """).strip() diff --git a/app/tool/base.py b/app/tool/base.py new file mode 100644 index 0000000000000000000000000000000000000000..fdb8b7d3a62a05725c453ef0358b1407c127657f --- /dev/null +++ b/app/tool/base.py @@ -0,0 +1,181 @@ +import json +from abc import ABC, abstractmethod +from typing import Any, Dict, Optional, Union + +from pydantic import BaseModel, Field + +from app.utils.logger import logger + + +# class BaseTool(ABC, BaseModel): +# name: str +# description: str +# parameters: Optional[dict] = None + +# class Config: +# arbitrary_types_allowed = True + +# async def __call__(self, **kwargs) -> Any: +# """Execute the tool with given parameters.""" +# return await self.execute(**kwargs) + +# @abstractmethod +# async def execute(self, **kwargs) -> Any: +# """Execute the tool with given parameters.""" + +# def to_param(self) -> Dict: +# """Convert tool to function call format.""" +# return { +# "type": "function", +# "function": { +# "name": self.name, +# "description": self.description, +# "parameters": self.parameters, +# }, +# } + + +class ToolResult(BaseModel): + """Represents the result of a tool execution.""" + + output: Any = Field(default=None) + error: Optional[str] = Field(default=None) + base64_image: Optional[str] = Field(default=None) + system: Optional[str] = Field(default=None) + + class Config: + arbitrary_types_allowed = True + + def __bool__(self): + return any(getattr(self, field) for field in self.__fields__) + + def __add__(self, other: "ToolResult"): + def combine_fields( + field: Optional[str], other_field: Optional[str], concatenate: bool = True + ): + if field and other_field: + if concatenate: + return field + other_field + raise ValueError("Cannot combine tool results") + return field or other_field + + return ToolResult( + output=combine_fields(self.output, other.output), + error=combine_fields(self.error, other.error), + base64_image=combine_fields(self.base64_image, other.base64_image, False), + system=combine_fields(self.system, other.system), + ) + + def __str__(self): + return f"Error: {self.error}" if self.error else self.output + + def replace(self, **kwargs): + """Returns a new ToolResult with the given fields replaced.""" + # return self.copy(update=kwargs) + return type(self)(**{**self.dict(), **kwargs}) + + +class BaseTool(ABC, BaseModel): + """Consolidated base class for all tools combining BaseModel and Tool functionality. + + Provides: + - Pydantic model validation + - Schema registration + - Standardized result handling + - Abstract execution interface + + Attributes: + name (str): Tool name + description (str): Tool description + parameters (dict): Tool parameters schema + _schemas (Dict[str, List[ToolSchema]]): Registered method schemas + """ + + name: str + description: str + parameters: Optional[dict] = None + # _schemas: Dict[str, List[ToolSchema]] = {} + + class Config: + arbitrary_types_allowed = True + underscore_attrs_are_private = False + + # def __init__(self, **data): + # """Initialize tool with model validation and schema registration.""" + # super().__init__(**data) + # logger.debug(f"Initializing tool class: {self.__class__.__name__}") + # self._register_schemas() + + # def _register_schemas(self): + # """Register schemas from all decorated methods.""" + # for name, method in inspect.getmembers(self, predicate=inspect.ismethod): + # if hasattr(method, 'tool_schemas'): + # self._schemas[name] = method.tool_schemas + # logger.debug(f"Registered schemas for method '{name}' in {self.__class__.__name__}") + + async def __call__(self, **kwargs) -> Any: + """Execute the tool with given parameters.""" + return await self.execute(**kwargs) + + @abstractmethod + async def execute(self, **kwargs) -> Any: + """Execute the tool with given parameters.""" + + def to_param(self) -> Dict: + """Convert tool to function call format. + + Returns: + Dictionary with tool metadata in OpenAI function calling format + """ + return { + "type": "function", + "function": { + "name": self.name, + "description": self.description, + "parameters": self.parameters, + }, + } + + # def get_schemas(self) -> Dict[str, List[ToolSchema]]: + # """Get all registered tool schemas. + + # Returns: + # Dict mapping method names to their schema definitions + # """ + # return self._schemas + + def success_response(self, data: Union[Dict[str, Any], str]) -> ToolResult: + """Create a successful tool result. + + Args: + data: Result data (dictionary or string) + + Returns: + ToolResult with success=True and formatted output + """ + if isinstance(data, str): + text = data + else: + text = json.dumps(data, indent=2) + logger.debug(f"Created success response for {self.__class__.__name__}") + return ToolResult(output=text) + + def fail_response(self, msg: str) -> ToolResult: + """Create a failed tool result. + + Args: + msg: Error message describing the failure + + Returns: + ToolResult with success=False and error message + """ + logger.debug(f"Tool {self.__class__.__name__} returned failed result: {msg}") + return ToolResult(error=msg) + + +class CLIResult(ToolResult): + """A ToolResult that can be rendered as a CLI output.""" + + +class ToolFailure(ToolResult): + """A ToolResult that represents a failure.""" diff --git a/app/tool/bash.py b/app/tool/bash.py new file mode 100644 index 0000000000000000000000000000000000000000..c6b9072fd1e22e9af812855bc63372c988021200 --- /dev/null +++ b/app/tool/bash.py @@ -0,0 +1,158 @@ +import asyncio +import os +from typing import Optional + +from app.exceptions import ToolError +from app.tool.base import BaseTool, CLIResult + + +_BASH_DESCRIPTION = """Execute a bash command in the terminal. +* Long running commands: For commands that may run indefinitely, it should be run in the background and the output should be redirected to a file, e.g. command = `python3 app.py > server.log 2>&1 &`. +* Interactive: If a bash command returns exit code `-1`, this means the process is not yet finished. The assistant must then send a second call to terminal with an empty `command` (which will retrieve any additional logs), or it can send additional text (set `command` to the text) to STDIN of the running process, or it can send command=`ctrl+c` to interrupt the process. +* Timeout: If a command execution result says "Command timed out. Sending SIGINT to the process", the assistant should retry running the command in the background. +""" + + +class _BashSession: + """A session of a bash shell.""" + + _started: bool + _process: asyncio.subprocess.Process + + command: str = "/bin/bash" + _output_delay: float = 0.2 # seconds + _timeout: float = 120.0 # seconds + _sentinel: str = "<>" + + def __init__(self): + self._started = False + self._timed_out = False + + async def start(self): + if self._started: + return + + self._process = await asyncio.create_subprocess_shell( + self.command, + preexec_fn=os.setsid, + shell=True, + bufsize=0, + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + self._started = True + + def stop(self): + """Terminate the bash shell.""" + if not self._started: + raise ToolError("Session has not started.") + if self._process.returncode is not None: + return + self._process.terminate() + + async def run(self, command: str): + """Execute a command in the bash shell.""" + if not self._started: + raise ToolError("Session has not started.") + if self._process.returncode is not None: + return CLIResult( + system="tool must be restarted", + error=f"bash has exited with returncode {self._process.returncode}", + ) + if self._timed_out: + raise ToolError( + f"timed out: bash has not returned in {self._timeout} seconds and must be restarted", + ) + + # we know these are not None because we created the process with PIPEs + assert self._process.stdin + assert self._process.stdout + assert self._process.stderr + + # send command to the process + self._process.stdin.write( + command.encode() + f"; echo '{self._sentinel}'\n".encode() + ) + await self._process.stdin.drain() + + # read output from the process, until the sentinel is found + try: + async with asyncio.timeout(self._timeout): + while True: + await asyncio.sleep(self._output_delay) + # if we read directly from stdout/stderr, it will wait forever for + # EOF. use the StreamReader buffer directly instead. + output = ( + self._process.stdout._buffer.decode() + ) # pyright: ignore[reportAttributeAccessIssue] + if self._sentinel in output: + # strip the sentinel and break + output = output[: output.index(self._sentinel)] + break + except asyncio.TimeoutError: + self._timed_out = True + raise ToolError( + f"timed out: bash has not returned in {self._timeout} seconds and must be restarted", + ) from None + + if output.endswith("\n"): + output = output[:-1] + + error = ( + self._process.stderr._buffer.decode() + ) # pyright: ignore[reportAttributeAccessIssue] + if error.endswith("\n"): + error = error[:-1] + + # clear the buffers so that the next output can be read correctly + self._process.stdout._buffer.clear() # pyright: ignore[reportAttributeAccessIssue] + self._process.stderr._buffer.clear() # pyright: ignore[reportAttributeAccessIssue] + + return CLIResult(output=output, error=error) + + +class Bash(BaseTool): + """A tool for executing bash commands""" + + name: str = "bash" + description: str = _BASH_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "command": { + "type": "string", + "description": "The bash command to execute. Can be empty to view additional logs when previous exit code is `-1`. Can be `ctrl+c` to interrupt the currently running process.", + }, + }, + "required": ["command"], + } + + _session: Optional[_BashSession] = None + + async def execute( + self, command: str | None = None, restart: bool = False, **kwargs + ) -> CLIResult: + if restart: + if self._session: + self._session.stop() + self._session = _BashSession() + await self._session.start() + + return CLIResult(system="tool has been restarted.") + + if self._session is None: + self._session = _BashSession() + await self._session.start() + + if command is not None: + return await self._session.run(command) + + raise ToolError("no command provided.") + + +if __name__ == "__main__": + bash = Bash() + rst = asyncio.run(bash.execute("ls -l")) + print(rst) diff --git a/app/tool/browser_use_tool.py b/app/tool/browser_use_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..449e8e558f0d9c3b5caac73080475fc2cbc61ae4 --- /dev/null +++ b/app/tool/browser_use_tool.py @@ -0,0 +1,567 @@ +import asyncio +import base64 +import json +from typing import Generic, Optional, TypeVar + +from browser_use import Browser as BrowserUseBrowser +from browser_use import BrowserConfig +from browser_use.browser.context import BrowserContext, BrowserContextConfig +from browser_use.dom.service import DomService +from pydantic import Field, field_validator +from pydantic_core.core_schema import ValidationInfo + +from app.config import config +from app.llm import LLM +from app.tool.base import BaseTool, ToolResult +from app.tool.web_search import WebSearch + + +_BROWSER_DESCRIPTION = """\ +A powerful browser automation tool that allows interaction with web pages through various actions. +* This tool provides commands for controlling a browser session, navigating web pages, and extracting information +* It maintains state across calls, keeping the browser session alive until explicitly closed +* Use this when you need to browse websites, fill forms, click buttons, extract content, or perform web searches +* Each action requires specific parameters as defined in the tool's dependencies + +Key capabilities include: +* Navigation: Go to specific URLs, go back, search the web, or refresh pages +* Interaction: Click elements, input text, select from dropdowns, send keyboard commands +* Scrolling: Scroll up/down by pixel amount or scroll to specific text +* Content extraction: Extract and analyze content from web pages based on specific goals +* Tab management: Switch between tabs, open new tabs, or close tabs + +Note: When using element indices, refer to the numbered elements shown in the current browser state. +""" + +Context = TypeVar("Context") + + +class BrowserUseTool(BaseTool, Generic[Context]): + name: str = "browser_use" + description: str = _BROWSER_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "go_to_url", + "click_element", + "input_text", + "scroll_down", + "scroll_up", + "scroll_to_text", + "send_keys", + "get_dropdown_options", + "select_dropdown_option", + "go_back", + "web_search", + "wait", + "extract_content", + "switch_tab", + "open_tab", + "close_tab", + ], + "description": "The browser action to perform", + }, + "url": { + "type": "string", + "description": "URL for 'go_to_url' or 'open_tab' actions", + }, + "index": { + "type": "integer", + "description": "Element index for 'click_element', 'input_text', 'get_dropdown_options', or 'select_dropdown_option' actions", + }, + "text": { + "type": "string", + "description": "Text for 'input_text', 'scroll_to_text', or 'select_dropdown_option' actions", + }, + "scroll_amount": { + "type": "integer", + "description": "Pixels to scroll (positive for down, negative for up) for 'scroll_down' or 'scroll_up' actions", + }, + "tab_id": { + "type": "integer", + "description": "Tab ID for 'switch_tab' action", + }, + "query": { + "type": "string", + "description": "Search query for 'web_search' action", + }, + "goal": { + "type": "string", + "description": "Extraction goal for 'extract_content' action", + }, + "keys": { + "type": "string", + "description": "Keys to send for 'send_keys' action", + }, + "seconds": { + "type": "integer", + "description": "Seconds to wait for 'wait' action", + }, + }, + "required": ["action"], + "dependencies": { + "go_to_url": ["url"], + "click_element": ["index"], + "input_text": ["index", "text"], + "switch_tab": ["tab_id"], + "open_tab": ["url"], + "scroll_down": ["scroll_amount"], + "scroll_up": ["scroll_amount"], + "scroll_to_text": ["text"], + "send_keys": ["keys"], + "get_dropdown_options": ["index"], + "select_dropdown_option": ["index", "text"], + "go_back": [], + "web_search": ["query"], + "wait": ["seconds"], + "extract_content": ["goal"], + }, + } + + lock: asyncio.Lock = Field(default_factory=asyncio.Lock) + browser: Optional[BrowserUseBrowser] = Field(default=None, exclude=True) + context: Optional[BrowserContext] = Field(default=None, exclude=True) + dom_service: Optional[DomService] = Field(default=None, exclude=True) + web_search_tool: WebSearch = Field(default_factory=WebSearch, exclude=True) + + # Context for generic functionality + tool_context: Optional[Context] = Field(default=None, exclude=True) + + llm: Optional[LLM] = Field(default_factory=LLM) + + @field_validator("parameters", mode="before") + def validate_parameters(cls, v: dict, info: ValidationInfo) -> dict: + if not v: + raise ValueError("Parameters cannot be empty") + return v + + async def _ensure_browser_initialized(self) -> BrowserContext: + """Ensure browser and context are initialized.""" + if self.browser is None: + browser_config_kwargs = {"headless": False, "disable_security": True} + + if config.browser_config: + from browser_use.browser.browser import ProxySettings + + # handle proxy settings. + if config.browser_config.proxy and config.browser_config.proxy.server: + browser_config_kwargs["proxy"] = ProxySettings( + server=config.browser_config.proxy.server, + username=config.browser_config.proxy.username, + password=config.browser_config.proxy.password, + ) + + browser_attrs = [ + "headless", + "disable_security", + "extra_chromium_args", + "chrome_instance_path", + "wss_url", + "cdp_url", + ] + + for attr in browser_attrs: + value = getattr(config.browser_config, attr, None) + if value is not None: + if not isinstance(value, list) or value: + browser_config_kwargs[attr] = value + + self.browser = BrowserUseBrowser(BrowserConfig(**browser_config_kwargs)) + + if self.context is None: + context_config = BrowserContextConfig() + + # if there is context config in the config, use it. + if ( + config.browser_config + and hasattr(config.browser_config, "new_context_config") + and config.browser_config.new_context_config + ): + context_config = config.browser_config.new_context_config + + self.context = await self.browser.new_context(context_config) + self.dom_service = DomService(await self.context.get_current_page()) + + return self.context + + async def execute( + self, + action: str, + url: Optional[str] = None, + index: Optional[int] = None, + text: Optional[str] = None, + scroll_amount: Optional[int] = None, + tab_id: Optional[int] = None, + query: Optional[str] = None, + goal: Optional[str] = None, + keys: Optional[str] = None, + seconds: Optional[int] = None, + **kwargs, + ) -> ToolResult: + """ + Execute a specified browser action. + + Args: + action: The browser action to perform + url: URL for navigation or new tab + index: Element index for click or input actions + text: Text for input action or search query + scroll_amount: Pixels to scroll for scroll action + tab_id: Tab ID for switch_tab action + query: Search query for Google search + goal: Extraction goal for content extraction + keys: Keys to send for keyboard actions + seconds: Seconds to wait + **kwargs: Additional arguments + + Returns: + ToolResult with the action's output or error + """ + async with self.lock: + try: + context = await self._ensure_browser_initialized() + + # Get max content length from config + max_content_length = getattr( + config.browser_config, "max_content_length", 2000 + ) + + # Navigation actions + if action == "go_to_url": + if not url: + return ToolResult( + error="URL is required for 'go_to_url' action" + ) + page = await context.get_current_page() + await page.goto(url) + await page.wait_for_load_state() + return ToolResult(output=f"Navigated to {url}") + + elif action == "go_back": + await context.go_back() + return ToolResult(output="Navigated back") + + elif action == "refresh": + await context.refresh_page() + return ToolResult(output="Refreshed current page") + + elif action == "web_search": + if not query: + return ToolResult( + error="Query is required for 'web_search' action" + ) + # Execute the web search and return results directly without browser navigation + search_response = await self.web_search_tool.execute( + query=query, fetch_content=True, num_results=1 + ) + # Navigate to the first search result + first_search_result = search_response.results[0] + url_to_navigate = first_search_result.url + + page = await context.get_current_page() + await page.goto(url_to_navigate) + await page.wait_for_load_state() + + return search_response + + # Element interaction actions + elif action == "click_element": + if index is None: + return ToolResult( + error="Index is required for 'click_element' action" + ) + element = await context.get_dom_element_by_index(index) + if not element: + return ToolResult(error=f"Element with index {index} not found") + download_path = await context._click_element_node(element) + output = f"Clicked element at index {index}" + if download_path: + output += f" - Downloaded file to {download_path}" + return ToolResult(output=output) + + elif action == "input_text": + if index is None or not text: + return ToolResult( + error="Index and text are required for 'input_text' action" + ) + element = await context.get_dom_element_by_index(index) + if not element: + return ToolResult(error=f"Element with index {index} not found") + await context._input_text_element_node(element, text) + return ToolResult( + output=f"Input '{text}' into element at index {index}" + ) + + elif action == "scroll_down" or action == "scroll_up": + direction = 1 if action == "scroll_down" else -1 + amount = ( + scroll_amount + if scroll_amount is not None + else context.config.browser_window_size["height"] + ) + await context.execute_javascript( + f"window.scrollBy(0, {direction * amount});" + ) + return ToolResult( + output=f"Scrolled {'down' if direction > 0 else 'up'} by {amount} pixels" + ) + + elif action == "scroll_to_text": + if not text: + return ToolResult( + error="Text is required for 'scroll_to_text' action" + ) + page = await context.get_current_page() + try: + locator = page.get_by_text(text, exact=False) + await locator.scroll_into_view_if_needed() + return ToolResult(output=f"Scrolled to text: '{text}'") + except Exception as e: + return ToolResult(error=f"Failed to scroll to text: {str(e)}") + + elif action == "send_keys": + if not keys: + return ToolResult( + error="Keys are required for 'send_keys' action" + ) + page = await context.get_current_page() + await page.keyboard.press(keys) + return ToolResult(output=f"Sent keys: {keys}") + + elif action == "get_dropdown_options": + if index is None: + return ToolResult( + error="Index is required for 'get_dropdown_options' action" + ) + element = await context.get_dom_element_by_index(index) + if not element: + return ToolResult(error=f"Element with index {index} not found") + page = await context.get_current_page() + options = await page.evaluate( + """ + (xpath) => { + const select = document.evaluate(xpath, document, null, + XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; + if (!select) return null; + return Array.from(select.options).map(opt => ({ + text: opt.text, + value: opt.value, + index: opt.index + })); + } + """, + element.xpath, + ) + return ToolResult(output=f"Dropdown options: {options}") + + elif action == "select_dropdown_option": + if index is None or not text: + return ToolResult( + error="Index and text are required for 'select_dropdown_option' action" + ) + element = await context.get_dom_element_by_index(index) + if not element: + return ToolResult(error=f"Element with index {index} not found") + page = await context.get_current_page() + await page.select_option(element.xpath, label=text) + return ToolResult( + output=f"Selected option '{text}' from dropdown at index {index}" + ) + + # Content extraction actions + elif action == "extract_content": + if not goal: + return ToolResult( + error="Goal is required for 'extract_content' action" + ) + + page = await context.get_current_page() + import markdownify + + content = markdownify.markdownify(await page.content()) + + prompt = f"""\ +Your task is to extract the content of the page. You will be given a page and a goal, and you should extract all relevant information around this goal from the page. If the goal is vague, summarize the page. Respond in json format. +Extraction goal: {goal} + +Page content: +{content[:max_content_length]} +""" + messages = [{"role": "system", "content": prompt}] + + # Define extraction function schema + extraction_function = { + "type": "function", + "function": { + "name": "extract_content", + "description": "Extract specific information from a webpage based on a goal", + "parameters": { + "type": "object", + "properties": { + "extracted_content": { + "type": "object", + "description": "The content extracted from the page according to the goal", + "properties": { + "text": { + "type": "string", + "description": "Text content extracted from the page", + }, + "metadata": { + "type": "object", + "description": "Additional metadata about the extracted content", + "properties": { + "source": { + "type": "string", + "description": "Source of the extracted content", + } + }, + }, + }, + } + }, + "required": ["extracted_content"], + }, + }, + } + + # Use LLM to extract content with required function calling + response = await self.llm.ask_tool( + messages, + tools=[extraction_function], + tool_choice="required", + ) + + if response and response.tool_calls: + args = json.loads(response.tool_calls[0].function.arguments) + extracted_content = args.get("extracted_content", {}) + return ToolResult( + output=f"Extracted from page:\n{extracted_content}\n" + ) + + return ToolResult(output="No content was extracted from the page.") + + # Tab management actions + elif action == "switch_tab": + if tab_id is None: + return ToolResult( + error="Tab ID is required for 'switch_tab' action" + ) + await context.switch_to_tab(tab_id) + page = await context.get_current_page() + await page.wait_for_load_state() + return ToolResult(output=f"Switched to tab {tab_id}") + + elif action == "open_tab": + if not url: + return ToolResult(error="URL is required for 'open_tab' action") + await context.create_new_tab(url) + return ToolResult(output=f"Opened new tab with {url}") + + elif action == "close_tab": + await context.close_current_tab() + return ToolResult(output="Closed current tab") + + # Utility actions + elif action == "wait": + seconds_to_wait = seconds if seconds is not None else 3 + await asyncio.sleep(seconds_to_wait) + return ToolResult(output=f"Waited for {seconds_to_wait} seconds") + + else: + return ToolResult(error=f"Unknown action: {action}") + + except Exception as e: + return ToolResult(error=f"Browser action '{action}' failed: {str(e)}") + + async def get_current_state( + self, context: Optional[BrowserContext] = None + ) -> ToolResult: + """ + Get the current browser state as a ToolResult. + If context is not provided, uses self.context. + """ + try: + # Use provided context or fall back to self.context + ctx = context or self.context + if not ctx: + return ToolResult(error="Browser context not initialized") + + state = await ctx.get_state() + + # Create a viewport_info dictionary if it doesn't exist + viewport_height = 0 + if hasattr(state, "viewport_info") and state.viewport_info: + viewport_height = state.viewport_info.height + elif hasattr(ctx, "config") and hasattr(ctx.config, "browser_window_size"): + viewport_height = ctx.config.browser_window_size.get("height", 0) + + # Take a screenshot for the state + page = await ctx.get_current_page() + + await page.bring_to_front() + await page.wait_for_load_state() + + screenshot = await page.screenshot( + full_page=True, animations="disabled", type="jpeg", quality=100 + ) + + screenshot = base64.b64encode(screenshot).decode("utf-8") + + # Build the state info with all required fields + state_info = { + "url": state.url, + "title": state.title, + "tabs": [tab.model_dump() for tab in state.tabs], + "help": "[0], [1], [2], etc., represent clickable indices corresponding to the elements listed. Clicking on these indices will navigate to or interact with the respective content behind them.", + "interactive_elements": ( + state.element_tree.clickable_elements_to_string() + if state.element_tree + else "" + ), + "scroll_info": { + "pixels_above": getattr(state, "pixels_above", 0), + "pixels_below": getattr(state, "pixels_below", 0), + "total_height": getattr(state, "pixels_above", 0) + + getattr(state, "pixels_below", 0) + + viewport_height, + }, + "viewport_height": viewport_height, + } + + return ToolResult( + output=json.dumps(state_info, indent=4, ensure_ascii=False), + base64_image=screenshot, + ) + except Exception as e: + return ToolResult(error=f"Failed to get browser state: {str(e)}") + + async def cleanup(self): + """Clean up browser resources.""" + async with self.lock: + if self.context is not None: + await self.context.close() + self.context = None + self.dom_service = None + if self.browser is not None: + await self.browser.close() + self.browser = None + + def __del__(self): + """Ensure cleanup when object is destroyed.""" + if self.browser is not None or self.context is not None: + try: + asyncio.run(self.cleanup()) + except RuntimeError: + loop = asyncio.new_event_loop() + loop.run_until_complete(self.cleanup()) + loop.close() + + @classmethod + def create_with_context(cls, context: Context) -> "BrowserUseTool[Context]": + """Factory method to create a BrowserUseTool with a specific context.""" + tool = cls() + tool.tool_context = context + return tool diff --git a/app/tool/chart_visualization/README.md b/app/tool/chart_visualization/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b5e0df5bb41e82abae619d9aa5ca57985744e88a --- /dev/null +++ b/app/tool/chart_visualization/README.md @@ -0,0 +1,146 @@ + + +# Chart Visualization Tool + +The chart visualization tool generates data processing code through Python and ultimately invokes [@visactor/vmind](https://github.com/VisActor/VMind) to obtain chart specifications. Chart rendering is implemented using [@visactor/vchart](https://github.com/VisActor/VChart). + +## Installation (Mac / Linux) + +1. Install node >= 18 + +```bash +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash +# Activate nvm, for example in Bash +source ~/.bashrc +# Then install the latest stable release of Node +nvm install node +# Activate usage, for example if the latest stable release is 22, then use 22 +nvm use 22 +``` + +2. Install dependencies + +```bash +# Navigate to the appropriate location in the current repository +cd app/tool/chart_visualization +npm install +``` + +## Installation (Windows) +1. Install nvm-windows + + Download the latest version `nvm-setup.exe` from the [official GitHub page](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme) and install it. + +2. Use nvm to install node + +```powershell +# Then install the latest stable release of Node +nvm install node +# Activate usage, for example if the latest stable release is 22, then use 22 +nvm use 22 +``` + +3. Install dependencies + +```bash +# Navigate to the appropriate location in the current repository +cd app/tool/chart_visualization +npm install +``` + +## Tool +### python_execute + +Execute the necessary parts of data analysis (excluding data visualization) using Python code, including data processing, data summary, report generation, and some general Python script code. + +#### Input +```typescript +{ + // Code type: data processing/data report/other general tasks + code_type: "process" | "report" | "others" + // Final execution code + code: string; +} +``` + +#### Output +Python execution results, including the saving of intermediate files and print output results. + +### visualization_preparation + +A pre-tool for data visualization with two purposes, + +#### Data -> Chart +Used to extract the data needed for analysis (.csv) and the corresponding visualization description from the data, ultimately outputting a JSON configuration file. + +#### Chart + Insight -> Chart +Select existing charts and corresponding data insights, choose data insights to add to the chart in the form of data annotations, and finally generate a JSON configuration file. + +#### Input +```typescript +{ + // Code type: data visualization or data insight addition + code_type: "visualization" | "insight" + // Python code used to produce the final JSON file + code: string; +} +``` + +#### Output +A configuration file for data visualization, used for the `data_visualization tool`. + +## data_visualization + +Generate specific data visualizations based on the content of `visualization_preparation`. + +### Input +```typescript +{ + // Configuration file path + json_path: string; + // Current purpose, data visualization or insight annotation addition + tool_type: "visualization" | "insight"; + // Final product png or html; html supports vchart rendering and interaction + output_type: 'png' | 'html' + // Language, currently supports Chinese and English + language: "zh" | "en" +} +``` + +## VMind Configuration + +### LLM + +VMind requires LLM invocation for intelligent chart generation. By default, it uses the `config.llm["default"]` configuration. + +### Generation Settings + +Main configurations include chart dimensions, theme, and generation method: +### Generation Method +Default: png. Currently supports automatic selection of `output_type` by LLM based on context. + +### Dimensions +Default dimensions are unspecified. For HTML output, charts fill the entire page by default. For PNG output, defaults to `1000*1000`. + +### Theme +Default theme: `'light'`. VChart supports multiple themes. See [Themes](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension). + +## Test + +Currently, three tasks of different difficulty levels are set for testing. + +### Simple Chart Generation Task + +Provide data and specific chart generation requirements, test results, execute the command: +```bash +python -m app.tool.chart_visualization.test.chart_demo +``` +The results should be located under `workspace\visualization`, involving 9 different chart results. + +### Simple Data Report Task + +Provide simple raw data analysis requirements, requiring simple processing of the data, execute the command: +```bash +python -m app.tool.chart_visualization.test.report_demo +``` +The results are also located under `workspace\visualization`. diff --git a/app/tool/chart_visualization/README_ja.md b/app/tool/chart_visualization/README_ja.md new file mode 100644 index 0000000000000000000000000000000000000000..7c702785fa832561f7283862419f454172c61d6e --- /dev/null +++ b/app/tool/chart_visualization/README_ja.md @@ -0,0 +1,114 @@ +# グラフ可視化ツール + +グラフ可視化ツールは、Pythonを使用してデータ処理コードを生成し、最終的に[@visactor/vmind](https://github.com/VisActor/VMind)を呼び出してグラフのspec結果を得ます。グラフのレンダリングには[@visactor/vchart](https://github.com/VisActor/VChart)を使用します。 + +## インストール (Mac / Linux) + +1. Node >= 18をインストール + +```bash +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash +# nvmを有効化、例としてBashを使用 +source ~/.bashrc +# その後、最新の安定版Nodeをインストール +nvm install node +# 使用を有効化、例えば最新の安定版が22の場合、use 22 +nvm use 22 +``` + +2. 依存関係をインストール + +```bash +cd app/tool/chart_visualization +npm install +``` + +## インストール (Windows) +1. nvm-windowsをインストール + + [GitHub公式サイト](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)から最新バージョンの`nvm-setup.exe`をダウンロードしてインストール + +2. nvmを使用してNodeをインストール + +```powershell +# その後、最新の安定版Nodeをインストール +nvm install node +# 使用を有効化、例えば最新の安定版が22の場合、use 22 +nvm use 22 +``` + +3. 依存関係をインストール + +```bash +# 現在のリポジトリで適切な位置に移動 +cd app/tool/chart_visualization +npm install +``` + +## ツール +### python_execute + +Pythonコードを使用してデータ分析(データ可視化を除く)に必要な部分を実行します。これにはデータ処理、データ要約、レポート生成、および一般的なPythonスクリプトコードが含まれます。 + +#### 入力 +```typescript +{ + // コードタイプ:データ処理/データレポート/その他の一般的なタスク + code_type: "process" | "report" | "others" + // 最終実行コード + code: string; +} +``` + +#### 出力 +Python実行結果、中間ファイルの保存とprint出力結果を含む + +### visualization_preparation + +データ可視化の準備ツールで、2つの用途があります。 + +#### Data -> Chart +データから分析に必要なデータ(.csv)と対応する可視化の説明を抽出し、最終的にJSON設定ファイルを出力します。 + +#### Chart + Insight -> Chart +既存のグラフと対応するデータインサイトを選択し、データインサイトをデータ注釈の形式でグラフに追加し、最終的にJSON設定ファイルを生成します。 + +#### 入力 +```typescript +{ + // コードタイプ:データ可視化またはデータインサイト追加 + code_type: "visualization" | "insight" + // 最終的なJSONファイルを生成するためのPythonコード + code: string; +} +``` + +#### 出力 +データ可視化の設定ファイル、`data_visualization tool`で使用 + +## data_visualization + +`visualization_preparation`の内容に基づいて具体的なデータ可視化を生成 + +### 入力 +```typescript +{ + // 設定ファイルのパス + json_path: string; + // 現在の用途、データ可視化またはインサイト注釈追加 + tool_type: "visualization" | "insight"; + // 最終成果物pngまたはhtml;htmlではvchartのレンダリングとインタラクションをサポート + output_type: 'png' | 'html' + // 言語、現在は中国語と英語をサポート + language: "zh" | "en" +} +``` + +## 出力 +最終的に'png'または'html'の形式でローカルに保存され、保存されたグラフのパスとグラフ内で発見されたデータインサイトを出力 + +## VMind設定 + +### LLM + +VMind自体 diff --git a/app/tool/chart_visualization/README_ko.md b/app/tool/chart_visualization/README_ko.md new file mode 100644 index 0000000000000000000000000000000000000000..4f65f0091dcab68dd6135d7fb5cfe5d0a0437724 --- /dev/null +++ b/app/tool/chart_visualization/README_ko.md @@ -0,0 +1,128 @@ +# 차트 시각화 도구 + +차트 시각화 도구는 Python을 통해 데이터 처리 코드를 생성하고, 최종적으로 [@visactor/vmind](https://github.com/VisActor/VMind)를 호출하여 차트 사양을 얻습니다. 차트 렌더링은 [@visactor/vchart](https://github.com/VisActor/VChart)를 사용하여 구현됩니다. + +## 설치 (Mac / Linux) + +1. Node.js 18 이상 설치 + +```bash +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash +# nvm 활성화, 예를 들어 Bash +source ~/.bashrc +# 그런 다음 최신 안정 버전의 Node 설치 +nvm install node +# 사용 활성화, 예를 들어 최신 안정 버전이 22인 경우 use 22 +nvm use 22 +``` + +2. 의존성 설치 + +```bash +# 현재 저장소에서 해당 위치로 이동 +cd app/tool/chart_visualization +npm install +``` + +## 설치 (Windows) +1. nvm-windows 설치 + + [공식 GitHub 페이지](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)에서 최신 버전의 `nvm-setup.exe`를 다운로드하고 설치합니다. + +2. nvm을 사용하여 Node.js 설치 + +```powershell +# 그런 다음 최신 안정 버전의 Node 설치 +nvm install node +# 사용 활성화, 예를 들어 최신 안정 버전이 22인 경우 use 22 +nvm use 22 +``` + +3. 의존성 설치 + +```bash +# 현재 저장소에서 해당 위치로 이동 +cd app/tool/chart_visualization +npm install +``` + +## 도구 +### python_execute + +Python 코드를 사용하여 데이터 분석의 필요한 부분(데이터 시각화 제외)을 실행합니다. 여기에는 데이터 처리, 데이터 요약, 보고서 생성 및 일부 일반적인 Python 스크립트 코드가 포함됩니다. + +#### 입력 +```typescript +{ + // 코드 유형: 데이터 처리/데이터 보고서/기타 일반 작업 + code_type: "process" | "report" | "others" + // 최종 실행 코드 + code: string; +} +``` + +#### 출력 +Python 실행 결과, 중간 파일 저장 및 출력 결과 포함. + +### visualization_preparation + +데이터 시각화를 위한 사전 도구로 두 가지 목적이 있습니다. + +#### 데이터 -> 차트 +분석에 필요한 데이터(.csv)와 해당 시각화 설명을 데이터에서 추출하여 최종적으로 JSON 구성 파일을 출력합니다. + +#### 차트 + 인사이트 -> 차트 +기존 차트와 해당 데이터 인사이트를 선택하고, 데이터 주석 형태로 차트에 추가할 데이터 인사이트를 선택하여 최종적으로 JSON 구성 파일을 생성합니다. + +#### 입력 +```typescript +{ + // 코드 유형: 데이터 시각화 또는 데이터 인사이트 추가 + code_type: "visualization" | "insight" + // 최종 JSON 파일을 생성하는 데 사용되는 Python 코드 + code: string; +} +``` + +#### 출력 +`data_visualization tool`에 사용되는 데이터 시각화를 위한 구성 파일. + +## data_visualization + +`visualization_preparation`의 내용을 기반으로 특정 데이터 시각화를 생성합니다. + +### 입력 +```typescript +{ + // 구성 파일 경로 + json_path: string; + // 현재 목적, 데이터 시각화 또는 인사이트 주석 추가 + tool_type: "visualization" | "insight"; + // 최종 제품 png 또는 html; html은 vchart 렌더링 및 상호작용 지원 + output_type: 'png' | 'html' + // 언어, 현재 중국어 및 영어 지원 + language: "zh" | "en" +} +``` + +## VMind 구성 + +### LLM + +VMind는 지능형 차트 생성을 위해 LLM 호출이 필요합니다. 기본적으로 `config.llm["default"]` 구성을 사용합니다. + +### 생성 설정 + +주요 구성에는 차트 크기, 테마 및 생성 방법이 포함됩니다. +### 생성 방법 +기본값: png. 현재 LLM이 컨텍스트에 따라 `output_type`을 자동으로 선택하는 것을 지원합니다. + +### 크기 +기본 크기는 지정되지 않았습니다. HTML 출력의 경우 차트는 기본적으로 전체 페이지를 채웁니다. PNG 출력의 경우 기본값은 `1000*1000`입니다. + +### 테마 +기본 테마: `'light'`. VChart는 여러 테마를 지원합니다. [테마](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension)를 참조하세요. + +## 테스트 + +현재, 서로 다른 난이도의 diff --git a/app/tool/chart_visualization/README_zh.md b/app/tool/chart_visualization/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..b1e414c6592554a67e2d7537998a140cb0a390bf --- /dev/null +++ b/app/tool/chart_visualization/README_zh.md @@ -0,0 +1,147 @@ +# 图表可视化工具 + +图表可视化工具,通过python生成数据处理代码,最终调用[@visactor/vmind](https://github.com/VisActor/VMind)得到图表的spec结果,图表渲染使用[@visactor/vchart](https://github.com/VisActor/VChart) + +## 安装(Mac / Linux) + +1. 安装node >= 18 + +```bash +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash +# 激活nvm,以Bash为例 +source ~/.bashrc +# 然后安装 Node 最近一个稳定颁布 +nvm install node +# 激活使用,例如最新一个稳定颁布为22,则use 22 +nvm use 22 +``` + +2. 安装依赖 + +```bash +cd app/tool/chart_visualization +npm install +``` + +## 安装(Windows) +1. 安装nvm-windows + + 从[github官网](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)上下载最新版本`nvm-setup.exe`并且安装 + +2. 使用nvm安装node + +```powershell +# 然后安装 Node 最近一个稳定颁布 +nvm install node +# 激活使用,例如最新一个稳定颁布为22,则use 22 +nvm use 22 +``` + +3. 安装依赖 + +```bash +# 在当前仓库下定位到相应位置 +cd app/tool/chart_visualization +npm install +``` +## Tool +### python_execute + +用python代码执行数据分析(除数据可视化以外)中需要的部分,包括数据处理,数据总结摘要,报告生成以及一些通用python脚本代码 + +#### 输入 +```typescript +{ + // 代码类型:数据处理/数据报告/其他通用任务 + code_type: "process" | "report" | "others" + // 最终执行代码 + code: string; +} +``` + +#### 输出 +python执行结果,带有中间文件的保存和print输出结果 + +### visualization_preparation + +数据可视化前置工具,有两种用途, + +#### Data -〉 Chart +用于从数据中提取需要分析的数据(.csv)和对应可视化的描述,最终输出一份json配置文件。 + +#### Chart + Insight -> Chart +选取已有的图表和对应的数据洞察,挑选数据洞察以数据标注的形式增加到图表中,最终生成一份json配置文件。 + +#### 输入 +```typescript +{ + // 代码类型:数据可视化 或者 数据洞察添加 + code_type: "visualization" | "insight" + // 用于生产最终json文件的python代码 + code: string; +} +``` + +#### 输出 +数据可视化的配置文件,用于`data_visualization tool` + + +## data_visualization + +根据`visualization_preparation`的内容,生成具体的数据可视化 + +### 输入 +```typescript +{ + // 配置文件路径 + json_path: string; + // 当前用途,数据可视化或者洞察标注添加 + tool_type: "visualization" | "insight"; + // 最终产物png或者html;html下支持vchart渲染和交互 + output_type: 'png' | 'html' + // 语言,目前支持中文和英文 + language: "zh" | "en" +} +``` + +## 输出 +最终以'png'或者'html'的形式保存在本地,输出保存的图表路径以及图表中发现的数据洞察 + +## VMind配置 + +### LLM + +VMind本身也需要通过调用大模型得到智能图表生成结果,目前默认会使用`config.llm["default"]`配置 + +### 生成配置 + +主要生成配置包括图表的宽高、主题以及生成方式; +### 生成方式 +默认为png,目前支持大模型根据上下文自己选择`output_type` + +### 宽高 +目前默认不指定宽高,`html`下默认占满整个页面,'png'下默认为`1000 * 1000` + +### 主题 +目前默认主题为`'light'`,VChart图表支持多种主题,详见[主题](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension) + + +## 测试 + +当前设置了三种不同难度的任务用于测试 + +### 简单图表生成任务 + +给予数据和具体的图表生成需求,测试结果,执行命令: +```bash +python -m app.tool.chart_visualization.test.chart_demo +``` +结果应位于`worksapce\visualization`下,涉及到9种不同的图表结果 + +### 简单数据报表任务 + +给予简单原始数据可分析需求,需要对数据进行简单加工处理,执行命令: +```bash +python -m app.tool.chart_visualization.test.report_demo +``` +结果同样位于`worksapce\visualization`下 diff --git a/app/tool/chart_visualization/__init__.py b/app/tool/chart_visualization/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..ea7d51a39efe0fbae3bf1398063db7aac501fd3a --- /dev/null +++ b/app/tool/chart_visualization/__init__.py @@ -0,0 +1,6 @@ +from app.tool.chart_visualization.chart_prepare import VisualizationPrepare +from app.tool.chart_visualization.data_visualization import DataVisualization +from app.tool.chart_visualization.python_execute import NormalPythonExecute + + +__all__ = ["DataVisualization", "VisualizationPrepare", "NormalPythonExecute"] diff --git a/app/tool/chart_visualization/chart_prepare.py b/app/tool/chart_visualization/chart_prepare.py new file mode 100644 index 0000000000000000000000000000000000000000..1eed35e4db431b866d232fde2a0ea71af625d600 --- /dev/null +++ b/app/tool/chart_visualization/chart_prepare.py @@ -0,0 +1,38 @@ +from app.tool.chart_visualization.python_execute import NormalPythonExecute + + +class VisualizationPrepare(NormalPythonExecute): + """A tool for Chart Generation Preparation""" + + name: str = "visualization_preparation" + description: str = "Using Python code to generates metadata of data_visualization tool. Outputs: 1) JSON Information. 2) Cleaned CSV data files (Optional)." + parameters: dict = { + "type": "object", + "properties": { + "code_type": { + "description": "code type, visualization: csv -> chart; insight: choose insight into chart", + "type": "string", + "default": "visualization", + "enum": ["visualization", "insight"], + }, + "code": { + "type": "string", + "description": """Python code for data_visualization prepare. +## Visualization Type +1. Data loading logic +2. Csv Data and chart description generate +2.1 Csv data (The data you want to visulazation, cleaning / transform from origin data, saved in .csv) +2.2 Chart description of csv data (The chart title or description should be concise and clear. Examples: 'Product sales distribution', 'Monthly revenue trend'.) +3. Save information in json file.( format: {"csvFilePath": string, "chartTitle": string}[]) +## Insight Type +1. Select the insights from the data_visualization results that you want to add to the chart. +2. Save information in json file.( format: {"chartPath": string, "insights_id": number[]}[]) +# Note +1. You can generate one or multiple csv data with different visualization needs. +2. Make each chart data esay, clean and different. +3. Json file saving in utf-8 with path print: print(json_path) +""", + }, + }, + "required": ["code", "code_type"], + } diff --git a/app/tool/chart_visualization/data_visualization.py b/app/tool/chart_visualization/data_visualization.py new file mode 100644 index 0000000000000000000000000000000000000000..26dfaa985ebe10ac8ed8b5405c2fbb360a19b963 --- /dev/null +++ b/app/tool/chart_visualization/data_visualization.py @@ -0,0 +1,263 @@ +import asyncio +import json +import os +from typing import Any, Hashable + +import pandas as pd +from pydantic import Field, model_validator + +from app.config import config +from app.llm import LLM +from app.logger import logger +from app.tool.base import BaseTool + + +class DataVisualization(BaseTool): + name: str = "data_visualization" + description: str = """Visualize statistical chart or Add insights in chart with JSON info from visualization_preparation tool. You can do steps as follows: +1. Visualize statistical chart +2. Choose insights into chart based on step 1 (Optional) +Outputs: +1. Charts (png/html) +2. Charts Insights (.md)(Optional)""" + parameters: dict = { + "type": "object", + "properties": { + "json_path": { + "type": "string", + "description": """file path of json info with ".json" in the end""", + }, + "output_type": { + "description": "Rendering format (html=interactive)", + "type": "string", + "default": "html", + "enum": ["png", "html"], + }, + "tool_type": { + "description": "visualize chart or add insights", + "type": "string", + "default": "visualization", + "enum": ["visualization", "insight"], + }, + "language": { + "description": "english(en) / chinese(zh)", + "type": "string", + "default": "en", + "enum": ["zh", "en"], + }, + }, + "required": ["code"], + } + llm: LLM = Field(default_factory=LLM, description="Language model instance") + + @model_validator(mode="after") + def initialize_llm(self): + """Initialize llm with default settings if not provided.""" + if self.llm is None or not isinstance(self.llm, LLM): + self.llm = LLM(config_name=self.name.lower()) + return self + + def get_file_path( + self, + json_info: list[dict[str, str]], + path_str: str, + directory: str = None, + ) -> list[str]: + res = [] + for item in json_info: + if os.path.exists(item[path_str]): + res.append(item[path_str]) + elif os.path.exists( + os.path.join(f"{directory or config.workspace_root}", item[path_str]) + ): + res.append( + os.path.join( + f"{directory or config.workspace_root}", item[path_str] + ) + ) + else: + raise Exception(f"No such file or directory: {item[path_str]}") + return res + + def success_output_template(self, result: list[dict[str, str]]) -> str: + content = "" + if len(result) == 0: + return "Is EMPTY!" + for item in result: + content += f"""## {item['title']}\nChart saved in: {item['chart_path']}""" + if "insight_path" in item and item["insight_path"] and "insight_md" in item: + content += "\n" + item["insight_md"] + else: + content += "\n" + return f"Chart Generated Successful!\n{content}" + + async def data_visualization( + self, json_info: list[dict[str, str]], output_type: str, language: str + ) -> str: + data_list = [] + csv_file_path = self.get_file_path(json_info, "csvFilePath") + for index, item in enumerate(json_info): + df = pd.read_csv(csv_file_path[index], encoding="utf-8") + df = df.astype(object) + df = df.where(pd.notnull(df), None) + data_dict_list = df.to_json(orient="records", force_ascii=False) + + data_list.append( + { + "file_name": os.path.basename(csv_file_path[index]).replace( + ".csv", "" + ), + "dict_data": data_dict_list, + "chartTitle": item["chartTitle"], + } + ) + tasks = [ + self.invoke_vmind( + dict_data=item["dict_data"], + chart_description=item["chartTitle"], + file_name=item["file_name"], + output_type=output_type, + task_type="visualization", + language=language, + ) + for item in data_list + ] + + results = await asyncio.gather(*tasks) + error_list = [] + success_list = [] + for index, result in enumerate(results): + csv_path = csv_file_path[index] + if "error" in result and "chart_path" not in result: + error_list.append(f"Error in {csv_path}: {result['error']}") + else: + success_list.append( + { + **result, + "title": json_info[index]["chartTitle"], + } + ) + if len(error_list) > 0: + return { + "observation": f"# Error chart generated{'\n'.join(error_list)}\n{self.success_output_template(success_list)}", + "success": False, + } + else: + return {"observation": f"{self.success_output_template(success_list)}"} + + async def add_insighs( + self, json_info: list[dict[str, str]], output_type: str + ) -> str: + data_list = [] + chart_file_path = self.get_file_path( + json_info, "chartPath", os.path.join(config.workspace_root, "visualization") + ) + for index, item in enumerate(json_info): + if "insights_id" in item: + data_list.append( + { + "file_name": os.path.basename(chart_file_path[index]).replace( + f".{output_type}", "" + ), + "insights_id": item["insights_id"], + } + ) + tasks = [ + self.invoke_vmind( + insights_id=item["insights_id"], + file_name=item["file_name"], + output_type=output_type, + task_type="insight", + ) + for item in data_list + ] + results = await asyncio.gather(*tasks) + error_list = [] + success_list = [] + for index, result in enumerate(results): + chart_path = chart_file_path[index] + if "error" in result and "chart_path" not in result: + error_list.append(f"Error in {chart_path}: {result['error']}") + else: + success_list.append(chart_path) + success_template = ( + f"# Charts Update with Insights\n{','.join(success_list)}" + if len(success_list) > 0 + else "" + ) + if len(error_list) > 0: + return { + "observation": f"# Error in chart insights:{'\n'.join(error_list)}\n{success_template}", + "success": False, + } + else: + return {"observation": f"{success_template}"} + + async def execute( + self, + json_path: str, + output_type: str | None = "html", + tool_type: str | None = "visualization", + language: str | None = "en", + ) -> str: + try: + logger.info(f"📈 data_visualization with {json_path} in: {tool_type} ") + with open(json_path, "r", encoding="utf-8") as file: + json_info = json.load(file) + if tool_type == "visualization": + return await self.data_visualization(json_info, output_type, language) + else: + return await self.add_insighs(json_info, output_type) + except Exception as e: + return { + "observation": f"Error: {e}", + "success": False, + } + + async def invoke_vmind( + self, + file_name: str, + output_type: str, + task_type: str, + insights_id: list[str] = None, + dict_data: list[dict[Hashable, Any]] = None, + chart_description: str = None, + language: str = "en", + ): + llm_config = { + "base_url": self.llm.base_url, + "model": self.llm.model, + "api_key": self.llm.api_key, + } + vmind_params = { + "llm_config": llm_config, + "user_prompt": chart_description, + "dataset": dict_data, + "file_name": file_name, + "output_type": output_type, + "insights_id": insights_id, + "task_type": task_type, + "directory": str(config.workspace_root), + "language": language, + } + # build async sub process + process = await asyncio.create_subprocess_exec( + "npx", + "ts-node", + "src/chartVisualize.ts", + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + cwd=os.path.dirname(__file__), + ) + input_json = json.dumps(vmind_params, ensure_ascii=False).encode("utf-8") + try: + stdout, stderr = await process.communicate(input_json) + stdout_str = stdout.decode("utf-8") + stderr_str = stderr.decode("utf-8") + if process.returncode == 0: + return json.loads(stdout_str) + else: + return {"error": f"Node.js Error: {stderr_str}"} + except Exception as e: + return {"error": f"Subprocess Error: {str(e)}"} diff --git a/app/tool/chart_visualization/package-lock.json b/app/tool/chart_visualization/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..19dae01ea7942c60c5b588b0838bbea9c5e3f5f3 --- /dev/null +++ b/app/tool/chart_visualization/package-lock.json @@ -0,0 +1,8739 @@ +{ + "name": "chart_visualization", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "chart_visualization", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@visactor/vchart": "^1.13.7", + "@visactor/vmind": "2.0.5", + "get-stdin": "^9.0.0", + "puppeteer": "^24.9.0" + }, + "devDependencies": { + "@types/node": "^22.10.1", + "ts-node": "^10.9.2", + "typescript": "^5.7.2" + } + }, + "node_modules/@babel/code-frame": { + "version": "7.27.1", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz", + "integrity": "sha512-cjQ7ZlQ0Mv3b47hABuTevyTuYN4i+loJKGeV9flcCgIK37cCXRh+L1bd3iBHlynerhQ7BhCkn2BPbQUL+rGqFg==", + "dependencies": { + "@babel/helper-validator-identifier": "^7.27.1", + "js-tokens": "^4.0.0", + "picocolors": "^1.1.1" + }, + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "7.27.1", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.27.1.tgz", + "integrity": "sha512-D2hP9eA+Sqx1kBZgzxZh0y1trbuU+JoDkiEwqhQ36nodYqJwyEIhPSdMNd7lOm/4io72luTPWH20Yda0xOuUow==", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.0.tgz", + "integrity": "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==", + "dev": true + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@puppeteer/browsers": { + "version": "2.10.5", + "resolved": "https://registry.npmjs.org/@puppeteer/browsers/-/browsers-2.10.5.tgz", + "integrity": "sha512-eifa0o+i8dERnngJwKrfp3dEq7ia5XFyoqB17S4gK8GhsQE4/P8nxOfQSE0zQHxzzLo/cmF+7+ywEQ7wK7Fb+w==", + "dependencies": { + "debug": "^4.4.1", + "extract-zip": "^2.0.1", + "progress": "^2.0.3", + "proxy-agent": "^6.5.0", + "semver": "^7.7.2", + "tar-fs": "^3.0.8", + "yargs": "^17.7.2" + }, + "bin": { + "browsers": "lib/cjs/main-cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/@puppeteer/browsers/node_modules/cliui": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", + "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.1", + "wrap-ansi": "^7.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@puppeteer/browsers/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/@puppeteer/browsers/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/@puppeteer/browsers/node_modules/yargs": { + "version": "17.7.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", + "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", + "dependencies": { + "cliui": "^8.0.1", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.3", + "y18n": "^5.0.5", + "yargs-parser": "^21.1.1" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@puppeteer/browsers/node_modules/yargs-parser": { + "version": "21.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", + "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==", + "engines": { + "node": ">=12" + } + }, + "node_modules/@resvg/resvg-js": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/@resvg/resvg-js/-/resvg-js-2.4.1.tgz", + "integrity": "sha512-wTOf1zerZX8qYcMmLZw3czR4paI4hXqPjShNwJRh5DeHxvgffUS5KM7XwxtbIheUW6LVYT5fhT2AJiP6mU7U4A==", + "engines": { + "node": ">= 10" + }, + "optionalDependencies": { + "@resvg/resvg-js-android-arm-eabi": "2.4.1", + "@resvg/resvg-js-android-arm64": "2.4.1", + "@resvg/resvg-js-darwin-arm64": "2.4.1", + "@resvg/resvg-js-darwin-x64": "2.4.1", + "@resvg/resvg-js-linux-arm-gnueabihf": "2.4.1", + "@resvg/resvg-js-linux-arm64-gnu": "2.4.1", + "@resvg/resvg-js-linux-arm64-musl": "2.4.1", + "@resvg/resvg-js-linux-x64-gnu": "2.4.1", + "@resvg/resvg-js-linux-x64-musl": "2.4.1", + "@resvg/resvg-js-win32-arm64-msvc": "2.4.1", + "@resvg/resvg-js-win32-ia32-msvc": "2.4.1", + "@resvg/resvg-js-win32-x64-msvc": "2.4.1" + } + }, + "node_modules/@resvg/resvg-js-darwin-arm64": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/@resvg/resvg-js-darwin-arm64/-/resvg-js-darwin-arm64-2.4.1.tgz", + "integrity": "sha512-U1oMNhea+kAXgiEXgzo7EbFGCD1Edq5aSlQoe6LMly6UjHzgx2W3N5kEXCwU/CgN5FiQhZr7PlSJSlcr7mdhfg==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@stdlib/array-base-filled": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-base-filled/-/array-base-filled-0.2.2.tgz", + "integrity": "sha512-T7nB7dni5Y4/nsq6Gc1bAhYfzJbcOdqsmVZJUI698xpDbhCdVCIIaEbf0PnDMGN24psN+5mgAVmnNBom+uF0Xg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-base-zeros": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-base-zeros/-/array-base-zeros-0.2.2.tgz", + "integrity": "sha512-iwxqaEtpi4c2qpqabmhFdaQGkzgo5COwjHPn2T0S0wfJuM1VuVl5UBl15syr+MmZPJQOB1eBbh6F1uTh9597qw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-base-filled": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-float32": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-float32/-/array-float32-0.2.2.tgz", + "integrity": "sha512-pTcy1FNQrrJLL1LMxJjuVpcKJaibbGCFFTe41iCSXpSOC8SuTBuNohrO6K9+xR301Ruxxn4yrzjJJ6Fa3nQJ2g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-float32array-support": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-float64": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-float64/-/array-float64-0.2.2.tgz", + "integrity": "sha512-ZmV5wcacGrhT0maw9dfLXNv4N3ZwFUV3D7ItFfZFGFnKIJbubrWzwtaYnxzIXigrDc8g3F6FVHRpsQLMxq0/lA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-float64array-support": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-uint16": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-uint16/-/array-uint16-0.2.2.tgz", + "integrity": "sha512-z5c/Izw43HkKfb1pTgEUMAS8GFvhtHkkHZSjX3XJN+17P0VjknxjlSvPiCBGqaDX9jXtlWH3mn1LSyDKtJQoeA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-uint16array-support": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-uint32": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-uint32/-/array-uint32-0.2.2.tgz", + "integrity": "sha512-3T894I9C2MqZJJmRCYFTuJp4Qw9RAt+GzYnVPyIXoK1h3TepUXe9VIVx50cUFIibdXycgu0IFGASeAb3YMyupw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-uint32array-support": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/array-uint8": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/array-uint8/-/array-uint8-0.2.2.tgz", + "integrity": "sha512-Ip9MUC8+10U9x0crMKWkpvfoUBBhWzc6k5SI4lxx38neFVmiJ3f+5MBADEagjpoKSBs71vlY2drnEZe+Gs2Ytg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-uint8array-support": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-float32array-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-float32array-support/-/assert-has-float32array-support-0.2.2.tgz", + "integrity": "sha512-pi2akQl8mVki43fF1GNQVLYW0bHIPp2HuRNThX9GjB3OFQTpvrV8/3zPSh4lOxQa5gRiabgf0+Rgeu3AOhEw9A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-float32array": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-float64array-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-float64array-support/-/assert-has-float64array-support-0.2.2.tgz", + "integrity": "sha512-8L3GuKY1o0dJARCOsW9MXcugXapaMTpSG6dGxyNuUVEvFfY5UOzcj9/JIDal5FjqSgqVOGL5qZl2qtRwub34VA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-float64array": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-generator-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-generator-support/-/assert-has-generator-support-0.2.2.tgz", + "integrity": "sha512-TcE9BGV8i7B2OmxPlJ/2DUrAwG0W4fFS/DE7HmVk68PXVZsgyNQ/WP/IHBoazHDjhN5c3dU21c20kM/Bw007Rw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-eval": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-own-property": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-own-property/-/assert-has-own-property-0.2.2.tgz", + "integrity": "sha512-m5rV4Z2/iNkwx2vRsNheM6sQZMzc8rQQOo90LieICXovXZy8wA5jNld4kRKjMNcRt/TjrNP7i2Rhh8hruRDlHg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-symbol-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-symbol-support/-/assert-has-symbol-support-0.2.2.tgz", + "integrity": "sha512-vCsGGmDZz5dikGgdF26rIL0y0nHvH7qaVf89HLLTybceuZijAqFSJEqcB3Gpl5uaeueLNAWExHi2EkoUVqKHGg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-tostringtag-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-tostringtag-support/-/assert-has-tostringtag-support-0.2.2.tgz", + "integrity": "sha512-bSHGqku11VH0swPEzO4Y2Dr+lTYEtjSWjamwqCTC8udOiOIOHKoxuU4uaMGKJjVfXG1L+XefLHqzuO5azxdRaA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-symbol-support": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-uint16array-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-uint16array-support/-/assert-has-uint16array-support-0.2.2.tgz", + "integrity": "sha512-aL188V7rOkkEH4wYjfpB+1waDO4ULxo5ppGEK6X0kG4YiXYBL2Zyum53bjEQvo0Nkn6ixe18dNzqqWWytBmDeg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-uint16array": "^0.2.1", + "@stdlib/constants-uint16-max": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-uint32array-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-uint32array-support/-/assert-has-uint32array-support-0.2.2.tgz", + "integrity": "sha512-+UHKP3mZOACkJ9CQjeKNfbXHm5HGQB862V5nV5q3UQlHPzhslnXKyG1SwAxTx+0g88C/2vlDLeqG8H4TH2UTFA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-uint32array": "^0.2.1", + "@stdlib/constants-uint32-max": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-has-uint8array-support": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-has-uint8array-support/-/assert-has-uint8array-support-0.2.2.tgz", + "integrity": "sha512-VfzrB0BMik9MvPyKcMDJL3waq4nM30RZUrr2EuuQ/RbUpromRWSDbzGTlRq5SfjtJrHDxILPV3rytDCc03dgWA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-uint8array": "^0.2.1", + "@stdlib/constants-uint8-max": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-array/-/assert-is-array-0.2.2.tgz", + "integrity": "sha512-aJyTX2U3JqAGCATgaAX9ygvDHc97GCIKkIhiZm/AZaLoFHPtMA1atQ4bKcefEC8Um9eefryxTHfFPfSr9CoNQQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-big-endian": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-big-endian/-/assert-is-big-endian-0.2.2.tgz", + "integrity": "sha512-mPEl30/bqZh++UyQbxlyOuB7k0wC73y5J9nD2J6Ud6Fcl76R5IAGHRW0WT3W18is/6jG1jzMd8hrISFyD7N0sA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-uint16": "^0.2.1", + "@stdlib/array-uint8": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-boolean": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-boolean/-/assert-is-boolean-0.2.2.tgz", + "integrity": "sha512-3KFLRTYZpX6u95baZ6PubBvjehJs2xBU6+zrenR0jx8KToUYCnJPxqqj7JXRhSD+cOURmcjj9rocVaG9Nz18Pg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-tostringtag-support": "^0.2.2", + "@stdlib/boolean-ctor": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-buffer": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-buffer/-/assert-is-buffer-0.2.2.tgz", + "integrity": "sha512-4/WMFTEcDYlVbRhxY8Wlqag4S70QCnn6WmQ4wmfiLW92kqQHsLvTNvdt/qqh/SDyDV31R/cpd3QPsVN534dNEA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-object-like": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-float32array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-float32array/-/assert-is-float32array-0.2.2.tgz", + "integrity": "sha512-hxEKz/Y4m1NYuOaiQKoqQA1HeAYwNXFqSk3FJ4hC71DuGNit2tuxucVyck3mcWLpLmqo0+Qlojgwo5P9/C/9MQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-float64array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-float64array/-/assert-is-float64array-0.2.2.tgz", + "integrity": "sha512-3R1wLi6u/IHXsXMtaLnvN9BSpqAJ8tWhwjOOr6kadDqCWsU7Odc7xKLeAXAInAxwnV8VDpO4ifym4A3wehazPQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-function": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-function/-/assert-is-function-0.2.2.tgz", + "integrity": "sha512-whY69DUYWljCJ79Cvygp7VzWGOtGTsh3SQhzNuGt+ut6EsOW+8nwiRkyBXYKf/MOF+NRn15pxg8cJEoeRgsPcA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-type-of": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-little-endian": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-little-endian/-/assert-is-little-endian-0.2.2.tgz", + "integrity": "sha512-KMzPndj85jDiE1+hYCpw12k2OQOVkfpCo7ojCmCl8366wtKGEaEdGbz1iH98zkxRvnZLSMXcYXI2z3gtdmB0Ag==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-uint16": "^0.2.1", + "@stdlib/array-uint8": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-number": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-number/-/assert-is-number-0.2.2.tgz", + "integrity": "sha512-sWpJ59GqGbmlcdYSUV/OYkmQW8k47w10+E0K0zPu1x1VKzhjgA5ZB2sJcpgI8Vt3ckRLjdhuc62ZHJkrJujG7A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-tostringtag-support": "^0.2.2", + "@stdlib/number-ctor": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-object": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-object/-/assert-is-object-0.2.2.tgz", + "integrity": "sha512-sNnphJuHyMDHHHaonlx6vaCKMe4sHOn0ag5Ck4iW3kJtM2OZB2J4h8qFcwKzlMk7fgFu7vYNGCZtpm1dYbbUfQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-array": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-object-like": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-object-like/-/assert-is-object-like-0.2.2.tgz", + "integrity": "sha512-MjQBpHdEebbJwLlxh/BKNH8IEHqY0YlcCMRKOQU0UOlILSJg0vG+GL4fDDqtx9FSXxcTqC+w3keHx8kAKvQhzg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-tools-array-function": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-plain-object": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-plain-object/-/assert-is-plain-object-0.2.2.tgz", + "integrity": "sha512-o4AFWgBsSNzZAOOfIrxoDFYTqnLuGiaHDFwIeZGUHdpQeav2Fll+sGeaqOcekF7yKawoswnwWdJqTsjapb4Yzw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-own-property": "^0.2.1", + "@stdlib/assert-is-function": "^0.2.1", + "@stdlib/assert-is-object": "^0.2.1", + "@stdlib/utils-get-prototype-of": "^0.2.1", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-regexp": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-regexp/-/assert-is-regexp-0.2.2.tgz", + "integrity": "sha512-2JtiUtRJxPaVXL7dkWoV3n5jouI65DwYDXsDXg3xo23TXlTNGgU/HhKO4FWC1Yqju7YMZi0hcZSW6E9v8ISqeQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-tostringtag-support": "^0.2.2", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-string": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-string/-/assert-is-string-0.2.2.tgz", + "integrity": "sha512-SOkFg4Hq443hkadM4tzcwTHWvTyKP9ULOZ8MSnnqmU0nBX1zLVFLFGY8jnF6Cary0dL0V7QQBCfuxqKFM6u2PQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-tostringtag-support": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-uint16array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-uint16array/-/assert-is-uint16array-0.2.2.tgz", + "integrity": "sha512-w3+HeTiXGLJGw5nCqr0WbvgArNMEj7ulED1Yd19xXbmmk2W1ZUB+g9hJDOQTiKsTU4AVyH4/As+aA8eDVmWtmg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-uint32array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-uint32array/-/assert-is-uint32array-0.2.2.tgz", + "integrity": "sha512-3F4nIHg1Qp0mMIsImWUC8DwQ3qBK5vdIJTjS2LufLbFBhHNmv5kK1yJiIXQDTLkENU0STZe05TByo01ZNLOmDQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-is-uint8array": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-is-uint8array/-/assert-is-uint8array-0.2.2.tgz", + "integrity": "sha512-51WnDip6H2RrN0CbqWmfqySAjam8IZ0VjlfUDc3PtcgrZGrKKjVgyHAsT/L3ZDydwF+aB94uvYJu5QyrCPNaZw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/assert-tools-array-function": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/assert-tools-array-function/-/assert-tools-array-function-0.2.2.tgz", + "integrity": "sha512-FYeT7X9x0C8Nh+MN6IJUDz+7i7yB6mio2/SDlrvyepjyPSU/cfHfwW0GEOnQhxZ+keLZC/YqDD930WjRODwMdA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-array": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/string-format": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/boolean-ctor": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/boolean-ctor/-/boolean-ctor-0.2.2.tgz", + "integrity": "sha512-qIkHzmfxDvGzQ3XI9R7sZG97QSaWG5TvWVlrvcysOGT1cs6HtQgnf4D//SRzZ52VLm8oICP+6OKtd8Hpm6G7Ww==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float32": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float32/-/complex-float32-0.2.1.tgz", + "integrity": "sha512-tp83HfJzcZLK7/6P6gZPcAa/8F/aHS7gBHgB6ft45d/n6oE+/VbnyOvsJKanRv8S96kBRj8xkvlWHz4IiBrT0Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-number": "^0.2.1", + "@stdlib/number-float64-base-to-float32": "^0.2.1", + "@stdlib/string-format": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", + "@stdlib/utils-define-property": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float32-ctor": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float32-ctor/-/complex-float32-ctor-0.0.2.tgz", + "integrity": "sha512-QsTLynhTRmDT0mSkfdHj0FSqQSxh2nKx+vvrH3Y0/Cd/r0WoHFZwyibndDxshfkf9B7nist8QKyvV82I3IZciA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-number": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/number-float64-base-to-float32": "^0.2.1", + "@stdlib/string-format": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-define-property": "^0.2.4" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float32-reim": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float32-reim/-/complex-float32-reim-0.1.2.tgz", + "integrity": "sha512-24H+t1xwQF6vhOoMZdDA3TFB4M+jb5Swm/FwNaepovlzVIG2NlthUZs6mZg1T3oegqesIRQRwhpn4jIPjuGiTw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float32": "^0.2.2", + "@stdlib/complex-float32-ctor": "^0.0.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float64": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float64/-/complex-float64-0.2.1.tgz", + "integrity": "sha512-vN9GqlSaonoREf8/RIN9tfNLnkfN4s7AI0DPsGnvc1491oOqq9UqMw8rYTrnxuum9/OaNAAUqDkb5GLu5uTveQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-number": "^0.2.1", + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/string-format": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", + "@stdlib/utils-define-property": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float64-ctor": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float64-ctor/-/complex-float64-ctor-0.0.3.tgz", + "integrity": "sha512-oixCtBif+Uab2rKtgedwQTbQTEC+wVSu4JQH935eJ8Jo0eL6vXUHHlVrkLgYKlCDLvq5px1QQn42Czg/ixh6Gw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-number": "^0.2.2", + "@stdlib/complex-float32-ctor": "^0.0.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/string-format": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-define-property": "^0.2.4" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-float64-reim": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@stdlib/complex-float64-reim/-/complex-float64-reim-0.1.2.tgz", + "integrity": "sha512-q6RnfgbUunApAYuGmkft1oOM3x3xVMVJwNRlRgfIXwKDb8pYt+S/CeIwi3Su5SF6ay3AqA1s+ze7m21osXAJyw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.2", + "@stdlib/complex-float64-ctor": "^0.0.3" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-reim": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/complex-reim/-/complex-reim-0.2.1.tgz", + "integrity": "sha512-67nakj+HwBRx/ha3j/sLbrMr2hwFVgEZtaczOgn1Jy/cU03lKvNbMkR7QI9s+sA+b+A3yJB3ob8ZQSqh3D1+dA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/complex-reimf": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/complex-reimf/-/complex-reimf-0.2.1.tgz", + "integrity": "sha512-6HyPPmo0CEHoBjOg2w70mMFLcFEunM78ljnW6kf1OxjM/mqMaBM1NRpDrQoFwCIdh1RF1ojl3JR0YLllEf0qyQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float32": "^0.2.1", + "@stdlib/complex-float32": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float32-max": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float32-max/-/constants-float32-max-0.2.2.tgz", + "integrity": "sha512-uxvIm/KmIeZP4vyfoqPd72l5/uidnCN9YJT3p7Z2LD8hYN3PPLu6pd/5b51HMFLwfkZ27byRJ9+YK6XnneJP0Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float32-smallest-normal": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float32-smallest-normal/-/constants-float32-smallest-normal-0.2.2.tgz", + "integrity": "sha512-2qkGjGML2/8P9YguHnac2AKXLbfycpYdCxKmuXQdAVzMMNCJWjHoIqZMFG29WBEDBOP057X+48S6WhIqoxRpWA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-e": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-e/-/constants-float64-e-0.2.2.tgz", + "integrity": "sha512-7fxHaABwosbUzpBsw6Z9Dd9MqUYne8x+44EjohVcWDr0p0mHB/DXVYEYTlwEP/U/XbRrKdO3jUG6IO/GsEjzWg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-eps": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-eps/-/constants-float64-eps-0.2.2.tgz", + "integrity": "sha512-61Pb2ip9aPhHXxiCn+VZ0UVw2rMYUp0xrX93FXyB3UTLacrofRKLMKtbV0SFac4VXx5igv2+0G+h6G/fwCgjyw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-eulergamma": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-eulergamma/-/constants-float64-eulergamma-0.2.2.tgz", + "integrity": "sha512-XsuVud0d1hLTQspFzgUSH2e3IawTXLlJi2k4Vg0Nn6juulxfNO9PnAGtHz+p1BynYF/YwN+qhKnISQxrN31rsQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-exponent-bias": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-exponent-bias/-/constants-float64-exponent-bias-0.2.2.tgz", + "integrity": "sha512-zLWkjzDYHSsBsXB/4mwHysOGl64JS3XBt/McjvjCLc/IZpfsUNFxLCl7oVCplXzYYHcQj/RfEBFy6cxQ6FvdpQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-fourth-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-fourth-pi/-/constants-float64-fourth-pi-0.2.2.tgz", + "integrity": "sha512-j0NOg45ouibms4ML8pfS/eDrurdtnhJTNPCGQM4mg3X+1ljsuO0pvkpVCvuz29t5J23KTcfGBXXr90ikoBmjlw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-gamma-lanczos-g": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-gamma-lanczos-g/-/constants-float64-gamma-lanczos-g-0.2.2.tgz", + "integrity": "sha512-hCaZbZ042htCy9mlGrfUEbz4d0xW/DLdr3vHs5KiBWU+G+WHVH33vubSnEoyT0ugWpAk2ZqWXe/V8sLGgOu0xg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-half-ln-two": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-half-ln-two/-/constants-float64-half-ln-two-0.2.2.tgz", + "integrity": "sha512-yv1XhzZR2AfJmnAGL0kdWlIUhc/vqdWol+1Gq2brXPVfgqbUmJu5XZuuK+jZA2k+fHyvRHNEwQRv9OPnOjchFg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-half-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-half-pi/-/constants-float64-half-pi-0.2.2.tgz", + "integrity": "sha512-lM3SiDsZCKiuF5lPThZFFqioIwh1bUiBUnnDMLB04/QkVRCAaXUo+dsq2hOB6iBhHoYhiKds6T+PsHSBlpqAaA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-high-word-abs-mask": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-high-word-abs-mask/-/constants-float64-high-word-abs-mask-0.2.2.tgz", + "integrity": "sha512-YtYngcHlw9qvOpmsSlkNHi6cy/7Y7QkyYh5kJbDvuOUXPDKa3rEwBln4mKjbWsXhmmN0bk7TLypH7Ryd/UAjUQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-high-word-exponent-mask": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-high-word-exponent-mask/-/constants-float64-high-word-exponent-mask-0.2.2.tgz", + "integrity": "sha512-LhYUXvpnLOFnWr8ucHA9N/H75VxcS2T9EoBDTmWBZoKj2Pg0icGVDmcNciRLIWbuPA9osgcKpxoU+ADIfaipVA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-high-word-sign-mask": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-high-word-sign-mask/-/constants-float64-high-word-sign-mask-0.2.1.tgz", + "integrity": "sha512-Fep/Ccgvz5i9d5k96zJsDjgXGno8HJfmH7wihLmziFmA2z9t7NSacH4/BH4rPJ5yXFHLkacNLDxaF1gO1XpcLA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-high-word-significand-mask": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-high-word-significand-mask/-/constants-float64-high-word-significand-mask-0.2.2.tgz", + "integrity": "sha512-eDDyiQ5PR1/qyklrW0Pus0ZopM7BYjkWTjqhSHhj0DibH6UMwSMlIl4ddCh3VX37p5eByuAavnaPgizk5c9mUw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-ln-sqrt-two-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-ln-sqrt-two-pi/-/constants-float64-ln-sqrt-two-pi-0.2.2.tgz", + "integrity": "sha512-C9YS9W/lvv54wUC7DojQSRH9faKw0sMAM09oMRVm8OOYNr01Rs1wXeSPStl9ns4qiV/G13vZzd1I3nGqgqihbw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-ln-two": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-ln-two/-/constants-float64-ln-two-0.2.2.tgz", + "integrity": "sha512-EQ8EJ6B1wPfuhva0aApKIsF7lTna++txV4AUzL2wTfwDHw6RzWpA44u+k54KnLF8ZXUNIYDNQHHvtzdfKrFzCA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max/-/constants-float64-max-0.2.2.tgz", + "integrity": "sha512-S3kcIKTK65hPqirziof3KTYqfFKopgaTnaiDlDKdzaCzBZ5qkrAcRd4vl+W1KHoZruUyWC2/RYZUa/8+h075TQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max-base2-exponent": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max-base2-exponent/-/constants-float64-max-base2-exponent-0.2.2.tgz", + "integrity": "sha512-KmDe98pJ2HXz2SbqyFfSDhlSSVD7JssjbZ5K11HEK2avqMcoCbdHH20T+6/TpA01VqaK8dLbeyphOfALcDdMKA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max-base2-exponent-subnormal": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max-base2-exponent-subnormal/-/constants-float64-max-base2-exponent-subnormal-0.2.1.tgz", + "integrity": "sha512-D1wBNn54Hu2pK6P/yBz0FtPBI3/7HdgK8igYjWDKWUKzC92R/6PHZ9q5NzedcGxoBs8MUk1zNpP0tZyYj9Y4YQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max-ln": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max-ln/-/constants-float64-max-ln-0.2.2.tgz", + "integrity": "sha512-FPAEGjnoQMDPWJbCyyto7HWQ/SY2jjD8IkjyD8aOwENqbswjCbOINXRiK2ar27OOXG7Dv7CCpFpoorTxv0gmfA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max-safe-integer": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max-safe-integer/-/constants-float64-max-safe-integer-0.2.2.tgz", + "integrity": "sha512-d+sxmxhkt980SDFhnnRDSpujPQTv4nEt5Ox3L86HgYZU4mQU/wbzYVkMuHIANW9x3ehww5blnGXTKYG9rQCXAw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-max-safe-nth-factorial": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-max-safe-nth-factorial/-/constants-float64-max-safe-nth-factorial-0.1.0.tgz", + "integrity": "sha512-sppIfkBbeyKNwfRbmNFi5obI7Q+IJCQzfWKYqvzmEJVOkmEg6hhtEeFc8zZJGCU7+Pndc3M2wdbTT5a3rhamHw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-min-base2-exponent": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-min-base2-exponent/-/constants-float64-min-base2-exponent-0.2.2.tgz", + "integrity": "sha512-YZmBiKik6LbWB4EOZ/ZUs/u6OIF742xNK8mhEqL0OEN4NuJe3OdErpOic6KjMmHjQuqCXdFoSqsWZaFHcIN7HA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-min-base2-exponent-subnormal": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-min-base2-exponent-subnormal/-/constants-float64-min-base2-exponent-subnormal-0.2.1.tgz", + "integrity": "sha512-fTXfvctXWj/48gK+gbRBrHuEHEKY4QOJoXSGp414Sz6vUxHusHJJ686p8ze3XqM7CY6fmL09ZgdGz/uhJl/7lw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-min-ln": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-min-ln/-/constants-float64-min-ln-0.2.2.tgz", + "integrity": "sha512-N1Sxjo3uTdEIpHeG2TzaX06UuvpcKHvjYKpIMhJSajbxvfVDURHlc9kIpfbP9C9/YYoCy0FvewA/kvbqNaYypA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-ninf": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-ninf/-/constants-float64-ninf-0.2.2.tgz", + "integrity": "sha512-Iu+wZs/vgudAKVg9FEcRY3FadkmvsWuq/wJ3jIHjhaP5xcnoF3XJUO4IneEndybHwehfJL65NShnDsJcg1gicw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/number-ctor": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-pi/-/constants-float64-pi-0.2.2.tgz", + "integrity": "sha512-ix34KmpUQ0LUM++L6avLhM9LFCcGTlsUDyWD/tYVGZBiIzDS3TMKShHRkZvC+v87fuyYNPoxolYtk5AlbacI6g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-pinf": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-pinf/-/constants-float64-pinf-0.2.2.tgz", + "integrity": "sha512-UcwnWaSkUMD8QyKADwkXPlY7yOosCPZpE2EDXf/+WOzuWi5vpsec+JaasD5ggAN8Rv8OTVmexTFs1uZfrHgqVQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-smallest-normal": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-smallest-normal/-/constants-float64-smallest-normal-0.2.2.tgz", + "integrity": "sha512-GXNBkdqLT9X+dU59O1kmb7W5da/RhSXSvxx0xG5r7ipJPOtRLfTXGGvvTzWD4xA3Z5TKlrEL6ww5sph9BsPJnA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-smallest-subnormal": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-smallest-subnormal/-/constants-float64-smallest-subnormal-0.2.2.tgz", + "integrity": "sha512-KuF+scDOsP0okx8RLF+q3l1RheaYChf+u/HbhzFbz82GeCIdIVp86UMwoBgfn8AT8cnR5SrtvLtQw15MGfa/vg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-sqrt-eps": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-sqrt-eps/-/constants-float64-sqrt-eps-0.2.2.tgz", + "integrity": "sha512-X7LnGfnwNnhiwlY+zd3FX6zclsx61MaboGTNAAdaV78YjBDTdGdWMHk5MQo1U17ryPlhdGphOAejhDHeaSnTXQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-sqrt-two": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-sqrt-two/-/constants-float64-sqrt-two-0.2.2.tgz", + "integrity": "sha512-iqqouCuS9pUhjD91i5siScxLDtQTF1HsSZor6jaZRviMiOjCj/mjzxxTFHWUlU/rxHMBBhj/u7i12fv6a7dCAQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-sqrt-two-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-sqrt-two-pi/-/constants-float64-sqrt-two-pi-0.2.2.tgz", + "integrity": "sha512-I8Ylr64x8AFSQ2hFBT8szuIBAy2wqPx69taJMzfcmuM5SnSbS8SE/H19YnCimZErVFo4bz0Rh8Fp3edN4i6teQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-float64-two-pi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-float64-two-pi/-/constants-float64-two-pi-0.2.2.tgz", + "integrity": "sha512-cyXuwYOersVsA8tDSJ0ocMbtOc5KGxjlGvYC4vrpLQVkgNpxcGbA57n6JvaGmNk7+InXXbQ7qhTWGbTNgafcLQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-int32-max": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/constants-int32-max/-/constants-int32-max-0.3.0.tgz", + "integrity": "sha512-jYN84QfG/yP2RYw98OR6UYehFFs0PsGAihV6pYU0ey+WF9IOXgSjRP56KMoZ7ctHwl4wsnj9I+qB2tGuEXr+pQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-uint16-max": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-uint16-max/-/constants-uint16-max-0.2.2.tgz", + "integrity": "sha512-qaFXbxgFnAkt73P5Ch7ODb0TsOTg0LEBM52hw6qt7+gTMZUdS0zBAiy5J2eEkTxA9rD9X3nIyUtLf2C7jafNdw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-uint32-max": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-uint32-max/-/constants-uint32-max-0.2.2.tgz", + "integrity": "sha512-2G44HQgIKDrh3tJUkmvtz+eM+uwDvOMF+2I3sONcTHacANb+zP7la4LDYiTp+HFkPJyfh/kPapXBiHpissAb1A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/constants-uint8-max": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/constants-uint8-max/-/constants-uint8-max-0.2.2.tgz", + "integrity": "sha512-ZTBQq3fqS/Y4ll6cPY5SKaS266EfmKP9PW3YLJaTELmYIzVo9w2RFtfCqN05G3olTQ6Le9MUEE/C6VFgZNElDQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/error-tools-fmtprodmsg": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/error-tools-fmtprodmsg/-/error-tools-fmtprodmsg-0.2.2.tgz", + "integrity": "sha512-2IliQfTes4WV5odPidZFGD5eYDswZrPXob7oOu95Q69ERqImo8WzSwnG2EDbHPyOyYCewuMfM5Ha6Ggf+u944Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/fs-exists": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/fs-exists/-/fs-exists-0.2.2.tgz", + "integrity": "sha512-uGLqc7izCIam2aTyv0miyktl4l8awgRkCS39eIEvvvnKIaTBF6pxfac7FtFHeEQKE3XhtKsOmdQ/yJjUMChLuA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/fs-resolve-parent-path": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/fs-resolve-parent-path/-/fs-resolve-parent-path-0.2.2.tgz", + "integrity": "sha512-ZG78ouZc+pdPLtU+sSpYTvbKTiLUgn6NTtlVFYmcmkYRFn+fGOOakwVuhYMcYG6ti10cLD6WzB/YujxIt8f+nA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-own-property": "^0.2.2", + "@stdlib/assert-is-function": "^0.2.2", + "@stdlib/assert-is-plain-object": "^0.2.2", + "@stdlib/assert-is-string": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/fs-exists": "^0.2.2", + "@stdlib/process-cwd": "^0.2.2", + "@stdlib/string-format": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/function-ctor": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/function-ctor/-/function-ctor-0.2.2.tgz", + "integrity": "sha512-qSn1XQnnhgCSYBfFy4II0dY5eW4wdOprgDTHcOJ3PkPWuZHDC1fXZsok1OYAosHqIiIw44zBFcMS/JRex4ebdQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-even": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-even/-/math-base-assert-is-even-0.2.3.tgz", + "integrity": "sha512-cziGv8F/aNyfME7Wx2XJjnYBnf9vIeh8yTIzlLELd0OqGHqfsHU5OQxxcl9x5DbjZ1G/w0lphWxHFHYCuwFCHw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-integer": "^0.2.4", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-infinite": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-infinite/-/math-base-assert-is-infinite-0.2.2.tgz", + "integrity": "sha512-4zDZuinC3vkXRdQepr0ZTwWX3KgM0VIWqYthOmCSgLLA87L9M9z9MgUZL1QeYeYa0+60epjDcQ8MS3ecT70Jxw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-integer": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-integer/-/math-base-assert-is-integer-0.2.5.tgz", + "integrity": "sha512-Zi8N66GbWtSCR3OUsRdBknjNlX+aBN8w6CaVEP5+Jy/a7MgMYzevS52TNS5sm8jqzKBlFhZlPLex+Zl2GlPvSA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-nan": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-nan/-/math-base-assert-is-nan-0.2.2.tgz", + "integrity": "sha512-QVS8rpWdkR9YmHqiYLDVLsCiM+dASt/2feuTl4T/GSdou3Y/PS/4j/tuDvCDoHDNfDkULUW+FCVjKYpbyoeqBQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-negative-zero": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-negative-zero/-/math-base-assert-is-negative-zero-0.2.2.tgz", + "integrity": "sha512-WvKNuBZ6CDarOTzOuFLmO1jwZnFD+butIvfD2Ws6SsuqSCiWOaF4OhIckqPzo1XEdkqqhRNPqBxqc0D+hsEYVA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-odd": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-odd/-/math-base-assert-is-odd-0.3.0.tgz", + "integrity": "sha512-V44F3xdR5/bHXqqYvE/AldLnVmijLr/rgf7EjnJXXDQLfPCgemy0iHTFl19N68KG1YO9SMPdyOaNjh4K0O9Qqw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-even": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-assert-is-positive-zero": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-positive-zero/-/math-base-assert-is-positive-zero-0.2.2.tgz", + "integrity": "sha512-mMX5xsemKpHRAgjpVJCb3eVZ3WIkZh6KnHQH8i8n4vI44pcdpN5rcTdEAMlhLjxT/rT7H2wq85f7/FRsq9r9rw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-napi-binary": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.3.0.tgz", + "integrity": "sha512-bhwsmGMOMN1srcpNAFRjDMSXe9ue1s/XmaoBBlqcG6S2nqRQlIVnKKH4WZx4hmC1jDqoFXuNPJGE47VXpVV+mA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32-ctor": "^0.0.2", + "@stdlib/complex-float32-reim": "^0.1.2", + "@stdlib/complex-float64-ctor": "^0.0.3", + "@stdlib/complex-float64-reim": "^0.1.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-napi-unary": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-unary/-/math-base-napi-unary-0.2.3.tgz", + "integrity": "sha512-BCyJmpq2S8EFo2yMt1z+v1EL7nn8RHcM6jn7fa8n3BTP679K0MSlawIh3A0CFogfrTdjPM4G44VO1ddsdLExcg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32-ctor": "^0.0.2", + "@stdlib/complex-float32-reim": "^0.1.1", + "@stdlib/complex-float64-ctor": "^0.0.3", + "@stdlib/complex-float64-reim": "^0.1.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-abs": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-abs/-/math-base-special-abs-0.2.2.tgz", + "integrity": "sha512-cw5CXj05c/L0COaD9J+paHXwmoN5IBYh+Spk0331f1pEMvGxSO1KmCREZaooUEEFKPhKDukEHKeitja2yAQh4Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-acos": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-acos/-/math-base-special-acos-0.2.3.tgz", + "integrity": "sha512-f66Ikq0E3U5XQm6sTu4UHwP3TmcPrVgSK/mZTvg2JenswZ6qPtGO1A8KHZ5+/5bk1TSc9EW4zDGUqWG7mGzT4Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-fourth-pi": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-asin": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-asin": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-asin/-/math-base-special-asin-0.2.3.tgz", + "integrity": "sha512-Ju1UFJspOOL630SqBtVmUh3lHv5JMu1szcAgx7kQupJwZiwWljoVQ5MmxlNY4l3nGM5oMokenlqTDNXOau43lw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-fourth-pi": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-beta": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-beta/-/math-base-special-beta-0.2.1.tgz", + "integrity": "sha512-/crN/ptCu7ld7KodGkYUJIweUTHdxO5mw+rgkrMqNVqJ83QQPd1czB6hvNYFLfmhy3ckj7t/UYoYhhg/x/Wd7g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-e": "^0.2.1", + "@stdlib/constants-float64-eps": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-exp": "^0.2.1", + "@stdlib/math-base-special-log1p": "^0.2.1", + "@stdlib/math-base-special-pow": "^0.2.1", + "@stdlib/math-base-special-sqrt": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-betainc": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-betainc/-/math-base-special-betainc-0.2.2.tgz", + "integrity": "sha512-95tzDgn5d9RV9al4gxHwKfszd9M6AizlpnhAiwIi0JwqcO+OY3xgbABWal4/H09Tb8DaC9jDqiyGuyPuB0iDew==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-special-kernel-betainc": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-binomcoef": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-binomcoef/-/math-base-special-binomcoef-0.2.3.tgz", + "integrity": "sha512-RxnQ/QGgKUeqTvBL+7IH8rNKQYCfGs0I3PsFYfb0e9V1O2yIVvthURUpzjukurZM89JRapK1dN6aeZ5UM71Zgw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-max-safe-integer": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-gcd": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-ceil": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-ceil/-/math-base-special-ceil-0.2.2.tgz", + "integrity": "sha512-zGkDaMcPrxQ9Zo+fegf2MyI8UPIrVTK5sc/FgCN9qdwEFJTKGLsBd249T3xH7L2MDxx5JbIMGrr6L4U4uEm2Hw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-copysign": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-copysign/-/math-base-special-copysign-0.2.2.tgz", + "integrity": "sha512-m9nWIQhKsaNrZtS2vIPeToWDbzs/T0d0NWy7gSci38auQVufSbF6FYnCKl0f+uwiWlh5GYXs0uVbyCp7FFXN+A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-sign-mask": "^0.2.1", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/number-float64-base-from-words": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-cos": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-cos/-/math-base-special-cos-0.2.1.tgz", + "integrity": "sha512-Yre+ASwsv4pQJk5dqY6488ZfmYDA6vtUTdapAVjCx28NluSFhXw1+S8EmsqnzYnqp/4x7Y1H7V2UPZfw+AdnbQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-special-kernel-cos": "^0.2.1", + "@stdlib/math-base-special-kernel-sin": "^0.2.1", + "@stdlib/math-base-special-rempio2": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-erfc": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-erfc/-/math-base-special-erfc-0.2.4.tgz", + "integrity": "sha512-tVI+mMnW+oDfQXwoH86sZ8q4ximpUXX6wZFCYZB6KcO5GXeKuvK74DnU0YyIm+sTV+r9WJiTSBEW9iVQLZOkzg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-erfcinv": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-erfcinv/-/math-base-special-erfcinv-0.2.3.tgz", + "integrity": "sha512-B8u7WZiIh0+rX8VWNOwvjPWpmeKBHIQoJtIigUseBgbch/rmgV43k63MCkjh2u+V2SmcFo38yD94qJg5bYyWeA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-exp": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-exp/-/math-base-special-exp-0.2.4.tgz", + "integrity": "sha512-G6pZqu1wA4WwBj7DcnztA+/ro61wXJUTpKFLOwrIb2f/28pHGpA//Lub+3vAk6/ksAkhJ+qM/dfdM2ue7zLuEw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-ldexp": "^0.2.3", + "@stdlib/math-base-special-trunc": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-expm1": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-expm1/-/math-base-special-expm1-0.2.3.tgz", + "integrity": "sha512-uJlYZjPjG9X8owuwp1h1/iz9xf21v3dlyEAuutQ0NoacUDzZKVSCbQ3Of0i2Mujn+4N+kjCvEeph6cqhfYAl+A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-half-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/number-float64-base-from-words": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-factorial": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-factorial/-/math-base-special-factorial-0.2.1.tgz", + "integrity": "sha512-uqsANeW4gHFzhgDrV9X0INEwO74MPzQvDVXbxY9+b0E13Vq2HHCi0GqdtPOWXdhOCUk8RkLRs9GizU3X6Coy8A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-special-gamma": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-floor": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-floor/-/math-base-special-floor-0.2.3.tgz", + "integrity": "sha512-zTkxVRawtWwJ4NmAT/1e+ZsIoBj1JqUquGOpiNVGNIKtyLOeCONZlZSbN7zuxPkshvmcSjpQ/VLKR8Tw/37E9A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-fmod": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-fmod/-/math-base-special-fmod-0.1.0.tgz", + "integrity": "sha512-osHwmEOT5MPWOXRx8y3wKCp362eGHIcJRt8LARJJICr/qTZlu1HMnZnbwuhfy1NIQzpJ8aLOhEdl2PrProTt3A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-exponent-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-sign-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-min-base2-exponent": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/number-float64-base-from-words": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma/-/math-base-special-gamma-0.2.1.tgz", + "integrity": "sha512-Sfq1HnVoL4kN9EDHH3YparEAF0r7QD5jNFppUTOXmrqkofgImSl5tLttttnr2I7O9zsNhYkBAiTx9q0y25bAiA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eulergamma": "^0.2.1", + "@stdlib/constants-float64-ninf": "^0.2.1", + "@stdlib/constants-float64-pi": "^0.2.1", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-exp": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.1", + "@stdlib/math-base-special-pow": "^0.2.1", + "@stdlib/math-base-special-sin": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma-delta-ratio": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma-delta-ratio/-/math-base-special-gamma-delta-ratio-0.2.2.tgz", + "integrity": "sha512-lan+cfafH7aoyUxa88vLO+pYwLA+0uiyVFmCumxDemQUboCrTiNCYhBjONFGI/ljE3RukHoE3ZV4AccIcx526A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-e": "^0.2.2", + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/constants-float64-gamma-lanczos-g": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-factorial": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-gamma": "^0.2.1", + "@stdlib/math-base-special-gamma-lanczos-sum": "^0.3.0", + "@stdlib/math-base-special-log1p": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma-delta-ratio/node_modules/@stdlib/math-base-special-pow": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.3.0.tgz", + "integrity": "sha512-sMDYRUYGFyMXDHcCYy7hE07lV7jgI6rDspLMROKyESWcH4n8j54XE4/0w0i8OpdzR40H895MaPWU/tVnU1tP6w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/number-uint32-base-to-int32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma-lanczos-sum": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma-lanczos-sum/-/math-base-special-gamma-lanczos-sum-0.3.0.tgz", + "integrity": "sha512-q13p6r7G0TmbD54cU8QgG8wGgdGGznV9dNKiNszw+hOqCQ+1DqziG8I6vN64R3EQLP7QN4yVprZcmuXSK+fgsg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled/-/math-base-special-gamma-lanczos-sum-expg-scaled-0.3.0.tgz", + "integrity": "sha512-hScjKZvueOK5piX84ZLIV3ZiYvtvYtcixN8psxkPIxJlN7Bd5nAmSkEOBL+T+LeW2RjmdEMXFFJMF7FsK1js/Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gamma1pm1": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma1pm1/-/math-base-special-gamma1pm1-0.2.2.tgz", + "integrity": "sha512-lNT1lk0ifK2a/ta3GfR5V8KvfgkgheE44n5AQ/07BBfcVBMiAdqNuyjSMeWqsH/zVGzjU6G8+kLBzmaJXivPXQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-expm1": "^0.2.3", + "@stdlib/math-base-special-gamma": "^0.2.1", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-log1p": "^0.2.3" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammainc": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gammainc/-/math-base-special-gammainc-0.2.2.tgz", + "integrity": "sha512-ffKZFiv/41SXs2Xms7IW3lPnICR898yfWAidq5uKjOLgRb3wrzNjq0sZ6EAVXvdBwyGULvSjyud28PpVhDLv3A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-e": "^0.2.2", + "@stdlib/constants-float64-gamma-lanczos-g": "^0.2.2", + "@stdlib/constants-float64-max": "^0.2.2", + "@stdlib/constants-float64-max-ln": "^0.2.2", + "@stdlib/constants-float64-min-ln": "^0.2.2", + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/constants-float64-sqrt-eps": "^0.2.2", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.2", + "@stdlib/constants-float64-two-pi": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-erfc": "^0.2.4", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-gamma": "^0.3.0", + "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled": "^0.3.0", + "@stdlib/math-base-special-gamma1pm1": "^0.2.2", + "@stdlib/math-base-special-gammaln": "^0.2.2", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-log1p": "^0.2.3", + "@stdlib/math-base-special-log1pmx": "^0.2.3", + "@stdlib/math-base-special-max": "^0.3.0", + "@stdlib/math-base-special-min": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-powm1": "^0.3.0", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/math-base-tools-continued-fraction": "^0.2.2", + "@stdlib/math-base-tools-evalpoly": "^0.2.2", + "@stdlib/math-base-tools-sum-series": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammainc/node_modules/@stdlib/math-base-special-gamma": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma/-/math-base-special-gamma-0.3.0.tgz", + "integrity": "sha512-YfW+e5xuSDoUxgpquXPrFtAbdwOzE7Kqt7M0dcAkDNot8/yUn+QmrDGzURyBVzUyhRm9SaC9bACHxTShdJkcuA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eulergamma": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sin": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammainc/node_modules/@stdlib/math-base-special-max": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-max/-/math-base-special-max-0.3.0.tgz", + "integrity": "sha512-yXsmdFCLHRB24l34Kn1kHZXHKoGqBxPY/5Mi+n5qLg+FwrX85ZG6KGVbO3DfcpG1NxDTcEKb1hxbUargI0P5fw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-positive-zero": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammainc/node_modules/@stdlib/math-base-special-max/node_modules/@stdlib/math-base-napi-binary": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.2.1.tgz", + "integrity": "sha512-ewGarSRaz5gaLsE17yJ4me03e56ICgPAA0ru0SYFCeMK2E5Z4Z2Lbu7HAQTTg+8XhpoaZSw0h2GJopTV7PCKmw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1", + "@stdlib/complex-reim": "^0.2.1", + "@stdlib/complex-reimf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammainc/node_modules/@stdlib/math-base-special-pow": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.3.0.tgz", + "integrity": "sha512-sMDYRUYGFyMXDHcCYy7hE07lV7jgI6rDspLMROKyESWcH4n8j54XE4/0w0i8OpdzR40H895MaPWU/tVnU1tP6w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/number-uint32-base-to-int32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammaincinv": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gammaincinv/-/math-base-special-gammaincinv-0.2.2.tgz", + "integrity": "sha512-bIZ94ob1rY87seDWsvBTBRxp8Ja2Y46DLtQYuaylHUQuK+I2xKy8XKL2ZHPsOfuwhXRqm+q+91PDjPEAdH1dQw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float32-max": "^0.2.2", + "@stdlib/constants-float32-smallest-normal": "^0.2.2", + "@stdlib/constants-float64-ln-sqrt-two-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.2", + "@stdlib/constants-float64-two-pi": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-erfcinv": "^0.2.3", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-gamma": "^0.3.0", + "@stdlib/math-base-special-gammainc": "^0.2.2", + "@stdlib/math-base-special-gammaln": "^0.2.2", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-min": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/math-base-tools-evalpoly": "^0.2.2", + "debug": "^2.6.9" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammaincinv/node_modules/@stdlib/math-base-special-gamma": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma/-/math-base-special-gamma-0.3.0.tgz", + "integrity": "sha512-YfW+e5xuSDoUxgpquXPrFtAbdwOzE7Kqt7M0dcAkDNot8/yUn+QmrDGzURyBVzUyhRm9SaC9bACHxTShdJkcuA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eulergamma": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sin": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammaincinv/node_modules/@stdlib/math-base-special-pow": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.3.0.tgz", + "integrity": "sha512-sMDYRUYGFyMXDHcCYy7hE07lV7jgI6rDspLMROKyESWcH4n8j54XE4/0w0i8OpdzR40H895MaPWU/tVnU1tP6w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/number-uint32-base-to-int32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gammaln": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gammaln/-/math-base-special-gammaln-0.2.2.tgz", + "integrity": "sha512-opG6HUlspi/GLvQAr4pcwyAevm7BYuymlopgNZ1VulWUvksDpytalaX3zva0idlD2HvniKrDmzHngT1N9p0J1A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-sinpi": "^0.2.1", + "@stdlib/math-base-special-trunc": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gcd": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gcd/-/math-base-special-gcd-0.2.1.tgz", + "integrity": "sha512-w10k9W176lDkbiDIwnmVr1nkTyypTQLwA3/CN9qEUmXh/u8NlxkSnDYBpArcWnxE0oFaIggw8sLJ58TuMvxMaw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.1", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/constants-int32-max": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-gcd/node_modules/@stdlib/constants-int32-max": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/constants-int32-max/-/constants-int32-max-0.2.1.tgz", + "integrity": "sha512-vKtp3q/HdAeGG8BJBZdNzFrYpVQeleODgvOxh9Pn/TX1Ktjc50I9TVl7nTVWsT2QnacruOorILk2zNsdgBHPUQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-kernel-betainc/-/math-base-special-kernel-betainc-0.2.2.tgz", + "integrity": "sha512-DQwQUWQkmZtjRgdvZ1yZOEdAYLQoEUEndbr47Z69Oe6AgwKwxxpZUh09h9imKheFCFHLVnwVUz20azIM5KifQw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-e": "^0.2.2", + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/constants-float64-gamma-lanczos-g": "^0.2.2", + "@stdlib/constants-float64-half-pi": "^0.2.2", + "@stdlib/constants-float64-max": "^0.2.2", + "@stdlib/constants-float64-max-ln": "^0.2.2", + "@stdlib/constants-float64-min-ln": "^0.2.2", + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-smallest-normal": "^0.2.2", + "@stdlib/constants-int32-max": "^0.3.0", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-asin": "^0.2.3", + "@stdlib/math-base-special-beta": "^0.3.0", + "@stdlib/math-base-special-binomcoef": "^0.2.3", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-expm1": "^0.2.3", + "@stdlib/math-base-special-factorial": "^0.3.0", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-gamma": "^0.3.0", + "@stdlib/math-base-special-gamma-delta-ratio": "^0.2.2", + "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled": "^0.3.0", + "@stdlib/math-base-special-gammainc": "^0.2.1", + "@stdlib/math-base-special-gammaln": "^0.2.2", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-log1p": "^0.2.3", + "@stdlib/math-base-special-max": "^0.3.0", + "@stdlib/math-base-special-maxabs": "^0.3.0", + "@stdlib/math-base-special-min": "^0.2.3", + "@stdlib/math-base-special-minabs": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/math-base-tools-continued-fraction": "^0.2.2", + "@stdlib/math-base-tools-sum-series": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-beta": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-beta/-/math-base-special-beta-0.3.0.tgz", + "integrity": "sha512-SWUF1AZLqaEJ8g1Lj0/UOfj955AsIS3QPYH/ZMijELVxCwmp7VRgalI0AxMM09IraJt1cH5WrSwSnouH1WC3ZQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-e": "^0.2.2", + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-log1p": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-factorial": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-factorial/-/math-base-special-factorial-0.3.0.tgz", + "integrity": "sha512-tXdXqstF4gmy4HpzALo3Bhkj2UQSlyk+PU3alWXZH5XtKUozHuXhQDnak+2c4w0JqnKxHq4mnaR2qgjfkDNZcA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-max-safe-nth-factorial": "^0.1.0", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-gamma": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-factorial/node_modules/@stdlib/math-base-assert-is-odd": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-odd/-/math-base-assert-is-odd-0.2.1.tgz", + "integrity": "sha512-V4qQuCO6/AA5udqlNatMRZ8R/MgpqD8mPIkFrpSZJdpLcGYSz815uAAf3NBOuWXkE2Izw0/Tg/hTQ+YcOW2g5g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-even": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-factorial/node_modules/@stdlib/math-base-special-gamma": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma/-/math-base-special-gamma-0.2.1.tgz", + "integrity": "sha512-Sfq1HnVoL4kN9EDHH3YparEAF0r7QD5jNFppUTOXmrqkofgImSl5tLttttnr2I7O9zsNhYkBAiTx9q0y25bAiA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eulergamma": "^0.2.1", + "@stdlib/constants-float64-ninf": "^0.2.1", + "@stdlib/constants-float64-pi": "^0.2.1", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-exp": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.1", + "@stdlib/math-base-special-pow": "^0.2.1", + "@stdlib/math-base-special-sin": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-factorial/node_modules/@stdlib/math-base-special-pow": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.2.1.tgz", + "integrity": "sha512-7SvgVzDkuilZKrHh4tPiXx9fypF/V7PSvAcUVjvcRj5kVEwv/15RpzlmCJlT9B20VPSx4gJ1S0UIA6xgmYFuAg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.1", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.1", + "@stdlib/constants-float64-ln-two": "^0.2.1", + "@stdlib/constants-float64-ninf": "^0.2.1", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/math-base-assert-is-infinite": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-assert-is-odd": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.1", + "@stdlib/math-base-special-sqrt": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.1", + "@stdlib/number-float64-base-set-high-word": "^0.2.1", + "@stdlib/number-float64-base-set-low-word": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/number-uint32-base-to-int32": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-gamma": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-gamma/-/math-base-special-gamma-0.3.0.tgz", + "integrity": "sha512-YfW+e5xuSDoUxgpquXPrFtAbdwOzE7Kqt7M0dcAkDNot8/yUn+QmrDGzURyBVzUyhRm9SaC9bACHxTShdJkcuA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eulergamma": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pi": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/constants-float64-sqrt-two-pi": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-exp": "^0.2.4", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-sin": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-max": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-max/-/math-base-special-max-0.3.0.tgz", + "integrity": "sha512-yXsmdFCLHRB24l34Kn1kHZXHKoGqBxPY/5Mi+n5qLg+FwrX85ZG6KGVbO3DfcpG1NxDTcEKb1hxbUargI0P5fw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-positive-zero": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-max/node_modules/@stdlib/math-base-napi-binary": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.2.1.tgz", + "integrity": "sha512-ewGarSRaz5gaLsE17yJ4me03e56ICgPAA0ru0SYFCeMK2E5Z4Z2Lbu7HAQTTg+8XhpoaZSw0h2GJopTV7PCKmw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1", + "@stdlib/complex-reim": "^0.2.1", + "@stdlib/complex-reimf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betainc/node_modules/@stdlib/math-base-special-pow": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.3.0.tgz", + "integrity": "sha512-sMDYRUYGFyMXDHcCYy7hE07lV7jgI6rDspLMROKyESWcH4n8j54XE4/0w0i8OpdzR40H895MaPWU/tVnU1tP6w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/number-uint32-base-to-int32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-betaincinv": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-kernel-betaincinv/-/math-base-special-kernel-betaincinv-0.1.1.tgz", + "integrity": "sha512-DZLALmQj0m3Wx8L8/na8Jj9vluNj4Z5DxmAPvnA1AWGYy7KsotmP4HXwgSTlsfbXeF3iGcrmworPOo4HJUSxIQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eps": "^0.2.1", + "@stdlib/constants-float64-half-pi": "^0.2.1", + "@stdlib/constants-float64-max": "^0.2.1", + "@stdlib/constants-float64-pi": "^0.2.1", + "@stdlib/constants-float64-smallest-normal": "^0.2.1", + "@stdlib/constants-float64-smallest-subnormal": "^0.2.1", + "@stdlib/constants-float64-sqrt-two": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-acos": "^0.2.1", + "@stdlib/math-base-special-asin": "^0.2.1", + "@stdlib/math-base-special-beta": "^0.2.1", + "@stdlib/math-base-special-betainc": "^0.2.1", + "@stdlib/math-base-special-cos": "^0.2.1", + "@stdlib/math-base-special-erfcinv": "^0.2.1", + "@stdlib/math-base-special-exp": "^0.2.1", + "@stdlib/math-base-special-expm1": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.1", + "@stdlib/math-base-special-gamma-delta-ratio": "^0.2.1", + "@stdlib/math-base-special-gammaincinv": "^0.2.1", + "@stdlib/math-base-special-kernel-betainc": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.1", + "@stdlib/math-base-special-ln": "^0.2.1", + "@stdlib/math-base-special-log1p": "^0.2.1", + "@stdlib/math-base-special-max": "^0.2.1", + "@stdlib/math-base-special-min": "^0.2.1", + "@stdlib/math-base-special-pow": "^0.2.1", + "@stdlib/math-base-special-round": "^0.2.1", + "@stdlib/math-base-special-signum": "^0.2.1", + "@stdlib/math-base-special-sin": "^0.2.1", + "@stdlib/math-base-special-sqrt": "^0.2.1", + "@stdlib/math-base-tools-evalpoly": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-cos": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-kernel-cos/-/math-base-special-kernel-cos-0.2.3.tgz", + "integrity": "sha512-K5FbN25SmEc5Z89GejUkrZpqCv05ZX6D7g9SUFcKWFJ1fwiZNgxrF8q4aJtGDQhuV3q66C1gaKJyQeLq/OI8lQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-kernel-sin": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-kernel-sin/-/math-base-special-kernel-sin-0.2.3.tgz", + "integrity": "sha512-PFnlGdapUaCaMXqZr+tG5Ioq+l4TCyGE5e8XEYlsyhNDILf0XE2ghHzlROA/wW365Arl4sPLWUoo4oH98DUPqw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-ldexp": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-ldexp/-/math-base-special-ldexp-0.2.3.tgz", + "integrity": "sha512-yD4YisQGVTJmTJUshuzpaoq34sxJtrU+Aw4Ih39mzgXiQi6sh3E3nijB8WXDNKr2v05acUWJ1PRMkkJSfu16Kg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-max-base2-exponent": "^0.2.2", + "@stdlib/constants-float64-max-base2-exponent-subnormal": "^0.2.1", + "@stdlib/constants-float64-min-base2-exponent-subnormal": "^0.2.1", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/number-float32-base-to-word": "^0.2.2", + "@stdlib/number-float64-base-exponent": "^0.2.2", + "@stdlib/number-float64-base-from-words": "^0.2.2", + "@stdlib/number-float64-base-normalize": "^0.2.3", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-ln": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-ln/-/math-base-special-ln-0.2.4.tgz", + "integrity": "sha512-lSB47USaixrEmxwadT0/yByvTtxNhaRwN0FIXt5oj38bsgMXGW4V8xrANOy1N+hrn3KGfHJNDyFPYbXWVdMTIw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-log1p": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-log1p/-/math-base-special-log1p-0.2.3.tgz", + "integrity": "sha512-1Pu3attNR+DcskIvhvyls+2KRZ0UCHQ/jP2tvgFI9bWDCgb4oEimXPzjFteGNg9Mj6WlAW2b9wU9tHt3bp8R3g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-log1pmx": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-log1pmx/-/math-base-special-log1pmx-0.2.3.tgz", + "integrity": "sha512-HfjDXcbFztm/GQRrn7a9FMYS0rm/4VPXWa50sYQzBHSYaEwYv5Y1awaZz+cA/ncuqAq1Mw0dfcwEMNRmZtnxEQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-log1p": "^0.2.3", + "@stdlib/math-base-tools-sum-series": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-max": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-max/-/math-base-special-max-0.2.1.tgz", + "integrity": "sha512-jsA3x5azfclbULDFwvHjNlB2nciUDHwrw7qHP/QlSdJi47E1iBDNYdzhlOa3JKzblbrITpzgZEsGBcpCinEInQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-assert-is-positive-zero": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-maxabs": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-maxabs/-/math-base-special-maxabs-0.3.0.tgz", + "integrity": "sha512-SDj+rGD9itZ/YG2hKzhLX4Tf13SNJdOyNsMy1ezjec6Az3xJXKzv2wJAJIteo0KF6jQnEDkI/F6OIF65MY+o0g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-binary": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-max": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-maxabs/node_modules/@stdlib/math-base-napi-binary": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.2.1.tgz", + "integrity": "sha512-ewGarSRaz5gaLsE17yJ4me03e56ICgPAA0ru0SYFCeMK2E5Z4Z2Lbu7HAQTTg+8XhpoaZSw0h2GJopTV7PCKmw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1", + "@stdlib/complex-reim": "^0.2.1", + "@stdlib/complex-reimf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-min": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-min/-/math-base-special-min-0.2.3.tgz", + "integrity": "sha512-tNrKnkcHCRVWzteZJpZ/xql9B6N6EzecnUVizDYqG9y66bOVtI+TADcQ5I/bijEwAIi2BjrIVeq/TBEgQEQBkw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-negative-zero": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-min/node_modules/@stdlib/math-base-napi-binary": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.2.1.tgz", + "integrity": "sha512-ewGarSRaz5gaLsE17yJ4me03e56ICgPAA0ru0SYFCeMK2E5Z4Z2Lbu7HAQTTg+8XhpoaZSw0h2GJopTV7PCKmw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1", + "@stdlib/complex-reim": "^0.2.1", + "@stdlib/complex-reimf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-minabs": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-minabs/-/math-base-special-minabs-0.2.3.tgz", + "integrity": "sha512-IV7PSL09S2GHmsxxtFgebPEwLm/wHnC1e1ulP8Uiuo2zinOiv4NXy2tpf9T+nq95d0ICFMnr9IGxFs6Nd74hRw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-binary": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-min": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-minabs/node_modules/@stdlib/math-base-napi-binary": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-napi-binary/-/math-base-napi-binary-0.2.1.tgz", + "integrity": "sha512-ewGarSRaz5gaLsE17yJ4me03e56ICgPAA0ru0SYFCeMK2E5Z4Z2Lbu7HAQTTg+8XhpoaZSw0h2GJopTV7PCKmw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/complex-float32": "^0.2.1", + "@stdlib/complex-float64": "^0.2.1", + "@stdlib/complex-reim": "^0.2.1", + "@stdlib/complex-reimf": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-pow": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.2.1.tgz", + "integrity": "sha512-7SvgVzDkuilZKrHh4tPiXx9fypF/V7PSvAcUVjvcRj5kVEwv/15RpzlmCJlT9B20VPSx4gJ1S0UIA6xgmYFuAg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.1", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.1", + "@stdlib/constants-float64-ln-two": "^0.2.1", + "@stdlib/constants-float64-ninf": "^0.2.1", + "@stdlib/constants-float64-pinf": "^0.2.1", + "@stdlib/math-base-assert-is-infinite": "^0.2.1", + "@stdlib/math-base-assert-is-integer": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-assert-is-odd": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.1", + "@stdlib/math-base-special-sqrt": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.1", + "@stdlib/number-float64-base-set-high-word": "^0.2.1", + "@stdlib/number-float64-base-set-low-word": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/number-uint32-base-to-int32": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-pow/node_modules/@stdlib/math-base-assert-is-odd": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-assert-is-odd/-/math-base-assert-is-odd-0.2.1.tgz", + "integrity": "sha512-V4qQuCO6/AA5udqlNatMRZ8R/MgpqD8mPIkFrpSZJdpLcGYSz815uAAf3NBOuWXkE2Izw0/Tg/hTQ+YcOW2g5g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-even": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-powm1": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-powm1/-/math-base-special-powm1-0.3.1.tgz", + "integrity": "sha512-Pz7e2JlZH9EktJCDuyFPoT9IxMUSiZiJquyh2xB92NQQi9CAIdyaPUryNo36LxG65bne5GZF47MeiWCE8oWgiA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-expm1": "^0.2.3", + "@stdlib/math-base-special-fmod": "^0.1.0", + "@stdlib/math-base-special-ln": "^0.2.4", + "@stdlib/math-base-special-pow": "^0.3.0", + "@stdlib/math-base-special-trunc": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-powm1/node_modules/@stdlib/math-base-special-pow": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-pow/-/math-base-special-pow-0.3.0.tgz", + "integrity": "sha512-sMDYRUYGFyMXDHcCYy7hE07lV7jgI6rDspLMROKyESWcH4n8j54XE4/0w0i8OpdzR40H895MaPWU/tVnU1tP6w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.2", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.2", + "@stdlib/constants-float64-ln-two": "^0.2.2", + "@stdlib/constants-float64-ninf": "^0.2.2", + "@stdlib/constants-float64-pinf": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-integer": "^0.2.5", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-assert-is-odd": "^0.3.0", + "@stdlib/math-base-napi-binary": "^0.3.0", + "@stdlib/math-base-special-abs": "^0.2.2", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.2", + "@stdlib/math-base-special-sqrt": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-high-word": "^0.2.2", + "@stdlib/number-float64-base-set-low-word": "^0.2.2", + "@stdlib/number-float64-base-to-words": "^0.2.2", + "@stdlib/number-uint32-base-to-int32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-rempio2": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-rempio2/-/math-base-special-rempio2-0.2.1.tgz", + "integrity": "sha512-ErV5EAe3SQCSijg4Pi4Z0sRPOGrODF3jkyCeiLM+iYj2TMOwDaOWQ0xCTME0p9G45TDrbZCLM5arxN83TfzgXQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-base-zeros": "^0.2.1", + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-exponent-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-significand-mask": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.1", + "@stdlib/math-base-special-ldexp": "^0.2.1", + "@stdlib/math-base-special-round": "^0.2.1", + "@stdlib/number-float64-base-from-words": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.1", + "@stdlib/number-float64-base-get-low-word": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-round": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-round/-/math-base-special-round-0.2.1.tgz", + "integrity": "sha512-ibeKiN9z//6wS4H4uaa+vGnh/t1vJtZYXz+NqRVtwoP+nnE/mtL+fIrBlAnkIWVIH+smQPNNo8qsohjyGLBvUQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-signum": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-signum/-/math-base-special-signum-0.2.2.tgz", + "integrity": "sha512-cszwgkfeMTnUiORRWdWv6Q/tpoXkXkMYNMoAFO5qzHTuahnDP37Lkn8fTmCEtgHEasg3Cm69xLbqP0UDuNPHyA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-sin": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-sin/-/math-base-special-sin-0.2.1.tgz", + "integrity": "sha512-IQ6+bzfiZ6/VUn5DIe6iwCsYERE1pwtAOsAWkgNZ1Ih3FzXUxdEOyHtv1zraPrVUb8mR+V5q7OfAGy8TCTnkUg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-high-word-abs-mask": "^0.2.1", + "@stdlib/constants-float64-high-word-exponent-mask": "^0.2.1", + "@stdlib/math-base-special-kernel-cos": "^0.2.1", + "@stdlib/math-base-special-kernel-sin": "^0.2.1", + "@stdlib/math-base-special-rempio2": "^0.2.1", + "@stdlib/number-float64-base-get-high-word": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-sinpi": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-sinpi/-/math-base-special-sinpi-0.2.1.tgz", + "integrity": "sha512-Q3yCp1CoD7gemIILO28bU7iBn8OFiCgXm9vP/9q0tRBxmjtiUnjqbFd+3jRXdAmiCc/B/bPjwGBtVnCnrEMY9g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-pi": "^0.2.1", + "@stdlib/math-base-assert-is-infinite": "^0.2.1", + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/math-base-special-copysign": "^0.2.1", + "@stdlib/math-base-special-cos": "^0.2.1", + "@stdlib/math-base-special-sin": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-sqrt": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-sqrt/-/math-base-special-sqrt-0.2.2.tgz", + "integrity": "sha512-YWxe9vVE5blDbRPDAdZfU03vfGTBHy/8pLDa/qLz7SiJj5n5sVWKObdbMR2oPHF4c6DaZh4IYkrcHFleiY8YkQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-unary": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-special-trunc": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-special-trunc/-/math-base-special-trunc-0.2.2.tgz", + "integrity": "sha512-cvizbo6oFEbdiv7BrtEMODGW+cJcBgyAIleJnIpCf75C722Y/IZgWikWhACSjv4stxGywFubx85B7uvm3vLgwA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-napi-unary": "^0.2.3", + "@stdlib/math-base-special-ceil": "^0.2.1", + "@stdlib/math-base-special-floor": "^0.2.3", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-tools-continued-fraction": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-tools-continued-fraction/-/math-base-tools-continued-fraction-0.2.2.tgz", + "integrity": "sha512-5dm72lTXwSVOsBsOLF57RZqqHehRd9X3HKdQ3WhOoHx7fNc0lxJJEDjtK8gMdV3NvfoER1MBiGbs2h23oaK5qw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-generator-support": "^0.2.2", + "@stdlib/constants-float32-smallest-normal": "^0.2.2", + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-tools-evalpoly": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-tools-evalpoly/-/math-base-tools-evalpoly-0.2.2.tgz", + "integrity": "sha512-vLvfkMkccXZGFiyI3GPf8Ayi6vPEZeHgENnoBDGC+eMIDIoVWmOpVWsjpUz8xtc5xGNsa1hKalSI40IrouHsYA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/function-ctor": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/math-base-tools-sum-series": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/math-base-tools-sum-series/-/math-base-tools-sum-series-0.2.2.tgz", + "integrity": "sha512-P3X+jMONClp93ucJi1Up/x26uwL0kH20CMV9bLzcQyRY8Mceh7jPZuEwzGQR0jq/tJ/4J7AnHg4kdrx4Pd+BNA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-generator-support": "^0.2.2", + "@stdlib/constants-float64-eps": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-ctor": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-ctor/-/number-ctor-0.2.2.tgz", + "integrity": "sha512-98pL4f1uiXVIw9uRV6t4xecMFUYRRTUoctsqDDV8MSRtKEYDzqkWCNz/auupJFJ135L1ejzkejh73fASsgcwKQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float32-base-to-word": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float32-base-to-word/-/number-float32-base-to-word-0.2.2.tgz", + "integrity": "sha512-/I866ocLExPpAjgZnHAjeaBw3ZHg5tVPcRdJoTPEiBG2hwD/OonHdCsfB9lu6FxO6sbp7I9BR1JolCoEyrhmYg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float32": "^0.2.2", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-exponent": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-exponent/-/number-float64-base-exponent-0.2.2.tgz", + "integrity": "sha512-mYivBQKCuu54ulorf5A5rIhFaGPjGvmtkxhvK14q7gzRA80si83dk8buUsLpeeYsakg7yLn10RCVjBEP9/gm7Q==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-exponent-bias": "^0.2.2", + "@stdlib/constants-float64-high-word-exponent-mask": "^0.2.2", + "@stdlib/number-float64-base-get-high-word": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-from-words": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-from-words/-/number-float64-base-from-words-0.2.2.tgz", + "integrity": "sha512-SzMDXSnIDZ8l3PDmtN9TPKTf0mUmh83kKCtj4FisKTcTbcmUmT/ovmrpMTiqdposymjHBieNvGiCz/K03NmlAA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-get-high-word": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-get-high-word/-/number-float64-base-get-high-word-0.2.2.tgz", + "integrity": "sha512-LMNQAHdLZepKOFMRXAXLuq30GInmEdTtR0rO7Ka4F3m7KpYvw84JMyvZByMQHBu+daR6JNr2a/o9aFjmVIe51g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-get-low-word": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-get-low-word/-/number-float64-base-get-low-word-0.2.2.tgz", + "integrity": "sha512-VZjflvoQ9//rZwwuhl7uSLUnnscdIIYmBrHofnBHRjHwdLGUzSd9PM0iagtvI82OHw5QnydBYI4hohBeAAg+aQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-normalize": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-normalize/-/number-float64-base-normalize-0.2.3.tgz", + "integrity": "sha512-HT+3fhYZOEg2JgHBWS/ysc9ZveQZV10weKbtxhLHOsvceQVp1GbThsLik62mU2/3f96S9MgiVfPfSDI3jnBoYw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/constants-float64-smallest-normal": "^0.2.2", + "@stdlib/math-base-assert-is-infinite": "^0.2.2", + "@stdlib/math-base-assert-is-nan": "^0.2.2", + "@stdlib/math-base-special-abs": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-set-high-word": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-set-high-word/-/number-float64-base-set-high-word-0.2.2.tgz", + "integrity": "sha512-bLvH15GJgX5URMaOOJAQgO8/dCJPYUQoXPZH7ecSC3XnnVMfWEf43knkjEGYCnWp4nro5hPRElbtdV4mKEjpUg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-set-low-word": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-set-low-word/-/number-float64-base-set-low-word-0.2.2.tgz", + "integrity": "sha512-E1pGjTwacJ+Tkt5rKQNdwitKnM1iDgMlulYosNdn6CtvU0Pkq359bNhscMscxehdY3MifwuJpuGzDWD2EGUXzQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/number-float64-base-to-words": "^0.2.1", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-to-float32": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-to-float32/-/number-float64-base-to-float32-0.2.2.tgz", + "integrity": "sha512-T5snDkVNZY6pomrSW/qLWQfZ9JHgqCFLi8jaaarfNj2o+5NMUuvvRifLUIacTm8/uC96xB0j3+wKTh1zbIV5ig==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float32": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-float64-base-to-words": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-float64-base-to-words/-/number-float64-base-to-words-0.2.2.tgz", + "integrity": "sha512-nkFHHXoMhb3vcfl7ZvzgiNdqBdBfbKxHtgvDXRxrNQoVmyYbnjljjYD489d2/TAhe+Zvn7qph6QLgTod3zaeww==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/array-float64": "^0.2.1", + "@stdlib/array-uint32": "^0.2.2", + "@stdlib/assert-is-little-endian": "^0.2.1", + "@stdlib/os-byte-order": "^0.2.1", + "@stdlib/os-float-word-order": "^0.2.2", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", + "@stdlib/utils-library-manifest": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/number-uint32-base-to-int32": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/number-uint32-base-to-int32/-/number-uint32-base-to-int32-0.2.2.tgz", + "integrity": "sha512-NPADfdHE/3VEifKDttXM24dRj5YQqxwh2wTRD8fQrpHeaWiMIUo8yDqWrrFNIdLVAcqjL2SwWpo4VJ7oKTYaIA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/object-ctor": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/object-ctor/-/object-ctor-0.2.1.tgz", + "integrity": "sha512-HEIBBpfdQS9Nh5mmIqMk9fzedx6E0tayJrVa2FD7No86rVuq/Ikxq1QP7qNXm+i6z9iNUUS/lZq7BmJESWO/Zg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/os-byte-order": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/os-byte-order/-/os-byte-order-0.2.2.tgz", + "integrity": "sha512-2y6rHAvZo43YmZu9u/E/7cnqZa0hNTLoIiMpV1IxQ/7iv03xZ45Z3xyvWMk0b7bAWwWL7iUknOAAmEchK/kHBA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-big-endian": "^0.2.1", + "@stdlib/assert-is-little-endian": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/os-float-word-order": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/os-float-word-order/-/os-float-word-order-0.2.2.tgz", + "integrity": "sha512-5xpcEuxv/CudKctHS5czKdM7Bj/gC+sm/5R5bRPYyqxANM67t365j3v2v8rmmOxkEp9t0fa8Dggx8VmOkpJXaA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/os-byte-order": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/process-cwd": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/process-cwd/-/process-cwd-0.2.2.tgz", + "integrity": "sha512-8Q/nA/ud5d5PEzzG6ZtKzcOw+RMLm5CWR8Wd+zVO5vcPj+JD7IV7M2lBhbzfUzr63Torrf/vEhT3cob8vUHV/A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/regexp-extended-length-path": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/regexp-extended-length-path/-/regexp-extended-length-path-0.2.2.tgz", + "integrity": "sha512-z3jqauEsaxpsQU3rj1A1QnOgu17pyW5kt+Az8QkoTk7wqNE8HhPikI6k4o7XBHV689rSFWZCl4c4W+7JAiNObQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/regexp-function-name": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/regexp-function-name/-/regexp-function-name-0.2.2.tgz", + "integrity": "sha512-0z/KRsgHJJ3UQkmBeLH+Nin0hXIeA+Fw1T+mnG2V5CHnTA6FKlpxJxWrvwLEsRX7mR/DNtDp06zGyzMFE/4kig==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/stats-base-dists-t-quantile": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@stdlib/stats-base-dists-t-quantile/-/stats-base-dists-t-quantile-0.2.1.tgz", + "integrity": "sha512-59sdJjHsOMd9JlZ/Kdz4Jc/QHESejDRATw/G/zHafMrO6vIhaus9Y5O2PYUzPQx7nR6i5hsXnT8OQwqZ+RoVNQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/math-base-assert-is-nan": "^0.2.1", + "@stdlib/math-base-special-kernel-betaincinv": "^0.1.1", + "@stdlib/math-base-special-signum": "^0.2.1", + "@stdlib/math-base-special-sqrt": "^0.2.1", + "@stdlib/utils-constant-function": "^0.2.1", + "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-base-format-interpolate": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/string-base-format-interpolate/-/string-base-format-interpolate-0.2.2.tgz", + "integrity": "sha512-i9nU9rAB2+o/RR66TS9iQ8x+YzeUDL1SGiAo6GY3hP6Umz5Dx9Qp/v8T69gWVsb4a1YSclz5+YeCWaFgwvPjKA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-base-format-tokenize": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/string-base-format-tokenize/-/string-base-format-tokenize-0.2.2.tgz", + "integrity": "sha512-kXq2015i+LJjqth5dN+hYnvJXBSzRm8w0ABWB5tYAsIuQTpQK+mSo2muM8JBEFEnqUHAwpUsu2qNTK/9o8lsJg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-base-lowercase": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@stdlib/string-base-lowercase/-/string-base-lowercase-0.4.0.tgz", + "integrity": "sha512-IH35Z5e4T+S3b3SfYY39mUhrD2qvJVp4VS7Rn3+jgj4+C3syocuAPsJ8C4OQXWGfblX/N9ymizbpFBCiVvMW8w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-base-replace": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/string-base-replace/-/string-base-replace-0.2.2.tgz", + "integrity": "sha512-Y4jZwRV4Uertw7AlA/lwaYl1HjTefSriN5+ztRcQQyDYmoVN3gzoVKLJ123HPiggZ89vROfC+sk/6AKvly+0CA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-format": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/string-format/-/string-format-0.2.2.tgz", + "integrity": "sha512-GUa50uxgMAtoItsxTbMmwkyhIwrCxCrsjzk3nAbLnt/1Kt1EWOWMwsALqZdD6K4V/xSJ4ns6PZur3W6w+vKk9g==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/string-base-format-interpolate": "^0.2.1", + "@stdlib/string-base-format-tokenize": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/string-replace": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/string-replace/-/string-replace-0.2.2.tgz", + "integrity": "sha512-czNS5IU7sBuHjac45Y3VWUTsUoi82yc8JsMZrOMcjgSrEuDrVmA6sNJg7HC1DuSpdPjm/v9uUk102s1gIfk3Nw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-function": "^0.2.2", + "@stdlib/assert-is-regexp": "^0.2.2", + "@stdlib/assert-is-string": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/string-base-replace": "^0.2.2", + "@stdlib/string-format": "^0.2.2", + "@stdlib/utils-escape-regexp-string": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/symbol-ctor": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/symbol-ctor/-/symbol-ctor-0.2.2.tgz", + "integrity": "sha512-XsmiTfHnTb9jSPf2SoK3O0wrNOXMxqzukvDvtzVur1XBKfim9+seaAS4akmV1H3+AroAXQWVtde885e1B6jz1w==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-constant-function": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-constant-function/-/utils-constant-function-0.2.2.tgz", + "integrity": "sha512-ezRenGy5zU4R0JTfha/bpF8U+Hx0b52AZV++ca/pcaQVvPBRkgCsJacXX0eDbexoBB4+ZZ1vcyIi4RKJ0RRlbQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-constructor-name": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-constructor-name/-/utils-constructor-name-0.2.2.tgz", + "integrity": "sha512-TBtO3MKDAf05ij5ajmyBCbpKKt0Lfahn5tu18gqds4PkFltgcw5tVZfSHY5DZ2HySJQ2GMMYjPW2Kbg6yPCSVg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-buffer": "^0.2.1", + "@stdlib/regexp-function-name": "^0.2.2", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-convert-path": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-convert-path/-/utils-convert-path-0.2.2.tgz", + "integrity": "sha512-8nNuAgt23Np9NssjShUrPK42c6gRTweGuoQw+yTpTfBR9VQv8WFyt048n8gRGUlAHizrdMNpEY9VAb7IBzpVYw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-string": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/regexp-extended-length-path": "^0.2.2", + "@stdlib/string-base-lowercase": "^0.4.0", + "@stdlib/string-format": "^0.2.2", + "@stdlib/string-replace": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-define-nonenumerable-read-only-property": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-define-nonenumerable-read-only-property/-/utils-define-nonenumerable-read-only-property-0.2.2.tgz", + "integrity": "sha512-V3mpAesJemLYDKG376CsmoczWPE/4LKsp8xBvUxCt5CLNAx3J/1W39iZQyA5q6nY1RStGinGn1/dYZwa8ig0Uw==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-define-property": "^0.2.3" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-define-property": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@stdlib/utils-define-property/-/utils-define-property-0.2.4.tgz", + "integrity": "sha512-XlMdz7xwuw/sqXc9LbsV8XunCzZXjbZPC+OAdf4t4PBw4ZRwGzlTI6WED+f4PYR5Tp9F1cHgLPyMYCIBfA2zRg==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", + "@stdlib/string-format": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-escape-regexp-string": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-escape-regexp-string/-/utils-escape-regexp-string-0.2.2.tgz", + "integrity": "sha512-areCibzgpmvm6pGKBg+mXkSDJW4NxtS5jcAT7RtunGMdAYhA/I5whISMPaeJkIT2XhjjFkjKBaIs5pF6aPr4fQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-string": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/string-format": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-eval": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-eval/-/utils-eval-0.2.2.tgz", + "integrity": "sha512-MaFpWZh3fGcTjUeozju5faXqH8w4MRVfpO/M5pon3osTM0by8zrKiI5D9oWqNVygb9JBd+etE+4tj2L1nr5j2A==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-get-prototype-of": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-get-prototype-of/-/utils-get-prototype-of-0.2.2.tgz", + "integrity": "sha512-eDb1BAvt7GW/jduBkfuQrUsA9p09mV8RW20g0DWPaxci6ORYg/UB0tdbAA23aZz2QUoxdYY5s/UJxlq/GHwoKQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-function": "^0.2.1", + "@stdlib/object-ctor": "^0.2.1", + "@stdlib/utils-native-class": "^0.2.1" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-global": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-global/-/utils-global-0.2.2.tgz", + "integrity": "sha512-A4E8VFHn+1bpfJ4PA8H2b62CMQpjv2A+H3QDEBrouLFWne0wrx0TNq8vH6VYHxx9ZRxhgWQjfHiSAxtUJobrbQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-is-boolean": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", + "@stdlib/string-format": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-library-manifest": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-library-manifest/-/utils-library-manifest-0.2.2.tgz", + "integrity": "sha512-YqzVLuBsB4wTqzdUtRArAjBJoT3x61iop2jFChXexhl6ejV3vDpHcukEEkqIOcJKut+1cG5TLJdexgHNt1C0NA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/fs-resolve-parent-path": "^0.2.1", + "@stdlib/utils-convert-path": "^0.2.1", + "debug": "^2.6.9", + "resolve": "^1.1.7" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-native-class": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-native-class/-/utils-native-class-0.2.2.tgz", + "integrity": "sha512-cSn/FozbEpfR/FlJAoceQtZ8yUJFhZ8RFkbEsxW/7+H4o09yln3lBS0HSfUJISYNtpTNN/2/Fup88vmvwspvwA==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/assert-has-own-property": "^0.2.1", + "@stdlib/assert-has-tostringtag-support": "^0.2.2", + "@stdlib/symbol-ctor": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@stdlib/utils-type-of": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@stdlib/utils-type-of/-/utils-type-of-0.2.2.tgz", + "integrity": "sha512-RLGFxPNgY9AtVVrFGdKO6Y3pOd/Ov2WA4O2/czZN/AbgYzbPdoF0KkfvHRIney6k+TtvoyYB8YqZXJ4G88f9BQ==", + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "dependencies": { + "@stdlib/utils-constructor-name": "^0.2.1", + "@stdlib/utils-global": "^0.2.2" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/stdlib" + } + }, + "node_modules/@tootallnate/quickjs-emscripten": { + "version": "0.23.0", + "resolved": "https://registry.npmjs.org/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz", + "integrity": "sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==" + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", + "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==", + "dev": true + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", + "dev": true + }, + "node_modules/@turf/boolean-clockwise": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/boolean-clockwise/-/boolean-clockwise-6.5.0.tgz", + "integrity": "sha512-45+C7LC5RMbRWrxh3Z0Eihsc8db1VGBO5d9BLTOAwU4jR6SgsunTfRWR16X7JUwIDYlCVEmnjcXJNi/kIU3VIw==", + "dependencies": { + "@turf/helpers": "^6.5.0", + "@turf/invariant": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/clone": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/clone/-/clone-6.5.0.tgz", + "integrity": "sha512-mzVtTFj/QycXOn6ig+annKrM6ZlimreKYz6f/GSERytOpgzodbQyOgkfwru100O1KQhhjSudKK4DsQ0oyi9cTw==", + "dependencies": { + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/flatten": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/flatten/-/flatten-6.5.0.tgz", + "integrity": "sha512-IBZVwoNLVNT6U/bcUUllubgElzpMsNoCw8tLqBw6dfYg9ObGmpEjf9BIYLr7a2Yn5ZR4l7YIj2T7kD5uJjZADQ==", + "dependencies": { + "@turf/helpers": "^6.5.0", + "@turf/meta": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/helpers": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.5.0.tgz", + "integrity": "sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==", + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/invariant": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/invariant/-/invariant-6.5.0.tgz", + "integrity": "sha512-Wv8PRNCtPD31UVbdJE/KVAWKe7l6US+lJItRR/HOEW3eh+U/JwRCSUl/KZ7bmjM/C+zLNoreM2TU6OoLACs4eg==", + "dependencies": { + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/meta": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-6.5.0.tgz", + "integrity": "sha512-RrArvtsV0vdsCBegoBtOalgdSOfkBrTJ07VkpiCnq/491W67hnMWmDu7e6Ztw0C3WldRYTXkg3SumfdzZxLBHA==", + "dependencies": { + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/rewind": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/rewind/-/rewind-6.5.0.tgz", + "integrity": "sha512-IoUAMcHWotBWYwSYuYypw/LlqZmO+wcBpn8ysrBNbazkFNkLf3btSDZMkKJO/bvOzl55imr/Xj4fi3DdsLsbzQ==", + "dependencies": { + "@turf/boolean-clockwise": "^6.5.0", + "@turf/clone": "^6.5.0", + "@turf/helpers": "^6.5.0", + "@turf/invariant": "^6.5.0", + "@turf/meta": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@types/node": { + "version": "22.13.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.17.tgz", + "integrity": "sha512-nAJuQXoyPj04uLgu+obZcSmsfOenUg6DxPKogeUy6yNCFwWaj5sBF8/G/pNo8EtBJjAfSVgfIlugR/BCOleO+g==", + "devOptional": true, + "dependencies": { + "undici-types": "~6.20.0" + } + }, + "node_modules/@types/yauzl": { + "version": "2.10.3", + "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.3.tgz", + "integrity": "sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q==", + "optional": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@visactor/calculator": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@visactor/calculator/-/calculator-2.0.5.tgz", + "integrity": "sha512-/NBDB/wBQLeQuSspDBuiEAbbyfJS/xPX6mubVsLGhfy65UwUBojAQgmX25FcRJnUsRXooK5heshni19DBBf8xA==", + "dependencies": { + "@visactor/vutils": "~0.19.3", + "node-sql-parser": "~4.17.0", + "ts-pattern": "~4.1.4" + } + }, + "node_modules/@visactor/chart-advisor": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@visactor/chart-advisor/-/chart-advisor-2.0.5.tgz", + "integrity": "sha512-pvHceRlworB7kDSmbWXUtherLLXh5nMj0aEGuxtzKQyHmeO0sjuu9gGXBFIgscGliSZM4tmeNrFU9eBLGJ8dxw==", + "dependencies": { + "@visactor/vutils": "~0.19.3" + } + }, + "node_modules/@visactor/vchart": { + "version": "1.13.8", + "resolved": "https://registry.npmjs.org/@visactor/vchart/-/vchart-1.13.8.tgz", + "integrity": "sha512-g8GacKxDvxUiuT4kW83u5vrAoAvpJ0+yca4IUYvdSTxdYXzipJEgiNmSnwd46GP8eBKJj36ZmqzEhLVzPJ+/Pw==", + "dependencies": { + "@visactor/vdataset": "~0.19.4", + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-hierarchy": "0.16.3", + "@visactor/vgrammar-projection": "0.16.3", + "@visactor/vgrammar-sankey": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vgrammar-venn": "0.16.3", + "@visactor/vgrammar-wordcloud": "0.16.3", + "@visactor/vgrammar-wordcloud-shape": "0.16.3", + "@visactor/vrender-components": "0.22.6", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vscale": "~0.19.4", + "@visactor/vutils": "~0.19.4", + "@visactor/vutils-extension": "1.13.8" + } + }, + "node_modules/@visactor/vchart-theme": { + "version": "1.12.2", + "resolved": "https://registry.npmjs.org/@visactor/vchart-theme/-/vchart-theme-1.12.2.tgz", + "integrity": "sha512-r298TUdK+CKbHGVYWgQnNSEB5uqpFvF2/aMNZ/2POQnd2CovAPJOx2nTE6hAcOn8rra2FwJ2xF8AyP1O5OhrTw==", + "peerDependencies": { + "@visactor/vchart": ">=1.10.4" + } + }, + "node_modules/@visactor/vdataset": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@visactor/vdataset/-/vdataset-0.19.4.tgz", + "integrity": "sha512-xxglcFtvho5jWiQPKwTolKXbNOG8f77CrK7TJhfiqNlzoe27qO8B+A6lUKlLMt1kZaCH7ZNrFFkHyPjnnZ/gng==", + "dependencies": { + "@turf/flatten": "^6.5.0", + "@turf/helpers": "^6.5.0", + "@turf/rewind": "^6.5.0", + "@visactor/vutils": "0.19.4", + "d3-dsv": "^2.0.0", + "d3-geo": "^1.12.1", + "d3-hexbin": "^0.2.2", + "d3-hierarchy": "^3.1.1", + "eventemitter3": "^4.0.7", + "geobuf": "^3.0.1", + "geojson-dissolve": "^3.1.0", + "path-browserify": "^1.0.1", + "pbf": "^3.2.1", + "point-at-length": "^1.1.0", + "simple-statistics": "^7.7.3", + "simplify-geojson": "^1.0.4", + "topojson-client": "^3.1.0" + } + }, + "node_modules/@visactor/vgrammar-coordinate": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-coordinate/-/vgrammar-coordinate-0.16.3.tgz", + "integrity": "sha512-tfDSi3WgY/GWDvbf67eus4a7jR74y7OMod3JrTqyDVzSNZUOgUtS3ieEM71f9yipxjY8gxo53GPDpH/advxUZw==", + "dependencies": { + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-core": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-core/-/vgrammar-core-0.16.3.tgz", + "integrity": "sha512-cd7hmh9JobbCDUJPOshmQB5V0KVM0GLTPBe/ZySJDi1cUSWpukAgRrLozEk/M5XgDbVIT+4pjqe6siacCad8dg==", + "dependencies": { + "@visactor/vdataset": "~0.19.4", + "@visactor/vgrammar-coordinate": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-components": "0.22.6", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vscale": "~0.19.4", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-hierarchy": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-hierarchy/-/vgrammar-hierarchy-0.16.3.tgz", + "integrity": "sha512-qnfSWRt1PErkVPTtet8DVc4MY+WwmgJoNNW2FALFht1qUfPdglTqT96drPbkurwiZMzSk+Xfr7+IPUA8ZQwWag==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-projection": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-projection/-/vgrammar-projection-0.16.3.tgz", + "integrity": "sha512-c+MJ3qgtsNQHwZCDBVT7fNahNxe0g827IiytQWvrtMxavLIrtJqeul5H+6BYrGvYk8d81ByxNZdoVNn/mfNtDw==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vutils": "~0.19.4", + "d3-geo": "^1.12.1" + } + }, + "node_modules/@visactor/vgrammar-sankey": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-sankey/-/vgrammar-sankey-0.16.3.tgz", + "integrity": "sha512-7j0xx77Yn2KzY4EcZ27qFF6R1KTcmy3BtQQewOHA1uoUX8ZRsfe57eziYRiBhyVrzdFWoa0IJqzH7Yk/zITvuQ==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-util": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-util/-/vgrammar-util-0.16.3.tgz", + "integrity": "sha512-aF9MqjTR7YvBAVDtp1A/CDVcXFGlO+TxkHVPEQVrn7cVu2DGRXCZnu/iQ+AUhttVYaWlSRflZj4cnQrKS4zy4g==", + "dependencies": { + "@visactor/vrender-core": "0.22.6", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-venn": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-venn/-/vgrammar-venn-0.16.3.tgz", + "integrity": "sha512-M6mtCrpOcPrD6nkQFZ3Fl0Z2zPaKFTyRIPeO235vDwB/ZzefN5BObh85UGsv0swK46L5yu3daBxW0VtrGMBZRA==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-wordcloud": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-wordcloud/-/vgrammar-wordcloud-0.16.3.tgz", + "integrity": "sha512-uIHUJ3CGir+IjDjv4SpJR5SZvWSIYU2VoBdoCvFdhP9j8t15wadGYfe0/br9d6xOM3laiSCFYvPdhy0Ke5sP4w==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vgrammar-wordcloud-shape": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@visactor/vgrammar-wordcloud-shape/-/vgrammar-wordcloud-shape-0.16.3.tgz", + "integrity": "sha512-ZWRHbec4WM2W3v2t57gRaX1IUGy+nDRjumcctgzSvmCpmR3nORgLKmMhxXYEA0VwcpY+umM0lVcd42iqPH8c7g==", + "dependencies": { + "@visactor/vgrammar-core": "0.16.3", + "@visactor/vgrammar-util": "0.16.3", + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vscale": "~0.19.4", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vmind": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@visactor/vmind/-/vmind-2.0.5.tgz", + "integrity": "sha512-QztQaeSkdeRZYOUlB4qaBpx3/swyO3JzFH8eYvSgvptS/rf8aQDZiufAUasafDLkcME5N6RpBGkcGYIDkmt74Q==", + "dependencies": { + "@stdlib/stats-base-dists-t-quantile": "0.2.1", + "@visactor/calculator": "2.0.5", + "@visactor/chart-advisor": "2.0.5", + "@visactor/vchart-theme": "^1.11.2", + "@visactor/vdataset": "~0.19.3", + "@visactor/vutils": "~0.19.3", + "alasql": "~4.3.2", + "array-normalize": "~2.0.0", + "axios": "^1.4.0", + "bayesian-changepoint": "~1.0.1", + "dayjs": "~1.11.10", + "density-clustering": "~1.3.0", + "euclidean-distance": "~1.0.0", + "js-yaml": "~4.1.0", + "json5": "~2.2.3", + "jsonrepair": "~3.8.1", + "jstat": "~1.9.6", + "string-similarity-js": "~2.1.4" + } + }, + "node_modules/@visactor/vrender-components": { + "version": "0.22.6", + "resolved": "https://registry.npmjs.org/@visactor/vrender-components/-/vrender-components-0.22.6.tgz", + "integrity": "sha512-YHLjA2GzP5LQxAAgzo2iniBxDldy9GtEzjm/sCXrrOGzzwMFlyhAeXCbUVEIMhTTfpRdK5LocAP1PSJsv4BObA==", + "dependencies": { + "@visactor/vrender-core": "0.22.6", + "@visactor/vrender-kits": "0.22.6", + "@visactor/vscale": "~0.19.4", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/@visactor/vrender-core": { + "version": "0.22.6", + "resolved": "https://registry.npmjs.org/@visactor/vrender-core/-/vrender-core-0.22.6.tgz", + "integrity": "sha512-R/MPjAuF9vT5atn7tAqhA5K1FMqYzv2SOhREsJpgP6QbJSnGR2uMTrNENRFvrM81ikR6yeh7WeTx6Fh2av+M4A==", + "dependencies": { + "@visactor/vutils": "~0.19.4", + "color-convert": "2.0.1" + } + }, + "node_modules/@visactor/vrender-kits": { + "version": "0.22.6", + "resolved": "https://registry.npmjs.org/@visactor/vrender-kits/-/vrender-kits-0.22.6.tgz", + "integrity": "sha512-0yRvhMhnT3JeFKCOi8riubkuKjNMIlzcW1FQV+kIyOGGV6nCSjvFL4+XDuEGalHlHt76BSlM1/cmxnmRNTHCRQ==", + "dependencies": { + "@resvg/resvg-js": "2.4.1", + "@visactor/vrender-core": "0.22.6", + "@visactor/vutils": "~0.19.4", + "gifuct-js": "2.1.2", + "lottie-web": "^5.12.2", + "roughjs": "4.5.2" + } + }, + "node_modules/@visactor/vscale": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@visactor/vscale/-/vscale-0.19.4.tgz", + "integrity": "sha512-kp69hPMof3GBKRuUiXSR9+9K+Z8ZXsTlOAwcnknXmiiZDhdcDkPlv27/d+Xx1Wi/iqw+BS2S7YIjHmfzdiVQ/Q==", + "dependencies": { + "@visactor/vutils": "0.19.4" + } + }, + "node_modules/@visactor/vutils": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@visactor/vutils/-/vutils-0.19.4.tgz", + "integrity": "sha512-kLbcsTe1/3HSSvEJvJikzGD0plY0gdHbpxt98oo7W6OrianfYd97nm/w7rFXcq/S49e6C5d1SdU4MZk/PYxhEQ==", + "dependencies": { + "@turf/helpers": "^6.5.0", + "@turf/invariant": "^6.5.0", + "eventemitter3": "^4.0.7" + } + }, + "node_modules/@visactor/vutils-extension": { + "version": "1.13.8", + "resolved": "https://registry.npmjs.org/@visactor/vutils-extension/-/vutils-extension-1.13.8.tgz", + "integrity": "sha512-mOtUJjUEthQTHyYnynWJs8wbbW+UoW0z18lH++TqGoDbsJLcr4Mlpxhe8IDP/bda7kRVTI/FHbzVHhKWKLBvxw==", + "dependencies": { + "@visactor/vdataset": "~0.19.4", + "@visactor/vutils": "~0.19.4" + } + }, + "node_modules/abs-svg-path": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/abs-svg-path/-/abs-svg-path-0.1.1.tgz", + "integrity": "sha512-d8XPSGjfyzlXC3Xx891DJRyZfqk5JU0BJrDQcsWomFIV1/BIzPW5HDH5iDdWpqWaav0YVIEzT1RHTwWr0FFshA==" + }, + "node_modules/acorn": { + "version": "8.14.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.1.tgz", + "integrity": "sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg==", + "dev": true, + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.3.4", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.4.tgz", + "integrity": "sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g==", + "dev": true, + "dependencies": { + "acorn": "^8.11.0" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/alasql": { + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/alasql/-/alasql-4.3.3.tgz", + "integrity": "sha512-IP64TOG+zBTPA41OB2NJVkM3urEIhvZtYwtPFC/1QSH7nCzwShIwWfxwyOhTK7yzF/ZaNGEpc3Eexyzb2nUbFg==", + "dependencies": { + "cross-fetch": "4", + "yargs": "16" + }, + "bin": { + "alasql": "bin/alasql-cli.js" + }, + "engines": { + "node": ">=15" + } + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, + "node_modules/array-bounds": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/array-bounds/-/array-bounds-1.0.1.tgz", + "integrity": "sha512-8wdW3ZGk6UjMPJx/glyEt0sLzzwAE1bhToPsO1W2pbpR2gULyxe3BjSiuJFheP50T/GgODVPz2fuMUmIywt8cQ==" + }, + "node_modules/array-normalize": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/array-normalize/-/array-normalize-2.0.0.tgz", + "integrity": "sha512-WofPolGg9OqpmfYh2qqOJ0yeJ9Idjn+EcQ+Nyy3eQbqtuz0MRyqTEHB0PH/Ypp2PpsOAfjsqTMzu1fHOaPzO1Q==", + "dependencies": { + "array-bounds": "^1.0.0" + } + }, + "node_modules/array-source": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/array-source/-/array-source-0.0.4.tgz", + "integrity": "sha512-frNdc+zBn80vipY+GdcJkLEbMWj3xmzArYApmUGxoiV8uAu/ygcs9icPdsGdA26h0MkHUMW6EN2piIvVx+M5Mw==" + }, + "node_modules/ast-types": { + "version": "0.13.4", + "resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.13.4.tgz", + "integrity": "sha512-x1FCFnFifvYDDzTaLII71vG5uvDwgtmDTEVWAxrgeiR8VjMONcCXJx7E+USjDtHlwFmt9MysbqgF9b9Vjr6w+w==", + "dependencies": { + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.8.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.8.4.tgz", + "integrity": "sha512-eBSYY4Y68NNlHbHBMdeDmKNtDgXWhQsJcGqzO3iLUM0GraQFSS9cVgPX5I9b3lbdFKyYoAEGAZF1DwhTaljNAw==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/b4a": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.7.tgz", + "integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==" + }, + "node_modules/bare-events": { + "version": "2.5.4", + "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.5.4.tgz", + "integrity": "sha512-+gFfDkR8pj4/TrWCGUGWmJIkBwuxPS5F+a5yWjOHQt2hHvNZd5YLzadjmDUtFmMM4y429bnKLa8bYBMHcYdnQA==", + "optional": true + }, + "node_modules/bare-fs": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/bare-fs/-/bare-fs-4.1.5.tgz", + "integrity": "sha512-1zccWBMypln0jEE05LzZt+V/8y8AQsQQqxtklqaIyg5nu6OAYFhZxPXinJTSG+kU5qyNmeLgcn9AW7eHiCHVLA==", + "optional": true, + "dependencies": { + "bare-events": "^2.5.4", + "bare-path": "^3.0.0", + "bare-stream": "^2.6.4" + }, + "engines": { + "bare": ">=1.16.0" + }, + "peerDependencies": { + "bare-buffer": "*" + }, + "peerDependenciesMeta": { + "bare-buffer": { + "optional": true + } + } + }, + "node_modules/bare-os": { + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.6.1.tgz", + "integrity": "sha512-uaIjxokhFidJP+bmmvKSgiMzj2sV5GPHaZVAIktcxcpCyBFFWO+YlikVAdhmUo2vYFvFhOXIAlldqV29L8126g==", + "optional": true, + "engines": { + "bare": ">=1.14.0" + } + }, + "node_modules/bare-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bare-path/-/bare-path-3.0.0.tgz", + "integrity": "sha512-tyfW2cQcB5NN8Saijrhqn0Zh7AnFNsnczRcuWODH0eYAXBsJ5gVxAUuNr7tsHSC6IZ77cA0SitzT+s47kot8Mw==", + "optional": true, + "dependencies": { + "bare-os": "^3.0.1" + } + }, + "node_modules/bare-stream": { + "version": "2.6.5", + "resolved": "https://registry.npmjs.org/bare-stream/-/bare-stream-2.6.5.tgz", + "integrity": "sha512-jSmxKJNJmHySi6hC42zlZnq00rga4jjxcgNZjY9N5WlOe/iOoGRtdwGsHzQv2RlH2KOYMwGUXhf2zXd32BA9RA==", + "optional": true, + "dependencies": { + "streamx": "^2.21.0" + }, + "peerDependencies": { + "bare-buffer": "*", + "bare-events": "*" + }, + "peerDependenciesMeta": { + "bare-buffer": { + "optional": true + }, + "bare-events": { + "optional": true + } + } + }, + "node_modules/basic-ftp": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/basic-ftp/-/basic-ftp-5.0.5.tgz", + "integrity": "sha512-4Bcg1P8xhUuqcii/S0Z9wiHIrQVPMermM1any+MX5GeGD7faD3/msQUDGLol9wOcz4/jbg/WJnGqoJF6LiBdtg==", + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/bayesian-changepoint": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/bayesian-changepoint/-/bayesian-changepoint-1.0.1.tgz", + "integrity": "sha512-OhSHWfGiEcBtI46b5guJGmj6pJEjvyaXsRPCAQy5MPoVaDZ38poXmzVZLSIuw6VLQmZs58+uf5F9iFA4NVmTTA==" + }, + "node_modules/big-integer": { + "version": "1.6.52", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", + "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", + "engines": { + "node": "*" + } + }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/callsites": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", + "integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==", + "engines": { + "node": ">=6" + } + }, + "node_modules/chromium-bidi": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-5.1.0.tgz", + "integrity": "sha512-9MSRhWRVoRPDG0TgzkHrshFSJJNZzfY5UFqUMuksg7zL1yoZIZ3jLB0YAgHclbiAxPI86pBnwDX1tbzoiV8aFw==", + "dependencies": { + "mitt": "^3.0.1", + "zod": "^3.24.1" + }, + "peerDependencies": { + "devtools-protocol": "*" + } + }, + "node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + }, + "node_modules/concat-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-2.0.0.tgz", + "integrity": "sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==", + "engines": [ + "node >= 6.0" + ], + "dependencies": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.0.2", + "typedarray": "^0.0.6" + } + }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "node_modules/cosmiconfig": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-9.0.0.tgz", + "integrity": "sha512-itvL5h8RETACmOTFc4UfIyB2RfEHi71Ax6E/PivVxq9NseKbOWpeyHEOIbmAw1rs8Ak0VursQNww7lf7YtUwzg==", + "dependencies": { + "env-paths": "^2.2.1", + "import-fresh": "^3.3.0", + "js-yaml": "^4.1.0", + "parse-json": "^5.2.0" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/d-fischer" + }, + "peerDependencies": { + "typescript": ">=4.9.5" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, + "node_modules/cross-fetch": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-4.1.0.tgz", + "integrity": "sha512-uKm5PU+MHTootlWEY+mZ4vvXoCn4fLQxT9dSc1sXVMSFkINTJVN8cAQROpwcKm8bJ/c7rgZVIBWzH5T78sNZZw==", + "dependencies": { + "node-fetch": "^2.7.0" + } + }, + "node_modules/d3-array": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-1.2.4.tgz", + "integrity": "sha512-KHW6M86R+FUPYGb3R5XiYjXPq7VzwxZ22buHhAEVG5ztoEcZZMLov530mmccaqA1GghZArjQV46fuc8kUqhhHw==" + }, + "node_modules/d3-dsv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/d3-dsv/-/d3-dsv-2.0.0.tgz", + "integrity": "sha512-E+Pn8UJYx9mViuIUkoc93gJGGYut6mSDKy2+XaPwccwkRGlR+LO97L2VCCRjQivTwLHkSnAJG7yo00BWY6QM+w==", + "dependencies": { + "commander": "2", + "iconv-lite": "0.4", + "rw": "1" + }, + "bin": { + "csv2json": "bin/dsv2json", + "csv2tsv": "bin/dsv2dsv", + "dsv2dsv": "bin/dsv2dsv", + "dsv2json": "bin/dsv2json", + "json2csv": "bin/json2dsv", + "json2dsv": "bin/json2dsv", + "json2tsv": "bin/json2dsv", + "tsv2csv": "bin/dsv2dsv", + "tsv2json": "bin/dsv2json" + } + }, + "node_modules/d3-geo": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/d3-geo/-/d3-geo-1.12.1.tgz", + "integrity": "sha512-XG4d1c/UJSEX9NfU02KwBL6BYPj8YKHxgBEw5om2ZnTRSbIcego6dhHwcxuSR3clxh0EpE38os1DVPOmnYtTPg==", + "dependencies": { + "d3-array": "1" + } + }, + "node_modules/d3-hexbin": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/d3-hexbin/-/d3-hexbin-0.2.2.tgz", + "integrity": "sha512-KS3fUT2ReD4RlGCjvCEm1RgMtp2NFZumdMu4DBzQK8AZv3fXRM6Xm8I4fSU07UXvH4xxg03NwWKWdvxfS/yc4w==" + }, + "node_modules/d3-hierarchy": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz", + "integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-6.0.2.tgz", + "integrity": "sha512-7hvf7/GW8e86rW0ptuwS3OcBGDjIi6SZva7hCyWC0yYry2cOPmLIjXAUHI6DK2HsnwJd9ifmt57i8eV2n4YNpw==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/dayjs": { + "version": "1.11.13", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.13.tgz", + "integrity": "sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==" + }, + "node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/degenerator": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/degenerator/-/degenerator-5.0.1.tgz", + "integrity": "sha512-TllpMR/t0M5sqCXfj85i4XaAzxmS5tVA16dqvdkMwGmzI+dXLXnw3J+3Vdv7VKw+ThlTMboK6i9rnZ6Nntj5CQ==", + "dependencies": { + "ast-types": "^0.13.4", + "escodegen": "^2.1.0", + "esprima": "^4.0.1" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/density-clustering": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/density-clustering/-/density-clustering-1.3.0.tgz", + "integrity": "sha512-icpmBubVTwLnsaor9qH/4tG5+7+f61VcqMN3V3pm9sxxSCt2Jcs0zWOgwZW9ARJYaKD3FumIgHiMOcIMRRAzFQ==" + }, + "node_modules/devtools-protocol": { + "version": "0.0.1439962", + "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1439962.tgz", + "integrity": "sha512-jJF48UdryzKiWhJ1bLKr7BFWUQCEIT5uCNbDLqkQJBtkFxYzILJH44WN0PDKMIlGDN7Utb8vyUY85C3w4R/t2g==" + }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" + }, + "node_modules/end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "dependencies": { + "once": "^1.4.0" + } + }, + "node_modules/env-paths": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", + "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==", + "engines": { + "node": ">=6" + } + }, + "node_modules/error-ex": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", + "integrity": "sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g==", + "dependencies": { + "is-arrayish": "^0.2.1" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escalade": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", + "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", + "engines": { + "node": ">=6" + } + }, + "node_modules/escodegen": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/escodegen/-/escodegen-2.1.0.tgz", + "integrity": "sha512-2NlIDTwUWJN0mRPQOdtQBzbUHvdGY2P1VXSyU83Q3xKxM7WHX2Ql8dKq782Q9TgQUNOLEzEYu9bzLNj1q88I5w==", + "dependencies": { + "esprima": "^4.0.1", + "estraverse": "^5.2.0", + "esutils": "^2.0.2" + }, + "bin": { + "escodegen": "bin/escodegen.js", + "esgenerate": "bin/esgenerate.js" + }, + "engines": { + "node": ">=6.0" + }, + "optionalDependencies": { + "source-map": "~0.6.1" + } + }, + "node_modules/esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "bin": { + "esparse": "bin/esparse.js", + "esvalidate": "bin/esvalidate.js" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/estraverse": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.3.0.tgz", + "integrity": "sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==", + "engines": { + "node": ">=4.0" + } + }, + "node_modules/esutils": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/euclidean-distance": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/euclidean-distance/-/euclidean-distance-1.0.0.tgz", + "integrity": "sha512-3+1fOi9GKT2PhSX+uKZ/cX4F98wLY2gTibZPPZeToEPvHZNLnnoymcJgQzWeeIMvqciQRIhn9KEKY7QVplC7hQ==" + }, + "node_modules/eventemitter3": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", + "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" + }, + "node_modules/extract-zip": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz", + "integrity": "sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==", + "dependencies": { + "debug": "^4.1.1", + "get-stream": "^5.1.0", + "yauzl": "^2.10.0" + }, + "bin": { + "extract-zip": "cli.js" + }, + "engines": { + "node": ">= 10.17.0" + }, + "optionalDependencies": { + "@types/yauzl": "^2.9.1" + } + }, + "node_modules/extract-zip/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/extract-zip/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/fast-fifo": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz", + "integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==" + }, + "node_modules/fd-slicer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", + "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", + "dependencies": { + "pend": "~1.2.0" + } + }, + "node_modules/file-source": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/file-source/-/file-source-0.6.1.tgz", + "integrity": "sha512-1R1KneL7eTXmXfKxC10V/9NeGOdbsAXJ+lQ//fvvcHUgtaZcZDWNJNblxAoVOyV1cj45pOtUrR3vZTBwqcW8XA==", + "dependencies": { + "stream-source": "0.3" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.2.tgz", + "integrity": "sha512-hGfm/slu0ZabnNt4oaRZ6uREyfCj6P4fT/n6A1rGV+Z0VdGXjfOhVUpkn6qVQONHGIFwmveGXyDs75+nr6FM8w==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/geobuf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/geobuf/-/geobuf-3.0.2.tgz", + "integrity": "sha512-ASgKwEAQQRnyNFHNvpd5uAwstbVYmiTW0Caw3fBb509tNTqXyAAPMyFs5NNihsLZhLxU1j/kjFhkhLWA9djuVg==", + "dependencies": { + "concat-stream": "^2.0.0", + "pbf": "^3.2.1", + "shapefile": "~0.6.6" + }, + "bin": { + "geobuf2json": "bin/geobuf2json", + "json2geobuf": "bin/json2geobuf", + "shp2geobuf": "bin/shp2geobuf" + } + }, + "node_modules/geojson-dissolve": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/geojson-dissolve/-/geojson-dissolve-3.1.0.tgz", + "integrity": "sha512-JXHfn+A3tU392HA703gJbjmuHaQOAE/C1KzbELCczFRFux+GdY6zt1nKb1VMBHp4LWeE7gUY2ql+g06vJqhiwQ==", + "dependencies": { + "@turf/meta": "^3.7.5", + "geojson-flatten": "^0.2.1", + "geojson-linestring-dissolve": "0.0.1", + "topojson-client": "^3.0.0", + "topojson-server": "^3.0.0" + } + }, + "node_modules/geojson-dissolve/node_modules/@turf/meta": { + "version": "3.14.0", + "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-3.14.0.tgz", + "integrity": "sha512-OtXqLQuR9hlQ/HkAF/OdzRea7E0eZK1ay8y8CBXkoO2R6v34CsDrWYLMSo0ZzMsaQDpKo76NPP2GGo+PyG1cSg==" + }, + "node_modules/geojson-flatten": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/geojson-flatten/-/geojson-flatten-0.2.4.tgz", + "integrity": "sha512-LiX6Jmot8adiIdZ/fthbcKKPOfWjTQchX/ggHnwMZ2e4b0I243N1ANUos0LvnzepTEsj0+D4fIJ5bKhBrWnAHA==", + "dependencies": { + "get-stdin": "^6.0.0", + "minimist": "1.2.0" + }, + "bin": { + "geojson-flatten": "geojson-flatten" + } + }, + "node_modules/geojson-flatten/node_modules/get-stdin": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz", + "integrity": "sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g==", + "engines": { + "node": ">=4" + } + }, + "node_modules/geojson-linestring-dissolve": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/geojson-linestring-dissolve/-/geojson-linestring-dissolve-0.0.1.tgz", + "integrity": "sha512-Y8I2/Ea28R/Xeki7msBcpMvJL2TaPfaPKP8xqueJfQ9/jEhps+iOJxOR2XCBGgVb12Z6XnDb1CMbaPfLepsLaw==" + }, + "node_modules/get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "engines": { + "node": "6.* || 8.* || >= 10.*" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/get-stdin": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-9.0.0.tgz", + "integrity": "sha512-dVKBjfWisLAicarI2Sf+JuBE/DghV4UzNAVe9yhEJuzeREd3JhOTE9cUaJTeSa77fsbQUK3pcOpJfM59+VKZaA==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/get-stream": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz", + "integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==", + "dependencies": { + "pump": "^3.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/get-uri": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/get-uri/-/get-uri-6.0.4.tgz", + "integrity": "sha512-E1b1lFFLvLgak2whF2xDBcOy6NLVGZBqqjJjsIhvopKfWWEi64pLVTWWehV8KlLerZkfNTA95sTe2OdJKm1OzQ==", + "dependencies": { + "basic-ftp": "^5.0.2", + "data-uri-to-buffer": "^6.0.2", + "debug": "^4.3.4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/get-uri/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/get-uri/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/gifuct-js": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/gifuct-js/-/gifuct-js-2.1.2.tgz", + "integrity": "sha512-rI2asw77u0mGgwhV3qA+OEgYqaDn5UNqgs+Bx0FGwSpuqfYn+Ir6RQY5ENNQ8SbIiG/m5gVa7CD5RriO4f4Lsg==", + "dependencies": { + "js-binary-schema-parser": "^2.0.3" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-proxy-agent": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz", + "integrity": "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==", + "dependencies": { + "agent-base": "^7.1.0", + "debug": "^4.3.4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/http-proxy-agent/node_modules/agent-base": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.3.tgz", + "integrity": "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/http-proxy-agent/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/http-proxy-agent/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/import-fresh": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz", + "integrity": "sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ==", + "dependencies": { + "parent-module": "^1.0.0", + "resolve-from": "^4.0.0" + }, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/ip-address": { + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-9.0.5.tgz", + "integrity": "sha512-zHtQzGojZXTwZTHQqra+ETKd4Sn3vgi7uBmlPoXVWZqYvuKmtI0l/VZTjqGmJY9x88GGOaZ9+G9ES8hC4T4X8g==", + "dependencies": { + "jsbn": "1.1.0", + "sprintf-js": "^1.1.3" + }, + "engines": { + "node": ">= 12" + } + }, + "node_modules/is-arrayish": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", + "integrity": "sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==" + }, + "node_modules/is-core-module": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.16.1.tgz", + "integrity": "sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==", + "dependencies": { + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "engines": { + "node": ">=8" + } + }, + "node_modules/isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" + }, + "node_modules/js-binary-schema-parser": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/js-binary-schema-parser/-/js-binary-schema-parser-2.0.3.tgz", + "integrity": "sha512-xezGJmOb4lk/M1ZZLTR/jaBHQ4gG/lqQnJqdIv4721DMggsa1bDVlHXNeHYogaIEHD9vCRv0fcL4hMA+Coarkg==" + }, + "node_modules/js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, + "node_modules/jsbn": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz", + "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==" + }, + "node_modules/json-parse-even-better-errors": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", + "integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==" + }, + "node_modules/json5": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", + "bin": { + "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/jsonrepair": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/jsonrepair/-/jsonrepair-3.8.1.tgz", + "integrity": "sha512-5wnjaO53EJOhfLFY92nvBz2B9gqF9ql/D4HKUb1WOSBaqtVcAifFfmurblnhCJn/ySqKFA8U3n7nhGMAu/hEjQ==", + "bin": { + "jsonrepair": "bin/cli.js" + } + }, + "node_modules/jstat": { + "version": "1.9.6", + "resolved": "https://registry.npmjs.org/jstat/-/jstat-1.9.6.tgz", + "integrity": "sha512-rPBkJbK2TnA8pzs93QcDDPlKcrtZWuuCo2dVR0TFLOJSxhqfWOVCSp8aV3/oSbn+4uY4yw1URtLpHQedtmXfug==" + }, + "node_modules/lines-and-columns": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", + "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==" + }, + "node_modules/lottie-web": { + "version": "5.12.2", + "resolved": "https://registry.npmjs.org/lottie-web/-/lottie-web-5.12.2.tgz", + "integrity": "sha512-uvhvYPC8kGPjXT3MyKMrL3JitEAmDMp30lVkuq/590Mw9ok6pWcFCwXJveo0t5uqYw1UREQHofD+jVpdjBv8wg==" + }, + "node_modules/lru-cache": { + "version": "7.18.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz", + "integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", + "dev": true + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha512-7Wl+Jz+IGWuSdgsQEJ4JunV0si/iMhg42MnQQG6h1R6TNeVenp4U9x5CC5v/gYqz/fENLQITAWXidNtVL0NNbw==" + }, + "node_modules/mitt": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.1.tgz", + "integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==" + }, + "node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, + "node_modules/netmask": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/netmask/-/netmask-2.0.2.tgz", + "integrity": "sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==", + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-sql-parser": { + "version": "4.17.0", + "resolved": "https://registry.npmjs.org/node-sql-parser/-/node-sql-parser-4.17.0.tgz", + "integrity": "sha512-3IhovpmUBpcETnoKK/KBdkz2mz53kVG5E1dnqz1QuYvtzdxYZW5xaGGEvW9u6Yyy2ivwR3eUZrn9inmEVef02w==", + "dependencies": { + "big-integer": "^1.6.48" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/pac-proxy-agent": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/pac-proxy-agent/-/pac-proxy-agent-7.2.0.tgz", + "integrity": "sha512-TEB8ESquiLMc0lV8vcd5Ql/JAKAoyzHFXaStwjkzpOpC5Yv+pIzLfHvjTSdf3vpa2bMiUQrg9i6276yn8666aA==", + "dependencies": { + "@tootallnate/quickjs-emscripten": "^0.23.0", + "agent-base": "^7.1.2", + "debug": "^4.3.4", + "get-uri": "^6.0.1", + "http-proxy-agent": "^7.0.0", + "https-proxy-agent": "^7.0.6", + "pac-resolver": "^7.0.1", + "socks-proxy-agent": "^8.0.5" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/pac-proxy-agent/node_modules/agent-base": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.3.tgz", + "integrity": "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/pac-proxy-agent/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/pac-proxy-agent/node_modules/https-proxy-agent": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", + "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/pac-proxy-agent/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/pac-resolver": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/pac-resolver/-/pac-resolver-7.0.1.tgz", + "integrity": "sha512-5NPgf87AT2STgwa2ntRMr45jTKrYBGkVU36yT0ig/n/GMAa3oPqhZfIQ2kMEimReg0+t9kZViDVZ83qfVUlckg==", + "dependencies": { + "degenerator": "^5.0.0", + "netmask": "^2.0.2" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/parent-module": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", + "integrity": "sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==", + "dependencies": { + "callsites": "^3.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/parse-json": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz", + "integrity": "sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg==", + "dependencies": { + "@babel/code-frame": "^7.0.0", + "error-ex": "^1.3.1", + "json-parse-even-better-errors": "^2.3.0", + "lines-and-columns": "^1.1.6" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/parse-svg-path": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/parse-svg-path/-/parse-svg-path-0.1.2.tgz", + "integrity": "sha512-JyPSBnkTJ0AI8GGJLfMXvKq42cj5c006fnLz6fXy6zfoVjJizi8BNTpu8on8ziI1cKy9d9DGNuY17Ce7wuejpQ==" + }, + "node_modules/path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==" + }, + "node_modules/path-data-parser": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/path-data-parser/-/path-data-parser-0.1.0.tgz", + "integrity": "sha512-NOnmBpt5Y2RWbuv0LMzsayp3lVylAHLPUTut412ZA3l+C4uw4ZVkQbjShYCQ8TCpUMdPapr4YjUqLYD6v68j+w==" + }, + "node_modules/path-parse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" + }, + "node_modules/path-source": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/path-source/-/path-source-0.1.3.tgz", + "integrity": "sha512-dWRHm5mIw5kw0cs3QZLNmpUWty48f5+5v9nWD2dw3Y0Hf+s01Ag8iJEWV0Sm0kocE8kK27DrIowha03e1YR+Qw==", + "dependencies": { + "array-source": "0.0", + "file-source": "0.6" + } + }, + "node_modules/pbf": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/pbf/-/pbf-3.3.0.tgz", + "integrity": "sha512-XDF38WCH3z5OV/OVa8GKUNtLAyneuzbCisx7QUCF8Q6Nutx0WnJrQe5O+kOtBlLfRNUws98Y58Lblp+NJG5T4Q==", + "dependencies": { + "ieee754": "^1.1.12", + "resolve-protobuf-schema": "^2.1.0" + }, + "bin": { + "pbf": "bin/pbf" + } + }, + "node_modules/pend": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", + "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==" + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==" + }, + "node_modules/point-at-length": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/point-at-length/-/point-at-length-1.1.0.tgz", + "integrity": "sha512-nNHDk9rNEh/91o2Y8kHLzBLNpLf80RYd2gCun9ss+V0ytRSf6XhryBTx071fesktjbachRmGuUbId+JQmzhRXw==", + "dependencies": { + "abs-svg-path": "~0.1.1", + "isarray": "~0.0.1", + "parse-svg-path": "~0.1.1" + } + }, + "node_modules/points-on-curve": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/points-on-curve/-/points-on-curve-0.2.0.tgz", + "integrity": "sha512-0mYKnYYe9ZcqMCWhUjItv/oHjvgEsfKvnUTg8sAtnHr3GVy7rGkXCb6d5cSyqrWqL4k81b9CPg3urd+T7aop3A==" + }, + "node_modules/points-on-path": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/points-on-path/-/points-on-path-0.2.1.tgz", + "integrity": "sha512-25ClnWWuw7JbWZcgqY/gJ4FQWadKxGWk+3kR/7kD0tCaDtPPMj7oHu2ToLaVhfpnHrZzYby2w6tUA0eOIuUg8g==", + "dependencies": { + "path-data-parser": "0.1.0", + "points-on-curve": "0.2.0" + } + }, + "node_modules/progress": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", + "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/protocol-buffers-schema": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz", + "integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw==" + }, + "node_modules/proxy-agent": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/proxy-agent/-/proxy-agent-6.5.0.tgz", + "integrity": "sha512-TmatMXdr2KlRiA2CyDu8GqR8EjahTG3aY3nXjdzFyoZbmB8hrBsTyMezhULIXKnC0jpfjlmiZ3+EaCzoInSu/A==", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "^4.3.4", + "http-proxy-agent": "^7.0.1", + "https-proxy-agent": "^7.0.6", + "lru-cache": "^7.14.1", + "pac-proxy-agent": "^7.1.0", + "proxy-from-env": "^1.1.0", + "socks-proxy-agent": "^8.0.5" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/proxy-agent/node_modules/agent-base": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.3.tgz", + "integrity": "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/proxy-agent/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/proxy-agent/node_modules/https-proxy-agent": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", + "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/proxy-agent/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, + "node_modules/pump": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz", + "integrity": "sha512-tUPXtzlGM8FE3P0ZL6DVs/3P58k9nk8/jZeQCurTJylQA8qFYzHFfhBJkuqyE0FifOsQ0uKWekiZ5g8wtr28cw==", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, + "node_modules/puppeteer": { + "version": "24.9.0", + "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-24.9.0.tgz", + "integrity": "sha512-L0pOtALIx8rgDt24Y+COm8X52v78gNtBOW6EmUcEPci0TYD72SAuaXKqasRIx4JXxmg2Tkw5ySKcpPOwN8xXnQ==", + "hasInstallScript": true, + "dependencies": { + "@puppeteer/browsers": "2.10.5", + "chromium-bidi": "5.1.0", + "cosmiconfig": "^9.0.0", + "devtools-protocol": "0.0.1439962", + "puppeteer-core": "24.9.0", + "typed-query-selector": "^2.12.0" + }, + "bin": { + "puppeteer": "lib/cjs/puppeteer/node/cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/puppeteer-core": { + "version": "24.9.0", + "resolved": "https://registry.npmjs.org/puppeteer-core/-/puppeteer-core-24.9.0.tgz", + "integrity": "sha512-HFdCeH/wx6QPz8EncafbCqJBqaCG1ENW75xg3cLFMRUoqZDgByT6HSueiumetT2uClZxwqj0qS4qMVZwLHRHHw==", + "dependencies": { + "@puppeteer/browsers": "2.10.5", + "chromium-bidi": "5.1.0", + "debug": "^4.4.1", + "devtools-protocol": "0.0.1439962", + "typed-query-selector": "^2.12.0", + "ws": "^8.18.2" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/puppeteer-core/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/puppeteer-core/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/resolve": { + "version": "1.22.10", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.10.tgz", + "integrity": "sha512-NPRy+/ncIMeDlTAsuqwKIiferiawhefFJtkNSW0qZJEqMEb+qBt/77B/jGeeek+F0uOeN05CDa6HXbbIgtVX4w==", + "dependencies": { + "is-core-module": "^2.16.0", + "path-parse": "^1.0.7", + "supports-preserve-symlinks-flag": "^1.0.0" + }, + "bin": { + "resolve": "bin/resolve" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/resolve-from": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", + "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", + "engines": { + "node": ">=4" + } + }, + "node_modules/resolve-protobuf-schema": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/resolve-protobuf-schema/-/resolve-protobuf-schema-2.1.0.tgz", + "integrity": "sha512-kI5ffTiZWmJaS/huM8wZfEMer1eRd7oJQhDuxeCLe3t7N7mX3z94CN0xPxBQxFYQTSNz9T0i+v6inKqSdK8xrQ==", + "dependencies": { + "protocol-buffers-schema": "^3.3.1" + } + }, + "node_modules/roughjs": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/roughjs/-/roughjs-4.5.2.tgz", + "integrity": "sha512-2xSlLDKdsWyFxrveYWk9YQ/Y9UfK38EAMRNkYkMqYBJvPX8abCa9PN0x3w02H8Oa6/0bcZICJU+U95VumPqseg==", + "dependencies": { + "path-data-parser": "^0.1.0", + "points-on-curve": "^0.2.0", + "points-on-path": "^0.2.1" + } + }, + "node_modules/rw": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz", + "integrity": "sha512-PdhdWy89SiZogBLaw42zdeqtRJ//zFd2PgQavcICDUgJT5oW10QCRKbJ6bg4r0/UY2M6BWd5tkxuGFRvCkgfHQ==" + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/semver": { + "version": "7.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz", + "integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/shapefile": { + "version": "0.6.6", + "resolved": "https://registry.npmjs.org/shapefile/-/shapefile-0.6.6.tgz", + "integrity": "sha512-rLGSWeK2ufzCVx05wYd+xrWnOOdSV7xNUW5/XFgx3Bc02hBkpMlrd2F1dDII7/jhWzv0MSyBFh5uJIy9hLdfuw==", + "dependencies": { + "array-source": "0.0", + "commander": "2", + "path-source": "0.1", + "slice-source": "0.4", + "stream-source": "0.3", + "text-encoding": "^0.6.4" + }, + "bin": { + "dbf2json": "bin/dbf2json", + "shp2json": "bin/shp2json" + } + }, + "node_modules/simple-statistics": { + "version": "7.8.8", + "resolved": "https://registry.npmjs.org/simple-statistics/-/simple-statistics-7.8.8.tgz", + "integrity": "sha512-CUtP0+uZbcbsFpqEyvNDYjJCl+612fNgjT8GaVuvMG7tBuJg8gXGpsP5M7X658zy0IcepWOZ6nPBu1Qb9ezA1w==", + "engines": { + "node": "*" + } + }, + "node_modules/simplify-geojson": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/simplify-geojson/-/simplify-geojson-1.0.5.tgz", + "integrity": "sha512-02l1W4UipP5ivNVq6kX15mAzCRIV1oI3tz0FUEyOsNiv1ltuFDjbNhO+nbv/xhbDEtKqWLYuzpWhUsJrjR/ypA==", + "dependencies": { + "concat-stream": "~1.4.1", + "minimist": "1.2.6", + "simplify-geometry": "0.0.2" + }, + "bin": { + "simplify-geojson": "cli.js" + } + }, + "node_modules/simplify-geojson/node_modules/concat-stream": { + "version": "1.4.11", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.4.11.tgz", + "integrity": "sha512-X3JMh8+4je3U1cQpG87+f9lXHDrqcb2MVLg9L7o8b1UZ0DzhRrUpdn65ttzu10PpJPPI3MQNkis+oha6TSA9Mw==", + "engines": [ + "node >= 0.8" + ], + "dependencies": { + "inherits": "~2.0.1", + "readable-stream": "~1.1.9", + "typedarray": "~0.0.5" + } + }, + "node_modules/simplify-geojson/node_modules/minimist": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==" + }, + "node_modules/simplify-geojson/node_modules/readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "node_modules/simplify-geojson/node_modules/string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" + }, + "node_modules/simplify-geometry": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/simplify-geometry/-/simplify-geometry-0.0.2.tgz", + "integrity": "sha512-ZEyrplkqgCqDlL7V8GbbYgTLlcnNF+MWWUdy8s8ZeJru50bnI71rDew/I+HG36QS2mPOYAq1ZjwNXxHJ8XOVBw==" + }, + "node_modules/slice-source": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/slice-source/-/slice-source-0.4.1.tgz", + "integrity": "sha512-YiuPbxpCj4hD9Qs06hGAz/OZhQ0eDuALN0lRWJez0eD/RevzKqGdUx1IOMUnXgpr+sXZLq3g8ERwbAH0bCb8vg==" + }, + "node_modules/smart-buffer": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", + "engines": { + "node": ">= 6.0.0", + "npm": ">= 3.0.0" + } + }, + "node_modules/socks": { + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.4.tgz", + "integrity": "sha512-D3YaD0aRxR3mEcqnidIs7ReYJFVzWdd6fXJYUM8ixcQcJRGTka/b3saV0KflYhyVJXKhb947GndU35SxYNResQ==", + "dependencies": { + "ip-address": "^9.0.5", + "smart-buffer": "^4.2.0" + }, + "engines": { + "node": ">= 10.0.0", + "npm": ">= 3.0.0" + } + }, + "node_modules/socks-proxy-agent": { + "version": "8.0.5", + "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-8.0.5.tgz", + "integrity": "sha512-HehCEsotFqbPW9sJ8WVYB6UbmIMv7kUUORIF2Nncq4VQvBfNBLibW9YZR5dlYCSUhwcD628pRllm7n+E+YTzJw==", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "^4.3.4", + "socks": "^2.8.3" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/socks-proxy-agent/node_modules/agent-base": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.3.tgz", + "integrity": "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/socks-proxy-agent/node_modules/debug": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", + "integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/socks-proxy-agent/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "optional": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/sprintf-js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", + "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==" + }, + "node_modules/stream-source": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/stream-source/-/stream-source-0.3.5.tgz", + "integrity": "sha512-ZuEDP9sgjiAwUVoDModftG0JtYiLUV8K4ljYD1VyUMRWtbVf92474o4kuuul43iZ8t/hRuiDAx1dIJSvirrK/g==" + }, + "node_modules/streamx": { + "version": "2.22.0", + "resolved": "https://registry.npmjs.org/streamx/-/streamx-2.22.0.tgz", + "integrity": "sha512-sLh1evHOzBy/iWRiR6d1zRcLao4gGZr3C1kzNz4fopCOKJb6xD9ub8Mpi9Mr1R6id5o43S+d93fI48UC5uM9aw==", + "dependencies": { + "fast-fifo": "^1.3.2", + "text-decoder": "^1.1.0" + }, + "optionalDependencies": { + "bare-events": "^2.2.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/string-similarity-js": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/string-similarity-js/-/string-similarity-js-2.1.4.tgz", + "integrity": "sha512-uApODZNjCHGYROzDSAdCmAHf60L/pMDHnP/yk6TAbvGg7JSPZlSto/ceCI7hZEqzc53/juU2aOJFkM2yUVTMTA==" + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/supports-preserve-symlinks-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", + "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/tar-fs": { + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.0.8.tgz", + "integrity": "sha512-ZoROL70jptorGAlgAYiLoBLItEKw/fUxg9BSYK/dF/GAGYFJOJJJMvjPAKDJraCXFwadD456FCuvLWgfhMsPwg==", + "dependencies": { + "pump": "^3.0.0", + "tar-stream": "^3.1.5" + }, + "optionalDependencies": { + "bare-fs": "^4.0.1", + "bare-path": "^3.0.0" + } + }, + "node_modules/tar-stream": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-3.1.7.tgz", + "integrity": "sha512-qJj60CXt7IU1Ffyc3NJMjh6EkuCFej46zUqJ4J7pqYlThyd9bO0XBTmcOIhSzZJVWfsLks0+nle/j538YAW9RQ==", + "dependencies": { + "b4a": "^1.6.4", + "fast-fifo": "^1.2.0", + "streamx": "^2.15.0" + } + }, + "node_modules/text-decoder": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/text-decoder/-/text-decoder-1.2.3.tgz", + "integrity": "sha512-3/o9z3X0X0fTupwsYvR03pJ/DjWuqqrfwBgTQzdWDiQSm9KitAyz/9WqsT2JQW7KV2m+bC2ol/zqpW37NHxLaA==", + "dependencies": { + "b4a": "^1.6.4" + } + }, + "node_modules/text-encoding": { + "version": "0.6.4", + "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", + "integrity": "sha512-hJnc6Qg3dWoOMkqP53F0dzRIgtmsAge09kxUIqGrEUS4qr5rWLckGYaQAVr+opBrIMRErGgy6f5aPnyPpyGRfg==", + "deprecated": "no longer maintained" + }, + "node_modules/topojson-client": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/topojson-client/-/topojson-client-3.1.0.tgz", + "integrity": "sha512-605uxS6bcYxGXw9qi62XyrV6Q3xwbndjachmNxu8HWTtVPxZfEJN9fd/SZS1Q54Sn2y0TMyMxFj/cJINqGHrKw==", + "dependencies": { + "commander": "2" + }, + "bin": { + "topo2geo": "bin/topo2geo", + "topomerge": "bin/topomerge", + "topoquantize": "bin/topoquantize" + } + }, + "node_modules/topojson-server": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/topojson-server/-/topojson-server-3.0.1.tgz", + "integrity": "sha512-/VS9j/ffKr2XAOjlZ9CgyyeLmgJ9dMwq6Y0YEON8O7p/tGGk+dCWnrE03zEdu7i4L7YsFZLEPZPzCvcB7lEEXw==", + "dependencies": { + "commander": "2" + }, + "bin": { + "geo2topo": "bin/geo2topo" + } + }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/ts-pattern": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/ts-pattern/-/ts-pattern-4.1.4.tgz", + "integrity": "sha512-Mcw65oUd1w5ktKi5BRwrnz16Otwk9iv7P0dKgvbi+A1albCDgnixohSqNLuFwIp5dzxPmTPm0iDQ6p1ZJr9uGw==" + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==" + }, + "node_modules/typed-query-selector": { + "version": "2.12.0", + "resolved": "https://registry.npmjs.org/typed-query-selector/-/typed-query-selector-2.12.0.tgz", + "integrity": "sha512-SbklCd1F0EiZOyPiW192rrHZzZ5sBijB6xM+cpmrwDqObvdtunOHHIk9fCGsoK5JVIYXoyEp4iEdE3upFH3PAg==" + }, + "node_modules/typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" + }, + "node_modules/typescript": { + "version": "5.8.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.2.tgz", + "integrity": "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ==", + "devOptional": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz", + "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==", + "devOptional": true + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, + "node_modules/wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + }, + "node_modules/ws": { + "version": "8.18.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.2.tgz", + "integrity": "sha512-DMricUmwGZUVr++AEAe2uiVM7UoO9MAVZMDu05UQOaUII0lp+zOzLLU4Xqh/JvTqklB1T4uELaaPBKyjE1r4fQ==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/y18n": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "engines": { + "node": ">=10" + } + }, + "node_modules/yauzl": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", + "dependencies": { + "buffer-crc32": "~0.2.3", + "fd-slicer": "~1.1.0" + } + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/zod": { + "version": "3.25.20", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.20.tgz", + "integrity": "sha512-z03fqpTMDF1G02VLKUMt6vyACE7rNWkh3gpXVHgPTw28NPtDFRGvcpTtPwn2kMKtQ0idtYJUTxchytmnqYswcw==", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + } + } +} diff --git a/app/tool/chart_visualization/package.json b/app/tool/chart_visualization/package.json new file mode 100644 index 0000000000000000000000000000000000000000..1e9189dcbecb13fd80ee301b71cc0ef235a7d5ba --- /dev/null +++ b/app/tool/chart_visualization/package.json @@ -0,0 +1,22 @@ +{ + "name": "chart_visualization", + "version": "1.0.0", + "main": "src/index.ts", + "devDependencies": { + "@types/node": "^22.10.1", + "ts-node": "^10.9.2", + "typescript": "^5.7.2" + }, + "dependencies": { + "@visactor/vchart": "^1.13.7", + "@visactor/vmind": "2.0.5", + "get-stdin": "^9.0.0", + "puppeteer": "^24.9.0" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/app/tool/chart_visualization/python_execute.py b/app/tool/chart_visualization/python_execute.py new file mode 100644 index 0000000000000000000000000000000000000000..8a7b5bb4db1af723cdda252210bbf94cc27db9ad --- /dev/null +++ b/app/tool/chart_visualization/python_execute.py @@ -0,0 +1,36 @@ +from app.config import config +from app.tool.python_execute import PythonExecute + + +class NormalPythonExecute(PythonExecute): + """A tool for executing Python code with timeout and safety restrictions.""" + + name: str = "python_execute" + description: str = """Execute Python code for in-depth data analysis / data report(task conclusion) / other normal task without direct visualization.""" + parameters: dict = { + "type": "object", + "properties": { + "code_type": { + "description": "code type, data process / data report / others", + "type": "string", + "default": "process", + "enum": ["process", "report", "others"], + }, + "code": { + "type": "string", + "description": """Python code to execute. +# Note +1. The code should generate a comprehensive text-based report containing dataset overview, column details, basic statistics, derived metrics, timeseries comparisons, outliers, and key insights. +2. Use print() for all outputs so the analysis (including sections like 'Dataset Overview' or 'Preprocessing Results') is clearly visible and save it also +3. Save any report / processed files / each analysis result in worksapce directory: {directory} +4. Data reports need to be content-rich, including your overall analysis process and corresponding data visualization. +5. You can invode this tool step-by-step to do data analysis from summary to in-depth with data report saved also""".format( + directory=config.workspace_root + ), + }, + }, + "required": ["code"], + } + + async def execute(self, code: str, code_type: str | None = None, timeout=5): + return await super().execute(code, timeout) diff --git a/app/tool/chart_visualization/src/chartVisualize.ts b/app/tool/chart_visualization/src/chartVisualize.ts new file mode 100644 index 0000000000000000000000000000000000000000..c5091a373fc0a5a0b156fdae18fc2a6623a56946 --- /dev/null +++ b/app/tool/chart_visualization/src/chartVisualize.ts @@ -0,0 +1,372 @@ +import path from "path"; +import fs from "fs"; +import puppeteer from "puppeteer"; +import VMind, { ChartType, DataTable } from "@visactor/vmind"; +import { isString } from "@visactor/vutils"; + +enum AlgorithmType { + OverallTrending = "overallTrend", + AbnormalTrend = "abnormalTrend", + PearsonCorrelation = "pearsonCorrelation", + SpearmanCorrelation = "spearmanCorrelation", + ExtremeValue = "extremeValue", + MajorityValue = "majorityValue", + StatisticsAbnormal = "statisticsAbnormal", + StatisticsBase = "statisticsBase", + DbscanOutlier = "dbscanOutlier", + LOFOutlier = "lofOutlier", + TurningPoint = "turningPoint", + PageHinkley = "pageHinkley", + DifferenceOutlier = "differenceOutlier", + Volatility = "volatility", +} + +const getBase64 = async (spec: any, width?: number, height?: number) => { + spec.animation = false; + width && (spec.width = width); + height && (spec.height = height); + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setContent(getHtmlVChart(spec, width, height)); + + const dataUrl = await page.evaluate(() => { + const canvas: any = document + .getElementById("chart-container") + ?.querySelector("canvas"); + return canvas?.toDataURL("image/png"); + }); + + const base64Data = dataUrl.replace(/^data:image\/png;base64,/, ""); + await browser.close(); + return Buffer.from(base64Data, "base64"); +}; + +const serializeSpec = (spec: any) => { + return JSON.stringify(spec, (key, value) => { + if (typeof value === "function") { + const funcStr = value + .toString() + .replace(/(\r\n|\n|\r)/gm, "") + .replace(/\s+/g, " "); + + return `__FUNCTION__${funcStr}`; + } + return value; + }); +}; + +function getHtmlVChart(spec: any, width?: number, height?: number) { + return ` + + + VChart Demo + + + +
+ + + +`; +} + +/** + * get file path saved string + * @param isUpdate {boolean} default: false, update existed file when is true + */ +function getSavedPathName( + directory: string, + fileName: string, + outputType: "html" | "png" | "json" | "md", + isUpdate: boolean = false +) { + let newFileName = fileName; + while ( + !isUpdate && + fs.existsSync( + path.join(directory, "visualization", `${newFileName}.${outputType}`) + ) + ) { + newFileName += "_new"; + } + return path.join(directory, "visualization", `${newFileName}.${outputType}`); +} + +const readStdin = (): Promise => { + return new Promise((resolve) => { + let input = ""; + process.stdin.setEncoding("utf-8"); // 确保编码与 Python 端一致 + process.stdin.on("data", (chunk) => (input += chunk)); + process.stdin.on("end", () => resolve(input)); + }); +}; + +/** Save insights markdown in local, and return content && path */ +const setInsightTemplate = ( + path: string, + title: string, + insights: string[] +) => { + let res = ""; + if (insights.length) { + res += `## ${title} Insights`; + insights.forEach((insight, index) => { + res += `\n${index + 1}. ${insight}`; + }); + } + if (res) { + fs.writeFileSync(path, res, "utf-8"); + return { insight_path: path, insight_md: res }; + } + return {}; +}; + +/** Save vmind result into local file, Return chart file path */ +async function saveChartRes(options: { + spec: any; + directory: string; + outputType: "png" | "html"; + fileName: string; + width?: number; + height?: number; + isUpdate?: boolean; +}) { + const { directory, fileName, spec, outputType, width, height, isUpdate } = + options; + const specPath = getSavedPathName(directory, fileName, "json", isUpdate); + fs.writeFileSync(specPath, JSON.stringify(spec, null, 2)); + const savedPath = getSavedPathName(directory, fileName, outputType, isUpdate); + if (outputType === "png") { + const base64 = await getBase64(spec, width, height); + fs.writeFileSync(savedPath, base64); + } else { + const html = getHtmlVChart(spec, width, height); + fs.writeFileSync(savedPath, html, "utf-8"); + } + return savedPath; +} + +async function generateChart( + vmind: VMind, + options: { + dataset: string | DataTable; + userPrompt: string; + directory: string; + outputType: "png" | "html"; + fileName: string; + width?: number; + height?: number; + language?: "en" | "zh"; + } +) { + let res: { + chart_path?: string; + error?: string; + insight_path?: string; + insight_md?: string; + } = {}; + const { + dataset, + userPrompt, + directory, + width, + height, + outputType, + fileName, + language, + } = options; + try { + // Get chart spec and save in local file + const jsonDataset = isString(dataset) ? JSON.parse(dataset) : dataset; + const { spec, error, chartType } = await vmind.generateChart( + userPrompt, + undefined, + jsonDataset, + { + enableDataQuery: false, + theme: "light", + } + ); + if (error || !spec) { + return { + error: error || "Spec of Chart was Empty!", + }; + } + + spec.title = { + text: userPrompt, + }; + if (!fs.existsSync(path.join(directory, "visualization"))) { + fs.mkdirSync(path.join(directory, "visualization")); + } + const specPath = getSavedPathName(directory, fileName, "json"); + res.chart_path = await saveChartRes({ + directory, + spec, + width, + height, + fileName, + outputType, + }); + + // get chart insights and save in local + const insights = []; + if ( + chartType && + [ + ChartType.BarChart, + ChartType.LineChart, + ChartType.AreaChart, + ChartType.ScatterPlot, + ChartType.DualAxisChart, + ].includes(chartType) + ) { + const { insights: vmindInsights } = await vmind.getInsights(spec, { + maxNum: 6, + algorithms: [ + AlgorithmType.OverallTrending, + AlgorithmType.AbnormalTrend, + AlgorithmType.PearsonCorrelation, + AlgorithmType.SpearmanCorrelation, + AlgorithmType.StatisticsAbnormal, + AlgorithmType.LOFOutlier, + AlgorithmType.DbscanOutlier, + AlgorithmType.MajorityValue, + AlgorithmType.PageHinkley, + AlgorithmType.TurningPoint, + AlgorithmType.StatisticsBase, + AlgorithmType.Volatility, + ], + usePolish: false, + language: language === "en" ? "english" : "chinese", + }); + insights.push(...vmindInsights); + } + const insightsText = insights + .map((insight) => insight.textContent?.plainText) + .filter((insight) => !!insight) as string[]; + spec.insights = insights; + fs.writeFileSync(specPath, JSON.stringify(spec, null, 2)); + res = { + ...res, + ...setInsightTemplate( + getSavedPathName(directory, fileName, "md"), + userPrompt, + insightsText + ), + }; + } catch (error: any) { + res.error = error.toString(); + } finally { + return res; + } +} + +async function updateChartWithInsight( + vmind: VMind, + options: { + directory: string; + outputType: "png" | "html"; + fileName: string; + insightsId: number[]; + } +) { + const { directory, outputType, fileName, insightsId } = options; + let res: { error?: string; chart_path?: string } = {}; + try { + const specPath = getSavedPathName(directory, fileName, "json", true); + const spec = JSON.parse(fs.readFileSync(specPath, "utf8")); + // llm select index from 1 + const insights = (spec.insights || []).filter( + (_insight: any, index: number) => insightsId.includes(index + 1) + ); + const { newSpec, error } = await vmind.updateSpecByInsights(spec, insights); + if (error) { + throw error; + } + res.chart_path = await saveChartRes({ + spec: newSpec, + directory, + outputType, + fileName, + isUpdate: true, + }); + } catch (error: any) { + res.error = error.toString(); + } finally { + return res; + } +} + +async function executeVMind() { + const input = await readStdin(); + const inputData = JSON.parse(input); + let res; + const { + llm_config, + width, + dataset = [], + height, + directory, + user_prompt: userPrompt, + output_type: outputType = "png", + file_name: fileName, + task_type: taskType = "visualization", + insights_id: insightsId = [], + language = "en", + } = inputData; + const { base_url: baseUrl, model, api_key: apiKey } = llm_config; + const vmind = new VMind({ + url: `${baseUrl}/chat/completions`, + model, + headers: { + "api-key": apiKey, + Authorization: `Bearer ${apiKey}`, + }, + }); + if (taskType === "visualization") { + res = await generateChart(vmind, { + dataset, + userPrompt, + directory, + outputType, + fileName, + width, + height, + language, + }); + } else if (taskType === "insight" && insightsId.length) { + res = await updateChartWithInsight(vmind, { + directory, + fileName, + outputType, + insightsId, + }); + } + console.log(JSON.stringify(res)); +} + +executeVMind(); diff --git a/app/tool/chart_visualization/test/chart_demo.py b/app/tool/chart_visualization/test/chart_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..d89d993f24b7f04c92d6110e2827d51d64dad9a4 --- /dev/null +++ b/app/tool/chart_visualization/test/chart_demo.py @@ -0,0 +1,191 @@ +import asyncio + +from app.agent.data_analysis import DataAnalysis +from app.logger import logger + + +prefix = "Help me generate charts and save them locally, specifically:" +tasks = [ + { + "prompt": "Help me show the sales of different products in different regions", + "data": """Product Name,Region,Sales +Coke,South,2350 +Coke,East,1027 +Coke,West,1027 +Coke,North,1027 +Sprite,South,215 +Sprite,East,654 +Sprite,West,159 +Sprite,North,28 +Fanta,South,345 +Fanta,East,654 +Fanta,West,2100 +Fanta,North,1679 +Xingmu,South,1476 +Xingmu,East,830 +Xingmu,West,532 +Xingmu,North,498 +""", + }, + { + "prompt": "Show market share of each brand", + "data": """Brand Name,Market Share,Average Price,Net Profit +Apple,0.5,7068,314531 +Samsung,0.2,6059,362345 +Vivo,0.05,3406,234512 +Nokia,0.01,1064,-1345 +Xiaomi,0.1,4087,131345""", + }, + { + "prompt": "Please help me show the sales trend of each product", + "data": """Date,Type,Value +2023-01-01,Product A,52.9 +2023-01-01,Product B,63.6 +2023-01-01,Product C,11.2 +2023-01-02,Product A,45.7 +2023-01-02,Product B,89.1 +2023-01-02,Product C,21.4 +2023-01-03,Product A,67.2 +2023-01-03,Product B,82.4 +2023-01-03,Product C,31.7 +2023-01-04,Product A,80.7 +2023-01-04,Product B,55.1 +2023-01-04,Product C,21.1 +2023-01-05,Product A,65.6 +2023-01-05,Product B,78 +2023-01-05,Product C,31.3 +2023-01-06,Product A,75.6 +2023-01-06,Product B,89.1 +2023-01-06,Product C,63.5 +2023-01-07,Product A,67.3 +2023-01-07,Product B,77.2 +2023-01-07,Product C,43.7 +2023-01-08,Product A,96.1 +2023-01-08,Product B,97.6 +2023-01-08,Product C,59.9 +2023-01-09,Product A,96.1 +2023-01-09,Product B,100.6 +2023-01-09,Product C,66.8 +2023-01-10,Product A,101.6 +2023-01-10,Product B,108.3 +2023-01-10,Product C,56.9""", + }, + { + "prompt": "Show the popularity of search keywords", + "data": """Keyword,Popularity +Hot Word,1000 +Zao Le Wo Men,800 +Rao Jian Huo,400 +My Wish is World Peace,400 +Xiu Xiu Xiu,400 +Shenzhou 11,400 +Hundred Birds Facing the Wind,400 +China Women's Volleyball Team,400 +My Guan Na,400 +Leg Dong,400 +Hot Pot Hero,400 +Baby's Heart is Bitter,400 +Olympics,400 +Awesome My Brother,400 +Poetry and Distance,400 +Song Joong-ki,400 +PPAP,400 +Blue Thin Mushroom,400 +Rain Dew Evenly,400 +Friendship's Little Boat Says It Flips,400 +Beijing Slump,400 +Dedication,200 +Apple,200 +Dog Belt,200 +Old Driver,200 +Melon-Eating Crowd,200 +Zootopia,200 +City Will Play,200 +Routine,200 +Water Reverse,200 +Why Don't You Go to Heaven,200 +Snake Spirit Man,200 +Why Don't You Go to Heaven,200 +Samsung Explosion Gate,200 +Little Li Oscar,200 +Ugly People Need to Read More,200 +Boyfriend Power,200 +A Face of Confusion,200 +Descendants of the Sun,200""", + }, + { + "prompt": "Help me compare the performance of different electric vehicle brands using a scatter plot", + "data": """Range,Charging Time,Brand Name,Average Price +2904,46,Brand1,2350 +1231,146,Brand2,1027 +5675,324,Brand3,1242 +543,57,Brand4,6754 +326,234,Brand5,215 +1124,67,Brand6,654 +3426,81,Brand7,159 +2134,24,Brand8,28 +1234,52,Brand9,345 +2345,27,Brand10,654 +526,145,Brand11,2100 +234,93,Brand12,1679 +567,94,Brand13,1476 +789,45,Brand14,830 +469,75,Brand15,532 +5689,54,Brand16,498 +""", + }, + { + "prompt": "Show conversion rates for each process", + "data": """Process,Conversion Rate,Month +Step1,100,1 +Step2,80,1 +Step3,60,1 +Step4,40,1""", + }, + { + "prompt": "Show the difference in breakfast consumption between men and women", + "data": """Day,Men-Breakfast,Women-Breakfast +Monday,15,22 +Tuesday,12,10 +Wednesday,15,20 +Thursday,10,12 +Friday,13,15 +Saturday,10,15 +Sunday,12,14""", + }, + { + "prompt": "Help me show this person's performance in different aspects, is he a hexagonal warrior", + "data": """dimension,performance +Strength,5 +Speed,5 +Shooting,3 +Endurance,5 +Precision,5 +Growth,5""", + }, + { + "prompt": "Show data flow", + "data": """Origin,Destination,value +Node A,Node 1,10 +Node A,Node 2,5 +Node B,Node 2,8 +Node B,Node 3,2 +Node C,Node 2,4 +Node A,Node C,2 +Node C,Node 1,2""", + }, +] + + +async def main(): + for index, item in enumerate(tasks): + logger.info(f"Begin task {index} / {len(tasks)}!") + agent = DataAnalysis() + await agent.run( + f"{prefix},chart_description:{item['prompt']},Data:{item['data']}" + ) + logger.info(f"Finish with {item['prompt']}") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/app/tool/chart_visualization/test/report_demo.py b/app/tool/chart_visualization/test/report_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..d66f8cf25800a1c861a001355641bb70f00d8110 --- /dev/null +++ b/app/tool/chart_visualization/test/report_demo.py @@ -0,0 +1,27 @@ +import asyncio + +from app.agent.data_analysis import DataAnalysis + + +# from app.agent.manus import Manus + + +async def main(): + agent = DataAnalysis() + # agent = Manus() + await agent.run( + """Requirement: +1. Analyze the following data and generate a graphical data report in HTML format. The final product should be a data report. +Data: +Month | Team A | Team B | Team C +January | 1200 hours | 1350 hours | 1100 hours +February | 1250 hours | 1400 hours | 1150 hours +March | 1180 hours | 1300 hours | 1300 hours +April | 1220 hours | 1280 hours | 1400 hours +May | 1230 hours | 1320 hours | 1450 hours +June | 1200 hours | 1250 hours | 1500 hours """ + ) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/app/tool/chart_visualization/tsconfig.json b/app/tool/chart_visualization/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..7e499fa4097eab7a267b478b165e717d41283422 --- /dev/null +++ b/app/tool/chart_visualization/tsconfig.json @@ -0,0 +1,109 @@ +{ + "include": [ + "src/**/*.ts", + ], + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + /* Language and Environment */ + "target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + "typeRoots": [ + "./node_modules/@types", + "src/types" + ], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "noUncheckedSideEffectImports": true, /* Check side effect imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + /* JavaScript Support */ + "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + "checkJs": false, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/app/tool/computer_use_tool.py b/app/tool/computer_use_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..0ea57a7343a023a7a3fb9f177032b03dfc7dde50 --- /dev/null +++ b/app/tool/computer_use_tool.py @@ -0,0 +1,487 @@ +import asyncio +import base64 +import logging +import os +import time +from typing import Dict, Literal, Optional + +import aiohttp +from pydantic import Field + +from app.daytona.tool_base import Sandbox, SandboxToolsBase +from app.tool.base import ToolResult + + +KEYBOARD_KEYS = [ + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "i", + "j", + "k", + "l", + "m", + "n", + "o", + "p", + "q", + "r", + "s", + "t", + "u", + "v", + "w", + "x", + "y", + "z", + "0", + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "enter", + "esc", + "backspace", + "tab", + "space", + "delete", + "ctrl", + "alt", + "shift", + "win", + "up", + "down", + "left", + "right", + "f1", + "f2", + "f3", + "f4", + "f5", + "f6", + "f7", + "f8", + "f9", + "f10", + "f11", + "f12", + "ctrl+c", + "ctrl+v", + "ctrl+x", + "ctrl+z", + "ctrl+a", + "ctrl+s", + "alt+tab", + "alt+f4", + "ctrl+alt+delete", +] +MOUSE_BUTTONS = ["left", "right", "middle"] +_COMPUTER_USE_DESCRIPTION = """\ +A comprehensive computer automation tool that allows interaction with the desktop environment. +* This tool provides commands for controlling mouse, keyboard, and taking screenshots +* It maintains state including current mouse position +* Use this when you need to automate desktop applications, fill forms, or perform GUI interactions +Key capabilities include: +* Mouse Control: Move, click, drag, scroll +* Keyboard Input: Type text, press keys or key combinations +* Screenshots: Capture and save screen images +* Waiting: Pause execution for specified duration +""" + + +class ComputerUseTool(SandboxToolsBase): + """Computer automation tool for controlling the desktop environment.""" + + name: str = "computer_use" + description: str = _COMPUTER_USE_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "move_to", + "click", + "scroll", + "typing", + "press", + "wait", + "mouse_down", + "mouse_up", + "drag_to", + "hotkey", + "screenshot", + ], + "description": "The computer action to perform", + }, + "x": {"type": "number", "description": "X coordinate for mouse actions"}, + "y": {"type": "number", "description": "Y coordinate for mouse actions"}, + "button": { + "type": "string", + "enum": MOUSE_BUTTONS, + "description": "Mouse button for click/drag actions", + "default": "left", + }, + "num_clicks": { + "type": "integer", + "description": "Number of clicks", + "enum": [1, 2, 3], + "default": 1, + }, + "amount": { + "type": "integer", + "description": "Scroll amount (positive for up, negative for down)", + "minimum": -10, + "maximum": 10, + }, + "text": {"type": "string", "description": "Text to type"}, + "key": { + "type": "string", + "enum": KEYBOARD_KEYS, + "description": "Key to press", + }, + "keys": { + "type": "string", + "enum": KEYBOARD_KEYS, + "description": "Key combination to press", + }, + "duration": { + "type": "number", + "description": "Duration in seconds to wait", + "default": 0.5, + }, + }, + "required": ["action"], + "dependencies": { + "move_to": ["x", "y"], + "click": [], + "scroll": ["amount"], + "typing": ["text"], + "press": ["key"], + "wait": [], + "mouse_down": [], + "mouse_up": [], + "drag_to": ["x", "y"], + "hotkey": ["keys"], + "screenshot": [], + }, + } + session: Optional[aiohttp.ClientSession] = Field(default=None, exclude=True) + mouse_x: int = Field(default=0, exclude=True) + mouse_y: int = Field(default=0, exclude=True) + api_base_url: Optional[str] = Field(default=None, exclude=True) + + def __init__(self, sandbox: Optional[Sandbox] = None, **data): + """Initialize with optional sandbox.""" + super().__init__(**data) + if sandbox is not None: + self._sandbox = sandbox # 直接操作基类的私有属性 + self.api_base_url = sandbox.get_preview_link(8000).url + logging.info( + f"Initialized ComputerUseTool with API URL: {self.api_base_url}" + ) + + @classmethod + def create_with_sandbox(cls, sandbox: Sandbox) -> "ComputerUseTool": + """Factory method to create a tool with sandbox.""" + return cls(sandbox=sandbox) # 通过构造函数初始化 + + async def _get_session(self) -> aiohttp.ClientSession: + """Get or create aiohttp session for API requests.""" + if self.session is None or self.session.closed: + self.session = aiohttp.ClientSession() + return self.session + + async def _api_request( + self, method: str, endpoint: str, data: Optional[Dict] = None + ) -> Dict: + """Send request to automation service API.""" + try: + session = await self._get_session() + url = f"{self.api_base_url}/api{endpoint}" + logging.debug(f"API request: {method} {url} {data}") + if method.upper() == "GET": + async with session.get(url) as response: + result = await response.json() + else: # POST + async with session.post(url, json=data) as response: + result = await response.json() + logging.debug(f"API response: {result}") + return result + except Exception as e: + logging.error(f"API request failed: {str(e)}") + return {"success": False, "error": str(e)} + + async def execute( + self, + action: Literal[ + "move_to", + "click", + "scroll", + "typing", + "press", + "wait", + "mouse_down", + "mouse_up", + "drag_to", + "hotkey", + "screenshot", + ], + x: Optional[float] = None, + y: Optional[float] = None, + button: str = "left", + num_clicks: int = 1, + amount: Optional[int] = None, + text: Optional[str] = None, + key: Optional[str] = None, + keys: Optional[str] = None, + duration: float = 0.5, + **kwargs, + ) -> ToolResult: + """ + Execute a specified computer automation action. + Args: + action: The action to perform + x: X coordinate for mouse actions + y: Y coordinate for mouse actions + button: Mouse button for click/drag actions + num_clicks: Number of clicks to perform + amount: Scroll amount (positive for up, negative for down) + text: Text to type + key: Key to press + keys: Key combination to press + duration: Duration in seconds to wait + **kwargs: Additional arguments + Returns: + ToolResult with the action's output or error + """ + try: + if action == "move_to": + if x is None or y is None: + return ToolResult(error="x and y coordinates are required") + x_int = int(round(float(x))) + y_int = int(round(float(y))) + result = await self._api_request( + "POST", "/automation/mouse/move", {"x": x_int, "y": y_int} + ) + if result.get("success", False): + self.mouse_x = x_int + self.mouse_y = y_int + return ToolResult(output=f"Moved to ({x_int}, {y_int})") + else: + return ToolResult( + error=f"Failed to move: {result.get('error', 'Unknown error')}" + ) + elif action == "click": + x_val = x if x is not None else self.mouse_x + y_val = y if y is not None else self.mouse_y + x_int = int(round(float(x_val))) + y_int = int(round(float(y_val))) + num_clicks = int(num_clicks) + result = await self._api_request( + "POST", + "/automation/mouse/click", + { + "x": x_int, + "y": y_int, + "clicks": num_clicks, + "button": button.lower(), + }, + ) + if result.get("success", False): + self.mouse_x = x_int + self.mouse_y = y_int + return ToolResult( + output=f"{num_clicks} {button} click(s) performed at ({x_int}, {y_int})" + ) + else: + return ToolResult( + error=f"Failed to click: {result.get('error', 'Unknown error')}" + ) + elif action == "scroll": + if amount is None: + return ToolResult(error="Scroll amount is required") + amount = int(float(amount)) + amount = max(-10, min(10, amount)) + result = await self._api_request( + "POST", + "/automation/mouse/scroll", + {"clicks": amount, "x": self.mouse_x, "y": self.mouse_y}, + ) + if result.get("success", False): + direction = "up" if amount > 0 else "down" + steps = abs(amount) + return ToolResult( + output=f"Scrolled {direction} {steps} step(s) at position ({self.mouse_x}, {self.mouse_y})" + ) + else: + return ToolResult( + error=f"Failed to scroll: {result.get('error', 'Unknown error')}" + ) + elif action == "typing": + if text is None: + return ToolResult(error="Text is required for typing") + text = str(text) + result = await self._api_request( + "POST", + "/automation/keyboard/write", + {"message": text, "interval": 0.01}, + ) + if result.get("success", False): + return ToolResult(output=f"Typed: {text}") + else: + return ToolResult( + error=f"Failed to type: {result.get('error', 'Unknown error')}" + ) + elif action == "press": + if key is None: + return ToolResult(error="Key is required for press action") + key = str(key).lower() + result = await self._api_request( + "POST", "/automation/keyboard/press", {"keys": key, "presses": 1} + ) + if result.get("success", False): + return ToolResult(output=f"Pressed key: {key}") + else: + return ToolResult( + error=f"Failed to press key: {result.get('error', 'Unknown error')}" + ) + elif action == "wait": + duration = float(duration) + duration = max(0, min(10, duration)) + await asyncio.sleep(duration) + return ToolResult(output=f"Waited {duration} seconds") + elif action == "mouse_down": + x_val = x if x is not None else self.mouse_x + y_val = y if y is not None else self.mouse_y + x_int = int(round(float(x_val))) + y_int = int(round(float(y_val))) + result = await self._api_request( + "POST", + "/automation/mouse/down", + {"x": x_int, "y": y_int, "button": button.lower()}, + ) + if result.get("success", False): + self.mouse_x = x_int + self.mouse_y = y_int + return ToolResult( + output=f"{button} button pressed at ({x_int}, {y_int})" + ) + else: + return ToolResult( + error=f"Failed to press button: {result.get('error', 'Unknown error')}" + ) + elif action == "mouse_up": + x_val = x if x is not None else self.mouse_x + y_val = y if y is not None else self.mouse_y + x_int = int(round(float(x_val))) + y_int = int(round(float(y_val))) + result = await self._api_request( + "POST", + "/automation/mouse/up", + {"x": x_int, "y": y_int, "button": button.lower()}, + ) + if result.get("success", False): + self.mouse_x = x_int + self.mouse_y = y_int + return ToolResult( + output=f"{button} button released at ({x_int}, {y_int})" + ) + else: + return ToolResult( + error=f"Failed to release button: {result.get('error', 'Unknown error')}" + ) + elif action == "drag_to": + if x is None or y is None: + return ToolResult(error="x and y coordinates are required") + target_x = int(round(float(x))) + target_y = int(round(float(y))) + start_x = self.mouse_x + start_y = self.mouse_y + result = await self._api_request( + "POST", + "/automation/mouse/drag", + {"x": target_x, "y": target_y, "duration": 0.3, "button": "left"}, + ) + if result.get("success", False): + self.mouse_x = target_x + self.mouse_y = target_y + return ToolResult( + output=f"Dragged from ({start_x}, {start_y}) to ({target_x}, {target_y})" + ) + else: + return ToolResult( + error=f"Failed to drag: {result.get('error', 'Unknown error')}" + ) + elif action == "hotkey": + if keys is None: + return ToolResult(error="Keys are required for hotkey action") + keys = str(keys).lower().strip() + key_sequence = keys.split("+") + result = await self._api_request( + "POST", + "/automation/keyboard/hotkey", + {"keys": key_sequence, "interval": 0.01}, + ) + if result.get("success", False): + return ToolResult(output=f"Pressed key combination: {keys}") + else: + return ToolResult( + error=f"Failed to press keys: {result.get('error', 'Unknown error')}" + ) + elif action == "screenshot": + result = await self._api_request("POST", "/automation/screenshot") + if "image" in result: + base64_str = result["image"] + timestamp = time.strftime("%Y%m%d_%H%M%S") + # Save screenshot to file + screenshots_dir = "screenshots" + if not os.path.exists(screenshots_dir): + os.makedirs(screenshots_dir) + timestamped_filename = os.path.join( + screenshots_dir, f"screenshot_{timestamp}.png" + ) + latest_filename = "latest_screenshot.png" + # Decode base64 string and save to file + img_data = base64.b64decode(base64_str) + with open(timestamped_filename, "wb") as f: + f.write(img_data) + # Save a copy as the latest screenshot + with open(latest_filename, "wb") as f: + f.write(img_data) + return ToolResult( + output=f"Screenshot saved as {timestamped_filename}", + base64_image=base64_str, + ) + else: + return ToolResult(error="Failed to capture screenshot") + else: + return ToolResult(error=f"Unknown action: {action}") + except Exception as e: + return ToolResult(error=f"Computer action failed: {str(e)}") + + async def cleanup(self): + """Clean up resources.""" + if self.session and not self.session.closed: + await self.session.close() + self.session = None + + def __del__(self): + """Ensure cleanup on destruction.""" + if hasattr(self, "session") and self.session is not None: + try: + asyncio.run(self.cleanup()) + except RuntimeError: + loop = asyncio.new_event_loop() + loop.run_until_complete(self.cleanup()) + loop.close() diff --git a/app/tool/crawl4ai.py b/app/tool/crawl4ai.py new file mode 100644 index 0000000000000000000000000000000000000000..d0f9133684784c3dc163aca9d641aeebf95edca6 --- /dev/null +++ b/app/tool/crawl4ai.py @@ -0,0 +1,269 @@ +""" +Crawl4AI Web Crawler Tool for OpenManus + +This tool integrates Crawl4AI, a high-performance web crawler designed for LLMs and AI agents, +providing fast, precise, and AI-ready data extraction with clean Markdown generation. +""" + +import asyncio +from typing import List, Union +from urllib.parse import urlparse + +from app.logger import logger +from app.tool.base import BaseTool, ToolResult + + +class Crawl4aiTool(BaseTool): + """ + Web crawler tool powered by Crawl4AI. + + Provides clean markdown extraction optimized for AI processing. + """ + + name: str = "crawl4ai" + description: str = """Web crawler that extracts clean, AI-ready content from web pages. + + Features: + - Extracts clean markdown content optimized for LLMs + - Handles JavaScript-heavy sites and dynamic content + - Supports multiple URLs in a single request + - Fast and reliable with built-in error handling + + Perfect for content analysis, research, and feeding web content to AI models.""" + + parameters: dict = { + "type": "object", + "properties": { + "urls": { + "type": "array", + "items": {"type": "string"}, + "description": "(required) List of URLs to crawl. Can be a single URL or multiple URLs.", + "minItems": 1, + }, + "timeout": { + "type": "integer", + "description": "(optional) Timeout in seconds for each URL. Default is 30.", + "default": 30, + "minimum": 5, + "maximum": 120, + }, + "bypass_cache": { + "type": "boolean", + "description": "(optional) Whether to bypass cache and fetch fresh content. Default is false.", + "default": False, + }, + "word_count_threshold": { + "type": "integer", + "description": "(optional) Minimum word count for content blocks. Default is 10.", + "default": 10, + "minimum": 1, + }, + }, + "required": ["urls"], + } + + async def execute( + self, + urls: Union[str, List[str]], + timeout: int = 30, + bypass_cache: bool = False, + word_count_threshold: int = 10, + ) -> ToolResult: + """ + Execute web crawling for the specified URLs. + + Args: + urls: Single URL string or list of URLs to crawl + timeout: Timeout in seconds for each URL + bypass_cache: Whether to bypass cache + word_count_threshold: Minimum word count for content blocks + + Returns: + ToolResult with crawl results + """ + # Normalize URLs to list + if isinstance(urls, str): + url_list = [urls] + else: + url_list = urls + + # Validate URLs + valid_urls = [] + for url in url_list: + if self._is_valid_url(url): + valid_urls.append(url) + else: + logger.warning(f"Invalid URL skipped: {url}") + + if not valid_urls: + return ToolResult(error="No valid URLs provided") + + try: + # Import crawl4ai components + from crawl4ai import ( + AsyncWebCrawler, + BrowserConfig, + CacheMode, + CrawlerRunConfig, + ) + + # Configure browser settings + browser_config = BrowserConfig( + headless=True, + verbose=False, + browser_type="chromium", + ignore_https_errors=True, + java_script_enabled=True, + ) + + # Configure crawler settings + run_config = CrawlerRunConfig( + cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED, + word_count_threshold=word_count_threshold, + process_iframes=True, + remove_overlay_elements=True, + excluded_tags=["script", "style"], + page_timeout=timeout * 1000, # Convert to milliseconds + verbose=False, + wait_until="domcontentloaded", + ) + + results = [] + successful_count = 0 + failed_count = 0 + + # Process each URL + async with AsyncWebCrawler(config=browser_config) as crawler: + for url in valid_urls: + try: + logger.info(f"🕷️ Crawling URL: {url}") + start_time = asyncio.get_event_loop().time() + + result = await crawler.arun(url=url, config=run_config) + + end_time = asyncio.get_event_loop().time() + execution_time = end_time - start_time + + if result.success: + # Count words in markdown + word_count = 0 + if hasattr(result, "markdown") and result.markdown: + word_count = len(result.markdown.split()) + + # Count links + links_count = 0 + if hasattr(result, "links") and result.links: + internal_links = result.links.get("internal", []) + external_links = result.links.get("external", []) + links_count = len(internal_links) + len(external_links) + + # Count images + images_count = 0 + if hasattr(result, "media") and result.media: + images = result.media.get("images", []) + images_count = len(images) + + results.append( + { + "url": url, + "success": True, + "status_code": getattr(result, "status_code", 200), + "title": result.metadata.get("title") + if result.metadata + else None, + "markdown": result.markdown + if hasattr(result, "markdown") + else None, + "word_count": word_count, + "links_count": links_count, + "images_count": images_count, + "execution_time": execution_time, + } + ) + successful_count += 1 + logger.info( + f"✅ Successfully crawled {url} in {execution_time:.2f}s" + ) + + else: + results.append( + { + "url": url, + "success": False, + "error_message": getattr( + result, "error_message", "Unknown error" + ), + "execution_time": execution_time, + } + ) + failed_count += 1 + logger.warning(f"❌ Failed to crawl {url}") + + except Exception as e: + error_msg = f"Error crawling {url}: {str(e)}" + logger.error(error_msg) + results.append( + {"url": url, "success": False, "error_message": error_msg} + ) + failed_count += 1 + + # Format output + output_lines = [f"🕷️ Crawl4AI Results Summary:"] + output_lines.append(f"📊 Total URLs: {len(valid_urls)}") + output_lines.append(f"✅ Successful: {successful_count}") + output_lines.append(f"❌ Failed: {failed_count}") + output_lines.append("") + + for i, result in enumerate(results, 1): + output_lines.append(f"{i}. {result['url']}") + + if result["success"]: + output_lines.append( + f" ✅ Status: Success (HTTP {result.get('status_code', 'N/A')})" + ) + if result.get("title"): + output_lines.append(f" 📄 Title: {result['title']}") + + if result.get("markdown"): + # Show first 300 characters of markdown content + content_preview = result["markdown"] + if len(result["markdown"]) > 300: + content_preview += "..." + output_lines.append(f" 📝 Content: {content_preview}") + + output_lines.append( + f" 📊 Stats: {result.get('word_count', 0)} words, {result.get('links_count', 0)} links, {result.get('images_count', 0)} images" + ) + + if result.get("execution_time"): + output_lines.append( + f" ⏱️ Time: {result['execution_time']:.2f}s" + ) + else: + output_lines.append(f" ❌ Status: Failed") + if result.get("error_message"): + output_lines.append(f" 🚫 Error: {result['error_message']}") + + output_lines.append("") + + return ToolResult(output="\n".join(output_lines)) + + except ImportError: + error_msg = "Crawl4AI is not installed. Please install it with: pip install crawl4ai" + logger.error(error_msg) + return ToolResult(error=error_msg) + except Exception as e: + error_msg = f"Crawl4AI execution failed: {str(e)}" + logger.error(error_msg) + return ToolResult(error=error_msg) + + def _is_valid_url(self, url: str) -> bool: + """Validate if a URL is properly formatted.""" + try: + result = urlparse(url) + return all([result.scheme, result.netloc]) and result.scheme in [ + "http", + "https", + ] + except Exception: + return False diff --git a/app/tool/create_chat_completion.py b/app/tool/create_chat_completion.py new file mode 100644 index 0000000000000000000000000000000000000000..882a5bebff0b80d803b7713a3107f7655ec6e3eb --- /dev/null +++ b/app/tool/create_chat_completion.py @@ -0,0 +1,169 @@ +from typing import Any, List, Optional, Type, Union, get_args, get_origin + +from pydantic import BaseModel, Field + +from app.tool import BaseTool + + +class CreateChatCompletion(BaseTool): + name: str = "create_chat_completion" + description: str = ( + "Creates a structured completion with specified output formatting." + ) + + # Type mapping for JSON schema + type_mapping: dict = { + str: "string", + int: "integer", + float: "number", + bool: "boolean", + dict: "object", + list: "array", + } + response_type: Optional[Type] = None + required: List[str] = Field(default_factory=lambda: ["response"]) + + def __init__(self, response_type: Optional[Type] = str): + """Initialize with a specific response type.""" + super().__init__() + self.response_type = response_type + self.parameters = self._build_parameters() + + def _build_parameters(self) -> dict: + """Build parameters schema based on response type.""" + if self.response_type == str: + return { + "type": "object", + "properties": { + "response": { + "type": "string", + "description": "The response text that should be delivered to the user.", + }, + }, + "required": self.required, + } + + if isinstance(self.response_type, type) and issubclass( + self.response_type, BaseModel + ): + schema = self.response_type.model_json_schema() + return { + "type": "object", + "properties": schema["properties"], + "required": schema.get("required", self.required), + } + + return self._create_type_schema(self.response_type) + + def _create_type_schema(self, type_hint: Type) -> dict: + """Create a JSON schema for the given type.""" + origin = get_origin(type_hint) + args = get_args(type_hint) + + # Handle primitive types + if origin is None: + return { + "type": "object", + "properties": { + "response": { + "type": self.type_mapping.get(type_hint, "string"), + "description": f"Response of type {type_hint.__name__}", + } + }, + "required": self.required, + } + + # Handle List type + if origin is list: + item_type = args[0] if args else Any + return { + "type": "object", + "properties": { + "response": { + "type": "array", + "items": self._get_type_info(item_type), + } + }, + "required": self.required, + } + + # Handle Dict type + if origin is dict: + value_type = args[1] if len(args) > 1 else Any + return { + "type": "object", + "properties": { + "response": { + "type": "object", + "additionalProperties": self._get_type_info(value_type), + } + }, + "required": self.required, + } + + # Handle Union type + if origin is Union: + return self._create_union_schema(args) + + return self._build_parameters() + + def _get_type_info(self, type_hint: Type) -> dict: + """Get type information for a single type.""" + if isinstance(type_hint, type) and issubclass(type_hint, BaseModel): + return type_hint.model_json_schema() + + return { + "type": self.type_mapping.get(type_hint, "string"), + "description": f"Value of type {getattr(type_hint, '__name__', 'any')}", + } + + def _create_union_schema(self, types: tuple) -> dict: + """Create schema for Union types.""" + return { + "type": "object", + "properties": { + "response": {"anyOf": [self._get_type_info(t) for t in types]} + }, + "required": self.required, + } + + async def execute(self, required: list | None = None, **kwargs) -> Any: + """Execute the chat completion with type conversion. + + Args: + required: List of required field names or None + **kwargs: Response data + + Returns: + Converted response based on response_type + """ + required = required or self.required + + # Handle case when required is a list + if isinstance(required, list) and len(required) > 0: + if len(required) == 1: + required_field = required[0] + result = kwargs.get(required_field, "") + else: + # Return multiple fields as a dictionary + return {field: kwargs.get(field, "") for field in required} + else: + required_field = "response" + result = kwargs.get(required_field, "") + + # Type conversion logic + if self.response_type == str: + return result + + if isinstance(self.response_type, type) and issubclass( + self.response_type, BaseModel + ): + return self.response_type(**kwargs) + + if get_origin(self.response_type) in (list, dict): + return result # Assuming result is already in correct format + + try: + return self.response_type(result) + except (ValueError, TypeError): + return result diff --git a/app/tool/file_operators.py b/app/tool/file_operators.py new file mode 100644 index 0000000000000000000000000000000000000000..dd64c838699daa3155d0c05ab1b274ca9d35e880 --- /dev/null +++ b/app/tool/file_operators.py @@ -0,0 +1,158 @@ +"""File operation interfaces and implementations for local and sandbox environments.""" + +import asyncio +from pathlib import Path +from typing import Optional, Protocol, Tuple, Union, runtime_checkable + +from app.config import SandboxSettings +from app.exceptions import ToolError +from app.sandbox.client import SANDBOX_CLIENT + + +PathLike = Union[str, Path] + + +@runtime_checkable +class FileOperator(Protocol): + """Interface for file operations in different environments.""" + + async def read_file(self, path: PathLike) -> str: + """Read content from a file.""" + ... + + async def write_file(self, path: PathLike, content: str) -> None: + """Write content to a file.""" + ... + + async def is_directory(self, path: PathLike) -> bool: + """Check if path points to a directory.""" + ... + + async def exists(self, path: PathLike) -> bool: + """Check if path exists.""" + ... + + async def run_command( + self, cmd: str, timeout: Optional[float] = 120.0 + ) -> Tuple[int, str, str]: + """Run a shell command and return (return_code, stdout, stderr).""" + ... + + +class LocalFileOperator(FileOperator): + """File operations implementation for local filesystem.""" + + encoding: str = "utf-8" + + async def read_file(self, path: PathLike) -> str: + """Read content from a local file.""" + try: + return Path(path).read_text(encoding=self.encoding) + except Exception as e: + raise ToolError(f"Failed to read {path}: {str(e)}") from None + + async def write_file(self, path: PathLike, content: str) -> None: + """Write content to a local file.""" + try: + Path(path).write_text(content, encoding=self.encoding) + except Exception as e: + raise ToolError(f"Failed to write to {path}: {str(e)}") from None + + async def is_directory(self, path: PathLike) -> bool: + """Check if path points to a directory.""" + return Path(path).is_dir() + + async def exists(self, path: PathLike) -> bool: + """Check if path exists.""" + return Path(path).exists() + + async def run_command( + self, cmd: str, timeout: Optional[float] = 120.0 + ) -> Tuple[int, str, str]: + """Run a shell command locally.""" + process = await asyncio.create_subprocess_shell( + cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE + ) + + try: + stdout, stderr = await asyncio.wait_for( + process.communicate(), timeout=timeout + ) + return ( + process.returncode or 0, + stdout.decode(), + stderr.decode(), + ) + except asyncio.TimeoutError as exc: + try: + process.kill() + except ProcessLookupError: + pass + raise TimeoutError( + f"Command '{cmd}' timed out after {timeout} seconds" + ) from exc + + +class SandboxFileOperator(FileOperator): + """File operations implementation for sandbox environment.""" + + def __init__(self): + self.sandbox_client = SANDBOX_CLIENT + + async def _ensure_sandbox_initialized(self): + """Ensure sandbox is initialized.""" + if not self.sandbox_client.sandbox: + await self.sandbox_client.create(config=SandboxSettings()) + + async def read_file(self, path: PathLike) -> str: + """Read content from a file in sandbox.""" + await self._ensure_sandbox_initialized() + try: + return await self.sandbox_client.read_file(str(path)) + except Exception as e: + raise ToolError(f"Failed to read {path} in sandbox: {str(e)}") from None + + async def write_file(self, path: PathLike, content: str) -> None: + """Write content to a file in sandbox.""" + await self._ensure_sandbox_initialized() + try: + await self.sandbox_client.write_file(str(path), content) + except Exception as e: + raise ToolError(f"Failed to write to {path} in sandbox: {str(e)}") from None + + async def is_directory(self, path: PathLike) -> bool: + """Check if path points to a directory in sandbox.""" + await self._ensure_sandbox_initialized() + result = await self.sandbox_client.run_command( + f"test -d {path} && echo 'true' || echo 'false'" + ) + return result.strip() == "true" + + async def exists(self, path: PathLike) -> bool: + """Check if path exists in sandbox.""" + await self._ensure_sandbox_initialized() + result = await self.sandbox_client.run_command( + f"test -e {path} && echo 'true' || echo 'false'" + ) + return result.strip() == "true" + + async def run_command( + self, cmd: str, timeout: Optional[float] = 120.0 + ) -> Tuple[int, str, str]: + """Run a command in sandbox environment.""" + await self._ensure_sandbox_initialized() + try: + stdout = await self.sandbox_client.run_command( + cmd, timeout=int(timeout) if timeout else None + ) + return ( + 0, # Always return 0 since we don't have explicit return code from sandbox + stdout, + "", # No stderr capture in the current sandbox implementation + ) + except TimeoutError as exc: + raise TimeoutError( + f"Command '{cmd}' timed out after {timeout} seconds in sandbox" + ) from exc + except Exception as exc: + return 1, "", f"Error executing command in sandbox: {str(exc)}" diff --git a/app/tool/huggingface_models_tool.py b/app/tool/huggingface_models_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..c7fea3fae6a61708204f298fb265c1ea5d681ae8 --- /dev/null +++ b/app/tool/huggingface_models_tool.py @@ -0,0 +1,746 @@ +""" +Hugging Face Models Tool for OpenManus AI Agent +Tool for calling any Hugging Face model via Inference API +""" + +import asyncio +import base64 +import io +from typing import Any, Dict, List, Optional, Union + +from app.huggingface_models import HuggingFaceModelManager, ModelCategory +from app.tool.base import BaseTool + + +class HuggingFaceModelsTool(BaseTool): + """Tool for accessing Hugging Face models via Inference API""" + + def __init__(self, api_token: str): + super().__init__() + self.name = "huggingface_models" + self.description = """ + Access thousands of Hugging Face models for various AI tasks including: + - Text generation (GPT-like models, instruction-tuned models) + - Image generation (FLUX, Stable Diffusion, Qwen-Image) + - Speech recognition (Whisper, Parakeet, Canary) + - Text-to-speech (Kokoro, XTTS, VibeVoice) + - Image classification (NSFW detection, emotion recognition) + - Feature extraction (embeddings, sentence transformers) + - Translation, summarization, question answering + + Use this tool to leverage state-of-the-art AI models for any task. + """ + self.model_manager = HuggingFaceModelManager(api_token) + + async def text_generation( + self, + model_name: str, + prompt: str, + max_tokens: int = 100, + temperature: float = 0.7, + stream: bool = False, + ) -> Dict[str, Any]: + """ + Generate text using a text generation model + + Args: + model_name: Name or ID of the model (e.g., "MiniMax-M2", "GPT-OSS 20B") + prompt: Input text prompt + max_tokens: Maximum tokens to generate + temperature: Sampling temperature (0.0 to 2.0) + stream: Whether to stream the response + """ + try: + # Find model by name or ID + model = self._find_model(model_name, ModelCategory.TEXT_GENERATION) + if not model: + return {"error": f"Text generation model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_GENERATION, + prompt=prompt, + max_tokens=max_tokens, + temperature=temperature, + stream=stream, + ) + + return {"model": model.name, "model_id": model.model_id, "result": result} + + except Exception as e: + return {"error": f"Text generation failed: {str(e)}"} + + async def generate_image( + self, + model_name: str, + prompt: str, + negative_prompt: Optional[str] = None, + width: int = 1024, + height: int = 1024, + num_inference_steps: int = 20, + ) -> Dict[str, Any]: + """ + Generate image from text prompt + + Args: + model_name: Name or ID of the model (e.g., "FLUX.1 Dev", "Stable Diffusion XL") + prompt: Text description of the image + negative_prompt: What to avoid in the image + width: Image width in pixels + height: Image height in pixels + num_inference_steps: Number of denoising steps + """ + try: + model = self._find_model(model_name, ModelCategory.TEXT_TO_IMAGE) + if not model: + return {"error": f"Text-to-image model '{model_name}' not found"} + + image_bytes = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_TO_IMAGE, + prompt=prompt, + negative_prompt=negative_prompt, + width=width, + height=height, + num_inference_steps=num_inference_steps, + ) + + # Convert bytes to base64 for display + image_b64 = base64.b64encode(image_bytes).decode() + + return { + "model": model.name, + "model_id": model.model_id, + "image_base64": image_b64, + "size": f"{width}x{height}", + "prompt": prompt, + } + + except Exception as e: + return {"error": f"Image generation failed: {str(e)}"} + + async def transcribe_audio( + self, + model_name: str, + audio_data: bytes, + language: Optional[str] = None, + task: str = "transcribe", + ) -> Dict[str, Any]: + """ + Transcribe audio to text + + Args: + model_name: Name or ID of the model (e.g., "Whisper Large v3") + audio_data: Audio file as bytes + language: Source language code (e.g., "en", "es") + task: "transcribe" or "translate" + """ + try: + model = self._find_model( + model_name, ModelCategory.AUTOMATIC_SPEECH_RECOGNITION + ) + if not model: + return {"error": f"ASR model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.AUTOMATIC_SPEECH_RECOGNITION, + audio_data=audio_data, + language=language, + task=task, + ) + + return { + "model": model.name, + "model_id": model.model_id, + "transcription": result.get("text", ""), + "language": language, + "task": task, + } + + except Exception as e: + return {"error": f"Audio transcription failed: {str(e)}"} + + async def text_to_speech( + self, + model_name: str, + text: str, + voice_id: Optional[str] = None, + speed: float = 1.0, + ) -> Dict[str, Any]: + """ + Convert text to speech + + Args: + model_name: Name or ID of the model (e.g., "Kokoro 82M", "VibeVoice 1.5B") + text: Text to convert to speech + voice_id: Voice identifier (model-specific) + speed: Speech speed multiplier + """ + try: + model = self._find_model(model_name, ModelCategory.TEXT_TO_SPEECH) + if not model: + return {"error": f"TTS model '{model_name}' not found"} + + audio_bytes = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_TO_SPEECH, + text=text, + voice_id=voice_id, + speed=speed, + ) + + # Convert to base64 for transport + audio_b64 = base64.b64encode(audio_bytes).decode() + + return { + "model": model.name, + "model_id": model.model_id, + "audio_base64": audio_b64, + "text": text, + "voice_id": voice_id, + } + + except Exception as e: + return {"error": f"Text-to-speech failed: {str(e)}"} + + async def classify_image( + self, model_name: str, image_data: bytes, top_k: int = 5 + ) -> Dict[str, Any]: + """ + Classify image content + + Args: + model_name: Name or ID of the model (e.g., "NSFW Image Detection") + image_data: Image file as bytes + top_k: Number of top predictions to return + """ + try: + model = self._find_model(model_name, ModelCategory.IMAGE_CLASSIFICATION) + if not model: + return {"error": f"Image classification model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.IMAGE_CLASSIFICATION, + image_data=image_data, + top_k=top_k, + ) + + return { + "model": model.name, + "model_id": model.model_id, + "predictions": result, + "top_k": top_k, + } + + except Exception as e: + return {"error": f"Image classification failed: {str(e)}"} + + async def get_embeddings( + self, model_name: str, texts: Union[str, List[str]] + ) -> Dict[str, Any]: + """ + Extract embeddings from text + + Args: + model_name: Name or ID of the model (e.g., "Sentence Transformers All MiniLM") + texts: Text or list of texts to embed + """ + try: + model = self._find_model(model_name, ModelCategory.FEATURE_EXTRACTION) + if not model: + return {"error": f"Feature extraction model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, ModelCategory.FEATURE_EXTRACTION, texts=texts + ) + + return { + "model": model.name, + "model_id": model.model_id, + "embeddings": result, + "input_count": len(texts) if isinstance(texts, list) else 1, + } + + except Exception as e: + return {"error": f"Feature extraction failed: {str(e)}"} + + async def translate_text( + self, + model_name: str, + text: str, + source_language: Optional[str] = None, + target_language: Optional[str] = None, + ) -> Dict[str, Any]: + """ + Translate text between languages + + Args: + model_name: Name or ID of the model (e.g., "M2M100 1.2B") + text: Text to translate + source_language: Source language code + target_language: Target language code + """ + try: + model = self._find_model(model_name, ModelCategory.TRANSLATION) + if not model: + return {"error": f"Translation model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.TRANSLATION, + text=text, + src_lang=source_language, + tgt_lang=target_language, + ) + + return { + "model": model.name, + "model_id": model.model_id, + "translation": result, + "source_language": source_language, + "target_language": target_language, + "original_text": text, + } + + except Exception as e: + return {"error": f"Translation failed: {str(e)}"} + + async def summarize_text( + self, model_name: str, text: str, max_length: int = 150, min_length: int = 30 + ) -> Dict[str, Any]: + """ + Summarize long text + + Args: + model_name: Name or ID of the model (e.g., "PEGASUS XSum") + text: Text to summarize + max_length: Maximum summary length + min_length: Minimum summary length + """ + try: + model = self._find_model(model_name, ModelCategory.SUMMARIZATION) + if not model: + return {"error": f"Summarization model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.SUMMARIZATION, + text=text, + max_length=max_length, + min_length=min_length, + ) + + return { + "model": model.name, + "model_id": model.model_id, + "summary": result, + "original_length": len(text), + "summary_length": ( + len(result.get("summary_text", "")) + if isinstance(result, dict) + else len(str(result)) + ), + } + + except Exception as e: + return {"error": f"Summarization failed: {str(e)}"} + + async def answer_question( + self, model_name: str, question: str, context: str + ) -> Dict[str, Any]: + """ + Answer questions based on context + + Args: + model_name: Name or ID of the model + question: Question to answer + context: Context containing the answer + """ + try: + # Use a text generation model for question answering + model = self._find_model(model_name, ModelCategory.TEXT_GENERATION) + if not model: + return {"error": f"Question answering model '{model_name}' not found"} + + # Format as instruction + prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:" + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_GENERATION, + prompt=prompt, + max_tokens=200, + temperature=0.3, + ) + + return { + "model": model.name, + "model_id": model.model_id, + "answer": result, + "question": question, + "context_length": len(context), + } + + except Exception as e: + return {"error": f"Question answering failed: {str(e)}"} + + def list_available_models(self, category: Optional[str] = None) -> Dict[str, Any]: + """ + List all available models by category + + Args: + category: Specific category to filter (optional) + """ + try: + if category: + cat_enum = ModelCategory(category.lower().replace("-", "_")) + models = self.model_manager.get_models_by_category(cat_enum) + return { + "category": category, + "models": [ + { + "name": model.name, + "model_id": model.model_id, + "description": model.description, + "endpoint_compatible": model.endpoint_compatible, + "requires_auth": model.requires_auth, + } + for model in models + ], + } + else: + all_models = self.model_manager.get_all_models() + return { + "categories": { + cat.value: [ + { + "name": model.name, + "model_id": model.model_id, + "description": model.description, + "endpoint_compatible": model.endpoint_compatible, + "requires_auth": model.requires_auth, + } + for model in models + ] + for cat, models in all_models.items() + } + } + except Exception as e: + return {"error": f"Failed to list models: {str(e)}"} + + def _find_model(self, model_name: str, category: ModelCategory): + """Find a model by name or ID within a category""" + models = self.model_manager.get_models_by_category(category) + + # Try exact name match first + for model in models: + if model.name.lower() == model_name.lower(): + return model + + # Try model ID match + for model in models: + if model.model_id.lower() == model_name.lower(): + return model + + # Try partial name match + for model in models: + if model_name.lower() in model.name.lower(): + return model + + return None + + async def execute(self, **kwargs) -> Dict[str, Any]: + """Execute the Hugging Face models tool""" + action = kwargs.get("action", "list_models") + + if action == "text_generation": + return await self.text_generation( + kwargs.get("model_name"), + kwargs.get("prompt"), + kwargs.get("max_tokens", 100), + kwargs.get("temperature", 0.7), + kwargs.get("stream", False), + ) + elif action == "generate_image": + return await self.generate_image( + kwargs.get("model_name"), + kwargs.get("prompt"), + kwargs.get("negative_prompt"), + kwargs.get("width", 1024), + kwargs.get("height", 1024), + kwargs.get("num_inference_steps", 20), + ) + elif action == "transcribe_audio": + return await self.transcribe_audio( + kwargs.get("model_name"), + kwargs.get("audio_data"), + kwargs.get("language"), + kwargs.get("task", "transcribe"), + ) + elif action == "text_to_speech": + return await self.text_to_speech( + kwargs.get("model_name"), + kwargs.get("text"), + kwargs.get("voice_id"), + kwargs.get("speed", 1.0), + ) + elif action == "classify_image": + return await self.classify_image( + kwargs.get("model_name"), + kwargs.get("image_data"), + kwargs.get("top_k", 5), + ) + elif action == "get_embeddings": + return await self.get_embeddings( + kwargs.get("model_name"), kwargs.get("texts") + ) + elif action == "translate_text": + return await self.translate_text( + kwargs.get("model_name"), + kwargs.get("text"), + kwargs.get("source_language"), + kwargs.get("target_language"), + ) + elif action == "summarize_text": + return await self.summarize_text( + kwargs.get("model_name"), + kwargs.get("text"), + kwargs.get("max_length", 150), + kwargs.get("min_length", 30), + ) + elif action == "answer_question": + return await self.answer_question( + kwargs.get("model_name"), kwargs.get("question"), kwargs.get("context") + ) + elif action == "list_models": + return self.list_available_models(kwargs.get("category")) + + # New expanded actions + elif action == "text_to_video": + return await self.text_to_video( + kwargs.get("model_name"), kwargs.get("prompt"), **kwargs + ) + elif action == "code_generation": + return await self.code_generation( + kwargs.get("model_name"), kwargs.get("prompt"), **kwargs + ) + elif action == "text_to_3d": + return await self.text_to_3d( + kwargs.get("model_name"), kwargs.get("prompt"), **kwargs + ) + elif action == "ocr": + return await self.ocr( + kwargs.get("model_name"), kwargs.get("image_data"), **kwargs + ) + elif action == "document_analysis": + return await self.document_analysis( + kwargs.get("model_name"), kwargs.get("document_data"), **kwargs + ) + elif action == "vision_language": + return await self.vision_language( + kwargs.get("model_name"), + kwargs.get("image_data"), + kwargs.get("text"), + **kwargs, + ) + elif action == "music_generation": + return await self.music_generation( + kwargs.get("model_name"), kwargs.get("prompt"), **kwargs + ) + elif action == "creative_writing": + return await self.creative_writing( + kwargs.get("model_name"), kwargs.get("prompt"), **kwargs + ) + elif action == "business_document": + return await self.business_document( + kwargs.get("model_name"), + kwargs.get("document_type"), + kwargs.get("context"), + **kwargs, + ) + else: + return {"error": f"Unknown action: {action}"} + + # New methods for expanded model categories + + async def text_to_video( + self, model_name: str, prompt: str, duration: int = 5, fps: int = 24, **kwargs + ) -> Dict[str, Any]: + """Generate video from text prompt""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_TO_VIDEO, + prompt=prompt, + duration=duration, + fps=fps, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def code_generation( + self, model_name: str, prompt: str, language: str = "python", **kwargs + ) -> Dict[str, Any]: + """Generate code from natural language description""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.CODE_GENERATION, + prompt=prompt, + language=language, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def text_to_3d( + self, model_name: str, prompt: str, resolution: int = 64, **kwargs + ) -> Dict[str, Any]: + """Generate 3D model from text description""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.TEXT_TO_3D, + prompt=prompt, + resolution=resolution, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def ocr( + self, model_name: str, image_data: bytes, language: str = "en", **kwargs + ) -> Dict[str, Any]: + """Perform OCR on image""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.OCR, + image_data=image_data, + language=language, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def document_analysis( + self, model_name: str, document_data: bytes, **kwargs + ) -> Dict[str, Any]: + """Analyze document structure and content""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.DOCUMENT_ANALYSIS, + document_data=document_data, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def vision_language( + self, model_name: str, image_data: bytes, text: str, **kwargs + ) -> Dict[str, Any]: + """Process image and text together using multimodal models""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.VISION_LANGUAGE, + image_data=image_data, + text=text, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def music_generation( + self, model_name: str, prompt: str, duration: int = 30, **kwargs + ) -> Dict[str, Any]: + """Generate music from text description""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.MUSIC_GENERATION, + prompt=prompt, + duration=duration, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def creative_writing( + self, model_name: str, prompt: str, content_type: str = "story", **kwargs + ) -> Dict[str, Any]: + """Generate creative content""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + enhanced_prompt = f"Write a {content_type}: {prompt}" + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.CREATIVE_WRITING, + prompt=enhanced_prompt, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} + + async def business_document( + self, model_name: str, document_type: str, context: str, **kwargs + ) -> Dict[str, Any]: + """Generate business documents""" + try: + model = self._get_model_by_name(model_name) + if not model: + return {"error": f"Model '{model_name}' not found"} + + result = await self.model_manager.call_model( + model.model_id, + ModelCategory.EMAIL_GENERATION, # Generic business category + document_type=document_type, + context=context, + **kwargs, + ) + return {"success": True, "result": result} + except Exception as e: + return {"error": str(e)} diff --git a/app/tool/mcp.py b/app/tool/mcp.py new file mode 100644 index 0000000000000000000000000000000000000000..32fa8249e59ce36b12a8e947e91ce2d892c376f7 --- /dev/null +++ b/app/tool/mcp.py @@ -0,0 +1,194 @@ +from contextlib import AsyncExitStack +from typing import Dict, List, Optional + +from mcp import ClientSession, StdioServerParameters +from mcp.client.sse import sse_client +from mcp.client.stdio import stdio_client +from mcp.types import ListToolsResult, TextContent + +from app.logger import logger +from app.tool.base import BaseTool, ToolResult +from app.tool.tool_collection import ToolCollection + + +class MCPClientTool(BaseTool): + """Represents a tool proxy that can be called on the MCP server from the client side.""" + + session: Optional[ClientSession] = None + server_id: str = "" # Add server identifier + original_name: str = "" + + async def execute(self, **kwargs) -> ToolResult: + """Execute the tool by making a remote call to the MCP server.""" + if not self.session: + return ToolResult(error="Not connected to MCP server") + + try: + logger.info(f"Executing tool: {self.original_name}") + result = await self.session.call_tool(self.original_name, kwargs) + content_str = ", ".join( + item.text for item in result.content if isinstance(item, TextContent) + ) + return ToolResult(output=content_str or "No output returned.") + except Exception as e: + return ToolResult(error=f"Error executing tool: {str(e)}") + + +class MCPClients(ToolCollection): + """ + A collection of tools that connects to multiple MCP servers and manages available tools through the Model Context Protocol. + """ + + sessions: Dict[str, ClientSession] = {} + exit_stacks: Dict[str, AsyncExitStack] = {} + description: str = "MCP client tools for server interaction" + + def __init__(self): + super().__init__() # Initialize with empty tools list + self.name = "mcp" # Keep name for backward compatibility + + async def connect_sse(self, server_url: str, server_id: str = "") -> None: + """Connect to an MCP server using SSE transport.""" + if not server_url: + raise ValueError("Server URL is required.") + + server_id = server_id or server_url + + # Always ensure clean disconnection before new connection + if server_id in self.sessions: + await self.disconnect(server_id) + + exit_stack = AsyncExitStack() + self.exit_stacks[server_id] = exit_stack + + streams_context = sse_client(url=server_url) + streams = await exit_stack.enter_async_context(streams_context) + session = await exit_stack.enter_async_context(ClientSession(*streams)) + self.sessions[server_id] = session + + await self._initialize_and_list_tools(server_id) + + async def connect_stdio( + self, command: str, args: List[str], server_id: str = "" + ) -> None: + """Connect to an MCP server using stdio transport.""" + if not command: + raise ValueError("Server command is required.") + + server_id = server_id or command + + # Always ensure clean disconnection before new connection + if server_id in self.sessions: + await self.disconnect(server_id) + + exit_stack = AsyncExitStack() + self.exit_stacks[server_id] = exit_stack + + server_params = StdioServerParameters(command=command, args=args) + stdio_transport = await exit_stack.enter_async_context( + stdio_client(server_params) + ) + read, write = stdio_transport + session = await exit_stack.enter_async_context(ClientSession(read, write)) + self.sessions[server_id] = session + + await self._initialize_and_list_tools(server_id) + + async def _initialize_and_list_tools(self, server_id: str) -> None: + """Initialize session and populate tool map.""" + session = self.sessions.get(server_id) + if not session: + raise RuntimeError(f"Session not initialized for server {server_id}") + + await session.initialize() + response = await session.list_tools() + + # Create proper tool objects for each server tool + for tool in response.tools: + original_name = tool.name + tool_name = f"mcp_{server_id}_{original_name}" + tool_name = self._sanitize_tool_name(tool_name) + + server_tool = MCPClientTool( + name=tool_name, + description=tool.description, + parameters=tool.inputSchema, + session=session, + server_id=server_id, + original_name=original_name, + ) + self.tool_map[tool_name] = server_tool + + # Update tools tuple + self.tools = tuple(self.tool_map.values()) + logger.info( + f"Connected to server {server_id} with tools: {[tool.name for tool in response.tools]}" + ) + + def _sanitize_tool_name(self, name: str) -> str: + """Sanitize tool name to match MCPClientTool requirements.""" + import re + + # Replace invalid characters with underscores + sanitized = re.sub(r"[^a-zA-Z0-9_-]", "_", name) + + # Remove consecutive underscores + sanitized = re.sub(r"_+", "_", sanitized) + + # Remove leading/trailing underscores + sanitized = sanitized.strip("_") + + # Truncate to 64 characters if needed + if len(sanitized) > 64: + sanitized = sanitized[:64] + + return sanitized + + async def list_tools(self) -> ListToolsResult: + """List all available tools.""" + tools_result = ListToolsResult(tools=[]) + for session in self.sessions.values(): + response = await session.list_tools() + tools_result.tools += response.tools + return tools_result + + async def disconnect(self, server_id: str = "") -> None: + """Disconnect from a specific MCP server or all servers if no server_id provided.""" + if server_id: + if server_id in self.sessions: + try: + exit_stack = self.exit_stacks.get(server_id) + + # Close the exit stack which will handle session cleanup + if exit_stack: + try: + await exit_stack.aclose() + except RuntimeError as e: + if "cancel scope" in str(e).lower(): + logger.warning( + f"Cancel scope error during disconnect from {server_id}, continuing with cleanup: {e}" + ) + else: + raise + + # Clean up references + self.sessions.pop(server_id, None) + self.exit_stacks.pop(server_id, None) + + # Remove tools associated with this server + self.tool_map = { + k: v + for k, v in self.tool_map.items() + if v.server_id != server_id + } + self.tools = tuple(self.tool_map.values()) + logger.info(f"Disconnected from MCP server {server_id}") + except Exception as e: + logger.error(f"Error disconnecting from server {server_id}: {e}") + else: + # Disconnect from all servers in a deterministic order + for sid in sorted(list(self.sessions.keys())): + await self.disconnect(sid) + self.tool_map = {} + self.tools = tuple() + logger.info("Disconnected from all MCP servers") diff --git a/app/tool/planning.py b/app/tool/planning.py new file mode 100644 index 0000000000000000000000000000000000000000..47e334d6632cd18373a48fced78bb21662952217 --- /dev/null +++ b/app/tool/planning.py @@ -0,0 +1,363 @@ +# tool/planning.py +from typing import Dict, List, Literal, Optional + +from app.exceptions import ToolError +from app.tool.base import BaseTool, ToolResult + + +_PLANNING_TOOL_DESCRIPTION = """ +A planning tool that allows the agent to create and manage plans for solving complex tasks. +The tool provides functionality for creating plans, updating plan steps, and tracking progress. +""" + + +class PlanningTool(BaseTool): + """ + A planning tool that allows the agent to create and manage plans for solving complex tasks. + The tool provides functionality for creating plans, updating plan steps, and tracking progress. + """ + + name: str = "planning" + description: str = _PLANNING_TOOL_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "command": { + "description": "The command to execute. Available commands: create, update, list, get, set_active, mark_step, delete.", + "enum": [ + "create", + "update", + "list", + "get", + "set_active", + "mark_step", + "delete", + ], + "type": "string", + }, + "plan_id": { + "description": "Unique identifier for the plan. Required for create, update, set_active, and delete commands. Optional for get and mark_step (uses active plan if not specified).", + "type": "string", + }, + "title": { + "description": "Title for the plan. Required for create command, optional for update command.", + "type": "string", + }, + "steps": { + "description": "List of plan steps. Required for create command, optional for update command.", + "type": "array", + "items": {"type": "string"}, + }, + "step_index": { + "description": "Index of the step to update (0-based). Required for mark_step command.", + "type": "integer", + }, + "step_status": { + "description": "Status to set for a step. Used with mark_step command.", + "enum": ["not_started", "in_progress", "completed", "blocked"], + "type": "string", + }, + "step_notes": { + "description": "Additional notes for a step. Optional for mark_step command.", + "type": "string", + }, + }, + "required": ["command"], + "additionalProperties": False, + } + + plans: dict = {} # Dictionary to store plans by plan_id + _current_plan_id: Optional[str] = None # Track the current active plan + + async def execute( + self, + *, + command: Literal[ + "create", "update", "list", "get", "set_active", "mark_step", "delete" + ], + plan_id: Optional[str] = None, + title: Optional[str] = None, + steps: Optional[List[str]] = None, + step_index: Optional[int] = None, + step_status: Optional[ + Literal["not_started", "in_progress", "completed", "blocked"] + ] = None, + step_notes: Optional[str] = None, + **kwargs, + ): + """ + Execute the planning tool with the given command and parameters. + + Parameters: + - command: The operation to perform + - plan_id: Unique identifier for the plan + - title: Title for the plan (used with create command) + - steps: List of steps for the plan (used with create command) + - step_index: Index of the step to update (used with mark_step command) + - step_status: Status to set for a step (used with mark_step command) + - step_notes: Additional notes for a step (used with mark_step command) + """ + + if command == "create": + return self._create_plan(plan_id, title, steps) + elif command == "update": + return self._update_plan(plan_id, title, steps) + elif command == "list": + return self._list_plans() + elif command == "get": + return self._get_plan(plan_id) + elif command == "set_active": + return self._set_active_plan(plan_id) + elif command == "mark_step": + return self._mark_step(plan_id, step_index, step_status, step_notes) + elif command == "delete": + return self._delete_plan(plan_id) + else: + raise ToolError( + f"Unrecognized command: {command}. Allowed commands are: create, update, list, get, set_active, mark_step, delete" + ) + + def _create_plan( + self, plan_id: Optional[str], title: Optional[str], steps: Optional[List[str]] + ) -> ToolResult: + """Create a new plan with the given ID, title, and steps.""" + if not plan_id: + raise ToolError("Parameter `plan_id` is required for command: create") + + if plan_id in self.plans: + raise ToolError( + f"A plan with ID '{plan_id}' already exists. Use 'update' to modify existing plans." + ) + + if not title: + raise ToolError("Parameter `title` is required for command: create") + + if ( + not steps + or not isinstance(steps, list) + or not all(isinstance(step, str) for step in steps) + ): + raise ToolError( + "Parameter `steps` must be a non-empty list of strings for command: create" + ) + + # Create a new plan with initialized step statuses + plan = { + "plan_id": plan_id, + "title": title, + "steps": steps, + "step_statuses": ["not_started"] * len(steps), + "step_notes": [""] * len(steps), + } + + self.plans[plan_id] = plan + self._current_plan_id = plan_id # Set as active plan + + return ToolResult( + output=f"Plan created successfully with ID: {plan_id}\n\n{self._format_plan(plan)}" + ) + + def _update_plan( + self, plan_id: Optional[str], title: Optional[str], steps: Optional[List[str]] + ) -> ToolResult: + """Update an existing plan with new title or steps.""" + if not plan_id: + raise ToolError("Parameter `plan_id` is required for command: update") + + if plan_id not in self.plans: + raise ToolError(f"No plan found with ID: {plan_id}") + + plan = self.plans[plan_id] + + if title: + plan["title"] = title + + if steps: + if not isinstance(steps, list) or not all( + isinstance(step, str) for step in steps + ): + raise ToolError( + "Parameter `steps` must be a list of strings for command: update" + ) + + # Preserve existing step statuses for unchanged steps + old_steps = plan["steps"] + old_statuses = plan["step_statuses"] + old_notes = plan["step_notes"] + + # Create new step statuses and notes + new_statuses = [] + new_notes = [] + + for i, step in enumerate(steps): + # If the step exists at the same position in old steps, preserve status and notes + if i < len(old_steps) and step == old_steps[i]: + new_statuses.append(old_statuses[i]) + new_notes.append(old_notes[i]) + else: + new_statuses.append("not_started") + new_notes.append("") + + plan["steps"] = steps + plan["step_statuses"] = new_statuses + plan["step_notes"] = new_notes + + return ToolResult( + output=f"Plan updated successfully: {plan_id}\n\n{self._format_plan(plan)}" + ) + + def _list_plans(self) -> ToolResult: + """List all available plans.""" + if not self.plans: + return ToolResult( + output="No plans available. Create a plan with the 'create' command." + ) + + output = "Available plans:\n" + for plan_id, plan in self.plans.items(): + current_marker = " (active)" if plan_id == self._current_plan_id else "" + completed = sum( + 1 for status in plan["step_statuses"] if status == "completed" + ) + total = len(plan["steps"]) + progress = f"{completed}/{total} steps completed" + output += f"• {plan_id}{current_marker}: {plan['title']} - {progress}\n" + + return ToolResult(output=output) + + def _get_plan(self, plan_id: Optional[str]) -> ToolResult: + """Get details of a specific plan.""" + if not plan_id: + # If no plan_id is provided, use the current active plan + if not self._current_plan_id: + raise ToolError( + "No active plan. Please specify a plan_id or set an active plan." + ) + plan_id = self._current_plan_id + + if plan_id not in self.plans: + raise ToolError(f"No plan found with ID: {plan_id}") + + plan = self.plans[plan_id] + return ToolResult(output=self._format_plan(plan)) + + def _set_active_plan(self, plan_id: Optional[str]) -> ToolResult: + """Set a plan as the active plan.""" + if not plan_id: + raise ToolError("Parameter `plan_id` is required for command: set_active") + + if plan_id not in self.plans: + raise ToolError(f"No plan found with ID: {plan_id}") + + self._current_plan_id = plan_id + return ToolResult( + output=f"Plan '{plan_id}' is now the active plan.\n\n{self._format_plan(self.plans[plan_id])}" + ) + + def _mark_step( + self, + plan_id: Optional[str], + step_index: Optional[int], + step_status: Optional[str], + step_notes: Optional[str], + ) -> ToolResult: + """Mark a step with a specific status and optional notes.""" + if not plan_id: + # If no plan_id is provided, use the current active plan + if not self._current_plan_id: + raise ToolError( + "No active plan. Please specify a plan_id or set an active plan." + ) + plan_id = self._current_plan_id + + if plan_id not in self.plans: + raise ToolError(f"No plan found with ID: {plan_id}") + + if step_index is None: + raise ToolError("Parameter `step_index` is required for command: mark_step") + + plan = self.plans[plan_id] + + if step_index < 0 or step_index >= len(plan["steps"]): + raise ToolError( + f"Invalid step_index: {step_index}. Valid indices range from 0 to {len(plan['steps'])-1}." + ) + + if step_status and step_status not in [ + "not_started", + "in_progress", + "completed", + "blocked", + ]: + raise ToolError( + f"Invalid step_status: {step_status}. Valid statuses are: not_started, in_progress, completed, blocked" + ) + + if step_status: + plan["step_statuses"][step_index] = step_status + + if step_notes: + plan["step_notes"][step_index] = step_notes + + return ToolResult( + output=f"Step {step_index} updated in plan '{plan_id}'.\n\n{self._format_plan(plan)}" + ) + + def _delete_plan(self, plan_id: Optional[str]) -> ToolResult: + """Delete a plan.""" + if not plan_id: + raise ToolError("Parameter `plan_id` is required for command: delete") + + if plan_id not in self.plans: + raise ToolError(f"No plan found with ID: {plan_id}") + + del self.plans[plan_id] + + # If the deleted plan was the active plan, clear the active plan + if self._current_plan_id == plan_id: + self._current_plan_id = None + + return ToolResult(output=f"Plan '{plan_id}' has been deleted.") + + def _format_plan(self, plan: Dict) -> str: + """Format a plan for display.""" + output = f"Plan: {plan['title']} (ID: {plan['plan_id']})\n" + output += "=" * len(output) + "\n\n" + + # Calculate progress statistics + total_steps = len(plan["steps"]) + completed = sum(1 for status in plan["step_statuses"] if status == "completed") + in_progress = sum( + 1 for status in plan["step_statuses"] if status == "in_progress" + ) + blocked = sum(1 for status in plan["step_statuses"] if status == "blocked") + not_started = sum( + 1 for status in plan["step_statuses"] if status == "not_started" + ) + + output += f"Progress: {completed}/{total_steps} steps completed " + if total_steps > 0: + percentage = (completed / total_steps) * 100 + output += f"({percentage:.1f}%)\n" + else: + output += "(0%)\n" + + output += f"Status: {completed} completed, {in_progress} in progress, {blocked} blocked, {not_started} not started\n\n" + output += "Steps:\n" + + # Add each step with its status and notes + for i, (step, status, notes) in enumerate( + zip(plan["steps"], plan["step_statuses"], plan["step_notes"]) + ): + status_symbol = { + "not_started": "[ ]", + "in_progress": "[→]", + "completed": "[✓]", + "blocked": "[!]", + }.get(status, "[ ]") + + output += f"{i}. {status_symbol} {step}\n" + if notes: + output += f" Notes: {notes}\n" + + return output diff --git a/app/tool/python_execute.py b/app/tool/python_execute.py new file mode 100644 index 0000000000000000000000000000000000000000..08ceffa85f9e444d36bd07e4e6241efce6a66336 --- /dev/null +++ b/app/tool/python_execute.py @@ -0,0 +1,75 @@ +import multiprocessing +import sys +from io import StringIO +from typing import Dict + +from app.tool.base import BaseTool + + +class PythonExecute(BaseTool): + """A tool for executing Python code with timeout and safety restrictions.""" + + name: str = "python_execute" + description: str = "Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results." + parameters: dict = { + "type": "object", + "properties": { + "code": { + "type": "string", + "description": "The Python code to execute.", + }, + }, + "required": ["code"], + } + + def _run_code(self, code: str, result_dict: dict, safe_globals: dict) -> None: + original_stdout = sys.stdout + try: + output_buffer = StringIO() + sys.stdout = output_buffer + exec(code, safe_globals, safe_globals) + result_dict["observation"] = output_buffer.getvalue() + result_dict["success"] = True + except Exception as e: + result_dict["observation"] = str(e) + result_dict["success"] = False + finally: + sys.stdout = original_stdout + + async def execute( + self, + code: str, + timeout: int = 5, + ) -> Dict: + """ + Executes the provided Python code with a timeout. + + Args: + code (str): The Python code to execute. + timeout (int): Execution timeout in seconds. + + Returns: + Dict: Contains 'output' with execution output or error message and 'success' status. + """ + + with multiprocessing.Manager() as manager: + result = manager.dict({"observation": "", "success": False}) + if isinstance(__builtins__, dict): + safe_globals = {"__builtins__": __builtins__} + else: + safe_globals = {"__builtins__": __builtins__.__dict__.copy()} + proc = multiprocessing.Process( + target=self._run_code, args=(code, result, safe_globals) + ) + proc.start() + proc.join(timeout) + + # timeout process + if proc.is_alive(): + proc.terminate() + proc.join(1) + return { + "observation": f"Execution timeout after {timeout} seconds", + "success": False, + } + return dict(result) diff --git a/app/tool/sandbox/sb_browser_tool.py b/app/tool/sandbox/sb_browser_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..b3a862eddfa66cdc34e9c4ecb0edc3dcad591c34 --- /dev/null +++ b/app/tool/sandbox/sb_browser_tool.py @@ -0,0 +1,450 @@ +import base64 +import io +import json +import traceback +from typing import Optional # Add this import for Optional + +from PIL import Image +from pydantic import Field + +from app.daytona.tool_base import ( # Ensure Sandbox is imported correctly + Sandbox, + SandboxToolsBase, + ThreadMessage, +) +from app.tool.base import ToolResult +from app.utils.logger import logger + + +# Context = TypeVar("Context") +_BROWSER_DESCRIPTION = """\ +A sandbox-based browser automation tool that allows interaction with web pages through various actions. +* This tool provides commands for controlling a browser session in a sandboxed environment +* It maintains state across calls, keeping the browser session alive until explicitly closed +* Use this when you need to browse websites, fill forms, click buttons, or extract content in a secure sandbox +* Each action requires specific parameters as defined in the tool's dependencies +Key capabilities include: +* Navigation: Go to specific URLs, go back in history +* Interaction: Click elements by index, input text, send keyboard commands +* Scrolling: Scroll up/down by pixel amount or scroll to specific text +* Tab management: Switch between tabs or close tabs +* Content extraction: Get dropdown options or select dropdown options +""" + + +# noinspection PyArgumentList +class SandboxBrowserTool(SandboxToolsBase): + """Tool for executing tasks in a Daytona sandbox with browser-use capabilities.""" + + name: str = "sandbox_browser" + description: str = _BROWSER_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "navigate_to", + "go_back", + "wait", + "click_element", + "input_text", + "send_keys", + "switch_tab", + "close_tab", + "scroll_down", + "scroll_up", + "scroll_to_text", + "get_dropdown_options", + "select_dropdown_option", + "click_coordinates", + "drag_drop", + ], + "description": "The browser action to perform", + }, + "url": { + "type": "string", + "description": "URL for 'navigate_to' action", + }, + "index": { + "type": "integer", + "description": "Element index for interaction actions", + }, + "text": { + "type": "string", + "description": "Text for input or scroll actions", + }, + "amount": { + "type": "integer", + "description": "Pixel amount to scroll", + }, + "page_id": { + "type": "integer", + "description": "Tab ID for tab management actions", + }, + "keys": { + "type": "string", + "description": "Keys to send for keyboard actions", + }, + "seconds": { + "type": "integer", + "description": "Seconds to wait", + }, + "x": { + "type": "integer", + "description": "X coordinate for click or drag actions", + }, + "y": { + "type": "integer", + "description": "Y coordinate for click or drag actions", + }, + "element_source": { + "type": "string", + "description": "Source element for drag and drop", + }, + "element_target": { + "type": "string", + "description": "Target element for drag and drop", + }, + }, + "required": ["action"], + "dependencies": { + "navigate_to": ["url"], + "click_element": ["index"], + "input_text": ["index", "text"], + "send_keys": ["keys"], + "switch_tab": ["page_id"], + "close_tab": ["page_id"], + "scroll_down": ["amount"], + "scroll_up": ["amount"], + "scroll_to_text": ["text"], + "get_dropdown_options": ["index"], + "select_dropdown_option": ["index", "text"], + "click_coordinates": ["x", "y"], + "drag_drop": ["element_source", "element_target"], + "wait": ["seconds"], + }, + } + browser_message: Optional[ThreadMessage] = Field(default=None, exclude=True) + + def __init__( + self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data + ): + """Initialize with optional sandbox and thread_id.""" + super().__init__(**data) + if sandbox is not None: + self._sandbox = sandbox # Directly set the base class private attribute + + def _validate_base64_image( + self, base64_string: str, max_size_mb: int = 10 + ) -> tuple[bool, str]: + """ + Validate base64 image data. + Args: + base64_string: The base64 encoded image data + max_size_mb: Maximum allowed image size in megabytes + Returns: + Tuple of (is_valid, error_message) + """ + try: + if not base64_string or len(base64_string) < 10: + return False, "Base64 string is empty or too short" + if base64_string.startswith("data:"): + try: + base64_string = base64_string.split(",", 1)[1] + except (IndexError, ValueError): + return False, "Invalid data URL format" + import re + + if not re.match(r"^[A-Za-z0-9+/]*={0,2}$", base64_string): + return False, "Invalid base64 characters detected" + if len(base64_string) % 4 != 0: + return False, "Invalid base64 string length" + try: + image_data = base64.b64decode(base64_string, validate=True) + except Exception as e: + return False, f"Base64 decoding failed: {str(e)}" + max_size_bytes = max_size_mb * 1024 * 1024 + if len(image_data) > max_size_bytes: + return False, f"Image size exceeds limit ({max_size_bytes} bytes)" + try: + image_stream = io.BytesIO(image_data) + with Image.open(image_stream) as img: + img.verify() + supported_formats = {"JPEG", "PNG", "GIF", "BMP", "WEBP", "TIFF"} + if img.format not in supported_formats: + return False, f"Unsupported image format: {img.format}" + image_stream.seek(0) + with Image.open(image_stream) as img_check: + width, height = img_check.size + max_dimension = 8192 + if width > max_dimension or height > max_dimension: + return ( + False, + f"Image dimensions exceed limit ({max_dimension}x{max_dimension})", + ) + if width < 1 or height < 1: + return False, f"Invalid image dimensions: {width}x{height}" + except Exception as e: + return False, f"Invalid image data: {str(e)}" + return True, "Valid image" + except Exception as e: + logger.error(f"Unexpected error during base64 image validation: {e}") + return False, f"Validation error: {str(e)}" + + async def _execute_browser_action( + self, endpoint: str, params: dict = None, method: str = "POST" + ) -> ToolResult: + """Execute a browser automation action through the sandbox API.""" + try: + await self._ensure_sandbox() + url = f"http://localhost:8003/api/automation/{endpoint}" + if method == "GET" and params: + query_params = "&".join([f"{k}={v}" for k, v in params.items()]) + url = f"{url}?{query_params}" + curl_cmd = ( + f"curl -s -X {method} '{url}' -H 'Content-Type: application/json'" + ) + else: + curl_cmd = ( + f"curl -s -X {method} '{url}' -H 'Content-Type: application/json'" + ) + if params: + json_data = json.dumps(params) + curl_cmd += f" -d '{json_data}'" + logger.debug(f"Executing curl command: {curl_cmd}") + response = self.sandbox.process.exec(curl_cmd, timeout=30) + if response.exit_code == 0: + try: + result = json.loads(response.result) + result.setdefault("content", "") + result.setdefault("role", "assistant") + if "screenshot_base64" in result: + screenshot_data = result["screenshot_base64"] + is_valid, validation_message = self._validate_base64_image( + screenshot_data + ) + if not is_valid: + logger.warning( + f"Screenshot validation failed: {validation_message}" + ) + result["image_validation_error"] = validation_message + del result["screenshot_base64"] + + # added_message = await self.thread_manager.add_message( + # thread_id=self.thread_id, + # type="browser_state", + # content=result, + # is_llm_message=False + # ) + message = ThreadMessage( + type="browser_state", content=result, is_llm_message=False + ) + self.browser_message = message + success_response = { + "success": result.get("success", False), + "message": result.get("message", "Browser action completed"), + } + # if added_message and 'message_id' in added_message: + # success_response['message_id'] = added_message['message_id'] + for field in [ + "url", + "title", + "element_count", + "pixels_below", + "ocr_text", + "image_url", + ]: + if field in result: + success_response[field] = result[field] + return ( + self.success_response(success_response) + if success_response["success"] + else self.fail_response(success_response) + ) + except json.JSONDecodeError as e: + logger.error(f"Failed to parse response JSON: {e}") + return self.fail_response(f"Failed to parse response JSON: {e}") + else: + logger.error(f"Browser automation request failed: {response}") + return self.fail_response( + f"Browser automation request failed: {response}" + ) + except Exception as e: + logger.error(f"Error executing browser action: {e}") + logger.debug(traceback.format_exc()) + return self.fail_response(f"Error executing browser action: {e}") + + async def execute( + self, + action: str, + url: Optional[str] = None, + index: Optional[int] = None, + text: Optional[str] = None, + amount: Optional[int] = None, + page_id: Optional[int] = None, + keys: Optional[str] = None, + seconds: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + element_source: Optional[str] = None, + element_target: Optional[str] = None, + **kwargs, + ) -> ToolResult: + """ + Execute a browser action in the sandbox environment. + Args: + action: The browser action to perform + url: URL for navigation + index: Element index for interaction + text: Text for input or scroll actions + amount: Pixel amount to scroll + page_id: Tab ID for tab management + keys: Keys to send for keyboard actions + seconds: Seconds to wait + x: X coordinate for click/drag + y: Y coordinate for click/drag + element_source: Source element for drag and drop + element_target: Target element for drag and drop + Returns: + ToolResult with the action's output or error + """ + # async with self.lock: + try: + # Navigation actions + if action == "navigate_to": + if not url: + return self.fail_response("URL is required for navigation") + return await self._execute_browser_action("navigate_to", {"url": url}) + elif action == "go_back": + return await self._execute_browser_action("go_back", {}) + # Interaction actions + elif action == "click_element": + if index is None: + return self.fail_response("Index is required for click_element") + return await self._execute_browser_action( + "click_element", {"index": index} + ) + elif action == "input_text": + if index is None or not text: + return self.fail_response( + "Index and text are required for input_text" + ) + return await self._execute_browser_action( + "input_text", {"index": index, "text": text} + ) + elif action == "send_keys": + if not keys: + return self.fail_response("Keys are required for send_keys") + return await self._execute_browser_action("send_keys", {"keys": keys}) + # Tab management + elif action == "switch_tab": + if page_id is None: + return self.fail_response("Page ID is required for switch_tab") + return await self._execute_browser_action( + "switch_tab", {"page_id": page_id} + ) + elif action == "close_tab": + if page_id is None: + return self.fail_response("Page ID is required for close_tab") + return await self._execute_browser_action( + "close_tab", {"page_id": page_id} + ) + # Scrolling actions + elif action == "scroll_down": + params = {"amount": amount} if amount is not None else {} + return await self._execute_browser_action("scroll_down", params) + elif action == "scroll_up": + params = {"amount": amount} if amount is not None else {} + return await self._execute_browser_action("scroll_up", params) + elif action == "scroll_to_text": + if not text: + return self.fail_response("Text is required for scroll_to_text") + return await self._execute_browser_action( + "scroll_to_text", {"text": text} + ) + # Dropdown actions + elif action == "get_dropdown_options": + if index is None: + return self.fail_response( + "Index is required for get_dropdown_options" + ) + return await self._execute_browser_action( + "get_dropdown_options", {"index": index} + ) + elif action == "select_dropdown_option": + if index is None or not text: + return self.fail_response( + "Index and text are required for select_dropdown_option" + ) + return await self._execute_browser_action( + "select_dropdown_option", {"index": index, "text": text} + ) + # Coordinate-based actions + elif action == "click_coordinates": + if x is None or y is None: + return self.fail_response( + "X and Y coordinates are required for click_coordinates" + ) + return await self._execute_browser_action( + "click_coordinates", {"x": x, "y": y} + ) + elif action == "drag_drop": + if not element_source or not element_target: + return self.fail_response( + "Source and target elements are required for drag_drop" + ) + return await self._execute_browser_action( + "drag_drop", + { + "element_source": element_source, + "element_target": element_target, + }, + ) + # Utility actions + elif action == "wait": + seconds_to_wait = seconds if seconds is not None else 3 + return await self._execute_browser_action( + "wait", {"seconds": seconds_to_wait} + ) + else: + return self.fail_response(f"Unknown action: {action}") + except Exception as e: + logger.error(f"Error executing browser action: {e}") + return self.fail_response(f"Error executing browser action: {e}") + + async def get_current_state( + self, message: Optional[ThreadMessage] = None + ) -> ToolResult: + """ + Get the current browser state as a ToolResult. + If context is not provided, uses self.context. + """ + try: + # Use provided context or fall back to self.context + message = message or self.browser_message + if not message: + return ToolResult(error="Browser context not initialized") + state = message.content + screenshot = state.get("screenshot_base64") + # Build the state info with all required fields + state_info = { + "url": state.get("url", ""), + "title": state.get("title", ""), + "tabs": [tab.model_dump() for tab in state.get("tabs", [])], + "pixels_above": getattr(state, "pixels_above", 0), + "pixels_below": getattr(state, "pixels_below", 0), + "help": "[0], [1], [2], etc., represent clickable indices corresponding to the elements listed. Clicking on these indices will navigate to or interact with the respective content behind them.", + } + + return ToolResult( + output=json.dumps(state_info, indent=4, ensure_ascii=False), + base64_image=screenshot, + ) + except Exception as e: + return ToolResult(error=f"Failed to get browser state: {str(e)}") + + @classmethod + def create_with_sandbox(cls, sandbox: Sandbox) -> "SandboxBrowserTool": + """Factory method to create a tool with sandbox.""" + return cls(sandbox=sandbox) diff --git a/app/tool/sandbox/sb_files_tool.py b/app/tool/sandbox/sb_files_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..be558b0cb2ef5165fece4cf0da70dc1f9de2e846 --- /dev/null +++ b/app/tool/sandbox/sb_files_tool.py @@ -0,0 +1,361 @@ +import asyncio +from typing import Optional, TypeVar + +from pydantic import Field + +from app.daytona.tool_base import Sandbox, SandboxToolsBase +from app.tool.base import ToolResult +from app.utils.files_utils import clean_path, should_exclude_file +from app.utils.logger import logger + + +Context = TypeVar("Context") + +_FILES_DESCRIPTION = """\ +A sandbox-based file system tool that allows file operations in a secure sandboxed environment. +* This tool provides commands for creating, reading, updating, and deleting files in the workspace +* All operations are performed relative to the /workspace directory for security +* Use this when you need to manage files, edit code, or manipulate file contents in a sandbox +* Each action requires specific parameters as defined in the tool's dependencies +Key capabilities include: +* File creation: Create new files with specified content and permissions +* File modification: Replace specific strings or completely rewrite files +* File deletion: Remove files from the workspace +* File reading: Read file contents with optional line range specification +""" + + +class SandboxFilesTool(SandboxToolsBase): + name: str = "sandbox_files" + description: str = _FILES_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "create_file", + "str_replace", + "full_file_rewrite", + "delete_file", + ], + "description": "The file operation to perform", + }, + "file_path": { + "type": "string", + "description": "Path to the file, relative to /workspace (e.g., 'src/main.py')", + }, + "file_contents": { + "type": "string", + "description": "Content to write to the file", + }, + "old_str": { + "type": "string", + "description": "Text to be replaced (must appear exactly once)", + }, + "new_str": { + "type": "string", + "description": "Replacement text", + }, + "permissions": { + "type": "string", + "description": "File permissions in octal format (e.g., '644')", + "default": "644", + }, + }, + "required": ["action"], + "dependencies": { + "create_file": ["file_path", "file_contents"], + "str_replace": ["file_path", "old_str", "new_str"], + "full_file_rewrite": ["file_path", "file_contents"], + "delete_file": ["file_path"], + }, + } + SNIPPET_LINES: int = Field(default=4, exclude=True) + # workspace_path: str = Field(default="/workspace", exclude=True) + # sandbox: Optional[Sandbox] = Field(default=None, exclude=True) + + def __init__( + self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data + ): + """Initialize with optional sandbox and thread_id.""" + super().__init__(**data) + if sandbox is not None: + self._sandbox = sandbox + + def clean_path(self, path: str) -> str: + """Clean and normalize a path to be relative to /workspace""" + return clean_path(path, self.workspace_path) + + def _should_exclude_file(self, rel_path: str) -> bool: + """Check if a file should be excluded based on path, name, or extension""" + return should_exclude_file(rel_path) + + def _file_exists(self, path: str) -> bool: + """Check if a file exists in the sandbox""" + try: + self.sandbox.fs.get_file_info(path) + return True + except Exception: + return False + + async def get_workspace_state(self) -> dict: + """Get the current workspace state by reading all files""" + files_state = {} + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + files = self.sandbox.fs.list_files(self.workspace_path) + for file_info in files: + rel_path = file_info.name + + # Skip excluded files and directories + if self._should_exclude_file(rel_path) or file_info.is_dir: + continue + + try: + full_path = f"{self.workspace_path}/{rel_path}" + content = self.sandbox.fs.download_file(full_path).decode() + files_state[rel_path] = { + "content": content, + "is_dir": file_info.is_dir, + "size": file_info.size, + "modified": file_info.mod_time, + } + except Exception as e: + print(f"Error reading file {rel_path}: {e}") + except UnicodeDecodeError: + print(f"Skipping binary file: {rel_path}") + + return files_state + + except Exception as e: + print(f"Error getting workspace state: {str(e)}") + return {} + + async def execute( + self, + action: str, + file_path: Optional[str] = None, + file_contents: Optional[str] = None, + old_str: Optional[str] = None, + new_str: Optional[str] = None, + permissions: Optional[str] = "644", + **kwargs, + ) -> ToolResult: + """ + Execute a file operation in the sandbox environment. + Args: + action: The file operation to perform + file_path: Path to the file relative to /workspace + file_contents: Content to write to the file + old_str: Text to be replaced (for str_replace) + new_str: Replacement text (for str_replace) + permissions: File permissions in octal format + Returns: + ToolResult with the operation's output or error + """ + async with asyncio.Lock(): + try: + # File creation + if action == "create_file": + if not file_path or not file_contents: + return self.fail_response( + "file_path and file_contents are required for create_file" + ) + return await self._create_file( + file_path, file_contents, permissions + ) + + # String replacement + elif action == "str_replace": + if not file_path or not old_str or not new_str: + return self.fail_response( + "file_path, old_str, and new_str are required for str_replace" + ) + return await self._str_replace(file_path, old_str, new_str) + + # Full file rewrite + elif action == "full_file_rewrite": + if not file_path or not file_contents: + return self.fail_response( + "file_path and file_contents are required for full_file_rewrite" + ) + return await self._full_file_rewrite( + file_path, file_contents, permissions + ) + + # File deletion + elif action == "delete_file": + if not file_path: + return self.fail_response( + "file_path is required for delete_file" + ) + return await self._delete_file(file_path) + + else: + return self.fail_response(f"Unknown action: {action}") + + except Exception as e: + logger.error(f"Error executing file action: {e}") + return self.fail_response(f"Error executing file action: {e}") + + async def _create_file( + self, file_path: str, file_contents: str, permissions: str = "644" + ) -> ToolResult: + """Create a new file with the provided contents""" + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + file_path = self.clean_path(file_path) + full_path = f"{self.workspace_path}/{file_path}" + if self._file_exists(full_path): + return self.fail_response( + f"File '{file_path}' already exists. Use full_file_rewrite to modify existing files." + ) + + # Create parent directories if needed + parent_dir = "/".join(full_path.split("/")[:-1]) + if parent_dir: + self.sandbox.fs.create_folder(parent_dir, "755") + + # Write the file content + self.sandbox.fs.upload_file(file_contents.encode(), full_path) + self.sandbox.fs.set_file_permissions(full_path, permissions) + + message = f"File '{file_path}' created successfully." + + # Check if index.html was created and add 8080 server info (only in root workspace) + if file_path.lower() == "index.html": + try: + website_link = self.sandbox.get_preview_link(8080) + website_url = ( + website_link.url + if hasattr(website_link, "url") + else str(website_link).split("url='")[1].split("'")[0] + ) + message += f"\n\n[Auto-detected index.html - HTTP server available at: {website_url}]" + message += "\n[Note: Use the provided HTTP server URL above instead of starting a new server]" + except Exception as e: + logger.warning( + f"Failed to get website URL for index.html: {str(e)}" + ) + + return self.success_response(message) + except Exception as e: + return self.fail_response(f"Error creating file: {str(e)}") + + async def _str_replace( + self, file_path: str, old_str: str, new_str: str + ) -> ToolResult: + """Replace specific text in a file""" + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + file_path = self.clean_path(file_path) + full_path = f"{self.workspace_path}/{file_path}" + if not self._file_exists(full_path): + return self.fail_response(f"File '{file_path}' does not exist") + + content = self.sandbox.fs.download_file(full_path).decode() + old_str = old_str.expandtabs() + new_str = new_str.expandtabs() + + occurrences = content.count(old_str) + if occurrences == 0: + return self.fail_response(f"String '{old_str}' not found in file") + if occurrences > 1: + lines = [ + i + 1 + for i, line in enumerate(content.split("\n")) + if old_str in line + ] + return self.fail_response( + f"Multiple occurrences found in lines {lines}. Please ensure string is unique" + ) + + # Perform replacement + new_content = content.replace(old_str, new_str) + self.sandbox.fs.upload_file(new_content.encode(), full_path) + + # Show snippet around the edit + replacement_line = content.split(old_str)[0].count("\n") + start_line = max(0, replacement_line - self.SNIPPET_LINES) + end_line = replacement_line + self.SNIPPET_LINES + new_str.count("\n") + snippet = "\n".join(new_content.split("\n")[start_line : end_line + 1]) + + message = f"Replacement successful." + + return self.success_response(message) + + except Exception as e: + return self.fail_response(f"Error replacing string: {str(e)}") + + async def _full_file_rewrite( + self, file_path: str, file_contents: str, permissions: str = "644" + ) -> ToolResult: + """Completely rewrite an existing file with new content""" + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + file_path = self.clean_path(file_path) + full_path = f"{self.workspace_path}/{file_path}" + if not self._file_exists(full_path): + return self.fail_response( + f"File '{file_path}' does not exist. Use create_file to create a new file." + ) + + self.sandbox.fs.upload_file(file_contents.encode(), full_path) + self.sandbox.fs.set_file_permissions(full_path, permissions) + + message = f"File '{file_path}' completely rewritten successfully." + + # Check if index.html was rewritten and add 8080 server info (only in root workspace) + if file_path.lower() == "index.html": + try: + website_link = self.sandbox.get_preview_link(8080) + website_url = ( + website_link.url + if hasattr(website_link, "url") + else str(website_link).split("url='")[1].split("'")[0] + ) + message += f"\n\n[Auto-detected index.html - HTTP server available at: {website_url}]" + message += "\n[Note: Use the provided HTTP server URL above instead of starting a new server]" + except Exception as e: + logger.warning( + f"Failed to get website URL for index.html: {str(e)}" + ) + + return self.success_response(message) + except Exception as e: + return self.fail_response(f"Error rewriting file: {str(e)}") + + async def _delete_file(self, file_path: str) -> ToolResult: + """Delete a file at the given path""" + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + file_path = self.clean_path(file_path) + full_path = f"{self.workspace_path}/{file_path}" + if not self._file_exists(full_path): + return self.fail_response(f"File '{file_path}' does not exist") + + self.sandbox.fs.delete_file(full_path) + return self.success_response(f"File '{file_path}' deleted successfully.") + except Exception as e: + return self.fail_response(f"Error deleting file: {str(e)}") + + async def cleanup(self): + """Clean up sandbox resources.""" + + @classmethod + def create_with_context(cls, context: Context) -> "SandboxFilesTool[Context]": + """Factory method to create a SandboxFilesTool with a specific context.""" + raise NotImplementedError( + "create_with_context not implemented for SandboxFilesTool" + ) diff --git a/app/tool/sandbox/sb_shell_tool.py b/app/tool/sandbox/sb_shell_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..8a45244d0c8d8493459f3405a7d3adc98baa1dbd --- /dev/null +++ b/app/tool/sandbox/sb_shell_tool.py @@ -0,0 +1,419 @@ +import asyncio +import time +from typing import Any, Dict, Optional, TypeVar +from uuid import uuid4 + +from app.daytona.tool_base import Sandbox, SandboxToolsBase +from app.tool.base import ToolResult +from app.utils.logger import logger + + +Context = TypeVar("Context") +_SHELL_DESCRIPTION = """\ +Execute a shell command in the workspace directory. +IMPORTANT: Commands are non-blocking by default and run in a tmux session. +This is ideal for long-running operations like starting servers or build processes. +Uses sessions to maintain state between commands. +This tool is essential for running CLI tools, installing packages, and managing system operations. +""" + + +class SandboxShellTool(SandboxToolsBase): + """Tool for executing tasks in a Daytona sandbox with browser-use capabilities. + Uses sessions for maintaining state between commands and provides comprehensive process management. + """ + + name: str = "sandbox_shell" + description: str = _SHELL_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "execute_command", + "check_command_output", + "terminate_command", + "list_commands", + ], + "description": "The shell action to perform", + }, + "command": { + "type": "string", + "description": "The shell command to execute. Use this for running CLI tools, installing packages, " + "or system operations. Commands can be chained using &&, ||, and | operators.", + }, + "folder": { + "type": "string", + "description": "Optional relative path to a subdirectory of /workspace where the command should be " + "executed. Example: 'data/pdfs'", + }, + "session_name": { + "type": "string", + "description": "Optional name of the tmux session to use. Use named sessions for related commands " + "that need to maintain state. Defaults to a random session name.", + }, + "blocking": { + "type": "boolean", + "description": "Whether to wait for the command to complete. Defaults to false for non-blocking " + "execution.", + "default": False, + }, + "timeout": { + "type": "integer", + "description": "Optional timeout in seconds for blocking commands. Defaults to 60. Ignored for " + "non-blocking commands.", + "default": 60, + }, + "kill_session": { + "type": "boolean", + "description": "Whether to terminate the tmux session after checking. Set to true when you're done " + "with the command.", + "default": False, + }, + }, + "required": ["action"], + "dependencies": { + "execute_command": ["command"], + "check_command_output": ["session_name"], + "terminate_command": ["session_name"], + "list_commands": [], + }, + } + + def __init__( + self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data + ): + """Initialize with optional sandbox and thread_id.""" + super().__init__(**data) + if sandbox is not None: + self._sandbox = sandbox + + async def _ensure_session(self, session_name: str = "default") -> str: + """Ensure a session exists and return its ID.""" + if session_name not in self._sessions: + session_id = str(uuid4()) + try: + await self._ensure_sandbox() # Ensure sandbox is initialized + self.sandbox.process.create_session(session_id) + self._sessions[session_name] = session_id + except Exception as e: + raise RuntimeError(f"Failed to create session: {str(e)}") + return self._sessions[session_name] + + async def _cleanup_session(self, session_name: str): + """Clean up a session if it exists.""" + if session_name in self._sessions: + try: + await self._ensure_sandbox() # Ensure sandbox is initialized + self.sandbox.process.delete_session(self._sessions[session_name]) + del self._sessions[session_name] + except Exception as e: + print(f"Warning: Failed to cleanup session {session_name}: {str(e)}") + + async def _execute_raw_command(self, command: str) -> Dict[str, Any]: + """Execute a raw command directly in the sandbox.""" + # Ensure session exists for raw commands + session_id = await self._ensure_session("raw_commands") + + # Execute command in session + from app.daytona.sandbox import SessionExecuteRequest + + req = SessionExecuteRequest( + command=command, run_async=False, cwd=self.workspace_path + ) + + response = self.sandbox.process.execute_session_command( + session_id=session_id, + req=req, + timeout=30, # Short timeout for utility commands + ) + + logs = self.sandbox.process.get_session_command_logs( + session_id=session_id, command_id=response.cmd_id + ) + + return {"output": logs, "exit_code": response.exit_code} + + async def _execute_command( + self, + command: str, + folder: Optional[str] = None, + session_name: Optional[str] = None, + blocking: bool = False, + timeout: int = 60, + ) -> ToolResult: + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + # Set up working directory + cwd = self.workspace_path + if folder: + folder = folder.strip("/") + cwd = f"{self.workspace_path}/{folder}" + + # Generate a session name if not provided + if not session_name: + session_name = f"session_{str(uuid4())[:8]}" + + # Check if tmux session already exists + check_session = await self._execute_raw_command( + f"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'" + ) + session_exists = "not_exists" not in check_session.get("output", "") + + if not session_exists: + # Create a new tmux session + await self._execute_raw_command( + f"tmux new-session -d -s {session_name}" + ) + + # Ensure we're in the correct directory and send command to tmux + full_command = f"cd {cwd} && {command}" + wrapped_command = full_command.replace('"', '\\"') # Escape double quotes + + # Send command to tmux session + await self._execute_raw_command( + f'tmux send-keys -t {session_name} "{wrapped_command}" Enter' + ) + + if blocking: + # For blocking execution, wait and capture output + start_time = time.time() + while (time.time() - start_time) < timeout: + # Wait a bit before checking + time.sleep(2) + + # Check if session still exists (command might have exited) + check_result = await self._execute_raw_command( + f"tmux has-session -t {session_name} 2>/dev/null || echo 'ended'" + ) + if "ended" in check_result.get("output", ""): + break + + # Get current output and check for common completion indicators + output_result = await self._execute_raw_command( + f"tmux capture-pane -t {session_name} -p -S - -E -" + ) + current_output = output_result.get("output", "") + + # Check for prompt indicators that suggest command completion + last_lines = current_output.split("\n")[-3:] + completion_indicators = [ + "$", + "#", + ">", + "Done", + "Completed", + "Finished", + "✓", + ] + if any( + indicator in line + for indicator in completion_indicators + for line in last_lines + ): + break + + # Capture final output + output_result = await self._execute_raw_command( + f"tmux capture-pane -t {session_name} -p -S - -E -" + ) + final_output = output_result.get("output", "") + + # Kill the session after capture + await self._execute_raw_command(f"tmux kill-session -t {session_name}") + + return self.success_response( + { + "output": final_output, + "session_name": session_name, + "cwd": cwd, + "completed": True, + } + ) + else: + # For non-blocking, just return immediately + return self.success_response( + { + "session_name": session_name, + "cwd": cwd, + "message": f"Command sent to tmux session '{session_name}'. Use check_command_output to view results.", + "completed": False, + } + ) + + except Exception as e: + # Attempt to clean up session in case of error + if session_name: + try: + await self._execute_raw_command( + f"tmux kill-session -t {session_name}" + ) + except: + pass + return self.fail_response(f"Error executing command: {str(e)}") + + async def _check_command_output( + self, session_name: str, kill_session: bool = False + ) -> ToolResult: + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + # Check if session exists + check_result = await self._execute_raw_command( + f"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'" + ) + if "not_exists" in check_result.get("output", ""): + return self.fail_response( + f"Tmux session '{session_name}' does not exist." + ) + + # Get output from tmux pane + output_result = await self._execute_raw_command( + f"tmux capture-pane -t {session_name} -p -S - -E -" + ) + output = output_result.get("output", "") + + # Kill session if requested + if kill_session: + await self._execute_raw_command(f"tmux kill-session -t {session_name}") + termination_status = "Session terminated." + else: + termination_status = "Session still running." + + return self.success_response( + { + "output": output, + "session_name": session_name, + "status": termination_status, + } + ) + + except Exception as e: + return self.fail_response(f"Error checking command output: {str(e)}") + + async def _terminate_command(self, session_name: str) -> ToolResult: + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + # Check if session exists + check_result = await self._execute_raw_command( + f"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'" + ) + if "not_exists" in check_result.get("output", ""): + return self.fail_response( + f"Tmux session '{session_name}' does not exist." + ) + + # Kill the session + await self._execute_raw_command(f"tmux kill-session -t {session_name}") + + return self.success_response( + {"message": f"Tmux session '{session_name}' terminated successfully."} + ) + + except Exception as e: + return self.fail_response(f"Error terminating command: {str(e)}") + + async def _list_commands(self) -> ToolResult: + try: + # Ensure sandbox is initialized + await self._ensure_sandbox() + + # List all tmux sessions + result = await self._execute_raw_command( + "tmux list-sessions 2>/dev/null || echo 'No sessions'" + ) + output = result.get("output", "") + + if "No sessions" in output or not output.strip(): + return self.success_response( + {"message": "No active tmux sessions found.", "sessions": []} + ) + + # Parse session list + sessions = [] + for line in output.split("\n"): + if line.strip(): + parts = line.split(":") + if parts: + session_name = parts[0].strip() + sessions.append(session_name) + + return self.success_response( + { + "message": f"Found {len(sessions)} active sessions.", + "sessions": sessions, + } + ) + + except Exception as e: + return self.fail_response(f"Error listing commands: {str(e)}") + + async def execute( + self, + action: str, + command: str, + folder: Optional[str] = None, + session_name: Optional[str] = None, + blocking: bool = False, + timeout: int = 60, + kill_session: bool = False, + ) -> ToolResult: + """ + Execute a browser action in the sandbox environment. + Args: + timeout: + blocking: + session_name: + folder: + command: + kill_session: + action: The browser action to perform + Returns: + ToolResult with the action's output or error + """ + async with asyncio.Lock(): + try: + # Navigation actions + if action == "execute_command": + if not command: + return self.fail_response("command is required for navigation") + return await self._execute_command( + command, folder, session_name, blocking, timeout + ) + elif action == "check_command_output": + if session_name is None: + return self.fail_response( + "session_name is required for navigation" + ) + return await self._check_command_output(session_name, kill_session) + elif action == "terminate_command": + if session_name is None: + return self.fail_response( + "session_name is required for click_element" + ) + return await self._terminate_command(session_name) + elif action == "list_commands": + return await self._list_commands() + else: + return self.fail_response(f"Unknown action: {action}") + except Exception as e: + logger.error(f"Error executing shell action: {e}") + return self.fail_response(f"Error executing shell action: {e}") + + async def cleanup(self): + """Clean up all sessions.""" + for session_name in list(self._sessions.keys()): + await self._cleanup_session(session_name) + + # Also clean up any tmux sessions + try: + await self._ensure_sandbox() + await self._execute_raw_command("tmux kill-server 2>/dev/null || true") + except Exception as e: + logger.error(f"Error shell box cleanup action: {e}") diff --git a/app/tool/sandbox/sb_vision_tool.py b/app/tool/sandbox/sb_vision_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..ffe847d4861a8c7ef5ac12d3800e6b8df7d937b7 --- /dev/null +++ b/app/tool/sandbox/sb_vision_tool.py @@ -0,0 +1,178 @@ +import base64 +import mimetypes +import os +from io import BytesIO +from typing import Optional + +from PIL import Image +from pydantic import Field + +from app.daytona.tool_base import Sandbox, SandboxToolsBase, ThreadMessage +from app.tool.base import ToolResult + + +# 最大文件大小(原图10MB,压缩后5MB) +MAX_IMAGE_SIZE = 10 * 1024 * 1024 +MAX_COMPRESSED_SIZE = 5 * 1024 * 1024 + +# 压缩设置 +DEFAULT_MAX_WIDTH = 1920 +DEFAULT_MAX_HEIGHT = 1080 +DEFAULT_JPEG_QUALITY = 85 +DEFAULT_PNG_COMPRESS_LEVEL = 6 + +_VISION_DESCRIPTION = """ +A sandbox-based vision tool that allows the agent to read image files inside the sandbox using the see_image action. +* Only the see_image action is supported, with the parameter being the relative path of the image under /workspace. +* The image will be compressed and converted to base64 for use in subsequent context. +* Supported formats: JPG, PNG, GIF, WEBP. Maximum size: 10MB. +""" + + +class SandboxVisionTool(SandboxToolsBase): + name: str = "sandbox_vision" + description: str = _VISION_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": ["see_image"], + "description": "要执行的视觉动作,目前仅支持 see_image", + }, + "file_path": { + "type": "string", + "description": "图片在 /workspace 下的相对路径,如 'screenshots/image.png'", + }, + }, + "required": ["action", "file_path"], + "dependencies": {"see_image": ["file_path"]}, + } + + # def __init__(self, project_id: str, thread_id: str, thread_manager: ThreadManager): + # super().__init__(project_id=project_id, thread_manager=thread_manager) + # self.thread_id = thread_id + # self.thread_manager = thread_manager + + vision_message: Optional[ThreadMessage] = Field(default=None, exclude=True) + + def __init__( + self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data + ): + """Initialize with optional sandbox and thread_id.""" + super().__init__(**data) + if sandbox is not None: + self._sandbox = sandbox + + def compress_image(self, image_bytes: bytes, mime_type: str, file_path: str): + """压缩图片,保持合理质量。""" + try: + img = Image.open(BytesIO(image_bytes)) + if img.mode in ("RGBA", "LA", "P"): + background = Image.new("RGB", img.size, (255, 255, 255)) + if img.mode == "P": + img = img.convert("RGBA") + background.paste( + img, mask=img.split()[-1] if img.mode == "RGBA" else None + ) + img = background + width, height = img.size + if width > DEFAULT_MAX_WIDTH or height > DEFAULT_MAX_HEIGHT: + ratio = min(DEFAULT_MAX_WIDTH / width, DEFAULT_MAX_HEIGHT / height) + new_width = int(width * ratio) + new_height = int(height * ratio) + img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) + output = BytesIO() + if mime_type == "image/gif": + img.save(output, format="GIF", optimize=True) + output_mime = "image/gif" + elif mime_type == "image/png": + img.save( + output, + format="PNG", + optimize=True, + compress_level=DEFAULT_PNG_COMPRESS_LEVEL, + ) + output_mime = "image/png" + else: + img.save( + output, format="JPEG", quality=DEFAULT_JPEG_QUALITY, optimize=True + ) + output_mime = "image/jpeg" + compressed_bytes = output.getvalue() + return compressed_bytes, output_mime + except Exception: + return image_bytes, mime_type + + async def execute( + self, action: str, file_path: Optional[str] = None, **kwargs + ) -> ToolResult: + """ + 执行视觉动作,目前仅支持 see_image。 + 参数: + action: 必须为 'see_image' + file_path: 图片相对路径 + """ + if action != "see_image": + return self.fail_response(f"未知的视觉动作: {action}") + if not file_path: + return self.fail_response("file_path 参数不能为空") + try: + await self._ensure_sandbox() + cleaned_path = self.clean_path(file_path) + full_path = f"{self.workspace_path}/{cleaned_path}" + try: + file_info = self.sandbox.fs.get_file_info(full_path) + if file_info.is_dir: + return self.fail_response(f"路径 '{cleaned_path}' 是目录,不是图片文件。") + except Exception: + return self.fail_response(f"图片文件未找到: '{cleaned_path}'") + if file_info.size > MAX_IMAGE_SIZE: + return self.fail_response( + f"图片文件 '{cleaned_path}' 过大 ({file_info.size / (1024*1024):.2f}MB),最大允许 {MAX_IMAGE_SIZE / (1024*1024)}MB。" + ) + try: + image_bytes = self.sandbox.fs.download_file(full_path) + except Exception: + return self.fail_response(f"无法读取图片文件: {cleaned_path}") + mime_type, _ = mimetypes.guess_type(full_path) + if not mime_type or not mime_type.startswith("image/"): + ext = os.path.splitext(cleaned_path)[1].lower() + if ext == ".jpg" or ext == ".jpeg": + mime_type = "image/jpeg" + elif ext == ".png": + mime_type = "image/png" + elif ext == ".gif": + mime_type = "image/gif" + elif ext == ".webp": + mime_type = "image/webp" + else: + return self.fail_response( + f"不支持或未知的图片格式: '{cleaned_path}'。支持: JPG, PNG, GIF, WEBP。" + ) + compressed_bytes, compressed_mime_type = self.compress_image( + image_bytes, mime_type, cleaned_path + ) + if len(compressed_bytes) > MAX_COMPRESSED_SIZE: + return self.fail_response( + f"图片文件 '{cleaned_path}' 压缩后仍过大 ({len(compressed_bytes) / (1024*1024):.2f}MB),最大允许 {MAX_COMPRESSED_SIZE / (1024*1024)}MB。" + ) + base64_image = base64.b64encode(compressed_bytes).decode("utf-8") + image_context_data = { + "mime_type": compressed_mime_type, + "base64": base64_image, + "file_path": cleaned_path, + "original_size": file_info.size, + "compressed_size": len(compressed_bytes), + } + message = ThreadMessage( + type="image_context", content=image_context_data, is_llm_message=False + ) + self.vision_message = message + # return self.success_response(f"成功加载并压缩图片 '{cleaned_path}' (由 {file_info.size / 1024:.1f}KB 压缩到 {len(compressed_bytes) / 1024:.1f}KB)。") + return ToolResult( + output=f"成功加载并压缩图片 '{cleaned_path}'", + base64_image=base64_image, + ) + except Exception as e: + return self.fail_response(f"see_image 执行异常: {str(e)}") diff --git a/app/tool/search/__init__.py b/app/tool/search/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fe127ae309a1b550e5e36ffa3e0828f869c14182 --- /dev/null +++ b/app/tool/search/__init__.py @@ -0,0 +1,14 @@ +from app.tool.search.baidu_search import BaiduSearchEngine +from app.tool.search.base import WebSearchEngine +from app.tool.search.bing_search import BingSearchEngine +from app.tool.search.duckduckgo_search import DuckDuckGoSearchEngine +from app.tool.search.google_search import GoogleSearchEngine + + +__all__ = [ + "WebSearchEngine", + "BaiduSearchEngine", + "DuckDuckGoSearchEngine", + "GoogleSearchEngine", + "BingSearchEngine", +] diff --git a/app/tool/search/baidu_search.py b/app/tool/search/baidu_search.py new file mode 100644 index 0000000000000000000000000000000000000000..8beb3300535eded784bcc3f70f6840c2b643d9c3 --- /dev/null +++ b/app/tool/search/baidu_search.py @@ -0,0 +1,54 @@ +from typing import List + +from baidusearch.baidusearch import search + +from app.tool.search.base import SearchItem, WebSearchEngine + + +class BaiduSearchEngine(WebSearchEngine): + def perform_search( + self, query: str, num_results: int = 10, *args, **kwargs + ) -> List[SearchItem]: + """ + Baidu search engine. + + Returns results formatted according to SearchItem model. + """ + raw_results = search(query, num_results=num_results) + + # Convert raw results to SearchItem format + results = [] + for i, item in enumerate(raw_results): + if isinstance(item, str): + # If it's just a URL + results.append( + SearchItem(title=f"Baidu Result {i+1}", url=item, description=None) + ) + elif isinstance(item, dict): + # If it's a dictionary with details + results.append( + SearchItem( + title=item.get("title", f"Baidu Result {i+1}"), + url=item.get("url", ""), + description=item.get("abstract", None), + ) + ) + else: + # Try to get attributes directly + try: + results.append( + SearchItem( + title=getattr(item, "title", f"Baidu Result {i+1}"), + url=getattr(item, "url", ""), + description=getattr(item, "abstract", None), + ) + ) + except Exception: + # Fallback to a basic result + results.append( + SearchItem( + title=f"Baidu Result {i+1}", url=str(item), description=None + ) + ) + + return results diff --git a/app/tool/search/base.py b/app/tool/search/base.py new file mode 100644 index 0000000000000000000000000000000000000000..31d78b9fdc45f72bd93795440a1f2e30044d306d --- /dev/null +++ b/app/tool/search/base.py @@ -0,0 +1,40 @@ +from typing import List, Optional + +from pydantic import BaseModel, Field + + +class SearchItem(BaseModel): + """Represents a single search result item""" + + title: str = Field(description="The title of the search result") + url: str = Field(description="The URL of the search result") + description: Optional[str] = Field( + default=None, description="A description or snippet of the search result" + ) + + def __str__(self) -> str: + """String representation of a search result item.""" + return f"{self.title} - {self.url}" + + +class WebSearchEngine(BaseModel): + """Base class for web search engines.""" + + model_config = {"arbitrary_types_allowed": True} + + def perform_search( + self, query: str, num_results: int = 10, *args, **kwargs + ) -> List[SearchItem]: + """ + Perform a web search and return a list of search items. + + Args: + query (str): The search query to submit to the search engine. + num_results (int, optional): The number of search results to return. Default is 10. + args: Additional arguments. + kwargs: Additional keyword arguments. + + Returns: + List[SearchItem]: A list of SearchItem objects matching the search query. + """ + raise NotImplementedError diff --git a/app/tool/search/bing_search.py b/app/tool/search/bing_search.py new file mode 100644 index 0000000000000000000000000000000000000000..620620cd33d206944d0ac6a1b161dc317db3e5ce --- /dev/null +++ b/app/tool/search/bing_search.py @@ -0,0 +1,144 @@ +from typing import List, Optional, Tuple + +import requests +from bs4 import BeautifulSoup + +from app.logger import logger +from app.tool.search.base import SearchItem, WebSearchEngine + + +ABSTRACT_MAX_LENGTH = 300 + +USER_AGENTS = [ + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", + "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36", + "Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR) AppleWebKit/533.3 (KHTML, like Gecko) QtWeb Internet Browser/3.7 http://www.QtWeb.net", + "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", + "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/532.2", + "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4pre) Gecko/20070404 K-Ninja/2.1.3", + "Mozilla/5.0 (Future Star Technologies Corp.; Star-Blade OS; x86_64; U; en-US) iNet Browser 4.7", + "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201", + "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080414 Firefox/2.0.0.13 Pogo/2.0.0.13.6866", +] + +HEADERS = { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", + "Content-Type": "application/x-www-form-urlencoded", + "User-Agent": USER_AGENTS[0], + "Referer": "https://www.bing.com/", + "Accept-Encoding": "gzip, deflate", + "Accept-Language": "zh-CN,zh;q=0.9", +} + +BING_HOST_URL = "https://www.bing.com" +BING_SEARCH_URL = "https://www.bing.com/search?q=" + + +class BingSearchEngine(WebSearchEngine): + session: Optional[requests.Session] = None + + def __init__(self, **data): + """Initialize the BingSearch tool with a requests session.""" + super().__init__(**data) + self.session = requests.Session() + self.session.headers.update(HEADERS) + + def _search_sync(self, query: str, num_results: int = 10) -> List[SearchItem]: + """ + Synchronous Bing search implementation to retrieve search results. + + Args: + query (str): The search query to submit to Bing. + num_results (int, optional): Maximum number of results to return. Defaults to 10. + + Returns: + List[SearchItem]: A list of search items with title, URL, and description. + """ + if not query: + return [] + + list_result = [] + first = 1 + next_url = BING_SEARCH_URL + query + + while len(list_result) < num_results: + data, next_url = self._parse_html( + next_url, rank_start=len(list_result), first=first + ) + if data: + list_result.extend(data) + if not next_url: + break + first += 10 + + return list_result[:num_results] + + def _parse_html( + self, url: str, rank_start: int = 0, first: int = 1 + ) -> Tuple[List[SearchItem], str]: + """ + Parse Bing search result HTML to extract search results and the next page URL. + + Returns: + tuple: (List of SearchItem objects, next page URL or None) + """ + try: + res = self.session.get(url=url) + res.encoding = "utf-8" + root = BeautifulSoup(res.text, "lxml") + + list_data = [] + ol_results = root.find("ol", id="b_results") + if not ol_results: + return [], None + + for li in ol_results.find_all("li", class_="b_algo"): + title = "" + url = "" + abstract = "" + try: + h2 = li.find("h2") + if h2: + title = h2.text.strip() + url = h2.a["href"].strip() + + p = li.find("p") + if p: + abstract = p.text.strip() + + if ABSTRACT_MAX_LENGTH and len(abstract) > ABSTRACT_MAX_LENGTH: + abstract = abstract[:ABSTRACT_MAX_LENGTH] + + rank_start += 1 + + # Create a SearchItem object + list_data.append( + SearchItem( + title=title or f"Bing Result {rank_start}", + url=url, + description=abstract, + ) + ) + except Exception: + continue + + next_btn = root.find("a", title="Next page") + if not next_btn: + return list_data, None + + next_url = BING_HOST_URL + next_btn["href"] + return list_data, next_url + except Exception as e: + logger.warning(f"Error parsing HTML: {e}") + return [], None + + def perform_search( + self, query: str, num_results: int = 10, *args, **kwargs + ) -> List[SearchItem]: + """ + Bing search engine. + + Returns results formatted according to SearchItem model. + """ + return self._search_sync(query, num_results=num_results) diff --git a/app/tool/search/duckduckgo_search.py b/app/tool/search/duckduckgo_search.py new file mode 100644 index 0000000000000000000000000000000000000000..ca269b803f3be7e7be84758c939a4300311b4f34 --- /dev/null +++ b/app/tool/search/duckduckgo_search.py @@ -0,0 +1,57 @@ +from typing import List + +from duckduckgo_search import DDGS + +from app.tool.search.base import SearchItem, WebSearchEngine + + +class DuckDuckGoSearchEngine(WebSearchEngine): + def perform_search( + self, query: str, num_results: int = 10, *args, **kwargs + ) -> List[SearchItem]: + """ + DuckDuckGo search engine. + + Returns results formatted according to SearchItem model. + """ + raw_results = DDGS().text(query, max_results=num_results) + + results = [] + for i, item in enumerate(raw_results): + if isinstance(item, str): + # If it's just a URL + results.append( + SearchItem( + title=f"DuckDuckGo Result {i + 1}", url=item, description=None + ) + ) + elif isinstance(item, dict): + # Extract data from the dictionary + results.append( + SearchItem( + title=item.get("title", f"DuckDuckGo Result {i + 1}"), + url=item.get("href", ""), + description=item.get("body", None), + ) + ) + else: + # Try to extract attributes directly + try: + results.append( + SearchItem( + title=getattr(item, "title", f"DuckDuckGo Result {i + 1}"), + url=getattr(item, "href", ""), + description=getattr(item, "body", None), + ) + ) + except Exception: + # Fallback + results.append( + SearchItem( + title=f"DuckDuckGo Result {i + 1}", + url=str(item), + description=None, + ) + ) + + return results diff --git a/app/tool/search/google_search.py b/app/tool/search/google_search.py new file mode 100644 index 0000000000000000000000000000000000000000..bd0838ee72485c945333a8a5e01932f37b11e8cb --- /dev/null +++ b/app/tool/search/google_search.py @@ -0,0 +1,33 @@ +from typing import List + +from googlesearch import search + +from app.tool.search.base import SearchItem, WebSearchEngine + + +class GoogleSearchEngine(WebSearchEngine): + def perform_search( + self, query: str, num_results: int = 10, *args, **kwargs + ) -> List[SearchItem]: + """ + Google search engine. + + Returns results formatted according to SearchItem model. + """ + raw_results = search(query, num_results=num_results, advanced=True) + + results = [] + for i, item in enumerate(raw_results): + if isinstance(item, str): + # If it's just a URL + results.append( + {"title": f"Google Result {i+1}", "url": item, "description": ""} + ) + else: + results.append( + SearchItem( + title=item.title, url=item.url, description=item.description + ) + ) + + return results diff --git a/app/tool/str_replace_editor.py b/app/tool/str_replace_editor.py new file mode 100644 index 0000000000000000000000000000000000000000..a907f41e139e6a80e75e57aa41379172c7667109 --- /dev/null +++ b/app/tool/str_replace_editor.py @@ -0,0 +1,432 @@ +"""File and directory manipulation tool with sandbox support.""" + +from collections import defaultdict +from pathlib import Path +from typing import Any, DefaultDict, List, Literal, Optional, get_args + +from app.config import config +from app.exceptions import ToolError +from app.tool import BaseTool +from app.tool.base import CLIResult, ToolResult +from app.tool.file_operators import ( + FileOperator, + LocalFileOperator, + PathLike, + SandboxFileOperator, +) + + +Command = Literal[ + "view", + "create", + "str_replace", + "insert", + "undo_edit", +] + +# Constants +SNIPPET_LINES: int = 4 +MAX_RESPONSE_LEN: int = 16000 +TRUNCATED_MESSAGE: str = ( + "To save on context only part of this file has been shown to you. " + "You should retry this tool after you have searched inside the file with `grep -n` " + "in order to find the line numbers of what you are looking for." +) + +# Tool description +_STR_REPLACE_EDITOR_DESCRIPTION = """Custom editing tool for viewing, creating and editing files +* State is persistent across command calls and discussions with the user +* If `path` is a file, `view` displays the result of applying `cat -n`. If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep +* The `create` command cannot be used if the specified `path` already exists as a file +* If a `command` generates a long output, it will be truncated and marked with `` +* The `undo_edit` command will revert the last edit made to the file at `path` + +Notes for using the `str_replace` command: +* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces! +* If the `old_str` parameter is not unique in the file, the replacement will not be performed. Make sure to include enough context in `old_str` to make it unique +* The `new_str` parameter should contain the edited lines that should replace the `old_str` +""" + + +def maybe_truncate( + content: str, truncate_after: Optional[int] = MAX_RESPONSE_LEN +) -> str: + """Truncate content and append a notice if content exceeds the specified length.""" + if not truncate_after or len(content) <= truncate_after: + return content + return content[:truncate_after] + TRUNCATED_MESSAGE + + +class StrReplaceEditor(BaseTool): + """A tool for viewing, creating, and editing files with sandbox support.""" + + name: str = "str_replace_editor" + description: str = _STR_REPLACE_EDITOR_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "command": { + "description": "The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.", + "enum": ["view", "create", "str_replace", "insert", "undo_edit"], + "type": "string", + }, + "path": { + "description": "Absolute path to file or directory.", + "type": "string", + }, + "file_text": { + "description": "Required parameter of `create` command, with the content of the file to be created.", + "type": "string", + }, + "old_str": { + "description": "Required parameter of `str_replace` command containing the string in `path` to replace.", + "type": "string", + }, + "new_str": { + "description": "Optional parameter of `str_replace` command containing the new string (if not given, no string will be added). Required parameter of `insert` command containing the string to insert.", + "type": "string", + }, + "insert_line": { + "description": "Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.", + "type": "integer", + }, + "view_range": { + "description": "Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.", + "items": {"type": "integer"}, + "type": "array", + }, + }, + "required": ["command", "path"], + } + _file_history: DefaultDict[PathLike, List[str]] = defaultdict(list) + _local_operator: LocalFileOperator = LocalFileOperator() + _sandbox_operator: SandboxFileOperator = SandboxFileOperator() + + # def _get_operator(self, use_sandbox: bool) -> FileOperator: + def _get_operator(self) -> FileOperator: + """Get the appropriate file operator based on execution mode.""" + return ( + self._sandbox_operator + if config.sandbox.use_sandbox + else self._local_operator + ) + + async def execute( + self, + *, + command: Command, + path: str, + file_text: str | None = None, + view_range: list[int] | None = None, + old_str: str | None = None, + new_str: str | None = None, + insert_line: int | None = None, + **kwargs: Any, + ) -> str: + """Execute a file operation command.""" + # Get the appropriate file operator + operator = self._get_operator() + + # Validate path and command combination + await self.validate_path(command, Path(path), operator) + + # Execute the appropriate command + if command == "view": + result = await self.view(path, view_range, operator) + elif command == "create": + if file_text is None: + raise ToolError("Parameter `file_text` is required for command: create") + await operator.write_file(path, file_text) + self._file_history[path].append(file_text) + result = ToolResult(output=f"File created successfully at: {path}") + elif command == "str_replace": + if old_str is None: + raise ToolError( + "Parameter `old_str` is required for command: str_replace" + ) + result = await self.str_replace(path, old_str, new_str, operator) + elif command == "insert": + if insert_line is None: + raise ToolError( + "Parameter `insert_line` is required for command: insert" + ) + if new_str is None: + raise ToolError("Parameter `new_str` is required for command: insert") + result = await self.insert(path, insert_line, new_str, operator) + elif command == "undo_edit": + result = await self.undo_edit(path, operator) + else: + # This should be caught by type checking, but we include it for safety + raise ToolError( + f'Unrecognized command {command}. The allowed commands for the {self.name} tool are: {", ".join(get_args(Command))}' + ) + + return str(result) + + async def validate_path( + self, command: str, path: Path, operator: FileOperator + ) -> None: + """Validate path and command combination based on execution environment.""" + # Check if path is absolute + if not path.is_absolute(): + raise ToolError(f"The path {path} is not an absolute path") + + # Only check if path exists for non-create commands + if command != "create": + if not await operator.exists(path): + raise ToolError( + f"The path {path} does not exist. Please provide a valid path." + ) + + # Check if path is a directory + is_dir = await operator.is_directory(path) + if is_dir and command != "view": + raise ToolError( + f"The path {path} is a directory and only the `view` command can be used on directories" + ) + + # Check if file exists for create command + elif command == "create": + exists = await operator.exists(path) + if exists: + raise ToolError( + f"File already exists at: {path}. Cannot overwrite files using command `create`." + ) + + async def view( + self, + path: PathLike, + view_range: Optional[List[int]] = None, + operator: FileOperator = None, + ) -> CLIResult: + """Display file or directory content.""" + # Determine if path is a directory + is_dir = await operator.is_directory(path) + + if is_dir: + # Directory handling + if view_range: + raise ToolError( + "The `view_range` parameter is not allowed when `path` points to a directory." + ) + + return await self._view_directory(path, operator) + else: + # File handling + return await self._view_file(path, operator, view_range) + + @staticmethod + async def _view_directory(path: PathLike, operator: FileOperator) -> CLIResult: + """Display directory contents.""" + find_cmd = f"find {path} -maxdepth 2 -not -path '*/\\.*'" + + # Execute command using the operator + returncode, stdout, stderr = await operator.run_command(find_cmd) + + if not stderr: + stdout = ( + f"Here's the files and directories up to 2 levels deep in {path}, " + f"excluding hidden items:\n{stdout}\n" + ) + + return CLIResult(output=stdout, error=stderr) + + async def _view_file( + self, + path: PathLike, + operator: FileOperator, + view_range: Optional[List[int]] = None, + ) -> CLIResult: + """Display file content, optionally within a specified line range.""" + # Read file content + file_content = await operator.read_file(path) + init_line = 1 + + # Apply view range if specified + if view_range: + if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range): + raise ToolError( + "Invalid `view_range`. It should be a list of two integers." + ) + + file_lines = file_content.split("\n") + n_lines_file = len(file_lines) + init_line, final_line = view_range + + # Validate view range + if init_line < 1 or init_line > n_lines_file: + raise ToolError( + f"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be " + f"within the range of lines of the file: {[1, n_lines_file]}" + ) + if final_line > n_lines_file: + raise ToolError( + f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be " + f"smaller than the number of lines in the file: `{n_lines_file}`" + ) + if final_line != -1 and final_line < init_line: + raise ToolError( + f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be " + f"larger or equal than its first `{init_line}`" + ) + + # Apply range + if final_line == -1: + file_content = "\n".join(file_lines[init_line - 1 :]) + else: + file_content = "\n".join(file_lines[init_line - 1 : final_line]) + + # Format and return result + return CLIResult( + output=self._make_output(file_content, str(path), init_line=init_line) + ) + + async def str_replace( + self, + path: PathLike, + old_str: str, + new_str: Optional[str] = None, + operator: FileOperator = None, + ) -> CLIResult: + """Replace a unique string in a file with a new string.""" + # Read file content and expand tabs + file_content = (await operator.read_file(path)).expandtabs() + old_str = old_str.expandtabs() + new_str = new_str.expandtabs() if new_str is not None else "" + + # Check if old_str is unique in the file + occurrences = file_content.count(old_str) + if occurrences == 0: + raise ToolError( + f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}." + ) + elif occurrences > 1: + # Find line numbers of occurrences + file_content_lines = file_content.split("\n") + lines = [ + idx + 1 + for idx, line in enumerate(file_content_lines) + if old_str in line + ] + raise ToolError( + f"No replacement was performed. Multiple occurrences of old_str `{old_str}` " + f"in lines {lines}. Please ensure it is unique" + ) + + # Replace old_str with new_str + new_file_content = file_content.replace(old_str, new_str) + + # Write the new content to the file + await operator.write_file(path, new_file_content) + + # Save the original content to history + self._file_history[path].append(file_content) + + # Create a snippet of the edited section + replacement_line = file_content.split(old_str)[0].count("\n") + start_line = max(0, replacement_line - SNIPPET_LINES) + end_line = replacement_line + SNIPPET_LINES + new_str.count("\n") + snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1]) + + # Prepare the success message + success_msg = f"The file {path} has been edited. " + success_msg += self._make_output( + snippet, f"a snippet of {path}", start_line + 1 + ) + success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary." + + return CLIResult(output=success_msg) + + async def insert( + self, + path: PathLike, + insert_line: int, + new_str: str, + operator: FileOperator = None, + ) -> CLIResult: + """Insert text at a specific line in a file.""" + # Read and prepare content + file_text = (await operator.read_file(path)).expandtabs() + new_str = new_str.expandtabs() + file_text_lines = file_text.split("\n") + n_lines_file = len(file_text_lines) + + # Validate insert_line + if insert_line < 0 or insert_line > n_lines_file: + raise ToolError( + f"Invalid `insert_line` parameter: {insert_line}. It should be within " + f"the range of lines of the file: {[0, n_lines_file]}" + ) + + # Perform insertion + new_str_lines = new_str.split("\n") + new_file_text_lines = ( + file_text_lines[:insert_line] + + new_str_lines + + file_text_lines[insert_line:] + ) + + # Create a snippet for preview + snippet_lines = ( + file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line] + + new_str_lines + + file_text_lines[insert_line : insert_line + SNIPPET_LINES] + ) + + # Join lines and write to file + new_file_text = "\n".join(new_file_text_lines) + snippet = "\n".join(snippet_lines) + + await operator.write_file(path, new_file_text) + self._file_history[path].append(file_text) + + # Prepare success message + success_msg = f"The file {path} has been edited. " + success_msg += self._make_output( + snippet, + "a snippet of the edited file", + max(1, insert_line - SNIPPET_LINES + 1), + ) + success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary." + + return CLIResult(output=success_msg) + + async def undo_edit( + self, path: PathLike, operator: FileOperator = None + ) -> CLIResult: + """Revert the last edit made to a file.""" + if not self._file_history[path]: + raise ToolError(f"No edit history found for {path}.") + + old_text = self._file_history[path].pop() + await operator.write_file(path, old_text) + + return CLIResult( + output=f"Last edit to {path} undone successfully. {self._make_output(old_text, str(path))}" + ) + + def _make_output( + self, + file_content: str, + file_descriptor: str, + init_line: int = 1, + expand_tabs: bool = True, + ) -> str: + """Format file content for display with line numbers.""" + file_content = maybe_truncate(file_content) + if expand_tabs: + file_content = file_content.expandtabs() + + # Add line numbers to each line + file_content = "\n".join( + [ + f"{i + init_line:6}\t{line}" + for i, line in enumerate(file_content.split("\n")) + ] + ) + + return ( + f"Here's the result of running `cat -n` on {file_descriptor}:\n" + + file_content + + "\n" + ) diff --git a/app/tool/terminate.py b/app/tool/terminate.py new file mode 100644 index 0000000000000000000000000000000000000000..8c2d82ca7f3d840c2af53177a25bd187a854952f --- /dev/null +++ b/app/tool/terminate.py @@ -0,0 +1,25 @@ +from app.tool.base import BaseTool + + +_TERMINATE_DESCRIPTION = """Terminate the interaction when the request is met OR if the assistant cannot proceed further with the task. +When you have finished all the tasks, call this tool to end the work.""" + + +class Terminate(BaseTool): + name: str = "terminate" + description: str = _TERMINATE_DESCRIPTION + parameters: dict = { + "type": "object", + "properties": { + "status": { + "type": "string", + "description": "The finish status of the interaction.", + "enum": ["success", "failure"], + } + }, + "required": ["status"], + } + + async def execute(self, status: str) -> str: + """Finish the current execution""" + return f"The interaction has been completed with status: {status}" diff --git a/app/tool/tool_collection.py b/app/tool/tool_collection.py new file mode 100644 index 0000000000000000000000000000000000000000..297ab6cbe831eebbb91fe0fdfe341794e6cddeca --- /dev/null +++ b/app/tool/tool_collection.py @@ -0,0 +1,71 @@ +"""Collection classes for managing multiple tools.""" +from typing import Any, Dict, List + +from app.exceptions import ToolError +from app.logger import logger +from app.tool.base import BaseTool, ToolFailure, ToolResult + + +class ToolCollection: + """A collection of defined tools.""" + + class Config: + arbitrary_types_allowed = True + + def __init__(self, *tools: BaseTool): + self.tools = tools + self.tool_map = {tool.name: tool for tool in tools} + + def __iter__(self): + return iter(self.tools) + + def to_params(self) -> List[Dict[str, Any]]: + return [tool.to_param() for tool in self.tools] + + async def execute( + self, *, name: str, tool_input: Dict[str, Any] = None + ) -> ToolResult: + tool = self.tool_map.get(name) + if not tool: + return ToolFailure(error=f"Tool {name} is invalid") + try: + result = await tool(**tool_input) + return result + except ToolError as e: + return ToolFailure(error=e.message) + + async def execute_all(self) -> List[ToolResult]: + """Execute all tools in the collection sequentially.""" + results = [] + for tool in self.tools: + try: + result = await tool() + results.append(result) + except ToolError as e: + results.append(ToolFailure(error=e.message)) + return results + + def get_tool(self, name: str) -> BaseTool: + return self.tool_map.get(name) + + def add_tool(self, tool: BaseTool): + """Add a single tool to the collection. + + If a tool with the same name already exists, it will be skipped and a warning will be logged. + """ + if tool.name in self.tool_map: + logger.warning(f"Tool {tool.name} already exists in collection, skipping") + return self + + self.tools += (tool,) + self.tool_map[tool.name] = tool + return self + + def add_tools(self, *tools: BaseTool): + """Add multiple tools to the collection. + + If any tool has a name conflict with an existing tool, it will be skipped and a warning will be logged. + """ + for tool in tools: + self.add_tool(tool) + return self diff --git a/app/tool/web_search.py b/app/tool/web_search.py new file mode 100644 index 0000000000000000000000000000000000000000..b9b9e31ae91ac6cda22f3b4b308876546023002d --- /dev/null +++ b/app/tool/web_search.py @@ -0,0 +1,418 @@ +import asyncio +from typing import Any, Dict, List, Optional + +import requests +from bs4 import BeautifulSoup +from pydantic import BaseModel, ConfigDict, Field, model_validator +from tenacity import retry, stop_after_attempt, wait_exponential + +from app.config import config +from app.logger import logger +from app.tool.base import BaseTool, ToolResult +from app.tool.search import ( + BaiduSearchEngine, + BingSearchEngine, + DuckDuckGoSearchEngine, + GoogleSearchEngine, + WebSearchEngine, +) +from app.tool.search.base import SearchItem + + +class SearchResult(BaseModel): + """Represents a single search result returned by a search engine.""" + + model_config = ConfigDict(arbitrary_types_allowed=True) + + position: int = Field(description="Position in search results") + url: str = Field(description="URL of the search result") + title: str = Field(default="", description="Title of the search result") + description: str = Field( + default="", description="Description or snippet of the search result" + ) + source: str = Field(description="The search engine that provided this result") + raw_content: Optional[str] = Field( + default=None, description="Raw content from the search result page if available" + ) + + def __str__(self) -> str: + """String representation of a search result.""" + return f"{self.title} ({self.url})" + + +class SearchMetadata(BaseModel): + """Metadata about the search operation.""" + + model_config = ConfigDict(arbitrary_types_allowed=True) + + total_results: int = Field(description="Total number of results found") + language: str = Field(description="Language code used for the search") + country: str = Field(description="Country code used for the search") + + +class SearchResponse(ToolResult): + """Structured response from the web search tool, inheriting ToolResult.""" + + query: str = Field(description="The search query that was executed") + results: List[SearchResult] = Field( + default_factory=list, description="List of search results" + ) + metadata: Optional[SearchMetadata] = Field( + default=None, description="Metadata about the search" + ) + + @model_validator(mode="after") + def populate_output(self) -> "SearchResponse": + """Populate output or error fields based on search results.""" + if self.error: + return self + + result_text = [f"Search results for '{self.query}':"] + + for i, result in enumerate(self.results, 1): + # Add title with position number + title = result.title.strip() or "No title" + result_text.append(f"\n{i}. {title}") + + # Add URL with proper indentation + result_text.append(f" URL: {result.url}") + + # Add description if available + if result.description.strip(): + result_text.append(f" Description: {result.description}") + + # Add content preview if available + if result.raw_content: + content_preview = result.raw_content[:1000].replace("\n", " ").strip() + if len(result.raw_content) > 1000: + content_preview += "..." + result_text.append(f" Content: {content_preview}") + + # Add metadata at the bottom if available + if self.metadata: + result_text.extend( + [ + f"\nMetadata:", + f"- Total results: {self.metadata.total_results}", + f"- Language: {self.metadata.language}", + f"- Country: {self.metadata.country}", + ] + ) + + self.output = "\n".join(result_text) + return self + + +class WebContentFetcher: + """Utility class for fetching web content.""" + + @staticmethod + async def fetch_content(url: str, timeout: int = 10) -> Optional[str]: + """ + Fetch and extract the main content from a webpage. + + Args: + url: The URL to fetch content from + timeout: Request timeout in seconds + + Returns: + Extracted text content or None if fetching fails + """ + headers = { + "WebSearch": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" + } + + try: + # Use asyncio to run requests in a thread pool + response = await asyncio.get_event_loop().run_in_executor( + None, lambda: requests.get(url, headers=headers, timeout=timeout) + ) + + if response.status_code != 200: + logger.warning( + f"Failed to fetch content from {url}: HTTP {response.status_code}" + ) + return None + + # Parse HTML with BeautifulSoup + soup = BeautifulSoup(response.text, "html.parser") + + # Remove script and style elements + for script in soup(["script", "style", "header", "footer", "nav"]): + script.extract() + + # Get text content + text = soup.get_text(separator="\n", strip=True) + + # Clean up whitespace and limit size (100KB max) + text = " ".join(text.split()) + return text[:10000] if text else None + + except Exception as e: + logger.warning(f"Error fetching content from {url}: {e}") + return None + + +class WebSearch(BaseTool): + """Search the web for information using various search engines.""" + + name: str = "web_search" + description: str = """Search the web for real-time information about any topic. + This tool returns comprehensive search results with relevant information, URLs, titles, and descriptions. + If the primary search engine fails, it automatically falls back to alternative engines.""" + parameters: dict = { + "type": "object", + "properties": { + "query": { + "type": "string", + "description": "(required) The search query to submit to the search engine.", + }, + "num_results": { + "type": "integer", + "description": "(optional) The number of search results to return. Default is 5.", + "default": 5, + }, + "lang": { + "type": "string", + "description": "(optional) Language code for search results (default: en).", + "default": "en", + }, + "country": { + "type": "string", + "description": "(optional) Country code for search results (default: us).", + "default": "us", + }, + "fetch_content": { + "type": "boolean", + "description": "(optional) Whether to fetch full content from result pages. Default is false.", + "default": False, + }, + }, + "required": ["query"], + } + _search_engine: dict[str, WebSearchEngine] = { + "google": GoogleSearchEngine(), + "baidu": BaiduSearchEngine(), + "duckduckgo": DuckDuckGoSearchEngine(), + "bing": BingSearchEngine(), + } + content_fetcher: WebContentFetcher = WebContentFetcher() + + async def execute( + self, + query: str, + num_results: int = 5, + lang: Optional[str] = None, + country: Optional[str] = None, + fetch_content: bool = False, + ) -> SearchResponse: + """ + Execute a Web search and return detailed search results. + + Args: + query: The search query to submit to the search engine + num_results: The number of search results to return (default: 5) + lang: Language code for search results (default from config) + country: Country code for search results (default from config) + fetch_content: Whether to fetch content from result pages (default: False) + + Returns: + A structured response containing search results and metadata + """ + # Get settings from config + retry_delay = ( + getattr(config.search_config, "retry_delay", 60) + if config.search_config + else 60 + ) + max_retries = ( + getattr(config.search_config, "max_retries", 3) + if config.search_config + else 3 + ) + + # Use config values for lang and country if not specified + if lang is None: + lang = ( + getattr(config.search_config, "lang", "en") + if config.search_config + else "en" + ) + + if country is None: + country = ( + getattr(config.search_config, "country", "us") + if config.search_config + else "us" + ) + + search_params = {"lang": lang, "country": country} + + # Try searching with retries when all engines fail + for retry_count in range(max_retries + 1): + results = await self._try_all_engines(query, num_results, search_params) + + if results: + # Fetch content if requested + if fetch_content: + results = await self._fetch_content_for_results(results) + + # Return a successful structured response + return SearchResponse( + status="success", + query=query, + results=results, + metadata=SearchMetadata( + total_results=len(results), + language=lang, + country=country, + ), + ) + + if retry_count < max_retries: + # All engines failed, wait and retry + logger.warning( + f"All search engines failed. Waiting {retry_delay} seconds before retry {retry_count + 1}/{max_retries}..." + ) + await asyncio.sleep(retry_delay) + else: + logger.error( + f"All search engines failed after {max_retries} retries. Giving up." + ) + + # Return an error response + return SearchResponse( + query=query, + error="All search engines failed to return results after multiple retries.", + results=[], + ) + + async def _try_all_engines( + self, query: str, num_results: int, search_params: Dict[str, Any] + ) -> List[SearchResult]: + """Try all search engines in the configured order.""" + engine_order = self._get_engine_order() + failed_engines = [] + + for engine_name in engine_order: + engine = self._search_engine[engine_name] + logger.info(f"🔎 Attempting search with {engine_name.capitalize()}...") + search_items = await self._perform_search_with_engine( + engine, query, num_results, search_params + ) + + if not search_items: + continue + + if failed_engines: + logger.info( + f"Search successful with {engine_name.capitalize()} after trying: {', '.join(failed_engines)}" + ) + + # Transform search items into structured results + return [ + SearchResult( + position=i + 1, + url=item.url, + title=item.title + or f"Result {i+1}", # Ensure we always have a title + description=item.description or "", + source=engine_name, + ) + for i, item in enumerate(search_items) + ] + + if failed_engines: + logger.error(f"All search engines failed: {', '.join(failed_engines)}") + return [] + + async def _fetch_content_for_results( + self, results: List[SearchResult] + ) -> List[SearchResult]: + """Fetch and add web content to search results.""" + if not results: + return [] + + # Create tasks for each result + tasks = [self._fetch_single_result_content(result) for result in results] + + # Type annotation to help type checker + fetched_results = await asyncio.gather(*tasks) + + # Explicit validation of return type + return [ + ( + result + if isinstance(result, SearchResult) + else SearchResult(**result.dict()) + ) + for result in fetched_results + ] + + async def _fetch_single_result_content(self, result: SearchResult) -> SearchResult: + """Fetch content for a single search result.""" + if result.url: + content = await self.content_fetcher.fetch_content(result.url) + if content: + result.raw_content = content + return result + + def _get_engine_order(self) -> List[str]: + """Determines the order in which to try search engines.""" + preferred = ( + getattr(config.search_config, "engine", "google").lower() + if config.search_config + else "google" + ) + fallbacks = ( + [engine.lower() for engine in config.search_config.fallback_engines] + if config.search_config + and hasattr(config.search_config, "fallback_engines") + else [] + ) + + # Start with preferred engine, then fallbacks, then remaining engines + engine_order = [preferred] if preferred in self._search_engine else [] + engine_order.extend( + [ + fb + for fb in fallbacks + if fb in self._search_engine and fb not in engine_order + ] + ) + engine_order.extend([e for e in self._search_engine if e not in engine_order]) + + return engine_order + + @retry( + stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10) + ) + async def _perform_search_with_engine( + self, + engine: WebSearchEngine, + query: str, + num_results: int, + search_params: Dict[str, Any], + ) -> List[SearchItem]: + """Execute search with the given engine and parameters.""" + return await asyncio.get_event_loop().run_in_executor( + None, + lambda: list( + engine.perform_search( + query, + num_results=num_results, + lang=search_params.get("lang"), + country=search_params.get("country"), + ) + ), + ) + + +if __name__ == "__main__": + web_search = WebSearch() + search_response = asyncio.run( + web_search.execute( + query="Python programming", fetch_content=True, num_results=1 + ) + ) + print(search_response.to_tool_result()) diff --git a/app/ui/huggingface_interface.py b/app/ui/huggingface_interface.py new file mode 100644 index 0000000000000000000000000000000000000000..b5a0864445958b1c06b01766ed3d8aef6e158fea --- /dev/null +++ b/app/ui/huggingface_interface.py @@ -0,0 +1,1010 @@ +""" +Hugging Face Models Interface for OpenManus Gradio App +Advanced UI for accessing and using HuggingFace models +""" + +import base64 +import io +from typing import Dict, Tuple + +import gradio as gr +import PIL.Image + +from app.agent.huggingface_agent import HuggingFaceAgent +from app.huggingface_models import HuggingFaceModels, ModelCategory + + +class HuggingFaceModelsInterface: + """Gradio interface for HuggingFace models integration""" + + def __init__(self, hf_agent: HuggingFaceAgent): + self.hf_agent = hf_agent + self.models = HuggingFaceModels() + + def create_interface(self) -> gr.Interface: + """Create the HuggingFace models Gradio interface""" + + with gr.Blocks(title="🤗 HuggingFace Models") as interface: + gr.Markdown("# 🤗 HuggingFace Models Integration") + gr.Markdown( + "Access thousands of state-of-the-art AI models via HuggingFace Inference API" + ) + + with gr.Tabs(): + # Core AI Tabs + with gr.TabItem("📝 Text Generation"): + self._create_text_generation_interface() + + with gr.TabItem("🎨 Image Generation"): + self._create_image_generation_interface() + + with gr.TabItem("🎵 Audio Processing"): + self._create_audio_processing_interface() + + with gr.TabItem("👁️ Image Analysis"): + self._create_image_analysis_interface() + + with gr.TabItem("🔍 Text Analysis"): + self._create_text_analysis_interface() + + # New Advanced Tabs + with gr.TabItem("🎬 Video Generation"): + self._create_video_generation_interface() + + with gr.TabItem("💻 Code Generation"): + self._create_code_generation_interface() + + with gr.TabItem("🧊 3D Creation"): + self._create_3d_generation_interface() + + with gr.TabItem("📄 Document Processing"): + self._create_document_processing_interface() + + with gr.TabItem("🔗 Multimodal AI"): + self._create_multimodal_interface() + + with gr.TabItem("🎭 Creative Content"): + self._create_creative_content_interface() + + with gr.TabItem("🎮 Game Development"): + self._create_game_development_interface() + + with gr.TabItem("� Science & Research"): + self._create_science_research_interface() + + with gr.TabItem("💼 Business Tools"): + self._create_business_tools_interface() + + with gr.TabItem("🗂️ Model Browser"): + self._create_model_browser_interface() + + return interface + + def _create_text_generation_interface(self): + """Create text generation interface""" + gr.Markdown("## Generate text with powerful language models") + + with gr.Row(): + with gr.Column(): + text_model_dropdown = gr.Dropdown( + choices=[ + model.name for model in self.models.TEXT_GENERATION_MODELS + ], + value="MiniMax-M2", + label="Text Generation Model", + info="Choose from the latest and most powerful language models", + ) + + text_prompt = gr.Textbox( + label="Prompt", + placeholder="Enter your text prompt here...", + lines=4, + max_lines=10, + ) + + with gr.Row(): + text_max_tokens = gr.Slider( + minimum=10, maximum=2048, value=200, step=10, label="Max Tokens" + ) + + text_temperature = gr.Slider( + minimum=0.0, + maximum=2.0, + value=0.7, + step=0.1, + label="Temperature", + ) + + text_generate_btn = gr.Button("🚀 Generate Text", variant="primary") + + with gr.Column(): + text_output = gr.Textbox( + label="Generated Text", lines=10, max_lines=20, interactive=False + ) + + text_info = gr.JSON(label="Model Info", visible=False) + + text_generate_btn.click( + fn=self._generate_text, + inputs=[ + text_model_dropdown, + text_prompt, + text_max_tokens, + text_temperature, + ], + outputs=[text_output, text_info], + ) + + def _create_image_generation_interface(self): + """Create image generation interface""" + gr.Markdown("## Create stunning images from text descriptions") + + with gr.Row(): + with gr.Column(): + image_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.TEXT_TO_IMAGE_MODELS], + value="FLUX.1 Dev", + label="Image Generation Model", + info="State-of-the-art text-to-image models", + ) + + image_prompt = gr.Textbox( + label="Image Prompt", + placeholder="Describe the image you want to create...", + lines=3, + ) + + image_negative_prompt = gr.Textbox( + label="Negative Prompt (Optional)", + placeholder="What to avoid in the image...", + lines=2, + ) + + with gr.Row(): + image_width = gr.Slider( + minimum=256, maximum=2048, value=1024, step=64, label="Width" + ) + + image_height = gr.Slider( + minimum=256, maximum=2048, value=1024, step=64, label="Height" + ) + + image_generate_btn = gr.Button("🎨 Generate Image", variant="primary") + + with gr.Column(): + image_output = gr.Image(label="Generated Image", type="pil") + image_info = gr.JSON(label="Generation Info", visible=False) + + image_generate_btn.click( + fn=self._generate_image, + inputs=[ + image_model_dropdown, + image_prompt, + image_negative_prompt, + image_width, + image_height, + ], + outputs=[image_output, image_info], + ) + + def _create_audio_processing_interface(self): + """Create audio processing interface""" + gr.Markdown("## Speech recognition and text-to-speech") + + with gr.Tabs(): + with gr.TabItem("🎤 Speech Recognition"): + with gr.Row(): + with gr.Column(): + asr_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.ASR_MODELS], + value="Whisper Large v3", + label="Speech Recognition Model", + ) + + audio_input = gr.Audio( + label="Upload Audio File", type="filepath" + ) + + asr_language = gr.Dropdown( + choices=[ + "auto", + "en", + "es", + "fr", + "de", + "it", + "pt", + "ru", + "ja", + "ko", + "zh", + ], + value="auto", + label="Language (auto-detect if not specified)", + ) + + transcribe_btn = gr.Button("📝 Transcribe", variant="primary") + + with gr.Column(): + transcription_output = gr.Textbox( + label="Transcription", lines=8, interactive=False + ) + + with gr.TabItem("🔊 Text-to-Speech"): + with gr.Row(): + with gr.Column(): + tts_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.TTS_MODELS], + value="Kokoro 82M", + label="Text-to-Speech Model", + ) + + tts_text = gr.Textbox( + label="Text to Speak", + placeholder="Enter text to convert to speech...", + lines=4, + ) + + tts_voice = gr.Textbox( + label="Voice ID (Optional)", + placeholder="Leave empty for default voice", + ) + + synthesize_btn = gr.Button("🔊 Synthesize", variant="primary") + + with gr.Column(): + audio_output = gr.Audio(label="Generated Audio") + + transcribe_btn.click( + fn=self._transcribe_audio, + inputs=[asr_model_dropdown, audio_input, asr_language], + outputs=[transcription_output], + ) + + synthesize_btn.click( + fn=self._synthesize_speech, + inputs=[tts_model_dropdown, tts_text, tts_voice], + outputs=[audio_output], + ) + + def _create_image_analysis_interface(self): + """Create image analysis interface""" + gr.Markdown("## Analyze and classify images") + + with gr.Row(): + with gr.Column(): + analysis_model_dropdown = gr.Dropdown( + choices=[ + model.name for model in self.models.IMAGE_CLASSIFICATION_MODELS + ], + value="ViT Base Patch16", + label="Image Analysis Model", + ) + + analysis_task = gr.Radio( + choices=[ + "General Classification", + "NSFW Detection", + "Emotion Recognition", + "Deepfake Detection", + ], + value="General Classification", + label="Analysis Task", + ) + + image_input = gr.Image(label="Upload Image", type="pil") + + analyze_btn = gr.Button("🔍 Analyze Image", variant="primary") + + with gr.Column(): + analysis_output = gr.JSON(label="Analysis Results") + analysis_confidence = gr.Plot(label="Confidence Scores") + + analyze_btn.click( + fn=self._analyze_image, + inputs=[analysis_model_dropdown, analysis_task, image_input], + outputs=[analysis_output, analysis_confidence], + ) + + def _create_text_analysis_interface(self): + """Create text analysis interface""" + gr.Markdown("## Analyze, translate, and summarize text") + + with gr.Tabs(): + with gr.TabItem("🌍 Translation"): + with gr.Row(): + with gr.Column(): + translation_text = gr.Textbox( + label="Text to Translate", lines=5 + ) + + with gr.Row(): + source_lang = gr.Dropdown( + choices=[ + "auto", + "en", + "es", + "fr", + "de", + "it", + "pt", + "ru", + "ja", + "ko", + "zh", + ], + value="auto", + label="Source Language", + ) + + target_lang = gr.Dropdown( + choices=[ + "en", + "es", + "fr", + "de", + "it", + "pt", + "ru", + "ja", + "ko", + "zh", + ], + value="en", + label="Target Language", + ) + + translate_btn = gr.Button("🌍 Translate", variant="primary") + + with gr.Column(): + translation_output = gr.Textbox( + label="Translation", lines=5, interactive=False + ) + + with gr.TabItem("📄 Summarization"): + with gr.Row(): + with gr.Column(): + summary_text = gr.Textbox( + label="Text to Summarize", + lines=8, + placeholder="Paste long text here...", + ) + + summary_length = gr.Slider( + minimum=50, + maximum=500, + value=150, + step=25, + label="Summary Length", + ) + + summarize_btn = gr.Button("📄 Summarize", variant="primary") + + with gr.Column(): + summary_output = gr.Textbox( + label="Summary", lines=8, interactive=False + ) + + translate_btn.click( + fn=self._translate_text, + inputs=[translation_text, source_lang, target_lang], + outputs=[translation_output], + ) + + summarize_btn.click( + fn=self._summarize_text, + inputs=[summary_text, summary_length], + outputs=[summary_output], + ) + + def _create_model_browser_interface(self): + """Create model browser interface""" + gr.Markdown("## Browse available HuggingFace models") + + category_dropdown = gr.Dropdown( + choices=[cat.value for cat in ModelCategory], + value="text-generation", + label="Model Category", + ) + + refresh_btn = gr.Button("🔄 Refresh Models") + + models_display = gr.DataFrame( + headers=["Model Name", "Model ID", "Description", "Compatible"], + label="Available Models", + interactive=False, + ) + + def update_models(category): + models_data = [] + if category: + models = self.hf_agent.get_available_hf_models(category) + if "models" in models: + for model in models["models"]: + models_data.append( + [ + model["name"], + model["model_id"], + ( + model["description"][:100] + "..." + if len(model["description"]) > 100 + else model["description"] + ), + "✅" if model["endpoint_compatible"] else "❌", + ] + ) + return models_data + + category_dropdown.change( + fn=update_models, inputs=[category_dropdown], outputs=[models_display] + ) + + refresh_btn.click( + fn=update_models, inputs=[category_dropdown], outputs=[models_display] + ) + + async def _generate_text( + self, model_name: str, prompt: str, max_tokens: int, temperature: float + ) -> Tuple[str, Dict]: + """Generate text using selected model""" + try: + result = await self.hf_agent.generate_text_with_hf( + prompt=prompt, + model_name=model_name, + max_tokens=max_tokens, + temperature=temperature, + ) + + if "error" in result: + return f"Error: {result['error']}", {} + + # Extract text from result + generated_text = "" + if "result" in result and isinstance(result["result"], list): + generated_text = result["result"][0].get("generated_text", "") + elif "result" in result and isinstance(result["result"], dict): + generated_text = result["result"].get("generated_text", "") + + return generated_text, result + + except Exception as e: + return f"Error: {str(e)}", {} + + async def _generate_image( + self, + model_name: str, + prompt: str, + negative_prompt: str, + width: int, + height: int, + ) -> Tuple[PIL.Image.Image, Dict]: + """Generate image using selected model""" + try: + result = await self.hf_agent.generate_image_with_hf( + prompt=prompt, + model_name=model_name, + negative_prompt=negative_prompt or None, + width=width, + height=height, + ) + + if "error" in result: + return None, result + + # Convert base64 to PIL Image + if "image_base64" in result: + image_data = base64.b64decode(result["image_base64"]) + image = PIL.Image.open(io.BytesIO(image_data)) + return image, result + + return None, result + + except Exception as e: + return None, {"error": str(e)} + + async def _transcribe_audio( + self, model_name: str, audio_path: str, language: str + ) -> str: + """Transcribe audio using selected model""" + try: + if not audio_path: + return "Please upload an audio file" + + with open(audio_path, "rb") as f: + audio_data = f.read() + + result = await self.hf_agent.transcribe_audio_with_hf( + audio_data=audio_data, + model_name=model_name, + language=language if language != "auto" else None, + ) + + if "error" in result: + return f"Error: {result['error']}" + + return result.get("transcription", "No transcription available") + + except Exception as e: + return f"Error: {str(e)}" + + async def _synthesize_speech( + self, model_name: str, text: str, voice_id: str + ) -> bytes: + """Synthesize speech using selected model""" + try: + if not text.strip(): + return None + + result = await self.hf_agent.synthesize_speech_with_hf( + text=text, model_name=model_name, voice_id=voice_id or None + ) + + if "error" in result: + return None + + if "audio_base64" in result: + return base64.b64decode(result["audio_base64"]) + + return None + + except Exception as e: + return None + + async def _analyze_image( + self, model_name: str, task: str, image: PIL.Image.Image + ) -> Tuple[Dict, gr.Plot]: + """Analyze image using selected model""" + try: + if image is None: + return {"error": "Please upload an image"}, None + + # Convert PIL to bytes + img_byte_arr = io.BytesIO() + image.save(img_byte_arr, format="PNG") + img_byte_arr = img_byte_arr.getvalue() + + # Map task to model + task_models = { + "NSFW Detection": "NSFW Image Detection", + "Emotion Recognition": "Facial Emotions Detection", + "Deepfake Detection": "Deepfake Detection", + "General Classification": model_name, + } + + selected_model = task_models.get(task, model_name) + + result = await self.hf_agent.classify_image_with_hf( + image_data=img_byte_arr, model_name=selected_model + ) + + if "error" in result: + return result, None + + return result, None + + except Exception as e: + return {"error": str(e)}, None + + async def _translate_text( + self, text: str, source_lang: str, target_lang: str + ) -> str: + """Translate text""" + try: + if not text.strip(): + return "Please enter text to translate" + + result = await self.hf_agent.translate_with_hf( + text=text, + source_language=source_lang if source_lang != "auto" else None, + target_language=target_lang, + ) + + if "error" in result: + return f"Error: {result['error']}" + + return result.get("translation", {}).get( + "translation_text", "Translation failed" + ) + + except Exception as e: + return f"Error: {str(e)}" + + async def _summarize_text(self, text: str, max_length: int) -> str: + """Summarize text""" + try: + if not text.strip(): + return "Please enter text to summarize" + + result = await self.hf_agent.summarize_with_hf( + text=text, max_length=max_length + ) + + if "error" in result: + return f"Error: {result['error']}" + + return result.get("summary", {}).get("summary_text", "Summarization failed") + + except Exception: + return "Error: Summarization failed" + + def _create_video_generation_interface(self): + """Create video generation interface""" + gr.Markdown("## 🎬 Create videos from text descriptions") + + with gr.Row(): + with gr.Column(): + video_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.VIDEO_GENERATION_MODELS], + value="Stable Video Diffusion", + label="Video Generation Model" + ) + + video_prompt = gr.Textbox( + label="Video Description", + placeholder="Describe the video you want to create...", + lines=3 + ) + + with gr.Row(): + video_duration = gr.Slider( + minimum=1, + maximum=30, + value=5, + step=1, + label="Duration (seconds)" + ) + + video_fps = gr.Slider( + minimum=12, + maximum=60, + value=24, + step=1, + label="FPS" + ) + + generate_video_btn = gr.Button("🎬 Generate Video", variant="primary") + + with gr.Column(): + video_output = gr.Video(label="Generated Video") + video_info = gr.JSON(label="Generation Info", visible=False) + + def _create_code_generation_interface(self): + """Create code generation interface""" + gr.Markdown("## 💻 Generate code from natural language") + + with gr.Tabs(): + with gr.TabItem("Code Generation"): + with gr.Row(): + with gr.Column(): + code_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.CODE_GENERATION_MODELS], + value="CodeLlama 34B Instruct", + label="Code Generation Model" + ) + + code_prompt = gr.Textbox( + label="Code Description", + placeholder="Describe the code you want to generate...", + lines=4 + ) + + code_language = gr.Dropdown( + choices=["python", "javascript", "java", "cpp", "c", "rust", "go", "swift"], + value="python", + label="Programming Language" + ) + + generate_code_btn = gr.Button("💻 Generate Code", variant="primary") + + with gr.Column(): + code_output = gr.Code(label="Generated Code", language="python") + + with gr.TabItem("App Generation"): + with gr.Row(): + with gr.Column(): + app_description = gr.Textbox( + label="App Description", + placeholder="Describe the application you want to create...", + lines=5 + ) + + app_type = gr.Dropdown( + choices=["web_app", "mobile_app", "desktop_app", "api", "cli_tool"], + value="web_app", + label="Application Type" + ) + + generate_app_btn = gr.Button("🚀 Generate App", variant="primary") + + with gr.Column(): + app_output = gr.Code(label="Generated App Code", language="python") + + def _create_3d_generation_interface(self): + """Create 3D model generation interface""" + gr.Markdown("## 🧊 Create 3D models and assets") + + with gr.Tabs(): + with gr.TabItem("Text to 3D"): + with gr.Row(): + with gr.Column(): + three_d_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.THREE_D_MODELS], + value="Shap-E", + label="3D Generation Model" + ) + + three_d_prompt = gr.Textbox( + label="3D Object Description", + placeholder="Describe the 3D object you want to create...", + lines=3 + ) + + three_d_resolution = gr.Slider( + minimum=32, + maximum=256, + value=64, + step=32, + label="Resolution" + ) + + generate_3d_btn = gr.Button("🧊 Generate 3D Model", variant="primary") + + with gr.Column(): + three_d_output = gr.File(label="Generated 3D Model") + + with gr.TabItem("Image to 3D"): + with gr.Row(): + with gr.Column(): + image_3d_input = gr.Image(label="Input Image", type="pil") + convert_3d_btn = gr.Button("🔄 Convert to 3D", variant="primary") + + with gr.Column(): + image_3d_output = gr.File(label="3D Model from Image") + + def _create_document_processing_interface(self): + """Create document processing interface""" + gr.Markdown("## 📄 Process and analyze documents") + + with gr.Tabs(): + with gr.TabItem("OCR"): + with gr.Row(): + with gr.Column(): + ocr_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.DOCUMENT_PROCESSING_MODELS if "ocr" in model.name.lower()], + value="TrOCR Large", + label="OCR Model" + ) + + ocr_image_input = gr.Image(label="Document Image", type="pil") + + ocr_language = gr.Dropdown( + choices=["auto", "en", "es", "fr", "de", "it", "pt", "ru", "ja", "ko", "zh"], + value="auto", + label="Language" + ) + + extract_text_btn = gr.Button("📝 Extract Text", variant="primary") + + with gr.Column(): + ocr_output = gr.Textbox(label="Extracted Text", lines=10) + + with gr.TabItem("Document Analysis"): + with gr.Row(): + with gr.Column(): + doc_file_input = gr.File(label="Upload Document", file_types=[".pdf", ".png", ".jpg", ".jpeg"]) + analyze_doc_btn = gr.Button("🔍 Analyze Document", variant="primary") + + with gr.Column(): + doc_analysis_output = gr.JSON(label="Document Analysis") + + def _create_multimodal_interface(self): + """Create multimodal AI interface""" + gr.Markdown("## 🔗 Combine vision, text, and reasoning") + + with gr.Row(): + with gr.Column(): + multimodal_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.MULTIMODAL_MODELS], + value="BLIP-2", + label="Multimodal Model" + ) + + multimodal_image = gr.Image(label="Input Image", type="pil") + + multimodal_text = gr.Textbox( + label="Text Query/Instruction", + placeholder="Ask questions about the image or give instructions...", + lines=3 + ) + + multimodal_task = gr.Radio( + choices=["Visual Q&A", "Image Captioning", "Multimodal Chat", "Cross-modal Generation"], + value="Visual Q&A", + label="Task Type" + ) + + process_multimodal_btn = gr.Button("🔗 Process", variant="primary") + + with gr.Column(): + multimodal_output = gr.Textbox(label="Response", lines=8) + + def _create_creative_content_interface(self): + """Create creative content generation interface""" + gr.Markdown("## 🎭 Generate creative content") + + with gr.Tabs(): + with gr.TabItem("Creative Writing"): + with gr.Row(): + with gr.Column(): + creative_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.CREATIVE_CONTENT_MODELS], + value="GPT-3.5 Creative", + label="Creative Writing Model" + ) + + creative_prompt = gr.Textbox( + label="Creative Prompt", + placeholder="Provide a creative writing prompt...", + lines=4 + ) + + creative_type = gr.Dropdown( + choices=["story", "poem", "article", "script", "blog"], + value="story", + label="Content Type" + ) + + creative_length = gr.Slider( + minimum=100, + maximum=2000, + value=500, + step=100, + label="Length (words)" + ) + + generate_creative_btn = gr.Button("🎭 Generate Content", variant="primary") + + with gr.Column(): + creative_output = gr.Textbox(label="Generated Content", lines=15) + + def _create_game_development_interface(self): + """Create game development interface""" + gr.Markdown("## 🎮 Generate game content and assets") + + with gr.Tabs(): + with gr.TabItem("Character Generation"): + with gr.Row(): + with gr.Column(): + character_prompt = gr.Textbox( + label="Character Description", + placeholder="Describe your game character...", + lines=4 + ) + + character_type = gr.Dropdown( + choices=["hero", "villain", "npc", "companion", "boss"], + value="hero", + label="Character Type" + ) + + generate_character_btn = gr.Button("👾 Generate Character", variant="primary") + + with gr.Column(): + character_output = gr.Textbox(label="Character Profile", lines=10) + + with gr.TabItem("Level Design"): + with gr.Row(): + with gr.Column(): + level_description = gr.Textbox( + label="Level Description", + placeholder="Describe your game level...", + lines=4 + ) + + level_type = gr.Dropdown( + choices=["dungeon", "outdoor", "city", "space", "underwater"], + value="dungeon", + label="Environment Type" + ) + + generate_level_btn = gr.Button("🗺️ Generate Level", variant="primary") + + with gr.Column(): + level_output = gr.Textbox(label="Level Design", lines=10) + + def _create_science_research_interface(self): + """Create science and research interface""" + gr.Markdown("## 🔬 Scientific research and analysis tools") + + with gr.Tabs(): + with gr.TabItem("Research Writing"): + with gr.Row(): + with gr.Column(): + research_model_dropdown = gr.Dropdown( + choices=[model.name for model in self.models.SCIENCE_RESEARCH_MODELS], + value="SciBERT", + label="Research Model" + ) + + research_topic = gr.Textbox( + label="Research Topic", + placeholder="Enter your research topic...", + lines=3 + ) + + research_type = gr.Dropdown( + choices=["abstract", "introduction", "methodology", "discussion", "conclusion"], + value="abstract", + label="Section Type" + ) + + generate_research_btn = gr.Button("📊 Generate Research Content", variant="primary") + + with gr.Column(): + research_output = gr.Textbox(label="Research Content", lines=12) + + with gr.TabItem("Data Analysis"): + with gr.Row(): + with gr.Column(): + data_file = gr.File(label="Upload Data File", file_types=[".csv", ".xlsx", ".json"]) + analysis_type = gr.Dropdown( + choices=["descriptive", "statistical", "predictive", "clustering"], + value="descriptive", + label="Analysis Type" + ) + + analyze_data_btn = gr.Button("📈 Analyze Data", variant="primary") + + with gr.Column(): + data_analysis_output = gr.JSON(label="Analysis Results") + + def _create_business_tools_interface(self): + """Create business tools interface""" + gr.Markdown("## 💼 Professional business content generation") + + with gr.Tabs(): + with gr.TabItem("Email Generation"): + with gr.Row(): + with gr.Column(): + email_type = gr.Dropdown( + choices=["formal", "casual", "marketing", "follow_up", "invitation"], + value="formal", + label="Email Type" + ) + + email_context = gr.Textbox( + label="Email Context", + placeholder="Provide context for the email...", + lines=4 + ) + + email_tone = gr.Dropdown( + choices=["professional", "friendly", "urgent", "persuasive"], + value="professional", + label="Tone" + ) + + generate_email_btn = gr.Button("📧 Generate Email", variant="primary") + + with gr.Column(): + email_output = gr.Textbox(label="Generated Email", lines=10) + + with gr.TabItem("Report Generation"): + with gr.Row(): + with gr.Column(): + report_type = gr.Dropdown( + choices=["quarterly", "annual", "project", "analysis", "summary"], + value="project", + label="Report Type" + ) + + report_data = gr.Textbox( + label="Report Data/Context", + placeholder="Provide data or context for the report...", + lines=6 + ) + + generate_report_btn = gr.Button("📋 Generate Report", variant="primary") + + with gr.Column(): + report_output = gr.Textbox(label="Generated Report", lines=15) diff --git a/app/utils/__init__.py b/app/utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4d3ecf1be8204d9d53f73b47c36de690ead82310 --- /dev/null +++ b/app/utils/__init__.py @@ -0,0 +1 @@ +# Utility functions and constants for agent tools diff --git a/app/utils/files_utils.py b/app/utils/files_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..d14ad552bf6cbd557cf40fc7e5cc16baf966549e --- /dev/null +++ b/app/utils/files_utils.py @@ -0,0 +1,87 @@ +import os + + +# Files to exclude from operations +EXCLUDED_FILES = { + ".DS_Store", + ".gitignore", + "package-lock.json", + "postcss.config.js", + "postcss.config.mjs", + "jsconfig.json", + "components.json", + "tsconfig.tsbuildinfo", + "tsconfig.json", +} + +# Directories to exclude from operations +EXCLUDED_DIRS = {"node_modules", ".next", "dist", "build", ".git"} + +# File extensions to exclude from operations +EXCLUDED_EXT = { + ".ico", + ".svg", + ".png", + ".jpg", + ".jpeg", + ".gif", + ".bmp", + ".tiff", + ".webp", + ".db", + ".sql", +} + + +def should_exclude_file(rel_path: str) -> bool: + """Check if a file should be excluded based on path, name, or extension + + Args: + rel_path: Relative path of the file to check + + Returns: + True if the file should be excluded, False otherwise + """ + # Check filename + filename = os.path.basename(rel_path) + if filename in EXCLUDED_FILES: + return True + + # Check directory + dir_path = os.path.dirname(rel_path) + if any(excluded in dir_path for excluded in EXCLUDED_DIRS): + return True + + # Check extension + _, ext = os.path.splitext(filename) + if ext.lower() in EXCLUDED_EXT: + return True + + return False + + +def clean_path(path: str, workspace_path: str = "/workspace") -> str: + """Clean and normalize a path to be relative to the workspace + + Args: + path: The path to clean + workspace_path: The base workspace path to remove (default: "/workspace") + + Returns: + The cleaned path, relative to the workspace + """ + # Remove any leading slash + path = path.lstrip("/") + + # Remove workspace prefix if present + if path.startswith(workspace_path.lstrip("/")): + path = path[len(workspace_path.lstrip("/")) :] + + # Remove workspace/ prefix if present + if path.startswith("workspace/"): + path = path[9:] + + # Remove any remaining leading slash + path = path.lstrip("/") + + return path diff --git a/app/utils/logger.py b/app/utils/logger.py new file mode 100644 index 0000000000000000000000000000000000000000..3c4f6458bef0c7fd4aae3330a2ab06f893ab28f1 --- /dev/null +++ b/app/utils/logger.py @@ -0,0 +1,32 @@ +import logging +import os + +import structlog + + +ENV_MODE = os.getenv("ENV_MODE", "LOCAL") + +renderer = [structlog.processors.JSONRenderer()] +if ENV_MODE.lower() == "local".lower(): + renderer = [structlog.dev.ConsoleRenderer()] + +structlog.configure( + processors=[ + structlog.stdlib.add_log_level, + structlog.stdlib.PositionalArgumentsFormatter(), + structlog.processors.dict_tracebacks, + structlog.processors.CallsiteParameterAdder( + { + structlog.processors.CallsiteParameter.FILENAME, + structlog.processors.CallsiteParameter.FUNC_NAME, + structlog.processors.CallsiteParameter.LINENO, + } + ), + structlog.processors.TimeStamper(fmt="iso"), + structlog.contextvars.merge_contextvars, + *renderer, + ], + cache_logger_on_first_use=True, +) + +logger: structlog.stdlib.BoundLogger = structlog.get_logger(level=logging.DEBUG) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..6fc371c5e7a0a102bf80a0096d22c172f5cfda79 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +fastapi==0.115.0 +uvicorn[standard]==0.30.6 +pydantic==2.9.2 +huggingface-hub==0.26.2 +python-multipart==0.0.12 + +# Agent dependencies +anthropic~=0.39.0 +httpx~=0.28.1 +pydantic~=2.9.2 +selenium~=4.26.1 +beautifulsoup4~=4.12.3 +playwright~=1.49.0 +crawl4ai~=0.4.243 +pandas~=2.2.3 +matplotlib~=3.9.2