-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate_protocol_types.py
More file actions
756 lines (630 loc) · 25.1 KB
/
Copy pathgenerate_protocol_types.py
File metadata and controls
756 lines (630 loc) · 25.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#!/usr/bin/env python3
"""Generate complete, wire-preserving C++ types from official Copilot schemas."""
from __future__ import annotations
import argparse
import hashlib
import json
import re
import subprocess
import sys
from collections import Counter
from pathlib import Path
from typing import Any, Iterable
REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_BASELINE = REPO_ROOT / "schemas" / "official-baseline.json"
DEFAULT_OUTPUT = REPO_ROOT / "include" / "copilot" / "generated"
SCHEMA_GROUPS = ("api", "sessionEvents")
def canonical_json(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, separators=(",", ":"), sort_keys=True)
def sha256_bytes(value: bytes) -> str:
return hashlib.sha256(value).hexdigest()
def sha256_json(value: Any) -> str:
return sha256_bytes(canonical_json(value).encode("utf-8"))
def sha256_names(values: Iterable[str]) -> str:
return sha256_bytes(("\n".join(sorted(values)) + "\n").encode("utf-8"))
def load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def cpp_string(value: str) -> str:
return json.dumps(value, ensure_ascii=False)
def official_cli_version(root: Path) -> str:
"""Copilot CLI version the official SDK checkout is pinned to.
Upstream moved this. It used to be an ordinary npm dependency recorded in
nodejs/package-lock.json; since the CLI began shipping via GitHub Releases
(github/copilot-sdk ec1c6f4b, 570ae11b) that lock file has no @github/copilot
entry at all and the version is declared in nodejs/src/cliVersion.ts.
Read the current location first and keep the lock file as a fallback so older
references still validate. Reading the lock file unconditionally raised a bare
KeyError against any reference newer than 2026-09, which said nothing about
what was actually wrong.
"""
version_ts = root / "nodejs" / "src" / "cliVersion.ts"
if version_ts.exists():
match = re.search(
r'COPILOT_CLI_VERSION\s*=\s*"([^"]+)"',
version_ts.read_text(encoding="utf-8"),
)
if match:
return match.group(1)
lock_path = root / "nodejs" / "package-lock.json"
if lock_path.exists():
entry = load_json(lock_path).get("packages", {}).get("node_modules/@github/copilot")
if entry and "version" in entry:
return entry["version"]
raise ValueError(
f"could not determine the Copilot CLI version from {root}: neither "
f"nodejs/src/cliVersion.ts (COPILOT_CLI_VERSION) nor nodejs/package-lock.json "
f"(packages -> node_modules/@github/copilot) provided one"
)
def validate_official_sdk(root: Path, baseline: dict[str, Any]) -> None:
expected = baseline["officialSdk"]
head = subprocess.run(
["git", "-C", str(root), "rev-parse", "HEAD"],
check=True,
capture_output=True,
text=True,
).stdout.strip()
if head != expected["commit"]:
raise ValueError(f"official SDK commit is {head}, expected {expected['commit']}")
version_file = load_json(root / expected["protocolVersionSource"])
if version_file["version"] != baseline["protocol"]["version"]:
raise ValueError(
f"official protocol version is {version_file['version']}, "
f"expected {baseline['protocol']['version']}"
)
cli_version = official_cli_version(root)
if cli_version != expected["schemaPackageVersion"]:
raise ValueError(
f"official schema package is {cli_version}, "
f"expected {expected['schemaPackageVersion']}"
)
def validate_schema_package(schema_path: Path, expected_version: str) -> None:
for directory in schema_path.resolve().parents:
package_json = directory / "package.json"
if not package_json.exists():
continue
package = load_json(package_json)
name = package.get("name", "")
if name == "@github/copilot" or name.startswith("@github/copilot-"):
if package.get("version") != expected_version:
raise ValueError(
f"{schema_path} comes from {name}@{package.get('version')}, "
f"expected {expected_version}"
)
return
raise ValueError(f"could not find the @github/copilot package for {schema_path}")
def schema_kind(schema: dict[str, Any]) -> str:
if schema.get("x-opaque-json"):
return "opaque"
if "anyOf" in schema or "oneOf" in schema:
return "union"
if "$ref" in schema:
return "alias"
kind = schema.get("type")
if kind == "string" and ("enum" in schema or "const" in schema):
return "enum"
if kind == "object":
return "map" if not schema.get("properties") and schema.get("additionalProperties") else "object"
if kind in {"array", "string", "integer", "number", "boolean", "null"}:
return kind
return "opaque"
def known_string_values(schema: dict[str, Any]) -> list[str]:
values: list[str] = []
if isinstance(schema.get("const"), str):
values.append(schema["const"])
values.extend(value for value in schema.get("enum", []) if isinstance(value, str))
for branch in schema.get("anyOf", []) + schema.get("oneOf", []):
if isinstance(branch, dict):
values.extend(known_string_values(branch))
return list(dict.fromkeys(values))
def definition_entry(name: str, schema: dict[str, Any]) -> dict[str, Any]:
properties = schema.get("properties", {})
required = set(schema.get("required", []))
variants = schema.get("anyOf", schema.get("oneOf", []))
additional = schema.get("additionalProperties")
return {
"name": name,
"kind": schema_kind(schema),
"schemaSha256": sha256_json(schema),
"propertyCount": len(properties),
"requiredPropertyCount": len(required),
"hasOptionalProperties": bool(set(properties) - required),
"hasMap": additional not in (None, False),
"unionVariantCount": len(variants),
"knownStringValues": known_string_values(schema),
}
def collect_rpc_methods(document: dict[str, Any]) -> list[str]:
methods: list[str] = []
def visit(value: Any) -> None:
if isinstance(value, dict):
method = value.get("rpcMethod")
if isinstance(method, str):
methods.append(method)
for child in value.values():
visit(child)
elif isinstance(value, list):
for child in value:
visit(child)
for root_name in ("server", "session", "clientSession", "clientGlobal"):
visit(document[root_name])
if len(methods) != len(set(methods)):
raise ValueError("api.schema.json contains duplicate rpcMethod values")
return methods
def collect_event_variants(document: dict[str, Any]) -> list[dict[str, str]]:
definitions = document["definitions"]
union = definitions["SessionEvent"].get("anyOf", [])
variants: list[dict[str, str]] = []
for branch in union:
reference = branch.get("$ref", "")
name = reference.rsplit("/", 1)[-1]
event_schema = definitions.get(name)
if not event_schema:
raise ValueError(f"SessionEvent references missing definition {name}")
wire_type = event_schema.get("properties", {}).get("type", {}).get("const")
if not isinstance(wire_type, str):
raise ValueError(f"{name} does not have a constant event type discriminator")
variants.append({"definition": name, "wireType": wire_type})
wire_types = [variant["wireType"] for variant in variants]
if len(wire_types) != len(set(wire_types)):
raise ValueError("session event union contains duplicate wire type values")
return variants
def build_surface(
api_path: Path, session_events_path: Path, baseline: dict[str, Any]
) -> dict[str, Any]:
expected_version = baseline["officialSdk"]["schemaPackageVersion"]
validate_schema_package(api_path, expected_version)
validate_schema_package(session_events_path, expected_version)
api = load_json(api_path)
events = load_json(session_events_path)
api_definitions = api["definitions"]
event_definitions = events["definitions"]
event_variants = collect_event_variants(events)
surface = {
"api": [
definition_entry(name, api_definitions[name]) for name in sorted(api_definitions)
],
"sessionEvents": [
definition_entry(name, event_definitions[name])
for name in sorted(event_definitions)
],
"rpcMethods": collect_rpc_methods(api),
"sessionEventVariants": event_variants,
}
session_event_entry = next(
entry for entry in surface["sessionEvents"] if entry["name"] == "SessionEvent"
)
session_event_entry["knownStringValues"] = [
variant["wireType"] for variant in event_variants
]
api_names = set(api_definitions)
event_names = set(event_definitions)
api_kinds = Counter(entry["kind"] for entry in surface["api"])
event_kinds = Counter(entry["kind"] for entry in surface["sessionEvents"])
coverage = {
"apiDefinitionCount": len(api_names),
"sessionEventDefinitionCount": len(event_names),
"sharedDefinitionCount": len(api_names & event_names),
"uniqueDefinitionCount": len(api_names | event_names),
"rpcMethodCount": len(surface["rpcMethods"]),
"sessionEventVariantCount": len(surface["sessionEventVariants"]),
"apiDefinitionNamesSha256": sha256_names(api_names),
"sessionEventDefinitionNamesSha256": sha256_names(event_names),
"rpcMethodNamesSha256": sha256_names(surface["rpcMethods"]),
"sessionEventWireTypesSha256": sha256_names(
variant["wireType"] for variant in surface["sessionEventVariants"]
),
"apiKindCounts": dict(sorted(api_kinds.items())),
"sessionEventKindCounts": dict(sorted(event_kinds.items())),
"surfaceSha256": sha256_json(surface),
}
schema_sources = {
"apiSha256": sha256_bytes(api_path.read_bytes()),
"sessionEventsSha256": sha256_bytes(session_events_path.read_bytes()),
}
return {"schemaSources": schema_sources, "coverage": coverage, "surface": surface}
def validate_surface(baseline: dict[str, Any]) -> None:
coverage = baseline["coverage"]
surface = baseline["surface"]
api = surface["api"]
events = surface["sessionEvents"]
methods = surface["rpcMethods"]
variants = surface["sessionEventVariants"]
checks = {
"apiDefinitionCount": len(api),
"sessionEventDefinitionCount": len(events),
"sharedDefinitionCount": len(
{entry["name"] for entry in api} & {entry["name"] for entry in events}
),
"uniqueDefinitionCount": len(
{entry["name"] for entry in api} | {entry["name"] for entry in events}
),
"rpcMethodCount": len(methods),
"sessionEventVariantCount": len(variants),
"apiDefinitionNamesSha256": sha256_names(entry["name"] for entry in api),
"sessionEventDefinitionNamesSha256": sha256_names(
entry["name"] for entry in events
),
"rpcMethodNamesSha256": sha256_names(methods),
"sessionEventWireTypesSha256": sha256_names(
variant["wireType"] for variant in variants
),
"apiKindCounts": dict(sorted(Counter(entry["kind"] for entry in api).items())),
"sessionEventKindCounts": dict(
sorted(Counter(entry["kind"] for entry in events).items())
),
"surfaceSha256": sha256_json(surface),
}
for key, actual in checks.items():
if coverage.get(key) != actual:
raise ValueError(
f"coverage mismatch for {key}: {actual!r}, expected {coverage.get(key)!r}"
)
for group in SCHEMA_GROUPS:
entries = surface[group]
names = [entry["name"] for entry in entries]
if names != sorted(names) or len(names) != len(set(names)):
raise ValueError(f"{group} definitions are not unique and sorted")
for entry in entries:
expected_keys = {
"name",
"kind",
"schemaSha256",
"propertyCount",
"requiredPropertyCount",
"hasOptionalProperties",
"hasMap",
"unionVariantCount",
"knownStringValues",
}
if set(entry) != expected_keys:
raise ValueError(f"{group}.{entry.get('name')} has incomplete metadata")
def render_protocol(baseline: dict[str, Any]) -> str:
official = baseline["officialSdk"]
protocol = baseline["protocol"]
compatibility = baseline["cppCompatibility"]
return f"""// Generated by tools/generate_protocol_types.py. DO NOT EDIT.
#pragma once
#include <string_view>
namespace copilot::generated
{{
inline constexpr int kSdkProtocolVersion = {protocol["version"]};
inline constexpr int kMinProtocolVersion = {compatibility["minimumProtocolVersion"]};
inline constexpr std::string_view kOfficialSdkCommit = "{official["commit"]}";
inline constexpr std::string_view kOfficialSchemaPackageVersion = "{official["schemaPackageVersion"]}";
}} // namespace copilot::generated
"""
def render_schema_value() -> str:
return """// Generated by tools/generate_protocol_types.py. DO NOT EDIT.
#pragma once
#include <algorithm>
#include <array>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
namespace copilot::generated
{
enum class SchemaKind
{
Alias,
Array,
Boolean,
Enum,
Integer,
Map,
Null,
Number,
Object,
Opaque,
String,
Union,
};
namespace detail
{
template <typename Tag>
class SchemaValue
{
public:
SchemaValue()
: value_(default_value())
{
}
explicit SchemaValue(nlohmann::json value)
: value_(std::move(value))
{
}
const nlohmann::json& raw() const noexcept
{
return value_;
}
nlohmann::json& raw() noexcept
{
return value_;
}
template <typename T>
T as() const
{
return value_.template get<T>();
}
bool is_known_string_value() const
{
return value_.is_string() && contains_known(value_.template get_ref<const std::string&>());
}
std::optional<std::string> discriminator(std::string_view key = "type") const
{
if (!value_.is_object())
return std::nullopt;
const auto it = value_.find(std::string(key));
if (it == value_.end() || !it->is_string())
return std::nullopt;
return it->template get<std::string>();
}
bool has_known_discriminator(std::string_view key = "type") const
{
const auto value = discriminator(key);
return value && contains_known(*value);
}
bool operator==(const SchemaValue&) const = default;
private:
static nlohmann::json default_value()
{
if constexpr (Tag::kind == SchemaKind::Object || Tag::kind == SchemaKind::Map)
return nlohmann::json::object();
if constexpr (Tag::kind == SchemaKind::Array)
return nlohmann::json::array();
return nullptr;
}
static bool contains_known(std::string_view value)
{
return std::find(Tag::known_string_values.begin(), Tag::known_string_values.end(), value) !=
Tag::known_string_values.end();
}
nlohmann::json value_;
};
template <typename Tag>
inline void to_json(nlohmann::json& j, const SchemaValue<Tag>& value)
{
j = value.raw();
}
template <typename Tag>
inline void from_json(const nlohmann::json& j, SchemaValue<Tag>& value)
{
value = SchemaValue<Tag>(j);
}
} // namespace detail
} // namespace copilot::generated
"""
CPP_KIND = {
"alias": "Alias",
"array": "Array",
"boolean": "Boolean",
"enum": "Enum",
"integer": "Integer",
"map": "Map",
"null": "Null",
"number": "Number",
"object": "Object",
"opaque": "Opaque",
"string": "String",
"union": "Union",
}
def render_tag(entry: dict[str, Any]) -> str:
values = entry["knownStringValues"]
value_lines = ", ".join(cpp_string(value) for value in values)
return f"""struct {entry["name"]}SchemaTag
{{
inline static constexpr std::string_view schema_name = {cpp_string(entry["name"])};
inline static constexpr SchemaKind kind = SchemaKind::{CPP_KIND[entry["kind"]]};
inline static constexpr bool has_optional_properties = {str(entry["hasOptionalProperties"]).lower()};
inline static constexpr bool has_map = {str(entry["hasMap"]).lower()};
inline static constexpr std::size_t property_count = {entry["propertyCount"]};
inline static constexpr std::size_t required_property_count = {entry["requiredPropertyCount"]};
inline static constexpr std::size_t union_variant_count = {entry["unionVariantCount"]};
inline static constexpr std::array<std::string_view, {len(values)}> known_string_values{{{value_lines}}};
}};
using {entry["name"]} = detail::SchemaValue<{entry["name"]}SchemaTag>;"""
def render_schema_header(
entries: list[dict[str, Any]],
namespace_name: str,
source_name: str,
extra: str,
) -> str:
tags = "\n\n".join(render_tag(entry) for entry in entries)
return f"""// Generated by tools/generate_protocol_types.py from {source_name}. DO NOT EDIT.
#pragma once
#include <copilot/generated/compatibility_types.hpp>
#include <copilot/generated/schema_coverage.hpp>
#include <copilot/generated/schema_value.hpp>
namespace copilot::generated::{namespace_name}
{{
inline constexpr std::size_t kGeneratedDefinitionCount = {len(entries)};
{extra}
{tags}
}} // namespace copilot::generated::{namespace_name}
"""
def render_string_array(name: str, values: list[str]) -> str:
lines = ",\n".join(f" {cpp_string(value)}" for value in values)
return (
f"inline constexpr std::array<std::string_view, {len(values)}> {name}{{{{\n"
f"{lines}\n"
"}};\n"
)
def render_coverage(baseline: dict[str, Any]) -> str:
coverage = baseline["coverage"]
sources = baseline["schemaSources"]
return f"""// Generated by tools/generate_protocol_types.py. DO NOT EDIT.
#pragma once
#include <cstddef>
#include <string_view>
namespace copilot::generated::coverage
{{
inline constexpr std::size_t kApiDefinitionCount = {coverage["apiDefinitionCount"]};
inline constexpr std::size_t kSessionEventDefinitionCount = {coverage["sessionEventDefinitionCount"]};
inline constexpr std::size_t kSharedDefinitionCount = {coverage["sharedDefinitionCount"]};
inline constexpr std::size_t kUniqueDefinitionCount = {coverage["uniqueDefinitionCount"]};
inline constexpr std::size_t kRpcMethodCount = {coverage["rpcMethodCount"]};
inline constexpr std::size_t kSessionEventVariantCount = {coverage["sessionEventVariantCount"]};
inline constexpr std::string_view kApiSchemaSha256 = "{sources["apiSha256"]}";
inline constexpr std::string_view kSessionEventsSchemaSha256 = "{sources["sessionEventsSha256"]}";
inline constexpr std::string_view kApiDefinitionNamesSha256 = "{coverage["apiDefinitionNamesSha256"]}";
inline constexpr std::string_view kSessionEventDefinitionNamesSha256 = "{coverage["sessionEventDefinitionNamesSha256"]}";
inline constexpr std::string_view kRpcMethodNamesSha256 = "{coverage["rpcMethodNamesSha256"]}";
inline constexpr std::string_view kSessionEventWireTypesSha256 = "{coverage["sessionEventWireTypesSha256"]}";
inline constexpr std::string_view kSurfaceSha256 = "{coverage["surfaceSha256"]}";
}} // namespace copilot::generated::coverage
"""
def render_compatibility_types() -> str:
return """// Generated compatibility types retained from the first parity tranche. DO NOT EDIT.
#pragma once
#include <cstdint>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
namespace copilot::generated
{
struct PingRequest
{
std::optional<std::string> message{};
bool operator==(const PingRequest&) const = default;
};
inline void to_json(nlohmann::json& j, const PingRequest& value)
{
j = nlohmann::json::object();
if (value.message)
j["message"] = *value.message;
}
inline void from_json(const nlohmann::json& j, PingRequest& value)
{
if (j.contains("message") && !j.at("message").is_null())
value.message = j.at("message").get<std::string>();
else
value.message.reset();
}
struct PingResult
{
std::string message{};
std::string timestamp{};
std::int64_t protocol_version{};
bool operator==(const PingResult&) const = default;
};
inline void to_json(nlohmann::json& j, const PingResult& value)
{
j = nlohmann::json{
{"message", value.message},
{"timestamp", value.timestamp},
{"protocolVersion", value.protocol_version},
};
}
inline void from_json(const nlohmann::json& j, PingResult& value)
{
j.at("message").get_to(value.message);
j.at("timestamp").get_to(value.timestamp);
j.at("protocolVersion").get_to(value.protocol_version);
}
struct SessionIdleData
{
std::optional<bool> aborted{};
bool operator==(const SessionIdleData&) const = default;
};
inline void to_json(nlohmann::json& j, const SessionIdleData& value)
{
j = nlohmann::json::object();
if (value.aborted)
j["aborted"] = *value.aborted;
}
inline void from_json(const nlohmann::json& j, SessionIdleData& value)
{
if (j.contains("aborted") && !j.at("aborted").is_null())
value.aborted = j.at("aborted").get<bool>();
else
value.aborted.reset();
}
} // namespace copilot::generated
"""
def rendered_files(baseline: dict[str, Any]) -> dict[str, str]:
validate_surface(baseline)
surface = baseline["surface"]
rpc_extra = render_string_array("kRpcMethods", surface["rpcMethods"])
event_wire_types = [
variant["wireType"] for variant in surface["sessionEventVariants"]
]
event_extra = render_string_array("kSessionEventWireTypes", event_wire_types)
return {
"protocol_version.hpp": render_protocol(baseline),
"schema_value.hpp": render_schema_value(),
"schema_coverage.hpp": render_coverage(baseline),
"api_types.hpp": render_schema_header(
surface["api"], "api", "api.schema.json", rpc_extra
),
"session_events.hpp": render_schema_header(
surface["sessionEvents"],
"events",
"session-events.schema.json",
event_extra,
),
"compatibility_types.hpp": render_compatibility_types(),
}
def write_or_check(files: dict[str, str], output: Path, check: bool) -> list[str]:
failures: list[str] = []
for name, content in files.items():
path = output / name
if check:
actual = path.read_text(encoding="utf-8") if path.exists() else None
if actual != content:
try:
failures.append(str(path.relative_to(REPO_ROOT)))
except ValueError:
failures.append(str(path))
else:
output.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8", newline="\n")
return failures
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--baseline", type=Path, default=DEFAULT_BASELINE)
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
parser.add_argument("--check", action="store_true")
parser.add_argument("--refresh", action="store_true")
parser.add_argument("--official-sdk-root", type=Path)
parser.add_argument("--api-schema", type=Path)
parser.add_argument("--session-events-schema", type=Path)
args = parser.parse_args()
baseline = load_json(args.baseline)
if args.official_sdk_root:
validate_official_sdk(args.official_sdk_root, baseline)
if args.refresh:
if args.check:
parser.error("--refresh and --check cannot be combined")
if not args.official_sdk_root or not args.api_schema or not args.session_events_schema:
parser.error(
"--refresh requires --official-sdk-root, --api-schema, "
"and --session-events-schema"
)
refreshed = build_surface(args.api_schema, args.session_events_schema, baseline)
baseline.update(refreshed)
baseline.pop("selectedDefinitions", None)
baseline["formatVersion"] = 2
args.baseline.write_text(
json.dumps(baseline, indent=2, ensure_ascii=False) + "\n",
encoding="utf-8",
newline="\n",
)
failures = write_or_check(rendered_files(baseline), args.output, args.check)
if failures:
print("Generated protocol files are stale:", file=sys.stderr)
for failure in failures:
print(f" {failure}", file=sys.stderr)
print("Run: python tools/generate_protocol_types.py", file=sys.stderr)
return 1
action = "checked" if args.check else "generated"
coverage = baseline["coverage"]
print(
f"Protocol files {action}: "
f"{coverage['apiDefinitionCount']} API definitions, "
f"{coverage['sessionEventDefinitionCount']} event definitions, "
f"{coverage['rpcMethodCount']} RPC methods, "
f"{coverage['sessionEventVariantCount']} event variants"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())