diff --git a/package.json b/package.json index 37a4e595207..ba0c925fded 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/scripts/lib/plugin-sdk-entrypoints.json b/scripts/lib/plugin-sdk-entrypoints.json index c59e7930ba2..22152408e54 100644 --- a/scripts/lib/plugin-sdk-entrypoints.json +++ b/scripts/lib/plugin-sdk-entrypoints.json @@ -76,6 +76,7 @@ "voice-call", "zalo", "zalouser", + "account-helpers", "account-id", "account-resolution", "allow-from", diff --git a/src/plugin-sdk/account-helpers.ts b/src/plugin-sdk/account-helpers.ts new file mode 100644 index 00000000000..5055e80571a --- /dev/null +++ b/src/plugin-sdk/account-helpers.ts @@ -0,0 +1 @@ +export { createAccountListHelpers } from "../channels/plugins/account-helpers.js"; diff --git a/src/plugin-sdk/core.ts b/src/plugin-sdk/core.ts index 7bec25a02cf..232989ebbfc 100644 --- a/src/plugin-sdk/core.ts +++ b/src/plugin-sdk/core.ts @@ -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 +>[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 = { id: string; name: string; diff --git a/src/plugin-sdk/subpaths.test.ts b/src/plugin-sdk/subpaths.test.ts index 052b9629421..1b31ed580e4 100644 --- a/src/plugin-sdk/subpaths.test.ts +++ b/src/plugin-sdk/subpaths.test.ts @@ -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"); });