mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-05 12:29:44 +08:00
* refactor(FileManager): streamline file path handling in FilesPage and ImageBlock components * refactor(file): implement getSafeFilePath utility for consistent file path handling across loaders and preprocessors * refactor(FileStorage): replace getSafeFilePath with fileStorage.getFilePathById for consistent file path retrieval across services * refactor(file): unify file path retrieval across loaders and preprocessors for improved consistency * refactor(Inputbar, MessageEditor): replace getFileExtension with file.ext for improved file type handling * refactor(FileStorage): simplify getFilePathById method by removing redundant checks for file path retrieval * fix(FileStorage): update getFilePathById to ensure file.path is consistent with generated filePath * refactor(FileStorage): simplify getFilePathById method by removing unnecessary file path consistency checks * fix(FileStorage): update duplicate file check to use file.path for accurate detection * fix(FileStorage): correct file path usage in uploadFile method for accurate duplicate detection * fix(loader): update file path retrieval to use file.path for consistency across loaders
108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import fs from 'node:fs/promises'
|
|
|
|
import { loggerService } from '@logger'
|
|
import { fileStorage } from '@main/services/FileStorage'
|
|
import { Mistral } from '@mistralai/mistralai'
|
|
import { FileListResponse, FileMetadata, FileUploadResponse, Provider } from '@types'
|
|
|
|
import { MistralClientManager } from '../MistralClientManager'
|
|
import { BaseFileService } from './BaseFileService'
|
|
|
|
const logger = loggerService.withContext('MistralService')
|
|
|
|
export class MistralService extends BaseFileService {
|
|
private readonly client: Mistral
|
|
|
|
constructor(provider: Provider) {
|
|
super(provider)
|
|
const clientManager = MistralClientManager.getInstance()
|
|
clientManager.initializeClient(provider)
|
|
this.client = clientManager.getClient()
|
|
}
|
|
|
|
async uploadFile(file: FileMetadata): Promise<FileUploadResponse> {
|
|
try {
|
|
const fileBuffer = await fs.readFile(fileStorage.getFilePathById(file))
|
|
const response = await this.client.files.upload({
|
|
file: {
|
|
fileName: file.origin_name,
|
|
content: new Uint8Array(fileBuffer)
|
|
},
|
|
purpose: 'ocr'
|
|
})
|
|
|
|
return {
|
|
fileId: response.id,
|
|
displayName: file.origin_name,
|
|
status: 'success',
|
|
originalFile: {
|
|
type: 'mistral',
|
|
file: response
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error('Error uploading file:', error as Error)
|
|
return {
|
|
fileId: '',
|
|
displayName: file.origin_name,
|
|
status: 'failed'
|
|
}
|
|
}
|
|
}
|
|
|
|
async listFiles(): Promise<FileListResponse> {
|
|
try {
|
|
const response = await this.client.files.list({})
|
|
return {
|
|
files: response.data.map((file) => ({
|
|
id: file.id,
|
|
displayName: file.filename || '',
|
|
size: file.sizeBytes,
|
|
status: 'success', // All listed files are processed,
|
|
originalFile: {
|
|
type: 'mistral',
|
|
file
|
|
}
|
|
}))
|
|
}
|
|
} catch (error) {
|
|
logger.error('Error listing files:', error as Error)
|
|
return { files: [] }
|
|
}
|
|
}
|
|
|
|
async deleteFile(fileId: string): Promise<void> {
|
|
try {
|
|
await this.client.files.delete({
|
|
fileId
|
|
})
|
|
logger.debug(`File ${fileId} deleted`)
|
|
} catch (error) {
|
|
logger.error('Error deleting file:', error as Error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async retrieveFile(fileId: string): Promise<FileUploadResponse> {
|
|
try {
|
|
const response = await this.client.files.retrieve({
|
|
fileId
|
|
})
|
|
|
|
return {
|
|
fileId: response.id,
|
|
displayName: response.filename || '',
|
|
status: 'success' // Retrieved files are always processed
|
|
}
|
|
} catch (error) {
|
|
logger.error('Error retrieving file:', error as Error)
|
|
return {
|
|
fileId: fileId,
|
|
displayName: '',
|
|
status: 'failed',
|
|
originalFile: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|