-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix: cursor use ollama verify issue #28
Conversation
WalkthroughThe pull request introduces an update to the Changes
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (5)
proxy.ts (3)
37-37
: Replace 'void' with 'undefined' in return type to avoid confusionUsing
void
in a union type can be confusing. Consider replacingvoid
withundefined
for clarity.Apply this diff to update the return type:
-private handleAllRequest(c: any, next: () => Promise<any>): Response | Promise<void | Response> { +private handleAllRequest(c: any, next: () => Promise<any>): Response | Promise<Response | undefined> {🧰 Tools
🪛 Biome (1.9.4)
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
97-100
: Improve type safety by specifying explicit types instead of 'any'The method
isVerifyRequest
uses parameters typed asany
. Specifying explicit types enhances type safety and code readability.Consider defining a type or interface for the context
c
and thejson
object. For example:interface RequestContext { req: Request; res: Response; // other properties... } interface JsonBody { model: string; stream: boolean; // other properties... }Then, update the method signature:
-private isVerifyRequest(c: any, json: any) { +private isVerifyRequest(c: RequestContext, json: JsonBody) {
190-207
: Handle potential errors when parsing JSON in 'getModelsList'In the
getModelsList
method, if the response is not valid JSON,res.json()
could throw an error. Consider adding a check to handle this scenario gracefully.Apply this diff to catch JSON parsing errors:
try { const res = await fetch(req); - const data = await res.json(); + const data = await res.json().catch(() => null); + if (!data) { + throw new Error("Invalid JSON response"); + } return data.models || []; } catch (error) {main.ts (1)
90-90
: Fix typo: 'OpenAl' should be 'OpenAI'There's a typo in the console output. The term 'OpenAl' should be corrected to 'OpenAI' to reflect the proper name.
Apply this diff to correct the typo:
italic(`Override OpenAl Base URL`) +italic(`Override OpenAI Base URL`)
util.ts (1)
42-44
: Avoid exporting from a test file by separating tests into a dedicated fileStatic analysis indicates that exports are present in a test file, which is discouraged. Consider moving the test cases to a separate file to adhere to best practices.
Steps to refactor:
- Create a new file named
util.test.ts
for the test cases.- Move all the
Deno.test
blocks fromutil.ts
toutil.test.ts
.- Ensure the imports in
util.test.ts
are updated accordingly.- Keep the utility functions and exports in
util.ts
.This separation enhances code organization and maintainability.
🧰 Tools
🪛 Biome (1.9.4)
[error] 43-44: Do not export from a test file.
(lint/suspicious/noExportsInTest)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
⛔ Files ignored due to path filters (1)
deno.lock
is excluded by!**/*.lock
📒 Files selected for processing (4)
deno.json
(1 hunks)main.ts
(2 hunks)proxy.ts
(1 hunks)util.ts
(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- deno.json
🧰 Additional context used
🪛 Biome (1.9.4)
util.ts
[error] 43-44: Do not export from a test file.
(lint/suspicious/noExportsInTest)
proxy.ts
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (5)
proxy.ts (5)
Line range hint
1-30
: LGTM! Consider adding TypeScript interfaces for better type safety.The class structure is well-organized with proper encapsulation and documentation. However, consider defining interfaces for the configuration and request/response types to improve type safety.
Add these type definitions at the beginning of the file:
interface ProxyConfig { openAIEndpoint: string; ollamaEndpoint: string; OPENAI_API_KEY?: string; } interface OllamaModel { name: string; // add other relevant fields }🧰 Tools
🪛 Biome (1.9.4)
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
110-137
: Enhance error handling in POST request processing.While the request handling is good, consider adding try-catch blocks and more specific error responses.
Apply this improvement:
private async handlePostRequest(c: any): Promise<Response> { + try { const json = await c.req.raw.clone().json(); assert(json, is.ObjectOf({ model: is.String })); const endpoint = chooseEndpoint({ model: json.model, ollamaEndpoint: this.ollamaEndpoint, openAIEndpoint: this.openAIEndpoint, }); let res = await this.handleOpenAIModelVerify(c, json); if (res) { return res; } const origin = new URL(this.ollamaEndpoint).origin; const url = convertToCustomEndpoint(c.req.url, parseURL(endpoint)); const reqHeaders = this.setCORSHeaders(c.req.raw, origin); reqHeaders.set('Origin', origin); const req = new Request(url, { ...c.req.raw, method: "POST", body: JSON.stringify(json), headers: reqHeaders, mode: 'no-cors' }); return fetch(req); + } catch (error) { + console.error('Error processing POST request:', error); + return new Response( + JSON.stringify({ error: 'Failed to process request' }), + { + status: 500, + headers: { 'Content-Type': 'application/json' } + } + ); + } }
65-72
: Add type definitions for CORS configuration.Consider adding type definitions for CORS configuration to make the code more maintainable.
Add these types:
interface CORSConfig { allowedOrigins: string | string[]; allowedMethods: string[]; allowedHeaders: string[]; allowCredentials: boolean; }Then use them in the method:
- private setCORSHeaders(res: Response, origin: string): Headers { + private setCORSHeaders(res: Response, origin: string, config?: Partial<CORSConfig>): Headers { const headers = new Headers(res.headers); - headers.set('Access-Control-Allow-Origin', origin || '*'); + headers.set('Access-Control-Allow-Origin', origin || config?.allowedOrigins || '*'); // ... rest of the method }
74-78
: Translate Chinese comments to English for consistency.The codebase appears to be primarily in English, but these comments are in Chinese. Consider translating them for better maintainability.
Replace with:
- /** - * 处理验证请求的模型 - * @param c - * @param json - * @returns - */ + /** + * Handles model verification requests + * @param c - The context object + * @param json - The request body + * @returns Response or null if verification succeeds + */
37-37
: Replace void with undefined in union type.Using void in a union type can be confusing. Consider using undefined instead for better type clarity.
- private handleAllRequest(c: any, next: () => Promise<any>): Response | Promise<void | Response> { + private handleAllRequest(c: any, next: () => Promise<any>): Response | Promise<undefined | Response> {🧰 Tools
🪛 Biome (1.9.4)
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
proxy.ts
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
proxy.ts
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
🔇 Additional comments (2)
proxy.ts (2)
41-42
: Security improvement: API key validation added.
Good addition of the API key validation check, addressing the previous security concern about unauthorized access.
194-211
: Method name should be 'getModelsList' for consistency.
The method name should reflect that it's fetching models, not modules.
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.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
proxy.ts (3)
37-37
: Consider refining the return type signatureThe return type
Response | Promise<void | Response>
could be simplified toPromise<Response | undefined>
for better type clarity.- private handleAllRequest(c: any, next: () => Promise<any>): Response | Promise<void | Response> { + private handleAllRequest(c: any, next: () => Promise<any>): Promise<Response | undefined> {🧰 Tools
🪛 Biome (1.9.4)
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
96-100
: Translate comments to English for consistencyThe Chinese comment should be translated to English to maintain documentation consistency.
- * 验证是否是Cursor的请求 + * Verifies if this is a Cursor request
79-94
: Enhance error handling with more specific messagesThe error handling could be more informative by including additional context about why the model wasn't found.
- console.error(`Ollama Model not found: ${json.model}`); - return new Response(JSON.stringify({ error: "Model not found" }), { + const errorMessage = `Model '${json.model}' not found in available models: ${models.map((m: any) => m.name).join(', ')}`; + console.error(errorMessage); + return new Response(JSON.stringify({ error: errorMessage }), {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
proxy.ts
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
proxy.ts
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
🔇 Additional comments (2)
proxy.ts (2)
Line range hint 1-29
: LGTM! Well-structured class initialization
The class structure is well-documented with proper JSDoc comments and the constructor correctly initializes all required fields.
🧰 Tools
🪛 Biome (1.9.4)
[error] 37-37: void is confusing inside a union type.
Unsafe fix: Use undefined instead.
(lint/suspicious/noConfusingVoidType)
41-42
: LGTM! Proper API key validation
Good addition of API key validation before proceeding with authentication.
proxy.ts
Outdated
private async handlePostRequest(c: any): Promise<Response> { | ||
const json = await c.req.raw.clone().json(); | ||
const { model } = json; | ||
|
||
// Validate the request payload | ||
assert(json, is.ObjectOf({ model: is.String })); | ||
|
||
const endpoint = chooseEndpoint({ | ||
model, | ||
ollamaEndpoint, | ||
openAIEndpoint, | ||
model: json.model, | ||
ollamaEndpoint: this.ollamaEndpoint, | ||
openAIEndpoint: this.openAIEndpoint, | ||
}); | ||
|
||
let res = await this.handleOpenAIModelVerify(c, json); | ||
if (res) { | ||
return res; | ||
} | ||
|
||
const origin = new URL(this.ollamaEndpoint).origin; | ||
const url = convertToCustomEndpoint(c.req.url, parseURL(endpoint)); | ||
const req = new Request(url, c.req.raw); | ||
req.headers.set("Host", ollamaEndpoint); | ||
return fetch(req); | ||
}); | ||
const reqHeaders = this.setCORSHeaders(c.req.raw, origin); | ||
// 设置Origin头, 否则ollama会报错 | ||
reqHeaders.set('Origin', origin); | ||
const req = new Request(url, { | ||
...c.req.raw, | ||
method: "POST", | ||
body: JSON.stringify(json), | ||
headers: reqHeaders, | ||
mode: 'no-cors' | ||
}); | ||
return fetch(req) | ||
} |
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.
🛠️ Refactor suggestion
Improve error handling and documentation
Several improvements could be made to this method:
- Add proper error handling for the JSON parsing
- Translate the Chinese comment to English
- Consider adding request timeout handling
private async handlePostRequest(c: any): Promise<Response> {
- const json = await c.req.raw.clone().json();
- assert(json, is.ObjectOf({ model: is.String }));
+ try {
+ const json = await c.req.raw.clone().json();
+ assert(json, is.ObjectOf({ model: is.String }));
+ } catch (error) {
+ return new Response(
+ JSON.stringify({ error: 'Invalid request format' }),
+ { status: 400, headers: { 'Content-Type': 'application/json' } }
+ );
+ }
- // 设置Origin头, 否则ollama会报错
+ // Set Origin header to prevent Ollama errors
const req = new Request(url, {
...c.req.raw,
method: "POST",
body: JSON.stringify(json),
headers: reqHeaders,
- mode: 'no-cors'
+ mode: 'no-cors',
+ signal: AbortSignal.timeout(30000) // 30 second timeout
});
- return fetch(req)
+ try {
+ return await fetch(req);
+ } catch (error) {
+ return new Response(
+ JSON.stringify({ error: 'Failed to proxy request' }),
+ { status: 502, headers: { 'Content-Type': 'application/json' } }
+ );
+ }
Committable suggestion skipped: line range outside the PR's diff.
Thank you for your hard work. |
OK, I got what you want. |
@jincdream
|
@jincdream |
@jincdream enable |
@jincdream I'm sorry, I force pushed..... |
I accidentally blew up the entire contents of your MAIN branch. |
fixes: #27
This pull request includes significant changes to the
curxy
project, focusing on refactoring the proxy application and updating the project metadata. The most important changes include modifying the project name and version, refactoring the proxy application into a class, and updating utility functions.Project metadata updates:
deno.json
: Changed the project name from@ryoppippi/curxy
to@jinc/curxy
and updated the version from0.1.8
to0.2.0
.Refactoring the proxy application:
main.ts
: ReplacedcreateApp
function import withProxyApp
class import and updated the instantiation and usage accordingly. [1] [2]proxy.ts
: Refactored the proxy application into aProxyApp
class, encapsulating all related methods and properties, and updated the request handling logic.Utility function enhancements:
util.ts
: AddedisCursorRequest
andisOpenAIModel
utility functions and updatedchooseEndpoint
to useisOpenAIModel
. [1] [2]Summary by CodeRabbit
New Features
Refactor
ProxyApp
class.Bug Fixes