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

feat: Simulate discord typing while generating a response #1712

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,16 @@ export class MessageManager {
discordMessageHandlerTemplate,
});

// simulate discord typing while generating a response
const stopTyping = this.simulateTyping(message)

const responseContent = await this._generateResponse(
memory,
state,
context
);
).finally(() => {
stopTyping()
});

responseContent.text = responseContent.text?.trim();
responseContent.inReplyTo = stringToUuid(
Expand Down Expand Up @@ -1307,4 +1312,27 @@ export class MessageManager {
const data = await response.json();
return data.username;
}

/**
* Simulate discord typing while generating a response;
* returns a function to interrupt the typing loop
*
* @param message
*/
private simulateTyping(message: DiscordMessage) {
let typing = true;

const typingLoop = async () => {
while (typing) {
await message.channel.sendTyping();
await new Promise((resolve) => setTimeout(resolve, 3000));
}
};

typingLoop();

return function stopTyping() {
typing = false
}
}
}
Loading