Skip to content

Commit

Permalink
Add functionality to respond to agent questions
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Nov 27, 2023
1 parent 03c71ee commit 1411068
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
18 changes: 7 additions & 11 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,27 @@ export default function ConversationWrapper() {
try {
// 1. add new conversation to table
const payload = {
// @ts-ignore
conversation_id: conversations.id,
conversations: [
// Todo: remove the below ignore comment
// @ts-ignore
...conversations.conversation,
...[{ role: "user", content: userQuery }],
],
conversations: [{ role: "user", content: userQuery }],
};

await updateConversation(payload);
const updatedConversation = await updateConversation(payload);
// 2. call backend python server to get agent response
setIsLoading(true);
const response = await getAgentResponse({
message: payload.conversations,
message: updatedConversation["conversation"],
conv_id: payload.conversation_id,
// @ts-ignore
is_answer_to_agent_question: conversations.status === "pause",
});
// 3. add agent response as new conversation in the table
const openAIResponse = {
// @ts-ignore
conversation_id: conversations.id,
conversations: [
...payload.conversations,
// Todo: remove the below ignore comment
// @ts-ignore
...[{ role: "assistant", content: response.content }],
{ role: "assistant", content: response.content },
],
// @ts-ignore
...(response.team_status && { status: response.team_status }),
Expand Down
34 changes: 33 additions & 1 deletion src/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,49 @@ type UpdateConversationPayload = {
status?: string;
};

type ConversationItem = {
role: string;
content: string;
};

function convertConversationList(
currentConversation: Conversation
): Array<ConversationItem> {
const conversationList: Array<any> = Object.entries(
// @ts-ignore
currentConversation.conversation
);
return conversationList.map((item) => item[1]);
}

export const updateConversation: UpdateConversation<
UpdateConversationPayload,
Conversation
> = async (args, context) => {
if (!context.user) {
throw new HttpError(401);
}
const currentConversation =
await context.entities.Conversation.findFirstOrThrow({
where: { id: args.conversation_id },
});

let currentConversationList = convertConversationList(currentConversation);
const existingRole =
currentConversationList[currentConversationList.length - 1]["role"];
const openAIResponseRole = args.conversations[0]["role"];

if (!(existingRole === "assistant" && existingRole === openAIResponseRole)) {
currentConversationList = [
...currentConversationList,
...args.conversations,
];
}

return context.entities.Conversation.update({
where: { id: args.conversation_id },
data: {
conversation: args.conversations,
conversation: currentConversationList,
...(args.status && { status: args.status }),
},
});
Expand Down
1 change: 0 additions & 1 deletion src/server/webSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const webSocketFn = (io, context) => {

// Check for updates every 3 seconds
const updateInterval = setInterval(async () => {
console.log("Checking database for inprogress tasks");
const conversations = await context.entities.Conversation.findMany({
where: { userId: socket.data.user.id, status: "inprogress" },
});
Expand Down

0 comments on commit 1411068

Please sign in to comment.