fix: 修复 KnowledgeSettingsPanel 崩溃并优化工具集复制为纯前端操作

main
sp mac bookpro 2605 2026-07-22 14:01:40 +08:00
parent 16051c1f7a
commit 7f5b7f0537
5 changed files with 31 additions and 43 deletions

View File

@ -57,6 +57,7 @@ export interface ExternalToolPlugin extends ExternalToolPluginPayload {
id: string;
enabled: number;
createdAt: number;
isTemp?: boolean;
}
export interface Agent {

View File

@ -107,7 +107,7 @@ export default function ExternalToolEditor({ open, agentId, plugin, onClose, onS
})),
};
if (plugin) {
if (plugin && !plugin.id.startsWith('temp_')) {
await AgentAPI.updatePlugin(agentId, plugin.id, payload);
message.success('外部工具更新成功');
} else {

View File

@ -213,13 +213,13 @@ export default function KnowledgeSettingsPanel({
<Space size={6} wrap className="agent-editor-plugin-meta">
<Tag>{plugin.authType || 'none'}</Tag>
<span>{plugin.baseUrl}</span>
<span>{plugin.apis.length} API</span>
<span>{plugin.apis?.length ?? 0} API</span>
</Space>
<Collapse
ghost
size="small"
className="agent-editor-plugin-apis"
items={plugin.apis.map((api) => ({
items={plugin.apis?.map((api) => ({
key: api.id || api.name,
label: (
<Space wrap>
@ -243,7 +243,7 @@ export default function KnowledgeSettingsPanel({
</div>
</div>
),
}))}
})) || []}
/>
</Card>
))}

View File

@ -245,28 +245,23 @@ export function useAgentEditor({ id, isNew, form, message, navigate }: UseAgentE
}
};
const handleCopyPlugin = async (pluginId: string) => {
const handleCopyPlugin = (pluginId: string) => {
if (!id || !agent) return;
const plugin = agent.plugins?.find((p) => p.id === pluginId);
if (!plugin) return;
try {
const copyPayload = {
const newPlugin = {
...plugin,
id: `temp_${Date.now()}`,
name: `${plugin.name}-copy`,
description: plugin.description,
baseUrl: plugin.baseUrl,
authType: plugin.authType,
authConfig: plugin.authConfig,
apis: plugin.apis.map((api) => ({
apis: plugin.apis?.map((api) => ({
...api,
name: `${api.name}-copy`,
description: api.description,
method: api.method,
path: api.path,
headers: api.headers,
parametersSchema: api.parametersSchema,
})),
id: undefined,
})) || [],
isTemp: true,
};
const newPlugin = await AgentAPI.bindPlugin(id, copyPayload as any);
setAgent((prev) => {
if (!prev) return prev;
return {
@ -274,12 +269,8 @@ export function useAgentEditor({ id, isNew, form, message, navigate }: UseAgentE
plugins: [...(prev.plugins || []), newPlugin],
};
});
message.success('外部工具复制成功');
return newPlugin;
} catch (e: any) {
message.error('外部工具复制失败:' + (e?.message ?? e));
throw e;
}
};
const beforeUploadKnowledge = async (file: any) => {

View File

@ -104,16 +104,12 @@ export default function AgentEditor() {
setEditingPluginId(null);
setExternalToolEditorOpen(true);
}}
onCopyExternalTool={async (pluginId) => {
try {
const newPlugin = await handleCopyPlugin(pluginId);
onCopyExternalTool={(pluginId) => {
const newPlugin = handleCopyPlugin(pluginId);
if (newPlugin) {
setEditingPluginId(newPlugin.id);
setExternalToolEditorOpen(true);
}
} catch (e) {
// error handled in hook
}
}}
onEditExternalTool={(pluginId) => {
setEditingPluginId(pluginId);