DebugForm.tsx
9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { useState, useEffect, useCallback } from 'react';
import { Select, Radio, Input, Button, Form, message, Tag, Space } from 'antd';
import { ThunderboltOutlined, ClockCircleOutlined } from '@ant-design/icons';
import { accountService } from '../../services/accountService';
import { promptService } from '../../services/promptService';
import { orchestrationService } from '../../services/orchestrationService';
import type { CallResultData } from './DebugConsole';
interface AccountOption {
id: string;
account_name: string;
code: string;
model_name: string;
}
interface PromptOption {
id: string;
title: string;
code: string;
template_type: string;
}
interface DebugFormProps {
onResult: (data: CallResultData) => void;
setLoading: (v: boolean) => void;
}
export default function DebugForm({ onResult, setLoading }: DebugFormProps) {
const [form] = Form.useForm();
const [accounts, setAccounts] = useState<AccountOption[]>([]);
const [prompts, setPrompts] = useState<PromptOption[]>([]);
const [callMode, setCallMode] = useState<'sync' | 'async'>('sync');
const [loadingAccounts, setLoadingAccounts] = useState(false);
const [loadingPrompts, setLoadingPrompts] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [promptVariables, setPromptVariables] = useState<Array<{ name: string; type: string }>>([]);
const [selectedPromptId, setSelectedPromptId] = useState<string | null>(null);
// 加载账号列表
const loadAccounts = useCallback(async () => {
setLoadingAccounts(true);
try {
const res = await accountService.list({ page_size: 100 });
setAccounts(
res.items.map((a) => ({
id: a.id,
account_name: a.account_name,
code: a.code,
model_name: a.model_name,
}))
);
} catch {
message.error('加载账号列表失败');
} finally {
setLoadingAccounts(false);
}
}, []);
// 加载提示词模板列表
const loadPrompts = useCallback(async () => {
setLoadingPrompts(true);
try {
const res = await promptService.list({ page_size: 100 });
setPrompts(
res.items.map((p) => ({
id: p.id,
title: p.title,
code: p.code,
template_type: p.template_type,
}))
);
} catch {
message.error('加载提示词模板失败');
} finally {
setLoadingPrompts(false);
}
}, []);
useEffect(() => {
loadAccounts();
loadPrompts();
}, [loadAccounts, loadPrompts]);
// 从文本中提取 {{变量名}}
const extractVarsFromText = (text: string): string[] => {
const matches = text.match(/\{\{(\w+)\}\}/g);
if (!matches) return [];
return [...new Set(matches.map((m) => m.replace(/\{|\}/g, '')))];
};
// 选择模板后加载变量
const handlePromptSelect = async (promptId: string | undefined) => {
if (!promptId) {
setSelectedPromptId(null);
setPromptVariables([]);
return;
}
setSelectedPromptId(promptId);
try {
const detail = await promptService.getById(promptId);
// 优先使用数据库中保存的 variables 字段
if (detail.variables && detail.variables.length > 0) {
setPromptVariables(detail.variables);
} else {
// 兜底:从模板文本中自动提取 {{变量}}
const allVars = [
...extractVarsFromText(detail.system_prompt || ''),
...extractVarsFromText(detail.user_prompt || ''),
];
const uniqueVars = [...new Set(allVars)];
setPromptVariables(uniqueVars.map((name) => ({ name, type: 'string' })));
}
} catch {
message.error('加载模板详情失败');
}
};
// 提交调用
const handleSubmit = async () => {
try {
const values = await form.validateFields();
setSubmitting(true);
setLoading(true);
// Build variables from form
const variables: Record<string, string> = {};
if (values.variables) {
for (const [key, val] of Object.entries(values.variables)) {
variables[key] = String(val || '');
}
}
const result = await orchestrationService.byTemplate({
template_id: values.template_id,
account_id: values.account_id,
variables: Object.keys(variables).length > 0 ? variables : undefined,
mode: callMode,
});
if (callMode === 'sync') {
const syncRes = result as { status: string; model_name: string; call_record_id: string; account_id: string; response_content: string | null };
onResult({
status: syncRes.status === 'success' ? 'success' : 'error',
callType: 'sync',
responseBody: null,
responseContent: syncRes.response_content,
executionLog: null,
modelName: syncRes.model_name,
accountId: syncRes.account_id,
});
message.success('调用完成');
} else {
const asyncRes = result as { status: string; task_id: string; call_record_id: string };
onResult({
status: 'accepted',
callType: 'async',
responseBody: null,
responseContent: null,
executionLog: null,
taskId: asyncRes.task_id,
});
message.info('异步任务已提交');
}
} catch (err: unknown) {
const errMsg =
(err as any)?.response?.data?.detail ||
(err as Error)?.message ||
'调用失败';
message.error(errMsg);
onResult({
status: 'error',
callType: callMode,
responseBody: null,
responseContent: null,
executionLog: null,
errorMessage: errMsg,
});
} finally {
setSubmitting(false);
setLoading(false);
}
};
return (
<Form
form={form}
layout="vertical"
size="middle"
onFinish={handleSubmit}
>
{/* Step 1: Account */}
<div className="debug-section">
<div className="debug-section-header">
<span className="debug-section-badge">1</span>
<span>选择账号</span>
</div>
<Form.Item
name="account_id"
label="调用账号"
rules={[{ required: true, message: '请选择账号' }]}
>
<Select
placeholder="选择调用账号"
loading={loadingAccounts}
showSearch
allowClear
optionFilterProp="label"
options={accounts.map((a) => ({
label: `${a.account_name} (${a.code}) — ${a.model_name}`,
value: a.id,
}))}
/>
</Form.Item>
</div>
{/* Step 2: Call Mode */}
<div className="debug-section">
<div className="debug-section-header">
<span className="debug-section-badge">2</span>
<span>调用模式</span>
</div>
<Form.Item label="调用方式">
<Radio.Group value={callMode} onChange={(e) => setCallMode(e.target.value)}>
<Radio value="sync">
<Space>
<ThunderboltOutlined />
同步调用
</Space>
</Radio>
<Radio value="async">
<Space>
<ClockCircleOutlined />
异步调用
</Space>
</Radio>
</Radio.Group>
</Form.Item>
</div>
{/* Step 3: Template */}
<div className="debug-section">
<div className="debug-section-header">
<span className="debug-section-badge">3</span>
<span>提示词模板</span>
</div>
<Form.Item
name="template_id"
label="选择模板"
rules={[{ required: true, message: '请选择提示词模板' }]}
>
<Select
placeholder="选择提示词模板"
loading={loadingPrompts}
showSearch
allowClear
optionFilterProp="label"
onChange={handlePromptSelect}
options={prompts.map((p) => ({
label: `${p.title} (${p.code}) — ${p.template_type}`,
value: p.id,
}))}
/>
</Form.Item>
{/* Template Variables */}
{promptVariables.length > 0 && (
<div style={{ marginTop: 8 }}>
<div style={{ fontSize: 13, color: '#64748b', marginBottom: 8 }}>
模板变量
</div>
{promptVariables.map((v) => (
<Form.Item
key={v.name}
name={['variables', v.name]}
label={
<Space size={4}>
<Tag color="blue">{v.name}</Tag>
<span style={{ fontSize: 12, color: '#888' }}>({v.type})</span>
</Space>
}
style={{ marginBottom: 8 }}
>
<Input placeholder={`输入 ${v.name}`} />
</Form.Item>
))}
</div>
)}
{selectedPromptId && promptVariables.length === 0 && (
<div style={{ padding: '8px 12px', background: '#f8fafc', borderRadius: 6, fontSize: 13, color: '#94a3b8' }}>
该模板无需变量
</div>
)}
</div>
{/* Submit */}
<div className="debug-actions">
<Button
type="primary"
htmlType="submit"
loading={submitting}
icon={callMode === 'sync' ? <ThunderboltOutlined /> : <ClockCircleOutlined />}
size="large"
block
>
{callMode === 'sync' ? '同步调用' : '异步调用'}
</Button>
</div>
</Form>
);
}