Ask a persona a question
The Personaas app asks a persona in three calls: it creates a conversation, opens the conversation's live stream, then sends the question. The answer arrives on the stream while the persona writes it, which can take from a few seconds to several minutes. Your app does the same.
1. Create a conversation
curl -X POST "$PERSONAAS_BASE_URL/api/v1/activity/" \
-H "x-personaas-key: $PERSONAAS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "persona_id": 12, "task_id": 40, "name": "Leave question" }'
{ "activity_id": "0b6f6a0e-…", "message": "Activity created successfully", "is_sensitive": false }
| Field | Required | Notes |
|---|---|---|
persona_id | yes | From List personas and tasks |
task_id | yes | A task of that persona |
name | no | Title of the conversation, shown in the user's history |
Save activity_id. Every later call for this conversation uses it: the
stream, the question, follow-ups, history and files. This path is always
/api/v1.
2. Open the stream
Open it before you send the question, so you do not miss the start of the answer, and keep it open:
curl -N "$PERSONAAS_BASE_URL/api/v2/activity/$ACTIVITY_ID/stream" \
-H "x-personaas-key: $PERSONAAS_API_KEY"
This is a server-sent events stream. Its path is /api/v2 on both engines.
If opening it fails, wait a second and open it again; the Personaas app does
the same.
3. Send the question
curl -X POST "$PERSONAAS_BASE_URL/api/$PERSONAAS_ENGINE/activity/$ACTIVITY_ID/message" \
-H "x-personaas-key: $PERSONAAS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "input": "How many leave days can I carry over?" }'
The path uses your organisation's engine, v1 or v3; see Engines.
Any 2xx means the question was accepted: v1 answers 200, v3 answers
202. The answer is not in this response; it comes on the stream.
| Field | Type | Required | Notes |
|---|---|---|---|
input | string | yes | What the user asks |
context | string | no | Extra background for the persona that is not part of the user's question, for example "The user is on the Leave page" |
additional_information | object | no | Extra structured data for the persona |
files | [{"filename": "…"}] | no | Files already uploaded to this conversation. See Send and receive files |
4. Read the answer from the stream
Text arrives in small pieces. Append each piece as it comes, and stop when the run is finished:
event:RunContent
data:{"content":"You can carry ", ...}
event:WorkflowCompleted
data:{"content":"You can carry over up to 10 days…", "status":"completed", ...}
delta,RunContentorTeamRunContent: a piece of the answer incontent. Append it.WorkflowCompleted: finished.contentholds the whole answer.- Failure events and
stream_endalso matter, and some events differ between engines. Stream the answer has the full rules and ready-to-use code.
When the run is finished, close the stream, then read the conversation's
messages and show the last assistant message as the final answer
(Continue a conversation).
The text built from the stream is a live preview.
Without a stream: check the conversation
If your app cannot keep a connection open, for example a script or a scheduled job, skip step 2. After sending, check the conversation every few seconds:
curl "$PERSONAAS_BASE_URL/api/v1/activity/$ACTIVITY_ID" \
-H "x-personaas-key: $PERSONAAS_API_KEY"
# → { "id": "0b6f6a0e-…", "status": "in_progress", … }
curl "$PERSONAAS_BASE_URL/api/v1/activity/$ACTIVITY_ID/messages" \
-H "x-personaas-key: $PERSONAAS_API_KEY"
The run is finished when status is no longer in_progress and an
assistant message comes after your question. That message's content is the
answer, usually Markdown. Check both: right after you send, status can still
show the state from before your question (a new conversation starts as
completed) until the persona picks the question up.
status | What it means |
|---|---|
in_progress | Still working. Check again in a few seconds |
completed | Done, once your answer is in the messages |
failed | The run failed. Show a plain message |
killed | The run was stopped |
needs_input, pending_approval, planning | The task needs the user in Personaas, for example to approve a plan. Tell the user to open the conversation there |
Wait 2 to 3 seconds between checks and stop after a reasonable limit, for example 10 minutes.
Failures
| Status | Meaning | What to do |
|---|---|---|
400 with "encryption key" in detail | The task is marked sensitive | Apps cannot use sensitive tasks yet. Hide tasks where sensitive is true |
404 | Persona, task or conversation not found | Re-list personas (List personas and tasks) |
503 on send | v3 only: no free runner | If detail is "No runner available", send the same message once on v1; the stream stays the same (Engines). Otherwise check the conversation before sending again |
other 4xx | Read detail | For example the user's usage allowance is used up. Show detail |
Example code
In JavaScript, as an illustration. streamAnswer is the function from
Stream the answer: it opens the stream, sends
the question and reads the answer.
// engine: the organisation's engine, "v1" or "v3" (see Engines)
async function askPersona(baseUrl, apiKey, engine, personaId, taskId, input, onText) {
const headers = { "x-personaas-key": apiKey, "Content-Type": "application/json" };
// 1. create the conversation
const res = await fetch(`${baseUrl}/api/v1/activity/`, {
method: "POST",
headers,
body: JSON.stringify({ persona_id: personaId, task_id: taskId }),
});
const created = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(created.detail || `Personaas returned ${res.status}`);
// 2-4. open the stream, send the question, read the answer
const { result, text } = await streamAnswer(baseUrl, apiKey, engine, created.activity_id, input, onText);
// keep activity_id for follow-ups; then show the stored messages as the final answer
return { activityId: created.activity_id, result, text };
}
Mistakes to avoid
- Sending before the stream is open. The start of the answer can be lost.
- Sending the question again because the answer is slow. A new message stops the run in progress and starts over. Keep waiting.
- Creating a new conversation for a follow-up. The persona has no memory
of the first question there. Send follow-ups to the same
activity_id(Continue a conversation). - Checking the conversation in a tight loop. Wait a few seconds between checks.