commit bd16379fe164e4af2c1a00b0214054c6f24ac661
parent 145c03dc3b3228b262bc666e3e321a4a51233aaa
Author: Omar Gonzalez <s9tpepper@apache.org>
Date: Fri, 19 Dec 2025 01:49:48 +0000
Bug 2006807: Add ChatConversation methods for renderState and currentTurnIndex r=ngrato,ai-frontend-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D277054
Diffstat:
2 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/browser/components/aiwindow/ui/modules/ChatConversation.sys.mjs b/browser/components/aiwindow/ui/modules/ChatConversation.sys.mjs
@@ -12,6 +12,8 @@ import {
UserRoleOpts,
} from "./ChatMessage.sys.mjs";
+const CHAT_ROLES = [MESSAGE_ROLE.USER, MESSAGE_ROLE.ASSISTANT];
+
/**
* A conversation containing messages.
*/
@@ -65,6 +67,31 @@ export class ChatConversation {
}
/**
+ * Returns a filtered messages array consisting only of the messages
+ * that are meant to be rendered as the chat conversation.
+ *
+ * @returns {Array<ChatMessage>}
+ */
+ renderState() {
+ const messages = this.#messages.filter(message => {
+ return CHAT_ROLES.includes(message.role);
+ });
+
+ return messages;
+ }
+
+ /**
+ * Returns the current turn index for the conversation
+ *
+ * @returns {number}
+ */
+ currentTurnIndex() {
+ return this.#messages.reduce((turnIndex, message) => {
+ return Math.max(turnIndex, message.turnIndex);
+ }, 0);
+ }
+
+ /**
* Adds a message to the conversation
*
* @param {ConversationRole} role - The type of conversation message
diff --git a/browser/components/aiwindow/ui/test/xpcshell/test_ChatConversation.js b/browser/components/aiwindow/ui/test/xpcshell/test_ChatConversation.js
@@ -410,3 +410,44 @@ add_task(function test_noBrowsing_ChatConversation_getMostRecentPageVisited() {
Assert.equal(mostRecentPageVisited, null);
});
+
+add_task(function test_ChatConversation_renderState() {
+ const conversation = new ChatConversation({});
+
+ const content = "user to assistant msg";
+
+ conversation.addUserMessage(content, "about:aiwindow", 0);
+ conversation.addToolCallMessage("some content", 0);
+ conversation.addAssistantMessage("text", "a response", 0);
+ conversation.addUserMessage(content, "about:aiwindow", 1);
+ conversation.addSystemMessage("text", "some system message", 1);
+ conversation.addAssistantMessage("text", "a response", 1);
+
+ const renderState = conversation.renderState();
+
+ Assert.deepEqual(renderState, [
+ conversation.messages[0],
+ conversation.messages[2],
+ conversation.messages[3],
+ conversation.messages[5],
+ ]);
+});
+
+add_task(function test_ChatConversation_currentTurnIndex() {
+ const conversation = new ChatConversation({});
+
+ const content = "user to assistant msg";
+
+ conversation.addUserMessage(content, "about:aiwindow", 0);
+ conversation.addAssistantMessage("text", "a response", 0);
+ conversation.addUserMessage(content, "about:aiwindow", 2);
+ conversation.addAssistantMessage("text", "a response", 2);
+ conversation.addUserMessage(content, "about:aiwindow", 1);
+ conversation.addAssistantMessage("text", "a response", 1);
+ conversation.addUserMessage(content, "about:aiwindow", 4);
+ conversation.addAssistantMessage("text", "a response", 4);
+ conversation.addUserMessage(content, "about:aiwindow", 3);
+ conversation.addAssistantMessage("text", "a response", 3);
+
+ Assert.deepEqual(conversation.currentTurnIndex(), 4);
+});