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

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

Open
2 of 5 tasks
NG-Bullseye opened this issue May 28, 2024 · 0 comments
Assignees

Comments

@NG-Bullseye
Copy link

NG-Bullseye commented May 28, 2024

Version 1.4.1196

  • v1 (has no resource to fix)
  • v2

Platform

  • Macos
  • Windows
  • Linux

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.

@NG-Bullseye NG-Bullseye changed the title Custom Plugin, arg variable wont be passed to the RPCActionMethod. Custom Plugin, arg variable won't be passed to the RPCActionMethod and becomes an empty String. May 28, 2024
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

No branches or pull requests

2 participants