Repository navigation
Expand file tree
/
Copy pathrouter.js
More file actions
787 lines (723 loc) · 36.5 KB
/
Copy pathrouter.js
File metadata and controls
787 lines (723 loc) · 36.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Refunds and charges always beat a product bug. "billing reasons" in a
// desktop 402 report is not a Shopify refund.
const STRONG_MONEY = [
/\brefunds?\b/i,
/\bcharged\b/i,
/\binvoice\b/i,
/\btax(es)?\b/i,
/\bduties\b/i,
/\bwrong address\b/i,
/\bchange (my )?(the )?(shipping )?address\b/i,
/\bcancel (my )?(the )?(order|purchase)\b/i,
/\bsubscription\b/i,
/\bredemption code\b/i,
/\bunlimited yearly\b/i,
/\bplus plan\b/i,
/\bno active plan\b/i,
/\bstuck on free\b/i,
/\bdowngraded to free\b/i,
/\bbilling bug\b/i,
];
const WEAK_MONEY = [
/\bmy billing\b/i,
/\bbilling (issue|question|problem|for|bug)\b/i,
/\bapp\/billing\b/i,
/\bpayment\b/i,
];
const PRIVACY = [/\bprivacy\b/i, /\bgdpr\b/i, /\bdelete my (account|data)\b/i];
// Where-is-it, status, tracking, shipping, and customs stay above firmware.
// A bare order number stays below firmware, desktop, and app.
// "got my order" / "received my order" is not a lookup.
const STRONG_SHOP = [
/\bwhere\s+is\s+my\s+order\b/i,
/\border\s*status\b/i,
/\bstatus\s+of\s+(?:my\s+|the\s+)?order\b/i,
/\btracking\b/i,
/\bshipping\b/i,
/\bcustoms\b/i,
/\bexpress delivery\b/i,
/\bstill in ["']?preparing\b/i,
/\B#\d{3,}\b[^\n]{0,48}\b(?:never|not|hasn'?t|has not)\s+arrived\b/i,
/\b(?:never|not|hasn'?t|has not)\s+arrived\b[^\n]{0,48}\B#\d{3,}\b/i,
];
const WEAK_SHOP = [/\border\s*(#|number|id|num)\b/i, /\border\s*no\.?\s*#?\d{3,}/i];
const SHOP = [...STRONG_SHOP, ...WEAK_SHOP];
const ORDER_STATUS_OPENING = 'Let’s work through your delivery question.';
const FIRMWARE_FAILURE = [
/powers? off (by itself|after)/i,
/dies? seconds after/i,
/turns? off .+ (100\s*%|battery)/i,
/turning itself off/i,
/turns? itself off/i,
/keeps turning (itself )?off/i,
/shuts? (itself )?off after/i,
/\bnot charging\b/i,
/\bwon'?t charge\b/i,
];
const FIRMWARE = [
/\bfirmware\b/i,
/\brma\b/i,
/\bwarehouse\b/i,
/\bserial( number)?\b/i,
/\breplacement (device|unit|omi)\b/i,
/\bcharger\b/i,
...FIRMWARE_FAILURE,
];
const DESKTOP = [
/\bmac\s?os\b/i,
/\bmacos\b/i,
/(?<!\bhardware\s)\bmac\b(?!(?:[- ]|['\u2019]s\s+)(?:add?ress(?:es)?|addr)\b|\s+id\b|\s+(?:of|on)\s+(?:(?:the|this|my|your)\s+)?omi\b)/i,
/\bdesktop app\b/i,
/\bomi desktop\b/i,
/\bdesktop voice\b/i,
/\bomi-windows\b/i,
/\bomi windows\b/i,
/\bwindows app\b/i,
/\bnpm error\b/i,
/\bERESOLVE\b/,
/\bomi window\b/i,
/\bfloating bubble\b/i,
/\bcome to the front\b/i,
];
const INTEGRATION_FAILURE = [
/\b(?:app|extension|integration).{0,40}\bblocked\b/i,
/\b(?:google|gmail|calendar|integration|extension).{0,70}\b(?:blocked|error|fail(?:ed|s|ing)?|cannot connect|won'?t connect|not connect|only (?:shows|gets|syncs))\b/i,
];
const APP_FAILURE = [
...INTEGRATION_FAILURE,
/\b(the )?app (crash|crashed|force.?clos)/i,
/\bcrash(ed|es|ing)?\b/i,
/\bdidn'?t sync\b/i,
/\b(recordings?|clips?) (are )?(missing|gone)\b/i,
/\bstill nothing recorded\b/i,
];
const APP = [
/\b(android|iphone|ios)\b/i,
/\bapple watch\b/i,
/\blisten socket\b/i,
/\b1011\b/,
/\btranscription\b/i,
/\bwss:\/\/api\.omi/i,
/\bapp stayed open\b/i,
/\b(disconnected|offline).{0,40}\bapp\b/i,
/\bapp.{0,40}(disconnected|offline)\b/i,
...APP_FAILURE,
];
const CAPTURE_FAILURE = [
/doesn'?t capture/i,
/does not capture/i,
/not capturing/i,
/isn'?t capturing/i,
/aren'?t capturing/i,
/\bno audio\b/i,
];
const FAQ = [
/\bleds?\b/i,
/\bpair(ing)?\b/i,
/\bdev kit\b/i,
/\bcv1\b/i,
/\bhow do i (turn|power|pair)/i,
/\bbackground\b/i,
];
const ACCOUNT = [
/fair[- ]use/i,
/filled the memory/i,
/memory.{0,40}full/i,
/what are (all )?these plans/i,
/\bthese plans\b/i,
];
const WANT_HUMAN = [
/\b(?:talk|speak|chat)\s+(?:to|with)\s+(?:(?:an?|the|actual|real|live)\s+)*(?:human|person|agent|representative|someone\s+from\s+(?:the\s+)?(?:team|sales|support))\b/i,
/\b(?:i want to|i['’]d like to|can i|let me|need to)\s+(?:talk|speak|chat)\s+(?:to|with)\s+someone\b/i,
/\b(?:i|we)\s+(?:really\s+)?(?:need|want)\s+(?:(?:a|an|the|real|actual|live)\s+)*(?:human|person|agent|representative)(?=\s*(?:$|[.!?]|please\b|to\s+(?:help|check|look|review|answer|handle|contact|call|email|talk|speak)\b))/i,
/^\s*(?:please[,\s]+)?(?:need|want)\s+(?:(?:a|an|the|real|actual|live)\s+)*(?:human|person|agent|representative)(?=\s*(?:$|[.!?]|please\b|to\s+(?:help|check|look|review|answer|handle|contact|call|email|talk|speak)\b))/i,
/\b(?:connect me with|(?:help me )?(?:get|put)(?: me)? in touch with)\s+(?:(?:an?|the)\s+)?(?:human|person|agent|representative|team|staff|sales|support)\b/i,
/\bcontact\s+(?:an?\s+|the\s+)?(?:human|person|agent|representative|support|shop|shipping)(?:\s+team)?\b/i,
/\bcan\s+someone(?:\s+from\s+(?:the\s+)?(?:team|sales|support))?\s+(?:contact|call|email)\s+me\b/i,
/\bis\s+there\s+(?:an?\s+|the\s+)?(?:real|actual|live)\s+(?:human|person|agent|representative)(?=\s*(?:$|[.!?]|to\s+(?:help|talk|chat|speak|assist)\b|available\s*(?:$|[.!?]|to\s+(?:help|talk|chat|speak|assist)\b)))/i,
/\b(?:get|give|connect|put)\s+me\s+(?:(?:in\s+touch\s+)?with\s+)?(?:(?:an?|the)\s+)?(?:(?:real|actual|live)\s+)?(?:human|person|agent|representative|someone(?:\s+from\s+(?:the\s+)?(?:team|support|sales))?|(?:the\s+)?(?:team|support|sales))\b/i,
/\b(?:(?:i|we)\s+(?:really\s+)?)?(?:need|want)\s+(?:(?:an?|the|real|actual|live)\s+)*(?:someone|human|person|agent|representative)(?:\s+from\s+(?:the\s+)?(?:team|support|sales))?\s+to\s+(?:help|check|look(?:\s+at)?|review|contact|reply|call|email)\b/i,
/\bput\s+me\s+through\s+to\s+(?:(?:your|the|a|an)\s+)?(?:team|support|sales|human|person|agent|representative)\b/i,
/\bis\s+(?:(?:a|an|the)\s+)?(?:(?:real|actual|live)\s+)?(?:human|person|agent|representative|someone)\s+available(?=\s*(?:$|[.!?]|to\s+(?:help|talk|chat|speak|assist)\b))/i,
/\b(?:please\s+)?have\s+(?:(?:someone|a\s+person|an?\s+(?:human|agent|representative))|(?:the|your)\s+(?:team|support|sales))\s+(?:contact|call|email)\s+me\b/i,
/\bconnect\s+me\s+to\s+(?:(?:your|the|a|an|real|actual|live)\s+)*(?:team|support|sales|human|person|agent|representative|someone)\b/i,
/(?:^\s*(?:please\s+)?reach\s+out\s+to\s+me\b|\b(?:team|support|staff|someone|person|human|agent)\b[^.!?]{0,40}\breach\s+out\s+to\s+me\b|\b(?:i\s+need|i\s+want|i['’]d\s+like)\s+(?:you|the\s+team)\s+to\s+reach\s+out\s+to\s+me\b)/i,
/\b(?:i['’]d|i\s+would)\s+like\s+(?:(?:a|an|the|real|actual|live)\s+)*(?:human|person|someone|agent|representative)(?:\s+from\s+(?:the\s+)?(?:team|support|sales))?\s+to\s+(?:help|look(?:\s+at)?|check|review)\b/i,
];
function wantsHumanSupport(text) {
const question = String(text || '');
if (any(question, WANT_HUMAN)) return true;
const availability = /\bis\s+(?:there\s+)?(?:(?:a|an|the)\s+)?(?:(?:real|actual|live)\s+)?(?:human|person|agent|representative|someone)\s+available\b/i.exec(question);
if (availability && !/^\s*(?:$|[.!?]|to\s+(?:help|talk|chat|speak|assist)\b)/i.test(question.slice(availability.index + availability[0].length))) return false;
const person = /\b(?:real|actual|live)\s+(?:human|person|agent|representative)\b/i;
if (!person.test(question)) return false;
const describesSpeaker = /\b(?:real|actual|live)\s+(?:human|person|agent|representative)\b[^.!?\n]{0,80}\b(?:speak(?:s|ing|ed)|talk(?:s|ing|ed)|record(?:s|ing|ed)|mic(?:rophone)?|audio|video|voice|to\s+be\s+(?:present|close|near))\b/i;
return !describesSpeaker.test(question);
}
const SUPPORT_NUDGE = [
/\b(?:same|similar) (?:problem|issue)(?:\s+(?:here|too|for me))?\b/i,
/\b(?:i(?:'m| am)|we(?:'re| are)) (?:having|seeing|experiencing) the same\b/i,
/^any(?:one|body)\??[.!\s]*(?:even a bot.*)?$/i,
/\bany(?:one|body) (?:there|available|responding)\b/i,
/\b(?:still|been) waiting\b/i,
/\bno (?:reply|response|answer)\b/i,
/\bplease (?:reply|respond|answer)\b/i,
/\beven a bot(?:'s)? answer\b/i,
];
function any(text, patterns) {
const s = String(text || '');
return patterns.some((re) => re.test(s));
}
function looksLikePhone(text) {
const re = /(?<![\d#])\+?(?:\d[\s().-]*){6,14}\d(?!\d)/g;
const s = String(text || '');
let match;
while ((match = re.exec(s))) {
const digits = match[0].replace(/\D/g, '');
if (digits.length >= 7 && digits.length <= 15) return true;
}
return false;
}
function looksLikePii(text) {
const s = String(text || '');
if (/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i.test(s)) return true;
if (/\b\d{1,5}\s+\w+.+\b(st|street|ave|avenue|rd|road|blvd)\b/i.test(s)) return true;
if (/\b(phone|tel)\b.{0,12}\d{7,}/i.test(s)) return true;
if (looksLikePhone(s)) return true;
return false;
}
function isPublicForumSafe(text) {
const route = classify(text);
if (looksLikePii(text)) return false;
if (route?.responseMode === 'grounded') return true;
if (['shop', 'privacy'].includes(route.area)) return false;
if (route.lane === 'money') return false;
return true;
}
function namesBothApps(text) {
const s = String(text || '');
const desktop = /\b(desktop|macos|mac\s?os|computer app)\b/i.test(s);
const phone = /\b(mobile app|phone app|iphone|ios app|android)\b/i.test(s);
return desktop && phone;
}
function looksLikeCaptureFailure(text) {
return any(text, CAPTURE_FAILURE);
}
function looksLikeTranscription(text) {
return /\btranscri(?:b\w*|pt\w*)\b/i.test(String(text || ''));
}
function reasonNamesCause(text) {
return /\b(app-side|app side|app bug|firmware bug|phone app bug|known cause|computer app bug)\b/i.test(
String(text || '')
);
}
function classify(text) {
return { ...classifyRoute(text), captureFailure: looksLikeCaptureFailure(text) };
}
function looksLikeOtherLanguage(text) {
const letters = String(text || '').match(/\p{L}/gu) || [];
if (letters.length < 12) return false;
const nonLatin = letters.filter((ch) => !/\p{Script=Latin}/u.test(ch)).length;
return nonLatin / letters.length >= 0.35;
}
function classifyRoute(text) {
const s = String(text || '');
const wantHuman = wantsHumanSupport(s);
if (/\bin order to\b/i.test(s) && !any(s, SHOP) && !any(s, STRONG_MONEY) && !any(s, WEAK_MONEY)) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
if (any(s, PRIVACY)) {
return { area: 'privacy', lane: 'privacy', escalate: true, wantHuman };
}
if (any(s, STRONG_MONEY)) {
return { area: 'shop', lane: 'money', escalate: true, wantHuman };
}
if (looksLikeShippingQuote(s)) {
return {
area: 'shop',
lane: 'shop',
intent: 'shipping_quote',
responseMode: 'grounded',
escalate: true,
wantHuman,
};
}
if (any(s, STRONG_SHOP)) {
return { area: 'shop', lane: 'shop', escalate: true, wantHuman };
}
if (looksLikeOtherLanguage(s)) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
if (looksLikeProductQuestion(s)) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
if (conversationPhoneDeleteReply(s)) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
if (any(s, FIRMWARE)) {
const deviceRequest = /\brma\b|\breplacement\s+(?:device|unit|omi)\b/i.test(s);
return { area: 'firmware', lane: 'firmware', escalate: wantHuman || deviceRequest || looksLikeReportedFailure(s), wantHuman };
}
if (any(s, DESKTOP)) {
return { area: 'desktop', lane: 'tech', escalate: wantHuman || looksLikeReportedFailure(s), wantHuman };
}
if (any(s, APP)) {
return { area: 'app', lane: 'tech', escalate: wantHuman || looksLikeReportedFailure(s), wantHuman };
}
if (any(s, WEAK_SHOP)) {
return { area: 'shop', lane: 'shop', escalate: true, wantHuman };
}
if (any(s, WEAK_MONEY)) {
return { area: 'shop', lane: 'money', escalate: true, wantHuman };
}
if (any(s, ACCOUNT)) {
return { area: 'shop', lane: 'account', escalate: true, wantHuman };
}
if (
looksLikeDocs(s) &&
!any(s, STRONG_MONEY) &&
!any(s, SHOP) &&
!any(s, WEAK_MONEY) &&
!any(s, ACCOUNT)
) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
if (any(s, FAQ)) {
return { area: 'unknown', lane: 'faq', escalate: wantHuman, wantHuman };
}
return { area: 'unknown', lane: 'unknown', escalate: wantHuman, wantHuman };
}
function isTechLane(route) {
return route?.lane === 'tech' || route?.area === 'firmware';
}
function specialistNames(area, lane) {
if (area === 'shop' || area === 'app') return 'Mohsin';
if (area === 'desktop') return 'Aryan';
if (area === 'firmware') return 'TuEmb';
if (area === 'privacy') return 'David';
return 'Aryan, David, undivisible';
}
function needsGroundedShopAnswer(question) {
const text = String(question || '');
if (looksLikeShippingQuote(text)) return false;
if (/\bstuck\s+(?:in|at)\s+customs\b/i.test(text)) return true;
const deliveredDevice = /\bmy\s+(?:omi|device|necklace|glasses)\b[^.!?]{0,100}\bdelivered\b/i.test(text);
const notReceived = /\b(?:isn['’]?t|is\s+not|aren['’]?t|are\s+not|wasn['’]?t|was\s+not|weren['’]?t|were\s+not|not)\s+here\b|\b(?:never|hasn['’]?t|has\s+not|didn['’]?t|did\s+not)\s+arriv(?:e|ed)\b/i.test(text);
if (deliveredDevice && notReceived) return true;
const shippingContext = /\b(?:order|package|parcel|shipment|shipping|delivery)\b/i.test(text);
const trackingObject = /\btracking\s+(?:number|link|page)\b/i.test(text);
if (!shippingContext && !trackingObject) return false;
const deliveryProblem = /\b(?:where\s+(?:is|are)\s+(?:my|the)\s+(?:order|package|parcel|shipment)|(?:order|shipment|shipping|delivery)\s+status|status\s+of\s+(?:my|the)\s+(?:order|shipment|delivery)|when\s+(?:will|does|is)\s+(?:my|the)\s+(?:order|package|parcel|shipment)\b.{0,40}\b(?:ship|arrive|deliver)|(?:order|package|parcel|shipment|delivery)\b.{0,60}\b(?:lost|missing|not\s+received|never\s+arrived|hasn['’]?t\s+arrived|delivered\b.{0,40}\b(?:not|nothing|missing|never))|(?:lost|missing|not\s+received|never\s+arrived)\b.{0,60}\b(?:order|package|parcel|shipment|delivery))\b/i.test(text);
const trackingProblem = /\btracking\b.{0,60}\b(?:stuck|not\s+updat\w*|no\s+updat\w*|not\s+mov\w*|delivered|lost|missing)\b|\btracking\s+status\b/i.test(text);
return deliveryProblem || trackingProblem;
}
function skipModel(route) {
return (
route?.lane === 'money' ||
route?.lane === 'privacy' ||
route?.lane === 'shop'
);
}
function requiresGroundedAnswer(route) {
return route?.responseMode === 'grounded' || !skipModel(route);
}
function looksLikeTax(text) {
return /\b((import\s+)?tax(es)?|duties|customs)\b/i.test(String(text || ''));
}
function looksLikeShippingQuote(text) {
const s = String(text || '');
return (
/\b(shipping|delivery)\s+(costs?|fees?|prices?|quotes?|rates?)\b/i.test(s) ||
/\b(costs?|prices?|quotes?|rates?)\s+(?:for|of)\s+(shipping|delivery)\b/i.test(s) ||
/\b(checkout|cashout)\b.{0,40}\b(shipping|delivery)\b/i.test(s) ||
/\b(shipping|delivery)\b.{0,40}\b(checkout|cashout)\b/i.test(s) ||
/\b(arrange|arranging|alternative|different|cheaper|special)\b.{0,32}\b(shipping|delivery)\b/i.test(s)
);
}
function looksLikeSupportNudge(text) {
return any(text, SUPPORT_NUDGE);
}
function isFailedSupportFollowup(text) {
const value = String(text || '');
const feedback = /\b(?:still|again|after\s+(?:those|the|these)\s+steps|(?:that|those steps|the steps)\s+(?:did\s+not|didn['’]t|do\s+not|don['’]t))\b|\b(?:sigue|siguen|todav[ií]a|a[uú]n|los\s+pasos)\b/i;
const observed = /\b(?:not\s+working|doesn['’]t\s+work|didn['’]t\s+work|did\s+not\s+work|no\s+funcionan?|sin\s+funcionar|still\s+(?:fails?|broken|stuck))\b/i;
const reported = value.replace(/\b(?:did|does|do)\s+not\s+work\b/gi, 'not working')
.replace(/\bsin\s+funcionar\b/gi, 'no funciona');
return feedback.test(value) && observed.test(value) && looksLikeReportedFailure(reported);
}
function looksLikePlan(text) {
return /\b(subscription|redemption code|unlimited yearly|plus plan|no active plan|stuck on free|downgraded to free|billing bug)\b/i.test(
String(text || '')
);
}
function looksLikeHardFailure(text) {
const s = String(text || '');
if (any(s, INTEGRATION_FAILURE)) return true;
if (/\b(crash|crashed|1011|exception|error code|won'?t charge|not charging|turns? itself off)\b/i.test(s)) return true;
return /\b(app (was|stayed) open|kept the app (open|running)|blue light)\b/i.test(s) && /\b(nothing|still)\b/i.test(s);
}
function conditionalFailureContext(clause) {
// A past event in a temporal clause is an observation, not a hypothetical.
if (/^(?:when(?:ever)?|while|cuando|mientras)\b/i.test(clause) &&
/\b(?:crashed|failed|froze|stopped|fall[oó]|se\s+detuvo|se\s+bloque[oó])\b/i.test(clause)) return clause;
// Customers omit commas. An action followed by a separate finite fault
// predicate starts the main clause; keep it. A coordinated condition
// ("...and the app fails") still belongs to the hypothetical instead.
const action = /\b(?:open(?:s|ed|ing)?|launch(?:es|ed|ing)?|start(?:s|ed|ing)?|select(?:s|ed|ing)?|choos(?:e|es|ing)|record(?:s|ed|ing)?|abro|abrimos|inici[oó]|inicio|intento|selecciono|grabo)\b/i.exec(clause);
if (!action) return 'condition';
const remainder = clause.slice(action.index + action[0].length);
const consequence = /\b(?:(?:it|they|i|we|(?:the|my)\s+(?:app|application|device|transcript|recording|firmware))\s+(?:crash(?:es|ed|ing)?|fail(?:s|ed|ing)?|freez(?:es|ing)|froze|stop(?:s|ped)?|can't|cannot|doesn't|won't|(?:is|stays?)\s+(?:offline|broken)|(?:get|see|receive)\s+(?:an?\s+)?error)|se\s+(?:bloquea|congela)|aparece\s+(?:un\s+)?error)\b/i.exec(remainder);
if (!consequence || /\b(?:and|or|y|o)\s*$/i.test(remainder.slice(0, consequence.index))) return 'condition';
return 'condition ' + remainder.slice(consequence.index);
}
function looksLikeReportedFailure(text) {
const s = String(text || '').replace(/[\u2018\u2019]/g, "'")
// In a diagnostic noun phrase, crash/error names the type of artifact,
// not an assertion that the customer's app crashed or raised an error.
// Reported failures elsewhere in the same question remain untouched.
.replace(/\b(?:crash|error|exception|failure)s?\s+(logs?|reports?|guides?|documentation)\b/gi, 'diagnostic $1')
// Not knowing a navigation/procedure is not a broken capability. Remove
// just that grammatical signal; missing recordings or a separate incident
// in the remainder still reaches the ordinary failure checks below.
.replace(/\b(?:can't|cannot|can not|couldn't|could not)\s+(?:find|locate|see\s+where|figure\s+out|work\s+out)\b/gi, 'navigation')
.replace(/\b(?:i'm|i am)\s+lost\b|\bnot\s+sure\s+how\b|\b(?:don't|do not|didn't|did not)\s+know\s+(?:how|where)\b/gi, 'navigation')
.replace(/\b(?:no\s+(?:puedo|podemos|consigo)\s+(?:encontrar|localizar|ver\s+d[oó]nde|averiguar|entender)|estoy\s+perdid[oa]|no\s+s[eé]\s+(?:c[oó]mo|d[oó]nde))\b/gi, 'navigation')
// A condition describes a circumstance to explain, not an observed fault.
// Stop at clause/sentence boundaries so a main or contrasting clause can
// still report a genuine crash, missing capture, or failed operation.
.replace(/\b(?:if|when(?:ever)?|while|in\s+case|si|cuando|mientras|en\s+caso\s+de\s+que)\b(?:(?![,;:\n]|[.!?](?:\s|$)|\b(?:but|however|yet|pero|sin\s+embargo|entonces|then)\b).)*/gi, conditionalFailureContext);
if (looksLikeHardFailure(s) || looksLikeCaptureFailure(s) ||
any(s, APP_FAILURE) || any(s, FIRMWARE_FAILURE)) return true;
// Topic names select the product lane, not a support incident. Only an
// observed failure/negated capability adds the deterministic handoff signal.
const englishFailure = /\b(?:not\s+working|broken|errors?|fail(?:ed|s|ing)?|stopped|stuck|missing|lost|gone|cannot|can't|couldn't|could\s+not|won't|doesn't|didn't|unavailable|freez(?:e|es|ing)|froze|never\s+(?:get|gets|receive|receives)|no\s+(?:audio|transcript|recording|response))\b/i;
const spanishFailure = /\b(?:no\s+(?:funciona|funcionan|abre|abren|graba|graban|sincroniza|sincronizan|conecta|conectan|termina|terminan)|dej[oó]\s+de\s+(?:funcionar|grabar|transcribir)|fall(?:a|an|o|os|ando)|atascad[oa]s?|rot[oa]s?|perdid[oa]s?|faltan?)\b/i;
const connectionFailure = /\b(?:is|was|stays?|stayed|keeps?|kept|show(?:s|ing)?|says?|reports?|became|went)\s+(?:as\s+)?(?:disconnected|offline)\b/i;
return englishFailure.test(s) || spanishFailure.test(s) || connectionFailure.test(s);
}
function looksLikeProductQuestion(text) {
const s = String(text || '');
if (looksLikeReportedFailure(s)) return false;
if (
any(s, STRONG_MONEY) ||
any(s, WEAK_MONEY) ||
any(s, STRONG_SHOP) ||
any(s, WEAK_SHOP) ||
any(s, PRIVACY) ||
any(s, ACCOUNT)
) return false;
if (looksLikeRecordingHow(s)) return true;
if (looksLikeDeviceReset(s)) return true;
return /\b(how (do|does|can|should)|where (do|does|can|is|are)|which (omi )?(device|app|setting|option)|if i .{0,60}\b(can|will|does)|is it (meant|supposed)|supposed to|any clue|like \w+ does|can (it|omi|the device)|does (it|omi|the device)|what does|why (does|is|can)|i'?d like)\b/i.test(s);
}
function looksLikeRecordingHow(text) {
const s = String(text || '');
if (looksLikeHardFailure(s)) return false;
const plaud = /\bplaud\b/i.test(s) && /\b(record|24)/i.test(s);
const day = /\b24\s*hours?\b/i.test(s) && /\brecord/i.test(s);
const alone = /\b(not app|without the app|by itself)\b/i.test(s) && /\brecord/i.test(s);
return plaud || day || alone;
}
function looksLikeDeviceReset(text) {
const s = String(text || '');
if (looksLikeHardFailure(s)) return false;
if (/\bcrash(ed|es|ing)?\b/i.test(s)) return false;
if (/\b(delete|wipe|erase)\b/i.test(s) && /\b(account|data|conversation|memory)\b/i.test(s)) return false;
if (/\b(already reset|after (a |the )?reset|reset it and)\b/i.test(s)) return false;
return /\breset\b/i.test(s) && /\b(how|can i|want to|omi|device|necklace|pendant)\b/i.test(s);
}
function deviceResetReply() {
return [
'To reset the necklace, press and hold the button, and while still holding, place it on the charger. It may take a few tries.',
'If that does not reset it, let it discharge until it powers off, then charge it again.',
'If there is no light at all, use that same reset. If it still stays off, leave it on the charger for 6–8 hours. If it gets slightly warm, keep charging for 12–14 hours.',
'If none of that works, email help@omi.me. These steps are from the Omi troubleshooting guide: https://help.omi.me/en/articles/12847359-omi-device-troubleshooting-guide',
].join('\n\n');
}
function recordingHowReply() {
return "The 24 hours on the Omi page is battery life, about a day to a few days depending on the device. It is not a full day of recording with the phone app closed. The necklace needs the Omi app. You can leave the app in the background. If you swipe it away, it stops writing down what was said and the device disconnects. Turn it on with one press. Blue means it is connected to the phone. Red means it is on but not connected. Speak near it. The words can take up to a minute to show in the app. DevKit 2 is the one whose docs say it can record on its own. If the app stayed open, the light was blue, and still nothing showed up, say so here.";
}
function batteryReadingReply(question) {
const text = String(question || '');
const percentages = text.match(/\b\d{1,3}\s*%/g) || [];
const unstable =
/\b(?:jump(?:s|ed|ing)?|drop(?:s|ped|ping)?|fluctuat(?:e|es|ed|ing)|unreliable|inaccurate|wrong|random|stuck)\b/i.test(
text
) || percentages.length >= 2;
if (!/\bbatter(?:y|ies)\b/i.test(text) || !unstable) return '';
const lines = [];
if (/\b(?:green.{0,20}blue|blue.{0,20}green)\b/i.test(text)) {
lines.push(
'For the consumer Omi necklace, blinking green and blue means it is charging while connected to the phone; that colour pattern itself is expected.'
);
}
lines.push(
'The sharp battery-percentage changes after plugging in, restarting, or unplugging are the unresolved problem—not the green/blue charging indication.'
);
const hasTicket = /\b(?:ticket|case|request)\b.{0,20}\b(?:#?\d+|open(?:ed)?|made|submitted)\b/i.test(text);
const sentDiagnostics = /\b(?:sent|send|emailed|uploaded|provided|shared)\b.{0,40}\b(?:diagnostics?|logs?|screenshots?)\b/i.test(
text
);
if (hasTicket || sentDiagnostics) {
lines.push(
`You already ${hasTicket ? 'opened a support ticket' : 'contacted support'}${
sentDiagnostics ? ' and sent diagnostics or screenshots' : ''
}, so you should not need to repeat those steps. The existing case needs a review of the battery readings and diagnostics.`
);
} else {
lines.push(
'The battery readings need a device review before the cause or a charging step can be confirmed.'
);
}
lines.push('Source: https://github.com/BasedHardware/omi/issues/5653');
return lines.join('\n\n');
}
function conversationPhoneDeleteReply(question) {
const text = String(question || '');
if (
/\b(?:cannot|can['’]?t|unable|won['’]?t|doesn['’]?t|didn['’]?t|failed?|error|resurfac\w*|reappear\w*|not working)\b/i.test(
text
)
) {
return '';
}
const removing = /\b(?:delete|remove|clear|erase)\w*\b/i.test(text);
const conversation = /\b(?:conversation|transcript|past conversation)s?\b/i.test(text);
const surface = /\b(?:app|phone|local|device|recording cop(?:y|ies)|stored recording)s?\b/i.test(text);
if (!removing || !conversation || !surface) return '';
return [
'To remove a past conversation from Omi and also remove its phone-local recording copy, treat those as two separate deletions:',
'1. Open the conversation in the Omi app, open the ⋯ menu, and choose **Delete conversation**. That removes the conversation/transcript from your synced Omi data.',
'2. To clear recording files stored on the phone, open **Settings → Device → Offline Sync**, tap **⋯ → Manage Storage**, then clear **Synced** copies. Clearing synced copies frees phone storage but keeps any conversations that you did not delete in step 1.',
'Do not clear **Pending** or **All** unless you also intend to discard unsynced recordings. PR #20172 applies only to synced copies on the phone; it does not erase the pendant/device storage.',
'Source: https://github.com/BasedHardware/omi/pull/20172 https://help.omi.me/en/articles/13162549-omi-privacy-policy',
].join('\n\n');
}
function looksLikeDocs(text) {
return /\b(instructions|documentation|how (it|this|does it) works?|how it'?s different)\b/i.test(
String(text || '')
);
}
function whenModelDown(route, question) {
if (looksLikeOtherLanguage(question) && route?.lane === 'faq' && !route?.wantHuman) {
return {
agent: {
final_answer: '',
confidence: 0.2,
escalate: true,
reason: 'Question is not in English.',
},
reply: 'Your question needs support in your language. Automated answers are temporarily unavailable.',
};
}
const known = cannedReply(route, question);
const canAnswer = route?.lane === 'faq' && !route?.wantHuman && known;
return {
agent: {
final_answer: '',
confidence: canAnswer ? 0.9 : 0.2,
escalate: !canAnswer,
reason: canAnswer ? '' : staffReason(route, question),
},
reply: canAnswer ? known : [known || 'You still need help with this question.', 'Automated answers are temporarily unavailable.'].join('\n\n'),
};
}
function orderLookupLive() {
try {
return require('./orderFlow').isLive();
} catch {
return false;
}
}
function shopStatusReply(route, question) {
if (route?.wantHuman || looksLikeSupportNudge(question)) {
return [
"You’re asking for a person from the shop team to check this shipment.",
'Tracking and an expected delivery date need a verified order check.',
"Please don't post your address or payment details here.",
].join(' ');
}
const head = `${ORDER_STATUS_OPENING} An order status or delivery date needs a verified order check. Please keep your address and payment details private.`;
if (orderLookupLive()) {
return `${head}\n\nUse /order to check your own orders privately. We email a code to the address on the order so nobody can look up someone else's. Keep your order number handy.`;
}
return head;
}
function shippingQuoteReply() {
return [
'You want another option for the checkout shipping quote.',
'The shop team needs to check whether another shipping option is available for your destination; no different rate or route is confirmed yet.',
"Keep a screenshot of the checkout quote ready, but don't post your full address here.",
].join(' ');
}
function cannedReply(route, question) {
const lane = route?.lane;
if (lane === 'money') {
if (looksLikeTax(question)) {
return 'You need help with tax or duties on an order. The charge needs a team review before any adjustment can be confirmed.';
}
if (looksLikePlan(question)) {
return 'You need help with your paid plan or redemption code. The account needs a team review before any change can be confirmed.';
}
const text = String(question || '');
if (/\brefunds?\b/i.test(text)) return 'You’re asking for a refund. The request needs a team review before a refund or timing can be confirmed.';
if (/\bcancel\b/i.test(text)) return 'You want to cancel your order. The request needs a team review before cancellation can be confirmed.';
if (/\b(?:address|shipping details)\b/i.test(text)) return 'You want to change the shipping address. The request needs a team review before an address change can be confirmed.';
return 'You need help with a payment or charge. The account or order needs a team review before any adjustment can be confirmed.';
}
if (lane === 'privacy') {
return 'You’re asking about your account or saved data. Any account change or data deletion needs a verified privacy review.';
}
if (lane === 'account') {
return 'You need clarification about your account limits or plan. The account needs a team review before a limit or plan change can be confirmed.';
}
if (lane === 'shop') {
if (looksLikeTax(question)) {
return 'You need help with tax or duties on an order. The charge needs a team review before any adjustment can be confirmed.';
}
if (route?.intent === 'shipping_quote' || looksLikeShippingQuote(question)) {
return shippingQuoteReply();
}
return shopStatusReply(route, question);
}
if (looksLikeRecordingHow(question)) return recordingHowReply();
if (looksLikeDeviceReset(question)) return deviceResetReply();
const deletionReply = conversationPhoneDeleteReply(question);
if (deletionReply) return deletionReply;
const batteryReply = batteryReadingReply(question);
if (batteryReply) return batteryReply;
if (lane === 'firmware') {
if (route?.escalate === false) return 'You need guidance on using or setting up your Omi device.';
return 'Your Omi device isn’t working as expected. A device review is needed to identify the cause and a safe next step.';
}
if (lane === 'tech' && route?.area === 'desktop') {
if (route?.escalate === false) return 'You need guidance on using or setting up the computer app.';
return 'The computer app isn’t working as expected. A closer review is needed to find a safe next step without repeating checks you’ve already tried.';
}
if (lane === 'tech' && route?.area === 'unknown') {
return 'You’re running into a technical problem. A closer review is needed to identify the cause and a safe next step.';
}
if (lane === 'tech') {
if (route?.escalate === false) return 'You need guidance on using or setting up the phone app.';
return 'The phone app isn’t working as expected. A closer review is needed to find a safe next step without repeating checks you’ve already tried.';
}
if (/\bblue\b/i.test(String(question || '')) && /\blight\b/i.test(String(question || ''))) {
return 'A solid blue light means the Omi is on and connected to your phone.';
}
if (/\bhow do i turn\b/i.test(String(question || '')) && /\boff\b/i.test(String(question || '')) &&
/\bnecklace\b/i.test(String(question || '')) && !/\bdev\s*kit\b|devkit/i.test(String(question || ''))) {
return 'On the necklace, hold the button for about 3 seconds to turn it off. Press once to turn it back on. Source: https://docs.omi.me/onboarding/omi';
}
if (/\bdelete\b/i.test(String(question || '')) && /\bconversation\b/i.test(String(question || ''))) {
return 'Open that conversation and delete it from its detail view. That deletes the transcript and any stored audio for it.';
}
return null;
}
function staffReason(route, question) {
const lane = route?.lane;
if (route?.intent === 'shipping_quote' || looksLikeShippingQuote(question)) {
return 'Checkout shipping quote or alternate shipping route';
}
if (looksLikeTax(question) && (lane === 'money' || lane === 'shop' || route?.area === 'shop')) {
return 'Tax, duties, or customs';
}
if (looksLikePlan(question) && (lane === 'money' || route?.area === 'shop')) {
return 'Paid plan or redemption code';
}
if (lane === 'money') return 'Refund, charge, or address change';
if (lane === 'privacy') return 'Data deletion / privacy request';
if (looksLikeCaptureFailure(question) && looksLikeTranscription(question)) {
return 'Transcription unavailable, and the device is not capturing. Cannot see the app from chat.';
}
if (
namesBothApps(question) &&
(lane === 'tech' || route?.area === 'desktop' || route?.area === 'app')
) {
return 'Cannot see the computer app or the phone app from chat.';
}
if (route?.area === 'app') return 'Cannot see the phone app from chat';
if (route?.area === 'desktop') return 'Cannot see the computer app from chat';
if (lane === 'firmware' || route?.area === 'firmware') {
return 'Device problem; cannot see the Omi from chat';
}
if (lane === 'tech') return 'Cannot see the app from chat';
if (lane === 'shop') return 'Order or shipping';
if (lane === 'account') return 'Fair use, memory limit, or plan question';
if (lane === 'faq' && looksLikeDocs(question)) {
return 'Docs question. Do not invent how it works or plan prices.';
}
return 'Needs a person';
}
function pickStaffReason(route, modelReason, question) {
if (route?.lane === 'faq' && looksLikeDocs(question)) return staffReason(route, question);
const lane = route?.lane;
const area = route?.area;
if (namesBothApps(question) && (lane === 'tech' || area === 'desktop' || area === 'app')) {
return staffReason(route, question);
}
const reason = String(modelReason || '').trim();
const techish =
lane === 'tech' ||
lane === 'firmware' ||
area === 'app' ||
area === 'desktop' ||
area === 'firmware';
if (techish && reasonNamesCause(reason)) return staffReason(route, question);
return reason || staffReason(route, question);
}
function knownIssueReply() {
return 'This is already marked as a known issue. A person has to confirm whether it matches your report before a cause or fix can be stated.';
}
function describe(route) {
const lane = route?.lane;
const area = route?.area;
if (lane === 'account') {
return 'Account limits and plans. Answer phone vs computer if they asked. Do not invent prices.';
}
if (lane === 'firmware' || area === 'firmware') {
if (route?.escalate === false) return 'Device usage, setup or firmware guidance. Answer the requested procedure from official sources without assuming a device fault.';
return 'The device itself is failing. They may already have paired.';
}
if (area === 'desktop') return route?.escalate === false
? 'Computer app usage or setup. Answer the requested procedure from official sources without assuming a fault.'
: 'Computer app install or bug.';
if (area === 'app') {
if (route?.escalate === false) return 'Phone app or transcription usage and setup. Answer the requested procedure from official sources without assuming a fault.';
return 'The phone app is reporting a problem. Do not name a cause or where recordings are.';
}
if (lane === 'shop') return 'Order, shipping, or customs.';
if (lane === 'money') return 'Refund, charge, tax, plan, or address. Do not promise money back.';
if (lane === 'privacy') return 'Delete account or data.';
if (lane === 'faq') return 'How-to they asked for. Do not dump extra setup.';
return 'Read their message. Answer what they asked. Do not change the subject.';
}
module.exports = {
ORDER_STATUS_OPENING,
classify,
looksLikePii,
isPublicForumSafe,
isTechLane,
specialistNames,
needsGroundedShopAnswer,
skipModel,
requiresGroundedAnswer,
looksLikeTax,
looksLikeShippingQuote,
looksLikeSupportNudge,
isFailedSupportFollowup,
looksLikePlan,
looksLikeDocs,
looksLikeRecordingHow,
looksLikeDeviceReset,
looksLikeProductQuestion,
looksLikeOtherLanguage,
cannedReply,
whenModelDown,
staffReason,
pickStaffReason,
knownIssueReply,
looksLikeCaptureFailure,
looksLikeTranscription,
namesBothApps,
describe,
};