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

fix: Gemini streaming — multi-part text + URL-encode model id

nash_su145e218825f4
diff --git a/src/lib/__tests__/llm-providers.test.ts b/src/lib/__tests__/llm-providers.test.ts
index 46d51d99..c2aa0a9c 100644
--- a/src/lib/__tests__/llm-providers.test.ts
+++ b/src/lib/__tests__/llm-providers.test.ts
@@ -1,5 +1,6 @@
import { describe, it, expect } from "vitest"
-import { buildAnthropicUrl } from "../llm-providers"
+import { buildAnthropicUrl, parseGoogleLine, getProviderConfig } from "../llm-providers"
+import type { LlmConfig as RealLlmConfig } from "@/stores/wiki-store"
// Inline minimal types to avoid store/zustand dependencies in unit tests
type Provider = "openai" | "anthropic" | "google" | "ollama" | "custom" | "minimax"
@@ -156,3 +157,65 @@ describe("buildAnthropicUrl — URL suffix handling", () => {
)
})
})
+
+describe("parseGoogleLine — Gemini SSE parsing", () => {
+ it("extracts plain text from a single-part event", () => {
+ const line = 'data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}]}'
+ expect(parseGoogleLine(line)).toBe("Hello")
+ })
+
+ it("concatenates text across multiple parts in one event", () => {
+ // Gemini 2.5/3.x reasoning models sometimes split output across
+ // multiple parts in a single streaming chunk. The old parser only
+ // took parts[0], silently dropping the tail.
+ const line = 'data: {"candidates":[{"content":{"parts":[{"text":"Hello "},{"text":"world"}]}}]}'
+ expect(parseGoogleLine(line)).toBe("Hello world")
+ })
+
+ it("skips thought parts so reasoning tokens don't leak into output", () => {
+ const line =
+ 'data: {"candidates":[{"content":{"parts":[{"thought":true,"text":"let me think"},{"text":"The answer is 42"}]}}]}'
+ expect(parseGoogleLine(line)).toBe("The answer is 42")
+ })
+
+ it("returns null when the event has no visible text", () => {
+ const line = 'data: {"candidates":[{"content":{"parts":[{"thought":true,"text":"thinking"}]}}]}'
+ expect(parseGoogleLine(line)).toBeNull()
+ })
+
+ it("returns null for non-data lines", () => {
+ expect(parseGoogleLine("event: something")).toBeNull()
+ expect(parseGoogleLine("")).toBeNull()
+ expect(parseGoogleLine(":keepalive")).toBeNull()
+ })
+
+ it("returns null for malformed JSON", () => {
+ expect(parseGoogleLine("data: {not json")).toBeNull()
+ })
+})
+
+describe("Google provider URL — model path encoding", () => {
+ const makeGoogleConfig = (model: string): RealLlmConfig => ({
+ provider: "google",
+ apiKey: "test",
+ model,
+ ollamaUrl: "",
+ customEndpoint: "",
+ maxContextSize: 128000,
+ })
+
+ it("embeds a normal model id directly in the URL", () => {
+ const cfg = getProviderConfig(makeGoogleConfig("gemini-2.5-flash"))
+ expect(cfg.url).toBe(
+ "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse",
+ )
+ })
+
+ it("encodes slashes in a pasted OpenRouter-style id so the path stays well-formed", () => {
+ // Previously bare interpolation put a real "/" in the path, which
+ // would have been interpreted as a segment boundary → 404 on a
+ // nonexistent Google resource.
+ const cfg = getProviderConfig(makeGoogleConfig("google/gemini-3-pro-preview"))
+ expect(cfg.url).toContain("google%2Fgemini-3-pro-preview:streamGenerateContent")
+ })
+})
diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.ts
index 7036074f..56c8ccf8 100644
--- a/src/lib/llm-providers.ts
+++ b/src/lib/llm-providers.ts
@@ -48,16 +48,30 @@ function parseAnthropicLine(line: string): string | null {
}
}
-function parseGoogleLine(line: string): string | null {
+export function parseGoogleLine(line: string): string | null {
if (!line.startsWith("data: ")) return null
const data = line.slice(6).trim()
try {
const parsed = JSON.parse(data) as {
candidates: Array<{
- content: { parts: Array<{ text?: string }> }
+ content: { parts: Array<{ text?: string; thought?: boolean }> }
}>
}
- return parsed.candidates?.[0]?.content?.parts?.[0]?.text ?? null
+ // Gemini can split a single event's output across multiple parts —
+ // common with 2.5/3.x reasoning models, which interleave
+ // `thought: true` parts (chain-of-thought) with the real answer.
+ // Previous impl only took parts[0].text, silently dropping anything
+ // that came in a later part. Concatenate all visible text parts and
+ // skip ones flagged as thoughts so we don't leak reasoning text into
+ // the user-visible stream.
+ const parts = parsed.candidates?.[0]?.content?.parts
+ if (!parts || parts.length === 0) return null
+ let out = ""
+ for (const p of parts) {
+ if (p.thought) continue
+ if (p.text) out += p.text
+ }
+ return out.length > 0 ? out : null
} catch {
return null
}
@@ -191,9 +205,14 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
}
}
- case "google":
+ case "google": {
+ // Encode the model segment — users sometimes paste OpenRouter-style
+ // ids with slashes (e.g. "google/gemini-3-pro-preview") and bare
+ // interpolation would produce a broken URL. encodeURIComponent
+ // handles that plus any other path-illegal characters.
+ const encodedModel = encodeURIComponent(model)
return {
- url: `https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent?alt=sse`,
+ url: `https://generativelanguage.googleapis.com/v1beta/models/${encodedModel}:streamGenerateContent?alt=sse`,
headers: {
"Content-Type": JSON_CONTENT_TYPE,
"x-goog-api-key": apiKey,
@@ -201,6 +220,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
buildBody: buildGoogleBody,
parseStream: parseGoogleLine,
}
+ }
case "ollama": {
// Defense-in-depth for the same reason as the custom branch: if a