refactor: consolidate typing lifecycle and queue policy

This commit is contained in:
Peter Steinberger
2026-02-25 02:15:54 +00:00
parent 24d7612ddf
commit d42ef2ac62
19 changed files with 410 additions and 301 deletions

View File

@@ -1,3 +1,4 @@
import { createTypingKeepaliveLoop } from "../../channels/typing-lifecycle.js";
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../tokens.js";
export type TypingController = {
@@ -35,7 +36,6 @@ export function createTypingController(params: {
// especially when upstream event emitters don't await async listeners.
// Once we stop typing, we "seal" the controller so late events can't restart typing forever.
let sealed = false;
let typingTimer: NodeJS.Timeout | undefined;
let typingTtlTimer: NodeJS.Timeout | undefined;
const typingIntervalMs = typingIntervalSeconds * 1000;
@@ -61,10 +61,7 @@ export function createTypingController(params: {
clearTimeout(typingTtlTimer);
typingTtlTimer = undefined;
}
if (typingTimer) {
clearInterval(typingTimer);
typingTimer = undefined;
}
typingLoop.stop();
// Notify the channel to stop its typing indicator (e.g., on NO_REPLY).
// This fires only once (sealed prevents re-entry).
if (active) {
@@ -88,7 +85,7 @@ export function createTypingController(params: {
clearTimeout(typingTtlTimer);
}
typingTtlTimer = setTimeout(() => {
if (!typingTimer) {
if (!typingLoop.isRunning()) {
return;
}
log?.(`typing TTL reached (${formatTypingTtl(typingTtlMs)}); stopping typing indicator`);
@@ -105,6 +102,11 @@ export function createTypingController(params: {
await onReplyStart?.();
};
const typingLoop = createTypingKeepaliveLoop({
intervalMs: typingIntervalMs,
onTick: triggerTyping,
});
const ensureStart = async () => {
if (sealed) {
return;
@@ -146,16 +148,11 @@ export function createTypingController(params: {
if (!onReplyStart) {
return;
}
if (typingIntervalMs <= 0) {
return;
}
if (typingTimer) {
if (typingLoop.isRunning()) {
return;
}
await ensureStart();
typingTimer = setInterval(() => {
void triggerTyping();
}, typingIntervalMs);
typingLoop.start();
};
const startTypingOnText = async (text?: string) => {