File tree Expand file tree Collapse file tree
apps/sim/providers/openai-compat Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,4 +10,13 @@ describe('getOpenAICompatibleApiBaseUrl', () => {
1010 ] ) ( 'normalizes %s to %s' , ( input , expected ) => {
1111 expect ( getOpenAICompatibleApiBaseUrl ( input ) ) . toBe ( expected )
1212 } )
13+
14+ it . each ( [ 'http://localhost:8000?token=value' , 'http://localhost:8000#models' ] ) (
15+ 'rejects unsupported URL components in %s' ,
16+ ( input ) => {
17+ expect ( ( ) => getOpenAICompatibleApiBaseUrl ( input ) ) . toThrow (
18+ 'OpenAI-compatible base URL must not include query parameters or a fragment'
19+ )
20+ }
21+ )
1322} )
Original file line number Diff line number Diff line change 11/**
22 * Normalizes a server root or versioned OpenAI-compatible URL to the `/v1` API base.
3+ *
4+ * @throws When the URL contains query parameters or a fragment.
35 */
46export function getOpenAICompatibleApiBaseUrl ( baseUrl : string ) : string {
5- const normalized = baseUrl . trim ( ) . replace ( / \/ + $ / , '' )
6- return normalized . endsWith ( '/v1' ) ? normalized : `${ normalized } /v1`
7+ const url = new URL ( baseUrl . trim ( ) )
8+ if ( url . search || url . hash ) {
9+ throw new Error ( 'OpenAI-compatible base URL must not include query parameters or a fragment' )
10+ }
11+
12+ const pathname = url . pathname . replace ( / \/ + $ / , '' )
13+ url . pathname = pathname . endsWith ( '/v1' ) ? pathname : `${ pathname } /v1`
14+ return url . toString ( )
715}
You can’t perform that action at this time.
0 commit comments