IndexingModal.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/50 backdrop-blur-sm border border-blue-100 rounded-xl p-4 text-xs">
  134. <div className="flex items-center gap-2 mb-2 font-bold text-blue-900">
  135. <Info className="w-4 h-4 text-blue-600" />
  136. {t('modelLimitsInfo')}
  137. </div>
  138. <div className="grid grid-cols-2 gap-y-2 gap-x-4 text-slate-600">
  139. <div>{t('model')}: <span className="font-semibold text-slate-900">{limits.modelInfo.name}</span></div>
  140. <div>{t('maxChunkSize')}: <span className="font-semibold text-slate-900">{limits.maxChunkSize} tokens</span></div>
  141. <div>{t('maxOverlapSize')}: <span className="font-semibold text-slate-900">{limits.maxOverlapSize} tokens</span></div>
  142. <div>{t('maxBatchSize')}: <span className="font-semibold text-slate-900">{limits.modelInfo.maxBatchSize}</span></div>
  143. </div>
  144. {limits.modelInfo.maxInputTokens > limits.maxChunkSize && (
  145. <div className="mt-2 text-blue-600/80 text-[10px] flex items-center gap-1">
  146. <Info size={10} />
  147. {t('envLimitWeaker')}: {limits.maxChunkSize} &lt; {limits.modelInfo.maxInputTokens}
  148. </div>
  149. )}
  150. </div>
  151. );
  152. };
  153. if (!isOpen) return null;
  154. return (
  155. <div className="fixed inset-0 z-[70] flex items-center justify-center bg-black/40 backdrop-blur-md p-4 animate-in fade-in duration-300">
  156. <div className="bg-white rounded-3xl shadow-2xl w-full max-w-lg overflow-hidden flex flex-col max-h-[90vh] border border-white/20">
  157. {/* Header */}
  158. <div className="p-6 border-b border-slate-50 bg-white">
  159. <div className="flex justify-between items-start">
  160. <div>
  161. <h2 className="text-xl font-bold text-slate-900 flex items-center gap-2.5">
  162. <div className="p-2 bg-blue-50 rounded-xl">
  163. <Database className="w-5 h-5 text-blue-600" />
  164. </div>
  165. {isReconfiguring ? t('reconfigureFile') : t('idxModalTitle')}
  166. </h2>
  167. <p className="text-[13px] text-slate-500 mt-1 ml-12">
  168. {isReconfiguring ? t('modifySettings') : t('idxDesc')}
  169. </p>
  170. </div>
  171. <button onClick={onClose} className="p-2 hover:bg-slate-100 rounded-xl transition-all active:scale-95">
  172. <X className="w-5 h-5 text-slate-400" />
  173. </button>
  174. </div>
  175. </div>
  176. <div className="flex-1 overflow-y-auto p-5 space-y-6">
  177. {/* Pending Files */}
  178. <div>
  179. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  180. <Files className="w-4 h-4 text-slate-500" />
  181. {t('idxFiles')}
  182. </h3>
  183. <div className="space-y-1 max-h-32 overflow-y-auto bg-slate-50/50 rounded-xl p-3 border border-slate-100">
  184. {files.map((file, index) => (
  185. <div key={index} className="text-xs text-slate-600 flex items-center justify-between py-1.5 px-2 hover:bg-white/80 rounded-lg transition-colors">
  186. <span className="truncate flex-1">{file.name}</span>
  187. <span className="text-slate-400 ml-2 font-medium">{formatBytes(file.size)}</span>
  188. </div>
  189. ))}
  190. </div>
  191. </div>
  192. {/* Embedding Model Selection */}
  193. <div>
  194. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  195. <Layers className="w-4 h-4 text-slate-500" />
  196. {t('idxEmbeddingModel')}
  197. </h3>
  198. <select
  199. className="w-full text-sm border border-slate-100 bg-slate-50/50 rounded-xl px-4 py-2.5 focus:ring-2 focus:ring-blue-100 focus:border-blue-400 outline-none transition-all cursor-pointer"
  200. value={selectedEmbedding}
  201. onChange={(e) => setSelectedEmbedding(e.target.value)}
  202. >
  203. <option value="">{t('pleaseSelect')}</option>
  204. {embeddingModels.map(model => (
  205. <option key={model.id} value={model.id}>
  206. {model.name}
  207. </option>
  208. ))}
  209. </select>
  210. </div>
  211. {/* Chunk Configuration */}
  212. <div>
  213. <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
  214. <FileText className="w-4 h-4 text-slate-500" />
  215. {t('idxMethod')}
  216. </h3>
  217. <div className="space-y-3">
  218. {/* Chunk Size */}
  219. <div>
  220. <div className="flex justify-between mb-1 text-xs">
  221. <span className="text-slate-600">{t('chunkSize')}</span>
  222. <span className="font-mono font-semibold text-blue-600">{chunkSize}</span>
  223. </div>
  224. <input
  225. type="range"
  226. min="50"
  227. max={limits?.maxChunkSize || 8191}
  228. value={chunkSize}
  229. onChange={(e) => handleChunkSizeChange(Number(e.target.value))}
  230. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  231. disabled={!selectedEmbedding || isLoadingLimits}
  232. />
  233. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  234. <span>{t('min')}: 50</span>
  235. <span>{t('max')}: {limits?.maxChunkSize || '—'}</span>
  236. </div>
  237. </div>
  238. {/* Overlap Size */}
  239. <div>
  240. <div className="flex justify-between mb-1 text-xs">
  241. <span className="text-slate-600">{t('chunkOverlap')}</span>
  242. <span className="font-mono font-semibold text-blue-600">{chunkOverlap}</span>
  243. </div>
  244. <input
  245. type="range"
  246. min="0"
  247. max={limits?.maxOverlapSize || 200}
  248. value={chunkOverlap}
  249. onChange={(e) => handleChunkOverlapChange(Number(e.target.value))}
  250. className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
  251. disabled={!selectedEmbedding || isLoadingLimits}
  252. />
  253. <div className="flex justify-between text-[10px] text-slate-400 mt-1">
  254. <span>{t('min')}: 0</span>
  255. <span>{t('max')}: {limits?.maxOverlapSize || '—'}</span>
  256. </div>
  257. </div>
  258. </div>
  259. </div>
  260. {/* Model Limits Info */}
  261. {renderLimitsInfo()}
  262. {/* Optimization Tips */}
  263. {limits && (
  264. <div className="bg-amber-50/50 backdrop-blur-sm border border-amber-100 rounded-xl p-4 text-xs text-amber-900">
  265. <p className="font-bold mb-2 flex items-center gap-1.5">
  266. <span className="text-amber-500">💡</span>
  267. {t('optimizationTips')}
  268. </p>
  269. <ul className="list-disc list-inside space-y-1.5 text-[11px] text-amber-800/80">
  270. {chunkSize > 800 && <li>{t('tipChunkTooLarge')}</li>}
  271. {chunkOverlap < chunkSize * 0.1 && <li>{t('tipOverlapSmall').replace('$1', String(Math.floor(chunkSize * 0.1)))}</li>}
  272. {chunkSize === limits.maxChunkSize && <li>{t('tipMaxValues')}</li>}
  273. </ul>
  274. </div>
  275. )}
  276. </div>
  277. {/* Footer Buttons */}
  278. <div className="p-6 border-t border-slate-50 bg-white flex justify-end gap-3">
  279. <button
  280. onClick={onClose}
  281. className="px-6 py-2.5 text-sm font-semibold text-slate-600 hover:bg-slate-50 rounded-xl transition-all active:scale-95"
  282. >
  283. {t('idxCancel')}
  284. </button>
  285. <button
  286. onClick={() => {
  287. if (!selectedEmbedding) {
  288. showWarning(t('selectEmbeddingFirst'));
  289. return;
  290. }
  291. onConfirm({
  292. chunkSize,
  293. chunkOverlap,
  294. embeddingModelId: selectedEmbedding
  295. });
  296. }}
  297. disabled={!selectedEmbedding || isLoadingLimits}
  298. className="px-8 py-2.5 text-sm font-bold bg-blue-600 text-white hover:bg-blue-700 rounded-xl shadow-lg shadow-blue-200 flex items-center gap-2 transition-all active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed"
  299. >
  300. <ArrowRight className="w-4 h-4" />
  301. {t('idxStart')}
  302. </button>
  303. </div>
  304. </div>
  305. </div>
  306. );
  307. };
  308. export default IndexingModal;