@gakr-gakr/whatsapp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/action-runtime-api.ts +1 -0
  2. package/action-runtime.runtime.ts +1 -0
  3. package/api.ts +67 -0
  4. package/auth-presence.ts +80 -0
  5. package/autobot.plugin.json +23 -0
  6. package/channel-config-api.ts +1 -0
  7. package/channel-plugin-api.ts +3 -0
  8. package/config-api.ts +4 -0
  9. package/constants.ts +1 -0
  10. package/contract-api.ts +29 -0
  11. package/directory-contract-api.ts +4 -0
  12. package/doctor-contract-api.ts +8 -0
  13. package/index.ts +16 -0
  14. package/legacy-session-surface-api.ts +6 -0
  15. package/legacy-state-migrations-api.ts +1 -0
  16. package/light-runtime-api.ts +12 -0
  17. package/login-qr-api.ts +1 -0
  18. package/login-qr-runtime.ts +23 -0
  19. package/outbound-payload-test-api.ts +1 -0
  20. package/package.json +76 -0
  21. package/runtime-api.ts +84 -0
  22. package/secret-contract-api.ts +4 -0
  23. package/security-contract-api.ts +4 -0
  24. package/setup-entry.ts +21 -0
  25. package/setup-plugin-api.ts +3 -0
  26. package/src/account-config.ts +77 -0
  27. package/src/account-ids.ts +17 -0
  28. package/src/account-types.ts +5 -0
  29. package/src/accounts.ts +176 -0
  30. package/src/action-runtime-target-auth.ts +27 -0
  31. package/src/action-runtime.ts +76 -0
  32. package/src/active-listener.ts +17 -0
  33. package/src/agent-tools-login.ts +113 -0
  34. package/src/approval-auth.ts +27 -0
  35. package/src/auth-store.runtime.ts +1 -0
  36. package/src/auth-store.ts +494 -0
  37. package/src/auto-reply/config.runtime.ts +16 -0
  38. package/src/auto-reply/constants.ts +1 -0
  39. package/src/auto-reply/deliver-reply.ts +332 -0
  40. package/src/auto-reply/loggers.ts +6 -0
  41. package/src/auto-reply/mentions.ts +131 -0
  42. package/src/auto-reply/monitor/ack-reaction.ts +99 -0
  43. package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
  44. package/src/auto-reply/monitor/broadcast.ts +153 -0
  45. package/src/auto-reply/monitor/commands.ts +19 -0
  46. package/src/auto-reply/monitor/echo.ts +64 -0
  47. package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
  48. package/src/auto-reply/monitor/group-activation.ts +73 -0
  49. package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
  50. package/src/auto-reply/monitor/group-gating.ts +218 -0
  51. package/src/auto-reply/monitor/group-members.ts +65 -0
  52. package/src/auto-reply/monitor/inbound-context.ts +92 -0
  53. package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
  54. package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
  55. package/src/auto-reply/monitor/last-route.ts +61 -0
  56. package/src/auto-reply/monitor/listener-log.ts +28 -0
  57. package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
  58. package/src/auto-reply/monitor/message-line.ts +54 -0
  59. package/src/auto-reply/monitor/on-message.ts +333 -0
  60. package/src/auto-reply/monitor/peer.ts +17 -0
  61. package/src/auto-reply/monitor/process-message.ts +584 -0
  62. package/src/auto-reply/monitor/runtime-api.ts +36 -0
  63. package/src/auto-reply/monitor/status-reaction.ts +108 -0
  64. package/src/auto-reply/monitor-state.ts +114 -0
  65. package/src/auto-reply/monitor.ts +720 -0
  66. package/src/auto-reply/reply-resolver.runtime.ts +1 -0
  67. package/src/auto-reply/types.ts +48 -0
  68. package/src/auto-reply/util.ts +62 -0
  69. package/src/auto-reply.impl.ts +6 -0
  70. package/src/auto-reply.ts +1 -0
  71. package/src/channel-actions.runtime.ts +7 -0
  72. package/src/channel-actions.ts +85 -0
  73. package/src/channel-outbound.ts +87 -0
  74. package/src/channel-react-action.runtime.ts +10 -0
  75. package/src/channel-react-action.ts +247 -0
  76. package/src/channel.runtime.ts +117 -0
  77. package/src/channel.setup.ts +32 -0
  78. package/src/channel.ts +356 -0
  79. package/src/command-policy.ts +7 -0
  80. package/src/config-accessors.ts +22 -0
  81. package/src/config-schema.ts +6 -0
  82. package/src/config-ui-hints.ts +24 -0
  83. package/src/connection-controller-registry.ts +49 -0
  84. package/src/connection-controller.ts +680 -0
  85. package/src/creds-files.ts +19 -0
  86. package/src/creds-persistence.ts +71 -0
  87. package/src/directory-config.ts +40 -0
  88. package/src/doctor-contract.ts +11 -0
  89. package/src/doctor.ts +56 -0
  90. package/src/document-filename.ts +17 -0
  91. package/src/group-intro.ts +15 -0
  92. package/src/group-policy.ts +40 -0
  93. package/src/group-session-contract.ts +20 -0
  94. package/src/group-session-key.ts +42 -0
  95. package/src/heartbeat.ts +34 -0
  96. package/src/identity.ts +164 -0
  97. package/src/inbound/access-control.ts +187 -0
  98. package/src/inbound/dedupe.ts +132 -0
  99. package/src/inbound/extract.ts +484 -0
  100. package/src/inbound/lifecycle.ts +39 -0
  101. package/src/inbound/media.ts +128 -0
  102. package/src/inbound/monitor.ts +1042 -0
  103. package/src/inbound/outbound-mentions.ts +260 -0
  104. package/src/inbound/runtime-api.ts +7 -0
  105. package/src/inbound/save-media.runtime.ts +1 -0
  106. package/src/inbound/send-api.ts +203 -0
  107. package/src/inbound/send-result.ts +109 -0
  108. package/src/inbound/types.ts +107 -0
  109. package/src/inbound-policy.ts +215 -0
  110. package/src/inbound.ts +9 -0
  111. package/src/login-qr.ts +542 -0
  112. package/src/login.ts +83 -0
  113. package/src/media.ts +10 -0
  114. package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
  115. package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
  116. package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
  117. package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
  118. package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
  119. package/src/normalize-target.ts +148 -0
  120. package/src/normalize.ts +8 -0
  121. package/src/outbound-adapter.ts +36 -0
  122. package/src/outbound-base.ts +256 -0
  123. package/src/outbound-media-contract.ts +307 -0
  124. package/src/outbound-media.runtime.ts +41 -0
  125. package/src/outbound-send-deps.ts +1 -0
  126. package/src/outbound-test-support.ts +16 -0
  127. package/src/qa-driver.runtime.ts +189 -0
  128. package/src/qr-image.ts +1 -0
  129. package/src/qr-terminal.ts +1 -0
  130. package/src/quoted-message.ts +184 -0
  131. package/src/reaction-level.ts +24 -0
  132. package/src/reconnect.ts +55 -0
  133. package/src/resolve-outbound-target.ts +58 -0
  134. package/src/runtime-api.ts +59 -0
  135. package/src/runtime-group-policy.ts +16 -0
  136. package/src/runtime.ts +9 -0
  137. package/src/security-contract.ts +47 -0
  138. package/src/security-fix.ts +71 -0
  139. package/src/send.ts +342 -0
  140. package/src/session-contract.ts +43 -0
  141. package/src/session-errors.ts +125 -0
  142. package/src/session-route.ts +32 -0
  143. package/src/session.runtime.ts +8 -0
  144. package/src/session.ts +327 -0
  145. package/src/setup-core.ts +52 -0
  146. package/src/setup-finalize.ts +450 -0
  147. package/src/setup-surface.ts +71 -0
  148. package/src/setup-test-helpers.ts +217 -0
  149. package/src/shared.ts +291 -0
  150. package/src/socket-timing.ts +38 -0
  151. package/src/state-migrations.ts +55 -0
  152. package/src/status-issues.ts +185 -0
  153. package/src/system-prompt.ts +31 -0
  154. package/src/targets-runtime.ts +221 -0
  155. package/src/text-runtime.ts +18 -0
  156. package/src/vcard.ts +84 -0
  157. package/targets.ts +5 -0
  158. package/test-api.ts +2 -0
  159. package/tsconfig.json +16 -0
@@ -0,0 +1,185 @@
1
+ import type {
2
+ ChannelAccountSnapshot,
3
+ ChannelStatusIssue,
4
+ } from "autobot/plugin-sdk/channel-contract";
5
+ import { formatCliCommand } from "autobot/plugin-sdk/cli-runtime";
6
+ import {
7
+ asString,
8
+ collectIssuesForEnabledAccounts,
9
+ isRecord,
10
+ } from "autobot/plugin-sdk/status-helpers";
11
+
12
+ type WhatsAppAccountStatus = {
13
+ accountId?: unknown;
14
+ statusState?: unknown;
15
+ enabled?: unknown;
16
+ linked?: unknown;
17
+ connected?: unknown;
18
+ running?: unknown;
19
+ reconnectAttempts?: unknown;
20
+ lastDisconnect?: unknown;
21
+ lastInboundAt?: unknown;
22
+ lastError?: unknown;
23
+ healthState?: unknown;
24
+ };
25
+
26
+ const RECENT_DISCONNECT_WARNING_WINDOW_MS = 15 * 60 * 1000;
27
+
28
+ function readWhatsAppAccountStatus(value: ChannelAccountSnapshot): WhatsAppAccountStatus | null {
29
+ if (!isRecord(value)) {
30
+ return null;
31
+ }
32
+ return {
33
+ accountId: value.accountId,
34
+ statusState: value.statusState,
35
+ enabled: value.enabled,
36
+ linked: value.linked,
37
+ connected: value.connected,
38
+ running: value.running,
39
+ reconnectAttempts: value.reconnectAttempts,
40
+ lastDisconnect: value.lastDisconnect,
41
+ lastInboundAt: value.lastInboundAt,
42
+ lastError: value.lastError,
43
+ healthState: value.healthState,
44
+ };
45
+ }
46
+
47
+ function readLastDisconnect(value: unknown): { at: number | null; error?: string } | null {
48
+ if (typeof value === "string") {
49
+ const error = asString(value);
50
+ return error ? { at: null, error } : null;
51
+ }
52
+ if (!isRecord(value)) {
53
+ return null;
54
+ }
55
+ return {
56
+ at: typeof value.at === "number" ? value.at : null,
57
+ error: asString(value.error),
58
+ };
59
+ }
60
+
61
+ function isRecentDisconnect(disconnect: { at: number | null } | null, now = Date.now()): boolean {
62
+ if (disconnect?.at == null) {
63
+ return false;
64
+ }
65
+ return now - disconnect.at <= RECENT_DISCONNECT_WARNING_WINDOW_MS;
66
+ }
67
+
68
+ export function collectWhatsAppStatusIssues(
69
+ accounts: ChannelAccountSnapshot[],
70
+ ): ChannelStatusIssue[] {
71
+ return collectIssuesForEnabledAccounts({
72
+ accounts,
73
+ readAccount: readWhatsAppAccountStatus,
74
+ collectIssues: ({ account, accountId, issues }) => {
75
+ const linked = account.linked === true;
76
+ const statusState = asString(account.statusState);
77
+ const running = account.running === true;
78
+ const connected = account.connected === true;
79
+ const reconnectAttempts =
80
+ typeof account.reconnectAttempts === "number" ? account.reconnectAttempts : null;
81
+ const lastInboundAt =
82
+ typeof account.lastInboundAt === "number" ? account.lastInboundAt : null;
83
+ const lastDisconnect = readLastDisconnect(account.lastDisconnect);
84
+ const lastError = asString(account.lastError) ?? lastDisconnect?.error;
85
+ const healthState = asString(account.healthState);
86
+
87
+ if (statusState === "unstable") {
88
+ issues.push({
89
+ channel: "whatsapp",
90
+ accountId,
91
+ kind: "auth",
92
+ message: "Auth state is still stabilizing.",
93
+ fix: "Wait a moment for queued credential writes to finish, then retry the command or rerun health.",
94
+ });
95
+ return;
96
+ }
97
+
98
+ if (!linked) {
99
+ issues.push({
100
+ channel: "whatsapp",
101
+ accountId,
102
+ kind: "auth",
103
+ message: "Not linked (no WhatsApp Web session).",
104
+ fix: `Run: ${formatCliCommand("autobot channels login")} (scan QR on the gateway host).`,
105
+ });
106
+ return;
107
+ }
108
+
109
+ if (healthState === "stale") {
110
+ const staleSuffix =
111
+ lastInboundAt != null
112
+ ? ` (last inbound ${Math.max(0, Math.floor((Date.now() - lastInboundAt) / 60000))}m ago)`
113
+ : "";
114
+ issues.push({
115
+ channel: "whatsapp",
116
+ accountId,
117
+ kind: "runtime",
118
+ message: `Linked but stale${staleSuffix}${lastError ? `: ${lastError}` : "."}`,
119
+ fix: `Run: ${formatCliCommand("autobot doctor")} (or restart the gateway). If it persists, relink via channels login and check logs.`,
120
+ });
121
+ return;
122
+ }
123
+
124
+ if (
125
+ healthState === "reconnecting" ||
126
+ healthState === "conflict" ||
127
+ healthState === "stopped"
128
+ ) {
129
+ const stateLabel =
130
+ healthState === "conflict"
131
+ ? "session conflict"
132
+ : healthState === "reconnecting"
133
+ ? "reconnecting"
134
+ : "stopped";
135
+ issues.push({
136
+ channel: "whatsapp",
137
+ accountId,
138
+ kind: "runtime",
139
+ message: `Linked but ${stateLabel}${reconnectAttempts != null ? ` (reconnectAttempts=${reconnectAttempts})` : ""}${lastError ? `: ${lastError}` : "."}`,
140
+ fix: `Run: ${formatCliCommand("autobot doctor")} (or restart the gateway). If it persists, relink via channels login and check logs.`,
141
+ });
142
+ return;
143
+ }
144
+
145
+ if (healthState === "logged-out") {
146
+ issues.push({
147
+ channel: "whatsapp",
148
+ accountId,
149
+ kind: "auth",
150
+ message: `Linked session logged out${lastError ? `: ${lastError}` : "."}`,
151
+ fix: `Run: ${formatCliCommand("autobot channels login")} (scan QR on the gateway host).`,
152
+ });
153
+ return;
154
+ }
155
+
156
+ if (
157
+ linked &&
158
+ running &&
159
+ connected &&
160
+ reconnectAttempts != null &&
161
+ reconnectAttempts > 0 &&
162
+ isRecentDisconnect(lastDisconnect)
163
+ ) {
164
+ issues.push({
165
+ channel: "whatsapp",
166
+ accountId,
167
+ kind: "runtime",
168
+ message: `Linked but recently reconnected (reconnectAttempts=${reconnectAttempts})${lastError ? `: ${lastError}` : "."}`,
169
+ fix: `Watch: ${formatCliCommand("autobot logs --follow")} and run ${formatCliCommand("autobot channels status --probe")} if disconnects continue. If it keeps flapping, restart the gateway or relink via channels login.`,
170
+ });
171
+ return;
172
+ }
173
+
174
+ if (running && !connected) {
175
+ issues.push({
176
+ channel: "whatsapp",
177
+ accountId,
178
+ kind: "runtime",
179
+ message: `Linked but disconnected${reconnectAttempts != null ? ` (reconnectAttempts=${reconnectAttempts})` : ""}${lastError ? `: ${lastError}` : "."}`,
180
+ fix: `Run: ${formatCliCommand("autobot doctor")} (or restart the gateway). If it persists, relink via channels login and check logs.`,
181
+ });
182
+ }
183
+ },
184
+ });
185
+ }
@@ -0,0 +1,31 @@
1
+ export function resolveWhatsAppGroupSystemPrompt(params: {
2
+ accountConfig?: { groups?: Record<string, { systemPrompt?: string | null }> } | null;
3
+ groupId?: string | null;
4
+ }): string | undefined {
5
+ if (!params.groupId) {
6
+ return undefined;
7
+ }
8
+ const groups = params.accountConfig?.groups;
9
+ const specific = groups?.[params.groupId];
10
+ if (specific != null && specific.systemPrompt != null) {
11
+ return specific.systemPrompt.trim() || undefined;
12
+ }
13
+ const wildcard = groups?.["*"]?.systemPrompt;
14
+ return wildcard != null ? wildcard.trim() || undefined : undefined;
15
+ }
16
+
17
+ export function resolveWhatsAppDirectSystemPrompt(params: {
18
+ accountConfig?: { direct?: Record<string, { systemPrompt?: string | null }> } | null;
19
+ peerId?: string | null;
20
+ }): string | undefined {
21
+ if (!params.peerId) {
22
+ return undefined;
23
+ }
24
+ const direct = params.accountConfig?.direct;
25
+ const specific = direct?.[params.peerId];
26
+ if (specific != null && specific.systemPrompt != null) {
27
+ return specific.systemPrompt.trim() || undefined;
28
+ }
29
+ const wildcard = direct?.["*"]?.systemPrompt;
30
+ return wildcard != null ? wildcard.trim() || undefined : undefined;
31
+ }
@@ -0,0 +1,221 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { normalizeE164 } from "autobot/plugin-sdk/account-resolution";
4
+ import { logVerbose, shouldLogVerbose } from "autobot/plugin-sdk/runtime-env";
5
+ import { escapeRegExp } from "autobot/plugin-sdk/text-utility-runtime";
6
+ import { CONFIG_DIR, resolveUserPath } from "autobot/plugin-sdk/text-utility-runtime";
7
+
8
+ const WHATSAPP_FENCE_PLACEHOLDER = "\x00FENCE";
9
+ const WHATSAPP_INLINE_CODE_PLACEHOLDER = "\x00CODE";
10
+
11
+ export type WebChannel = "web";
12
+
13
+ export function assertWebChannel(input: string): asserts input is WebChannel {
14
+ if (input !== "web") {
15
+ throw new Error("Web channel must be 'web'");
16
+ }
17
+ }
18
+
19
+ export function isSelfChatMode(
20
+ selfE164: string | null | undefined,
21
+ allowFrom?: Array<string | number> | null,
22
+ ): boolean {
23
+ if (!selfE164) {
24
+ return false;
25
+ }
26
+ if (!Array.isArray(allowFrom) || allowFrom.length === 0) {
27
+ return false;
28
+ }
29
+ const normalizedSelf = normalizeE164(selfE164);
30
+ return allowFrom.some((n) => {
31
+ if (n === "*") {
32
+ return false;
33
+ }
34
+ try {
35
+ return normalizeE164(String(n)) === normalizedSelf;
36
+ } catch {
37
+ return false;
38
+ }
39
+ });
40
+ }
41
+
42
+ export function toWhatsappJid(number: string): string {
43
+ const withoutPrefix = number.replace(/^whatsapp:/i, "").trim();
44
+ if (withoutPrefix.includes("@")) {
45
+ return withoutPrefix;
46
+ }
47
+ const e164 = normalizeE164(withoutPrefix);
48
+ const digits = e164.replace(/\D/g, "");
49
+ return `${digits}@s.whatsapp.net`;
50
+ }
51
+
52
+ // LID-aware outbound JID resolver. When a forward mapping file
53
+ // `lid-mapping-{phone-digits}.json` is present in any candidate dir, prefer
54
+ // the `{lid}@lid` JID over `{phone-digits}@s.whatsapp.net`. This avoids the
55
+ // ghost-chat failure mode where messages route to a sender-only thread that
56
+ // never reaches recipients whose contact is internally LID-based (#67378).
57
+ export function toWhatsappJidWithLid(number: string, opts?: JidToE164Options): string {
58
+ const stripped = number.replace(/^whatsapp:/i, "").trim();
59
+ if (stripped.includes("@")) {
60
+ return stripped;
61
+ }
62
+ const e164 = normalizeE164(stripped);
63
+ const phoneDigits = e164.replace(/\D/g, "");
64
+ const lid = readLidForwardMapping({ phoneDigits, opts });
65
+ return lid ? `${lid}@lid` : `${phoneDigits}@s.whatsapp.net`;
66
+ }
67
+
68
+ export type JidToE164Options = {
69
+ authDir?: string;
70
+ lidMappingDirs?: string[];
71
+ logMissing?: boolean;
72
+ };
73
+
74
+ type LidLookup = {
75
+ getPNForLID?: (jid: string) => Promise<string | null>;
76
+ };
77
+
78
+ function resolveLidMappingDirs(params: { opts?: JidToE164Options }): string[] {
79
+ const dirs = new Set<string>();
80
+ const addDir = (dir?: string | null) => {
81
+ if (!dir) {
82
+ return;
83
+ }
84
+ dirs.add(resolveUserPath(dir));
85
+ };
86
+ addDir(params.opts?.authDir);
87
+ for (const dir of params.opts?.lidMappingDirs ?? []) {
88
+ addDir(dir);
89
+ }
90
+ addDir(CONFIG_DIR);
91
+ addDir(path.join(CONFIG_DIR, "credentials"));
92
+ return [...dirs];
93
+ }
94
+
95
+ function readLidReverseMapping(params: { lid: string; opts?: JidToE164Options }): string | null {
96
+ const mappingFilename = `lid-mapping-${params.lid}_reverse.json`;
97
+ const mappingDirs = resolveLidMappingDirs({ opts: params.opts });
98
+ for (const dir of mappingDirs) {
99
+ const mappingPath = path.join(dir, mappingFilename);
100
+ try {
101
+ const data = fs.readFileSync(mappingPath, "utf8");
102
+ const phone = JSON.parse(data) as string | number | null;
103
+ if (phone === null || phone === undefined) {
104
+ continue;
105
+ }
106
+ return normalizeE164(String(phone));
107
+ } catch {
108
+ // next location
109
+ }
110
+ }
111
+ return null;
112
+ }
113
+
114
+ function readLidForwardMapping(params: {
115
+ phoneDigits: string;
116
+ opts?: JidToE164Options;
117
+ }): string | null {
118
+ const mappingFilename = `lid-mapping-${params.phoneDigits}.json`;
119
+ const mappingDirs = resolveLidMappingDirs({ opts: params.opts });
120
+ for (const dir of mappingDirs) {
121
+ const mappingPath = path.join(dir, mappingFilename);
122
+ try {
123
+ const data = fs.readFileSync(mappingPath, "utf8");
124
+ const lid = JSON.parse(data) as string | number | null;
125
+ if (lid === null || lid === undefined) {
126
+ continue;
127
+ }
128
+ const digits = String(lid).replace(/\D/g, "");
129
+ if (digits) {
130
+ return digits;
131
+ }
132
+ } catch {
133
+ // next location
134
+ }
135
+ }
136
+ return null;
137
+ }
138
+
139
+ export function jidToE164(jid: string, opts?: JidToE164Options): string | null {
140
+ const match = jid.match(/^(\d+)(?::\d+)?@(s\.whatsapp\.net|hosted)$/);
141
+ if (match) {
142
+ return `+${match[1]}`;
143
+ }
144
+
145
+ const lidMatch = jid.match(/^(\d+)(?::\d+)?@(lid|hosted\.lid)$/);
146
+ if (!lidMatch) {
147
+ return null;
148
+ }
149
+ const phone = readLidReverseMapping({
150
+ lid: lidMatch[1],
151
+ opts,
152
+ });
153
+ if (phone) {
154
+ return phone;
155
+ }
156
+ const shouldLog = opts?.logMissing ?? shouldLogVerbose();
157
+ if (shouldLog) {
158
+ logVerbose(`LID mapping not found for ${lidMatch[1]}; skipping inbound message`);
159
+ }
160
+ return null;
161
+ }
162
+
163
+ export async function resolveJidToE164(
164
+ jid: string | null | undefined,
165
+ opts?: JidToE164Options & { lidLookup?: LidLookup },
166
+ ): Promise<string | null> {
167
+ if (!jid) {
168
+ return null;
169
+ }
170
+ const direct = jidToE164(jid, opts);
171
+ if (direct) {
172
+ return direct;
173
+ }
174
+ if (!/(@lid|@hosted\.lid)$/.test(jid) || !opts?.lidLookup?.getPNForLID) {
175
+ return null;
176
+ }
177
+ try {
178
+ const pnJid = await opts.lidLookup.getPNForLID(jid);
179
+ if (!pnJid) {
180
+ return null;
181
+ }
182
+ return jidToE164(pnJid, opts);
183
+ } catch (err) {
184
+ if (shouldLogVerbose()) {
185
+ logVerbose(`LID mapping lookup failed for ${jid}: ${String(err)}`);
186
+ }
187
+ return null;
188
+ }
189
+ }
190
+
191
+ export function markdownToWhatsApp(text: string): string {
192
+ if (!text) {
193
+ return text;
194
+ }
195
+
196
+ const fences: string[] = [];
197
+ let result = text.replace(/```[\s\S]*?```/g, (match) => {
198
+ fences.push(match);
199
+ return `${WHATSAPP_FENCE_PLACEHOLDER}${fences.length - 1}`;
200
+ });
201
+
202
+ const inlineCodes: string[] = [];
203
+ result = result.replace(/`[^`\n]+`/g, (match) => {
204
+ inlineCodes.push(match);
205
+ return `${WHATSAPP_INLINE_CODE_PLACEHOLDER}${inlineCodes.length - 1}`;
206
+ });
207
+
208
+ result = result.replace(/\*\*(.+?)\*\*/g, "*$1*");
209
+ result = result.replace(/__(.+?)__/g, "*$1*");
210
+ result = result.replace(/~~(.+?)~~/g, "~$1~");
211
+
212
+ result = result.replace(
213
+ new RegExp(`${escapeRegExp(WHATSAPP_INLINE_CODE_PLACEHOLDER)}(\\d+)`, "g"),
214
+ (_, idx) => inlineCodes[Number(idx)] ?? "",
215
+ );
216
+ result = result.replace(
217
+ new RegExp(`${escapeRegExp(WHATSAPP_FENCE_PLACEHOLDER)}(\\d+)`, "g"),
218
+ (_, idx) => fences[Number(idx)] ?? "",
219
+ );
220
+ return result;
221
+ }
@@ -0,0 +1,18 @@
1
+ export {
2
+ convertMarkdownTables,
3
+ sanitizeAssistantVisibleText,
4
+ sanitizeAssistantVisibleTextWithProfile,
5
+ stripToolCallXmlTags,
6
+ } from "autobot/plugin-sdk/text-chunking";
7
+ export { normalizeE164, resolveUserPath, sleep } from "autobot/plugin-sdk/text-utility-runtime";
8
+ export {
9
+ assertWebChannel,
10
+ isSelfChatMode,
11
+ jidToE164,
12
+ markdownToWhatsApp,
13
+ resolveJidToE164,
14
+ toWhatsappJid,
15
+ toWhatsappJidWithLid,
16
+ type JidToE164Options,
17
+ type WebChannel,
18
+ } from "./targets-runtime.js";
package/src/vcard.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
2
+
3
+ type ParsedVcard = {
4
+ name?: string;
5
+ phones: string[];
6
+ };
7
+
8
+ const ALLOWED_VCARD_KEYS = new Set(["FN", "N", "TEL"]);
9
+
10
+ export function parseVcard(vcard?: string): ParsedVcard {
11
+ if (!vcard) {
12
+ return { phones: [] };
13
+ }
14
+ const lines = vcard.split(/\r?\n/);
15
+ let nameFromN: string | undefined;
16
+ let nameFromFn: string | undefined;
17
+ const phones: string[] = [];
18
+ for (const rawLine of lines) {
19
+ const line = rawLine.trim();
20
+ if (!line) {
21
+ continue;
22
+ }
23
+ const colonIndex = line.indexOf(":");
24
+ if (colonIndex === -1) {
25
+ continue;
26
+ }
27
+ const key = line.slice(0, colonIndex).toUpperCase();
28
+ const rawValue = line.slice(colonIndex + 1).trim();
29
+ if (!rawValue) {
30
+ continue;
31
+ }
32
+ const baseKey = normalizeVcardKey(key);
33
+ if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) {
34
+ continue;
35
+ }
36
+ const value = cleanVcardValue(rawValue);
37
+ if (!value) {
38
+ continue;
39
+ }
40
+ if (baseKey === "FN" && !nameFromFn) {
41
+ nameFromFn = normalizeVcardName(value);
42
+ continue;
43
+ }
44
+ if (baseKey === "N" && !nameFromN) {
45
+ nameFromN = normalizeVcardName(value);
46
+ continue;
47
+ }
48
+ if (baseKey === "TEL") {
49
+ const phone = normalizeVcardPhone(value);
50
+ if (phone) {
51
+ phones.push(phone);
52
+ }
53
+ }
54
+ }
55
+ return { name: nameFromFn ?? nameFromN, phones };
56
+ }
57
+
58
+ function normalizeVcardKey(key: string): string | undefined {
59
+ const [primary] = key.split(";");
60
+ if (!primary) {
61
+ return undefined;
62
+ }
63
+ const segments = primary.split(".");
64
+ return segments[segments.length - 1] || undefined;
65
+ }
66
+
67
+ function cleanVcardValue(value: string): string {
68
+ return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
69
+ }
70
+
71
+ function normalizeVcardName(value: string): string {
72
+ return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
73
+ }
74
+
75
+ function normalizeVcardPhone(value: string): string {
76
+ const trimmed = value.trim();
77
+ if (!trimmed) {
78
+ return "";
79
+ }
80
+ if (normalizeLowercaseStringOrEmpty(trimmed).startsWith("tel:")) {
81
+ return trimmed.slice(4).trim();
82
+ }
83
+ return trimmed;
84
+ }
package/targets.ts ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ isWhatsAppGroupJid,
3
+ isWhatsAppUserTarget,
4
+ normalizeWhatsAppTarget,
5
+ } from "./src/normalize-target.js";
package/test-api.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { whatsappOutbound } from "./src/outbound-adapter.js";
2
+ export { resolveWhatsAppRuntimeGroupPolicy } from "./src/runtime-group-policy.js";
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "../tsconfig.package-boundary.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "."
5
+ },
6
+ "include": ["./*.ts", "./src/**/*.ts"],
7
+ "exclude": [
8
+ "./**/*.test.ts",
9
+ "./dist/**",
10
+ "./node_modules/**",
11
+ "./src/test-support/**",
12
+ "./src/**/*test-helpers.ts",
13
+ "./src/**/*test-harness.ts",
14
+ "./src/**/*test-support.ts"
15
+ ]
16
+ }