in server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/chatDb.ts [297:342]
addMessage(tabId: string, tabType: TabType, conversationId: string, message: Message) {
if (this.#initialized) {
const clientType = this.#features.lsp.getClientInitializeParams()?.clientInfo?.name || 'unknown'
const tabCollection = this.#db.getCollection<Tab>(TabCollection)
this.#features.logging.log(
`Adding message to history: tabId=${tabId}, tabType=${tabType}, conversationId=${conversationId}`
)
let historyId = this.#historyIdMapping.get(tabId)
if (!historyId) {
historyId = crypto.randomUUID()
this.#features.logging.log(`Creating new historyId=${historyId} for tabId=${tabId}`)
this.setHistoryIdMapping(tabId, historyId)
}
const tabData = historyId ? tabCollection.findOne({ historyId }) : undefined
const tabTitle =
(message.type === 'prompt' && message.body.trim().length > 0 ? message.body : tabData?.title) ||
'Amazon Q Chat'
message = this.formatChatHistoryMessage(message)
if (tabData) {
this.#features.logging.log(`Updating existing tab with historyId=${historyId}`)
tabData.conversations = updateOrCreateConversation(
tabData.conversations,
conversationId,
message,
clientType
)
tabData.updatedAt = new Date()
tabData.title = tabTitle
tabCollection.update(tabData)
} else {
this.#features.logging.log(`Creating new tab with historyId=${historyId}`)
tabCollection.insert({
historyId,
updatedAt: new Date(),
isOpen: true,
tabType: tabType,
title: tabTitle,
conversations: [{ conversationId, clientType, messages: [message] }],
})
}
}
}