@trainly/react 1.4.3 → 1.5.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/TrainlyProvider.js +141 -27
- package/dist/api/TrainlyClient.d.ts +3 -1
- package/dist/api/TrainlyClient.js +161 -0
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
package/dist/TrainlyProvider.js
CHANGED
|
@@ -373,7 +373,7 @@ export function TrainlyProvider(_a) {
|
|
|
373
373
|
}
|
|
374
374
|
});
|
|
375
375
|
}); };
|
|
376
|
-
var
|
|
376
|
+
var uploadText = function (text, contentName, scopeValues) { return __awaiter(_this, void 0, void 0, function () {
|
|
377
377
|
var result, err_5, errorMessage, newToken, result, refreshError_3, error_5;
|
|
378
378
|
return __generator(this, function (_a) {
|
|
379
379
|
switch (_a.label) {
|
|
@@ -381,7 +381,7 @@ export function TrainlyProvider(_a) {
|
|
|
381
381
|
_a.trys.push([0, 2, 10, 11]);
|
|
382
382
|
setIsLoading(true);
|
|
383
383
|
setError(null);
|
|
384
|
-
return [4 /*yield*/, client.
|
|
384
|
+
return [4 /*yield*/, client.uploadText(text, contentName, scopeValues)];
|
|
385
385
|
case 1:
|
|
386
386
|
result = _a.sent();
|
|
387
387
|
return [2 /*return*/, result];
|
|
@@ -396,7 +396,7 @@ export function TrainlyProvider(_a) {
|
|
|
396
396
|
_a.label = 3;
|
|
397
397
|
case 3:
|
|
398
398
|
_a.trys.push([3, 8, , 9]);
|
|
399
|
-
console.log("🔄 Token expired during
|
|
399
|
+
console.log("🔄 Token expired during text upload, refreshing...");
|
|
400
400
|
return [4 /*yield*/, getToken()];
|
|
401
401
|
case 4:
|
|
402
402
|
newToken = _a.sent();
|
|
@@ -404,20 +404,20 @@ export function TrainlyProvider(_a) {
|
|
|
404
404
|
return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
|
|
405
405
|
case 5:
|
|
406
406
|
_a.sent();
|
|
407
|
-
return [4 /*yield*/, client.
|
|
407
|
+
return [4 /*yield*/, client.uploadText(text, contentName, scopeValues)];
|
|
408
408
|
case 6:
|
|
409
409
|
result = _a.sent();
|
|
410
|
-
console.log("✅
|
|
410
|
+
console.log("✅ Text upload succeeded after token refresh");
|
|
411
411
|
return [2 /*return*/, result];
|
|
412
412
|
case 7: return [3 /*break*/, 9];
|
|
413
413
|
case 8:
|
|
414
414
|
refreshError_3 = _a.sent();
|
|
415
|
-
console.error("❌ Token refresh failed
|
|
415
|
+
console.error("❌ Token refresh failed:", refreshError_3);
|
|
416
416
|
return [3 /*break*/, 9];
|
|
417
417
|
case 9:
|
|
418
418
|
error_5 = {
|
|
419
|
-
code: "
|
|
420
|
-
message: "Failed to upload
|
|
419
|
+
code: "TEXT_UPLOAD_FAILED",
|
|
420
|
+
message: "Failed to upload text content",
|
|
421
421
|
details: err_5,
|
|
422
422
|
};
|
|
423
423
|
setError(error_5);
|
|
@@ -429,7 +429,7 @@ export function TrainlyProvider(_a) {
|
|
|
429
429
|
}
|
|
430
430
|
});
|
|
431
431
|
}); };
|
|
432
|
-
var
|
|
432
|
+
var bulkUploadFiles = function (files, scopeValues) { return __awaiter(_this, void 0, void 0, function () {
|
|
433
433
|
var result, err_6, errorMessage, newToken, result, refreshError_4, error_6;
|
|
434
434
|
return __generator(this, function (_a) {
|
|
435
435
|
switch (_a.label) {
|
|
@@ -437,7 +437,7 @@ export function TrainlyProvider(_a) {
|
|
|
437
437
|
_a.trys.push([0, 2, 10, 11]);
|
|
438
438
|
setIsLoading(true);
|
|
439
439
|
setError(null);
|
|
440
|
-
return [4 /*yield*/, client.
|
|
440
|
+
return [4 /*yield*/, client.bulkUploadFiles(files, scopeValues)];
|
|
441
441
|
case 1:
|
|
442
442
|
result = _a.sent();
|
|
443
443
|
return [2 /*return*/, result];
|
|
@@ -452,7 +452,7 @@ export function TrainlyProvider(_a) {
|
|
|
452
452
|
_a.label = 3;
|
|
453
453
|
case 3:
|
|
454
454
|
_a.trys.push([3, 8, , 9]);
|
|
455
|
-
console.log("🔄 Token expired during
|
|
455
|
+
console.log("🔄 Token expired during bulk upload, refreshing...");
|
|
456
456
|
return [4 /*yield*/, getToken()];
|
|
457
457
|
case 4:
|
|
458
458
|
newToken = _a.sent();
|
|
@@ -460,20 +460,20 @@ export function TrainlyProvider(_a) {
|
|
|
460
460
|
return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
|
|
461
461
|
case 5:
|
|
462
462
|
_a.sent();
|
|
463
|
-
return [4 /*yield*/, client.
|
|
463
|
+
return [4 /*yield*/, client.bulkUploadFiles(files, scopeValues)];
|
|
464
464
|
case 6:
|
|
465
465
|
result = _a.sent();
|
|
466
|
-
console.log("✅
|
|
466
|
+
console.log("✅ Bulk upload succeeded after token refresh");
|
|
467
467
|
return [2 /*return*/, result];
|
|
468
468
|
case 7: return [3 /*break*/, 9];
|
|
469
469
|
case 8:
|
|
470
470
|
refreshError_4 = _a.sent();
|
|
471
|
-
console.error("❌ Token refresh failed:", refreshError_4);
|
|
471
|
+
console.error("❌ Token refresh failed during bulk upload:", refreshError_4);
|
|
472
472
|
return [3 /*break*/, 9];
|
|
473
473
|
case 9:
|
|
474
474
|
error_6 = {
|
|
475
|
-
code: "
|
|
476
|
-
message: "Failed to
|
|
475
|
+
code: "BULK_UPLOAD_FAILED",
|
|
476
|
+
message: "Failed to upload files",
|
|
477
477
|
details: err_6,
|
|
478
478
|
};
|
|
479
479
|
setError(error_6);
|
|
@@ -485,7 +485,7 @@ export function TrainlyProvider(_a) {
|
|
|
485
485
|
}
|
|
486
486
|
});
|
|
487
487
|
}); };
|
|
488
|
-
var
|
|
488
|
+
var bulkUploadText = function (textContents, scopeValues) { return __awaiter(_this, void 0, void 0, function () {
|
|
489
489
|
var result, err_7, errorMessage, newToken, result, refreshError_5, error_7;
|
|
490
490
|
return __generator(this, function (_a) {
|
|
491
491
|
switch (_a.label) {
|
|
@@ -493,7 +493,7 @@ export function TrainlyProvider(_a) {
|
|
|
493
493
|
_a.trys.push([0, 2, 10, 11]);
|
|
494
494
|
setIsLoading(true);
|
|
495
495
|
setError(null);
|
|
496
|
-
return [4 /*yield*/, client.
|
|
496
|
+
return [4 /*yield*/, client.bulkUploadText(textContents, scopeValues)];
|
|
497
497
|
case 1:
|
|
498
498
|
result = _a.sent();
|
|
499
499
|
return [2 /*return*/, result];
|
|
@@ -508,7 +508,7 @@ export function TrainlyProvider(_a) {
|
|
|
508
508
|
_a.label = 3;
|
|
509
509
|
case 3:
|
|
510
510
|
_a.trys.push([3, 8, , 9]);
|
|
511
|
-
console.log("🔄 Token expired during
|
|
511
|
+
console.log("🔄 Token expired during bulk text upload, refreshing...");
|
|
512
512
|
return [4 /*yield*/, getToken()];
|
|
513
513
|
case 4:
|
|
514
514
|
newToken = _a.sent();
|
|
@@ -516,20 +516,20 @@ export function TrainlyProvider(_a) {
|
|
|
516
516
|
return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
|
|
517
517
|
case 5:
|
|
518
518
|
_a.sent();
|
|
519
|
-
return [4 /*yield*/, client.
|
|
519
|
+
return [4 /*yield*/, client.bulkUploadText(textContents, scopeValues)];
|
|
520
520
|
case 6:
|
|
521
521
|
result = _a.sent();
|
|
522
|
-
console.log("✅
|
|
522
|
+
console.log("✅ Bulk text upload succeeded after token refresh");
|
|
523
523
|
return [2 /*return*/, result];
|
|
524
524
|
case 7: return [3 /*break*/, 9];
|
|
525
525
|
case 8:
|
|
526
526
|
refreshError_5 = _a.sent();
|
|
527
|
-
console.error("❌ Token refresh failed:", refreshError_5);
|
|
527
|
+
console.error("❌ Token refresh failed during bulk text upload:", refreshError_5);
|
|
528
528
|
return [3 /*break*/, 9];
|
|
529
529
|
case 9:
|
|
530
530
|
error_7 = {
|
|
531
|
-
code: "
|
|
532
|
-
message: "Failed to
|
|
531
|
+
code: "BULK_TEXT_UPLOAD_FAILED",
|
|
532
|
+
message: "Failed to upload text content",
|
|
533
533
|
details: err_7,
|
|
534
534
|
};
|
|
535
535
|
setError(error_7);
|
|
@@ -541,8 +541,120 @@ export function TrainlyProvider(_a) {
|
|
|
541
541
|
}
|
|
542
542
|
});
|
|
543
543
|
}); };
|
|
544
|
+
var listFiles = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
545
|
+
var result, err_8, errorMessage, newToken, result, refreshError_6, error_8;
|
|
546
|
+
return __generator(this, function (_a) {
|
|
547
|
+
switch (_a.label) {
|
|
548
|
+
case 0:
|
|
549
|
+
_a.trys.push([0, 2, 10, 11]);
|
|
550
|
+
setIsLoading(true);
|
|
551
|
+
setError(null);
|
|
552
|
+
return [4 /*yield*/, client.listFiles()];
|
|
553
|
+
case 1:
|
|
554
|
+
result = _a.sent();
|
|
555
|
+
return [2 /*return*/, result];
|
|
556
|
+
case 2:
|
|
557
|
+
err_8 = _a.sent();
|
|
558
|
+
errorMessage = err_8 instanceof Error ? err_8.message : String(err_8);
|
|
559
|
+
if (!(getToken &&
|
|
560
|
+
appId &&
|
|
561
|
+
(errorMessage.includes("401") ||
|
|
562
|
+
errorMessage.includes("authentication") ||
|
|
563
|
+
errorMessage.includes("Unauthorized")))) return [3 /*break*/, 9];
|
|
564
|
+
_a.label = 3;
|
|
565
|
+
case 3:
|
|
566
|
+
_a.trys.push([3, 8, , 9]);
|
|
567
|
+
console.log("🔄 Token expired during file listing, refreshing...");
|
|
568
|
+
return [4 /*yield*/, getToken()];
|
|
569
|
+
case 4:
|
|
570
|
+
newToken = _a.sent();
|
|
571
|
+
if (!newToken) return [3 /*break*/, 7];
|
|
572
|
+
return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
|
|
573
|
+
case 5:
|
|
574
|
+
_a.sent();
|
|
575
|
+
return [4 /*yield*/, client.listFiles()];
|
|
576
|
+
case 6:
|
|
577
|
+
result = _a.sent();
|
|
578
|
+
console.log("✅ File listing succeeded after token refresh");
|
|
579
|
+
return [2 /*return*/, result];
|
|
580
|
+
case 7: return [3 /*break*/, 9];
|
|
581
|
+
case 8:
|
|
582
|
+
refreshError_6 = _a.sent();
|
|
583
|
+
console.error("❌ Token refresh failed:", refreshError_6);
|
|
584
|
+
return [3 /*break*/, 9];
|
|
585
|
+
case 9:
|
|
586
|
+
error_8 = {
|
|
587
|
+
code: "LIST_FILES_FAILED",
|
|
588
|
+
message: "Failed to list files",
|
|
589
|
+
details: err_8,
|
|
590
|
+
};
|
|
591
|
+
setError(error_8);
|
|
592
|
+
throw error_8;
|
|
593
|
+
case 10:
|
|
594
|
+
setIsLoading(false);
|
|
595
|
+
return [7 /*endfinally*/];
|
|
596
|
+
case 11: return [2 /*return*/];
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
}); };
|
|
600
|
+
var deleteFile = function (fileId) { return __awaiter(_this, void 0, void 0, function () {
|
|
601
|
+
var result, err_9, errorMessage, newToken, result, refreshError_7, error_9;
|
|
602
|
+
return __generator(this, function (_a) {
|
|
603
|
+
switch (_a.label) {
|
|
604
|
+
case 0:
|
|
605
|
+
_a.trys.push([0, 2, 10, 11]);
|
|
606
|
+
setIsLoading(true);
|
|
607
|
+
setError(null);
|
|
608
|
+
return [4 /*yield*/, client.deleteFile(fileId)];
|
|
609
|
+
case 1:
|
|
610
|
+
result = _a.sent();
|
|
611
|
+
return [2 /*return*/, result];
|
|
612
|
+
case 2:
|
|
613
|
+
err_9 = _a.sent();
|
|
614
|
+
errorMessage = err_9 instanceof Error ? err_9.message : String(err_9);
|
|
615
|
+
if (!(getToken &&
|
|
616
|
+
appId &&
|
|
617
|
+
(errorMessage.includes("401") ||
|
|
618
|
+
errorMessage.includes("authentication") ||
|
|
619
|
+
errorMessage.includes("Unauthorized")))) return [3 /*break*/, 9];
|
|
620
|
+
_a.label = 3;
|
|
621
|
+
case 3:
|
|
622
|
+
_a.trys.push([3, 8, , 9]);
|
|
623
|
+
console.log("🔄 Token expired during file deletion, refreshing...");
|
|
624
|
+
return [4 /*yield*/, getToken()];
|
|
625
|
+
case 4:
|
|
626
|
+
newToken = _a.sent();
|
|
627
|
+
if (!newToken) return [3 /*break*/, 7];
|
|
628
|
+
return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
|
|
629
|
+
case 5:
|
|
630
|
+
_a.sent();
|
|
631
|
+
return [4 /*yield*/, client.deleteFile(fileId)];
|
|
632
|
+
case 6:
|
|
633
|
+
result = _a.sent();
|
|
634
|
+
console.log("✅ File deletion succeeded after token refresh");
|
|
635
|
+
return [2 /*return*/, result];
|
|
636
|
+
case 7: return [3 /*break*/, 9];
|
|
637
|
+
case 8:
|
|
638
|
+
refreshError_7 = _a.sent();
|
|
639
|
+
console.error("❌ Token refresh failed:", refreshError_7);
|
|
640
|
+
return [3 /*break*/, 9];
|
|
641
|
+
case 9:
|
|
642
|
+
error_9 = {
|
|
643
|
+
code: "DELETE_FILE_FAILED",
|
|
644
|
+
message: "Failed to delete file",
|
|
645
|
+
details: err_9,
|
|
646
|
+
};
|
|
647
|
+
setError(error_9);
|
|
648
|
+
throw error_9;
|
|
649
|
+
case 10:
|
|
650
|
+
setIsLoading(false);
|
|
651
|
+
return [7 /*endfinally*/];
|
|
652
|
+
case 11: return [2 /*return*/];
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
}); };
|
|
544
656
|
var sendMessage = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
545
|
-
var userMessage, response, assistantMessage_1,
|
|
657
|
+
var userMessage, response, assistantMessage_1, err_10;
|
|
546
658
|
return __generator(this, function (_a) {
|
|
547
659
|
switch (_a.label) {
|
|
548
660
|
case 0:
|
|
@@ -569,9 +681,9 @@ export function TrainlyProvider(_a) {
|
|
|
569
681
|
setMessages(function (prev) { return __spreadArray(__spreadArray([], prev, true), [assistantMessage_1], false); });
|
|
570
682
|
return [3 /*break*/, 4];
|
|
571
683
|
case 3:
|
|
572
|
-
|
|
684
|
+
err_10 = _a.sent();
|
|
573
685
|
// Error is already set by askWithCitations
|
|
574
|
-
console.error("Failed to send message:",
|
|
686
|
+
console.error("Failed to send message:", err_10);
|
|
575
687
|
return [3 /*break*/, 4];
|
|
576
688
|
case 4: return [2 /*return*/];
|
|
577
689
|
}
|
|
@@ -584,7 +696,9 @@ export function TrainlyProvider(_a) {
|
|
|
584
696
|
ask: ask,
|
|
585
697
|
askWithCitations: askWithCitations, // Deprecated - kept for backward compatibility
|
|
586
698
|
upload: upload,
|
|
699
|
+
uploadText: uploadText, // NEW: Text content upload
|
|
587
700
|
bulkUploadFiles: bulkUploadFiles, // NEW: Bulk file upload method
|
|
701
|
+
bulkUploadText: bulkUploadText, // NEW: Bulk text content upload
|
|
588
702
|
listFiles: listFiles, // NEW: File management methods
|
|
589
703
|
deleteFile: deleteFile,
|
|
590
704
|
connectWithOAuthToken: connectWithOAuthToken, // NEW: V1 OAuth connection method
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TrainlyConfig, Citation, UploadResult, FileListResult, FileDeleteResult, BulkUploadResult } from "../types";
|
|
1
|
+
import { TrainlyConfig, Citation, UploadResult, FileListResult, FileDeleteResult, BulkUploadResult, TextContent } from "../types";
|
|
2
2
|
interface QueryResponse {
|
|
3
3
|
answer: string;
|
|
4
4
|
citations?: Citation[];
|
|
@@ -20,7 +20,9 @@ export declare class TrainlyClient {
|
|
|
20
20
|
scope_filters?: Record<string, string | number | boolean>;
|
|
21
21
|
}): Promise<QueryResponse>;
|
|
22
22
|
upload(file: File, scopeValues?: Record<string, string | number | boolean>): Promise<UploadResult>;
|
|
23
|
+
uploadText(text: string, contentName: string, scopeValues?: Record<string, string | number | boolean>): Promise<UploadResult>;
|
|
23
24
|
bulkUploadFiles(files: File[], scopeValues?: Record<string, string | number | boolean>): Promise<BulkUploadResult>;
|
|
25
|
+
bulkUploadText(textContents: TextContent[], scopeValues?: Record<string, string | number | boolean>): Promise<BulkUploadResult>;
|
|
24
26
|
listFiles(): Promise<FileListResult>;
|
|
25
27
|
deleteFile(fileId: string): Promise<FileDeleteResult>;
|
|
26
28
|
private extractChatId;
|
|
@@ -338,6 +338,55 @@ var TrainlyClient = /** @class */ (function () {
|
|
|
338
338
|
});
|
|
339
339
|
});
|
|
340
340
|
};
|
|
341
|
+
TrainlyClient.prototype.uploadText = function (text, contentName, scopeValues) {
|
|
342
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
343
|
+
var formData, response, error, data;
|
|
344
|
+
return __generator(this, function (_a) {
|
|
345
|
+
switch (_a.label) {
|
|
346
|
+
case 0:
|
|
347
|
+
if (!this.scopedToken) {
|
|
348
|
+
throw new Error("Not connected. Call connect() or connectWithOAuthToken() first.");
|
|
349
|
+
}
|
|
350
|
+
if (!(this.isV1Mode && this.config.appId)) return [3 /*break*/, 5];
|
|
351
|
+
formData = new FormData();
|
|
352
|
+
formData.append("text_content", text);
|
|
353
|
+
formData.append("content_name", contentName);
|
|
354
|
+
// Add scope values if provided
|
|
355
|
+
if (scopeValues && Object.keys(scopeValues).length > 0) {
|
|
356
|
+
formData.append("scope_values", JSON.stringify(scopeValues));
|
|
357
|
+
}
|
|
358
|
+
return [4 /*yield*/, fetch("".concat(this.config.baseUrl, "/v1/me/chats/files/upload"), {
|
|
359
|
+
method: "POST",
|
|
360
|
+
headers: {
|
|
361
|
+
Authorization: "Bearer ".concat(this.scopedToken),
|
|
362
|
+
"X-App-ID": this.config.appId,
|
|
363
|
+
},
|
|
364
|
+
body: formData,
|
|
365
|
+
})];
|
|
366
|
+
case 1:
|
|
367
|
+
response = _a.sent();
|
|
368
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
|
369
|
+
return [4 /*yield*/, response.json()];
|
|
370
|
+
case 2:
|
|
371
|
+
error = _a.sent();
|
|
372
|
+
throw new Error("V1 text upload failed: ".concat(error.detail || response.statusText));
|
|
373
|
+
case 3: return [4 /*yield*/, response.json()];
|
|
374
|
+
case 4:
|
|
375
|
+
data = _a.sent();
|
|
376
|
+
return [2 /*return*/, {
|
|
377
|
+
success: data.success,
|
|
378
|
+
filename: data.filename,
|
|
379
|
+
size: data.size_bytes,
|
|
380
|
+
message: data.message ||
|
|
381
|
+
"Text content uploaded to your permanent private subchat",
|
|
382
|
+
}];
|
|
383
|
+
case 5:
|
|
384
|
+
// For non-V1 modes, text upload is not yet supported
|
|
385
|
+
throw new Error("Text upload is currently only available in V1 Trusted Issuer mode");
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
};
|
|
341
390
|
TrainlyClient.prototype.bulkUploadFiles = function (files, scopeValues) {
|
|
342
391
|
return __awaiter(this, void 0, void 0, function () {
|
|
343
392
|
var formData_1, response, error, data, results, successful_uploads, total_size_bytes, _i, files_1, file, uploadResult, error_1;
|
|
@@ -450,6 +499,118 @@ var TrainlyClient = /** @class */ (function () {
|
|
|
450
499
|
});
|
|
451
500
|
});
|
|
452
501
|
};
|
|
502
|
+
TrainlyClient.prototype.bulkUploadText = function (textContents, scopeValues) {
|
|
503
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
504
|
+
var formData, texts, names, response, error, data, results, successful_uploads, total_size_bytes, _i, textContents_1, textContent, uploadResult, error_2;
|
|
505
|
+
return __generator(this, function (_a) {
|
|
506
|
+
switch (_a.label) {
|
|
507
|
+
case 0:
|
|
508
|
+
if (!this.scopedToken) {
|
|
509
|
+
throw new Error("Not connected. Call connect() or connectWithOAuthToken() first.");
|
|
510
|
+
}
|
|
511
|
+
if (!textContents || textContents.length === 0) {
|
|
512
|
+
throw new Error("No text content provided for bulk upload.");
|
|
513
|
+
}
|
|
514
|
+
if (textContents.length > 10) {
|
|
515
|
+
throw new Error("Too many items. Maximum 10 items per bulk upload.");
|
|
516
|
+
}
|
|
517
|
+
if (!(this.isV1Mode && this.config.appId)) return [3 /*break*/, 5];
|
|
518
|
+
formData = new FormData();
|
|
519
|
+
texts = textContents.map(function (tc) { return tc.text; });
|
|
520
|
+
names = textContents.map(function (tc) { return tc.contentName; });
|
|
521
|
+
formData.append("text_contents", JSON.stringify(texts));
|
|
522
|
+
formData.append("content_names", JSON.stringify(names));
|
|
523
|
+
// Add scope values if provided
|
|
524
|
+
if (scopeValues && Object.keys(scopeValues).length > 0) {
|
|
525
|
+
formData.append("scope_values", JSON.stringify(scopeValues));
|
|
526
|
+
}
|
|
527
|
+
return [4 /*yield*/, fetch("".concat(this.config.baseUrl, "/v1/me/chats/files/upload-bulk"), {
|
|
528
|
+
method: "POST",
|
|
529
|
+
headers: {
|
|
530
|
+
Authorization: "Bearer ".concat(this.scopedToken),
|
|
531
|
+
"X-App-ID": this.config.appId,
|
|
532
|
+
},
|
|
533
|
+
body: formData,
|
|
534
|
+
})];
|
|
535
|
+
case 1:
|
|
536
|
+
response = _a.sent();
|
|
537
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
|
538
|
+
return [4 /*yield*/, response.json()];
|
|
539
|
+
case 2:
|
|
540
|
+
error = _a.sent();
|
|
541
|
+
throw new Error("V1 bulk text upload failed: ".concat(error.detail || response.statusText));
|
|
542
|
+
case 3: return [4 /*yield*/, response.json()];
|
|
543
|
+
case 4:
|
|
544
|
+
data = _a.sent();
|
|
545
|
+
return [2 /*return*/, {
|
|
546
|
+
success: data.success,
|
|
547
|
+
total_files: data.total_files,
|
|
548
|
+
successful_uploads: data.successful_uploads,
|
|
549
|
+
failed_uploads: data.failed_uploads,
|
|
550
|
+
total_size_bytes: data.total_size_bytes,
|
|
551
|
+
chat_id: data.chat_id,
|
|
552
|
+
user_id: data.user_id,
|
|
553
|
+
results: data.results,
|
|
554
|
+
message: data.message,
|
|
555
|
+
}];
|
|
556
|
+
case 5:
|
|
557
|
+
results = [];
|
|
558
|
+
successful_uploads = 0;
|
|
559
|
+
total_size_bytes = 0;
|
|
560
|
+
_i = 0, textContents_1 = textContents;
|
|
561
|
+
_a.label = 6;
|
|
562
|
+
case 6:
|
|
563
|
+
if (!(_i < textContents_1.length)) return [3 /*break*/, 11];
|
|
564
|
+
textContent = textContents_1[_i];
|
|
565
|
+
_a.label = 7;
|
|
566
|
+
case 7:
|
|
567
|
+
_a.trys.push([7, 9, , 10]);
|
|
568
|
+
return [4 /*yield*/, this.uploadText(textContent.text, textContent.contentName, scopeValues)];
|
|
569
|
+
case 8:
|
|
570
|
+
uploadResult = _a.sent();
|
|
571
|
+
results.push({
|
|
572
|
+
filename: uploadResult.filename,
|
|
573
|
+
success: uploadResult.success,
|
|
574
|
+
error: null,
|
|
575
|
+
file_id: null,
|
|
576
|
+
size_bytes: uploadResult.size,
|
|
577
|
+
processing_status: uploadResult.success ? "completed" : "failed",
|
|
578
|
+
message: uploadResult.message,
|
|
579
|
+
});
|
|
580
|
+
if (uploadResult.success) {
|
|
581
|
+
successful_uploads++;
|
|
582
|
+
total_size_bytes += uploadResult.size;
|
|
583
|
+
}
|
|
584
|
+
return [3 /*break*/, 10];
|
|
585
|
+
case 9:
|
|
586
|
+
error_2 = _a.sent();
|
|
587
|
+
results.push({
|
|
588
|
+
filename: textContent.contentName,
|
|
589
|
+
success: false,
|
|
590
|
+
error: error_2 instanceof Error ? error_2.message : String(error_2),
|
|
591
|
+
file_id: null,
|
|
592
|
+
size_bytes: textContent.text.length,
|
|
593
|
+
processing_status: "failed",
|
|
594
|
+
});
|
|
595
|
+
return [3 /*break*/, 10];
|
|
596
|
+
case 10:
|
|
597
|
+
_i++;
|
|
598
|
+
return [3 /*break*/, 6];
|
|
599
|
+
case 11: return [2 /*return*/, {
|
|
600
|
+
success: successful_uploads > 0,
|
|
601
|
+
total_files: textContents.length,
|
|
602
|
+
successful_uploads: successful_uploads,
|
|
603
|
+
failed_uploads: textContents.length - successful_uploads,
|
|
604
|
+
total_size_bytes: total_size_bytes,
|
|
605
|
+
chat_id: this.currentUserId || "",
|
|
606
|
+
user_id: this.currentUserId || "",
|
|
607
|
+
results: results,
|
|
608
|
+
message: "Bulk text upload completed: ".concat(successful_uploads, "/").concat(textContents.length, " items processed successfully"),
|
|
609
|
+
}];
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
};
|
|
453
614
|
TrainlyClient.prototype.listFiles = function () {
|
|
454
615
|
return __awaiter(this, void 0, void 0, function () {
|
|
455
616
|
var response, error, data;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { TrainlyChat } from "./components/TrainlyChat";
|
|
|
4
4
|
export { TrainlyUpload } from "./components/TrainlyUpload";
|
|
5
5
|
export { TrainlyStatus } from "./components/TrainlyStatus";
|
|
6
6
|
export { TrainlyFileManager } from "./components/TrainlyFileManager";
|
|
7
|
-
export type { TrainlyProviderProps, TrainlyConfig, ChatMessage, Citation, UploadResult, FileInfo, FileListResult, FileDeleteResult, TrainlyError, TrainlyFileManagerProps, } from "./types";
|
|
7
|
+
export type { TrainlyProviderProps, TrainlyConfig, ChatMessage, Citation, UploadResult, TextContent, BulkUploadResult, BulkUploadFileResult, FileInfo, FileListResult, FileDeleteResult, TrainlyError, TrainlyFileManagerProps, } from "./types";
|
package/dist/types.d.ts
CHANGED
|
@@ -34,6 +34,10 @@ export interface UploadResult {
|
|
|
34
34
|
size: number;
|
|
35
35
|
message?: string;
|
|
36
36
|
}
|
|
37
|
+
export interface TextContent {
|
|
38
|
+
text: string;
|
|
39
|
+
contentName: string;
|
|
40
|
+
}
|
|
37
41
|
export interface BulkUploadFileResult {
|
|
38
42
|
filename: string;
|
|
39
43
|
success: boolean;
|
|
@@ -100,9 +104,11 @@ export interface TrainlyContextValue {
|
|
|
100
104
|
citations: Citation[];
|
|
101
105
|
}>;
|
|
102
106
|
upload: (file: File, scopeValues?: Record<string, string | number | boolean>) => Promise<UploadResult>;
|
|
107
|
+
uploadText: (text: string, contentName: string, scopeValues?: Record<string, string | number | boolean>) => Promise<UploadResult>;
|
|
103
108
|
listFiles: () => Promise<FileListResult>;
|
|
104
109
|
deleteFile: (fileId: string) => Promise<FileDeleteResult>;
|
|
105
110
|
bulkUploadFiles: (files: File[], scopeValues?: Record<string, string | number | boolean>) => Promise<BulkUploadResult>;
|
|
111
|
+
bulkUploadText: (textContents: TextContent[], scopeValues?: Record<string, string | number | boolean>) => Promise<BulkUploadResult>;
|
|
106
112
|
connectWithOAuthToken: (idToken: string) => Promise<void>;
|
|
107
113
|
isLoading: boolean;
|
|
108
114
|
isConnected: boolean;
|