@conversionpros/aiva 1.0.0 → 2.0.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 (150) hide show
  1. package/bin/aiva.js +26 -14
  2. package/lib/bluebubbles.js +145 -0
  3. package/lib/config-gen.js +253 -0
  4. package/lib/config.js +1 -1
  5. package/lib/constants.js +72 -0
  6. package/lib/launch-agent.js +112 -0
  7. package/lib/prerequisites.js +236 -0
  8. package/lib/process.js +59 -145
  9. package/lib/setup.js +224 -194
  10. package/lib/validate.js +194 -0
  11. package/package.json +7 -32
  12. package/auto-deploy.js +0 -190
  13. package/cli-sync.js +0 -126
  14. package/d2a-prompt-template.txt +0 -106
  15. package/diagnostics-api.js +0 -304
  16. package/docs/ara-dedup-fix-scope.md +0 -112
  17. package/docs/ara-fix-round2-scope.md +0 -61
  18. package/docs/ara-greeting-fix-scope.md +0 -70
  19. package/docs/calendar-date-fix-scope.md +0 -28
  20. package/docs/getting-started.md +0 -115
  21. package/docs/network-architecture-rollout-scope.md +0 -43
  22. package/docs/scope-google-oauth-integration.md +0 -351
  23. package/docs/settings-page-scope.md +0 -50
  24. package/docs/xai-imagine-scope.md +0 -116
  25. package/docs/xai-voice-integration-scope.md +0 -115
  26. package/docs/xai-voice-tools-scope.md +0 -165
  27. package/email-router.js +0 -512
  28. package/follow-up-handler.js +0 -606
  29. package/gateway-monitor.js +0 -158
  30. package/google-email.js +0 -379
  31. package/google-oauth.js +0 -310
  32. package/grok-imagine.js +0 -97
  33. package/health-reporter.js +0 -287
  34. package/invisible-prefix-base.txt +0 -206
  35. package/invisible-prefix-owner.txt +0 -26
  36. package/invisible-prefix-slim.txt +0 -10
  37. package/invisible-prefix.txt +0 -43
  38. package/knowledge-base.js +0 -472
  39. package/lib/cli.js +0 -19
  40. package/lib/server.js +0 -42
  41. package/meta-capi.js +0 -206
  42. package/meta-leads.js +0 -411
  43. package/notion-oauth.js +0 -323
  44. package/public/agent-config.html +0 -241
  45. package/public/aiva-avatar-anime.png +0 -0
  46. package/public/css/docs.css.bak +0 -688
  47. package/public/css/onboarding.css +0 -543
  48. package/public/diagrams/claude-subscription-pool.html +0 -329
  49. package/public/diagrams/claude-subscription-pool.png +0 -0
  50. package/public/docs-icon.png +0 -0
  51. package/public/escalation.html +0 -237
  52. package/public/group-config.html +0 -300
  53. package/public/icon-192.png +0 -0
  54. package/public/icon-512.png +0 -0
  55. package/public/icons/agents.svg +0 -1
  56. package/public/icons/attach.svg +0 -1
  57. package/public/icons/characters.svg +0 -1
  58. package/public/icons/chat.svg +0 -1
  59. package/public/icons/docs.svg +0 -1
  60. package/public/icons/heartbeat.svg +0 -1
  61. package/public/icons/messages.svg +0 -1
  62. package/public/icons/mic.svg +0 -1
  63. package/public/icons/notes.svg +0 -1
  64. package/public/icons/settings.svg +0 -1
  65. package/public/icons/tasks.svg +0 -1
  66. package/public/images/onboarding/p0-communication-layer.png +0 -0
  67. package/public/images/onboarding/p0-infinite-surface.png +0 -0
  68. package/public/images/onboarding/p0-learning-model.png +0 -0
  69. package/public/images/onboarding/p0-meet-aiva.png +0 -0
  70. package/public/images/onboarding/p4-contact-intelligence.png +0 -0
  71. package/public/images/onboarding/p4-context-compounds.png +0 -0
  72. package/public/images/onboarding/p4-message-router.png +0 -0
  73. package/public/images/onboarding/p4-per-contact-rules.png +0 -0
  74. package/public/images/onboarding/p4-send-messages.png +0 -0
  75. package/public/images/onboarding/p6-be-precise.png +0 -0
  76. package/public/images/onboarding/p6-review-escalations.png +0 -0
  77. package/public/images/onboarding/p6-voice-input.png +0 -0
  78. package/public/images/onboarding/p7-completion.png +0 -0
  79. package/public/index.html +0 -11594
  80. package/public/js/onboarding.js +0 -699
  81. package/public/manifest.json +0 -24
  82. package/public/messages-v2.html +0 -2824
  83. package/public/permission-approve.html.bak +0 -107
  84. package/public/permissions.html +0 -150
  85. package/public/styles/design-system.css +0 -68
  86. package/router-db.js +0 -604
  87. package/router-utils.js +0 -28
  88. package/router-v2/adapters/imessage.js +0 -191
  89. package/router-v2/adapters/quo.js +0 -82
  90. package/router-v2/adapters/whatsapp.js +0 -192
  91. package/router-v2/contact-manager.js +0 -234
  92. package/router-v2/conversation-engine.js +0 -498
  93. package/router-v2/data/knowledge-base.json +0 -176
  94. package/router-v2/data/router-v2.db +0 -0
  95. package/router-v2/data/router-v2.db-shm +0 -0
  96. package/router-v2/data/router-v2.db-wal +0 -0
  97. package/router-v2/data/router.db +0 -0
  98. package/router-v2/db.js +0 -457
  99. package/router-v2/escalation-bridge.js +0 -540
  100. package/router-v2/follow-up-engine.js +0 -347
  101. package/router-v2/index.js +0 -441
  102. package/router-v2/ingestion.js +0 -213
  103. package/router-v2/knowledge-base.js +0 -231
  104. package/router-v2/lead-qualifier.js +0 -152
  105. package/router-v2/learning-loop.js +0 -202
  106. package/router-v2/outbound-sender.js +0 -160
  107. package/router-v2/package.json +0 -13
  108. package/router-v2/permission-gate.js +0 -86
  109. package/router-v2/playbook.js +0 -177
  110. package/router-v2/prompts/base.js +0 -52
  111. package/router-v2/prompts/first-contact.js +0 -38
  112. package/router-v2/prompts/lead-qualification.js +0 -37
  113. package/router-v2/prompts/scheduling.js +0 -72
  114. package/router-v2/prompts/style-overrides.js +0 -22
  115. package/router-v2/scheduler.js +0 -301
  116. package/router-v2/scripts/migrate-v1-to-v2.js +0 -215
  117. package/router-v2/scripts/seed-faq.js +0 -67
  118. package/router-v2/seed-knowledge-base.js +0 -39
  119. package/router-v2/utils/ai.js +0 -129
  120. package/router-v2/utils/phone.js +0 -52
  121. package/router-v2/utils/response-validator.js +0 -98
  122. package/router-v2/utils/sanitize.js +0 -222
  123. package/router.js +0 -5005
  124. package/routes/google-calendar.js +0 -186
  125. package/scripts/deploy.sh +0 -62
  126. package/scripts/macos-calendar.sh +0 -232
  127. package/scripts/onboard-device.sh +0 -466
  128. package/server.js +0 -5131
  129. package/start.sh +0 -24
  130. package/templates/AGENTS.md +0 -548
  131. package/templates/IDENTITY.md +0 -15
  132. package/templates/docs-agents.html +0 -132
  133. package/templates/docs-app.html +0 -130
  134. package/templates/docs-home.html +0 -83
  135. package/templates/docs-imessage.html +0 -121
  136. package/templates/docs-tasks.html +0 -123
  137. package/templates/docs-tips.html +0 -175
  138. package/templates/getting-started.html +0 -809
  139. package/templates/invisible-prefix-base.txt +0 -171
  140. package/templates/invisible-prefix-owner.txt +0 -282
  141. package/templates/invisible-prefix.txt +0 -338
  142. package/templates/manifest.json +0 -61
  143. package/templates/memory-org/clients.md +0 -7
  144. package/templates/memory-org/credentials.md +0 -9
  145. package/templates/memory-org/devices.md +0 -7
  146. package/templates/updates.html +0 -464
  147. package/tts-proxy.js +0 -96
  148. package/voice-call-local.js +0 -731
  149. package/voice-call.js +0 -732
  150. package/wa-listener.js +0 -354
package/wa-listener.js DELETED
@@ -1,354 +0,0 @@
1
- // ── WhatsApp Message Listener ────────────────────────────
2
- // Polls wacli for new incoming WhatsApp messages and forwards them
3
- // to the AIVA router webhook for processing.
4
- //
5
- // Architecture: wacli sync runs as a background process maintaining
6
- // the WhatsApp connection. This module polls wacli's local DB for
7
- // new messages and POSTs them to /webhook/whatsapp.
8
- // Sending: sync must be stopped briefly to release the store lock,
9
- // then send, then restart sync.
10
-
11
- const { execSync, spawn } = require('child_process');
12
- const fs = require('fs');
13
- const path = require('path');
14
-
15
- const WEBHOOK_URL = 'http://127.0.0.1:3847/webhook/whatsapp';
16
- const POLL_INTERVAL_MS = 5000; // 5 seconds
17
- const WACLI_BIN = '/opt/homebrew/bin/wacli';
18
-
19
- // Check if wacli binary exists
20
- let WACLI_AVAILABLE = false;
21
- try {
22
- fs.accessSync(WACLI_BIN, fs.constants.X_OK);
23
- WACLI_AVAILABLE = true;
24
- } catch {
25
- // Also check PATH
26
- try {
27
- execSync('which wacli', { timeout: 3000, encoding: 'utf-8' });
28
- WACLI_AVAILABLE = true;
29
- } catch {
30
- WACLI_AVAILABLE = false;
31
- }
32
- }
33
-
34
- // Track processed messages to avoid duplicates
35
- const processedMessageIds = new Set();
36
- const MAX_PROCESSED_IDS = 5000;
37
- let lastPollTimestamp = null; // ISO timestamp of last poll
38
- let pollTimer = null;
39
- let syncProcess = null;
40
- let syncPid = null;
41
-
42
- // Send queue -- serialized to avoid lock contention
43
- let sendQueue = [];
44
- let sendProcessing = false;
45
-
46
- function log(msg, data) {
47
- const ts = new Date().toISOString();
48
- if (data) console.log(`[${ts}] [WA-LISTENER] ${msg}`, JSON.stringify(data));
49
- else console.log(`[${ts}] [WA-LISTENER] ${msg}`);
50
- }
51
-
52
- function isAuthenticated() {
53
- try {
54
- const output = execSync(`${WACLI_BIN} doctor`, { timeout: 10000, encoding: 'utf-8' });
55
- return output.includes('AUTHENTICATED true');
56
- } catch {
57
- return false;
58
- }
59
- }
60
-
61
- function isLocked() {
62
- try {
63
- const output = execSync(`${WACLI_BIN} doctor`, { timeout: 10000, encoding: 'utf-8' });
64
- return output.includes('LOCKED true');
65
- } catch {
66
- return false;
67
- }
68
- }
69
-
70
- function killAllWacliSync() {
71
- try {
72
- execSync('pkill -f "wacli (sync|auth)"', { timeout: 5000 });
73
- log('Killed existing wacli sync processes');
74
- } catch {
75
- // No processes to kill
76
- }
77
- syncProcess = null;
78
- syncPid = null;
79
- }
80
-
81
- function startSync() {
82
- // Check if already running
83
- try {
84
- const ps = execSync('pgrep -f "wacli (sync|auth)"', { timeout: 5000, encoding: 'utf-8' }).trim();
85
- if (ps) {
86
- syncPid = parseInt(ps.split('\n')[0]);
87
- log('Sync already running', { pid: syncPid });
88
- return true;
89
- }
90
- } catch {
91
- // No process found
92
- }
93
-
94
- if (isLocked()) {
95
- log('Store locked by another process, waiting...');
96
- return false;
97
- }
98
-
99
- try {
100
- syncProcess = spawn(WACLI_BIN, ['sync', '--follow', '--refresh-contacts'], {
101
- detached: true,
102
- stdio: 'ignore',
103
- });
104
- syncProcess.unref();
105
- syncPid = syncProcess.pid;
106
- log('Started wacli sync', { pid: syncPid });
107
- return true;
108
- } catch (err) {
109
- log('Failed to start wacli sync', { error: err.message });
110
- return false;
111
- }
112
- }
113
-
114
- function stopSync() {
115
- if (syncPid) {
116
- try {
117
- process.kill(syncPid, 'SIGTERM');
118
- log('Stopped sync process', { pid: syncPid });
119
- } catch {
120
- // Process already dead
121
- }
122
- syncProcess = null;
123
- syncPid = null;
124
- }
125
- // Also kill any stray wacli sync processes
126
- killAllWacliSync();
127
- // Wait for processes to die and lock to release
128
- try { execSync('sleep 0.5'); } catch {}
129
- // Force kill if anything survived
130
- try { execSync('pkill -9 -f "wacli sync"', { timeout: 3000 }); } catch {}
131
- try { execSync('sleep 0.3'); } catch {}
132
- }
133
-
134
- /**
135
- * Send a WhatsApp message. Handles store lock by stopping sync,
136
- * sending, then restarting sync.
137
- * @param {string} phone - Phone number with + prefix
138
- * @param {string} text - Message text
139
- * @returns {Promise<{success: boolean, error?: string}>}
140
- */
141
- function sendMessage(phone, text) {
142
- if (!WACLI_AVAILABLE) {
143
- log('sendMessage called but wacli not installed -- skipping');
144
- return Promise.resolve({ success: false, error: 'wacli not installed' });
145
- }
146
- return new Promise((resolve) => {
147
- sendQueue.push({ phone, text, resolve });
148
- processSendQueue();
149
- });
150
- }
151
-
152
- async function processSendQueue() {
153
- if (sendProcessing || sendQueue.length === 0) return;
154
- sendProcessing = true;
155
-
156
- // Stop sync to release the lock
157
- stopSync();
158
-
159
- // Wait for lock to fully release and WA session to free up
160
- await new Promise(r => setTimeout(r, 3000));
161
-
162
- while (sendQueue.length > 0) {
163
- const { phone, text, resolve } = sendQueue.shift();
164
- log('Sending WhatsApp message', { phone, textLen: text.length });
165
- try {
166
- const result = await new Promise((res, rej) => {
167
- const child = require('child_process').exec(
168
- `${WACLI_BIN} send text --to "${phone.replace(/^\+/, '')}" --message ${JSON.stringify(text)} --timeout 60s`,
169
- { timeout: 90000, encoding: 'utf-8' },
170
- (err, stdout, stderr) => {
171
- if (err) rej(err);
172
- else res(stdout);
173
- }
174
- );
175
- });
176
- log('WhatsApp send success', { phone, result: (result || '').trim().substring(0, 100) });
177
- resolve({ success: true });
178
- } catch (err) {
179
- log('WhatsApp send FAILED', { phone, error: err.message });
180
- resolve({ success: false, error: err.message });
181
- }
182
- }
183
-
184
- // Restart sync
185
- sendProcessing = false;
186
- await new Promise(r => setTimeout(r, 300));
187
- startSync();
188
- }
189
-
190
- function fetchNewMessages() {
191
- try {
192
- let cmd = `${WACLI_BIN} messages list --json --limit 20`;
193
- if (lastPollTimestamp) {
194
- cmd += ` --after "${lastPollTimestamp}"`;
195
- } else {
196
- const since = new Date(Date.now() - 60000).toISOString();
197
- cmd += ` --after "${since}"`;
198
- }
199
-
200
- const output = execSync(cmd, { timeout: 15000, encoding: 'utf-8' });
201
- const result = JSON.parse(output);
202
-
203
- if (!result.success || !result.data) {
204
- log('fetchNewMessages: no success/data', { success: result.success, hasData: !!result.data });
205
- return [];
206
- }
207
- const msgs = result.data.messages;
208
- if (!Array.isArray(msgs)) return [];
209
- return msgs;
210
- } catch (err) {
211
- if (err.message && (err.message.includes('unknown flag') || err.message.includes('not found'))) {
212
- return fetchNewMessagesFallback();
213
- }
214
- // Don't log lock errors during send operations
215
- if (!sendProcessing) {
216
- log('Failed to fetch messages', { error: err.message.substring(0, 150) });
217
- }
218
- return [];
219
- }
220
- }
221
-
222
- function fetchNewMessagesFallback() {
223
- try {
224
- const cmd = `${WACLI_BIN} messages list --json --limit 20`;
225
- const output = execSync(cmd, { timeout: 15000, encoding: 'utf-8' });
226
- const result = JSON.parse(output);
227
-
228
- if (!result.success || !result.data) return [];
229
-
230
- const messages = result.data.messages;
231
- if (!Array.isArray(messages)) return [];
232
-
233
- const cutoff = lastPollTimestamp ? new Date(lastPollTimestamp).getTime() : (Date.now() - 60000);
234
- return messages.filter(m => {
235
- const msgTime = new Date(m.Timestamp || m.timestamp || 0).getTime();
236
- return !m.FromMe && !m.fromMe && msgTime > cutoff;
237
- });
238
- } catch (err) {
239
- if (!sendProcessing) {
240
- log('Fallback fetch failed', { error: err.message.substring(0, 150) });
241
- }
242
- return [];
243
- }
244
- }
245
-
246
- async function forwardToRouter(phone, text, messageId) {
247
- const dedupKey = messageId || `${phone}:${text.substring(0, 100)}`;
248
- if (processedMessageIds.has(dedupKey)) return;
249
-
250
- processedMessageIds.add(dedupKey);
251
-
252
- // Prune old entries
253
- if (processedMessageIds.size > MAX_PROCESSED_IDS) {
254
- const arr = [...processedMessageIds];
255
- arr.splice(0, arr.length - MAX_PROCESSED_IDS / 2);
256
- processedMessageIds.clear();
257
- arr.forEach(id => processedMessageIds.add(id));
258
- }
259
-
260
- log('Forwarding WA message to router', { phone, textLen: text.length, messageId });
261
-
262
- try {
263
- const resp = await fetch(WEBHOOK_URL, {
264
- method: 'POST',
265
- headers: { 'Content-Type': 'application/json' },
266
- body: JSON.stringify({
267
- from: phone,
268
- text: text,
269
- fromMe: false,
270
- messageId: messageId,
271
- _source: 'whatsapp'
272
- }),
273
- signal: AbortSignal.timeout(30000),
274
- });
275
-
276
- const result = await resp.json();
277
- log('Router response', { phone, status: result.status, action: result.action });
278
- } catch (err) {
279
- log('Failed to forward to router', { phone, error: err.message });
280
- }
281
- }
282
-
283
- function normalizePhone(jid) {
284
- if (!jid) return null;
285
- let phone = jid.split('@')[0];
286
- phone = phone.split(':')[0];
287
- if (!phone || phone.length < 7) return null;
288
- return phone.startsWith('+') ? phone : '+' + phone;
289
- }
290
-
291
- async function pollMessages() {
292
- // Don't poll while sending (sync is stopped for send)
293
- if (sendProcessing) return;
294
-
295
- // Ensure sync is running (receives messages from WA servers)
296
- startSync();
297
-
298
- const messages = fetchNewMessages();
299
- if (!messages || !Array.isArray(messages)) return;
300
-
301
- for (const msg of messages) {
302
- if (msg.FromMe || msg.fromMe) continue;
303
-
304
- const chatJid = msg.ChatJID || msg.chatJID || msg.chat || '';
305
- if (chatJid.includes('@g.us')) continue;
306
-
307
- const phone = normalizePhone(msg.SenderJID || msg.senderJID || msg.sender || chatJid);
308
- const text = msg.Text || msg.DisplayText || msg.Snippet || msg.text || msg.Body || msg.body || '';
309
- const messageId = msg.MsgID || msg.ID || msg.id || msg.MessageID || msg.messageId || '';
310
-
311
- if (!phone || !text) continue;
312
-
313
- await forwardToRouter(phone, text, messageId);
314
- }
315
-
316
- lastPollTimestamp = new Date().toISOString();
317
- }
318
-
319
- function start() {
320
- if (!WACLI_AVAILABLE) {
321
- log('wacli not installed -- WhatsApp listener disabled');
322
- return;
323
- }
324
-
325
- log('Starting WhatsApp listener (wacli polling)');
326
-
327
- if (!isAuthenticated()) {
328
- log('wacli not authenticated -- WhatsApp listener inactive. Run `wacli auth` to connect.');
329
- } else {
330
- log('wacli authenticated, ensuring sync is running');
331
- startSync();
332
- }
333
-
334
- // Look back 10 minutes on startup to catch messages that arrived during downtime
335
- lastPollTimestamp = new Date(Date.now() - 600000).toISOString();
336
-
337
- pollTimer = setInterval(async () => {
338
- try {
339
- await pollMessages();
340
- } catch (err) {
341
- log('Poll error', { error: err.message });
342
- }
343
- }, POLL_INTERVAL_MS);
344
- }
345
-
346
- function stop() {
347
- if (pollTimer) {
348
- clearInterval(pollTimer);
349
- pollTimer = null;
350
- }
351
- log('WhatsApp listener stopped');
352
- }
353
-
354
- module.exports = { start, stop, sendMessage };