Hapti streams what happens with Server-Sent Events. It is the fastest way to show progress in your app, and the libraries use it to return as soon as a task ends.
Follow one task
for await (const e of hapti.tasks.stream('tsk_...')) {
if (e.event === 'step') console.log(e.data.summary);
if (e.event === 'task') console.log('status', e.data.status);
}for e in hapti.tasks.stream("tsk_..."):
if e["event"] == "step":
print(e["data"].get("summary"))
if e["event"] == "task":
print("status", e["data"]["status"])curl -N https://api.hapti.dev/v1/tasks/tsk_.../stream \
-H "Authorization: Bearer $HAPTI_KEY"| Event | Data | When |
|---|---|---|
task | The full task | First, then each time its status, question, result or price changes |
step | One action of the agent, with a summary | Each time the agent acts |
screenshot | n and the url of the image | Each time a screenshot is saved |
done | id and final status | Once, at the end. The stream then closes |
reconnect | reason | After 30 minutes: connect again |
Each step carries an id. If the connection drops, connect again with the header Last-Event-ID set to the last id you received, and the stream resumes right after it, without gaps or repeats. The libraries do this for you.
A comment line : ping is sent every 15 seconds so proxies keep the connection open.
Wait for the end with the libraries
tasks.wait and tasks.run follow the stream and return the moment the task is over. They fall back to asking for the task at intervals when a stream cannot be opened.
const task = await hapti.tasks.run(
{ url: 'https://portal.example.com', goal: 'Download my last statement.' },
{ onStep: (s) => progress(s.summary) },
);Follow every task
GET /v1/stream sends a task event each time any task of the key mode changes state. Use it to keep a dashboard up to date.
curl -N https://api.hapti.dev/v1/stream -H "Authorization: Bearer $HAPTI_KEY"