// Probe 08: raw REST streaming protocol (alt=sse) and the default (no alt) JSON-array streaming, on one model. import { KEY, MODELS, save } from "./lib.js"; const model = MODELS[0]; const body = JSON.stringify({ contents: [{ role: "user", parts: [{ text: "Say hello in five words, then think of nothing else." }] }], generationConfig: { maxOutputTokens: 1500, thinkingConfig: { includeThoughts: true, thinkingLevel: "LOW" } } }); const results: any = { model }; { const res = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent?alt=sse`, { method: "POST", headers: { "x-goog-api-key": KEY, "content-type": "application/json" }, body }); const raw = await res.text(); const events = raw.split("\n\n").filter((s) => s.trim()); results.sse = { status: res.status, contentType: res.headers.get("content-type"), transferEncoding: res.headers.get("transfer-encoding"), eventCount: events.length, firstEventRaw: events[0]?.slice(0, 1500), lastEventRaw: events.at(-1)?.slice(0, 1500), linePrefixes: [...new Set(events.flatMap((e) => e.split("\n").map((l) => l.split(":")[0])))] }; console.log("SSE", res.status, results.sse.contentType, "events", events.length, "prefixes", results.sse.linePrefixes); console.log(events[0]?.slice(0, 600)); } { const res = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent`, { method: "POST", headers: { "x-goog-api-key": KEY, "content-type": "application/json" }, body }); const raw = await res.text(); results.noAlt = { status: res.status, contentType: res.headers.get("content-type"), startsWith: raw.slice(0, 120), endsWith: raw.slice(-60) }; console.log("no-alt", res.status, results.noAlt.contentType, JSON.stringify(results.noAlt.startsWith)); } save(`08-rest-sse-${model}.json`, results);