Plugin SDK: add outbound session route helpers

This commit is contained in:
Gustavo Madeira Santana 2026-03-18 04:03:00 +00:00
parent 92a40d324a
commit 826c592deb
No known key found for this signature in database
5 changed files with 69 additions and 0 deletions

View File

@ -346,6 +346,10 @@
"types": "./dist/plugin-sdk/zalouser.d.ts",
"default": "./dist/plugin-sdk/zalouser.js"
},
"./plugin-sdk/account-helpers": {
"types": "./dist/plugin-sdk/account-helpers.d.ts",
"default": "./dist/plugin-sdk/account-helpers.js"
},
"./plugin-sdk/account-id": {
"types": "./dist/plugin-sdk/account-id.d.ts",
"default": "./dist/plugin-sdk/account-id.js"

View File

@ -76,6 +76,7 @@
"voice-call",
"zalo",
"zalouser",
"account-helpers",
"account-id",
"account-resolution",
"allow-from",

View File

@ -0,0 +1 @@
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";

View File

@ -1,5 +1,11 @@
import type {
ChannelMessagingAdapter,
ChannelOutboundSessionRoute,
} from "../channels/plugins/types.core.js";
import type { ChannelPlugin } from "../channels/plugins/types.plugin.js";
import { getChatChannelMeta } from "../channels/registry.js";
import type { OpenClawConfig } from "../config/config.js";
import { buildOutboundBaseSessionKey } from "../infra/outbound/base-session-key.js";
import { emptyPluginConfigSchema } from "../plugins/config-schema.js";
import type { PluginRuntime } from "../plugins/runtime/types.js";
import type {
@ -49,6 +55,10 @@ export type {
} from "../plugins/types.js";
export type { OpenClawConfig } from "../config/config.js";
export type { GatewayRequestHandlerOptions } from "../gateway/server-methods/types.js";
export type {
ChannelOutboundSessionRoute,
ChannelMessagingAdapter,
} from "../channels/plugins/types.core.js";
export type {
ProviderUsageSnapshot,
UsageProviderId,
@ -105,6 +115,54 @@ export { buildOutboundBaseSessionKey } from "../infra/outbound/base-session-key.
export { normalizeOutboundThreadId } from "../infra/outbound/thread-id.js";
export { resolveThreadSessionKeys } from "../routing/session-key.js";
export type ChannelOutboundSessionRouteParams = Parameters<
NonNullable<ChannelMessagingAdapter["resolveOutboundSessionRoute"]>
>[0];
export function stripChannelTargetPrefix(raw: string, ...providers: string[]): string {
const trimmed = raw.trim();
for (const provider of providers) {
const prefix = `${provider.toLowerCase()}:`;
if (trimmed.toLowerCase().startsWith(prefix)) {
return trimmed.slice(prefix.length).trim();
}
}
return trimmed;
}
export function stripTargetKindPrefix(raw: string): string {
return raw.replace(/^(user|channel|group|conversation|room|dm):/i, "").trim();
}
export function buildChannelOutboundSessionRoute(params: {
cfg: OpenClawConfig;
agentId: string;
channel: string;
accountId?: string | null;
peer: { kind: "direct" | "group" | "channel"; id: string };
chatType: "direct" | "group" | "channel";
from: string;
to: string;
threadId?: string | number;
}): ChannelOutboundSessionRoute {
const baseSessionKey = buildOutboundBaseSessionKey({
cfg: params.cfg,
agentId: params.agentId,
channel: params.channel,
accountId: params.accountId,
peer: params.peer,
});
return {
sessionKey: baseSessionKey,
baseSessionKey,
peer: params.peer,
chatType: params.chatType,
from: params.from,
to: params.to,
...(params.threadId !== undefined ? { threadId: params.threadId } : {}),
};
}
type DefineChannelPluginEntryOptions<TPlugin extends ChannelPlugin = ChannelPlugin> = {
id: string;
name: string;

View File

@ -56,6 +56,7 @@ const matrixSdk = await import("openclaw/plugin-sdk/matrix");
const mattermostSdk = await import("openclaw/plugin-sdk/mattermost");
const nextcloudTalkSdk = await import("openclaw/plugin-sdk/nextcloud-talk");
const twitchSdk = await import("openclaw/plugin-sdk/twitch");
const accountHelpersSdk = await import("openclaw/plugin-sdk/account-helpers");
describe("plugin-sdk subpath exports", () => {
it("exports compat helpers", () => {
@ -83,6 +84,10 @@ describe("plugin-sdk subpath exports", () => {
expect(typeof routingSdk.resolveThreadSessionKeys).toBe("function");
});
it("exports account helper builders from the dedicated subpath", () => {
expect(typeof accountHelpersSdk.createAccountListHelpers).toBe("function");
});
it("exports runtime helpers from the dedicated subpath", () => {
expect(typeof runtimeSdk.createLoggerBackedRuntime).toBe("function");
});