vpschatbotwidget 1.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 (63) hide show
  1. package/Chatbot.svg +1 -0
  2. package/README.md +86 -0
  3. package/dist/assets/index-C0fUDi2s.css +1 -0
  4. package/dist/assets/index-foGq5_Av.js +159 -0
  5. package/dist/index.html +17 -0
  6. package/index.html +16 -0
  7. package/package.json +47 -0
  8. package/src/App.tsx +85 -0
  9. package/src/chatbot/api/chatbot-api.ts +258 -0
  10. package/src/chatbot/api/chatbot-types.ts +145 -0
  11. package/src/chatbot/chatbot.test.tsx +274 -0
  12. package/src/chatbot/config/colors.ts +166 -0
  13. package/src/chatbot/config/suggestions.ts +21 -0
  14. package/src/chatbot/core/ChatContext.tsx +43 -0
  15. package/src/chatbot/core/ChatEmptyState.tsx +21 -0
  16. package/src/chatbot/core/ChatError.tsx +26 -0
  17. package/src/chatbot/core/ChatHeader.tsx +108 -0
  18. package/src/chatbot/core/ChatInput.tsx +103 -0
  19. package/src/chatbot/core/ChatMarkdown.tsx +87 -0
  20. package/src/chatbot/core/ChatMessage.tsx +115 -0
  21. package/src/chatbot/core/ChatMessageList.tsx +47 -0
  22. package/src/chatbot/core/ChatPanel.tsx +66 -0
  23. package/src/chatbot/core/ChatProvider.tsx +185 -0
  24. package/src/chatbot/core/ChatSuggestions.tsx +71 -0
  25. package/src/chatbot/core/ChatTicketConfirmation.tsx +59 -0
  26. package/src/chatbot/core/ChatTypingIndicator.tsx +16 -0
  27. package/src/chatbot/core/ticket-intent.ts +12 -0
  28. package/src/chatbot/embedded/EmbeddedChatbot.tsx +47 -0
  29. package/src/chatbot/embedded/EmbeddedChatbotPanel.tsx +56 -0
  30. package/src/chatbot/embedded/EmbeddedChatbotTrigger.tsx +167 -0
  31. package/src/chatbot/hooks/useChatbot.ts +5 -0
  32. package/src/chatbot/index.ts +7 -0
  33. package/src/chatbot/widget/ChatbotWidget.tsx +43 -0
  34. package/src/chatbot/widget/ChatbotWidgetPanel.tsx +39 -0
  35. package/src/chatbot/widget/ChatbotWidgetTrigger.tsx +32 -0
  36. package/src/components/AgentSummaryCard.tsx +37 -0
  37. package/src/components/ChatHeader.tsx +142 -0
  38. package/src/components/ChatInput.tsx +123 -0
  39. package/src/components/ChatWidget.tsx +295 -0
  40. package/src/components/ConnectingStatus.tsx +16 -0
  41. package/src/components/EnvBadge.tsx +103 -0
  42. package/src/components/FeedbackRatingCard.tsx +123 -0
  43. package/src/components/MessageItem.tsx +157 -0
  44. package/src/components/MessageList.tsx +143 -0
  45. package/src/components/OrderTrackingCard.tsx +104 -0
  46. package/src/components/QuickPrompts.tsx +46 -0
  47. package/src/components/ResponseIconMapper.tsx +155 -0
  48. package/src/components/SourceCitations.tsx +46 -0
  49. package/src/components/ToolCallBadge.tsx +29 -0
  50. package/src/components/TypingProgressBar.tsx +113 -0
  51. package/src/components/ui/message-scroller.tsx +128 -0
  52. package/src/index.css +550 -0
  53. package/src/lib/cn.ts +5 -0
  54. package/src/lib/gibberishDetector.ts +159 -0
  55. package/src/lib/textHighlighter.tsx +206 -0
  56. package/src/main.tsx +10 -0
  57. package/src/services/api.ts +74 -0
  58. package/src/test/message-scroller-stub.tsx +60 -0
  59. package/src/test/setup.ts +8 -0
  60. package/src/types/chat.ts +66 -0
  61. package/src/vite-env.d.ts +14 -0
  62. package/tsconfig.json +21 -0
  63. package/vite.config.ts +28 -0
package/src/index.css ADDED
@@ -0,0 +1,550 @@
1
+ @import url("https://fonts.googleapis.com/css2?family=Bai+Jamjuree:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700&display=swap");
2
+ @import "tailwindcss";
3
+
4
+ :root {
5
+ --vps-white: #fff;
6
+ --vps-dark-blue: #1a1a31;
7
+ --vps-light-blue: #353552;
8
+ --vps-light-grey: #d1d1d6;
9
+ --vps-orange: #ff4717;
10
+
11
+ --dark-blue-50: #f1f5fc;
12
+ --dark-blue-100: #e5ebfa;
13
+ --dark-blue-200: #d1daf4;
14
+ --dark-blue-950: #1a1a31;
15
+
16
+ --light-blue-50: #f4f6fa;
17
+ --light-blue-100: #e6e9f3;
18
+ --light-blue-900: #404168;
19
+ --light-blue-950: #353552;
20
+
21
+ --grey-50: #f6f6f7;
22
+ --grey-100: #efeff0;
23
+ --grey-200: #e2e1e4;
24
+ --grey-300: #d1d1d6;
25
+ --grey-400: #b9b9c0;
26
+ --grey-500: #a7a6ae;
27
+ --grey-600: #93919a;
28
+ --grey-700: #7e7d85;
29
+
30
+ --orange-50: #fff3ed;
31
+ --orange-100: #ffe4d4;
32
+ --orange-500: #ff4717;
33
+ --orange-600: #f02706;
34
+ --orange-900: #7f150f;
35
+
36
+ --success-50: #f1fcf5;
37
+ --success-600: #1d9c53;
38
+ --success-700: #1b7a43;
39
+
40
+ --error-50: #fff1f1;
41
+ --error-200: #ffc5c5;
42
+ --error-300: #ff9d9d;
43
+ --error-800: #a50f0f;
44
+
45
+ --tint-solus-10: #ffede8;
46
+ --neutral-500: #727681;
47
+ --neutral-950: #0a0d12;
48
+
49
+ --bg-app: var(--light-blue-50);
50
+ --card-bg: var(--vps-white);
51
+ --border-light: var(--grey-200);
52
+ --surface-muted: var(--dark-blue-50);
53
+ --input-bg: var(--grey-100);
54
+
55
+ --primary: var(--vps-orange);
56
+ --primary-hover: var(--orange-600);
57
+ --primary-purple: var(--primary);
58
+
59
+ --action: var(--vps-dark-blue);
60
+ --action-hover: var(--light-blue-900);
61
+
62
+ --bubble-user-bg: var(--tint-solus-10);
63
+ --bubble-user-text: var(--vps-dark-blue);
64
+ --bubble-assistant-bg: var(--surface-muted);
65
+
66
+ --text-main: var(--vps-dark-blue);
67
+ --text-muted: var(--grey-600);
68
+ --text-subtle: var(--grey-400);
69
+ --text-on-action: var(--vps-white);
70
+
71
+ --success: var(--success-600);
72
+ --error-bg: var(--error-50);
73
+ --error-border: var(--error-200);
74
+ --error-text: var(--error-800);
75
+
76
+ --code-bg: var(--vps-dark-blue);
77
+ --overlay: #1a1a3133;
78
+ --scrollbar: var(--grey-300);
79
+
80
+ --font-main: "Bai Jamjuree", sans-serif;
81
+ --font-jamjuree: "Bai Jamjuree", sans-serif;
82
+ }
83
+
84
+ @theme inline {
85
+ --color-vps-white: var(--vps-white);
86
+ --color-vps-dark-blue: var(--vps-dark-blue);
87
+ --color-vps-light-blue: var(--vps-light-blue);
88
+ --color-vps-light-grey: var(--vps-light-grey);
89
+ --color-vps-orange: var(--vps-orange);
90
+ --color-background: var(--bg-app);
91
+ --color-foreground: var(--text-main);
92
+ --color-card: var(--card-bg);
93
+ --color-muted: var(--surface-muted);
94
+ --color-muted-foreground: var(--text-muted);
95
+ --color-border: var(--border-light);
96
+ --color-primary: var(--primary);
97
+ --color-primary-foreground: var(--text-on-action);
98
+ --color-action: var(--action);
99
+ --color-action-foreground: var(--text-on-action);
100
+ --color-success: var(--success);
101
+ }
102
+
103
+ * {
104
+ box-sizing: border-box;
105
+ }
106
+
107
+ html,
108
+ body,
109
+ #root {
110
+ min-height: 100%;
111
+ }
112
+
113
+ body {
114
+ margin: 0;
115
+ min-height: 100vh;
116
+
117
+ font-family: var(--font-main);
118
+
119
+ background-color: var(--bg-app);
120
+ color: var(--text-main);
121
+
122
+ -webkit-font-smoothing: antialiased;
123
+ -moz-osx-font-smoothing: grayscale;
124
+ }
125
+
126
+ button,
127
+ input,
128
+ textarea,
129
+ select {
130
+ font-family: inherit;
131
+ }
132
+
133
+ button {
134
+ cursor: pointer;
135
+ }
136
+
137
+ @keyframes chat-pop {
138
+ from {
139
+ opacity: 0;
140
+ transform: scale(0.96) translateY(8px);
141
+ }
142
+
143
+ to {
144
+ opacity: 1;
145
+ transform: scale(1) translateY(0);
146
+ }
147
+ }
148
+
149
+ .chat-pop {
150
+ animation: chat-pop 0.2s ease-out;
151
+ }
152
+
153
+ @media (prefers-reduced-motion: reduce) {
154
+ *,
155
+ *::before,
156
+ *::after {
157
+ animation-duration: 0.01ms !important;
158
+ animation-iteration-count: 1 !important;
159
+ transition-duration: 0.01ms !important;
160
+ scroll-behavior: auto !important;
161
+ }
162
+ }
163
+
164
+ ::-webkit-scrollbar {
165
+ width: 5px;
166
+ height: 5px;
167
+ }
168
+
169
+ ::-webkit-scrollbar-thumb {
170
+ background: var(--scrollbar);
171
+ border-radius: 9999px;
172
+ }
173
+
174
+ ::-webkit-scrollbar-track {
175
+ background: transparent;
176
+ }
177
+
178
+ /* ── Text Highlight Tokens ──────────────────────────────────────────────────── */
179
+
180
+ /* Double-quoted strings "..." → warm amber */
181
+ .hl-double-quote {
182
+ color: #b45309;
183
+ background: #fef3c7;
184
+ border-radius: 3px;
185
+ padding: 0 3px;
186
+ font-weight: 600;
187
+ font-style: italic;
188
+ }
189
+
190
+ /* Single-quoted strings '...' → sky blue */
191
+ .hl-single-quote {
192
+ color: #0369a1;
193
+ background: #e0f2fe;
194
+ border-radius: 3px;
195
+ padding: 0 3px;
196
+ font-weight: 600;
197
+ font-style: italic;
198
+ }
199
+
200
+ /* Dates → emerald green */
201
+ .hl-date {
202
+ display: inline-flex;
203
+ align-items: center;
204
+ gap: 3px;
205
+ color: #065f46;
206
+ background: #d1fae5;
207
+ border: 1px solid #6ee7b7;
208
+ border-radius: 4px;
209
+ padding: 1px 5px;
210
+ font-size: 0.85em;
211
+ font-weight: 600;
212
+ white-space: nowrap;
213
+ }
214
+
215
+ /* Times → soft violet */
216
+ .hl-time {
217
+ display: inline-flex;
218
+ align-items: center;
219
+ gap: 3px;
220
+ color: #5b21b6;
221
+ background: #ede9fe;
222
+ border: 1px solid #c4b5fd;
223
+ border-radius: 4px;
224
+ padding: 1px 5px;
225
+ font-size: 0.85em;
226
+ font-weight: 600;
227
+ white-space: nowrap;
228
+ }
229
+
230
+ /* URLs / Links → orange (brand primary) */
231
+ .hl-link {
232
+ display: inline-flex;
233
+ align-items: center;
234
+ gap: 3px;
235
+ color: var(--vps-orange);
236
+ background: var(--orange-50);
237
+ border-bottom: 1.5px solid var(--vps-orange);
238
+ border-radius: 3px;
239
+ padding: 0 3px;
240
+ font-weight: 600;
241
+ text-decoration: none;
242
+ word-break: break-all;
243
+ transition: background 0.15s, color 0.15s;
244
+ }
245
+ .hl-link:hover {
246
+ background: var(--orange-100);
247
+ color: var(--orange-600);
248
+ }
249
+
250
+ /* ── Gibberish / Unreadable Input Warning Banner ────────────────────────────── */
251
+
252
+ .chat-input-warning {
253
+ display: flex;
254
+ align-items: flex-start;
255
+ gap: 8px;
256
+ margin: 0 12px 8px 12px;
257
+ padding: 9px 12px;
258
+ background: #fff7ed;
259
+ border: 1px solid #fed7aa;
260
+ border-radius: 8px;
261
+ color: #9a3412;
262
+ font-size: 0.78rem;
263
+ line-height: 1.45;
264
+ animation: chat-pop 0.2s ease-out;
265
+ }
266
+
267
+ .chat-input-warning-icon {
268
+ flex-shrink: 0;
269
+ margin-top: 1px;
270
+ color: #ea580c;
271
+ }
272
+
273
+ /* Textarea border hint when warning is active */
274
+ .chat-textarea--warn {
275
+ border-color: #fb923c !important;
276
+ outline-color: #fb923c !important;
277
+ }
278
+
279
+ /* ── Messages Scroll Outer (wraps scroll-area + progress bar) ───────────────── */
280
+
281
+ .messages-scroll-outer {
282
+ display: flex;
283
+ flex-direction: column;
284
+ flex: 1;
285
+ min-height: 0;
286
+ position: relative;
287
+ overflow: hidden;
288
+ }
289
+
290
+ /* ── Typing Progress Bar ────────────────────────────────────────────────────── */
291
+
292
+ @keyframes tpb-shimmer {
293
+ 0% { transform: translateX(-100%); }
294
+ 100% { transform: translateX(100%); }
295
+ }
296
+
297
+ @keyframes tpb-slide-up {
298
+ from { transform: translateY(100%); opacity: 0; }
299
+ to { transform: translateY(0); opacity: 1; }
300
+ }
301
+
302
+ @keyframes tpb-slide-down {
303
+ from { transform: translateY(0); opacity: 1; }
304
+ to { transform: translateY(100%); opacity: 0; }
305
+ }
306
+
307
+ @keyframes tpb-spin {
308
+ to { transform: rotate(360deg); }
309
+ }
310
+
311
+ .typing-progress-bar-root {
312
+ position: sticky;
313
+ bottom: 0;
314
+ left: 0;
315
+ right: 0;
316
+ z-index: 10;
317
+ background: var(--vps-white);
318
+ border-top: 1px solid var(--border-light);
319
+ padding: 5px 14px 6px 14px;
320
+ display: flex;
321
+ flex-direction: column;
322
+ gap: 4px;
323
+ box-shadow: 0 -2px 12px 0 rgba(26, 26, 49, 0.06);
324
+ }
325
+
326
+ .tpb-enter { animation: tpb-slide-up 0.32s cubic-bezier(0.22,1,0.36,1) both; }
327
+ .tpb-exit { animation: tpb-slide-down 0.28s ease-in both; }
328
+
329
+ /* Shimmer track */
330
+ .tpb-shimmer-track {
331
+ width: 100%;
332
+ height: 3px;
333
+ background: var(--grey-100);
334
+ border-radius: 9999px;
335
+ overflow: hidden;
336
+ }
337
+
338
+ .tpb-shimmer-fill {
339
+ width: 55%;
340
+ height: 100%;
341
+ border-radius: 9999px;
342
+ background: linear-gradient(
343
+ 90deg,
344
+ var(--orange-100) 0%,
345
+ var(--vps-orange) 50%,
346
+ var(--orange-100) 100%
347
+ );
348
+ animation: tpb-shimmer 1.6s ease-in-out infinite;
349
+ }
350
+
351
+ /* Phase label row */
352
+ .tpb-label-row {
353
+ display: flex;
354
+ align-items: center;
355
+ gap: 5px;
356
+ transition: opacity 0.3s ease, transform 0.3s ease;
357
+ }
358
+
359
+ .tpb-label-in { opacity: 1; transform: translateY(0); }
360
+ .tpb-label-out { opacity: 0; transform: translateY(-4px); }
361
+
362
+ .tpb-spinner-icon {
363
+ color: var(--vps-orange);
364
+ animation: tpb-spin 1s linear infinite;
365
+ flex-shrink: 0;
366
+ }
367
+
368
+ .tpb-phase-icon {
369
+ color: var(--grey-600);
370
+ flex-shrink: 0;
371
+ }
372
+
373
+ .tpb-phase-text {
374
+ font-size: 0.72rem;
375
+ color: var(--text-muted);
376
+ font-weight: 500;
377
+ letter-spacing: 0.01em;
378
+ }
379
+
380
+ /* ── Platform-specific Link Badges ──────────────────────────────────────────── */
381
+
382
+ /* LinkedIn */
383
+ .hl-link--linkedin {
384
+ color: #fff !important;
385
+ background: #0a66c2 !important;
386
+ border-bottom-color: #004a96 !important;
387
+ }
388
+ .hl-link--linkedin:hover {
389
+ background: #004a96 !important;
390
+ }
391
+
392
+ /* YouTube */
393
+ .hl-link--youtube {
394
+ color: #fff !important;
395
+ background: #ff0000 !important;
396
+ border-bottom-color: #cc0000 !important;
397
+ }
398
+ .hl-link--youtube:hover {
399
+ background: #cc0000 !important;
400
+ }
401
+
402
+ /* GitHub */
403
+ .hl-link--github {
404
+ color: #fff !important;
405
+ background: #24292f !important;
406
+ border-bottom-color: #000 !important;
407
+ }
408
+ .hl-link--github:hover {
409
+ background: #000 !important;
410
+ }
411
+
412
+ /* Twitter / X */
413
+ .hl-link--twitter {
414
+ color: #fff !important;
415
+ background: #1d9bf0 !important;
416
+ border-bottom-color: #0c7abf !important;
417
+ }
418
+ .hl-link--twitter:hover {
419
+ background: #0c7abf !important;
420
+ }
421
+
422
+ /* External hint globe icon inside link badge */
423
+ .hl-link-external-hint {
424
+ opacity: 0.6;
425
+ flex-shrink: 0;
426
+ }
427
+
428
+ /* ── Response Icon Mapper Badges ────────────────────────────────────────────── */
429
+
430
+ .resp-icon-badge {
431
+ display: inline-flex;
432
+ align-items: center;
433
+ justify-content: center;
434
+ width: 20px;
435
+ height: 20px;
436
+ border-radius: 50%;
437
+ flex-shrink: 0;
438
+ float: right;
439
+ margin: 0 0 4px 6px;
440
+ }
441
+
442
+ .resp-icon-badge--error { background: #fee2e2; color: #b91c1c; }
443
+ .resp-icon-badge--warning { background: #fef3c7; color: #b45309; }
444
+ .resp-icon-badge--success { background: #d1fae5; color: #065f46; }
445
+ .resp-icon-badge--info { background: #e0f2fe; color: #0369a1; }
446
+ .resp-icon-badge--linkedin { background: #dbeafe; color: #0a66c2; }
447
+ .resp-icon-badge--youtube { background: #fee2e2; color: #ff0000; }
448
+ .resp-icon-badge--link { background: var(--orange-50); color: var(--vps-orange); }
449
+ .resp-icon-badge--database { background: #e0e7ff; color: #4338ca; }
450
+ .resp-icon-badge--chem { background: #ccfbf1; color: #0f766e; }
451
+ .resp-icon-badge--vessel { background: #f1f5f9; color: #475569; }
452
+ .resp-icon-badge--muted { background: var(--grey-100); color: var(--grey-600); }
453
+
454
+ /* ── Header title flex row (badge lives here) ───────────────────────────────── */
455
+
456
+ .header-title-row {
457
+ display: flex;
458
+ align-items: center;
459
+ gap: 5px;
460
+ flex-wrap: nowrap;
461
+ }
462
+
463
+ /* ── EnvBadge ───────────────────────────────────────────────────────────────── */
464
+
465
+ @keyframes env-badge-ping {
466
+ 0% { transform: scale(1); opacity: 0.9; }
467
+ 70% { transform: scale(1.9); opacity: 0; }
468
+ 100% { transform: scale(1); opacity: 0; }
469
+ }
470
+
471
+ /* Base badge */
472
+ .env-badge {
473
+ display: inline-flex;
474
+ align-items: center;
475
+ gap: 4px;
476
+ padding: 2px 7px 2px 5px;
477
+ border-radius: 9999px;
478
+ font-size: 0.65rem;
479
+ font-weight: 700;
480
+ letter-spacing: 0.04em;
481
+ text-transform: uppercase;
482
+ white-space: nowrap;
483
+ line-height: 1;
484
+ user-select: none;
485
+ border: 1px solid transparent;
486
+ /* slide-in animation on mount */
487
+ animation: chat-pop 0.22s ease-out both;
488
+ }
489
+
490
+ /* Status dot */
491
+ .env-badge-dot {
492
+ display: inline-block;
493
+ width: 6px;
494
+ height: 6px;
495
+ border-radius: 50%;
496
+ background: currentColor;
497
+ opacity: 0.8;
498
+ flex-shrink: 0;
499
+ position: relative;
500
+ }
501
+
502
+ /* Pulsing ring overlay */
503
+ .env-badge-dot--pulse::before {
504
+ content: "";
505
+ position: absolute;
506
+ inset: -1px;
507
+ border-radius: 50%;
508
+ background: currentColor;
509
+ opacity: 0;
510
+ animation: env-badge-ping 1.8s cubic-bezier(0, 0, 0.2, 1) infinite;
511
+ }
512
+
513
+ .env-badge-icon {
514
+ opacity: 0.85;
515
+ flex-shrink: 0;
516
+ }
517
+
518
+ .env-badge-label {
519
+ flex-shrink: 0;
520
+ }
521
+
522
+ /* ── Stage variants ─────────────────────────────────────────────────────────── */
523
+
524
+ /* Alpha — purple */
525
+ .env-badge--alpha {
526
+ color: #6d28d9;
527
+ background: #ede9fe;
528
+ border-color: #c4b5fd;
529
+ }
530
+
531
+ /* Beta — royal blue */
532
+ .env-badge--beta {
533
+ color: #1d4ed8;
534
+ background: #dbeafe;
535
+ border-color: #93c5fd;
536
+ }
537
+
538
+ /* Gamma / RC — teal */
539
+ .env-badge--gamma {
540
+ color: #0f766e;
541
+ background: #ccfbf1;
542
+ border-color: #5eead4;
543
+ }
544
+
545
+ /* QA — amber */
546
+ .env-badge--qa {
547
+ color: #b45309;
548
+ background: #fef3c7;
549
+ border-color: #fcd34d;
550
+ }
package/src/lib/cn.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { clsx } from "clsx";
2
+
3
+ export function cn(...inputs: Array<string | false | null | undefined>) {
4
+ return clsx(inputs);
5
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * gibberishDetector.ts
3
+ *
4
+ * Detects unreadable / gibberish / random text that is not worth
5
+ * sending to the AI backend (saves LLM tokens).
6
+ *
7
+ * Strategy:
8
+ * 1. Entropy check – high Shannon entropy → likely random chars
9
+ * 2. Consonant-cluster ratio – too many consecutive consonants → not human words
10
+ * 3. No-vowel ratio – excessive sequences with no vowels
11
+ * 4. Keyboard-smash patterns – e.g. "asdfgh", "qwerty" runs
12
+ * 5. Minimum meaningful length – ignore very short strings (handled upstream)
13
+ *
14
+ * Returns { isGibberish: boolean, reason: string }
15
+ */
16
+
17
+ const VOWELS = new Set(["a", "e", "i", "o", "u"]);
18
+
19
+ /** Shannon entropy of a string (bits per character) */
20
+ function shannonEntropy(s: string): number {
21
+ const freq: Record<string, number> = {};
22
+ for (const ch of s) freq[ch] = (freq[ch] || 0) + 1;
23
+ const len = s.length;
24
+ return Object.values(freq).reduce((sum, count) => {
25
+ const p = count / len;
26
+ return sum - p * Math.log2(p);
27
+ }, 0);
28
+ }
29
+
30
+ /** Ratio of characters that are NOT a-z, A-Z, 0-9, or common punctuation */
31
+ function nonPrintableRatio(s: string): number {
32
+ const nonPrint = s.split("").filter((c) => !/[a-zA-Z0-9 .,!?'"\-:;@#$%&()\n]/.test(c));
33
+ return nonPrint.length / s.length;
34
+ }
35
+
36
+ /** Ratio of "words" (space-split tokens) that have no vowels */
37
+ function noVowelWordRatio(s: string): number {
38
+ const words = s.split(/\s+/).filter(Boolean);
39
+ if (words.length === 0) return 0;
40
+ const noVowelCount = words.filter((w) => {
41
+ const alpha = w.replace(/[^a-zA-Z]/g, "").toLowerCase();
42
+ if (alpha.length < 3) return false; // skip short tokens like "v" or "2"
43
+ return ![...alpha].some((c) => VOWELS.has(c));
44
+ }).length;
45
+ return noVowelCount / words.length;
46
+ }
47
+
48
+ /** Max run of consecutive consonants anywhere in the string */
49
+ function maxConsonantRun(s: string): number {
50
+ const lower = s.toLowerCase().replace(/[^a-z]/g, " ");
51
+ let max = 0;
52
+ let run = 0;
53
+ for (const ch of lower) {
54
+ if (ch === " ") {
55
+ run = 0;
56
+ } else if (!VOWELS.has(ch)) {
57
+ run++;
58
+ max = Math.max(max, run);
59
+ } else {
60
+ run = 0;
61
+ }
62
+ }
63
+ return max;
64
+ }
65
+
66
+ /** Detects keyboard-smash sequences (sequential keyboard rows) */
67
+ function hasKeyboardSmash(s: string): boolean {
68
+ const rows = [
69
+ "qwertyuiop",
70
+ "asdfghjkl",
71
+ "zxcvbnm",
72
+ "poiuytrewq",
73
+ "lkjhgfdsa",
74
+ "mnbvcxz",
75
+ ];
76
+ const lower = s.toLowerCase();
77
+ for (const row of rows) {
78
+ // look for ≥4 consecutive characters from the same keyboard row
79
+ for (let i = 0; i <= row.length - 4; i++) {
80
+ if (lower.includes(row.slice(i, i + 4))) return true;
81
+ }
82
+ }
83
+ return false;
84
+ }
85
+
86
+ export interface GibberishResult {
87
+ isGibberish: boolean;
88
+ reason: string;
89
+ }
90
+
91
+ /**
92
+ * Main export.
93
+ *
94
+ * @param input Raw user-typed string
95
+ * @returns { isGibberish, reason }
96
+ */
97
+ export function detectGibberish(input: string): GibberishResult {
98
+ const trimmed = input.trim();
99
+
100
+ // Very short inputs are fine — let the AI handle "hi", "ok", etc.
101
+ if (trimmed.length < 6) {
102
+ return { isGibberish: false, reason: "" };
103
+ }
104
+
105
+ // 1. Non-printable / symbol soup
106
+ const npRatio = nonPrintableRatio(trimmed);
107
+ if (npRatio > 0.35) {
108
+ return {
109
+ isGibberish: true,
110
+ reason: `High symbol/special-character ratio (${(npRatio * 100).toFixed(0)}%)`,
111
+ };
112
+ }
113
+
114
+ // Strip numbers and punctuation for linguistic checks
115
+ const alpha = trimmed.replace(/[^a-zA-Z\s]/g, "").trim();
116
+
117
+ // If after stripping there's almost nothing alphabetic, it may be all symbols/numbers — OK (e.g. ticket IDs), skip
118
+ if (alpha.length < 4) {
119
+ return { isGibberish: false, reason: "" };
120
+ }
121
+
122
+ // 2. Shannon entropy on alpha-only content
123
+ const entropy = shannonEntropy(alpha.toLowerCase());
124
+ // English prose ≈ 3.5-4.5 bits. Random letter strings → ≥ 4.7
125
+ if (entropy > 4.6 && alpha.length > 15) {
126
+ return {
127
+ isGibberish: true,
128
+ reason: `Very high character entropy (${entropy.toFixed(2)} bits) — looks random`,
129
+ };
130
+ }
131
+
132
+ // 3. No-vowel word ratio
133
+ const nvRatio = noVowelWordRatio(alpha);
134
+ if (nvRatio > 0.65) {
135
+ return {
136
+ isGibberish: true,
137
+ reason: `${(nvRatio * 100).toFixed(0)}% of words have no vowels`,
138
+ };
139
+ }
140
+
141
+ // 4. Max consonant run
142
+ const mcRun = maxConsonantRun(alpha);
143
+ if (mcRun >= 7) {
144
+ return {
145
+ isGibberish: true,
146
+ reason: `Consonant run of ${mcRun} characters — not human-readable`,
147
+ };
148
+ }
149
+
150
+ // 5. Keyboard smash
151
+ if (hasKeyboardSmash(trimmed) && trimmed.length < 30) {
152
+ return {
153
+ isGibberish: true,
154
+ reason: "Keyboard-smash pattern detected",
155
+ };
156
+ }
157
+
158
+ return { isGibberish: false, reason: "" };
159
+ }