Skip to content

Commit

Permalink
Show document upload card in regular message format (microsoft#1566)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
messages indicating a successful document upload should be shown in the
typical message format.

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
- moves code from `ChatHistory` used to render regular messages into
`ChatHistoryTextContent`
- changes `ChatHistoryFileItem` to `ChatHistoryDocumentContent` and
removes the JSX used for rendering the header

### Preview

![image](https://github.com/microsoft/semantic-kernel/assets/52973358/3f4fb405-8365-4c20-96f7-bb2bfcf68d9c)

![image](https://github.com/microsoft/semantic-kernel/assets/52973358/b702bfd1-fcc2-47b9-8232-bba1e60ea5ba)

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [X] The code builds clean without any errors or warnings
- [X] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [X] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: Teresa Hoang <125500434+teresaqhoang@users.noreply.github.com>
  • Loading branch information
dehoward and teresaqhoang committed Jun 21, 2023
1 parent 28181d7 commit 7ab8a05
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public async Task<SKContext> ChatAsync(string message, SKContext context)
{
ChatMessage botMessage = await this.SaveNewResponseAsync(response, prompt, chatId);
context.Variables.Set("messageId", botMessage.Id);
context.Variables.Set("messageType", botMessage.Type.ToString());
context.Variables.Set("messageType", ((int)botMessage.Type).ToString(CultureInfo.InvariantCulture));
}
catch (Exception ex) when (!ex.IsCriticalException())
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { useAppDispatch, useAppSelector } from '../../redux/app/hooks';
import { RootState } from '../../redux/app/store';
import { updateConversationFromUser } from '../../redux/features/conversations/conversationsSlice';
import { SharedStyles } from '../../styles';
import { ChatHistory } from './ChatHistory';
import { ChatInput } from './ChatInput';
import { ChatHistory } from './chat-history/ChatHistory';

const log = debug(Constants.debug.root).extend('chat-room');

Expand Down Expand Up @@ -122,11 +122,7 @@ export const ChatRoom: React.FC = () => {
</div>
</div>
<div className={classes.input}>
<ChatInput
isDraggingOver={isDraggingOver}
onDragLeave={onDragLeave}
onSubmit={handleSubmit}
/>
<ChatInput isDraggingOver={isDraggingOver} onDragLeave={onDragLeave} onSubmit={handleSubmit} />
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import { makeStyles, shorthands, tokens } from '@fluentui/react-components';
import React from 'react';
import { ChatMessageType, IChatMessage } from '../../libs/models/ChatMessage';
import { GetResponseOptions } from '../../libs/useChat';
import { ChatHistoryFileItem } from './ChatHistoryFileItem';
import { IChatMessage } from '../../../libs/models/ChatMessage';
import { GetResponseOptions } from '../../../libs/useChat';
import { ChatHistoryItem } from './ChatHistoryItem';

const useClasses = makeStyles({
Expand Down Expand Up @@ -35,18 +34,14 @@ export const ChatHistory: React.FC<ChatHistoryProps> = ({ messages, onGetRespons
{messages
.slice()
.sort((a, b) => a.timestamp - b.timestamp)
.map((message, index) =>
message.type === ChatMessageType.Document ? (
<ChatHistoryFileItem key={message.timestamp} message={message} />
) : (
<ChatHistoryItem
key={message.timestamp}
message={message}
getResponse={onGetResponse}
messageIndex={index}
/>
),
)}
.map((message, index) => (
<ChatHistoryItem
key={message.timestamp}
message={message}
getResponse={onGetResponse}
messageIndex={index}
/>
))}
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft. All rights reserved.

import {
Caption1,
Card,
CardHeader,
ProgressBar,
Text,
makeStyles,
mergeClasses,
shorthands,
tokens,
} from '@fluentui/react-components';
import React from 'react';
import { IChatMessage } from '../../../libs/models/ChatMessage';
import { getFileIconByFileExtension } from '../ChatResourceList';

const useClasses = makeStyles({
card: {
height: 'fit-content',
width: '275px',
backgroundColor: tokens.colorNeutralBackground3,
...shorthands.gap(0),
...shorthands.margin(tokens.spacingVerticalS, 0),
...shorthands.padding(tokens.spacingVerticalXS, 0),
},
cardCaption: {
color: tokens.colorNeutralForeground2,
},
cardHeader: {
...shorthands.margin(0, tokens.spacingHorizontalS),
},
cardHeaderText: {
fontSize: 'small',
fontWeight: '500',
},
footer: {
float: 'right',
fontSize: 'small',
fontWeight: '500',
color: tokens.colorNeutralForegroundDisabled,
},
icon: {
height: '32px',
width: '32px',
},
floatLeft: {
float: 'left',
},
});

interface ChatHistoryDocumentContentProps {
isMe: boolean;
message: IChatMessage;
}

interface DocumentMessageContent {
name: string;
size: string;
}

export const ChatHistoryDocumentContent: React.FC<ChatHistoryDocumentContentProps> = ({ isMe, message }) => {
const classes = useClasses();

let name = '',
size = '';
try {
({ name, size } = JSON.parse(message.content) as DocumentMessageContent);
} catch (e) {
console.error('Error parsing chat history file item: ' + message.content);
}

return (
<>
<Card appearance="filled-alternative" className={classes.card}>
<CardHeader
className={classes.cardHeader}
image={getFileIconByFileExtension(name, { className: classes.icon })}
header={<Text className={classes.cardHeaderText}>{name}</Text>}
description={
<Caption1 block className={classes.cardCaption}>
{size}
</Caption1>
}
/>
<ProgressBar thickness="large" color="success" value={1} />
</Card>
<span className={isMe ? classes.footer : mergeClasses(classes.footer, classes.floatLeft)}>
Success: memory established
</span>
</>
);
};
Loading

0 comments on commit 7ab8a05

Please sign in to comment.