( payload: StreamPipelineChatPayload, )
| 20 | } |
| 21 | |
| 22 | export async function* streamPipelineChat( |
| 23 | payload: StreamPipelineChatPayload, |
| 24 | ): AsyncGenerator<PipelineChatEvent> { |
| 25 | const endpoint = buildApiUrl(`/api/pipelines/${encodeURIComponent(payload.pipelineName)}/chat`); |
| 26 | const dynamicParams: Record<string, unknown> = { |
| 27 | memory: { user_id: payload.userId }, |
| 28 | }; |
| 29 | if (payload.collectionName) { |
| 30 | dynamicParams.collection_name = payload.collectionName; |
| 31 | } |
| 32 | |
| 33 | const response = await fetch(endpoint, { |
| 34 | method: "POST", |
| 35 | credentials: "include", |
| 36 | headers: { |
| 37 | "Content-Type": "application/json", |
| 38 | }, |
| 39 | body: JSON.stringify({ |
| 40 | question: payload.question, |
| 41 | history: payload.history, |
| 42 | is_demo: true, |
| 43 | session_id: payload.sessionId, |
| 44 | chat_session_id: payload.chatSessionId, |
| 45 | dynamic_params: dynamicParams, |
| 46 | }), |
| 47 | signal: payload.signal, |
| 48 | }); |
| 49 | |
| 50 | if (!response.ok) { |
| 51 | let apiMessage = response.statusText; |
| 52 | try { |
| 53 | const errorPayload = (await response.json()) as { error?: string; details?: string }; |
| 54 | apiMessage = errorPayload.error ?? errorPayload.details ?? apiMessage; |
| 55 | } catch { |
| 56 | // noop |
| 57 | } |
| 58 | throw new ApiHttpError(response.status, { error: apiMessage }, "Pipeline chat request failed"); |
| 59 | } |
| 60 | |
| 61 | if (!response.body) { |
| 62 | throw new Error("Pipeline chat stream body is empty"); |
| 63 | } |
| 64 | |
| 65 | const reader = response.body.getReader(); |
| 66 | const decoder = new TextDecoder(); |
| 67 | let buffer = ""; |
| 68 | |
| 69 | while (true) { |
| 70 | const { done, value } = await reader.read(); |
| 71 | if (done) break; |
| 72 | buffer += decoder.decode(value, { stream: true }); |
| 73 | const blocks = buffer.split("\n\n"); |
| 74 | buffer = blocks.pop() ?? ""; |
| 75 | |
| 76 | for (const block of blocks) { |
| 77 | const line = block |
| 78 | .split("\n") |
| 79 | .map((segment) => segment.trim()) |
no test coverage detected