mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 02:30:24 +00:00
refactor(channels): share draft stream loop across slack and telegram
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Bot } from "grammy";
|
||||
import { createDraftStreamLoop } from "../channels/draft-stream-loop.js";
|
||||
import { buildTelegramThreadParams, type TelegramThreadSpec } from "./bot/helpers.js";
|
||||
|
||||
const TELEGRAM_STREAM_MAX_CHARS = 4096;
|
||||
@@ -38,10 +39,6 @@ export function createTelegramDraftStream(params: {
|
||||
|
||||
let streamMessageId: number | undefined;
|
||||
let lastSentText = "";
|
||||
let lastSentAt = 0;
|
||||
let pendingText = "";
|
||||
let inFlightPromise: Promise<void> | undefined;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let stopped = false;
|
||||
|
||||
const sendOrEditStreamMessage = async (text: string) => {
|
||||
@@ -65,7 +62,6 @@ export function createTelegramDraftStream(params: {
|
||||
return;
|
||||
}
|
||||
lastSentText = trimmed;
|
||||
lastSentAt = Date.now();
|
||||
try {
|
||||
if (typeof streamMessageId === "number") {
|
||||
await params.api.editMessageText(chatId, streamMessageId, trimmed);
|
||||
@@ -86,47 +82,15 @@ export function createTelegramDraftStream(params: {
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const flush = async () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
while (!stopped) {
|
||||
if (inFlightPromise) {
|
||||
await inFlightPromise;
|
||||
continue;
|
||||
}
|
||||
const text = pendingText;
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed) {
|
||||
pendingText = "";
|
||||
return;
|
||||
}
|
||||
pendingText = "";
|
||||
const current = sendOrEditStreamMessage(text).finally(() => {
|
||||
if (inFlightPromise === current) {
|
||||
inFlightPromise = undefined;
|
||||
}
|
||||
});
|
||||
inFlightPromise = current;
|
||||
await current;
|
||||
if (!pendingText) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
const loop = createDraftStreamLoop({
|
||||
throttleMs,
|
||||
isStopped: () => stopped,
|
||||
sendOrEditStreamMessage,
|
||||
});
|
||||
|
||||
const clear = async () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
pendingText = "";
|
||||
stopped = true;
|
||||
if (inFlightPromise) {
|
||||
await inFlightPromise;
|
||||
}
|
||||
stop();
|
||||
await loop.waitForInFlight();
|
||||
const messageId = streamMessageId;
|
||||
streamMessageId = undefined;
|
||||
if (typeof messageId !== "number") {
|
||||
@@ -141,52 +105,22 @@ export function createTelegramDraftStream(params: {
|
||||
}
|
||||
};
|
||||
|
||||
const schedule = () => {
|
||||
if (timer) {
|
||||
return;
|
||||
}
|
||||
const delay = Math.max(0, throttleMs - (Date.now() - lastSentAt));
|
||||
timer = setTimeout(() => {
|
||||
void flush();
|
||||
}, delay);
|
||||
};
|
||||
|
||||
const update = (text: string) => {
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
pendingText = text;
|
||||
if (inFlightPromise) {
|
||||
schedule();
|
||||
return;
|
||||
}
|
||||
if (!timer && Date.now() - lastSentAt >= throttleMs) {
|
||||
void flush();
|
||||
return;
|
||||
}
|
||||
schedule();
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
stopped = true;
|
||||
pendingText = "";
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
loop.stop();
|
||||
};
|
||||
|
||||
const forceNewMessage = () => {
|
||||
streamMessageId = undefined;
|
||||
lastSentText = "";
|
||||
pendingText = "";
|
||||
loop.resetPending();
|
||||
};
|
||||
|
||||
params.log?.(`telegram stream preview ready (maxChars=${maxChars}, throttleMs=${throttleMs})`);
|
||||
|
||||
return {
|
||||
update,
|
||||
flush,
|
||||
update: loop.update,
|
||||
flush: loop.flush,
|
||||
messageId: () => streamMessageId,
|
||||
clear,
|
||||
stop,
|
||||
|
||||
Reference in New Issue
Block a user