test-local-import.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || (function () {
  19. var ownKeys = function(o) {
  20. ownKeys = Object.getOwnPropertyNames || function (o) {
  21. var ar = [];
  22. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  23. return ar;
  24. };
  25. return ownKeys(o);
  26. };
  27. return function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. })();
  35. var __importDefault = (this && this.__importDefault) || function (mod) {
  36. return (mod && mod.__esModule) ? mod : { "default": mod };
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. const axios_1 = __importDefault(require("axios"));
  40. const fs = __importStar(require("fs"));
  41. const path = __importStar(require("path"));
  42. const os = __importStar(require("os"));
  43. async function testLocalImport() {
  44. const baseURL = 'http://localhost:3001/api';
  45. const username = process.argv[2] || 'admin';
  46. const password = process.argv[3];
  47. const sourceFolder = process.argv[4];
  48. const tenantId = process.argv[5];
  49. if (!password) {
  50. console.error('Usage: ts-node scripts/test-local-import.ts <username> <password> [sourceFolder] [tenantId]');
  51. process.exit(1);
  52. }
  53. try {
  54. console.log(`Logging in as ${username} to ${baseURL}...`);
  55. const loginRes = await axios_1.default.post(`${baseURL}/auth/login`, {
  56. username,
  57. password
  58. });
  59. const jwtToken = loginRes.data.access_token;
  60. console.log('Login successful.');
  61. console.log('Retrieving API key...');
  62. const apiKeyRes = await axios_1.default.get(`${baseURL}/auth/api-key`, {
  63. headers: { Authorization: `Bearer ${jwtToken}` }
  64. });
  65. const apiKey = apiKeyRes.data.apiKey;
  66. console.log('API Key retrieved:', apiKey);
  67. const authHeaders = { 'x-api-key': apiKey };
  68. if (tenantId) {
  69. authHeaders['x-tenant-id'] = tenantId;
  70. console.log(`Target tenant set to: ${tenantId}`);
  71. }
  72. let targetPath = sourceFolder;
  73. let isTemp = false;
  74. if (!targetPath) {
  75. isTemp = true;
  76. targetPath = path.join(os.tmpdir(), `aurak-test-${Date.now()}`);
  77. const subDir = path.join(targetPath, 'subfolder');
  78. fs.mkdirSync(targetPath, { recursive: true });
  79. fs.mkdirSync(subDir, { recursive: true });
  80. fs.writeFileSync(path.join(targetPath, 'root-file.md'), '# Root File\nContent in root.', 'utf8');
  81. fs.writeFileSync(path.join(subDir, 'sub-file.txt'), 'Content in subfolder.', 'utf8');
  82. console.log(`Created temporary test structure at: ${targetPath}`);
  83. }
  84. else {
  85. console.log(`Using provided source folder: ${targetPath}`);
  86. if (!fs.existsSync(targetPath)) {
  87. throw new Error(`The specified folder does not exist: ${targetPath}`);
  88. }
  89. }
  90. const modelsRes = await axios_1.default.get(`${baseURL}/models`, {
  91. headers: authHeaders
  92. });
  93. const embeddingModel = modelsRes.data.find((m) => m.type === 'embedding' && m.isEnabled !== false);
  94. if (!embeddingModel) {
  95. throw new Error('No enabled embedding model found');
  96. }
  97. console.log(`Using embedding model: ${embeddingModel.id}`);
  98. console.log('Triggering local folder import...');
  99. const importRes = await axios_1.default.post(`${baseURL}/upload/local-folder`, {
  100. sourcePath: targetPath,
  101. embeddingModelId: embeddingModel.id,
  102. useHierarchy: true
  103. }, {
  104. headers: authHeaders
  105. });
  106. console.log('Import response:', importRes.data);
  107. if (isTemp) {
  108. console.log('Waiting for background processing (10s)...');
  109. await new Promise(resolve => setTimeout(resolve, 10000));
  110. const kbRes = await axios_1.default.get(`${baseURL}/knowledge-bases`, {
  111. headers: authHeaders
  112. });
  113. const importedFiles = kbRes.data.filter((f) => f.originalName === 'root-file.md' || f.originalName === 'sub-file.txt');
  114. console.log(`Found ${importedFiles.length} imported files in KB.`);
  115. if (importedFiles.length === 2) {
  116. console.log('SUCCESS: All files imported.');
  117. }
  118. else {
  119. console.log('FAILURE: Not all files were imported.');
  120. }
  121. }
  122. else {
  123. console.log('Custom folder import triggered. Please check the UI or database for results.');
  124. }
  125. }
  126. catch (error) {
  127. if (error.response) {
  128. console.error(`Test failed with status ${error.response.status}:`, JSON.stringify(error.response.data));
  129. }
  130. else {
  131. console.error('Test failed:', error.message);
  132. }
  133. process.exit(1);
  134. }
  135. }
  136. testLocalImport();
  137. //# sourceMappingURL=test-local-import.js.map