IndexingModal.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import React, { useState, useEffect } from 'react';
  2. import { ModelConfig, RawFile, IndexingConfig } from '../types';
  3. import { useLanguage } from '../contexts/LanguageContext';
  4. import { useToast } from '../contexts/ToastContext';
  5. import { Layers, FileText, Database, X, ArrowRight, Files, Info } from 'lucide-react';
  6. import { formatBytes } from '../utils/fileUtils';
  7. import { chunkConfigService } from '../services/chunkConfigService';
  8. interface IndexingModalProps {
  9. isOpen: boolean;
  10. onClose: () => void;
  11. files: RawFile[];
  12. embeddingModels: ModelConfig[];
  13. defaultEmbeddingId: string;
  14. onConfirm: (config: IndexingConfig) => void;
  15. isReconfiguring?: boolean;
  16. }
  17. const IndexingModal: React.FC<IndexingModalProps> = ({
  18. isOpen,
  19. onClose,
  20. files,
  21. embeddingModels,
  22. defaultEmbeddingId,
  23. onConfirm,
  24. isReconfiguring = false
  25. }) => {
  26. const { t } = useLanguage();
  27. const { showWarning } = useToast();
  28. // Configuration state
  29. const [chunkSize, setChunkSize] = useState(200);
  30. const [chunkOverlap, setChunkOverlap] = useState(40);
  31. const [selectedEmbedding, setSelectedEmbedding] = useState('');
  32. // Limit info state
  33. const [limits, setLimits] = useState<{
  34. maxChunkSize: number;
  35. maxOverlapSize: number;
  36. defaultChunkSize: number;
  37. defaultOverlapSize: number;
  38. modelInfo: {
  39. name: string;
  40. maxInputTokens: number;
  41. maxBatchSize: number;
  42. expectedDimensions: number;
  43. };
  44. } | null>(null);
  45. const [isLoadingLimits, setIsLoadingLimits] = useState(false);
  46. // Get auth token
  47. const getAuthToken = () => {
  48. return localStorage.getItem('authToken') || '';
  49. };
  50. // Load config limits when selected model changes
  51. useEffect(() => {
  52. if (!isOpen || !selectedEmbedding) {
  53. setLimits(null);
  54. return;
  55. }
  56. const loadLimits = async () => {
  57. setIsLoadingLimits(true);
  58. try {
  59. const token = getAuthToken();
  60. if (!token) return;
  61. const limitData = await chunkConfigService.getLimits(selectedEmbedding, token);
  62. setLimits(limitData);
  63. // Auto-adjust if current values exceed new limits
  64. if (chunkSize > limitData.maxChunkSize) {
  65. setChunkSize(limitData.maxChunkSize);
  66. showWarning(t('autoAdjustChunk', limitData.maxChunkSize));
  67. }
  68. if (chunkOverlap > limitData.maxOverlapSize) {
  69. setChunkOverlap(limitData.maxOverlapSize);
  70. showWarning(t('autoAdjustOverlap', limitData.maxOverlapSize));
  71. }
  72. } catch (error) {
  73. console.error('設定制限の読み込みに失敗しました:', error);
  74. showWarning(t('loadLimitsFailed'));
  75. } finally {
  76. setIsLoadingLimits(false);
  77. }
  78. };
  79. loadLimits();
  80. }, [isOpen, selectedEmbedding]);
  81. // Initialize modal
  82. useEffect(() => {
  83. if (isOpen) {
  84. // Set default embedding model
  85. const validDefault = embeddingModels.find(m => m.id === defaultEmbeddingId);
  86. if (validDefault) {
  87. setSelectedEmbedding(defaultEmbeddingId);
  88. } else if (embeddingModels.length > 0) {
  89. setSelectedEmbedding(embeddingModels[0].id);
  90. } else {
  91. setSelectedEmbedding('');
  92. }
  93. // Reset to defaults
  94. setChunkSize(200);
  95. setChunkOverlap(40);
  96. }
  97. }, [isOpen, defaultEmbeddingId, embeddingModels]);
  98. // Handle chunk size change
  99. const handleChunkSizeChange = (value: number) => {
  100. if (limits && value > limits.maxChunkSize) {
  101. showWarning(t('maxValueMsg', limits.maxChunkSize));
  102. setChunkSize(limits.maxChunkSize);
  103. return;
  104. }
  105. setChunkSize(value);
  106. // Auto-adjust overlap if it exceeds 50% of new chunk size
  107. if (chunkOverlap > value * 0.5) {
  108. setChunkOverlap(Math.floor(value * 0.5));
  109. }
  110. };
  111. // Handle overlap size change
  112. const handleChunkOverlapChange = (value: number) => {
  113. if (limits && value > limits.maxOverlapSize) {
  114. showWarning(t('maxValueMsg', limits.maxOverlapSize));
  115. setChunkOverlap(limits.maxOverlapSize);
  116. return;
  117. }
  118. // Check if it exceeds 50% of chunk size
  119. const maxOverlapByRatio = Math.floor(chunkSize * 0.5);
  120. if (value > maxOverlapByRatio) {
  121. showWarning(t('overlapRatioLimit', maxOverlapByRatio));
  122. setChunkOverlap(maxOverlapByRatio);
  123. return;
  124. }
  125. setChunkOverlap(value);
  126. };
  127. // Render limits info
  128. const renderLimitsInfo = () => {
  129. if (!limits || isLoadingLimits) {
  130. return null;
  131. }
  132. return (
  133. <div className="bg-blue-50 border border-blue-200 rounded-lg p-3 text-xs">
  134. <div className="flex items-center gap-2 mb-2 font-semibold text-blue-800">
  135. <Info className="w-4 h-4" />
  136. {t('modelLimitsInfo')}
  137. </div>
  138. <div className="grid grid-cols-2 gap-2 text-blue-700">
  139. <div>{t('model')}: <span className="font-medium">{limits.modelInfo.name}</span></div>
  140. <div>{t('maxChunkSize')}: <span className="font-medium">{limits.maxChunkSize} tokens</span></div>
  141. <div>{t('maxOverlapSize')}: <span className="font-medium">{limits.maxOverlapSize} tokens</span></div>
  142. <div>{t('maxBatchSize')}: <span className="font-medium">{limits.modelInfo.maxBatchSize}</span></div>
  143. </div>
  144. {limits.modelInfo.maxInputTokens > limits.maxChunkSize && (
  145. <div className="mt-1 text-blue-600 text-[10px]">
  146. ⚠️ {t('envLimitWeaker')}: {limits.maxChunkSize} &lt; {limits.modelInfo.maxInputTokens}
  147. </div>
  148. )}
  149. </div>
  150. );
  151. };
  152. if (!isOpen) return null;
  153. return (
  154. <div className="fixed inset-0 z-[70] flex items-center justify-center bg-black/60 backdrop-blur-sm p-4 animate-in fade-in duration-200">
  155. <div className="bg-white rounded-xl shadow-2xl w-full max-w-lg overflow-hidden flex flex-col max-h-[90vh]">
  156. {/* Header */}
  157. <div className="p-5 border-b border-slate-100 bg-slate-50">
  158. <div className="flex justify-between items-start">
  159. <div>
  160. <h2 className="text-lg font-bold text-slate-800 flex items-center gap-2">
  161. <Database className="w-5 h-5 text-blue-600" />
  162. {isReconfiguring ? t('reconfigureFile') : t('idxModalTitle')}
  163. </h2>
  164. <p className="text-xs text-slate-500 mt-1">
  165. {isReconfiguring ? t('modifySettings') : t('idxDesc')}
  166. </p>
  167. </div>
  168. <button onClick={onClose} className="p-1 hover:bg-slate-200 rounded-full transition-colors">
  169. <X className="w-5 h-5 text-slate-400" />
  170. </button>
  171. </div>
  172. </div>
  173. <div className="flex-1 overflow-y-auto p-5 space-y-6">
  174. {/* Pending Files */}
  175. <div>
  176. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  177. <Files className="w-4 h-4 text-slate-500" />
  178. {t('idxFiles')}
  179. </h3>
  180. <div className="space-y-1 max-h-32 overflow-y-auto bg-slate-50 rounded-lg p-2 border border-slate-200">
  181. {files.map((file, index) => (
  182. <div key={index} className="text-xs text-slate-600 flex items-center justify-between py-1 px-2 hover:bg-white rounded transition-colors">
  183. <span className="truncate flex-1">{file.name}</span>
  184. <span className="text-slate-400 ml-2">{formatBytes(file.size)}</span>
  185. </div>
  186. ))}
  187. </div>
  188. </div>
  189. {/* Embedding Model Selection */}
  190. <div>
  191. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  192. <Layers className="w-4 h-4 text-slate-500" />
  193. {t('idxEmbeddingModel')}
  194. </h3>
  195. <select
  196. className="w-full text-sm border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 outline-none"
  197. value={selectedEmbedding}
  198. onChange={(e) => setSelectedEmbedding(e.target.value)}
  199. >
  200. <option value="">{t('pleaseSelect')}</option>
  201. {embeddingModels.map(model => (
  202. <option key={model.id} value={model.id}>
  203. {model.name} ({model.modelId})
  204. </option>
  205. ))}
  206. </select>
  207. </div>
  208. {/* Chunk Configuration */}
  209. <div>
  210. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  211. <FileText className="w-4 h-4 text-slate-500" />
  212. {t('idxMethod')}
  213. </h3>
  214. <div className="space-y-3">
  215. {/* Chunk Size */}
  216. <div>
  217. <div className="flex justify-between mb-1 text-xs">
  218. <span className="text-slate-600">{t('chunkSize')}</span>
  219. <span className="font-mono font-semibold text-blue-600">{chunkSize}</span>
  220. </div>
  221. <input
  222. type="range"
  223. min="50"
  224. max={limits?.maxChunkSize || 8191}
  225. value={chunkSize}
  226. onChange={(e) => handleChunkSizeChange(Number(e.target.value))}
  227. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  228. disabled={!selectedEmbedding || isLoadingLimits}
  229. />
  230. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  231. <span>{t('min')}: 50</span>
  232. <span>{t('max')}: {limits?.maxChunkSize || '—'}</span>
  233. </div>
  234. </div>
  235. {/* Overlap Size */}
  236. <div>
  237. <div className="flex justify-between mb-1 text-xs">
  238. <span className="text-slate-600">{t('chunkOverlap')}</span>
  239. <span className="font-mono font-semibold text-blue-600">{chunkOverlap}</span>
  240. </div>
  241. <input
  242. type="range"
  243. min="0"
  244. max={limits?.maxOverlapSize || 200}
  245. value={chunkOverlap}
  246. onChange={(e) => handleChunkOverlapChange(Number(e.target.value))}
  247. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  248. disabled={!selectedEmbedding || isLoadingLimits}
  249. />
  250. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  251. <span>{t('min')}: 0</span>
  252. <span>{t('max')}: {limits?.maxOverlapSize || '—'}</span>
  253. </div>
  254. </div>
  255. </div>
  256. </div>
  257. {/* Model Limits Info */}
  258. {renderLimitsInfo()}
  259. {/* Optimization Tips */}
  260. {limits && (
  261. <div className="bg-amber-50 border border-amber-200 rounded-lg p-3 text-xs text-amber-800">
  262. <p className="font-medium mb-1">💡 {t('optimizationTips')}</p>
  263. <ul className="list-disc list-inside space-y-0.5 text-[11px]">
  264. {chunkSize > 800 && <li>{t('tipChunkTooLarge')}</li>}
  265. {chunkOverlap < chunkSize * 0.1 && <li>{t('tipOverlapSmall').replace('$1', String(Math.floor(chunkSize * 0.1)))}</li>}
  266. {chunkSize === limits.maxChunkSize && <li>{t('tipMaxValues')}</li>}
  267. </ul>
  268. </div>
  269. )}
  270. </div>
  271. {/* Footer Buttons */}
  272. <div className="p-4 border-t border-slate-100 bg-slate-50 flex justify-end gap-2">
  273. <button
  274. onClick={onClose}
  275. className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-lg transition-colors"
  276. >
  277. {t('idxCancel')}
  278. </button>
  279. <button
  280. onClick={() => {
  281. if (!selectedEmbedding) {
  282. showWarning(t('selectEmbeddingFirst'));
  283. return;
  284. }
  285. onConfirm({
  286. chunkSize,
  287. chunkOverlap,
  288. embeddingModelId: selectedEmbedding
  289. });
  290. }}
  291. disabled={!selectedEmbedding || isLoadingLimits}
  292. 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"
  293. >
  294. <Database className="w-4 h-4" />
  295. {t('idxStart')}
  296. </button>
  297. </div>
  298. </div>
  299. </div>
  300. );
  301. };
  302. export default IndexingModal;