Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/promptFiles/parsePromptFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export function parsePromptFile(path: string, content: string) {
const version = preamble.version ?? 2;

let systemMessage: string | undefined = undefined;
if (prompt.includes("<system>")) {
// Require both tags: a `<system>` with no matching `</system>` would make
// `split("</system>")[1]` undefined and crash on `.trim()`.
if (prompt.includes("<system>") && prompt.includes("</system>")) {
systemMessage = prompt.split("<system>")[1].split("</system>")[0].trim();
prompt = prompt.split("</system>")[1].trim();
}
Expand Down
32 changes: 32 additions & 0 deletions core/promptFiles/parsePromptFile.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";

import { parsePromptFile } from "./parsePromptFile.js";

describe("parsePromptFile", () => {
it("extracts the system message when both tags are present", () => {
const result = parsePromptFile(
"greeting.prompt",
"<system>You are helpful</system>\nSummarize the file",
);

expect(result.systemMessage).toBe("You are helpful");
expect(result.prompt).toBe("Summarize the file");
});

it("does not throw when <system> has no closing tag", () => {
const content = "<system>You forgot to close the tag\nSummarize the file";

expect(() => parsePromptFile("broken.prompt", content)).not.toThrow();

const result = parsePromptFile("broken.prompt", content);
expect(result.systemMessage).toBeUndefined();
expect(result.prompt).toBe(content);
});

it("leaves the prompt untouched when there is no system block", () => {
const result = parsePromptFile("plain.prompt", "Just a prompt body");

expect(result.systemMessage).toBeUndefined();
expect(result.prompt).toBe("Just a prompt body");
});
});
Loading