Skip to content

Commit

Permalink
WIP: Create single team per chat
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Dec 18, 2023
1 parent 53157d3 commit ce5d872
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 209 deletions.
18 changes: 11 additions & 7 deletions main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ entity Chat {=psl
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team_id Int?
team_name String?
team_status String?
user User? @relation(fields: [userId], references: [id])
userId Int?
name String?
Expand All @@ -133,10 +136,6 @@ entity Conversation {=psl
updatedAt DateTime @updatedAt
message String
role String
team_id Int?
team_name String?
team_status String?
is_question_from_agent Boolean @default(false)
chat Chat? @relation(fields: [chatId], references: [id])
chatId Int?
user User? @relation(fields: [userId], references: [id])
Expand Down Expand Up @@ -227,9 +226,9 @@ action addNewConversationToChat {
entities: [Chat, Conversation]
}

action updateExistingConversation {
fn: import { updateExistingConversation } from "@server/actions.js",
entities: [Chat, Conversation]
action updateExistingChat {
fn: import { updateExistingChat } from "@server/actions.js",
entities: [Chat]
}

action getAgentResponse {
Expand Down Expand Up @@ -259,6 +258,11 @@ query getChats {
entities: [Chat]
}

query getChat {
fn: import { getChat } from "@server/queries.js",
entities: [Chat]
}

query getConversations {
fn: import { getConversations } from "@server/queries.js",
entities: [Conversation]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Warnings:
- You are about to drop the column `is_question_from_agent` on the `Conversation` table. All the data in the column will be lost.
- You are about to drop the column `team_id` on the `Conversation` table. All the data in the column will be lost.
- You are about to drop the column `team_name` on the `Conversation` table. All the data in the column will be lost.
- You are about to drop the column `team_status` on the `Conversation` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "Chat" ADD COLUMN "team_id" INTEGER,
ADD COLUMN "team_name" TEXT,
ADD COLUMN "team_status" TEXT;

-- AlterTable
ALTER TABLE "Conversation" DROP COLUMN "is_question_from_agent",
DROP COLUMN "team_id",
DROP COLUMN "team_name",
DROP COLUMN "team_status";
67 changes: 26 additions & 41 deletions src/client/chatConversationHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,60 @@
import getAgentResponse from "@wasp/actions/getAgentResponse";
import addNewConversationToChat from "@wasp/actions/addNewConversationToChat";
import updateExistingConversation from "@wasp/actions/updateExistingConversation";
import updateExistingChat from "@wasp/actions/updateExistingChat";
import { prepareOpenAIRequest } from "./helpers";

export async function addUserMessageToConversation(
chat_id: number,
userQuery: string,
conv_id?: number,
team_name?: string,
team_id?: number
userQuery: string
) {
let userMessage = userQuery;
let isAnswerToAgentQuestion = false;
let user_answer_to_team_id = null;
if (team_id) {
if (conv_id) {
const payload = {
chat_id: chat_id,
conv_id: conv_id,
is_question_from_agent: false,
team_status: null,
};
await updateExistingConversation(payload);
}
userMessage = `<p>Replying to ${team_name}:</p><br/><br/>` + userQuery;
isAnswerToAgentQuestion = true;
user_answer_to_team_id = team_id;
}

const payload = {
chat_id: chat_id,
message: userMessage,
role: "user",
};

const updatedConversation: any = await addNewConversationToChat(payload);

const [messages, latestConversationID]: [
messages: any,
latestConversationID: number
] = prepareOpenAIRequest(updatedConversation);
return [
messages,
latestConversationID,
isAnswerToAgentQuestion,
user_answer_to_team_id,
];
const messages: any = prepareOpenAIRequest(updatedConversation);
return messages;
}

export async function addAgentMessageToConversation(
chat_id: number,
message: any,
conv_id: number,
isAnswerToAgentQuestion: boolean,
userResponseToTeamId: number | null | undefined
team_id: number | null | undefined
) {
const response: any = await getAgentResponse({
chat_id: chat_id,
message: message,
conv_id: conv_id,
isAnswerToAgentQuestion: isAnswerToAgentQuestion,
userResponseToTeamId: userResponseToTeamId,
team_id: team_id,
});

// ...(response.team_name && { team_name: response.team_name }),
// ...(response.team_id && { team_id: response.team_id }),
// ...(response.team_status && { team_status: response.team_status }),

if (response.team_name) {
const payload = {
chat_id: chat_id,
team_name: response.team_name,
team_id: response.team_id,
team_status: response.team_status,
};
await updateExistingChat(payload);
}

const openAIResponse = {
chat_id: Number(chat_id),
message: response.content,
role: "assistant",
};

await addNewConversationToChat(openAIResponse);

return {
chat_id: Number(chat_id),
...(response.team_name && { team_name: response.team_name }),
...(response.team_id && { team_id: response.team_id }),
...(response.team_status && { team_status: response.team_status }),
};
return await addNewConversationToChat(openAIResponse);
}
57 changes: 0 additions & 57 deletions src/client/components/ConversationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ import logo from "../static/captn-logo.png";

type ConversationsListProps = {
conversations: Conversation[];
onInlineFormSubmit: (
userQuery: string,
conv_id: number,
team_name: string,
team_id: number
) => void;
};

export default function ConversationsList({
conversations,
onInlineFormSubmit,
}: ConversationsListProps) {
return (
<div className="w-full">
Expand Down Expand Up @@ -62,18 +55,6 @@ export default function ConversationsList({
/>
);

const handleFormSubmit = (
event: React.FormEvent<HTMLFormElement>,
conv_id: number,
team_name: string,
team_id: number
) => {
event.preventDefault();
const target = event.target as HTMLFormElement;
const userQuery = target.userQuery.value;
target.reset();
onInlineFormSubmit(userQuery, conv_id, team_name, team_id);
};
return (
<div key={idx}>
<div
Expand All @@ -98,44 +79,6 @@ export default function ConversationsList({
<div className="chat-conversations text-base flex flex-col gap-2">
<Markdown>{conversation.message}</Markdown>
</div>
{conversation.is_question_from_agent && (
<form
key={conversation.id}
// onSubmit={handleFormSubmit}
onSubmit={(event) =>
handleFormSubmit(
event,
conversation.id,
conversation.team_name,
conversation.team_id
)
}
className="relative block w-full mt-[15px]"
>
<label
htmlFor="search"
className="mb-2 text-sm font-medium text-gray-900 sr-only dark:text-white"
>
Search
</label>
<div className="relative">
<input
type="search"
id="userQuery"
name="search"
className="block w-full p-4 pl-5 text-sm text-captn-dark-blue border border-gray-300 rounded-lg bg-gray-300 focus:ring-blue-500 focus:border-blue-500 placeholder-captn-dark-blue"
placeholder="Reply"
required
/>
<button
type="submit"
className="text-white absolute right-2.5 bottom-2.5 bg-captn-cta-green hover:bg-captn-cta-green-hover focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2"
>
Send
</button>
</div>
</form>
)}
</div>
</div>
</div>
Expand Down
63 changes: 14 additions & 49 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Redirect } from "react-router-dom";

import { useQuery } from "@wasp/queries";
import getConversations from "@wasp/queries/getConversations";
import getChat from "@wasp/queries/getChat";
import { useSocket, useSocketListener } from "@wasp/webSocket";

import ConversationsList from "./ConversationList";
Expand All @@ -22,6 +23,9 @@ export default function ConversationWrapper() {
const { socket, isConnected } = useSocket();
const [isLoading, setIsLoading] = useState(false);
const chatWindowRef = useRef(null);
const { data: currentChatDetails } = useQuery(getChat, {
chatId: Number(id),
});
const { data: conversations, refetch } = useQuery(
getConversations,
{
Expand All @@ -41,12 +45,7 @@ export default function ConversationWrapper() {
googleRedirectLoginTeamName &&
googleRedirectLoginTeadId
) {
await addMessagesToConversation(
googleRedirectLoginMsg,
undefined,
googleRedirectLoginTeamName,
googleRedirectLoginTeadId
);
await addMessagesToConversation(googleRedirectLoginMsg);
}
},
[
Expand Down Expand Up @@ -77,43 +76,21 @@ export default function ConversationWrapper() {
}
};

async function addMessagesToConversation(
userQuery: string,
conv_id?: number,
team_name?: string,
team_id?: number
) {
async function addMessagesToConversation(userQuery: string) {
try {
const [
messages,
conversation_id,
isAnswerToAgentQuestion,
user_answer_to_team_id,
] = await addUserMessageToConversation(
const messages = await addUserMessageToConversation(
Number(id),
userQuery,
conv_id,
team_name,
team_id
userQuery
);
setIsLoading(true);
const updatedConversations: any = await addAgentMessageToConversation(
const response: any = await addAgentMessageToConversation(
Number(id),
messages,
conversation_id,
isAnswerToAgentQuestion,
user_answer_to_team_id
// @ts-ignore
currentChatDetails.team_id
);

const lastConversation =
updatedConversations[updatedConversations.length - 1];
if (lastConversation.team_status === "inprogress") {
socket.emit(
"newConversationAdded",
lastConversation.team_id,
lastConversation.id,
lastConversation.chatId
);
if (response.team_status === "inprogress") {
socket.emit("newConversationAdded", response.chat_id);
}

setIsLoading(false);
Expand All @@ -132,15 +109,6 @@ export default function ConversationWrapper() {
await addMessagesToConversation(userQuery);
};

const handleInlineFormSubmit = async (
userQuery: string,
conv_id: number,
team_name: string,
team_id: number
) => {
await addMessagesToConversation(userQuery, conv_id, team_name, team_id);
};

const chatContainerClass = `flex h-full flex-col items-center justify-between pb-24 overflow-y-auto bg-captn-light-blue ${
isLoading ? "opacity-40" : "opacity-100"
}`;
Expand All @@ -165,10 +133,7 @@ export default function ConversationWrapper() {
style={{ height: "85%" }}
>
{conversations && (
<ConversationsList
conversations={conversations}
onInlineFormSubmit={handleInlineFormSubmit}
/>
<ConversationsList conversations={conversations} />
)}
</div>
{isLoading && <Loader />}
Expand Down
7 changes: 2 additions & 5 deletions src/client/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ function getLatestConversationID(input: InputMessage[]): number {
return latestConversationID;
}

export function prepareOpenAIRequest(
input: InputMessage[]
): [OutputMessage[], number] {
export function prepareOpenAIRequest(input: InputMessage[]): OutputMessage[] {
const messages: OutputMessage[] = input.map((message) => {
return {
role: message.role,
content: message.message,
};
});
const latestConversationID = getLatestConversationID(input);
return [messages, latestConversationID];
return messages;
}

// A custom hook that builds on useLocation to parse
Expand Down
Loading

0 comments on commit ce5d872

Please sign in to comment.