({
method,
input,
req,
init,
onGeneratedChunk,
onFinish,
}: {
method: HttpMethod;
input: RequestInfo;
req: REQ | null;
init?: RequestInit;
onGeneratedChunk?: (chunk: string, generatedContent: string) => void;
onFinish?: (generatedContent: string) => void;
})
| 88 | } |
| 89 | |
| 90 | async function makeRequestStreaming<REQ>({ |
| 91 | method, |
| 92 | input, |
| 93 | req, |
| 94 | init, |
| 95 | onGeneratedChunk, |
| 96 | onFinish, |
| 97 | }: { |
| 98 | method: HttpMethod; |
| 99 | input: RequestInfo; |
| 100 | req: REQ | null; |
| 101 | init?: RequestInit; |
| 102 | onGeneratedChunk?: (chunk: string, generatedContent: string) => void; |
| 103 | onFinish?: (generatedContent: string) => void; |
| 104 | }): Promise<void> { |
| 105 | const res = await fetchHelper(method, input, req, init); |
| 106 | if (!res || !res.body) { |
| 107 | throw new Error(`received null response! ${res}, ${res.body}`); |
| 108 | } |
| 109 | |
| 110 | const reader = res.body.pipeThrough(new TextDecoderStream()).getReader(); |
| 111 | let generatedContent = ""; |
| 112 | while (true) { |
| 113 | const { value: chunk, done } = await reader.read(); |
| 114 | if (done) { |
| 115 | break; |
| 116 | } |
| 117 | generatedContent += chunk; |
| 118 | onGeneratedChunk?.(chunk, generatedContent); |
| 119 | } |
| 120 | onFinish?.(generatedContent); |
| 121 | } |
| 122 | |
| 123 | async function fetchHelper<REQ>( |
| 124 | method: HttpMethod, |
nothing calls this directly
no test coverage detected