import json
import os
import httpx
BASE = "https://haico.gr/api"
TOKEN = os.environ["HAICO_TOKEN"]
HEADERS = {"Authorization": f"Bearer {TOKEN}"}
def run_turn(client: httpx.Client, query: str, thread_id: str | None = None):
"""Send one message to the agent and consume the SSE stream.
Returns the thread id, the reasoning steps, and any artifacts.
"""
steps, artifacts = [], []
with client.stream(
"POST",
f"{BASE}/query/stream_steps/sse",
headers={**HEADERS, "Content-Type": "application/json"},
json={"query": query, "thread_id": thread_id},
timeout=None, # the agent may think for a while
) as response:
response.raise_for_status()
for line in response.iter_lines():
# Only "data:" lines carry events. Lines starting with ":" are
# keep-alive comments. The event kind is the "type" field inside
# the JSON, not an SSE "event:" line.
if not line.startswith("data:"):
continue
data = json.loads(line[len("data:"):].strip())
kind = data.get("type")
if kind == "conversation_info":
thread_id = data["thread_id"]
elif kind == "step":
steps.append(data["step"])
elif kind == "artifact":
artifacts.append(data["artifact"])
elif kind == "error":
raise RuntimeError(f"agent turn failed: {data['error']}")
elif kind == "complete":
break
return thread_id, steps, artifacts
with httpx.Client() as client:
# Confirm the token works before spending anything.
me = client.get(f"{BASE}/auth/me", headers=HEADERS)
me.raise_for_status()
print("authenticated as", me.json()["username"])
thread_id, steps, artifacts = run_turn(
client, "Draft a one-paragraph summary of the Apollo programme."
)
print(f"thread {thread_id}: {len(steps)} steps, {len(artifacts)} artifacts")
# A second turn on the same thread continues the co-construction.
thread_id, *_ = run_turn(client, "Now make it two sentences shorter.", thread_id)
# Read back the shared workspace. The document endpoint returns JSON null
# (with HTTP 200) until the agent has actually written one, so a turn that
# only answered in chat leaves this empty.
document = client.get(f"{BASE}/workspace/{thread_id}/document", headers=HEADERS).json()
print(document["content"] if document else "(no document written yet)")