Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send user response to agent #43

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,25 @@ 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,
conv_id: payload.conversation_id,
message: updatedConversation["conversation"],
conv_id: updatedConversation.id,
// @ts-ignore
is_answer_to_agent_question: updatedConversation.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 }],
],
Expand All @@ -134,7 +131,7 @@ export default function ConversationWrapper() {
// Todo: remove the below ignore comment
// @ts-ignore
target.reset();
callAgent(userQuery);
await callAgent(userQuery);
};

if (isConversationLoading && !!id) return <Loader />;
Expand Down
38 changes: 36 additions & 2 deletions 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 All @@ -223,10 +255,11 @@ export const updateConversation: UpdateConversation<
type AgentPayload = {
message: any;
conv_id: number;
is_answer_to_agent_question?: boolean;
};

export const getAgentResponse: GetAgentResponse<AgentPayload> = async (
{ message, conv_id },
{ message, conv_id, is_answer_to_agent_question },
context
) => {
if (!context.user) {
Expand All @@ -237,6 +270,7 @@ export const getAgentResponse: GetAgentResponse<AgentPayload> = async (
message: message,
conv_id: conv_id,
user_id: context.user.id,
is_answer_to_agent_question: is_answer_to_agent_question,
};
console.log("===========");
console.log("Payload to Python server");
Expand Down
6 changes: 4 additions & 2 deletions 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 Expand Up @@ -43,7 +42,10 @@ export const webSocketFn = (io, context) => {
}

const conversation_status = json["status"];
if (conversation_status === "ready") {
if (
conversation_status === "completed" ||
conversation_status === "pause"
) {
const updated_conversation = conversation.conversation.concat([
{ role: "assistant", content: json["msg"] },
]);
Expand Down
Loading