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 MinerU 4 V1 API

nash_su7fc668bbf46b
diff --git a/src/lib/mineru.test.ts b/src/lib/mineru.test.ts
index 724e336c..30f661a8 100644
--- a/src/lib/mineru.test.ts
+++ b/src/lib/mineru.test.ts
@@ -350,6 +350,79 @@ describe("MinerU API helpers", () => {
})
describe("parseWithMineru", () => {
+ it("uses the MinerU 4 V1 upload, job, and ZIP artifact flow", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(new Response("not found", { status: 404 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "ok", version: "4.0.10" }))
+ .mockResolvedValueOnce(jsonResponse({
+ id: "upload-1",
+ status: "completed",
+ file: { id: "source-file-1" },
+ }))
+ .mockResolvedValueOnce(jsonResponse({
+ job_id: "job-1",
+ status: "completed",
+ files: [{
+ name: "report.pdf",
+ status: "completed",
+ output_files: { zip: { file_id: "zip-file-1" } },
+ }],
+ }))
+ .mockResolvedValueOnce(await zipResponse({ "full.md": "# Parsed with V1" }))
+
+ await expect(parseWithMineru({
+ enabled: true,
+ backend: "local",
+ localBackend: "hybrid-engine",
+ localEffort: "medium",
+ token: "",
+ modelVersion: "vlm",
+ }, "/tmp/report.pdf")).resolves.toBe("# Parsed with V1")
+
+ expect(mockHttpFetch.mock.calls.map(([url]) => url)).toEqual([
+ "http://127.0.0.1:8000/health",
+ "http://127.0.0.1:8000/v1/health",
+ "http://127.0.0.1:8000/v1/uploads",
+ "http://127.0.0.1:8000/v1/parse/jobs",
+ "http://127.0.0.1:8000/v1/files/zip-file-1/content",
+ ])
+ const jobBody = JSON.parse(String(mockHttpFetch.mock.calls[3]?.[1]?.body))
+ expect(jobBody).toMatchObject({ tier: "standard", output_formats: ["zip"] })
+ })
+
+ it("does not leak the local API token to a cross-origin V1 upload URL", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(new Response("not found", { status: 404 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "ok" }))
+ .mockResolvedValueOnce(jsonResponse({
+ id: "upload-1",
+ status: "pending",
+ upload_url: "https://objects.example/upload-1",
+ upload_method: "PUT",
+ upload_headers: { "X-Upload-Token": "signed" },
+ }))
+ .mockResolvedValueOnce(new Response(null, { status: 200 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "completed", file: { id: "source-1" } }))
+ .mockResolvedValueOnce(jsonResponse({
+ job_id: "job-1",
+ status: "completed",
+ files: [{ status: "completed", output_files: { zip: { file_id: "zip-1" } } }],
+ }))
+ .mockResolvedValueOnce(await zipResponse({ "full.md": "parsed" }))
+
+ await parseWithMineru({
+ enabled: true,
+ backend: "local",
+ localToken: "local-secret",
+ localBackend: "hybrid-engine",
+ token: "",
+ modelVersion: "vlm",
+ }, "/tmp/report.pdf")
+
+ expect(mockHttpFetch.mock.calls[3]?.[0]).toBe("https://objects.example/upload-1")
+ expect(mockHttpFetch.mock.calls[3]?.[1]?.headers).toEqual({ "X-Upload-Token": "signed" })
+ })
+
it("submits a Windows-path PDF and pipeline mode to a custom local endpoint", async () => {
mockHttpFetch
.mockResolvedValueOnce(jsonResponse({
@@ -836,6 +909,28 @@ describe("testMineruConnection", () => {
)
})
+ it("falls back to the MinerU 4 V1 health endpoint", async () => {
+ mockHttpFetch
+ .mockResolvedValueOnce(new Response("not found", { status: 404 }))
+ .mockResolvedValueOnce(jsonResponse({ status: "ok", version: "4.0.10" }))
+
+ await expect(testMineruConnection("", { backend: "local" })).resolves.toBeUndefined()
+ expect(mockHttpFetch.mock.calls.map(([url]) => url)).toEqual([
+ "http://127.0.0.1:8000/health",
+ "http://127.0.0.1:8000/v1/health",
+ ])
+ })
+
+ it("accepts a configured endpoint that already ends in /v1", async () => {
+ mockHttpFetch.mockResolvedValueOnce(jsonResponse({ status: "ok", version: "4.0.10" }))
+ await expect(testMineruConnection("", {
+ backend: "local",
+ localEndpoint: "http://127.0.0.1:8000/v1/",
+ })).resolves.toBeUndefined()
+ expect(mockHttpFetch).toHaveBeenCalledTimes(1)
+ expect(mockHttpFetch.mock.calls[0]?.[0]).toBe("http://127.0.0.1:8000/v1/health")
+ })
+
it("rejects a 200 response that is not a healthy official MinerU service", async () => {
mockHttpFetch.mockResolvedValueOnce(jsonResponse({ status: "unhealthy" }))
diff --git a/src/lib/mineru.ts b/src/lib/mineru.ts
index 7776434d..6f455957 100644
--- a/src/lib/mineru.ts
+++ b/src/lib/mineru.ts
@@ -92,6 +92,11 @@ function localMineruApiBase(endpoint: string | undefined): string {
return candidate.replace(/\/+$/, "")
}
+function localMineruV1Base(endpoint: string | undefined): string {
+ const base = localMineruApiBase(endpoint)
+ return /\/v1$/i.test(base) ? base : `${base}/v1`
+}
+
// ── API calls ──
async function mineruHeaders(token: string): Promise<HeadersInit> {
@@ -114,7 +119,7 @@ function localMineruRequestInit(
): TauriRequestInit {
return {
...init,
- headers: localMineruHeaders(token),
+ headers: { ...localMineruHeaders(token), ...init.headers },
// plugin-http uses maxRedirections while browser fetch uses redirect.
// Set both so credentials never follow an untrusted service redirect.
redirect: "manual",
@@ -122,6 +127,15 @@ function localMineruRequestInit(
}
}
+
+function sameOrigin(left: string, right: string): boolean {
+ try {
+ return new URL(left).origin === new URL(right).origin
+ } catch {
+ return false
+ }
+}
+
function mineruApiErrorMessage(code: number | string | undefined, msg?: string): string {
const key = String(code ?? "")
const known: Record<string, string> = {
@@ -629,6 +643,15 @@ async function downloadAndExtractMarkdown(
const res = await httpFetch(zipUrl, { signal })
if (!res.ok) throw new Error(`MinerU zip download failed: HTTP ${res.status}`)
+ return extractMarkdownFromZipResponse(res, signal, assetOptions)
+}
+
+async function extractMarkdownFromZipResponse(
+ res: Response,
+ signal?: AbortSignal,
+ assetOptions?: MineruAssetOptions,
+): Promise<MineruExtractedMarkdown> {
+
const buffer = await res.arrayBuffer()
const zip = await JSZip.loadAsync(buffer)
@@ -690,6 +713,145 @@ function localMineruBackendForVersion(
return backend
}
+function localMineruV1Tier(effort: MineruConfig["localEffort"]): string {
+ if (effort === "high") return "advanced"
+ return "standard"
+}
+
+async function fetchLocalMineruV1Artifact(
+ apiBase: string,
+ token: string | undefined,
+ fileId: string,
+ signal?: AbortSignal,
+): Promise<Response> {
+ const httpFetch = await getHttpFetch()
+ const url = `${apiBase}/files/${encodeURIComponent(fileId)}/content`
+ const response = await httpFetch(url, localMineruRequestInit(token, { signal }))
+ if (![301, 302, 303, 307, 308].includes(response.status)) return response
+ const location = response.headers.get("location")
+ if (!location) throw new Error("Local MinerU artifact redirect has no Location header")
+ const target = new URL(location, url).toString()
+ return httpFetch(target, {
+ signal,
+ redirect: "follow",
+ ...(sameOrigin(url, target) ? { headers: localMineruHeaders(token) } : {}),
+ })
+}
+
+async function parseWithLocalMineruV1(
+ config: MineruConfig,
+ fileName: string,
+ bytes: Uint8Array,
+ onProgress?: (msg: string) => void,
+ signal?: AbortSignal,
+ assetOptions?: MineruAssetOptions,
+): Promise<MineruExtractedMarkdown> {
+ const httpFetch = await getHttpFetch()
+ const apiBase = localMineruV1Base(config.localEndpoint)
+ const createRes = await httpFetch(`${apiBase}/uploads`, localMineruRequestInit(config.localToken, {
+ method: "POST",
+ signal,
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ filename: fileName,
+ bytes: bytes.byteLength,
+ mime_type: "application/pdf",
+ purpose: "parse",
+ sha256sum: await sha256OfBytes(bytes),
+ }),
+ }))
+ if (!createRes.ok) throw new Error(`Local MinerU V1 upload creation failed: HTTP ${createRes.status}`)
+ let upload = await createRes.json() as {
+ id?: unknown
+ status?: unknown
+ upload_url?: unknown
+ upload_method?: unknown
+ upload_headers?: unknown
+ file?: { id?: unknown }
+ }
+ const uploadId = typeof upload.id === "string" ? upload.id : ""
+ let fileId = typeof upload.file?.id === "string" ? upload.file.id : ""
+ if (upload.status === "pending") {
+ if (!uploadId || typeof upload.upload_url !== "string") {
+ throw new Error("Local MinerU V1 returned an invalid upload session")
+ }
+ const uploadUrl = new URL(upload.upload_url, apiBase).toString()
+ const returnedHeaders = upload.upload_headers && typeof upload.upload_headers === "object"
+ ? Object.fromEntries(Object.entries(upload.upload_headers).filter((entry): entry is [string, string] => typeof entry[1] === "string"))
+ : {}
+ const uploadHeaders = sameOrigin(apiBase, uploadUrl)
+ ? { ...localMineruHeaders(config.localToken), ...returnedHeaders }
+ : returnedHeaders
+ onProgress?.("Uploading to local MinerU V1...")
+ const bytesRes = await httpFetch(uploadUrl, {
+ method: typeof upload.upload_method === "string" ? upload.upload_method : "PUT",
+ headers: uploadHeaders,
+ body: bytesToUploadBody(bytes),
+ signal,
+ redirect: "manual",
+ maxRedirections: 0,
+ } as TauriRequestInit)
+ if (!bytesRes.ok) throw new Error(`Local MinerU V1 byte upload failed: HTTP ${bytesRes.status}`)
+ const completeRes = await httpFetch(
+ `${apiBase}/uploads/${encodeURIComponent(uploadId)}/complete`,
+ localMineruRequestInit(config.localToken, { method: "POST", signal }),
+ )
+ if (!completeRes.ok) throw new Error(`Local MinerU V1 upload completion failed: HTTP ${completeRes.status}`)
+ upload = await completeRes.json()
+ fileId = typeof upload.file?.id === "string" ? upload.file.id : ""
+ }
+ if (!fileId) throw new Error("Local MinerU V1 returned no file ID")
+
+ const jobRes = await httpFetch(`${apiBase}/parse/jobs`, localMineruRequestInit(config.localToken, {
+ method: "POST",
+ signal,
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ files: [{ source: { type: "file_id", file_id: fileId } }],
+ tier: localMineruV1Tier(config.localEffort),
+ output_formats: ["zip"],
+ }),
+ }))
+ if (!jobRes.ok) throw new Error(`Local MinerU V1 job creation failed: HTTP ${jobRes.status}`)
+ let job = await jobRes.json() as Record<string, unknown>
+ const jobId = typeof job.job_id === "string" ? job.job_id : ""
+ if (!jobId) throw new Error("Local MinerU V1 returned no job ID")
+
+ onProgress?.("Waiting for local MinerU V1 to finish...")
+ const start = Date.now()
+ while (Date.now() - start < LOCAL_POLL_TIMEOUT_MS) {
+ throwIfAborted(signal)
+ const status = job.status
+ if (status === "completed" || status === "partial") {
+ const files = Array.isArray(job.files) ? job.files : []
+ const parsed = files.find((file): file is Record<string, unknown> =>
+ Boolean(file && typeof file === "object" && (file as Record<string, unknown>).status === "completed"))
+ const outputFiles = parsed?.output_files
+ const zip = outputFiles && typeof outputFiles === "object"
+ ? (outputFiles as Record<string, unknown>).zip
+ : undefined
+ const artifactId = zip && typeof zip === "object" && typeof (zip as Record<string, unknown>).file_id === "string"
+ ? (zip as Record<string, unknown>).file_id as string
+ : ""
+ if (!artifactId) throw new Error("Local MinerU V1 completed without a ZIP artifact")
+ const artifact = await fetchLocalMineruV1Artifact(apiBase, config.localToken, artifactId, signal)
+ if (!artifact.ok) throw new Error(`Local MinerU V1 artifact download failed: HTTP ${artifact.status}`)
+ return extractMarkdownFromZipResponse(artifact, signal, assetOptions)
+ }
+ if (status === "failed" || status === "canceled") {
+ throw new Error(`Local MinerU V1 parsing ${status}`)
+ }
+ await waitForPollInterval(signal)
+ const poll = await httpFetch(
+ `${apiBase}/parse/jobs/${encodeURIComponent(jobId)}`,
+ localMineruRequestInit(config.localToken, { signal }),
+ )
+ if (!poll.ok) throw new Error(`Local MinerU V1 status check failed: HTTP ${poll.status}`)
+ job = await poll.json()
+ }
+ throw new Error("Local MinerU V1 parsing timed out")
+}
+
/**
* Parse a document through the official `mineru-api` asynchronous protocol.
* Files are submitted as multipart/form-data to `/tasks`; the task status and
@@ -715,6 +877,7 @@ async function parseWithLocalMineru(
}
throwIfAborted(signal)
let serverVersion: unknown
+ let useV1 = false
if (configuredBackend.endsWith("-engine")) {
try {
const healthRes = await httpFetch(
@@ -722,16 +885,29 @@ async function parseWithLocalMineru(
localMineruRequestInit(config.localToken, { signal }),
)
if (healthRes.ok) {
- const health = await healthRes.json() as { version?: unknown }
+ const health = await healthRes.json() as { status?: unknown; version?: unknown }
serverVersion = health.version
+ useV1 = health.status === "ok"
+ } else {
+ const v1Health = await httpFetch(
+ `${localMineruV1Base(config.localEndpoint)}/health`,
+ localMineruRequestInit(config.localToken, { signal }),
+ )
+ if (v1Health.ok) {
+ const health = await v1Health.json().catch(() => null) as { status?: unknown } | null
+ useV1 = health?.status === "ok"
+ }
}
} catch (err) {
if (signal?.aborted) throw err
}
}
- const localBackend = localMineruBackendForVersion(configuredBackend, serverVersion)
const { base64 } = await readFileAsBase64(sourcePath)
const bytes = decodeBase64ToBytes(base64)
+ if (useV1) {
+ return parseWithLocalMineruV1(config, fileName, bytes, onProgress, signal, assetOptions)
+ }
+ const localBackend = localMineruBackendForVersion(configuredBackend, serverVersion)
const fileBuffer = bytes.buffer.slice(
bytes.byteOffset,
bytes.byteOffset + bytes.byteLength,
@@ -954,18 +1130,31 @@ export async function testMineruConnection(
const httpFetch = await getHttpFetch()
if (config?.backend === "local") {
+ const base = localMineruApiBase(config.localEndpoint)
const res = await httpFetch(
- `${localMineruApiBase(config.localEndpoint)}/health`,
+ `${base}/health`,
localMineruRequestInit(config.localToken),
)
- if (!res.ok) {
+ if (res.ok) {
+ const health = await res.json().catch(() => null) as { status?: unknown } | null
+ if (health?.status === "healthy") return
+ if (health?.status === "ok" && /\/v1$/i.test(base)) return
+ throw new Error("Local MinerU service returned an invalid or unhealthy status")
+ }
+ if (res.status !== 404) {
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")
+ const v1 = await httpFetch(
+ `${localMineruV1Base(config.localEndpoint)}/health`,
+ localMineruRequestInit(config.localToken),
+ )
+ if (!v1.ok) {
+ const text = await v1.text().catch(() => "")
+ throw new Error(`Local MinerU V1 service unavailable: HTTP ${v1.status}: ${text}`)
}
+ const health = await v1.json().catch(() => null) as { status?: unknown } | null
+ if (health?.status !== "ok") throw new Error("Local MinerU V1 service returned an invalid status")
return
}