Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/channel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { registerWeixinAccountId } from "./auth/accounts.js";
import { weixinPlugin } from "./channel.js";

let stateDir: string;

beforeEach(() => {
stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "weixin-session-route-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
});

afterEach(() => {
delete process.env.OPENCLAW_STATE_DIR;
fs.rmSync(stateDir, { recursive: true, force: true });
});

async function resolveRoute(params: { target: string; accountId?: string }) {
const resolver = weixinPlugin.messaging?.resolveOutboundSessionRoute;
if (!resolver) throw new Error("expected outbound session route resolver");
return await resolver({
cfg: { session: { dmScope: "per-account-channel-peer" } },
agentId: "main",
target: params.target,
accountId: params.accountId,
});
}

describe("Weixin outbound session route", () => {
it("certifies an explicit account and canonical Weixin user", async () => {
const route = await resolveRoute({
target: "openclaw-weixin:user:Alice@im.wechat",
accountId: "Bot@One",
});

expect(route).toMatchObject({
sessionKey: "agent:main:openclaw-weixin:bot-one:direct:alice@im.wechat",
recipientSessionExact: true,
peer: { kind: "direct", id: "Alice@im.wechat" },
to: "Alice@im.wechat",
});
});

it("uses the only registered account instead of the default account", async () => {
registerWeixinAccountId("bot-one");

const route = await resolveRoute({ target: "alice@im.wechat" });

expect(route).toMatchObject({
sessionKey: "agent:main:openclaw-weixin:bot-one:direct:alice@im.wechat",
recipientSessionExact: true,
});
});

it("rejects targets that cannot identify an inbound Weixin session", async () => {
await expect(resolveRoute({ target: "group:alice@im.wechat" })).resolves.toBeNull();
});
});
41 changes: 39 additions & 2 deletions src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import path from "node:path";

import type { ChannelPlugin, OpenClawConfig, PluginRuntime } from "openclaw/plugin-sdk/core";
import {
buildChannelOutboundSessionRoute,
stripChannelTargetPrefix,
stripTargetKindPrefix,
type ChannelOutboundSessionRouteParams,
type ChannelPlugin,
type OpenClawConfig,
type PluginRuntime,
} from "openclaw/plugin-sdk/core";
import { normalizeAccountId } from "openclaw/plugin-sdk/account-id";
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/infra-runtime";

Expand Down Expand Up @@ -106,6 +114,34 @@ function resolveOutboundAccountId(
);
}

function normalizeWeixinUserId(raw: string): string | null {
const channelTarget = stripChannelTargetPrefix(raw.trim(), "openclaw-weixin");
const userId = stripTargetKindPrefix(channelTarget).trim();
if (userId !== channelTarget.trim() && !/^(user|dm):/i.test(channelTarget)) return null;
return userId.endsWith("@im.wechat") ? userId : null;
}

function resolveWeixinOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
const userId = normalizeWeixinUserId(params.target);
if (!userId) return null;
const accountId = params.accountId?.trim()
? resolveWeixinAccount(params.cfg, params.accountId).accountId
: resolveOutboundAccountId(params.cfg, userId);
return {
...buildChannelOutboundSessionRoute({
cfg: params.cfg,
agentId: params.agentId,
channel: "openclaw-weixin",
accountId,
peer: { kind: "direct", id: userId },
chatType: "direct",
from: userId,
to: userId,
}),
recipientSessionExact: true as const,
};
}

async function sendWeixinOutbound(params: {
cfg: OpenClawConfig;
to: string;
Expand Down Expand Up @@ -190,8 +226,9 @@ export const weixinPlugin: ChannelPlugin<ResolvedWeixinAccount> = {
messaging: {
targetResolver: {
// Weixin user IDs always end with @im.wechat; treat as direct IDs, skip directory lookup.
looksLikeId: (raw) => raw.endsWith("@im.wechat"),
looksLikeId: (raw) => normalizeWeixinUserId(raw) !== null,
},
resolveOutboundSessionRoute: resolveWeixinOutboundSessionRoute,
},
agentPrompt: {
messageToolHints: () => [
Expand Down