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