import { SSEEventData, SSEChunk } from "../types.js"; import { logDebug, logError } from "../logger.js"; export function parseSseStream(sseText: string): SSEEventData | null { const lines = sseText.split("\n"); for (const line of lines) { if (line.startsWith("data: ")) { try { const data = JSON.parse(line.substring(6)) as SSEEventData; if (data.type === "response.done" || data.type === "response.completed") { logDebug(null, "Found response.done event in SSE stream"); return data; } } catch (error) { logError(null, `Failed to parse SSE event: ${line}`); } } } logError(null, "No response.done event found in SSE stream"); return null; } export function parseSseChunks(sseText: string): SSEChunk[] { const chunks: SSEChunk[] = []; const lines = sseText.split("\n"); for (const line of lines) { if (line.startsWith("data: ")) { try { const data = JSON.parse(line.substring(6)); chunks.push(data as SSEChunk); } catch (error) { logError(null, `Failed to parse SSE chunk: ${line}`); } } } return chunks; }