Skip to content

Commit eff606f

Browse files
Mlightsnowvincentkoc
authored andcommitted
fix(telegram): honor human delay for streamed replies
Punchcard-Session: calm-brook-harbor-jm
1 parent 7ba58ab commit eff606f

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎extensions/telegram/src/bot-message-dispatch-turn.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
handleReplySkip,
4242
resetReasoningStepState,
4343
} from "./bot-message-dispatch-reply.js";
44+
import { resolveHumanDelayConfig } from "./bot-message-dispatch.agent.runtime.js";
4445
import type { TelegramDispatchTurn as Turn } from "./bot-message-dispatch.types.js";
4546
import { TELEGRAM_CHAT_ACTION_INTERVAL_MS } from "./chat-action-timing.js";
4647
import { telegramInboundEventDelivery } from "./inbound-event-delivery.js";
@@ -134,6 +135,7 @@ export async function runTelegramDispatchTurn(turn: Turn) {
134135
},
135136
dispatcherOptions: {
136137
...replyPipeline,
138+
humanDelay: resolveHumanDelayConfig(turn.cfg, context.route.agentId),
137139
beforeDeliver: async (payload) => payload,
138140
onBeforeDeliverCancelled: (payload, info) =>
139141
handleBeforeDeliverCancelled(turn, payload, info),

‎extensions/telegram/src/bot-message-dispatch.agent.runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export {
55
modelSupportsVision,
66
resolveAgentDir,
77
resolveDefaultModelForAgent,
8+
resolveHumanDelayConfig,
89
} from "openclaw/plugin-sdk/agent-runtime";

‎extensions/telegram/src/bot-message-dispatch.delivery-basics.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
mockDefaultSessionEntry,
1717
readLatestAssistantTextByIdentity,
1818
recordOutboundMessageForPromptContext,
19+
resolveHumanDelayConfig,
1920
setupDraftStreams,
2021
telegramDepsForTest,
2122
} from "./bot-message-dispatch.test-harness.js";
@@ -25,6 +26,29 @@ import type {
2526
} from "./bot-message-dispatch.test-harness.js";
2627

2728
describeTelegramDispatch("dispatchTelegramMessage delivery-basics", () => {
29+
it("forwards route-scoped humanDelay to the block dispatcher", async () => {
30+
const humanDelay = { mode: "custom" as const, minMs: 800, maxMs: 2_500 };
31+
const cfg = { agents: { defaults: { humanDelay } } } as Parameters<
32+
typeof dispatchWithContext
33+
>[0]["cfg"];
34+
resolveHumanDelayConfig.mockReturnValue(humanDelay);
35+
36+
await dispatchWithContext({
37+
context: createContext({
38+
route: {
39+
agentId: "ops",
40+
accountId: "default",
41+
} as unknown as TelegramMessageContext["route"],
42+
}),
43+
cfg,
44+
streamMode: "off",
45+
});
46+
47+
expect(resolveHumanDelayConfig).toHaveBeenCalledWith(cfg, "ops");
48+
const dispatch = expectRecordFields(mockCallArg(dispatchReplyWithBufferedBlockDispatcher), {});
49+
expectRecordFields(dispatch.dispatcherOptions, { humanDelay });
50+
});
51+
2852
it("forwards cfg to direct reply delivery", async () => {
2953
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ dispatcherOptions }) => {
3054
await dispatcherOptions.deliver({ text: "Hello" }, { kind: "final" });

‎extensions/telegram/src/bot-message-dispatch.test-harness.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const resolveAgentWorkspaceDirHoisted = vi.hoisted(() => vi.fn(() => "/tmp/works
110110
const resolveDefaultModelForAgentHoisted = vi.hoisted(() =>
111111
vi.fn(() => ({ provider: "openai", model: "gpt-test" })),
112112
);
113+
const resolveHumanDelayConfigHoisted = vi.hoisted(() => vi.fn());
113114
const getAgentScopedMediaLocalRootsHoisted = vi.hoisted(() =>
114115
vi.fn((_cfg: unknown, agentId: string) => [`/tmp/.openclaw/workspace-${agentId}`]),
115116
);
@@ -154,6 +155,7 @@ const findModelInCatalog = findModelInCatalogHoisted;
154155
const modelSupportsVision = modelSupportsVisionHoisted;
155156
const resolveAgentDir = resolveAgentDirHoisted;
156157
const resolveDefaultModelForAgent = resolveDefaultModelForAgentHoisted;
158+
export const resolveHumanDelayConfig = resolveHumanDelayConfigHoisted;
157159
const getAgentScopedMediaLocalRoots = getAgentScopedMediaLocalRootsHoisted;
158160
const resolveChunkMode = resolveChunkModeHoisted;
159161
export const resolveMarkdownTableMode = resolveMarkdownTableModeHoisted;
@@ -301,6 +303,7 @@ vi.mock("./bot-message-dispatch.agent.runtime.js", () => ({
301303
resolveAgentDir: resolveAgentDirHoisted,
302304
resolveAgentWorkspaceDir: resolveAgentWorkspaceDirHoisted,
303305
resolveDefaultModelForAgent: resolveDefaultModelForAgentHoisted,
306+
resolveHumanDelayConfig: resolveHumanDelayConfigHoisted,
304307
}));
305308

306309
vi.mock("./sticker-cache.js", () => ({
@@ -411,6 +414,7 @@ function resetTelegramDispatchTestState() {
411414
modelSupportsVision.mockReset();
412415
resolveAgentDir.mockReset();
413416
resolveDefaultModelForAgent.mockReset();
417+
resolveHumanDelayConfig.mockReset();
414418
loadConfig.mockReturnValue({});
415419
dispatchReplyWithBufferedBlockDispatcher.mockResolvedValue({
416420
queuedFinal: false,
@@ -473,6 +477,7 @@ function resetTelegramDispatchTestState() {
473477
provider: "openai",
474478
model: "gpt-test",
475479
});
480+
resolveHumanDelayConfig.mockReturnValue(undefined);
476481
getGlobalHookRunner.mockReturnValue(null);
477482
}
478483

0 commit comments

Comments
 (0)