Skip to content

Create default llm provider using env variables OPENAI_* - #184

Merged
iFurySt merged 2 commits into
AmoyLab:mainfrom
boston008:env_create_default_llm_provider
Jul 9, 2025
Merged

iFurySt merged 2 commits into
AmoyLab:mainfrom
boston008:env_create_default_llm_provider

Conversation

@boston008

@boston008 boston008 commented Jul 4, 2025 •

Copy link
Copy Markdown
Contributor

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:

  • Expose a /defaultllmprovider API endpoint that derives a default LLM provider from OPENAI_* environment variables
  • Fetch and integrate the default LLM provider into the frontend useLLMConfig hook on mount

Enhancements:

  • Merge the fetched default provider into the existing provider list and enable consistent config/model deduplication
  • Rerun and adapt the LLM config loading logic when the default provider updates and return defaultProvider from the hook

Create default llm provider using env variable OPENAI_*
@sourcery-ai

sourcery-ai Bot commented Jul 4, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This 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 provider

sequenceDiagram
    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
Loading

Class diagram for updated useLLMConfig hook and LLMProvider merging

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Expose default LLM provider via new backend endpoint
  • Register GET /defaultllmprovider in router
  • Implement handler reading OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL
  • Build default provider object with id custom_default, config, models, settings
  • Return provider under "configs" field in JSON
  • Add getEnv helper for reading environment variables
cmd/apiserver/main.go
internal/apiserver/handler/defaultllmprovider.go
Integrate default provider into front-end useLLMConfig hook
  • Import api service and LLMModel type
  • Fetch default provider on mount and store in state
  • Merge or update custom_default in loadConfig paths and error handling
  • Include defaultProvider in useCallback deps and returned hook
  • Extend getEnabledProviders to merge/prepend and dedupe default provider
  • Add defaultProvider to hook API interface
web/src/hooks/useLLMConfig.ts

Possibly linked issues

  • #0: PR implements the issue by adding default LLM provider using OPENAI env vars.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @boston008 - I've reviewed your changes - here's some feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 263 to +268
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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 {

Comment on lines +28 to +40
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);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid function declarations, favouring function assignment expressions, inside blocks. (avoid-function-declarations-in-blocks)

ExplanationFunction 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

@iFurySt iFurySt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@iFurySt
iFurySt merged commit a32649a into AmoyLab:main Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants