const response = await fetch("https://api.praxa.io/v1/execute", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PRAXA_SK_KEY}`,
"Content-Type": "application/json",
Accept: "text/event-stream",
"X-AI-Platform-Version": "v1",
"Idempotency-Key": crypto.randomUUID(),
},
body: JSON.stringify({
task: "Research three competitors and draft a one-page comparison.",
context: { notes: "Focus on pricing and support SLAs." },
tools: { allow: ["web_search", "document_write"] },
policy: {
riskCeiling: "human_approval",
budgetCap: { amountMicros: 2_000_000, currency: "USD" },
toolAllowlist: ["web_search", "document_write"],
},
delivery: { mode: "turn" },
}),
});
if (!response.ok || !response.body) {
throw new Error(`Execute request failed: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
for (const line of buffer.split("\n")) {
if (!line.startsWith("data:")) continue;
const payload = line.slice(5).trim();
if (payload === "[DONE]") continue;
const event = JSON.parse(payload);
if (event.type === "approval.required") {
// Show event.summary to a human, then resolve it:
// POST /v1/runs/:id/approve { "action_digest": event.action_digest, "decision": "approved" }
}
if (event.type === "response.final" || event.type === "request.failed") {
// Terminal event. Stop reading.
}
}
}