openrouter-rs 0.11.1

A type-safe OpenRouter Rust SDK
Documentation
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
#!/usr/bin/env python3

from __future__ import annotations

import argparse
import datetime as dt
import difflib
import hashlib
import json
import re
import sys
import urllib.request
from pathlib import Path
from typing import Any

UPSTREAM_OPENAPI_URL = "https://openrouter.ai/openapi.json"
OPENROUTER_EXAMPLE_TOKEN_PATTERN = re.compile(r"sk-or-v1-[A-Za-z0-9_-]{20,}")
OPENROUTER_EXAMPLE_TOKEN_PLACEHOLDER = "sk-or-v1-[REDACTED]"
HTTP_METHODS = ("get", "post", "put", "patch", "delete", "options", "head", "trace")
DOC_ONLY_FIELDS = {"description", "example", "examples", "externalDocs", "summary", "title"}
REPO_KNOWN_METADATA_PARAMETERS = frozenset(
    {
        ("header", "HTTP-Referer"),
        ("header", "X-Title"),
        ("header", "X-OpenRouter-Categories"),
        ("header", "X-OpenRouter-Title"),
    }
)
REPO_SUPPORTED_METADATA_PARAMETER_SHAPES = {
    ("header", "HTTP-Referer"): {
        "in": "header",
        "name": "HTTP-Referer",
        "schema": {
            "type": "string",
        },
    },
    ("header", "X-OpenRouter-Categories"): {
        "in": "header",
        "name": "X-OpenRouter-Categories",
        "schema": {
            "type": "string",
        },
        "x-speakeasy-name-override": "appCategories",
    },
    ("header", "X-Title"): {
        "in": "header",
        "name": "X-Title",
        "schema": {
            "type": "string",
        },
    },
    ("header", "X-OpenRouter-Title"): {
        "in": "header",
        "name": "X-OpenRouter-Title",
        "schema": {
            "type": "string",
        },
        "x-speakeasy-name-override": "appTitle",
    },
}
REPO_DYNAMIC_PROVIDER_NAME_MARKERS = frozenset({"Anthropic", "Google", "OpenAI"})
REPO_DYNAMIC_OUTPUT_MODALITY_MARKERS = frozenset({"image", "text", "video"})
REPO_FLEXIBLE_PROVIDER_OPTION_MARKERS = frozenset({"anthropic", "google-vertex", "openai"})
REPO_FLEXIBLE_PROVIDER_OPTION_VALUE_SCHEMA = {
    "additionalProperties": {
        "nullable": True,
    },
    "type": "object",
}
REPO_RESPONSES_FLEXIBLE_NULLABILITY_FIELDS = frozenset(
    {
        "instructions",
        "text",
        "top_logprobs",
    }
)
REPO_FLEXIBLE_PLUGIN_OPERATION_KEYS = frozenset(
    {
        "POST /chat/completions",
        "POST /messages",
        "POST /responses",
    }
)
BASELINE_TOP_LEVEL_FIELDS = (
    "components",
    "info",
    "jsonSchemaDialect",
    "openapi",
    "paths",
    "security",
    "servers",
    "tags",
)


def utc_now_iso() -> str:
    return dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat()


def ensure_parent(path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)


def read_json(path: Path) -> dict[str, Any]:
    with path.open("r", encoding="utf-8") as handle:
        return json.load(handle)


def write_json(path: Path, payload: Any) -> None:
    ensure_parent(path)
    with path.open("w", encoding="utf-8") as handle:
        json.dump(payload, handle, indent=2, sort_keys=True)
        handle.write("\n")


def write_text(path: Path, payload: str) -> None:
    ensure_parent(path)
    path.write_text(payload, encoding="utf-8")


def fetch_spec(url: str) -> dict[str, Any]:
    with urllib.request.urlopen(url) as response:
        return json.load(response)


def strip_doc_only_fields(value: Any) -> Any:
    if isinstance(value, dict):
        return {
            key: strip_doc_only_fields(item)
            for key, item in value.items()
            if key not in DOC_ONLY_FIELDS
        }

    if isinstance(value, list):
        return [strip_doc_only_fields(item) for item in value]

    return value


def canonical_json(value: Any) -> str:
    return json.dumps(value, sort_keys=True, separators=(",", ":"))


def short_hash(value: Any) -> str:
    return hashlib.sha256(canonical_json(value).encode("utf-8")).hexdigest()[:16]


def validate_openapi_spec(spec: Any, source: str) -> dict[str, Any]:
    if not isinstance(spec, dict):
        raise ValueError(f"{source} did not contain a top-level JSON object.")

    openapi_version = spec.get("openapi")
    if not isinstance(openapi_version, str) or not openapi_version:
        raise ValueError(f"{source} is not an OpenAPI document: missing top-level `openapi` string.")

    paths = spec.get("paths")
    if not isinstance(paths, dict):
        raise ValueError(f"{source} is not an OpenAPI document: missing top-level `paths` object.")

    return redact_openrouter_example_tokens(spec)


def decode_json_pointer_token(token: str) -> str:
    return token.replace("~1", "/").replace("~0", "~")


def resolve_json_pointer(document: dict[str, Any], pointer: str) -> Any:
    if pointer == "#":
        return document

    if not pointer.startswith("#/"):
        raise ValueError(f"Only local JSON pointers are supported, got: {pointer}")

    current: Any = document
    for token in pointer[2:].split("/"):
        decoded_token = decode_json_pointer_token(token)

        if isinstance(current, list):
            try:
                current = current[int(decoded_token)]
            except (ValueError, IndexError) as exc:
                raise KeyError(decoded_token) from exc
            continue

        if isinstance(current, dict):
            current = current[decoded_token]
            continue

        raise TypeError(f"JSON pointer segment {decoded_token!r} cannot be applied to {type(current).__name__}")
    return current


def resolve_local_refs(value: Any, document: dict[str, Any], active_refs: frozenset[str] = frozenset()) -> Any:
    if isinstance(value, dict):
        ref = value.get("$ref")
        if isinstance(ref, str) and ref.startswith("#/"):
            if ref in active_refs:
                return {"$ref": ref}

            resolved = resolve_local_refs(
                resolve_json_pointer(document, ref),
                document,
                active_refs | {ref},
            )
            siblings = {
                key: resolve_local_refs(item, document, active_refs)
                for key, item in value.items()
                if key != "$ref"
            }

            if siblings and isinstance(resolved, dict):
                merged = dict(resolved)
                merged.update(siblings)
                return merged

            if siblings:
                return {"allOf": [resolved], **siblings}

            return resolved

        return {
            key: resolve_local_refs(item, document, active_refs)
            for key, item in value.items()
        }

    if isinstance(value, list):
        return [resolve_local_refs(item, document, active_refs) for item in value]

    return value


def merge_parameter_lists(
    inherited_parameters: list[Any],
    operation_parameters: list[Any],
) -> list[Any]:
    merged: list[Any] = []
    parameter_index: dict[tuple[str, str], int] = {}

    for parameter in inherited_parameters + operation_parameters:
        if not isinstance(parameter, dict):
            merged.append(parameter)
            continue

        name = parameter.get("name")
        location = parameter.get("in")
        if not isinstance(name, str) or not isinstance(location, str):
            merged.append(parameter)
            continue

        parameter_key = (name, location)
        existing_index = parameter_index.get(parameter_key)
        if existing_index is None:
            parameter_index[parameter_key] = len(merged)
            merged.append(parameter)
            continue

        merged[existing_index] = parameter

    return merged


def normalize_parameter_order(parameters: Any) -> Any:
    if not isinstance(parameters, list):
        return parameters

    def parameter_sort_key(parameter: Any) -> tuple[int, str, str, str]:
        if isinstance(parameter, dict):
            name = parameter.get("name")
            location = parameter.get("in")
            if isinstance(name, str) and isinstance(location, str):
                return (0, location, name, canonical_json(parameter))

        return (1, "", "", canonical_json(parameter))

    return sorted(parameters, key=parameter_sort_key)


def repo_known_metadata_parameter_key(parameter: Any) -> tuple[str, str] | None:
    if not isinstance(parameter, dict):
        return None

    name = parameter.get("name")
    location = parameter.get("in")
    if not isinstance(name, str) or not isinstance(location, str):
        return None

    parameter_key = (location, name)
    if parameter_key not in REPO_KNOWN_METADATA_PARAMETERS:
        return None

    return parameter_key


def is_repo_supported_metadata_parameter(parameter: Any) -> bool:
    parameter_key = repo_known_metadata_parameter_key(parameter)
    if parameter_key is None:
        return False

    return parameter == REPO_SUPPORTED_METADATA_PARAMETER_SHAPES[parameter_key]


def collect_repo_supported_metadata_parameters(operation: Any) -> list[str]:
    if not isinstance(operation, dict):
        return []

    parameters = operation.get("parameters")
    if not isinstance(parameters, list):
        return []

    supported_parameters = {
        f"{parameter['in']} {parameter['name']}"
        for parameter in parameters
        if repo_known_metadata_parameter_key(parameter) is not None
    }
    return sorted(supported_parameters)


def collect_exact_repo_supported_metadata_parameters(operation: Any) -> list[str]:
    if not isinstance(operation, dict):
        return []

    parameters = operation.get("parameters")
    if not isinstance(parameters, list):
        return []

    exact_supported_parameters = {
        f"{parameter['in']} {parameter['name']}"
        for parameter in parameters
        if is_repo_supported_metadata_parameter(parameter)
    }
    return sorted(exact_supported_parameters)


def strip_repo_supported_metadata_parameters(operation: Any) -> Any:
    if not isinstance(operation, dict):
        return operation

    stripped_operation = dict(operation)
    parameters = stripped_operation.get("parameters")
    if not isinstance(parameters, list):
        return stripped_operation

    filtered_parameters = [
        parameter
        for parameter in parameters
        if not is_repo_supported_metadata_parameter(parameter)
    ]

    if filtered_parameters:
        stripped_operation["parameters"] = normalize_parameter_order(filtered_parameters)
    else:
        stripped_operation.pop("parameters", None)

    return stripped_operation


def scalar_enum_values(value: Any) -> set[Any]:
    enum_values = value.get("enum") if isinstance(value, dict) else None
    if not isinstance(enum_values, list):
        return set()

    return {
        item
        for item in enum_values
        if item is None or isinstance(item, (str, int, float, bool))
    }


def is_repo_supported_dynamic_provider_name_enum(value: Any) -> bool:
    if not isinstance(value, dict):
        return False

    enum_values = scalar_enum_values(value)
    string_values = {item for item in enum_values if isinstance(item, str)}
    return (
        value.get("type") == "string"
        and value.get("x-speakeasy-unknown-values") == "allow"
        and REPO_DYNAMIC_PROVIDER_NAME_MARKERS.issubset(string_values)
    )


def is_repo_supported_dynamic_output_modality_enum(value: Any) -> bool:
    if not isinstance(value, dict):
        return False

    enum_values = scalar_enum_values(value)
    string_values = {item for item in enum_values if isinstance(item, str)}
    return (
        value.get("type") == "string"
        and value.get("x-speakeasy-unknown-values") == "allow"
        and REPO_DYNAMIC_OUTPUT_MODALITY_MARKERS.issubset(string_values)
    )


def is_repo_supported_provider_options_map(value: Any) -> bool:
    if not isinstance(value, dict):
        return False

    properties = value.get("properties")
    if not isinstance(properties, dict):
        return False

    property_names = set(properties)
    return (
        value.get("type") == "object"
        and REPO_FLEXIBLE_PROVIDER_OPTION_MARKERS.issubset(property_names)
        and all(
            property_schema == REPO_FLEXIBLE_PROVIDER_OPTION_VALUE_SCHEMA
            for property_schema in properties.values()
        )
    )


def is_responses_response_payload_path(operation_key: str, path: tuple[Any, ...]) -> bool:
    return operation_key == "POST /responses" and bool(path) and path[0] == "responses"


def is_request_schema_property_path(path: tuple[Any, ...], property_name: str) -> bool:
    return (
        "requestBody" in path
        and len(path) >= 2
        and path[-2:] == ("properties", property_name)
    )


def is_response_schema_property_path(path: tuple[Any, ...], property_name: str) -> bool:
    return (
        "responses" in path
        and len(path) >= 2
        and path[-2:] == ("properties", property_name)
    )


def schema_has_type(value: Any, schema_type: str) -> bool:
    if not isinstance(value, dict):
        return False

    value_type = value.get("type")
    return value_type == schema_type or (
        isinstance(value_type, list) and schema_type in value_type
    )


def is_repo_supported_flexible_plugin_payload_path(
    operation_key: str,
    path: tuple[Any, ...],
) -> bool:
    return (
        operation_key in REPO_FLEXIBLE_PLUGIN_OPERATION_KEYS
        and is_request_schema_property_path(path, "plugins")
    )


def is_repo_supported_messages_tool_payload_path(
    operation_key: str,
    path: tuple[Any, ...],
) -> bool:
    return operation_key == "POST /messages" and is_request_schema_property_path(path, "tools")


def is_repo_supported_responses_tool_payload_path(
    operation_key: str,
    path: tuple[Any, ...],
) -> bool:
    return operation_key == "POST /responses" and is_request_schema_property_path(path, "tools")


def is_repo_supported_responses_output_payload_path(
    operation_key: str,
    path: tuple[Any, ...],
) -> bool:
    return operation_key == "POST /responses" and is_response_schema_property_path(path, "output")


def is_repo_supported_flexible_plugin_payload(
    operation_key: str,
    path: tuple[Any, ...],
    value: Any,
) -> bool:
    return is_repo_supported_flexible_plugin_payload_path(
        operation_key, path
    ) and schema_has_type(value, "array")


def is_repo_supported_messages_tool_payload(
    operation_key: str,
    path: tuple[Any, ...],
    value: Any,
) -> bool:
    return is_repo_supported_messages_tool_payload_path(
        operation_key, path
    ) and schema_has_type(value, "array")


def is_repo_supported_responses_tool_payload(
    operation_key: str,
    path: tuple[Any, ...],
    value: Any,
) -> bool:
    return is_repo_supported_responses_tool_payload_path(
        operation_key, path
    ) and schema_has_type(value, "array")


def is_repo_supported_responses_output_payload(
    operation_key: str,
    path: tuple[Any, ...],
    value: Any,
) -> bool:
    return is_repo_supported_responses_output_payload_path(
        operation_key, path
    ) and schema_has_type(value, "array")


def strip_repo_supported_schema_details(
    operation_key: str,
    value: Any,
    path: tuple[Any, ...] = (),
) -> Any:
    if isinstance(value, dict):
        if is_repo_supported_flexible_plugin_payload(operation_key, path, value):
            return {"<repo-supported-flexible-plugin-payload>": True}

        if is_repo_supported_messages_tool_payload(operation_key, path, value):
            return {"<repo-supported-messages-tool-payload>": True}

        if is_repo_supported_responses_tool_payload(operation_key, path, value):
            return {"<repo-supported-responses-tool-payload>": True}

        if is_repo_supported_responses_output_payload(operation_key, path, value):
            return {"<repo-supported-responses-output-payload>": True}

        stripped = {
            key: strip_repo_supported_schema_details(operation_key, item, path + (key,))
            for key, item in value.items()
        }

        if (
            is_repo_supported_dynamic_provider_name_enum(stripped)
            or is_repo_supported_dynamic_output_modality_enum(stripped)
        ):
            stripped["enum"] = ["<repo-supported-dynamic-enum>"]

        if is_repo_supported_provider_options_map(stripped):
            stripped["properties"] = {
                "<repo-supported-provider-options>": REPO_FLEXIBLE_PROVIDER_OPTION_VALUE_SCHEMA
            }

        if is_responses_response_payload_path(operation_key, path):
            properties = stripped.get("properties")
            if isinstance(properties, dict):
                for field_name in REPO_RESPONSES_FLEXIBLE_NULLABILITY_FIELDS:
                    field_schema = properties.get(field_name)
                    if isinstance(field_schema, dict):
                        field_schema.pop("nullable", None)

        return stripped

    if isinstance(value, list):
        return [
            strip_repo_supported_schema_details(operation_key, item, path + (index,))
            for index, item in enumerate(value)
        ]

    return value


def collect_repo_supported_schema_rules(operation_key: str, value: Any) -> list[str]:
    rules: set[str] = set()

    def collect(item: Any, path: tuple[Any, ...] = ()) -> None:
        if isinstance(item, dict):
            if is_repo_supported_dynamic_provider_name_enum(item):
                rules.add("dynamic provider name enum")
            if is_repo_supported_dynamic_output_modality_enum(item):
                rules.add("dynamic output modality enum")
            if is_repo_supported_provider_options_map(item):
                rules.add("provider-specific options map")
            if is_repo_supported_flexible_plugin_payload(operation_key, path, item):
                rules.add("flexible plugin payload")
            if is_repo_supported_messages_tool_payload(operation_key, path, item):
                rules.add("Messages flexible tool payload")
            if is_repo_supported_responses_tool_payload(operation_key, path, item):
                rules.add("Responses flexible tool payload")
            if is_repo_supported_responses_output_payload(operation_key, path, item):
                rules.add("Responses flexible output payload")
            if is_responses_response_payload_path(operation_key, path):
                properties = item.get("properties")
                if isinstance(properties, dict):
                    for field_name in REPO_RESPONSES_FLEXIBLE_NULLABILITY_FIELDS:
                        field_schema = properties.get(field_name)
                        if isinstance(field_schema, dict) and field_schema.get("nullable") is True:
                            rules.add("Responses flexible nullable fields")

            for key, child in item.items():
                collect(child, path + (key,))
            return

        if isinstance(item, list):
            for index, child in enumerate(item):
                collect(child, path + (index,))

    collect(value)
    return sorted(rules)


def classify_repo_impact_for_changed_operation(
    operation_key: str,
    baseline_operation: dict[str, Any],
    candidate_operation: dict[str, Any],
) -> dict[str, Any]:
    baseline_normalized = baseline_operation["normalized"]
    candidate_normalized = candidate_operation["normalized"]
    supported_parameters = sorted(
        {
            *collect_repo_supported_metadata_parameters(baseline_normalized),
            *collect_repo_supported_metadata_parameters(candidate_normalized),
        }
    )
    exact_supported_parameters = sorted(
        {
            *collect_exact_repo_supported_metadata_parameters(baseline_normalized),
            *collect_exact_repo_supported_metadata_parameters(candidate_normalized),
        }
    )

    baseline_without_supported = strip_repo_supported_metadata_parameters(
        baseline_normalized
    )
    candidate_without_supported = strip_repo_supported_metadata_parameters(
        candidate_normalized
    )
    schema_rules = sorted(
        {
            *collect_repo_supported_schema_rules(operation_key, baseline_without_supported),
            *collect_repo_supported_schema_rules(operation_key, candidate_without_supported),
        }
    )

    baseline_without_supported = strip_repo_supported_schema_details(
        operation_key,
        baseline_without_supported,
    )
    candidate_without_supported = strip_repo_supported_schema_details(
        operation_key,
        candidate_without_supported,
    )

    if (
        (exact_supported_parameters or schema_rules)
        and baseline_without_supported == candidate_without_supported
    ):
        return {
            "category": "already_supported",
            "schema_rules": schema_rules,
            "supported_parameters": supported_parameters,
        }

    return {
        "category": "actionable",
        "schema_rules": schema_rules,
        "supported_parameters": supported_parameters,
    }


def normalize_security_order(security: Any) -> Any:
    if not isinstance(security, list):
        return security

    normalized_requirements: list[Any] = []
    for requirement in security:
        if not isinstance(requirement, dict):
            normalized_requirements.append(requirement)
            continue

        normalized_requirement: dict[str, Any] = {}
        for scheme_name in sorted(requirement):
            scopes = requirement[scheme_name]
            if isinstance(scopes, list):
                normalized_requirement[scheme_name] = sorted(scopes, key=canonical_json)
            else:
                normalized_requirement[scheme_name] = scopes

        normalized_requirements.append(normalized_requirement)

    return sorted(normalized_requirements, key=canonical_json)


def canonicalize_unordered_schema_collections(value: Any, key: str | None = None) -> Any:
    if isinstance(value, dict):
        normalized: dict[str, Any] = {}
        for child_key, child_value in value.items():
            normalized[child_key] = canonicalize_unordered_schema_collections(
                child_value,
                child_key,
            )

        dependent_required = normalized.get("dependentRequired")
        if isinstance(dependent_required, dict):
            normalized["dependentRequired"] = {
                dependency_key: canonicalize_unordered_schema_collections(
                    dependency_value,
                    "required",
                )
                for dependency_key, dependency_value in dependent_required.items()
            }

        return normalized

    if isinstance(value, list):
        normalized_items = [
            canonicalize_unordered_schema_collections(item)
            for item in value
        ]

        if key in {"required", "enum", "type"}:
            return sorted(normalized_items, key=canonical_json)

        if key in {"allOf", "anyOf", "oneOf"}:
            return sorted(normalized_items, key=canonical_json)

        return normalized_items

    return value


def collect_effective_security_schemes(
    effective_security: Any,
    spec: dict[str, Any],
) -> dict[str, Any] | None:
    if not isinstance(effective_security, list):
        return None

    security_schemes = spec.get("components", {}).get("securitySchemes", {})
    if not isinstance(security_schemes, dict):
        return None

    resolved_schemes: dict[str, Any] = {}
    for requirement in effective_security:
        if not isinstance(requirement, dict):
            continue

        for scheme_name in sorted(requirement):
            scheme_definition = security_schemes.get(scheme_name)
            if scheme_definition is None:
                continue
            resolved_schemes[scheme_name] = resolve_local_refs(scheme_definition, spec)

    return resolved_schemes or None


def inherit_effective_operation_fields(
    raw_operation: dict[str, Any],
    path_item: dict[str, Any],
    spec: dict[str, Any],
) -> dict[str, Any]:
    inherited_operation = dict(raw_operation)

    path_parameters = path_item.get("parameters", [])
    operation_parameters = raw_operation.get("parameters", [])
    if path_parameters or operation_parameters:
        inherited_operation["parameters"] = merge_parameter_lists(
            path_parameters if isinstance(path_parameters, list) else [],
            operation_parameters if isinstance(operation_parameters, list) else [],
        )
        inherited_operation["parameters"] = normalize_parameter_order(
            inherited_operation["parameters"]
        )

    if "servers" not in inherited_operation:
        if "servers" in path_item:
            inherited_operation["servers"] = path_item["servers"]
        elif "servers" in spec:
            inherited_operation["servers"] = spec["servers"]

    if "security" not in inherited_operation and "security" in spec:
        inherited_operation["security"] = spec["security"]
    if "security" in inherited_operation:
        inherited_operation["security"] = normalize_security_order(
            inherited_operation["security"]
        )

    resolved_security_schemes = collect_effective_security_schemes(
        inherited_operation.get("security"),
        spec,
    )
    if resolved_security_schemes:
        inherited_operation["_effective_security_schemes"] = resolved_security_schemes

    return inherited_operation


def normalize_path_item(path_item: dict[str, Any], spec: dict[str, Any]) -> dict[str, Any]:
    resolved_path_item = resolve_local_refs(path_item, spec)
    if not isinstance(resolved_path_item, dict):
        raise TypeError("Resolved Path Item must be an object")
    return resolved_path_item


def normalize_operation(raw_operation: dict[str, Any], path_item: dict[str, Any], spec: dict[str, Any]) -> Any:
    inherited_operation = inherit_effective_operation_fields(raw_operation, path_item, spec)
    normalized_operation = strip_doc_only_fields(inherited_operation)
    return canonicalize_unordered_schema_collections(normalized_operation)


def collect_operations(spec: dict[str, Any]) -> dict[str, dict[str, Any]]:
    operations: dict[str, dict[str, Any]] = {}

    for path, raw_path_item in sorted(spec.get("paths", {}).items()):
        path_item = normalize_path_item(raw_path_item, spec)
        for method in HTTP_METHODS:
            if method not in path_item:
                continue

            raw_operation = path_item[method]
            if not isinstance(raw_operation, dict):
                continue

            normalized = normalize_operation(raw_operation, path_item, spec)
            operation_key = f"{method.upper()} {path}"
            operations[operation_key] = {
                "id": operation_key,
                "method": method.upper(),
                "path": path,
                "operation_id": raw_operation.get("operationId"),
                "tags": raw_operation.get("tags", []),
                "deprecated": raw_operation.get("deprecated", False),
                "fingerprint": short_hash(normalized),
                "normalized": normalized,
            }

    return operations


def reduce_spec_for_baseline(spec: dict[str, Any]) -> dict[str, Any]:
    reduced = {field: spec[field] for field in BASELINE_TOP_LEVEL_FIELDS if field in spec}
    reduced["paths"] = spec.get("paths", {})
    return redact_openrouter_example_tokens(reduced)


def redact_openrouter_example_tokens(value: Any) -> Any:
    if isinstance(value, str):
        return OPENROUTER_EXAMPLE_TOKEN_PATTERN.sub(
            OPENROUTER_EXAMPLE_TOKEN_PLACEHOLDER,
            value,
        )
    if isinstance(value, list):
        return [redact_openrouter_example_tokens(item) for item in value]
    if isinstance(value, dict):
        return {
            key: redact_openrouter_example_tokens(item)
            for key, item in value.items()
        }
    return value


def build_snapshot(spec: dict[str, Any], source_url: str) -> dict[str, Any]:
    operations = collect_operations(spec)
    return {
        "captured_at": utc_now_iso(),
        "info": spec.get("info", {}),
        "operation_count": len(operations),
        "operations": [
            {
                "deprecated": operation["deprecated"],
                "fingerprint": operation["fingerprint"],
                "id": operation["id"],
                "method": operation["method"],
                "operation_id": operation["operation_id"],
                "path": operation["path"],
                "tags": operation["tags"],
            }
            for _, operation in sorted(operations.items())
        ],
        "source_url": source_url,
    }


def diff_preview(before: Any, after: Any, max_diff_lines: int) -> list[str]:
    diff_lines = list(
        difflib.unified_diff(
            json.dumps(before, indent=2, sort_keys=True).splitlines(),
            json.dumps(after, indent=2, sort_keys=True).splitlines(),
            fromfile="baseline",
            tofile="candidate",
            lineterm="",
        )
    )

    if len(diff_lines) <= max_diff_lines:
        return diff_lines

    truncated = diff_lines[:max_diff_lines]
    truncated.append(f"... truncated {len(diff_lines) - max_diff_lines} additional diff line(s) ...")
    return truncated


def build_report(
    baseline_spec: dict[str, Any],
    candidate_spec: dict[str, Any],
    baseline_label: str,
    candidate_label: str,
    source_url: str,
    max_diff_lines: int,
) -> dict[str, Any]:
    baseline_operations = collect_operations(baseline_spec)
    candidate_operations = collect_operations(candidate_spec)

    baseline_keys = set(baseline_operations)
    candidate_keys = set(candidate_operations)

    added = sorted(candidate_keys - baseline_keys)
    removed = sorted(baseline_keys - candidate_keys)
    changed = []
    already_supported_count = 0
    actionable_changed_count = 0

    for operation_key in sorted(baseline_keys & candidate_keys):
        baseline_operation = baseline_operations[operation_key]
        candidate_operation = candidate_operations[operation_key]
        if baseline_operation["fingerprint"] == candidate_operation["fingerprint"]:
            continue

        repo_impact = classify_repo_impact_for_changed_operation(
            operation_key,
            baseline_operation,
            candidate_operation,
        )
        if repo_impact["category"] == "already_supported":
            already_supported_count += 1
        else:
            actionable_changed_count += 1

        changed.append(
            {
                "id": operation_key,
                "baseline_fingerprint": baseline_operation["fingerprint"],
                "candidate_fingerprint": candidate_operation["fingerprint"],
                "repo_impact": repo_impact,
                "diff_preview": diff_preview(
                    baseline_operation["normalized"],
                    candidate_operation["normalized"],
                    max_diff_lines=max_diff_lines,
                ),
            }
        )

    return {
        "baseline": {
            "info": baseline_spec.get("info", {}),
            "label": baseline_label,
            "operation_count": len(baseline_operations),
        },
        "candidate": {
            "info": candidate_spec.get("info", {}),
            "label": candidate_label,
            "operation_count": len(candidate_operations),
        },
        "compared_at": utc_now_iso(),
        "source_url": source_url,
        "has_drift": bool(added or removed or changed),
        "has_actionable_drift": bool(added or removed or actionable_changed_count),
        "summary": {
            "added": len(added),
            "removed": len(removed),
            "changed": len(changed),
        },
        "repo_summary": {
            "already_supported_changed": already_supported_count,
            "actionable_added": len(added),
            "actionable_removed": len(removed),
            "actionable_changed": actionable_changed_count,
        },
        "added": [
            {
                "fingerprint": candidate_operations[operation_key]["fingerprint"],
                "id": operation_key,
                "operation_id": candidate_operations[operation_key]["operation_id"],
                "tags": candidate_operations[operation_key]["tags"],
            }
            for operation_key in added
        ],
        "removed": [
            {
                "fingerprint": baseline_operations[operation_key]["fingerprint"],
                "id": operation_key,
                "operation_id": baseline_operations[operation_key]["operation_id"],
                "tags": baseline_operations[operation_key]["tags"],
            }
            for operation_key in removed
        ],
        "changed": changed,
    }


def markdown_list(title: str, items: list[str]) -> list[str]:
    if not items:
        return [f"## {title}", "", "- None", ""]

    return [f"## {title}", "", *[f"- `{item}`" for item in items], ""]


def render_markdown_report(report: dict[str, Any]) -> str:
    already_supported_changes = [
        entry
        for entry in report["changed"]
        if entry["repo_impact"]["category"] == "already_supported"
    ]
    actionable_changes = [
        entry
        for entry in report["changed"]
        if entry["repo_impact"]["category"] == "actionable"
    ]

    lines = [
        "# OpenRouter OpenAPI Drift Report",
        "",
        f"Compared at: `{report['compared_at']}`",
        f"Upstream source: `{report['source_url']}`",
        "",
        "## Summary",
        "",
        f"- Baseline: `{report['baseline']['label']}` with `{report['baseline']['operation_count']}` method+path entries",
        f"- Candidate: `{report['candidate']['label']}` with `{report['candidate']['operation_count']}` method+path entries",
        f"- Added operations: `{report['summary']['added']}`",
        f"- Removed operations: `{report['summary']['removed']}`",
        f"- Changed operations: `{report['summary']['changed']}`",
        "",
    ]

    if not report["has_drift"]:
        lines.extend(
            [
                "No operation-level drift detected after resolving local component refs and removing",
                "docs-only OpenAPI fields",
                "(`summary`, `description`, `title`, `example`, `examples`, `externalDocs`).",
                "",
            ]
        )
    else:
        lines.extend(
            [
                "Operation-level drift detected after resolving local component refs and removing",
                "docs-only OpenAPI fields",
                "(`summary`, `description`, `title`, `example`, `examples`, `externalDocs`).",
                "",
            ]
        )

    lines.extend(
        [
            "## Repo-Aware Classification",
            "",
            f"- Actionable added operations: `{report['repo_summary']['actionable_added']}`",
            f"- Actionable removed operations: `{report['repo_summary']['actionable_removed']}`",
            f"- Actionable changed operations: `{report['repo_summary']['actionable_changed']}`",
            (
                "- Changed operations already supported by repo handling: "
                f"`{report['repo_summary']['already_supported_changed']}`"
            ),
            "",
        ]
    )

    if report["has_drift"] and not report["has_actionable_drift"]:
        lines.extend(
            [
                "No actionable repo drift detected after repo-aware classification.",
                "The tracked baseline is stale, but the changed operations are already covered by",
                "the repository's global request-metadata or flexible schema handling.",
                "",
            ]
        )

    lines.extend(markdown_list("Added Operations", [entry["id"] for entry in report["added"]]))
    lines.extend(markdown_list("Removed Operations", [entry["id"] for entry in report["removed"]]))

    lines.append("## Changes Already Supported By Repo")
    lines.append("")
    if not already_supported_changes:
        lines.append("- None")
        lines.append("")
    else:
        for entry in already_supported_changes:
            support_notes = []
            if entry["repo_impact"]["supported_parameters"]:
                support_notes.extend(
                    f"`{parameter}`"
                    for parameter in entry["repo_impact"]["supported_parameters"]
                )
            if entry["repo_impact"].get("schema_rules"):
                support_notes.extend(
                    f"`{rule}`"
                    for rule in entry["repo_impact"]["schema_rules"]
                )
            support_note = ", ".join(support_notes)
            lines.append(f"- `{entry['id']}` ({support_note})")
        lines.append("")

    lines.append("## Actionable Changed Operations")
    lines.append("")
    if not actionable_changes:
        lines.append("- None")
        lines.append("")
    else:
        for entry in actionable_changes:
            lines.append(
                f"- `{entry['id']}` "
                f"(`{entry['baseline_fingerprint']}` -> `{entry['candidate_fingerprint']}`)"
            )
            support_notes = []
            if entry["repo_impact"]["supported_parameters"]:
                support_notes.extend(
                    f"`{parameter}`"
                    for parameter in entry["repo_impact"]["supported_parameters"]
                )
            if entry["repo_impact"].get("schema_rules"):
                support_notes.extend(
                    f"`{rule}`"
                    for rule in entry["repo_impact"]["schema_rules"]
                )
            if support_notes:
                lines.append(f"  Repo already covers: {', '.join(support_notes)}")
            lines.append("")
            lines.append("```diff")
            lines.extend(entry["diff_preview"] or ["# normalized operation diff was empty"])
            lines.append("```")
            lines.append("")

    lines.extend(
        [
            "## Follow-up",
            "",
            "- Review the upstream spec change against `docs/operations/official-endpoint-test-matrix.md`.",
            "- If the upstream change is accepted, refresh the tracked baseline with `just openapi-refresh-baseline`.",
            "- Update docs, tests, or endpoint coverage notes before closing the follow-up issue.",
            "",
        ]
    )

    return "\n".join(lines)


def write_github_output(
    path: Path,
    *,
    has_drift: bool,
    has_actionable_drift: bool,
    report_md: Path,
    report_json: Path,
) -> None:
    ensure_parent(path)
    with path.open("a", encoding="utf-8") as handle:
        handle.write(f"has_drift={'true' if has_drift else 'false'}\n")
        handle.write(
            "has_actionable_drift="
            f"{'true' if has_actionable_drift else 'false'}\n"
        )
        handle.write(f"report_markdown={report_md}\n")
        handle.write(f"report_json={report_json}\n")


def command_refresh_baseline(args: argparse.Namespace) -> int:
    source_url = args.source_url or UPSTREAM_OPENAPI_URL
    source_label = str(args.source_file) if args.source_file else source_url
    raw_spec = read_json(args.source_file) if args.source_file else fetch_spec(source_url)
    spec = validate_openapi_spec(raw_spec, source_label)
    reduced_spec = reduce_spec_for_baseline(spec)
    snapshot = build_snapshot(reduced_spec, source_url)
    write_json(args.baseline_json, reduced_spec)
    write_json(args.operations_json, snapshot)
    print(
        f"Refreshed baseline from {source_url} with "
        f"{snapshot['operation_count']} method+path entries."
    )
    print(f"- raw baseline: {args.baseline_json}")
    print(f"- normalized snapshot: {args.operations_json}")
    return 0


def command_refresh_source(args: argparse.Namespace) -> int:
    source_url = args.source_url or UPSTREAM_OPENAPI_URL
    source_label = str(args.source_file) if args.source_file else source_url
    raw_spec = read_json(args.source_file) if args.source_file else fetch_spec(source_url)
    spec = validate_openapi_spec(raw_spec, source_label)
    write_json(args.source_json, spec)
    print(f"Refreshed generation source snapshot from {source_url}.")
    print(f"- source snapshot: {args.source_json}")
    return 0


def command_compare(args: argparse.Namespace) -> int:
    baseline_spec = validate_openapi_spec(read_json(args.baseline), str(args.baseline))
    candidate_label = args.candidate_url or str(args.candidate)
    raw_candidate_spec = fetch_spec(args.candidate_url) if args.candidate_url else read_json(args.candidate)
    candidate_spec = validate_openapi_spec(raw_candidate_spec, candidate_label)
    report = build_report(
        baseline_spec=baseline_spec,
        candidate_spec=candidate_spec,
        baseline_label=args.baseline_label,
        candidate_label=args.candidate_label,
        source_url=args.source_url,
        max_diff_lines=args.max_diff_lines,
    )

    report_markdown = render_markdown_report(report)
    write_json(args.report_json, report)
    write_text(args.report_md, report_markdown)

    if args.candidate_operations:
        write_json(args.candidate_operations, build_snapshot(candidate_spec, args.source_url))

    if args.github_output:
        write_github_output(
            args.github_output,
            has_drift=report["has_drift"],
            has_actionable_drift=report["has_actionable_drift"],
            report_md=args.report_md,
            report_json=args.report_json,
        )

    if args.step_summary:
        write_text(args.step_summary, report_markdown)

    print(
        f"Compared baseline `{args.baseline_label}` to candidate `{args.candidate_label}`: "
        f"added={report['summary']['added']}, "
        f"removed={report['summary']['removed']}, "
        f"changed={report['summary']['changed']}, "
        f"actionable_changed={report['repo_summary']['actionable_changed']}, "
        "already_supported_changed="
        f"{report['repo_summary']['already_supported_changed']}"
    )
    print(f"- markdown report: {args.report_md}")
    print(f"- json report: {args.report_json}")

    if report["has_drift"] and args.fail_on_drift:
        return 2

    return 0


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="OpenRouter OpenAPI drift tooling")
    subparsers = parser.add_subparsers(dest="command", required=True)

    refresh = subparsers.add_parser(
        "refresh-baseline",
        help="Fetch the latest upstream spec and refresh the tracked baseline artifacts.",
    )
    refresh_source = refresh.add_mutually_exclusive_group()
    refresh_source.add_argument(
        "--source-url",
        default=UPSTREAM_OPENAPI_URL,
        help="OpenAPI URL used to refresh the tracked baseline.",
    )
    refresh_source.add_argument(
        "--source-file",
        type=Path,
        help="Local OpenAPI JSON file used to refresh the tracked baseline.",
    )
    refresh.add_argument(
        "--baseline-json",
        type=Path,
        required=True,
        help="Path where the raw tracked baseline JSON should be written.",
    )
    refresh.add_argument(
        "--operations-json",
        type=Path,
        required=True,
        help="Path where the normalized operations snapshot should be written.",
    )
    refresh.set_defaults(func=command_refresh_baseline)

    refresh_source = subparsers.add_parser(
        "refresh-source",
        help="Fetch and validate a full accepted source snapshot for future generation work.",
    )
    refresh_source_input = refresh_source.add_mutually_exclusive_group()
    refresh_source_input.add_argument(
        "--source-url",
        default=UPSTREAM_OPENAPI_URL,
        help="OpenAPI URL used to refresh the accepted source snapshot.",
    )
    refresh_source_input.add_argument(
        "--source-file",
        type=Path,
        help="Local OpenAPI JSON file used to refresh the accepted source snapshot.",
    )
    refresh_source.add_argument(
        "--source-json",
        type=Path,
        required=True,
        help="Path where the validated full source snapshot should be written.",
    )
    refresh_source.set_defaults(func=command_refresh_source)

    compare = subparsers.add_parser(
        "compare",
        help="Compare the tracked baseline against a candidate OpenAPI spec and emit reports.",
    )
    compare.add_argument(
        "--baseline",
        type=Path,
        required=True,
        help="Tracked baseline OpenAPI JSON file.",
    )
    compare_source = compare.add_mutually_exclusive_group(required=True)
    compare_source.add_argument(
        "--candidate",
        type=Path,
        help="Candidate OpenAPI JSON file to compare against the tracked baseline.",
    )
    compare_source.add_argument(
        "--candidate-url",
        help="Candidate OpenAPI URL to compare against the tracked baseline.",
    )
    compare.add_argument(
        "--source-url",
        default=UPSTREAM_OPENAPI_URL,
        help="Source URL associated with the compared candidate spec.",
    )
    compare.add_argument(
        "--baseline-label",
        default="tracked baseline",
        help="Human-readable label for the tracked baseline in reports.",
    )
    compare.add_argument(
        "--candidate-label",
        default="latest upstream",
        help="Human-readable label for the candidate spec in reports.",
    )
    compare.add_argument(
        "--report-md",
        type=Path,
        required=True,
        help="Markdown report output path.",
    )
    compare.add_argument(
        "--report-json",
        type=Path,
        required=True,
        help="JSON report output path.",
    )
    compare.add_argument(
        "--candidate-operations",
        type=Path,
        help="Optional output path for the candidate normalized operations snapshot.",
    )
    compare.add_argument(
        "--github-output",
        type=Path,
        help="Optional GitHub Actions output file path.",
    )
    compare.add_argument(
        "--step-summary",
        type=Path,
        help="Optional GitHub Actions step summary output path.",
    )
    compare.add_argument(
        "--max-diff-lines",
        type=int,
        default=60,
        help="Maximum diff lines to include for each changed operation in the markdown report.",
    )
    compare.add_argument(
        "--fail-on-drift",
        action="store_true",
        help="Exit with code 2 when drift is detected.",
    )
    compare.set_defaults(func=command_compare)

    return parser


def main() -> int:
    parser = build_parser()
    args = parser.parse_args()
    try:
        return args.func(args)
    except ValueError as exc:
        print(f"error: {exc}", file=sys.stderr)
        return 1


if __name__ == "__main__":
    sys.exit(main())