feat(faq): enhance FAQ handling and localization support

- Updated the agent system prompt to clarify mandatory deep read requirements for FAQ entries, ensuring accurate retrieval and response generation.
- Added new localization strings for FAQ-related labels in English and Chinese, improving user experience and consistency across the application.
- Refactored the document and grep results components to support distinct FAQ identification, enhancing clarity in the output.
- Introduced new tests for FAQ metadata handling, ensuring robust functionality and correctness in the FAQ processing logic.
This commit is contained in:
wizardchen
2026-06-04 20:17:43 +08:00
committed by lyingbug
parent ba8c96d83a
commit 4bb8906520
14 changed files with 1075 additions and 225 deletions
+5
View File
@@ -2701,6 +2701,9 @@ export default {
referenceIconAlt: 'Reference materials icon',
chunkIdLabel: 'Chunk ID:',
documentIdLabel: 'Document ID:',
faqIdLabel: 'FAQ ID:',
faqContainerIdLabel: 'Container ID:',
faqAnswersLabel: 'Answers:',
chunkOrdinal: 'Chunk {index}',
previewContent: 'Preview content',
noPlanSteps: 'No detailed steps provided',
@@ -4570,6 +4573,7 @@ export default {
getDocument: 'Get document: {title}',
document: 'Document',
listChunks: 'View {title}',
listFaqEntry: 'View FAQ: {question}',
deepThinking: 'Deep Thinking',
},
plan: {
@@ -4588,6 +4592,7 @@ export default {
chunkHits: '{count} chunks',
keywordHits: '{count} hits',
titleMatch: 'title',
faqEntry: 'FAQ entry',
},
toolStatus: {
calling: 'Calling {name}...',
+5
View File
@@ -806,6 +806,9 @@ export default {
referenceIconAlt: "参考内容图标",
chunkIdLabel: "片段ID:",
documentIdLabel: "文档ID:",
faqIdLabel: "FAQ ID:",
faqContainerIdLabel: "所属文档ID:",
faqAnswersLabel: "答案:",
chunkOrdinal: "片段 {index}",
previewContent: "预览内容",
noPlanSteps: "未提供具体步骤",
@@ -4564,6 +4567,7 @@ export default {
getDocument: "获取文档:{title}",
document: "文档",
listChunks: "查看 {title}",
listFaqEntry: "查看 FAQ:{question}",
deepThinking: "深度思考",
},
plan: {
@@ -4582,6 +4586,7 @@ export default {
chunkHits: "{count} 片段",
keywordHits: "{count} 次",
titleMatch: "标题匹配",
faqEntry: "FAQ 条目",
},
toolStatus: {
calling: "正在调用 {name}...",
+25 -4
View File
@@ -105,8 +105,14 @@ export interface KnowledgeBaseListData {
// Document info data
export interface DocumentInfoDocument {
knowledge_id: string;
knowledge_id?: string;
faq_id?: string;
chunk_id?: string;
title: string;
faq_question?: string;
faq_answers?: string[];
faq_similar_questions?: string[];
is_faq?: boolean;
description?: string;
type?: string;
source?: string;
@@ -204,13 +210,11 @@ export interface WebFetchResultsData {
count?: number;
}
// Grep knowledge aggregation item
// Grep knowledge aggregation item (legacy, grouped by knowledge_id)
export interface GrepKnowledgeResult {
knowledge_id: string;
knowledge_base_id: string;
knowledge_title: string;
// Standard question of the first matched FAQ entry, used as the row label
// since FAQ entries share the owning document's title.
faq_question?: string;
title_match?: boolean;
chunk_hit_count: number;
@@ -220,11 +224,28 @@ export interface GrepKnowledgeResult {
distinct_patterns: number;
}
// Per-chunk grep hit (preferred for UI — one row per FAQ entry or chunk)
export interface GrepChunkResult {
chunk_id: string;
faq_id?: string;
knowledge_id: string;
knowledge_base_id: string;
knowledge_title: string;
chunk_type?: string;
index?: number;
chunk_index?: number;
faq_question?: string;
title_match?: boolean;
match_snippet?: string;
score?: number;
}
// Grep results data
export interface GrepResultsData {
display_type: 'grep_results';
query?: string;
patterns: string[];
chunk_results?: GrepChunkResult[];
knowledge_results: GrepKnowledgeResult[];
result_count: number;
total_matches: number;
@@ -2080,6 +2080,9 @@ const getToolSummary = (event: any): string => {
return t('agentStream.toolSummary.getDocument', { title: toolData.title });
}
} else if (toolName === 'list_knowledge_chunks') {
if (toolData?.faq_question) {
return t('agentStream.toolSummary.listFaqEntry', { question: toolData.faq_question });
}
if (toolData?.fetched_chunks !== undefined) {
const title = toolData?.knowledge_title || toolData?.knowledge_id || t('agentStream.toolSummary.document');
return t('agentStream.toolSummary.listChunks', { title, fetched: toolData.fetched_chunks, total: toolData.total_chunks ?? '?' });
@@ -2238,7 +2241,8 @@ const getGrepResultsSummary = (toolData: any): string => {
if (!toolData) return '';
const totalChunks = toolData.total_matches || 0;
const docCount = toolData.result_count || 0;
const rowCount =
toolData.chunk_results?.length ?? toolData.result_count ?? toolData.knowledge_results?.length ?? 0;
if (totalChunks === 0) {
return t('agentStream.search.noResults');
@@ -2246,7 +2250,7 @@ const getGrepResultsSummary = (toolData: any): string => {
return t('agentStream.search.grepSummary', {
chunks: `<strong>${totalChunks}</strong>`,
docs: `<strong>${docCount}</strong>`,
docs: `<strong>${rowCount}</strong>`,
});
};
@@ -4,7 +4,7 @@
<div v-if="documents.length" class="documents-list">
<div
v-for="(doc, index) in documents"
:key="doc.knowledge_id || index"
:key="doc.faq_id || doc.knowledge_id || index"
class="result-card document-card"
>
<div class="result-header document-header">
@@ -20,10 +20,23 @@
</div>
<div class="result-content expanded">
<div class="info-section">
<div class="info-field">
<span class="field-label">{{ $t('chat.documentIdLabel') }}</span>
<div class="info-field" v-if="doc.is_faq && doc.faq_id">
<span class="field-label">{{ $t('chat.faqIdLabel') }}</span>
<span class="field-value"><code>{{ doc.faq_id }}</code></span>
</div>
<div class="info-field" v-if="doc.knowledge_id">
<span class="field-label">{{ doc.is_faq ? $t('chat.faqContainerIdLabel') : $t('chat.documentIdLabel') }}</span>
<span class="field-value"><code>{{ doc.knowledge_id }}</code></span>
</div>
<div
v-if="doc.is_faq && doc.faq_answers?.length"
class="info-field info-field--block"
>
<span class="field-label">{{ $t('chat.faqAnswersLabel') }}</span>
<ul class="faq-answers-list">
<li v-for="(ans, aIdx) in doc.faq_answers" :key="aIdx">{{ ans }}</li>
</ul>
</div>
<div class="info-field" v-if="doc.description">
<span class="field-label">{{ $t('chat.documentDescriptionLabel') }}</span>
<span class="field-value">{{ doc.description }}</span>
@@ -205,6 +218,21 @@ const formatMetadataValue = (value: unknown) => {
}
}
.faq-answers-list {
list-style: none;
margin: 4px 0 0;
padding: 0;
display: flex;
flex-direction: column;
gap: 4px;
li {
font-size: 12px;
line-height: 1.5;
color: var(--td-text-color-primary);
}
}
.info-field {
display: flex;
gap: 10px;
@@ -212,6 +240,11 @@ const formatMetadataValue = (value: unknown) => {
font-size: 12px;
line-height: 1.5;
&--block {
flex-direction: column;
gap: 4px;
}
.field-label {
color: var(--td-text-color-secondary);
min-width: 90px;
@@ -1,16 +1,17 @@
<template>
<div class="grep-results">
<div v-if="results.length" class="results-list">
<div v-if="rows.length" class="results-list">
<ResultRow
v-for="(result, index) in results"
:key="result.knowledge_id"
v-for="(result, index) in rows"
:key="result.key"
:index="index + 1"
:title="result.faq_question || result.knowledge_title || $t('knowledge.untitledDocument')"
:meta="formatMeta(result)"
:popup-key="result.knowledge_id || index"
:show-popup="!!cleanedSnippet(result)"
:content="result.match_snippet"
:knowledge-id="result.knowledge_id"
:title="result.title"
:meta="result.meta"
:popup-key="result.key"
:show-popup="!!result.snippet"
:content="result.snippet"
:chunk-id="result.chunkId"
:knowledge-id="result.knowledgeId"
:highlight="searchPattern"
:regex="true"
/>
@@ -27,7 +28,7 @@ import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { cleanSnippet } from './contentClean';
import ResultRow from './ResultRow.vue';
import type { GrepKnowledgeResult, GrepResultsData } from '@/types/tool-results';
import type { GrepChunkResult, GrepKnowledgeResult, GrepResultsData } from '@/types/tool-results';
const props = defineProps<{
data: GrepResultsData;
@@ -35,10 +36,18 @@ const props = defineProps<{
const { t } = useI18n();
const results = computed(() => props.data.knowledge_results ?? []);
const searchPattern = computed(() => props.data.query ?? props.data.patterns?.[0] ?? '');
const formatMeta = (result: GrepKnowledgeResult): string => {
type GrepRow = {
key: string;
title: string;
meta: string;
snippet: string;
chunkId?: string;
knowledgeId?: string;
};
const formatKnowledgeMeta = (result: GrepKnowledgeResult): string => {
const parts: string[] = [];
const chunks = result.chunk_hit_count ?? 0;
if (chunks > 0) {
@@ -54,8 +63,46 @@ const formatMeta = (result: GrepKnowledgeResult): string => {
return parts.join(' · ');
};
const cleanedSnippet = (result: GrepKnowledgeResult): string =>
cleanSnippet(result.match_snippet ?? '');
const rowFromChunk = (result: GrepChunkResult): GrepRow => {
const isFAQ = !!result.faq_id || result.chunk_type === 'faq';
const title = result.faq_question || result.knowledge_title || t('knowledge.untitledDocument');
const meta = isFAQ
? t('agentStream.grepResults.faqEntry')
: formatKnowledgeMeta({
knowledge_id: result.knowledge_id,
knowledge_base_id: result.knowledge_base_id,
knowledge_title: result.knowledge_title,
chunk_hit_count: 1,
total_pattern_hits: 1,
distinct_patterns: 1,
pattern_counts: {},
title_match: !!result.title_match,
});
return {
key: result.faq_id || result.chunk_id || String(result.index ?? '') || result.knowledge_id,
title,
meta,
snippet: cleanSnippet(result.match_snippet ?? ''),
chunkId: result.faq_id || result.chunk_id,
knowledgeId: result.knowledge_id,
};
};
const rowFromKnowledge = (result: GrepKnowledgeResult): GrepRow => ({
key: result.knowledge_id,
title: result.faq_question || result.knowledge_title || t('knowledge.untitledDocument'),
meta: formatKnowledgeMeta(result),
snippet: cleanSnippet(result.match_snippet ?? ''),
knowledgeId: result.knowledge_id,
});
const rows = computed((): GrepRow[] => {
const chunkRows = props.data.chunk_results;
if (chunkRows?.length) {
return chunkRows.map(rowFromChunk);
}
return (props.data.knowledge_results ?? []).map(rowFromKnowledge);
});
</script>
<style lang="less" scoped>