CreateAgent.tsx
12.2 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { useCallback, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { createAgent, checkStewardExists, getPositionTags, getSkills, getEmployeeMounts } from '../api/agent';
import type { CreateAgentPayload, PositionTagItem, SkillItem, EmployeeItem } from '../api/agent';
import { Select, MultiSelect } from '../components/Select';
interface FormState {
name: string;
description: string;
positionIds: number[];
selectedSkillCodes: string[];
managedUserId: string;
isSteward: boolean;
}
const INITIAL_FORM: FormState = {
name: '',
description: '',
positionIds: [],
selectedSkillCodes: [],
managedUserId: '',
isSteward: false,
};
export function CreateAgent() {
const navigate = useNavigate();
const [form, setForm] = useState<FormState>(INITIAL_FORM);
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [result, setResult] = useState<{ agent_id: number; name: string } | null>(null);
const [stewardConflict, setStewardConflict] = useState<string | null>(null);
/* ---------- Reference Data ---------- */
const [positions, setPositions] = useState<PositionTagItem[]>([]);
const [positionsLoading, setPositionsLoading] = useState(true);
const [positionsError, setPositionsError] = useState<string | null>(null);
const [skills, setSkills] = useState<SkillItem[]>([]);
const [skillsLoading, setSkillsLoading] = useState(true);
const [skillsError, setSkillsError] = useState<string | null>(null);
const [employees, setEmployees] = useState<EmployeeItem[]>([]);
const [employeesLoading, setEmployeesLoading] = useState(true);
const [employeesError, setEmployeesError] = useState<string | null>(null);
const fetchPositions = useCallback(() => {
setPositionsLoading(true);
setPositionsError(null);
getPositionTags()
.then(setPositions)
.catch(() => setPositionsError('岗位数据加载失败'))
.finally(() => setPositionsLoading(false));
}, []);
const fetchSkills = useCallback(() => {
setSkillsLoading(true);
setSkillsError(null);
getSkills()
.then(setSkills)
.catch(() => setSkillsError('技能数据加载失败'))
.finally(() => setSkillsLoading(false));
}, []);
const fetchEmployees = useCallback(() => {
setEmployeesLoading(true);
setEmployeesError(null);
getEmployeeMounts()
.then(setEmployees)
.catch(() => setEmployeesError('员工数据加载失败'))
.finally(() => setEmployeesLoading(false));
}, []);
useEffect(() => {
fetchPositions();
fetchSkills();
fetchEmployees();
}, [fetchPositions, fetchSkills, fetchEmployees]);
/* ---------- Submit ---------- */
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
setResult(null);
if (!form.name.trim()) {
setError('请输入 Agent 名称');
return;
}
if (!form.isSteward && form.positionIds.length === 0) {
setError('请选择至少一个岗位');
return;
}
if (!form.managedUserId) {
setError('请选择托管员工');
return;
}
if (form.isSteward && stewardConflict) {
setError(stewardConflict);
return;
}
const payload: CreateAgentPayload = {
name: form.name.trim(),
description: form.description.trim() || undefined,
position_ids: form.positionIds,
skill_codes: form.selectedSkillCodes,
managed_user_id: form.managedUserId,
is_steward: form.isSteward,
};
setSubmitting(true);
try {
const res = await createAgent(payload);
setResult(res);
setForm(INITIAL_FORM);
} catch (err: unknown) {
setError(err instanceof Error ? err.message : '创建失败');
} finally {
setSubmitting(false);
}
}
/* ---------- Loading state ---------- */
if (positionsLoading || skillsLoading || employeesLoading) {
return (
<div className="page">
<div className="page-heading">
<h1>新增 Agent</h1>
<p className="page-desc">配置 Agent 的基本信息、关联岗位与技能</p>
</div>
<div className="loading-block">
<div className="loading-spinner" />
<span>正在加载参考数据...</span>
</div>
</div>
);
}
return (
<div className="page">
<div className="page-heading">
<h1>新增 Agent</h1>
<p className="page-desc">配置 Agent 的基本信息、关联岗位与技能</p>
</div>
{error && (
<div className="alert alert-error">
<span className="alert-icon">!</span>
<span>{error}</span>
</div>
)}
{result && (
<div className="alert alert-success">
<span className="alert-icon">✓</span>
<span>
Agent <code className="tag">{result.name}</code> 创建成功
<span className="badge">ID: {result.agent_id}</span>
</span>
</div>
)}
<div className="form-card">
<div className="form-card-header">
<span className="form-card-title">基本信息</span>
</div>
<form onSubmit={handleSubmit}>
<div className="form-body">
{/* Row 1: Name + Managed User */}
<div className="form-row">
<div className="form-group">
<label className="form-label">
Agent 名称 <span className="required">*</span>
</label>
<input
className="form-input"
placeholder="例:复盘 Agent"
value={form.name}
onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))}
required
/>
</div>
<div className="form-group">
<label className="form-label">
托管员工 <span className="required">*</span>
</label>
<Select
options={employees
.filter((emp) => emp.agent_count === 0)
.map((emp) => ({
value: emp.employee_id,
label: `${emp.employee_name} (${emp.employee_id})`,
subtitle: emp.wechat_user_id
? `微信号: ${emp.wechat_user_id}`
: undefined,
}))}
value={form.managedUserId}
onChange={(v) => setForm((f) => ({ ...f, managedUserId: v }))}
placeholder="选择托管员工"
searchable
error={employeesError}
onRetry={fetchEmployees}
mono
/>
</div>
</div>
{/* Description */}
<div className="form-group">
<label className="form-label">描述</label>
<textarea
className="form-input form-textarea"
placeholder="Agent 描述信息(可选)"
rows={3}
value={form.description}
onChange={(e) => setForm((f) => ({ ...f, description: e.target.value }))}
/>
</div>
{/* Steward Toggle */}
<div className="form-group">
<label className="form-label">管家 Agent</label>
<label className="toggle-switch">
<input
type="checkbox"
checked={form.isSteward}
onChange={async (e) => {
const checked = e.target.checked;
setForm((f) => ({
...f,
isSteward: checked,
positionIds: checked ? [] : f.positionIds,
selectedSkillCodes: checked ? [] : f.selectedSkillCodes,
}));
setError(null);
if (checked) {
try {
const result = await checkStewardExists();
if (result.exists) {
setStewardConflict(
`管家 Agent 已存在(${result.name},ID: ${result.agent_id}),不能重复创建`
);
} else {
setStewardConflict(null);
}
} catch {
setStewardConflict(null);
}
} else {
setStewardConflict(null);
}
}}
/>
<span className="toggle-track">
<span className="toggle-thumb" />
</span>
<span className="toggle-label">
{form.isSteward ? '是 — 该 Agent 作为管家,可调度其他 Agent' : '否 — 普通 Agent'}
</span>
</label>
{stewardConflict && (
<div className="steward-warning">{stewardConflict}</div>
)}
</div>
{/* Position Tags — Multi Select */}
<div className="form-group">
<label className="form-label">
关联岗位 {!form.isSteward && <span className="required">*</span>}
</label>
{form.isSteward ? (
<div className="form-hint warn">管家 Agent 默认关联所有岗位,无需单独配置</div>
) : (
<MultiSelect
options={positions.map((p) => ({
value: String(p.tag_id),
label: `#${p.tag_id} ${p.name}`,
subtitle: `已绑 ${p.agent_count} 个 Agent`,
}))}
value={form.positionIds.map(String)}
onChange={(v) => setForm((f) => ({ ...f, positionIds: v.map(Number) }))}
placeholder="选择关联岗位"
error={positionsError}
onRetry={fetchPositions}
/>
)}
</div>
{/* Skills — Pill Checkbox Group */}
<div className="form-group">
<label className="form-label">技能编码</label>
{form.isSteward ? (
<div className="form-hint warn">管家 Agent 不需要配置技能,技能选择已禁用</div>
) : skillsError ? (
<div className="inline-error">
<span>{skillsError}</span>
<button className="btn btn-ghost btn-sm" onClick={fetchSkills}>重试</button>
</div>
) : (
<div className="tag-checkbox-group">
{skills.map((sk) => (
<label key={sk.skill_code} className={`tag-checkbox${form.isSteward ? ' disabled' : ''}`}>
<input
type="checkbox"
disabled={form.isSteward}
checked={form.selectedSkillCodes.includes(sk.skill_code)}
onChange={() =>
setForm((f) => ({
...f,
selectedSkillCodes: f.selectedSkillCodes.includes(sk.skill_code)
? f.selectedSkillCodes.filter((c) => c !== sk.skill_code)
: [...f.selectedSkillCodes, sk.skill_code],
}))
}
/>
<span className="tag-checkbox-label" title={sk.description ?? ''}>
{sk.skill_name}
<span className="tag-checkbox-code">{sk.skill_code}</span>
</span>
</label>
))}
</div>
)}
</div>
</div>
<div className="form-footer">
<button type="submit" className="btn btn-primary" disabled={submitting}>
{submitting ? '创建中...' : '创建 Agent'}
</button>
<button type="button" className="btn btn-ghost" onClick={() => navigate('/')}>
取消
</button>
</div>
</form>
</div>
</div>
);
}