<template>
  <div class="page-content">
    <div class="mb-5"><h2 class="text-xl font-semibold">模型治理</h2><p class="text-g-500 mt-1 text-sm">统一处理门户模型申请与基于企业知识库的事实核验策略</p></div>
    <ElTabs v-model="tab">
      <ElTabPane label="模型申请" name="requests"><ElTable v-loading="loading" :data="requests"><ElTableColumn prop="user_login" label="账号"/><ElTableColumn prop="model" label="模型" min-width="170"/><ElTableColumn prop="provider_code" label="供应商"/><ElTableColumn prop="reason" label="理由"/><ElTableColumn prop="requested_rpm" label="RPM" width="80"/><ElTableColumn prop="requested_monthly_tokens" label="月 Token" width="120"/><ElTableColumn prop="status" label="状态" width="90"><template #default="{row}"><ElTag>{{row.status}}</ElTag></template></ElTableColumn><ElTableColumn label="操作" width="140"><template #default="{row}"><template v-if="row.status==='pending'"><ElButton link type="success" @click="decide(row,'approve')">通过</ElButton><ElButton link type="danger" @click="decide(row,'reject')">拒绝</ElButton></template></template></ElTableColumn></ElTable></ElTabPane>
      <ElTabPane label="事实核验策略" name="policies"><div class="mb-4 flex justify-between"><ElAlert class="mr-4" :closable="false" type="info" title="异步策略仅观察；同步策略可标注或阻断。核验模型复用 Provider 加密凭据。"/><ElButton type="primary" @click="openPolicy()">新建策略</ElButton></div><ElTable :data="policies"><ElTableColumn prop="scope" label="作用域"/><ElTableColumn prop="mode" label="模式" width="90"/><ElTableColumn prop="action" label="动作" width="90"/><ElTableColumn label="知识库" width="100"><template #default="{row}">{{row.knowledge_base_ids.length}}</template></ElTableColumn><ElTableColumn prop="support_threshold" label="支持阈值" width="100"/><ElTableColumn label="状态" width="90"><template #default="{row}"><ElTag :type="row.enabled?'success':'info'">{{row.enabled?'启用':'停用'}}</ElTag></template></ElTableColumn><ElTableColumn label="操作" width="130"><template #default="{row}"><ElButton link type="primary" @click="openPolicy(row)">编辑</ElButton><ElButton link type="danger" @click="removePolicy(row)">删除</ElButton></template></ElTableColumn></ElTable></ElTabPane>
      <ElTabPane label="核验事件" name="events"><ElTable :data="events"><ElTableColumn prop="created_at" label="时间" width="190"/><ElTableColumn prop="request_id" label="Request ID"/><ElTableColumn prop="model" label="模型"/><ElTableColumn prop="verdict" label="结论" width="100"/><ElTableColumn prop="support_score" label="支持度" width="90"/><ElTableColumn prop="latency_ms" label="耗时(ms)" width="100"/><ElTableColumn prop="error" label="错误"/></ElTable></ElTabPane>
      <ElTabPane label="核验设置" name="settings"><ElForm class="max-w-xl" label-width="120px"><ElFormItem label="供应商"><ElSelect v-model="settings.provider_id" clearable class="w-full"><ElOption v-for="p in providers" :key="p.id" :label="p.code" :value="p.id"/></ElSelect></ElFormItem><ElFormItem label="核验模型"><ElInput v-model="settings.model"/></ElFormItem><ElFormItem label="超时（秒）"><ElInputNumber v-model="settings.timeout_seconds" :min="3" :max="60"/></ElFormItem><ElFormItem><ElButton type="primary" @click="saveSettings">保存设置</ElButton></ElFormItem></ElForm></ElTabPane>
    </ElTabs>
    <ElDialog v-model="policyVisible" :title="policy.id?'编辑策略':'新建策略'" width="680px"><ElForm label-width="110px"><ElFormItem label="作用域"><ElInput v-model="policy.scope" placeholder="global 或 department/application/api_key:UUID"/></ElFormItem><div class="grid grid-cols-2"><ElFormItem label="模式"><ElSelect v-model="policy.mode"><ElOption value="async" label="异步"/><ElOption value="sync" label="同步"/></ElSelect></ElFormItem><ElFormItem label="动作"><ElSelect v-model="policy.action"><ElOption value="observe" label="观察"/><ElOption value="annotate" label="标注"/><ElOption value="block" label="阻断"/></ElSelect></ElFormItem></div><ElFormItem label="知识库"><ElSelect v-model="policy.knowledge_base_ids" multiple class="w-full"><ElOption v-for="k in knowledge" :key="k.id" :label="k.name" :value="k.id"/></ElSelect></ElFormItem><div class="grid grid-cols-2"><ElFormItem label="支持阈值"><ElInputNumber v-model="policy.support_threshold" :min="0" :max="100"/></ElFormItem><ElFormItem label="检索 Top K"><ElInputNumber v-model="policy.top_k" :min="1" :max="10"/></ElFormItem><ElFormItem label="证据阈值"><ElInputNumber v-model="policy.evidence_threshold" :min="0" :max="1" :step="0.05"/></ElFormItem><ElFormItem label="最大事实数"><ElInputNumber v-model="policy.max_claims" :min="1" :max="20"/></ElFormItem></div><ElFormItem label="状态"><ElSwitch v-model="policy.enabled"/></ElFormItem></ElForm><template #footer><ElButton @click="policyVisible=false">取消</ElButton><ElButton type="primary" @click="savePolicy">保存</ElButton></template></ElDialog>
  </div>
</template>
<script setup lang="ts">
import{FactPolicy,FactSettings,ModelRequest,createFactPolicy,decideModelRequest,deleteFactPolicy,fetchFactEvents,fetchFactPolicies,fetchFactSettings,fetchModelRequests,saveFactSettings,updateFactPolicy}from'@/api/governance';import{fetchProviders,ProviderRecord}from'@/api/providers';import{fetchKnowledgeBases,KnowledgeBase}from'@/api/workbench';import{ElMessage,ElMessageBox}from'element-plus';const tab=ref('requests'),loading=ref(false),requests=ref<ModelRequest[]>([]),policies=ref<FactPolicy[]>([]),events=ref<any[]>([]),providers=ref<ProviderRecord[]>([]),knowledge=ref<KnowledgeBase[]>([]),settings=reactive<FactSettings>({provider_code:'',model:'',timeout_seconds:15}),policyVisible=ref(false);const blank=():FactPolicy=>({scope:'global',enabled:false,mode:'async',action:'observe',knowledge_base_ids:[],support_threshold:70,evidence_threshold:.35,top_k:4,max_claims:8});const policy=reactive<FactPolicy>(blank());async function load(){loading.value=true;try{const[r,p,e,s,pr,k]=await Promise.all([fetchModelRequests(),fetchFactPolicies(),fetchFactEvents(),fetchFactSettings(),fetchProviders(),fetchKnowledgeBases()]);requests.value=r;policies.value=p.items;events.value=e.items;Object.assign(settings,s);providers.value=pr;knowledge.value=k}finally{loading.value=false}}async function decide(row:ModelRequest,status:'approve'|'reject'){const{value}=await ElMessageBox.prompt('审批意见（可选）',status==='approve'?'通过申请':'拒绝申请');await decideModelRequest(row.id,status,value||'');await load()}function openPolicy(row?:FactPolicy){Object.assign(policy,row?{...row,knowledge_base_ids:[...row.knowledge_base_ids]}:blank());policyVisible.value=true}async function savePolicy(){policy.id?await updateFactPolicy(policy.id,policy):await createFactPolicy(policy);policyVisible.value=false;ElMessage.success('策略已保存');await load()}async function removePolicy(row:FactPolicy){if(!row.id)return;await ElMessageBox.confirm('确认删除该策略？','删除',{type:'warning'});await deleteFactPolicy(row.id);await load()}async function saveSettings(){await saveFactSettings(settings);ElMessage.success('设置已保存')}onMounted(load)
</script>
