-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
82 lines (66 loc) · 2.21 KB
/
index.ts
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
import { OpenAI } from "openai";
import type { ChatCompletionStreamParams } from "openai/lib/ChatCompletionStream.mjs";
import { z } from "zod";
const Message = z.object({
role: z.string(),
name: z.string().optional(),
content: z.string(),
});
const Input = z.object({
messages: z.array(Message),
});
const openai = new OpenAI({ apiKey: Bun.env.OPENAI_API_KEY });
Bun.serve({
port: Bun.env.PORT ?? "3000",
async fetch(request) {
console.debug("received request", request.url);
// Do nothing with the OAuth callback, for now. Just return a 200.
if (new URL(request.url).pathname === "/oauth/callback") {
console.debug("received oauth callback");
return Response.json({ ok: true }, { status: 200 });
}
// Parsing with Zod strips unknown Copilot-specific fields in the request
// body, which cause OpenAI errors if they're included.
const json = await request.json();
const input = Input.safeParse(json);
if (!input.success) {
return Response.json({ error: "Bad request" }, { status: 400 });
}
const messages = input.data.messages;
console.debug("received input", JSON.stringify(json, null, 4));
console.debug("received messages", JSON.stringify(messages, null, 4));
// Insert a special hackery system message in our message list.
messages.splice(-1, 0, {
role: "system",
content: "Please use entirely made-up hackery-sounding terminology.",
});
const openaiRequestBody = {
messages,
model: "gpt-4-1106-preview",
stream: true,
};
const stream = openai.beta.chat.completions.stream(
openaiRequestBody as ChatCompletionStreamParams,
);
console.debug(
"sending request to OpenAI",
JSON.stringify(openaiRequestBody, null, 4),
);
// Proxy the OpenAI API response right back to the extensibility platform.
return new Response(
new ReadableStream({
async start(controller) {
stream.on("end", () => {
controller.close();
});
stream.on("chunk", (chunk) =>
controller.enqueue(`data: ${JSON.stringify(chunk)}\n\n`),
);
},
cancel() {
stream.abort();
},
}),
);
},
});