🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
/**
* Consumes a Response stream completely to ensure all callbacks are executed
* @param response - The Response object with a ReadableStream body
* @returns Promise that resolves when the stream is fully consumed
*
* @example
* ```ts
* const response = await modelRuntime.chat(payload, {
* callback: {
* onText: async (text) => {
* await saveToDatabase(text);
* }
* }
* });
*
* // Ensure all callbacks complete before proceeding
* await consumeStreamUntilDone(response);
* ```
*/
export async function consumeStreamUntilDone(response: Response): Promise<void> {
if (!response.body) {
return;
}
const reader = response.body.getReader();
try {
while (true) {
const { done } = await reader.read();
if (done) break;
}
} finally {
reader.releaseLock();
}
}
/**
* Answer a JSON-mode chat with the raw provider body. Usage callbacks only fire inside the
* stream transform, so drain it first; otherwise `onFinal` never reports usage and usage
* tracking (billing, tracing) silently sees nothing.
*/
export async function respondJsonAfterStream(
streamingResponse: Response,
body: unknown,
): Promise<Response> {
await consumeStreamUntilDone(streamingResponse);
return Response.json(body);
}