
























@@ -103,12 +103,46 @@ import { assertValidParams } from "./validation.js";
103103type SessionsRuntimeModule = typeof import("./sessions.runtime.js");
104104105105let sessionsRuntimeModulePromise: Promise<SessionsRuntimeModule> | undefined;
106+let loggedSlowSessionsListCatalog = false;
107+108+const SESSIONS_LIST_MODEL_CATALOG_TIMEOUT_MS = 750;
106109107110function loadSessionsRuntimeModule(): Promise<SessionsRuntimeModule> {
108111sessionsRuntimeModulePromise ??= import("./sessions.runtime.js");
109112return sessionsRuntimeModulePromise;
110113}
111114115+async function loadOptionalSessionsListModelCatalog(
116+context: GatewayRequestContext,
117+): Promise<Awaited<ReturnType<GatewayRequestContext["loadGatewayModelCatalog"]>> | undefined> {
118+let timeout: NodeJS.Timeout | undefined;
119+const timedOut = Symbol("sessions-list-model-catalog-timeout");
120+const timeoutPromise = new Promise<typeof timedOut>((resolve) => {
121+timeout = setTimeout(() => resolve(timedOut), SESSIONS_LIST_MODEL_CATALOG_TIMEOUT_MS);
122+timeout.unref?.();
123+});
124+try {
125+const result = await Promise.race([
126+context.loadGatewayModelCatalog().catch(() => undefined),
127+timeoutPromise,
128+]);
129+if (result === timedOut) {
130+if (!loggedSlowSessionsListCatalog) {
131+loggedSlowSessionsListCatalog = true;
132+context.logGateway.debug(
133+`sessions.list continuing without model catalog after ${SESSIONS_LIST_MODEL_CATALOG_TIMEOUT_MS}ms`,
134+);
135+}
136+return undefined;
137+}
138+return Array.isArray(result) ? result : undefined;
139+} finally {
140+if (timeout) {
141+clearTimeout(timeout);
142+}
143+}
144+}
145+112146function requireSessionKey(key: unknown, respond: RespondFn): string | null {
113147const raw =
114148typeof key === "string"
@@ -613,8 +647,7 @@ export const sessionsHandlers: GatewayRequestHandlers = {
613647const p = params;
614648const cfg = context.getRuntimeConfig();
615649const { storePath, store } = loadCombinedSessionStoreForGateway(cfg);
616-const loadedCatalog = await context.loadGatewayModelCatalog().catch(() => undefined);
617-const modelCatalog = Array.isArray(loadedCatalog) ? loadedCatalog : undefined;
650+const modelCatalog = await loadOptionalSessionsListModelCatalog(context);
618651const result = listSessionsFromStore({
619652 cfg,
620653 storePath,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。