Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions packages/core/src/codewhispererChat/clients/chat/v0/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import { createQDeveloperStreamingClient } from '../../../../shared/clients/qDev
import { UserWrittenCodeTracker } from '../../../../codewhisperer/tracker/userWrittenCodeTracker'
import { PromptMessage } from '../../../controllers/chat/model'
import { FsWriteBackup } from '../../../../codewhispererChat/tools/fsWrite'
import { randomUUID } from '../../../../shared/crypto'

export type ToolUseWithError = {
toolUse: ToolUse
error: Error | undefined
}
import { getLogger } from '../../../../shared/logger/logger'
import { randomUUID } from '../../../../shared/crypto'

export class ChatSession {
private sessionId?: string
private sessionId: string
/**
* _readFiles = list of files read from the project to gather context before generating response.
* _showDiffOnFileWrite = Controls whether to show diff view (true) or file context view (false) to the user
Expand All @@ -46,7 +45,7 @@ export class ChatSession {
// TODO: doesn't handle the edge case when two files share the same relativePath string but from different root
// e.g. root_a/file1 vs root_b/file1
relativePathToWorkspaceRoot: Map<string, string> = new Map()
public get sessionIdentifier(): string | undefined {
public get sessionIdentifier(): string {
return this.sessionId
}

Expand Down Expand Up @@ -86,13 +85,14 @@ export class ChatSession {

constructor() {
this.createNewTokenSource()
this.sessionId = randomUUID()
}

createNewTokenSource() {
this.tokenSource = new vscode.CancellationTokenSource()
}

public setSessionID(id?: string) {
public setSessionID(id: string) {
this.sessionId = id
}
public get readFiles(): string[] {
Expand Down Expand Up @@ -120,14 +120,6 @@ export class ChatSession {
)
}

const responseStream = response.sendMessageResponse
for await (const event of responseStream) {
if ('messageMetadataEvent' in event) {
this.sessionId = event.messageMetadataEvent?.conversationId
break
}
}

UserWrittenCodeTracker.instance.onQFeatureInvoked()
return response
}
Expand All @@ -142,12 +134,6 @@ export class ChatSession {
)
}

this.sessionId = response.conversationId
if (this.sessionId?.length === 0) {
getLogger().debug(`Session ID: ${this.sessionId} is empty. Generating random UUID`)
this.sessionId = randomUUID()
}

UserWrittenCodeTracker.instance.onQFeatureInvoked()

return response
Expand Down
29 changes: 8 additions & 21 deletions packages/core/src/codewhispererChat/controllers/chat/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ import { amazonQTabSuffix } from '../../../shared/constants'
import { OutputKind } from '../../tools/toolShared'
import { ToolUtils, Tool, ToolType } from '../../tools/toolUtils'
import { ChatStream } from '../../tools/chatStream'
import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
import { tempDirPath } from '../../../shared/filesystemUtilities'
import { Database } from '../../../shared/db/chatDb/chatDb'
import { TabBarController } from './tabBarController'
import { messageToChatMessage } from '../../../shared/db/chatDb/util'

export interface ChatControllerMessagePublishers {
readonly processPromptChatMessage: MessagePublisher<PromptMessage>
Expand Down Expand Up @@ -175,7 +175,6 @@ export class ChatController {
private readonly userIntentRecognizer: UserIntentRecognizer
private readonly telemetryHelper: CWCTelemetryHelper
private userPromptsWatcher: vscode.FileSystemWatcher | undefined
private readonly chatHistoryStorage: ChatHistoryStorage
private chatHistoryDb = Database.getInstance()
private cancelTokenSource: vscode.CancellationTokenSource = new vscode.CancellationTokenSource()

Expand All @@ -195,7 +194,6 @@ export class ChatController {
this.editorContentController = new EditorContentController()
this.promptGenerator = new PromptsGenerator()
this.userIntentRecognizer = new UserIntentRecognizer()
this.chatHistoryStorage = new ChatHistoryStorage()
this.tabBarController = new TabBarController(this.messenger)

onDidChangeAmazonQVisibility((visible) => {
Expand Down Expand Up @@ -417,7 +415,7 @@ export class ChatController {
const session = this.sessionStorage.getSession(message.tabID)
session.tokenSource.cancel()
this.messenger.sendEmptyMessage(message.tabID, '', undefined)
this.chatHistoryStorage.getTabHistory(message.tabID).clearRecentHistory()
this.chatHistoryDb.clearRecentHistory(message.tabID)
this.telemetryHelper.recordInteractionWithAgenticChat(AgenticChatInteractionType.StopChat, message)
}

Expand Down Expand Up @@ -475,7 +473,6 @@ export class ChatController {

private async processTabCloseMessage(message: TabClosedMessage) {
this.sessionStorage.deleteSession(message.tabID)
this.chatHistoryStorage.deleteHistory(message.tabID)
this.triggerEventsStorage.removeTabEvents(message.tabID)
// this.telemetryHelper.recordCloseChat(message.tabID)
this.chatHistoryDb.updateTabOpenState(message.tabID, false)
Expand Down Expand Up @@ -1039,7 +1036,7 @@ export class ChatController {
getLogger().error(`error: ${errorMessage} tabID: ${tabID} requestID: ${requestID}`)

this.sessionStorage.deleteSession(tabID)
this.chatHistoryStorage.getTabHistory(tabID).clear()
this.chatHistoryDb.clearTab(tabID)
}

private async processContextMenuCommand(command: EditorContextCommand) {
Expand Down Expand Up @@ -1161,7 +1158,6 @@ export class ChatController {
switch (message.command) {
case 'clear':
this.sessionStorage.deleteSession(message.tabID)
this.chatHistoryStorage.getTabHistory(message.tabID).clear()
this.triggerEventsStorage.removeTabEvents(message.tabID)
recordTelemetryChatRunCommand('clear')
this.chatHistoryDb.clearTab(message.tabID)
Expand Down Expand Up @@ -1452,10 +1448,6 @@ export class ChatController {
}

const session = this.sessionStorage.getSession(tabID)
if (!session.localHistoryHydrated) {
triggerPayload.history = this.chatHistoryDb.getMessages(triggerEvent.tabID, 10)
session.localHistoryHydrated = true
}
await this.resolveContextCommandPayload(triggerPayload, session)
triggerPayload.useRelevantDocuments = triggerPayload.context.some(
(context) => typeof context !== 'string' && context.command === '@workspace'
Expand Down Expand Up @@ -1494,16 +1486,14 @@ export class ChatController {

const request = triggerPayloadToChatRequest(triggerPayload)

const chatHistory = this.chatHistoryStorage.getTabHistory(tabID)
const currentMessage = request.conversationState.currentMessage
if (currentMessage) {
chatHistory.fixHistory(currentMessage)
this.chatHistoryDb.fixHistory(tabID, currentMessage)
}
request.conversationState.history = chatHistory.getHistory()

const conversationId = chatHistory.getConversationId() || randomUUID()
chatHistory.setConversationId(conversationId)
request.conversationState.conversationId = conversationId
request.conversationState.history = this.chatHistoryDb
.getMessages(tabID)
.map((chat) => messageToChatMessage(chat))
request.conversationState.conversationId = session.sessionIdentifier

triggerPayload.documentReferences = this.mergeRelevantTextDocuments(triggerPayload.relevantTextDocuments)

Expand Down Expand Up @@ -1562,7 +1552,6 @@ export class ChatController {
this.telemetryHelper.recordStartConversation(triggerEvent, triggerPayload)

if (currentMessage && session.sessionIdentifier) {
chatHistory.appendUserMessage(currentMessage)
this.chatHistoryDb.addMessage(tabID, 'cwc', session.sessionIdentifier, {
body: triggerPayload.message,
type: 'prompt' as any,
Expand All @@ -1577,15 +1566,13 @@ export class ChatController {
response.$metadata.requestId
} metadata: ${inspect(response.$metadata, { depth: 12 })}`
)

this.cancelTokenSource = new vscode.CancellationTokenSource()
await this.messenger.sendAIResponse(
response,
session,
tabID,
triggerID,
triggerPayload,
chatHistory,
this.cancelTokenSource.token
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
} from '@aws/mynah-ui'
import { Database } from '../../../../shared/db/chatDb/chatDb'
import { TabType } from '../../../../amazonq/webview/ui/storages/tabsStorage'
import { ChatHistoryManager } from '../../../storages/chatHistory'
import { ToolType, ToolUtils } from '../../../tools/toolUtils'
import { ChatStream } from '../../../tools/chatStream'
import path from 'path'
Expand Down Expand Up @@ -185,7 +184,6 @@ export class Messenger {
tabID: string,
triggerID: string,
triggerPayload: TriggerPayload,
chatHistoryManager: ChatHistoryManager,
cancelToken: vscode.CancellationToken
) {
let message = ''
Expand Down Expand Up @@ -508,17 +506,6 @@ export class Messenger {
)
)

chatHistoryManager.pushAssistantMessage({
assistantResponseMessage: {
messageId: messageID,
content: message,
references: codeReference,
...(toolUse &&
toolUse.input !== undefined &&
toolUse.input !== '' && { toolUses: [{ ...toolUse }] }),
},
})

getLogger().info(
`All events received. requestId=%s counts=%s`,
response.$metadata.requestId,
Expand Down
Loading