mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-06 14:51:08 +00:00
* feat(telegram): add child thread-binding placement via createForumTopic Enable ACP subagent spawn on Telegram by adding "child" placement support to the thread-bindings adapter. When a child binding is requested, the adapter creates a new forum topic via the Telegram Bot API and binds the subagent session to it using the canonical chatId:topic:topicId conversation ID format. When the ACP spawn context provides only a topic ID (not a full group chat ID), the adapter resolves the group from the configured Telegram groups in openclaw.json. This mirrors the Discord adapter's child placement behavior (thread creation + session binding) and unblocks the orchestrator pattern on Telegram forum-enabled groups. Closes #5737 Ref #23414 * fix(telegram): return null with warning instead of silent group fallback for bare topic IDs in child bind * telegram: fix ACP child thread spawn with group chat ID from agentGroupId * telegram: scope agentGroupId substitution to telegram channel only * Telegram: fix forum topic replies routing to root chat instead of topic thread * fix: clean up dead guard in child bind + add explicit threadId override test - Simplify bare-topic-ID guards in thread-bindings.ts: split into separate !chatId and !chatId.startsWith("-") checks, removing unreachable second condition - Add regression test confirming explicit turnSourceThreadId overrides session lastThreadId on same channel * fix: guard threadId fallback against shared-session race Codex review P1: when turnSourceTo differs from the session's stored to, the session threadId may belong to a different chat/topic. Only fall back to context.threadId when the destination also matches. * fix(telegram): enable ACP spawn from forum topics without thread binding extractExplicitGroupId returned topic-qualified IDs (-100...:topic:1264) instead of bare group chat IDs, breaking agentGroupId resolution. agentGroupId was also never wired in the inline actions path. For Telegram forum topics, skip thread binding entirely — the delivery plan already routes correctly via requester origin (to + threadId). Creating new forum topics per child session is unnecessary; output goes back to the same topic the user asked from. * fix(acp): bind Telegram forum sessions to current topic * fix: restore Telegram forum-topic routing (#56060) (thanks @one27001) --------- Co-authored-by: openclaw <mgabrie.dev@gmail.com> Co-authored-by: Ayaan Zaidi <hi@obviy.us>
326 lines
10 KiB
TypeScript
326 lines
10 KiB
TypeScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { getSessionBindingService } from "openclaw/plugin-sdk/conversation-runtime";
|
|
import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { importFreshModule } from "../../../test/helpers/import-fresh.js";
|
|
import { loadBundledPluginTestApiSync } from "../../../test/helpers/plugins/bundled-plugin-public-surface.js";
|
|
import {
|
|
__testing,
|
|
createTelegramThreadBindingManager,
|
|
setTelegramThreadBindingIdleTimeoutBySessionKey,
|
|
setTelegramThreadBindingMaxAgeBySessionKey,
|
|
} from "./thread-bindings.js";
|
|
|
|
describe("telegram thread bindings", () => {
|
|
let stateDirOverride: string | undefined;
|
|
|
|
beforeEach(async () => {
|
|
await __testing.resetTelegramThreadBindingsForTests();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
vi.useRealTimers();
|
|
await __testing.resetTelegramThreadBindingsForTests();
|
|
if (stateDirOverride) {
|
|
delete process.env.OPENCLAW_STATE_DIR;
|
|
fs.rmSync(stateDirOverride, { recursive: true, force: true });
|
|
stateDirOverride = undefined;
|
|
}
|
|
});
|
|
|
|
it("registers a telegram binding adapter and binds current conversations", async () => {
|
|
const manager = createTelegramThreadBindingManager({
|
|
accountId: "work",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
idleTimeoutMs: 30_000,
|
|
maxAgeMs: 0,
|
|
});
|
|
const bound = await getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-1",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "work",
|
|
conversationId: "-100200300:topic:77",
|
|
},
|
|
placement: "current",
|
|
metadata: {
|
|
boundBy: "user-1",
|
|
},
|
|
});
|
|
|
|
expect(bound.conversation.channel).toBe("telegram");
|
|
expect(bound.conversation.accountId).toBe("work");
|
|
expect(bound.conversation.conversationId).toBe("-100200300:topic:77");
|
|
expect(bound.targetSessionKey).toBe("agent:main:subagent:child-1");
|
|
expect(manager.getByConversationId("-100200300:topic:77")?.boundBy).toBe("user-1");
|
|
});
|
|
|
|
it("rejects child placement when conversationId is a bare topic ID with no group context", async () => {
|
|
createTelegramThreadBindingManager({
|
|
accountId: "default",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
await expect(
|
|
getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-1",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "default",
|
|
conversationId: "77",
|
|
},
|
|
placement: "child",
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: "BINDING_CREATE_FAILED",
|
|
});
|
|
});
|
|
|
|
it("rejects child placement when parentConversationId is also a bare topic ID", async () => {
|
|
createTelegramThreadBindingManager({
|
|
accountId: "default",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
await expect(
|
|
getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:acp:child-acp-1",
|
|
targetKind: "session",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "default",
|
|
conversationId: "77",
|
|
parentConversationId: "99",
|
|
},
|
|
placement: "child",
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: "BINDING_CREATE_FAILED",
|
|
});
|
|
});
|
|
|
|
it("shares binding state across distinct module instances", async () => {
|
|
const bindingsA = await importFreshModule<typeof import("./thread-bindings.js")>(
|
|
import.meta.url,
|
|
"./thread-bindings.js?scope=shared-a",
|
|
);
|
|
const bindingsB = await importFreshModule<typeof import("./thread-bindings.js")>(
|
|
import.meta.url,
|
|
"./thread-bindings.js?scope=shared-b",
|
|
);
|
|
|
|
await bindingsA.__testing.resetTelegramThreadBindingsForTests();
|
|
|
|
try {
|
|
const managerA = bindingsA.createTelegramThreadBindingManager({
|
|
accountId: "shared-runtime",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
const managerB = bindingsB.createTelegramThreadBindingManager({
|
|
accountId: "shared-runtime",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
expect(managerB).toBe(managerA);
|
|
|
|
await getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-shared",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "shared-runtime",
|
|
conversationId: "-100200300:topic:44",
|
|
},
|
|
placement: "current",
|
|
});
|
|
|
|
expect(
|
|
bindingsB
|
|
.getTelegramThreadBindingManager("shared-runtime")
|
|
?.getByConversationId("-100200300:topic:44")?.targetSessionKey,
|
|
).toBe("agent:main:subagent:child-shared");
|
|
} finally {
|
|
await bindingsA.__testing.resetTelegramThreadBindingsForTests();
|
|
}
|
|
});
|
|
|
|
it("updates lifecycle windows by session key", async () => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2026-03-06T10:00:00.000Z"));
|
|
const manager = createTelegramThreadBindingManager({
|
|
accountId: "work",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
await getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-1",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "work",
|
|
conversationId: "1234",
|
|
},
|
|
});
|
|
const original = manager.listBySessionKey("agent:main:subagent:child-1")[0];
|
|
expect(original).toBeDefined();
|
|
|
|
const idleUpdated = setTelegramThreadBindingIdleTimeoutBySessionKey({
|
|
accountId: "work",
|
|
targetSessionKey: "agent:main:subagent:child-1",
|
|
idleTimeoutMs: 2 * 60 * 60 * 1000,
|
|
});
|
|
vi.setSystemTime(new Date("2026-03-06T12:00:00.000Z"));
|
|
const maxAgeUpdated = setTelegramThreadBindingMaxAgeBySessionKey({
|
|
accountId: "work",
|
|
targetSessionKey: "agent:main:subagent:child-1",
|
|
maxAgeMs: 6 * 60 * 60 * 1000,
|
|
});
|
|
|
|
expect(idleUpdated).toHaveLength(1);
|
|
expect(idleUpdated[0]?.idleTimeoutMs).toBe(2 * 60 * 60 * 1000);
|
|
expect(maxAgeUpdated).toHaveLength(1);
|
|
expect(maxAgeUpdated[0]?.maxAgeMs).toBe(6 * 60 * 60 * 1000);
|
|
expect(maxAgeUpdated[0]?.boundAt).toBe(original?.boundAt);
|
|
expect(maxAgeUpdated[0]?.lastActivityAt).toBe(Date.parse("2026-03-06T12:00:00.000Z"));
|
|
expect(manager.listBySessionKey("agent:main:subagent:child-1")[0]?.maxAgeMs).toBe(
|
|
6 * 60 * 60 * 1000,
|
|
);
|
|
});
|
|
|
|
it("exposes the reset helper through the bundled test api", async () => {
|
|
const { resetTelegramThreadBindingsForTests } = loadBundledPluginTestApiSync<{
|
|
resetTelegramThreadBindingsForTests: () => Promise<void>;
|
|
}>("telegram");
|
|
|
|
await expect(resetTelegramThreadBindingsForTests()).resolves.toBeUndefined();
|
|
});
|
|
|
|
it("does not persist lifecycle updates when manager persistence is disabled", async () => {
|
|
stateDirOverride = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-bindings-"));
|
|
process.env.OPENCLAW_STATE_DIR = stateDirOverride;
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2026-03-06T10:00:00.000Z"));
|
|
|
|
createTelegramThreadBindingManager({
|
|
accountId: "no-persist",
|
|
persist: false,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
await getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-2",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "no-persist",
|
|
conversationId: "-100200300:topic:88",
|
|
},
|
|
});
|
|
|
|
setTelegramThreadBindingIdleTimeoutBySessionKey({
|
|
accountId: "no-persist",
|
|
targetSessionKey: "agent:main:subagent:child-2",
|
|
idleTimeoutMs: 60 * 60 * 1000,
|
|
});
|
|
setTelegramThreadBindingMaxAgeBySessionKey({
|
|
accountId: "no-persist",
|
|
targetSessionKey: "agent:main:subagent:child-2",
|
|
maxAgeMs: 2 * 60 * 60 * 1000,
|
|
});
|
|
|
|
const statePath = path.join(
|
|
resolveStateDir(process.env, os.homedir),
|
|
"telegram",
|
|
"thread-bindings-no-persist.json",
|
|
);
|
|
expect(fs.existsSync(statePath)).toBe(false);
|
|
});
|
|
|
|
it("persists unbinds before restart so removed bindings do not come back", async () => {
|
|
stateDirOverride = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-bindings-"));
|
|
process.env.OPENCLAW_STATE_DIR = stateDirOverride;
|
|
|
|
createTelegramThreadBindingManager({
|
|
accountId: "default",
|
|
persist: true,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
const bound = await getSessionBindingService().bind({
|
|
targetSessionKey: "plugin-binding:openclaw-codex-app-server:abc123",
|
|
targetKind: "session",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "default",
|
|
conversationId: "8460800771",
|
|
},
|
|
});
|
|
|
|
await getSessionBindingService().unbind({
|
|
bindingId: bound.bindingId,
|
|
reason: "test-detach",
|
|
});
|
|
|
|
await __testing.resetTelegramThreadBindingsForTests();
|
|
|
|
const reloaded = createTelegramThreadBindingManager({
|
|
accountId: "default",
|
|
persist: true,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
expect(reloaded.getByConversationId("8460800771")).toBeUndefined();
|
|
});
|
|
|
|
it("flushes pending lifecycle update persists before test reset", async () => {
|
|
stateDirOverride = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-bindings-"));
|
|
process.env.OPENCLAW_STATE_DIR = stateDirOverride;
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2026-03-06T10:00:00.000Z"));
|
|
|
|
createTelegramThreadBindingManager({
|
|
accountId: "persist-reset",
|
|
persist: true,
|
|
enableSweeper: false,
|
|
});
|
|
|
|
await getSessionBindingService().bind({
|
|
targetSessionKey: "agent:main:subagent:child-3",
|
|
targetKind: "subagent",
|
|
conversation: {
|
|
channel: "telegram",
|
|
accountId: "persist-reset",
|
|
conversationId: "-100200300:topic:99",
|
|
},
|
|
});
|
|
|
|
setTelegramThreadBindingIdleTimeoutBySessionKey({
|
|
accountId: "persist-reset",
|
|
targetSessionKey: "agent:main:subagent:child-3",
|
|
idleTimeoutMs: 90_000,
|
|
});
|
|
|
|
await __testing.resetTelegramThreadBindingsForTests();
|
|
|
|
const statePath = path.join(
|
|
resolveStateDir(process.env, os.homedir),
|
|
"telegram",
|
|
"thread-bindings-persist-reset.json",
|
|
);
|
|
const persisted = JSON.parse(fs.readFileSync(statePath, "utf8")) as {
|
|
bindings?: Array<{ idleTimeoutMs?: number }>;
|
|
};
|
|
expect(persisted.bindings?.[0]?.idleTimeoutMs).toBe(90_000);
|
|
});
|
|
});
|