Skip to content

Commit b136dbe

Browse files
committed
Refactoring code to store the logs from the terminal.
1 parent 36ba8b8 commit b136dbe

File tree

1 file changed

+18
-60
lines changed
  • packages/core/src/codewhispererChat/controllers/chat

1 file changed

+18
-60
lines changed

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

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
*/
55
import * as path from 'path'
66
import * as vscode from 'vscode'
7-
import * as os from 'os'
87
import { fs } from '../../../shared/fs/fs'
9-
import { ChildProcess } from '../../../shared/utilities/processUtils'
108
import { Event as VSCodeEvent, Uri, workspace, window, ViewColumn, Position, Selection } from 'vscode'
119
import { EditorContextExtractor } from '../../editor/context/extractor'
1210
import { ChatSessionStorage } from '../../storages/chatSession'
@@ -82,6 +80,7 @@ import {
8280
contextMaxLength,
8381
} from '../../constants'
8482
import { ChatSession } from '../../clients/chat/v0/chat'
83+
import { tempDirPath } from '../../../shared/filesystemUtilities'
8584

8685
export interface ChatControllerMessagePublishers {
8786
readonly processPromptChatMessage: MessagePublisher<PromptMessage>
@@ -580,80 +579,39 @@ export class ChatController {
580579
}
581580

582581
terminal.show()
582+
// Create a temporary file at /tmp/aws-toolkit-vscode/agenticChatTerminalCommandLogs.log
583+
const outLogFilePath = path.join(tempDirPath, 'agenticChatTerminalCommandLogs.log')
583584

584-
const command = session.storedBashCommands[0]
585+
try {
586+
const command = `${session.storedBashCommands[0]} > ${outLogFilePath}`
585587

586-
// Get the current path of the terminal
587-
const currentPath = await this.getCurrentTerminalPath(terminal)
588+
// Execute the command in terminal
589+
terminal.sendText(command)
588590

589-
let terminalOutput = ''
591+
// Store the outLogFilePath text in lastTerminalOutput
592+
this.lastTerminalOutput = outLogFilePath
590593

591-
try {
592-
// Execute the command in the terminal's current directory
593-
const childProcess = new ChildProcess('bash', ['-c', command], {
594-
spawnOptions: { cwd: currentPath },
595-
collect: true,
596-
})
594+
getLogger().info(`Command executed: ${command}`)
595+
getLogger().info(`Output saved to: ${outLogFilePath}`)
597596

598-
const result = await childProcess.run()
599-
600-
if (result.exitCode !== 0 || result.error) {
601-
const errorMessage = result.error ? result.error.message : result.stderr
602-
getLogger().error(`Error executing command: ${errorMessage}`)
603-
terminal.sendText(`echo "Error executing command: ${errorMessage}"`)
604-
this.lastTerminalOutput = `Error: ${errorMessage}`
605-
} else {
606-
terminalOutput = result.stdout.trim()
607-
terminal.sendText(command)
608-
getLogger().info(`Command executed: ${command}`)
609-
getLogger().info(`Command output: ${terminalOutput}`)
610-
this.lastTerminalOutput = terminalOutput
611-
}
597+
// TODO: Move this deleting temp file logic near send the logs to RTS.
598+
// Delete the temporary agenticChatTerminalCommandLogs file
599+
fs.delete(outLogFilePath).catch((err: Error) => {
600+
getLogger().error(`Failed to delete output log file: ${err}`)
601+
})
612602
} catch (error) {
613603
const errorMessage = error instanceof Error ? error.message : String(error)
614604
getLogger().error(`Error executing command: ${errorMessage}`)
615605
terminal.sendText(`echo "Error executing command: ${errorMessage}"`)
616606
this.lastTerminalOutput = `Error: ${errorMessage}`
617607
}
618608
}
619-
getLogger().error(`Last terminal output: ${this.getLastTerminalOutput()}`)
609+
610+
getLogger().info(`Last terminal output: ${this.getLastTerminalOutput()}`)
620611
this.messenger.sendMessage(this.getLastTerminalOutput(), message.tabID ?? '', message.tabID ?? 'unknown')
621612
session.storedBashCommands = []
622613
}
623614

624-
private async getCurrentTerminalPath(terminal: vscode.Terminal): Promise<string> {
625-
try {
626-
// Get the current workspace folders
627-
const workspaceFolders = vscode.workspace.workspaceFolders
628-
629-
// Try to get the terminal's creation options
630-
// We need to use type assertion since the API types might not expose cwd directly
631-
const options = terminal.creationOptions as any
632-
if (options) {
633-
// Check if cwd exists in the options
634-
if (options.cwd) {
635-
if (typeof options.cwd === 'string') {
636-
return options.cwd
637-
} else if (options.cwd instanceof vscode.Uri) {
638-
return options.cwd.fsPath
639-
}
640-
}
641-
}
642-
643-
// If there's an active workspace folder, use its path
644-
if (workspaceFolders && workspaceFolders.length > 0) {
645-
const activeWorkspace = workspaceFolders[0]
646-
return activeWorkspace.uri.fsPath
647-
}
648-
649-
// Fallback to user's home directory
650-
return os.homedir()
651-
} catch (err) {
652-
getLogger().error(`Failed to get terminal path: ${err}`)
653-
return os.homedir()
654-
}
655-
}
656-
657615
// Get the last terminal output
658616
public getLastTerminalOutput(): string {
659617
return this.lastTerminalOutput

0 commit comments

Comments
 (0)