-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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; | ||||||||||
|
||||||||||
| let body; | |
| let body; | |
| assert(request.method === "POST"); |
Copilot
AI
Jul 16, 2025
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.
Silent fallback to empty object when JSON parsing fails could mask client errors. Consider throwing a more specific error for malformed JSON requests.
Copilot
AI
Jul 16, 2025
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.
Missing return statement for the embeddings case and no fallback for unexpected endpoint values. This could cause the function to return undefined.
| return handleEmbeddings(body, apiKey).catch(errHandler); | |
| return handleEmbeddings(body, apiKey).catch(errHandler); | |
| } else { | |
| throw new HttpError("Invalid endpoint. Supported endpoints are 'chat/completions' and 'embeddings'.", 400); |
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}'