Path: blob/main/extensions/copilot/src/extension/prompts/node/base/terminalState.tsx
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { BasePromptElementProps, PromptElement } from '@vscode/prompt-tsx';6import { ITasksService } from '../../../../platform/tasks/common/tasksService';7import { ITerminalService } from '../../../../platform/terminal/common/terminalService';89export interface TerminalStateProps extends BasePromptElementProps {10sessionId?: string;11}1213/**14* PromptElement that gets the current task and terminal state for the chat context.15*/16export class TerminalStatePromptElement extends PromptElement<TerminalStateProps> {17constructor(18props: TerminalStateProps,19@ITasksService private readonly tasksService: ITasksService,20@ITerminalService private readonly terminalService: ITerminalService21) {22super(props);23}24async render() {25const allTasks = this.tasksService.getTasks()?.[0]?.[1] ?? [];26const tasks = Array.isArray(allTasks) ? allTasks : [];27const activeTaskNames = tasks.filter(t => this.tasksService.isTaskActive(t)).map(t => t.label);2829if (this.terminalService && Array.isArray(this.terminalService.terminals)) {30const terminals = await Promise.all(this.terminalService.terminals.map(async (term) => {31const lastCommand = await this.terminalService.getLastCommandForTerminal(term);32return {33name: term.name,34lastCommand: lastCommand ? {35commandLine: lastCommand.commandLine ?? '(no last command)',36cwd: lastCommand.cwd?.toString() ?? '(unknown)',37exitCode: lastCommand.exitCode,38} : undefined39} as ITerminalPromptInfo;40}));41const resultTerminals = terminals.filter(t => !!t && !activeTaskNames.includes(t.name));4243if (resultTerminals.length === 0) {44return;45}4647const renderTerminals = () => (48<>49{resultTerminals.length > 0 && (50<>51Terminals:<br />52{resultTerminals.map((term: ITerminalPromptInfo) => (53<>54Terminal: {term.name}<br />55{term.lastCommand ? (56<>57Last Command: {term.lastCommand.commandLine ?? '(no last command)'}<br />58Cwd: {term.lastCommand.cwd ?? '(unknown)'}<br />59Exit Code: {term.lastCommand.exitCode ?? '(unknown)'}<br />60</>61) : ''}62</>63))}64</>65)}66</>67);68return (69<>70{resultTerminals.length > 0 ? renderTerminals() : 'Terminals: No terminals found.\n'}71</>72);73}74}75}76interface ITerminalPromptInfo {77name: string;78pid: number | undefined;79lastCommand: { commandLine: string; cwd: string; exitCode: number | undefined } | undefined;80}8182