Skip to content

Commit 3f5b097

Browse files
committed
use session in chatHistory
1 parent e7ccaea commit 3f5b097

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,8 @@ export class ChatController {
785785
const promptTypeValue = message.optionsValues['prompt-type']
786786
// TODO: display message: You turned off pair programmer mode. Q will not include code diffs or run commands in the chat.
787787
if (promptTypeValue === 'pair-programming-on') {
788-
this.chatHistoryStorage.setTools(message.tabID, tools)
789788
session.setPairProgrammingModeOn(true)
790789
} else {
791-
this.chatHistoryStorage.setTools(message.tabID, noWriteTools)
792790
session.setPairProgrammingModeOn(false)
793791
}
794792
}

packages/core/src/codewhispererChat/storages/chatHistory.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
*/
55
import {
66
ChatMessage,
7-
Tool,
87
ToolResult,
98
ToolResultStatus,
109
UserInputMessage,
1110
UserInputMessageContext,
1211
} from '@amzn/codewhisperer-streaming'
1312
import { randomUUID } from '../../shared/crypto'
1413
import { getLogger } from '../../shared/logger/logger'
15-
import { tools } from '../constants'
14+
import { tools, noWriteTools } from '../constants'
15+
import { ChatSessionStorage } from './chatSession'
1616

1717
// Maximum number of messages to keep in history
1818
const MaxConversationHistoryLength = 100
@@ -27,12 +27,12 @@ export class ChatHistoryManager {
2727
private history: ChatMessage[] = []
2828
private logger = getLogger()
2929
private lastUserMessage?: ChatMessage
30-
private tools: Tool[] = []
30+
private sessionStorage: ChatSessionStorage
3131

3232
constructor(tabId?: string) {
3333
this.conversationId = randomUUID()
3434
this.tabId = tabId ?? randomUUID()
35-
this.tools = tools
35+
this.sessionStorage = new ChatSessionStorage()
3636
}
3737

3838
/**
@@ -46,10 +46,6 @@ export class ChatHistoryManager {
4646
this.conversationId = conversationId
4747
}
4848

49-
public setTools(tools: Tool[]) {
50-
this.tools = tools
51-
}
52-
5349
/**
5450
* Get the tab ID
5551
*/
@@ -182,11 +178,14 @@ export class ChatHistoryManager {
182178
status: ToolResultStatus.ERROR,
183179
}))
184180

181+
const session = this.sessionStorage.getSession(this.tabId)
182+
const availableTools = session.pairProgrammingModeOn ? tools : noWriteTools
183+
185184
newUserMessage.userInputMessage.userInputMessageContext = {
186185
shellState: undefined,
187186
envState: undefined,
188187
toolResults: toolResults,
189-
tools: this.tools.length === 0 ? undefined : [...this.tools],
188+
tools: availableTools.length === 0 ? undefined : [...availableTools],
190189
}
191190

192191
return newUserMessage
@@ -203,11 +202,14 @@ export class ChatHistoryManager {
203202
* Adds tool results to the conversation.
204203
*/
205204
addToolResults(toolResults: ToolResult[]): void {
205+
const session = this.sessionStorage.getSession(this.tabId)
206+
const availableTools = session.pairProgrammingModeOn ? tools : noWriteTools
207+
206208
const userInputMessageContext: UserInputMessageContext = {
207209
shellState: undefined,
208210
envState: undefined,
209211
toolResults: toolResults,
210-
tools: this.tools.length === 0 ? undefined : [...this.tools],
212+
tools: availableTools.length === 0 ? undefined : [...availableTools],
211213
}
212214

213215
const msg: UserInputMessage = {
@@ -252,12 +254,15 @@ export class ChatHistoryManager {
252254
status: ToolResultStatus.ERROR,
253255
}))
254256

257+
const session = this.sessionStorage.getSession(this.tabId)
258+
const availableTools = session.pairProgrammingModeOn ? tools : noWriteTools
259+
255260
// Create a new user message with cancelled tool results
256261
const userInputMessageContext: UserInputMessageContext = {
257262
shellState: undefined,
258263
envState: undefined,
259264
toolResults: toolResults,
260-
tools: this.tools.length === 0 ? undefined : [...this.tools],
265+
tools: availableTools.length === 0 ? undefined : [...availableTools],
261266
}
262267

263268
const userMessage: ChatMessage = {

packages/core/src/codewhispererChat/storages/chatHistoryStorage.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { Tool } from '@amzn/codewhisperer-streaming'
76
import { ChatHistoryManager } from './chatHistory'
87

98
/**
@@ -41,8 +40,4 @@ export class ChatHistoryStorage {
4140
public deleteHistory(tabId: string) {
4241
this.histories.delete(tabId)
4342
}
44-
45-
public setTools(tabId: string, tools: Tool[]) {
46-
this.histories.get(tabId)?.setTools(tools)
47-
}
4843
}

0 commit comments

Comments
 (0)