forked from microsoft/semanticworkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds support for export / import of assistant configs, including part…
…ials imports, in json and yaml (microsoft#129)
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
workbench-app/src/components/Assistants/AssistantConfigExportButton.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Menu, MenuItem, MenuList, MenuPopover, MenuTrigger, SplitButton } from '@fluentui/react-components'; | ||
import YAML from 'js-yaml'; | ||
import React from 'react'; | ||
|
||
interface AssistantConfigExportButtonProps { | ||
config: object; | ||
assistantId: string; | ||
} | ||
|
||
export const AssistantConfigExportButton: React.FC<AssistantConfigExportButtonProps> = ({ config, assistantId }) => { | ||
const exportConfig = (format: 'json' | 'yaml') => { | ||
let content = ''; | ||
let filename = `config_${assistantId}`; | ||
|
||
try { | ||
if (format === 'yaml') { | ||
content = YAML.dump(config); | ||
filename += '.yaml'; | ||
} else { | ||
content = JSON.stringify(config, null, 2); | ||
filename += '.json'; | ||
} | ||
|
||
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' }); | ||
const url = URL.createObjectURL(blob); | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = filename; | ||
a.click(); | ||
|
||
URL.revokeObjectURL(url); | ||
} catch (error) { | ||
console.error('Error while generating config file:', error); | ||
} | ||
}; | ||
|
||
const primaryActionButtonProps = { | ||
onClick: () => exportConfig('json'), | ||
}; | ||
|
||
return ( | ||
<Menu positioning="below-end"> | ||
<MenuTrigger disableButtonEnhancement> | ||
{(triggerProps) => ( | ||
<SplitButton menuButton={triggerProps} primaryActionButton={primaryActionButtonProps}> | ||
Export Config | ||
</SplitButton> | ||
)} | ||
</MenuTrigger> | ||
|
||
<MenuPopover> | ||
<MenuList> | ||
<MenuItem onClick={() => exportConfig('json')}>JSON Format</MenuItem> | ||
<MenuItem onClick={() => exportConfig('yaml')}>YAML Format</MenuItem> | ||
</MenuList> | ||
</MenuPopover> | ||
</Menu> | ||
); | ||
}; |
59 changes: 59 additions & 0 deletions
59
workbench-app/src/components/Assistants/AssistantConfigImportButton.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Button } from '@fluentui/react-components'; | ||
import YAML from 'js-yaml'; | ||
import React, { useRef } from 'react'; | ||
import { useAppDispatch } from '../../redux/app/hooks'; // Import the relevant hooks | ||
import { addError } from '../../redux/features/app/appSlice'; // Import the error action | ||
|
||
interface AssistantConfigImportButtonProps { | ||
onImport: (config: object) => void; | ||
} | ||
|
||
export const AssistantConfigImportButton: React.FC<AssistantConfigImportButtonProps> = ({ onImport }) => { | ||
const fileInputRef = useRef<HTMLInputElement | null>(null); | ||
const dispatch = useAppDispatch(); // Use the dispatch hook | ||
|
||
const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target?.files?.[0]; | ||
if (file) { | ||
const reader = new FileReader(); | ||
reader.onload = (e) => { | ||
try { | ||
const content = e.target?.result as string; | ||
let importedConfig; | ||
|
||
if (file.name.endsWith('.yaml') || file.name.endsWith('.yml')) { | ||
importedConfig = YAML.load(content); | ||
} else { | ||
importedConfig = JSON.parse(content); | ||
} | ||
|
||
if (typeof importedConfig === 'object' && importedConfig !== null) { | ||
onImport(importedConfig as object); | ||
} else { | ||
throw new Error('Invalid configuration format'); | ||
} | ||
} catch (error) { | ||
console.error('Error reading configuration file:', error); | ||
dispatch( | ||
addError({ | ||
title: 'Import Error', | ||
message: | ||
'There was an error importing the configuration file. Please check the file format and contents.', | ||
}), | ||
); | ||
} | ||
}; | ||
reader.readAsText(file); | ||
} | ||
if (fileInputRef.current) { | ||
fileInputRef.current.value = ''; | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input type="file" hidden ref={fileInputRef} accept=".json,.yaml,.yml" onChange={handleFileChange} /> | ||
<Button onClick={() => fileInputRef.current?.click()}>Import Config</Button> | ||
</div> | ||
); | ||
}; |
This file contains 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