-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add custom base URL support for Requesty provider #7337
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04cf0c2
fix: update tests to match new getRequestyModels signature with baseU…
daniel-lxs 29d782f
fix: changing base URL for fetching models
requesty-JohnCosta27 0bfa63a
added configurations for base url api keys
Thibault00 a47fcd6
Update webview-ui/src/components/ui/hooks/useRequestyKeyInfo.ts
requesty-JohnCosta27 c5e918b
test: add comprehensive test coverage for toRequestyServiceUrl
daniel-lxs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { toRequestyServiceUrl } from "../requesty" | ||
|
|
||
| describe("toRequestyServiceUrl", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| // Mock console.warn to avoid noise in test output | ||
| vi.spyOn(console, "warn").mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| describe("with default parameters", () => { | ||
| it("should return default router URL when no baseUrl provided", () => { | ||
| const result = toRequestyServiceUrl() | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
|
|
||
| it("should return default router URL when baseUrl is undefined", () => { | ||
| const result = toRequestyServiceUrl(undefined) | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
|
|
||
| it("should return default router URL when baseUrl is empty string", () => { | ||
| const result = toRequestyServiceUrl("") | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
| }) | ||
|
|
||
| describe("with custom baseUrl", () => { | ||
| it("should use custom baseUrl for router service", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai/v1") | ||
| expect(result).toBe("https://custom.requesty.ai/v1") | ||
| }) | ||
|
|
||
| it("should handle baseUrl with trailing slash", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai/v1/") | ||
| expect(result).toBe("https://custom.requesty.ai/v1/") | ||
| }) | ||
|
|
||
| it("should handle baseUrl without path", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai") | ||
| expect(result).toBe("https://custom.requesty.ai/") | ||
| }) | ||
|
|
||
| it("should handle localhost URLs", () => { | ||
| const result = toRequestyServiceUrl("http://localhost:8080/v1") | ||
| expect(result).toBe("http://localhost:8080/v1") | ||
| }) | ||
|
|
||
| it("should handle URLs with ports", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai:3000/v1") | ||
| expect(result).toBe("https://custom.requesty.ai:3000/v1") | ||
| }) | ||
| }) | ||
|
|
||
| describe("with different service types", () => { | ||
| it("should return router URL for router service", () => { | ||
| const result = toRequestyServiceUrl("https://router.requesty.ai/v1", "router") | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
|
|
||
| it("should replace router with app and remove v1 for app service", () => { | ||
| const result = toRequestyServiceUrl("https://router.requesty.ai/v1", "app") | ||
| expect(result).toBe("https://app.requesty.ai/") | ||
| }) | ||
|
|
||
| it("should replace router with api and remove v1 for api service", () => { | ||
| const result = toRequestyServiceUrl("https://router.requesty.ai/v1", "api") | ||
| expect(result).toBe("https://api.requesty.ai/") | ||
| }) | ||
|
|
||
| it("should handle custom baseUrl with app service", () => { | ||
| const result = toRequestyServiceUrl("https://router.custom.ai/v1", "app") | ||
| expect(result).toBe("https://app.custom.ai/") | ||
| }) | ||
|
|
||
| it("should handle URLs where router appears multiple times", () => { | ||
| const result = toRequestyServiceUrl("https://router.router-requesty.ai/v1", "app") | ||
| // This will replace the first occurrence only | ||
| expect(result).toBe("https://app.router-requesty.ai/") | ||
| }) | ||
| }) | ||
|
|
||
| describe("error handling", () => { | ||
| it("should fall back to default URL for invalid baseUrl", () => { | ||
| const result = toRequestyServiceUrl("not-a-valid-url") | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| expect(console.warn).toHaveBeenCalledWith('Invalid base URL "not-a-valid-url", falling back to default') | ||
| }) | ||
|
|
||
| it("should fall back to default URL for malformed URL", () => { | ||
| const result = toRequestyServiceUrl("ht!tp://[invalid") | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| expect(console.warn).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should fall back to default app URL for invalid baseUrl with app service", () => { | ||
| const result = toRequestyServiceUrl("invalid-url", "app") | ||
| expect(result).toBe("https://app.requesty.ai/") | ||
| expect(console.warn).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should handle null baseUrl gracefully", () => { | ||
| const result = toRequestyServiceUrl(null as any) | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
|
|
||
| it("should handle non-string baseUrl gracefully", () => { | ||
| const result = toRequestyServiceUrl(123 as any) | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| }) | ||
| }) | ||
|
|
||
| describe("edge cases", () => { | ||
| it("should handle protocol-relative URLs by falling back to default", () => { | ||
| const result = toRequestyServiceUrl("//custom.requesty.ai/v1") | ||
| // Protocol-relative URLs are not valid for URL constructor, will fall back | ||
| expect(result).toBe("https://router.requesty.ai/v1") | ||
| expect(console.warn).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should preserve query parameters", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai/v1?key=value") | ||
| expect(result).toBe("https://custom.requesty.ai/v1?key=value") | ||
| }) | ||
|
|
||
| it("should preserve URL fragments", () => { | ||
| const result = toRequestyServiceUrl("https://custom.requesty.ai/v1#section") | ||
| expect(result).toBe("https://custom.requesty.ai/v1#section") | ||
| }) | ||
|
|
||
| it("should handle URLs with authentication", () => { | ||
| const result = toRequestyServiceUrl("https://user:[email protected]/v1") | ||
| expect(result).toBe("https://user:[email protected]/v1") | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const REQUESTY_BASE_URL = "https://router.requesty.ai/v1" | ||
|
|
||
| type URLType = "router" | "app" | "api" | ||
|
|
||
| /** | ||
| * Replaces the service type in the URL (router -> app/api) and removes version suffix for non-router services | ||
| * @param baseUrl The base URL to transform | ||
| * @param type The service type to use | ||
| * @returns The transformed URL | ||
| */ | ||
| const replaceCname = (baseUrl: string, type: URLType): string => { | ||
| if (type === "router") { | ||
| return baseUrl | ||
| } | ||
|
|
||
| // Parse the URL to safely replace the subdomain | ||
| try { | ||
| const url = new URL(baseUrl) | ||
| // Replace 'router' in the hostname with the service type | ||
| if (url.hostname.includes("router")) { | ||
| url.hostname = url.hostname.replace("router", type) | ||
| } | ||
| // Remove '/v1' from the pathname for non-router services | ||
| if (url.pathname.endsWith("/v1")) { | ||
| url.pathname = url.pathname.slice(0, -3) | ||
| } | ||
| return url.toString() | ||
| } catch { | ||
| // Fallback to simple string replacement if URL parsing fails | ||
| return baseUrl.replace("router", type).replace("/v1", "") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a base URL to a Requesty service URL with proper validation and fallback | ||
| * @param baseUrl Optional custom base URL. Falls back to default if invalid or not provided | ||
| * @param service The service type (router, app, or api). Defaults to 'router' | ||
| * @returns A valid Requesty service URL | ||
| */ | ||
| export const toRequestyServiceUrl = (baseUrl?: string | null, service: URLType = "router"): string => { | ||
| // Handle null, undefined, empty string, or non-string values | ||
| const urlToUse = baseUrl && typeof baseUrl === "string" && baseUrl.trim() ? baseUrl.trim() : REQUESTY_BASE_URL | ||
|
|
||
| try { | ||
| // Validate the URL first | ||
| const validatedUrl = new URL(urlToUse).toString() | ||
| // Apply service type transformation | ||
| return replaceCname(validatedUrl, service) | ||
| } catch (error) { | ||
| // If the provided baseUrl is invalid, fall back to the default | ||
| if (baseUrl && baseUrl !== REQUESTY_BASE_URL) { | ||
| console.warn(`Invalid base URL "${baseUrl}", falling back to default`) | ||
| } | ||
| return replaceCname(REQUESTY_BASE_URL, service) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a specific reason for changing the parameter order to (baseUrl?, apiKey?) instead of keeping apiKey as the first parameter? Most other provider functions have apiKey as the primary parameter. This could be confusing for consistency.