feat: 支持外部工具配置使用对象格式 (JS object literal)

main
sp mac bookpro 2605 2026-07-19 23:24:57 +08:00
parent 86c923cc40
commit bcb226c98b
1 changed files with 22 additions and 10 deletions

View File

@ -30,16 +30,28 @@ const EMPTY_API: ToolApiFormValue = {
}; };
function parseJsonObject(value: string | undefined, fieldName: string, optional = false) { function parseJsonObject(value: string | undefined, fieldName: string, optional = false) {
if (!value?.trim()) { const trimmedValue = value?.trim();
if (!trimmedValue) {
if (optional) return undefined; if (optional) return undefined;
throw new Error(`${fieldName}不能为空`); throw new Error(`${fieldName}不能为空`);
} }
const parsed = JSON.parse(value); try {
if (!parsed || Array.isArray(parsed) || typeof parsed !== 'object') { // 优先尝试标准 JSON 解析
throw new Error(`${fieldName}必须是 JSON 对象`); return JSON.parse(trimmedValue);
} catch (e) {
try {
// 失败后尝试作为 JS 对象解析 (支持无引号、单引号等)
// eslint-disable-next-line no-new-func
const parsed = new Function(`return (${trimmedValue})`)();
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
return parsed;
}
throw new Error();
} catch (e2) {
throw new Error(`${fieldName}格式不正确,请确保是有效的 JSON 或对象格式`);
}
} }
return parsed;
} }
export default function ExternalToolEditor({ open, agentId, plugin, onClose, onSaved }: Props) { export default function ExternalToolEditor({ open, agentId, plugin, onClose, onSaved }: Props) {
@ -78,7 +90,7 @@ export default function ExternalToolEditor({ open, agentId, plugin, onClose, onS
} catch (error: any) { } catch (error: any) {
if (error?.errorFields) return; if (error?.errorFields) return;
if (error instanceof SyntaxError) { if (error instanceof SyntaxError) {
message.error('JSON 格式不正确,请检查认证配置、请求头或依赖参数'); message.error('配置格式不正确,请检查认证配置、请求头或依赖参数(支持 JSON 或对象格式)');
return; return;
} }
message.error(error?.message || '外部工具绑定失败'); message.error(error?.message || '外部工具绑定失败');
@ -149,8 +161,8 @@ export default function ExternalToolEditor({ open, agentId, plugin, onClose, onS
]} ]}
/> />
</Form.Item> </Form.Item>
<Form.Item label="认证配置JSON" name="authConfig"> <Form.Item label="认证配置JSON 或对象" name="authConfig">
<Input.TextArea rows={3} className="agent-editor-code-input" placeholder={'{\n "token": "YOUR_API_KEY"\n}'} /> <Input.TextArea rows={3} className="agent-editor-code-input" placeholder={'{\n token: "YOUR_API_KEY"\n}'} />
</Form.Item> </Form.Item>
</div> </div>
@ -207,11 +219,11 @@ export default function ExternalToolEditor({ open, agentId, plugin, onClose, onS
<Input placeholder="/v1/products/hot-selling" /> <Input placeholder="/v1/products/hot-selling" />
</Form.Item> </Form.Item>
<div className="agent-editor-tool-grid"> <div className="agent-editor-tool-grid">
<Form.Item label="headers请求头 JSON" name={[field.name, 'headers']}> <Form.Item label="headers请求头 JSON 或对象" name={[field.name, 'headers']}>
<Input.TextArea rows={7} className="agent-editor-code-input" placeholder={'{\n "X-Custom-Source": "aura-agent"\n}'} /> <Input.TextArea rows={7} className="agent-editor-code-input" placeholder={'{\n "X-Custom-Source": "aura-agent"\n}'} />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label="parametersSchema依赖参数 JSON Schema" label="parametersSchema依赖参数 JSON 或对象"
name={[field.name, 'parametersSchema']} name={[field.name, 'parametersSchema']}
rules={[{ required: true, message: '请输入依赖参数 Schema' }]} rules={[{ required: true, message: '请输入依赖参数 Schema' }]}
> >