feat(knowledge): add short-term ask memory

This commit is contained in:
2026-07-18 21:22:49 +08:00
parent 2eda757483
commit 064816a784
33 changed files with 1348 additions and 99 deletions
+219 -6
View File
@@ -3,7 +3,10 @@
<view class="chat-head">
<view class="chat-head-row">
<text class="chat-title">问 · 数字师傅</text>
<text class="chat-context">{{ positionLabel }} · 住宅项目</text>
<view class="chat-head-actions">
<text class="chat-context">{{ positionLabel }} · 住宅项目</text>
<button class="new-chat-button" :disabled="loading" @click="startNewConversation">新对话</button>
</view>
</view>
<view class="source-switch chat-source-switch" aria-label="问答来源">
<button class="source-option active" aria-current="page">企业问师傅</button>
@@ -59,6 +62,10 @@
</view>
<view v-if="msg.voiceStatus === 'unavailable' || msg.transcriptOpen" class="bubble master transcript-bubble">
<text
v-if="msg.result.rewrittenQuery && msg.result.rewrittenQuery !== msg.result.queryText"
class="context-used"
>已结合本次对话理解</text>
<text class="bubble-text">{{ msg.result.answer || '未生成直接答案,请查看引用依据。' }}</text>
<view v-if="msg.result.keyPoints?.length" class="bubble-keypoints">
<text v-for="(point, index) in msg.result.keyPoints" :key="point" class="bubble-keypoint">
@@ -116,6 +123,32 @@
<text class="transcript-collapse-text">收起全文 ▲</text>
</view>
</view>
<view v-if="msg.result.resources?.length" class="resource-list">
<view
v-for="resource in msg.result.resources"
:key="resource.attachmentId"
class="resource-card"
:class="{ video: resource.type === 'VIDEO' }"
@click="openKnowledgeResource(resource)"
>
<view class="resource-icon">{{ resource.type === 'VIDEO' ? '▶' : '文' }}</view>
<view class="resource-copy">
<text class="resource-title">{{ resource.title }}</text>
<text class="resource-meta">
{{ resource.loading ? '正在安全读取…' : resource.type === 'VIDEO' ? (resource.localUrl ? '已加载,可直接播放' : '点击加载操作视频') : '点击查看或下载原文件' }}
</text>
</view>
<text class="resource-arrow">›</text>
<video
v-if="resource.type === 'VIDEO' && resource.localUrl"
class="resource-video"
:src="resource.localUrl"
controls
@click.stop
/>
</view>
</view>
</view>
</view>
</template>
@@ -237,7 +270,9 @@ import { nextTick, ref } from 'vue';
import { onHide, onShow, onUnload } from '@dcloudio/uni-app';
import type {
AnswerFeedbackVerdict,
KnowledgeConversationState,
KnowledgeMediaSelectedFile,
KnowledgeResource,
KnowledgeSearchResponse,
SpeechPlaybackStatus,
SummaryCardResponse
@@ -260,6 +295,7 @@ import {
roleKey
} from '@/services/auth';
import { resetPageScroll } from '@/services/navigation';
import { authenticatedFileUrl, releaseAuthenticatedFileUrl } from '@/services/api';
import { ensureEmployeePosition } from '@/services/position';
import { chooseSpeechAudio, createSpeechPlaybackController, measureAudioDuration, MENTOR_VOICE_PROFILE, transcribeSpeechBlob, transcribeSpeechFile } from '@/services/speech';
import { downloadSummaryCardImage } from '@/services/summary-card-image';
@@ -323,6 +359,14 @@ const pendingSopQueryKey = 'aihr_sop_pending_query';
const pendingToolKey = 'aihr_sop_pending_tool';
const currentRole = ref(String(uni.getStorageSync(roleKey) || ''));
const supervisorLearner = ref(isSupervisorLearnerMode());
const createConversationId = () => {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
return `chat_${Date.now()}_${Math.random().toString(36).slice(2, 12)}`;
};
const conversationId = ref(createConversationId());
const contextVersion = ref(0);
let msgSeq = 0;
let voiceTranscriptionGeneration = 0;
let abortVoiceTranscription: (() => void) | null = null;
@@ -363,6 +407,41 @@ const replaceMsg = (id: number, next: ChatMsg | null) => {
const pushSystem = (text: string) => pushMsg<SystemMsg>({ role: 'system', text });
const conversationState = (): KnowledgeConversationState => ({
conversationId: conversationId.value,
contextVersion: contextVersion.value
});
const applyConversationState = (result: KnowledgeSearchResponse) => {
if (result.conversationId === conversationId.value && typeof result.contextVersion === 'number') {
contextVersion.value = result.contextVersion;
}
};
const releaseMessageResources = () => {
messages.value.forEach((msg) => {
if (msg.role !== 'master') return;
(msg.result.resources || []).forEach((resource) => {
if (resource.localUrl) releaseAuthenticatedFileUrl(resource.localUrl);
resource.localUrl = undefined;
});
});
};
const resetConversation = (notify: boolean) => {
cancelPendingMediaSearch();
releaseMessageResources();
speechPlayback.stop();
messages.value = [];
summaryCard.value = null;
selectedMedia.value = null;
conversationId.value = createConversationId();
contextVersion.value = 0;
if (notify) pushSystem('已开始新对话,后续问题将从新的上下文理解');
};
const startNewConversation = () => resetConversation(true);
const refreshAuthState = () => {
loggedIn.value = isLoggedIn();
positionLabel.value = getSelectedPosition();
@@ -420,9 +499,10 @@ const sendQuestion = async (rawText: string, options: { voice?: boolean } = {})
const nextResult = media
? await searchKnowledgeWithMedia(media, text, getSelectedPosition(), (task) => {
if (generation === knowledgeSearchGeneration) activeMediaUploadTask = task;
})
: await searchKnowledge(text);
}, conversationState())
: await searchKnowledge(text, getSelectedPosition(), conversationState());
if (generation !== knowledgeSearchGeneration) return;
applyConversationState(nextResult);
const spoken = (nextResult.answer || '').trim();
replaceMsg(pending.id, {
id: pending.id,
@@ -480,8 +560,9 @@ const runDataTool = async (toolCode: 'MY_PRACTICE_SUMMARY' | 'TEAM_PRACTICE_SUMM
loading.value = true;
const generation = ++knowledgeSearchGeneration;
try {
const nextResult = await queryKnowledgeTool(effectiveToolCode, question);
const nextResult = await queryKnowledgeTool(effectiveToolCode, question, conversationState());
if (generation !== knowledgeSearchGeneration) return;
applyConversationState(nextResult);
replaceMsg(pending.id, {
id: pending.id,
role: 'master',
@@ -626,6 +707,36 @@ const playVoice = (msg: MasterMsg) => {
void speechPlayback.toggle(`answer-${msg.id}`, spokenTextOf(msg));
};
const openKnowledgeResource = async (resource: KnowledgeResource) => {
if (resource.loading) return;
if (resource.type === 'VIDEO' && resource.localUrl) return;
resource.loading = true;
try {
const localUrl = await authenticatedFileUrl(resource.contentUrl);
if (resource.localUrl && resource.localUrl !== localUrl) {
releaseAuthenticatedFileUrl(resource.localUrl);
}
resource.localUrl = localUrl;
if (resource.type === 'FILE') {
if (typeof document !== 'undefined') {
const link = document.createElement('a');
link.href = localUrl;
link.download = resource.title || '资料文件';
link.target = '_blank';
document.body.appendChild(link);
link.click();
link.remove();
} else {
uni.openDocument({ filePath: localUrl, showMenu: true });
}
}
} catch (error) {
uni.showToast({ title: requestErrorText(error, '资料读取失败'), icon: 'none' });
} finally {
resource.loading = false;
}
};
const snippetExcerpt = (text = '') => {
const value = text.trim();
return value.length > 260 ? `${value.slice(0, 260)}...` : value;
@@ -750,6 +861,9 @@ const openWebAi = () => {
const handleShow = async () => {
resetPageScroll();
refreshAuthState();
if (!loggedIn.value && messages.value.length) {
resetConversation(false);
}
if (loggedIn.value && !(await ensureEmployeePosition('/pages/user/sop/index'))) {
return;
}
@@ -784,6 +898,7 @@ onHide(() => {
});
onUnload(() => {
releaseMessageResources();
speechPlayback.destroy();
composerRef.value?.teardown();
cancelVoiceTranscription();
@@ -1873,6 +1988,23 @@ onUnload(() => {
font-size: 12px;
}
.chat-head-actions {
display: flex;
align-items: center;
gap: 8px;
}
.new-chat-button {
margin: 0;
padding: 3px 9px;
border: 1px solid #e3e6ea;
border-radius: 999px;
background: #ffffff;
color: #5a6270;
font-size: 12px;
line-height: 1.5;
}
.chat-source-switch {
display: flex;
padding: 3px;
@@ -1971,7 +2103,7 @@ onUnload(() => {
border-radius: 6px;
background: rgba(200, 0, 26, 0.08);
color: #a04a00;
font-size: 11px;
font-size: 12px;
}
.bubble.user .bubble-tag {
@@ -2013,6 +2145,13 @@ onUnload(() => {
font-size: 13px;
}
.context-used {
display: block;
margin-bottom: 6px;
color: #74839a;
font-size: 12px;
}
.bubble-actions {
display: flex;
flex-wrap: wrap;
@@ -2062,6 +2201,80 @@ onUnload(() => {
border-top: 1px dashed #eceff2;
}
.resource-list {
display: flex;
flex-direction: column;
gap: 8px;
width: min(280px, 100%);
}
.resource-card {
display: grid;
grid-template-columns: 38px minmax(0, 1fr) 16px;
gap: 9px;
align-items: center;
width: 100%;
padding: 10px;
border: 1px solid #e3e7ed;
border-radius: 12px;
background: #ffffff;
box-shadow: 0 1px 3px rgba(20, 24, 31, 0.06);
}
.resource-card.video {
border-color: #f1cbd0;
}
.resource-icon {
display: flex;
align-items: center;
justify-content: center;
width: 38px;
height: 38px;
border-radius: 9px;
background: #fff0f2;
color: #c8001a;
font-size: 14px;
font-weight: 800;
}
.resource-copy {
min-width: 0;
}
.resource-title,
.resource-meta {
display: block;
}
.resource-title {
color: #232a33;
font-size: 13px;
font-weight: 700;
line-height: 1.4;
word-break: break-word;
}
.resource-meta {
margin-top: 3px;
color: #8a93a0;
font-size: 12px;
}
.resource-arrow {
color: #a4abb5;
font-size: 22px;
}
.resource-video {
grid-column: 1 / -1;
width: 100%;
height: 156px;
margin-top: 2px;
border-radius: 8px;
background: #111111;
}
.citation {
padding: 8px 10px;
border-left: 3px solid #f0c0c5;
@@ -2080,7 +2293,7 @@ onUnload(() => {
display: block;
margin-top: 2px;
color: #98a1ad;
font-size: 11px;
font-size: 12px;
}
.citation-content {