-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Expand file tree
/
Copy pathsync-agent-host-protocol.ts
More file actions
282 lines (243 loc) · 9.99 KB
/
Copy pathsync-agent-host-protocol.ts
File metadata and controls
282 lines (243 loc) · 9.99 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// Copies type definitions from the sibling `agent-host-protocol` repo into
// `src/vs/platform/agentHost/common/state/protocol/`. Run via:
//
// npx tsx scripts/sync-agent-host-protocol.ts
//
// Source layout is preserved verbatim: every `.ts` file under
// `agent-host-protocol/types/` (notably the `common/` and `channels-*` folders)
// is copied into the matching subfolder of `protocol/`. Test fixtures
// (`test-cases/`), `*.test.ts` files, and `index.ts` are skipped.
//
// Transformations applied:
// 1. Converts 2-space indentation to tabs.
// 2. Merges duplicate imports from the same module.
// 3. Formats with the project's tsfmt.json settings.
// 4. Adds Microsoft copyright header.
//
// URI stays as `string` (the protocol's canonical representation). VS Code code
// should call `URI.parse()` at point-of-use where a URI class is needed.
import * as fs from 'fs';
import * as path from 'path';
import { execSync } from 'child_process';
import * as ts from 'typescript';
const ROOT = path.resolve(__dirname, '..');
const PROTOCOL_REPO = path.resolve(ROOT, '../agent-host-protocol');
const TYPES_DIR = path.join(PROTOCOL_REPO, 'types');
const DEST_DIR = path.join(ROOT, 'src/vs/platform/agentHost/common/state/protocol');
// Load tsfmt.json formatting options once
const TSFMT_PATH = path.join(ROOT, 'tsfmt.json');
const FORMAT_OPTIONS: ts.FormatCodeSettings = JSON.parse(fs.readFileSync(TSFMT_PATH, 'utf-8'));
/**
* Formats a TypeScript source string using the TypeScript language service
* formatter with the project's tsfmt.json settings.
*/
function formatTypeScript(content: string, fileName: string): string {
const host: ts.LanguageServiceHost = {
getCompilationSettings: () => ({}),
getScriptFileNames: () => [fileName],
getScriptVersion: () => '1',
getScriptSnapshot: (name: string) => name === fileName ? ts.ScriptSnapshot.fromString(content) : undefined,
getCurrentDirectory: () => ROOT,
getDefaultLibFileName: () => '',
fileExists: () => false,
readFile: () => undefined,
};
const ls = ts.createLanguageService(host);
const edits = ls.getFormattingEditsForDocument(fileName, FORMAT_OPTIONS);
// Apply edits in reverse order to preserve offsets
for (let i = edits.length - 1; i >= 0; i--) {
const edit = edits[i];
content = content.substring(0, edit.span.start) + edit.newText + content.substring(edit.span.start + edit.span.length);
}
ls.dispose();
return content;
}
const COPYRIGHT = `/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/`;
const BANNER = '// allow-any-unicode-comment-file\n// DO NOT EDIT -- auto-generated by scripts/sync-agent-host-protocol.ts';
/**
* Files and directories to exclude when discovering protocol sources. Anything
* else under `types/` is copied verbatim into `protocol/`, preserving the
* subdirectory layout (e.g. `types/channels-session/state.ts` →
* `protocol/channels-session/state.ts`).
*/
const EXCLUDE_DIR_NAMES = new Set([
'test-cases', // reducer test fixtures
'node_modules',
]);
const EXCLUDE_FILE_NAMES = new Set([
'tsconfig.json',
'message-checks.ts',
'index.ts', // protocol's public entry point — VS Code has its own re-export layout
]);
/**
* Walks `TYPES_DIR` recursively and yields `{ src, dest }` pairs (relative
* to `TYPES_DIR` / `DEST_DIR` respectively) for every `.ts` file that should
* be synced. Test files (`*.test.ts`) and the excluded names above are
* skipped. Yields a stable order so the output is reproducible.
*/
function discoverSourceFiles(): { src: string; dest: string }[] {
const results: { src: string; dest: string }[] = [];
function walk(dir: string, relBase: string): void {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.isDirectory()) {
if (EXCLUDE_DIR_NAMES.has(entry.name)) {
continue;
}
const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
walk(path.join(dir, entry.name), rel);
} else if (entry.isFile()) {
if (EXCLUDE_FILE_NAMES.has(entry.name)) {
continue;
}
if (!entry.name.endsWith('.ts')) {
continue;
}
if (entry.name.endsWith('.test.ts')) {
continue;
}
const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
results.push({ src: rel, dest: rel });
}
}
}
walk(TYPES_DIR, '');
results.sort((a, b) => a.dest.localeCompare(b.dest));
return results;
}
function getSourceCommitHash(): string {
try {
return execSync('git rev-parse --short HEAD', { cwd: PROTOCOL_REPO, encoding: 'utf-8' }).trim();
} catch {
return 'unknown';
}
}
function stripExistingHeader(content: string): string {
return content.replace(/^\/\*\*?[\s\S]*?\*\/\s*/, '');
}
function convertIndentation(content: string): string {
const lines = content.split('\n');
return lines.map(line => {
const match = line.match(/^( +)/);
if (!match) {
return line;
}
const spaces = match[1].length;
const tabs = Math.floor(spaces / 2);
const remainder = spaces % 2;
return '\t'.repeat(tabs) + ' '.repeat(remainder) + line.slice(spaces);
}).join('\n');
}
/**
* Merges duplicate imports from the same module.
* Combines `import type { A }` and `import { B }` from the same module into
* `import { B, type A }` to satisfy the no-duplicate-imports lint rule.
*/
function mergeDuplicateImports(content: string): string {
// Normalize line endings so the `$`-anchored import regexes below match
// regardless of whether the source was checked out with CRLF or LF.
content = content.replace(/\r\n/g, '\n');
// Collapse multi-line imports into single lines first
content = content.replace(/import\s+(type\s+)?\{([^}]+)\}\s+from\s+'([^']+)';/g, (_match, typeKeyword, names, mod) => {
const collapsed = names.replace(/\s+/g, ' ').trim();
return typeKeyword ? `import type { ${collapsed} } from '${mod}';` : `import { ${collapsed} } from '${mod}';`;
});
const importsByModule = new Map<string, { typeNames: string[]; valueNames: string[] }>();
const otherLines: string[] = [];
const seenModules = new Set<string>();
for (const line of content.split('\n')) {
const typeMatch = line.match(/^import type \{([^}]+)\} from '([^']+)';$/);
const valueMatch = line.match(/^import \{([^}]+)\} from '([^']+)';$/);
if (typeMatch) {
const [, names, mod] = typeMatch;
if (!importsByModule.has(mod)) {
importsByModule.set(mod, { typeNames: [], valueNames: [] });
}
importsByModule.get(mod)!.typeNames.push(...names.split(',').map(s => s.trim()).filter(s => s.length > 0));
if (!seenModules.has(mod)) {
seenModules.add(mod);
otherLines.push(`__IMPORT_PLACEHOLDER__${mod}`);
}
} else if (valueMatch) {
const [, names, mod] = valueMatch;
if (!importsByModule.has(mod)) {
importsByModule.set(mod, { typeNames: [], valueNames: [] });
}
importsByModule.get(mod)!.valueNames.push(...names.split(',').map(s => s.trim()).filter(s => s.length > 0));
if (!seenModules.has(mod)) {
seenModules.add(mod);
otherLines.push(`__IMPORT_PLACEHOLDER__${mod}`);
}
} else {
otherLines.push(line);
}
}
return otherLines.map(line => {
if (line.startsWith('__IMPORT_PLACEHOLDER__')) {
const mod = line.substring('__IMPORT_PLACEHOLDER__'.length);
const entry = importsByModule.get(mod)!;
const uniqueTypes = [...new Set(entry.typeNames)];
const uniqueValues = [...new Set(entry.valueNames)];
if (uniqueValues.length > 0 && uniqueTypes.length > 0) {
const allNames = [...uniqueValues, ...uniqueTypes.map(n => `type ${n}`)];
return `import { ${allNames.join(', ')} } from '${mod}';`;
} else if (uniqueValues.length > 0) {
return `import { ${uniqueValues.join(', ')} } from '${mod}';`;
} else {
return `import type { ${uniqueTypes.join(', ')} } from '${mod}';`;
}
}
return line;
}).join('\n');
}
function processFile(src: string, dest: string): void {
let content = fs.readFileSync(src, 'utf-8');
content = stripExistingHeader(content);
// Merge duplicate imports from the same module
content = mergeDuplicateImports(content);
content = convertIndentation(content);
content = content.split('\n').map(line => line.trimEnd()).join('\n');
const header = `${COPYRIGHT}\n\n${BANNER}\n`;
content = header + '\n' + content;
if (!content.endsWith('\n')) {
content += '\n';
}
const destPath = path.join(DEST_DIR, dest);
fs.mkdirSync(path.dirname(destPath), { recursive: true });
content = formatTypeScript(content, dest);
fs.writeFileSync(destPath, content, 'utf-8');
console.log(` ${dest}`);
}
// ---- Main -------------------------------------------------------------------
function main() {
if (!fs.existsSync(TYPES_DIR)) {
console.error(`ERROR: Cannot find ${TYPES_DIR}`);
console.error('Clone agent-host-protocol as a sibling of the VS Code repo:');
console.error(' git clone git@github.com:microsoft/agent-host-protocol.git ../agent-host-protocol');
process.exit(1);
}
const commitHash = getSourceCommitHash();
console.log(`Syncing from agent-host-protocol @ ${commitHash}`);
console.log(` Source: ${TYPES_DIR}`);
console.log(` Dest: ${DEST_DIR}`);
console.log();
// Discover and copy protocol files
const files = discoverSourceFiles();
for (const file of files) {
const srcPath = path.join(TYPES_DIR, file.src);
processFile(srcPath, file.dest);
}
// Write the source commit hash to a single version file
const versionFile = path.join(DEST_DIR, '.ahp-version');
fs.writeFileSync(versionFile, commitHash + '\n', 'utf-8');
console.log(` .ahp-version -> ${commitHash}`);
console.log();
console.log('Done.');
}
main();