Skip to content

Commit 103bcc8

Browse files
pullfrog[bot]Trigger.dev RepoOps
authored andcommitted
fix(sdk): refine local prompt rendering
Mono-RevId: e9d01ed558ce39906c771581773e5d93588dd7cf
1 parent b6aeb7e commit 103bcc8

2 files changed

Lines changed: 91 additions & 8 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it } from "vitest";
2+
import { definePrompt } from "./prompt.js";
3+
4+
describe("local prompt resolution", () => {
5+
// Catches: values inserted inside conditional sections being evaluated again as placeholders.
6+
it("keeps substituted template syntax literal inside and outside conditionals", async () => {
7+
const prompt = definePrompt({
8+
id: "literal-prompt-values",
9+
content: "{{#show}}Hello {{name}} / {{ name }}!{{/show}} Outside: {{name}}",
10+
});
11+
12+
const result = await prompt.resolve({
13+
show: true,
14+
name: "{{systemPrompt}}",
15+
systemPrompt: "private instructions",
16+
});
17+
18+
expect(result.text).toBe(
19+
"Hello {{systemPrompt}} / {{systemPrompt}}! Outside: {{systemPrompt}}"
20+
);
21+
});
22+
23+
// Catches: inherited Object.prototype properties resolving as values or enabling conditionals.
24+
it.each(["constructor", "__proto__", "toString"])(
25+
"does not resolve inherited %s properties",
26+
async (key) => {
27+
const prompt = definePrompt({
28+
id: `inherited-prompt-${key}`,
29+
content: `Before{{${key}}}{{#${key}}}hidden{{/${key}}}After`,
30+
});
31+
32+
expect((await prompt.resolve({})).text).toBe("BeforeAfter");
33+
}
34+
);
35+
36+
// Catches: custom prototype values being substituted, including inside enabled sections.
37+
it("ignores custom inherited properties and supports shadowed hasOwnProperty", async () => {
38+
const variables = Object.assign(Object.create({ inherited: "private" }), {
39+
show: true,
40+
hasOwnProperty: "own value",
41+
});
42+
const prompt = definePrompt({
43+
id: "custom-prototype-prompt",
44+
content:
45+
"{{inherited}}{{#inherited}}hidden{{/inherited}}{{#show}}{{inherited}}{{hasOwnProperty}}{{/show}}",
46+
});
47+
48+
expect((await prompt.resolve(variables)).text).toBe("own value");
49+
});
50+
51+
// Catches: own properties with prototype-related names being incorrectly rejected.
52+
it("resolves explicitly supplied prototype-related names on a null-prototype object", async () => {
53+
const variables = Object.assign(Object.create(null), {
54+
constructor: "own constructor",
55+
["__proto__"]: "own proto",
56+
});
57+
const prompt = definePrompt({
58+
id: "own-prototype-names-prompt",
59+
content: "{{constructor}} / {{#__proto__}}{{ __proto__ }}{{/__proto__}}",
60+
});
61+
62+
expect((await prompt.resolve(variables)).text).toBe("own constructor / own proto");
63+
});
64+
65+
// Catches: conditional selection or ordinary substitution changing during compiler refactoring.
66+
it("preserves conditionals, whitespace, missing values, and falsy substitutions", async () => {
67+
const prompt = definePrompt({
68+
id: "ordinary-prompt-substitutions",
69+
content:
70+
"{{#show}}Hello {{ name }}: {{count}} / {{enabled}} / {{nil}} / {{missing}}{{/show}}" +
71+
"{{#disabled}}hidden{{/disabled}}{{#missing}}hidden{{/missing}} Outside: {{name}}",
72+
});
73+
74+
const result = await prompt.resolve({
75+
show: true,
76+
name: "Ada",
77+
count: 0,
78+
enabled: false,
79+
nil: null,
80+
disabled: false,
81+
});
82+
83+
expect(result.text).toBe("Hello Ada: 0 / false / / Outside: Ada");
84+
});
85+
});

‎packages/trigger-sdk/src/v3/prompt.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ export type PromptVariables<T extends AnyPromptHandle> =
7070
function compileTemplate(template: string, variables: Record<string, unknown>): string {
7171
// Handle conditional sections: {{#key}}...{{/key}}
7272
let result = template.replace(/\{\{#(\w+)\}\}([\s\S]*?)\{\{\/\1\}\}/g, (_match, key, content) => {
73-
const value = variables[key];
74-
return value
75-
? content.replace(/\{\{(\w+)\}\}/g, (_m: string, k: string) => {
76-
return String(variables[k] ?? "");
77-
})
78-
: "";
73+
// oxlint-disable-next-line prefer-object-has-own -- Use the explicit own-property check required here.
74+
const value = Object.prototype.hasOwnProperty.call(variables, key) ? variables[key] : undefined;
75+
return value ? content : "";
7976
});
8077

81-
// Handle simple substitutions: {{key}}
78+
// Substitute only once, so inserted values are never interpreted as template syntax.
8279
result = result.replace(/\{\{\s*(\w+)\s*\}\}/g, (_match, key) => {
83-
const value = variables[key];
80+
// oxlint-disable-next-line prefer-object-has-own -- Use the explicit own-property check required here.
81+
const value = Object.prototype.hasOwnProperty.call(variables, key) ? variables[key] : undefined;
8482
return value !== undefined && value !== null ? String(value) : "";
8583
});
8684

0 commit comments

Comments
 (0)