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

feat: support official local MinerU API

nash_sufdf5a693c540
diff --git a/src/components/settings/sections/mineru-section.tsx b/src/components/settings/sections/mineru-section.tsx
index 5dcee41d..5bf41d36 100644
--- a/src/components/settings/sections/mineru-section.tsx
+++ b/src/components/settings/sections/mineru-section.tsx
@@ -4,7 +4,7 @@ import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Button } from "@/components/ui/button"
import type { SettingsDraft, DraftSetter } from "../settings-types"
-import type { MineruModelVersion } from "@/stores/wiki-store"
+import type { MineruLocalBackend, MineruModelVersion, MineruParseMethod } from "@/stores/wiki-store"
import { testMineruConnection } from "@/lib/mineru"
interface Props {
@@ -26,6 +26,7 @@ export function MineruSection({ draft, setDraft }: Props) {
try {
await testMineruConnection(draft.mineruToken.trim(), {
backend: draft.mineruBackend,
+ localEndpoint: draft.mineruLocalEndpoint,
})
setTestState("success")
} catch (err) {
@@ -43,15 +44,17 @@ export function MineruSection({ draft, setDraft }: Props) {
<p className="text-sm text-muted-foreground mt-1">
{t("settings.sections.mineru.description", {
defaultValue:
- "Use MinerU cloud API for higher quality PDF parsing (tables, formulas, complex layouts)",
- })}
- </p>
- <p className="mt-2 rounded-md border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-xs text-amber-800 dark:text-amber-200">
- {t("settings.sections.mineru.privacyNotice", {
- defaultValue:
- "When enabled, PDF contents are uploaded to MinerU cloud for parsing. Do not enable this for sensitive documents unless you accept that.",
+ "Use MinerU cloud or a self-hosted local service for higher quality PDF parsing (tables, formulas, complex layouts).",
})}
</p>
+ {draft.mineruBackend === "cloud" && (
+ <p className="mt-2 rounded-md border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-xs text-amber-800 dark:text-amber-200">
+ {t("settings.sections.mineru.privacyNotice", {
+ defaultValue:
+ "When enabled, PDF contents are uploaded to MinerU cloud for parsing. Do not enable this for sensitive documents unless you accept that.",
+ })}
+ </p>
+ )}
</div>
<label className="flex items-center gap-2">
@@ -99,7 +102,7 @@ export function MineruSection({ draft, setDraft }: Props) {
/>
<span className="text-sm">
{t("settings.sections.mineru.localBackend", {
- defaultValue: "Local (127.0.0.1:8790) - private, offline",
+ defaultValue: "Official self-hosted mineru-api - private, offline",
})}
</span>
</label>
@@ -127,17 +130,32 @@ export function MineruSection({ draft, setDraft }: Props) {
)}
{draft.mineruBackend === "local" && (
- <div className="rounded-md border border-blue-500/30 bg-blue-500/10 px-3 py-2">
- <p className="text-xs text-blue-800 dark:text-blue-200">
+ <div className="space-y-2">
+ <Label htmlFor="mineru-local-endpoint">
+ {t("settings.sections.mineru.localEndpoint", {
+ defaultValue: "Local service endpoint",
+ })}
+ </Label>
+ <Input
+ id="mineru-local-endpoint"
+ type="url"
+ value={draft.mineruLocalEndpoint}
+ onChange={(e) => {
+ setDraft("mineruLocalEndpoint", e.target.value)
+ setTestState("idle")
+ }}
+ placeholder="http://127.0.0.1:8000"
+ />
+ <p className="text-xs text-muted-foreground">
{t("settings.sections.mineru.localInfo", {
defaultValue:
- "Using local MinerU service on http://127.0.0.1:8790. Make sure the service is running!",
+ "Base URL of an official mineru-api or mineru-router service.",
})}
</p>
</div>
)}
- <div className="space-y-2">
+ {draft.mineruBackend === "cloud" && <div className="space-y-2">
<Label htmlFor="mineru-model">
{t("settings.sections.mineru.model", { defaultValue: "Model Version" })}
</Label>
@@ -165,13 +183,75 @@ export function MineruSection({ draft, setDraft }: Props) {
defaultValue: "PDF parsing supports pipeline and vlm. MinerU-HTML is for HTML files and is not used here.",
})}
</p>
- </div>
- <p className="rounded-md bg-muted/50 px-3 py-2 text-xs text-muted-foreground">
- {t("settings.sections.mineru.testQuotaNotice", {
- defaultValue:
- "Connection test submits a small demo file to MinerU and may consume a small amount of quota.",
- })}
- </p>
+ </div>}
+
+ {draft.mineruBackend === "local" && (
+ <div className="space-y-4">
+ <div className="space-y-2">
+ <Label htmlFor="mineru-local-backend">{t("settings.sections.mineru.parsingBackend")}</Label>
+ <select
+ id="mineru-local-backend"
+ className="w-full rounded-md border bg-background px-3 py-2 text-sm"
+ value={draft.mineruLocalBackend}
+ onChange={(e) => setDraft("mineruLocalBackend", e.target.value as MineruLocalBackend)}
+ >
+ <option value="hybrid-engine">{t("settings.sections.mineru.backendHybrid")}</option>
+ <option value="pipeline">{t("settings.sections.mineru.backendPipeline")}</option>
+ <option value="vlm-engine">{t("settings.sections.mineru.backendVlm")}</option>
+ <option value="hybrid-http-client">{t("settings.sections.mineru.backendHybridHttp")}</option>
+ <option value="vlm-http-client">{t("settings.sections.mineru.backendVlmHttp")}</option>
+ </select>
+ </div>
+
+ {draft.mineruLocalBackend.startsWith("hybrid-") && (
+ <div className="space-y-2">
+ <Label htmlFor="mineru-effort">{t("settings.sections.mineru.effort")}</Label>
+ <select id="mineru-effort" className="w-full rounded-md border bg-background px-3 py-2 text-sm" value={draft.mineruLocalEffort} onChange={(e) => setDraft("mineruLocalEffort", e.target.value as "medium" | "high")}>
+ <option value="medium">{t("settings.sections.mineru.effortMedium")}</option>
+ <option value="high">{t("settings.sections.mineru.effortHigh")}</option>
+ </select>
+ </div>
+ )}
+
+ {(draft.mineruLocalBackend === "pipeline" || draft.mineruLocalBackend.startsWith("hybrid-")) && (
+ <div className="grid gap-4 sm:grid-cols-2">
+ <div className="space-y-2">
+ <Label htmlFor="mineru-language">{t("settings.sections.mineru.ocrLanguage")}</Label>
+ <Input id="mineru-language" value={draft.mineruLocalLanguage} onChange={(e) => setDraft("mineruLocalLanguage", e.target.value)} placeholder="ch" />
+ </div>
+ <div className="space-y-2">
+ <Label htmlFor="mineru-parse-method">{t("settings.sections.mineru.parseMethod")}</Label>
+ <select id="mineru-parse-method" className="w-full rounded-md border bg-background px-3 py-2 text-sm" value={draft.mineruLocalParseMethod} onChange={(e) => setDraft("mineruLocalParseMethod", e.target.value as MineruParseMethod)}>
+ <option value="auto">{t("settings.sections.mineru.parseAuto")}</option>
+ <option value="txt">{t("settings.sections.mineru.parseText")}</option>
+ <option value="ocr">{t("settings.sections.mineru.parseOcr")}</option>
+ </select>
+ </div>
+ </div>
+ )}
+
+ {draft.mineruLocalBackend.endsWith("http-client") && (
+ <div className="space-y-2">
+ <Label htmlFor="mineru-server-url">{t("settings.sections.mineru.serverUrl")}</Label>
+ <Input id="mineru-server-url" type="url" value={draft.mineruLocalServerUrl} onChange={(e) => setDraft("mineruLocalServerUrl", e.target.value)} placeholder="http://127.0.0.1:30000" />
+ </div>
+ )}
+
+ <div className="flex flex-wrap gap-4 text-sm">
+ <label className="flex items-center gap-2"><input type="checkbox" checked={draft.mineruLocalFormulaEnabled} onChange={(e) => setDraft("mineruLocalFormulaEnabled", e.target.checked)} />{t("settings.sections.mineru.formulaParsing")}</label>
+ <label className="flex items-center gap-2"><input type="checkbox" checked={draft.mineruLocalTableEnabled} onChange={(e) => setDraft("mineruLocalTableEnabled", e.target.checked)} />{t("settings.sections.mineru.tableParsing")}</label>
+ <label className="flex items-center gap-2"><input type="checkbox" checked={draft.mineruLocalImageAnalysis} onChange={(e) => setDraft("mineruLocalImageAnalysis", e.target.checked)} />{t("settings.sections.mineru.imageAnalysis")}</label>
+ </div>
+ </div>
+ )}
+ {draft.mineruBackend === "cloud" && (
+ <p className="rounded-md bg-muted/50 px-3 py-2 text-xs text-muted-foreground">
+ {t("settings.sections.mineru.testQuotaNotice", {
+ defaultValue:
+ "Connection test submits a small demo file to MinerU and may consume a small amount of quota.",
+ })}
+ </p>
+ )}
<div className="flex items-center gap-3">
<Button
@@ -180,6 +260,7 @@ export function MineruSection({ draft, setDraft }: Props) {
onClick={handleTest}
disabled={
(draft.mineruBackend === "cloud" && !draft.mineruToken.trim()) ||
+ (draft.mineruBackend === "local" && !draft.mineruLocalEndpoint.trim()) ||
testState === "running"
}
>
diff --git a/src/components/settings/settings-types.ts b/src/components/settings/settings-types.ts
index 7d7e89a4..67f42d28 100644
--- a/src/components/settings/settings-types.ts
+++ b/src/components/settings/settings-types.ts
@@ -1,5 +1,5 @@
import type { CustomApiMode } from "./llm-presets"
-import type { AzureModelFamily, CloseBehavior, MineruModelVersion, ReasoningConfig, SourceWatchConfig } from "@/stores/wiki-store"
+import type { AzureModelFamily, CloseBehavior, MineruEffort, MineruLocalBackend, MineruModelVersion, MineruParseMethod, ReasoningConfig, SourceWatchConfig } from "@/stores/wiki-store"
/**
* Shape of the draft state each section reads from and writes into.
@@ -79,6 +79,15 @@ export interface SettingsDraft {
// MinerU PDF parsing
mineruEnabled: boolean
mineruBackend: "cloud" | "local"
+ mineruLocalEndpoint: string
+ mineruLocalBackend: MineruLocalBackend
+ mineruLocalEffort: MineruEffort
+ mineruLocalParseMethod: MineruParseMethod
+ mineruLocalLanguage: string
+ mineruLocalFormulaEnabled: boolean
+ mineruLocalTableEnabled: boolean
+ mineruLocalImageAnalysis: boolean
+ mineruLocalServerUrl: string
mineruToken: string
mineruModelVersion: MineruModelVersion
diff --git a/src/components/settings/settings-view.tsx b/src/components/settings/settings-view.tsx
index 63a37d5f..2d323995 100644
--- a/src/components/settings/settings-view.tsx
+++ b/src/components/settings/settings-view.tsx
@@ -159,6 +159,16 @@ function initialDraft(
sourceWatchConfig: normalizeSourceWatchConfig(sourceWatch),
mineruEnabled: mineru.enabled,
mineruBackend: mineru.backend || "cloud",
+ mineruLocalEndpoint:
+ mineru.localEndpoint || "http://127.0.0.1:8000",
+ mineruLocalBackend: mineru.localBackend || "hybrid-engine",
+ mineruLocalEffort: mineru.localEffort || "medium",
+ mineruLocalParseMethod: mineru.localParseMethod || "auto",
+ mineruLocalLanguage: mineru.localLanguage || "ch",
+ mineruLocalFormulaEnabled: mineru.localFormulaEnabled !== false,
+ mineruLocalTableEnabled: mineru.localTableEnabled !== false,
+ mineruLocalImageAnalysis: mineru.localImageAnalysis !== false,
+ mineruLocalServerUrl: mineru.localServerUrl || "",
mineruToken: mineru.token,
mineruModelVersion: mineru.modelVersion,
apiEnabled: apiConfig.enabled,
@@ -397,6 +407,15 @@ export function SettingsView() {
const newMineruConfig = {
enabled: draft.mineruEnabled,
backend: draft.mineruBackend,
+ localEndpoint: draft.mineruLocalEndpoint.trim(),
+ localBackend: draft.mineruLocalBackend,
+ localEffort: draft.mineruLocalEffort,
+ localParseMethod: draft.mineruLocalParseMethod,
+ localLanguage: draft.mineruLocalLanguage.trim(),
+ localFormulaEnabled: draft.mineruLocalFormulaEnabled,
+ localTableEnabled: draft.mineruLocalTableEnabled,
+ localImageAnalysis: draft.mineruLocalImageAnalysis,
+ localServerUrl: draft.mineruLocalServerUrl.trim(),
token: draft.mineruToken.trim(),
modelVersion: draft.mineruModelVersion,
}
diff --git a/src/i18n/en.json b/src/i18n/en.json
index fc15b40b..601165ed 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -772,9 +772,32 @@
},
"mineru": {
"title": "MinerU PDF Parser",
- "description": "Use MinerU cloud API for higher quality PDF parsing (tables, formulas, complex layouts). Default uses built-in pdfium extraction.",
+ "description": "Use MinerU cloud or a self-hosted local service for higher quality PDF parsing (tables, formulas, complex layouts). Default uses built-in pdfium extraction.",
"privacyNotice": "When enabled, PDF contents are uploaded to MinerU cloud for parsing. Do not enable this for sensitive documents unless you accept that.",
"enabled": "Enable MinerU",
+ "backend": "Backend",
+ "cloudBackend": "Cloud (mineru.net) - requires token, higher quality",
+ "localBackend": "Official self-hosted mineru-api - private, offline",
+ "localEndpoint": "Local service endpoint",
+ "localInfo": "Base URL of an official mineru-api or mineru-router service.",
+ "parsingBackend": "Parsing backend",
+ "backendHybrid": "Hybrid engine (Recommended)",
+ "backendPipeline": "Pipeline",
+ "backendVlm": "VLM engine",
+ "backendHybridHttp": "Hybrid HTTP client",
+ "backendVlmHttp": "VLM HTTP client",
+ "effort": "Hybrid effort",
+ "effortMedium": "Medium (Faster)",
+ "effortHigh": "High (Image analysis and maximum accuracy)",
+ "ocrLanguage": "OCR language",
+ "parseMethod": "Parse method",
+ "parseAuto": "Auto",
+ "parseText": "Text extraction",
+ "parseOcr": "Force OCR",
+ "serverUrl": "OpenAI-compatible model server URL",
+ "formulaParsing": "Formula parsing",
+ "tableParsing": "Table parsing",
+ "imageAnalysis": "Image analysis",
"token": "API Token",
"tokenHint": "Get your token from mineru.net",
"model": "Model Version",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 2f33f08b..1fdedd50 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -772,9 +772,32 @@
},
"mineru": {
"title": "MinerU PDF 解析",
- "description": "使用 MinerU 云端 API 获得更高质量的 PDF 解析(表格、公式、复杂排版)。默认使用内置 pdfium 提取。",
+ "description": "使用 MinerU 云端或自托管本地服务获得更高质量的 PDF 解析(表格、公式、复杂排版)。默认使用内置 pdfium 提取。",
"privacyNotice": "启用后,PDF 内容会上传到 MinerU 云端进行解析。除非你接受这一点,否则不要用于敏感文档。",
"enabled": "启用 MinerU",
+ "backend": "解析后端",
+ "cloudBackend": "云端(mineru.net)——需要 Token,解析质量更高",
+ "localBackend": "官方自托管 mineru-api——私有、离线",
+ "localEndpoint": "本地服务地址",
+ "localInfo": "填写官方 mineru-api 或 mineru-router 服务的基础地址。",
+ "parsingBackend": "解析后端",
+ "backendHybrid": "Hybrid engine(推荐)",
+ "backendPipeline": "Pipeline",
+ "backendVlm": "VLM engine",
+ "backendHybridHttp": "Hybrid HTTP client",
+ "backendVlmHttp": "VLM HTTP client",
+ "effort": "Hybrid 精度",
+ "effortMedium": "Medium(更快)",
+ "effortHigh": "High(图片分析和最高精度)",
+ "ocrLanguage": "OCR 语言",
+ "parseMethod": "解析方式",
+ "parseAuto": "自动",
+ "parseText": "文本提取",
+ "parseOcr": "强制 OCR",
+ "serverUrl": "OpenAI-compatible 模型服务地址",
+ "formulaParsing": "公式解析",
+ "tableParsing": "表格解析",
+ "imageAnalysis": "图片分析",
"token": "API Token",
"tokenHint": "从 mineru.net 获取您的 Token",
"model": "模型版本",
diff --git a/src/lib/__tests__/llm-providers.test.ts b/src/lib/__tests__/llm-providers.test.ts
index 7ec6930e..f7650b7e 100644
--- a/src/lib/__tests__/llm-providers.test.ts
+++ b/src/lib/__tests__/llm-providers.test.ts
@@ -1,5 +1,11 @@
import { describe, it, expect } from "vitest"
-import { buildAnthropicUrl, parseGoogleLine, parseAnthropicLine, getProviderConfig } from "../llm-providers"
+import {
+ buildAnthropicUrl,
+ deriveAnthropicMaxTokens,
+ getProviderConfig,
+ parseAnthropicLine,
+ parseGoogleLine,
+} from "../llm-providers"
import type { LlmConfig as RealLlmConfig } from "@/stores/wiki-store"
const makeConfig = (overrides: Partial<RealLlmConfig> = {}): RealLlmConfig => ({
@@ -72,6 +78,39 @@ describe("MiniMax Provider", () => {
})
})
+describe("Anthropic output budget", () => {
+ it("derives output tokens from the character response reserve", () => {
+ expect(deriveAnthropicMaxTokens(204_800)).toBe(10_240)
+ })
+
+ it("caps large context windows at the Anthropic default limit", () => {
+ expect(deriveAnthropicMaxTokens(1_000_000)).toBe(16_384)
+ })
+
+ it("never emits a protocol-invalid zero token limit", () => {
+ expect(deriveAnthropicMaxTokens(1)).toBe(1)
+ })
+
+ it("applies the derived default to native and custom Anthropic providers", () => {
+ const native = getProviderConfig(makeConfig({ provider: "anthropic" }))
+ const custom = getProviderConfig(makeConfig({
+ provider: "custom",
+ apiMode: "anthropic_messages",
+ customEndpoint: "https://example.com/anthropic",
+ }))
+
+ expect((native.buildBody([]) as Record<string, unknown>).max_tokens).toBe(10_240)
+ expect((custom.buildBody([]) as Record<string, unknown>).max_tokens).toBe(10_240)
+ })
+
+ it("preserves an explicit caller override", () => {
+ const provider = getProviderConfig(makeConfig({ provider: "anthropic" }))
+ const body = provider.buildBody([], { max_tokens: 777 }) as Record<string, unknown>
+
+ expect(body.max_tokens).toBe(777)
+ })
+})
+
describe("MiniMax provider registration", () => {
it("minimax is a valid provider value in the type union", () => {
const provider: RealLlmConfig["provider"] = "minimax"
diff --git a/src/lib/ingest-source-path-collision.test.ts b/src/lib/ingest-source-path-collision.test.ts
index 5fb8aa13..7736601b 100644
--- a/src/lib/ingest-source-path-collision.test.ts
+++ b/src/lib/ingest-source-path-collision.test.ts
@@ -194,6 +194,12 @@ describe("autoIngest source summary paths", () => {
apiKey: "",
model: "",
},
+ mineruConfig: {
+ enabled: false,
+ backend: "cloud",
+ token: "",
+ modelVersion: "vlm",
+ },
})
})
@@ -669,6 +675,34 @@ describe("autoIngest source summary paths", () => {
updateSpy.mockRestore()
})
+ it("uses a configured local MinerU backend without a cloud token", async () => {
+ if (!tmp) throw new Error("missing temp project")
+ sourceMarkers = ["local mineru source"]
+ await writeFileRaw(`${tmp.path}/raw/sources/project-a/local.pdf`, "pdf fallback text\n")
+ useWikiStore.setState({
+ mineruConfig: {
+ enabled: true,
+ backend: "local",
+ token: "",
+ modelVersion: "vlm",
+ },
+ })
+ mockParseWithMineru.mockResolvedValueOnce({
+ markdown: "local MinerU markdown",
+ savedImages: [],
+ })
+
+ await autoIngest(
+ tmp.path,
+ `${tmp.path}/raw/sources/project-a/local.pdf`,
+ useWikiStore.getState().llmConfig,
+ undefined,
+ "project-a",
+ )
+
+ expect(mockParseWithMineru).toHaveBeenCalled()
+ })
+
it("does not fall back to built-in PDF extraction when MinerU is cancelled", async () => {
if (!tmp) throw new Error("missing temp project")
await writeFileRaw(`${tmp.path}/raw/sources/project-a/cancelled.pdf`, "pdf fallback text\n")
diff --git a/src/lib/ingest.ts b/src/lib/ingest.ts
index 61030bd2..ef42a4a4 100644
--- a/src/lib/ingest.ts
+++ b/src/lib/ingest.ts
@@ -653,7 +653,8 @@ async function autoIngestImpl(
const mineruCfg = useWikiStore.getState().mineruConfig
let mineruSucceeded = false
let mineruSavedImages: SavedImage[] = []
- if (isPdf && mineruCfg.enabled && mineruCfg.token) {
+ const mineruConfigured = mineruCfg.backend === "local" || Boolean(mineruCfg.token)
+ if (isPdf && mineruCfg.enabled && mineruConfigured) {
try {
const cacheDir = sp.substring(0, sp.lastIndexOf("/"))
const cachePath = `${cacheDir}/.cache/${fileName}.txt`
diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.ts
index 5a7c5bf8..13faa9f4 100644
--- a/src/lib/llm-providers.ts
+++ b/src/lib/llm-providers.ts
@@ -799,6 +799,17 @@ function buildGoogleBody(
}
}
+/**
+ * Convert the character-based response reserve into an Anthropic output-token
+ * limit. The lower bound is defensive: settings UI enforces a useful context
+ * size, but migrated or hand-edited persisted configuration must never produce
+ * the protocol-invalid `max_tokens: 0` value.
+ */
+export function deriveAnthropicMaxTokens(maxContextSize: number | undefined): number {
+ const { responseReserve } = computeContextBudget(maxContextSize)
+ return Math.max(1, Math.min(16_384, Math.floor(responseReserve / 3)))
+}
+
export function getProviderConfig(config: LlmConfig): ProviderConfig {
const { provider, apiKey, model, ollamaUrl, customEndpoint } = config
@@ -808,8 +819,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
// at 16 384 to stay within typical per-request limits. Explicit
// overrides from callers (e.g. max_tokens: 300 for routing decisions)
// always take precedence because they are spread after this value.
- const { responseReserve } = computeContextBudget(config.maxContextSize)
- const anthropicBudgetTokens = Math.min(16_384, Math.floor(responseReserve / 3))
+ const anthropicBudgetTokens = deriveAnthropicMaxTokens(config.maxContextSize)
switch (provider) {
case "openai":
diff --git a/src/lib/mineru.test.ts b/src/lib/mineru.test.ts
index a9820505..86adfd49 100644
--- a/src/lib/mineru.test.ts
+++ b/src/lib/mineru.test.ts
@@ -350,6 +350,127 @@ describe("MinerU API helpers", () => {
})
describe("parseWithMineru", () => {
+ it("submits a Windows-path PDF and pipeline mode to a custom local endpoint", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(jsonResponse({
+ task_id: "task/1",
+ status_url: "http://localhost:9000/custom/tasks/task%2F1",
+ result_url: "http://localhost:9000/custom/tasks/task%2F1/result",
+ }, { status: 202 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "completed" }))
+ .mockResolvedValueOnce(jsonResponse({
+ results: { report: { md_content: "# Parsed locally" } },
+ }))
+
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ localEndpoint: "http://localhost:9000/custom/",
+ localBackend: "pipeline",
+ token: "",
+ modelVersion: "pipeline",
+ }, "C:\\documents\\report.pdf")).resolves.toBe("# Parsed locally")
+
+ const form = mockHttpFetch.mock.calls[0]?.[1]?.body as FormData
+ expect(form.get("files")).toBeInstanceOf(Blob)
+ expect(form.get("backend")).toBe("pipeline")
+ expect(form.get("return_md")).toBe("true")
+ expect(mockHttpFetch.mock.calls[0]?.[0]).toBe("http://localhost:9000/custom/tasks")
+ expect(mockHttpFetch.mock.calls[1]?.[0]).toContain("/tasks/task%2F1")
+ expect(mockHttpFetch.mock.calls[2]?.[0]).toContain("/tasks/task%2F1/result")
+ })
+
+ it("rejects oversized local-backend files before reading or uploading", async () => {
+ fsMocks.getFileSize.mockResolvedValueOnce(__mineruTest.MAX_ACCURATE_PARSE_BYTES + 1)
+
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ token: "",
+ modelVersion: "vlm",
+ }, "/tmp/large.pdf")).rejects.toThrow("200 MB")
+
+ expect(fsMocks.readFileAsBase64).not.toHaveBeenCalled()
+ expect(mockHttpFetch).not.toHaveBeenCalled()
+ })
+
+ it("rejects an empty local-backend result instead of caching it as success", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(jsonResponse({ task_id: "task-1" }, { status: 202 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "completed" }))
+ .mockResolvedValueOnce(jsonResponse({ results: { doc: { md_content: " " } } }))
+
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ token: "",
+ modelVersion: "vlm",
+ }, "/tmp/doc.pdf")).rejects.toThrow("empty parsing result")
+ })
+
+ it("saves and rewrites images returned by the official local API", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(jsonResponse({ task_id: "task-1" }, { status: 202 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "completed" }))
+ .mockResolvedValueOnce(jsonResponse({
+ results: {
+ doc: {
+ md_content: "![Chart](images/chart.png)",
+ images: { "chart.png": `data:image/png;base64,${btoa("image bytes")}` },
+ },
+ },
+ }))
+
+ const result = await parseWithMineruResult({
+ enabled: true,
+ backend: "local",
+ token: "",
+ modelVersion: "vlm",
+ localBackend: "hybrid-engine",
+ }, "/tmp/doc.pdf", undefined, undefined, undefined, {
+ projectPath: "/project",
+ sourceSummarySlug: "doc",
+ })
+
+ expect(result.markdown).toBe("![Chart](media/doc/mineru/images/image-1.png)")
+ expect(result.savedImages[0]?.relPath).toBe("media/doc/mineru/images/image-1.png")
+ expect(fsMocks.writeFileBase64).toHaveBeenCalledWith(
+ "/project/wiki/media/doc/mineru/images/image-1.png",
+ btoa("image bytes"),
+ )
+ const form = mockHttpFetch.mock.calls[0]?.[1]?.body as FormData
+ expect(form.get("return_images")).toBe("true")
+ })
+
+ it("requires a model server URL for official HTTP-client backends", async () => {
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ token: "",
+ modelVersion: "vlm",
+ localBackend: "vlm-http-client",
+ localServerUrl: "",
+ }, "/tmp/doc.pdf")).rejects.toThrow("require a model server URL")
+
+ expect(mockHttpFetch).not.toHaveBeenCalled()
+ })
+
+ it("rejects malformed or credential-bearing local endpoints before upload", async () => {
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ localEndpoint: "file:///tmp/mineru",
+ token: "",
+ modelVersion: "pipeline",
+ }, "/tmp/doc.pdf")).rejects.toThrow("HTTP(S)")
+
+ await expect(testMineruConnection("", {
+ backend: "local",
+ localEndpoint: "http://user:pass@localhost:8000",
+ })).rejects.toThrow("without credentials")
+ expect(mockHttpFetch).not.toHaveBeenCalled()
+ })
+
it("rejects unsupported MinerU model versions before reading or uploading", async () => {
await expect(parseWithMineru({
enabled: true,
@@ -636,6 +757,23 @@ describe("parseWithMineru", () => {
})
describe("testMineruConnection", () => {
+ it("checks local health without requiring a cloud token", async () => {
+ mockHttpFetch.mockResolvedValueOnce(jsonResponse({ status: "healthy" }))
+
+ await expect(testMineruConnection("", { backend: "local" })).resolves.toBeUndefined()
+ expect(mockHttpFetch).toHaveBeenCalledWith(
+ "http://127.0.0.1:8000/health",
+ )
+ })
+
+ it("rejects a 200 response that is not a healthy official MinerU service", async () => {
+ mockHttpFetch.mockResolvedValueOnce(jsonResponse({ status: "unhealthy" }))
+
+ await expect(testMineruConnection("", { backend: "local" })).rejects.toThrow(
+ "invalid or unhealthy",
+ )
+ })
+
it("resolves when MinerU accepts the connection test task", async () => {
mockHttpFetch.mockResolvedValueOnce(jsonResponse({
code: "0",
diff --git a/src/lib/mineru.ts b/src/lib/mineru.ts
index 187f6c58..6086b481 100644
--- a/src/lib/mineru.ts
+++ b/src/lib/mineru.ts
@@ -6,9 +6,10 @@ import { getFileName, normalizePath } from "@/lib/path-utils"
import type { SavedImage } from "@/lib/extract-source-images"
const API_BASE = "https://mineru.net/api/v4"
-const LOCAL_API_BASE = "http://127.0.0.1:8790/api/mineru-local"
+export const DEFAULT_LOCAL_MINERU_ENDPOINT = "http://127.0.0.1:8000"
const POLL_INTERVAL_MS = 3_000
const POLL_TIMEOUT_MS = 300_000 // 5 minutes
+const LOCAL_POLL_TIMEOUT_MS = 3_600_000 // Official mineru-api tasks can include model cold starts.
const MAX_ACCURATE_PARSE_BYTES = 200 * 1024 * 1024
const MINERU_IMAGE_EXTS = new Set([
"png",
@@ -77,6 +78,20 @@ interface MineruExtractedMarkdown {
savedImages: SavedImage[]
}
+function localMineruApiBase(endpoint: string | undefined): string {
+ const candidate = endpoint?.trim() || DEFAULT_LOCAL_MINERU_ENDPOINT
+ let parsed: URL
+ try {
+ parsed = new URL(candidate)
+ } catch {
+ throw new Error("Local MinerU endpoint must be a valid HTTP(S) URL")
+ }
+ if (!/^https?:$/.test(parsed.protocol) || parsed.username || parsed.password) {
+ throw new Error("Local MinerU endpoint must be an HTTP(S) URL without credentials")
+ }
+ return candidate.replace(/\/+$/, "")
+}
+
// ── API calls ──
async function mineruHeaders(token: string): Promise<HeadersInit> {
@@ -625,54 +640,131 @@ async function downloadAndExtractMarkdown(
// ── Local backend ──
/**
- * Parse a document using a self-hosted MinerU service (no token required).
- * The service accepts a JSON body with base64 file content and exposes
- * task polling + result endpoints. Selected via `config.backend === "local"`.
+ * Parse a document through the official `mineru-api` asynchronous protocol.
+ * Files are submitted as multipart/form-data to `/tasks`; the task status and
+ * result contracts mirror MinerU's own API client.
*/
async function parseWithLocalMineru(
+ config: MineruConfig,
sourcePath: string,
fileName: string,
onProgress?: (msg: string) => void,
signal?: AbortSignal,
-): Promise<string> {
+ assetOptions?: MineruAssetOptions,
+): Promise<MineruExtractedMarkdown> {
const httpFetch = await getHttpFetch()
+ const apiBase = localMineruApiBase(config.localEndpoint)
+ if (config.localBackend?.endsWith("http-client") && !config.localServerUrl?.trim()) {
+ throw new Error("MinerU HTTP client backends require a model server URL")
+ }
+ const fileSize = await getFileSize(sourcePath)
+ if (fileSize > MAX_ACCURATE_PARSE_BYTES) {
+ throw new Error("MinerU accurate parsing supports files up to 200 MB")
+ }
+ throwIfAborted(signal)
const { base64 } = await readFileAsBase64(sourcePath)
+ const bytes = decodeBase64ToBytes(base64)
+ const fileBuffer = bytes.buffer.slice(
+ bytes.byteOffset,
+ bytes.byteOffset + bytes.byteLength,
+ ) as ArrayBuffer
+ const form = new FormData()
+ form.append("files", new Blob([fileBuffer], { type: "application/pdf" }), fileName)
+ form.append("lang_list", config.localLanguage || "ch")
+ form.append("backend", config.localBackend || "hybrid-engine")
+ form.append("effort", config.localEffort || "medium")
+ form.append("parse_method", config.localParseMethod || "auto")
+ form.append("formula_enable", String(config.localFormulaEnabled !== false))
+ form.append("table_enable", String(config.localTableEnabled !== false))
+ form.append("image_analysis", String(config.localImageAnalysis !== false))
+ form.append("return_md", "true")
+ form.append("return_images", String(Boolean(assetOptions)))
+ form.append("response_format_zip", "false")
+ if (config.localServerUrl?.trim()) form.append("server_url", config.localServerUrl.trim())
onProgress?.("Uploading to local MinerU...")
- const submitRes = await httpFetch(`${LOCAL_API_BASE}/parse`, {
+ const submitRes = await httpFetch(`${apiBase}/tasks`, {
method: "POST",
- headers: { "Content-Type": "application/json" },
signal,
- body: JSON.stringify({ file: base64, filename: fileName }),
+ body: form,
})
if (!submitRes.ok) {
const text = await submitRes.text().catch(() => "")
throw new Error(`Local MinerU submit failed: HTTP ${submitRes.status}: ${text}`)
}
- const submitData = await submitRes.json()
- const taskId = submitData.task_id
+ const submitData = await submitRes.json() as { task_id?: unknown }
+ const taskId = typeof submitData.task_id === "string" ? submitData.task_id.trim() : ""
if (!taskId) throw new Error("Local MinerU returned no task ID")
+ const encodedTaskId = encodeURIComponent(taskId)
+ // Keep all follow-up requests pinned to the user-configured service. The
+ // official response includes absolute URLs, but following arbitrary values
+ // from a compromised or incompatible server would create an SSRF redirect.
+ const statusUrl = `${apiBase}/tasks/${encodedTaskId}`
+ const resultUrl = `${apiBase}/tasks/${encodedTaskId}/result`
onProgress?.("Waiting for local MinerU to finish...")
const start = Date.now()
- while (Date.now() - start < POLL_TIMEOUT_MS) {
+ while (Date.now() - start < LOCAL_POLL_TIMEOUT_MS) {
throwIfAborted(signal)
- const statusRes = await httpFetch(`${LOCAL_API_BASE}/tasks/${taskId}`, { signal })
+ const statusRes = await httpFetch(statusUrl, { signal })
if (!statusRes.ok) {
throw new Error(`Local MinerU status check failed: HTTP ${statusRes.status}`)
}
const status = await statusRes.json()
- if (status.status === "done") {
+ if (status.status === "completed") {
onProgress?.("Downloading parsed result...")
- const resultRes = await httpFetch(`${LOCAL_API_BASE}/results/${taskId}`, { signal })
+ const resultRes = await httpFetch(resultUrl, { signal })
if (!resultRes.ok) {
throw new Error(`Local MinerU download failed: HTTP ${resultRes.status}`)
}
- const result = await resultRes.json()
- return result.content || ""
+ const result = await resultRes.json() as {
+ results?: Record<string, { md_content?: unknown; images?: unknown }>
+ }
+ const first = result.results && Object.values(result.results)[0]
+ if (!first || typeof first.md_content !== "string" || !first.md_content.trim()) {
+ throw new Error("Local MinerU returned an empty parsing result")
+ }
+ let markdown = convertHtmlTablesToMarkdown(first.md_content)
+ const savedImages: SavedImage[] = []
+ const pathMap = new Map<string, string>()
+ if (assetOptions && first.images && typeof first.images === "object") {
+ const mediaDir = `${normalizePath(assetOptions.projectPath)}/wiki/media/${assetOptions.sourceSummarySlug}/mineru/images`
+ await createDirectory(mediaDir)
+ for (const [rawName, rawData] of Object.entries(first.images)) {
+ throwIfAborted(signal)
+ if (typeof rawData !== "string") continue
+ const match = rawData.match(/^data:(image\/[^;]+);base64,(.+)$/s)
+ if (!match) continue
+ const sourceName = getFileName(normalizeMineruZipPath(rawName))
+ if (!sourceName || !isMineruImagePath(sourceName)) continue
+ const extension = sourceName.split(".").pop()?.toLowerCase()
+ if (!extension) continue
+ // Server-provided names are untrusted and may collide or contain a
+ // Windows reserved device name. Generate deterministic local names
+ // while retaining lookup entries for the original Markdown target.
+ const safeName = `image-${savedImages.length + 1}.${extension}`
+ const absPath = `${mediaDir}/${safeName}`
+ const relPath = `media/${assetOptions.sourceSummarySlug}/mineru/images/${safeName}`
+ await writeFileBase64(absPath, match[2])
+ savedImages.push({
+ index: savedImages.length,
+ mimeType: match[1],
+ page: null,
+ width: 0,
+ height: 0,
+ relPath,
+ absPath,
+ sha256: "",
+ })
+ pathMap.set(rawName, relPath)
+ pathMap.set(sourceName, relPath)
+ }
+ }
+ if (pathMap.size > 0) markdown = rewriteMineruMarkdownImages(markdown, pathMap)
+ return { markdown, savedImages }
}
if (status.status === "failed") {
throw new Error(`Local MinerU parsing failed: ${status.error || "unknown error"}`)
@@ -717,10 +809,17 @@ export async function parseWithMineruResult(
throwIfAborted(signal)
if (config.backend === "local") {
- const fileName = sourcePath.split("/").pop() ?? "document.pdf"
- const markdown = await parseWithLocalMineru(sourcePath, fileName, onProgress, signal)
+ const fileName = getFileName(sourcePath) || "document.pdf"
+ const result = await parseWithLocalMineru(
+ config,
+ sourcePath,
+ fileName,
+ onProgress,
+ signal,
+ assetOptions,
+ )
onProgress?.("Done")
- return { markdown, savedImages: [] }
+ return result
}
if (!config.token) throw new Error("MinerU API token not configured")
@@ -774,16 +873,20 @@ export async function parseWithMineruResult(
*/
export async function testMineruConnection(
token: string,
- config?: Pick<MineruConfig, "backend">,
+ config?: Pick<MineruConfig, "backend" | "localEndpoint">,
): Promise<void> {
const httpFetch = await getHttpFetch()
if (config?.backend === "local") {
- const res = await httpFetch(`${LOCAL_API_BASE}/health`)
+ const res = await httpFetch(`${localMineruApiBase(config.localEndpoint)}/health`)
if (!res.ok) {
const text = await res.text().catch(() => "")
throw new Error(`Local MinerU service unavailable: HTTP ${res.status}: ${text}`)
}
+ const health = await res.json().catch(() => null) as { status?: unknown } | null
+ if (health?.status !== "healthy") {
+ throw new Error("Local MinerU service returned an invalid or unhealthy status")
+ }
return
}
diff --git a/src/lib/project-store.test.ts b/src/lib/project-store.test.ts
index 0ca83011..6642263d 100644
--- a/src/lib/project-store.test.ts
+++ b/src/lib/project-store.test.ts
@@ -5,10 +5,30 @@ describe("project-store MinerU config normalization", () => {
it("preserves valid MinerU config values", () => {
expect(__projectStoreTest.normalizeMineruConfig({
enabled: true,
+ backend: "local",
+ localEndpoint: "http://localhost:9000/mineru",
+ localBackend: "pipeline",
+ localEffort: "high",
+ localParseMethod: "ocr",
+ localLanguage: "korean",
+ localFormulaEnabled: false,
+ localTableEnabled: true,
+ localImageAnalysis: false,
+ localServerUrl: "http://localhost:30000",
token: "token-123",
modelVersion: "pipeline",
})).toEqual({
enabled: true,
+ backend: "local",
+ localEndpoint: "http://localhost:9000/mineru",
+ localBackend: "pipeline",
+ localEffort: "high",
+ localParseMethod: "ocr",
+ localLanguage: "korean",
+ localFormulaEnabled: false,
+ localTableEnabled: true,
+ localImageAnalysis: false,
+ localServerUrl: "http://localhost:30000",
token: "token-123",
modelVersion: "pipeline",
})
@@ -21,6 +41,16 @@ describe("project-store MinerU config normalization", () => {
modelVersion: "mineru-html" as "vlm",
})).toEqual({
enabled: false,
+ backend: "cloud",
+ localEndpoint: "http://127.0.0.1:8000",
+ localBackend: "hybrid-engine",
+ localEffort: "medium",
+ localParseMethod: "auto",
+ localLanguage: "ch",
+ localFormulaEnabled: true,
+ localTableEnabled: true,
+ localImageAnalysis: true,
+ localServerUrl: "",
token: "",
modelVersion: "vlm",
})
diff --git a/src/lib/project-store.ts b/src/lib/project-store.ts
index 45e16e27..08377bfe 100644
--- a/src/lib/project-store.ts
+++ b/src/lib/project-store.ts
@@ -112,10 +112,40 @@ export async function loadMultimodalConfig(): Promise<MultimodalConfig | null> {
}
const MINERU_KEY = "mineruConfig"
+const DEFAULT_LOCAL_MINERU_ENDPOINT = "http://127.0.0.1:8000"
+const LOCAL_MINERU_BACKENDS = new Set([
+ "pipeline",
+ "vlm-engine",
+ "hybrid-engine",
+ "vlm-http-client",
+ "hybrid-http-client",
+])
function normalizeMineruConfig(config: MineruConfig): MineruConfig {
return {
enabled: config.enabled === true,
+ backend: config.backend === "local" ? "local" : "cloud",
+ localEndpoint:
+ typeof config.localEndpoint === "string" && config.localEndpoint.trim()
+ ? config.localEndpoint.trim()
+ : DEFAULT_LOCAL_MINERU_ENDPOINT,
+ localBackend: LOCAL_MINERU_BACKENDS.has(config.localBackend ?? "")
+ ? config.localBackend
+ : "hybrid-engine",
+ localEffort: config.localEffort === "high" ? "high" : "medium",
+ localParseMethod:
+ config.localParseMethod === "txt" || config.localParseMethod === "ocr"
+ ? config.localParseMethod
+ : "auto",
+ localLanguage:
+ typeof config.localLanguage === "string" && config.localLanguage.trim()
+ ? config.localLanguage.trim()
+ : "ch",
+ localFormulaEnabled: config.localFormulaEnabled !== false,
+ localTableEnabled: config.localTableEnabled !== false,
+ localImageAnalysis: config.localImageAnalysis !== false,
+ localServerUrl:
+ typeof config.localServerUrl === "string" ? config.localServerUrl.trim() : "",
token: typeof config.token === "string" ? config.token : "",
modelVersion: config.modelVersion === "pipeline" ? "pipeline" : "vlm",
}
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index 3e6770fa..4a2654da 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -255,11 +255,29 @@ interface SourceWatchConfig {
}
export type MineruModelVersion = "pipeline" | "vlm"
+export type MineruLocalBackend =
+ | "pipeline"
+ | "vlm-engine"
+ | "hybrid-engine"
+ | "vlm-http-client"
+ | "hybrid-http-client"
+export type MineruParseMethod = "auto" | "txt" | "ocr"
+export type MineruEffort = "medium" | "high"
export interface MineruConfig {
enabled: boolean
/** Parsing backend: MinerU cloud API (default) or a self-hosted local service. */
backend?: "cloud" | "local"
+ /** Base URL of a compatible self-hosted MinerU HTTP wrapper. */
+ localEndpoint?: string
+ localBackend?: MineruLocalBackend
+ localEffort?: MineruEffort
+ localParseMethod?: MineruParseMethod
+ localLanguage?: string
+ localFormulaEnabled?: boolean
+ localTableEnabled?: boolean
+ localImageAnalysis?: boolean
+ localServerUrl?: string
token: string
modelVersion: MineruModelVersion
}
@@ -521,7 +539,21 @@ export const useWikiStore = create<WikiState>((set) => ({
},
sourceWatchConfig: DEFAULT_SOURCE_WATCH_CONFIG,
- mineruConfig: { enabled: false, token: "", modelVersion: "vlm" },
+ mineruConfig: {
+ enabled: false,
+ backend: "cloud",
+ localEndpoint: "http://127.0.0.1:8000",
+ localBackend: "hybrid-engine",
+ localEffort: "medium",
+ localParseMethod: "auto",
+ localLanguage: "ch",
+ localFormulaEnabled: true,
+ localTableEnabled: true,
+ localImageAnalysis: true,
+ localServerUrl: "",
+ token: "",
+ modelVersion: "vlm",
+ },
// Default `enabled: true` preserves the pre-toggle behavior: anyone
// who already had `LLM_WIKI_API_TOKEN` set or `apiConfig.token`