Files
openclaw/src/auto-reply/reply/queue/drain.ts

144 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-01-14 01:08:15 +00:00
import { defaultRuntime } from "../../../runtime.js";
import {
buildCollectPrompt,
clearQueueSummaryState,
drainNextQueueItem,
hasCrossChannelItems,
previewQueueSummaryPrompt,
waitForQueueDebounce,
} from "../../../utils/queue-helpers.js";
2026-01-14 01:08:15 +00:00
import { isRoutableChannel } from "../route-reply.js";
import { FOLLOWUP_QUEUES } from "./state.js";
import type { FollowupRun } from "./types.js";
2026-01-14 01:08:15 +00:00
export function scheduleFollowupDrain(
key: string,
runFollowup: (run: FollowupRun) => Promise<void>,
): void {
const queue = FOLLOWUP_QUEUES.get(key);
if (!queue || queue.draining) {
return;
}
2026-01-14 01:08:15 +00:00
queue.draining = true;
void (async () => {
try {
let forceIndividualCollect = false;
while (queue.items.length > 0 || queue.droppedCount > 0) {
await waitForQueueDebounce(queue);
if (queue.mode === "collect") {
// Once the batch is mixed, never collect again within this drain.
// Prevents “collect after shift” collapsing different targets.
//
// Debug: `pnpm test src/auto-reply/reply/queue.collect-routing.test.ts`
if (forceIndividualCollect) {
if (!(await drainNextQueueItem(queue.items, runFollowup))) {
break;
}
2026-01-14 01:08:15 +00:00
continue;
}
// Check if messages span multiple channels.
// If so, process individually to preserve per-message routing.
const isCrossChannel = hasCrossChannelItems(queue.items, (item) => {
const channel = item.originatingChannel;
const to = item.originatingTo;
const accountId = item.originatingAccountId;
const threadId = item.originatingThreadId;
if (!channel && !to && !accountId && threadId == null) {
return {};
}
if (!isRoutableChannel(channel) || !to) {
return { cross: true };
}
const threadKey = threadId != null ? String(threadId) : "";
return {
key: [channel, to, accountId || "", threadKey].join("|"),
};
});
2026-01-14 01:08:15 +00:00
if (isCrossChannel) {
forceIndividualCollect = true;
if (!(await drainNextQueueItem(queue.items, runFollowup))) {
break;
}
2026-01-14 01:08:15 +00:00
continue;
}
const items = queue.items.slice();
const summary = previewQueueSummaryPrompt({ state: queue, noun: "message" });
2026-01-14 01:08:15 +00:00
const run = items.at(-1)?.run ?? queue.lastRun;
if (!run) {
break;
}
2026-01-14 01:08:15 +00:00
// Preserve originating channel from items when collecting same-channel.
const originatingChannel = items.find((i) => i.originatingChannel)?.originatingChannel;
const originatingTo = items.find((i) => i.originatingTo)?.originatingTo;
2026-01-14 01:08:15 +00:00
const originatingAccountId = items.find(
(i) => i.originatingAccountId,
)?.originatingAccountId;
const originatingThreadId = items.find(
(i) => i.originatingThreadId != null,
2026-01-14 01:08:15 +00:00
)?.originatingThreadId;
const prompt = buildCollectPrompt({
title: "[Queued messages while agent was busy]",
items,
summary,
renderItem: (item, idx) => `---\nQueued #${idx + 1}\n${item.prompt}`.trim(),
});
2026-01-14 01:08:15 +00:00
await runFollowup({
prompt,
run,
enqueuedAt: Date.now(),
originatingChannel,
originatingTo,
originatingAccountId,
originatingThreadId,
});
queue.items.splice(0, items.length);
if (summary) {
clearQueueSummaryState(queue);
}
2026-01-14 01:08:15 +00:00
continue;
}
const summaryPrompt = previewQueueSummaryPrompt({ state: queue, noun: "message" });
2026-01-14 01:08:15 +00:00
if (summaryPrompt) {
const run = queue.lastRun;
if (!run) {
break;
}
if (
!(await drainNextQueueItem(queue.items, async () => {
await runFollowup({
prompt: summaryPrompt,
run,
enqueuedAt: Date.now(),
});
}))
) {
break;
}
clearQueueSummaryState(queue);
2026-01-14 01:08:15 +00:00
continue;
}
if (!(await drainNextQueueItem(queue.items, runFollowup))) {
break;
}
2026-01-14 01:08:15 +00:00
}
} catch (err) {
queue.lastEnqueuedAt = Date.now();
defaultRuntime.error?.(`followup queue drain failed for ${key}: ${String(err)}`);
2026-01-14 01:08:15 +00:00
} finally {
queue.draining = false;
if (queue.items.length === 0 && queue.droppedCount === 0) {
FOLLOWUP_QUEUES.delete(key);
} else {
scheduleFollowupDrain(key, runFollowup);
}
}
})();
}