






















@@ -15,6 +15,7 @@ import {
1515loadCron,
1616refreshActiveTab,
1717setLastActiveSessionKey,
18+syncUrlWithSessionKey,
1819} from "./app-settings.ts";
1920import { handleAgentEvent, resetToolStream, type AgentEventPayload } from "./app-tool-stream.ts";
2021import { shouldReloadHistoryForFinalEvent } from "./chat-event-reload.ts";
@@ -58,6 +59,7 @@ import {
5859} from "./gateway.ts";
5960import { GatewayBrowserClient } from "./gateway.ts";
6061import type { Tab } from "./navigation.ts";
62+import { buildAgentMainSessionKey, normalizeAgentId, parseAgentSessionKey } from "./session-key.ts";
6163import type { UiSettings } from "./storage.ts";
6264import type {
6365AgentsListResult,
@@ -401,6 +403,60 @@ function applySessionDefaults(host: GatewayHost, defaults?: SessionDefaultsSnaps
401403}
402404}
403405406+function resolveMainSessionFallback(host: GatewayHost): string {
407+const snapshot = host.hello?.snapshot as
408+| { sessionDefaults?: SessionDefaultsSnapshot }
409+| undefined;
410+const mainSessionKey = snapshot?.sessionDefaults?.mainSessionKey?.trim();
411+if (mainSessionKey) {
412+return mainSessionKey;
413+}
414+const configuredMainKey =
415+snapshot?.sessionDefaults?.mainKey?.trim() || host.agentsList?.mainKey?.trim();
416+if (configuredMainKey && parseAgentSessionKey(configuredMainKey)) {
417+return configuredMainKey;
418+}
419+const defaultAgentId = host.agentsList?.defaultId?.trim() || "main";
420+return buildAgentMainSessionKey({
421+agentId: defaultAgentId,
422+mainKey: configuredMainKey,
423+});
424+}
425+426+function fallbackUnconfiguredSessionSelection(host: GatewayHost) {
427+const parsed = parseAgentSessionKey(host.sessionKey);
428+if (!parsed) {
429+return;
430+}
431+const configuredAgentIds = new Set(
432+(host.agentsList?.agents ?? []).map((entry) => normalizeAgentId(entry.id)),
433+);
434+if (configuredAgentIds.size === 0 || configuredAgentIds.has(normalizeAgentId(parsed.agentId))) {
435+return;
436+}
437+const nextSessionKey = resolveMainSessionFallback(host);
438+host.sessionKey = nextSessionKey;
439+applySettings(host as unknown as Parameters<typeof applySettings>[0], {
440+ ...host.settings,
441+sessionKey: nextSessionKey,
442+lastActiveSessionKey: nextSessionKey,
443+});
444+syncUrlWithSessionKey(
445+host as unknown as Parameters<typeof syncUrlWithSessionKey>[0],
446+nextSessionKey,
447+true,
448+);
449+}
450+451+async function loadAgentsThenRefreshActiveTab(host: GatewayHost) {
452+try {
453+await loadAgents(host as unknown as AgentsState);
454+fallbackUnconfiguredSessionSelection(host);
455+} finally {
456+await refreshActiveTab(host as unknown as Parameters<typeof refreshActiveTab>[0]);
457+}
458+}
459+404460export function connectGateway(host: GatewayHost, options?: ConnectGatewayOptions) {
405461const shutdownHost = host as GatewayHostWithShutdownMessage;
406462const reconnectReason = options?.reason ?? "initial";
@@ -486,11 +542,10 @@ export function connectGateway(host: GatewayHost, options?: ConnectGatewayOption
486542if (host.tab !== "chat") {
487543void refreshChatAvatar(host as unknown as Parameters<typeof refreshChatAvatar>[0]);
488544}
489-void loadAgents(host as unknown as AgentsState);
490545void loadHealthState(host as unknown as HealthState);
491546void loadNodes(host as unknown as NodesState, { quiet: true });
492547void loadDevices(host as unknown as DevicesState, { quiet: true });
493-void refreshActiveTab(host as unknown as Parameters<typeof refreshActiveTab>[0]);
548+void loadAgentsThenRefreshActiveTab(host);
494549// Re-run push reconciliation now that the gateway client is available.
495550void host.reconcileWebPushState?.();
496551void verifyPendingUpdateVersion(host, client);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。