| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { apiClient } from './apiClient';
- import { IndexingConfig } from '../types';
- // web/services/uploadService.ts
- export const uploadService = {
- async uploadFile(file: File, authToken: string): Promise<any> {
- const formData = new FormData();
- formData.append('file', file);
- const response = await apiClient.request('/upload', {
- method: 'POST',
- body: formData,
- });
- if (!response.ok) {
- const errorData = await response.json();
- throw new Error(errorData.message || 'fileUploadFailed');
- }
- return response.json();
- },
- async uploadFileWithConfig(file: File, config: IndexingConfig, authToken: string): Promise<any> {
- const formData = new FormData();
- formData.append('file', file);
- formData.append('chunkSize', config.chunkSize.toString());
- formData.append('chunkOverlap', config.chunkOverlap.toString());
- formData.append('embeddingModelId', config.embeddingModelId);
-
- if (config.mode) {
- formData.append('mode', config.mode);
- }
-
- if (config.groupIds && config.groupIds.length > 0) {
- formData.append('groupIds', JSON.stringify(config.groupIds));
- }
- const response = await apiClient.request('/upload', {
- method: 'POST',
- body: formData,
- });
- if (!response.ok) {
- const errorData = await response.json();
- throw new Error(errorData.message || 'fileUploadFailed');
- }
- return response.json();
- },
- async uploadText(content: string, title: string, config: IndexingConfig, authToken: string): Promise<any> {
- const { data } = await apiClient.post('/upload/text', {
- content,
- title,
- chunkSize: config.chunkSize.toString(),
- chunkOverlap: config.chunkOverlap.toString(),
- embeddingModelId: config.embeddingModelId,
- mode: config.mode
- });
- return data;
- },
-
- async recommendMode(file: File): Promise<any> {
-
- if (!file || !file.name) {
- return {
- recommendedMode: 'fast',
- reason: 'invalidFile',
- warnings: ['incompleteFileInfo'],
- };
- }
-
- const ext = file.name.toLowerCase().split('.').pop();
- const sizeMB = file.size / (1024 * 1024);
- const preciseFormats = ['pdf', 'doc', 'docx', 'ppt', 'pptx'];
- const supportedFormats = [...preciseFormats, 'xls', 'xlsx', 'txt', 'md', 'html', 'json', 'csv'];
- if (!supportedFormats.includes(ext || '')) {
- return {
- recommendedMode: 'fast',
- reason: `unsupportedFileFormat`,
- reasonArgs: [ext],
- warnings: ['willUseFastMode'],
- };
- }
- if (!preciseFormats.includes(ext || '')) {
- return {
- recommendedMode: 'fast',
- reason: `formatNoPrecise`,
- reasonArgs: [ext],
- warnings: ['willUseFastMode'],
- };
- }
-
- if (sizeMB < 5) {
- return {
- recommendedMode: 'fast',
- reason: 'smallFileFastOk',
- estimatedCost: 0,
- estimatedTime: sizeMB * 2,
- warnings: [],
- };
- }
-
- if (sizeMB < 50) {
- return {
- recommendedMode: 'precise',
- reason: 'mixedContentPreciseRecommended',
- estimatedCost: Math.max(0.01, sizeMB * 0.01),
- estimatedTime: sizeMB * 8,
- warnings: ['willIncurApiCost'],
- };
- }
-
- return {
- recommendedMode: 'precise',
- reason: 'largeFilePreciseRecommended',
- estimatedCost: sizeMB * 0.015,
- estimatedTime: sizeMB * 12,
- warnings: ['longProcessingTime', 'highApiCost', 'considerFileSplitting'],
- };
- },
- };
|