Skip to content

Commit

Permalink
Merge pull request #72 from Jonghakseo/feature/options
Browse files Browse the repository at this point in the history
feature: add chat history
  • Loading branch information
Jonghakseo authored Aug 6, 2023
2 parents 2e0d7e5 + 8b33837 commit 962a4e5
Show file tree
Hide file tree
Showing 41 changed files with 1,034 additions and 180 deletions.
3 changes: 3 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"slotListPage_newSlotButtonText": {
"message": "Add Slot"
},
"slogListPage_showChatHistoryButtonText": {
"message": "History"
},
"slotListPage_quickChatButtonText": {
"message": "Quick Chat"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"slotListPage_newSlotButtonText": {
"message": "スロットを追加"
},
"slogListPage_showChatHistoryButtonText": {
"message": "대화 기록 보기"
},
"slotListPage_quickChatButtonText": {
"message": "クイックチャット"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/ko/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"slotListPage_newSlotButtonText": {
"message": "슬롯 추가"
},
"slogListPage_showChatHistoryButtonText": {
"message": "대화 기록 보기"
},
"slotListPage_quickChatButtonText": {
"message": "빠른 대화"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"slotListPage_newSlotButtonText": {
"message": "添加插槽"
},
"slogListPage_showChatHistoryButtonText": {
"message": "대화 기록 보기"
},
"slotListPage_quickChatButtonText": {
"message": "快速对话"
},
Expand Down
2 changes: 1 addition & 1 deletion src/chrome/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type GetDataType<T extends Message["type"]> = Exclude<
export type GetDataType<T extends Message["type"]> = Exclude<
Extract<
Message,
{
Expand Down
111 changes: 75 additions & 36 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type Chrome from "chrome";
import type { ChatCompletionRequestMessage } from "openai";
import {
ChatHistories,
SessionHistories,
} from "@pages/background/lib/storage/chatHistoryStorage";

declare namespace chrome {
export default Chrome;
Expand Down Expand Up @@ -56,112 +59,148 @@ declare global {
content: string;
};

type AddNewSlotMessage = {
type AddNewSlot = {
type: "AddNewSlot";
input: Slot;
data?: "success";
};
type SelectSlotMessage = {
type SelectSlot = {
type: "SelectSlot";
input: string;
data?: "success";
};
type UpdateSlotMessage = {
type UpdateSlot = {
type: "UpdateSlot";
input: Slot;
data?: "success";
};
type DeleteSlotMessage = {
type DeleteSlot = {
type: "DeleteSlot";
input: string;
data?: "success";
};
type RequestOnetimeChatGPTMessage = {
type RequestOnetimeChatGPT = {
type: "RequestOnetimeChatGPT";
input: string;
data?: { result: string };
};
type RequestGenerateChatGPTPromptMessage = {
type RequestGenerateChatGPTPrompt = {
type: "RequestGenerateChatGPTPrompt";
input: string;
data?: { result: string };
};
type RequestOngoingChatGPTMessage = {
type RequestOngoingChatGPT = {
type: "RequestOngoingChatGPT";
input: ChatCompletionRequestMessage[];
input: Chat[];
data?: { result: string };
};
type RequestInitialDragGPTMessage = {
type RequestInitialDragGPT = {
type: "RequestInitialDragGPTStream";
input?: string;
data?: { result: string; chunk?: string; isDone?: boolean };
};
type RequestDragGPTMessage = {
type RequestDragGPT = {
type: "RequestDragGPTStream";
input?: ChatCompletionRequestMessage[];
input?: { chats: Chat[]; sessionId: string };
data?: { result: string; chunk?: string; isDone?: boolean };
};
type RequestQuickChatGPTMessage = {
type RequestQuickChatGPT = {
type: "RequestQuickChatGPTStream";
input?: {
messages: ChatCompletionRequestMessage[];
messages: Chat[];
isGpt4: boolean;
};
data?: { result: string; chunk?: string; isDone?: boolean };
};
type SaveAPIKeyMessage = {
type SaveAPIKey = {
type: "SaveAPIKey";
input: string;
data?: "success";
};
type ResetAPIKeyMessage = {
type ResetAPIKey = {
type: "ResetAPIKey";
input?: never;
data?: "success";
};
type GetAPIKeyMessage = {
type GetAPIKey = {
type: "GetAPIKey";
input?: never;
data?: string;
};
type GetSlotsMessage = {
type GetSlots = {
type: "GetSlots";
input?: never;
data?: Slot[];
};
type GetQuickChatHistoryMessage = {
type GetQuickChatHistory = {
type: "GetQuickChatHistory";
input?: never;
data?: Chat[];
};
type ResetQuickChatHistoryMessage = {
type ResetQuickChatHistory = {
type: "ResetQuickChatHistory";
input?: never;
data?: "success";
};
type PushChatHistory = {
type: "PushChatHistory";
input: { chats: Chat | Chat[]; sessionId: string };
data?: "success";
};
type SaveChatHistory = {
type: "SaveChatHistory";
input: { chats: Chat[]; sessionId: string; type: "Quick" | "Drag" };
data?: "success";
};
type DeleteAllChatHistory = {
type: "DeleteAllChatHistory";
input?: never;
data?: "success";
};
type DeleteChatHistorySession = {
type: "DeleteChatHistorySession";
input: string;
data?: "success";
};
type GetAllChatHistory = {
type: "GetAllChatHistory";
input?: never;
data?: ChatHistories;
};
type GetChatSessionHistory = {
type: "GetChatSessionHistory";
input: string;
data?: SessionHistories;
};
type ErrorMessage = {
type: "Error";
input?: never;
error: Error;
};

type Message =
| RequestInitialDragGPTMessage
| RequestQuickChatGPTMessage
| RequestDragGPTMessage
| RequestOngoingChatGPTMessage
| ResetQuickChatHistoryMessage
| GetQuickChatHistoryMessage
| AddNewSlotMessage
| UpdateSlotMessage
| GetSlotsMessage
| GetAPIKeyMessage
| ResetAPIKeyMessage
| SelectSlotMessage
| DeleteSlotMessage
| RequestOnetimeChatGPTMessage
| RequestGenerateChatGPTPromptMessage
| SaveAPIKeyMessage;
| RequestInitialDragGPT
| RequestQuickChatGPT
| RequestDragGPT
| RequestOngoingChatGPT
| ResetQuickChatHistory
| SaveChatHistory
| PushChatHistory
| GetAllChatHistory
| DeleteAllChatHistory
| DeleteChatHistorySession
| GetChatSessionHistory
| GetQuickChatHistory
| AddNewSlot
| UpdateSlot
| GetSlots
| GetAPIKey
| ResetAPIKey
| SelectSlot
| DeleteSlot
| RequestOnetimeChatGPT
| RequestGenerateChatGPTPrompt
| SaveAPIKey;

type RequestMessage<M = Message> = Omit<M, "data">;
type ResponseMessage<M = Message> = Omit<M, "input" | "error">;
Expand Down
49 changes: 46 additions & 3 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
sendMessageToClient,
} from "@src/chrome/message";
import { QuickChatHistoryStorage } from "@pages/background/lib/storage/quickChatHistoryStorage";
import { exhaustiveMatchingGuard } from "@src/shared/ts-util/exhaustiveMatchingGuard";
import { exhaustiveMatchingGuard } from "@src/shared/ts-utils/exhaustiveMatchingGuard";
import { createNewChatGPTSlot } from "@src/shared/slot/createNewChatGPTSlot";
import { PROMPT_GENERATE_PROMPT } from "@src/constant/promptGeneratePrompt";
import { ChatHistoryStorage } from "@pages/background/lib/storage/chatHistoryStorage";

reloadOnUpdate("pages/background");

Expand Down Expand Up @@ -166,9 +167,10 @@ chrome.runtime.onConnect.addListener((port) => {
}
case "RequestDragGPTStream": {
const apiKey = await ApiKeyStorage.getApiKey();
const slot = await SlotStorage.getSelectedSlot();
const response = await chatGPT({
chats: message.input,
slot: { type: "ChatGPT" },
chats: message.input?.chats,
slot: { type: slot.type },
apiKey,
onDelta: (chunk) => {
sendResponse({
Expand Down Expand Up @@ -223,6 +225,47 @@ chrome.runtime.onConnect.addListener((port) => {
sendResponse({ type: "ResetQuickChatHistory", data: "success" });
break;
}
case "PushChatHistory": {
await ChatHistoryStorage.pushChatHistories(
message.input.sessionId,
message.input.chats
);
sendResponse({ type: "PushChatHistory", data: "success" });
break;
}
case "SaveChatHistory": {
await ChatHistoryStorage.saveChatHistories(
message.input.sessionId,
message.input.chats,
message.input.type
);
sendResponse({ type: "SaveChatHistory", data: "success" });
break;
}
case "DeleteChatHistorySession": {
await ChatHistoryStorage.deleteChatHistory(message.input);
sendResponse({ type: "DeleteChatHistorySession", data: "success" });
break;
}
case "DeleteAllChatHistory": {
await ChatHistoryStorage.resetChatHistories();
sendResponse({ type: "DeleteAllChatHistory", data: "success" });
break;
}
case "GetAllChatHistory": {
sendResponse({
type: "GetAllChatHistory",
data: await ChatHistoryStorage.getChatHistories(),
});
break;
}
case "GetChatSessionHistory": {
sendResponse({
type: "GetChatSessionHistory",
data: await ChatHistoryStorage.getChatHistory(message.input),
});
break;
}
default: {
exhaustiveMatchingGuard(message);
}
Expand Down
19 changes: 14 additions & 5 deletions src/pages/background/lib/infra/chatGPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function chatGPT({
onDelta,
}: {
slot: ChatGPTSlot;
chats?: ChatCompletionRequestMessage[];
chats?: Chat[];
input?: string;
apiKey: string;
onDelta?: (chunk: string) => unknown;
Expand All @@ -32,7 +32,7 @@ export async function chatGPT({
});
}
if (hasChats(chats)) {
messages.push(...chats);
messages.push(...convertChatsToMessages(chats));
}
if (input) {
messages.push({ role: "user", content: input });
Expand Down Expand Up @@ -131,8 +131,17 @@ async function requestApi(apiKey: string, body: CreateChatCompletionRequest) {
});
}

function hasChats(
chats?: ChatCompletionRequestMessage[]
): chats is ChatCompletionRequestMessage[] {
function hasChats(chats?: Chat[]): chats is Chat[] {
return chats !== undefined && chats.length > 0;
}

function convertChatsToMessages(chats: Chat[]): ChatCompletionRequestMessage[] {
return chats
.filter((chat) => chat.role !== "error")
.map((chat) => {
return {
role: chat.role === "user" ? "user" : "assistant",
content: chat.content,
};
});
}
Loading

0 comments on commit 962a4e5

Please sign in to comment.