-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Update worker.mjs #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,21 +18,27 @@ export default { | |||||||||
} | ||||||||||
}; | ||||||||||
const { pathname } = new URL(request.url); | ||||||||||
switch (true) { | ||||||||||
case pathname.endsWith("/chat/completions"): | ||||||||||
assert(request.method === "POST"); | ||||||||||
return handleCompletions(await request.json(), apiKey) | ||||||||||
.catch(errHandler); | ||||||||||
case pathname.endsWith("/embeddings"): | ||||||||||
assert(request.method === "POST"); | ||||||||||
return handleEmbeddings(await request.json(), apiKey) | ||||||||||
.catch(errHandler); | ||||||||||
case pathname.endsWith("/models"): | ||||||||||
assert(request.method === "GET"); | ||||||||||
return handleModels(apiKey) | ||||||||||
.catch(errHandler); | ||||||||||
default: | ||||||||||
throw new HttpError("404 Not Found", 404); | ||||||||||
const routeRegex = /^\/([\w.-]+)\/v1\/(chat\/completions|embeddings)$/; | ||||||||||
const match = pathname.match(routeRegex); | ||||||||||
|
||||||||||
if (!match) { | ||||||||||
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/chat/completions", 404); | ||||||||||
} | ||||||||||
|
||||||||||
const modelFromUrl = match[1]; | ||||||||||
const endpoint = match[2]; | ||||||||||
let body; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing method validation. The original code verified POST method for both endpoints. Consider adding 'assert(request.method === "POST");' before parsing JSON.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
try { | ||||||||||
body = await request.json(); | ||||||||||
} catch (e) { | ||||||||||
body = {}; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent fallback to empty object when JSON parsing fails could mask client errors. Consider throwing a more specific error for malformed JSON requests. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
} | ||||||||||
body.model = modelFromUrl; | ||||||||||
|
||||||||||
if (endpoint === "chat/completions") { | ||||||||||
return handleCompletions(body, apiKey).catch(errHandler); | ||||||||||
} else if (endpoint === "embeddings") { | ||||||||||
return handleEmbeddings(body, apiKey).catch(errHandler); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing return statement for the embeddings case and no fallback for unexpected endpoint values. This could cause the function to return undefined.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
} | ||||||||||
} catch (err) { | ||||||||||
return errHandler(err); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message only mentions chat/completions format but the code also supports embeddings. Consider updating to: '404 Not Found. Use format: //v1/{chat/completions|embeddings}'
Copilot uses AI. Check for mistakes.