const base = "https://www.ghosty.studio/api/v2/me";const h = { Authorization: `Bearer ${token}`, "Content-Type": "application/json" };
// 1. open a conversationconst { id: sessionId } = await (await fetch(`${base}/agents/${agentId}/conversations`, { method: "POST", headers: h })).json();
// 2. subscribe to events BEFORE sending (the stream replays what was already emitted, so the order isn't critical)const es = new EventSource(`${base}/agents/${agentId}/conversations/${sessionId}/events`); // add the bearer with a polyfill that accepts headerses.addEventListener("chunk", (e) => process.stdout.write(JSON.parse(e.data).text));es.addEventListener("done", () => es.close());
// 3. send the message: responds 202 on acceptance, not on completionawait fetch(`${base}/agents/${agentId}/conversations/${sessionId}/messages`, { method: "POST", headers: h, body: JSON.stringify({ content: "Summarize this PDF in 5 lines", images: [{ name: "contract.pdf", mimeType: "application/pdf", data: base64 }] }),});