2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
- import { ChatMessage , Tool , ToolResult , ToolResultStatus , ToolUse } from '@amzn/codewhisperer-streaming'
5
+ import { ChatMessage , ToolResult , ToolResultStatus , ToolUse } from '@amzn/codewhisperer-streaming'
6
6
import { randomUUID } from '../../shared/crypto'
7
7
import { getLogger } from '../../shared/logger/logger'
8
- import { tools } from '../constants'
9
8
10
9
// Maximum number of messages to keep in history
11
10
const MaxConversationHistoryLength = 100
@@ -20,12 +19,10 @@ export class ChatHistoryManager {
20
19
private history : ChatMessage [ ] = [ ]
21
20
private logger = getLogger ( )
22
21
private lastUserMessage ?: ChatMessage
23
- private tools : Tool [ ] = [ ]
24
22
25
23
constructor ( tabId ?: string ) {
26
24
this . conversationId = randomUUID ( )
27
25
this . tabId = tabId ?? randomUUID ( )
28
- this . tools = tools
29
26
}
30
27
31
28
/**
@@ -97,10 +94,10 @@ export class ChatHistoryManager {
97
94
* 4. If the last message is from the assistant and it contains tool uses, and a next user
98
95
* message is set without tool results, then the user message will have cancelled tool results.
99
96
*/
100
- public fixHistory ( newUserMessage : ChatMessage ) : ChatMessage {
97
+ public fixHistory ( newUserMessage : ChatMessage ) : void {
101
98
this . trimConversationHistory ( )
102
99
this . ensureLastMessageFromAssistant ( )
103
- return this . handleToolUses ( newUserMessage )
100
+ this . ensureCurrentMessageIsValid ( newUserMessage )
104
101
}
105
102
106
103
private trimConversationHistory ( ) : void {
@@ -145,42 +142,27 @@ export class ChatHistoryManager {
145
142
}
146
143
}
147
144
148
- private handleToolUses ( newUserMessage : ChatMessage ) : ChatMessage {
145
+ private ensureCurrentMessageIsValid ( newUserMessage : ChatMessage ) : void {
149
146
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
165
149
}
166
150
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
+ )
168
157
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
+ }
177
161
}
178
162
}
179
-
180
- return newUserMessage
181
163
}
182
164
183
- private createToolResults ( toolUses : ToolUse [ ] ) : ToolResult [ ] {
165
+ private createAbandonedToolResults ( toolUses : ToolUse [ ] ) : ToolResult [ ] {
184
166
return toolUses . map ( ( toolUse ) => ( {
185
167
toolUseId : toolUse . toolUseId ,
186
168
content : [
0 commit comments