Add configurable timeout calculation for transfers

Introduce timeout configuration and automatic timeout estimation for uploads/downloads. Added TimeoutConfig to OneBot config with baseTimeout, upload/downloadSpeedKBps and maxTimeout, plus calculateTimeout and capTimeout helpers. Applied estimation to FileApi.downloadFileById calls (GetFile, BaseDownloadStream) and to message sending (OneBotMsgApi) to compute timeouts from total payload size; sendMsg now accepts an optional timeout override which is capped by maxTimeout. Updated SendMsg payload schema to allow a custom timeout, and updated web UI store, types and config form to expose timeout settings and defaults.
This commit is contained in:
手瓜一十雪 committed 2026-03-12 15:01:34 +08:00
1 parent df0795f2c5
commit e9b74138d1
8 files changed
+173 -28

No files matched your search

@@ -4,6 +4,7 @@ import { FileNapCatOneBotUUID } from 'napcat-common/src/file-uuid';
import { ActionName } from '@/napcat-onebot/action/router';
import { OB11MessageImage, OB11MessageVideo } from '@/napcat-onebot/types';
import { Static, Type } from '@sinclair/typebox';
import { calculateTimeout } from '@/napcat-onebot/config/config';
import { FileActionsExamples } from '../example/FileActionsExamples';
@@ -96,7 +97,10 @@ export class GetFileBase extends OneBotAction<GetFilePayload, GetFileResponse> {
// 搜索名字模式
const searchResult = (await this.core.apis.FileApi.searchForFile([payload.file]));
if (searchResult) {
const downloadPath = await this.core.apis.FileApi.downloadFileById(searchResult.id, parseInt(searchResult.fileSize));
const fileSize = parseInt(searchResult.fileSize);
const timeoutConfig = this.obContext.configLoader.configData.timeout;
const estimatedTime = calculateTimeout(timeoutConfig, fileSize, timeoutConfig.downloadSpeedKBps);
const downloadPath = await this.core.apis.FileApi.downloadFileById(searchResult.id, fileSize, estimatedTime);
const res: GetFileResponse = {
file: downloadPath,
url: downloadPath,
+2 -1
View File
@@ -33,6 +33,7 @@ export const SendMsgPayloadSchema = Type.Object({
news: Type.Optional(Type.Array(Type.Object({ text: Type.String() }), { description: '合并转发新闻' })),
summary: Type.Optional(Type.String({ description: '合并转发摘要' })),
prompt: Type.Optional(Type.String({ description: '合并转发提示' })),
timeout: Type.Optional(Type.Number({ description: '自定义发送超时(毫秒),覆盖自动计算值' })),
});
export type SendMsgPayload = Static<typeof SendMsgPayloadSchema>;
@@ -245,7 +246,7 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
const { sendElements, deleteAfterSentFiles } = await this.obContext.apis.MsgApi
.createSendElements(messages, peer);
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles);
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, payload.timeout);
return { message_id: returnMsg.id! };
}
@@ -2,6 +2,7 @@ import { OneBotAction, OneBotRequestToolkit } from '@/napcat-onebot/action/OneBo
import { StreamPacket, StreamStatus } from './StreamBasic';
import fs from 'fs';
import { FileNapCatOneBotUUID } from 'napcat-common/src/file-uuid';
import { calculateTimeout } from '@/napcat-onebot/config/config';
export interface ResolvedFileInfo {
downloadPath: string;
@@ -64,8 +65,11 @@ export abstract class BaseDownloadStream<PayloadType, ResultType> extends OneBot
const searchResult = (await this.core.apis.FileApi.searchForFile([target]));
if (searchResult) {
downloadPath = await this.core.apis.FileApi.downloadFileById(searchResult.id, parseInt(searchResult.fileSize));
fileSize = parseInt(searchResult.fileSize);
const fileSizeNum = parseInt(searchResult.fileSize);
const timeoutConfig = this.obContext.configLoader.configData.timeout;
const estimatedTime = calculateTimeout(timeoutConfig, fileSizeNum, timeoutConfig.downloadSpeedKBps);
downloadPath = await this.core.apis.FileApi.downloadFileById(searchResult.id, fileSizeNum, estimatedTime);
fileSize = fileSizeNum;
fileName = searchResult.fileName;
return { downloadPath, fileName, fileSize };
}