const COMPLETION = "/chat/completions";
const MODELS = "/models";
const supported_urls = [COMPLETION, MODELS];
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "*",
};
function err(msg): Response {
return new Response(
JSON.stringify({
error: {
message: msg,
code: 400,
},
}),
{
status: 400,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
},
);
}
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
const headers = new Headers(req.headers);
if (req.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: {
...corsHeaders,
"Access-Control-Max-Age": "86400",
},
});
}
if (!supported_urls.includes(pathname)) {
return new Response(null, {
status: 302,
headers: {
"Location": "https://chatcraft.org",
},
});
}
let body: string | ReadableStream<Uint8Array> | null = null;
if (pathname == COMPLETION) {
const params = await req.clone().json();
const model = params.model.replace("fireworks/", "accounts/fireworks/models/");
const messages = params.messages.map(msg => {
const content = typeof msg.content == "string" ? msg.content : msg.content.map(x => x.text).join("");
return { ...msg, content: content };
});
body = JSON.stringify({ ...params, model: model, messages: messages });
} else {
body = req.body;
}
url.host = "api.fireworks.ai";
url.port = "443";
url.protocol = "https";
url.pathname = "/inference/v1" + pathname;
const init = {
method: req.method,
headers: {
"authorization": headers.get("authorization"),
"content-type": headers.get("content-type"),
},
body: body,
};
const response = await fetch(url.toString(), init);
const responseHeaders = new Headers(response.headers);
if (pathname == MODELS) {
const responseBody = await response.clone().json();
const fixedModels = responseBody.data
.filter((model) => model.supports_chat)