UNPKG

genkit

Version:

Genkit AI framework

139 lines 3.46 kB
import { Channel, createTask } from "@genkit-ai/core/async"; const __flowStreamDelimiter = "\n\n"; function streamFlow({ url, input, init, streamId, headers, abortSignal }) { const channel = new Channel(); const streamIdTask = createTask(); const operationPromise = __flowRunEnvelope({ url, input, init, sendChunk: (c) => channel.send(c), headers: streamId ? { ...headers, "x-genkit-stream-id": streamId } : headers, abortSignal, responseCallback: (response) => { streamIdTask.resolve(response.headers.get("x-genkit-stream-id")); } }); operationPromise.then( () => channel.close(), (err) => channel.error(err) ); return { output: operationPromise, stream: channel, streamId: streamIdTask.promise }; } async function __flowRunEnvelope({ url, input, init, sendChunk, headers, abortSignal, responseCallback }) { const response = await fetch(url, { method: "POST", body: JSON.stringify({ data: input, ...init !== void 0 && { init } }), headers: { Accept: "text/event-stream", "Content-Type": "application/json", ...headers }, signal: abortSignal }); if (responseCallback) { responseCallback(response); } if (response.status === 204) { throw new Error("NOT_FOUND: Stream not found."); } if (response.status !== 200) { throw new Error( `Server returned: ${response.status}: ${await response.text()}` ); } if (!response.body) { throw new Error("Response body is empty"); } var reader = response.body.getReader(); var decoder = new TextDecoder(); let buffer = ""; while (true) { const result = await reader.read(); const decodedValue = decoder.decode(result.value); if (decodedValue) { buffer += decodedValue; } while (buffer.includes(__flowStreamDelimiter)) { const chunk = JSON.parse( buffer.substring(0, buffer.indexOf(__flowStreamDelimiter)).substring("data: ".length) ); if (chunk.hasOwnProperty("message")) { sendChunk(chunk.message); } else if (chunk.hasOwnProperty("result")) { return chunk.result; } else if (chunk.hasOwnProperty("error")) { throw new Error( `${chunk.error.status}: ${chunk.error.message} ${chunk.error.details}` ); } else { throw new Error("unknown chunk format: " + JSON.stringify(chunk)); } buffer = buffer.substring( buffer.indexOf(__flowStreamDelimiter) + __flowStreamDelimiter.length ); } } throw new Error("stream did not terminate correctly"); } async function runFlow({ url, input, init, headers, abortSignal }) { const response = await fetch(url, { method: "POST", body: JSON.stringify({ data: input, ...init !== void 0 && { init } }), headers: { "Content-Type": "application/json", ...headers }, signal: abortSignal }); if (response.status !== 200) { throw new Error( `Server returned: ${response.status}: ${await response.text()}` ); } const wrappedResult = await response.json(); if ("error" in wrappedResult) { if (typeof wrappedResult.error === "string") { throw new Error(wrappedResult.error); } throw new Error(JSON.stringify(wrappedResult.error)); } return wrappedResult.result; } export { runFlow, streamFlow }; //# sourceMappingURL=client.mjs.map