Action
Personal Assistant
Posted by meetheverse,
Last update
over 1 year ago
This action allows the user to get an AI-assisted response to a prompt:
1. The user is prompted to enter input, which can be pasted from the clipboard or manually entered.
2. The input is then sent to the OpenAI API, and the response is inserted into the current draft 3 lines after the cursor.
3. If there is no selected text in the draft, the user is asked if they would like to use text from the clipboard. If the prompt is canceled or the input is empty, the action cancels.
4. If there is no response from the API, the output is set to “No reply received.”
For more detailed information, the script is sufficiently comprehensive. Refer to the action steps in order to customize it to your preferences.
Steps
-
script
// See online documentation for examples // https://docs.getdrafts.com/docs/actions/scripting // For more information on using this action, visit // https://forums.getdrafts.com/t/using-openai-chatgpt-with-drafts/14221 // Define a function f let f = () => { // Create a new Prompt object let p = new Prompt(); // Set the title of the prompt p.title = "AI Assistant"; // Set the message of the prompt p.message = "The box below contains your input, which can either be pasted from the clipboard or manually provided. The output will be inserted 3 lines after the cursor in the current draft."; // Define placeholder text for the prompt input let placeholderText = ""; // Get selected text from the editor let selectedText = editor.getSelectedText(); // If there is selected text, add it to the placeholder text if (selectedText.length > 0) { placeholderText += selectedText; } else { // If there is no selected text, get text from clipboard let clipboardText = app.getClipboard(); // Create a new Prompt object to ask if user wants to use clipboard text let cp = new Prompt(); cp.title = "Paste from Clipboard"; cp.message = "Would you like to use your clipboard as an input for the prompt? If so, hit ‘Yes.’ Otherwise, ‘Cancel’ to skip this step."; cp.addButton("Yes"); let response = cp.show(); // If user wants to use clipboard text, add it to placeholder text if (response && cp.buttonPressed == "Yes") { placeholderText += clipboardText; } } // Add a text view to the prompt with placeholder text p.addTextView("prompt", "Ask me anything...", placeholderText); // Add a button to the prompt p.addButton("Done"); // Show the prompt and return false if user cancels if (!p.show()) { return false; } // Get user input from prompt const chatPrompt = p.fieldValues["prompt"]; // Return false if user input is empty if (chatPrompt.length == 0) { return false; } // Create a new OpenAI object let ai = new OpenAI(); // Get AI response using quickChatResponse method with specified model let answer = ai.quickChatResponse(chatPrompt, { model: "gpt-3.5-turbo" }); // If no answer is received, set answer to "No reply received" if (!answer || answer.length == 0) { answer = "No reply received"; } // Format content with input and output let content = `\n\n\n\n# Input:\n${chatPrompt}\n\n## Output:\n${answer}`; // Insert content into editor after selected text editor.setSelectedText(selectedText + content); // Return true to indicate success return true; }; // Call function f and cancel context if it returns false if (!f()) { context.cancel(); }
Options
-
After Success Nothing , Tags: ai-assisted Notification Info Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.