fix: stop typing after dispatcher idle
This commit is contained in:
@@ -3,6 +3,8 @@ export type TypingController = {
|
||||
startTypingLoop: () => Promise<void>;
|
||||
startTypingOnText: (text?: string) => Promise<void>;
|
||||
refreshTypingTtl: () => void;
|
||||
markRunComplete: () => void;
|
||||
markDispatchIdle: () => void;
|
||||
cleanup: () => void;
|
||||
};
|
||||
|
||||
@@ -21,6 +23,9 @@ export function createTypingController(params: {
|
||||
log,
|
||||
} = params;
|
||||
let started = false;
|
||||
let active = false;
|
||||
let runComplete = false;
|
||||
let dispatchIdle = false;
|
||||
let typingTimer: NodeJS.Timeout | undefined;
|
||||
let typingTtlTimer: NodeJS.Timeout | undefined;
|
||||
const typingIntervalMs = typingIntervalSeconds * 1000;
|
||||
@@ -30,6 +35,13 @@ export function createTypingController(params: {
|
||||
return `${Math.round(ms / 1000)}s`;
|
||||
};
|
||||
|
||||
const resetCycle = () => {
|
||||
started = false;
|
||||
active = false;
|
||||
runComplete = false;
|
||||
dispatchIdle = false;
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
if (typingTtlTimer) {
|
||||
clearTimeout(typingTtlTimer);
|
||||
@@ -39,6 +51,7 @@ export function createTypingController(params: {
|
||||
clearInterval(typingTimer);
|
||||
typingTimer = undefined;
|
||||
}
|
||||
resetCycle();
|
||||
};
|
||||
|
||||
const refreshTypingTtl = () => {
|
||||
@@ -61,11 +74,22 @@ export function createTypingController(params: {
|
||||
};
|
||||
|
||||
const ensureStart = async () => {
|
||||
if (!active) {
|
||||
active = true;
|
||||
runComplete = false;
|
||||
dispatchIdle = false;
|
||||
}
|
||||
if (started) return;
|
||||
started = true;
|
||||
await triggerTyping();
|
||||
};
|
||||
|
||||
const maybeStopOnIdle = () => {
|
||||
if (!active) return;
|
||||
// Stop only when the model run is done and the dispatcher queue is empty.
|
||||
if (runComplete && dispatchIdle) cleanup();
|
||||
};
|
||||
|
||||
const startTypingLoop = async () => {
|
||||
if (!onReplyStart) return;
|
||||
if (typingIntervalMs <= 0) return;
|
||||
@@ -85,11 +109,23 @@ export function createTypingController(params: {
|
||||
await startTypingLoop();
|
||||
};
|
||||
|
||||
const markRunComplete = () => {
|
||||
runComplete = true;
|
||||
maybeStopOnIdle();
|
||||
};
|
||||
|
||||
const markDispatchIdle = () => {
|
||||
dispatchIdle = true;
|
||||
maybeStopOnIdle();
|
||||
};
|
||||
|
||||
return {
|
||||
onReplyStart: ensureStart,
|
||||
startTypingLoop,
|
||||
startTypingOnText,
|
||||
refreshTypingTtl,
|
||||
markRunComplete,
|
||||
markDispatchIdle,
|
||||
cleanup,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user