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

simple add-on created #1

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions src/content.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,78 @@
import * as InboxSDK from '@inboxsdk/core';
import { Configuration, OpenAIApi } from "openai";

InboxSDK.load(2, "Hello World!").then((sdk) => {
// the SDK has been loaded, now do something with it!
const apiKey = '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';

async function generateText(prompt) {


// Make the API request
const response = await fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
"model": "text-davinci-002",
"prompt": prompt,
"max_tokens": 2048
})
});
const json = await response.json();
console.log(json.choices.text)
console.log(json)
console.log(json.choices)
console.log(json.choices["0"].text)


return json.choices["0"].text;

}

InboxSDK.load(2, "Hello World!", { timeout: 30000 }).then((sdk) => {
sdk.Compose.registerComposeViewHandler((composeView) => {
// a compose view has come into existence, do something with it!
composeView.addButton({
title: "My Nifty Button!",
title: "Generate Email Response",
iconUrl:
"https://lh5.googleusercontent.com/itq66nh65lfCick8cJ-OPuqZ8OUDTIxjCc25dkc4WUT1JG8XG3z6-eboCu63_uDXSqMnLRdlvQ=s128-h128-e365",
onClick(event) {
event.composeView.insertTextIntoBodyAtCursor("Hello World!");
// Create a form element
const form = document.createElement('form');
form.innerHTML = `
<label for="prompt">What is the email about?</label><br>
<input type="text" id="prompt" name="prompt"><br>
<input type="submit" value="Generate Email">
`;
// Add the form to the compose view
event.composeView.insertHTMLIntoBodyAtCursor(form);

// Add a submit event listener to the form
form.addEventListener('submit', async (e) => {
e.preventDefault();
// Get the value of the prompt input field
const prompt = form.querySelector('#prompt').value;

// Declare the response variable
let response;
// Generate the email response
response = await generateText(prompt);
console.log("m")
console.log(response)
console.log("i")

form.remove();
console.log(response)
// Insert the email response into the compose view
event.composeView.insertHTMLIntoBodyAtCursor(`<div>${response}</div>`);



});
},


});
});
});