-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathgen_schema.py
More file actions
385 lines (325 loc) · 13.3 KB
/
gen_schema.py
File metadata and controls
385 lines (325 loc) · 13.3 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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import ast
import re
import shutil
import subprocess
import sys
from collections.abc import Callable
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SCHEMA_DIR = ROOT / "schema"
SCHEMA_JSON = SCHEMA_DIR / "schema.json"
VERSION_FILE = SCHEMA_DIR / "VERSION"
SCHEMA_OUT = ROOT / "src" / "acp" / "schema.py"
BACKCOMPAT_MARKER = "# Backwards compatibility aliases"
# Pattern caches used when post-processing generated schema.
FIELD_DECLARATION_PATTERN = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\s*:")
DESCRIPTION_PATTERN = re.compile(
r"description\s*=\s*(?P<prefix>[rRbBuU]*)?(?P<quote>'''|\"\"\"|'|\")(?P<value>.*?)(?P=quote)",
re.DOTALL,
)
# Map of numbered classes produced by datamodel-code-generator to descriptive names.
# Keep this in sync with the Rust/TypeScript SDK nomenclature.
RENAME_MAP: dict[str, str] = {
"AvailableCommandInput1": "CommandInputHint",
"ContentBlock1": "TextContentBlock",
"ContentBlock2": "ImageContentBlock",
"ContentBlock3": "AudioContentBlock",
"ContentBlock4": "ResourceContentBlock",
"ContentBlock5": "EmbeddedResourceContentBlock",
"McpServer1": "HttpMcpServer",
"McpServer2": "SseMcpServer",
"McpServer3": "StdioMcpServer",
"RequestPermissionOutcome1": "DeniedOutcome",
"RequestPermissionOutcome2": "AllowedOutcome",
"SessionUpdate1": "UserMessageChunk",
"SessionUpdate2": "AgentMessageChunk",
"SessionUpdate3": "AgentThoughtChunk",
"SessionUpdate4": "ToolCallStart",
"SessionUpdate5": "ToolCallProgress",
"SessionUpdate6": "AgentPlanUpdate",
"SessionUpdate7": "AvailableCommandsUpdate",
"SessionUpdate8": "CurrentModeUpdate",
"ToolCallContent1": "ContentToolCallContent",
"ToolCallContent2": "FileEditToolCallContent",
"ToolCallContent3": "TerminalToolCallContent",
}
ENUM_LITERAL_MAP: dict[str, tuple[str, ...]] = {
"PermissionOptionKind": (
"allow_once",
"allow_always",
"reject_once",
"reject_always",
),
"PlanEntryPriority": ("high", "medium", "low"),
"PlanEntryStatus": ("pending", "in_progress", "completed"),
"StopReason": ("end_turn", "max_tokens", "max_turn_requests", "refusal", "cancelled"),
"ToolCallStatus": ("pending", "in_progress", "completed", "failed"),
"ToolKind": ("read", "edit", "delete", "move", "search", "execute", "think", "fetch", "switch_mode", "other"),
}
FIELD_TYPE_OVERRIDES: tuple[tuple[str, str, str, bool], ...] = (
("PermissionOption", "kind", "PermissionOptionKind", False),
("PlanEntry", "priority", "PlanEntryPriority", False),
("PlanEntry", "status", "PlanEntryStatus", False),
("PromptResponse", "stopReason", "StopReason", False),
("ToolCallUpdate", "kind", "ToolKind", True),
("ToolCallUpdate", "status", "ToolCallStatus", True),
("ToolCallProgress", "kind", "ToolKind", True),
("ToolCallProgress", "status", "ToolCallStatus", True),
("ToolCallStart", "kind", "ToolKind", True),
("ToolCallStart", "status", "ToolCallStatus", True),
("ToolCall", "kind", "ToolKind", True),
("ToolCall", "status", "ToolCallStatus", True),
)
DEFAULT_VALUE_OVERRIDES: tuple[tuple[str, str, str], ...] = (
("AgentCapabilities", "mcpCapabilities", "McpCapabilities(http=False, sse=False)"),
(
"AgentCapabilities",
"promptCapabilities",
"PromptCapabilities(audio=False, embeddedContext=False, image=False)",
),
("ClientCapabilities", "fs", "FileSystemCapability(readTextFile=False, writeTextFile=False)"),
("ClientCapabilities", "terminal", "False"),
(
"InitializeRequest",
"clientCapabilities",
"ClientCapabilities(fs=FileSystemCapability(readTextFile=False, writeTextFile=False), terminal=False)",
),
(
"InitializeResponse",
"agentCapabilities",
"AgentCapabilities(loadSession=False, mcpCapabilities=McpCapabilities(http=False, sse=False), promptCapabilities=PromptCapabilities(audio=False, embeddedContext=False, image=False))",
),
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate src/acp/schema.py from the ACP JSON schema.")
parser.add_argument(
"--format",
dest="format_output",
action="store_true",
help="Format generated files with 'uv run ruff format'.",
)
parser.add_argument(
"--no-format",
dest="format_output",
action="store_false",
help="Disable formatting with ruff.",
)
parser.set_defaults(format_output=True)
return parser.parse_args()
def main() -> None:
args = parse_args()
generate_schema(format_output=args.format_output)
def generate_schema(*, format_output: bool = True) -> None:
if not SCHEMA_JSON.exists():
print(
"Schema file missing. Ensure schema/schema.json exists (run gen_all.py --version to download).",
file=sys.stderr,
)
sys.exit(1)
cmd = [
sys.executable,
"-m",
"datamodel_code_generator",
"--input",
str(SCHEMA_JSON),
"--input-file-type",
"jsonschema",
"--output",
str(SCHEMA_OUT),
"--target-python-version",
"3.12",
"--collapse-root-models",
"--output-model-type",
"pydantic_v2.BaseModel",
"--use-annotated",
]
subprocess.check_call(cmd) # noqa: S603
warnings = rename_types(SCHEMA_OUT)
for warning in warnings:
print(f"Warning: {warning}", file=sys.stderr)
if format_output:
format_with_ruff(SCHEMA_OUT)
def rename_types(output_path: Path) -> list[str]:
if not output_path.exists():
raise RuntimeError(f"Generated schema not found at {output_path}") # noqa: TRY003
content = output_path.read_text(encoding="utf-8")
header_lines = ["# Generated from schema/schema.json. Do not edit by hand."]
if VERSION_FILE.exists():
ref = VERSION_FILE.read_text(encoding="utf-8").strip()
if ref:
header_lines.append(f"# Schema ref: {ref}")
existing_header = re.match(r"(#.*\n)+", content)
if existing_header:
content = content[existing_header.end() :]
content = content.lstrip("\n")
marker_index = content.find(BACKCOMPAT_MARKER)
if marker_index != -1:
content = content[:marker_index].rstrip()
for old, new in sorted(RENAME_MAP.items(), key=lambda item: len(item[0]), reverse=True):
pattern = re.compile(rf"\b{re.escape(old)}\b")
content = pattern.sub(new, content)
leftover_class_pattern = re.compile(r"^class (\w+\d+)\(", re.MULTILINE)
leftover_classes = sorted(set(leftover_class_pattern.findall(content)))
header_block = "\n".join(header_lines) + "\n\n"
content = _apply_field_overrides(content)
content = _apply_default_overrides(content)
content = _add_description_comments(content)
alias_lines = [f"{old} = {new}" for old, new in sorted(RENAME_MAP.items())]
alias_block = BACKCOMPAT_MARKER + "\n" + "\n".join(alias_lines) + "\n"
content = _inject_enum_aliases(content)
content = header_block + content.rstrip() + "\n\n" + alias_block
if not content.endswith("\n"):
content += "\n"
output_path.write_text(content, encoding="utf-8")
warnings: list[str] = []
if leftover_classes:
warnings.append(
"Unrenamed schema models detected: "
+ ", ".join(leftover_classes)
+ ". Update RENAME_MAP in scripts/gen_schema.py."
)
return warnings
def _apply_field_overrides(content: str) -> str:
for class_name, field_name, new_type, optional in FIELD_TYPE_OVERRIDES:
if optional:
pattern = re.compile(
rf"(class {class_name}\(BaseModel\):.*?\n\s+{field_name}:\s+Annotated\[\s*)Optional\[str],",
re.DOTALL,
)
content, count = pattern.subn(rf"\1Optional[{new_type}],", content)
else:
pattern = re.compile(
rf"(class {class_name}\(BaseModel\):.*?\n\s+{field_name}:\s+Annotated\[\s*)str,",
re.DOTALL,
)
content, count = pattern.subn(rf"\1{new_type},", content)
if count == 0:
print(
f"Warning: failed to apply type override for {class_name}.{field_name} -> {new_type}",
file=sys.stderr,
)
return content
def _apply_default_overrides(content: str) -> str:
for class_name, field_name, replacement in DEFAULT_VALUE_OVERRIDES:
class_pattern = re.compile(
rf"(class {class_name}\(BaseModel\):)(.*?)(?=\nclass |\Z)",
re.DOTALL,
)
def replace_block(
match: re.Match[str],
_field_name: str = field_name,
_replacement: str = replacement,
_class_name: str = class_name,
) -> str:
header, block = match.group(1), match.group(2)
field_patterns: tuple[tuple[re.Pattern[str], Callable[[re.Match[str]], str]], ...] = (
(
re.compile(
rf"(\n\s+{_field_name}:.*?\]\s*=\s*)([\s\S]*?)(?=\n\s{{4}}[A-Za-z_]|$)",
re.DOTALL,
),
lambda m, _rep=_replacement: m.group(1) + _rep,
),
(
re.compile(
rf"(\n\s+{_field_name}:[^\n]*=)\s*([^\n]+)",
re.MULTILINE,
),
lambda m, _rep=_replacement: m.group(1) + " " + _rep,
),
)
for pattern, replacer in field_patterns:
new_block, count = pattern.subn(replacer, block, count=1)
if count:
return header + new_block
print(
f"Warning: failed to override default for {_class_name}.{_field_name}",
file=sys.stderr,
)
return match.group(0)
content, count = class_pattern.subn(replace_block, content, count=1)
if count == 0:
print(
f"Warning: class {class_name} not found for default override on {field_name}",
file=sys.stderr,
)
return content
def _add_description_comments(content: str) -> str:
lines = content.splitlines()
new_lines: list[str] = []
index = 0
while index < len(lines):
line = lines[index]
stripped = line.lstrip()
indent = len(line) - len(stripped)
if indent == 4 and FIELD_DECLARATION_PATTERN.match(stripped or ""):
block_lines, next_index = _collect_field_block(lines, index, indent)
block_text = "\n".join(block_lines)
description = _extract_description(block_text)
if description:
indent_str = " " * indent
comment_lines = [
f"{indent_str}# {comment_line}" if comment_line else f"{indent_str}#"
for comment_line in description.splitlines()
]
if comment_lines:
new_lines.extend(comment_lines)
new_lines.extend(block_lines)
index = next_index
continue
new_lines.append(line)
index += 1
return "\n".join(new_lines)
def _collect_field_block(lines: list[str], start: int, indent: int) -> tuple[list[str], int]:
block: list[str] = []
index = start
while index < len(lines):
current_line = lines[index]
current_indent = len(current_line) - len(current_line.lstrip())
if index != start and current_line.strip() and current_indent <= indent:
break
block.append(current_line)
index += 1
return block, index
def _extract_description(block_text: str) -> str | None:
match = DESCRIPTION_PATTERN.search(block_text)
if not match:
return None
prefix = match.group("prefix") or ""
quote = match.group("quote")
value = match.group("value")
literal = f"{prefix}{quote}{value}{quote}"
# datamodel-code-generator emits standard string literals, but fall back to raw text on parse errors.
try:
parsed = ast.literal_eval(literal)
except (SyntaxError, ValueError):
return value.replace("\\n", "\n")
if isinstance(parsed, str):
return parsed
return str(parsed)
def _inject_enum_aliases(content: str) -> str:
enum_lines = [
f"{name} = Literal[{', '.join(repr(value) for value in values)}]" for name, values in ENUM_LITERAL_MAP.items()
]
if not enum_lines:
return content
block = "\n".join(enum_lines) + "\n\n"
class_index = content.find("\nclass ")
if class_index == -1:
return content
insertion_point = class_index + 1 # include leading newline
return content[:insertion_point] + block + content[insertion_point:]
def format_with_ruff(file_path: Path) -> None:
uv_executable = shutil.which("uv")
if uv_executable is None:
print("Warning: 'uv' executable not found; skipping formatting.", file=sys.stderr)
return
try:
subprocess.check_call([uv_executable, "run", "ruff", "format", str(file_path)]) # noqa: S603
except (FileNotFoundError, subprocess.CalledProcessError) as exc: # pragma: no cover - best effort
print(f"Warning: failed to format {file_path}: {exc}", file=sys.stderr)
if __name__ == "__main__":
main()