Compare commits
1 Commits
main
...
codex/part
| Author | SHA1 | Date | |
|---|---|---|---|
| 31bba5aac9 |
@ -1,11 +1,10 @@
|
|||||||
import { isExplicitDestructiveIntent } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
import { isExplicitDestructiveIntent } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
||||||
import { inferVideoTaskRequirements } from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
import { inferVideoTaskRequirements } from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
import { geolocation } from "@vercel/functions";
|
import { geolocation } from "@vercel/functions";
|
||||||
import {
|
import {
|
||||||
convertToModelMessages,
|
convertToModelMessages,
|
||||||
createUIMessageStream as createAiUIMessageStream,
|
createUIMessageStream,
|
||||||
generateText,
|
generateText,
|
||||||
smoothStream,
|
smoothStream,
|
||||||
stepCountIs,
|
stepCountIs,
|
||||||
@ -427,8 +426,6 @@ import {
|
|||||||
} from "./_lib/video-models";
|
} from "./_lib/video-models";
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.route");
|
const chatApiLogger = createLogger("chat.route");
|
||||||
const createUIMessageStream: typeof createAiUIMessageStream = (options) =>
|
|
||||||
createAiUIMessageStream({ ...options, execute: (writer) => trackRuntimeWork(() => options.execute(writer)) });
|
|
||||||
|
|
||||||
const INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN =
|
const INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN =
|
||||||
/生成图片失败|图片生成失败|未找到图片数据|未解析到图片|模型未返回生成图片|图片数据不可用|未返回可用图片/i;
|
/生成图片失败|图片生成失败|未找到图片数据|未解析到图片|模型未返回生成图片|图片数据不可用|未返回可用图片/i;
|
||||||
@ -446,13 +443,13 @@ function assertGeneratedImageMarkdownHasImage(markdown: string, modelLabel: stri
|
|||||||
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPublicImageGenerationErrorText(error: unknown, requestId: string) {
|
function getPublicImageGenerationErrorText(error: unknown) {
|
||||||
const message = error instanceof Error ? error.message : String(error || "");
|
const message = error instanceof Error ? error.message : String(error || "");
|
||||||
const publicMessage = getUserFacingImageGenerationErrorMessage(error);
|
const publicMessage = getUserFacingImageGenerationErrorMessage(error);
|
||||||
if (!message || (publicMessage === message && INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN.test(message))) {
|
if (!message || (publicMessage === message && INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN.test(message))) {
|
||||||
return `图片生成失败,请稍后重试。若连续失败,请联系管理员。\n\n请求编号:${requestId}`;
|
return "图片生成失败,请稍后重试";
|
||||||
}
|
}
|
||||||
return `${publicMessage}\n\n请求编号:${requestId}`;
|
return publicMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -9633,10 +9630,9 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
// 如果故障转移也失败,返回错误消息
|
// 如果故障转移也失败,返回错误消息
|
||||||
const errorMessageId = generateUUID();
|
const errorMessageId = generateUUID();
|
||||||
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
|
||||||
writeAssistantTextChunk(
|
writeAssistantTextChunk(
|
||||||
writer,
|
writer,
|
||||||
errorText,
|
"图片生成失败,请稍后重试",
|
||||||
"gemini:error",
|
"gemini:error",
|
||||||
errorMessageId,
|
errorMessageId,
|
||||||
);
|
);
|
||||||
@ -9656,7 +9652,7 @@ export async function POST(request: Request) {
|
|||||||
id: errorMessageId,
|
id: errorMessageId,
|
||||||
chatId: id,
|
chatId: id,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
parts: [{ type: "text", text: errorText }],
|
parts: [{ type: "text", text: "图片生成失败,请稍后重试" }],
|
||||||
attachments: [],
|
attachments: [],
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
videoStorageStatus: null,
|
videoStorageStatus: null,
|
||||||
@ -10336,7 +10332,7 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
// 返回错误消息
|
// 返回错误消息
|
||||||
const errorMessageId = generateUUID();
|
const errorMessageId = generateUUID();
|
||||||
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
const errorText = getPublicImageGenerationErrorText(error);
|
||||||
writeAssistantTextChunk(
|
writeAssistantTextChunk(
|
||||||
writer,
|
writer,
|
||||||
errorText,
|
errorText,
|
||||||
@ -10776,10 +10772,9 @@ export async function POST(request: Request) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
||||||
const errorMessageId = generateUUID();
|
const errorMessageId = generateUUID();
|
||||||
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
|
||||||
writeAssistantTextChunk(
|
writeAssistantTextChunk(
|
||||||
writer,
|
writer,
|
||||||
errorText,
|
"图片生成失败,请稍后重试",
|
||||||
"volcano:error",
|
"volcano:error",
|
||||||
errorMessageId,
|
errorMessageId,
|
||||||
);
|
);
|
||||||
@ -10799,7 +10794,7 @@ export async function POST(request: Request) {
|
|||||||
id: errorMessageId,
|
id: errorMessageId,
|
||||||
chatId: id,
|
chatId: id,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
parts: [{ type: "text", text: errorText }],
|
parts: [{ type: "text", text: "图片生成失败,请稍后重试" }],
|
||||||
attachments: [], // 必须提供 attachments 字段
|
attachments: [], // 必须提供 attachments 字段
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
videoStorageStatus: null,
|
videoStorageStatus: null,
|
||||||
@ -11707,7 +11702,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
||||||
|
|
||||||
const errorMessageId = generateUUID();
|
const errorMessageId = generateUUID();
|
||||||
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
const errorText = getPublicImageGenerationErrorText(error);
|
||||||
|
|
||||||
dataStream.write({
|
dataStream.write({
|
||||||
type: "data-appendMessage",
|
type: "data-appendMessage",
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry"
|
|||||||
import { executeCanvasCapabilityRuntime } from "@/lib/canvas/capabilities/runtime";
|
import { executeCanvasCapabilityRuntime } from "@/lib/canvas/capabilities/runtime";
|
||||||
import { getCanvasCapabilityTask } from "@/lib/canvas/capabilities/task-store";
|
import { getCanvasCapabilityTask } from "@/lib/canvas/capabilities/task-store";
|
||||||
import { CanvasAgentMutationError } from "@/lib/canvas/agent-mutations";
|
import { CanvasAgentMutationError } from "@/lib/canvas/agent-mutations";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const maxDuration = 600;
|
export const maxDuration = 600;
|
||||||
@ -40,8 +39,7 @@ export async function GET(
|
|||||||
{ status: 404 }
|
{ status: 404 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const failure = task.status === "failed" ? getRequestFailureDetails(task.error) : undefined;
|
return NextResponse.json({ success: true, task });
|
||||||
return NextResponse.json({ success: true, task: { ...task, failure, error: failure ? formatRequestFailure(failure, task.id) : task.error } });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
@ -53,11 +51,9 @@ export async function POST(
|
|||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
let requestId: string | undefined;
|
|
||||||
try {
|
try {
|
||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
const command = canvasCapabilityClientCommandSchema.parse(await request.json());
|
const command = canvasCapabilityClientCommandSchema.parse(await request.json());
|
||||||
requestId = command.requestId;
|
|
||||||
const result = await executeCanvasCapabilityRuntime({
|
const result = await executeCanvasCapabilityRuntime({
|
||||||
command,
|
command,
|
||||||
projectId: id,
|
projectId: id,
|
||||||
@ -71,9 +67,8 @@ export async function POST(
|
|||||||
error instanceof CanvasCapabilityRuntimeError ||
|
error instanceof CanvasCapabilityRuntimeError ||
|
||||||
error instanceof CanvasAgentMutationError
|
error instanceof CanvasAgentMutationError
|
||||||
) {
|
) {
|
||||||
const failure = getRequestFailureDetails(error);
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: formatRequestFailure(failure, requestId), code: error.code, failure, requestId },
|
{ success: false, error: error.message, code: error.code },
|
||||||
{ status: error.status }
|
{ status: error.status }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -87,9 +82,9 @@ export async function POST(
|
|||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const failure = getRequestFailureDetails(error, "执行画布能力失败");
|
const message = error instanceof Error ? error.message : "执行画布能力失败";
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: formatRequestFailure(failure, requestId), failure, requestId },
|
{ success: false, error: message },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { executeCanvasImageResponse } from "@/lib/canvas/generate-image-response
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getCanvasImageTaskForUser } from "@/lib/canvas/image-task-queue";
|
import { getCanvasImageTaskForUser } from "@/lib/canvas/image-task-queue";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
export const maxDuration = 1200;
|
||||||
|
|
||||||
@ -24,15 +23,13 @@ export async function GET(request: Request) {
|
|||||||
return NextResponse.json({ success: false, error: "任务不存在" }, { status: 404 });
|
return NextResponse.json({ success: false, error: "任务不存在" }, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const failure = task.status === "failed" ? getRequestFailureDetails(task.error) : undefined;
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
task: {
|
task: {
|
||||||
id: task.id,
|
id: task.id,
|
||||||
status: task.status,
|
status: task.status,
|
||||||
attempts: task.attempts,
|
attempts: task.attempts,
|
||||||
error: failure ? formatRequestFailure(failure, task.id) : null,
|
error: task.error || null,
|
||||||
failure,
|
|
||||||
result: task.result || null,
|
result: task.result || null,
|
||||||
createdAt: task.createdAt,
|
createdAt: task.createdAt,
|
||||||
startedAt: task.startedAt,
|
startedAt: task.startedAt,
|
||||||
|
|||||||
@ -66,7 +66,6 @@ import {
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { useCanvasStore } from "@/store/canvas";
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
|
||||||
import { fetcher, fetchWithErrorHandlers, generateUUID } from "@/lib/utils";
|
import { fetcher, fetchWithErrorHandlers, generateUUID } from "@/lib/utils";
|
||||||
import {
|
import {
|
||||||
fetchUploadWithRetry,
|
fetchUploadWithRetry,
|
||||||
@ -374,9 +373,7 @@ function MarkdownResponseFallback({
|
|||||||
}: Pick<ResponseProps, "children" | "className">) {
|
}: Pick<ResponseProps, "children" | "className">) {
|
||||||
return (
|
return (
|
||||||
<div className={cn("whitespace-pre-wrap break-words [overflow-wrap:anywhere]", className)}>
|
<div className={cn("whitespace-pre-wrap break-words [overflow-wrap:anywhere]", className)}>
|
||||||
{typeof children === "string"
|
{children}
|
||||||
? getGatewayErrorPageMessage(children) || children
|
|
||||||
: children}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1024,77 +1024,6 @@ function getConfiguredPollSeconds(config: Vip98kModelRuntimeConfig, key: string,
|
|||||||
return Math.min(Math.max(value, 1), 3600);
|
return Math.min(Math.max(value, 1), 3600);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSynchronousImagePath(path: string) {
|
|
||||||
return /^\/v1\/images\/(?:generations|edits)\/?$/.test(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function recoverImageSubmission(params: {
|
|
||||||
apiKey: string;
|
|
||||||
baseURL: string;
|
|
||||||
modelId: string;
|
|
||||||
path: string;
|
|
||||||
idempotencyKey: string;
|
|
||||||
pollIntervalMs: number;
|
|
||||||
maxPollMs: number;
|
|
||||||
}) {
|
|
||||||
if (!isSynchronousImagePath(params.path)) return null;
|
|
||||||
const startedAt = Date.now();
|
|
||||||
const deadline = startedAt + params.maxPollMs;
|
|
||||||
logger.info("recovering original image request", { modelId: params.modelId });
|
|
||||||
|
|
||||||
while (Date.now() < deadline) {
|
|
||||||
try {
|
|
||||||
const lookup = await fetch(`${params.baseURL}/api/external/requests/by-idempotency-key`, {
|
|
||||||
cache: "no-store",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${params.apiKey}`,
|
|
||||||
"Idempotency-Key": params.idempotencyKey,
|
|
||||||
},
|
|
||||||
signal: AbortSignal.timeout(Math.min(60_000, deadline - Date.now())),
|
|
||||||
});
|
|
||||||
if ([401, 403, 404, 410].includes(lookup.status)) return null;
|
|
||||||
if (lookup.ok) {
|
|
||||||
const state = toRecord(await parseJsonResponse(lookup));
|
|
||||||
if (!state || !["processing", "succeeded"].includes(String(state.status))) return null;
|
|
||||||
if (state.status === "succeeded") {
|
|
||||||
if (state.result_available !== true || !state.request_id) return null;
|
|
||||||
const result = await fetch(
|
|
||||||
`${params.baseURL}/api/external/requests/${encodeURIComponent(String(state.request_id))}/result`,
|
|
||||||
{
|
|
||||||
cache: "no-store",
|
|
||||||
headers: { Authorization: `Bearer ${params.apiKey}` },
|
|
||||||
signal: AbortSignal.timeout(Math.max(1, Math.min(60_000, deadline - Date.now()))),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if ([401, 403, 404, 410].includes(result.status)) return null;
|
|
||||||
if (result.ok) {
|
|
||||||
const payload = await parseJsonResponse(result);
|
|
||||||
const outputs = collectOutputUrls(payload);
|
|
||||||
if (!outputs.length) {
|
|
||||||
throw new ProviderTaskUncertainError("原生图请求已完成,但未取回图片结果,请勿重复生成");
|
|
||||||
}
|
|
||||||
logger.info("original image request recovered", {
|
|
||||||
modelId: params.modelId,
|
|
||||||
providerRequestId: state.request_id,
|
|
||||||
outputCount: outputs.length,
|
|
||||||
durationMs: Date.now() - startedAt,
|
|
||||||
});
|
|
||||||
return { payload, taskId: extractTaskId(payload), resultPath: extractResultPath(payload), outputs };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof ProviderTaskUncertainError) throw error;
|
|
||||||
}
|
|
||||||
await delay(Math.max(0, Math.min(params.pollIntervalMs, deadline - Date.now())));
|
|
||||||
}
|
|
||||||
logger.warn("original image request recovery timed out", {
|
|
||||||
modelId: params.modelId,
|
|
||||||
durationMs: Date.now() - startedAt,
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function submitTask(params: {
|
async function submitTask(params: {
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
baseURL?: string | null;
|
baseURL?: string | null;
|
||||||
@ -1107,8 +1036,6 @@ async function submitTask(params: {
|
|||||||
submissionTimeoutMs: number;
|
submissionTimeoutMs: number;
|
||||||
maxSubmissionAttempts: number;
|
maxSubmissionAttempts: number;
|
||||||
submissionRetryBaseDelayMs: number;
|
submissionRetryBaseDelayMs: number;
|
||||||
pollIntervalMs: number;
|
|
||||||
maxPollMs: number;
|
|
||||||
}) {
|
}) {
|
||||||
const baseURL = normalizeVip98kBaseUrl(params.baseURL);
|
const baseURL = normalizeVip98kBaseUrl(params.baseURL);
|
||||||
const path = String(params.path || "/api/external/task/generate").trim();
|
const path = String(params.path || "/api/external/task/generate").trim();
|
||||||
@ -1172,9 +1099,7 @@ async function submitTask(params: {
|
|||||||
signal: AbortSignal.timeout(params.submissionTimeoutMs),
|
signal: AbortSignal.timeout(params.submissionTimeoutMs),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const recovered = await recoverImageSubmission({ ...params, baseURL, path, idempotencyKey });
|
if (attempt >= maxAttempts) {
|
||||||
if (recovered) return recovered;
|
|
||||||
if (isSynchronousImagePath(path) || attempt >= maxAttempts) {
|
|
||||||
logger.error("provider submission transport failed", {
|
logger.error("provider submission transport failed", {
|
||||||
modelId: params.modelId,
|
modelId: params.modelId,
|
||||||
path,
|
path,
|
||||||
@ -1220,14 +1145,6 @@ async function submitTask(params: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([502, 504, 524].includes(response.status)) {
|
|
||||||
const recovered = await recoverImageSubmission({ ...params, baseURL, path, idempotencyKey });
|
|
||||||
if (recovered) return recovered;
|
|
||||||
if (isSynchronousImagePath(path)) {
|
|
||||||
throw new ProviderTaskUncertainError(`供应商提交结果未确认,可能仍在处理,请勿重复生成:${getUserFacingImageGenerationErrorMessage({ message: extractErrorMessage(payload), status: response.status })} (HTTP ${response.status})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const errorMessage = extractErrorMessage(payload);
|
const errorMessage = extractErrorMessage(payload);
|
||||||
const stagedMediaRetryDelayMs = getVip98kStagedMediaRetryDelay({
|
const stagedMediaRetryDelayMs = getVip98kStagedMediaRetryDelay({
|
||||||
status: response.status,
|
status: response.status,
|
||||||
@ -1394,12 +1311,6 @@ export async function submitVip98kExternalTask(params: {
|
|||||||
),
|
),
|
||||||
maxSubmissionAttempts: params.durable ? 1 : runtimeConfig.maxSubmissionAttempts,
|
maxSubmissionAttempts: params.durable ? 1 : runtimeConfig.maxSubmissionAttempts,
|
||||||
submissionRetryBaseDelayMs: runtimeConfig.submissionRetryBaseDelayMs,
|
submissionRetryBaseDelayMs: runtimeConfig.submissionRetryBaseDelayMs,
|
||||||
pollIntervalMs: getConfiguredPollSeconds(
|
|
||||||
taskParams.modelConfig, "pollIntervalSeconds", DEFAULT_POLL_INTERVAL_SECONDS,
|
|
||||||
) * 1000,
|
|
||||||
maxPollMs: getConfiguredPollSeconds(
|
|
||||||
taskParams.modelConfig, "maxPollSeconds", DEFAULT_MAX_POLL_SECONDS,
|
|
||||||
) * 1000,
|
|
||||||
});
|
});
|
||||||
const resultPath = submitted.resultPath || invocation?.resultPath || "";
|
const resultPath = submitted.resultPath || invocation?.resultPath || "";
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { ProviderTaskUncertainError } from "@/lib/video-canvas/video-task-lifecycle";
|
|
||||||
import { isProviderAuthenticationFailure, isCredentialFailureBan } from "./authentication-failover";
|
import { isProviderAuthenticationFailure, isCredentialFailureBan } from "./authentication-failover";
|
||||||
|
|
||||||
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
||||||
@ -344,7 +343,7 @@ export function classifyAiModelHealthFailure(error: unknown): {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function shouldFailoverAiModelError(error: unknown) {
|
export function shouldFailoverAiModelError(error: unknown) {
|
||||||
if (error instanceof ProviderTaskUncertainError || isGatewayResponseTimeout(error)) return false;
|
if (isGatewayResponseTimeout(error)) return false;
|
||||||
const classification = classifyAiModelHealthFailure(error);
|
const classification = classifyAiModelHealthFailure(error);
|
||||||
const normalized = getErrorMessage(error).toLowerCase();
|
const normalized = getErrorMessage(error).toLowerCase();
|
||||||
const imageProviderTaskFailed =
|
const imageProviderTaskFailed =
|
||||||
|
|||||||
@ -14,16 +14,13 @@ import { getCanvasCapability } from "@/lib/canvas/capabilities/registry";
|
|||||||
import { enqueueCanvasCapabilityExecution } from "@/lib/canvas/capabilities/task-queue";
|
import { enqueueCanvasCapabilityExecution } from "@/lib/canvas/capabilities/task-queue";
|
||||||
import {
|
import {
|
||||||
createOrRecoverCanvasCapabilityTask,
|
createOrRecoverCanvasCapabilityTask,
|
||||||
getCanvasCapabilityTask,
|
MAX_CANVAS_CAPABILITY_TASK_ATTEMPTS,
|
||||||
heartbeatCanvasCapabilityTask,
|
|
||||||
markCanvasCapabilityTaskFailed,
|
markCanvasCapabilityTaskFailed,
|
||||||
markCanvasCapabilityTaskRunning,
|
markCanvasCapabilityTaskRunning,
|
||||||
markCanvasCapabilityTaskSucceeded,
|
markCanvasCapabilityTaskSucceeded,
|
||||||
} from "@/lib/canvas/capabilities/task-store";
|
} from "@/lib/canvas/capabilities/task-store";
|
||||||
import { applyCanvasOperationPlan } from "@/lib/canvas/agent-mutations";
|
import { applyCanvasOperationPlan } from "@/lib/canvas/agent-mutations";
|
||||||
import type { CanvasOperationPlan } from "@/types/canvas/agent-protocol";
|
import type { CanvasOperationPlan } from "@/types/canvas/agent-protocol";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
|
|
||||||
export type CanvasCapabilityRuntimeResult = {
|
export type CanvasCapabilityRuntimeResult = {
|
||||||
clientCommand?: CanvasCapabilityClientCommand;
|
clientCommand?: CanvasCapabilityClientCommand;
|
||||||
@ -61,11 +58,9 @@ function serializeTask(task: {
|
|||||||
status: string;
|
status: string;
|
||||||
version: number;
|
version: number;
|
||||||
}) {
|
}) {
|
||||||
const failure = task.status === "failed" ? getRequestFailureDetails(task.error) : undefined;
|
|
||||||
return {
|
return {
|
||||||
attempts: task.attempts,
|
attempts: task.attempts,
|
||||||
error: failure ? formatRequestFailure(failure, task.id) : task.error,
|
error: task.error,
|
||||||
failure,
|
|
||||||
id: task.id,
|
id: task.id,
|
||||||
requestId: task.requestId,
|
requestId: task.requestId,
|
||||||
result: task.result,
|
result: task.result,
|
||||||
@ -74,13 +69,6 @@ function serializeTask(task: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function storedTaskResult(task: Parameters<typeof serializeTask>[0], mode: "headless" | "interactive"): CanvasCapabilityRuntimeResult {
|
|
||||||
const result = task.status === "succeeded" && task.result && typeof task.result === "object"
|
|
||||||
? task.result as Omit<CanvasCapabilityRuntimeResult, "task">
|
|
||||||
: { mode };
|
|
||||||
return { ...result, task: serializeTask(task) };
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildMediaOperationPlan(params: {
|
function buildMediaOperationPlan(params: {
|
||||||
command: CanvasCapabilityClientCommand;
|
command: CanvasCapabilityClientCommand;
|
||||||
mediaResults: CanvasCapabilityMediaResult[];
|
mediaResults: CanvasCapabilityMediaResult[];
|
||||||
@ -223,7 +211,11 @@ export async function executeCanvasCapabilityRuntime(params: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (task.status === "succeeded") {
|
if (task.status === "succeeded") {
|
||||||
return storedTaskResult(task, "headless");
|
const storedResult =
|
||||||
|
task.result && typeof task.result === "object"
|
||||||
|
? (task.result as Omit<CanvasCapabilityRuntimeResult, "task">)
|
||||||
|
: ({ mode: "headless" } as const);
|
||||||
|
return { ...storedResult, task: serializeTask(task) };
|
||||||
}
|
}
|
||||||
if (task.status === "running") {
|
if (task.status === "running") {
|
||||||
return {
|
return {
|
||||||
@ -233,8 +225,15 @@ export async function executeCanvasCapabilityRuntime(params: {
|
|||||||
task: serializeTask(task),
|
task: serializeTask(task),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (task.status === "failed") {
|
if (
|
||||||
return { mode: "headless", task: serializeTask(task) };
|
task.status === "failed" &&
|
||||||
|
task.attempts >= MAX_CANVAS_CAPABILITY_TASK_ATTEMPTS
|
||||||
|
) {
|
||||||
|
throw new CanvasCapabilityRuntimeError(
|
||||||
|
task.error || "画布能力任务超过最大重试次数",
|
||||||
|
409,
|
||||||
|
"CANVAS_CAPABILITY_RETRY_EXHAUSTED"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const shouldRunHeadlessly = shouldExecuteCanvasCapabilityHeadlessly({
|
const shouldRunHeadlessly = shouldExecuteCanvasCapabilityHeadlessly({
|
||||||
@ -242,9 +241,7 @@ export async function executeCanvasCapabilityRuntime(params: {
|
|||||||
command,
|
command,
|
||||||
});
|
});
|
||||||
if (!shouldRunHeadlessly) {
|
if (!shouldRunHeadlessly) {
|
||||||
const claimed = await markCanvasCapabilityTaskRunning(task.id);
|
task = await markCanvasCapabilityTaskRunning(task.id);
|
||||||
if (!claimed) return storedTaskResult((await getCanvasCapabilityTask({ requestId: command.requestId, userId: params.userId, projectId: params.projectId })) || task, "interactive");
|
|
||||||
task = claimed;
|
|
||||||
const result = {
|
const result = {
|
||||||
mode: "interactive" as const,
|
mode: "interactive" as const,
|
||||||
clientCommand: command,
|
clientCommand: command,
|
||||||
@ -253,18 +250,10 @@ export async function executeCanvasCapabilityRuntime(params: {
|
|||||||
return { ...result, task: serializeTask(task) };
|
return { ...result, task: serializeTask(task) };
|
||||||
}
|
}
|
||||||
|
|
||||||
const heartbeat = setInterval(() => {
|
return enqueueCanvasCapabilityExecution({
|
||||||
void heartbeatCanvasCapabilityTask(task.id).catch((error) => {
|
|
||||||
console.warn("[Canvas Capability] Task heartbeat failed:", error);
|
|
||||||
});
|
|
||||||
}, 30_000);
|
|
||||||
try {
|
|
||||||
return await trackRuntimeWork(() => enqueueCanvasCapabilityExecution({
|
|
||||||
projectId: params.projectId,
|
projectId: params.projectId,
|
||||||
execute: async () => {
|
execute: async () => {
|
||||||
const claimed = await markCanvasCapabilityTaskRunning(task.id);
|
task = await markCanvasCapabilityTaskRunning(task.id);
|
||||||
if (!claimed) return storedTaskResult((await getCanvasCapabilityTask({ requestId: command.requestId, userId: params.userId, projectId: params.projectId })) || task, "headless");
|
|
||||||
task = claimed;
|
|
||||||
try {
|
try {
|
||||||
const projectRevision = context.project.updatedAt.toISOString();
|
const projectRevision = context.project.updatedAt.toISOString();
|
||||||
const mutationPlan = buildMutationOperationPlan({
|
const mutationPlan = buildMutationOperationPlan({
|
||||||
@ -310,8 +299,5 @@ export async function executeCanvasCapabilityRuntime(params: {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
});
|
||||||
} finally {
|
|
||||||
clearInterval(heartbeat);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import { and, eq, inArray, lt, sql } from "drizzle-orm";
|
import { and, eq, lt, sql } from "drizzle-orm";
|
||||||
|
|
||||||
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { canvasCapabilityTask } from "@/lib/db/schema";
|
import { canvasCapabilityTask } from "@/lib/db/schema";
|
||||||
|
|
||||||
const STALE_RUNNING_TASK_MS = 15 * 60 * 1000;
|
const STALE_RUNNING_TASK_MS = 15 * 60 * 1000;
|
||||||
const INTERRUPTED_TASK_MESSAGE = "任务执行中断,结果尚未确认。请先查看画布和历史记录,或联系管理员核对原任务,避免重复处理。";
|
export const MAX_CANVAS_CAPABILITY_TASK_ATTEMPTS = 3;
|
||||||
|
|
||||||
export type CanvasCapabilityTaskResult = Record<string, unknown>;
|
export type CanvasCapabilityTaskResult = Record<string, unknown>;
|
||||||
|
|
||||||
@ -25,25 +25,7 @@ export async function getCanvasCapabilityTask(params: {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (!task) return null;
|
return task || null;
|
||||||
const staleBefore = new Date(Date.now() - STALE_RUNNING_TASK_MS);
|
|
||||||
if (["pending", "running"].includes(task.status) && task.updatedAt < staleBefore) {
|
|
||||||
const [interrupted] = await db.update(canvasCapabilityTask).set({
|
|
||||||
status: "failed",
|
|
||||||
error: INTERRUPTED_TASK_MESSAGE,
|
|
||||||
completedAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
}).where(and(
|
|
||||||
eq(canvasCapabilityTask.id, task.id),
|
|
||||||
inArray(canvasCapabilityTask.status, ["pending", "running"]),
|
|
||||||
lt(canvasCapabilityTask.updatedAt, staleBefore),
|
|
||||||
)).returning();
|
|
||||||
if (interrupted) return interrupted;
|
|
||||||
const [latest] = await db.select().from(canvasCapabilityTask)
|
|
||||||
.where(eq(canvasCapabilityTask.id, task.id)).limit(1);
|
|
||||||
return latest || task;
|
|
||||||
}
|
|
||||||
return task;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createOrRecoverCanvasCapabilityTask(params: {
|
export async function createOrRecoverCanvasCapabilityTask(params: {
|
||||||
@ -60,7 +42,6 @@ export async function createOrRecoverCanvasCapabilityTask(params: {
|
|||||||
capabilityId: params.command.capabilityId,
|
capabilityId: params.command.capabilityId,
|
||||||
source: params.command.source,
|
source: params.command.source,
|
||||||
request: params.command,
|
request: params.command,
|
||||||
updatedAt: new Date(),
|
|
||||||
})
|
})
|
||||||
.onConflictDoNothing({ target: canvasCapabilityTask.requestId });
|
.onConflictDoNothing({ target: canvasCapabilityTask.requestId });
|
||||||
|
|
||||||
@ -69,17 +50,57 @@ export async function createOrRecoverCanvasCapabilityTask(params: {
|
|||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
projectId: params.projectId,
|
projectId: params.projectId,
|
||||||
};
|
};
|
||||||
const task = await getCanvasCapabilityTask(taskLookup);
|
let task = await getCanvasCapabilityTask(taskLookup);
|
||||||
if (!task) {
|
if (!task) {
|
||||||
throw new Error("无法创建画布能力任务");
|
throw new Error("无法创建画布能力任务");
|
||||||
}
|
}
|
||||||
|
|
||||||
return task;
|
const staleBefore = new Date(Date.now() - STALE_RUNNING_TASK_MS);
|
||||||
}
|
if (
|
||||||
|
task.status === "failed" &&
|
||||||
|
task.attempts < MAX_CANVAS_CAPABILITY_TASK_ATTEMPTS
|
||||||
|
) {
|
||||||
|
await db
|
||||||
|
.update(canvasCapabilityTask)
|
||||||
|
.set({
|
||||||
|
status: "pending",
|
||||||
|
error: null,
|
||||||
|
completedAt: null,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(canvasCapabilityTask.id, task.id));
|
||||||
|
} else if (
|
||||||
|
task.status === "running" &&
|
||||||
|
task.updatedAt < staleBefore &&
|
||||||
|
task.attempts < MAX_CANVAS_CAPABILITY_TASK_ATTEMPTS
|
||||||
|
) {
|
||||||
|
await db
|
||||||
|
.update(canvasCapabilityTask)
|
||||||
|
.set({
|
||||||
|
status: "pending",
|
||||||
|
error: "恢复超时的执行中任务",
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(canvasCapabilityTask.id, task.id),
|
||||||
|
lt(canvasCapabilityTask.updatedAt, staleBefore)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else if (task.status === "running" && task.updatedAt < staleBefore) {
|
||||||
|
await db
|
||||||
|
.update(canvasCapabilityTask)
|
||||||
|
.set({
|
||||||
|
status: "failed",
|
||||||
|
error: "画布能力任务超过最大重试次数",
|
||||||
|
completedAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(canvasCapabilityTask.id, task.id));
|
||||||
|
}
|
||||||
|
|
||||||
export async function heartbeatCanvasCapabilityTask(taskId: string) {
|
task = (await getCanvasCapabilityTask(taskLookup)) || task;
|
||||||
await db.update(canvasCapabilityTask).set({ updatedAt: new Date() })
|
return task;
|
||||||
.where(and(eq(canvasCapabilityTask.id, taskId), inArray(canvasCapabilityTask.status, ["pending", "running"])));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markCanvasCapabilityTaskRunning(taskId: string) {
|
export async function markCanvasCapabilityTaskRunning(taskId: string) {
|
||||||
@ -94,10 +115,11 @@ export async function markCanvasCapabilityTaskRunning(taskId: string) {
|
|||||||
error: null,
|
error: null,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
})
|
})
|
||||||
.where(and(eq(canvasCapabilityTask.id, taskId), eq(canvasCapabilityTask.status, "pending")))
|
.where(eq(canvasCapabilityTask.id, taskId))
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
return task || null;
|
if (!task) throw new Error("画布能力任务不存在");
|
||||||
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markCanvasCapabilityTaskSucceeded(
|
export async function markCanvasCapabilityTaskSucceeded(
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import { generateCanvasImage, type CanvasGenerateImageRequest } from "@/lib/canv
|
|||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
import { getImageToolCreditCost, type ImageToolCreditFeature } from "@/lib/points/config";
|
import { getImageToolCreditCost, type ImageToolCreditFeature } from "@/lib/points/config";
|
||||||
import { getUserFacingImageGenerationErrorMessage, getRequestFailureDetails } from "@/lib/request-error";
|
import { getUserFacingImageGenerationErrorMessage } from "@/lib/request-error";
|
||||||
|
|
||||||
interface GenerateImageRequest extends CanvasGenerateImageRequest {
|
interface GenerateImageRequest extends CanvasGenerateImageRequest {
|
||||||
async?: boolean;
|
async?: boolean;
|
||||||
@ -121,7 +121,7 @@ export async function executeCanvasImageResponse(body: GenerateImageRequest, use
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[Canvas Generate Image] Error:", error);
|
console.error("[Canvas Generate Image] Error:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: getUserFacingImageGenerationErrorMessage(error), failure: getRequestFailureDetails(error),
|
{ success: false, error: getUserFacingImageGenerationErrorMessage(error),
|
||||||
...(error instanceof ProviderTaskUncertainError ? { code: "PROVIDER_TASK_UNCERTAIN" } : {}) },
|
...(error instanceof ProviderTaskUncertainError ? { code: "PROVIDER_TASK_UNCERTAIN" } : {}) },
|
||||||
{ status: error instanceof ProviderTaskUncertainError ? 503 : 500 },
|
{ status: error instanceof ProviderTaskUncertainError ? 503 : 500 },
|
||||||
);
|
);
|
||||||
|
|||||||
@ -38,7 +38,6 @@ import {
|
|||||||
} from "@/lib/agents/detail-replacement-batch";
|
} from "@/lib/agents/detail-replacement-batch";
|
||||||
import { executeDetailReplacementBatch } from "@/lib/agents/detail-replacement-batch-runner";
|
import { executeDetailReplacementBatch } from "@/lib/agents/detail-replacement-batch-runner";
|
||||||
import { getUserFacingImageGenerationErrorMessage } from "@/lib/request-error";
|
import { getUserFacingImageGenerationErrorMessage } from "@/lib/request-error";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
|
|
||||||
type CanvasImageTaskStatus =
|
type CanvasImageTaskStatus =
|
||||||
| "waiting"
|
| "waiting"
|
||||||
@ -329,7 +328,7 @@ async function markStaleDatabaseRunningTasksFailed() {
|
|||||||
.update(canvasImageTask)
|
.update(canvasImageTask)
|
||||||
.set({
|
.set({
|
||||||
status: "failed",
|
status: "failed",
|
||||||
error: "图片任务执行中断或等待超时,结果尚未确认。请先查看原任务和历史记录,联系管理员核对,避免重复生成。",
|
error: "图片生成任务已超时,请重新发起生成",
|
||||||
completedAt,
|
completedAt,
|
||||||
updatedAt: completedAt,
|
updatedAt: completedAt,
|
||||||
})
|
})
|
||||||
@ -384,7 +383,7 @@ async function runSingleTask(entry: PendingTaskEntry) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const runningTimeoutMs = getCanvasImageRunningTimeoutMs(request);
|
const runningTimeoutMs = getCanvasImageRunningTimeoutMs(request);
|
||||||
const execution: Promise<CanvasImageTaskResult> = trackRuntimeWork<CanvasImageTaskResult>(() => isStyleProTaskRequest(request)
|
const execution: Promise<CanvasImageTaskResult> = isStyleProTaskRequest(request)
|
||||||
? executeStyleProTask({
|
? executeStyleProTask({
|
||||||
request,
|
request,
|
||||||
userId: String(task.userId || entry.userId),
|
userId: String(task.userId || entry.userId),
|
||||||
@ -413,7 +412,7 @@ async function runSingleTask(entry: PendingTaskEntry) {
|
|||||||
ownerKey,
|
ownerKey,
|
||||||
request: imageRequest,
|
request: imageRequest,
|
||||||
});
|
});
|
||||||
})());
|
})();
|
||||||
const heartbeat = isDetailReplacementBatchRequest(request)
|
const heartbeat = isDetailReplacementBatchRequest(request)
|
||||||
? setInterval(() => {
|
? setInterval(() => {
|
||||||
void db.update(canvasImageTask).set({ updatedAt: new Date() })
|
void db.update(canvasImageTask).set({ updatedAt: new Date() })
|
||||||
@ -427,7 +426,7 @@ async function runSingleTask(entry: PendingTaskEntry) {
|
|||||||
try {
|
try {
|
||||||
result = await withCanvasImageTaskExecutionTimeout(
|
result = await withCanvasImageTaskExecutionTimeout(
|
||||||
execution,
|
execution,
|
||||||
Math.max(runningTimeoutMs, LOCAL_ACTIVE_RUNNING_HARD_TIMEOUT_MS),
|
runningTimeoutMs,
|
||||||
entry.taskId
|
entry.taskId
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
@ -752,7 +751,7 @@ export async function getCanvasImageTaskForUser(params: {
|
|||||||
|
|
||||||
if (shouldMarkStale) {
|
if (shouldMarkStale) {
|
||||||
const staleBefore = new Date(now - runningTimeoutMs);
|
const staleBefore = new Date(now - runningTimeoutMs);
|
||||||
const timeoutMessage = "图片任务执行中断或等待超时,结果尚未确认。请先查看原任务和历史记录,联系管理员核对,避免重复生成。";
|
const timeoutMessage = "图片生成任务已超时,请重新发起生成";
|
||||||
const completedAt = new Date();
|
const completedAt = new Date();
|
||||||
const [timedOutTask] = await db
|
const [timedOutTask] = await db
|
||||||
.update(canvasImageTask)
|
.update(canvasImageTask)
|
||||||
|
|||||||
@ -1,40 +1,6 @@
|
|||||||
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
||||||
import { mergeCanvasSnapshots } from "@/lib/video-canvas/merge-snapshot";
|
import { mergeCanvasSnapshots } from "@/lib/video-canvas/merge-snapshot";
|
||||||
import { useCanvasStore } from "@/store/canvas";
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
async function requestCapabilityResult(command: CanvasCapabilityClientCommand) {
|
|
||||||
const endpoint = `/api/canvas/${command.projectId}/capabilities`;
|
|
||||||
let payload: any = null;
|
|
||||||
try {
|
|
||||||
const response = await fetch(endpoint, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(command),
|
|
||||||
});
|
|
||||||
payload = await response.json().catch(() => null);
|
|
||||||
if (payload?.success === false || (!response.ok && ![502, 503, 504, 524].includes(response.status))) {
|
|
||||||
throw Object.assign(new Error(payload?.error || "图片处理请求未通过"), { confirmedRejection: true });
|
|
||||||
}
|
|
||||||
if (!response.ok) payload = null;
|
|
||||||
} catch (error) {
|
|
||||||
if ((error as { confirmedRejection?: boolean })?.confirmedRejection) throw error;
|
|
||||||
}
|
|
||||||
const deadline = Date.now() + 20 * 60 * 1000;
|
|
||||||
while (Date.now() < deadline) {
|
|
||||||
const task = payload?.task;
|
|
||||||
if (task?.status === "succeeded") return payload;
|
|
||||||
if (task?.status === "failed") throw new Error(task.error || "图片处理失败,请联系管理员核对任务结果");
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${endpoint}?requestId=${encodeURIComponent(command.requestId)}`, { cache: "no-store", signal: AbortSignal.timeout(12_000) });
|
|
||||||
payload = response.ok ? await response.json() : null;
|
|
||||||
} catch {
|
|
||||||
payload = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error(formatRequestFailure(getRequestFailureDetails({ providerOutcomeUnknown: true }), command.requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Runs after the editor has saved and while its save queue is reserved. */
|
/** Runs after the editor has saved and while its save queue is reserved. */
|
||||||
export async function executeCanvasToolbarCapability(
|
export async function executeCanvasToolbarCapability(
|
||||||
@ -44,7 +10,15 @@ export async function executeCanvasToolbarCapability(
|
|||||||
const before = useCanvasStore.getState();
|
const before = useCanvasStore.getState();
|
||||||
if (before.projectId !== projectId) throw new Error("画布已切换,请重新选择图片");
|
if (before.projectId !== projectId) throw new Error("画布已切换,请重新选择图片");
|
||||||
const base = JSON.stringify({ canvasData: before.getCanvasState() });
|
const base = JSON.stringify({ canvasData: before.getCanvasState() });
|
||||||
await requestCapabilityResult(command);
|
const response = await fetch(`/api/canvas/${projectId}/capabilities`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(command),
|
||||||
|
});
|
||||||
|
const payload = await response.json().catch(() => ({}));
|
||||||
|
if (!response.ok || payload?.success !== true) {
|
||||||
|
throw new Error(payload?.error || "图片处理失败");
|
||||||
|
}
|
||||||
|
|
||||||
const projectResponse = await fetch(`/api/canvas/${projectId}`);
|
const projectResponse = await fetch(`/api/canvas/${projectId}`);
|
||||||
const result = await projectResponse.json();
|
const result = await projectResponse.json();
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { preparePendingImageDownloads } from "@/lib/storage/image-download-preparation";
|
import { preparePendingImageDownloads } from "@/lib/storage/image-download-preparation";
|
||||||
import "server-only";
|
import "server-only";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
|
|
||||||
import { archiveTemporaryImage } from "@/lib/storage/image-archive";
|
import { archiveTemporaryImage } from "@/lib/storage/image-archive";
|
||||||
import {
|
import {
|
||||||
@ -27,7 +26,6 @@ function getBatchSize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function runImageArchiveJob() {
|
export async function runImageArchiveJob() {
|
||||||
return trackRuntimeWork(async () => {
|
|
||||||
const batchSize = getBatchSize();
|
const batchSize = getBatchSize();
|
||||||
console.log(`[ImageArchiveJob] Start image archive job (batchSize=${batchSize})`);
|
console.log(`[ImageArchiveJob] Start image archive job (batchSize=${batchSize})`);
|
||||||
|
|
||||||
@ -127,5 +125,4 @@ export async function runImageArchiveJob() {
|
|||||||
downloadTasks,
|
downloadTasks,
|
||||||
pendingDownloads: await hasPendingImageDownloads(),
|
pendingDownloads: await hasPendingImageDownloads(),
|
||||||
};
|
};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
import "server-only";
|
|
||||||
import { unlinkSync, writeFileSync } from "node:fs";
|
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
const state = globalThis as typeof globalThis & { __aiChatbotActiveWork?: number };
|
|
||||||
const path = join(process.platform === "win32" ? tmpdir() : "/tmp", `ai-chatbot-active-work-${process.pid}.json`);
|
|
||||||
|
|
||||||
function update(delta: number) {
|
|
||||||
state.__aiChatbotActiveWork = (state.__aiChatbotActiveWork || 0) + delta;
|
|
||||||
try {
|
|
||||||
writeFileSync(path, JSON.stringify({ pid: process.pid, active: state.__aiChatbotActiveWork }), { mode: 0o600 });
|
|
||||||
} catch (error) {
|
|
||||||
try { unlinkSync(path); } catch {}
|
|
||||||
console.warn("[Active Work] Failed to record running work:", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Includes generation that continues after the client connection closes. */
|
|
||||||
export async function trackRuntimeWork<T>(execute: () => T | Promise<T>): Promise<T> {
|
|
||||||
update(1);
|
|
||||||
try { return await execute(); } finally { update(-1); }
|
|
||||||
}
|
|
||||||
@ -189,6 +189,14 @@ export function isGatewayResponseTimeout(error: unknown): boolean {
|
|||||||
/\bHTTP\s*524\b|\b524\s*:\s*A timeout occurred\b|Error code\s*524\b/i.test(getRequestErrorText(error));
|
/\bHTTP\s*524\b|\b524\s*:\s*A timeout occurred\b|Error code\s*524\b/i.test(getRequestErrorText(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPublicGatewayErrorMessage(error: unknown): string | null {
|
||||||
|
if (isGatewayResponseTimeout(error)) {
|
||||||
|
return "模型服务响应超时(HTTP 524),生成结果尚未确认。请先查看历史记录,避免重复提交。";
|
||||||
|
}
|
||||||
|
const message = getRequestErrorMessage(error);
|
||||||
|
return message ? getGatewayErrorPageMessage(message) : null;
|
||||||
|
}
|
||||||
|
|
||||||
export function isProviderBalanceError(error: unknown): boolean {
|
export function isProviderBalanceError(error: unknown): boolean {
|
||||||
const message = getRequestErrorText(error);
|
const message = getRequestErrorText(error);
|
||||||
return PROVIDER_BALANCE_PATTERNS.some((pattern) => pattern.test(message));
|
return PROVIDER_BALANCE_PATTERNS.some((pattern) => pattern.test(message));
|
||||||
@ -213,77 +221,19 @@ export function isRateLimitError(error: unknown): boolean {
|
|||||||
RATE_LIMIT_PATTERNS.some((pattern) => pattern.test(message));
|
RATE_LIMIT_PATTERNS.some((pattern) => pattern.test(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RequestFailureDetails = {
|
|
||||||
code: string;
|
|
||||||
message: string;
|
|
||||||
recommendation: string;
|
|
||||||
outcome: "failed" | "unknown";
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getRequestFailureDetails(
|
|
||||||
error: unknown,
|
|
||||||
fallbackMessage = "请求未能完成",
|
|
||||||
): RequestFailureDetails {
|
|
||||||
const text = getRequestErrorText(error);
|
|
||||||
const status = getRequestErrorStatus(error);
|
|
||||||
const detail = (code: string, message: string, recommendation: string, outcome: "failed" | "unknown" = "failed") =>
|
|
||||||
({ code, message, recommendation, outcome });
|
|
||||||
if (isGatewayResponseTimeout(error)) {
|
|
||||||
return detail("RESULT_UNCONFIRMED", "模型服务响应超时(HTTP 524),生成结果尚未确认。", "请先查看历史记录,避免重复提交。", "unknown");
|
|
||||||
}
|
|
||||||
if ((isRecord(error) && error.providerOutcomeUnknown === true) || /结果(?:尚)?未确认|结果暂未确认|request_outcome_unknown|任务执行中断|may still be running/i.test(text)) {
|
|
||||||
return detail("RESULT_UNCONFIRMED", "任务结果尚未确认,可能仍在处理。", "请先查看原任务或历史记录;持续没有结果时联系管理员,避免重复生成。", "unknown");
|
|
||||||
}
|
|
||||||
if (isProviderBalanceError(error)) {
|
|
||||||
return detail("PROVIDER_BALANCE_EXHAUSTED", "图片服务商线路余额或额度不足,与您的积分余额无关。", "请切换可用模型或联系管理员处理线路额度。");
|
|
||||||
}
|
|
||||||
if (isRateLimitError(error)) {
|
|
||||||
return detail("RATE_LIMITED", "当前请求过于频繁。", "请稍后重试,避免连续点击;模型线路限流时可切换其他可用模型。");
|
|
||||||
}
|
|
||||||
if (/content.?polic|moderation|safety|policy_rejected|内容(?:敏感|审核|限制)|不能生成像那样|不能生成那样|违规/i.test(text)) {
|
|
||||||
return detail("CONTENT_POLICY_REJECTED", "模型服务因内容审核限制拒绝了本次请求。", "请调整描述或参考图中的受限内容后再提交;重复提交相同内容通常无法解决。");
|
|
||||||
}
|
|
||||||
if (/上传到存储桶失败|存储.*失败|归档.*失败|archive.*failed|storage.*failed/i.test(text)) {
|
|
||||||
return detail("RESULT_STORAGE_FAILED", "处理结果保存失败,生成结果可能已经存在。", "请保留当前页面并联系管理员提供请求编号,先不要重复生成。", "unknown");
|
|
||||||
}
|
|
||||||
if (/参考图.*(?:下载失败|不可|失效)|无法获取图片|图片(?:源文件|链接).*不可|获取图片.*失败|get image url failed/i.test(text)) {
|
|
||||||
return detail("SOURCE_IMAGE_UNAVAILABLE", "请求中的图片无法读取,链接可能已失效或没有访问权限。", "请重新上传原图后再提交;若原图仍可打开,请联系管理员检查取图服务。");
|
|
||||||
}
|
|
||||||
if (/版本冲突|画布.*(?:更新|过期)|CANVAS_REVISION_CONFLICT|stale.canvas/i.test(text)) {
|
|
||||||
return detail("CANVAS_REVISION_CONFLICT", "画布已更新,本次保存与当前版本冲突。", "请先重新同步画布,再继续操作,避免覆盖较新的修改。");
|
|
||||||
}
|
|
||||||
if (/no_eligible_route|service_unavailable|service_rejected|channel.*unavailable|暂无可用服务|未配置.*模型|没有可用.*(?:模型|通道)|通道.*(?:不可用|未通过)|鉴权/i.test(text)) {
|
|
||||||
return detail("PROVIDER_UNAVAILABLE", "当前模型服务或通道暂不可用。", "请切换其他可用模型;若多个模型都失败,请联系管理员检查服务配置。");
|
|
||||||
}
|
|
||||||
if (/neither a task ID nor output URLs|已完成.*未取回|completed without.*output/i.test(text)) {
|
|
||||||
return detail("RESULT_UNCONFIRMED", "模型服务没有返回可读取的任务结果,生成状态尚未确认。", "请保留当前任务并联系管理员核对,避免重复生成。", "unknown");
|
|
||||||
}
|
|
||||||
if (/(?:抠图|去水印|图片增强|图片|扩图)处理失败/.test(text)) {
|
|
||||||
return detail("PROCESSING_FAILED", "图片处理服务执行失败,未返回具体原因。", "请联系管理员并提供请求编号,先核对原任务结果,避免重复处理。", "unknown");
|
|
||||||
}
|
|
||||||
if (/service_error|upstream_server_error|excessive system load|generate(?: image)? failed/i.test(text)) {
|
|
||||||
return detail("PROVIDER_EXECUTION_FAILED", "模型服务未能完成本次处理。", "请稍后重试;若连续失败,请切换模型或联系管理员,并提供请求编号。");
|
|
||||||
}
|
|
||||||
if (/timeout|timed out|超时|fetch failed|ECONNRESET|ECONNREFUSED|failed to fetch|networkerror|load failed|network request failed/i.test(text) || [502, 504].includes(status || 0)) {
|
|
||||||
return detail("RESULT_UNCONFIRMED", "请求连接中断或等待超时,任务结果尚未确认。", "请先查看原任务或历史记录,避免重复提交;持续没有结果时联系管理员。", "unknown");
|
|
||||||
}
|
|
||||||
if (/generate(?: image)? failed|generation failed|upstream_server_error|service_error|excessive system load|未返回可用图片|任务FAILED/i.test(text) || (status !== null && status >= 500)) {
|
|
||||||
return detail("PROVIDER_EXECUTION_FAILED", "模型服务未能完成本次处理。", "请稍后重试;若连续失败,请切换模型或联系管理员,并提供请求编号。");
|
|
||||||
}
|
|
||||||
const message = getRequestErrorMessage(error);
|
|
||||||
return detail("REQUEST_FAILED", message && !/^\s*</.test(message) ? message : fallbackMessage, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatRequestFailure(details: RequestFailureDetails, requestId?: string) {
|
|
||||||
const text = [details.message, details.recommendation].filter(Boolean).join("");
|
|
||||||
return requestId ? `${text}\n\n请求编号:${requestId}` : text;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getUserFacingImageGenerationErrorMessage(
|
export function getUserFacingImageGenerationErrorMessage(
|
||||||
error: unknown,
|
error: unknown,
|
||||||
fallbackMessage = "图片生成失败,请稍后重试",
|
fallbackMessage = "图片生成失败,请稍后重试",
|
||||||
) {
|
) {
|
||||||
return formatRequestFailure(getRequestFailureDetails(error, fallbackMessage));
|
const gatewayMessage = getPublicGatewayErrorMessage(error);
|
||||||
|
if (gatewayMessage) return gatewayMessage;
|
||||||
|
if (isProviderBalanceError(error)) {
|
||||||
|
return "图片服务商线路余额或额度不足,请切换模型或联系管理员;与您的积分余额无关。";
|
||||||
|
}
|
||||||
|
if (isRateLimitError(error)) {
|
||||||
|
return "图片服务请求过于频繁,请稍后重试或切换模型。";
|
||||||
|
}
|
||||||
|
return getRequestErrorMessage(error) || fallbackMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFriendlyRateLimitMessage(
|
export function getFriendlyRateLimitMessage(
|
||||||
@ -301,12 +251,15 @@ export function getUserFacingRequestErrorMessage(
|
|||||||
rateLimitMessage?: string;
|
rateLimitMessage?: string;
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
|
const gatewayMessage = getPublicGatewayErrorMessage(error);
|
||||||
|
if (gatewayMessage) return gatewayMessage;
|
||||||
|
if (isProviderBalanceError(error)) {
|
||||||
|
return "模型服务商线路余额或额度不足,请切换模型或联系管理员。";
|
||||||
|
}
|
||||||
if (isRateLimitError(error)) {
|
if (isRateLimitError(error)) {
|
||||||
return getFriendlyRateLimitMessage(options?.rateLimitMessage);
|
return getFriendlyRateLimitMessage(options?.rateLimitMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
const failure = getRequestFailureDetails(error, fallbackMessage);
|
|
||||||
if (failure.code !== "REQUEST_FAILED") return formatRequestFailure(failure);
|
|
||||||
const message = getRequestErrorMessage(error);
|
const message = getRequestErrorMessage(error);
|
||||||
if (!message) {
|
if (!message) {
|
||||||
return fallbackMessage;
|
return fallbackMessage;
|
||||||
|
|||||||
@ -3,9 +3,6 @@ import { defineConfig } from "@playwright/test";
|
|||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
testDir: "./tests",
|
testDir: "./tests",
|
||||||
testMatch: [
|
testMatch: [
|
||||||
/routes\/runtime-active-work\.test\.ts/,
|
|
||||||
/routes\/canvas-capability-task-recovery\.test\.ts/,
|
|
||||||
/routes\/canvas-toolbar-capability\.test\.ts/,
|
|
||||||
/routes\/agent-project-recovery\.test\.ts/,
|
/routes\/agent-project-recovery\.test\.ts/,
|
||||||
/routes\/admin-media-loading\.test\.ts/,
|
/routes\/admin-media-loading\.test\.ts/,
|
||||||
/routes\/video-canvas-production\.test\.ts/,
|
/routes\/video-canvas-production\.test\.ts/,
|
||||||
|
|||||||
16
proxy.ts
16
proxy.ts
@ -43,7 +43,6 @@ const manualIpBlockCache = new Map<
|
|||||||
>();
|
>();
|
||||||
|
|
||||||
function isHighFrequencyReadApi(pathname: string, method: string) {
|
function isHighFrequencyReadApi(pathname: string, method: string) {
|
||||||
if (method === "POST" && /^\/api\/canvas\/[^/]+\/image-archives$/.test(pathname)) return true;
|
|
||||||
if (method === "GET" && pathname === "/api/canvas/download-zip-task") return true;
|
if (method === "GET" && pathname === "/api/canvas/download-zip-task") return true;
|
||||||
if (method !== "GET") return false;
|
if (method !== "GET") return false;
|
||||||
if (/^\/api\/canvas\/[^/]+\/detail-archives$/.test(pathname)) return true;
|
if (/^\/api\/canvas\/[^/]+\/detail-archives$/.test(pathname)) return true;
|
||||||
@ -669,20 +668,7 @@ export async function proxy(request: NextRequest, event: NextFetchEvent) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (request.method === "POST" && /^\/api\/canvas\/[^/]+\/image-archives$/.test(pathname)) {
|
} else if (isHighFrequencyReadApi(pathname, request.method)) {
|
||||||
const token = await resolveToken();
|
|
||||||
const identifier = typeof token?.sub === "string" && token.sub
|
|
||||||
? `user:${token.sub}:image-archives`
|
|
||||||
: `ip:${clientIP}:image-archives`;
|
|
||||||
const result = checkRateLimitByIdentifier(identifier, rateLimiters.uiPolling);
|
|
||||||
if (!result.allowed) {
|
|
||||||
const retryAfter = Math.max(1, Math.ceil((result.resetTime - Date.now()) / 1000));
|
|
||||||
return withAccessLog(NextResponse.json(
|
|
||||||
{ error: "图片状态查询过于频繁,请稍后再查看", retryAfter },
|
|
||||||
{ status: 429, headers: { "Retry-After": String(retryAfter) } },
|
|
||||||
), { outcome: "rate_limited", severity: "watch", reasons: ["图片归档状态查询限流"] });
|
|
||||||
}
|
|
||||||
} else if (isHighFrequencyReadApi(pathname, request.method)) {
|
|
||||||
const pollingRateLimit = checkRateLimit(request, rateLimiters.uiPolling);
|
const pollingRateLimit = checkRateLimit(request, rateLimiters.uiPolling);
|
||||||
if (!pollingRateLimit.allowed) {
|
if (!pollingRateLimit.allowed) {
|
||||||
console.warn(`[Security] UI polling rate limit exceeded for IP: ${clientIP}`, {
|
console.warn(`[Security] UI polling rate limit exceeded for IP: ${clientIP}`, {
|
||||||
|
|||||||
@ -1,65 +0,0 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
|
||||||
|
|
||||||
function harness(status = "running") {
|
|
||||||
let now = 2_000_000;
|
|
||||||
const row: any = { id: "task-a", requestId: "request-a", userId: "user-a", projectId: "project-a", status, attempts: status === "pending" ? 0 : 1, updatedAt: new Date(now - 16 * 60_000), error: null };
|
|
||||||
const matches = (predicate: any): boolean => !predicate || (predicate[0] === "and" ? predicate.slice(1).every(matches)
|
|
||||||
: predicate[0] === "eq" ? row[predicate[1]] === predicate[2]
|
|
||||||
: predicate[0] === "lt" ? row[predicate[1]] < predicate[2]
|
|
||||||
: predicate[2].includes(row[predicate[1]]));
|
|
||||||
const db = {
|
|
||||||
select: () => ({ from: () => ({ where: (p: any) => ({ limit: async () => matches(p) ? [{ ...row }] : [] }) }) }),
|
|
||||||
insert: () => ({ values: () => ({ onConflictDoNothing: async () => undefined }) }),
|
|
||||||
update: () => ({ set: (values: any) => ({ where: (p: any) => {
|
|
||||||
const apply = () => {
|
|
||||||
if (!matches(p)) return [];
|
|
||||||
for (const [key, value] of Object.entries(values)) row[key] = key === "attempts" ? row.attempts + 1 : value;
|
|
||||||
return [{ ...row }];
|
|
||||||
};
|
|
||||||
const result = apply();
|
|
||||||
return Object.assign(Promise.resolve(result), { returning: async () => result });
|
|
||||||
} }) }),
|
|
||||||
};
|
|
||||||
const columns = Object.fromEntries(["id", "requestId", "userId", "projectId", "status", "updatedAt", "attempts"].map(k => [k, k]));
|
|
||||||
const store = serverLoader({
|
|
||||||
"@/lib/db": { db }, "@/lib/db/schema": { canvasCapabilityTask: columns },
|
|
||||||
"drizzle-orm": { eq: (...v: any[]) => ["eq", ...v], lt: (...v: any[]) => ["lt", ...v], inArray: (...v: any[]) => ["in", ...v], and: (...v: any[]) => ["and", ...v], sql: () => "increment" },
|
|
||||||
Date: class extends Date { constructor(value?: any) { super(value ?? now); } static now() { return now; } },
|
|
||||||
})("lib/canvas/capabilities/task-store.ts");
|
|
||||||
const lookup = { requestId: "request-a", userId: "user-a", projectId: "project-a" };
|
|
||||||
return { row, store, lookup, advance: (ms: number) => { now += ms; } };
|
|
||||||
}
|
|
||||||
|
|
||||||
test("a stale task stops reporting running without authorizing another execution", async () => {
|
|
||||||
const h = harness();
|
|
||||||
const task = await h.store.getCanvasCapabilityTask(h.lookup);
|
|
||||||
expect(task.status).toBe("failed");
|
|
||||||
expect(task.error).toContain("结果尚未确认");
|
|
||||||
const repeated = await h.store.createOrRecoverCanvasCapabilityTask({ ...h.lookup, command: { requestId: "request-a" } });
|
|
||||||
expect(repeated.status).toBe("failed");
|
|
||||||
expect(repeated.attempts).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("heartbeats keep a legitimate long-running task active beyond fifteen minutes", async () => {
|
|
||||||
const h = harness();
|
|
||||||
await h.store.heartbeatCanvasCapabilityTask("task-a");
|
|
||||||
h.advance(14 * 60_000);
|
|
||||||
await h.store.heartbeatCanvasCapabilityTask("task-a");
|
|
||||||
h.advance(14 * 60_000);
|
|
||||||
expect((await h.store.getCanvasCapabilityTask(h.lookup)).status).toBe("running");
|
|
||||||
expect(h.row.attempts).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("only the pending task can be claimed and a duplicate claim cannot run again", async () => {
|
|
||||||
const h = harness("pending");
|
|
||||||
expect(await h.store.markCanvasCapabilityTaskRunning("task-a")).toMatchObject({ status: "running", attempts: 1 });
|
|
||||||
expect(await h.store.markCanvasCapabilityTaskRunning("task-a")).toBeNull();
|
|
||||||
expect(h.row.attempts).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("another user cannot inspect or change a stale task", async () => {
|
|
||||||
const h = harness();
|
|
||||||
expect(await h.store.getCanvasCapabilityTask({ ...h.lookup, userId: "user-b" })).toBeNull();
|
|
||||||
expect(h.row.status).toBe("running");
|
|
||||||
});
|
|
||||||
@ -47,7 +47,7 @@ test("merges the result with edits made while processing and advances the save r
|
|||||||
globalThis.fetch = (async (url) => {
|
globalThis.fetch = (async (url) => {
|
||||||
if (String(url).endsWith("/capabilities")) {
|
if (String(url).endsWith("/capabilities")) {
|
||||||
useCanvasStore.getState().moveNode("source", { x: 50, y: 80 });
|
useCanvasStore.getState().moveNode("source", { x: 50, y: 80 });
|
||||||
return Response.json({ success: true, task: { status: "succeeded" } });
|
return Response.json({ success: true });
|
||||||
}
|
}
|
||||||
return Response.json({ success: true, data: {
|
return Response.json({ success: true, data: {
|
||||||
updatedAt: "r2", canvasData: { nodes: [node("source"), node("result")], edges: [] },
|
updatedAt: "r2", canvasData: { nodes: [node("source"), node("result")], edges: [] },
|
||||||
@ -67,31 +67,12 @@ test("a provider failure is shown without reloading or changing the canvas", asy
|
|||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("a lost submission response polls the original task and never resubmits", async () => {
|
|
||||||
const requests: { method: string; url: string }[] = [];
|
|
||||||
const originalSetTimeout = globalThis.setTimeout;
|
|
||||||
let polls = 0;
|
|
||||||
globalThis.setTimeout = ((fn: () => void) => originalSetTimeout(fn, 0)) as typeof setTimeout;
|
|
||||||
globalThis.fetch = (async (url, init) => {
|
|
||||||
requests.push({ method: init?.method || "GET", url: String(url) });
|
|
||||||
if (init?.method === "POST") throw new TypeError("fetch failed");
|
|
||||||
if (String(url).includes("/capabilities?")) return Response.json({ success: true, task: { status: ++polls === 1 ? "running" : "succeeded" } });
|
|
||||||
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: { nodes: [node("source"), node("result")], edges: [] } } });
|
|
||||||
}) as typeof fetch;
|
|
||||||
const submitted = command();
|
|
||||||
try { await executeCanvasToolbarCapability(submitted); } finally { globalThis.setTimeout = originalSetTimeout; }
|
|
||||||
expect(requests.filter(r => r.method === "POST")).toHaveLength(1);
|
|
||||||
expect(requests.filter(r => r.url.includes("/capabilities?"))).toHaveLength(2);
|
|
||||||
expect(requests.filter(r => r.url.includes("/capabilities?")).every(r => r.url.includes(encodeURIComponent(submitted.requestId)))).toBe(true);
|
|
||||||
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["source", "result"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("switching projects during processing does not load the old result into the new project", async () => {
|
test("switching projects during processing does not load the old result into the new project", async () => {
|
||||||
globalThis.fetch = (async (url) => {
|
globalThis.fetch = (async (url) => {
|
||||||
if (String(url).endsWith("/capabilities")) {
|
if (String(url).endsWith("/capabilities")) {
|
||||||
useCanvasStore.getState().setProject("project-b");
|
useCanvasStore.getState().setProject("project-b");
|
||||||
useCanvasStore.getState().loadCanvas({ nodes: [node("other") as any], edges: [] });
|
useCanvasStore.getState().loadCanvas({ nodes: [node("other") as any], edges: [] });
|
||||||
return Response.json({ success: true, task: { status: "succeeded" } });
|
return Response.json({ success: true });
|
||||||
}
|
}
|
||||||
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: { nodes: [node("result")], edges: [] } } });
|
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: { nodes: [node("result")], edges: [] } } });
|
||||||
}) as typeof fetch;
|
}) as typeof fetch;
|
||||||
@ -123,7 +104,7 @@ test("a result merge conflict retains both the local edit and its original revis
|
|||||||
globalThis.fetch = (async (url) => {
|
globalThis.fetch = (async (url) => {
|
||||||
if (String(url).endsWith("/capabilities")) {
|
if (String(url).endsWith("/capabilities")) {
|
||||||
useCanvasStore.getState().moveNode("source", { x: 50, y: 80 });
|
useCanvasStore.getState().moveNode("source", { x: 50, y: 80 });
|
||||||
return Response.json({ success: true, task: { status: "succeeded" } });
|
return Response.json({ success: true });
|
||||||
}
|
}
|
||||||
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: {
|
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: {
|
||||||
nodes: [{ ...node("source"), position: { x: 200, y: 80 } }, node("result")], edges: [],
|
nodes: [{ ...node("source"), position: { x: 200, y: 80 } }, node("result")], edges: [],
|
||||||
|
|||||||
@ -2,34 +2,11 @@ import { expect, test } from "@playwright/test";
|
|||||||
import {
|
import {
|
||||||
getUserFacingImageGenerationErrorMessage,
|
getUserFacingImageGenerationErrorMessage,
|
||||||
getUserFacingRequestErrorMessage,
|
getUserFacingRequestErrorMessage,
|
||||||
getRequestFailureDetails,
|
|
||||||
formatRequestFailure,
|
|
||||||
getGatewayErrorPageMessage,
|
getGatewayErrorPageMessage,
|
||||||
isGatewayResponseTimeout,
|
isGatewayResponseTimeout,
|
||||||
isProviderBalanceError,
|
isProviderBalanceError,
|
||||||
isRateLimitError,
|
isRateLimitError,
|
||||||
} from "@/lib/request-error";
|
} from "@/lib/request-error";
|
||||||
|
|
||||||
test("known provider and platform failures explain their cause and next action", () => {
|
|
||||||
for (const [message, code, next] of [
|
|
||||||
["We are sorry, this prompt may violate our content policies", "CONTENT_POLICY_REJECTED", "调整描述"],
|
|
||||||
["The channel is temporarily unavailable", "PROVIDER_UNAVAILABLE", "切换其他"],
|
|
||||||
["generate image failed", "PROVIDER_EXECUTION_FAILED", "稍后重试"],
|
|
||||||
["excessive system load", "PROVIDER_EXECUTION_FAILED", "稍后重试"],
|
|
||||||
["get image url failed", "SOURCE_IMAGE_UNAVAILABLE", "重新上传"],
|
|
||||||
["抠图处理失败", "PROCESSING_FAILED", "核对原任务"],
|
|
||||||
["去水印处理失败", "PROCESSING_FAILED", "核对原任务"],
|
|
||||||
["图片上传到存储桶失败", "RESULT_STORAGE_FAILED", "不要重复生成"],
|
|
||||||
["任务执行中断,结果尚未确认", "RESULT_UNCONFIRMED", "避免重复生成"],
|
|
||||||
]) {
|
|
||||||
const failure = getRequestFailureDetails(new Error(message));
|
|
||||||
expect(failure.code).toBe(code);
|
|
||||||
expect(failure.recommendation).toContain(next);
|
|
||||||
expect(formatRequestFailure(failure, "request-123")).toContain("请求编号:request-123");
|
|
||||||
}
|
|
||||||
expect(getRequestFailureDetails({ providerOutcomeUnknown: true, message: "content policy" }).outcome).toBe("unknown");
|
|
||||||
expect(getRequestFailureDetails(new Error("图片上传到存储桶失败")).outcome).toBe("unknown");
|
|
||||||
});
|
|
||||||
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||||||
|
|
||||||
@ -68,14 +45,13 @@ test("HTTP 524 does not start another model while the original result is unconfi
|
|||||||
expect(health.shouldFailoverAiModelError(new Error("Service unavailable (HTTP 503)"))).toBe(true);
|
expect(health.shouldFailoverAiModelError(new Error("Service unavailable (HTTP 503)"))).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("the provider submits once when an HTTP 524 request cannot be located", async () => {
|
test("the provider submits once and keeps an HTTP 524 response unconfirmed", async () => {
|
||||||
let submissions = 0;
|
let submissions = 0;
|
||||||
const provider = serverLoader({
|
const provider = serverLoader({
|
||||||
"@/lib/db/queries": { getSystemSetting: async () => null },
|
"@/lib/db/queries": { getSystemSetting: async () => null },
|
||||||
"@/lib/storage/image-archive": {},
|
"@/lib/storage/image-archive": {},
|
||||||
"@/lib/storage/service": {},
|
"@/lib/storage/service": {},
|
||||||
fetch: async (_url: string, init: RequestInit) => {
|
fetch: async () => {
|
||||||
if (init?.method !== "POST") return Response.json({}, { status: 404 });
|
|
||||||
submissions++;
|
submissions++;
|
||||||
return new Response(gatewayHtml, { status: 524, headers: { "content-type": "text/html" } });
|
return new Response(gatewayHtml, { status: 524, headers: { "content-type": "text/html" } });
|
||||||
},
|
},
|
||||||
@ -85,136 +61,6 @@ test("the provider submits once when an HTTP 524 request cannot be located", asy
|
|||||||
expect(submissions).toBe(1);
|
expect(submissions).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
function imageRecoveryHarness(
|
|
||||||
handler: (path: string, method: string, call: number) => Response | Promise<Response>,
|
|
||||||
maxPollSeconds = 3,
|
|
||||||
) {
|
|
||||||
let now = 1_000;
|
|
||||||
const calls: { path: string; method: string; headers: Headers }[] = [];
|
|
||||||
const archived: string[] = [];
|
|
||||||
const load = serverLoader({
|
|
||||||
"@/lib/db": { db: {} },
|
|
||||||
"@/lib/db/queries": { getSystemSetting: async () => null },
|
|
||||||
"@/lib/storage/image-archive": { createDeferredUploadedImage: async (url: string) => {
|
|
||||||
archived.push(url);
|
|
||||||
return { thumbnail: url, original: url };
|
|
||||||
} },
|
|
||||||
"@/lib/storage/service": {},
|
|
||||||
Date: class extends Date { static now() { return now; } },
|
|
||||||
setTimeout: (fn: () => void, ms: number) => { now += ms; return setTimeout(fn, 0); },
|
|
||||||
fetch: async (url: string, init: RequestInit) => {
|
|
||||||
const path = new URL(url).pathname;
|
|
||||||
const method = init?.method || "GET";
|
|
||||||
calls.push({ path, method, headers: new Headers(init?.headers) });
|
|
||||||
return handler(path, method, calls.length);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const provider = load("lib/ai/providers/vip98k.ts");
|
|
||||||
return {
|
|
||||||
calls, load, archived,
|
|
||||||
generate: () => provider.createVip98k({ apiKey: "test-only-key", baseURL: "https://provider.test" })
|
|
||||||
.chat("gpt-image-2.5", { capabilities: { vip98k: { pollIntervalSeconds: 1, maxPollSeconds } } })
|
|
||||||
.doGenerate({ messages: [{ role: "user", content: "A desk" }] }),
|
|
||||||
run: () => provider.executeVip98kExternalTask({
|
|
||||||
apiKey: "test-only-key", baseURL: "https://provider.test", modelId: "gpt-image-2.5",
|
|
||||||
kind: "image", prompt: "A desk", imageUrls: [],
|
|
||||||
modelConfig: { capabilities: { vip98k: { pollIntervalSeconds: 1, maxPollSeconds } } },
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const recoveredImage = { created: 1, data: [{ url: "https://image.example/original.png" }] };
|
|
||||||
const originalCompleted = { request_id: "original-request", status: "succeeded", result_available: true };
|
|
||||||
|
|
||||||
test("an edge timeout waits for and delivers the original image without another POST", async () => {
|
|
||||||
let lookups = 0;
|
|
||||||
const h = imageRecoveryHarness((path, method) => {
|
|
||||||
if (method === "POST") return new Response(gatewayHtml, { status: 524 });
|
|
||||||
if (path.endsWith("by-idempotency-key")) {
|
|
||||||
return Response.json(++lookups === 1 ? { status: "processing" } : originalCompleted);
|
|
||||||
}
|
|
||||||
return Response.json(recoveredImage);
|
|
||||||
});
|
|
||||||
expect((await h.generate()).text).toContain(recoveredImage.data[0].url);
|
|
||||||
expect(h.archived).toEqual([recoveredImage.data[0].url]);
|
|
||||||
expect(h.calls.filter(c => c.method === "POST")).toHaveLength(1);
|
|
||||||
expect(lookups).toBe(2);
|
|
||||||
const originalKey = h.calls[0].headers.get("Idempotency-Key");
|
|
||||||
expect(originalKey).toBeTruthy();
|
|
||||||
for (const call of h.calls.filter(c => c.path.endsWith("by-idempotency-key"))) {
|
|
||||||
expect(call.headers.get("Idempotency-Key")).toBe(originalKey);
|
|
||||||
expect(call.headers.get("Authorization")).toBe("Bearer test-only-key");
|
|
||||||
}
|
|
||||||
expect(h.calls.at(-1)?.path).toBe("/api/external/requests/original-request/result");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a lost POST response recovers by its original key without resubmitting", async () => {
|
|
||||||
const h = imageRecoveryHarness((path, method) => {
|
|
||||||
if (method === "POST") throw new TypeError("fetch failed");
|
|
||||||
return Response.json(path.endsWith("by-idempotency-key") ? originalCompleted : recoveredImage);
|
|
||||||
});
|
|
||||||
expect((await h.run()).outputUrls).toHaveLength(1);
|
|
||||||
expect(h.calls.filter(c => c.method === "POST")).toHaveLength(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("temporary lookup and result-read failures retry only GET requests", async () => {
|
|
||||||
let lookups = 0;
|
|
||||||
let reads = 0;
|
|
||||||
const h = imageRecoveryHarness((path, method) => {
|
|
||||||
if (method === "POST") return new Response("Gateway timeout", { status: 504 });
|
|
||||||
if (path.endsWith("by-idempotency-key")) {
|
|
||||||
return ++lookups === 1 ? new Response("Unavailable", { status: 503 }) : Response.json(originalCompleted);
|
|
||||||
}
|
|
||||||
return ++reads === 1 ? Response.json({}, { status: 409 }) : Response.json(recoveredImage);
|
|
||||||
}, 5);
|
|
||||||
expect((await h.run()).outputUrls).toHaveLength(1);
|
|
||||||
expect(h.calls.filter(c => c.method === "POST")).toHaveLength(1);
|
|
||||||
expect(reads).toBe(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("missing, unconfirmed and unretained originals never create a replacement", async () => {
|
|
||||||
for (const state of [null, { status: "failed" }, { status: "unresolved" }, { ...originalCompleted, result_available: false }]) {
|
|
||||||
const h = imageRecoveryHarness((_path, method) => method === "POST"
|
|
||||||
? new Response(gatewayHtml, { status: 524 })
|
|
||||||
: Response.json(state || {}, { status: state ? 200 : 404 }));
|
|
||||||
await expect(h.run()).rejects.toThrow(/结果未确认.*HTTP 524/);
|
|
||||||
expect(h.calls.map(c => c.method)).toEqual(["POST", "GET"]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a still-processing original stops at the polling deadline without another POST", async () => {
|
|
||||||
const h = imageRecoveryHarness((_path, method) => method === "POST"
|
|
||||||
? new Response(gatewayHtml, { status: 524 }) : Response.json({ status: "processing" }), 2);
|
|
||||||
await expect(h.run()).rejects.toThrow(/结果未确认.*HTTP 524/);
|
|
||||||
expect(h.calls.map(c => c.method)).toEqual(["POST", "GET", "GET"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("HTTP 502 and 504 without a recoverable original do not repeat the POST", async () => {
|
|
||||||
for (const status of [502, 504]) {
|
|
||||||
const h = imageRecoveryHarness((_path, method) => method === "POST"
|
|
||||||
? new Response("Gateway timeout", { status }) : Response.json({}, { status: 404 }));
|
|
||||||
await expect(h.run()).rejects.toThrow(/结果未确认/);
|
|
||||||
expect(h.calls.map(c => c.method)).toEqual(["POST", "GET"]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a completed original with no image stays unconfirmed and cannot fail over", async () => {
|
|
||||||
const h = imageRecoveryHarness((path, method) => {
|
|
||||||
if (method === "POST") return new Response(gatewayHtml, { status: 524 });
|
|
||||||
return Response.json(path.endsWith("by-idempotency-key") ? originalCompleted : { data: [] });
|
|
||||||
});
|
|
||||||
const error = await h.run().then(() => null, (e: Error) => e);
|
|
||||||
expect(error).toBeTruthy();
|
|
||||||
expect(h.load("lib/ai/runtime/model-health.ts").shouldFailoverAiModelError(error)).toBe(false);
|
|
||||||
expect(h.calls.filter(c => c.method === "POST")).toHaveLength(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("ordinary image success uses the original response directly", async () => {
|
|
||||||
const h = imageRecoveryHarness(() => Response.json(recoveredImage));
|
|
||||||
expect((await h.run()).outputUrls).toHaveLength(1);
|
|
||||||
expect(h.calls.map(c => c.method)).toEqual(["POST"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an explicit provider balance error takes precedence over HTTP 429", () => {
|
test("an explicit provider balance error takes precedence over HTTP 429", () => {
|
||||||
const error = Object.assign(new Error("余额不足,请点击充值页面续费(HTTP 429)"), { status: 429 });
|
const error = Object.assign(new Error("余额不足,请点击充值页面续费(HTTP 429)"), { status: 429 });
|
||||||
expect(isProviderBalanceError(error)).toBe(true);
|
expect(isProviderBalanceError(error)).toBe(true);
|
||||||
|
|||||||
@ -1,19 +0,0 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
|
||||||
|
|
||||||
test("release draining includes background work until its original promise settles", async () => {
|
|
||||||
const counts: number[] = [];
|
|
||||||
const work = serverLoader({
|
|
||||||
"node:fs": { writeFileSync: (_path: string, text: string) => counts.push(JSON.parse(text).active), unlinkSync() {} },
|
|
||||||
})("lib/observability/active-work.ts");
|
|
||||||
let finish!: () => void;
|
|
||||||
const original = new Promise<void>(resolve => { finish = resolve; });
|
|
||||||
const pending = work.trackRuntimeWork(() => original);
|
|
||||||
await work.trackRuntimeWork(async () => "another request");
|
|
||||||
expect(counts).toEqual([1, 2, 1]);
|
|
||||||
finish();
|
|
||||||
await pending;
|
|
||||||
expect(counts).toEqual([1, 2, 1, 0]);
|
|
||||||
await expect(work.trackRuntimeWork(async () => { throw new Error("expected failure"); })).rejects.toThrow("expected failure");
|
|
||||||
expect(counts.at(-1)).toBe(0);
|
|
||||||
});
|
|
||||||
Loading…
x
Reference in New Issue
Block a user