Open Correct selection with ChatGPT in Script Kit
/*# Correct selection with ChatGPTFix grammar and spelling mistakes in any text field.Highlight some text and press `cmd+option+g` to send it through ChatGPT to replace the text response.*/// Name: Correct selection// Description: Fix grammar and spelling mistakes in any text field.// Author: Evan Fisher// Twitter: @ivryb// Shortcut: cmd option gimport '@johnlindquist/kit';import Bottleneck from 'bottleneck';import { createChat } from 'completions';const openAiKey = await env('OPENAI_API_KEY', {hint: `Grab a key from <a href="https://platform.openai.com/account/api-keys">here</a>`,});const chat = createChat({apiKey: openAiKey,model: 'gpt-3.5-turbo',});const correctionPrompt = (text) =>`Please fix the grammar and spelling of the following text and return it without any other changes: ###${text}###`;const limiter = new Bottleneck({maxConcurrent: 1,minTime: 100,});const type = (text) => {return new Promise((resolve) => {keyboard.type(text);resolve();});};const wrappedType = limiter.wrap(type);const text = await getSelectedText();if (text) {await chat.sendMessage(correctionPrompt(text), {onUpdate: async ({ message }) => {const content = message.choices[0]?.delta?.content;if (content) {wrappedType(content);}},});}