From 6357ff6a5807be578515c02ad0965ca4b2b3c7b6 Mon Sep 17 00:00:00 2001 From: mikael-lovqvists-claude-agent Date: Sun, 7 Jun 2026 07:37:42 +0000 Subject: [PATCH] Add POST /command endpoint with terminate support (closes #2) Sends {"status": "ok"} before shutting down so the caller gets a clean response. Shutdown runs in a daemon thread to avoid deadlocking the handler. Adds examples/terminate.mjs as a Node.js usage example. Co-Authored-By: Claude Sonnet 4.6 --- chatterbox-server.py | 9 +++++++++ examples/terminate.mjs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 examples/terminate.mjs diff --git a/chatterbox-server.py b/chatterbox-server.py index 46af4aa..efde2a4 100755 --- a/chatterbox-server.py +++ b/chatterbox-server.py @@ -6,6 +6,7 @@ Endpoints: POST /speak {"text": "...", "temperature": 0.8, "top_p": 0.95, "audio_prompt": "/path.wav"} POST /chime {"path": "/path/to/file.wav"} POST /preload {"path": "/path/to/file.wav"} + POST /command {"command": "terminate"} All endpoints return {"status": "ok"} or {"status": "error", "message": "..."}. Responses are sent after audio is queued for playback (not after playback finishes). @@ -237,6 +238,14 @@ class Handler(BaseHTTPRequestHandler): except Exception as e: self.send_json({'status': 'error', 'message': str(e)}, 500) + elif self.path == '/command': + command = req.get('command', '') + if command == 'terminate': + self.send_json({'status': 'ok'}) + threading.Thread(target=server.shutdown, daemon=True).start() + else: + self.send_json({'status': 'error', 'message': f'unknown command: {command}'}, 400) + else: self.send_json({'status': 'error', 'message': 'not found'}, 404) diff --git a/examples/terminate.mjs b/examples/terminate.mjs new file mode 100644 index 0000000..800f91e --- /dev/null +++ b/examples/terminate.mjs @@ -0,0 +1,16 @@ +// Gracefully terminate the Chatterbox TTS server. +// Usage: node terminate.mjs + +const PORT = process.env.TTS_PORT ?? '11500' + +const res = await fetch(`http://localhost:${PORT}/command`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ command: 'terminate' }), +}) + +const data = await res.json() +if (data.status !== 'ok') { + console.error('error:', data.message) + process.exit(1) +} -- 2.49.1