SettingsTab.vue 7.9 KB
<!-- 助手设置:助手名称、通用提示词、对话参数。群里 @ 的机器人昵称在「消息通道 → 接收对象」里配。
  各技能的流程说明(提示词片段)、开关和参数在「技能」标签里。
  右侧实时预览模型实际收到的内容:系统提示词(通用提示词 + 技能片段 + 工具清单)+ 上下文(当前状态 + 最近聊天记录 + 这句话)。 -->
<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed, onMounted, ref, watch } from 'vue'
import { api } from '@/api'
import EmptyState from '@/components/EmptyState.vue'
import FormSection from '@/components/FormSection.vue'
import HelpTip from '@/components/HelpTip.vue'
import Panel from '@/components/Panel.vue'
import SaveBar from '@/components/SaveBar.vue'
import StatusPill from '@/components/StatusPill.vue'
import SwitchRow from '@/components/SwitchRow.vue'

const emit = defineEmits<{ go: [tab: string] }>()
const data = ref<any>(null)
const v = ref<any>(null)
const prompt = ref('')
const orig = ref('')
const saving = ref(false)
const failed = ref('')
const preview = ref<{ system: string; context: string; error: string }>({ system: '', context: '', error: '' })

function snapshot() { return JSON.stringify({ v: v.value, p: prompt.value }) }
function apply(r: any) {
  data.value = r
  v.value = { ...r.values }
  prompt.value = r.prompt.content
  orig.value = snapshot()
}
async function load() {
  failed.value = ''
  try {
    apply(await api.get('/api/assistant/settings'))
    refreshPreview()
  } catch (e: any) { failed.value = e.message; ElMessage.error(e.message) }
}
onMounted(load)
const dirty = computed(() => v.value && snapshot() !== orig.value)
const isDefault = computed(() => data.value && prompt.value.trim() === data.value.prompt.default.trim())

let timer: any = null
async function refreshPreview() {
  try {
    preview.value = await api.post('/api/assistant/prompt/preview', { content: prompt.value })
  } catch (e: any) { preview.value = { system: '', context: '', error: e.message } }
}
watch(prompt, () => { clearTimeout(timer); timer = setTimeout(refreshPreview, 400) })

async function save() {
  saving.value = true
  try {
    apply(await api.put('/api/assistant/settings', { values: v.value, prompt: prompt.value }))
    ElMessage.success('已保存,下一条消息生效')
    refreshPreview()
  } catch (e: any) { ElMessage.error(e.message) } finally { saving.value = false }
}
function resetAll() {
  v.value = { ...data.value.values }
  prompt.value = data.value.prompt.content
}
async function useDefault() {
  try {
    await ElMessageBox.confirm('把提示词换回系统默认版本?保存后生效。', '恢复默认', { type: 'warning' })
    prompt.value = data.value.prompt.default
  } catch { /* 取消 */ }
}
</script>

<template>
  <div v-if="v" class="settings">
    <div>
      <div class="form-card">
        <FormSection title="基本信息">
          <el-form label-position="top">
            <el-form-item label="助手名称"><el-input v-model="v.name" maxlength="30" show-word-limit /></el-form-item>
          </el-form>
          <SwitchRow label="消息里使用 emoji" hint="关闭后,消息里的 📦 ✅ 👉 等符号会自动去掉"><el-switch v-model="v.use_emoji" /></SwitchRow>
          <SwitchRow label="补货以外的问题由大模型简短回复" hint="关闭后统一回复固定引导语"><el-switch v-model="v.free_reply" /></SwitchRow>
          <div v-if="!data.llm.ready" class="notice block warn mt-3">还没有配置大模型 API Key(系统设置 → 大模型):智能助手不可用,群里 @助手 只会回「暂时不可用」。</div>
        </FormSection>
        <FormSection title="对话参数" desc="每句话交给大模型时带多少上下文。聊天记录不限时间、按条数取(含助手的回复和系统推送),零件号也只认原话和这些记录里出现过的。">
          <div class="params">
            <div class="param"><span>最近聊天记录</span><el-input-number v-model="v.history_limit" :min="1" :max="50" /><span class="unit">条</span></div>
            <div class="param"><span>每条最多</span><el-input-number v-model="v.history_chars" :min="100" :max="2000" :step="50" /><span class="unit">字</span></div>
          </div>
        </FormSection>
        <FormSection title="通用提示词" desc="告诉大模型它是谁、怎么判断(人设与判断原则)。单次补货、定时补货各项能力的说明在「技能」里,系统会按已启用的技能自动拼在后面。">
          <template #aside>
            <StatusPill v-if="data.prompt.changed" tone="warn" :dot="false">已自定义</StatusPill>
            <StatusPill v-else tone="ok" :dot="false">默认版本</StatusPill>
          </template>
          <el-input v-model="prompt" type="textarea" :autosize="{ minRows: 14, maxRows: 30 }" class="mono-input" />
          <div class="prompt-foot">
            <span class="muted small">
              可用变量:<code v-pre>{{ store.name }}</code> 门店名称、<code v-pre>{{ store.brand }}</code> 品牌、<code v-pre>{{ persona.name }}</code> 助手名称。
              <template v-if="data.prompt.changed && data.prompt.updated_by">上次由 {{ data.prompt.updated_by }} 于 {{ data.prompt.updated_at }} 修改。</template>
            </span>
            <el-button link type="primary" :disabled="isDefault" @click="useDefault">恢复默认</el-button>
          </div>
          <div class="notice block mt-3">
            <b>提交是硬性规则,不受提示词影响:</b>只有原话正好是「提交」或「确认提交」才会提交采购单;建单只建「待提交」草稿;每个会话同时只处理一单。
            各技能的流程说明在 <el-button link type="primary" @click="emit('go', 'skills')">技能</el-button> 里改。
          </div>
        </FormSection>
      </div>
      <SaveBar :show="!!dirty" :saving="saving" @save="save" @reset="resetAll" />
    </div>
    <Panel :desc="dirty ? '按左边未保存的提示词实时预览,保存后生效' : '模型每次收到的完整内容'">
      <template #title>模型实际收到的内容<HelpTip :width="360">
        系统提示词 = 通用提示词 + 已启用技能的提示词片段 + 系统自动拼上的工具清单。
        上下文由系统每次实时生成:【当前状态】从数据库读出(以它为准),【最近聊天记录】是这个会话最近 {{ v.history_limit }} 条真实收发记录(含系统推送的查询结果、回执)。
        这里的上下文用第一个「补货对话」会话做示例。
      </HelpTip></template>
      <div v-if="preview.error" class="notice err">{{ preview.error }}</div>
      <div class="sub-title">系统提示词</div>
      <div class="code-block pv">{{ preview.system }}</div>
      <div class="sub-title mt-4">上下文(示例)</div>
      <div class="code-block pv">{{ preview.context }}</div>
    </Panel>
  </div>
  <EmptyState v-else-if="failed" title="助手设置加载失败" :desc="failed"><el-button @click="load">重试</el-button></EmptyState>
  <el-skeleton v-else :rows="8" animated />
</template>

<style scoped>
.settings { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: var(--sp-4); align-items: start; }
@media (max-width: 1200px) { .settings { grid-template-columns: minmax(0, 1fr); } }
.settings :deep(.fs) { --label-w: 150px; }
.prompt-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 6px; }
.mono-input :deep(textarea) { font-family: var(--font-mono, ui-monospace, monospace); font-size: 12.5px; line-height: 1.7; }
.pv { white-space: pre-wrap; max-height: 420px; overflow-y: auto; }
.params { display: grid; gap: var(--sp-3); }
.param { display: flex; align-items: center; gap: var(--sp-2); }
.param > span:first-child { width: 110px; font-weight: 600; font-size: 13.5px; }
.unit { color: var(--c-text-3); font-size: 13px; }
</style>