Skip to content

Commit 65834cd

Browse files
committed
Add deleting to tool calling demo app + some logic fixes in calendar handling
1 parent dc37f8e commit 65834cd

4 files changed

Lines changed: 71 additions & 21 deletions

File tree

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import React, { memo } from 'react';
2-
import { View, StyleSheet } from 'react-native';
2+
import {
3+
View,
4+
StyleSheet,
5+
TouchableOpacity,
6+
Text,
7+
Platform,
8+
} from 'react-native';
39
import MarkdownComponent from './MarkdownComponent';
410
import LlamaIcon from '../../assets/icons/llama_icon.svg';
5-
import { MessageType } from 'react-native-executorch/lib/typescript/types/llm';
611
import ColorPalette from '../colors';
12+
import { MessageType } from 'react-native-executorch/lib/typescript/types/llm';
713

814
interface MessageItemProps {
915
message: MessageType;
16+
deleteMessage: () => void;
1017
}
1118

12-
const MessageItem = memo(({ message }: MessageItemProps) => {
19+
const MessageItem = memo(({ message, deleteMessage }: MessageItemProps) => {
1320
return (
1421
<View
1522
style={
@@ -22,28 +29,52 @@ const MessageItem = memo(({ message }: MessageItemProps) => {
2229
</View>
2330
)}
2431
<MarkdownComponent text={message.content} />
32+
<CloseButton deleteMessage={deleteMessage} role={message.role} />
2533
</View>
2634
);
2735
});
2836

37+
const CloseButton = ({
38+
deleteMessage,
39+
role,
40+
}: {
41+
deleteMessage: () => void;
42+
role: string;
43+
}) => {
44+
return (
45+
<TouchableOpacity
46+
style={[
47+
styles.closeButton,
48+
role === 'assistant' ? styles.closeButtonRight : styles.closeButtonLeft,
49+
]}
50+
onPress={deleteMessage}
51+
>
52+
<Text style={styles.buttonText}></Text>
53+
</TouchableOpacity>
54+
);
55+
};
56+
2957
export default MessageItem;
3058

3159
const styles = StyleSheet.create({
3260
aiMessage: {
3361
flexDirection: 'row',
34-
maxWidth: '80%',
62+
maxWidth: '75%',
3563
alignSelf: 'flex-start',
3664
marginVertical: 8,
65+
alignItems: 'center',
3766
},
3867
userMessage: {
68+
flexDirection: 'row-reverse',
3969
paddingHorizontal: 12,
4070
paddingVertical: 8,
4171
marginRight: 8,
4272
marginVertical: 8,
43-
maxWidth: 220,
73+
maxWidth: '75%',
4474
borderRadius: 8,
4575
backgroundColor: ColorPalette.seaBlueLight,
4676
alignSelf: 'flex-end',
77+
alignItems: 'center',
4778
},
4879
aiMessageIconContainer: {
4980
backgroundColor: ColorPalette.seaBlueLight,
@@ -54,10 +85,22 @@ const styles = StyleSheet.create({
5485
borderRadius: 16,
5586
marginHorizontal: 7,
5687
},
57-
messageText: {
58-
fontSize: 14,
59-
lineHeight: 19.6,
60-
color: ColorPalette.primary,
61-
fontFamily: 'regular',
88+
closeButton: {
89+
borderRadius: 11,
90+
backgroundColor: ColorPalette.blueLight,
91+
alignItems: 'center',
92+
justifyContent: 'center',
93+
width: 22,
94+
height: 22,
95+
},
96+
closeButtonRight: {
97+
marginLeft: 8,
98+
},
99+
closeButtonLeft: {
100+
marginRight: 8,
101+
},
102+
buttonText: {
103+
fontSize: Platform.OS === 'ios' ? 16 : 14,
104+
color: '#000',
62105
},
63106
});

examples/llm-tool-calling/src/components/Messages.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import { useRef } from 'react';
22
import { ScrollView, StyleSheet, View, Text } from 'react-native';
33
import AnimatedChatLoading from './AnimatedChatLoading';
44
import LlamaIcon from '../../assets/icons/llama_icon.svg';
5-
import MessageItem from './MessageItem';
6-
import { MessageType } from 'react-native-executorch/lib/typescript/types/llm';
75
import ColorPalette from '../colors';
6+
import MessageItem from './MessageItem';
7+
import { MessageType } from 'react-native-executorch';
88

99
interface MessagesComponentProps {
1010
chatHistory: MessageType[];
1111
llmResponse: string;
1212
isGenerating: boolean;
13+
deleteMessage: (index: number) => void;
1314
}
1415

1516
export default function Messages({
1617
chatHistory,
1718
llmResponse,
1819
isGenerating,
20+
deleteMessage,
1921
}: MessagesComponentProps) {
2022
const scrollViewRef = useRef<ScrollView>(null);
2123

@@ -27,7 +29,11 @@ export default function Messages({
2729
>
2830
<View onStartShouldSetResponder={() => true}>
2931
{chatHistory.map((message, index) => (
30-
<MessageItem key={index} message={message} />
32+
<MessageItem
33+
key={index}
34+
message={message}
35+
deleteMessage={() => deleteMessage(index)}
36+
/>
3137
))}
3238
{isGenerating && (
3339
<View style={styles.aiMessage}>

examples/llm-tool-calling/src/screens/ChatScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export default function ChatScreen() {
108108
chatHistory={llm.messageHistory}
109109
llmResponse={llm.response}
110110
isGenerating={llm.isGenerating}
111+
deleteMessage={llm.deleteMessage}
111112
/>
112113
</View>
113114
) : (

examples/llm-tool-calling/src/screens/tools.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,22 @@ const readCalendar = async (call: ToolCall) => {
149149
).valueOf();
150150
} else if (Number.isNaN(startTime) || Number.isNaN(endTime)) {
151151
if (Number.isNaN(startTime)) {
152-
const endDay = new Date(endTime);
152+
const today = new Date();
153153
startTime = new Date(
154-
endDay.getFullYear(),
155-
endDay.getMonth(),
156-
endDay.getDate(),
154+
today.getFullYear(),
155+
today.getMonth(),
156+
today.getDate(),
157157
0,
158158
0,
159159
0
160160
).valueOf();
161161
} else if (Number.isNaN(endTime)) {
162-
const startDay = new Date(startTime);
162+
const today = new Date();
163163

164164
endTime = new Date(
165-
startDay.getFullYear(),
166-
startDay.getMonth(),
167-
startDay.getDate(),
165+
today.getFullYear(),
166+
today.getMonth(),
167+
today.getDate(),
168168
23,
169169
59,
170170
59

0 commit comments

Comments
 (0)