-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathbuild-status-client.ts
More file actions
194 lines (169 loc) · 5.38 KB
/
build-status-client.ts
File metadata and controls
194 lines (169 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import WebSocket from 'ws';
/**
* URLs for an operation's log files, served by the rush-serve-plugin.
*/
export interface ILogFileURLs {
text: string;
error: string;
jsonl: string;
}
/**
* Minimal subset of operation info from the rush-serve-plugin WebSocket protocol.
*/
export interface IOperationSummary {
name: string;
packageName: string;
phaseName: string;
status: string;
startTime: number | undefined;
endTime: number | undefined;
logFileURLs: ILogFileURLs | undefined;
}
/**
* Session information from the rush-serve-plugin WebSocket protocol.
*/
export interface IRushSessionInfo {
actionName: string;
repositoryIdentifier: string;
}
/**
* A snapshot of the current build status, returned by the WebSocket utility functions.
*/
export interface IBuildStatusSnapshot {
status: string;
operations: IOperationSummary[];
sessionInfo?: IRushSessionInfo;
}
/**
* Options for connecting to the rush-serve-plugin WebSocket server.
*/
export interface IBuildStatusClientOptions {
port: number;
host?: string;
}
/**
* WebSocket event message types matching the rush-serve-plugin wire format.
* Duplicated here to avoid a runtime dependency on rush-serve-plugin.
*/
interface IWebSocketSyncEventMessage {
event: 'sync';
operations: IOperationSummary[];
sessionInfo: IRushSessionInfo;
status: string;
}
type IWebSocketEventMessage =
| IWebSocketSyncEventMessage
| { event: 'before-execute' | 'status-change' | 'after-execute'; operations: IOperationSummary[] };
function buildWebSocketUrl(options: IBuildStatusClientOptions): string {
const host: string = options.host ?? '127.0.0.1';
return `wss://${host}:${options.port}/ws`;
}
function toSnapshot(message: IWebSocketSyncEventMessage): IBuildStatusSnapshot {
return {
status: message.status,
operations: message.operations,
sessionInfo: message.sessionInfo
};
}
/**
* Formats a build status snapshot into a human-readable string for LLM consumption.
*/
export function formatBuildStatusSnapshot(snapshot: IBuildStatusSnapshot): string {
const lines: string[] = [];
lines.push(`Build Status: ${snapshot.status}`);
if (snapshot.sessionInfo) {
lines.push(`Command: ${snapshot.sessionInfo.actionName}`);
lines.push(`Repository: ${snapshot.sessionInfo.repositoryIdentifier}`);
}
// Summarize operation statuses
const statusCounts: Map<string, number> = new Map();
for (const op of snapshot.operations) {
statusCounts.set(op.status, (statusCounts.get(op.status) ?? 0) + 1);
}
lines.push('');
const total: number = snapshot.operations.length;
const summaryParts: string[] = [];
for (const [status, count] of statusCounts) {
summaryParts.push(`${status}: ${count}`);
}
lines.push(`Operation Summary: ${total} total`);
if (summaryParts.length > 0) {
lines.push(` ${summaryParts.join(', ')}`);
}
// List failed operations
const failedOps: IOperationSummary[] = snapshot.operations.filter((op) => op.status === 'Failure');
if (failedOps.length > 0) {
lines.push('');
lines.push('Failed Operations:');
for (const op of failedOps) {
lines.push(` - ${op.packageName} (${op.phaseName})`);
}
}
// List blocked operations
const blockedOps: IOperationSummary[] = snapshot.operations.filter((op) => op.status === 'Blocked');
if (blockedOps.length > 0) {
lines.push('');
lines.push('Blocked Operations:');
for (const op of blockedOps) {
lines.push(` - ${op.packageName} (${op.phaseName})`);
}
}
return lines.join('\n');
}
/**
* Connects to the rush-serve-plugin WebSocket, receives the initial sync message,
* and returns a snapshot of the current build status.
*/
export async function fetchBuildStatusAsync(
options: IBuildStatusClientOptions
): Promise<IBuildStatusSnapshot> {
const url: string = buildWebSocketUrl(options);
return new Promise<IBuildStatusSnapshot>((resolve, reject) => {
const ws: WebSocket = new WebSocket(url, { rejectUnauthorized: false });
let settled: boolean = false;
const connectionTimeout: NodeJS.Timeout = setTimeout(() => {
if (!settled) {
settled = true;
ws.close();
reject(new Error(`Connection to rush start timed out after 10000ms.`));
}
}, 10000);
function settle(action: () => void): void {
if (!settled) {
settled = true;
clearTimeout(connectionTimeout);
action();
}
}
ws.on('error', (err: Error) => {
settle(() =>
reject(
new Error(
`Cannot connect to rush start on port ${options.port}. Ensure \`rush start\` is running. (${err.message})`
)
)
);
});
ws.on('close', () => {
settle(() =>
reject(
new Error(`Connection to rush start on port ${options.port} closed before receiving build status.`)
)
);
});
ws.on('message', (data: WebSocket.Data) => {
try {
const message: IWebSocketEventMessage = JSON.parse(data.toString());
if (message.event === 'sync') {
settle(() => resolve(toSnapshot(message)));
ws.close();
}
} catch (parseError: unknown) {
ws.close();
settle(() => reject(new Error(`Failed to parse WebSocket message: ${parseError}`)));
}
});
});
}