@@ -36,6 +36,7 @@ import androidx.compose.ui.unit.dp
3636import xyz.junerver.compose.ai.usechat.Message
3737import xyz.junerver.compose.ai.usechat.Role
3838import xyz.junerver.compose.ai.usechat.useChat
39+ import xyz.junerver.compose.hooks.useState
3940import xyz.junerver.composehooks.ui.component.TButton
4041
4142/*
@@ -49,9 +50,9 @@ import xyz.junerver.composehooks.ui.component.TButton
4950@Composable
5051fun UseChatExample () {
5152 // Configure your API settings here
52- var apiKey by remember { mutableStateOf (" " ) }
53- var baseUrl by remember { mutableStateOf (" https://api.openai.com/v1" ) }
54- var model by remember { mutableStateOf (" gpt-3.5-turbo " ) }
53+ var apiKey by useState (" " )
54+ var baseUrl by useState (" https://api.openai.com/v1" )
55+ var model by useState (" gpt-5-nano " )
5556
5657 val (messages, isLoading, error, sendMessage, _, _, reload, stop) = useChat {
5758 this .baseUrl = baseUrl
@@ -82,7 +83,7 @@ fun UseChatExample() {
8283 // Configuration Section
8384 Text (
8485 text = " useChat Example" ,
85- style = MaterialTheme .typography.headlineSmall
86+ style = MaterialTheme .typography.headlineSmall,
8687 )
8788 Spacer (modifier = Modifier .height(8 .dp))
8889
@@ -92,7 +93,7 @@ fun UseChatExample() {
9293 onValueChange = { apiKey = it },
9394 label = { Text (" API Key" ) },
9495 modifier = Modifier .fillMaxWidth(),
95- singleLine = true
96+ singleLine = true ,
9697 )
9798 Spacer (modifier = Modifier .height(4 .dp))
9899
@@ -102,15 +103,15 @@ fun UseChatExample() {
102103 onValueChange = { baseUrl = it },
103104 label = { Text (" Base URL" ) },
104105 modifier = Modifier .weight(1f ),
105- singleLine = true
106+ singleLine = true ,
106107 )
107108 Spacer (modifier = Modifier .width(8 .dp))
108109 OutlinedTextField (
109110 value = model,
110111 onValueChange = { model = it },
111112 label = { Text (" Model" ) },
112113 modifier = Modifier .weight(0.5f ),
113- singleLine = true
114+ singleLine = true ,
114115 )
115116 }
116117
@@ -120,14 +121,14 @@ fun UseChatExample() {
120121 error.value?.let { err ->
121122 Card (
122123 colors = CardDefaults .cardColors(
123- containerColor = MaterialTheme .colorScheme.errorContainer
124+ containerColor = MaterialTheme .colorScheme.errorContainer,
124125 ),
125- modifier = Modifier .fillMaxWidth()
126+ modifier = Modifier .fillMaxWidth(),
126127 ) {
127128 Text (
128129 text = " Error: ${err.message} " ,
129130 color = MaterialTheme .colorScheme.onErrorContainer,
130- modifier = Modifier .padding(8 .dp)
131+ modifier = Modifier .padding(8 .dp),
131132 )
132133 }
133134 Spacer (modifier = Modifier .height(8 .dp))
@@ -139,7 +140,7 @@ fun UseChatExample() {
139140 modifier = Modifier
140141 .weight(1f )
141142 .fillMaxWidth(),
142- verticalArrangement = Arrangement .spacedBy(8 .dp)
143+ verticalArrangement = Arrangement .spacedBy(8 .dp),
143144 ) {
144145 items(messages.value.filter { it.role != Role .System }) { message ->
145146 MessageBubble (message = message)
@@ -150,16 +151,16 @@ fun UseChatExample() {
150151 item {
151152 Row (
152153 modifier = Modifier .fillMaxWidth(),
153- horizontalArrangement = Arrangement .Start
154+ horizontalArrangement = Arrangement .Start ,
154155 ) {
155156 CircularProgressIndicator (
156157 modifier = Modifier .padding(8 .dp),
157- strokeWidth = 2 .dp
158+ strokeWidth = 2 .dp,
158159 )
159160 Text (
160161 text = " Thinking..." ,
161162 modifier = Modifier .padding(8 .dp),
162- color = MaterialTheme .colorScheme.onSurfaceVariant
163+ color = MaterialTheme .colorScheme.onSurfaceVariant,
163164 )
164165 }
165166 }
@@ -171,7 +172,7 @@ fun UseChatExample() {
171172 // Input Section
172173 Row (
173174 modifier = Modifier .fillMaxWidth(),
174- verticalAlignment = Alignment .CenterVertically
175+ verticalAlignment = Alignment .CenterVertically ,
175176 ) {
176177 OutlinedTextField (
177178 value = inputText,
@@ -186,14 +187,14 @@ fun UseChatExample() {
186187 sendMessage(inputText)
187188 inputText = " "
188189 }
189- }
190- )
190+ },
191+ ),
191192 )
192193 Spacer (modifier = Modifier .width(8 .dp))
193194 Column {
194195 TButton (
195196 text = if (isLoading.value) " Stop" else " Send" ,
196- enabled = apiKey.isNotBlank() && (isLoading.value || inputText.isNotBlank())
197+ enabled = apiKey.isNotBlank() && (isLoading.value || inputText.isNotBlank()),
197198 ) {
198199 if (isLoading.value) {
199200 stop()
@@ -204,7 +205,7 @@ fun UseChatExample() {
204205 }
205206 TButton (
206207 text = " Reload" ,
207- enabled = ! isLoading.value && messages.value.any { it.role == Role .Assistant }
208+ enabled = ! isLoading.value && messages.value.any { it.role == Role .Assistant },
208209 ) {
209210 reload()
210211 }
@@ -230,28 +231,28 @@ private fun MessageBubble(message: Message) {
230231
231232 Row (
232233 modifier = Modifier .fillMaxWidth(),
233- horizontalArrangement = if (isUser) Arrangement .End else Arrangement .Start
234+ horizontalArrangement = if (isUser) Arrangement .End else Arrangement .Start ,
234235 ) {
235236 Card (
236237 colors = CardDefaults .cardColors(containerColor = backgroundColor),
237238 shape = RoundedCornerShape (
238239 topStart = 16 .dp,
239240 topEnd = 16 .dp,
240241 bottomStart = if (isUser) 16 .dp else 4 .dp,
241- bottomEnd = if (isUser) 4 .dp else 16 .dp
242+ bottomEnd = if (isUser) 4 .dp else 16 .dp,
242243 ),
243- modifier = Modifier .widthIn(max = 280 .dp)
244+ modifier = Modifier .widthIn(max = 280 .dp),
244245 ) {
245246 Column (modifier = Modifier .padding(12 .dp)) {
246247 Text (
247248 text = if (isUser) " You" else " Assistant" ,
248249 style = MaterialTheme .typography.labelSmall,
249- color = textColor.copy(alpha = 0.7f )
250+ color = textColor.copy(alpha = 0.7f ),
250251 )
251252 Spacer (modifier = Modifier .height(4 .dp))
252253 Text (
253254 text = message.content.ifEmpty { " ..." },
254- color = textColor
255+ color = textColor,
255256 )
256257 }
257258 }
0 commit comments