|
| 1 | +# Response Handling |
| 2 | + |
| 3 | +## Streamed vs non-streamed responses |
| 4 | + |
| 5 | +- **Streamed Responses**: This method streams data in real-time, providing immediate feedback as the response is generated. Set the `stream` parameter to `true` in your request, the API delivers responses via Server-Sent Events (SSE). |
| 6 | +- **Non-Streamed Responses**: When the `stream` parameter is set to `false`, the API returns the complete response in a single JSON payload after processing is complete. |
| 7 | + |
| 8 | +## Handling Streamed Responses |
| 9 | + |
| 10 | +For `stream:true`, you'll need to handle the following event types: |
| 11 | + |
| 12 | +- `init`: Initializes the stream and provides session information |
| 13 | +- `presence`: Provides backend status updates about worker processing |
| 14 | +- `delta`: Contains chunks of the response message text |
| 15 | +- `action`: Contains blockchain transaction or action data |
| 16 | +- `image`: Contains image data |
| 17 | +- `context`: Contains context data |
| 18 | +- `error`: Contains error information if something goes wrong |
| 19 | + |
| 20 | +**Example SSE Stream:** |
| 21 | + |
| 22 | +```http |
| 23 | +event: init |
| 24 | +data: { |
| 25 | + "session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a", |
| 26 | + "request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164", |
| 27 | + "type": "init", |
| 28 | + "source": "model", |
| 29 | +} |
| 30 | +
|
| 31 | +event: presence |
| 32 | +data: { |
| 33 | + "session_id": "c238c379-9875-4729-afb2-bf3c26081e24", |
| 34 | + "request_id": "8b0e733a-89fd-4585-8fac-b4334208eada", |
| 35 | + "source": "model", |
| 36 | + "type": "presence", |
| 37 | + "data": "Need to convert 0.00001 ETH to wei, then prepare native transfer to `0x2247d5d238d0f9d37184d8332aE0289d1aD9991b`." |
| 38 | +} |
| 39 | +
|
| 40 | +event: presence |
| 41 | +data: { |
| 42 | + "session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a", |
| 43 | + "request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164", |
| 44 | + "type": "presence", |
| 45 | + "source": "model", |
| 46 | + "data": "Now that I have the wei value (10,000,000,000,000), I will prepare a native transfer to the recipient." |
| 47 | +} |
| 48 | +
|
| 49 | +event: delta |
| 50 | +data: {"v": "To send 0.0001 ETH on the Sepolia network"} |
| 51 | +
|
| 52 | +event: delta |
| 53 | +data: {"v": " to the address associated with"} |
| 54 | +
|
| 55 | +event: action |
| 56 | +data: { |
| 57 | + "session_id": "f4b45429-9570-4ee8-8c8f-8b267429915a", |
| 58 | + "request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164", |
| 59 | + "type": "sign_transaction", |
| 60 | + "source": "executor", |
| 61 | + "data": "{\"chainId\": 11155111, \"to\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\", \"data\": \"0x\", \"value\": \"0x5af3107a4000\"}" |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### TypeScript Example |
| 66 | + |
| 67 | +We recommend using the `fetch-event-stream` package to handle the event stream in TypeScript. This gives you an easy way to handle each event type. |
| 68 | + |
| 69 | +```jsx |
| 70 | +import { stream } from "fetch-event-stream"; |
| 71 | + |
| 72 | +const events = await stream(`https://api.thirdweb.com/ai/chat`, { |
| 73 | + body: JSON.stringify({ |
| 74 | + messages: [ |
| 75 | + { |
| 76 | + role: "user", |
| 77 | + content: "Transfer 10 USDC to vitalik.eth", |
| 78 | + }, |
| 79 | + ], |
| 80 | + }), |
| 81 | + headers: { |
| 82 | + "Content-Type": "application/json", |
| 83 | + "x-secret-key": "<your-secret-key>", |
| 84 | + }, |
| 85 | + method: "POST", |
| 86 | +}); |
| 87 | + |
| 88 | +// process the event stream |
| 89 | +for await (const event of events) { |
| 90 | + if (!event.data) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + switch (event.event) { |
| 94 | + case "init": { |
| 95 | + console.log("Init event", event.data); |
| 96 | + // handle init event (session id and request id) |
| 97 | + break; |
| 98 | + } |
| 99 | + case "presence": { |
| 100 | + console.log("Presence event", event.data); |
| 101 | + // handle intermediate thinking steps |
| 102 | + break; |
| 103 | + } |
| 104 | + case "delta": { |
| 105 | + console.log("Delta event", event.data); |
| 106 | + // handle delta event (streamed output text response) |
| 107 | + break; |
| 108 | + } |
| 109 | + case "action": { |
| 110 | + const data = JSON.parse(event.data); |
| 111 | + |
| 112 | + // handle transaction signing |
| 113 | + if (data.type === "sign_transaction") { |
| 114 | + const transactionData = JSON.parse(data.data); |
| 115 | + console.log("Sign transaction event", transactionData); |
| 116 | + } |
| 117 | + |
| 118 | + // handle swap signing |
| 119 | + if (data.type === "sign_swap") { |
| 120 | + const swapData = JSON.parse(data.data); |
| 121 | + console.log("Sign swap event", swapData); |
| 122 | + } |
| 123 | + |
| 124 | + break; |
| 125 | + } |
| 126 | + case "image": { |
| 127 | + const data = JSON.parse(event.data); |
| 128 | + // handle image rendering |
| 129 | + console.log("Image data", data); |
| 130 | + break; |
| 131 | + } |
| 132 | + case "context": { |
| 133 | + console.log("Context event", event.data); |
| 134 | + // handle context changes (chain ids, wallet address, etc) |
| 135 | + break; |
| 136 | + } |
| 137 | + case "error": { |
| 138 | + console.log("Error event", event.data); |
| 139 | + // handle error event (error message) |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
0 commit comments