缓解模型流式卡死导致站点偶发无响应

This commit is contained in:
Your Name 2026-02-08 23:47:17 +08:00
parent 5361f7fc29
commit c4ca7e4282

View File

@ -994,6 +994,7 @@ export async function POST(request: Request) {
// 获取语言模型实例 // 获取语言模型实例
let languageModel; let languageModel;
let actualModelId: string; let actualModelId: string;
let resolvedModelSupportsImageGen = false;
if (isUUID) { if (isUUID) {
// 从数据库获取动态配置的模型 // 从数据库获取动态配置的模型
@ -1055,6 +1056,7 @@ export async function POST(request: Request) {
// 对于动态模型,我们需要从数据库获取 modelId // 对于动态模型,我们需要从数据库获取 modelId
const finalModelData = await getAiModelById({ id: targetModelId }); const finalModelData = await getAiModelById({ id: targetModelId });
actualModelId = finalModelData?.modelId ?? targetModelId; actualModelId = finalModelData?.modelId ?? targetModelId;
resolvedModelSupportsImageGen = Boolean(finalModelData?.supportsImageGen);
} else { } else {
// 使用预定义的模型 // 使用预定义的模型
languageModel = myProvider.languageModel(finalModelId); languageModel = myProvider.languageModel(finalModelId);
@ -2456,6 +2458,10 @@ export async function POST(request: Request) {
} }
// 常规文本模型使用 streamText // 常规文本模型使用 streamText
const streamRequestTimeoutMs = resolvedModelSupportsImageGen ? 180_000 : 90_000;
const shouldDisableSmoothStream =
resolvedModelSupportsImageGen || isVolcanoVideoModel;
const result = streamText({ const result = streamText({
model: languageModel, model: languageModel,
system: systemPrompt({ selectedChatModel: finalModelId, requestHints }), system: systemPrompt({ selectedChatModel: finalModelId, requestHints }),
@ -2463,8 +2469,11 @@ export async function POST(request: Request) {
stopWhen: stepCountIs(5), stopWhen: stepCountIs(5),
experimental_activeTools: experimental_activeTools:
finalModelId === "chat-model-reasoning" || isVolcanoVideoModel ? [] : activeTools, finalModelId === "chat-model-reasoning" || isVolcanoVideoModel ? [] : activeTools,
experimental_transform: smoothStream({ chunking: "word" }), experimental_transform: shouldDisableSmoothStream
? undefined
: smoothStream({ chunking: "word" }),
tools: isVolcanoVideoModel ? {} : tools, // 🆕 视频模型禁用工具 tools: isVolcanoVideoModel ? {} : tools, // 🆕 视频模型禁用工具
abortSignal: AbortSignal.timeout(streamRequestTimeoutMs),
experimental_telemetry: { experimental_telemetry: {
isEnabled: isProductionEnvironment, isEnabled: isProductionEnvironment,
functionId: "stream-text", functionId: "stream-text",