SkillsTab.vue 9.56 KB
<!-- 技能:智能助手在会话里能做的事(单次补货、定时补货)。列表一行一个技能,行内直接启停;
  点「设置」打开抽屉改参数(按后端 schema 自动渲染)和提示词片段(拼在通用提示词后面,告诉大模型什么时候调这个技能的工具)。
  大模型只负责选工具、取参数;执行逻辑都在代码里(确定性)。 -->
<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed, onMounted, 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 SchemaForm from '@/components/SchemaForm.vue'
import StatusPill from '@/components/StatusPill.vue'
import SwitchRow from '@/components/SwitchRow.vue'
import { fmtShort } from '@/format'

const emit = defineEmits<{ go: [tab: string] }>()
type Draft = { enabled: boolean; config: Record<string, any>; prompt: string }
const list = ref<any[]>([])
const failed = ref('')
const loading = ref(true)
const toggling = ref<string | null>(null)
const cur = ref<any>(null)            // 抽屉里正在改的技能
const draft = ref<Draft | null>(null)
const orig = ref('')
const saving = ref(false)

function put(s: any) {
  const i = list.value.findIndex((x) => x.key === s.key)
  if (i >= 0) list.value[i] = s
  else list.value.push(s)
}
async function load() {
  failed.value = ''
  loading.value = true
  try {
    list.value = (await api.get('/api/assistant/skills')).skills
  } catch (e: any) { failed.value = e.message; ElMessage.error(e.message) } finally { loading.value = false }
}
onMounted(load)

async function toggle(s: any, on: boolean) {
  toggling.value = s.key
  try {
    put(await api.put(`/api/assistant/skills/${s.key}`, { enabled: on }))
    ElMessage.success(on ? `已启用「${s.name}」,下一条消息生效` : `已停用「${s.name}」`)
  } catch (e: any) { ElMessage.error(e.message) } finally { toggling.value = null }
}

function snap(d: Draft) { return JSON.stringify(d) }
function open(s: any) {
  cur.value = s
  draft.value = { enabled: s.enabled, config: { ...(s.config || {}) }, prompt: s.prompt?.content ?? '' }
  orig.value = snap(draft.value)
}
const dirty = computed(() => !!draft.value && snap(draft.value) !== orig.value)
async function close(done: () => void) {
  if (dirty.value) {
    try { await ElMessageBox.confirm('有未保存的修改,确定放弃?', '关闭', { type: 'warning' }) } catch { return }
  }
  done()
}
function closed() { cur.value = null; draft.value = null }
async function save() {
  const s = cur.value
  const d = draft.value!
  const o: Draft = JSON.parse(orig.value)
  const body: any = {}
  if (d.enabled !== o.enabled) body.enabled = d.enabled
  if (JSON.stringify(d.config) !== JSON.stringify(o.config)) body.config = d.config
  if (s.prompt && d.prompt !== o.prompt) body.prompt = d.prompt
  saving.value = true
  try {
    const r = await api.put(`/api/assistant/skills/${s.key}`, body)
    put(r)
    open(r)
    ElMessage.success('已保存,下一条消息生效')
  } catch (e: any) { ElMessage.error(e.message) } finally { saving.value = false }
}
async function useDefault() {
  try {
    await ElMessageBox.confirm(`把「${cur.value.name}」的提示词片段换回系统默认版本?保存后生效。`, '恢复默认', { type: 'warning' })
    draft.value!.prompt = cur.value.prompt.default
  } catch { /* 取消 */ }
}
function rehearse() { cur.value = null; emit('go', 'rehearse') }
</script>

<template>
  <div v-if="list.length" class="stack">
    <div class="notice block">
      每句话由大模型根据已启用技能的说明,选一个工具执行;查询报价、建单、提交都由系统按固定流程执行。
      零件号只认对方原话或最近聊天记录里出现过的;采购单只有原话正好是「提交 / 确认提交」才会提交。
    </div>
    <Panel flush>
      <el-table :data="list" row-key="key" class="clickable" @row-click="open">
        <el-table-column label="技能" min-width="260">
          <template #default="{ row }">
            <div class="name"><b>{{ row.name }}</b><span class="mono small key">{{ row.key }}</span></div>
            <div class="muted small">{{ row.summary }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="150">
          <template #default="{ row }">
            <StatusPill v-if="!row.feature_enabled" tone="err" :dot="false">「{{ row.feature_name }}」没开通</StatusPill>
            <StatusPill v-else-if="!row.active" tone="info" :dot="false">已停用</StatusPill>
            <StatusPill v-else tone="ok">生效中</StatusPill>
          </template>
        </el-table-column>
        <el-table-column label="工具" width="90"><template #default="{ row }">{{ row.tools.length }} 个</template></el-table-column>
        <el-table-column label="提示词片段" width="110">
          <template #default="{ row }">
            <template v-if="row.prompt">
              <StatusPill v-if="row.prompt.changed" tone="warn" :dot="false">已自定义</StatusPill>
              <span v-else class="muted small">默认版本</span>
            </template>
            <span v-else class="muted small">—</span>
          </template>
        </el-table-column>
        <el-table-column label="启用" width="80">
          <template #default="{ row }">
            <span @click.stop><el-switch :model-value="row.enabled" :loading="toggling === row.key" :disabled="!row.feature_enabled"
              @change="(on: any) => toggle(row, !!on)" /></span>
          </template>
        </el-table-column>
        <el-table-column label="" width="80" align="right">
          <template #default="{ row }"><el-button link type="primary" @click.stop="open(row)">设置</el-button></template>
        </el-table-column>
      </el-table>
    </Panel>

    <el-drawer :model-value="!!cur" size="760px" :title="cur ? `技能:${cur.name}` : ''" :before-close="close" @closed="closed">
      <div v-if="cur && draft" class="stack-lg">
        <div class="muted">{{ cur.summary }}</div>
        <section>
          <SwitchRow label="启用这个技能" :hint="cur.feature_enabled ? '停用后,大模型看不到这个技能的工具和提示词' : `所属功能「${cur.feature_name}」没开通,技能开着也不生效`">
            <el-switch v-model="draft.enabled" />
          </SwitchRow>
          <SchemaForm v-if="cur.schema" v-model="draft.config" :schema="cur.schema" class="mt-3" />
        </section>
        <section v-if="cur.prompt">
          <div class="sub-title">
            提示词片段
            <HelpTip :width="340">拼在通用提示词后面,标题是「【{{ cur.name }}】」;告诉大模型遇到什么说法调哪个工具。技能停用时不拼。改完到「试聊」里验证。</HelpTip>
            <StatusPill v-if="cur.prompt.changed" tone="warn" :dot="false">已自定义</StatusPill>
            <StatusPill v-else tone="ok" :dot="false">默认版本</StatusPill>
          </div>
          <el-input v-model="draft.prompt" type="textarea" :autosize="{ minRows: 8, maxRows: 22 }" class="mono-input" />
          <div class="prompt-foot">
            <span class="muted small">
              <template v-if="cur.prompt.variables?.length">可用变量:<template v-for="x in cur.prompt.variables" :key="x.name"><code>{{ x.name }}</code> {{ x.desc }};</template></template>
              <template v-if="cur.prompt.changed && cur.prompt.updated_by">上次由 {{ cur.prompt.updated_by }} 于 {{ fmtShort(cur.prompt.updated_at) }} 修改。</template>
            </span>
            <span class="foot-btns">
              <el-button link type="primary" @click="rehearse">去试聊</el-button>
              <el-button link type="primary" :disabled="draft.prompt.trim() === cur.prompt.default.trim()" @click="useDefault">恢复默认</el-button>
            </span>
          </div>
        </section>
        <section>
          <div class="sub-title">工具<HelpTip text="大模型能调用的动作,执行逻辑都在代码里;说明只描述「做什么」,什么时候调用写在提示词片段里" /></div>
          <div v-for="t in cur.tools" :key="t.name" class="tool">
            <span class="mono">{{ t.name }}</span><span class="muted small">{{ t.description }}</span>
          </div>
        </section>
      </div>
      <template #footer>
        <el-button :disabled="!dirty" @click="open(cur)">撤销</el-button>
        <el-button type="primary" :disabled="!dirty" :loading="saving" @click="save">保存</el-button>
      </template>
    </el-drawer>
  </div>
  <EmptyState v-else-if="failed" title="技能加载失败" :desc="failed"><el-button @click="load">重试</el-button></EmptyState>
  <EmptyState v-else-if="!loading" title="还没有任何技能" desc="开通业务功能后,这里会列出它提供的对话技能" />
  <el-skeleton v-else :rows="6" animated />
</template>

<style scoped>
.clickable :deep(.el-table__row) { cursor: pointer; }
.name { display: flex; gap: var(--sp-2); align-items: baseline; flex-wrap: wrap; }
.key { color: var(--c-text-3); }
.sub-title { display: flex; align-items: center; gap: 6px; }
.tool { display: grid; gap: 2px; padding: 6px 0; border-bottom: 1px dashed var(--c-border); }
.tool:last-child { border-bottom: 0; }
.prompt-foot { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-top: 6px; }
.foot-btns { display: flex; gap: 4px; flex-shrink: 0; }
.mono-input :deep(textarea) { font-family: var(--font-mono, ui-monospace, monospace); font-size: 12.5px; line-height: 1.7; }
</style>