Repeatedly send typing indicator while executing/generating response
fixes #10
This commit is contained in:
parent
d9a97cce8d
commit
db8628d425
1 changed files with 33 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
import DiscordApi, { GuildTextBasedChannel } from "discord.js";
|
import DiscordApi, { GuildTextBasedChannel, TextBasedChannel } from "discord.js";
|
||||||
import { ChatCompletionRequestMessage, ChatCompletionResponseMessage } from "openai";
|
import { ChatCompletionRequestMessage, ChatCompletionResponseMessage } from "openai";
|
||||||
import Axios from "axios";
|
import Axios from "axios";
|
||||||
|
|
||||||
|
@ -14,6 +14,33 @@ export type RequestMessage = apiRequest & NonNullableInObject<apiRequest, "chann
|
||||||
|
|
||||||
class ChannelsRunningValue extends Array<RequestMessage> {
|
class ChannelsRunningValue extends Array<RequestMessage> {
|
||||||
tries = 0;
|
tries = 0;
|
||||||
|
channel: TextBasedChannel;
|
||||||
|
|
||||||
|
private typingWorker = setInterval(() => this.sendTyping(), 5000).unref();
|
||||||
|
|
||||||
|
private typingStopper = setTimeout(() => this.stopTyping(), 60000).unref();
|
||||||
|
|
||||||
|
constructor(channel: TextBasedChannel) {
|
||||||
|
super();
|
||||||
|
this.channel = channel;
|
||||||
|
clearInterval(this.typingStopper);
|
||||||
|
clearTimeout(this.typingStopper);
|
||||||
|
}
|
||||||
|
|
||||||
|
startTyping() {
|
||||||
|
this.sendTyping();
|
||||||
|
this.typingWorker.refresh();
|
||||||
|
this.typingStopper.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
stopTyping() {
|
||||||
|
clearInterval(this.typingWorker);
|
||||||
|
clearTimeout(this.typingStopper);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTyping() {
|
||||||
|
this.channel.sendTyping().catch(() => {/* GRACEFAIL: we fail to send the typing then */});
|
||||||
|
}
|
||||||
|
|
||||||
shift() {
|
shift() {
|
||||||
this.tries = 0;
|
this.tries = 0;
|
||||||
|
@ -177,7 +204,7 @@ export async function queueRequest(request: apiRequest) {
|
||||||
|
|
||||||
const messagesForChannel = channelsRunning.ensure(
|
const messagesForChannel = channelsRunning.ensure(
|
||||||
request.channelId,
|
request.channelId,
|
||||||
() => { return new ChannelsRunningValue; },
|
() => { return new ChannelsRunningValue(request.channel as TextBasedChannel); },
|
||||||
);
|
);
|
||||||
const shouldStart = messagesForChannel.length === 0;
|
const shouldStart = messagesForChannel.length === 0;
|
||||||
messagesForChannel.push(request as RequestMessage);
|
messagesForChannel.push(request as RequestMessage);
|
||||||
|
@ -241,7 +268,7 @@ async function executeFromQueue(channel: string) {
|
||||||
messages.forEach(m => { Moderation.checkMessageNoReturn(m); });
|
messages.forEach(m => { Moderation.checkMessageNoReturn(m); });
|
||||||
|
|
||||||
if (message instanceof DiscordApi.Message) {
|
if (message instanceof DiscordApi.Message) {
|
||||||
await message.channel.sendTyping();
|
channelQueue.startTyping();
|
||||||
}
|
}
|
||||||
else if (message.isRepliable()) {
|
else if (message.isRepliable()) {
|
||||||
await message.deferReply();
|
await message.deferReply();
|
||||||
|
@ -274,6 +301,8 @@ async function executeFromQueue(channel: string) {
|
||||||
}
|
}
|
||||||
} while (generatedMessage.function_call);
|
} while (generatedMessage.function_call);
|
||||||
|
|
||||||
|
channelQueue.stopTyping();
|
||||||
|
|
||||||
const answerContent = answer.data.choices[0].message?.content;
|
const answerContent = answer.data.choices[0].message?.content;
|
||||||
|
|
||||||
if (answerContent === undefined || answerContent === "") {
|
if (answerContent === undefined || answerContent === "") {
|
||||||
|
@ -297,6 +326,7 @@ async function executeFromQueue(channel: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
channelQueue.stopTyping();
|
||||||
console.error(`Error ocurred while handling chat completion request (${(e as object).constructor.name}):`);
|
console.error(`Error ocurred while handling chat completion request (${(e as object).constructor.name}):`);
|
||||||
console.error(e);
|
console.error(e);
|
||||||
if (OpenAImessages.length !== 0) {
|
if (OpenAImessages.length !== 0) {
|
||||||
|
|
Loading…
Reference in a new issue