|
| 1 | +import * as React from 'react'; |
| 2 | +import { Map as ImmutableMap } from 'immutable'; |
| 3 | +import { useSelector } from 'react-redux'; |
| 4 | +import { State } from '../redux-reducers'; |
| 5 | +import { useToolUIMapping } from '../hooks/useToolUIMapping'; |
| 6 | +import type { OlsToolUIComponent, Tool } from '../types'; |
| 7 | + |
| 8 | +type OlsToolUIProps = { |
| 9 | + tool: Tool; |
| 10 | + toolUIComponent: OlsToolUIComponent; |
| 11 | +}; |
| 12 | + |
| 13 | +export const OlsToolUI: React.FC<OlsToolUIProps> = ({ tool, toolUIComponent: toolUIElement }) => { |
| 14 | + const ToolComponent = toolUIElement; |
| 15 | + return <ToolComponent tool={tool} />; |
| 16 | +}; |
| 17 | + |
| 18 | +type OlsUIToolsProps = { |
| 19 | + entryIndex: number; |
| 20 | +}; |
| 21 | + |
| 22 | +export const OlsToolUIs: React.FC<OlsUIToolsProps> = ({ entryIndex }) => { |
| 23 | + const [toolUIMapping] = useToolUIMapping(); |
| 24 | + |
| 25 | + const toolsData: ImmutableMap<string, ImmutableMap<string, unknown>> = useSelector((s: State) => |
| 26 | + s.plugins?.ols?.getIn(['chatHistory', entryIndex, 'tools']), |
| 27 | + ); |
| 28 | + |
| 29 | + const olsToolsWithUI = toolsData |
| 30 | + .map((value) => { |
| 31 | + const tool = value.toJS() as Tool; |
| 32 | + const olsUiID = tool.tool_meta?.olsUi?.id; |
| 33 | + const toolUIComponent = olsUiID && toolUIMapping[olsUiID]; |
| 34 | + return { tool, toolUIComponent }; |
| 35 | + }) |
| 36 | + .filter(({ toolUIComponent }) => !!toolUIComponent); |
| 37 | + |
| 38 | + return ( |
| 39 | + <> |
| 40 | + {olsToolsWithUI |
| 41 | + .map(({ tool, toolUIComponent }, toolID) => ( |
| 42 | + <OlsToolUI key={`ols-app-${toolID}`} tool={tool} toolUIComponent={toolUIComponent} /> |
| 43 | + )) |
| 44 | + .valueSeq()} |
| 45 | + </> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +export default OlsToolUIs; |
0 commit comments