Files
ai_academy/internal/agent/tools/faq_snippet.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

338 lines
9.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tools
import (
"fmt"
"regexp"
"strings"
"academy/internal/searchutil"
"academy/internal/types"
)
// faqMaxSimilarQuestionsDisplay caps similar_question rows in tool output.
const faqMaxSimilarQuestionsDisplay = 5
func truncateSimilarQuestionsForDisplay(questions []string) (display []string, omitted int) {
if len(questions) == 0 {
return nil, 0
}
if len(questions) <= faqMaxSimilarQuestionsDisplay {
return questions, 0
}
return questions[:faqMaxSimilarQuestionsDisplay], len(questions) - faqMaxSimilarQuestionsDisplay
}
func writeSimilarQuestionsXML(b *strings.Builder, questions []string) {
display, omitted := truncateSimilarQuestionsForDisplay(questions)
for _, sq := range display {
fmt.Fprintf(b, "<similar_question>%s</similar_question>\n", xmlEscape(sq))
}
if omitted > 0 {
fmt.Fprintf(b, "<similar_questions_omitted count=\"%d\" />\n", omitted)
}
}
func appendSimilarQuestionsToChunkData(chunkData map[string]interface{}, questions []string) {
display, omitted := truncateSimilarQuestionsForDisplay(questions)
if len(display) == 0 {
return
}
chunkData["faq_similar_questions"] = display
if omitted > 0 {
chunkData["faq_similar_questions_omitted"] = omitted
}
}
// writeFAQFieldsXML emits question / similar_question / answer children (no wrapper).
func writeFAQFieldsXML(b *strings.Builder, meta *types.FAQChunkMetadata) {
if meta == nil {
return
}
if meta.StandardQuestion != "" {
fmt.Fprintf(b, "<question>%s</question>\n", xmlEscape(meta.StandardQuestion))
}
writeSimilarQuestionsXML(b, meta.SimilarQuestions)
for _, ans := range meta.Answers {
if strings.TrimSpace(ans) == "" {
continue
}
fmt.Fprintf(b, "<answer>%s</answer>\n", xmlEscape(ans))
}
}
func faqFieldsEmpty(meta *types.FAQChunkMetadata) bool {
if meta == nil {
return true
}
return meta.StandardQuestion == "" && len(meta.SimilarQuestions) == 0 && len(meta.Answers) == 0
}
// writeFAQMetadataXML emits a nested <faq> block (used inside search_knowledge <chunk>).
func writeFAQMetadataXML(b *strings.Builder, meta *types.FAQChunkMetadata) {
if faqFieldsEmpty(meta) {
return
}
b.WriteString("<faq>\n")
writeFAQFieldsXML(b, meta)
b.WriteString("</faq>\n")
}
// writeFAQEntryXML emits a top-level FAQ entry for read_document (not wrapped in <chunk>).
func writeFAQEntryXML(b *strings.Builder, c *types.Chunk) {
if c == nil || c.ChunkType != types.ChunkTypeFAQ {
return
}
meta, err := c.FAQMetadata()
if err != nil {
meta = nil
}
q := faqStandardQuestion(c)
questionAttr := ""
if q != "" {
questionAttr = fmt.Sprintf(" question=\"%s\"", xmlEscape(q))
}
fmt.Fprintf(b, "<faq faq_id=\"%s\" index=\"%d\"%s>\n",
xmlEscape(c.ID),
c.ChunkIndex,
questionAttr,
)
if !faqFieldsEmpty(meta) {
writeFAQFieldsXML(b, meta)
} else if q != "" {
fmt.Fprintf(b, "<question>%s</question>\n", xmlEscape(q))
}
b.WriteString("</faq>\n")
}
// normalizeFAQChunkDataMap uses faq_id / index instead of chunk_id / chunk_index in JSON payloads.
func normalizeFAQChunkDataMap(chunkData map[string]interface{}, c *types.Chunk) {
if c == nil || c.ChunkType != types.ChunkTypeFAQ || chunkData == nil {
return
}
chunkData["faq_id"] = c.ID
chunkData["index"] = c.ChunkIndex
delete(chunkData, "chunk_id")
delete(chunkData, "chunk_index")
}
// appendFAQChunkData adds FAQ metadata fields to structured tool result maps.
func appendFAQChunkData(chunkData map[string]interface{}, c *types.Chunk) {
if c == nil || c.ChunkType != types.ChunkTypeFAQ {
return
}
meta, err := c.FAQMetadata()
if err != nil || meta == nil {
return
}
if q := strings.TrimSpace(meta.StandardQuestion); q != "" {
chunkData["faq_question"] = q
}
appendSimilarQuestionsToChunkData(chunkData, meta.SimilarQuestions)
if len(meta.Answers) > 0 {
chunkData["faq_answers"] = meta.Answers
}
}
// Bounds for retrieval-tool match snippets (search_knowledge, read_document).
const (
snippetContextRunes = 200
snippetMaxMatchRunes = 200
snippetMaxTotalRunes = 800
snippetMaxAnswerRunes = 600
)
// faqMatchSnippetFromQueries builds "Q: … | A: …" for search_knowledge hits.
func faqMatchSnippetFromQueries(meta *types.FAQChunkMetadata, queries []string) string {
if meta == nil {
return ""
}
question := faqMatchedQuestionFromQueries(meta, queries)
if question == "" {
question = strings.TrimSpace(meta.StandardQuestion)
}
return formatFAQMatchSnippet(question, meta.Answers)
}
// faqMatchSnippet builds "Q: … | A: …" for read_document regex hits.
func faqMatchSnippet(chunk *types.Chunk, compiled []*regexp.Regexp) string {
if chunk == nil {
return ""
}
meta, err := chunk.FAQMetadata()
if err != nil || meta == nil {
return ""
}
question := faqMatchedQuestionFromRegex(meta, compiled)
if question == "" {
question = strings.TrimSpace(meta.StandardQuestion)
}
if question == "" {
return ""
}
return formatFAQMatchSnippet(question, meta.Answers)
}
func formatFAQMatchSnippet(question string, answers []string) string {
question = strings.TrimSpace(question)
if question == "" {
return ""
}
var b strings.Builder
b.WriteString("Q: ")
b.WriteString(question)
if answer := faqAnswersForSnippet(answers); answer != "" {
b.WriteString(" | A: ")
b.WriteString(answer)
}
snippet := strings.TrimSpace(b.String())
if len([]rune(snippet)) > snippetMaxTotalRunes {
snippet = truncateRunes(snippet, snippetMaxTotalRunes)
}
return snippet
}
func faqMatchedQuestionFromRegex(meta *types.FAQChunkMetadata, compiled []*regexp.Regexp) string {
if meta == nil {
return ""
}
for _, sq := range meta.SimilarQuestions {
if regexMatchesAny(sq, compiled) {
return sq
}
}
if regexMatchesAny(meta.StandardQuestion, compiled) {
return meta.StandardQuestion
}
return meta.StandardQuestion
}
// faqMatchedQuestionFromQueries picks the question the queries match best:
// one containing a whole query beats term matches, and more matched terms
// beat fewer. Ties go to similar questions in order, then the standard one.
// With word-level terms (Chinese queries are segmented) "the first question
// sharing any term" picked a loosely related similar question.
func faqMatchedQuestionFromQueries(meta *types.FAQChunkMetadata, queries []string) string {
if meta == nil {
return ""
}
tokens := searchQueryTokens(queries)
best, bestScore := meta.StandardQuestion, 0
candidates := append(append([]string(nil), meta.SimilarQuestions...), meta.StandardQuestion)
for _, q := range candidates {
if score := searchQueryMatchScore(q, queries, tokens); score > bestScore {
best, bestScore = q, score
}
}
return best
}
// searchQueryMatchScore rates how well text matches the queries: a whole
// query contained in text counts more than any number of single terms.
func searchQueryMatchScore(text string, queries []string, tokens []string) int {
if text == "" {
return 0
}
lowered := strings.ToLower(text)
score := 0
for _, q := range queries {
q = strings.ToLower(strings.TrimSpace(q))
if q != "" && strings.Contains(lowered, q) {
score += 1000
}
}
for _, tok := range tokens {
if strings.Contains(lowered, tok) {
score++
}
}
return score
}
func faqAnswersForSnippet(answers []string) string {
if len(answers) == 0 {
return ""
}
parts := make([]string, 0, len(answers))
for _, a := range answers {
a = strings.TrimSpace(a)
if a != "" {
parts = append(parts, a)
}
}
if len(parts) == 0 {
return ""
}
return truncateRunes(strings.Join(parts, " | "), snippetMaxAnswerRunes)
}
func searchQueryTokens(queries []string) []string {
tokens := make([]string, 0, 8)
seen := make(map[string]struct{})
for _, q := range queries {
for _, tok := range queryTerms(q) {
tok = strings.ToLower(tok)
if _, ok := seen[tok]; ok {
continue
}
seen[tok] = struct{}{}
tokens = append(tokens, tok)
}
}
return tokens
}
// queryTerms splits a query into candidate terms, keeping their case: words
// split on whitespace and punctuation, Chinese fields segmented into words,
// dropping single characters and stopwords. Terms may repeat.
func queryTerms(query string) []string {
var terms []string
add := func(word string) {
word = strings.TrimSpace(word)
if len([]rune(word)) < 2 {
return
}
if _, stop := snippetStopwords[strings.ToLower(word)]; stop {
return
}
terms = append(terms, word)
}
for _, field := range strings.FieldsFunc(query, isSnippetSeparator) {
// A Chinese question has no spaces, so the whole question was one
// token that never occurred in any chunk and every snippet fell
// back to the chunk's opening. Segment it into words instead.
if searchutil.ContainsChinese(field) {
for _, word := range types.Jieba.CutForSearch(field, true) {
add(word)
}
continue
}
add(field)
}
return terms
}
// isSnippetSeparator splits a query into candidate terms on whitespace and
// ASCII or full-width punctuation.
func isSnippetSeparator(r rune) bool {
switch r {
case ' ', '\t', '\n', '\r', ',', '.', ';', ':', '?', '!',
'(', ')', '[', ']', '{', '}', '"', '\'',
',', '。', ';', ':', '?', '!', '、', '(', ')', '【', '】', '“', '”', '‘', '’':
return true
}
return false
}
// snippetStopwords are function words that occur in almost every chunk; as
// the earliest match they placed the snippet at an arbitrary position.
var snippetStopwords = map[string]struct{}{
"the": {}, "and": {}, "or": {}, "of": {}, "to": {}, "in": {}, "on": {}, "for": {}, "with": {},
"is": {}, "are": {}, "was": {}, "be": {}, "do": {}, "does": {}, "did": {}, "an": {}, "at": {},
"by": {}, "it": {}, "its": {}, "as": {}, "from": {}, "that": {}, "this": {}, "what": {}, "how": {},
"why": {}, "when": {}, "which": {}, "who": {}, "can": {}, "if": {},
"什么": {}, "怎么": {}, "如何": {}, "哪些": {}, "是否": {}, "可以": {}, "一个": {}, "我们": {}, "你们": {},
}