Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"timeoutErrorDesc": "The request took too long, please try again",
"tooManyRequests": "Too many requests",
"tooManyRequestsDesc": "You have made too many requests, please try again later",
"promptFlagged": "Prompt flagged",
"promptFlaggedDesc": "Your prompt violates content policy, please modify and try again",
"inputImageFlagged": "Image flagged",
"inputImageFlaggedDesc": "Your image violates content policy, please change and try again",
"unknownError": "Unknown error",
"unknownErrorDesc": "An unexpected error occurred, please try again",
"goToSettings": "Go to Settings",
Expand Down
4 changes: 4 additions & 0 deletions src/app/i18n/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"timeoutErrorDesc": "请求时间过长,请重试",
"tooManyRequests": "请求过于频繁",
"tooManyRequestsDesc": "您的请求次数过多,请稍后再试",
"promptFlagged": "提示词违规",
"promptFlaggedDesc": "提示词违反内容政策,请修改后重试",
"inputImageFlagged": "图片违规",
"inputImageFlaggedDesc": "图片违反内容政策,请更换后重试",
"unknownError": "未知错误",
"unknownErrorDesc": "发生了意外错误,请重试",
"goToSettings": "前往设置",
Expand Down
16 changes: 16 additions & 0 deletions src/app/routes/chat/-components/chat/GenerationErrorItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ export function GenerationErrorItem({ errorReason, provider, onRetry, className
buttonIcon: RefreshCw,
buttonAction: "retry" as const,
};
case "PROMPT_FLAGGED":
return {
title: t("chat.generation.promptFlagged"),
description: t("chat.generation.promptFlaggedDesc"),
buttonText: t("chat.generation.retry"),
buttonIcon: RefreshCw,
buttonAction: "retry" as const,
};
case "INPUT_IMAGE_FLAGGED":
return {
title: t("chat.generation.inputImageFlagged"),
description: t("chat.generation.inputImageFlaggedDesc"),
buttonText: t("chat.generation.retry"),
buttonIcon: RefreshCw,
buttonAction: "retry" as const,
};
default:
return {
title: t("chat.generation.unknownError"),
Expand Down
32 changes: 20 additions & 12 deletions src/server/ai/provider/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inCfWorker } from "@/server/lib/env";
import type { ReplacePropertyType } from "@/server/lib/types";
import { GenError, type ReplacePropertyType } from "@/server/lib/types";
import { base64ToBlob, base64ToDataURI, dataURItoBase64, readableStreamToDataURI } from "@/server/lib/util";
import { getContext } from "@/server/service/context";
import { type TypixGenerateRequest, commonAspectRatioSizes } from "../types/api";
Expand Down Expand Up @@ -38,13 +38,27 @@ const createFormData = (params: any, model: CloudflareAiModel, request: TypixGen
// Helper function to handle API response
const handleApiResponse = async (resp: Response): Promise<string[]> => {
if (!resp.ok) {
const errorText = await resp.text();
if (resp.status === 401 || resp.status === 404) {
throw new Error("CONFIG_ERROR");
throw new GenError("CONFIG_ERROR");
}
if (resp.status === 429) {
throw new Error("TOO_MANY_REQUESTS");
throw new GenError("TOO_MANY_REQUESTS");
}
if (resp.status === 400) {
const errorResp = JSON.parse(errorText);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 400 responses, JSON.parse(errorText) (and firstErr.message.includes(...)) can throw if Cloudflare returns non-JSON or the shape differs, which would skip the intended GenError mapping. Consider guarding the parse/message access so flagged requests reliably surface as PROMPT_FLAGGED / INPUT_IMAGE_FLAGGED.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

if (errorResp.errors && Array.isArray(errorResp.errors)) {
const firstErr = errorResp.errors.find((err: any) => err.code === 3030);
if (firstErr) {
if (firstErr.message.includes("prompt")) {
throw new GenError("PROMPT_FLAGGED");
}
if (firstErr.message.includes("Input image")) {
throw new GenError("INPUT_IMAGE_FLAGGED");
}
}
}
}
const errorText = await resp.text();
throw new Error(`Cloudflare API error: ${resp.status} ${resp.statusText} - ${errorText}`);
}

Expand Down Expand Up @@ -249,15 +263,9 @@ const Cloudflare: CloudflareProvider = {
images: allImages,
};
} catch (error: any) {
if (error.message === "CONFIG_ERROR") {
return {
errorReason: "CONFIG_ERROR",
images: [],
};
}
if (error.message === "TOO_MANY_REQUESTS") {
if (error instanceof GenError) {
return {
errorReason: "TOO_MANY_REQUESTS",
errorReason: error.reason,
images: [],
};
}
Expand Down
11 changes: 10 additions & 1 deletion src/server/db/schemas/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ export const messageAttachments = sqliteTable("message_attachments", {
...metaFields,
});

const errorReason = ["CONFIG_INVALID", "CONFIG_ERROR", "API_ERROR", "TOO_MANY_REQUESTS", "TIMEOUT", "UNKNOWN"] as const;
const errorReason = [
"CONFIG_INVALID",
"CONFIG_ERROR",
"API_ERROR",
"TOO_MANY_REQUESTS",
"TIMEOUT",
"PROMPT_FLAGGED",
"INPUT_IMAGE_FLAGGED",
"UNKNOWN",
] as const;
export type ErrorReason = (typeof errorReason)[number];

// Generations table - stores AI generation requests and results (images, videos, etc.)
Expand Down
10 changes: 10 additions & 0 deletions src/server/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import type { ErrorReason } from "../db/schemas";

export type StrictOmit<T, K extends keyof T> = Omit<T, K>;
export type ReplacePropertyType<T, K extends keyof T, NewType> = {
[P in keyof T]: P extends K ? NewType : T[P];
};
export class GenError extends Error {
reason: ErrorReason;

constructor(reason: ErrorReason) {
super(reason);
this.reason = reason;
}
}