AiTraceDrawer.vue
7.41 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
<!-- 智能助手对一句话的处理过程(按收发记录 id 取 /api/wecom/logs/{id}/trace):
收到什么 → 每一轮大模型(耗时、token、思考、决定调用哪个工具 / 直接回复 / 结束)→ 工具执行结果 → 最终结果;
给模型的原始输入(上下文、系统提示词)折叠在最后。消息记录、补货单时间线里点「处理过程」打开。 -->
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { computed, ref, watch } from 'vue'
import { api } from '@/api'
import StatusPill from './StatusPill.vue'
import { AI_RESULT, BLOCKED_CN, fmtTime, SKILL_CN, TOOL_CN } from '@/format'
const props = defineProps<{ logId: number | null }>()
const emit = defineEmits<{ close: [] }>()
const t = ref<any>(null)
const loading = ref(false)
const NO_REPLY = '__NO_REPLY__'
watch(() => props.logId, async (id) => {
t.value = null
if (!id) return
loading.value = true
try { t.value = await api.get(`/api/wecom/logs/${id}/trace`) } catch (e: any) { ElMessage.error(e.message); emit('close') } finally { loading.value = false }
}, { immediate: true })
// 每一轮模型发起的工具调用,按顺序对上实际执行记录(calls);对不上的(模型把工具名当文字输出等)放到最后
const steps = computed(() => {
if (!t.value) return { rounds: [], rest: [] }
const pool = [...(t.value.calls || [])]
const take = (name: string) => {
const i = pool.findIndex((c) => c.tool === name)
return i >= 0 ? pool.splice(i, 1)[0] : null
}
const rounds = (t.value.rounds || []).map((r: any) => ({
...r, runs: (r.tool_calls || []).map((tc: any) => ({ call: tc, run: take(tc.name) })),
}))
return { rounds, rest: pool }
})
const tokens = (r: any) => (r.tokens_in != null || r.tokens_out != null ? `入 ${r.tokens_in ?? '?'} / 出 ${r.tokens_out ?? '?'} token` : '')
const sec = (ms: number | null | undefined) => (ms != null ? `${(ms / 1000).toFixed(1)} 秒` : '')
const json = (o: any) => JSON.stringify(o || {})
</script>
<template>
<el-drawer :model-value="!!logId" size="720px" title="助手处理过程" @close="emit('close')">
<div v-loading="loading" class="stack">
<template v-if="t">
<dl class="kv">
<dt>时间</dt><dd>{{ fmtTime(t.ts) }}</dd>
<dt>结果</dt><dd><StatusPill :tone="AI_RESULT[t.result]?.tone || 'info'" :dot="false">{{ AI_RESULT[t.result]?.text || t.result || '—' }}</StatusPill></dd>
<dt>耗时</dt><dd>共 {{ t.seconds }} 秒<span v-if="t.llm_ms != null" class="muted">,其中大模型 {{ sec(t.llm_ms) }}</span></dd>
<dt>模型</dt><dd>{{ t.model || '—' }}<span v-if="t.tokens_in != null" class="muted small"> 入 {{ t.tokens_in }} / 出 {{ t.tokens_out }} token</span></dd>
</dl>
<div v-if="t.error" class="notice err"><b>大模型出错:</b>{{ t.error }}</div>
<ol class="flow">
<li class="in">
<div class="head">收到</div>
<div class="pre"><b>{{ t.sender }}:</b>{{ t.text }}</div>
</li>
<li v-for="(r, i) in steps.rounds" :key="i" class="llm">
<div class="head">第 {{ i + 1 }} 轮 · 大模型 <span class="muted small">{{ sec(r.ms) }} {{ tokens(r) }}</span></div>
<details v-if="r.thinking" class="think"><summary class="small">思考过程({{ r.thinking.length }} 字)</summary><div class="code-block">{{ r.thinking }}</div></details>
<template v-if="r.runs.length">
<div v-if="r.text" class="pre muted small">{{ r.text }}</div>
<div v-for="(x, j) in r.runs" :key="j" class="call">
<div>决定调用 <b>{{ TOOL_CN[x.call.name] || x.call.name }}</b> <span class="mono small muted">{{ x.call.name }} {{ json(x.call.args) }}</span></div>
<div v-if="!x.run" class="muted small">(没有执行记录)</div>
<template v-else>
<div v-if="x.run.duplicate" class="warn small">同一句话里重复调用,已忽略</div>
<div v-else-if="x.run.blocked" class="warn small">没有执行:{{ BLOCKED_CN[x.run.blocked] || x.run.blocked }}</div>
<div v-else class="small">
执行 {{ x.run.seconds }} 秒<span v-if="x.run.skill" class="muted"> · {{ SKILL_CN[x.run.skill] || x.run.skill }}</span>
<span v-if="x.run.replied" class="ok"> · 已把结果发给用户</span>
</div>
<div v-if="x.run.result && !x.run.duplicate" class="pre small result">{{ x.run.result }}</div>
</template>
</div>
</template>
<div v-else-if="!r.text || r.text.trim() === NO_REPLY" class="muted small">结束,不再回复</div>
<div v-else class="pre small"><b>给出回复:</b>{{ r.text }}</div>
</li>
<li v-for="(c, j) in steps.rest" :key="'rest' + j" class="llm">
<div class="head">执行 {{ TOOL_CN[c.tool] || c.tool }} <span class="mono small muted">{{ c.tool }} {{ json(c.args) }}</span></div>
<div v-if="c.blocked" class="warn small">没有执行:{{ BLOCKED_CN[c.blocked] || c.blocked }}</div>
<div v-if="c.result" class="pre small result">{{ c.result }}</div>
</li>
<li v-if="!steps.rounds.length && !steps.rest.length && t.route === 'llm'" class="llm">
<div class="muted small">没有调用大模型</div>
</li>
<li class="out">
<div class="head">结果:{{ AI_RESULT[t.result]?.text || t.result }}</div>
<div v-if="t.reply" class="pre small"><b>助手发出:</b>{{ t.reply }}</div>
<div v-else-if="t.result === 'tool'" class="muted small">由工具按话术发出,见消息记录</div>
</li>
</ol>
<details v-if="t.context || t.prompt" class="raw">
<summary>给模型的原始输入</summary>
<div v-if="t.context" class="sub-title mt-3">上下文</div>
<div v-if="t.context" class="code-block">{{ t.context }}</div>
<div v-if="t.prompt" class="sub-title mt-3">系统提示词 <span class="muted small">版本 #{{ t.prompt_id }}</span></div>
<div v-if="t.prompt" class="code-block">{{ t.prompt }}</div>
</details>
</template>
</div>
</el-drawer>
</template>
<style scoped>
.flow { list-style: none; margin: 0; padding: 0; display: grid; gap: 0; }
.flow > li { position: relative; padding: 0 0 var(--sp-4) 22px; display: grid; gap: 6px; }
.flow > li::before { content: ''; position: absolute; left: 5px; top: 6px; width: 9px; height: 9px; border-radius: 50%; background: var(--c-primary); }
.flow > li::after { content: ''; position: absolute; left: 9px; top: 18px; bottom: 0; width: 1px; background: var(--c-border); }
.flow > li:last-child::after { display: none; }
.flow > li.in::before { background: var(--c-text-3); }
.flow > li.out::before { background: var(--c-ok, #16a34a); }
.head { font-weight: 600; font-size: 13.5px; }
.call { padding: 8px 10px; background: var(--c-surface-2, #f5f6f8); border-radius: 6px; display: grid; gap: 3px; font-size: 13px; }
.result { color: var(--c-text-2); }
.pre { white-space: pre-wrap; word-break: break-all; }
.think summary, .raw > summary { cursor: pointer; color: var(--c-primary); }
.think .code-block, .raw .code-block { margin-top: 4px; max-height: 360px; overflow-y: auto; white-space: pre-wrap; }
.warn { color: var(--c-warn, #8a4a04); }
.ok { color: var(--c-ok, #0e6b36); }
</style>