Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 3 additions & 61 deletions src/popup/sections/ApiModes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
getProviderReferenceCheckApiModes,
getReferencedCustomProviderIdsFromSessions,
getSelectableProviders,
isProviderEndpointRewriteBlockedBySavedConversations,
isProviderDeleteDisabled,
isProviderReferencedByApiModes,
loadSavedConversationState,
Expand Down Expand Up @@ -71,7 +70,6 @@ const defaultProviderDraft = {
const defaultProviderDraftValidation = {
name: false,
apiUrl: false,
savedConversations: false,
}

export function ApiModes({ config, updateConfig }) {
Expand Down Expand Up @@ -203,27 +201,6 @@ export function ApiModes({ config, updateConfig }) {
return getConfiguredCustomApiModesForSessionRecovery(recoveryApiModes, recoverySelectedApiMode)
}, [apiModes, config.apiMode, editing, editingApiMode, editingIndex])

const configuredCustomApiModesForSaveGuard = useMemo(() => {
let nextApiModes = apiModes
if (editing && editingIndex !== -1) {
nextApiModes = apiModes.map((apiMode, index) =>
index === editingIndex ? editingApiMode : apiMode,
)
} else if (
editing &&
editingIndex === -1 &&
editingApiMode.groupName === 'customApiModelKeys'
) {
nextApiModes = [...apiModes, editingApiMode]
}
const nextSelectedApiMode =
editing && editingIndex !== -1 && isApiModeSelected(apiModes[editingIndex], config)
? editingApiMode
: config.apiMode

return getConfiguredCustomApiModesForSessionRecovery(nextApiModes, nextSelectedApiMode)
}, [apiModes, config, editing, editingApiMode, editingIndex])

const sessionReferencedProviderIds = useMemo(
() =>
getReferencedCustomProviderIdsFromSessions(
Expand Down Expand Up @@ -314,37 +291,13 @@ export function ApiModes({ config, updateConfig }) {
pendingNewProvider && pendingNewProvider.id === providerEditingId
? pendingNewProvider
: selectedCustomProvider || {}
const persistedProvider = customProviders.find((provider) => provider.id === providerEditingId)
const endpointDraft = validateProviderEndpointDraft(providerDraft.apiUrl)
const parsedEndpoint = endpointDraft.parsedEndpoint
const providerEndpointChanged =
Boolean(providerEditingId) &&
Boolean(persistedProvider) &&
parsedEndpoint.valid &&
parsedEndpoint.chatCompletionsUrl !== resolveProviderChatEndpointUrl(persistedProvider)
const effectiveProviderSecrets =
pendingDeletedProviderSecretIds.length > 0
? applyDeletedProviderSecrets(config.providerSecrets, pendingDeletedProviderSecretIds)
: config.providerSecrets
const nextProviderDraftValidation = {
name: !providerName,
apiUrl: !endpointDraft.valid,
savedConversations:
providerEndpointChanged &&
isProviderEndpointRewriteBlockedBySavedConversations(
providerEditingId,
sessionsLoaded,
sessions,
effectiveProviders,
configuredCustomApiModesForSaveGuard,
effectiveProviderSecrets,
),
}
if (
nextProviderDraftValidation.name ||
nextProviderDraftValidation.apiUrl ||
nextProviderDraftValidation.savedConversations
) {
if (nextProviderDraftValidation.name || nextProviderDraftValidation.apiUrl) {
setProviderDraftValidation(nextProviderDraftValidation)
if (nextProviderDraftValidation.name) {
providerNameInputRef.current?.focus()
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Expand Down Expand Up @@ -602,11 +555,10 @@ export function ApiModes({ config, updateConfig }) {
placeholder={t('Provider')}
onChange={(e) => {
setProviderDraft({ ...providerDraft, name: e.target.value })
if (providerDraftValidation.name || providerDraftValidation.savedConversations) {
if (providerDraftValidation.name) {
setProviderDraftValidation({
...providerDraftValidation,
name: false,
savedConversations: false,
})
}
}}
Expand All @@ -621,11 +573,10 @@ export function ApiModes({ config, updateConfig }) {
title={t('API Url')}
onChange={(e) => {
setProviderDraft({ ...providerDraft, apiUrl: e.target.value })
if (providerDraftValidation.apiUrl || providerDraftValidation.savedConversations) {
if (providerDraftValidation.apiUrl) {
setProviderDraftValidation({
...providerDraftValidation,
apiUrl: false,
savedConversations: false,
})
}
}}
Expand All @@ -635,15 +586,6 @@ export function ApiModes({ config, updateConfig }) {
{providerDraftValidation.apiUrl && (
<div style={{ color: 'red' }}>{t('Please enter a full Chat Completions URL')}</div>
)}
{providerDraftValidation.savedConversations && (
<div style={{ color: 'red' }}>
{t(
sessionsLoaded
? 'This provider endpoint is still needed by saved conversations'
: 'Loading saved conversations…',
)}
</div>
)}
<div
style={{
display: 'grid',
Expand Down
166 changes: 0 additions & 166 deletions src/popup/sections/api-modes-provider-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -478,28 +478,6 @@ function getProvidersMatchingSessionProviderId(providers = [], providerId = '')
)
}

function isProviderReferencedBySessionsViaProviderId(providerId, sessions = [], providers = []) {
const normalizedTargetProviderId = normalizeText(providerId)
if (!normalizedTargetProviderId || normalizedTargetProviderId === 'legacy-custom-default') {
return false
}

for (const session of Array.isArray(sessions) ? sessions : []) {
if (normalizeText(session?.apiMode?.groupName) !== 'customApiModelKeys') continue

const sessionProviderId = normalizeText(session?.apiMode?.providerId)
if (!sessionProviderId || sessionProviderId === 'legacy-custom-default') continue

const matchedByProviderId = getProvidersMatchingSessionProviderId(providers, sessionProviderId)
const matchesTargetProvider = matchedByProviderId.some(
(provider) => normalizeText(provider?.id) === normalizedTargetProviderId,
)
if (matchesTargetProvider) return true
}

return false
}

function getProviderIdsMatchingSessionLabel(session = null, providers = [], apiModes = []) {
if (normalizeText(session?.apiMode?.groupName) !== 'customApiModelKeys') return []

Expand Down Expand Up @@ -541,42 +519,6 @@ function getProviderIdsMatchingSessionLabel(session = null, providers = [], apiM
)
}

function canSessionRecoverViaLegacyLabelFallback(session = null, apiModes = []) {
if (normalizeText(session?.apiMode?.groupName) !== 'customApiModelKeys') return false

const normalizedSessionLabel = {
groupName: normalizeText(session?.apiMode?.groupName),
itemName: normalizeText(session?.apiMode?.itemName),
isCustom: Boolean(session?.apiMode?.isCustom),
customName: normalizeText(session?.apiMode?.customName),
}
if (!normalizedSessionLabel.customName) return false

const allCandidates = (Array.isArray(apiModes) ? apiModes : []).filter((apiMode) => {
if (!apiMode || typeof apiMode !== 'object') return false
return (
normalizeText(apiMode.groupName) === normalizedSessionLabel.groupName &&
normalizeText(apiMode.customName) === normalizedSessionLabel.customName
)
})
const exactCandidates = allCandidates.filter(
(apiMode) =>
normalizeText(apiMode?.itemName) === normalizedSessionLabel.itemName &&
Boolean(apiMode?.isCustom) === normalizedSessionLabel.isCustom,
)
const matchedApiModes = exactCandidates.length === 1 ? exactCandidates : []
const isLegacyCustomShape = !normalizedSessionLabel.itemName
const fallbackApiModes =
matchedApiModes.length === 0 && isLegacyCustomShape && allCandidates.length === 1
? allCandidates
: matchedApiModes

return fallbackApiModes
.map((apiMode) => normalizeProviderId(apiMode?.providerId))
.filter((providerId, index, providerIds) => providerIds.indexOf(providerId) === index)
.includes('legacy-custom-default')
}

export function getReferencedCustomProviderIdsFromSessions(
sessions = [],
providers = [],
Expand Down Expand Up @@ -627,114 +569,6 @@ export function getReferencedCustomProviderIdsFromSessions(
return Array.from(referencedProviderIds)
}

export function isProviderReferencedBySessionsViaUrl(
providerId,
sessions = [],
providers = [],
apiModes = [],
providerSecrets = {},
) {
const normalizedTargetProviderId = normalizeText(providerId)
if (!normalizedTargetProviderId || normalizedTargetProviderId === 'legacy-custom-default') {
return false
}

for (const session of Array.isArray(sessions) ? sessions : []) {
if (normalizeText(session?.apiMode?.groupName) !== 'customApiModelKeys') continue

const matchedByProviderId = getProvidersMatchingSessionProviderId(
providers,
session?.apiMode?.providerId,
)
if (matchedByProviderId.length > 0) continue

const matchedByCustomUrl = getProvidersMatchingLegacySessionUrl(providers, session)
const matchesTargetByCustomUrl = matchedByCustomUrl.some(
(provider) => normalizeText(provider?.id) === normalizedTargetProviderId,
)
if (!matchesTargetByCustomUrl) continue
const sessionApiKey =
session?.apiMode &&
typeof session.apiMode === 'object' &&
typeof session.apiMode.apiKey === 'string'
? session.apiMode.apiKey.trim()
: ''
if (matchedByCustomUrl.length > 1 && sessionApiKey) {
const matchedBySessionKey = matchedByCustomUrl.filter((provider) => {
if (!provider || typeof provider !== 'object') return false
const providerSecretValue =
providerSecrets && typeof providerSecrets === 'object' ? providerSecrets[provider.id] : ''
return String(providerSecretValue || '').trim() === sessionApiKey
})
if (
matchedBySessionKey.length === 1 &&
normalizeText(matchedBySessionKey[0]?.id) !== normalizedTargetProviderId
) {
continue
}
}

const matchedByLabel = getProviderIdsMatchingSessionLabel(session, providers, apiModes)
if (matchedByLabel.length > 0) continue
if (canSessionRecoverViaLegacyLabelFallback(session, apiModes)) continue

return true
}

return false
}

export function isProviderEndpointRewriteBlockedBySavedConversations(
providerId,
sessionsLoaded = true,
sessions = [],
providers = [],
apiModes = [],
providerSecrets = {},
) {
if (!sessionsLoaded) return true

const normalizedTargetProviderId = normalizeProviderId(providerId)
const isProviderReferencedBySessionsViaLabel = () => {
if (!normalizedTargetProviderId || normalizedTargetProviderId === 'legacy-custom-default') {
return false
}

for (const session of Array.isArray(sessions) ? sessions : []) {
if (normalizeText(session?.apiMode?.groupName) !== 'customApiModelKeys') continue
if (
getProvidersMatchingSessionProviderId(providers, session?.apiMode?.providerId).length > 0
) {
continue
}
if (getProvidersMatchingLegacySessionUrl(providers, session).length > 0) {
continue
}
if (
getProviderIdsMatchingSessionLabel(session, providers, apiModes).includes(
normalizedTargetProviderId,
)
) {
return true
}
}

return false
}

return (
isProviderReferencedBySessionsViaProviderId(providerId, sessions, providers) ||
isProviderReferencedBySessionsViaUrl(
providerId,
sessions,
providers,
apiModes,
providerSecrets,
) ||
isProviderReferencedBySessionsViaLabel()
)
}

export function getApiModeDisplayLabel(apiMode, t, providers = []) {
const modelName = apiModeToModelName(apiMode)
const fallbackLabel = modelNameToDesc(modelName, t)
Expand Down
Loading