From 3ceb3e23c6904adc604d88493b2aa3631c2bb51a Mon Sep 17 00:00:00 2001 From: Will Chen Date: Mon, 14 Apr 2025 16:12:25 -0700 Subject: [PATCH] strip out code fences (fix gemini occasional mis-format) --- src/__tests__/chat_stream_handlers.test.ts | 35 +++++++++++++++++++++- src/ipc/processors/response_processor.ts | 9 +++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/__tests__/chat_stream_handlers.test.ts b/src/__tests__/chat_stream_handlers.test.ts index fa74284..cc60edf 100644 --- a/src/__tests__/chat_stream_handlers.test.ts +++ b/src/__tests__/chat_stream_handlers.test.ts @@ -55,6 +55,39 @@ describe("getDyadWriteTags", () => { expect(result).toEqual([]); }); + it("should return a dyad-write tag", () => { + const result = + getDyadWriteTags(` +import React from "react"; +console.log("TodoItem"); +`); + expect(result).toEqual([ + { + path: "src/components/TodoItem.tsx", + content: `import React from \"react\"; +console.log(\"TodoItem\");`, + }, + ]); + }); + + it("should strip out code fence (if needed) from a dyad-write tag", () => { + const result = + getDyadWriteTags(` +\`\`\`tsx +import React from "react"; +console.log("TodoItem"); +\`\`\` + +`); + expect(result).toEqual([ + { + path: "src/components/TodoItem.tsx", + content: `import React from \"react\"; +console.log(\"TodoItem\");`, + }, + ]); + }); + it("should return an array of dyad-write tags", () => { const result = getDyadWriteTags( `I'll create a simple todo list app using React, TypeScript, and shadcn/ui components. Let's get started! @@ -557,7 +590,7 @@ describe("processFullResponse", () => { expect(fs.writeFileSync).toHaveBeenNthCalledWith( 3, "/mock/user/data/path/mock-app-path/src/components/Button.tsx", - "\n import React from 'react';\n export const Button = ({ children }) => ;\n " + "import React from 'react';\n export const Button = ({ children }) => ;" ); // Verify git operations were called for each file diff --git a/src/ipc/processors/response_processor.ts b/src/ipc/processors/response_processor.ts index 4d22f45..c318d78 100644 --- a/src/ipc/processors/response_processor.ts +++ b/src/ipc/processors/response_processor.ts @@ -15,7 +15,14 @@ export function getDyadWriteTags(fullResponse: string): { let match; const tags: { path: string; content: string }[] = []; while ((match = dyadWriteRegex.exec(fullResponse)) !== null) { - tags.push({ path: match[1], content: match[2] }); + const content = match[2].trim().split("\n"); + if (content[0].startsWith("```")) { + content.shift(); + } + if (content[content.length - 1].startsWith("```")) { + content.pop(); + } + tags.push({ path: match[1], content: content.join("\n") }); } return tags; }