mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-21 11:43:56 +00:00

* feat: rewrite chat context (#3176) * feat: add app auto execute (#3115) * feat: add app auto execute * auto exec configtion * chatting animation * change icon * fix * fix * fix link * feat: add chat context to all chatbox * perf: loading ui --------- Co-authored-by: heheer <heheer@sealos.io> * app auto exec (#3179) * add chat records loaded state (#3184) * perf: chat store reset storage (#3186) * perf: chat store reset storage * perf: auto exec code * chore: workflow ui (#3175) * chore: workflow ui * fix * change icon color config * change popover to mymenu * 4.8.14 test (#3189) * update doc * fix: token check * perf: icon button * update doc * feat: share page support configuration Whether to allow the original view (#3194) * update doc * perf: fix index (#3206) * perf: i18n * perf: Add service entry (#3226) * 4.8.14 test (#3228) * fix: ai log * fix: text splitter * fix: reference unselect & user form description & simple to advance (#3229) * fix: reference unselect & user form description & simple to advance * change abort position * perf * perf: code (#3232) * perf: code * update doc * fix: create btn permission (#3233) * update doc * fix: refresh chatbox listener * perf: check invalid reference * perf: check invalid reference * update doc * fix: ui props --------- Co-authored-by: heheer <heheer@sealos.io>
258 lines
6.2 KiB
TypeScript
258 lines
6.2 KiB
TypeScript
import { Types, connectionMongo, ReadPreference } from '../../mongo';
|
|
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
|
import fsp from 'fs/promises';
|
|
import fs from 'fs';
|
|
import { DatasetFileSchema } from '@fastgpt/global/core/dataset/type';
|
|
import { MongoChatFileSchema, MongoDatasetFileSchema } from './schema';
|
|
import { detectFileEncoding } from '@fastgpt/global/common/file/tools';
|
|
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
|
|
import { MongoRawTextBuffer } from '../../buffer/rawText/schema';
|
|
import { readRawContentByFileBuffer } from '../read/utils';
|
|
import { gridFsStream2Buffer, stream2Encoding } from './utils';
|
|
import { addLog } from '../../system/log';
|
|
import { readFromSecondary } from '../../mongo/utils';
|
|
import { parseFileExtensionFromUrl } from '@fastgpt/global/common/string/tools';
|
|
import { Readable } from 'stream';
|
|
|
|
export function getGFSCollection(bucket: `${BucketNameEnum}`) {
|
|
MongoDatasetFileSchema;
|
|
MongoChatFileSchema;
|
|
|
|
return connectionMongo.connection.db.collection(`${bucket}.files`);
|
|
}
|
|
export function getGridBucket(bucket: `${BucketNameEnum}`) {
|
|
return new connectionMongo.mongo.GridFSBucket(connectionMongo.connection.db, {
|
|
bucketName: bucket,
|
|
// @ts-ignore
|
|
readPreference: ReadPreference.SECONDARY_PREFERRED // Read from secondary node
|
|
});
|
|
}
|
|
|
|
/* crud file */
|
|
export async function uploadFile({
|
|
bucketName,
|
|
teamId,
|
|
uid,
|
|
path,
|
|
filename,
|
|
contentType,
|
|
encoding,
|
|
metadata = {}
|
|
}: {
|
|
bucketName: `${BucketNameEnum}`;
|
|
teamId: string;
|
|
uid: string; // tmbId / outLinkUId
|
|
path: string;
|
|
filename: string;
|
|
contentType?: string;
|
|
encoding: string;
|
|
metadata?: Record<string, any>;
|
|
}) {
|
|
if (!path) return Promise.reject(`filePath is empty`);
|
|
if (!filename) return Promise.reject(`filename is empty`);
|
|
|
|
const stats = await fsp.stat(path);
|
|
if (!stats.isFile()) return Promise.reject(`${path} is not a file`);
|
|
|
|
const readStream = fs.createReadStream(path);
|
|
|
|
// Add default metadata
|
|
metadata.teamId = teamId;
|
|
metadata.uid = uid;
|
|
metadata.encoding = encoding;
|
|
|
|
// create a gridfs bucket
|
|
const bucket = getGridBucket(bucketName);
|
|
|
|
const stream = bucket.openUploadStream(filename, {
|
|
metadata,
|
|
contentType
|
|
});
|
|
|
|
// save to gridfs
|
|
await new Promise((resolve, reject) => {
|
|
readStream
|
|
.pipe(stream as any)
|
|
.on('finish', resolve)
|
|
.on('error', reject);
|
|
});
|
|
|
|
return String(stream.id);
|
|
}
|
|
export async function uploadFileFromBase64Img({
|
|
bucketName,
|
|
teamId,
|
|
tmbId,
|
|
base64,
|
|
filename,
|
|
metadata = {}
|
|
}: {
|
|
bucketName: `${BucketNameEnum}`;
|
|
teamId: string;
|
|
tmbId: string;
|
|
base64: string;
|
|
filename: string;
|
|
metadata?: Record<string, any>;
|
|
}) {
|
|
if (!base64) return Promise.reject(`filePath is empty`);
|
|
if (!filename) return Promise.reject(`filename is empty`);
|
|
|
|
const base64Data = base64.split(',')[1];
|
|
const contentType = base64.split(',')?.[0]?.split?.(':')?.[1];
|
|
const buffer = Buffer.from(base64Data, 'base64');
|
|
const readableStream = new Readable({
|
|
read() {
|
|
this.push(buffer);
|
|
this.push(null);
|
|
}
|
|
});
|
|
|
|
const { stream: readStream, encoding } = await stream2Encoding(readableStream);
|
|
|
|
// Add default metadata
|
|
metadata.teamId = teamId;
|
|
metadata.tmbId = tmbId;
|
|
metadata.encoding = encoding;
|
|
|
|
// create a gridfs bucket
|
|
const bucket = getGridBucket(bucketName);
|
|
|
|
const stream = bucket.openUploadStream(filename, {
|
|
metadata,
|
|
contentType
|
|
});
|
|
|
|
// save to gridfs
|
|
await new Promise((resolve, reject) => {
|
|
readStream
|
|
.pipe(stream as any)
|
|
.on('finish', resolve)
|
|
.on('error', reject);
|
|
});
|
|
|
|
return String(stream.id);
|
|
}
|
|
|
|
export async function getFileById({
|
|
bucketName,
|
|
fileId
|
|
}: {
|
|
bucketName: `${BucketNameEnum}`;
|
|
fileId: string;
|
|
}) {
|
|
const db = getGFSCollection(bucketName);
|
|
const file = await db.findOne<DatasetFileSchema>({
|
|
_id: new Types.ObjectId(fileId)
|
|
});
|
|
|
|
// if (!file) {
|
|
// return Promise.reject('File not found');
|
|
// }
|
|
|
|
return file || undefined;
|
|
}
|
|
|
|
export async function delFileByFileIdList({
|
|
bucketName,
|
|
fileIdList,
|
|
retry = 3
|
|
}: {
|
|
bucketName: `${BucketNameEnum}`;
|
|
fileIdList: string[];
|
|
retry?: number;
|
|
}): Promise<any> {
|
|
try {
|
|
const bucket = getGridBucket(bucketName);
|
|
|
|
for await (const fileId of fileIdList) {
|
|
await bucket.delete(new Types.ObjectId(fileId));
|
|
}
|
|
} catch (error) {
|
|
if (retry > 0) {
|
|
return delFileByFileIdList({ bucketName, fileIdList, retry: retry - 1 });
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function getDownloadStream({
|
|
bucketName,
|
|
fileId
|
|
}: {
|
|
bucketName: `${BucketNameEnum}`;
|
|
fileId: string;
|
|
}) {
|
|
const bucket = getGridBucket(bucketName);
|
|
|
|
return bucket.openDownloadStream(new Types.ObjectId(fileId));
|
|
}
|
|
|
|
export const readFileContentFromMongo = async ({
|
|
teamId,
|
|
bucketName,
|
|
fileId,
|
|
isQAImport = false
|
|
}: {
|
|
teamId: string;
|
|
bucketName: `${BucketNameEnum}`;
|
|
fileId: string;
|
|
isQAImport?: boolean;
|
|
}): Promise<{
|
|
rawText: string;
|
|
filename: string;
|
|
}> => {
|
|
// read buffer
|
|
const fileBuffer = await MongoRawTextBuffer.findOne({ sourceId: fileId }, undefined, {
|
|
...readFromSecondary
|
|
}).lean();
|
|
if (fileBuffer) {
|
|
return {
|
|
rawText: fileBuffer.rawText,
|
|
filename: fileBuffer.metadata?.filename || ''
|
|
};
|
|
}
|
|
|
|
const [file, fileStream] = await Promise.all([
|
|
getFileById({ bucketName, fileId }),
|
|
getDownloadStream({ bucketName, fileId })
|
|
]);
|
|
if (!file) {
|
|
return Promise.reject(CommonErrEnum.fileNotFound);
|
|
}
|
|
|
|
const extension = parseFileExtensionFromUrl(file?.filename);
|
|
|
|
const start = Date.now();
|
|
const fileBuffers = await gridFsStream2Buffer(fileStream);
|
|
addLog.debug('get file buffer', { time: Date.now() - start });
|
|
|
|
const encoding = file?.metadata?.encoding || detectFileEncoding(fileBuffers);
|
|
|
|
// Get raw text
|
|
const { rawText } = await readRawContentByFileBuffer({
|
|
extension,
|
|
isQAImport,
|
|
teamId,
|
|
buffer: fileBuffers,
|
|
encoding,
|
|
metadata: {
|
|
relatedId: fileId
|
|
}
|
|
});
|
|
|
|
// < 14M
|
|
if (fileBuffers.length < 14 * 1024 * 1024 && rawText.trim()) {
|
|
MongoRawTextBuffer.create({
|
|
sourceId: fileId,
|
|
rawText,
|
|
metadata: {
|
|
filename: file.filename
|
|
}
|
|
});
|
|
}
|
|
|
|
return {
|
|
rawText,
|
|
filename: file.filename
|
|
};
|
|
};
|