Closed Bug 2091569 Opened 6 days ago Closed 4 days ago

renderPrompt should single pass substitute placeholders like GenAI.buildChatPrompt

Categories

(Core :: Machine Learning: General, task)

task

Tracking

()

RESOLVED FIXED
162 Branch
Tracking Status
firefox162 --- fixed

People

(Reporter: Mardak, Assigned: Mardak)

References

(Blocks 1 open bug)

Details

(Whiteboard: [aiasst])

Attachments

(1 external file)

renderPrompt in browser/components/aiwindow/models/Utils.sys.mjs fills each key with its own replace call over the result of the previous ones:

for (const [orig, repl] of Object.entries(stringsToReplace)) {
  const regex = new RegExp(`{${orig}}`, "g");
  finalPromptContent = finalPromptContent.replace(regex, () => repl);
}

So the output depends on key order: text that matches a later key placeholder inside an earlier value is replaced too. For example, generateConversationStartersSidebar fills current_tab before date, so a tab titled "Notes for {date}" reaches the model with today date in its title instead of the literal text.

GenAI.buildChatPrompt in browser/components/genai/GenAI.sys.mjs fills all placeholders in a single template.replace(regex, callback), so inserted values are never scanned again.

renderPrompt could do the same after handling {#key}...{/key} sections:

return finalPromptContent.replace(/\{(\w+)\}/g, (match, key) =>
  Object.hasOwn(stringsToReplace, key) ? String(stringsToReplace[key]) : match
);

Output is unchanged except when a value contains another key placeholder. Unknown placeholders are still left as they are, and $ in values stays literal because the replacement is a callback. An xpcshell test where one value contains another key placeholder would cover it.

use regex replace with callback instead of looping

Assignee: nobody → edilee
Status: NEW → ASSIGNED
Pushed by elee@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/5ea5424d1b47 https://hg.mozilla.org/integration/autoland/rev/131ed04548e1 renderPrompt should single pass substitute placeholders like GenAI.buildChatPrompt r=cdipersio
Status: ASSIGNED → RESOLVED
Closed: 4 days ago
Resolution: --- → FIXED
Target Milestone: --- → 162 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: