
























@@ -3,6 +3,11 @@ import { parseGeminiAuth } from "../../infra/gemini-auth.js";
33import { normalizeGoogleApiBaseUrl } from "../../infra/google-api-base-url.js";
44import { streamWithPayloadPatch } from "../../llm/providers/stream-wrappers/stream-payload-utils.js";
55import type { Model } from "../../llm/types.js";
6+import {
7+asDateTimestampMs,
8+isFutureDateTimestampMs,
9+resolveExpiresAtMsFromDurationMs,
10+} from "../../shared/number-coercion.js";
611import { normalizeOptionalString } from "../../shared/string-coerce.js";
712import { buildGuardedModelFetch } from "../provider-transport-fetch.js";
813import type { StreamFn } from "../runtime/index.js";
@@ -185,8 +190,7 @@ function parseExpireTimeMs(expireTime: string | undefined): number | null {
185190if (!expireTime) {
186191return null;
187192}
188-const timestamp = Date.parse(expireTime);
189-return Number.isFinite(timestamp) ? timestamp : null;
193+return asDateTimestampMs(Date.parse(expireTime)) ?? null;
190194}
191195192196function convertManagedGoogleTools(tools: NonNullable<GooglePromptCacheContext["tools"]>) {
@@ -342,7 +346,10 @@ async function ensureGooglePromptCache(
342346deps: GooglePromptCacheDeps,
343347): Promise<string | null> {
344348const baseUrl = normalizeGoogleApiBaseUrl(params.model.baseUrl);
345-const now = deps.now?.() ?? Date.now();
349+const now = asDateTimestampMs(deps.now?.() ?? Date.now());
350+if (now === undefined) {
351+return null;
352+}
346353const systemPromptDigest = digestSystemPrompt(params.systemPrompt);
347354const matchKey = buildGooglePromptCacheMatchKey({
348355provider: params.provider,
@@ -354,15 +361,18 @@ async function ensureGooglePromptCache(
354361});
355362const latestEntry = readLatestGooglePromptCacheEntry(params.sessionManager, matchKey);
356363357-if (latestEntry?.status === "failed" && latestEntry.retryAfter > now) {
364+if (
365+latestEntry?.status === "failed" &&
366+isFutureDateTimestampMs(latestEntry.retryAfter, { nowMs: now })
367+) {
358368return null;
359369}
360370361371const fetchImpl = (deps.buildGuardedFetch ?? buildGuardedModelFetch)(params.model);
362372const refreshWindowMs = resolveGooglePromptCacheRefreshWindowMs(params.cacheRetention);
363373if (latestEntry?.status === "ready" && latestEntry.cachedContent) {
364374const expiresAt = parseExpireTimeMs(latestEntry.expireTime);
365-const isExpired = expiresAt !== null && expiresAt <= now;
375+const isExpired = expiresAt !== null && !isFutureDateTimestampMs(expiresAt, { nowMs: now });
366376if (!isExpired) {
367377const needsRefresh = expiresAt !== null && expiresAt - now <= refreshWindowMs;
368378if (!needsRefresh) {
@@ -420,7 +430,8 @@ async function ensureGooglePromptCache(
420430 systemPromptDigest,
421431cacheConfigDigest: params.cacheConfigDigest,
422432cacheRetention: params.cacheRetention,
423-retryAfter: now + GOOGLE_PROMPT_CACHE_RETRY_BACKOFF_MS,
433+retryAfter:
434+resolveExpiresAtMsFromDurationMs(GOOGLE_PROMPT_CACHE_RETRY_BACKOFF_MS, { nowMs: now }) ?? 0,
424435});
425436return null;
426437}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。