Skip to content

Commit 340f60a

Browse files
committed
refactor(codegen): name all anonymous variants
1 parent 7f8b620 commit 340f60a

2 files changed

Lines changed: 215 additions & 209 deletions

File tree

scripts/gen_schema.py

Lines changed: 114 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,123 @@ def _inline_model_ref(definition: str, *steps: tuple[str, int | None]) -> str:
5151
return ref
5252

5353

54-
# A few schema definitions share their name with a tagged SessionUpdate or
55-
# MultiSelectItems variant. Give the definition an internal base name so the public
56-
# tagged model can keep the established SDK name without shadowing a class.
54+
def _variant_model_map(
55+
definition: str,
56+
keyword: str,
57+
branch: str,
58+
names: tuple[str, ...],
59+
) -> dict[str, str]:
60+
return {_inline_model_ref(definition, (keyword, index), (branch, None)): name for index, name in enumerate(names)}
61+
62+
5763
MODEL_NAME_MAP = {
58-
f"#/$defs/{name}": f"{name}Base"
59-
for name in (
60-
"AvailableCommandsUpdate",
61-
"ConfigOptionUpdate",
62-
"CurrentModeUpdate",
63-
"SessionInfoUpdate",
64-
"StringMultiSelectItems",
65-
"UsageUpdate",
66-
)
64+
"#/$defs/AvailableCommandsUpdate": "AvailableCommandsUpdateBase",
65+
"#/$defs/ConfigOptionUpdate": "ConfigOptionUpdateBase",
66+
"#/$defs/CurrentModeUpdate": "CurrentModeUpdateBase",
67+
"#/$defs/SessionInfoUpdate": "SessionInfoUpdateBase",
68+
"#/$defs/StringMultiSelectItems": "StringMultiSelectItemsBase",
69+
"#/$defs/UsageUpdate": "UsageUpdateBase",
6770
}
68-
MODEL_NAME_MAP.update({
69-
_inline_model_ref("AgentResponse", ("anyOf", 0), ("object", None)): "AgentResponseMessage",
70-
_inline_model_ref("AgentResponse", ("anyOf", 1), ("object", None)): "AgentErrorMessage",
71-
_inline_model_ref("ClientResponse", ("anyOf", 0), ("object", None)): "ClientResponseMessage",
72-
_inline_model_ref("ClientResponse", ("anyOf", 1), ("object", None)): "ClientErrorMessage",
73-
_inline_model_ref("SetSessionConfigOptionRequest", ("anyOf", 0), ("object", None)): (
74-
"SetSessionConfigOptionBooleanRequest"
71+
for variant_map in (
72+
_variant_model_map("AgentResponse", "anyOf", "object", ("AgentResponseMessage", "AgentErrorMessage")),
73+
_variant_model_map("ClientResponse", "anyOf", "object", ("ClientResponseMessage", "ClientErrorMessage")),
74+
_variant_model_map("AuthMethod", "anyOf", "allOf", ("EnvVarAuthMethod", "TerminalAuthMethod")),
75+
_variant_model_map("McpServer", "anyOf", "allOf", ("HttpMcpServer", "SseMcpServer", "AcpMcpServer")),
76+
_variant_model_map(
77+
"SetSessionConfigOptionRequest",
78+
"anyOf",
79+
"object",
80+
("SetSessionConfigOptionBooleanRequest", "SetSessionConfigOptionSelectRequest"),
81+
),
82+
_variant_model_map(
83+
"ContentBlock",
84+
"oneOf",
85+
"allOf",
86+
(
87+
"TextContentBlock",
88+
"ImageContentBlock",
89+
"AudioContentBlock",
90+
"ResourceContentBlock",
91+
"EmbeddedResourceContentBlock",
92+
),
93+
),
94+
_variant_model_map(
95+
"ToolCallContent",
96+
"oneOf",
97+
"allOf",
98+
("ContentToolCallContent", "FileEditToolCallContent", "TerminalToolCallContent"),
99+
),
100+
_variant_model_map(
101+
"PlanUpdateContent",
102+
"oneOf",
103+
"allOf",
104+
("PlanUpdateItems", "PlanUpdateFile", "PlanUpdateMarkdown"),
75105
),
76-
_inline_model_ref("SetSessionConfigOptionRequest", ("anyOf", 1), ("object", None)): (
77-
"SetSessionConfigOptionSelectRequest"
106+
_variant_model_map(
107+
"NesSuggestion",
108+
"oneOf",
109+
"allOf",
110+
(
111+
"NesEditSuggestionVariant",
112+
"NesJumpSuggestionVariant",
113+
"NesRenameSuggestionVariant",
114+
"NesSearchAndReplaceSuggestionVariant",
115+
),
78116
),
117+
_variant_model_map(
118+
"SessionUpdate",
119+
"oneOf",
120+
"allOf",
121+
(
122+
"UserMessageChunk",
123+
"AgentMessageChunk",
124+
"AgentThoughtChunk",
125+
"ToolCallStart",
126+
"ToolCallProgress",
127+
"AgentPlanUpdate",
128+
"AgentPlanContentUpdate",
129+
"AgentPlanRemovedUpdate",
130+
"AvailableCommandsUpdate",
131+
"CurrentModeUpdate",
132+
"ConfigOptionUpdate",
133+
"SessionInfoUpdate",
134+
"UsageUpdate",
135+
),
136+
),
137+
_variant_model_map(
138+
"ElicitationFormMode",
139+
"anyOf",
140+
"allOf",
141+
("ElicitationFormSessionMode", "ElicitationFormRequestMode"),
142+
),
143+
_variant_model_map(
144+
"ElicitationUrlMode",
145+
"anyOf",
146+
"allOf",
147+
("ElicitationUrlSessionMode", "ElicitationUrlRequestMode"),
148+
),
149+
_variant_model_map(
150+
"ElicitationPropertySchema",
151+
"anyOf",
152+
"allOf",
153+
(
154+
"ElicitationStringPropertySchema",
155+
"ElicitationNumberPropertySchema",
156+
"ElicitationIntegerPropertySchema",
157+
"ElicitationBooleanPropertySchema",
158+
"ElicitationMultiSelectPropertySchema",
159+
),
160+
),
161+
):
162+
MODEL_NAME_MAP.update(variant_map)
163+
164+
MODEL_NAME_MAP.update({
165+
_inline_model_ref("RequestPermissionOutcome", ("oneOf", 0), ("object", None)): "DeniedOutcome",
166+
_inline_model_ref("RequestPermissionOutcome", ("oneOf", 1), ("allOf", None)): "AllowedOutcome",
79167
_inline_model_ref("CreateElicitationResponse", ("anyOf", 0), ("allOf", None)): "AcceptElicitationResponse",
80168
_inline_model_ref("CreateElicitationResponse", ("anyOf", 1), ("object", None)): "DeclineElicitationResponse",
81169
_inline_model_ref("CreateElicitationResponse", ("anyOf", 2), ("object", None)): "CancelElicitationResponse",
82170
_inline_model_ref("CreateElicitationResponse", ("anyOf", 3), ("object", None)): "OtherElicitationResponse",
83-
_inline_model_ref("ElicitationFormMode", ("anyOf", 0), ("allOf", None)): "ElicitationFormSessionMode",
84-
_inline_model_ref("ElicitationFormMode", ("anyOf", 1), ("allOf", None)): "ElicitationFormRequestMode",
85-
_inline_model_ref("ElicitationUrlMode", ("anyOf", 0), ("allOf", None)): "ElicitationUrlSessionMode",
86-
_inline_model_ref("ElicitationUrlMode", ("anyOf", 1), ("allOf", None)): "ElicitationUrlRequestMode",
87-
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 0), ("allOf", None)): ("ElicitationStringPropertySchema"),
88-
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 1), ("allOf", None)): ("ElicitationNumberPropertySchema"),
89-
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 2), ("allOf", None)): ("ElicitationIntegerPropertySchema"),
90-
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 3), ("allOf", None)): ("ElicitationBooleanPropertySchema"),
91-
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 4), ("allOf", None)): (
92-
"ElicitationMultiSelectPropertySchema"
93-
),
94171
_inline_model_ref("ElicitationPropertySchema", ("anyOf", 5), ("object", None)): ("ElicitationOtherPropertySchema"),
95172
_inline_model_ref("MultiSelectItems", ("anyOf", 0), ("allOf", None)): "StringMultiSelectItems",
96173
_inline_model_ref("MultiSelectItems", ("anyOf", 1), ("object", None)): "OtherMultiSelectItems",
@@ -153,46 +230,6 @@ def _inline_model_ref(definition: str, *steps: tuple[str, int | None]) -> str:
153230
"other",
154231
]
155232
156-
TextContentBlock = ContentBlockText
157-
ImageContentBlock = ContentBlockImage
158-
AudioContentBlock = ContentBlockAudio
159-
ResourceContentBlock = ContentBlockResourceLink
160-
EmbeddedResourceContentBlock = ContentBlockResource
161-
162-
HttpMcpServer = McpServerHttpModel
163-
SseMcpServer = McpServerSseModel
164-
AcpMcpServer = McpServerAcpModel
165-
DeniedOutcome = RequestPermissionOutcomeCancelled
166-
AllowedOutcome = RequestPermissionOutcomeSelected
167-
EnvVarAuthMethod = AuthMethodEnvVarModel
168-
TerminalAuthMethod = AuthMethodTerminalModel
169-
UserMessageChunk = SessionUpdateUserMessageChunk
170-
AgentMessageChunk = SessionUpdateAgentMessageChunk
171-
AgentThoughtChunk = SessionUpdateAgentThoughtChunk
172-
ToolCallStart = SessionUpdateToolCall
173-
ToolCallProgress = SessionUpdateToolCallUpdate
174-
AgentPlanUpdate = SessionUpdatePlan
175-
AgentPlanContentUpdate = SessionUpdatePlanUpdate
176-
AgentPlanRemovedUpdate = SessionUpdatePlanRemoved
177-
178-
_AvailableCommandsUpdate = AvailableCommandsUpdateBase
179-
_CurrentModeUpdate = CurrentModeUpdateBase
180-
_ConfigOptionUpdate = ConfigOptionUpdateBase
181-
_SessionInfoUpdate = SessionInfoUpdateBase
182-
_UsageUpdate = UsageUpdateBase
183-
AvailableCommandsUpdate = SessionUpdateAvailableCommandsUpdate
184-
CurrentModeUpdate = SessionUpdateCurrentModeUpdate
185-
ConfigOptionUpdate = SessionUpdateConfigOptionUpdate
186-
SessionInfoUpdate = SessionUpdateSessionInfoUpdate
187-
UsageUpdate = SessionUpdateUsageUpdate
188-
189-
PlanUpdateItems = PlanUpdateContentItems
190-
PlanUpdateFile = PlanUpdateContentFile
191-
PlanUpdateMarkdown = PlanUpdateContentMarkdown
192-
ContentToolCallContent = ToolCallContentContent
193-
FileEditToolCallContent = ToolCallContentDiff
194-
TerminalToolCallContent = ToolCallContentTerminal
195-
196233
CreateOtherElicitationRequest = Union[
197234
CreateOtherSessionElicitationRequest,
198235
CreateOtherRequestElicitationRequest,
@@ -224,13 +261,13 @@ def _inline_model_ref(definition: str, *steps: tuple[str, int | None]) -> str:
224261
ElicitationUrlRequestMode,
225262
]
226263
264+
_AvailableCommandsUpdate = AvailableCommandsUpdateBase
265+
_CurrentModeUpdate = CurrentModeUpdateBase
266+
_ConfigOptionUpdate = ConfigOptionUpdateBase
267+
_SessionInfoUpdate = SessionInfoUpdateBase
268+
_UsageUpdate = UsageUpdateBase
227269
_StringMultiSelectItems = StringMultiSelectItemsBase
228270
229-
NesEditSuggestionVariant = NesSuggestionEdit
230-
NesJumpSuggestionVariant = NesSuggestionJump
231-
NesRenameSuggestionVariant = NesSuggestionRename
232-
NesSearchAndReplaceSuggestionVariant = NesSuggestionSearchAndReplace
233-
234271
class Jsonrpc(Enum):
235272
field_2_0 = "2.0"
236273
""").strip()

0 commit comments

Comments
 (0)