// Name: Correct selection // Description: Fix grammar and spelling mistakes in any text field. // Author: Evan Fisher // Twitter: @ivryb // Shortcut: cmd option g import '@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); } }, }); } ```