-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatPage.tsx
145 lines (140 loc) · 4.27 KB
/
ChatPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { AuthUser } from "aws-amplify/auth";
import { generateClient } from "aws-amplify/data";
import { createAIHooks, AIConversation } from "@aws-amplify/ui-react-ai";
import type { Schema } from "../../amplify/data/resource";
import { Loader, ScrollView, Text, View } from "@aws-amplify/ui-react";
import { useEffect, useState } from "react";
const client = generateClient<Schema>({ authMode: "userPool" });
const { useAIConversation } = createAIHooks(client);
function ChatComponent(props: {
conversationId: string | null;
user?: AuthUser;
randomNumber: number;
}) {
const [
{
data: { messages },
// hasError,
isLoading,
},
sendMessage,
] = useAIConversation("chat", {
id: props.conversationId ?? undefined,
onResponse: (response) => {
console.log({ method: "onResponse", response });
},
});
messages.sort((a, b) => (a.createdAt < b.createdAt ? -1 : 1));
return (
<>
<View
paddingLeft={"large"}
paddingRight={"large"}
paddingBottom={messages.length ? "small" : "xxxl"}
textAlign={"center"}
>
<Text fontSize={"xl"} fontWeight={"bold"}>
🤖 MotivationBot 🤖
</Text>
<Text fontSize={"xs"}>
AI chat about your eating and excercise goals.
</Text>
<Text fontSize={"xxs"}>(experimental)</Text>
</View>
<ScrollView
width="100%"
minHeight="300px"
maxHeight={"50vh"}
autoScroll="smooth"
>
<AIConversation
messages={messages}
handleSendMessage={(content) =>
sendMessage({
...content,
aiContext: { ignoreThisArgument: "true" },
// toolConfiguration: {
// tools: {
// generateRecipe: {
// description: "List ingredients needed for a recipe",
// inputSchema: {
// json: {
// type: "object",
// properties: {
// ingredients: {
// type: "array",
// items: {
// type: "object",
// properties: {
// ingredientName: {type: "string"},
// quantity: {type: "number"},
// unit: {type: "string"},
// } },
// },
// },
// },
// },
// },
// }
// }
})
}
variant="bubble"
avatars={{
user: {
username:
props.user?.signInDetails?.loginId?.split("@")[0] || "User",
avatar: "🥷",
},
ai: {
username: "MotivationBot",
avatar: "🤖",
},
}}
/>
{isLoading ? <Loader variation="linear" /> : <></>}
<Text as="span" style={{ visibility: "hidden" }}>
{props.randomNumber}
</Text>
</ScrollView>
</>
);
}
export default function ChatPage(props: {
user: AuthUser | undefined;
randomNumber: number;
}) {
const [conversationId, setConversationId] = useState<string | undefined>();
useEffect(() => {
console.log("ChatPage Effect Running");
const fetchConversation = async () => {
const { data, errors } = await client.conversations.chat.list();
data.sort((a, b) => (a.id < b.id ? 1 : -1));
const c = data.find((t) => t);
if (!c) {
const { data: newConversation, errors } =
await client.conversations.chat.create();
if (errors) {
console.log({ method: "createConversation", errors });
}
setConversationId(newConversation!.id);
return;
}
if (errors) {
console.log({ method: "fetchConversation", errors });
}
setConversationId(c?.id ?? null);
};
fetchConversation();
}, []);
if (conversationId === undefined) {
return <Loader />;
}
return (
<ChatComponent
conversationId={conversationId}
user={props.user}
randomNumber={props.randomNumber}
/>
);
}