App.vue 9.21 KB
<template>
  <el-container class="app-shell">
    <el-header height="64px" class="app-header">
      <div class="app-header-left">
        <div class="app-logo">
          <div class="logo-box">IF</div>
        </div>
        <div class="app-title-group">
          <span class="app-title">Intent Fusion</span>
          <span class="app-subtitle">SaaS Dual-Channel Routing Engine</span>
        </div>
      </div>
      <div class="app-actions">
        <el-button class="action-btn upload-btn" @click="showUploadDialog = true">
          <el-icon class="btn-icon"><Upload /></el-icon>
          <span>上传意图类别</span>
        </el-button>
        <el-button class="action-btn refresh-btn" @click="catalog.refresh" :loading="catalog.loading">
          <el-icon class="btn-icon" v-if="!catalog.loading"><Refresh /></el-icon>
          <span>刷新意图</span>
        </el-button>
        <el-button class="action-btn clear-btn" @click="chat.clear" :disabled="chat.messages.length === 0">
          <el-icon class="btn-icon"><Delete /></el-icon>
          <span>清空对话</span>
        </el-button>
      </div>
    </el-header>
    <el-main class="app-main">
      <router-view />
    </el-main>

    <!-- 上传自定义意图类别对话框 -->
    <el-dialog 
      v-model="showUploadDialog" 
      title="上传自定义意图类别" 
      width="460px" 
      destroy-on-close
      class="custom-dialog"
    >
      <div class="dialog-body-desc">
        上传你的 <code>intents.yaml</code> 意图配置文件。系统会自动编译对应的 Embedding 向量索引并建立多级大模型推理树。
      </div>
      <el-form label-position="top">
        <el-form-item label="意图类别标识 (唯一ID,如 glsx_json)" required>
          <el-input 
            v-model="uploadName" 
            placeholder="如: glsx_json" 
            class="custom-input"
          />
        </el-form-item>
        <el-form-item label="选择 YAML 意图配置文件 (intents.yaml)" required>
          <div class="file-upload-wrapper">
            <input type="file" accept=".yaml,.yml" @change="onFileChange" class="hidden-file-input" id="yaml-file-input" />
            <label for="yaml-file-input" class="file-upload-label">
              <el-icon class="upload-icon"><Document /></el-icon>
              <span class="upload-text">{{ uploadFile ? uploadFile.name : '点击选择 intents.yaml 文件' }}</span>
            </label>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button class="dialog-btn cancel-btn" @click="showUploadDialog = false">取消</el-button>
          <el-button 
            class="dialog-btn submit-btn" 
            type="primary" 
            :loading="uploading" 
            :disabled="!uploadName || !uploadFile" 
            @click="handleUpload"
          >
            上传并自动应用
          </el-button>
        </div>
      </template>
    </el-dialog>
  </el-container>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useCatalogStore } from "./stores/catalog";
import { useChatStore } from "./stores/chat";
import { ElMessage } from "element-plus";
import { Upload, Refresh, Delete, Document } from "@element-plus/icons-vue";

const catalog = useCatalogStore();
const chat = useChatStore();

const showUploadDialog = ref(false);
const uploadName = ref("");
const uploadFile = ref<File | null>(null);
const uploading = ref(false);

function onFileChange(event: Event) {
  const target = event.target as HTMLInputElement;
  if (target.files && target.files.length > 0) {
    uploadFile.value = target.files[0];
  } else {
    uploadFile.value = null;
  }
}

async function handleUpload() {
  const datasetName = uploadName.value.trim();
  if (!datasetName || !uploadFile.value) return;

  uploading.value = true;
  try {
    const reader = new FileReader();
    reader.onload = async (e) => {
      const yamlContent = e.target?.result as string;
      try {
        const response = await fetch("/api/intents/upload", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            name: datasetName,
            yaml_content: yamlContent,
          }),
        });

        const resData = await response.json();
        if (!response.ok) {
          throw new Error(resData.error || `HTTP ${response.status}`);
        }

        ElMessage.success("上传成功!正在切换至新上传的意图类别...");
        showUploadDialog.value = false;

        // 刷新数据集列表
        await catalog.refreshDatasets();

        // 切换到新上传的类别(默认为 uploads/名称)
        const datasetKey = resData.dataset || `uploads/${datasetName}`;
        await catalog.switchDataset(datasetKey);

        // 重置状态
        uploadName.value = "";
        uploadFile.value = null;
      } catch (err: any) {
        ElMessage.error(`上传失败: ${err.message || err}`);
      } finally {
        uploading.value = false;
      }
    };
    reader.readAsText(uploadFile.value);
  } catch (err: any) {
    ElMessage.error(`读取文件失败: ${err.message || err}`);
    uploading.value = false;
  }
}

onMounted(() => {
  catalog.refreshDatasets();
  catalog.refresh();
});
</script>

<style scoped>
.app-shell {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background-color: #f8fafc;
}
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(226, 232, 240, 0.8);
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  padding: 0 24px;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.02);
}
.app-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.app-logo {
  display: flex;
  align-items: center;
  justify-content: center;
}
.logo-box {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #4f46e5 0%, #3b82f6 100%);
  color: white;
  font-weight: 800;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
  font-family: 'Plus Jakarta Sans', sans-serif;
}
.app-title-group {
  display: flex;
  flex-direction: column;
}
.app-title {
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-weight: 800;
  font-size: 16px;
  color: #0f172a;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.app-subtitle {
  font-size: 11px;
  color: #64748b;
  font-weight: 500;
}
.app-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}
.action-btn {
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  height: 36px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid #e2e8f0;
  background: #ffffff;
  color: #334155;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.action-btn:hover {
  border-color: #cbd5e1;
  background: #f8fafc;
  color: #0f172a;
  transform: translateY(-1px);
}
.action-btn:active {
  transform: translateY(0);
}
.upload-btn {
  background: linear-gradient(135deg, #4f46e5 0%, #4338ca 100%);
  border: none;
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.15);
}
.upload-btn:hover {
  background: linear-gradient(135deg, #4338ca 0%, #3730a3 100%);
  color: #ffffff;
  border: none;
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.25);
}
.clear-btn:disabled {
  opacity: 0.5;
  pointer-events: none;
  box-shadow: none;
  border-color: #f1f5f9;
  background: #f8fafc;
}
.btn-icon {
  font-size: 15px;
}
.app-main {
  padding: 0;
  overflow: hidden;
}

/* Dialog Stylings */
.dialog-body-desc {
  font-size: 13px;
  color: #64748b;
  line-height: 1.5;
  margin-bottom: 20px;
}
.dialog-body-desc code {
  background-color: #f1f5f9;
  color: #0f172a;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 12px;
}
.custom-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 8px 12px;
  border: 1px solid #e2e8f0;
  box-shadow: none !important;
  transition: all 0.2s;
}
.custom-input :deep(.el-input__wrapper):hover,
.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #818cf8;
}
.file-upload-wrapper {
  width: 100%;
}
.hidden-file-input {
  display: none;
}
.file-upload-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 48px;
  border: 2px dashed #cbd5e1;
  border-radius: 8px;
  cursor: pointer;
  background-color: #f8fafc;
  color: #64748b;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s;
}
.file-upload-label:hover {
  border-color: #818cf8;
  background-color: #f5f3ff;
  color: #4f46e5;
}
.upload-icon {
  font-size: 18px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 15px;
}
.dialog-btn {
  height: 38px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  padding: 0 20px;
}
.submit-btn {
  background: linear-gradient(135deg, #4f46e5 0%, #4338ca 100%);
  border: none;
}
.submit-btn:hover {
  background: linear-gradient(135deg, #4338ca 0%, #3730a3 100%);
}
.cancel-btn {
  border: 1px solid #e2e8f0;
  color: #475569;
}
.cancel-btn:hover {
  background-color: #f8fafc;
  color: #0f172a;
  border-color: #cbd5e1;
}
</style>