RehearseTab.vue 11.5 KB
<!-- 试聊(沙盒):像在企业微信群里一样和助手多轮对话;助手调用的是各技能的沙盒工具——按真实话术回复、改的是沙盒状态,
     不查 DMS、不建单、不发企微。右侧按技能实时显示沙盒状态(沙盒单次补货)。 -->
<script setup lang="ts">
import { Promotion, RefreshRight } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { computed, nextTick, ref } from 'vue'
import { api } from '@/api'
import EmptyState from '@/components/EmptyState.vue'
import HelpTip from '@/components/HelpTip.vue'
import Panel from '@/components/Panel.vue'
import StatusPill from '@/components/StatusPill.vue'
import { BLOCKED_CN, fmtMoney, lineClass, SKILL_CN, STAGE, TOOL_CN } from '@/format'

type Msg = { who: 'me' | 'bot' | 'sys'; text: string; kind?: string; calls?: any[]; result?: string; mode?: string; llm_error?: string; failed?: boolean }
const session = ref(Math.random().toString(36).slice(2, 10))
const text = ref('')
const busy = ref(false)
const log = ref<Msg[]>([])
const sandbox = ref<any[]>([])
const box = ref<HTMLElement | null>(null)
const composing = ref(false)
const SAMPLES = ['8713902280 有货吗', '那订 2 个', '帮我定:8713902280*2', '定', '没毛病,整吧', '提交吧', '确认提交', '不订了',
  '我那单到哪了?', '今天定时补货到哪了?', '你好,你能做什么?']
const RESULT: Record<string, { text: string; tone: any }> = {
  tool: { text: '调用工具', tone: 'primary' }, free_reply: { text: '自由回复', tone: 'ok' }, refused: { text: '固定拒答', tone: 'info' },
  tool_then_llm_error: { text: '调用工具后模型出错', tone: 'warn' },
  no_tool_text: { text: '没调工具直接回复,已拦截', tone: 'warn' }, reply_blocked: { text: '直接回复涉及业务,已拦截', tone: 'warn' },
  llm_unavailable: { text: '大模型不可用,已提示', tone: 'err' }, no_skills: { text: '没有启用技能', tone: 'warn' },
}
const MODE: Record<string, string> = { llm: '大模型结合上下文判断' }
const single = computed(() => sandbox.value.find((v) => v.skill === 'restock.single') || null)

async function scrollEnd() {
  await nextTick()
  box.value?.scrollTo({ top: box.value.scrollHeight, behavior: 'smooth' })
}
async function send(t?: string, retry?: Msg) {
  const msg = (t ?? text.value).trim()
  if (!msg || busy.value) return
  if (!t) text.value = ''
  const mine: Msg = retry || { who: 'me', text: msg }
  mine.failed = false
  if (!retry) log.value.push(mine)
  busy.value = true
  scrollEnd()
  try {
    const r = await api.post('/api/assistant/rehearse', { text: msg, session: session.value })
    const msgs: any[] = r.messages?.length ? r.messages : [{ text: '(助手没有回复)', kind: 'note' }]
    msgs.forEach((m: any, i: number) => log.value.push({ who: m.kind === 'note' ? 'sys' : 'bot', text: m.text, kind: m.kind,
      ...(i === msgs.length - 1 ? { calls: r.calls, result: r.result, mode: r.mode, llm_error: r.llm_error } : {}) }))
    sandbox.value = r.sandbox || []
  } catch (e: any) {
    mine.failed = true
    ElMessage.error(e.message)
  } finally {
    busy.value = false
    scrollEnd()
  }
}
function onEnter(e: KeyboardEvent) {
  if (composing.value || e.isComposing) return       // 输入法正在选词时的回车不发送
  e.preventDefault()
  send()
}
async function restart() {
  try { await api.del('/api/assistant/rehearse/' + session.value) } catch { /* 忽略 */ }
  session.value = Math.random().toString(36).slice(2, 10)
  log.value = []
  sandbox.value = []
}
</script>

<template>
  <div class="split">
    <Panel>
      <template #title>对话演练<HelpTip :width="380">
        和企业微信里一样多轮对话,回复按真实话术生成;不会查 DMS、不会建单、不会发企微。
        和企微里走同一套流程:规则能确定的说法直接识别,其他的交给大模型结合当前状态和聊天记录判断。
        助手听不懂或选错时:到「助手设置」改通用提示词,或到「技能」改对应技能的提示词片段,回来点「重新开始」再试;
        每句话下面可以展开看是规则还是大模型识别的、调用了哪个工具。
      </HelpTip></template>
      <template #extra><el-button :icon="RefreshRight" @click="restart">重新开始</el-button></template>
      <div ref="box" class="chat">
        <div v-if="!log.length" class="hello">
          <div class="strong">从一句话开始</div>
          <div class="muted small">比如先说「帮我定:8713902280*2」拿到沙盒查询结果,回「定」或「没毛病,整吧」建单,再回「确认提交」演练提交(「提交吧」不会提交)。</div>
        </div>
        <template v-for="(m, i) in log" :key="i">
          <div v-if="m.who === 'me'" class="msg me">
            <div class="b">{{ m.text }}</div>
            <button v-if="m.failed" type="button" class="retry" @click="send(m.text, m)">发送失败,点此重发</button>
          </div>
          <div v-else-if="m.who === 'sys'" class="msg sys"><span>{{ m.text }}</span></div>
          <div v-else class="msg bot">
            <div class="avatar">助</div>
            <div class="bot-col">
              <div class="b" :class="{ err: m.kind === 'error' }">{{ m.text }}</div>
              <details v-if="m.result" class="why">
                <summary>
                  <StatusPill :tone="RESULT[m.result]?.tone || 'info'" :dot="false">{{ RESULT[m.result]?.text || m.result }}</StatusPill>
                  <span v-for="(c, j) in m.calls" :key="j" class="small">{{ TOOL_CN[c.tool] || c.tool }}</span>
                  <span v-if="m.mode && MODE[m.mode]" class="small">{{ MODE[m.mode] }}</span>
                </summary>
                <div v-for="(c, j) in m.calls" :key="j" class="call">
                  <div><b>{{ TOOL_CN[c.tool] || c.tool }}</b> <span class="mono muted small">{{ c.tool }}</span>
                    <span v-if="c.skill" class="muted small">· {{ SKILL_CN[c.skill] || c.skill }}</span>
                    <span v-if="c.blocked" class="warn small">{{ BLOCKED_CN[c.blocked] || c.blocked }}</span></div>
                  <div v-if="Object.keys(c.args || {}).length" class="mono small">参数 {{ JSON.stringify(c.args) }}</div>
                  <div v-if="c.result" class="muted small">工具返回:{{ c.result }}</div>
                </div>
                <div v-if="m.llm_error" class="muted small">{{ m.llm_error }}</div>
              </details>
            </div>
          </div>
        </template>
        <div v-if="busy" class="msg bot"><div class="avatar">助</div><div class="b typing">正在想…</div></div>
      </div>
      <div class="samples">
        <button v-for="s in SAMPLES" :key="s" type="button" class="sample" :disabled="busy" @click="send(s)">{{ s }}</button>
      </div>
      <div class="input">
        <el-input v-model="text" placeholder="输入计划员可能说的话,回车发送" :disabled="busy"
                  @compositionstart="composing = true" @compositionend="composing = false" @keydown.enter="onEnter" />
        <el-button type="primary" :icon="Promotion" :loading="busy" @click="send()">发送</el-button>
      </div>
    </Panel>

    <div class="stack">
      <Panel :title="single ? single.title : '沙盒单次补货'" :desc="single ? `数据来自${single.source}` : '发「零件号 数量」后这里显示沙盒单次补货,回「订」会实时变化'">
        <template v-if="single" #extra><StatusPill :tone="STAGE[single.status]?.tone || 'info'">{{ single.stage }}</StatusPill></template>
        <template v-if="single">
          <div class="figs">
            <div><span class="fig-label">单号</span><span class="fig-value mono">{{ single.doc }}</span></div>
            <div><span class="fig-label">可订</span><span class="fig-value">{{ single.summary.order_count }} 项 / {{ single.summary.order_qty }} 件</span></div>
            <div><span class="fig-label">金额</span><span class="fig-value">{{ fmtMoney(single.summary.amount) }}</span></div>
            <div v-if="single.order_no"><span class="fig-label">模拟采购单</span><span class="fig-value mono">{{ single.order_no }}</span></div>
          </div>
          <el-table :data="single.lines" size="small" class="mt-3" :row-class-name="({ row }: any) => lineClass(row)" max-height="420">
            <el-table-column prop="seq" label="#" width="44" />
            <el-table-column prop="name" label="配件" min-width="110" show-overflow-tooltip />
            <el-table-column prop="part_no" label="零件号" width="110" />
            <el-table-column label="库存+在途" width="84"><template #default="{ row }">{{ row.stock }}+{{ row.in_transit }}</template></el-table-column>
            <el-table-column label="可订" width="56"><template #default="{ row }">{{ row.cap ?? '不限' }}</template></el-table-column>
            <el-table-column label="订购" width="64"><template #default="{ row }"><b>{{ row.final_qty }}</b></template></el-table-column>
            <el-table-column label="说明" min-width="100" show-overflow-tooltip><template #default="{ row }">{{ row.label || row.note }}</template></el-table-column>
          </el-table>
        </template>
        <EmptyState v-else title="还没有沙盒单次补货" desc="试试「8713902280 空调滤 2个」,再回「订」;只问「8713902280 有货吗」会先反问要订几个" />
      </Panel>
    </div>
  </div>
</template>

<style scoped>
.chat { height: 480px; overflow-y: auto; background: #eef0f3; border-radius: var(--radius-sm); padding: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
.hello { margin: auto; text-align: center; display: grid; gap: 6px; max-width: 360px; }
.msg { display: flex; gap: var(--sp-2); align-items: flex-start; }
.msg.me { flex-direction: column; align-items: flex-end; }
.msg.sys { justify-content: center; }
.msg.sys span { font-size: 12px; color: var(--c-text-3); background: rgba(255, 255, 255, 0.7); padding: 3px 10px; border-radius: 10px; }
.avatar { width: 30px; height: 30px; border-radius: 6px; background: var(--c-primary); color: #fff; display: grid; place-items: center; font-size: 13px; font-weight: 600; flex-shrink: 0; }
.bot-col { display: flex; flex-direction: column; gap: 4px; max-width: 80%; min-width: 0; }
.b { max-width: 80%; padding: 9px 13px; border-radius: 10px; font-size: 13.5px; line-height: 1.65; white-space: pre-wrap; word-break: break-all; }
.bot-col .b { max-width: 100%; }
.me .b { background: #95ec69; border-top-right-radius: 3px; }
.bot .b { background: #fff; border-top-left-radius: 3px; box-shadow: var(--shadow); }
.bot .b.err { color: var(--c-err); }
.typing { color: var(--c-text-3); }
.retry { margin-top: 4px; border: 0; background: none; color: var(--c-err); font-size: 12px; cursor: pointer; padding: 0; }
.why { font-size: 12.5px; }
.why summary { cursor: pointer; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; list-style: none; color: var(--c-text-3); }
.why summary::-webkit-details-marker { display: none; }
.call { margin-top: 6px; padding: 6px 10px; background: rgba(255, 255, 255, 0.75); border-radius: 6px; display: grid; gap: 2px; }
.samples { display: flex; flex-wrap: wrap; gap: 6px; margin: var(--sp-3) 0; }
.sample { border: 1px solid var(--c-border-strong); background: var(--c-surface); border-radius: 14px; padding: 4px 12px; font: inherit; font-size: 12.5px; color: var(--c-text-2); cursor: pointer; }
.sample:hover:not(:disabled) { border-color: var(--c-primary); color: var(--c-primary); }
.sample:disabled { opacity: 0.5; cursor: default; }
.input { display: flex; gap: var(--sp-2); }
</style>