CallAssemblyDetail.tsx
25 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import { useEffect, useMemo } from 'react';
import { Drawer, Spin, Tag, Descriptions, Divider, Empty, message } from 'antd';
import {
ThunderboltOutlined,
FileTextOutlined,
ApartmentOutlined,
CodeOutlined,
SwapOutlined,
CheckCircleOutlined,
CloseCircleOutlined,
ClockCircleOutlined,
SafetyOutlined,
CopyOutlined,
EyeOutlined,
} from '@ant-design/icons';
import { useCallRecordStore } from '../../stores/callRecordStore';
import { TEMPLATE_TYPE_OPTIONS } from '../../types';
const TEMPLATE_TYPE_MAP = Object.fromEntries(
TEMPLATE_TYPE_OPTIONS.map((opt) => [opt.value, opt.label]),
);
const CALL_STATUS_MAP: Record<string, { label: string; color: string }> = {
pending: { label: '待调度', color: 'default' },
success: { label: '成功', color: 'success' },
failed: { label: '失败', color: 'error' },
};
interface Props {
open: boolean;
callId: string | null;
onClose: () => void;
}
/** 递归收集 JSON 对象中所有字符串值 */
function collectStrings(obj: unknown): string[] {
const result: string[] = [];
function walk(val: unknown) {
if (typeof val === 'string') {
result.push(val);
} else if (Array.isArray(val)) {
val.forEach(walk);
} else if (val && typeof val === 'object') {
Object.values(val).forEach(walk);
}
}
walk(obj);
return result;
}
/** 从模板文本和最终文本中提取变量值 */
function extractFromText(template: string, resolved: string): Record<string, string> | null {
const placeholders = [...template.matchAll(/\{\{(\w+)\}\}/g)];
if (placeholders.length === 0) return null;
// 构建正则:将非变量部分转义,变量部分替换为捕获组
let pattern = '';
let lastIndex = 0;
const varNames: string[] = [];
for (const m of placeholders) {
// 转义变量前的普通文本
pattern += escapeRegExp(template.slice(lastIndex, m.index));
pattern += '(.+)';
varNames.push(m[1]);
lastIndex = m.index! + m[0].length;
}
// 变量后的剩余文本
pattern += escapeRegExp(template.slice(lastIndex));
const regex = new RegExp(pattern, 's');
const match = regex.exec(resolved);
if (!match) return null;
const vars: Record<string, string> = {};
for (let i = 0; i < varNames.length; i++) {
const value = match[i + 1];
if (value !== undefined) {
vars[varNames[i]] = value;
}
}
return vars;
}
/** 转义正则特殊字符 */
function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export default function CallAssemblyDetail({ open, callId, onClose }: Props) {
const { detail, detailLoading, getCallDetail } = useCallRecordStore();
useEffect(() => {
if (open && callId) {
getCallDetail(callId);
}
}, [open, callId, getCallDetail]);
const record = detail;
/** 从模板内容中反向提取变量值 */
const extractedVariables = useMemo(() => {
if (!record?.template_info || !record?.request_body) return null;
const { system_prompt, user_prompt } = record.template_info;
// 收集所有提示词文本
const templateTexts: string[] = [];
if (system_prompt) templateTexts.push(system_prompt);
if (user_prompt) templateTexts.push(user_prompt);
// 从 request_body 中提取所有字符串值作为可能的目标
const allStrings = collectStrings(record.request_body);
// 对每个模板文本尝试提取变量
const variables: Record<string, string> = {};
for (const template of templateTexts) {
// 找出所有 {{xxx}}
const placeholders = [...template.matchAll(/\{\{(\w+)\}\}/g)];
if (placeholders.length === 0) continue;
// 为已知的变量名匹配
for (const resolved of allStrings) {
const extracted = extractFromText(template, resolved);
if (extracted && Object.keys(extracted).length > 0) {
Object.assign(variables, extracted);
}
}
}
return Object.keys(variables).length > 0 ? variables : null;
}, [record]);
/** 将实际换行转为字面 \n,双引号转义为 \",保证复制到 Apifox 作为双引号包裹的值不报错 */
const normalizeValue = (value: string) => value.replace(/\n/g, '\\n').replace(/"/g, '\\"');
const handleCopy = (value: string) => {
navigator.clipboard.writeText(normalizeValue(value)).then(() => {
message.success('已复制');
});
};
const handleCopyRequest = () => {
if (record?.request_body) {
navigator.clipboard.writeText(JSON.stringify(record.request_body, null, 2)).then(() => {
message.success('请求体已复制');
});
}
};
return (
<Drawer
title={
<span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<ThunderboltOutlined style={{ color: 'var(--color-primary)' }} />
<span>调用详情</span>
{record && (
<Tag color="blue" style={{ fontSize: 11 }}>{record.model_name}</Tag>
)}
</span>
}
open={open}
onClose={onClose}
width={800}
destroyOnClose
className="call-assembly-drawer"
>
<Spin spinning={detailLoading}>
{record && (
<>
{/* 基本信息 */}
<div className="assembly-section">
<div className="assembly-section-header">
<ApartmentOutlined style={{ color: 'var(--color-primary)' }} />
<span className="assembly-section-title">基本信息</span>
</div>
<Descriptions column={2} size="small" className="assembly-descriptions">
<Descriptions.Item label="模型名称">
<span style={{ fontWeight: 500 }}>{record.model_name}</span>
</Descriptions.Item>
<Descriptions.Item label="账号">
{record.account_name || '-'}
</Descriptions.Item>
<Descriptions.Item label="供应商">
{record.provider_name || '-'}
</Descriptions.Item>
<Descriptions.Item label="业务编码">
{record.biz_type ? <Tag>{record.biz_type}</Tag> : '-'}
</Descriptions.Item>
<Descriptions.Item label="状态">
<Tag color={CALL_STATUS_MAP[record.status]?.color || 'default'}>
{CALL_STATUS_MAP[record.status]?.label || record.status}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="耗时">
<code style={{ fontFamily: "'JetBrains Mono', monospace" }}>
{(record.duration_ms / 1000).toFixed(2)}s
</code>
</Descriptions.Item>
<Descriptions.Item label="Prompt Tokens">
<code style={{ fontFamily: "'JetBrains Mono', monospace" }}>
{record.prompt_tokens.toLocaleString()}
</code>
</Descriptions.Item>
<Descriptions.Item label="Completion Tokens">
<code style={{ fontFamily: "'JetBrains Mono', monospace" }}>
{record.completion_tokens.toLocaleString()}
</code>
</Descriptions.Item>
<Descriptions.Item label="总 Tokens">
<code style={{ fontFamily: "'JetBrains Mono', monospace", fontWeight: 600 }}>
{record.total_tokens.toLocaleString()}
</code>
</Descriptions.Item>
<Descriptions.Item label="调用时间">
<code style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: 12 }}>
{new Date(record.called_at).toLocaleString('zh-CN')}
</code>
</Descriptions.Item>
{record.error_message && (
<Descriptions.Item label="错误信息" span={2}>
<pre style={{ color: 'var(--color-error)', margin: 0, fontSize: 12 }}>
{record.error_message}
</pre>
</Descriptions.Item>
)}
</Descriptions>
</div>
<Divider style={{ margin: '12px 0' }} />
{/* 组装过程 */}
<div className="assembly-section">
<div className="assembly-section-header">
<SwapOutlined style={{ color: 'var(--color-primary)' }} />
<span className="assembly-section-title">组装过程</span>
</div>
<div className="assembly-flow">
{/* Step 1: 提示词模板 */}
<div className="flow-step">
<div className="flow-step-number">1</div>
<div className="flow-step-content">
<div className="flow-step-title">
<FileTextOutlined />
<span>提示词模板</span>
{record.template_info ? (
<Tag color="purple" style={{ marginLeft: 8, fontSize: 10 }}>
v{record.template_info.version}
</Tag>
) : (
<Tag style={{ marginLeft: 8, fontSize: 10 }}>未关联</Tag>
)}
</div>
{record.template_info ? (
<div className="flow-step-body">
<div className="template-info-row">
<label>标题</label>
<span>{record.template_info.title}</span>
</div>
<div className="template-info-row">
<label>编码</label>
<code>{record.template_info.code}</code>
</div>
<div className="template-info-row">
<label>类型</label>
<Tag>{TEMPLATE_TYPE_MAP[record.template_info.template_type] || record.template_info.template_type}</Tag>
</div>
<div className="template-info-row">
<label>System Prompt</label>
<pre className="flow-pre">{record.template_info.system_prompt}</pre>
</div>
{record.template_info.user_prompt && (
<div className="template-info-row">
<label>User Prompt</label>
<pre className="flow-pre">{record.template_info.user_prompt}</pre>
</div>
)}
</div>
) : (
<div className="flow-step-empty">
<ClockCircleOutlined />
<span>未关联提示词模板</span>
</div>
)}
</div>
</div>
{/* Step 2: 输入变量 */}
{extractedVariables && (
<div className="flow-step">
<div className="flow-step-number">2</div>
<div className="flow-step-content">
<div className="flow-step-title">
<EyeOutlined style={{ color: '#6366f1' }} />
<span>输入变量</span>
<Tag color="geekblue" style={{ fontSize: 10 }}>
{Object.keys(extractedVariables).length} 项
</Tag>
</div>
<div className="flow-step-body" style={{ background: '#f0f0ff', borderColor: '#d1d5fc' }}>
<div className="variables-grid">
{Object.entries(extractedVariables).map(([key, value]) => (
<div className="variable-card" key={key}>
<div className="variable-card-header">
<code className="variable-name">{`{{${key}}}`}</code>
<button
className="variable-copy-btn"
onClick={() => handleCopy(String(value))}
title="复制值"
>
<CopyOutlined />
</button>
</div>
<div className="variable-card-value">{normalizeValue(String(value))}</div>
</div>
))}
</div>
</div>
</div>
</div>
)}
{/* Step 3: 敏感词替换 */}
<div className="flow-step">
<div className="flow-step-number">3</div>
<div className="flow-step-content">
<div className="flow-step-title">
<SafetyOutlined style={{ color: 'var(--color-success)' }} />
<span>敏感词替换</span>
{record.sensitive_replacements && record.sensitive_replacements.length > 0 ? (
<Tag color="success" style={{ fontSize: 10 }}>替换 {record.sensitive_replacements.length} 项</Tag>
) : (
<Tag style={{ fontSize: 10 }}>无需替换</Tag>
)}
</div>
{record.sensitive_replacements && record.sensitive_replacements.length > 0 ? (
<div className="flow-step-body" style={{ background: '#f6ffed', borderColor: '#b7eb8f' }}>
<table className="replacement-table">
<thead>
<tr>
<th>原始词</th>
<th>替换为</th>
</tr>
</thead>
<tbody>
{record.sensitive_replacements.map((r, i) => (
<tr key={i}>
<td>
<code className="replacement-word">{r.word}</code>
</td>
<td>
<span className="replacement-arrow">→</span>
<code className="replacement-target">{r.replacement}</code>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<div className="flow-step-empty" style={{ background: '#f6ffed', borderColor: '#b7eb8f', borderStyle: 'solid' }}>
<CheckCircleOutlined style={{ color: '#52c41a' }} />
<span style={{ color: '#52c41a' }}>无敏感词被替换</span>
</div>
)}
</div>
</div>
{/* Step 4: 最终请求体 */}
<div className="flow-step">
<div className="flow-step-number">4</div>
<div className="flow-step-content">
<div className="flow-step-title">
<CodeOutlined />
<span>最终请求体</span>
{record.request_body && (
<button className="request-copy-btn" onClick={handleCopyRequest} title="复制请求体">
<CopyOutlined /> 复制
</button>
)}
</div>
{record.request_body ? (
<pre className="flow-pre json">{JSON.stringify(record.request_body, null, 2)}</pre>
) : (
<div className="flow-step-empty">无请求体</div>
)}
</div>
</div>
{/* Step 5: 调用结果 */}
<div className="flow-step">
<div className="flow-step-number">5</div>
<div className="flow-step-content">
<div className="flow-step-title">
{record.status === 'success' ? (
<CheckCircleOutlined style={{ color: 'var(--color-success)' }} />
) : record.status === 'failed' ? (
<CloseCircleOutlined style={{ color: 'var(--color-error)' }} />
) : (
<ClockCircleOutlined />
)}
<span>调用结果</span>
<Tag
color={CALL_STATUS_MAP[record.status]?.color || 'default'}
style={{ marginLeft: 8 }}
>
{CALL_STATUS_MAP[record.status]?.label || record.status}
</Tag>
</div>
{record.response_body ? (
<pre className="flow-pre json">{JSON.stringify(record.response_body, null, 2)}</pre>
) : record.status === 'failed' ? (
<pre className="flow-pre" style={{ color: 'var(--color-error)' }}>
{record.error_message || '未知错误'}
</pre>
) : (
<div className="flow-step-empty">
<ClockCircleOutlined />
<span>等待响应</span>
</div>
)}
</div>
</div>
</div>
</div>
</>
)}
{!detailLoading && !record && (
<Empty description="加载失败" style={{ marginTop: 60 }} />
)}
</Spin>
<style>{`
.call-assembly-drawer .ant-drawer-body {
padding: 20px 24px;
}
.assembly-section {
margin-bottom: 4px;
}
.assembly-section-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
.assembly-section-title {
font-size: 14px;
font-weight: 600;
color: var(--color-text-primary);
}
.assembly-descriptions .ant-descriptions-item-label {
font-size: 12px;
color: var(--color-text-muted);
font-weight: 500;
white-space: nowrap;
}
.assembly-descriptions .ant-descriptions-item-content {
font-size: 13px;
}
.assembly-flow {
display: flex;
flex-direction: column;
gap: 0;
position: relative;
padding-left: 0;
}
.flow-step {
display: flex;
gap: 16px;
position: relative;
padding-bottom: 24px;
}
.flow-step:last-child {
padding-bottom: 0;
}
.flow-step-number {
width: 28px;
height: 28px;
border-radius: 50%;
background: var(--color-primary);
color: #fff;
font-size: 13px;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
position: relative;
z-index: 1;
}
.flow-step::before {
content: '';
position: absolute;
left: 13px;
top: 28px;
bottom: 0;
width: 2px;
background: var(--color-border-light);
}
.flow-step:last-child::before {
display: none;
}
.flow-step-content {
flex: 1;
min-width: 0;
}
.flow-step-title {
display: flex;
align-items: center;
gap: 6px;
font-weight: 600;
font-size: 13px;
color: var(--color-text-primary);
margin-bottom: 10px;
}
.request-copy-btn {
display: inline-flex;
align-items: center;
gap: 4px;
margin-left: auto;
font-size: 12px;
color: var(--color-primary);
background: transparent;
border: 1px solid var(--color-primary);
border-radius: 4px;
padding: 2px 10px;
cursor: pointer;
transition: background 0.15s;
}
.request-copy-btn:hover {
background: #eef2ff;
}
.flow-step-body {
background: var(--color-surface-subtle);
border: 1px solid var(--color-border-light);
border-radius: 8px;
padding: 12px 16px;
}
.template-info-row {
margin-bottom: 8px;
}
.template-info-row:last-child {
margin-bottom: 0;
}
.template-info-row label {
display: block;
font-size: 11px;
color: var(--color-text-muted);
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 2px;
}
.template-info-row > span,
.template-info-row > code {
font-size: 13px;
color: var(--color-text-primary);
}
.template-info-row > code {
font-family: var(--font-utility);
background: var(--color-surface);
padding: 1px 6px;
border-radius: 3px;
border: 1px solid var(--color-border-light);
}
.flow-pre {
margin: 0;
padding: 10px 14px;
background: var(--color-surface);
border: 1px solid var(--color-border-light);
border-radius: 6px;
font-family: var(--font-utility);
font-size: 12px;
line-height: 1.6;
color: var(--color-text-primary);
white-space: pre-wrap;
word-break: break-word;
max-height: 300px;
overflow-y: auto;
}
.flow-pre.json {
max-height: 400px;
}
.flow-step-empty {
display: flex;
align-items: center;
gap: 8px;
padding: 16px;
color: var(--color-text-muted);
font-size: 13px;
background: var(--color-surface-subtle);
border: 1px dashed var(--color-border-light);
border-radius: 6px;
}
.replacement-table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
.replacement-table th {
text-align: left;
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--color-text-muted);
font-weight: 500;
padding-bottom: 8px;
}
.replacement-table td {
padding: 6px 0;
border-top: 1px solid #d9f7be;
}
.replacement-word {
font-family: var(--font-utility);
background: #fff1f0;
color: #cf1322;
padding: 2px 8px;
border-radius: 3px;
border: 1px solid #ffa39e;
font-size: 12px;
}
.replacement-arrow {
color: var(--color-text-muted);
margin: 0 10px;
}
.replacement-target {
font-family: var(--font-utility);
background: #f6ffed;
color: #389e0d;
padding: 2px 8px;
border-radius: 3px;
border: 1px solid #b7eb8f;
font-size: 12px;
}
/* 变量卡片样式 */
.variables-grid {
display: flex;
flex-direction: column;
gap: 8px;
}
.variable-card {
background: #ffffff;
border: 1px solid #d1d5fc;
border-radius: 8px;
padding: 10px 14px;
transition: box-shadow 0.15s;
}
.variable-card:hover {
box-shadow: 0 2px 8px rgba(99, 102, 241, 0.12);
}
.variable-card-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 6px;
}
.variable-name {
font-family: var(--font-utility);
font-size: 13px;
font-weight: 600;
color: #4338ca;
background: #eef2ff;
padding: 1px 8px;
border-radius: 4px;
}
.variable-copy-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 26px;
height: 26px;
border: 1px solid #c7d2fe;
border-radius: 4px;
background: transparent;
color: #6366f1;
cursor: pointer;
transition: all 0.15s;
font-size: 13px;
}
.variable-copy-btn:hover {
background: #6366f1;
color: #fff;
border-color: #6366f1;
}
.variable-card-value {
font-family: var(--font-utility);
font-size: 13px;
color: var(--color-text-primary);
line-height: 1.5;
word-break: break-all;
max-height: 60px;
overflow-y: auto;
}
`}</style>
</Drawer>
);
}