feat(gateway): generate shared TypeScript contracts

Define gateway payloads as Python TypedDict contracts and generate the shared
TypeScript surface through ts-type during shared, TUI, and desktop workflows.
This commit is contained in:
ethernet 2026-07-23 01:09:34 -04:00
parent 75afaf46da
commit 2cdfe35429
12 changed files with 166 additions and 49 deletions

View file

@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Generate TypeScript gateway contracts from ``tui_gateway.contracts``."""
from __future__ import annotations
import os
from pathlib import Path
from tempfile import NamedTemporaryFile
import ts_type as ts
from tui_gateway.contracts import (
GatewayMcpServerStatus,
GatewayProjectInfo,
GatewaySessionRuntimeInfo,
GatewayUsage,
)
ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "apps/shared/src/generated/gateway.ts"
def main() -> None:
for contract in (
GatewayMcpServerStatus,
GatewayProjectInfo,
GatewayUsage,
GatewaySessionRuntimeInfo,
):
ts.generator.add(contract, "gateway", contract.__name__)
source = ts.generator.render()["gateway"]
output = "// Generated by scripts/generate_gateway_types.py. Do not edit.\n\n" + source + "\n"
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
# Workspace checks may run concurrently. Replacing an already-complete
# temporary file means a typecheck always observes either version, never a
# partially-written module.
with NamedTemporaryFile("w", encoding="utf-8", dir=OUTPUT.parent, delete=False) as file:
file.write(output)
temp_path = Path(file.name)
os.replace(temp_path, OUTPUT)
if __name__ == "__main__":
main()