src/vs/platform/agentHost/node/agentHostHeadlessTerminal.ts

145 LOC · 144 covered · 1 uncovered · 36 ranges · 2246 concepts · 14 introducers · 1048 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostHeadlessTerminal.ts ×11
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 { Emitter, Event } from '../../../base/common/event.js';
7 > import { DeferredPromise } from '../../../base/common/async.js';
8 > import { Disposable, DisposableStore } from '../../../base/common/lifecycle.js';
9 > import type { ILogService } from '../../log/common/log.js';
10 > import pkg from '@xterm/headless';
11 >
12 > type XtermTerminal = pkg.Terminal;
13 > const { Terminal: XtermTerminal } = pkg;
14 >
15 > export interface IAgentHostHeadlessTerminalOptions {
16 > cols: number;
17 > rows: number;
18 > scrollback: number;
19 > logService: ILogService;
20 > terminalFactory?: (options: IXtermTerminalOptions) => XtermTerminal;
21 > }
22 >
23 > /**
24 > * Mirrors an agent-host PTY into xterm's interpreted terminal model.
25 > *
26 > * The mirror is intentionally internal to the agent host. Protocol-visible
27 > * terminal data still flows through the existing OSC 633 parser and content
28 > * model; this class provides terminal responses for programs that query
29 > * terminal state.
30 > */
31 > export class AgentHostHeadlessTerminal extends Disposable {
32 >
33 > private readonly _terminal: XtermTerminal;
34 > private readonly _logService: ILogService;
35 > private readonly _onResponseData = this._register(new Emitter<string>());
36 > readonly onResponseData: Event<string> = this._onResponseData.event;
37 > private _writeBarrier: Promise<void> = Promise.resolve();
38 > private _isDisposed = false;
39 >
40 > constructor(options: IAgentHostHeadlessTerminalOptions) {
42 > this._logService = options.logService;
43 > const terminalOptions: IXtermTerminalOptions = {
44 > cols: options.cols,
45 > rows: options.rows,
46 > scrollback: options.scrollback,
47 > allowProposedApi: true,
48 > };
49 > this._terminal = options.terminalFactory?.(terminalOptions) ?? new XtermTerminal(terminalOptions);
50 >
51 > this._register(this._terminal.onData(data => {
52 > if (this._isCursorPositionReportResponse(data)) { agentHostHeadlessTerminal.ts ×3
53 > this._logService.debug(`[AgentHostHeadlessTerminal] Forwarding terminal response ${JSON.stringify(data)}`); agentHostHeadlessTerminal.ts ×1
54 > this._onResponseData.fire(data);
56 > this._logService.debug(`[AgentHostHeadlessTerminal] Dropping terminal response ${JSON.stringify(data)}`); agentHostHeadlessTerminal.ts ×1
57 > }
59 > this._register({
60 > dispose: () => {
61 > this._isDisposed = true;
62 > this._terminal.dispose();
63 > }
64 > });
65 > }
67 > writePtyData(data: string): Promise<void> {
68 > this._writeBarrier = this._writeBarrier.catch(() => undefined).then(() => { agentHostHeadlessTerminal.ts ×5
69 > if (this._isDisposed) {
71 > }
72 > return new Promise<void>(resolve => { agentHostHeadlessTerminal.ts ×2
73 > try {
74 > this._terminal.write(data, resolve);
75 > } catch {
77 > }
80 > return this._writeBarrier;
81 > }
83 > whenPtyDataFlushed(): Promise<void> {
84 > return this._writeBarrier.catch(() => undefined); agentHostHeadlessTerminal.ts ×1
85 > }
87 > resize(cols: number, rows: number): void {
88 > this._terminal.resize(cols, rows); agentHostHeadlessTerminal.ts ×1
89 > }
91 > isBracketedPasteMode(): boolean {
92 > return this._terminal.modes.bracketedPasteMode; agentHostHeadlessTerminal.ts ×1
93 > }
95 > isInAltBuffer(): boolean {
96 > return this._terminal.buffer.active === this._terminal.buffer.alternate; agentHostHeadlessTerminal.ts ×5
97 > }
99 > createAltBufferPromise(store: DisposableStore): Promise<void> {
100 > const deferred = new DeferredPromise<void>(); agentHostHeadlessTerminal.ts ×5
101 > const complete = () => {
102 > if (!deferred.isSettled) { agentHostHeadlessTerminal.ts ×2
103 > this._logService.debug('[AgentHostHeadlessTerminal] Detected alternate buffer entry');
104 > deferred.complete();
105 > }
106 > };
108 > if (this.isInAltBuffer()) {
109 complete();
111 > store.add(this._terminal.buffer.onBufferChange(() => {
112 > if (this.isInAltBuffer()) { agentHostHeadlessTerminal.ts ×2
113 > complete();
114 > }
116 > }
117 >
118 > return deferred.p;
119 > }
121 > clear(): void {
122 > // xterm.clear() preserves the visible line content; emulate a terminal agentHostHeadlessTerminal.ts ×1
123 > // clear sequence so future terminal-state reads match a user-visible clear.
124 > void this.writePtyData('\x1b[2J\x1b[3J\x1b[H');
125 > }
127 > override dispose(): void {
128 > this._isDisposed = true; agentHostHeadlessTerminal.ts ×5
129 > super.dispose();
130 > }
132 > private _isCursorPositionReportResponse(data: string): boolean {
133 > // Only forward cursor position reports for now. xterm can also answer agentHostHeadlessTerminal.ts ×3
134 > // device attribute queries, but workbench only forwards those in narrow
135 > // ConPTY-specific cases; keep Agent Host conservative until needed.
136 > return /^(?:\x1b\[\??\d+;\d+R)+$/.test(data);
137 > }
139 >
140 > interface IXtermTerminalOptions {
141 > cols: number;
142 > rows: number;
143 > scrollback: number;
144 > allowProposedApi: boolean;
145 > }