|
41 | 41 | PythonVersion, |
42 | 42 | ) |
43 | 43 |
|
44 | | -_THIS_DIR = Path(__file__).parent |
| 44 | + |
| 45 | +_THIS_FILE = Path(__file__) |
| 46 | +_THIS_DIR = _THIS_FILE.parent |
45 | 47 | _LMSJS_DIR = _THIS_DIR / "lmstudio-js" |
46 | 48 | _EXPORTER_DIR = _LMSJS_DIR / "packages/lms-json-schema" |
47 | 49 | _SCHEMA_DIR = _EXPORTER_DIR / "schemas" |
|
51 | 53 | _TEMPLATE_DIR = _THIS_DIR / "_templates" |
52 | 54 | _MODEL_DIR = _THIS_DIR.parent / "src/lmstudio/_sdk_models" |
53 | 55 | _MODEL_PATH = _MODEL_DIR / "__init__.py" |
| 56 | +_LMSJS_PORTS_PATH = _LMSJS_DIR / "packages/lms-common/src/apiServerPorts.ts" |
| 57 | +_PY_PORTS_PATH = _THIS_DIR.parent / "src/lmstudio/_api_server_ports.py" |
| 58 | + |
| 59 | +GENERATED_SOURCE_HEADER = f"""\ |
| 60 | +# Automatically generated by {_THIS_FILE.name}. DO NOT EDIT THIS FILE! |
| 61 | +""".splitlines() |
54 | 62 |
|
55 | 63 | # The following schemas are not actually used anywhere, |
56 | 64 | # so they're excluded to avoid any conflicts with automatically |
@@ -630,13 +638,40 @@ def _generate_data_model_from_json_schema() -> None: |
630 | 638 | if line.startswith("class"): |
631 | 639 | break |
632 | 640 | updated_source_lines[idx:idx] = lines_to_insert |
| 641 | + # Insert auto-generated code header |
| 642 | + updated_source_lines[0:0] = GENERATED_SOURCE_HEADER |
633 | 643 | _MODEL_PATH.write_text("\n".join(updated_source_lines) + "\n") |
634 | 644 |
|
635 | 645 |
|
| 646 | +def _sync_default_port_list() -> None: |
| 647 | + """Copy the list of default ports to check for the local API server.""" |
| 648 | + print("Extracting default port list...") |
| 649 | + print(f" Reading {_LMSJS_PORTS_PATH}") |
| 650 | + lmsjs_source = _LMSJS_PORTS_PATH.read_text() |
| 651 | + START_PORTS = "apiServerPorts = [" |
| 652 | + END_PORTS = "];" |
| 653 | + _, found, remaining_text = lmsjs_source.partition(START_PORTS) |
| 654 | + if not found: |
| 655 | + raise RuntimeError(f"Failed to find {START_PORTS} in {lmsjs_source}") |
| 656 | + ports_text, suffix_found, _ = remaining_text.partition(END_PORTS) |
| 657 | + if not suffix_found: |
| 658 | + raise RuntimeError(f"Failed to find {END_PORTS} in {remaining_text}") |
| 659 | + default_ports = [*map(int, ports_text.split(","))] |
| 660 | + if not default_ports: |
| 661 | + raise RuntimeError("Failed to extract any default ports") |
| 662 | + py_source_lines = [ |
| 663 | + *GENERATED_SOURCE_HEADER, |
| 664 | + f"default_api_ports = ({','.join(map(str, default_ports))})", |
| 665 | + ] |
| 666 | + print(f" Writing {_PY_PORTS_PATH}") |
| 667 | + _PY_PORTS_PATH.write_text("\n".join(py_source_lines) + "\n") |
| 668 | + |
| 669 | + |
636 | 670 | def _main() -> None: |
637 | 671 | if sys.argv[1:] == ["--regen-schema"] or not _SCHEMA_PATH.exists(): |
638 | 672 | _export_zod_schemas_to_json_schema() |
639 | 673 | _generate_data_model_from_json_schema() |
| 674 | + _sync_default_port_list() |
640 | 675 | print("Running automatic formatter after data model code generation") |
641 | 676 | subprocess.run(["tox", "-e", "format"]) |
642 | 677 |
|
|
0 commit comments