Skip to content

Commit

Permalink
v0.0.14 (#21)
Browse files Browse the repository at this point in the history
*v0.0.14
  • Loading branch information
albertocubeddu authored Aug 14, 2024
1 parent 9232260 commit 556a575
Show file tree
Hide file tree
Showing 19 changed files with 13,589 additions and 10,459 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,22 @@ Move it somewhere else ASAP:

# Changelog

### 0.0.13


### 0.0.14
- Fixed grammar issues, thanks to Luca.
- Introduced a new menu, courtesy of Denis.
- The new menu currently does not support phone calls (feature coming soon).


### 0.0.13

- Enhanced UI (tooltips are now more noticeable) thanks to Juanjo (We Move Experience) and Agostina (PepperStudio)
- Prompt Factory: Utilizing AutoTextArea for improved prompt display
- Prompt Factory: Removed the ID to improve user experience (non-tech users)
- System: Split the systemPrompt from the userPrompt.
- UX: Small improvements and removed the complicated items



### 0.0.12 (Not released to the public)

- General: Free tier exhaustion. We haven't got a sponsor (yet) to support our community users.
Expand Down Expand Up @@ -211,3 +217,9 @@ Move it somewhere else ASAP:
## 0.0.1

- Check the demo video

# Gotchas

- Ensure that the open.sidePanel is always initialized before the Plasmo Storage.
- We currently have two menus that function similarly but not identically; we need to implement a more efficient solution to consolidate them into one.
- The Plasmo handler may stop functioning unexpectedly without errors if a response is not returned; ensure to always return a response to prevent this issue.
75 changes: 17 additions & 58 deletions background/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// nneolbdbfjmdjmnpginhclljaphcdnad
import { sendToBackground } from "@plasmohq/messaging";
import { Storage } from "@plasmohq/storage";

import { initializeStorage } from "~background/init";
import { cleanProperties } from "~lib/cleanContextMenu";
import { callOpenAIReturn, type ApiResponse } from "~lib/openAITypeCall";
import { createCall } from "~lib/vapiOutbound";
// Importing the handler and renaming it to openOptionPage
import openOptionPage, {
openOptionsPageHandler,
} from "./messages/openOptionPage"; // Fixed import path
import sendLoadingAction, {
sendLoadingActionHandler,
} from "./messages/sendLoadingAction";
import copyTextToClipboard, {
copyTextToClipboardHandler,
} from "./messages/copyTextToClipboard";

const storage = new Storage();

/*
Fired when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version.
*/
// Fired when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version.
// */
chrome.runtime.onInstalled.addListener(async (details) => {
if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
console.log(
Expand Down Expand Up @@ -65,16 +74,16 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
//In the past we've used the hashmap, however it would overcomplicated the rest of the codebase always because we are not able to use the chrome.storage and the sidebar.open in the same function. This can be reviewed and use an hashmap if we find the solution for that bug. At the moment i don't expect having more than 20 prompt per user, so readability and clean code beats efficiency.
const element = items.find((item) => item.id === info.menuItemId);

//We have to use handler, as the other option would be to modify how plasmo work, or extend the responseClass to accept a return that is not VOID!
switch (info.menuItemId) {
case element.id:
if (element.id === "configuration") {
await storage.set("activeTab", "promptFactory");
chrome.runtime.openOptionsPage();
await openOptionsPageHandler();
}
if (element.functionType === "callAI-copyClipboard") {
sendLoadingAction();
await sendLoadingActionHandler();
response = await callOpenAIReturn(element.prompt, message);
copyTextToClipboard(response);
await copyTextToClipboardHandler(response.data);
break;
}

Expand All @@ -93,11 +102,6 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (element.functionType === "callAI-openSideBar") {
response = await callOpenAIReturn(element.prompt, message);

// Leaving this comment to open an issue with Plamos or to dig down in the codebase and see why it doens't work as expeected. The issuse disappear as soon as i remove the storage.get()
// await chrome.sidePanel.open({
// tabId: tab.id ?? undefined, // Use nullish coalescing to handle undefined or null
// });

try {
chrome.runtime.sendMessage({
action: "send_to_sidepanel",
Expand All @@ -112,48 +116,3 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
console.warn("Unhandled menu item:", info.menuItemId);
}
});

const copyTextToClipboard = (response: ApiResponse<any>) => {
if (!response.errorMessage) {
try {
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "copyToClipboard",
text: response.data,
});
} else {
throw new Error("No active tab found.");
}
}
);
} catch (error) {
console.error("Failed to copy text to clipboard:", error);
}
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "error",
text: response.errorMessage,
});
} else {
throw new Error("No active tab found.");
}
});
}
};

function sendLoadingAction() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "loadingAction",
});
} else {
throw new Error("No active tab found.");
}
});
}
16 changes: 16 additions & 0 deletions background/messages/callOpenAIReturn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import { callOpenAIReturn } from "~lib/openAITypeCall";

export type RequestBody = {
prompt: string;
selectedText: string;
};

const handler: PlasmoMessaging.MessageHandler = async (request, response) => {
const { prompt, selectedText } = request.body;

const responseFromApi = await callOpenAIReturn(prompt, selectedText);
response.send(responseFromApi);
};

export default handler;
50 changes: 50 additions & 0 deletions background/messages/copyTextToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";

export type RequestBody = {
errorMessage: string;
data: string;
};

export type RequestResponse = string;

export async function copyTextToClipboardHandler(req) {
if (!req.errorMessage) {
try {
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "copyToClipboard",
text: req.data,
});
} else {
throw new Error("No active tab found.");
}
}
);
} catch (error) {
console.error("Failed to copy text to clipboard:", error);
}
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "error",
text: req.errorMessage,
});
} else {
throw new Error("No active tab found.");
}
});
}
}

const handler: PlasmoMessaging.MessageHandler<
RequestBody,
RequestResponse | undefined
> = async (req, res) => {
copyTextToClipboardHandler(req.body);
};

export default handler;
19 changes: 19 additions & 0 deletions background/messages/openOptionPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import { Storage } from "@plasmohq/storage";

const storage = new Storage();

export async function openOptionsPageHandler() {
await storage.set("activeTab", "promptFactory");
chrome.runtime.openOptionsPage();
return {
message: "Options page opened",
};
}

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const result = await openOptionsPageHandler();
res.send(result);
};

export default handler;
11 changes: 11 additions & 0 deletions background/messages/openSidePanel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
await chrome.sidePanel.open({
tabId: req.sender.tab.id ?? undefined, // Use nullish coalescing to handle undefined or null
});

res.send("ok");
};

export default handler;
23 changes: 23 additions & 0 deletions background/messages/sendLoadingAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";

export async function sendLoadingActionHandler() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "loadingAction",
});
} else {
throw new Error("No active tab found.");
}
});
return {
message: "Options page opened",
};
}

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const result = await sendLoadingActionHandler();
res.send(result);
};

export default handler;
19 changes: 19 additions & 0 deletions background/messages/sendToSidepanel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";

export type RequestBody = {
data: string;
};

const handler: PlasmoMessaging.MessageHandler = async (request, response) => {
try {
await chrome.runtime.sendMessage({
action: "send_to_sidepanel",
payload: request.body.data,
});
} catch (error) {
response.send("Ok");
}
response.send("Ok");
};

export default handler;
Loading

0 comments on commit 556a575

Please sign in to comment.