Skip to content

Commit

Permalink
feat(panel-ui): error handling for JSON messages
Browse files Browse the repository at this point in the history
  • Loading branch information
samzhangjy committed Aug 30, 2023
1 parent 714ce0c commit 5e1d25e
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions packages/panel-ui/app/routes/messages.$parentId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ const useStyles = createStyles((theme) => ({
fontSize: rem(14),
},

itemActionCallFailed: {
fontSize: rem(14),
},

unreadButton: {
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.gray[0] : theme.colors.dark[7],
color: theme.colorScheme === 'dark' ? theme.colors.dark[7] : theme.colors.gray[0],
Expand Down Expand Up @@ -255,6 +259,39 @@ export default function MessagesDetail() {
});
}, [isFetchingMore]);

const ActionMessage = ({ message }: { message: Message }) => {
try {
const args: Record<string, any> = JSON.parse(message.action!.arguments);
return (
<Text className={classes.itemActionCall} color="dimmed">
{message.action!.name}(
{Object.entries(args).map(([key, value], index) => (
<>
<Text span className={classes.itemActionCall}>
{key}=
</Text>
<Text span italic className={classes.itemActionCall}>
{value}
</Text>
{index !== Object.keys(args).length - 1 && (
<Text span className={classes.itemActionCall}>
,{' '}
</Text>
)}
</>
))}
)
</Text>
);
} catch (e) {
return (
<Text className={classes.itemActionCallFailed} color="red">
无法渲染此节点:{(e as any).toString()}
</Text>
);
}
};

return (
<Shell>
<Container my={60}>
Expand All @@ -277,7 +314,9 @@ export default function MessagesDetail() {
)}
{!hasMore && (
<Center my={10}>
<Text color="dimmed" size="sm">没有更多了</Text>
<Text color="dimmed" size="sm">
没有更多了
</Text>
</Center>
)}
{messages.map((message) => {
Expand All @@ -302,30 +341,7 @@ export default function MessagesDetail() {
>
{message.role}
</Badge>
{message.action && (
<Text className={classes.itemActionCall} color="dimmed">
{message.action.name}(
{Object.entries(
JSON.parse(message.action.arguments) as Record<string, any>,
).map(([key, value], index) => (
<>
<Text span className={classes.itemActionCall}>
{key}=
</Text>
<Text span italic className={classes.itemActionCall}>
{value}
</Text>
{index !==
Object.keys(JSON.parse(message.action!.arguments)).length - 1 && (
<Text span className={classes.itemActionCall}>
,{' '}
</Text>
)}
</>
))}
)
</Text>
)}
{message.action && <ActionMessage message={message} />}
{message.content && (
<Text className={classes.itemContent}>
{message.sender && (
Expand Down

1 comment on commit 5e1d25e

@vercel
Copy link

@vercel vercel bot commented on 5e1d25e Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.