-
Notifications
You must be signed in to change notification settings - Fork 836
Add provider-level management for custom models to simplify setup #877
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
Draft
Copilot
wants to merge
2
commits into
master
Choose a base branch
from
copilot/fix-874
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -553,6 +553,23 @@ export const defaultConfig = { | |
active: false, | ||
}, | ||
], | ||
// Provider-level management for OpenAI-compatible APIs | ||
customProviders: [ | ||
{ | ||
id: '', | ||
name: '', | ||
baseUrl: '', | ||
apiKey: '', | ||
active: true, | ||
models: [ | ||
{ | ||
name: '', | ||
displayName: '', | ||
active: true, | ||
}, | ||
], | ||
}, | ||
], | ||
activeSelectionTools: ['translate', 'translateToEn', 'summary', 'polish', 'code', 'ask'], | ||
customSelectionTools: [ | ||
{ | ||
|
@@ -712,6 +729,53 @@ export function isUsingCustomModel(configOrSession) { | |
return isInApiModeGroup(customApiModelKeys, configOrSession) | ||
} | ||
|
||
/** | ||
* Check if a session is using a custom provider model | ||
* @param {object} configOrSession | ||
* @returns {boolean} | ||
*/ | ||
export function isUsingCustomProviderModel(configOrSession) { | ||
return configOrSession.providerId && configOrSession.providerModelName | ||
} | ||
|
||
/** | ||
* Get provider configuration by ID | ||
* @param {UserConfig} config | ||
* @param {string} providerId | ||
* @returns {object|null} | ||
*/ | ||
export function getCustomProvider(config, providerId) { | ||
return config.customProviders?.find((provider) => provider.id === providerId) || null | ||
} | ||
|
||
/** | ||
* Get all active custom providers | ||
* @param {UserConfig} config | ||
* @returns {array} | ||
*/ | ||
export function getActiveCustomProviders(config) { | ||
return config.customProviders?.filter((provider) => provider.active) || [] | ||
} | ||
|
||
/** | ||
* Get all active models from all providers | ||
* @param {UserConfig} config | ||
* @returns {array} Array of {provider, model} objects | ||
*/ | ||
export function getAllActiveProviderModels(config) { | ||
const result = [] | ||
const activeProviders = getActiveCustomProviders(config) | ||
|
||
for (const provider of activeProviders) { | ||
const activeModels = provider.models?.filter((model) => model.active) || [] | ||
for (const model of activeModels) { | ||
result.push({ provider, model }) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
|
@@ -729,11 +793,60 @@ export async function getPreferredLanguageKey() { | |
* get user config from local storage | ||
* @returns {Promise<UserConfig>} | ||
*/ | ||
/** | ||
* Migrate legacy custom model configuration to providers if needed | ||
* @param {UserConfig} config | ||
* @returns {UserConfig} | ||
*/ | ||
function migrateCustomModelToProvider(config) { | ||
// Skip migration if no legacy custom model or already has providers | ||
if (!config.customApiKey || !config.customModelApiUrl || !config.customModelName) { | ||
return config | ||
} | ||
|
||
// Skip if already has custom providers (user has already migrated or set up providers) | ||
if (config.customProviders && config.customProviders.length > 0 && config.customProviders[0].id) { | ||
return config | ||
} | ||
|
||
// Create a provider from the legacy configuration | ||
const legacyProvider = { | ||
id: 'legacy_custom_' + Date.now(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The migration function uses Date.now() for ID generation, which could create duplicate IDs if migration runs multiple times in quick succession. Consider using a more robust ID generation or checking for existing IDs. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
name: 'Legacy Custom Model', | ||
baseUrl: config.customModelApiUrl, | ||
apiKey: config.customApiKey, | ||
active: true, | ||
models: [ | ||
{ | ||
name: config.customModelName, | ||
displayName: config.customModelName, | ||
active: true, | ||
}, | ||
], | ||
} | ||
|
||
return { | ||
...config, | ||
customProviders: [legacyProvider], | ||
} | ||
} | ||
|
||
export async function getUserConfig() { | ||
const options = await Browser.storage.local.get(Object.keys(defaultConfig)) | ||
if (options.customChatGptWebApiUrl === 'https://chat.openai.com') | ||
options.customChatGptWebApiUrl = 'https://chatgpt.com' | ||
return defaults(options, defaultConfig) | ||
|
||
const config = defaults(options, defaultConfig) | ||
|
||
// Run migration for legacy custom models | ||
const migratedConfig = migrateCustomModelToProvider(config) | ||
|
||
// Save migrated config if changes were made | ||
if (JSON.stringify(migratedConfig.customProviders) !== JSON.stringify(config.customProviders)) { | ||
await setUserConfig({ customProviders: migratedConfig.customProviders }) | ||
} | ||
|
||
return migratedConfig | ||
} | ||
|
||
/** | ||
|
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.
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.
The default configuration contains empty strings for required fields like
id
,name
, andbaseUrl
. This could lead to issues when the default is accidentally used. Consider using more descriptive placeholder values or null values that would fail fast if used incorrectly.Copilot uses AI. Check for mistakes.