@rhq/rover 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +362 -2
- package/eng/relay_rover.wasm +0 -0
- package/index.d.ts +192 -0
- package/index.js +425 -0
- package/package.json +25 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RelayHQ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,363 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rhq/rover (Relay Rover)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **High-Performance WebAssembly Email Intelligence Engine for Real-World Email Clients, React Native, and Relay Mail.**
|
|
4
|
+
> *Zero LLMs. Zero cloud API calls. 100% on-device deterministic understanding.*
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/@rhq/rover)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://webassembly.org/)
|
|
9
|
+
[](#performance-benchmarks)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## The Problem: Emails Are More Than Messages
|
|
14
|
+
|
|
15
|
+
Traditional email clients treat every message the same: they show sender, subject, and an excerpt of raw text or messy HTML, forcing the user to manually open the email to hunt for verification codes, tracking numbers, flight gates, or payment receipts.
|
|
16
|
+
|
|
17
|
+
> *"Gmail gives Relay the emails. Rover gives Relay an understanding of those emails."*
|
|
18
|
+
> — [*Meet Rover: The Intelligence Behind Relay Mail*](https://blogs.relaymail.online/blog/rover)
|
|
19
|
+
|
|
20
|
+
`@rhq/rover` is the production email intelligence engine behind **Relay Mail**. Written in Rust and compiled directly to WebAssembly (`eng/relay_rover.wasm`), Rover runs client-side inside the user's application with microsecond latency, zero operational costs, and total data privacy.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Key Capabilities
|
|
25
|
+
|
|
26
|
+
- **Dual-Layer Classification:**
|
|
27
|
+
- **Folder Layer (General):** `Primary`, `Updates`, `Social`, `Promotions`, `Forums`.
|
|
28
|
+
- **Action Layer (Purpose):** `Authentication` (OTP), `Transactions` (Payment/Invoice), `Deliveries` (Shopping), `Jobs`, `SecurityAlerts`, `Travel`, `Subscriptions`, `Newsletters`.
|
|
29
|
+
- **Urgency & Alert Engine:** Automatically flags critical emails with `is_alert: true` and urgency tiers (`Urgent`, `High`, `Normal`, `Low`) for real-time notification banners and inbox badges.
|
|
30
|
+
- **Deep Deterministic Extraction:**
|
|
31
|
+
- **OTPs:** Extract code, service, expiration timeframe, and auth purpose (e.g. `0313`, `Nintendo`, `24 hours`).
|
|
32
|
+
- **Orders & Deliveries:** Extract order ID, merchant, product, amount, tax/GST, order date, and delivery status.
|
|
33
|
+
- **Payments & Receipts:** Extract amount, currency, payer, recipient, transaction ID, date, time, and payment method.
|
|
34
|
+
- **Job Vacancies & Alerts:** Extract company, position, location, job vacancy count, and application URLs.
|
|
35
|
+
- **Social & Alerts:** Extract actor, interaction type, target post URL, or CI/CD build failure details.
|
|
36
|
+
- **Semantic Contextual Previews:** Generates clean, informative preview lines (< 100 characters) tailored to the category (e.g., `₹29.00 · Special Airdrop 412 (Google Play)` or `Job Alert · 8 React Native Developer jobs at Webomindapps`).
|
|
37
|
+
- **The Rover Intelligence Index:** Computes `timeline_key` identifiers to automatically group related emails (Order Confirmation $\rightarrow$ Payment Receipt $\rightarrow$ Shipment $\rightarrow$ Delivery) into connected purchase and activity timelines.
|
|
38
|
+
- **HTML Sanitization & Normalization:** Built-in fast HTML-to-text parser that strips `<style>`, scripts, and tracking markup without third-party dependencies.
|
|
39
|
+
- **Blistering Throughput:** Processes over **4,900 emails/second** in WebAssembly and **9,200 emails/second** natively.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @rhq/rover
|
|
47
|
+
# or with pnpm
|
|
48
|
+
pnpm add @rhq/rover
|
|
49
|
+
# or with yarn
|
|
50
|
+
yarn add @rhq/rover
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Integrating Into an Email Client Application
|
|
56
|
+
|
|
57
|
+
### 1. Ingesting Real-World Raw Emails (IMAP, Gmail API, JMAP)
|
|
58
|
+
|
|
59
|
+
In a production email application, incoming emails contain messy HTML, multi-part bodies, and complex header structures. Rover accepts raw email objects, normalizes headers, sanitizes HTML, and extracts intelligence in one call:
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import { RelayRover } from '@rhq/rover';
|
|
63
|
+
|
|
64
|
+
// Example raw email payload from Gmail API / IMAP / backend sync
|
|
65
|
+
const rawEmail = {
|
|
66
|
+
id: "msg_18b5ef21a4",
|
|
67
|
+
from: "Google Play <googleplay-noreply@google.com>",
|
|
68
|
+
subject: "Your Google Play Order Receipt from 27 Oct 2026",
|
|
69
|
+
date: "2026-10-27T12:29:12+05:30",
|
|
70
|
+
htmlBody: `
|
|
71
|
+
<div>
|
|
72
|
+
<style>.btn { color: red; }</style>
|
|
73
|
+
<h1>Order number: GPA.3399-0949-2150-45472</h1>
|
|
74
|
+
<p>Special Airdrop 412 (Free Fire MAX x NARUTO) ₹29.00</p>
|
|
75
|
+
<p>Total: ₹29.00 (Includes GST of ₹4.42)</p>
|
|
76
|
+
<p>Payment method: Google Play balance</p>
|
|
77
|
+
</div>
|
|
78
|
+
`
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Process email with Rover
|
|
82
|
+
const processed = RelayRover.process(rawEmail);
|
|
83
|
+
|
|
84
|
+
console.log(processed.primary_category);
|
|
85
|
+
// "shopping"
|
|
86
|
+
|
|
87
|
+
console.log(processed.preview);
|
|
88
|
+
// "₹29.00 · Special Airdrop 412 (Google Play)"
|
|
89
|
+
|
|
90
|
+
console.log(processed.classification);
|
|
91
|
+
// {
|
|
92
|
+
// general_category: "updates",
|
|
93
|
+
// purpose: "shopping",
|
|
94
|
+
// importance: "normal",
|
|
95
|
+
// is_alert: false,
|
|
96
|
+
// security: "none",
|
|
97
|
+
// confidence: 0.95
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
console.log(processed.extraction.data);
|
|
101
|
+
// {
|
|
102
|
+
// store: "Google Play",
|
|
103
|
+
// order_id: "GPA.3399-0949-2150-45472",
|
|
104
|
+
// product: "Special Airdrop 412",
|
|
105
|
+
// amount: "29.00",
|
|
106
|
+
// currency: "INR",
|
|
107
|
+
// tax: "4.42",
|
|
108
|
+
// order_date: "27 Oct 2026",
|
|
109
|
+
// payment_method: "Google Play balance",
|
|
110
|
+
// timeline_key: "order:GPA.3399-0949-2150-45472"
|
|
111
|
+
// }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### 2. High-Speed Batch Ingestion (Initial Sync & Background Fetch)
|
|
117
|
+
|
|
118
|
+
When your app syncs an inbox folder or executes a background fetch, pass the array of emails to `processBatch()`. The entire batch is processed in WebAssembly memory in a single invocation without round-trips:
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
import { RelayRover } from '@rhq/rover';
|
|
122
|
+
|
|
123
|
+
// Syncing 100+ emails from the local SQLite store or IMAP cache
|
|
124
|
+
async function onInboxSync(incomingMessages) {
|
|
125
|
+
const startTime = performance.now();
|
|
126
|
+
|
|
127
|
+
// Process 100 emails in ~20 ms (~4,900 emails/sec)
|
|
128
|
+
const results = RelayRover.processBatch(incomingMessages);
|
|
129
|
+
|
|
130
|
+
const duration = (performance.now() - startTime).toFixed(1);
|
|
131
|
+
console.log(`Classified & extracted ${results.length} emails in ${duration} ms`);
|
|
132
|
+
|
|
133
|
+
// Persist structured metadata into your SQLite or Realm database
|
|
134
|
+
for (const item of results) {
|
|
135
|
+
await db.emails.update(item.id, {
|
|
136
|
+
category: item.primary_category,
|
|
137
|
+
folder: item.classification.general_category,
|
|
138
|
+
previewText: item.preview,
|
|
139
|
+
importance: item.classification.importance,
|
|
140
|
+
isAlert: item.classification.is_alert,
|
|
141
|
+
timelineKey: item.timeline_key,
|
|
142
|
+
extractedData: JSON.stringify(item.extraction.data),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
### 3. Rendering Actionable Inbox Cards & Action Banners
|
|
151
|
+
|
|
152
|
+
Rover provides structured fields that allow your UI to render rich, actionable elements directly in the inbox feed without opening the email:
|
|
153
|
+
|
|
154
|
+
#### A. One-Tap OTP Copy Banner
|
|
155
|
+
```tsx
|
|
156
|
+
import React from 'react';
|
|
157
|
+
import { View, Text, TouchableOpacity, Clipboard } from 'react-native';
|
|
158
|
+
|
|
159
|
+
export function OtpActionBanner({ processedEmail }) {
|
|
160
|
+
if (processedEmail.primary_category !== 'otp') return null;
|
|
161
|
+
|
|
162
|
+
const { otp_code, service_name, expiration_time } = processedEmail.extraction.data;
|
|
163
|
+
if (!otp_code) return null;
|
|
164
|
+
|
|
165
|
+
return (
|
|
166
|
+
<View style={{ backgroundColor: '#EEF2FF', padding: 12, borderRadius: 8, flexDirection: 'row', alignItems: 'center' }}>
|
|
167
|
+
<View style={{ flex: 1 }}>
|
|
168
|
+
<Text style={{ fontWeight: 'bold', color: '#1E40AF' }}>
|
|
169
|
+
{service_name || 'Authentication'} Verification Code
|
|
170
|
+
</Text>
|
|
171
|
+
<Text style={{ fontSize: 24, letterSpacing: 4, fontWeight: '700', color: '#1E1B4B' }}>
|
|
172
|
+
{otp_code}
|
|
173
|
+
</Text>
|
|
174
|
+
{expiration_time && (
|
|
175
|
+
<Text style={{ fontSize: 12, color: '#6B7280' }}>Expires in {expiration_time}</Text>
|
|
176
|
+
)}
|
|
177
|
+
</View>
|
|
178
|
+
<TouchableOpacity
|
|
179
|
+
onPress={() => Clipboard.setString(otp_code)}
|
|
180
|
+
style={{ backgroundColor: '#3B82F6', paddingHorizontal: 16, paddingVertical: 8, borderRadius: 6 }}
|
|
181
|
+
>
|
|
182
|
+
<Text style={{ color: '#fff', fontWeight: '600' }}>Copy</Text>
|
|
183
|
+
</TouchableOpacity>
|
|
184
|
+
</View>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### B. Delivery Tracking Card
|
|
190
|
+
```tsx
|
|
191
|
+
export function DeliveryCard({ processedEmail }) {
|
|
192
|
+
if (processedEmail.primary_category !== 'shopping') return null;
|
|
193
|
+
const { store, product, tracking_number, delivery_status } = processedEmail.extraction.data;
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<View style={{ padding: 12, borderWidth: 1, borderColor: '#E5E7EB', borderRadius: 8 }}>
|
|
197
|
+
<Text style={{ fontSize: 14, color: '#4B5563' }}>{store} · {delivery_status}</Text>
|
|
198
|
+
<Text style={{ fontSize: 16, fontWeight: 'bold' }}>{product}</Text>
|
|
199
|
+
{tracking_number && (
|
|
200
|
+
<TouchableOpacity onPress={() => openTracking(tracking_number)}>
|
|
201
|
+
<Text style={{ color: '#2563EB', marginTop: 4 }}>Track Package: {tracking_number}</Text>
|
|
202
|
+
</TouchableOpacity>
|
|
203
|
+
)}
|
|
204
|
+
</View>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
### 4. Connecting Fragmented Emails with the Rover Intelligence Index
|
|
212
|
+
|
|
213
|
+
Purchases, trips, and projects often scatter multiple emails over days or weeks:
|
|
214
|
+
1. *Order Confirmation*
|
|
215
|
+
2. *Payment Receipt*
|
|
216
|
+
3. *Shipping Update*
|
|
217
|
+
4. *Delivered Notification*
|
|
218
|
+
|
|
219
|
+
Rover provides `RoverIndex` to automatically connect these related communications into unified activity timelines:
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
import { RelayRover, RoverIndex } from '@rhq/rover';
|
|
223
|
+
|
|
224
|
+
// Array of all emails in the inbox
|
|
225
|
+
const processedEmails = RelayRover.processBatch(inboxEmails);
|
|
226
|
+
|
|
227
|
+
// Group related emails by timeline key
|
|
228
|
+
const { timelines, unlinked } = RoverIndex.buildTimelines(processedEmails);
|
|
229
|
+
|
|
230
|
+
// Inspect continuous order timeline:
|
|
231
|
+
console.log(timelines["order:GPA.3399-0949-2150-45472"]);
|
|
232
|
+
/*
|
|
233
|
+
{
|
|
234
|
+
timelineKey: "order:GPA.3399-0949-2150-45472",
|
|
235
|
+
primaryCategory: "shopping",
|
|
236
|
+
events: [
|
|
237
|
+
{ preview: "Order Confirmed · Special Airdrop 412", date: "2026-10-27" },
|
|
238
|
+
{ preview: "₹29.00 · Google Play receipt", date: "2026-10-27" },
|
|
239
|
+
{ preview: "In Transit · Special Airdrop 412", date: "2026-10-28" }
|
|
240
|
+
]
|
|
241
|
+
}
|
|
242
|
+
*/
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
### 5. Multi-Folder Inbox Navigation
|
|
248
|
+
|
|
249
|
+
Use the dual-layer classification to power your email client's primary navigation tabs and sub-category filters:
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// Filter emails for the "Primary" or "Updates" folder tab
|
|
253
|
+
const primaryInbox = allEmails.filter(
|
|
254
|
+
email => email.classification.general_category === 'primary'
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const updatesTab = allEmails.filter(
|
|
258
|
+
email => email.classification.general_category === 'updates'
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
// Filter by actionable purpose chip in your search bar
|
|
262
|
+
const allOrders = allEmails.filter(
|
|
263
|
+
email => email.primary_category === 'shopping'
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const pendingJobs = allEmails.filter(
|
|
267
|
+
email => email.primary_category === 'job'
|
|
268
|
+
);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Complete API Reference
|
|
274
|
+
|
|
275
|
+
### `RelayRover.process(email: EmailRecordInput | string): ProcessedEmail`
|
|
276
|
+
Processes an email object or text string. Automatically handles HTML sanitization, header normalization, category detection, typed entity extraction, and contextual preview generation.
|
|
277
|
+
|
|
278
|
+
### `RelayRover.processBatch(emails: EmailRecordInput[] | string): ProcessedEmail[]`
|
|
279
|
+
Processes an array of emails in a single linear WebAssembly memory call. Designed for initial inbox loads and background sync workers.
|
|
280
|
+
|
|
281
|
+
### `RelayRover.classify(subject: string, body: string): string`
|
|
282
|
+
Ultra-fast sub-millisecond purpose category classification without full extraction.
|
|
283
|
+
|
|
284
|
+
### `RelayRover.generatePreview(subject: string, body: string): string`
|
|
285
|
+
Generates a concise contextual summary line (< 100 characters) for push notifications or inbox row subtitles.
|
|
286
|
+
|
|
287
|
+
### `RoverIndex.buildTimelines(processedEmails: ProcessedEmail[]): TimelineBuildResult`
|
|
288
|
+
Groups an array of processed emails into connected timelines (`timelines`) and standalone emails (`unlinked`) using deterministic timeline keys (`order:*`, `payment:*`, `travel:*`, `invoice:*`).
|
|
289
|
+
|
|
290
|
+
### `cleanEmailBody(email: EmailRecordInput | string): string`
|
|
291
|
+
Extracts readable plain-text, strips script and style blocks, unescapes entities, and preserves line breaks.
|
|
292
|
+
|
|
293
|
+
### `htmlToText(html: string): string`
|
|
294
|
+
Fast WASM-powered HTML-to-plain-text sanitizer.
|
|
295
|
+
|
|
296
|
+
### `normalizeEmail(email: EmailRecordInput): NormalizedEmail`
|
|
297
|
+
Standardizes various header formats (`from`, `sender`, `sender_name`, `date`, `received_at`) into a uniform record.
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## TypeScript Typings & Extraction Schemas
|
|
302
|
+
|
|
303
|
+
All types are exported from `@rhq/rover`:
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
import type {
|
|
307
|
+
ProcessedEmail,
|
|
308
|
+
EmailClassification,
|
|
309
|
+
GeneralCategory,
|
|
310
|
+
Importance,
|
|
311
|
+
SecurityClassification,
|
|
312
|
+
PaymentExtraction,
|
|
313
|
+
OtpExtraction,
|
|
314
|
+
JobExtraction,
|
|
315
|
+
ShoppingExtraction,
|
|
316
|
+
SocialExtraction
|
|
317
|
+
} from '@rhq/rover';
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Extraction Types Summary
|
|
321
|
+
|
|
322
|
+
- **`OtpExtraction`**: `otp_code`, `service_name`, `expiration_time`, `auth_purpose`, `action_url`
|
|
323
|
+
- **`ShoppingExtraction`**: `store`, `order_id`, `product`, `amount`, `currency`, `tax`, `order_date`, `delivery_status`, `tracking_number`, `courier`, `payment_method`, `timeline_key`
|
|
324
|
+
- **`PaymentExtraction`**: `amount`, `currency`, `payer`, `recipient`, `transaction_id`, `transaction_date`, `transaction_time`, `payment_method`, `transaction_status`, `timeline_key`
|
|
325
|
+
- **`JobExtraction`**: `company`, `position`, `location`, `salary`, `job_count`, `is_job_alert`, `job_url`, `application_deadline`, `interview_date`
|
|
326
|
+
- **`SocialExtraction`**: `platform`, `activity_type`, `relevant_entity`, `actor`, `post_url`
|
|
327
|
+
- **`SecurityAlertExtraction`**: `alert_type`, `target_resource`, `severity`, `action_required`, `timeline_key`
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## Performance & Architecture Comparison
|
|
332
|
+
|
|
333
|
+
| Metric / Feature | Relay Rover (`@rhq/rover`) | Cloud LLM (e.g. GPT-4o / Claude) |
|
|
334
|
+
|---|---|---|
|
|
335
|
+
| **Latency per Email** | **~200 µs** (0.2 ms) | **1,200 ms – 3,500 ms** |
|
|
336
|
+
| **Throughput** | **4,900+ emails/sec** | 2 – 10 emails/sec (rate limited) |
|
|
337
|
+
| **Cost per 100,000 Emails** | **$0.00 (Free, runs on-device)** | **$1,500 – $3,000+** in tokens |
|
|
338
|
+
| **Network Dependency** | **100% Offline Capable** | Requires active internet & API keys |
|
|
339
|
+
| **Privacy & Compliance** | **Zero Data Leaves Device** | Full user email sent to cloud |
|
|
340
|
+
| **Determinism** | **100% Consistent (Zero Hallucination)** | Variable responses, model drift |
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## Compiling & Updating the Engine
|
|
345
|
+
|
|
346
|
+
To rebuild the WebAssembly binary from Rust source:
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
# 1. Compile optimized release target
|
|
350
|
+
cargo build --target wasm32-unknown-unknown --release
|
|
351
|
+
|
|
352
|
+
# 2. Copy compiled binary into package eng/ directory
|
|
353
|
+
cp target/wasm32-unknown-unknown/release/relay_rover.wasm packages/rover/eng/relay_rover.wasm
|
|
354
|
+
|
|
355
|
+
# 3. Run validation tests
|
|
356
|
+
node test_rover.js
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
361
|
+
## License
|
|
362
|
+
|
|
363
|
+
MIT © [Relay](https://blogs.relaymail.online)
|
|
Binary file
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
export type Category =
|
|
2
|
+
| 'Payment'
|
|
3
|
+
| 'Otp'
|
|
4
|
+
| 'Job'
|
|
5
|
+
| 'Shopping'
|
|
6
|
+
| 'Invoice'
|
|
7
|
+
| 'Subscription'
|
|
8
|
+
| 'Travel'
|
|
9
|
+
| 'Newsletter'
|
|
10
|
+
| 'Social'
|
|
11
|
+
| 'SecurityAlert'
|
|
12
|
+
| 'General';
|
|
13
|
+
|
|
14
|
+
export type GeneralCategory =
|
|
15
|
+
| 'primary'
|
|
16
|
+
| 'social'
|
|
17
|
+
| 'promotions'
|
|
18
|
+
| 'updates'
|
|
19
|
+
| 'forums'
|
|
20
|
+
| 'spam';
|
|
21
|
+
|
|
22
|
+
export type Importance = 'urgent' | 'high' | 'normal' | 'low';
|
|
23
|
+
|
|
24
|
+
export type SecurityClassification =
|
|
25
|
+
| 'authentication'
|
|
26
|
+
| 'password_reset'
|
|
27
|
+
| 'security_alert'
|
|
28
|
+
| 'account_change'
|
|
29
|
+
| 'build_failure'
|
|
30
|
+
| 'action_required'
|
|
31
|
+
| 'none';
|
|
32
|
+
|
|
33
|
+
export interface ConfidenceFactors {
|
|
34
|
+
authenticated_sender: boolean;
|
|
35
|
+
has_structured_entities: boolean;
|
|
36
|
+
pattern_specificity: string;
|
|
37
|
+
subject_body_aligned: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface EmailClassification {
|
|
41
|
+
general_category: GeneralCategory;
|
|
42
|
+
purpose: Category;
|
|
43
|
+
importance: Importance;
|
|
44
|
+
is_alert: boolean;
|
|
45
|
+
security: SecurityClassification;
|
|
46
|
+
confidence: number;
|
|
47
|
+
factors: ConfidenceFactors;
|
|
48
|
+
secondary_purposes: Category[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ProcessedEmail<T = any> {
|
|
52
|
+
id: string;
|
|
53
|
+
primary_category: string;
|
|
54
|
+
matched_categories: string[];
|
|
55
|
+
classification?: EmailClassification;
|
|
56
|
+
extraction: {
|
|
57
|
+
category: string;
|
|
58
|
+
data: T;
|
|
59
|
+
};
|
|
60
|
+
preview: string;
|
|
61
|
+
timeline_key?: string;
|
|
62
|
+
cleanBody?: string;
|
|
63
|
+
sender?: string;
|
|
64
|
+
senderName?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface EmailTimelineGroup {
|
|
68
|
+
timelineKey: string;
|
|
69
|
+
primaryCategory: string;
|
|
70
|
+
events: ProcessedEmail[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface TimelineBuildResult {
|
|
74
|
+
timelines: Record<string, EmailTimelineGroup>;
|
|
75
|
+
unlinked: ProcessedEmail[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export declare const RoverIndex: {
|
|
79
|
+
buildTimelines(processedEmails: ProcessedEmail[]): TimelineBuildResult;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export interface EmailRecordInput {
|
|
83
|
+
id?: string;
|
|
84
|
+
sender?: string;
|
|
85
|
+
sender_name?: string;
|
|
86
|
+
from?: any;
|
|
87
|
+
subject?: string;
|
|
88
|
+
body?: string;
|
|
89
|
+
textBody?: string;
|
|
90
|
+
htmlBody?: string;
|
|
91
|
+
content?: string;
|
|
92
|
+
date?: string;
|
|
93
|
+
received_at?: string;
|
|
94
|
+
headers?: any;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface NormalizedEmail {
|
|
98
|
+
id: string;
|
|
99
|
+
sender: string;
|
|
100
|
+
senderName: string;
|
|
101
|
+
subject: string;
|
|
102
|
+
body: string;
|
|
103
|
+
receivedAt: string;
|
|
104
|
+
isHtml: boolean;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convert HTML email content to clean, readable plain text.
|
|
109
|
+
* Strips scripts, styles, HTML tags, unescapes entities, and normalizes whitespace.
|
|
110
|
+
*/
|
|
111
|
+
export declare function htmlToText(html: string): string;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Extract clean, readable normal message text from an email object or text string.
|
|
115
|
+
* Handles massive HTML emails, raw multipart text bodies, and synthetic email formats.
|
|
116
|
+
*/
|
|
117
|
+
export declare function cleanEmailBody(email: EmailRecordInput | string): string;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Normalize an email of any format into a standardized structured object.
|
|
121
|
+
*/
|
|
122
|
+
export declare function normalizeEmail(email: EmailRecordInput): NormalizedEmail;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Generate a concise snippet/summary from an email body for inbox listings.
|
|
126
|
+
*/
|
|
127
|
+
export declare function extractSummary(email: EmailRecordInput | string, maxLength?: number): string;
|
|
128
|
+
|
|
129
|
+
export declare const RelayRover: {
|
|
130
|
+
/**
|
|
131
|
+
* Explicitly initialize the WebAssembly engine with custom bytes (optional).
|
|
132
|
+
*/
|
|
133
|
+
init(customBytes?: ArrayBuffer | Uint8Array): any;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fast category detection for any arbitrary email subject and body.
|
|
137
|
+
*/
|
|
138
|
+
classify(subject: string, body: string): string;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Generate concise contextual preview line (< 100 characters).
|
|
142
|
+
*/
|
|
143
|
+
generatePreview(subject: string, body: string): string;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Process email with full category detection, typed extraction, and preview generation.
|
|
147
|
+
*/
|
|
148
|
+
processEmail(subject: string, body: string): ProcessedEmail;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Process a single email record object or string (auto-handles HTML and extracts cleanBody).
|
|
152
|
+
*/
|
|
153
|
+
process(email: EmailRecordInput | string): ProcessedEmail;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Process multiple emails in a SINGLE in-memory WebAssembly call.
|
|
157
|
+
* Accepts an array of email objects (including raw Relay Mail with htmlBody) or a serialized JSON string.
|
|
158
|
+
*/
|
|
159
|
+
processBatch(emails: EmailRecordInput[] | string): ProcessedEmail[];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Alias for processBatch to process an array of JSON email records.
|
|
163
|
+
*/
|
|
164
|
+
processEmailsJson(emails: EmailRecordInput[] | string): ProcessedEmail[];
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Convert HTML email content to clean, readable plain text.
|
|
168
|
+
*/
|
|
169
|
+
htmlToText(html: string): string;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Extract clean, readable normal message text from an email object or text.
|
|
173
|
+
*/
|
|
174
|
+
cleanEmailBody(email: EmailRecordInput | string): string;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Normalize an email of any format into a standardized structured object.
|
|
178
|
+
*/
|
|
179
|
+
normalizeEmail(email: EmailRecordInput): NormalizedEmail;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Generate a concise summary snippet from an email body.
|
|
183
|
+
*/
|
|
184
|
+
extractSummary(email: EmailRecordInput | string, maxLength?: number): string;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Rover Intelligence Index timeline grouping engine.
|
|
188
|
+
*/
|
|
189
|
+
index: typeof RoverIndex;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export default RelayRover;
|
package/index.js
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @rhq/rover (Relay Rover)
|
|
3
|
+
* High-performance WebAssembly Email Intelligence Engine for React Native and JavaScript.
|
|
4
|
+
* Zero-dependency, in-memory execution powered by compiled Rust WASM.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
|
|
11
|
+
let wasmInstance = null;
|
|
12
|
+
let wasmExports = null;
|
|
13
|
+
let memory = null;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ensure the WASM engine is loaded and initialized.
|
|
17
|
+
* Can be called explicitly with custom bytes or lazy-initializes on first call.
|
|
18
|
+
*
|
|
19
|
+
* @param {ArrayBuffer|Uint8Array} [customBytes] Optional custom WASM binary buffer
|
|
20
|
+
*/
|
|
21
|
+
export function init(customBytes) {
|
|
22
|
+
if (wasmExports) {
|
|
23
|
+
return wasmExports;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let bytes = customBytes;
|
|
27
|
+
|
|
28
|
+
if (!bytes) {
|
|
29
|
+
// 1. Try reading from eng/relay_rover.wasm relative to this module
|
|
30
|
+
try {
|
|
31
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
32
|
+
const __dirname = path.dirname(__filename);
|
|
33
|
+
const wasmPath = path.resolve(__dirname, 'eng/relay_rover.wasm');
|
|
34
|
+
if (fs.existsSync(wasmPath)) {
|
|
35
|
+
bytes = fs.readFileSync(wasmPath);
|
|
36
|
+
}
|
|
37
|
+
} catch (_) {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!bytes) {
|
|
41
|
+
// 2. Try global WASM binary if injected by React Native or bundler
|
|
42
|
+
if (typeof globalThis !== 'undefined' && globalThis.__RELAY_ROVER_WASM__) {
|
|
43
|
+
bytes = globalThis.__RELAY_ROVER_WASM__;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!bytes) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
'Relay Rover WASM engine binary not found. Call RelayRover.init(wasmBytes) or ensure eng/relay_rover.wasm is present.'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const module = new WebAssembly.Module(bytes);
|
|
54
|
+
wasmInstance = new WebAssembly.Instance(module, {});
|
|
55
|
+
wasmExports = wasmInstance.exports;
|
|
56
|
+
memory = wasmExports.memory;
|
|
57
|
+
return wasmExports;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getExports() {
|
|
61
|
+
if (!wasmExports) {
|
|
62
|
+
init();
|
|
63
|
+
}
|
|
64
|
+
return wasmExports;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function writeStringToMemory(exp, mem, str) {
|
|
68
|
+
const encoder = new TextEncoder();
|
|
69
|
+
const encoded = encoder.encode(str || '');
|
|
70
|
+
const ptr = exp.rover_alloc(encoded.length);
|
|
71
|
+
const heap = new Uint8Array(mem.buffer, ptr, encoded.length);
|
|
72
|
+
heap.set(encoded);
|
|
73
|
+
return { ptr, len: encoded.length };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function readStringFromMemory(exp, mem, ptr) {
|
|
77
|
+
const view = new DataView(mem.buffer, ptr);
|
|
78
|
+
const len = view.getUint32(0, true);
|
|
79
|
+
const bytes = new Uint8Array(mem.buffer, ptr + 4, len);
|
|
80
|
+
const text = new TextDecoder('utf-8').decode(bytes);
|
|
81
|
+
exp.rover_dealloc(ptr, len + 4);
|
|
82
|
+
return text;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Convert HTML email content to clean, readable plain text.
|
|
87
|
+
* Strips script tags, style blocks, HTML tags, unescapes entities, and collapses excessive whitespace.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} html Raw HTML email string
|
|
90
|
+
* @returns {string} Clean plain-text message
|
|
91
|
+
*/
|
|
92
|
+
export function htmlToText(html) {
|
|
93
|
+
if (!html || typeof html !== 'string') return '';
|
|
94
|
+
|
|
95
|
+
// Use fast Rust WASM HTML-to-text if engine is loaded
|
|
96
|
+
try {
|
|
97
|
+
const exp = getExports();
|
|
98
|
+
if (exp && typeof exp.rover_html_to_text === 'function') {
|
|
99
|
+
const input = writeStringToMemory(exp, memory, html);
|
|
100
|
+
const resPtr = exp.rover_html_to_text(input.ptr, input.len);
|
|
101
|
+
const text = readStringFromMemory(exp, memory, resPtr);
|
|
102
|
+
exp.rover_dealloc(input.ptr, input.len);
|
|
103
|
+
return text;
|
|
104
|
+
}
|
|
105
|
+
} catch (_) {}
|
|
106
|
+
|
|
107
|
+
// JavaScript fallback
|
|
108
|
+
return html
|
|
109
|
+
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, ' ')
|
|
110
|
+
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ' ')
|
|
111
|
+
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, ' ')
|
|
112
|
+
.replace(/<\/(p|div|tr|h[1-6]|li|blockquote)>/gi, '\n')
|
|
113
|
+
.replace(/<(br|hr)\s*\/?>/gi, '\n')
|
|
114
|
+
.replace(/<[^>]+>/g, ' ')
|
|
115
|
+
.replace(/ /gi, ' ')
|
|
116
|
+
.replace(/&/gi, '&')
|
|
117
|
+
.replace(/</gi, '<')
|
|
118
|
+
.replace(/>/gi, '>')
|
|
119
|
+
.replace(/"/gi, '"')
|
|
120
|
+
.replace(/'/gi, "'")
|
|
121
|
+
.replace(/'/gi, "'")
|
|
122
|
+
.replace(/[ \t]+/g, ' ')
|
|
123
|
+
.replace(/\r?\n\s*\r?\n+/g, '\n\n')
|
|
124
|
+
.trim();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Extract clean, readable normal message text from an email object or text.
|
|
129
|
+
* Handles massive HTML emails, raw multipart text bodies, and synthetic email formats.
|
|
130
|
+
*
|
|
131
|
+
* @param {object|string} email Email record or body string
|
|
132
|
+
* @returns {string} Clean message text
|
|
133
|
+
*/
|
|
134
|
+
export function cleanEmailBody(email) {
|
|
135
|
+
if (!email) return '';
|
|
136
|
+
if (typeof email === 'string') {
|
|
137
|
+
if (/<[a-z][\s\S]*>/i.test(email)) {
|
|
138
|
+
return htmlToText(email);
|
|
139
|
+
}
|
|
140
|
+
return email.replace(/\r\n/g, '\n').trim();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 1. Check textBody / body / content
|
|
144
|
+
const textCandidate = email.textBody || email.body || email.content || '';
|
|
145
|
+
if (typeof textCandidate === 'string' && textCandidate.trim().length > 0) {
|
|
146
|
+
if (/<[a-z][\s\S]*>/i.test(textCandidate)) {
|
|
147
|
+
return htmlToText(textCandidate);
|
|
148
|
+
}
|
|
149
|
+
return textCandidate.replace(/\r\n/g, '\n').trim();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// 2. Fallback to htmlBody if textBody is empty (common in marketing / newsletters)
|
|
153
|
+
if (email.htmlBody && typeof email.htmlBody === 'string') {
|
|
154
|
+
return htmlToText(email.htmlBody);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return '';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Normalize an email of any format (Relay Mail, RFC822-style, or synthetic)
|
|
162
|
+
* into a standardized structured object ready for processing and display.
|
|
163
|
+
*
|
|
164
|
+
* @param {object} email Raw email object
|
|
165
|
+
* @returns {object} Normalized email object
|
|
166
|
+
*/
|
|
167
|
+
export function normalizeEmail(email) {
|
|
168
|
+
if (!email || typeof email !== 'object') {
|
|
169
|
+
throw new TypeError('Expected an email object');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let sender = 'unknown@example.com';
|
|
173
|
+
let senderName = '';
|
|
174
|
+
|
|
175
|
+
if (email.from) {
|
|
176
|
+
if (Array.isArray(email.from) && email.from.length > 0) {
|
|
177
|
+
sender = email.from[0].email || sender;
|
|
178
|
+
senderName = email.from[0].name || '';
|
|
179
|
+
} else if (typeof email.from === 'object') {
|
|
180
|
+
sender = email.from.email || sender;
|
|
181
|
+
senderName = email.from.name || '';
|
|
182
|
+
} else if (typeof email.from === 'string') {
|
|
183
|
+
const match = email.from.match(/(.*?)\s*<([^>]+)>/);
|
|
184
|
+
if (match) {
|
|
185
|
+
senderName = match[1].trim();
|
|
186
|
+
sender = match[2].trim();
|
|
187
|
+
} else {
|
|
188
|
+
sender = email.from.trim();
|
|
189
|
+
senderName = '';
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
if (email.sender) sender = email.sender;
|
|
194
|
+
if (email.sender_name) senderName = email.sender_name;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const subject = (email.subject || '').replace(/\r?\n/g, ' ').trim();
|
|
198
|
+
const body = cleanEmailBody(email);
|
|
199
|
+
const receivedAt = email.date || email.received_at || new Date().toISOString();
|
|
200
|
+
const id = email.id || `email_${Date.now()}`;
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
id,
|
|
204
|
+
sender,
|
|
205
|
+
senderName,
|
|
206
|
+
subject,
|
|
207
|
+
body,
|
|
208
|
+
receivedAt,
|
|
209
|
+
isHtml: Boolean(!email.textBody && email.htmlBody),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Generate a concise snippet/summary from an email body for inbox listings.
|
|
215
|
+
*
|
|
216
|
+
* @param {object|string} email Email record or text
|
|
217
|
+
* @param {number} [maxLength=100] Maximum character length
|
|
218
|
+
* @returns {string} Truncated message summary
|
|
219
|
+
*/
|
|
220
|
+
export function extractSummary(email, maxLength = 100) {
|
|
221
|
+
const text = typeof email === 'string' ? email : cleanEmailBody(email);
|
|
222
|
+
const clean = text.replace(/\s+/g, ' ').trim();
|
|
223
|
+
if (clean.length <= maxLength) return clean;
|
|
224
|
+
return clean.slice(0, maxLength - 3).trim() + '...';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Rover Intelligence Index
|
|
229
|
+
* Organizes emails into connected purchase, project, and activity timelines.
|
|
230
|
+
*/
|
|
231
|
+
export const RoverIndex = {
|
|
232
|
+
/**
|
|
233
|
+
* Group processed emails into connected timelines using their deterministic timeline keys.
|
|
234
|
+
* @param {Array<object>} processedEmails
|
|
235
|
+
* @returns {{ timelines: Record<string, object>, unlinked: Array<object> }}
|
|
236
|
+
*/
|
|
237
|
+
buildTimelines(processedEmails) {
|
|
238
|
+
const timelines = {};
|
|
239
|
+
const unlinked = [];
|
|
240
|
+
|
|
241
|
+
for (const email of processedEmails) {
|
|
242
|
+
const key = email.timeline_key || email.timelineKey;
|
|
243
|
+
if (key) {
|
|
244
|
+
if (!timelines[key]) {
|
|
245
|
+
timelines[key] = {
|
|
246
|
+
timelineKey: key,
|
|
247
|
+
primaryCategory: email.primary_category,
|
|
248
|
+
events: [],
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
timelines[key].events.push(email);
|
|
252
|
+
} else {
|
|
253
|
+
unlinked.push(email);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return { timelines, unlinked };
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
export const RelayRover = {
|
|
262
|
+
/**
|
|
263
|
+
* Explicitly initialize the WASM engine with a binary buffer (optional).
|
|
264
|
+
*/
|
|
265
|
+
init,
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Fast category detection for any arbitrary email subject and body.
|
|
269
|
+
* @param {string} subject
|
|
270
|
+
* @param {string} body
|
|
271
|
+
* @returns {string} Category name (e.g. 'Payment', 'OTP', 'Jobs', 'Travel', etc.)
|
|
272
|
+
*/
|
|
273
|
+
classify(subject, body) {
|
|
274
|
+
const exp = getExports();
|
|
275
|
+
const sub = writeStringToMemory(exp, memory, subject);
|
|
276
|
+
const bod = writeStringToMemory(exp, memory, body);
|
|
277
|
+
const resPtr = exp.rover_classify(sub.ptr, sub.len, bod.ptr, bod.len);
|
|
278
|
+
const category = readStringFromMemory(exp, memory, resPtr);
|
|
279
|
+
exp.rover_dealloc(sub.ptr, sub.len);
|
|
280
|
+
exp.rover_dealloc(bod.ptr, bod.len);
|
|
281
|
+
return category;
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Generate concise contextual preview line (< 100 characters).
|
|
286
|
+
* @param {string} subject
|
|
287
|
+
* @param {string} body
|
|
288
|
+
* @returns {string} Preview text
|
|
289
|
+
*/
|
|
290
|
+
generatePreview(subject, body) {
|
|
291
|
+
const exp = getExports();
|
|
292
|
+
const sub = writeStringToMemory(exp, memory, subject);
|
|
293
|
+
const bod = writeStringToMemory(exp, memory, body);
|
|
294
|
+
const resPtr = exp.rover_preview(sub.ptr, sub.len, bod.ptr, bod.len);
|
|
295
|
+
const preview = readStringFromMemory(exp, memory, resPtr);
|
|
296
|
+
exp.rover_dealloc(sub.ptr, sub.len);
|
|
297
|
+
exp.rover_dealloc(bod.ptr, bod.len);
|
|
298
|
+
return preview;
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Process email with full category detection, typed extraction, and preview generation.
|
|
303
|
+
* @param {string} subject
|
|
304
|
+
* @param {string} body
|
|
305
|
+
* @returns {object} ProcessedEmail result
|
|
306
|
+
*/
|
|
307
|
+
processEmail(subject, body) {
|
|
308
|
+
const exp = getExports();
|
|
309
|
+
const sub = writeStringToMemory(exp, memory, subject);
|
|
310
|
+
const bod = writeStringToMemory(exp, memory, body);
|
|
311
|
+
const resPtr = exp.rover_process_email(sub.ptr, sub.len, bod.ptr, bod.len);
|
|
312
|
+
const jsonStr = readStringFromMemory(exp, memory, resPtr);
|
|
313
|
+
exp.rover_dealloc(sub.ptr, sub.len);
|
|
314
|
+
exp.rover_dealloc(bod.ptr, bod.len);
|
|
315
|
+
return JSON.parse(jsonStr);
|
|
316
|
+
},
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Process a single email from any email object (Relay Mail, HTML, or synthetic) or string.
|
|
320
|
+
* Automatically normalizes headers, cleans HTML if present, and attaches cleanBody.
|
|
321
|
+
*
|
|
322
|
+
* @param {object|string} input Email object or text
|
|
323
|
+
* @returns {object} ProcessedEmail result with cleanBody
|
|
324
|
+
*/
|
|
325
|
+
process(input) {
|
|
326
|
+
if (typeof input === 'string') {
|
|
327
|
+
const clean = cleanEmailBody(input);
|
|
328
|
+
const res = this.processEmail('', clean);
|
|
329
|
+
return { ...res, cleanBody: clean };
|
|
330
|
+
}
|
|
331
|
+
if (input && typeof input === 'object') {
|
|
332
|
+
const normalized = normalizeEmail(input);
|
|
333
|
+
const exp = getExports();
|
|
334
|
+
let res;
|
|
335
|
+
if (exp && typeof exp.rover_process_email_json === 'function') {
|
|
336
|
+
const jsonStr = JSON.stringify(normalized);
|
|
337
|
+
const inMem = writeStringToMemory(exp, memory, jsonStr);
|
|
338
|
+
const resPtr = exp.rover_process_email_json(inMem.ptr, inMem.len);
|
|
339
|
+
const outStr = readStringFromMemory(exp, memory, resPtr);
|
|
340
|
+
exp.rover_dealloc(inMem.ptr, inMem.len);
|
|
341
|
+
res = JSON.parse(outStr);
|
|
342
|
+
} else {
|
|
343
|
+
res = this.processEmail(normalized.subject, normalized.body);
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
...res,
|
|
347
|
+
id: normalized.id,
|
|
348
|
+
cleanBody: normalized.body,
|
|
349
|
+
sender: normalized.sender,
|
|
350
|
+
senderName: normalized.senderName,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
throw new TypeError('Expected email object or string');
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Process multiple emails in a single in-memory WebAssembly engine call.
|
|
358
|
+
* Accepts an array of email objects (including raw Relay Mail with htmlBody) or a serialized JSON string.
|
|
359
|
+
*
|
|
360
|
+
* @param {Array<object>|string} emails Array of email records
|
|
361
|
+
* @returns {Array<object>} Array of ProcessedEmail results with cleanBody
|
|
362
|
+
*/
|
|
363
|
+
processBatch(emails) {
|
|
364
|
+
const exp = getExports();
|
|
365
|
+
const emailsArray = typeof emails === 'string' ? JSON.parse(emails) : emails;
|
|
366
|
+
|
|
367
|
+
const normalizedArray = emailsArray.map(m => {
|
|
368
|
+
if (typeof m === 'object' && m !== null) {
|
|
369
|
+
return normalizeEmail(m);
|
|
370
|
+
}
|
|
371
|
+
return m;
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
const jsonString = JSON.stringify(normalizedArray);
|
|
375
|
+
const input = writeStringToMemory(exp, memory, jsonString);
|
|
376
|
+
const resPtr = exp.rover_process_batch_json(input.ptr, input.len);
|
|
377
|
+
const resultsJson = readStringFromMemory(exp, memory, resPtr);
|
|
378
|
+
exp.rover_dealloc(input.ptr, input.len);
|
|
379
|
+
|
|
380
|
+
const parsed = JSON.parse(resultsJson);
|
|
381
|
+
if (Array.isArray(parsed)) {
|
|
382
|
+
return parsed.map((item, idx) => ({
|
|
383
|
+
...item,
|
|
384
|
+
cleanBody: normalizedArray[idx]?.body || '',
|
|
385
|
+
}));
|
|
386
|
+
}
|
|
387
|
+
return parsed;
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Alias for processBatch to process an array of JSON email records.
|
|
392
|
+
* @param {Array<object>|string} emails
|
|
393
|
+
* @returns {Array<object>}
|
|
394
|
+
*/
|
|
395
|
+
processEmailsJson(emails) {
|
|
396
|
+
return this.processBatch(emails);
|
|
397
|
+
},
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Convert HTML email content to clean, readable plain text.
|
|
401
|
+
*/
|
|
402
|
+
htmlToText,
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Extract clean, readable normal message text from an email object or text.
|
|
406
|
+
*/
|
|
407
|
+
cleanEmailBody,
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Normalize an email of any format into a standardized structured object.
|
|
411
|
+
*/
|
|
412
|
+
normalizeEmail,
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Generate a concise summary snippet from an email body.
|
|
416
|
+
*/
|
|
417
|
+
extractSummary,
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Rover Intelligence Index timeline grouping engine.
|
|
421
|
+
*/
|
|
422
|
+
index: RoverIndex,
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
export default RelayRover;
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhq/rover",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "High-performance React Native and JavaScript bindings for Relay Rover Email Intelligence Engine",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.d.ts",
|
|
14
|
+
"eng/relay_rover.wasm",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react-native",
|
|
19
|
+
"relay-rover",
|
|
20
|
+
"email-intelligence",
|
|
21
|
+
"rust",
|
|
22
|
+
"regex",
|
|
23
|
+
"wasm"
|
|
24
|
+
],
|
|
25
|
+
"author": "Relay Team",
|
|
26
|
+
"license": "MIT"
|
|
27
|
+
}
|