Skip to content

Commit

Permalink
Merge pull request #68 from Jonghakseo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Jonghakseo authored May 28, 2023
2 parents 8aba780 + df785fa commit 42840d4
Show file tree
Hide file tree
Showing 21 changed files with 587 additions and 154 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Code Review with ChatGPT

on:
pull_request:
types: [opened]

jobs:
add-auto-review-comment:
if: contains(github.event.pull_request.labels.*.name, 'review')
runs-on: ubuntu-latest
name: Code Review with ChatGPT
steps:
- uses: Jonghakseo/gpt-pr-github-actions@v1
with:
openai_api_key: ${{ secrets.openai_api_key }} # Get the OpenAI API key from repository secrets
github_token: ${{ secrets.GITHUB_TOKEN }} # Get the Github Token from repository secrets
github_pr_id: ${{ github.event.number }} # Get the Github Pull Request ID from the Github event
openai_model: "gpt-3.5-turbo" # Optional: specify the OpenAI engine to use. [gpt-3.5-turbo, text-davinci-002, text-babbage-001, text-curie-001, text-ada-001'] Default is 'gpt-3.5-turbo'
openai_temperature: 0.5 # Optional: Default is 0.7
openai_top_p: 0.5 # Optional: Default 0.8
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drag-gpt",
"version": "1.3.11",
"version": "1.4.0",
"description": "Drag GPT chrome extension",
"license": "MIT",
"repository": {
Expand All @@ -24,11 +24,8 @@
"@emotion/cache": "11.10.5",
"@emotion/react": "11.10.6",
"@emotion/styled": "11.10.6",
"@vespaiach/axios-fetch-adapter": "0.3.1",
"@xstate/react": "3.2.1",
"axios": "0.26.0",
"framer-motion": "10.0.1",
"openai": "3.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-draggable": "4.4.5",
Expand All @@ -54,6 +51,7 @@
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-react": "7.31.8",
"fs-extra": "10.1.0",
"openai": "3.2.1",
"jest": "29.0.3",
"jest-environment-jsdom": "29.0.3",
"npm-run-all": "^4.1.5",
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"quickChattingPage_sendButtonText": {
"message": "SEND"
},
"quickChattingPage_stopButtonText": {
"message": "STOP"
},
"quickChattingPage_chattingPlaceholder": {
"message": "ex. Hello!"
},
Expand Down Expand Up @@ -110,6 +113,9 @@
"responseMessageBox_sendButtonText": {
"message": "SEND"
},
"responseMessageBox_stopButtonText": {
"message": "STOP"
},
"responseMessageBox_messageInputPlacepolder": {
"message": "ex. Summarize!"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"quickChattingPage_sendButtonText": {
"message": "送信"
},
"quickChattingPage_stopButtonText": {
"message": "回答中断"
},
"quickChattingPage_chattingPlaceholder": {
"message": "例:こんにちは!"
},
Expand Down Expand Up @@ -110,6 +113,9 @@
"responseMessageBox_sendButtonText": {
"message": "送信"
},
"responseMessageBox_stopButtonText": {
"message": "回答中断"
},
"responseMessageBox_messageInputPlacepolder": {
"message": "例:要約!"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/ko/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"quickChattingPage_sendButtonText": {
"message": "전송"
},
"quickChattingPage_stopButtonText": {
"message": "답변 중단"
},
"quickChattingPage_chattingPlaceholder": {
"message": "ex. 안녕!"
},
Expand Down Expand Up @@ -110,6 +113,9 @@
"responseMessageBox_sendButtonText": {
"message": "전송"
},
"responseMessageBox_stopButtonText": {
"message": "답변 중단"
},
"responseMessageBox_messageInputPlacepolder": {
"message": "ex. 위 내용을 알기 쉽게 요약해줘"
},
Expand Down
6 changes: 6 additions & 0 deletions public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"quickChattingPage_sendButtonText": {
"message": "发送"
},
"quickChattingPage_stopButtonText": {
"message": "回答中止"
},
"quickChattingPage_chattingPlaceholder": {
"message": "例如:你好!"
},
Expand Down Expand Up @@ -110,6 +113,9 @@
"responseMessageBox_sendButtonText": {
"message": "发送"
},
"responseMessageBox_stopButtonText": {
"message": "回答中止"
},
"responseMessageBox_messageInputPlacepolder": {
"message": "例如:总结!"
},
Expand Down
29 changes: 13 additions & 16 deletions src/chrome/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AxiosError } from "axios";

type GetDataType<T extends Message["type"]> = Exclude<
Extract<
Message,
Expand Down Expand Up @@ -51,6 +49,10 @@ export function sendMessageToBackground<M extends Message>({
} catch (error) {
console.log(error);
}
const disconnect = () => {
port.disconnect();
};
return { disconnect };
}

export function sendMessageToClient(
Expand All @@ -68,19 +70,14 @@ export function sendErrorMessageToClient(
port: chrome.runtime.Port,
error: unknown
) {
if (!(error instanceof Error)) {
const unknownError = new Error();
unknownError.name = "Unknown Error";
sendMessageToClient(port, { type: "Error", error: unknownError });
return;
}
if ((error as AxiosError).isAxiosError) {
const axiosError = error as AxiosError;
const customError = new Error();
customError.message = axiosError.response?.data?.error?.message;
customError.name = axiosError.response?.data?.error?.code ?? error.name;
sendMessageToClient(port, { type: "Error", error: customError });
} else {
sendMessageToClient(port, { type: "Error", error });
const sendError = new Error();
sendError.name = "Unknown Error";

if (error instanceof Error) {
error.name && (sendError.name = error.name);
sendError.message = error.message;
}

sendMessageToClient(port, { type: "Error", error: sendError });
return;
}
16 changes: 11 additions & 5 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,27 @@ declare global {
type RequestOnetimeChatGPTMessage = {
type: "RequestOnetimeChatGPT";
input: string;
data?: { result: string; tokenUsage: number };
data?: { result: string };
};
type RequestGenerateChatGPTPromptMessage = {
type: "RequestGenerateChatGPTPrompt";
input: string;
data?: { result: string; tokenUsage: number };
data?: { result: string };
};
type RequestOngoingChatGPTMessage = {
type: "RequestOngoingChatGPT";
input: ChatCompletionRequestMessage[];
data?: { result: string; tokenUsage: number };
data?: { result: string };
};
type RequestInitialDragGPTMessage = {
type: "RequestInitialDragGPTStream";
input?: string;
data?: { result: string; chunk?: string; isDone?: boolean };
};
type RequestQuickChatGPTMessage = {
type: "RequestQuickChatGPT";
type: "RequestChatGPTStream";
input?: ChatCompletionRequestMessage[];
data?: { result: string; tokenUsage: number };
data?: { result: string; chunk?: string; isDone?: boolean };
};
type SaveAPIKeyMessage = {
type: "SaveAPIKey";
Expand Down Expand Up @@ -133,6 +138,7 @@ declare global {
};

type Message =
| RequestInitialDragGPTMessage
| RequestQuickChatGPTMessage
| RequestOngoingChatGPTMessage
| ResetQuickChatHistoryMessage
Expand Down
46 changes: 43 additions & 3 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type RequiredDataNullableInput<T extends Message> = {
};

chrome.runtime.onConnect.addListener((port) => {
port.onDisconnect.addListener(() => console.log("Port disconnected"));
port.onDisconnect.addListener(() => {
console.log("Port disconnected");
});
port.onMessage.addListener(async (message: Message) => {
Logger.receive(message);

Expand Down Expand Up @@ -92,6 +94,32 @@ chrome.runtime.onConnect.addListener((port) => {
await ApiKeyStorage.setApiKey(null);
sendResponse({ type: "ResetAPIKey", data: "success" });
break;
case "RequestInitialDragGPTStream": {
const slot = await SlotStorage.getSelectedSlot();
const apiKey = await ApiKeyStorage.getApiKey();
const response = await chatGPT({
input: message.input,
slot,
apiKey,
onDelta: (chunk) => {
sendResponse({
type: "RequestInitialDragGPTStream",
data: {
result: "",
chunk,
},
});
},
});
sendResponse({
type: "RequestInitialDragGPTStream",
data: {
isDone: true,
result: response.result,
},
});
break;
}
case "RequestOnetimeChatGPT": {
const selectedSlot = await SlotStorage.getSelectedSlot();
const apiKey = await ApiKeyStorage.getApiKey();
Expand All @@ -106,7 +134,7 @@ chrome.runtime.onConnect.addListener((port) => {
});
break;
}
case "RequestQuickChatGPT": {
case "RequestChatGPTStream": {
await QuickChatHistoryStorage.pushChatHistories({
role: "user",
content: message.input?.at(-1)?.content ?? "",
Expand All @@ -116,12 +144,24 @@ chrome.runtime.onConnect.addListener((port) => {
chats: message.input,
slot: { type: "ChatGPT" },
apiKey,
onDelta: (chunk) => {
sendResponse({
type: "RequestChatGPTStream",
data: {
result: "",
chunk,
},
});
},
});
await QuickChatHistoryStorage.pushChatHistories({
role: "assistant",
content: response.result,
});
sendResponse({ type: "RequestQuickChatGPT", data: response });
sendResponse({
type: "RequestChatGPTStream",
data: { result: response.result, isDone: true },
});
break;
}
case "RequestOngoingChatGPT": {
Expand Down
Loading

0 comments on commit 42840d4

Please sign in to comment.