LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。
TypeScript
feat: add chat agent routing
nash_sucea002957229
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 9add7a06..b05cd8e7 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -18,13 +18,6 @@ jobs:- platform: macos-latestargs: '--target aarch64-apple-darwin'rust_target: aarch64-apple-darwin- # Intel macOS build. macos-13 is GitHub's last x86_64 runner- # (macos-14+ are Apple Silicon), so this is a NATIVE build —- # no cross-compile. Restores the Intel .dmg dropped after- # v0.2.0, when the macOS matrix narrowed to aarch64 only.- - platform: macos-13- args: '--target x86_64-apple-darwin'- rust_target: x86_64-apple-darwin- platform: ubuntu-22.04args: ''rust_target: ''@@ -66,8 +59,8 @@ jobs:# The repo ships pre-downloaded PDFium binaries under# src-tauri/pdfium/ for every supported architecture# (libpdfium.so = Linux x86_64, libpdfium-arm64.so = Linux aarch64,- # libpdfium.dylib = macOS arm64, libpdfium-x86_64.dylib = macOS Intel,- # pdfium.dll = Windows). For architectures whose bundled filename must+ # libpdfium.dylib = macOS arm64, pdfium.dll = Windows).+ # For architectures whose bundled filename must# stay `libpdfium.*`, swap the matching binary into place before cargo# runs. We do NOT pull from bblanchon/pdfium-binaries during CI — that# download has historically failed often enough that committing the@@ -84,12 +77,6 @@ jobs:cp src-tauri/pdfium/libpdfium-arm64.so src-tauri/pdfium/libpdfium.sofile src-tauri/pdfium/libpdfium.so- - name: Use x86_64 pdfium binary (macOS Intel only)- if: matrix.rust_target == 'x86_64-apple-darwin'- run: |- cp src-tauri/pdfium/libpdfium-x86_64.dylib src-tauri/pdfium/libpdfium.dylib- file src-tauri/pdfium/libpdfium.dylib-- name: Install protoc (Windows)if: matrix.platform == 'windows-latest'uses: arduino/setup-protoc@v3diff --git a/src/App.tsx b/src/App.tsxindex 7114388b..3cb841ea 100644--- a/src/App.tsx+++ b/src/App.tsx@@ -10,7 +10,7 @@ import { useChatStore } from "@/stores/chat-store"import { BASE_FONT_SIZE_PX, useZoomStore } from "@/stores/zoom-store"import { listDirectory, openProject } from "@/commands/fs"import { getLastProject, getRecentProjects, saveLastProject, loadLlmConfig, loadLanguage, loadSearchApiConfig, loadEmbeddingConfig, loadMineruConfig, loadMultimodalConfig, loadOutputLanguage, loadProviderConfigs, loadActivePresetId, loadProxyConfig, loadScheduledImportConfig, saveScheduledImportConfig, loadSourceWatchConfig, loadApiConfig, loadGeneralConfig, loadZoomLevel } from "@/lib/project-store"-import { loadReviewItems, loadLintItems, loadChatHistory } from "@/lib/persist"+import { loadReviewItems, loadLintItems, loadChatHistory, loadChatPreferences } from "@/lib/persist"import { setupAutoSave } from "@/lib/auto-save"import { startClipWatcher } from "@/lib/clip-watcher"import { AppLayout } from "@/components/layout/app-layout"@@ -434,6 +434,9 @@ function App() {}// Load persisted chat historytry {+ const savedChatPreferences = await loadChatPreferences(proj.path)+ useChatStore.getState().setUseWebSearch(savedChatPreferences.useWebSearch)+ useChatStore.getState().setUseAnyTxtSearch(savedChatPreferences.useAnyTxtSearch)const savedChat = await loadChatHistory(proj.path)if (savedChat.conversations.length > 0) {useChatStore.getState().setConversations(savedChat.conversations)diff --git a/src/components/chat/chat-input.tsx b/src/components/chat/chat-input.tsxindex 40b89fbf..d6de2d6f 100644--- a/src/components/chat/chat-input.tsx+++ b/src/components/chat/chat-input.tsx@@ -23,6 +23,10 @@ interface ChatInputProps {onSend: (text: string, images: MessageImage[], options: ChatSendOptions) => voidonStop: () => voidisStreaming: boolean+ useWebSearch: boolean+ useAnyTxtSearch: boolean+ onUseWebSearchChange: (enabled: boolean) => void+ onUseAnyTxtSearchChange: (enabled: boolean) => voidanyTxtAvailable?: booleanimageInputAvailable?: booleanplaceholder?: string@@ -32,22 +36,24 @@ export function ChatInput({onSend,onStop,isStreaming,+ useWebSearch,+ useAnyTxtSearch,+ onUseWebSearchChange,+ onUseAnyTxtSearchChange,anyTxtAvailable = true,imageInputAvailable = true,placeholder,}: ChatInputProps) {const { t } = useTranslation()const [value, setValue] = useState("")- const [useWebSearch, setUseWebSearch] = useState(false)- const [useAnyTxtSearch, setUseAnyTxtSearch] = useState(false)const [images, setImages] = useState<MessageImage[]>([])const [imageError, setImageError] = useState<string | null>(null)const textareaRef = useRef<HTMLTextAreaElement>(null)const fileInputRef = useRef<HTMLInputElement>(null)useEffect(() => {- if (!anyTxtAvailable) setUseAnyTxtSearch(false)- }, [anyTxtAvailable])+ if (!anyTxtAvailable && useAnyTxtSearch) onUseAnyTxtSearchChange(false)+ }, [anyTxtAvailable, onUseAnyTxtSearchChange, useAnyTxtSearch])// Validate + decode a batch of files (from paste, drop, or the file// picker) and append the accepted ones to `images`. Rejections set a@@ -248,7 +254,7 @@ export function ChatInput({<buttontype="button"aria-pressed={useWebSearch}- onClick={() => setUseWebSearch((v) => !v)}+ onClick={() => onUseWebSearchChange(!useWebSearch)}disabled={isStreaming}className={searchToggleClass(useWebSearch)}>@@ -270,7 +276,7 @@ export function ChatInput({<buttontype="button"aria-pressed={useAnyTxtSearch}- onClick={() => setUseAnyTxtSearch((v) => !v)}+ onClick={() => onUseAnyTxtSearchChange(!useAnyTxtSearch)}disabled={isStreaming || !anyTxtAvailable}className={searchToggleClass(useAnyTxtSearch)}>diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsxindex 45293f96..3f211ef0 100644--- a/src/components/chat/chat-message.tsx+++ b/src/components/chat/chat-message.tsx@@ -1,4 +1,5 @@import { memo, useCallback, useEffect, useRef, useState, useMemo } from "react"+import { useTranslation } from "react-i18next"import ReactMarkdown from "react-markdown"import remarkGfm from "remark-gfm"import remarkMath from "remark-math"@@ -7,7 +8,7 @@ import "katex/dist/katex.min.css"import {Bot, User, FileText, BookmarkPlus, ChevronDown, ChevronRight, RefreshCw, Copy, Check,Users, Lightbulb, BookOpen, HelpCircle, GitMerge, BarChart3, Layout, Globe,- TrendingUp, Target, Image as ImageIcon, FileSearch,+ TrendingUp, Target, Sparkles, Image as ImageIcon, FileSearch,} from "lucide-react"import { openUrl } from "@tauri-apps/plugin-opener"import { useWikiStore } from "@/stores/wiki-store"@@ -29,6 +30,7 @@ import { getHtmlLang, getTextDirection } from "@/lib/language-metadata"import { MermaidDiagram, unwrapMermaidPre } from "@/components/mermaid-diagram"import { inferWikiTypeFromPath } from "@/lib/wiki-page-types"import { cleanAssistantContentForWikiSave, titleFromCleanAssistantContent } from "@/lib/chat-save-to-wiki"+import type { ChatAgentEvent, ChatAgentEventStage } from "@/lib/chat-agent"// Module-level cache of source file nameslet cachedSourceFiles: string[] = []@@ -67,9 +69,19 @@ interface ChatMessageProps {message: DisplayMessageisLastAssistant?: booleanonRegenerate?: () => void+ onOpenReferencePreview?: (preview: ChatReferencePreview) => void}-function ChatMessageImpl({ message, isLastAssistant, onRegenerate }: ChatMessageProps) {+export interface ChatReferencePreview {+ title: string+ path: string+ content: string+ source?: string+ external?: boolean+ snippet?: string+}++function ChatMessageImpl({ message, isLastAssistant, onRegenerate, onOpenReferencePreview }: ChatMessageProps) {const isUser = message.role === "user"const isSystem = message.role === "system"const isAssistant = message.role === "assistant"@@ -121,7 +133,13 @@ function ChatMessageImpl({ message, isLastAssistant, onRegenerate }: ChatMessage)}</div>)}- {isAssistant && <CitedReferencesPanel content={message.content} savedReferences={message.references} />}+ {isAssistant && (+ <CitedReferencesPanel+ content={message.content}+ savedReferences={message.references}+ onOpenReferencePreview={onOpenReferencePreview}+ />+ )}{isAssistant && hovered && (<div className="flex items-center gap-1"><CopyButton content={message.content} />@@ -147,6 +165,7 @@ export const ChatMessage = memo(ChatMessageImpl, (prev, next) =>prev.message === next.message&& prev.isLastAssistant === next.isLastAssistant&& prev.onRegenerate === next.onRegenerate+ && prev.onOpenReferencePreview === next.onOpenReferencePreview)function CopyButton({ content }: { content: string }) {@@ -326,6 +345,33 @@ function displayExternalPath(page: CitedPage): string {return raw}+function isAnyTxtReference(page: CitedPage): boolean {+ return page.kind === "external" && page.source?.toLowerCase() === "anytxt"+}++function referenceSourceLabel(page: CitedPage): string {+ if (isAnyTxtReference(page)) return "AnyTXT"+ if (page.kind === "external") return page.source || "Web"+ return "Wiki"+}++function referenceLocator(page: CitedPage): string {+ if (page.kind === "external") return displayExternalPath(page)+ return page.path+}++function referenceSnippet(page: CitedPage): string {+ return page.kind === "external" ? page.snippet?.trim() ?? "" : ""+}++function projectAbsolutePath(projectPath: string, path: string): string {+ const pp = normalizePath(projectPath)+ const normalized = normalizePath(path)+ if (normalized.startsWith(`${pp}/`)) return normalized+ if (normalized.startsWith("/")) return normalized+ return `${pp}/${normalized.replace(/^\/+/, "")}`+}+/*** Markdown image-reference regex used to count `` occurrences* in cited pages AND extract the first URL (so the image-badge@@ -346,10 +392,17 @@ interface CitedImageInfo {firstUrl: string | null}-function CitedReferencesPanel({ content, savedReferences }: { content: string; savedReferences?: CitedPage[] }) {+function CitedReferencesPanel({+ content,+ savedReferences,+ onOpenReferencePreview,+}: {+ content: string+ savedReferences?: CitedPage[]+ onOpenReferencePreview?: (preview: ChatReferencePreview) => void+}) {const project = useWikiStore((s) => s.project)const openFileInPreview = useWikiStore((s) => s.openFileInPreview)- const setExternalPreview = useWikiStore((s) => s.setExternalPreview)const setPendingScrollImageSrc = useWikiStore((s) => s.setPendingScrollImageSrc)const [expanded, setExpanded] = useState(false)/**@@ -442,7 +495,15 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; stry {const content = await readFile(rawPath)setPendingScrollImageSrc(imageUrlToAbsolute(firstUrl, pp))- openFileInPreview(rawPath, content)+ if (onOpenReferencePreview) {+ onOpenReferencePreview({+ title: getFileName(rawPath),+ path: rawPath,+ content,+ })+ } else {+ openFileInPreview(rawPath, content)+ }console.log(`[refs:image-jump] ${firstUrl} → raw source ${rawPath}`)return} catch (err) {@@ -453,14 +514,23 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s// target — at least the safety-net section will scroll into// view there.try {- const content = await readFile(`${pp}/${fallbackPath}`)+ const fallbackAbsPath = projectAbsolutePath(pp, fallbackPath)+ const content = await readFile(fallbackAbsPath)setPendingScrollImageSrc(firstUrl)- openFileInPreview(`${pp}/${fallbackPath}`, content)+ if (onOpenReferencePreview) {+ onOpenReferencePreview({+ title: getFileName(fallbackAbsPath),+ path: fallbackAbsPath,+ content,+ })+ } else {+ openFileInPreview(fallbackAbsPath, content)+ }} catch (err) {console.warn(`[refs:image-jump] fallback also failed:`, err)}},- [project, setPendingScrollImageSrc, openFileInPreview],+ [project, setPendingScrollImageSrc, openFileInPreview, onOpenReferencePreview],)if (citedPages.length === 0) return null@@ -494,24 +564,35 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; sconst openCitedPage = async () => {if (page.kind === "external") {const target = page.url || page.path- if (page.source?.toLowerCase() === "anytxt") {- const displayPath = displayExternalPath(page)- const previewPath = `anytxt-preview://${encodeURIComponent(target || page.title)}`- const previewContent = [- `# ${page.title}`,- "",- `**Source:** ${page.source ?? "AnyTXT"}`,- `**Path:** ${displayPath}`,- "",- "## Preview",- "",- page.snippet?.trim() || "(No fragment returned by AnyTXT.)",- ].join("\n")+ const displayPath = displayExternalPath(page)+ const previewPath = `${isAnyTxtReference(page) ? "anytxt" : "external"}-preview://${encodeURIComponent(target || page.title)}`+ const previewContent = [+ `# ${page.title}`,+ "",+ `**Source:** ${referenceSourceLabel(page)}`,+ `**Path:** ${displayPath}`,+ "",+ "## Preview",+ "",+ page.snippet?.trim() || "(No preview fragment returned.)",+ ].join("\n")+ if (onOpenReferencePreview) {+ onOpenReferencePreview({+ title: page.title,+ path: displayPath,+ source: referenceSourceLabel(page),+ external: true,+ content: previewContent,+ snippet: page.snippet ?? "",+ })+ return+ }+ if (isAnyTxtReference(page)) {openFileInPreview(previewPath, previewContent)- setExternalPreview({+ useWikiStore.getState().setExternalPreview({title: page.title,path: previewPath,- source: page.source ?? "AnyTXT",+ source: referenceSourceLabel(page),url: displayPath,snippet: page.snippet ?? "",})@@ -528,7 +609,7 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; sconst pp = normalizePath(project.path)const id = getFileName(page.path.replace(/^wiki\//, "").replace(/\.md$/, ""))const candidates = [- `${pp}/${page.path}`,+ projectAbsolutePath(pp, page.path),`${pp}/wiki/entities/${id}.md`,`${pp}/wiki/concepts/${id}.md`,`${pp}/wiki/sources/${id}.md`,@@ -540,13 +621,31 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; sfor (const candidate of candidates) {try {const content = await readFile(candidate)- openFileInPreview(candidate, content)+ if (onOpenReferencePreview) {+ onOpenReferencePreview({+ title: page.title,+ path: candidate,+ content,+ })+ } else {+ openFileInPreview(candidate, content)+ }return} catch {// try next}}- openFileInPreview(`${pp}/${page.path}`, `Unable to load: ${page.path}`)+ const fallbackPath = projectAbsolutePath(pp, page.path)+ const fallbackContent = `Unable to load: ${page.path}`+ if (onOpenReferencePreview) {+ onOpenReferencePreview({+ title: page.title,+ path: fallbackPath,+ content: fallbackContent,+ })+ } else {+ openFileInPreview(fallbackPath, fallbackContent)+ }}return (// Outer is a div, NOT a button — we have two click@@ -557,7 +656,7 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s<divkey={page.path}className="flex w-full items-center gap-1.5 rounded text-left"- title={page.kind === "external" ? `${page.source ?? "External"}: ${page.url ?? page.path}` : page.path}+ title={page.kind === "external" ? `${referenceSourceLabel(page)}: ${referenceLocator(page)}` : page.path}><span className="text-[10px] text-muted-foreground/60 w-4 shrink-0 text-right">[{i + 1}]</span>{/*@@ -590,17 +689,22 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; sclassName="flex min-w-0 flex-1 items-center gap-1.5 rounded px-1 py-0.5 text-left hover:bg-accent/50 transition-colors"><Icon className={`h-3 w-3 shrink-0 ${config.color}`} />- <span className="min-w-0 flex-1 truncate text-foreground/80">- {page.title}- {page.kind === "external" && page.source?.toLowerCase() === "anytxt" && (+ <span className="min-w-0 flex-1 text-foreground/80">+ <span className="block truncate">{page.title}</span>+ {page.kind === "external" && (<span className="mt-0.5 block truncate text-[10px] text-muted-foreground/75">- {displayExternalPath(page)}+ {referenceLocator(page)}+ </span>+ )}+ {isAnyTxtReference(page) && referenceSnippet(page) && (+ <span className="mt-0.5 line-clamp-2 whitespace-normal text-[10px] leading-4 text-muted-foreground">+ {referenceSnippet(page)}</span>)}</span>- {page.kind === "external" && page.source && (- <span className="shrink-0 rounded bg-background/80 px-1 py-0 text-[10px] text-muted-foreground">- {page.source}+ {page.kind === "external" && (+ <span className="shrink-0 rounded border border-border/60 bg-background/80 px-1 py-0 text-[10px] text-muted-foreground">+ {referenceSourceLabel(page)}</span>)}</button>@@ -696,9 +800,10 @@ function extractCitedPages(text: string): CitedPage[] {interface StreamingMessageProps {content: string+ agentEvents?: ChatAgentEvent[]}-export function StreamingMessage({ content }: StreamingMessageProps) {+export function StreamingMessage({ content, agentEvents = [] }: StreamingMessageProps) {const { thinking, answer } = useMemo(() => separateThinking(content), [content])const isThinking = thinking !== null && answer.length === 0@@ -708,6 +813,7 @@ export function StreamingMessage({ content }: StreamingMessageProps) {<Bot className="h-4 w-4" /></div><div className="max-w-[80%] rounded-lg px-3 py-2 text-sm bg-muted text-foreground">+ <AgentActivity events={agentEvents} />{isThinking ? (<StreamingThinkingBlock content={thinking} />) : (@@ -722,6 +828,79 @@ export function StreamingMessage({ content }: StreamingMessageProps) {)}+function AgentActivity({ events }: { events: ChatAgentEvent[] }) {+ const { t } = useTranslation()+ const visible = events.filter((event, index, arr) => {+ const prev = arr[index - 1]+ return !prev+ || prev.stage !== event.stage+ || prev.query !== event.query+ || prev.tool !== event.tool+ || prev.message !== event.message+ })+ if (visible.length === 0) return null++ return (+ <div className="mb-2 flex flex-col gap-1.5 border-b border-border/40 pb-2">+ {visible.map((event, index) => {+ const active = index === visible.length - 1+ const Icon = agentStageIcon(event.stage)+ return (+ <div+ key={`${event.stage}-${event.query ?? ""}-${index}`}+ className={`flex min-w-0 items-center gap-2 text-xs ${+ active ? "text-foreground" : "text-muted-foreground"+ }`}+ >+ <span+ className={`flex h-4 w-4 shrink-0 items-center justify-center ${+ active+ ? "text-primary/70"+ : "text-muted-foreground/60"+ }`}+ >+ <Icon className={`h-3.5 w-3.5 ${active ? "animate-pulse" : ""}`} />+ </span>+ <span className="truncate">+ {event.message || t(`chat.agent.${event.stage}`)}+ {event.query ? <span className="text-muted-foreground"> · {event.query}</span> : null}+ {typeof event.count === "number" ? (+ <span className="text-muted-foreground"> · {t("chat.agent.resultCount", { count: event.count })}</span>+ ) : null}+ </span>+ </div>+ )+ })}+ </div>+ )+}++function agentStageIcon(stage: ChatAgentEventStage) {+ switch (stage) {+ case "understanding":+ return Target+ case "tool_call":+ return Sparkles+ case "tool_result":+ return Check+ case "searching_wiki":+ return BookOpen+ case "searching_graph":+ return GitMerge+ case "searching_web":+ return Globe+ case "searching_anytxt":+ return FileSearch+ case "reading_context":+ return Layout+ case "writing":+ return Bot+ case "routing":+ default:+ return Sparkles+ }+}+function MarkdownContent({ content }: { content: string }) {// Strip hidden commentsconst cleaned = content.replace(/<!--.*?-->/gs, "").trimEnd()diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsxindex 3c1991e9..5f968378 100644--- a/src/components/chat/chat-panel.tsx+++ b/src/components/chat/chat-panel.tsx@@ -1,40 +1,27 @@import { useRef, useEffect, useCallback, useState } from "react"import { useTranslation } from "react-i18next"-import { BookOpen, Plus, Trash2, MessageSquare } from "lucide-react"+import { BookOpen, Plus, Trash2, MessageSquare, X } from "lucide-react"import { Button } from "@/components/ui/button"-import { ChatMessage, StreamingMessage, useSourceFiles } from "./chat-message"+import { ChatMessage, StreamingMessage, useSourceFiles, type ChatReferencePreview } from "./chat-message"import { ChatInput, type ChatSendOptions } from "./chat-input"-import { useChatStore, chatMessagesToLLM, type MessageReference, type MessageImage } from "@/stores/chat-store"+import { useChatStore, chatMessagesToLLM, type MessageImage, type MessageReference } from "@/stores/chat-store"import { useWikiStore } from "@/stores/wiki-store"-import { streamChat, type ChatMessage as LLMMessage } from "@/lib/llm-client"+import { streamChat } from "@/lib/llm-client"import { supportsImageInput } from "@/lib/llm-providers"import { executeIngestWrites } from "@/lib/ingest"-import { listDirectory, readFile, deleteFile } from "@/commands/fs"-import { searchWiki } from "@/lib/search"-import { buildRetrievalGraph, getRelatedNodes } from "@/lib/graph-relevance"-import { normalizePath, getFileName, getRelativePath } from "@/lib/path-utils"-import { buildLanguageDirective, buildLanguageReminder } from "@/lib/output-language"-import { isGreeting } from "@/lib/greeting-detector"-import { computeContextBudget } from "@/lib/context-budget"-import { anyTxtSearchSmart, hasConfiguredAnyTxt } from "@/lib/anytxt-search"-import { resolveSearchConfig, webSearch, type WebSearchResult } from "@/lib/web-search"+import { listDirectory, deleteFile } from "@/commands/fs"+import { getFileName, normalizePath } from "@/lib/path-utils"+import { hasConfiguredAnyTxt } from "@/lib/anytxt-search"+import { buildChatAgentMessages, type ChatAgentEvent } from "@/lib/chat-agent"+import { FilePreview } from "@/components/editor/file-preview"+import { WikiReader } from "@/components/editor/wiki-reader"+import { FrontmatterPanel } from "@/components/editor/frontmatter-panel"+import { parseFrontmatter } from "@/lib/frontmatter"+import { getFileCategory } from "@/lib/file-types"// Store the page mapping from the last query so SourceFilesBar can show which pages were citedexport let lastQueryPages: { title: string; path: string }[] = []-function formatExternalSearchContext(results: WebSearchResult[]): string {- if (results.length === 0) return ""- return results- .map((result, index) => [- `### [E${index + 1}] ${result.title}`,- `Source: ${result.source}`,- `URL: ${result.url}`,- "",- result.snippet,- ].join("\n"))- .join("\n\n---\n\n")-}-function formatDate(timestamp: number): string {const d = new Date(timestamp)const now = new Date()@@ -150,6 +137,10 @@ export function ChatPanel() {const createConversation = useChatStore((s) => s.createConversation)const removeLastAssistantMessage = useChatStore((s) => s.removeLastAssistantMessage)const maxHistoryMessages = useChatStore((s) => s.maxHistoryMessages)+ const useWebSearch = useChatStore((s) => s.useWebSearch)+ const useAnyTxtSearch = useChatStore((s) => s.useAnyTxtSearch)+ const setUseWebSearch = useChatStore((s) => s.setUseWebSearch)+ const setUseAnyTxtSearch = useChatStore((s) => s.setUseAnyTxtSearch)// Derive active messages via selector to re-render on message changesconst allMessages = useChatStore((s) => s.messages)@@ -165,8 +156,20 @@ export function ChatPanel() {const setFileTree = useWikiStore((s) => s.setFileTree)const abortRef = useRef<AbortController | null>(null)+ const runIdRef = useRef(0)const scrollContainerRef = useRef<HTMLDivElement>(null)const bottomRef = useRef<HTMLDivElement>(null)+ const [agentEvents, setAgentEvents] = useState<ChatAgentEvent[]>([])+ const [referencePreview, setReferencePreview] = useState<ChatReferencePreview | null>(null)+ const [referencePreviewWidth, setReferencePreviewWidth] = useState(420)+ const lastMessage = activeMessages[activeMessages.length - 1]+ const scrollKey = [+ activeConversationId ?? "",+ activeMessages.length,+ lastMessage?.id ?? "",+ lastMessage?.content.length ?? 0,+ isStreaming ? streamingContent.length : 0,+ ].join(":")// Auto-scroll to bottom when messages change or streaming content updatesuseEffect(() => {@@ -174,14 +177,18 @@ export function ChatPanel() {if (container) {container.scrollTop = container.scrollHeight}- }, [activeMessages, streamingContent])+ }, [scrollKey])const handleSend = useCallback(async (text: string,images: MessageImage[] = [],- options: ChatSendOptions = { useWebSearch: false, useAnyTxtSearch: false },+ options?: ChatSendOptions,) => {+ const sendOptions = options ?? {+ useWebSearch: useChatStore.getState().useWebSearch,+ useAnyTxtSearch: useChatStore.getState().useAnyTxtSearch,+ }// Auto-create a conversation if none is activelet convId = useChatStore.getState().activeConversationIdif (!convId) {@@ -190,343 +197,124 @@ export function ChatPanel() {addMessage("user", text, images)setStreaming(true)+ setAgentEvents([])+ let finalized = false+ const runId = ++runIdRef.current- // Build system prompt with wiki context using graph-enhanced retrieval- const systemMessages: LLMMessage[] = []- let queryRefs: MessageReference[] = []- let langReminder: string | undefined- // Pure greetings ("hi", "你好", "嗨") don't warrant running the whole- // retrieval pipeline — it's slow, costs context, and drags in random- // wiki pages the user clearly didn't ask about. Short-circuit with a- // minimal system prompt and let the model reply conversationally.- const greetingOnly = isGreeting(text)- if (project && greetingOnly) {- systemMessages.push({- role: "system",- content: [- `You are a wiki assistant for the project "${project.name}".`,- "The user sent a casual greeting — reply briefly and naturally, in one or two sentences.",- "Do NOT invent wiki content or pretend to have retrieved pages. Invite the user to ask a concrete question if they want information from the wiki.",- "",- buildLanguageReminder(text),- ].join("\n"),+ try {+ const controller = new AbortController()+ abortRef.current = controller+ const isCurrentRun = () => runIdRef.current === runId && !controller.signal.aborted++ const activeConvMessages = useChatStore.getState().getActiveMessages()+ .filter((m) => m.role === "user" || m.role === "assistant")+ .slice(-maxHistoryMessages)+ const historyMessages = chatMessagesToLLM(activeConvMessages)+ const retrievalHistory = collectRecentRetrievalHistory(activeConvMessages)+ const agentResult = await buildChatAgentMessages({+ project: project ? { name: project.name, path: project.path } : null,+ llmConfig,+ searchApiConfig,+ text,+ historyMessages,+ retrievalHistory,+ dataVersion: useWikiStore.getState().dataVersion,+ options: sendOptions,+ signal: controller.signal,+ onEvent: (event) => {+ if (!isCurrentRun()) return+ setAgentEvents((prev) => [...prev, event].slice(-6))+ },})- // Skip retrieval; queryRefs stays empty so no "Sources" chip is shown.- } else if (project) {- const pp = normalizePath(project.path)- const dataVersion = useWikiStore.getState().dataVersion-- // ── Budget allocation (see context-budget.ts) ─────────- // Page budget scales with the LLM's context window; we now- // also reserve ~15% as headroom for the response so the- // model isn't truncated mid-sentence on a packed prompt.- const {- indexBudget: INDEX_BUDGET,- pageBudget: PAGE_BUDGET,- maxPageSize: MAX_PAGE_SIZE,- } = computeContextBudget(llmConfig.maxContextSize)-- const [rawIndex, purpose] = await Promise.all([- readFile(`${pp}/wiki/index.md`).catch(() => ""),- readFile(`${pp}/purpose.md`).catch(() => ""),- ])-- // ── Phase 1: Tokenized search → top 10 ────────────────- const searchResults = await searchWiki(pp, text)- const topSearchResults = searchResults.slice(0, 10)-- const resolvedExternalSearchConfig = resolveSearchConfig(searchApiConfig)- const externalSearchResults: WebSearchResult[] = []- const externalSearchErrors: string[] = []- const externalCalls: Promise<WebSearchResult[]>[] = []-- if (options.useWebSearch) {- externalCalls.push(- webSearch(text, resolvedExternalSearchConfig, 5).catch((err) => {- externalSearchErrors.push(- `Web Search: ${err instanceof Error ? err.message : String(err)}`,- )- return []- }),- )- }-- if (options.useAnyTxtSearch) {- externalCalls.push(- anyTxtSearchSmart(text, resolvedExternalSearchConfig.anyTxt, llmConfig, 5, pp).catch((err) => {- externalSearchErrors.push(- `AnyTXT: ${err instanceof Error ? err.message : String(err)}`,- )- return []- }),- )- }-- if (externalCalls.length > 0) {- const batches = await Promise.all(externalCalls)- const seenExternal = new Set<string>()- for (const result of batches.flat()) {- const key = result.url || `${result.source}:${result.title}:${result.snippet}`- if (seenExternal.has(key)) continue- seenExternal.add(key)- externalSearchResults.push(result)- if (externalSearchResults.length >= 10) break- }- }-- // ── Trim index by relevance if over budget ─────────────- let index = rawIndex- if (rawIndex.length > INDEX_BUDGET) {- const { tokenizeQuery } = await import("@/lib/search")- const tokens = tokenizeQuery(text)- const lines = rawIndex.split("\n")- const keptLines: string[] = []- let keptSize = 0-- for (const line of lines) {- const isHeader = line.startsWith("##")- const lower = line.toLowerCase()- const isRelevant = tokens.some((t) => lower.includes(t))-- if (isHeader || isRelevant) {- if (keptSize + line.length + 1 <= INDEX_BUDGET) {- keptLines.push(line)- keptSize += line.length + 1- }- }- }- index = keptLines.join("\n")- if (index.length < rawIndex.length) {- index += "\n\n[...index trimmed to relevant entries...]"- }- }-- // ── Phase 2: Graph 1-level expansion ───────────────────- // Note: Vector search (if enabled) is already merged into searchResults- // by searchWiki() in search.ts — no duplicate code needed here.- const graph = await buildRetrievalGraph(pp, dataVersion)- const expandedIds = new Set<string>()- const searchHitPaths = new Set(topSearchResults.map((r) => r.path))- const graphExpansions: { title: string; path: string; relevance: number }[] = []-- for (const result of topSearchResults) {- const fileName = getFileName(result.path)- const nodeId = fileName.replace(/\.md$/, "")- const related = getRelatedNodes(nodeId, graph, 3)- for (const { node, relevance } of related) {- if (relevance < 2.0) continue- if (searchHitPaths.has(node.path)) continue- if (expandedIds.has(node.id)) continue- expandedIds.add(node.id)- graphExpansions.push({ title: node.title, path: node.path, relevance })+ if (!isCurrentRun()) return+ lastQueryPages = agentResult.queryPages++ let accumulated = ""+ let thinkingOpen = false++ const appendReasoning = (token: string) => {+ if (!token) return+ if (!thinkingOpen) {+ thinkingOpen = true+ accumulated += "<think>"+ appendStreamToken("<think>")}- }- graphExpansions.sort((a, b) => b.relevance - a.relevance)-- // ── Phase 3 & 4: Page budget control ───────────────────- let usedChars = 0- type PageEntry = { title: string; path: string; content: string; priority: number }- const relevantPages: PageEntry[] = []-- const tryAddPage = async (title: string, filePath: string, priority: number): Promise<boolean> => {- if (usedChars >= PAGE_BUDGET) return false- try {- const raw = await readFile(filePath)- const relativePath = getRelativePath(filePath, pp)- const truncated = raw.length > MAX_PAGE_SIZE- ? raw.slice(0, MAX_PAGE_SIZE) + "\n\n[...truncated...]"- : raw- if (usedChars + truncated.length > PAGE_BUDGET) return false- usedChars += truncated.length- relevantPages.push({ title, path: relativePath, content: truncated, priority })- return true- } catch { return false }+ accumulated += token+ appendStreamToken(token)}- // P0: Title matches- for (const r of topSearchResults.filter((r) => r.titleMatch)) {- await tryAddPage(r.title, r.path, 0)- }- // P1: Content matches- for (const r of topSearchResults.filter((r) => !r.titleMatch)) {- await tryAddPage(r.title, r.path, 1)- }- // P2: Graph expansions- for (const exp of graphExpansions) {- await tryAddPage(exp.title, exp.path, 2)+ const closeReasoning = () => {+ if (!thinkingOpen) return+ thinkingOpen = false+ accumulated += "</think>"+ appendStreamToken("</think>")}- // P3: Overview fallback- if (relevantPages.length === 0) {- await tryAddPage("Overview", `${pp}/wiki/overview.md`, 3)- }-- const pagesContext = relevantPages.length > 0- ? relevantPages.map((p, i) =>- `### [${i + 1}] ${p.title}\nPath: ${p.path}\n\n${p.content}`- ).join("\n\n---\n\n")- : "(No wiki pages found)"-- const pageList = relevantPages.map((p, i) =>- `[${i + 1}] ${p.title} (${p.path})`- ).join("\n")- const externalContext = formatExternalSearchContext(externalSearchResults)-- systemMessages.push({- role: "system",- content: [- "You are a knowledgeable wiki assistant. Answer questions based on the wiki content provided below.",- "",- "## Rules",- externalContext- ? "- Answer based ONLY on the numbered wiki pages and external sources provided below."- : "- Answer based ONLY on the numbered wiki pages provided below.",- "- If the provided pages don't contain enough information, say so honestly.",- "- Keep subject boundaries strict: do not apply a claim, limitation, evaluation, benchmark result, or recommendation about one entity/model/product/method to another subject just because they share keywords.",- "- If pages or external sources discuss multiple subjects, attribute each claim to the exact subject named in that page or source; when uncertain, state the uncertainty instead of generalizing.",- "- Use [[wikilink]] syntax to reference wiki pages.",- externalContext- ? "- When citing wiki information, use page numbers like [1], [2]. When citing external information, use external source IDs like [E1], [E2]."- : "- When citing information, use the page number in brackets, e.g. [1], [2].",- "- At the VERY END of your response, add a hidden comment listing which page numbers you used:",- " <!-- cited: 1, 3, 5 -->",- "",- "Use markdown formatting for clarity.",- "",- purpose ? `## Wiki Purpose\n${purpose}` : "",- index ? `## Wiki Index\n${index}` : "",- relevantPages.length > 0 ? `## Page List\n${pageList}` : "",- `## Wiki Pages\n\n${pagesContext}`,- externalContext ? `## External Sources\n\n${externalContext}` : "",- externalSearchErrors.length > 0- ? `## External Source Errors\n${externalSearchErrors.map((err) => `- ${err}`).join("\n")}`- : "",- "",- "---",- "",- buildLanguageDirective(text),- ].filter(Boolean).join("\n"),- })-- // Reminder injected later, right before the user's current message- // (after history so it's the last system instruction the LLM sees).- langReminder = buildLanguageReminder(text)-- lastQueryPages = relevantPages.map((p) => ({ title: p.title, path: p.path }))- const externalRefs: MessageReference[] = externalSearchResults.map((result) => ({- title: result.title,- path: result.url,- kind: "external",- source: result.source,- url: result.url,- snippet: result.snippet,- }))- queryRefs = [...lastQueryPages.map((page) => ({ ...page, kind: "wiki" as const })), ...externalRefs]- }- // ── Conversation history with count limit ────────────────- // Only include messages from the active conversation, last N messages- const activeConvMessages = useChatStore.getState().getActiveMessages()- .filter((m) => m.role === "user" || m.role === "assistant")- .slice(-maxHistoryMessages)-- // Prepend the language reminder onto the final user turn rather than- // inserting a second {role:"system"} between history and the final- // user message. vLLM / llama.cpp / Ollama drive their chat templates- // from HF Jinja, and Qwen3-family templates enforce "system only at- // index 0" — a mid-conversation system message gets rejected with- // "System message must be at the beginning." (HTTP 400). OpenAI and- // Anthropic are more lenient, but keeping a single system at the top- // is the safest shape across every OpenAI-compatible backend.- const historyMessages = chatMessagesToLLM(activeConvMessages)- let llmMessages: LLMMessage[] = [...systemMessages, ...historyMessages]- if (langReminder && historyMessages.length > 0) {- const lastIdx = llmMessages.length - 1- const last = llmMessages[lastIdx]- if (last && last.role === "user") {- // The final user turn may now be either a plain string- // (text-only) or a ContentBlock[] (carries images). Prepend- // the language reminder to the textual part in both shapes —- // string-concat for the legacy form, and into the first text- // block (or a new leading text block) for the multimodal form.- const prefix = `[${langReminder}]\n\n`- let newContent: LLMMessage["content"]- if (typeof last.content === "string") {- newContent = `${prefix}${last.content}`- } else {- const blocks = [...last.content]- const firstTextIdx = blocks.findIndex((b) => b.type === "text")- if (firstTextIdx >= 0) {- const tb = blocks[firstTextIdx]- if (tb.type === "text") {- blocks[firstTextIdx] = { type: "text", text: `${prefix}${tb.text}` }+ await streamChat(+ llmConfig,+ agentResult.messages,+ {+ onToken: (token) => {+ if (!isCurrentRun()) return+ closeReasoning()+ accumulated += token+ appendStreamToken(token)+ },+ onReasoningToken: (token) => {+ if (!isCurrentRun()) return+ appendReasoning(token)+ },+ onDone: () => {+ if (!isCurrentRun()) return+ closeReasoning()+ finalized = true+ finalizeStream(accumulated, agentResult.references)+ setAgentEvents([])+ abortRef.current = null+ // save-worthy detection removed — user has direct "Save to Wiki" button on each message+ },+ onError: (err) => {+ if (!isCurrentRun()) return+ if (controller.signal.aborted || isAbortLikeError(err)) {+ finalized = true+ setStreaming(false)+ setAgentEvents([])+ abortRef.current = null+ return}- } else {- blocks.unshift({ type: "text", text: prefix })- }- newContent = blocks+ finalized = true+ finalizeStream(`Error: ${err.message}`, undefined)+ setAgentEvents([])+ abortRef.current = null+ },+ },+ controller.signal,+ )+ } catch (err) {+ if (!finalized) {+ if (isAbortLikeError(err) || runIdRef.current !== runId) {+ setStreaming(false)+ setAgentEvents([])+ abortRef.current = null+ return}- llmMessages = [- ...llmMessages.slice(0, lastIdx),- { role: "user", content: newContent },- ]- }- }-- const controller = new AbortController()- abortRef.current = controller-- let accumulated = ""- let thinkingOpen = false-- const appendReasoning = (token: string) => {- if (!token) return- if (!thinkingOpen) {- thinkingOpen = true- accumulated += "<think>"- appendStreamToken("<think>")+ const message = err instanceof Error ? err.message : String(err)+ finalizeStream(`Error: ${message}`, undefined)+ setAgentEvents([])}- accumulated += token- appendStreamToken(token)+ abortRef.current = null}-- const closeReasoning = () => {- if (!thinkingOpen) return- thinkingOpen = false- accumulated += "</think>"- appendStreamToken("</think>")- }-- await streamChat(- llmConfig,- llmMessages,- {- onToken: (token) => {- closeReasoning()- accumulated += token- appendStreamToken(token)- },- onReasoningToken: appendReasoning,- onDone: () => {- closeReasoning()- finalizeStream(accumulated, queryRefs)- abortRef.current = null- // save-worthy detection removed — user has direct "Save to Wiki" button on each message- },- onError: (err) => {- finalizeStream(`Error: ${err.message}`, undefined)- abortRef.current = null- },- },- controller.signal,- )},- [llmConfig, searchApiConfig, addMessage, setStreaming, appendStreamToken, finalizeStream, createConversation, maxHistoryMessages],+ [project, llmConfig, searchApiConfig, addMessage, setStreaming, appendStreamToken, finalizeStream, createConversation, maxHistoryMessages],)const handleStop = useCallback(() => {+ runIdRef.current += 1abortRef.current?.abort()abortRef.current = null- }, [])+ setStreaming(false)+ setAgentEvents([])+ }, [setStreaming])const handleRegenerate = useCallback(async () => {if (isStreaming) return@@ -604,10 +392,11 @@ export function ChatPanel() {message={msg}isLastAssistant={isLastAssistant && !isStreaming}onRegenerate={isLastAssistant ? handleRegenerate : undefined}+ onOpenReferencePreview={setReferencePreview}/>)})}- {isStreaming && <StreamingMessage content={streamingContent} />}+ {isStreaming && <StreamingMessage content={streamingContent} agentEvents={agentEvents} />}<div ref={bottomRef} /></div></div>@@ -632,6 +421,10 @@ export function ChatPanel() {onSend={handleSend}onStop={handleStop}isStreaming={isStreaming}+ useWebSearch={useWebSearch}+ useAnyTxtSearch={useAnyTxtSearch}+ onUseWebSearchChange={setUseWebSearch}+ onUseAnyTxtSearchChange={setUseAnyTxtSearch}anyTxtAvailable={anyTxtAvailable}imageInputAvailable={imageInputAvailable}placeholder={@@ -641,6 +434,172 @@ export function ChatPanel() {}/></div>++ {referencePreview && (+ <ChatReferencePreviewPanel+ preview={referencePreview}+ width={referencePreviewWidth}+ onResize={setReferencePreviewWidth}+ onClose={() => setReferencePreview(null)}+ />+ )}+ </div>+ )+}++function ChatReferencePreviewPanel({+ preview,+ width,+ onResize,+ onClose,+}: {+ preview: ChatReferencePreview+ width: number+ onResize: (width: number) => void+ onClose: () => void+}) {+ const { t } = useTranslation()+ const displayTitle = preview.title || getFileName(preview.path)+ const dragStartRef = useRef<{ x: number; width: number } | null>(null)++ const startResize = useCallback((event: React.PointerEvent<HTMLDivElement>) => {+ event.preventDefault()+ dragStartRef.current = { x: event.clientX, width }+ event.currentTarget.setPointerCapture(event.pointerId)+ }, [width])++ const handleResize = useCallback((event: React.PointerEvent<HTMLDivElement>) => {+ if (!dragStartRef.current) return+ const delta = dragStartRef.current.x - event.clientX+ onResize(clampReferencePreviewWidth(dragStartRef.current.width + delta))+ }, [onResize])++ const stopResize = useCallback((event: React.PointerEvent<HTMLDivElement>) => {+ dragStartRef.current = null+ if (event.currentTarget.hasPointerCapture(event.pointerId)) {+ event.currentTarget.releasePointerCapture(event.pointerId)+ }+ }, [])++ return (+ <aside+ className="relative flex h-full min-w-[320px] max-w-[56%] shrink-0 flex-col border-l bg-background"+ style={{ width }}+ >+ <div+ role="separator"+ aria-orientation="vertical"+ aria-label={t("chat.resizeReferencePreview")}+ tabIndex={0}+ onPointerDown={startResize}+ onPointerMove={handleResize}+ onPointerUp={stopResize}+ onPointerCancel={stopResize}+ onKeyDown={(event) => {+ if (event.key === "ArrowLeft") {+ event.preventDefault()+ onResize(clampReferencePreviewWidth(width + 32))+ } else if (event.key === "ArrowRight") {+ event.preventDefault()+ onResize(clampReferencePreviewWidth(width - 32))+ }+ }}+ className="absolute -left-1 top-0 z-10 h-full w-2 cursor-col-resize outline-none transition-colors hover:bg-primary/15 focus-visible:bg-primary/20"+ />+ <div className="flex min-h-10 items-center gap-2 border-b px-3 py-2">+ <div className="min-w-0 flex-1">+ <div className="truncate text-xs font-medium" title={displayTitle}>+ {displayTitle}+ </div>+ <div className="mt-0.5 truncate text-[10px] text-muted-foreground" title={preview.path}>+ {preview.source ?? t("chat.referencePreview")} · {preview.path}+ </div>+ </div>+ <button+ type="button"+ onClick={onClose}+ className="shrink-0 rounded p-1 text-muted-foreground hover:bg-accent hover:text-foreground"+ title={t("chat.closeReferencePreview")}+ aria-label={t("chat.closeReferencePreview")}+ >+ <X className="h-3.5 w-3.5" />+ </button>+ </div>+ <div className="min-h-0 flex-1 overflow-auto">+ {preview.external ? (+ <ExternalReferencePreview preview={preview} />+ ) : getFileCategory(preview.path) === "markdown" ? (+ <ChatMarkdownReferencePreview preview={preview} />+ ) : (+ <FilePreview+ key={preview.path}+ filePath={preview.path}+ textContent={preview.content}+ />+ )}+ </div>+ </aside>+ )+}++function clampReferencePreviewWidth(width: number): number {+ return Math.min(760, Math.max(320, Math.round(width)))+}++function ChatMarkdownReferencePreview({ preview }: { preview: ChatReferencePreview }) {+ const { frontmatter, body } = parseFrontmatter(preview.content)+ return (+ <div className="h-full overflow-auto px-6 py-6">+ {frontmatter && <FrontmatterPanel data={frontmatter} />}+ <WikiReader body={body} filePath={preview.path} />+ </div>+ )+}++function ExternalReferencePreview({ preview }: { preview: ChatReferencePreview }) {+ const { t } = useTranslation()+ return (+ <div className="flex h-full flex-col overflow-auto p-5">+ <div className="mb-4 space-y-2">+ <div className="flex items-center gap-2">+ {preview.source && (+ <span className="rounded border border-border/60 bg-muted px-1.5 py-0.5 text-[10px] font-medium uppercase text-muted-foreground">+ {preview.source}+ </span>+ )}+ <h3 className="truncate text-sm font-medium" title={preview.title}>{preview.title}</h3>+ </div>+ <div className="break-all rounded-md border border-border/60 bg-muted/30 px-3 py-2 text-xs text-muted-foreground">+ {preview.path.replace(/^[a-z]+-preview:\/\//, "")}+ </div>+ </div>+ <div className="min-h-0 flex-1 overflow-auto rounded-lg border border-border/60 bg-muted/20 p-4">+ <pre className="whitespace-pre-wrap break-words font-sans text-sm leading-6">+ {preview.snippet?.trim() || t("chat.noReferencePreviewFragment")}+ </pre>+ </div></div>)}++function isAbortLikeError(err: unknown): boolean {+ if (err instanceof DOMException && err.name === "AbortError") return true+ if (!(err instanceof Error)) return false+ return err.name === "AbortError" || /abort|cancel/i.test(err.message)+}++function collectRecentRetrievalHistory(messages: ReturnType<typeof useChatStore.getState>["messages"]): MessageReference[] {+ const refs: MessageReference[] = []+ const seen = new Set<string>()+ for (const msg of [...messages].reverse()) {+ if (msg.role !== "assistant" || !msg.references) continue+ for (const ref of msg.references) {+ const key = `${ref.kind ?? "wiki"}:${ref.url ?? ref.path}`.toLowerCase()+ if (seen.has(key)) continue+ seen.add(key)+ refs.push(ref)+ if (refs.length >= 10) return refs+ }+ }+ return refs+}diff --git a/src/i18n/en.json b/src/i18n/en.jsonindex 4899a17a..65eee874 100644--- a/src/i18n/en.json+++ b/src/i18n/en.json@@ -149,7 +149,24 @@"imageInputUnavailable": "The current provider does not support image input. Switch to a vision-capable provider in Settings.","imageTooLarge": "Image too large (max {{max}}MB): {{name}}","tooManyImages": "Too many images (max {{max}})",- "unsupportedImageType": "Unsupported image type: {{type}}"+ "unsupportedImageType": "Unsupported image type: {{type}}",+ "referencePreview": "Reference preview",+ "closeReferencePreview": "Close reference preview",+ "resizeReferencePreview": "Resize reference preview",+ "noReferencePreviewFragment": "No preview fragment returned.",+ "agent": {+ "understanding": "Understanding request",+ "routing": "Planning next step",+ "tool_call": "Calling tool",+ "tool_result": "Tool result",+ "searching_wiki": "Searching wiki",+ "searching_graph": "Exploring knowledge graph",+ "searching_web": "Searching web",+ "searching_anytxt": "Searching AnyTXT",+ "reading_context": "Reading context",+ "writing": "Writing answer",+ "resultCount": "results: {{count}}"+ }},"search": {"title": "Search",diff --git a/src/i18n/zh.json b/src/i18n/zh.jsonindex 07ed25dd..f997eace 100644--- a/src/i18n/zh.json+++ b/src/i18n/zh.json@@ -149,7 +149,24 @@"imageInputUnavailable": "当前 Provider 不支持图片输入。请在设置中切换到支持视觉的 Provider。","imageTooLarge": "图片过大(最大 {{max}}MB):{{name}}","tooManyImages": "图片数量过多(最多 {{max}} 张)",- "unsupportedImageType": "不支持的图片类型:{{type}}"+ "unsupportedImageType": "不支持的图片类型:{{type}}",+ "referencePreview": "引用预览",+ "closeReferencePreview": "关闭引用预览",+ "resizeReferencePreview": "调整引用预览宽度",+ "noReferencePreviewFragment": "没有返回可预览的片段。",+ "agent": {+ "understanding": "理解问题",+ "routing": "规划下一步",+ "tool_call": "调用工具",+ "tool_result": "工具结果",+ "searching_wiki": "搜索 Wiki",+ "searching_graph": "探索知识图谱",+ "searching_web": "搜索网页",+ "searching_anytxt": "搜索 AnyTXT",+ "reading_context": "读取上下文",+ "writing": "生成回答",+ "resultCount": "{{count}} 条结果"+ }},"search": {"title": "搜索",diff --git a/src/lib/auto-save.test.ts b/src/lib/auto-save.test.tsindex 706a9299..9286d220 100644--- a/src/lib/auto-save.test.ts+++ b/src/lib/auto-save.test.ts@@ -12,13 +12,14 @@*/import { describe, it, expect, beforeEach, vi } from "vitest"-const { saveReviewItems, saveLintItems, saveChatHistory } = vi.hoisted(() => ({+const { saveReviewItems, saveLintItems, saveChatHistory, saveChatPreferences } = vi.hoisted(() => ({saveReviewItems: vi.fn().mockResolvedValue(undefined),saveLintItems: vi.fn().mockResolvedValue(undefined),saveChatHistory: vi.fn().mockResolvedValue(undefined),+ saveChatPreferences: vi.fn().mockResolvedValue(undefined),}))-vi.mock("./persist", () => ({ saveReviewItems, saveLintItems, saveChatHistory }))+vi.mock("./persist", () => ({ saveReviewItems, saveLintItems, saveChatHistory, saveChatPreferences }))import {setupAutoSave,@@ -46,6 +47,7 @@ beforeEach(() => {saveReviewItems.mockClear()saveLintItems.mockClear()saveChatHistory.mockClear()+ saveChatPreferences.mockClear()useReviewStore.setState({ items: [] })useLintStore.setState({ items: [] })useChatStore.setState({ conversations: [], messages: [], isStreaming: false })@@ -116,6 +118,7 @@ describe("auto-save project-switch guard", () => {saveReviewItems.mockClear()saveLintItems.mockClear()saveChatHistory.mockClear()+ saveChatPreferences.mockClear()// A post-failure store change should not write empty data to the half-opened// project because the cleanup cleared the active project before resume.@@ -127,6 +130,7 @@ describe("auto-save project-switch guard", () => {expect(saveReviewItems).not.toHaveBeenCalled()expect(saveLintItems).not.toHaveBeenCalled()expect(saveChatHistory).not.toHaveBeenCalled()+ expect(saveChatPreferences).not.toHaveBeenCalled()})it("skips chat flush while streaming", async () => {@@ -136,6 +140,22 @@ describe("auto-save project-switch guard", () => {await flushAndSuspendAutoSave()expect(saveChatHistory).not.toHaveBeenCalled()+ expect(saveChatPreferences).toHaveBeenCalledWith("/proj/A", {+ useWebSearch: false,+ useAnyTxtSearch: false,+ })expect(saveReviewItems).toHaveBeenCalled()})++ it("persists chat search preferences on flush", async () => {+ setProjectPath("/proj/A")+ useChatStore.setState({ useWebSearch: true, useAnyTxtSearch: true })++ await flushAndSuspendAutoSave()++ expect(saveChatPreferences).toHaveBeenCalledWith("/proj/A", {+ useWebSearch: true,+ useAnyTxtSearch: true,+ })+ })})diff --git a/src/lib/auto-save.ts b/src/lib/auto-save.tsindex 46e7cffc..846e4517 100644--- a/src/lib/auto-save.ts+++ b/src/lib/auto-save.ts@@ -2,7 +2,7 @@ import { useReviewStore } from "@/stores/review-store"import { useLintStore } from "@/stores/lint-store"import { useChatStore } from "@/stores/chat-store"import { useWikiStore } from "@/stores/wiki-store"-import { saveReviewItems, saveLintItems, saveChatHistory } from "./persist"+import { saveReviewItems, saveLintItems, saveChatHistory, saveChatPreferences } from "./persist"let reviewTimer: ReturnType<typeof setTimeout> | null = nulllet lintTimer: ReturnType<typeof setTimeout> | null = null@@ -38,6 +38,10 @@ export async function flushAndSuspendAutoSave(): Promise<void> {await Promise.allSettled([saveReviewItems(projectPath, review),saveLintItems(projectPath, lint),+ saveChatPreferences(projectPath, {+ useWebSearch: chat.useWebSearch,+ useAnyTxtSearch: chat.useAnyTxtSearch,+ }),chat.isStreaming? Promise.resolve(): saveChatHistory(projectPath, chat.conversations, chat.messages),@@ -105,7 +109,13 @@ export function setupAutoSave(): void {if (chatTimer) clearTimeout(chatTimer)chatTimer = setTimeout(() => {if (projectPath) {- saveChatHistory(projectPath, state.conversations, state.messages).catch(() => {})+ Promise.allSettled([+ saveChatPreferences(projectPath, {+ useWebSearch: state.useWebSearch,+ useAnyTxtSearch: state.useAnyTxtSearch,+ }),+ saveChatHistory(projectPath, state.conversations, state.messages),+ ]).catch(() => {})}}, 2000)})diff --git a/src/lib/chat-agent.test.ts b/src/lib/chat-agent.test.tsnew file mode 100644index 00000000..b603fca8--- /dev/null+++ b/src/lib/chat-agent.test.ts@@ -0,0 +1,603 @@+import { describe, expect, it, vi } from "vitest"+import {+ buildChatAgentMessages,+ getChatAgentTools,+ parseDecision,+ parseUnderstanding,+ shouldBypassAgentPlanner,+ type ChatAgentDeps,+} from "./chat-agent"+import type { LlmConfig, SearchApiConfig } from "@/stores/wiki-store"+import type { ChatMessage as LLMMessage } from "@/lib/llm-client"++vi.mock("@/commands/fs", () => ({+ readFile: vi.fn(async (path: string) => {+ if (path.endsWith("purpose.md")) {+ return "This wiki tracks transformer research."+ }+ if (path.endsWith("wiki/index.md")) {+ return "## Concepts\n- [[Attention]]\n- [[Transformer]]"+ }+ if (path.endsWith("wiki/overview.md")) {+ return "# Overview\n\nThis wiki covers transformer architecture, attention, and benchmark notes."+ }+ if (path.endsWith("attention.md")) {+ return "---\ntitle: Attention\ntype: concept\n---\n# Attention\n\nAttention lets models focus on relevant tokens."+ }+ if (path.endsWith("transformer.md")) {+ return "---\ntitle: Transformer\ntype: concept\n---\n# Transformer\n\nTransformers use attention layers."+ }+ return ""+ }),+}))++vi.mock("@/lib/graph-relevance", () => ({+ buildRetrievalGraph: vi.fn(async () => ({ nodes: new Map(), dataVersion: 1 })),+ getRelatedNodes: vi.fn(() => [+ {+ node: {+ id: "transformer",+ title: "Transformer",+ type: "concept",+ path: "/tmp/project/wiki/concepts/transformer.md",+ sources: [],+ outLinks: new Set(),+ inLinks: new Set(),+ },+ relevance: 3,+ },+ ]),+}))++const llmConfig: LlmConfig = {+ provider: "openai",+ apiKey: "test",+ model: "test-model",+ ollamaUrl: "",+ customEndpoint: "https://example.com/v1",+ maxContextSize: 128_000,+}++const searchApiConfig: SearchApiConfig = {+ provider: "none",+ apiKey: "",+}++describe("chat agent routing", () => {+ it("bypasses retrieval for greetings", () => {+ expect(shouldBypassAgentPlanner("你好")?.action).toBe("answer")+ })++ it("bypasses retrieval for short follow-up commands", () => {+ expect(shouldBypassAgentPlanner("继续")?.action).toBe("answer")+ expect(shouldBypassAgentPlanner("summarize the above")?.action).toBe("answer")+ })++ it("parses JSON decisions from model output", () => {+ expect(parseDecision('```json\n{"action":"graph_search","queries":["A B relationship"]}\n```', "fallback")).toEqual({+ action: "graph_search",+ queries: ["A B relationship"],+ answer: undefined,+ reason: undefined,+ })+ })++ it("falls back to wiki search when model routing output is malformed", () => {+ expect(parseDecision("not json", "attention")).toMatchObject({+ action: "wiki_search",+ queries: ["attention"],+ })+ })++ it("parses query understanding output", () => {+ expect(parseUnderstanding(+ '{"intent":"mixed","rewrittenQuery":"transformer benchmark","wikiQueries":["transformer"],"graphQueries":["attention transformer"],"externalQueries":["latest transformer benchmark"],"needsWiki":true,"needsGraph":true,"needsExternal":true,"isFollowUp":false,"reason":"needs local and current context"}',+ "fallback",+ { hasProject: true, webSearchEnabled: true, anyTxtSearchEnabled: false },+ )).toMatchObject({+ intent: "mixed",+ rewrittenQuery: "transformer benchmark",+ wikiQueries: ["transformer"],+ graphQueries: ["attention transformer"],+ externalQueries: ["latest transformer benchmark"],+ needsWiki: true,+ needsGraph: true,+ needsExternal: true,+ })+ })++ it("filters tool registry by available capabilities", () => {+ expect(getChatAgentTools({+ hasProject: false,+ webSearchEnabled: true,+ anyTxtSearchEnabled: false,+ }).map((tool) => tool.name)).toEqual(["web_search"])+ expect(getChatAgentTools({+ hasProject: true,+ webSearchEnabled: false,+ anyTxtSearchEnabled: true,+ }).map((tool) => tool.name)).toEqual(["wiki_search", "graph_search", "anytxt_search"])+ })++ it("does not call search tools for direct greetings", async () => {+ const deps: ChatAgentDeps = {+ searchWiki: vi.fn(),+ streamChat: vi.fn(),+ }++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "hello",+ historyMessages: [{ role: "user", content: "hello" }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ deps,+ })++ expect(deps.searchWiki).not.toHaveBeenCalled()+ expect(deps.streamChat).not.toHaveBeenCalled()+ expect(result.references).toEqual([])+ expect(result.messages[0].content).toContain("Do not claim that you searched")+ })++ it("executes wiki search only after the planner chooses it", async () => {+ const searchWiki = vi.fn(async () => [+ {+ path: "/tmp/project/wiki/concepts/attention.md",+ title: "Attention",+ snippet: "Attention lets models focus.",+ titleMatch: true,+ score: 1,+ images: [],+ },+ ])+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"wiki_search","queries":["attention mechanism"],"reason":"needs local notes"}')+ callbacks.onDone()+ })+ const deps: ChatAgentDeps = { searchWiki, streamChat }++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "What do my notes say about attention?",+ historyMessages: [{ role: "user", content: "What do my notes say about attention?" }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ deps,+ })++ expect(searchWiki).toHaveBeenCalledWith("/tmp/project", "attention mechanism")+ expect(result.references).toEqual([+ {+ title: "Attention",+ path: "/tmp/project/wiki/concepts/attention.md",+ kind: "wiki",+ },+ ])+ expect(String(result.messages[0].content)).toContain("## Retrieved Context")+ expect(String(result.messages[0].content)).toContain('<context id="1" source="wiki"')+ expect(String(result.messages[0].content)).toContain("Attention lets models focus on relevant tokens")+ expect(String(result.messages[0].content)).toContain("## Wiki Purpose")+ expect(String(result.messages[0].content)).toContain("This wiki tracks transformer research.")+ expect(String(result.messages[0].content)).toContain("## Wiki Overview")+ expect(String(result.messages[0].content)).toContain("This wiki covers transformer architecture")+ expect(String(result.messages[0].content)).toContain("## Wiki Index")+ expect(String(result.messages[result.messages.length - 1]?.content)).toContain("[REMINDER: Write prose in English")+ })++ it("emits detailed agent steps for tool calls and results", async () => {+ const searchWiki = vi.fn(async () => [+ {+ path: "/tmp/project/wiki/concepts/attention.md",+ title: "Attention",+ snippet: "Attention lets models focus.",+ titleMatch: true,+ score: 1,+ images: [],+ },+ ])+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"wiki_search","queries":["attention mechanism"],"reason":"needs local notes"}')+ callbacks.onDone()+ })+ const events: Array<{ stage: string; tool?: string; count?: number }> = []++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "What do my notes say about attention?",+ historyMessages: [{ role: "user", content: "What do my notes say about attention?" }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ onEvent: (event) => events.push(event),+ deps: { searchWiki, streamChat },+ })++ expect(result.steps.map((step) => step.type)).toContain("understanding")+ expect(result.steps).toEqual(expect.arrayContaining([+ expect.objectContaining({ type: "tool_call", tool: "wiki_search", status: "running" }),+ expect.objectContaining({ type: "tool_result", tool: "wiki_search", status: "success", count: 1 }),+ ]))+ expect(events).toEqual(expect.arrayContaining([+ expect.objectContaining({ stage: "tool_call", tool: "wiki_search" }),+ expect.objectContaining({ stage: "tool_result", tool: "wiki_search", count: 1 }),+ ]))+ })++ it("materializes graph results without double-prefixing absolute node paths", async () => {+ const searchWiki = vi.fn(async () => [+ {+ path: "/tmp/project/wiki/concepts/attention.md",+ title: "Attention",+ snippet: "Attention relates to transformers.",+ titleMatch: true,+ score: 1,+ images: [],+ },+ ])+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"graph_search","queries":["attention transformer"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "How is attention connected to transformers?",+ historyMessages: [{ role: "user", content: "How is attention connected to transformers?" }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ deps: { searchWiki, streamChat },+ })++ expect(result.references).toEqual([+ {+ title: "Transformer",+ path: "/tmp/project/wiki/concepts/transformer.md",+ kind: "wiki",+ },+ ])+ expect(String(result.messages[0].content)).toContain("Transformers use attention layers")+ expect(String(result.messages[0].content)).not.toContain("/tmp/project//tmp/project")+ })++ it("uses external search only when the planner selects it", async () => {+ const webSearch = vi.fn(async () => [+ {+ title: "External Paper",+ url: "https://example.com/paper",+ snippet: "A current external result.",+ source: "web",+ },+ ])+ const searchWiki = vi.fn()+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"external_search","queries":["latest transformer benchmark"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "Find the latest transformer benchmark news.",+ historyMessages: [{ role: "user", content: "Find the latest transformer benchmark news." }],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { searchWiki, webSearch, streamChat },+ })++ expect(searchWiki).not.toHaveBeenCalled()+ expect(webSearch).toHaveBeenCalledWith("latest transformer benchmark", expect.any(Object), 5)+ expect(result.references).toEqual([+ {+ title: "External Paper",+ path: "https://example.com/paper",+ kind: "external",+ source: "web",+ url: "https://example.com/paper",+ snippet: "A current external result.",+ },+ ])+ expect(String(result.messages[0].content)).toContain("[E1]")+ expect(String(result.messages[0].content)).toContain('<context id="E1" source="web"')+ expect(String(result.messages[0].content)).toContain("External search is enabled for this conversation")+ expect(String(result.messages[0].content)).toContain("decide whether they are relevant")+ expect(String(result.messages[0].content)).toContain("If your answer uses any external search fact, include its [E#] citation")+ expect(String(result.messages[0].content)).toContain("Do not rely on the separate References panel")+ })++ it("overrides stale no-internet history when current external search results exist", async () => {+ const webSearch = vi.fn(async () => [+ {+ title: "Current Source",+ url: "https://example.com/current",+ snippet: "Fresh web result that answers the question.",+ source: "web",+ },+ ])+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"external_search","queries":["current source"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "Please search the web and answer.",+ historyMessages: [+ { role: "user", content: "Can you search the web?" },+ { role: "assistant", content: "I cannot access the internet." },+ { role: "user", content: "Please search the web and answer." },+ ],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { webSearch, streamChat },+ })++ const system = String(result.messages[0].content)+ expect(system).toContain("Fresh web result that answers the question.")+ expect(system).toContain("If earlier conversation messages claimed external search was unavailable, ignore that stale claim")+ expect(system).toContain("Do not apologize for lacking web access")+ })++ it("can reuse recent external retrieval history without repeating web search", async () => {+ const webSearch = vi.fn()+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"finish","queries":[],"reason":"history is enough"}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "What were the main points from the external source?",+ historyMessages: [{ role: "user", content: "What were the main points from the external source?" }],+ retrievalHistory: [+ {+ title: "External Paper",+ path: "https://example.com/paper",+ kind: "external",+ source: "web",+ url: "https://example.com/paper",+ snippet: "A prior external result.",+ },+ ],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { webSearch, streamChat },+ })++ expect(webSearch).not.toHaveBeenCalled()+ expect(result.references).toEqual([+ {+ title: "External Paper",+ path: "https://example.com/paper",+ kind: "external",+ source: "web",+ url: "https://example.com/paper",+ snippet: "A prior external result.",+ },+ ])+ expect(String(result.messages[0].content)).toContain("## Retrieved Context")+ expect(String(result.messages[0].content)).toContain('<context id="E1" source="web"')+ expect(String(result.messages[0].content)).toContain("A prior external result.")+ })++ it("lets follow-up understanding reach the planner so retrieval history can be reused", async () => {+ const webSearch = vi.fn()+ let call = 0+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ call += 1+ callbacks.onToken(call === 1+ ? '{"intent":"follow_up","rewrittenQuery":"main points","wikiQueries":[],"graphQueries":[],"externalQueries":[],"needsWiki":false,"needsGraph":false,"needsExternal":false,"isFollowUp":true}'+ : '{"action":"finish","queries":[],"reason":"history is enough"}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "What were the main points?",+ historyMessages: [{ role: "user", content: "What were the main points?" }],+ retrievalHistory: [+ {+ title: "External Paper",+ path: "https://example.com/paper",+ kind: "external",+ source: "web",+ url: "https://example.com/paper",+ snippet: "A prior external result.",+ },+ ],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { webSearch, streamChat },+ })++ expect(streamChat).toHaveBeenCalledTimes(2)+ expect(webSearch).not.toHaveBeenCalled()+ expect(result.references).toHaveLength(1)+ expect(String(result.messages[0].content)).toContain("A prior external result.")+ })++ it("tells the planner when web search is enabled for the turn", async () => {+ const plannerUserPrompts: string[] = []+ const streamChat = vi.fn(async (_cfg, messages: LLMMessage[], callbacks) => {+ plannerUserPrompts.push(String(messages.find((msg) => msg.role === "user")?.content ?? ""))+ callbacks.onToken('{"action":"answer","queries":[]}')+ callbacks.onDone()+ })++ await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "What's new in the latest release?",+ historyMessages: [{ role: "user", content: "What's new in the latest release?" }],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { streamChat },+ })++ expect(plannerUserPrompts[0]).toContain("User enabled Web Search for this turn: yes")+ expect(plannerUserPrompts[0]).toContain("User enabled AnyTXT Search for this turn: no")+ expect(plannerUserPrompts[0]).toContain("## Current Wiki Overview")+ expect(plannerUserPrompts[0]).toContain("This wiki covers transformer architecture")+ expect(plannerUserPrompts[1]).toContain("## Current Wiki Overview")+ })++ it("does not run tools when the planner chooses a direct answer", async () => {+ const searchWiki = vi.fn()+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"answer","queries":[],"answer":"This is a normal follow-up."}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "Can you rewrite that more clearly?",+ historyMessages: [{ role: "user", content: "Can you rewrite that more clearly?" }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ deps: { searchWiki, streamChat },+ })++ expect(searchWiki).not.toHaveBeenCalled()+ expect(result.references).toEqual([])+ expect(String(result.messages[0].content)).toContain("Possible answer direction: This is a normal follow-up.")+ expect(String(result.messages[0].content)).not.toContain("Retrieved Context")+ })++ it("deduplicates repeated external retrieval items by url", async () => {+ const webSearch = vi.fn(async () => [+ {+ title: "Same Result A",+ url: "https://example.com/duplicate",+ snippet: "First copy.",+ source: "web",+ },+ {+ title: "Same Result B",+ url: "https://example.com/duplicate",+ snippet: "Second copy.",+ source: "web",+ },+ ])+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"external_search","queries":["duplicate result"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "Search the web for duplicate result.",+ historyMessages: [{ role: "user", content: "Search the web for duplicate result." }],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { webSearch, streamChat },+ })++ expect(result.references).toHaveLength(1)+ expect(String(result.messages[0].content).match(/<context id="E\d+"/g)).toHaveLength(1)+ })++ it("keeps external search errors visible when no source returns results", async () => {+ const webSearch = vi.fn(async () => {+ throw new Error("Search provider unavailable")+ })+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"external_search","queries":["current outage"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig: { provider: "firecrawl", apiKey: "" },+ text: "Search for the current outage.",+ historyMessages: [{ role: "user", content: "Search for the current outage." }],+ dataVersion: 1,+ options: { useWebSearch: true, useAnyTxtSearch: false },+ deps: { webSearch, streamChat },+ })++ expect(result.references).toEqual([])+ expect(String(result.messages[0].content)).toContain("Retrieval status")+ expect(String(result.messages[0].content)).toContain("Search provider unavailable")+ expect(String(result.messages[0].content)).toContain("Do not apologize for lacking web access")+ })++ it("ranks query-overlapping wiki context ahead of weaker graph context", async () => {+ const searchWiki = vi.fn(async (projectPath: string, query: string) => {+ if (query === "attention") {+ return [+ {+ path: `${projectPath}/wiki/concepts/attention.md`,+ title: "Attention",+ snippet: "Attention lets models focus.",+ titleMatch: true,+ score: 1,+ images: [],+ },+ ]+ }+ return []+ })+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ callbacks.onToken('{"action":"multi_search","queries":["attention"]}')+ callbacks.onDone()+ })++ const result = await buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "Explain attention.",+ historyMessages: [{ role: "user", content: "Explain attention." }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ deps: { searchWiki, streamChat },+ })++ const system = String(result.messages[0].content)+ expect(system.indexOf('title="Attention"')).toBeGreaterThan(-1)+ expect(system.indexOf('title="Transformer"')).toBeGreaterThan(system.indexOf('title="Attention"'))+ })++ it("stops the agent flow when the planner request is aborted", async () => {+ const controller = new AbortController()+ const searchWiki = vi.fn()+ const streamChat = vi.fn(async (_cfg, _messages, callbacks) => {+ controller.abort()+ callbacks.onDone()+ })++ await expect(buildChatAgentMessages({+ project: { name: "Demo", path: "/tmp/project" },+ llmConfig,+ searchApiConfig,+ text: "Search my wiki for attention.",+ historyMessages: [{ role: "user", content: "Search my wiki for attention." }],+ dataVersion: 1,+ options: { useWebSearch: false, useAnyTxtSearch: false },+ signal: controller.signal,+ deps: { searchWiki, streamChat },+ })).rejects.toMatchObject({ name: "AbortError" })++ expect(searchWiki).not.toHaveBeenCalled()+ })+})diff --git a/src/lib/chat-agent.ts b/src/lib/chat-agent.tsnew file mode 100644index 00000000..07a9f387--- /dev/null+++ b/src/lib/chat-agent.ts@@ -0,0 +1,1560 @@+import { readFile } from "@/commands/fs"+import { anyTxtSearchSmart } from "@/lib/anytxt-search"+import { computeContextBudget } from "@/lib/context-budget"+import { isGreeting } from "@/lib/greeting-detector"+import { buildRetrievalGraph, getRelatedNodes } from "@/lib/graph-relevance"+import { streamChat, type ChatMessage as LLMMessage } from "@/lib/llm-client"+import { buildLanguageDirective, buildLanguageReminder } from "@/lib/output-language"+import { getFileName, getRelativePath, normalizePath } from "@/lib/path-utils"+import { searchWiki, type SearchResult } from "@/lib/search"+import { resolveSearchConfig, webSearch, type WebSearchResult } from "@/lib/web-search"+import type { LlmConfig, SearchApiConfig } from "@/stores/wiki-store"+import type { MessageReference } from "@/stores/chat-store"++export type ChatAgentAction =+ | "answer"+ | "wiki_search"+ | "graph_search"+ | "external_search"+ | "multi_search"+ | "finish"++export interface ChatAgentDecision {+ action: ChatAgentAction+ queries: string[]+ answer?: string+ reason?: string+}++export type ChatAgentIntent =+ | "chitchat"+ | "follow_up"+ | "rewrite"+ | "kb_search"+ | "graph"+ | "external"+ | "mixed"++export interface ChatQueryUnderstanding {+ intent: ChatAgentIntent+ rewrittenQuery: string+ wikiQueries: string[]+ graphQueries: string[]+ externalQueries: string[]+ needsWiki: boolean+ needsGraph: boolean+ needsExternal: boolean+ isFollowUp: boolean+ reason?: string+}++export interface ChatAgentProject {+ name: string+ path: string+}++export interface ChatAgentOptions {+ useWebSearch: boolean+ useAnyTxtSearch: boolean+}++export interface ChatAgentDeps {+ searchWiki?: typeof searchWiki+ webSearch?: typeof webSearch+ anyTxtSearchSmart?: typeof anyTxtSearchSmart+ streamChat?: typeof streamChat+}++export type ChatAgentEventStage =+ | "understanding"+ | "routing"+ | "tool_call"+ | "tool_result"+ | "searching_wiki"+ | "searching_graph"+ | "searching_web"+ | "searching_anytxt"+ | "reading_context"+ | "writing"++export interface ChatAgentEvent {+ stage: ChatAgentEventStage+ query?: string+ tool?: ChatAgentToolName+ message?: string+ count?: number+ status?: "running" | "success" | "error" | "skipped"+}++export interface ChatAgentInput {+ project: ChatAgentProject | null+ llmConfig: LlmConfig+ searchApiConfig: SearchApiConfig+ text: string+ historyMessages: LLMMessage[]+ retrievalHistory?: MessageReference[]+ dataVersion: number+ options: ChatAgentOptions+ signal?: AbortSignal+ onEvent?: (event: ChatAgentEvent) => void+ deps?: ChatAgentDeps+}++export interface ChatAgentResult {+ messages: LLMMessage[]+ references: MessageReference[]+ queryPages: { title: string; path: string }[]+ plan: ChatAgentDecision[]+ steps: ChatAgentStep[]+}++interface ToolObservation {+ tool: "wiki_search" | "graph_search" | "external_search"+ query: string+ content: string+ references: MessageReference[]+ pages: { title: string; path: string }[]+ items: RetrievedItem[]+ errorCount?: number+}++export type ChatAgentToolName = "wiki_search" | "graph_search" | "web_search" | "anytxt_search"++export interface ChatAgentToolDefinition {+ name: ChatAgentToolName+ action: Extract<ChatAgentAction, "wiki_search" | "graph_search" | "external_search">+ stage: Extract<ChatAgentEventStage, "searching_wiki" | "searching_graph" | "searching_web" | "searching_anytxt">+ label: string+ description: string+ requiresProject?: boolean+}++export interface ChatAgentStep {+ id: string+ type: "understanding" | "routing" | "tool_call" | "tool_result" | "final"+ tool?: ChatAgentToolName+ query?: string+ message?: string+ count?: number+ status?: "running" | "success" | "error" | "skipped"+}++type PageEntry = { title: string; path: string; content: string; priority: number }+type ProjectPromptContext = { purpose: string; index: string; overview: string }+type RetrievedItemKind = "wiki" | "graph" | "external" | "history"++interface RetrievedItem {+ id: string+ kind: RetrievedItemKind+ source: string+ title: string+ path: string+ url?: string+ snippet: string+ content: string+ score: number+ query: string+ reference: MessageReference+ page?: { title: string; path: string }+}++interface RetrievedContext {+ contextText: string+ references: MessageReference[]+ pages: { title: string; path: string }[]+ itemCount: number+}++const MAX_AGENT_ROUNDS = 3+const MAX_TOOL_CONTEXT_CHARS = 48_000++const CHAT_AGENT_TOOL_REGISTRY: ChatAgentToolDefinition[] = [+ {+ name: "wiki_search",+ action: "wiki_search",+ stage: "searching_wiki",+ label: "Wiki Search",+ description: "Search local wiki pages and retrieve relevant page content.",+ requiresProject: true,+ },+ {+ name: "graph_search",+ action: "graph_search",+ stage: "searching_graph",+ label: "Graph Search",+ description: "Inspect relationships between wiki entities, concepts, and pages.",+ requiresProject: true,+ },+ {+ name: "web_search",+ action: "external_search",+ stage: "searching_web",+ label: "Web Search",+ description: "Search enabled web providers for current or external information.",+ },+ {+ name: "anytxt_search",+ action: "external_search",+ stage: "searching_anytxt",+ label: "AnyTXT Search",+ description: "Search external local files indexed by AnyTXT.",+ },+]++export function getChatAgentTools(args: {+ hasProject: boolean+ webSearchEnabled: boolean+ anyTxtSearchEnabled: boolean+}): ChatAgentToolDefinition[] {+ return CHAT_AGENT_TOOL_REGISTRY.filter((tool) => {+ if (tool.requiresProject && !args.hasProject) return false+ if (tool.name === "web_search") return args.webSearchEnabled+ if (tool.name === "anytxt_search") return args.anyTxtSearchEnabled+ return true+ })+}++export function shouldBypassAgentPlanner(text: string): ChatAgentDecision | null {+ const q = text.trim()+ const lower = q.toLowerCase()+ if (!q) return { action: "answer", queries: [], reason: "empty" }++ if (isGreeting(q)) {+ return { action: "answer", queries: [], reason: "greeting" }+ }++ if (/^(继续|接着说|展开|展开一下|详细说说|换个说法|重新说|总结一下|总结上面|翻译|翻译成英文|翻译成中文|这是什么意思|什么意思)[\s\S]{0,30}$/i.test(q)) {+ return { action: "answer", queries: [], reason: "short follow-up" }+ }++ if (+ /^(continue|go on|expand|summari[sz]e|translate|rewrite|rephrase|what do you mean)\b/i.test(lower) &&+ q.length < 120+ ) {+ return { action: "answer", queries: [], reason: "short follow-up" }+ }++ return null+}++export async function buildChatAgentMessages(input: ChatAgentInput): Promise<ChatAgentResult> {+ throwIfAborted(input.signal)+ const deps = {+ searchWiki,+ webSearch,+ anyTxtSearchSmart,+ streamChat,+ ...input.deps,+ }+ const projectPath = input.project ? normalizePath(input.project.path) : ""+ const searchConfig = resolveSearchConfig(input.searchApiConfig)+ const observations: ToolObservation[] = []+ const historicalObservations = await buildHistoricalObservations({+ projectPath,+ references: input.retrievalHistory ?? [],+ llmConfig: input.llmConfig,+ })+ const plan: ChatAgentDecision[] = []+ const steps: ChatAgentStep[] = []+ const executedToolKeys = new Set<string>()+ const enabledTools = getChatAgentTools({+ hasProject: Boolean(input.project),+ webSearchEnabled: input.options.useWebSearch,+ anyTxtSearchEnabled: input.options.useAnyTxtSearch,+ })+ const projectRoutingContext = input.project+ ? await readProjectRoutingContext(projectPath, input.text, input.llmConfig)+ : undefined++ const direct = shouldBypassAgentPlanner(input.text)+ if (direct) {+ plan.push(direct)+ steps.push({+ id: makeStepId(steps),+ type: "understanding",+ message: direct.reason ?? "Direct answer",+ status: "success",+ })+ } else {+ input.onEvent?.({ stage: "understanding", status: "running" })+ const understanding = await understandUserQuery({+ llmConfig: input.llmConfig,+ text: input.text,+ historyMessages: input.historyMessages,+ hasProject: Boolean(input.project),+ projectContext: projectRoutingContext,+ webSearchEnabled: input.options.useWebSearch,+ anyTxtSearchEnabled: input.options.useAnyTxtSearch,+ tools: enabledTools,+ signal: input.signal,+ streamChatImpl: deps.streamChat,+ })+ steps.push({+ id: makeStepId(steps),+ type: "understanding",+ query: understanding.rewrittenQuery,+ message: understanding.reason ?? understanding.intent,+ status: "success",+ })+ input.onEvent?.({+ stage: "understanding",+ query: understanding.rewrittenQuery,+ message: understanding.reason ?? understanding.intent,+ status: "success",+ })++ const understandingDecision = decisionFromUnderstanding(understanding, input.text)+ if (understandingDecision.action === "answer") {+ plan.push(understandingDecision)+ }++ for (let round = 0; round < MAX_AGENT_ROUNDS; round++) {+ if (plan[plan.length - 1]?.action === "answer") break+ throwIfAborted(input.signal)+ const observationsBefore = observations.length+ input.onEvent?.({ stage: "routing", status: "running" })+ const decision = await decideNextAction({+ llmConfig: input.llmConfig,+ text: input.text,+ understanding,+ tools: enabledTools,+ historyMessages: input.historyMessages,+ observations,+ historicalObservations,+ projectName: input.project?.name,+ hasProject: Boolean(input.project),+ projectContext: projectRoutingContext,+ webSearchEnabled: input.options.useWebSearch,+ anyTxtSearchEnabled: input.options.useAnyTxtSearch,+ signal: input.signal,+ streamChatImpl: deps.streamChat,+ })+ throwIfAborted(input.signal)+ plan.push(decision)+ steps.push({+ id: makeStepId(steps),+ type: "routing",+ query: decision.queries.join(" | "),+ message: decision.reason ?? decision.action,+ status: "success",+ })+ input.onEvent?.({+ stage: "routing",+ query: decision.queries.join(" | "),+ message: decision.reason ?? decision.action,+ status: "success",+ })+ if (decision.action === "answer" || decision.action === "finish") break++ const queries = normalizeDecisionQueries(decision, input.text)+ if (queries.length === 0) break+ const toolKey = `${decision.action}:${queries.map((query) => query.toLowerCase()).join("|")}`+ if (executedToolKeys.has(toolKey)) break+ executedToolKeys.add(toolKey)++ if ((decision.action === "wiki_search" || decision.action === "multi_search") && input.project) {+ throwIfAborted(input.signal)+ const tool = enabledTools.find((item) => item.name === "wiki_search")+ if (tool) emitToolCall({ tool, queries, steps, onEvent: input.onEvent })+ const observation = await runWikiSearchTool({+ projectPath,+ queries,+ llmConfig: input.llmConfig,+ searchWikiImpl: deps.searchWiki,+ })+ observations.push(observation)+ if (tool) emitToolResult({ tool, observation, steps, onEvent: input.onEvent })+ }++ if ((decision.action === "graph_search" || decision.action === "multi_search") && input.project) {+ throwIfAborted(input.signal)+ const tool = enabledTools.find((item) => item.name === "graph_search")+ if (tool) emitToolCall({ tool, queries, steps, onEvent: input.onEvent })+ const observation = await runGraphSearchTool({+ projectPath,+ dataVersion: input.dataVersion,+ queries,+ llmConfig: input.llmConfig,+ searchWikiImpl: deps.searchWiki,+ })+ observations.push(observation)+ if (tool) emitToolResult({ tool, observation, steps, onEvent: input.onEvent })+ }++ if ((decision.action === "external_search" || decision.action === "multi_search") && input.options.useWebSearch) {+ throwIfAborted(input.signal)+ const tool = enabledTools.find((item) => item.name === "web_search")+ if (tool) emitToolCall({ tool, queries, steps, onEvent: input.onEvent })+ const observation = await runExternalSearchTool({+ queries,+ searchConfig,+ webSearchImpl: deps.webSearch,+ source: "web",+ })+ observations.push(observation)+ if (tool) emitToolResult({ tool, observation, steps, onEvent: input.onEvent })+ }++ if ((decision.action === "external_search" || decision.action === "multi_search") && input.options.useAnyTxtSearch) {+ throwIfAborted(input.signal)+ const tool = enabledTools.find((item) => item.name === "anytxt_search")+ if (tool) emitToolCall({ tool, queries, steps, onEvent: input.onEvent })+ const observation = await runExternalSearchTool({+ queries,+ searchConfig,+ llmConfig: input.llmConfig,+ projectPath,+ anyTxtSearchSmartImpl: deps.anyTxtSearchSmart,+ source: "anytxt",+ })+ observations.push(observation)+ if (tool) emitToolResult({ tool, observation, steps, onEvent: input.onEvent })+ }++ if (observations.length === observationsBefore) break+ }+ }++ throwIfAborted(input.signal)+ const lastDecision = plan[plan.length - 1]+ const observationsForAnswer = observations.length > 0+ ? observations+ : lastDecision?.action === "finish"+ ? historicalObservations+ : []+ if (observations.length > 0) input.onEvent?.({ stage: "reading_context" })+ const projectContext = input.project && observationsForAnswer.some((obs) => obs.tool === "wiki_search" || obs.tool === "graph_search")+ ? await readProjectPromptContext(projectPath, input.text, input.llmConfig)+ : undefined+ const retrievedContext = buildRetrievedContext(observationsForAnswer, input.text, input.llmConfig)+ throwIfAborted(input.signal)+ input.onEvent?.({ stage: "writing" })+ steps.push({+ id: makeStepId(steps),+ type: "final",+ message: retrievedContext.itemCount > 0 ? "Answer with retrieved context" : "Direct answer",+ count: retrievedContext.references.length,+ status: "success",+ })+ return {+ messages: buildFinalMessages({+ project: input.project,+ text: input.text,+ historyMessages: input.historyMessages,+ observations: observationsForAnswer,+ retrievedContext,+ directAnswerHint: plan.find((item) => item.answer)?.answer,+ projectContext,+ }),+ references: retrievedContext.references,+ queryPages: retrievedContext.pages,+ plan,+ steps,+ }+}++async function decideNextAction(args: {+ llmConfig: LlmConfig+ text: string+ understanding: ChatQueryUnderstanding+ tools: ChatAgentToolDefinition[]+ historyMessages: LLMMessage[]+ observations: ToolObservation[]+ historicalObservations: ToolObservation[]+ projectName?: string+ hasProject: boolean+ projectContext?: ProjectPromptContext+ webSearchEnabled: boolean+ anyTxtSearchEnabled: boolean+ signal?: AbortSignal+ streamChatImpl: typeof streamChat+}): Promise<ChatAgentDecision> {+ const externalEnabled = args.webSearchEnabled || args.anyTxtSearchEnabled+ const toolDescriptions = args.tools.length > 0+ ? args.tools.map((tool) => `- ${tool.name}: ${tool.description}`).join("\n")+ : "- none"+ const system = [+ "You are a routing controller for a local knowledge assistant.",+ "Return JSON only. Do not answer in prose.",+ "",+ "Available actions:",+ "- answer: answer from conversation history or general reasoning without retrieval.",+ "- wiki_search: search the local wiki when the user asks about project/wiki/docs/knowledge.",+ "- graph_search: inspect relationships between entities/concepts/pages.",+ "- external_search: search enabled external information sources.",+ "- multi_search: use more than one retrieval source.",+ "- finish: enough tool observations are available; final answer can be produced.",+ "",+ "Enabled tools:",+ toolDescriptions,+ "",+ "Return shape:",+ "{\"action\":\"answer|wiki_search|graph_search|external_search|multi_search|finish\",\"queries\":[\"short query\"],\"answer\":\"optional direct draft\",\"reason\":\"short reason\"}",+ "",+ "Rules:",+ "- Do not retrieve for greetings, casual chat, translation/rewrite/summarize/follow-up requests about prior assistant messages.",+ "- Use wiki_search for questions about the local wiki, project materials, documents, notes, or remembered knowledge.",+ "- Use the project overview/purpose/index below to judge whether the local wiki is likely to contain the answer.",+ "- If the project overview says the current topic is in scope, prefer wiki_search or multi_search before relying only on external_search.",+ "- Use graph_search for relationships, dependencies, links, entities, concepts, clusters, or graph questions.",+ "- If the user enabled Web Search for this turn, treat that as a strong preference to use external_search or multi_search for current facts, web pages, public docs, product/API details, news, versions, pricing, or anything likely outside the local wiki.",+ "- If the user enabled AnyTXT Search for this turn, treat that as a strong preference to use external_search or multi_search when the answer may depend on external local files outside the wiki.",+ "- Do not repeat external_search for simple follow-ups, summaries, rewrites, translations, or questions that can be answered from already available observations/history, unless the user explicitly asks to search again or asks for newer information.",+ "- Never choose external_search if no external source is enabled.",+ "- If tool observations are already enough, choose finish.",+ "- Keep queries concise and keyword-rich.",+ ].join("\n")++ const recentHistory = args.historyMessages.slice(-6).map((msg) => {+ const content = typeof msg.content === "string"+ ? msg.content+ : msg.content.map((block) => block.type === "text" ? block.text : "[image]").join("\n")+ return `${msg.role}: ${content.slice(0, 1200)}`+ }).join("\n\n")++ const observations = args.observations.map((obs, index) => [+ `Observation ${index + 1}: ${obs.tool}`,+ `Query: ${obs.query}`,+ obs.content.slice(0, 5000),+ ].join("\n")).join("\n\n---\n\n")++ const historyObservations = args.historicalObservations.map((obs, index) => [+ `Historical observation ${index + 1}: ${obs.tool}`,+ `Query: ${obs.query}`,+ obs.content.slice(0, 3000),+ ].join("\n")).join("\n\n---\n\n")++ const user = [+ `Project: ${args.projectName ?? "none"}`,+ `Local wiki available: ${args.hasProject ? "yes" : "no"}`,+ `External search enabled: ${externalEnabled ? "yes" : "no"}`,+ `User enabled Web Search for this turn: ${args.webSearchEnabled ? "yes" : "no"}`,+ `User enabled AnyTXT Search for this turn: ${args.anyTxtSearchEnabled ? "yes" : "no"}`,+ `Query understanding: ${JSON.stringify(args.understanding)}`,+ "",+ formatProjectContextForRouting(args.projectContext),+ "",+ recentHistory ? `Recent conversation:\n${recentHistory}` : "Recent conversation: none",+ "",+ observations ? `Tool observations:\n${observations}` : "Tool observations: none",+ "",+ historyObservations+ ? [+ "Recent retrieval history:",+ historyObservations,+ "Use this only when the current message is a follow-up to the earlier retrieved sources. If it already answers the current follow-up, choose finish instead of repeating the same search.",+ ].join("\n")+ : "Recent retrieval history: none",+ "",+ `Current user message:\n${args.text}`,+ ].join("\n")++ const raw = await collectChatText(args.llmConfig, [+ { role: "system", content: system },+ { role: "user", content: user },+ ], args.streamChatImpl, args.signal, { temperature: 0, max_tokens: 300 })++ return parseDecision(raw, args.text)+}++async function understandUserQuery(args: {+ llmConfig: LlmConfig+ text: string+ historyMessages: LLMMessage[]+ hasProject: boolean+ projectContext?: ProjectPromptContext+ webSearchEnabled: boolean+ anyTxtSearchEnabled: boolean+ tools: ChatAgentToolDefinition[]+ signal?: AbortSignal+ streamChatImpl: typeof streamChat+}): Promise<ChatQueryUnderstanding> {+ const toolDescriptions = args.tools.length > 0+ ? args.tools.map((tool) => `- ${tool.name}: ${tool.description}`).join("\n")+ : "- none"+ const system = [+ "You are the query understanding stage for a local knowledge assistant.",+ "Return JSON only. Do not answer the user.",+ "",+ "Classify the current message and produce search-ready queries.",+ "Return shape:",+ "{\"intent\":\"chitchat|follow_up|rewrite|kb_search|graph|external|mixed\",\"rewrittenQuery\":\"short normalized query\",\"wikiQueries\":[\"...\"],\"graphQueries\":[\"...\"],\"externalQueries\":[\"...\"],\"needsWiki\":true,\"needsGraph\":false,\"needsExternal\":false,\"isFollowUp\":false,\"reason\":\"short reason\"}",+ "",+ "Rules:",+ "- chitchat/follow_up/rewrite should not require retrieval unless the user explicitly asks to search.",+ "- Use kb_search for local wiki/project/document questions.",+ "- Use the project overview/purpose/index below to judge whether the local wiki is likely to contain the answer.",+ "- If the project overview says the current topic is in scope, set needsWiki=true. If current external facts are also useful, use mixed instead of external-only.",+ "- Use graph for relationship/entity/connection questions.",+ "- Use external for current facts, public docs, web pages, product/API details, versions, pricing, or local files outside the wiki when the matching external source is enabled.",+ "- Use mixed when local wiki plus external sources are both useful.",+ "- Keep each query concise and keyword-rich.",+ "",+ "Enabled tools:",+ toolDescriptions,+ ].join("\n")++ const recentHistory = args.historyMessages.slice(-6).map((msg) => {+ const content = typeof msg.content === "string"+ ? msg.content+ : msg.content.map((block) => block.type === "text" ? block.text : "[image]").join("\n")+ return `${msg.role}: ${content.slice(0, 1000)}`+ }).join("\n\n")++ const user = [+ `Local wiki available: ${args.hasProject ? "yes" : "no"}`,+ `Web Search enabled: ${args.webSearchEnabled ? "yes" : "no"}`,+ `AnyTXT Search enabled: ${args.anyTxtSearchEnabled ? "yes" : "no"}`,+ `User enabled Web Search for this turn: ${args.webSearchEnabled ? "yes" : "no"}`,+ `User enabled AnyTXT Search for this turn: ${args.anyTxtSearchEnabled ? "yes" : "no"}`,+ "",+ formatProjectContextForRouting(args.projectContext),+ "",+ recentHistory ? `Recent conversation:\n${recentHistory}` : "Recent conversation: none",+ "",+ `Current user message:\n${args.text}`,+ ].join("\n")++ const raw = await collectChatText(args.llmConfig, [+ { role: "system", content: system },+ { role: "user", content: user },+ ], args.streamChatImpl, args.signal, { temperature: 0, max_tokens: 450 })++ return parseUnderstanding(raw, args.text, {+ hasProject: args.hasProject,+ webSearchEnabled: args.webSearchEnabled,+ anyTxtSearchEnabled: args.anyTxtSearchEnabled,+ })+}++export function parseUnderstanding(+ raw: string,+ fallbackQuery: string,+ availability: { hasProject: boolean; webSearchEnabled: boolean; anyTxtSearchEnabled: boolean },+): ChatQueryUnderstanding {+ const fallback = fallbackUnderstanding(fallbackQuery, availability)+ const text = raw.trim()+ const start = text.indexOf("{")+ const end = text.lastIndexOf("}")+ const json = start >= 0 && end > start ? text.slice(start, end + 1) : text+ try {+ const parsed = JSON.parse(json) as Partial<ChatQueryUnderstanding & ChatAgentDecision>+ const intent = normalizeIntent(parsed.intent)+ const rewrittenQuery = typeof parsed.rewrittenQuery === "string" && parsed.rewrittenQuery.trim()+ ? parsed.rewrittenQuery.trim()+ : fallback.rewrittenQuery+ const wikiQueries = normalizeQueryList(parsed.wikiQueries, rewrittenQuery)+ const graphQueries = normalizeQueryList(parsed.graphQueries, rewrittenQuery)+ const externalQueries = normalizeQueryList(parsed.externalQueries, rewrittenQuery)+ return {+ intent,+ rewrittenQuery,+ wikiQueries,+ graphQueries,+ externalQueries,+ needsWiki: typeof parsed.needsWiki === "boolean" ? parsed.needsWiki : fallback.needsWiki,+ needsGraph: typeof parsed.needsGraph === "boolean" ? parsed.needsGraph : fallback.needsGraph,+ needsExternal: typeof parsed.needsExternal === "boolean" ? parsed.needsExternal : fallback.needsExternal,+ isFollowUp: typeof parsed.isFollowUp === "boolean" ? parsed.isFollowUp : intent === "follow_up",+ reason: typeof parsed.reason === "string" ? parsed.reason.trim() : fallback.reason,+ }+ } catch {+ return fallback+ }+}++function fallbackUnderstanding(+ query: string,+ availability: { hasProject: boolean; webSearchEnabled: boolean; anyTxtSearchEnabled: boolean },+): ChatQueryUnderstanding {+ const lower = query.toLowerCase()+ const needsGraph = /(关系|关联|连接|图谱|依赖|relationship|related|graph|connection|linked)/i.test(query)+ const needsExternal = availability.webSearchEnabled && /(latest|current|today|news|price|version|docs|api|最新|现在|今天|新闻|价格|版本|官方文档)/i.test(lower)+ const needsWiki = availability.hasProject && !needsExternal+ const intent: ChatAgentIntent = needsGraph+ ? "graph"+ : needsExternal && needsWiki+ ? "mixed"+ : needsExternal+ ? "external"+ : needsWiki+ ? "kb_search"+ : "chitchat"+ return {+ intent,+ rewrittenQuery: query.trim(),+ wikiQueries: needsWiki ? [query.trim()] : [],+ graphQueries: needsGraph ? [query.trim()] : [],+ externalQueries: needsExternal || availability.anyTxtSearchEnabled ? [query.trim()] : [],+ needsWiki,+ needsGraph,+ needsExternal: needsExternal || availability.anyTxtSearchEnabled,+ isFollowUp: false,+ reason: "fallback understanding",+ }+}++function normalizeIntent(intent: unknown): ChatAgentIntent {+ switch (intent) {+ case "chitchat":+ case "follow_up":+ case "rewrite":+ case "kb_search":+ case "graph":+ case "external":+ case "mixed":+ return intent+ default:+ return "kb_search"+ }+}++function normalizeQueryList(value: unknown, fallback: string): string[] {+ if (!Array.isArray(value)) return fallback ? [fallback] : []+ const queries = value.map((item) => String(item).trim()).filter(Boolean)+ return (queries.length > 0 ? queries : fallback ? [fallback] : []).slice(0, 5)+}++function decisionFromUnderstanding(understanding: ChatQueryUnderstanding, fallback: string): ChatAgentDecision {+ if (understanding.intent === "chitchat" || understanding.intent === "rewrite") {+ return {+ action: "answer",+ queries: [],+ reason: understanding.reason ?? understanding.intent,+ }+ }+ if (understanding.intent === "follow_up") {+ return {+ action: "finish",+ queries: [],+ reason: understanding.reason ?? understanding.intent,+ }+ }+ if (understanding.needsWiki && understanding.needsGraph || understanding.needsWiki && understanding.needsExternal) {+ return { action: "multi_search", queries: preferredQueries(understanding, fallback), reason: understanding.reason }+ }+ if (understanding.needsGraph) {+ return { action: "graph_search", queries: nonEmptyQueries(understanding.graphQueries, fallback), reason: understanding.reason }+ }+ if (understanding.needsExternal) {+ return { action: "external_search", queries: nonEmptyQueries(understanding.externalQueries, fallback), reason: understanding.reason }+ }+ if (understanding.needsWiki) {+ return { action: "wiki_search", queries: nonEmptyQueries(understanding.wikiQueries, fallback), reason: understanding.reason }+ }+ return { action: "answer", queries: [], reason: understanding.reason }+}++function preferredQueries(understanding: ChatQueryUnderstanding, fallback: string): string[] {+ return nonEmptyQueries([+ ...understanding.wikiQueries,+ ...understanding.graphQueries,+ ...understanding.externalQueries,+ ], fallback)+}++function nonEmptyQueries(queries: string[], fallback: string): string[] {+ const normalized = queries.map((q) => q.trim()).filter(Boolean)+ return (normalized.length > 0 ? normalized : [fallback]).slice(0, 5)+}++export function parseDecision(raw: string, fallbackQuery: string): ChatAgentDecision {+ const text = raw.trim()+ const start = text.indexOf("{")+ const end = text.lastIndexOf("}")+ const json = start >= 0 && end > start ? text.slice(start, end + 1) : text+ try {+ const parsed = JSON.parse(json) as Partial<ChatAgentDecision>+ const action = normalizeAction(parsed.action)+ return {+ action,+ queries: Array.isArray(parsed.queries)+ ? parsed.queries.map((q) => String(q).trim()).filter(Boolean).slice(0, 5)+ : [fallbackQuery],+ answer: typeof parsed.answer === "string" ? parsed.answer.trim() : undefined,+ reason: typeof parsed.reason === "string" ? parsed.reason.trim() : undefined,+ }+ } catch {+ return { action: "wiki_search", queries: [fallbackQuery], reason: "router fallback" }+ }+}++function normalizeAction(action: unknown): ChatAgentAction {+ switch (action) {+ case "answer":+ case "wiki_search":+ case "graph_search":+ case "external_search":+ case "multi_search":+ case "finish":+ return action+ default:+ return "wiki_search"+ }+}++function normalizeDecisionQueries(decision: ChatAgentDecision, fallback: string): string[] {+ const queries = decision.queries.map((q) => q.trim()).filter(Boolean)+ return (queries.length > 0 ? queries : [fallback]).slice(0, 5)+}++async function readProjectRoutingContext(+ projectPath: string,+ query: string,+ llmConfig: LlmConfig,+): Promise<ProjectPromptContext> {+ const { indexBudget } = computeContextBudget(llmConfig.maxContextSize)+ const [purpose, overview, rawIndex] = await Promise.all([+ readFile(`${projectPath}/purpose.md`).catch(() => ""),+ readFile(`${projectPath}/wiki/overview.md`).catch(() => ""),+ readFile(`${projectPath}/wiki/index.md`).catch(() => ""),+ ])+ return {+ purpose: trimForBudget(purpose, 2500),+ overview: trimForBudget(overview, Math.min(7000, Math.max(2500, Math.floor(indexBudget * 0.4)))),+ index: trimRelevantIndex(rawIndex, query, Math.min(5000, Math.max(1800, Math.floor(indexBudget * 0.35)))),+ }+}++function formatProjectContextForRouting(context?: ProjectPromptContext): string {+ if (!context || (!context.purpose.trim() && !context.overview.trim() && !context.index.trim())) {+ return "Project context: none"+ }+ return [+ "Project context for routing:",+ context.purpose.trim() ? `## Project Purpose\n${context.purpose.trim()}` : "",+ context.overview.trim() ? `## Current Wiki Overview\n${context.overview.trim()}` : "",+ context.index.trim() ? `## Wiki Index Signals\n${context.index.trim()}` : "",+ ].filter(Boolean).join("\n\n")+}++function trimRelevantIndex(rawIndex: string, query: string, maxChars: number): string {+ if (!rawIndex.trim()) return ""+ if (rawIndex.length <= maxChars) return rawIndex+ const tokens = tokenizeIndexQuery(query)+ const lines = rawIndex.split("\n")+ const keptLines: string[] = []+ let keptSize = 0+ for (const line of lines) {+ const isHeader = line.startsWith("#")+ const lower = line.toLowerCase()+ const isRelevant = tokens.length === 0 || tokens.some((token) => lower.includes(token))+ if (!isHeader && !isRelevant) continue+ if (keptSize + line.length + 1 > maxChars) continue+ keptLines.push(line)+ keptSize += line.length + 1+ }+ return keptLines.length > 0+ ? `${keptLines.join("\n")}\n\n[...index trimmed to routing-relevant entries...]`+ : rawIndex.slice(0, Math.max(0, maxChars - 40)).trimEnd() + "\n[...index truncated...]"+}++function makeStepId(steps: ChatAgentStep[]): string {+ return `step-${steps.length + 1}`+}++function emitToolCall(args: {+ tool: ChatAgentToolDefinition+ queries: string[]+ steps: ChatAgentStep[]+ onEvent?: (event: ChatAgentEvent) => void+}): void {+ const query = args.queries.join(" | ")+ args.steps.push({+ id: makeStepId(args.steps),+ type: "tool_call",+ tool: args.tool.name,+ query,+ message: args.tool.label,+ status: "running",+ })+ args.onEvent?.({+ stage: "tool_call",+ tool: args.tool.name,+ query,+ message: args.tool.label,+ status: "running",+ })+ args.onEvent?.({+ stage: args.tool.stage,+ tool: args.tool.name,+ query,+ message: args.tool.label,+ status: "running",+ })+}++function emitToolResult(args: {+ tool: ChatAgentToolDefinition+ observation: ToolObservation+ steps: ChatAgentStep[]+ onEvent?: (event: ChatAgentEvent) => void+}): void {+ const count = args.observation.references.length+ const status = args.observation.errorCount && count === 0 ? "error" : "success"+ const message = count > 0+ ? `${args.tool.label}: ${count} result${count === 1 ? "" : "s"}`+ : args.observation.errorCount+ ? `${args.tool.label}: failed`+ : `${args.tool.label}: no results`+ args.steps.push({+ id: makeStepId(args.steps),+ type: "tool_result",+ tool: args.tool.name,+ query: args.observation.query,+ message,+ count,+ status,+ })+ args.onEvent?.({+ stage: "tool_result",+ tool: args.tool.name,+ query: args.observation.query,+ message,+ count,+ status,+ })+}++async function runWikiSearchTool(args: {+ projectPath: string+ queries: string[]+ llmConfig: LlmConfig+ searchWikiImpl: typeof searchWiki+}): Promise<ToolObservation> {+ const { pageBudget, maxPageSize } = computeContextBudget(args.llmConfig.maxContextSize)+ const seen = new Set<string>()+ const results: SearchResult[] = []+ for (const query of args.queries.slice(0, 3)) {+ for (const result of await args.searchWikiImpl(args.projectPath, query)) {+ const key = normalizePath(result.path)+ if (seen.has(key)) continue+ seen.add(key)+ results.push(result)+ if (results.length >= 8) break+ }+ if (results.length >= 8) break+ }++ const pages = await materializePages(args.projectPath, results, Math.min(pageBudget, MAX_TOOL_CONTEXT_CHARS), maxPageSize)+ return {+ tool: "wiki_search",+ query: args.queries.join(" | "),+ content: formatWikiObservation("Wiki search results", pages, results),+ references: pages.map((page) => ({ title: page.title, path: `${args.projectPath}/${page.path}`, kind: "wiki" as const })),+ pages: pages.map((page) => ({ title: page.title, path: `${args.projectPath}/${page.path}` })),+ items: pages.map((page) => pageToRetrievedItem({+ projectPath: args.projectPath,+ page,+ query: args.queries.join(" | "),+ kind: "wiki",+ source: "wiki",+ score: searchScoreForPage(page, results),+ })),+ errorCount: 0,+ }+}++async function runGraphSearchTool(args: {+ projectPath: string+ dataVersion: number+ queries: string[]+ llmConfig: LlmConfig+ searchWikiImpl: typeof searchWiki+}): Promise<ToolObservation> {+ const base = await args.searchWikiImpl(args.projectPath, args.queries[0] ?? "")+ const graph = await buildRetrievalGraph(args.projectPath, args.dataVersion)+ const candidates = new Map<string, { title: string; path: string; relevance: number }>()+ const hitPaths = new Set(base.slice(0, 6).map((item) => normalizePath(item.path)))++ for (const result of base.slice(0, 6)) {+ const fileName = getFileName(result.path)+ const nodeId = fileName.replace(/\.md$/, "")+ for (const { node, relevance } of getRelatedNodes(nodeId, graph, 5)) {+ if (relevance < 1.5) continue+ if (hitPaths.has(normalizePath(node.path))) continue+ const nodePath = normalizePath(node.path)+ const absoluteNodePath = nodePath.startsWith(`${args.projectPath}/`)+ ? nodePath+ : `${args.projectPath}/${nodePath.replace(/^\/+/, "")}`+ const current = candidates.get(nodePath)+ if (!current || relevance > current.relevance) {+ candidates.set(nodePath, { title: node.title, path: absoluteNodePath, relevance })+ }+ }+ }++ const relatedResults: SearchResult[] = [...candidates.values()]+ .sort((a, b) => b.relevance - a.relevance)+ .slice(0, 8)+ .map((item) => ({+ path: item.path,+ title: item.title,+ snippet: `Graph relevance ${item.relevance.toFixed(2)}`,+ titleMatch: false,+ score: item.relevance,+ images: [],+ }))++ const pages = await materializePages(args.projectPath, relatedResults, Math.min(computeContextBudget(args.llmConfig.maxContextSize).pageBudget, 24_000), 6000)+ return {+ tool: "graph_search",+ query: args.queries.join(" | "),+ content: formatWikiObservation("Graph-related pages", pages, relatedResults),+ references: pages.map((page) => ({ title: page.title, path: `${args.projectPath}/${page.path}`, kind: "wiki" as const })),+ pages: pages.map((page) => ({ title: page.title, path: `${args.projectPath}/${page.path}` })),+ items: pages.map((page) => pageToRetrievedItem({+ projectPath: args.projectPath,+ page,+ query: args.queries.join(" | "),+ kind: "graph",+ source: "graph",+ score: searchScoreForPage(page, relatedResults),+ })),+ errorCount: 0,+ }+}++async function runExternalSearchTool(args: {+ queries: string[]+ searchConfig: SearchApiConfig+ source: "web" | "anytxt"+ llmConfig?: LlmConfig+ projectPath?: string+ webSearchImpl?: typeof webSearch+ anyTxtSearchSmartImpl?: typeof anyTxtSearchSmart+}): Promise<ToolObservation> {+ const results: WebSearchResult[] = []+ const seen = new Set<string>()+ const errors: string[] = []+ for (const query of args.queries.slice(0, 3)) {+ try {+ const batch = args.source === "web"+ ? await args.webSearchImpl?.(query, args.searchConfig, 5) ?? []+ : await args.anyTxtSearchSmartImpl?.(query, args.searchConfig.anyTxt, args.llmConfig, 5, args.projectPath) ?? []+ for (const result of batch) {+ const key = (result.url || `${result.source}:${result.title}:${result.snippet}`).toLowerCase()+ if (seen.has(key)) continue+ seen.add(key)+ results.push(result)+ if (results.length >= 8) break+ }+ } catch (err) {+ errors.push(err instanceof Error ? err.message : String(err))+ }+ if (results.length >= 8) break+ }++ return {+ tool: "external_search",+ query: args.queries.join(" | "),+ content: [+ formatExternalSearchContext(results),+ errors.length > 0 ? `Errors:\n${errors.map((err) => `- ${err}`).join("\n")}` : "",+ ].filter(Boolean).join("\n\n"),+ references: results.map((result) => ({+ title: result.title,+ path: result.url,+ kind: "external" as const,+ source: result.source,+ url: result.url,+ snippet: result.snippet,+ })),+ pages: [],+ items: results.map((result, index) => externalResultToRetrievedItem(result, args.queries.join(" | "), index)),+ errorCount: errors.length,+ }+}++async function buildHistoricalObservations(args: {+ projectPath: string+ references: MessageReference[]+ llmConfig: LlmConfig+}): Promise<ToolObservation[]> {+ const refs = dedupeReferences(args.references).slice(0, 10)+ if (refs.length === 0) return []++ const wikiRefs = refs.filter((ref) => ref.kind !== "external" && ref.path)+ const externalRefs = refs.filter((ref) => ref.kind === "external")+ const observations: ToolObservation[] = []++ if (wikiRefs.length > 0 && args.projectPath) {+ const results: SearchResult[] = wikiRefs.slice(0, 5).map((ref, index) => ({+ path: normalizeReferencePath(args.projectPath, ref.path),+ title: ref.title,+ snippet: ref.snippet ?? "Previously cited local wiki page.",+ titleMatch: false,+ score: 1 / (index + 1),+ images: [],+ }))+ const pages = await materializePages(+ args.projectPath,+ results,+ Math.min(computeContextBudget(args.llmConfig.maxContextSize).pageBudget, 18_000),+ 5000,+ )+ if (pages.length > 0) {+ observations.push({+ tool: "wiki_search",+ query: "recent local references",+ content: formatWikiObservation("Recent local retrieval history", pages, results),+ references: pages.map((page) => ({+ title: page.title,+ path: `${args.projectPath}/${page.path}`,+ kind: "wiki" as const,+ })),+ pages: pages.map((page) => ({ title: page.title, path: `${args.projectPath}/${page.path}` })),+ items: pages.map((page) => pageToRetrievedItem({+ projectPath: args.projectPath,+ page,+ query: "recent local references",+ kind: "history",+ source: "history",+ score: 0.75,+ })),+ errorCount: 0,+ })+ }+ }++ if (externalRefs.length > 0) {+ const results: WebSearchResult[] = externalRefs.slice(0, 8).map((ref) => ({+ title: ref.title,+ url: ref.url ?? ref.path,+ snippet: ref.snippet ?? "",+ source: ref.source ?? "external",+ }))+ observations.push({+ tool: "external_search",+ query: "recent external references",+ content: [+ "# Recent external retrieval history",+ formatExternalSearchContext(results),+ ].filter(Boolean).join("\n\n"),+ references: externalRefs,+ pages: [],+ items: results.map((result, index) => externalResultToRetrievedItem(result, "recent external references", index, "history")),+ errorCount: 0,+ })+ }++ return observations+}++function normalizeReferencePath(projectPath: string, path: string): string {+ const normalized = normalizePath(path)+ if (normalized.startsWith(`${projectPath}/`)) return normalized+ return `${projectPath}/${normalized.replace(/^\/+/, "")}`+}++async function materializePages(+ projectPath: string,+ results: SearchResult[],+ pageBudget: number,+ maxPageSize: number,+): Promise<PageEntry[]> {+ let usedChars = 0+ const pages: PageEntry[] = []+ for (const [index, result] of results.entries()) {+ if (usedChars >= pageBudget) break+ try {+ const raw = await readFile(result.path)+ const relativePath = getRelativePath(result.path, projectPath)+ const truncated = raw.length > maxPageSize+ ? raw.slice(0, maxPageSize) + "\n\n[...truncated...]"+ : raw+ if (usedChars + truncated.length > pageBudget) continue+ usedChars += truncated.length+ pages.push({+ title: result.title,+ path: relativePath,+ content: truncated,+ priority: index,+ })+ } catch {+ // Skip unreadable pages; search snippets remain in the observation.+ }+ }+ return pages+}++function buildFinalMessages(args: {+ project: ChatAgentProject | null+ text: string+ historyMessages: LLMMessage[]+ observations: ToolObservation[]+ retrievedContext: RetrievedContext+ directAnswerHint?: string+ projectContext?: ProjectPromptContext+}): LLMMessage[] {+ const hasTools = args.retrievedContext.itemCount > 0+ const hasExternalObservations = args.observations.some((obs) => obs.tool === "external_search")+ const localRefs = args.retrievedContext.references+ .filter((ref) => ref.kind !== "external")+ const pageList = localRefs.map((ref, index) => `[${index + 1}] ${ref.title} (${ref.path})`).join("\n")++ const system = hasTools+ ? [+ "You are a knowledgeable wiki assistant. Answer using the retrieved context below and conversation history.",+ "If the observations are insufficient, say what is missing instead of inventing facts.",+ "Keep subject boundaries strict: do not apply a claim, limitation, evaluation, benchmark result, or recommendation about one entity/model/product/method to another subject just because they share keywords.",+ "If retrieved context discusses multiple subjects, attribute each claim to the exact subject named in that context block; when uncertain, state the uncertainty instead of generalizing.",+ "Use [[wikilink]] syntax for local wiki pages when relevant.",+ "When a sentence or bullet uses retrieved context, include an inline citation immediately after that claim.",+ "Cite local context blocks with [1], [2]. Cite external context blocks with [E1], [E2].",+ "Do not rely on the separate References panel as a substitute for inline citations in the answer body.",+ hasExternalObservations+ ? [+ "External search is enabled for this conversation, and you can use external search results when they are relevant.",+ "For this answer, external search context blocks are available below; decide whether they are relevant to the user's request and use them when they help.",+ "If your answer uses any external search fact, include its [E#] citation in the same sentence or bullet.",+ "Do not apologize for lacking web access, do not say you cannot browse, and do not say you can only access the local wiki when external context blocks are present.",+ "If earlier conversation messages claimed external search was unavailable, ignore that stale claim for this turn and reason from the current available context.",+ ].join(" ")+ : "",+ "At the VERY END of your response, add a hidden comment listing which local page numbers you used:",+ " <!-- cited: 1, 3 -->",+ "",+ args.project ? `Project: ${args.project.name}` : "",+ args.projectContext?.purpose ? `## Wiki Purpose\n${args.projectContext.purpose}` : "",+ args.projectContext?.overview ? `## Wiki Overview\n${args.projectContext.overview}` : "",+ args.projectContext?.index ? `## Wiki Index\n${args.projectContext.index}` : "",+ pageList ? `## Page List\n${pageList}` : "",+ "",+ args.retrievedContext.contextText,+ "",+ buildLanguageDirective(args.text),+ ].filter(Boolean).join("\n")+ : [+ args.project+ ? `You are a wiki assistant for the project "${args.project.name}".`+ : "You are a helpful assistant.",+ "Answer directly from the conversation. Do not claim that you searched the wiki or external sources.",+ args.directAnswerHint ? `Possible answer direction: ${args.directAnswerHint}` : "",+ buildLanguageReminder(args.text),+ ].filter(Boolean).join("\n")++ return addLanguageReminderToLastUser([+ { role: "system", content: system },+ ...args.historyMessages,+ ], args.text)+}++function buildRetrievedContext(observations: ToolObservation[], query: string, llmConfig: LlmConfig): RetrievedContext {+ const items = fuseRetrievedItems(observations.flatMap((obs) => obs.items), query)+ const { pageBudget } = computeContextBudget(llmConfig.maxContextSize)+ const budget = Math.max(8_000, Math.min(pageBudget, MAX_TOOL_CONTEXT_CHARS))+ const blocks: string[] = []+ const references: MessageReference[] = []+ const pages: { title: string; path: string }[] = []+ let used = 0+ let localIndex = 0+ let externalIndex = 0++ for (const item of items) {+ const isExternal = item.reference.kind === "external"+ const refId = isExternal ? `E${++externalIndex}` : `${++localIndex}`+ const maxItemChars = isExternal ? 1800 : 6500+ const content = trimForBudget(item.content || item.snippet, Math.min(maxItemChars, Math.max(800, budget - used)))+ if (!content.trim()) continue+ const block = [+ `<context id="${refId}" source="${escapeContextAttr(item.source)}" kind="${item.kind}" title="${escapeContextAttr(item.title)}" path="${escapeContextAttr(item.url ?? item.path)}">`,+ content,+ "</context>",+ ].join("\n")+ if (used + block.length > budget && blocks.length > 0) break+ used += block.length+ blocks.push(block)+ references.push(item.reference)+ if (item.page) pages.push(item.page)+ if (used >= budget) break+ }++ return {+ contextText: blocks.length > 0+ ? `## Retrieved Context\n${blocks.join("\n\n---\n\n")}`+ : buildEmptyRetrievalStatus(observations),+ references: dedupeReferences(references),+ pages,+ itemCount: blocks.length || observations.length,+ }+}++function buildEmptyRetrievalStatus(observations: ToolObservation[]): string {+ if (observations.length === 0) return ""+ return [+ "## Retrieved Context",+ observations.map((obs, index) => [+ `<context id="status-${index + 1}" source="${obs.tool}" kind="status" title="Retrieval status" path="">`,+ `Query: ${obs.query}`,+ obs.content || "(no results)",+ "</context>",+ ].join("\n")).join("\n\n---\n\n"),+ ].join("\n")+}++function fuseRetrievedItems(items: RetrievedItem[], query: string): RetrievedItem[] {+ const tokens = tokenizeIndexQuery(query)+ const merged = new Map<string, RetrievedItem>()+ for (const item of items) {+ const key = retrievedItemKey(item)+ const ranked = {+ ...item,+ score: rankRetrievedItem(item, tokens),+ }+ const existing = merged.get(key)+ if (!existing || ranked.score > existing.score || ranked.content.length > existing.content.length) {+ merged.set(key, ranked)+ }+ }+ return [...merged.values()].sort((a, b) => {+ if (b.score !== a.score) return b.score - a.score+ return a.title.localeCompare(b.title)+ })+}++function rankRetrievedItem(item: RetrievedItem, tokens: string[]): number {+ const sourceWeight = item.kind === "wiki"+ ? 1+ : item.kind === "graph"+ ? 0.88+ : item.kind === "external"+ ? 0.92+ : 0.78+ const text = `${item.title}\n${item.snippet}\n${item.path}`.toLowerCase()+ const overlap = tokens.reduce((score, token) => score + (text.includes(token) ? 0.12 : 0), 0)+ const boundedSourceScore = Math.max(0, Math.min(1, item.score))+ return sourceWeight + boundedSourceScore + overlap+}++function retrievedItemKey(item: RetrievedItem): string {+ const locator = item.url || item.path || item.id+ return `${item.reference.kind ?? "wiki"}:${locator}`.toLowerCase()+}++function pageToRetrievedItem(args: {+ projectPath: string+ page: PageEntry+ query: string+ kind: Extract<RetrievedItemKind, "wiki" | "graph" | "history">+ source: string+ score: number+}): RetrievedItem {+ const absolutePath = `${args.projectPath}/${args.page.path}`+ return {+ id: absolutePath,+ kind: args.kind,+ source: args.source,+ title: args.page.title,+ path: absolutePath,+ snippet: args.page.content.slice(0, 400),+ content: args.page.content,+ score: args.score,+ query: args.query,+ reference: { title: args.page.title, path: absolutePath, kind: "wiki" },+ page: { title: args.page.title, path: absolutePath },+ }+}++function externalResultToRetrievedItem(+ result: WebSearchResult,+ query: string,+ index: number,+ kind: Extract<RetrievedItemKind, "external" | "history"> = "external",+): RetrievedItem {+ return {+ id: result.url || `${result.source}:${result.title}:${index}`,+ kind,+ source: result.source,+ title: result.title,+ path: result.url,+ url: result.url,+ snippet: result.snippet,+ content: result.snippet,+ score: 1 / (index + 1),+ query,+ reference: {+ title: result.title,+ path: result.url,+ kind: "external",+ source: result.source,+ url: result.url,+ snippet: result.snippet,+ },+ }+}++function searchScoreForPage(page: PageEntry, results: SearchResult[]): number {+ const normalized = normalizePath(page.path)+ const found = results.find((result) => normalizePath(result.path).endsWith(normalized))+ return found?.score ?? 1 / (page.priority + 1)+}++function trimForBudget(content: string, maxChars: number): string {+ if (content.length <= maxChars) return content+ return `${content.slice(0, Math.max(0, maxChars - 20)).trimEnd()}\n[...truncated...]`+}++function escapeContextAttr(value: string): string {+ return value.replace(/[<>"&]/g, (char) => {+ switch (char) {+ case "<": return "<"+ case ">": return ">"+ case "\"": return """+ case "&": return "&"+ default: return char+ }+ })+}++async function readProjectPromptContext(+ projectPath: string,+ query: string,+ llmConfig: LlmConfig,+): Promise<ProjectPromptContext> {+ const { indexBudget } = computeContextBudget(llmConfig.maxContextSize)+ const [rawIndex, purpose, overview] = await Promise.all([+ readFile(`${projectPath}/wiki/index.md`).catch(() => ""),+ readFile(`${projectPath}/purpose.md`).catch(() => ""),+ readFile(`${projectPath}/wiki/overview.md`).catch(() => ""),+ ])+ if (rawIndex.length <= indexBudget) return { purpose, index: rawIndex, overview }++ const tokens = tokenizeIndexQuery(query)+ const lines = rawIndex.split("\n")+ const keptLines: string[] = []+ let keptSize = 0+ for (const line of lines) {+ const isHeader = line.startsWith("##")+ const lower = line.toLowerCase()+ const isRelevant = tokens.some((token) => lower.includes(token))+ if (!isHeader && !isRelevant) continue+ if (keptSize + line.length + 1 > indexBudget) continue+ keptLines.push(line)+ keptSize += line.length + 1+ }+ const index = keptLines.length > 0+ ? `${keptLines.join("\n")}\n\n[...index trimmed to relevant entries...]`+ : ""+ return { purpose, index, overview }+}++function tokenizeIndexQuery(query: string): string[] {+ return query+ .toLowerCase()+ .split(/[\s,,。!?、;:""''()()\-_/\\·~~…]+/)+ .map((token) => token.trim())+ .filter((token) => token.length > 1)+ .slice(0, 20)+}++function addLanguageReminderToLastUser(messages: LLMMessage[], text: string): LLMMessage[] {+ const reminder = buildLanguageReminder(text)+ if (!reminder) return messages+ const lastIdx = [...messages].reverse().findIndex((msg) => msg.role === "user")+ if (lastIdx < 0) return messages+ const idx = messages.length - 1 - lastIdx+ const target = messages[idx]+ if (!target || target.role !== "user") return messages+ const prefix = `[${reminder}]\n\n`+ const content = typeof target.content === "string"+ ? `${prefix}${target.content}`+ : addTextPrefix(target.content, prefix)+ return [+ ...messages.slice(0, idx),+ { ...target, content },+ ...messages.slice(idx + 1),+ ]+}++function addTextPrefix(content: Exclude<LLMMessage["content"], string>, prefix: string): LLMMessage["content"] {+ const blocks = [...content]+ const firstTextIdx = blocks.findIndex((block) => block.type === "text")+ if (firstTextIdx >= 0) {+ const block = blocks[firstTextIdx]+ if (block.type === "text") blocks[firstTextIdx] = { type: "text", text: `${prefix}${block.text}` }+ return blocks+ }+ return [{ type: "text", text: prefix }, ...blocks]+}++function formatWikiObservation(title: string, pages: PageEntry[], snippets: SearchResult[]): string {+ const pageText = pages.length > 0+ ? pages.map((page, index) => [+ `### [${index + 1}] ${page.title}`,+ `Path: ${page.path}`,+ "",+ page.content,+ ].join("\n")).join("\n\n---\n\n")+ : ""++ const snippetText = snippets.length > 0+ ? snippets.slice(0, 8).map((result, index) => [+ `- ${index + 1}. ${result.title}`,+ ` Path: ${result.path}`,+ ` Snippet: ${result.snippet}`,+ ].join("\n")).join("\n")+ : ""++ return [+ `# ${title}`,+ pageText,+ snippetText ? `## Search snippets\n${snippetText}` : "",+ ].filter(Boolean).join("\n\n")+}++function formatExternalSearchContext(results: WebSearchResult[]): string {+ if (results.length === 0) return ""+ return results.map((result, index) => [+ `### [E${index + 1}] ${result.title}`,+ `Source: ${result.source}`,+ `URL: ${result.url}`,+ "",+ result.snippet,+ ].join("\n")).join("\n\n---\n\n")+}++function dedupeReferences(refs: MessageReference[]): MessageReference[] {+ const seen = new Set<string>()+ const out: MessageReference[] = []+ for (const ref of refs) {+ const key = `${ref.kind ?? "wiki"}:${ref.url ?? ref.path}`.toLowerCase()+ if (seen.has(key)) continue+ seen.add(key)+ out.push(ref)+ }+ return out+}++async function collectChatText(+ llmConfig: LlmConfig,+ messages: LLMMessage[],+ streamChatImpl: typeof streamChat,+ signal?: AbortSignal,+ overrides?: Parameters<typeof streamChat>[4],+): Promise<string> {+ let out = ""+ let error: Error | null = null+ await streamChatImpl(+ llmConfig,+ messages,+ {+ onToken: (token) => { out += token },+ onReasoningToken: () => {},+ onDone: () => {},+ onError: (err) => { error = err },+ },+ signal,+ overrides,+ )+ if (error) throw error+ throwIfAborted(signal)+ return out+}++function throwIfAborted(signal?: AbortSignal): void {+ if (!signal?.aborted) return+ const err = new Error("Chat request aborted")+ err.name = "AbortError"+ throw err+}diff --git a/src/lib/persist.integration.test.ts b/src/lib/persist.integration.test.tsindex 2a986d93..4dc35e87 100644--- a/src/lib/persist.integration.test.ts+++ b/src/lib/persist.integration.test.ts@@ -22,6 +22,8 @@ import {loadLintItems,saveChatHistory,loadChatHistory,+ saveChatPreferences,+ loadChatPreferences,} from "./persist"let tmp: { path: string; cleanup: () => Promise<void> }@@ -260,6 +262,24 @@ describe("chat persistence — round-trip (new format)", () => {expect(loaded).toEqual({ conversations: [], messages: [] })})+ it("round-trips chat search preferences", async () => {+ await saveChatPreferences(tmp.path, { useWebSearch: true, useAnyTxtSearch: false })+ await expect(loadChatPreferences(tmp.path)).resolves.toEqual({+ useWebSearch: true,+ useAnyTxtSearch: false,+ })++ const raw = await readFileRaw(`${tmp.path}/.llm-wiki/chat-preferences.json`)+ expect(raw).toContain('"useWebSearch": true')+ })++ it("defaults chat search preferences to off when no file exists", async () => {+ await expect(loadChatPreferences(tmp.path)).resolves.toEqual({+ useWebSearch: false,+ useAnyTxtSearch: false,+ })+ })+it("skips missing per-conversation files without throwing", async () => {// conversations.json references c1 + c2, but chats/c2.json is missingawait writeFileRaw(diff --git a/src/lib/persist.ts b/src/lib/persist.tsindex cf8ca398..a3fa0456 100644--- a/src/lib/persist.ts+++ b/src/lib/persist.ts@@ -46,6 +46,11 @@ interface PersistedChatData {messages: DisplayMessage[]}+export interface ChatPreferences {+ useWebSearch: boolean+ useAnyTxtSearch: boolean+}+function stripPersistedMessageImages(msg: DisplayMessage): DisplayMessage {if (!msg.images || msg.images.length === 0) return msgconst { images: _images, ...rest } = msg@@ -136,3 +141,26 @@ export async function loadChatHistory(projectPath: string): Promise<PersistedCha}}}++export async function saveChatPreferences(+ projectPath: string,+ preferences: ChatPreferences,+): Promise<void> {+ const pp = normalizePath(projectPath)+ await ensureDir(pp)+ await writeFile(`${pp}/.llm-wiki/chat-preferences.json`, JSON.stringify(preferences, null, 2))+}++export async function loadChatPreferences(projectPath: string): Promise<ChatPreferences> {+ const pp = normalizePath(projectPath)+ try {+ const content = await readFile(`${pp}/.llm-wiki/chat-preferences.json`)+ const parsed = JSON.parse(content) as Partial<ChatPreferences>+ return {+ useWebSearch: parsed.useWebSearch === true,+ useAnyTxtSearch: parsed.useAnyTxtSearch === true,+ }+ } catch {+ return { useWebSearch: false, useAnyTxtSearch: false }+ }+}diff --git a/src/lib/reset-project-state.ts b/src/lib/reset-project-state.tsindex acf6c1cc..c47d261d 100644--- a/src/lib/reset-project-state.ts+++ b/src/lib/reset-project-state.ts@@ -23,6 +23,8 @@ export async function resetProjectState(): Promise<void> {ingestSource: null,isStreaming: false,streamingContent: "",+ useWebSearch: false,+ useAnyTxtSearch: false,})useReviewStore.setState({diff --git a/src/stores/chat-store.ts b/src/stores/chat-store.tsindex 4d35efb8..36177996 100644--- a/src/stores/chat-store.ts+++ b/src/stores/chat-store.ts@@ -49,6 +49,8 @@ interface ChatState {mode: "chat" | "ingest"ingestSource: string | nullmaxHistoryMessages: number+ useWebSearch: boolean+ useAnyTxtSearch: boolean// Conversation managementcreateConversation: () => string@@ -67,6 +69,8 @@ interface ChatState {setIngestSource: (path: string | null) => voidclearMessages: () => voidsetMaxHistoryMessages: (n: number) => void+ setUseWebSearch: (enabled: boolean) => void+ setUseAnyTxtSearch: (enabled: boolean) => voidremoveLastAssistantMessage: () => void // for regenerate: remove last assistant reply// Helpers@@ -93,6 +97,8 @@ export const useChatStore = create<ChatState>((set, get) => ({mode: "chat",ingestSource: null,maxHistoryMessages: 10,+ useWebSearch: false,+ useAnyTxtSearch: false,createConversation: () => {const id = generateConversationId()@@ -232,6 +238,10 @@ export const useChatStore = create<ChatState>((set, get) => ({setMaxHistoryMessages: (maxHistoryMessages) => set({ maxHistoryMessages }),+ setUseWebSearch: (useWebSearch) => set({ useWebSearch }),++ setUseAnyTxtSearch: (useAnyTxtSearch) => set({ useAnyTxtSearch }),+removeLastAssistantMessage: () =>set((state) => {const activeId = state.activeConversationId