@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.
- package/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +67 -0
- package/auth-presence.ts +80 -0
- package/autobot.plugin.json +23 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/doctor-contract-api.ts +8 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +76 -0
- package/runtime-api.ts +84 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +17 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.ts +176 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.ts +494 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.ts +332 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +218 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/listener-log.ts +28 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.ts +333 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.ts +584 -0
- package/src/auto-reply/monitor/runtime-api.ts +36 -0
- package/src/auto-reply/monitor/status-reaction.ts +108 -0
- package/src/auto-reply/monitor-state.ts +114 -0
- package/src/auto-reply/monitor.ts +720 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +62 -0
- package/src/auto-reply.impl.ts +6 -0
- package/src/auto-reply.ts +1 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.ts +85 -0
- package/src/channel-outbound.ts +87 -0
- package/src/channel-react-action.runtime.ts +10 -0
- package/src/channel-react-action.ts +247 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.ts +32 -0
- package/src/channel.ts +356 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.ts +680 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +71 -0
- package/src/directory-config.ts +40 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.ts +56 -0
- package/src/document-filename.ts +17 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.ts +42 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.ts +187 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.ts +484 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.ts +128 -0
- package/src/inbound/monitor.ts +1042 -0
- package/src/inbound/outbound-mentions.ts +260 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.ts +203 -0
- package/src/inbound/send-result.ts +109 -0
- package/src/inbound/types.ts +107 -0
- package/src/inbound-policy.ts +215 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.ts +542 -0
- package/src/login.ts +83 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
- package/src/normalize-target.ts +148 -0
- package/src/normalize.ts +8 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.ts +256 -0
- package/src/outbound-media-contract.ts +307 -0
- package/src/outbound-media.runtime.ts +41 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/qa-driver.runtime.ts +189 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.ts +24 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.ts +58 -0
- package/src/runtime-api.ts +59 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.ts +342 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +32 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.ts +327 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +450 -0
- package/src/setup-surface.ts +71 -0
- package/src/setup-test-helpers.ts +217 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +55 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +221 -0
- package/src/text-runtime.ts +18 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { MiscMessageGenerationOptions } from "baileys";
|
|
2
|
+
import { jidToE164 } from "./text-runtime.js";
|
|
3
|
+
|
|
4
|
+
// ── Inbound message metadata cache ──────────────────────────────────────
|
|
5
|
+
// Maps messageId → { participant, participantE164, body, fromMe } so the
|
|
6
|
+
// outbound adapter can
|
|
7
|
+
// populate the quote key with the sender JID and preview text even though
|
|
8
|
+
// the outbound path only receives a bare messageId string.
|
|
9
|
+
|
|
10
|
+
type QuotedMeta = {
|
|
11
|
+
participant?: string;
|
|
12
|
+
participantE164?: string;
|
|
13
|
+
body?: string;
|
|
14
|
+
fromMe?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type CacheEntry = QuotedMeta & { ts: number };
|
|
17
|
+
type QuotedMetaLookup = QuotedMeta & { remoteJid: string };
|
|
18
|
+
|
|
19
|
+
const CACHE_TTL_MS = 10 * 60 * 1000;
|
|
20
|
+
const MAX_ENTRIES = 500;
|
|
21
|
+
const cache = new Map<string, CacheEntry>();
|
|
22
|
+
|
|
23
|
+
function makeCacheKey(accountId: string, remoteJid: string, messageId: string): string {
|
|
24
|
+
return `${accountId}:${remoteJid}:${messageId}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function cacheInboundMessageMeta(
|
|
28
|
+
accountId: string,
|
|
29
|
+
remoteJid: string,
|
|
30
|
+
messageId: string,
|
|
31
|
+
meta: QuotedMeta,
|
|
32
|
+
): void {
|
|
33
|
+
if (!accountId || !messageId || !remoteJid) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (cache.size >= MAX_ENTRIES) {
|
|
37
|
+
const oldest = cache.keys().next().value;
|
|
38
|
+
if (oldest) {
|
|
39
|
+
cache.delete(oldest);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
cache.set(makeCacheKey(accountId, remoteJid, messageId), { ...meta, ts: Date.now() });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function lookupInboundMessageMeta(
|
|
46
|
+
accountId: string,
|
|
47
|
+
remoteJid: string,
|
|
48
|
+
messageId: string,
|
|
49
|
+
): QuotedMeta | undefined {
|
|
50
|
+
const cacheKey = makeCacheKey(accountId, remoteJid, messageId);
|
|
51
|
+
const entry = cache.get(cacheKey);
|
|
52
|
+
if (!entry) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
if (Date.now() - entry.ts > CACHE_TTL_MS) {
|
|
56
|
+
cache.delete(cacheKey);
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
participant: entry.participant,
|
|
61
|
+
participantE164: entry.participantE164,
|
|
62
|
+
body: entry.body,
|
|
63
|
+
fromMe: entry.fromMe,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function normalizeComparableJid(jid: string | undefined): string | undefined {
|
|
68
|
+
const normalized = jid?.trim().replace(/:\d+/, "").toLowerCase();
|
|
69
|
+
return normalized || undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isGroupJid(jid: string | undefined): boolean {
|
|
73
|
+
return Boolean(jid && jid.endsWith("@g.us"));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function areComparableE164sEqual(left: string | undefined, right: string | undefined): boolean {
|
|
77
|
+
const normalizedLeft = left?.trim();
|
|
78
|
+
const normalizedRight = right?.trim();
|
|
79
|
+
if (!normalizedLeft || !normalizedRight) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return normalizedLeft === normalizedRight;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function areComparableJidsEqual(left: string | undefined, right: string | undefined): boolean {
|
|
86
|
+
const normalizedLeft = normalizeComparableJid(left);
|
|
87
|
+
const normalizedRight = normalizeComparableJid(right);
|
|
88
|
+
if (!normalizedLeft || !normalizedRight) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (normalizedLeft === normalizedRight) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
const leftE164 = jidToE164(normalizedLeft);
|
|
95
|
+
const rightE164 = jidToE164(normalizedRight);
|
|
96
|
+
return Boolean(leftE164 && rightE164 && leftE164 === rightE164);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function matchesQuotedConversationTarget(targetJid: string, candidate: QuotedMetaLookup): boolean {
|
|
100
|
+
if (areComparableJidsEqual(targetJid, candidate.remoteJid)) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (isGroupJid(targetJid) || isGroupJid(candidate.remoteJid)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return (
|
|
107
|
+
areComparableJidsEqual(targetJid, candidate.participant) ||
|
|
108
|
+
areComparableE164sEqual(jidToE164(targetJid) ?? undefined, candidate.participantE164)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function lookupInboundMessageMetaForTarget(
|
|
113
|
+
accountId: string,
|
|
114
|
+
targetJid: string,
|
|
115
|
+
messageId: string,
|
|
116
|
+
): QuotedMetaLookup | undefined {
|
|
117
|
+
if (!accountId || !messageId || !targetJid) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const exact = lookupInboundMessageMeta(accountId, targetJid, messageId);
|
|
121
|
+
if (exact) {
|
|
122
|
+
return {
|
|
123
|
+
remoteJid: targetJid,
|
|
124
|
+
participant: exact.participant,
|
|
125
|
+
participantE164: exact.participantE164,
|
|
126
|
+
body: exact.body,
|
|
127
|
+
fromMe: exact.fromMe,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const prefix = `${accountId}:`;
|
|
131
|
+
const suffix = `:${messageId}`;
|
|
132
|
+
let matched: QuotedMetaLookup | undefined;
|
|
133
|
+
for (const [cacheKey, entry] of cache.entries()) {
|
|
134
|
+
if (!cacheKey.startsWith(prefix) || !cacheKey.endsWith(suffix)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (Date.now() - entry.ts > CACHE_TTL_MS) {
|
|
138
|
+
cache.delete(cacheKey);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const remoteJid = cacheKey.slice(prefix.length, cacheKey.length - suffix.length);
|
|
142
|
+
const candidate = {
|
|
143
|
+
remoteJid,
|
|
144
|
+
participant: entry.participant,
|
|
145
|
+
participantE164: entry.participantE164,
|
|
146
|
+
body: entry.body,
|
|
147
|
+
fromMe: entry.fromMe,
|
|
148
|
+
};
|
|
149
|
+
if (!matchesQuotedConversationTarget(targetJid, candidate)) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (matched) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
matched = candidate;
|
|
156
|
+
}
|
|
157
|
+
return matched;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function buildQuotedMessageOptions(params: {
|
|
161
|
+
messageId?: string | null;
|
|
162
|
+
remoteJid?: string | null;
|
|
163
|
+
fromMe?: boolean;
|
|
164
|
+
participant?: string;
|
|
165
|
+
/** Original message text — shown in the quote preview bubble. */
|
|
166
|
+
messageText?: string;
|
|
167
|
+
}): MiscMessageGenerationOptions | undefined {
|
|
168
|
+
const id = params.messageId?.trim();
|
|
169
|
+
const remoteJid = params.remoteJid?.trim();
|
|
170
|
+
if (!id || !remoteJid) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
quoted: {
|
|
175
|
+
key: {
|
|
176
|
+
remoteJid,
|
|
177
|
+
id,
|
|
178
|
+
fromMe: params.fromMe ?? false,
|
|
179
|
+
participant: params.participant,
|
|
180
|
+
},
|
|
181
|
+
message: { conversation: params.messageText ?? "" },
|
|
182
|
+
},
|
|
183
|
+
} as MiscMessageGenerationOptions;
|
|
184
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
2
|
+
import {
|
|
3
|
+
resolveReactionLevel,
|
|
4
|
+
type ResolvedReactionLevel,
|
|
5
|
+
} from "autobot/plugin-sdk/status-helpers";
|
|
6
|
+
import { resolveMergedWhatsAppAccountConfig } from "./account-config.js";
|
|
7
|
+
|
|
8
|
+
type ResolvedWhatsAppReactionLevel = ResolvedReactionLevel;
|
|
9
|
+
|
|
10
|
+
/** Resolve the effective reaction level and its implications for WhatsApp. */
|
|
11
|
+
export function resolveWhatsAppReactionLevel(params: {
|
|
12
|
+
cfg: AutoBotConfig;
|
|
13
|
+
accountId?: string;
|
|
14
|
+
}): ResolvedWhatsAppReactionLevel {
|
|
15
|
+
const account = resolveMergedWhatsAppAccountConfig({
|
|
16
|
+
cfg: params.cfg,
|
|
17
|
+
accountId: params.accountId,
|
|
18
|
+
});
|
|
19
|
+
return resolveReactionLevel({
|
|
20
|
+
value: account.reactionLevel,
|
|
21
|
+
defaultLevel: "minimal",
|
|
22
|
+
invalidFallback: "minimal",
|
|
23
|
+
});
|
|
24
|
+
}
|
package/src/reconnect.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
3
|
+
import {
|
|
4
|
+
computeBackoff,
|
|
5
|
+
sleepWithAbort,
|
|
6
|
+
type BackoffPolicy,
|
|
7
|
+
} from "autobot/plugin-sdk/runtime-env";
|
|
8
|
+
import { clamp } from "autobot/plugin-sdk/text-utility-runtime";
|
|
9
|
+
|
|
10
|
+
export type ReconnectPolicy = BackoffPolicy & {
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_HEARTBEAT_SECONDS = 60;
|
|
15
|
+
export const DEFAULT_RECONNECT_POLICY: ReconnectPolicy = {
|
|
16
|
+
initialMs: 2_000,
|
|
17
|
+
maxMs: 30_000,
|
|
18
|
+
factor: 1.8,
|
|
19
|
+
jitter: 0.25,
|
|
20
|
+
maxAttempts: 12,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function resolveHeartbeatSeconds(cfg: AutoBotConfig, overrideSeconds?: number): number {
|
|
24
|
+
const candidate = overrideSeconds ?? cfg.web?.heartbeatSeconds;
|
|
25
|
+
if (typeof candidate === "number" && candidate > 0) {
|
|
26
|
+
return candidate;
|
|
27
|
+
}
|
|
28
|
+
return DEFAULT_HEARTBEAT_SECONDS;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveReconnectPolicy(
|
|
32
|
+
cfg: AutoBotConfig,
|
|
33
|
+
overrides?: Partial<ReconnectPolicy>,
|
|
34
|
+
): ReconnectPolicy {
|
|
35
|
+
const reconnectOverrides = cfg.web?.reconnect ?? {};
|
|
36
|
+
const overrideConfig = overrides ?? {};
|
|
37
|
+
const merged = {
|
|
38
|
+
...DEFAULT_RECONNECT_POLICY,
|
|
39
|
+
...reconnectOverrides,
|
|
40
|
+
...overrideConfig,
|
|
41
|
+
} as ReconnectPolicy;
|
|
42
|
+
|
|
43
|
+
merged.initialMs = Math.max(250, merged.initialMs);
|
|
44
|
+
merged.maxMs = Math.max(merged.initialMs, merged.maxMs);
|
|
45
|
+
merged.factor = clamp(merged.factor, 1.1, 10);
|
|
46
|
+
merged.jitter = clamp(merged.jitter, 0, 1);
|
|
47
|
+
merged.maxAttempts = Math.max(0, Math.floor(merged.maxAttempts));
|
|
48
|
+
return merged;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { computeBackoff, sleepWithAbort };
|
|
52
|
+
|
|
53
|
+
export function newConnectionId() {
|
|
54
|
+
return randomUUID();
|
|
55
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { missingTargetError } from "autobot/plugin-sdk/channel-feedback";
|
|
2
|
+
import {
|
|
3
|
+
isWhatsAppGroupJid,
|
|
4
|
+
isWhatsAppNewsletterJid,
|
|
5
|
+
normalizeWhatsAppTarget,
|
|
6
|
+
} from "./normalize-target.js";
|
|
7
|
+
|
|
8
|
+
export type WhatsAppOutboundTargetResolution =
|
|
9
|
+
| { ok: true; to: string }
|
|
10
|
+
| { ok: false; error: Error };
|
|
11
|
+
|
|
12
|
+
function whatsappAllowFromPolicyError(target: string): Error {
|
|
13
|
+
return new Error(`Target "${target}" is not listed in the configured WhatsApp allowFrom policy.`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function resolveWhatsAppOutboundTarget(params: {
|
|
17
|
+
to: string | null | undefined;
|
|
18
|
+
allowFrom: Array<string | number> | null | undefined;
|
|
19
|
+
mode: string | null | undefined;
|
|
20
|
+
}): WhatsAppOutboundTargetResolution {
|
|
21
|
+
const trimmed = params.to?.trim() ?? "";
|
|
22
|
+
if (!trimmed) {
|
|
23
|
+
return {
|
|
24
|
+
ok: false,
|
|
25
|
+
error: missingTargetError("WhatsApp", "<E.164|group JID|newsletter JID>"),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
|
30
|
+
if (!normalizedTo) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: missingTargetError("WhatsApp", "<E.164|group JID|newsletter JID>"),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (isWhatsAppGroupJid(normalizedTo) || isWhatsAppNewsletterJid(normalizedTo)) {
|
|
37
|
+
return { ok: true, to: normalizedTo };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const allowListRaw = (params.allowFrom ?? [])
|
|
41
|
+
.map((entry) => String(entry).trim())
|
|
42
|
+
.filter(Boolean);
|
|
43
|
+
const hasWildcard = allowListRaw.includes("*");
|
|
44
|
+
const allowList = allowListRaw
|
|
45
|
+
.filter((entry) => entry !== "*")
|
|
46
|
+
.map((entry) => normalizeWhatsAppTarget(entry))
|
|
47
|
+
.filter((entry): entry is string => Boolean(entry));
|
|
48
|
+
if (hasWildcard || allowList.length === 0) {
|
|
49
|
+
return { ok: true, to: normalizedTo };
|
|
50
|
+
}
|
|
51
|
+
if (allowList.includes(normalizedTo)) {
|
|
52
|
+
return { ok: true, to: normalizedTo };
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
error: whatsappAllowFromPolicyError(normalizedTo),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export { getChatChannelMeta, type ChannelPlugin } from "autobot/plugin-sdk/core";
|
|
2
|
+
export { buildChannelConfigSchema, WhatsAppConfigSchema } from "../config-api.js";
|
|
3
|
+
export { DEFAULT_ACCOUNT_ID } from "autobot/plugin-sdk/account-id";
|
|
4
|
+
export {
|
|
5
|
+
formatWhatsAppConfigAllowFromEntries,
|
|
6
|
+
resolveWhatsAppConfigAllowFrom,
|
|
7
|
+
resolveWhatsAppConfigDefaultTo,
|
|
8
|
+
} from "./config-accessors.js";
|
|
9
|
+
export {
|
|
10
|
+
createActionGate,
|
|
11
|
+
jsonResult,
|
|
12
|
+
readReactionParams,
|
|
13
|
+
readStringParam,
|
|
14
|
+
ToolAuthorizationError,
|
|
15
|
+
} from "autobot/plugin-sdk/channel-actions";
|
|
16
|
+
export { normalizeE164 } from "autobot/plugin-sdk/account-resolution";
|
|
17
|
+
export type { DmPolicy, GroupPolicy } from "autobot/plugin-sdk/config-contracts";
|
|
18
|
+
import type { AutoBotConfig as RuntimeAutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
19
|
+
|
|
20
|
+
export { type ChannelMessageActionName } from "autobot/plugin-sdk/channel-contract";
|
|
21
|
+
export { loadOutboundMediaFromUrl } from "./outbound-media.runtime.js";
|
|
22
|
+
export {
|
|
23
|
+
resolveWhatsAppGroupRequireMention,
|
|
24
|
+
resolveWhatsAppGroupToolPolicy,
|
|
25
|
+
} from "./group-policy.js";
|
|
26
|
+
export {
|
|
27
|
+
resolveWhatsAppGroupIntroHint,
|
|
28
|
+
resolveWhatsAppMentionStripRegexes,
|
|
29
|
+
} from "./group-intro.js";
|
|
30
|
+
export { createWhatsAppOutboundBase } from "./outbound-base.js";
|
|
31
|
+
export {
|
|
32
|
+
isWhatsAppGroupJid,
|
|
33
|
+
isWhatsAppUserTarget,
|
|
34
|
+
looksLikeWhatsAppTargetId,
|
|
35
|
+
normalizeWhatsAppAllowFromEntries,
|
|
36
|
+
normalizeWhatsAppMessagingTarget,
|
|
37
|
+
normalizeWhatsAppTarget,
|
|
38
|
+
} from "./normalize-target.js";
|
|
39
|
+
export { resolveWhatsAppOutboundTarget } from "./resolve-outbound-target.js";
|
|
40
|
+
export { resolveWhatsAppReactionLevel } from "./reaction-level.js";
|
|
41
|
+
|
|
42
|
+
export type AutoBotConfig = RuntimeAutoBotConfig;
|
|
43
|
+
export type { WhatsAppAccountConfig } from "./account-types.js";
|
|
44
|
+
|
|
45
|
+
type MonitorWebChannel = typeof import("./channel.runtime.js").monitorWebChannel;
|
|
46
|
+
|
|
47
|
+
let channelRuntimePromise: Promise<typeof import("./channel.runtime.js")> | null = null;
|
|
48
|
+
|
|
49
|
+
function loadChannelRuntime() {
|
|
50
|
+
channelRuntimePromise ??= import("./channel.runtime.js");
|
|
51
|
+
return channelRuntimePromise;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function monitorWebChannel(
|
|
55
|
+
...args: Parameters<MonitorWebChannel>
|
|
56
|
+
): ReturnType<MonitorWebChannel> {
|
|
57
|
+
const { monitorWebChannel } = await loadChannelRuntime();
|
|
58
|
+
return await monitorWebChannel(...args);
|
|
59
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { resolveOpenProviderRuntimeGroupPolicy } from "autobot/plugin-sdk/runtime-group-policy";
|
|
2
|
+
|
|
3
|
+
export function resolveWhatsAppRuntimeGroupPolicy(params: {
|
|
4
|
+
providerConfigPresent: boolean;
|
|
5
|
+
groupPolicy?: "open" | "allowlist" | "disabled";
|
|
6
|
+
defaultGroupPolicy?: "open" | "allowlist" | "disabled";
|
|
7
|
+
}): {
|
|
8
|
+
groupPolicy: "open" | "allowlist" | "disabled";
|
|
9
|
+
providerMissingFallbackApplied: boolean;
|
|
10
|
+
} {
|
|
11
|
+
return resolveOpenProviderRuntimeGroupPolicy({
|
|
12
|
+
providerConfigPresent: params.providerConfigPresent,
|
|
13
|
+
groupPolicy: params.groupPolicy,
|
|
14
|
+
defaultGroupPolicy: params.defaultGroupPolicy,
|
|
15
|
+
});
|
|
16
|
+
}
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PluginRuntime } from "autobot/plugin-sdk/core";
|
|
2
|
+
import { createPluginRuntimeStore } from "autobot/plugin-sdk/runtime-store";
|
|
3
|
+
|
|
4
|
+
const { setRuntime: setWhatsAppRuntime, getRuntime: getWhatsAppRuntime } =
|
|
5
|
+
createPluginRuntimeStore<PluginRuntime>({
|
|
6
|
+
pluginId: "whatsapp",
|
|
7
|
+
errorMessage: "WhatsApp runtime not initialized",
|
|
8
|
+
});
|
|
9
|
+
export { getWhatsAppRuntime, setWhatsAppRuntime };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { isRecord } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
|
|
3
|
+
type UnsupportedSecretRefConfigCandidate = {
|
|
4
|
+
path: string;
|
|
5
|
+
value: unknown;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const unsupportedSecretRefSurfacePatterns = [
|
|
9
|
+
"channels.whatsapp.creds.json",
|
|
10
|
+
"channels.whatsapp.accounts.*.creds.json",
|
|
11
|
+
] as const;
|
|
12
|
+
|
|
13
|
+
export function collectUnsupportedSecretRefConfigCandidates(
|
|
14
|
+
raw: unknown,
|
|
15
|
+
): UnsupportedSecretRefConfigCandidate[] {
|
|
16
|
+
if (!isRecord(raw)) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
if (!isRecord(raw.channels) || !isRecord(raw.channels.whatsapp)) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const candidates: UnsupportedSecretRefConfigCandidate[] = [];
|
|
24
|
+
const whatsapp = raw.channels.whatsapp;
|
|
25
|
+
const creds = isRecord(whatsapp.creds) ? whatsapp.creds : null;
|
|
26
|
+
if (creds) {
|
|
27
|
+
candidates.push({
|
|
28
|
+
path: "channels.whatsapp.creds.json",
|
|
29
|
+
value: creds.json,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const accounts = isRecord(whatsapp.accounts) ? whatsapp.accounts : null;
|
|
34
|
+
if (!accounts) {
|
|
35
|
+
return candidates;
|
|
36
|
+
}
|
|
37
|
+
for (const [accountId, account] of Object.entries(accounts)) {
|
|
38
|
+
if (!isRecord(account) || !isRecord(account.creds)) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
candidates.push({
|
|
42
|
+
path: `channels.whatsapp.accounts.${accountId}.creds.json`,
|
|
43
|
+
value: account.creds.json,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return candidates;
|
|
47
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DEFAULT_ACCOUNT_ID } from "autobot/plugin-sdk/account-id";
|
|
2
|
+
import type { ChannelDoctorConfigMutation } from "autobot/plugin-sdk/channel-contract";
|
|
3
|
+
import { readChannelAllowFromStore } from "autobot/plugin-sdk/channel-pairing";
|
|
4
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
5
|
+
|
|
6
|
+
function applyGroupAllowFromFromStore(params: {
|
|
7
|
+
cfg: AutoBotConfig;
|
|
8
|
+
storeAllowFrom: string[];
|
|
9
|
+
changes: string[];
|
|
10
|
+
}): AutoBotConfig {
|
|
11
|
+
const next = structuredClone(params.cfg ?? {});
|
|
12
|
+
const section = next.channels?.whatsapp as Record<string, unknown> | undefined;
|
|
13
|
+
if (!section || typeof section !== "object" || params.storeAllowFrom.length === 0) {
|
|
14
|
+
return params.cfg;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let changed = false;
|
|
18
|
+
const maybeApply = (prefix: string, holder: Record<string, unknown>) => {
|
|
19
|
+
if (holder.groupPolicy !== "allowlist") {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const allowFrom = Array.isArray(holder.allowFrom) ? holder.allowFrom : [];
|
|
23
|
+
const groupAllowFrom = Array.isArray(holder.groupAllowFrom) ? holder.groupAllowFrom : [];
|
|
24
|
+
if (allowFrom.length > 0 || groupAllowFrom.length > 0) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
holder.groupAllowFrom = params.storeAllowFrom;
|
|
28
|
+
params.changes.push(`${prefix}groupAllowFrom=pairing-store`);
|
|
29
|
+
changed = true;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
maybeApply("channels.whatsapp.", section);
|
|
33
|
+
|
|
34
|
+
const accounts = section.accounts;
|
|
35
|
+
if (accounts && typeof accounts === "object") {
|
|
36
|
+
for (const [accountId, accountValue] of Object.entries(accounts)) {
|
|
37
|
+
if (!accountValue || typeof accountValue !== "object") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
maybeApply(
|
|
41
|
+
`channels.whatsapp.accounts.${accountId}.`,
|
|
42
|
+
accountValue as Record<string, unknown>,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return changed ? next : params.cfg;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function applyWhatsAppSecurityConfigFixes(params: {
|
|
51
|
+
cfg: AutoBotConfig;
|
|
52
|
+
env: NodeJS.ProcessEnv;
|
|
53
|
+
}): Promise<ChannelDoctorConfigMutation> {
|
|
54
|
+
const fromStore = await readChannelAllowFromStore(
|
|
55
|
+
"whatsapp",
|
|
56
|
+
params.env,
|
|
57
|
+
DEFAULT_ACCOUNT_ID,
|
|
58
|
+
).catch(() => []);
|
|
59
|
+
const normalized = Array.from(new Set(fromStore.map((entry) => entry.trim()))).filter(Boolean);
|
|
60
|
+
if (normalized.length === 0) {
|
|
61
|
+
return { config: params.cfg, changes: [] };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const changes: string[] = [];
|
|
65
|
+
const config = applyGroupAllowFromFromStore({
|
|
66
|
+
cfg: params.cfg,
|
|
67
|
+
storeAllowFrom: normalized,
|
|
68
|
+
changes,
|
|
69
|
+
});
|
|
70
|
+
return { config, changes };
|
|
71
|
+
}
|