|
| 1 | +// TODO - copy the source of this library to dashboard |
| 2 | +import { stream } from "fetch-event-stream"; |
| 3 | +import { NEXT_PUBLIC_NEBULA_URL } from "../../../@/constants/env"; |
| 4 | +import type { ExecuteConfig } from "./types"; |
| 5 | + |
| 6 | +// TODO - ready response as stream |
| 7 | + |
| 8 | +type ChatStreamedResponse = |
| 9 | + | { |
| 10 | + event: "presence"; |
| 11 | + data: { |
| 12 | + session_id: string; |
| 13 | + request_id: string; |
| 14 | + source: "user" | "reviewer" | (string & {}); |
| 15 | + data: string; |
| 16 | + }; |
| 17 | + } |
| 18 | + | { |
| 19 | + event: "delta"; |
| 20 | + data: { |
| 21 | + v: string; |
| 22 | + }; |
| 23 | + } |
| 24 | + | { |
| 25 | + event: "action"; |
| 26 | + type: "sign_transaction"; |
| 27 | + data: string; |
| 28 | + } |
| 29 | + | { |
| 30 | + event: "action"; |
| 31 | + // other types of actions |
| 32 | + type: string & {}; |
| 33 | + }; |
| 34 | + |
| 35 | +type HandleStreamCallback = (res: ChatStreamedResponse) => void; |
| 36 | + |
| 37 | +export async function promptNebula(params: { |
| 38 | + message: string; |
| 39 | + sessionId: string; |
| 40 | + config: ExecuteConfig; |
| 41 | + authToken: string; |
| 42 | + handleStream: HandleStreamCallback; |
| 43 | + onStreamEnd: () => void; |
| 44 | +}) { |
| 45 | + // TODO: properly type the body |
| 46 | + const body: Record<string, string | boolean | object> = { |
| 47 | + message: params.message, |
| 48 | + user_id: "default-user", |
| 49 | + session_id: params.sessionId, |
| 50 | + stream: true, |
| 51 | + Authorization: `Bearer ${params.authToken}`, |
| 52 | + }; |
| 53 | + |
| 54 | + if (params.config.mode === "client") { |
| 55 | + body.execute = { |
| 56 | + type: "client", |
| 57 | + signer_wallet_address: params.config.signer_wallet_address, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + const events = await stream(`${NEXT_PUBLIC_NEBULA_URL}/chat`, { |
| 62 | + method: "POST", |
| 63 | + headers: { |
| 64 | + Authorization: `Bearer ${params.authToken}`, |
| 65 | + "Content-Type": "application/json", |
| 66 | + }, |
| 67 | + body: JSON.stringify(body), |
| 68 | + }); |
| 69 | + |
| 70 | + for await (const event of events) { |
| 71 | + if (!event.data) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + // delta |
| 76 | + if (event.event === "delta") { |
| 77 | + params.handleStream({ |
| 78 | + event: "delta", |
| 79 | + data: { |
| 80 | + v: JSON.parse(event.data).v, |
| 81 | + }, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // presence |
| 86 | + if (event.event === "presence") { |
| 87 | + params.handleStream({ |
| 88 | + event: "presence", |
| 89 | + data: JSON.parse(event.data), |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + // action |
| 94 | + if (event.event === "action") { |
| 95 | + params.handleStream({ |
| 96 | + event: "action", |
| 97 | + type: JSON.parse(event.data).type, |
| 98 | + data: JSON.parse(event.data).data, |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + params.onStreamEnd(); |
| 104 | +} |
0 commit comments