mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 23:21:30 +00:00
perf: direct export whatsapp target helpers
This commit is contained in:
@@ -880,6 +880,11 @@ export const GENERATED_PLUGIN_SDK_FACADES = [
|
||||
subpath: "whatsapp-targets",
|
||||
source: pluginSource("whatsapp", "targets.js"),
|
||||
exports: ["isWhatsAppGroupJid", "isWhatsAppUserTarget", "normalizeWhatsAppTarget"],
|
||||
directExports: {
|
||||
isWhatsAppGroupJid: "./whatsapp-targets-shared.js",
|
||||
isWhatsAppUserTarget: "./whatsapp-targets-shared.js",
|
||||
normalizeWhatsAppTarget: "./whatsapp-targets-shared.js",
|
||||
},
|
||||
},
|
||||
{
|
||||
subpath: "whatsapp-surface",
|
||||
@@ -1194,8 +1199,11 @@ export function buildPluginSdkFacadeModule(entry, params = {}) {
|
||||
typeExports.push(typeExport);
|
||||
}
|
||||
}
|
||||
const nonDirectValueExports = valueExports.filter(
|
||||
(exportName) => !directExportSources[exportName],
|
||||
);
|
||||
const lines = [`// Generated by ${GENERATED_PLUGIN_SDK_FACADES_SCRIPT}. Do not edit manually.`];
|
||||
if (valueExports.length || typeExports.length) {
|
||||
if (nonDirectValueExports.length || typeExports.length) {
|
||||
lines.push(
|
||||
'import type { PluginSdkFacadeTypeMap } from "../generated/plugin-sdk-facade-type-map.generated.js";',
|
||||
);
|
||||
@@ -1229,7 +1237,7 @@ export function buildPluginSdkFacadeModule(entry, params = {}) {
|
||||
);
|
||||
}
|
||||
}
|
||||
if (valueExports.length) {
|
||||
if (nonDirectValueExports.length) {
|
||||
const runtimeImports = new Set();
|
||||
if (needsLazyArrayHelper) {
|
||||
runtimeImports.add("createLazyFacadeArrayValue");
|
||||
@@ -1270,11 +1278,11 @@ export function buildPluginSdkFacadeModule(entry, params = {}) {
|
||||
lines.push("}");
|
||||
}
|
||||
}
|
||||
if (valueExports.length) {
|
||||
if (nonDirectValueExports.length) {
|
||||
const sourceIndexByPath = new Map(
|
||||
listFacadeEntrySourcePaths(entry).map((sourcePath, index) => [sourcePath, index]),
|
||||
);
|
||||
for (const exportName of valueExports) {
|
||||
for (const exportName of nonDirectValueExports) {
|
||||
if (directExportSources[exportName]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
69
src/plugin-sdk/whatsapp-targets-shared.ts
Normal file
69
src/plugin-sdk/whatsapp-targets-shared.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
|
||||
const WHATSAPP_USER_JID_RE = /^(\d+)(?::\d+)?@s\.whatsapp\.net$/i;
|
||||
const WHATSAPP_LID_RE = /^(\d+)@lid$/i;
|
||||
|
||||
function stripWhatsAppTargetPrefixes(value: string): string {
|
||||
let candidate = value.trim();
|
||||
for (;;) {
|
||||
const previous = candidate;
|
||||
candidate = candidate.replace(/^whatsapp:/i, "").trim();
|
||||
if (candidate === previous) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isWhatsAppGroupJid(value: string): boolean {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
const lower = candidate.toLowerCase();
|
||||
if (!lower.endsWith("@g.us")) {
|
||||
return false;
|
||||
}
|
||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
||||
if (!localPart || localPart.includes("@")) {
|
||||
return false;
|
||||
}
|
||||
return /^[0-9]+(-[0-9]+)*$/.test(localPart);
|
||||
}
|
||||
|
||||
export function isWhatsAppUserTarget(value: string): boolean {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
return WHATSAPP_USER_JID_RE.test(candidate) || WHATSAPP_LID_RE.test(candidate);
|
||||
}
|
||||
|
||||
function extractUserJidPhone(jid: string): string | null {
|
||||
const userMatch = jid.match(WHATSAPP_USER_JID_RE);
|
||||
if (userMatch) {
|
||||
return userMatch[1];
|
||||
}
|
||||
const lidMatch = jid.match(WHATSAPP_LID_RE);
|
||||
if (lidMatch) {
|
||||
return lidMatch[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function normalizeWhatsAppTarget(value: string): string | null {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
if (!candidate) {
|
||||
return null;
|
||||
}
|
||||
if (isWhatsAppGroupJid(candidate)) {
|
||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
||||
return `${localPart}@g.us`;
|
||||
}
|
||||
if (isWhatsAppUserTarget(candidate)) {
|
||||
const phone = extractUserJidPhone(candidate);
|
||||
if (!phone) {
|
||||
return null;
|
||||
}
|
||||
const normalized = normalizeE164(phone);
|
||||
return normalized.length > 1 ? normalized : null;
|
||||
}
|
||||
if (candidate.includes("@")) {
|
||||
return null;
|
||||
}
|
||||
const normalized = normalizeE164(candidate);
|
||||
return normalized.length > 1 ? normalized : null;
|
||||
}
|
||||
@@ -1,20 +1,6 @@
|
||||
// Generated by scripts/generate-plugin-sdk-facades.mjs. Do not edit manually.
|
||||
import type { PluginSdkFacadeTypeMap } from "../generated/plugin-sdk-facade-type-map.generated.js";
|
||||
type FacadeEntry = PluginSdkFacadeTypeMap["whatsapp-targets"];
|
||||
type FacadeModule = FacadeEntry["module"];
|
||||
import { loadBundledPluginPublicSurfaceModuleSync } from "./facade-runtime.js";
|
||||
|
||||
function loadFacadeModule(): FacadeModule {
|
||||
return loadBundledPluginPublicSurfaceModuleSync<FacadeModule>({
|
||||
dirName: "whatsapp",
|
||||
artifactBasename: "targets.js",
|
||||
});
|
||||
}
|
||||
export const isWhatsAppGroupJid: FacadeModule["isWhatsAppGroupJid"] = ((...args) =>
|
||||
loadFacadeModule()["isWhatsAppGroupJid"](...args)) as FacadeModule["isWhatsAppGroupJid"];
|
||||
export const isWhatsAppUserTarget: FacadeModule["isWhatsAppUserTarget"] = ((...args) =>
|
||||
loadFacadeModule()["isWhatsAppUserTarget"](...args)) as FacadeModule["isWhatsAppUserTarget"];
|
||||
export const normalizeWhatsAppTarget: FacadeModule["normalizeWhatsAppTarget"] = ((...args) =>
|
||||
loadFacadeModule()["normalizeWhatsAppTarget"](
|
||||
...args,
|
||||
)) as FacadeModule["normalizeWhatsAppTarget"];
|
||||
export {
|
||||
isWhatsAppGroupJid,
|
||||
isWhatsAppUserTarget,
|
||||
normalizeWhatsAppTarget,
|
||||
} from "./whatsapp-targets-shared.js";
|
||||
|
||||
Reference in New Issue
Block a user