diff src/lib/Chat.luan @ 35:3117876debca

ai_first_message in textarea
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Aug 2025 16:41:29 -0600
parents 0fb3488a017d
children 2737eeedc1d5
line wrap: on
line diff
--- a/src/lib/Chat.luan	Mon Aug 04 23:06:19 2025 -0600
+++ b/src/lib/Chat.luan	Tue Aug 05 16:41:29 2025 -0600
@@ -43,7 +43,7 @@
 		chat_updated = long(chat.updated)
 		course_id = long(chat.course_id)
 		name = chat.name or error()
-		ai_thread = chat.ai_thread
+		ai_thread = chat.ai_thread or error()
 		language = chat.language or error()
 		language_region = chat.language_region or error()
 		voice = chat.voice or error()
@@ -89,19 +89,12 @@
 		return html_encode(chat.name)
 	end
 
-	function chat.init_ai()  -- return if added message
-		if chat.ai_thread ~= nil then
-			return false
+	function chat.init_text()  -- return text for textarea
+		if Ai_chat.has_messages(chat.ai_thread) then
+			return ""
 		end
 		local course = get_course_by_id(chat.course_id) or error()
-		local ai_first_message = course.ai_first_message
-		local ai_thread = Ai_chat.ask_first(course.ai_system_prompt,ai_first_message)
-		run_in_transaction( function()
-			chat = chat.reload()
-			chat.ai_thread = ai_thread
-			chat.save()
-		end )
-		return ai_first_message ~= nil
+		return course.ai_first_message or error()
 	end
 
 	function chat.output_system_prompt()
@@ -114,7 +107,7 @@
 
 	function chat.ask(input)
 		local old_thread = chat.ai_thread
-		local ai_thread = Ai_chat.ask_more(old_thread,input)
+		local ai_thread = Ai_chat.ask(old_thread,input)
 		run_in_transaction( function()
 			chat = chat.reload()
 			chat.ai_thread = ai_thread