IndexingModalWithMode.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**
  2. * Processing mode selection (Fast/Precise) support
  3. */
  4. import React, { useState, useEffect } from 'react';
  5. import { createPortal } from 'react-dom';
  6. import { ModelConfig, RawFile, IndexingConfig } from '../types';
  7. import { useLanguage } from '../contexts/LanguageContext';
  8. import { useToast } from '../contexts/ToastContext';
  9. import { Layers, FileText, Database, X, ArrowRight, Files, Info, Zap, Target, AlertTriangle, Clock, DollarSign } from 'lucide-react';
  10. import { formatBytes } from '../utils/fileUtils';
  11. import { chunkConfigService } from '../services/chunkConfigService';
  12. import { uploadService } from '../services/uploadService';
  13. interface IndexingModalWithModeProps {
  14. isOpen: boolean;
  15. onClose: () => void;
  16. files?: RawFile[];
  17. embeddingModels: ModelConfig[];
  18. defaultEmbeddingId: string;
  19. onConfirm: (config: IndexingConfig) => void;
  20. isReconfiguring?: boolean;
  21. }
  22. const IndexingModalWithMode: React.FC<IndexingModalWithModeProps> = ({
  23. isOpen,
  24. onClose,
  25. files = [],
  26. embeddingModels,
  27. defaultEmbeddingId,
  28. onConfirm,
  29. isReconfiguring = false
  30. }) => {
  31. const { t } = useLanguage();
  32. const { showWarning, showInfo } = useToast();
  33. // Configuration state
  34. const [chunkSize, setChunkSize] = useState(200);
  35. const [chunkOverlap, setChunkOverlap] = useState(40);
  36. const [selectedEmbedding, setSelectedEmbedding] = useState('');
  37. const [mode, setMode] = useState<'fast' | 'precise'>('fast');
  38. const [userSelectedMode, setUserSelectedMode] = useState(false); // Track if user manually selected mode
  39. // Mode recommendation info
  40. const [modeRecommendation, setModeRecommendation] = useState<any>(null);
  41. const [isLoadingRecommendation, setIsLoadingRecommendation] = useState(false);
  42. // Limit info state
  43. const [limits, setLimits] = useState<{
  44. maxChunkSize: number;
  45. maxOverlapSize: number;
  46. defaultChunkSize: number;
  47. defaultOverlapSize: number;
  48. modelInfo: {
  49. name: string;
  50. maxInputTokens: number;
  51. maxBatchSize: number;
  52. expectedDimensions: number;
  53. };
  54. } | null>(null);
  55. const [isLoadingLimits, setIsLoadingLimits] = useState(false);
  56. // Get auth token
  57. const getAuthToken = () => {
  58. return localStorage.getItem('authToken') || '';
  59. };
  60. // Load mode recommendation when files change
  61. useEffect(() => {
  62. if (!isOpen || !files || files.length === 0) return;
  63. const loadRecommendation = async () => {
  64. setIsLoadingRecommendation(true);
  65. try {
  66. // Use first file for recommendation (assume similar types)
  67. const file = files[0];
  68. const rec = await uploadService.recommendMode(file.file);
  69. setModeRecommendation(rec);
  70. // Auto-select recommended mode if user hasn't manually selected one
  71. if (!isReconfiguring && !userSelectedMode) {
  72. setMode(rec.recommendedMode);
  73. showInfo(t('recommendationMsg', rec.recommendedMode === 'precise' ? t('preciseMode') : t('fastMode'), t(rec.reason, ...(rec.reasonArgs || []))));
  74. }
  75. } catch (error) {
  76. console.error('モード推奨の取得に失敗しました:', error);
  77. } finally {
  78. setIsLoadingRecommendation(false);
  79. }
  80. };
  81. loadRecommendation();
  82. }, [isOpen, files, isReconfiguring]);
  83. // Load config limits when selected model changes
  84. useEffect(() => {
  85. if (!isOpen || !selectedEmbedding) {
  86. setLimits(null);
  87. return;
  88. }
  89. const loadLimits = async () => {
  90. setIsLoadingLimits(true);
  91. try {
  92. const token = getAuthToken();
  93. if (!token) return;
  94. const limitData = await chunkConfigService.getLimits(selectedEmbedding, token);
  95. setLimits(limitData);
  96. // Auto-adjust if current values exceed new limits
  97. if (chunkSize > limitData.maxChunkSize) {
  98. setChunkSize(limitData.maxChunkSize);
  99. showWarning(t('autoAdjustChunk', limitData.maxChunkSize));
  100. }
  101. if (chunkOverlap > limitData.maxOverlapSize) {
  102. setChunkOverlap(limitData.maxOverlapSize);
  103. showWarning(t('autoAdjustOverlap', limitData.maxOverlapSize));
  104. }
  105. } catch (error) {
  106. console.error('設定制限の読み込みに失敗しました:', error);
  107. showWarning(t('loadLimitsFailed'));
  108. } finally {
  109. setIsLoadingLimits(false);
  110. }
  111. };
  112. loadLimits();
  113. }, [isOpen, selectedEmbedding]);
  114. // Track isOpen state change, reset only on open
  115. const [prevOpen, setPrevOpen] = useState(false);
  116. // Initialize modal
  117. useEffect(() => {
  118. if (isOpen && !prevOpen) {
  119. // Execute initialization only when going from closed to open
  120. console.log('DEBUG: IndexingModalWithMode opening, files:', files);
  121. // Set default embedding model
  122. const enabledModels = embeddingModels.filter(m => m.isEnabled !== false);
  123. const validDefault = enabledModels.find(m => m.id === defaultEmbeddingId);
  124. if (validDefault) {
  125. setSelectedEmbedding(defaultEmbeddingId);
  126. } else if (enabledModels.length > 0) {
  127. setSelectedEmbedding(enabledModels[0].id);
  128. } else {
  129. setSelectedEmbedding('');
  130. }
  131. // Reset to defaults
  132. setChunkSize(200);
  133. setChunkOverlap(40);
  134. if (!isReconfiguring) {
  135. setMode('fast');
  136. setUserSelectedMode(false); // Reset user selection status
  137. }
  138. setModeRecommendation(null);
  139. }
  140. setPrevOpen(isOpen);
  141. }, [isOpen, prevOpen, defaultEmbeddingId, embeddingModels, isReconfiguring]);
  142. // Handle chunk size change
  143. const handleChunkSizeChange = (value: number) => {
  144. if (limits && value > limits.maxChunkSize) {
  145. showWarning(t('maxValueMsg', limits.maxChunkSize));
  146. setChunkSize(limits.maxChunkSize);
  147. return;
  148. }
  149. setChunkSize(value);
  150. // Auto-adjust overlap if it exceeds 50% of new chunk size
  151. if (chunkOverlap > value * 0.5) {
  152. setChunkOverlap(Math.floor(value * 0.5));
  153. }
  154. };
  155. // Handle overlap size change
  156. const handleChunkOverlapChange = (value: number) => {
  157. if (limits && value > limits.maxOverlapSize) {
  158. showWarning(t('maxValueMsg', limits.maxOverlapSize));
  159. setChunkOverlap(limits.maxOverlapSize);
  160. return;
  161. }
  162. // Check if it exceeds 50% of chunk size
  163. const maxOverlapByRatio = Math.floor(chunkSize * 0.5);
  164. if (value > maxOverlapByRatio) {
  165. showWarning(t('overlapRatioLimit', maxOverlapByRatio));
  166. setChunkOverlap(maxOverlapByRatio);
  167. return;
  168. }
  169. setChunkOverlap(value);
  170. };
  171. // Render limits info
  172. const renderLimitsInfo = () => {
  173. if (!limits || isLoadingLimits) {
  174. return null;
  175. }
  176. return (
  177. <div className="bg-blue-50 border border-blue-200 rounded-lg p-3 text-xs">
  178. <div className="flex items-center gap-2 mb-2 font-semibold text-blue-800">
  179. <Info className="w-4 h-4" />
  180. {t('modelLimitsInfo')}
  181. </div>
  182. <div className="grid grid-cols-2 gap-2 text-blue-700">
  183. <div>{t('model')}: <span className="font-medium">{limits.modelInfo.name}</span></div>
  184. <div>{t('maxChunkSize')}: <span className="font-medium">{limits.maxChunkSize} tokens</span></div>
  185. <div>{t('maxOverlapSize')}: <span className="font-medium">{limits.maxOverlapSize} tokens</span></div>
  186. <div>{t('maxBatchSize')}: <span className="font-medium">{limits.modelInfo.maxBatchSize}</span></div>
  187. </div>
  188. {limits.modelInfo.maxInputTokens > limits.maxChunkSize && (
  189. <div className="mt-1 text-blue-600 text-[10px]">
  190. ⚠️ {t('envLimitWeaker')}: {limits.maxChunkSize} &lt; {limits.modelInfo.maxInputTokens}
  191. </div>
  192. )}
  193. </div>
  194. );
  195. };
  196. // Render mode recommendation info
  197. const renderModeRecommendation = () => {
  198. if (!modeRecommendation || isLoadingRecommendation) {
  199. return null;
  200. }
  201. return (
  202. <div className="space-y-2 p-3 bg-purple-50 border border-purple-200 rounded-lg text-xs">
  203. <div className="font-semibold text-purple-800 flex items-center gap-2">
  204. <Target className="w-4 h-4" />
  205. {t('processingMode')}
  206. </div>
  207. <div className="text-purple-700">
  208. <strong>{t('recommendationReason')}:</strong> {t(modeRecommendation.reason, ...(modeRecommendation.reasonArgs || []))}
  209. </div>
  210. {modeRecommendation.warnings && modeRecommendation.warnings.length > 0 && (
  211. <div className="mt-1 space-y-1">
  212. {modeRecommendation.warnings.map((warning: string, idx: number) => (
  213. <div key={idx} className="text-purple-800 flex items-start gap-1">
  214. <AlertTriangle className="w-3 h-3 mt-0.5 flex-shrink-0" />
  215. <span>{t(warning as any)}</span>
  216. </div>
  217. ))}
  218. </div>
  219. )}
  220. </div>
  221. );
  222. };
  223. // Render current mode description
  224. const renderModeDescription = () => {
  225. if (mode === 'fast') {
  226. return (
  227. <div className="text-xs text-slate-600 bg-slate-50 p-2 rounded border border-slate-200">
  228. <div className="font-semibold text-slate-700 mb-1 flex items-center gap-1">
  229. <Zap className="w-3 h-3 text-yellow-600" />
  230. {t('fastModeFeatures')}
  231. </div>
  232. <ul className="list-disc list-inside space-y-0.5 text-[11px]">
  233. <li>{t('fastFeature1')}</li>
  234. <li>{t('fastFeature2')}</li>
  235. <li>{t('fastFeature3')}</li>
  236. <li>{t('fastFeature4')}</li>
  237. <li>{t('fastFeature5')}</li>
  238. </ul>
  239. </div>
  240. );
  241. }
  242. return (
  243. <div className="text-xs text-slate-600 bg-slate-50 p-2 rounded border border-slate-200">
  244. <div className="font-semibold text-slate-700 mb-1 flex items-center gap-1">
  245. <Target className="w-3 h-3 text-blue-600" />
  246. {t('preciseModeFeatures')}
  247. </div>
  248. <ul className="list-disc list-inside space-y-0.5 text-[11px]">
  249. <li>{t('preciseFeature1')}</li>
  250. <li>{t('preciseFeature2')}</li>
  251. <li>{t('preciseFeature3')}</li>
  252. <li>{t('preciseFeature4')}</li>
  253. <li>{t('preciseFeature5')}</li>
  254. <li>{t('preciseFeature6')}</li>
  255. </ul>
  256. </div>
  257. );
  258. };
  259. if (!isOpen) return null;
  260. return createPortal(
  261. <>
  262. <div
  263. className="fixed inset-0 z-[100] bg-black/50 backdrop-blur-sm transition-opacity"
  264. onClick={onClose}
  265. />
  266. <div className="fixed right-0 top-0 h-full w-full max-w-lg bg-white shadow-2xl z-[101] transform transition-transform duration-300 ease-in-out animate-in slide-in-from-right flex flex-col">
  267. {/* Header */}
  268. <div className="p-5 border-b border-slate-100 bg-slate-50 shrink-0">
  269. <div className="flex justify-between items-start">
  270. <div>
  271. <h2 className="text-lg font-bold text-slate-800 flex items-center gap-2">
  272. <Database className="w-5 h-5 text-blue-600" />
  273. {isReconfiguring ? t('reconfigureTitle') : t('indexingConfigTitle')}
  274. </h2>
  275. <p className="text-xs text-slate-500 mt-1">
  276. {isReconfiguring ? t('reconfigureDesc') : t('indexingConfigDesc')}
  277. </p>
  278. </div>
  279. <button
  280. onClick={(e) => {
  281. e.stopPropagation();
  282. onClose();
  283. }}
  284. className="p-2 hover:bg-slate-200 rounded-full transition-colors active:scale-90"
  285. >
  286. <X className="w-5 h-5 text-slate-500" />
  287. </button>
  288. </div>
  289. </div>
  290. <div className="flex-1 overflow-y-auto p-5 space-y-6">
  291. {/* Pending files - only show when there are files */}
  292. {files && files.length > 0 && (
  293. <div>
  294. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  295. <Files className="w-4 h-4 text-slate-500" />
  296. {t('pendingFiles')}
  297. </h3>
  298. <div className="space-y-1 max-h-32 overflow-y-auto bg-slate-50 rounded-lg p-2 border border-slate-200">
  299. {files.map((file, index) => (
  300. <div key={index} className="text-xs text-slate-600 flex items-center justify-between py-1 px-2 hover:bg-white rounded transition-colors">
  301. <span className="truncate flex-1">{file.name}</span>
  302. <span className="text-slate-400 ml-2">{formatBytes(file.size)}</span>
  303. </div>
  304. ))}
  305. </div>
  306. </div>
  307. )}
  308. {/* Processing mode selection */}
  309. {!isReconfiguring && (
  310. <div>
  311. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  312. <Target className="w-4 h-4 text-slate-500" />
  313. {t('processingMode')}
  314. {isLoadingRecommendation && <span className="text-xs text-blue-600 ml-2">{t('analyzingFile')}</span>}
  315. </h3>
  316. {/* Mode recommendation info */}
  317. {renderModeRecommendation()}
  318. {/* Mode selection */}
  319. <div className="grid grid-cols-2 gap-3 mt-3">
  320. {/* Fast Mode */}
  321. <button
  322. onClick={() => {
  323. setMode('fast');
  324. setUserSelectedMode(true); // 标记用户手动选择
  325. }}
  326. className={`relative p-3 rounded-lg border-2 text-left transition-all ${mode === 'fast'
  327. ? 'border-blue-500 bg-blue-50'
  328. : 'border-slate-200 hover:border-slate-300'
  329. }`}
  330. >
  331. <div className="flex items-center gap-2 mb-2">
  332. <Zap className="w-4 h-4 text-yellow-600" />
  333. <span className="font-semibold text-sm">{t('fastMode')}</span>
  334. </div>
  335. <div className="text-xs text-slate-600 leading-relaxed">
  336. {t('fastModeDesc')}
  337. </div>
  338. {mode === 'fast' && (
  339. <div className="absolute top-2 right-2 text-blue-600">
  340. <div className="w-2 h-2 bg-blue-600 rounded-full"></div>
  341. </div>
  342. )}
  343. </button>
  344. {/* Precise Mode */}
  345. <button
  346. onClick={() => {
  347. setMode('precise');
  348. setUserSelectedMode(true); // 标记用户手动选择
  349. }}
  350. className={`relative p-3 rounded-lg border-2 text-left transition-all ${mode === 'precise'
  351. ? 'border-purple-500 bg-purple-50'
  352. : 'border-slate-200 hover:border-slate-300'
  353. }`}
  354. >
  355. <div className="flex items-center gap-2 mb-2">
  356. <Target className="w-4 h-4 text-purple-600" />
  357. <span className="font-semibold text-sm">{t('preciseMode')}</span>
  358. </div>
  359. <div className="text-xs text-slate-600 leading-relaxed">
  360. {t('preciseModeDesc')}
  361. </div>
  362. {mode === 'precise' && (
  363. <div className="absolute top-2 right-2 text-purple-600">
  364. <div className="w-2 h-2 bg-purple-600 rounded-full"></div>
  365. </div>
  366. )}
  367. </button>
  368. </div>
  369. {/* Mode description */}
  370. <div className="mt-3">
  371. {renderModeDescription()}
  372. </div>
  373. </div>
  374. )}
  375. {/* Embedding model selection */}
  376. <div>
  377. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  378. <Layers className="w-4 h-4 text-slate-500" />
  379. {t('embeddingModel')}
  380. </h3>
  381. <select
  382. className="w-full text-sm border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 outline-none"
  383. value={selectedEmbedding}
  384. onChange={(e) => setSelectedEmbedding(e.target.value)}
  385. >
  386. <option value="">{t('pleaseSelect')}</option>
  387. {embeddingModels.filter(m => m.isEnabled !== false).map(model => (
  388. <option key={model.id} value={model.id}>
  389. {model.name} ({model.modelId})
  390. </option>
  391. ))}
  392. </select>
  393. </div>
  394. {/* Chunk config */}
  395. <div>
  396. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  397. <FileText className="w-4 h-4 text-slate-500" />
  398. {t('chunkConfig')}
  399. </h3>
  400. <div className="space-y-3">
  401. {/* Chunk size */}
  402. <div>
  403. <div className="flex justify-between mb-1 text-xs">
  404. <span className="text-slate-600">{t('chunkSize')}</span>
  405. <span className="font-mono font-semibold text-blue-600">{chunkSize}</span>
  406. </div>
  407. <input
  408. type="range"
  409. min="50"
  410. max={limits?.maxChunkSize || 8191}
  411. value={chunkSize}
  412. onChange={(e) => handleChunkSizeChange(Number(e.target.value))}
  413. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  414. disabled={!selectedEmbedding || isLoadingLimits}
  415. />
  416. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  417. <span>{t('min')}: 50</span>
  418. <span>{t('max')}: {limits?.maxChunkSize || '-'}</span>
  419. </div>
  420. </div>
  421. {/* Overlap size */}
  422. <div>
  423. <div className="flex justify-between mb-1 text-xs">
  424. <span className="text-slate-600">{t('chunkOverlap')}</span>
  425. <span className="font-mono font-semibold text-blue-600">{chunkOverlap}</span>
  426. </div>
  427. <input
  428. type="range"
  429. min="0"
  430. max={limits?.maxOverlapSize || 200}
  431. value={chunkOverlap}
  432. onChange={(e) => handleChunkOverlapChange(Number(e.target.value))}
  433. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  434. disabled={!selectedEmbedding || isLoadingLimits}
  435. />
  436. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  437. <span>{t('min')}: 0</span>
  438. <span>{t('max')}: {limits?.maxOverlapSize || '-'}</span>
  439. </div>
  440. </div>
  441. </div>
  442. </div>
  443. {isReconfiguring && renderLimitsInfo()}
  444. {/* Optimization tips */}
  445. {limits && (
  446. <div className="bg-amber-50 border border-amber-200 rounded-lg p-3 text-xs text-amber-800">
  447. <p className="font-medium mb-1">💡 {t('optimizationTips')}</p>
  448. <ul className="list-disc list-inside space-y-0.5 text-[11px]">
  449. {chunkSize > 800 && <li>{t('tipChunkTooLarge')}</li>}
  450. {chunkOverlap < chunkSize * 0.1 && <li>{t('tipOverlapSmall').replace('$1', `${Math.floor(chunkSize * 0.1)}`)}</li>}
  451. {chunkSize === limits.maxChunkSize && <li>{t('tipMaxValues')}</li>}
  452. {mode === 'precise' && <li>{t('tipPreciseCost')}</li>}
  453. </ul>
  454. </div>
  455. )}
  456. </div>
  457. {/* Footer buttons */}
  458. <div className="p-4 border-t border-slate-100 bg-slate-50 flex justify-end gap-2 shrink-0">
  459. <button
  460. onClick={(e) => {
  461. e.stopPropagation();
  462. onClose();
  463. }}
  464. className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-lg transition-colors active:scale-95"
  465. >
  466. {t('cancel')}
  467. </button>
  468. <button
  469. onClick={() => {
  470. if (!selectedEmbedding) {
  471. showWarning(t('selectEmbeddingFirst'));
  472. return;
  473. }
  474. if (!isReconfiguring && mode === 'precise') {
  475. // Precise mode confirmation
  476. if (!confirm(t('confirmPreciseCost'))) {
  477. return;
  478. }
  479. }
  480. onConfirm({
  481. chunkSize,
  482. chunkOverlap,
  483. embeddingModelId: selectedEmbedding,
  484. mode,
  485. });
  486. }}
  487. disabled={isLoadingLimits}
  488. className="px-4 py-2 text-sm bg-blue-600 text-white hover:bg-blue-700 rounded-lg shadow-sm flex items-center gap-2 transition-transform active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed"
  489. >
  490. <Database className="w-4 h-4" />
  491. {t('startProcessing')}
  492. </button>
  493. </div>
  494. </div>
  495. </>,
  496. document.body
  497. );
  498. };
  499. export default IndexingModalWithMode;