π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { SHARE_UPLOAD_STORAGE_BLOCK_PREFIX, SHARE_VISITOR_MAX_FILE_SIZE } from '@lobechat/const';
import { shareChatService } from '@/services/shareChat';
import { uploadService } from '@/services/upload';
import type { UploadFileItem } from '@/types/files';
import { getAudioDuration } from '@/utils/client/audioDuration';
import { getImageDimensions } from '@/utils/client/imageDimensions';
import { normalizeUploadedFileType } from '../upload/action';
import { audioMimeFromExtension } from './uploadGuard';
type OnStatusUpdate = (data: {
id: string;
type: 'updateFile';
value: Partial<UploadFileItem>;
}) => void;
/** Storage refused the bytes β the share's upload cap or the creator's quota (`shareChat.createUploadUrl`). */
export const isShareStorageBlockError = (error: unknown) =>
error instanceof Error && error.message.startsWith(SHARE_UPLOAD_STORAGE_BLOCK_PREFIX);
export const isShareFileTooLarge = (file: File) => file.size > SHARE_VISITOR_MAX_FILE_SIZE;
/**
* Upload one chat attachment as an agent-share VISITOR.
*
* The share counterpart of `uploadWithProgress`: same client-side prep
* (type sniffing, image dimensions, audio duration) so the attachment renders
* exactly like an owner upload, but the storage reservation, the file row and
* any cleanup go through the share-scoped endpoints β the file lands under the
* CREATOR's account and quota, which the visitor's own `upload.*` / `file.*`
* procedures could never write to.
*
* Deliberately no content hash: no `checkFileHash` dedup (that lookup is the
* visitor's own global-file view, and a share file must always be a fresh
* creator-owned row with share provenance) and none sent to the server either
* β `shareChat.createFile` keeps share rows out of the hash-keyed global-file
* graph so each upload's object is deleted with its row.
*/
export const uploadShareVisitorFile = async ({
abortController,
file,
onStatusUpdate,
shareId,
uploadId,
}: {
abortController?: AbortController;
file: File;
onStatusUpdate?: OnStatusUpdate;
shareId: string;
/** Id of the chip this upload reports to; defaults to the file name. */
uploadId?: string;
}): Promise<{ id: string; url: string }> => {
const statusId = uploadId ?? file.name;
const { detectedMimeType, file: normalizedFile } = await normalizeUploadedFileType(file);
const extensionAudioMime = audioMimeFromExtension(normalizedFile.name);
let fileType = normalizedFile.type || detectedMimeType || 'text/plain';
if (extensionAudioMime && !fileType.startsWith('audio/')) fileType = extensionAudioMime;
const [dimensions, durationMs] = await Promise.all([
getImageDimensions(normalizedFile),
fileType.startsWith('audio/')
? getAudioDuration(normalizedFile).catch(() => undefined)
: undefined,
]);
const { pathname, url } = await shareChatService.createUploadUrl(shareId, {
name: normalizedFile.name,
size: normalizedFile.size,
});
try {
await uploadService.uploadToPresignedUrl(normalizedFile, url, {
abortController,
onProgress: (status, uploadState) => {
onStatusUpdate?.({
id: statusId,
type: 'updateFile',
value: { status: status === 'success' ? 'processing' : status, uploadState },
});
},
});
const data = await shareChatService.createFile({
fileType,
metadata: {
...dimensions,
...(durationMs === undefined ? {} : { durationMs }),
},
name: normalizedFile.name,
pathname,
shareId,
size: normalizedFile.size,
});
onStatusUpdate?.({
id: statusId,
type: 'updateFile',
value: {
...(dimensions && { dimensions }),
fileType,
fileUrl: data.url,
id: data.id,
status: 'success',
uploadState: { progress: 100, restTime: 0, speed: 0 },
},
});
return data;
} catch (error) {
await shareChatService.abortUpload(shareId, pathname);
throw error;
}
};