Skip to content

Commit

Permalink
Merge pull request #699 from openchatai/handle-handout-and-fixes
Browse files Browse the repository at this point in the history
Handle Handoff
  • Loading branch information
ah7255703 committed Mar 14, 2024
2 parents 0e5a435 + b308ee4 commit 3b28e1a
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 77 deletions.
41 changes: 41 additions & 0 deletions copilot-widget/lib/contexts/messageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export type ComponentType<DT> = {
key: string;
data?: DT;
};
export type HandoffPayloadType = {
summery: string;
sentiment: "happy" | "angry" | "neutral";
}; // sometimes it will be an empty object

export type State = {
currentUserMessage: null | UserMessageType;
Expand Down Expand Up @@ -180,6 +184,21 @@ export class ChatController {
socket.emit("send_chat", userMessage);
};

internalHandleHandoff = (payload: HandoffPayloadType) => {
// handle handoff
const id = this.genId();
this.setValueImmer((draft) => {
const message = {
from: "bot",
type: "HANDOFF",
id: id,
responseFor: id,
timestamp: this.getTimeStamp(),
data: payload,
} as BotMessageType<HandoffPayloadType>;
draft.messages.push(message);
});
};
// Called for every character recived from the bot
appendToCurrentBotMessage = (message: string) => {
const currentUserMessage = this.state.currentUserMessage;
Expand Down Expand Up @@ -370,6 +389,28 @@ export class ChatController {
};
};

socketHandoffHandler = (
socket: Socket,
externalCallback?: (payload: HandoffPayloadType) => void
) => {
const sessionId = this.sessionId;
if (!sessionId) {
return;
}

const handle = (msg: string) => {
const parsedResponse = JSON.parse(msg) as HandoffPayloadType;
externalCallback?.(parsedResponse);
this.internalHandleHandoff(parsedResponse);
};

socket.on(`${sessionId}_handoff`, handle);

return () => {
socket.off(`${sessionId}_handoff`, handle);
};
};

private startTimeout = (callback: () => void) => {
this.clearTimeout();
timeout = setTimeout(() => {
Expand Down
8 changes: 7 additions & 1 deletion copilot-widget/lib/contexts/statefulMessageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const [useMessageHandler, MessageHandlerSafeProvider] = createSafeContext<{
}>();

function MessageHandlerProvider(props: { children: React.ReactNode }) {
const { token, components } = useConfigData();
const { token, components, onHandoff } = useConfigData();
const { sessionId } = useSessionId(token);
const { __socket } = useSocket();

const __components = useMemo(
() => new ComponentRegistery({ components }),
[components]
);

const handler = useMemo(() => new ChatController(sessionId), [sessionId]);

useEffect(() => {
Expand All @@ -38,6 +40,10 @@ function MessageHandlerProvider(props: { children: React.ReactNode }) {
return handler.socketUiHandler(__socket);
}, [__socket, handler]);

useEffect(() => {
return handler.socketHandoffHandler(__socket, onHandoff);
}, [__socket, handler, onHandoff]);

return (
<MessageHandlerSafeProvider
value={{
Expand Down
11 changes: 9 additions & 2 deletions copilot-widget/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export { default as Root } from "./Root";
export { CopilotWidget } from "./CopilotWidget";
export type { ComponentType } from "./contexts/componentRegistery";
export type {
ComponentType,
ComponentProps,
} from "./contexts/componentRegistery";
export type { Options } from "./types/options";
export type { ComponentProps } from "./contexts/componentRegistery";
export {
useChatState,
useChatLoading,
Expand All @@ -12,3 +14,8 @@ export { useConfigData } from "./contexts/ConfigData";
export { useWidgetState } from "./contexts/WidgetState";
export { useSocket } from "./contexts/SocketProvider";
export { useLang } from "./contexts/LocalesProvider";
export type {
UserMessageType,
BotMessageType,
HandoffPayloadType,
} from "./contexts/messageHandler";
5 changes: 1 addition & 4 deletions copilot-widget/lib/screens/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ export default function ChatScreen() {
className="flex-1 w-full flex flex-col overflow-x-hidden shrink-0 relative overflow-auto scrollbar-thin scroll-smooth"
>
<div className="flex flex-1 w-full min-h-fit mt-auto flex-col py-2 max-h-full items-center gap-1 last:fade-in-right">
{
// If there are initial messages, show them.
initialMessage && <BotInitialMessage message={initialMessage} />
}
{initialMessage && <BotInitialMessage message={initialMessage} />}
<Map
data={messages}
render={(message, index) => {
Expand Down
2 changes: 2 additions & 0 deletions copilot-widget/lib/types/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ComponentType } from "@lib/contexts/componentRegistery";
import { HandoffPayloadType } from "@lib/contexts/messageHandler";
import type { LangType } from "@lib/locales";

export type Options = {
Expand All @@ -14,6 +15,7 @@ export type Options = {
language?: LangType;
warnBeforeClose?: boolean;
onClose?: () => void;
onHandoff?: (handout: HandoffPayloadType) => void;
containerProps?: React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
Expand Down
2 changes: 1 addition & 1 deletion copilot-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@openchatai/copilot-widget",
"private": false,
"version": "2.8.5",
"version": "2.8.6",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@hookform/resolvers": "^3.3.1",
"@kbox-labs/react-echarts": "^1.0.3",
"@openchatai/copilot-widget": "^2.8.5",
"@openchatai/copilot-widget": "^2.8.6",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
Expand Down
8 changes: 4 additions & 4 deletions dashboard/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3b28e1a

Please sign in to comment.