forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ feat: support using env variable to set regions for OpenAI Edge Fu…
…nctions. (lobehub#473) * ⚡️ feat: support using env variable to set preferredRegion for OpenAI edge function * fix: fix the import problems * ⚡️ feat: add preferredRegion support for tts and stt * ✅ test: add more unit test for config.ts and server.ts --------- Co-authored-by: vophan <dev@vophan.day>
- Loading branch information
Showing
9 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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,38 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { getPreferredRegion } from './config'; | ||
|
||
// Stub the global process object to safely mock environment variables | ||
vi.stubGlobal('process', { | ||
...process, // Preserve the original process object | ||
env: { ...process.env }, // Clone the environment variables object for modification | ||
}); | ||
|
||
describe('getPreferredRegion', () => { | ||
beforeEach(() => { | ||
// Reset environment variables before each test case | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('returns default value when get config error', () => { | ||
const originalProcess = global.process; | ||
// @ts-ignore | ||
global.process = undefined; | ||
const preferredRegion = getPreferredRegion(); | ||
expect(preferredRegion).toBe('auto'); | ||
|
||
global.process = originalProcess; | ||
}); | ||
|
||
it('return default value when preferredRegion is empty', () => { | ||
process.env.OPENAI_FUNCTION_REGIONS = ''; | ||
const preferredRegion = getPreferredRegion(); | ||
expect(preferredRegion).toBe('auto'); | ||
}); | ||
|
||
it('return correct list values when preferredRegion is correctly passed', () => { | ||
process.env.OPENAI_FUNCTION_REGIONS = 'ida1,sfo1'; | ||
const preferredRegion = getPreferredRegion(); | ||
expect(preferredRegion).toStrictEqual(['ida1', 'sfo1']); | ||
}); | ||
}); |
This file contains 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,15 @@ | ||
import { getServerConfig } from '@/config/server'; | ||
|
||
export const getPreferredRegion = () => { | ||
try { | ||
const cfg = getServerConfig(); | ||
if (cfg.OPENAI_FUNCTION_REGIONS.length <= 0) { | ||
return 'auto'; | ||
} | ||
|
||
return cfg.OPENAI_FUNCTION_REGIONS; | ||
} catch (error) { | ||
console.error('get server config failed, error:', error); | ||
return 'auto'; | ||
} | ||
}; |
This file contains 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 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 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 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 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