MCPcopy Create free account
hub / github.com/Alphanimble/htmlstream / pipeOpenRouterStream

Function pipeOpenRouterStream

demo/server/openrouter-chat.ts:163–245  ·  view source on GitHub ↗
(
  res: ServerResponse,
  apiKey: string,
  model: string,
  messages: { role: string; content: string }[],
)

Source from the content-addressed store, hash-verified

161}
162
163async function pipeOpenRouterStream(
164 res: ServerResponse,
165 apiKey: string,
166 model: string,
167 messages: { role: string; content: string }[],
168): Promise<void> {
169 const upstream = await fetch("https://openrouter.ai/api/v1/chat/completions", {
170 method: "POST",
171 headers: {
172 Authorization: `Bearer ${apiKey}`,
173 "Content-Type": "application/json",
174 "HTTP-Referer": "http://localhost:5173",
175 "X-Title": "StreamHtml Chat",
176 },
177 body: JSON.stringify({
178 model,
179 messages: [{ role: "system", content: SYSTEM_PROMPT }, ...messages],
180 stream: true,
181 reasoning: { enabled: true },
182 }),
183 });
184
185 if (!upstream.ok) {
186 const errText = await upstream.text();
187 res.statusCode = upstream.status;
188 res.setHeader("Content-Type", "application/json");
189 res.end(JSON.stringify({ error: errText || upstream.statusText }));
190 return;
191 }
192
193 if (!upstream.body) {
194 res.statusCode = 502;
195 res.end("No response body from OpenRouter");
196 return;
197 }
198
199 res.statusCode = 200;
200 res.setHeader("Content-Type", "application/x-ndjson; charset=utf-8");
201 res.setHeader("Cache-Control", "no-cache, no-transform");
202 res.setHeader("Connection", "keep-alive");
203 res.setHeader("X-Content-Type-Options", "nosniff");
204 res.flushHeaders?.();
205
206 const reader = upstream.body.getReader();
207 const decoder = new TextDecoder();
208 let buffer = "";
209
210 try {
211 while (true) {
212 const { done, value } = await reader.read();
213 if (done) {
214 break;
215 }
216
217 buffer += decoder.decode(value, { stream: true });
218 const lines = buffer.split("\n");
219 buffer = lines.pop() ?? "";
220

Callers 1

configureServerFunction · 0.85

Calls 2

extractDeltaPartsFunction · 0.85
writeStreamPartFunction · 0.85

Tested by

no test coverage detected