Skip to content

Commit

Permalink
Fix saving chat settings (#935)
Browse files Browse the repository at this point in the history
* fix settings save

* show placeholder message when API keys section is empty
  • Loading branch information
dlqqq authored Aug 6, 2024
1 parent 4d69962 commit 7531f42
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions packages/jupyter-ai/src/components/chat-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,31 @@ export function ChatSettings(props: ChatSettingsProps): JSX.Element {
const newApiKeys: Record<string, string> = {};
const lmAuth = lmProvider?.auth_strategy;
const emAuth = emProvider?.auth_strategy;
if (lmAuth?.type === 'env') {
if (
lmAuth?.type === 'env' &&
!server.config.api_keys.includes(lmAuth.name)
) {
newApiKeys[lmAuth.name] = '';
}
if (lmAuth?.type === 'multienv') {
lmAuth.names.forEach(apiKey => {
newApiKeys[apiKey] = '';
if (!server.config.api_keys.includes(apiKey)) {
newApiKeys[apiKey] = '';
}
});
}
if (emAuth?.type === 'env') {

if (
emAuth?.type === 'env' &&
!server.config.api_keys.includes(emAuth.name)
) {
newApiKeys[emAuth.name] = '';
}
if (emAuth?.type === 'multienv') {
emAuth.names.forEach(apiKey => {
newApiKeys[apiKey] = '';
if (!server.config.api_keys.includes(apiKey)) {
newApiKeys[apiKey] = '';
}
});
}

Expand Down Expand Up @@ -471,28 +482,28 @@ export function ChatSettings(props: ChatSettingsProps): JSX.Element {

{/* API Keys section */}
<h2 className="jp-ai-ChatSettings-header">API Keys</h2>

{Object.entries(apiKeys).length === 0 &&
server.config.api_keys.length === 0 ? (
<p>No API keys are required by the selected models.</p>
) : null}

{/* API key inputs for newly-used providers */}
{Object.entries(apiKeys).length === 0 ? (
<p>No API Keys needed for selected model.</p>
) : (
Object.entries(apiKeys).map(([apiKeyName, apiKeyValue], idx) =>
!server.config.api_keys.includes(apiKeyName) ? (
<TextField
key={idx}
label={apiKeyName}
value={apiKeyValue}
fullWidth
type="password"
onChange={e =>
setApiKeys(apiKeys => ({
...apiKeys,
[apiKeyName]: e.target.value
}))
}
/>
) : null
)
)}
{Object.entries(apiKeys).map(([apiKeyName, apiKeyValue], idx) => (
<TextField
key={idx}
label={apiKeyName}
value={apiKeyValue}
fullWidth
type="password"
onChange={e =>
setApiKeys(apiKeys => ({
...apiKeys,
[apiKeyName]: e.target.value
}))
}
/>
))}
{/* Pre-existing API keys */}
<ExistingApiKeys
alert={apiKeysAlert}
Expand Down

0 comments on commit 7531f42

Please sign in to comment.