fix(agents): reload empty agent definitions in menu

## Bug 详情
生产构建后执行交互式 /agents 时,菜单可能从 AppState 读到空的 agentDefinitions,导致显示 No agents found;同一环境下 bun run dev 可以正常显示 agents。

## 根因
生产 bundle 中底层 agent loader 和内置 agents 均可用,但 /agents 动态菜单只消费启动时注入的 AppState.agentDefinitions。构建产物复现时该状态可能为空,菜单没有现场刷新兜底。

## 修复方案
在 AgentsMenu 打开时检测 allAgents 和 activeAgents 是否同时为空;为空时基于当前 cwd 调用 getAgentDefinitionsWithOverrides 重新加载并写回 AppState,正常已有 agents 的路径直接跳过。

## 变更要点
- 为 AgentsMenu 增加空 agentDefinitions 的 fallback reload
- 使用取消标记避免组件卸载后继续写状态
- 保持现有编辑、删除和正常 dev 路径不变

## 自测
- bun run build
- ./dist/cli-bun.js agents 可列出 27 active agents
- bun run typecheck 仍存在仓库既有类型错误,未新增 AgentsMenu 相关错误
This commit is contained in:
IronRookieCoder 2026-05-16 10:48:31 +08:00
parent fd55d42048
commit aaeb662bee

View File

@ -1,6 +1,6 @@
import chalk from 'chalk';
import * as React from 'react';
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import type { SettingSource } from 'src/utils/settings/constants.js';
import type { CommandResultDisplay } from '../../commands.js';
import { useExitOnCtrlCDWithKeybindings } from '../../hooks/useExitOnCtrlCDWithKeybindings.js';
@ -15,7 +15,9 @@ import {
import {
type AgentDefinition,
getActiveAgentsFromList,
getAgentDefinitionsWithOverrides,
} from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js';
import { getCwd } from '../../utils/cwd.js';
import { toError } from '../../utils/errors.js';
import { logError } from '../../utils/log.js';
import { Select } from '../CustomSelect/select.js';
@ -50,6 +52,28 @@ export function AgentsMenu({ tools, onExit }: Props): React.ReactNode {
useExitOnCtrlCDWithKeybindings();
useEffect(() => {
if (allAgents.length > 0 || agents.length > 0) return;
let cancelled = false;
void getAgentDefinitionsWithOverrides(getCwd())
.then(freshAgentDefinitions => {
if (cancelled) return;
if (freshAgentDefinitions.allAgents.length === 0 && freshAgentDefinitions.activeAgents.length === 0) return;
setAppState(state => ({
...state,
agentDefinitions: freshAgentDefinitions,
}));
})
.catch(error => {
logError(toError(error));
});
return () => {
cancelled = true;
};
}, [allAgents.length, agents.length, setAppState]);
const agentsBySource: Record<SettingSource | 'all' | 'built-in' | 'plugin', AgentDefinition[]> = useMemo(
() => ({
'built-in': allAgents.filter(a => a.source === 'built-in'),