Skip to content

Commit

Permalink
Fixing initial load of the bots
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino committed Jan 7, 2025
1 parent 220055a commit 49dcf05
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
7 changes: 7 additions & 0 deletions webapp/src/ai_integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export const useBotSelector = () => {
});
};

export const useBotsLoaderHook = () => {
return useSelector<GlobalState, any[]>((state) => {
//@ts-ignore plugins state is a thing
return state['plugins-' + aiPluginID]?.botsLoaderHook || (() => null);
});
};

26 changes: 13 additions & 13 deletions webapp/src/components/modals/ai_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import IconAI from 'src/components/assets/icons/ai';
import {Textbox} from 'src/webapp_globals';

import {generateStatusUpdate} from 'src/client';
import {useAIAvailableBots, useBotSelector} from 'src/ai_integration';
import {useAIAvailableBots, useBotSelector, useBotsLoaderHook} from 'src/ai_integration';

import postEventListener, {PostUpdateWebsocketMessage} from 'src/websocket';

Expand All @@ -30,19 +30,19 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
const [copied, setCopied] = useState(false);
const [instruction, setInstruction] = useState('');
const suggestionBox = useRef<HTMLDivElement>()
const aiAvailableBots = useAIAvailableBots();
const BotSelector = useBotSelector() as any;
const [currentBot, setCurrentBot] = useState<any>(aiAvailableBots.length > 0 ? aiAvailableBots[0] : null);
const useBotlist = useBotsLoaderHook() as any;
const {bots, activeBot, setActiveBot} = useBotlist()
const [currentVersion, setCurrentVersion] = useState<number>(0);
const [versions, setVersions] = useState<Version[]>([]);
const [generating, setGenerating] = useState<any>(null);

useEffect(() => {
if (currentBot?.id && isOpen) {
if (activeBot?.id && isOpen) {
setCurrentVersion(versions.length + 1)
setVersions([...versions, {instruction: '', value: '', prevValue: ''}])
setGenerating(true);
generateStatusUpdate(playbookRunId, currentBot.id, [], []);
generateStatusUpdate(playbookRunId, activeBot.id, [], []);
}
}, [isOpen]);

Expand All @@ -67,7 +67,7 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
}, [generating]);

const onBotChange = useCallback((bot: any) => {
setCurrentBot(bot)
setActiveBot(bot)
setCurrentVersion(versions.length + 1)
setVersions([...versions, {instruction: '', value: '', prevValue: ''}])
setGenerating(true);
Expand All @@ -76,10 +76,10 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {

const regenerate = useCallback(() => {
setGenerating(true);
generateStatusUpdate(playbookRunId, currentBot?.id, [versions[currentVersion-1].instruction], [versions[currentVersion-1].prevValue]);
generateStatusUpdate(playbookRunId, activeBot?.id, [versions[currentVersion-1].instruction], [versions[currentVersion-1].prevValue]);
setCurrentVersion(versions.length + 1)
setVersions([...versions, {...versions[currentVersion-1], value: ''}])
}, [versions, playbookRunId, instruction, versions, currentVersion, currentBot?.id]);
}, [versions, playbookRunId, instruction, versions, currentVersion, activeBot?.id]);

const copyText = useCallback(() => {
navigator.clipboard.writeText(versions[currentVersion-1].value);
Expand All @@ -90,20 +90,20 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
const onInputEnter = useCallback((e: React.KeyboardEvent) => {
// Detect hitting enter and run the generateStatusUpdate
if (e.key === 'Enter') {
generateStatusUpdate(playbookRunId, currentBot?.id, [instruction], [versions[currentVersion-1].value]);
generateStatusUpdate(playbookRunId, activeBot?.id, [instruction], [versions[currentVersion-1].value]);
setVersions([...versions, {instruction, prevValue: versions[currentVersion-1].value, value: ''}])
setCurrentVersion(versions.length + 1)
setInstruction('')
setGenerating(true);
setTimeout(() => suggestionBox.current?.scrollTo(0, suggestionBox.current?.scrollHeight), 0)
}
}, [versions, instruction, playbookRunId, versions, currentVersion, currentBot?.id])
}, [versions, instruction, playbookRunId, versions, currentVersion, activeBot?.id])

const stopGenerating = useCallback(() => {
setGenerating(false);
}, [])

if (!currentBot?.id) {
if (!activeBot?.id) {
return null
}

Expand All @@ -125,8 +125,8 @@ const AIModal = ({playbookRunId, onAccept, onClose, isOpen}: Props) => {
</IconButton>
</Versions>
<BotSelector
bots={aiAvailableBots || []}
activeBot={currentBot}
bots={bots || []}
activeBot={activeBot}
setActiveBot={onBotChange}
/>
<button type="button" className="close" aria-label="Close" onClick={onClose}>
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/modals/update_run_status_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const UpdateRunStatusModal = ({
))
}
</LastChangeSince>
{ aiAvailable && aiAvailableBots.length > 0 &&
{ aiAvailable &&
<AiModalContainer>
<AIModal
playbookRunId={playbookRunId}
Expand Down

0 comments on commit 49dcf05

Please sign in to comment.