fix: prevent "cannot read properties of undefined (reading 'map')" crash (#532)

This commit is contained in:
paisley
2026-03-16 19:52:01 +08:00
committed by GitHub
Unverified
parent 925fbab86d
commit 11e28a2cfa
6 changed files with 23 additions and 20 deletions

View File

@@ -182,7 +182,7 @@ export function Sidebar() {
}, [fetchAgents]); }, [fetchAgents]);
const agentNameById = useMemo( const agentNameById = useMemo(
() => Object.fromEntries(agents.map((agent) => [agent.id, agent.name])), () => Object.fromEntries((agents ?? []).map((agent) => [agent.id, agent.name])),
[agents], [agents],
); );
const sessionBuckets: Array<{ key: SessionBucketKey; label: string; sessions: typeof sessions }> = [ const sessionBuckets: Array<{ key: SessionBucketKey; label: string; sessions: typeof sessions }> = [

View File

@@ -97,11 +97,14 @@ export function buildProviderListItems(
vendors: ProviderVendorInfo[], vendors: ProviderVendorInfo[],
defaultAccountId: string | null, defaultAccountId: string | null,
): ProviderListItem[] { ): ProviderListItem[] {
const vendorMap = new Map(vendors.map((vendor) => [vendor.id, vendor])); const safeAccounts = accounts ?? [];
const statusMap = new Map(statuses.map((status) => [status.id, status])); const safeStatuses = statuses ?? [];
const safeVendors = vendors ?? [];
const vendorMap = new Map(safeVendors.map((vendor) => [vendor.id, vendor]));
const statusMap = new Map(safeStatuses.map((status) => [status.id, status]));
if (accounts.length > 0) { if (safeAccounts.length > 0) {
return accounts return safeAccounts
.map((account) => ({ .map((account) => ({
account, account,
vendor: vendorMap.get(account.vendorId), vendor: vendorMap.get(account.vendorId),
@@ -114,7 +117,7 @@ export function buildProviderListItems(
}); });
} }
return statuses.map((status) => ({ return safeStatuses.map((status) => ({
account: legacyProviderToAccount(status), account: legacyProviderToAccount(status),
vendor: vendorMap.get(status.type), vendor: vendorMap.get(status.type),
status, status,

View File

@@ -97,15 +97,15 @@ export function ChatInput({ onSend, onStop, disabled = false, sending = false, i
const agents = useAgentsStore((s) => s.agents); const agents = useAgentsStore((s) => s.agents);
const currentAgentId = useChatStore((s) => s.currentAgentId); const currentAgentId = useChatStore((s) => s.currentAgentId);
const currentAgentName = useMemo( const currentAgentName = useMemo(
() => agents.find((agent) => agent.id === currentAgentId)?.name ?? currentAgentId, () => (agents ?? []).find((agent) => agent.id === currentAgentId)?.name ?? currentAgentId,
[agents, currentAgentId], [agents, currentAgentId],
); );
const mentionableAgents = useMemo( const mentionableAgents = useMemo(
() => agents.filter((agent) => agent.id !== currentAgentId), () => (agents ?? []).filter((agent) => agent.id !== currentAgentId),
[agents, currentAgentId], [agents, currentAgentId],
); );
const selectedTarget = useMemo( const selectedTarget = useMemo(
() => agents.find((agent) => agent.id === targetAgentId) ?? null, () => (agents ?? []).find((agent) => agent.id === targetAgentId) ?? null,
[agents, targetAgentId], [agents, targetAgentId],
); );
const showAgentPicker = mentionableAgents.length > 0; const showAgentPicker = mentionableAgents.length > 0;
@@ -132,7 +132,7 @@ export function ChatInput({ onSend, onStop, disabled = false, sending = false, i
setPickerOpen(false); setPickerOpen(false);
return; return;
} }
if (!agents.some((agent) => agent.id === targetAgentId)) { if (!(agents ?? []).some((agent) => agent.id === targetAgentId)) {
setTargetAgentId(null); setTargetAgentId(null);
setPickerOpen(false); setPickerOpen(false);
} }

View File

@@ -21,7 +21,7 @@ export function ChatToolbar() {
const agents = useAgentsStore((s) => s.agents); const agents = useAgentsStore((s) => s.agents);
const { t } = useTranslation('chat'); const { t } = useTranslation('chat');
const currentAgentName = useMemo( const currentAgentName = useMemo(
() => agents.find((agent) => agent.id === currentAgentId)?.name ?? currentAgentId, () => (agents ?? []).find((agent) => agent.id === currentAgentId)?.name ?? currentAgentId,
[agents, currentAgentId], [agents, currentAgentId],
); );

View File

@@ -22,11 +22,11 @@ interface AgentsState {
function applySnapshot(snapshot: AgentsSnapshot | undefined) { function applySnapshot(snapshot: AgentsSnapshot | undefined) {
return snapshot ? { return snapshot ? {
agents: snapshot.agents, agents: snapshot.agents ?? [],
defaultAgentId: snapshot.defaultAgentId, defaultAgentId: snapshot.defaultAgentId ?? 'main',
configuredChannelTypes: snapshot.configuredChannelTypes, configuredChannelTypes: snapshot.configuredChannelTypes ?? [],
channelOwners: snapshot.channelOwners, channelOwners: snapshot.channelOwners ?? {},
channelAccountOwners: snapshot.channelAccountOwners, channelAccountOwners: snapshot.channelAccountOwners ?? {},
} : {}; } : {};
} }

View File

@@ -82,10 +82,10 @@ export const useProviderStore = create<ProviderState>((set, get) => ({
const snapshot = await fetchProviderSnapshot(); const snapshot = await fetchProviderSnapshot();
set({ set({
statuses: snapshot.statuses, statuses: snapshot.statuses ?? [],
accounts: snapshot.accounts, accounts: snapshot.accounts ?? [],
vendors: snapshot.vendors, vendors: snapshot.vendors ?? [],
defaultAccountId: snapshot.defaultAccountId, defaultAccountId: snapshot.defaultAccountId ?? null,
loading: false loading: false
}); });
} catch (error) { } catch (error) {