| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- import React, { useState, useEffect } from 'react';
- import { ModelConfig, RawFile, IndexingConfig } from '../types';
- import { useLanguage } from '../contexts/LanguageContext';
- import { useToast } from '../contexts/ToastContext';
- import { Layers, FileText, Database, X, ArrowRight, Files, Info } from 'lucide-react';
- import { formatBytes } from '../utils/fileUtils';
- import { chunkConfigService } from '../services/chunkConfigService';
- interface IndexingModalProps {
- isOpen: boolean;
- onClose: () => void;
- files: RawFile[];
- embeddingModels: ModelConfig[];
- defaultEmbeddingId: string;
- onConfirm: (config: IndexingConfig) => void;
- isReconfiguring?: boolean;
- }
- const IndexingModal: React.FC<IndexingModalProps> = ({
- isOpen,
- onClose,
- files,
- embeddingModels,
- defaultEmbeddingId,
- onConfirm,
- isReconfiguring = false
- }) => {
- const { t } = useLanguage();
- const { showWarning } = useToast();
- // Configuration state
- const [chunkSize, setChunkSize] = useState(200);
- const [chunkOverlap, setChunkOverlap] = useState(40);
- const [selectedEmbedding, setSelectedEmbedding] = useState('');
- // Limit info state
- const [limits, setLimits] = useState<{
- maxChunkSize: number;
- maxOverlapSize: number;
- defaultChunkSize: number;
- defaultOverlapSize: number;
- modelInfo: {
- name: string;
- maxInputTokens: number;
- maxBatchSize: number;
- expectedDimensions: number;
- };
- } | null>(null);
- const [isLoadingLimits, setIsLoadingLimits] = useState(false);
- // Get auth token
- const getAuthToken = () => {
- return localStorage.getItem('authToken') || '';
- };
- // Load config limits when selected model changes
- useEffect(() => {
- if (!isOpen || !selectedEmbedding) {
- setLimits(null);
- return;
- }
- const loadLimits = async () => {
- setIsLoadingLimits(true);
- try {
- const token = getAuthToken();
- if (!token) return;
- const limitData = await chunkConfigService.getLimits(selectedEmbedding, token);
- setLimits(limitData);
- // Auto-adjust if current values exceed new limits
- if (chunkSize > limitData.maxChunkSize) {
- setChunkSize(limitData.maxChunkSize);
- showWarning(t('autoAdjustChunk', limitData.maxChunkSize));
- }
- if (chunkOverlap > limitData.maxOverlapSize) {
- setChunkOverlap(limitData.maxOverlapSize);
- showWarning(t('autoAdjustOverlap', limitData.maxOverlapSize));
- }
- } catch (error) {
- console.error('設定制限の読み込みに失敗しました:', error);
- showWarning(t('loadLimitsFailed'));
- } finally {
- setIsLoadingLimits(false);
- }
- };
- loadLimits();
- }, [isOpen, selectedEmbedding]);
- // Initialize modal
- useEffect(() => {
- if (isOpen) {
- // Set default embedding model
- const validDefault = embeddingModels.find(m => m.id === defaultEmbeddingId);
- if (validDefault) {
- setSelectedEmbedding(defaultEmbeddingId);
- } else if (embeddingModels.length > 0) {
- setSelectedEmbedding(embeddingModels[0].id);
- } else {
- setSelectedEmbedding('');
- }
- // Reset to defaults
- setChunkSize(200);
- setChunkOverlap(40);
- }
- }, [isOpen, defaultEmbeddingId, embeddingModels]);
- // Handle chunk size change
- const handleChunkSizeChange = (value: number) => {
- if (limits && value > limits.maxChunkSize) {
- showWarning(t('maxValueMsg', limits.maxChunkSize));
- setChunkSize(limits.maxChunkSize);
- return;
- }
- setChunkSize(value);
- // Auto-adjust overlap if it exceeds 50% of new chunk size
- if (chunkOverlap > value * 0.5) {
- setChunkOverlap(Math.floor(value * 0.5));
- }
- };
- // Handle overlap size change
- const handleChunkOverlapChange = (value: number) => {
- if (limits && value > limits.maxOverlapSize) {
- showWarning(t('maxValueMsg', limits.maxOverlapSize));
- setChunkOverlap(limits.maxOverlapSize);
- return;
- }
- // Check if it exceeds 50% of chunk size
- const maxOverlapByRatio = Math.floor(chunkSize * 0.5);
- if (value > maxOverlapByRatio) {
- showWarning(t('overlapRatioLimit', maxOverlapByRatio));
- setChunkOverlap(maxOverlapByRatio);
- return;
- }
- setChunkOverlap(value);
- };
- // Render limits info
- const renderLimitsInfo = () => {
- if (!limits || isLoadingLimits) {
- return null;
- }
- return (
- <div className="bg-blue-50 border border-blue-200 rounded-lg p-3 text-xs">
- <div className="flex items-center gap-2 mb-2 font-semibold text-blue-800">
- <Info className="w-4 h-4" />
- {t('modelLimitsInfo')}
- </div>
- <div className="grid grid-cols-2 gap-2 text-blue-700">
- <div>{t('model')}: <span className="font-medium">{limits.modelInfo.name}</span></div>
- <div>{t('maxChunkSize')}: <span className="font-medium">{limits.maxChunkSize} tokens</span></div>
- <div>{t('maxOverlapSize')}: <span className="font-medium">{limits.maxOverlapSize} tokens</span></div>
- <div>{t('maxBatchSize')}: <span className="font-medium">{limits.modelInfo.maxBatchSize}</span></div>
- </div>
- {limits.modelInfo.maxInputTokens > limits.maxChunkSize && (
- <div className="mt-1 text-blue-600 text-[10px]">
- ⚠️ {t('envLimitWeaker')}: {limits.maxChunkSize} < {limits.modelInfo.maxInputTokens}
- </div>
- )}
- </div>
- );
- };
- if (!isOpen) return null;
- return (
- <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">
- <div className="bg-white rounded-xl shadow-2xl w-full max-w-lg overflow-hidden flex flex-col max-h-[90vh]">
- {/* Header */}
- <div className="p-5 border-b border-slate-100 bg-slate-50">
- <div className="flex justify-between items-start">
- <div>
- <h2 className="text-lg font-bold text-slate-800 flex items-center gap-2">
- <Database className="w-5 h-5 text-blue-600" />
- {isReconfiguring ? t('reconfigureFile') : t('idxModalTitle')}
- </h2>
- <p className="text-xs text-slate-500 mt-1">
- {isReconfiguring ? t('modifySettings') : t('idxDesc')}
- </p>
- </div>
- <button onClick={onClose} className="p-1 hover:bg-slate-200 rounded-full transition-colors">
- <X className="w-5 h-5 text-slate-400" />
- </button>
- </div>
- </div>
- <div className="flex-1 overflow-y-auto p-5 space-y-6">
- {/* Pending Files */}
- <div>
- <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
- <Files className="w-4 h-4 text-slate-500" />
- {t('idxFiles')}
- </h3>
- <div className="space-y-1 max-h-32 overflow-y-auto bg-slate-50 rounded-lg p-2 border border-slate-200">
- {files.map((file, index) => (
- <div key={index} className="text-xs text-slate-600 flex items-center justify-between py-1 px-2 hover:bg-white rounded transition-colors">
- <span className="truncate flex-1">{file.name}</span>
- <span className="text-slate-400 ml-2">{formatBytes(file.size)}</span>
- </div>
- ))}
- </div>
- </div>
- {/* Embedding Model Selection */}
- <div>
- <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
- <Layers className="w-4 h-4 text-slate-500" />
- {t('idxEmbeddingModel')}
- </h3>
- <select
- className="w-full text-sm border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 outline-none"
- value={selectedEmbedding}
- onChange={(e) => setSelectedEmbedding(e.target.value)}
- >
- <option value="">{t('pleaseSelect')}</option>
- {embeddingModels.map(model => (
- <option key={model.id} value={model.id}>
- {model.name} ({model.modelId})
- </option>
- ))}
- </select>
- </div>
- {/* Chunk Configuration */}
- <div>
- <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
- <FileText className="w-4 h-4 text-slate-500" />
- {t('idxMethod')}
- </h3>
- <div className="space-y-3">
- {/* Chunk Size */}
- <div>
- <div className="flex justify-between mb-1 text-xs">
- <span className="text-slate-600">{t('chunkSize')}</span>
- <span className="font-mono font-semibold text-blue-600">{chunkSize}</span>
- </div>
- <input
- type="range"
- min="50"
- max={limits?.maxChunkSize || 8191}
- value={chunkSize}
- onChange={(e) => handleChunkSizeChange(Number(e.target.value))}
- className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
- disabled={!selectedEmbedding || isLoadingLimits}
- />
- <div className="flex justify-between text-[10px] text-slate-400 mt-1">
- <span>{t('min')}: 50</span>
- <span>{t('max')}: {limits?.maxChunkSize || '—'}</span>
- </div>
- </div>
- {/* Overlap Size */}
- <div>
- <div className="flex justify-between mb-1 text-xs">
- <span className="text-slate-600">{t('chunkOverlap')}</span>
- <span className="font-mono font-semibold text-blue-600">{chunkOverlap}</span>
- </div>
- <input
- type="range"
- min="0"
- max={limits?.maxOverlapSize || 200}
- value={chunkOverlap}
- onChange={(e) => handleChunkOverlapChange(Number(e.target.value))}
- className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
- disabled={!selectedEmbedding || isLoadingLimits}
- />
- <div className="flex justify-between text-[10px] text-slate-400 mt-1">
- <span>{t('min')}: 0</span>
- <span>{t('max')}: {limits?.maxOverlapSize || '—'}</span>
- </div>
- </div>
- </div>
- </div>
- {/* Model Limits Info */}
- {renderLimitsInfo()}
- {/* Optimization Tips */}
- {limits && (
- <div className="bg-amber-50 border border-amber-200 rounded-lg p-3 text-xs text-amber-800">
- <p className="font-medium mb-1">💡 {t('optimizationTips')}</p>
- <ul className="list-disc list-inside space-y-0.5 text-[11px]">
- {chunkSize > 800 && <li>{t('tipChunkTooLarge')}</li>}
- {chunkOverlap < chunkSize * 0.1 && <li>{t('tipOverlapSmall').replace('$1', String(Math.floor(chunkSize * 0.1)))}</li>}
- {chunkSize === limits.maxChunkSize && <li>{t('tipMaxValues')}</li>}
- </ul>
- </div>
- )}
- </div>
- {/* Footer Buttons */}
- <div className="p-4 border-t border-slate-100 bg-slate-50 flex justify-end gap-2">
- <button
- onClick={onClose}
- className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-lg transition-colors"
- >
- {t('idxCancel')}
- </button>
- <button
- onClick={() => {
- if (!selectedEmbedding) {
- showWarning(t('selectEmbeddingFirst'));
- return;
- }
- onConfirm({
- chunkSize,
- chunkOverlap,
- embeddingModelId: selectedEmbedding
- });
- }}
- disabled={!selectedEmbedding || isLoadingLimits}
- 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"
- >
- <Database className="w-4 h-4" />
- {t('idxStart')}
- </button>
- </div>
- </div>
- </div>
- );
- };
- export default IndexingModal;
|