[
  {
    "path": ".gitattributes",
    "content": "# HTML code is incorrectly calculated into statistics, so ignore them\n*.html linguist-detectable=false\n# Auto detect text files and perform LF normalization\n* text=auto eol=lf\n# Ensure shell scripts use LF (Linux style) line endings on Windows\n*.sh text eol=lf\n# Treat specific binary files as binary and prevent line ending conversion\n*.png binary\n*.jpg binary\n*.gif binary\n*.ico binary\n*.jpeg binary\n*.mp3 binary\n*.zip binary\n*.bin binary\n# Preserve original line endings for specific document files\n*.doc text eol=crlf\n*.docx text eol=crlf\n*.pdf binary\n# Ensure source code and script files use LF line endings\n*.py text eol=lf\n*.js text eol=lf\n*.html text eol=lf\n*.css text eol=lf\n# Specify custom diff driver for specific file types\n*.md diff=markdown\n*.json diff=json\n*.mp4 filter=lfs diff=lfs merge=lfs -text\n*.mov filter=lfs diff=lfs merge=lfs -text\n*.webm filter=lfs diff=lfs merge=lfs -text\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/config.yml",
    "content": "blank_issues_enabled: false\ncontact_links:\n  - name: \"Join the Community Group\"\n    about: Join the OpenManus community to discuss and get help from others\n    url: https://github.com/FoundationAgents/OpenManus?tab=readme-ov-file#community-group\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/request_new_features.yaml",
    "content": "name: \"🤔 Request new features\"\ndescription: Suggest ideas or features you’d like to see implemented in OpenManus.\nlabels: enhancement\nbody:\n  - type: textarea\n    id: feature-description\n    attributes:\n      label: Feature description\n      description: |\n        Provide a clear and concise description of the proposed feature\n    validations:\n      required: true\n  - type: textarea\n    id: your-feature\n    attributes:\n      label: Your Feature\n      description: |\n        Explain your idea or implementation process, if any. Optionally, include a Pull Request URL.\n        Ensure accompanying docs/tests/examples are provided for review.\n    validations:\n      required: false\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/show_me_the_bug.yaml",
    "content": "name: \"🪲 Show me the Bug\"\ndescription: Report a bug encountered while using OpenManus and seek assistance.\nlabels: bug\nbody:\n  - type: textarea\n    id: bug-description\n    attributes:\n      label: Bug Description\n      description: |\n        Clearly describe the bug you encountered\n    validations:\n      required: true\n  - type: textarea\n    id: solve-method\n    attributes:\n      label: Bug solved method\n      description: |\n        If resolved, explain the solution. Optionally, include a Pull Request URL.\n        If unresolved, provide additional details to aid investigation\n    validations:\n      required: true\n  - type: textarea\n    id: environment-information\n    attributes:\n      label: Environment information\n      description: |\n        System: e.g., Ubuntu 22.04\n        Python: e.g., 3.12\n        OpenManus version: e.g., 0.1.0\n      value: |\n        - System version:\n        - Python version:\n        - OpenManus version or branch:\n        - Installation method (e.g., `pip install -r requirements.txt` or `pip install -e .`):\n    validations:\n      required: true\n  - type: textarea\n    id: extra-information\n    attributes:\n      label: Extra information\n      description: |\n        For example, attach screenshots or logs to help diagnose the issue\n    validations:\n      required: false\n"
  },
  {
    "path": ".github/PULL_REQUEST_TEMPLATE.md",
    "content": "**Features**\n<!-- Describe the features or bug fixes in this PR. For bug fixes, link to the issue. -->\n\n- Feature 1\n- Feature 2\n\n**Feature Docs**\n<!-- Provide RFC, tutorial, or use case links for significant updates. Optional for minor changes. -->\n\n**Influence**\n<!-- Explain the impact of these changes for reviewer focus. -->\n\n**Result**\n<!-- Include screenshots or logs of unit tests or running results. -->\n\n**Other**\n<!-- Additional notes about this PR. -->\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "version: 2\nupdates:\n  - package-ecosystem: \"pip\"\n    directory: \"/\"\n    schedule:\n      interval: \"weekly\"\n    open-pull-requests-limit: 4\n    groups:\n      # Group critical packages that might need careful review\n      core-dependencies:\n        patterns:\n          - \"pydantic*\"\n          - \"openai\"\n          - \"fastapi\"\n          - \"tiktoken\"\n      browsergym-related:\n        patterns:\n          - \"browsergym*\"\n          - \"browser-use\"\n          - \"playwright\"\n      search-tools:\n        patterns:\n          - \"googlesearch-python\"\n          - \"baidusearch\"\n          - \"duckduckgo_search\"\n      pre-commit:\n        patterns:\n          - \"pre-commit\"\n      security-all:\n        applies-to: \"security-updates\"\n        patterns:\n          - \"*\"\n      version-all:\n        applies-to: \"version-updates\"\n        patterns:\n          - \"*\"\n        exclude-patterns:\n          - \"pydantic*\"\n          - \"openai\"\n          - \"fastapi\"\n          - \"tiktoken\"\n          - \"browsergym*\"\n          - \"browser-use\"\n          - \"playwright\"\n          - \"googlesearch-python\"\n          - \"baidusearch\"\n          - \"duckduckgo_search\"\n          - \"pre-commit\"\n\n  - package-ecosystem: \"github-actions\"\n    directory: \"/\"\n    schedule:\n      interval: \"weekly\"\n    open-pull-requests-limit: 4\n    groups:\n      actions:\n        patterns:\n          - \"*\"\n"
  },
  {
    "path": ".github/workflows/build-package.yaml",
    "content": "name: Build and upload Python package\n\non:\n  workflow_dispatch:\n  release:\n    types: [created, published]\n\njobs:\n  deploy:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - name: Set up Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.12'\n          cache: 'pip'\n      - name: Install dependencies\n        run: |\n          python -m pip install --upgrade pip\n          pip install -r requirements.txt\n          pip install setuptools wheel twine\n      - name: Set package version\n        run: |\n          export VERSION=\"${GITHUB_REF#refs/tags/v}\"\n          sed -i \"s/version=.*/version=\\\"${VERSION}\\\",/\" setup.py\n      - name: Build and publish\n        env:\n          TWINE_USERNAME: __token__\n          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}\n        run: |\n          python setup.py bdist_wheel sdist\n          twine upload dist/*\n"
  },
  {
    "path": ".github/workflows/environment-corrupt-check.yaml",
    "content": "name: Environment Corruption Check\non:\n  push:\n    branches: [\"main\"]\n    paths:\n      - requirements.txt\n  pull_request:\n    branches: [\"main\"]\n    paths:\n      - requirements.txt\nconcurrency:\n  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}\n  cancel-in-progress: true\njobs:\n  test-python-versions:\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        python-version: [\"3.11.11\", \"3.12.8\", \"3.13.2\"]\n      fail-fast: false\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n      - name: Set up Python ${{ matrix.python-version }}\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python-version }}\n      - name: Upgrade pip\n        run: |\n          python -m pip install --upgrade pip\n      - name: Install dependencies\n        run: |\n          pip install -r requirements.txt\n"
  },
  {
    "path": ".github/workflows/pr-autodiff.yaml",
    "content": "name: PR Diff Summarization\non:\n  # pull_request:\n  #   branches: [main]\n  #   types: [opened, ready_for_review, reopened]\n  issue_comment:\n    types: [created]\npermissions:\n  contents: read\n  pull-requests: write\njobs:\n  pr-diff-summarization:\n    runs-on: ubuntu-latest\n    if: |\n      (github.event_name == 'pull_request') ||\n      (github.event_name == 'issue_comment' &&\n       contains(github.event.comment.body, '!pr-diff') &&\n       (github.event.comment.author_association == 'CONTRIBUTOR' || github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') &&\n       github.event.issue.pull_request)\n    steps:\n      - name: Get PR head SHA\n        id: get-pr-sha\n        run: |\n          PR_URL=\"${{ github.event.issue.pull_request.url || github.event.pull_request.url }}\"\n          # https://api.github.com/repos/OpenManus/pulls/1\n          RESPONSE=$(curl -s -H \"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}\" $PR_URL)\n          SHA=$(echo $RESPONSE | jq -r '.head.sha')\n          TARGET_BRANCH=$(echo $RESPONSE | jq -r '.base.ref')\n          echo \"pr_sha=$SHA\" >> $GITHUB_OUTPUT\n          echo \"target_branch=$TARGET_BRANCH\" >> $GITHUB_OUTPUT\n          echo \"Retrieved PR head SHA from API: $SHA, target branch: $TARGET_BRANCH\"\n      - name: Check out code\n        uses: actions/checkout@v4\n        with:\n          ref: ${{ steps.get-pr-sha.outputs.pr_sha }}\n          fetch-depth: 0\n      - name: Set up Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.11'\n      - name: Install dependencies\n        run: |\n          python -m pip install --upgrade pip\n          pip install openai requests\n      - name: Create and run Python script\n        env:\n          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}\n          OPENAI_BASE_URL: ${{ secrets.OPENAI_BASE_URL }}\n          GH_TOKEN: ${{ github.token }}\n          PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}\n          TARGET_BRANCH: ${{ steps.get-pr-sha.outputs.target_branch }}\n        run: |-\n          cat << 'EOF' > /tmp/_workflow_core.py\n          import os\n          import subprocess\n          import json\n          import requests\n          from openai import OpenAI\n\n          def get_diff():\n              result = subprocess.run(\n                  ['git', 'diff', 'origin/' + os.getenv('TARGET_BRANCH') + '...HEAD'],\n                  capture_output=True, text=True, check=True)\n              return '\\n'.join(\n                  line for line in result.stdout.split('\\n')\n                  if any(line.startswith(c) for c in ('+', '-'))\n                  and not line.startswith(('---', '+++'))\n              )[:round(200000 * 0.4)]  # Truncate to prevent overflow\n\n          def generate_comment(diff_content):\n              client = OpenAI(\n                  base_url=os.getenv(\"OPENAI_BASE_URL\"),\n                  api_key=os.getenv(\"OPENAI_API_KEY\")\n              )\n\n              guidelines = '''\n          1. English version first, Chinese Simplified version after\n          2. Example format:\n              # Diff Report\n              ## English\n              - Added `ABC` class\n              - Fixed `f()` behavior in `foo` module\n\n              ### Comments Highlight\n              - `config.toml` needs to be configured properly to make sure new features work as expected.\n\n              ### Spelling/Offensive Content Check\n              - No spelling mistakes or offensive content found in the code or comments.\n\n              ## 中文（简体）\n              - 新增了 `ABC` 类\n              - `foo` 模块中的 `f()` 行为已修复\n\n              ### 评论高亮\n              - `config.toml` 需要正确配置才能确保新功能正常运行。\n\n              ### 内容检查\n              - 没有发现代码或注释中的拼写错误或不当措辞。\n\n          3. Highlight non-English comments\n          4. Check for spelling/offensive content'''\n\n              response = client.chat.completions.create(\n                  model=\"o3-mini\",\n                  messages=[{\n                      \"role\": \"system\",\n                      \"content\": \"Generate bilingual code review feedback.\"\n                  }, {\n                      \"role\": \"user\",\n                      \"content\": f\"Review these changes per guidelines:\\n{guidelines}\\n\\nDIFF:\\n{diff_content}\"\n                  }]\n              )\n              return response.choices[0].message.content\n\n          def post_comment(comment):\n              repo = os.getenv(\"GITHUB_REPOSITORY\")\n              pr_number = os.getenv(\"PR_NUMBER\")\n\n              headers = {\n                  \"Authorization\": f\"Bearer {os.getenv('GH_TOKEN')}\",\n                  \"Accept\": \"application/vnd.github.v3+json\"\n              }\n              url = f\"https://api.github.com/repos/{repo}/issues/{pr_number}/comments\"\n\n              requests.post(url, json={\"body\": comment}, headers=headers)\n\n          if __name__ == \"__main__\":\n              diff_content = get_diff()\n              if not diff_content.strip():\n                  print(\"No meaningful diff detected.\")\n                  exit(0)\n\n              comment = generate_comment(diff_content)\n              post_comment(comment)\n              print(\"Comment posted successfully.\")\n          EOF\n\n          python /tmp/_workflow_core.py\n"
  },
  {
    "path": ".github/workflows/pre-commit.yaml",
    "content": "name: Pre-commit checks\n\non:\n  pull_request:\n    branches:\n      - '**'\n  push:\n    branches:\n      - '**'\n\njobs:\n  pre-commit-check:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout Source Code\n        uses: actions/checkout@v4\n      - name: Set up Python 3.12\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.12'\n      - name: Install pre-commit and tools\n        run: |\n          python -m pip install --upgrade pip\n          pip install pre-commit black==23.1.0 isort==5.12.0 autoflake==2.0.1\n      - name: Run pre-commit hooks\n        run: pre-commit run --all-files\n"
  },
  {
    "path": ".github/workflows/stale.yaml",
    "content": "name: Close inactive issues\n\non:\n  schedule:\n    - cron: \"5 0 * * *\"\n\njobs:\n  close-issues:\n    runs-on: ubuntu-latest\n    permissions:\n      issues: write\n      pull-requests: write\n    steps:\n      - uses: actions/stale@v9\n        with:\n          days-before-issue-stale: 30\n          days-before-issue-close: 14\n          stale-issue-label: \"inactive\"\n          stale-issue-message: \"This issue has been inactive for 30 days. Please comment if you have updates.\"\n          close-issue-message: \"This issue was closed due to 45 days of inactivity. Reopen if still relevant.\"\n          days-before-pr-stale: -1\n          days-before-pr-close: -1\n          repo-token: ${{ secrets.GITHUB_TOKEN }}\n"
  },
  {
    "path": ".github/workflows/top-issues.yaml",
    "content": "name: Top issues\non:\n  schedule:\n    - cron: '0 0/2 * * *'\n  workflow_dispatch:\njobs:\n  ShowAndLabelTopIssues:\n    permissions:\n      issues: write\n      pull-requests: write\n      actions: read\n      contents: read\n    name: Display and label top issues\n    runs-on: ubuntu-latest\n    if: github.repository == 'FoundationAgents/OpenManus'\n    steps:\n      - name: Run top issues action\n        uses: rickstaa/top-issues-action@7e8dda5d5ae3087670f9094b9724a9a091fc3ba1 # v1.3.101\n        env:\n          github_token: ${{ secrets.GITHUB_TOKEN }}\n        with:\n          label: true\n          dashboard: true\n          dashboard_show_total_reactions: true\n          top_issues: true\n          top_features: true\n          top_bugs: true\n          top_pull_requests: true\n          top_list_size: 14\n"
  },
  {
    "path": ".gitignore",
    "content": "### Project-specific ###\n# Logs\nlogs/\n\n# Data\ndata/\n\n# Workspace\nworkspace/\n\n### Python ###\n# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\nshare/python-wheels/\n*.egg-info/\n.installed.cfg\n*.egg\nMANIFEST\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.nox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n*.py,cover\n.hypothesis/\n.pytest_cache/\ncover/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\ndb.sqlite3\ndb.sqlite3-journal\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\n.pybuilder/\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# IPython\nprofile_default/\nipython_config.py\n\n# pyenv\n#   For a library or package, you might want to ignore these files since the code is\n#   intended to run in multiple environments; otherwise, check them in:\n# .python-version\n\n# pipenv\n#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.\n#   However, in case of collaboration, if having platform-specific dependencies or dependencies\n#   having no cross-platform support, pipenv may install dependencies that don't work, or not\n#   install all needed dependencies.\n#Pipfile.lock\n\n# UV\n#   Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.\n#   This is especially recommended for binary packages to ensure reproducibility, and is more\n#   commonly ignored for libraries.\n#uv.lock\n\n# poetry\n#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.\n#   This is especially recommended for binary packages to ensure reproducibility, and is more\n#   commonly ignored for libraries.\n#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control\n#poetry.lock\n\n# pdm\n#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.\n#pdm.lock\n#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it\n#   in version control.\n#   https://pdm.fming.dev/latest/usage/project/#working-with-version-control\n.pdm.toml\n.pdm-python\n.pdm-build/\n\n# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm\n__pypackages__/\n\n# Celery stuff\ncelerybeat-schedule\ncelerybeat.pid\n\n# SageMath parsed files\n*.sage.py\n\n# Environments\n.env\n.venv\nenv/\nvenv/\nENV/\nenv.bak/\nvenv.bak/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n.dmypy.json\ndmypy.json\n\n# Pyre type checker\n.pyre/\n\n# pytype static type analyzer\n.pytype/\n\n# Cython debug symbols\ncython_debug/\n\n# PyCharm\n#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can\n#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore\n#  and can be added to the global gitignore or merged into this file.  For a more nuclear\n#  option (not recommended) you can uncomment the following to ignore the entire idea folder.\n.idea/\n\n# PyPI configuration file\n.pypirc\n\n### Visual Studio Code ###\n.vscode/*\n!.vscode/settings.json\n!.vscode/tasks.json\n!.vscode/launch.json\n!.vscode/extensions.json\n!.vscode/*.code-snippets\n\n# Local History for Visual Studio Code\n.history/\n\n# Built Visual Studio Code Extensions\n*.vsix\n\n# OSX\n.DS_Store\n\n# node\nnode_modules\n"
  },
  {
    "path": ".pre-commit-config.yaml",
    "content": "repos:\n  - repo: https://github.com/psf/black\n    rev: 23.1.0\n    hooks:\n      - id: black\n\n  - repo: https://github.com/pre-commit/pre-commit-hooks\n    rev: v4.4.0\n    hooks:\n      - id: trailing-whitespace\n      - id: end-of-file-fixer\n      - id: check-yaml\n      - id: check-added-large-files\n\n  - repo: https://github.com/PyCQA/autoflake\n    rev: v2.0.1\n    hooks:\n      - id: autoflake\n        args:\n          [\n            --remove-all-unused-imports,\n            --ignore-init-module-imports,\n            --expand-star-imports,\n            --remove-duplicate-keys,\n            --remove-unused-variables,\n            --recursive,\n            --in-place,\n            --exclude=__init__.py,\n          ]\n        files: \\.py$\n\n  - repo: https://github.com/pycqa/isort\n    rev: 5.12.0\n    hooks:\n      - id: isort\n        args:\n          [\"--profile\", \"black\", \"--filter-files\", \"--lines-after-imports=2\"]\n"
  },
  {
    "path": ".vscode/extensions.json",
    "content": "{\n\t\"recommendations\": [\n\t\t\"tamasfe.even-better-toml\",\n\t\t\"ms-python.black-formatter\",\n\t\t\"ms-python.isort\"\n\t],\n\t\"unwantedRecommendations\": []\n}\n"
  },
  {
    "path": ".vscode/settings.json",
    "content": "{\n    \"[python]\": {\n        \"editor.defaultFormatter\": \"ms-python.black-formatter\",\n        \"editor.codeActionsOnSave\": {\n            \"source.organizeImports\": \"always\"\n        }\n    },\n    \"[toml]\": {\n        \"editor.defaultFormatter\": \"tamasfe.even-better-toml\",\n    },\n    \"pre-commit-helper.runOnSave\": \"none\",\n    \"pre-commit-helper.config\": \".pre-commit-config.yaml\",\n    \"evenBetterToml.schema.enabled\": true,\n    \"evenBetterToml.schema.associations\": {\n        \"^.+config[/\\\\\\\\].+\\\\.toml$\": \"../config/schema.config.json\"\n    },\n    \"files.insertFinalNewline\": true,\n    \"files.trimTrailingWhitespace\": true,\n    \"editor.formatOnSave\": true\n}\n"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nWe as members, contributors, and leaders pledge to make participation in our\ncommunity a harassment-free experience for everyone, regardless of age, body\nsize, visible or invisible disability, ethnicity, sex characteristics, gender\nidentity and expression, level of experience, education, socio-economic status,\nnationality, personal appearance, race, caste, color, religion, or sexual\nidentity and orientation.\n\nWe pledge to act and interact in ways that contribute to an open, welcoming,\ndiverse, inclusive, and healthy community.\n\n## Our Standards\n\nExamples of behavior that contributes to a positive environment for our\ncommunity include:\n\n* Demonstrating empathy and kindness toward other people.\n* Being respectful of differing opinions, viewpoints, and experiences.\n* Giving and gracefully accepting constructive feedback.\n* Accepting responsibility and apologizing to those affected by our mistakes,\n  and learning from the experience.\n* Focusing on what is best not just for us as individuals, but for the overall\n  community.\n\nExamples of unacceptable behavior include:\n\n* The use of sexualized language or imagery, and sexual attention or advances of\n  any kind.\n* Trolling, insulting or derogatory comments, and personal or political attacks.\n* Public or private harassment.\n* Publishing others' private information, such as a physical or email address,\n  without their explicit permission.\n* Other conduct which could reasonably be considered inappropriate in a\n  professional setting.\n\n## Enforcement Responsibilities\n\nCommunity leaders are responsible for clarifying and enforcing our standards of\nacceptable behavior and will take appropriate and fair corrective action in\nresponse to any behavior that they deem inappropriate, threatening, offensive,\nor harmful.\n\nCommunity leaders have the right and responsibility to remove, edit, or reject\ncomments, commits, code, wiki edits, issues, and other contributions that are\nnot aligned to this Code of Conduct, and will communicate reasons for moderation\ndecisions when appropriate.\n\n## Scope\n\nThis Code of Conduct applies within all community spaces, and also applies when\nan individual is officially representing the community in public spaces.\nExamples of representing our community include using an official email address,\nposting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported to the community leaders responsible for enforcement at\nmannaandpoem@gmail.com\nAll complaints will be reviewed and investigated promptly and fairly.\n\nAll community leaders are obligated to respect the privacy and security of the\nreporter of any incident.\n\n## Enforcement Guidelines\n\nCommunity leaders will follow these Community Impact Guidelines in determining\nthe consequences for any action they deem in violation of this Code of Conduct:\n\n### 1. Correction\n\n**Community Impact**: Use of inappropriate language or other behavior deemed\nunprofessional or unwelcome in the community.\n\n**Consequence**: A private, written warning from community leaders, providing\nclarity around the nature of the violation and an explanation of why the\nbehavior was inappropriate. A public apology may be requested.\n\n### 2. Warning\n\n**Community Impact**: A violation through a single incident or series of\nactions.\n\n**Consequence**: A warning with consequences for continued behavior. No\ninteraction with the people involved, including unsolicited interaction with\nthose enforcing the Code of Conduct, for a specified period of time. This\nincludes avoiding interactions in community spaces as well as external channels\nlike social media. Violating these terms may lead to a temporary or permanent\nban.\n\n### 3. Temporary Ban\n\n**Community Impact**: A serious violation of community standards, including\nsustained inappropriate behavior.\n\n**Consequence**: A temporary ban from any sort of interaction or public\ncommunication with the community for a specified period of time. No public or\nprivate interaction with the people involved, including unsolicited interaction\nwith those enforcing the Code of Conduct, is allowed during this period.\nViolating these terms may lead to a permanent ban.\n\n### 4. Permanent Ban\n\n**Community Impact**: Demonstrating a pattern of violation of community\nstandards, including sustained inappropriate behavior, harassment of an\nindividual, or aggression toward or disparagement of classes of individuals.\n\n**Consequence**: A permanent ban from any sort of public interaction within the\ncommunity.\n\n### Slack and Discord Etiquettes\n\nThese Slack and Discord etiquette guidelines are designed to foster an inclusive, respectful, and productive environment\nfor all community members. By following these best practices, we ensure effective communication and collaboration while\nminimizing disruptions. Let’s work together to build a supportive and welcoming community!\n\n- Communicate respectfully and professionally, avoiding sarcasm or harsh language, and remember that tone can be\n  difficult to interpret in text.\n- Use threads for specific discussions to keep channels organized and easier to follow.\n- Tag others only when their input is critical or urgent, and use @here, @channel or @everyone sparingly to minimize\n  disruptions.\n- Be patient, as open-source contributors and maintainers often have other commitments and may need time to respond.\n- Post questions or discussions in the most relevant\n  channel ([discord - #general](https://discord.com/channels/1125308739348594758/1138430348557025341)).\n- When asking for help or raising issues, include necessary details like links, screenshots, or clear explanations to\n  provide context.\n- Keep discussions in public channels whenever possible to allow others to benefit from the conversation, unless the\n  matter is sensitive or private.\n- Always adhere to [our standards](https://github.com/FoundationAgents/OpenManus/blob/main/CODE_OF_CONDUCT.md#our-standards)\n  to ensure a welcoming and collaborative environment.\n- If you choose to mute a channel, consider setting up alerts for topics that still interest you to stay engaged. For\n  Slack, Go to Settings → Notifications → My Keywords to add specific keywords that will notify you when mentioned. For\n  example, if you're here for discussions about LLMs, mute the channel if it’s too busy, but set notifications to alert\n  you only when “LLMs” appears in messages. Also for Discord, go to the channel notifications and choose the option that\n  best describes your need.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage],\nversion 2.1, available at\n[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].\n\nCommunity Impact Guidelines were inspired by\n[Mozilla's code of conduct enforcement ladder][Mozilla CoC].\n\nFor answers to common questions about this code of conduct, see the FAQ at\n[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at\n[https://www.contributor-covenant.org/translations][translations].\n\n[homepage]: https://www.contributor-covenant.org\n\n[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html\n\n[Mozilla CoC]: https://github.com/mozilla/diversity\n\n[FAQ]: https://www.contributor-covenant.org/faq\n\n[translations]: https://www.contributor-covenant.org/translations\n"
  },
  {
    "path": "Dockerfile",
    "content": "FROM python:3.12-slim\n\nWORKDIR /app/OpenManus\n\nRUN apt-get update && apt-get install -y --no-install-recommends git curl \\\n    && rm -rf /var/lib/apt/lists/* \\\n    && (command -v uv >/dev/null 2>&1 || pip install --no-cache-dir uv)\n\nCOPY . .\n\nRUN uv pip install --system -r requirements.txt\n\nCMD [\"bash\"]\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2025 manna_and_poem\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "<p align=\"center\">\n  <img src=\"assets/logo.jpg\" width=\"200\"/>\n</p>\n\nEnglish | [中文](README_zh.md) | [한국어](README_ko.md) | [日本語](README_ja.md)\n\n[![GitHub stars](https://img.shields.io/github/stars/FoundationAgents/OpenManus?style=social)](https://github.com/FoundationAgents/OpenManus/stargazers)\n&ensp;\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) &ensp;\n[![Discord Follow](https://dcbadge.vercel.app/api/server/DYn29wFk9z?style=flat)](https://discord.gg/DYn29wFk9z)\n[![Demo](https://img.shields.io/badge/Demo-Hugging%20Face-yellow)](https://huggingface.co/spaces/lyh-917/OpenManusDemo)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.15186407.svg)](https://doi.org/10.5281/zenodo.15186407)\n\n# 👋 OpenManus\n\nManus is incredible, but OpenManus can achieve any idea without an *Invite Code* 🛫!\n\nOur team members [@Xinbin Liang](https://github.com/mannaandpoem) and [@Jinyu Xiang](https://github.com/XiangJinyu) (core authors), along with [@Zhaoyang Yu](https://github.com/MoshiQAQ), [@Jiayi Zhang](https://github.com/didiforgithub), and [@Sirui Hong](https://github.com/stellaHSR), we are from [@MetaGPT](https://github.com/geekan/MetaGPT). The prototype is launched within 3 hours and we are keeping building!\n\nIt's a simple implementation, so we welcome any suggestions, contributions, and feedback!\n\nEnjoy your own agent with OpenManus!\n\nWe're also excited to introduce [OpenManus-RL](https://github.com/OpenManus/OpenManus-RL), an open-source project dedicated to reinforcement learning (RL)- based (such as GRPO) tuning methods for LLM agents, developed collaboratively by researchers from UIUC and OpenManus.\n\n## Project Demo\n\n<video src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" data-canonical-src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" controls=\"controls\" muted=\"muted\" class=\"d-block rounded-bottom-2 border-top width-fit\" style=\"max-height:640px; min-height: 200px\"></video>\n\n## Installation\n\nWe provide two installation methods. Method 2 (using uv) is recommended for faster installation and better dependency management.\n\n### Method 1: Using conda\n\n1. Create a new conda environment:\n\n```bash\nconda create -n open_manus python=3.12\nconda activate open_manus\n```\n\n2. Clone the repository:\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. Install dependencies:\n\n```bash\npip install -r requirements.txt\n```\n\n### Method 2: Using uv (Recommended)\n\n1. Install uv (A fast Python package installer and resolver):\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n```\n\n2. Clone the repository:\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. Create a new virtual environment and activate it:\n\n```bash\nuv venv --python 3.12\nsource .venv/bin/activate  # On Unix/macOS\n# Or on Windows:\n# .venv\\Scripts\\activate\n```\n\n4. Install dependencies:\n\n```bash\nuv pip install -r requirements.txt\n```\n\n### Browser Automation Tool (Optional)\n```bash\nplaywright install\n```\n\n## Configuration\n\nOpenManus requires configuration for the LLM APIs it uses. Follow these steps to set up your configuration:\n\n1. Create a `config.toml` file in the `config` directory (you can copy from the example):\n\n```bash\ncp config/config.example.toml config/config.toml\n```\n\n2. Edit `config/config.toml` to add your API keys and customize settings:\n\n```toml\n# Global LLM configuration\n[llm]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # Replace with your actual API key\nmax_tokens = 4096\ntemperature = 0.0\n\n# Optional configuration for specific LLM models\n[llm.vision]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # Replace with your actual API key\n```\n\n## Quick Start\n\nOne line for run OpenManus:\n\n```bash\npython main.py\n```\n\nThen input your idea via terminal!\n\nFor MCP tool version, you can run:\n```bash\npython run_mcp.py\n```\n\nFor unstable multi-agent version, you also can run:\n\n```bash\npython run_flow.py\n```\n\n### Custom Adding Multiple Agents\n\nCurrently, besides the general OpenManus Agent, we have also integrated the DataAnalysis Agent, which is suitable for data analysis and data visualization tasks. You can add this agent to `run_flow` in `config.toml`.\n\n```toml\n# Optional configuration for run-flow\n[runflow]\nuse_data_analysis_agent = true     # Disabled by default, change to true to activate\n```\nIn addition, you need to install the relevant dependencies to ensure the agent runs properly: [Detailed Installation Guide](app/tool/chart_visualization/README.md##Installation)\n\n## How to contribute\n\nWe welcome any friendly suggestions and helpful contributions! Just create issues or submit pull requests.\n\nOr contact @mannaandpoem via 📧email: mannaandpoem@gmail.com\n\n**Note**: Before submitting a pull request, please use the pre-commit tool to check your changes. Run `pre-commit run --all-files` to execute the checks.\n\n## Community Group\nJoin our networking group on Feishu and share your experience with other developers!\n\n<div align=\"center\" style=\"display: flex; gap: 20px;\">\n    <img src=\"assets/community_group.jpg\" alt=\"OpenManus 交流群\" width=\"300\" />\n</div>\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=FoundationAgents/OpenManus&type=Date)](https://star-history.com/#FoundationAgents/OpenManus&Date)\n\n## Sponsors\nThanks to [PPIO](https://ppinfra.com/user/register?invited_by=OCPKCN&utm_source=github_openmanus&utm_medium=github_readme&utm_campaign=link) for computing source support.\n> PPIO: The most affordable and easily-integrated MaaS and GPU cloud solution.\n\n\n## Acknowledgement\n\nThanks to [anthropic-computer-use](https://github.com/anthropics/anthropic-quickstarts/tree/main/computer-use-demo), [browser-use](https://github.com/browser-use/browser-use) and [crawl4ai](https://github.com/unclecode/crawl4ai) for providing basic support for this project!\n\nAdditionally, we are grateful to [AAAJ](https://github.com/metauto-ai/agent-as-a-judge), [MetaGPT](https://github.com/geekan/MetaGPT), [OpenHands](https://github.com/All-Hands-AI/OpenHands) and [SWE-agent](https://github.com/SWE-agent/SWE-agent).\n\nWe also thank stepfun(阶跃星辰) for supporting our Hugging Face demo space.\n\nOpenManus is built by contributors from MetaGPT. Huge thanks to this agent community!\n\n## Cite\n```bibtex\n@misc{openmanus2025,\n  author = {Xinbin Liang and Jinyu Xiang and Zhaoyang Yu and Jiayi Zhang and Sirui Hong and Sheng Fan and Xiao Tang and Bang Liu and Yuyu Luo and Chenglin Wu},\n  title = {OpenManus: An open-source framework for building general AI agents},\n  year = {2025},\n  publisher = {Zenodo},\n  doi = {10.5281/zenodo.15186407},\n  url = {https://doi.org/10.5281/zenodo.15186407},\n}\n```\n"
  },
  {
    "path": "README_ja.md",
    "content": "<p align=\"center\">\n  <img src=\"assets/logo.jpg\" width=\"200\"/>\n</p>\n\n[English](README.md) | [中文](README_zh.md) | [한국어](README_ko.md) | 日本語\n\n[![GitHub stars](https://img.shields.io/github/stars/FoundationAgents/OpenManus?style=social)](https://github.com/FoundationAgents/OpenManus/stargazers)\n&ensp;\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) &ensp;\n[![Discord Follow](https://dcbadge.vercel.app/api/server/DYn29wFk9z?style=flat)](https://discord.gg/DYn29wFk9z)\n[![Demo](https://img.shields.io/badge/Demo-Hugging%20Face-yellow)](https://huggingface.co/spaces/lyh-917/OpenManusDemo)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.15186407.svg)](https://doi.org/10.5281/zenodo.15186407)\n\n# 👋 OpenManus\n\nManusは素晴らしいですが、OpenManusは*招待コード*なしでどんなアイデアも実現できます！🛫\n\n私たちのチームメンバー [@Xinbin Liang](https://github.com/mannaandpoem) と [@Jinyu Xiang](https://github.com/XiangJinyu)（主要開発者）、そして [@Zhaoyang Yu](https://github.com/MoshiQAQ)、[@Jiayi Zhang](https://github.com/didiforgithub)、[@Sirui Hong](https://github.com/stellaHSR) は [@MetaGPT](https://github.com/geekan/MetaGPT) から来ました。プロトタイプは3時間以内に立ち上げられ、継続的に開発を進めています！\n\nこれはシンプルな実装ですので、どんな提案、貢献、フィードバックも歓迎します！\n\nOpenManusで自分だけのエージェントを楽しみましょう！\n\nまた、UIUCとOpenManusの研究者が共同開発した[OpenManus-RL](https://github.com/OpenManus/OpenManus-RL)をご紹介できることを嬉しく思います。これは強化学習（RL）ベース（GRPOなど）のLLMエージェントチューニング手法に特化したオープンソースプロジェクトです。\n\n## プロジェクトデモ\n\n<video src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" data-canonical-src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" controls=\"controls\" muted=\"muted\" class=\"d-block rounded-bottom-2 border-top width-fit\" style=\"max-height:640px; min-height: 200px\"></video>\n\n## インストール方法\n\nインストール方法は2つ提供しています。方法2（uvを使用）は、より高速なインストールと優れた依存関係管理のため推奨されています。\n\n### 方法1：condaを使用\n\n1. 新しいconda環境を作成します：\n\n```bash\nconda create -n open_manus python=3.12\nconda activate open_manus\n```\n\n2. リポジトリをクローンします：\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 依存関係をインストールします：\n\n```bash\npip install -r requirements.txt\n```\n\n### 方法2：uvを使用（推奨）\n\n1. uv（高速なPythonパッケージインストーラーと管理機能）をインストールします：\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n```\n\n2. リポジトリをクローンします：\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 新しい仮想環境を作成してアクティベートします：\n\n```bash\nuv venv --python 3.12\nsource .venv/bin/activate  # Unix/macOSの場合\n# Windowsの場合：\n# .venv\\Scripts\\activate\n```\n\n4. 依存関係をインストールします：\n\n```bash\nuv pip install -r requirements.txt\n```\n\n### ブラウザ自動化ツール（オプション）\n```bash\nplaywright install\n```\n\n## 設定\n\nOpenManusを使用するには、LLM APIの設定が必要です。以下の手順に従って設定してください：\n\n1. `config`ディレクトリに`config.toml`ファイルを作成します（サンプルからコピーできます）：\n\n```bash\ncp config/config.example.toml config/config.toml\n```\n\n2. `config/config.toml`を編集してAPIキーを追加し、設定をカスタマイズします：\n\n```toml\n# グローバルLLM設定\n[llm]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 実際のAPIキーに置き換えてください\nmax_tokens = 4096\ntemperature = 0.0\n\n# 特定のLLMモデル用のオプション設定\n[llm.vision]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 実際のAPIキーに置き換えてください\n```\n\n## クイックスタート\n\nOpenManusを実行する一行コマンド：\n\n```bash\npython main.py\n```\n\nその後、ターミナルからプロンプトを入力してください！\n\nMCP ツールバージョンを使用する場合は、以下を実行します：\n```bash\npython run_mcp.py\n```\n\n開発中のマルチエージェントバージョンを試すには、以下を実行します：\n\n```bash\npython run_flow.py\n```\n\n## カスタムマルチエージェントの追加\n\n現在、一般的なOpenManusエージェントに加えて、データ分析とデータ可視化タスクに適したDataAnalysisエージェントが組み込まれています。このエージェントを`config.toml`の`run_flow`に追加することができます。\n\n```toml\n# run-flowのオプション設定\n[runflow]\nuse_data_analysis_agent = true     # デフォルトでは無効、trueに変更すると有効化されます\n```\n\nこれに加えて、エージェントが正常に動作するために必要な依存関係をインストールする必要があります：[具体的なインストールガイド](app/tool/chart_visualization/README_ja.md##インストール)\n\n\n## 貢献方法\n\n我々は建設的な意見や有益な貢献を歓迎します！issueを作成するか、プルリクエストを提出してください。\n\nまたは @mannaandpoem に📧メールでご連絡ください：mannaandpoem@gmail.com\n\n**注意**: プルリクエストを送信する前に、pre-commitツールを使用して変更を確認してください。`pre-commit run --all-files`を実行してチェックを実行します。\n\n## コミュニティグループ\nFeishuのネットワーキンググループに参加して、他の開発者と経験を共有しましょう！\n\n<div align=\"center\" style=\"display: flex; gap: 20px;\">\n    <img src=\"assets/community_group.jpg\" alt=\"OpenManus 交流群\" width=\"300\" />\n</div>\n\n## スター履歴\n\n[![Star History Chart](https://api.star-history.com/svg?repos=FoundationAgents/OpenManus&type=Date)](https://star-history.com/#FoundationAgents/OpenManus&Date)\n\n## 謝辞\n\nこのプロジェクトの基本的なサポートを提供してくれた[anthropic-computer-use](https://github.com/anthropics/anthropic-quickstarts/tree/main/computer-use-demo)\nと[browser-use](https://github.com/browser-use/browser-use)に感謝します！\n\nさらに、[AAAJ](https://github.com/metauto-ai/agent-as-a-judge)、[MetaGPT](https://github.com/geekan/MetaGPT)、[OpenHands](https://github.com/All-Hands-AI/OpenHands)、[SWE-agent](https://github.com/SWE-agent/SWE-agent)にも感謝します。\n\nまた、Hugging Face デモスペースをサポートしてくださった阶跃星辰 (stepfun)にも感謝いたします。\n\nOpenManusはMetaGPTのコントリビューターによって構築されました。このエージェントコミュニティに大きな感謝を！\n\n## 引用\n```bibtex\n@misc{openmanus2025,\n  author = {Xinbin Liang and Jinyu Xiang and Zhaoyang Yu and Jiayi Zhang and Sirui Hong and Sheng Fan and Xiao Tang},\n  title = {OpenManus: An open-source framework for building general AI agents},\n  year = {2025},\n  publisher = {Zenodo},\n  doi = {10.5281/zenodo.15186407},\n  url = {https://doi.org/10.5281/zenodo.15186407},\n}\n```\n"
  },
  {
    "path": "README_ko.md",
    "content": "<p align=\"center\">\n  <img src=\"assets/logo.jpg\" width=\"200\"/>\n</p>\n\n[English](README.md) | [中文](README_zh.md) | 한국어 | [日本語](README_ja.md)\n\n[![GitHub stars](https://img.shields.io/github/stars/FoundationAgents/OpenManus?style=social)](https://github.com/FoundationAgents/OpenManus/stargazers)\n&ensp;\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) &ensp;\n[![Discord Follow](https://dcbadge.vercel.app/api/server/DYn29wFk9z?style=flat)](https://discord.gg/DYn29wFk9z)\n[![Demo](https://img.shields.io/badge/Demo-Hugging%20Face-yellow)](https://huggingface.co/spaces/lyh-917/OpenManusDemo)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.15186407.svg)](https://doi.org/10.5281/zenodo.15186407)\n\n# 👋 OpenManus\n\nManus는 놀라운 도구지만, OpenManus는 *초대 코드* 없이도 모든 아이디어를 실현할 수 있습니다! 🛫\n\n우리 팀의 멤버인 [@Xinbin Liang](https://github.com/mannaandpoem)와 [@Jinyu Xiang](https://github.com/XiangJinyu) (핵심 작성자), 그리고 [@Zhaoyang Yu](https://github.com/MoshiQAQ), [@Jiayi Zhang](https://github.com/didiforgithub), [@Sirui Hong](https://github.com/stellaHSR)이 함께 했습니다. 우리는 [@MetaGPT](https://github.com/geekan/MetaGPT)로부터 왔습니다. 프로토타입은 단 3시간 만에 출시되었으며, 계속해서 발전하고 있습니다!\n\n이 프로젝트는 간단한 구현에서 시작되었으며, 여러분의 제안, 기여 및 피드백을 환영합니다!\n\nOpenManus를 통해 여러분만의 에이전트를 즐겨보세요!\n\n또한 [OpenManus-RL](https://github.com/OpenManus/OpenManus-RL)을 소개하게 되어 기쁩니다. OpenManus와 UIUC 연구자들이 공동 개발한 이 오픈소스 프로젝트는 LLM 에이전트에 대해 강화 학습(RL) 기반 (예: GRPO) 튜닝 방법을 제공합니다.\n\n## 프로젝트 데모\n\n<video src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" data-canonical-src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" controls=\"controls\" muted=\"muted\" class=\"d-block rounded-bottom-2 border-top width-fit\" style=\"max-height:640px; min-height: 200px\"></video>\n\n## 설치 방법\n\n두 가지 설치 방법을 제공합니다. **방법 2 (uv 사용)** 이 더 빠른 설치와 효율적인 종속성 관리를 위해 권장됩니다.\n\n### 방법 1: conda 사용\n\n1. 새로운 conda 환경을 생성합니다:\n\n```bash\nconda create -n open_manus python=3.12\nconda activate open_manus\n```\n\n2. 저장소를 클론합니다:\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 종속성을 설치합니다:\n\n```bash\npip install -r requirements.txt\n```\n\n### 방법 2: uv 사용 (권장)\n\n1. uv를 설치합니다. (빠른 Python 패키지 설치 및 종속성 관리 도구):\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n```\n\n2. 저장소를 클론합니다:\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 새로운 가상 환경을 생성하고 활성화합니다:\n\n```bash\nuv venv --python 3.12\nsource .venv/bin/activate  # Unix/macOS의 경우\n# Windows의 경우:\n# .venv\\Scripts\\activate\n```\n\n4. 종속성을 설치합니다:\n\n```bash\nuv pip install -r requirements.txt\n```\n\n### 브라우저 자동화 도구 (선택사항)\n```bash\nplaywright install\n```\n\n## 설정 방법\n\nOpenManus를 사용하려면 사용하는 LLM API에 대한 설정이 필요합니다. 아래 단계를 따라 설정을 완료하세요:\n\n1. `config` 디렉토리에 `config.toml` 파일을 생성하세요 (예제 파일을 복사하여 사용할 수 있습니다):\n\n```bash\ncp config/config.example.toml config/config.toml\n```\n\n2. `config/config.toml` 파일을 편집하여 API 키를 추가하고 설정을 커스터마이징하세요:\n\n```toml\n# 전역 LLM 설정\n[llm]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 실제 API 키로 변경하세요\nmax_tokens = 4096\ntemperature = 0.0\n\n# 특정 LLM 모델에 대한 선택적 설정\n[llm.vision]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 실제 API 키로 변경하세요\n```\n\n## 빠른 시작\n\nOpenManus를 실행하는 한 줄 명령어:\n\n```bash\npython main.py\n```\n\n이후 터미널에서 아이디어를 작성하세요!\n\nMCP 도구 버전을 사용하려면 다음을 실행하세요:\n```bash\npython run_mcp.py\n```\n\n불안정한 멀티 에이전트 버전을 실행하려면 다음을 실행할 수 있습니다:\n\n```bash\npython run_flow.py\n```\n\n### 사용자 정의 다중 에이전트 추가\n\n현재 일반 OpenManus 에이전트 외에도 데이터 분석 및 데이터 시각화 작업에 적합한 DataAnalysis 에이전트를 통합했습니다. 이 에이전트를 `config.toml`의 `run_flow`에 추가할 수 있습니다.\n\n```toml\n# run-flow에 대한 선택적 구성\n[runflow]\nuse_data_analysis_agent = true     # 기본적으로 비활성화되어 있으며, 활성화하려면 true로 변경\n```\n\n또한, 에이전트가 제대로 작동하도록 관련 종속성을 설치해야 합니다: [상세 설치 가이드](app/tool/chart_visualization/README.md##Installation)\n\n## 기여 방법\n\n모든 친절한 제안과 유용한 기여를 환영합니다! 이슈를 생성하거나 풀 리퀘스트를 제출해 주세요.\n\n또는 📧 메일로 연락주세요. @mannaandpoem : mannaandpoem@gmail.com\n\n**참고**: pull request를 제출하기 전에 pre-commit 도구를 사용하여 변경 사항을 확인하십시오. `pre-commit run --all-files`를 실행하여 검사를 실행합니다.\n\n## 커뮤니티 그룹\nFeishu 네트워킹 그룹에 참여하여 다른 개발자들과 경험을 공유하세요!\n\n<div align=\"center\" style=\"display: flex; gap: 20px;\">\n    <img src=\"assets/community_group.jpg\" alt=\"OpenManus 交流群\" width=\"300\" />\n</div>\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=FoundationAgents/OpenManus&type=Date)](https://star-history.com/#FoundationAgents/OpenManus&Date)\n\n## 감사의 글\n\n이 프로젝트에 기본적인 지원을 제공해 주신 [anthropic-computer-use](https://github.com/anthropics/anthropic-quickstarts/tree/main/computer-use-demo)와\n[browser-use](https://github.com/browser-use/browser-use)에게 감사드립니다!\n\n또한, [AAAJ](https://github.com/metauto-ai/agent-as-a-judge), [MetaGPT](https://github.com/geekan/MetaGPT), [OpenHands](https://github.com/All-Hands-AI/OpenHands), [SWE-agent](https://github.com/SWE-agent/SWE-agent)에 깊은 감사를 드립니다.\n\n또한 Hugging Face 데모 공간을 지원해 주신 阶跃星辰 (stepfun)에게 감사드립니다.\n\nOpenManus는 MetaGPT 기여자들에 의해 개발되었습니다. 이 에이전트 커뮤니티에 깊은 감사를 전합니다!\n\n## 인용\n```bibtex\n@misc{openmanus2025,\n  author = {Xinbin Liang and Jinyu Xiang and Zhaoyang Yu and Jiayi Zhang and Sirui Hong and Sheng Fan and Xiao Tang},\n  title = {OpenManus: An open-source framework for building general AI agents},\n  year = {2025},\n  publisher = {Zenodo},\n  doi = {10.5281/zenodo.15186407},\n  url = {https://doi.org/10.5281/zenodo.15186407},\n}\n```\n"
  },
  {
    "path": "README_zh.md",
    "content": "<p align=\"center\">\n  <img src=\"assets/logo.jpg\" width=\"200\"/>\n</p>\n\n[English](README.md) | 中文 | [한국어](README_ko.md) | [日本語](README_ja.md)\n\n[![GitHub stars](https://img.shields.io/github/stars/FoundationAgents/OpenManus?style=social)](https://github.com/FoundationAgents/OpenManus/stargazers)\n&ensp;\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) &ensp;\n[![Discord Follow](https://dcbadge.vercel.app/api/server/DYn29wFk9z?style=flat)](https://discord.gg/DYn29wFk9z)\n[![Demo](https://img.shields.io/badge/Demo-Hugging%20Face-yellow)](https://huggingface.co/spaces/lyh-917/OpenManusDemo)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.15186407.svg)](https://doi.org/10.5281/zenodo.15186407)\n\n# 👋 OpenManus\n\nManus 非常棒，但 OpenManus 无需邀请码即可实现任何创意 🛫！\n\n我们的团队成员 [@Xinbin Liang](https://github.com/mannaandpoem) 和 [@Jinyu Xiang](https://github.com/XiangJinyu)（核心作者），以及 [@Zhaoyang Yu](https://github.com/MoshiQAQ)、[@Jiayi Zhang](https://github.com/didiforgithub) 和 [@Sirui Hong](https://github.com/stellaHSR)，来自 [@MetaGPT](https://github.com/geekan/MetaGPT)团队。我们在 3\n小时内完成了开发并持续迭代中！\n\n这是一个简洁的实现方案，欢迎任何建议、贡献和反馈！\n\n用 OpenManus 开启你的智能体之旅吧！\n\n我们也非常高兴地向大家介绍 [OpenManus-RL](https://github.com/OpenManus/OpenManus-RL)，这是一个专注于基于强化学习（RL，例如 GRPO）的方法来优化大语言模型（LLM）智能体的开源项目，由来自UIUC 和 OpenManus 的研究人员合作开发。\n\n## 项目演示\n\n<video src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" data-canonical-src=\"https://private-user-images.githubusercontent.com/61239030/420168772-6dcfd0d2-9142-45d9-b74e-d10aa75073c6.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDEzMTgwNTksIm5iZiI6MTc0MTMxNzc1OSwicGF0aCI6Ii82MTIzOTAzMC80MjAxNjg3NzItNmRjZmQwZDItOTE0Mi00NWQ5LWI3NGUtZDEwYWE3NTA3M2M2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzA3VDAzMjIzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZjFkNjlmYWNjMmEzOTliM2Y3M2VlYjgyNDRlZDJmOWE3NWZhZjE1MzhiZWY4YmQ3NjdkNTYwYTU5ZDA2MzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UuHQCgWYkh0OQq9qsUWqGsUbhG3i9jcZDAMeHjLt5T4\" controls=\"controls\" muted=\"muted\" class=\"d-block rounded-bottom-2 border-top width-fit\" style=\"max-height:640px; min-height: 200px\"></video>\n\n## 安装指南\n\n我们提供两种安装方式。推荐使用方式二（uv），因为它能提供更快的安装速度和更好的依赖管理。\n\n### 方式一：使用 conda\n\n1. 创建新的 conda 环境：\n\n```bash\nconda create -n open_manus python=3.12\nconda activate open_manus\n```\n\n2. 克隆仓库：\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 安装依赖：\n\n```bash\npip install -r requirements.txt\n```\n\n### 方式二：使用 uv（推荐）\n\n1. 安装 uv（一个快速的 Python 包管理器）：\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n```\n\n2. 克隆仓库：\n\n```bash\ngit clone https://github.com/FoundationAgents/OpenManus.git\ncd OpenManus\n```\n\n3. 创建并激活虚拟环境：\n\n```bash\nuv venv --python 3.12\nsource .venv/bin/activate  # Unix/macOS 系统\n# Windows 系统使用：\n# .venv\\Scripts\\activate\n```\n\n4. 安装依赖：\n\n```bash\nuv pip install -r requirements.txt\n```\n\n### 浏览器自动化工具（可选）\n```bash\nplaywright install\n```\n\n## 配置说明\n\nOpenManus 需要配置使用的 LLM API，请按以下步骤设置：\n\n1. 在 `config` 目录创建 `config.toml` 文件（可从示例复制）：\n\n```bash\ncp config/config.example.toml config/config.toml\n```\n\n2. 编辑 `config/config.toml` 添加 API 密钥和自定义设置：\n\n```toml\n# 全局 LLM 配置\n[llm]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 替换为真实 API 密钥\nmax_tokens = 4096\ntemperature = 0.0\n\n# 可选特定 LLM 模型配置\n[llm.vision]\nmodel = \"gpt-4o\"\nbase_url = \"https://api.openai.com/v1\"\napi_key = \"sk-...\"  # 替换为真实 API 密钥\n```\n\n## 快速启动\n\n一行命令运行 OpenManus：\n\n```bash\npython main.py\n```\n\n然后通过终端输入你的创意！\n\n如需使用 MCP 工具版本，可运行：\n```bash\npython run_mcp.py\n```\n\n如需体验不稳定的多智能体版本，可运行：\n\n```bash\npython run_flow.py\n```\n\n## 添加自定义多智能体\n\n目前除了通用的 OpenManus Agent, 我们还内置了DataAnalysis Agent，适用于数据分析和数据可视化任务，你可以在`config.toml`中将这个智能体加入到`run_flow`中\n```toml\n# run-flow可选配置\n[runflow]\nuse_data_analysis_agent = true     # 默认关闭，将其改为true则为激活\n```\n除此之外，你还需要安装相关的依赖来确保智能体正常运行：[具体安装指南](app/tool/chart_visualization/README_zh.md##安装)\n\n\n## 贡献指南\n\n我们欢迎任何友好的建议和有价值的贡献！可以直接创建 issue 或提交 pull request。\n\n或通过 📧 邮件联系 @mannaandpoem：mannaandpoem@gmail.com\n\n**注意**: 在提交 pull request 之前，请使用 pre-commit 工具检查您的更改。运行 `pre-commit run --all-files` 来执行检查。\n\n## 交流群\n\n加入我们的飞书交流群，与其他开发者分享经验！\n\n<div align=\"center\" style=\"display: flex; gap: 20px;\">\n    <img src=\"assets/community_group.jpg\" alt=\"OpenManus 交流群\" width=\"300\" />\n</div>\n\n## Star 数量\n\n[![Star History Chart](https://api.star-history.com/svg?repos=FoundationAgents/OpenManus&type=Date)](https://star-history.com/#FoundationAgents/OpenManus&Date)\n\n\n## 赞助商\n感谢[PPIO](https://ppinfra.com/user/register?invited_by=OCPKCN&utm_source=github_openmanus&utm_medium=github_readme&utm_campaign=link) 提供的算力支持。\n> PPIO派欧云：一键调用高性价比的开源模型API和GPU容器\n\n## 致谢\n\n特别感谢 [anthropic-computer-use](https://github.com/anthropics/anthropic-quickstarts/tree/main/computer-use-demo)\n和 [browser-use](https://github.com/browser-use/browser-use) 为本项目提供的基础支持！\n\n此外，我们感谢 [AAAJ](https://github.com/metauto-ai/agent-as-a-judge)，[MetaGPT](https://github.com/geekan/MetaGPT)，[OpenHands](https://github.com/All-Hands-AI/OpenHands) 和 [SWE-agent](https://github.com/SWE-agent/SWE-agent).\n\n我们也感谢阶跃星辰 (stepfun) 提供的 Hugging Face 演示空间支持。\n\nOpenManus 由 MetaGPT 社区的贡献者共同构建，感谢这个充满活力的智能体开发者社区！\n\n## 引用\n```bibtex\n@misc{openmanus2025,\n  author = {Xinbin Liang and Jinyu Xiang and Zhaoyang Yu and Jiayi Zhang and Sirui Hong and Sheng Fan and Xiao Tang},\n  title = {OpenManus: An open-source framework for building general AI agents},\n  year = {2025},\n  publisher = {Zenodo},\n  doi = {10.5281/zenodo.15186407},\n  url = {https://doi.org/10.5281/zenodo.15186407},\n}\n```\n"
  },
  {
    "path": "app/__init__.py",
    "content": "# Python version check: 3.11-3.13\nimport sys\n\n\nif sys.version_info < (3, 11) or sys.version_info > (3, 13):\n    print(\n        \"Warning: Unsupported Python version {ver}, please use 3.11-3.13\".format(\n            ver=\".\".join(map(str, sys.version_info))\n        )\n    )\n"
  },
  {
    "path": "app/agent/__init__.py",
    "content": "from app.agent.base import BaseAgent\nfrom app.agent.browser import BrowserAgent\nfrom app.agent.mcp import MCPAgent\nfrom app.agent.react import ReActAgent\nfrom app.agent.swe import SWEAgent\nfrom app.agent.toolcall import ToolCallAgent\n\n\n__all__ = [\n    \"BaseAgent\",\n    \"BrowserAgent\",\n    \"ReActAgent\",\n    \"SWEAgent\",\n    \"ToolCallAgent\",\n    \"MCPAgent\",\n]\n"
  },
  {
    "path": "app/agent/base.py",
    "content": "from abc import ABC, abstractmethod\nfrom contextlib import asynccontextmanager\nfrom typing import List, Optional\n\nfrom pydantic import BaseModel, Field, model_validator\n\nfrom app.llm import LLM\nfrom app.logger import logger\nfrom app.sandbox.client import SANDBOX_CLIENT\nfrom app.schema import ROLE_TYPE, AgentState, Memory, Message\n\n\nclass BaseAgent(BaseModel, ABC):\n    \"\"\"Abstract base class for managing agent state and execution.\n\n    Provides foundational functionality for state transitions, memory management,\n    and a step-based execution loop. Subclasses must implement the `step` method.\n    \"\"\"\n\n    # Core attributes\n    name: str = Field(..., description=\"Unique name of the agent\")\n    description: Optional[str] = Field(None, description=\"Optional agent description\")\n\n    # Prompts\n    system_prompt: Optional[str] = Field(\n        None, description=\"System-level instruction prompt\"\n    )\n    next_step_prompt: Optional[str] = Field(\n        None, description=\"Prompt for determining next action\"\n    )\n\n    # Dependencies\n    llm: LLM = Field(default_factory=LLM, description=\"Language model instance\")\n    memory: Memory = Field(default_factory=Memory, description=\"Agent's memory store\")\n    state: AgentState = Field(\n        default=AgentState.IDLE, description=\"Current agent state\"\n    )\n\n    # Execution control\n    max_steps: int = Field(default=10, description=\"Maximum steps before termination\")\n    current_step: int = Field(default=0, description=\"Current step in execution\")\n\n    duplicate_threshold: int = 2\n\n    class Config:\n        arbitrary_types_allowed = True\n        extra = \"allow\"  # Allow extra fields for flexibility in subclasses\n\n    @model_validator(mode=\"after\")\n    def initialize_agent(self) -> \"BaseAgent\":\n        \"\"\"Initialize agent with default settings if not provided.\"\"\"\n        if self.llm is None or not isinstance(self.llm, LLM):\n            self.llm = LLM(config_name=self.name.lower())\n        if not isinstance(self.memory, Memory):\n            self.memory = Memory()\n        return self\n\n    @asynccontextmanager\n    async def state_context(self, new_state: AgentState):\n        \"\"\"Context manager for safe agent state transitions.\n\n        Args:\n            new_state: The state to transition to during the context.\n\n        Yields:\n            None: Allows execution within the new state.\n\n        Raises:\n            ValueError: If the new_state is invalid.\n        \"\"\"\n        if not isinstance(new_state, AgentState):\n            raise ValueError(f\"Invalid state: {new_state}\")\n\n        previous_state = self.state\n        self.state = new_state\n        try:\n            yield\n        except Exception as e:\n            self.state = AgentState.ERROR  # Transition to ERROR on failure\n            raise e\n        finally:\n            self.state = previous_state  # Revert to previous state\n\n    def update_memory(\n        self,\n        role: ROLE_TYPE,  # type: ignore\n        content: str,\n        base64_image: Optional[str] = None,\n        **kwargs,\n    ) -> None:\n        \"\"\"Add a message to the agent's memory.\n\n        Args:\n            role: The role of the message sender (user, system, assistant, tool).\n            content: The message content.\n            base64_image: Optional base64 encoded image.\n            **kwargs: Additional arguments (e.g., tool_call_id for tool messages).\n\n        Raises:\n            ValueError: If the role is unsupported.\n        \"\"\"\n        message_map = {\n            \"user\": Message.user_message,\n            \"system\": Message.system_message,\n            \"assistant\": Message.assistant_message,\n            \"tool\": lambda content, **kw: Message.tool_message(content, **kw),\n        }\n\n        if role not in message_map:\n            raise ValueError(f\"Unsupported message role: {role}\")\n\n        # Create message with appropriate parameters based on role\n        kwargs = {\"base64_image\": base64_image, **(kwargs if role == \"tool\" else {})}\n        self.memory.add_message(message_map[role](content, **kwargs))\n\n    async def run(self, request: Optional[str] = None) -> str:\n        \"\"\"Execute the agent's main loop asynchronously.\n\n        Args:\n            request: Optional initial user request to process.\n\n        Returns:\n            A string summarizing the execution results.\n\n        Raises:\n            RuntimeError: If the agent is not in IDLE state at start.\n        \"\"\"\n        if self.state != AgentState.IDLE:\n            raise RuntimeError(f\"Cannot run agent from state: {self.state}\")\n\n        if request:\n            self.update_memory(\"user\", request)\n\n        results: List[str] = []\n        async with self.state_context(AgentState.RUNNING):\n            while (\n                self.current_step < self.max_steps and self.state != AgentState.FINISHED\n            ):\n                self.current_step += 1\n                logger.info(f\"Executing step {self.current_step}/{self.max_steps}\")\n                step_result = await self.step()\n\n                # Check for stuck state\n                if self.is_stuck():\n                    self.handle_stuck_state()\n\n                results.append(f\"Step {self.current_step}: {step_result}\")\n\n            if self.current_step >= self.max_steps:\n                self.current_step = 0\n                self.state = AgentState.IDLE\n                results.append(f\"Terminated: Reached max steps ({self.max_steps})\")\n        await SANDBOX_CLIENT.cleanup()\n        return \"\\n\".join(results) if results else \"No steps executed\"\n\n    @abstractmethod\n    async def step(self) -> str:\n        \"\"\"Execute a single step in the agent's workflow.\n\n        Must be implemented by subclasses to define specific behavior.\n        \"\"\"\n\n    def handle_stuck_state(self):\n        \"\"\"Handle stuck state by adding a prompt to change strategy\"\"\"\n        stuck_prompt = \"\\\n        Observed duplicate responses. Consider new strategies and avoid repeating ineffective paths already attempted.\"\n        self.next_step_prompt = f\"{stuck_prompt}\\n{self.next_step_prompt}\"\n        logger.warning(f\"Agent detected stuck state. Added prompt: {stuck_prompt}\")\n\n    def is_stuck(self) -> bool:\n        \"\"\"Check if the agent is stuck in a loop by detecting duplicate content\"\"\"\n        if len(self.memory.messages) < 2:\n            return False\n\n        last_message = self.memory.messages[-1]\n        if not last_message.content:\n            return False\n\n        # Count identical content occurrences\n        duplicate_count = sum(\n            1\n            for msg in reversed(self.memory.messages[:-1])\n            if msg.role == \"assistant\" and msg.content == last_message.content\n        )\n\n        return duplicate_count >= self.duplicate_threshold\n\n    @property\n    def messages(self) -> List[Message]:\n        \"\"\"Retrieve a list of messages from the agent's memory.\"\"\"\n        return self.memory.messages\n\n    @messages.setter\n    def messages(self, value: List[Message]):\n        \"\"\"Set the list of messages in the agent's memory.\"\"\"\n        self.memory.messages = value\n"
  },
  {
    "path": "app/agent/browser.py",
    "content": "import json\nfrom typing import TYPE_CHECKING, Optional\n\nfrom pydantic import Field, model_validator\n\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.logger import logger\nfrom app.prompt.browser import NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.schema import Message, ToolChoice\nfrom app.tool import BrowserUseTool, Terminate, ToolCollection\nfrom app.tool.sandbox.sb_browser_tool import SandboxBrowserTool\n\n\n# Avoid circular import if BrowserAgent needs BrowserContextHelper\nif TYPE_CHECKING:\n    from app.agent.base import BaseAgent  # Or wherever memory is defined\n\n\nclass BrowserContextHelper:\n    def __init__(self, agent: \"BaseAgent\"):\n        self.agent = agent\n        self._current_base64_image: Optional[str] = None\n\n    async def get_browser_state(self) -> Optional[dict]:\n        browser_tool = self.agent.available_tools.get_tool(BrowserUseTool().name)\n        if not browser_tool:\n            browser_tool = self.agent.available_tools.get_tool(\n                SandboxBrowserTool().name\n            )\n        if not browser_tool or not hasattr(browser_tool, \"get_current_state\"):\n            logger.warning(\"BrowserUseTool not found or doesn't have get_current_state\")\n            return None\n        try:\n            result = await browser_tool.get_current_state()\n            if result.error:\n                logger.debug(f\"Browser state error: {result.error}\")\n                return None\n            if hasattr(result, \"base64_image\") and result.base64_image:\n                self._current_base64_image = result.base64_image\n            else:\n                self._current_base64_image = None\n            return json.loads(result.output)\n        except Exception as e:\n            logger.debug(f\"Failed to get browser state: {str(e)}\")\n            return None\n\n    async def format_next_step_prompt(self) -> str:\n        \"\"\"Gets browser state and formats the browser prompt.\"\"\"\n        browser_state = await self.get_browser_state()\n        url_info, tabs_info, content_above_info, content_below_info = \"\", \"\", \"\", \"\"\n        results_info = \"\"  # Or get from agent if needed elsewhere\n\n        if browser_state and not browser_state.get(\"error\"):\n            url_info = f\"\\n   URL: {browser_state.get('url', 'N/A')}\\n   Title: {browser_state.get('title', 'N/A')}\"\n            tabs = browser_state.get(\"tabs\", [])\n            if tabs:\n                tabs_info = f\"\\n   {len(tabs)} tab(s) available\"\n            pixels_above = browser_state.get(\"pixels_above\", 0)\n            pixels_below = browser_state.get(\"pixels_below\", 0)\n            if pixels_above > 0:\n                content_above_info = f\" ({pixels_above} pixels)\"\n            if pixels_below > 0:\n                content_below_info = f\" ({pixels_below} pixels)\"\n\n            if self._current_base64_image:\n                image_message = Message.user_message(\n                    content=\"Current browser screenshot:\",\n                    base64_image=self._current_base64_image,\n                )\n                self.agent.memory.add_message(image_message)\n                self._current_base64_image = None  # Consume the image after adding\n\n        return NEXT_STEP_PROMPT.format(\n            url_placeholder=url_info,\n            tabs_placeholder=tabs_info,\n            content_above_placeholder=content_above_info,\n            content_below_placeholder=content_below_info,\n            results_placeholder=results_info,\n        )\n\n    async def cleanup_browser(self):\n        browser_tool = self.agent.available_tools.get_tool(BrowserUseTool().name)\n        if browser_tool and hasattr(browser_tool, \"cleanup\"):\n            await browser_tool.cleanup()\n\n\nclass BrowserAgent(ToolCallAgent):\n    \"\"\"\n    A browser agent that uses the browser_use library to control a browser.\n\n    This agent can navigate web pages, interact with elements, fill forms,\n    extract content, and perform other browser-based actions to accomplish tasks.\n    \"\"\"\n\n    name: str = \"browser\"\n    description: str = \"A browser agent that can control a browser to accomplish tasks\"\n\n    system_prompt: str = SYSTEM_PROMPT\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    max_observe: int = 10000\n    max_steps: int = 20\n\n    # Configure the available tools\n    available_tools: ToolCollection = Field(\n        default_factory=lambda: ToolCollection(BrowserUseTool(), Terminate())\n    )\n\n    # Use Auto for tool choice to allow both tool usage and free-form responses\n    tool_choices: ToolChoice = ToolChoice.AUTO\n    special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name])\n\n    browser_context_helper: Optional[BrowserContextHelper] = None\n\n    @model_validator(mode=\"after\")\n    def initialize_helper(self) -> \"BrowserAgent\":\n        self.browser_context_helper = BrowserContextHelper(self)\n        return self\n\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next actions using tools, with browser state info added\"\"\"\n        self.next_step_prompt = (\n            await self.browser_context_helper.format_next_step_prompt()\n        )\n        return await super().think()\n\n    async def cleanup(self):\n        \"\"\"Clean up browser agent resources by calling parent cleanup.\"\"\"\n        await self.browser_context_helper.cleanup_browser()\n"
  },
  {
    "path": "app/agent/data_analysis.py",
    "content": "from pydantic import Field\n\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.config import config\nfrom app.prompt.visualization import NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.tool import Terminate, ToolCollection\nfrom app.tool.chart_visualization.chart_prepare import VisualizationPrepare\nfrom app.tool.chart_visualization.data_visualization import DataVisualization\nfrom app.tool.chart_visualization.python_execute import NormalPythonExecute\n\n\nclass DataAnalysis(ToolCallAgent):\n    \"\"\"\n    A data analysis agent that uses planning to solve various data analysis tasks.\n\n    This agent extends ToolCallAgent with a comprehensive set of tools and capabilities,\n    including Data Analysis, Chart Visualization, Data Report.\n    \"\"\"\n\n    name: str = \"Data_Analysis\"\n    description: str = \"An analytical agent that utilizes python and data visualization tools to solve diverse data analysis tasks\"\n\n    system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root)\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    max_observe: int = 15000\n    max_steps: int = 20\n\n    # Add general-purpose tools to the tool collection\n    available_tools: ToolCollection = Field(\n        default_factory=lambda: ToolCollection(\n            NormalPythonExecute(),\n            VisualizationPrepare(),\n            DataVisualization(),\n            Terminate(),\n        )\n    )\n"
  },
  {
    "path": "app/agent/manus.py",
    "content": "from typing import Dict, List, Optional\n\nfrom pydantic import Field, model_validator\n\nfrom app.agent.browser import BrowserContextHelper\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.config import config\nfrom app.logger import logger\nfrom app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.tool import Terminate, ToolCollection\nfrom app.tool.ask_human import AskHuman\nfrom app.tool.browser_use_tool import BrowserUseTool\nfrom app.tool.mcp import MCPClients, MCPClientTool\nfrom app.tool.python_execute import PythonExecute\nfrom app.tool.str_replace_editor import StrReplaceEditor\n\n\nclass Manus(ToolCallAgent):\n    \"\"\"A versatile general-purpose agent with support for both local and MCP tools.\"\"\"\n\n    name: str = \"Manus\"\n    description: str = \"A versatile agent that can solve various tasks using multiple tools including MCP-based tools\"\n\n    system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root)\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    max_observe: int = 10000\n    max_steps: int = 20\n\n    # MCP clients for remote tool access\n    mcp_clients: MCPClients = Field(default_factory=MCPClients)\n\n    # Add general-purpose tools to the tool collection\n    available_tools: ToolCollection = Field(\n        default_factory=lambda: ToolCollection(\n            PythonExecute(),\n            BrowserUseTool(),\n            StrReplaceEditor(),\n            AskHuman(),\n            Terminate(),\n        )\n    )\n\n    special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name])\n    browser_context_helper: Optional[BrowserContextHelper] = None\n\n    # Track connected MCP servers\n    connected_servers: Dict[str, str] = Field(\n        default_factory=dict\n    )  # server_id -> url/command\n    _initialized: bool = False\n\n    @model_validator(mode=\"after\")\n    def initialize_helper(self) -> \"Manus\":\n        \"\"\"Initialize basic components synchronously.\"\"\"\n        self.browser_context_helper = BrowserContextHelper(self)\n        return self\n\n    @classmethod\n    async def create(cls, **kwargs) -> \"Manus\":\n        \"\"\"Factory method to create and properly initialize a Manus instance.\"\"\"\n        instance = cls(**kwargs)\n        await instance.initialize_mcp_servers()\n        instance._initialized = True\n        return instance\n\n    async def initialize_mcp_servers(self) -> None:\n        \"\"\"Initialize connections to configured MCP servers.\"\"\"\n        for server_id, server_config in config.mcp_config.servers.items():\n            try:\n                if server_config.type == \"sse\":\n                    if server_config.url:\n                        await self.connect_mcp_server(server_config.url, server_id)\n                        logger.info(\n                            f\"Connected to MCP server {server_id} at {server_config.url}\"\n                        )\n                elif server_config.type == \"stdio\":\n                    if server_config.command:\n                        await self.connect_mcp_server(\n                            server_config.command,\n                            server_id,\n                            use_stdio=True,\n                            stdio_args=server_config.args,\n                        )\n                        logger.info(\n                            f\"Connected to MCP server {server_id} using command {server_config.command}\"\n                        )\n            except Exception as e:\n                logger.error(f\"Failed to connect to MCP server {server_id}: {e}\")\n\n    async def connect_mcp_server(\n        self,\n        server_url: str,\n        server_id: str = \"\",\n        use_stdio: bool = False,\n        stdio_args: List[str] = None,\n    ) -> None:\n        \"\"\"Connect to an MCP server and add its tools.\"\"\"\n        if use_stdio:\n            await self.mcp_clients.connect_stdio(\n                server_url, stdio_args or [], server_id\n            )\n            self.connected_servers[server_id or server_url] = server_url\n        else:\n            await self.mcp_clients.connect_sse(server_url, server_id)\n            self.connected_servers[server_id or server_url] = server_url\n\n        # Update available tools with only the new tools from this server\n        new_tools = [\n            tool for tool in self.mcp_clients.tools if tool.server_id == server_id\n        ]\n        self.available_tools.add_tools(*new_tools)\n\n    async def disconnect_mcp_server(self, server_id: str = \"\") -> None:\n        \"\"\"Disconnect from an MCP server and remove its tools.\"\"\"\n        await self.mcp_clients.disconnect(server_id)\n        if server_id:\n            self.connected_servers.pop(server_id, None)\n        else:\n            self.connected_servers.clear()\n\n        # Rebuild available tools without the disconnected server's tools\n        base_tools = [\n            tool\n            for tool in self.available_tools.tools\n            if not isinstance(tool, MCPClientTool)\n        ]\n        self.available_tools = ToolCollection(*base_tools)\n        self.available_tools.add_tools(*self.mcp_clients.tools)\n\n    async def cleanup(self):\n        \"\"\"Clean up Manus agent resources.\"\"\"\n        if self.browser_context_helper:\n            await self.browser_context_helper.cleanup_browser()\n        # Disconnect from all MCP servers only if we were initialized\n        if self._initialized:\n            await self.disconnect_mcp_server()\n            self._initialized = False\n\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next actions with appropriate context.\"\"\"\n        if not self._initialized:\n            await self.initialize_mcp_servers()\n            self._initialized = True\n\n        original_prompt = self.next_step_prompt\n        recent_messages = self.memory.messages[-3:] if self.memory.messages else []\n        browser_in_use = any(\n            tc.function.name == BrowserUseTool().name\n            for msg in recent_messages\n            if msg.tool_calls\n            for tc in msg.tool_calls\n        )\n\n        if browser_in_use:\n            self.next_step_prompt = (\n                await self.browser_context_helper.format_next_step_prompt()\n            )\n\n        result = await super().think()\n\n        # Restore original prompt\n        self.next_step_prompt = original_prompt\n\n        return result\n"
  },
  {
    "path": "app/agent/mcp.py",
    "content": "from typing import Any, Dict, List, Optional, Tuple\n\nfrom pydantic import Field\n\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.logger import logger\nfrom app.prompt.mcp import MULTIMEDIA_RESPONSE_PROMPT, NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.schema import AgentState, Message\nfrom app.tool.base import ToolResult\nfrom app.tool.mcp import MCPClients\n\n\nclass MCPAgent(ToolCallAgent):\n    \"\"\"Agent for interacting with MCP (Model Context Protocol) servers.\n\n    This agent connects to an MCP server using either SSE or stdio transport\n    and makes the server's tools available through the agent's tool interface.\n    \"\"\"\n\n    name: str = \"mcp_agent\"\n    description: str = \"An agent that connects to an MCP server and uses its tools.\"\n\n    system_prompt: str = SYSTEM_PROMPT\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    # Initialize MCP tool collection\n    mcp_clients: MCPClients = Field(default_factory=MCPClients)\n    available_tools: MCPClients = None  # Will be set in initialize()\n\n    max_steps: int = 20\n    connection_type: str = \"stdio\"  # \"stdio\" or \"sse\"\n\n    # Track tool schemas to detect changes\n    tool_schemas: Dict[str, Dict[str, Any]] = Field(default_factory=dict)\n    _refresh_tools_interval: int = 5  # Refresh tools every N steps\n\n    # Special tool names that should trigger termination\n    special_tool_names: List[str] = Field(default_factory=lambda: [\"terminate\"])\n\n    async def initialize(\n        self,\n        connection_type: Optional[str] = None,\n        server_url: Optional[str] = None,\n        command: Optional[str] = None,\n        args: Optional[List[str]] = None,\n    ) -> None:\n        \"\"\"Initialize the MCP connection.\n\n        Args:\n            connection_type: Type of connection to use (\"stdio\" or \"sse\")\n            server_url: URL of the MCP server (for SSE connection)\n            command: Command to run (for stdio connection)\n            args: Arguments for the command (for stdio connection)\n        \"\"\"\n        if connection_type:\n            self.connection_type = connection_type\n\n        # Connect to the MCP server based on connection type\n        if self.connection_type == \"sse\":\n            if not server_url:\n                raise ValueError(\"Server URL is required for SSE connection\")\n            await self.mcp_clients.connect_sse(server_url=server_url)\n        elif self.connection_type == \"stdio\":\n            if not command:\n                raise ValueError(\"Command is required for stdio connection\")\n            await self.mcp_clients.connect_stdio(command=command, args=args or [])\n        else:\n            raise ValueError(f\"Unsupported connection type: {self.connection_type}\")\n\n        # Set available_tools to our MCP instance\n        self.available_tools = self.mcp_clients\n\n        # Store initial tool schemas\n        await self._refresh_tools()\n\n        # Add system message about available tools\n        tool_names = list(self.mcp_clients.tool_map.keys())\n        tools_info = \", \".join(tool_names)\n\n        # Add system prompt and available tools information\n        self.memory.add_message(\n            Message.system_message(\n                f\"{self.system_prompt}\\n\\nAvailable MCP tools: {tools_info}\"\n            )\n        )\n\n    async def _refresh_tools(self) -> Tuple[List[str], List[str]]:\n        \"\"\"Refresh the list of available tools from the MCP server.\n\n        Returns:\n            A tuple of (added_tools, removed_tools)\n        \"\"\"\n        if not self.mcp_clients.sessions:\n            return [], []\n\n        # Get current tool schemas directly from the server\n        response = await self.mcp_clients.list_tools()\n        current_tools = {tool.name: tool.inputSchema for tool in response.tools}\n\n        # Determine added, removed, and changed tools\n        current_names = set(current_tools.keys())\n        previous_names = set(self.tool_schemas.keys())\n\n        added_tools = list(current_names - previous_names)\n        removed_tools = list(previous_names - current_names)\n\n        # Check for schema changes in existing tools\n        changed_tools = []\n        for name in current_names.intersection(previous_names):\n            if current_tools[name] != self.tool_schemas.get(name):\n                changed_tools.append(name)\n\n        # Update stored schemas\n        self.tool_schemas = current_tools\n\n        # Log and notify about changes\n        if added_tools:\n            logger.info(f\"Added MCP tools: {added_tools}\")\n            self.memory.add_message(\n                Message.system_message(f\"New tools available: {', '.join(added_tools)}\")\n            )\n        if removed_tools:\n            logger.info(f\"Removed MCP tools: {removed_tools}\")\n            self.memory.add_message(\n                Message.system_message(\n                    f\"Tools no longer available: {', '.join(removed_tools)}\"\n                )\n            )\n        if changed_tools:\n            logger.info(f\"Changed MCP tools: {changed_tools}\")\n\n        return added_tools, removed_tools\n\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next action.\"\"\"\n        # Check MCP session and tools availability\n        if not self.mcp_clients.sessions or not self.mcp_clients.tool_map:\n            logger.info(\"MCP service is no longer available, ending interaction\")\n            self.state = AgentState.FINISHED\n            return False\n\n        # Refresh tools periodically\n        if self.current_step % self._refresh_tools_interval == 0:\n            await self._refresh_tools()\n            # All tools removed indicates shutdown\n            if not self.mcp_clients.tool_map:\n                logger.info(\"MCP service has shut down, ending interaction\")\n                self.state = AgentState.FINISHED\n                return False\n\n        # Use the parent class's think method\n        return await super().think()\n\n    async def _handle_special_tool(self, name: str, result: Any, **kwargs) -> None:\n        \"\"\"Handle special tool execution and state changes\"\"\"\n        # First process with parent handler\n        await super()._handle_special_tool(name, result, **kwargs)\n\n        # Handle multimedia responses\n        if isinstance(result, ToolResult) and result.base64_image:\n            self.memory.add_message(\n                Message.system_message(\n                    MULTIMEDIA_RESPONSE_PROMPT.format(tool_name=name)\n                )\n            )\n\n    def _should_finish_execution(self, name: str, **kwargs) -> bool:\n        \"\"\"Determine if tool execution should finish the agent\"\"\"\n        # Terminate if the tool name is 'terminate'\n        return name.lower() == \"terminate\"\n\n    async def cleanup(self) -> None:\n        \"\"\"Clean up MCP connection when done.\"\"\"\n        if self.mcp_clients.sessions:\n            await self.mcp_clients.disconnect()\n            logger.info(\"MCP connection closed\")\n\n    async def run(self, request: Optional[str] = None) -> str:\n        \"\"\"Run the agent with cleanup when done.\"\"\"\n        try:\n            result = await super().run(request)\n            return result\n        finally:\n            # Ensure cleanup happens even if there's an error\n            await self.cleanup()\n"
  },
  {
    "path": "app/agent/react.py",
    "content": "from abc import ABC, abstractmethod\nfrom typing import Optional\n\nfrom pydantic import Field\n\nfrom app.agent.base import BaseAgent\nfrom app.llm import LLM\nfrom app.schema import AgentState, Memory\n\n\nclass ReActAgent(BaseAgent, ABC):\n    name: str\n    description: Optional[str] = None\n\n    system_prompt: Optional[str] = None\n    next_step_prompt: Optional[str] = None\n\n    llm: Optional[LLM] = Field(default_factory=LLM)\n    memory: Memory = Field(default_factory=Memory)\n    state: AgentState = AgentState.IDLE\n\n    max_steps: int = 10\n    current_step: int = 0\n\n    @abstractmethod\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next action\"\"\"\n\n    @abstractmethod\n    async def act(self) -> str:\n        \"\"\"Execute decided actions\"\"\"\n\n    async def step(self) -> str:\n        \"\"\"Execute a single step: think and act.\"\"\"\n        should_act = await self.think()\n        if not should_act:\n            return \"Thinking complete - no action needed\"\n        return await self.act()\n"
  },
  {
    "path": "app/agent/sandbox_agent.py",
    "content": "from typing import Dict, List, Optional\n\nfrom pydantic import Field, model_validator\n\nfrom app.agent.browser import BrowserContextHelper\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.config import config\nfrom app.daytona.sandbox import create_sandbox, delete_sandbox\nfrom app.daytona.tool_base import SandboxToolsBase\nfrom app.logger import logger\nfrom app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.tool import Terminate, ToolCollection\nfrom app.tool.ask_human import AskHuman\nfrom app.tool.mcp import MCPClients, MCPClientTool\nfrom app.tool.sandbox.sb_browser_tool import SandboxBrowserTool\nfrom app.tool.sandbox.sb_files_tool import SandboxFilesTool\nfrom app.tool.sandbox.sb_shell_tool import SandboxShellTool\nfrom app.tool.sandbox.sb_vision_tool import SandboxVisionTool\n\n\nclass SandboxManus(ToolCallAgent):\n    \"\"\"A versatile general-purpose agent with support for both local and MCP tools.\"\"\"\n\n    name: str = \"SandboxManus\"\n    description: str = \"A versatile agent that can solve various tasks using multiple sandbox-tools including MCP-based tools\"\n\n    system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root)\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    max_observe: int = 10000\n    max_steps: int = 20\n\n    # MCP clients for remote tool access\n    mcp_clients: MCPClients = Field(default_factory=MCPClients)\n\n    # Add general-purpose tools to the tool collection\n    available_tools: ToolCollection = Field(\n        default_factory=lambda: ToolCollection(\n            # PythonExecute(),\n            # BrowserUseTool(),\n            # StrReplaceEditor(),\n            AskHuman(),\n            Terminate(),\n        )\n    )\n\n    special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name])\n    browser_context_helper: Optional[BrowserContextHelper] = None\n\n    # Track connected MCP servers\n    connected_servers: Dict[str, str] = Field(\n        default_factory=dict\n    )  # server_id -> url/command\n    _initialized: bool = False\n    sandbox_link: Optional[dict[str, dict[str, str]]] = Field(default_factory=dict)\n\n    @model_validator(mode=\"after\")\n    def initialize_helper(self) -> \"SandboxManus\":\n        \"\"\"Initialize basic components synchronously.\"\"\"\n        self.browser_context_helper = BrowserContextHelper(self)\n        return self\n\n    @classmethod\n    async def create(cls, **kwargs) -> \"SandboxManus\":\n        \"\"\"Factory method to create and properly initialize a Manus instance.\"\"\"\n        instance = cls(**kwargs)\n        await instance.initialize_mcp_servers()\n        await instance.initialize_sandbox_tools()\n        instance._initialized = True\n        return instance\n\n    async def initialize_sandbox_tools(\n        self,\n        password: str = config.daytona.VNC_password,\n    ) -> None:\n        try:\n            # 创建新沙箱\n            if password:\n                sandbox = create_sandbox(password=password)\n                self.sandbox = sandbox\n            else:\n                raise ValueError(\"password must be provided\")\n            vnc_link = sandbox.get_preview_link(6080)\n            website_link = sandbox.get_preview_link(8080)\n            vnc_url = vnc_link.url if hasattr(vnc_link, \"url\") else str(vnc_link)\n            website_url = (\n                website_link.url if hasattr(website_link, \"url\") else str(website_link)\n            )\n\n            # Get the actual sandbox_id from the created sandbox\n            actual_sandbox_id = sandbox.id if hasattr(sandbox, \"id\") else \"new_sandbox\"\n            if not self.sandbox_link:\n                self.sandbox_link = {}\n            self.sandbox_link[actual_sandbox_id] = {\n                \"vnc\": vnc_url,\n                \"website\": website_url,\n            }\n            logger.info(f\"VNC URL: {vnc_url}\")\n            logger.info(f\"Website URL: {website_url}\")\n            SandboxToolsBase._urls_printed = True\n            sb_tools = [\n                SandboxBrowserTool(sandbox),\n                SandboxFilesTool(sandbox),\n                SandboxShellTool(sandbox),\n                SandboxVisionTool(sandbox),\n            ]\n            self.available_tools.add_tools(*sb_tools)\n\n        except Exception as e:\n            logger.error(f\"Error initializing sandbox tools: {e}\")\n            raise\n\n    async def initialize_mcp_servers(self) -> None:\n        \"\"\"Initialize connections to configured MCP servers.\"\"\"\n        for server_id, server_config in config.mcp_config.servers.items():\n            try:\n                if server_config.type == \"sse\":\n                    if server_config.url:\n                        await self.connect_mcp_server(server_config.url, server_id)\n                        logger.info(\n                            f\"Connected to MCP server {server_id} at {server_config.url}\"\n                        )\n                elif server_config.type == \"stdio\":\n                    if server_config.command:\n                        await self.connect_mcp_server(\n                            server_config.command,\n                            server_id,\n                            use_stdio=True,\n                            stdio_args=server_config.args,\n                        )\n                        logger.info(\n                            f\"Connected to MCP server {server_id} using command {server_config.command}\"\n                        )\n            except Exception as e:\n                logger.error(f\"Failed to connect to MCP server {server_id}: {e}\")\n\n    async def connect_mcp_server(\n        self,\n        server_url: str,\n        server_id: str = \"\",\n        use_stdio: bool = False,\n        stdio_args: List[str] = None,\n    ) -> None:\n        \"\"\"Connect to an MCP server and add its tools.\"\"\"\n        if use_stdio:\n            await self.mcp_clients.connect_stdio(\n                server_url, stdio_args or [], server_id\n            )\n            self.connected_servers[server_id or server_url] = server_url\n        else:\n            await self.mcp_clients.connect_sse(server_url, server_id)\n            self.connected_servers[server_id or server_url] = server_url\n\n        # Update available tools with only the new tools from this server\n        new_tools = [\n            tool for tool in self.mcp_clients.tools if tool.server_id == server_id\n        ]\n        self.available_tools.add_tools(*new_tools)\n\n    async def disconnect_mcp_server(self, server_id: str = \"\") -> None:\n        \"\"\"Disconnect from an MCP server and remove its tools.\"\"\"\n        await self.mcp_clients.disconnect(server_id)\n        if server_id:\n            self.connected_servers.pop(server_id, None)\n        else:\n            self.connected_servers.clear()\n\n        # Rebuild available tools without the disconnected server's tools\n        base_tools = [\n            tool\n            for tool in self.available_tools.tools\n            if not isinstance(tool, MCPClientTool)\n        ]\n        self.available_tools = ToolCollection(*base_tools)\n        self.available_tools.add_tools(*self.mcp_clients.tools)\n\n    async def delete_sandbox(self, sandbox_id: str) -> None:\n        \"\"\"Delete a sandbox by ID.\"\"\"\n        try:\n            await delete_sandbox(sandbox_id)\n            logger.info(f\"Sandbox {sandbox_id} deleted successfully\")\n            if sandbox_id in self.sandbox_link:\n                del self.sandbox_link[sandbox_id]\n        except Exception as e:\n            logger.error(f\"Error deleting sandbox {sandbox_id}: {e}\")\n            raise e\n\n    async def cleanup(self):\n        \"\"\"Clean up Manus agent resources.\"\"\"\n        if self.browser_context_helper:\n            await self.browser_context_helper.cleanup_browser()\n        # Disconnect from all MCP servers only if we were initialized\n        if self._initialized:\n            await self.disconnect_mcp_server()\n            await self.delete_sandbox(self.sandbox.id if self.sandbox else \"unknown\")\n            self._initialized = False\n\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next actions with appropriate context.\"\"\"\n        if not self._initialized:\n            await self.initialize_mcp_servers()\n            self._initialized = True\n\n        original_prompt = self.next_step_prompt\n        recent_messages = self.memory.messages[-3:] if self.memory.messages else []\n        browser_in_use = any(\n            tc.function.name == SandboxBrowserTool().name\n            for msg in recent_messages\n            if msg.tool_calls\n            for tc in msg.tool_calls\n        )\n\n        if browser_in_use:\n            self.next_step_prompt = (\n                await self.browser_context_helper.format_next_step_prompt()\n            )\n\n        result = await super().think()\n\n        # Restore original prompt\n        self.next_step_prompt = original_prompt\n\n        return result\n"
  },
  {
    "path": "app/agent/swe.py",
    "content": "from typing import List\n\nfrom pydantic import Field\n\nfrom app.agent.toolcall import ToolCallAgent\nfrom app.prompt.swe import SYSTEM_PROMPT\nfrom app.tool import Bash, StrReplaceEditor, Terminate, ToolCollection\n\n\nclass SWEAgent(ToolCallAgent):\n    \"\"\"An agent that implements the SWEAgent paradigm for executing code and natural conversations.\"\"\"\n\n    name: str = \"swe\"\n    description: str = \"an autonomous AI programmer that interacts directly with the computer to solve tasks.\"\n\n    system_prompt: str = SYSTEM_PROMPT\n    next_step_prompt: str = \"\"\n\n    available_tools: ToolCollection = ToolCollection(\n        Bash(), StrReplaceEditor(), Terminate()\n    )\n    special_tool_names: List[str] = Field(default_factory=lambda: [Terminate().name])\n\n    max_steps: int = 20\n"
  },
  {
    "path": "app/agent/toolcall.py",
    "content": "import asyncio\nimport json\nfrom typing import Any, List, Optional, Union\n\nfrom pydantic import Field\n\nfrom app.agent.react import ReActAgent\nfrom app.exceptions import TokenLimitExceeded\nfrom app.logger import logger\nfrom app.prompt.toolcall import NEXT_STEP_PROMPT, SYSTEM_PROMPT\nfrom app.schema import TOOL_CHOICE_TYPE, AgentState, Message, ToolCall, ToolChoice\nfrom app.tool import CreateChatCompletion, Terminate, ToolCollection\n\n\nTOOL_CALL_REQUIRED = \"Tool calls required but none provided\"\n\n\nclass ToolCallAgent(ReActAgent):\n    \"\"\"Base agent class for handling tool/function calls with enhanced abstraction\"\"\"\n\n    name: str = \"toolcall\"\n    description: str = \"an agent that can execute tool calls.\"\n\n    system_prompt: str = SYSTEM_PROMPT\n    next_step_prompt: str = NEXT_STEP_PROMPT\n\n    available_tools: ToolCollection = ToolCollection(\n        CreateChatCompletion(), Terminate()\n    )\n    tool_choices: TOOL_CHOICE_TYPE = ToolChoice.AUTO  # type: ignore\n    special_tool_names: List[str] = Field(default_factory=lambda: [Terminate().name])\n\n    tool_calls: List[ToolCall] = Field(default_factory=list)\n    _current_base64_image: Optional[str] = None\n\n    max_steps: int = 30\n    max_observe: Optional[Union[int, bool]] = None\n\n    async def think(self) -> bool:\n        \"\"\"Process current state and decide next actions using tools\"\"\"\n        if self.next_step_prompt:\n            user_msg = Message.user_message(self.next_step_prompt)\n            self.messages += [user_msg]\n\n        try:\n            # Get response with tool options\n            response = await self.llm.ask_tool(\n                messages=self.messages,\n                system_msgs=(\n                    [Message.system_message(self.system_prompt)]\n                    if self.system_prompt\n                    else None\n                ),\n                tools=self.available_tools.to_params(),\n                tool_choice=self.tool_choices,\n            )\n        except ValueError:\n            raise\n        except Exception as e:\n            # Check if this is a RetryError containing TokenLimitExceeded\n            if hasattr(e, \"__cause__\") and isinstance(e.__cause__, TokenLimitExceeded):\n                token_limit_error = e.__cause__\n                logger.error(\n                    f\"🚨 Token limit error (from RetryError): {token_limit_error}\"\n                )\n                self.memory.add_message(\n                    Message.assistant_message(\n                        f\"Maximum token limit reached, cannot continue execution: {str(token_limit_error)}\"\n                    )\n                )\n                self.state = AgentState.FINISHED\n                return False\n            raise\n\n        self.tool_calls = tool_calls = (\n            response.tool_calls if response and response.tool_calls else []\n        )\n        content = response.content if response and response.content else \"\"\n\n        # Log response info\n        logger.info(f\"✨ {self.name}'s thoughts: {content}\")\n        logger.info(\n            f\"🛠️ {self.name} selected {len(tool_calls) if tool_calls else 0} tools to use\"\n        )\n        if tool_calls:\n            logger.info(\n                f\"🧰 Tools being prepared: {[call.function.name for call in tool_calls]}\"\n            )\n            logger.info(f\"🔧 Tool arguments: {tool_calls[0].function.arguments}\")\n\n        try:\n            if response is None:\n                raise RuntimeError(\"No response received from the LLM\")\n\n            # Handle different tool_choices modes\n            if self.tool_choices == ToolChoice.NONE:\n                if tool_calls:\n                    logger.warning(\n                        f\"🤔 Hmm, {self.name} tried to use tools when they weren't available!\"\n                    )\n                if content:\n                    self.memory.add_message(Message.assistant_message(content))\n                    return True\n                return False\n\n            # Create and add assistant message\n            assistant_msg = (\n                Message.from_tool_calls(content=content, tool_calls=self.tool_calls)\n                if self.tool_calls\n                else Message.assistant_message(content)\n            )\n            self.memory.add_message(assistant_msg)\n\n            if self.tool_choices == ToolChoice.REQUIRED and not self.tool_calls:\n                return True  # Will be handled in act()\n\n            # For 'auto' mode, continue with content if no commands but content exists\n            if self.tool_choices == ToolChoice.AUTO and not self.tool_calls:\n                return bool(content)\n\n            return bool(self.tool_calls)\n        except Exception as e:\n            logger.error(f\"🚨 Oops! The {self.name}'s thinking process hit a snag: {e}\")\n            self.memory.add_message(\n                Message.assistant_message(\n                    f\"Error encountered while processing: {str(e)}\"\n                )\n            )\n            return False\n\n    async def act(self) -> str:\n        \"\"\"Execute tool calls and handle their results\"\"\"\n        if not self.tool_calls:\n            if self.tool_choices == ToolChoice.REQUIRED:\n                raise ValueError(TOOL_CALL_REQUIRED)\n\n            # Return last message content if no tool calls\n            return self.messages[-1].content or \"No content or commands to execute\"\n\n        results = []\n        for command in self.tool_calls:\n            # Reset base64_image for each tool call\n            self._current_base64_image = None\n\n            result = await self.execute_tool(command)\n\n            if self.max_observe:\n                result = result[: self.max_observe]\n\n            logger.info(\n                f\"🎯 Tool '{command.function.name}' completed its mission! Result: {result}\"\n            )\n\n            # Add tool response to memory\n            tool_msg = Message.tool_message(\n                content=result,\n                tool_call_id=command.id,\n                name=command.function.name,\n                base64_image=self._current_base64_image,\n            )\n            self.memory.add_message(tool_msg)\n            results.append(result)\n\n        return \"\\n\\n\".join(results)\n\n    async def execute_tool(self, command: ToolCall) -> str:\n        \"\"\"Execute a single tool call with robust error handling\"\"\"\n        if not command or not command.function or not command.function.name:\n            return \"Error: Invalid command format\"\n\n        name = command.function.name\n        if name not in self.available_tools.tool_map:\n            return f\"Error: Unknown tool '{name}'\"\n\n        try:\n            # Parse arguments\n            args = json.loads(command.function.arguments or \"{}\")\n\n            # Execute the tool\n            logger.info(f\"🔧 Activating tool: '{name}'...\")\n            result = await self.available_tools.execute(name=name, tool_input=args)\n\n            # Handle special tools\n            await self._handle_special_tool(name=name, result=result)\n\n            # Check if result is a ToolResult with base64_image\n            if hasattr(result, \"base64_image\") and result.base64_image:\n                # Store the base64_image for later use in tool_message\n                self._current_base64_image = result.base64_image\n\n            # Format result for display (standard case)\n            observation = (\n                f\"Observed output of cmd `{name}` executed:\\n{str(result)}\"\n                if result\n                else f\"Cmd `{name}` completed with no output\"\n            )\n\n            return observation\n        except json.JSONDecodeError:\n            error_msg = f\"Error parsing arguments for {name}: Invalid JSON format\"\n            logger.error(\n                f\"📝 Oops! The arguments for '{name}' don't make sense - invalid JSON, arguments:{command.function.arguments}\"\n            )\n            return f\"Error: {error_msg}\"\n        except Exception as e:\n            error_msg = f\"⚠️ Tool '{name}' encountered a problem: {str(e)}\"\n            logger.exception(error_msg)\n            return f\"Error: {error_msg}\"\n\n    async def _handle_special_tool(self, name: str, result: Any, **kwargs):\n        \"\"\"Handle special tool execution and state changes\"\"\"\n        if not self._is_special_tool(name):\n            return\n\n        if self._should_finish_execution(name=name, result=result, **kwargs):\n            # Set agent state to finished\n            logger.info(f\"🏁 Special tool '{name}' has completed the task!\")\n            self.state = AgentState.FINISHED\n\n    @staticmethod\n    def _should_finish_execution(**kwargs) -> bool:\n        \"\"\"Determine if tool execution should finish the agent\"\"\"\n        return True\n\n    def _is_special_tool(self, name: str) -> bool:\n        \"\"\"Check if tool name is in special tools list\"\"\"\n        return name.lower() in [n.lower() for n in self.special_tool_names]\n\n    async def cleanup(self):\n        \"\"\"Clean up resources used by the agent's tools.\"\"\"\n        logger.info(f\"🧹 Cleaning up resources for agent '{self.name}'...\")\n        for tool_name, tool_instance in self.available_tools.tool_map.items():\n            if hasattr(tool_instance, \"cleanup\") and asyncio.iscoroutinefunction(\n                tool_instance.cleanup\n            ):\n                try:\n                    logger.debug(f\"🧼 Cleaning up tool: {tool_name}\")\n                    await tool_instance.cleanup()\n                except Exception as e:\n                    logger.error(\n                        f\"🚨 Error cleaning up tool '{tool_name}': {e}\", exc_info=True\n                    )\n        logger.info(f\"✨ Cleanup complete for agent '{self.name}'.\")\n\n    async def run(self, request: Optional[str] = None) -> str:\n        \"\"\"Run the agent with cleanup when done.\"\"\"\n        try:\n            return await super().run(request)\n        finally:\n            await self.cleanup()\n"
  },
  {
    "path": "app/bedrock.py",
    "content": "import json\nimport sys\nimport time\nimport uuid\nfrom datetime import datetime\nfrom typing import Dict, List, Literal, Optional\n\nimport boto3\n\n\n# Global variables to track the current tool use ID across function calls\n# Tmp solution\nCURRENT_TOOLUSE_ID = None\n\n\n# Class to handle OpenAI-style response formatting\nclass OpenAIResponse:\n    def __init__(self, data):\n        # Recursively convert nested dicts and lists to OpenAIResponse objects\n        for key, value in data.items():\n            if isinstance(value, dict):\n                value = OpenAIResponse(value)\n            elif isinstance(value, list):\n                value = [\n                    OpenAIResponse(item) if isinstance(item, dict) else item\n                    for item in value\n                ]\n            setattr(self, key, value)\n\n    def model_dump(self, *args, **kwargs):\n        # Convert object to dict and add timestamp\n        data = self.__dict__\n        data[\"created_at\"] = datetime.now().isoformat()\n        return data\n\n\n# Main client class for interacting with Amazon Bedrock\nclass BedrockClient:\n    def __init__(self):\n        # Initialize Bedrock client, you need to configure AWS env first\n        try:\n            self.client = boto3.client(\"bedrock-runtime\")\n            self.chat = Chat(self.client)\n        except Exception as e:\n            print(f\"Error initializing Bedrock client: {e}\")\n            sys.exit(1)\n\n\n# Chat interface class\nclass Chat:\n    def __init__(self, client):\n        self.completions = ChatCompletions(client)\n\n\n# Core class handling chat completions functionality\nclass ChatCompletions:\n    def __init__(self, client):\n        self.client = client\n\n    def _convert_openai_tools_to_bedrock_format(self, tools):\n        # Convert OpenAI function calling format to Bedrock tool format\n        bedrock_tools = []\n        for tool in tools:\n            if tool.get(\"type\") == \"function\":\n                function = tool.get(\"function\", {})\n                bedrock_tool = {\n                    \"toolSpec\": {\n                        \"name\": function.get(\"name\", \"\"),\n                        \"description\": function.get(\"description\", \"\"),\n                        \"inputSchema\": {\n                            \"json\": {\n                                \"type\": \"object\",\n                                \"properties\": function.get(\"parameters\", {}).get(\n                                    \"properties\", {}\n                                ),\n                                \"required\": function.get(\"parameters\", {}).get(\n                                    \"required\", []\n                                ),\n                            }\n                        },\n                    }\n                }\n                bedrock_tools.append(bedrock_tool)\n        return bedrock_tools\n\n    def _convert_openai_messages_to_bedrock_format(self, messages):\n        # Convert OpenAI message format to Bedrock message format\n        bedrock_messages = []\n        system_prompt = []\n        for message in messages:\n            if message.get(\"role\") == \"system\":\n                system_prompt = [{\"text\": message.get(\"content\")}]\n            elif message.get(\"role\") == \"user\":\n                bedrock_message = {\n                    \"role\": message.get(\"role\", \"user\"),\n                    \"content\": [{\"text\": message.get(\"content\")}],\n                }\n                bedrock_messages.append(bedrock_message)\n            elif message.get(\"role\") == \"assistant\":\n                bedrock_message = {\n                    \"role\": \"assistant\",\n                    \"content\": [{\"text\": message.get(\"content\")}],\n                }\n                openai_tool_calls = message.get(\"tool_calls\", [])\n                if openai_tool_calls:\n                    bedrock_tool_use = {\n                        \"toolUseId\": openai_tool_calls[0][\"id\"],\n                        \"name\": openai_tool_calls[0][\"function\"][\"name\"],\n                        \"input\": json.loads(\n                            openai_tool_calls[0][\"function\"][\"arguments\"]\n                        ),\n                    }\n                    bedrock_message[\"content\"].append({\"toolUse\": bedrock_tool_use})\n                    global CURRENT_TOOLUSE_ID\n                    CURRENT_TOOLUSE_ID = openai_tool_calls[0][\"id\"]\n                bedrock_messages.append(bedrock_message)\n            elif message.get(\"role\") == \"tool\":\n                bedrock_message = {\n                    \"role\": \"user\",\n                    \"content\": [\n                        {\n                            \"toolResult\": {\n                                \"toolUseId\": CURRENT_TOOLUSE_ID,\n                                \"content\": [{\"text\": message.get(\"content\")}],\n                            }\n                        }\n                    ],\n                }\n                bedrock_messages.append(bedrock_message)\n            else:\n                raise ValueError(f\"Invalid role: {message.get('role')}\")\n        return system_prompt, bedrock_messages\n\n    def _convert_bedrock_response_to_openai_format(self, bedrock_response):\n        # Convert Bedrock response format to OpenAI format\n        content = \"\"\n        if bedrock_response.get(\"output\", {}).get(\"message\", {}).get(\"content\"):\n            content_array = bedrock_response[\"output\"][\"message\"][\"content\"]\n            content = \"\".join(item.get(\"text\", \"\") for item in content_array)\n        if content == \"\":\n            content = \".\"\n\n        # Handle tool calls in response\n        openai_tool_calls = []\n        if bedrock_response.get(\"output\", {}).get(\"message\", {}).get(\"content\"):\n            for content_item in bedrock_response[\"output\"][\"message\"][\"content\"]:\n                if content_item.get(\"toolUse\"):\n                    bedrock_tool_use = content_item[\"toolUse\"]\n                    global CURRENT_TOOLUSE_ID\n                    CURRENT_TOOLUSE_ID = bedrock_tool_use[\"toolUseId\"]\n                    openai_tool_call = {\n                        \"id\": CURRENT_TOOLUSE_ID,\n                        \"type\": \"function\",\n                        \"function\": {\n                            \"name\": bedrock_tool_use[\"name\"],\n                            \"arguments\": json.dumps(bedrock_tool_use[\"input\"]),\n                        },\n                    }\n                    openai_tool_calls.append(openai_tool_call)\n\n        # Construct final OpenAI format response\n        openai_format = {\n            \"id\": f\"chatcmpl-{uuid.uuid4()}\",\n            \"created\": int(time.time()),\n            \"object\": \"chat.completion\",\n            \"system_fingerprint\": None,\n            \"choices\": [\n                {\n                    \"finish_reason\": bedrock_response.get(\"stopReason\", \"end_turn\"),\n                    \"index\": 0,\n                    \"message\": {\n                        \"content\": content,\n                        \"role\": bedrock_response.get(\"output\", {})\n                        .get(\"message\", {})\n                        .get(\"role\", \"assistant\"),\n                        \"tool_calls\": openai_tool_calls\n                        if openai_tool_calls != []\n                        else None,\n                        \"function_call\": None,\n                    },\n                }\n            ],\n            \"usage\": {\n                \"completion_tokens\": bedrock_response.get(\"usage\", {}).get(\n                    \"outputTokens\", 0\n                ),\n                \"prompt_tokens\": bedrock_response.get(\"usage\", {}).get(\n                    \"inputTokens\", 0\n                ),\n                \"total_tokens\": bedrock_response.get(\"usage\", {}).get(\"totalTokens\", 0),\n            },\n        }\n        return OpenAIResponse(openai_format)\n\n    async def _invoke_bedrock(\n        self,\n        model: str,\n        messages: List[Dict[str, str]],\n        max_tokens: int,\n        temperature: float,\n        tools: Optional[List[dict]] = None,\n        tool_choice: Literal[\"none\", \"auto\", \"required\"] = \"auto\",\n        **kwargs,\n    ) -> OpenAIResponse:\n        # Non-streaming invocation of Bedrock model\n        (\n            system_prompt,\n            bedrock_messages,\n        ) = self._convert_openai_messages_to_bedrock_format(messages)\n        response = self.client.converse(\n            modelId=model,\n            system=system_prompt,\n            messages=bedrock_messages,\n            inferenceConfig={\"temperature\": temperature, \"maxTokens\": max_tokens},\n            toolConfig={\"tools\": tools} if tools else None,\n        )\n        openai_response = self._convert_bedrock_response_to_openai_format(response)\n        return openai_response\n\n    async def _invoke_bedrock_stream(\n        self,\n        model: str,\n        messages: List[Dict[str, str]],\n        max_tokens: int,\n        temperature: float,\n        tools: Optional[List[dict]] = None,\n        tool_choice: Literal[\"none\", \"auto\", \"required\"] = \"auto\",\n        **kwargs,\n    ) -> OpenAIResponse:\n        # Streaming invocation of Bedrock model\n        (\n            system_prompt,\n            bedrock_messages,\n        ) = self._convert_openai_messages_to_bedrock_format(messages)\n        response = self.client.converse_stream(\n            modelId=model,\n            system=system_prompt,\n            messages=bedrock_messages,\n            inferenceConfig={\"temperature\": temperature, \"maxTokens\": max_tokens},\n            toolConfig={\"tools\": tools} if tools else None,\n        )\n\n        # Initialize response structure\n        bedrock_response = {\n            \"output\": {\"message\": {\"role\": \"\", \"content\": []}},\n            \"stopReason\": \"\",\n            \"usage\": {},\n            \"metrics\": {},\n        }\n        bedrock_response_text = \"\"\n        bedrock_response_tool_input = \"\"\n\n        # Process streaming response\n        stream = response.get(\"stream\")\n        if stream:\n            for event in stream:\n                if event.get(\"messageStart\", {}).get(\"role\"):\n                    bedrock_response[\"output\"][\"message\"][\"role\"] = event[\n                        \"messageStart\"\n                    ][\"role\"]\n                if event.get(\"contentBlockDelta\", {}).get(\"delta\", {}).get(\"text\"):\n                    bedrock_response_text += event[\"contentBlockDelta\"][\"delta\"][\"text\"]\n                    print(\n                        event[\"contentBlockDelta\"][\"delta\"][\"text\"], end=\"\", flush=True\n                    )\n                if event.get(\"contentBlockStop\", {}).get(\"contentBlockIndex\") == 0:\n                    bedrock_response[\"output\"][\"message\"][\"content\"].append(\n                        {\"text\": bedrock_response_text}\n                    )\n                if event.get(\"contentBlockStart\", {}).get(\"start\", {}).get(\"toolUse\"):\n                    bedrock_tool_use = event[\"contentBlockStart\"][\"start\"][\"toolUse\"]\n                    tool_use = {\n                        \"toolUseId\": bedrock_tool_use[\"toolUseId\"],\n                        \"name\": bedrock_tool_use[\"name\"],\n                    }\n                    bedrock_response[\"output\"][\"message\"][\"content\"].append(\n                        {\"toolUse\": tool_use}\n                    )\n                    global CURRENT_TOOLUSE_ID\n                    CURRENT_TOOLUSE_ID = bedrock_tool_use[\"toolUseId\"]\n                if event.get(\"contentBlockDelta\", {}).get(\"delta\", {}).get(\"toolUse\"):\n                    bedrock_response_tool_input += event[\"contentBlockDelta\"][\"delta\"][\n                        \"toolUse\"\n                    ][\"input\"]\n                    print(\n                        event[\"contentBlockDelta\"][\"delta\"][\"toolUse\"][\"input\"],\n                        end=\"\",\n                        flush=True,\n                    )\n                if event.get(\"contentBlockStop\", {}).get(\"contentBlockIndex\") == 1:\n                    bedrock_response[\"output\"][\"message\"][\"content\"][1][\"toolUse\"][\n                        \"input\"\n                    ] = json.loads(bedrock_response_tool_input)\n        print()\n        openai_response = self._convert_bedrock_response_to_openai_format(\n            bedrock_response\n        )\n        return openai_response\n\n    def create(\n        self,\n        model: str,\n        messages: List[Dict[str, str]],\n        max_tokens: int,\n        temperature: float,\n        stream: Optional[bool] = True,\n        tools: Optional[List[dict]] = None,\n        tool_choice: Literal[\"none\", \"auto\", \"required\"] = \"auto\",\n        **kwargs,\n    ) -> OpenAIResponse:\n        # Main entry point for chat completion\n        bedrock_tools = []\n        if tools is not None:\n            bedrock_tools = self._convert_openai_tools_to_bedrock_format(tools)\n        if stream:\n            return self._invoke_bedrock_stream(\n                model,\n                messages,\n                max_tokens,\n                temperature,\n                bedrock_tools,\n                tool_choice,\n                **kwargs,\n            )\n        else:\n            return self._invoke_bedrock(\n                model,\n                messages,\n                max_tokens,\n                temperature,\n                bedrock_tools,\n                tool_choice,\n                **kwargs,\n            )\n"
  },
  {
    "path": "app/config.py",
    "content": "import json\nimport threading\nimport tomllib\nfrom pathlib import Path\nfrom typing import Dict, List, Optional\n\nfrom pydantic import BaseModel, Field\n\n\ndef get_project_root() -> Path:\n    \"\"\"Get the project root directory\"\"\"\n    return Path(__file__).resolve().parent.parent\n\n\nPROJECT_ROOT = get_project_root()\nWORKSPACE_ROOT = PROJECT_ROOT / \"workspace\"\n\n\nclass LLMSettings(BaseModel):\n    model: str = Field(..., description=\"Model name\")\n    base_url: str = Field(..., description=\"API base URL\")\n    api_key: str = Field(..., description=\"API key\")\n    max_tokens: int = Field(4096, description=\"Maximum number of tokens per request\")\n    max_input_tokens: Optional[int] = Field(\n        None,\n        description=\"Maximum input tokens to use across all requests (None for unlimited)\",\n    )\n    temperature: float = Field(1.0, description=\"Sampling temperature\")\n    api_type: str = Field(..., description=\"Azure, Openai, or Ollama\")\n    api_version: str = Field(..., description=\"Azure Openai version if AzureOpenai\")\n\n\nclass ProxySettings(BaseModel):\n    server: str = Field(None, description=\"Proxy server address\")\n    username: Optional[str] = Field(None, description=\"Proxy username\")\n    password: Optional[str] = Field(None, description=\"Proxy password\")\n\n\nclass SearchSettings(BaseModel):\n    engine: str = Field(default=\"Google\", description=\"Search engine the llm to use\")\n    fallback_engines: List[str] = Field(\n        default_factory=lambda: [\"DuckDuckGo\", \"Baidu\", \"Bing\"],\n        description=\"Fallback search engines to try if the primary engine fails\",\n    )\n    retry_delay: int = Field(\n        default=60,\n        description=\"Seconds to wait before retrying all engines again after they all fail\",\n    )\n    max_retries: int = Field(\n        default=3,\n        description=\"Maximum number of times to retry all engines when all fail\",\n    )\n    lang: str = Field(\n        default=\"en\",\n        description=\"Language code for search results (e.g., en, zh, fr)\",\n    )\n    country: str = Field(\n        default=\"us\",\n        description=\"Country code for search results (e.g., us, cn, uk)\",\n    )\n\n\nclass RunflowSettings(BaseModel):\n    use_data_analysis_agent: bool = Field(\n        default=False, description=\"Enable data analysis agent in run flow\"\n    )\n\n\nclass BrowserSettings(BaseModel):\n    headless: bool = Field(False, description=\"Whether to run browser in headless mode\")\n    disable_security: bool = Field(\n        True, description=\"Disable browser security features\"\n    )\n    extra_chromium_args: List[str] = Field(\n        default_factory=list, description=\"Extra arguments to pass to the browser\"\n    )\n    chrome_instance_path: Optional[str] = Field(\n        None, description=\"Path to a Chrome instance to use\"\n    )\n    wss_url: Optional[str] = Field(\n        None, description=\"Connect to a browser instance via WebSocket\"\n    )\n    cdp_url: Optional[str] = Field(\n        None, description=\"Connect to a browser instance via CDP\"\n    )\n    proxy: Optional[ProxySettings] = Field(\n        None, description=\"Proxy settings for the browser\"\n    )\n    max_content_length: int = Field(\n        2000, description=\"Maximum length for content retrieval operations\"\n    )\n\n\nclass SandboxSettings(BaseModel):\n    \"\"\"Configuration for the execution sandbox\"\"\"\n\n    use_sandbox: bool = Field(False, description=\"Whether to use the sandbox\")\n    image: str = Field(\"python:3.12-slim\", description=\"Base image\")\n    work_dir: str = Field(\"/workspace\", description=\"Container working directory\")\n    memory_limit: str = Field(\"512m\", description=\"Memory limit\")\n    cpu_limit: float = Field(1.0, description=\"CPU limit\")\n    timeout: int = Field(300, description=\"Default command timeout (seconds)\")\n    network_enabled: bool = Field(\n        False, description=\"Whether network access is allowed\"\n    )\n\n\nclass DaytonaSettings(BaseModel):\n    daytona_api_key: str\n    daytona_server_url: Optional[str] = Field(\n        \"https://app.daytona.io/api\", description=\"\"\n    )\n    daytona_target: Optional[str] = Field(\"us\", description=\"enum ['eu', 'us']\")\n    sandbox_image_name: Optional[str] = Field(\"whitezxj/sandbox:0.1.0\", description=\"\")\n    sandbox_entrypoint: Optional[str] = Field(\n        \"/usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf\",\n        description=\"\",\n    )\n    # sandbox_id: Optional[str] = Field(\n    #     None, description=\"ID of the daytona sandbox to use, if any\"\n    # )\n    VNC_password: Optional[str] = Field(\n        \"123456\", description=\"VNC password for the vnc service in sandbox\"\n    )\n\n\nclass MCPServerConfig(BaseModel):\n    \"\"\"Configuration for a single MCP server\"\"\"\n\n    type: str = Field(..., description=\"Server connection type (sse or stdio)\")\n    url: Optional[str] = Field(None, description=\"Server URL for SSE connections\")\n    command: Optional[str] = Field(None, description=\"Command for stdio connections\")\n    args: List[str] = Field(\n        default_factory=list, description=\"Arguments for stdio command\"\n    )\n\n\nclass MCPSettings(BaseModel):\n    \"\"\"Configuration for MCP (Model Context Protocol)\"\"\"\n\n    server_reference: str = Field(\n        \"app.mcp.server\", description=\"Module reference for the MCP server\"\n    )\n    servers: Dict[str, MCPServerConfig] = Field(\n        default_factory=dict, description=\"MCP server configurations\"\n    )\n\n    @classmethod\n    def load_server_config(cls) -> Dict[str, MCPServerConfig]:\n        \"\"\"Load MCP server configuration from JSON file\"\"\"\n        config_path = PROJECT_ROOT / \"config\" / \"mcp.json\"\n\n        try:\n            config_file = config_path if config_path.exists() else None\n            if not config_file:\n                return {}\n\n            with config_file.open() as f:\n                data = json.load(f)\n                servers = {}\n\n                for server_id, server_config in data.get(\"mcpServers\", {}).items():\n                    servers[server_id] = MCPServerConfig(\n                        type=server_config[\"type\"],\n                        url=server_config.get(\"url\"),\n                        command=server_config.get(\"command\"),\n                        args=server_config.get(\"args\", []),\n                    )\n                return servers\n        except Exception as e:\n            raise ValueError(f\"Failed to load MCP server config: {e}\")\n\n\nclass AppConfig(BaseModel):\n    llm: Dict[str, LLMSettings]\n    sandbox: Optional[SandboxSettings] = Field(\n        None, description=\"Sandbox configuration\"\n    )\n    browser_config: Optional[BrowserSettings] = Field(\n        None, description=\"Browser configuration\"\n    )\n    search_config: Optional[SearchSettings] = Field(\n        None, description=\"Search configuration\"\n    )\n    mcp_config: Optional[MCPSettings] = Field(None, description=\"MCP configuration\")\n    run_flow_config: Optional[RunflowSettings] = Field(\n        None, description=\"Run flow configuration\"\n    )\n    daytona_config: Optional[DaytonaSettings] = Field(\n        None, description=\"Daytona configuration\"\n    )\n\n    class Config:\n        arbitrary_types_allowed = True\n\n\nclass Config:\n    _instance = None\n    _lock = threading.Lock()\n    _initialized = False\n\n    def __new__(cls):\n        if cls._instance is None:\n            with cls._lock:\n                if cls._instance is None:\n                    cls._instance = super().__new__(cls)\n        return cls._instance\n\n    def __init__(self):\n        if not self._initialized:\n            with self._lock:\n                if not self._initialized:\n                    self._config = None\n                    self._load_initial_config()\n                    self._initialized = True\n\n    @staticmethod\n    def _get_config_path() -> Path:\n        root = PROJECT_ROOT\n        config_path = root / \"config\" / \"config.toml\"\n        if config_path.exists():\n            return config_path\n        example_path = root / \"config\" / \"config.example.toml\"\n        if example_path.exists():\n            return example_path\n        raise FileNotFoundError(\"No configuration file found in config directory\")\n\n    def _load_config(self) -> dict:\n        config_path = self._get_config_path()\n        with config_path.open(\"rb\") as f:\n            return tomllib.load(f)\n\n    def _load_initial_config(self):\n        raw_config = self._load_config()\n        base_llm = raw_config.get(\"llm\", {})\n        llm_overrides = {\n            k: v for k, v in raw_config.get(\"llm\", {}).items() if isinstance(v, dict)\n        }\n\n        default_settings = {\n            \"model\": base_llm.get(\"model\"),\n            \"base_url\": base_llm.get(\"base_url\"),\n            \"api_key\": base_llm.get(\"api_key\"),\n            \"max_tokens\": base_llm.get(\"max_tokens\", 4096),\n            \"max_input_tokens\": base_llm.get(\"max_input_tokens\"),\n            \"temperature\": base_llm.get(\"temperature\", 1.0),\n            \"api_type\": base_llm.get(\"api_type\", \"\"),\n            \"api_version\": base_llm.get(\"api_version\", \"\"),\n        }\n\n        # handle browser config.\n        browser_config = raw_config.get(\"browser\", {})\n        browser_settings = None\n\n        if browser_config:\n            # handle proxy settings.\n            proxy_config = browser_config.get(\"proxy\", {})\n            proxy_settings = None\n\n            if proxy_config and proxy_config.get(\"server\"):\n                proxy_settings = ProxySettings(\n                    **{\n                        k: v\n                        for k, v in proxy_config.items()\n                        if k in [\"server\", \"username\", \"password\"] and v\n                    }\n                )\n\n            # filter valid browser config parameters.\n            valid_browser_params = {\n                k: v\n                for k, v in browser_config.items()\n                if k in BrowserSettings.__annotations__ and v is not None\n            }\n\n            # if there is proxy settings, add it to the parameters.\n            if proxy_settings:\n                valid_browser_params[\"proxy\"] = proxy_settings\n\n            # only create BrowserSettings when there are valid parameters.\n            if valid_browser_params:\n                browser_settings = BrowserSettings(**valid_browser_params)\n\n        search_config = raw_config.get(\"search\", {})\n        search_settings = None\n        if search_config:\n            search_settings = SearchSettings(**search_config)\n        sandbox_config = raw_config.get(\"sandbox\", {})\n        if sandbox_config:\n            sandbox_settings = SandboxSettings(**sandbox_config)\n        else:\n            sandbox_settings = SandboxSettings()\n        daytona_config = raw_config.get(\"daytona\", {})\n        if daytona_config:\n            daytona_settings = DaytonaSettings(**daytona_config)\n        else:\n            daytona_settings = DaytonaSettings()\n\n        mcp_config = raw_config.get(\"mcp\", {})\n        mcp_settings = None\n        if mcp_config:\n            # Load server configurations from JSON\n            mcp_config[\"servers\"] = MCPSettings.load_server_config()\n            mcp_settings = MCPSettings(**mcp_config)\n        else:\n            mcp_settings = MCPSettings(servers=MCPSettings.load_server_config())\n\n        run_flow_config = raw_config.get(\"runflow\")\n        if run_flow_config:\n            run_flow_settings = RunflowSettings(**run_flow_config)\n        else:\n            run_flow_settings = RunflowSettings()\n        config_dict = {\n            \"llm\": {\n                \"default\": default_settings,\n                **{\n                    name: {**default_settings, **override_config}\n                    for name, override_config in llm_overrides.items()\n                },\n            },\n            \"sandbox\": sandbox_settings,\n            \"browser_config\": browser_settings,\n            \"search_config\": search_settings,\n            \"mcp_config\": mcp_settings,\n            \"run_flow_config\": run_flow_settings,\n            \"daytona_config\": daytona_settings,\n        }\n\n        self._config = AppConfig(**config_dict)\n\n    @property\n    def llm(self) -> Dict[str, LLMSettings]:\n        return self._config.llm\n\n    @property\n    def sandbox(self) -> SandboxSettings:\n        return self._config.sandbox\n\n    @property\n    def daytona(self) -> DaytonaSettings:\n        return self._config.daytona_config\n\n    @property\n    def browser_config(self) -> Optional[BrowserSettings]:\n        return self._config.browser_config\n\n    @property\n    def search_config(self) -> Optional[SearchSettings]:\n        return self._config.search_config\n\n    @property\n    def mcp_config(self) -> MCPSettings:\n        \"\"\"Get the MCP configuration\"\"\"\n        return self._config.mcp_config\n\n    @property\n    def run_flow_config(self) -> RunflowSettings:\n        \"\"\"Get the Run Flow configuration\"\"\"\n        return self._config.run_flow_config\n\n    @property\n    def workspace_root(self) -> Path:\n        \"\"\"Get the workspace root directory\"\"\"\n        return WORKSPACE_ROOT\n\n    @property\n    def root_path(self) -> Path:\n        \"\"\"Get the root path of the application\"\"\"\n        return PROJECT_ROOT\n\n\nconfig = Config()\n"
  },
  {
    "path": "app/daytona/README.md",
    "content": "# Agent with Daytona sandbox\n\n\n\n\n## Prerequisites\n- conda activate 'Your OpenManus python env'\n- pip install daytona==0.21.8 structlog==25.4.0\n\n\n\n## Setup & Running\n\n1. daytona config :\n   ```bash\n   cd OpenManus\n   cp config/config.example-daytona.toml config/config.toml\n   ```\n2. get daytona apikey :\n   goto https://app.daytona.io/dashboard/keys and create your apikey\n\n3. set your apikey in config.toml\n   ```toml\n   # daytona config\n   [daytona]\n   daytona_api_key = \"\"\n   #daytona_server_url = \"https://app.daytona.io/api\"\n   #daytona_target = \"us\"                                   #Daytona is currently available in the following regions:United States (us)、Europe (eu)\n   #sandbox_image_name = \"whitezxj/sandbox:0.1.0\"                #If you don't use this default image,sandbox tools may be useless\n   #sandbox_entrypoint = \"/usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf\"   #If you change this entrypoint,server in sandbox may be useless\n   #VNC_password =                                          #The password you set to log in sandbox by VNC,it will be 123456 if you don't set\n   ```\n2. Run :\n\n   ```bash\n   cd OpenManus\n   python sandbox_main.py\n   ```\n\n3. Send tasks to Agent\n   You can sent tasks to Agent by terminate,agent will use sandbox tools to handle your tasks.\n\n4. See results\n   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.\n   If agent use sb_shell tool, you can see the results by terminate of sandbox in https://app.daytona.io/dashboard/sandboxes.\n   Agent can use sb_files tool to operate files to sandbox.\n\n\n## Example\n\n 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\"\n\n 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).\n\n## Learn More\n\n- [Daytona Documentation](https://www.daytona.io/docs/)\n"
  },
  {
    "path": "app/daytona/sandbox.py",
    "content": "import time\n\nfrom daytona import (\n    CreateSandboxFromImageParams,\n    Daytona,\n    DaytonaConfig,\n    Resources,\n    Sandbox,\n    SandboxState,\n    SessionExecuteRequest,\n)\n\nfrom app.config import config\nfrom app.utils.logger import logger\n\n\n# load_dotenv()\ndaytona_settings = config.daytona\nlogger.info(\"Initializing Daytona sandbox configuration\")\ndaytona_config = DaytonaConfig(\n    api_key=daytona_settings.daytona_api_key,\n    server_url=daytona_settings.daytona_server_url,\n    target=daytona_settings.daytona_target,\n)\n\nif daytona_config.api_key:\n    logger.info(\"Daytona API key configured successfully\")\nelse:\n    logger.warning(\"No Daytona API key found in environment variables\")\n\nif daytona_config.server_url:\n    logger.info(f\"Daytona server URL set to: {daytona_config.server_url}\")\nelse:\n    logger.warning(\"No Daytona server URL found in environment variables\")\n\nif daytona_config.target:\n    logger.info(f\"Daytona target set to: {daytona_config.target}\")\nelse:\n    logger.warning(\"No Daytona target found in environment variables\")\n\ndaytona = Daytona(daytona_config)\nlogger.info(\"Daytona client initialized\")\n\n\nasync def get_or_start_sandbox(sandbox_id: str):\n    \"\"\"Retrieve a sandbox by ID, check its state, and start it if needed.\"\"\"\n\n    logger.info(f\"Getting or starting sandbox with ID: {sandbox_id}\")\n\n    try:\n        sandbox = daytona.get(sandbox_id)\n\n        # Check if sandbox needs to be started\n        if (\n            sandbox.state == SandboxState.ARCHIVED\n            or sandbox.state == SandboxState.STOPPED\n        ):\n            logger.info(f\"Sandbox is in {sandbox.state} state. Starting...\")\n            try:\n                daytona.start(sandbox)\n                # Wait a moment for the sandbox to initialize\n                # sleep(5)\n                # Refresh sandbox state after starting\n                sandbox = daytona.get(sandbox_id)\n\n                # Start supervisord in a session when restarting\n                start_supervisord_session(sandbox)\n            except Exception as e:\n                logger.error(f\"Error starting sandbox: {e}\")\n                raise e\n\n        logger.info(f\"Sandbox {sandbox_id} is ready\")\n        return sandbox\n\n    except Exception as e:\n        logger.error(f\"Error retrieving or starting sandbox: {str(e)}\")\n        raise e\n\n\ndef start_supervisord_session(sandbox: Sandbox):\n    \"\"\"Start supervisord in a session.\"\"\"\n    session_id = \"supervisord-session\"\n    try:\n        logger.info(f\"Creating session {session_id} for supervisord\")\n        sandbox.process.create_session(session_id)\n\n        # Execute supervisord command\n        sandbox.process.execute_session_command(\n            session_id,\n            SessionExecuteRequest(\n                command=\"exec /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf\",\n                var_async=True,\n            ),\n        )\n        time.sleep(25)  # Wait a bit to ensure supervisord starts properly\n        logger.info(f\"Supervisord started in session {session_id}\")\n    except Exception as e:\n        logger.error(f\"Error starting supervisord session: {str(e)}\")\n        raise e\n\n\ndef create_sandbox(password: str, project_id: str = None):\n    \"\"\"Create a new sandbox with all required services configured and running.\"\"\"\n\n    logger.info(\"Creating new Daytona sandbox environment\")\n    logger.info(\"Configuring sandbox with browser-use image and environment variables\")\n\n    labels = None\n    if project_id:\n        logger.info(f\"Using sandbox_id as label: {project_id}\")\n        labels = {\"id\": project_id}\n\n    params = CreateSandboxFromImageParams(\n        image=daytona_settings.sandbox_image_name,\n        public=True,\n        labels=labels,\n        env_vars={\n            \"CHROME_PERSISTENT_SESSION\": \"true\",\n            \"RESOLUTION\": \"1024x768x24\",\n            \"RESOLUTION_WIDTH\": \"1024\",\n            \"RESOLUTION_HEIGHT\": \"768\",\n            \"VNC_PASSWORD\": password,\n            \"ANONYMIZED_TELEMETRY\": \"false\",\n            \"CHROME_PATH\": \"\",\n            \"CHROME_USER_DATA\": \"\",\n            \"CHROME_DEBUGGING_PORT\": \"9222\",\n            \"CHROME_DEBUGGING_HOST\": \"localhost\",\n            \"CHROME_CDP\": \"\",\n        },\n        resources=Resources(\n            cpu=2,\n            memory=4,\n            disk=5,\n        ),\n        auto_stop_interval=15,\n        auto_archive_interval=24 * 60,\n    )\n\n    # Create the sandbox\n    sandbox = daytona.create(params)\n    logger.info(f\"Sandbox created with ID: {sandbox.id}\")\n\n    # Start supervisord in a session for new sandbox\n    start_supervisord_session(sandbox)\n\n    logger.info(f\"Sandbox environment successfully initialized\")\n    return sandbox\n\n\nasync def delete_sandbox(sandbox_id: str):\n    \"\"\"Delete a sandbox by its ID.\"\"\"\n    logger.info(f\"Deleting sandbox with ID: {sandbox_id}\")\n\n    try:\n        # Get the sandbox\n        sandbox = daytona.get(sandbox_id)\n\n        # Delete the sandbox\n        daytona.delete(sandbox)\n\n        logger.info(f\"Successfully deleted sandbox {sandbox_id}\")\n        return True\n    except Exception as e:\n        logger.error(f\"Error deleting sandbox {sandbox_id}: {str(e)}\")\n        raise e\n"
  },
  {
    "path": "app/daytona/tool_base.py",
    "content": "from dataclasses import dataclass, field\nfrom datetime import datetime\nfrom typing import Any, ClassVar, Dict, Optional\n\nfrom daytona import Daytona, DaytonaConfig, Sandbox, SandboxState\nfrom pydantic import Field\n\nfrom app.config import config\nfrom app.daytona.sandbox import create_sandbox, start_supervisord_session\nfrom app.tool.base import BaseTool\nfrom app.utils.files_utils import clean_path\nfrom app.utils.logger import logger\n\n\n# load_dotenv()\ndaytona_settings = config.daytona\ndaytona_config = DaytonaConfig(\n    api_key=daytona_settings.daytona_api_key,\n    server_url=daytona_settings.daytona_server_url,\n    target=daytona_settings.daytona_target,\n)\ndaytona = Daytona(daytona_config)\n\n\n@dataclass\nclass ThreadMessage:\n    \"\"\"\n    Represents a message to be added to a thread.\n    \"\"\"\n\n    type: str\n    content: Dict[str, Any]\n    is_llm_message: bool = False\n    metadata: Optional[Dict[str, Any]] = None\n    timestamp: Optional[float] = field(\n        default_factory=lambda: datetime.now().timestamp()\n    )\n\n    def to_dict(self) -> Dict[str, Any]:\n        \"\"\"Convert the message to a dictionary for API calls\"\"\"\n        return {\n            \"type\": self.type,\n            \"content\": self.content,\n            \"is_llm_message\": self.is_llm_message,\n            \"metadata\": self.metadata or {},\n            \"timestamp\": self.timestamp,\n        }\n\n\nclass SandboxToolsBase(BaseTool):\n    \"\"\"Base class for all sandbox tools that provides project-based sandbox access.\"\"\"\n\n    # Class variable to track if sandbox URLs have been printed\n    _urls_printed: ClassVar[bool] = False\n\n    # Required fields\n    project_id: Optional[str] = None\n    # thread_manager: Optional[ThreadManager] = None\n\n    # Private fields (not part of the model schema)\n    _sandbox: Optional[Sandbox] = None\n    _sandbox_id: Optional[str] = None\n    _sandbox_pass: Optional[str] = None\n    workspace_path: str = Field(default=\"/workspace\", exclude=True)\n    _sessions: dict[str, str] = {}\n\n    class Config:\n        arbitrary_types_allowed = True  # Allow non-pydantic types like ThreadManager\n        underscore_attrs_are_private = True\n\n    async def _ensure_sandbox(self) -> Sandbox:\n        \"\"\"Ensure we have a valid sandbox instance, retrieving it from the project if needed.\"\"\"\n        if self._sandbox is None:\n            # Get or start the sandbox\n            try:\n                self._sandbox = create_sandbox(password=config.daytona.VNC_password)\n                # Log URLs if not already printed\n                if not SandboxToolsBase._urls_printed:\n                    vnc_link = self._sandbox.get_preview_link(6080)\n                    website_link = self._sandbox.get_preview_link(8080)\n\n                    vnc_url = (\n                        vnc_link.url if hasattr(vnc_link, \"url\") else str(vnc_link)\n                    )\n                    website_url = (\n                        website_link.url\n                        if hasattr(website_link, \"url\")\n                        else str(website_link)\n                    )\n\n                    print(\"\\033[95m***\")\n                    print(f\"VNC URL: {vnc_url}\")\n                    print(f\"Website URL: {website_url}\")\n                    print(\"***\\033[0m\")\n                    SandboxToolsBase._urls_printed = True\n            except Exception as e:\n                logger.error(f\"Error retrieving or starting sandbox: {str(e)}\")\n                raise e\n        else:\n            if (\n                self._sandbox.state == SandboxState.ARCHIVED\n                or self._sandbox.state == SandboxState.STOPPED\n            ):\n                logger.info(f\"Sandbox is in {self._sandbox.state} state. Starting...\")\n                try:\n                    daytona.start(self._sandbox)\n                    # Wait a moment for the sandbox to initialize\n                    # sleep(5)\n                    # Refresh sandbox state after starting\n\n                    # Start supervisord in a session when restarting\n                    start_supervisord_session(self._sandbox)\n                except Exception as e:\n                    logger.error(f\"Error starting sandbox: {e}\")\n                    raise e\n        return self._sandbox\n\n    @property\n    def sandbox(self) -> Sandbox:\n        \"\"\"Get the sandbox instance, ensuring it exists.\"\"\"\n        if self._sandbox is None:\n            raise RuntimeError(\"Sandbox not initialized. Call _ensure_sandbox() first.\")\n        return self._sandbox\n\n    @property\n    def sandbox_id(self) -> str:\n        \"\"\"Get the sandbox ID, ensuring it exists.\"\"\"\n        if self._sandbox_id is None:\n            raise RuntimeError(\n                \"Sandbox ID not initialized. Call _ensure_sandbox() first.\"\n            )\n        return self._sandbox_id\n\n    def clean_path(self, path: str) -> str:\n        \"\"\"Clean and normalize a path to be relative to /workspace.\"\"\"\n        cleaned_path = clean_path(path, self.workspace_path)\n        logger.debug(f\"Cleaned path: {path} -> {cleaned_path}\")\n        return cleaned_path\n"
  },
  {
    "path": "app/exceptions.py",
    "content": "class ToolError(Exception):\n    \"\"\"Raised when a tool encounters an error.\"\"\"\n\n    def __init__(self, message):\n        self.message = message\n\n\nclass OpenManusError(Exception):\n    \"\"\"Base exception for all OpenManus errors\"\"\"\n\n\nclass TokenLimitExceeded(OpenManusError):\n    \"\"\"Exception raised when the token limit is exceeded\"\"\"\n"
  },
  {
    "path": "app/flow/__init__.py",
    "content": ""
  },
  {
    "path": "app/flow/base.py",
    "content": "from abc import ABC, abstractmethod\nfrom typing import Dict, List, Optional, Union\n\nfrom pydantic import BaseModel\n\nfrom app.agent.base import BaseAgent\n\n\nclass BaseFlow(BaseModel, ABC):\n    \"\"\"Base class for execution flows supporting multiple agents\"\"\"\n\n    agents: Dict[str, BaseAgent]\n    tools: Optional[List] = None\n    primary_agent_key: Optional[str] = None\n\n    class Config:\n        arbitrary_types_allowed = True\n\n    def __init__(\n        self, agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]], **data\n    ):\n        # Handle different ways of providing agents\n        if isinstance(agents, BaseAgent):\n            agents_dict = {\"default\": agents}\n        elif isinstance(agents, list):\n            agents_dict = {f\"agent_{i}\": agent for i, agent in enumerate(agents)}\n        else:\n            agents_dict = agents\n\n        # If primary agent not specified, use first agent\n        primary_key = data.get(\"primary_agent_key\")\n        if not primary_key and agents_dict:\n            primary_key = next(iter(agents_dict))\n            data[\"primary_agent_key\"] = primary_key\n\n        # Set the agents dictionary\n        data[\"agents\"] = agents_dict\n\n        # Initialize using BaseModel's init\n        super().__init__(**data)\n\n    @property\n    def primary_agent(self) -> Optional[BaseAgent]:\n        \"\"\"Get the primary agent for the flow\"\"\"\n        return self.agents.get(self.primary_agent_key)\n\n    def get_agent(self, key: str) -> Optional[BaseAgent]:\n        \"\"\"Get a specific agent by key\"\"\"\n        return self.agents.get(key)\n\n    def add_agent(self, key: str, agent: BaseAgent) -> None:\n        \"\"\"Add a new agent to the flow\"\"\"\n        self.agents[key] = agent\n\n    @abstractmethod\n    async def execute(self, input_text: str) -> str:\n        \"\"\"Execute the flow with given input\"\"\"\n"
  },
  {
    "path": "app/flow/flow_factory.py",
    "content": "from enum import Enum\nfrom typing import Dict, List, Union\n\nfrom app.agent.base import BaseAgent\nfrom app.flow.base import BaseFlow\nfrom app.flow.planning import PlanningFlow\n\n\nclass FlowType(str, Enum):\n    PLANNING = \"planning\"\n\n\nclass FlowFactory:\n    \"\"\"Factory for creating different types of flows with support for multiple agents\"\"\"\n\n    @staticmethod\n    def create_flow(\n        flow_type: FlowType,\n        agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]],\n        **kwargs,\n    ) -> BaseFlow:\n        flows = {\n            FlowType.PLANNING: PlanningFlow,\n        }\n\n        flow_class = flows.get(flow_type)\n        if not flow_class:\n            raise ValueError(f\"Unknown flow type: {flow_type}\")\n\n        return flow_class(agents, **kwargs)\n"
  },
  {
    "path": "app/flow/planning.py",
    "content": "import json\nimport time\nfrom enum import Enum\nfrom typing import Dict, List, Optional, Union\n\nfrom pydantic import Field\n\nfrom app.agent.base import BaseAgent\nfrom app.flow.base import BaseFlow\nfrom app.llm import LLM\nfrom app.logger import logger\nfrom app.schema import AgentState, Message, ToolChoice\nfrom app.tool import PlanningTool\n\n\nclass PlanStepStatus(str, Enum):\n    \"\"\"Enum class defining possible statuses of a plan step\"\"\"\n\n    NOT_STARTED = \"not_started\"\n    IN_PROGRESS = \"in_progress\"\n    COMPLETED = \"completed\"\n    BLOCKED = \"blocked\"\n\n    @classmethod\n    def get_all_statuses(cls) -> list[str]:\n        \"\"\"Return a list of all possible step status values\"\"\"\n        return [status.value for status in cls]\n\n    @classmethod\n    def get_active_statuses(cls) -> list[str]:\n        \"\"\"Return a list of values representing active statuses (not started or in progress)\"\"\"\n        return [cls.NOT_STARTED.value, cls.IN_PROGRESS.value]\n\n    @classmethod\n    def get_status_marks(cls) -> Dict[str, str]:\n        \"\"\"Return a mapping of statuses to their marker symbols\"\"\"\n        return {\n            cls.COMPLETED.value: \"[✓]\",\n            cls.IN_PROGRESS.value: \"[→]\",\n            cls.BLOCKED.value: \"[!]\",\n            cls.NOT_STARTED.value: \"[ ]\",\n        }\n\n\nclass PlanningFlow(BaseFlow):\n    \"\"\"A flow that manages planning and execution of tasks using agents.\"\"\"\n\n    llm: LLM = Field(default_factory=lambda: LLM())\n    planning_tool: PlanningTool = Field(default_factory=PlanningTool)\n    executor_keys: List[str] = Field(default_factory=list)\n    active_plan_id: str = Field(default_factory=lambda: f\"plan_{int(time.time())}\")\n    current_step_index: Optional[int] = None\n\n    def __init__(\n        self, agents: Union[BaseAgent, List[BaseAgent], Dict[str, BaseAgent]], **data\n    ):\n        # Set executor keys before super().__init__\n        if \"executors\" in data:\n            data[\"executor_keys\"] = data.pop(\"executors\")\n\n        # Set plan ID if provided\n        if \"plan_id\" in data:\n            data[\"active_plan_id\"] = data.pop(\"plan_id\")\n\n        # Initialize the planning tool if not provided\n        if \"planning_tool\" not in data:\n            planning_tool = PlanningTool()\n            data[\"planning_tool\"] = planning_tool\n\n        # Call parent's init with the processed data\n        super().__init__(agents, **data)\n\n        # Set executor_keys to all agent keys if not specified\n        if not self.executor_keys:\n            self.executor_keys = list(self.agents.keys())\n\n    def get_executor(self, step_type: Optional[str] = None) -> BaseAgent:\n        \"\"\"\n        Get an appropriate executor agent for the current step.\n        Can be extended to select agents based on step type/requirements.\n        \"\"\"\n        # If step type is provided and matches an agent key, use that agent\n        if step_type and step_type in self.agents:\n            return self.agents[step_type]\n\n        # Otherwise use the first available executor or fall back to primary agent\n        for key in self.executor_keys:\n            if key in self.agents:\n                return self.agents[key]\n\n        # Fallback to primary agent\n        return self.primary_agent\n\n    async def execute(self, input_text: str) -> str:\n        \"\"\"Execute the planning flow with agents.\"\"\"\n        try:\n            if not self.primary_agent:\n                raise ValueError(\"No primary agent available\")\n\n            # Create initial plan if input provided\n            if input_text:\n                await self._create_initial_plan(input_text)\n\n                # Verify plan was created successfully\n                if self.active_plan_id not in self.planning_tool.plans:\n                    logger.error(\n                        f\"Plan creation failed. Plan ID {self.active_plan_id} not found in planning tool.\"\n                    )\n                    return f\"Failed to create plan for: {input_text}\"\n\n            result = \"\"\n            while True:\n                # Get current step to execute\n                self.current_step_index, step_info = await self._get_current_step_info()\n\n                # Exit if no more steps or plan completed\n                if self.current_step_index is None:\n                    result += await self._finalize_plan()\n                    break\n\n                # Execute current step with appropriate agent\n                step_type = step_info.get(\"type\") if step_info else None\n                executor = self.get_executor(step_type)\n                step_result = await self._execute_step(executor, step_info)\n                result += step_result + \"\\n\"\n\n                # Check if agent wants to terminate\n                if hasattr(executor, \"state\") and executor.state == AgentState.FINISHED:\n                    break\n\n            return result\n        except Exception as e:\n            logger.error(f\"Error in PlanningFlow: {str(e)}\")\n            return f\"Execution failed: {str(e)}\"\n\n    async def _create_initial_plan(self, request: str) -> None:\n        \"\"\"Create an initial plan based on the request using the flow's LLM and PlanningTool.\"\"\"\n        logger.info(f\"Creating initial plan with ID: {self.active_plan_id}\")\n\n        system_message_content = (\n            \"You are a planning assistant. Create a concise, actionable plan with clear steps. \"\n            \"Focus on key milestones rather than detailed sub-steps. \"\n            \"Optimize for clarity and efficiency.\"\n        )\n        agents_description = []\n        for key in self.executor_keys:\n            if key in self.agents:\n                agents_description.append(\n                    {\n                        \"name\": key.upper(),\n                        \"description\": self.agents[key].description,\n                    }\n                )\n        if len(agents_description) > 1:\n            # Add description of agents to select\n            system_message_content += (\n                f\"\\nNow we have {agents_description} agents. \"\n                f\"The infomation of them are below: {json.dumps(agents_description)}\\n\"\n                \"When creating steps in the planning tool, please specify the agent names using the format '[agent_name]'.\"\n            )\n\n        # Create a system message for plan creation\n        system_message = Message.system_message(system_message_content)\n\n        # Create a user message with the request\n        user_message = Message.user_message(\n            f\"Create a reasonable plan with clear steps to accomplish the task: {request}\"\n        )\n\n        # Call LLM with PlanningTool\n        response = await self.llm.ask_tool(\n            messages=[user_message],\n            system_msgs=[system_message],\n            tools=[self.planning_tool.to_param()],\n            tool_choice=ToolChoice.AUTO,\n        )\n\n        # Process tool calls if present\n        if response.tool_calls:\n            for tool_call in response.tool_calls:\n                if tool_call.function.name == \"planning\":\n                    # Parse the arguments\n                    args = tool_call.function.arguments\n                    if isinstance(args, str):\n                        try:\n                            args = json.loads(args)\n                        except json.JSONDecodeError:\n                            logger.error(f\"Failed to parse tool arguments: {args}\")\n                            continue\n\n                    # Ensure plan_id is set correctly and execute the tool\n                    args[\"plan_id\"] = self.active_plan_id\n\n                    # Execute the tool via ToolCollection instead of directly\n                    result = await self.planning_tool.execute(**args)\n\n                    logger.info(f\"Plan creation result: {str(result)}\")\n                    return\n\n        # If execution reached here, create a default plan\n        logger.warning(\"Creating default plan\")\n\n        # Create default plan using the ToolCollection\n        await self.planning_tool.execute(\n            **{\n                \"command\": \"create\",\n                \"plan_id\": self.active_plan_id,\n                \"title\": f\"Plan for: {request[:50]}{'...' if len(request) > 50 else ''}\",\n                \"steps\": [\"Analyze request\", \"Execute task\", \"Verify results\"],\n            }\n        )\n\n    async def _get_current_step_info(self) -> tuple[Optional[int], Optional[dict]]:\n        \"\"\"\n        Parse the current plan to identify the first non-completed step's index and info.\n        Returns (None, None) if no active step is found.\n        \"\"\"\n        if (\n            not self.active_plan_id\n            or self.active_plan_id not in self.planning_tool.plans\n        ):\n            logger.error(f\"Plan with ID {self.active_plan_id} not found\")\n            return None, None\n\n        try:\n            # Direct access to plan data from planning tool storage\n            plan_data = self.planning_tool.plans[self.active_plan_id]\n            steps = plan_data.get(\"steps\", [])\n            step_statuses = plan_data.get(\"step_statuses\", [])\n\n            # Find first non-completed step\n            for i, step in enumerate(steps):\n                if i >= len(step_statuses):\n                    status = PlanStepStatus.NOT_STARTED.value\n                else:\n                    status = step_statuses[i]\n\n                if status in PlanStepStatus.get_active_statuses():\n                    # Extract step type/category if available\n                    step_info = {\"text\": step}\n\n                    # Try to extract step type from the text (e.g., [SEARCH] or [CODE])\n                    import re\n\n                    type_match = re.search(r\"\\[([A-Z_]+)\\]\", step)\n                    if type_match:\n                        step_info[\"type\"] = type_match.group(1).lower()\n\n                    # Mark current step as in_progress\n                    try:\n                        await self.planning_tool.execute(\n                            command=\"mark_step\",\n                            plan_id=self.active_plan_id,\n                            step_index=i,\n                            step_status=PlanStepStatus.IN_PROGRESS.value,\n                        )\n                    except Exception as e:\n                        logger.warning(f\"Error marking step as in_progress: {e}\")\n                        # Update step status directly if needed\n                        if i < len(step_statuses):\n                            step_statuses[i] = PlanStepStatus.IN_PROGRESS.value\n                        else:\n                            while len(step_statuses) < i:\n                                step_statuses.append(PlanStepStatus.NOT_STARTED.value)\n                            step_statuses.append(PlanStepStatus.IN_PROGRESS.value)\n\n                        plan_data[\"step_statuses\"] = step_statuses\n\n                    return i, step_info\n\n            return None, None  # No active step found\n\n        except Exception as e:\n            logger.warning(f\"Error finding current step index: {e}\")\n            return None, None\n\n    async def _execute_step(self, executor: BaseAgent, step_info: dict) -> str:\n        \"\"\"Execute the current step with the specified agent using agent.run().\"\"\"\n        # Prepare context for the agent with current plan status\n        plan_status = await self._get_plan_text()\n        step_text = step_info.get(\"text\", f\"Step {self.current_step_index}\")\n\n        # Create a prompt for the agent to execute the current step\n        step_prompt = f\"\"\"\n        CURRENT PLAN STATUS:\n        {plan_status}\n\n        YOUR CURRENT TASK:\n        You are now working on step {self.current_step_index}: \"{step_text}\"\n\n        Please only execute this current step using the appropriate tools. When you're done, provide a summary of what you accomplished.\n        \"\"\"\n\n        # Use agent.run() to execute the step\n        try:\n            step_result = await executor.run(step_prompt)\n\n            # Mark the step as completed after successful execution\n            await self._mark_step_completed()\n\n            return step_result\n        except Exception as e:\n            logger.error(f\"Error executing step {self.current_step_index}: {e}\")\n            return f\"Error executing step {self.current_step_index}: {str(e)}\"\n\n    async def _mark_step_completed(self) -> None:\n        \"\"\"Mark the current step as completed.\"\"\"\n        if self.current_step_index is None:\n            return\n\n        try:\n            # Mark the step as completed\n            await self.planning_tool.execute(\n                command=\"mark_step\",\n                plan_id=self.active_plan_id,\n                step_index=self.current_step_index,\n                step_status=PlanStepStatus.COMPLETED.value,\n            )\n            logger.info(\n                f\"Marked step {self.current_step_index} as completed in plan {self.active_plan_id}\"\n            )\n        except Exception as e:\n            logger.warning(f\"Failed to update plan status: {e}\")\n            # Update step status directly in planning tool storage\n            if self.active_plan_id in self.planning_tool.plans:\n                plan_data = self.planning_tool.plans[self.active_plan_id]\n                step_statuses = plan_data.get(\"step_statuses\", [])\n\n                # Ensure the step_statuses list is long enough\n                while len(step_statuses) <= self.current_step_index:\n                    step_statuses.append(PlanStepStatus.NOT_STARTED.value)\n\n                # Update the status\n                step_statuses[self.current_step_index] = PlanStepStatus.COMPLETED.value\n                plan_data[\"step_statuses\"] = step_statuses\n\n    async def _get_plan_text(self) -> str:\n        \"\"\"Get the current plan as formatted text.\"\"\"\n        try:\n            result = await self.planning_tool.execute(\n                command=\"get\", plan_id=self.active_plan_id\n            )\n            return result.output if hasattr(result, \"output\") else str(result)\n        except Exception as e:\n            logger.error(f\"Error getting plan: {e}\")\n            return self._generate_plan_text_from_storage()\n\n    def _generate_plan_text_from_storage(self) -> str:\n        \"\"\"Generate plan text directly from storage if the planning tool fails.\"\"\"\n        try:\n            if self.active_plan_id not in self.planning_tool.plans:\n                return f\"Error: Plan with ID {self.active_plan_id} not found\"\n\n            plan_data = self.planning_tool.plans[self.active_plan_id]\n            title = plan_data.get(\"title\", \"Untitled Plan\")\n            steps = plan_data.get(\"steps\", [])\n            step_statuses = plan_data.get(\"step_statuses\", [])\n            step_notes = plan_data.get(\"step_notes\", [])\n\n            # Ensure step_statuses and step_notes match the number of steps\n            while len(step_statuses) < len(steps):\n                step_statuses.append(PlanStepStatus.NOT_STARTED.value)\n            while len(step_notes) < len(steps):\n                step_notes.append(\"\")\n\n            # Count steps by status\n            status_counts = {status: 0 for status in PlanStepStatus.get_all_statuses()}\n\n            for status in step_statuses:\n                if status in status_counts:\n                    status_counts[status] += 1\n\n            completed = status_counts[PlanStepStatus.COMPLETED.value]\n            total = len(steps)\n            progress = (completed / total) * 100 if total > 0 else 0\n\n            plan_text = f\"Plan: {title} (ID: {self.active_plan_id})\\n\"\n            plan_text += \"=\" * len(plan_text) + \"\\n\\n\"\n\n            plan_text += (\n                f\"Progress: {completed}/{total} steps completed ({progress:.1f}%)\\n\"\n            )\n            plan_text += f\"Status: {status_counts[PlanStepStatus.COMPLETED.value]} completed, {status_counts[PlanStepStatus.IN_PROGRESS.value]} in progress, \"\n            plan_text += f\"{status_counts[PlanStepStatus.BLOCKED.value]} blocked, {status_counts[PlanStepStatus.NOT_STARTED.value]} not started\\n\\n\"\n            plan_text += \"Steps:\\n\"\n\n            status_marks = PlanStepStatus.get_status_marks()\n\n            for i, (step, status, notes) in enumerate(\n                zip(steps, step_statuses, step_notes)\n            ):\n                # Use status marks to indicate step status\n                status_mark = status_marks.get(\n                    status, status_marks[PlanStepStatus.NOT_STARTED.value]\n                )\n\n                plan_text += f\"{i}. {status_mark} {step}\\n\"\n                if notes:\n                    plan_text += f\"   Notes: {notes}\\n\"\n\n            return plan_text\n        except Exception as e:\n            logger.error(f\"Error generating plan text from storage: {e}\")\n            return f\"Error: Unable to retrieve plan with ID {self.active_plan_id}\"\n\n    async def _finalize_plan(self) -> str:\n        \"\"\"Finalize the plan and provide a summary using the flow's LLM directly.\"\"\"\n        plan_text = await self._get_plan_text()\n\n        # Create a summary using the flow's LLM directly\n        try:\n            system_message = Message.system_message(\n                \"You are a planning assistant. Your task is to summarize the completed plan.\"\n            )\n\n            user_message = Message.user_message(\n                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.\"\n            )\n\n            response = await self.llm.ask(\n                messages=[user_message], system_msgs=[system_message]\n            )\n\n            return f\"Plan completed:\\n\\n{response}\"\n        except Exception as e:\n            logger.error(f\"Error finalizing plan with LLM: {e}\")\n\n            # Fallback to using an agent for the summary\n            try:\n                agent = self.primary_agent\n                summary_prompt = f\"\"\"\n                The plan has been completed. Here is the final plan status:\n\n                {plan_text}\n\n                Please provide a summary of what was accomplished and any final thoughts.\n                \"\"\"\n                summary = await agent.run(summary_prompt)\n                return f\"Plan completed:\\n\\n{summary}\"\n            except Exception as e2:\n                logger.error(f\"Error finalizing plan with agent: {e2}\")\n                return \"Plan completed. Error generating summary.\"\n"
  },
  {
    "path": "app/llm.py",
    "content": "import math\nfrom typing import Dict, List, Optional, Union\n\nimport tiktoken\nfrom openai import (\n    APIError,\n    AsyncAzureOpenAI,\n    AsyncOpenAI,\n    AuthenticationError,\n    OpenAIError,\n    RateLimitError,\n)\nfrom openai.types.chat import ChatCompletion, ChatCompletionMessage\nfrom tenacity import (\n    retry,\n    retry_if_exception_type,\n    stop_after_attempt,\n    wait_random_exponential,\n)\n\nfrom app.bedrock import BedrockClient\nfrom app.config import LLMSettings, config\nfrom app.exceptions import TokenLimitExceeded\nfrom app.logger import logger  # Assuming a logger is set up in your app\nfrom app.schema import (\n    ROLE_VALUES,\n    TOOL_CHOICE_TYPE,\n    TOOL_CHOICE_VALUES,\n    Message,\n    ToolChoice,\n)\n\n\nREASONING_MODELS = [\"o1\", \"o3-mini\"]\nMULTIMODAL_MODELS = [\n    \"gpt-4-vision-preview\",\n    \"gpt-4o\",\n    \"gpt-4o-mini\",\n    \"claude-3-opus-20240229\",\n    \"claude-3-sonnet-20240229\",\n    \"claude-3-haiku-20240307\",\n]\n\n\nclass TokenCounter:\n    # Token constants\n    BASE_MESSAGE_TOKENS = 4\n    FORMAT_TOKENS = 2\n    LOW_DETAIL_IMAGE_TOKENS = 85\n    HIGH_DETAIL_TILE_TOKENS = 170\n\n    # Image processing constants\n    MAX_SIZE = 2048\n    HIGH_DETAIL_TARGET_SHORT_SIDE = 768\n    TILE_SIZE = 512\n\n    def __init__(self, tokenizer):\n        self.tokenizer = tokenizer\n\n    def count_text(self, text: str) -> int:\n        \"\"\"Calculate tokens for a text string\"\"\"\n        return 0 if not text else len(self.tokenizer.encode(text))\n\n    def count_image(self, image_item: dict) -> int:\n        \"\"\"\n        Calculate tokens for an image based on detail level and dimensions\n\n        For \"low\" detail: fixed 85 tokens\n        For \"high\" detail:\n        1. Scale to fit in 2048x2048 square\n        2. Scale shortest side to 768px\n        3. Count 512px tiles (170 tokens each)\n        4. Add 85 tokens\n        \"\"\"\n        detail = image_item.get(\"detail\", \"medium\")\n\n        # For low detail, always return fixed token count\n        if detail == \"low\":\n            return self.LOW_DETAIL_IMAGE_TOKENS\n\n        # For medium detail (default in OpenAI), use high detail calculation\n        # OpenAI doesn't specify a separate calculation for medium\n\n        # For high detail, calculate based on dimensions if available\n        if detail == \"high\" or detail == \"medium\":\n            # If dimensions are provided in the image_item\n            if \"dimensions\" in image_item:\n                width, height = image_item[\"dimensions\"]\n                return self._calculate_high_detail_tokens(width, height)\n\n        return (\n            self._calculate_high_detail_tokens(1024, 1024) if detail == \"high\" else 1024\n        )\n\n    def _calculate_high_detail_tokens(self, width: int, height: int) -> int:\n        \"\"\"Calculate tokens for high detail images based on dimensions\"\"\"\n        # Step 1: Scale to fit in MAX_SIZE x MAX_SIZE square\n        if width > self.MAX_SIZE or height > self.MAX_SIZE:\n            scale = self.MAX_SIZE / max(width, height)\n            width = int(width * scale)\n            height = int(height * scale)\n\n        # Step 2: Scale so shortest side is HIGH_DETAIL_TARGET_SHORT_SIDE\n        scale = self.HIGH_DETAIL_TARGET_SHORT_SIDE / min(width, height)\n        scaled_width = int(width * scale)\n        scaled_height = int(height * scale)\n\n        # Step 3: Count number of 512px tiles\n        tiles_x = math.ceil(scaled_width / self.TILE_SIZE)\n        tiles_y = math.ceil(scaled_height / self.TILE_SIZE)\n        total_tiles = tiles_x * tiles_y\n\n        # Step 4: Calculate final token count\n        return (\n            total_tiles * self.HIGH_DETAIL_TILE_TOKENS\n        ) + self.LOW_DETAIL_IMAGE_TOKENS\n\n    def count_content(self, content: Union[str, List[Union[str, dict]]]) -> int:\n        \"\"\"Calculate tokens for message content\"\"\"\n        if not content:\n            return 0\n\n        if isinstance(content, str):\n            return self.count_text(content)\n\n        token_count = 0\n        for item in content:\n            if isinstance(item, str):\n                token_count += self.count_text(item)\n            elif isinstance(item, dict):\n                if \"text\" in item:\n                    token_count += self.count_text(item[\"text\"])\n                elif \"image_url\" in item:\n                    token_count += self.count_image(item)\n        return token_count\n\n    def count_tool_calls(self, tool_calls: List[dict]) -> int:\n        \"\"\"Calculate tokens for tool calls\"\"\"\n        token_count = 0\n        for tool_call in tool_calls:\n            if \"function\" in tool_call:\n                function = tool_call[\"function\"]\n                token_count += self.count_text(function.get(\"name\", \"\"))\n                token_count += self.count_text(function.get(\"arguments\", \"\"))\n        return token_count\n\n    def count_message_tokens(self, messages: List[dict]) -> int:\n        \"\"\"Calculate the total number of tokens in a message list\"\"\"\n        total_tokens = self.FORMAT_TOKENS  # Base format tokens\n\n        for message in messages:\n            tokens = self.BASE_MESSAGE_TOKENS  # Base tokens per message\n\n            # Add role tokens\n            tokens += self.count_text(message.get(\"role\", \"\"))\n\n            # Add content tokens\n            if \"content\" in message:\n                tokens += self.count_content(message[\"content\"])\n\n            # Add tool calls tokens\n            if \"tool_calls\" in message:\n                tokens += self.count_tool_calls(message[\"tool_calls\"])\n\n            # Add name and tool_call_id tokens\n            tokens += self.count_text(message.get(\"name\", \"\"))\n            tokens += self.count_text(message.get(\"tool_call_id\", \"\"))\n\n            total_tokens += tokens\n\n        return total_tokens\n\n\nclass LLM:\n    _instances: Dict[str, \"LLM\"] = {}\n\n    def __new__(\n        cls, config_name: str = \"default\", llm_config: Optional[LLMSettings] = None\n    ):\n        if config_name not in cls._instances:\n            instance = super().__new__(cls)\n            instance.__init__(config_name, llm_config)\n            cls._instances[config_name] = instance\n        return cls._instances[config_name]\n\n    def __init__(\n        self, config_name: str = \"default\", llm_config: Optional[LLMSettings] = None\n    ):\n        if not hasattr(self, \"client\"):  # Only initialize if not already initialized\n            llm_config = llm_config or config.llm\n            llm_config = llm_config.get(config_name, llm_config[\"default\"])\n            self.model = llm_config.model\n            self.max_tokens = llm_config.max_tokens\n            self.temperature = llm_config.temperature\n            self.api_type = llm_config.api_type\n            self.api_key = llm_config.api_key\n            self.api_version = llm_config.api_version\n            self.base_url = llm_config.base_url\n\n            # Add token counting related attributes\n            self.total_input_tokens = 0\n            self.total_completion_tokens = 0\n            self.max_input_tokens = (\n                llm_config.max_input_tokens\n                if hasattr(llm_config, \"max_input_tokens\")\n                else None\n            )\n\n            # Initialize tokenizer\n            try:\n                self.tokenizer = tiktoken.encoding_for_model(self.model)\n            except KeyError:\n                # If the model is not in tiktoken's presets, use cl100k_base as default\n                self.tokenizer = tiktoken.get_encoding(\"cl100k_base\")\n\n            if self.api_type == \"azure\":\n                self.client = AsyncAzureOpenAI(\n                    base_url=self.base_url,\n                    api_key=self.api_key,\n                    api_version=self.api_version,\n                )\n            elif self.api_type == \"aws\":\n                self.client = BedrockClient()\n            else:\n                self.client = AsyncOpenAI(api_key=self.api_key, base_url=self.base_url)\n\n            self.token_counter = TokenCounter(self.tokenizer)\n\n    def count_tokens(self, text: str) -> int:\n        \"\"\"Calculate the number of tokens in a text\"\"\"\n        if not text:\n            return 0\n        return len(self.tokenizer.encode(text))\n\n    def count_message_tokens(self, messages: List[dict]) -> int:\n        return self.token_counter.count_message_tokens(messages)\n\n    def update_token_count(self, input_tokens: int, completion_tokens: int = 0) -> None:\n        \"\"\"Update token counts\"\"\"\n        # Only track tokens if max_input_tokens is set\n        self.total_input_tokens += input_tokens\n        self.total_completion_tokens += completion_tokens\n        logger.info(\n            f\"Token usage: Input={input_tokens}, Completion={completion_tokens}, \"\n            f\"Cumulative Input={self.total_input_tokens}, Cumulative Completion={self.total_completion_tokens}, \"\n            f\"Total={input_tokens + completion_tokens}, Cumulative Total={self.total_input_tokens + self.total_completion_tokens}\"\n        )\n\n    def check_token_limit(self, input_tokens: int) -> bool:\n        \"\"\"Check if token limits are exceeded\"\"\"\n        if self.max_input_tokens is not None:\n            return (self.total_input_tokens + input_tokens) <= self.max_input_tokens\n        # If max_input_tokens is not set, always return True\n        return True\n\n    def get_limit_error_message(self, input_tokens: int) -> str:\n        \"\"\"Generate error message for token limit exceeded\"\"\"\n        if (\n            self.max_input_tokens is not None\n            and (self.total_input_tokens + input_tokens) > self.max_input_tokens\n        ):\n            return f\"Request may exceed input token limit (Current: {self.total_input_tokens}, Needed: {input_tokens}, Max: {self.max_input_tokens})\"\n\n        return \"Token limit exceeded\"\n\n    @staticmethod\n    def format_messages(\n        messages: List[Union[dict, Message]], supports_images: bool = False\n    ) -> List[dict]:\n        \"\"\"\n        Format messages for LLM by converting them to OpenAI message format.\n\n        Args:\n            messages: List of messages that can be either dict or Message objects\n            supports_images: Flag indicating if the target model supports image inputs\n\n        Returns:\n            List[dict]: List of formatted messages in OpenAI format\n\n        Raises:\n            ValueError: If messages are invalid or missing required fields\n            TypeError: If unsupported message types are provided\n\n        Examples:\n            >>> msgs = [\n            ...     Message.system_message(\"You are a helpful assistant\"),\n            ...     {\"role\": \"user\", \"content\": \"Hello\"},\n            ...     Message.user_message(\"How are you?\")\n            ... ]\n            >>> formatted = LLM.format_messages(msgs)\n        \"\"\"\n        formatted_messages = []\n\n        for message in messages:\n            # Convert Message objects to dictionaries\n            if isinstance(message, Message):\n                message = message.to_dict()\n\n            if isinstance(message, dict):\n                # If message is a dict, ensure it has required fields\n                if \"role\" not in message:\n                    raise ValueError(\"Message dict must contain 'role' field\")\n\n                # Process base64 images if present and model supports images\n                if supports_images and message.get(\"base64_image\"):\n                    # Initialize or convert content to appropriate format\n                    if not message.get(\"content\"):\n                        message[\"content\"] = []\n                    elif isinstance(message[\"content\"], str):\n                        message[\"content\"] = [\n                            {\"type\": \"text\", \"text\": message[\"content\"]}\n                        ]\n                    elif isinstance(message[\"content\"], list):\n                        # Convert string items to proper text objects\n                        message[\"content\"] = [\n                            (\n                                {\"type\": \"text\", \"text\": item}\n                                if isinstance(item, str)\n                                else item\n                            )\n                            for item in message[\"content\"]\n                        ]\n\n                    # Add the image to content\n                    message[\"content\"].append(\n                        {\n                            \"type\": \"image_url\",\n                            \"image_url\": {\n                                \"url\": f\"data:image/jpeg;base64,{message['base64_image']}\"\n                            },\n                        }\n                    )\n\n                    # Remove the base64_image field\n                    del message[\"base64_image\"]\n                # If model doesn't support images but message has base64_image, handle gracefully\n                elif not supports_images and message.get(\"base64_image\"):\n                    # Just remove the base64_image field and keep the text content\n                    del message[\"base64_image\"]\n\n                if \"content\" in message or \"tool_calls\" in message:\n                    formatted_messages.append(message)\n                # else: do not include the message\n            else:\n                raise TypeError(f\"Unsupported message type: {type(message)}\")\n\n        # Validate all messages have required fields\n        for msg in formatted_messages:\n            if msg[\"role\"] not in ROLE_VALUES:\n                raise ValueError(f\"Invalid role: {msg['role']}\")\n\n        return formatted_messages\n\n    @retry(\n        wait=wait_random_exponential(min=1, max=60),\n        stop=stop_after_attempt(6),\n        retry=retry_if_exception_type(\n            (OpenAIError, Exception, ValueError)\n        ),  # Don't retry TokenLimitExceeded\n    )\n    async def ask(\n        self,\n        messages: List[Union[dict, Message]],\n        system_msgs: Optional[List[Union[dict, Message]]] = None,\n        stream: bool = True,\n        temperature: Optional[float] = None,\n    ) -> str:\n        \"\"\"\n        Send a prompt to the LLM and get the response.\n\n        Args:\n            messages: List of conversation messages\n            system_msgs: Optional system messages to prepend\n            stream (bool): Whether to stream the response\n            temperature (float): Sampling temperature for the response\n\n        Returns:\n            str: The generated response\n\n        Raises:\n            TokenLimitExceeded: If token limits are exceeded\n            ValueError: If messages are invalid or response is empty\n            OpenAIError: If API call fails after retries\n            Exception: For unexpected errors\n        \"\"\"\n        try:\n            # Check if the model supports images\n            supports_images = self.model in MULTIMODAL_MODELS\n\n            # Format system and user messages with image support check\n            if system_msgs:\n                system_msgs = self.format_messages(system_msgs, supports_images)\n                messages = system_msgs + self.format_messages(messages, supports_images)\n            else:\n                messages = self.format_messages(messages, supports_images)\n\n            # Calculate input token count\n            input_tokens = self.count_message_tokens(messages)\n\n            # Check if token limits are exceeded\n            if not self.check_token_limit(input_tokens):\n                error_message = self.get_limit_error_message(input_tokens)\n                # Raise a special exception that won't be retried\n                raise TokenLimitExceeded(error_message)\n\n            params = {\n                \"model\": self.model,\n                \"messages\": messages,\n            }\n\n            if self.model in REASONING_MODELS:\n                params[\"max_completion_tokens\"] = self.max_tokens\n            else:\n                params[\"max_tokens\"] = self.max_tokens\n                params[\"temperature\"] = (\n                    temperature if temperature is not None else self.temperature\n                )\n\n            if not stream:\n                # Non-streaming request\n                response = await self.client.chat.completions.create(\n                    **params, stream=False\n                )\n\n                if not response.choices or not response.choices[0].message.content:\n                    raise ValueError(\"Empty or invalid response from LLM\")\n\n                # Update token counts\n                self.update_token_count(\n                    response.usage.prompt_tokens, response.usage.completion_tokens\n                )\n\n                return response.choices[0].message.content\n\n            # Streaming request, For streaming, update estimated token count before making the request\n            self.update_token_count(input_tokens)\n\n            response = await self.client.chat.completions.create(**params, stream=True)\n\n            collected_messages = []\n            completion_text = \"\"\n            async for chunk in response:\n                chunk_message = chunk.choices[0].delta.content or \"\"\n                collected_messages.append(chunk_message)\n                completion_text += chunk_message\n                print(chunk_message, end=\"\", flush=True)\n\n            print()  # Newline after streaming\n            full_response = \"\".join(collected_messages).strip()\n            if not full_response:\n                raise ValueError(\"Empty response from streaming LLM\")\n\n            # estimate completion tokens for streaming response\n            completion_tokens = self.count_tokens(completion_text)\n            logger.info(\n                f\"Estimated completion tokens for streaming response: {completion_tokens}\"\n            )\n            self.total_completion_tokens += completion_tokens\n\n            return full_response\n\n        except TokenLimitExceeded:\n            # Re-raise token limit errors without logging\n            raise\n        except ValueError:\n            logger.exception(f\"Validation error\")\n            raise\n        except OpenAIError as oe:\n            logger.exception(f\"OpenAI API error\")\n            if isinstance(oe, AuthenticationError):\n                logger.error(\"Authentication failed. Check API key.\")\n            elif isinstance(oe, RateLimitError):\n                logger.error(\"Rate limit exceeded. Consider increasing retry attempts.\")\n            elif isinstance(oe, APIError):\n                logger.error(f\"API error: {oe}\")\n            raise\n        except Exception:\n            logger.exception(f\"Unexpected error in ask\")\n            raise\n\n    @retry(\n        wait=wait_random_exponential(min=1, max=60),\n        stop=stop_after_attempt(6),\n        retry=retry_if_exception_type(\n            (OpenAIError, Exception, ValueError)\n        ),  # Don't retry TokenLimitExceeded\n    )\n    async def ask_with_images(\n        self,\n        messages: List[Union[dict, Message]],\n        images: List[Union[str, dict]],\n        system_msgs: Optional[List[Union[dict, Message]]] = None,\n        stream: bool = False,\n        temperature: Optional[float] = None,\n    ) -> str:\n        \"\"\"\n        Send a prompt with images to the LLM and get the response.\n\n        Args:\n            messages: List of conversation messages\n            images: List of image URLs or image data dictionaries\n            system_msgs: Optional system messages to prepend\n            stream (bool): Whether to stream the response\n            temperature (float): Sampling temperature for the response\n\n        Returns:\n            str: The generated response\n\n        Raises:\n            TokenLimitExceeded: If token limits are exceeded\n            ValueError: If messages are invalid or response is empty\n            OpenAIError: If API call fails after retries\n            Exception: For unexpected errors\n        \"\"\"\n        try:\n            # For ask_with_images, we always set supports_images to True because\n            # this method should only be called with models that support images\n            if self.model not in MULTIMODAL_MODELS:\n                raise ValueError(\n                    f\"Model {self.model} does not support images. Use a model from {MULTIMODAL_MODELS}\"\n                )\n\n            # Format messages with image support\n            formatted_messages = self.format_messages(messages, supports_images=True)\n\n            # Ensure the last message is from the user to attach images\n            if not formatted_messages or formatted_messages[-1][\"role\"] != \"user\":\n                raise ValueError(\n                    \"The last message must be from the user to attach images\"\n                )\n\n            # Process the last user message to include images\n            last_message = formatted_messages[-1]\n\n            # Convert content to multimodal format if needed\n            content = last_message[\"content\"]\n            multimodal_content = (\n                [{\"type\": \"text\", \"text\": content}]\n                if isinstance(content, str)\n                else content\n                if isinstance(content, list)\n                else []\n            )\n\n            # Add images to content\n            for image in images:\n                if isinstance(image, str):\n                    multimodal_content.append(\n                        {\"type\": \"image_url\", \"image_url\": {\"url\": image}}\n                    )\n                elif isinstance(image, dict) and \"url\" in image:\n                    multimodal_content.append({\"type\": \"image_url\", \"image_url\": image})\n                elif isinstance(image, dict) and \"image_url\" in image:\n                    multimodal_content.append(image)\n                else:\n                    raise ValueError(f\"Unsupported image format: {image}\")\n\n            # Update the message with multimodal content\n            last_message[\"content\"] = multimodal_content\n\n            # Add system messages if provided\n            if system_msgs:\n                all_messages = (\n                    self.format_messages(system_msgs, supports_images=True)\n                    + formatted_messages\n                )\n            else:\n                all_messages = formatted_messages\n\n            # Calculate tokens and check limits\n            input_tokens = self.count_message_tokens(all_messages)\n            if not self.check_token_limit(input_tokens):\n                raise TokenLimitExceeded(self.get_limit_error_message(input_tokens))\n\n            # Set up API parameters\n            params = {\n                \"model\": self.model,\n                \"messages\": all_messages,\n                \"stream\": stream,\n            }\n\n            # Add model-specific parameters\n            if self.model in REASONING_MODELS:\n                params[\"max_completion_tokens\"] = self.max_tokens\n            else:\n                params[\"max_tokens\"] = self.max_tokens\n                params[\"temperature\"] = (\n                    temperature if temperature is not None else self.temperature\n                )\n\n            # Handle non-streaming request\n            if not stream:\n                response = await self.client.chat.completions.create(**params)\n\n                if not response.choices or not response.choices[0].message.content:\n                    raise ValueError(\"Empty or invalid response from LLM\")\n\n                self.update_token_count(response.usage.prompt_tokens)\n                return response.choices[0].message.content\n\n            # Handle streaming request\n            self.update_token_count(input_tokens)\n            response = await self.client.chat.completions.create(**params)\n\n            collected_messages = []\n            async for chunk in response:\n                chunk_message = chunk.choices[0].delta.content or \"\"\n                collected_messages.append(chunk_message)\n                print(chunk_message, end=\"\", flush=True)\n\n            print()  # Newline after streaming\n            full_response = \"\".join(collected_messages).strip()\n\n            if not full_response:\n                raise ValueError(\"Empty response from streaming LLM\")\n\n            return full_response\n\n        except TokenLimitExceeded:\n            raise\n        except ValueError as ve:\n            logger.error(f\"Validation error in ask_with_images: {ve}\")\n            raise\n        except OpenAIError as oe:\n            logger.error(f\"OpenAI API error: {oe}\")\n            if isinstance(oe, AuthenticationError):\n                logger.error(\"Authentication failed. Check API key.\")\n            elif isinstance(oe, RateLimitError):\n                logger.error(\"Rate limit exceeded. Consider increasing retry attempts.\")\n            elif isinstance(oe, APIError):\n                logger.error(f\"API error: {oe}\")\n            raise\n        except Exception as e:\n            logger.error(f\"Unexpected error in ask_with_images: {e}\")\n            raise\n\n    @retry(\n        wait=wait_random_exponential(min=1, max=60),\n        stop=stop_after_attempt(6),\n        retry=retry_if_exception_type(\n            (OpenAIError, Exception, ValueError)\n        ),  # Don't retry TokenLimitExceeded\n    )\n    async def ask_tool(\n        self,\n        messages: List[Union[dict, Message]],\n        system_msgs: Optional[List[Union[dict, Message]]] = None,\n        timeout: int = 300,\n        tools: Optional[List[dict]] = None,\n        tool_choice: TOOL_CHOICE_TYPE = ToolChoice.AUTO,  # type: ignore\n        temperature: Optional[float] = None,\n        **kwargs,\n    ) -> ChatCompletionMessage | None:\n        \"\"\"\n        Ask LLM using functions/tools and return the response.\n\n        Args:\n            messages: List of conversation messages\n            system_msgs: Optional system messages to prepend\n            timeout: Request timeout in seconds\n            tools: List of tools to use\n            tool_choice: Tool choice strategy\n            temperature: Sampling temperature for the response\n            **kwargs: Additional completion arguments\n\n        Returns:\n            ChatCompletionMessage: The model's response\n\n        Raises:\n            TokenLimitExceeded: If token limits are exceeded\n            ValueError: If tools, tool_choice, or messages are invalid\n            OpenAIError: If API call fails after retries\n            Exception: For unexpected errors\n        \"\"\"\n        try:\n            # Validate tool_choice\n            if tool_choice not in TOOL_CHOICE_VALUES:\n                raise ValueError(f\"Invalid tool_choice: {tool_choice}\")\n\n            # Check if the model supports images\n            supports_images = self.model in MULTIMODAL_MODELS\n\n            # Format messages\n            if system_msgs:\n                system_msgs = self.format_messages(system_msgs, supports_images)\n                messages = system_msgs + self.format_messages(messages, supports_images)\n            else:\n                messages = self.format_messages(messages, supports_images)\n\n            # Calculate input token count\n            input_tokens = self.count_message_tokens(messages)\n\n            # If there are tools, calculate token count for tool descriptions\n            tools_tokens = 0\n            if tools:\n                for tool in tools:\n                    tools_tokens += self.count_tokens(str(tool))\n\n            input_tokens += tools_tokens\n\n            # Check if token limits are exceeded\n            if not self.check_token_limit(input_tokens):\n                error_message = self.get_limit_error_message(input_tokens)\n                # Raise a special exception that won't be retried\n                raise TokenLimitExceeded(error_message)\n\n            # Validate tools if provided\n            if tools:\n                for tool in tools:\n                    if not isinstance(tool, dict) or \"type\" not in tool:\n                        raise ValueError(\"Each tool must be a dict with 'type' field\")\n\n            # Set up the completion request\n            params = {\n                \"model\": self.model,\n                \"messages\": messages,\n                \"tools\": tools,\n                \"tool_choice\": tool_choice,\n                \"timeout\": timeout,\n                **kwargs,\n            }\n\n            if self.model in REASONING_MODELS:\n                params[\"max_completion_tokens\"] = self.max_tokens\n            else:\n                params[\"max_tokens\"] = self.max_tokens\n                params[\"temperature\"] = (\n                    temperature if temperature is not None else self.temperature\n                )\n\n            params[\"stream\"] = False  # Always use non-streaming for tool requests\n            response: ChatCompletion = await self.client.chat.completions.create(\n                **params\n            )\n\n            # Check if response is valid\n            if not response.choices or not response.choices[0].message:\n                print(response)\n                # raise ValueError(\"Invalid or empty response from LLM\")\n                return None\n\n            # Update token counts\n            self.update_token_count(\n                response.usage.prompt_tokens, response.usage.completion_tokens\n            )\n\n            return response.choices[0].message\n\n        except TokenLimitExceeded:\n            # Re-raise token limit errors without logging\n            raise\n        except ValueError as ve:\n            logger.error(f\"Validation error in ask_tool: {ve}\")\n            raise\n        except OpenAIError as oe:\n            logger.error(f\"OpenAI API error: {oe}\")\n            if isinstance(oe, AuthenticationError):\n                logger.error(\"Authentication failed. Check API key.\")\n            elif isinstance(oe, RateLimitError):\n                logger.error(\"Rate limit exceeded. Consider increasing retry attempts.\")\n            elif isinstance(oe, APIError):\n                logger.error(f\"API error: {oe}\")\n            raise\n        except Exception as e:\n            logger.error(f\"Unexpected error in ask_tool: {e}\")\n            raise\n"
  },
  {
    "path": "app/logger.py",
    "content": "import sys\nfrom datetime import datetime\n\nfrom loguru import logger as _logger\n\nfrom app.config import PROJECT_ROOT\n\n\n_print_level = \"INFO\"\n\n\ndef define_log_level(print_level=\"INFO\", logfile_level=\"DEBUG\", name: str = None):\n    \"\"\"Adjust the log level to above level\"\"\"\n    global _print_level\n    _print_level = print_level\n\n    current_date = datetime.now()\n    formatted_date = current_date.strftime(\"%Y%m%d%H%M%S\")\n    log_name = (\n        f\"{name}_{formatted_date}\" if name else formatted_date\n    )  # name a log with prefix name\n\n    _logger.remove()\n    _logger.add(sys.stderr, level=print_level)\n    _logger.add(PROJECT_ROOT / f\"logs/{log_name}.log\", level=logfile_level)\n    return _logger\n\n\nlogger = define_log_level()\n\n\nif __name__ == \"__main__\":\n    logger.info(\"Starting application\")\n    logger.debug(\"Debug message\")\n    logger.warning(\"Warning message\")\n    logger.error(\"Error message\")\n    logger.critical(\"Critical message\")\n\n    try:\n        raise ValueError(\"Test error\")\n    except Exception as e:\n        logger.exception(f\"An error occurred: {e}\")\n"
  },
  {
    "path": "app/mcp/__init__.py",
    "content": ""
  },
  {
    "path": "app/mcp/server.py",
    "content": "import logging\nimport sys\n\n\nlogging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stderr)])\n\nimport argparse\nimport asyncio\nimport atexit\nimport json\nfrom inspect import Parameter, Signature\nfrom typing import Any, Dict, Optional\n\nfrom mcp.server.fastmcp import FastMCP\n\nfrom app.logger import logger\nfrom app.tool.base import BaseTool\nfrom app.tool.bash import Bash\nfrom app.tool.browser_use_tool import BrowserUseTool\nfrom app.tool.str_replace_editor import StrReplaceEditor\nfrom app.tool.terminate import Terminate\n\n\nclass MCPServer:\n    \"\"\"MCP Server implementation with tool registration and management.\"\"\"\n\n    def __init__(self, name: str = \"openmanus\"):\n        self.server = FastMCP(name)\n        self.tools: Dict[str, BaseTool] = {}\n\n        # Initialize standard tools\n        self.tools[\"bash\"] = Bash()\n        self.tools[\"browser\"] = BrowserUseTool()\n        self.tools[\"editor\"] = StrReplaceEditor()\n        self.tools[\"terminate\"] = Terminate()\n\n    def register_tool(self, tool: BaseTool, method_name: Optional[str] = None) -> None:\n        \"\"\"Register a tool with parameter validation and documentation.\"\"\"\n        tool_name = method_name or tool.name\n        tool_param = tool.to_param()\n        tool_function = tool_param[\"function\"]\n\n        # Define the async function to be registered\n        async def tool_method(**kwargs):\n            logger.info(f\"Executing {tool_name}: {kwargs}\")\n            result = await tool.execute(**kwargs)\n\n            logger.info(f\"Result of {tool_name}: {result}\")\n\n            # Handle different types of results (match original logic)\n            if hasattr(result, \"model_dump\"):\n                return json.dumps(result.model_dump())\n            elif isinstance(result, dict):\n                return json.dumps(result)\n            return result\n\n        # Set method metadata\n        tool_method.__name__ = tool_name\n        tool_method.__doc__ = self._build_docstring(tool_function)\n        tool_method.__signature__ = self._build_signature(tool_function)\n\n        # Store parameter schema (important for tools that access it programmatically)\n        param_props = tool_function.get(\"parameters\", {}).get(\"properties\", {})\n        required_params = tool_function.get(\"parameters\", {}).get(\"required\", [])\n        tool_method._parameter_schema = {\n            param_name: {\n                \"description\": param_details.get(\"description\", \"\"),\n                \"type\": param_details.get(\"type\", \"any\"),\n                \"required\": param_name in required_params,\n            }\n            for param_name, param_details in param_props.items()\n        }\n\n        # Register with server\n        self.server.tool()(tool_method)\n        logger.info(f\"Registered tool: {tool_name}\")\n\n    def _build_docstring(self, tool_function: dict) -> str:\n        \"\"\"Build a formatted docstring from tool function metadata.\"\"\"\n        description = tool_function.get(\"description\", \"\")\n        param_props = tool_function.get(\"parameters\", {}).get(\"properties\", {})\n        required_params = tool_function.get(\"parameters\", {}).get(\"required\", [])\n\n        # Build docstring (match original format)\n        docstring = description\n        if param_props:\n            docstring += \"\\n\\nParameters:\\n\"\n            for param_name, param_details in param_props.items():\n                required_str = (\n                    \"(required)\" if param_name in required_params else \"(optional)\"\n                )\n                param_type = param_details.get(\"type\", \"any\")\n                param_desc = param_details.get(\"description\", \"\")\n                docstring += (\n                    f\"    {param_name} ({param_type}) {required_str}: {param_desc}\\n\"\n                )\n\n        return docstring\n\n    def _build_signature(self, tool_function: dict) -> Signature:\n        \"\"\"Build a function signature from tool function metadata.\"\"\"\n        param_props = tool_function.get(\"parameters\", {}).get(\"properties\", {})\n        required_params = tool_function.get(\"parameters\", {}).get(\"required\", [])\n\n        parameters = []\n\n        # Follow original type mapping\n        for param_name, param_details in param_props.items():\n            param_type = param_details.get(\"type\", \"\")\n            default = Parameter.empty if param_name in required_params else None\n\n            # Map JSON Schema types to Python types (same as original)\n            annotation = Any\n            if param_type == \"string\":\n                annotation = str\n            elif param_type == \"integer\":\n                annotation = int\n            elif param_type == \"number\":\n                annotation = float\n            elif param_type == \"boolean\":\n                annotation = bool\n            elif param_type == \"object\":\n                annotation = dict\n            elif param_type == \"array\":\n                annotation = list\n\n            # Create parameter with same structure as original\n            param = Parameter(\n                name=param_name,\n                kind=Parameter.KEYWORD_ONLY,\n                default=default,\n                annotation=annotation,\n            )\n            parameters.append(param)\n\n        return Signature(parameters=parameters)\n\n    async def cleanup(self) -> None:\n        \"\"\"Clean up server resources.\"\"\"\n        logger.info(\"Cleaning up resources\")\n        # Follow original cleanup logic - only clean browser tool\n        if \"browser\" in self.tools and hasattr(self.tools[\"browser\"], \"cleanup\"):\n            await self.tools[\"browser\"].cleanup()\n\n    def register_all_tools(self) -> None:\n        \"\"\"Register all tools with the server.\"\"\"\n        for tool in self.tools.values():\n            self.register_tool(tool)\n\n    def run(self, transport: str = \"stdio\") -> None:\n        \"\"\"Run the MCP server.\"\"\"\n        # Register all tools\n        self.register_all_tools()\n\n        # Register cleanup function (match original behavior)\n        atexit.register(lambda: asyncio.run(self.cleanup()))\n\n        # Start server (with same logging as original)\n        logger.info(f\"Starting OpenManus server ({transport} mode)\")\n        self.server.run(transport=transport)\n\n\ndef parse_args() -> argparse.Namespace:\n    \"\"\"Parse command line arguments.\"\"\"\n    parser = argparse.ArgumentParser(description=\"OpenManus MCP Server\")\n    parser.add_argument(\n        \"--transport\",\n        choices=[\"stdio\"],\n        default=\"stdio\",\n        help=\"Communication method: stdio or http (default: stdio)\",\n    )\n    return parser.parse_args()\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n\n    # Create and run server (maintaining original flow)\n    server = MCPServer()\n    server.run(transport=args.transport)\n"
  },
  {
    "path": "app/prompt/__init__.py",
    "content": ""
  },
  {
    "path": "app/prompt/browser.py",
    "content": "SYSTEM_PROMPT = \"\"\"\\\nYou are an AI agent designed to automate browser tasks. Your goal is to accomplish the ultimate task following the rules.\n\n# Input Format\nTask\nPrevious steps\nCurrent URL\nOpen Tabs\nInteractive Elements\n[index]<type>text</type>\n- index: Numeric identifier for interaction\n- type: HTML element type (button, input, etc.)\n- text: Element description\nExample:\n[33]<button>Submit Form</button>\n\n- Only elements with numeric indexes in [] are interactive\n- elements without [] provide only context\n\n# Response Rules\n1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format:\n{{\"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\",\n\"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\",\n\"next_goal\": \"What needs to be done with the next immediate action\"}},\n\"action\":[{{\"one_action_name\": {{// action-specific parameter}}}}, // ... more actions in sequence]}}\n\n2. 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.\nCommon action sequences:\n- Form filling: [{{\"input_text\": {{\"index\": 1, \"text\": \"username\"}}}}, {{\"input_text\": {{\"index\": 2, \"text\": \"password\"}}}}, {{\"click_element\": {{\"index\": 3}}}}]\n- Navigation and extraction: [{{\"go_to_url\": {{\"url\": \"https://example.com\"}}}}, {{\"extract_content\": {{\"goal\": \"extract the names\"}}}}]\n- Actions are executed in the given order\n- If the page changes after an action, the sequence is interrupted and you get the new state.\n- Only provide the action sequence until an action which changes the page state significantly.\n- Try to be efficient, e.g. fill forms at once, or chain actions where nothing changes on the page\n- only use multiple actions if it makes sense.\n\n3. ELEMENT INTERACTION:\n- Only use indexes of the interactive elements\n- Elements marked with \"[]Non-interactive text\" are non-interactive\n\n4. NAVIGATION & ERROR HANDLING:\n- If no suitable elements exist, use other functions to complete the task\n- If stuck, try alternative approaches - like going back to a previous page, new search, new tab etc.\n- Handle popups/cookies by accepting or closing them\n- Use scroll to find elements you are looking for\n- If you want to research something, open a new tab instead of using the current tab\n- If captcha pops up, try to solve it - else try a different approach\n- If the page is not fully loaded, use wait action\n\n5. TASK COMPLETION:\n- Use the done action as the last action as soon as the ultimate task is complete\n- Dont use \"done\" before you are done with everything the user asked you, except you reach the last step of max_steps.\n- 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!\n- 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.\n- Don't hallucinate actions\n- 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.\n\n6. VISUAL CONTEXT:\n- When an image is provided, use it to understand the page layout\n- Bounding boxes with labels on their top right corner correspond to element indexes\n\n7. Form filling:\n- If you fill an input field and your action sequence is interrupted, most often something changed e.g. suggestions popped up under the field.\n\n8. Long tasks:\n- Keep track of the status and subresults in the memory.\n\n9. Extraction:\n- If your task is to find information - call extract_content on the specific pages to get and store the information.\nYour responses must be always JSON with the specified format.\n\"\"\"\n\nNEXT_STEP_PROMPT = \"\"\"\nWhat should I do next to achieve my goal?\n\nWhen you see [Current state starts here], focus on the following:\n- Current URL and page title{url_placeholder}\n- Available tabs{tabs_placeholder}\n- Interactive elements and their indices\n- Content above{content_above_placeholder} or below{content_below_placeholder} the viewport (if indicated)\n- Any action results or errors{results_placeholder}\n\nFor browser interactions:\n- To navigate: browser_use with action=\"go_to_url\", url=\"...\"\n- To click: browser_use with action=\"click_element\", index=N\n- To type: browser_use with action=\"input_text\", index=N, text=\"...\"\n- To extract: browser_use with action=\"extract_content\", goal=\"...\"\n- To scroll: browser_use with action=\"scroll_down\" or \"scroll_up\"\n\nConsider both what's visible and what might be beyond the current viewport.\nBe methodical - remember your progress and what you've learned so far.\n\nIf you want to stop the interaction at any point, use the `terminate` tool/function call.\n\"\"\"\n"
  },
  {
    "path": "app/prompt/manus.py",
    "content": "SYSTEM_PROMPT = (\n    \"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.\"\n    \"The initial directory is: {directory}\"\n)\n\nNEXT_STEP_PROMPT = \"\"\"\nBased 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.\n\nIf you want to stop the interaction at any point, use the `terminate` tool/function call.\n\"\"\"\n"
  },
  {
    "path": "app/prompt/mcp.py",
    "content": "\"\"\"Prompts for the MCP Agent.\"\"\"\n\nSYSTEM_PROMPT = \"\"\"You are an AI assistant with access to a Model Context Protocol (MCP) server.\nYou can use the tools provided by the MCP server to complete tasks.\nThe MCP server will dynamically expose tools that you can use - always check the available tools first.\n\nWhen using an MCP tool:\n1. Choose the appropriate tool based on your task requirements\n2. Provide properly formatted arguments as required by the tool\n3. Observe the results and use them to determine next steps\n4. Tools may change during operation - new tools might appear or existing ones might disappear\n\nFollow these guidelines:\n- Call tools with valid parameters as documented in their schemas\n- Handle errors gracefully by understanding what went wrong and trying again with corrected parameters\n- For multimedia responses (like images), you'll receive a description of the content\n- Complete user requests step by step, using the most appropriate tools\n- If multiple tools need to be called in sequence, make one call at a time and wait for results\n\nRemember to clearly explain your reasoning and actions to the user.\n\"\"\"\n\nNEXT_STEP_PROMPT = \"\"\"Based on the current state and available tools, what should be done next?\nThink step by step about the problem and identify which MCP tool would be most helpful for the current stage.\nIf you've already made progress, consider what additional information you need or what actions would move you closer to completing the task.\n\"\"\"\n\n# Additional specialized prompts\nTOOL_ERROR_PROMPT = \"\"\"You encountered an error with the tool '{tool_name}'.\nTry to understand what went wrong and correct your approach.\nCommon issues include:\n- Missing or incorrect parameters\n- Invalid parameter formats\n- Using a tool that's no longer available\n- Attempting an operation that's not supported\n\nPlease check the tool specifications and try again with corrected parameters.\n\"\"\"\n\nMULTIMEDIA_RESPONSE_PROMPT = \"\"\"You've received a multimedia response (image, audio, etc.) from the tool '{tool_name}'.\nThis content has been processed and described for you.\nUse this information to continue the task or provide insights to the user.\n\"\"\"\n"
  },
  {
    "path": "app/prompt/planning.py",
    "content": "PLANNING_SYSTEM_PROMPT = \"\"\"\nYou are an expert Planning Agent tasked with solving problems efficiently through structured plans.\nYour job is:\n1. Analyze requests to understand the task scope\n2. Create a clear, actionable plan that makes meaningful progress with the `planning` tool\n3. Execute steps using available tools as needed\n4. Track progress and adapt plans when necessary\n5. Use `finish` to conclude immediately when the task is complete\n\n\nAvailable tools will vary by task but may include:\n- `planning`: Create, update, and track plans (commands: create, update, mark_step, etc.)\n- `finish`: End the task when complete\nBreak tasks into logical steps with clear outcomes. Avoid excessive detail or sub-steps.\nThink about dependencies and verification methods.\nKnow when to conclude - don't continue thinking once objectives are met.\n\"\"\"\n\nNEXT_STEP_PROMPT = \"\"\"\nBased on the current state, what's your next action?\nChoose the most efficient path forward:\n1. Is the plan sufficient, or does it need refinement?\n2. Can you execute the next step immediately?\n3. Is the task complete? If so, use `finish` right away.\n\nBe concise in your reasoning, then select the appropriate tool or action.\n\"\"\"\n"
  },
  {
    "path": "app/prompt/swe.py",
    "content": "SYSTEM_PROMPT = \"\"\"SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface.\n\nThe special interface consists of a file editor that shows you {{WINDOW}} lines of a file at a time.\nIn addition to typical bash commands, you can also use specific commands to help you navigate and edit files.\nTo call a command, you need to invoke it with a function call/tool call.\n\nPlease note that THE EDIT COMMAND REQUIRES PROPER INDENTATION.\nIf 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.\n\nRESPONSE FORMAT:\nYour shell prompt is formatted as follows:\n(Open file: <path>)\n(Current directory: <cwd>)\nbash-$\n\nFirst, you should _always_ include a general thought about what you're going to do next.\nThen, for every response, you must include exactly _ONE_ tool call/function call.\n\nRemember, 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.\nIf 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.\nNote that the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them.\n\"\"\"\n"
  },
  {
    "path": "app/prompt/toolcall.py",
    "content": "SYSTEM_PROMPT = \"You are an agent that can execute tool calls\"\n\nNEXT_STEP_PROMPT = (\n    \"If you want to stop interaction, use `terminate` tool/function call.\"\n)\n"
  },
  {
    "path": "app/prompt/visualization.py",
    "content": "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.\n# Note:\n1. The workspace directory is: {directory}; Read / write file in workspace\n2. Generate analysis conclusion report in the end\"\"\"\n\nNEXT_STEP_PROMPT = \"\"\"Based on user needs, break down the problem and use different tools step by step to solve it.\n# Note\n1. Each step select the most appropriate tool proactively (ONLY ONE).\n2. After using each tool, clearly explain the execution results and suggest the next steps.\n3. When observation with Error, review and fix it.\"\"\"\n"
  },
  {
    "path": "app/sandbox/__init__.py",
    "content": "\"\"\"\nDocker Sandbox Module\n\nProvides secure containerized execution environment with resource limits\nand isolation for running untrusted code.\n\"\"\"\nfrom app.sandbox.client import (\n    BaseSandboxClient,\n    LocalSandboxClient,\n    create_sandbox_client,\n)\nfrom app.sandbox.core.exceptions import (\n    SandboxError,\n    SandboxResourceError,\n    SandboxTimeoutError,\n)\nfrom app.sandbox.core.manager import SandboxManager\nfrom app.sandbox.core.sandbox import DockerSandbox\n\n\n__all__ = [\n    \"DockerSandbox\",\n    \"SandboxManager\",\n    \"BaseSandboxClient\",\n    \"LocalSandboxClient\",\n    \"create_sandbox_client\",\n    \"SandboxError\",\n    \"SandboxTimeoutError\",\n    \"SandboxResourceError\",\n]\n"
  },
  {
    "path": "app/sandbox/client.py",
    "content": "from abc import ABC, abstractmethod\nfrom typing import Dict, Optional, Protocol\n\nfrom app.config import SandboxSettings\nfrom app.sandbox.core.sandbox import DockerSandbox\n\n\nclass SandboxFileOperations(Protocol):\n    \"\"\"Protocol for sandbox file operations.\"\"\"\n\n    async def copy_from(self, container_path: str, local_path: str) -> None:\n        \"\"\"Copies file from container to local.\n\n        Args:\n            container_path: File path in container.\n            local_path: Local destination path.\n        \"\"\"\n        ...\n\n    async def copy_to(self, local_path: str, container_path: str) -> None:\n        \"\"\"Copies file from local to container.\n\n        Args:\n            local_path: Local source file path.\n            container_path: Destination path in container.\n        \"\"\"\n        ...\n\n    async def read_file(self, path: str) -> str:\n        \"\"\"Reads file content from container.\n\n        Args:\n            path: File path in container.\n\n        Returns:\n            str: File content.\n        \"\"\"\n        ...\n\n    async def write_file(self, path: str, content: str) -> None:\n        \"\"\"Writes content to file in container.\n\n        Args:\n            path: File path in container.\n            content: Content to write.\n        \"\"\"\n        ...\n\n\nclass BaseSandboxClient(ABC):\n    \"\"\"Base sandbox client interface.\"\"\"\n\n    @abstractmethod\n    async def create(\n        self,\n        config: Optional[SandboxSettings] = None,\n        volume_bindings: Optional[Dict[str, str]] = None,\n    ) -> None:\n        \"\"\"Creates sandbox.\"\"\"\n\n    @abstractmethod\n    async def run_command(self, command: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Executes command.\"\"\"\n\n    @abstractmethod\n    async def copy_from(self, container_path: str, local_path: str) -> None:\n        \"\"\"Copies file from container.\"\"\"\n\n    @abstractmethod\n    async def copy_to(self, local_path: str, container_path: str) -> None:\n        \"\"\"Copies file to container.\"\"\"\n\n    @abstractmethod\n    async def read_file(self, path: str) -> str:\n        \"\"\"Reads file.\"\"\"\n\n    @abstractmethod\n    async def write_file(self, path: str, content: str) -> None:\n        \"\"\"Writes file.\"\"\"\n\n    @abstractmethod\n    async def cleanup(self) -> None:\n        \"\"\"Cleans up resources.\"\"\"\n\n\nclass LocalSandboxClient(BaseSandboxClient):\n    \"\"\"Local sandbox client implementation.\"\"\"\n\n    def __init__(self):\n        \"\"\"Initializes local sandbox client.\"\"\"\n        self.sandbox: Optional[DockerSandbox] = None\n\n    async def create(\n        self,\n        config: Optional[SandboxSettings] = None,\n        volume_bindings: Optional[Dict[str, str]] = None,\n    ) -> None:\n        \"\"\"Creates a sandbox.\n\n        Args:\n            config: Sandbox configuration.\n            volume_bindings: Volume mappings.\n\n        Raises:\n            RuntimeError: If sandbox creation fails.\n        \"\"\"\n        self.sandbox = DockerSandbox(config, volume_bindings)\n        await self.sandbox.create()\n\n    async def run_command(self, command: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Runs command in sandbox.\n\n        Args:\n            command: Command to execute.\n            timeout: Execution timeout in seconds.\n\n        Returns:\n            Command output.\n\n        Raises:\n            RuntimeError: If sandbox not initialized.\n        \"\"\"\n        if not self.sandbox:\n            raise RuntimeError(\"Sandbox not initialized\")\n        return await self.sandbox.run_command(command, timeout)\n\n    async def copy_from(self, container_path: str, local_path: str) -> None:\n        \"\"\"Copies file from container to local.\n\n        Args:\n            container_path: File path in container.\n            local_path: Local destination path.\n\n        Raises:\n            RuntimeError: If sandbox not initialized.\n        \"\"\"\n        if not self.sandbox:\n            raise RuntimeError(\"Sandbox not initialized\")\n        await self.sandbox.copy_from(container_path, local_path)\n\n    async def copy_to(self, local_path: str, container_path: str) -> None:\n        \"\"\"Copies file from local to container.\n\n        Args:\n            local_path: Local source file path.\n            container_path: Destination path in container.\n\n        Raises:\n            RuntimeError: If sandbox not initialized.\n        \"\"\"\n        if not self.sandbox:\n            raise RuntimeError(\"Sandbox not initialized\")\n        await self.sandbox.copy_to(local_path, container_path)\n\n    async def read_file(self, path: str) -> str:\n        \"\"\"Reads file from container.\n\n        Args:\n            path: File path in container.\n\n        Returns:\n            File content.\n\n        Raises:\n            RuntimeError: If sandbox not initialized.\n        \"\"\"\n        if not self.sandbox:\n            raise RuntimeError(\"Sandbox not initialized\")\n        return await self.sandbox.read_file(path)\n\n    async def write_file(self, path: str, content: str) -> None:\n        \"\"\"Writes file to container.\n\n        Args:\n            path: File path in container.\n            content: File content.\n\n        Raises:\n            RuntimeError: If sandbox not initialized.\n        \"\"\"\n        if not self.sandbox:\n            raise RuntimeError(\"Sandbox not initialized\")\n        await self.sandbox.write_file(path, content)\n\n    async def cleanup(self) -> None:\n        \"\"\"Cleans up resources.\"\"\"\n        if self.sandbox:\n            await self.sandbox.cleanup()\n            self.sandbox = None\n\n\ndef create_sandbox_client() -> LocalSandboxClient:\n    \"\"\"Creates a sandbox client.\n\n    Returns:\n        LocalSandboxClient: Sandbox client instance.\n    \"\"\"\n    return LocalSandboxClient()\n\n\nSANDBOX_CLIENT = create_sandbox_client()\n"
  },
  {
    "path": "app/sandbox/core/exceptions.py",
    "content": "\"\"\"Exception classes for the sandbox system.\n\nThis module defines custom exceptions used throughout the sandbox system to\nhandle various error conditions in a structured way.\n\"\"\"\n\n\nclass SandboxError(Exception):\n    \"\"\"Base exception for sandbox-related errors.\"\"\"\n\n\nclass SandboxTimeoutError(SandboxError):\n    \"\"\"Exception raised when a sandbox operation times out.\"\"\"\n\n\nclass SandboxResourceError(SandboxError):\n    \"\"\"Exception raised for resource-related errors.\"\"\"\n"
  },
  {
    "path": "app/sandbox/core/manager.py",
    "content": "import asyncio\nimport uuid\nfrom contextlib import asynccontextmanager\nfrom typing import Dict, Optional, Set\n\nimport docker\nfrom docker.errors import APIError, ImageNotFound\n\nfrom app.config import SandboxSettings\nfrom app.logger import logger\nfrom app.sandbox.core.sandbox import DockerSandbox\n\n\nclass SandboxManager:\n    \"\"\"Docker sandbox manager.\n\n    Manages multiple DockerSandbox instances lifecycle including creation,\n    monitoring, and cleanup. Provides concurrent access control and automatic\n    cleanup mechanisms for sandbox resources.\n\n    Attributes:\n        max_sandboxes: Maximum allowed number of sandboxes.\n        idle_timeout: Sandbox idle timeout in seconds.\n        cleanup_interval: Cleanup check interval in seconds.\n        _sandboxes: Active sandbox instance mapping.\n        _last_used: Last used time record for sandboxes.\n    \"\"\"\n\n    def __init__(\n        self,\n        max_sandboxes: int = 100,\n        idle_timeout: int = 3600,\n        cleanup_interval: int = 300,\n    ):\n        \"\"\"Initializes sandbox manager.\n\n        Args:\n            max_sandboxes: Maximum sandbox count limit.\n            idle_timeout: Idle timeout in seconds.\n            cleanup_interval: Cleanup check interval in seconds.\n        \"\"\"\n        self.max_sandboxes = max_sandboxes\n        self.idle_timeout = idle_timeout\n        self.cleanup_interval = cleanup_interval\n\n        # Docker client\n        self._client = docker.from_env()\n\n        # Resource mappings\n        self._sandboxes: Dict[str, DockerSandbox] = {}\n        self._last_used: Dict[str, float] = {}\n\n        # Concurrency control\n        self._locks: Dict[str, asyncio.Lock] = {}\n        self._global_lock = asyncio.Lock()\n        self._active_operations: Set[str] = set()\n\n        # Cleanup task\n        self._cleanup_task: Optional[asyncio.Task] = None\n        self._is_shutting_down = False\n\n        # Start automatic cleanup\n        self.start_cleanup_task()\n\n    async def ensure_image(self, image: str) -> bool:\n        \"\"\"Ensures Docker image is available.\n\n        Args:\n            image: Image name.\n\n        Returns:\n            bool: Whether image is available.\n        \"\"\"\n        try:\n            self._client.images.get(image)\n            return True\n        except ImageNotFound:\n            try:\n                logger.info(f\"Pulling image {image}...\")\n                await asyncio.get_event_loop().run_in_executor(\n                    None, self._client.images.pull, image\n                )\n                return True\n            except (APIError, Exception) as e:\n                logger.error(f\"Failed to pull image {image}: {e}\")\n                return False\n\n    @asynccontextmanager\n    async def sandbox_operation(self, sandbox_id: str):\n        \"\"\"Context manager for sandbox operations.\n\n        Provides concurrency control and usage time updates.\n\n        Args:\n            sandbox_id: Sandbox ID.\n\n        Raises:\n            KeyError: If sandbox not found.\n        \"\"\"\n        if sandbox_id not in self._locks:\n            self._locks[sandbox_id] = asyncio.Lock()\n\n        async with self._locks[sandbox_id]:\n            if sandbox_id not in self._sandboxes:\n                raise KeyError(f\"Sandbox {sandbox_id} not found\")\n\n            self._active_operations.add(sandbox_id)\n            try:\n                self._last_used[sandbox_id] = asyncio.get_event_loop().time()\n                yield self._sandboxes[sandbox_id]\n            finally:\n                self._active_operations.remove(sandbox_id)\n\n    async def create_sandbox(\n        self,\n        config: Optional[SandboxSettings] = None,\n        volume_bindings: Optional[Dict[str, str]] = None,\n    ) -> str:\n        \"\"\"Creates a new sandbox instance.\n\n        Args:\n            config: Sandbox configuration.\n            volume_bindings: Volume mapping configuration.\n\n        Returns:\n            str: Sandbox ID.\n\n        Raises:\n            RuntimeError: If max sandbox count reached or creation fails.\n        \"\"\"\n        async with self._global_lock:\n            if len(self._sandboxes) >= self.max_sandboxes:\n                raise RuntimeError(\n                    f\"Maximum number of sandboxes ({self.max_sandboxes}) reached\"\n                )\n\n            config = config or SandboxSettings()\n            if not await self.ensure_image(config.image):\n                raise RuntimeError(f\"Failed to ensure Docker image: {config.image}\")\n\n            sandbox_id = str(uuid.uuid4())\n            try:\n                sandbox = DockerSandbox(config, volume_bindings)\n                await sandbox.create()\n\n                self._sandboxes[sandbox_id] = sandbox\n                self._last_used[sandbox_id] = asyncio.get_event_loop().time()\n                self._locks[sandbox_id] = asyncio.Lock()\n\n                logger.info(f\"Created sandbox {sandbox_id}\")\n                return sandbox_id\n\n            except Exception as e:\n                logger.error(f\"Failed to create sandbox: {e}\")\n                if sandbox_id in self._sandboxes:\n                    await self.delete_sandbox(sandbox_id)\n                raise RuntimeError(f\"Failed to create sandbox: {e}\")\n\n    async def get_sandbox(self, sandbox_id: str) -> DockerSandbox:\n        \"\"\"Gets a sandbox instance.\n\n        Args:\n            sandbox_id: Sandbox ID.\n\n        Returns:\n            DockerSandbox: Sandbox instance.\n\n        Raises:\n            KeyError: If sandbox does not exist.\n        \"\"\"\n        async with self.sandbox_operation(sandbox_id) as sandbox:\n            return sandbox\n\n    def start_cleanup_task(self) -> None:\n        \"\"\"Starts automatic cleanup task.\"\"\"\n\n        async def cleanup_loop():\n            while not self._is_shutting_down:\n                try:\n                    await self._cleanup_idle_sandboxes()\n                except Exception as e:\n                    logger.error(f\"Error in cleanup loop: {e}\")\n                await asyncio.sleep(self.cleanup_interval)\n\n        self._cleanup_task = asyncio.create_task(cleanup_loop())\n\n    async def _cleanup_idle_sandboxes(self) -> None:\n        \"\"\"Cleans up idle sandboxes.\"\"\"\n        current_time = asyncio.get_event_loop().time()\n        to_cleanup = []\n\n        async with self._global_lock:\n            for sandbox_id, last_used in self._last_used.items():\n                if (\n                    sandbox_id not in self._active_operations\n                    and current_time - last_used > self.idle_timeout\n                ):\n                    to_cleanup.append(sandbox_id)\n\n        for sandbox_id in to_cleanup:\n            try:\n                await self.delete_sandbox(sandbox_id)\n            except Exception as e:\n                logger.error(f\"Error cleaning up sandbox {sandbox_id}: {e}\")\n\n    async def cleanup(self) -> None:\n        \"\"\"Cleans up all resources.\"\"\"\n        logger.info(\"Starting manager cleanup...\")\n        self._is_shutting_down = True\n\n        # Cancel cleanup task\n        if self._cleanup_task:\n            self._cleanup_task.cancel()\n            try:\n                await asyncio.wait_for(self._cleanup_task, timeout=1.0)\n            except (asyncio.CancelledError, asyncio.TimeoutError):\n                pass\n\n        # Get all sandbox IDs to clean up\n        async with self._global_lock:\n            sandbox_ids = list(self._sandboxes.keys())\n\n        # Concurrently clean up all sandboxes\n        cleanup_tasks = []\n        for sandbox_id in sandbox_ids:\n            task = asyncio.create_task(self._safe_delete_sandbox(sandbox_id))\n            cleanup_tasks.append(task)\n\n        if cleanup_tasks:\n            # Wait for all cleanup tasks to complete, with timeout to avoid infinite waiting\n            try:\n                await asyncio.wait(cleanup_tasks, timeout=30.0)\n            except asyncio.TimeoutError:\n                logger.error(\"Sandbox cleanup timed out\")\n\n        # Clean up remaining references\n        self._sandboxes.clear()\n        self._last_used.clear()\n        self._locks.clear()\n        self._active_operations.clear()\n\n        logger.info(\"Manager cleanup completed\")\n\n    async def _safe_delete_sandbox(self, sandbox_id: str) -> None:\n        \"\"\"Safely deletes a single sandbox.\n\n        Args:\n            sandbox_id: Sandbox ID to delete.\n        \"\"\"\n        try:\n            if sandbox_id in self._active_operations:\n                logger.warning(\n                    f\"Sandbox {sandbox_id} has active operations, waiting for completion\"\n                )\n                for _ in range(10):  # Wait at most 10 times\n                    await asyncio.sleep(0.5)\n                    if sandbox_id not in self._active_operations:\n                        break\n                else:\n                    logger.warning(\n                        f\"Timeout waiting for sandbox {sandbox_id} operations to complete\"\n                    )\n\n            # Get reference to sandbox object\n            sandbox = self._sandboxes.get(sandbox_id)\n            if sandbox:\n                await sandbox.cleanup()\n\n                # Remove sandbox record from manager\n                async with self._global_lock:\n                    self._sandboxes.pop(sandbox_id, None)\n                    self._last_used.pop(sandbox_id, None)\n                    self._locks.pop(sandbox_id, None)\n                    logger.info(f\"Deleted sandbox {sandbox_id}\")\n        except Exception as e:\n            logger.error(f\"Error during cleanup of sandbox {sandbox_id}: {e}\")\n\n    async def delete_sandbox(self, sandbox_id: str) -> None:\n        \"\"\"Deletes specified sandbox.\n\n        Args:\n            sandbox_id: Sandbox ID.\n        \"\"\"\n        if sandbox_id not in self._sandboxes:\n            return\n\n        try:\n            await self._safe_delete_sandbox(sandbox_id)\n        except Exception as e:\n            logger.error(f\"Failed to delete sandbox {sandbox_id}: {e}\")\n\n    async def __aenter__(self) -> \"SandboxManager\":\n        \"\"\"Async context manager entry.\"\"\"\n        return self\n\n    async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:\n        \"\"\"Async context manager exit.\"\"\"\n        await self.cleanup()\n\n    def get_stats(self) -> Dict:\n        \"\"\"Gets manager statistics.\n\n        Returns:\n            Dict: Statistics information.\n        \"\"\"\n        return {\n            \"total_sandboxes\": len(self._sandboxes),\n            \"active_operations\": len(self._active_operations),\n            \"max_sandboxes\": self.max_sandboxes,\n            \"idle_timeout\": self.idle_timeout,\n            \"cleanup_interval\": self.cleanup_interval,\n            \"is_shutting_down\": self._is_shutting_down,\n        }\n"
  },
  {
    "path": "app/sandbox/core/sandbox.py",
    "content": "import asyncio\nimport io\nimport os\nimport tarfile\nimport tempfile\nimport uuid\nfrom typing import Dict, Optional\n\nimport docker\nfrom docker.errors import NotFound\nfrom docker.models.containers import Container\n\nfrom app.config import SandboxSettings\nfrom app.sandbox.core.exceptions import SandboxTimeoutError\nfrom app.sandbox.core.terminal import AsyncDockerizedTerminal\n\n\nclass DockerSandbox:\n    \"\"\"Docker sandbox environment.\n\n    Provides a containerized execution environment with resource limits,\n    file operations, and command execution capabilities.\n\n    Attributes:\n        config: Sandbox configuration.\n        volume_bindings: Volume mapping configuration.\n        client: Docker client.\n        container: Docker container instance.\n        terminal: Container terminal interface.\n    \"\"\"\n\n    def __init__(\n        self,\n        config: Optional[SandboxSettings] = None,\n        volume_bindings: Optional[Dict[str, str]] = None,\n    ):\n        \"\"\"Initializes a sandbox instance.\n\n        Args:\n            config: Sandbox configuration. Default configuration used if None.\n            volume_bindings: Volume mappings in {host_path: container_path} format.\n        \"\"\"\n        self.config = config or SandboxSettings()\n        self.volume_bindings = volume_bindings or {}\n        self.client = docker.from_env()\n        self.container: Optional[Container] = None\n        self.terminal: Optional[AsyncDockerizedTerminal] = None\n\n    async def create(self) -> \"DockerSandbox\":\n        \"\"\"Creates and starts the sandbox container.\n\n        Returns:\n            Current sandbox instance.\n\n        Raises:\n            docker.errors.APIError: If Docker API call fails.\n            RuntimeError: If container creation or startup fails.\n        \"\"\"\n        try:\n            # Prepare container config\n            host_config = self.client.api.create_host_config(\n                mem_limit=self.config.memory_limit,\n                cpu_period=100000,\n                cpu_quota=int(100000 * self.config.cpu_limit),\n                network_mode=\"none\" if not self.config.network_enabled else \"bridge\",\n                binds=self._prepare_volume_bindings(),\n            )\n\n            # Generate unique container name with sandbox_ prefix\n            container_name = f\"sandbox_{uuid.uuid4().hex[:8]}\"\n\n            # Create container\n            container = await asyncio.to_thread(\n                self.client.api.create_container,\n                image=self.config.image,\n                command=\"tail -f /dev/null\",\n                hostname=\"sandbox\",\n                working_dir=self.config.work_dir,\n                host_config=host_config,\n                name=container_name,\n                tty=True,\n                detach=True,\n            )\n\n            self.container = self.client.containers.get(container[\"Id\"])\n\n            # Start container\n            await asyncio.to_thread(self.container.start)\n\n            # Initialize terminal\n            self.terminal = AsyncDockerizedTerminal(\n                container[\"Id\"],\n                self.config.work_dir,\n                env_vars={\"PYTHONUNBUFFERED\": \"1\"}\n                # Ensure Python output is not buffered\n            )\n            await self.terminal.init()\n\n            return self\n\n        except Exception as e:\n            await self.cleanup()  # Ensure resources are cleaned up\n            raise RuntimeError(f\"Failed to create sandbox: {e}\") from e\n\n    def _prepare_volume_bindings(self) -> Dict[str, Dict[str, str]]:\n        \"\"\"Prepares volume binding configuration.\n\n        Returns:\n            Volume binding configuration dictionary.\n        \"\"\"\n        bindings = {}\n\n        # Create and add working directory mapping\n        work_dir = self._ensure_host_dir(self.config.work_dir)\n        bindings[work_dir] = {\"bind\": self.config.work_dir, \"mode\": \"rw\"}\n\n        # Add custom volume bindings\n        for host_path, container_path in self.volume_bindings.items():\n            bindings[host_path] = {\"bind\": container_path, \"mode\": \"rw\"}\n\n        return bindings\n\n    @staticmethod\n    def _ensure_host_dir(path: str) -> str:\n        \"\"\"Ensures directory exists on the host.\n\n        Args:\n            path: Directory path.\n\n        Returns:\n            Actual path on the host.\n        \"\"\"\n        host_path = os.path.join(\n            tempfile.gettempdir(),\n            f\"sandbox_{os.path.basename(path)}_{os.urandom(4).hex()}\",\n        )\n        os.makedirs(host_path, exist_ok=True)\n        return host_path\n\n    async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Runs a command in the sandbox.\n\n        Args:\n            cmd: Command to execute.\n            timeout: Timeout in seconds.\n\n        Returns:\n            Command output as string.\n\n        Raises:\n            RuntimeError: If sandbox not initialized or command execution fails.\n            TimeoutError: If command execution times out.\n        \"\"\"\n        if not self.terminal:\n            raise RuntimeError(\"Sandbox not initialized\")\n\n        try:\n            return await self.terminal.run_command(\n                cmd, timeout=timeout or self.config.timeout\n            )\n        except TimeoutError:\n            raise SandboxTimeoutError(\n                f\"Command execution timed out after {timeout or self.config.timeout} seconds\"\n            )\n\n    async def read_file(self, path: str) -> str:\n        \"\"\"Reads a file from the container.\n\n        Args:\n            path: File path.\n\n        Returns:\n            File contents as string.\n\n        Raises:\n            FileNotFoundError: If file does not exist.\n            RuntimeError: If read operation fails.\n        \"\"\"\n        if not self.container:\n            raise RuntimeError(\"Sandbox not initialized\")\n\n        try:\n            # Get file archive\n            resolved_path = self._safe_resolve_path(path)\n            tar_stream, _ = await asyncio.to_thread(\n                self.container.get_archive, resolved_path\n            )\n\n            # Read file content from tar stream\n            content = await self._read_from_tar(tar_stream)\n            return content.decode(\"utf-8\")\n\n        except NotFound:\n            raise FileNotFoundError(f\"File not found: {path}\")\n        except Exception as e:\n            raise RuntimeError(f\"Failed to read file: {e}\")\n\n    async def write_file(self, path: str, content: str) -> None:\n        \"\"\"Writes content to a file in the container.\n\n        Args:\n            path: Target path.\n            content: File content.\n\n        Raises:\n            RuntimeError: If write operation fails.\n        \"\"\"\n        if not self.container:\n            raise RuntimeError(\"Sandbox not initialized\")\n\n        try:\n            resolved_path = self._safe_resolve_path(path)\n            parent_dir = os.path.dirname(resolved_path)\n\n            # Create parent directory\n            if parent_dir:\n                await self.run_command(f\"mkdir -p {parent_dir}\")\n\n            # Prepare file data\n            tar_stream = await self._create_tar_stream(\n                os.path.basename(path), content.encode(\"utf-8\")\n            )\n\n            # Write file\n            await asyncio.to_thread(\n                self.container.put_archive, parent_dir or \"/\", tar_stream\n            )\n\n        except Exception as e:\n            raise RuntimeError(f\"Failed to write file: {e}\")\n\n    def _safe_resolve_path(self, path: str) -> str:\n        \"\"\"Safely resolves container path, preventing path traversal.\n\n        Args:\n            path: Original path.\n\n        Returns:\n            Resolved absolute path.\n\n        Raises:\n            ValueError: If path contains potentially unsafe patterns.\n        \"\"\"\n        # Check for path traversal attempts\n        if \"..\" in path.split(\"/\"):\n            raise ValueError(\"Path contains potentially unsafe patterns\")\n\n        resolved = (\n            os.path.join(self.config.work_dir, path)\n            if not os.path.isabs(path)\n            else path\n        )\n        return resolved\n\n    async def copy_from(self, src_path: str, dst_path: str) -> None:\n        \"\"\"Copies a file from the container.\n\n        Args:\n            src_path: Source file path (container).\n            dst_path: Destination path (host).\n\n        Raises:\n            FileNotFoundError: If source file does not exist.\n            RuntimeError: If copy operation fails.\n        \"\"\"\n        try:\n            # Ensure destination file's parent directory exists\n            parent_dir = os.path.dirname(dst_path)\n            if parent_dir:\n                os.makedirs(parent_dir, exist_ok=True)\n\n            # Get file stream\n            resolved_src = self._safe_resolve_path(src_path)\n            stream, stat = await asyncio.to_thread(\n                self.container.get_archive, resolved_src\n            )\n\n            # Create temporary directory to extract file\n            with tempfile.TemporaryDirectory() as tmp_dir:\n                # Write stream to temporary file\n                tar_path = os.path.join(tmp_dir, \"temp.tar\")\n                with open(tar_path, \"wb\") as f:\n                    for chunk in stream:\n                        f.write(chunk)\n\n                # Extract file\n                with tarfile.open(tar_path) as tar:\n                    members = tar.getmembers()\n                    if not members:\n                        raise FileNotFoundError(f\"Source file is empty: {src_path}\")\n\n                    # If destination is a directory, we should preserve relative path structure\n                    if os.path.isdir(dst_path):\n                        tar.extractall(dst_path)\n                    else:\n                        # If destination is a file, we only extract the source file's content\n                        if len(members) > 1:\n                            raise RuntimeError(\n                                f\"Source path is a directory but destination is a file: {src_path}\"\n                            )\n\n                        with open(dst_path, \"wb\") as dst:\n                            src_file = tar.extractfile(members[0])\n                            if src_file is None:\n                                raise RuntimeError(\n                                    f\"Failed to extract file: {src_path}\"\n                                )\n                            dst.write(src_file.read())\n\n        except docker.errors.NotFound:\n            raise FileNotFoundError(f\"Source file not found: {src_path}\")\n        except Exception as e:\n            raise RuntimeError(f\"Failed to copy file: {e}\")\n\n    async def copy_to(self, src_path: str, dst_path: str) -> None:\n        \"\"\"Copies a file to the container.\n\n        Args:\n            src_path: Source file path (host).\n            dst_path: Destination path (container).\n\n        Raises:\n            FileNotFoundError: If source file does not exist.\n            RuntimeError: If copy operation fails.\n        \"\"\"\n        try:\n            if not os.path.exists(src_path):\n                raise FileNotFoundError(f\"Source file not found: {src_path}\")\n\n            # Create destination directory in container\n            resolved_dst = self._safe_resolve_path(dst_path)\n            container_dir = os.path.dirname(resolved_dst)\n            if container_dir:\n                await self.run_command(f\"mkdir -p {container_dir}\")\n\n            # Create tar file to upload\n            with tempfile.TemporaryDirectory() as tmp_dir:\n                tar_path = os.path.join(tmp_dir, \"temp.tar\")\n                with tarfile.open(tar_path, \"w\") as tar:\n                    # Handle directory source path\n                    if os.path.isdir(src_path):\n                        os.path.basename(src_path.rstrip(\"/\"))\n                        for root, _, files in os.walk(src_path):\n                            for file in files:\n                                file_path = os.path.join(root, file)\n                                arcname = os.path.join(\n                                    os.path.basename(dst_path),\n                                    os.path.relpath(file_path, src_path),\n                                )\n                                tar.add(file_path, arcname=arcname)\n                    else:\n                        # Add single file to tar\n                        tar.add(src_path, arcname=os.path.basename(dst_path))\n\n                # Read tar file content\n                with open(tar_path, \"rb\") as f:\n                    data = f.read()\n\n                # Upload to container\n                await asyncio.to_thread(\n                    self.container.put_archive,\n                    os.path.dirname(resolved_dst) or \"/\",\n                    data,\n                )\n\n                # Verify file was created successfully\n                try:\n                    await self.run_command(f\"test -e {resolved_dst}\")\n                except Exception:\n                    raise RuntimeError(f\"Failed to verify file creation: {dst_path}\")\n\n        except FileNotFoundError:\n            raise\n        except Exception as e:\n            raise RuntimeError(f\"Failed to copy file: {e}\")\n\n    @staticmethod\n    async def _create_tar_stream(name: str, content: bytes) -> io.BytesIO:\n        \"\"\"Creates a tar file stream.\n\n        Args:\n            name: Filename.\n            content: File content.\n\n        Returns:\n            Tar file stream.\n        \"\"\"\n        tar_stream = io.BytesIO()\n        with tarfile.open(fileobj=tar_stream, mode=\"w\") as tar:\n            tarinfo = tarfile.TarInfo(name=name)\n            tarinfo.size = len(content)\n            tar.addfile(tarinfo, io.BytesIO(content))\n        tar_stream.seek(0)\n        return tar_stream\n\n    @staticmethod\n    async def _read_from_tar(tar_stream) -> bytes:\n        \"\"\"Reads file content from a tar stream.\n\n        Args:\n            tar_stream: Tar file stream.\n\n        Returns:\n            File content.\n\n        Raises:\n            RuntimeError: If read operation fails.\n        \"\"\"\n        with tempfile.NamedTemporaryFile() as tmp:\n            for chunk in tar_stream:\n                tmp.write(chunk)\n            tmp.seek(0)\n\n            with tarfile.open(fileobj=tmp) as tar:\n                member = tar.next()\n                if not member:\n                    raise RuntimeError(\"Empty tar archive\")\n\n                file_content = tar.extractfile(member)\n                if not file_content:\n                    raise RuntimeError(\"Failed to extract file content\")\n\n                return file_content.read()\n\n    async def cleanup(self) -> None:\n        \"\"\"Cleans up sandbox resources.\"\"\"\n        errors = []\n        try:\n            if self.terminal:\n                try:\n                    await self.terminal.close()\n                except Exception as e:\n                    errors.append(f\"Terminal cleanup error: {e}\")\n                finally:\n                    self.terminal = None\n\n            if self.container:\n                try:\n                    await asyncio.to_thread(self.container.stop, timeout=5)\n                except Exception as e:\n                    errors.append(f\"Container stop error: {e}\")\n\n                try:\n                    await asyncio.to_thread(self.container.remove, force=True)\n                except Exception as e:\n                    errors.append(f\"Container remove error: {e}\")\n                finally:\n                    self.container = None\n\n        except Exception as e:\n            errors.append(f\"General cleanup error: {e}\")\n\n        if errors:\n            print(f\"Warning: Errors during cleanup: {', '.join(errors)}\")\n\n    async def __aenter__(self) -> \"DockerSandbox\":\n        \"\"\"Async context manager entry.\"\"\"\n        return await self.create()\n\n    async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:\n        \"\"\"Async context manager exit.\"\"\"\n        await self.cleanup()\n"
  },
  {
    "path": "app/sandbox/core/terminal.py",
    "content": "\"\"\"\nAsynchronous Docker Terminal\n\nThis module provides asynchronous terminal functionality for Docker containers,\nallowing interactive command execution with timeout control.\n\"\"\"\n\nimport asyncio\nimport re\nimport socket\nfrom typing import Dict, Optional, Tuple, Union\n\nimport docker\nfrom docker import APIClient\nfrom docker.errors import APIError\nfrom docker.models.containers import Container\n\n\nclass DockerSession:\n    def __init__(self, container_id: str) -> None:\n        \"\"\"Initializes a Docker session.\n\n        Args:\n            container_id: ID of the Docker container.\n        \"\"\"\n        self.api = APIClient()\n        self.container_id = container_id\n        self.exec_id = None\n        self.socket = None\n\n    async def create(self, working_dir: str, env_vars: Dict[str, str]) -> None:\n        \"\"\"Creates an interactive session with the container.\n\n        Args:\n            working_dir: Working directory inside the container.\n            env_vars: Environment variables to set.\n\n        Raises:\n            RuntimeError: If socket connection fails.\n        \"\"\"\n        startup_command = [\n            \"bash\",\n            \"-c\",\n            f\"cd {working_dir} && \"\n            \"PROMPT_COMMAND='' \"\n            \"PS1='$ ' \"\n            \"exec bash --norc --noprofile\",\n        ]\n\n        exec_data = self.api.exec_create(\n            self.container_id,\n            startup_command,\n            stdin=True,\n            tty=True,\n            stdout=True,\n            stderr=True,\n            privileged=True,\n            user=\"root\",\n            environment={**env_vars, \"TERM\": \"dumb\", \"PS1\": \"$ \", \"PROMPT_COMMAND\": \"\"},\n        )\n        self.exec_id = exec_data[\"Id\"]\n\n        socket_data = self.api.exec_start(\n            self.exec_id, socket=True, tty=True, stream=True, demux=True\n        )\n\n        if hasattr(socket_data, \"_sock\"):\n            self.socket = socket_data._sock\n            self.socket.setblocking(False)\n        else:\n            raise RuntimeError(\"Failed to get socket connection\")\n\n        await self._read_until_prompt()\n\n    async def close(self) -> None:\n        \"\"\"Cleans up session resources.\n\n        1. Sends exit command\n        2. Closes socket connection\n        3. Checks and cleans up exec instance\n        \"\"\"\n        try:\n            if self.socket:\n                # Send exit command to close bash session\n                try:\n                    self.socket.sendall(b\"exit\\n\")\n                    # Allow time for command execution\n                    await asyncio.sleep(0.1)\n                except:\n                    pass  # Ignore sending errors, continue cleanup\n\n                # Close socket connection\n                try:\n                    self.socket.shutdown(socket.SHUT_RDWR)\n                except:\n                    pass  # Some platforms may not support shutdown\n\n                self.socket.close()\n                self.socket = None\n\n            if self.exec_id:\n                try:\n                    # Check exec instance status\n                    exec_inspect = self.api.exec_inspect(self.exec_id)\n                    if exec_inspect.get(\"Running\", False):\n                        # If still running, wait for it to complete\n                        await asyncio.sleep(0.5)\n                except:\n                    pass  # Ignore inspection errors, continue cleanup\n\n                self.exec_id = None\n\n        except Exception as e:\n            # Log error but don't raise, ensure cleanup continues\n            print(f\"Warning: Error during session cleanup: {e}\")\n\n    async def _read_until_prompt(self) -> str:\n        \"\"\"Reads output until prompt is found.\n\n        Returns:\n            String containing output up to the prompt.\n\n        Raises:\n            socket.error: If socket communication fails.\n        \"\"\"\n        buffer = b\"\"\n        while b\"$ \" not in buffer:\n            try:\n                chunk = self.socket.recv(4096)\n                if chunk:\n                    buffer += chunk\n            except socket.error as e:\n                if e.errno == socket.EWOULDBLOCK:\n                    await asyncio.sleep(0.1)\n                    continue\n                raise\n        return buffer.decode(\"utf-8\")\n\n    async def execute(self, command: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Executes a command and returns cleaned output.\n\n        Args:\n            command: Shell command to execute.\n            timeout: Maximum execution time in seconds.\n\n        Returns:\n            Command output as string with prompt markers removed.\n\n        Raises:\n            RuntimeError: If session not initialized or execution fails.\n            TimeoutError: If command execution exceeds timeout.\n        \"\"\"\n        if not self.socket:\n            raise RuntimeError(\"Session not initialized\")\n\n        try:\n            # Sanitize command to prevent shell injection\n            sanitized_command = self._sanitize_command(command)\n            full_command = f\"{sanitized_command}\\necho $?\\n\"\n            self.socket.sendall(full_command.encode())\n\n            async def read_output() -> str:\n                buffer = b\"\"\n                result_lines = []\n                command_sent = False\n\n                while True:\n                    try:\n                        chunk = self.socket.recv(4096)\n                        if not chunk:\n                            break\n\n                        buffer += chunk\n                        lines = buffer.split(b\"\\n\")\n\n                        buffer = lines[-1]\n                        lines = lines[:-1]\n\n                        for line in lines:\n                            line = line.rstrip(b\"\\r\")\n\n                            if not command_sent:\n                                command_sent = True\n                                continue\n\n                            if line.strip() == b\"echo $?\" or line.strip().isdigit():\n                                continue\n\n                            if line.strip():\n                                result_lines.append(line)\n\n                        if buffer.endswith(b\"$ \"):\n                            break\n\n                    except socket.error as e:\n                        if e.errno == socket.EWOULDBLOCK:\n                            await asyncio.sleep(0.1)\n                            continue\n                        raise\n\n                output = b\"\\n\".join(result_lines).decode(\"utf-8\")\n                output = re.sub(r\"\\n\\$ echo \\$\\$?.*$\", \"\", output)\n\n                return output\n\n            if timeout:\n                result = await asyncio.wait_for(read_output(), timeout)\n            else:\n                result = await read_output()\n\n            return result.strip()\n\n        except asyncio.TimeoutError:\n            raise TimeoutError(f\"Command execution timed out after {timeout} seconds\")\n        except Exception as e:\n            raise RuntimeError(f\"Failed to execute command: {e}\")\n\n    def _sanitize_command(self, command: str) -> str:\n        \"\"\"Sanitizes the command string to prevent shell injection.\n\n        Args:\n            command: Raw command string.\n\n        Returns:\n            Sanitized command string.\n\n        Raises:\n            ValueError: If command contains potentially dangerous patterns.\n        \"\"\"\n\n        # Additional checks for specific risky commands\n        risky_commands = [\n            \"rm -rf /\",\n            \"rm -rf /*\",\n            \"mkfs\",\n            \"dd if=/dev/zero\",\n            \":(){:|:&};:\",\n            \"chmod -R 777 /\",\n            \"chown -R\",\n        ]\n\n        for risky in risky_commands:\n            if risky in command.lower():\n                raise ValueError(\n                    f\"Command contains potentially dangerous operation: {risky}\"\n                )\n\n        return command\n\n\nclass AsyncDockerizedTerminal:\n    def __init__(\n        self,\n        container: Union[str, Container],\n        working_dir: str = \"/workspace\",\n        env_vars: Optional[Dict[str, str]] = None,\n        default_timeout: int = 60,\n    ) -> None:\n        \"\"\"Initializes an asynchronous terminal for Docker containers.\n\n        Args:\n            container: Docker container ID or Container object.\n            working_dir: Working directory inside the container.\n            env_vars: Environment variables to set.\n            default_timeout: Default command execution timeout in seconds.\n        \"\"\"\n        self.client = docker.from_env()\n        self.container = (\n            container\n            if isinstance(container, Container)\n            else self.client.containers.get(container)\n        )\n        self.working_dir = working_dir\n        self.env_vars = env_vars or {}\n        self.default_timeout = default_timeout\n        self.session = None\n\n    async def init(self) -> None:\n        \"\"\"Initializes the terminal environment.\n\n        Ensures working directory exists and creates an interactive session.\n\n        Raises:\n            RuntimeError: If initialization fails.\n        \"\"\"\n        await self._ensure_workdir()\n\n        self.session = DockerSession(self.container.id)\n        await self.session.create(self.working_dir, self.env_vars)\n\n    async def _ensure_workdir(self) -> None:\n        \"\"\"Ensures working directory exists in container.\n\n        Raises:\n            RuntimeError: If directory creation fails.\n        \"\"\"\n        try:\n            await self._exec_simple(f\"mkdir -p {self.working_dir}\")\n        except APIError as e:\n            raise RuntimeError(f\"Failed to create working directory: {e}\")\n\n    async def _exec_simple(self, cmd: str) -> Tuple[int, str]:\n        \"\"\"Executes a simple command using Docker's exec_run.\n\n        Args:\n            cmd: Command to execute.\n\n        Returns:\n            Tuple of (exit_code, output).\n        \"\"\"\n        result = await asyncio.to_thread(\n            self.container.exec_run, cmd, environment=self.env_vars\n        )\n        return result.exit_code, result.output.decode(\"utf-8\")\n\n    async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Runs a command in the container with timeout.\n\n        Args:\n            cmd: Shell command to execute.\n            timeout: Maximum execution time in seconds.\n\n        Returns:\n            Command output as string.\n\n        Raises:\n            RuntimeError: If terminal not initialized.\n        \"\"\"\n        if not self.session:\n            raise RuntimeError(\"Terminal not initialized\")\n\n        return await self.session.execute(cmd, timeout=timeout or self.default_timeout)\n\n    async def close(self) -> None:\n        \"\"\"Closes the terminal session.\"\"\"\n        if self.session:\n            await self.session.close()\n\n    async def __aenter__(self) -> \"AsyncDockerizedTerminal\":\n        \"\"\"Async context manager entry.\"\"\"\n        await self.init()\n        return self\n\n    async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:\n        \"\"\"Async context manager exit.\"\"\"\n        await self.close()\n"
  },
  {
    "path": "app/schema.py",
    "content": "from enum import Enum\nfrom typing import Any, List, Literal, Optional, Union\n\nfrom pydantic import BaseModel, Field\n\n\nclass Role(str, Enum):\n    \"\"\"Message role options\"\"\"\n\n    SYSTEM = \"system\"\n    USER = \"user\"\n    ASSISTANT = \"assistant\"\n    TOOL = \"tool\"\n\n\nROLE_VALUES = tuple(role.value for role in Role)\nROLE_TYPE = Literal[ROLE_VALUES]  # type: ignore\n\n\nclass ToolChoice(str, Enum):\n    \"\"\"Tool choice options\"\"\"\n\n    NONE = \"none\"\n    AUTO = \"auto\"\n    REQUIRED = \"required\"\n\n\nTOOL_CHOICE_VALUES = tuple(choice.value for choice in ToolChoice)\nTOOL_CHOICE_TYPE = Literal[TOOL_CHOICE_VALUES]  # type: ignore\n\n\nclass AgentState(str, Enum):\n    \"\"\"Agent execution states\"\"\"\n\n    IDLE = \"IDLE\"\n    RUNNING = \"RUNNING\"\n    FINISHED = \"FINISHED\"\n    ERROR = \"ERROR\"\n\n\nclass Function(BaseModel):\n    name: str\n    arguments: str\n\n\nclass ToolCall(BaseModel):\n    \"\"\"Represents a tool/function call in a message\"\"\"\n\n    id: str\n    type: str = \"function\"\n    function: Function\n\n\nclass Message(BaseModel):\n    \"\"\"Represents a chat message in the conversation\"\"\"\n\n    role: ROLE_TYPE = Field(...)  # type: ignore\n    content: Optional[str] = Field(default=None)\n    tool_calls: Optional[List[ToolCall]] = Field(default=None)\n    name: Optional[str] = Field(default=None)\n    tool_call_id: Optional[str] = Field(default=None)\n    base64_image: Optional[str] = Field(default=None)\n\n    def __add__(self, other) -> List[\"Message\"]:\n        \"\"\"支持 Message + list 或 Message + Message 的操作\"\"\"\n        if isinstance(other, list):\n            return [self] + other\n        elif isinstance(other, Message):\n            return [self, other]\n        else:\n            raise TypeError(\n                f\"unsupported operand type(s) for +: '{type(self).__name__}' and '{type(other).__name__}'\"\n            )\n\n    def __radd__(self, other) -> List[\"Message\"]:\n        \"\"\"支持 list + Message 的操作\"\"\"\n        if isinstance(other, list):\n            return other + [self]\n        else:\n            raise TypeError(\n                f\"unsupported operand type(s) for +: '{type(other).__name__}' and '{type(self).__name__}'\"\n            )\n\n    def to_dict(self) -> dict:\n        \"\"\"Convert message to dictionary format\"\"\"\n        message = {\"role\": self.role}\n        if self.content is not None:\n            message[\"content\"] = self.content\n        if self.tool_calls is not None:\n            message[\"tool_calls\"] = [tool_call.dict() for tool_call in self.tool_calls]\n        if self.name is not None:\n            message[\"name\"] = self.name\n        if self.tool_call_id is not None:\n            message[\"tool_call_id\"] = self.tool_call_id\n        if self.base64_image is not None:\n            message[\"base64_image\"] = self.base64_image\n        return message\n\n    @classmethod\n    def user_message(\n        cls, content: str, base64_image: Optional[str] = None\n    ) -> \"Message\":\n        \"\"\"Create a user message\"\"\"\n        return cls(role=Role.USER, content=content, base64_image=base64_image)\n\n    @classmethod\n    def system_message(cls, content: str) -> \"Message\":\n        \"\"\"Create a system message\"\"\"\n        return cls(role=Role.SYSTEM, content=content)\n\n    @classmethod\n    def assistant_message(\n        cls, content: Optional[str] = None, base64_image: Optional[str] = None\n    ) -> \"Message\":\n        \"\"\"Create an assistant message\"\"\"\n        return cls(role=Role.ASSISTANT, content=content, base64_image=base64_image)\n\n    @classmethod\n    def tool_message(\n        cls, content: str, name, tool_call_id: str, base64_image: Optional[str] = None\n    ) -> \"Message\":\n        \"\"\"Create a tool message\"\"\"\n        return cls(\n            role=Role.TOOL,\n            content=content,\n            name=name,\n            tool_call_id=tool_call_id,\n            base64_image=base64_image,\n        )\n\n    @classmethod\n    def from_tool_calls(\n        cls,\n        tool_calls: List[Any],\n        content: Union[str, List[str]] = \"\",\n        base64_image: Optional[str] = None,\n        **kwargs,\n    ):\n        \"\"\"Create ToolCallsMessage from raw tool calls.\n\n        Args:\n            tool_calls: Raw tool calls from LLM\n            content: Optional message content\n            base64_image: Optional base64 encoded image\n        \"\"\"\n        formatted_calls = [\n            {\"id\": call.id, \"function\": call.function.model_dump(), \"type\": \"function\"}\n            for call in tool_calls\n        ]\n        return cls(\n            role=Role.ASSISTANT,\n            content=content,\n            tool_calls=formatted_calls,\n            base64_image=base64_image,\n            **kwargs,\n        )\n\n\nclass Memory(BaseModel):\n    messages: List[Message] = Field(default_factory=list)\n    max_messages: int = Field(default=100)\n\n    def add_message(self, message: Message) -> None:\n        \"\"\"Add a message to memory\"\"\"\n        self.messages.append(message)\n        # Optional: Implement message limit\n        if len(self.messages) > self.max_messages:\n            self.messages = self.messages[-self.max_messages :]\n\n    def add_messages(self, messages: List[Message]) -> None:\n        \"\"\"Add multiple messages to memory\"\"\"\n        self.messages.extend(messages)\n        # Optional: Implement message limit\n        if len(self.messages) > self.max_messages:\n            self.messages = self.messages[-self.max_messages :]\n\n    def clear(self) -> None:\n        \"\"\"Clear all messages\"\"\"\n        self.messages.clear()\n\n    def get_recent_messages(self, n: int) -> List[Message]:\n        \"\"\"Get n most recent messages\"\"\"\n        return self.messages[-n:]\n\n    def to_dict_list(self) -> List[dict]:\n        \"\"\"Convert messages to list of dicts\"\"\"\n        return [msg.to_dict() for msg in self.messages]\n"
  },
  {
    "path": "app/tool/__init__.py",
    "content": "from app.tool.base import BaseTool\nfrom app.tool.bash import Bash\nfrom app.tool.browser_use_tool import BrowserUseTool\nfrom app.tool.crawl4ai import Crawl4aiTool\nfrom app.tool.create_chat_completion import CreateChatCompletion\nfrom app.tool.planning import PlanningTool\nfrom app.tool.str_replace_editor import StrReplaceEditor\nfrom app.tool.terminate import Terminate\nfrom app.tool.tool_collection import ToolCollection\nfrom app.tool.web_search import WebSearch\n\n\n__all__ = [\n    \"BaseTool\",\n    \"Bash\",\n    \"BrowserUseTool\",\n    \"Terminate\",\n    \"StrReplaceEditor\",\n    \"WebSearch\",\n    \"ToolCollection\",\n    \"CreateChatCompletion\",\n    \"PlanningTool\",\n    \"Crawl4aiTool\",\n]\n"
  },
  {
    "path": "app/tool/ask_human.py",
    "content": "from app.tool import BaseTool\n\n\nclass AskHuman(BaseTool):\n    \"\"\"Add a tool to ask human for help.\"\"\"\n\n    name: str = \"ask_human\"\n    description: str = \"Use this tool to ask human for help.\"\n    parameters: str = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"inquire\": {\n                \"type\": \"string\",\n                \"description\": \"The question you want to ask human.\",\n            }\n        },\n        \"required\": [\"inquire\"],\n    }\n\n    async def execute(self, inquire: str) -> str:\n        return input(f\"\"\"Bot: {inquire}\\n\\nYou: \"\"\").strip()\n"
  },
  {
    "path": "app/tool/base.py",
    "content": "import json\nfrom abc import ABC, abstractmethod\nfrom typing import Any, Dict, Optional, Union\n\nfrom pydantic import BaseModel, Field\n\nfrom app.utils.logger import logger\n\n\n# class BaseTool(ABC, BaseModel):\n#     name: str\n#     description: str\n#     parameters: Optional[dict] = None\n\n#     class Config:\n#         arbitrary_types_allowed = True\n\n#     async def __call__(self, **kwargs) -> Any:\n#         \"\"\"Execute the tool with given parameters.\"\"\"\n#         return await self.execute(**kwargs)\n\n#     @abstractmethod\n#     async def execute(self, **kwargs) -> Any:\n#         \"\"\"Execute the tool with given parameters.\"\"\"\n\n#     def to_param(self) -> Dict:\n#         \"\"\"Convert tool to function call format.\"\"\"\n#         return {\n#             \"type\": \"function\",\n#             \"function\": {\n#                 \"name\": self.name,\n#                 \"description\": self.description,\n#                 \"parameters\": self.parameters,\n#             },\n#         }\n\n\nclass ToolResult(BaseModel):\n    \"\"\"Represents the result of a tool execution.\"\"\"\n\n    output: Any = Field(default=None)\n    error: Optional[str] = Field(default=None)\n    base64_image: Optional[str] = Field(default=None)\n    system: Optional[str] = Field(default=None)\n\n    class Config:\n        arbitrary_types_allowed = True\n\n    def __bool__(self):\n        return any(getattr(self, field) for field in self.__fields__)\n\n    def __add__(self, other: \"ToolResult\"):\n        def combine_fields(\n            field: Optional[str], other_field: Optional[str], concatenate: bool = True\n        ):\n            if field and other_field:\n                if concatenate:\n                    return field + other_field\n                raise ValueError(\"Cannot combine tool results\")\n            return field or other_field\n\n        return ToolResult(\n            output=combine_fields(self.output, other.output),\n            error=combine_fields(self.error, other.error),\n            base64_image=combine_fields(self.base64_image, other.base64_image, False),\n            system=combine_fields(self.system, other.system),\n        )\n\n    def __str__(self):\n        return f\"Error: {self.error}\" if self.error else self.output\n\n    def replace(self, **kwargs):\n        \"\"\"Returns a new ToolResult with the given fields replaced.\"\"\"\n        # return self.copy(update=kwargs)\n        return type(self)(**{**self.dict(), **kwargs})\n\n\nclass BaseTool(ABC, BaseModel):\n    \"\"\"Consolidated base class for all tools combining BaseModel and Tool functionality.\n\n    Provides:\n    - Pydantic model validation\n    - Schema registration\n    - Standardized result handling\n    - Abstract execution interface\n\n    Attributes:\n        name (str): Tool name\n        description (str): Tool description\n        parameters (dict): Tool parameters schema\n        _schemas (Dict[str, List[ToolSchema]]): Registered method schemas\n    \"\"\"\n\n    name: str\n    description: str\n    parameters: Optional[dict] = None\n    # _schemas: Dict[str, List[ToolSchema]] = {}\n\n    class Config:\n        arbitrary_types_allowed = True\n        underscore_attrs_are_private = False\n\n    # def __init__(self, **data):\n    #     \"\"\"Initialize tool with model validation and schema registration.\"\"\"\n    #     super().__init__(**data)\n    #     logger.debug(f\"Initializing tool class: {self.__class__.__name__}\")\n    #     self._register_schemas()\n\n    # def _register_schemas(self):\n    #     \"\"\"Register schemas from all decorated methods.\"\"\"\n    #     for name, method in inspect.getmembers(self, predicate=inspect.ismethod):\n    #         if hasattr(method, 'tool_schemas'):\n    #             self._schemas[name] = method.tool_schemas\n    #             logger.debug(f\"Registered schemas for method '{name}' in {self.__class__.__name__}\")\n\n    async def __call__(self, **kwargs) -> Any:\n        \"\"\"Execute the tool with given parameters.\"\"\"\n        return await self.execute(**kwargs)\n\n    @abstractmethod\n    async def execute(self, **kwargs) -> Any:\n        \"\"\"Execute the tool with given parameters.\"\"\"\n\n    def to_param(self) -> Dict:\n        \"\"\"Convert tool to function call format.\n\n        Returns:\n            Dictionary with tool metadata in OpenAI function calling format\n        \"\"\"\n        return {\n            \"type\": \"function\",\n            \"function\": {\n                \"name\": self.name,\n                \"description\": self.description,\n                \"parameters\": self.parameters,\n            },\n        }\n\n    # def get_schemas(self) -> Dict[str, List[ToolSchema]]:\n    #     \"\"\"Get all registered tool schemas.\n\n    #     Returns:\n    #         Dict mapping method names to their schema definitions\n    #     \"\"\"\n    #     return self._schemas\n\n    def success_response(self, data: Union[Dict[str, Any], str]) -> ToolResult:\n        \"\"\"Create a successful tool result.\n\n        Args:\n            data: Result data (dictionary or string)\n\n        Returns:\n            ToolResult with success=True and formatted output\n        \"\"\"\n        if isinstance(data, str):\n            text = data\n        else:\n            text = json.dumps(data, indent=2)\n        logger.debug(f\"Created success response for {self.__class__.__name__}\")\n        return ToolResult(output=text)\n\n    def fail_response(self, msg: str) -> ToolResult:\n        \"\"\"Create a failed tool result.\n\n        Args:\n            msg: Error message describing the failure\n\n        Returns:\n            ToolResult with success=False and error message\n        \"\"\"\n        logger.debug(f\"Tool {self.__class__.__name__} returned failed result: {msg}\")\n        return ToolResult(error=msg)\n\n\nclass CLIResult(ToolResult):\n    \"\"\"A ToolResult that can be rendered as a CLI output.\"\"\"\n\n\nclass ToolFailure(ToolResult):\n    \"\"\"A ToolResult that represents a failure.\"\"\"\n"
  },
  {
    "path": "app/tool/bash.py",
    "content": "import asyncio\nimport os\nfrom typing import Optional\n\nfrom app.exceptions import ToolError\nfrom app.tool.base import BaseTool, CLIResult\n\n\n_BASH_DESCRIPTION = \"\"\"Execute a bash command in the terminal.\n* 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 &`.\n* 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.\n* 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.\n\"\"\"\n\n\nclass _BashSession:\n    \"\"\"A session of a bash shell.\"\"\"\n\n    _started: bool\n    _process: asyncio.subprocess.Process\n\n    command: str = \"/bin/bash\"\n    _output_delay: float = 0.2  # seconds\n    _timeout: float = 120.0  # seconds\n    _sentinel: str = \"<<exit>>\"\n\n    def __init__(self):\n        self._started = False\n        self._timed_out = False\n\n    async def start(self):\n        if self._started:\n            return\n\n        self._process = await asyncio.create_subprocess_shell(\n            self.command,\n            preexec_fn=os.setsid,\n            shell=True,\n            bufsize=0,\n            stdin=asyncio.subprocess.PIPE,\n            stdout=asyncio.subprocess.PIPE,\n            stderr=asyncio.subprocess.PIPE,\n        )\n\n        self._started = True\n\n    def stop(self):\n        \"\"\"Terminate the bash shell.\"\"\"\n        if not self._started:\n            raise ToolError(\"Session has not started.\")\n        if self._process.returncode is not None:\n            return\n        self._process.terminate()\n\n    async def run(self, command: str):\n        \"\"\"Execute a command in the bash shell.\"\"\"\n        if not self._started:\n            raise ToolError(\"Session has not started.\")\n        if self._process.returncode is not None:\n            return CLIResult(\n                system=\"tool must be restarted\",\n                error=f\"bash has exited with returncode {self._process.returncode}\",\n            )\n        if self._timed_out:\n            raise ToolError(\n                f\"timed out: bash has not returned in {self._timeout} seconds and must be restarted\",\n            )\n\n        # we know these are not None because we created the process with PIPEs\n        assert self._process.stdin\n        assert self._process.stdout\n        assert self._process.stderr\n\n        # send command to the process\n        self._process.stdin.write(\n            command.encode() + f\"; echo '{self._sentinel}'\\n\".encode()\n        )\n        await self._process.stdin.drain()\n\n        # read output from the process, until the sentinel is found\n        try:\n            async with asyncio.timeout(self._timeout):\n                while True:\n                    await asyncio.sleep(self._output_delay)\n                    # if we read directly from stdout/stderr, it will wait forever for\n                    # EOF. use the StreamReader buffer directly instead.\n                    output = (\n                        self._process.stdout._buffer.decode()\n                    )  # pyright: ignore[reportAttributeAccessIssue]\n                    if self._sentinel in output:\n                        # strip the sentinel and break\n                        output = output[: output.index(self._sentinel)]\n                        break\n        except asyncio.TimeoutError:\n            self._timed_out = True\n            raise ToolError(\n                f\"timed out: bash has not returned in {self._timeout} seconds and must be restarted\",\n            ) from None\n\n        if output.endswith(\"\\n\"):\n            output = output[:-1]\n\n        error = (\n            self._process.stderr._buffer.decode()\n        )  # pyright: ignore[reportAttributeAccessIssue]\n        if error.endswith(\"\\n\"):\n            error = error[:-1]\n\n        # clear the buffers so that the next output can be read correctly\n        self._process.stdout._buffer.clear()  # pyright: ignore[reportAttributeAccessIssue]\n        self._process.stderr._buffer.clear()  # pyright: ignore[reportAttributeAccessIssue]\n\n        return CLIResult(output=output, error=error)\n\n\nclass Bash(BaseTool):\n    \"\"\"A tool for executing bash commands\"\"\"\n\n    name: str = \"bash\"\n    description: str = _BASH_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"command\": {\n                \"type\": \"string\",\n                \"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.\",\n            },\n        },\n        \"required\": [\"command\"],\n    }\n\n    _session: Optional[_BashSession] = None\n\n    async def execute(\n        self, command: str | None = None, restart: bool = False, **kwargs\n    ) -> CLIResult:\n        if restart:\n            if self._session:\n                self._session.stop()\n            self._session = _BashSession()\n            await self._session.start()\n\n            return CLIResult(system=\"tool has been restarted.\")\n\n        if self._session is None:\n            self._session = _BashSession()\n            await self._session.start()\n\n        if command is not None:\n            return await self._session.run(command)\n\n        raise ToolError(\"no command provided.\")\n\n\nif __name__ == \"__main__\":\n    bash = Bash()\n    rst = asyncio.run(bash.execute(\"ls -l\"))\n    print(rst)\n"
  },
  {
    "path": "app/tool/browser_use_tool.py",
    "content": "import asyncio\nimport base64\nimport json\nfrom typing import Generic, Optional, TypeVar\n\nfrom browser_use import Browser as BrowserUseBrowser\nfrom browser_use import BrowserConfig\nfrom browser_use.browser.context import BrowserContext, BrowserContextConfig\nfrom browser_use.dom.service import DomService\nfrom pydantic import Field, field_validator\nfrom pydantic_core.core_schema import ValidationInfo\n\nfrom app.config import config\nfrom app.llm import LLM\nfrom app.tool.base import BaseTool, ToolResult\nfrom app.tool.web_search import WebSearch\n\n\n_BROWSER_DESCRIPTION = \"\"\"\\\nA powerful browser automation tool that allows interaction with web pages through various actions.\n* This tool provides commands for controlling a browser session, navigating web pages, and extracting information\n* It maintains state across calls, keeping the browser session alive until explicitly closed\n* Use this when you need to browse websites, fill forms, click buttons, extract content, or perform web searches\n* Each action requires specific parameters as defined in the tool's dependencies\n\nKey capabilities include:\n* Navigation: Go to specific URLs, go back, search the web, or refresh pages\n* Interaction: Click elements, input text, select from dropdowns, send keyboard commands\n* Scrolling: Scroll up/down by pixel amount or scroll to specific text\n* Content extraction: Extract and analyze content from web pages based on specific goals\n* Tab management: Switch between tabs, open new tabs, or close tabs\n\nNote: When using element indices, refer to the numbered elements shown in the current browser state.\n\"\"\"\n\nContext = TypeVar(\"Context\")\n\n\nclass BrowserUseTool(BaseTool, Generic[Context]):\n    name: str = \"browser_use\"\n    description: str = _BROWSER_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\n                    \"go_to_url\",\n                    \"click_element\",\n                    \"input_text\",\n                    \"scroll_down\",\n                    \"scroll_up\",\n                    \"scroll_to_text\",\n                    \"send_keys\",\n                    \"get_dropdown_options\",\n                    \"select_dropdown_option\",\n                    \"go_back\",\n                    \"web_search\",\n                    \"wait\",\n                    \"extract_content\",\n                    \"switch_tab\",\n                    \"open_tab\",\n                    \"close_tab\",\n                ],\n                \"description\": \"The browser action to perform\",\n            },\n            \"url\": {\n                \"type\": \"string\",\n                \"description\": \"URL for 'go_to_url' or 'open_tab' actions\",\n            },\n            \"index\": {\n                \"type\": \"integer\",\n                \"description\": \"Element index for 'click_element', 'input_text', 'get_dropdown_options', or 'select_dropdown_option' actions\",\n            },\n            \"text\": {\n                \"type\": \"string\",\n                \"description\": \"Text for 'input_text', 'scroll_to_text', or 'select_dropdown_option' actions\",\n            },\n            \"scroll_amount\": {\n                \"type\": \"integer\",\n                \"description\": \"Pixels to scroll (positive for down, negative for up) for 'scroll_down' or 'scroll_up' actions\",\n            },\n            \"tab_id\": {\n                \"type\": \"integer\",\n                \"description\": \"Tab ID for 'switch_tab' action\",\n            },\n            \"query\": {\n                \"type\": \"string\",\n                \"description\": \"Search query for 'web_search' action\",\n            },\n            \"goal\": {\n                \"type\": \"string\",\n                \"description\": \"Extraction goal for 'extract_content' action\",\n            },\n            \"keys\": {\n                \"type\": \"string\",\n                \"description\": \"Keys to send for 'send_keys' action\",\n            },\n            \"seconds\": {\n                \"type\": \"integer\",\n                \"description\": \"Seconds to wait for 'wait' action\",\n            },\n        },\n        \"required\": [\"action\"],\n        \"dependencies\": {\n            \"go_to_url\": [\"url\"],\n            \"click_element\": [\"index\"],\n            \"input_text\": [\"index\", \"text\"],\n            \"switch_tab\": [\"tab_id\"],\n            \"open_tab\": [\"url\"],\n            \"scroll_down\": [\"scroll_amount\"],\n            \"scroll_up\": [\"scroll_amount\"],\n            \"scroll_to_text\": [\"text\"],\n            \"send_keys\": [\"keys\"],\n            \"get_dropdown_options\": [\"index\"],\n            \"select_dropdown_option\": [\"index\", \"text\"],\n            \"go_back\": [],\n            \"web_search\": [\"query\"],\n            \"wait\": [\"seconds\"],\n            \"extract_content\": [\"goal\"],\n        },\n    }\n\n    lock: asyncio.Lock = Field(default_factory=asyncio.Lock)\n    browser: Optional[BrowserUseBrowser] = Field(default=None, exclude=True)\n    context: Optional[BrowserContext] = Field(default=None, exclude=True)\n    dom_service: Optional[DomService] = Field(default=None, exclude=True)\n    web_search_tool: WebSearch = Field(default_factory=WebSearch, exclude=True)\n\n    # Context for generic functionality\n    tool_context: Optional[Context] = Field(default=None, exclude=True)\n\n    llm: Optional[LLM] = Field(default_factory=LLM)\n\n    @field_validator(\"parameters\", mode=\"before\")\n    def validate_parameters(cls, v: dict, info: ValidationInfo) -> dict:\n        if not v:\n            raise ValueError(\"Parameters cannot be empty\")\n        return v\n\n    async def _ensure_browser_initialized(self) -> BrowserContext:\n        \"\"\"Ensure browser and context are initialized.\"\"\"\n        if self.browser is None:\n            browser_config_kwargs = {\"headless\": False, \"disable_security\": True}\n\n            if config.browser_config:\n                from browser_use.browser.browser import ProxySettings\n\n                # handle proxy settings.\n                if config.browser_config.proxy and config.browser_config.proxy.server:\n                    browser_config_kwargs[\"proxy\"] = ProxySettings(\n                        server=config.browser_config.proxy.server,\n                        username=config.browser_config.proxy.username,\n                        password=config.browser_config.proxy.password,\n                    )\n\n                browser_attrs = [\n                    \"headless\",\n                    \"disable_security\",\n                    \"extra_chromium_args\",\n                    \"chrome_instance_path\",\n                    \"wss_url\",\n                    \"cdp_url\",\n                ]\n\n                for attr in browser_attrs:\n                    value = getattr(config.browser_config, attr, None)\n                    if value is not None:\n                        if not isinstance(value, list) or value:\n                            browser_config_kwargs[attr] = value\n\n            self.browser = BrowserUseBrowser(BrowserConfig(**browser_config_kwargs))\n\n        if self.context is None:\n            context_config = BrowserContextConfig()\n\n            # if there is context config in the config, use it.\n            if (\n                config.browser_config\n                and hasattr(config.browser_config, \"new_context_config\")\n                and config.browser_config.new_context_config\n            ):\n                context_config = config.browser_config.new_context_config\n\n            self.context = await self.browser.new_context(context_config)\n            self.dom_service = DomService(await self.context.get_current_page())\n\n        return self.context\n\n    async def execute(\n        self,\n        action: str,\n        url: Optional[str] = None,\n        index: Optional[int] = None,\n        text: Optional[str] = None,\n        scroll_amount: Optional[int] = None,\n        tab_id: Optional[int] = None,\n        query: Optional[str] = None,\n        goal: Optional[str] = None,\n        keys: Optional[str] = None,\n        seconds: Optional[int] = None,\n        **kwargs,\n    ) -> ToolResult:\n        \"\"\"\n        Execute a specified browser action.\n\n        Args:\n            action: The browser action to perform\n            url: URL for navigation or new tab\n            index: Element index for click or input actions\n            text: Text for input action or search query\n            scroll_amount: Pixels to scroll for scroll action\n            tab_id: Tab ID for switch_tab action\n            query: Search query for Google search\n            goal: Extraction goal for content extraction\n            keys: Keys to send for keyboard actions\n            seconds: Seconds to wait\n            **kwargs: Additional arguments\n\n        Returns:\n            ToolResult with the action's output or error\n        \"\"\"\n        async with self.lock:\n            try:\n                context = await self._ensure_browser_initialized()\n\n                # Get max content length from config\n                max_content_length = getattr(\n                    config.browser_config, \"max_content_length\", 2000\n                )\n\n                # Navigation actions\n                if action == \"go_to_url\":\n                    if not url:\n                        return ToolResult(\n                            error=\"URL is required for 'go_to_url' action\"\n                        )\n                    page = await context.get_current_page()\n                    await page.goto(url)\n                    await page.wait_for_load_state()\n                    return ToolResult(output=f\"Navigated to {url}\")\n\n                elif action == \"go_back\":\n                    await context.go_back()\n                    return ToolResult(output=\"Navigated back\")\n\n                elif action == \"refresh\":\n                    await context.refresh_page()\n                    return ToolResult(output=\"Refreshed current page\")\n\n                elif action == \"web_search\":\n                    if not query:\n                        return ToolResult(\n                            error=\"Query is required for 'web_search' action\"\n                        )\n                    # Execute the web search and return results directly without browser navigation\n                    search_response = await self.web_search_tool.execute(\n                        query=query, fetch_content=True, num_results=1\n                    )\n                    # Navigate to the first search result\n                    first_search_result = search_response.results[0]\n                    url_to_navigate = first_search_result.url\n\n                    page = await context.get_current_page()\n                    await page.goto(url_to_navigate)\n                    await page.wait_for_load_state()\n\n                    return search_response\n\n                # Element interaction actions\n                elif action == \"click_element\":\n                    if index is None:\n                        return ToolResult(\n                            error=\"Index is required for 'click_element' action\"\n                        )\n                    element = await context.get_dom_element_by_index(index)\n                    if not element:\n                        return ToolResult(error=f\"Element with index {index} not found\")\n                    download_path = await context._click_element_node(element)\n                    output = f\"Clicked element at index {index}\"\n                    if download_path:\n                        output += f\" - Downloaded file to {download_path}\"\n                    return ToolResult(output=output)\n\n                elif action == \"input_text\":\n                    if index is None or not text:\n                        return ToolResult(\n                            error=\"Index and text are required for 'input_text' action\"\n                        )\n                    element = await context.get_dom_element_by_index(index)\n                    if not element:\n                        return ToolResult(error=f\"Element with index {index} not found\")\n                    await context._input_text_element_node(element, text)\n                    return ToolResult(\n                        output=f\"Input '{text}' into element at index {index}\"\n                    )\n\n                elif action == \"scroll_down\" or action == \"scroll_up\":\n                    direction = 1 if action == \"scroll_down\" else -1\n                    amount = (\n                        scroll_amount\n                        if scroll_amount is not None\n                        else context.config.browser_window_size[\"height\"]\n                    )\n                    await context.execute_javascript(\n                        f\"window.scrollBy(0, {direction * amount});\"\n                    )\n                    return ToolResult(\n                        output=f\"Scrolled {'down' if direction > 0 else 'up'} by {amount} pixels\"\n                    )\n\n                elif action == \"scroll_to_text\":\n                    if not text:\n                        return ToolResult(\n                            error=\"Text is required for 'scroll_to_text' action\"\n                        )\n                    page = await context.get_current_page()\n                    try:\n                        locator = page.get_by_text(text, exact=False)\n                        await locator.scroll_into_view_if_needed()\n                        return ToolResult(output=f\"Scrolled to text: '{text}'\")\n                    except Exception as e:\n                        return ToolResult(error=f\"Failed to scroll to text: {str(e)}\")\n\n                elif action == \"send_keys\":\n                    if not keys:\n                        return ToolResult(\n                            error=\"Keys are required for 'send_keys' action\"\n                        )\n                    page = await context.get_current_page()\n                    await page.keyboard.press(keys)\n                    return ToolResult(output=f\"Sent keys: {keys}\")\n\n                elif action == \"get_dropdown_options\":\n                    if index is None:\n                        return ToolResult(\n                            error=\"Index is required for 'get_dropdown_options' action\"\n                        )\n                    element = await context.get_dom_element_by_index(index)\n                    if not element:\n                        return ToolResult(error=f\"Element with index {index} not found\")\n                    page = await context.get_current_page()\n                    options = await page.evaluate(\n                        \"\"\"\n                        (xpath) => {\n                            const select = document.evaluate(xpath, document, null,\n                                XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\n                            if (!select) return null;\n                            return Array.from(select.options).map(opt => ({\n                                text: opt.text,\n                                value: opt.value,\n                                index: opt.index\n                            }));\n                        }\n                    \"\"\",\n                        element.xpath,\n                    )\n                    return ToolResult(output=f\"Dropdown options: {options}\")\n\n                elif action == \"select_dropdown_option\":\n                    if index is None or not text:\n                        return ToolResult(\n                            error=\"Index and text are required for 'select_dropdown_option' action\"\n                        )\n                    element = await context.get_dom_element_by_index(index)\n                    if not element:\n                        return ToolResult(error=f\"Element with index {index} not found\")\n                    page = await context.get_current_page()\n                    await page.select_option(element.xpath, label=text)\n                    return ToolResult(\n                        output=f\"Selected option '{text}' from dropdown at index {index}\"\n                    )\n\n                # Content extraction actions\n                elif action == \"extract_content\":\n                    if not goal:\n                        return ToolResult(\n                            error=\"Goal is required for 'extract_content' action\"\n                        )\n\n                    page = await context.get_current_page()\n                    import markdownify\n\n                    content = markdownify.markdownify(await page.content())\n\n                    prompt = f\"\"\"\\\nYour 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.\nExtraction goal: {goal}\n\nPage content:\n{content[:max_content_length]}\n\"\"\"\n                    messages = [{\"role\": \"system\", \"content\": prompt}]\n\n                    # Define extraction function schema\n                    extraction_function = {\n                        \"type\": \"function\",\n                        \"function\": {\n                            \"name\": \"extract_content\",\n                            \"description\": \"Extract specific information from a webpage based on a goal\",\n                            \"parameters\": {\n                                \"type\": \"object\",\n                                \"properties\": {\n                                    \"extracted_content\": {\n                                        \"type\": \"object\",\n                                        \"description\": \"The content extracted from the page according to the goal\",\n                                        \"properties\": {\n                                            \"text\": {\n                                                \"type\": \"string\",\n                                                \"description\": \"Text content extracted from the page\",\n                                            },\n                                            \"metadata\": {\n                                                \"type\": \"object\",\n                                                \"description\": \"Additional metadata about the extracted content\",\n                                                \"properties\": {\n                                                    \"source\": {\n                                                        \"type\": \"string\",\n                                                        \"description\": \"Source of the extracted content\",\n                                                    }\n                                                },\n                                            },\n                                        },\n                                    }\n                                },\n                                \"required\": [\"extracted_content\"],\n                            },\n                        },\n                    }\n\n                    # Use LLM to extract content with required function calling\n                    response = await self.llm.ask_tool(\n                        messages,\n                        tools=[extraction_function],\n                        tool_choice=\"required\",\n                    )\n\n                    if response and response.tool_calls:\n                        args = json.loads(response.tool_calls[0].function.arguments)\n                        extracted_content = args.get(\"extracted_content\", {})\n                        return ToolResult(\n                            output=f\"Extracted from page:\\n{extracted_content}\\n\"\n                        )\n\n                    return ToolResult(output=\"No content was extracted from the page.\")\n\n                # Tab management actions\n                elif action == \"switch_tab\":\n                    if tab_id is None:\n                        return ToolResult(\n                            error=\"Tab ID is required for 'switch_tab' action\"\n                        )\n                    await context.switch_to_tab(tab_id)\n                    page = await context.get_current_page()\n                    await page.wait_for_load_state()\n                    return ToolResult(output=f\"Switched to tab {tab_id}\")\n\n                elif action == \"open_tab\":\n                    if not url:\n                        return ToolResult(error=\"URL is required for 'open_tab' action\")\n                    await context.create_new_tab(url)\n                    return ToolResult(output=f\"Opened new tab with {url}\")\n\n                elif action == \"close_tab\":\n                    await context.close_current_tab()\n                    return ToolResult(output=\"Closed current tab\")\n\n                # Utility actions\n                elif action == \"wait\":\n                    seconds_to_wait = seconds if seconds is not None else 3\n                    await asyncio.sleep(seconds_to_wait)\n                    return ToolResult(output=f\"Waited for {seconds_to_wait} seconds\")\n\n                else:\n                    return ToolResult(error=f\"Unknown action: {action}\")\n\n            except Exception as e:\n                return ToolResult(error=f\"Browser action '{action}' failed: {str(e)}\")\n\n    async def get_current_state(\n        self, context: Optional[BrowserContext] = None\n    ) -> ToolResult:\n        \"\"\"\n        Get the current browser state as a ToolResult.\n        If context is not provided, uses self.context.\n        \"\"\"\n        try:\n            # Use provided context or fall back to self.context\n            ctx = context or self.context\n            if not ctx:\n                return ToolResult(error=\"Browser context not initialized\")\n\n            state = await ctx.get_state()\n\n            # Create a viewport_info dictionary if it doesn't exist\n            viewport_height = 0\n            if hasattr(state, \"viewport_info\") and state.viewport_info:\n                viewport_height = state.viewport_info.height\n            elif hasattr(ctx, \"config\") and hasattr(ctx.config, \"browser_window_size\"):\n                viewport_height = ctx.config.browser_window_size.get(\"height\", 0)\n\n            # Take a screenshot for the state\n            page = await ctx.get_current_page()\n\n            await page.bring_to_front()\n            await page.wait_for_load_state()\n\n            screenshot = await page.screenshot(\n                full_page=True, animations=\"disabled\", type=\"jpeg\", quality=100\n            )\n\n            screenshot = base64.b64encode(screenshot).decode(\"utf-8\")\n\n            # Build the state info with all required fields\n            state_info = {\n                \"url\": state.url,\n                \"title\": state.title,\n                \"tabs\": [tab.model_dump() for tab in state.tabs],\n                \"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.\",\n                \"interactive_elements\": (\n                    state.element_tree.clickable_elements_to_string()\n                    if state.element_tree\n                    else \"\"\n                ),\n                \"scroll_info\": {\n                    \"pixels_above\": getattr(state, \"pixels_above\", 0),\n                    \"pixels_below\": getattr(state, \"pixels_below\", 0),\n                    \"total_height\": getattr(state, \"pixels_above\", 0)\n                    + getattr(state, \"pixels_below\", 0)\n                    + viewport_height,\n                },\n                \"viewport_height\": viewport_height,\n            }\n\n            return ToolResult(\n                output=json.dumps(state_info, indent=4, ensure_ascii=False),\n                base64_image=screenshot,\n            )\n        except Exception as e:\n            return ToolResult(error=f\"Failed to get browser state: {str(e)}\")\n\n    async def cleanup(self):\n        \"\"\"Clean up browser resources.\"\"\"\n        async with self.lock:\n            if self.context is not None:\n                await self.context.close()\n                self.context = None\n                self.dom_service = None\n            if self.browser is not None:\n                await self.browser.close()\n                self.browser = None\n\n    def __del__(self):\n        \"\"\"Ensure cleanup when object is destroyed.\"\"\"\n        if self.browser is not None or self.context is not None:\n            try:\n                asyncio.run(self.cleanup())\n            except RuntimeError:\n                loop = asyncio.new_event_loop()\n                loop.run_until_complete(self.cleanup())\n                loop.close()\n\n    @classmethod\n    def create_with_context(cls, context: Context) -> \"BrowserUseTool[Context]\":\n        \"\"\"Factory method to create a BrowserUseTool with a specific context.\"\"\"\n        tool = cls()\n        tool.tool_context = context\n        return tool\n"
  },
  {
    "path": "app/tool/chart_visualization/README.md",
    "content": "\n\n# Chart Visualization Tool\n\nThe 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).\n\n## Installation (Mac / Linux)\n\n1. Install node >= 18\n\n```bash\ncurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash\n# Activate nvm, for example in Bash\nsource ~/.bashrc\n# Then install the latest stable release of Node\nnvm install node\n# Activate usage, for example if the latest stable release is 22, then use 22\nnvm use 22\n```\n\n2. Install dependencies\n\n```bash\n# Navigate to the appropriate location in the current repository\ncd app/tool/chart_visualization\nnpm install\n```\n\n## Installation (Windows)\n1. Install nvm-windows\n\n    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.\n\n2. Use nvm to install node\n\n```powershell\n# Then install the latest stable release of Node\nnvm install node\n# Activate usage, for example if the latest stable release is 22, then use 22\nnvm use 22\n```\n\n3. Install dependencies\n\n```bash\n# Navigate to the appropriate location in the current repository\ncd app/tool/chart_visualization\nnpm install\n```\n\n## Tool\n### python_execute\n\nExecute 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.\n\n#### Input\n```typescript\n{\n  // Code type: data processing/data report/other general tasks\n  code_type: \"process\" | \"report\" | \"others\"\n  // Final execution code\n  code: string;\n}\n```\n\n#### Output\nPython execution results, including the saving of intermediate files and print output results.\n\n### visualization_preparation\n\nA pre-tool for data visualization with two purposes,\n\n#### Data -> Chart\nUsed to extract the data needed for analysis (.csv) and the corresponding visualization description from the data, ultimately outputting a JSON configuration file.\n\n#### Chart + Insight -> Chart\nSelect 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.\n\n#### Input\n```typescript\n{\n  // Code type: data visualization or data insight addition\n  code_type: \"visualization\" | \"insight\"\n  // Python code used to produce the final JSON file\n  code: string;\n}\n```\n\n#### Output\nA configuration file for data visualization, used for the `data_visualization tool`.\n\n## data_visualization\n\nGenerate specific data visualizations based on the content of `visualization_preparation`.\n\n### Input\n```typescript\n{\n  // Configuration file path\n  json_path: string;\n  // Current purpose, data visualization or insight annotation addition\n  tool_type: \"visualization\" | \"insight\";\n  // Final product png or html; html supports vchart rendering and interaction\n  output_type: 'png' | 'html'\n  // Language, currently supports Chinese and English\n  language: \"zh\" | \"en\"\n}\n```\n\n## VMind Configuration\n\n### LLM\n\nVMind requires LLM invocation for intelligent chart generation. By default, it uses the `config.llm[\"default\"]` configuration.\n\n### Generation Settings\n\nMain configurations include chart dimensions, theme, and generation method:\n### Generation Method\nDefault: png. Currently supports automatic selection of `output_type` by LLM based on context.\n\n### Dimensions\nDefault dimensions are unspecified. For HTML output, charts fill the entire page by default. For PNG output, defaults to `1000*1000`.\n\n### Theme\nDefault theme: `'light'`. VChart supports multiple themes. See [Themes](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension).\n\n## Test\n\nCurrently, three tasks of different difficulty levels are set for testing.\n\n### Simple Chart Generation Task\n\nProvide data and specific chart generation requirements, test results, execute the command:\n```bash\npython -m app.tool.chart_visualization.test.chart_demo\n```\nThe results should be located under `workspace\\visualization`, involving 9 different chart results.\n\n### Simple Data Report Task\n\nProvide simple raw data analysis requirements, requiring simple processing of the data, execute the command:\n```bash\npython -m app.tool.chart_visualization.test.report_demo\n```\nThe results are also located under `workspace\\visualization`.\n"
  },
  {
    "path": "app/tool/chart_visualization/README_ja.md",
    "content": "# グラフ可視化ツール\n\nグラフ可視化ツールは、Pythonを使用してデータ処理コードを生成し、最終的に[@visactor/vmind](https://github.com/VisActor/VMind)を呼び出してグラフのspec結果を得ます。グラフのレンダリングには[@visactor/vchart](https://github.com/VisActor/VChart)を使用します。\n\n## インストール (Mac / Linux)\n\n1. Node >= 18をインストール\n\n```bash\ncurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash\n# nvmを有効化、例としてBashを使用\nsource ~/.bashrc\n# その後、最新の安定版Nodeをインストール\nnvm install node\n# 使用を有効化、例えば最新の安定版が22の場合、use 22\nnvm use 22\n```\n\n2. 依存関係をインストール\n\n```bash\ncd app/tool/chart_visualization\nnpm install\n```\n\n## インストール (Windows)\n1. nvm-windowsをインストール\n\n    [GitHub公式サイト](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)から最新バージョンの`nvm-setup.exe`をダウンロードしてインストール\n\n2. nvmを使用してNodeをインストール\n\n```powershell\n# その後、最新の安定版Nodeをインストール\nnvm install node\n# 使用を有効化、例えば最新の安定版が22の場合、use 22\nnvm use 22\n```\n\n3. 依存関係をインストール\n\n```bash\n# 現在のリポジトリで適切な位置に移動\ncd app/tool/chart_visualization\nnpm install\n```\n\n## ツール\n### python_execute\n\nPythonコードを使用してデータ分析（データ可視化を除く）に必要な部分を実行します。これにはデータ処理、データ要約、レポート生成、および一般的なPythonスクリプトコードが含まれます。\n\n#### 入力\n```typescript\n{\n  // コードタイプ：データ処理/データレポート/その他の一般的なタスク\n  code_type: \"process\" | \"report\" | \"others\"\n  // 最終実行コード\n  code: string;\n}\n```\n\n#### 出力\nPython実行結果、中間ファイルの保存とprint出力結果を含む\n\n### visualization_preparation\n\nデータ可視化の準備ツールで、2つの用途があります。\n\n#### Data -> Chart\nデータから分析に必要なデータ(.csv)と対応する可視化の説明を抽出し、最終的にJSON設定ファイルを出力します。\n\n#### Chart + Insight -> Chart\n既存のグラフと対応するデータインサイトを選択し、データインサイトをデータ注釈の形式でグラフに追加し、最終的にJSON設定ファイルを生成します。\n\n#### 入力\n```typescript\n{\n  // コードタイプ：データ可視化またはデータインサイト追加\n  code_type: \"visualization\" | \"insight\"\n  // 最終的なJSONファイルを生成するためのPythonコード\n  code: string;\n}\n```\n\n#### 出力\nデータ可視化の設定ファイル、`data_visualization tool`で使用\n\n## data_visualization\n\n`visualization_preparation`の内容に基づいて具体的なデータ可視化を生成\n\n### 入力\n```typescript\n{\n  // 設定ファイルのパス\n  json_path: string;\n  // 現在の用途、データ可視化またはインサイト注釈追加\n  tool_type: \"visualization\" | \"insight\";\n  // 最終成果物pngまたはhtml;htmlではvchartのレンダリングとインタラクションをサポート\n  output_type: 'png' | 'html'\n  // 言語、現在は中国語と英語をサポート\n  language: \"zh\" | \"en\"\n}\n```\n\n## 出力\n最終的に'png'または'html'の形式でローカルに保存され、保存されたグラフのパスとグラフ内で発見されたデータインサイトを出力\n\n## VMind設定\n\n### LLM\n\nVMind自体\n"
  },
  {
    "path": "app/tool/chart_visualization/README_ko.md",
    "content": "# 차트 시각화 도구\n\n차트 시각화 도구는 Python을 통해 데이터 처리 코드를 생성하고, 최종적으로 [@visactor/vmind](https://github.com/VisActor/VMind)를 호출하여 차트 사양을 얻습니다. 차트 렌더링은 [@visactor/vchart](https://github.com/VisActor/VChart)를 사용하여 구현됩니다.\n\n## 설치 (Mac / Linux)\n\n1. Node.js 18 이상 설치\n\n```bash\ncurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash\n# nvm 활성화, 예를 들어 Bash\nsource ~/.bashrc\n# 그런 다음 최신 안정 버전의 Node 설치\nnvm install node\n# 사용 활성화, 예를 들어 최신 안정 버전이 22인 경우 use 22\nnvm use 22\n```\n\n2. 의존성 설치\n\n```bash\n# 현재 저장소에서 해당 위치로 이동\ncd app/tool/chart_visualization\nnpm install\n```\n\n## 설치 (Windows)\n1. nvm-windows 설치\n\n    [공식 GitHub 페이지](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)에서 최신 버전의 `nvm-setup.exe`를 다운로드하고 설치합니다.\n\n2. nvm을 사용하여 Node.js 설치\n\n```powershell\n# 그런 다음 최신 안정 버전의 Node 설치\nnvm install node\n# 사용 활성화, 예를 들어 최신 안정 버전이 22인 경우 use 22\nnvm use 22\n```\n\n3. 의존성 설치\n\n```bash\n# 현재 저장소에서 해당 위치로 이동\ncd app/tool/chart_visualization\nnpm install\n```\n\n## 도구\n### python_execute\n\nPython 코드를 사용하여 데이터 분석의 필요한 부분(데이터 시각화 제외)을 실행합니다. 여기에는 데이터 처리, 데이터 요약, 보고서 생성 및 일부 일반적인 Python 스크립트 코드가 포함됩니다.\n\n#### 입력\n```typescript\n{\n  // 코드 유형: 데이터 처리/데이터 보고서/기타 일반 작업\n  code_type: \"process\" | \"report\" | \"others\"\n  // 최종 실행 코드\n  code: string;\n}\n```\n\n#### 출력\nPython 실행 결과, 중간 파일 저장 및 출력 결과 포함.\n\n### visualization_preparation\n\n데이터 시각화를 위한 사전 도구로 두 가지 목적이 있습니다.\n\n#### 데이터 -> 차트\n분석에 필요한 데이터(.csv)와 해당 시각화 설명을 데이터에서 추출하여 최종적으로 JSON 구성 파일을 출력합니다.\n\n#### 차트 + 인사이트 -> 차트\n기존 차트와 해당 데이터 인사이트를 선택하고, 데이터 주석 형태로 차트에 추가할 데이터 인사이트를 선택하여 최종적으로 JSON 구성 파일을 생성합니다.\n\n#### 입력\n```typescript\n{\n  // 코드 유형: 데이터 시각화 또는 데이터 인사이트 추가\n  code_type: \"visualization\" | \"insight\"\n  // 최종 JSON 파일을 생성하는 데 사용되는 Python 코드\n  code: string;\n}\n```\n\n#### 출력\n`data_visualization tool`에 사용되는 데이터 시각화를 위한 구성 파일.\n\n## data_visualization\n\n`visualization_preparation`의 내용을 기반으로 특정 데이터 시각화를 생성합니다.\n\n### 입력\n```typescript\n{\n  // 구성 파일 경로\n  json_path: string;\n  // 현재 목적, 데이터 시각화 또는 인사이트 주석 추가\n  tool_type: \"visualization\" | \"insight\";\n  // 최종 제품 png 또는 html; html은 vchart 렌더링 및 상호작용 지원\n  output_type: 'png' | 'html'\n  // 언어, 현재 중국어 및 영어 지원\n  language: \"zh\" | \"en\"\n}\n```\n\n## VMind 구성\n\n### LLM\n\nVMind는 지능형 차트 생성을 위해 LLM 호출이 필요합니다. 기본적으로 `config.llm[\"default\"]` 구성을 사용합니다.\n\n### 생성 설정\n\n주요 구성에는 차트 크기, 테마 및 생성 방법이 포함됩니다.\n### 생성 방법\n기본값: png. 현재 LLM이 컨텍스트에 따라 `output_type`을 자동으로 선택하는 것을 지원합니다.\n\n### 크기\n기본 크기는 지정되지 않았습니다. HTML 출력의 경우 차트는 기본적으로 전체 페이지를 채웁니다. PNG 출력의 경우 기본값은 `1000*1000`입니다.\n\n### 테마\n기본 테마: `'light'`. VChart는 여러 테마를 지원합니다. [테마](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension)를 참조하세요.\n\n## 테스트\n\n현재, 서로 다른 난이도의\n"
  },
  {
    "path": "app/tool/chart_visualization/README_zh.md",
    "content": "# 图表可视化工具\n\n图表可视化工具，通过python生成数据处理代码，最终调用[@visactor/vmind](https://github.com/VisActor/VMind)得到图表的spec结果，图表渲染使用[@visactor/vchart](https://github.com/VisActor/VChart)\n\n## 安装(Mac / Linux)\n\n1. 安装node >= 18\n\n```bash\ncurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash\n# 激活nvm，以Bash为例\nsource ~/.bashrc\n# 然后安装 Node 最近一个稳定颁布\nnvm install node\n# 激活使用，例如最新一个稳定颁布为22，则use 22\nnvm use 22\n```\n\n2. 安装依赖\n\n```bash\ncd app/tool/chart_visualization\nnpm install\n```\n\n## 安装(Windows)\n1. 安装nvm-windows\n\n    从[github官网](https://github.com/coreybutler/nvm-windows?tab=readme-ov-file#readme)上下载最新版本`nvm-setup.exe`并且安装\n\n2. 使用nvm安装node\n\n```powershell\n# 然后安装 Node 最近一个稳定颁布\nnvm install node\n# 激活使用，例如最新一个稳定颁布为22，则use 22\nnvm use 22\n```\n\n3. 安装依赖\n\n```bash\n# 在当前仓库下定位到相应位置\ncd app/tool/chart_visualization\nnpm install\n```\n## Tool\n### python_execute\n\n用python代码执行数据分析（除数据可视化以外）中需要的部分，包括数据处理，数据总结摘要，报告生成以及一些通用python脚本代码\n\n#### 输入\n```typescript\n{\n  // 代码类型：数据处理/数据报告/其他通用任务\n  code_type: \"process\" | \"report\" | \"others\"\n  // 最终执行代码\n  code: string;\n}\n```\n\n#### 输出\npython执行结果，带有中间文件的保存和print输出结果\n\n### visualization_preparation\n\n数据可视化前置工具，有两种用途，\n\n#### Data -〉 Chart\n用于从数据中提取需要分析的数据(.csv)和对应可视化的描述，最终输出一份json配置文件。\n\n#### Chart + Insight -> Chart\n选取已有的图表和对应的数据洞察，挑选数据洞察以数据标注的形式增加到图表中，最终生成一份json配置文件。\n\n#### 输入\n```typescript\n{\n  // 代码类型：数据可视化 或者 数据洞察添加\n  code_type: \"visualization\" | \"insight\"\n  // 用于生产最终json文件的python代码\n  code: string;\n}\n```\n\n#### 输出\n数据可视化的配置文件，用于`data_visualization tool`\n\n\n## data_visualization\n\n根据`visualization_preparation`的内容，生成具体的数据可视化\n\n### 输入\n```typescript\n{\n  // 配置文件路径\n  json_path: string;\n  // 当前用途，数据可视化或者洞察标注添加\n  tool_type: \"visualization\" | \"insight\";\n  // 最终产物png或者html;html下支持vchart渲染和交互\n  output_type: 'png' | 'html'\n  // 语言,目前支持中文和英文\n  language: \"zh\" | \"en\"\n}\n```\n\n## 输出\n最终以'png'或者'html'的形式保存在本地，输出保存的图表路径以及图表中发现的数据洞察\n\n## VMind配置\n\n### LLM\n\nVMind本身也需要通过调用大模型得到智能图表生成结果，目前默认会使用`config.llm[\"default\"]`配置\n\n### 生成配置\n\n主要生成配置包括图表的宽高、主题以及生成方式；\n### 生成方式\n默认为png，目前支持大模型根据上下文自己选择`output_type`\n\n### 宽高\n目前默认不指定宽高，`html`下默认占满整个页面，'png'下默认为`1000 * 1000`\n\n### 主题\n目前默认主题为`'light'`，VChart图表支持多种主题，详见[主题](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension)\n\n\n## 测试\n\n当前设置了三种不同难度的任务用于测试\n\n### 简单图表生成任务\n\n给予数据和具体的图表生成需求，测试结果，执行命令：\n```bash\npython -m app.tool.chart_visualization.test.chart_demo\n```\n结果应位于`worksapce\\visualization`下，涉及到9种不同的图表结果\n\n### 简单数据报表任务\n\n给予简单原始数据可分析需求，需要对数据进行简单加工处理，执行命令：\n```bash\npython -m app.tool.chart_visualization.test.report_demo\n```\n结果同样位于`worksapce\\visualization`下\n"
  },
  {
    "path": "app/tool/chart_visualization/__init__.py",
    "content": "from app.tool.chart_visualization.chart_prepare import VisualizationPrepare\nfrom app.tool.chart_visualization.data_visualization import DataVisualization\nfrom app.tool.chart_visualization.python_execute import NormalPythonExecute\n\n\n__all__ = [\"DataVisualization\", \"VisualizationPrepare\", \"NormalPythonExecute\"]\n"
  },
  {
    "path": "app/tool/chart_visualization/chart_prepare.py",
    "content": "from app.tool.chart_visualization.python_execute import NormalPythonExecute\n\n\nclass VisualizationPrepare(NormalPythonExecute):\n    \"\"\"A tool for Chart Generation Preparation\"\"\"\n\n    name: str = \"visualization_preparation\"\n    description: str = \"Using Python code to generates metadata of data_visualization tool. Outputs: 1) JSON Information. 2) Cleaned CSV data files (Optional).\"\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"code_type\": {\n                \"description\": \"code type, visualization: csv -> chart; insight: choose insight into chart\",\n                \"type\": \"string\",\n                \"default\": \"visualization\",\n                \"enum\": [\"visualization\", \"insight\"],\n            },\n            \"code\": {\n                \"type\": \"string\",\n                \"description\": \"\"\"Python code for data_visualization prepare.\n## Visualization Type\n1. Data loading logic\n2. Csv Data and chart description generate\n2.1 Csv data (The data you want to visulazation, cleaning / transform from origin data, saved in .csv)\n2.2 Chart description of csv data (The chart title or description should be concise and clear. Examples: 'Product sales distribution', 'Monthly revenue trend'.)\n3. Save information in json file.( format: {\"csvFilePath\": string, \"chartTitle\": string}[])\n## Insight Type\n1. Select the insights from the data_visualization results that you want to add to the chart.\n2. Save information in json file.( format: {\"chartPath\": string, \"insights_id\": number[]}[])\n# Note\n1. You can generate one or multiple csv data with different visualization needs.\n2. Make each chart data esay, clean and different.\n3. Json file saving in utf-8 with path print: print(json_path)\n\"\"\",\n            },\n        },\n        \"required\": [\"code\", \"code_type\"],\n    }\n"
  },
  {
    "path": "app/tool/chart_visualization/data_visualization.py",
    "content": "import asyncio\nimport json\nimport os\nfrom typing import Any, Hashable\n\nimport pandas as pd\nfrom pydantic import Field, model_validator\n\nfrom app.config import config\nfrom app.llm import LLM\nfrom app.logger import logger\nfrom app.tool.base import BaseTool\n\n\nclass DataVisualization(BaseTool):\n    name: str = \"data_visualization\"\n    description: str = \"\"\"Visualize statistical chart or Add insights in chart with JSON info from visualization_preparation tool. You can do steps as follows:\n1. Visualize statistical chart\n2. Choose insights into chart based on step 1 (Optional)\nOutputs:\n1. Charts (png/html)\n2. Charts Insights (.md)(Optional)\"\"\"\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"json_path\": {\n                \"type\": \"string\",\n                \"description\": \"\"\"file path of json info with \".json\" in the end\"\"\",\n            },\n            \"output_type\": {\n                \"description\": \"Rendering format (html=interactive)\",\n                \"type\": \"string\",\n                \"default\": \"html\",\n                \"enum\": [\"png\", \"html\"],\n            },\n            \"tool_type\": {\n                \"description\": \"visualize chart or add insights\",\n                \"type\": \"string\",\n                \"default\": \"visualization\",\n                \"enum\": [\"visualization\", \"insight\"],\n            },\n            \"language\": {\n                \"description\": \"english(en) / chinese(zh)\",\n                \"type\": \"string\",\n                \"default\": \"en\",\n                \"enum\": [\"zh\", \"en\"],\n            },\n        },\n        \"required\": [\"code\"],\n    }\n    llm: LLM = Field(default_factory=LLM, description=\"Language model instance\")\n\n    @model_validator(mode=\"after\")\n    def initialize_llm(self):\n        \"\"\"Initialize llm with default settings if not provided.\"\"\"\n        if self.llm is None or not isinstance(self.llm, LLM):\n            self.llm = LLM(config_name=self.name.lower())\n        return self\n\n    def get_file_path(\n        self,\n        json_info: list[dict[str, str]],\n        path_str: str,\n        directory: str = None,\n    ) -> list[str]:\n        res = []\n        for item in json_info:\n            if os.path.exists(item[path_str]):\n                res.append(item[path_str])\n            elif os.path.exists(\n                os.path.join(f\"{directory or config.workspace_root}\", item[path_str])\n            ):\n                res.append(\n                    os.path.join(\n                        f\"{directory or config.workspace_root}\", item[path_str]\n                    )\n                )\n            else:\n                raise Exception(f\"No such file or directory: {item[path_str]}\")\n        return res\n\n    def success_output_template(self, result: list[dict[str, str]]) -> str:\n        content = \"\"\n        if len(result) == 0:\n            return \"Is EMPTY!\"\n        for item in result:\n            content += f\"\"\"## {item['title']}\\nChart saved in: {item['chart_path']}\"\"\"\n            if \"insight_path\" in item and item[\"insight_path\"] and \"insight_md\" in item:\n                content += \"\\n\" + item[\"insight_md\"]\n            else:\n                content += \"\\n\"\n        return f\"Chart Generated Successful!\\n{content}\"\n\n    async def data_visualization(\n        self, json_info: list[dict[str, str]], output_type: str, language: str\n    ) -> str:\n        data_list = []\n        csv_file_path = self.get_file_path(json_info, \"csvFilePath\")\n        for index, item in enumerate(json_info):\n            df = pd.read_csv(csv_file_path[index], encoding=\"utf-8\")\n            df = df.astype(object)\n            df = df.where(pd.notnull(df), None)\n            data_dict_list = df.to_json(orient=\"records\", force_ascii=False)\n\n            data_list.append(\n                {\n                    \"file_name\": os.path.basename(csv_file_path[index]).replace(\n                        \".csv\", \"\"\n                    ),\n                    \"dict_data\": data_dict_list,\n                    \"chartTitle\": item[\"chartTitle\"],\n                }\n            )\n        tasks = [\n            self.invoke_vmind(\n                dict_data=item[\"dict_data\"],\n                chart_description=item[\"chartTitle\"],\n                file_name=item[\"file_name\"],\n                output_type=output_type,\n                task_type=\"visualization\",\n                language=language,\n            )\n            for item in data_list\n        ]\n\n        results = await asyncio.gather(*tasks)\n        error_list = []\n        success_list = []\n        for index, result in enumerate(results):\n            csv_path = csv_file_path[index]\n            if \"error\" in result and \"chart_path\" not in result:\n                error_list.append(f\"Error in {csv_path}: {result['error']}\")\n            else:\n                success_list.append(\n                    {\n                        **result,\n                        \"title\": json_info[index][\"chartTitle\"],\n                    }\n                )\n        if len(error_list) > 0:\n            return {\n                \"observation\": f\"# Error chart generated{'\\n'.join(error_list)}\\n{self.success_output_template(success_list)}\",\n                \"success\": False,\n            }\n        else:\n            return {\"observation\": f\"{self.success_output_template(success_list)}\"}\n\n    async def add_insighs(\n        self, json_info: list[dict[str, str]], output_type: str\n    ) -> str:\n        data_list = []\n        chart_file_path = self.get_file_path(\n            json_info, \"chartPath\", os.path.join(config.workspace_root, \"visualization\")\n        )\n        for index, item in enumerate(json_info):\n            if \"insights_id\" in item:\n                data_list.append(\n                    {\n                        \"file_name\": os.path.basename(chart_file_path[index]).replace(\n                            f\".{output_type}\", \"\"\n                        ),\n                        \"insights_id\": item[\"insights_id\"],\n                    }\n                )\n        tasks = [\n            self.invoke_vmind(\n                insights_id=item[\"insights_id\"],\n                file_name=item[\"file_name\"],\n                output_type=output_type,\n                task_type=\"insight\",\n            )\n            for item in data_list\n        ]\n        results = await asyncio.gather(*tasks)\n        error_list = []\n        success_list = []\n        for index, result in enumerate(results):\n            chart_path = chart_file_path[index]\n            if \"error\" in result and \"chart_path\" not in result:\n                error_list.append(f\"Error in {chart_path}: {result['error']}\")\n            else:\n                success_list.append(chart_path)\n        success_template = (\n            f\"# Charts Update with Insights\\n{','.join(success_list)}\"\n            if len(success_list) > 0\n            else \"\"\n        )\n        if len(error_list) > 0:\n            return {\n                \"observation\": f\"# Error in chart insights:{'\\n'.join(error_list)}\\n{success_template}\",\n                \"success\": False,\n            }\n        else:\n            return {\"observation\": f\"{success_template}\"}\n\n    async def execute(\n        self,\n        json_path: str,\n        output_type: str | None = \"html\",\n        tool_type: str | None = \"visualization\",\n        language: str | None = \"en\",\n    ) -> str:\n        try:\n            logger.info(f\"📈 data_visualization with {json_path} in: {tool_type} \")\n            with open(json_path, \"r\", encoding=\"utf-8\") as file:\n                json_info = json.load(file)\n            if tool_type == \"visualization\":\n                return await self.data_visualization(json_info, output_type, language)\n            else:\n                return await self.add_insighs(json_info, output_type)\n        except Exception as e:\n            return {\n                \"observation\": f\"Error: {e}\",\n                \"success\": False,\n            }\n\n    async def invoke_vmind(\n        self,\n        file_name: str,\n        output_type: str,\n        task_type: str,\n        insights_id: list[str] = None,\n        dict_data: list[dict[Hashable, Any]] = None,\n        chart_description: str = None,\n        language: str = \"en\",\n    ):\n        llm_config = {\n            \"base_url\": self.llm.base_url,\n            \"model\": self.llm.model,\n            \"api_key\": self.llm.api_key,\n        }\n        vmind_params = {\n            \"llm_config\": llm_config,\n            \"user_prompt\": chart_description,\n            \"dataset\": dict_data,\n            \"file_name\": file_name,\n            \"output_type\": output_type,\n            \"insights_id\": insights_id,\n            \"task_type\": task_type,\n            \"directory\": str(config.workspace_root),\n            \"language\": language,\n        }\n        # build async sub process\n        process = await asyncio.create_subprocess_exec(\n            \"npx\",\n            \"ts-node\",\n            \"src/chartVisualize.ts\",\n            stdin=asyncio.subprocess.PIPE,\n            stdout=asyncio.subprocess.PIPE,\n            stderr=asyncio.subprocess.PIPE,\n            cwd=os.path.dirname(__file__),\n        )\n        input_json = json.dumps(vmind_params, ensure_ascii=False).encode(\"utf-8\")\n        try:\n            stdout, stderr = await process.communicate(input_json)\n            stdout_str = stdout.decode(\"utf-8\")\n            stderr_str = stderr.decode(\"utf-8\")\n            if process.returncode == 0:\n                return json.loads(stdout_str)\n            else:\n                return {\"error\": f\"Node.js Error: {stderr_str}\"}\n        except Exception as e:\n            return {\"error\": f\"Subprocess Error: {str(e)}\"}\n"
  },
  {
    "path": "app/tool/chart_visualization/package.json",
    "content": "{\n  \"name\": \"chart_visualization\",\n  \"version\": \"1.0.0\",\n  \"main\": \"src/index.ts\",\n  \"devDependencies\": {\n    \"@types/node\": \"^22.10.1\",\n    \"ts-node\": \"^10.9.2\",\n    \"typescript\": \"^5.7.2\"\n  },\n  \"dependencies\": {\n    \"@visactor/vchart\": \"^1.13.7\",\n    \"@visactor/vmind\": \"2.0.5\",\n    \"get-stdin\": \"^9.0.0\",\n    \"puppeteer\": \"^24.9.0\"\n  },\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n  },\n  \"author\": \"\",\n  \"license\": \"ISC\",\n  \"description\": \"\"\n}\n"
  },
  {
    "path": "app/tool/chart_visualization/python_execute.py",
    "content": "from app.config import config\nfrom app.tool.python_execute import PythonExecute\n\n\nclass NormalPythonExecute(PythonExecute):\n    \"\"\"A tool for executing Python code with timeout and safety restrictions.\"\"\"\n\n    name: str = \"python_execute\"\n    description: str = \"\"\"Execute Python code for in-depth data analysis / data report(task conclusion) / other normal task without direct visualization.\"\"\"\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"code_type\": {\n                \"description\": \"code type, data process / data report / others\",\n                \"type\": \"string\",\n                \"default\": \"process\",\n                \"enum\": [\"process\", \"report\", \"others\"],\n            },\n            \"code\": {\n                \"type\": \"string\",\n                \"description\": \"\"\"Python code to execute.\n# Note\n1. The code should generate a comprehensive text-based report containing dataset overview, column details, basic statistics, derived metrics, timeseries comparisons, outliers, and key insights.\n2. Use print() for all outputs so the analysis (including sections like 'Dataset Overview' or 'Preprocessing Results') is clearly visible and save it also\n3. Save any report / processed files / each analysis result in worksapce directory: {directory}\n4. Data reports need to be content-rich, including your overall analysis process and corresponding data visualization.\n5. You can invode this tool step-by-step to do data analysis from summary to in-depth with data report saved also\"\"\".format(\n                    directory=config.workspace_root\n                ),\n            },\n        },\n        \"required\": [\"code\"],\n    }\n\n    async def execute(self, code: str, code_type: str | None = None, timeout=5):\n        return await super().execute(code, timeout)\n"
  },
  {
    "path": "app/tool/chart_visualization/src/chartVisualize.ts",
    "content": "import path from \"path\";\nimport fs from \"fs\";\nimport puppeteer from \"puppeteer\";\nimport VMind, { ChartType, DataTable } from \"@visactor/vmind\";\nimport { isString } from \"@visactor/vutils\";\n\nenum AlgorithmType {\n  OverallTrending = \"overallTrend\",\n  AbnormalTrend = \"abnormalTrend\",\n  PearsonCorrelation = \"pearsonCorrelation\",\n  SpearmanCorrelation = \"spearmanCorrelation\",\n  ExtremeValue = \"extremeValue\",\n  MajorityValue = \"majorityValue\",\n  StatisticsAbnormal = \"statisticsAbnormal\",\n  StatisticsBase = \"statisticsBase\",\n  DbscanOutlier = \"dbscanOutlier\",\n  LOFOutlier = \"lofOutlier\",\n  TurningPoint = \"turningPoint\",\n  PageHinkley = \"pageHinkley\",\n  DifferenceOutlier = \"differenceOutlier\",\n  Volatility = \"volatility\",\n}\n\nconst getBase64 = async (spec: any, width?: number, height?: number) => {\n  spec.animation = false;\n  width && (spec.width = width);\n  height && (spec.height = height);\n  const browser = await puppeteer.launch();\n  const page = await browser.newPage();\n  await page.setContent(getHtmlVChart(spec, width, height));\n\n  const dataUrl = await page.evaluate(() => {\n    const canvas: any = document\n      .getElementById(\"chart-container\")\n      ?.querySelector(\"canvas\");\n    return canvas?.toDataURL(\"image/png\");\n  });\n\n  const base64Data = dataUrl.replace(/^data:image\\/png;base64,/, \"\");\n  await browser.close();\n  return Buffer.from(base64Data, \"base64\");\n};\n\nconst serializeSpec = (spec: any) => {\n  return JSON.stringify(spec, (key, value) => {\n    if (typeof value === \"function\") {\n      const funcStr = value\n        .toString()\n        .replace(/(\\r\\n|\\n|\\r)/gm, \"\")\n        .replace(/\\s+/g, \" \");\n\n      return `__FUNCTION__${funcStr}`;\n    }\n    return value;\n  });\n};\n\nfunction getHtmlVChart(spec: any, width?: number, height?: number) {\n  return `<!DOCTYPE html>\n<html>\n<head>\n    <title>VChart Demo</title>\n    <script src=\"https://unpkg.com/@visactor/vchart/build/index.min.js\"></script>\n</head>\n<body>\n    <div id=\"chart-container\" style=\"width: ${\n      width ? `${width}px` : \"100%\"\n    }; height: ${height ? `${height}px` : \"100%\"};\"></div>\n    <script>\n      // parse spec with function\n      function parseSpec(stringSpec) {\n        return JSON.parse(stringSpec, (k, v) => {\n          if (typeof v === 'string' && v.startsWith('__FUNCTION__')) {\n            const funcBody = v.slice(12); // 移除标记\n            try {\n              return new Function('return (' + funcBody + ')')();\n            } catch(e) {\n              console.error('函数解析失败:', e);\n              return () => {};\n            }\n          }\n          return v;\n        });\n      }\n      const spec = parseSpec(\\`${serializeSpec(spec)}\\`);\n      const chart = new VChart.VChart(spec, {\n          dom: 'chart-container'\n      });\n      chart.renderSync();\n    </script>\n</body>\n</html>\n`;\n}\n\n/**\n * get file path saved string\n * @param isUpdate {boolean} default: false, update existed file when is true\n */\nfunction getSavedPathName(\n  directory: string,\n  fileName: string,\n  outputType: \"html\" | \"png\" | \"json\" | \"md\",\n  isUpdate: boolean = false\n) {\n  let newFileName = fileName;\n  while (\n    !isUpdate &&\n    fs.existsSync(\n      path.join(directory, \"visualization\", `${newFileName}.${outputType}`)\n    )\n  ) {\n    newFileName += \"_new\";\n  }\n  return path.join(directory, \"visualization\", `${newFileName}.${outputType}`);\n}\n\nconst readStdin = (): Promise<string> => {\n  return new Promise((resolve) => {\n    let input = \"\";\n    process.stdin.setEncoding(\"utf-8\"); // 确保编码与 Python 端一致\n    process.stdin.on(\"data\", (chunk) => (input += chunk));\n    process.stdin.on(\"end\", () => resolve(input));\n  });\n};\n\n/** Save insights markdown in local, and return content && path */\nconst setInsightTemplate = (\n  path: string,\n  title: string,\n  insights: string[]\n) => {\n  let res = \"\";\n  if (insights.length) {\n    res += `## ${title} Insights`;\n    insights.forEach((insight, index) => {\n      res += `\\n${index + 1}. ${insight}`;\n    });\n  }\n  if (res) {\n    fs.writeFileSync(path, res, \"utf-8\");\n    return { insight_path: path, insight_md: res };\n  }\n  return {};\n};\n\n/** Save vmind result into local file, Return chart file path */\nasync function saveChartRes(options: {\n  spec: any;\n  directory: string;\n  outputType: \"png\" | \"html\";\n  fileName: string;\n  width?: number;\n  height?: number;\n  isUpdate?: boolean;\n}) {\n  const { directory, fileName, spec, outputType, width, height, isUpdate } =\n    options;\n  const specPath = getSavedPathName(directory, fileName, \"json\", isUpdate);\n  fs.writeFileSync(specPath, JSON.stringify(spec, null, 2));\n  const savedPath = getSavedPathName(directory, fileName, outputType, isUpdate);\n  if (outputType === \"png\") {\n    const base64 = await getBase64(spec, width, height);\n    fs.writeFileSync(savedPath, base64);\n  } else {\n    const html = getHtmlVChart(spec, width, height);\n    fs.writeFileSync(savedPath, html, \"utf-8\");\n  }\n  return savedPath;\n}\n\nasync function generateChart(\n  vmind: VMind,\n  options: {\n    dataset: string | DataTable;\n    userPrompt: string;\n    directory: string;\n    outputType: \"png\" | \"html\";\n    fileName: string;\n    width?: number;\n    height?: number;\n    language?: \"en\" | \"zh\";\n  }\n) {\n  let res: {\n    chart_path?: string;\n    error?: string;\n    insight_path?: string;\n    insight_md?: string;\n  } = {};\n  const {\n    dataset,\n    userPrompt,\n    directory,\n    width,\n    height,\n    outputType,\n    fileName,\n    language,\n  } = options;\n  try {\n    // Get chart spec and save in local file\n    const jsonDataset = isString(dataset) ? JSON.parse(dataset) : dataset;\n    const { spec, error, chartType } = await vmind.generateChart(\n      userPrompt,\n      undefined,\n      jsonDataset,\n      {\n        enableDataQuery: false,\n        theme: \"light\",\n      }\n    );\n    if (error || !spec) {\n      return {\n        error: error || \"Spec of Chart was Empty!\",\n      };\n    }\n\n    spec.title = {\n      text: userPrompt,\n    };\n    if (!fs.existsSync(path.join(directory, \"visualization\"))) {\n      fs.mkdirSync(path.join(directory, \"visualization\"));\n    }\n    const specPath = getSavedPathName(directory, fileName, \"json\");\n    res.chart_path = await saveChartRes({\n      directory,\n      spec,\n      width,\n      height,\n      fileName,\n      outputType,\n    });\n\n    // get chart insights and save in local\n    const insights = [];\n    if (\n      chartType &&\n      [\n        ChartType.BarChart,\n        ChartType.LineChart,\n        ChartType.AreaChart,\n        ChartType.ScatterPlot,\n        ChartType.DualAxisChart,\n      ].includes(chartType)\n    ) {\n      const { insights: vmindInsights } = await vmind.getInsights(spec, {\n        maxNum: 6,\n        algorithms: [\n          AlgorithmType.OverallTrending,\n          AlgorithmType.AbnormalTrend,\n          AlgorithmType.PearsonCorrelation,\n          AlgorithmType.SpearmanCorrelation,\n          AlgorithmType.StatisticsAbnormal,\n          AlgorithmType.LOFOutlier,\n          AlgorithmType.DbscanOutlier,\n          AlgorithmType.MajorityValue,\n          AlgorithmType.PageHinkley,\n          AlgorithmType.TurningPoint,\n          AlgorithmType.StatisticsBase,\n          AlgorithmType.Volatility,\n        ],\n        usePolish: false,\n        language: language === \"en\" ? \"english\" : \"chinese\",\n      });\n      insights.push(...vmindInsights);\n    }\n    const insightsText = insights\n      .map((insight) => insight.textContent?.plainText)\n      .filter((insight) => !!insight) as string[];\n    spec.insights = insights;\n    fs.writeFileSync(specPath, JSON.stringify(spec, null, 2));\n    res = {\n      ...res,\n      ...setInsightTemplate(\n        getSavedPathName(directory, fileName, \"md\"),\n        userPrompt,\n        insightsText\n      ),\n    };\n  } catch (error: any) {\n    res.error = error.toString();\n  } finally {\n    return res;\n  }\n}\n\nasync function updateChartWithInsight(\n  vmind: VMind,\n  options: {\n    directory: string;\n    outputType: \"png\" | \"html\";\n    fileName: string;\n    insightsId: number[];\n  }\n) {\n  const { directory, outputType, fileName, insightsId } = options;\n  let res: { error?: string; chart_path?: string } = {};\n  try {\n    const specPath = getSavedPathName(directory, fileName, \"json\", true);\n    const spec = JSON.parse(fs.readFileSync(specPath, \"utf8\"));\n    // llm select index from 1\n    const insights = (spec.insights || []).filter(\n      (_insight: any, index: number) => insightsId.includes(index + 1)\n    );\n    const { newSpec, error } = await vmind.updateSpecByInsights(spec, insights);\n    if (error) {\n      throw error;\n    }\n    res.chart_path = await saveChartRes({\n      spec: newSpec,\n      directory,\n      outputType,\n      fileName,\n      isUpdate: true,\n    });\n  } catch (error: any) {\n    res.error = error.toString();\n  } finally {\n    return res;\n  }\n}\n\nasync function executeVMind() {\n  const input = await readStdin();\n  const inputData = JSON.parse(input);\n  let res;\n  const {\n    llm_config,\n    width,\n    dataset = [],\n    height,\n    directory,\n    user_prompt: userPrompt,\n    output_type: outputType = \"png\",\n    file_name: fileName,\n    task_type: taskType = \"visualization\",\n    insights_id: insightsId = [],\n    language = \"en\",\n  } = inputData;\n  const { base_url: baseUrl, model, api_key: apiKey } = llm_config;\n  const vmind = new VMind({\n    url: `${baseUrl}/chat/completions`,\n    model,\n    headers: {\n      \"api-key\": apiKey,\n      Authorization: `Bearer ${apiKey}`,\n    },\n  });\n  if (taskType === \"visualization\") {\n    res = await generateChart(vmind, {\n      dataset,\n      userPrompt,\n      directory,\n      outputType,\n      fileName,\n      width,\n      height,\n      language,\n    });\n  } else if (taskType === \"insight\" && insightsId.length) {\n    res = await updateChartWithInsight(vmind, {\n      directory,\n      fileName,\n      outputType,\n      insightsId,\n    });\n  }\n  console.log(JSON.stringify(res));\n}\n\nexecuteVMind();\n"
  },
  {
    "path": "app/tool/chart_visualization/test/chart_demo.py",
    "content": "import asyncio\n\nfrom app.agent.data_analysis import DataAnalysis\nfrom app.logger import logger\n\n\nprefix = \"Help me generate charts and save them locally, specifically:\"\ntasks = [\n    {\n        \"prompt\": \"Help me show the sales of different products in different regions\",\n        \"data\": \"\"\"Product Name,Region,Sales\nCoke,South,2350\nCoke,East,1027\nCoke,West,1027\nCoke,North,1027\nSprite,South,215\nSprite,East,654\nSprite,West,159\nSprite,North,28\nFanta,South,345\nFanta,East,654\nFanta,West,2100\nFanta,North,1679\nXingmu,South,1476\nXingmu,East,830\nXingmu,West,532\nXingmu,North,498\n\"\"\",\n    },\n    {\n        \"prompt\": \"Show market share of each brand\",\n        \"data\": \"\"\"Brand Name,Market Share,Average Price,Net Profit\nApple,0.5,7068,314531\nSamsung,0.2,6059,362345\nVivo,0.05,3406,234512\nNokia,0.01,1064,-1345\nXiaomi,0.1,4087,131345\"\"\",\n    },\n    {\n        \"prompt\": \"Please help me show the sales trend of each product\",\n        \"data\": \"\"\"Date,Type,Value\n2023-01-01,Product A,52.9\n2023-01-01,Product B,63.6\n2023-01-01,Product C,11.2\n2023-01-02,Product A,45.7\n2023-01-02,Product B,89.1\n2023-01-02,Product C,21.4\n2023-01-03,Product A,67.2\n2023-01-03,Product B,82.4\n2023-01-03,Product C,31.7\n2023-01-04,Product A,80.7\n2023-01-04,Product B,55.1\n2023-01-04,Product C,21.1\n2023-01-05,Product A,65.6\n2023-01-05,Product B,78\n2023-01-05,Product C,31.3\n2023-01-06,Product A,75.6\n2023-01-06,Product B,89.1\n2023-01-06,Product C,63.5\n2023-01-07,Product A,67.3\n2023-01-07,Product B,77.2\n2023-01-07,Product C,43.7\n2023-01-08,Product A,96.1\n2023-01-08,Product B,97.6\n2023-01-08,Product C,59.9\n2023-01-09,Product A,96.1\n2023-01-09,Product B,100.6\n2023-01-09,Product C,66.8\n2023-01-10,Product A,101.6\n2023-01-10,Product B,108.3\n2023-01-10,Product C,56.9\"\"\",\n    },\n    {\n        \"prompt\": \"Show the popularity of search keywords\",\n        \"data\": \"\"\"Keyword,Popularity\nHot Word,1000\nZao Le Wo Men,800\nRao Jian Huo,400\nMy Wish is World Peace,400\nXiu Xiu Xiu,400\nShenzhou 11,400\nHundred Birds Facing the Wind,400\nChina Women's Volleyball Team,400\nMy Guan Na,400\nLeg Dong,400\nHot Pot Hero,400\nBaby's Heart is Bitter,400\nOlympics,400\nAwesome My Brother,400\nPoetry and Distance,400\nSong Joong-ki,400\nPPAP,400\nBlue Thin Mushroom,400\nRain Dew Evenly,400\nFriendship's Little Boat Says It Flips,400\nBeijing Slump,400\nDedication,200\nApple,200\nDog Belt,200\nOld Driver,200\nMelon-Eating Crowd,200\nZootopia,200\nCity Will Play,200\nRoutine,200\nWater Reverse,200\nWhy Don't You Go to Heaven,200\nSnake Spirit Man,200\nWhy Don't You Go to Heaven,200\nSamsung Explosion Gate,200\nLittle Li Oscar,200\nUgly People Need to Read More,200\nBoyfriend Power,200\nA Face of Confusion,200\nDescendants of the Sun,200\"\"\",\n    },\n    {\n        \"prompt\": \"Help me compare the performance of different electric vehicle brands using a scatter plot\",\n        \"data\": \"\"\"Range,Charging Time,Brand Name,Average Price\n2904,46,Brand1,2350\n1231,146,Brand2,1027\n5675,324,Brand3,1242\n543,57,Brand4,6754\n326,234,Brand5,215\n1124,67,Brand6,654\n3426,81,Brand7,159\n2134,24,Brand8,28\n1234,52,Brand9,345\n2345,27,Brand10,654\n526,145,Brand11,2100\n234,93,Brand12,1679\n567,94,Brand13,1476\n789,45,Brand14,830\n469,75,Brand15,532\n5689,54,Brand16,498\n\"\"\",\n    },\n    {\n        \"prompt\": \"Show conversion rates for each process\",\n        \"data\": \"\"\"Process,Conversion Rate,Month\nStep1,100,1\nStep2,80,1\nStep3,60,1\nStep4,40,1\"\"\",\n    },\n    {\n        \"prompt\": \"Show the difference in breakfast consumption between men and women\",\n        \"data\": \"\"\"Day,Men-Breakfast,Women-Breakfast\nMonday,15,22\nTuesday,12,10\nWednesday,15,20\nThursday,10,12\nFriday,13,15\nSaturday,10,15\nSunday,12,14\"\"\",\n    },\n    {\n        \"prompt\": \"Help me show this person's performance in different aspects, is he a hexagonal warrior\",\n        \"data\": \"\"\"dimension,performance\nStrength,5\nSpeed,5\nShooting,3\nEndurance,5\nPrecision,5\nGrowth,5\"\"\",\n    },\n    {\n        \"prompt\": \"Show data flow\",\n        \"data\": \"\"\"Origin,Destination,value\nNode A,Node 1,10\nNode A,Node 2,5\nNode B,Node 2,8\nNode B,Node 3,2\nNode C,Node 2,4\nNode A,Node C,2\nNode C,Node 1,2\"\"\",\n    },\n]\n\n\nasync def main():\n    for index, item in enumerate(tasks):\n        logger.info(f\"Begin task {index} / {len(tasks)}!\")\n        agent = DataAnalysis()\n        await agent.run(\n            f\"{prefix},chart_description:{item['prompt']},Data:{item['data']}\"\n        )\n        logger.info(f\"Finish with {item['prompt']}\")\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n"
  },
  {
    "path": "app/tool/chart_visualization/test/report_demo.py",
    "content": "import asyncio\n\nfrom app.agent.data_analysis import DataAnalysis\n\n\n# from app.agent.manus import Manus\n\n\nasync def main():\n    agent = DataAnalysis()\n    # agent = Manus()\n    await agent.run(\n        \"\"\"Requirement:\n1. Analyze the following data and generate a graphical data report in HTML format. The final product should be a data report.\nData:\nMonth | Team A | Team B | Team C\nJanuary | 1200 hours | 1350 hours | 1100 hours\nFebruary | 1250 hours | 1400 hours | 1150 hours\nMarch | 1180 hours | 1300 hours | 1300 hours\nApril | 1220 hours | 1280 hours | 1400 hours\nMay | 1230 hours | 1320 hours | 1450 hours\nJune | 1200 hours | 1250 hours | 1500 hours  \"\"\"\n    )\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n"
  },
  {
    "path": "app/tool/chart_visualization/tsconfig.json",
    "content": "{\n  \"include\": [\n    \"src/**/*.ts\",\n  ],\n  \"compilerOptions\": {\n    /* Visit https://aka.ms/tsconfig to read more about this file */\n    /* Projects */\n    // \"incremental\": true,                              /* Save .tsbuildinfo files to allow for incremental compilation of projects. */\n    // \"composite\": true,                                /* Enable constraints that allow a TypeScript project to be used with project references. */\n    // \"tsBuildInfoFile\": \"./.tsbuildinfo\",              /* Specify the path to .tsbuildinfo incremental compilation file. */\n    // \"disableSourceOfProjectReferenceRedirect\": true,  /* Disable preferring source files instead of declaration files when referencing composite projects. */\n    // \"disableSolutionSearching\": true,                 /* Opt a project out of multi-project reference checking when editing. */\n    // \"disableReferencedProjectLoad\": true,             /* Reduce the number of projects loaded automatically by TypeScript. */\n    /* Language and Environment */\n    \"target\": \"ES2021\", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */\n    // \"lib\": [],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */\n    // \"jsx\": \"preserve\",                                /* Specify what JSX code is generated. */\n    // \"experimentalDecorators\": true,                   /* Enable experimental support for legacy experimental decorators. */\n    // \"emitDecoratorMetadata\": true,                    /* Emit design-type metadata for decorated declarations in source files. */\n    // \"jsxFactory\": \"\",                                 /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */\n    // \"jsxFragmentFactory\": \"\",                         /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */\n    // \"jsxImportSource\": \"\",                            /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */\n    // \"reactNamespace\": \"\",                             /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */\n    // \"noLib\": true,                                    /* Disable including any library files, including the default lib.d.ts. */\n    // \"useDefineForClassFields\": true,                  /* Emit ECMAScript-standard-compliant class fields. */\n    // \"moduleDetection\": \"auto\",                        /* Control what method is used to detect module-format JS files. */\n    /* Modules */\n    \"module\": \"commonjs\", /* Specify what module code is generated. */\n    // \"rootDir\": \"./\",                                  /* Specify the root folder within your source files. */\n    \"moduleResolution\": \"node\", /* Specify how TypeScript looks up a file from a given module specifier. */\n    // \"baseUrl\": \"./\",                                  /* Specify the base directory to resolve non-relative module names. */\n    // \"paths\": {},                                      /* Specify a set of entries that re-map imports to additional lookup locations. */\n    // \"rootDirs\": [],                                   /* Allow multiple folders to be treated as one when resolving modules. */\n    \"typeRoots\": [\n      \"./node_modules/@types\",\n      \"src/types\"\n    ], /* Specify multiple folders that act like './node_modules/@types'. */\n    // \"types\": [],                                      /* Specify type package names to be included without being referenced in a source file. */\n    // \"allowUmdGlobalAccess\": true,                     /* Allow accessing UMD globals from modules. */\n    // \"moduleSuffixes\": [],                             /* List of file name suffixes to search when resolving a module. */\n    // \"allowImportingTsExtensions\": true,               /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */\n    // \"rewriteRelativeImportExtensions\": true,          /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */\n    // \"resolvePackageJsonExports\": true,                /* Use the package.json 'exports' field when resolving package imports. */\n    // \"resolvePackageJsonImports\": true,                /* Use the package.json 'imports' field when resolving imports. */\n    // \"customConditions\": [],                           /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */\n    // \"noUncheckedSideEffectImports\": true,             /* Check side effect imports. */\n    // \"resolveJsonModule\": true,                        /* Enable importing .json files. */\n    // \"allowArbitraryExtensions\": true,                 /* Enable importing files with any extension, provided a declaration file is present. */\n    // \"noResolve\": true,                                /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */\n    /* JavaScript Support */\n    \"allowJs\": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */\n    \"checkJs\": false, /* Enable error reporting in type-checked JavaScript files. */\n    // \"maxNodeModuleJsDepth\": 1,                        /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */\n    /* Emit */\n    // \"declaration\": true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */\n    // \"declarationMap\": true,                           /* Create sourcemaps for d.ts files. */\n    // \"emitDeclarationOnly\": true,                      /* Only output d.ts files and not JavaScript files. */\n    // \"sourceMap\": true,                                /* Create source map files for emitted JavaScript files. */\n    // \"inlineSourceMap\": true,                          /* Include sourcemap files inside the emitted JavaScript. */\n    // \"noEmit\": true,                                   /* Disable emitting files from a compilation. */\n    // \"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. */\n    // \"outDir\": \"./\",                                   /* Specify an output folder for all emitted files. */\n    // \"removeComments\": true,                           /* Disable emitting comments. */\n    // \"importHelpers\": true,                            /* Allow importing helper functions from tslib once per project, instead of including them per-file. */\n    // \"downlevelIteration\": true,                       /* Emit more compliant, but verbose and less performant JavaScript for iteration. */\n    // \"sourceRoot\": \"\",                                 /* Specify the root path for debuggers to find the reference source code. */\n    // \"mapRoot\": \"\",                                    /* Specify the location where debugger should locate map files instead of generated locations. */\n    // \"inlineSources\": true,                            /* Include source code in the sourcemaps inside the emitted JavaScript. */\n    // \"emitBOM\": true,                                  /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */\n    // \"newLine\": \"crlf\",                                /* Set the newline character for emitting files. */\n    // \"stripInternal\": true,                            /* Disable emitting declarations that have '@internal' in their JSDoc comments. */\n    // \"noEmitHelpers\": true,                            /* Disable generating custom helper functions like '__extends' in compiled output. */\n    // \"noEmitOnError\": true,                            /* Disable emitting files if any type checking errors are reported. */\n    // \"preserveConstEnums\": true,                       /* Disable erasing 'const enum' declarations in generated code. */\n    // \"declarationDir\": \"./\",                           /* Specify the output directory for generated declaration files. */\n    /* Interop Constraints */\n    // \"isolatedModules\": true,                          /* Ensure that each file can be safely transpiled without relying on other imports. */\n    // \"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. */\n    // \"isolatedDeclarations\": true,                     /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */\n    // \"allowSyntheticDefaultImports\": true,             /* Allow 'import x from y' when a module doesn't have a default export. */\n    \"esModuleInterop\": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */\n    // \"preserveSymlinks\": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */\n    \"forceConsistentCasingInFileNames\": true, /* Ensure that casing is correct in imports. */\n    /* Type Checking */\n    \"strict\": true, /* Enable all strict type-checking options. */\n    // \"noImplicitAny\": true,                            /* Enable error reporting for expressions and declarations with an implied 'any' type. */\n    // \"strictNullChecks\": true,                         /* When type checking, take into account 'null' and 'undefined'. */\n    // \"strictFunctionTypes\": true,                      /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */\n    // \"strictBindCallApply\": true,                      /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */\n    // \"strictPropertyInitialization\": true,             /* Check for class properties that are declared but not set in the constructor. */\n    // \"strictBuiltinIteratorReturn\": true,              /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */\n    // \"noImplicitThis\": true,                           /* Enable error reporting when 'this' is given the type 'any'. */\n    // \"useUnknownInCatchVariables\": true,               /* Default catch clause variables as 'unknown' instead of 'any'. */\n    // \"alwaysStrict\": true,                             /* Ensure 'use strict' is always emitted. */\n    // \"noUnusedLocals\": true,                           /* Enable error reporting when local variables aren't read. */\n    // \"noUnusedParameters\": true,                       /* Raise an error when a function parameter isn't read. */\n    // \"exactOptionalPropertyTypes\": true,               /* Interpret optional property types as written, rather than adding 'undefined'. */\n    // \"noImplicitReturns\": true,                        /* Enable error reporting for codepaths that do not explicitly return in a function. */\n    // \"noFallthroughCasesInSwitch\": true,               /* Enable error reporting for fallthrough cases in switch statements. */\n    // \"noUncheckedIndexedAccess\": true,                 /* Add 'undefined' to a type when accessed using an index. */\n    // \"noImplicitOverride\": true,                       /* Ensure overriding members in derived classes are marked with an override modifier. */\n    // \"noPropertyAccessFromIndexSignature\": true,       /* Enforces using indexed accessors for keys declared using an indexed type. */\n    // \"allowUnusedLabels\": true,                        /* Disable error reporting for unused labels. */\n    // \"allowUnreachableCode\": true,                     /* Disable error reporting for unreachable code. */\n    /* Completeness */\n    // \"skipDefaultLibCheck\": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */\n    \"skipLibCheck\": true /* Skip type checking all .d.ts files. */\n  }\n}\n"
  },
  {
    "path": "app/tool/computer_use_tool.py",
    "content": "import asyncio\nimport base64\nimport logging\nimport os\nimport time\nfrom typing import Dict, Literal, Optional\n\nimport aiohttp\nfrom pydantic import Field\n\nfrom app.daytona.tool_base import Sandbox, SandboxToolsBase\nfrom app.tool.base import ToolResult\n\n\nKEYBOARD_KEYS = [\n    \"a\",\n    \"b\",\n    \"c\",\n    \"d\",\n    \"e\",\n    \"f\",\n    \"g\",\n    \"h\",\n    \"i\",\n    \"j\",\n    \"k\",\n    \"l\",\n    \"m\",\n    \"n\",\n    \"o\",\n    \"p\",\n    \"q\",\n    \"r\",\n    \"s\",\n    \"t\",\n    \"u\",\n    \"v\",\n    \"w\",\n    \"x\",\n    \"y\",\n    \"z\",\n    \"0\",\n    \"1\",\n    \"2\",\n    \"3\",\n    \"4\",\n    \"5\",\n    \"6\",\n    \"7\",\n    \"8\",\n    \"9\",\n    \"enter\",\n    \"esc\",\n    \"backspace\",\n    \"tab\",\n    \"space\",\n    \"delete\",\n    \"ctrl\",\n    \"alt\",\n    \"shift\",\n    \"win\",\n    \"up\",\n    \"down\",\n    \"left\",\n    \"right\",\n    \"f1\",\n    \"f2\",\n    \"f3\",\n    \"f4\",\n    \"f5\",\n    \"f6\",\n    \"f7\",\n    \"f8\",\n    \"f9\",\n    \"f10\",\n    \"f11\",\n    \"f12\",\n    \"ctrl+c\",\n    \"ctrl+v\",\n    \"ctrl+x\",\n    \"ctrl+z\",\n    \"ctrl+a\",\n    \"ctrl+s\",\n    \"alt+tab\",\n    \"alt+f4\",\n    \"ctrl+alt+delete\",\n]\nMOUSE_BUTTONS = [\"left\", \"right\", \"middle\"]\n_COMPUTER_USE_DESCRIPTION = \"\"\"\\\nA comprehensive computer automation tool that allows interaction with the desktop environment.\n* This tool provides commands for controlling mouse, keyboard, and taking screenshots\n* It maintains state including current mouse position\n* Use this when you need to automate desktop applications, fill forms, or perform GUI interactions\nKey capabilities include:\n* Mouse Control: Move, click, drag, scroll\n* Keyboard Input: Type text, press keys or key combinations\n* Screenshots: Capture and save screen images\n* Waiting: Pause execution for specified duration\n\"\"\"\n\n\nclass ComputerUseTool(SandboxToolsBase):\n    \"\"\"Computer automation tool for controlling the desktop environment.\"\"\"\n\n    name: str = \"computer_use\"\n    description: str = _COMPUTER_USE_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\n                    \"move_to\",\n                    \"click\",\n                    \"scroll\",\n                    \"typing\",\n                    \"press\",\n                    \"wait\",\n                    \"mouse_down\",\n                    \"mouse_up\",\n                    \"drag_to\",\n                    \"hotkey\",\n                    \"screenshot\",\n                ],\n                \"description\": \"The computer action to perform\",\n            },\n            \"x\": {\"type\": \"number\", \"description\": \"X coordinate for mouse actions\"},\n            \"y\": {\"type\": \"number\", \"description\": \"Y coordinate for mouse actions\"},\n            \"button\": {\n                \"type\": \"string\",\n                \"enum\": MOUSE_BUTTONS,\n                \"description\": \"Mouse button for click/drag actions\",\n                \"default\": \"left\",\n            },\n            \"num_clicks\": {\n                \"type\": \"integer\",\n                \"description\": \"Number of clicks\",\n                \"enum\": [1, 2, 3],\n                \"default\": 1,\n            },\n            \"amount\": {\n                \"type\": \"integer\",\n                \"description\": \"Scroll amount (positive for up, negative for down)\",\n                \"minimum\": -10,\n                \"maximum\": 10,\n            },\n            \"text\": {\"type\": \"string\", \"description\": \"Text to type\"},\n            \"key\": {\n                \"type\": \"string\",\n                \"enum\": KEYBOARD_KEYS,\n                \"description\": \"Key to press\",\n            },\n            \"keys\": {\n                \"type\": \"string\",\n                \"enum\": KEYBOARD_KEYS,\n                \"description\": \"Key combination to press\",\n            },\n            \"duration\": {\n                \"type\": \"number\",\n                \"description\": \"Duration in seconds to wait\",\n                \"default\": 0.5,\n            },\n        },\n        \"required\": [\"action\"],\n        \"dependencies\": {\n            \"move_to\": [\"x\", \"y\"],\n            \"click\": [],\n            \"scroll\": [\"amount\"],\n            \"typing\": [\"text\"],\n            \"press\": [\"key\"],\n            \"wait\": [],\n            \"mouse_down\": [],\n            \"mouse_up\": [],\n            \"drag_to\": [\"x\", \"y\"],\n            \"hotkey\": [\"keys\"],\n            \"screenshot\": [],\n        },\n    }\n    session: Optional[aiohttp.ClientSession] = Field(default=None, exclude=True)\n    mouse_x: int = Field(default=0, exclude=True)\n    mouse_y: int = Field(default=0, exclude=True)\n    api_base_url: Optional[str] = Field(default=None, exclude=True)\n\n    def __init__(self, sandbox: Optional[Sandbox] = None, **data):\n        \"\"\"Initialize with optional sandbox.\"\"\"\n        super().__init__(**data)\n        if sandbox is not None:\n            self._sandbox = sandbox  # 直接操作基类的私有属性\n            self.api_base_url = sandbox.get_preview_link(8000).url\n            logging.info(\n                f\"Initialized ComputerUseTool with API URL: {self.api_base_url}\"\n            )\n\n    @classmethod\n    def create_with_sandbox(cls, sandbox: Sandbox) -> \"ComputerUseTool\":\n        \"\"\"Factory method to create a tool with sandbox.\"\"\"\n        return cls(sandbox=sandbox)  # 通过构造函数初始化\n\n    async def _get_session(self) -> aiohttp.ClientSession:\n        \"\"\"Get or create aiohttp session for API requests.\"\"\"\n        if self.session is None or self.session.closed:\n            self.session = aiohttp.ClientSession()\n        return self.session\n\n    async def _api_request(\n        self, method: str, endpoint: str, data: Optional[Dict] = None\n    ) -> Dict:\n        \"\"\"Send request to automation service API.\"\"\"\n        try:\n            session = await self._get_session()\n            url = f\"{self.api_base_url}/api{endpoint}\"\n            logging.debug(f\"API request: {method} {url} {data}\")\n            if method.upper() == \"GET\":\n                async with session.get(url) as response:\n                    result = await response.json()\n            else:  # POST\n                async with session.post(url, json=data) as response:\n                    result = await response.json()\n            logging.debug(f\"API response: {result}\")\n            return result\n        except Exception as e:\n            logging.error(f\"API request failed: {str(e)}\")\n            return {\"success\": False, \"error\": str(e)}\n\n    async def execute(\n        self,\n        action: Literal[\n            \"move_to\",\n            \"click\",\n            \"scroll\",\n            \"typing\",\n            \"press\",\n            \"wait\",\n            \"mouse_down\",\n            \"mouse_up\",\n            \"drag_to\",\n            \"hotkey\",\n            \"screenshot\",\n        ],\n        x: Optional[float] = None,\n        y: Optional[float] = None,\n        button: str = \"left\",\n        num_clicks: int = 1,\n        amount: Optional[int] = None,\n        text: Optional[str] = None,\n        key: Optional[str] = None,\n        keys: Optional[str] = None,\n        duration: float = 0.5,\n        **kwargs,\n    ) -> ToolResult:\n        \"\"\"\n        Execute a specified computer automation action.\n        Args:\n            action: The action to perform\n            x: X coordinate for mouse actions\n            y: Y coordinate for mouse actions\n            button: Mouse button for click/drag actions\n            num_clicks: Number of clicks to perform\n            amount: Scroll amount (positive for up, negative for down)\n            text: Text to type\n            key: Key to press\n            keys: Key combination to press\n            duration: Duration in seconds to wait\n            **kwargs: Additional arguments\n        Returns:\n            ToolResult with the action's output or error\n        \"\"\"\n        try:\n            if action == \"move_to\":\n                if x is None or y is None:\n                    return ToolResult(error=\"x and y coordinates are required\")\n                x_int = int(round(float(x)))\n                y_int = int(round(float(y)))\n                result = await self._api_request(\n                    \"POST\", \"/automation/mouse/move\", {\"x\": x_int, \"y\": y_int}\n                )\n                if result.get(\"success\", False):\n                    self.mouse_x = x_int\n                    self.mouse_y = y_int\n                    return ToolResult(output=f\"Moved to ({x_int}, {y_int})\")\n                else:\n                    return ToolResult(\n                        error=f\"Failed to move: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"click\":\n                x_val = x if x is not None else self.mouse_x\n                y_val = y if y is not None else self.mouse_y\n                x_int = int(round(float(x_val)))\n                y_int = int(round(float(y_val)))\n                num_clicks = int(num_clicks)\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/mouse/click\",\n                    {\n                        \"x\": x_int,\n                        \"y\": y_int,\n                        \"clicks\": num_clicks,\n                        \"button\": button.lower(),\n                    },\n                )\n                if result.get(\"success\", False):\n                    self.mouse_x = x_int\n                    self.mouse_y = y_int\n                    return ToolResult(\n                        output=f\"{num_clicks} {button} click(s) performed at ({x_int}, {y_int})\"\n                    )\n                else:\n                    return ToolResult(\n                        error=f\"Failed to click: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"scroll\":\n                if amount is None:\n                    return ToolResult(error=\"Scroll amount is required\")\n                amount = int(float(amount))\n                amount = max(-10, min(10, amount))\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/mouse/scroll\",\n                    {\"clicks\": amount, \"x\": self.mouse_x, \"y\": self.mouse_y},\n                )\n                if result.get(\"success\", False):\n                    direction = \"up\" if amount > 0 else \"down\"\n                    steps = abs(amount)\n                    return ToolResult(\n                        output=f\"Scrolled {direction} {steps} step(s) at position ({self.mouse_x}, {self.mouse_y})\"\n                    )\n                else:\n                    return ToolResult(\n                        error=f\"Failed to scroll: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"typing\":\n                if text is None:\n                    return ToolResult(error=\"Text is required for typing\")\n                text = str(text)\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/keyboard/write\",\n                    {\"message\": text, \"interval\": 0.01},\n                )\n                if result.get(\"success\", False):\n                    return ToolResult(output=f\"Typed: {text}\")\n                else:\n                    return ToolResult(\n                        error=f\"Failed to type: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"press\":\n                if key is None:\n                    return ToolResult(error=\"Key is required for press action\")\n                key = str(key).lower()\n                result = await self._api_request(\n                    \"POST\", \"/automation/keyboard/press\", {\"keys\": key, \"presses\": 1}\n                )\n                if result.get(\"success\", False):\n                    return ToolResult(output=f\"Pressed key: {key}\")\n                else:\n                    return ToolResult(\n                        error=f\"Failed to press key: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"wait\":\n                duration = float(duration)\n                duration = max(0, min(10, duration))\n                await asyncio.sleep(duration)\n                return ToolResult(output=f\"Waited {duration} seconds\")\n            elif action == \"mouse_down\":\n                x_val = x if x is not None else self.mouse_x\n                y_val = y if y is not None else self.mouse_y\n                x_int = int(round(float(x_val)))\n                y_int = int(round(float(y_val)))\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/mouse/down\",\n                    {\"x\": x_int, \"y\": y_int, \"button\": button.lower()},\n                )\n                if result.get(\"success\", False):\n                    self.mouse_x = x_int\n                    self.mouse_y = y_int\n                    return ToolResult(\n                        output=f\"{button} button pressed at ({x_int}, {y_int})\"\n                    )\n                else:\n                    return ToolResult(\n                        error=f\"Failed to press button: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"mouse_up\":\n                x_val = x if x is not None else self.mouse_x\n                y_val = y if y is not None else self.mouse_y\n                x_int = int(round(float(x_val)))\n                y_int = int(round(float(y_val)))\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/mouse/up\",\n                    {\"x\": x_int, \"y\": y_int, \"button\": button.lower()},\n                )\n                if result.get(\"success\", False):\n                    self.mouse_x = x_int\n                    self.mouse_y = y_int\n                    return ToolResult(\n                        output=f\"{button} button released at ({x_int}, {y_int})\"\n                    )\n                else:\n                    return ToolResult(\n                        error=f\"Failed to release button: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"drag_to\":\n                if x is None or y is None:\n                    return ToolResult(error=\"x and y coordinates are required\")\n                target_x = int(round(float(x)))\n                target_y = int(round(float(y)))\n                start_x = self.mouse_x\n                start_y = self.mouse_y\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/mouse/drag\",\n                    {\"x\": target_x, \"y\": target_y, \"duration\": 0.3, \"button\": \"left\"},\n                )\n                if result.get(\"success\", False):\n                    self.mouse_x = target_x\n                    self.mouse_y = target_y\n                    return ToolResult(\n                        output=f\"Dragged from ({start_x}, {start_y}) to ({target_x}, {target_y})\"\n                    )\n                else:\n                    return ToolResult(\n                        error=f\"Failed to drag: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"hotkey\":\n                if keys is None:\n                    return ToolResult(error=\"Keys are required for hotkey action\")\n                keys = str(keys).lower().strip()\n                key_sequence = keys.split(\"+\")\n                result = await self._api_request(\n                    \"POST\",\n                    \"/automation/keyboard/hotkey\",\n                    {\"keys\": key_sequence, \"interval\": 0.01},\n                )\n                if result.get(\"success\", False):\n                    return ToolResult(output=f\"Pressed key combination: {keys}\")\n                else:\n                    return ToolResult(\n                        error=f\"Failed to press keys: {result.get('error', 'Unknown error')}\"\n                    )\n            elif action == \"screenshot\":\n                result = await self._api_request(\"POST\", \"/automation/screenshot\")\n                if \"image\" in result:\n                    base64_str = result[\"image\"]\n                    timestamp = time.strftime(\"%Y%m%d_%H%M%S\")\n                    # Save screenshot to file\n                    screenshots_dir = \"screenshots\"\n                    if not os.path.exists(screenshots_dir):\n                        os.makedirs(screenshots_dir)\n                    timestamped_filename = os.path.join(\n                        screenshots_dir, f\"screenshot_{timestamp}.png\"\n                    )\n                    latest_filename = \"latest_screenshot.png\"\n                    # Decode base64 string and save to file\n                    img_data = base64.b64decode(base64_str)\n                    with open(timestamped_filename, \"wb\") as f:\n                        f.write(img_data)\n                    # Save a copy as the latest screenshot\n                    with open(latest_filename, \"wb\") as f:\n                        f.write(img_data)\n                    return ToolResult(\n                        output=f\"Screenshot saved as {timestamped_filename}\",\n                        base64_image=base64_str,\n                    )\n                else:\n                    return ToolResult(error=\"Failed to capture screenshot\")\n            else:\n                return ToolResult(error=f\"Unknown action: {action}\")\n        except Exception as e:\n            return ToolResult(error=f\"Computer action failed: {str(e)}\")\n\n    async def cleanup(self):\n        \"\"\"Clean up resources.\"\"\"\n        if self.session and not self.session.closed:\n            await self.session.close()\n            self.session = None\n\n    def __del__(self):\n        \"\"\"Ensure cleanup on destruction.\"\"\"\n        if hasattr(self, \"session\") and self.session is not None:\n            try:\n                asyncio.run(self.cleanup())\n            except RuntimeError:\n                loop = asyncio.new_event_loop()\n                loop.run_until_complete(self.cleanup())\n                loop.close()\n"
  },
  {
    "path": "app/tool/crawl4ai.py",
    "content": "\"\"\"\nCrawl4AI Web Crawler Tool for OpenManus\n\nThis tool integrates Crawl4AI, a high-performance web crawler designed for LLMs and AI agents,\nproviding fast, precise, and AI-ready data extraction with clean Markdown generation.\n\"\"\"\n\nimport asyncio\nfrom typing import List, Union\nfrom urllib.parse import urlparse\n\nfrom app.logger import logger\nfrom app.tool.base import BaseTool, ToolResult\n\n\nclass Crawl4aiTool(BaseTool):\n    \"\"\"\n    Web crawler tool powered by Crawl4AI.\n\n    Provides clean markdown extraction optimized for AI processing.\n    \"\"\"\n\n    name: str = \"crawl4ai\"\n    description: str = \"\"\"Web crawler that extracts clean, AI-ready content from web pages.\n\n    Features:\n    - Extracts clean markdown content optimized for LLMs\n    - Handles JavaScript-heavy sites and dynamic content\n    - Supports multiple URLs in a single request\n    - Fast and reliable with built-in error handling\n\n    Perfect for content analysis, research, and feeding web content to AI models.\"\"\"\n\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"urls\": {\n                \"type\": \"array\",\n                \"items\": {\"type\": \"string\"},\n                \"description\": \"(required) List of URLs to crawl. Can be a single URL or multiple URLs.\",\n                \"minItems\": 1,\n            },\n            \"timeout\": {\n                \"type\": \"integer\",\n                \"description\": \"(optional) Timeout in seconds for each URL. Default is 30.\",\n                \"default\": 30,\n                \"minimum\": 5,\n                \"maximum\": 120,\n            },\n            \"bypass_cache\": {\n                \"type\": \"boolean\",\n                \"description\": \"(optional) Whether to bypass cache and fetch fresh content. Default is false.\",\n                \"default\": False,\n            },\n            \"word_count_threshold\": {\n                \"type\": \"integer\",\n                \"description\": \"(optional) Minimum word count for content blocks. Default is 10.\",\n                \"default\": 10,\n                \"minimum\": 1,\n            },\n        },\n        \"required\": [\"urls\"],\n    }\n\n    async def execute(\n        self,\n        urls: Union[str, List[str]],\n        timeout: int = 30,\n        bypass_cache: bool = False,\n        word_count_threshold: int = 10,\n    ) -> ToolResult:\n        \"\"\"\n        Execute web crawling for the specified URLs.\n\n        Args:\n            urls: Single URL string or list of URLs to crawl\n            timeout: Timeout in seconds for each URL\n            bypass_cache: Whether to bypass cache\n            word_count_threshold: Minimum word count for content blocks\n\n        Returns:\n            ToolResult with crawl results\n        \"\"\"\n        # Normalize URLs to list\n        if isinstance(urls, str):\n            url_list = [urls]\n        else:\n            url_list = urls\n\n        # Validate URLs\n        valid_urls = []\n        for url in url_list:\n            if self._is_valid_url(url):\n                valid_urls.append(url)\n            else:\n                logger.warning(f\"Invalid URL skipped: {url}\")\n\n        if not valid_urls:\n            return ToolResult(error=\"No valid URLs provided\")\n\n        try:\n            # Import crawl4ai components\n            from crawl4ai import (\n                AsyncWebCrawler,\n                BrowserConfig,\n                CacheMode,\n                CrawlerRunConfig,\n            )\n\n            # Configure browser settings\n            browser_config = BrowserConfig(\n                headless=True,\n                verbose=False,\n                browser_type=\"chromium\",\n                ignore_https_errors=True,\n                java_script_enabled=True,\n            )\n\n            # Configure crawler settings\n            run_config = CrawlerRunConfig(\n                cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED,\n                word_count_threshold=word_count_threshold,\n                process_iframes=True,\n                remove_overlay_elements=True,\n                excluded_tags=[\"script\", \"style\"],\n                page_timeout=timeout * 1000,  # Convert to milliseconds\n                verbose=False,\n                wait_until=\"domcontentloaded\",\n            )\n\n            results = []\n            successful_count = 0\n            failed_count = 0\n\n            # Process each URL\n            async with AsyncWebCrawler(config=browser_config) as crawler:\n                for url in valid_urls:\n                    try:\n                        logger.info(f\"🕷️ Crawling URL: {url}\")\n                        start_time = asyncio.get_event_loop().time()\n\n                        result = await crawler.arun(url=url, config=run_config)\n\n                        end_time = asyncio.get_event_loop().time()\n                        execution_time = end_time - start_time\n\n                        if result.success:\n                            # Count words in markdown\n                            word_count = 0\n                            if hasattr(result, \"markdown\") and result.markdown:\n                                word_count = len(result.markdown.split())\n\n                            # Count links\n                            links_count = 0\n                            if hasattr(result, \"links\") and result.links:\n                                internal_links = result.links.get(\"internal\", [])\n                                external_links = result.links.get(\"external\", [])\n                                links_count = len(internal_links) + len(external_links)\n\n                            # Count images\n                            images_count = 0\n                            if hasattr(result, \"media\") and result.media:\n                                images = result.media.get(\"images\", [])\n                                images_count = len(images)\n\n                            results.append(\n                                {\n                                    \"url\": url,\n                                    \"success\": True,\n                                    \"status_code\": getattr(result, \"status_code\", 200),\n                                    \"title\": result.metadata.get(\"title\")\n                                    if result.metadata\n                                    else None,\n                                    \"markdown\": result.markdown\n                                    if hasattr(result, \"markdown\")\n                                    else None,\n                                    \"word_count\": word_count,\n                                    \"links_count\": links_count,\n                                    \"images_count\": images_count,\n                                    \"execution_time\": execution_time,\n                                }\n                            )\n                            successful_count += 1\n                            logger.info(\n                                f\"✅ Successfully crawled {url} in {execution_time:.2f}s\"\n                            )\n\n                        else:\n                            results.append(\n                                {\n                                    \"url\": url,\n                                    \"success\": False,\n                                    \"error_message\": getattr(\n                                        result, \"error_message\", \"Unknown error\"\n                                    ),\n                                    \"execution_time\": execution_time,\n                                }\n                            )\n                            failed_count += 1\n                            logger.warning(f\"❌ Failed to crawl {url}\")\n\n                    except Exception as e:\n                        error_msg = f\"Error crawling {url}: {str(e)}\"\n                        logger.error(error_msg)\n                        results.append(\n                            {\"url\": url, \"success\": False, \"error_message\": error_msg}\n                        )\n                        failed_count += 1\n\n            # Format output\n            output_lines = [f\"🕷️ Crawl4AI Results Summary:\"]\n            output_lines.append(f\"📊 Total URLs: {len(valid_urls)}\")\n            output_lines.append(f\"✅ Successful: {successful_count}\")\n            output_lines.append(f\"❌ Failed: {failed_count}\")\n            output_lines.append(\"\")\n\n            for i, result in enumerate(results, 1):\n                output_lines.append(f\"{i}. {result['url']}\")\n\n                if result[\"success\"]:\n                    output_lines.append(\n                        f\"   ✅ Status: Success (HTTP {result.get('status_code', 'N/A')})\"\n                    )\n                    if result.get(\"title\"):\n                        output_lines.append(f\"   📄 Title: {result['title']}\")\n\n                    if result.get(\"markdown\"):\n                        # Show first 300 characters of markdown content\n                        content_preview = result[\"markdown\"]\n                        if len(result[\"markdown\"]) > 300:\n                            content_preview += \"...\"\n                        output_lines.append(f\"   📝 Content: {content_preview}\")\n\n                    output_lines.append(\n                        f\"   📊 Stats: {result.get('word_count', 0)} words, {result.get('links_count', 0)} links, {result.get('images_count', 0)} images\"\n                    )\n\n                    if result.get(\"execution_time\"):\n                        output_lines.append(\n                            f\"   ⏱️ Time: {result['execution_time']:.2f}s\"\n                        )\n                else:\n                    output_lines.append(f\"   ❌ Status: Failed\")\n                    if result.get(\"error_message\"):\n                        output_lines.append(f\"   🚫 Error: {result['error_message']}\")\n\n                output_lines.append(\"\")\n\n            return ToolResult(output=\"\\n\".join(output_lines))\n\n        except ImportError:\n            error_msg = \"Crawl4AI is not installed. Please install it with: pip install crawl4ai\"\n            logger.error(error_msg)\n            return ToolResult(error=error_msg)\n        except Exception as e:\n            error_msg = f\"Crawl4AI execution failed: {str(e)}\"\n            logger.error(error_msg)\n            return ToolResult(error=error_msg)\n\n    def _is_valid_url(self, url: str) -> bool:\n        \"\"\"Validate if a URL is properly formatted.\"\"\"\n        try:\n            result = urlparse(url)\n            return all([result.scheme, result.netloc]) and result.scheme in [\n                \"http\",\n                \"https\",\n            ]\n        except Exception:\n            return False\n"
  },
  {
    "path": "app/tool/create_chat_completion.py",
    "content": "from typing import Any, List, Optional, Type, Union, get_args, get_origin\n\nfrom pydantic import BaseModel, Field\n\nfrom app.tool import BaseTool\n\n\nclass CreateChatCompletion(BaseTool):\n    name: str = \"create_chat_completion\"\n    description: str = (\n        \"Creates a structured completion with specified output formatting.\"\n    )\n\n    # Type mapping for JSON schema\n    type_mapping: dict = {\n        str: \"string\",\n        int: \"integer\",\n        float: \"number\",\n        bool: \"boolean\",\n        dict: \"object\",\n        list: \"array\",\n    }\n    response_type: Optional[Type] = None\n    required: List[str] = Field(default_factory=lambda: [\"response\"])\n\n    def __init__(self, response_type: Optional[Type] = str):\n        \"\"\"Initialize with a specific response type.\"\"\"\n        super().__init__()\n        self.response_type = response_type\n        self.parameters = self._build_parameters()\n\n    def _build_parameters(self) -> dict:\n        \"\"\"Build parameters schema based on response type.\"\"\"\n        if self.response_type == str:\n            return {\n                \"type\": \"object\",\n                \"properties\": {\n                    \"response\": {\n                        \"type\": \"string\",\n                        \"description\": \"The response text that should be delivered to the user.\",\n                    },\n                },\n                \"required\": self.required,\n            }\n\n        if isinstance(self.response_type, type) and issubclass(\n            self.response_type, BaseModel\n        ):\n            schema = self.response_type.model_json_schema()\n            return {\n                \"type\": \"object\",\n                \"properties\": schema[\"properties\"],\n                \"required\": schema.get(\"required\", self.required),\n            }\n\n        return self._create_type_schema(self.response_type)\n\n    def _create_type_schema(self, type_hint: Type) -> dict:\n        \"\"\"Create a JSON schema for the given type.\"\"\"\n        origin = get_origin(type_hint)\n        args = get_args(type_hint)\n\n        # Handle primitive types\n        if origin is None:\n            return {\n                \"type\": \"object\",\n                \"properties\": {\n                    \"response\": {\n                        \"type\": self.type_mapping.get(type_hint, \"string\"),\n                        \"description\": f\"Response of type {type_hint.__name__}\",\n                    }\n                },\n                \"required\": self.required,\n            }\n\n        # Handle List type\n        if origin is list:\n            item_type = args[0] if args else Any\n            return {\n                \"type\": \"object\",\n                \"properties\": {\n                    \"response\": {\n                        \"type\": \"array\",\n                        \"items\": self._get_type_info(item_type),\n                    }\n                },\n                \"required\": self.required,\n            }\n\n        # Handle Dict type\n        if origin is dict:\n            value_type = args[1] if len(args) > 1 else Any\n            return {\n                \"type\": \"object\",\n                \"properties\": {\n                    \"response\": {\n                        \"type\": \"object\",\n                        \"additionalProperties\": self._get_type_info(value_type),\n                    }\n                },\n                \"required\": self.required,\n            }\n\n        # Handle Union type\n        if origin is Union:\n            return self._create_union_schema(args)\n\n        return self._build_parameters()\n\n    def _get_type_info(self, type_hint: Type) -> dict:\n        \"\"\"Get type information for a single type.\"\"\"\n        if isinstance(type_hint, type) and issubclass(type_hint, BaseModel):\n            return type_hint.model_json_schema()\n\n        return {\n            \"type\": self.type_mapping.get(type_hint, \"string\"),\n            \"description\": f\"Value of type {getattr(type_hint, '__name__', 'any')}\",\n        }\n\n    def _create_union_schema(self, types: tuple) -> dict:\n        \"\"\"Create schema for Union types.\"\"\"\n        return {\n            \"type\": \"object\",\n            \"properties\": {\n                \"response\": {\"anyOf\": [self._get_type_info(t) for t in types]}\n            },\n            \"required\": self.required,\n        }\n\n    async def execute(self, required: list | None = None, **kwargs) -> Any:\n        \"\"\"Execute the chat completion with type conversion.\n\n        Args:\n            required: List of required field names or None\n            **kwargs: Response data\n\n        Returns:\n            Converted response based on response_type\n        \"\"\"\n        required = required or self.required\n\n        # Handle case when required is a list\n        if isinstance(required, list) and len(required) > 0:\n            if len(required) == 1:\n                required_field = required[0]\n                result = kwargs.get(required_field, \"\")\n            else:\n                # Return multiple fields as a dictionary\n                return {field: kwargs.get(field, \"\") for field in required}\n        else:\n            required_field = \"response\"\n            result = kwargs.get(required_field, \"\")\n\n        # Type conversion logic\n        if self.response_type == str:\n            return result\n\n        if isinstance(self.response_type, type) and issubclass(\n            self.response_type, BaseModel\n        ):\n            return self.response_type(**kwargs)\n\n        if get_origin(self.response_type) in (list, dict):\n            return result  # Assuming result is already in correct format\n\n        try:\n            return self.response_type(result)\n        except (ValueError, TypeError):\n            return result\n"
  },
  {
    "path": "app/tool/file_operators.py",
    "content": "\"\"\"File operation interfaces and implementations for local and sandbox environments.\"\"\"\n\nimport asyncio\nfrom pathlib import Path\nfrom typing import Optional, Protocol, Tuple, Union, runtime_checkable\n\nfrom app.config import SandboxSettings\nfrom app.exceptions import ToolError\nfrom app.sandbox.client import SANDBOX_CLIENT\n\n\nPathLike = Union[str, Path]\n\n\n@runtime_checkable\nclass FileOperator(Protocol):\n    \"\"\"Interface for file operations in different environments.\"\"\"\n\n    async def read_file(self, path: PathLike) -> str:\n        \"\"\"Read content from a file.\"\"\"\n        ...\n\n    async def write_file(self, path: PathLike, content: str) -> None:\n        \"\"\"Write content to a file.\"\"\"\n        ...\n\n    async def is_directory(self, path: PathLike) -> bool:\n        \"\"\"Check if path points to a directory.\"\"\"\n        ...\n\n    async def exists(self, path: PathLike) -> bool:\n        \"\"\"Check if path exists.\"\"\"\n        ...\n\n    async def run_command(\n        self, cmd: str, timeout: Optional[float] = 120.0\n    ) -> Tuple[int, str, str]:\n        \"\"\"Run a shell command and return (return_code, stdout, stderr).\"\"\"\n        ...\n\n\nclass LocalFileOperator(FileOperator):\n    \"\"\"File operations implementation for local filesystem.\"\"\"\n\n    encoding: str = \"utf-8\"\n\n    async def read_file(self, path: PathLike) -> str:\n        \"\"\"Read content from a local file.\"\"\"\n        try:\n            return Path(path).read_text(encoding=self.encoding)\n        except Exception as e:\n            raise ToolError(f\"Failed to read {path}: {str(e)}\") from None\n\n    async def write_file(self, path: PathLike, content: str) -> None:\n        \"\"\"Write content to a local file.\"\"\"\n        try:\n            Path(path).write_text(content, encoding=self.encoding)\n        except Exception as e:\n            raise ToolError(f\"Failed to write to {path}: {str(e)}\") from None\n\n    async def is_directory(self, path: PathLike) -> bool:\n        \"\"\"Check if path points to a directory.\"\"\"\n        return Path(path).is_dir()\n\n    async def exists(self, path: PathLike) -> bool:\n        \"\"\"Check if path exists.\"\"\"\n        return Path(path).exists()\n\n    async def run_command(\n        self, cmd: str, timeout: Optional[float] = 120.0\n    ) -> Tuple[int, str, str]:\n        \"\"\"Run a shell command locally.\"\"\"\n        process = await asyncio.create_subprocess_shell(\n            cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE\n        )\n\n        try:\n            stdout, stderr = await asyncio.wait_for(\n                process.communicate(), timeout=timeout\n            )\n            return (\n                process.returncode or 0,\n                stdout.decode(),\n                stderr.decode(),\n            )\n        except asyncio.TimeoutError as exc:\n            try:\n                process.kill()\n            except ProcessLookupError:\n                pass\n            raise TimeoutError(\n                f\"Command '{cmd}' timed out after {timeout} seconds\"\n            ) from exc\n\n\nclass SandboxFileOperator(FileOperator):\n    \"\"\"File operations implementation for sandbox environment.\"\"\"\n\n    def __init__(self):\n        self.sandbox_client = SANDBOX_CLIENT\n\n    async def _ensure_sandbox_initialized(self):\n        \"\"\"Ensure sandbox is initialized.\"\"\"\n        if not self.sandbox_client.sandbox:\n            await self.sandbox_client.create(config=SandboxSettings())\n\n    async def read_file(self, path: PathLike) -> str:\n        \"\"\"Read content from a file in sandbox.\"\"\"\n        await self._ensure_sandbox_initialized()\n        try:\n            return await self.sandbox_client.read_file(str(path))\n        except Exception as e:\n            raise ToolError(f\"Failed to read {path} in sandbox: {str(e)}\") from None\n\n    async def write_file(self, path: PathLike, content: str) -> None:\n        \"\"\"Write content to a file in sandbox.\"\"\"\n        await self._ensure_sandbox_initialized()\n        try:\n            await self.sandbox_client.write_file(str(path), content)\n        except Exception as e:\n            raise ToolError(f\"Failed to write to {path} in sandbox: {str(e)}\") from None\n\n    async def is_directory(self, path: PathLike) -> bool:\n        \"\"\"Check if path points to a directory in sandbox.\"\"\"\n        await self._ensure_sandbox_initialized()\n        result = await self.sandbox_client.run_command(\n            f\"test -d {path} && echo 'true' || echo 'false'\"\n        )\n        return result.strip() == \"true\"\n\n    async def exists(self, path: PathLike) -> bool:\n        \"\"\"Check if path exists in sandbox.\"\"\"\n        await self._ensure_sandbox_initialized()\n        result = await self.sandbox_client.run_command(\n            f\"test -e {path} && echo 'true' || echo 'false'\"\n        )\n        return result.strip() == \"true\"\n\n    async def run_command(\n        self, cmd: str, timeout: Optional[float] = 120.0\n    ) -> Tuple[int, str, str]:\n        \"\"\"Run a command in sandbox environment.\"\"\"\n        await self._ensure_sandbox_initialized()\n        try:\n            stdout = await self.sandbox_client.run_command(\n                cmd, timeout=int(timeout) if timeout else None\n            )\n            return (\n                0,  # Always return 0 since we don't have explicit return code from sandbox\n                stdout,\n                \"\",  # No stderr capture in the current sandbox implementation\n            )\n        except TimeoutError as exc:\n            raise TimeoutError(\n                f\"Command '{cmd}' timed out after {timeout} seconds in sandbox\"\n            ) from exc\n        except Exception as exc:\n            return 1, \"\", f\"Error executing command in sandbox: {str(exc)}\"\n"
  },
  {
    "path": "app/tool/mcp.py",
    "content": "from contextlib import AsyncExitStack\nfrom typing import Dict, List, Optional\n\nfrom mcp import ClientSession, StdioServerParameters\nfrom mcp.client.sse import sse_client\nfrom mcp.client.stdio import stdio_client\nfrom mcp.types import ListToolsResult, TextContent\n\nfrom app.logger import logger\nfrom app.tool.base import BaseTool, ToolResult\nfrom app.tool.tool_collection import ToolCollection\n\n\nclass MCPClientTool(BaseTool):\n    \"\"\"Represents a tool proxy that can be called on the MCP server from the client side.\"\"\"\n\n    session: Optional[ClientSession] = None\n    server_id: str = \"\"  # Add server identifier\n    original_name: str = \"\"\n\n    async def execute(self, **kwargs) -> ToolResult:\n        \"\"\"Execute the tool by making a remote call to the MCP server.\"\"\"\n        if not self.session:\n            return ToolResult(error=\"Not connected to MCP server\")\n\n        try:\n            logger.info(f\"Executing tool: {self.original_name}\")\n            result = await self.session.call_tool(self.original_name, kwargs)\n            content_str = \", \".join(\n                item.text for item in result.content if isinstance(item, TextContent)\n            )\n            return ToolResult(output=content_str or \"No output returned.\")\n        except Exception as e:\n            return ToolResult(error=f\"Error executing tool: {str(e)}\")\n\n\nclass MCPClients(ToolCollection):\n    \"\"\"\n    A collection of tools that connects to multiple MCP servers and manages available tools through the Model Context Protocol.\n    \"\"\"\n\n    sessions: Dict[str, ClientSession] = {}\n    exit_stacks: Dict[str, AsyncExitStack] = {}\n    description: str = \"MCP client tools for server interaction\"\n\n    def __init__(self):\n        super().__init__()  # Initialize with empty tools list\n        self.name = \"mcp\"  # Keep name for backward compatibility\n\n    async def connect_sse(self, server_url: str, server_id: str = \"\") -> None:\n        \"\"\"Connect to an MCP server using SSE transport.\"\"\"\n        if not server_url:\n            raise ValueError(\"Server URL is required.\")\n\n        server_id = server_id or server_url\n\n        # Always ensure clean disconnection before new connection\n        if server_id in self.sessions:\n            await self.disconnect(server_id)\n\n        exit_stack = AsyncExitStack()\n        self.exit_stacks[server_id] = exit_stack\n\n        streams_context = sse_client(url=server_url)\n        streams = await exit_stack.enter_async_context(streams_context)\n        session = await exit_stack.enter_async_context(ClientSession(*streams))\n        self.sessions[server_id] = session\n\n        await self._initialize_and_list_tools(server_id)\n\n    async def connect_stdio(\n        self, command: str, args: List[str], server_id: str = \"\"\n    ) -> None:\n        \"\"\"Connect to an MCP server using stdio transport.\"\"\"\n        if not command:\n            raise ValueError(\"Server command is required.\")\n\n        server_id = server_id or command\n\n        # Always ensure clean disconnection before new connection\n        if server_id in self.sessions:\n            await self.disconnect(server_id)\n\n        exit_stack = AsyncExitStack()\n        self.exit_stacks[server_id] = exit_stack\n\n        server_params = StdioServerParameters(command=command, args=args)\n        stdio_transport = await exit_stack.enter_async_context(\n            stdio_client(server_params)\n        )\n        read, write = stdio_transport\n        session = await exit_stack.enter_async_context(ClientSession(read, write))\n        self.sessions[server_id] = session\n\n        await self._initialize_and_list_tools(server_id)\n\n    async def _initialize_and_list_tools(self, server_id: str) -> None:\n        \"\"\"Initialize session and populate tool map.\"\"\"\n        session = self.sessions.get(server_id)\n        if not session:\n            raise RuntimeError(f\"Session not initialized for server {server_id}\")\n\n        await session.initialize()\n        response = await session.list_tools()\n\n        # Create proper tool objects for each server tool\n        for tool in response.tools:\n            original_name = tool.name\n            tool_name = f\"mcp_{server_id}_{original_name}\"\n            tool_name = self._sanitize_tool_name(tool_name)\n\n            server_tool = MCPClientTool(\n                name=tool_name,\n                description=tool.description,\n                parameters=tool.inputSchema,\n                session=session,\n                server_id=server_id,\n                original_name=original_name,\n            )\n            self.tool_map[tool_name] = server_tool\n\n        # Update tools tuple\n        self.tools = tuple(self.tool_map.values())\n        logger.info(\n            f\"Connected to server {server_id} with tools: {[tool.name for tool in response.tools]}\"\n        )\n\n    def _sanitize_tool_name(self, name: str) -> str:\n        \"\"\"Sanitize tool name to match MCPClientTool requirements.\"\"\"\n        import re\n\n        # Replace invalid characters with underscores\n        sanitized = re.sub(r\"[^a-zA-Z0-9_-]\", \"_\", name)\n\n        # Remove consecutive underscores\n        sanitized = re.sub(r\"_+\", \"_\", sanitized)\n\n        # Remove leading/trailing underscores\n        sanitized = sanitized.strip(\"_\")\n\n        # Truncate to 64 characters if needed\n        if len(sanitized) > 64:\n            sanitized = sanitized[:64]\n\n        return sanitized\n\n    async def list_tools(self) -> ListToolsResult:\n        \"\"\"List all available tools.\"\"\"\n        tools_result = ListToolsResult(tools=[])\n        for session in self.sessions.values():\n            response = await session.list_tools()\n            tools_result.tools += response.tools\n        return tools_result\n\n    async def disconnect(self, server_id: str = \"\") -> None:\n        \"\"\"Disconnect from a specific MCP server or all servers if no server_id provided.\"\"\"\n        if server_id:\n            if server_id in self.sessions:\n                try:\n                    exit_stack = self.exit_stacks.get(server_id)\n\n                    # Close the exit stack which will handle session cleanup\n                    if exit_stack:\n                        try:\n                            await exit_stack.aclose()\n                        except RuntimeError as e:\n                            if \"cancel scope\" in str(e).lower():\n                                logger.warning(\n                                    f\"Cancel scope error during disconnect from {server_id}, continuing with cleanup: {e}\"\n                                )\n                            else:\n                                raise\n\n                    # Clean up references\n                    self.sessions.pop(server_id, None)\n                    self.exit_stacks.pop(server_id, None)\n\n                    # Remove tools associated with this server\n                    self.tool_map = {\n                        k: v\n                        for k, v in self.tool_map.items()\n                        if v.server_id != server_id\n                    }\n                    self.tools = tuple(self.tool_map.values())\n                    logger.info(f\"Disconnected from MCP server {server_id}\")\n                except Exception as e:\n                    logger.error(f\"Error disconnecting from server {server_id}: {e}\")\n        else:\n            # Disconnect from all servers in a deterministic order\n            for sid in sorted(list(self.sessions.keys())):\n                await self.disconnect(sid)\n            self.tool_map = {}\n            self.tools = tuple()\n            logger.info(\"Disconnected from all MCP servers\")\n"
  },
  {
    "path": "app/tool/planning.py",
    "content": "# tool/planning.py\nfrom typing import Dict, List, Literal, Optional\n\nfrom app.exceptions import ToolError\nfrom app.tool.base import BaseTool, ToolResult\n\n\n_PLANNING_TOOL_DESCRIPTION = \"\"\"\nA planning tool that allows the agent to create and manage plans for solving complex tasks.\nThe tool provides functionality for creating plans, updating plan steps, and tracking progress.\n\"\"\"\n\n\nclass PlanningTool(BaseTool):\n    \"\"\"\n    A planning tool that allows the agent to create and manage plans for solving complex tasks.\n    The tool provides functionality for creating plans, updating plan steps, and tracking progress.\n    \"\"\"\n\n    name: str = \"planning\"\n    description: str = _PLANNING_TOOL_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"command\": {\n                \"description\": \"The command to execute. Available commands: create, update, list, get, set_active, mark_step, delete.\",\n                \"enum\": [\n                    \"create\",\n                    \"update\",\n                    \"list\",\n                    \"get\",\n                    \"set_active\",\n                    \"mark_step\",\n                    \"delete\",\n                ],\n                \"type\": \"string\",\n            },\n            \"plan_id\": {\n                \"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).\",\n                \"type\": \"string\",\n            },\n            \"title\": {\n                \"description\": \"Title for the plan. Required for create command, optional for update command.\",\n                \"type\": \"string\",\n            },\n            \"steps\": {\n                \"description\": \"List of plan steps. Required for create command, optional for update command.\",\n                \"type\": \"array\",\n                \"items\": {\"type\": \"string\"},\n            },\n            \"step_index\": {\n                \"description\": \"Index of the step to update (0-based). Required for mark_step command.\",\n                \"type\": \"integer\",\n            },\n            \"step_status\": {\n                \"description\": \"Status to set for a step. Used with mark_step command.\",\n                \"enum\": [\"not_started\", \"in_progress\", \"completed\", \"blocked\"],\n                \"type\": \"string\",\n            },\n            \"step_notes\": {\n                \"description\": \"Additional notes for a step. Optional for mark_step command.\",\n                \"type\": \"string\",\n            },\n        },\n        \"required\": [\"command\"],\n        \"additionalProperties\": False,\n    }\n\n    plans: dict = {}  # Dictionary to store plans by plan_id\n    _current_plan_id: Optional[str] = None  # Track the current active plan\n\n    async def execute(\n        self,\n        *,\n        command: Literal[\n            \"create\", \"update\", \"list\", \"get\", \"set_active\", \"mark_step\", \"delete\"\n        ],\n        plan_id: Optional[str] = None,\n        title: Optional[str] = None,\n        steps: Optional[List[str]] = None,\n        step_index: Optional[int] = None,\n        step_status: Optional[\n            Literal[\"not_started\", \"in_progress\", \"completed\", \"blocked\"]\n        ] = None,\n        step_notes: Optional[str] = None,\n        **kwargs,\n    ):\n        \"\"\"\n        Execute the planning tool with the given command and parameters.\n\n        Parameters:\n        - command: The operation to perform\n        - plan_id: Unique identifier for the plan\n        - title: Title for the plan (used with create command)\n        - steps: List of steps for the plan (used with create command)\n        - step_index: Index of the step to update (used with mark_step command)\n        - step_status: Status to set for a step (used with mark_step command)\n        - step_notes: Additional notes for a step (used with mark_step command)\n        \"\"\"\n\n        if command == \"create\":\n            return self._create_plan(plan_id, title, steps)\n        elif command == \"update\":\n            return self._update_plan(plan_id, title, steps)\n        elif command == \"list\":\n            return self._list_plans()\n        elif command == \"get\":\n            return self._get_plan(plan_id)\n        elif command == \"set_active\":\n            return self._set_active_plan(plan_id)\n        elif command == \"mark_step\":\n            return self._mark_step(plan_id, step_index, step_status, step_notes)\n        elif command == \"delete\":\n            return self._delete_plan(plan_id)\n        else:\n            raise ToolError(\n                f\"Unrecognized command: {command}. Allowed commands are: create, update, list, get, set_active, mark_step, delete\"\n            )\n\n    def _create_plan(\n        self, plan_id: Optional[str], title: Optional[str], steps: Optional[List[str]]\n    ) -> ToolResult:\n        \"\"\"Create a new plan with the given ID, title, and steps.\"\"\"\n        if not plan_id:\n            raise ToolError(\"Parameter `plan_id` is required for command: create\")\n\n        if plan_id in self.plans:\n            raise ToolError(\n                f\"A plan with ID '{plan_id}' already exists. Use 'update' to modify existing plans.\"\n            )\n\n        if not title:\n            raise ToolError(\"Parameter `title` is required for command: create\")\n\n        if (\n            not steps\n            or not isinstance(steps, list)\n            or not all(isinstance(step, str) for step in steps)\n        ):\n            raise ToolError(\n                \"Parameter `steps` must be a non-empty list of strings for command: create\"\n            )\n\n        # Create a new plan with initialized step statuses\n        plan = {\n            \"plan_id\": plan_id,\n            \"title\": title,\n            \"steps\": steps,\n            \"step_statuses\": [\"not_started\"] * len(steps),\n            \"step_notes\": [\"\"] * len(steps),\n        }\n\n        self.plans[plan_id] = plan\n        self._current_plan_id = plan_id  # Set as active plan\n\n        return ToolResult(\n            output=f\"Plan created successfully with ID: {plan_id}\\n\\n{self._format_plan(plan)}\"\n        )\n\n    def _update_plan(\n        self, plan_id: Optional[str], title: Optional[str], steps: Optional[List[str]]\n    ) -> ToolResult:\n        \"\"\"Update an existing plan with new title or steps.\"\"\"\n        if not plan_id:\n            raise ToolError(\"Parameter `plan_id` is required for command: update\")\n\n        if plan_id not in self.plans:\n            raise ToolError(f\"No plan found with ID: {plan_id}\")\n\n        plan = self.plans[plan_id]\n\n        if title:\n            plan[\"title\"] = title\n\n        if steps:\n            if not isinstance(steps, list) or not all(\n                isinstance(step, str) for step in steps\n            ):\n                raise ToolError(\n                    \"Parameter `steps` must be a list of strings for command: update\"\n                )\n\n            # Preserve existing step statuses for unchanged steps\n            old_steps = plan[\"steps\"]\n            old_statuses = plan[\"step_statuses\"]\n            old_notes = plan[\"step_notes\"]\n\n            # Create new step statuses and notes\n            new_statuses = []\n            new_notes = []\n\n            for i, step in enumerate(steps):\n                # If the step exists at the same position in old steps, preserve status and notes\n                if i < len(old_steps) and step == old_steps[i]:\n                    new_statuses.append(old_statuses[i])\n                    new_notes.append(old_notes[i])\n                else:\n                    new_statuses.append(\"not_started\")\n                    new_notes.append(\"\")\n\n            plan[\"steps\"] = steps\n            plan[\"step_statuses\"] = new_statuses\n            plan[\"step_notes\"] = new_notes\n\n        return ToolResult(\n            output=f\"Plan updated successfully: {plan_id}\\n\\n{self._format_plan(plan)}\"\n        )\n\n    def _list_plans(self) -> ToolResult:\n        \"\"\"List all available plans.\"\"\"\n        if not self.plans:\n            return ToolResult(\n                output=\"No plans available. Create a plan with the 'create' command.\"\n            )\n\n        output = \"Available plans:\\n\"\n        for plan_id, plan in self.plans.items():\n            current_marker = \" (active)\" if plan_id == self._current_plan_id else \"\"\n            completed = sum(\n                1 for status in plan[\"step_statuses\"] if status == \"completed\"\n            )\n            total = len(plan[\"steps\"])\n            progress = f\"{completed}/{total} steps completed\"\n            output += f\"• {plan_id}{current_marker}: {plan['title']} - {progress}\\n\"\n\n        return ToolResult(output=output)\n\n    def _get_plan(self, plan_id: Optional[str]) -> ToolResult:\n        \"\"\"Get details of a specific plan.\"\"\"\n        if not plan_id:\n            # If no plan_id is provided, use the current active plan\n            if not self._current_plan_id:\n                raise ToolError(\n                    \"No active plan. Please specify a plan_id or set an active plan.\"\n                )\n            plan_id = self._current_plan_id\n\n        if plan_id not in self.plans:\n            raise ToolError(f\"No plan found with ID: {plan_id}\")\n\n        plan = self.plans[plan_id]\n        return ToolResult(output=self._format_plan(plan))\n\n    def _set_active_plan(self, plan_id: Optional[str]) -> ToolResult:\n        \"\"\"Set a plan as the active plan.\"\"\"\n        if not plan_id:\n            raise ToolError(\"Parameter `plan_id` is required for command: set_active\")\n\n        if plan_id not in self.plans:\n            raise ToolError(f\"No plan found with ID: {plan_id}\")\n\n        self._current_plan_id = plan_id\n        return ToolResult(\n            output=f\"Plan '{plan_id}' is now the active plan.\\n\\n{self._format_plan(self.plans[plan_id])}\"\n        )\n\n    def _mark_step(\n        self,\n        plan_id: Optional[str],\n        step_index: Optional[int],\n        step_status: Optional[str],\n        step_notes: Optional[str],\n    ) -> ToolResult:\n        \"\"\"Mark a step with a specific status and optional notes.\"\"\"\n        if not plan_id:\n            # If no plan_id is provided, use the current active plan\n            if not self._current_plan_id:\n                raise ToolError(\n                    \"No active plan. Please specify a plan_id or set an active plan.\"\n                )\n            plan_id = self._current_plan_id\n\n        if plan_id not in self.plans:\n            raise ToolError(f\"No plan found with ID: {plan_id}\")\n\n        if step_index is None:\n            raise ToolError(\"Parameter `step_index` is required for command: mark_step\")\n\n        plan = self.plans[plan_id]\n\n        if step_index < 0 or step_index >= len(plan[\"steps\"]):\n            raise ToolError(\n                f\"Invalid step_index: {step_index}. Valid indices range from 0 to {len(plan['steps'])-1}.\"\n            )\n\n        if step_status and step_status not in [\n            \"not_started\",\n            \"in_progress\",\n            \"completed\",\n            \"blocked\",\n        ]:\n            raise ToolError(\n                f\"Invalid step_status: {step_status}. Valid statuses are: not_started, in_progress, completed, blocked\"\n            )\n\n        if step_status:\n            plan[\"step_statuses\"][step_index] = step_status\n\n        if step_notes:\n            plan[\"step_notes\"][step_index] = step_notes\n\n        return ToolResult(\n            output=f\"Step {step_index} updated in plan '{plan_id}'.\\n\\n{self._format_plan(plan)}\"\n        )\n\n    def _delete_plan(self, plan_id: Optional[str]) -> ToolResult:\n        \"\"\"Delete a plan.\"\"\"\n        if not plan_id:\n            raise ToolError(\"Parameter `plan_id` is required for command: delete\")\n\n        if plan_id not in self.plans:\n            raise ToolError(f\"No plan found with ID: {plan_id}\")\n\n        del self.plans[plan_id]\n\n        # If the deleted plan was the active plan, clear the active plan\n        if self._current_plan_id == plan_id:\n            self._current_plan_id = None\n\n        return ToolResult(output=f\"Plan '{plan_id}' has been deleted.\")\n\n    def _format_plan(self, plan: Dict) -> str:\n        \"\"\"Format a plan for display.\"\"\"\n        output = f\"Plan: {plan['title']} (ID: {plan['plan_id']})\\n\"\n        output += \"=\" * len(output) + \"\\n\\n\"\n\n        # Calculate progress statistics\n        total_steps = len(plan[\"steps\"])\n        completed = sum(1 for status in plan[\"step_statuses\"] if status == \"completed\")\n        in_progress = sum(\n            1 for status in plan[\"step_statuses\"] if status == \"in_progress\"\n        )\n        blocked = sum(1 for status in plan[\"step_statuses\"] if status == \"blocked\")\n        not_started = sum(\n            1 for status in plan[\"step_statuses\"] if status == \"not_started\"\n        )\n\n        output += f\"Progress: {completed}/{total_steps} steps completed \"\n        if total_steps > 0:\n            percentage = (completed / total_steps) * 100\n            output += f\"({percentage:.1f}%)\\n\"\n        else:\n            output += \"(0%)\\n\"\n\n        output += f\"Status: {completed} completed, {in_progress} in progress, {blocked} blocked, {not_started} not started\\n\\n\"\n        output += \"Steps:\\n\"\n\n        # Add each step with its status and notes\n        for i, (step, status, notes) in enumerate(\n            zip(plan[\"steps\"], plan[\"step_statuses\"], plan[\"step_notes\"])\n        ):\n            status_symbol = {\n                \"not_started\": \"[ ]\",\n                \"in_progress\": \"[→]\",\n                \"completed\": \"[✓]\",\n                \"blocked\": \"[!]\",\n            }.get(status, \"[ ]\")\n\n            output += f\"{i}. {status_symbol} {step}\\n\"\n            if notes:\n                output += f\"   Notes: {notes}\\n\"\n\n        return output\n"
  },
  {
    "path": "app/tool/python_execute.py",
    "content": "import multiprocessing\nimport sys\nfrom io import StringIO\nfrom typing import Dict\n\nfrom app.tool.base import BaseTool\n\n\nclass PythonExecute(BaseTool):\n    \"\"\"A tool for executing Python code with timeout and safety restrictions.\"\"\"\n\n    name: str = \"python_execute\"\n    description: str = \"Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results.\"\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"code\": {\n                \"type\": \"string\",\n                \"description\": \"The Python code to execute.\",\n            },\n        },\n        \"required\": [\"code\"],\n    }\n\n    def _run_code(self, code: str, result_dict: dict, safe_globals: dict) -> None:\n        original_stdout = sys.stdout\n        try:\n            output_buffer = StringIO()\n            sys.stdout = output_buffer\n            exec(code, safe_globals, safe_globals)\n            result_dict[\"observation\"] = output_buffer.getvalue()\n            result_dict[\"success\"] = True\n        except Exception as e:\n            result_dict[\"observation\"] = str(e)\n            result_dict[\"success\"] = False\n        finally:\n            sys.stdout = original_stdout\n\n    async def execute(\n        self,\n        code: str,\n        timeout: int = 5,\n    ) -> Dict:\n        \"\"\"\n        Executes the provided Python code with a timeout.\n\n        Args:\n            code (str): The Python code to execute.\n            timeout (int): Execution timeout in seconds.\n\n        Returns:\n            Dict: Contains 'output' with execution output or error message and 'success' status.\n        \"\"\"\n\n        with multiprocessing.Manager() as manager:\n            result = manager.dict({\"observation\": \"\", \"success\": False})\n            if isinstance(__builtins__, dict):\n                safe_globals = {\"__builtins__\": __builtins__}\n            else:\n                safe_globals = {\"__builtins__\": __builtins__.__dict__.copy()}\n            proc = multiprocessing.Process(\n                target=self._run_code, args=(code, result, safe_globals)\n            )\n            proc.start()\n            proc.join(timeout)\n\n            # timeout process\n            if proc.is_alive():\n                proc.terminate()\n                proc.join(1)\n                return {\n                    \"observation\": f\"Execution timeout after {timeout} seconds\",\n                    \"success\": False,\n                }\n            return dict(result)\n"
  },
  {
    "path": "app/tool/sandbox/sb_browser_tool.py",
    "content": "import base64\nimport io\nimport json\nimport traceback\nfrom typing import Optional  # Add this import for Optional\n\nfrom PIL import Image\nfrom pydantic import Field\n\nfrom app.daytona.tool_base import (  # Ensure Sandbox is imported correctly\n    Sandbox,\n    SandboxToolsBase,\n    ThreadMessage,\n)\nfrom app.tool.base import ToolResult\nfrom app.utils.logger import logger\n\n\n# Context = TypeVar(\"Context\")\n_BROWSER_DESCRIPTION = \"\"\"\\\nA sandbox-based browser automation tool that allows interaction with web pages through various actions.\n* This tool provides commands for controlling a browser session in a sandboxed environment\n* It maintains state across calls, keeping the browser session alive until explicitly closed\n* Use this when you need to browse websites, fill forms, click buttons, or extract content in a secure sandbox\n* Each action requires specific parameters as defined in the tool's dependencies\nKey capabilities include:\n* Navigation: Go to specific URLs, go back in history\n* Interaction: Click elements by index, input text, send keyboard commands\n* Scrolling: Scroll up/down by pixel amount or scroll to specific text\n* Tab management: Switch between tabs or close tabs\n* Content extraction: Get dropdown options or select dropdown options\n\"\"\"\n\n\n# noinspection PyArgumentList\nclass SandboxBrowserTool(SandboxToolsBase):\n    \"\"\"Tool for executing tasks in a Daytona sandbox with browser-use capabilities.\"\"\"\n\n    name: str = \"sandbox_browser\"\n    description: str = _BROWSER_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\n                    \"navigate_to\",\n                    \"go_back\",\n                    \"wait\",\n                    \"click_element\",\n                    \"input_text\",\n                    \"send_keys\",\n                    \"switch_tab\",\n                    \"close_tab\",\n                    \"scroll_down\",\n                    \"scroll_up\",\n                    \"scroll_to_text\",\n                    \"get_dropdown_options\",\n                    \"select_dropdown_option\",\n                    \"click_coordinates\",\n                    \"drag_drop\",\n                ],\n                \"description\": \"The browser action to perform\",\n            },\n            \"url\": {\n                \"type\": \"string\",\n                \"description\": \"URL for 'navigate_to' action\",\n            },\n            \"index\": {\n                \"type\": \"integer\",\n                \"description\": \"Element index for interaction actions\",\n            },\n            \"text\": {\n                \"type\": \"string\",\n                \"description\": \"Text for input or scroll actions\",\n            },\n            \"amount\": {\n                \"type\": \"integer\",\n                \"description\": \"Pixel amount to scroll\",\n            },\n            \"page_id\": {\n                \"type\": \"integer\",\n                \"description\": \"Tab ID for tab management actions\",\n            },\n            \"keys\": {\n                \"type\": \"string\",\n                \"description\": \"Keys to send for keyboard actions\",\n            },\n            \"seconds\": {\n                \"type\": \"integer\",\n                \"description\": \"Seconds to wait\",\n            },\n            \"x\": {\n                \"type\": \"integer\",\n                \"description\": \"X coordinate for click or drag actions\",\n            },\n            \"y\": {\n                \"type\": \"integer\",\n                \"description\": \"Y coordinate for click or drag actions\",\n            },\n            \"element_source\": {\n                \"type\": \"string\",\n                \"description\": \"Source element for drag and drop\",\n            },\n            \"element_target\": {\n                \"type\": \"string\",\n                \"description\": \"Target element for drag and drop\",\n            },\n        },\n        \"required\": [\"action\"],\n        \"dependencies\": {\n            \"navigate_to\": [\"url\"],\n            \"click_element\": [\"index\"],\n            \"input_text\": [\"index\", \"text\"],\n            \"send_keys\": [\"keys\"],\n            \"switch_tab\": [\"page_id\"],\n            \"close_tab\": [\"page_id\"],\n            \"scroll_down\": [\"amount\"],\n            \"scroll_up\": [\"amount\"],\n            \"scroll_to_text\": [\"text\"],\n            \"get_dropdown_options\": [\"index\"],\n            \"select_dropdown_option\": [\"index\", \"text\"],\n            \"click_coordinates\": [\"x\", \"y\"],\n            \"drag_drop\": [\"element_source\", \"element_target\"],\n            \"wait\": [\"seconds\"],\n        },\n    }\n    browser_message: Optional[ThreadMessage] = Field(default=None, exclude=True)\n\n    def __init__(\n        self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data\n    ):\n        \"\"\"Initialize with optional sandbox and thread_id.\"\"\"\n        super().__init__(**data)\n        if sandbox is not None:\n            self._sandbox = sandbox  # Directly set the base class private attribute\n\n    def _validate_base64_image(\n        self, base64_string: str, max_size_mb: int = 10\n    ) -> tuple[bool, str]:\n        \"\"\"\n        Validate base64 image data.\n        Args:\n            base64_string: The base64 encoded image data\n            max_size_mb: Maximum allowed image size in megabytes\n        Returns:\n            Tuple of (is_valid, error_message)\n        \"\"\"\n        try:\n            if not base64_string or len(base64_string) < 10:\n                return False, \"Base64 string is empty or too short\"\n            if base64_string.startswith(\"data:\"):\n                try:\n                    base64_string = base64_string.split(\",\", 1)[1]\n                except (IndexError, ValueError):\n                    return False, \"Invalid data URL format\"\n            import re\n\n            if not re.match(r\"^[A-Za-z0-9+/]*={0,2}$\", base64_string):\n                return False, \"Invalid base64 characters detected\"\n            if len(base64_string) % 4 != 0:\n                return False, \"Invalid base64 string length\"\n            try:\n                image_data = base64.b64decode(base64_string, validate=True)\n            except Exception as e:\n                return False, f\"Base64 decoding failed: {str(e)}\"\n            max_size_bytes = max_size_mb * 1024 * 1024\n            if len(image_data) > max_size_bytes:\n                return False, f\"Image size exceeds limit ({max_size_bytes} bytes)\"\n            try:\n                image_stream = io.BytesIO(image_data)\n                with Image.open(image_stream) as img:\n                    img.verify()\n                    supported_formats = {\"JPEG\", \"PNG\", \"GIF\", \"BMP\", \"WEBP\", \"TIFF\"}\n                    if img.format not in supported_formats:\n                        return False, f\"Unsupported image format: {img.format}\"\n                    image_stream.seek(0)\n                    with Image.open(image_stream) as img_check:\n                        width, height = img_check.size\n                        max_dimension = 8192\n                        if width > max_dimension or height > max_dimension:\n                            return (\n                                False,\n                                f\"Image dimensions exceed limit ({max_dimension}x{max_dimension})\",\n                            )\n                        if width < 1 or height < 1:\n                            return False, f\"Invalid image dimensions: {width}x{height}\"\n            except Exception as e:\n                return False, f\"Invalid image data: {str(e)}\"\n            return True, \"Valid image\"\n        except Exception as e:\n            logger.error(f\"Unexpected error during base64 image validation: {e}\")\n            return False, f\"Validation error: {str(e)}\"\n\n    async def _execute_browser_action(\n        self, endpoint: str, params: dict = None, method: str = \"POST\"\n    ) -> ToolResult:\n        \"\"\"Execute a browser automation action through the sandbox API.\"\"\"\n        try:\n            await self._ensure_sandbox()\n            url = f\"http://localhost:8003/api/automation/{endpoint}\"\n            if method == \"GET\" and params:\n                query_params = \"&\".join([f\"{k}={v}\" for k, v in params.items()])\n                url = f\"{url}?{query_params}\"\n                curl_cmd = (\n                    f\"curl -s -X {method} '{url}' -H 'Content-Type: application/json'\"\n                )\n            else:\n                curl_cmd = (\n                    f\"curl -s -X {method} '{url}' -H 'Content-Type: application/json'\"\n                )\n                if params:\n                    json_data = json.dumps(params)\n                    curl_cmd += f\" -d '{json_data}'\"\n            logger.debug(f\"Executing curl command: {curl_cmd}\")\n            response = self.sandbox.process.exec(curl_cmd, timeout=30)\n            if response.exit_code == 0:\n                try:\n                    result = json.loads(response.result)\n                    result.setdefault(\"content\", \"\")\n                    result.setdefault(\"role\", \"assistant\")\n                    if \"screenshot_base64\" in result:\n                        screenshot_data = result[\"screenshot_base64\"]\n                        is_valid, validation_message = self._validate_base64_image(\n                            screenshot_data\n                        )\n                        if not is_valid:\n                            logger.warning(\n                                f\"Screenshot validation failed: {validation_message}\"\n                            )\n                            result[\"image_validation_error\"] = validation_message\n                            del result[\"screenshot_base64\"]\n\n                    # added_message = await self.thread_manager.add_message(\n                    #     thread_id=self.thread_id,\n                    #     type=\"browser_state\",\n                    #     content=result,\n                    #     is_llm_message=False\n                    # )\n                    message = ThreadMessage(\n                        type=\"browser_state\", content=result, is_llm_message=False\n                    )\n                    self.browser_message = message\n                    success_response = {\n                        \"success\": result.get(\"success\", False),\n                        \"message\": result.get(\"message\", \"Browser action completed\"),\n                    }\n                    #         if added_message and 'message_id' in added_message:\n                    #             success_response['message_id'] = added_message['message_id']\n                    for field in [\n                        \"url\",\n                        \"title\",\n                        \"element_count\",\n                        \"pixels_below\",\n                        \"ocr_text\",\n                        \"image_url\",\n                    ]:\n                        if field in result:\n                            success_response[field] = result[field]\n                    return (\n                        self.success_response(success_response)\n                        if success_response[\"success\"]\n                        else self.fail_response(success_response)\n                    )\n                except json.JSONDecodeError as e:\n                    logger.error(f\"Failed to parse response JSON: {e}\")\n                    return self.fail_response(f\"Failed to parse response JSON: {e}\")\n            else:\n                logger.error(f\"Browser automation request failed: {response}\")\n                return self.fail_response(\n                    f\"Browser automation request failed: {response}\"\n                )\n        except Exception as e:\n            logger.error(f\"Error executing browser action: {e}\")\n            logger.debug(traceback.format_exc())\n            return self.fail_response(f\"Error executing browser action: {e}\")\n\n    async def execute(\n        self,\n        action: str,\n        url: Optional[str] = None,\n        index: Optional[int] = None,\n        text: Optional[str] = None,\n        amount: Optional[int] = None,\n        page_id: Optional[int] = None,\n        keys: Optional[str] = None,\n        seconds: Optional[int] = None,\n        x: Optional[int] = None,\n        y: Optional[int] = None,\n        element_source: Optional[str] = None,\n        element_target: Optional[str] = None,\n        **kwargs,\n    ) -> ToolResult:\n        \"\"\"\n        Execute a browser action in the sandbox environment.\n        Args:\n            action: The browser action to perform\n            url: URL for navigation\n            index: Element index for interaction\n            text: Text for input or scroll actions\n            amount: Pixel amount to scroll\n            page_id: Tab ID for tab management\n            keys: Keys to send for keyboard actions\n            seconds: Seconds to wait\n            x: X coordinate for click/drag\n            y: Y coordinate for click/drag\n            element_source: Source element for drag and drop\n            element_target: Target element for drag and drop\n        Returns:\n            ToolResult with the action's output or error\n        \"\"\"\n        # async with self.lock:\n        try:\n            # Navigation actions\n            if action == \"navigate_to\":\n                if not url:\n                    return self.fail_response(\"URL is required for navigation\")\n                return await self._execute_browser_action(\"navigate_to\", {\"url\": url})\n            elif action == \"go_back\":\n                return await self._execute_browser_action(\"go_back\", {})\n                # Interaction actions\n            elif action == \"click_element\":\n                if index is None:\n                    return self.fail_response(\"Index is required for click_element\")\n                return await self._execute_browser_action(\n                    \"click_element\", {\"index\": index}\n                )\n            elif action == \"input_text\":\n                if index is None or not text:\n                    return self.fail_response(\n                        \"Index and text are required for input_text\"\n                    )\n                return await self._execute_browser_action(\n                    \"input_text\", {\"index\": index, \"text\": text}\n                )\n            elif action == \"send_keys\":\n                if not keys:\n                    return self.fail_response(\"Keys are required for send_keys\")\n                return await self._execute_browser_action(\"send_keys\", {\"keys\": keys})\n                # Tab management\n            elif action == \"switch_tab\":\n                if page_id is None:\n                    return self.fail_response(\"Page ID is required for switch_tab\")\n                return await self._execute_browser_action(\n                    \"switch_tab\", {\"page_id\": page_id}\n                )\n            elif action == \"close_tab\":\n                if page_id is None:\n                    return self.fail_response(\"Page ID is required for close_tab\")\n                return await self._execute_browser_action(\n                    \"close_tab\", {\"page_id\": page_id}\n                )\n                # Scrolling actions\n            elif action == \"scroll_down\":\n                params = {\"amount\": amount} if amount is not None else {}\n                return await self._execute_browser_action(\"scroll_down\", params)\n            elif action == \"scroll_up\":\n                params = {\"amount\": amount} if amount is not None else {}\n                return await self._execute_browser_action(\"scroll_up\", params)\n            elif action == \"scroll_to_text\":\n                if not text:\n                    return self.fail_response(\"Text is required for scroll_to_text\")\n                return await self._execute_browser_action(\n                    \"scroll_to_text\", {\"text\": text}\n                )\n            # Dropdown actions\n            elif action == \"get_dropdown_options\":\n                if index is None:\n                    return self.fail_response(\n                        \"Index is required for get_dropdown_options\"\n                    )\n                return await self._execute_browser_action(\n                    \"get_dropdown_options\", {\"index\": index}\n                )\n            elif action == \"select_dropdown_option\":\n                if index is None or not text:\n                    return self.fail_response(\n                        \"Index and text are required for select_dropdown_option\"\n                    )\n                return await self._execute_browser_action(\n                    \"select_dropdown_option\", {\"index\": index, \"text\": text}\n                )\n                # Coordinate-based actions\n            elif action == \"click_coordinates\":\n                if x is None or y is None:\n                    return self.fail_response(\n                        \"X and Y coordinates are required for click_coordinates\"\n                    )\n                return await self._execute_browser_action(\n                    \"click_coordinates\", {\"x\": x, \"y\": y}\n                )\n            elif action == \"drag_drop\":\n                if not element_source or not element_target:\n                    return self.fail_response(\n                        \"Source and target elements are required for drag_drop\"\n                    )\n                return await self._execute_browser_action(\n                    \"drag_drop\",\n                    {\n                        \"element_source\": element_source,\n                        \"element_target\": element_target,\n                    },\n                )\n            # Utility actions\n            elif action == \"wait\":\n                seconds_to_wait = seconds if seconds is not None else 3\n                return await self._execute_browser_action(\n                    \"wait\", {\"seconds\": seconds_to_wait}\n                )\n            else:\n                return self.fail_response(f\"Unknown action: {action}\")\n        except Exception as e:\n            logger.error(f\"Error executing browser action: {e}\")\n            return self.fail_response(f\"Error executing browser action: {e}\")\n\n    async def get_current_state(\n        self, message: Optional[ThreadMessage] = None\n    ) -> ToolResult:\n        \"\"\"\n        Get the current browser state as a ToolResult.\n        If context is not provided, uses self.context.\n        \"\"\"\n        try:\n            # Use provided context or fall back to self.context\n            message = message or self.browser_message\n            if not message:\n                return ToolResult(error=\"Browser context not initialized\")\n            state = message.content\n            screenshot = state.get(\"screenshot_base64\")\n            # Build the state info with all required fields\n            state_info = {\n                \"url\": state.get(\"url\", \"\"),\n                \"title\": state.get(\"title\", \"\"),\n                \"tabs\": [tab.model_dump() for tab in state.get(\"tabs\", [])],\n                \"pixels_above\": getattr(state, \"pixels_above\", 0),\n                \"pixels_below\": getattr(state, \"pixels_below\", 0),\n                \"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.\",\n            }\n\n            return ToolResult(\n                output=json.dumps(state_info, indent=4, ensure_ascii=False),\n                base64_image=screenshot,\n            )\n        except Exception as e:\n            return ToolResult(error=f\"Failed to get browser state: {str(e)}\")\n\n    @classmethod\n    def create_with_sandbox(cls, sandbox: Sandbox) -> \"SandboxBrowserTool\":\n        \"\"\"Factory method to create a tool with sandbox.\"\"\"\n        return cls(sandbox=sandbox)\n"
  },
  {
    "path": "app/tool/sandbox/sb_files_tool.py",
    "content": "import asyncio\nfrom typing import Optional, TypeVar\n\nfrom pydantic import Field\n\nfrom app.daytona.tool_base import Sandbox, SandboxToolsBase\nfrom app.tool.base import ToolResult\nfrom app.utils.files_utils import clean_path, should_exclude_file\nfrom app.utils.logger import logger\n\n\nContext = TypeVar(\"Context\")\n\n_FILES_DESCRIPTION = \"\"\"\\\nA sandbox-based file system tool that allows file operations in a secure sandboxed environment.\n* This tool provides commands for creating, reading, updating, and deleting files in the workspace\n* All operations are performed relative to the /workspace directory for security\n* Use this when you need to manage files, edit code, or manipulate file contents in a sandbox\n* Each action requires specific parameters as defined in the tool's dependencies\nKey capabilities include:\n* File creation: Create new files with specified content and permissions\n* File modification: Replace specific strings or completely rewrite files\n* File deletion: Remove files from the workspace\n* File reading: Read file contents with optional line range specification\n\"\"\"\n\n\nclass SandboxFilesTool(SandboxToolsBase):\n    name: str = \"sandbox_files\"\n    description: str = _FILES_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\n                    \"create_file\",\n                    \"str_replace\",\n                    \"full_file_rewrite\",\n                    \"delete_file\",\n                ],\n                \"description\": \"The file operation to perform\",\n            },\n            \"file_path\": {\n                \"type\": \"string\",\n                \"description\": \"Path to the file, relative to /workspace (e.g., 'src/main.py')\",\n            },\n            \"file_contents\": {\n                \"type\": \"string\",\n                \"description\": \"Content to write to the file\",\n            },\n            \"old_str\": {\n                \"type\": \"string\",\n                \"description\": \"Text to be replaced (must appear exactly once)\",\n            },\n            \"new_str\": {\n                \"type\": \"string\",\n                \"description\": \"Replacement text\",\n            },\n            \"permissions\": {\n                \"type\": \"string\",\n                \"description\": \"File permissions in octal format (e.g., '644')\",\n                \"default\": \"644\",\n            },\n        },\n        \"required\": [\"action\"],\n        \"dependencies\": {\n            \"create_file\": [\"file_path\", \"file_contents\"],\n            \"str_replace\": [\"file_path\", \"old_str\", \"new_str\"],\n            \"full_file_rewrite\": [\"file_path\", \"file_contents\"],\n            \"delete_file\": [\"file_path\"],\n        },\n    }\n    SNIPPET_LINES: int = Field(default=4, exclude=True)\n    # workspace_path: str = Field(default=\"/workspace\", exclude=True)\n    # sandbox: Optional[Sandbox] = Field(default=None, exclude=True)\n\n    def __init__(\n        self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data\n    ):\n        \"\"\"Initialize with optional sandbox and thread_id.\"\"\"\n        super().__init__(**data)\n        if sandbox is not None:\n            self._sandbox = sandbox\n\n    def clean_path(self, path: str) -> str:\n        \"\"\"Clean and normalize a path to be relative to /workspace\"\"\"\n        return clean_path(path, self.workspace_path)\n\n    def _should_exclude_file(self, rel_path: str) -> bool:\n        \"\"\"Check if a file should be excluded based on path, name, or extension\"\"\"\n        return should_exclude_file(rel_path)\n\n    def _file_exists(self, path: str) -> bool:\n        \"\"\"Check if a file exists in the sandbox\"\"\"\n        try:\n            self.sandbox.fs.get_file_info(path)\n            return True\n        except Exception:\n            return False\n\n    async def get_workspace_state(self) -> dict:\n        \"\"\"Get the current workspace state by reading all files\"\"\"\n        files_state = {}\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            files = self.sandbox.fs.list_files(self.workspace_path)\n            for file_info in files:\n                rel_path = file_info.name\n\n                # Skip excluded files and directories\n                if self._should_exclude_file(rel_path) or file_info.is_dir:\n                    continue\n\n                try:\n                    full_path = f\"{self.workspace_path}/{rel_path}\"\n                    content = self.sandbox.fs.download_file(full_path).decode()\n                    files_state[rel_path] = {\n                        \"content\": content,\n                        \"is_dir\": file_info.is_dir,\n                        \"size\": file_info.size,\n                        \"modified\": file_info.mod_time,\n                    }\n                except Exception as e:\n                    print(f\"Error reading file {rel_path}: {e}\")\n                except UnicodeDecodeError:\n                    print(f\"Skipping binary file: {rel_path}\")\n\n            return files_state\n\n        except Exception as e:\n            print(f\"Error getting workspace state: {str(e)}\")\n            return {}\n\n    async def execute(\n        self,\n        action: str,\n        file_path: Optional[str] = None,\n        file_contents: Optional[str] = None,\n        old_str: Optional[str] = None,\n        new_str: Optional[str] = None,\n        permissions: Optional[str] = \"644\",\n        **kwargs,\n    ) -> ToolResult:\n        \"\"\"\n        Execute a file operation in the sandbox environment.\n        Args:\n            action: The file operation to perform\n            file_path: Path to the file relative to /workspace\n            file_contents: Content to write to the file\n            old_str: Text to be replaced (for str_replace)\n            new_str: Replacement text (for str_replace)\n            permissions: File permissions in octal format\n        Returns:\n            ToolResult with the operation's output or error\n        \"\"\"\n        async with asyncio.Lock():\n            try:\n                # File creation\n                if action == \"create_file\":\n                    if not file_path or not file_contents:\n                        return self.fail_response(\n                            \"file_path and file_contents are required for create_file\"\n                        )\n                    return await self._create_file(\n                        file_path, file_contents, permissions\n                    )\n\n                # String replacement\n                elif action == \"str_replace\":\n                    if not file_path or not old_str or not new_str:\n                        return self.fail_response(\n                            \"file_path, old_str, and new_str are required for str_replace\"\n                        )\n                    return await self._str_replace(file_path, old_str, new_str)\n\n                # Full file rewrite\n                elif action == \"full_file_rewrite\":\n                    if not file_path or not file_contents:\n                        return self.fail_response(\n                            \"file_path and file_contents are required for full_file_rewrite\"\n                        )\n                    return await self._full_file_rewrite(\n                        file_path, file_contents, permissions\n                    )\n\n                # File deletion\n                elif action == \"delete_file\":\n                    if not file_path:\n                        return self.fail_response(\n                            \"file_path is required for delete_file\"\n                        )\n                    return await self._delete_file(file_path)\n\n                else:\n                    return self.fail_response(f\"Unknown action: {action}\")\n\n            except Exception as e:\n                logger.error(f\"Error executing file action: {e}\")\n                return self.fail_response(f\"Error executing file action: {e}\")\n\n    async def _create_file(\n        self, file_path: str, file_contents: str, permissions: str = \"644\"\n    ) -> ToolResult:\n        \"\"\"Create a new file with the provided contents\"\"\"\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            file_path = self.clean_path(file_path)\n            full_path = f\"{self.workspace_path}/{file_path}\"\n            if self._file_exists(full_path):\n                return self.fail_response(\n                    f\"File '{file_path}' already exists. Use full_file_rewrite to modify existing files.\"\n                )\n\n            # Create parent directories if needed\n            parent_dir = \"/\".join(full_path.split(\"/\")[:-1])\n            if parent_dir:\n                self.sandbox.fs.create_folder(parent_dir, \"755\")\n\n            # Write the file content\n            self.sandbox.fs.upload_file(file_contents.encode(), full_path)\n            self.sandbox.fs.set_file_permissions(full_path, permissions)\n\n            message = f\"File '{file_path}' created successfully.\"\n\n            # Check if index.html was created and add 8080 server info (only in root workspace)\n            if file_path.lower() == \"index.html\":\n                try:\n                    website_link = self.sandbox.get_preview_link(8080)\n                    website_url = (\n                        website_link.url\n                        if hasattr(website_link, \"url\")\n                        else str(website_link).split(\"url='\")[1].split(\"'\")[0]\n                    )\n                    message += f\"\\n\\n[Auto-detected index.html - HTTP server available at: {website_url}]\"\n                    message += \"\\n[Note: Use the provided HTTP server URL above instead of starting a new server]\"\n                except Exception as e:\n                    logger.warning(\n                        f\"Failed to get website URL for index.html: {str(e)}\"\n                    )\n\n            return self.success_response(message)\n        except Exception as e:\n            return self.fail_response(f\"Error creating file: {str(e)}\")\n\n    async def _str_replace(\n        self, file_path: str, old_str: str, new_str: str\n    ) -> ToolResult:\n        \"\"\"Replace specific text in a file\"\"\"\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            file_path = self.clean_path(file_path)\n            full_path = f\"{self.workspace_path}/{file_path}\"\n            if not self._file_exists(full_path):\n                return self.fail_response(f\"File '{file_path}' does not exist\")\n\n            content = self.sandbox.fs.download_file(full_path).decode()\n            old_str = old_str.expandtabs()\n            new_str = new_str.expandtabs()\n\n            occurrences = content.count(old_str)\n            if occurrences == 0:\n                return self.fail_response(f\"String '{old_str}' not found in file\")\n            if occurrences > 1:\n                lines = [\n                    i + 1\n                    for i, line in enumerate(content.split(\"\\n\"))\n                    if old_str in line\n                ]\n                return self.fail_response(\n                    f\"Multiple occurrences found in lines {lines}. Please ensure string is unique\"\n                )\n\n            # Perform replacement\n            new_content = content.replace(old_str, new_str)\n            self.sandbox.fs.upload_file(new_content.encode(), full_path)\n\n            # Show snippet around the edit\n            replacement_line = content.split(old_str)[0].count(\"\\n\")\n            start_line = max(0, replacement_line - self.SNIPPET_LINES)\n            end_line = replacement_line + self.SNIPPET_LINES + new_str.count(\"\\n\")\n            snippet = \"\\n\".join(new_content.split(\"\\n\")[start_line : end_line + 1])\n\n            message = f\"Replacement successful.\"\n\n            return self.success_response(message)\n\n        except Exception as e:\n            return self.fail_response(f\"Error replacing string: {str(e)}\")\n\n    async def _full_file_rewrite(\n        self, file_path: str, file_contents: str, permissions: str = \"644\"\n    ) -> ToolResult:\n        \"\"\"Completely rewrite an existing file with new content\"\"\"\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            file_path = self.clean_path(file_path)\n            full_path = f\"{self.workspace_path}/{file_path}\"\n            if not self._file_exists(full_path):\n                return self.fail_response(\n                    f\"File '{file_path}' does not exist. Use create_file to create a new file.\"\n                )\n\n            self.sandbox.fs.upload_file(file_contents.encode(), full_path)\n            self.sandbox.fs.set_file_permissions(full_path, permissions)\n\n            message = f\"File '{file_path}' completely rewritten successfully.\"\n\n            # Check if index.html was rewritten and add 8080 server info (only in root workspace)\n            if file_path.lower() == \"index.html\":\n                try:\n                    website_link = self.sandbox.get_preview_link(8080)\n                    website_url = (\n                        website_link.url\n                        if hasattr(website_link, \"url\")\n                        else str(website_link).split(\"url='\")[1].split(\"'\")[0]\n                    )\n                    message += f\"\\n\\n[Auto-detected index.html - HTTP server available at: {website_url}]\"\n                    message += \"\\n[Note: Use the provided HTTP server URL above instead of starting a new server]\"\n                except Exception as e:\n                    logger.warning(\n                        f\"Failed to get website URL for index.html: {str(e)}\"\n                    )\n\n            return self.success_response(message)\n        except Exception as e:\n            return self.fail_response(f\"Error rewriting file: {str(e)}\")\n\n    async def _delete_file(self, file_path: str) -> ToolResult:\n        \"\"\"Delete a file at the given path\"\"\"\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            file_path = self.clean_path(file_path)\n            full_path = f\"{self.workspace_path}/{file_path}\"\n            if not self._file_exists(full_path):\n                return self.fail_response(f\"File '{file_path}' does not exist\")\n\n            self.sandbox.fs.delete_file(full_path)\n            return self.success_response(f\"File '{file_path}' deleted successfully.\")\n        except Exception as e:\n            return self.fail_response(f\"Error deleting file: {str(e)}\")\n\n    async def cleanup(self):\n        \"\"\"Clean up sandbox resources.\"\"\"\n\n    @classmethod\n    def create_with_context(cls, context: Context) -> \"SandboxFilesTool[Context]\":\n        \"\"\"Factory method to create a SandboxFilesTool with a specific context.\"\"\"\n        raise NotImplementedError(\n            \"create_with_context not implemented for SandboxFilesTool\"\n        )\n"
  },
  {
    "path": "app/tool/sandbox/sb_shell_tool.py",
    "content": "import asyncio\nimport time\nfrom typing import Any, Dict, Optional, TypeVar\nfrom uuid import uuid4\n\nfrom app.daytona.tool_base import Sandbox, SandboxToolsBase\nfrom app.tool.base import ToolResult\nfrom app.utils.logger import logger\n\n\nContext = TypeVar(\"Context\")\n_SHELL_DESCRIPTION = \"\"\"\\\nExecute a shell command in the workspace directory.\nIMPORTANT: Commands are non-blocking by default and run in a tmux session.\nThis is ideal for long-running operations like starting servers or build processes.\nUses sessions to maintain state between commands.\nThis tool is essential for running CLI tools, installing packages, and managing system operations.\n\"\"\"\n\n\nclass SandboxShellTool(SandboxToolsBase):\n    \"\"\"Tool for executing tasks in a Daytona sandbox with browser-use capabilities.\n    Uses sessions for maintaining state between commands and provides comprehensive process management.\n    \"\"\"\n\n    name: str = \"sandbox_shell\"\n    description: str = _SHELL_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\n                    \"execute_command\",\n                    \"check_command_output\",\n                    \"terminate_command\",\n                    \"list_commands\",\n                ],\n                \"description\": \"The shell action to perform\",\n            },\n            \"command\": {\n                \"type\": \"string\",\n                \"description\": \"The shell command to execute. Use this for running CLI tools, installing packages, \"\n                \"or system operations. Commands can be chained using &&, ||, and | operators.\",\n            },\n            \"folder\": {\n                \"type\": \"string\",\n                \"description\": \"Optional relative path to a subdirectory of /workspace where the command should be \"\n                \"executed. Example: 'data/pdfs'\",\n            },\n            \"session_name\": {\n                \"type\": \"string\",\n                \"description\": \"Optional name of the tmux session to use. Use named sessions for related commands \"\n                \"that need to maintain state. Defaults to a random session name.\",\n            },\n            \"blocking\": {\n                \"type\": \"boolean\",\n                \"description\": \"Whether to wait for the command to complete. Defaults to false for non-blocking \"\n                \"execution.\",\n                \"default\": False,\n            },\n            \"timeout\": {\n                \"type\": \"integer\",\n                \"description\": \"Optional timeout in seconds for blocking commands. Defaults to 60. Ignored for \"\n                \"non-blocking commands.\",\n                \"default\": 60,\n            },\n            \"kill_session\": {\n                \"type\": \"boolean\",\n                \"description\": \"Whether to terminate the tmux session after checking. Set to true when you're done \"\n                \"with the command.\",\n                \"default\": False,\n            },\n        },\n        \"required\": [\"action\"],\n        \"dependencies\": {\n            \"execute_command\": [\"command\"],\n            \"check_command_output\": [\"session_name\"],\n            \"terminate_command\": [\"session_name\"],\n            \"list_commands\": [],\n        },\n    }\n\n    def __init__(\n        self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data\n    ):\n        \"\"\"Initialize with optional sandbox and thread_id.\"\"\"\n        super().__init__(**data)\n        if sandbox is not None:\n            self._sandbox = sandbox\n\n    async def _ensure_session(self, session_name: str = \"default\") -> str:\n        \"\"\"Ensure a session exists and return its ID.\"\"\"\n        if session_name not in self._sessions:\n            session_id = str(uuid4())\n            try:\n                await self._ensure_sandbox()  # Ensure sandbox is initialized\n                self.sandbox.process.create_session(session_id)\n                self._sessions[session_name] = session_id\n            except Exception as e:\n                raise RuntimeError(f\"Failed to create session: {str(e)}\")\n        return self._sessions[session_name]\n\n    async def _cleanup_session(self, session_name: str):\n        \"\"\"Clean up a session if it exists.\"\"\"\n        if session_name in self._sessions:\n            try:\n                await self._ensure_sandbox()  # Ensure sandbox is initialized\n                self.sandbox.process.delete_session(self._sessions[session_name])\n                del self._sessions[session_name]\n            except Exception as e:\n                print(f\"Warning: Failed to cleanup session {session_name}: {str(e)}\")\n\n    async def _execute_raw_command(self, command: str) -> Dict[str, Any]:\n        \"\"\"Execute a raw command directly in the sandbox.\"\"\"\n        # Ensure session exists for raw commands\n        session_id = await self._ensure_session(\"raw_commands\")\n\n        # Execute command in session\n        from app.daytona.sandbox import SessionExecuteRequest\n\n        req = SessionExecuteRequest(\n            command=command, run_async=False, cwd=self.workspace_path\n        )\n\n        response = self.sandbox.process.execute_session_command(\n            session_id=session_id,\n            req=req,\n            timeout=30,  # Short timeout for utility commands\n        )\n\n        logs = self.sandbox.process.get_session_command_logs(\n            session_id=session_id, command_id=response.cmd_id\n        )\n\n        return {\"output\": logs, \"exit_code\": response.exit_code}\n\n    async def _execute_command(\n        self,\n        command: str,\n        folder: Optional[str] = None,\n        session_name: Optional[str] = None,\n        blocking: bool = False,\n        timeout: int = 60,\n    ) -> ToolResult:\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            # Set up working directory\n            cwd = self.workspace_path\n            if folder:\n                folder = folder.strip(\"/\")\n                cwd = f\"{self.workspace_path}/{folder}\"\n\n            # Generate a session name if not provided\n            if not session_name:\n                session_name = f\"session_{str(uuid4())[:8]}\"\n\n            # Check if tmux session already exists\n            check_session = await self._execute_raw_command(\n                f\"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'\"\n            )\n            session_exists = \"not_exists\" not in check_session.get(\"output\", \"\")\n\n            if not session_exists:\n                # Create a new tmux session\n                await self._execute_raw_command(\n                    f\"tmux new-session -d -s {session_name}\"\n                )\n\n            # Ensure we're in the correct directory and send command to tmux\n            full_command = f\"cd {cwd} && {command}\"\n            wrapped_command = full_command.replace('\"', '\\\\\"')  # Escape double quotes\n\n            # Send command to tmux session\n            await self._execute_raw_command(\n                f'tmux send-keys -t {session_name} \"{wrapped_command}\" Enter'\n            )\n\n            if blocking:\n                # For blocking execution, wait and capture output\n                start_time = time.time()\n                while (time.time() - start_time) < timeout:\n                    # Wait a bit before checking\n                    time.sleep(2)\n\n                    # Check if session still exists (command might have exited)\n                    check_result = await self._execute_raw_command(\n                        f\"tmux has-session -t {session_name} 2>/dev/null || echo 'ended'\"\n                    )\n                    if \"ended\" in check_result.get(\"output\", \"\"):\n                        break\n\n                    # Get current output and check for common completion indicators\n                    output_result = await self._execute_raw_command(\n                        f\"tmux capture-pane -t {session_name} -p -S - -E -\"\n                    )\n                    current_output = output_result.get(\"output\", \"\")\n\n                    # Check for prompt indicators that suggest command completion\n                    last_lines = current_output.split(\"\\n\")[-3:]\n                    completion_indicators = [\n                        \"$\",\n                        \"#\",\n                        \">\",\n                        \"Done\",\n                        \"Completed\",\n                        \"Finished\",\n                        \"✓\",\n                    ]\n                    if any(\n                        indicator in line\n                        for indicator in completion_indicators\n                        for line in last_lines\n                    ):\n                        break\n\n                # Capture final output\n                output_result = await self._execute_raw_command(\n                    f\"tmux capture-pane -t {session_name} -p -S - -E -\"\n                )\n                final_output = output_result.get(\"output\", \"\")\n\n                # Kill the session after capture\n                await self._execute_raw_command(f\"tmux kill-session -t {session_name}\")\n\n                return self.success_response(\n                    {\n                        \"output\": final_output,\n                        \"session_name\": session_name,\n                        \"cwd\": cwd,\n                        \"completed\": True,\n                    }\n                )\n            else:\n                # For non-blocking, just return immediately\n                return self.success_response(\n                    {\n                        \"session_name\": session_name,\n                        \"cwd\": cwd,\n                        \"message\": f\"Command sent to tmux session '{session_name}'. Use check_command_output to view results.\",\n                        \"completed\": False,\n                    }\n                )\n\n        except Exception as e:\n            # Attempt to clean up session in case of error\n            if session_name:\n                try:\n                    await self._execute_raw_command(\n                        f\"tmux kill-session -t {session_name}\"\n                    )\n                except:\n                    pass\n            return self.fail_response(f\"Error executing command: {str(e)}\")\n\n    async def _check_command_output(\n        self, session_name: str, kill_session: bool = False\n    ) -> ToolResult:\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            # Check if session exists\n            check_result = await self._execute_raw_command(\n                f\"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'\"\n            )\n            if \"not_exists\" in check_result.get(\"output\", \"\"):\n                return self.fail_response(\n                    f\"Tmux session '{session_name}' does not exist.\"\n                )\n\n            # Get output from tmux pane\n            output_result = await self._execute_raw_command(\n                f\"tmux capture-pane -t {session_name} -p -S - -E -\"\n            )\n            output = output_result.get(\"output\", \"\")\n\n            # Kill session if requested\n            if kill_session:\n                await self._execute_raw_command(f\"tmux kill-session -t {session_name}\")\n                termination_status = \"Session terminated.\"\n            else:\n                termination_status = \"Session still running.\"\n\n            return self.success_response(\n                {\n                    \"output\": output,\n                    \"session_name\": session_name,\n                    \"status\": termination_status,\n                }\n            )\n\n        except Exception as e:\n            return self.fail_response(f\"Error checking command output: {str(e)}\")\n\n    async def _terminate_command(self, session_name: str) -> ToolResult:\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            # Check if session exists\n            check_result = await self._execute_raw_command(\n                f\"tmux has-session -t {session_name} 2>/dev/null || echo 'not_exists'\"\n            )\n            if \"not_exists\" in check_result.get(\"output\", \"\"):\n                return self.fail_response(\n                    f\"Tmux session '{session_name}' does not exist.\"\n                )\n\n            # Kill the session\n            await self._execute_raw_command(f\"tmux kill-session -t {session_name}\")\n\n            return self.success_response(\n                {\"message\": f\"Tmux session '{session_name}' terminated successfully.\"}\n            )\n\n        except Exception as e:\n            return self.fail_response(f\"Error terminating command: {str(e)}\")\n\n    async def _list_commands(self) -> ToolResult:\n        try:\n            # Ensure sandbox is initialized\n            await self._ensure_sandbox()\n\n            # List all tmux sessions\n            result = await self._execute_raw_command(\n                \"tmux list-sessions 2>/dev/null || echo 'No sessions'\"\n            )\n            output = result.get(\"output\", \"\")\n\n            if \"No sessions\" in output or not output.strip():\n                return self.success_response(\n                    {\"message\": \"No active tmux sessions found.\", \"sessions\": []}\n                )\n\n            # Parse session list\n            sessions = []\n            for line in output.split(\"\\n\"):\n                if line.strip():\n                    parts = line.split(\":\")\n                    if parts:\n                        session_name = parts[0].strip()\n                        sessions.append(session_name)\n\n            return self.success_response(\n                {\n                    \"message\": f\"Found {len(sessions)} active sessions.\",\n                    \"sessions\": sessions,\n                }\n            )\n\n        except Exception as e:\n            return self.fail_response(f\"Error listing commands: {str(e)}\")\n\n    async def execute(\n        self,\n        action: str,\n        command: str,\n        folder: Optional[str] = None,\n        session_name: Optional[str] = None,\n        blocking: bool = False,\n        timeout: int = 60,\n        kill_session: bool = False,\n    ) -> ToolResult:\n        \"\"\"\n        Execute a browser action in the sandbox environment.\n        Args:\n            timeout:\n            blocking:\n            session_name:\n            folder:\n            command:\n            kill_session:\n            action: The browser action to perform\n        Returns:\n            ToolResult with the action's output or error\n        \"\"\"\n        async with asyncio.Lock():\n            try:\n                # Navigation actions\n                if action == \"execute_command\":\n                    if not command:\n                        return self.fail_response(\"command is required for navigation\")\n                    return await self._execute_command(\n                        command, folder, session_name, blocking, timeout\n                    )\n                elif action == \"check_command_output\":\n                    if session_name is None:\n                        return self.fail_response(\n                            \"session_name is required for navigation\"\n                        )\n                    return await self._check_command_output(session_name, kill_session)\n                elif action == \"terminate_command\":\n                    if session_name is None:\n                        return self.fail_response(\n                            \"session_name is required for click_element\"\n                        )\n                    return await self._terminate_command(session_name)\n                elif action == \"list_commands\":\n                    return await self._list_commands()\n                else:\n                    return self.fail_response(f\"Unknown action: {action}\")\n            except Exception as e:\n                logger.error(f\"Error executing shell action: {e}\")\n                return self.fail_response(f\"Error executing shell action: {e}\")\n\n    async def cleanup(self):\n        \"\"\"Clean up all sessions.\"\"\"\n        for session_name in list(self._sessions.keys()):\n            await self._cleanup_session(session_name)\n\n        # Also clean up any tmux sessions\n        try:\n            await self._ensure_sandbox()\n            await self._execute_raw_command(\"tmux kill-server 2>/dev/null || true\")\n        except Exception as e:\n            logger.error(f\"Error shell box cleanup action: {e}\")\n"
  },
  {
    "path": "app/tool/sandbox/sb_vision_tool.py",
    "content": "import base64\nimport mimetypes\nimport os\nfrom io import BytesIO\nfrom typing import Optional\n\nfrom PIL import Image\nfrom pydantic import Field\n\nfrom app.daytona.tool_base import Sandbox, SandboxToolsBase, ThreadMessage\nfrom app.tool.base import ToolResult\n\n\n# 最大文件大小（原图10MB，压缩后5MB）\nMAX_IMAGE_SIZE = 10 * 1024 * 1024\nMAX_COMPRESSED_SIZE = 5 * 1024 * 1024\n\n# 压缩设置\nDEFAULT_MAX_WIDTH = 1920\nDEFAULT_MAX_HEIGHT = 1080\nDEFAULT_JPEG_QUALITY = 85\nDEFAULT_PNG_COMPRESS_LEVEL = 6\n\n_VISION_DESCRIPTION = \"\"\"\nA sandbox-based vision tool that allows the agent to read image files inside the sandbox using the see_image action.\n* Only the see_image action is supported, with the parameter being the relative path of the image under /workspace.\n* The image will be compressed and converted to base64 for use in subsequent context.\n* Supported formats: JPG, PNG, GIF, WEBP. Maximum size: 10MB.\n\"\"\"\n\n\nclass SandboxVisionTool(SandboxToolsBase):\n    name: str = \"sandbox_vision\"\n    description: str = _VISION_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"action\": {\n                \"type\": \"string\",\n                \"enum\": [\"see_image\"],\n                \"description\": \"要执行的视觉动作，目前仅支持 see_image\",\n            },\n            \"file_path\": {\n                \"type\": \"string\",\n                \"description\": \"图片在 /workspace 下的相对路径，如 'screenshots/image.png'\",\n            },\n        },\n        \"required\": [\"action\", \"file_path\"],\n        \"dependencies\": {\"see_image\": [\"file_path\"]},\n    }\n\n    # def __init__(self, project_id: str, thread_id: str, thread_manager: ThreadManager):\n    #     super().__init__(project_id=project_id, thread_manager=thread_manager)\n    #     self.thread_id = thread_id\n    #     self.thread_manager = thread_manager\n\n    vision_message: Optional[ThreadMessage] = Field(default=None, exclude=True)\n\n    def __init__(\n        self, sandbox: Optional[Sandbox] = None, thread_id: Optional[str] = None, **data\n    ):\n        \"\"\"Initialize with optional sandbox and thread_id.\"\"\"\n        super().__init__(**data)\n        if sandbox is not None:\n            self._sandbox = sandbox\n\n    def compress_image(self, image_bytes: bytes, mime_type: str, file_path: str):\n        \"\"\"压缩图片，保持合理质量。\"\"\"\n        try:\n            img = Image.open(BytesIO(image_bytes))\n            if img.mode in (\"RGBA\", \"LA\", \"P\"):\n                background = Image.new(\"RGB\", img.size, (255, 255, 255))\n                if img.mode == \"P\":\n                    img = img.convert(\"RGBA\")\n                background.paste(\n                    img, mask=img.split()[-1] if img.mode == \"RGBA\" else None\n                )\n                img = background\n            width, height = img.size\n            if width > DEFAULT_MAX_WIDTH or height > DEFAULT_MAX_HEIGHT:\n                ratio = min(DEFAULT_MAX_WIDTH / width, DEFAULT_MAX_HEIGHT / height)\n                new_width = int(width * ratio)\n                new_height = int(height * ratio)\n                img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)\n            output = BytesIO()\n            if mime_type == \"image/gif\":\n                img.save(output, format=\"GIF\", optimize=True)\n                output_mime = \"image/gif\"\n            elif mime_type == \"image/png\":\n                img.save(\n                    output,\n                    format=\"PNG\",\n                    optimize=True,\n                    compress_level=DEFAULT_PNG_COMPRESS_LEVEL,\n                )\n                output_mime = \"image/png\"\n            else:\n                img.save(\n                    output, format=\"JPEG\", quality=DEFAULT_JPEG_QUALITY, optimize=True\n                )\n                output_mime = \"image/jpeg\"\n            compressed_bytes = output.getvalue()\n            return compressed_bytes, output_mime\n        except Exception:\n            return image_bytes, mime_type\n\n    async def execute(\n        self, action: str, file_path: Optional[str] = None, **kwargs\n    ) -> ToolResult:\n        \"\"\"\n        执行视觉动作，目前仅支持 see_image。\n        参数：\n            action: 必须为 'see_image'\n            file_path: 图片相对路径\n        \"\"\"\n        if action != \"see_image\":\n            return self.fail_response(f\"未知的视觉动作: {action}\")\n        if not file_path:\n            return self.fail_response(\"file_path 参数不能为空\")\n        try:\n            await self._ensure_sandbox()\n            cleaned_path = self.clean_path(file_path)\n            full_path = f\"{self.workspace_path}/{cleaned_path}\"\n            try:\n                file_info = self.sandbox.fs.get_file_info(full_path)\n                if file_info.is_dir:\n                    return self.fail_response(f\"路径 '{cleaned_path}' 是目录，不是图片文件。\")\n            except Exception:\n                return self.fail_response(f\"图片文件未找到: '{cleaned_path}'\")\n            if file_info.size > MAX_IMAGE_SIZE:\n                return self.fail_response(\n                    f\"图片文件 '{cleaned_path}' 过大 ({file_info.size / (1024*1024):.2f}MB)，最大允许 {MAX_IMAGE_SIZE / (1024*1024)}MB。\"\n                )\n            try:\n                image_bytes = self.sandbox.fs.download_file(full_path)\n            except Exception:\n                return self.fail_response(f\"无法读取图片文件: {cleaned_path}\")\n            mime_type, _ = mimetypes.guess_type(full_path)\n            if not mime_type or not mime_type.startswith(\"image/\"):\n                ext = os.path.splitext(cleaned_path)[1].lower()\n                if ext == \".jpg\" or ext == \".jpeg\":\n                    mime_type = \"image/jpeg\"\n                elif ext == \".png\":\n                    mime_type = \"image/png\"\n                elif ext == \".gif\":\n                    mime_type = \"image/gif\"\n                elif ext == \".webp\":\n                    mime_type = \"image/webp\"\n                else:\n                    return self.fail_response(\n                        f\"不支持或未知的图片格式: '{cleaned_path}'。支持: JPG, PNG, GIF, WEBP。\"\n                    )\n            compressed_bytes, compressed_mime_type = self.compress_image(\n                image_bytes, mime_type, cleaned_path\n            )\n            if len(compressed_bytes) > MAX_COMPRESSED_SIZE:\n                return self.fail_response(\n                    f\"图片文件 '{cleaned_path}' 压缩后仍过大 ({len(compressed_bytes) / (1024*1024):.2f}MB)，最大允许 {MAX_COMPRESSED_SIZE / (1024*1024)}MB。\"\n                )\n            base64_image = base64.b64encode(compressed_bytes).decode(\"utf-8\")\n            image_context_data = {\n                \"mime_type\": compressed_mime_type,\n                \"base64\": base64_image,\n                \"file_path\": cleaned_path,\n                \"original_size\": file_info.size,\n                \"compressed_size\": len(compressed_bytes),\n            }\n            message = ThreadMessage(\n                type=\"image_context\", content=image_context_data, is_llm_message=False\n            )\n            self.vision_message = message\n            # return self.success_response(f\"成功加载并压缩图片 '{cleaned_path}' (由 {file_info.size / 1024:.1f}KB 压缩到 {len(compressed_bytes) / 1024:.1f}KB)。\")\n            return ToolResult(\n                output=f\"成功加载并压缩图片 '{cleaned_path}'\",\n                base64_image=base64_image,\n            )\n        except Exception as e:\n            return self.fail_response(f\"see_image 执行异常: {str(e)}\")\n"
  },
  {
    "path": "app/tool/search/__init__.py",
    "content": "from app.tool.search.baidu_search import BaiduSearchEngine\nfrom app.tool.search.base import WebSearchEngine\nfrom app.tool.search.bing_search import BingSearchEngine\nfrom app.tool.search.duckduckgo_search import DuckDuckGoSearchEngine\nfrom app.tool.search.google_search import GoogleSearchEngine\n\n\n__all__ = [\n    \"WebSearchEngine\",\n    \"BaiduSearchEngine\",\n    \"DuckDuckGoSearchEngine\",\n    \"GoogleSearchEngine\",\n    \"BingSearchEngine\",\n]\n"
  },
  {
    "path": "app/tool/search/baidu_search.py",
    "content": "from typing import List\n\nfrom baidusearch.baidusearch import search\n\nfrom app.tool.search.base import SearchItem, WebSearchEngine\n\n\nclass BaiduSearchEngine(WebSearchEngine):\n    def perform_search(\n        self, query: str, num_results: int = 10, *args, **kwargs\n    ) -> List[SearchItem]:\n        \"\"\"\n        Baidu search engine.\n\n        Returns results formatted according to SearchItem model.\n        \"\"\"\n        raw_results = search(query, num_results=num_results)\n\n        # Convert raw results to SearchItem format\n        results = []\n        for i, item in enumerate(raw_results):\n            if isinstance(item, str):\n                # If it's just a URL\n                results.append(\n                    SearchItem(title=f\"Baidu Result {i+1}\", url=item, description=None)\n                )\n            elif isinstance(item, dict):\n                # If it's a dictionary with details\n                results.append(\n                    SearchItem(\n                        title=item.get(\"title\", f\"Baidu Result {i+1}\"),\n                        url=item.get(\"url\", \"\"),\n                        description=item.get(\"abstract\", None),\n                    )\n                )\n            else:\n                # Try to get attributes directly\n                try:\n                    results.append(\n                        SearchItem(\n                            title=getattr(item, \"title\", f\"Baidu Result {i+1}\"),\n                            url=getattr(item, \"url\", \"\"),\n                            description=getattr(item, \"abstract\", None),\n                        )\n                    )\n                except Exception:\n                    # Fallback to a basic result\n                    results.append(\n                        SearchItem(\n                            title=f\"Baidu Result {i+1}\", url=str(item), description=None\n                        )\n                    )\n\n        return results\n"
  },
  {
    "path": "app/tool/search/base.py",
    "content": "from typing import List, Optional\n\nfrom pydantic import BaseModel, Field\n\n\nclass SearchItem(BaseModel):\n    \"\"\"Represents a single search result item\"\"\"\n\n    title: str = Field(description=\"The title of the search result\")\n    url: str = Field(description=\"The URL of the search result\")\n    description: Optional[str] = Field(\n        default=None, description=\"A description or snippet of the search result\"\n    )\n\n    def __str__(self) -> str:\n        \"\"\"String representation of a search result item.\"\"\"\n        return f\"{self.title} - {self.url}\"\n\n\nclass WebSearchEngine(BaseModel):\n    \"\"\"Base class for web search engines.\"\"\"\n\n    model_config = {\"arbitrary_types_allowed\": True}\n\n    def perform_search(\n        self, query: str, num_results: int = 10, *args, **kwargs\n    ) -> List[SearchItem]:\n        \"\"\"\n        Perform a web search and return a list of search items.\n\n        Args:\n            query (str): The search query to submit to the search engine.\n            num_results (int, optional): The number of search results to return. Default is 10.\n            args: Additional arguments.\n            kwargs: Additional keyword arguments.\n\n        Returns:\n            List[SearchItem]: A list of SearchItem objects matching the search query.\n        \"\"\"\n        raise NotImplementedError\n"
  },
  {
    "path": "app/tool/search/bing_search.py",
    "content": "from typing import List, Optional, Tuple\n\nimport requests\nfrom bs4 import BeautifulSoup\n\nfrom app.logger import logger\nfrom app.tool.search.base import SearchItem, WebSearchEngine\n\n\nABSTRACT_MAX_LENGTH = 300\n\nUSER_AGENTS = [\n    \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36\",\n    \"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\",\n    \"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\",\n    \"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\",\n    \"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36\",\n    \"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\",\n    \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4pre) Gecko/20070404 K-Ninja/2.1.3\",\n    \"Mozilla/5.0 (Future Star Technologies Corp.; Star-Blade OS; x86_64; U; en-US) iNet Browser 4.7\",\n    \"Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201\",\n    \"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\",\n]\n\nHEADERS = {\n    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\",\n    \"Content-Type\": \"application/x-www-form-urlencoded\",\n    \"User-Agent\": USER_AGENTS[0],\n    \"Referer\": \"https://www.bing.com/\",\n    \"Accept-Encoding\": \"gzip, deflate\",\n    \"Accept-Language\": \"zh-CN,zh;q=0.9\",\n}\n\nBING_HOST_URL = \"https://www.bing.com\"\nBING_SEARCH_URL = \"https://www.bing.com/search?q=\"\n\n\nclass BingSearchEngine(WebSearchEngine):\n    session: Optional[requests.Session] = None\n\n    def __init__(self, **data):\n        \"\"\"Initialize the BingSearch tool with a requests session.\"\"\"\n        super().__init__(**data)\n        self.session = requests.Session()\n        self.session.headers.update(HEADERS)\n\n    def _search_sync(self, query: str, num_results: int = 10) -> List[SearchItem]:\n        \"\"\"\n        Synchronous Bing search implementation to retrieve search results.\n\n        Args:\n            query (str): The search query to submit to Bing.\n            num_results (int, optional): Maximum number of results to return. Defaults to 10.\n\n        Returns:\n            List[SearchItem]: A list of search items with title, URL, and description.\n        \"\"\"\n        if not query:\n            return []\n\n        list_result = []\n        first = 1\n        next_url = BING_SEARCH_URL + query\n\n        while len(list_result) < num_results:\n            data, next_url = self._parse_html(\n                next_url, rank_start=len(list_result), first=first\n            )\n            if data:\n                list_result.extend(data)\n            if not next_url:\n                break\n            first += 10\n\n        return list_result[:num_results]\n\n    def _parse_html(\n        self, url: str, rank_start: int = 0, first: int = 1\n    ) -> Tuple[List[SearchItem], str]:\n        \"\"\"\n        Parse Bing search result HTML to extract search results and the next page URL.\n\n        Returns:\n            tuple: (List of SearchItem objects, next page URL or None)\n        \"\"\"\n        try:\n            res = self.session.get(url=url)\n            res.encoding = \"utf-8\"\n            root = BeautifulSoup(res.text, \"lxml\")\n\n            list_data = []\n            ol_results = root.find(\"ol\", id=\"b_results\")\n            if not ol_results:\n                return [], None\n\n            for li in ol_results.find_all(\"li\", class_=\"b_algo\"):\n                title = \"\"\n                url = \"\"\n                abstract = \"\"\n                try:\n                    h2 = li.find(\"h2\")\n                    if h2:\n                        title = h2.text.strip()\n                        url = h2.a[\"href\"].strip()\n\n                    p = li.find(\"p\")\n                    if p:\n                        abstract = p.text.strip()\n\n                    if ABSTRACT_MAX_LENGTH and len(abstract) > ABSTRACT_MAX_LENGTH:\n                        abstract = abstract[:ABSTRACT_MAX_LENGTH]\n\n                    rank_start += 1\n\n                    # Create a SearchItem object\n                    list_data.append(\n                        SearchItem(\n                            title=title or f\"Bing Result {rank_start}\",\n                            url=url,\n                            description=abstract,\n                        )\n                    )\n                except Exception:\n                    continue\n\n            next_btn = root.find(\"a\", title=\"Next page\")\n            if not next_btn:\n                return list_data, None\n\n            next_url = BING_HOST_URL + next_btn[\"href\"]\n            return list_data, next_url\n        except Exception as e:\n            logger.warning(f\"Error parsing HTML: {e}\")\n            return [], None\n\n    def perform_search(\n        self, query: str, num_results: int = 10, *args, **kwargs\n    ) -> List[SearchItem]:\n        \"\"\"\n        Bing search engine.\n\n        Returns results formatted according to SearchItem model.\n        \"\"\"\n        return self._search_sync(query, num_results=num_results)\n"
  },
  {
    "path": "app/tool/search/duckduckgo_search.py",
    "content": "from typing import List\n\nfrom duckduckgo_search import DDGS\n\nfrom app.tool.search.base import SearchItem, WebSearchEngine\n\n\nclass DuckDuckGoSearchEngine(WebSearchEngine):\n    def perform_search(\n        self, query: str, num_results: int = 10, *args, **kwargs\n    ) -> List[SearchItem]:\n        \"\"\"\n        DuckDuckGo search engine.\n\n        Returns results formatted according to SearchItem model.\n        \"\"\"\n        raw_results = DDGS().text(query, max_results=num_results)\n\n        results = []\n        for i, item in enumerate(raw_results):\n            if isinstance(item, str):\n                # If it's just a URL\n                results.append(\n                    SearchItem(\n                        title=f\"DuckDuckGo Result {i + 1}\", url=item, description=None\n                    )\n                )\n            elif isinstance(item, dict):\n                # Extract data from the dictionary\n                results.append(\n                    SearchItem(\n                        title=item.get(\"title\", f\"DuckDuckGo Result {i + 1}\"),\n                        url=item.get(\"href\", \"\"),\n                        description=item.get(\"body\", None),\n                    )\n                )\n            else:\n                # Try to extract attributes directly\n                try:\n                    results.append(\n                        SearchItem(\n                            title=getattr(item, \"title\", f\"DuckDuckGo Result {i + 1}\"),\n                            url=getattr(item, \"href\", \"\"),\n                            description=getattr(item, \"body\", None),\n                        )\n                    )\n                except Exception:\n                    # Fallback\n                    results.append(\n                        SearchItem(\n                            title=f\"DuckDuckGo Result {i + 1}\",\n                            url=str(item),\n                            description=None,\n                        )\n                    )\n\n        return results\n"
  },
  {
    "path": "app/tool/search/google_search.py",
    "content": "from typing import List\n\nfrom googlesearch import search\n\nfrom app.tool.search.base import SearchItem, WebSearchEngine\n\n\nclass GoogleSearchEngine(WebSearchEngine):\n    def perform_search(\n        self, query: str, num_results: int = 10, *args, **kwargs\n    ) -> List[SearchItem]:\n        \"\"\"\n        Google search engine.\n\n        Returns results formatted according to SearchItem model.\n        \"\"\"\n        raw_results = search(query, num_results=num_results, advanced=True)\n\n        results = []\n        for i, item in enumerate(raw_results):\n            if isinstance(item, str):\n                # If it's just a URL\n                results.append(\n                    {\"title\": f\"Google Result {i+1}\", \"url\": item, \"description\": \"\"}\n                )\n            else:\n                results.append(\n                    SearchItem(\n                        title=item.title, url=item.url, description=item.description\n                    )\n                )\n\n        return results\n"
  },
  {
    "path": "app/tool/str_replace_editor.py",
    "content": "\"\"\"File and directory manipulation tool with sandbox support.\"\"\"\n\nfrom collections import defaultdict\nfrom pathlib import Path\nfrom typing import Any, DefaultDict, List, Literal, Optional, get_args\n\nfrom app.config import config\nfrom app.exceptions import ToolError\nfrom app.tool import BaseTool\nfrom app.tool.base import CLIResult, ToolResult\nfrom app.tool.file_operators import (\n    FileOperator,\n    LocalFileOperator,\n    PathLike,\n    SandboxFileOperator,\n)\n\n\nCommand = Literal[\n    \"view\",\n    \"create\",\n    \"str_replace\",\n    \"insert\",\n    \"undo_edit\",\n]\n\n# Constants\nSNIPPET_LINES: int = 4\nMAX_RESPONSE_LEN: int = 16000\nTRUNCATED_MESSAGE: str = (\n    \"<response clipped><NOTE>To save on context only part of this file has been shown to you. \"\n    \"You should retry this tool after you have searched inside the file with `grep -n` \"\n    \"in order to find the line numbers of what you are looking for.</NOTE>\"\n)\n\n# Tool description\n_STR_REPLACE_EDITOR_DESCRIPTION = \"\"\"Custom editing tool for viewing, creating and editing files\n* State is persistent across command calls and discussions with the user\n* 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\n* The `create` command cannot be used if the specified `path` already exists as a file\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`\n* The `undo_edit` command will revert the last edit made to the file at `path`\n\nNotes for using the `str_replace` command:\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\n* 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\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`\n\"\"\"\n\n\ndef maybe_truncate(\n    content: str, truncate_after: Optional[int] = MAX_RESPONSE_LEN\n) -> str:\n    \"\"\"Truncate content and append a notice if content exceeds the specified length.\"\"\"\n    if not truncate_after or len(content) <= truncate_after:\n        return content\n    return content[:truncate_after] + TRUNCATED_MESSAGE\n\n\nclass StrReplaceEditor(BaseTool):\n    \"\"\"A tool for viewing, creating, and editing files with sandbox support.\"\"\"\n\n    name: str = \"str_replace_editor\"\n    description: str = _STR_REPLACE_EDITOR_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"command\": {\n                \"description\": \"The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.\",\n                \"enum\": [\"view\", \"create\", \"str_replace\", \"insert\", \"undo_edit\"],\n                \"type\": \"string\",\n            },\n            \"path\": {\n                \"description\": \"Absolute path to file or directory.\",\n                \"type\": \"string\",\n            },\n            \"file_text\": {\n                \"description\": \"Required parameter of `create` command, with the content of the file to be created.\",\n                \"type\": \"string\",\n            },\n            \"old_str\": {\n                \"description\": \"Required parameter of `str_replace` command containing the string in `path` to replace.\",\n                \"type\": \"string\",\n            },\n            \"new_str\": {\n                \"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.\",\n                \"type\": \"string\",\n            },\n            \"insert_line\": {\n                \"description\": \"Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.\",\n                \"type\": \"integer\",\n            },\n            \"view_range\": {\n                \"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.\",\n                \"items\": {\"type\": \"integer\"},\n                \"type\": \"array\",\n            },\n        },\n        \"required\": [\"command\", \"path\"],\n    }\n    _file_history: DefaultDict[PathLike, List[str]] = defaultdict(list)\n    _local_operator: LocalFileOperator = LocalFileOperator()\n    _sandbox_operator: SandboxFileOperator = SandboxFileOperator()\n\n    # def _get_operator(self, use_sandbox: bool) -> FileOperator:\n    def _get_operator(self) -> FileOperator:\n        \"\"\"Get the appropriate file operator based on execution mode.\"\"\"\n        return (\n            self._sandbox_operator\n            if config.sandbox.use_sandbox\n            else self._local_operator\n        )\n\n    async def execute(\n        self,\n        *,\n        command: Command,\n        path: str,\n        file_text: str | None = None,\n        view_range: list[int] | None = None,\n        old_str: str | None = None,\n        new_str: str | None = None,\n        insert_line: int | None = None,\n        **kwargs: Any,\n    ) -> str:\n        \"\"\"Execute a file operation command.\"\"\"\n        # Get the appropriate file operator\n        operator = self._get_operator()\n\n        # Validate path and command combination\n        await self.validate_path(command, Path(path), operator)\n\n        # Execute the appropriate command\n        if command == \"view\":\n            result = await self.view(path, view_range, operator)\n        elif command == \"create\":\n            if file_text is None:\n                raise ToolError(\"Parameter `file_text` is required for command: create\")\n            await operator.write_file(path, file_text)\n            self._file_history[path].append(file_text)\n            result = ToolResult(output=f\"File created successfully at: {path}\")\n        elif command == \"str_replace\":\n            if old_str is None:\n                raise ToolError(\n                    \"Parameter `old_str` is required for command: str_replace\"\n                )\n            result = await self.str_replace(path, old_str, new_str, operator)\n        elif command == \"insert\":\n            if insert_line is None:\n                raise ToolError(\n                    \"Parameter `insert_line` is required for command: insert\"\n                )\n            if new_str is None:\n                raise ToolError(\"Parameter `new_str` is required for command: insert\")\n            result = await self.insert(path, insert_line, new_str, operator)\n        elif command == \"undo_edit\":\n            result = await self.undo_edit(path, operator)\n        else:\n            # This should be caught by type checking, but we include it for safety\n            raise ToolError(\n                f'Unrecognized command {command}. The allowed commands for the {self.name} tool are: {\", \".join(get_args(Command))}'\n            )\n\n        return str(result)\n\n    async def validate_path(\n        self, command: str, path: Path, operator: FileOperator\n    ) -> None:\n        \"\"\"Validate path and command combination based on execution environment.\"\"\"\n        # Check if path is absolute\n        if not path.is_absolute():\n            raise ToolError(f\"The path {path} is not an absolute path\")\n\n        # Only check if path exists for non-create commands\n        if command != \"create\":\n            if not await operator.exists(path):\n                raise ToolError(\n                    f\"The path {path} does not exist. Please provide a valid path.\"\n                )\n\n            # Check if path is a directory\n            is_dir = await operator.is_directory(path)\n            if is_dir and command != \"view\":\n                raise ToolError(\n                    f\"The path {path} is a directory and only the `view` command can be used on directories\"\n                )\n\n        # Check if file exists for create command\n        elif command == \"create\":\n            exists = await operator.exists(path)\n            if exists:\n                raise ToolError(\n                    f\"File already exists at: {path}. Cannot overwrite files using command `create`.\"\n                )\n\n    async def view(\n        self,\n        path: PathLike,\n        view_range: Optional[List[int]] = None,\n        operator: FileOperator = None,\n    ) -> CLIResult:\n        \"\"\"Display file or directory content.\"\"\"\n        # Determine if path is a directory\n        is_dir = await operator.is_directory(path)\n\n        if is_dir:\n            # Directory handling\n            if view_range:\n                raise ToolError(\n                    \"The `view_range` parameter is not allowed when `path` points to a directory.\"\n                )\n\n            return await self._view_directory(path, operator)\n        else:\n            # File handling\n            return await self._view_file(path, operator, view_range)\n\n    @staticmethod\n    async def _view_directory(path: PathLike, operator: FileOperator) -> CLIResult:\n        \"\"\"Display directory contents.\"\"\"\n        find_cmd = f\"find {path} -maxdepth 2 -not -path '*/\\\\.*'\"\n\n        # Execute command using the operator\n        returncode, stdout, stderr = await operator.run_command(find_cmd)\n\n        if not stderr:\n            stdout = (\n                f\"Here's the files and directories up to 2 levels deep in {path}, \"\n                f\"excluding hidden items:\\n{stdout}\\n\"\n            )\n\n        return CLIResult(output=stdout, error=stderr)\n\n    async def _view_file(\n        self,\n        path: PathLike,\n        operator: FileOperator,\n        view_range: Optional[List[int]] = None,\n    ) -> CLIResult:\n        \"\"\"Display file content, optionally within a specified line range.\"\"\"\n        # Read file content\n        file_content = await operator.read_file(path)\n        init_line = 1\n\n        # Apply view range if specified\n        if view_range:\n            if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):\n                raise ToolError(\n                    \"Invalid `view_range`. It should be a list of two integers.\"\n                )\n\n            file_lines = file_content.split(\"\\n\")\n            n_lines_file = len(file_lines)\n            init_line, final_line = view_range\n\n            # Validate view range\n            if init_line < 1 or init_line > n_lines_file:\n                raise ToolError(\n                    f\"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be \"\n                    f\"within the range of lines of the file: {[1, n_lines_file]}\"\n                )\n            if final_line > n_lines_file:\n                raise ToolError(\n                    f\"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be \"\n                    f\"smaller than the number of lines in the file: `{n_lines_file}`\"\n                )\n            if final_line != -1 and final_line < init_line:\n                raise ToolError(\n                    f\"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be \"\n                    f\"larger or equal than its first `{init_line}`\"\n                )\n\n            # Apply range\n            if final_line == -1:\n                file_content = \"\\n\".join(file_lines[init_line - 1 :])\n            else:\n                file_content = \"\\n\".join(file_lines[init_line - 1 : final_line])\n\n        # Format and return result\n        return CLIResult(\n            output=self._make_output(file_content, str(path), init_line=init_line)\n        )\n\n    async def str_replace(\n        self,\n        path: PathLike,\n        old_str: str,\n        new_str: Optional[str] = None,\n        operator: FileOperator = None,\n    ) -> CLIResult:\n        \"\"\"Replace a unique string in a file with a new string.\"\"\"\n        # Read file content and expand tabs\n        file_content = (await operator.read_file(path)).expandtabs()\n        old_str = old_str.expandtabs()\n        new_str = new_str.expandtabs() if new_str is not None else \"\"\n\n        # Check if old_str is unique in the file\n        occurrences = file_content.count(old_str)\n        if occurrences == 0:\n            raise ToolError(\n                f\"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}.\"\n            )\n        elif occurrences > 1:\n            # Find line numbers of occurrences\n            file_content_lines = file_content.split(\"\\n\")\n            lines = [\n                idx + 1\n                for idx, line in enumerate(file_content_lines)\n                if old_str in line\n            ]\n            raise ToolError(\n                f\"No replacement was performed. Multiple occurrences of old_str `{old_str}` \"\n                f\"in lines {lines}. Please ensure it is unique\"\n            )\n\n        # Replace old_str with new_str\n        new_file_content = file_content.replace(old_str, new_str)\n\n        # Write the new content to the file\n        await operator.write_file(path, new_file_content)\n\n        # Save the original content to history\n        self._file_history[path].append(file_content)\n\n        # Create a snippet of the edited section\n        replacement_line = file_content.split(old_str)[0].count(\"\\n\")\n        start_line = max(0, replacement_line - SNIPPET_LINES)\n        end_line = replacement_line + SNIPPET_LINES + new_str.count(\"\\n\")\n        snippet = \"\\n\".join(new_file_content.split(\"\\n\")[start_line : end_line + 1])\n\n        # Prepare the success message\n        success_msg = f\"The file {path} has been edited. \"\n        success_msg += self._make_output(\n            snippet, f\"a snippet of {path}\", start_line + 1\n        )\n        success_msg += \"Review the changes and make sure they are as expected. Edit the file again if necessary.\"\n\n        return CLIResult(output=success_msg)\n\n    async def insert(\n        self,\n        path: PathLike,\n        insert_line: int,\n        new_str: str,\n        operator: FileOperator = None,\n    ) -> CLIResult:\n        \"\"\"Insert text at a specific line in a file.\"\"\"\n        # Read and prepare content\n        file_text = (await operator.read_file(path)).expandtabs()\n        new_str = new_str.expandtabs()\n        file_text_lines = file_text.split(\"\\n\")\n        n_lines_file = len(file_text_lines)\n\n        # Validate insert_line\n        if insert_line < 0 or insert_line > n_lines_file:\n            raise ToolError(\n                f\"Invalid `insert_line` parameter: {insert_line}. It should be within \"\n                f\"the range of lines of the file: {[0, n_lines_file]}\"\n            )\n\n        # Perform insertion\n        new_str_lines = new_str.split(\"\\n\")\n        new_file_text_lines = (\n            file_text_lines[:insert_line]\n            + new_str_lines\n            + file_text_lines[insert_line:]\n        )\n\n        # Create a snippet for preview\n        snippet_lines = (\n            file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]\n            + new_str_lines\n            + file_text_lines[insert_line : insert_line + SNIPPET_LINES]\n        )\n\n        # Join lines and write to file\n        new_file_text = \"\\n\".join(new_file_text_lines)\n        snippet = \"\\n\".join(snippet_lines)\n\n        await operator.write_file(path, new_file_text)\n        self._file_history[path].append(file_text)\n\n        # Prepare success message\n        success_msg = f\"The file {path} has been edited. \"\n        success_msg += self._make_output(\n            snippet,\n            \"a snippet of the edited file\",\n            max(1, insert_line - SNIPPET_LINES + 1),\n        )\n        success_msg += \"Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary.\"\n\n        return CLIResult(output=success_msg)\n\n    async def undo_edit(\n        self, path: PathLike, operator: FileOperator = None\n    ) -> CLIResult:\n        \"\"\"Revert the last edit made to a file.\"\"\"\n        if not self._file_history[path]:\n            raise ToolError(f\"No edit history found for {path}.\")\n\n        old_text = self._file_history[path].pop()\n        await operator.write_file(path, old_text)\n\n        return CLIResult(\n            output=f\"Last edit to {path} undone successfully. {self._make_output(old_text, str(path))}\"\n        )\n\n    def _make_output(\n        self,\n        file_content: str,\n        file_descriptor: str,\n        init_line: int = 1,\n        expand_tabs: bool = True,\n    ) -> str:\n        \"\"\"Format file content for display with line numbers.\"\"\"\n        file_content = maybe_truncate(file_content)\n        if expand_tabs:\n            file_content = file_content.expandtabs()\n\n        # Add line numbers to each line\n        file_content = \"\\n\".join(\n            [\n                f\"{i + init_line:6}\\t{line}\"\n                for i, line in enumerate(file_content.split(\"\\n\"))\n            ]\n        )\n\n        return (\n            f\"Here's the result of running `cat -n` on {file_descriptor}:\\n\"\n            + file_content\n            + \"\\n\"\n        )\n"
  },
  {
    "path": "app/tool/terminate.py",
    "content": "from app.tool.base import BaseTool\n\n\n_TERMINATE_DESCRIPTION = \"\"\"Terminate the interaction when the request is met OR if the assistant cannot proceed further with the task.\nWhen you have finished all the tasks, call this tool to end the work.\"\"\"\n\n\nclass Terminate(BaseTool):\n    name: str = \"terminate\"\n    description: str = _TERMINATE_DESCRIPTION\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"status\": {\n                \"type\": \"string\",\n                \"description\": \"The finish status of the interaction.\",\n                \"enum\": [\"success\", \"failure\"],\n            }\n        },\n        \"required\": [\"status\"],\n    }\n\n    async def execute(self, status: str) -> str:\n        \"\"\"Finish the current execution\"\"\"\n        return f\"The interaction has been completed with status: {status}\"\n"
  },
  {
    "path": "app/tool/tool_collection.py",
    "content": "\"\"\"Collection classes for managing multiple tools.\"\"\"\nfrom typing import Any, Dict, List\n\nfrom app.exceptions import ToolError\nfrom app.logger import logger\nfrom app.tool.base import BaseTool, ToolFailure, ToolResult\n\n\nclass ToolCollection:\n    \"\"\"A collection of defined tools.\"\"\"\n\n    class Config:\n        arbitrary_types_allowed = True\n\n    def __init__(self, *tools: BaseTool):\n        self.tools = tools\n        self.tool_map = {tool.name: tool for tool in tools}\n\n    def __iter__(self):\n        return iter(self.tools)\n\n    def to_params(self) -> List[Dict[str, Any]]:\n        return [tool.to_param() for tool in self.tools]\n\n    async def execute(\n        self, *, name: str, tool_input: Dict[str, Any] = None\n    ) -> ToolResult:\n        tool = self.tool_map.get(name)\n        if not tool:\n            return ToolFailure(error=f\"Tool {name} is invalid\")\n        try:\n            result = await tool(**tool_input)\n            return result\n        except ToolError as e:\n            return ToolFailure(error=e.message)\n\n    async def execute_all(self) -> List[ToolResult]:\n        \"\"\"Execute all tools in the collection sequentially.\"\"\"\n        results = []\n        for tool in self.tools:\n            try:\n                result = await tool()\n                results.append(result)\n            except ToolError as e:\n                results.append(ToolFailure(error=e.message))\n        return results\n\n    def get_tool(self, name: str) -> BaseTool:\n        return self.tool_map.get(name)\n\n    def add_tool(self, tool: BaseTool):\n        \"\"\"Add a single tool to the collection.\n\n        If a tool with the same name already exists, it will be skipped and a warning will be logged.\n        \"\"\"\n        if tool.name in self.tool_map:\n            logger.warning(f\"Tool {tool.name} already exists in collection, skipping\")\n            return self\n\n        self.tools += (tool,)\n        self.tool_map[tool.name] = tool\n        return self\n\n    def add_tools(self, *tools: BaseTool):\n        \"\"\"Add multiple tools to the collection.\n\n        If any tool has a name conflict with an existing tool, it will be skipped and a warning will be logged.\n        \"\"\"\n        for tool in tools:\n            self.add_tool(tool)\n        return self\n"
  },
  {
    "path": "app/tool/web_search.py",
    "content": "import asyncio\nfrom typing import Any, Dict, List, Optional\n\nimport requests\nfrom bs4 import BeautifulSoup\nfrom pydantic import BaseModel, ConfigDict, Field, model_validator\nfrom tenacity import retry, stop_after_attempt, wait_exponential\n\nfrom app.config import config\nfrom app.logger import logger\nfrom app.tool.base import BaseTool, ToolResult\nfrom app.tool.search import (\n    BaiduSearchEngine,\n    BingSearchEngine,\n    DuckDuckGoSearchEngine,\n    GoogleSearchEngine,\n    WebSearchEngine,\n)\nfrom app.tool.search.base import SearchItem\n\n\nclass SearchResult(BaseModel):\n    \"\"\"Represents a single search result returned by a search engine.\"\"\"\n\n    model_config = ConfigDict(arbitrary_types_allowed=True)\n\n    position: int = Field(description=\"Position in search results\")\n    url: str = Field(description=\"URL of the search result\")\n    title: str = Field(default=\"\", description=\"Title of the search result\")\n    description: str = Field(\n        default=\"\", description=\"Description or snippet of the search result\"\n    )\n    source: str = Field(description=\"The search engine that provided this result\")\n    raw_content: Optional[str] = Field(\n        default=None, description=\"Raw content from the search result page if available\"\n    )\n\n    def __str__(self) -> str:\n        \"\"\"String representation of a search result.\"\"\"\n        return f\"{self.title} ({self.url})\"\n\n\nclass SearchMetadata(BaseModel):\n    \"\"\"Metadata about the search operation.\"\"\"\n\n    model_config = ConfigDict(arbitrary_types_allowed=True)\n\n    total_results: int = Field(description=\"Total number of results found\")\n    language: str = Field(description=\"Language code used for the search\")\n    country: str = Field(description=\"Country code used for the search\")\n\n\nclass SearchResponse(ToolResult):\n    \"\"\"Structured response from the web search tool, inheriting ToolResult.\"\"\"\n\n    query: str = Field(description=\"The search query that was executed\")\n    results: List[SearchResult] = Field(\n        default_factory=list, description=\"List of search results\"\n    )\n    metadata: Optional[SearchMetadata] = Field(\n        default=None, description=\"Metadata about the search\"\n    )\n\n    @model_validator(mode=\"after\")\n    def populate_output(self) -> \"SearchResponse\":\n        \"\"\"Populate output or error fields based on search results.\"\"\"\n        if self.error:\n            return self\n\n        result_text = [f\"Search results for '{self.query}':\"]\n\n        for i, result in enumerate(self.results, 1):\n            # Add title with position number\n            title = result.title.strip() or \"No title\"\n            result_text.append(f\"\\n{i}. {title}\")\n\n            # Add URL with proper indentation\n            result_text.append(f\"   URL: {result.url}\")\n\n            # Add description if available\n            if result.description.strip():\n                result_text.append(f\"   Description: {result.description}\")\n\n            # Add content preview if available\n            if result.raw_content:\n                content_preview = result.raw_content[:1000].replace(\"\\n\", \" \").strip()\n                if len(result.raw_content) > 1000:\n                    content_preview += \"...\"\n                result_text.append(f\"   Content: {content_preview}\")\n\n        # Add metadata at the bottom if available\n        if self.metadata:\n            result_text.extend(\n                [\n                    f\"\\nMetadata:\",\n                    f\"- Total results: {self.metadata.total_results}\",\n                    f\"- Language: {self.metadata.language}\",\n                    f\"- Country: {self.metadata.country}\",\n                ]\n            )\n\n        self.output = \"\\n\".join(result_text)\n        return self\n\n\nclass WebContentFetcher:\n    \"\"\"Utility class for fetching web content.\"\"\"\n\n    @staticmethod\n    async def fetch_content(url: str, timeout: int = 10) -> Optional[str]:\n        \"\"\"\n        Fetch and extract the main content from a webpage.\n\n        Args:\n            url: The URL to fetch content from\n            timeout: Request timeout in seconds\n\n        Returns:\n            Extracted text content or None if fetching fails\n        \"\"\"\n        headers = {\n            \"WebSearch\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\"\n        }\n\n        try:\n            # Use asyncio to run requests in a thread pool\n            response = await asyncio.get_event_loop().run_in_executor(\n                None, lambda: requests.get(url, headers=headers, timeout=timeout)\n            )\n\n            if response.status_code != 200:\n                logger.warning(\n                    f\"Failed to fetch content from {url}: HTTP {response.status_code}\"\n                )\n                return None\n\n            # Parse HTML with BeautifulSoup\n            soup = BeautifulSoup(response.text, \"html.parser\")\n\n            # Remove script and style elements\n            for script in soup([\"script\", \"style\", \"header\", \"footer\", \"nav\"]):\n                script.extract()\n\n            # Get text content\n            text = soup.get_text(separator=\"\\n\", strip=True)\n\n            # Clean up whitespace and limit size (100KB max)\n            text = \" \".join(text.split())\n            return text[:10000] if text else None\n\n        except Exception as e:\n            logger.warning(f\"Error fetching content from {url}: {e}\")\n            return None\n\n\nclass WebSearch(BaseTool):\n    \"\"\"Search the web for information using various search engines.\"\"\"\n\n    name: str = \"web_search\"\n    description: str = \"\"\"Search the web for real-time information about any topic.\n    This tool returns comprehensive search results with relevant information, URLs, titles, and descriptions.\n    If the primary search engine fails, it automatically falls back to alternative engines.\"\"\"\n    parameters: dict = {\n        \"type\": \"object\",\n        \"properties\": {\n            \"query\": {\n                \"type\": \"string\",\n                \"description\": \"(required) The search query to submit to the search engine.\",\n            },\n            \"num_results\": {\n                \"type\": \"integer\",\n                \"description\": \"(optional) The number of search results to return. Default is 5.\",\n                \"default\": 5,\n            },\n            \"lang\": {\n                \"type\": \"string\",\n                \"description\": \"(optional) Language code for search results (default: en).\",\n                \"default\": \"en\",\n            },\n            \"country\": {\n                \"type\": \"string\",\n                \"description\": \"(optional) Country code for search results (default: us).\",\n                \"default\": \"us\",\n            },\n            \"fetch_content\": {\n                \"type\": \"boolean\",\n                \"description\": \"(optional) Whether to fetch full content from result pages. Default is false.\",\n                \"default\": False,\n            },\n        },\n        \"required\": [\"query\"],\n    }\n    _search_engine: dict[str, WebSearchEngine] = {\n        \"google\": GoogleSearchEngine(),\n        \"baidu\": BaiduSearchEngine(),\n        \"duckduckgo\": DuckDuckGoSearchEngine(),\n        \"bing\": BingSearchEngine(),\n    }\n    content_fetcher: WebContentFetcher = WebContentFetcher()\n\n    async def execute(\n        self,\n        query: str,\n        num_results: int = 5,\n        lang: Optional[str] = None,\n        country: Optional[str] = None,\n        fetch_content: bool = False,\n    ) -> SearchResponse:\n        \"\"\"\n        Execute a Web search and return detailed search results.\n\n        Args:\n            query: The search query to submit to the search engine\n            num_results: The number of search results to return (default: 5)\n            lang: Language code for search results (default from config)\n            country: Country code for search results (default from config)\n            fetch_content: Whether to fetch content from result pages (default: False)\n\n        Returns:\n            A structured response containing search results and metadata\n        \"\"\"\n        # Get settings from config\n        retry_delay = (\n            getattr(config.search_config, \"retry_delay\", 60)\n            if config.search_config\n            else 60\n        )\n        max_retries = (\n            getattr(config.search_config, \"max_retries\", 3)\n            if config.search_config\n            else 3\n        )\n\n        # Use config values for lang and country if not specified\n        if lang is None:\n            lang = (\n                getattr(config.search_config, \"lang\", \"en\")\n                if config.search_config\n                else \"en\"\n            )\n\n        if country is None:\n            country = (\n                getattr(config.search_config, \"country\", \"us\")\n                if config.search_config\n                else \"us\"\n            )\n\n        search_params = {\"lang\": lang, \"country\": country}\n\n        # Try searching with retries when all engines fail\n        for retry_count in range(max_retries + 1):\n            results = await self._try_all_engines(query, num_results, search_params)\n\n            if results:\n                # Fetch content if requested\n                if fetch_content:\n                    results = await self._fetch_content_for_results(results)\n\n                # Return a successful structured response\n                return SearchResponse(\n                    status=\"success\",\n                    query=query,\n                    results=results,\n                    metadata=SearchMetadata(\n                        total_results=len(results),\n                        language=lang,\n                        country=country,\n                    ),\n                )\n\n            if retry_count < max_retries:\n                # All engines failed, wait and retry\n                logger.warning(\n                    f\"All search engines failed. Waiting {retry_delay} seconds before retry {retry_count + 1}/{max_retries}...\"\n                )\n                await asyncio.sleep(retry_delay)\n            else:\n                logger.error(\n                    f\"All search engines failed after {max_retries} retries. Giving up.\"\n                )\n\n        # Return an error response\n        return SearchResponse(\n            query=query,\n            error=\"All search engines failed to return results after multiple retries.\",\n            results=[],\n        )\n\n    async def _try_all_engines(\n        self, query: str, num_results: int, search_params: Dict[str, Any]\n    ) -> List[SearchResult]:\n        \"\"\"Try all search engines in the configured order.\"\"\"\n        engine_order = self._get_engine_order()\n        failed_engines = []\n\n        for engine_name in engine_order:\n            engine = self._search_engine[engine_name]\n            logger.info(f\"🔎 Attempting search with {engine_name.capitalize()}...\")\n            search_items = await self._perform_search_with_engine(\n                engine, query, num_results, search_params\n            )\n\n            if not search_items:\n                continue\n\n            if failed_engines:\n                logger.info(\n                    f\"Search successful with {engine_name.capitalize()} after trying: {', '.join(failed_engines)}\"\n                )\n\n            # Transform search items into structured results\n            return [\n                SearchResult(\n                    position=i + 1,\n                    url=item.url,\n                    title=item.title\n                    or f\"Result {i+1}\",  # Ensure we always have a title\n                    description=item.description or \"\",\n                    source=engine_name,\n                )\n                for i, item in enumerate(search_items)\n            ]\n\n        if failed_engines:\n            logger.error(f\"All search engines failed: {', '.join(failed_engines)}\")\n        return []\n\n    async def _fetch_content_for_results(\n        self, results: List[SearchResult]\n    ) -> List[SearchResult]:\n        \"\"\"Fetch and add web content to search results.\"\"\"\n        if not results:\n            return []\n\n        # Create tasks for each result\n        tasks = [self._fetch_single_result_content(result) for result in results]\n\n        # Type annotation to help type checker\n        fetched_results = await asyncio.gather(*tasks)\n\n        # Explicit validation of return type\n        return [\n            (\n                result\n                if isinstance(result, SearchResult)\n                else SearchResult(**result.dict())\n            )\n            for result in fetched_results\n        ]\n\n    async def _fetch_single_result_content(self, result: SearchResult) -> SearchResult:\n        \"\"\"Fetch content for a single search result.\"\"\"\n        if result.url:\n            content = await self.content_fetcher.fetch_content(result.url)\n            if content:\n                result.raw_content = content\n        return result\n\n    def _get_engine_order(self) -> List[str]:\n        \"\"\"Determines the order in which to try search engines.\"\"\"\n        preferred = (\n            getattr(config.search_config, \"engine\", \"google\").lower()\n            if config.search_config\n            else \"google\"\n        )\n        fallbacks = (\n            [engine.lower() for engine in config.search_config.fallback_engines]\n            if config.search_config\n            and hasattr(config.search_config, \"fallback_engines\")\n            else []\n        )\n\n        # Start with preferred engine, then fallbacks, then remaining engines\n        engine_order = [preferred] if preferred in self._search_engine else []\n        engine_order.extend(\n            [\n                fb\n                for fb in fallbacks\n                if fb in self._search_engine and fb not in engine_order\n            ]\n        )\n        engine_order.extend([e for e in self._search_engine if e not in engine_order])\n\n        return engine_order\n\n    @retry(\n        stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10)\n    )\n    async def _perform_search_with_engine(\n        self,\n        engine: WebSearchEngine,\n        query: str,\n        num_results: int,\n        search_params: Dict[str, Any],\n    ) -> List[SearchItem]:\n        \"\"\"Execute search with the given engine and parameters.\"\"\"\n        return await asyncio.get_event_loop().run_in_executor(\n            None,\n            lambda: list(\n                engine.perform_search(\n                    query,\n                    num_results=num_results,\n                    lang=search_params.get(\"lang\"),\n                    country=search_params.get(\"country\"),\n                )\n            ),\n        )\n\n\nif __name__ == \"__main__\":\n    web_search = WebSearch()\n    search_response = asyncio.run(\n        web_search.execute(\n            query=\"Python programming\", fetch_content=True, num_results=1\n        )\n    )\n    print(search_response.to_tool_result())\n"
  },
  {
    "path": "app/utils/__init__.py",
    "content": "# Utility functions and constants for agent tools\n"
  },
  {
    "path": "app/utils/files_utils.py",
    "content": "import os\n\n\n# Files to exclude from operations\nEXCLUDED_FILES = {\n    \".DS_Store\",\n    \".gitignore\",\n    \"package-lock.json\",\n    \"postcss.config.js\",\n    \"postcss.config.mjs\",\n    \"jsconfig.json\",\n    \"components.json\",\n    \"tsconfig.tsbuildinfo\",\n    \"tsconfig.json\",\n}\n\n# Directories to exclude from operations\nEXCLUDED_DIRS = {\"node_modules\", \".next\", \"dist\", \"build\", \".git\"}\n\n# File extensions to exclude from operations\nEXCLUDED_EXT = {\n    \".ico\",\n    \".svg\",\n    \".png\",\n    \".jpg\",\n    \".jpeg\",\n    \".gif\",\n    \".bmp\",\n    \".tiff\",\n    \".webp\",\n    \".db\",\n    \".sql\",\n}\n\n\ndef should_exclude_file(rel_path: str) -> bool:\n    \"\"\"Check if a file should be excluded based on path, name, or extension\n\n    Args:\n        rel_path: Relative path of the file to check\n\n    Returns:\n        True if the file should be excluded, False otherwise\n    \"\"\"\n    # Check filename\n    filename = os.path.basename(rel_path)\n    if filename in EXCLUDED_FILES:\n        return True\n\n    # Check directory\n    dir_path = os.path.dirname(rel_path)\n    if any(excluded in dir_path for excluded in EXCLUDED_DIRS):\n        return True\n\n    # Check extension\n    _, ext = os.path.splitext(filename)\n    if ext.lower() in EXCLUDED_EXT:\n        return True\n\n    return False\n\n\ndef clean_path(path: str, workspace_path: str = \"/workspace\") -> str:\n    \"\"\"Clean and normalize a path to be relative to the workspace\n\n    Args:\n        path: The path to clean\n        workspace_path: The base workspace path to remove (default: \"/workspace\")\n\n    Returns:\n        The cleaned path, relative to the workspace\n    \"\"\"\n    # Remove any leading slash\n    path = path.lstrip(\"/\")\n\n    # Remove workspace prefix if present\n    if path.startswith(workspace_path.lstrip(\"/\")):\n        path = path[len(workspace_path.lstrip(\"/\")) :]\n\n    # Remove workspace/ prefix if present\n    if path.startswith(\"workspace/\"):\n        path = path[9:]\n\n    # Remove any remaining leading slash\n    path = path.lstrip(\"/\")\n\n    return path\n"
  },
  {
    "path": "app/utils/logger.py",
    "content": "import logging\nimport os\n\nimport structlog\n\n\nENV_MODE = os.getenv(\"ENV_MODE\", \"LOCAL\")\n\nrenderer = [structlog.processors.JSONRenderer()]\nif ENV_MODE.lower() == \"local\".lower():\n    renderer = [structlog.dev.ConsoleRenderer()]\n\nstructlog.configure(\n    processors=[\n        structlog.stdlib.add_log_level,\n        structlog.stdlib.PositionalArgumentsFormatter(),\n        structlog.processors.dict_tracebacks,\n        structlog.processors.CallsiteParameterAdder(\n            {\n                structlog.processors.CallsiteParameter.FILENAME,\n                structlog.processors.CallsiteParameter.FUNC_NAME,\n                structlog.processors.CallsiteParameter.LINENO,\n            }\n        ),\n        structlog.processors.TimeStamper(fmt=\"iso\"),\n        structlog.contextvars.merge_contextvars,\n        *renderer,\n    ],\n    cache_logger_on_first_use=True,\n)\n\nlogger: structlog.stdlib.BoundLogger = structlog.get_logger(level=logging.DEBUG)\n"
  },
  {
    "path": "config/.gitignore",
    "content": "# prevent the local config file from being uploaded to the remote repository\nconfig.toml\n"
  },
  {
    "path": "config/config.example-daytona.toml",
    "content": "# Global LLM configuration\n[llm]\nmodel = \"claude-3-7-sonnet-20250219\"       # The LLM model to use\nbase_url = \"https://api.anthropic.com/v1/\" # API endpoint URL\napi_key = \"YOUR_API_KEY\"                   # Your API key\nmax_tokens = 8192                          # Maximum number of tokens in the response\ntemperature = 0.0                          # Controls randomness\n\n# [llm] # Amazon Bedrock\n# api_type = \"aws\"                                       # Required\n# model = \"us.anthropic.claude-3-7-sonnet-20250219-v1:0\" # Bedrock supported modelID\n# base_url = \"bedrock-runtime.us-west-2.amazonaws.com\"   # Not used now\n# max_tokens = 8192\n# temperature = 1.0\n# api_key = \"bear\"                                       # Required but not used for Bedrock\n\n# [llm] #AZURE OPENAI:\n# api_type= 'azure'\n# model = \"YOUR_MODEL_NAME\" #\"gpt-4o-mini\"\n# base_url = \"{YOUR_AZURE_ENDPOINT.rstrip('/')}/openai/deployments/{AZURE_DEPLOYMENT_ID}\"\n# api_key = \"AZURE API KEY\"\n# max_tokens = 8096\n# temperature = 0.0\n# api_version=\"AZURE API VERSION\" #\"2024-08-01-preview\"\n\n# [llm] #OLLAMA:\n# api_type = 'ollama'\n# model = \"llama3.2\"\n# base_url = \"http://localhost:11434/v1\"\n# api_key = \"ollama\"\n# max_tokens = 4096\n# temperature = 0.0\n\n# Optional configuration for specific LLM models\n[llm.vision]\nmodel = \"claude-3-7-sonnet-20250219\"       # The vision model to use\nbase_url = \"https://api.anthropic.com/v1/\" # API endpoint URL for vision model\napi_key = \"YOUR_API_KEY\"                   # Your API key for vision model\nmax_tokens = 8192                          # Maximum number of tokens in the response\ntemperature = 0.0                          # Controls randomness for vision model\n\n# [llm.vision] #OLLAMA VISION:\n# api_type = 'ollama'\n# model = \"llama3.2-vision\"\n# base_url = \"http://localhost:11434/v1\"\n# api_key = \"ollama\"\n# max_tokens = 4096\n# temperature = 0.0\n\n# Optional configuration for specific browser configuration\n# [browser]\n# Whether to run browser in headless mode (default: false)\n#headless = false\n# Disable browser security features (default: true)\n#disable_security = true\n# Extra arguments to pass to the browser\n#extra_chromium_args = []\n# Path to a Chrome instance to use to connect to your normal browser\n# e.g. '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'\n#chrome_instance_path = \"\"\n# Connect to a browser instance via WebSocket\n#wss_url = \"\"\n# Connect to a browser instance via CDP\n#cdp_url = \"\"\n\n# Optional configuration, Proxy settings for the browser\n# [browser.proxy]\n# server = \"http://proxy-server:port\"\n# username = \"proxy-username\"\n# password = \"proxy-password\"\n\n# Optional configuration, Search settings.\n# [search]\n# Search engine for agent to use. Default is \"Google\", can be set to \"Baidu\" or \"DuckDuckGo\" or \"Bing\".\n#engine = \"Google\"\n# Fallback engine order. Default is [\"DuckDuckGo\", \"Baidu\", \"Bing\"] - will try in this order after primary engine fails.\n#fallback_engines = [\"DuckDuckGo\", \"Baidu\", \"Bing\"]\n# Seconds to wait before retrying all engines again when they all fail due to rate limits. Default is 60.\n#retry_delay = 60\n# Maximum number of times to retry all engines when all fail. Default is 3.\n#max_retries = 3\n# Language code for search results. Options: \"en\" (English), \"zh\" (Chinese), etc.\n#lang = \"en\"\n# Country code for search results. Options: \"us\" (United States), \"cn\" (China), etc.\n#country = \"us\"\n\n\n## Sandbox configuration\n#[sandbox]\n#use_sandbox = false\n#image = \"python:3.12-slim\"\n#work_dir = \"/workspace\"\n#memory_limit = \"1g\"  # 512m\n#cpu_limit = 2.0\n#timeout = 300\n#network_enabled = true\n\n# Daytona configuration\n[daytona]\ndaytona_api_key = \"\"\n#daytona_server_url = \"https://app.daytona.io/api\"\n#daytona_target = \"us\"                                   #Daytona is currently available in the following regions:United States (us)、Europe (eu)\n#sandbox_image_name = \"whitezxj/sandbox:0.1.0\"           #If you don't use this default image,sandbox tools may be useless\n#sandbox_entrypoint = \"/usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf\"   #If you change this entrypoint,server in sandbox may be useless\n#VNC_password =                                          #The password you set to log in sandbox by VNC,it will be 123456 if you don't set\n\n# MCP (Model Context Protocol) configuration\n[mcp]\nserver_reference = \"app.mcp.server\" # default server module reference\n\n# Optional Runflow configuration\n# Your can add additional agents into run-flow workflow to solve different-type tasks.\n[runflow]\nuse_data_analysis_agent = false     # The Data Analysi Agent to solve various data analysis tasks\n"
  },
  {
    "path": "config/config.example-model-anthropic.toml",
    "content": "# Global LLM configuration\n[llm]\nmodel = \"claude-3-7-sonnet-latest\"        # The LLM model to use\nbase_url = \"https://api.anthropic.com/v1/\"  # API endpoint URL\napi_key = \"YOUR_API_KEY\"                    # Your API key\nmax_tokens = 8192                           # Maximum number of tokens in the response\ntemperature = 0.0                           # Controls randomness\n\n\n# Optional configuration for specific LLM models\n[llm.vision]\nmodel = \"claude-3-7-sonnet-20250219\"        # The vision model to use\nbase_url = \"https://api.anthropic.com/v1/\"  # API endpoint URL for vision model\napi_key = \"YOUR_API_KEY\"                    # Your API key for vision model\nmax_tokens = 8192                           # Maximum number of tokens in the response\ntemperature = 0.0                           # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example-model-azure.toml",
    "content": "# Global LLM configuration\n[llm] #AZURE OPENAI:\napi_type= 'azure'\nmodel = \"gpt-4o-mini\"                                                                   # The LLM model to use\nbase_url = \"{YOUR_AZURE_ENDPOINT.rstrip('/')}/openai/deployments/{AZURE_DEPLOYMENT_ID}\" # API endpoint URL\napi_key = \"YOUR_API_KEY\"                                                                # Your API key\nmax_tokens = 8096                                                                       # Maximum number of tokens in the response\ntemperature = 0.0                                                                       # Controls randomness\napi_version=\"AZURE API VERSION\" #\"2024-08-01-preview\"                                   # Azure Openai version if AzureOpenai\n\n\n# Optional configuration for specific LLM models\n[llm.vision]\nmodel = \"gpt-4o\"        # The vision model to use\nbase_url = \"{YOUR_AZURE_ENDPOINT.rstrip('/')}/openai/deployments/{AZURE_DEPLOYMENT_ID}\"\napi_key = \"YOUR_API_KEY\"                    # Your API key for vision model\nmax_tokens = 8192                           # Maximum number of tokens in the response\ntemperature = 0.0                           # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example-model-google.toml",
    "content": " # Global LLM configuration\n[llm]\nmodel = \"gemini-2.0-flash\"                                              # The LLM model to use\nbase_url = \"https://generativelanguage.googleapis.com/v1beta/openai/\"   # API endpoint URL\napi_key = \"YOUR_API_KEY\"                                                # Your API key\ntemperature = 0.0                                                       # Controls randomness\nmax_tokens = 8096                                                       # Maximum number of tokens in the response\n\n\n# Optional configuration for specific LLM models for Google\n[llm.vision]\nmodel = \"gemini-2.0-flash-exp\"                                      # The vision model to use\nbase_url = \"https://generativelanguage.googleapis.com/v1beta/openai/\"  # API endpoint URL for vision model\napi_key = \"YOUR_API_KEY\"                                               # Your API key for vision model\nmax_tokens = 8192                                                      # Maximum number of tokens in the response\ntemperature = 0.0                                                      # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example-model-jiekouai.toml",
    "content": "# Global LLM configuration\n[llm] #Jiekou.AI:\napi_type = 'jiekou'\nmodel = \"claude-sonnet-4-5-20250929\"                               # The LLM model to use\nbase_url = \"https://api.jiekou.ai/openai\"                          # API endpoint URL\napi_key = \"your Jiekou.AI api key\"                                 # Your API key\nmax_tokens = 64000                                                 # Maximum number of tokens in the response\ntemperature = 0.0                                                  # Controls randomness\n\n\n[llm.vision] #Jiekou.AI VISION:\napi_type = 'jiekou'\nmodel = \"claude-sonnet-4-5-20250929\"                             # The vision model to use\nbase_url = \"https://api.jiekou.ai/openai\"                        # API endpoint URL for vision model\napi_key = \"your Jiekou.AI api key\"                               # Your API key for vision model\nmax_tokens = 64000                                               # Maximum number of tokens in the response\ntemperature = 0.0                                                # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example-model-ollama.toml",
    "content": "# Global LLM configuration\n[llm] #OLLAMA:\napi_type = 'ollama'\nmodel = \"llama3.2\"                                                 # The LLM model to use\nbase_url = \"http://localhost:11434/v1\"                             # API endpoint URL\napi_key = \"ollama\"                                                 # Your API key\nmax_tokens = 4096                                                  # Maximum number of tokens in the response\ntemperature = 0.0                                                  # Controls randomness\n\n\n[llm.vision] #OLLAMA VISION:\napi_type = 'ollama'\nmodel = \"llama3.2-vision\"                                          # The vision model to use\nbase_url = \"http://localhost:11434/v1\"                             # API endpoint URL for vision model\napi_key = \"ollama\"                                                 # Your API key for vision model\nmax_tokens = 4096                                                  # Maximum number of tokens in the response\ntemperature = 0.0                                                  # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example-model-ppio.toml",
    "content": "# Global LLM configuration\n[llm] #PPIO:\napi_type = 'ppio'\nmodel = \"deepseek/deepseek-v3-0324\"                                # The LLM model to use\nbase_url = \"https://api.ppinfra.com/v3/openai\"                     # API endpoint URL\napi_key = \"your ppio api key\"                                      # Your API key\nmax_tokens = 16000                                                 # Maximum number of tokens in the response\ntemperature = 0.0                                                  # Controls randomness\n\n\n[llm.vision] #PPIO VISION:\napi_type = 'ppio'\nmodel = \"qwen/qwen2.5-vl-72b-instruct\"                             # The vision model to use\nbase_url = \"https://api.ppinfra.com/v3/openai\"                     # API endpoint URL for vision model\napi_key = \"your ppio api key\"                                      # Your API key for vision model\nmax_tokens = 96000                                                 # Maximum number of tokens in the response\ntemperature = 0.0                                                  # Controls randomness for vision model\n"
  },
  {
    "path": "config/config.example.toml",
    "content": "# Global LLM configuration\n[llm]\nmodel = \"claude-3-7-sonnet-20250219\"       # The LLM model to use\nbase_url = \"https://api.anthropic.com/v1/\" # API endpoint URL\napi_key = \"YOUR_API_KEY\"                   # Your API key\nmax_tokens = 8192                          # Maximum number of tokens in the response\ntemperature = 0.0                          # Controls randomness\n\n# [llm] # Amazon Bedrock\n# api_type = \"aws\"                                       # Required\n# model = \"us.anthropic.claude-3-7-sonnet-20250219-v1:0\" # Bedrock supported modelID\n# base_url = \"bedrock-runtime.us-west-2.amazonaws.com\"   # Not used now\n# max_tokens = 8192\n# temperature = 1.0\n# api_key = \"bear\"                                       # Required but not used for Bedrock\n\n# [llm] #AZURE OPENAI:\n# api_type= 'azure'\n# model = \"YOUR_MODEL_NAME\" #\"gpt-4o-mini\"\n# base_url = \"{YOUR_AZURE_ENDPOINT.rstrip('/')}/openai/deployments/{AZURE_DEPLOYMENT_ID}\"\n# api_key = \"AZURE API KEY\"\n# max_tokens = 8096\n# temperature = 0.0\n# api_version=\"AZURE API VERSION\" #\"2024-08-01-preview\"\n\n# [llm] #OLLAMA:\n# api_type = 'ollama'\n# model = \"llama3.2\"\n# base_url = \"http://localhost:11434/v1\"\n# api_key = \"ollama\"\n# max_tokens = 4096\n# temperature = 0.0\n\n# [llm] #Jiekou.AI:\n# api_type = 'jiekou'\n# model = \"claude-sonnet-4-5-20250929\"                               # The LLM model to use\n# base_url = \"https://api.jiekou.ai/openai\"                          # API endpoint URL\n# api_key = \"your Jiekou.AI api key\"                                 # Your API key\n# max_tokens = 64000                                                 # Maximum number of tokens in the response\n# temperature = 0.0                                                  # Controls randomness\n\n# Optional configuration for specific LLM models\n[llm.vision]\nmodel = \"claude-3-7-sonnet-20250219\"       # The vision model to use\nbase_url = \"https://api.anthropic.com/v1/\" # API endpoint URL for vision model\napi_key = \"YOUR_API_KEY\"                   # Your API key for vision model\nmax_tokens = 8192                          # Maximum number of tokens in the response\ntemperature = 0.0                          # Controls randomness for vision model\n\n# [llm.vision] #OLLAMA VISION:\n# api_type = 'ollama'\n# model = \"llama3.2-vision\"\n# base_url = \"http://localhost:11434/v1\"\n# api_key = \"ollama\"\n# max_tokens = 4096\n# temperature = 0.0\n\n# Optional configuration for specific browser configuration\n# [browser]\n# Whether to run browser in headless mode (default: false)\n#headless = false\n# Disable browser security features (default: true)\n#disable_security = true\n# Extra arguments to pass to the browser\n#extra_chromium_args = []\n# Path to a Chrome instance to use to connect to your normal browser\n# e.g. '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'\n#chrome_instance_path = \"\"\n# Connect to a browser instance via WebSocket\n#wss_url = \"\"\n# Connect to a browser instance via CDP\n#cdp_url = \"\"\n\n# Optional configuration, Proxy settings for the browser\n# [browser.proxy]\n# server = \"http://proxy-server:port\"\n# username = \"proxy-username\"\n# password = \"proxy-password\"\n\n# Optional configuration, Search settings.\n# [search]\n# Search engine for agent to use. Default is \"Google\", can be set to \"Baidu\" or \"DuckDuckGo\" or \"Bing\".\n#engine = \"Google\"\n# Fallback engine order. Default is [\"DuckDuckGo\", \"Baidu\", \"Bing\"] - will try in this order after primary engine fails.\n#fallback_engines = [\"DuckDuckGo\", \"Baidu\", \"Bing\"]\n# Seconds to wait before retrying all engines again when they all fail due to rate limits. Default is 60.\n#retry_delay = 60\n# Maximum number of times to retry all engines when all fail. Default is 3.\n#max_retries = 3\n# Language code for search results. Options: \"en\" (English), \"zh\" (Chinese), etc.\n#lang = \"en\"\n# Country code for search results. Options: \"us\" (United States), \"cn\" (China), etc.\n#country = \"us\"\n\n\n## Sandbox configuration\n#[sandbox]\n#use_sandbox = false\n#image = \"python:3.12-slim\"\n#work_dir = \"/workspace\"\n#memory_limit = \"1g\"  # 512m\n#cpu_limit = 2.0\n#timeout = 300\n#network_enabled = true\n\n# MCP (Model Context Protocol) configuration\n[mcp]\nserver_reference = \"app.mcp.server\" # default server module reference\n\n# Optional Runflow configuration\n# Your can add additional agents into run-flow workflow to solve different-type tasks.\n[runflow]\nuse_data_analysis_agent = false     # The Data Analysi Agent to solve various data analysis tasks\n"
  },
  {
    "path": "config/mcp.example.json",
    "content": "{\n    \"mcpServers\": {\n      \"server1\": {\n        \"type\": \"sse\",\n        \"url\": \"http://localhost:8000/sse\"\n      }\n    }\n}\n"
  },
  {
    "path": "examples/benchmarks/__init__.py",
    "content": "\"\"\"\nOpenManus benchmark system for standardized agent evaluation.\n\"\"\"\n"
  },
  {
    "path": "examples/use_case/japan-travel-plan/japan_travel_guide_instructions.txt",
    "content": "JAPAN TRAVEL HANDBOOK - GUIDE TO VERSIONS\n\nLocation: D:/OpenManus/\n\n1. DETAILED DIGITAL VERSION\nFile: japan_travel_handbook.html\nBest for: Desktop/laptop viewing\nFeatures:\n- Complete comprehensive guide\n- Detailed itinerary\n- Full proposal planning section\n- All hotel recommendations\n- Comprehensive budget breakdown\nUsage: Open in web browser for trip planning and detailed reference\n\n2. PRINT-FRIENDLY VERSION\nFile: japan_travel_handbook_print.html\nBest for: Physical reference during travel\nFeatures:\n- Condensed essential information\n- Optimized for paper printing\n- Clear, printer-friendly formatting\n- Quick reference tables\nUsage: Print and keep in travel documents folder\n\n3. MOBILE-OPTIMIZED VERSION\nFile: japan_travel_handbook_mobile.html\nBest for: On-the-go reference during trip\nFeatures:\n- Touch-friendly interface\n- Collapsible sections\n- Quick access emergency buttons\n- Dark mode support\n- Responsive design\nUsage: Save to phone's browser bookmarks for quick access\n\nRECOMMENDED SETUP:\n1. Before Trip:\n   - Use detailed version for planning\n   - Print the print-friendly version\n   - Save mobile version to phone\n\n2. During Trip:\n   - Keep printed version with travel documents\n   - Use mobile version for daily reference\n   - Access detailed version when needed for specific information\n\n3. Emergency Access:\n   - Mobile version has quick-access emergency information\n   - Keep printed version as backup\n   - All emergency numbers and contacts in both versions\n\nNote: All versions contain the same core information but are formatted differently for optimal use in different situations.\n\nIMPORTANT DATES:\n- Trip Duration: April 15-23, 2024\n- Proposal Day: April 19, 2024\n- Key Reservation Deadlines:\n  * Flights: Book by January 2024\n  * Hotels: Book by February 2024\n  * Restaurant Reservations: Book by January 2024\n  * JR Pass: Purchase by March 2024\n"
  },
  {
    "path": "examples/use_case/japan-travel-plan/japan_travel_handbook.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>Japan Travel Handbook - April 15-23, 2024</title>\n    <style>\n        body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; }\n        .container { max-width: 1000px; margin: 0 auto; }\n        h1, h2, h3 { color: #333; }\n        .day-item { background: #f9f9f9; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .important-note { background: #ffe6e6; padding: 10px; border-radius: 5px; }\n        .phrase-table { width: 100%; border-collapse: collapse; }\n        .phrase-table td, .phrase-table th { border: 1px solid #ddd; padding: 8px; }\n        .proposal-spot { background: #e6ffe6; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .flight-info { background: #e6f3ff; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .checklist { background: #fff3e6; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .hotels { background: #e6e6ff; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .proposal-plan { background: #ffe6ff; padding: 15px; margin: 10px 0; border-radius: 5px; }\n        .checkbox-list li { list-style-type: none; margin-bottom: 8px; }\n        .checkbox-list li:before { content: \"☐ \"; }\n        .warning { color: #ff4444; }\n    </style>\n</head>\n<body>\n    <div class=\"container\">\n        [Previous content remains the same...]\n\n        <div class=\"proposal-plan\">\n            <h2>🌸 Proposal Planning Guide 🌸</h2>\n\n            <h3>Ring Security & Transport</h3>\n            <ul>\n                <li><strong>Carrying the Ring:</strong>\n                    <ul>\n                        <li>Always keep the ring in your carry-on luggage, never in checked bags</li>\n                        <li>Use a discrete, non-branded box or case</li>\n                        <li>Consider travel insurance that covers jewelry</li>\n                        <li>Keep receipt/appraisal documentation separate from the ring</li>\n                    </ul>\n                </li>\n                <li><strong>Airport Security Tips:</strong>\n                    <ul>\n                        <li>No need to declare the ring unless value exceeds ¥1,000,000 (~$6,700)</li>\n                        <li>If asked, simply state it's \"personal jewelry\"</li>\n                        <li>Consider requesting private screening to maintain surprise</li>\n                        <li>Keep ring in original box until through security, then transfer to more discrete case</li>\n                    </ul>\n                </li>\n            </ul>\n\n            <h3>Proposal Location Details - Maruyama Park</h3>\n            <ul>\n                <li><strong>Best Timing:</strong>\n                    <ul>\n                        <li>Date: April 19 (Day 5)</li>\n                        <li>Time: 5:30 PM (30 minutes before sunset)</li>\n                        <li>Park closes at 8:00 PM in April</li>\n                    </ul>\n                </li>\n                <li><strong>Specific Spot Recommendations:</strong>\n                    <ul>\n                        <li>Primary Location: Near the famous weeping cherry tree\n                            <br>- Less crowded in early evening\n                            <br>- Beautiful illumination starts at dusk\n                            <br>- Iconic Kyoto backdrop\n                        </li>\n                        <li>Backup Location: Gion Shirakawa area\n                            <br>- Atmospheric stone-paved street\n                            <br>- Traditional buildings and cherry trees\n                            <br>- Beautiful in light rain\n                        </li>\n                    </ul>\n                </li>\n            </ul>\n\n            <h3>Proposal Day Planning</h3>\n            <ul>\n                <li><strong>Morning Preparation:</strong>\n                    <ul>\n                        <li>Confirm weather forecast</li>\n                        <li>Transfer ring to secure pocket/bag</li>\n                        <li>Have backup indoor location details ready</li>\n                    </ul>\n                </li>\n                <li><strong>Suggested Timeline:</strong>\n                    <ul>\n                        <li>4:00 PM: Start heading to Maruyama Park area</li>\n                        <li>4:30 PM: Light refreshments at nearby tea house</li>\n                        <li>5:15 PM: Begin walk through park</li>\n                        <li>5:30 PM: Arrive at proposal spot</li>\n                        <li>6:00 PM: Sunset and illumination begins</li>\n                        <li>7:00 PM: Celebratory dinner reservation</li>\n                    </ul>\n                </li>\n            </ul>\n\n            <h3>Celebration Dinner Options</h3>\n            <ul>\n                <li><strong>Traditional Japanese:</strong> Kikunoi Roan\n                    <br>- Intimate 2-star Michelin restaurant\n                    <br>- Advance reservation required (3 months)\n                    <br>- Price: ¥15,000-20,000 per person\n                </li>\n                <li><strong>Modern Fusion:</strong> The Sodoh\n                    <br>- Beautiful garden views\n                    <br>- Western-style seating available\n                    <br>- Price: ¥12,000-15,000 per person\n                </li>\n            </ul>\n\n            <div class=\"warning\">\n                <h3>Important Notes:</h3>\n                <ul>\n                    <li>Keep proposal plans in separate notes from shared itinerary</li>\n                    <li>Have a backup plan in case of rain (indoor locations listed above)</li>\n                    <li>Consider hiring a local photographer to capture the moment</li>\n                    <li>Save restaurant staff contact info in case of timing changes</li>\n                </ul>\n            </div>\n        </div>\n    </div>\n</body>\n</html>\n"
  },
  {
    "path": "examples/use_case/japan-travel-plan/japan_travel_handbook_mobile.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n    <title>Japan Travel Guide (Mobile)</title>\n    <style>\n        * { box-sizing: border-box; }\n\n        body {\n            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;\n            margin: 0;\n            padding: 10px;\n            line-height: 1.6;\n            font-size: 16px;\n        }\n        .container {\n            max-width: 100%;\n            margin: 0 auto;\n        }\n        h1 { font-size: 1.5em; margin: 10px 0; }\n        h2 { font-size: 1.3em; margin: 8px 0; }\n        h3 { font-size: 1.1em; margin: 6px 0; }\n\n        /* Mobile-friendly cards */\n        .card {\n            background: #fff;\n            border-radius: 10px;\n            box-shadow: 0 2px 5px rgba(0,0,0,0.1);\n            margin: 10px 0;\n            padding: 15px;\n        }\n\n        /* Collapsible sections */\n        .collapsible {\n            background: #f8f9fa;\n            border: none;\n            border-radius: 8px;\n            width: 100%;\n            padding: 15px;\n            text-align: left;\n            font-size: 1.1em;\n            font-weight: bold;\n            cursor: pointer;\n            margin: 5px 0;\n        }\n\n        .content {\n            display: none;\n            padding: 10px;\n        }\n\n        .active {\n            background: #e9ecef;\n        }\n\n        /* Mobile-friendly tables */\n        .table-wrapper {\n            overflow-x: auto;\n            margin: 10px 0;\n        }\n        table {\n            width: 100%;\n            border-collapse: collapse;\n            min-width: 300px;\n        }\n        th, td {\n            padding: 10px;\n            border: 1px solid #ddd;\n            text-align: left;\n        }\n        th {\n            background: #f8f9fa;\n        }\n\n        /* Touch-friendly lists */\n        ul, ol {\n            padding-left: 20px;\n            margin: 10px 0;\n        }\n        li {\n            margin: 8px 0;\n            padding: 5px 0;\n        }\n\n        /* Emergency info styling */\n        .emergency {\n            background: #ffe6e6;\n            border-left: 4px solid #ff4444;\n            padding: 10px;\n            margin: 10px 0;\n        }\n\n        /* Quick access buttons */\n        .quick-access {\n            display: flex;\n            flex-wrap: wrap;\n            gap: 10px;\n            margin: 10px 0;\n        }\n        .quick-btn {\n            background: #007bff;\n            color: white;\n            border: none;\n            border-radius: 20px;\n            padding: 10px 20px;\n            font-size: 0.9em;\n            cursor: pointer;\n            flex: 1 1 auto;\n            text-align: center;\n            min-width: 120px;\n        }\n\n        /* Dark mode support */\n        @media (prefers-color-scheme: dark) {\n            body {\n                background: #1a1a1a;\n                color: #fff;\n            }\n            .card {\n                background: #2d2d2d;\n            }\n            .collapsible {\n                background: #333;\n                color: #fff;\n            }\n            .active {\n                background: #404040;\n            }\n            th {\n                background: #333;\n            }\n            td, th {\n                border-color: #404040;\n            }\n        }\n    </style>\n</head>\n<body>\n    <div class=\"container\">\n        <h1>Japan Travel Guide</h1>\n        <p><strong>April 15-23, 2024</strong></p>\n\n        <div class=\"quick-access\">\n            <button class=\"quick-btn\" onclick=\"showSection('emergency')\">Emergency</button>\n            <button class=\"quick-btn\" onclick=\"showSection('phrases')\">Phrases</button>\n            <button class=\"quick-btn\" onclick=\"showSection('transport')\">Transport</button>\n            <button class=\"quick-btn\" onclick=\"showSection('proposal')\">Proposal</button>\n        </div>\n\n        <div class=\"emergency card\" id=\"emergency\">\n            <h2>Emergency Contacts</h2>\n            <ul>\n                <li>🚑 Emergency: 119</li>\n                <li>👮 Police: 110</li>\n                <li>🏢 US Embassy: +81-3-3224-5000</li>\n                <li>ℹ️ Tourist Info: 03-3201-3331</li>\n            </ul>\n        </div>\n\n        <button class=\"collapsible\">📅 Daily Itinerary</button>\n        <div class=\"content\">\n            <div class=\"table-wrapper\">\n                <table>\n                    <tr><th>Date</th><th>Location</th><th>Activities</th></tr>\n                    <tr><td>Apr 15</td><td>Tokyo</td><td>Arrival, Shinjuku</td></tr>\n                    <tr><td>Apr 16</td><td>Tokyo</td><td>Meiji, Harajuku, Senso-ji</td></tr>\n                    <tr><td>Apr 17</td><td>Tokyo</td><td>Tea Ceremony, Budokan</td></tr>\n                    <tr><td>Apr 18</td><td>Kyoto</td><td>Travel, Kinkaku-ji</td></tr>\n                    <tr><td>Apr 19</td><td>Kyoto</td><td>Fushimi Inari, Proposal</td></tr>\n                    <tr><td>Apr 20</td><td>Nara</td><td>Deer Park, Temples</td></tr>\n                    <tr><td>Apr 21</td><td>Tokyo</td><td>Return, Bay Cruise</td></tr>\n                </table>\n            </div>\n        </div>\n\n        <button class=\"collapsible\">🗣️ Essential Phrases</button>\n        <div class=\"content\">\n            <div class=\"table-wrapper\">\n                <table>\n                    <tr><th>English</th><th>Japanese</th></tr>\n                    <tr><td>Thank you</td><td>ありがとう</td></tr>\n                    <tr><td>Excuse me</td><td>すみません</td></tr>\n                    <tr><td>Please</td><td>お願いします</td></tr>\n                    <tr><td>Where is...</td><td>...はどこですか</td></tr>\n                    <tr><td>Help!</td><td>助けて!</td></tr>\n                </table>\n            </div>\n        </div>\n\n        <button class=\"collapsible\">🚅 Transportation</button>\n        <div class=\"content\">\n            <div class=\"card\">\n                <h3>Key Routes</h3>\n                <ul>\n                    <li>Tokyo-Kyoto: 2h15m</li>\n                    <li>Kyoto-Nara: 45m</li>\n                    <li>Last trains: ~midnight</li>\n                </ul>\n                <p><strong>JR Pass:</strong> Activate April 15</p>\n            </div>\n        </div>\n\n        <button class=\"collapsible\">💍 Proposal Plan</button>\n        <div class=\"content\">\n            <div class=\"card\">\n                <h3>April 19 Timeline</h3>\n                <ul>\n                    <li>4:00 PM: Head to Maruyama Park</li>\n                    <li>5:30 PM: Arrive at spot</li>\n                    <li>7:00 PM: Dinner at Kikunoi Roan</li>\n                </ul>\n                <p><strong>Backup:</strong> Gion Shirakawa area</p>\n            </div>\n        </div>\n\n        <button class=\"collapsible\">💰 Budget Tracker</button>\n        <div class=\"content\">\n            <div class=\"table-wrapper\">\n                <table>\n                    <tr><th>Item</th><th>Budget</th></tr>\n                    <tr><td>Hotels</td><td>$1500-2000</td></tr>\n                    <tr><td>Transport</td><td>$600-800</td></tr>\n                    <tr><td>Food</td><td>$800-1000</td></tr>\n                    <tr><td>Activities</td><td>$600-800</td></tr>\n                    <tr><td>Shopping</td><td>$500-400</td></tr>\n                </table>\n            </div>\n        </div>\n    </div>\n\n    <script>\n        // Add click handlers for collapsible sections\n        var coll = document.getElementsByClassName(\"collapsible\");\n        for (var i = 0; i < coll.length; i++) {\n            coll[i].addEventListener(\"click\", function() {\n                this.classList.toggle(\"active\");\n                var content = this.nextElementSibling;\n                if (content.style.display === \"block\") {\n                    content.style.display = \"none\";\n                } else {\n                    content.style.display = \"block\";\n                }\n            });\n        }\n\n        // Function to show specific section\n        function showSection(id) {\n            document.getElementById(id).scrollIntoView({\n                behavior: 'smooth'\n            });\n        }\n    </script>\n</body>\n</html>\n"
  },
  {
    "path": "examples/use_case/japan-travel-plan/japan_travel_handbook_print.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <title>Japan Travel Handbook (Print Version) - April 15-23, 2024</title>\n    <style>\n        @media print {\n            body {\n                font-family: Arial, sans-serif;\n                font-size: 11pt;\n                line-height: 1.4;\n                margin: 0.5in;\n            }\n            h1 { font-size: 16pt; }\n            h2 { font-size: 14pt; }\n            h3 { font-size: 12pt; }\n\n            .section {\n                margin: 10px 0;\n                padding: 5px;\n                border: 1px solid #ccc;\n                page-break-inside: avoid;\n            }\n            .no-break {\n                page-break-inside: avoid;\n            }\n\n            table {\n                border-collapse: collapse;\n                width: 100%;\n                margin: 10px 0;\n            }\n            td, th {\n                border: 1px solid #000;\n                padding: 4px;\n                font-size: 10pt;\n            }\n            ul, ol {\n                margin: 5px 0;\n                padding-left: 20px;\n            }\n            li {\n                margin: 3px 0;\n            }\n            .page-break {\n                page-break-before: always;\n            }\n        }\n        /* Screen styles */\n        body {\n            font-family: Arial, sans-serif;\n            line-height: 1.4;\n            margin: 20px;\n            max-width: 800px;\n            margin: 0 auto;\n        }\n\n        .section {\n            margin: 15px 0;\n            padding: 15px;\n            border: 1px solid #ccc;\n            border-radius: 5px;\n        }\n\n        table {\n            border-collapse: collapse;\n            width: 100%;\n            margin: 10px 0;\n        }\n        td, th {\n            border: 1px solid #000;\n            padding: 8px;\n        }\n        @media screen {\n            .page-break {\n                margin: 30px 0;\n                border-top: 2px dashed #ccc;\n            }\n        }\n    </style>\n</head>\n<body>\n    <h1>Japan Travel Handbook (Print Version)</h1>\n    <p><strong>Trip Dates:</strong> April 15-23, 2024</p>\n\n    <div class=\"section\">\n        <h2>Emergency Contacts & Important Information</h2>\n        <ul>\n            <li>Emergency in Japan: 119 (Ambulance/Fire) / 110 (Police)</li>\n            <li>US Embassy Tokyo: +81-3-3224-5000</li>\n            <li>Tourist Information Hotline: 03-3201-3331</li>\n            <li>Your Travel Insurance: [Write number here]</li>\n        </ul>\n    </div>\n\n    <div class=\"section\">\n        <h2>Daily Itinerary Summary</h2>\n        <table>\n            <tr><th>Date</th><th>Location</th><th>Key Activities</th></tr>\n            <tr><td>Apr 15</td><td>Tokyo</td><td>Arrival, Shinjuku area exploration</td></tr>\n            <tr><td>Apr 16</td><td>Tokyo</td><td>Meiji Shrine, Harajuku, Senso-ji, Skytree</td></tr>\n            <tr><td>Apr 17</td><td>Tokyo</td><td>Tea Ceremony, Budokan, Yanaka Ginza</td></tr>\n            <tr><td>Apr 18</td><td>Kyoto</td><td>Travel to Kyoto, Kinkaku-ji, Gion</td></tr>\n            <tr><td>Apr 19</td><td>Kyoto</td><td>Fushimi Inari, Arashiyama, Evening Proposal</td></tr>\n            <tr><td>Apr 20</td><td>Nara/Kyoto</td><td>Nara Park day trip, deer feeding</td></tr>\n            <tr><td>Apr 21</td><td>Tokyo</td><td>Return to Tokyo, bay cruise</td></tr>\n        </table>\n    </div>\n\n    <div class=\"page-break\"></div>\n\n    <div class=\"section\">\n        <h2>Essential Japanese Phrases</h2>\n        <table>\n            <tr><th>English</th><th>Japanese</th><th>When to Use</th></tr>\n            <tr><td>Arigatou gozaimasu</td><td>ありがとうございます</td><td>Thank you (formal)</td></tr>\n            <tr><td>Sumimasen</td><td>すみません</td><td>Excuse me/Sorry</td></tr>\n            <tr><td>Onegaishimasu</td><td>お願いします</td><td>Please</td></tr>\n            <tr><td>Toire wa doko desu ka?</td><td>トイレはどこですか？</td><td>Where is the bathroom?</td></tr>\n            <tr><td>Eigo ga hanasemasu ka?</td><td>英語が話せますか？</td><td>Do you speak English?</td></tr>\n        </table>\n    </div>\n\n    <div class=\"section\">\n        <h2>Transportation Notes</h2>\n        <ul>\n            <li>JR Pass: Activate on April 15</li>\n            <li>Tokyo-Kyoto Shinkansen: ~2h15m</li>\n            <li>Kyoto-Nara Local Train: ~45m</li>\n            <li>Last trains: Usually around midnight</li>\n            <li>Keep ¥3000 for unexpected taxi rides</li>\n        </ul>\n    </div>\n\n    <div class=\"page-break\"></div>\n\n    <div class=\"section no-break\">\n        <h2>Proposal Day Timeline (April 19)</h2>\n        <table>\n            <tr><th>Time</th><th>Activity</th><th>Notes</th></tr>\n            <tr><td>4:00 PM</td><td>Head to Maruyama Park</td><td>Check weather first</td></tr>\n            <tr><td>4:30 PM</td><td>Tea house visit</td><td>Light refreshments</td></tr>\n            <tr><td>5:15 PM</td><td>Park walk begins</td><td>Head to weeping cherry tree</td></tr>\n            <tr><td>5:30 PM</td><td>Arrive at spot</td><td>Find quiet area</td></tr>\n            <tr><td>7:00 PM</td><td>Dinner reservation</td><td>Kikunoi Roan</td></tr>\n        </table>\n        <p><strong>Backup Location:</strong> Gion Shirakawa area (in case of rain)</p>\n    </div>\n\n    <div class=\"section\">\n        <h2>Quick Reference Budget</h2>\n        <table>\n            <tr><th>Item</th><th>Budget (USD)</th><th>Notes</th></tr>\n            <tr><td>Hotels</td><td>1500-2000</td><td>Pre-booked</td></tr>\n            <tr><td>Transport</td><td>600-800</td><td>Including JR Pass</td></tr>\n            <tr><td>Food</td><td>800-1000</td><td>~$60/person/day</td></tr>\n            <tr><td>Activities</td><td>600-800</td><td>Including tea ceremony</td></tr>\n            <tr><td>Shopping</td><td>500-400</td><td>Souvenirs/gifts</td></tr>\n        </table>\n    </div>\n</body>\n</html>\n"
  },
  {
    "path": "examples/use_case/readme.md",
    "content": "# Examples\n\nWe put some examples in the `examples` directory. All the examples use the same prompt\nas [Manus](https://manus.im/?utm_source=ai-bot.cn).\n\nThe Model we use is `claude3.5`.\n\n## Japan Travel Plan\n**Prompt**：\n```\nI need a 7-day Japan itinerary for April 15-23 from Seattle, with a $2500-5000 budget for my fiancée and me. We love historical sites, hidden gems, and Japanese culture (kendo, tea ceremonies, Zen meditation). We want to see Nara's deer and explore cities on foot. I plan to propose during this trip and need a special location recommendation. Please provide a detailed itinerary and a simple HTML travel handbook with maps, attraction descriptions, essential Japanese phrases, and travel tips we can reference throughout our journey.\n```\n**preview**：\n![alt text](picturesapan-travel-plan-1.png)\n\n![alt text](picturesapan-travel-plan-2.png)\n"
  },
  {
    "path": "main.py",
    "content": "import argparse\nimport asyncio\n\nfrom app.agent.manus import Manus\nfrom app.logger import logger\n\n\nasync def main():\n    # Parse command line arguments\n    parser = argparse.ArgumentParser(description=\"Run Manus agent with a prompt\")\n    parser.add_argument(\n        \"--prompt\", type=str, required=False, help=\"Input prompt for the agent\"\n    )\n    args = parser.parse_args()\n\n    # Create and initialize Manus agent\n    agent = await Manus.create()\n    try:\n        # Use command line prompt if provided, otherwise ask for input\n        prompt = args.prompt if args.prompt else input(\"Enter your prompt: \")\n        if not prompt.strip():\n            logger.warning(\"Empty prompt provided.\")\n            return\n\n        logger.warning(\"Processing your request...\")\n        await agent.run(prompt)\n        logger.info(\"Request processing completed.\")\n    except KeyboardInterrupt:\n        logger.warning(\"Operation interrupted.\")\n    finally:\n        # Ensure agent resources are cleaned up before exiting\n        await agent.cleanup()\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n"
  },
  {
    "path": "protocol/a2a/__init__.py",
    "content": ""
  },
  {
    "path": "protocol/a2a/app/README.md",
    "content": "# Manus Agent with A2A Protocol\n\nThis is an experimental integration of the A2A protocol (https://google.github.io/A2A/#/documentation) with OpenManus, currently supporting only non-streaming mode.\n\n## Prerequisites\n- conda activate 'Your OpenManus python env'\n- pip install a2a-sdk==0.2.5\n\n\n\n## Setup & Running\n\n1. Run A2A Server:\n\n   ```bash\n   cd OpenManus\n   python -m protocol.a2a.app.main\n   ```\n\n2. Clone A2A official repository and run A2A Client,there are two ways to use A2AClient——CLI and  Register A2A Agent Server in UI.(details at https://github.com/google/A2A):\n\n   ```bash\n   git clone https://github.com/google-a2a/a2a-samples.git\n   cd a2a-samples\n   echo \"GOOGLE_API_KEY=your_api_key_here\" > .env\n   cd samples/python/hosts/cli\n   uv run .\n   ```\n\n3. Send tasks to OpenManus via A2A Client CLI or Register A2A Agent Server in UI\n\n\n## Examples\n\n**Get Agent Card**\n\nRequest:\n\n```\ncurl http://localhost:10000/.well-known/agent.json\n\n```\n\n\n```\nResponse:\n\n{\n    \"capabilities\": {\n        \"pushNotifications\": true,\n        \"streaming\": false\n    },\n    \"defaultInputModes\": [\n        \"text\",\n        \"text/plain\"\n    ],\n    \"defaultOutputModes\": [\n        \"text\",\n        \"text/plain\"\n    ],\n    \"description\": \"A versatile agent that can solve various tasks using multiple tools including MCP-based tools\",\n    \"name\": \"Manus Agent\",\n    \"skills\": [\n        {\n            \"description\": \"Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results.\",\n            \"examples\": [\n                \"Execute Python code:'''python \\n Print('Hello World') \\n '''\"\n            ],\n            \"id\": \"Python Execute\",\n            \"name\": \"Python Execute Tool\",\n            \"tags\": [\n                \"Execute Python Code\"\n            ]\n        },\n        {\n            \"description\": \"A powerful browser automation tool that allows interaction with web pages through various actions.\\n* This tool provides commands for controlling a browser session, navigating web pages, and extracting information\\n* It maintains state across calls, keeping the browser session alive until explicitly closed\\n* Use this when you need to browse websites, fill forms, click buttons, extract content, or perform web searches\\n* Each action requires specific parameters as defined in the tool's dependencies\\n\\nKey capabilities include:\\n* Navigation: Go to specific URLs, go back, search the web, or refresh pages\\n* Interaction: Click elements, input text, select from dropdowns, send keyboard commands\\n* Scrolling: Scroll up/down by pixel amount or scroll to specific text\\n* Content extraction: Extract and analyze content from web pages based on specific goals\\n* Tab management: Switch between tabs, open new tabs, or close tabs\\n\\nNote: When using element indices, refer to the numbered elements shown in the current browser state.\\n\",\n            \"examples\": [\n                \"go_to 'https://www.google.com'\"\n            ],\n            \"id\": \"Browser use\",\n            \"name\": \"Browser use Tool\",\n            \"tags\": [\n                \"Use Browser\"\n            ]\n        },\n        {\n            \"description\": \"Custom editing tool for viewing, creating and editing files\\n* State is persistent across command calls and discussions with the user\\n* 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\\n* The `create` command cannot be used if the specified `path` already exists as a file\\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`\\n* The `undo_edit` command will revert the last edit made to the file at `path`\\n\\nNotes for using the `str_replace` command:\\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\\n* 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\\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`\\n\",\n            \"examples\": [\n                \"Replace 'old' with 'new' in 'file.txt'\"\n            ],\n            \"id\": \"Replace String\",\n            \"name\": \"Str_replace Tool\",\n            \"tags\": [\n                \"Operate Files\"\n            ]\n        },\n        {\n            \"description\": \"Use this tool to ask human for help.\",\n            \"examples\": [\n                \"Ask human: 'What time is it?'\"\n            ],\n            \"id\": \"Ask human\",\n            \"name\": \"Ask human Tool\",\n            \"tags\": [\n                \"Ask human for help\"\n            ]\n        },\n        {\n            \"description\": \"Terminate the interaction when the request is met OR if the assistant cannot proceed further with the task.\\nWhen you have finished all the tasks, call this tool to end the work.\",\n            \"examples\": [\n                \"terminate\"\n            ],\n            \"id\": \"terminate\",\n            \"name\": \"terminate Tool\",\n            \"tags\": [\n                \"terminate task\"\n            ]\n        }\n    ],\n    \"url\": \"http://localhost:10000/\",\n    \"version\": \"1.0.0\"\n}\n```\n\n**Send Task**\n\nRequest:\n\n```\ncurl --location 'http://localhost:10000' \\\n--header 'Content-Type: application/json' \\\n--data '{\n    \"id\":130,\n    \"jsonrpc\":\"2.0\",\n    \"method\": \"message/send\",\n    \"params\": {\n        \"message\": {\n            \"messageId\": \"\",\n            \"role\": \"user\",\n            \"parts\": [{\"text\":\"什么是快乐星球\"}]\n        }\n    }\n}'\n```\n\nResponse:\n\n```\n{\n    \"id\": 130,\n    \"jsonrpc\": \"2.0\",\n    \"result\": {\n        \"artifacts\": [\n            {\n                \"artifactId\": \"2f9d0af8-c7da-4f88-9c8c-3033836322b8\",\n                \"description\": \"\",\n                \"name\": \"task_cf64d3c9-1e08-4948-a620-76900aa204cf\",\n                \"parts\": [\n                    {\n                        \"kind\": \"text\",\n                        \"text\": \"Step 1: “快乐星球”是一个流行的网络用语，源自中国儿童科幻电视剧《快乐星球》。这部剧讲述了一群孩子在一个虚构的“快乐星球”上经历的冒险故事，主题围绕着友谊、成长和科学幻想。后来，“快乐星球”逐渐成为一种网络梗，用来形容一种无忧无虑、充满快乐的理想状态。\\n\\n如果你对这个词的具体含义、出处或者相关的文化背景有更多兴趣，可以告诉我，我可以为你提供更详细的信息！\\nStep 2: Observed output of cmd `terminate` executed:\\nThe interaction has been completed with status: success\"\n                    }\n                ]\n            }\n        ],\n        \"contextId\": \"44d16c16-9ccf-49c2-9a99-5c9513969b5f\",\n        \"history\": [\n            {\n                \"contextId\": \"44d16c16-9ccf-49c2-9a99-5c9513969b5f\",\n                \"kind\": \"message\",\n                \"messageId\": \"\",\n                \"parts\": [\n                    {\n                        \"kind\": \"text\",\n                        \"text\": \"什么是快乐星球\"\n                    }\n                ],\n                \"role\": \"user\",\n                \"taskId\": \"cf64d3c9-1e08-4948-a620-76900aa204cf\"\n            }\n        ],\n        \"id\": \"cf64d3c9-1e08-4948-a620-76900aa204cf\",\n        \"kind\": \"task\",\n        \"status\": {\n            \"state\": \"completed\"\n        }\n    }\n}\n```\n\n\n## Learn More\n\n- [A2A Protocol Documentation](https://google.github.io/A2A/#/documentation)\n"
  },
  {
    "path": "protocol/a2a/app/README_zh.md",
    "content": "# Manus Agent with A2A Protocol\n\n这是一个将A2A协议(https://google.github.io/A2A/#/documentation)与OpenManus结合的一个尝试,当前仅支持非流式\n\n## Prerequisites\n- conda activate 'Your OpenManus python env'\n- pip install a2a-sdk==0.2.5\n\n\n\n## Setup & Running\n\n1. 运行A2A Server:\n\n   ```bash\n   cd OpenManus\n   python -m protocol.a2a.app.main\n   ```\n\n2. 拉取A2A官方库并运行A2A Client，有两种使用A2A客户端的方式——CLI以及在前端页面注册Agent服务。（详情参考https://github.com/google/A2A）:\n\n   ```bash\n   git clone https://github.com/google-a2a/a2a-samples.git\n   cd a2a-samples\n   echo \"GOOGLE_API_KEY=your_api_key_here\" > .env\n   cd samples/python/hosts/cli\n   uv run .\n   ```\n\n3. 通过A2A Client的命令行向OpenManus发送任务或者在A2A前端页面上将其注册\n\n\n## Examples\n\n**获得Agent Card**\n\nRequest:\n\n```\ncurl http://localhost:10000/.well-known/agent.json\n\n```\n\n\n```\nResponse:\n\n{\n    \"capabilities\": {\n        \"pushNotifications\": true,\n        \"streaming\": false\n    },\n    \"defaultInputModes\": [\n        \"text\",\n        \"text/plain\"\n    ],\n    \"defaultOutputModes\": [\n        \"text\",\n        \"text/plain\"\n    ],\n    \"description\": \"A versatile agent that can solve various tasks using multiple tools including MCP-based tools\",\n    \"name\": \"Manus Agent\",\n    \"skills\": [\n        {\n            \"description\": \"Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results.\",\n            \"examples\": [\n                \"Execute Python code:'''python \\n Print('Hello World') \\n '''\"\n            ],\n            \"id\": \"Python Execute\",\n            \"name\": \"Python Execute Tool\",\n            \"tags\": [\n                \"Execute Python Code\"\n            ]\n        },\n        {\n            \"description\": \"A powerful browser automation tool that allows interaction with web pages through various actions.\\n* This tool provides commands for controlling a browser session, navigating web pages, and extracting information\\n* It maintains state across calls, keeping the browser session alive until explicitly closed\\n* Use this when you need to browse websites, fill forms, click buttons, extract content, or perform web searches\\n* Each action requires specific parameters as defined in the tool's dependencies\\n\\nKey capabilities include:\\n* Navigation: Go to specific URLs, go back, search the web, or refresh pages\\n* Interaction: Click elements, input text, select from dropdowns, send keyboard commands\\n* Scrolling: Scroll up/down by pixel amount or scroll to specific text\\n* Content extraction: Extract and analyze content from web pages based on specific goals\\n* Tab management: Switch between tabs, open new tabs, or close tabs\\n\\nNote: When using element indices, refer to the numbered elements shown in the current browser state.\\n\",\n            \"examples\": [\n                \"go_to 'https://www.google.com'\"\n            ],\n            \"id\": \"Browser use\",\n            \"name\": \"Browser use Tool\",\n            \"tags\": [\n                \"Use Browser\"\n            ]\n        },\n        {\n            \"description\": \"Custom editing tool for viewing, creating and editing files\\n* State is persistent across command calls and discussions with the user\\n* 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\\n* The `create` command cannot be used if the specified `path` already exists as a file\\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`\\n* The `undo_edit` command will revert the last edit made to the file at `path`\\n\\nNotes for using the `str_replace` command:\\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\\n* 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\\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`\\n\",\n            \"examples\": [\n                \"Replace 'old' with 'new' in 'file.txt'\"\n            ],\n            \"id\": \"Replace String\",\n            \"name\": \"Str_replace Tool\",\n            \"tags\": [\n                \"Operate Files\"\n            ]\n        },\n        {\n            \"description\": \"Use this tool to ask human for help.\",\n            \"examples\": [\n                \"Ask human: 'What time is it?'\"\n            ],\n            \"id\": \"Ask human\",\n            \"name\": \"Ask human Tool\",\n            \"tags\": [\n                \"Ask human for help\"\n            ]\n        },\n        {\n            \"description\": \"Terminate the interaction when the request is met OR if the assistant cannot proceed further with the task.\\nWhen you have finished all the tasks, call this tool to end the work.\",\n            \"examples\": [\n                \"terminate\"\n            ],\n            \"id\": \"terminate\",\n            \"name\": \"terminate Tool\",\n            \"tags\": [\n                \"terminate task\"\n            ]\n        }\n    ],\n    \"url\": \"http://localhost:10000/\",\n    \"version\": \"1.0.0\"\n}\n```\n\n**发送任务**\n\nRequest:\n\n```\ncurl --location 'http://localhost:10000' \\\n--header 'Content-Type: application/json' \\\n--data '{\n    \"id\":130,\n    \"jsonrpc\":\"2.0\",\n    \"method\": \"message/send\",\n    \"params\": {\n        \"message\": {\n            \"messageId\": \"\",\n            \"role\": \"user\",\n            \"parts\": [{\"text\":\"什么是快乐星球\"}]\n        }\n    }\n}'\n```\n\nResponse:\n\n```\n{\n    \"id\": 130,\n    \"jsonrpc\": \"2.0\",\n    \"result\": {\n        \"artifacts\": [\n            {\n                \"artifactId\": \"2f9d0af8-c7da-4f88-9c8c-3033836322b8\",\n                \"description\": \"\",\n                \"name\": \"task_cf64d3c9-1e08-4948-a620-76900aa204cf\",\n                \"parts\": [\n                    {\n                        \"kind\": \"text\",\n                        \"text\": \"Step 1: “快乐星球”是一个流行的网络用语，源自中国儿童科幻电视剧《快乐星球》。这部剧讲述了一群孩子在一个虚构的“快乐星球”上经历的冒险故事，主题围绕着友谊、成长和科学幻想。后来，“快乐星球”逐渐成为一种网络梗，用来形容一种无忧无虑、充满快乐的理想状态。\\n\\n如果你对这个词的具体含义、出处或者相关的文化背景有更多兴趣，可以告诉我，我可以为你提供更详细的信息！\\nStep 2: Observed output of cmd `terminate` executed:\\nThe interaction has been completed with status: success\"\n                    }\n                ]\n            }\n        ],\n        \"contextId\": \"44d16c16-9ccf-49c2-9a99-5c9513969b5f\",\n        \"history\": [\n            {\n                \"contextId\": \"44d16c16-9ccf-49c2-9a99-5c9513969b5f\",\n                \"kind\": \"message\",\n                \"messageId\": \"\",\n                \"parts\": [\n                    {\n                        \"kind\": \"text\",\n                        \"text\": \"什么是快乐星球\"\n                    }\n                ],\n                \"role\": \"user\",\n                \"taskId\": \"cf64d3c9-1e08-4948-a620-76900aa204cf\"\n            }\n        ],\n        \"id\": \"cf64d3c9-1e08-4948-a620-76900aa204cf\",\n        \"kind\": \"task\",\n        \"status\": {\n            \"state\": \"completed\"\n        }\n    }\n}\n```\n\n\n## Learn More\n\n- [A2A Protocol Documentation](https://google.github.io/A2A/#/documentation)\n"
  },
  {
    "path": "protocol/a2a/app/__init__.py",
    "content": ""
  },
  {
    "path": "protocol/a2a/app/agent.py",
    "content": "from typing import Any, AsyncIterable, ClassVar, Dict, List, Literal\n\nfrom pydantic import BaseModel\n\nfrom app.agent.manus import Manus\n\n\nclass ResponseFormat(BaseModel):\n    \"\"\"Respond to the user in this format.\"\"\"\n\n    status: Literal[\"input_required\", \"completed\", \"error\"] = \"input_required\"\n    message: str\n\n\nclass A2AManus(Manus):\n    async def invoke(self, query, sessionId) -> str:\n        config = {\"configurable\": {\"thread_id\": sessionId}}\n        response = await self.run(query)\n        return self.get_agent_response(config, response)\n\n    async def stream(self, query: str) -> AsyncIterable[Dict[str, Any]]:\n        \"\"\"Streaming is not supported by Manus.\"\"\"\n        raise NotImplementedError(\"Streaming is not supported by Manus yet.\")\n\n    def get_agent_response(self, config, agent_response):\n        return {\n            \"is_task_complete\": True,\n            \"require_user_input\": False,\n            \"content\": agent_response,\n        }\n\n    SUPPORTED_CONTENT_TYPES: ClassVar[List[str]] = [\"text\", \"text/plain\"]\n"
  },
  {
    "path": "protocol/a2a/app/agent_executor.py",
    "content": "import logging\nfrom typing import Awaitable, Callable\n\nfrom a2a.server.agent_execution import AgentExecutor, RequestContext\nfrom a2a.server.events import EventQueue\nfrom a2a.types import (\n    InvalidParamsError,\n    Part,\n    Task,\n    TextPart,\n    UnsupportedOperationError,\n)\nfrom a2a.utils import completed_task, new_artifact\nfrom a2a.utils.errors import ServerError\n\nfrom .agent import A2AManus\n\n\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n\nclass ManusExecutor(AgentExecutor):\n    \"\"\"Currency Conversion AgentExecutor Example.\"\"\"\n\n    def __init__(self, agent_factory: Callable[[], Awaitable[A2AManus]]):\n        self.agent_factory = agent_factory\n\n    async def execute(\n        self,\n        context: RequestContext,\n        event_queue: EventQueue,\n    ) -> None:\n        error = self._validate_request(context)\n        if error:\n            raise ServerError(error=InvalidParamsError())\n\n        query = context.get_user_input()\n        try:\n            self.agent = await self.agent_factory()\n            result = await self.agent.invoke(query, context.context_id)\n            print(f\"Final Result ===> {result}\")\n        except Exception as e:\n            print(\"Error invoking agent: %s\", e)\n            raise ServerError(error=ValueError(f\"Error invoking agent: {e}\")) from e\n        parts = [\n            Part(\n                root=TextPart(\n                    text=(\n                        result[\"content\"]\n                        if result[\"content\"]\n                        else \"failed to generate response\"\n                    )\n                ),\n            )\n        ]\n        event_queue.enqueue_event(\n            completed_task(\n                context.task_id,\n                context.context_id,\n                [new_artifact(parts, f\"task_{context.task_id}\")],\n                [context.message],\n            )\n        )\n\n    def _validate_request(self, context: RequestContext) -> bool:\n        return False\n\n    async def cancel(\n        self, request: RequestContext, event_queue: EventQueue\n    ) -> Task | None:\n        raise ServerError(error=UnsupportedOperationError())\n"
  },
  {
    "path": "protocol/a2a/app/main.py",
    "content": "import argparse\nimport asyncio\nimport logging\nfrom typing import Optional\n\nimport httpx\nfrom a2a.server.apps import A2AStarletteApplication\nfrom a2a.server.request_handlers import DefaultRequestHandler\nfrom a2a.server.tasks import InMemoryPushNotifier, InMemoryTaskStore\nfrom a2a.types import AgentCapabilities, AgentCard, AgentSkill\nfrom dotenv import load_dotenv\n\nfrom app.tool.browser_use_tool import _BROWSER_DESCRIPTION\nfrom app.tool.str_replace_editor import _STR_REPLACE_EDITOR_DESCRIPTION\nfrom app.tool.terminate import _TERMINATE_DESCRIPTION\n\nfrom .agent import A2AManus\nfrom .agent_executor import ManusExecutor\n\n\nload_dotenv()\n\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n\nasync def main(host: str = \"localhost\", port: int = 10000):\n    \"\"\"Starts the Manus Agent server.\"\"\"\n    try:\n        capabilities = AgentCapabilities(streaming=False, pushNotifications=True)\n        skills = [\n            AgentSkill(\n                id=\"Python Execute\",\n                name=\"Python Execute Tool\",\n                description=\"Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results.\",\n                tags=[\"Execute Python Code\"],\n                examples=[\n                    \"Execute Python code:'''python \\n Print('Hello World') \\n '''\"\n                ],\n            ),\n            AgentSkill(\n                id=\"Browser use\",\n                name=\"Browser use Tool\",\n                description=_BROWSER_DESCRIPTION,\n                tags=[\"Use Browser\"],\n                examples=[\"go_to 'https://www.google.com'\"],\n            ),\n            AgentSkill(\n                id=\"Replace String\",\n                name=\"Str_replace Tool\",\n                description=_STR_REPLACE_EDITOR_DESCRIPTION,\n                tags=[\"Operate Files\"],\n                examples=[\"Replace 'old' with 'new' in 'file.txt'\"],\n            ),\n            AgentSkill(\n                id=\"Ask human\",\n                name=\"Ask human Tool\",\n                description=\"Use this tool to ask human for help.\",\n                tags=[\"Ask human for help\"],\n                examples=[\"Ask human: 'What time is it?'\"],\n            ),\n            AgentSkill(\n                id=\"terminate\",\n                name=\"terminate Tool\",\n                description=_TERMINATE_DESCRIPTION,\n                tags=[\"terminate task\"],\n                examples=[\"terminate\"],\n            ),\n            # Add more skills as needed\n        ]\n\n        agent_card = AgentCard(\n            name=\"Manus Agent\",\n            description=\"A versatile agent that can solve various tasks using multiple tools including MCP-based tools\",\n            url=f\"http://{host}:{port}/\",\n            version=\"1.0.0\",\n            defaultInputModes=A2AManus.SUPPORTED_CONTENT_TYPES,\n            defaultOutputModes=A2AManus.SUPPORTED_CONTENT_TYPES,\n            capabilities=capabilities,\n            skills=skills,\n        )\n\n        httpx_client = httpx.AsyncClient()\n        request_handler = DefaultRequestHandler(\n            agent_executor=ManusExecutor(\n                agent_factory=lambda: A2AManus.create(max_steps=3)\n            ),\n            task_store=InMemoryTaskStore(),\n            push_notifier=InMemoryPushNotifier(httpx_client),\n        )\n\n        server = A2AStarletteApplication(\n            agent_card=agent_card, http_handler=request_handler\n        )\n\n        logger.info(f\"Starting server on {host}:{port}\")\n        return server.build()\n    except Exception as e:\n        logger.error(f\"An error occurred during server startup: {e}\")\n        exit(1)\n\n\ndef run_server(host: Optional[str] = \"localhost\", port: Optional[int] = 10000):\n    try:\n        import uvicorn\n\n        app = asyncio.run(main(host, port))\n        config = uvicorn.Config(\n            app=app, host=host, port=port, loop=\"asyncio\", proxy_headers=True\n        )\n        uvicorn.Server(config=config).run()\n        logger.info(f\"Server started on {host}:{port}\")\n    except Exception as e:\n        logger.error(f\"An error occurred while starting the server: {e}\")\n\n\nif __name__ == \"__main__\":\n    # Parse command line arguments for host and port, with default values\n    parser = argparse.ArgumentParser(description=\"Start Manus Agent service\")\n    parser.add_argument(\n        \"--host\",\n        type=str,\n        default=\"localhost\",\n        help=\"Server host address, default is localhost\",\n    )\n    parser.add_argument(\n        \"--port\", type=int, default=10000, help=\"Server port, default is 10000\"\n    )\n    args = parser.parse_args()\n    # Start the server with the specified or default host and port\n    run_server(args.host, args.port)\n"
  },
  {
    "path": "requirements.txt",
    "content": "pydantic~=2.10.6\nopenai~=1.66.3\ntenacity~=9.0.0\npyyaml~=6.0.2\nloguru~=0.7.3\nnumpy\ndatasets~=3.4.1\nfastapi~=0.115.11\ntiktoken~=0.9.0\n\nhtml2text~=2024.2.26\ngymnasium~=1.1.1\npillow~=11.1.0\nbrowsergym~=0.13.3\nuvicorn~=0.34.0\nunidiff~=0.7.5\nbrowser-use~=0.1.40\ngooglesearch-python~=1.3.0\nbaidusearch~=1.0.3\nduckduckgo_search~=7.5.3\n\naiofiles~=24.1.0\npydantic_core~=2.27.2\ncolorama~=0.4.6\nplaywright~=1.51.0\n\ndocker~=7.1.0\npytest~=8.3.5\npytest-asyncio~=0.25.3\n\nmcp~=1.5.0\nhttpx>=0.27.0\ntomli>=2.0.0\n\nboto3~=1.37.18\n\nrequests~=2.32.3\nbeautifulsoup4~=4.13.3\ncrawl4ai~=0.6.3\n\nhuggingface-hub~=0.29.2\nsetuptools~=75.8.0\n"
  },
  {
    "path": "run_flow.py",
    "content": "import asyncio\nimport time\n\nfrom app.agent.data_analysis import DataAnalysis\nfrom app.agent.manus import Manus\nfrom app.config import config\nfrom app.flow.flow_factory import FlowFactory, FlowType\nfrom app.logger import logger\n\n\nasync def run_flow():\n    agents = {\n        \"manus\": Manus(),\n    }\n    if config.run_flow_config.use_data_analysis_agent:\n        agents[\"data_analysis\"] = DataAnalysis()\n    try:\n        prompt = input(\"Enter your prompt: \")\n\n        if prompt.strip().isspace() or not prompt:\n            logger.warning(\"Empty prompt provided.\")\n            return\n\n        flow = FlowFactory.create_flow(\n            flow_type=FlowType.PLANNING,\n            agents=agents,\n        )\n        logger.warning(\"Processing your request...\")\n\n        try:\n            start_time = time.time()\n            result = await asyncio.wait_for(\n                flow.execute(prompt),\n                timeout=3600,  # 60 minute timeout for the entire execution\n            )\n            elapsed_time = time.time() - start_time\n            logger.info(f\"Request processed in {elapsed_time:.2f} seconds\")\n            logger.info(result)\n        except asyncio.TimeoutError:\n            logger.error(\"Request processing timed out after 1 hour\")\n            logger.info(\n                \"Operation terminated due to timeout. Please try a simpler request.\"\n            )\n\n    except KeyboardInterrupt:\n        logger.info(\"Operation cancelled by user.\")\n    except Exception as e:\n        logger.error(f\"Error: {str(e)}\")\n\n\nif __name__ == \"__main__\":\n    asyncio.run(run_flow())\n"
  },
  {
    "path": "run_mcp.py",
    "content": "#!/usr/bin/env python\nimport argparse\nimport asyncio\nimport sys\n\nfrom app.agent.mcp import MCPAgent\nfrom app.config import config\nfrom app.logger import logger\n\n\nclass MCPRunner:\n    \"\"\"Runner class for MCP Agent with proper path handling and configuration.\"\"\"\n\n    def __init__(self):\n        self.root_path = config.root_path\n        self.server_reference = config.mcp_config.server_reference\n        self.agent = MCPAgent()\n\n    async def initialize(\n        self,\n        connection_type: str,\n        server_url: str | None = None,\n    ) -> None:\n        \"\"\"Initialize the MCP agent with the appropriate connection.\"\"\"\n        logger.info(f\"Initializing MCPAgent with {connection_type} connection...\")\n\n        if connection_type == \"stdio\":\n            await self.agent.initialize(\n                connection_type=\"stdio\",\n                command=sys.executable,\n                args=[\"-m\", self.server_reference],\n            )\n        else:  # sse\n            await self.agent.initialize(connection_type=\"sse\", server_url=server_url)\n\n        logger.info(f\"Connected to MCP server via {connection_type}\")\n\n    async def run_interactive(self) -> None:\n        \"\"\"Run the agent in interactive mode.\"\"\"\n        print(\"\\nMCP Agent Interactive Mode (type 'exit' to quit)\\n\")\n        while True:\n            user_input = input(\"\\nEnter your request: \")\n            if user_input.lower() in [\"exit\", \"quit\", \"q\"]:\n                break\n            response = await self.agent.run(user_input)\n            print(f\"\\nAgent: {response}\")\n\n    async def run_single_prompt(self, prompt: str) -> None:\n        \"\"\"Run the agent with a single prompt.\"\"\"\n        await self.agent.run(prompt)\n\n    async def run_default(self) -> None:\n        \"\"\"Run the agent in default mode.\"\"\"\n        prompt = input(\"Enter your prompt: \")\n        if not prompt.strip():\n            logger.warning(\"Empty prompt provided.\")\n            return\n\n        logger.warning(\"Processing your request...\")\n        await self.agent.run(prompt)\n        logger.info(\"Request processing completed.\")\n\n    async def cleanup(self) -> None:\n        \"\"\"Clean up agent resources.\"\"\"\n        await self.agent.cleanup()\n        logger.info(\"Session ended\")\n\n\ndef parse_args() -> argparse.Namespace:\n    \"\"\"Parse command line arguments.\"\"\"\n    parser = argparse.ArgumentParser(description=\"Run the MCP Agent\")\n    parser.add_argument(\n        \"--connection\",\n        \"-c\",\n        choices=[\"stdio\", \"sse\"],\n        default=\"stdio\",\n        help=\"Connection type: stdio or sse\",\n    )\n    parser.add_argument(\n        \"--server-url\",\n        default=\"http://127.0.0.1:8000/sse\",\n        help=\"URL for SSE connection\",\n    )\n    parser.add_argument(\n        \"--interactive\", \"-i\", action=\"store_true\", help=\"Run in interactive mode\"\n    )\n    parser.add_argument(\"--prompt\", \"-p\", help=\"Single prompt to execute and exit\")\n    return parser.parse_args()\n\n\nasync def run_mcp() -> None:\n    \"\"\"Main entry point for the MCP runner.\"\"\"\n    args = parse_args()\n    runner = MCPRunner()\n\n    try:\n        await runner.initialize(args.connection, args.server_url)\n\n        if args.prompt:\n            await runner.run_single_prompt(args.prompt)\n        elif args.interactive:\n            await runner.run_interactive()\n        else:\n            await runner.run_default()\n\n    except KeyboardInterrupt:\n        logger.info(\"Program interrupted by user\")\n    except Exception as e:\n        logger.error(f\"Error running MCPAgent: {str(e)}\", exc_info=True)\n        sys.exit(1)\n    finally:\n        await runner.cleanup()\n\n\nif __name__ == \"__main__\":\n    asyncio.run(run_mcp())\n"
  },
  {
    "path": "run_mcp_server.py",
    "content": "# coding: utf-8\n# A shortcut to launch OpenManus MCP server, where its introduction also solves other import issues.\nfrom app.mcp.server import MCPServer, parse_args\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n\n    # Create and run server (maintaining original flow)\n    server = MCPServer()\n    server.run(transport=args.transport)\n"
  },
  {
    "path": "sandbox_main.py",
    "content": "import argparse\nimport asyncio\n\nfrom app.agent.sandbox_agent import SandboxManus\nfrom app.logger import logger\n\n\nasync def main():\n    # Parse command line arguments\n    parser = argparse.ArgumentParser(description=\"Run Manus agent with a prompt\")\n    parser.add_argument(\n        \"--prompt\", type=str, required=False, help=\"Input prompt for the agent\"\n    )\n    args = parser.parse_args()\n\n    # Create and initialize Manus agent\n    agent = await SandboxManus.create()\n    try:\n        # Use command line prompt if provided, otherwise ask for input\n        prompt = args.prompt if args.prompt else input(\"Enter your prompt: \")\n        if not prompt.strip():\n            logger.warning(\"Empty prompt provided.\")\n            return\n\n        logger.warning(\"Processing your request...\")\n        await agent.run(prompt)\n        logger.info(\"Request processing completed.\")\n    except KeyboardInterrupt:\n        logger.warning(\"Operation interrupted.\")\n    finally:\n        # Ensure agent resources are cleaned up before exiting\n        await agent.cleanup()\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n"
  },
  {
    "path": "setup.py",
    "content": "from setuptools import find_packages, setup\n\n\nwith open(\"README.md\", \"r\", encoding=\"utf-8\") as fh:\n    long_description = fh.read()\n\nsetup(\n    name=\"openmanus\",\n    version=\"0.1.0\",\n    author=\"mannaandpoem and OpenManus Team\",\n    author_email=\"mannaandpoem@gmail.com\",\n    description=\"A versatile agent that can solve various tasks using multiple tools\",\n    long_description=long_description,\n    long_description_content_type=\"text/markdown\",\n    url=\"https://github.com/FoundationAgents/OpenManus\",\n    packages=find_packages(),\n    install_requires=[\n        \"pydantic~=2.10.4\",\n        \"openai>=1.58.1,<1.67.0\",\n        \"tenacity~=9.0.0\",\n        \"pyyaml~=6.0.2\",\n        \"loguru~=0.7.3\",\n        \"numpy\",\n        \"datasets>=3.2,<3.5\",\n        \"html2text~=2024.2.26\",\n        \"gymnasium>=1.0,<1.2\",\n        \"pillow>=10.4,<11.2\",\n        \"browsergym~=0.13.3\",\n        \"uvicorn~=0.34.0\",\n        \"unidiff~=0.7.5\",\n        \"browser-use~=0.1.40\",\n        \"googlesearch-python~=1.3.0\",\n        \"aiofiles~=24.1.0\",\n        \"pydantic_core>=2.27.2,<2.28.0\",\n        \"colorama~=0.4.6\",\n    ],\n    classifiers=[\n        \"Programming Language :: Python :: 3\",\n        \"Programming Language :: Python :: 3.12\",\n        \"License :: OSI Approved :: MIT License\",\n        \"Operating System :: OS Independent\",\n    ],\n    python_requires=\">=3.12\",\n    entry_points={\n        \"console_scripts\": [\n            \"openmanus=main:main\",\n        ],\n    },\n)\n"
  },
  {
    "path": "tests/sandbox/test_client.py",
    "content": "import tempfile\nfrom pathlib import Path\nfrom typing import AsyncGenerator\n\nimport pytest\nimport pytest_asyncio\n\nfrom app.config import SandboxSettings\nfrom app.sandbox.client import LocalSandboxClient, create_sandbox_client\n\n\n@pytest_asyncio.fixture(scope=\"function\")\nasync def local_client() -> AsyncGenerator[LocalSandboxClient, None]:\n    \"\"\"Creates a local sandbox client for testing.\"\"\"\n    client = create_sandbox_client()\n    try:\n        yield client\n    finally:\n        await client.cleanup()\n\n\n@pytest.fixture(scope=\"function\")\ndef temp_dir() -> Path:\n    \"\"\"Creates a temporary directory for testing.\"\"\"\n    with tempfile.TemporaryDirectory() as tmp_dir:\n        yield Path(tmp_dir)\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_creation(local_client: LocalSandboxClient):\n    \"\"\"Tests sandbox creation with specific configuration.\"\"\"\n    config = SandboxSettings(\n        image=\"python:3.12-slim\",\n        work_dir=\"/workspace\",\n        memory_limit=\"512m\",\n        cpu_limit=0.5,\n    )\n\n    await local_client.create(config)\n    result = await local_client.run_command(\"python3 --version\")\n    assert \"Python 3.10\" in result\n\n\n@pytest.mark.asyncio\nasync def test_local_command_execution(local_client: LocalSandboxClient):\n    \"\"\"Tests command execution in local sandbox.\"\"\"\n    await local_client.create()\n\n    result = await local_client.run_command(\"echo 'test'\")\n    assert result.strip() == \"test\"\n\n    with pytest.raises(Exception):\n        await local_client.run_command(\"sleep 10\", timeout=1)\n\n\n@pytest.mark.asyncio\nasync def test_local_file_operations(local_client: LocalSandboxClient, temp_dir: Path):\n    \"\"\"Tests file operations in local sandbox.\"\"\"\n    await local_client.create()\n\n    # Test write and read operations\n    test_content = \"Hello, World!\"\n    await local_client.write_file(\"/workspace/test.txt\", test_content)\n    content = await local_client.read_file(\"/workspace/test.txt\")\n    assert content.strip() == test_content\n\n    # Test copying file to container\n    src_file = temp_dir / \"src.txt\"\n    src_file.write_text(\"Copy to container\")\n    await local_client.copy_to(str(src_file), \"/workspace/copied.txt\")\n    content = await local_client.read_file(\"/workspace/copied.txt\")\n    assert content.strip() == \"Copy to container\"\n\n    # Test copying file from container\n    dst_file = temp_dir / \"dst.txt\"\n    await local_client.copy_from(\"/workspace/test.txt\", str(dst_file))\n    assert dst_file.read_text().strip() == test_content\n\n\n@pytest.mark.asyncio\nasync def test_local_volume_binding(local_client: LocalSandboxClient, temp_dir: Path):\n    \"\"\"Tests volume binding in local sandbox.\"\"\"\n    bind_path = str(temp_dir)\n    volume_bindings = {bind_path: \"/data\"}\n\n    await local_client.create(volume_bindings=volume_bindings)\n\n    test_file = temp_dir / \"test.txt\"\n    test_file.write_text(\"Volume test\")\n\n    content = await local_client.read_file(\"/data/test.txt\")\n    assert \"Volume test\" in content\n\n\n@pytest.mark.asyncio\nasync def test_local_error_handling(local_client: LocalSandboxClient):\n    \"\"\"Tests error handling in local sandbox.\"\"\"\n    await local_client.create()\n\n    with pytest.raises(Exception) as exc:\n        await local_client.read_file(\"/nonexistent.txt\")\n    assert \"not found\" in str(exc.value).lower()\n\n    with pytest.raises(Exception) as exc:\n        await local_client.copy_from(\"/nonexistent.txt\", \"local.txt\")\n    assert \"not found\" in str(exc.value).lower()\n\n\nif __name__ == \"__main__\":\n    pytest.main([\"-v\", __file__])\n"
  },
  {
    "path": "tests/sandbox/test_docker_terminal.py",
    "content": "\"\"\"Tests for the AsyncDockerizedTerminal implementation.\"\"\"\n\nimport docker\nimport pytest\nimport pytest_asyncio\n\nfrom app.sandbox.core.terminal import AsyncDockerizedTerminal\n\n\n@pytest.fixture(scope=\"module\")\ndef docker_client():\n    \"\"\"Fixture providing a Docker client.\"\"\"\n    return docker.from_env()\n\n\n@pytest_asyncio.fixture(scope=\"module\")\nasync def docker_container(docker_client):\n    \"\"\"Fixture providing a test Docker container.\"\"\"\n    container = docker_client.containers.run(\n        \"python:3.12-slim\",\n        \"tail -f /dev/null\",\n        name=\"test_container\",\n        detach=True,\n        remove=True,\n    )\n    yield container\n    container.stop()\n\n\n@pytest_asyncio.fixture\nasync def terminal(docker_container):\n    \"\"\"Fixture providing an initialized AsyncDockerizedTerminal instance.\"\"\"\n    terminal = AsyncDockerizedTerminal(\n        docker_container,\n        working_dir=\"/workspace\",\n        env_vars={\"TEST_VAR\": \"test_value\"},\n        default_timeout=30,\n    )\n    await terminal.init()\n    yield terminal\n    await terminal.close()\n\n\nclass TestAsyncDockerizedTerminal:\n    \"\"\"Test cases for AsyncDockerizedTerminal.\"\"\"\n\n    @pytest.mark.asyncio\n    async def test_basic_command_execution(self, terminal):\n        \"\"\"Test basic command execution functionality.\"\"\"\n        result = await terminal.run_command(\"echo 'Hello World'\")\n        assert \"Hello World\" in result\n\n    @pytest.mark.asyncio\n    async def test_environment_variables(self, terminal):\n        \"\"\"Test environment variable setting and access.\"\"\"\n        result = await terminal.run_command(\"echo $TEST_VAR\")\n        assert \"test_value\" in result\n\n    @pytest.mark.asyncio\n    async def test_working_directory(self, terminal):\n        \"\"\"Test working directory setup.\"\"\"\n        result = await terminal.run_command(\"pwd\")\n        assert \"/workspace\" == result\n\n    @pytest.mark.asyncio\n    async def test_command_timeout(self, docker_container):\n        \"\"\"Test command timeout functionality.\"\"\"\n        terminal = AsyncDockerizedTerminal(docker_container, default_timeout=1)\n        await terminal.init()\n        try:\n            with pytest.raises(TimeoutError):\n                await terminal.run_command(\"sleep 5\")\n        finally:\n            await terminal.close()\n\n    @pytest.mark.asyncio\n    async def test_multiple_commands(self, terminal):\n        \"\"\"Test execution of multiple commands in sequence.\"\"\"\n        cmd1 = await terminal.run_command(\"echo 'First'\")\n        cmd2 = await terminal.run_command(\"echo 'Second'\")\n        assert \"First\" in cmd1\n        assert \"Second\" in cmd2\n\n    @pytest.mark.asyncio\n    async def test_session_cleanup(self, docker_container):\n        \"\"\"Test proper cleanup of resources.\"\"\"\n        terminal = AsyncDockerizedTerminal(docker_container)\n        await terminal.init()\n        assert terminal.session is not None\n        await terminal.close()\n        # Verify session is properly cleaned up\n        # Note: session object still exists, but internal connection is closed\n        assert terminal.session is not None\n\n\n# Configure pytest-asyncio\ndef pytest_configure(config):\n    \"\"\"Configure pytest-asyncio.\"\"\"\n    config.addinivalue_line(\"asyncio_mode\", \"strict\")\n    config.addinivalue_line(\"asyncio_default_fixture_loop_scope\", \"function\")\n\n\nif __name__ == \"__main__\":\n    pytest.main([\"-v\", __file__])\n"
  },
  {
    "path": "tests/sandbox/test_sandbox.py",
    "content": "import pytest\nimport pytest_asyncio\n\nfrom app.sandbox.core.sandbox import DockerSandbox, SandboxSettings\n\n\n@pytest.fixture(scope=\"module\")\ndef sandbox_config():\n    \"\"\"Creates sandbox configuration for testing.\"\"\"\n    return SandboxSettings(\n        image=\"python:3.12-slim\",\n        work_dir=\"/workspace\",\n        memory_limit=\"1g\",\n        cpu_limit=0.5,\n        network_enabled=True,\n    )\n\n\n@pytest_asyncio.fixture(scope=\"module\")\nasync def sandbox(sandbox_config):\n    \"\"\"Creates and manages a test sandbox instance.\"\"\"\n    sandbox = DockerSandbox(sandbox_config)\n    await sandbox.create()\n    try:\n        yield sandbox\n    finally:\n        await sandbox.cleanup()\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_working_directory(sandbox):\n    \"\"\"Tests sandbox working directory configuration.\"\"\"\n    result = await sandbox.terminal.run_command(\"pwd\")\n    assert result.strip() == \"/workspace\"\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_file_operations(sandbox):\n    \"\"\"Tests sandbox file read/write operations.\"\"\"\n    # Test file writing\n    test_content = \"Hello from sandbox!\"\n    await sandbox.write_file(\"/workspace/test.txt\", test_content)\n\n    # Test file reading\n    content = await sandbox.read_file(\"/workspace/test.txt\")\n    assert content.strip() == test_content\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_python_execution(sandbox):\n    \"\"\"Tests Python code execution in sandbox.\"\"\"\n    # Write test file\n    await sandbox.write_file(\"/workspace/test.txt\", \"Hello from file!\")\n\n    # Write Python script\n    python_code = \"\"\"\nprint(\"Hello from Python!\")\nwith open('/workspace/test.txt') as f:\n    print(f.read())\n\"\"\"\n    await sandbox.write_file(\"/workspace/test.py\", python_code)\n\n    # Execute script and verify output\n    result = await sandbox.terminal.run_command(\"python3 /workspace/test.py\")\n    assert \"Hello from Python!\" in result\n    assert \"Hello from file!\" in result\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_file_persistence(sandbox):\n    \"\"\"Tests file persistence in sandbox.\"\"\"\n    # Create multiple files\n    files = {\n        \"file1.txt\": \"Content 1\",\n        \"file2.txt\": \"Content 2\",\n        \"nested/file3.txt\": \"Content 3\",\n    }\n\n    # Write files\n    for path, content in files.items():\n        await sandbox.write_file(f\"/workspace/{path}\", content)\n\n    # Verify file contents\n    for path, expected_content in files.items():\n        content = await sandbox.read_file(f\"/workspace/{path}\")\n        assert content.strip() == expected_content\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_python_environment(sandbox):\n    \"\"\"Tests Python environment configuration.\"\"\"\n    # Test Python version\n    result = await sandbox.terminal.run_command(\"python3 --version\")\n    assert \"Python 3.10\" in result\n\n    # Test basic module imports\n    python_code = \"\"\"\nimport sys\nimport os\nimport json\nprint(\"Python is working!\")\n\"\"\"\n    await sandbox.write_file(\"/workspace/env_test.py\", python_code)\n    result = await sandbox.terminal.run_command(\"python3 /workspace/env_test.py\")\n    assert \"Python is working!\" in result\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_network_access(sandbox):\n    \"\"\"Tests sandbox network access.\"\"\"\n    if not sandbox.config.network_enabled:\n        pytest.skip(\"Network access is disabled\")\n\n    # Test network connectivity\n    await sandbox.terminal.run_command(\"apt update && apt install curl -y\")\n    result = await sandbox.terminal.run_command(\"curl -I https://www.example.com\")\n    assert \"HTTP/2 200\" in result\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_cleanup(sandbox_config):\n    \"\"\"Tests sandbox cleanup process.\"\"\"\n    sandbox = DockerSandbox(sandbox_config)\n    await sandbox.create()\n\n    # Create test files\n    await sandbox.write_file(\"/workspace/test.txt\", \"test\")\n    container_id = sandbox.terminal.container.id\n    # Perform cleanup\n    await sandbox.cleanup()\n\n    # Verify container has been removed\n    import docker\n\n    client = docker.from_env()\n    containers = client.containers.list(all=True)\n    assert not any(c.id == container_id for c in containers)\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_error_handling():\n    \"\"\"Tests error handling with invalid configuration.\"\"\"\n    # Test invalid configuration\n    invalid_config = SandboxSettings(image=\"nonexistent:latest\", work_dir=\"/invalid\")\n\n    sandbox = DockerSandbox(invalid_config)\n    with pytest.raises(Exception):\n        await sandbox.create()\n\n\nif __name__ == \"__main__\":\n    pytest.main([\"-v\", __file__])\n"
  },
  {
    "path": "tests/sandbox/test_sandbox_manager.py",
    "content": "import asyncio\nimport os\nimport tempfile\nfrom typing import AsyncGenerator\n\nimport pytest\nimport pytest_asyncio\n\nfrom app.sandbox.core.manager import SandboxManager\n\n\n@pytest_asyncio.fixture(scope=\"function\")\nasync def manager() -> AsyncGenerator[SandboxManager, None]:\n    \"\"\"Creates a sandbox manager instance.\n\n    Uses function scope to ensure each test case has its own manager instance.\n    \"\"\"\n    manager = SandboxManager(max_sandboxes=2, idle_timeout=60, cleanup_interval=30)\n    try:\n        yield manager\n    finally:\n        # Ensure all resources are cleaned up\n        await manager.cleanup()\n\n\n@pytest.fixture\ndef temp_file():\n    \"\"\"Creates a temporary test file.\"\"\"\n    with tempfile.NamedTemporaryFile(mode=\"w+\", delete=False) as f:\n        f.write(\"test content\")\n        path = f.name\n    try:\n        yield path\n    finally:\n        if os.path.exists(path):\n            os.unlink(path)\n\n\n@pytest.mark.asyncio\nasync def test_create_sandbox(manager):\n    \"\"\"Tests sandbox creation.\"\"\"\n    # Create default sandbox\n    sandbox_id = await manager.create_sandbox()\n    assert sandbox_id in manager._sandboxes\n    assert sandbox_id in manager._last_used\n\n    # Verify sandbox functionality\n    sandbox = await manager.get_sandbox(sandbox_id)\n    result = await sandbox.run_command(\"echo 'test'\")\n    assert result.strip() == \"test\"\n\n\n@pytest.mark.asyncio\nasync def test_max_sandboxes_limit(manager):\n    \"\"\"Tests maximum sandbox limit enforcement.\"\"\"\n    created_sandboxes = []\n    try:\n        # Create maximum number of sandboxes\n        for _ in range(manager.max_sandboxes):\n            sandbox_id = await manager.create_sandbox()\n            created_sandboxes.append(sandbox_id)\n\n        # Verify created sandbox count\n        assert len(manager._sandboxes) == manager.max_sandboxes\n\n        # Attempting to create additional sandbox should fail\n        with pytest.raises(RuntimeError) as exc_info:\n            await manager.create_sandbox()\n\n        # Verify error message\n        expected_message = (\n            f\"Maximum number of sandboxes ({manager.max_sandboxes}) reached\"\n        )\n        assert str(exc_info.value) == expected_message\n\n    finally:\n        # Clean up all created sandboxes\n        for sandbox_id in created_sandboxes:\n            try:\n                await manager.delete_sandbox(sandbox_id)\n            except Exception as e:\n                print(f\"Failed to cleanup sandbox {sandbox_id}: {e}\")\n\n\n@pytest.mark.asyncio\nasync def test_get_nonexistent_sandbox(manager):\n    \"\"\"Tests retrieving a non-existent sandbox.\"\"\"\n    with pytest.raises(KeyError, match=\"Sandbox .* not found\"):\n        await manager.get_sandbox(\"nonexistent-id\")\n\n\n@pytest.mark.asyncio\nasync def test_sandbox_cleanup(manager):\n    \"\"\"Tests sandbox cleanup functionality.\"\"\"\n    sandbox_id = await manager.create_sandbox()\n    assert sandbox_id in manager._sandboxes\n\n    await manager.delete_sandbox(sandbox_id)\n    assert sandbox_id not in manager._sandboxes\n    assert sandbox_id not in manager._last_used\n\n\n@pytest.mark.asyncio\nasync def test_idle_sandbox_cleanup(manager):\n    \"\"\"Tests automatic cleanup of idle sandboxes.\"\"\"\n    # Set short idle timeout\n    manager.idle_timeout = 0.1\n\n    sandbox_id = await manager.create_sandbox()\n    assert sandbox_id in manager._sandboxes\n\n    # Wait longer than idle timeout\n    await asyncio.sleep(0.2)\n\n    # Trigger cleanup\n    await manager._cleanup_idle_sandboxes()\n    assert sandbox_id not in manager._sandboxes\n\n\n@pytest.mark.asyncio\nasync def test_manager_cleanup(manager):\n    \"\"\"Tests manager cleanup functionality.\"\"\"\n    # Create multiple sandboxes\n    sandbox_ids = []\n    for _ in range(2):\n        sandbox_id = await manager.create_sandbox()\n        sandbox_ids.append(sandbox_id)\n\n    # Clean up all resources\n    await manager.cleanup()\n\n    # Verify all sandboxes have been cleaned up\n    assert not manager._sandboxes\n    assert not manager._last_used\n\n\nif __name__ == \"__main__\":\n    pytest.main([\"-v\", __file__])\n"
  }
]