fix(core): handle group file storage limits

Retry eligible storage failures as temporary files and preserve their storage type for later use. Reject incompatible mixed content before chat publication.

Refs #644
This commit is contained in:
DrSmoothl committed 2026-11-07 15:14:55 +08:00
1 parent 84f4cde85e
commit 8bea681aa2
16 files changed
+750 -104

No files matched your search

+5 -5
View File
@@ -4,17 +4,17 @@ import { RETCODE, failedResponse, okResponse } from '../types';
// NapCat/LLOneBot omit `busid` from their `get_group_file_url` schema entirely,
// so whatever value a client tacks on is silently ignored. Mirror that here:
// accept any value, coerce when it's a usable non-negative integer, else fall
// back to the canonical 102. Restores the legacy `asNumber(busid) || 102`
// back to the uploaded file's known storage type (otherwise 102). Retains the
// semantics that a strict `f.int({min:0}).default(102)` regressed (a *present*
// non-numeric busid was rejected with `expected an integer`). See issue #147.
function busidOr102(raw: unknown): number {
function optionalBusId(raw: unknown): number | undefined {
const n =
typeof raw === 'number' && Number.isFinite(raw)
? Math.trunc(raw)
: typeof raw === 'string' && raw.trim() !== '' && Number.isFinite(Number(raw))
? Math.trunc(Number(raw))
: NaN;
return Number.isInteger(n) && n >= 0 ? n : 102;
return Number.isInteger(n) && n >= 0 ? n : undefined;
}
export const actions = [
@@ -159,7 +159,7 @@ export const actions = [
properties: { url: { type: 'string', description: '文件下载直链' } },
required: ['url'],
},
// busid is accepted but tolerant (see busidOr102): NapCat/LLBot ignore it,
// busid is accepted but tolerant (see optionalBusId): NapCat/LLBot ignore it,
// so a present null/empty/non-numeric value must not 400 the request (#147).
params: { file_id: f.fileId(), busid: f.raw() },
run: async (p, ctx) => {
@@ -167,7 +167,7 @@ export const actions = [
url: await ctx.bridge.apis.groupFile.getUrl(
p.group_id,
p.file_id,
busidOr102(p.busid),
optionalBusId(p.busid),
),
});
},
+78 -31
View File
@@ -1409,9 +1409,8 @@ export async function sendGroupMessage(
throw new Error('message is empty');
}
// Two sub-paths for group file segments:
// a) has url/path but no file_id → upload() which internally calls publish()
// b) has file_id from a prior upload_group_file → publish() only
// Prepare all file uploads before publishing any part of the message.
// Whether storage is temporary is only known after the upload preflight.
let allFileElements = elements.filter((e) => e.type === 'file');
let nonFileElements = elements.filter((e) => e.type !== 'file');
@@ -1426,6 +1425,73 @@ export async function sendGroupMessage(
}
}
let allowTemporary = nonFileElements.every((element) => element.type === 'image');
const assertStorageAllowed = (busId: number | undefined) => {
if (busId === 104 && !allowTemporary) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'temporary group files require only file or image segments; remove text and other content before retrying',
'file',
);
}
};
const preparedFiles: Array<{
element: MessageElementOf<'file'>;
fileId: string;
busId?: number;
}> = [];
const prepareFiles = async (files: Array<MessageElementOf<'file'>>) => {
// Check known temporary ids first, even if preceded by a new upload.
const candidates = files.map((element) => {
const cached = element.fileId
? ref.bridge.recallUploadedFile(element.fileId)
: undefined;
const busId =
cached?.scope === 'group' && cached.groupId === groupId ? cached.busId : undefined;
assertStorageAllowed(busId);
return { element, fileId: element.fileId, busId };
});
for (const candidate of candidates) {
const { element } = candidate;
if (!candidate.fileId) {
if (!element.url) {
throw new MessageElementValidationError(
'MISSING_FIELD',
'group file segment requires file_id or url',
'file',
);
}
const name = element.fileName || guessFileNameFromUrl(element.url) || 'file';
const uploaded = await ref.bridge.apis.groupFile.upload(
groupId,
element.url,
name,
'/',
true,
false,
{ allowTemporary },
);
candidate.fileId = uploaded.fileId;
candidate.busId = uploaded.busId;
assertStorageAllowed(candidate.busId);
}
if (!candidate.fileId) {
throw new Error('group file auto-upload returned no file_id');
}
preparedFiles.push({ element, fileId: candidate.fileId, busId: candidate.busId });
}
};
await prepareFiles(allFileElements);
let lastReceipt: Awaited<ReturnType<typeof ref.bridge.apis.message.sendGroup>> | undefined;
if (nonFileElements.length > 0) {
@@ -1447,8 +1513,9 @@ export async function sendGroupMessage(
err instanceof Error ? err.message : String(err),
);
const { fileEls, remaining } = splitVideoFileFallback(nonFileElements, isSizeErr);
allFileElements = [...allFileElements, ...fileEls];
nonFileElements = remaining;
allowTemporary = remaining.every((element) => element.type === 'image');
await prepareFiles(fileEls);
if (nonFileElements.length > 0) {
lastReceipt = await ref.bridge.apis.message.sendGroup(groupId, nonFileElements);
@@ -1457,34 +1524,14 @@ export async function sendGroupMessage(
}
}
for (const fileEl of allFileElements) {
let fileId: string;
if (fileEl.url && !fileEl.fileId) {
// upload() already calls publish() internally — do NOT call publish() again.
const name = fileEl.fileName || guessFileNameFromUrl(fileEl.url) || 'file';
const result = await ref.bridge.apis.groupFile.upload(
groupId,
fileEl.url,
name,
'/',
true,
for (const { element: fileEl, fileId, busId } of preparedFiles) {
try {
await ref.bridge.apis.groupFile.publish(groupId, fileId, busId);
} catch (err) {
throw new Error(
`group file chat post failed (fileId=${fileId}): ${err instanceof Error ? err.message : String(err)}`,
{ cause: err },
);
fileId = result.fileId ?? '';
if (!fileId) {
throw new Error('group file auto-upload returned no file_id');
}
} else if (fileEl.fileId) {
fileId = fileEl.fileId;
await ref.bridge.apis.groupFile.publish(groupId, fileId);
} else {
throw new MessageElementValidationError(
'MISSING_FIELD',
'group file segment requires file_id or url',
'file',
);
continue;
}
logSentMessage(true, groupId, [fileEl]);