Skip to content

Commit

Permalink
Merge pull request #705 from openchatai/fixxxx
Browse files Browse the repository at this point in the history
Fix the json parsing issues
  • Loading branch information
ah7255703 committed Mar 15, 2024
2 parents 24031c1 + 3011b15 commit d295321
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
34 changes: 33 additions & 1 deletion copilot-widget/lib/contexts/messageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,40 @@ export type UserMessageType = {
headers: Record<string, string>;
bot_token: string;
query_params: Record<string, string>;
language?: string;
};

function unsafe__decodeJSON<T extends Record<string, any>>(
jsonString: string
): T | null {
try {
const parsed = JSON.parse(jsonString);
if (typeof parsed === "object" && parsed !== null) {
// Recursively parse nested JSON strings or arrays
Object.keys(parsed).forEach((key) => {
const value = parsed[key];
if (typeof value === "string") {
if (
typeof value === "string" &&
((value.startsWith("{") && value.endsWith("}")) ||
(value.startsWith("[") && value.endsWith("]")))
) {
try {
parsed[key] = JSON.parse(value);
} catch (e) {
// Ignore errors for invalid JSON strings
}
}
}
});
return parsed as T;
}
return null;
} catch (e) {
return null;
}
}

export type BotMessageType<TData = Record<string, unknown>> = {
from: "bot";
type: string;
Expand Down Expand Up @@ -351,7 +383,7 @@ export class ChatController {
name: string;
};
const handle = (msg: string) => {
const parsedResponse = JSON.parse(msg) as ResponseObject;
const parsedResponse = unsafe__decodeJSON(msg) as ResponseObject;
this.setValueImmer((draft) => {
let message: BotMessageType | null = null;
if (parsedResponse.type === "ui_form") {
Expand Down
3 changes: 3 additions & 0 deletions copilot-widget/lib/contexts/statefulMessageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSessionId } from "@lib/hooks/useSessionId";
import { useConfigData } from "./ConfigData";
import { useSocket } from "./SocketProvider";
import { ComponentRegistery } from "./componentRegistery";
import { useLang } from "..";

const [useMessageHandler, MessageHandlerSafeProvider] = createSafeContext<{
__handler: ChatController;
Expand Down Expand Up @@ -68,6 +69,7 @@ function useChatLoading() {
function useSendMessage() {
const { __handler } = useMessageHandler();
const { headers, token, queryParams } = useConfigData();
const { lang } = useLang();
const socket = useSocket();

function send(content: string) {
Expand All @@ -77,6 +79,7 @@ function useSendMessage() {
query_params: queryParams ?? {},
content,
bot_token: token,
language: lang,
},
socket.__socket
);
Expand Down
12 changes: 2 additions & 10 deletions copilot-widget/lib/utils/createSocket.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { Socket, io } from "socket.io-client";
import { io } from "socket.io-client";

interface ConfigType {
socketUrl: string;
token: string;
sessionId: string;
}

const sockets = new Map<string, Socket>();

export function createSocketClient({
socketUrl,
token,
sessionId,
}: ConfigType) {
const socket = sockets.get(sessionId + token);
if (socket) {
return socket;
}
const newSocket = io(socketUrl, {
return io(socketUrl, {
autoConnect: false,
transports: ["websocket"],
extraHeaders: {
"X-Bot-Token": token,
"X-Session-Id": sessionId,
},
});
sockets.set(sessionId + token, newSocket);
return newSocket;
}
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.7",
"version": "2.8.8",
"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.7",
"@openchatai/copilot-widget": "^2.8.8",
"@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.

0 comments on commit d295321

Please sign in to comment.