diff src/lib/Chat.luan @ 25:3a80ddafe5a4

courses work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 Aug 2025 00:33:51 -0600
parents 87fe70201aa8
children 505185272dd7
line wrap: on
line diff
--- a/src/lib/Chat.luan	Thu Jul 31 22:30:26 2025 -0600
+++ b/src/lib/Chat.luan	Fri Aug 01 00:33:51 2025 -0600
@@ -9,10 +9,12 @@
 local html_encode = Html.encode or error()
 local Db = require "site:/lib/Db.luan"
 local run_in_transaction = Db.run_in_transaction or error()
-local Ai = require "site:/lib/ai/Ai.luan"
+local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan"
 local languages = require "site:/lib/languages.luan"
 local Utils = require "site:/lib/Utils.luan"
 local get_first = Utils.get_first or error()
+local Course = require "site:/lib/Course.luan"
+local get_course_by_id = Course.get_by_id or error()
 
 
 local Chat = {}
@@ -25,7 +27,6 @@
 		updated = doc.chat_updated
 		course_id = doc.course_id
 		name = doc.name
-		ai_name = doc.ai_name
 		ai_thread = doc.ai_thread
 		language = doc.language
 		language_region = doc.language_region
@@ -40,8 +41,7 @@
 		chat_updated = long(chat.updated)
 		course_id = long(chat.course_id)
 		name = chat.name or error()
-		ai_name = chat.ai_name or error()
-		ai_thread = chat.ai_thread -- or error()
+		ai_thread = chat.ai_thread
 		language = chat.language or error()
 		language_region = chat.language_region or error()
 	}
@@ -53,8 +53,6 @@
 
 function Chat.new(chat)
 	chat.updated = chat.updated or time_now()
-	chat.ai_name = chat.ai_name or "claude"
-	chat.ai = Ai[chat.ai_name]["Ai_chat.luan"] or error()
 	chat.language_region = chat.language_region or first_region(chat.language)
 
 	function chat.save()
@@ -75,23 +73,38 @@
 		return html_encode(chat.name)
 	end
 
-	function chat.output_system_prompt()
-		chat.ai.output_system_prompt(chat.ai_thread)
-	end
-
-	function chat.output_messages_html()
-		chat.ai.output_messages_html(chat.language_region,chat.ai_thread)
-	end
-
-	function chat.ask(input)
-		local old_thread = chat.ai_thread
-		local ai_thread = chat.ai.ask(old_thread,input)
+	function chat.init_ai()  -- return if added message
+		if chat.ai_thread ~= nil then
+			return false
+		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 `chat.ai.output_messages_html(chat.language_region,ai_thread,old_thread)`
+		return ai_first_message ~= nil
+	end
+
+	function chat.output_system_prompt()
+		Ai_chat.output_system_prompt(chat.ai_thread)
+	end
+
+	function chat.output_messages_html()
+		Ai_chat.output_messages_html(chat.language_region,chat.ai_thread)
+	end
+
+	function chat.ask(input)
+		local old_thread = chat.ai_thread
+		local ai_thread = Ai_chat.ask_more(old_thread,input)
+		run_in_transaction( function()
+			chat = chat.reload()
+			chat.ai_thread = ai_thread
+			chat.save()
+		end )
+		return `Ai_chat.output_messages_html(chat.language_region,ai_thread,old_thread)`
 	end
 
 	function chat.language_name()