Files
VoiceStudio/scripts/export_cli_contract.py
T
mostafa-ismail-2004 4708ae9a51 feat(cli): add the voicestudio terminal command
Add a terminal command that drives the desktop app's local backend.

- The desktop app owns the backend. When no backend answers, the CLI
  starts the installed app in the background (no window); it publishes
  its address in backend.json and quits after 10 idle minutes. Opening
  the app later shows a window in the same process, so a terminal
  session and the GUI never run two backends.
- The backend reports activity (requests, jobs, downloads, installs,
  calls) at /system/activity so idle shutdown never interrupts work, and
  holds a kernel lock on its data directory so a second backend fails
  fast with an actionable message.
- `voicestudio stop` stops a background app; commands that only report
  state never start it, and `status` exits 3 when it is not running.
- Output is readable tables and fields chosen from each result's shape;
  `--json` prints the raw data and piped output is tab-separated.
- Tab completion for bash, zsh, fish and PowerShell
  (`voicestudio completion <shell>`), without building the command tree.
- `voicestudio update` opens the app's updater; the CLI itself makes no
  network requests.
- Settings → General shows the terminal command and can add it to PATH
  on request; shell profiles are only edited with that consent.
2026-11-05 18:18:58 +02:00

46 lines
1.8 KiB
Python

"""Export the CLI request contract from the already generated backend OpenAPI.
Run after scripts/export_openapi.py when backend request contracts change.
"""
import json
from pathlib import Path
import re
import sys
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / 'cli'))
from voicestudio_cli.registry import COMMANDS
def build_contract():
schema = json.loads((ROOT / 'mobile/contract/openapi.json').read_text())
result = {}
def shape(path):
return re.sub(r'\{[^}]+\}', '{}', path)
for command in COMMANDS:
if command.route.startswith('/audio/') and command.name == 'history download':
continue # Starlette's static mount is intentionally absent from OpenAPI.
if command.route == '/openapi.json':
continue
path = next(p for p in schema['paths'] if shape(p) == shape(command.route))
operation = schema['paths'][path][command.method.lower()]
content = operation.get('requestBody', {}).get('content', {})
encoding = next(iter(content), '')
body = content.get(encoding, {}).get('schema', {})
if '$ref' in body:
body = schema['components']['schemas'][body['$ref'].rsplit('/', 1)[-1]]
result[command.name] = {
'encoding': encoding,
'required': body.get('required', []),
'fields': list(body.get('properties', {})),
'query': [p['name'] for p in operation.get('parameters', []) if p['in'] == 'query'],
'required_query': [p['name'] for p in operation.get('parameters', [])
if p['in'] == 'query' and p.get('required')],
}
return result
if __name__ == '__main__':
target = ROOT / 'cli/voicestudio_cli/contracts.json'
target.write_text(json.dumps(build_contract(), indent=2, sort_keys=True) + '\n')