handle preflight
This commit is contained in:
@@ -45,12 +45,16 @@ var provider = new YouProvider(config);
|
||||
await provider.init(config);
|
||||
|
||||
// handle preflight request
|
||||
app.options("/v1/messages", (req, res) => {
|
||||
app.use((req, res, next) => {
|
||||
if (req.method === "OPTIONS") {
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
res.setHeader("Access-Control-Allow-Methods", "*");
|
||||
res.setHeader("Access-Control-Allow-Headers", "*");
|
||||
res.setHeader("Access-Control-Max-Age", "86400");
|
||||
res.status(200).end();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
// openai format model request
|
||||
app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
||||
@@ -122,7 +126,6 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
||||
completion.on("completion", (id, text) => {
|
||||
if (jsonBody.stream) {
|
||||
// send message delta
|
||||
if (jsonBody.stream) {
|
||||
res.write(
|
||||
createEvent("data", {
|
||||
choices: [
|
||||
@@ -145,7 +148,6 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
||||
system_fingerprint: "114514",
|
||||
})
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// 只会发一次,发送final response
|
||||
res.write(
|
||||
@@ -312,7 +314,6 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
||||
completion.on("completion", (id, text) => {
|
||||
if (jsonBody.stream) {
|
||||
// send message delta
|
||||
if (jsonBody.stream) {
|
||||
res.write(
|
||||
createEvent("content_block_delta", {
|
||||
type: "content_block_delta",
|
||||
@@ -320,7 +321,6 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
||||
delta: { type: "text_delta", text: text },
|
||||
})
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// 只会发一次,发送final response
|
||||
res.write(
|
||||
|
||||
Reference in New Issue
Block a user