refactor: unify peer kind to ChatType, rename dm to direct (#11881)

* fix: use .js extension for ESM imports of RoutePeerKind

The imports incorrectly used .ts extension which doesn't resolve
with moduleResolution: NodeNext. Changed to .js and added 'type'
import modifier.

* fix tsconfig

* refactor: unify peer kind to ChatType, rename dm to direct

- Replace RoutePeerKind with ChatType throughout codebase
- Change 'dm' literal values to 'direct' in routing/session keys
- Keep backward compat: normalizeChatType accepts 'dm' -> 'direct'
- Add ChatType export to plugin-sdk, deprecate RoutePeerKind
- Update session key parsing to accept both 'dm' and 'direct' markers
- Update all channel monitors and extensions to use ChatType

BREAKING CHANGE: Session keys now use 'direct' instead of 'dm'.
Existing 'dm' keys still work via backward compat layer.

* fix tests

* test: update session key expectations for dmdirect migration

- Fix test expectations to expect :direct: in generated output
- Add explicit backward compat test for normalizeChatType('dm')
- Keep input test data with :dm: keys to verify backward compat

* fix: accept legacy 'dm' in session key parsing for backward compat

getDmHistoryLimitFromSessionKey now accepts both :dm: and :direct:
to ensure old session keys continue to work correctly.

* test: add explicit backward compat tests for dmdirect migration

- session-key.test.ts: verify both :dm: and :direct: keys are valid
- getDmHistoryLimitFromSessionKey: verify both formats work

* feat: backward compat for resetByType.dm config key

* test: skip unix-path Nix tests on Windows
This commit is contained in:
max
2026-02-08 16:20:52 -08:00
committed by GitHub
parent 0b07e15b63
commit 223eee0a20
64 changed files with 377 additions and 185 deletions

View File

@@ -9,7 +9,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: null,
peer: { kind: "dm", id: "+15551234567" },
peer: { kind: "direct", id: "+15551234567" },
});
expect(route.agentId).toBe("main");
expect(route.accountId).toBe("default");
@@ -25,9 +25,9 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: null,
peer: { kind: "dm", id: "+15551234567" },
peer: { kind: "direct", id: "+15551234567" },
});
expect(route.sessionKey).toBe("agent:main:dm:+15551234567");
expect(route.sessionKey).toBe("agent:main:direct:+15551234567");
});
test("dmScope=per-channel-peer isolates DM sessions per channel and sender", () => {
@@ -38,9 +38,9 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: null,
peer: { kind: "dm", id: "+15551234567" },
peer: { kind: "direct", id: "+15551234567" },
});
expect(route.sessionKey).toBe("agent:main:whatsapp:dm:+15551234567");
expect(route.sessionKey).toBe("agent:main:whatsapp:direct:+15551234567");
});
test("identityLinks collapses per-peer DM sessions across providers", () => {
@@ -56,9 +56,9 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "telegram",
accountId: null,
peer: { kind: "dm", id: "111111111" },
peer: { kind: "direct", id: "111111111" },
});
expect(route.sessionKey).toBe("agent:main:dm:alice");
expect(route.sessionKey).toBe("agent:main:direct:alice");
});
test("identityLinks applies to per-channel-peer DM sessions", () => {
@@ -74,9 +74,9 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "discord",
accountId: null,
peer: { kind: "dm", id: "222222222222222222" },
peer: { kind: "direct", id: "222222222222222222" },
});
expect(route.sessionKey).toBe("agent:main:discord:dm:alice");
expect(route.sessionKey).toBe("agent:main:discord:direct:alice");
});
test("peer binding wins over account binding", () => {
@@ -87,7 +87,7 @@ describe("resolveAgentRoute", () => {
match: {
channel: "whatsapp",
accountId: "biz",
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
},
},
{
@@ -100,7 +100,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: "biz",
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
});
expect(route.agentId).toBe("a");
expect(route.sessionKey).toBe("agent:a:main");
@@ -177,7 +177,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: undefined,
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
});
expect(defaultRoute.agentId).toBe("defaultacct");
expect(defaultRoute.matchedBy).toBe("binding.account");
@@ -186,7 +186,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: "biz",
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
});
expect(otherRoute.agentId).toBe("main");
});
@@ -204,7 +204,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: "biz",
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
});
expect(route.agentId).toBe("any");
expect(route.matchedBy).toBe("binding.channel");
@@ -220,7 +220,7 @@ describe("resolveAgentRoute", () => {
cfg,
channel: "whatsapp",
accountId: "biz",
peer: { kind: "dm", id: "+1000" },
peer: { kind: "direct", id: "+1000" },
});
expect(route.agentId).toBe("home");
expect(route.sessionKey).toBe("agent:home:main");
@@ -235,9 +235,9 @@ test("dmScope=per-account-channel-peer isolates DM sessions per account, channel
cfg,
channel: "telegram",
accountId: "tasks",
peer: { kind: "dm", id: "7550356539" },
peer: { kind: "direct", id: "7550356539" },
});
expect(route.sessionKey).toBe("agent:main:telegram:tasks:dm:7550356539");
expect(route.sessionKey).toBe("agent:main:telegram:tasks:direct:7550356539");
});
test("dmScope=per-account-channel-peer uses default accountId when not provided", () => {
@@ -248,9 +248,9 @@ test("dmScope=per-account-channel-peer uses default accountId when not provided"
cfg,
channel: "telegram",
accountId: null,
peer: { kind: "dm", id: "7550356539" },
peer: { kind: "direct", id: "7550356539" },
});
expect(route.sessionKey).toBe("agent:main:telegram:default:dm:7550356539");
expect(route.sessionKey).toBe("agent:main:telegram:default:direct:7550356539");
});
describe("parentPeer binding inheritance (thread support)", () => {
@@ -409,3 +409,29 @@ describe("parentPeer binding inheritance (thread support)", () => {
expect(route.matchedBy).toBe("default");
});
});
describe("backward compatibility: peer.kind dm → direct", () => {
test("legacy dm in config matches runtime direct peer", () => {
const cfg: OpenClawConfig = {
bindings: [
{
agentId: "alex",
match: {
channel: "whatsapp",
// Legacy config uses "dm" instead of "direct"
peer: { kind: "dm", id: "+15551234567" },
},
},
],
};
const route = resolveAgentRoute({
cfg,
channel: "whatsapp",
accountId: null,
// Runtime uses canonical "direct"
peer: { kind: "direct", id: "+15551234567" },
});
expect(route.agentId).toBe("alex");
expect(route.matchedBy).toBe("binding.peer");
});
});