File size: 7,353 Bytes
e81f4e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | import type { ErrorWithMetadata } from "../../shared/types";
import { promptByteLength } from "../../shared/tokens";
export const LARGE_PROMPT_EMPTY_RESPONSE_MIN_BYTES = 95000;
export const LARGE_PROMPT_EMPTY_RESPONSE_CODE = "large_prompt_empty_response";
export const DATA_ANALYSIS_EMPTY_RESPONSE_CODE = "data_analysis_empty_response";
export const INVALID_GEMINI_COOKIE_CODE = "invalid_gemini_cookie";
export const UPSTREAM_EMPTY_RESPONSE_CODE = "upstream_empty_response";
export const UPSTREAM_IMAGE_GENERATION_EMPTY_CODE =
"upstream_image_generation_empty";
export const UPSTREAM_IMAGE_FETCH_FAILED_CODE = "upstream_image_fetch_failed";
export const UPSTREAM_IMAGE_PROVIDER_ERROR_CODE =
"upstream_image_provider_error";
const AUTH_FAILURE_STATUSES = new Set([401, 403]);
type LargePromptConfig =
| { current_input_file_min_bytes?: unknown }
| null
| undefined;
type CookieConfig = { cookie?: unknown } | null | undefined;
const COOKIE_DIAGNOSTIC_MESSAGES: Record<string, string> = {
missing_cookie: "no Gemini cookie is configured",
missing_secure_1psid: "configured cookie is missing __Secure-1PSID",
recent_rotation:
"cookie rotation was skipped because a rotation ran recently",
rotation_rejected: "Google rejected the RotateCookies request",
rotation_failed: "RotateCookies returned a non-success status",
rotation_no_update:
"RotateCookies completed but did not return an updated cookie",
rotation_error: "RotateCookies could not be completed",
rotation_updated:
"cookie rotation succeeded but Gemini still rejected the request",
missing_page_at_token: "Gemini page did not return the required auth token",
};
export function largePromptEmptyResponseThreshold(
cfg: LargePromptConfig,
): number {
return Math.max(
0,
Number(cfg?.current_input_file_min_bytes) ||
LARGE_PROMPT_EMPTY_RESPONSE_MIN_BYTES,
);
}
export function largePromptEmptyResponseError(
prompt: unknown,
status: unknown,
rawLength: number | null,
thresholdBytes: unknown = LARGE_PROMPT_EMPTY_RESPONSE_MIN_BYTES,
): ErrorWithMetadata | null {
const bytes = promptByteLength(prompt);
const threshold = Math.max(
0,
Number(thresholdBytes) || LARGE_PROMPT_EMPTY_RESPONSE_MIN_BYTES,
);
if (bytes <= threshold) return null;
const err: ErrorWithMetadata = new Error(
`Context is too long and triggered Gemini Web risk controls, so Gemini returned an empty response ` +
`(${bytes} UTF-8 bytes > ${threshold}). This is unrelated to GEMINI_BL; ` +
"configure GEMINI_COOKIE so this worker can route long context through txt attachments, or reduce the latest inline request size.",
);
err.code = LARGE_PROMPT_EMPTY_RESPONSE_CODE;
err.promptBytes = bytes;
err.thresholdBytes = threshold;
err.upstreamStatus = Number(status);
err.rawLength = rawLength;
return err;
}
export function isLargePromptEmptyResponseError(e: unknown): boolean {
return (
!!e &&
typeof e === "object" &&
(e as Partial<ErrorWithMetadata>).code === LARGE_PROMPT_EMPTY_RESPONSE_CODE
);
}
export function dataAnalysisEmptyResponseError(
rawSnippet: unknown,
fileRefs: unknown,
): ErrorWithMetadata | null {
if (!fileRefs || !String(rawSnippet || "").includes("data_analysis_tool"))
return null;
const err: ErrorWithMetadata = new Error(
"Gemini accepted the uploaded context file but routed it into the internal data_analysis_tool and returned no final text. " +
"This Worker does not implement Gemini Web's follow-up data-analysis tool loop. Try the markdown context-file defaults, lower CURRENT_INPUT_FILE_MIN_BYTES, or disable CURRENT_INPUT_FILE_ENABLED for this request.",
);
err.code = DATA_ANALYSIS_EMPTY_RESPONSE_CODE;
return err;
}
export function isDataAnalysisEmptyResponseError(e: unknown): boolean {
return (
!!e &&
typeof e === "object" &&
(e as Partial<ErrorWithMetadata>).code === DATA_ANALYSIS_EMPTY_RESPONSE_CODE
);
}
export function upstreamEmptyResponseError(
status: unknown,
rawLength: number | null,
context = "",
): ErrorWithMetadata {
const httpStatus = Number(status);
const err: ErrorWithMetadata = new Error(
`Gemini upstream HTTP ${Number.isFinite(httpStatus) ? httpStatus : String(status)} returned no parseable text` +
(context ? ` (${context})` : "") +
". The upstream request completed but the Worker could not extract a final model response.",
);
err.code = UPSTREAM_EMPTY_RESPONSE_CODE;
err.status = 502;
err.upstreamStatus = httpStatus;
err.rawLength = rawLength;
return err;
}
export function upstreamImageGenerationEmptyError(
status: unknown,
rawLength: number | null,
context = "",
): ErrorWithMetadata {
const httpStatus = Number(status);
const err: ErrorWithMetadata = new Error(
`Gemini upstream HTTP ${Number.isFinite(httpStatus) ? httpStatus : String(status)} returned no usable generated image` +
(context ? ` (${context})` : "") +
". The upstream request completed but the Worker could not extract generated image output.",
);
err.code = UPSTREAM_IMAGE_GENERATION_EMPTY_CODE;
err.status = 502;
err.upstreamStatus = httpStatus;
err.rawLength = rawLength;
return err;
}
export function upstreamImageFetchFailedError(
message: unknown,
status: unknown = 502,
): ErrorWithMetadata {
const err: ErrorWithMetadata = new Error(
`failed to fetch generated image bytes: ${String(message || "unknown error")}`,
);
err.code = UPSTREAM_IMAGE_FETCH_FAILED_CODE;
err.status = 502;
const upstreamStatus = Number(status);
if (Number.isFinite(upstreamStatus)) err.upstreamStatus = upstreamStatus;
return err;
}
export function upstreamImageProviderError(code: unknown): ErrorWithMetadata {
const err: ErrorWithMetadata = new Error(
`Gemini returned image generation provider error code ${String(code || "unknown")}`,
);
err.code = UPSTREAM_IMAGE_PROVIDER_ERROR_CODE;
err.status = 502;
return err;
}
export function invalidGeminiCookieError(
cfg: CookieConfig,
status: unknown,
rawLength: number | null = null,
diagnosticReason: unknown = "",
): ErrorWithMetadata | null {
if (!cfg?.cookie || !AUTH_FAILURE_STATUSES.has(Number(status))) return null;
const reason = cookieDiagnosticMessage(diagnosticReason);
const err: ErrorWithMetadata = new Error(
`Gemini rejected the selected account credentials (upstream HTTP ${status}). ` +
(reason ? `Diagnostic: ${reason}. ` : "") +
"Update GEMINI_COOKIE with valid, unexpired Gemini web session credentials.",
);
err.code = INVALID_GEMINI_COOKIE_CODE;
err.status = 401;
err.upstreamStatus = Number(status);
err.rawLength = rawLength;
if (reason) err.reason = reason;
return err;
}
export function unverifiedGeminiCookieError(
reason = "missing Gemini page auth token",
) {
const messageReason = cookieDiagnosticMessage(reason) || reason;
const err: ErrorWithMetadata = new Error(
`Could not verify the selected Gemini account credentials (${messageReason}). ` +
"Update GEMINI_COOKIE with valid, unexpired Gemini web session credentials.",
);
err.code = INVALID_GEMINI_COOKIE_CODE;
err.status = 401;
return err;
}
export function isInvalidGeminiCookieError(e: unknown): boolean {
return (
!!e &&
typeof e === "object" &&
(e as Partial<ErrorWithMetadata>).code === INVALID_GEMINI_COOKIE_CODE
);
}
function cookieDiagnosticMessage(reason: unknown): string {
const key = String(reason || "").trim();
return COOKIE_DIAGNOSTIC_MESSAGES[key] || "";
}
|