mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:24:32 +00:00
refactor: unify inbound debounce policy and split gateway/models helpers
This commit is contained in:
61
src/channels/inbound-debounce-policy.test.ts
Normal file
61
src/channels/inbound-debounce-policy.test.ts
Normal 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();
|
||||
}
|
||||
});
|
||||
});
|
||||
51
src/channels/inbound-debounce-policy.ts
Normal file
51
src/channels/inbound-debounce-policy.ts
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user