refactor: unify inbound debounce policy and split gateway/models helpers

This commit is contained in:
Peter Steinberger
2026-03-03 00:54:28 +00:00
parent 7de4204e57
commit 47083460ea
13 changed files with 415 additions and 192 deletions

View File

@@ -0,0 +1,61 @@
import { describe, expect, it, vi } from "vitest";
import {
createChannelInboundDebouncer,
shouldDebounceTextInbound,
} from "./inbound-debounce-policy.js";
describe("shouldDebounceTextInbound", () => {
it("rejects blank text, media, and control commands", () => {
const cfg = {} as Parameters<typeof shouldDebounceTextInbound>[0]["cfg"];
expect(shouldDebounceTextInbound({ text: " ", cfg })).toBe(false);
expect(shouldDebounceTextInbound({ text: "hello", cfg, hasMedia: true })).toBe(false);
expect(shouldDebounceTextInbound({ text: "/status", cfg })).toBe(false);
});
it("accepts normal text when debounce is allowed", () => {
const cfg = {} as Parameters<typeof shouldDebounceTextInbound>[0]["cfg"];
expect(shouldDebounceTextInbound({ text: "hello there", cfg })).toBe(true);
expect(shouldDebounceTextInbound({ text: "hello there", cfg, allowDebounce: false })).toBe(
false,
);
});
});
describe("createChannelInboundDebouncer", () => {
it("resolves per-channel debounce and forwards callbacks", async () => {
vi.useFakeTimers();
try {
const flushed: string[][] = [];
const cfg = {
messages: {
inbound: {
debounceMs: 10,
byChannel: {
slack: 25,
},
},
},
} as Parameters<typeof createChannelInboundDebouncer<{ id: string }>>[0]["cfg"];
const { debounceMs, debouncer } = createChannelInboundDebouncer<{ id: string }>({
cfg,
channel: "slack",
buildKey: (item) => item.id,
onFlush: async (items) => {
flushed.push(items.map((entry) => entry.id));
},
});
expect(debounceMs).toBe(25);
await debouncer.enqueue({ id: "a" });
await debouncer.enqueue({ id: "a" });
await vi.advanceTimersByTimeAsync(30);
expect(flushed).toEqual([["a", "a"]]);
} finally {
vi.useRealTimers();
}
});
});

View File

@@ -0,0 +1,51 @@
import { hasControlCommand } from "../auto-reply/command-detection.js";
import type { CommandNormalizeOptions } from "../auto-reply/commands-registry.js";
import {
createInboundDebouncer,
resolveInboundDebounceMs,
type InboundDebounceCreateParams,
} from "../auto-reply/inbound-debounce.js";
import type { OpenClawConfig } from "../config/types.js";
export function shouldDebounceTextInbound(params: {
text: string | null | undefined;
cfg: OpenClawConfig;
hasMedia?: boolean;
commandOptions?: CommandNormalizeOptions;
allowDebounce?: boolean;
}): boolean {
if (params.allowDebounce === false) {
return false;
}
if (params.hasMedia) {
return false;
}
const text = params.text?.trim() ?? "";
if (!text) {
return false;
}
return !hasControlCommand(text, params.cfg, params.commandOptions);
}
export function createChannelInboundDebouncer<T>(
params: Omit<InboundDebounceCreateParams<T>, "debounceMs"> & {
cfg: OpenClawConfig;
channel: string;
debounceMsOverride?: number;
},
): {
debounceMs: number;
debouncer: ReturnType<typeof createInboundDebouncer<T>>;
} {
const debounceMs = resolveInboundDebounceMs({
cfg: params.cfg,
channel: params.channel,
overrideMs: params.debounceMsOverride,
});
const { cfg: _cfg, channel: _channel, debounceMsOverride: _override, ...rest } = params;
const debouncer = createInboundDebouncer<T>({
debounceMs,
...rest,
});
return { debounceMs, debouncer };
}