368 lines
13 KiB
Vue
368 lines
13 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* features/kb/components/DifySettingsDialog.vue
|
|
* 用途:提供知识库 Dify 连接及默认同步参数的查看、编辑和保存界面。
|
|
* 依赖:Vue 响应式 API、Element Plus、Vue I18n、kb.api 的 Dify 设置读写接口。
|
|
* 逻辑:KbAdminPanel 控制弹窗开关;本组件加载或保存设置后通过事件把结果反馈给父组件。表单按连接、检索、分段三块排,密钥默认遮挡;开关行按左标题、开关、右标题、数字四列对齐。
|
|
*/
|
|
|
|
import { reactive, ref, useId, watch } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import {
|
|
ElButton,
|
|
ElDialog,
|
|
ElForm,
|
|
ElFormItem,
|
|
ElInput,
|
|
ElInputNumber,
|
|
ElLoading,
|
|
ElOption,
|
|
ElSelect,
|
|
ElSwitch,
|
|
} from 'element-plus';
|
|
import { getKbDifySettings, updateKbDifySettings } from '../api/kb.api';
|
|
|
|
const props = defineProps<{ open: boolean }>();
|
|
const emit = defineEmits<{ close: []; saved: [message: string]; error: [message: string] }>();
|
|
const { t } = useI18n();
|
|
const vLoading = ElLoading.directive;
|
|
const rerankLabelId = useId();
|
|
const topKLabelId = useId();
|
|
const scoreSwitchLabelId = useId();
|
|
const scoreValueLabelId = useId();
|
|
|
|
const loading = ref(false);
|
|
const saving = ref(false);
|
|
const form = reactive({
|
|
DIFY_API_BASE: '',
|
|
DIFY_API_KEY: '',
|
|
KB_DIFY_PERMISSION: 'only_me',
|
|
KB_DIFY_INDEXING_TECHNIQUE: 'high_quality',
|
|
KB_DIFY_SEARCH_METHOD: 'semantic_search',
|
|
KB_DIFY_DOC_FORM: 'text_model',
|
|
KB_DIFY_RERANKING_ENABLE: true,
|
|
KB_DIFY_TOP_K: 5,
|
|
KB_DIFY_SCORE_THRESHOLD_ENABLED: true,
|
|
KB_DIFY_SCORE_THRESHOLD: 0.25,
|
|
KB_DIFY_SEGMENT_MAX_TOKENS: 600,
|
|
// Excel/CSV 专用分段 Token 上限;普通文档仍使用上面的通用配置。
|
|
KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL: 1200,
|
|
KB_DIFY_SEGMENT_CHUNK_OVERLAP: 100,
|
|
});
|
|
|
|
/**
|
|
* 将后端持久化的布尔配置转换为 checkbox 可消费的布尔值。
|
|
* 核心逻辑:兼容运行时配置服务返回的字符串与新提交表单返回的布尔值。
|
|
* @param value - 待转换的 Dify 配置值。
|
|
* @returns checkbox 使用的布尔值。
|
|
*/
|
|
const toBooleanSetting = (value: boolean | string | undefined): boolean => value === true || value === 'true';
|
|
|
|
/**
|
|
* 将后端持久化的数值配置转换为 number input 可消费的有限数值。
|
|
* 核心逻辑:保留 0 这类合法阈值;仅在缺失或非数值时使用指定默认值。
|
|
* @param value - 待转换的 Dify 配置值。
|
|
* @param fallback - 原始值无效时显示的默认值。
|
|
* @returns number input 使用的有限数值。
|
|
*/
|
|
const toNumberSetting = (value: number | string | undefined, fallback: number): number => {
|
|
if (value === '' || value === undefined) return fallback;
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : fallback;
|
|
};
|
|
|
|
/**
|
|
* 关闭 Dify 设置弹窗而不更改当前表单数据。
|
|
* 核心逻辑:向父组件发送 close 事件,由父组件更新弹窗可见状态。
|
|
* @returns void
|
|
*/
|
|
const close = () => emit('close');
|
|
|
|
/**
|
|
* 在弹窗打开时读取当前知识库 Dify 设置并填充表单。
|
|
* 核心逻辑:调用设置查询接口,并把持久化的字符串布尔值/数值转换为表单控件所需类型;读取失败时向父组件发送已本地化的错误消息。
|
|
* @returns Promise<void>
|
|
*/
|
|
const loadSettings = async () => {
|
|
loading.value = true;
|
|
const settings = await getKbDifySettings();
|
|
loading.value = false;
|
|
if (!settings) {
|
|
emit('error', t('kbDifySettings.msg.loadFailed'));
|
|
return;
|
|
}
|
|
Object.assign(form, settings, {
|
|
KB_DIFY_RERANKING_ENABLE: toBooleanSetting(settings.KB_DIFY_RERANKING_ENABLE),
|
|
KB_DIFY_TOP_K: toNumberSetting(settings.KB_DIFY_TOP_K, 5),
|
|
KB_DIFY_SCORE_THRESHOLD_ENABLED: toBooleanSetting(settings.KB_DIFY_SCORE_THRESHOLD_ENABLED),
|
|
KB_DIFY_SCORE_THRESHOLD: toNumberSetting(settings.KB_DIFY_SCORE_THRESHOLD, 0.25),
|
|
KB_DIFY_SEGMENT_MAX_TOKENS: toNumberSetting(settings.KB_DIFY_SEGMENT_MAX_TOKENS, 600),
|
|
KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL: toNumberSetting(settings.KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL, 1200),
|
|
KB_DIFY_SEGMENT_CHUNK_OVERLAP: toNumberSetting(settings.KB_DIFY_SEGMENT_CHUNK_OVERLAP, 100),
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 保存当前表单中的 Dify 设置,并在成功后关闭弹窗。
|
|
* 核心逻辑:提交设置更新接口;失败反馈接口错误或本地化兜底消息,成功反馈后关闭。
|
|
* @returns Promise<void>
|
|
*/
|
|
const save = async () => {
|
|
saving.value = true;
|
|
const result = await updateKbDifySettings(form);
|
|
saving.value = false;
|
|
if (!result.ok) {
|
|
emit('error', result.error || t('kbDifySettings.msg.saveFailed'));
|
|
return;
|
|
}
|
|
emit('saved', t('kbDifySettings.msg.saved'));
|
|
emit('close');
|
|
};
|
|
|
|
watch(() => props.open, (open) => {
|
|
if (open) void loadSettings();
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<ElDialog
|
|
:model-value="open"
|
|
width="680px"
|
|
align-center
|
|
append-to-body
|
|
:close-on-click-modal="false"
|
|
@update:model-value="(value) => { if (!value) close(); }"
|
|
>
|
|
<template #header="{ titleId, titleClass }">
|
|
<div class="dify-settings-heading">
|
|
<span :id="titleId" :class="titleClass">{{ t('kbDifySettings.title') }}</span>
|
|
<p class="dify-settings-subtitle">{{ t('kbDifySettings.subtitle') }}</p>
|
|
</div>
|
|
</template>
|
|
<div v-loading="loading" class="dify-settings-body">
|
|
<ElForm v-if="!loading" class="dify-settings-form" label-position="top" :model="form">
|
|
<section class="dify-settings-section">
|
|
<p class="dify-settings-section-title">{{ t('kbDifySettings.sections.connection') }}</p>
|
|
<div class="dify-settings-panel dify-settings-stack">
|
|
<ElFormItem :label="t('kbDifySettings.fields.baseUrl')">
|
|
<ElInput v-model="form.DIFY_API_BASE" placeholder="https://api.dify.ai/v1" />
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.apiKey')">
|
|
<ElInput v-model="form.DIFY_API_KEY" type="password" show-password autocomplete="off" placeholder="app-..." />
|
|
</ElFormItem>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="dify-settings-section">
|
|
<p class="dify-settings-section-title">{{ t('kbDifySettings.sections.retrieval') }}</p>
|
|
<div class="dify-settings-panel">
|
|
<div class="dify-settings-grid">
|
|
<ElFormItem :label="t('kbDifySettings.fields.permission')">
|
|
<ElSelect v-model="form.KB_DIFY_PERMISSION">
|
|
<ElOption value="only_me" :label="t('kbDifySettings.options.permission.onlyMe')" />
|
|
<ElOption value="all_team_members" :label="t('kbDifySettings.options.permission.allTeamMembers')" />
|
|
</ElSelect>
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.indexingTechnique')">
|
|
<ElSelect v-model="form.KB_DIFY_INDEXING_TECHNIQUE">
|
|
<ElOption value="high_quality" :label="t('kbDifySettings.options.indexingTechnique.highQuality')" />
|
|
<ElOption value="economy" :label="t('kbDifySettings.options.indexingTechnique.economy')" />
|
|
</ElSelect>
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.searchMethod')">
|
|
<ElSelect v-model="form.KB_DIFY_SEARCH_METHOD">
|
|
<ElOption value="semantic_search" :label="t('kbDifySettings.options.searchMethod.semanticSearch')" />
|
|
<ElOption value="hybrid_search" :label="t('kbDifySettings.options.searchMethod.hybridSearch')" />
|
|
<ElOption value="full_text_search" :label="t('kbDifySettings.options.searchMethod.fullTextSearch')" />
|
|
</ElSelect>
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.documentForm')">
|
|
<ElSelect v-model="form.KB_DIFY_DOC_FORM">
|
|
<ElOption value="text_model" :label="t('kbDifySettings.options.documentForm.textModel')" />
|
|
</ElSelect>
|
|
</ElFormItem>
|
|
</div>
|
|
|
|
<div class="dify-settings-switch-grid">
|
|
<div class="dify-settings-switch-row">
|
|
<span :id="rerankLabelId">{{ t('kbDifySettings.fields.rerankingEnable') }}</span>
|
|
<ElSwitch v-model="form.KB_DIFY_RERANKING_ENABLE" :aria-labelledby="rerankLabelId" />
|
|
<span :id="topKLabelId">{{ t('kbDifySettings.fields.topK') }}</span>
|
|
<ElInputNumber
|
|
v-model="form.KB_DIFY_TOP_K"
|
|
:min="1"
|
|
:max="10"
|
|
:step="1"
|
|
controls-position="right"
|
|
:aria-labelledby="topKLabelId"
|
|
/>
|
|
</div>
|
|
<div class="dify-settings-switch-row">
|
|
<span :id="scoreSwitchLabelId">{{ t('kbDifySettings.fields.scoreThresholdEnabled') }}</span>
|
|
<ElSwitch v-model="form.KB_DIFY_SCORE_THRESHOLD_ENABLED" :aria-labelledby="scoreSwitchLabelId" />
|
|
<span :id="scoreValueLabelId">{{ t('kbDifySettings.fields.scoreThreshold') }}</span>
|
|
<ElInputNumber
|
|
v-model="form.KB_DIFY_SCORE_THRESHOLD"
|
|
:min="0"
|
|
:max="1"
|
|
:step="0.01"
|
|
:precision="2"
|
|
controls-position="right"
|
|
:disabled="!form.KB_DIFY_SCORE_THRESHOLD_ENABLED"
|
|
:aria-labelledby="scoreValueLabelId"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="dify-settings-section">
|
|
<p class="dify-settings-section-title">{{ t('kbDifySettings.sections.chunking') }}</p>
|
|
<div class="dify-settings-panel dify-settings-segments">
|
|
<ElFormItem :label="t('kbDifySettings.fields.segmentMaxTokens')">
|
|
<ElInputNumber v-model="form.KB_DIFY_SEGMENT_MAX_TOKENS" :min="1" :step="1" controls-position="right" />
|
|
<p class="dify-settings-hint">{{ t('kbDifySettings.hints.maxTokens') }}</p>
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.segmentMaxTokensExcel')">
|
|
<ElInputNumber v-model="form.KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL" :min="1" :step="1" controls-position="right" />
|
|
<p class="dify-settings-hint">{{ t('kbDifySettings.hints.maxTokens') }}</p>
|
|
</ElFormItem>
|
|
<ElFormItem :label="t('kbDifySettings.fields.segmentChunkOverlap')">
|
|
<ElInputNumber v-model="form.KB_DIFY_SEGMENT_CHUNK_OVERLAP" :min="0" :step="1" controls-position="right" />
|
|
<p class="dify-settings-hint">{{ t('kbDifySettings.hints.tokens') }}</p>
|
|
</ElFormItem>
|
|
</div>
|
|
</section>
|
|
</ElForm>
|
|
</div>
|
|
<template #footer>
|
|
<ElButton @click="close">{{ t('common.cancel') }}</ElButton>
|
|
<ElButton type="primary" :loading="saving" @click="save">{{ t('kbDifySettings.save') }}</ElButton>
|
|
</template>
|
|
</ElDialog>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.dify-settings-heading {
|
|
padding-right: 24px;
|
|
}
|
|
|
|
.dify-settings-subtitle {
|
|
margin: 4px 0 0;
|
|
color: var(--el-text-color-secondary);
|
|
font-size: var(--el-font-size-extra-small);
|
|
font-weight: 400;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
.dify-settings-body {
|
|
min-height: 8rem;
|
|
}
|
|
|
|
.dify-settings-section + .dify-settings-section {
|
|
margin-top: 16px;
|
|
}
|
|
|
|
.dify-settings-section-title {
|
|
margin: 0 0 8px;
|
|
color: var(--el-text-color-primary);
|
|
font-size: var(--el-font-size-base);
|
|
font-weight: 600;
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.dify-settings-panel {
|
|
padding: 16px;
|
|
border: 1px solid var(--el-border-color-lighter);
|
|
border-radius: var(--el-border-radius-base);
|
|
}
|
|
|
|
.dify-settings-stack,
|
|
.dify-settings-grid,
|
|
.dify-settings-segments {
|
|
display: grid;
|
|
gap: 12px 16px;
|
|
}
|
|
|
|
.dify-settings-grid,
|
|
.dify-settings-segments {
|
|
grid-template-columns: 1fr 1fr;
|
|
}
|
|
|
|
.dify-settings-segments {
|
|
grid-template-columns: 1fr 1fr 1fr;
|
|
}
|
|
|
|
.dify-settings-form :deep(.el-form-item) {
|
|
margin-bottom: 0;
|
|
}
|
|
|
|
.dify-settings-form :deep(.el-form-item__label) {
|
|
margin-bottom: 4px;
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.dify-settings-form :deep(.el-select),
|
|
.dify-settings-stack :deep(.el-input),
|
|
.dify-settings-segments :deep(.el-input-number) {
|
|
width: 100%;
|
|
}
|
|
|
|
.dify-settings-segments :deep(.el-form-item__content) {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: stretch;
|
|
}
|
|
|
|
.dify-settings-hint {
|
|
margin: 4px 0 0;
|
|
color: var(--el-text-color-secondary);
|
|
font-size: var(--el-font-size-extra-small);
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.dify-settings-switch-grid {
|
|
display: grid;
|
|
grid-template-columns: minmax(0, 1fr) auto max-content 8.5rem;
|
|
column-gap: 16px;
|
|
}
|
|
|
|
.dify-settings-switch-row {
|
|
display: grid;
|
|
grid-template-columns: subgrid;
|
|
grid-column: 1 / -1;
|
|
align-items: center;
|
|
margin-top: 12px;
|
|
padding-top: 12px;
|
|
border-top: 1px solid var(--el-border-color-lighter);
|
|
color: var(--el-text-color-regular);
|
|
font-size: var(--el-font-size-base);
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.dify-settings-form :deep(.el-input-number .el-input__inner) {
|
|
text-align: left;
|
|
}
|
|
|
|
.dify-settings-switch-row :deep(.el-input-number) {
|
|
width: 8.5rem;
|
|
}
|
|
|
|
@media (max-width: 640px) {
|
|
.dify-settings-grid,
|
|
.dify-settings-segments {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
.dify-settings-switch-grid,
|
|
.dify-settings-switch-row {
|
|
grid-template-columns: 1fr;
|
|
row-gap: 8px;
|
|
}
|
|
|
|
.dify-settings-switch-row :deep(.el-input-number) {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|