import json
import os
import httpx
ENGINE_URL = os.environ.get("ENGINE_URL", "http://localhost:8000")
ENGINE_KEY = os.environ["ENGINE_KEY"]
def execute(message: str, task_id: str) -> None:
"""Stream a response from the Engine and print text deltas."""
headers = {
"X-Engine-Key": ENGINE_KEY,
"Content-Type": "application/json",
}
payload = {"message": message, "task_id": task_id}
with httpx.Client(timeout=None) as client:
with client.stream("POST", f"{ENGINE_URL}/execute",
json=payload, headers=headers) as resp:
resp.raise_for_status()
for line in resp.iter_lines():
if not line or not line.startswith("data:"):
continue
data = json.loads(line[len("data:"):].strip())
if "text" in data:
print(data["text"], end="", flush=True)
print() # final newline
if __name__ == "__main__":
execute("Say hello in one short sentence.", task_id="demo-001")