Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(reasoning): guard Anthropic response text extraction against non-text content (#1709)
Aditya Painuli84dd7902b6ab
diff --git a/src/helpers/anthropicResponse.js b/src/helpers/anthropicResponse.jsnew file mode 100644index 000000000..929bcb79e--- /dev/null+++ b/src/helpers/anthropicResponse.js@@ -0,0 +1,27 @@+// Text extraction for Anthropic Messages API responses. `content` is a block+// array whose shape varies run to run: it can be empty (max_tokens hit before+// any text), or lead with non-text blocks (thinking, tool_use), so+// `content[0].text` is not a safe read (#1653).++// Returns the trimmed concatenation of all text blocks, or null when the+// response contains no text block at all.+function extractAnthropicText(data) {+ const blocks = Array.isArray(data?.content) ? data.content : [];+ const parts = blocks+ .filter((block) => block?.type === "text" && typeof block.text === "string")+ .map((block) => block.text);+ if (parts.length === 0) return null;+ return parts.join("").trim();+}++function describeMissingAnthropicText(data) {+ const stopReason = data?.stop_reason;+ if (stopReason === "max_tokens") {+ return "Anthropic returned no text before hitting the max_tokens limit";+ }+ return stopReason+ ? `Anthropic response contained no text content (stop_reason: ${stopReason})`+ : "Anthropic response contained no text content";+}++module.exports = { extractAnthropicText, describeMissingAnthropicText };diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex e041cd0dc..6c98268eb 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -19,6 +19,7 @@ const {} = require("./workspacePolicyManager");const { createEnterpriseIdentityManager } = require("./enterpriseIdentityManager");const { createCloudConfigRequestHandler } = require("./cloudConfigRequest");+const { extractAnthropicText, describeMissingAnthropicText } = require("./anthropicResponse");const {createPolicyResponseError,readPolicyResponseError,@@ -4744,7 +4745,11 @@ class IPCHandlers {if (config?.requireCompleteOutput && data.stop_reason === "max_tokens") {throw new Error("Model output was truncated before the selection edit completed");}- return { success: true, text: data.content[0].text.trim() };+ const outputText = extractAnthropicText(data);+ if (outputText === null) {+ throw new Error(describeMissingAnthropicText(data));+ }+ return { success: true, text: outputText };} catch (error) {debugLogger.error("Anthropic reasoning error:", error);return { success: false, error: error.message };diff --git a/test/helpers/anthropicResponse.test.js b/test/helpers/anthropicResponse.test.jsnew file mode 100644index 000000000..60574c868--- /dev/null+++ b/test/helpers/anthropicResponse.test.js@@ -0,0 +1,84 @@+const { test, describe } = require("node:test");+const assert = require("node:assert");+const {+ extractAnthropicText,+ describeMissingAnthropicText,+} = require("../../src/helpers/anthropicResponse.js");++describe("extractAnthropicText", () => {+ test("returns trimmed text for a normal single text block", () => {+ const data = { content: [{ type: "text", text: " cleaned up dictation " }] };+ assert.strictEqual(extractAnthropicText(data), "cleaned up dictation");+ });++ test("joins multiple text blocks", () => {+ const data = {+ content: [+ { type: "text", text: "first " },+ { type: "text", text: "second" },+ ],+ };+ assert.strictEqual(extractAnthropicText(data), "first second");+ });++ test("skips leading thinking block and returns the text block (#1653)", () => {+ const data = {+ content: [+ { type: "thinking", thinking: "let me consider..." },+ { type: "text", text: "the answer" },+ ],+ };+ assert.strictEqual(extractAnthropicText(data), "the answer");+ });++ test("returns null when content is empty (max_tokens before any text)", () => {+ assert.strictEqual(extractAnthropicText({ content: [], stop_reason: "max_tokens" }), null);+ });++ test("returns null when content has only non-text blocks", () => {+ const data = { content: [{ type: "thinking", thinking: "..." }] };+ assert.strictEqual(extractAnthropicText(data), null);+ });++ test("returns null for missing or malformed content", () => {+ assert.strictEqual(extractAnthropicText({}), null);+ assert.strictEqual(extractAnthropicText(null), null);+ assert.strictEqual(extractAnthropicText({ content: "nope" }), null);+ assert.strictEqual(extractAnthropicText({ content: [{ type: "text" }] }), null);+ });++ test("preserves empty-string success for an all-whitespace text block", () => {+ const data = { content: [{ type: "text", text: " " }] };+ assert.strictEqual(extractAnthropicText(data), "");+ });++ test("pre-fix expression throws on the shapes this helper now handles", () => {+ // The old handler read data.content[0].text.trim() directly; these are the+ // response shapes from #1653 that made it throw the reported TypeError.+ const shapes = [+ { content: [], stop_reason: "max_tokens" },+ { content: [{ type: "thinking", thinking: "..." }] },+ ];+ for (const data of shapes) {+ assert.throws(() => data.content[0].text.trim(), TypeError);+ assert.doesNotThrow(() => extractAnthropicText(data));+ }+ });+});++describe("describeMissingAnthropicText", () => {+ test("names max_tokens explicitly", () => {+ assert.match(describeMissingAnthropicText({ stop_reason: "max_tokens" }), /max_tokens limit/);+ });++ test("includes other stop reasons", () => {+ assert.match(describeMissingAnthropicText({ stop_reason: "refusal" }), /refusal/);+ });++ test("falls back to a generic message", () => {+ assert.strictEqual(+ describeMissingAnthropicText({}),+ "Anthropic response contained no text content"+ );+ });+});