-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming-proxy.ts
More file actions
270 lines (247 loc) · 9.18 KB
/
streaming-proxy.ts
File metadata and controls
270 lines (247 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// ---------------------------------------------------------------------------
// src/streaming-proxy.ts — Streaming‐to‐non‐streaming proxy for Foundry Local
//
// Foundry Local (as of v0.5.0) sometimes doesn't respond to streaming
// requests at all (the TCP connection hangs forever once "stream": true is
// in the body). Non-streaming requests, on the other hand, work perfectly.
//
// This tiny HTTP proxy transparently converts streaming requests into
// non-streaming ones, then re-encodes the single JSON response as
// server-sent events (SSE) — the format the OpenAI SDK expects.
//
// Non-streaming requests and non-chat-completions endpoints are proxied
// through unchanged.
//
// Usage:
// const { proxyBaseUrl, close } = await startStreamingProxy(foundryEndpoint);
// // Point the Copilot SDK BYOK provider at proxyBaseUrl instead of foundryEndpoint
// ---------------------------------------------------------------------------
import http from "http";
export interface StreamingProxyHandle {
/** Base URL for the proxy, e.g. "http://127.0.0.1:54321/v1" */
proxyBaseUrl: string;
/** Shut down the proxy server. */
close: () => Promise<void>;
}
/**
* Start the streaming proxy.
*
* @param upstreamBaseUrl Foundry Local endpoint including `/v1`, e.g.
* `http://127.0.0.1:51995/v1`
* @returns A handle with `proxyBaseUrl` and `close()`.
*/
export async function startStreamingProxy(
upstreamBaseUrl: string,
): Promise<StreamingProxyHandle> {
// Normalise: strip trailing slash if any
const upstream = upstreamBaseUrl.replace(/\/+$/, "");
// Strip /v1 suffix from upstream so it's not doubled when the SDK's
// request URL already includes /v1 (e.g. /v1/chat/completions).
const upstreamOrigin = upstream.replace(/\/v1$/, "");
const server = http.createServer(async (req, res) => {
const target = `${upstreamOrigin}${req.url ?? ""}`;
// ── Read the incoming body ──────────────────────────────────────────
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
const rawBody = Buffer.concat(chunks).toString("utf-8");
// Decide whether this is a streaming chat-completions request
let body: any = null;
let isStreamingChat = false;
const isChatCompletions = (req.url ?? "").includes("/chat/completions");
if (rawBody && isChatCompletions) {
try {
body = JSON.parse(rawBody);
if (body.stream === true) {
isStreamingChat = true;
body.stream = false; // ← the key conversion
}
} catch {
// Not valid JSON — just forward as-is
}
}
const outBody = isStreamingChat ? JSON.stringify(body) : rawBody;
// ── Forward to upstream Foundry Local ────────────────────────────────
const headers: Record<string, string> = {
"content-type": req.headers["content-type"] ?? "application/json",
};
if (req.headers.authorization) {
headers.authorization = req.headers.authorization;
}
try {
const upRes = await fetch(target, {
method: req.method ?? "POST",
headers,
body: req.method !== "GET" ? outBody : undefined,
});
if (!upRes.ok) {
res.writeHead(upRes.status, { "content-type": "application/json" });
const errText = await upRes.text();
res.end(errText);
return;
}
// ── If this was a streaming request: re-encode as SSE ─────────────
if (isStreamingChat) {
const upJson = await upRes.json() as any;
res.writeHead(200, {
"content-type": "text/event-stream",
"cache-control": "no-cache",
connection: "keep-alive",
});
const id = upJson.id ?? `chatcmpl-proxy-${Date.now()}`;
const created = upJson.created ?? Math.floor(Date.now() / 1000);
const model = upJson.model ?? "";
for (const choice of upJson.choices ?? []) {
const msg = choice.message ?? {};
const idx = choice.index ?? 0;
// 1. Role chunk
const roleChunk = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [
{
index: idx,
delta: { role: msg.role ?? "assistant" },
finish_reason: null,
},
],
};
res.write(`data: ${JSON.stringify(roleChunk)}\n\n`);
// 2. Content chunks (split into ~80-char pieces for realism)
if (msg.content) {
const content = msg.content as string;
const CHUNK_SIZE = 80;
for (let i = 0; i < content.length; i += CHUNK_SIZE) {
const piece = content.slice(i, i + CHUNK_SIZE);
const contentChunk = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [
{
index: idx,
delta: { content: piece },
finish_reason: null,
},
],
};
res.write(`data: ${JSON.stringify(contentChunk)}\n\n`);
}
}
// 3. Tool-call chunks (if any)
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
for (let ti = 0; ti < msg.tool_calls.length; ti++) {
const tc = msg.tool_calls[ti];
// First chunk: id + function name
const tcStartChunk = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [
{
index: idx,
delta: {
tool_calls: [
{
index: ti,
id: tc.id ?? `call_${ti}`,
type: "function",
function: {
name: tc.function?.name ?? "",
arguments: "",
},
},
],
},
finish_reason: null,
},
],
};
res.write(`data: ${JSON.stringify(tcStartChunk)}\n\n`);
// Arguments in ~80-char chunks
const args = tc.function?.arguments ?? "{}";
const CHUNK_SIZE = 80;
for (let i = 0; i < args.length; i += CHUNK_SIZE) {
const piece = args.slice(i, i + CHUNK_SIZE);
const tcArgChunk = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [
{
index: idx,
delta: {
tool_calls: [
{
index: ti,
function: { arguments: piece },
},
],
},
finish_reason: null,
},
],
};
res.write(`data: ${JSON.stringify(tcArgChunk)}\n\n`);
}
}
}
// 4. Finish chunk
const finishChunk = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [
{
index: idx,
delta: {},
finish_reason: choice.finish_reason ?? "stop",
},
],
};
res.write(`data: ${JSON.stringify(finishChunk)}\n\n`);
}
// 5. [DONE]
res.write("data: [DONE]\n\n");
res.end();
return;
}
// ── Non-streaming: pass through unchanged ─────────────────────────
const resBody = await upRes.text();
const resHeaders: Record<string, string> = {};
upRes.headers.forEach((v, k) => {
resHeaders[k] = v;
});
res.writeHead(upRes.status, resHeaders);
res.end(resBody);
} catch (err: any) {
console.error("[streaming-proxy] upstream error:", err.message ?? err);
res.writeHead(502, { "content-type": "application/json" });
res.end(JSON.stringify({ error: "proxy_error", message: err.message }));
}
});
// Listen on a random port
return new Promise((resolve, reject) => {
server.listen(0, "127.0.0.1", () => {
const addr = server.address();
if (!addr || typeof addr === "string") {
reject(new Error("Failed to start streaming proxy"));
return;
}
const proxyBaseUrl = `http://127.0.0.1:${addr.port}/v1`;
console.log(` [streaming-proxy] Listening on ${proxyBaseUrl} → ${upstream}`);
resolve({
proxyBaseUrl,
close: () =>
new Promise<void>((res) => {
server.close(() => res());
}),
});
});
});
}