|
4 | 4 | */ |
5 | 5 | import * as path from 'path' |
6 | 6 | import * as vscode from 'vscode' |
7 | | -import * as os from 'os' |
8 | 7 | import { fs } from '../../../shared/fs/fs' |
9 | | -import { ChildProcess } from '../../../shared/utilities/processUtils' |
10 | 8 | import { Event as VSCodeEvent, Uri, workspace, window, ViewColumn, Position, Selection } from 'vscode' |
11 | 9 | import { EditorContextExtractor } from '../../editor/context/extractor' |
12 | 10 | import { ChatSessionStorage } from '../../storages/chatSession' |
@@ -82,6 +80,7 @@ import { |
82 | 80 | contextMaxLength, |
83 | 81 | } from '../../constants' |
84 | 82 | import { ChatSession } from '../../clients/chat/v0/chat' |
| 83 | +import { tempDirPath } from '../../../shared/filesystemUtilities' |
85 | 84 |
|
86 | 85 | export interface ChatControllerMessagePublishers { |
87 | 86 | readonly processPromptChatMessage: MessagePublisher<PromptMessage> |
@@ -580,80 +579,39 @@ export class ChatController { |
580 | 579 | } |
581 | 580 |
|
582 | 581 | terminal.show() |
| 582 | + // Create a temporary file at /tmp/aws-toolkit-vscode/agenticChatTerminalCommandLogs.log |
| 583 | + const outLogFilePath = path.join(tempDirPath, 'agenticChatTerminalCommandLogs.log') |
583 | 584 |
|
584 | | - const command = session.storedBashCommands[0] |
| 585 | + try { |
| 586 | + const command = `${session.storedBashCommands[0]} > ${outLogFilePath}` |
585 | 587 |
|
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) |
588 | 590 |
|
589 | | - let terminalOutput = '' |
| 591 | + // Store the outLogFilePath text in lastTerminalOutput |
| 592 | + this.lastTerminalOutput = outLogFilePath |
590 | 593 |
|
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}`) |
597 | 596 |
|
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 | + }) |
612 | 602 | } catch (error) { |
613 | 603 | const errorMessage = error instanceof Error ? error.message : String(error) |
614 | 604 | getLogger().error(`Error executing command: ${errorMessage}`) |
615 | 605 | terminal.sendText(`echo "Error executing command: ${errorMessage}"`) |
616 | 606 | this.lastTerminalOutput = `Error: ${errorMessage}` |
617 | 607 | } |
618 | 608 | } |
619 | | - getLogger().error(`Last terminal output: ${this.getLastTerminalOutput()}`) |
| 609 | + |
| 610 | + getLogger().info(`Last terminal output: ${this.getLastTerminalOutput()}`) |
620 | 611 | this.messenger.sendMessage(this.getLastTerminalOutput(), message.tabID ?? '', message.tabID ?? 'unknown') |
621 | 612 | session.storedBashCommands = [] |
622 | 613 | } |
623 | 614 |
|
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 | | - |
657 | 615 | // Get the last terminal output |
658 | 616 | public getLastTerminalOutput(): string { |
659 | 617 | return this.lastTerminalOutput |
|
0 commit comments