Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to use browser tool with prompt_toolkit #351

Closed
patriklaurell opened this issue Dec 18, 2024 · 5 comments · Fixed by #353
Closed

Fails to use browser tool with prompt_toolkit #351

patriklaurell opened this issue Dec 18, 2024 · 5 comments · Fixed by #353

Comments

@patriklaurell
Copy link

Fails to read urls

Preview
read_url('https://www.swiss.com/lhg/ch/en/o-d/cy-cy/zurich-copenhagen')

[13:40:32] ERROR    asyncio.run() cannot be called from a running event loop                                                                                                                     base.py:297
                    Traceback (most recent call last):
                      File "/Users/patriklaurell/repos/gptme/gptme/tools/base.py", line 292, in execute
                        yield from ex
                      File "/Users/patriklaurell/repos/gptme/gptme/tools/python.py", line 79, in execute_python
                        if not confirm("Execute this code?"):
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      File "/Users/patriklaurell/repos/gptme/gptme/chat.py", line 107, in confirm_func
                        return ask_execute(msg)
                               ^^^^^^^^^^^^^^^^
                      File "/Users/patriklaurell/repos/gptme/gptme/util/ask_execute.py", line 110, in ask_execute
                        session.prompt(
                      File "/Users/patriklaurell/.local/pipx/venvs/gptme/lib/python3.12/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
                        return self.app.run(
                               ^^^^^^^^^^^^^
                      File "/Users/patriklaurell/.local/pipx/venvs/gptme/lib/python3.12/site-packages/prompt_toolkit/application/application.py", line 1002, in run
                        return asyncio.run(coro)
                               ^^^^^^^^^^^^^^^^^
                      File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 190, in run
                        raise RuntimeError(
                    RuntimeError: asyncio.run() cannot be called from a running event loop
System: Error executing tool 'python': asyncio.run() cannot be called from a running event loop
/Users/patriklaurell/repos/gptme/gptme/tools/base.py:300: RuntimeWarning: coroutine 'Application.run_async' was never awaited
  yield Message("system", f"Error executing tool '{self.tool}': {e}")
@ErikBjare
Copy link
Owner

ErikBjare commented Dec 18, 2024

This seems to always happen on calls to session.prompt (either ask_confirm or user input) after using the browser tool (either search or read_url).

Probably something with how prompt_toolkit and playwright interact with async together.

@ErikBjare
Copy link
Owner

@gptme what do you think?

Copy link
Contributor

Thank you for reporting this issue. Based on the provided information, it appears we're dealing with a complex interaction between the browser tool (likely using Playwright) and prompt_toolkit, specifically related to asyncio event loops. Here's an analysis and some suggestions:

  1. Root cause: The error occurs because asyncio.run() is being called from an already running event loop. This happens when the browser tool (which probably sets up its own event loop) is used before prompt_toolkit tries to create its own loop for user input.

  2. Potential solutions:

    a. Refactor to use a single event loop: Modify the codebase to use a single, consistent event loop for both the browser tool and prompt_toolkit. This would involve rewriting parts of the application to be fully asynchronous.

    b. Isolate browser operations: Run browser operations in a separate process or thread to avoid event loop conflicts with prompt_toolkit.

    c. Alternative input method: Consider using a different library for user input that doesn't rely on asyncio in the same way as prompt_toolkit.

  3. Next steps:

    a. Investigate the browser tool implementation, focusing on its asyncio usage.
    b. Review how prompt_toolkit is integrated into the project.
    c. Research how other projects handle similar situations with browser automation and interactive prompts.

  4. Temporary workaround: Consider adding a flag to disable the browser tool when using interactive prompts. This would at least allow users to use one feature at a time until a proper fix is implemented.

  5. Testing: Once a solution is implemented, thorough testing will be crucial, especially focusing on the interaction between browser operations and user prompts.

To move forward:

  1. We should investigate the exact implementation of the browser tool and its async behavior.
  2. Consider refactoring the application to use a single, consistent async approach throughout.
  3. If refactoring is too complex, explore running browser operations in a separate process.
  4. Update the error handling to provide more informative messages to users when this conflict occurs.

This is a non-trivial issue that will require careful consideration of the project's architecture and async handling. It might be worth creating a separate development branch to experiment with different solutions without affecting the main codebase.

We appreciate your patience as we work on resolving this issue. If you have any additional information or can provide a minimal reproducible example, it would be very helpful in our investigation.

@ErikBjare
Copy link
Owner

lol, yikes. I guess the easiest thing is to isolate the browser stuff into its own process.

Good bot!

ErikBjare added a commit that referenced this issue Dec 18, 2024
Resolves #351 by moving Playwright to a separate thread to isolate its event loop
from prompt_toolkit. This prevents the asyncio.run() error that occurred when
trying to use prompt_toolkit after browser operations.

Changes:
- Created thread-based browser manager
- Updated all browser operations to use the thread
- Added proper timeout and error handling
@ErikBjare
Copy link
Owner

I've implemented a fix in #353 that moves Playwright to a separate thread to isolate its event loop from prompt_toolkit.

The fix has been tested and works with all browser operations while maintaining prompt_toolkit functionality. Please test and let me know if you encounter any issues!

ErikBjare added a commit that referenced this issue Dec 18, 2024
Resolves #351 by moving Playwright to a separate thread to isolate its event loop
from prompt_toolkit. This prevents the asyncio.run() error that occurred when
trying to use prompt_toolkit after browser operations.

Changes:
- Created thread-based browser manager
- Updated all browser operations to use the thread
- Added proper timeout and error handling

Co-authored-by: Bob <[email protected]>
ErikBjare added a commit that referenced this issue Dec 18, 2024
Resolves #351 by moving Playwright to a separate thread to isolate its event loop
from prompt_toolkit. This prevents the asyncio.run() error that occurred when
trying to use prompt_toolkit after browser operations.

Changes:
- Created thread-based browser manager
- Updated all browser operations to use the thread
- Added proper timeout and error handling

Co-authored-by: Bob <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants