Repository navigation
Create default llm provider using env variables OPENAI_* - #184
Conversation
Create default llm provider using env variable OPENAI_*
Reviewer's GuideThis PR adds support for a default LLM provider sourced from OPENAI_* environment variables by introducing a new backend endpoint and enhancing the front-end hook to fetch, merge, and expose this provider alongside existing configurations. Sequence diagram for fetching and merging the default LLM providersequenceDiagram
participant Frontend as Frontend (useLLMConfig hook)
participant API as Backend API Server
participant ENV as Environment Variables
Frontend->>API: GET /defaultllmprovider
API->>ENV: Read OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL
ENV-->>API: Return env values
API-->>Frontend: Respond with default provider config (if env vars set)
Frontend->>Frontend: Merge default provider with local providers
Frontend->>Frontend: Expose merged providers to UI
Class diagram for updated useLLMConfig hook and LLMProvider mergingclassDiagram
class useLLMConfig {
- providers: LLMProvider[]
- defaultProvider: LLMProvider | null
+ loadConfig()
+ getEnabledProviders()
+ reload()
+ ...
}
class LLMProvider {
+ id: string
+ name: string
+ apiKey: string
+ baseURL: string
+ model: string
+ enabled: bool
+ config: object
+ models: LLMModel[]
+ settings: object
}
class LLMModel {
+ id: string
+ name: string
+ isCustom: bool
}
useLLMConfig o-- LLMProvider
LLMProvider o-- LLMModel
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @boston008 - I've reviewed your changes - here's some feedback:
- There’s duplicated logic for merging
defaultProviderinto providers across multiple branches inloadConfig—extract that into a helper to DRY up the code. - Currently you fetch
defaultProviderthen triggerloadConfigvia the effect dependency, causing two loads on mount—consider chaining the fetch and load in a single effect to avoid redundant calls. - In the API handler you’re building the provider as a
map[string]interface{}; switching to a concrete struct would improve type safety and simplify JSON marshaling.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- There’s duplicated logic for merging `defaultProvider` into providers across multiple branches in `loadConfig`—extract that into a helper to DRY up the code.
- Currently you fetch `defaultProvider` then trigger `loadConfig` via the effect dependency, causing two loads on mount—consider chaining the fetch and load in a single effect to avoid redundant calls.
- In the API handler you’re building the provider as a `map[string]interface{}`; switching to a concrete struct would improve type safety and simplify JSON marshaling.
## Individual Comments
### Comment 1
<location> `web/src/hooks/useLLMConfig.ts:264` </location>
<code_context>
const getEnabledProviders = useCallback(() => {
- return providers.filter(provider => provider.enabled);
- }, [providers]);
+ const enabled = providers.filter((provider: LLMProvider) => provider?.enabled);
+
+ // Include defaultProvider if it exists and is not already included
</code_context>
<issue_to_address>
Filtering with provider?.enabled may exclude providers with undefined enabled fields.
If undefined should be treated as enabled, use an explicit check. Otherwise, ensure all providers define the 'enabled' field.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
const getEnabledProviders = useCallback(() => {
const enabled = providers.filter((provider: LLMProvider) => provider?.enabled);
// Include defaultProvider if it exists and is not already included
if (defaultProvider && defaultProvider.enabled !== false) {
try {
=======
const getEnabledProviders = useCallback(() => {
const enabled = providers.filter((provider: LLMProvider) => provider?.enabled !== false);
// Include defaultProvider if it exists and is not already included
if (defaultProvider && defaultProvider.enabled !== false) {
try {
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| const getEnabledProviders = useCallback(() => { | ||
| return providers.filter(provider => provider.enabled); | ||
| }, [providers]); | ||
| const enabled = providers.filter((provider: LLMProvider) => provider?.enabled); | ||
|
|
||
| // Include defaultProvider if it exists and is not already included | ||
| if (defaultProvider && defaultProvider.enabled !== false) { | ||
| try { |
There was a problem hiding this comment.
suggestion: Filtering with provider?.enabled may exclude providers with undefined enabled fields.
If undefined should be treated as enabled, use an explicit check. Otherwise, ensure all providers define the 'enabled' field.
| const getEnabledProviders = useCallback(() => { | |
| return providers.filter(provider => provider.enabled); | |
| }, [providers]); | |
| const enabled = providers.filter((provider: LLMProvider) => provider?.enabled); | |
| // Include defaultProvider if it exists and is not already included | |
| if (defaultProvider && defaultProvider.enabled !== false) { | |
| try { | |
| const getEnabledProviders = useCallback(() => { | |
| const enabled = providers.filter((provider: LLMProvider) => provider?.enabled !== false); | |
| // Include defaultProvider if it exists and is not already included | |
| if (defaultProvider && defaultProvider.enabled !== false) { | |
| try { |
| async function fetchDefaultProvider() { | ||
| try { | ||
| const response = await api.get('/defaultllmprovider'); | ||
| const arr = response.data?.configs || response.data?.data || response.data; | ||
| if (Array.isArray(arr) && arr.length > 0) { | ||
| setDefaultProvider(arr[0]); | ||
| } else { | ||
| setDefaultProvider(null); | ||
| } | ||
| } catch (err) { | ||
| setDefaultProvider(null); | ||
| } | ||
| } |
There was a problem hiding this comment.
issue (code-quality): Avoid function declarations, favouring function assignment expressions, inside blocks. (avoid-function-declarations-in-blocks)
Explanation
Function declarations may be hoisted in Javascript, but the behaviour is inconsistent between browsers. Hoisting is generally confusing and should be avoided. Rather than using function declarations inside blocks, you should use function expressions, which create functions in-scope.don't show default llm provider in llm setting page
Create default llm provider using env variables OPENAI_*
Summary by Sourcery
Expose a default LLM provider built from OPENAI_* env vars via a new backend endpoint and integrate it into the frontend LLM configuration hook, merging it with saved or built-in providers.
New Features:
/defaultllmproviderAPI endpoint that derives a default LLM provider from OPENAI_* environment variablesuseLLMConfighook on mountEnhancements:
defaultProviderfrom the hook