ChatMessage.sys.mjs (7530B)
1 /* 2 This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 5 6 import { makeGuid } from "./ChatUtils.sys.mjs"; 7 8 /** 9 * A message in a conversation. 10 */ 11 export class ChatMessage { 12 id; 13 createdDate; 14 parentMessageId; 15 revisionRootMessageId; 16 ordinal; 17 isActiveBranch; 18 role; 19 modelId; 20 params; 21 usage; 22 content; 23 convId; 24 pageUrl; 25 turnIndex; 26 insightsEnabled; 27 insightsFlagSource; 28 insightsApplied; 29 webSearchQueries; 30 31 /** 32 * @param {object} param 33 * @param {number} param.ordinal - The order of the message 34 * @param {MessageRole} param.role - The message role 35 * @param {object} param.content - The message content object 36 * @param {number} param.turnIndex - The message turn, different than ordinal, 37 * prompt/reply for example would be one turn 38 * @param {string} [param.pageUrl = null] - A URL object defining which page 39 * the user was on when submitting a message if role == user 40 * @param {string} [param.id = makeGuid()] - The row.message_id of the 41 * message in the database 42 * @param {number} [param.createdDate = Date.now()] - The date the message was 43 * sent/stored in the database 44 * @param {string} [param.parentMessageId = null] - The id of the message 45 * which came before this message when it was added to the conversation, 46 * null if its the first message of the converation 47 * @param {string} [param.convId = null] - The id of the conversation the 48 * message belongs to 49 * @param {?boolean} param.insightsEnabled - Whether insights were enabled 50 * when the message was submitted if role == assistant 51 * @param {InsightsFlagSource} param.insightsFlagSource - How the 52 * insightsEnabled flag was determined if role == assistant, one of 53 * INSIGHTS_FLAG_SOURCE.GLOBAL, INSIGHTS_FLAG_SOURCE.CONVERSATION, 54 * INSIGHTS_FLAG_SOURCE.MESSAGE_ONCE 55 * @param {?Array<string>} param.insightsApplied - List of strings of insights 56 * that were applied to a response if insightsEnabled == true 57 * @param {?Array<string>} param.webSearchQueries - List of strings of web 58 * search queries that were applied to a response if role == assistant 59 * @param {object} [param.params = null] - Model params used if role == assistant|tool 60 * @param {object} [param.usage = null] - Token usage data for the current 61 * response if role == assistant 62 * @param {string} [param.modelId = null] - The model used for content 63 * generation if role == assistant|tool 64 * @param {string} [param.revisionRootMessageId = id] - Reference to the root 65 * of this branch, which ID a message branched from. Should be set to the 66 * same value as id when a message is first created. If a message is 67 * edited/regenerated revisionRootMessageId should remain the same for 68 * subsequent edits/regenerations, the id would diverge for subsequent 69 * edits/regenerations. 70 * @param {boolean} [param.isActiveBranch = true] - Defaults to true when a 71 * message is originally generated. If a message is edited/regenerated, the 72 * edited message turns to false and the newly edited/regenerated message is 73 * the only message of the revision branch set to true. 74 */ 75 constructor({ 76 ordinal, 77 role, 78 content, 79 turnIndex, 80 pageUrl = null, 81 id = makeGuid(), 82 createdDate = Date.now(), 83 parentMessageId = null, 84 convId = null, 85 insightsEnabled = null, 86 insightsFlagSource = null, 87 insightsApplied = null, 88 webSearchQueries = null, 89 params = null, 90 usage = null, 91 modelId = null, 92 revisionRootMessageId = id, 93 isActiveBranch = true, 94 }) { 95 this.id = id; 96 this.createdDate = createdDate; 97 this.parentMessageId = parentMessageId; 98 this.revisionRootMessageId = revisionRootMessageId; 99 this.isActiveBranch = isActiveBranch; 100 this.ordinal = ordinal; 101 this.role = role; 102 this.modelId = modelId; 103 this.params = params; 104 this.usage = usage; 105 this.content = content; 106 this.convId = convId; 107 this.pageUrl = pageUrl ? new URL(pageUrl) : null; 108 this.turnIndex = turnIndex; 109 this.insightsEnabled = insightsEnabled; 110 this.insightsFlagSource = insightsFlagSource; 111 this.insightsApplied = insightsApplied; 112 this.webSearchQueries = webSearchQueries; 113 } 114 } 115 116 /** 117 * Options required for a conversation message with 118 * role of assistant 119 */ 120 export class AssistantRoleOpts { 121 insightsEnabled; 122 insightsFlagSource; 123 insightsApplied; 124 webSearchQueries; 125 params; 126 usage; 127 modelId; 128 129 /** 130 * @param {string} [modelId=null] 131 * @param {object} [params=null] - The model params used 132 * @param {object} [usage=null] - Token usage data for the current response 133 * @param {boolean} [insightsEnabled=false] - Whether insights were enabled when the message was submitted 134 * @param {import("moz-src:///browser/components/aiwindow/ui/modules/ChatStorage.sys.mjs").InsightsFlagSource} [insightsFlagSource=null] - How the insightsEnabled flag was determined 135 * @param {?Array<string>} [insightsApplied=[]] - List of strings of insights that were applied to a response 136 * @param {?Array<string>} [webSearchQueries=[]] - List of strings of web search queries that were applied to a response 137 */ 138 constructor( 139 modelId = null, 140 params = null, 141 usage = null, 142 insightsEnabled = false, 143 insightsFlagSource = null, 144 insightsApplied = [], 145 webSearchQueries = [] 146 ) { 147 this.insightsEnabled = insightsEnabled; 148 this.insightsFlagSource = insightsFlagSource; 149 this.insightsApplied = insightsApplied; 150 this.webSearchQueries = webSearchQueries; 151 this.params = params; 152 this.usage = usage; 153 this.modelId = modelId; 154 } 155 } 156 157 /** 158 * Options required for a conversation message with 159 * role of assistant 160 */ 161 export class ToolRoleOpts { 162 modelId; 163 164 /** 165 * @param {string} [modelId=null] 166 */ 167 constructor(modelId = null) { 168 this.modelId = modelId; 169 } 170 } 171 172 /** 173 * Options required for a conversation message with 174 * role of user 175 */ 176 export class UserRoleOpts { 177 revisionRootMessageId; 178 179 /** 180 * @param {string} [revisionRootMessageId=undefined] 181 */ 182 constructor(revisionRootMessageId) { 183 if (revisionRootMessageId) { 184 this.revisionRootMessageId = revisionRootMessageId; 185 } 186 } 187 } 188 189 /** 190 * Used to retrieve chat entries for the History app menu 191 */ 192 export class ChatMinimal { 193 #id; 194 #title; 195 196 /** 197 * @param {object} params 198 * @param {string} params.convId 199 * @param {string} params.title 200 */ 201 constructor({ convId, title }) { 202 this.#id = convId; 203 this.#title = title; 204 } 205 206 get id() { 207 return this.#id; 208 } 209 210 get title() { 211 return this.#title; 212 } 213 } 214 215 /** 216 * Used to retrieve chat entries for Chat History view 217 */ 218 export class ChatHistoryResult { 219 #convId; 220 #title; 221 #createdDate; 222 #updatedDate; 223 #urls; 224 225 constructor({ convId, title, createdDate, updatedDate, urls }) { 226 this.#convId = convId; 227 this.#title = title; 228 this.#createdDate = createdDate; 229 this.#updatedDate = updatedDate; 230 this.#urls = urls; 231 } 232 233 /** 234 * @returns {string} 235 */ 236 get convId() { 237 return this.#convId; 238 } 239 240 /** 241 * @returns {string} 242 */ 243 get title() { 244 return this.#title; 245 } 246 247 /** 248 * @returns {Date} 249 */ 250 get createdDate() { 251 return this.#createdDate; 252 } 253 254 /** 255 * @returns {Date} 256 */ 257 get updatedDate() { 258 return this.#updatedDate; 259 } 260 261 /** 262 * @returns {Array<URL>} 263 */ 264 get urls() { 265 return this.#urls; 266 } 267 }