1
>
/*---------------------------------------------------------------------------------------------
osc633Parser.ts
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
>
/**
7
>
* Lightweight parser for OSC 633 (VS Code shell integration) sequences in raw
8
>
* PTY output. Designed for the agent host where we don't have a full xterm.js
9
>
* instance - it scans data chunks for the sequences, extracts events, and
10
>
* removes the sequences from the data stream.
11
>
*
12
>
* Handles partial sequences that span across data chunk boundaries.
13
>
*/
14
>
15
>
/** OSC 633 event types we care about. */
16
>
export const enum Osc633EventType {
17
>
/** 633;A - Prompt start. Used to detect shell integration is active. */
18
>
PromptStart,
19
>
/** 633;B - Command start (where user inputs command). */
20
>
CommandStart,
21
>
/** 633;C - Command executed (output begins). */
22
>
CommandExecuted,
23
>
/** 633;D[;exitCode] - Command finished. */
24
>
CommandFinished,
25
>
/** 633;E;commandLine[;nonce] - Explicit command line. */
26
>
CommandLine,
27
>
/** 633;P;Key=Value - Property (e.g. Cwd). */
28
>
Property,
29
>
}
30
>
31
>
export interface IOsc633PromptStartEvent {
32
>
type: Osc633EventType.PromptStart;
33
>
}
34
>
35
>
export interface IOsc633CommandStartEvent {
36
>
type: Osc633EventType.CommandStart;
37
>
}
38
>
39
>
export interface IOsc633CommandExecutedEvent {
40
>
type: Osc633EventType.CommandExecuted;
41
>
}
42
>
43
>
export interface IOsc633CommandFinishedEvent {
44
>
type: Osc633EventType.CommandFinished;
45
>
exitCode: number | undefined;
46
>
}
47
>
48
>
export interface IOsc633CommandLineEvent {
49
>
type: Osc633EventType.CommandLine;
50
>
commandLine: string;
51
>
nonce: string | undefined;
52
>
}
53
>
54
>
export interface IOsc633PropertyEvent {
55
>
type: Osc633EventType.Property;
56
>
key: string;
57
>
value: string;
58
>
}
59
>
60
>
export type Osc633Event =
61
>
| IOsc633PromptStartEvent
62
>
| IOsc633CommandStartEvent
63
>
| IOsc633CommandExecutedEvent
64
>
| IOsc633CommandFinishedEvent
65
>
| IOsc633CommandLineEvent
66
>
| IOsc633PropertyEvent;
67
>
68
>
export interface IOsc633ParseResult {
69
>
/** Data with all OSC 633 sequences stripped. */
70
>
cleanedData: string;
71
>
/** Parsed events in order of appearance. */
72
>
events: Osc633Event[];
73
>
}
74
>
75
>
/**
76
>
* A single segment of parsed PTY data: either a run of cleaned output data or
77
>
* an OSC 633 event. Segments are emitted in stream order so that output which
78
>
* arrives before an event (e.g. a `CommandFinished` marker) can be attributed
79
>
* to the command before the event is handled — see {@link Osc633Parser.parseSegments}.
80
>
*/
81
>
export type Osc633ParseSegment =
82
>
| { readonly kind: 'data'; readonly data: string }
83
>
| { readonly kind: 'event'; readonly event: Osc633Event };
84
>
85
>
/**
86
>
* Decode escaped values in OSC 633 messages.
87
>
* Handles `\\` -> `\` and `\xAB` -> character with code 0xAB.
88
>
*/
89
function deserializeOscMessage(message: string): string {
90
if (message.indexOf('\\') === -1) {