Skip to content

Commit 47230d5

Browse files
authored
fix(action): handle edge case for add to context action (RooCodeInc#2780)
1 parent 612b948 commit 47230d5

File tree

2 files changed

+132
-11
lines changed

2 files changed

+132
-11
lines changed

src/shared/__tests__/support-prompts.test.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,123 @@ describe("Code Action Prompts", () => {
8686
})
8787
})
8888

89+
describe("ADD_TO_CONTEXT action", () => {
90+
it("should format ADD_TO_CONTEXT prompt correctly with all parameters", () => {
91+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
92+
name: "Roo",
93+
place: "Workspace",
94+
filePath: testFilePath,
95+
selectedText: testCode,
96+
startLine: "1",
97+
endLine: "1",
98+
diagnostics: [],
99+
})
100+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
101+
expect(prompt).toBe(expected)
102+
})
103+
104+
it("should format ADD_TO_CONTEXT prompt with diagnostics", () => {
105+
const diagnostics = [{ message: "Error 1" }, { source: "Linter", message: "Warning 2" }]
106+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
107+
filePath: testFilePath,
108+
selectedText: testCode,
109+
startLine: "10",
110+
endLine: "20",
111+
diagnostics,
112+
})
113+
const expected = `${testFilePath}:10-20\n\`\`\`\n${testCode}\n\`\`\``
114+
expect(prompt).toBe(expected)
115+
})
116+
117+
it("should not replace placeholders within parameter values", () => {
118+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
119+
value1: "This is ${value2}",
120+
value2: "Actual Value 2",
121+
filePath: testFilePath,
122+
selectedText: testCode,
123+
startLine: "5",
124+
endLine: "15",
125+
})
126+
const expected = `${testFilePath}:5-15\n\`\`\`\n${testCode}\n\`\`\``
127+
expect(prompt).toBe(expected)
128+
})
129+
130+
it("should replace remaining placeholders (not in params) with empty strings", () => {
131+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
132+
name: "Roo",
133+
filePath: testFilePath,
134+
selectedText: testCode,
135+
startLine: "1",
136+
endLine: "1",
137+
}) // 'status' is missing
138+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
139+
expect(prompt).toBe(expected)
140+
})
141+
142+
it("should handle placeholders in values that are not in the template", () => {
143+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
144+
data: "Some data with ${extraInfo}",
145+
filePath: testFilePath,
146+
selectedText: testCode,
147+
startLine: "1",
148+
endLine: "1",
149+
})
150+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
151+
expect(prompt).toBe(expected)
152+
})
153+
154+
it("should handle minimal params object", () => {
155+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
156+
filePath: testFilePath,
157+
selectedText: testCode,
158+
startLine: "1",
159+
endLine: "1",
160+
})
161+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
162+
expect(prompt).toBe(expected)
163+
})
164+
165+
it("should handle params with non-string values", () => {
166+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
167+
count: "5",
168+
isActive: "true",
169+
filePath: testFilePath,
170+
selectedText: testCode,
171+
startLine: "1",
172+
endLine: "1",
173+
}) // Convert to strings
174+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
175+
expect(prompt).toBe(expected)
176+
})
177+
178+
it("should handle keys with special regex characters", () => {
179+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
180+
"key.with.dots": "Dotty",
181+
value: "Simple",
182+
filePath: testFilePath,
183+
selectedText: testCode,
184+
startLine: "1",
185+
endLine: "1",
186+
})
187+
const expected = `${testFilePath}:1-1\n\`\`\`\n${testCode}\n\`\`\``
188+
expect(prompt).toBe(expected)
189+
})
190+
191+
it("should handle bash script selection", () => {
192+
const bashText =
193+
'if [ "${#usecase_deployments[@]}" -gt 0 ] && [ ${{ parameters.single_deployment_per_environment }} = true ]; then'
194+
const prompt = supportPrompt.create("ADD_TO_CONTEXT", {
195+
selectedText: bashText,
196+
filePath: testFilePath,
197+
startLine: "1",
198+
endLine: "1",
199+
diagnostics: [],
200+
})
201+
const expected = `${testFilePath}:1-1\n\`\`\`\n${bashText}\n\`\`\``
202+
expect(prompt).toBe(expected)
203+
})
204+
})
205+
89206
describe("get template", () => {
90207
it("should return default template when no custom prompts provided", () => {
91208
const template = supportPrompt.get(undefined, "EXPLAIN")

src/shared/support-prompt.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ const generateDiagnosticText = (diagnostics?: any[]) => {
99
}
1010

1111
export const createPrompt = (template: string, params: PromptParams): string => {
12-
let result = template
13-
for (const [key, value] of Object.entries(params)) {
14-
if (key === "diagnostics") {
15-
result = result.replaceAll("${diagnosticText}", generateDiagnosticText(value as any[]))
12+
return template.replace(/\${(.*?)}/g, (_, key) => {
13+
if (key === "diagnosticText") {
14+
return generateDiagnosticText(params["diagnostics"] as any[])
15+
} else if (params.hasOwnProperty(key)) {
16+
// Ensure the value is treated as a string for replacement
17+
const value = params[key]
18+
if (typeof value === "string") {
19+
return value
20+
} else {
21+
// Convert non-string values to string for replacement
22+
return String(value)
23+
}
1624
} else {
17-
result = result.replaceAll(`\${${key}}`, value as string)
25+
// If the placeholder key is not in params, replace with empty string
26+
return ""
1827
}
19-
}
20-
21-
// Replace any remaining placeholders with empty strings
22-
result = result.replaceAll(/\${[^}]*}/g, "")
23-
24-
return result
28+
})
2529
}
2630

2731
interface SupportPromptConfig {

0 commit comments

Comments
 (0)