@redonvn/redai-tool 0.0.0-stage → 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.
package/dist/index.mjs ADDED
@@ -0,0 +1,694 @@
1
+ // src/client.ts
2
+ import axios from "axios";
3
+
4
+ // src/errors.ts
5
+ var RedaiToolError = class extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = "RedaiToolError";
9
+ Object.setPrototypeOf(this, new.target.prototype);
10
+ }
11
+ };
12
+ var RedaiToolApiError = class extends RedaiToolError {
13
+ constructor(message, status, responseData) {
14
+ super(message);
15
+ this.name = "RedaiToolApiError";
16
+ this.status = status;
17
+ this.responseData = responseData;
18
+ }
19
+ };
20
+ var RedaiToolAuthError = class extends RedaiToolApiError {
21
+ constructor(message = "X-API-Key is missing or invalid") {
22
+ super(message, 401);
23
+ this.name = "RedaiToolAuthError";
24
+ }
25
+ };
26
+ var RedaiToolTimeoutError = class extends RedaiToolError {
27
+ constructor(message = "Request to redai-tool timed out") {
28
+ super(message);
29
+ this.name = "RedaiToolTimeoutError";
30
+ }
31
+ };
32
+
33
+ // src/modules/google-search/index.ts
34
+ var GoogleSearchModule = class {
35
+ constructor(http) {
36
+ this.http = http;
37
+ }
38
+ /**
39
+ * Tìm kiếm danh sách kết quả nhanh trên Google (không crawl sâu chi tiết)
40
+ */
41
+ async list(request, options) {
42
+ const response = await this.http.post(
43
+ "/api/google-search/list",
44
+ request,
45
+ options
46
+ );
47
+ return response.data;
48
+ }
49
+ /**
50
+ * Tìm kiếm Google và bóc tách thông tin liên hệ đa kênh (Hotline, Zalo, FB, Shopee, App, Bộ Công Thương...)
51
+ */
52
+ async scrape(request, options) {
53
+ const response = await this.http.post(
54
+ "/api/google-search/scrape",
55
+ request,
56
+ options
57
+ );
58
+ return response.data;
59
+ }
60
+ /**
61
+ * Bóc tách thông tin liên hệ và tóm tắt AI trực tiếp từ một danh sách các URL website
62
+ */
63
+ async scrapeDetails(request, options) {
64
+ const response = await this.http.post(
65
+ "/api/google-search/detail",
66
+ request,
67
+ options
68
+ );
69
+ return response.data;
70
+ }
71
+ /**
72
+ * Stream SSE kết quả tìm kiếm và bóc tách theo thời gian thực
73
+ */
74
+ async stream(request, handlers, options) {
75
+ const params = new URLSearchParams();
76
+ params.set("keyword", request.keyword);
77
+ if (request.max_results) params.set("max_results", String(request.max_results));
78
+ if (request.deep_crawl !== void 0) params.set("deep_crawl", String(request.deep_crawl));
79
+ if (request.summary_mode) params.set("summary_mode", request.summary_mode);
80
+ if (request.target_sites?.length) {
81
+ request.target_sites.forEach((site) => params.append("target_sites", site));
82
+ }
83
+ const response = await this.http.get(
84
+ `/api/google-search/stream?${params.toString()}`,
85
+ {
86
+ ...options,
87
+ responseType: "stream",
88
+ headers: {
89
+ Accept: "text/event-stream",
90
+ ...options?.headers || {}
91
+ }
92
+ }
93
+ );
94
+ const stream = response.data;
95
+ let buffer = "";
96
+ stream.on("data", (chunk) => {
97
+ buffer += chunk.toString();
98
+ const lines = buffer.split("\n");
99
+ buffer = lines.pop() || "";
100
+ for (const line of lines) {
101
+ const trimmed = line.trim();
102
+ if (trimmed.startsWith("data:")) {
103
+ const jsonStr = trimmed.replace(/^data:\s*/, "").trim();
104
+ if (jsonStr === "[DONE]") {
105
+ handlers.onEnd?.();
106
+ return;
107
+ }
108
+ try {
109
+ const parsed = JSON.parse(jsonStr);
110
+ handlers.onData?.(parsed);
111
+ } catch (e) {
112
+ }
113
+ }
114
+ }
115
+ });
116
+ stream.on("end", () => {
117
+ handlers.onEnd?.();
118
+ });
119
+ stream.on("error", (err) => {
120
+ handlers.onError?.(err);
121
+ });
122
+ }
123
+ };
124
+
125
+ // src/modules/google-maps/index.ts
126
+ var GoogleMapsModule = class {
127
+ constructor(http) {
128
+ this.http = http;
129
+ }
130
+ /**
131
+ * Tạo background job cào dữ liệu Google Maps
132
+ */
133
+ async createJob(payload, options) {
134
+ const response = await this.http.post(
135
+ "/api/map/jobs",
136
+ payload,
137
+ options
138
+ );
139
+ return response.data;
140
+ }
141
+ /**
142
+ * Kiểm tra trạng thái và tiến độ của job cào Google Maps
143
+ */
144
+ async getJobStatus(jobId, options) {
145
+ const response = await this.http.get(
146
+ `/api/map/jobs/${jobId}`,
147
+ options
148
+ );
149
+ return response.data;
150
+ }
151
+ /**
152
+ * Lấy danh sách kết quả địa điểm thu thập được từ job
153
+ */
154
+ async getJobResults(jobId, options) {
155
+ const response = await this.http.get(
156
+ `/api/map/jobs/${jobId}/results`,
157
+ options
158
+ );
159
+ return response.data;
160
+ }
161
+ /**
162
+ * Xuất kết quả cào Maps dạng file CSV (trả về Buffer)
163
+ */
164
+ async exportCsv(jobId, options) {
165
+ const response = await this.http.get(
166
+ `/api/map/jobs/${jobId}/export/csv`,
167
+ {
168
+ ...options,
169
+ responseType: "arraybuffer"
170
+ }
171
+ );
172
+ return Buffer.from(response.data);
173
+ }
174
+ /**
175
+ * Xuất kết quả cào Maps dạng file Excel XLSX (trả về Buffer)
176
+ */
177
+ async exportExcel(jobId, options) {
178
+ const response = await this.http.get(
179
+ `/api/map/jobs/${jobId}/export/excel`,
180
+ {
181
+ ...options,
182
+ responseType: "arraybuffer"
183
+ }
184
+ );
185
+ return Buffer.from(response.data);
186
+ }
187
+ /**
188
+ * Stream SSE tiến độ cào Google Maps theo thời gian thực
189
+ */
190
+ async streamJob(jobId, handlers, options) {
191
+ const response = await this.http.get(
192
+ `/api/map/jobs/${jobId}/stream`,
193
+ {
194
+ ...options,
195
+ responseType: "stream",
196
+ headers: {
197
+ Accept: "text/event-stream",
198
+ ...options?.headers || {}
199
+ }
200
+ }
201
+ );
202
+ const stream = response.data;
203
+ let buffer = "";
204
+ stream.on("data", (chunk) => {
205
+ buffer += chunk.toString();
206
+ const lines = buffer.split("\n");
207
+ buffer = lines.pop() || "";
208
+ for (const line of lines) {
209
+ const trimmed = line.trim();
210
+ if (trimmed.startsWith("data:")) {
211
+ const jsonStr = trimmed.replace(/^data:\s*/, "").trim();
212
+ if (jsonStr === "[DONE]") {
213
+ handlers.onEnd?.();
214
+ return;
215
+ }
216
+ try {
217
+ const parsed = JSON.parse(jsonStr);
218
+ handlers.onData?.(parsed);
219
+ } catch (e) {
220
+ }
221
+ }
222
+ }
223
+ });
224
+ stream.on("end", () => {
225
+ handlers.onEnd?.();
226
+ });
227
+ stream.on("error", (err) => {
228
+ handlers.onError?.(err);
229
+ });
230
+ }
231
+ /**
232
+ * Geocode địa chỉ văn bản thành tọa độ và các cấp hành chính Việt Nam
233
+ */
234
+ async geocodeAddress(address, options) {
235
+ const payload = typeof address === "string" ? { address } : address;
236
+ const response = await this.http.post(
237
+ "/api/map/geocode/address",
238
+ payload,
239
+ options
240
+ );
241
+ return response.data;
242
+ }
243
+ /**
244
+ * Reverse geocode tọa độ ra địa chỉ Việt Nam
245
+ */
246
+ async reverseGeocode(req, options) {
247
+ const response = await this.http.post(
248
+ "/api/map/geocode/coordinates",
249
+ req,
250
+ options
251
+ );
252
+ return response.data;
253
+ }
254
+ /**
255
+ * Chuẩn hóa và bóc tách địa chỉ đa năng (Số nhà, đường, phường xã, quận huyện, tỉnh thành)
256
+ */
257
+ async parseAddressUniversal(req, options) {
258
+ const response = await this.http.post(
259
+ "/api/map/address/universal-parse",
260
+ req,
261
+ options
262
+ );
263
+ return response.data;
264
+ }
265
+ };
266
+
267
+ // src/modules/social-tracer/index.ts
268
+ var SocialTracerModule = class {
269
+ constructor(http) {
270
+ this.http = http;
271
+ }
272
+ /**
273
+ * Lấy danh sách hơn 400+ nền tảng mạng xã hội được hỗ trợ truy vết
274
+ */
275
+ async getPlatforms(options) {
276
+ const response = await this.http.get(
277
+ "/api/social-tracer/platforms",
278
+ options
279
+ );
280
+ return response.data;
281
+ }
282
+ /**
283
+ * Truy vết đồng bộ danh tính mạng xã hội theo username trên hàng trăm trang
284
+ */
285
+ async traceSync(payload, options) {
286
+ const response = await this.http.post(
287
+ "/api/social-tracer/trace",
288
+ payload,
289
+ options
290
+ );
291
+ return response.data;
292
+ }
293
+ /**
294
+ * Stream SSE quá trình truy vết mạng xã hội thời gian thực
295
+ */
296
+ async streamTrace(payload, handlers, options) {
297
+ const params = new URLSearchParams();
298
+ params.set("username", payload.username);
299
+ if (payload.timeout_per_site) params.set("timeout_per_site", String(payload.timeout_per_site));
300
+ if (payload.concurrency) params.set("concurrency", String(payload.concurrency));
301
+ if (payload.platforms?.length) {
302
+ payload.platforms.forEach((p) => params.append("platforms", p));
303
+ }
304
+ const response = await this.http.get(
305
+ `/api/social-tracer/trace/stream?${params.toString()}`,
306
+ {
307
+ ...options,
308
+ responseType: "stream",
309
+ headers: {
310
+ Accept: "text/event-stream",
311
+ ...options?.headers || {}
312
+ }
313
+ }
314
+ );
315
+ const stream = response.data;
316
+ let buffer = "";
317
+ stream.on("data", (chunk) => {
318
+ buffer += chunk.toString();
319
+ const lines = buffer.split("\n");
320
+ buffer = lines.pop() || "";
321
+ for (const line of lines) {
322
+ const trimmed = line.trim();
323
+ if (trimmed.startsWith("data:")) {
324
+ const jsonStr = trimmed.replace(/^data:\s*/, "").trim();
325
+ if (jsonStr === "[DONE]") {
326
+ handlers.onEnd?.();
327
+ return;
328
+ }
329
+ try {
330
+ const parsed = JSON.parse(jsonStr);
331
+ handlers.onData?.(parsed);
332
+ } catch (e) {
333
+ }
334
+ }
335
+ }
336
+ });
337
+ stream.on("end", () => {
338
+ handlers.onEnd?.();
339
+ });
340
+ stream.on("error", (err) => {
341
+ handlers.onError?.(err);
342
+ });
343
+ }
344
+ /**
345
+ * Tạo background job truy vết danh tính mạng xã hội
346
+ */
347
+ async createJob(payload, options) {
348
+ const response = await this.http.post(
349
+ "/api/social-tracer/jobs",
350
+ payload,
351
+ options
352
+ );
353
+ return response.data;
354
+ }
355
+ /**
356
+ * Kiểm tra trạng thái job truy vết
357
+ */
358
+ async getJobStatus(jobId, options) {
359
+ const response = await this.http.get(
360
+ `/api/social-tracer/jobs/${jobId}`,
361
+ options
362
+ );
363
+ return response.data;
364
+ }
365
+ /**
366
+ * Lấy kết quả truy vết từ job
367
+ */
368
+ async getJobResults(jobId, options) {
369
+ const response = await this.http.get(
370
+ `/api/social-tracer/jobs/${jobId}/results`,
371
+ options
372
+ );
373
+ return response.data;
374
+ }
375
+ /**
376
+ * Xuất file CSV kết quả truy vết mạng xã hội (trả về Buffer)
377
+ */
378
+ async exportCsv(jobId, options) {
379
+ const response = await this.http.get(
380
+ `/api/social-tracer/jobs/${jobId}/export/csv`,
381
+ {
382
+ ...options,
383
+ responseType: "arraybuffer"
384
+ }
385
+ );
386
+ return Buffer.from(response.data);
387
+ }
388
+ };
389
+
390
+ // src/modules/ocr/index.ts
391
+ import FormData from "form-data";
392
+ var OcrModule = class {
393
+ constructor(http) {
394
+ this.http = http;
395
+ }
396
+ /**
397
+ * Bóc tách thông tin thẻ Căn cước công dân (CCCD) gắn chip/mã QR qua Base64 hoặc URL
398
+ */
399
+ async extractCccd(payload, options) {
400
+ const response = await this.http.post(
401
+ "/api/cccd/extract",
402
+ payload,
403
+ options
404
+ );
405
+ return response.data;
406
+ }
407
+ /**
408
+ * Upload file ảnh trực tiếp để bóc tách thông tin CCCD
409
+ */
410
+ async uploadAndExtractCccd(fileBuffer, filename = "cccd.jpg", options) {
411
+ const formData = new FormData();
412
+ formData.append("file", fileBuffer, filename);
413
+ const response = await this.http.post(
414
+ "/api/cccd/upload",
415
+ formData,
416
+ {
417
+ ...options,
418
+ headers: {
419
+ ...formData.getHeaders(),
420
+ ...options?.headers || {}
421
+ }
422
+ }
423
+ );
424
+ return response.data;
425
+ }
426
+ /**
427
+ * Thẩm định tính hợp pháp và các đặc trưng bảo an thẻ CCCD
428
+ */
429
+ async verifyCccd(payload, options) {
430
+ const response = await this.http.post(
431
+ "/api/cccd/verify",
432
+ payload,
433
+ options
434
+ );
435
+ return response.data;
436
+ }
437
+ /**
438
+ * Bóc tách Giấy chứng nhận đăng ký doanh nghiệp / hộ kinh doanh qua JSON (Base64 hoặc URL)
439
+ */
440
+ async extractBusinessLicense(payload, options) {
441
+ const response = await this.http.post(
442
+ "/api/business-license/extract",
443
+ payload,
444
+ options
445
+ );
446
+ return response.data;
447
+ }
448
+ /**
449
+ * Upload file ảnh/PDF trực tiếp để bóc tách Giấy phép ĐKKD
450
+ */
451
+ async uploadAndExtractBusinessLicense(fileBuffer, filename = "giay_phep_kinh_doanh.pdf", options) {
452
+ const formData = new FormData();
453
+ formData.append("file", fileBuffer, filename);
454
+ const response = await this.http.post(
455
+ "/api/business-license/upload",
456
+ formData,
457
+ {
458
+ ...options,
459
+ headers: {
460
+ ...formData.getHeaders(),
461
+ ...options?.headers || {}
462
+ }
463
+ }
464
+ );
465
+ return response.data;
466
+ }
467
+ /**
468
+ * Thẩm định 6 lớp bảo an Giấy phép kinh doanh (Chống cắt ghép, ELA, con dấu đỏ, chữ ký)
469
+ */
470
+ async verifyBusinessLicense(payload, options) {
471
+ const response = await this.http.post(
472
+ "/api/business-license/verify",
473
+ payload,
474
+ options
475
+ );
476
+ return response.data;
477
+ }
478
+ };
479
+
480
+ // src/modules/ip-geolocation/index.ts
481
+ var IpGeolocationModule = class {
482
+ constructor(http) {
483
+ this.http = http;
484
+ }
485
+ /**
486
+ * Tra cứu vị trí địa lý, tọa độ GPS (lat, lon) và nhà mạng ISP từ 1 hoặc nhiều IP
487
+ */
488
+ async lookup(request, options) {
489
+ let payload;
490
+ if (typeof request === "string") {
491
+ payload = { ip: request };
492
+ } else if (Array.isArray(request)) {
493
+ payload = { ips: request };
494
+ } else {
495
+ payload = request;
496
+ }
497
+ const response = await this.http.post(
498
+ "/api/ip-location",
499
+ payload,
500
+ options
501
+ );
502
+ return response.data;
503
+ }
504
+ /**
505
+ * Tự động lấy IP Public hiện tại của client / server và vị trí địa lý
506
+ */
507
+ async getMyPublicIp(options) {
508
+ const response = await this.http.get(
509
+ "/api/ip-location/my-ip",
510
+ options
511
+ );
512
+ return response.data;
513
+ }
514
+ };
515
+
516
+ // src/modules/image-compare/index.ts
517
+ var ImageCompareModule = class {
518
+ constructor(http) {
519
+ this.http = http;
520
+ }
521
+ /**
522
+ * So sánh nội dung & độ tương đồng giữa 2 bức ảnh (qua URL hoặc Base64)
523
+ */
524
+ async compare(request, options) {
525
+ const response = await this.http.post(
526
+ "/api/image-compare",
527
+ request,
528
+ options
529
+ );
530
+ return response.data;
531
+ }
532
+ /**
533
+ * Chụp màn hình 2 trang web và so sánh độ lệch UI (Visual Regression Testing)
534
+ */
535
+ async compareWebpages(request, options) {
536
+ const response = await this.http.post(
537
+ "/api/image-compare/compare-webpages",
538
+ request,
539
+ options
540
+ );
541
+ return response.data;
542
+ }
543
+ };
544
+
545
+ // src/modules/web-qa/index.ts
546
+ var WebQaModule = class {
547
+ constructor(http) {
548
+ this.http = http;
549
+ }
550
+ /**
551
+ * Hỏi đáp AI dựa trên nội dung được cào trực tiếp từ URL website
552
+ */
553
+ async ask(request, options) {
554
+ const response = await this.http.post(
555
+ "/api/web-qa/ask",
556
+ request,
557
+ options
558
+ );
559
+ return response.data;
560
+ }
561
+ /**
562
+ * Chạy audit toàn diện 5 lớp chuẩn QA/Tester cho website (SEO, Accessibility, Console errors, Broken links)
563
+ */
564
+ async audit(request, options) {
565
+ const response = await this.http.post(
566
+ "/api/web-qa/audit",
567
+ request,
568
+ options
569
+ );
570
+ return response.data;
571
+ }
572
+ };
573
+
574
+ // src/modules/screenshot/index.ts
575
+ var ScreenshotModule = class {
576
+ constructor(http) {
577
+ this.http = http;
578
+ }
579
+ /**
580
+ * Chụp ảnh màn hình website (hỗ trợ full-page, kích thước tùy chỉnh)
581
+ */
582
+ async capture(request, options) {
583
+ const response = await this.http.post(
584
+ "/api/screenshot/capture",
585
+ request,
586
+ options
587
+ );
588
+ return response.data;
589
+ }
590
+ /**
591
+ * Chụp ảnh màn hình và trả về trực tiếp binary Buffer ảnh
592
+ */
593
+ async captureAsBuffer(request, options) {
594
+ const response = await this.http.post(
595
+ "/api/screenshot/render",
596
+ request,
597
+ {
598
+ ...options,
599
+ responseType: "arraybuffer"
600
+ }
601
+ );
602
+ return Buffer.from(response.data);
603
+ }
604
+ };
605
+
606
+ // src/client.ts
607
+ var RedaiToolClient = class {
608
+ constructor(config = {}) {
609
+ const defaultBaseUrl = typeof process !== "undefined" && process.env?.REDAI_TOOL_BASE_URL ? process.env.REDAI_TOOL_BASE_URL : "http://localhost:8000";
610
+ const defaultApiKey = typeof process !== "undefined" && process.env?.REDAI_TOOL_API_KEY ? process.env.REDAI_TOOL_API_KEY : "";
611
+ this.config = {
612
+ baseUrl: config.baseUrl || defaultBaseUrl,
613
+ apiKey: config.apiKey || defaultApiKey,
614
+ timeout: config.timeout ?? 6e4,
615
+ headers: config.headers ?? {}
616
+ };
617
+ const headers = {
618
+ Accept: "application/json",
619
+ "Content-Type": "application/json",
620
+ ...this.config.headers
621
+ };
622
+ if (this.config.apiKey) {
623
+ headers["X-API-Key"] = this.config.apiKey;
624
+ }
625
+ this.http = axios.create({
626
+ baseURL: this.config.baseUrl,
627
+ timeout: this.config.timeout,
628
+ headers
629
+ });
630
+ this.http.interceptors.response.use(
631
+ (response) => response,
632
+ (error) => {
633
+ if (error.code === "ECONNABORTED" || error.message.includes("timeout")) {
634
+ return Promise.reject(
635
+ new RedaiToolTimeoutError(
636
+ `Y\xEAu c\u1EA7u t\u1EDBi redai-tool (${error.config?.url}) \u0111\xE3 h\u1EBFt h\u1EA1n sau ${this.config.timeout}ms`
637
+ )
638
+ );
639
+ }
640
+ if (error.response?.status === 401 || error.response?.status === 403) {
641
+ return Promise.reject(
642
+ new RedaiToolAuthError(
643
+ `L\u1ED7i x\xE1c th\u1EF1c: X-API-Key kh\xF4ng h\u1EE3p l\u1EC7 ho\u1EB7c thi\u1EBFu khi g\u1ECDi ${error.config?.url}`
644
+ )
645
+ );
646
+ }
647
+ const data = error.response?.data;
648
+ const msg = data?.detail || data?.message || data?.error || error.message || "L\u1ED7i kh\xF4ng x\xE1c \u0111\u1ECBnh t\u1EEB redai-tool";
649
+ return Promise.reject(
650
+ new RedaiToolApiError(msg, error.response?.status, data)
651
+ );
652
+ }
653
+ );
654
+ this.googleSearch = new GoogleSearchModule(this.http);
655
+ this.googleMaps = new GoogleMapsModule(this.http);
656
+ this.socialTracer = new SocialTracerModule(this.http);
657
+ this.ocr = new OcrModule(this.http);
658
+ this.ip = new IpGeolocationModule(this.http);
659
+ this.imageCompare = new ImageCompareModule(this.http);
660
+ this.webQa = new WebQaModule(this.http);
661
+ this.screenshot = new ScreenshotModule(this.http);
662
+ }
663
+ /**
664
+ * Kiểm tra tình trạng kết nối tới service redai-tool
665
+ */
666
+ async healthCheck() {
667
+ try {
668
+ const resp = await this.http.get("/health");
669
+ return resp.data;
670
+ } catch {
671
+ return { status: "down" };
672
+ }
673
+ }
674
+ };
675
+
676
+ // src/index.ts
677
+ var index_default = RedaiToolClient;
678
+ export {
679
+ GoogleMapsModule,
680
+ GoogleSearchModule,
681
+ ImageCompareModule,
682
+ IpGeolocationModule,
683
+ OcrModule,
684
+ RedaiToolApiError,
685
+ RedaiToolAuthError,
686
+ RedaiToolClient,
687
+ RedaiToolError,
688
+ RedaiToolTimeoutError,
689
+ ScreenshotModule,
690
+ SocialTracerModule,
691
+ WebQaModule,
692
+ index_default as default
693
+ };
694
+ //# sourceMappingURL=index.mjs.map