Skip to content

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

Improve AnyTXT chat integration

nash_su35783ec1c501
diff --git a/src/components/chat/chat-input.tsx b/src/components/chat/chat-input.tsx
index a8ade743..28e4e997 100644
--- a/src/components/chat/chat-input.tsx
+++ b/src/components/chat/chat-input.tsx
@@ -1,19 +1,34 @@
-import { useRef, useState, useCallback } from "react"
-import { Send, Square } from "lucide-react"
+import { useEffect, useRef, useState, useCallback } from "react"
+import { FileSearch, Globe2, Send, Square } from "lucide-react"
+import { useTranslation } from "react-i18next"
import { Button } from "@/components/ui/button"
+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { isImeComposing } from "@/lib/keyboard-utils"
+export interface ChatSendOptions {
+ useWebSearch: boolean
+ useAnyTxtSearch: boolean
+}
+
interface ChatInputProps {
- onSend: (text: string) => void
+ onSend: (text: string, options: ChatSendOptions) => void
onStop: () => void
isStreaming: boolean
+ anyTxtAvailable?: boolean
placeholder?: string
}
-export function ChatInput({ onSend, onStop, isStreaming, placeholder }: ChatInputProps) {
+export function ChatInput({ onSend, onStop, isStreaming, anyTxtAvailable = true, placeholder }: ChatInputProps) {
+ const { t } = useTranslation()
const [value, setValue] = useState("")
+ const [useWebSearch, setUseWebSearch] = useState(false)
+ const [useAnyTxtSearch, setUseAnyTxtSearch] = useState(false)
const textareaRef = useRef<HTMLTextAreaElement>(null)
+ useEffect(() => {
+ if (!anyTxtAvailable) setUseAnyTxtSearch(false)
+ }, [anyTxtAvailable])
+
const handleInput = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
setValue(e.target.value)
const ta = e.target
@@ -24,12 +39,12 @@ export function ChatInput({ onSend, onStop, isStreaming, placeholder }: ChatInpu
const handleSend = useCallback(() => {
const trimmed = value.trim()
if (!trimmed || isStreaming) return
- onSend(trimmed)
+ onSend(trimmed, { useWebSearch, useAnyTxtSearch })
setValue("")
if (textareaRef.current) {
textareaRef.current.style.height = "auto"
}
- }, [value, isStreaming, onSend])
+ }, [value, isStreaming, onSend, useWebSearch, useAnyTxtSearch])
const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
@@ -46,41 +61,101 @@ export function ChatInput({ onSend, onStop, isStreaming, placeholder }: ChatInpu
[handleSend],
)
+ const searchToggleClass = (active: boolean) =>
+ `inline-flex h-7 items-center gap-1.5 rounded-md border px-2.5 text-xs font-medium transition-colors ${
+ active
+ ? "border-border bg-accent text-foreground shadow-sm"
+ : "border-transparent bg-transparent text-muted-foreground hover:bg-accent/60 hover:text-foreground"
+ } disabled:pointer-events-none disabled:opacity-50`
+
return (
- <div className="flex items-end gap-2 border-t p-3">
- <textarea
- ref={textareaRef}
- value={value}
- dir="auto"
- onChange={handleInput}
- onKeyDown={handleKeyDown}
- placeholder={placeholder ?? "Type a message... (Enter to send, Shift+Enter for newline)"}
- disabled={isStreaming}
- rows={1}
- className="flex-1 resize-none rounded-md border bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
- style={{ maxHeight: "120px", overflowY: "auto" }}
- />
- {isStreaming ? (
- <Button
- variant="destructive"
- size="icon"
- onClick={onStop}
- className="shrink-0"
- title="Stop generation"
- >
- <Square className="h-4 w-4" />
- </Button>
- ) : (
- <Button
- size="icon"
- onClick={handleSend}
- disabled={!value.trim()}
- className="shrink-0"
- title="Send message"
- >
- <Send className="h-4 w-4" />
- </Button>
- )}
+ <div className="border-t bg-background/95 p-3">
+ <div className="rounded-lg border border-border/80 bg-card/80 p-2 shadow-sm ring-1 ring-black/5 focus-within:border-ring/60 focus-within:ring-ring/20 dark:ring-white/5">
+ <textarea
+ ref={textareaRef}
+ value={value}
+ dir="auto"
+ onChange={handleInput}
+ onKeyDown={handleKeyDown}
+ placeholder={placeholder ?? "Type a message... (Enter to send, Shift+Enter for newline)"}
+ disabled={isStreaming}
+ rows={1}
+ className="block w-full resize-none border-0 bg-transparent px-2 py-2 text-sm leading-6 placeholder:text-muted-foreground focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
+ style={{ maxHeight: "120px", overflowY: "auto" }}
+ />
+ <div className="mt-1 flex items-center justify-between gap-3 border-t border-border/50 pt-2">
+ <div className="flex min-w-0 flex-wrap items-center gap-1.5">
+ <button
+ type="button"
+ aria-pressed={useWebSearch}
+ onClick={() => setUseWebSearch((v) => !v)}
+ disabled={isStreaming}
+ className={searchToggleClass(useWebSearch)}
+ >
+ <Globe2 className="h-3.5 w-3.5" />
+ {t("chat.useWebSearch")}
+ <span
+ className={`ml-0.5 h-1.5 w-1.5 rounded-full ${
+ useWebSearch ? "bg-emerald-500" : "bg-muted-foreground/30"
+ }`}
+ />
+ </button>
+ <TooltipProvider delay={0}>
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <span className="inline-flex" />
+ }
+ >
+ <button
+ type="button"
+ aria-pressed={useAnyTxtSearch}
+ onClick={() => setUseAnyTxtSearch((v) => !v)}
+ disabled={isStreaming || !anyTxtAvailable}
+ className={searchToggleClass(useAnyTxtSearch)}
+ >
+ <FileSearch className="h-3.5 w-3.5" />
+ {t("chat.useAnyTxtSearch")}
+ <span
+ className={`ml-0.5 h-1.5 w-1.5 rounded-full ${
+ useAnyTxtSearch ? "bg-emerald-500" : "bg-muted-foreground/30"
+ }`}
+ />
+ </button>
+ </TooltipTrigger>
+ {!anyTxtAvailable && (
+ <TooltipContent side="top" className="max-w-64 whitespace-normal leading-relaxed">
+ {t("chat.enableAnyTxtInSettings")}
+ </TooltipContent>
+ )}
+ </Tooltip>
+ </TooltipProvider>
+ </div>
+ {isStreaming ? (
+ <Button
+ variant="destructive"
+ size="sm"
+ onClick={onStop}
+ className="h-8 shrink-0 gap-1.5 rounded-md px-3"
+ title={t("chat.stopGeneration")}
+ >
+ <Square className="h-3.5 w-3.5" />
+ <span className="hidden sm:inline">{t("chat.stopGeneration")}</span>
+ </Button>
+ ) : (
+ <Button
+ size="sm"
+ onClick={handleSend}
+ disabled={!value.trim()}
+ className="h-8 shrink-0 gap-1.5 rounded-md px-3"
+ title={t("chat.sendMessage")}
+ >
+ <Send className="h-3.5 w-3.5" />
+ <span className="hidden sm:inline">{t("chat.sendMessage")}</span>
+ </Button>
+ )}
+ </div>
+ </div>
</div>
)
}
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
index 2636dc83..b1be512d 100644
--- a/src/components/chat/chat-message.tsx
+++ b/src/components/chat/chat-message.tsx
@@ -7,12 +7,13 @@ 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,
+ TrendingUp, Target, Image as ImageIcon, FileSearch,
} from "lucide-react"
+import { openUrl } from "@tauri-apps/plugin-opener"
import { useWikiStore } from "@/stores/wiki-store"
import { readFile, writeFile, listDirectory } from "@/commands/fs"
import { lastQueryPages } from "@/components/chat/chat-panel"
-import type { DisplayMessage } from "@/stores/chat-store"
+import type { DisplayMessage, MessageReference } from "@/stores/chat-store"
import type { FileNode } from "@/types/wiki"
import { convertLatexToUnicode } from "@/lib/latex-to-unicode"
@@ -265,10 +266,7 @@ function SaveToWikiButton({ content, visible }: { content: string; visible: bool
)
}
-interface CitedPage {
- title: string
- path: string
-}
+type CitedPage = MessageReference
const REF_TYPE_CONFIG: Record<string, { icon: typeof FileText; color: string }> = {
entity: { icon: Users, color: "text-blue-500" },
@@ -282,13 +280,35 @@ const REF_TYPE_CONFIG: Record<string, { icon: typeof FileText; color: string }>
methodology: { icon: BookOpen, color: "text-teal-500" },
overview: { icon: Layout, color: "text-yellow-500" },
clip: { icon: Globe, color: "text-blue-400" },
+ external: { icon: Globe, color: "text-sky-500" },
+ anytxt: { icon: FileSearch, color: "text-emerald-500" },
}
-function getRefType(path: string): string {
+function getRefType(path: string, page?: CitedPage): string {
+ if (page?.kind === "external") {
+ return page.source?.toLowerCase() === "anytxt" ? "anytxt" : "external"
+ }
if (path.includes("raw/sources/")) return "clip"
return inferWikiTypeFromPath(path) ?? "source"
}
+function displayExternalPath(page: CitedPage): string {
+ const raw = page.url || page.path
+ if (!raw) return page.path
+ if (raw.startsWith("file://")) {
+ try {
+ const url = new URL(raw)
+ const decoded = decodeURIComponent(url.pathname)
+ if (/^\/[A-Za-z]:\//.test(decoded)) return decoded.slice(1)
+ if (url.hostname) return `//${url.hostname}${decoded}`
+ return decoded
+ } catch {
+ return raw.replace(/^file:\/\//, "")
+ }
+ }
+ return raw
+}
+
/**
* Markdown image-reference regex used to count `![](url)` occurrences
* in cited pages AND extract the first URL (so the image-badge
@@ -313,6 +333,7 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s
const project = useWikiStore((s) => s.project)
const setSelectedFile = useWikiStore((s) => s.setSelectedFile)
const setFileContent = useWikiStore((s) => s.setFileContent)
+ const setExternalPreview = useWikiStore((s) => s.setExternalPreview)
const setPendingScrollImageSrc = useWikiStore((s) => s.setPendingScrollImageSrc)
const [expanded, setExpanded] = useState(false)
/**
@@ -344,6 +365,9 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s
// Try the path verbatim first, then the same fallback set
// the click-handler uses below — keeps "is the file on
// disk" check consistent across the panel.
+ if (page.kind === "external") {
+ return [page.path, { count: 0, firstUrl: null }] as const
+ }
const id = getFileName(page.path.replace(/^wiki\//, "").replace(/\.md$/, ""))
const candidates = [
`${pp}/${page.path}`,
@@ -448,12 +472,45 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s
</button>
<div className="px-2 pb-1.5">
{visiblePages.map((page, i) => {
- const refType = getRefType(page.path)
+ const refType = getRefType(page.path, page)
const config = REF_TYPE_CONFIG[refType] ?? REF_TYPE_CONFIG.source
const Icon = config.icon
const info = imageInfos[page.path]
const hasImages = (info?.count ?? 0) > 0
const 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")
+ setSelectedFile(previewPath)
+ setFileContent(previewContent)
+ setExternalPreview({
+ title: page.title,
+ path: previewPath,
+ source: page.source ?? "AnyTXT",
+ url: displayPath,
+ snippet: page.snippet ?? "",
+ })
+ return
+ }
+ if (target) {
+ await openUrl(target).catch((err) => {
+ console.warn("[chat refs] failed to open external reference:", err)
+ })
+ }
+ return
+ }
if (!project) return
const pp = normalizePath(project.path)
const id = getFileName(page.path.replace(/^wiki\//, "").replace(/\.md$/, ""))
@@ -487,7 +544,7 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s
<div
key={page.path}
className="flex w-full items-center gap-1.5 rounded text-left"
- title={page.path}
+ title={page.kind === "external" ? `${page.source ?? "External"}: ${page.url ?? page.path}` : page.path}
>
<span className="text-[10px] text-muted-foreground/60 w-4 shrink-0 text-right">[{i + 1}]</span>
{/*
@@ -520,7 +577,19 @@ function CitedReferencesPanel({ content, savedReferences }: { content: string; s
className="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="truncate text-foreground/80">{page.title}</span>
+ <span className="min-w-0 flex-1 truncate text-foreground/80">
+ {page.title}
+ {page.kind === "external" && page.source?.toLowerCase() === "anytxt" && (
+ <span className="mt-0.5 block truncate text-[10px] text-muted-foreground/75">
+ {displayExternalPath(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}
+ </span>
+ )}
</button>
</div>
)
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index 7e7fa61d..0571ca27 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -3,8 +3,8 @@ import { useTranslation } from "react-i18next"
import { BookOpen, Plus, Trash2, MessageSquare } from "lucide-react"
import { Button } from "@/components/ui/button"
import { ChatMessage, StreamingMessage, useSourceFiles } from "./chat-message"
-import { ChatInput } from "./chat-input"
-import { useChatStore, chatMessagesToLLM } from "@/stores/chat-store"
+import { ChatInput, type ChatSendOptions } from "./chat-input"
+import { useChatStore, chatMessagesToLLM, type MessageReference } from "@/stores/chat-store"
import { useWikiStore } from "@/stores/wiki-store"
import { streamChat, type ChatMessage as LLMMessage } from "@/lib/llm-client"
import { executeIngestWrites } from "@/lib/ingest"
@@ -15,10 +15,25 @@ import { normalizePath, getFileName, getRelativePath } from "@/lib/path-utils"
import { getOutputLanguage, 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"
// Store the page mapping from the last query so SourceFilesBar can show which pages were cited
export 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()
@@ -143,6 +158,8 @@ export function ChatPanel() {
const project = useWikiStore((s) => s.project)
const llmConfig = useWikiStore((s) => s.llmConfig)
+ const searchApiConfig = useWikiStore((s) => s.searchApiConfig)
+ const anyTxtAvailable = hasConfiguredAnyTxt(searchApiConfig.anyTxt)
const setFileTree = useWikiStore((s) => s.setFileTree)
const abortRef = useRef<AbortController | null>(null)
@@ -158,7 +175,7 @@ export function ChatPanel() {
}, [activeMessages, streamingContent])
const handleSend = useCallback(
- async (text: string) => {
+ async (text: string, options: ChatSendOptions = { useWebSearch: false, useAnyTxtSearch: false }) => {
// Auto-create a conversation if none is active
let convId = useChatStore.getState().activeConversationId
if (!convId) {
@@ -170,7 +187,7 @@ export function ChatPanel() {
// Build system prompt with wiki context using graph-enhanced retrieval
const systemMessages: LLMMessage[] = []
- let queryRefs: { title: string; path: string }[] = []
+ 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
@@ -213,6 +230,45 @@ export function ChatPanel() {
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) {
@@ -308,6 +364,7 @@ export function ChatPanel() {
const pageList = relevantPages.map((p, i) =>
`[${i + 1}] ${p.title} (${p.path})`
).join("\n")
+ const externalContext = formatExternalSearchContext(externalSearchResults)
const outLang = getOutputLanguage(text)
@@ -317,10 +374,14 @@ export function ChatPanel() {
"You are a knowledgeable wiki assistant. Answer questions based on the wiki content provided below.",
"",
"## Rules",
- "- Answer based ONLY on the numbered wiki pages provided below.",
+ 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.",
"- Use [[wikilink]] syntax to reference wiki pages.",
- "- When citing information, use the page number in brackets, e.g. [1], [2].",
+ 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 -->",
"",
@@ -330,6 +391,10 @@ export function ChatPanel() {
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")}`
+ : "",
"",
"---",
"",
@@ -348,7 +413,15 @@ export function ChatPanel() {
langReminder = buildLanguageReminder(text)
lastQueryPages = relevantPages.map((p) => ({ title: p.title, path: p.path }))
- queryRefs = [...lastQueryPages]
+ 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 ────────────────
@@ -426,7 +499,7 @@ export function ChatPanel() {
controller.signal,
)
},
- [llmConfig, addMessage, setStreaming, appendStreamToken, finalizeStream, createConversation, maxHistoryMessages],
+ [llmConfig, searchApiConfig, addMessage, setStreaming, appendStreamToken, finalizeStream, createConversation, maxHistoryMessages],
)
const handleStop = useCallback(() => {
@@ -536,6 +609,7 @@ export function ChatPanel() {
onSend={handleSend}
onStop={handleStop}
isStreaming={isStreaming}
+ anyTxtAvailable={anyTxtAvailable}
placeholder={
mode === "ingest"
? t("chat.ingestPlaceholder")
diff --git a/src/components/layout/preview-panel.tsx b/src/components/layout/preview-panel.tsx
index dfc79b7d..ace2b928 100644
--- a/src/components/layout/preview-panel.tsx
+++ b/src/components/layout/preview-panel.tsx
@@ -10,6 +10,7 @@ import { getFileName } from "@/lib/path-utils"
export function PreviewPanel() {
const selectedFile = useWikiStore((s) => s.selectedFile)
const fileContent = useWikiStore((s) => s.fileContent)
+ const externalPreview = useWikiStore((s) => s.externalPreview)
const setFileContent = useWikiStore((s) => s.setFileContent)
const setSelectedFile = useWikiStore((s) => s.setSelectedFile)
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
@@ -26,6 +27,10 @@ export function PreviewPanel() {
lastLoadedRef.current = ""
return
}
+ if (externalPreview?.path === selectedFile) {
+ lastLoadedRef.current = fileContent
+ return
+ }
const category = getFileCategory(selectedFile)
@@ -44,7 +49,7 @@ export function PreviewPanel() {
lastLoadedRef.current = ""
setFileContent(`Error loading file: ${err}`)
})
- }, [selectedFile, setFileContent])
+ }, [selectedFile, externalPreview, fileContent, setFileContent])
const writeNow = useCallback((path: string, markdown: string, syncStore = false) => {
writeFile(path, markdown)
@@ -90,7 +95,9 @@ export function PreviewPanel() {
}
const category = getFileCategory(selectedFile)
- const fileName = getFileName(selectedFile)
+ const fileName = externalPreview?.path === selectedFile
+ ? externalPreview.title
+ : getFileName(selectedFile)
return (
<div className="flex h-full flex-col">
@@ -106,7 +113,14 @@ export function PreviewPanel() {
</button>
</div>
<div className="flex-1 min-w-0 overflow-auto">
- {category === "markdown" ? (
+ {externalPreview?.path === selectedFile ? (
+ <ExternalReferencePreview
+ source={externalPreview.source}
+ title={externalPreview.title}
+ path={externalPreview.url}
+ snippet={externalPreview.snippet || fileContent}
+ />
+ ) : category === "markdown" ? (
<WikiEditor
key={selectedFile}
content={fileContent}
@@ -123,3 +137,36 @@ export function PreviewPanel() {
</div>
)
}
+
+function ExternalReferencePreview({
+ source,
+ title,
+ path,
+ snippet,
+}: {
+ source: string
+ title: string
+ path: string
+ snippet: string
+}) {
+ return (
+ <div className="flex h-full flex-col overflow-auto p-6">
+ <div className="mb-4 space-y-2">
+ <div className="flex items-center gap-2">
+ <span className="rounded bg-muted px-1.5 py-0.5 text-[10px] font-medium uppercase text-muted-foreground">
+ {source}
+ </span>
+ <h3 className="truncate text-sm font-medium" title={title}>{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">
+ {path}
+ </div>
+ </div>
+ <div className="min-h-0 flex-1 overflow-auto rounded-lg border border-border/60 bg-background p-4">
+ <pre className="whitespace-pre-wrap break-words font-sans text-sm leading-6">
+ {snippet || "(No preview fragment returned.)"}
+ </pre>
+ </div>
+ </div>
+ )
+}
diff --git a/src/components/settings/sections/web-search-section.tsx b/src/components/settings/sections/web-search-section.tsx
index f78d7157..a4d1a02e 100644
--- a/src/components/settings/sections/web-search-section.tsx
+++ b/src/components/settings/sections/web-search-section.tsx
@@ -147,11 +147,34 @@ export function WebSearchSection() {
{t("settings.sections.webSearch.anyTxtDescription")}
</p>
</div>
- {savedId === "anytxt" && (
- <span className="shrink-0 text-[10px] text-emerald-600">
- {t("settings.sections.webSearch.savedBadge")}
- </span>
- )}
+ <div className="flex shrink-0 items-center gap-2">
+ {savedId === "anytxt" && (
+ <span className="text-[10px] text-emerald-600">
+ {t("settings.sections.webSearch.savedBadge")}
+ </span>
+ )}
+ {anyTxtConfig.enabled && (
+ <span className="rounded-full bg-primary/20 px-1.5 py-0.5 text-[10px] font-medium text-primary">
+ {t("settings.sections.webSearch.activeBadge")}
+ </span>
+ )}
+ <button
+ type="button"
+ onClick={() => updateAnyTxt({ enabled: !anyTxtConfig.enabled })}
+ className={`relative inline-flex h-5 w-9 items-center rounded-full border transition-colors ${
+ anyTxtConfig.enabled
+ ? "border-primary bg-primary"
+ : "border-muted-foreground/30 bg-muted-foreground/20 hover:bg-muted-foreground/30"
+ }`}
+ aria-label={anyTxtConfig.enabled ? t("settings.sections.webSearch.deactivate") : t("settings.sections.webSearch.activate")}
+ >
+ <span
+ className={`inline-block h-3.5 w-3.5 rounded-full bg-white shadow-sm ring-1 ring-black/10 transition-transform ${
+ anyTxtConfig.enabled ? "translate-x-4" : "translate-x-0.5"
+ }`}
+ />
+ </button>
+ </div>
</div>
<div className="grid gap-3 md:grid-cols-2">
<div className="space-y-2">
diff --git a/src/i18n/en.json b/src/i18n/en.json
index 05080c05..35a0e9a7 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -31,7 +31,7 @@
},
"research": {
"title": "Deep Research",
- "notConfigured": "Deep Research sources are not configured. Go to Settings → Deep Research to configure Web Search or AnyTXT.",
+ "notConfigured": "Deep Research sources are not configured. Go to Settings → External Information Sources to configure Web Search or AnyTXT.",
"activeBadge": "{{running}} active{{queued, plural, =0 {} other {, {{queued}} queued}}}",
"inputPlaceholder": "Enter a research topic...",
"emptyTitle": "No research tasks yet",
@@ -134,7 +134,12 @@
"msgCount": "msgs",
"startNewConversation": "Start a new conversation",
"clickNewChatToBegin": "Click \"New Chat\" to begin",
- "typeAMessage": "Type a message..."
+ "typeAMessage": "Type a message...",
+ "useWebSearch": "Web search",
+ "useAnyTxtSearch": "AnyTXT search",
+ "enableAnyTxtInSettings": "Enable AnyTXT in Settings → External Information Sources",
+ "sendMessage": "Send message",
+ "stopGeneration": "Stop generation"
},
"search": {
"title": "Search",
@@ -203,7 +208,7 @@
"llm": "LLM Models",
"embedding": "Embeddings",
"multimodal": "Image Captioning",
- "webSearch": "Deep Research",
+ "webSearch": "External Information Sources",
"network": "Network",
"sourceWatch": "Source Watch",
"scheduledImport": "Scheduled Import",
@@ -338,9 +343,9 @@
"costPoint4": "Prefer a local vision model (Qwen2.5-VL via LM Studio / Ollama) for bulk ingestion; reserve hosted vision APIs for one-off high-stakes documents."
},
"webSearch": {
- "title": "Deep Research",
- "description": "Choose where Deep Research gathers context before synthesis.",
- "deepResearchSources": "Research sources",
+ "title": "External Information Sources",
+ "description": "Configure external information sources that LLM Wiki can use.",
+ "deepResearchSources": "Deep Research source mode",
"deepResearchSourcesHint": "Web Search uses the selected online provider. AnyTXT searches local indexed files through ATGUI.exe on this machine.",
"sourceWeb": "Web Search",
"sourceAnyTxt": "AnyTXT",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 54c47e49..a1ac1e03 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -31,7 +31,7 @@
},
"research": {
"title": "深度研究",
- "notConfigured": "深度研究信息来源尚未配置。请到设置 → 深度研究中配置网页搜索或 AnyTXT。",
+ "notConfigured": "深度研究信息来源尚未配置。请到设置 → 外部信息源中配置网页搜索或 AnyTXT。",
"activeBadge": "{{running}} 个运行中{{queued, plural, =0 {} other {,{{queued}} 个排队中}}}",
"inputPlaceholder": "输入研究主题...",
"emptyTitle": "还没有研究任务",
@@ -134,7 +134,12 @@
"msgCount": "条消息",
"startNewConversation": "开始新对话",
"clickNewChatToBegin": "点击「新建对话」开始",
- "typeAMessage": "输入消息..."
+ "typeAMessage": "输入消息...",
+ "useWebSearch": "网页搜索",
+ "useAnyTxtSearch": "AnyTXT 搜索",
+ "enableAnyTxtInSettings": "请在设置 → 外部信息源中启用 AnyTXT",
+ "sendMessage": "发送消息",
+ "stopGeneration": "停止生成"
},
"search": {
"title": "搜索",
@@ -203,7 +208,7 @@
"llm": "LLM 模型",
"embedding": "向量嵌入",
"multimodal": "图片描述",
- "webSearch": "深度研究",
+ "webSearch": "外部信息源",
"network": "网络",
"sourceWatch": "资料文件夹监控",
"scheduledImport": "定时导入",
@@ -338,9 +343,9 @@
"costPoint4": "批量 ingest 推荐用本地视觉模型(LM Studio / Ollama 跑 Qwen2.5-VL);托管视觉 API 留给少量高价值文档。"
},
"webSearch": {
- "title": "深度研究",
- "description": "选择 Deep Research 在综合分析前从哪些来源收集上下文。",
- "deepResearchSources": "研究信息来源",
+ "title": "外部信息源",
+ "description": "配置 LLM Wiki 可以使用的外部信息来源。",
+ "deepResearchSources": "深度研究来源模式",
"deepResearchSourcesHint": "网页搜索使用当前选中的在线搜索 Provider。AnyTXT 会通过本机 ATGUI.exe 的本地索引搜索文件内容。",
"sourceWeb": "网页搜索",
"sourceAnyTxt": "AnyTXT",
diff --git a/src/lib/anytxt-search.test.ts b/src/lib/anytxt-search.test.ts
index 694dec68..59d81796 100644
--- a/src/lib/anytxt-search.test.ts
+++ b/src/lib/anytxt-search.test.ts
@@ -1,8 +1,30 @@
import { beforeEach, describe, expect, it, vi } from "vitest"
-import { anyTxtSearch, normalizeAnyTxtConfig } from "./anytxt-search"
+import type { LlmConfig } from "@/stores/wiki-store"
+import {
+ anyTxtSearch,
+ anyTxtSearchSmart,
+ normalizeAnyTxtConfig,
+ parseAnyTxtQueryRewrite,
+ prepareAnyTxtQueries,
+} from "./anytxt-search"
+
+const streamChatMock = vi.hoisted(() => vi.fn())
+
+vi.mock("@/lib/llm-client", () => ({
+ streamChat: streamChatMock,
+}))
const fetchMock = vi.fn<typeof fetch>()
+const llmConfig: LlmConfig = {
+ provider: "custom",
+ apiKey: "test",
+ model: "test-model",
+ ollamaUrl: "",
+ customEndpoint: "http://localhost/v1/chat/completions",
+ maxContextSize: 128000,
+}
+
function jsonResponse(body: unknown, init?: ResponseInit): Response {
return new Response(JSON.stringify(body), {
status: 200,
@@ -14,6 +36,7 @@ function jsonResponse(body: unknown, init?: ResponseInit): Response {
describe("anyTxtSearch", () => {
beforeEach(() => {
fetchMock.mockReset()
+ streamChatMock.mockReset()
vi.stubGlobal("fetch", fetchMock)
})
@@ -199,6 +222,16 @@ describe("anyTxtSearch", () => {
expect(out).toEqual([])
})
+ it("returns no results when AnyTXT is disabled", async () => {
+ const out = await anyTxtSearch("alpha", {
+ enabled: false,
+ endpoint: "http://127.0.0.1:9920",
+ })
+
+ expect(fetchMock).not.toHaveBeenCalled()
+ expect(out).toEqual([])
+ })
+
it("surfaces JSON-RPC errors", async () => {
fetchMock.mockResolvedValueOnce(jsonResponse({
jsonrpc: "2.0",
@@ -256,3 +289,68 @@ describe("anyTxtSearch", () => {
.rejects.toThrow("Check that ATGUI.exe is running")
})
})
+
+describe("AnyTXT query rewrite", () => {
+ it("parses JSON-array query rewrites and deduplicates them", () => {
+ expect(parseAnyTxtQueryRewrite('```json\n["MBR ammonia", "winter nitrification", "MBR ammonia"]\n```'))
+ .toEqual(["MBR ammonia", "winter nitrification"])
+ })
+
+ it("falls back to line-based query parsing", () => {
+ expect(parseAnyTxtQueryRewrite("QUERY: 反硝化除磷\n- 污水处理 冬季 氨氮\n3. MBR nitrification"))
+ .toEqual(["反硝化除磷", "污水处理 冬季 氨氮", "MBR nitrification"])
+ })
+
+ it("prefers rewritten AnyTXT queries when original queries would fill the cap", async () => {
+ streamChatMock.mockImplementationOnce(async (...args: unknown[]) => {
+ const handlers = args[2] as { onToken?: (token: string) => void; onDone?: () => void }
+ handlers.onToken?.('["kw1", "kw2", "kw3"]')
+ handlers.onDone?.()
+ })
+
+ const queries = await prepareAnyTxtQueries(
+ ["q1 long natural language", "q2 long natural language", "q3 long natural language"],
+ llmConfig,
+ )
+
+ expect(queries).toEqual(["kw1", "kw2", "kw3"])
+ })
+
+ it("falls back to original queries when rewrite fails", async () => {
+ streamChatMock.mockRejectedValueOnce(new Error("model offline"))
+
+ const queries = await prepareAnyTxtQueries(["how did the project handle winter ammonia?"], llmConfig)
+
+ expect(queries).toEqual(["how did the project handle winter ammonia?"])
+ })
+
+ it("searches rewritten queries through the smart AnyTXT entry point", async () => {
+ fetchMock.mockReset()
+ vi.stubGlobal("fetch", fetchMock)
+ streamChatMock.mockImplementationOnce(async (...args: unknown[]) => {
+ const handlers = args[2] as { onToken?: (token: string) => void; onDone?: () => void }
+ handlers.onToken?.('["煤矿 安全"]')
+ handlers.onDone?.()
+ })
+ fetchMock.mockResolvedValueOnce(jsonResponse({
+ result: {
+ items: [
+ { path: "/docs/coal.md", snippet: "煤矿安全 local match" },
+ ],
+ },
+ }))
+
+ const out = await anyTxtSearchSmart("请帮我找一下煤矿安全相关资料", {
+ endpoint: "http://127.0.0.1:9920",
+ }, llmConfig, 1, "/Users/me/wiki")
+
+ expect(fetchMock).toHaveBeenCalled()
+ expect(JSON.parse(String(fetchMock.mock.calls[0][1]?.body))).toMatchObject({
+ params: { input: { pattern: "煤矿 安全" } },
+ })
+ expect(out[0]).toMatchObject({
+ title: "coal.md",
+ source: "AnyTXT",
+ })
+ })
+})
diff --git a/src/lib/anytxt-search.ts b/src/lib/anytxt-search.ts
index 0eb5541c..6a8c0d56 100644
--- a/src/lib/anytxt-search.ts
+++ b/src/lib/anytxt-search.ts
@@ -1,12 +1,14 @@
-import type { AnyTxtConfig } from "@/stores/wiki-store"
+import type { AnyTxtConfig, LlmConfig } from "@/stores/wiki-store"
import { getHttpFetch, isFetchNetworkError } from "@/lib/tauri-fetch"
import { normalizePath } from "@/lib/path-utils"
+import { streamChat } from "@/lib/llm-client"
import type { WebSearchResult } from "./web-search"
export const DEFAULT_ANYTXT_ENDPOINT = "http://127.0.0.1:9920"
export const DEFAULT_ANYTXT_FILTER_EXT = "*"
export const DEFAULT_ANYTXT_LIMIT = 20
const ANYTXT_LAST_MODIFY_END = 2147483647 // AnyTXT docs use signed 32-bit Unix timestamps.
+const ANYTXT_QUERY_LIMIT = 3
let nextRpcId = 1
interface AnyTxtRpcResponse {
@@ -23,6 +25,7 @@ interface AnyTxtItem {
export function normalizeAnyTxtConfig(config?: AnyTxtConfig, _projectPath?: string): Required<AnyTxtConfig> {
return {
+ enabled: config?.enabled ?? Boolean(config?.endpoint?.trim()),
endpoint: normalizeAnyTxtEndpoint(config?.endpoint),
filterDir: config?.filterDir?.trim() || defaultAnyTxtFilterDir(_projectPath),
filterExt: config?.filterExt?.trim() || DEFAULT_ANYTXT_FILTER_EXT,
@@ -31,7 +34,8 @@ export function normalizeAnyTxtConfig(config?: AnyTxtConfig, _projectPath?: stri
}
export function hasConfiguredAnyTxt(config?: AnyTxtConfig): boolean {
- return Boolean(normalizeAnyTxtEndpoint(config?.endpoint).trim())
+ const resolved = normalizeAnyTxtConfig(config)
+ return Boolean(resolved.enabled && resolved.endpoint.trim())
}
export async function anyTxtSearch(
@@ -42,6 +46,7 @@ export async function anyTxtSearch(
): Promise<WebSearchResult[]> {
if (!query.trim()) return []
const resolved = normalizeAnyTxtConfig(config, projectPath)
+ if (!resolved.enabled) return []
const limit = Math.min(clampAnyTxtLimit(maxResults), resolved.limit)
const response = await callAnyTxtRpc(resolved.endpoint, "ATRpcServer.Searcher.V1.GetResult", buildAnyTxtSearchInput({
pattern: query,
@@ -72,6 +77,123 @@ export async function anyTxtSearch(
return out.filter((item) => item.url || item.snippet)
}
+export async function anyTxtSearchSmart(
+ query: string | string[],
+ config?: AnyTxtConfig,
+ llmConfig?: LlmConfig,
+ maxResults: number = DEFAULT_ANYTXT_LIMIT,
+ projectPath?: string,
+): Promise<WebSearchResult[]> {
+ const resolved = normalizeAnyTxtConfig(config, projectPath)
+ if (!resolved.enabled) return []
+ const queries = Array.isArray(query) ? query : [query]
+ const preparedQueries = await prepareAnyTxtQueries(queries, llmConfig)
+ const allResults: WebSearchResult[] = []
+ const seen = new Set<string>()
+
+ for (const preparedQuery of preparedQueries) {
+ const results = await anyTxtSearch(preparedQuery, resolved, maxResults, projectPath)
+ for (const result of results) {
+ const key = (result.url || `${result.source}:${result.title}:${result.snippet}`).toLowerCase()
+ if (seen.has(key)) continue
+ seen.add(key)
+ allResults.push(result)
+ if (allResults.length >= maxResults) return allResults
+ }
+ }
+
+ return allResults
+}
+
+export async function prepareAnyTxtQueries(queries: string[], llmConfig?: LlmConfig): Promise<string[]> {
+ const cleanQueries = uniqueAnyTxtQueries(queries)
+ if (cleanQueries.length === 0) return []
+ if (!llmConfig) return cleanQueries
+
+ try {
+ const rewritten = await rewriteAnyTxtQueries(cleanQueries, llmConfig)
+ return uniqueAnyTxtQueries([...rewritten, ...cleanQueries])
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err)
+ console.warn("[AnyTXT] query rewrite failed, using original queries:", message)
+ return cleanQueries
+ }
+}
+
+export async function rewriteAnyTxtQueries(queries: string[], llmConfig: LlmConfig): Promise<string[]> {
+ const cleanQueries = uniqueAnyTxtQueries(queries)
+ if (cleanQueries.length === 0) return []
+
+ const prompt = [
+ "Convert the user's search or research topics into concise AnyTXT local file search keyword queries.",
+ "",
+ "AnyTXT searches local indexed file text. Natural-language questions often fail, so produce keyword-style searches.",
+ "Rules:",
+ "- Return ONLY a JSON array of strings.",
+ "- Produce 1-3 search queries total.",
+ "- Keep proper nouns, filenames, technical terms, dates, abbreviations, and non-English terms.",
+ "- Prefer compact keyword phrases over full questions.",
+ "- Do not add explanations, markdown, comments, or code fences.",
+ "",
+ "User topics:",
+ JSON.stringify(cleanQueries, null, 2),
+ ].join("\n")
+
+ let output = ""
+ await streamChat(
+ llmConfig,
+ [{ role: "user", content: prompt }],
+ {
+ onToken: (token) => { output += token },
+ onDone: () => {},
+ onError: () => {},
+ },
+ undefined,
+ { temperature: 0.1, max_tokens: 512, reasoning: { mode: "off" } },
+ )
+
+ const rewritten = parseAnyTxtQueryRewrite(output)
+ return rewritten.length > 0 ? rewritten : cleanQueries
+}
+
+export function parseAnyTxtQueryRewrite(output: string): string[] {
+ const stripped = output
+ .replace(/```(?:json)?/gi, "")
+ .replace(/```/g, "")
+ .trim()
+
+ const jsonMatch = stripped.match(/\[[\s\S]*\]/)
+ if (jsonMatch) {
+ try {
+ const parsed = JSON.parse(jsonMatch[0])
+ if (Array.isArray(parsed)) {
+ return uniqueAnyTxtQueries(parsed.map((item) => typeof item === "string" ? item : ""))
+ }
+ } catch {
+ // Fall through to line parser.
+ }
+ }
+
+ return uniqueAnyTxtQueries(stripped
+ .split(/\r?\n/)
+ .map((line) => line.replace(/^\s*(?:[-*]|\d+[.)]|QUERY:)\s*/i, "").trim()))
+}
+
+export function uniqueAnyTxtQueries(queries: string[], limit: number = ANYTXT_QUERY_LIMIT): string[] {
+ const seen = new Set<string>()
+ const out: string[] = []
+ for (const raw of queries) {
+ const query = raw.replace(/^["']|["']$/g, "").trim()
+ if (!query) continue
+ const key = query.toLowerCase()
+ if (seen.has(key)) continue
+ seen.add(key)
+ out.push(query)
+ if (out.length >= limit) break
+ }
+ return out
+}
+
async function getAnyTxtFragment(endpoint: string, fid: string, pattern: string): Promise<string> {
const response = await callAnyTxtRpc(endpoint, "ATRpcServer.Searcher.V1.GetFragment", {
fid,
diff --git a/src/lib/deep-research.test.ts b/src/lib/deep-research.test.ts
index 84076f42..4ee4ed1b 100644
--- a/src/lib/deep-research.test.ts
+++ b/src/lib/deep-research.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from "vitest"
-import { collectResearchSources, parseAnyTxtQueryRewrite } from "./deep-research"
+import { collectResearchSources } from "./deep-research"
import type { SearchApiConfig } from "@/stores/wiki-store"
import type { WebSearchResult } from "./web-search"
@@ -60,6 +60,7 @@ describe("collectResearchSources", () => {
expect(webSearch).not.toHaveBeenCalled()
expect(anyTxtSearch).toHaveBeenCalledTimes(1)
+ expect(anyTxtSearch.mock.calls[0][0]).toEqual(["alpha"])
expect(out.results).toEqual([localResult])
})
@@ -141,62 +142,6 @@ describe("collectResearchSources", () => {
expect(out.results).toEqual([])
})
- it("uses original and rewritten AnyTXT queries without changing Web Search queries", async () => {
- const webSearch = vi.fn().mockResolvedValue([webResult])
- const anyTxtSearch = vi.fn().mockResolvedValue([localResult])
-
- await collectResearchSources(
- ["how did the membrane bioreactor project handle winter ammonia spikes?"],
- config({
- deepResearchSource: "both",
- provider: "tavily",
- apiKey: "tvly",
- anyTxt: { endpoint: "http://127.0.0.1:9920" },
- }),
- "/project",
- { webSearch, anyTxtSearch },
- { anyTxtQueries: ["membrane bioreactor winter ammonia"] },
- )
-
- expect(webSearch).toHaveBeenCalledWith(
- "how did the membrane bioreactor project handle winter ammonia spikes?",
- expect.any(Object),
- 5,
- )
- expect(anyTxtSearch).toHaveBeenNthCalledWith(
- 1,
- "membrane bioreactor winter ammonia",
- expect.any(Object),
- 5,
- "/project",
- )
- expect(anyTxtSearch).toHaveBeenNthCalledWith(
- 2,
- "how did the membrane bioreactor project handle winter ammonia spikes?",
- expect.any(Object),
- 5,
- "/project",
- )
- })
-
- it("prefers rewritten AnyTXT queries when original queries would fill the cap", async () => {
- const webSearch = vi.fn().mockResolvedValue([])
- const anyTxtSearch = vi.fn().mockResolvedValue([])
-
- await collectResearchSources(
- ["q1 long natural language", "q2 long natural language", "q3 long natural language"],
- config({
- deepResearchSource: "anytxt",
- anyTxt: { endpoint: "http://127.0.0.1:9920" },
- }),
- "/project",
- { webSearch, anyTxtSearch },
- { anyTxtQueries: ["kw1", "kw2", "kw3"] },
- )
-
- expect(anyTxtSearch.mock.calls.map((call) => call[0])).toEqual(["kw1", "kw2", "kw3"])
- })
-
it("logs once when research sources are capped", async () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {})
const webSearch = vi.fn().mockResolvedValue(
@@ -221,15 +166,3 @@ describe("collectResearchSources", () => {
infoSpy.mockRestore()
})
})
-
-describe("parseAnyTxtQueryRewrite", () => {
- it("parses JSON-array query rewrites and deduplicates them", () => {
- expect(parseAnyTxtQueryRewrite('```json\n["MBR ammonia", "winter nitrification", "MBR ammonia"]\n```'))
- .toEqual(["MBR ammonia", "winter nitrification"])
- })
-
- it("falls back to line-based query parsing", () => {
- expect(parseAnyTxtQueryRewrite("QUERY: 反硝化除磷\n- 污水处理 冬季 氨氮\n3. MBR nitrification"))
- .toEqual(["反硝化除磷", "污水处理 冬季 氨氮", "MBR nitrification"])
- })
-})
diff --git a/src/lib/deep-research.ts b/src/lib/deep-research.ts
index 70957526..40ccea76 100644
--- a/src/lib/deep-research.ts
+++ b/src/lib/deep-research.ts
@@ -1,4 +1,4 @@
-import { anyTxtSearch } from "./anytxt-search"
+import { anyTxtSearchSmart, hasConfiguredAnyTxt } from "./anytxt-search"
import { hasConfiguredSearchProvider, resolveSearchConfig, webSearch } from "./web-search"
import { streamChat } from "./llm-client"
import { autoIngest } from "./ingest"
@@ -12,11 +12,11 @@ const MAX_RESEARCH_SOURCES = 20
interface ResearchSourceDeps {
webSearch: typeof webSearch
- anyTxtSearch: typeof anyTxtSearch
+ anyTxtSearch: typeof anyTxtSearchSmart
}
interface CollectResearchSourceOptions {
- anyTxtQueries?: string[]
+ llmConfig?: LlmConfig
}
interface ResearchSourceCollection {
@@ -53,13 +53,13 @@ export async function collectResearchSources(
queries: string[],
searchConfig: SearchApiConfig,
projectPath: string,
- deps: ResearchSourceDeps = { webSearch, anyTxtSearch },
+ deps: ResearchSourceDeps = { webSearch, anyTxtSearch: anyTxtSearchSmart },
options: CollectResearchSourceOptions = {},
): Promise<ResearchSourceCollection> {
const resolvedSearchConfig = resolveSearchConfig(searchConfig)
const sourceMode = resolvedSearchConfig.deepResearchSource ?? "web"
const useWeb = sourceMode === "web" || sourceMode === "both"
- const useAnyTxt = hasAnyTxtSource(resolvedSearchConfig)
+ const useAnyTxt = hasAnyTxtSource(resolvedSearchConfig) && hasConfiguredAnyTxt(resolvedSearchConfig.anyTxt)
const webConfigured = hasConfiguredSearchProvider(resolvedSearchConfig)
const allResults: import("./web-search").WebSearchResult[] = []
const errors: string[] = []
@@ -84,31 +84,25 @@ export async function collectResearchSources(
}
const webQueries = queries.map((q) => q.trim()).filter(Boolean)
- const anyTxtQueries = uniqueQueries([
- ...(options.anyTxtQueries ?? []),
- ...queries,
- ])
- const maxQueryCount = Math.max(webQueries.length, anyTxtQueries.length)
-
- for (let i = 0; i < maxQueryCount; i++) {
- const webQuery = webQueries[i]
- const anyTxtQuery = anyTxtQueries[i]
- const calls: Array<Promise<{ results: import("./web-search").WebSearchResult[] }>> = []
+ const calls: Array<Promise<{ results: import("./web-search").WebSearchResult[] }>> = []
+
+ for (const webQuery of webQueries) {
if (useWeb && webConfigured && webQuery) {
calls.push(deps.webSearch(webQuery, resolvedSearchConfig, 5).then((results) => ({ results })))
}
- if (useAnyTxt && anyTxtQuery) {
- calls.push(deps.anyTxtSearch(anyTxtQuery, resolvedSearchConfig.anyTxt, 5, projectPath).then((results) => ({ results })))
- }
- const settled = await Promise.allSettled(calls)
- for (const item of settled) {
- if (item.status === "fulfilled") {
- addResults(item.value.results)
- } else {
- const message = item.reason instanceof Error ? item.reason.message : String(item.reason)
- errors.push(message)
- console.warn("[DeepResearch] source search failed:", message)
- }
+ }
+ if (useAnyTxt) {
+ calls.push(deps.anyTxtSearch(queries, resolvedSearchConfig.anyTxt, options.llmConfig, 15, projectPath).then((results) => ({ results })))
+ }
+
+ const settled = await Promise.allSettled(calls)
+ for (const item of settled) {
+ if (item.status === "fulfilled") {
+ addResults(item.value.results)
+ } else {
+ const message = item.reason instanceof Error ? item.reason.message : String(item.reason)
+ errors.push(message)
+ console.warn("[DeepResearch] source search failed:", message)
}
}
@@ -120,80 +114,6 @@ function hasAnyTxtSource(searchConfig: SearchApiConfig): boolean {
return sourceMode === "anytxt" || sourceMode === "both"
}
-export async function rewriteAnyTxtQueries(queries: string[], llmConfig: LlmConfig): Promise<string[]> {
- const cleanQueries = queries.map((q) => q.trim()).filter(Boolean)
- if (cleanQueries.length === 0) return []
-
- const prompt = [
- "Convert the user's research topics into concise AnyTXT local file search keyword queries.",
- "",
- "AnyTXT searches local indexed file text. Natural-language questions often fail, so produce keyword-style searches.",
- "Rules:",
- "- Return ONLY a JSON array of strings.",
- "- Produce 1-3 search queries total.",
- "- Keep proper nouns, filenames, technical terms, dates, abbreviations, and non-English terms.",
- "- Prefer compact keyword phrases over full questions.",
- "- Do not add explanations, markdown, comments, or code fences.",
- "",
- "User research topics:",
- JSON.stringify(cleanQueries, null, 2),
- ].join("\n")
-
- let output = ""
- await streamChat(
- llmConfig,
- [{ role: "user", content: prompt }],
- {
- onToken: (token) => { output += token },
- onDone: () => {},
- onError: () => {},
- },
- undefined,
- { temperature: 0.1, max_tokens: 512, reasoning: { mode: "off" } },
- )
-
- const rewritten = parseAnyTxtQueryRewrite(output)
- return rewritten.length > 0 ? rewritten : cleanQueries
-}
-
-export function parseAnyTxtQueryRewrite(output: string): string[] {
- const stripped = output
- .replace(/```(?:json)?/gi, "")
- .replace(/```/g, "")
- .trim()
-
- const jsonMatch = stripped.match(/\[[\s\S]*\]/)
- if (jsonMatch) {
- try {
- const parsed = JSON.parse(jsonMatch[0])
- if (Array.isArray(parsed)) {
- return uniqueQueries(parsed.map((item) => typeof item === "string" ? item : ""))
- }
- } catch {
- // fall through to line parser
- }
- }
-
- return uniqueQueries(stripped
- .split(/\r?\n/)
- .map((line) => line.replace(/^\s*(?:[-*]|\d+[.)]|QUERY:)\s*/i, "").trim()))
-}
-
-function uniqueQueries(queries: string[]): string[] {
- const seen = new Set<string>()
- const out: string[] = []
- for (const raw of queries) {
- const query = raw.replace(/^["']|["']$/g, "").trim()
- if (!query) continue
- const key = query.toLowerCase()
- if (seen.has(key)) continue
- seen.add(key)
- out.push(query)
- if (out.length >= 3) break
- }
- return out
-}
-
/**
* Process queued tasks up to maxConcurrent limit.
*/
@@ -232,21 +152,12 @@ async function executeResearch(
const queries = task?.searchQueries && task.searchQueries.length > 0
? task.searchQueries
: [topic]
- let anyTxtQueries: string[] | undefined
- if (hasAnyTxtSource(resolveSearchConfig(searchConfig))) {
- try {
- anyTxtQueries = await rewriteAnyTxtQueries(queries, llmConfig)
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err)
- console.warn("[DeepResearch] AnyTXT query rewrite failed, using original queries:", message)
- }
- }
const { results: allResults, errors: sourceErrors } = await collectResearchSources(
queries,
searchConfig,
pp,
- { webSearch, anyTxtSearch },
- { anyTxtQueries },
+ { webSearch, anyTxtSearch: anyTxtSearchSmart },
+ { llmConfig },
)
const webResults = allResults
diff --git a/src/lib/web-search.test.ts b/src/lib/web-search.test.ts
index c66d03a5..9f219e97 100644
--- a/src/lib/web-search.test.ts
+++ b/src/lib/web-search.test.ts
@@ -217,9 +217,16 @@ describe("webSearch", () => {
provider: "none",
apiKey: "",
deepResearchSource: "anytxt",
- anyTxt: { endpoint: "http://127.0.0.1:9920" },
+ anyTxt: { enabled: true, endpoint: "http://127.0.0.1:9920" },
})).toBe(true)
+ expect(hasConfiguredDeepResearchSources({
+ provider: "none",
+ apiKey: "",
+ deepResearchSource: "anytxt",
+ anyTxt: { enabled: false, endpoint: "http://127.0.0.1:9920" },
+ })).toBe(false)
+
expect(hasConfiguredDeepResearchSources({
provider: "none",
apiKey: "",
diff --git a/src/stores/chat-store.ts b/src/stores/chat-store.ts
index 69df20fb..b1118c62 100644
--- a/src/stores/chat-store.ts
+++ b/src/stores/chat-store.ts
@@ -12,6 +12,10 @@ export interface Conversation {
export interface MessageReference {
title: string
path: string
+ kind?: "wiki" | "external"
+ source?: string
+ url?: string
+ snippet?: string
}
export interface DisplayMessage {
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index 80b3d233..0c46605a 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -67,6 +67,7 @@ export interface SearchProviderOverride {
export type SearchProviderConfigs = Partial<Record<Exclude<SearchProvider, "none">, SearchProviderOverride>>
export interface AnyTxtConfig {
+ enabled?: boolean
endpoint?: string
filterDir?: string
filterExt?: string
@@ -260,11 +261,20 @@ export interface ProviderOverride {
export type ProviderConfigs = Record<string, ProviderOverride>
+export interface ExternalPreview {
+ title: string
+ path: string
+ source: string
+ url: string
+ snippet: string
+}
+
interface WikiState {
project: WikiProject | null
fileTree: FileNode[]
selectedFile: string | null
fileContent: string
+ externalPreview: ExternalPreview | null
/**
* One-shot scroll target for the markdown preview. When the user
* clicks an image in search results and chooses "jump to source",
@@ -301,6 +311,7 @@ interface WikiState {
setFileTree: (tree: FileNode[]) => void
setSelectedFile: (path: string | null) => void
setFileContent: (content: string) => void
+ setExternalPreview: (preview: ExternalPreview | null) => void
setPendingScrollImageSrc: (src: string | null) => void
setChatExpanded: (expanded: boolean) => void
setActiveView: (view: WikiState["activeView"]) => void
@@ -323,6 +334,7 @@ export const useWikiStore = create<WikiState>((set) => ({
fileTree: [],
selectedFile: null,
fileContent: "",
+ externalPreview: null,
pendingScrollImageSrc: null,
chatExpanded: false,
activeView: "wiki",
@@ -343,8 +355,9 @@ export const useWikiStore = create<WikiState>((set) => ({
setProject: (project) => set({ project }),
setFileTree: (fileTree) => set({ fileTree }),
- setSelectedFile: (selectedFile) => set({ selectedFile }),
+ setSelectedFile: (selectedFile) => set({ selectedFile, externalPreview: null }),
setFileContent: (fileContent) => set({ fileContent }),
+ setExternalPreview: (externalPreview) => set({ externalPreview }),
setPendingScrollImageSrc: (pendingScrollImageSrc) => set({ pendingScrollImageSrc }),
setChatExpanded: (chatExpanded) => set({ chatExpanded }),
setActiveView: (activeView) => set({ activeView }),
@@ -357,6 +370,7 @@ export const useWikiStore = create<WikiState>((set) => ({
providerConfigs: {},
deepResearchSource: "web",
anyTxt: {
+ enabled: false,
endpoint: "http://127.0.0.1:9920",
filterDir: "",
filterExt: "*",