Skip to content

Commit

Permalink
feat: add interaction into message_bubble.tsx
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Nov 22, 2023
1 parent dae5a21 commit cdfae7a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
31 changes: 27 additions & 4 deletions public/hooks/use_chat_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
*/

import { ASSISTANT_API } from '../../common/constants/llm';
import { IMessage, ISuggestedAction } from '../../common/types/chat_saved_object_attributes';
import {
IMessage,
ISuggestedAction,
Interaction,
} from '../../common/types/chat_saved_object_attributes';
import { useChatContext } from '../contexts/chat_context';
import { useCore } from '../contexts/core_context';
import { AssistantActions } from '../types';
Expand All @@ -14,6 +18,7 @@ interface SendResponse {
sessionId: string;
title: string;
messages: IMessage[];
interactions: Interaction[];
}

interface SetParagraphResponse {
Expand Down Expand Up @@ -47,7 +52,13 @@ export const useChatActions = (): AssistantActions => {
if (!chatContext.title) {
chatContext.setTitle(response.title);
}
chatStateDispatch({ type: 'receive', payload: response.messages });
chatStateDispatch({
type: 'receive',
payload: {
messages: response.messages,
interactions: response.interactions,
},
});
} catch (error) {
if (abortController.signal.aborted) return;
chatStateDispatch({ type: 'error', payload: error });
Expand All @@ -70,7 +81,13 @@ export const useChatActions = (): AssistantActions => {
}
const session = await core.services.sessionLoad.load(sessionId);
if (session) {
chatStateDispatch({ type: 'receive', payload: session.messages });
chatStateDispatch({
type: 'receive',
payload: {
messages: session.messages,
interactions: session.interactions,
},
});
}
};

Expand Down Expand Up @@ -147,7 +164,13 @@ export const useChatActions = (): AssistantActions => {
if (abortController.signal.aborted) {
return;
}
chatStateDispatch({ type: 'receive', payload: response.messages });
chatStateDispatch({
type: 'receive',
payload: {
messages: response.messages,
interactions: response.interactions,
},
});
} catch (error) {
if (abortController.signal.aborted) {
return;
Expand Down
15 changes: 12 additions & 3 deletions public/hooks/use_chat_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import { produce } from 'immer';
import React, { useContext, useMemo, useReducer } from 'react';
import { IMessage } from '../../common/types/chat_saved_object_attributes';
import { IMessage, Interaction } from '../../common/types/chat_saved_object_attributes';

interface ChatState {
messages: IMessage[];
interactions: Interaction[];
llmResponding: boolean;
llmError?: Error;
}
Expand All @@ -18,7 +19,13 @@ type ChatStateAction =
| { type: 'abort' }
| { type: 'reset' }
| { type: 'send'; payload: IMessage }
| { type: 'receive'; payload: ChatState['messages'] }
| {
type: 'receive';
payload: {
messages: ChatState['messages'];
interactions: ChatState['interactions'];
};
}
| {
type: 'error';
payload: NonNullable<ChatState['llmError']> | { body: NonNullable<ChatState['llmError']> };
Expand All @@ -31,6 +38,7 @@ interface IChatStateContext {
const ChatStateContext = React.createContext<IChatStateContext | null>(null);

const initialState: ChatState = {
interactions: [],
messages: [],
llmResponding: false,
};
Expand All @@ -48,7 +56,8 @@ const chatStateReducer: React.Reducer<ChatState, ChatStateAction> = (state, acti
break;

case 'receive':
draft.messages = action.payload;
draft.messages = action.payload.messages;
draft.interactions = action.payload.interactions;
draft.llmResponding = false;
draft.llmError = undefined;
break;
Expand Down
8 changes: 7 additions & 1 deletion public/tabs/chat/chat_page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export const ChatPage: React.FC<ChatPageProps> = (props) => {
}
const session = await core.services.sessionLoad.load(chatContext.sessionId);
if (session) {
chatStateDispatch({ type: 'receive', payload: session.messages });
chatStateDispatch({
type: 'receive',
payload: {
messages: session.messages,
interactions: session.interactions,
},
});
}
}, [chatContext.sessionId, chatStateDispatch]);

Expand Down
14 changes: 13 additions & 1 deletion public/tabs/chat/chat_page_content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
EuiText,
} from '@elastic/eui';
import React, { useLayoutEffect, useRef } from 'react';
import { IMessage, ISuggestedAction } from '../../../common/types/chat_saved_object_attributes';
import {
IMessage,
ISuggestedAction,
Interaction,
} from '../../../common/types/chat_saved_object_attributes';
import { TermsAndConditions } from '../../components/terms_and_conditions';
import { useChatContext } from '../../contexts/chat_context';
import { useChatState } from '../../hooks/use_chat_state';
Expand Down Expand Up @@ -120,6 +124,13 @@ export const ChatPageContent: React.FC<ChatPageContentProps> = React.memo((props
// Only show suggestion on llm outputs after last user input
const showSuggestions = i > lastInputIndex;

let interaction: Interaction | undefined;
if (message.type === 'output' && message.traceId) {
interaction = chatState.interactions.find(
(item) => item.interaction_id === message.traceId
);
}

return (
<React.Fragment key={i}>
<ToolsUsed message={message} />
Expand All @@ -129,6 +140,7 @@ export const ChatPageContent: React.FC<ChatPageContentProps> = React.memo((props
showRegenerate={isLatestOutput}
shouldActionBarVisibleOnHover={!isLatestOutput}
onRegenerate={chatActions.regenerate}
interaction={interaction}
>
<MessageContent message={message} />
{/* <MessageFooter message={message} previousInput={findPreviousInput(array, i)} />*/}
Expand Down
7 changes: 6 additions & 1 deletion public/tabs/chat/messages/message_bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import React, { useCallback } from 'react';
import { IconType } from '@elastic/eui/src/components/icon/icon';
import cx from 'classnames';
import chatIcon from '../../../assets/chat.svg';
import { IMessage, IOutput } from '../../../../common/types/chat_saved_object_attributes';
import {
IMessage,
IOutput,
Interaction,
} from '../../../../common/types/chat_saved_object_attributes';
import { useFeedback } from '../../../hooks/use_feed_back';

type MessageBubbleProps = {
Expand All @@ -30,6 +34,7 @@ type MessageBubbleProps = {
} & (
| {
message: IMessage;
interaction?: Interaction;
}
| {
loading: boolean;
Expand Down
1 change: 1 addition & 0 deletions server/routes/chat_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions)
messages: finalMessage.messages,
sessionId: outputs.memoryId,
title: finalMessage.title,
interactions: finalMessage.interactions,
},
});
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion server/services/storage/agent_framework_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class AgentFrameworkStorageService implements StorageService {
createdTimeMs: Date.now(),
updatedTimeMs: Date.now(),
messages: finalMessages,
interactions: session.body.interactions,
interactions: finalInteractions,
};
}

Expand Down

0 comments on commit cdfae7a

Please sign in to comment.