Skip to content

Commit

Permalink
Dynamic delay
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio committed Dec 8, 2023
1 parent 7f94d46 commit 2392ee4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/Components/AstroChat/useAstro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { asyncSleep } from '../../utils/Async';
import Config from '../../Config';
import { MessageProcessor } from '../Message/MessageProcessor';
import { Command } from '../../types/Command';
import { DelayedResponseConfig, getDelayedResponse } from '../../utils/DelayedResponse';

type SetMessages = Dispatch<SetStateAction<Array<Message>>>;

const loadMessage = async (
from: From.ASSISTANT | From.FEEDBACK,
content: Promise<PostTalkResponse> | PostTalkResponse | string | undefined,
setMessages: SetMessages,
minTimeout: number,
delayedResponseConfig: DelayedResponseConfig,
processors: Array<MessageProcessor>
) => {
setMessages(
Expand All @@ -28,15 +29,16 @@ const loadMessage = async (

const startTime = new Date().getTime();
const resolvedContent = await content;

const endTime = new Date().getTime();
const remainingTime = Math.max(minTimeout - endTime + startTime, 0);

await asyncSleep(remainingTime);

if (resolvedContent !== undefined) {
const contentString = typeof resolvedContent === 'string' ? resolvedContent : resolvedContent.text;

const waitTime = getDelayedResponse(contentString, delayedResponseConfig);
const remainingTime = Math.max(waitTime - endTime + startTime, 0);

await asyncSleep(remainingTime);

const message: AssistantMessage | FeedbackMessage = {
from,
isLoading: false,
Expand Down Expand Up @@ -70,6 +72,7 @@ const loadMessage = async (
})
);
} else {
await asyncSleep(delayedResponseConfig.delay.min);
setMessages(
produce((draft) => {
draft.pop();
Expand Down Expand Up @@ -136,14 +139,14 @@ export const useAstro = (messageProcessors: Array<MessageProcessor>) => {
From.ASSISTANT,
postTalkResponse.then((r) => r[0]),
setMessages,
Config.messages.delays.minAssistantResponse,
Config.messages.delays.assistantDelayResponse,
messageProcessors
);

// responses has already been resolved
const responses = await postTalkResponse;
for (let i = 1; i < responses.length; i++) {
await loadMessage(From.ASSISTANT, responses[i], setMessages, Config.messages.delays.minAssistantResponse, messageProcessors);
await loadMessage(From.ASSISTANT, responses[i], setMessages, Config.messages.delays.assistantDelayResponse, messageProcessors);
}
};

Expand Down
14 changes: 13 additions & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ const config = {
messages: {
delays: {
// Artificial delays (ms) to avoid overwhelming the user
minAssistantResponse: 2000,
assistantDelayResponse: {
delay: {
min: 500,
max: 2000,
},
words: {
max: 15,
min: 5,
},
},
minAssistantDelayResponse: 500,
maxAssi: 2000,
maxTextLength: 100,
feedback: 1000,
},
},
Expand Down
21 changes: 21 additions & 0 deletions src/utils/DelayedResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { lerp1d } from './Interpolation';

export interface DelayedResponseConfig {
delay: {
min: number;
max: number;
};
words: {
min: number;
max: number;
};
}

export const getDelayedResponse = (message: string | undefined, config: DelayedResponseConfig) => {
if (!message) {
return config.delay.min;
}

const words = Math.min(config.words.max, Math.max(config.words.min, message.split(' ').length));
return lerp1d(config.delay.min, config.delay.max, (words - config.words.min) / (config.words.max - config.words.min));
};
4 changes: 4 additions & 0 deletions src/utils/Interpolation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Linear interpolation
export const lerp1d = (min: number, max: number, t: number) => {
return min + t * (max - min);
};

0 comments on commit 2392ee4

Please sign in to comment.