Skip to content

Commit

Permalink
Create single team per chat (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj authored Dec 19, 2023
1 parent 53157d3 commit ea216f5
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 249 deletions.
19 changes: 12 additions & 7 deletions main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ entity Chat {=psl
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team_id Int?
team_name String?
team_status String?
showLoader Boolean @default(false)
user User? @relation(fields: [userId], references: [id])
userId Int?
name String?
Expand All @@ -133,10 +137,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 +227,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 +259,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";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Chat" ADD COLUMN "showLoader" BOOLEAN NOT NULL DEFAULT false;
75 changes: 31 additions & 44 deletions src/client/chatConversationHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,62 @@
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,
});

const openAIResponse = {
// ...(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);
}

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

await addNewConversationToChat(openAIResponse);
}

return {
chat_id: Number(chat_id),
message: response.content,
role: "assistant",
...(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);
}
96 changes: 38 additions & 58 deletions src/client/components/ConversationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import { useState } from "react";
import Markdown from "markdown-to-jsx";

import type { Conversation } from "@wasp/entities";

import logo from "../static/captn-logo.png";

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

export default function ConversationsList({
conversations,
onInlineFormSubmit,
isLoading,
}: ConversationsListProps) {
return (
<div className="w-full">
Expand Down Expand Up @@ -62,18 +56,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,49 +80,47 @@ 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>
);
})}
<div
className={`flex items-center px-5 py-2 group bg-captn-light-cream flex-col ${
isLoading ? "" : "hidden"
}`}
style={{ minHeight: "85px" }}
>
<div
className="relative ml-3 block w-full p-4 pl-10 text-sm text-captn-dark-blue border-captn-light-cream rounded-lg bg-captn-light-cream "
style={{ maxWidth: "840px", margin: "auto" }}
>
<span
className="absolute inline-block"
style={{
left: "-15px",
top: "6px",
height: " 45px",
width: "45px",
}}
>
<img
alt="captn logo"
src={logo}
className="w-full h-full"
style={{ borderRadius: "50%" }}
/>
</span>
<div className="chat-conversations text-base flex flex-col gap-2">
<span>
I am presently navigating the waters of your request.
<br />
Kindly stay anchored, and I will promptly return to you once I
have information to share.
</span>
</div>
</div>
</div>
</div>
);
}
Loading

0 comments on commit ea216f5

Please sign in to comment.