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