diff --git a/core/promptFiles/parsePromptFile.ts b/core/promptFiles/parsePromptFile.ts index 4b6cf6917b8..fd96e6f4230 100644 --- a/core/promptFiles/parsePromptFile.ts +++ b/core/promptFiles/parsePromptFile.ts @@ -3,7 +3,8 @@ import * as YAML from "yaml"; import { getLastNPathParts } from "../util/uri"; export function parsePromptFile(path: string, content: string) { - let [preambleRaw, prompt] = content.split("\n---\n"); + const normalizedContent = content.replace(/\r\n/g, "\n"); + let [preambleRaw, prompt] = normalizedContent.split("\n---\n"); if (prompt === undefined) { prompt = preambleRaw; preambleRaw = ""; diff --git a/core/promptFiles/parsePromptFile.vitest.ts b/core/promptFiles/parsePromptFile.vitest.ts new file mode 100644 index 00000000000..8ab66038a2b --- /dev/null +++ b/core/promptFiles/parsePromptFile.vitest.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from "vitest"; +import { parsePromptFile } from "./parsePromptFile"; + +describe("parsePromptFile", () => { + const path = "test.prompt"; + + it("should parse LF content correctly", () => { + const content = "name: test\ndescription: a test\n---\nHello world"; + const result = parsePromptFile(path, content); + expect(result.name).toBe("test"); + expect(result.description).toBe("a test"); + expect(result.prompt).toBe("Hello world"); + }); + + it("should parse CRLF content correctly", () => { + const content = "name: test\r\ndescription: a test\r\n---\r\nHello world"; + const result = parsePromptFile(path, content); + expect(result.name).toBe("test"); + expect(result.description).toBe("a test"); + expect(result.prompt).toBe("Hello world"); + }); + + it("should handle no frontmatter", () => { + const content = "Just a prompt"; + const result = parsePromptFile(path, content); + expect(result.prompt).toBe("Just a prompt"); + expect(result.name).toBe("test"); + }); + + it("should parse system tag with CRLF", () => { + const content = + "name: test\r\n---\r\nYou are helpful\r\nHello"; + const result = parsePromptFile(path, content); + expect(result.systemMessage).toBe("You are helpful"); + expect(result.prompt).toBe("Hello"); + }); +});