-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { renderMessages } from "./render-messages"; | ||
import { ChatCompletionMessageParam } from "openai/resources"; | ||
|
||
describe("renderMessages", () => { | ||
it("should never HTML-escape values, regardless of mustache syntax", () => { | ||
const messages: ChatCompletionMessageParam[] = [ | ||
{ role: "user", content: "{{value}} and {{{value}}}" }, | ||
]; | ||
const rendered = renderMessages(messages, { value: "<b>bold</b>" }); | ||
expect(rendered[0].content).toBe("<b>bold</b> and <b>bold</b>"); | ||
}); | ||
|
||
it("should stringify objects when using either {{...}} or {{{...}}}", () => { | ||
const messages: ChatCompletionMessageParam[] = [ | ||
{ | ||
role: "user", | ||
content: "Double braces: {{data}}, Triple braces: {{{data}}}", | ||
}, | ||
]; | ||
const data = { foo: "bar", num: 42 }; | ||
const rendered = renderMessages(messages, { data }); | ||
const stringified = JSON.stringify(data); | ||
expect(rendered[0].content).toBe( | ||
`Double braces: ${stringified}, Triple braces: ${stringified}`, | ||
); | ||
}); | ||
|
||
it("should handle empty content", () => { | ||
const messages: ChatCompletionMessageParam[] = [ | ||
{ role: "user", content: "" }, | ||
]; | ||
const rendered = renderMessages(messages, {}); | ||
expect(rendered[0].content).toBe(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import mustache from "mustache"; | ||
import { ChatCompletionMessageParam } from "openai/resources"; | ||
|
||
export function renderMessages( | ||
messages: ChatCompletionMessageParam[], | ||
renderArgs: Record<string, unknown>, | ||
): ChatCompletionMessageParam[] { | ||
return messages.map((m) => ({ | ||
...m, | ||
content: m.content | ||
? mustache.render( | ||
(m.content as string).replace(/\{{3}/g, "{{").replace(/\}{3}/g, "}}"), | ||
renderArgs, | ||
undefined, | ||
{ | ||
escape: (v: unknown) => | ||
typeof v === "string" ? v : JSON.stringify(v), | ||
}, | ||
) | ||
: "", | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters