Skip to content

Commit

Permalink
add Delete Config button to chatError widget
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-i committed Nov 7, 2023
1 parent 278e051 commit 003a721
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
15 changes: 15 additions & 0 deletions packages/jupyter-ai/jupyter_ai/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ def update_config(self, config_update: UpdateConfigRequest):
Merger.merge(config_dict, config_update.dict(exclude_unset=True))
self._write_config(GlobalConfig(**config_dict))

def delete_config(self):
try:
if os.path.exists(self.config_path):
os.remove(self.config_path)
self.log.info(f"Configutation file {self.config_path} has been deleted")
self._config = None
self._last_read = None
else:
self.log.warning(
f"Configuration file {self.config_path} does not exist"
)
except Exception as e:
self.log.warning(f"Failed to delete configuration file {self.config_path}: {e}")
raise

# this cannot be a property, as the parent Configurable already defines the
# self.config attr.
def get_config(self):
Expand Down
10 changes: 10 additions & 0 deletions packages/jupyter-ai/jupyter_ai/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ def post(self):
500, "Unexpected error occurred while updating the config."
) from e

@web.authenticated
def delete(self):
print("\n\n *** \n attempting to delete")
try:
self.config_manager.delete_config()
self.set_status(204)
self.finish()
except Exception as e:
raise HTTPError(500, str(e))


class ApiKeysHandler(BaseAPIHandler):
@property
Expand Down
4 changes: 4 additions & 0 deletions packages/jupyter-ai/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export namespace AiService {
});
}

export async function deleteConfig(): Promise<void> {
return requestAPI<void>('config', { method: 'DELETE' });
}

export async function deleteApiKey(keyName: string): Promise<void> {
return requestAPI<void>(`api_keys/${keyName}`, {
method: 'DELETE'
Expand Down
3 changes: 2 additions & 1 deletion packages/jupyter-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { buildChatSidebar } from './widgets/chat-sidebar';
import { SelectionWatcher } from './selection-watcher';
import { ChatHandler } from './chat_handler';
import { buildErrorWidget } from './widgets/chat-error';
import { AiService } from './handler';

export type DocumentTracker = IWidgetTracker<IDocumentWidget>;

Expand Down Expand Up @@ -46,7 +47,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
globalAwareness
);
} catch (e) {
chatWidget = buildErrorWidget();
chatWidget = buildErrorWidget(AiService.deleteConfig);
}

/**
Expand Down
16 changes: 13 additions & 3 deletions packages/jupyter-ai/src/widgets/chat-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import React from 'react';
import { ReactWidget } from '@jupyterlab/apputils';

import { chatIcon } from '../icons';
import { Alert, Box } from '@mui/material';
import { Alert, Box, Button } from '@mui/material';
import { JlThemeProvider } from '../components/jl-theme-provider';

export function buildErrorWidget(): ReactWidget {
export function buildErrorWidget(
deleteConfig: () => Promise<void>
): ReactWidget {
const ErrorWidget = ReactWidget.create(
<JlThemeProvider>
<Box
Expand All @@ -22,8 +24,16 @@ export function buildErrorWidget(): ReactWidget {
<Alert severity="error">
There seems to be a problem with the Chat backend, please look at
the JupyterLab server logs or contact your administrator to correct
this problem.
this problem. Deleting the config file
`$JUPYTER_DATA_DIR/jupyter_ai/config.json` to some other name and
then restarting `jupyter lab` may fix the issue if it is a result of
the config changes.
</Alert>
<Box sx={{ marginTop: 2 }}>
<Button variant="contained" onClick={deleteConfig}>
Delete Config File
</Button>
</Box>
</Box>
</Box>
</JlThemeProvider>
Expand Down

0 comments on commit 003a721

Please sign in to comment.