Wox-launcher / Wox

A cross-platform launcher that simply works
http://wox-launcher.github.io/Wox/
GNU General Public License v3.0
24.62k stars 2.37k forks source link

Custom Plugin, arg variable won't be passed to the RPCActionMethod and becomes an empty String. #4039

Open NG-Bullseye opened 6 months ago

NG-Bullseye commented 6 months ago

Version 1.4.1196

Platform

Description

I'm developing a custom plugin and encountering an issue.

_The issue is that the user_input argument that is passed from the query method to my custom method often contains an empty string even though the arg variable contains the input from the user from the Wox bar._

It turns out that for unknown reasons, very rarely the method does receive a portion of the user input. I invoke the script with Wox using the format "f text that should be passed as user_input", where f triggers the script. I have tested and tried everything. The HelloWorld example works flawlessly. Why is there nondeterministic behavior in parameter passing? Additionally, the arg variable is always populated with the user input as expected.

I'm not using a context menu as demonstrated in the HelloWorld plugin.

Here is my code. The variables arg and user_input are important for my explanation. arg contains what the user inputs in the Wox bar, and user_input is what the executing logic receives.

#` -*- coding: utf-8 -*-
import json
import webbrowser
import openai
import pyperclip
from wox import Wox, WoxAPI
import subprocess  # Import the subprocess module

class HelloWorld(Wox):
    """Example Python plugin for querying OpenAI GPT model and handling web shortcuts."""

    def query(self, arg):
        """Searches for a query string in titles and subtitles and returns results.
        Additionally, saves the structure of 'arg' to a JSON file."""
        # Return the correct JsonRPCAction with a parameter list
        return [{
            'Title': 'Clipboard GPT',
            'JsonRPCAction': {
                'method': 'gpt_clipboard',
                'parameters': [arg],  # Ensure parameters are passed as a list
                'dontHideAfterAction': False,
            }
        }]

    def gpt_clipboard(self, user_input):
        """Handles clipboard operations for GPT responses."""
        # Deserialize the 'user_input' if necessary (depends on how it's sent from 'query')
        command = "You are an assistant that answers with main information only. Here is your task: " + str(user_input)

        try:
            response = openai.chat.completions.create(
                model="gpt-4-turbo",
                messages=[{"role": "user", "content": command}]
            )
            answer = response.choices[0].message.content
            pyperclip.copy(answer)  # Copy the answer directly to clipboard

            reflection = command + " " + answer + " Here is your task. Check if the constraints are met. If not respond only with the piece of information the user needs for his clipboard. for example, the code, the link, the command, the number and so on."
            response = openai.chat.completions.create(
                model="gpt-4-turbo",
                messages=[{"role": "user", "content": reflection}]
            )
            answer = response.choices[0].message.content
            pyperclip.copy(answer)  # Copy the answer directly to clipboard
            return [{
                'Title': 'AI Response',
                'SubTitle': answer,
                'IcoPath': 'Images/app.png'
            }]
        except Exception as e:
            return [{
                'Title': 'Error',
                'SubTitle': f'An error occurred: {str(e)}',
                'IcoPath': 'Images/app.png'
            }]
if __name__ == "__main__":
    HelloWorld()

The issue is that the user_input argument in the gpt_clipboard method often contains an empty string. It turns out that for unknown reasons, very rarely the method does receive a portion of the user input. I invoke the script with Wox using the format "f text that should be passed as user_input", where f triggers the script. I have tested and tried everything. The HelloWorld example works flawlessly. Why is there nondeterministic behavior in parameter passing? Additionally, the arg variable is always populated with the user input as expected.

Thank you for your help in resolving this issue.