Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
fix(ai): split streamed reasoning when only </think> arrives (#1001)
WissssleyL14f8457161e5
diff --git a/Sources/Fluid/Services/ThinkingParsers.swift b/Sources/Fluid/Services/ThinkingParsers.swiftindex 625fbd40..cad3ac31 100644--- a/Sources/Fluid/Services/ThinkingParsers.swift+++ b/Sources/Fluid/Services/ThinkingParsers.swift@@ -113,6 +113,8 @@ nonisolated enum ThinkingParserFactory {/// Standard parser for models that use `<think>...</think>` or `<thinking>...</thinking>` tags./// Used by: DeepSeek, Qwen, Claude (when thinking enabled), most open modelsnonisolated struct StandardThinkingParser: ThinkingParser {+ private var sawOpeningTag = false+mutating func processChunk(_ chunk: String,currentState: ThinkingParserState,@@ -136,6 +138,7 @@ nonisolated struct StandardThinkingParser: ThinkingParser {// Remove processed part including open tagtagBuffer = String(tagBuffer[openRange.upperBound...])newState = .inThinking+ self.sawOpeningTag = true}}@@ -180,9 +183,20 @@ nonisolated struct StandardThinkingParser: ThinkingParser {}func finalize(thinkingBuffer: [String], contentBuffer: [String], finalState: ThinkingParserState) -> (thinking: String, content: String) {- let thinking = thinkingBuffer.joined()+ var thinking = thinkingBuffer.joined()var content = contentBuffer.joined()+ // Chat templates that open the think block in the prompt (e.g. Qwen3 Thinking-2507)+ // stream "reasoning</think>answer" with no opening tag. Split on that close the same+ // way stripThinkingTags does for non-streaming responses.+ let firstClose = ["</think>", "</thinking>"]+ .compactMap { content.range(of: $0) }+ .min { $0.lowerBound < $1.lowerBound }+ if !self.sawOpeningTag, let closeRange = firstClose {+ thinking += content[..<closeRange.lowerBound]+ content = String(content[closeRange.upperBound...])+ }+// Strip any remaining stray tagscontent = content.replacingOccurrences(of: "</think>", with: "")content = content.replacingOccurrences(of: "</thinking>", with: "")diff --git a/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift b/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swiftindex e668313d..9224a33d 100644--- a/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift+++ b/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift@@ -545,6 +545,44 @@ final class LLMClientStreamingTests: XCTestCase {XCTAssertEqual(response.toolCalls.first?.getString("command"), "pwd")}+ /// Qwen3 Thinking-2507 templates open the think block in the prompt, so the stream+ /// only carries the closing tag.+ func testTagParserSplitsReasoningWhenOnlyCloseTagStreams() async throws {+ let client = self.makeClient()+ var config = LLMClient.Config(+ messages: [["role": "user", "content": "Clean this up"]],+ model: "qwen3-30b-a3b-thinking-2507",+ baseURL: "https://issue-445.test/orphan-close/v1",+ apiKey: "",+ streaming: true+ )+ config.maxRetries = 1+ config.timeoutSeconds = 5++ let response = try await client.call(config)++ XCTAssertEqual(response.thinking, "Reasoning.")+ XCTAssertEqual(response.content, "Ready.")+ }++ func testTagParserSplitsOnEarliestCloseTag() async throws {+ let client = self.makeClient()+ var config = LLMClient.Config(+ messages: [["role": "user", "content": "Clean this up"]],+ model: "qwen3-30b-a3b-thinking-2507",+ baseURL: "https://issue-445.test/mixed-close/v1",+ apiKey: "",+ streaming: true+ )+ config.maxRetries = 1+ config.timeoutSeconds = 5++ let response = try await client.call(config)++ XCTAssertEqual(response.thinking, "Reasoning.")+ XCTAssertEqual(response.content, "Ready. Done.")+ }+func testStreamingDecodeAndCallbacksStayOffMainThread() async throws {let client = self.makeClient()let probe = LLMCallbackThreadProbe()@@ -615,6 +653,22 @@ private class Issue445StreamURLProtocol: URLProtocol {"""#+ private static let orphanCloseFixture = #"""+ data: {"choices":[{"index":0,"delta":{"content":"Reasoning.</thi"}}]}++ data: {"choices":[{"index":0,"delta":{"content":"nk>Ready."},"finish_reason":"stop"}]}++ data: [DONE]++ """#++ private static let mixedCloseFixture = #"""+ data: {"choices":[{"index":0,"delta":{"content":"Reasoning.</thinking>Ready.</think> Done."},"finish_reason":"stop"}]}++ data: [DONE]++ """#+override class func canInit(with request: URLRequest) -> Bool {request.url?.host == "issue-445.test"}@@ -638,7 +692,15 @@ private class Issue445StreamURLProtocol: URLProtocol {return}- let fixture = url.path.contains("tag-parser") ? Self.tagParserFixture : Self.separateReasoningFixture+ let fixture = if url.path.contains("orphan-close") {+ Self.orphanCloseFixture+ } else if url.path.contains("mixed-close") {+ Self.mixedCloseFixture+ } else if url.path.contains("tag-parser") {+ Self.tagParserFixture+ } else {+ Self.separateReasoningFixture+ }client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)client?.urlProtocol(self, didLoad: Data(fixture.utf8))