22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5- import { ChatMessage , Tool , ToolResult , ToolResultStatus , ToolUse } from '@amzn/codewhisperer-streaming'
5+ import { ChatMessage , ToolResult , ToolResultStatus , ToolUse } from '@amzn/codewhisperer-streaming'
66import { randomUUID } from '../../shared/crypto'
77import { getLogger } from '../../shared/logger/logger'
8- import { tools } from '../constants'
98
109// Maximum number of messages to keep in history
1110const MaxConversationHistoryLength = 100
@@ -20,12 +19,10 @@ export class ChatHistoryManager {
2019 private history : ChatMessage [ ] = [ ]
2120 private logger = getLogger ( )
2221 private lastUserMessage ?: ChatMessage
23- private tools : Tool [ ] = [ ]
2422
2523 constructor ( tabId ?: string ) {
2624 this . conversationId = randomUUID ( )
2725 this . tabId = tabId ?? randomUUID ( )
28- this . tools = tools
2926 }
3027
3128 /**
@@ -97,10 +94,10 @@ export class ChatHistoryManager {
9794 * 4. If the last message is from the assistant and it contains tool uses, and a next user
9895 * message is set without tool results, then the user message will have cancelled tool results.
9996 */
100- public fixHistory ( newUserMessage : ChatMessage ) : ChatMessage {
97+ public fixHistory ( newUserMessage : ChatMessage ) : void {
10198 this . trimConversationHistory ( )
10299 this . ensureLastMessageFromAssistant ( )
103- return this . handleToolUses ( newUserMessage )
100+ this . ensureCurrentMessageIsValid ( newUserMessage )
104101 }
105102
106103 private trimConversationHistory ( ) : void {
@@ -145,42 +142,27 @@ export class ChatHistoryManager {
145142 }
146143 }
147144
148- private handleToolUses ( newUserMessage : ChatMessage ) : ChatMessage {
145+ private ensureCurrentMessageIsValid ( newUserMessage : ChatMessage ) : void {
149146 const lastHistoryMessage = this . history [ this . history . length - 1 ]
150- if ( ! lastHistoryMessage || ! lastHistoryMessage . assistantResponseMessage || ! newUserMessage ) {
151- return newUserMessage
152- }
153-
154- const toolUses = lastHistoryMessage . assistantResponseMessage . toolUses
155- if ( ! toolUses || toolUses . length === 0 ) {
156- return newUserMessage
157- }
158-
159- return this . addToolResultsToUserMessage ( newUserMessage , toolUses )
160- }
161-
162- private addToolResultsToUserMessage ( newUserMessage : ChatMessage , toolUses : ToolUse [ ] ) : ChatMessage {
163- if ( ! newUserMessage . userInputMessage ) {
164- return newUserMessage
147+ if ( ! lastHistoryMessage ) {
148+ return
165149 }
166150
167- const toolResults = this . createToolResults ( toolUses )
151+ if ( lastHistoryMessage . assistantResponseMessage ?. toolUses ?. length ) {
152+ const toolResults = newUserMessage . userInputMessage ?. userInputMessageContext ?. toolResults
153+ if ( ! toolResults || toolResults . length === 0 ) {
154+ const abandonedToolResults = this . createAbandonedToolResults (
155+ lastHistoryMessage . assistantResponseMessage . toolUses
156+ )
168157
169- if ( newUserMessage . userInputMessage . userInputMessageContext ) {
170- newUserMessage . userInputMessage . userInputMessageContext . toolResults = toolResults
171- } else {
172- newUserMessage . userInputMessage . userInputMessageContext = {
173- shellState : undefined ,
174- envState : undefined ,
175- toolResults : toolResults ,
176- tools : this . tools . length === 0 ? undefined : [ ...this . tools ] ,
158+ if ( newUserMessage . userInputMessage ?. userInputMessageContext ) {
159+ newUserMessage . userInputMessage . userInputMessageContext . toolResults = abandonedToolResults
160+ }
177161 }
178162 }
179-
180- return newUserMessage
181163 }
182164
183- private createToolResults ( toolUses : ToolUse [ ] ) : ToolResult [ ] {
165+ private createAbandonedToolResults ( toolUses : ToolUse [ ] ) : ToolResult [ ] {
184166 return toolUses . map ( ( toolUse ) => ( {
185167 toolUseId : toolUse . toolUseId ,
186168 content : [
0 commit comments