Skip to content

Commit b4b7a3d

Browse files
committed
Removing Environment state objects from converter and addressing other comments
1 parent f0d6050 commit b4b7a3d

File tree

5 files changed

+22
-118
lines changed

5 files changed

+22
-118
lines changed

packages/core/src/codewhispererChat/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
import * as path from 'path'
66
import fs from '../shared/fs/fs'
7+
import { Tool } from '@amzn/codewhisperer-streaming'
8+
import toolsJson from '../codewhispererChat/tools/tool_index.json'
79

810
export const promptFileExtension = '.md'
911

@@ -19,3 +21,10 @@ export const getUserPromptsDirectory = () => {
1921
}
2022

2123
export const createSavedPromptCommandId = 'create-saved-prompt'
24+
25+
export const tools: Tool[] = Object.entries(toolsJson).map(([, toolSpec]) => ({
26+
toolSpecification: {
27+
...toolSpec,
28+
inputSchema: { json: toolSpec.inputSchema },
29+
},
30+
}))

packages/core/src/codewhispererChat/controllers/chat/chatRequest/converter.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ import {
77
ConversationState,
88
CursorState,
99
DocumentSymbol,
10-
EnvState,
1110
RelevantTextDocument,
12-
ShellState,
1311
SymbolType,
1412
TextDocument,
15-
Tool,
1613
} from '@amzn/codewhisperer-streaming'
1714
import { ChatTriggerType, TriggerPayload } from '../model'
1815
import { undefinedIfEmpty } from '../../../../shared/utilities/textUtilities'
19-
import { tryGetCurrentWorkingDirectory } from '../../../../shared/utilities/workspaceUtils'
20-
import toolsJson from '../../../tools/tool_index.json'
21-
import { getOperatingSystem } from '../../../../shared/telemetry/util'
16+
import { tools } from '../../../constants'
2217

2318
const fqnNameSizeDownLimit = 1
2419
const fqnNameSizeUpLimit = 256
@@ -43,14 +38,6 @@ export const supportedLanguagesList = [
4338
const filePathSizeLimit = 4_000
4439
const customerMessageSizeLimit = 4_000
4540

46-
interface ToolSpec {
47-
name: string
48-
description: string
49-
// eslint-disable-next-line @typescript-eslint/naming-convention
50-
input_schema: Record<string, any>
51-
[key: string]: any
52-
}
53-
5441
export function triggerPayloadToChatRequest(triggerPayload: TriggerPayload): { conversationState: ConversationState } {
5542
let document: TextDocument | undefined = undefined
5643
let cursorState: CursorState | undefined = undefined
@@ -116,15 +103,6 @@ export function triggerPayloadToChatRequest(triggerPayload: TriggerPayload): { c
116103
const customizationArn: string | undefined = undefinedIfEmpty(triggerPayload.customization.arn)
117104
const chatTriggerType = triggerPayload.trigger === ChatTriggerType.InlineChatMessage ? 'INLINE_CHAT' : 'MANUAL'
118105

119-
const tools: Tool[] = Object.entries(toolsJson as Record<string, ToolSpec>).map(([toolName, toolSpec]) => ({
120-
toolSpecification: {
121-
...toolSpec,
122-
// Use the key as name if not already defined in the spec
123-
name: toolSpec.name || toolName,
124-
inputSchema: { json: toolSpec.input_schema },
125-
},
126-
}))
127-
128106
return {
129107
conversationState: {
130108
currentMessage: {
@@ -138,9 +116,8 @@ export function triggerPayloadToChatRequest(triggerPayload: TriggerPayload): { c
138116
cursorState,
139117
relevantDocuments,
140118
useRelevantDocuments,
119+
// TODO: Need workspace folders here after model update.
141120
},
142-
envState: buildEnvState(),
143-
shellState: buildShellState(),
144121
additionalContext: triggerPayload.additionalContents,
145122
tools,
146123
...(triggerPayload.toolResults !== undefined &&
@@ -157,26 +134,3 @@ export function triggerPayloadToChatRequest(triggerPayload: TriggerPayload): { c
157134
},
158135
}
159136
}
160-
161-
/**
162-
* Helper function to build environment state
163-
*/
164-
export function buildEnvState(): EnvState {
165-
return {
166-
operatingSystem: getOperatingSystem(),
167-
currentWorkingDirectory: tryGetCurrentWorkingDirectory(),
168-
}
169-
}
170-
171-
/**
172-
* Helper function to build shell state
173-
*/
174-
export function buildShellState(): ShellState {
175-
// In a real implementation, you would detect the shell
176-
// This is a simplified version
177-
const shellName = process.env.SHELL || 'bash'
178-
return {
179-
shellName: shellName.split('/').pop() || 'bash',
180-
shellHistory: undefined,
181-
}
182-
}

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
} from '@amzn/codewhisperer-streaming'
1313
import { randomUUID } from '../../shared/crypto'
1414
import { getLogger } from '../../shared/logger/logger'
15-
import toolsJson from '../tools/tool_index.json'
16-
import { buildEnvState, buildShellState } from '../controllers/chat/chatRequest/converter'
15+
import { tools } from '../constants'
1716

1817
// Maximum number of messages to keep in history
1918
const MaxConversationHistoryLength = 100
@@ -32,13 +31,7 @@ export class ChatHistoryManager {
3231
constructor() {
3332
this.conversationId = randomUUID()
3433
this.logger.info(`Generated new conversation id: ${this.conversationId}`)
35-
36-
this.tools = Object.entries(toolsJson).map(([, toolSpec]) => ({
37-
toolSpecification: {
38-
...toolSpec,
39-
inputSchema: { json: toolSpec.input_schema },
40-
},
41-
}))
34+
this.tools = tools
4235
}
4336

4437
/**
@@ -76,8 +69,6 @@ export class ChatHistoryManager {
7669
content: newMessage.userInputMessage?.content ?? '',
7770
userIntent: newMessage.userInputMessage?.userIntent ?? undefined,
7871
userInputMessageContext: newMessage.userInputMessage?.userInputMessageContext ?? {
79-
envState: buildEnvState(),
80-
shellState: buildShellState(),
8172
tools: this.tools,
8273
},
8374
origin: newMessage.userInputMessage?.origin ?? undefined,
@@ -184,7 +175,7 @@ export class ChatHistoryManager {
184175

185176
this.lastUserMessage.userInputMessage.userInputMessageContext = {
186177
shellState: undefined,
187-
envState: buildEnvState(),
178+
envState: undefined,
188179
toolResults: toolResults,
189180
tools: this.tools.length === 0 ? undefined : [...this.tools],
190181
}
@@ -200,7 +191,7 @@ export class ChatHistoryManager {
200191
addToolResults(toolResults: ToolResult[]): void {
201192
const userInputMessageContext: UserInputMessageContext = {
202193
shellState: undefined,
203-
envState: buildEnvState(),
194+
envState: undefined,
204195
toolResults: toolResults,
205196
tools: this.tools.length === 0 ? undefined : [...this.tools],
206197
}
@@ -214,36 +205,4 @@ export class ChatHistoryManager {
214205
this.lastUserMessage.userInputMessage = msg
215206
}
216207
}
217-
218-
/**
219-
* Sets the next user message with "cancelled" tool results.
220-
*/
221-
abandonToolUse(toolsToBeAbandoned: Array<[string, any]>, denyInput: string): void {
222-
const toolResults = toolsToBeAbandoned.map(([toolUseId]) => ({
223-
toolUseId,
224-
content: [
225-
{
226-
type: 'Text' as const,
227-
text: 'Tool use was cancelled by the user',
228-
},
229-
],
230-
status: ToolResultStatus.ERROR,
231-
}))
232-
233-
const userInputMessageContext: UserInputMessageContext = {
234-
shellState: undefined,
235-
envState: buildEnvState(),
236-
toolResults,
237-
tools: this.tools.length > 0 ? [...this.tools] : undefined,
238-
}
239-
240-
const msg: ChatMessage = {
241-
userInputMessage: {
242-
content: denyInput,
243-
userInputMessageContext,
244-
},
245-
}
246-
247-
this.lastUserMessage = msg
248-
}
249208
}

packages/core/src/codewhispererChat/tools/tool_index.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"fs_read": {
3-
"name": "fs_read",
4-
"description": "A tool for reading files (e.g. `cat -n`), or listing files/directories (e.g. `ls -la` or `find . -maxdepth 2). The behavior of this tool is determined by the `path` parameter pointing to a file or directory.\n* If `path` is a file, this tool returns the result of running `cat -n`, and the optional `read_range` determines what range of lines will be read from the specified file.\n* If `path` is a directory, this tool returns the listed files and directories of the specified path, as if running `ls -la`. If the `read_range` parameter is provided, the tool acts like the `find . -maxdepth <read_range>`, where `read_range` is the number of subdirectories deep to search, e.g. [2] will run `find . -maxdepth 2`.",
5-
"input_schema": {
2+
"fsRead": {
3+
"name": "fsRead",
4+
"description": "A tool for reading files (e.g. `cat -n`), or listing files/directories (e.g. `ls -la` or `find . -maxdepth 2). The behavior of this tool is determined by the `path` parameter pointing to a file or directory.\n* If `path` is a file, this tool returns the result of running `cat -n`, and the optional `readRange` determines what range of lines will be read from the specified file.\n* If `path` is a directory, this tool returns the listed files and directories of the specified path, as if running `ls -la`. If the `readRange` parameter is provided, the tool acts like the `find . -maxdepth <readRange>`, where `readRange` is the number of subdirectories deep to search, e.g. [2] will run `find . -maxdepth 2`.",
5+
"inputSchema": {
66
"type": "object",
77
"properties": {
88
"path": {
99
"description": "Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.",
1010
"type": "string"
1111
},
12-
"read_range": {
13-
"description": "Optional parameter when reading either files or directories.\n* When `path` is a file, if none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.\n* When `path` is a directory, if none is given, the results of `ls -l` are given. If provided, the current directory and indicated number of subdirectories will be shown, e.g. [2] will show the current directory and directories two levels deep.",
12+
"readRange": {
13+
"description": "Optional parameter when reading either files or directories.\n* When `path` is a file, if none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[startLine, -1]` shows all lines from `startLine` to the end of the file.\n* When `path` is a directory, if none is given, the results of `ls -l` are given. If provided, the current directory and indicated number of subdirectories will be shown, e.g. [2] will show the current directory and directories two levels deep.",
1414
"items": {
1515
"type": "integer"
1616
},

packages/core/src/shared/utilities/workspaceUtils.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -673,26 +673,8 @@ export async function findStringInDirectory(searchStr: string, dirPath: string)
673673
}
674674

675675
/**
676-
* Attempts to determine the current working directory based on the active editor or workspace.
677-
*
678-
* This function first checks if there is an active text editor open. If so, it returns
679-
* the directory containing the active file. If no editor is active, it falls back to
680-
* the first workspace folder's path.
681-
*
682-
* @returns {string | undefined} The path to the current working directory if it can be determined,
683-
* or undefined if no workspace folders are available and no editor is active.
684-
*/
685-
export function tryGetCurrentWorkingDirectory() {
686-
const activeTextEditor = vscode.window.activeTextEditor
687-
return activeTextEditor
688-
? path.dirname(activeTextEditor.document.uri.fsPath)
689-
: vscode.workspace.workspaceFolders?.[0].uri.fsPath
690-
}
691-
692-
/**
693-
* Returns a one-character tag for a directory ('d'), symlink ('l'), or file ('-').
694-
*/
695676
* Returns a one-character tag for a directory ('d'), symlink ('l'), or file ('-').
677+
*
696678
*/
697679
export function formatListing(name: string, fileType: vscode.FileType, fullPath: string): string {
698680
let typeChar = '-'

0 commit comments

Comments
 (0)