feat(weknoracloud): implement credential management and status checks
- Introduced a new WeKnoraCloudSettings component for managing APPID and APPSECRET. - Added functionality to save WeKnoraCloud credentials without automatically creating models. - Implemented credential status checks across various components to provide user feedback on configuration status. - Updated API endpoints to handle credential saving and status retrieval. - Enhanced UI elements to guide users in configuring WeKnoraCloud settings. This update improves the integration of WeKnoraCloud by allowing users to manage their credentials more effectively and receive real-time feedback on their configuration status.
This commit is contained in:
@@ -131,16 +131,22 @@ export interface InitializeWeKnoraCloudRequest {
|
|||||||
app_secret: string
|
app_secret: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InitializeWeKnoraCloudResult {
|
// 仅保存 WeKnoraCloud 凭证,不自动创建模型
|
||||||
models: Array<{
|
export function saveWeKnoraCloudCredentials(data: InitializeWeKnoraCloudRequest): Promise<{ success: boolean; message: string }> {
|
||||||
name: string
|
return new Promise((resolve, reject) => {
|
||||||
type: string
|
post('/api/v1/weknoracloud/credentials', data)
|
||||||
action: 'created' | 'updated'
|
.then((response: any) => {
|
||||||
}>
|
if (response.success) {
|
||||||
}
|
resolve(response)
|
||||||
|
} else {
|
||||||
export function initializeWeKnoraCloud(data: InitializeWeKnoraCloudRequest): Promise<InitializeWeKnoraCloudResult> {
|
reject(new Error(response.message || response.error || '凭证保存失败'))
|
||||||
return post('/api/v1/models/weknoracloud/initialize', data) as Promise<InitializeWeKnoraCloudResult>
|
}
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
console.error('Failed to save WeKnoraCloud credentials:', error)
|
||||||
|
reject(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WeKnoraCloudStatusResult {
|
export interface WeKnoraCloudStatusResult {
|
||||||
|
|||||||
@@ -144,27 +144,76 @@
|
|||||||
</t-select>
|
</t-select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- WeKnoraCloud 提示信息 -->
|
||||||
|
<template v-if="formData.provider === 'weknoracloud'">
|
||||||
|
<!-- 凭证已配置 -->
|
||||||
|
<div v-if="wkcCredentialState === 'configured'" class="weknoracloud-hint weknoracloud-hint--ok">
|
||||||
|
<t-icon name="check-circle-filled" style="font-size: 16px; color: var(--td-success-color); flex-shrink: 0;" />
|
||||||
|
<div>
|
||||||
|
WeKnoraCloud 凭证已配置。
|
||||||
|
支持的模型可参考
|
||||||
|
<a
|
||||||
|
href="https://developers.weixin.qq.com/doc/aispeech/knowledge/atomic_capability/atomic_interface.html"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
class="weknoracloud-hint-link"
|
||||||
|
>接口文档</a>。
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 未配置 / 失效 -->
|
||||||
|
<div v-else-if="wkcCredentialState !== 'loading'" class="weknoracloud-hint weknoracloud-hint--warn">
|
||||||
|
<t-icon name="error-circle-filled" style="font-size: 16px; color: #f97316; flex-shrink: 0;" />
|
||||||
|
<div style="flex: 1;">
|
||||||
|
<template v-if="wkcCredentialState === 'expired'">
|
||||||
|
WeKnoraCloud 凭证已失效,请重新配置。
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
尚未配置 WeKnoraCloud 凭证,请先填写 APPID 和 APPSECRET。
|
||||||
|
</template>
|
||||||
|
<div style="margin-top: 8px;">
|
||||||
|
<t-button
|
||||||
|
variant="text"
|
||||||
|
size="small"
|
||||||
|
theme="primary"
|
||||||
|
@click="goToWeKnoraCloudSettings"
|
||||||
|
style="padding: 0; height: auto;"
|
||||||
|
>
|
||||||
|
前往设置中配置
|
||||||
|
</t-button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 加载中 -->
|
||||||
|
<div v-else class="weknoracloud-hint">
|
||||||
|
<t-icon name="loading" class="spinning" style="font-size: 16px; color: var(--td-text-color-placeholder); flex-shrink: 0;" />
|
||||||
|
<span>正在检查凭证状态...</span>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<!-- 模型名称 -->
|
<!-- 模型名称 -->
|
||||||
<div class="form-item">
|
<div class="form-item">
|
||||||
<label class="form-label required">{{ $t('model.modelName') }}</label>
|
<label class="form-label required">{{ $t('model.modelName') }}</label>
|
||||||
<t-input
|
<t-input
|
||||||
v-model="formData.modelName"
|
v-model="formData.modelName"
|
||||||
:placeholder="getModelNamePlaceholder()"
|
:placeholder="getModelNamePlaceholder()"
|
||||||
|
:disabled="formData.provider === 'weknoracloud' && wkcCredentialState !== 'configured'"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-item">
|
<div v-if="formData.provider !== 'weknoracloud'" class="form-item">
|
||||||
<label class="form-label required">{{ $t('model.editor.baseUrlLabel') }}</label>
|
<label class="form-label required">{{ $t('model.editor.baseUrlLabel') }}</label>
|
||||||
<t-input
|
<t-input
|
||||||
v-model="formData.baseUrl"
|
v-model="formData.baseUrl"
|
||||||
:placeholder="getBaseUrlPlaceholder()"
|
:placeholder="getBaseUrlPlaceholder()"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-item">
|
<div v-if="formData.provider !== 'weknoracloud'" class="form-item">
|
||||||
<label class="form-label">{{ $t('model.editor.apiKeyOptional') }}</label>
|
<label class="form-label">{{ $t('model.editor.apiKeyOptional') }}</label>
|
||||||
<t-input
|
<t-input
|
||||||
v-model="formData.apiKey"
|
v-model="formData.apiKey"
|
||||||
type="password"
|
type="password"
|
||||||
:placeholder="$t('model.editor.apiKeyPlaceholder')"
|
:placeholder="$t('model.editor.apiKeyPlaceholder')"
|
||||||
/>
|
/>
|
||||||
@@ -178,7 +227,7 @@
|
|||||||
variant="outline"
|
variant="outline"
|
||||||
@click="checkRemoteAPI"
|
@click="checkRemoteAPI"
|
||||||
:loading="checking"
|
:loading="checking"
|
||||||
:disabled="!formData.modelName || !formData.baseUrl"
|
:disabled="!formData.modelName || (!formData.baseUrl && formData.provider !== 'weknoracloud') || (formData.provider === 'weknoracloud' && wkcCredentialState !== 'configured')"
|
||||||
>
|
>
|
||||||
<template #icon>
|
<template #icon>
|
||||||
<t-icon
|
<t-icon
|
||||||
@@ -248,7 +297,7 @@
|
|||||||
<t-button theme="default" variant="outline" @click="handleCancel">
|
<t-button theme="default" variant="outline" @click="handleCancel">
|
||||||
{{ $t('common.cancel') }}
|
{{ $t('common.cancel') }}
|
||||||
</t-button>
|
</t-button>
|
||||||
<t-button theme="primary" @click="handleConfirm" :loading="saving">
|
<t-button theme="primary" @click="handleConfirm" :loading="saving" :disabled="formData.provider === 'weknoracloud' && wkcCredentialState !== 'configured'">
|
||||||
{{ $t('common.save') }}
|
{{ $t('common.save') }}
|
||||||
</t-button>
|
</t-button>
|
||||||
</div>
|
</div>
|
||||||
@@ -262,6 +311,7 @@
|
|||||||
import { ref, watch, computed, onUnmounted, nextTick } from 'vue'
|
import { ref, watch, computed, onUnmounted, nextTick } from 'vue'
|
||||||
import { MessagePlugin } from 'tdesign-vue-next'
|
import { MessagePlugin } from 'tdesign-vue-next'
|
||||||
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, checkASRModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, checkOllamaStatus, listModelProviders, type OllamaModelInfo, type ModelProviderOption } from '@/api/initialization'
|
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, checkASRModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, checkOllamaStatus, listModelProviders, type OllamaModelInfo, type ModelProviderOption } from '@/api/initialization'
|
||||||
|
import { getWeKnoraCloudStatus } from '@/api/model'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import { useUIStore } from '@/stores/ui'
|
import { useUIStore } from '@/stores/ui'
|
||||||
|
|
||||||
@@ -483,6 +533,34 @@ let downloadInterval: any = null
|
|||||||
const ollamaServiceStatus = ref<boolean | null>(null)
|
const ollamaServiceStatus = ref<boolean | null>(null)
|
||||||
const checkingOllamaStatus = ref(false)
|
const checkingOllamaStatus = ref(false)
|
||||||
|
|
||||||
|
// WeKnoraCloud 凭证状态
|
||||||
|
const wkcCredentialState = ref<'loading' | 'unconfigured' | 'configured' | 'expired'>('loading')
|
||||||
|
|
||||||
|
const checkWkcCredentialStatus = async () => {
|
||||||
|
wkcCredentialState.value = 'loading'
|
||||||
|
try {
|
||||||
|
const status = await getWeKnoraCloudStatus()
|
||||||
|
if (status.needs_reinit) {
|
||||||
|
wkcCredentialState.value = 'expired'
|
||||||
|
} else if (status.has_models) {
|
||||||
|
wkcCredentialState.value = 'configured'
|
||||||
|
} else {
|
||||||
|
wkcCredentialState.value = 'unconfigured'
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
wkcCredentialState.value = 'unconfigured'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const goToWeKnoraCloudSettings = async () => {
|
||||||
|
emit('update:visible', false)
|
||||||
|
if (uiStore.showSettingsModal) {
|
||||||
|
uiStore.closeSettings()
|
||||||
|
await nextTick()
|
||||||
|
}
|
||||||
|
uiStore.openSettings('weknoracloud')
|
||||||
|
}
|
||||||
|
|
||||||
const formData = ref<ModelFormData>({
|
const formData = ref<ModelFormData>({
|
||||||
id: '',
|
id: '',
|
||||||
name: '',
|
name: '',
|
||||||
@@ -625,6 +703,11 @@ watch(() => props.visible, (val) => {
|
|||||||
if (props.modelType === 'rerank') {
|
if (props.modelType === 'rerank') {
|
||||||
formData.value.source = 'remote'
|
formData.value.source = 'remote'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果当前 provider 是 WeKnoraCloud,检查凭证状态
|
||||||
|
if (formData.value.provider === 'weknoracloud') {
|
||||||
|
checkWkcCredentialStatus()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 恢复背景滚动
|
// 恢复背景滚动
|
||||||
document.body.style.overflow = ''
|
document.body.style.overflow = ''
|
||||||
@@ -670,6 +753,10 @@ const handleProviderChange = (value: string) => {
|
|||||||
remoteAvailable.value = false
|
remoteAvailable.value = false
|
||||||
remoteMessage.value = ''
|
remoteMessage.value = ''
|
||||||
}
|
}
|
||||||
|
// WeKnoraCloud: 检查凭证状态
|
||||||
|
if (value === 'weknoracloud') {
|
||||||
|
checkWkcCredentialStatus()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听来源变化,重置校验状态(已合并到下面的 watch)
|
// 监听来源变化,重置校验状态(已合并到下面的 watch)
|
||||||
@@ -803,7 +890,7 @@ const checkOllamaDimension = async () => {
|
|||||||
|
|
||||||
// 检查 Remote API 连接(根据模型类型调用不同的接口)
|
// 检查 Remote API 连接(根据模型类型调用不同的接口)
|
||||||
const checkRemoteAPI = async () => {
|
const checkRemoteAPI = async () => {
|
||||||
if (!formData.value.modelName || !formData.value.baseUrl) {
|
if (!formData.value.modelName || (!formData.value.baseUrl && formData.value.provider !== 'weknoracloud')) {
|
||||||
MessagePlugin.warning(t('model.editor.fillModelAndUrl'))
|
MessagePlugin.warning(t('model.editor.fillModelAndUrl'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -918,8 +1005,8 @@ const handleConfirm = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果是 remote 类型,必须填写 baseUrl
|
// 如果是 remote 类型且非 WeKnoraCloud,必须填写 baseUrl
|
||||||
if (formData.value.source === 'remote') {
|
if (formData.value.source === 'remote' && formData.value.provider !== 'weknoracloud') {
|
||||||
if (!formData.value.baseUrl || !formData.value.baseUrl.trim()) {
|
if (!formData.value.baseUrl || !formData.value.baseUrl.trim()) {
|
||||||
MessagePlugin.warning(t('model.editor.remoteBaseUrlRequired'))
|
MessagePlugin.warning(t('model.editor.remoteBaseUrlRequired'))
|
||||||
return
|
return
|
||||||
@@ -1414,6 +1501,39 @@ const handleOverlayMouseUp = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WeKnoraCloud 提示信息
|
||||||
|
.weknoracloud-hint {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
padding: 10px 12px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
line-height: 1.5;
|
||||||
|
|
||||||
|
&--ok {
|
||||||
|
background: var(--td-success-color-light);
|
||||||
|
border: 1px solid var(--td-success-color-focus);
|
||||||
|
}
|
||||||
|
|
||||||
|
&--warn {
|
||||||
|
background: #fff7ed;
|
||||||
|
border: 1px solid #fed7aa;
|
||||||
|
border-left: 3px solid #f97316;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weknoracloud-hint-link {
|
||||||
|
color: var(--td-brand-color);
|
||||||
|
text-decoration: none;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Ollama 模型选择器样式
|
// Ollama 模型选择器样式
|
||||||
.model-option {
|
.model-option {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -93,12 +93,14 @@ import { storeToRefs } from 'pinia'
|
|||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
|
||||||
function getEngineDisplayName(engineName: string): string {
|
function getEngineDisplayName(engineName: string): string {
|
||||||
|
if (engineName === 'weknoracloud') return 'WeKnora Cloud'
|
||||||
const key = `kbSettings.parser.engines.${engineName}.name`
|
const key = `kbSettings.parser.engines.${engineName}.name`
|
||||||
const translated = t(key)
|
const translated = t(key)
|
||||||
return translated !== key ? translated : engineName
|
return translated !== key ? translated : engineName
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEngineDisplayDesc(engineName: string, fallback: string): string {
|
function getEngineDisplayDesc(engineName: string, fallback: string): string {
|
||||||
|
if (engineName === 'weknoracloud') return '使用 WeKnora Cloud 进行文档解析'
|
||||||
const key = `kbSettings.parser.engines.${engineName}.desc`
|
const key = `kbSettings.parser.engines.${engineName}.desc`
|
||||||
const translated = t(key)
|
const translated = t(key)
|
||||||
return translated !== key ? translated : fallback
|
return translated !== key ? translated : fallback
|
||||||
|
|||||||
@@ -18,46 +18,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- WeKnoraCloud 厂商配置 -->
|
|
||||||
<div class="weknoracloud-config-section" style="margin-bottom: 24px;">
|
|
||||||
<div class="category-header" style="margin-bottom: 12px;">
|
|
||||||
<h3 style="margin: 0; font-size: 16px; font-weight: 600;">WeKnoraCloud 厂商配置</h3>
|
|
||||||
</div>
|
|
||||||
<!-- 凭证失效警告 -->
|
|
||||||
<div v-if="weKnoraCloudNeedsReinit" style="margin-bottom: 12px; background: #fff7ed; border: 1px solid #fed7aa; border-left: 3px solid #f97316; border-radius: 6px; padding: 12px 16px; display: flex; align-items: flex-start; gap: 10px;">
|
|
||||||
<t-icon name="error-circle" style="font-size: 16px; color: #f97316; flex-shrink: 0; margin-top: 1px;" />
|
|
||||||
<div style="font-size: 13px; color: #9a3412; line-height: 1.5;">
|
|
||||||
<strong>WeKnoraCloud 凭证已失效</strong><br />
|
|
||||||
{{ weKnoraCloudReinitReason || '服务重启后加密密钥已变更,已保存的凭证无法解密。' }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="weknoracloud-config-card" style="background: var(--td-bg-color-container); border: 1px solid var(--td-component-stroke); border-radius: 8px; padding: 20px;">
|
|
||||||
<div style="display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap;">
|
|
||||||
<t-form-item label="APPID" style="margin-bottom: 0;">
|
|
||||||
<t-input v-model="weKnoraCloudForm.appId" placeholder="请输入 APPID" style="width: 240px;" />
|
|
||||||
</t-form-item>
|
|
||||||
<t-form-item label="APPSECRET" style="margin-bottom: 0;">
|
|
||||||
<t-input
|
|
||||||
v-model="weKnoraCloudForm.appSecret"
|
|
||||||
type="password"
|
|
||||||
placeholder="请输入 APPSECRET"
|
|
||||||
style="width: 240px;"
|
|
||||||
/>
|
|
||||||
</t-form-item>
|
|
||||||
<t-button
|
|
||||||
theme="primary"
|
|
||||||
:loading="weKnoraCloudLoading"
|
|
||||||
@click="handleWeKnoraCloudInit"
|
|
||||||
>
|
|
||||||
保存并初始化
|
|
||||||
</t-button>
|
|
||||||
</div>
|
|
||||||
<p style="color: var(--td-text-color-secondary); font-size: 12px; margin-top: 12px; margin-bottom: 0;">
|
|
||||||
提交后将自动创建或更新三个模型(Chat / Embedding / Rerank)并设为各类型默认
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 对话模型 -->
|
<!-- 对话模型 -->
|
||||||
<div class="settings-group model-type-group" data-model-type="chat">
|
<div class="settings-group model-type-group" data-model-type="chat">
|
||||||
<div class="section-subheader">
|
<div class="section-subheader">
|
||||||
@@ -335,7 +295,7 @@ import { MessagePlugin } from 'tdesign-vue-next'
|
|||||||
import { AddIcon } from 'tdesign-icons-vue-next'
|
import { AddIcon } from 'tdesign-icons-vue-next'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import ModelEditorDialog from '@/components/ModelEditorDialog.vue'
|
import ModelEditorDialog from '@/components/ModelEditorDialog.vue'
|
||||||
import { listModels, createModel, updateModel as updateModelAPI, deleteModel as deleteModelAPI, initializeWeKnoraCloud, getWeKnoraCloudStatus, type ModelConfig } from '@/api/model'
|
import { listModels, createModel, updateModel as updateModelAPI, deleteModel as deleteModelAPI, type ModelConfig } from '@/api/model'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
|
||||||
@@ -344,38 +304,6 @@ const currentModelType = ref<'chat' | 'embedding' | 'rerank' | 'vllm' | 'asr'>('
|
|||||||
const editingModel = ref<any>(null)
|
const editingModel = ref<any>(null)
|
||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
|
|
||||||
// WeKnoraCloud 配置
|
|
||||||
const weKnoraCloudForm = ref({ appId: '', appSecret: '' })
|
|
||||||
const weKnoraCloudLoading = ref(false)
|
|
||||||
const weKnoraCloudNeedsReinit = ref(false)
|
|
||||||
const weKnoraCloudReinitReason = ref('')
|
|
||||||
|
|
||||||
const handleWeKnoraCloudInit = async () => {
|
|
||||||
if (!weKnoraCloudForm.value.appId || !weKnoraCloudForm.value.appSecret) {
|
|
||||||
MessagePlugin.warning('请填写 APPID 和 APPSECRET')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
weKnoraCloudLoading.value = true
|
|
||||||
try {
|
|
||||||
const result = await initializeWeKnoraCloud({
|
|
||||||
app_id: weKnoraCloudForm.value.appId,
|
|
||||||
app_secret: weKnoraCloudForm.value.appSecret,
|
|
||||||
})
|
|
||||||
const updatedCount = result.models.filter((m: any) => m.action === 'updated').length
|
|
||||||
const createdCount = result.models.filter((m: any) => m.action === 'created').length
|
|
||||||
MessagePlugin.success(`初始化成功:新建 ${createdCount} 个,更新 ${updatedCount} 个模型`)
|
|
||||||
weKnoraCloudForm.value.appId = ''
|
|
||||||
weKnoraCloudForm.value.appSecret = ''
|
|
||||||
weKnoraCloudNeedsReinit.value = false
|
|
||||||
weKnoraCloudReinitReason.value = ''
|
|
||||||
await loadModels()
|
|
||||||
} catch (err: any) {
|
|
||||||
MessagePlugin.error(err?.message || 'WeKnoraCloud 初始化失败')
|
|
||||||
} finally {
|
|
||||||
weKnoraCloudLoading.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 模型列表数据
|
// 模型列表数据
|
||||||
const allModels = ref<ModelConfig[]>([])
|
const allModels = ref<ModelConfig[]>([])
|
||||||
|
|
||||||
@@ -433,10 +361,6 @@ const loadModels = async () => {
|
|||||||
// 直接获取所有模型,不分类型
|
// 直接获取所有模型,不分类型
|
||||||
const models = await listModels()
|
const models = await listModels()
|
||||||
allModels.value = models
|
allModels.value = models
|
||||||
// 检查 WeKnoraCloud 凭证状态
|
|
||||||
const status = await getWeKnoraCloudStatus()
|
|
||||||
weKnoraCloudNeedsReinit.value = status.needs_reinit
|
|
||||||
weKnoraCloudReinitReason.value = status.reason || ''
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('加载模型列表失败:', error)
|
console.error('加载模型列表失败:', error)
|
||||||
MessagePlugin.error(error.message)
|
MessagePlugin.error(error.message)
|
||||||
|
|||||||
@@ -93,6 +93,34 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- weknoracloud: 凭证状态 -->
|
||||||
|
<template v-if="engine.Name === 'weknoracloud'">
|
||||||
|
<div v-if="wkcState === 'configured'" class="wkc-status wkc-status--ok">
|
||||||
|
<t-icon name="check-circle" style="font-size: 15px; color: var(--td-success-color); flex-shrink: 0;" />
|
||||||
|
<span>WeKnoraCloud 凭证已配置</span>
|
||||||
|
</div>
|
||||||
|
<div v-else-if="wkcState === 'loading'" class="wkc-status">
|
||||||
|
<t-loading size="small" />
|
||||||
|
<span>检查凭证状态...</span>
|
||||||
|
</div>
|
||||||
|
<div v-else class="wkc-status wkc-status--warn">
|
||||||
|
<t-icon name="error-circle" style="font-size: 15px; color: #f97316; flex-shrink: 0;" />
|
||||||
|
<div style="flex: 1;">
|
||||||
|
<span v-if="wkcState === 'expired'">凭证已失效,请重新配置。</span>
|
||||||
|
<span v-else>尚未配置凭证。</span>
|
||||||
|
<div style="margin-top: 6px;">
|
||||||
|
<t-button
|
||||||
|
variant="text"
|
||||||
|
size="small"
|
||||||
|
theme="primary"
|
||||||
|
@click="goToWkcSettings"
|
||||||
|
style="padding: 0; height: auto;"
|
||||||
|
>前往设置中配置</t-button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<div v-if="engine.FileTypes && engine.FileTypes.length" class="file-types">
|
<div v-if="engine.FileTypes && engine.FileTypes.length" class="file-types">
|
||||||
<t-tag
|
<t-tag
|
||||||
v-for="ft in engine.FileTypes"
|
v-for="ft in engine.FileTypes"
|
||||||
@@ -190,8 +218,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, onMounted } from 'vue'
|
import { ref, computed, onMounted, nextTick } from 'vue'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
|
import { useUIStore } from '@/stores/ui'
|
||||||
import {
|
import {
|
||||||
getParserEngines,
|
getParserEngines,
|
||||||
getParserEngineConfig,
|
getParserEngineConfig,
|
||||||
@@ -200,13 +229,16 @@ import {
|
|||||||
type ParserEngineInfo,
|
type ParserEngineInfo,
|
||||||
type ParserEngineConfig,
|
type ParserEngineConfig,
|
||||||
} from '@/api/system'
|
} from '@/api/system'
|
||||||
|
import { getWeKnoraCloudStatus } from '@/api/model'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
const uiStore = useUIStore()
|
||||||
|
|
||||||
const CONFIGURABLE_ENGINES = new Set(['mineru', 'mineru_cloud'])
|
const CONFIGURABLE_ENGINES = new Set(['mineru', 'mineru_cloud'])
|
||||||
|
|
||||||
/** 各解析引擎的项目/官方文档地址 */
|
/** 各解析引擎的项目/官方文档地址 */
|
||||||
const ENGINE_DOC_LINKS: Record<string, string> = {
|
const ENGINE_DOC_LINKS: Record<string, string> = {
|
||||||
|
weknoracloud: 'https://developers.weixin.qq.com/doc/aispeech/knowledge/atomic_capability/atomic_interface.html',
|
||||||
markitdown: 'https://github.com/microsoft/markitdown',
|
markitdown: 'https://github.com/microsoft/markitdown',
|
||||||
mineru: 'https://github.com/opendatalab/MinerU',
|
mineru: 'https://github.com/opendatalab/MinerU',
|
||||||
mineru_cloud: 'https://mineru.net/apiManage/docs',
|
mineru_cloud: 'https://mineru.net/apiManage/docs',
|
||||||
@@ -249,10 +281,11 @@ const hasBuiltinEngine = computed(() => engines.value.some(e => e.Name === 'buil
|
|||||||
/** 固定展示顺序,未列出的引擎排在末尾按名称排序 */
|
/** 固定展示顺序,未列出的引擎排在末尾按名称排序 */
|
||||||
const ENGINE_ORDER: Record<string, number> = {
|
const ENGINE_ORDER: Record<string, number> = {
|
||||||
builtin: 0,
|
builtin: 0,
|
||||||
simple: 1,
|
weknoracloud: 1,
|
||||||
markitdown: 2,
|
simple: 2,
|
||||||
mineru: 3,
|
markitdown: 3,
|
||||||
mineru_cloud: 4,
|
mineru: 4,
|
||||||
|
mineru_cloud: 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortedEngines = computed(() => {
|
const sortedEngines = computed(() => {
|
||||||
@@ -277,12 +310,16 @@ function engineDocLabel(_name: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getEngineDisplayName(engineName: string): string {
|
function getEngineDisplayName(engineName: string): string {
|
||||||
|
// WeKnoraCloud 特殊处理
|
||||||
|
if (engineName === 'weknoracloud') return 'WeKnora Cloud'
|
||||||
const key = `kbSettings.parser.engines.${engineName}.name`
|
const key = `kbSettings.parser.engines.${engineName}.name`
|
||||||
const translated = t(key)
|
const translated = t(key)
|
||||||
return translated !== key ? translated : engineName
|
return translated !== key ? translated : engineName
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEngineDisplayDesc(engineName: string, fallback: string): string {
|
function getEngineDisplayDesc(engineName: string, fallback: string): string {
|
||||||
|
// WeKnoraCloud 特殊处理
|
||||||
|
if (engineName === 'weknoracloud') return '使用 WeKnora Cloud 进行文档解析,需先在「设置 → WeKnora Cloud」中配置凭证'
|
||||||
const key = `kbSettings.parser.engines.${engineName}.desc`
|
const key = `kbSettings.parser.engines.${engineName}.desc`
|
||||||
const translated = t(key)
|
const translated = t(key)
|
||||||
return translated !== key ? translated : fallback
|
return translated !== key ? translated : fallback
|
||||||
@@ -331,7 +368,7 @@ async function loadConfig() {
|
|||||||
async function loadAll() {
|
async function loadAll() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = ''
|
error.value = ''
|
||||||
await Promise.all([loadEngines(), loadConfig()])
|
await Promise.all([loadEngines(), loadConfig(), checkWkcStatus()])
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,6 +426,33 @@ async function onSave() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- WeKnoraCloud 凭证状态 ----
|
||||||
|
const wkcState = ref<'loading' | 'unconfigured' | 'configured' | 'expired'>('loading')
|
||||||
|
|
||||||
|
async function checkWkcStatus() {
|
||||||
|
wkcState.value = 'loading'
|
||||||
|
try {
|
||||||
|
const status = await getWeKnoraCloudStatus()
|
||||||
|
if (status.needs_reinit) {
|
||||||
|
wkcState.value = 'expired'
|
||||||
|
} else if (status.has_models) {
|
||||||
|
wkcState.value = 'configured'
|
||||||
|
} else {
|
||||||
|
wkcState.value = 'unconfigured'
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
wkcState.value = 'unconfigured'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function goToWkcSettings() {
|
||||||
|
if (uiStore.showSettingsModal) {
|
||||||
|
uiStore.closeSettings()
|
||||||
|
await nextTick()
|
||||||
|
}
|
||||||
|
uiStore.openSettings('weknoracloud')
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(loadAll)
|
onMounted(loadAll)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -596,4 +660,29 @@ onMounted(loadAll)
|
|||||||
.tag-with-tooltip {
|
.tag-with-tooltip {
|
||||||
cursor: help;
|
cursor: help;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- WeKnoraCloud 凭证状态 ----
|
||||||
|
.wkc-status {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 10px 14px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
margin-bottom: 12px;
|
||||||
|
background: var(--td-bg-color-secondarycontainer);
|
||||||
|
|
||||||
|
&--ok {
|
||||||
|
background: var(--td-success-color-light);
|
||||||
|
border: 1px solid var(--td-success-color-focus);
|
||||||
|
color: var(--td-success-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
&--warn {
|
||||||
|
background: #fff7ed;
|
||||||
|
border: 1px solid #fed7aa;
|
||||||
|
border-left: 3px solid #f97316;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -27,11 +27,11 @@
|
|||||||
@click="handleNavClick(item)"
|
@click="handleNavClick(item)"
|
||||||
>
|
>
|
||||||
<!-- 网络搜索使用自定义 SVG 图标 -->
|
<!-- 网络搜索使用自定义 SVG 图标 -->
|
||||||
<svg
|
<svg
|
||||||
v-if="item.key === 'websearch'"
|
v-if="item.key === 'websearch'"
|
||||||
width="18"
|
width="18"
|
||||||
height="18"
|
height="18"
|
||||||
viewBox="0 0 18 18"
|
viewBox="0 0 18 18"
|
||||||
fill="none"
|
fill="none"
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
class="nav-icon"
|
class="nav-icon"
|
||||||
@@ -42,6 +42,19 @@
|
|||||||
<line x1="2.94" y1="5.5" x2="15.06" y2="5.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round"/>
|
<line x1="2.94" y1="5.5" x2="15.06" y2="5.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round"/>
|
||||||
<line x1="2.94" y1="12.5" x2="15.06" y2="12.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round"/>
|
<line x1="2.94" y1="12.5" x2="15.06" y2="12.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
<!-- WeKnora Cloud 使用自定义 W 图标 -->
|
||||||
|
<svg
|
||||||
|
v-else-if="item.key === 'weknoracloud'"
|
||||||
|
width="18"
|
||||||
|
height="18"
|
||||||
|
viewBox="0 0 18 18"
|
||||||
|
fill="none"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
class="nav-icon"
|
||||||
|
>
|
||||||
|
<rect x="1.5" y="1.5" width="15" height="15" rx="3.5" stroke="currentColor" stroke-width="1.2" fill="none"/>
|
||||||
|
<path d="M4.5 5.5L6.5 12.5L9 7.5L11.5 12.5L13.5 5.5" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
|
||||||
|
</svg>
|
||||||
<t-icon v-else :name="item.icon" class="nav-icon" />
|
<t-icon v-else :name="item.icon" class="nav-icon" />
|
||||||
<span class="nav-label">{{ item.label }}</span>
|
<span class="nav-label">{{ item.label }}</span>
|
||||||
<t-icon
|
<t-icon
|
||||||
@@ -79,16 +92,21 @@
|
|||||||
<GeneralSettings />
|
<GeneralSettings />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 模型配置 -->
|
|
||||||
<div v-if="currentSection === 'models'" class="section">
|
|
||||||
<ModelSettings />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Ollama 设置 -->
|
<!-- Ollama 设置 -->
|
||||||
<div v-if="currentSection === 'ollama'" class="section">
|
<div v-if="currentSection === 'ollama'" class="section">
|
||||||
<OllamaSettings />
|
<OllamaSettings />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- WeKnora Cloud -->
|
||||||
|
<div v-if="currentSection === 'weknoracloud'" class="section">
|
||||||
|
<WeKnoraCloudSettings />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 模型配置 -->
|
||||||
|
<div v-if="currentSection === 'models'" class="section">
|
||||||
|
<ModelSettings />
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- 网络搜索配置 -->
|
<!-- 网络搜索配置 -->
|
||||||
<div v-if="currentSection === 'websearch'" class="section">
|
<div v-if="currentSection === 'websearch'" class="section">
|
||||||
<WebSearchSettings />
|
<WebSearchSettings />
|
||||||
@@ -153,6 +171,7 @@ import WebSearchSettings from './WebSearchSettings.vue'
|
|||||||
import ChatHistorySettings from './ChatHistorySettings.vue'
|
import ChatHistorySettings from './ChatHistorySettings.vue'
|
||||||
import ParserEngineSettings from './ParserEngineSettings.vue'
|
import ParserEngineSettings from './ParserEngineSettings.vue'
|
||||||
import StorageEngineSettings from './StorageEngineSettings.vue'
|
import StorageEngineSettings from './StorageEngineSettings.vue'
|
||||||
|
import WeKnoraCloudSettings from './WeKnoraCloudSettings.vue'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
@@ -165,6 +184,8 @@ const expandedMenus = ref<string[]>([])
|
|||||||
|
|
||||||
const navItems = computed(() => [
|
const navItems = computed(() => [
|
||||||
{ key: 'general', icon: 'setting', label: t('general.title') },
|
{ key: 'general', icon: 'setting', label: t('general.title') },
|
||||||
|
{ key: 'ollama', icon: 'server', label: 'Ollama' },
|
||||||
|
{ key: 'weknoracloud', icon: '', label: 'WeKnora Cloud' },
|
||||||
{
|
{
|
||||||
key: 'models',
|
key: 'models',
|
||||||
icon: 'control-platform',
|
icon: 'control-platform',
|
||||||
@@ -176,8 +197,7 @@ const navItems = computed(() => [
|
|||||||
{ key: 'vllm', label: t('model.vlmModel') }
|
{ key: 'vllm', label: t('model.vlmModel') }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{ key: 'ollama', icon: 'server', label: 'Ollama' },
|
{ key: 'websearch', icon: 'search', label: t('settings.webSearchConfig') },
|
||||||
{ key: 'websearch', icon: 'search', label: t('settings.webSearchConfig') },
|
|
||||||
{ key: 'chathistory', icon: 'chat', label: t('chatHistorySettings.title') },
|
{ key: 'chathistory', icon: 'chat', label: t('chatHistorySettings.title') },
|
||||||
{
|
{
|
||||||
key: 'parser',
|
key: 'parser',
|
||||||
@@ -185,6 +205,7 @@ const navItems = computed(() => [
|
|||||||
label: t('settings.parserEngine'),
|
label: t('settings.parserEngine'),
|
||||||
children: [
|
children: [
|
||||||
{ key: 'builtin', label: 'Builtin (DocReader)' },
|
{ key: 'builtin', label: 'Builtin (DocReader)' },
|
||||||
|
{ key: 'weknoracloud', label: 'WeKnora Cloud' },
|
||||||
{ key: 'simple', label: 'Simple' },
|
{ key: 'simple', label: 'Simple' },
|
||||||
{ key: 'markitdown', label: 'Markitdown' },
|
{ key: 'markitdown', label: 'Markitdown' },
|
||||||
{ key: 'mineru', label: 'MinerU' },
|
{ key: 'mineru', label: 'MinerU' },
|
||||||
|
|||||||
@@ -0,0 +1,327 @@
|
|||||||
|
<template>
|
||||||
|
<div class="weknoracloud-settings">
|
||||||
|
<div class="section-header">
|
||||||
|
<h2>WeKnora Cloud</h2>
|
||||||
|
<p class="section-description">
|
||||||
|
配置 WeKnora Cloud 的 APPID 和 APPSECRET 凭证。配置完成后,可在模型管理中添加模型时选择 WeKnoraCloud 作为厂商。
|
||||||
|
</p>
|
||||||
|
<a
|
||||||
|
class="doc-link"
|
||||||
|
href="https://developers.weixin.qq.com/doc/aispeech/knowledge/atomic_capability/atomic_interface.html"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
>
|
||||||
|
<t-icon name="link" style="font-size: 14px;" />
|
||||||
|
查看文档
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 状态区域 -->
|
||||||
|
<!-- 未配置 -->
|
||||||
|
<div v-if="credentialState === 'unconfigured'" class="credential-status unconfigured">
|
||||||
|
<t-icon name="info-circle" style="font-size: 16px; flex-shrink: 0;" />
|
||||||
|
<span>尚未配置凭证,请填写 APPID 和 APPSECRET</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 凭证失效 -->
|
||||||
|
<div v-else-if="credentialState === 'expired'" class="credential-warning">
|
||||||
|
<t-icon name="error-circle" style="font-size: 16px; color: #f97316; flex-shrink: 0; margin-top: 1px;" />
|
||||||
|
<div class="warning-text">
|
||||||
|
<strong>WeKnora Cloud凭证已失效</strong><br />
|
||||||
|
{{ reinitReason || '服务重启后加密密钥已变更,已保存的凭证无法解密。请重新填写凭证。' }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 已配置正常 -->
|
||||||
|
<div v-else-if="credentialState === 'configured'" class="credential-status success">
|
||||||
|
<t-icon name="check-circle" style="font-size: 16px; color: var(--td-success-color); flex-shrink: 0;" />
|
||||||
|
<span class="status-text">凭证已配置,状态正常</span>
|
||||||
|
<t-button
|
||||||
|
v-if="!formExpanded"
|
||||||
|
variant="text"
|
||||||
|
size="small"
|
||||||
|
theme="primary"
|
||||||
|
@click="formExpanded = true"
|
||||||
|
>
|
||||||
|
重新配置
|
||||||
|
</t-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 配置表单(折叠控制) -->
|
||||||
|
<div v-if="formExpanded" class="settings-group">
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label class="setting-label">APPID</label>
|
||||||
|
<p class="setting-desc">WeKnora Cloud的应用 ID</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-input
|
||||||
|
v-model="form.appId"
|
||||||
|
placeholder="请输入 APPID"
|
||||||
|
autocomplete="off"
|
||||||
|
style="width: 280px;"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label class="setting-label">APPSECRET</label>
|
||||||
|
<p class="setting-desc">WeKnora Cloud的应用密钥</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-input
|
||||||
|
v-model="form.appSecret"
|
||||||
|
type="password"
|
||||||
|
placeholder="请输入 APPSECRET"
|
||||||
|
autocomplete="new-password"
|
||||||
|
style="width: 280px;"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="setting-row action-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<p class="setting-desc">保存后将验证服务可达性并加密存储凭证</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-button
|
||||||
|
theme="primary"
|
||||||
|
:loading="saving"
|
||||||
|
:disabled="!form.appId || !form.appSecret"
|
||||||
|
@click="handleSave"
|
||||||
|
>
|
||||||
|
保存凭证
|
||||||
|
</t-button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 使用说明 -->
|
||||||
|
<div class="usage-hint">
|
||||||
|
<p class="hint-title">使用说明</p>
|
||||||
|
<p class="hint-text">
|
||||||
|
1. 在此页面填写并保存 APPID 和 APPSECRET<br />
|
||||||
|
2. 前往「模型管理」添加模型,选择 Remote API 来源<br />
|
||||||
|
3. 在厂商下拉中选择「WeKnoraCloud」<br />
|
||||||
|
4. 填写模型名称后保存即可(Base URL 和认证信息会自动配置)
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, computed, onMounted } from 'vue'
|
||||||
|
import { MessagePlugin } from 'tdesign-vue-next'
|
||||||
|
import { saveWeKnoraCloudCredentials, getWeKnoraCloudStatus } from '@/api/model'
|
||||||
|
|
||||||
|
const form = ref({ appId: '', appSecret: '' })
|
||||||
|
const saving = ref(false)
|
||||||
|
const needsReinit = ref(false)
|
||||||
|
const reinitReason = ref('')
|
||||||
|
const hasCredentials = ref(false)
|
||||||
|
const formExpanded = ref(true)
|
||||||
|
|
||||||
|
// 三态:unconfigured / configured / expired
|
||||||
|
const credentialState = computed(() => {
|
||||||
|
if (needsReinit.value) return 'expired'
|
||||||
|
if (hasCredentials.value) return 'configured'
|
||||||
|
return 'unconfigured'
|
||||||
|
})
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
if (!form.value.appId || !form.value.appSecret) {
|
||||||
|
MessagePlugin.warning('请填写 APPID 和 APPSECRET')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
saving.value = true
|
||||||
|
try {
|
||||||
|
await saveWeKnoraCloudCredentials({
|
||||||
|
app_id: form.value.appId,
|
||||||
|
app_secret: form.value.appSecret,
|
||||||
|
})
|
||||||
|
MessagePlugin.success('凭证保存成功')
|
||||||
|
form.value.appId = ''
|
||||||
|
form.value.appSecret = ''
|
||||||
|
needsReinit.value = false
|
||||||
|
reinitReason.value = ''
|
||||||
|
hasCredentials.value = true
|
||||||
|
formExpanded.value = false
|
||||||
|
} catch (err: any) {
|
||||||
|
MessagePlugin.error(err?.message || '凭证保存失败')
|
||||||
|
} finally {
|
||||||
|
saving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkStatus = async () => {
|
||||||
|
try {
|
||||||
|
const status = await getWeKnoraCloudStatus()
|
||||||
|
needsReinit.value = status.needs_reinit
|
||||||
|
reinitReason.value = status.reason || ''
|
||||||
|
hasCredentials.value = status.has_models && !status.needs_reinit
|
||||||
|
// 已配置且未失效时默认收起表单
|
||||||
|
if (hasCredentials.value) {
|
||||||
|
formExpanded.value = false
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 静默失败
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
checkStatus()
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="less" scoped>
|
||||||
|
.weknoracloud-settings {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-header {
|
||||||
|
margin-bottom: 24px;
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--td-text-color-primary);
|
||||||
|
margin: 0 0 8px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-description {
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
margin: 0 0 10px 0;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.doc-link {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--td-brand-color);
|
||||||
|
text-decoration: none;
|
||||||
|
transition: opacity 0.2s;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
opacity: 0.8;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.credential-warning {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
background: #fff7ed;
|
||||||
|
border: 1px solid #fed7aa;
|
||||||
|
border-left: 3px solid #f97316;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 12px 16px;
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 10px;
|
||||||
|
|
||||||
|
.warning-text {
|
||||||
|
font-size: 13px;
|
||||||
|
color: #9a3412;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.credential-status {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
padding: 10px 14px;
|
||||||
|
border-radius: 6px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
font-size: 13px;
|
||||||
|
|
||||||
|
&.unconfigured {
|
||||||
|
background: var(--td-bg-color-secondarycontainer);
|
||||||
|
border: 1px solid var(--td-component-stroke);
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&.success {
|
||||||
|
background: var(--td-success-color-light);
|
||||||
|
border: 1px solid var(--td-success-color-focus);
|
||||||
|
color: var(--td-success-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-text {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings-group {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 16px;
|
||||||
|
padding: 16px 0;
|
||||||
|
border-bottom: 1px solid var(--td-component-stroke);
|
||||||
|
|
||||||
|
&:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.action-row {
|
||||||
|
padding-top: 20px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-info {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
|
||||||
|
.setting-label {
|
||||||
|
display: block;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--td-text-color-primary);
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-desc {
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
margin: 0;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-control {
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.usage-hint {
|
||||||
|
padding: 14px 16px;
|
||||||
|
background: var(--td-bg-color-secondarycontainer);
|
||||||
|
border: 1px solid var(--td-component-stroke);
|
||||||
|
border-radius: 8px;
|
||||||
|
|
||||||
|
.hint-title {
|
||||||
|
margin: 0 0 8px 0;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--td-text-color-placeholder);
|
||||||
|
}
|
||||||
|
|
||||||
|
.hint-text {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--td-text-color-secondary);
|
||||||
|
line-height: 1.8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -7999,6 +7999,8 @@ func (s *knowledgeService) convert(
|
|||||||
|
|
||||||
var reader interfaces.DocReader = s.resolveDocReader(ctx, parserEngine, fileType, isURL, overrides)
|
var reader interfaces.DocReader = s.resolveDocReader(ctx, parserEngine, fileType, isURL, overrides)
|
||||||
if reader == nil {
|
if reader == nil {
|
||||||
|
logger.Errorf(ctx, "[convert] no doc reader for kb=%s knowledge=%s fileType=%s engine=%q isURL=%v",
|
||||||
|
kb.ID, knowledge.ID, fileType, parserEngine, isURL)
|
||||||
knowledge.ParseStatus = "failed"
|
knowledge.ParseStatus = "failed"
|
||||||
knowledge.ErrorMessage = "Document parsing service is not configured. Please use text/paragraph import or set DOCREADER_ADDR."
|
knowledge.ErrorMessage = "Document parsing service is not configured. Please use text/paragraph import or set DOCREADER_ADDR."
|
||||||
knowledge.UpdatedAt = time.Now()
|
knowledge.UpdatedAt = time.Now()
|
||||||
@@ -8034,6 +8036,8 @@ func (s *knowledgeService) convert(
|
|||||||
return s.failKnowledge(ctx, knowledge, isLastRetry, "document read failed: %v", err)
|
return s.failKnowledge(ctx, knowledge, isLastRetry, "document read failed: %v", err)
|
||||||
}
|
}
|
||||||
if result.Error != "" {
|
if result.Error != "" {
|
||||||
|
logger.Errorf(ctx, "[convert] parser returned error kb=%s knowledge=%s file=%q type=%s engine=%q: %s",
|
||||||
|
kb.ID, knowledge.ID, req.FileName, fileType, parserEngine, result.Error)
|
||||||
knowledge.ParseStatus = "failed"
|
knowledge.ParseStatus = "failed"
|
||||||
knowledge.ErrorMessage = result.Error
|
knowledge.ErrorMessage = result.Error
|
||||||
knowledge.UpdatedAt = time.Now()
|
knowledge.UpdatedAt = time.Now()
|
||||||
@@ -8050,16 +8054,14 @@ func (s *knowledgeService) resolveDocReader(ctx context.Context, engine, fileTyp
|
|||||||
case docparser.SimpleEngineName:
|
case docparser.SimpleEngineName:
|
||||||
return &docparser.SimpleFormatReader{}
|
return &docparser.SimpleFormatReader{}
|
||||||
case docparser.WeKnoraCloudEngineName:
|
case docparser.WeKnoraCloudEngineName:
|
||||||
addr := strings.TrimSpace(overrides["docreader_addr"])
|
|
||||||
if !IsWeKnoraCloudDocReaderAddr(addr) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
creds := s.tenantService.GetDocreaderCredentials(ctx)
|
creds := s.tenantService.GetDocreaderCredentials(ctx)
|
||||||
if creds == nil {
|
if creds == nil {
|
||||||
|
logger.Warnf(ctx, "[resolveDocReader] WeKnoraCloud: no tenant docreader credentials (fileType=%s)", fileType)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(addr, creds.AppID, creds.APIKey)
|
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(creds.AppID, creds.APIKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(ctx, "[resolveDocReader] WeKnoraCloud reader init failed: %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return reader
|
return reader
|
||||||
|
|||||||
@@ -7,17 +7,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/models/provider"
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
|
modelsutils "github.com/Tencent/WeKnora/internal/models/utils"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
"github.com/Tencent/WeKnora/internal/utils"
|
"github.com/Tencent/WeKnora/internal/utils"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const WeKnoraCloudAPI = "https://weknora.woa.com/platform/openapi"
|
|
||||||
|
|
||||||
type weKnoraCloudService struct {
|
type weKnoraCloudService struct {
|
||||||
repo interfaces.ModelRepository
|
|
||||||
tenantRepo interfaces.TenantRepository
|
tenantRepo interfaces.TenantRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +25,6 @@ func NewWeKnoraCloudService(
|
|||||||
tenantRepo interfaces.TenantRepository,
|
tenantRepo interfaces.TenantRepository,
|
||||||
) interfaces.WeKnoraCloudService {
|
) interfaces.WeKnoraCloudService {
|
||||||
return &weKnoraCloudService{
|
return &weKnoraCloudService{
|
||||||
repo: repo,
|
|
||||||
tenantRepo: tenantRepo,
|
tenantRepo: tenantRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,68 +33,19 @@ func IsWeKnoraCloudDocReaderAddr(addr string) bool {
|
|||||||
return strings.TrimSuffix(strings.TrimSpace(addr), "/") == strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")+"/api/v1/doc/reader"
|
return strings.TrimSuffix(strings.TrimSpace(addr), "/") == strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")+"/api/v1/doc/reader"
|
||||||
}
|
}
|
||||||
|
|
||||||
// weKnoraCloudModelDefs 三个内置模型的静态定义
|
// SaveCredentials 仅保存 APPID/APPSECRET 凭证,不自动创建模型
|
||||||
var weKnoraCloudModelDefs = []struct {
|
func (s *weKnoraCloudService) SaveCredentials(ctx context.Context, appID, appSecret string) error {
|
||||||
name string
|
|
||||||
modelType types.ModelType
|
|
||||||
remoteModelName string
|
|
||||||
}{
|
|
||||||
{name: "weknoracloud-chat", modelType: types.ModelTypeKnowledgeQA, remoteModelName: "chat"},
|
|
||||||
{name: "weknoracloud-embedding", modelType: types.ModelTypeEmbedding, remoteModelName: "embedding"},
|
|
||||||
{name: "weknoracloud-rerank", modelType: types.ModelTypeRerank, remoteModelName: "rerank"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *weKnoraCloudService) Initialize(ctx context.Context, appID, appSecret string) (*types.InitializeResult, error) {
|
|
||||||
if err := s.validateInitializeInput(appID, appSecret); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
prepared, err := s.prepareInitialize(ctx, appID, appSecret)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result := &types.InitializeResult{}
|
|
||||||
|
|
||||||
actions, err := s.installBuiltinModels(ctx, prepared.tenantID, appID, prepared.encryptedSecret)
|
|
||||||
if err != nil {
|
|
||||||
return nil, s.failInitializeAndRollback(ctx, prepared.tenantID, prepared.snapshot, err)
|
|
||||||
}
|
|
||||||
result.Models = actions
|
|
||||||
|
|
||||||
if err := s.updateTenantDocReaderAddr(ctx, prepared.tenantID, appID, prepared.encryptedSecret); err != nil {
|
|
||||||
return nil, s.failInitializeAndRollback(ctx, prepared.tenantID, prepared.snapshot, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// validateInitializeInput validates that appID and appSecret are not empty.
|
|
||||||
func (s *weKnoraCloudService) validateInitializeInput(appID, appSecret string) error {
|
|
||||||
if appID == "" {
|
if appID == "" {
|
||||||
return fmt.Errorf("app_id is required")
|
return fmt.Errorf("app_id is required")
|
||||||
}
|
}
|
||||||
if appSecret == "" {
|
if appSecret == "" {
|
||||||
return fmt.Errorf("app_secret is required")
|
return fmt.Errorf("app_secret is required")
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// weKnoraCloudInitPrepared holds the prepared state for initialize operation.
|
if err := s.verifyCredentials(ctx, appID, appSecret); err != nil {
|
||||||
type weKnoraCloudInitPrepared struct {
|
return fmt.Errorf("credential verification failed: %w", err)
|
||||||
tenantID uint64
|
|
||||||
encryptedSecret string
|
|
||||||
snapshot *weKnoraCloudInitSnapshot
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepareInitialize performs pre-initialization checks: ping WeKnoraCloud, encrypt secret, and snapshot current state.
|
|
||||||
func (s *weKnoraCloudService) prepareInitialize(ctx context.Context, appID, appSecret string) (*weKnoraCloudInitPrepared, error) {
|
|
||||||
// Step 1: Check WeKnoraCloud service reachability
|
|
||||||
if err := s.pingWeKnoraCloud(ctx, appID, appSecret); err != nil {
|
|
||||||
return nil, fmt.Errorf("WeKnoraCloud service reachability check failed: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Encrypt appSecret
|
|
||||||
encryptedSecret := appSecret
|
encryptedSecret := appSecret
|
||||||
if key := utils.GetAESKey(); key != nil {
|
if key := utils.GetAESKey(); key != nil {
|
||||||
if encrypted, err := utils.EncryptAESGCM(appSecret, key); err == nil {
|
if encrypted, err := utils.EncryptAESGCM(appSecret, key); err == nil {
|
||||||
@@ -105,180 +53,45 @@ func (s *weKnoraCloudService) prepareInitialize(ctx context.Context, appID, appS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Get tenantID and take snapshot
|
|
||||||
tenantID := types.MustTenantIDFromContext(ctx)
|
tenantID := types.MustTenantIDFromContext(ctx)
|
||||||
snapshot, err := s.snapshotInitializeState(ctx, tenantID)
|
return s.updateTenantCredentials(ctx, tenantID, appID, encryptedSecret)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("snapshot initialize state failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &weKnoraCloudInitPrepared{
|
|
||||||
tenantID: tenantID,
|
|
||||||
encryptedSecret: encryptedSecret,
|
|
||||||
snapshot: snapshot,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// installBuiltinModels installs or updates the three builtin WeKnoraCloud models (chat, embedding, rerank).
|
// verifyCredentials 向 WeKnoraCloud /api/v1/health 发送带签名头的 GET。
|
||||||
// Returns a slice of ModelAction describing what was done.
|
//
|
||||||
func (s *weKnoraCloudService) installBuiltinModels(
|
// 注意:health 一般为探活接口,远端常不校验 APPID/SECRET 或签名;HTTP 200 通常只表示
|
||||||
ctx context.Context,
|
// 「网关/服务可达」,不能严格证明凭证有效。若需强校验,应改为调用必须鉴权的业务接口。
|
||||||
tenantID uint64,
|
func (s *weKnoraCloudService) verifyCredentials(ctx context.Context, appID, appSecret string) error {
|
||||||
appID, encryptedSecret string,
|
|
||||||
) ([]types.ModelAction, error) {
|
|
||||||
result := make([]types.ModelAction, 0, len(weKnoraCloudModelDefs))
|
|
||||||
|
|
||||||
for _, def := range weKnoraCloudModelDefs {
|
|
||||||
action, err := s.upsertModel(ctx, tenantID, def.name, def.modelType, def.remoteModelName, appID, encryptedSecret)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("upsert model %s failed: %w", def.name, err)
|
|
||||||
}
|
|
||||||
result = append(result, types.ModelAction{
|
|
||||||
Name: def.name,
|
|
||||||
Type: string(def.modelType),
|
|
||||||
Action: action,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// weKnoraCloudInitSnapshot holds a snapshot of the state before initialization.
|
|
||||||
type weKnoraCloudInitSnapshot struct {
|
|
||||||
modelsByType map[types.ModelType][]*types.Model
|
|
||||||
tenant *types.Tenant
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *weKnoraCloudService) snapshotInitializeState(ctx context.Context, tenantID uint64) (*weKnoraCloudInitSnapshot, error) {
|
|
||||||
snapshot := &weKnoraCloudInitSnapshot{modelsByType: make(map[types.ModelType][]*types.Model, len(weKnoraCloudModelDefs))}
|
|
||||||
for _, def := range weKnoraCloudModelDefs {
|
|
||||||
models, err := s.repo.List(ctx, tenantID, def.modelType, types.ModelSourceRemote)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
copies := make([]*types.Model, 0, len(models))
|
|
||||||
for _, model := range models {
|
|
||||||
copies = append(copies, cloneWeKnoraCloudModel(model))
|
|
||||||
}
|
|
||||||
snapshot.modelsByType[def.modelType] = copies
|
|
||||||
}
|
|
||||||
if s.tenantRepo != nil {
|
|
||||||
tenant, err := s.tenantRepo.GetTenantByID(ctx, tenantID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
snapshot.tenant = cloneWeKnoraCloudTenant(tenant)
|
|
||||||
}
|
|
||||||
return snapshot, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *weKnoraCloudService) failInitializeAndRollback(ctx context.Context, tenantID uint64, snapshot *weKnoraCloudInitSnapshot, err error) error {
|
|
||||||
if rollbackErr := s.rollbackInitializeState(ctx, tenantID, snapshot); rollbackErr != nil {
|
|
||||||
return fmt.Errorf("%w;回滚失败:%v", err, rollbackErr)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *weKnoraCloudService) rollbackInitializeState(ctx context.Context, tenantID uint64, snapshot *weKnoraCloudInitSnapshot) error {
|
|
||||||
if snapshot == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var rollbackErrs []string
|
|
||||||
for _, def := range weKnoraCloudModelDefs {
|
|
||||||
currentModels, err := s.repo.List(ctx, tenantID, def.modelType, types.ModelSourceRemote)
|
|
||||||
if err != nil {
|
|
||||||
rollbackErrs = append(rollbackErrs, err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
snapshotByID := map[string]*types.Model{}
|
|
||||||
for _, model := range snapshot.modelsByType[def.modelType] {
|
|
||||||
snapshotByID[model.ID] = cloneWeKnoraCloudModel(model)
|
|
||||||
}
|
|
||||||
for _, current := range currentModels {
|
|
||||||
if current == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := snapshotByID[current.ID]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if current.Parameters.Provider == string(provider.ProviderWeKnoraCloud) && isWeKnoraCloudBuiltinModelName(current.Name) {
|
|
||||||
if err := s.repo.Delete(ctx, tenantID, current.ID); err != nil {
|
|
||||||
rollbackErrs = append(rollbackErrs, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, model := range snapshot.modelsByType[def.modelType] {
|
|
||||||
if err := s.repo.Update(ctx, cloneWeKnoraCloudModel(model)); err != nil {
|
|
||||||
rollbackErrs = append(rollbackErrs, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if snapshot.tenant != nil && s.tenantRepo != nil {
|
|
||||||
if err := s.tenantRepo.UpdateTenant(ctx, cloneWeKnoraCloudTenant(snapshot.tenant)); err != nil {
|
|
||||||
rollbackErrs = append(rollbackErrs, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(rollbackErrs) > 0 {
|
|
||||||
return fmt.Errorf("%s", strings.Join(rollbackErrs, "; "))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isWeKnoraCloudBuiltinModelName(name string) bool {
|
|
||||||
for _, def := range weKnoraCloudModelDefs {
|
|
||||||
if def.name == name {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func cloneWeKnoraCloudModel(model *types.Model) *types.Model {
|
|
||||||
if model == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
clone := *model
|
|
||||||
if model.Parameters.ExtraConfig != nil {
|
|
||||||
clone.Parameters.ExtraConfig = make(map[string]string, len(model.Parameters.ExtraConfig))
|
|
||||||
for k, v := range model.Parameters.ExtraConfig {
|
|
||||||
clone.Parameters.ExtraConfig[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &clone
|
|
||||||
}
|
|
||||||
|
|
||||||
func cloneWeKnoraCloudTenant(tenant *types.Tenant) *types.Tenant {
|
|
||||||
if tenant == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
clone := *tenant
|
|
||||||
if tenant.ParserEngineConfig != nil {
|
|
||||||
cfg := *tenant.ParserEngineConfig
|
|
||||||
clone.ParserEngineConfig = &cfg
|
|
||||||
}
|
|
||||||
return &clone
|
|
||||||
}
|
|
||||||
|
|
||||||
// pingWeKnoraCloud 调用 WeKnoraCloud 服务的 GET /health 接口验证可达性。
|
|
||||||
// 注意:这里仅检查服务可达,不校验 APPID / API Key 是否有效。
|
|
||||||
func (s *weKnoraCloudService) pingWeKnoraCloud(ctx context.Context, appID, appSecret string) error {
|
|
||||||
baseURL := strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")
|
baseURL := strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")
|
||||||
healthURL := baseURL + "/api/v1/health"
|
healthURL := baseURL + "/api/v1/health"
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("创建健康检查请求失败: %w", err)
|
return fmt.Errorf("create verification request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requestID := fmt.Sprintf("verify-%d", time.Now().UnixNano())
|
||||||
|
signHeaders := modelsutils.Sign(appID, appSecret, requestID, "{}")
|
||||||
|
for k, v := range signHeaders {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "credential verification request: method=GET url=%s app_id=%s request_id=%s ",
|
||||||
|
healthURL, appID, requestID)
|
||||||
|
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("WeKnoraCloud 服务不可达 (%s): %w", healthURL, err)
|
logger.Warnf(ctx, "credential verification HTTP failed: url=%s err=%v", healthURL, err)
|
||||||
|
return fmt.Errorf("service unreachable: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
|
||||||
|
return fmt.Errorf("invalid APPID or APPSECRET (HTTP %d)", resp.StatusCode)
|
||||||
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("WeKnoraCloud 健康检查返回非 200 状态码: %d", resp.StatusCode)
|
return fmt.Errorf("invalid response status code: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -295,9 +108,8 @@ func (s *weKnoraCloudService) CheckStatus(ctx context.Context) (*types.WeKnoraCl
|
|||||||
// Check if tenant has WeKnoraCloud credentials in parser config
|
// Check if tenant has WeKnoraCloud credentials in parser config
|
||||||
if tenant.ParserEngineConfig == nil || tenant.ParserEngineConfig.DocreaderAppID == "" || tenant.ParserEngineConfig.DocreaderAPIKey == "" {
|
if tenant.ParserEngineConfig == nil || tenant.ParserEngineConfig.DocreaderAppID == "" || tenant.ParserEngineConfig.DocreaderAPIKey == "" {
|
||||||
return &types.WeKnoraCloudStatusResult{
|
return &types.WeKnoraCloudStatusResult{
|
||||||
HasModels: true,
|
HasModels: false,
|
||||||
NeedsReinit: true,
|
NeedsReinit: false,
|
||||||
Reason: fmt.Sprintf("WeKnoraCloud 凭证为空,请重新填写 APPID 和 API Key, 请前往:%s", WeKnoraCloudAPI),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,7 +119,7 @@ func (s *weKnoraCloudService) CheckStatus(ctx context.Context) (*types.WeKnoraCl
|
|||||||
return &types.WeKnoraCloudStatusResult{
|
return &types.WeKnoraCloudStatusResult{
|
||||||
HasModels: true,
|
HasModels: true,
|
||||||
NeedsReinit: true,
|
NeedsReinit: true,
|
||||||
Reason: "WeKnoraCloud 凭证解密失败(服务重启后加密密钥已变更),请重新填写 APPID 和 API Key",
|
Reason: "WeKnoraCloud 凭证解密失败(服务重启后加密密钥已变更),请重新填写 APPID 和 APPSECRET",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -315,8 +127,8 @@ func (s *weKnoraCloudService) CheckStatus(ctx context.Context) (*types.WeKnoraCl
|
|||||||
return &types.WeKnoraCloudStatusResult{HasModels: true, NeedsReinit: false}, nil
|
return &types.WeKnoraCloudStatusResult{HasModels: true, NeedsReinit: false}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateTenantDocReaderAddr updates tenant parser config with WeKnoraCloud credentials and DocReaderAddr
|
// updateTenantCredentials 更新租户的 WeKnoraCloud 凭证和 DocReader 地址
|
||||||
func (s *weKnoraCloudService) updateTenantDocReaderAddr(ctx context.Context, tenantID uint64, appID, encryptedSecret string) error {
|
func (s *weKnoraCloudService) updateTenantCredentials(ctx context.Context, tenantID uint64, appID, encryptedSecret string) error {
|
||||||
if s.tenantRepo == nil {
|
if s.tenantRepo == nil {
|
||||||
return fmt.Errorf("tenant repository is required")
|
return fmt.Errorf("tenant repository is required")
|
||||||
}
|
}
|
||||||
@@ -328,81 +140,7 @@ func (s *weKnoraCloudService) updateTenantDocReaderAddr(ctx context.Context, ten
|
|||||||
if tenant.ParserEngineConfig == nil {
|
if tenant.ParserEngineConfig == nil {
|
||||||
tenant.ParserEngineConfig = &types.ParserEngineConfig{}
|
tenant.ParserEngineConfig = &types.ParserEngineConfig{}
|
||||||
}
|
}
|
||||||
tenant.ParserEngineConfig.DocReaderAddr = strings.TrimRight(provider.WeKnoraCloudBaseURL, "/") + "/api/v1/doc/reader"
|
|
||||||
tenant.ParserEngineConfig.DocreaderAppID = appID
|
tenant.ParserEngineConfig.DocreaderAppID = appID
|
||||||
tenant.ParserEngineConfig.DocreaderAPIKey = encryptedSecret
|
tenant.ParserEngineConfig.DocreaderAPIKey = encryptedSecret
|
||||||
return s.tenantRepo.UpdateTenant(ctx, tenant)
|
return s.tenantRepo.UpdateTenant(ctx, tenant)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *weKnoraCloudService) upsertModel(
|
|
||||||
ctx context.Context,
|
|
||||||
tenantID uint64,
|
|
||||||
name string,
|
|
||||||
modelType types.ModelType,
|
|
||||||
remoteModelName string,
|
|
||||||
appID, encryptedSecret string,
|
|
||||||
) (string, error) {
|
|
||||||
// 列出该类型的远程模型,过滤出 weknoracloud provider
|
|
||||||
models, err := s.repo.List(ctx, tenantID, modelType, types.ModelSourceRemote)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
var existing *types.Model
|
|
||||||
for _, m := range models {
|
|
||||||
if m.Name == name && m.Parameters.Provider == string(provider.ProviderWeKnoraCloud) {
|
|
||||||
existing = m
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先清除同类型其他模型的默认标记
|
|
||||||
excludeID := ""
|
|
||||||
if existing != nil {
|
|
||||||
excludeID = existing.ID
|
|
||||||
}
|
|
||||||
if err := s.repo.ClearDefaultByType(ctx, uint(tenantID), modelType, excludeID); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if existing != nil {
|
|
||||||
// 更新凭证和默认标记
|
|
||||||
existing.Parameters.BaseURL = provider.WeKnoraCloudBaseURL
|
|
||||||
existing.Parameters.AppID = appID
|
|
||||||
existing.Parameters.AppSecret = encryptedSecret
|
|
||||||
if existing.Parameters.ExtraConfig == nil {
|
|
||||||
existing.Parameters.ExtraConfig = map[string]string{}
|
|
||||||
}
|
|
||||||
existing.Parameters.ExtraConfig["remote_model_name"] = remoteModelName
|
|
||||||
existing.IsDefault = true
|
|
||||||
if err := s.repo.Update(ctx, existing); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return "updated", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建新模型
|
|
||||||
newModel := &types.Model{
|
|
||||||
ID: uuid.New().String(),
|
|
||||||
TenantID: tenantID,
|
|
||||||
Name: name,
|
|
||||||
Type: modelType,
|
|
||||||
Source: types.ModelSourceRemote,
|
|
||||||
IsBuiltin: true,
|
|
||||||
IsDefault: true,
|
|
||||||
Status: types.ModelStatusActive,
|
|
||||||
Parameters: types.ModelParameters{
|
|
||||||
Provider: string(provider.ProviderWeKnoraCloud),
|
|
||||||
BaseURL: provider.WeKnoraCloudBaseURL,
|
|
||||||
AppID: appID,
|
|
||||||
AppSecret: encryptedSecret,
|
|
||||||
ExtraConfig: map[string]string{
|
|
||||||
"remote_model_name": remoteModelName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if err := s.repo.Create(ctx, newModel); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return "created", nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1508,15 +1508,28 @@ func (h *InitializationHandler) CheckRemoteModel(c *gin.Context) {
|
|||||||
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
|
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
tenantInfo, ok := types.TenantInfoFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
logger.Error(ctx, "Tenant info not found")
|
||||||
|
c.Error(errors.NewBadRequestError("租户信息未找到"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var appID, appSecret string
|
||||||
|
if tenantInfo.ParserEngineConfig != nil {
|
||||||
|
appID = tenantInfo.ParserEngineConfig.DocreaderAppID
|
||||||
|
appSecret = tenantInfo.ParserEngineConfig.DocreaderAPIKey
|
||||||
|
}
|
||||||
|
|
||||||
// 创建模型配置进行测试
|
// 创建模型配置进行测试
|
||||||
modelConfig := &types.Model{
|
modelConfig := &types.Model{
|
||||||
Name: req.ModelName,
|
Name: req.ModelName,
|
||||||
Source: "remote",
|
Source: "remote",
|
||||||
Parameters: types.ModelParameters{
|
Parameters: types.ModelParameters{
|
||||||
BaseURL: req.BaseURL,
|
BaseURL: req.BaseURL,
|
||||||
APIKey: req.APIKey,
|
APIKey: req.APIKey,
|
||||||
Provider: req.Provider,
|
Provider: req.Provider,
|
||||||
|
AppID: appID,
|
||||||
|
AppSecret: appSecret,
|
||||||
},
|
},
|
||||||
Type: "llm", // 默认类型,实际检查时不区分具体类型
|
Type: "llm", // 默认类型,实际检查时不区分具体类型
|
||||||
}
|
}
|
||||||
@@ -1592,6 +1605,17 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tenantInfo, ok := types.TenantInfoFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
logger.Error(ctx, "Tenant info not found")
|
||||||
|
c.Error(errors.NewBadRequestError("租户信息未找到"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var appID, appSecret string
|
||||||
|
if tenantInfo.ParserEngineConfig != nil {
|
||||||
|
appID = tenantInfo.ParserEngineConfig.DocreaderAppID
|
||||||
|
appSecret = tenantInfo.ParserEngineConfig.DocreaderAPIKey
|
||||||
|
}
|
||||||
|
|
||||||
// 构造 embedder 配置
|
// 构造 embedder 配置
|
||||||
cfg := embedding.Config{
|
cfg := embedding.Config{
|
||||||
@@ -1603,6 +1627,8 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
|||||||
Dimensions: req.Dimension,
|
Dimensions: req.Dimension,
|
||||||
ModelID: "",
|
ModelID: "",
|
||||||
Provider: req.Provider,
|
Provider: req.Provider,
|
||||||
|
AppID: appID,
|
||||||
|
AppSecret: appSecret,
|
||||||
}
|
}
|
||||||
|
|
||||||
emb, err := embedding.NewEmbedder(cfg, h.pooler, h.ollamaService)
|
emb, err := embedding.NewEmbedder(cfg, h.pooler, h.ollamaService)
|
||||||
@@ -1647,6 +1673,8 @@ func (h *InitializationHandler) checkRemoteModelConnection(ctx context.Context,
|
|||||||
APIKey: model.Parameters.APIKey,
|
APIKey: model.Parameters.APIKey,
|
||||||
ModelID: model.Name,
|
ModelID: model.Name,
|
||||||
Provider: model.Parameters.Provider,
|
Provider: model.Parameters.Provider,
|
||||||
|
AppID: model.Parameters.AppID,
|
||||||
|
AppSecret: model.Parameters.AppSecret,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建聊天实例
|
// 创建聊天实例
|
||||||
@@ -1706,6 +1734,18 @@ func (h *InitializationHandler) checkRerankModelConnection(ctx context.Context,
|
|||||||
ModelName: modelName,
|
ModelName: modelName,
|
||||||
Source: types.ModelSourceRemote, // 默认值,实际会根据URL判断
|
Source: types.ModelSourceRemote, // 默认值,实际会根据URL判断
|
||||||
}
|
}
|
||||||
|
tenantInfo, ok := types.TenantInfoFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
logger.Error(ctx, "Tenant info not found")
|
||||||
|
return false, "租户信息未找到"
|
||||||
|
}
|
||||||
|
var appID, appSecret string
|
||||||
|
if tenantInfo.ParserEngineConfig != nil {
|
||||||
|
appID = tenantInfo.ParserEngineConfig.DocreaderAppID
|
||||||
|
appSecret = tenantInfo.ParserEngineConfig.DocreaderAPIKey
|
||||||
|
}
|
||||||
|
config.AppID = appID
|
||||||
|
config.AppSecret = appSecret
|
||||||
|
|
||||||
// 创建Reranker实例
|
// 创建Reranker实例
|
||||||
reranker, err := rerank.NewReranker(config)
|
reranker, err := rerank.NewReranker(config)
|
||||||
|
|||||||
@@ -1038,7 +1038,7 @@ func (h *SystemHandler) ResolveDocumentReader(ctx context.Context, addr string)
|
|||||||
if creds == nil {
|
if creds == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(addr, creds.AppID, creds.APIKey)
|
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(creds.AppID, creds.APIKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WeKnoraCloudHandler 处理 WeKnoraCloud 初始化请求
|
// WeKnoraCloudHandler 处理 WeKnoraCloud 凭证管理
|
||||||
type WeKnoraCloudHandler struct {
|
type WeKnoraCloudHandler struct {
|
||||||
svc interfaces.WeKnoraCloudService
|
svc interfaces.WeKnoraCloudService
|
||||||
}
|
}
|
||||||
@@ -17,26 +17,26 @@ func NewWeKnoraCloudHandler(svc interfaces.WeKnoraCloudService) *WeKnoraCloudHan
|
|||||||
return &WeKnoraCloudHandler{svc: svc}
|
return &WeKnoraCloudHandler{svc: svc}
|
||||||
}
|
}
|
||||||
|
|
||||||
type initializeWeKnoraCloudRequest struct {
|
type weKnoraCloudCredentialsRequest struct {
|
||||||
AppID string `json:"app_id" binding:"required"`
|
AppID string `json:"app_id" binding:"required"`
|
||||||
AppSecret string `json:"app_secret" binding:"required"`
|
AppSecret string `json:"app_secret" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize POST /api/v1/models/weknoracloud/initialize
|
// SaveCredentials POST /api/v1/weknoracloud/credentials
|
||||||
func (h *WeKnoraCloudHandler) Initialize(c *gin.Context) {
|
// 仅保存 APPID/APPSECRET 凭证到租户配置,不自动创建模型
|
||||||
var req initializeWeKnoraCloudRequest
|
func (h *WeKnoraCloudHandler) SaveCredentials(c *gin.Context) {
|
||||||
|
var req weKnoraCloudCredentialsRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := h.svc.Initialize(c.Request.Context(), req.AppID, req.AppSecret)
|
if err := h.svc.SaveCredentials(c.Request.Context(), req.AppID, req.AppSecret); err != nil {
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, result)
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": "凭证保存成功"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status GET /api/v1/models/weknoracloud/status
|
// Status GET /api/v1/models/weknoracloud/status
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package docparser
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/provider"
|
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -87,13 +86,13 @@ type weKnoraCloudEngine struct{}
|
|||||||
func (e *weKnoraCloudEngine) Name() string { return WeKnoraCloudEngineName }
|
func (e *weKnoraCloudEngine) Name() string { return WeKnoraCloudEngineName }
|
||||||
func (e *weKnoraCloudEngine) Description() string { return "WeKnoraCloud document reader" }
|
func (e *weKnoraCloudEngine) Description() string { return "WeKnoraCloud document reader" }
|
||||||
func (e *weKnoraCloudEngine) FileTypes(_ bool) []string {
|
func (e *weKnoraCloudEngine) FileTypes(_ bool) []string {
|
||||||
return weKnoraCloudSupportedFileTypes()
|
return []string{"docx", "doc", "pdf", "md", "markdown", "xlsx", "xls", "pptx", "ppt"}
|
||||||
}
|
}
|
||||||
func (e *weKnoraCloudEngine) CheckAvailable(docreaderConnected bool, overrides map[string]string) (bool, string) {
|
func (e *weKnoraCloudEngine) CheckAvailable(docreaderConnected bool, overrides map[string]string) (bool, string) {
|
||||||
if docreaderConnected && isWeKnoraCloudDocReaderAddr(overrides["docreader_addr"]) {
|
if overrides["docreader_app_id"] != "" {
|
||||||
return true, ""
|
return true, ""
|
||||||
}
|
}
|
||||||
return false, "WeKnoraCloud document reader is not initialized"
|
return false, "WeKnora Cloud credentials not configured. Go to Settings → WeKnora Cloud to set up."
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -192,7 +191,3 @@ func ListAllEngines(docreaderConnected bool, overrides map[string]string, remote
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func isWeKnoraCloudDocReaderAddr(addr string) bool {
|
|
||||||
return strings.TrimSuffix(strings.TrimSpace(addr), "/") == strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")+"/api/v1/doc/reader"
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,21 +3,19 @@ package docparser
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/docreader/proto"
|
"github.com/Tencent/WeKnora/docreader/proto"
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"google.golang.org/grpc/resolver"
|
"google.golang.org/grpc/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = log.New(os.Stdout, "[DocParser] ", log.LstdFlags|log.Lmicroseconds)
|
|
||||||
|
|
||||||
func getMaxMessageSize() int {
|
func getMaxMessageSize() int {
|
||||||
if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" {
|
if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" {
|
||||||
if size, err := strconv.Atoi(sizeStr); err == nil && size > 0 {
|
if size, err := strconv.Atoi(sizeStr); err == nil && size > 0 {
|
||||||
@@ -46,7 +44,6 @@ func NewGRPCDocumentReader(addr string) (*GRPCDocumentReader, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *GRPCDocumentReader) connect(addr string) error {
|
func (p *GRPCDocumentReader) connect(addr string) error {
|
||||||
logger.Printf("INFO: Connecting to docreader at %s", addr)
|
|
||||||
|
|
||||||
maxMsgSize := getMaxMessageSize()
|
maxMsgSize := getMaxMessageSize()
|
||||||
opts := []grpc.DialOption{
|
opts := []grpc.DialOption{
|
||||||
@@ -64,7 +61,7 @@ func (p *GRPCDocumentReader) connect(addr string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to docreader: %w", err)
|
return fmt.Errorf("failed to connect to docreader: %w", err)
|
||||||
}
|
}
|
||||||
logger.Printf("INFO: Connected to docreader in %v", time.Since(start))
|
logger.Infof(context.Background(), "Connected to docreader in %v", time.Since(start))
|
||||||
|
|
||||||
p.conn = conn
|
p.conn = conn
|
||||||
p.client = proto.NewDocReaderClient(conn)
|
p.client = proto.NewDocReaderClient(conn)
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// stringOr returns val (trimmed) if non-empty, otherwise fallback.
|
// stringOr returns val (trimmed) if non-empty, otherwise fallback.
|
||||||
@@ -56,7 +58,7 @@ func logResponseStructure(label string, obj interface{}, prefix string) {
|
|||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
logger.Printf("DEBUG: [%s] %s = {object with %d keys: %s}", label, prefix, len(v), strings.Join(keys, ", "))
|
logger.Infof(context.Background(), "[%s] %s = {object with %d keys: %s}", label, prefix, len(v), strings.Join(keys, ", "))
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
val := v[key]
|
val := v[key]
|
||||||
path := prefix + "." + key
|
path := prefix + "." + key
|
||||||
@@ -64,36 +66,36 @@ func logResponseStructure(label string, obj interface{}, prefix string) {
|
|||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
logResponseStructure(label, inner, path)
|
logResponseStructure(label, inner, path)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
logger.Printf("DEBUG: [%s] %s = [array with %d items]", label, path, len(inner))
|
logger.Infof(context.Background(), "[%s] %s = [array with %d items]", label, path, len(inner))
|
||||||
if len(inner) > 0 {
|
if len(inner) > 0 {
|
||||||
logger.Printf("DEBUG: [%s] %s[0] type=%T", label, path, inner[0])
|
logger.Infof(context.Background(), "[%s] %s[0] type=%T", label, path, inner[0])
|
||||||
if len(inner) <= 3 {
|
if len(inner) <= 3 {
|
||||||
for i, item := range inner {
|
for i, item := range inner {
|
||||||
logResponseStructure(label, item, fmt.Sprintf("%s[%d]", path, i))
|
logResponseStructure(label, item, fmt.Sprintf("%s[%d]", path, i))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logResponseStructure(label, inner[0], path+"[0]")
|
logResponseStructure(label, inner[0], path+"[0]")
|
||||||
logger.Printf("DEBUG: [%s] ... and %d more items in %s", label, len(inner)-1, path)
|
logger.Infof(context.Background(), "[%s] ... and %d more items in %s", label, len(inner)-1, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if len(inner) > 200 {
|
if len(inner) > 200 {
|
||||||
logger.Printf("DEBUG: [%s] %s = string(%d chars): %.200s...", label, path, len(inner), inner)
|
logger.Infof(context.Background(), "[%s] %s = string(%d chars): %.200s...", label, path, len(inner), inner)
|
||||||
} else {
|
} else {
|
||||||
logger.Printf("DEBUG: [%s] %s = %q", label, path, inner)
|
logger.Infof(context.Background(), "[%s] %s = %q", label, path, inner)
|
||||||
}
|
}
|
||||||
case float64:
|
case float64:
|
||||||
logger.Printf("DEBUG: [%s] %s = %v (number)", label, path, inner)
|
logger.Infof(context.Background(), "[%s] %s = %v (number)", label, path, inner)
|
||||||
case bool:
|
case bool:
|
||||||
logger.Printf("DEBUG: [%s] %s = %v (bool)", label, path, inner)
|
logger.Infof(context.Background(), "[%s] %s = %v (bool)", label, path, inner)
|
||||||
case nil:
|
case nil:
|
||||||
logger.Printf("DEBUG: [%s] %s = null", label, path)
|
logger.Infof(context.Background(), "[%s] %s = null", label, path)
|
||||||
default:
|
default:
|
||||||
logger.Printf("DEBUG: [%s] %s = %v (%T)", label, path, val, val)
|
logger.Infof(context.Background(), "[%s] %s = %v (%T)", label, path, val, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
logger.Printf("DEBUG: [%s] %s = [array with %d items]", label, prefix, len(v))
|
logger.Infof(context.Background(), "[%s] %s = [array with %d items]", label, prefix, len(v))
|
||||||
if len(v) > 0 {
|
if len(v) > 0 {
|
||||||
if len(v) <= 3 {
|
if len(v) <= 3 {
|
||||||
for i, item := range v {
|
for i, item := range v {
|
||||||
@@ -101,10 +103,10 @@ func logResponseStructure(label string, obj interface{}, prefix string) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logResponseStructure(label, v[0], prefix+"[0]")
|
logResponseStructure(label, v[0], prefix+"[0]")
|
||||||
logger.Printf("DEBUG: [%s] ... and %d more items in %s", label, len(v)-1, prefix)
|
logger.Infof(context.Background(), "[%s] ... and %d more items in %s", label, len(v)-1, prefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
logger.Printf("DEBUG: [%s] %s = %v (%T)", label, prefix, v, v)
|
logger.Infof(context.Background(), "[%s] %s = %v (%T)", label, prefix, v, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ func NewHTTPDocumentReader(baseURL string) (*HTTPDocumentReader, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
if p.baseURL != "" {
|
if p.baseURL != "" {
|
||||||
logger.Printf("INFO: HTTP docreader base URL: %s", p.baseURL)
|
logger.Infof(context.Background(), "INFO: HTTP docreader base URL: %s", p.baseURL)
|
||||||
}
|
}
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
@@ -88,7 +89,7 @@ func (p *HTTPDocumentReader) Reconnect(addr string) error {
|
|||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
p.baseURL = strings.TrimSuffix(addr, "/")
|
p.baseURL = strings.TrimSuffix(addr, "/")
|
||||||
logger.Printf("INFO: HTTP docreader base URL set to %s", p.baseURL)
|
logger.Infof(context.Background(), "INFO: HTTP docreader base URL set to %s", p.baseURL)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/utils"
|
"github.com/Tencent/WeKnora/internal/utils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -61,7 +62,7 @@ func (c *MinerUCloudReader) Read(ctx context.Context, req *types.ReadRequest) (*
|
|||||||
return &types.ReadResult{Error: "no file content provided"}, nil
|
return &types.ReadResult{Error: "no file content provided"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("INFO: [MinerUCloud] Parsing file=%s size=%d via %s", req.FileName, len(content), c.baseURL)
|
logger.Infof(context.Background(), "[MinerUCloud] Parsing file=%s size=%d via %s", req.FileName, len(content), c.baseURL)
|
||||||
|
|
||||||
ext := filepath.Ext(req.FileName)
|
ext := filepath.Ext(req.FileName)
|
||||||
if ext == "" && req.FileType != "" {
|
if ext == "" && req.FileType != "" {
|
||||||
@@ -158,7 +159,7 @@ func (c *MinerUCloudReader) applyUploadURLs(ctx context.Context, fileName, ext s
|
|||||||
return "", "", fmt.Errorf("API returned no file_urls")
|
return "", "", fmt.Errorf("API returned no file_urls")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("INFO: [MinerUCloud] batch apply ok: batch_id=%s, urls=%d", result.Data.BatchID, len(result.Data.FileURLs))
|
logger.Infof(context.Background(), "[MinerUCloud] batch apply ok: batch_id=%s, urls=%d", result.Data.BatchID, len(result.Data.FileURLs))
|
||||||
return result.Data.BatchID, result.Data.FileURLs[0], nil
|
return result.Data.BatchID, result.Data.FileURLs[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,7 +179,7 @@ func (c *MinerUCloudReader) uploadFile(ctx context.Context, uploadURL string, co
|
|||||||
if resp.StatusCode >= 300 {
|
if resp.StatusCode >= 300 {
|
||||||
return fmt.Errorf("PUT upload status %d", resp.StatusCode)
|
return fmt.Errorf("PUT upload status %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
logger.Printf("INFO: [MinerUCloud] file uploaded, status=%d", resp.StatusCode)
|
logger.Infof(context.Background(), "[MinerUCloud] file uploaded, status=%d", resp.StatusCode)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,14 +219,14 @@ func (c *MinerUCloudReader) pollBatchResult(ctx context.Context, batchID string)
|
|||||||
|
|
||||||
items, err := c.fetchBatchStatus(ctx, batchID, headers)
|
items, err := c.fetchBatchStatus(ctx, batchID, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("WARN: [MinerUCloud] poll #%d failed: %v", pollCount, err)
|
logger.Errorf(context.Background(), "[MinerUCloud] poll #%d failed: %v", pollCount, err)
|
||||||
sleepCtx(ctx, defaultPollInterval)
|
sleepCtx(ctx, defaultPollInterval)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(items) == 0 {
|
if len(items) == 0 {
|
||||||
if pollCount <= 3 || pollCount%10 == 0 {
|
if pollCount <= 3 || pollCount%10 == 0 {
|
||||||
logger.Printf("INFO: [MinerUCloud] poll #%d: extract_result empty, retrying", pollCount)
|
logger.Infof(context.Background(), "[MinerUCloud] poll #%d: extract_result empty, retrying", pollCount)
|
||||||
}
|
}
|
||||||
sleepCtx(ctx, defaultPollInterval)
|
sleepCtx(ctx, defaultPollInterval)
|
||||||
continue
|
continue
|
||||||
@@ -235,7 +236,7 @@ func (c *MinerUCloudReader) pollBatchResult(ctx context.Context, batchID string)
|
|||||||
state := strings.ToLower(item.State)
|
state := strings.ToLower(item.State)
|
||||||
|
|
||||||
if pollCount == 1 || pollCount%10 == 0 || state == "done" || state == "failed" {
|
if pollCount == 1 || pollCount%10 == 0 || state == "done" || state == "failed" {
|
||||||
logger.Printf("INFO: [MinerUCloud] poll #%d: file=%s state=%s pages=%d/%d",
|
logger.Infof(context.Background(), "[MinerUCloud] poll #%d: file=%s state=%s pages=%d/%d",
|
||||||
pollCount, item.FileName, state, item.Progress.ExtractedPages, item.Progress.TotalPages)
|
pollCount, item.FileName, state, item.Progress.ExtractedPages, item.Progress.TotalPages)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,9 +291,9 @@ func (c *MinerUCloudReader) fetchBatchStatus(ctx context.Context, batchID string
|
|||||||
// Dump the raw extract_result JSON for debugging
|
// Dump the raw extract_result JSON for debugging
|
||||||
rawExtract := string(pollResp.Data.ExtractResult)
|
rawExtract := string(pollResp.Data.ExtractResult)
|
||||||
if len(rawExtract) > 4000 {
|
if len(rawExtract) > 4000 {
|
||||||
logger.Printf("DEBUG: [MinerUCloud] Raw extract_result (truncated to 4000 chars): %s ...", rawExtract[:4000])
|
logger.Infof(context.Background(), "[MinerUCloud] Raw extract_result (truncated to 4000 chars): %s ...", rawExtract[:4000])
|
||||||
} else {
|
} else {
|
||||||
logger.Printf("DEBUG: [MinerUCloud] Raw extract_result: %s", rawExtract)
|
logger.Infof(context.Background(), "[MinerUCloud] Raw extract_result: %s", rawExtract)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pretty-print the structure to reveal all available fields
|
// Pretty-print the structure to reveal all available fields
|
||||||
@@ -323,7 +324,7 @@ func (c *MinerUCloudReader) fetchBatchStatus(ctx context.Context, batchID string
|
|||||||
func (c *MinerUCloudReader) extractDoneResult(_ context.Context, item *extractResultItem) (string, []types.ImageRef, error) {
|
func (c *MinerUCloudReader) extractDoneResult(_ context.Context, item *extractResultItem) (string, []types.ImageRef, error) {
|
||||||
text := firstNonEmpty(item.Markdown, item.Content, item.Text)
|
text := firstNonEmpty(item.Markdown, item.Content, item.Text)
|
||||||
if text != "" {
|
if text != "" {
|
||||||
logger.Printf("INFO: [MinerUCloud] parsed (inline), length=%d", len(text))
|
logger.Infof(context.Background(), "[MinerUCloud] parsed (inline), length=%d", len(text))
|
||||||
return text, nil, nil
|
return text, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,7 +337,7 @@ func (c *MinerUCloudReader) extractDoneResult(_ context.Context, item *extractRe
|
|||||||
return "", nil, fmt.Errorf("extract zip: %w", err)
|
return "", nil, fmt.Errorf("extract zip: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("INFO: [MinerUCloud] parsed (zip), markdown=%d chars, images=%d", len(md), len(imageRefs))
|
logger.Infof(context.Background(), "[MinerUCloud] parsed (zip), markdown=%d chars, images=%d", len(md), len(imageRefs))
|
||||||
return md, imageRefs, nil
|
return md, imageRefs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,13 +411,13 @@ func downloadAndExtractZip(zipURL string) (string, []types.ImageRef, error) {
|
|||||||
|
|
||||||
resolved := resolveInZip(imgPath, mdDir, entries)
|
resolved := resolveInZip(imgPath, mdDir, entries)
|
||||||
if resolved == nil {
|
if resolved == nil {
|
||||||
logger.Printf("WARN: [MinerUCloud] image not found in zip: %s", imgPath)
|
logger.Errorf(context.Background(), "[MinerUCloud] image not found in zip: %s", imgPath)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
imgData, err := readZipEntryBytes(resolved)
|
imgData, err := readZipEntryBytes(resolved)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("WARN: [MinerUCloud] failed to read zip image %s: %v", imgPath, err)
|
logger.Errorf(context.Background(), "[MinerUCloud] failed to read zip image %s: %v", imgPath, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
htmltomd "github.com/JohannesKaufmann/html-to-markdown/v2"
|
htmltomd "github.com/JohannesKaufmann/html-to-markdown/v2"
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ func (c *MinerUReader) Read(ctx context.Context, req *types.ReadRequest) (*types
|
|||||||
return &types.ReadResult{Error: "no file content provided"}, nil
|
return &types.ReadResult{Error: "no file content provided"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("INFO: [MinerU] Parsing file=%s size=%d via %s", req.FileName, len(content), c.endpoint)
|
logger.Infof(context.Background(), "[MinerU] Parsing file=%s size=%d via %s", req.FileName, len(content), c.endpoint)
|
||||||
|
|
||||||
mdContent, imagesB64, err := c.callFileParse(ctx, content)
|
mdContent, imagesB64, err := c.callFileParse(ctx, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -71,7 +72,7 @@ func (c *MinerUReader) Read(ctx context.Context, req *types.ReadRequest) (*types
|
|||||||
|
|
||||||
mdContent, imageRefs = ensureOriginalImageRef(req, mdContent, imageRefs)
|
mdContent, imageRefs = ensureOriginalImageRef(req, mdContent, imageRefs)
|
||||||
|
|
||||||
logger.Printf("INFO: [MinerU] Parsed successfully, markdown=%d chars, images=%d", len(mdContent), len(imageRefs))
|
logger.Infof(context.Background(), "[MinerU] Parsed successfully, markdown=%d chars, images=%d", len(mdContent), len(imageRefs))
|
||||||
|
|
||||||
return &types.ReadResult{
|
return &types.ReadResult{
|
||||||
MarkdownContent: mdContent,
|
MarkdownContent: mdContent,
|
||||||
@@ -158,9 +159,9 @@ func (c *MinerUReader) callFileParse(ctx context.Context, content []byte) (strin
|
|||||||
// Dump raw response for debugging (truncate if too large)
|
// Dump raw response for debugging (truncate if too large)
|
||||||
rawStr := string(respBody)
|
rawStr := string(respBody)
|
||||||
if len(rawStr) > 4000 {
|
if len(rawStr) > 4000 {
|
||||||
logger.Printf("DEBUG: [MinerU] Raw response (truncated to 4000 chars): %s ...", rawStr[:4000])
|
logger.Infof(context.Background(), "[MinerU] Raw response (truncated to 4000 chars): %s ...", rawStr[:4000])
|
||||||
} else {
|
} else {
|
||||||
logger.Printf("DEBUG: [MinerU] Raw response: %s", rawStr)
|
logger.Infof(context.Background(), "[MinerU] Raw response: %s", rawStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also pretty-print the top-level structure (without large base64 blobs)
|
// Also pretty-print the top-level structure (without large base64 blobs)
|
||||||
@@ -179,15 +180,15 @@ func (c *MinerUReader) callFileParse(ctx context.Context, content []byte) (strin
|
|||||||
// - some variants: results.files.*
|
// - some variants: results.files.*
|
||||||
// Prefer document when available, then fallback to files.
|
// Prefer document when available, then fallback to files.
|
||||||
if result.Results.Document.MDContent != "" || len(result.Results.Document.Images) > 0 {
|
if result.Results.Document.MDContent != "" || len(result.Results.Document.Images) > 0 {
|
||||||
logger.Printf("DEBUG: [MinerU] Using response path: results.document")
|
logger.Infof(context.Background(), "[MinerU] Using response path: results.document")
|
||||||
return result.Results.Document.MDContent, result.Results.Document.Images, nil
|
return result.Results.Document.MDContent, result.Results.Document.Images, nil
|
||||||
}
|
}
|
||||||
if result.Results.Files.MDContent != "" || len(result.Results.Files.Images) > 0 {
|
if result.Results.Files.MDContent != "" || len(result.Results.Files.Images) > 0 {
|
||||||
logger.Printf("DEBUG: [MinerU] Using response path: results.files")
|
logger.Infof(context.Background(), "[MinerU] Using response path: results.files")
|
||||||
return result.Results.Files.MDContent, result.Results.Files.Images, nil
|
return result.Results.Files.MDContent, result.Results.Files.Images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("WARN: [MinerU] Response has no markdown/images under results.document or results.files")
|
logger.Errorf(context.Background(), "[MinerU] Response has no markdown/images under results.document or results.files")
|
||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +210,7 @@ func (c *MinerUReader) processImages(mdContent string, imagesB64 map[string]stri
|
|||||||
ext = m[1]
|
ext = m[1]
|
||||||
decoded, err := base64.StdEncoding.DecodeString(m[2])
|
decoded, err := base64.StdEncoding.DecodeString(m[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("WARN: [MinerU] Failed to decode base64 image %s: %v", ipath, err)
|
logger.Errorf(context.Background(), "[MinerU] Failed to decode base64 image %s: %v", ipath, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
imgBytes = decoded
|
imgBytes = decoded
|
||||||
@@ -217,7 +218,7 @@ func (c *MinerUReader) processImages(mdContent string, imagesB64 map[string]stri
|
|||||||
// raw base64 without data URI prefix
|
// raw base64 without data URI prefix
|
||||||
decoded, err := base64.StdEncoding.DecodeString(b64Str)
|
decoded, err := base64.StdEncoding.DecodeString(b64Str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("WARN: [MinerU] Failed to decode raw base64 image %s: %v", ipath, err)
|
logger.Errorf(context.Background(), "[MinerU] Failed to decode raw base64 image %s: %v", ipath, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
imgBytes = decoded
|
imgBytes = decoded
|
||||||
@@ -271,7 +272,7 @@ func PingMinerU(endpoint string) (bool, string) {
|
|||||||
func htmlToMarkdown(content string) string {
|
func htmlToMarkdown(content string) string {
|
||||||
md, err := htmltomd.ConvertString(content)
|
md, err := htmltomd.ConvertString(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("WARN: [MinerU] html-to-markdown conversion failed, using raw content: %v", err)
|
logger.Errorf(context.Background(), "[MinerU] html-to-markdown conversion failed, using raw content: %v", err)
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
return md
|
return md
|
||||||
|
|||||||
@@ -8,18 +8,22 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/utils"
|
"github.com/Tencent/WeKnora/internal/models/utils"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
weKnoraCloudReaderBaseURL = "https://weknora.weixin.qq.com/api/v1/doc"
|
||||||
|
)
|
||||||
|
|
||||||
// WeKnoraCloudSignedDocumentReader implements the docreader HTTP protocol with WeKnoraCloud signing.
|
// WeKnoraCloudSignedDocumentReader implements the docreader HTTP protocol with WeKnoraCloud signing.
|
||||||
type WeKnoraCloudSignedDocumentReader struct {
|
type WeKnoraCloudSignedDocumentReader struct {
|
||||||
baseURL string
|
|
||||||
appID string
|
appID string
|
||||||
apiKey string
|
apiKey string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
@@ -28,11 +32,7 @@ type WeKnoraCloudSignedDocumentReader struct {
|
|||||||
pollTimeout time.Duration
|
pollTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWeKnoraCloudSignedDocumentReader(baseURL, appID, apiKey string) (*WeKnoraCloudSignedDocumentReader, error) {
|
func NewWeKnoraCloudSignedDocumentReader(appID, apiKey string) (*WeKnoraCloudSignedDocumentReader, error) {
|
||||||
baseURL = strings.TrimSuffix(strings.TrimSpace(baseURL), "/")
|
|
||||||
if baseURL == "" {
|
|
||||||
return nil, fmt.Errorf("docreader base URL is required")
|
|
||||||
}
|
|
||||||
if appID == "" {
|
if appID == "" {
|
||||||
return nil, fmt.Errorf("WeKnoraCloud appID is required")
|
return nil, fmt.Errorf("WeKnoraCloud appID is required")
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,6 @@ func NewWeKnoraCloudSignedDocumentReader(baseURL, appID, apiKey string) (*WeKnor
|
|||||||
return nil, fmt.Errorf("WeKnoraCloud apiKey is required")
|
return nil, fmt.Errorf("WeKnoraCloud apiKey is required")
|
||||||
}
|
}
|
||||||
return &WeKnoraCloudSignedDocumentReader{
|
return &WeKnoraCloudSignedDocumentReader{
|
||||||
baseURL: baseURL,
|
|
||||||
appID: appID,
|
appID: appID,
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
initialPollInterval: 500 * time.Millisecond,
|
initialPollInterval: 500 * time.Millisecond,
|
||||||
@@ -58,28 +57,23 @@ func NewWeKnoraCloudSignedDocumentReader(baseURL, appID, apiKey string) (*WeKnor
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *WeKnoraCloudSignedDocumentReader) Reconnect(addr string) error {
|
func (p *WeKnoraCloudSignedDocumentReader) Reconnect(addr string) error {
|
||||||
p.baseURL = strings.TrimSuffix(strings.TrimSpace(addr), "/")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *WeKnoraCloudSignedDocumentReader) IsConnected() bool { return p.baseURL != "" }
|
func (p *WeKnoraCloudSignedDocumentReader) IsConnected() bool { return true }
|
||||||
|
|
||||||
func (p *WeKnoraCloudSignedDocumentReader) ListEngines(ctx context.Context, overrides map[string]string) ([]types.ParserEngineInfo, error) {
|
func (p *WeKnoraCloudSignedDocumentReader) ListEngines(ctx context.Context, overrides map[string]string) ([]types.ParserEngineInfo, error) {
|
||||||
if !p.IsConnected() {
|
|
||||||
return nil, errNotConnected
|
|
||||||
}
|
|
||||||
return []types.ParserEngineInfo{{
|
return []types.ParserEngineInfo{{
|
||||||
Name: WeKnoraCloudEngineName,
|
Name: WeKnoraCloudEngineName,
|
||||||
Description: "WeKnoraCloud signed docreader",
|
Description: "WeKnoraCloud signed docreader",
|
||||||
FileTypes: weKnoraCloudSupportedFileTypes(),
|
FileTypes: []string{"docx", "doc", "pdf", "md", "markdown", "xlsx", "xls", "pptx", "ppt"},
|
||||||
Available: true,
|
Available: true,
|
||||||
}}, nil
|
}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *WeKnoraCloudSignedDocumentReader) Read(ctx context.Context, req *types.ReadRequest) (*types.ReadResult, error) {
|
func (p *WeKnoraCloudSignedDocumentReader) Read(ctx context.Context, req *types.ReadRequest) (*types.ReadResult, error) {
|
||||||
if !p.IsConnected() {
|
logger.Infof(ctx, "[WeKnoraCloud] read start file=%q type=%q engine=%q hasURL=%v contentLen=%d requestID=%q",
|
||||||
return nil, errNotConnected
|
req.FileName, req.FileType, req.ParserEngine, strings.TrimSpace(req.URL) != "", len(req.FileContent), req.RequestID)
|
||||||
}
|
|
||||||
body := httpReadRequest{
|
body := httpReadRequest{
|
||||||
FileName: req.FileName,
|
FileName: req.FileName,
|
||||||
FileType: req.FileType,
|
FileType: req.FileType,
|
||||||
@@ -96,28 +90,35 @@ func (p *WeKnoraCloudSignedDocumentReader) Read(ctx context.Context, req *types.
|
|||||||
}
|
}
|
||||||
jsonBody, err := json.Marshal(body)
|
jsonBody, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] marshal read request: %v", err)
|
||||||
return nil, fmt.Errorf("http marshal read request: %w", err)
|
return nil, fmt.Errorf("http marshal read request: %w", err)
|
||||||
}
|
}
|
||||||
httpReq, err := p.newSignedRequest(ctx, http.MethodPost, p.baseURL, jsonBody)
|
httpReq, err := p.newSignedRequest(ctx, http.MethodPost, weKnoraCloudReaderBaseURL+"/reader", jsonBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] signed read request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err := p.client.Do(httpReq)
|
resp, err := p.client.Do(httpReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] http read request failed: %v", err)
|
||||||
return nil, fmt.Errorf("http read failed: %w", err)
|
return nil, fmt.Errorf("http read failed: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusOK {
|
||||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] http read unexpected status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||||
return nil, fmt.Errorf("http read status %d: %s", resp.StatusCode, string(bodyBytes))
|
return nil, fmt.Errorf("http read status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||||
}
|
}
|
||||||
var submit weKnoraCloudAsyncSubmitResponse
|
var submit weKnoraCloudAsyncSubmitResponse
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&submit); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&submit); err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] decode read submit response: %v", err)
|
||||||
return nil, fmt.Errorf("http decode read submit response: %w", err)
|
return nil, fmt.Errorf("http decode read submit response: %w", err)
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(submit.TaskID) == "" {
|
if strings.TrimSpace(submit.TaskID) == "" {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] submit response missing task_id (status=%q message=%q)", submit.Status, submit.Message)
|
||||||
return nil, fmt.Errorf("weknoracloud docreader submit response missing task_id")
|
return nil, fmt.Errorf("weknoracloud docreader submit response missing task_id")
|
||||||
}
|
}
|
||||||
|
logger.Infof(ctx, "[WeKnoraCloud] task submitted task_id=%s file=%q type=%q", submit.TaskID, req.FileName, req.FileType)
|
||||||
return p.pollTaskResult(ctx, submit.TaskID)
|
return p.pollTaskResult(ctx, submit.TaskID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,25 +140,6 @@ type weKnoraCloudAsyncTaskResponse struct {
|
|||||||
UpdatedAt int64 `json:"updated_at"`
|
UpdatedAt int64 `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func weKnoraCloudSupportedFileTypes() []string {
|
|
||||||
seen := map[string]struct{}{}
|
|
||||||
var fileTypes []string
|
|
||||||
for _, engine := range localEngines {
|
|
||||||
if engine.Name() == WeKnoraCloudEngineName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, fileType := range engine.FileTypes(true) {
|
|
||||||
if _, ok := seen[fileType]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seen[fileType] = struct{}{}
|
|
||||||
fileTypes = append(fileTypes, fileType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(fileTypes)
|
|
||||||
return fileTypes
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, taskID string) (*types.ReadResult, error) {
|
func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, taskID string) (*types.ReadResult, error) {
|
||||||
pollCtx := ctx
|
pollCtx := ctx
|
||||||
if _, ok := ctx.Deadline(); !ok && p.pollTimeout > 0 {
|
if _, ok := ctx.Deadline(); !ok && p.pollTimeout > 0 {
|
||||||
@@ -165,15 +147,17 @@ func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, t
|
|||||||
pollCtx, cancel = context.WithTimeout(ctx, p.pollTimeout)
|
pollCtx, cancel = context.WithTimeout(ctx, p.pollTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
statusURL := strings.TrimSuffix(p.baseURL, "/reader") + "/" + taskID
|
statusURL := weKnoraCloudReaderBaseURL + "/" + taskID
|
||||||
currentInterval := p.initialPollInterval
|
currentInterval := p.initialPollInterval
|
||||||
for {
|
for {
|
||||||
httpReq, err := p.newSignedRequest(pollCtx, http.MethodGet, statusURL, nil)
|
httpReq, err := p.newSignedRequest(pollCtx, http.MethodGet, statusURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] poll signed request task_id=%s: %v", taskID, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err := p.client.Do(httpReq)
|
resp, err := p.client.Do(httpReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] http poll task_id=%s failed: %v", taskID, err)
|
||||||
return nil, fmt.Errorf("http poll task failed: %w", err)
|
return nil, fmt.Errorf("http poll task failed: %w", err)
|
||||||
}
|
}
|
||||||
var taskResp weKnoraCloudAsyncTaskResponse
|
var taskResp weKnoraCloudAsyncTaskResponse
|
||||||
@@ -182,10 +166,12 @@ func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, t
|
|||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
err = fmt.Errorf("http poll task status %d: %s", resp.StatusCode, string(bodyBytes))
|
err = fmt.Errorf("http poll task status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] poll task_id=%s status %d: %s", taskID, resp.StatusCode, string(bodyBytes))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if decodeErr := json.NewDecoder(resp.Body).Decode(&taskResp); decodeErr != nil {
|
if decodeErr := json.NewDecoder(resp.Body).Decode(&taskResp); decodeErr != nil {
|
||||||
err = fmt.Errorf("http decode task response: %w", decodeErr)
|
err = fmt.Errorf("http decode task response: %w", decodeErr)
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] poll task_id=%s decode response: %v", taskID, decodeErr)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -194,27 +180,40 @@ func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, t
|
|||||||
switch taskResp.Status {
|
switch taskResp.Status {
|
||||||
case "completed":
|
case "completed":
|
||||||
if taskResp.Result == nil {
|
if taskResp.Result == nil {
|
||||||
|
logger.Infof(ctx, "[WeKnoraCloud] task_id=%s completed with no result payload", taskID)
|
||||||
return &types.ReadResult{}, nil
|
return &types.ReadResult{}, nil
|
||||||
}
|
}
|
||||||
return fromHTTPReadResponse(taskResp.Result), nil
|
res := fromHTTPReadResponse(taskResp.Result)
|
||||||
|
if res.Error != "" {
|
||||||
|
logger.Errorf(ctx, "[WeKnoraCloud] task_id=%s completed with result.error: %s", taskID, res.Error)
|
||||||
|
} else {
|
||||||
|
logger.Debugf(ctx, "[WeKnoraCloud] task_id=%s completed ok markdownLen=%d", taskID, len(res.MarkdownContent))
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
case "failed":
|
case "failed":
|
||||||
if taskResp.Error != "" {
|
if taskResp.Error != "" {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] task_id=%s failed: %s", taskID, taskResp.Error)
|
||||||
return nil, fmt.Errorf("weknoracloud docreader task failed: %s", taskResp.Error)
|
return nil, fmt.Errorf("weknoracloud docreader task failed: %s", taskResp.Error)
|
||||||
}
|
}
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] task_id=%s failed: %s", taskID, taskResp.Message)
|
||||||
return nil, fmt.Errorf("weknoracloud docreader task failed: %s", taskResp.Message)
|
return nil, fmt.Errorf("weknoracloud docreader task failed: %s", taskResp.Message)
|
||||||
case "cancelled":
|
case "cancelled":
|
||||||
if taskResp.Error != "" {
|
if taskResp.Error != "" {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] task_id=%s cancelled: %s", taskID, taskResp.Error)
|
||||||
return nil, fmt.Errorf("weknoracloud docreader task cancelled: %s", taskResp.Error)
|
return nil, fmt.Errorf("weknoracloud docreader task cancelled: %s", taskResp.Error)
|
||||||
}
|
}
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] task_id=%s cancelled", taskID)
|
||||||
return nil, fmt.Errorf("weknoracloud docreader task cancelled")
|
return nil, fmt.Errorf("weknoracloud docreader task cancelled")
|
||||||
}
|
}
|
||||||
if err := pollCtx.Err(); err != nil {
|
if err := pollCtx.Err(); err != nil {
|
||||||
|
logger.Errorf(ctx, "[WeKnoraCloud] poll task_id=%s aborted before sleep: %v", taskID, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exponential backoff: multiply by 1.5 each time, cap at maxPollInterval
|
// Exponential backoff: multiply by 1.5 each time, cap at maxPollInterval
|
||||||
select {
|
select {
|
||||||
case <-pollCtx.Done():
|
case <-pollCtx.Done():
|
||||||
|
logger.Errorf(ctx, "[WeKnoraCloud] poll task_id=%s stopped: %v", taskID, pollCtx.Err())
|
||||||
return nil, pollCtx.Err()
|
return nil, pollCtx.Err()
|
||||||
case <-time.After(currentInterval):
|
case <-time.After(currentInterval):
|
||||||
// Update interval for next iteration
|
// Update interval for next iteration
|
||||||
@@ -234,6 +233,7 @@ func (p *WeKnoraCloudSignedDocumentReader) newSignedRequest(ctx context.Context,
|
|||||||
}
|
}
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
|
httpReq, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorf(context.Background(), "[WeKnoraCloud] http new request %s %s: %v", method, url, err)
|
||||||
return nil, fmt.Errorf("http new request: %w", err)
|
return nil, fmt.Errorf("http new request: %w", err)
|
||||||
}
|
}
|
||||||
httpReq.Header.Set("Content-Type", "application/json")
|
httpReq.Header.Set("Content-Type", "application/json")
|
||||||
|
|||||||
@@ -312,7 +312,6 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.logRequest(ctx, req, false)
|
c.logRequest(ctx, req, false)
|
||||||
|
|
||||||
resp, err := c.client.CreateChatCompletion(ctx, req)
|
resp, err := c.client.CreateChatCompletion(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if isMultimodalNotSupportedError(err) {
|
if isMultimodalNotSupportedError(err) {
|
||||||
@@ -342,13 +341,15 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
|
|||||||
return nil, fmt.Errorf("marshal request: %w", err)
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof(ctx, "[LLM Request] model=%s, raw HTTP request:\n%s", c.modelName, secutils.CompactImageDataURLForLog(string(jsonData)))
|
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
endpoint = c.baseURL + "/chat/completions"
|
endpoint = c.baseURL + "/chat/completions"
|
||||||
}
|
}
|
||||||
if err := secutils.ValidateURLForSSRF(endpoint); err != nil {
|
if err := secutils.ValidateURLForSSRF(endpoint); err != nil {
|
||||||
return nil, fmt.Errorf("endpoint SSRF check failed: %w", err)
|
return nil, fmt.Errorf("endpoint SSRF check failed: %w", err)
|
||||||
}
|
}
|
||||||
|
logger.Infof(ctx, "[LLM Request] Remote HTTP, endpoint=%s, model=%s, raw HTTP request:\n%s",
|
||||||
|
endpoint, c.modelName, secutils.CompactImageDataURLForLog(string(jsonData)))
|
||||||
|
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create request: %w", err)
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
@@ -366,6 +367,14 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
|
|||||||
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print headers
|
||||||
|
headers, err := json.Marshal(httpReq.Header)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal headers: %w", err)
|
||||||
|
}
|
||||||
|
logger.Infof(ctx, "[LLM Request] Remote HTTP, endpoint=%s, model=%s, headers: %s",
|
||||||
|
endpoint, c.modelName, string(headers))
|
||||||
|
|
||||||
resp, err := rawHTTPClient.Do(httpReq)
|
resp, err := rawHTTPClient.Do(httpReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("send request: %w", err)
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ const (
|
|||||||
func AllProviders() []ProviderName {
|
func AllProviders() []ProviderName {
|
||||||
return []ProviderName{
|
return []ProviderName{
|
||||||
ProviderGeneric,
|
ProviderGeneric,
|
||||||
|
ProviderWeKnoraCloud,
|
||||||
ProviderAliyun,
|
ProviderAliyun,
|
||||||
ProviderZhipu,
|
ProviderZhipu,
|
||||||
ProviderVolcengine,
|
ProviderVolcengine,
|
||||||
@@ -87,7 +88,6 @@ func AllProviders() []ProviderName {
|
|||||||
ProviderNvidia,
|
ProviderNvidia,
|
||||||
ProviderNovita,
|
ProviderNovita,
|
||||||
ProviderAzureOpenAI,
|
ProviderAzureOpenAI,
|
||||||
ProviderWeKnoraCloud,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,6 +259,8 @@ func DetectProvider(baseURL string) ProviderName {
|
|||||||
return ProviderNvidia
|
return ProviderNvidia
|
||||||
case containsAny(baseURL, "api.novita.ai", "novita.ai"):
|
case containsAny(baseURL, "api.novita.ai", "novita.ai"):
|
||||||
return ProviderNovita
|
return ProviderNovita
|
||||||
|
case containsAny(baseURL, "weknora.weixin.qq.com"):
|
||||||
|
return ProviderWeKnoraCloud
|
||||||
default:
|
default:
|
||||||
return ProviderGeneric
|
return ProviderGeneric
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,16 +19,18 @@ func (p *WeKnoraCloudProvider) Info() ProviderInfo {
|
|||||||
return ProviderInfo{
|
return ProviderInfo{
|
||||||
Name: ProviderWeKnoraCloud,
|
Name: ProviderWeKnoraCloud,
|
||||||
DisplayName: "WeKnoraCloud",
|
DisplayName: "WeKnoraCloud",
|
||||||
Description: "WeKnoraCloud 平台,提供 Chat / Embedding / Rerank 模型,使用 APPID + API Key 签名认证",
|
Description: "WeKnora云服务,模型:chat, embedding, rerank, vlm",
|
||||||
DefaultURLs: map[types.ModelType]string{
|
DefaultURLs: map[types.ModelType]string{
|
||||||
types.ModelTypeKnowledgeQA: WeKnoraCloudBaseURL,
|
types.ModelTypeKnowledgeQA: WeKnoraCloudBaseURL,
|
||||||
types.ModelTypeEmbedding: WeKnoraCloudBaseURL,
|
types.ModelTypeEmbedding: WeKnoraCloudBaseURL,
|
||||||
types.ModelTypeRerank: WeKnoraCloudBaseURL,
|
types.ModelTypeRerank: WeKnoraCloudBaseURL,
|
||||||
|
types.ModelTypeVLLM: WeKnoraCloudBaseURL,
|
||||||
},
|
},
|
||||||
ModelTypes: []types.ModelType{
|
ModelTypes: []types.ModelType{
|
||||||
types.ModelTypeKnowledgeQA,
|
types.ModelTypeKnowledgeQA,
|
||||||
types.ModelTypeEmbedding,
|
types.ModelTypeEmbedding,
|
||||||
types.ModelTypeRerank,
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
},
|
},
|
||||||
RequiresAuth: true,
|
RequiresAuth: true,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -831,6 +831,6 @@ func RegisterDataSourceRoutes(r *gin.RouterGroup, handler *handler.DataSourceHan
|
|||||||
|
|
||||||
// RegisterWeKnoraCloudRoutes 注册 WeKnoraCloud 初始化路由
|
// RegisterWeKnoraCloudRoutes 注册 WeKnoraCloud 初始化路由
|
||||||
func RegisterWeKnoraCloudRoutes(r *gin.RouterGroup, handler *handler.WeKnoraCloudHandler) {
|
func RegisterWeKnoraCloudRoutes(r *gin.RouterGroup, handler *handler.WeKnoraCloudHandler) {
|
||||||
r.POST("/models/weknoracloud/initialize", handler.Initialize)
|
r.POST("/weknoracloud/credentials", handler.SaveCredentials)
|
||||||
r.GET("/models/weknoracloud/status", handler.Status)
|
r.GET("/models/weknoracloud/status", handler.Status)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WeKnoraCloudService 处理 WeKnoraCloud 厂商的初始化逻辑
|
// WeKnoraCloudService 处理 WeKnoraCloud 凭证管理
|
||||||
type WeKnoraCloudService interface {
|
type WeKnoraCloudService interface {
|
||||||
Initialize(ctx context.Context, appID, appSecret string) (*types.InitializeResult, error)
|
// SaveCredentials 仅保存 APPID/APPSECRET 凭证到租户配置,不自动创建模型
|
||||||
|
SaveCredentials(ctx context.Context, appID, appSecret string) error
|
||||||
// CheckStatus 检查当前租户的 WeKnoraCloud 凭证是否可正常解密
|
// CheckStatus 检查当前租户的 WeKnoraCloud 凭证是否可正常解密
|
||||||
// needsReinit=true 表示加密状态已损坏(salt 变更等),需要用户重新填写凭证
|
// needsReinit=true 表示加密状态已损坏(salt 变更等),需要用户重新填写凭证
|
||||||
CheckStatus(ctx context.Context) (*types.WeKnoraCloudStatusResult, error)
|
CheckStatus(ctx context.Context) (*types.WeKnoraCloudStatusResult, error)
|
||||||
|
|||||||
@@ -233,7 +233,6 @@ func (c *ConversationConfig) Scan(value interface{}) error {
|
|||||||
// ParserEngineConfig holds tenant-level overrides for document parser engines (e.g. MinerU endpoint, API key).
|
// ParserEngineConfig holds tenant-level overrides for document parser engines (e.g. MinerU endpoint, API key).
|
||||||
// These values take precedence over environment variables when parsing documents.
|
// These values take precedence over environment variables when parsing documents.
|
||||||
type ParserEngineConfig struct {
|
type ParserEngineConfig struct {
|
||||||
DocReaderAddr string `json:"docreader_addr"` // 文档解析服务地址
|
|
||||||
// docreader 凭证
|
// docreader 凭证
|
||||||
DocreaderAppID string `json:"docreader_app_id,omitempty"`
|
DocreaderAppID string `json:"docreader_app_id,omitempty"`
|
||||||
DocreaderAPIKey string `json:"docreader_api_key,omitempty"`
|
DocreaderAPIKey string `json:"docreader_api_key,omitempty"`
|
||||||
@@ -263,8 +262,8 @@ func (c *ParserEngineConfig) ToOverridesMap() map[string]string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
if c.DocReaderAddr != "" {
|
if c.DocreaderAppID != "" {
|
||||||
m["docreader_addr"] = c.DocReaderAddr
|
m["docreader_app_id"] = c.DocreaderAppID
|
||||||
}
|
}
|
||||||
if c.MinerUEndpoint != "" {
|
if c.MinerUEndpoint != "" {
|
||||||
m["mineru_endpoint"] = c.MinerUEndpoint
|
m["mineru_endpoint"] = c.MinerUEndpoint
|
||||||
|
|||||||
@@ -2,23 +2,11 @@ package types
|
|||||||
|
|
||||||
// WeKnoraCloudStatusResult 状态检查结果
|
// WeKnoraCloudStatusResult 状态检查结果
|
||||||
type WeKnoraCloudStatusResult struct {
|
type WeKnoraCloudStatusResult struct {
|
||||||
HasModels bool `json:"has_models"` // 是否已配置 WeKnoraCloud 模型
|
HasModels bool `json:"has_models"` // 是否已配置 WeKnoraCloud 凭证
|
||||||
NeedsReinit bool `json:"needs_reinit"` // 是否需要重新初始化(凭证损坏)
|
NeedsReinit bool `json:"needs_reinit"` // 是否需要重新初始化(凭证损坏)
|
||||||
Reason string `json:"reason,omitempty"` // 需要重新初始化的原因
|
Reason string `json:"reason,omitempty"` // 需要重新初始化的原因
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitializeResult 返回每个模型的操作结果
|
|
||||||
type InitializeResult struct {
|
|
||||||
Models []ModelAction `json:"models"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModelAction 单个模型的操作结果
|
|
||||||
type ModelAction struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Action string `json:"action"` // "created" | "updated"
|
|
||||||
}
|
|
||||||
|
|
||||||
type DocreaderCredentials struct {
|
type DocreaderCredentials struct {
|
||||||
AppID string
|
AppID string
|
||||||
APIKey string
|
APIKey string
|
||||||
|
|||||||
Reference in New Issue
Block a user