Skip to content

Commit

Permalink
Merge pull request #354 from massi-ang/fix_duplicates
Browse files Browse the repository at this point in the history
Fix duplicates when starting multiple sessions for the same user
  • Loading branch information
bigadsoleiman authored Feb 2, 2024
2 parents c1ef47d + a3b03d4 commit 1079079
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cli/magic-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async function processCreateOptions(options: any): Promise<void> {
},
];
const answers: any = await enquirer.prompt(questions);
const kendraExternal = [];
const kendraExternal: any[] = [];
let newKendra = answers.enableRag && answers.kendra;
const existingKendraIndices = Array.from(options.kendraExternal || []);
while (newKendra === true) {
Expand Down
7 changes: 6 additions & 1 deletion lib/chatbot-api/functions/resolvers/subscribe-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export function request(ctx) {
}

export function response(ctx) {
const filter = { and: [{ userId: { eq: ctx.identity.sub } }] };
const filter = {
and: [
{ userId: { eq: ctx.identity.sub } },
{ sessionId: { eq: ctx.args.sessionId } },
],
};
extensions.setSubscriptionFilter(util.transform.toSubscriptionFilter(filter));
return null;
}
61 changes: 26 additions & 35 deletions lib/user-interface/react-app/src/components/chatbot/multi-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ChatSession {
loading: boolean;
running: boolean;
messageHistory: ChatBotHistoryItem[];
subscription?: Promise<ZenObservable.Subscription>;
subscription?: ZenObservable.Subscription;
}

function createNewSession(): ChatSession {
Expand Down Expand Up @@ -153,7 +153,7 @@ export default function MultiChat() {
return () => {
refChatSessions.current.forEach((session) => {
console.log(`Unsubscribing from ${session.id}`);
session.subscription?.then((s) => s.unsubscribe());
session.subscription?.unsubscribe();
});
refChatSessions.current = [];
};
Expand Down Expand Up @@ -226,11 +226,9 @@ export default function MultiChat() {
});
};

async function subscribe(sessionId: string) {
function subscribe(sessionId: string): ZenObservable.Subscription {
console.log("Subscribing to AppSync");
const sub = await API.graphql<
GraphQLSubscription<ReceiveMessagesSubscription>
>({
const sub = API.graphql<GraphQLSubscription<ReceiveMessagesSubscription>>({
query: receiveMessages,
variables: {
sessionId: sessionId,
Expand All @@ -241,7 +239,7 @@ export default function MultiChat() {
const data = value.data!.receiveMessages?.data;
if (data !== undefined && data !== null) {
const response: ChatBotMessageResponse = JSON.parse(data);
console.log(response);
console.log(JSON.stringify(response));
if (response.action === ChatBotAction.Heartbeat) {
console.log("Heartbeat pong!");
return;
Expand Down Expand Up @@ -275,33 +273,28 @@ export default function MultiChat() {
}

const session = createNewSession();

const sub = subscribe(session.id);
sub
.then(() => {
console.log(`Subscribed to session ${session.id}}`);
const request: ChatBotHeartbeatRequest = {
action: ChatBotAction.Heartbeat,
modelInterface: ChatBotModelInterface.Langchain,
data: {
sessionId: session.id,
},
};
const result = API.graphql({
query: sendQuery,
variables: {
data: JSON.stringify(request),
},
});
console.log(result);
})
.catch((err) => {
console.log(err);
});

console.log(`Subscribed to session ${session.id}}`);
const request: ChatBotHeartbeatRequest = {
action: ChatBotAction.Heartbeat,
modelInterface: ChatBotModelInterface.Langchain,
data: {
sessionId: session.id,
},
};
API.graphql({
query: sendQuery,
variables: {
data: JSON.stringify(request),
},
});
session.subscription = sub;
refChatSessions.current.push(session);
console.log(refChatSessions);
console.log(
"Sessions",
refChatSessions.current.map((s) => s.id)
);
setChatSessions([...refChatSessions.current]);
}

Expand Down Expand Up @@ -369,7 +362,7 @@ export default function MultiChat() {
<Button
onClick={() => {
refChatSessions.current.forEach((s) => {
s.subscription?.then((s) => s.unsubscribe());
s.subscription?.unsubscribe();
s.messageHistory = [];
s.id = uuidv4();
s.subscription = subscribe(s.id);
Expand Down Expand Up @@ -432,10 +425,8 @@ export default function MultiChat() {
onClick={() => {
refChatSessions.current
.filter((c) => c.id == chatSession.id)[0]
.subscription?.then((s) => {
console.log(`Unsubscribe from ${chatSession.id}`);
s.unsubscribe();
});
.subscription?.unsubscribe();
console.log(`Unsubscribe from ${chatSession.id}`);
refChatSessions.current =
refChatSessions.current.filter(
(c) => c.id !== chatSession.id
Expand Down

0 comments on commit 1079079

Please sign in to comment.